| 1 |
import py |
| 2 |
from pypy.module.pypyjit.test_pypy_c.test_00_model import BaseTestPyPyC |
| 3 |
|
| 4 |
class TestIntbound(BaseTestPyPyC): |
| 5 |
|
| 6 |
def test_intbound_simple(self): |
| 7 |
""" |
| 8 |
This test only checks that we get the expected result, not that any |
| 9 |
optimization has been applied. |
| 10 |
""" |
| 11 |
ops = ('<', '>', '<=', '>=', '==', '!=') |
| 12 |
nbr = (3, 7) |
| 13 |
for o1 in ops: |
| 14 |
for o2 in ops: |
| 15 |
for n1 in nbr: |
| 16 |
for n2 in nbr: |
| 17 |
src = ''' |
| 18 |
def f(i): |
| 19 |
a, b = 3, 3 |
| 20 |
if i %s %d: |
| 21 |
a = 0 |
| 22 |
else: |
| 23 |
a = 1 |
| 24 |
if i %s %d: |
| 25 |
b = 0 |
| 26 |
else: |
| 27 |
b = 1 |
| 28 |
return a + b * 2 |
| 29 |
|
| 30 |
def main(): |
| 31 |
res = [0] * 4 |
| 32 |
idx = [] |
| 33 |
for i in range(15): |
| 34 |
idx.extend([i] * 15) |
| 35 |
for i in idx: |
| 36 |
res[f(i)] += 1 |
| 37 |
return res |
| 38 |
|
| 39 |
''' % (o1, n1, o2, n2) |
| 40 |
yield self.run_and_check, src |
| 41 |
|
| 42 |
def test_intbound_addsub_mix(self): |
| 43 |
""" |
| 44 |
This test only checks that we get the expected result, not that any |
| 45 |
optimization has been applied. |
| 46 |
""" |
| 47 |
tests = ('i > 4', 'i > 2', 'i + 1 > 2', '1 + i > 4', |
| 48 |
'i - 1 > 1', '1 - i > 1', '1 - i < -3', |
| 49 |
'i == 1', 'i == 5', 'i != 1', '-2 * i < -4') |
| 50 |
for t1 in tests: |
| 51 |
for t2 in tests: |
| 52 |
src = ''' |
| 53 |
def f(i): |
| 54 |
a, b = 3, 3 |
| 55 |
if %s: |
| 56 |
a = 0 |
| 57 |
else: |
| 58 |
a = 1 |
| 59 |
if %s: |
| 60 |
b = 0 |
| 61 |
else: |
| 62 |
b = 1 |
| 63 |
return a + b * 2 |
| 64 |
|
| 65 |
def main(): |
| 66 |
res = [0] * 4 |
| 67 |
idx = [] |
| 68 |
for i in range(15): |
| 69 |
idx.extend([i] * 15) |
| 70 |
for i in idx: |
| 71 |
res[f(i)] += 1 |
| 72 |
return res |
| 73 |
|
| 74 |
''' % (t1, t2) |
| 75 |
yield self.run_and_check, src |
| 76 |
|
| 77 |
def test_intbound_gt(self): |
| 78 |
def main(n): |
| 79 |
i, a, b = 0, 0, 0 |
| 80 |
while i < n: |
| 81 |
if i > -1: |
| 82 |
a += 1 |
| 83 |
if i > -2: |
| 84 |
b += 1 |
| 85 |
i += 1 |
| 86 |
return (a, b) |
| 87 |
|
| 88 |
log = self.run(main, [300]) |
| 89 |
assert log.result == (300, 300) |
| 90 |
loop, = log.loops_by_filename(self.filepath) |
| 91 |
assert loop.match(""" |
| 92 |
i10 = int_lt(i8, i9) |
| 93 |
guard_true(i10, descr=...) |
| 94 |
i12 = int_add_ovf(i7, 1) |
| 95 |
guard_no_overflow(descr=...) |
| 96 |
i14 = int_add_ovf(i6, 1) |
| 97 |
guard_no_overflow(descr=...) |
| 98 |
i17 = int_add(i8, 1) |
| 99 |
--TICK-- |
| 100 |
jump(..., descr=...) |
| 101 |
""") |
| 102 |
|
| 103 |
def test_intbound_sub_lt(self): |
| 104 |
def main(): |
| 105 |
i, a = 0, 0 |
| 106 |
while i < 300: |
| 107 |
if i - 10 < 295: |
| 108 |
a += 1 |
| 109 |
i += 1 |
| 110 |
return a |
| 111 |
|
| 112 |
log = self.run(main, []) |
| 113 |
assert log.result == 300 |
| 114 |
loop, = log.loops_by_filename(self.filepath) |
| 115 |
assert loop.match(""" |
| 116 |
i7 = int_lt(i5, 300) |
| 117 |
guard_true(i7, descr=...) |
| 118 |
i9 = int_sub_ovf(i5, 10) |
| 119 |
guard_no_overflow(descr=...) |
| 120 |
i11 = int_add_ovf(i4, 1) |
| 121 |
guard_no_overflow(descr=...) |
| 122 |
i13 = int_add(i5, 1) |
| 123 |
--TICK-- |
| 124 |
jump(..., descr=...) |
| 125 |
""") |
| 126 |
|
| 127 |
def test_intbound_addsub_ge(self): |
| 128 |
def main(n): |
| 129 |
i, a, b = 0, 0, 0 |
| 130 |
while i < n: |
| 131 |
if i + 5 >= 5: |
| 132 |
a += 1 |
| 133 |
if i - 1 >= -1: |
| 134 |
b += 1 |
| 135 |
i += 1 |
| 136 |
return (a, b) |
| 137 |
|
| 138 |
log = self.run(main, [300]) |
| 139 |
assert log.result == (300, 300) |
| 140 |
loop, = log.loops_by_filename(self.filepath) |
| 141 |
assert loop.match(""" |
| 142 |
i10 = int_lt(i8, i9) |
| 143 |
guard_true(i10, descr=...) |
| 144 |
i12 = int_add_ovf(i8, 5) |
| 145 |
guard_no_overflow(descr=...) |
| 146 |
i14 = int_add_ovf(i7, 1) |
| 147 |
guard_no_overflow(descr=...) |
| 148 |
i16s = int_sub(i8, 1) |
| 149 |
i16 = int_add_ovf(i6, 1) |
| 150 |
guard_no_overflow(descr=...) |
| 151 |
i19 = int_add(i8, 1) |
| 152 |
--TICK-- |
| 153 |
jump(..., descr=...) |
| 154 |
""") |
| 155 |
|
| 156 |
def test_intbound_addmul_ge(self): |
| 157 |
def main(n): |
| 158 |
i, a, b = 0, 0, 0 |
| 159 |
while i < 300: |
| 160 |
if i + 5 >= 5: |
| 161 |
a += 1 |
| 162 |
if 2 * i >= 0: |
| 163 |
b += 1 |
| 164 |
i += 1 |
| 165 |
return (a, b) |
| 166 |
|
| 167 |
log = self.run(main, [300]) |
| 168 |
assert log.result == (300, 300) |
| 169 |
loop, = log.loops_by_filename(self.filepath) |
| 170 |
assert loop.match(""" |
| 171 |
i10 = int_lt(i8, 300) |
| 172 |
guard_true(i10, descr=...) |
| 173 |
i12 = int_add(i8, 5) |
| 174 |
i14 = int_add_ovf(i7, 1) |
| 175 |
guard_no_overflow(descr=...) |
| 176 |
i16 = int_lshift(i8, 1) |
| 177 |
i18 = int_add_ovf(i6, 1) |
| 178 |
guard_no_overflow(descr=...) |
| 179 |
i21 = int_add(i8, 1) |
| 180 |
--TICK-- |
| 181 |
jump(..., descr=...) |
| 182 |
""") |
| 183 |
|
| 184 |
def test_intbound_eq(self): |
| 185 |
def main(a, n): |
| 186 |
i, s = 0, 0 |
| 187 |
while i < 300: |
| 188 |
if a == 7: |
| 189 |
s += a + 1 |
| 190 |
elif i == 10: |
| 191 |
s += i |
| 192 |
else: |
| 193 |
s += 1 |
| 194 |
i += 1 |
| 195 |
return s |
| 196 |
|
| 197 |
log = self.run(main, [7, 300]) |
| 198 |
assert log.result == main(7, 300) |
| 199 |
log = self.run(main, [10, 300]) |
| 200 |
assert log.result == main(10, 300) |
| 201 |
log = self.run(main, [42, 300]) |
| 202 |
assert log.result == main(42, 300) |
| 203 |
loop, = log.loops_by_filename(self.filepath) |
| 204 |
assert loop.match(""" |
| 205 |
i10 = int_lt(i8, 300) |
| 206 |
guard_true(i10, descr=...) |
| 207 |
i12 = int_eq(i8, 10) |
| 208 |
guard_false(i12, descr=...) |
| 209 |
i14 = int_add_ovf(i7, 1) |
| 210 |
guard_no_overflow(descr=...) |
| 211 |
i16 = int_add(i8, 1) |
| 212 |
--TICK-- |
| 213 |
jump(..., descr=...) |
| 214 |
""") |
| 215 |
|
| 216 |
def test_intbound_mul(self): |
| 217 |
def main(a): |
| 218 |
i, s = 0, 0 |
| 219 |
while i < 300: |
| 220 |
assert i >= 0 |
| 221 |
if 2 * i < 30000: |
| 222 |
s += 1 |
| 223 |
else: |
| 224 |
s += a |
| 225 |
i += 1 |
| 226 |
return s |
| 227 |
|
| 228 |
log = self.run(main, [7]) |
| 229 |
assert log.result == 300 |
| 230 |
loop, = log.loops_by_filename(self.filepath) |
| 231 |
assert loop.match(""" |
| 232 |
i8 = int_lt(i6, 300) |
| 233 |
guard_true(i8, descr=...) |
| 234 |
guard_not_invalidated? |
| 235 |
i10 = int_lshift(i6, 1) |
| 236 |
i12 = int_add_ovf(i5, 1) |
| 237 |
guard_no_overflow(descr=...) |
| 238 |
i14 = int_add(i6, 1) |
| 239 |
--TICK-- |
| 240 |
jump(..., descr=...) |
| 241 |
""") |
| 242 |
|
| 243 |
def test_assert(self): |
| 244 |
def main(a): |
| 245 |
i, s = 0, 0 |
| 246 |
while i < 300: |
| 247 |
assert a == 7 |
| 248 |
s += a + 1 |
| 249 |
i += 1 |
| 250 |
return s |
| 251 |
log = self.run(main, [7]) |
| 252 |
assert log.result == 300*8 |
| 253 |
loop, = log.loops_by_filename(self.filepath) |
| 254 |
assert loop.match(""" |
| 255 |
i8 = int_lt(i6, 300) |
| 256 |
guard_true(i8, descr=...) |
| 257 |
guard_not_invalidated? |
| 258 |
i10 = int_add_ovf(i5, 8) |
| 259 |
guard_no_overflow(descr=...) |
| 260 |
i12 = int_add(i6, 1) |
| 261 |
--TICK-- |
| 262 |
jump(..., descr=...) |
| 263 |
""") |
| 264 |
|
| 265 |
def test_xor(self): |
| 266 |
def main(b): |
| 267 |
a = sa = 0 |
| 268 |
while a < 300: |
| 269 |
if a > 0: |
| 270 |
pass |
| 271 |
if b > 10: |
| 272 |
pass |
| 273 |
if a^b >= 0: |
| 274 |
sa += 1 |
| 275 |
sa += a^a |
| 276 |
a += 1 |
| 277 |
return sa |
| 278 |
|
| 279 |
log = self.run(main, [11]) |
| 280 |
assert log.result == 300 |
| 281 |
loop, = log.loops_by_filename(self.filepath) |
| 282 |
|
| 283 |
|
| 284 |
assert loop.match_by_id('guard', """ |
| 285 |
i10 = int_xor(i5, i7) |
| 286 |
""") |
| 287 |
|
| 288 |
|
| 289 |
assert loop.match_by_id('a_xor_a', "") |
| 290 |
|
| 291 |
log = self.run(main, [9]) |
| 292 |
assert log.result == 300 |
| 293 |
loop, = log.loops_by_filename(self.filepath, is_entry_bridge=True) |
| 294 |
|
| 295 |
assert loop.match_by_id('guard', """ |
| 296 |
i10 = int_xor(i5, i7) |
| 297 |
i12 = int_ge(i10, 0) |
| 298 |
guard_true(i12, descr=...) |
| 299 |
""") |
| 300 |
|
| 301 |
def test_abs_branch_free(self): |
| 302 |
import sys |
| 303 |
def main(start, stop): |
| 304 |
res = 0 |
| 305 |
for i in range(start, stop): |
| 306 |
res += abs(i) |
| 307 |
return res |
| 308 |
|
| 309 |
log = self.run(main, [-10000, 10000]) |
| 310 |
loop, = log.loops_by_filename(self.filepath) |
| 311 |
assert loop.match_by_id('abs', """ |
| 312 |
setfield_gc(p18, i60, descr=...) |
| 313 |
guard_not_invalidated(descr=...) |
| 314 |
i63 = int_eq(i58, ...) # check whether it's MININT |
| 315 |
guard_false(i63, descr=...) |
| 316 |
i65 = int_rshift(i58, %s) |
| 317 |
i66 = int_xor(i58, i65) |
| 318 |
i67 = int_sub(i66, i65) |
| 319 |
i68 = int_add_ovf(i51, i67) |
| 320 |
guard_no_overflow(descr=...) |
| 321 |
--TICK-- |
| 322 |
""" % sys.maxsize.bit_length()) |
| 323 |
|
| 324 |
def test_mul_int_long_uses_int_shortcut(self): |
| 325 |
def main(start, stop): |
| 326 |
longvalue = 2**100 |
| 327 |
res = 0 |
| 328 |
for i in range(start, stop): |
| 329 |
longvalue = i * longvalue |
| 330 |
return res |
| 331 |
|
| 332 |
log = self.run(main, [-10000, 10000]) |
| 333 |
loop, = log.loops_by_filename(self.filepath) |
| 334 |
for op in loop.allops(): |
| 335 |
if op.name != "call_r": |
| 336 |
continue |
| 337 |
assert 'fromint' not in op.args[0] |
| 338 |
|
| 339 |
assert loop.match_by_id('mul', """ |
| 340 |
p45 = call_r(ConstClass(rbigint.int_mul), p34, i40, descr=...) |
| 341 |
setfield_gc(p19, i43, descr=...) |
| 342 |
guard_no_exception(descr=...) |
| 343 |
--TICK-- |
| 344 |
""") |