From 4acc715aa8e79286229afc837fd51c316fe9b6c2 Mon Sep 17 00:00:00 2001 From: Luis <42945842+ldiaz94@users.noreply.github.com> Date: Mon, 12 Oct 2026 18:18:26 +0100 Subject: [PATCH] Changed in place; a for loop's top aligned v0.8.0 step 3, second part. An assignment whose expression starts with its own variable, in a register, and changes it by +, -, | or xor, naming it nowhere else as the statement read ahead shows, has its value worked out in the variable's register (inPlace, lInto); a for loop steps its variable in its register. The sum's loop is four instructions, as FPC's: add r12, 1; add rbx, [rdi + r12 * 8]; cmp; jl. It ran no faster until the loop's top, which the code jumps to, was aligned to 32 bytes, so that the loop doesn't cross a 64-byte line; the no-ops before it never run. User-space cycles: sum -41%, search -6%, heap sort within noise; rat within noise (-4.5% to +1% over four runs), its instructions -1.4%, text -1.1%. --- diff --git a/PLAN.md b/PLAN.md index a69ffef..45cbbe6 100644 --- a/PLAN.md +++ b/PLAN.md @@ -33,7 +33,10 @@ 3. Done: **Addresses and operands as the machine has them.** An element of a slice in registers is `[base + i*8]`, read where it is used; a compare or an addition takes its right operand from memory - (`cmp r8, [rbp - 16]`), not through a register first. + (`cmp r8, [rbp - 16]`), not through a register first. Beyond the + plan, as the sum stayed at 1.75 without them: `t := t + e` and a + `for` loop's step are worked out in the variable's own register, + and a `for` loop's top is aligned to 32 bytes. 4. **`while` tested at the bottom**, its test moved after the body and jumped to first, as `for` is now. ulam found nothing in it, and lost 3% where the test was long: measured, and kept only if it pays. diff --git a/doc/compiler.md b/doc/compiler.md index 41305bc..0e3310a 100644 --- a/doc/compiler.md +++ b/doc/compiler.md @@ -360,6 +360,33 @@ bytes were tried with it, and taken out: the search lost 3%, and rat gained nothing. + Then the left operand, as the machine has it: `t := t + s[i]` still + went through a register of the stack, + + ``` + mov r8, rbx + add r8, [rdi + r12 * 8] + mov rbx, r8 + ``` + + and `i`'s step as well. An assignment whose expression starts with + its variable, in a register, and changes it by `+`, `-`, `|` or + `xor`, naming it nowhere else (`ChangesItself`, read ahead in the + statement), has gen work the value out in the variable's own + register (`inPlace`, `lInto`), and `ForStep` steps one so too: + + ``` + add r12, 1 + add rbx, [rdi + r12 * 8] + ``` + + That left the sum's loop four instructions, as FPC's are, and as + slow as before, for it crossed a 64-byte line that the processor + fetches decoded instructions in. The top of a `for` loop, which + the code jumps to rather than runs into, now starts at a multiple + of 32 bytes (`AlignLoop`, `Align` in x64), the no-ops before it + never run. The sum 41% faster, the search 6%; rat within noise. + ## diag.pas: messages Every message the compiler prints is made here. A position, `TPos`, is diff --git a/src/gen.pas b/src/gen.pas index def1fdf..27acef7 100644 --- a/src/gen.pas +++ b/src/gen.pas @@ -113,6 +113,10 @@ lost: boolean; { a parameter kept where it arrived was wanted for other work: the procedure is compiled again, the parameter in memory } + inPlace: longint = -1; { the register of a variable that the statement + being compiled assigns, x := x + e, where the + value can be worked out in the variable's own + register, from its first push on (lInto) } depth: longint; { the values on the stack } pushed: longint; { quadwords pushed on the machine's stack } codeChunk: longint = -1; { the procedure or body being compiled, or -1 } @@ -156,6 +160,7 @@ procedure JumpOut(op: TSym; signed: boolean; var L: longint); procedure CondValue(notOp: TSym; signed: boolean; fj, tj: longint); procedure JumpBack(top: longint); +procedure AlignLoop; procedure Fix(L: longint); procedure FixTo(L, top: longint); function JoinJumps(L1, L2: longint): longint; @@ -212,6 +217,9 @@ lMem, { not loaded: the value of size bytes at m, in the frame, a chunk, or at an address lAdr or lIdx noted, read where it is used (PushVar) } + lInto, { in register val, a register variable's own, which + is what the value goes to (inPlace): so it is + changed in place } lReg, { in its register } lFrame); { spilled: in the frame slot val } @@ -349,6 +357,7 @@ function Loaded(i: longint): longint; var r: longint; begin + if stack[i].loc = lInto then exit(stack[i].val); if stack[i].loc <> lReg then begin r := Claim(i); case stack[i].loc of @@ -432,7 +441,7 @@ begin case stack[i].loc of lConst: MovRI(r, stack[i].val); - lHeld: MovRR(r, stack[i].val); + lHeld, lInto: MovRR(r, stack[i].val); lAdr: Lea(r, AdrMem(i, 0)); lIdx: Lea(r, stack[i].m); lMem: Load(r, stack[i].m, stack[i].size, stack[i].signed); @@ -530,6 +539,12 @@ procedure PushVar(const a: TAdr; size: longint; signed: boolean; k: TKind); var m: TMem; r: longint; begin + r := inPlace; + inPlace := -1; + if (a.base = bReg) and (RegAt(a) = r) then begin + NewEntry(k, lInto, r); + exit; + end; if a.base = bReg then begin NewEntry(k, lHeld, RegAt(a)); exit; @@ -563,7 +578,8 @@ var r: longint; v: int64; begin if a.base = bReg then begin - if TopConst(v) then MovRI(RegAt(a), v) else MovRR(RegAt(a), Peek(depth - 1)); + if TopConst(v) then MovRI(RegAt(a), v) + else if Peek(depth - 1) <> RegAt(a) then MovRR(RegAt(a), Peek(depth - 1)); { else it is there, lInto } Drop; exit; end; @@ -1159,6 +1175,11 @@ procedure Fix(L: longint); begin FixLink(L); +end; + +procedure AlignLoop; +begin + Align(32); end; function JoinJumps(L1, L2: longint): longint; diff --git a/src/item.pas b/src/item.pas index 1f71f2a..360ff33 100644 --- a/src/item.pas +++ b/src/item.pas @@ -1350,6 +1350,12 @@ var v, w: TItem; begin MakeItem(v, o); + if v.base = bReg then begin + { a variable in a register is stepped in it } + PushConst(kWord, step); + IncDecReg(v.chunk, IntSize(o^.typ), Signed(o^.typ), false); + exit; + end; MakeItem(w, o); Value(w); PushConst(kWord, step); diff --git a/src/parse.pas b/src/parse.pas index 84dbc11..27ef574 100644 --- a/src/parse.pas +++ b/src/parse.pas @@ -1370,6 +1370,7 @@ if step > 0 then ForCmp(o, b, sGtr, exitL) else ForCmp(o, b, sLss, exitL); bodyL := NoLabel; Jump(bodyL); + AlignLoop; top := pc; ForStep(o, step); Fix(bodyL); @@ -1646,6 +1647,33 @@ inc(numDefers); end; +{ Whether the expression of x := ..., which starts at the current + symbol, is x changed by adding to it, taking from it or its bits: + x + e, x - e, x | e or x xor e, with x nowhere in e, so that it can be + worked out in x's own register, if x is in one (gen's inPlace). } +function ChangesItself(const x: TItem): boolean; +var st: TScanState; wasSkipping: boolean; d: longint; +begin + ChangesItself := false; + if (x.mode <> mVar) or (x.base <> bReg) or (x.typ^.form = fSlice) or (tok <> sIdent) or (id <> x.obj^.name) then exit; + SaveScan(st); + wasSkipping := skipping; + skipping := true; { its slips are told when it is compiled } + Next; + if tok in [sPlus, sMinus, sBar, sXor] then begin + ChangesItself := true; + d := 0; + while not ((d = 0) and (tok in [sSemicolon, sEnd])) and (tok <> sEof) do begin + if (tok = sIdent) and (id = x.obj^.name) then ChangesItself := false; + if tok in [sLparen, sLbrak, sLbrace] then inc(d) + else if tok in [sRparen, sRbrak, sRbrace] then dec(d); + Next; + end; + end; + RestoreScan(st); + skipping := wasSkipping; +end; + procedure OneStatement; var x, y: TItem; called: boolean; m: int64; c: TCall; start: TPos; first, n, i: longint; k: TKind; begin @@ -1661,7 +1689,9 @@ NotReadOnly(x); Assignable(x, first); Next; + if ChangesItself(x) then inPlace := x.chunk; Expression(y, x.typ); + inPlace := -1; Assign(x, y); end else if tok = sComma then MultiAssign(x, first, false) diff --git a/src/x64.pas b/src/x64.pas index 4c72ce9..4502cf3 100644 --- a/src/x64.pas +++ b/src/x64.pas @@ -81,6 +81,7 @@ procedure Syscall; procedure Mfence; procedure Ud2; +procedure Align(n: longint); procedure RepMovsb; procedure Std; procedure Cld; @@ -562,6 +563,23 @@ procedure Syscall; begin Say('syscall'); Put1($0F); Put1($05) end; procedure Mfence; begin Say('mfence'); Put1($0F); Put1($AE); Put1($F0) end; procedure Ud2; begin Say('ud2'); Put1($0F); Put1($0B) end; + +{ Code made up to a multiple of n bytes with no-ops, the fewest: the + long forms of nop, of up to 8 bytes, that x86 decodes as one } +procedure Align(n: longint); +const + Nops: array[1..8] of string[8] = (#$90, #$66#$90, #$0F#$1F#$00, #$0F#$1F#$40#$00, + #$0F#$1F#$44#$00#$00, #$66#$0F#$1F#$44#$00#$00, #$0F#$1F#$80#$00#$00#$00#$00, + #$0F#$1F#$84#$00#$00#$00#$00#$00); +var k, i: longint; +begin + while pc mod n <> 0 do begin + k := n - pc mod n; + if k > 8 then k := 8; + Say('nop'); + for i := 1 to k do Put1(ord(Nops[k][i])); + end; +end; procedure RepMovsb; begin Say('rep movsb'); Put1($F3); Put1($A4) end; { the direction flag, which has rep movsb go down, and back up } procedure Std; begin Say('std'); Put1($FD) end; diff --git a/test/enctest.pas b/test/enctest.pas index 1ab67c1..b732391 100644 --- a/test/enctest.pas +++ b/test/enctest.pas @@ -28,6 +28,7 @@ Ucomis(0, 1, false); Ucomis(0, 1, true); Cvtsi2s(0, RAX, false); Cvtsi2s(0, RAX, true); Cvtts2si(RAX, 0, false); Cvtts2si(RAX, 0, true); Btc(RAX, 63); L := NoLabel; Jcc(ccE, L); Jmp(L); FixLink(L); JmpBack(chunks^[e].start); JccBack(ccNE, chunks^[e].start); + Ret; Align(32); EndChunk(e); MarkLive(e); WriteElf(ParamStr(1), e); diff --git a/test/list/Listing.expect b/test/list/Listing.expect index 0a6c659..45a3618 100644 --- a/test/list/Listing.expect +++ b/test/list/Listing.expect @@ -28,34 +28,34 @@ 20 for i := 1 to 3 do 17 mov rbx, 1 22 cmp rbx, 3 - 26 jg 141 - 32 jmp 47 - 37 mov r8, rbx - 40 add r8, 1 - 44 mov rbx, r8 + 26 jg 156 + 32 jmp 68 + 37 nop + 45 nop + 53 nop + 61 nop + 64 add rbx, 1 21 if i # 2 then total := add(total, twice(i)); end; - 47 cmp rbx, 2 - 51 je 101 - 57 mov r8, [Listing.total] - 64 mov [rbp - 16], r8 - 68 mov rdi, rbx - 71 call Listing.twice - 76 mov r9, rax - 79 mov rdi, [rbp - 16] - 83 mov rsi, r9 - 86 call Listing.add - 91 mov r8, rax - 94 mov [Listing.total], r8 - 101 cmp rbx, 3 - 105 jl 37 - 111 cmp rbx, 3 - 115 je 141 - 121 mov r8, rbx - 124 add r8, 1 - 128 mov rbx, r8 - 131 cmp rbx, 3 - 135 jle 47 + 68 cmp rbx, 2 + 72 je 122 + 78 mov r8, [Listing.total] + 85 mov [rbp - 16], r8 + 89 mov rdi, rbx + 92 call Listing.twice + 97 mov r9, rax + 100 mov rdi, [rbp - 16] + 104 mov rsi, r9 + 107 call Listing.add + 112 mov r8, rax + 115 mov [Listing.total], r8 + 122 cmp rbx, 3 + 126 jl 64 + 132 cmp rbx, 3 + 136 je 156 + 142 add rbx, 1 + 146 cmp rbx, 3 + 150 jle 68 23 end main; - 141 mov rbx, [rbp - 8] - 145 leave - 146 ret + 156 mov rbx, [rbp - 8] + 160 leave + 161 ret