1 ! Copyright (C) 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors assocs kernel namespaces math sequences fry deques grouping
4 search-deques dlists sets make combinators compiler.cfg.copy-prop
5 compiler.cfg.def-use compiler.cfg.instructions compiler.cfg.registers
6 compiler.cfg.rpo compiler.cfg.hats ;
7 IN: compiler.cfg.stack-analysis
9 ! Convert stack operations to register operations
11 ! If 'poisoned' is set, disregard height information. This is set if we don't have
12 ! height change information for an instruction.
13 TUPLE: state locs>vregs vregs>locs changed-locs d-height r-height poisoned? ;
15 : <state> ( -- state )
17 H{ } clone >>locs>vregs
18 H{ } clone >>vregs>locs
19 H{ } clone >>changed-locs
25 [ clone ] change-locs>vregs
26 [ clone ] change-vregs>locs
27 [ clone ] change-changed-locs ;
29 : loc>vreg ( loc -- vreg ) state get locs>vregs>> at ;
31 : record-peek ( dst loc -- )
33 [ locs>vregs>> set-at ]
34 [ swapd vregs>locs>> set-at ]
37 : delete-old-vreg ( loc -- )
38 state get locs>vregs>> at [ state get vregs>locs>> delete-at ] when* ;
40 : changed-loc ( loc -- )
41 state get changed-locs>> conjoin ;
43 : redundant-replace? ( src loc -- ? )
46 : record-replace ( src loc -- )
47 ! Locs are not single assignment, which means we have to forget
48 ! that the previous vreg, if any, points at this loc. Also, record
49 ! that the loc changed so that all the right ##replace instructions
50 ! are emitted at a sync point.
51 2dup redundant-replace? [ 2drop ] [
52 dup delete-old-vreg dup changed-loc record-peek
55 : save-changed-locs ( state -- )
56 [ changed-locs>> ] [ locs>vregs>> ] bi '[
57 _ at swap 2dup redundant-replace?
58 [ 2drop ] [ ##replace ] if
61 : clear-state ( state -- )
65 [ changed-locs>> clear-assoc ]
66 [ locs>vregs>> clear-assoc ]
67 [ vregs>locs>> clear-assoc ]
70 ERROR: poisoned-state state ;
74 [ dup poisoned?>> [ poisoned-state ] [ drop ] if ]
76 [ d-height>> dup 0 = [ drop ] [ ##inc-d ] if ]
77 [ r-height>> dup 0 = [ drop ] [ ##inc-r ] if ]
81 : poison-state ( -- ) state get t >>poisoned? drop ;
83 GENERIC: translate-loc ( loc -- loc' )
85 M: ds-loc translate-loc n>> state get d-height>> + <ds-loc> ;
87 M: rs-loc translate-loc n>> state get r-height>> + <rs-loc> ;
89 ! Abstract interpretation
90 GENERIC: visit ( insn -- )
92 ! Instructions which don't have any effect on the stack
98 ##conditional-branch ;
100 M: neutral-insn visit , ;
102 : adjust-d ( n -- ) state get [ + ] change-d-height drop ;
104 M: ##inc-d visit n>> adjust-d ;
106 : adjust-r ( n -- ) state get [ + ] change-r-height drop ;
108 M: ##inc-r visit n>> adjust-r ;
110 : eliminate-peek ( dst src -- )
111 ! the requested stack location is already in 'src'
112 [ ##copy ] [ swap copies get set-at ] 2bi ;
116 [ dst>> ] [ loc>> translate-loc ] bi
117 dup loc>vreg dup [ nip eliminate-peek drop ] [ drop record-peek , ] if ;
120 [ src>> resolve ] [ loc>> translate-loc ] bi
124 [ call-next-method ] [ record-copy ] bi ;
127 [ call-next-method ] [ height>> [ adjust-d ] [ poison-state ] if* ] bi ;
129 M: ##fixnum-mul visit
130 call-next-method -1 adjust-d ;
132 M: ##fixnum-add visit
133 call-next-method -1 adjust-d ;
135 M: ##fixnum-sub visit
136 call-next-method -1 adjust-d ;
138 ! Instructions that poison the stack state
150 M: poison-insn visit call-next-method poison-state ;
152 ! Instructions that kill all live vregs
153 UNION: kill-vreg-insn
165 M: kill-vreg-insn visit sync-state , ;
167 : visit-alien-node ( node -- )
168 params>> [ out-d>> length ] [ in-d>> length ] bi - adjust-d ;
170 M: ##alien-invoke visit
171 [ call-next-method ] [ visit-alien-node ] bi ;
173 M: ##alien-indirect visit
174 [ call-next-method ] [ visit-alien-node ] bi ;
176 ! Basic blocks we still need to look at
179 : add-to-work-list ( basic-block -- )
180 work-list get push-front ;
182 ! Maps basic-blocks to states
183 SYMBOLS: state-in state-out ;
185 : sync-unpoisoned-states ( predecessors states -- )
187 dup poisoned?>> [ 2drop ] [
189 instructions>> building set
195 ERROR: must-equal-failed seq ;
197 : must-equal ( seq -- elt )
198 dup all-equal? [ first ] [ must-equal-failed ] if ;
200 : merge-heights ( state predecessors states -- state )
202 [ [ d-height>> ] map must-equal >>d-height ]
203 [ [ r-height>> ] map must-equal >>r-height ] bi ;
205 ERROR: inconsistent-vreg>loc states ;
207 : check-vreg>loc ( states -- )
208 ! The same vreg should not store different locs in
212 [ [ keys ] map concat prune ] keep
213 '[ _ [ at ] with map sift all-equal? ] all?
214 [ drop ] [ inconsistent-vreg>loc ] if ;
216 : insert-peek ( predecessor loc -- vreg )
218 [ instructions>> building ] dip '[ _ ^^peek ] with-variable ;
220 : merge-loc ( predecessors locs>vregs loc -- vreg )
221 ! Insert a ##phi in the current block where the input
222 ! is the vreg storing loc from each predecessor block
223 [ '[ [ _ ] dip at ] map ] keep
224 '[ [ ] [ _ insert-peek ] if ] 2map
227 : merge-locs ( state predecessors states -- state )
228 [ locs>vregs>> ] map dup [ keys ] map prune
230 [ 2nip ] [ merge-loc ] 3bi
231 ] with with H{ } map>assoc
234 : merge-states ( predecessors states -- state )
235 ! If any states are poisoned, save all registers
236 ! to the stack in each branch
238 dup [ poisoned?>> ] any? [
239 sync-unpoisoned-states <state>
245 ! what about vregs>locs
249 : block-in-state ( bb -- states )
250 predecessors>> dup state-out get '[ _ at ] map merge-states ;
252 : maybe-set-at ( value key assoc -- changed? )
253 3dup at* [ = [ 3drop f ] [ set-at t ] if ] [ 2drop set-at t ] if ;
255 : set-block-in-state ( state b -- )
256 state-in get set-at ;
258 : set-block-out-state ( bb state -- changed? )
259 swap state-out get maybe-set-at ;
261 : finish-block ( bb state -- )
262 [ drop ] [ set-block-out-state ] 2bi
263 [ successors>> [ add-to-work-list ] each ] [ drop ] if ;
265 : visit-block ( bb -- )
266 ! block-in-state may add phi nodes at the start of the basic block
267 ! so we wrap the whole thing with a 'make'
270 [ swap set-block-in-state ] [
272 [ instructions>> [ visit ] each ]
273 [ state get finish-block ]
278 ] V{ } make >>instructions drop ;
280 : visit-blocks ( bb -- )
281 reverse-post-order work-list get
282 [ '[ _ push-front ] each ] [ [ visit-block ] slurp-deque ] bi ;
284 : optimize-stack ( cfg -- cfg )
286 H{ } clone copies set
287 H{ } clone state-in set
288 H{ } clone state-out set
289 <hashed-dlist> work-list set
290 dup entry>> visit-blocks
293 ! XXX: what if our height doesn't match
294 ! a future block we're merging with?
295 ! - we should only poison tail calls
296 ! - non-tail poisoning nodes: ##alien-callback, ##call of a non-tail dispatch
297 ! do we need a distinction between height changes in code and height changes done by the callee