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