Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/seed/ng/src/opt.om # omega-mv Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance. Brought by drakestapleton Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / seed / ng / src / opt.om` 1136 lines · 26641 bytes Raw Blame History 1 note SPDX-License-Identifier: Apache-2.0 2 note seed/ng/src/opt.om: the ΩIR optimizer of the ng compiler (Hive NG D16, M3). Owner: ng-ir lane. A unit of 3 note seed/ng/ng.units after ir.om; no fn main. Functions op_*, globals o_*. It rewrites the ir.om tables in place 4 note between ir_read and ir_wr (main's modes `@ng opt` and `@ng opt-count`): it moves ops between blocks and 5 note blocks within a function, adds blocks and drops ops, through the same lists ir_wr walks. 6 note Per function, in order: 7 note 1. Preheaders. Every natural loop (header h, a back edge u -> h with h dominating u, the loop being the blocks 8 note that reach a latch without passing h) gets one block outside it that is h's only predecessor from outside: 9 note the existing one when h has exactly one outside predecessor and it ends in `br h`; otherwise a new block, 10 note placed before h, with h's parameter types, ending in `br h` with them, which every outside predecessor 11 note branches to instead (same arguments). A new block is added only within IR.md s12 (op_room); a loop 12 note that would need one past the limits keeps none, and LICM skips it. 13 note 1a. PHI (after the preheaders). A block parameter other than the token whose arguments, over every `br` that 14 note targets its block, are one value v or the parameter itself is replaced by v: every use takes v, and the 15 note parameter leaves the block and the matching argument every such `br` (a block with a parameter past its 16 note token is reached by `br` only, IR.md s9 rule 5). v dominates the block (IR.md s6.2): on any path from block 17 note 0 the edge that first enters the block comes from a predecessor it does not dominate, whose `br` passes v, 18 note and v dominates that `br`. One parameter per round, to a 19 note fixed point. Every value an outer loop carries unchanged through an inner loop's header is such a 20 note parameter; once it is gone LICM sees its uses as invariant and the back end moves nothing for it. 21 note 2. BCE (IR.md s15 item 1, origin 1 of the pass table below). A `chk.bounds r i e` is deleted when e is a 22 note const >= 1, r a REF to an ARRAY of count L, and i < X is known where the check runs, with X - 1 + e <= L: 23 note i is a const (X = i + 1), or a block on the check's dominator path has one predecessor, which ends in 24 note a `cbr` on `lt`/`le`/`gt`/`ge` of i and a const (either edge, read accordingly), so that block runs 25 note only after the test said i < X. The check is replaced in place by `fact.inbounds 1 r i e`, which keeps 26 note every access covered (s8). A `chk.divz` of a nonzero const is deleted. Uses of a deleted check's 27 note output token take its input token, so the chain stays linear (s6.4). 28 note 3. LICM, innermost loop first. An op of the loop (blocks in reverse postorder, ops in order) moves to the end 29 note of the preheader, before its `br`, when its class is P (IR.md s6.4: no token; every P op is total, s7.3, so 30 note it may run on a path where it did not), it defines a value, it is not `shared` (block 0 only) and not 31 note convergent (`sync`, `shfl`, `mmald`, `mmast`, `mma`: IR.md s7.12, never moved into or out of a branch, and a 32 note loop body is one; refused by number, whatever their class), and 33 note every operand is defined outside the loop. Checks, loads, stores, calls, host words, facts and terminators 34 note never move: the token chain and coverage (s6.4, s8, s15) fix their places. An op equal to one already in the 35 note preheader (op, type, imm and operands) is dropped and its uses take the earlier value. 36 note 4. ROT. A loop whose header ends in `cbr` with one target in the loop and one outside, and whose one latch 37 note ends in `br h`, gets its header placed right after that latch. The block order is the layout a back end 38 note emits (a64 falls through to the next block and branches on one side of a `cbr`), so the back branch falls 39 note through into the test and the loop is tested at the bottom: one conditional branch per pass. Block order 40 note changes no value: uses are judged by dominance (s6.2). 41 note PHI, BCE and LICM run again, in that order, while one of them changed something: LICM's merging gives two 42 note `br` arguments one value, which makes a parameter redundant only after PHI ran (ngrand seed 523), so one 43 note round alone left `@ng opt` short of a fixed point. ROT runs once, after the last round. 44 note Pass table (fact origins, IR.md s7.10): 1 BCE. 45 note `@ng opt-count` runs the same passes and prints one line instead of the unit: 46 note "opt fns=F loops=L inloop=A->B phi=Q bce=C hoisted=H merged=M rotated=R preheaders=P": L natural loops (by header), 47 note A and B the executing ops in loop blocks before and after (static, terminators included, facts not; a block 48 note counts once however 49 note deeply it nests), C checks deleted by BCE, H moves of an op out of a loop (an op leaving two nested loops counts twice; M of them 50 note dropped as equal to one already in the preheader), R headers placed after their latch, P preheader blocks 51 note added, Q block parameters removed by PHI. Counts of passes added after the first four (PHI and later) come 52 note before " bce=", so the line still ends " bce=C hoisted=H merged=M rotated=R preheaders=P"; the gate's 53 note fixed-point check reads the tail from " phi=" on (gate-ir.om q_fx), so a pass placed before PHI's count 54 note must extend q_fx to be covered. 55 note 56 note per block handle: o_bp position in its function, o_bl block at position, o_rn reverse postorder number (from 57 note 1; 0 not reached), o_ro block at rpo number, o_id immediate dominator, o_pc predecessor count, o_p0 first 58 note predecessor in o_pl, o_lm loop mark (a stamp), o_sk next successor in the depth-first walk, o_in in a loop 59 global o_bp uint `1048577` 60 global o_bl uint `1048577` 61 global o_rn uint `1048577` 62 global o_ro uint `1048578` 63 global o_id uint `1048577` 64 global o_pc uint `1048577` 65 global o_p0 uint `1048577` 66 global o_pl uint `2097154` 67 global o_lm uint `1048577` 68 global o_sk uchar `1048577` 69 global o_in uchar `1048577` 70 global o_st uint `1048577` 71 global o_po uint `1048577` 72 global o_hd uint `1048577` 73 global o_hz ulong `1048577` 74 note o_n: 0 blocks of the function, 1 loop stamp, 2 headers, 3 hoisted, 4 merged, 5 rotated, 6 preheaders added, 75 note 7 loops, 8 in-loop ops before, 9 after, 10 blocks reached, 11 current function, 12 output error (27), 76 note 13 checks deleted, 14 block parameters removed by PHI 77 global o_n ulong 32 78 text o_msg "opt fns= loops= inloop=-> phi= bce= hoisted= merged= rotated= preheaders=\n" 79 note o_pv: per value, the one argument seen for that block parameter (0 none yet, `4294967295` more than one) 80 global o_pv uint `8388609` 81 global o_dig uchar 24 82 note ---- control flow ---------------------------------------------------------------------------- 83 note successors of block b: br has one (slot 0), cbr two (slots 2, 3), other terminators none 84 fn op_nsu b uint to ulong 85 t = get i_bot b 86 if eq t 0 87 ret 0 88 end 89 op = get i_oop t 90 if eq op 60 91 ret 1 92 end 93 if eq op 61 94 ret 2 95 end 96 ret 0 97 end 98 fn op_su b uint k ulong to uint 99 t = get i_bot b 100 if eq get i_oop t 60 101 ret ir_arg t 0 102 end 103 ret ir_arg t uint add k 2 104 end 105 note blocks of f in order (o_bl, o_bp), analysis fields cleared 106 fn op_scan f uint to void 107 b = get i_fb1 f 108 k = uint 0 109 while ne b 0 bound `1048577` 110 put o_bl k b 111 put o_bp b k 112 put o_rn b 0 113 put o_id b 0 114 put o_pc b 0 115 put o_lm b 0 116 k = add k 1 117 b = get i_bnx b 118 end 119 put o_n 0 ulong k 120 end 121 note predecessor lists: counts, then starts, then the lists (o_pc ends as the counts again) 122 fn op_preds to void 123 n = get o_n 0 124 for j ulong n bound `1048577` 125 b = get o_bl j 126 for k ulong op_nsu b bound 2 127 s = op_su b k 128 put o_pc s add get o_pc s 1 129 end 130 end 131 c = uint 0 132 for j ulong n bound `1048577` 133 b = get o_bl j 134 put o_p0 b c 135 c = add c get o_pc b 136 put o_pc b 0 137 end 138 for j ulong n bound `1048577` 139 op_pfill get o_bl j 140 end 141 end 142 fn op_pfill b uint to void 143 for k ulong op_nsu b bound 2 144 s = op_su b k 145 put o_pl add get o_p0 s get o_pc s b 146 put o_pc s add get o_pc s 1 147 end 148 end 149 fn op_pred b uint k ulong to uint 150 ret get o_pl add ulong get o_p0 b k 151 end 152 note reverse postorder from the entry block: an explicit depth-first walk (o_st), postorder into o_po 153 fn op_rpo to void 154 e = get o_bl 0 155 put o_st 0 e 156 put o_sk e 0 157 put o_rn e 1 158 sp = ulong 1 159 po = ulong 0 160 while gt sp 0 bound `4194304` 161 b = get o_st sub sp 1 162 k = ulong get o_sk b 163 if lt k op_nsu b 164 put o_sk b uchar add k 1 165 sp = op_push b k sp 166 else 167 sp = sub sp 1 168 put o_po po b 169 po = add po 1 170 end 171 end 172 for i ulong po bound `1048577` 173 b = get o_po i 174 r = sub po i 175 put o_rn b uint r 176 put o_ro r b 177 end 178 put o_n 10 po 179 end 180 fn op_push b uint k ulong sp ulong to ulong 181 s = op_su b k 182 if ne get o_rn s 0 183 ret sp 184 end 185 put o_rn s 1 186 put o_sk s 0 187 put o_st sp s 188 ret add sp 1 189 end 190 note immediate dominators (Cooper, Harvey and Kennedy: iterate in reverse postorder to a fixed point) 191 fn op_dom to void 192 m = get o_n 10 193 e = get o_ro 1 194 put o_id e e 195 ch = true 196 while ch bound `1048577` 197 ch = false 198 for r ulong sub m 1 bound `1048577` 199 b = get o_ro add r 2 200 d = op_idm b 201 if ne d get o_id b 202 put o_id b d 203 ch = true 204 end 205 end 206 end 207 end 208 fn op_idm b uint to uint 209 d = uint 0 210 for k ulong ulong get o_pc b bound `1048577` 211 p = op_pred b k 212 if ne get o_id p 0 213 if eq d 0 214 d = p 215 else 216 d = op_int p d 217 end 218 end 219 end 220 ret d 221 end 222 fn op_int a uint b uint to uint 223 x = a 224 y = b 225 while ne x y bound `1048577` 226 while gt get o_rn x get o_rn y bound `1048577` 227 x = get o_id x 228 end 229 while gt get o_rn y get o_rn x bound `1048577` 230 y = get o_id y 231 end 232 end 233 ret x 234 end 235 note block a dominates block b 236 fn op_doms a uint b uint to bool 237 x = b 238 while true bound `1048577` 239 if eq x a 240 ret true 241 end 242 y = get o_id x 243 if eq y x 244 ret false 245 end 246 x = y 247 end 248 ret false 249 end 250 fn op_ana f uint to void 251 put o_n 11 ulong f 252 op_scan f 253 op_preds 254 op_rpo 255 op_dom 256 end 257 note ---- loops --------------------------------------------------------------------------------- 258 note loop headers of the analysed function into o_hd (count o_n 2): blocks with a predecessor they dominate 259 fn op_hdrs to void 260 c = ulong 0 261 for r ulong get o_n 10 bound `1048577` 262 h = get o_ro add r 1 263 if op_ishd h 264 put o_hd c h 265 c = add c 1 266 end 267 end 268 put o_n 2 c 269 end 270 fn op_ishd h uint to bool 271 for k ulong ulong get o_pc h bound `1048577` 272 if op_doms h op_pred h k 273 ret true 274 end 275 end 276 ret false 277 end 278 note mark the loop of header h with a new stamp (o_lm = o_n 1): h, its latches, and every block that reaches a 279 note latch without passing h; returns the number of blocks 280 fn op_mark h uint to ulong 281 s = uint add get o_n 1 1 282 put o_n 1 ulong s 283 put o_lm h s 284 z = ulong 1 285 sp = ulong 0 286 for k ulong ulong get o_pc h bound `1048577` 287 p = op_pred h k 288 if op_doms h p 289 if ne get o_lm p s 290 put o_lm p s 291 put o_st sp p 292 sp = add sp 1 293 z = add z 1 294 end 295 end 296 end 297 while gt sp 0 bound `1048577` 298 sp = sub sp 1 299 x = get o_st sp 300 for k ulong ulong get o_pc x bound `1048577` 301 y = op_pred x k 302 if ne get o_lm y s 303 put o_lm y s 304 put o_st sp y 305 sp = add sp 1 306 z = add z 1 307 end 308 end 309 end 310 ret z 311 end 312 fn op_inl b uint to bool 313 ret eq ulong get o_lm b get o_n 1 314 end 315 note the outside predecessor of the marked loop of h that ends in `br h` when it is the only outside one, else 0 316 fn op_ph h uint to uint 317 p = uint 0 318 c = ulong 0 319 for k ulong ulong get o_pc h bound `1048577` 320 q = op_pred h k 321 if not op_inl q 322 p = q 323 c = add c 1 324 end 325 end 326 if ne c 1 327 ret 0 328 end 329 if ne get i_oop get i_bot p 60 330 ret 0 331 end 332 ret p 333 end 334 note ---- lists ----------------------------------------------------------------------------------- 335 note the op before o in block b (0 when o is first) 336 fn op_oprv b uint o uint to uint 337 p = uint 0 338 x = get i_bo1 b 339 while ne x o bound `4194305` 340 p = x 341 x = get i_onx x 342 end 343 ret p 344 end 345 note take op o out of block b 346 fn op_unl b uint o uint to void 347 p = op_oprv b o 348 if eq p 0 349 put i_bo1 b get i_onx o 350 else 351 put i_onx p get i_onx o 352 end 353 if eq get i_bot b o 354 put i_bot b p 355 end 356 put i_bno b sub get i_bno b 1 357 put i_onx o 0 358 end 359 note put op o into block p right before its terminator 360 fn op_ins p uint o uint to void 361 t = get i_bot p 362 q = op_oprv p t 363 put i_onx o t 364 if eq q 0 365 put i_bo1 p o 366 else 367 put i_onx q o 368 end 369 put i_ob o p 370 put i_bno p add get i_bno p 1 371 end 372 note the block before b in function f (0 when b is first) 373 fn op_bprv f uint b uint to uint 374 p = uint 0 375 x = get i_fb1 f 376 while ne x b bound `1048577` 377 p = x 378 x = get i_bnx x 379 end 380 ret p 381 end 382 note move block b of f to right after block a 383 fn op_bmv f uint b uint a uint to void 384 p = op_bprv f b 385 if eq p 0 386 put i_fb1 f get i_bnx b 387 else 388 put i_bnx p get i_bnx b 389 end 390 if eq get i_fbt f b 391 put i_fbt f p 392 end 393 put i_bnx b get i_bnx a 394 put i_bnx a b 395 if eq get i_fbt f a 396 put i_fbt f b 397 end 398 end 399 note every value operand of every op of f that names a takes b instead 400 fn op_repl f uint a uint b uint to void 401 x = get i_fb1 f 402 while ne x 0 bound `1048577` 403 o = get i_bo1 x 404 while ne o 0 bound `4194305` 405 op_rep1 o a b 406 o = get i_onx o 407 end 408 x = get i_bnx x 409 end 410 end 411 fn op_rep1 o uint a uint b uint to void 412 op = uint get i_oop o 413 for k ulong ulong get i_ona o bound 1024 414 if eq ir_okd op k 0 415 if eq ir_arg o uint k a 416 ir_set o uint k b 417 end 418 end 419 end 420 end 421 note ---- 1. preheaders --------------------------------------------------------------------------- 422 note add one missing preheader to f; true when one was added (the analysis is then stale). A loop whose 423 note preheader would break a limit of IR.md s12 (op_room) keeps none, so LICM leaves it alone. 424 fn op_pre f uint to bool 425 op_ana f 426 op_hdrs 427 for i ulong get o_n 2 bound `1048577` 428 h = get o_hd i 429 x = op_mark h 430 if eq op_ph h 0 431 if op_room f h 432 op_newp f h 433 ret true 434 end 435 end 436 end 437 ret false 438 end 439 note room in f for a preheader of h (one block with h's i_bnp parameters, one `br` with that many arguments 440 note and its target) under IR.md s12: 65536 blocks per function and `1048576` in the unit, `1048576` ops per 441 note function and `4194304` in the unit, `2097152` values per function; and in ir.om's tables (i_vty, i_arg) 442 fn op_room f uint h uint to bool 443 p = ulong get i_bnp h 444 if ge get i_fnb f 65536 445 ret false 446 end 447 if ge get i_n 4 `1048576` 448 ret false 449 end 450 if ge get i_n 5 `4194304` 451 ret false 452 end 453 if gt add get i_n 6 p `8388608` 454 ret false 455 end 456 if gt add get i_n 7 add p 1 `16777216` 457 ret false 458 end 459 c = ulong 0 460 v = add ulong get i_fnp f 1 461 b = get i_fb1 f 462 while ne b 0 bound `1048577` 463 c = add c ulong get i_bno b 464 v = add v op_vals b 465 b = get i_bnx b 466 end 467 if ge c `1048576` 468 ret false 469 end 470 ret le add v p `2097152` 471 end 472 note values block b defines: its parameters, then each op's token and result 473 fn op_vals b uint to ulong 474 n = ulong get i_bnp b 475 o = get i_bo1 b 476 while ne o 0 bound `4194305` 477 if ne get i_otk o 0 478 n = add n 1 479 end 480 if ne get i_ors o 0 481 n = add n 1 482 end 483 o = get i_onx o 484 end 485 ret n 486 end 487 note a new block n with h's parameter types, ending in `br h` with them, before h; the outside predecessors of 488 note the marked loop of h branch to n instead 489 fn op_newp f uint h uint to void 490 n = ir_blk f 491 v = get i_vnx get i_bp1 h 492 while ne v 0 bound 65 493 w = ir_bpar n get i_vty v 494 v = get i_vnx v 495 end 496 o = ir_op n 60 0 0 add get i_bnp h 1 497 ir_set o 0 h 498 k = uint 1 499 v = get i_bp1 n 500 while ne v 0 bound 65 501 ir_set o k v 502 k = add k 1 503 v = get i_vnx v 504 end 505 for j ulong ulong get o_pc h bound `1048577` 506 q = op_pred h j 507 if not op_inl q 508 op_redir q h n 509 end 510 end 511 op_bmv f n op_bprv f h 512 put o_n 6 add get o_n 6 1 513 end 514 note the terminator of block q branches to n where it branched to h 515 fn op_redir q uint h uint n uint to void 516 t = get i_bot q 517 if eq get i_oop t 60 518 if eq ir_arg t 0 h 519 ir_set t 0 n 520 end 521 ret 522 end 523 for k ulong 2 524 s = uint add k 2 525 if eq ir_arg t s h 526 ir_set t s n 527 end 528 end 529 end 530 note ---- 1a. PHI --------------------------------------------------------------------------------- 531 note remove redundant block parameters of f, one per round, until none is left 532 fn op_phi f uint to void 533 go = true 534 while go bound `2097153` 535 go = op_phi1 f 536 end 537 end 538 note one round: the argument seen for every parameter past the token (o_pv), then the first parameter with 539 note exactly one argument other than itself is removed; true when one was 540 fn op_phi1 f uint to bool 541 b = get i_fb1 f 542 while ne b 0 bound `1048577` 543 v = get i_vnx get i_bp1 b 544 while ne v 0 bound 65 545 put o_pv v 0 546 v = get i_vnx v 547 end 548 b = get i_bnx b 549 end 550 b = get i_fb1 f 551 while ne b 0 bound `1048577` 552 t = get i_bot b 553 if eq get i_oop t 60 554 op_pvbr t 555 end 556 b = get i_bnx b 557 end 558 b = get i_fb1 f 559 while ne b 0 bound `1048577` 560 v = get i_vnx get i_bp1 b 561 while ne v 0 bound 65 562 w = get o_pv v 563 if and ne w 0 ne w `4294967295` 564 op_phirm f b v w 565 ret true 566 end 567 v = get i_vnx v 568 end 569 b = get i_bnx b 570 end 571 ret false 572 end 573 note the arguments of `br` t for its target's parameters past the token (slot 0 the target, slot 1 the token) 574 fn op_pvbr t uint to void 575 v = get i_vnx get i_bp1 ir_arg t 0 576 k = uint 2 577 while ne v 0 bound 65 578 op_pv1 v ir_arg t k 579 k = add k 1 580 v = get i_vnx v 581 end 582 end 583 fn op_pv1 p uint a uint to void 584 if eq a p 585 ret 586 end 587 x = get o_pv p 588 if eq x 0 589 put o_pv p a 590 ret 591 end 592 if ne x a 593 put o_pv p `4294967295` 594 end 595 end 596 note parameter p of block b becomes w: uses take w, p leaves b's list and its slot every `br` to b 597 fn op_phirm f uint b uint p uint w uint to void 598 op_repl f p w 599 k = uint 1 600 q = uint 0 601 v = get i_bp1 b 602 while ne v p bound 65 603 q = v 604 v = get i_vnx v 605 k = add k 1 606 end 607 put i_vnx q get i_vnx p 608 if eq get i_bpt b p 609 put i_bpt b q 610 end 611 put i_vnx p 0 612 put i_bnp b sub get i_bnp b 1 613 x = get i_fb1 f 614 while ne x 0 bound `1048577` 615 t = get i_bot x 616 if eq get i_oop t 60 617 if eq ir_arg t 0 b 618 op_adel t k 619 end 620 end 621 x = get i_bnx x 622 end 623 put o_n 14 add get o_n 14 1 624 end 625 note drop operand slot k of op t (the later slots move down one) 626 fn op_adel t uint k uint to void 627 n = get i_ona t 628 j = k 629 while lt add j 1 n bound 1025 630 ir_set t j ir_arg t add j 1 631 j = add j 1 632 end 633 put i_ona t sub n 1 634 end 635 note ---- 2. LICM --------------------------------------------------------------------------------- 636 fn op_licm f uint to void 637 op_ana f 638 op_hdrs 639 c = get o_n 2 640 for i ulong c bound `1048577` 641 put o_hz i op_mark get o_hd i 642 end 643 op_sort c 644 for i ulong c bound `1048577` 645 h = get o_hd i 646 x = op_mark h 647 p = op_ph h 648 if ne p 0 649 op_lic1 p 650 end 651 end 652 end 653 note headers by loop size, smallest (innermost) first: insertion sort of o_hd by o_hz 654 fn op_sort c ulong to void 655 for i ulong c bound `1048577` 656 j = i 657 while gt j 0 bound `1048577` 658 if le get o_hz sub j 1 get o_hz j 659 j = 0 660 else 661 op_swap sub j 1 j 662 j = sub j 1 663 end 664 end 665 end 666 end 667 fn op_swap a ulong b ulong to void 668 h = get o_hd a 669 z = get o_hz a 670 put o_hd a get o_hd b 671 put o_hz a get o_hz b 672 put o_hd b h 673 put o_hz b z 674 end 675 note the marked loop's blocks in reverse postorder, each op that may move to preheader p 676 fn op_lic1 p uint to void 677 for r ulong get o_n 10 bound `1048577` 678 b = get o_ro add r 1 679 if op_inl b 680 op_licb b p 681 end 682 end 683 end 684 fn op_licb b uint p uint to void 685 o = get i_bo1 b 686 while ne o 0 bound `4194305` 687 nx = get i_onx o 688 if op_canh o 689 if op_inv o 690 op_hoist o b p 691 end 692 end 693 o = nx 694 end 695 end 696 note op o may leave a loop: class P, defines a value, not `shared`, not convergent (IR.md s7.12: 44 sync, 45 shfl, 697 note 104 mmald, 105 mmast, 106 mma; sync, mmald and mmast are not class P either, the list does not lean on that) 698 fn op_canh o uint to bool 699 op = uint get i_oop o 700 if ne get i_rtk op 0 701 ret false 702 end 703 if eq get i_ors o 0 704 ret false 705 end 706 if or or eq op 44 eq op 45 or eq op 104 or eq op 105 eq op 106 707 ret false 708 end 709 ret ne op 41 710 end 711 note every operand of o past its token slot is defined outside the marked loop 712 fn op_inv o uint to bool 713 op = uint get i_oop o 714 h = ulong ir_htok op 715 for k ulong ulong get i_ona o bound 1024 716 if and ge k h eq ir_okd op k 0 717 if op_inl op_vdb ir_arg o uint k 718 ret false 719 end 720 end 721 end 722 ret true 723 end 724 note the block that defines value v (0 for the entry token and function parameters) 725 fn op_vdb v uint to uint 726 k = get i_vk v 727 if le k 1 728 ret 0 729 end 730 if eq k 2 731 ret get i_vo v 732 end 733 ret get i_ob get i_vo v 734 end 735 note move op o from loop block b to preheader p, or drop it when p already computes the same value 736 fn op_hoist o uint b uint p uint to void 737 put o_n 3 add get o_n 3 1 738 op_unl b o 739 q = op_same p o 740 if eq q 0 741 op_ins p o 742 ret 743 end 744 put o_n 4 add get o_n 4 1 745 op_repl uint get o_n 11 ir_res o ir_res q 746 end 747 note an op of p before its terminator equal to o (same op, type, imm and operands, defines a value), else 0 748 fn op_same p uint o uint to uint 749 t = get i_bot p 750 q = get i_bo1 p 751 while ne q t bound `4194305` 752 if op_eq q o 753 ret q 754 end 755 q = get i_onx q 756 end 757 ret 0 758 end 759 fn op_eq q uint o uint to bool 760 if ne get i_oop q get i_oop o 761 ret false 762 end 763 if ne get i_oty q get i_oty o 764 ret false 765 end 766 if ne get i_oim q get i_oim o 767 ret false 768 end 769 if ne get i_ona q get i_ona o 770 ret false 771 end 772 if eq get i_ors q 0 773 ret false 774 end 775 for k ulong ulong get i_ona o bound 1024 776 if ne ir_arg q uint k ir_arg o uint k 777 ret false 778 end 779 end 780 ret true 781 end 782 note ---- 2. BCE ---------------------------------------------------------------------------------- 783 fn op_bce f uint to void 784 op_ana f 785 for j ulong get o_n 0 bound `1048577` 786 b = get o_bl j 787 if ne get o_rn b 0 788 op_bceb b 789 end 790 end 791 end 792 fn op_bceb b uint to void 793 o = get i_bo1 b 794 while ne o 0 bound `4194305` 795 nx = get i_onx o 796 op = get i_oop o 797 if eq op 79 798 if op_inb o b 799 op_drop b o true 800 end 801 end 802 if eq op 74 803 if ne op_cv ir_arg o 1 0 804 op_drop b o false 805 end 806 end 807 o = nx 808 end 809 end 810 note value v is defined by a `const` 811 fn op_isc v uint to bool 812 if ne get i_vk v 4 813 ret false 814 end 815 ret eq get i_oop get i_vo v 1 816 end 817 note the bits of const v, 0 when v is not a const 818 fn op_cv v uint to ulong 819 if not op_isc v 820 ret 0 821 end 822 ret get i_oim get i_vo v 823 end 824 note element count of the ARRAY that REF value r refers to, 0 when r is not a REF to an ARRAY 825 fn op_alen r uint to ulong 826 t = get i_vty r 827 if lt t 32 828 ret 0 829 end 830 if ne get i_tk t 4 831 ret 0 832 end 833 a = get i_t1 t 834 if lt a 32 835 ret 0 836 end 837 if ne get i_tk a 2 838 ret 0 839 end 840 ret ulong get i_t2 a 841 end 842 note chk.bounds o (tok, r, i, e) in block b cannot fail 843 fn op_inb o uint b uint to bool 844 e = op_cv ir_arg o 3 845 if eq e 0 846 ret false 847 end 848 l = op_alen ir_arg o 1 849 if lt l e 850 ret false 851 end 852 x = op_ub ir_arg o 2 b 853 if eq x 0 854 ret false 855 end 856 ret le x add sub l e 1 857 end 858 note the least X with v < X known in block b from a const or the tests on b's dominator path; 0 none 859 fn op_ub v uint b uint to ulong 860 if op_isc v 861 c = op_cv v 862 if eq c `18446744073709551615` 863 ret 0 864 end 865 ret add c 1 866 end 867 x = ulong 0 868 d = b 869 while true bound `1048577` 870 y = op_edge v d 871 if ne y 0 872 if or eq x 0 lt y x 873 x = y 874 end 875 end 876 p = get o_id d 877 if eq p d 878 ret x 879 end 880 d = p 881 end 882 ret x 883 end 884 note the bound X with v < X that block d's one incoming edge proves, else 0: d has one predecessor, it 885 note ends in `cbr` with d as exactly one target, and the condition compares v with a const 886 fn op_edge v uint d uint to ulong 887 if ne get o_pc d 1 888 ret 0 889 end 890 t = get i_bot op_pred d 0 891 if ne get i_oop t 61 892 ret 0 893 end 894 if eq ir_arg t 2 ir_arg t 3 895 ret 0 896 end 897 c = ir_arg t 1 898 if ne get i_vk c 4 899 ret 0 900 end 901 q = get i_vo c 902 op = ulong get i_oop q 903 if or lt op 32 gt op 35 904 ret 0 905 end 906 if eq ir_arg t 2 d 907 ret op_cmpx q op 0 v 908 end 909 ret op_cmpx q op 1 v 910 end 911 note compare q `a OP b` (32 lt, 33 le, 34 gt, 35 ge) seen true (n 0) or false (n 1, the negation 67 - OP): 912 note the X with v < X when the relation bounds v above by a const; 0 otherwise 913 fn op_cmpx q uint op ulong n ulong v uint to ulong 914 k = op 915 if eq n 1 916 k = sub 67 op 917 end 918 s = ir_arg q 0 919 w = ir_arg q 1 920 if ge k 34 921 s = ir_arg q 1 922 w = ir_arg q 0 923 end 924 if ne s v 925 ret 0 926 end 927 if not op_isc w 928 ret 0 929 end 930 c = op_cv w 931 if or eq k 32 eq k 34 932 ret c 933 end 934 if eq c `18446744073709551615` 935 ret 0 936 end 937 ret add c 1 938 end 939 note delete check o of block b: uses of its output token take its input token; with fk, a 940 note `fact.inbounds 1` on its (r, i, e) takes its place 941 fn op_drop b uint o uint fk bool to void 942 if fk 943 x = ir_op b 92 0 1 3 944 op_unl b x 945 op_insb b x o 946 ir_set x 0 ir_arg o 1 947 ir_set x 1 ir_arg o 2 948 ir_set x 2 ir_arg o 3 949 end 950 op_repl uint get o_n 11 ir_otok o ir_arg o 0 951 op_unl b o 952 put o_n 13 add get o_n 13 1 953 end 954 note put op x into block b right before op o 955 fn op_insb b uint x uint o uint to void 956 q = op_oprv b o 957 put i_onx x o 958 if eq q 0 959 put i_bo1 b x 960 else 961 put i_onx q x 962 end 963 put i_ob x b 964 put i_bno b add get i_bno b 1 965 end 966 note ---- 4. ROT ---------------------------------------------------------------------------------- 967 fn op_rot f uint to void 968 op_ana f 969 op_hdrs 970 for i ulong get o_n 2 bound `1048577` 971 h = get o_hd i 972 x = op_mark h 973 op_rot1 f h 974 end 975 end 976 fn op_rot1 f uint h uint to void 977 t = get i_bot h 978 if ne get i_oop t 61 979 ret 980 end 981 if eq op_inl ir_arg t 2 op_inl ir_arg t 3 982 ret 983 end 984 u = op_latch h 985 if or eq u 0 eq u h 986 ret 987 end 988 if ne get i_oop get i_bot u 60 989 ret 990 end 991 if eq get i_bnx u h 992 ret 993 end 994 op_bmv f h u 995 put o_n 5 add get o_n 5 1 996 end 997 note the one predecessor of h inside its marked loop, else 0 998 fn op_latch h uint to uint 999 u = uint 0 1000 c = ulong 0 1001 for k ulong ulong get o_pc h bound `1048577` 1002 q = op_pred h k 1003 if op_inl q 1004 u = q 1005 c = add c 1 1006 end 1007 end 1008 if ne c 1 1009 ret 0 1010 end 1011 ret u 1012 end 1013 note ---- driver and counts ------------------------------------------------------------------------ 1014 note ops in the blocks of f that lie in a loop; adds f's loops to o_n 7 when k is 0 1015 fn op_cnt f uint k ulong to ulong 1016 op_ana f 1017 op_hdrs 1018 for j ulong get o_n 0 bound `1048577` 1019 put o_in get o_bl j 0 1020 end 1021 for i ulong get o_n 2 bound `1048577` 1022 x = op_mark get o_hd i 1023 op_inmk 1024 end 1025 if eq k 0 1026 put o_n 7 add get o_n 7 get o_n 2 1027 end 1028 c = ulong 0 1029 for j ulong get o_n 0 bound `1048577` 1030 b = get o_bl j 1031 if ne get o_in b 0 1032 c = add c op_nex b 1033 end 1034 end 1035 ret c 1036 end 1037 note executing ops of block b (facts 90..93 do not run) 1038 fn op_nex b uint to ulong 1039 c = ulong 0 1040 o = get i_bo1 b 1041 while ne o 0 bound `4194305` 1042 op = get i_oop o 1043 if or lt op 90 gt op 93 1044 c = add c 1 1045 end 1046 o = get i_onx o 1047 end 1048 ret c 1049 end 1050 fn op_inmk to void 1051 for j ulong get o_n 0 bound `1048577` 1052 b = get o_bl j 1053 if op_inl b 1054 put o_in b 1 1055 end 1056 end 1057 end 1058 note optimize every function of the unit read into the ir.om tables 1059 fn op_run to void 1060 for j ulong 32 1061 put o_n j 0 1062 end 1063 for i ulong get i_n 3 bound 65536 1064 op_fn uint add i 1 1065 end 1066 end 1067 fn op_fn f uint to void 1068 if lt get i_fnb f 2 1069 ret 1070 end 1071 put o_n 8 add get o_n 8 op_cnt f 0 1072 go = true 1073 while go bound 65536 1074 go = op_pre f 1075 end 1076 note PHI, BCE and LICM again while one of them changed something: LICM's merging (op_hoist) gives two `br` 1077 note arguments one value, which can make a parameter redundant only after PHI ran; every productive round 1078 note removes a parameter or a check or moves an op out of a loop, so the rounds end 1079 go = true 1080 while go bound `1048577` 1081 c = add add get o_n 14 get o_n 13 get o_n 3 1082 op_phi f 1083 op_bce f 1084 op_licm f 1085 go = ne c add add get o_n 14 get o_n 13 get o_n 3 1086 end 1087 op_rot f 1088 put o_n 9 add get o_n 9 op_cnt f 1 1089 end 1090 note the opt-count line on stdout; 27 when it cannot be written 1091 fn op_put to int 1092 op_say 0 8 1093 op_num get i_n 3 1094 op_say 8 15 1095 op_num get o_n 7 1096 op_say 15 23 1097 op_num get o_n 8 1098 op_say 23 25 1099 op_num get o_n 9 1100 op_say 25 30 1101 op_num get o_n 14 1102 op_say 30 35 1103 op_num get o_n 13 1104 op_say 35 44 1105 op_num get o_n 3 1106 op_say 44 52 1107 op_num get o_n 4 1108 op_say 52 61 1109 op_num get o_n 5 1110 op_say 61 73 1111 op_num get o_n 6 1112 op_say 73 74 1113 ret int get o_n 12 1114 end 1115 fn op_say a ulong b ulong to void 1116 k = write 1 o_msg a sub b a 1117 if ne k long sub b a 1118 put o_n 12 27 1119 end 1120 end 1121 fn op_num v ulong to void 1122 x = v 1123 i = ulong 24 1124 for j ulong 21 1125 i = sub i 1 1126 put o_dig i uchar add 48 rem x 10 1127 x = div x 10 1128 if eq x 0 1129 k = write 1 o_dig i sub 24 i 1130 if ne k long sub 24 i 1131 put o_n 12 27 1132 end 1133 ret 1134 end 1135 end 1136 end