1 ! Copyright (C) 2008, 2009 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 sets locals arrays
10 compiler.cfg.instructions
11 compiler.cfg.renaming.functor
12 compiler.cfg.linearization.order
13 compiler.cfg.linear-scan.allocation
14 compiler.cfg.linear-scan.allocation.state
15 compiler.cfg.linear-scan.live-intervals ;
16 IN: compiler.cfg.linear-scan.assignment
18 ! This contains both active and inactive intervals; any interval
19 ! such that start <= insn# <= end is in this set.
20 SYMBOL: pending-interval-heap
21 SYMBOL: pending-interval-assoc
23 : add-pending ( live-interval -- )
24 [ dup end>> pending-interval-heap get heap-push ]
25 [ [ reg>> ] [ vreg>> ] bi pending-interval-assoc get set-at ]
28 : remove-pending ( live-interval -- )
29 vreg>> pending-interval-assoc get delete-at ;
31 : (vreg>reg) ( vreg pending -- reg )
32 ! If a live vreg is not in the pending set, then it must
34 ?at [ spill-slots get at <spill-slot> ] unless ;
36 : vreg>reg ( vreg -- reg )
37 pending-interval-assoc get (vreg>reg) ;
39 : vregs>regs ( vregs -- assoc )
41 pending-interval-assoc get
42 '[ _ (vreg>reg) ] assoc-map
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 ! Mapping from basic blocks to values which are live at the start
55 SYMBOL: register-live-ins
57 ! Mapping from basic blocks to values which are live at the end
58 SYMBOL: register-live-outs
60 : init-assignment ( live-intervals -- )
61 <min-heap> pending-interval-heap set
62 H{ } clone pending-interval-assoc set
63 <min-heap> unhandled-intervals set
64 H{ } clone register-live-ins set
65 H{ } clone register-live-outs set
68 : insert-spill ( live-interval -- )
69 [ reg>> ] [ vreg>> rep-of ] [ spill-to>> ] tri _spill ;
71 : handle-spill ( live-interval -- )
72 dup spill-to>> [ insert-spill ] [ drop ] if ;
74 : expire-interval ( live-interval -- )
75 [ remove-pending ] [ handle-spill ] bi ;
77 : (expire-old-intervals) ( n heap -- )
78 dup heap-empty? [ 2drop ] [
79 2dup heap-peek nip <= [ 2drop ] [
80 dup heap-pop drop expire-interval
81 (expire-old-intervals)
85 : expire-old-intervals ( n -- )
86 pending-interval-heap get (expire-old-intervals) ;
88 : insert-reload ( live-interval -- )
89 [ reg>> ] [ vreg>> rep-of ] [ reload-from>> ] tri _reload ;
91 : handle-reload ( live-interval -- )
92 dup reload-from>> [ insert-reload ] [ drop ] if ;
94 : activate-interval ( live-interval -- )
95 [ add-pending ] [ handle-reload ] bi ;
97 : (activate-new-intervals) ( n heap -- )
98 dup heap-empty? [ 2drop ] [
99 2dup heap-peek nip = [
100 dup heap-pop drop activate-interval
101 (activate-new-intervals)
105 : activate-new-intervals ( n -- )
106 unhandled-intervals get (activate-new-intervals) ;
108 : prepare-insn ( n -- )
109 [ expire-old-intervals ] [ activate-new-intervals ] bi ;
111 GENERIC: assign-registers-in-insn ( insn -- )
113 RENAMING: assign [ vreg>reg ] [ vreg>reg ] [ vreg>reg ]
115 M: vreg-insn assign-registers-in-insn
116 [ assign-insn-defs ] [ assign-insn-uses ] [ assign-insn-temps ] tri ;
118 ! TODO: needs tagged-rep
120 : trace-on-gc ( assoc -- assoc' )
121 ! When a GC occurs, virtual registers which contain tagged data
122 ! are traced by the GC. Outputs a sequence physical registers.
123 [ drop rep-of int-rep eq? ] { } assoc-filter-as values ;
125 : spill-on-gc? ( vreg reg -- ? )
126 [ rep-of int-rep? not ] [ spill-slot? not ] bi* and ;
128 : spill-on-gc ( assoc -- assoc' )
129 ! When a GC occurs, virtual registers which contain untagged data,
130 ! and are stored in physical registers, are saved to their spill
131 ! slots. Outputs sequence of triples:
132 ! - physical register
138 [ swap [ vreg-spill-slot ] [ rep-of ] bi 3array , ] [ 2drop ] if
142 M: ##gc assign-registers-in-insn
143 ! Since ##gc is always the first instruction in a block, the set of
144 ! values live at the ##gc is just live-in.
146 basic-block get register-live-ins get at
147 [ trace-on-gc >>tagged-values ] [ spill-on-gc >>data-values ] bi
150 M: insn assign-registers-in-insn drop ;
152 : begin-block ( bb -- )
154 dup block-from activate-new-intervals
155 [ live-in vregs>regs ] keep register-live-ins get set-at ;
157 : end-block ( bb -- )
158 [ live-out vregs>regs ] keep register-live-outs get set-at ;
160 ERROR: bad-vreg vreg ;
162 : vreg-at-start ( vreg bb -- state )
163 register-live-ins get at ?at [ bad-vreg ] unless ;
165 : vreg-at-end ( vreg bb -- state )
166 register-live-outs get at ?at [ bad-vreg ] unless ;
168 :: assign-registers-in-block ( bb -- )
174 [ insn#>> 1 - prepare-insn ]
175 [ insn#>> prepare-insn ]
176 [ assign-registers-in-insn ]
182 ] change-instructions drop ;
184 : assign-registers ( live-intervals cfg -- )
185 [ init-assignment ] dip
186 linearization-order [ assign-registers-in-block ] each ;