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