1 ! Copyright (C) 2008, 2010 Slava Pestov.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays compiler.tree
4 compiler.tree.dead-code.branches
5 compiler.tree.dead-code.liveness compiler.tree.dead-code.simple
6 compiler.tree.recursive kernel locals sequences
7 stack-checker.backend ;
8 IN: compiler.tree.dead-code.recursive
10 M: #enter-recursive compute-live-values*
11 ! If the output of an #enter-recursive is live, then the
12 ! corresponding inputs to the #call-recursive are live also.
13 [ out-d>> ] [ recursive-phi-in ] bi look-at-phi ;
15 M: #return-recursive compute-live-values*
16 [ out-d>> ] [ in-d>> ] bi look-at-mapping ;
18 M: #call-recursive compute-live-values*
19 ! If the output of a #call-recursive is live, then the
20 ! corresponding inputs to #return nodes are live also.
21 [ out-d>> ] [ label>> return>> in-d>> ] bi look-at-mapping ;
23 :: drop-dead-inputs ( inputs outputs -- #shuffle )
25 outputs inputs filter-corresponding length make-values
30 M: #enter-recursive remove-dead-code*
31 [ filter-live ] change-out-d ;
33 : drop-call-recursive-inputs ( node -- #shuffle )
34 dup [ in-d>> ] [ label>> enter-out>> ] bi drop-dead-inputs
35 [ out-d>> >>in-d drop ]
39 :: (drop-call-recursive-outputs) ( inputs outputs -- #shuffle )
40 inputs outputs filter-corresponding length make-values :> new-live-outputs
41 outputs filter-live :> live-outputs
48 : drop-call-recursive-outputs ( node -- #shuffle )
49 dup [ label>> return>> in-d>> ] [ out-d>> ] bi
50 (drop-call-recursive-outputs)
51 [ in-d>> >>out-d drop ] keep ;
53 M: #call-recursive remove-dead-code*
54 [ drop-call-recursive-inputs ]
56 [ drop-call-recursive-outputs ]
59 :: drop-recursive-inputs ( node -- shuffle )
60 node [ in-d>> ] [ label>> enter-out>> ] bi drop-dead-inputs :> shuffle
61 shuffle out-d>> :> new-outputs
63 [ [ label>> enter-recursive>> ] dip >>in-d drop ] [ >>in-d drop ] 2bi
66 :: drop-recursive-outputs ( node -- shuffle )
67 node label>> return>> :> return
68 return in-d>> filter-live :> new-inputs
69 return [ in-d>> ] [ out-d>> ] bi filter-corresponding :> new-outputs
71 [ new-inputs >>in-d new-outputs >>out-d drop ]
75 M: #recursive remove-dead-code* ( node -- nodes )
76 [ drop-recursive-inputs ]
78 [ (remove-dead-code) ] change-child
79 dup label>> [ filter-live ] change-enter-out drop
81 [ drop-recursive-outputs ] tri 3array ;
83 M: #return-recursive remove-dead-code* ;