]> gitweb.factorcode.org Git - factor.git/blob - basis/compiler/cfg/linear-scan/assignment/assignment.factor
compiler.cfg.linear-scan.*: refactoring to simplify init-allocator and get rid of...
[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 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 locals make math namespaces sequences sets ;
9 FROM: namespaces => set ;
10 IN: compiler.cfg.linear-scan.assignment
11
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
16
17 : add-pending ( live-interval -- )
18     [ dup end>> pending-interval-heap get heap-push ]
19     [ [ reg>> ] [ vreg>> ] bi pending-interval-assoc get set-at ]
20     bi ;
21
22 : remove-pending ( live-interval -- )
23     vreg>> pending-interval-assoc get delete-at ;
24
25 :: vreg>reg ( vreg -- reg )
26     vreg leader :> leader
27     leader pending-interval-assoc get at* [
28         drop leader vreg rep-of lookup-spill-slot
29     ] unless ;
30
31 ERROR: not-spilled-error vreg ;
32
33 : vreg>spill-slot ( vreg -- spill-slot )
34     dup vreg>reg dup spill-slot? [ nip ] [ drop leader not-spilled-error ] if ;
35
36 : vregs>regs ( vregs -- assoc )
37     [ f ] [ [ dup vreg>reg ] H{ } map>assoc ] if-empty ;
38
39 SYMBOL: unhandled-intervals
40
41 ! Liveness info is used by resolve pass
42 SYMBOL: machine-live-ins
43
44 : machine-live-in ( bb -- assoc )
45     machine-live-ins get at ;
46
47 : compute-live-in ( bb -- )
48     [ live-in keys vregs>regs ] keep machine-live-ins get set-at ;
49
50 ! Mapping from basic blocks to predecessors to values which are
51 ! live on a particular incoming edge
52 SYMBOL: machine-edge-live-ins
53
54 : machine-edge-live-in ( predecessor bb -- assoc )
55     machine-edge-live-ins get at at ;
56
57 : compute-edge-live-in ( bb -- )
58     [ edge-live-ins get at [ keys vregs>regs ] assoc-map ] keep
59     machine-edge-live-ins get set-at ;
60
61 SYMBOL: machine-live-outs
62
63 : machine-live-out ( bb -- assoc )
64     machine-live-outs get at ;
65
66 : compute-live-out ( bb -- )
67     [ live-out keys vregs>regs ] keep machine-live-outs get set-at ;
68
69 : init-assignment ( live-intervals -- )
70     live-intervals>min-heap unhandled-intervals set
71     <min-heap> pending-interval-heap set
72     H{ } clone pending-interval-assoc set
73     H{ } clone machine-live-ins set
74     H{ } clone machine-edge-live-ins set
75     H{ } clone machine-live-outs set ;
76
77 : heap-pop-while ( heap quot: ( key -- ? ) -- values )
78     '[ dup heap-empty? [ f f ] [ dup heap-peek @ ] if ]
79     [ over heap-pop* ] produce 2nip ; inline
80
81 : insert-spill ( live-interval -- )
82     [ reg>> ] [ spill-rep>> ] [ spill-to>> ] tri ##spill, ;
83
84 : handle-spill ( live-interval -- )
85     dup spill-to>> [ insert-spill ] [ drop ] if ;
86
87 : expire-interval ( live-interval -- )
88     [ remove-pending ] [ handle-spill ] bi ;
89
90 : expire-old-intervals ( n -- )
91     pending-interval-heap get swap '[ _ < ] heap-pop-while
92     [ expire-interval ] each ;
93
94 : insert-reload ( live-interval -- )
95     [ reg>> ] [ reload-rep>> ] [ reload-from>> ] tri ##reload, ;
96
97 : handle-reload ( live-interval -- )
98     dup reload-from>> [ insert-reload ] [ drop ] if ;
99
100 : activate-interval ( live-interval -- )
101     [ add-pending ] [ handle-reload ] bi ;
102
103 : activate-new-intervals ( n -- )
104     unhandled-intervals get swap '[ _ = ] heap-pop-while
105     [ activate-interval ] each ;
106
107 : prepare-insn ( n -- )
108     [ expire-old-intervals ] [ activate-new-intervals ] bi ;
109
110 GENERIC: assign-registers-in-insn ( insn -- )
111
112 RENAMING: assign [ vreg>reg ] [ vreg>reg ] [ vreg>reg ]
113
114 M: vreg-insn assign-registers-in-insn
115     [ assign-insn-defs ] [ assign-insn-uses ] [ assign-insn-temps ] tri ;
116
117 : assign-gc-roots ( gc-map -- )
118     [ [ vreg>spill-slot ] map ] change-gc-roots drop ;
119
120 : assign-derived-roots ( gc-map -- )
121     [ [ [ vreg>spill-slot ] bi@ ] assoc-map ] change-derived-roots drop ;
122
123 M: gc-map-insn assign-registers-in-insn
124     [ [ assign-insn-defs ] [ assign-insn-uses ] [ assign-insn-temps ] tri ]
125     [ gc-map>> [ assign-gc-roots ] [ assign-derived-roots ] bi ]
126     bi ;
127
128 M: insn assign-registers-in-insn drop ;
129
130 : begin-block ( bb -- )
131     {
132         [ basic-block set ]
133         [ block-from activate-new-intervals ]
134         [ compute-edge-live-in ]
135         [ compute-live-in ]
136     } cleave ;
137
138 :: assign-registers-in-block ( bb -- )
139     bb [
140         [
141             bb begin-block
142             [
143                 {
144                     [ insn#>> 1 - prepare-insn ]
145                     [ insn#>> prepare-insn ]
146                     [ assign-registers-in-insn ]
147                     [ , ]
148                 } cleave
149             ] each
150             bb compute-live-out
151         ] V{ } make
152     ] change-instructions drop ;
153
154 : assign-registers ( live-intervals cfg -- )
155     [ init-assignment ] dip
156     linearization-order [ kill-block?>> not ] filter
157     [ assign-registers-in-block ] each ;