]> gitweb.factorcode.org Git - factor.git/blob - basis/compiler/cfg/linear-scan/assignment/assignment.factor
compiler.cfg.*: even more compiler docs
[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/spill-slot )
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     [ dup vreg>reg ] H{ } map>assoc ;
38
39 SYMBOL: unhandled-intervals
40
41 SYMBOL: machine-live-ins
42
43 : machine-live-in ( bb -- assoc )
44     machine-live-ins get at ;
45
46 : compute-live-in ( bb -- )
47     [ live-in keys vregs>regs ] keep machine-live-ins get set-at ;
48
49 SYMBOL: machine-edge-live-ins
50
51 : machine-edge-live-in ( predecessor bb -- assoc )
52     machine-edge-live-ins get at at ;
53
54 : compute-edge-live-in ( bb -- )
55     [ edge-live-ins get at [ keys vregs>regs ] assoc-map ] keep
56     machine-edge-live-ins get set-at ;
57
58 SYMBOL: machine-live-outs
59
60 : machine-live-out ( bb -- assoc )
61     machine-live-outs get at ;
62
63 : compute-live-out ( bb -- )
64     [ live-out keys vregs>regs ] keep machine-live-outs get set-at ;
65
66 : heap-pop-while ( heap quot: ( key -- ? ) -- values )
67     '[ dup heap-empty? [ f f ] [ dup heap-peek @ ] if ]
68     [ over heap-pop* ] produce 2nip ; inline
69
70 : insert-spill ( live-interval -- )
71     [ reg>> ] [ spill-rep>> ] [ spill-to>> ] tri ##spill, ;
72
73 : handle-spill ( live-interval -- )
74     dup spill-to>> [ insert-spill ] [ drop ] if ;
75
76 : expire-interval ( live-interval -- )
77     [ remove-pending ] [ handle-spill ] bi ;
78
79 : expire-old-intervals ( n -- )
80     pending-interval-heap get swap '[ _ < ] heap-pop-while
81     [ expire-interval ] each ;
82
83 : insert-reload ( live-interval -- )
84     [ reg>> ] [ reload-rep>> ] [ reload-from>> ] tri ##reload, ;
85
86 : handle-reload ( live-interval -- )
87     dup reload-from>> [ insert-reload ] [ drop ] if ;
88
89 : activate-interval ( live-interval -- )
90     [ add-pending ] [ handle-reload ] bi ;
91
92 : activate-new-intervals ( n -- )
93     unhandled-intervals get swap '[ _ = ] heap-pop-while
94     [ activate-interval ] each ;
95
96 : prepare-insn ( n -- )
97     [ expire-old-intervals ] [ activate-new-intervals ] bi ;
98
99 GENERIC: assign-registers-in-insn ( insn -- )
100
101 RENAMING: assign [ vreg>reg ] [ vreg>reg ] [ vreg>reg ]
102
103 M: vreg-insn assign-registers-in-insn
104     [ assign-insn-defs ] [ assign-insn-uses ] [ assign-insn-temps ] tri ;
105
106 : assign-gc-roots ( gc-map -- )
107     [ [ vreg>spill-slot ] map ] change-gc-roots drop ;
108
109 : assign-derived-roots ( gc-map -- )
110     [ [ [ vreg>spill-slot ] bi@ ] assoc-map ] change-derived-roots drop ;
111
112 M: gc-map-insn assign-registers-in-insn
113     [ [ assign-insn-defs ] [ assign-insn-uses ] [ assign-insn-temps ] tri ]
114     [ gc-map>> [ assign-gc-roots ] [ assign-derived-roots ] bi ]
115     bi ;
116
117 M: insn assign-registers-in-insn drop ;
118
119 : begin-block ( bb -- )
120     {
121         [ basic-block set ]
122         [ block-from activate-new-intervals ]
123         [ compute-edge-live-in ]
124         [ compute-live-in ]
125     } cleave ;
126
127 :: assign-registers-in-block ( bb -- )
128     bb begin-block
129     bb [
130         [
131             [
132                 {
133                     [ insn#>> 1 - prepare-insn ]
134                     [ insn#>> prepare-insn ]
135                     [ assign-registers-in-insn ]
136                     [ , ]
137                 } cleave
138             ] each
139         ] V{ } make
140     ] change-instructions drop
141
142     bb compute-live-out ;
143
144 : init-assignment ( live-intervals -- )
145     [ [ start>> ] map ] keep zip >min-heap unhandled-intervals set
146     <min-heap> pending-interval-heap set
147     H{ } clone pending-interval-assoc set
148     H{ } clone machine-live-ins set
149     H{ } clone machine-edge-live-ins set
150     H{ } clone machine-live-outs set ;
151
152 : assign-registers ( cfg live-intervals -- )
153     init-assignment
154     linearization-order [ kill-block?>> not ] filter
155     [ assign-registers-in-block ] each ;