1 ! Copyright (C) 2004, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: namespaces assocs sequences kernel generic assocs classes
4 vectors accessors combinators inference.dataflow inference.backend ;
9 : used-by ( value -- seq ) def-use get at ;
11 : unused? ( value -- ? )
14 : uses-values ( node seq -- )
15 [ def-use get push-at ] with each ;
17 : defs-values ( seq -- )
18 #! If there is no value, set it to a new empty vector,
19 #! otherwise do nothing.
20 [ def-use get [ V{ } like ] change-at ] each ;
22 GENERIC: node-def-use ( node -- )
24 : compute-def-use ( node -- node )
25 H{ } clone def-use set
26 dup [ node-def-use ] each-node ;
28 : nest-def-use ( node -- def-use )
29 [ compute-def-use drop def-use get ] with-scope ;
31 : (node-def-use) ( node -- )
33 [ dup in-d>> uses-values ]
34 [ dup in-r>> uses-values ]
35 [ out-d>> defs-values ]
36 [ out-r>> defs-values ]
39 M: object node-def-use (node-def-use) ;
41 ! nodes that don't use their values directly
43 #shuffle #>r #r> #call-label #merge #values #entry #declare ;
45 M: #passthru node-def-use drop ;
47 M: #return node-def-use
48 #! Values returned by local labels can be killed.
49 dup param>> [ drop ] [ (node-def-use) ] if ;
51 ! nodes that don't use their values directly
55 : purge-invariants ( stacks -- seq )
56 #! Output a sequence of values which are not present in the
57 #! same position in each sequence of the stacks sequence.
58 unify-lengths flip [ all-eq? not ] filter concat ;
60 M: #label node-def-use
63 dup node-child out-d>> ,
64 dup calls>> [ in-d>> , ] each
65 ] { } make purge-invariants uses-values ;
67 : branch-def-use ( #branch -- )
68 active-children [ in-d>> ] map
69 purge-invariants t swap uses-values ;
71 M: #branch node-def-use
72 #! This assumes that the last element of each branch is a
74 dup branch-def-use (node-def-use) ;
76 : compute-dead-literals ( -- values )
77 def-use get [ >r value? r> empty? and ] assoc-filter ;
82 GENERIC: kill-node* ( node -- node/t )
84 M: node kill-node* drop t ;
86 : prune-if ( node quot -- successor/t )
87 over >r call [ r> node-successor ] [ r> drop t ] if ;
90 M: #shuffle kill-node*
91 [ [ in-d>> empty? ] [ out-d>> empty? ] bi and ] prune-if ;
94 [ out-d>> empty? ] prune-if ;
97 [ in-d>> empty? ] prune-if ;
100 [ in-r>> empty? ] prune-if ;
102 : kill-node ( node -- node )
104 dup [ dead-literals get swap remove-all ] modify-values
105 dup kill-node* dup t eq? [
106 drop dup [ kill-nodes ] map-children
112 : kill-nodes ( node -- newnode )
113 [ kill-node ] transform-nodes ;
115 : kill-values ( node -- new-node )
116 #! Remove literals which are not actually used anywhere.
117 compute-dead-literals dup assoc-empty? [ drop ] [
118 dead-literals [ kill-nodes ] with-variable
121 : sole-consumer ( #call -- node/f )
122 out-d>> first used-by
123 dup length 1 = [ first ] [ drop f ] if ;
125 : splice-def-use ( node -- )
126 #! As a first approximation, we take all the values used
127 #! by the set of new nodes, and push a 't' on their
128 #! def-use list here. We could perform a full graph
129 #! substitution, but we don't need to, because the next
130 #! optimizer iteration will do that. We just need a minimal
131 #! degree of accuracy; the new values should be marked as
132 #! having _some_ usage, so that flushing doesn't erronously
134 nest-def-use keys def-use get [ t -rot push-at ] curry each ;