| 1 |
import sys |
| 2 |
from rpython.jit.metainterp.history import ConstInt |
| 3 |
from rpython.jit.metainterp.optimize import InvalidLoop |
| 4 |
from rpython.jit.metainterp.optimizeopt.intutils import IntBound |
| 5 |
from rpython.jit.metainterp.optimizeopt.optimizer import (Optimization, CONST_1, |
| 6 |
CONST_0) |
| 7 |
from rpython.jit.metainterp.optimizeopt.util import ( |
| 8 |
make_dispatcher_method, have_dispatcher_method, get_box_replacement) |
| 9 |
from rpython.jit.metainterp.optimizeopt.info import getptrinfo |
| 10 |
from rpython.jit.metainterp.resoperation import rop |
| 11 |
from rpython.jit.metainterp.optimizeopt import vstring |
| 12 |
from rpython.jit.metainterp.optimizeopt import autogenintrules |
| 13 |
from rpython.jit.codewriter.effectinfo import EffectInfo |
| 14 |
from rpython.rlib.rarithmetic import intmask |
| 15 |
from rpython.rlib.debug import debug_print |
| 16 |
from rpython.rlib import objectmodel |
| 17 |
|
| 18 |
def get_integer_min(is_unsigned, byte_size): |
| 19 |
if is_unsigned: |
| 20 |
return 0 |
| 21 |
else: |
| 22 |
return -(1 << ((byte_size << 3) - 1)) |
| 23 |
|
| 24 |
|
| 25 |
def get_integer_max(is_unsigned, byte_size): |
| 26 |
if is_unsigned: |
| 27 |
return (1 << (byte_size << 3)) - 1 |
| 28 |
else: |
| 29 |
return (1 << ((byte_size << 3) - 1)) - 1 |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
class OptIntBounds(Optimization): |
| 34 |
"""Keeps track of the bounds placed on integers by guards and remove |
| 35 |
redundant guards""" |
| 36 |
|
| 37 |
def propagate_forward(self, op): |
| 38 |
return dispatch_opt(self, op) |
| 39 |
|
| 40 |
def propagate_bounds_backward(self, box): |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
b = self.getintbound(box) |
| 45 |
if b.is_constant(): |
| 46 |
self.make_constant_int(box, b.get_constant_int()) |
| 47 |
|
| 48 |
box1 = self.optimizer.as_operation(box) |
| 49 |
if box1 is not None: |
| 50 |
dispatch_bounds_ops(self, box1) |
| 51 |
|
| 52 |
def _postprocess_guard_true_false_value(self, op): |
| 53 |
if op.getarg(0).type == 'i': |
| 54 |
self.propagate_bounds_backward(op.getarg(0)) |
| 55 |
|
| 56 |
postprocess_GUARD_TRUE = _postprocess_guard_true_false_value |
| 57 |
postprocess_GUARD_FALSE = _postprocess_guard_true_false_value |
| 58 |
postprocess_GUARD_VALUE = _postprocess_guard_true_false_value |
| 59 |
|
| 60 |
def postprocess_INT_OR(self, op): |
| 61 |
arg0 = get_box_replacement(op.getarg(0)) |
| 62 |
arg1 = get_box_replacement(op.getarg(1)) |
| 63 |
b0 = self.getintbound(arg0) |
| 64 |
b1 = self.getintbound(arg1) |
| 65 |
if b0.and_bound(b1).known_eq_const(0): |
| 66 |
self.pure_from_args2(rop.INT_ADD, |
| 67 |
arg0, arg1, op) |
| 68 |
self.pure_from_args2(rop.INT_XOR, |
| 69 |
arg0, arg1, op) |
| 70 |
b = b0.or_bound(b1) |
| 71 |
self.getintbound(op).intersect(b) |
| 72 |
|
| 73 |
def postprocess_INT_XOR(self, op): |
| 74 |
arg0 = get_box_replacement(op.getarg(0)) |
| 75 |
arg1 = get_box_replacement(op.getarg(1)) |
| 76 |
b0 = self.getintbound(arg0) |
| 77 |
b1 = self.getintbound(arg1) |
| 78 |
if b0.and_bound(b1).known_eq_const(0): |
| 79 |
self.pure_from_args2(rop.INT_ADD, |
| 80 |
arg0, arg1, op) |
| 81 |
self.pure_from_args2(rop.INT_OR, |
| 82 |
arg0, arg1, op) |
| 83 |
b = b0.xor_bound(b1) |
| 84 |
self.getintbound(op).intersect(b) |
| 85 |
|
| 86 |
def postprocess_INT_AND(self, op): |
| 87 |
b1 = self.getintbound(op.getarg(0)) |
| 88 |
b2 = self.getintbound(op.getarg(1)) |
| 89 |
b = b1.and_bound(b2) |
| 90 |
self.getintbound(op).intersect(b) |
| 91 |
|
| 92 |
def postprocess_INT_SUB(self, op): |
| 93 |
import sys |
| 94 |
arg0 = op.getarg(0) |
| 95 |
arg1 = op.getarg(1) |
| 96 |
b0 = self.getintbound(arg0) |
| 97 |
b1 = self.getintbound(arg1) |
| 98 |
b = b0.sub_bound(b1) |
| 99 |
self.getintbound(op).intersect(b) |
| 100 |
self.optimizer.pure_from_args2(rop.INT_ADD, op, arg1, arg0) |
| 101 |
self.optimizer.pure_from_args2(rop.INT_SUB, arg0, op, arg1) |
| 102 |
if isinstance(arg1, ConstInt): |
| 103 |
|
| 104 |
i1 = arg1.getint() |
| 105 |
if i1 == -sys.maxint - 1: |
| 106 |
return |
| 107 |
inv_arg1 = ConstInt(-i1) |
| 108 |
self.optimizer.pure_from_args2(rop.INT_ADD, arg0, inv_arg1, op) |
| 109 |
self.optimizer.pure_from_args2(rop.INT_ADD, inv_arg1, arg0, op) |
| 110 |
self.optimizer.pure_from_args2(rop.INT_SUB, op, inv_arg1, arg0) |
| 111 |
self.optimizer.pure_from_args2(rop.INT_SUB, op, arg0, inv_arg1) |
| 112 |
|
| 113 |
|
| 114 |
def postprocess_INT_ADD(self, op): |
| 115 |
import sys |
| 116 |
arg0 = get_box_replacement(op.getarg(0)) |
| 117 |
arg1 = get_box_replacement(op.getarg(1)) |
| 118 |
b0 = self.getintbound(arg0) |
| 119 |
if arg0 is arg1: |
| 120 |
b = b0.lshift_bound(IntBound.from_constant(1)) |
| 121 |
else: |
| 122 |
b1 = self.getintbound(arg1) |
| 123 |
b = b0.add_bound(b1) |
| 124 |
self.getintbound(op).intersect(b) |
| 125 |
|
| 126 |
self.optimizer.pure_from_args2(rop.INT_SUB, op, arg1, arg0) |
| 127 |
self.optimizer.pure_from_args2(rop.INT_SUB, op, arg0, arg1) |
| 128 |
if isinstance(arg0, ConstInt): |
| 129 |
|
| 130 |
i0 = arg0.getint() |
| 131 |
if i0 == -sys.maxint - 1: |
| 132 |
return |
| 133 |
inv_arg0 = ConstInt(-i0) |
| 134 |
elif isinstance(arg1, ConstInt): |
| 135 |
|
| 136 |
i0 = arg1.getint() |
| 137 |
if i0 == -sys.maxint - 1: |
| 138 |
return |
| 139 |
inv_arg0 = ConstInt(-i0) |
| 140 |
arg1 = arg0 |
| 141 |
else: |
| 142 |
return |
| 143 |
self.optimizer.pure_from_args2(rop.INT_SUB, arg1, inv_arg0, op) |
| 144 |
self.optimizer.pure_from_args2(rop.INT_SUB, arg1, op, inv_arg0) |
| 145 |
self.optimizer.pure_from_args2(rop.INT_ADD, op, inv_arg0, arg1) |
| 146 |
self.optimizer.pure_from_args2(rop.INT_ADD, inv_arg0, op, arg1) |
| 147 |
|
| 148 |
def postprocess_INT_MUL(self, op): |
| 149 |
b1 = self.getintbound(op.getarg(0)) |
| 150 |
b2 = self.getintbound(op.getarg(1)) |
| 151 |
r = self.getintbound(op) |
| 152 |
b = b1.mul_bound(b2) |
| 153 |
r.intersect(b) |
| 154 |
|
| 155 |
def postprocess_CALL_PURE_I(self, op): |
| 156 |
|
| 157 |
|
| 158 |
effectinfo = op.getdescr().get_extra_info() |
| 159 |
oopspecindex = effectinfo.oopspecindex |
| 160 |
if oopspecindex == EffectInfo.OS_INT_PY_DIV: |
| 161 |
self.post_call_INT_PY_DIV(op) |
| 162 |
elif oopspecindex == EffectInfo.OS_INT_PY_MOD: |
| 163 |
self.post_call_INT_PY_MOD(op) |
| 164 |
|
| 165 |
def post_call_INT_PY_DIV(self, op): |
| 166 |
b1 = self.getintbound(op.getarg(1)) |
| 167 |
b2 = self.getintbound(op.getarg(2)) |
| 168 |
r = self.getintbound(op) |
| 169 |
r.intersect(b1.py_div_bound(b2)) |
| 170 |
|
| 171 |
def post_call_INT_PY_MOD(self, op): |
| 172 |
b1 = self.getintbound(op.getarg(1)) |
| 173 |
b2 = self.getintbound(op.getarg(2)) |
| 174 |
r = self.getintbound(op) |
| 175 |
r.intersect(b1.mod_bound(b2)) |
| 176 |
|
| 177 |
def postprocess_INT_LSHIFT(self, op): |
| 178 |
arg0 = get_box_replacement(op.getarg(0)) |
| 179 |
b1 = self.getintbound(arg0) |
| 180 |
arg1 = get_box_replacement(op.getarg(1)) |
| 181 |
b2 = self.getintbound(arg1) |
| 182 |
r = self.getintbound(op) |
| 183 |
b = b1.lshift_bound(b2) |
| 184 |
r.intersect(b) |
| 185 |
if b1.lshift_bound_cannot_overflow(b2): |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
self.pure_from_args2(rop.INT_RSHIFT, |
| 193 |
op, arg1, arg0) |
| 194 |
|
| 195 |
def postprocess_INT_RSHIFT(self, op): |
| 196 |
b1 = self.getintbound(op.getarg(0)) |
| 197 |
b2 = self.getintbound(op.getarg(1)) |
| 198 |
b = b1.rshift_bound(b2) |
| 199 |
r = self.getintbound(op) |
| 200 |
r.intersect(b) |
| 201 |
|
| 202 |
def postprocess_UINT_RSHIFT(self, op): |
| 203 |
b1 = self.getintbound(op.getarg(0)) |
| 204 |
b2 = self.getintbound(op.getarg(1)) |
| 205 |
b = b1.urshift_bound(b2) |
| 206 |
r = self.getintbound(op) |
| 207 |
r.intersect(b) |
| 208 |
|
| 209 |
def optimize_GUARD_NO_OVERFLOW(self, op): |
| 210 |
lastop = self.last_emitted_operation |
| 211 |
if lastop is not None: |
| 212 |
opnum = lastop.getopnum() |
| 213 |
args = lastop.getarglist() |
| 214 |
result = lastop |
| 215 |
|
| 216 |
|
| 217 |
if (opnum != rop.INT_ADD_OVF and |
| 218 |
opnum != rop.INT_SUB_OVF and |
| 219 |
opnum != rop.INT_MUL_OVF): |
| 220 |
return |
| 221 |
|
| 222 |
|
| 223 |
elif opnum == rop.INT_ADD_OVF: |
| 224 |
self.pure_from_args2(rop.INT_SUB, result, args[1], args[0]) |
| 225 |
self.pure_from_args2(rop.INT_SUB, result, args[0], args[1]) |
| 226 |
elif opnum == rop.INT_SUB_OVF: |
| 227 |
self.pure_from_args2(rop.INT_ADD, result, args[1], args[0]) |
| 228 |
self.pure_from_args2(rop.INT_SUB, args[0], result, args[1]) |
| 229 |
return self.emit(op) |
| 230 |
|
| 231 |
def optimize_GUARD_OVERFLOW(self, op): |
| 232 |
|
| 233 |
|
| 234 |
lastop = self.last_emitted_operation |
| 235 |
if lastop is None: |
| 236 |
return |
| 237 |
opnum = lastop.getopnum() |
| 238 |
if opnum not in (rop.INT_ADD_OVF, rop.INT_SUB_OVF, rop.INT_MUL_OVF): |
| 239 |
raise InvalidLoop('An INT_xxx_OVF was proven not to overflow but' + |
| 240 |
'guarded with GUARD_OVERFLOW') |
| 241 |
|
| 242 |
return self.emit(op) |
| 243 |
|
| 244 |
def optimize_INT_ADD_OVF(self, op): |
| 245 |
b1 = self.getintbound(op.getarg(0)) |
| 246 |
b2 = self.getintbound(op.getarg(1)) |
| 247 |
if b1.add_bound_cannot_overflow(b2): |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
op = self.replace_op_with(op, rop.INT_ADD) |
| 255 |
return self.optimizer.send_extra_operation(op) |
| 256 |
return self.emit(op) |
| 257 |
|
| 258 |
def postprocess_INT_ADD_OVF(self, op): |
| 259 |
arg0 = get_box_replacement(op.getarg(0)) |
| 260 |
arg1 = get_box_replacement(op.getarg(1)) |
| 261 |
b0 = self.getintbound(arg0) |
| 262 |
b1 = self.getintbound(arg1) |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
if arg0 is arg1: |
| 269 |
resbound = b0.mul2_bound_no_overflow() |
| 270 |
else: |
| 271 |
resbound = b0.add_bound_no_overflow(b1) |
| 272 |
r = self.getintbound(op) |
| 273 |
r.intersect(resbound) |
| 274 |
|
| 275 |
def optimize_INT_SUB_OVF(self, op): |
| 276 |
arg0 = get_box_replacement(op.getarg(0)) |
| 277 |
arg1 = get_box_replacement(op.getarg(1)) |
| 278 |
b0 = self.getintbound(arg0) |
| 279 |
b1 = self.getintbound(arg1) |
| 280 |
if arg0.same_box(arg1): |
| 281 |
self.make_constant_int(op, 0) |
| 282 |
return None |
| 283 |
if b0.sub_bound_cannot_overflow(b1): |
| 284 |
|
| 285 |
op = self.replace_op_with(op, rop.INT_SUB) |
| 286 |
return self.optimizer.send_extra_operation(op) |
| 287 |
return self.emit(op) |
| 288 |
|
| 289 |
def postprocess_INT_SUB_OVF(self, op): |
| 290 |
arg0 = get_box_replacement(op.getarg(0)) |
| 291 |
arg1 = get_box_replacement(op.getarg(1)) |
| 292 |
b0 = self.getintbound(arg0) |
| 293 |
b1 = self.getintbound(arg1) |
| 294 |
resbound = b0.sub_bound_no_overflow(b1) |
| 295 |
r = self.getintbound(op) |
| 296 |
r.intersect(resbound) |
| 297 |
|
| 298 |
def optimize_INT_MUL_OVF(self, op): |
| 299 |
b0 = self.getintbound(op.getarg(0)) |
| 300 |
b1 = self.getintbound(op.getarg(1)) |
| 301 |
if b0.mul_bound_cannot_overflow(b1): |
| 302 |
|
| 303 |
op = self.replace_op_with(op, rop.INT_MUL) |
| 304 |
return self.optimizer.send_extra_operation(op) |
| 305 |
return self.emit(op) |
| 306 |
|
| 307 |
def postprocess_INT_MUL_OVF(self, op): |
| 308 |
arg0 = get_box_replacement(op.getarg(0)) |
| 309 |
arg1 = get_box_replacement(op.getarg(1)) |
| 310 |
b0 = self.getintbound(arg0) |
| 311 |
if arg0 is arg1: |
| 312 |
resbound = b0.square_bound_no_overflow() |
| 313 |
else: |
| 314 |
b1 = self.getintbound(arg1) |
| 315 |
resbound = b0.mul_bound_no_overflow(b1) |
| 316 |
r = self.getintbound(op) |
| 317 |
r.intersect(resbound) |
| 318 |
|
| 319 |
def optimize_INT_LT(self, op): |
| 320 |
arg1 = get_box_replacement(op.getarg(0)) |
| 321 |
arg2 = get_box_replacement(op.getarg(1)) |
| 322 |
b1 = self.getintbound(arg1) |
| 323 |
b2 = self.getintbound(arg2) |
| 324 |
if b1.known_lt(b2): |
| 325 |
self.make_constant_int(op, 1) |
| 326 |
elif b1.known_ge(b2) or arg1 is arg2: |
| 327 |
self.make_constant_int(op, 0) |
| 328 |
else: |
| 329 |
return self.emit(op) |
| 330 |
|
| 331 |
def optimize_INT_GT(self, op): |
| 332 |
arg1 = get_box_replacement(op.getarg(0)) |
| 333 |
arg2 = get_box_replacement(op.getarg(1)) |
| 334 |
b1 = self.getintbound(arg1) |
| 335 |
b2 = self.getintbound(arg2) |
| 336 |
if b1.known_gt(b2): |
| 337 |
self.make_constant_int(op, 1) |
| 338 |
elif b1.known_le(b2) or arg1 is arg2: |
| 339 |
self.make_constant_int(op, 0) |
| 340 |
else: |
| 341 |
return self.emit(op) |
| 342 |
|
| 343 |
def optimize_INT_LE(self, op): |
| 344 |
arg1 = get_box_replacement(op.getarg(0)) |
| 345 |
arg2 = get_box_replacement(op.getarg(1)) |
| 346 |
b1 = self.getintbound(arg1) |
| 347 |
b2 = self.getintbound(arg2) |
| 348 |
if b1.known_le(b2) or arg1 is arg2: |
| 349 |
self.make_constant_int(op, 1) |
| 350 |
elif b1.known_gt(b2): |
| 351 |
self.make_constant_int(op, 0) |
| 352 |
else: |
| 353 |
return self.emit(op) |
| 354 |
|
| 355 |
def optimize_INT_GE(self, op): |
| 356 |
arg1 = get_box_replacement(op.getarg(0)) |
| 357 |
arg2 = get_box_replacement(op.getarg(1)) |
| 358 |
b1 = self.getintbound(arg1) |
| 359 |
b2 = self.getintbound(arg2) |
| 360 |
if b1.known_ge(b2) or arg1 is arg2: |
| 361 |
self.make_constant_int(op, 1) |
| 362 |
elif b1.known_lt(b2): |
| 363 |
self.make_constant_int(op, 0) |
| 364 |
else: |
| 365 |
return self.emit(op) |
| 366 |
|
| 367 |
def optimize_UINT_LT(self, op): |
| 368 |
arg1 = get_box_replacement(op.getarg(0)) |
| 369 |
arg2 = get_box_replacement(op.getarg(1)) |
| 370 |
b1 = self.getintbound(arg1) |
| 371 |
b2 = self.getintbound(arg2) |
| 372 |
if b1.known_unsigned_lt(b2): |
| 373 |
self.make_constant_int(op, 1) |
| 374 |
elif b1.known_unsigned_ge(b2) or arg1 is arg2: |
| 375 |
self.make_constant_int(op, 0) |
| 376 |
else: |
| 377 |
return self.emit(op) |
| 378 |
|
| 379 |
def propagate_bounds_UINT_LT(self, op): |
| 380 |
r = self.getintbound(op) |
| 381 |
if r.is_constant(): |
| 382 |
if r.get_constant_int() == 1: |
| 383 |
self.make_unsigned_lt(op.getarg(0), op.getarg(1)) |
| 384 |
else: |
| 385 |
assert r.get_constant_int() == 0 |
| 386 |
self.make_unsigned_ge(op.getarg(0), op.getarg(1)) |
| 387 |
|
| 388 |
def optimize_UINT_GT(self, op): |
| 389 |
arg1 = get_box_replacement(op.getarg(0)) |
| 390 |
arg2 = get_box_replacement(op.getarg(1)) |
| 391 |
b1 = self.getintbound(arg1) |
| 392 |
b2 = self.getintbound(arg2) |
| 393 |
if b1.known_unsigned_gt(b2): |
| 394 |
self.make_constant_int(op, 1) |
| 395 |
elif b1.known_unsigned_le(b2) or arg1 is arg2: |
| 396 |
self.make_constant_int(op, 0) |
| 397 |
else: |
| 398 |
return self.emit(op) |
| 399 |
|
| 400 |
def propagate_bounds_UINT_GT(self, op): |
| 401 |
r = self.getintbound(op) |
| 402 |
if r.is_constant(): |
| 403 |
if r.get_constant_int() == 1: |
| 404 |
self.make_unsigned_gt(op.getarg(0), op.getarg(1)) |
| 405 |
else: |
| 406 |
assert r.get_constant_int() == 0 |
| 407 |
self.make_unsigned_le(op.getarg(0), op.getarg(1)) |
| 408 |
|
| 409 |
def optimize_UINT_LE(self, op): |
| 410 |
arg1 = get_box_replacement(op.getarg(0)) |
| 411 |
arg2 = get_box_replacement(op.getarg(1)) |
| 412 |
b1 = self.getintbound(arg1) |
| 413 |
b2 = self.getintbound(arg2) |
| 414 |
if b1.known_unsigned_le(b2) or arg1 is arg2: |
| 415 |
self.make_constant_int(op, 1) |
| 416 |
elif b1.known_unsigned_gt(b2): |
| 417 |
self.make_constant_int(op, 0) |
| 418 |
else: |
| 419 |
return self.emit(op) |
| 420 |
|
| 421 |
def propagate_bounds_UINT_LE(self, op): |
| 422 |
r = self.getintbound(op) |
| 423 |
if r.is_constant(): |
| 424 |
if r.get_constant_int() == 1: |
| 425 |
self.make_unsigned_le(op.getarg(0), op.getarg(1)) |
| 426 |
else: |
| 427 |
assert r.get_constant_int() == 0 |
| 428 |
self.make_unsigned_gt(op.getarg(0), op.getarg(1)) |
| 429 |
|
| 430 |
def optimize_UINT_GE(self, op): |
| 431 |
arg1 = get_box_replacement(op.getarg(0)) |
| 432 |
arg2 = get_box_replacement(op.getarg(1)) |
| 433 |
b1 = self.getintbound(arg1) |
| 434 |
b2 = self.getintbound(arg2) |
| 435 |
if b1.known_unsigned_ge(b2) or arg1 is arg2: |
| 436 |
self.make_constant_int(op, 1) |
| 437 |
elif b1.known_unsigned_lt(b2): |
| 438 |
self.make_constant_int(op, 0) |
| 439 |
else: |
| 440 |
return self.emit(op) |
| 441 |
|
| 442 |
def propagate_bounds_UINT_GE(self, op): |
| 443 |
r = self.getintbound(op) |
| 444 |
if r.is_constant(): |
| 445 |
if r.get_constant_int() == 1: |
| 446 |
self.make_unsigned_ge(op.getarg(0), op.getarg(1)) |
| 447 |
else: |
| 448 |
assert r.get_constant_int() == 0 |
| 449 |
self.make_unsigned_lt(op.getarg(0), op.getarg(1)) |
| 450 |
|
| 451 |
def optimize_INT_SIGNEXT(self, op): |
| 452 |
b = self.getintbound(op.getarg(0)) |
| 453 |
numbits = op.getarg(1).getint() * 8 |
| 454 |
start = -(1 << (numbits - 1)) |
| 455 |
stop = 1 << (numbits - 1) |
| 456 |
if b.is_within_range(start, stop - 1): |
| 457 |
self.make_equal_to(op, op.getarg(0)) |
| 458 |
else: |
| 459 |
return self.emit(op) |
| 460 |
|
| 461 |
def postprocess_INT_SIGNEXT(self, op): |
| 462 |
numbits = op.getarg(1).getint() * 8 |
| 463 |
start = -(1 << (numbits - 1)) |
| 464 |
stop = 1 << (numbits - 1) |
| 465 |
bres = self.getintbound(op) |
| 466 |
bres.intersect_const(start, stop - 1) |
| 467 |
|
| 468 |
def postprocess_INT_FORCE_GE_ZERO(self, op): |
| 469 |
b = self.getintbound(op) |
| 470 |
b.make_ge_const(0) |
| 471 |
b1 = self.getintbound(op.getarg(0)) |
| 472 |
if b1.upper >= 0: |
| 473 |
b.make_le(b1) |
| 474 |
|
| 475 |
def postprocess_INT_INVERT(self, op): |
| 476 |
b = self.getintbound(op.getarg(0)) |
| 477 |
bounds = b.invert_bound() |
| 478 |
bres = self.getintbound(op) |
| 479 |
bres.intersect(bounds) |
| 480 |
|
| 481 |
def propagate_bounds_INT_INVERT(self, op): |
| 482 |
arg0 = get_box_replacement(op.getarg(0)) |
| 483 |
b = self.getintbound(arg0) |
| 484 |
bres = self.getintbound(op) |
| 485 |
bounds = bres.invert_bound() |
| 486 |
if b.intersect(bounds): |
| 487 |
self.propagate_bounds_backward(arg0) |
| 488 |
|
| 489 |
def propagate_bounds_INT_NEG(self, op): |
| 490 |
arg0 = get_box_replacement(op.getarg(0)) |
| 491 |
b = self.getintbound(arg0) |
| 492 |
bres = self.getintbound(op) |
| 493 |
bounds = bres.neg_bound() |
| 494 |
if b.intersect(bounds): |
| 495 |
self.propagate_bounds_backward(arg0) |
| 496 |
|
| 497 |
def postprocess_INT_NEG(self, op): |
| 498 |
b = self.getintbound(op.getarg(0)) |
| 499 |
bounds = b.neg_bound() |
| 500 |
bres = self.getintbound(op) |
| 501 |
bres.intersect(bounds) |
| 502 |
|
| 503 |
def postprocess_ARRAYLEN_GC(self, op): |
| 504 |
array = self.ensure_ptr_info_arg0(op) |
| 505 |
self.optimizer.setintbound(op, array.getlenbound(None)) |
| 506 |
|
| 507 |
def postprocess_STRLEN(self, op): |
| 508 |
self.make_nonnull_str(op.getarg(0), vstring.mode_string) |
| 509 |
array = getptrinfo(op.getarg(0)) |
| 510 |
self.optimizer.setintbound(op, array.getlenbound(vstring.mode_string)) |
| 511 |
|
| 512 |
def postprocess_UNICODELEN(self, op): |
| 513 |
self.make_nonnull_str(op.getarg(0), vstring.mode_unicode) |
| 514 |
array = getptrinfo(op.getarg(0)) |
| 515 |
self.optimizer.setintbound(op, array.getlenbound(vstring.mode_unicode)) |
| 516 |
|
| 517 |
def postprocess_STRGETITEM(self, op): |
| 518 |
v1 = self.getintbound(op) |
| 519 |
v2 = getptrinfo(op.getarg(0)) |
| 520 |
intbound = self.getintbound(op.getarg(1)) |
| 521 |
if v2 is not None: |
| 522 |
lenbound = v2.getlenbound(vstring.mode_string) |
| 523 |
if lenbound is not None: |
| 524 |
lenbound.make_gt_const(intbound.lower) |
| 525 |
v1.intersect_const(0, 255) |
| 526 |
|
| 527 |
def postprocess_GETFIELD_RAW_I(self, op): |
| 528 |
descr = op.getdescr() |
| 529 |
if descr.is_integer_bounded(): |
| 530 |
b1 = self.getintbound(op) |
| 531 |
b1.intersect_const(descr.get_integer_min(), descr.get_integer_max()) |
| 532 |
|
| 533 |
postprocess_GETFIELD_RAW_F = postprocess_GETFIELD_RAW_I |
| 534 |
postprocess_GETFIELD_RAW_R = postprocess_GETFIELD_RAW_I |
| 535 |
postprocess_GETFIELD_GC_I = postprocess_GETFIELD_RAW_I |
| 536 |
postprocess_GETFIELD_GC_R = postprocess_GETFIELD_RAW_I |
| 537 |
postprocess_GETFIELD_GC_F = postprocess_GETFIELD_RAW_I |
| 538 |
|
| 539 |
postprocess_GETINTERIORFIELD_GC_I = postprocess_GETFIELD_RAW_I |
| 540 |
postprocess_GETINTERIORFIELD_GC_R = postprocess_GETFIELD_RAW_I |
| 541 |
postprocess_GETINTERIORFIELD_GC_F = postprocess_GETFIELD_RAW_I |
| 542 |
|
| 543 |
def postprocess_GETARRAYITEM_RAW_I(self, op): |
| 544 |
descr = op.getdescr() |
| 545 |
if descr and descr.is_item_integer_bounded(): |
| 546 |
intbound = self.getintbound(op) |
| 547 |
intbound.intersect_const(descr.get_item_integer_min(), descr.get_item_integer_max()) |
| 548 |
|
| 549 |
postprocess_GETARRAYITEM_RAW_F = postprocess_GETARRAYITEM_RAW_I |
| 550 |
postprocess_GETARRAYITEM_GC_I = postprocess_GETARRAYITEM_RAW_I |
| 551 |
postprocess_GETARRAYITEM_GC_F = postprocess_GETARRAYITEM_RAW_I |
| 552 |
postprocess_GETARRAYITEM_GC_R = postprocess_GETARRAYITEM_RAW_I |
| 553 |
|
| 554 |
def postprocess_UNICODEGETITEM(self, op): |
| 555 |
b1 = self.getintbound(op) |
| 556 |
b1.make_ge_const(0) |
| 557 |
v2 = getptrinfo(op.getarg(0)) |
| 558 |
intbound = self.getintbound(op.getarg(1)) |
| 559 |
if v2 is not None: |
| 560 |
lenbound = v2.getlenbound(vstring.mode_unicode) |
| 561 |
if lenbound is not None: |
| 562 |
lenbound.make_gt_const(intbound.lower) |
| 563 |
|
| 564 |
def make_int_lt(self, box1, box2): |
| 565 |
b1 = self.getintbound(box1) |
| 566 |
b2 = self.getintbound(box2) |
| 567 |
if b1.make_lt(b2): |
| 568 |
self.propagate_bounds_backward(box1) |
| 569 |
if b2.make_gt(b1): |
| 570 |
self.propagate_bounds_backward(box2) |
| 571 |
|
| 572 |
def make_int_le(self, box1, box2): |
| 573 |
b1 = self.getintbound(box1) |
| 574 |
b2 = self.getintbound(box2) |
| 575 |
if b1.make_le(b2): |
| 576 |
self.propagate_bounds_backward(box1) |
| 577 |
if b2.make_ge(b1): |
| 578 |
self.propagate_bounds_backward(box2) |
| 579 |
|
| 580 |
def make_int_gt(self, box1, box2): |
| 581 |
self.make_int_lt(box2, box1) |
| 582 |
|
| 583 |
def make_int_ge(self, box1, box2): |
| 584 |
self.make_int_le(box2, box1) |
| 585 |
|
| 586 |
def make_unsigned_lt(self, box1, box2): |
| 587 |
b1 = self.getintbound(box1) |
| 588 |
b2 = self.getintbound(box2) |
| 589 |
if b1.make_unsigned_lt(b2): |
| 590 |
self.propagate_bounds_backward(box1) |
| 591 |
if b2.make_unsigned_gt(b1): |
| 592 |
self.propagate_bounds_backward(box2) |
| 593 |
|
| 594 |
def make_unsigned_le(self, box1, box2): |
| 595 |
b1 = self.getintbound(box1) |
| 596 |
b2 = self.getintbound(box2) |
| 597 |
if b1.make_unsigned_le(b2): |
| 598 |
self.propagate_bounds_backward(box1) |
| 599 |
if b2.make_unsigned_ge(b1): |
| 600 |
self.propagate_bounds_backward(box2) |
| 601 |
|
| 602 |
def make_unsigned_gt(self, box1, box2): |
| 603 |
self.make_unsigned_lt(box2, box1) |
| 604 |
|
| 605 |
def make_unsigned_ge(self, box1, box2): |
| 606 |
self.make_unsigned_le(box2, box1) |
| 607 |
|
| 608 |
def propagate_bounds_INT_LT(self, op): |
| 609 |
r = self.getintbound(op) |
| 610 |
if r.is_constant(): |
| 611 |
if r.get_constant_int() == 1: |
| 612 |
self.make_int_lt(op.getarg(0), op.getarg(1)) |
| 613 |
else: |
| 614 |
assert r.get_constant_int() == 0 |
| 615 |
self.make_int_ge(op.getarg(0), op.getarg(1)) |
| 616 |
|
| 617 |
def propagate_bounds_INT_GT(self, op): |
| 618 |
r = self.getintbound(op) |
| 619 |
if r.is_constant(): |
| 620 |
if r.get_constant_int() == 1: |
| 621 |
self.make_int_gt(op.getarg(0), op.getarg(1)) |
| 622 |
else: |
| 623 |
assert r.get_constant_int() == 0 |
| 624 |
self.make_int_le(op.getarg(0), op.getarg(1)) |
| 625 |
|
| 626 |
def propagate_bounds_INT_LE(self, op): |
| 627 |
r = self.getintbound(op) |
| 628 |
if r.is_constant(): |
| 629 |
if r.get_constant_int() == 1: |
| 630 |
self.make_int_le(op.getarg(0), op.getarg(1)) |
| 631 |
else: |
| 632 |
assert r.get_constant_int() == 0 |
| 633 |
self.make_int_gt(op.getarg(0), op.getarg(1)) |
| 634 |
|
| 635 |
def propagate_bounds_INT_GE(self, op): |
| 636 |
r = self.getintbound(op) |
| 637 |
if r.is_constant(): |
| 638 |
if r.get_constant_int() == 1: |
| 639 |
self.make_int_ge(op.getarg(0), op.getarg(1)) |
| 640 |
else: |
| 641 |
assert r.get_constant_int() == 0 |
| 642 |
self.make_int_lt(op.getarg(0), op.getarg(1)) |
| 643 |
|
| 644 |
def propagate_bounds_INT_EQ(self, op): |
| 645 |
r = self.getintbound(op) |
| 646 |
if r.known_eq_const(1): |
| 647 |
self.make_eq(op.getarg(0), op.getarg(1)) |
| 648 |
elif r.known_eq_const(0): |
| 649 |
self.make_ne(op.getarg(0), op.getarg(1)) |
| 650 |
|
| 651 |
def propagate_bounds_INT_NE(self, op): |
| 652 |
r = self.getintbound(op) |
| 653 |
if r.known_eq_const(0): |
| 654 |
self.make_eq(op.getarg(0), op.getarg(1)) |
| 655 |
elif r.known_eq_const(1): |
| 656 |
self.make_ne(op.getarg(0), op.getarg(1)) |
| 657 |
|
| 658 |
def make_eq(self, arg0, arg1): |
| 659 |
b0 = self.getintbound(arg0) |
| 660 |
b1 = self.getintbound(arg1) |
| 661 |
if b0.intersect(b1): |
| 662 |
self.propagate_bounds_backward(arg0) |
| 663 |
if b1.intersect(b0): |
| 664 |
self.propagate_bounds_backward(arg1) |
| 665 |
|
| 666 |
def make_ne(self, arg0, arg1): |
| 667 |
b0 = self.getintbound(arg0) |
| 668 |
b1 = self.getintbound(arg1) |
| 669 |
if b1.is_constant(): |
| 670 |
v1 = b1.get_constant_int() |
| 671 |
if b0.make_ne_const(v1): |
| 672 |
self.propagate_bounds_backward(arg0) |
| 673 |
elif b0.is_constant(): |
| 674 |
v0 = b0.get_constant_int() |
| 675 |
if b1.make_ne_const(v0): |
| 676 |
self.propagate_bounds_backward(arg1) |
| 677 |
|
| 678 |
def _propagate_int_is_true_or_zero(self, op, valnonzero, valzero): |
| 679 |
if self.is_raw_ptr(op.getarg(0)): |
| 680 |
return |
| 681 |
r = self.getintbound(op) |
| 682 |
if r.is_constant(): |
| 683 |
if r.get_constant_int() == valnonzero: |
| 684 |
b1 = self.getintbound(op.getarg(0)) |
| 685 |
if b1.known_nonnegative(): |
| 686 |
b1.make_gt_const(0) |
| 687 |
self.propagate_bounds_backward(op.getarg(0)) |
| 688 |
elif b1.known_le_const(0): |
| 689 |
b1.make_lt_const(0) |
| 690 |
self.propagate_bounds_backward(op.getarg(0)) |
| 691 |
elif r.get_constant_int() == valzero: |
| 692 |
self.make_constant_int(op.getarg(0), 0) |
| 693 |
self.propagate_bounds_backward(op.getarg(0)) |
| 694 |
|
| 695 |
def propagate_bounds_INT_IS_TRUE(self, op): |
| 696 |
self._propagate_int_is_true_or_zero(op, 1, 0) |
| 697 |
|
| 698 |
def propagate_bounds_INT_IS_ZERO(self, op): |
| 699 |
self._propagate_int_is_true_or_zero(op, 0, 1) |
| 700 |
|
| 701 |
def propagate_bounds_INT_ADD(self, op): |
| 702 |
if self.is_raw_ptr(op.getarg(0)) or self.is_raw_ptr(op.getarg(1)): |
| 703 |
return |
| 704 |
b1 = self.getintbound(op.getarg(0)) |
| 705 |
b2 = self.getintbound(op.getarg(1)) |
| 706 |
r = self.getintbound(op) |
| 707 |
b = r.sub_bound(b2) |
| 708 |
if b1.intersect(b): |
| 709 |
self.propagate_bounds_backward(op.getarg(0)) |
| 710 |
b = r.sub_bound(b1) |
| 711 |
if b2.intersect(b): |
| 712 |
self.propagate_bounds_backward(op.getarg(1)) |
| 713 |
|
| 714 |
def propagate_bounds_INT_SUB(self, op): |
| 715 |
b1 = self.getintbound(op.getarg(0)) |
| 716 |
b2 = self.getintbound(op.getarg(1)) |
| 717 |
r = self.getintbound(op) |
| 718 |
b = r.add_bound(b2) |
| 719 |
if b1.intersect(b): |
| 720 |
self.propagate_bounds_backward(op.getarg(0)) |
| 721 |
b = r.sub_bound(b1).neg_bound() |
| 722 |
if b2.intersect(b): |
| 723 |
self.propagate_bounds_backward(op.getarg(1)) |
| 724 |
|
| 725 |
def propagate_bounds_INT_MUL(self, op): |
| 726 |
b1 = self.getintbound(op.getarg(0)) |
| 727 |
b2 = self.getintbound(op.getarg(1)) |
| 728 |
if op.opnum != rop.INT_MUL_OVF and not b1.mul_bound_cannot_overflow(b2): |
| 729 |
|
| 730 |
return |
| 731 |
r = self.getintbound(op) |
| 732 |
b = r.py_div_bound(b2) |
| 733 |
if b1.intersect(b): |
| 734 |
self.propagate_bounds_backward(op.getarg(0)) |
| 735 |
b = r.py_div_bound(b1) |
| 736 |
if b2.intersect(b): |
| 737 |
self.propagate_bounds_backward(op.getarg(1)) |
| 738 |
|
| 739 |
def propagate_bounds_INT_LSHIFT(self, op): |
| 740 |
b1 = self.getintbound(op.getarg(0)) |
| 741 |
b2 = self.getintbound(op.getarg(1)) |
| 742 |
if not b1.lshift_bound_cannot_overflow(b2): |
| 743 |
return |
| 744 |
r = self.getintbound(op) |
| 745 |
b = r.lshift_bound_backwards(b2) |
| 746 |
if b1.intersect(b): |
| 747 |
self.propagate_bounds_backward(op.getarg(0)) |
| 748 |
|
| 749 |
def propagate_bounds_UINT_RSHIFT(self, op): |
| 750 |
b1 = self.getintbound(op.getarg(0)) |
| 751 |
b2 = self.getintbound(op.getarg(1)) |
| 752 |
if not b2.is_constant(): |
| 753 |
return |
| 754 |
r = self.getintbound(op) |
| 755 |
b = r.urshift_bound_backwards(b2) |
| 756 |
if b1.intersect(b): |
| 757 |
self.propagate_bounds_backward(op.getarg(0)) |
| 758 |
|
| 759 |
def propagate_bounds_INT_RSHIFT(self, op): |
| 760 |
b1 = self.getintbound(op.getarg(0)) |
| 761 |
b2 = self.getintbound(op.getarg(1)) |
| 762 |
if not b2.is_constant(): |
| 763 |
return |
| 764 |
r = self.getintbound(op) |
| 765 |
b = r.rshift_bound_backwards(b2) |
| 766 |
if b1.intersect(b): |
| 767 |
self.propagate_bounds_backward(op.getarg(0)) |
| 768 |
|
| 769 |
propagate_bounds_INT_ADD_OVF = propagate_bounds_INT_ADD |
| 770 |
propagate_bounds_INT_SUB_OVF = propagate_bounds_INT_SUB |
| 771 |
propagate_bounds_INT_MUL_OVF = propagate_bounds_INT_MUL |
| 772 |
|
| 773 |
def propagate_bounds_INT_AND(self, op): |
| 774 |
r = self.getintbound(op) |
| 775 |
b0 = self.getintbound(op.getarg(0)) |
| 776 |
b1 = self.getintbound(op.getarg(1)) |
| 777 |
b = b0.and_bound_backwards(r) |
| 778 |
if b1.intersect(b): |
| 779 |
self.propagate_bounds_backward(op.getarg(1)) |
| 780 |
b = b1.and_bound_backwards(r) |
| 781 |
if b0.intersect(b): |
| 782 |
self.propagate_bounds_backward(op.getarg(0)) |
| 783 |
|
| 784 |
def propagate_bounds_INT_OR(self, op): |
| 785 |
r = self.getintbound(op) |
| 786 |
b0 = self.getintbound(op.getarg(0)) |
| 787 |
b1 = self.getintbound(op.getarg(1)) |
| 788 |
b = b0.or_bound_backwards(r) |
| 789 |
if b1.intersect(b): |
| 790 |
self.propagate_bounds_backward(op.getarg(1)) |
| 791 |
b = b1.or_bound_backwards(r) |
| 792 |
if b0.intersect(b): |
| 793 |
self.propagate_bounds_backward(op.getarg(0)) |
| 794 |
|
| 795 |
def propagate_bounds_INT_XOR(self, op): |
| 796 |
r = self.getintbound(op) |
| 797 |
b0 = self.getintbound(op.getarg(0)) |
| 798 |
b1 = self.getintbound(op.getarg(1)) |
| 799 |
b = b0.xor_bound(r) |
| 800 |
if b1.intersect(b): |
| 801 |
self.propagate_bounds_backward(op.getarg(1)) |
| 802 |
b = b1.xor_bound(r) |
| 803 |
if b0.intersect(b): |
| 804 |
self.propagate_bounds_backward(op.getarg(0)) |
| 805 |
|
| 806 |
objectmodel.import_from_mixin(autogenintrules.OptIntAutoGenerated) |
| 807 |
|
| 808 |
dispatch_opt = make_dispatcher_method(OptIntBounds, 'optimize_', |
| 809 |
default=OptIntBounds.emit) |
| 810 |
dispatch_bounds_ops = make_dispatcher_method(OptIntBounds, 'propagate_bounds_') |
| 811 |
OptIntBounds.propagate_postprocess = make_dispatcher_method(OptIntBounds, 'postprocess_') |
| 812 |
OptIntBounds.have_postprocess_op = have_dispatcher_method(OptIntBounds, 'postprocess_') |
| 813 |
|
| 814 |
|
| 815 |
class IntegerAnalysisLogger(object): |
| 816 |
def __init__(self, optimizer): |
| 817 |
from rpython.jit.metainterp.logger import LogOperations |
| 818 |
|
| 819 |
self.optimizer = optimizer |
| 820 |
self.log_operations = LogOperations( |
| 821 |
optimizer.metainterp_sd, False, None) |
| 822 |
self.last_printed_repr_memo = {} |
| 823 |
|
| 824 |
def log_op(self, op): |
| 825 |
|
| 826 |
|
| 827 |
for i in range(op.numargs()): |
| 828 |
arg = get_box_replacement(op.getarg(i)) |
| 829 |
if arg.type != 'i' or arg.is_constant(): |
| 830 |
continue |
| 831 |
b = arg.get_forwarded() |
| 832 |
if not isinstance(b, IntBound) or b.is_unbounded(): |
| 833 |
continue |
| 834 |
argop = self.optimizer.as_operation(arg) |
| 835 |
if argop is not None and rop.returns_bool_result(arg.opnum) and b.is_bool(): |
| 836 |
continue |
| 837 |
r = b.__repr__() |
| 838 |
if self.last_printed_repr_memo.get(arg, '') == r: |
| 839 |
continue |
| 840 |
self.last_printed_repr_memo[arg] = r |
| 841 |
debug_print("# %s: %s %s" % ( |
| 842 |
self.log_operations.repr_of_arg(arg), b.__str__(), r)) |
| 843 |
debug_print(self.log_operations.repr_of_resop(op)) |
| 844 |
|
| 845 |
def log_result(self, op): |
| 846 |
if op.type == 'i': |
| 847 |
b = op.get_forwarded() |
| 848 |
if not isinstance(b, IntBound): |
| 849 |
return |
| 850 |
if rop.returns_bool_result(op.opnum): |
| 851 |
return |
| 852 |
|
| 853 |
r = b.__repr__() |
| 854 |
debug_print("# %s -> %s %s" % ( |
| 855 |
self.log_operations.repr_of_arg(op), b.__str__(), r)) |
| 856 |
self.last_printed_repr_memo[op] = r |
| 857 |
|
| 858 |
def log_inputargs(self, inputargs): |
| 859 |
args = ", ".join([self.log_operations.repr_of_arg(arg) for arg in inputargs]) |
| 860 |
debug_print('[' + args + ']') |
| 861 |
|
| 862 |
def print_rewrite_rule_statistics(): |
| 863 |
from rpython.rlib.debug import debug_start, debug_stop, have_debug_prints |
| 864 |
debug_start("jit-intbounds-stats") |
| 865 |
if have_debug_prints(): |
| 866 |
for opname, names, counts in OptIntBounds._all_rules_fired: |
| 867 |
debug_print(opname) |
| 868 |
for index in range(len(names)): |
| 869 |
debug_print(" " + names[index], counts[index]) |
| 870 |
debug_stop("jit-intbounds-stats") |