interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / seed / ng / src / opt.om

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