Shifts for sizes, constants to the right v0.7.0 step 5. An index into a slice, and a slice's bounds, are multiplied by the element's size with a shift where it is a power of two (MulI), one cycle where imul takes three, on the chain of every load through an index. A constant on the left of + * & | xor goes right, where it is an immediate or a shift: 2 * root + 1 is shl, add. compiler.md's worked example is what the compiler makes now. rat: within noise (504 -> 506 ms), text -0.2%; a heap sort and a walk down a list of records 11% faster (116 -> 103 ms).
| @@ -13,7 +13,7 @@ | ||
| 13 | 13 | | `src/sym.pas` | 643 | 527 | Types, objects, scopes, layout | |
| 14 | 14 | | `src/chunk.pas` | 318 | 246 | The compiled program: chunks, fixups, line notes | |
| 15 | 15 | | `src/x64.pas` | 1,125 | 936 | The instruction encoder, jumps within a procedure, the assembler | |
| 16 | -| `src/gen.pas` | 1,653 | 1,284 | The stack machine, for x86-64: values, operations, checks, calls, frames | | |
| 16 | +| `src/gen.pas` | 1,665 | 1,291 | The stack machine, for x86-64: values, operations, checks, calls, frames | | |
| 17 | 17 | | `src/item.pas` | 1,493 | 1,247 | Items: types, constants, variables, operators, conversions, calls | |
| 18 | 18 | | `src/parse.pas` | 2,957 | 2,609 | Declarations, statements, expressions, built-ins, recovery, warnings | |
| 19 | 19 | | `src/link.pas` | 247 | 190 | Tree shaking, layout, patching, the places | |
| @@ -109,11 +109,8 @@ | ||
| 109 | 109 | mov r9,QWORD PTR [rbp-0x10] ; b, at depth 1; the 2 is only noted |
| 110 | 110 | shl r9,0x1 ; times 2 is a shift |
| 111 | 111 | add r8,r9 |
| 112 | -mov rax,r8 ; the result | |
| 113 | -jmp <leave> ; return | |
| 114 | -xor eax,eax ; running off the end returns zero | |
| 115 | -movq xmm0,rax | |
| 116 | -leave | |
| 112 | +mov rax,r8 ; return: the result | |
| 113 | +leave ; no jump to here, as it is next | |
| 117 | 114 | ret |
| 118 | 115 | ``` |
| 119 | 116 | |
| @@ -317,8 +314,11 @@ | ||
| 317 | 314 | (`JumpOut`), which both ways to its label must find in the same place. `pushed` counts what |
| 318 | 315 | is on the machine's stack, which a call must align: the return |
| 319 | 316 | address, in a deferred statement. `Op` takes a |
| 320 | -constant on the right as it is, and a power of two on the right of | |
| 321 | -`*`, `div` or `mod` becomes shifts (`DivPow2`), since `idiv` is slow. | |
| 317 | +constant on the right as it is, after moving one on the left of `+`, | |
| 318 | +`*`, `&`, `|` or `xor` there, and a power of two on the right of `*`, | |
| 319 | +`div` or `mod` becomes shifts (`DivPow2`), since `idiv` is slow; an | |
| 320 | +element's size multiplies an index by a shift too (`MulI`), as a shift | |
| 321 | +takes one cycle and `imul` three. | |
| 322 | 322 | Narrow integers are brought back to their width after each operation, |
| 323 | 323 | which is how `U8` arithmetic wraps; since they are computed in 64 bits, |
| 324 | 324 | a shift's count is cut to their width first, and `DivTrap` makes the |
| @@ -548,6 +548,14 @@ | ||
| 548 | 548 | Log2 := n; |
| 549 | 549 | end; |
| 550 | 550 | |
| 551 | +{ r times v, a size: by a shift where v is a power of two, as it | |
| 552 | + mostly is, since a shift takes one cycle and imul three } | |
| 553 | +procedure MulI(r: longint; v: int64); | |
| 554 | +begin | |
| 555 | + if Log2(v) > 0 then ShiftI(shShl, r, Log2(v)) | |
| 556 | + else if v <> 1 then ImulI(r, v); | |
| 557 | +end; | |
| 558 | + | |
| 551 | 559 | { r compared with v, through rdx if v needs more than 32 bits; with |
| 552 | 560 | zero by test, which sets the flags as cmp would and is shorter } |
| 553 | 561 | procedure CmpI(r: longint; v: int64); |
| @@ -637,6 +645,10 @@ | ||
| 637 | 645 | begin |
| 638 | 646 | if k in [kF32, kF64] then begin RealOp(op, k); exit end; |
| 639 | 647 | bits := size * 8; |
| 648 | + { a constant on the left of an operation whose order doesn't matter | |
| 649 | + goes right, where it can be an immediate or a shift } | |
| 650 | + if (op in [sPlus, sTimes, sAmp, sBar, sXor]) and (stack[depth - 2].loc = lConst) | |
| 651 | + and (stack[depth - 1].loc <> lConst) then Swap; | |
| 640 | 652 | c := TopConst(v); |
| 641 | 653 | sh := -1; |
| 642 | 654 | if c and (op in [sTimes, sDiv, sMod]) then sh := Log2(v); |
| @@ -1096,7 +1108,7 @@ | ||
| 1096 | 1108 | AluM(aluCmp, r, ml); |
| 1097 | 1109 | Check(ccB, 0, p, r, ml, 0, -1); |
| 1098 | 1110 | end; |
| 1099 | - if size <> 1 then ImulI(r, size); | |
| 1111 | + MulI(r, size); | |
| 1100 | 1112 | AluM(aluAdd, r, m); |
| 1101 | 1113 | if inRax then MovRR(Target, RAX) |
| 1102 | 1114 | else Claim(depth); { the index's register again, now for the address } |
| @@ -1123,7 +1135,7 @@ | ||
| 1123 | 1135 | MovRR(RAX, RDX); |
| 1124 | 1136 | Alu(aluSub, RAX, RCX); |
| 1125 | 1137 | Store(Mem(RBP, tmp + 8), RAX, 8); |
| 1126 | - if size <> 1 then ImulI(RCX, size); | |
| 1138 | + MulI(RCX, size); | |
| 1127 | 1139 | if isArray then Lea(RAX, m) else Load(RAX, m, 8, false); |
| 1128 | 1140 | Alu(aluAdd, RAX, RCX); |
| 1129 | 1141 | Store(Mem(RBP, tmp), RAX, 8); |
| @@ -1144,7 +1156,7 @@ | ||
| 1144 | 1156 | Check(ccBE, 1, p, RAX, ml, 0, -1); |
| 1145 | 1157 | end; |
| 1146 | 1158 | MovRR(RCX, RDX); |
| 1147 | - if size <> 1 then ImulI(RCX, size); | |
| 1159 | + MulI(RCX, size); | |
| 1148 | 1160 | Load(RSI, Mem(RBP, sOff), 8, false); |
| 1149 | 1161 | Load(RDI, Mem(RBP, dOff), 8, false); |
| 1150 | 1162 | Alu(aluCmp, RDI, RSI); |
| @@ -1176,7 +1188,7 @@ | ||
| 1176 | 1188 | MovRR(RAX, RDX); |
| 1177 | 1189 | Alu(aluSub, RAX, RCX); |
| 1178 | 1190 | Store(Mem(RBP, tmp + 8), RAX, 8); |
| 1179 | - if size <> 1 then ImulI(RCX, size); | |
| 1191 | + MulI(RCX, size); | |
| 1180 | 1192 | Alu(aluAdd, RCX, RDI); |
| 1181 | 1193 | Store(Mem(RBP, tmp), RCX, 8); |
| 1182 | 1194 | end; |