raz / commit 3c704507

commit
3c704507dd98660616834427ecbd8c2115438539
author
Luis <42945842+ldiaz94@users.noreply.github.com>
date
parent
4d68ba95
A for loop's limit in a register left free

A limit that isn't a constant was put in a frame slot and compared
there each round. chooseRegs now notes, reading ahead, the for loops
with none inside them whose limit names something not a constant; if a
variable register is left when the variables have theirs, it keeps it
for them (limitK, saved as a variable's), and forStatement puts each
such limit in a variable no one can name, in that register.

The sum 15% faster in the same instructions. Search 4% slower, its loop
unchanged and at the same address.

patch · log

2 files changed, +77 -6

doc/compiler.md (history) modified +29 -1

@@ -20,7 +20,7 @@
2020 | `src/X64.raz` | 1,517 | 1,253 | The instruction encoder, jumps within a procedure, the assembler |
2121 | `src/Gen.raz` | 2,506 | 1,794 | The stack machine, for x86-64: values, operations, checks, calls, frames |
2222 | `src/Item.raz` | 1,767 | 1,462 | Items: types, constants, variables, operators, conversions, calls |
23-| `src/Parse.raz` | 3,853 | 3,361 | Declarations, statements, expressions, built-ins, recovery, warnings |
23+| `src/Parse.raz` | 3,896 | 3,395 | Declarations, statements, expressions, built-ins, recovery, warnings |
2424 | `src/Link.raz` | 381 | 298 | Tree shaking, layout, patching, the places |
2525 | `src/Elf.raz` | 431 | 344 | Static and dynamic ELF, symbol table, line numbers |
2626 | `src/Raz.raz` | 305 | 238 | The command line |
@@ -807,6 +807,34 @@
807807 NBody 25% fewer instructions, 16% faster. Grep is 5% slower with
808808 the same code in its loops, only moved 96 bytes: where a loop falls
809809 changes its speed, as v0.8.0 found.
810+
811+3. **A loop's limit in a register.** `for i := 0 to LEN(s) - 1`
812+ works out its limit once, in a frame slot, and compared `i` with
813+ the slot each round:
814+
815+ ```
816+ add r12, 1
817+ add rbx, [rdi + r12 * 8]
818+ cmp r12, [rbp - 40]
819+ jl top
820+ ```
821+
822+ `chooseRegs`, reading ahead, notes the `for` loops with no `for`
823+ inside them whose limit names something not a constant (`limitIn`,
824+ by their order in the procedure). If one of the five variable
825+ registers is left when the variables have theirs, it is kept for
826+ those limits (`limitK`; `regsOf` saves it as a variable's), and
827+ `forStatement` puts each such limit in a variable no one can name,
828+ in that register, which `forCmp` and `forNext` compare as any:
829+
830+ ```
831+ cmp r12, r13
832+ jl top
833+ ```
834+
835+ The sum 15% faster, in the same instructions. Search, whose `for` is
836+ compared once a round of its inner `while`, is 4% slower in a loop
837+ that didn't change, at the same address; why is not known.
810838
811839 ## Diag.raz: messages
812840

src/Parse.raz (history) modified +48 -5

@@ -67,6 +67,7 @@
6767 MaxElements = 1024; (* elements of an array literal *)
6868 MaxFixes = 4096; (* addresses in one constant *)
6969 MaxPending = 64; (* modules being compiled at once *)
70+ MaxFors = 256; (* for loops of a procedure whose limit may go in a register *)
7071 Suffixes: [4][]U8 = [".linux-x86_64", ".linux", ".x86_64", ""];
7172
7273 var
@@ -113,6 +114,13 @@
113114 their end: each way through them returns *)
114115 useAt: Diag.Pos; (* ... which is here *)
115116 guards: INT; (* the recovery points open *)
117+ (* A variable register no variable took, for the limits of the
118+ procedure's innermost for loops that aren't constant: compared
119+ each round, they would be read from the frame. limitK is its place
120+ in VarRegs, or -1; limitIn says which for loops, by their order
121+ in the procedure (chooseRegs); numFors counts them as compiled. *)
122+ limitK, numFors: INT;
123+ limitIn: [MaxFors]BOOL;
116124
117125 proc expression(var x: TItem; want: PType): ERROR; forward;
118126 proc parseType: (PType, ERROR); forward;
@@ -1713,9 +1721,11 @@
17131721 so that a round of a loop by 1 takes one compare and one jump. *)
17141722 proc forStatement(start: Diag.Pos; ind: INT): ERROR;
17151723 var
1716- o: PObj; v, a, b, s: TItem; step: I64; top, body, bodyL, exitL: INT;
1724+ o, lim: PObj; v, a, b, s: TItem; step: I64; top, body, bodyL, exitL, n: INT;
17171725 lp: Loop; old: PLoop; f: Sym.Form; slot: I64;
17181726 begin
1727+ n := numFors;
1728+ INC(numFors);
17191729 Scan.next;
17201730 if Scan.tok # Scan.sIdent then return expected("a name"); end;
17211731 o := Sym.lookup(Scan.id);
@@ -1738,8 +1748,17 @@
17381748 try expect(Scan.sTo);
17391749 try expression(b, o^.typ);
17401750 try Item.coerceExact(b, o^.typ, "");
1741- (* the limit, evaluated once: a constant as it is, else in a slot *)
1742- if b.mode # Item.mConst then
1751+ (* the limit, evaluated once: a constant as it is; else, in an
1752+ innermost loop, in the register chooseRegs kept for it, as a
1753+ variable no one can name; else in a slot *)
1754+ if (b.mode # Item.mConst) and (limitK >= 0) and (n < MaxFors) and limitIn[n] then
1755+ lim := Sym.newObj(Sym.oVar, o^.name, b.typ);
1756+ lim^.lev := 1;
1757+ lim^.reg := Gen.VarRegs[limitK];
1758+ try Item.makeItem(v, lim);
1759+ try Item.assign(v, b);
1760+ b := v;
1761+ elsif b.mode # Item.mConst then
17431762 slot := try Item.toSlot(b);
17441763 Item.tempItem(b, b.typ, slot);
17451764 end;
@@ -2465,10 +2484,12 @@
24652484 proc chooseRegs(params, before: PObj);
24662485 var
24672486 isLoop: [256]BOOL;
2468- i, k, kx, d, loops, best, need: INT; o, q, qual: PObj; st: Scan.State;
2487+ forAt: [256]INT; (* the for loop opened at each depth, or -1 *)
2488+ i, k, kx, d, loops, best, need, nf, inLim: INT; o, q, qual: PObj; st: Scan.State;
24692489 afterDot, wasSkipping, leaf: BOOL;
24702490 begin
24712491 numCand := 0;
2492+ limitK := -1; nf := 0; inLim := -1;
24722493 o := params;
24732494 while o # nil do addCandidate(o); o := o^.next; end;
24742495 o := firstLocal(before);
@@ -2498,7 +2519,19 @@
24982519 INC(d);
24992520 isLoop[d] := (Scan.tok = Scan.sWhile) or (Scan.tok = Scan.sFor) or (Scan.tok = Scan.sRepeat);
25002521 if isLoop[d] then INC(loops); end;
2522+ forAt[d] := -1;
2523+ if (Scan.tok = Scan.sFor) and (nf < MaxFors) then
2524+ (* innermost until a for is found inside it; its
2525+ limit constant until a name in it isn't one *)
2526+ forAt[d] := nf; limitIn[nf] := false;
2527+ i := d - 1;
2528+ while (i > 0) and (forAt[i] < 0) do DEC(i); end;
2529+ if i > 0 then limitIn[forAt[i]] := false; end;
2530+ INC(nf);
2531+ end;
25012532 end;
2533+ | sTo: if (d > 0) and (forAt[d] >= 0) then inLim := forAt[d]; end;
2534+ | sDo, sBy: inLim := -1;
25022535 | sEnd, sUntil:
25032536 (* an end with no begin before it, in a body the parser will
25042537 report, leaves d at 0 *)
@@ -2517,6 +2550,10 @@
25172550 k := candidate(Scan.id);
25182551 if loops > 2 then INC(weight[k], 64); else INC(weight[k], 1 shl (2 * loops)); end;
25192552 end;
2553+ if (inLim >= 0) and not afterDot then
2554+ q := Sym.lookup(Scan.id);
2555+ if (q = nil) or (q^.cls # Sym.oConst) then limitIn[inLim] := true; end;
2556+ end;
25202557 | else:
25212558 end;
25222559 afterDot := Scan.tok = Scan.sPeriod; (* a name after a dot is a field, not a variable *)
@@ -2563,6 +2600,9 @@
25632600 weight[best] := 0; (* too big for what is left *)
25642601 end;
25652602 until false;
2603+ for i := 0 to nf - 1 do
2604+ if limitIn[i] and (k < 5) then limitK := k; end;
2605+ end;
25662606 end;
25672607
25682608 (* The registers the variables of the procedure are in, as bits: bit i
@@ -2594,6 +2634,7 @@
25942634 if o^.cls = Sym.oVar then m := m | regBits(o); end;
25952635 o := o^.chain;
25962636 end;
2637+ if limitK >= 0 then m := m | (1 shl limitK); end;
25972638 return m;
25982639 end;
25992640
@@ -2663,7 +2704,7 @@
26632704 try expect(Scan.sBegin);
26642705 curResult := sig^.base;
26652706 Gen.enterBody(p^.chunk, line);
2666- stray := false; elseIf := false; deferSlot := 0;
2707+ stray := false; elseIf := false; deferSlot := 0; numFors := 0;
26672708 try statements();
26682709 p^.openEnd := (curResult # nil) and not ends;
26692710 Chunk.noteLine(Scan.curLine(), Scan.curCol());
@@ -2697,6 +2738,7 @@
26972738 end;
26982739 curResult := old;
26992740 curProc := nil;
2741+ limitK := -1;
27002742 Sym.closeScope();
27012743 return nil;
27022744 end;
@@ -3841,6 +3883,7 @@
38413883 end;
38423884
38433885 begin
3886+ limitK := -1;
38443887 nameRt := internName("Rt");
38453888 nameFail := internName("fail");
38463889 nameAdr := internName("ADR");