factor/basis/compiler/cfg/stack-analysis/stack-analysis.factor

149 lines
3.7 KiB
Factor
Raw Normal View History

2009-05-19 18:28:13 -04:00
! Copyright (C) 2009 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors assocs kernel namespaces math sequences fry grouping
sets make combinators
compiler.cfg
compiler.cfg.copy-prop
compiler.cfg.def-use
compiler.cfg.instructions
compiler.cfg.registers
compiler.cfg.rpo
compiler.cfg.hats
compiler.cfg.stack-analysis.state
compiler.cfg.stack-analysis.merge ;
2009-05-19 18:28:13 -04:00
IN: compiler.cfg.stack-analysis
2009-05-25 20:18:13 -04:00
: redundant-replace? ( vreg loc -- ? )
dup state get untranslate-loc n>> 0 <
[ 2drop t ] [ state get actual-locs>vregs>> at = ] if ;
2009-05-19 18:28:13 -04:00
: save-changed-locs ( state -- )
[ changed-locs>> keys ] [ locs>vregs>> ] bi '[
dup _ at swap 2dup redundant-replace?
[ 2drop ] [ state get untranslate-loc ##replace ] if
] each ;
2009-05-19 18:28:13 -04:00
ERROR: poisoned-state state ;
2009-05-19 18:28:13 -04:00
: sync-state ( -- )
state get {
[ dup poisoned?>> [ poisoned-state ] [ drop ] if ]
[ ds-height>> save-ds-height ]
[ rs-height>> save-rs-height ]
2009-05-19 18:28:13 -04:00
[ save-changed-locs ]
[ clear-state ]
} cleave ;
: poison-state ( -- ) state get t >>poisoned? drop ;
! Abstract interpretation
GENERIC: visit ( insn -- )
M: ##inc-d visit
n>> state get [ + ] change-ds-height drop ;
M: ##inc-r visit
n>> state get [ + ] change-rs-height drop ;
2009-05-19 18:28:13 -04:00
! Instructions which don't have any effect on the stack
UNION: neutral-insn
##effect
##flushable ;
2009-05-19 18:28:13 -04:00
M: neutral-insn visit , ;
2009-05-26 04:42:39 -04:00
UNION: sync-if-back-edge
##branch
##conditional-branch
2009-05-30 14:22:30 -04:00
##compare-imm-branch
2009-05-31 20:04:26 -04:00
##dispatch
##loop-entry ;
2009-05-26 04:42:39 -04:00
2009-05-31 20:04:26 -04:00
: back-edge? ( from to -- ? )
[ number>> ] bi@ > ;
: sync-state? ( -- ? )
basic-block get successors>>
[ [ predecessors>> ] keep '[ _ back-edge? ] any? ] any? ;
2009-05-31 20:04:26 -04:00
2009-05-26 04:42:39 -04:00
M: sync-if-back-edge visit
2009-05-31 20:04:26 -04:00
sync-state? [ sync-state ] when , ;
2009-05-26 04:42:39 -04:00
2009-05-19 18:28:13 -04:00
: eliminate-peek ( dst src -- )
! the requested stack location is already in 'src'
[ ##copy ] [ swap copies get set-at ] 2bi ;
M: ##peek visit
[ dst>> ] [ loc>> state get translate-loc ] bi dup loc>vreg
[ eliminate-peek ] [ [ record-peek ] [ ##peek ] 2bi ] ?if ;
2009-05-19 18:28:13 -04:00
M: ##replace visit
[ src>> resolve ] [ loc>> state get translate-loc ] bi
2009-05-19 18:28:13 -04:00
record-replace ;
M: ##copy visit
[ call-next-method ] [ record-copy ] bi ;
! Instructions that poison the stack state
UNION: poison-insn
##jump
##return
##callback-return
##fixnum-mul-tail
##fixnum-add-tail
##fixnum-sub-tail ;
M: poison-insn visit call-next-method poison-state ;
! Instructions that kill all live vregs
UNION: kill-vreg-insn
poison-insn
##stack-frame
##call
##prologue
##epilogue
##fixnum-mul
##fixnum-add
##fixnum-sub
##alien-invoke
##alien-indirect
##alien-callback ;
2009-05-19 18:28:13 -04:00
M: kill-vreg-insn visit sync-state , ;
! Maps basic-blocks to states
SYMBOLS: state-in state-out ;
: block-in-state ( bb -- states )
2009-05-25 20:18:13 -04:00
dup predecessors>> state-out get '[ _ at ] map merge-states ;
2009-05-19 18:28:13 -04:00
2009-05-25 20:18:13 -04:00
: set-block-in-state ( state bb -- )
[ clone ] dip state-in get set-at ;
2009-05-19 18:28:13 -04:00
: set-block-out-state ( state bb -- )
[ clone ] dip state-out get set-at ;
2009-05-19 18:28:13 -04:00
: visit-block ( bb -- )
! block-in-state may add phi nodes at the start of the basic block
! so we wrap the whole thing with a 'make'
[
2009-05-26 04:42:39 -04:00
dup basic-block set
dup block-in-state
[ swap set-block-in-state ] [
2009-05-30 14:22:30 -04:00
state [
[ instructions>> [ visit ] each ]
[ [ state get ] dip set-block-out-state ]
[ ]
tri
2009-05-30 14:22:30 -04:00
] with-variable
] 2bi
] V{ } make >>instructions drop ;
2009-05-19 18:28:13 -04:00
: stack-analysis ( cfg -- cfg' )
2009-05-19 18:28:13 -04:00
[
H{ } clone copies set
H{ } clone state-in set
H{ } clone state-out set
dup [ visit-block ] each-basic-block
2009-05-19 18:28:13 -04:00
] with-scope ;