1 ! Copyright (C) 2008, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors kernel math assocs namespaces sequences heaps
4 fry make combinators combinators.short-circuit sets locals arrays
5 cpu.architecture layouts
10 compiler.cfg.instructions
11 compiler.cfg.linearization
12 compiler.cfg.ssa.destruction.leaders
13 compiler.cfg.renaming.functor
14 compiler.cfg.linear-scan.allocation
15 compiler.cfg.linear-scan.allocation.state
16 compiler.cfg.linear-scan.live-intervals ;
17 FROM: namespaces => set ;
18 IN: compiler.cfg.linear-scan.assignment
20 ! This contains both active and inactive intervals; any interval
21 ! such that start <= insn# <= end is in this set.
22 SYMBOL: pending-interval-heap
23 SYMBOL: pending-interval-assoc
25 : add-pending ( live-interval -- )
26 [ dup end>> pending-interval-heap get heap-push ]
27 [ [ reg>> ] [ vreg>> ] bi pending-interval-assoc get set-at ]
30 : remove-pending ( live-interval -- )
31 vreg>> pending-interval-assoc get delete-at ;
33 :: vreg>reg ( vreg -- reg )
35 leader pending-interval-assoc get at* [
36 drop leader vreg rep-of lookup-spill-slot
39 ERROR: not-spilled-error vreg ;
41 : vreg>spill-slot ( vreg -- spill-slot )
42 dup vreg>reg dup spill-slot? [ nip ] [ drop leader not-spilled-error ] if ;
44 : vregs>regs ( vregs -- assoc )
45 [ f ] [ [ dup vreg>reg ] H{ } map>assoc ] if-empty ;
47 ! Minheap of live intervals which still need a register allocation
48 SYMBOL: unhandled-intervals
50 : add-unhandled ( live-interval -- )
51 dup start>> unhandled-intervals get heap-push ;
53 : init-unhandled ( live-intervals -- )
54 [ add-unhandled ] each ;
56 ! Liveness info is used by resolve pass
58 ! Mapping from basic blocks to values which are live at the start
59 ! on all incoming CFG edges
60 SYMBOL: machine-live-ins
62 : machine-live-in ( bb -- assoc )
63 machine-live-ins get at ;
65 : compute-live-in ( bb -- )
66 [ live-in keys vregs>regs ] keep machine-live-ins get set-at ;
68 ! Mapping from basic blocks to predecessors to values which are
69 ! live on a particular incoming edge
70 SYMBOL: machine-edge-live-ins
72 : machine-edge-live-in ( predecessor bb -- assoc )
73 machine-edge-live-ins get at at ;
75 : compute-edge-live-in ( bb -- )
76 [ edge-live-ins get at [ keys vregs>regs ] assoc-map ] keep
77 machine-edge-live-ins get set-at ;
79 ! Mapping from basic blocks to values which are live at the end
80 SYMBOL: machine-live-outs
82 : machine-live-out ( bb -- assoc )
83 machine-live-outs get at ;
85 : compute-live-out ( bb -- )
86 [ live-out keys vregs>regs ] keep machine-live-outs get set-at ;
88 : init-assignment ( live-intervals -- )
89 <min-heap> pending-interval-heap set
90 H{ } clone pending-interval-assoc set
91 <min-heap> unhandled-intervals set
92 H{ } clone machine-live-ins set
93 H{ } clone machine-edge-live-ins set
94 H{ } clone machine-live-outs set
97 : insert-spill ( live-interval -- )
98 [ reg>> ] [ spill-rep>> ] [ spill-to>> ] tri ##spill, ;
100 : handle-spill ( live-interval -- )
101 dup spill-to>> [ insert-spill ] [ drop ] if ;
103 : expire-interval ( live-interval -- )
104 [ remove-pending ] [ handle-spill ] bi ;
106 : (expire-old-intervals) ( n heap -- )
107 dup heap-empty? [ 2drop ] [
108 2dup heap-peek nip <= [ 2drop ] [
109 dup heap-pop drop expire-interval
110 (expire-old-intervals)
114 : expire-old-intervals ( n -- )
115 pending-interval-heap get (expire-old-intervals) ;
117 : insert-reload ( live-interval -- )
118 [ reg>> ] [ reload-rep>> ] [ reload-from>> ] tri ##reload, ;
120 : handle-reload ( live-interval -- )
121 dup reload-from>> [ insert-reload ] [ drop ] if ;
123 : activate-interval ( live-interval -- )
124 [ add-pending ] [ handle-reload ] bi ;
126 : (activate-new-intervals) ( n heap -- )
127 dup heap-empty? [ 2drop ] [
128 2dup heap-peek nip = [
129 dup heap-pop drop activate-interval
130 (activate-new-intervals)
134 : activate-new-intervals ( n -- )
135 unhandled-intervals get (activate-new-intervals) ;
137 : prepare-insn ( n -- )
138 [ expire-old-intervals ] [ activate-new-intervals ] bi ;
140 GENERIC: assign-registers-in-insn ( insn -- )
142 RENAMING: assign [ vreg>reg ] [ vreg>reg ] [ vreg>reg ]
144 M: vreg-insn assign-registers-in-insn
145 [ assign-insn-defs ] [ assign-insn-uses ] [ assign-insn-temps ] tri ;
147 : assign-gc-roots ( gc-map -- )
148 [ [ vreg>spill-slot ] map ] change-gc-roots drop ;
150 : assign-derived-roots ( gc-map -- )
151 [ [ [ vreg>spill-slot ] bi@ ] assoc-map ] change-derived-roots drop ;
153 M: gc-map-insn assign-registers-in-insn
154 [ [ assign-insn-defs ] [ assign-insn-uses ] [ assign-insn-temps ] tri ]
155 [ gc-map>> [ assign-gc-roots ] [ assign-derived-roots ] bi ]
158 M: insn assign-registers-in-insn drop ;
160 : begin-block ( bb -- )
163 [ block-from activate-new-intervals ]
164 [ compute-edge-live-in ]
168 :: assign-registers-in-block ( bb -- )
175 [ insn#>> 1 - prepare-insn ]
176 [ insn#>> prepare-insn ]
177 [ assign-registers-in-insn ]
183 ] change-instructions drop
186 : assign-registers ( live-intervals cfg -- )
187 [ init-assignment ] dip
188 linearization-order [ assign-registers-in-block ] each ;