Slices in registers A slice whose address isn't taken may have two of the variables' registers, its pointer's and its length's (reg2), chosen by the read-ahead as a single value is, its count halved for each register: counted whole, a slice took registers from busier single values, and rat ran 3% slower. Its item names both registers: PushVar reads either word, IndexSlice and Slice use them as they are, a failed check takes the length from its register, CopyTo loads both, and what wants its memory has its words stored in a temporary. rat 583 -> 557 ms (-4.5%), text -0.1%; heap sort -5%, search -4%, sum within noise; make check compiles test/bench.
| @@ -19,7 +19,7 @@ | ||
| 19 | 19 | Each step is measured as v0.7.0's were, on rat and on its kernel, and |
| 20 | 20 | one that buys less than 2% on both is reverted and noted here. |
| 21 | 21 | |
| 22 | -1. **Slices in registers.** A slice parameter or variable whose address | |
| 22 | +1. Done: **Slices in registers.** A slice parameter or variable whose address | |
| 23 | 23 | isn't taken lives in two of the variables' registers, its pointer and |
| 24 | 24 | its length, chosen by the read-ahead as a single value is, and |
| 25 | 25 | counted as two. FPC's search keeps its slice in `rdi` and `rsi`; |
| @@ -262,6 +262,48 @@ | ||
| 262 | 262 | though this path uses one of them. Each could go, at the cost of lines |
| 263 | 263 | a reader would have to follow. |
| 264 | 264 | |
| 265 | +## Better code again, in v0.8.0 | |
| 266 | + | |
| 267 | +Each change below was measured on rat, a terminal emulator written in | |
| 268 | +raz, running its 26 MB workload, and on the kernels of `test/bench`, | |
| 269 | +against their twins built by FPC `-O2` (`make kernels`); one that | |
| 270 | +bought less than 2% on both was taken out again. | |
| 271 | + | |
| 272 | +1. **Slices in registers.** `sum(s: []INT)` kept its slice in the | |
| 273 | + frame, and read its pointer from there for each element: | |
| 274 | + | |
| 275 | + ``` | |
| 276 | + mov [rbp - 16], rdi ; the prologue: s, to its slots | |
| 277 | + mov [rbp - 8], rsi | |
| 278 | + ... | |
| 279 | + mov r9, r14 ; s[i], each time round | |
| 280 | + shl r9, 3 | |
| 281 | + add r9, [rbp - 16] | |
| 282 | + ``` | |
| 283 | + | |
| 284 | + Now a slice whose address isn't taken can have two of the | |
| 285 | + variables' registers, its pointer's and its length's | |
| 286 | + (`o^.reg2`), chosen as a single value's is but for half its weight | |
| 287 | + in each: | |
| 288 | + | |
| 289 | + ``` | |
| 290 | + mov r12, rdi | |
| 291 | + mov r13, rsi | |
| 292 | + ... | |
| 293 | + mov r9, r14 | |
| 294 | + shl r9, 3 | |
| 295 | + add r9, r12 | |
| 296 | + ``` | |
| 297 | + | |
| 298 | + Its item is at `bReg`, and its `TAdr` names both registers (`hi` | |
| 299 | + for the length's): `PushVar` reads either word (`RegAt`), | |
| 300 | + `IndexSlice` and `Slice` compare with the length's and add the | |
| 301 | + pointer's, a failed check takes the length from its register | |
| 302 | + (`InRegister`), and `CopyTo` loads both. What wants a slice's | |
| 303 | + memory, a comparison or `COPY`, has its words stored in a | |
| 304 | + temporary first (`CopyToTemp`). rat 5% faster, the heap sort 5%, | |
| 305 | + the search 4%. | |
| 306 | + | |
| 265 | 307 | ## diag.pas: messages |
| 266 | 308 | |
| 267 | 309 | Every message the compiler prints is made here. A position, `TPos`, is |
| @@ -532,7 +574,8 @@ | ||
| 532 | 574 | first. The |
| 533 | 575 | prologue saves them, zeroes them, and moves a parameter into its |
| 534 | 576 | register from the register or stack slot it arrived in; the epilogue |
| 535 | -restores them. A variable in one is an | |
| 577 | +restores them. A slice may have two, its pointer's and its length's | |
| 578 | +(see "Better code again" above). A variable in one is an | |
| 536 | 579 | item at `bReg`: `PushVar` notes it (`lHeld`) rather than copying it, |
| 537 | 580 | `Peek` reads it in place where the value is only read, so `i < n` is |
| 538 | 581 | `cmp rbx, r12`, and `PopVar` and `IncDecReg` write it. Rt finds the |
| @@ -726,7 +769,8 @@ | ||
| 726 | 769 | the body ahead, as `InstancesFor` does, and counts each variable's |
| 727 | 770 | uses: 1 for each, 4 in a loop, 16 in two, at most 64 (loops nested three |
| 728 | 771 | deep or more); one `ADR` takes is out. The |
| 729 | -five busiest used at least three times get registers (`o^.reg`). An | |
| 772 | +busiest used at least three times get the five registers (`o^.reg`), | |
| 773 | +a slice two (`o^.reg2` too), its count halved for each. An | |
| 730 | 774 | address wanted after all, by a `var` parameter, say, comes to |
| 731 | 775 | `Address` (`item.pas`), which puts the variable back in memory and |
| 732 | 776 | sets `demoted`: `Body` then throws the procedure's code away |
| @@ -862,7 +906,8 @@ | ||
| 862 | 906 | `_ := f(x)` drops one on purpose. `-Werror` makes each an error. |
| 863 | 907 | - **Registers for variables.** Up to five of a procedure's single |
| 864 | 908 | values, not reals, whose address is never taken, live in `rbx` and |
| 865 | - `r12` to `r15`; the trap report shows them as it shows the others. | |
| 909 | + `r12` to `r15`, a slice taking two of them; the trap report shows | |
| 910 | + them as it shows the others, but for slices, which it never shows. | |
| 866 | 911 | - **Several results** that are two words, and a slice, come back in |
| 867 | 912 | rax and rdx, as from a code procedure; others through a hidden |
| 868 | 913 | pointer to the caller's slots. |
| @@ -70,7 +70,9 @@ | ||
| 70 | 70 | TAdr = record |
| 71 | 71 | base: TBase; |
| 72 | 72 | chunk: longint; { bGlobal: the chunk; bSlot: the frame slot; bReg: the register } |
| 73 | - off: int64; { added to the address } | |
| 73 | + off: int64; { added to the address; for bReg, which word, 0 or 8 } | |
| 74 | + hi: longint; { bReg, a slice: the register of its length, the | |
| 75 | + second word; chunk is that of its pointer } | |
| 74 | 76 | end; |
| 75 | 77 | |
| 76 | 78 | { The code made so far, to go back to for a second try at a body } |
| @@ -411,6 +413,13 @@ | ||
| 411 | 413 | Fits32 := (v >= low(longint)) and (v <= high(longint)); |
| 412 | 414 | end; |
| 413 | 415 | |
| 416 | +{ The register of the word at a, a variable in registers: the variable's | |
| 417 | + own, or for a slice its pointer's (off 0) or its length's (off 8) } | |
| 418 | +function RegAt(const a: TAdr): longint; | |
| 419 | +begin | |
| 420 | + if a.off = 0 then RegAt := a.chunk else RegAt := a.hi; | |
| 421 | +end; | |
| 422 | + | |
| 414 | 423 | { The memory a names. An address on the stack is popped, and is in its |
| 415 | 424 | register still; one in a slot is loaded into r. } |
| 416 | 425 | function Place(const a: TAdr; r: longint): TMem; |
| @@ -458,7 +467,7 @@ | ||
| 458 | 467 | var m: TMem; r: longint; |
| 459 | 468 | begin |
| 460 | 469 | if a.base = bReg then begin |
| 461 | - NewEntry(k, lHeld, a.chunk); | |
| 470 | + NewEntry(k, lHeld, RegAt(a)); | |
| 462 | 471 | exit; |
| 463 | 472 | end; |
| 464 | 473 | { in the order PushAdr explains } |
| @@ -483,7 +492,7 @@ | ||
| 483 | 492 | var r: longint; v: int64; |
| 484 | 493 | begin |
| 485 | 494 | if a.base = bReg then begin |
| 486 | - if TopConst(v) then MovRI(a.chunk, v) else MovRR(a.chunk, Peek(depth - 1)); | |
| 495 | + if TopConst(v) then MovRI(RegAt(a), v) else MovRR(RegAt(a), Peek(depth - 1)); | |
| 487 | 496 | Drop; |
| 488 | 497 | exit; |
| 489 | 498 | end; |
| @@ -553,10 +562,16 @@ | ||
| 553 | 562 | CopyBytes(size); |
| 554 | 563 | end; |
| 555 | 564 | |
| 556 | -{ Pops a source's address, and copies size bytes to a. } | |
| 565 | +{ Pops a source's address, and copies size bytes to a: for a slice in | |
| 566 | + registers, its two words into them. } | |
| 557 | 567 | procedure CopyTo(const a: TAdr; size: int64); |
| 558 | 568 | begin |
| 559 | 569 | PopTo(RSI); |
| 570 | + if a.base = bReg then begin | |
| 571 | + Load(a.chunk, Mem(RSI, 0), 8, false); | |
| 572 | + Load(a.hi, Mem(RSI, 8), 8, false); | |
| 573 | + exit; | |
| 574 | + end; | |
| 560 | 575 | Lea(RDI, Place(a, RDI)); |
| 561 | 576 | CopyBytes(size); |
| 562 | 577 | end; |
| @@ -1069,6 +1084,7 @@ | ||
| 1069 | 1084 | |
| 1070 | 1085 | const |
| 1071 | 1086 | NoBase = -2; { a TMem that is no memory } |
| 1087 | + ARegister = -3; { a TMem that is no memory but a register, its index } | |
| 1072 | 1088 | |
| 1073 | 1089 | { The program goes on if the condition cc holds, and otherwise stops |
| 1074 | 1090 | in Rt.fail(what, a, b, len, at, name), which says what failed at p, |
| @@ -1110,7 +1126,9 @@ | ||
| 1110 | 1126 | LineCol(at, line, col); |
| 1111 | 1127 | NoteLine(line, col); |
| 1112 | 1128 | if ra <> RSI then MovRR(RSI, ra); |
| 1113 | - if m.base = NoBase then MovRI(RCX, n) else Load(RCX, m, 8, false); | |
| 1129 | + if m.base = NoBase then MovRI(RCX, n) | |
| 1130 | + else if m.base = ARegister then MovRR(RCX, m.index) | |
| 1131 | + else Load(RCX, m, 8, false); | |
| 1114 | 1132 | if name >= 0 then Lea(R9, MemRip(name, 0)); |
| 1115 | 1133 | if failStubs[what] < 0 then failStubs[what] := NewChunk(secText, Intern('Rt', 2), Intern(@StubNames[what][1], 6)); |
| 1116 | 1134 | CallChunk(failStubs[what]); |
| @@ -1122,6 +1140,13 @@ | ||
| 1122 | 1140 | function NoMem: TMem; |
| 1123 | 1141 | begin |
| 1124 | 1142 | NoMem := Mem(NoBase, 0); |
| 1143 | +end; | |
| 1144 | + | |
| 1145 | +{ For Check: a length in register r, a slice's in registers } | |
| 1146 | +function InRegister(r: longint): TMem; | |
| 1147 | +begin | |
| 1148 | + InRegister := Mem(ARegister, 0); | |
| 1149 | + InRegister.index := r; | |
| 1125 | 1150 | end; |
| 1126 | 1151 | |
| 1127 | 1152 | { The index on top is below n, unsigned } |
| @@ -1160,21 +1185,31 @@ | ||
| 1160 | 1185 | It is worked out in the index's own register, which then holds the |
| 1161 | 1186 | address; but if the slice's address is on the stack, under the |
| 1162 | 1187 | index, the element's register is the one that holds that, still |
| 1163 | - needed, so the work is done in rax. } | |
| 1188 | + needed, so the work is done in rax. A slice in registers is | |
| 1189 | + compared with and added to them. } | |
| 1164 | 1190 | procedure IndexSlice(const a: TAdr; size: int64; checked: boolean; const p: TPos); |
| 1165 | 1191 | var m, ml: TMem; r: longint; inRax: boolean; |
| 1166 | 1192 | begin |
| 1167 | 1193 | inRax := a.base = bStack; |
| 1168 | 1194 | if inRax then begin r := RAX; PopTo(RAX) end |
| 1169 | 1195 | else begin r := Top; Drop end; |
| 1170 | - m := Place(a, RCX); | |
| 1171 | - ml := m; ml.disp := ml.disp + 8; | |
| 1172 | - if checked then begin | |
| 1173 | - AluM(aluCmp, r, ml); | |
| 1174 | - Check(ccB, 0, p, r, ml, 0, -1); | |
| 1196 | + if a.base = bReg then begin | |
| 1197 | + if checked then begin | |
| 1198 | + Alu(aluCmp, r, a.hi); | |
| 1199 | + Check(ccB, 0, p, r, InRegister(a.hi), 0, -1); | |
| 1200 | + end; | |
| 1201 | + MulI(r, size); | |
| 1202 | + Alu(aluAdd, r, a.chunk); | |
| 1203 | + end else begin | |
| 1204 | + m := Place(a, RCX); | |
| 1205 | + ml := m; ml.disp := ml.disp + 8; | |
| 1206 | + if checked then begin | |
| 1207 | + AluM(aluCmp, r, ml); | |
| 1208 | + Check(ccB, 0, p, r, ml, 0, -1); | |
| 1209 | + end; | |
| 1210 | + MulI(r, size); | |
| 1211 | + AluM(aluAdd, r, m); | |
| 1175 | 1212 | end; |
| 1176 | - MulI(r, size); | |
| 1177 | - AluM(aluAdd, r, m); | |
| 1178 | 1213 | if inRax then MovRR(Target, RAX) |
| 1179 | 1214 | else Claim(depth); { the index's register again, now for the address } |
| 1180 | 1215 | Computed(kWord); |
| @@ -1188,12 +1223,16 @@ | ||
| 1188 | 1223 | begin |
| 1189 | 1224 | PopTo(RDX); |
| 1190 | 1225 | PopTo(RCX); |
| 1191 | - m := Place(a, RDI); | |
| 1192 | - ml := m; ml.disp := ml.disp + 8; | |
| 1226 | + if a.base = bReg then ml := InRegister(a.hi) { a slice in registers } | |
| 1227 | + else begin | |
| 1228 | + m := Place(a, RDI); | |
| 1229 | + ml := m; ml.disp := ml.disp + 8; | |
| 1230 | + end; | |
| 1193 | 1231 | if checked then begin |
| 1194 | 1232 | { two checks, both unsigned, so that a negative bound fails too: |
| 1195 | 1233 | hi against the length, then lo against hi } |
| 1196 | 1234 | if isArray then begin CmpI(RDX, n); ml := NoMem end |
| 1235 | + else if a.base = bReg then Alu(aluCmp, RDX, a.hi) | |
| 1197 | 1236 | else AluM(aluCmp, RDX, ml); |
| 1198 | 1237 | Check(ccBE, 1, p, RCX, ml, n, -1); |
| 1199 | 1238 | Alu(aluCmp, RCX, RDX); |
| @@ -1203,7 +1242,9 @@ | ||
| 1203 | 1242 | Alu(aluSub, RAX, RCX); |
| 1204 | 1243 | Store(Mem(RBP, tmp + 8), RAX, 8); |
| 1205 | 1244 | MulI(RCX, size); |
| 1206 | - if isArray then Lea(RAX, m) else Load(RAX, m, 8, false); | |
| 1245 | + if isArray then Lea(RAX, m) | |
| 1246 | + else if a.base = bReg then MovRR(RAX, a.chunk) | |
| 1247 | + else Load(RAX, m, 8, false); | |
| 1207 | 1248 | Alu(aluAdd, RAX, RCX); |
| 1208 | 1249 | Store(Mem(RBP, tmp), RAX, 8); |
| 1209 | 1250 | end; |
| @@ -1441,7 +1482,7 @@ | ||
| 1441 | 1482 | p^.val := AllocPerm(16, 8); |
| 1442 | 1483 | for k := 0 to 1 do begin |
| 1443 | 1484 | inReg[numIn] := IntRegs[ni]; inSlot[numIn] := p^.val + 8 * k; |
| 1444 | - inSize[numIn] := 8; inObj[numIn] := nil; | |
| 1485 | + inSize[numIn] := 8; inObj[numIn] := p; | |
| 1445 | 1486 | inc(numIn); inc(ni); |
| 1446 | 1487 | end; |
| 1447 | 1488 | end else begin |
| @@ -1513,7 +1554,7 @@ | ||
| 1513 | 1554 | registers of variables are zeroed in place, their save slots are |
| 1514 | 1555 | written before they are read, and so is every temporary. } |
| 1515 | 1556 | procedure EnterBody(c, line: longint); |
| 1516 | -var i, top: longint; n: int64; p: PObj; | |
| 1557 | +var i, top, r: longint; n: int64; p: PObj; | |
| 1517 | 1558 | begin |
| 1518 | 1559 | BeginChunk(c); |
| 1519 | 1560 | codeChunk := c; |
| @@ -1550,12 +1591,15 @@ | ||
| 1550 | 1591 | inc(n); |
| 1551 | 1592 | end; |
| 1552 | 1593 | { the parameters that came in registers, to their slots or, for a |
| 1553 | - register variable, to its register } | |
| 1594 | + register variable, to its register: a slice's second word, the one | |
| 1595 | + for the slot 8 bytes into its own, to its length's } | |
| 1554 | 1596 | for i := 0 to numIn - 1 do |
| 1555 | 1597 | if inReg[i] < 0 then MovsdMX(Mem(RBP, inSlot[i]), -1 - inReg[i]) |
| 1556 | 1598 | else if (inObj[i] <> nil) and (inObj[i]^.reg <> 0) then begin |
| 1557 | - MovRR(inObj[i]^.reg, inReg[i]); | |
| 1558 | - if inSize[i] < 8 then Extend(inObj[i]^.reg, inSize[i], inObj[i]^.typ^.signed); | |
| 1599 | + r := inObj[i]^.reg; | |
| 1600 | + if inSlot[i] <> inObj[i]^.val then r := inObj[i]^.reg2; | |
| 1601 | + MovRR(r, inReg[i]); | |
| 1602 | + if inSize[i] < 8 then Extend(r, inSize[i], inObj[i]^.typ^.signed); | |
| 1559 | 1603 | end else Store(Mem(RBP, inSlot[i]), inReg[i], inSize[i]); |
| 1560 | 1604 | { a register variable's parameter that came on the stack, above the |
| 1561 | 1605 | frame (val > 0), is loaded from there } |
| @@ -1564,7 +1608,10 @@ | ||
| 1564 | 1608 | if (p^.reg <> 0) and (p^.val > 0) then |
| 1565 | 1609 | if p^.typ^.form in [fInt, fBool, fEnum, fSet] then |
| 1566 | 1610 | Load(p^.reg, Mem(RBP, p^.val), p^.typ^.size, p^.typ^.signed) |
| 1567 | - else Load(p^.reg, Mem(RBP, p^.val), 8, false); | |
| 1611 | + else begin | |
| 1612 | + Load(p^.reg, Mem(RBP, p^.val), 8, false); | |
| 1613 | + if p^.typ^.form = fSlice then Load(p^.reg2, Mem(RBP, p^.val + 8), 8, false); | |
| 1614 | + end; | |
| 1568 | 1615 | p := p^.next; |
| 1569 | 1616 | end; |
| 1570 | 1617 | end; |
| @@ -191,6 +191,7 @@ | ||
| 191 | 191 | FrameAdr.base := bFrame; |
| 192 | 192 | FrameAdr.chunk := -1; |
| 193 | 193 | FrameAdr.off := off; |
| 194 | + FrameAdr.hi := 0; | |
| 194 | 195 | end; |
| 195 | 196 | |
| 196 | 197 | function GlobalAdr(c: longint): TAdr; |
| @@ -198,14 +199,17 @@ | ||
| 198 | 199 | GlobalAdr.base := bGlobal; |
| 199 | 200 | GlobalAdr.chunk := c; |
| 200 | 201 | GlobalAdr.off := 0; |
| 202 | + GlobalAdr.hi := 0; | |
| 201 | 203 | end; |
| 202 | 204 | |
| 203 | -{ Where the variable x is, for gen } | |
| 205 | +{ Where the variable x is, for gen; a slice in registers has two } | |
| 204 | 206 | function AdrOf(const x: TItem): TAdr; |
| 205 | 207 | begin |
| 206 | 208 | AdrOf.base := x.base; |
| 207 | 209 | AdrOf.chunk := x.chunk; |
| 208 | 210 | AdrOf.off := x.off; |
| 211 | + AdrOf.hi := 0; | |
| 212 | + if x.base = bReg then AdrOf.hi := x.obj^.reg2; | |
| 209 | 213 | end; |
| 210 | 214 | |
| 211 | 215 | { An item for "found ...": a constant without a type of its own as it |
| @@ -373,6 +377,7 @@ | ||
| 373 | 377 | { its address is wanted after all: the procedure is compiled again |
| 374 | 378 | with the variable in memory; till then, this code is thrown away } |
| 375 | 379 | x.obj^.reg := 0; |
| 380 | + x.obj^.reg2 := 0; | |
| 376 | 381 | demoted := true; |
| 377 | 382 | x.base := bFrame; |
| 378 | 383 | x.off := x.obj^.val; |
| @@ -417,10 +422,21 @@ | ||
| 417 | 422 | |
| 418 | 423 | { x, copied to a temporary } |
| 419 | 424 | procedure CopyToTemp(var x: TItem); |
| 420 | -var t: int64; f, g: PObj; | |
| 425 | +var t: int64; f, g: PObj; a: TAdr; | |
| 421 | 426 | begin |
| 422 | 427 | if x.vol then MarkAt(x.pos, 'can''t copy through a volatile pointer'); |
| 423 | 428 | t := AllocTemp(x.typ^.size, x.typ^.align); |
| 429 | + if (x.mode = mVar) and (x.base = bReg) then begin | |
| 430 | + { a slice in registers, which has no address: its words stored } | |
| 431 | + a := AdrOf(x); | |
| 432 | + PushVar(a, 8, false, kWord); | |
| 433 | + PopVar(FrameAdr(t), 8); | |
| 434 | + a.off := 8; | |
| 435 | + PushVar(a, 8, false, kWord); | |
| 436 | + PopVar(FrameAdr(t + 8), 8); | |
| 437 | + TempItem(x, x.typ, t); | |
| 438 | + exit; | |
| 439 | + end; | |
| 424 | 440 | if x.mode = mWords then begin |
| 425 | 441 | { the second result is on top, so it is popped first } |
| 426 | 442 | f := x.typ^.fields; g := f^.next; |
| @@ -542,6 +558,7 @@ | ||
| 542 | 558 | PopVar(AdrOf(dst), ScalarSize(dst.typ)); |
| 543 | 559 | end else begin |
| 544 | 560 | if dst.vol or src.vol then MarkAt(src.pos, 'can''t copy through a volatile pointer'); |
| 561 | + if (src.mode = mVar) and (src.base = bReg) then CopyToTemp(src); { a slice in registers } | |
| 545 | 562 | Address(src); |
| 546 | 563 | if dst.base = bStack then CopyBlock(dst.typ^.size, dst.off) |
| 547 | 564 | else CopyTo(AdrOf(dst), dst.typ^.size); |
| @@ -1164,7 +1181,7 @@ | ||
| 1164 | 1181 | if x.mode = mWords then exit; { there already } |
| 1165 | 1182 | { copied to a temporary unless its two words are in memory at a place |
| 1166 | 1183 | that a later argument can't change } |
| 1167 | - if (x.mode <> mVar) or x.vol or not (x.base in [bFrame, bGlobal, bSlot]) then CopyToTemp(x); | |
| 1184 | + if (x.mode <> mVar) or x.vol or not (x.base in [bFrame, bGlobal, bSlot, bReg]) then CopyToTemp(x); | |
| 1168 | 1185 | adr := AdrOf(x); |
| 1169 | 1186 | if x.typ^.form = fSlice then begin |
| 1170 | 1187 | PushVar(adr, 8, false, kWord); |
| @@ -1818,12 +1818,12 @@ | ||
| 1818 | 1818 | if o^.reg = VarRegs[i] then VarKind := k + o^.typ^.size * 16 + (i + 1) * 512; |
| 1819 | 1819 | end; |
| 1820 | 1820 | |
| 1821 | -{ Whether variable o could live in a register: a single value, not | |
| 1822 | - a real, that isn't C's } | |
| 1821 | +{ Whether variable o could live in registers: a single value, not a | |
| 1822 | + real, or a slice, in two, that isn't C's } | |
| 1823 | 1823 | function RegFit(o: PObj): boolean; |
| 1824 | 1824 | begin |
| 1825 | 1825 | RegFit := (o^.cls = oVar) and not o^.ind and not o^.ext |
| 1826 | - and ((o^.typ^.form in [fInt, fBool, fEnum, fPtr, fProc, fError]) | |
| 1826 | + and ((o^.typ^.form in [fInt, fBool, fEnum, fPtr, fProc, fError, fSlice]) | |
| 1827 | 1827 | or (o^.typ^.form = fSet) and (o^.typ^.size <= 8)); |
| 1828 | 1828 | end; |
| 1829 | 1829 | |
| @@ -1840,12 +1840,12 @@ | ||
| 1840 | 1840 | its "begin" to its end, a use counts 1, in a loop 4, in two 16, in |
| 1841 | 1841 | three or more 64, and a variable ADR is taken of can't have one. A register costs a save |
| 1842 | 1842 | and a restore, so a variable used less than three times keeps its |
| 1843 | - place in memory. } | |
| 1843 | + place in memory. A slice takes two, its pointer's and its length's. } | |
| 1844 | 1844 | procedure ChooseRegs(params, before: PObj); |
| 1845 | 1845 | var |
| 1846 | 1846 | cand: array[0..63] of PObj; weight: array[0..63] of longint; |
| 1847 | 1847 | isLoop: array[0..255] of boolean; |
| 1848 | - n, i, k, d, loops, best: longint; o: PObj; st: TScanState; afterDot, wasSkipping: boolean; | |
| 1848 | + n, i, k, d, loops, best, need: longint; o: PObj; st: TScanState; afterDot, wasSkipping: boolean; | |
| 1849 | 1849 | |
| 1850 | 1850 | function Candidate(name: longint): longint; |
| 1851 | 1851 | var j: longint; |
| @@ -1899,15 +1899,24 @@ | ||
| 1899 | 1899 | until (d = 0) or (tok = sEof); |
| 1900 | 1900 | RestoreScan(st); |
| 1901 | 1901 | skipping := wasSkipping; |
| 1902 | - { the five heaviest, each used at least three times, take the | |
| 1903 | - registers in turn } | |
| 1904 | - for k := 0 to 4 do begin | |
| 1902 | + { a slice's weight is shared by its two registers } | |
| 1903 | + for i := 0 to n - 1 do | |
| 1904 | + if cand[i]^.typ^.form = fSlice then weight[i] := weight[i] div 2; | |
| 1905 | + { the heaviest, each used at least three times a register, take the | |
| 1906 | + five registers in turn, while there are enough for each } | |
| 1907 | + k := 0; | |
| 1908 | + repeat | |
| 1905 | 1909 | best := -1; |
| 1906 | 1910 | for i := 0 to n - 1 do |
| 1907 | 1911 | if (cand[i]^.reg = 0) and (weight[i] >= 3) and ((best < 0) or (weight[i] > weight[best])) then best := i; |
| 1908 | 1912 | if best < 0 then break; |
| 1909 | - cand[best]^.reg := VarRegs[k]; | |
| 1910 | - end; | |
| 1913 | + need := 1 + ord(cand[best]^.typ^.form = fSlice); | |
| 1914 | + if k + need <= 5 then begin | |
| 1915 | + cand[best]^.reg := VarRegs[k]; | |
| 1916 | + if need = 2 then cand[best]^.reg2 := VarRegs[k + 1]; | |
| 1917 | + inc(k, need); | |
| 1918 | + end else weight[best] := 0; { too big for what is left } | |
| 1919 | + until k = 5; | |
| 1911 | 1920 | end; |
| 1912 | 1921 | end; |
| 1913 | 1922 | |
| @@ -1919,13 +1928,13 @@ | ||
| 1919 | 1928 | m := 0; |
| 1920 | 1929 | o := params; |
| 1921 | 1930 | while o <> nil do begin |
| 1922 | - for k := 0 to 4 do if o^.reg = VarRegs[k] then m := m or (1 shl k); | |
| 1931 | + for k := 0 to 4 do if (o^.reg = VarRegs[k]) or (o^.reg2 = VarRegs[k]) then m := m or (1 shl k); | |
| 1923 | 1932 | o := o^.next; |
| 1924 | 1933 | end; |
| 1925 | 1934 | o := FirstLocal(before); |
| 1926 | 1935 | while o <> nil do begin |
| 1927 | 1936 | if o^.cls = oVar then |
| 1928 | - for k := 0 to 4 do if o^.reg = VarRegs[k] then m := m or (1 shl k); | |
| 1937 | + for k := 0 to 4 do if (o^.reg = VarRegs[k]) or (o^.reg2 = VarRegs[k]) then m := m or (1 shl k); | |
| 1929 | 1938 | o := o^.link; |
| 1930 | 1939 | end; |
| 1931 | 1940 | RegsOf := m; |
| @@ -72,7 +72,9 @@ | ||
| 72 | 72 | fwd: boolean; { oProc: declared forward, no body yet } |
| 73 | 73 | isCode: boolean; { oProc: a code procedure } |
| 74 | 74 | openEnd: boolean; { oProc: has results, and may reach its end } |
| 75 | - reg: longint; { oVar: the register it lives in, or 0 if in memory } | |
| 75 | + reg: longint; { oVar: the register it lives in, or 0 if in memory; | |
| 76 | + for a slice, the one its pointer is in } | |
| 77 | + reg2: longint; { oVar: a slice in registers: the one its length is in } | |
| 76 | 78 | ext: boolean; { oProc, oVar: C's, reached through its chunk, which |
| 77 | 79 | holds its address } |
| 78 | 80 | fields: PObj; { oCase: its fields } |
| @@ -65,7 +65,7 @@ | ||
| 65 | 65 | fi |
| 66 | 66 | } |
| 67 | 67 | |
| 68 | -for f in examples/*.raz test/run/*.raz test/libc/*.raz; do | |
| 68 | +for f in examples/*.raz test/run/*.raz test/libc/*.raz test/bench/*.raz; do | |
| 69 | 69 | run $(dirname $f) $(basename $f .raz) |
| 70 | 70 | done |
| 71 | 71 |
| @@ -0,0 +1,6 @@ | ||
| 1 | +21 | |
| 2 | +4 3 | |
| 3 | +0 | |
| 4 | +1003 abcd | |
| 5 | +41 | |
| 6 | +27 |
| @@ -0,0 +1,78 @@ | ||
| 1 | +(* Slices in registers: a slice parameter or variable used enough to | |
| 2 | + be given its two registers, through every way a slice is used. *) | |
| 3 | +module SliceRegs; | |
| 4 | +import Out; | |
| 5 | + | |
| 6 | +proc sum(s: []INT): INT; | |
| 7 | +var i, t: INT; | |
| 8 | +begin | |
| 9 | + for i := 0 to LEN(s) - 1 do t := t + s[i]; end; | |
| 10 | + return t; | |
| 11 | +end; | |
| 12 | + | |
| 13 | +(* s sliced, assigned, passed and returned, all in registers *) | |
| 14 | +proc tail(s: []INT; n: INT): []INT; | |
| 15 | +var i: INT; | |
| 16 | +begin | |
| 17 | + for i := 1 to n do | |
| 18 | + if LEN(s) > 0 then s := s[1..LEN(s)]; end; | |
| 19 | + end; | |
| 20 | + return s; | |
| 21 | +end; | |
| 22 | + | |
| 23 | +(* two slices compared, copied and assigned to each other *) | |
| 24 | +proc mix(a, b: []U8): INT; | |
| 25 | +var i, n: INT; c: []U8; | |
| 26 | +begin | |
| 27 | + for i := 0 to LEN(a) - 1 do | |
| 28 | + if a[i] = b[i] then INC(n); end; | |
| 29 | + end; | |
| 30 | + if a = b then INC(n, 100); end; | |
| 31 | + COPY(b, a); | |
| 32 | + if a = b then INC(n, 1000); end; | |
| 33 | + c := a; | |
| 34 | + for i := 0 to LEN(c) - 1 do INC(n, INT(c[i]) - INT(a[i])); end; | |
| 35 | + return n; | |
| 36 | +end; | |
| 37 | + | |
| 38 | +proc bump(var s: []INT); | |
| 39 | +begin | |
| 40 | + s := s[1..LEN(s)]; | |
| 41 | +end; | |
| 42 | + | |
| 43 | +(* s passed to a var parameter: demoted to memory, and still right *) | |
| 44 | +proc demote(s: []INT): INT; | |
| 45 | +var i, t: INT; | |
| 46 | +begin | |
| 47 | + for i := 0 to LEN(s) - 1 do t := t + s[i]; end; | |
| 48 | + bump(s); | |
| 49 | + for i := 0 to LEN(s) - 1 do t := t + s[i]; end; | |
| 50 | + return t; | |
| 51 | +end; | |
| 52 | + | |
| 53 | +(* seven slices: some in registers, some not *) | |
| 54 | +proc many(a, b, c, d, e, f, g: []INT): INT; | |
| 55 | +var i, t: INT; | |
| 56 | +begin | |
| 57 | + for i := 0 to 1 do | |
| 58 | + t := t + a[i] + b[i] + c[i] + d[i] + e[i] + f[i] + g[i]; | |
| 59 | + end; | |
| 60 | + return t; | |
| 61 | +end; | |
| 62 | + | |
| 63 | +proc main*; | |
| 64 | +var x: [6]INT; i: INT; p, q: [4]U8; s: []INT; | |
| 65 | +begin | |
| 66 | + for i := 0 to 5 do x[i] := i + 1; end; | |
| 67 | + Out.int(sum(x)); Out.ln; | |
| 68 | + s := tail(x, 2); | |
| 69 | + Out.int(LEN(s)); Out.char(' '); Out.int(s[0]); Out.ln; | |
| 70 | + Out.int(LEN(tail(x, 9))); Out.ln; | |
| 71 | + p := "abcd"; | |
| 72 | + q := "abxd"; | |
| 73 | + Out.int(mix(p, q)); Out.char(' '); Out.str(q); Out.ln; | |
| 74 | + Out.int(demote(x)); Out.ln; | |
| 75 | + Out.int(many(x, x[1..6], x, x, x, x[2..6], x)); Out.ln; | |
| 76 | +end; | |
| 77 | + | |
| 78 | +end SliceRegs. |
| @@ -0,0 +1 @@ | ||
| 1 | +14 |
| @@ -0,0 +1,21 @@ | ||
| 1 | +(* A failed check of an index into a slice in registers says the | |
| 2 | + length, read from its register. *) | |
| 3 | +module SliceRegsCheck; | |
| 4 | +import Out; | |
| 5 | + | |
| 6 | +proc squares(s: []INT; n: INT): INT; | |
| 7 | +var i, t: INT; | |
| 8 | +begin | |
| 9 | + for i := 0 to n do t := t + s[i] * s[i]; end; | |
| 10 | + return t; | |
| 11 | +end; | |
| 12 | + | |
| 13 | +proc main*; | |
| 14 | +var x: [3]INT; | |
| 15 | +begin | |
| 16 | + x := [1, 2, 3]; | |
| 17 | + Out.int(squares(x, 2)); Out.ln; | |
| 18 | + Out.int(squares(x, 3)); Out.ln; | |
| 19 | +end; | |
| 20 | + | |
| 21 | +end SliceRegsCheck. |
| @@ -0,0 +1 @@ | ||
| 1 | +2 |
| @@ -0,0 +1,3 @@ | ||
| 1 | +test/run/SliceRegsCheck.raz:9:35: index 3 is out of range for a length of 3 | |
| 2 | + in SliceRegsCheck.squares at test/run/SliceRegsCheck.raz:9:35: n = 3, i = 3, t = 14 | |
| 3 | + in SliceRegsCheck.main at test/run/SliceRegsCheck.raz:18:5 |