1 ! Copyright (C) 2008, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors assocs columns combinators compiler.tree
4 compiler.tree.dead-code.liveness compiler.tree.dead-code.simple
5 fry kernel namespaces sequences stack-checker.backend
6 stack-checker.branches ;
7 IN: compiler.tree.dead-code.branches
9 M: #if mark-live-values* look-at-inputs ;
11 M: #dispatch mark-live-values* look-at-inputs ;
13 : look-at-phi ( value outputs inputs -- )
14 [ index ] dip swap [ <column> look-at-values ] [ drop ] if* ;
16 M: #phi compute-live-values*
17 ! If any of the outputs of a #phi are live, then the
18 ! corresponding inputs are live too.
19 [ out-d>> ] [ phi-in-d>> ] bi look-at-phi ;
23 M: #branch remove-dead-code*
24 [ [ [ (remove-dead-code) ] map ] change-children ]
28 : remove-phi-inputs ( #phi -- )
29 if-node get children>>
30 [ dup ends-with-terminate? [ drop f ] [ last out-d>> ] if ] map
31 pad-with-bottom >>phi-in-d drop ;
33 : live-value-indices ( values -- indices )
34 [ length iota ] keep live-values get
35 '[ _ nth _ key? ] filter ; inline
37 : drop-indexed-values ( values indices -- node )
38 [ drop filter-live ] [ swap nths ] 2bi
39 [ length make-values ] keep
43 : insert-drops ( nodes values indices -- nodes' )
45 over ends-with-terminate?
46 [ drop ] [ _ drop-indexed-values suffix ] if
49 : hoist-drops ( #phi -- )
51 [ phi-in-d>> ] [ out-d>> live-value-indices ] bi
52 '[ _ _ insert-drops ] change-children drop ;
54 : remove-phi-outputs ( #phi -- )
55 [ filter-live ] change-out-d drop ;
57 M: #phi remove-dead-code*
61 [ remove-phi-outputs ]