raz / commit 4acc715a

commit
4acc715aa8e79286229afc837fd51c316fe9b6c2
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
b8a03842
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%.

patch · log

8 files changed, +138 -32

PLAN.md (history) modified +4 -1

@@ -33,7 +33,10 @@
3333 3. Done: **Addresses and operands as the machine has them.** An element of a
3434 slice in registers is `[base + i*8]`, read where it is used; a
3535 compare or an addition takes its right operand from memory
36- (`cmp r8, [rbp - 16]`), not through a register first.
36+ (`cmp r8, [rbp - 16]`), not through a register first. Beyond the
37+ plan, as the sum stayed at 1.75 without them: `t := t + e` and a
38+ `for` loop's step are worked out in the variable's own register,
39+ and a `for` loop's top is aligned to 32 bytes.
3740 4. **`while` tested at the bottom**, its test moved after the body and
3841 jumped to first, as `for` is now. ulam found nothing in it, and lost
3942 3% where the test was long: measured, and kept only if it pays.

doc/compiler.md (history) modified +27 -0

@@ -360,6 +360,33 @@
360360 bytes were tried with it, and taken out: the search lost 3%, and
361361 rat gained nothing.
362362
363+ Then the left operand, as the machine has it: `t := t + s[i]` still
364+ went through a register of the stack,
365+
366+ ```
367+ mov r8, rbx
368+ add r8, [rdi + r12 * 8]
369+ mov rbx, r8
370+ ```
371+
372+ and `i`'s step as well. An assignment whose expression starts with
373+ its variable, in a register, and changes it by `+`, `-`, `|` or
374+ `xor`, naming it nowhere else (`ChangesItself`, read ahead in the
375+ statement), has gen work the value out in the variable's own
376+ register (`inPlace`, `lInto`), and `ForStep` steps one so too:
377+
378+ ```
379+ add r12, 1
380+ add rbx, [rdi + r12 * 8]
381+ ```
382+
383+ That left the sum's loop four instructions, as FPC's are, and as
384+ slow as before, for it crossed a 64-byte line that the processor
385+ fetches decoded instructions in. The top of a `for` loop, which
386+ the code jumps to rather than runs into, now starts at a multiple
387+ of 32 bytes (`AlignLoop`, `Align` in x64), the no-ops before it
388+ never run. The sum 41% faster, the search 6%; rat within noise.
389+
363390 ## diag.pas: messages
364391
365392 Every message the compiler prints is made here. A position, `TPos`, is

src/gen.pas modified +23 -2

@@ -113,6 +113,10 @@
113113 lost: boolean; { a parameter kept where it arrived was wanted
114114 for other work: the procedure is compiled again,
115115 the parameter in memory }
116+ inPlace: longint = -1; { the register of a variable that the statement
117+ being compiled assigns, x := x + e, where the
118+ value can be worked out in the variable's own
119+ register, from its first push on (lInto) }
116120 depth: longint; { the values on the stack }
117121 pushed: longint; { quadwords pushed on the machine's stack }
118122 codeChunk: longint = -1; { the procedure or body being compiled, or -1 }
@@ -156,6 +160,7 @@
156160 procedure JumpOut(op: TSym; signed: boolean; var L: longint);
157161 procedure CondValue(notOp: TSym; signed: boolean; fj, tj: longint);
158162 procedure JumpBack(top: longint);
163+procedure AlignLoop;
159164 procedure Fix(L: longint);
160165 procedure FixTo(L, top: longint);
161166 function JoinJumps(L1, L2: longint): longint;
@@ -212,6 +217,9 @@
212217 lMem, { not loaded: the value of size bytes at m, in the
213218 frame, a chunk, or at an address lAdr or lIdx
214219 noted, read where it is used (PushVar) }
220+ lInto, { in register val, a register variable's own, which
221+ is what the value goes to (inPlace): so it is
222+ changed in place }
215223 lReg, { in its register }
216224 lFrame); { spilled: in the frame slot val }
217225
@@ -349,6 +357,7 @@
349357 function Loaded(i: longint): longint;
350358 var r: longint;
351359 begin
360+ if stack[i].loc = lInto then exit(stack[i].val);
352361 if stack[i].loc <> lReg then begin
353362 r := Claim(i);
354363 case stack[i].loc of
@@ -432,7 +441,7 @@
432441 begin
433442 case stack[i].loc of
434443 lConst: MovRI(r, stack[i].val);
435- lHeld: MovRR(r, stack[i].val);
444+ lHeld, lInto: MovRR(r, stack[i].val);
436445 lAdr: Lea(r, AdrMem(i, 0));
437446 lIdx: Lea(r, stack[i].m);
438447 lMem: Load(r, stack[i].m, stack[i].size, stack[i].signed);
@@ -530,6 +539,12 @@
530539 procedure PushVar(const a: TAdr; size: longint; signed: boolean; k: TKind);
531540 var m: TMem; r: longint;
532541 begin
542+ r := inPlace;
543+ inPlace := -1;
544+ if (a.base = bReg) and (RegAt(a) = r) then begin
545+ NewEntry(k, lInto, r);
546+ exit;
547+ end;
533548 if a.base = bReg then begin
534549 NewEntry(k, lHeld, RegAt(a));
535550 exit;
@@ -563,7 +578,8 @@
563578 var r: longint; v: int64;
564579 begin
565580 if a.base = bReg then begin
566- if TopConst(v) then MovRI(RegAt(a), v) else MovRR(RegAt(a), Peek(depth - 1));
581+ if TopConst(v) then MovRI(RegAt(a), v)
582+ else if Peek(depth - 1) <> RegAt(a) then MovRR(RegAt(a), Peek(depth - 1)); { else it is there, lInto }
567583 Drop;
568584 exit;
569585 end;
@@ -1159,6 +1175,11 @@
11591175 procedure Fix(L: longint);
11601176 begin
11611177 FixLink(L);
1178+end;
1179+
1180+procedure AlignLoop;
1181+begin
1182+ Align(32);
11621183 end;
11631184
11641185 function JoinJumps(L1, L2: longint): longint;

