raz / commit b8a03842

commit
b8a03842be4b5190e2981c24958c7dbd2565a36e
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
654579f9
Addresses and operands as the machine has them

An element of a slice in registers is noted as the operand [pointer +
index * size] (lIdx), and a variable in memory, or at an address
noted, as the operand that reads it (lMem): each is read or written
where it is used, and add, sub, and, or, xor and cmp take an 8-byte
one as their right operand as it is. A value noted in memory is read
before a call or an atomic built-in, which could change it.

Measured in user-space cycles and instructions, fewest of nine runs:
heap sort -14% cycles (-45% instructions), search -5% (-21%), sum
-0% (-38%); rat within noise, +1 to +2% cycles, -2.5% instructions,
text -3.9%. Loop tops aligned to 16 bytes were tried and taken out:
the search lost 3%, rat gained nothing.

patch · log

3 files changed, +134 -19

PLAN.md (history) modified +1 -1

@@ -30,7 +30,7 @@
3030 and no register is saved for it. Most of rat's hot procedures are
3131 such small ones (`unsplit`, `Width.cells`), and each call now saves
3232 and restores what it uses.
33-3. **Addresses and operands as the machine has them.** An element of a
33+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
3636 (`cmp r8, [rbp - 16]`), not through a register first.

doc/compiler.md (history) modified +37 -2

