1 ! Copyright (C) 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: fry kernel sequences assocs accessors namespaces
4 math.intervals arrays classes.algebra combinators columns
7 compiler.tree.combinators
8 compiler.tree.propagation.info
9 compiler.tree.propagation.nodes
10 compiler.tree.propagation.simple
11 compiler.tree.propagation.constraints ;
12 IN: compiler.tree.propagation.branches
14 ! For conditionals, an assoc of child node # --> constraint
15 GENERIC: child-constraints ( node -- seq )
17 M: #if child-constraints
18 in-d>> first [ =t ] [ =f ] bi 2array ;
20 M: #dispatch child-constraints
21 children>> length f <repetition> ;
23 GENERIC: live-branches ( #branch -- indices )
26 in-d>> first value-info class>> {
27 { [ dup null-class? ] [ { f f } ] }
28 { [ dup true-class? ] [ { t f } ] }
29 { [ dup false-class? ] [ { f t } ] }
33 M: #dispatch live-branches
34 [ children>> length ] [ in-d>> first value-info interval>> ] bi
35 '[ _ interval-contains? ] map ;
37 : live-children ( #branch -- children )
38 [ children>> ] [ live-branches>> ] bi select-children ;
40 SYMBOL: infer-children-data
42 : copy-value-info ( -- )
43 value-infos [ clone ] change
44 constraints [ clone ] change ;
46 : no-value-info ( -- )
50 : infer-children ( node -- )
51 [ live-children ] [ child-constraints ] bi [
54 [ copy-value-info assume (propagate) ]
55 [ 2drop no-value-info ]
58 ] 2map infer-children-data set ;
60 : compute-phi-input-infos ( phi-in -- phi-info )
61 infer-children-data get
66 [ drop null-info ] [ value-info ] if
71 : annotate-phi-inputs ( #phi -- )
72 dup phi-in-d>> compute-phi-input-infos >>phi-info-d drop ;
74 : merge-value-infos ( infos outputs -- )
75 [ [ value-infos-union ] map ] dip set-value-infos ;
77 SYMBOL: condition-value
79 M: #phi propagate-before ( #phi -- )
80 [ annotate-phi-inputs ]
81 [ [ phi-info-d>> <flipped> ] [ out-d>> ] bi merge-value-infos ]
84 : branch-phi-constraints ( output values booleans -- )
89 drop condition-value get
90 [ [ =t ] [ =t ] bi* <--> ]
91 [ [ =f ] [ =f ] bi* <--> ] 2bi /\
97 drop condition-value get
98 [ [ =t ] [ =f ] bi* <--> ]
99 [ [ =f ] [ =t ] bi* <--> ] 2bi /\
106 condition-value get =t /\
114 condition-value get =f /\
122 ! [ [ =t ] bi@ <--> ]
123 ! [ [ =f ] bi@ <--> ] 2bi /\
130 ! [ [ =t ] bi@ <--> ]
131 ! [ [ =f ] bi@ <--> ] 2bi /\
137 M: #phi propagate-after ( #phi -- )
138 condition-value get [
140 [ phi-in-d>> <flipped> ]
141 [ phi-info-d>> <flipped> ] tri
143 [ possible-boolean-values ] map
144 branch-phi-constraints
148 M: #phi propagate-around ( #phi -- )
149 [ propagate-before ] [ propagate-after ] bi ;
151 M: #branch propagate-around
152 dup live-branches >>live-branches
153 [ infer-children ] [ annotate-node ] bi ;
155 M: #if propagate-around
156 [ in-d>> first condition-value set ] [ call-next-method ] bi ;
158 M: #dispatch propagate-around
159 condition-value off call-next-method ;