src/item.pas modified +6 -0

@@ -1350,6 +1350,12 @@
13501350 var v, w: TItem;
13511351 begin
13521352 MakeItem(v, o);
1353+ if v.base = bReg then begin
1354+ { a variable in a register is stepped in it }
1355+ PushConst(kWord, step);
1356+ IncDecReg(v.chunk, IntSize(o^.typ), Signed(o^.typ), false);
1357+ exit;
1358+ end;
13531359 MakeItem(w, o);
13541360 Value(w);
13551361 PushConst(kWord, step);

src/parse.pas modified +30 -0

@@ -1370,6 +1370,7 @@
13701370 if step > 0 then ForCmp(o, b, sGtr, exitL) else ForCmp(o, b, sLss, exitL);
13711371 bodyL := NoLabel;
13721372 Jump(bodyL);
1373+ AlignLoop;
13731374 top := pc;
13741375 ForStep(o, step);
13751376 Fix(bodyL);
@@ -1646,6 +1647,33 @@
16461647 inc(numDefers);
16471648 end;
16481649
1650+{ Whether the expression of x := ..., which starts at the current
1651+ symbol, is x changed by adding to it, taking from it or its bits:
1652+ x + e, x - e, x | e or x xor e, with x nowhere in e, so that it can be
1653+ worked out in x's own register, if x is in one (gen's inPlace). }
1654+function ChangesItself(const x: TItem): boolean;
1655+var st: TScanState; wasSkipping: boolean; d: longint;
1656+begin
1657+ ChangesItself := false;
1658+ if (x.mode <> mVar) or (x.base <> bReg) or (x.typ^.form = fSlice) or (tok <> sIdent) or (id <> x.obj^.name) then exit;
1659+ SaveScan(st);
1660+ wasSkipping := skipping;
1661+ skipping := true; { its slips are told when it is compiled }
1662+ Next;
1663+ if tok in [sPlus, sMinus, sBar, sXor] then begin
1664+ ChangesItself := true;
1665+ d := 0;
1666+ while not ((d = 0) and (tok in [sSemicolon, sEnd])) and (tok <> sEof) do begin
1667+ if (tok = sIdent) and (id = x.obj^.name) then ChangesItself := false;
1668+ if tok in [sLparen, sLbrak, sLbrace] then inc(d)
1669+ else if tok in [sRparen, sRbrak, sRbrace] then dec(d);
1670+ Next;
1671+ end;
1672+ end;
1673+ RestoreScan(st);
1674+ skipping := wasSkipping;
1675+end;
1676+
16491677 procedure OneStatement;
16501678 var x, y: TItem; called: boolean; m: int64; c: TCall; start: TPos; first, n, i: longint; k: TKind;
16511679 begin
@@ -1661,7 +1689,9 @@
16611689 NotReadOnly(x);
16621690 Assignable(x, first);
16631691 Next;
1692+ if ChangesItself(x) then inPlace := x.chunk;
16641693 Expression(y, x.typ);
1694+ inPlace := -1;
16651695 Assign(x, y);
16661696 end else if tok = sComma then
16671697 MultiAssign(x, first, false)

src/x64.pas modified +18 -0

