]> gitweb.factorcode.org Git - factor.git/blob - basis/compiler/tree/propagation/branches/branches.factor
cpu.ppc: fix load errors
[factor.git] / basis / compiler / tree / propagation / branches / branches.factor
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
5 stack-checker.branches
6 compiler.utilities
7 compiler.tree
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
14
15 ! For conditionals, an assoc of child node # --> constraint
16 GENERIC: child-constraints ( node -- seq )
17
18 M: #if child-constraints
19     in-d>> first [ =t ] [ =f ] bi 2array ;
20
21 M: #dispatch child-constraints
22     children>> length f <repetition> ;
23
24 GENERIC: live-branches ( #branch -- indices )
25
26 M: #if live-branches
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 } ] }
31         [ { t t } ]
32     } cond nip ;
33
34 M: #dispatch live-branches
35     [ children>> length ] [ in-d>> first value-info interval>> ] bi
36     '[ _ interval-contains? ] map ;
37
38 : live-children ( #branch -- children )
39     [ children>> ] [ live-branches>> ] bi select-children ;
40
41 SYMBOL: infer-children-data
42
43 : copy-value-info ( -- )
44     value-infos [ H{ } clone suffix ] change
45     constraints [ H{ } clone suffix ] change ;
46
47 : no-value-info ( -- )
48     value-infos off
49     constraints off ;
50
51 : infer-children ( node -- )
52     [ live-children ] [ child-constraints ] bi [
53         [
54             over
55             [ copy-value-info assume (propagate) ]
56             [ 2drop no-value-info ]
57             if
58         ] H{ } make-assoc
59     ] 2map infer-children-data set ;
60
61 : compute-phi-input-infos ( phi-in -- phi-info )
62     infer-children-data get
63     [
64         '[
65             _ [
66                 dup +bottom+ eq?
67                 [ drop null-info ] [ value-info ] if
68             ] bind
69         ] map
70     ] 2map ;
71
72 : annotate-phi-inputs ( #phi -- )
73     dup phi-in-d>> compute-phi-input-infos >>phi-info-d drop ;
74
75 : merge-value-infos ( infos outputs -- )
76     [ [ value-infos-union ] map ] dip set-value-infos ;
77
78 SYMBOL: condition-value
79
80 M: #phi propagate-before ( #phi -- )
81     [ annotate-phi-inputs ]
82     [ [ phi-info-d>> flip ] [ out-d>> ] bi merge-value-infos ]
83     bi ;
84
85 : branch-phi-constraints ( output values booleans -- )
86      {
87         {
88             { { t } { f } }
89             [
90                 drop condition-value get
91                 [ [ =t ] [ =t ] bi* <--> ]
92                 [ [ =f ] [ =f ] bi* <--> ] 2bi /\
93             ]
94         }
95         {
96             { { f } { t } }
97             [
98                 drop condition-value get
99                 [ [ =t ] [ =f ] bi* <--> ]
100                 [ [ =f ] [ =t ] bi* <--> ] 2bi /\
101             ]
102         }
103         {
104             { { t f } { f } }
105             [
106                 first =t
107                 condition-value get =t /\
108                 swap t-->
109             ]
110         }
111         {
112             { { f } { t f } }
113             [
114                 second =t
115                 condition-value get =f /\
116                 swap t-->
117             ]
118         }
119         ! {
120         !     { { t f } { } }
121         !     [ B
122         !         first
123         !         [ [ =t ] bi@ <--> ]
124         !         [ [ =f ] bi@ <--> ] 2bi /\
125         !     ]
126         ! }
127         ! {
128         !     { { } { t f } }
129         !     [
130         !         second
131         !         [ [ =t ] bi@ <--> ]
132         !         [ [ =f ] bi@ <--> ] 2bi /\
133         !     ]
134         ! }
135         [ 3drop f ]
136     } case assume ;
137
138 M: #phi propagate-after ( #phi -- )
139     condition-value get [
140         [ out-d>> ]
141         [ phi-in-d>> flip ]
142         [ phi-info-d>> flip ] tri
143         [
144             [ possible-boolean-values ] map
145             branch-phi-constraints
146         ] 3each
147     ] [ drop ] if ;
148
149 M: #phi propagate-around ( #phi -- )
150     [ propagate-before ] [ propagate-after ] bi ;
151
152 M: #branch propagate-around
153     dup live-branches >>live-branches
154     [ infer-children ] [ annotate-node ] bi ;
155
156 M: #if propagate-around
157     [ in-d>> first condition-value set ] [ call-next-method ] bi ;
158
159 M: #dispatch propagate-around
160     condition-value off call-next-method ;