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