Debian Codegraph @

pypy/module/pypyjit/test_pypy_c/test_boolrewrite.py

raw symbols SCIP
1 import py
2 from pypy.module.pypyjit.test_pypy_c.test_00_model import BaseTestPyPyC
3
4 class TestBoolRewrite(BaseTestPyPyC):
5
6 def test_boolrewrite_inverse(self):
7 """
8 Test for this case::
9 guard(i < x)
10 ...
11 guard(i >= y)
12
13 where x and y can be either constants or variables. There are cases in
14 which the second guard is proven to be always true.
15 """
16
17 for a, b, res, opt_expected in (('2000', '2000', 20001000, True),
18 ( '500', '500', 15001500, True),
19 ( '300', '600', 16001700, False),
20 ( 'a', 'b', 16001700, False),
21 ( 'a', 'a', 13001700, True)):
22 src = """
23 def main():
24 sa = 0
25 a = 300
26 b = 600
27 for i in range(1000):
28 if i < %s: # ID: lt
29 sa += 1
30 else:
31 sa += 2
32 #
33 if i >= %s: # ID: ge
34 sa += 10000
35 else:
36 sa += 20000
37 return sa
38 """ % (a, b)
39 #
40 log = self.run(src, [], threshold=400)
41 assert log.result == res
42 for loop in log.loops_by_filename(self.filepath):
43 le_ops = log.opnames(loop.ops_by_id('lt'))
44 ge_ops = log.opnames(loop.ops_by_id('ge'))
45 assert le_ops.count('int_lt') == 1
46 #
47 if opt_expected:
48 assert ge_ops.count('int_ge') == 0
49 else:
50 # if this assert fails it means that the optimization was
51 # applied even if we don't expect to. Check whether the
52 # optimization is valid, and either fix the code or fix the
53 # test :-)
54 assert ge_ops.count('int_ge') == 1
55
56 def test_boolrewrite_reflex(self):
57 """
58 Test for this case::
59 guard(i < x)
60 ...
61 guard(y > i)
62
63 where x and y can be either constants or variables. There are cases in
64 which the second guard is proven to be always true.
65 """
66 for a, b, res, opt_expected in (('2000', '2000', 10001000, True),
67 ( '500', '500', 15001500, True),
68 ( '300', '600', 14001700, False),
69 ( 'a', 'b', 14001700, False),
70 ( 'a', 'a', 17001700, True)):
71
72 src = """
73 def main():
74 sa = 0
75 a = 300
76 b = 600
77 for i in range(1000):
78 if i < %s: # ID: lt
79 sa += 1
80 else:
81 sa += 2
82 if %s > i: # ID: gt
83 sa += 10000
84 else:
85 sa += 20000
86 return sa
87 """ % (a, b)
88 log = self.run(src, [], threshold=400)
89 assert log.result == res
90 for loop in log.loops_by_filename(self.filepath):
91 le_ops = log.opnames(loop.ops_by_id('lt'))
92 gt_ops = log.opnames(loop.ops_by_id('gt'))
93 assert le_ops.count('int_lt') == 1
94 #
95 if opt_expected:
96 assert gt_ops.count('int_gt') == 0
97 else:
98 # if this assert fails it means that the optimization was
99 # applied even if we don't expect to. Check whether the
100 # optimization is valid, and either fix the code or fix the
101 # test :-)
102 assert gt_ops.count('int_gt') == 1
103
104
105 def test_boolrewrite_allcases_inverse(self):
106 """
107 Test for this case::
108 guard(i < x)
109 ...
110 guard(i > y)
111
112 with all possible combination of binary comparison operators. This
113 test only checks that we get the expected result, not that any
114 optimization has been applied.
115 """
116 ops = ('<', '>', '<=', '>=', '==', '!=')
117 for op1 in ops:
118 for op2 in ops:
119 for a,b in ((500, 500), (300, 600)):
120 src = """
121 def main():
122 sa = 0
123 for i in range(300):
124 if i %s %d:
125 sa += 1
126 else:
127 sa += 2
128 if i %s %d:
129 sa += 10000
130 else:
131 sa += 20000
132 return sa
133 """ % (op1, a, op2, b)
134 yield self.run_and_check, src
135
136 src = """
137 def main():
138 sa = 0
139 i = 0.0
140 while i < 250.0:
141 if i %s %f:
142 sa += 1
143 else:
144 sa += 2
145 if i %s %f:
146 sa += 10000
147 else:
148 sa += 20000
149 i += 0.25
150 return sa
151 """ % (op1, float(a)/4.0, op2, float(b)/4.0)
152 yield self.run_and_check, src
153
154
155 def test_boolrewrite_allcases_reflex(self):
156 """
157 Test for this case::
158 guard(i < x)
159 ...
160 guard(x > i)
161
162 with all possible combination of binary comparison operators. This
163 test only checks that we get the expected result, not that any
164 optimization has been applied.
165 """
166 ops = ('<', '>', '<=', '>=', '==', '!=')
167 for op1 in ops:
168 for op2 in ops:
169 for a,b in ((500, 500), (300, 600)):
170 src = """
171 def main():
172 sa = 0
173 for i in range(300):
174 if i %s %d:
175 sa += 1
176 else:
177 sa += 2
178 if %d %s i:
179 sa += 10000
180 else:
181 sa += 20000
182 return sa
183 """ % (op1, a, b, op2)
184 yield self.run_and_check, src
185
186 src = """
187 def main():
188 sa = 0
189 i = 0.0
190 while i < 250.0:
191 if i %s %f:
192 sa += 1
193 else:
194 sa += 2
195 if %f %s i:
196 sa += 10000
197 else:
198 sa += 20000
199 i += 0.25
200 return sa
201 """ % (op1, float(a)/4.0, float(b)/4.0, op2)
202 yield self.run_and_check, src
203
204 def test_boolrewrite_ptr(self):
205 """
206 This test only checks that we get the expected result, not that any
207 optimization has been applied.
208 """
209 compares = ('a == b', 'b == a', 'a != b', 'b != a', 'a == c', 'c != b')
210 for e1 in compares:
211 for e2 in compares:
212 src = """
213 class tst(object):
214 pass
215 def main():
216 a = tst()
217 b = tst()
218 c = tst()
219 sa = 0
220 for i in range(1000):
221 if %s:
222 sa += 1
223 else:
224 sa += 2
225 if %s:
226 sa += 10000
227 else:
228 sa += 20000
229 if i > 750:
230 a = b
231 return sa
232 """ % (e1, e2)
233 yield self.run_and_check, src