1 ! Copyright (C) 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays assocs sequences kernel locals fry
4 combinators stack-checker.backend
6 compiler.tree.recursive
7 compiler.tree.dead-code.branches
8 compiler.tree.dead-code.liveness
9 compiler.tree.dead-code.simple ;
10 IN: compiler.tree.dead-code.recursive
12 M: #enter-recursive compute-live-values*
13 #! If the output of an #enter-recursive is live, then the
14 #! corresponding inputs to the #call-recursive are live also.
15 [ out-d>> ] [ recursive-phi-in ] bi look-at-phi ;
17 M: #return-recursive compute-live-values*
18 [ out-d>> ] [ in-d>> ] bi look-at-mapping ;
20 M: #call-recursive compute-live-values*
21 #! If the output of a #call-recursive is live, then the
22 #! corresponding inputs to #return nodes are live also.
23 [ out-d>> ] [ label>> return>> in-d>> ] bi look-at-mapping ;
25 :: drop-dead-inputs ( inputs outputs -- #shuffle )
27 outputs inputs filter-corresponding make-values
32 M: #enter-recursive remove-dead-code*
33 [ filter-live ] change-out-d ;
35 : drop-call-recursive-inputs ( node -- #shuffle )
36 dup [ in-d>> ] [ label>> enter-out>> ] bi drop-dead-inputs
37 [ out-d>> >>in-d drop ]
41 :: (drop-call-recursive-outputs) ( inputs outputs -- #shuffle )
42 inputs outputs filter-corresponding make-values :> new-live-outputs
43 outputs filter-live :> live-outputs
50 : drop-call-recursive-outputs ( node -- #shuffle )
51 dup [ label>> return>> in-d>> ] [ out-d>> ] bi
52 (drop-call-recursive-outputs)
53 [ in-d>> >>out-d drop ] keep ;
55 M: #call-recursive remove-dead-code*
56 [ drop-call-recursive-inputs ]
58 [ drop-call-recursive-outputs ]
61 :: drop-recursive-inputs ( node -- shuffle )
62 node [ in-d>> ] [ label>> enter-out>> ] bi drop-dead-inputs :> shuffle
63 shuffle out-d>> :> new-outputs
65 [ [ label>> enter-recursive>> ] dip >>in-d drop ] [ >>in-d drop ] 2bi
68 :: drop-recursive-outputs ( node -- shuffle )
69 node label>> return>> :> return
70 return in-d>> filter-live :> new-inputs
71 return [ in-d>> ] [ out-d>> ] bi filter-corresponding :> new-outputs
73 [ new-inputs >>in-d new-outputs >>out-d drop ]
77 M: #recursive remove-dead-code* ( node -- nodes )
78 [ drop-recursive-inputs ]
80 [ (remove-dead-code) ] change-child
81 dup label>> [ filter-live ] change-enter-out drop
83 [ drop-recursive-outputs ] tri 3array ;
85 M: #return-recursive remove-dead-code* ;