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%.
| @@ -33,7 +33,10 @@ | ||
| 33 | 33 | 3. Done: **Addresses and operands as the machine has them.** An element of a |
| 34 | 34 | slice in registers is `[base + i*8]`, read where it is used; a |
| 35 | 35 | 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. | |
| 37 | 40 | 4. **`while` tested at the bottom**, its test moved after the body and |
| 38 | 41 | jumped to first, as `for` is now. ulam found nothing in it, and lost |
| 39 | 42 | 3% where the test was long: measured, and kept only if it pays. |
| @@ -360,6 +360,33 @@ | ||
| 360 | 360 | bytes were tried with it, and taken out: the search lost 3%, and |
| 361 | 361 | rat gained nothing. |
| 362 | 362 | |
| 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 | + | |
| 363 | 390 | ## diag.pas: messages |
| 364 | 391 | |
| 365 | 392 | Every message the compiler prints is made here. A position, `TPos`, is |
| @@ -113,6 +113,10 @@ | ||
| 113 | 113 | lost: boolean; { a parameter kept where it arrived was wanted |
| 114 | 114 | for other work: the procedure is compiled again, |
| 115 | 115 | 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) } | |
| 116 | 120 | depth: longint; { the values on the stack } |
| 117 | 121 | pushed: longint; { quadwords pushed on the machine's stack } |
| 118 | 122 | codeChunk: longint = -1; { the procedure or body being compiled, or -1 } |
| @@ -156,6 +160,7 @@ | ||
| 156 | 160 | procedure JumpOut(op: TSym; signed: boolean; var L: longint); |
| 157 | 161 | procedure CondValue(notOp: TSym; signed: boolean; fj, tj: longint); |
| 158 | 162 | procedure JumpBack(top: longint); |
| 163 | +procedure AlignLoop; | |
| 159 | 164 | procedure Fix(L: longint); |
| 160 | 165 | procedure FixTo(L, top: longint); |
| 161 | 166 | function JoinJumps(L1, L2: longint): longint; |
| @@ -212,6 +217,9 @@ | ||
| 212 | 217 | lMem, { not loaded: the value of size bytes at m, in the |
| 213 | 218 | frame, a chunk, or at an address lAdr or lIdx |
| 214 | 219 | 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 } | |
| 215 | 223 | lReg, { in its register } |
| 216 | 224 | lFrame); { spilled: in the frame slot val } |
| 217 | 225 | |
| @@ -349,6 +357,7 @@ | ||
| 349 | 357 | function Loaded(i: longint): longint; |
| 350 | 358 | var r: longint; |
| 351 | 359 | begin |
| 360 | + if stack[i].loc = lInto then exit(stack[i].val); | |
| 352 | 361 | if stack[i].loc <> lReg then begin |
| 353 | 362 | r := Claim(i); |
| 354 | 363 | case stack[i].loc of |
| @@ -432,7 +441,7 @@ | ||
| 432 | 441 | begin |
| 433 | 442 | case stack[i].loc of |
| 434 | 443 | lConst: MovRI(r, stack[i].val); |
| 435 | - lHeld: MovRR(r, stack[i].val); | |
| 444 | + lHeld, lInto: MovRR(r, stack[i].val); | |
| 436 | 445 | lAdr: Lea(r, AdrMem(i, 0)); |
| 437 | 446 | lIdx: Lea(r, stack[i].m); |
| 438 | 447 | lMem: Load(r, stack[i].m, stack[i].size, stack[i].signed); |
| @@ -530,6 +539,12 @@ | ||
| 530 | 539 | procedure PushVar(const a: TAdr; size: longint; signed: boolean; k: TKind); |
| 531 | 540 | var m: TMem; r: longint; |
| 532 | 541 | 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; | |
| 533 | 548 | if a.base = bReg then begin |
| 534 | 549 | NewEntry(k, lHeld, RegAt(a)); |
| 535 | 550 | exit; |
| @@ -563,7 +578,8 @@ | ||
| 563 | 578 | var r: longint; v: int64; |
| 564 | 579 | begin |
| 565 | 580 | 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 } | |
| 567 | 583 | Drop; |
| 568 | 584 | exit; |
| 569 | 585 | end; |
| @@ -1159,6 +1175,11 @@ | ||
| 1159 | 1175 | procedure Fix(L: longint); |
| 1160 | 1176 | begin |
| 1161 | 1177 | FixLink(L); |
| 1178 | +end; | |
| 1179 | + | |
| 1180 | +procedure AlignLoop; | |
| 1181 | +begin | |
| 1182 | + Align(32); | |
| 1162 | 1183 | end; |
| 1163 | 1184 | |
| 1164 | 1185 | function JoinJumps(L1, L2: longint): longint; |
| @@ -1350,6 +1350,12 @@ | ||
| 1350 | 1350 | var v, w: TItem; |
| 1351 | 1351 | begin |
| 1352 | 1352 | 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; | |
| 1353 | 1359 | MakeItem(w, o); |
| 1354 | 1360 | Value(w); |
| 1355 | 1361 | PushConst(kWord, step); |
| @@ -1370,6 +1370,7 @@ | ||
| 1370 | 1370 | if step > 0 then ForCmp(o, b, sGtr, exitL) else ForCmp(o, b, sLss, exitL); |
| 1371 | 1371 | bodyL := NoLabel; |
| 1372 | 1372 | Jump(bodyL); |
| 1373 | + AlignLoop; | |
| 1373 | 1374 | top := pc; |
| 1374 | 1375 | ForStep(o, step); |
| 1375 | 1376 | Fix(bodyL); |
| @@ -1646,6 +1647,33 @@ | ||
| 1646 | 1647 | inc(numDefers); |
| 1647 | 1648 | end; |
| 1648 | 1649 | |
| 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 | + | |
| 1649 | 1677 | procedure OneStatement; |
| 1650 | 1678 | var x, y: TItem; called: boolean; m: int64; c: TCall; start: TPos; first, n, i: longint; k: TKind; |
| 1651 | 1679 | begin |
| @@ -1661,7 +1689,9 @@ | ||
| 1661 | 1689 | NotReadOnly(x); |
| 1662 | 1690 | Assignable(x, first); |
| 1663 | 1691 | Next; |
| 1692 | + if ChangesItself(x) then inPlace := x.chunk; | |
| 1664 | 1693 | Expression(y, x.typ); |
| 1694 | + inPlace := -1; | |
| 1665 | 1695 | Assign(x, y); |
| 1666 | 1696 | end else if tok = sComma then |
| 1667 | 1697 | MultiAssign(x, first, false) |
| @@ -81,6 +81,7 @@ | ||
| 81 | 81 | procedure Syscall; |
| 82 | 82 | procedure Mfence; |
| 83 | 83 | procedure Ud2; |
| 84 | +procedure Align(n: longint); | |
| 84 | 85 | procedure RepMovsb; |
| 85 | 86 | procedure Std; |
| 86 | 87 | procedure Cld; |
| @@ -562,6 +563,23 @@ | ||
| 562 | 563 | procedure Syscall; begin Say('syscall'); Put1($0F); Put1($05) end; |
| 563 | 564 | procedure Mfence; begin Say('mfence'); Put1($0F); Put1($AE); Put1($F0) end; |
| 564 | 565 | 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; | |
| 565 | 583 | procedure RepMovsb; begin Say('rep movsb'); Put1($F3); Put1($A4) end; |
| 566 | 584 | { the direction flag, which has rep movsb go down, and back up } |
| 567 | 585 | procedure Std; begin Say('std'); Put1($FD) end; |
| @@ -28,6 +28,7 @@ | ||
| 28 | 28 | Ucomis(0, 1, false); Ucomis(0, 1, true); Cvtsi2s(0, RAX, false); Cvtsi2s(0, RAX, true); |
| 29 | 29 | Cvtts2si(RAX, 0, false); Cvtts2si(RAX, 0, true); Btc(RAX, 63); |
| 30 | 30 | L := NoLabel; Jcc(ccE, L); Jmp(L); FixLink(L); JmpBack(chunks^[e].start); JccBack(ccNE, chunks^[e].start); |
| 31 | + Ret; Align(32); | |
| 31 | 32 | EndChunk(e); |
| 32 | 33 | MarkLive(e); |
| 33 | 34 | WriteElf(ParamStr(1), e); |
| @@ -28,34 +28,34 @@ | ||
| 28 | 28 | 20 for i := 1 to 3 do |
| 29 | 29 | 17 mov rbx, 1 |
| 30 | 30 | 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 | |
| 36 | 38 | 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 | |
| 58 | 58 | 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 |