]> gitweb.factorcode.org Git - factor.git/blob - basis/compiler/cfg/linear-scan/assignment/assignment.factor
1780a1c907793d46a857ab3e21c9f6107253d052
[factor.git] / basis / compiler / cfg / linear-scan / assignment / assignment.factor
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
6 compiler.cfg
7 compiler.cfg.def-use
8 compiler.cfg.liveness
9 compiler.cfg.liveness.ssa
10 compiler.cfg.registers
11 compiler.cfg.instructions
12 compiler.cfg.linearization
13 compiler.cfg.ssa.destruction
14 compiler.cfg.renaming.functor
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
20
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
25
26 : add-pending ( live-interval -- )
27     [ dup end>> pending-interval-heap get heap-push ]
28     [ [ reg>> ] [ vreg>> ] bi pending-interval-assoc get set-at ]
29     bi ;
30
31 : remove-pending ( live-interval -- )
32     vreg>> pending-interval-assoc get delete-at ;
33
34 :: vreg>reg ( vreg -- reg )
35     ! If a live vreg is not in the pending set, then it must
36     ! have been spilled.
37     vreg leader :> leader
38     leader pending-interval-assoc get at* [
39         drop leader vreg rep-of lookup-spill-slot
40     ] unless ;
41
42 : vregs>regs ( vregs -- assoc )
43     [ f ] [ [ dup vreg>reg ] H{ } map>assoc ] if-empty ;
44
45 ! Minheap of live intervals which still need a register allocation
46 SYMBOL: unhandled-intervals
47
48 : add-unhandled ( live-interval -- )
49     dup start>> unhandled-intervals get heap-push ;
50
51 : init-unhandled ( live-intervals -- )
52     [ add-unhandled ] each ;
53
54 ! Liveness info is used by resolve pass
55
56 ! Mapping from basic blocks to values which are live at the start
57 ! on all incoming CFG edges
58 SYMBOL: machine-live-ins
59
60 : machine-live-in ( bb -- assoc )
61     machine-live-ins get at ;
62
63 : compute-live-in ( bb -- )
64     [ live-in keys vregs>regs ] keep machine-live-ins get set-at ;
65
66 ! Mapping from basic blocks to predecessors to values which are
67 ! live on a particular incoming edge
68 SYMBOL: machine-edge-live-ins
69
70 : machine-edge-live-in ( predecessor bb -- assoc )
71     machine-edge-live-ins get at at ;
72
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 ;
76
77 ! Mapping from basic blocks to values which are live at the end
78 SYMBOL: machine-live-outs
79
80 : machine-live-out ( bb -- assoc )
81     machine-live-outs get at ;
82
83 : compute-live-out ( bb -- )
84     [ live-out keys vregs>regs ] keep machine-live-outs get set-at ;
85
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
93     init-unhandled ;
94
95 : insert-spill ( live-interval -- )
96     [ reg>> ] [ spill-rep>> ] [ spill-to>> ] tri ##spill ;
97
98 : handle-spill ( live-interval -- )
99     dup spill-to>> [ insert-spill ] [ drop ] if ;
100
101 : expire-interval ( live-interval -- )
102     [ remove-pending ] [ handle-spill ] bi ;
103
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)
109         ] if
110     ] if ;
111
112 : expire-old-intervals ( n -- )
113     pending-interval-heap get (expire-old-intervals) ;
114
115 : insert-reload ( live-interval -- )
116     [ reg>> ] [ reload-rep>> ] [ reload-from>> ] tri ##reload ;
117
118 : handle-reload ( live-interval -- )
119     dup reload-from>> [ insert-reload ] [ drop ] if ;
120
121 : activate-interval ( live-interval -- )
122     [ add-pending ] [ handle-reload ] bi ;
123
124 : (activate-new-intervals) ( n heap -- )
125     dup heap-empty? [ 2drop ] [
126         2dup heap-peek nip = [
127             dup heap-pop drop activate-interval
128             (activate-new-intervals)
129         ] [ 2drop ] if
130     ] if ;
131
132 : activate-new-intervals ( n -- )
133     unhandled-intervals get (activate-new-intervals) ;
134
135 : prepare-insn ( n -- )
136     [ expire-old-intervals ] [ activate-new-intervals ] bi ;
137
138 GENERIC: assign-registers-in-insn ( insn -- )
139
140 RENAMING: assign [ vreg>reg ] [ vreg>reg ] [ vreg>reg ]
141
142 M: vreg-insn assign-registers-in-insn
143     [ assign-insn-defs ] [ assign-insn-uses ] [ assign-insn-temps ] tri ;
144
145 M: ##call-gc assign-registers-in-insn
146     dup call-next-method
147     [ [ vreg>reg ] map ] change-gc-roots drop ;
148
149 M: insn assign-registers-in-insn drop ;
150
151 : begin-block ( bb -- )
152     {
153         [ basic-block set ]
154         [ block-from activate-new-intervals ]
155         [ compute-edge-live-in ]
156         [ compute-live-in ]
157     } cleave ;
158
159 :: assign-registers-in-block ( bb -- )
160     bb [
161         [
162             bb begin-block
163             [
164                 {
165                     [ insn#>> 1 - prepare-insn ]
166                     [ insn#>> prepare-insn ]
167                     [ assign-registers-in-insn ]
168                     [ , ]
169                 } cleave
170             ] each
171             bb compute-live-out
172         ] V{ } make
173     ] change-instructions drop ;
174
175 : assign-registers ( live-intervals cfg -- )
176     [ init-assignment ] dip
177     linearization-order [ assign-registers-in-block ] each ;