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 FROM: namespaces => set ;
10 IN: compiler.cfg.linear-scan.assignment
12 ! This contains both active and inactive intervals; any interval
13 ! such that start <= insn# <= end is in this set.
14 SYMBOL: pending-interval-heap
15 SYMBOL: pending-interval-assoc
17 : add-pending ( live-interval -- )
18 [ dup end>> pending-interval-heap get heap-push ]
19 [ [ reg>> ] [ vreg>> ] bi pending-interval-assoc get set-at ]
22 : remove-pending ( live-interval -- )
23 vreg>> pending-interval-assoc get delete-at ;
25 : vreg>reg ( vreg -- reg/spill-slot )
26 dup leader dup pending-interval-assoc get at
27 [ 2nip ] [ swap rep-of lookup-spill-slot ] if* ;
29 ERROR: not-spilled-error vreg ;
31 : vreg>spill-slot ( vreg -- spill-slot )
32 dup vreg>reg dup spill-slot? [ nip ] [ drop leader not-spilled-error ] if ;
34 : vregs>regs ( vregs -- assoc )
35 [ dup vreg>reg ] H{ } map>assoc ;
37 SYMBOL: unhandled-intervals
39 SYMBOL: machine-live-ins
41 : machine-live-in ( bb -- assoc )
42 machine-live-ins get at ;
44 : compute-live-in ( bb -- )
45 [ live-in keys vregs>regs ] keep machine-live-ins get set-at ;
47 SYMBOL: machine-edge-live-ins
49 : machine-edge-live-in ( predecessor bb -- assoc )
50 machine-edge-live-ins get at at ;
52 : compute-edge-live-in ( bb -- )
53 [ edge-live-ins get at [ keys vregs>regs ] assoc-map ] keep
54 machine-edge-live-ins get set-at ;
56 SYMBOL: machine-live-outs
58 : machine-live-out ( bb -- assoc )
59 machine-live-outs get at ;
61 : compute-live-out ( bb -- )
62 [ live-out keys vregs>regs ] keep machine-live-outs get set-at ;
64 : heap-pop-while ( heap quot: ( key -- ? ) -- values )
65 '[ dup heap-empty? [ f f ] [ dup heap-peek @ ] if ]
66 [ over heap-pop* ] produce 2nip ; inline
68 : insert-spill ( live-interval -- )
69 [ reg>> ] [ spill-rep>> ] [ 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 -- )
78 pending-interval-heap get swap '[ _ < ] heap-pop-while
79 [ expire-interval ] each ;
81 : insert-reload ( live-interval -- )
82 [ reg>> ] [ reload-rep>> ] [ reload-from>> ] tri ##reload, ;
84 : handle-reload ( live-interval -- )
85 dup reload-from>> [ insert-reload ] [ drop ] if ;
87 : activate-interval ( live-interval -- )
88 [ add-pending ] [ handle-reload ] bi ;
90 : activate-new-intervals ( n -- )
91 unhandled-intervals get swap '[ _ = ] heap-pop-while
92 [ activate-interval ] each ;
94 : prepare-insn ( n -- )
95 [ expire-old-intervals ] [ activate-new-intervals ] bi ;
97 GENERIC: assign-registers-in-insn ( insn -- )
99 RENAMING: assign [ vreg>reg ] [ vreg>reg ] [ vreg>reg ]
101 M: vreg-insn assign-registers-in-insn
102 [ assign-insn-defs ] [ assign-insn-uses ] [ assign-insn-temps ] tri ;
104 : assign-gc-roots ( gc-map -- )
105 [ [ vreg>spill-slot ] map ] change-gc-roots drop ;
107 : assign-derived-roots ( gc-map -- )
108 [ [ [ vreg>spill-slot ] bi@ ] assoc-map ] change-derived-roots drop ;
110 M: gc-map-insn assign-registers-in-insn
111 [ [ assign-insn-defs ] [ assign-insn-uses ] [ assign-insn-temps ] tri ]
112 [ gc-map>> [ assign-gc-roots ] [ assign-derived-roots ] bi ]
115 M: insn assign-registers-in-insn drop ;
117 : begin-block ( bb -- )
120 [ block-from activate-new-intervals ]
121 [ compute-edge-live-in ]
125 : assign-registers-in-block ( bb -- )
131 [ insn#>> 1 - prepare-insn ]
132 [ insn#>> prepare-insn ]
133 [ assign-registers-in-insn ]
138 ] change-instructions compute-live-out ;
140 : init-assignment ( live-intervals -- )
141 [ [ start>> ] map ] keep zip >min-heap unhandled-intervals set
142 <min-heap> pending-interval-heap set
143 H{ } clone pending-interval-assoc set
144 H{ } clone machine-live-ins set
145 H{ } clone machine-edge-live-ins set
146 H{ } clone machine-live-outs set ;
148 : assign-registers ( cfg live-intervals -- )
150 linearization-order [ kill-block?>> not ] filter
151 [ assign-registers-in-block ] each ;