1 ! Copyright (C) 2008, 2010 Slava Pestov.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs combinators compiler.cfg
4 compiler.cfg.instructions compiler.cfg.linear-scan.allocation.state
5 compiler.cfg.linear-scan.live-intervals
6 compiler.cfg.linearization compiler.cfg.liveness
7 compiler.cfg.registers compiler.cfg.renaming.functor
8 compiler.cfg.ssa.destruction.leaders compiler.cfg.utilities
9 heaps kernel make math namespaces sequences ;
10 IN: compiler.cfg.linear-scan.assignment
13 ! This contains both active and inactive intervals; any interval
14 ! such that start <= insn# <= end is in this set.
15 SYMBOL: pending-interval-heap
16 SYMBOL: pending-interval-assoc
18 : insert-spill ( live-interval -- )
19 [ reg>> ] [ spill-rep>> ] [ spill-to>> ] tri ##spill, ;
21 : handle-spill ( live-interval -- )
22 dup spill-to>> [ insert-spill ] [ drop ] if ;
24 : add-pending ( live-interval -- )
25 [ dup live-interval-end pending-interval-heap get heap-push ]
26 [ [ reg>> ] [ vreg>> ] bi pending-interval-assoc get set-at ]
29 : remove-pending ( live-interval -- )
30 vreg>> pending-interval-assoc get delete-at ;
32 : vreg>spill-slot ( vreg -- spill-slot )
33 dup rep-of lookup-spill-slot ;
35 : vreg>reg ( vreg -- reg/spill-slot )
36 dup leader dup pending-interval-assoc get at
37 [ 2nip ] [ swap rep-of lookup-spill-slot ] if* ;
39 : vregs>regs ( assoc -- assoc' )
40 [ vreg>reg ] assoc-map ;
42 SYMBOL: unhandled-intervals
44 SYMBOL: machine-live-ins
46 : machine-live-in ( bb -- assoc )
47 machine-live-ins get at ;
49 : compute-live-in ( bb -- )
50 [ live-in vregs>regs ] keep machine-live-ins get set-at ;
52 SYMBOL: machine-edge-live-ins
54 : machine-edge-live-in ( predecessor bb -- assoc )
55 machine-edge-live-ins get at at ;
57 : compute-edge-live-in ( bb -- )
58 [ edge-live-ins get at [ vregs>regs ] assoc-map ] keep
59 machine-edge-live-ins get set-at ;
61 SYMBOL: machine-live-outs
63 : machine-live-out ( bb -- assoc )
64 machine-live-outs get at ;
66 : compute-live-out ( bb -- )
67 [ live-out vregs>regs ] keep machine-live-outs get set-at ;
69 : expire-interval ( live-interval -- )
70 [ remove-pending ] [ handle-spill ] bi ;
72 : expire-old-intervals ( n pending-heap -- )
73 [ > ] with heap-pop-while [ expire-interval ] each ;
75 : insert-reload ( live-interval -- )
76 [ reg>> ] [ reload-rep>> ] [ reload-from>> ] tri ##reload, ;
78 : handle-reload ( live-interval -- )
79 dup reload-from>> [ insert-reload ] [ drop ] if ;
81 : activate-interval ( live-interval -- )
82 [ add-pending ] [ handle-reload ] bi ;
84 : activate-new-intervals ( n unhandled-heap -- )
85 [ = ] with heap-pop-while [ activate-interval ] each ;
87 : prepare-insn ( n -- )
88 [ pending-interval-heap get expire-old-intervals ]
89 [ unhandled-intervals get activate-new-intervals ] bi ;
91 RENAMING: assign [ vreg>reg ] [ vreg>reg ] [ vreg>reg ]
93 : assign-all-registers ( insn -- )
94 [ assign-insn-defs ] [ assign-insn-uses ] [ assign-insn-temps ] tri ;
96 : begin-block ( bb -- )
98 [ basic-block namespaces:set ]
99 [ block-from unhandled-intervals get activate-new-intervals ]
100 [ compute-edge-live-in ]
104 : change-insn-gc-roots ( gc-map-insn quot: ( x -- x ) -- )
105 [ gc-map>> ] dip [ [ gc-roots>> ] dip map! drop ]
106 [ '[ [ _ bi@ ] assoc-map ] change-derived-roots drop ] 2bi ; inline
108 : spill-required? ( live-interval root-leaders n -- ? )
109 [ [ vreg>> ] dip sets:in? ] [ swap covers? ] bi-curry* bi or ;
111 : spill-intervals ( root-leaders n -- live-intervals )
112 [ pending-interval-heap get heap-members ] 2dip
113 '[ _ _ spill-required? ] filter ;
115 : rep-at-insn ( n interval -- rep )
116 (find-use) [ def-rep>> ] [ use-rep>> ] bi or ;
118 : spill/reload ( n interval -- {reg,rep,slot} )
119 [ rep-at-insn ] keep [ reg>> ] [ vreg>> ] bi
120 pick assign-spill-slot swapd 3array ;
122 : spill/reloads ( n intervals -- spill/reloads )
123 [ spill/reload ] with map ;
125 : spill/reloads-for-call-gc ( ##call-gc -- spill-seq )
126 [ gc-map>> gc-roots>> ] [ insn#>> ] bi
127 [ spill-intervals ] keep swap spill/reloads ;
129 : emit-##call-gc ( insn -- )
130 dup spill/reloads-for-call-gc
131 dup [ first3 ##spill, ] each
133 [ first3 ##reload, ] each ;
135 : emit-gc-map-insn ( gc-map-insn -- )
136 [ [ leader ] change-insn-gc-roots ]
137 [ dup ##call-gc? [ emit-##call-gc ] [ , ] if ]
138 [ [ vreg>spill-slot ] change-insn-gc-roots ] tri ;
140 : emit-insn ( insn -- )
141 dup gc-map-insn? [ emit-gc-map-insn ] [ , ] if ;
143 : assign-registers-in-block ( bb -- )
148 [ insn#>> prepare-insn ]
149 [ assign-all-registers ]
153 ] change-instructions compute-live-out ;
155 : live-intervals>min-heap ( live-intervals -- min-heap )
156 [ [ live-interval-start ] map ] keep zip >min-heap ;
158 : init-assignment ( live-intervals -- )
159 live-intervals>min-heap unhandled-intervals namespaces:set
160 <min-heap> pending-interval-heap namespaces:set
161 H{ } clone pending-interval-assoc namespaces:set
162 H{ } clone machine-live-ins namespaces:set
163 H{ } clone machine-edge-live-ins namespaces:set
164 H{ } clone machine-live-outs namespaces:set ;
166 : assign-registers ( cfg live-intervals -- )
168 linearization-order [ kill-block?>> ] reject
169 [ assign-registers-in-block ] each ;