2008-08-13 19:56:50 -04:00
|
|
|
! Copyright (C) 2008 Slava Pestov.
|
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2008-08-14 00:52:49 -04:00
|
|
|
USING: kernel accessors words assocs sequences arrays namespaces
|
2008-08-22 18:38:23 -04:00
|
|
|
fry locals classes.algebra stack-checker.backend
|
|
|
|
compiler.tree
|
|
|
|
compiler.tree.propagation.info
|
2008-08-13 19:56:50 -04:00
|
|
|
compiler.tree.dead-code.liveness ;
|
|
|
|
IN: compiler.tree.dead-code.simple
|
|
|
|
|
2008-08-22 18:38:23 -04:00
|
|
|
: flushable? ( word -- ? )
|
|
|
|
[ "flushable" word-prop ] [ "predicating" word-prop ] bi or ;
|
|
|
|
|
|
|
|
: flushable-call? ( #call -- ? )
|
|
|
|
dup word>> dup flushable? [
|
|
|
|
"input-classes" word-prop dup [
|
|
|
|
[ node-input-infos ] dip
|
|
|
|
[ [ class>> ] dip class<= ] 2all?
|
|
|
|
] [ 2drop t ] if
|
|
|
|
] [ 2drop f ] if ;
|
|
|
|
|
2008-08-13 19:56:50 -04:00
|
|
|
M: #call mark-live-values*
|
2008-08-22 18:38:23 -04:00
|
|
|
dup flushable-call? [ drop ] [ look-at-inputs ] if ;
|
2008-08-13 19:56:50 -04:00
|
|
|
|
|
|
|
M: #alien-invoke mark-live-values* look-at-inputs ;
|
|
|
|
|
|
|
|
M: #alien-indirect mark-live-values* look-at-inputs ;
|
|
|
|
|
|
|
|
M: #return mark-live-values* look-at-inputs ;
|
|
|
|
|
|
|
|
: look-at-mapping ( value inputs outputs -- )
|
|
|
|
[ index ] dip over [ nth look-at-value ] [ 2drop ] if ;
|
|
|
|
|
|
|
|
M: #copy compute-live-values*
|
|
|
|
#! If the output of a copy is live, then the corresponding
|
|
|
|
#! input is live also.
|
|
|
|
[ out-d>> ] [ in-d>> ] bi look-at-mapping ;
|
|
|
|
|
|
|
|
M: #call compute-live-values* nip look-at-inputs ;
|
|
|
|
|
|
|
|
M: #>r compute-live-values*
|
|
|
|
[ out-r>> ] [ in-d>> ] bi look-at-mapping ;
|
|
|
|
|
|
|
|
M: #r> compute-live-values*
|
|
|
|
[ out-d>> ] [ in-r>> ] bi look-at-mapping ;
|
|
|
|
|
|
|
|
M: #shuffle compute-live-values*
|
|
|
|
mapping>> at look-at-value ;
|
|
|
|
|
|
|
|
M: #alien-invoke compute-live-values* nip look-at-inputs ;
|
|
|
|
|
|
|
|
M: #alien-indirect compute-live-values* nip look-at-inputs ;
|
|
|
|
|
2008-08-14 00:52:49 -04:00
|
|
|
: filter-mapping ( assoc -- assoc' )
|
|
|
|
live-values get '[ drop , key? ] assoc-filter ;
|
|
|
|
|
2008-08-22 04:12:15 -04:00
|
|
|
: filter-corresponding ( new old -- old' )
|
|
|
|
#! Remove elements from 'old' if the element with the same
|
|
|
|
#! index in 'new' is dead.
|
2008-08-14 00:52:49 -04:00
|
|
|
zip filter-mapping values ;
|
2008-08-13 19:56:50 -04:00
|
|
|
|
|
|
|
: filter-live ( values -- values' )
|
|
|
|
[ live-value? ] filter ;
|
|
|
|
|
2008-08-22 19:09:48 -04:00
|
|
|
:: drop-values ( inputs outputs mapping-keys mapping-values -- #shuffle )
|
|
|
|
inputs
|
|
|
|
outputs
|
|
|
|
outputs
|
|
|
|
mapping-keys
|
|
|
|
mapping-values
|
|
|
|
filter-corresponding zip #shuffle ; inline
|
|
|
|
|
|
|
|
:: drop-dead-values ( outputs -- #shuffle )
|
|
|
|
[let* | new-outputs [ outputs make-values ]
|
|
|
|
live-outputs [ outputs filter-live ] |
|
|
|
|
new-outputs
|
|
|
|
live-outputs
|
|
|
|
outputs
|
|
|
|
new-outputs
|
|
|
|
drop-values
|
2008-08-14 00:52:49 -04:00
|
|
|
] ;
|
|
|
|
|
2008-08-22 19:09:48 -04:00
|
|
|
: drop-dead-outputs ( node -- nodes )
|
|
|
|
dup out-d>> drop-dead-values
|
|
|
|
[ in-d>> >>out-d drop ] [ 2array ] 2bi ;
|
|
|
|
|
2008-08-14 00:52:49 -04:00
|
|
|
M: #introduce remove-dead-code* ( #introduce -- nodes )
|
|
|
|
drop-dead-outputs ;
|
|
|
|
|
2008-08-13 19:56:50 -04:00
|
|
|
M: #>r remove-dead-code*
|
|
|
|
[ filter-live ] change-out-r
|
|
|
|
[ filter-live ] change-in-d
|
|
|
|
dup in-d>> empty? [ drop f ] when ;
|
|
|
|
|
|
|
|
M: #r> remove-dead-code*
|
|
|
|
[ filter-live ] change-out-d
|
|
|
|
[ filter-live ] change-in-r
|
|
|
|
dup in-r>> empty? [ drop f ] when ;
|
|
|
|
|
|
|
|
M: #push remove-dead-code*
|
|
|
|
dup out-d>> first live-value? [ drop f ] unless ;
|
|
|
|
|
|
|
|
: dead-flushable-call? ( #call -- ? )
|
2008-08-22 18:38:23 -04:00
|
|
|
dup flushable-call? [
|
|
|
|
out-d>> [ live-value? not ] all?
|
|
|
|
] [ drop f ] if ;
|
2008-08-13 19:56:50 -04:00
|
|
|
|
|
|
|
: remove-flushable-call ( #call -- node )
|
|
|
|
in-d>> #drop remove-dead-code* ;
|
|
|
|
|
|
|
|
: some-outputs-dead? ( #call -- ? )
|
|
|
|
out-d>> [ live-value? not ] contains? ;
|
|
|
|
|
|
|
|
M: #call remove-dead-code*
|
|
|
|
dup dead-flushable-call? [
|
|
|
|
remove-flushable-call
|
|
|
|
] [
|
|
|
|
dup some-outputs-dead? [
|
|
|
|
drop-dead-outputs
|
|
|
|
] when
|
|
|
|
] if ;
|
|
|
|
|
|
|
|
M: #shuffle remove-dead-code*
|
|
|
|
[ filter-live ] change-in-d
|
|
|
|
[ filter-live ] change-out-d
|
2008-08-14 00:52:49 -04:00
|
|
|
[ filter-mapping ] change-mapping
|
2008-08-13 19:56:50 -04:00
|
|
|
dup in-d>> empty? [ drop f ] when ;
|
|
|
|
|
|
|
|
M: #copy remove-dead-code*
|
|
|
|
[ in-d>> ] [ out-d>> ] bi
|
|
|
|
2dup swap zip #shuffle
|
|
|
|
remove-dead-code* ;
|
2008-08-15 00:35:19 -04:00
|
|
|
|
|
|
|
M: #terminate remove-dead-code*
|
|
|
|
[ filter-live ] change-in-d
|
|
|
|
[ filter-live ] change-in-r ;
|