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
9 compiler.cfg.liveness.ssa
10 compiler.cfg.registers
11 compiler.cfg.instructions
12 compiler.cfg.ssa.destruction
13 compiler.cfg.renaming.functor
14 compiler.cfg.linearization.order
15 compiler.cfg.linear-scan.allocation
16 compiler.cfg.linear-scan.allocation.state
17 compiler.cfg.linear-scan.live-intervals ;
18 FROM: namespaces => set ;
19 IN: compiler.cfg.linear-scan.assignment
21 ! This contains both active and inactive intervals; any interval
22 ! such that start <= insn# <= end is in this set.
23 SYMBOL: pending-interval-heap
24 SYMBOL: pending-interval-assoc
26 : add-pending ( live-interval -- )
27 [ dup end>> pending-interval-heap get heap-push ]
28 [ [ reg>> ] [ vreg>> ] bi pending-interval-assoc get set-at ]
31 : remove-pending ( live-interval -- )
32 vreg>> pending-interval-assoc get delete-at ;
34 :: vreg>reg ( vreg -- reg )
35 ! If a live vreg is not in the pending set, then it must
38 leader pending-interval-assoc get at* [
39 drop leader vreg rep-of lookup-spill-slot
42 : vregs>regs ( vregs -- assoc )
43 [ f ] [ [ dup vreg>reg ] H{ } map>assoc ] if-empty ;
45 ! Minheap of live intervals which still need a register allocation
46 SYMBOL: unhandled-intervals
48 : add-unhandled ( live-interval -- )
49 dup start>> unhandled-intervals get heap-push ;
51 : init-unhandled ( live-intervals -- )
52 [ add-unhandled ] each ;
54 ! Liveness info is used by resolve pass
56 ! Mapping from basic blocks to values which are live at the start
57 ! on all incoming CFG edges
58 SYMBOL: machine-live-ins
60 : machine-live-in ( bb -- assoc )
61 machine-live-ins get at ;
63 : compute-live-in ( bb -- )
64 [ live-in keys vregs>regs ] keep machine-live-ins get set-at ;
66 ! Mapping from basic blocks to predecessors to values which are
67 ! live on a particular incoming edge
68 SYMBOL: machine-edge-live-ins
70 : machine-edge-live-in ( predecessor bb -- assoc )
71 machine-edge-live-ins get at at ;
73 : compute-edge-live-in ( bb -- )
74 [ edge-live-ins get at [ keys vregs>regs ] assoc-map ] keep
75 machine-edge-live-ins get set-at ;
77 ! Mapping from basic blocks to values which are live at the end
78 SYMBOL: machine-live-outs
80 : machine-live-out ( bb -- assoc )
81 machine-live-outs get at ;
83 : compute-live-out ( bb -- )
84 [ live-out keys vregs>regs ] keep machine-live-outs get set-at ;
86 : init-assignment ( live-intervals -- )
87 <min-heap> pending-interval-heap set
88 H{ } clone pending-interval-assoc set
89 <min-heap> unhandled-intervals set
90 H{ } clone machine-live-ins set
91 H{ } clone machine-edge-live-ins set
92 H{ } clone machine-live-outs set
95 : insert-spill ( live-interval -- )
96 [ reg>> ] [ last-use rep>> ] [ spill-to>> ] tri ##spill ;
98 : handle-spill ( live-interval -- )
99 dup spill-to>> [ insert-spill ] [ drop ] if ;
101 : expire-interval ( live-interval -- )
102 [ remove-pending ] [ handle-spill ] bi ;
104 : (expire-old-intervals) ( n heap -- )
105 dup heap-empty? [ 2drop ] [
106 2dup heap-peek nip <= [ 2drop ] [
107 dup heap-pop drop expire-interval
108 (expire-old-intervals)
112 : expire-old-intervals ( n -- )
113 pending-interval-heap get (expire-old-intervals) ;
115 : insert-reload ( live-interval -- )
116 [ reg>> ] [ first-use rep>> ] [ reload-from>> ] tri ##reload ;
118 : insert-reload? ( live-interval -- ? )
119 ! Don't insert a reload if the register will be written to
120 ! before being read again.
123 [ first-use type>> +use+ eq? ]
126 : handle-reload ( live-interval -- )
127 dup insert-reload? [ insert-reload ] [ drop ] if ;
129 : activate-interval ( live-interval -- )
130 [ add-pending ] [ handle-reload ] bi ;
132 : (activate-new-intervals) ( n heap -- )
133 dup heap-empty? [ 2drop ] [
134 2dup heap-peek nip = [
135 dup heap-pop drop activate-interval
136 (activate-new-intervals)
140 : activate-new-intervals ( n -- )
141 unhandled-intervals get (activate-new-intervals) ;
143 : prepare-insn ( n -- )
144 [ expire-old-intervals ] [ activate-new-intervals ] bi ;
146 GENERIC: assign-registers-in-insn ( insn -- )
148 RENAMING: assign [ vreg>reg ] [ vreg>reg ] [ vreg>reg ]
150 M: vreg-insn assign-registers-in-insn
151 [ assign-insn-defs ] [ assign-insn-uses ] [ assign-insn-temps ] tri ;
153 M: ##call-gc assign-registers-in-insn
155 [ [ vreg>reg ] map ] change-gc-roots drop ;
157 M: insn assign-registers-in-insn drop ;
159 : begin-block ( bb -- )
162 [ block-from activate-new-intervals ]
163 [ compute-edge-live-in ]
167 :: assign-registers-in-block ( bb -- )
173 [ insn#>> 1 - prepare-insn ]
174 [ insn#>> prepare-insn ]
175 [ assign-registers-in-insn ]
181 ] change-instructions drop ;
183 : assign-registers ( live-intervals cfg -- )
184 [ init-assignment ] dip
185 linearization-order [ assign-registers-in-block ] each ;