factor/basis/compiler/tree/dead-code/dead-code.factor

159 lines
4.0 KiB
Factor
Raw Normal View History

2008-07-20 05:24:37 -04:00
! Copyright (C) 2008 Slava Pestov.
! See http://factorcode.org/license.txt for BSD license.
USING: fry accessors namespaces assocs dequeues search-dequeues
kernel sequences words sets stack-checker.inlining
compiler.tree
2008-08-08 14:14:36 -04:00
compiler.tree.combinators
compiler.tree.dataflow-analysis
2008-08-08 14:14:36 -04:00
compiler.tree.dataflow-analysis.backward ;
2008-07-20 05:24:37 -04:00
IN: compiler.tree.dead-code
! Dead code elimination: remove #push and flushable #call whose
! outputs are unused using backward DFA.
2008-07-20 05:24:37 -04:00
GENERIC: mark-live-values ( node -- )
2008-08-08 14:14:36 -04:00
M: #introduce mark-live-values
value>> look-at-value ;
2008-07-20 05:24:37 -04:00
M: #if mark-live-values look-at-inputs ;
M: #dispatch mark-live-values look-at-inputs ;
M: #call mark-live-values
dup word>> "flushable" word-prop
[ drop ] [ [ look-at-inputs ] [ look-at-outputs ] bi ] if ;
2008-07-20 05:24:37 -04:00
2008-08-12 03:41:18 -04:00
M: #alien-invoke mark-live-values
[ look-at-inputs ] [ look-at-outputs ] bi ;
M: #alien-indirect mark-live-values
[ look-at-inputs ] [ look-at-outputs ] bi ;
2008-07-20 05:24:37 -04:00
M: #return mark-live-values
2008-07-27 03:32:40 -04:00
look-at-inputs ;
2008-07-20 05:24:37 -04:00
M: node mark-live-values drop ;
SYMBOL: live-values
2008-07-20 05:24:37 -04:00
: live-value? ( value -- ? ) live-values get at ;
2008-07-20 05:24:37 -04:00
: compute-live-values ( node -- )
[ mark-live-values ] backward-dfa live-values set ;
2008-07-20 05:24:37 -04:00
GENERIC: remove-dead-values* ( node -- )
M: #>r remove-dead-values*
dup out-r>> first live-value? [ { } >>out-r ] unless
dup in-d>> first live-value? [ { } >>in-d ] unless
drop ;
M: #r> remove-dead-values*
dup out-d>> first live-value? [ { } >>out-d ] unless
dup in-r>> first live-value? [ { } >>in-r ] unless
drop ;
M: #push remove-dead-values*
dup out-d>> first live-value? [ { } >>out-d ] unless
drop ;
: filter-corresponding-values ( in out -- in' out' )
zip live-values get '[ drop _ , key? ] assoc-filter unzip ;
: filter-live ( values -- values' )
[ live-value? ] filter ;
2008-08-08 14:14:36 -04:00
M: #call remove-dead-values*
[ filter-live ] change-in-d
[ filter-live ] change-out-d
drop ;
M: #recursive remove-dead-values*
[ filter-live ] change-in-d
drop ;
M: #call-recursive remove-dead-values*
[ filter-live ] change-in-d
[ filter-live ] change-out-d
drop ;
M: #enter-recursive remove-dead-values*
[ filter-live ] change-in-d
[ filter-live ] change-out-d
drop ;
M: #return-recursive remove-dead-values*
[ filter-live ] change-in-d
[ filter-live ] change-out-d
drop ;
2008-07-20 05:24:37 -04:00
M: #shuffle remove-dead-values*
[ filter-live ] change-in-d
[ filter-live ] change-out-d
drop ;
M: #declare remove-dead-values*
[ [ drop live-value? ] assoc-filter ] change-declaration
drop ;
2008-07-20 05:24:37 -04:00
M: #copy remove-dead-values*
dup
[ in-d>> ] [ out-d>> ] bi
filter-corresponding-values
[ >>in-d ] [ >>out-d ] bi*
drop ;
2008-07-20 05:24:37 -04:00
: remove-dead-phi-d ( #phi -- #phi )
dup
2008-07-22 05:45:03 -04:00
[ phi-in-d>> ] [ out-d>> ] bi
2008-07-20 05:24:37 -04:00
filter-corresponding-values
2008-07-22 05:45:03 -04:00
[ >>phi-in-d ] [ >>out-d ] bi* ;
2008-07-20 05:24:37 -04:00
: remove-dead-phi-r ( #phi -- #phi )
dup
2008-07-22 05:45:03 -04:00
[ phi-in-r>> ] [ out-r>> ] bi
2008-07-20 05:24:37 -04:00
filter-corresponding-values
2008-07-22 05:45:03 -04:00
[ >>phi-in-r ] [ >>out-r ] bi* ;
2008-07-20 05:24:37 -04:00
M: #phi remove-dead-values*
remove-dead-phi-d
remove-dead-phi-r
drop ;
M: node remove-dead-values* drop ;
2008-08-08 14:14:36 -04:00
: remove-dead-values ( nodes -- )
[ remove-dead-values* ] each-node ;
2008-07-20 05:24:37 -04:00
2008-08-08 14:14:36 -04:00
GENERIC: remove-dead-nodes* ( node -- node/f )
2008-08-08 14:14:36 -04:00
: prune-if-empty ( node seq -- node/f )
empty? [ drop f ] when ; inline
2008-08-08 14:14:36 -04:00
: live-call? ( #call -- ? ) out-d>> [ live-value? ] contains? ;
2008-07-20 05:24:37 -04:00
M: #declare remove-dead-nodes* dup declaration>> prune-if-empty ;
2008-08-08 14:14:36 -04:00
M: #call remove-dead-nodes* dup live-call? [ in-d>> #drop ] unless ;
2008-07-20 05:24:37 -04:00
M: #shuffle remove-dead-nodes* dup in-d>> prune-if-empty ;
2008-07-20 05:24:37 -04:00
M: #push remove-dead-nodes* dup out-d>> prune-if-empty ;
2008-07-20 05:24:37 -04:00
M: #>r remove-dead-nodes* dup in-d>> prune-if-empty ;
2008-07-20 05:24:37 -04:00
M: #r> remove-dead-nodes* dup in-r>> prune-if-empty ;
2008-07-20 05:24:37 -04:00
M: #copy remove-dead-nodes* dup in-d>> prune-if-empty ;
2008-07-20 05:24:37 -04:00
2008-08-08 14:14:36 -04:00
M: node remove-dead-nodes* ;
2008-08-08 14:14:36 -04:00
: remove-dead-nodes ( nodes -- nodes' )
[ remove-dead-nodes* ] map-nodes ;
2008-07-20 05:24:37 -04:00
: remove-dead-code ( node -- newnode )
2008-08-08 14:14:36 -04:00
[ compute-live-values ]
[ remove-dead-values ]
[ remove-dead-nodes ]
tri ;