]> gitweb.factorcode.org Git - factor.git/blob - basis/compiler/tree/dead-code/branches/branches.factor
Updating code for make and fry changes
[factor.git] / basis / compiler / tree / dead-code / branches / branches.factor
1 ! Copyright (C) 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: sequences namespaces kernel accessors assocs sets fry
4 arrays combinators columns stack-checker.backend
5 stack-checker.branches compiler.tree compiler.tree.combinators
6 compiler.tree.dead-code.liveness compiler.tree.dead-code.simple
7 ;
8 IN: compiler.tree.dead-code.branches
9
10 M: #if mark-live-values* look-at-inputs ;
11
12 M: #dispatch mark-live-values* look-at-inputs ;
13
14 : look-at-phi ( value outputs inputs -- )
15     [ index ] dip swap dup [ <column> look-at-values ] [ 2drop ] if ;
16
17 M: #phi compute-live-values*
18     #! If any of the outputs of a #phi are live, then the
19     #! corresponding inputs are live too.
20     [ out-d>> ] [ phi-in-d>> ] bi look-at-phi ;
21
22 SYMBOL: if-node
23
24 M: #branch remove-dead-code*
25     [ [ [ (remove-dead-code) ] map ] change-children ]
26     [ if-node set ]
27     bi ;
28
29 : remove-phi-inputs ( #phi -- )
30     if-node get children>>
31     [ dup ends-with-terminate? [ drop f ] [ peek out-d>> ] if ] map
32     pad-with-bottom >>phi-in-d drop ;
33
34 : live-value-indices ( values -- indices )
35     [ length ] keep live-values get
36     '[ _ nth _ key? ] filter ; inline
37
38 : drop-indexed-values ( values indices -- node )
39     [ drop filter-live ] [ nths ] 2bi
40     [ make-values ] keep
41     [ drop ] [ zip ] 2bi
42     #shuffle ;
43
44 : insert-drops ( nodes values indices -- nodes' )
45     '[
46         over ends-with-terminate?
47         [ drop ] [ _ drop-indexed-values suffix ] if
48     ] 2map ;
49
50 : hoist-drops ( #phi -- )
51     if-node get swap
52     [ phi-in-d>> ] [ out-d>> live-value-indices ] bi
53     '[ _ _ insert-drops ] change-children drop ;
54
55 : remove-phi-outputs ( #phi -- )
56     [ filter-live ] change-out-d drop ;
57
58 M: #phi remove-dead-code*
59     {
60         [ hoist-drops ]
61         [ remove-phi-inputs ]
62         [ remove-phi-outputs ]
63         [ ]
64     } cleave ;