Debian Codegraph @

mypyc/test-data/opt-flag-elimination.test

raw
1 -- Test cases for "flag elimination" optimization. Used to optimize away
2 -- registers that are always used immediately after assignment as branch conditions.
3
4 [case testFlagEliminationSimple]
5 def c() -> bool:
6 return True
7 def d() -> bool:
8 return True
9
10 def f(x: bool) -> int:
11 if x:
12 b = c()
13 else:
14 b = d()
15 if b:
16 return 1
17 else:
18 return 2
19 [out]
20 def c():
21 L0:
22 return 1
23 def d():
24 L0:
25 return 1
26 def f(x):
27 x, r0, r1 :: bool
28 L0:
29 if x goto L1 else goto L2 :: bool
30 L1:
31 r0 = c()
32 if r0 goto L3 else goto L4 :: bool
33 L2:
34 r1 = d()
35 if r1 goto L3 else goto L4 :: bool
36 L3:
37 return 2
38 L4:
39 return 4
40
41 [case testFlagEliminationOneAssignment]
42 def c() -> bool:
43 return True
44
45 def f(x: bool) -> int:
46 # Not applied here
47 b = c()
48 if b:
49 return 1
50 else:
51 return 2
52 [out]
53 def c():
54 L0:
55 return 1
56 def f(x):
57 x, r0, b :: bool
58 L0:
59 r0 = c()
60 b = r0
61 if b goto L1 else goto L2 :: bool
62 L1:
63 return 2
64 L2:
65 return 4
66
67 [case testFlagEliminationThreeCases]
68 def c(x: int) -> bool:
69 return True
70
71 def f(x: bool, y: bool) -> int:
72 if x:
73 b = c(1)
74 elif y:
75 b = c(2)
76 else:
77 b = c(3)
78 if b:
79 return 1
80 else:
81 return 2
82 [out]
83 def c(x):
84 x :: int
85 L0:
86 return 1
87 def f(x, y):
88 x, y, r0, r1, r2 :: bool
89 L0:
90 if x goto L1 else goto L2 :: bool
91 L1:
92 r0 = c(2)
93 if r0 goto L5 else goto L6 :: bool
94 L2:
95 if y goto L3 else goto L4 :: bool
96 L3:
97 r1 = c(4)
98 if r1 goto L5 else goto L6 :: bool
99 L4:
100 r2 = c(6)
101 if r2 goto L5 else goto L6 :: bool
102 L5:
103 return 2
104 L6:
105 return 4
106
107 [case testFlagEliminationAssignmentNotLastOp]
108 def f(x: bool) -> int:
109 y = 0
110 if x:
111 b = True
112 y = 1
113 else:
114 b = False
115 if b:
116 return 1
117 else:
118 return 2
119 [out]
120 def f(x):
121 x :: bool
122 y :: int
123 b :: bool
124 L0:
125 y = 0
126 if x goto L1 else goto L2 :: bool
127 L1:
128 b = 1
129 y = 2
130 goto L3
131 L2:
132 b = 0
133 L3:
134 if b goto L4 else goto L5 :: bool
135 L4:
136 return 2
137 L5:
138 return 4
139
140 [case testFlagEliminationAssignmentNoDirectGoto]
141 def f(x: bool) -> int:
142 if x:
143 b = True
144 else:
145 b = False
146 if x:
147 if b:
148 return 1
149 else:
150 return 2
151 return 4
152 [out]
153 def f(x):
154 x, b :: bool
155 L0:
156 if x goto L1 else goto L2 :: bool
157 L1:
158 b = 1
159 goto L3
160 L2:
161 b = 0
162 L3:
163 if x goto L4 else goto L7 :: bool
164 L4:
165 if b goto L5 else goto L6 :: bool
166 L5:
167 return 2
168 L6:
169 return 4
170 L7:
171 return 8
172
173 [case testFlagEliminationBranchNotNextOpAfterGoto]
174 def f(x: bool) -> int:
175 if x:
176 b = True
177 else:
178 b = False
179 y = 1 # Prevents the optimization
180 if b:
181 return 1
182 else:
183 return 2
184 [out]
185 def f(x):
186 x, b :: bool
187 y :: int
188 L0:
189 if x goto L1 else goto L2 :: bool
190 L1:
191 b = 1
192 goto L3
193 L2:
194 b = 0
195 L3:
196 y = 2
197 if b goto L4 else goto L5 :: bool
198 L4:
199 return 2
200 L5:
201 return 4
202
203 [case testFlagEliminationFlagReadTwice]
204 def f(x: bool) -> bool:
205 if x:
206 b = True
207 else:
208 b = False
209 if b:
210 return b # Prevents the optimization
211 else:
212 return False
213 [out]
214 def f(x):
215 x, b :: bool
216 L0:
217 if x goto L1 else goto L2 :: bool
218 L1:
219 b = 1
220 goto L3
221 L2:
222 b = 0
223 L3:
224 if b goto L4 else goto L5 :: bool
225 L4:
226 return b
227 L5:
228 return 0
229
230 [case testFlagEliminationArgumentNotEligible]
231 def f(x: bool, b: bool) -> bool:
232 if x:
233 b = True
234 else:
235 b = False
236 if b:
237 return True
238 else:
239 return False
240 [out]
241 def f(x, b):
242 x, b :: bool
243 L0:
244 if x goto L1 else goto L2 :: bool
245 L1:
246 b = 1
247 goto L3
248 L2:
249 b = 0
250 L3:
251 if b goto L4 else goto L5 :: bool
252 L4:
253 return 1
254 L5:
255 return 0
256
257 [case testFlagEliminationFlagNotAlwaysDefined]
258 def f(x: bool, y: bool) -> bool:
259 if x:
260 b = True
261 elif y:
262 b = False
263 else:
264 bb = False # b not assigned here -> can't optimize
265 if b:
266 return True
267 else:
268 return False
269 [out]
270 def f(x, y):
271 x, y, r0, b, bb, r1 :: bool
272 L0:
273 r0 = <error> :: bool
274 b = r0
275 if x goto L1 else goto L2 :: bool
276 L1:
277 b = 1
278 goto L5
279 L2:
280 if y goto L3 else goto L4 :: bool
281 L3:
282 b = 0
283 goto L5
284 L4:
285 bb = 0
286 L5:
287 if is_error(b) goto L6 else goto L7
288 L6:
289 r1 = raise UnboundLocalError('local variable "b" referenced before assignment')
290 unreachable
291 L7:
292 if b goto L8 else goto L9 :: bool
293 L8:
294 return 1
295 L9:
296 return 0