@@ -328,6 +328,38 @@
328328 kernel saved. The search 9% faster, rat 1.5%, its text 0.7%
329329 smaller.
330330
331+3. **Addresses and operands as the machine has them.** `t := t + s[i]`
332+ worked out the element's address, read the element into a register,
333+ and added that:
334+
335+ ```
336+ mov r9, r12 ; i
337+ shl r9, 3
338+ add r9, rdi ; s's pointer
339+ mov r9, [r9]
340+ mov r8, rbx ; t
341+ add r8, r9
342+ ```
343+
344+ Now an element of a slice in registers is only noted, as the
345+ operand `[rdi + r12 * 8]` (`lIdx`, made by `IndexSlice`), and a
346+ variable in memory, or at an address noted, as the operand that
347+ reads it (`lMem`, made by `PushVar`): each is read where it is
348+ used, and `Op`, `Compare` and `JumpCmp` take an 8-byte one as their
349+ right operand as it is (`TopInMem`):
350+
351+ ```
352+ mov r8, rbx
353+ add r8, [rdi + r12 * 8]
354+ ```
355+
356+ A value noted in memory must be read before anything can change
357+ it: before a call (`SpillBelow`) and an atomic built-in (`Atomic`),
358+ both through `ReadMems`. The heap sort 14% faster, the search 5%;
359+ rat within noise, its text 3.9% smaller. Loop tops aligned to 16
360+ bytes were tried with it, and taken out: the search lost 3%, and
361+ rat gained nothing.
362+
331363 ## diag.pas: messages
332364
333365 Every message the compiler prints is made here. A position, `TPos`, is
@@ -505,10 +537,13 @@
505537 instruction.
506538
507539 The stack: `stack` notes where each value is, a constant (`lConst`), an
508-address not yet computed (`lAdr`, `AdrMem`'s operand), a register
540+address not yet computed (`lAdr`, `AdrMem`'s operand), an element's
541+address not yet computed (`lIdx`, an operand `[base + index * size]`),
542+a variable's value not yet read (`lMem`, at an operand), a register
509543 variable's own register, read in place (`lHeld`), the value's own
510544 register (`lReg`) or a frame slot (`lFrame`); `owner` says which
511-value each register holds. The value at depth `d` belongs in
545+value each register holds, `lIdx` and `lMem` holding theirs when it
546+is their index. The value at depth `d` belongs in
512547 `Regs[d mod 4]`, `r8` to `r11`: `rax`, `rdx` and `rcx`, which division
513548 and shifts need, and `rsi` and `rdi`, which copying needs, aren't among
514549 them. `Claim` gives a value its register, and `Spill`s the lower one

src/gen.pas modified +96 -16

@@ -205,6 +205,13 @@
205205 nothing in an expression can change }
206206 lAdr, { not loaded: the address of chunk, or of the frame
207207 if chunk is -1, plus val }
208+ lIdx, { not loaded: the address m, an element of a slice
209+ in registers: its pointer's register as the base,
210+ and the index in a register variable's or the
211+ value's own register (IndexSlice) }
212+ lMem, { not loaded: the value of size bytes at m, in the
213+ frame, a chunk, or at an address lAdr or lIdx
214+ noted, read where it is used (PushVar) }
208215 lReg, { in its register }
209216 lFrame); { spilled: in the frame slot val }
210217
@@ -213,6 +220,9 @@
213220 val: int64; { lConst: the value; lHeld: the register; lAdr: the
214221 offset; lFrame: the frame slot }
215222 chunk: longint; { lAdr: the chunk, or -1 for the frame }
223+ m: TMem; { lIdx, lMem }
224+ size: longint; { lMem }
225+ signed: boolean; { lMem }
216226 k: TKind;
217227 end;
218228
@@ -283,10 +293,14 @@
283293 RegOf := Regs[i mod NumRegs];
284294 end;
285295
286-{ The value i, in a register, goes to a frame slot }
296+function Loaded(i: longint): longint; forward;
297+
298+{ The value i, in a register, goes to a frame slot; one noted that
299+ holds its register, as an index, is worked out in it first }
287300 procedure Spill(i: longint);
288301 var s: int64;
289302 begin
303+ if stack[i].loc <> lReg then Loaded(i);
290304 s := AllocTemp(8, 8);
291305 Store(Mem(RBP, s), RegOf(i), 8);
292306 stack[i].loc := lFrame;
@@ -294,10 +308,21 @@
294308 owner[i mod NumRegs] := -1;
295309 end;
296310
297-{ Every value below depth d in a register, spilled }
311+{ Each value below depth d read from memory now, as what comes next
312+ may change it }
313+procedure ReadMems(d: longint);
314+var i: longint;
315+begin
316+ for i := 0 to d - 1 do
317+ if stack[i].loc = lMem then Loaded(i);
318+end;
319+
320+{ Every value below depth d in a register, spilled; one noted in memory
321+ is read first, as a call may change it }
298322 procedure SpillBelow(d: longint);
299323 var r: longint;
300324 begin
325+ ReadMems(d);
301326 for r := 0 to NumRegs - 1 do
302327 if (owner[r] >= 0) and (owner[r] < d) then Spill(owner[r]);
303328 end;
@@ -326,10 +351,14 @@
326351 begin
327352 if stack[i].loc <> lReg then begin
328353 r := Claim(i);
329- if stack[i].loc = lConst then MovRI(r, stack[i].val)
330- else if stack[i].loc = lHeld then MovRR(r, stack[i].val)
331- else if stack[i].loc = lAdr then Lea(r, AdrMem(i, 0))
354+ case stack[i].loc of
355+ lConst: MovRI(r, stack[i].val);
356+ lHeld: MovRR(r, stack[i].val);
357+ lAdr: Lea(r, AdrMem(i, 0));
358+ lIdx: Lea(r, stack[i].m);
359+ lMem: Load(r, stack[i].m, stack[i].size, stack[i].signed);
332360 else Load(r, Mem(RBP, stack[i].val), 8, false);
361+ end;
333362 stack[i].loc := lReg;
334363 end;
335364 Loaded := RegOf(i);
@@ -358,6 +387,18 @@
358387 inc(depth);
359388 end;
360389
390+{ An address, lIdx, or a value at it, lMem, noted as the operand m. Its
391+ index may be in the register of the value just dropped, at this
392+ depth: it is the new value's own, then. }
393+procedure NewMem(loc: TLoc; const m: TMem; k: TKind; size: longint; signed: boolean);
394+begin
395+ NewEntry(k, loc, 0);
396+ stack[depth - 1].m := m;
397+ stack[depth - 1].size := size;
398+ stack[depth - 1].signed := signed;
399+ if m.index = RegOf(depth - 1) then owner[(depth - 1) mod NumRegs] := depth - 1;
400+end;
401+
361402 { The register a new value on top is to be computed in }
362403 function Target: longint;
363404 begin
@@ -373,7 +414,7 @@
373414 procedure Drop;
374415 begin
375416 dec(depth);
376- if stack[depth].loc = lReg then owner[depth mod NumRegs] := -1;
417+ if owner[depth mod NumRegs] = depth then owner[depth mod NumRegs] := -1;
377418 end;
378419
379420 { rsi and rdi are about to be used for other work, as in copying: a
@@ -393,6 +434,8 @@
393434 lConst: MovRI(r, stack[i].val);
394435 lHeld: MovRR(r, stack[i].val);
395436 lAdr: Lea(r, AdrMem(i, 0));
437+ lIdx: Lea(r, stack[i].m);
438+ lMem: Load(r, stack[i].m, stack[i].size, stack[i].signed);
396439 lReg: if RegOf(i) <> r then MovRR(r, RegOf(i));
397440 else Load(r, Mem(RBP, stack[i].val), 8, false);
398441 end;
@@ -450,6 +493,10 @@
450493 if stack[depth - 1].loc = lAdr then begin
451494 Place := AdrMem(depth - 1, a.off);
452495 Drop;
496+ end else if stack[depth - 1].loc = lIdx then begin
497+ Place := stack[depth - 1].m;
498+ Place.disp := Place.disp + a.off;
499+ Drop;
453500 end else begin
454501 ra := Peek(depth - 1);
455502 Drop;
@@ -487,6 +534,13 @@
487534 NewEntry(k, lHeld, RegAt(a));
488535 exit;
489536 end;
537+ { a variable in the frame or a chunk, or at an address only noted, is
538+ only noted too: read where it is used, as an operand if it can be }
539+ if (a.base in [bFrame, bGlobal]) or (a.base = bStack) and (stack[depth - 1].loc in [lAdr, lIdx]) then begin
540+ m := Place(a, -1);
541+ NewMem(lMem, m, k, size, signed);
542+ exit;
543+ end;
490544 { in the order PushAdr explains }
491545 if a.base = bStack then begin m := Place(a, -1); r := Target end
492546 else begin r := Target; m := Place(a, r) end;
@@ -529,6 +583,10 @@
529583 var e: TEntry; x, y: longint; rx, ry: boolean;
530584 begin
531585 x := depth - 2; y := depth - 1;
586+ { a value noted at an address that uses its register is worked out,
587+ so that its register moves with it }
588+ if stack[x].loc in [lIdx, lMem] then Loaded(x);
589+ if stack[y].loc in [lIdx, lMem] then Loaded(y);
532590 rx := stack[x].loc = lReg;
533591 ry := stack[y].loc = lReg;
534592 if rx and ry then begin
@@ -672,6 +730,13 @@
672730 FixLink(L);
673731 end;
674732
733+{ Whether the value on top is a word in memory that can be the right
734+ operand of an instruction as it is: add r, [m] }
735+function TopInMem: boolean;
736+begin
737+ TopInMem := (stack[depth - 1].loc = lMem) and (stack[depth - 1].size = 8);
738+end;
739+
675740 { The right operand of the two values on top, popped, in register y:
676741 its own, or rcx for a constant; the left one stays, in its register,
677742 which is returned. }
@@ -714,7 +779,7 @@
714779 { The two values on top, the left op the right, an integer of size
715780 bytes, signed or not, brought back to its width. }
716781 procedure Op(op: TSym; k: TKind; size: longint; signed: boolean);
717-var v: int64; r, y, sh, bits: longint; c: boolean;
782+var v: int64; r, y, sh, bits: longint; c: boolean; m: TMem;
718783 begin
719784 if k in [kF32, kF64] then begin RealOp(op, k); exit end;
720785 bits := size * 8;
@@ -738,6 +803,11 @@
738803 sShr: if signed then ShiftI(shSar, r, v and (bits - 1)) else ShiftI(shShr, r, v and (bits - 1));
739804 else AluI(AluOf(op), r, v);
740805 end;
806+ end else if TopInMem and (op in [sPlus, sMinus, sAmp, sBar, sXor]) then begin
807+ m := stack[depth - 1].m;
808+ Drop;
809+ r := Top;
810+ AluM(AluOf(op), r, m);
741811 end else begin
742812 r := Operands(y);
743813 case op of
@@ -772,7 +842,7 @@
772842 const
773843 CcSigned: array[sEql..sGeq] of longint = (ccE, ccNE, ccL, ccLE, ccG, ccGE);
774844 CcUnsigned: array[sEql..sGeq] of longint = (ccE, ccNE, ccB, ccBE, ccA, ccAE);
775-var f: boolean; r, y: longint; v: int64;
845+var f: boolean; r, y: longint; v: int64; m: TMem;
776846 begin
777847 if k in [kF32, kF64] then begin
778848 f := k = kF32;
@@ -795,6 +865,7 @@
795865 end;
796866 end else begin
797867 if TopConst(v) then begin Drop; r := Top; CmpI(r, v) end
868+ else if TopInMem then begin m := stack[depth - 1].m; Drop; r := Top; AluM(aluCmp, r, m) end
798869 else begin r := Operands(y); Alu(aluCmp, r, y) end;
799870 if signed then SetCC(CcSigned[op], r) else SetCC(CcUnsigned[op], r);
800871 end;
@@ -979,6 +1050,7 @@
9791050 procedure Atomic(which: longint);
9801051 var r, n: longint;
9811052 begin
1053+ ReadMems(depth - 2 - ord(which = bCas)); { what it writes may be one }
9821054 if which = bCas then begin n := Top; Drop end;
9831055 PopTo(RAX); { cmpxchg compares with rax, so CAS's old value goes there }
9841056 r := Top;
@@ -1001,12 +1073,7 @@
10011073 begin
10021074 ClobberArgs;
10031075 SpillBelow(depth);
1004- for i := 0 to n - 1 do
1005- with stack[depth - n + i] do
1006- if loc = lConst then MovRI(SysRegs[i], val)
1007- else if loc = lHeld then MovRR(SysRegs[i], val)
1008- else if loc = lAdr then Lea(SysRegs[i], AdrMem(depth - n + i, 0))
1009- else Load(SysRegs[i], Mem(RBP, val), 8, false);
1076+ for i := 0 to n - 1 do ValueTo(depth - n + i, SysRegs[i]);
10101077 for i := 1 to n do Drop;
10111078 x64.Syscall;
10121079 MovRR(Target, RAX);
@@ -1040,10 +1107,11 @@
10401107 const
10411108 CcSigned: array[sEql..sGeq] of longint = (ccE, ccNE, ccL, ccLE, ccG, ccGE);
10421109 CcUnsigned: array[sEql..sGeq] of longint = (ccE, ccNE, ccB, ccBE, ccA, ccAE);
1043-var r, y: longint; v: int64;
1110+var r, y: longint; v: int64; m: TMem;
10441111 begin
10451112 { a comparison changes neither: a variable's register will do }
10461113 if TopConst(v) then begin Drop; r := Peek(depth - 1); CmpI(r, v) end
1114+ else if TopInMem then begin m := stack[depth - 1].m; Drop; r := Peek(depth - 1); AluM(aluCmp, r, m) end
10471115 else begin y := Peek(depth - 1); Drop; r := Peek(depth - 1); Alu(aluCmp, r, y) end;
10481116 Drop;
10491117 if signed then Jcc(CcSigned[op], L) else Jcc(CcUnsigned[op], L);
@@ -1212,10 +1280,22 @@
12121280 address; but if the slice's address is on the stack, under the
12131281 index, the element's register is the one that holds that, still
12141282 needed, so the work is done in rax. A slice in registers is
1215- compared with and added to them. }
1283+ compared with and added to them; for an element of 1, 2, 4 or 8
1284+ bytes, its address is only noted, as the operand [pointer + index *
1285+ size], to be read or written where it is used (lIdx). }
12161286 procedure IndexSlice(const a: TAdr; size: int64; checked: boolean; const p: TPos);
12171287 var m, ml: TMem; r: longint; inRax: boolean;
12181288 begin
1289+ if (a.base = bReg) and (size in [1, 2, 4, 8]) then begin
1290+ r := Peek(depth - 1);
1291+ if checked then begin
1292+ Alu(aluCmp, r, a.hi);
1293+ Check(ccB, 0, p, r, InRegister(a.hi), 0, -1);
1294+ end;
1295+ Drop;
1296+ NewMem(lIdx, MemIdx(a.chunk, r, size, 0), kWord, 8, false);
1297+ exit;
1298+ end;
12191299 inRax := a.base = bStack;
12201300 if inRax then begin r := RAX; PopTo(RAX) end
12211301 else begin r := Top; Drop end;