@@ -81,6 +81,7 @@
8181 procedure Syscall;
8282 procedure Mfence;
8383 procedure Ud2;
84+procedure Align(n: longint);
8485 procedure RepMovsb;
8586 procedure Std;
8687 procedure Cld;
@@ -562,6 +563,23 @@
562563 procedure Syscall; begin Say('syscall'); Put1($0F); Put1($05) end;
563564 procedure Mfence; begin Say('mfence'); Put1($0F); Put1($AE); Put1($F0) end;
564565 procedure Ud2; begin Say('ud2'); Put1($0F); Put1($0B) end;
566+
567+{ Code made up to a multiple of n bytes with no-ops, the fewest: the
568+ long forms of nop, of up to 8 bytes, that x86 decodes as one }
569+procedure Align(n: longint);
570+const
571+ Nops: array[1..8] of string[8] = (#$90, #$66#$90, #$0F#$1F#$00, #$0F#$1F#$40#$00,
572+ #$0F#$1F#$44#$00#$00, #$66#$0F#$1F#$44#$00#$00, #$0F#$1F#$80#$00#$00#$00#$00,
573+ #$0F#$1F#$84#$00#$00#$00#$00#$00);
574+var k, i: longint;
575+begin
576+ while pc mod n <> 0 do begin
577+ k := n - pc mod n;
578+ if k > 8 then k := 8;
579+ Say('nop');
580+ for i := 1 to k do Put1(ord(Nops[k][i]));
581+ end;
582+end;
565583 procedure RepMovsb; begin Say('rep movsb'); Put1($F3); Put1($A4) end;
566584 { the direction flag, which has rep movsb go down, and back up }
567585 procedure Std; begin Say('std'); Put1($FD) end;

test/enctest.pas modified +1 -0

@@ -28,6 +28,7 @@
2828 Ucomis(0, 1, false); Ucomis(0, 1, true); Cvtsi2s(0, RAX, false); Cvtsi2s(0, RAX, true);
2929 Cvtts2si(RAX, 0, false); Cvtts2si(RAX, 0, true); Btc(RAX, 63);
3030 L := NoLabel; Jcc(ccE, L); Jmp(L); FixLink(L); JmpBack(chunks^[e].start); JccBack(ccNE, chunks^[e].start);
31+ Ret; Align(32);
3132 EndChunk(e);
3233 MarkLive(e);
3334 WriteElf(ParamStr(1), e);

test/list/Listing.expect (history) modified +29 -29

@@ -28,34 +28,34 @@
2828 20 for i := 1 to 3 do
2929 17 mov rbx, 1
3030 22 cmp rbx, 3
31- 26 jg 141
32- 32 jmp 47
33- 37 mov r8, rbx
34- 40 add r8, 1
35- 44 mov rbx, r8
31+ 26 jg 156
32+ 32 jmp 68
33+ 37 nop
34+ 45 nop
35+ 53 nop
36+ 61 nop
37+ 64 add rbx, 1
3638 21 if i # 2 then total := add(total, twice(i)); end;
37- 47 cmp rbx, 2
38- 51 je 101
39- 57 mov r8, [Listing.total]
40- 64 mov [rbp - 16], r8
41- 68 mov rdi, rbx
42- 71 call Listing.twice
43- 76 mov r9, rax
44- 79 mov rdi, [rbp - 16]
45- 83 mov rsi, r9
46- 86 call Listing.add
47- 91 mov r8, rax
48- 94 mov [Listing.total], r8
49- 101 cmp rbx, 3
50- 105 jl 37
51- 111 cmp rbx, 3
52- 115 je 141
53- 121 mov r8, rbx
54- 124 add r8, 1
55- 128 mov rbx, r8
56- 131 cmp rbx, 3
57- 135 jle 47
39+ 68 cmp rbx, 2
40+ 72 je 122
41+ 78 mov r8, [Listing.total]
42+ 85 mov [rbp - 16], r8
43+ 89 mov rdi, rbx
44+ 92 call Listing.twice
45+ 97 mov r9, rax
46+ 100 mov rdi, [rbp - 16]
47+ 104 mov rsi, r9
48+ 107 call Listing.add
49+ 112 mov r8, rax
50+ 115 mov [Listing.total], r8
51+ 122 cmp rbx, 3
52+ 126 jl 64
53+ 132 cmp rbx, 3
54+ 136 je 156
55+ 142 add rbx, 1
56+ 146 cmp rbx, 3
57+ 150 jle 68
5858 23 end main;
59- 141 mov rbx, [rbp - 8]
60- 145 leave
61- 146 ret
59+ 156 mov rbx, [rbp - 8]
60+ 160 leave
61+ 161 ret