]> gitweb.factorcode.org Git - factor.git/blob - basis/compiler/cfg/ssa/construction/construction-tests.factor
Fixes #2966
[factor.git] / basis / compiler / cfg / ssa / construction / construction-tests.factor
1 USING: accessors assocs compiler.cfg compiler.cfg.instructions
2 compiler.cfg.registers compiler.cfg.ssa.construction
3 compiler.cfg.ssa.construction.private compiler.cfg.utilities
4 compiler.test kernel namespaces sequences tools.test ;
5 IN: compiler.cfg.ssa.construction.tests
6
7 ! insert-phi-later
8 {
9     { V{ T{ ##phi { dst 789 } { inputs H{ } } } } }
10 } [
11     H{ } clone inserting-phis set
12     789 { } 0 insns>block insert-phi-later
13     inserting-phis get values
14 ] unit-test
15
16 { 99 55 } [
17     H{ } clone inserting-phis set
18     { } 55 insns>block { } 1 insns>block [ connect-bbs ] keep
19     99 swap insert-phi-later
20     inserting-phis get values first first
21     [ dst>> ] [ inputs>> keys first number>> ] bi
22 ] unit-test
23
24 ! live-phi?
25 { f t } [
26     HS{ 68 } live-phis set
27     T{ ##phi } live-phi?
28     T{ ##phi { dst 68 } }  live-phi?
29 ] unit-test
30
31
32 : reset-counters ( -- )
33     ! Reset counters so that results are deterministic w.r.t. hash order
34     reset-vreg-counter 0 basic-block set-global ;
35
36 : test-ssa ( -- )
37     0 get block>cfg
38     dup cfg set
39     construct-ssa ;
40
41 : clean-up-phis ( insns -- insns' )
42     [ dup ##phi? [ [ [ [ number>> ] dip ] assoc-map ] change-inputs ] when ] map ;
43
44 ! Test 1
45 reset-counters
46
47 V{
48     T{ ##load-integer f 1 100 }
49     T{ ##add-imm f 2 1 50 }
50     T{ ##add-imm f 2 2 10 }
51     T{ ##branch }
52 } 0 test-bb
53
54 V{
55     T{ ##load-integer f 3 3 }
56     T{ ##branch }
57 } 1 test-bb
58
59 V{
60     T{ ##load-integer f 3 4 }
61     T{ ##branch }
62 } 2 test-bb
63
64 V{
65     T{ ##replace f 3 D: 0 }
66     T{ ##return }
67 } 3 test-bb
68
69 0 { 1 2 } edges
70 1 3 edge
71 2 3 edge
72
73 { } [ test-ssa ] unit-test
74
75 {
76     V{
77         T{ ##load-integer f 1 100 }
78         T{ ##add-imm f 2 1 50 }
79         T{ ##add-imm f 3 2 10 }
80         T{ ##branch }
81     }
82 } [ 0 get instructions>> ] unit-test
83
84 {
85     V{
86         T{ ##load-integer f 4 3 }
87         T{ ##branch }
88     }
89 } [ 1 get instructions>> ] unit-test
90
91 {
92     V{
93         T{ ##load-integer f 5 4 }
94         T{ ##branch }
95     }
96 } [ 2 get instructions>> ] unit-test
97
98 {
99     V{
100         T{ ##phi f 6 H{ { 1 4 } { 2 5 } } }
101         T{ ##replace f 6 D: 0 }
102         T{ ##return }
103     }
104 } [
105     3 get instructions>>
106     clean-up-phis
107 ] unit-test
108
109 ! Test 2
110 reset-counters
111
112 V{ } 0 test-bb
113 V{ } 1 test-bb
114 V{ T{ ##peek f 0 D: 0 } } 2 test-bb
115 V{ T{ ##peek f 0 D: 0 } } 3 test-bb
116 V{ T{ ##replace f 0 D: 0 } } 4 test-bb
117 V{ } 5 test-bb
118 V{ } 6 test-bb
119
120 0 { 1 5 } edges
121 1 { 2 3 } edges
122 2 4 edge
123 3 4 edge
124 4 6 edge
125 5 6 edge
126
127 { } [ test-ssa ] unit-test
128
129 {
130     V{
131         T{ ##phi f 3 H{ { 2 1 } { 3 2 } } }
132         T{ ##replace f 3 D: 0 }
133     }
134 } [
135     4 get instructions>>
136     clean-up-phis
137 ] unit-test
138
139 ! Test 3
140 reset-counters
141
142 V{
143     T{ ##branch }
144 } 0 test-bb
145
146 V{
147     T{ ##load-integer f 3 3 }
148     T{ ##branch }
149 } 1 test-bb
150
151 V{
152     T{ ##load-integer f 3 4 }
153     T{ ##branch }
154 } 2 test-bb
155
156 V{
157     T{ ##branch }
158 } 3 test-bb
159
160 V{
161     T{ ##return }
162 } 4 test-bb
163
164 0 { 1 2 3 } edges
165 1 4 edge
166 2 4 edge
167 3 4 edge
168
169 { } [ test-ssa ] unit-test
170
171 { V{ } } [ 4 get instructions>> [ ##phi? ] filter ] unit-test
172
173 ! Test 4
174 reset-counters
175
176 V{
177     T{ ##branch }
178 } 0 test-bb
179
180 V{
181     T{ ##branch }
182 } 1 test-bb
183
184 V{
185     T{ ##load-integer f 0 4 }
186     T{ ##branch }
187 } 2 test-bb
188
189 V{
190     T{ ##load-integer f 0 4 }
191     T{ ##branch }
192 } 3 test-bb
193
194 V{
195     T{ ##branch }
196 } 4 test-bb
197
198 V{
199     T{ ##branch }
200 } 5 test-bb
201
202 V{
203     T{ ##branch }
204 } 6 test-bb
205
206 V{
207     T{ ##return }
208 } 7 test-bb
209
210 0 { 1 6 } edges
211 1 { 2 3 4 } edges
212 2 5 edge
213 3 5 edge
214 4 5 edge
215 5 7 edge
216 6 7 edge
217
218 { } [ test-ssa ] unit-test
219
220 { V{ } } [ 5 get instructions>> [ ##phi? ] filter ] unit-test
221
222 { V{ } } [ 7 get instructions>> [ ##phi? ] filter ] unit-test