1 ! Copyright (C) 2008, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs combinators compiler.cfg
4 compiler.cfg.linearization compiler.cfg.liveness compiler.cfg.registers
5 compiler.cfg.instructions compiler.cfg.linear-scan.allocation.state
6 compiler.cfg.linear-scan.live-intervals compiler.cfg.renaming.functor
7 compiler.cfg.ssa.destruction.leaders cpu.architecture
8 fry heaps kernel make math namespaces sequences sets ;
9 IN: compiler.cfg.linear-scan.assignment
11 ! This contains both active and inactive intervals; any interval
12 ! such that start <= insn# <= end is in this set.
13 SYMBOL: pending-interval-heap
14 SYMBOL: pending-interval-assoc
16 : add-pending ( live-interval -- )
17 [ dup end>> pending-interval-heap get heap-push ]
18 [ [ reg>> ] [ vreg>> ] bi pending-interval-assoc get set-at ]
21 : remove-pending ( live-interval -- )
22 vreg>> pending-interval-assoc get delete-at ;
24 : vreg>reg ( vreg -- reg/spill-slot )
25 dup leader dup pending-interval-assoc get at
26 [ 2nip ] [ swap rep-of lookup-spill-slot ] if* ;
28 ERROR: not-spilled-error vreg ;
30 : vreg>spill-slot ( vreg -- spill-slot )
31 dup vreg>reg dup spill-slot? [ nip ] [ drop leader not-spilled-error ] if ;
33 : vregs>regs ( vregs -- assoc )
34 [ dup vreg>reg ] H{ } map>assoc ;
36 SYMBOL: unhandled-intervals
38 SYMBOL: machine-live-ins
40 : machine-live-in ( bb -- assoc )
41 machine-live-ins get at ;
43 : compute-live-in ( bb -- )
44 [ live-in keys vregs>regs ] keep machine-live-ins get set-at ;
46 SYMBOL: machine-edge-live-ins
48 : machine-edge-live-in ( predecessor bb -- assoc )
49 machine-edge-live-ins get at at ;
51 : compute-edge-live-in ( bb -- )
52 [ edge-live-ins get at [ keys vregs>regs ] assoc-map ] keep
53 machine-edge-live-ins get set-at ;
55 SYMBOL: machine-live-outs
57 : machine-live-out ( bb -- assoc )
58 machine-live-outs get at ;
60 : compute-live-out ( bb -- )
61 [ live-out keys vregs>regs ] keep machine-live-outs get set-at ;
63 : heap-pop-while ( heap quot: ( key -- ? ) -- values )
64 '[ dup heap-empty? [ f f ] [ dup heap-peek @ ] if ]
65 [ over heap-pop* ] produce 2nip ; inline
67 : insert-spill ( live-interval -- )
68 [ reg>> ] [ spill-rep>> ] [ spill-to>> ] tri ##spill, ;
70 : handle-spill ( live-interval -- )
71 dup spill-to>> [ insert-spill ] [ drop ] if ;
73 : expire-interval ( live-interval -- )
74 [ remove-pending ] [ handle-spill ] bi ;
76 : expire-old-intervals ( n -- )
77 pending-interval-heap get swap '[ _ < ] heap-pop-while
78 [ expire-interval ] each ;
80 : insert-reload ( live-interval -- )
81 [ reg>> ] [ reload-rep>> ] [ reload-from>> ] tri ##reload, ;
83 : handle-reload ( live-interval -- )
84 dup reload-from>> [ insert-reload ] [ drop ] if ;
86 : activate-interval ( live-interval -- )
87 [ add-pending ] [ handle-reload ] bi ;
89 : activate-new-intervals ( n -- )
90 unhandled-intervals get swap '[ _ = ] heap-pop-while
91 [ activate-interval ] each ;
93 : prepare-insn ( n -- )
94 [ expire-old-intervals ] [ activate-new-intervals ] bi ;
96 GENERIC: assign-registers-in-insn ( insn -- )
98 RENAMING: assign [ vreg>reg ] [ vreg>reg ] [ vreg>reg ]
100 M: vreg-insn assign-registers-in-insn
101 [ assign-insn-defs ] [ assign-insn-uses ] [ assign-insn-temps ] tri ;
103 : assign-gc-roots ( gc-map -- )
104 [ [ vreg>spill-slot ] map ] change-gc-roots drop ;
106 : assign-derived-roots ( gc-map -- )
107 [ [ [ vreg>spill-slot ] bi@ ] assoc-map ] change-derived-roots drop ;
109 M: gc-map-insn assign-registers-in-insn
110 [ [ assign-insn-defs ] [ assign-insn-uses ] [ assign-insn-temps ] tri ]
111 [ gc-map>> [ assign-gc-roots ] [ assign-derived-roots ] bi ]
114 M: insn assign-registers-in-insn drop ;
116 : begin-block ( bb -- )
119 [ block-from activate-new-intervals ]
120 [ compute-edge-live-in ]
124 : assign-registers-in-block ( bb -- )
130 [ insn#>> 1 - prepare-insn ]
131 [ insn#>> prepare-insn ]
132 [ assign-registers-in-insn ]
137 ] change-instructions compute-live-out ;
139 : init-assignment ( live-intervals -- )
140 [ [ start>> ] map ] keep zip >min-heap unhandled-intervals set
141 <min-heap> pending-interval-heap set
142 H{ } clone pending-interval-assoc set
143 H{ } clone machine-live-ins set
144 H{ } clone machine-edge-live-ins set
145 H{ } clone machine-live-outs set ;
147 : assign-registers ( cfg live-intervals -- )
149 linearization-order [ kill-block?>> ] reject
150 [ assign-registers-in-block ] each ;