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
8 compiler.tree.combinators
9 compiler.tree.propagation.info
10 compiler.tree.propagation.nodes
11 compiler.tree.propagation.simple
12 compiler.tree.propagation.constraints ;
13 IN: compiler.tree.propagation.branches
15 ! For conditionals, an assoc of child node # --> constraint
16 GENERIC: child-constraints ( node -- seq )
18 M: #if child-constraints
19 in-d>> first [ =t ] [ =f ] bi 2array ;
21 M: #dispatch child-constraints
22 children>> length f <repetition> ;
24 GENERIC: live-branches ( #branch -- indices )
27 in-d>> first value-info class>> {
28 { [ dup null-class? ] [ { f f } ] }
29 { [ dup true-class? ] [ { t f } ] }
30 { [ dup false-class? ] [ { f t } ] }
34 M: #dispatch live-branches
35 [ children>> length ] [ in-d>> first value-info interval>> ] bi
36 '[ _ interval-contains? ] map ;
38 : live-children ( #branch -- children )
39 [ children>> ] [ live-branches>> ] bi select-children ;
41 SYMBOL: infer-children-data
43 : copy-value-info ( -- )
44 value-infos [ H{ } clone suffix ] change
45 constraints [ H{ } clone suffix ] change ;
47 : no-value-info ( -- )
51 : infer-children ( node -- )
52 [ live-children ] [ child-constraints ] bi [
55 [ copy-value-info assume (propagate) ]
56 [ 2drop no-value-info ]
59 ] 2map infer-children-data set ;
61 : compute-phi-input-infos ( phi-in -- phi-info )
62 infer-children-data get
67 [ drop null-info ] [ value-info ] if
72 : annotate-phi-inputs ( #phi -- )
73 dup phi-in-d>> compute-phi-input-infos >>phi-info-d drop ;
75 : merge-value-infos ( infos outputs -- )
76 [ [ value-infos-union ] map ] dip set-value-infos ;
78 SYMBOL: condition-value
80 M: #phi propagate-before ( #phi -- )
81 [ annotate-phi-inputs ]
82 [ [ phi-info-d>> flip ] [ out-d>> ] bi merge-value-infos ]
85 : branch-phi-constraints ( output values booleans -- )
90 drop condition-value get
91 [ [ =t ] [ =t ] bi* <--> ]
92 [ [ =f ] [ =f ] bi* <--> ] 2bi /\
98 drop condition-value get
99 [ [ =t ] [ =f ] bi* <--> ]
100 [ [ =f ] [ =t ] bi* <--> ] 2bi /\
107 condition-value get =t /\
115 condition-value get =f /\
123 ! [ [ =t ] bi@ <--> ]
124 ! [ [ =f ] bi@ <--> ] 2bi /\
131 ! [ [ =t ] bi@ <--> ]
132 ! [ [ =f ] bi@ <--> ] 2bi /\
138 M: #phi propagate-after ( #phi -- )
139 condition-value get [
142 [ phi-info-d>> flip ] tri
144 [ possible-boolean-values ] map
145 branch-phi-constraints
149 M: #phi propagate-around ( #phi -- )
150 [ propagate-before ] [ propagate-after ] bi ;
152 M: #branch propagate-around
153 dup live-branches >>live-branches
154 [ infer-children ] [ annotate-node ] bi ;
156 M: #if propagate-around
157 [ in-d>> first condition-value set ] [ call-next-method ] bi ;
159 M: #dispatch propagate-around
160 condition-value off call-next-method ;