| 1 |
from collections import OrderedDict |
| 2 |
|
| 3 |
from rpython.jit.codewriter.effectinfo import EffectInfo |
| 4 |
from rpython.jit.metainterp.optimizeopt.util import args_dict |
| 5 |
from rpython.jit.metainterp.history import new_ref_dict |
| 6 |
from rpython.jit.metainterp.optimizeopt.optimizer import Optimization, REMOVED |
| 7 |
from rpython.jit.metainterp.optimizeopt.util import ( |
| 8 |
make_dispatcher_method, have_dispatcher_method, get_box_replacement) |
| 9 |
from rpython.jit.metainterp.optimizeopt.intutils import IntBound |
| 10 |
from rpython.jit.metainterp.optimizeopt.shortpreamble import PreambleOp |
| 11 |
from rpython.jit.metainterp.optimize import InvalidLoop |
| 12 |
from rpython.jit.metainterp.resoperation import rop |
| 13 |
from rpython.rlib.objectmodel import we_are_translated |
| 14 |
from rpython.jit.metainterp.optimizeopt import info |
| 15 |
|
| 16 |
MUST_ALIAS = 'm' |
| 17 |
CANNOT_ALIAS = 'c' |
| 18 |
UNKNOWN_ALIAS = '?' |
| 19 |
|
| 20 |
class AbstractCachedEntry(object): |
| 21 |
""" abstract base class abstracting over the difference between caching |
| 22 |
struct fields and array items. """ |
| 23 |
|
| 24 |
def __init__(self): |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
self.cached_infos = [] |
| 39 |
self.cached_structs = [] |
| 40 |
self._lazy_set = None |
| 41 |
|
| 42 |
def register_info(self, structop, info): |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
assert structop.type == 'r' |
| 48 |
self.cached_structs.append(structop) |
| 49 |
self.cached_infos.append(info) |
| 50 |
|
| 51 |
def produce_potential_short_preamble_ops(self, optimizer, shortboxes, |
| 52 |
descr, index=-1): |
| 53 |
assert self._lazy_set is None |
| 54 |
for i, info in enumerate(self.cached_infos): |
| 55 |
structbox = get_box_replacement(self.cached_structs[i]) |
| 56 |
info.produce_short_preamble_ops(structbox, descr, index, optimizer, |
| 57 |
shortboxes) |
| 58 |
|
| 59 |
def possible_aliasing(self, opinfo): |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
return (self._lazy_set is not None |
| 65 |
and not info.getptrinfo(self._lazy_set.getarg(0)).same_info(opinfo)) |
| 66 |
|
| 67 |
def possible_aliasing_two_infos(self, optheap, opinfo1, opinfo2): |
| 68 |
"""returns MUST_ALIAS, CANNOT_ALIAS, UNKNOWN_ALIAS """ |
| 69 |
if opinfo1.same_info(opinfo2): |
| 70 |
return MUST_ALIAS |
| 71 |
if self._cannot_alias_via_classes_or_lengths(optheap, opinfo1, opinfo2) == CANNOT_ALIAS: |
| 72 |
return CANNOT_ALIAS |
| 73 |
if self._cannot_alias_via_content(optheap, opinfo1, opinfo2) == CANNOT_ALIAS: |
| 74 |
return CANNOT_ALIAS |
| 75 |
return UNKNOWN_ALIAS |
| 76 |
|
| 77 |
def do_setfield(self, optheap, op): |
| 78 |
|
| 79 |
structinfo = optheap.ensure_ptr_info_arg0(op) |
| 80 |
arg1 = get_box_replacement(self._get_rhs_from_set_op(op)) |
| 81 |
if self.possible_aliasing(structinfo): |
| 82 |
self.force_lazy_set(optheap, op.getdescr()) |
| 83 |
assert not self.possible_aliasing(structinfo) |
| 84 |
cached_field = self._getfield(structinfo, op.getdescr(), optheap, False) |
| 85 |
if cached_field is not None: |
| 86 |
cached_field = cached_field.get_box_replacement() |
| 87 |
|
| 88 |
if not cached_field or not cached_field.same_box(arg1): |
| 89 |
|
| 90 |
self._lazy_set = op |
| 91 |
else: |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
self._getfield(structinfo, op.getdescr(), optheap) |
| 101 |
self._lazy_set = None |
| 102 |
|
| 103 |
def getfield_from_cache(self, optheap, opinfo, descr): |
| 104 |
|
| 105 |
op = self._lazy_set |
| 106 |
if op: |
| 107 |
opinfo2 = info.getptrinfo(op.getarg(0)) |
| 108 |
aliasing_state = self.possible_aliasing_two_infos(optheap, opinfo, opinfo2) |
| 109 |
if aliasing_state == UNKNOWN_ALIAS: |
| 110 |
self.force_lazy_set(optheap, descr) |
| 111 |
return None |
| 112 |
elif aliasing_state == MUST_ALIAS: |
| 113 |
return get_box_replacement(self._get_rhs_from_set_op(op)) |
| 114 |
else: |
| 115 |
assert aliasing_state == CANNOT_ALIAS |
| 116 |
|
| 117 |
res = self._getfield(opinfo, descr, optheap) |
| 118 |
if res is not None: |
| 119 |
return res.get_box_replacement() |
| 120 |
return None |
| 121 |
|
| 122 |
def force_lazy_set(self, optheap, descr, can_cache=True): |
| 123 |
op = self._lazy_set |
| 124 |
if op is not None: |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
self.invalidate(descr) |
| 130 |
self._lazy_set = None |
| 131 |
if optheap.postponed_op: |
| 132 |
for a in op.getarglist(): |
| 133 |
if a is optheap.postponed_op: |
| 134 |
optheap.emit_postponed_op() |
| 135 |
break |
| 136 |
optheap.emit_extra(op, emit=False) |
| 137 |
if not can_cache: |
| 138 |
return |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
opinfo = optheap.ensure_ptr_info_arg0(op) |
| 143 |
self.put_field_back_to_info(op, opinfo, optheap) |
| 144 |
elif not can_cache: |
| 145 |
self.invalidate(descr) |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
def _get_rhs_from_set_op(self, op): |
| 150 |
""" given a set(field or arrayitem) op, return the rhs argument """ |
| 151 |
raise NotImplementedError("abstract method") |
| 152 |
|
| 153 |
def put_field_back_to_info(self, op, opinfo, optheap): |
| 154 |
""" this method is called just after a lazy setfield was ommitted. it |
| 155 |
puts the information of the lazy setfield back into the proper cache in |
| 156 |
the info. """ |
| 157 |
raise NotImplementedError("abstract method") |
| 158 |
|
| 159 |
def _getfield(self, opinfo, descr, optheap, true_force=True): |
| 160 |
raise NotImplementedError("abstract method") |
| 161 |
|
| 162 |
def invalidate(self, descr): |
| 163 |
""" clear all the cached knowledge in the infos in self.cached_infos. |
| 164 |
""" |
| 165 |
raise NotImplementedError("abstract method") |
| 166 |
|
| 167 |
|
| 168 |
class CachedField(AbstractCachedEntry): |
| 169 |
def _get_rhs_from_set_op(self, op): |
| 170 |
return op.getarg(1) |
| 171 |
|
| 172 |
def put_field_back_to_info(self, op, opinfo, optheap): |
| 173 |
arg = get_box_replacement(op.getarg(1)) |
| 174 |
struct = get_box_replacement(op.getarg(0)) |
| 175 |
opinfo.setfield(op.getdescr(), struct, arg, optheap=optheap, cf=self) |
| 176 |
|
| 177 |
def _getfield(self, opinfo, descr, optheap, true_force=True): |
| 178 |
res = opinfo.getfield(descr, optheap) |
| 179 |
if not we_are_translated() and res: |
| 180 |
if isinstance(opinfo, info.AbstractStructPtrInfo): |
| 181 |
assert opinfo in self.cached_infos |
| 182 |
if isinstance(res, PreambleOp): |
| 183 |
if not true_force: |
| 184 |
return res.op |
| 185 |
res = optheap.optimizer.force_op_from_preamble(res) |
| 186 |
opinfo.setfield(descr, None, res, optheap=optheap) |
| 187 |
return res |
| 188 |
|
| 189 |
def invalidate(self, descr): |
| 190 |
if descr.is_always_pure(): |
| 191 |
return |
| 192 |
for opinfo in self.cached_infos: |
| 193 |
assert isinstance(opinfo, info.AbstractStructPtrInfo) |
| 194 |
opinfo._fields[descr.get_index()] = None |
| 195 |
self.cached_infos = [] |
| 196 |
self.cached_structs = [] |
| 197 |
|
| 198 |
def _cannot_alias_via_classes_or_lengths(self, optheap, opinfo1, opinfo2): |
| 199 |
constclass1 = opinfo1.get_known_class(optheap.optimizer.cpu) |
| 200 |
constclass2 = opinfo2.get_known_class(optheap.optimizer.cpu) |
| 201 |
if constclass1 is not None and constclass2 is not None and not constclass1.same_constant(constclass2): |
| 202 |
return CANNOT_ALIAS |
| 203 |
else: |
| 204 |
return UNKNOWN_ALIAS |
| 205 |
|
| 206 |
def _cannot_alias_via_content(self, optheap, opinfo1, opinfo2): |
| 207 |
if not isinstance(opinfo1, info.AbstractStructPtrInfo): |
| 208 |
return UNKNOWN_ALIAS |
| 209 |
if not isinstance(opinfo2, info.AbstractStructPtrInfo): |
| 210 |
return UNKNOWN_ALIAS |
| 211 |
content1 = opinfo1.all_items() |
| 212 |
if content1 is None: |
| 213 |
return UNKNOWN_ALIAS |
| 214 |
content2 = opinfo2.all_items() |
| 215 |
if content2 is None: |
| 216 |
return UNKNOWN_ALIAS |
| 217 |
for index in range(min(len(content1), len(content2))): |
| 218 |
value1 = get_box_replacement(content1[index]) |
| 219 |
value2 = get_box_replacement(content2[index]) |
| 220 |
if value1 is None or value2 is None: |
| 221 |
continue |
| 222 |
if not value1.is_constant() or not value2.is_constant(): |
| 223 |
continue |
| 224 |
if not value1.same_constant(value2): |
| 225 |
return CANNOT_ALIAS |
| 226 |
return UNKNOWN_ALIAS |
| 227 |
|
| 228 |
class ArrayCachedItem(AbstractCachedEntry): |
| 229 |
def __init__(self, index, parent): |
| 230 |
assert index >= 0 |
| 231 |
self.index = index |
| 232 |
self.parent = parent |
| 233 |
AbstractCachedEntry.__init__(self) |
| 234 |
|
| 235 |
def _get_rhs_from_set_op(self, op): |
| 236 |
return op.getarg(2) |
| 237 |
|
| 238 |
def _getfield(self, opinfo, descr, optheap, true_force=True): |
| 239 |
res = opinfo.getitem(descr, self.index, optheap) |
| 240 |
if not we_are_translated() and res: |
| 241 |
if isinstance(opinfo, info.ArrayPtrInfo): |
| 242 |
assert opinfo in self.cached_infos |
| 243 |
if (isinstance(res, PreambleOp) and |
| 244 |
optheap.optimizer.cpu.supports_guard_gc_type): |
| 245 |
if not true_force: |
| 246 |
return res.op |
| 247 |
index = res.preamble_op.getarg(1).getint() |
| 248 |
res = optheap.optimizer.force_op_from_preamble(res) |
| 249 |
opinfo.setitem(descr, index, None, res, optheap=optheap) |
| 250 |
return res |
| 251 |
|
| 252 |
def put_field_back_to_info(self, op, opinfo, optheap): |
| 253 |
arg = get_box_replacement(op.getarg(2)) |
| 254 |
struct = get_box_replacement(op.getarg(0)) |
| 255 |
opinfo.setitem(op.getdescr(), self.index, struct, arg, optheap=optheap, cf=self) |
| 256 |
|
| 257 |
def invalidate(self, descr): |
| 258 |
for opinfo in self.cached_infos: |
| 259 |
assert isinstance(opinfo, info.ArrayPtrInfo) |
| 260 |
|
| 261 |
if self.index < len(opinfo._items): |
| 262 |
opinfo._items[self.index] = None |
| 263 |
|
| 264 |
self.cached_infos = [] |
| 265 |
self.cached_structs = [] |
| 266 |
self.parent.clear_varindex() |
| 267 |
|
| 268 |
def _cannot_alias_via_classes_or_lengths(self, optheap, opinfo1, opinfo2): |
| 269 |
if not isinstance(opinfo1, info.ArrayPtrInfo): |
| 270 |
return UNKNOWN_ALIAS |
| 271 |
if not isinstance(opinfo2, info.ArrayPtrInfo): |
| 272 |
return UNKNOWN_ALIAS |
| 273 |
if opinfo1.getlenbound(None).known_ne(opinfo2.getlenbound(None)): |
| 274 |
return CANNOT_ALIAS |
| 275 |
else: |
| 276 |
return UNKNOWN_ALIAS |
| 277 |
|
| 278 |
def _cannot_alias_via_content(self, optheap, opinfo1, opinfo2): |
| 279 |
if not isinstance(opinfo1, info.ArrayPtrInfo): |
| 280 |
return UNKNOWN_ALIAS |
| 281 |
if not isinstance(opinfo2, info.ArrayPtrInfo): |
| 282 |
return UNKNOWN_ALIAS |
| 283 |
content1 = opinfo1.all_items() |
| 284 |
if content1 is None: |
| 285 |
return UNKNOWN_ALIAS |
| 286 |
content2 = opinfo2.all_items() |
| 287 |
if content2 is None: |
| 288 |
return UNKNOWN_ALIAS |
| 289 |
for index in range(min(len(content1), len(content2))): |
| 290 |
value1 = get_box_replacement(content1[index]) |
| 291 |
value2 = get_box_replacement(content2[index]) |
| 292 |
if value1 is None or value2 is None: |
| 293 |
continue |
| 294 |
if not value1.is_constant() or not value2.is_constant(): |
| 295 |
continue |
| 296 |
if not value1.same_constant(value2): |
| 297 |
return CANNOT_ALIAS |
| 298 |
return UNKNOWN_ALIAS |
| 299 |
|
| 300 |
class ArrayCacheSubMap(object): |
| 301 |
def __init__(self): |
| 302 |
self.const_indexes = {} |
| 303 |
self.clear_varindex() |
| 304 |
|
| 305 |
def clear_varindex(self): |
| 306 |
self.cached_varindex_triples = None |
| 307 |
|
| 308 |
def cache_varindex_read(self, arrayinfo, indexbox, resbox): |
| 309 |
|
| 310 |
entry = (arrayinfo, indexbox, resbox) |
| 311 |
if self.cached_varindex_triples is None: |
| 312 |
self.cached_varindex_triples = [entry] |
| 313 |
return |
| 314 |
self.cached_varindex_triples.append(entry) |
| 315 |
|
| 316 |
def cache_varindex_write(self, arrayinfo, indexbox, resbox): |
| 317 |
self.cached_varindex_triples = [(arrayinfo, indexbox, resbox)] |
| 318 |
|
| 319 |
def lookup_cached(self, arrayinfo, indexbox): |
| 320 |
if self.cached_varindex_triples is not None: |
| 321 |
for cached_arrayinfo, cached_index, cached_result in self.cached_varindex_triples: |
| 322 |
if cached_arrayinfo is arrayinfo and get_box_replacement(cached_index) is indexbox: |
| 323 |
return get_box_replacement(cached_result) |
| 324 |
return None |
| 325 |
|
| 326 |
class OptHeap(Optimization): |
| 327 |
"""Cache repeated heap accesses""" |
| 328 |
|
| 329 |
def __init__(self): |
| 330 |
|
| 331 |
self.cached_fields = OrderedDict() |
| 332 |
self.cached_arrayitems = OrderedDict() |
| 333 |
|
| 334 |
self.postponed_op = None |
| 335 |
|
| 336 |
|
| 337 |
self.cached_dict_reads = {} |
| 338 |
|
| 339 |
self.corresponding_array_descrs = {} |
| 340 |
|
| 341 |
self._seen_guard_not_invalidated = False |
| 342 |
|
| 343 |
def setup(self): |
| 344 |
self.optimizer.optheap = self |
| 345 |
|
| 346 |
self.const_infos = new_ref_dict() |
| 347 |
|
| 348 |
def flush(self): |
| 349 |
self.cached_dict_reads.clear() |
| 350 |
self.corresponding_array_descrs.clear() |
| 351 |
self.force_all_lazy_sets() |
| 352 |
self.emit_postponed_op() |
| 353 |
|
| 354 |
def emit_postponed_op(self): |
| 355 |
if self.postponed_op: |
| 356 |
postponed_op = self.postponed_op |
| 357 |
self.postponed_op = None |
| 358 |
self.emit_extra(postponed_op, emit=False) |
| 359 |
|
| 360 |
def produce_potential_short_preamble_ops(self, sb): |
| 361 |
descrkeys = self.cached_fields.keys() |
| 362 |
if not we_are_translated(): |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
descrkeys.sort(key=str, reverse=True) |
| 370 |
for descr in descrkeys: |
| 371 |
d = self.cached_fields[descr] |
| 372 |
d.produce_potential_short_preamble_ops(self.optimizer, sb, descr) |
| 373 |
|
| 374 |
for descr, submap in self.cached_arrayitems.items(): |
| 375 |
for index, d in submap.const_indexes.items(): |
| 376 |
d.produce_potential_short_preamble_ops(self.optimizer, sb, |
| 377 |
descr, index) |
| 378 |
|
| 379 |
def clean_caches(self): |
| 380 |
items = self.cached_fields.items() |
| 381 |
if not we_are_translated(): |
| 382 |
items.sort(key=str, reverse=True) |
| 383 |
for descr, cf in items: |
| 384 |
if not descr.is_always_pure(): |
| 385 |
cf.invalidate(descr) |
| 386 |
for descr, submap in self.cached_arrayitems.iteritems(): |
| 387 |
if not descr.is_always_pure(): |
| 388 |
for index, cf in submap.const_indexes.iteritems(): |
| 389 |
cf.invalidate(None) |
| 390 |
self.cached_dict_reads.clear() |
| 391 |
|
| 392 |
def field_cache(self, descr): |
| 393 |
try: |
| 394 |
cf = self.cached_fields[descr] |
| 395 |
except KeyError: |
| 396 |
cf = self.cached_fields[descr] = CachedField() |
| 397 |
return cf |
| 398 |
|
| 399 |
def arrayitem_submap(self, descr, create_if_nonexistant=True): |
| 400 |
try: |
| 401 |
return self.cached_arrayitems[descr] |
| 402 |
except KeyError: |
| 403 |
if create_if_nonexistant: |
| 404 |
submap = self.cached_arrayitems[descr] = ArrayCacheSubMap() |
| 405 |
else: |
| 406 |
submap = None |
| 407 |
return submap |
| 408 |
|
| 409 |
def arrayitem_cache(self, descr, index): |
| 410 |
submap = self.arrayitem_submap(descr) |
| 411 |
try: |
| 412 |
cf = submap.const_indexes[index] |
| 413 |
except KeyError: |
| 414 |
cf = submap.const_indexes[index] = ArrayCachedItem(index, submap) |
| 415 |
return cf |
| 416 |
|
| 417 |
def emit(self, op): |
| 418 |
self.emitting_operation(op) |
| 419 |
self.emit_postponed_op() |
| 420 |
opnum = op.opnum |
| 421 |
if (rop.is_comparison(opnum) or rop.is_call_may_force(opnum) |
| 422 |
or rop.is_ovf(opnum)): |
| 423 |
self.postponed_op = op |
| 424 |
else: |
| 425 |
return Optimization.emit(self, op) |
| 426 |
|
| 427 |
def emitting_operation(self, op): |
| 428 |
if rop.has_no_side_effect(op.opnum): |
| 429 |
return |
| 430 |
if rop.is_ovf(op.opnum): |
| 431 |
return |
| 432 |
if rop.is_guard(op.opnum): |
| 433 |
self.optimizer.pendingfields = ( |
| 434 |
self.force_lazy_sets_for_guard()) |
| 435 |
return |
| 436 |
opnum = op.getopnum() |
| 437 |
if (opnum == rop.SETFIELD_GC or |
| 438 |
opnum == rop.SETFIELD_RAW or |
| 439 |
opnum == rop.SETARRAYITEM_GC or |
| 440 |
opnum == rop.SETARRAYITEM_RAW or |
| 441 |
opnum == rop.SETINTERIORFIELD_RAW or |
| 442 |
opnum == rop.RAW_STORE or |
| 443 |
opnum == rop.STRSETITEM or |
| 444 |
opnum == rop.UNICODESETITEM or |
| 445 |
opnum == rop.DEBUG_MERGE_POINT or |
| 446 |
opnum == rop.JIT_DEBUG or |
| 447 |
opnum == rop.ENTER_PORTAL_FRAME or |
| 448 |
opnum == rop.LEAVE_PORTAL_FRAME or |
| 449 |
opnum == rop.COPYSTRCONTENT or |
| 450 |
opnum == rop.COPYUNICODECONTENT or |
| 451 |
opnum == rop.CHECK_MEMORY_ERROR): |
| 452 |
return |
| 453 |
if rop.is_call(op.opnum): |
| 454 |
if rop.is_call_assembler(op.getopnum()): |
| 455 |
self._seen_guard_not_invalidated = False |
| 456 |
else: |
| 457 |
effectinfo = op.getdescr().get_extra_info() |
| 458 |
if effectinfo.check_can_invalidate(): |
| 459 |
self._seen_guard_not_invalidated = False |
| 460 |
if not effectinfo.has_random_effects(): |
| 461 |
self.force_from_effectinfo(effectinfo) |
| 462 |
return |
| 463 |
self.force_all_lazy_sets() |
| 464 |
self.clean_caches() |
| 465 |
|
| 466 |
def optimize_CALL_I(self, op): |
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
effectinfo = op.getdescr().get_extra_info() |
| 471 |
oopspecindex = effectinfo.oopspecindex |
| 472 |
if oopspecindex == EffectInfo.OS_DICT_LOOKUP: |
| 473 |
if self._optimize_CALL_DICT_LOOKUP(op): |
| 474 |
return |
| 475 |
return self.emit(op) |
| 476 |
optimize_CALL_F = optimize_CALL_I |
| 477 |
optimize_CALL_R = optimize_CALL_I |
| 478 |
optimize_CALL_N = optimize_CALL_I |
| 479 |
|
| 480 |
def _optimize_CALL_DICT_LOOKUP(self, op): |
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
from rpython.rtyper.lltypesystem.rordereddict import FLAG_LOOKUP |
| 496 |
from rpython.rtyper.lltypesystem.rordereddict import FLAG_STORE |
| 497 |
flag_value = self.getintbound(op.getarg(4)) |
| 498 |
if not flag_value.is_constant(): |
| 499 |
return False |
| 500 |
flag = flag_value.get_constant_int() |
| 501 |
if flag != FLAG_LOOKUP and flag != FLAG_STORE: |
| 502 |
return False |
| 503 |
|
| 504 |
descrs = op.getdescr().get_extra_info().extradescrs |
| 505 |
assert descrs |
| 506 |
descr1 = descrs[0] |
| 507 |
try: |
| 508 |
d = self.cached_dict_reads[descr1] |
| 509 |
except KeyError: |
| 510 |
d = self.cached_dict_reads[descr1] = args_dict() |
| 511 |
self.corresponding_array_descrs[descrs[1]] = descr1 |
| 512 |
|
| 513 |
key = [get_box_replacement(op.getarg(1)), |
| 514 |
get_box_replacement(op.getarg(2))] |
| 515 |
|
| 516 |
try: |
| 517 |
res_v = d[key] |
| 518 |
except KeyError: |
| 519 |
if flag == FLAG_LOOKUP: |
| 520 |
d[key] = op |
| 521 |
return False |
| 522 |
else: |
| 523 |
if flag != FLAG_LOOKUP: |
| 524 |
if not self.getintbound(res_v).known_ge_const(0): |
| 525 |
return False |
| 526 |
self.make_equal_to(op, res_v) |
| 527 |
self.last_emitted_operation = REMOVED |
| 528 |
return True |
| 529 |
|
| 530 |
def optimize_GUARD_NO_EXCEPTION(self, op): |
| 531 |
if self.last_emitted_operation is REMOVED: |
| 532 |
return |
| 533 |
return self.emit(op) |
| 534 |
|
| 535 |
optimize_GUARD_EXCEPTION = optimize_GUARD_NO_EXCEPTION |
| 536 |
|
| 537 |
def force_from_effectinfo(self, effectinfo): |
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
for fielddescr, cf in self.cached_fields.items(): |
| 543 |
if effectinfo.check_readonly_descr_field(fielddescr): |
| 544 |
cf.force_lazy_set(self, fielddescr) |
| 545 |
if effectinfo.check_write_descr_field(fielddescr): |
| 546 |
cf.force_lazy_set(self, fielddescr, can_cache=False) |
| 547 |
if fielddescr.is_always_pure(): |
| 548 |
continue |
| 549 |
try: |
| 550 |
del self.cached_dict_reads[fielddescr] |
| 551 |
except KeyError: |
| 552 |
pass |
| 553 |
|
| 554 |
for arraydescr, submap in self.cached_arrayitems.items(): |
| 555 |
if effectinfo.check_readonly_descr_array(arraydescr): |
| 556 |
self.force_lazy_setarrayitem_submap(submap) |
| 557 |
if effectinfo.check_write_descr_array(arraydescr): |
| 558 |
self.force_lazy_setarrayitem_submap(submap, can_cache=False) |
| 559 |
|
| 560 |
for arraydescr, dictdescr in self.corresponding_array_descrs.items(): |
| 561 |
if effectinfo.check_write_descr_array(arraydescr): |
| 562 |
try: |
| 563 |
del self.cached_dict_reads[dictdescr] |
| 564 |
except KeyError: |
| 565 |
pass |
| 566 |
|
| 567 |
if effectinfo.check_forces_virtual_or_virtualizable(): |
| 568 |
vrefinfo = self.optimizer.metainterp_sd.virtualref_info |
| 569 |
self.force_lazy_set(vrefinfo.descr_forced) |
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
def force_lazy_set(self, descr, can_cache=True): |
| 574 |
try: |
| 575 |
cf = self.cached_fields[descr] |
| 576 |
except KeyError: |
| 577 |
return |
| 578 |
cf.force_lazy_set(self, descr, can_cache) |
| 579 |
|
| 580 |
def force_lazy_setarrayitem(self, arraydescr, indexb=None, can_cache=True): |
| 581 |
submap = self.arrayitem_submap(arraydescr, create_if_nonexistant=False) |
| 582 |
if not submap: |
| 583 |
return |
| 584 |
for idx, cf in submap.const_indexes.iteritems(): |
| 585 |
if indexb is None or indexb.contains(idx): |
| 586 |
cf.force_lazy_set(self, None, can_cache) |
| 587 |
|
| 588 |
|
| 589 |
def force_lazy_setarrayitem_submap(self, submap, can_cache=True): |
| 590 |
for cf in submap.const_indexes.itervalues(): |
| 591 |
cf.force_lazy_set(self, None, can_cache) |
| 592 |
if not can_cache: |
| 593 |
submap.clear_varindex() |
| 594 |
|
| 595 |
def force_all_lazy_sets(self): |
| 596 |
items = self.cached_fields.items() |
| 597 |
if not we_are_translated(): |
| 598 |
|
| 599 |
items.sort(key=str, reverse=True) |
| 600 |
for descr, cf in items: |
| 601 |
cf.force_lazy_set(self, descr) |
| 602 |
for submap in self.cached_arrayitems.itervalues(): |
| 603 |
items = submap.const_indexes.items() |
| 604 |
if not we_are_translated(): |
| 605 |
|
| 606 |
items.sort(key=lambda item: item[0]) |
| 607 |
for index, cf in items: |
| 608 |
cf.force_lazy_set(self, None) |
| 609 |
|
| 610 |
def force_lazy_sets_for_guard(self): |
| 611 |
pendingfields = [] |
| 612 |
items = self.cached_fields.items() |
| 613 |
if not we_are_translated(): |
| 614 |
items.sort(key=str, reverse=True) |
| 615 |
for descr, cf in items: |
| 616 |
op = cf._lazy_set |
| 617 |
if op is None: |
| 618 |
continue |
| 619 |
val = op.getarg(1) |
| 620 |
if self.optimizer.is_virtual(val): |
| 621 |
pendingfields.append(op) |
| 622 |
continue |
| 623 |
cf.force_lazy_set(self, descr) |
| 624 |
for descr, submap in self.cached_arrayitems.iteritems(): |
| 625 |
for index, cf in submap.const_indexes.iteritems(): |
| 626 |
op = cf._lazy_set |
| 627 |
if op is None: |
| 628 |
continue |
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
opinfo = info.getptrinfo(op.getarg(0)) |
| 634 |
assert not opinfo.is_virtual() |
| 635 |
if self.optimizer.is_virtual(op.getarg(2)): |
| 636 |
pendingfields.append(op) |
| 637 |
else: |
| 638 |
cf.force_lazy_set(self, descr) |
| 639 |
return pendingfields |
| 640 |
|
| 641 |
def optimize_GETFIELD_GC_I(self, op): |
| 642 |
descr = op.getdescr() |
| 643 |
if descr.is_always_pure() and self.get_constant_box(op.getarg(0)) is not None: |
| 644 |
resbox = self.optimizer.constant_fold(op) |
| 645 |
self.optimizer.make_constant(op, resbox) |
| 646 |
return |
| 647 |
structinfo = self.ensure_ptr_info_arg0(op) |
| 648 |
cf = self.field_cache(descr) |
| 649 |
field = cf.getfield_from_cache(self, structinfo, descr) |
| 650 |
if field is not None: |
| 651 |
self.make_equal_to(op, field) |
| 652 |
return |
| 653 |
|
| 654 |
self.make_nonnull(op.getarg(0)) |
| 655 |
|
| 656 |
return self.emit(op) |
| 657 |
|
| 658 |
def postprocess_GETFIELD_GC_I(self, op): |
| 659 |
|
| 660 |
structinfo = self.ensure_ptr_info_arg0(op) |
| 661 |
cf = self.field_cache(op.getdescr()) |
| 662 |
structinfo.setfield(op.getdescr(), op.getarg(0), op, optheap=self, |
| 663 |
cf=cf) |
| 664 |
optimize_GETFIELD_GC_R = optimize_GETFIELD_GC_I |
| 665 |
optimize_GETFIELD_GC_F = optimize_GETFIELD_GC_I |
| 666 |
|
| 667 |
postprocess_GETFIELD_GC_R = postprocess_GETFIELD_GC_I |
| 668 |
postprocess_GETFIELD_GC_F = postprocess_GETFIELD_GC_I |
| 669 |
|
| 670 |
def optimize_SETFIELD_GC(self, op): |
| 671 |
self.setfield(op) |
| 672 |
|
| 673 |
def setfield(self, op): |
| 674 |
cf = self.field_cache(op.getdescr()) |
| 675 |
cf.do_setfield(self, op) |
| 676 |
|
| 677 |
def optimize_GETARRAYITEM_GC_I(self, op): |
| 678 |
arrayinfo = self.ensure_ptr_info_arg0_array(op) |
| 679 |
indexb = self.getintbound(op.getarg(1)) |
| 680 |
cf = None |
| 681 |
if indexb.is_constant() and indexb.get_constant_int() >= 0: |
| 682 |
index = indexb.get_constant_int() |
| 683 |
arrayinfo.getlenbound(None).make_gt_const(index) |
| 684 |
|
| 685 |
cf = self.arrayitem_cache(op.getdescr(), index) |
| 686 |
field = cf.getfield_from_cache(self, arrayinfo, op.getdescr()) |
| 687 |
if field is not None: |
| 688 |
self.make_equal_to(op, field) |
| 689 |
return |
| 690 |
else: |
| 691 |
|
| 692 |
self.force_lazy_setarrayitem(op.getdescr(), |
| 693 |
self.getintbound(op.getarg(1))) |
| 694 |
|
| 695 |
submap = self.arrayitem_submap(op.getdescr(), create_if_nonexistant=False) |
| 696 |
indexop = get_box_replacement(op.getarg(1)) |
| 697 |
if submap is not None: |
| 698 |
cached_result = submap.lookup_cached(arrayinfo, indexop) |
| 699 |
if cached_result is not None: |
| 700 |
self.make_equal_to(op, cached_result) |
| 701 |
return |
| 702 |
|
| 703 |
self.make_nonnull(op.getarg(0)) |
| 704 |
|
| 705 |
return self.emit(op) |
| 706 |
|
| 707 |
def postprocess_GETARRAYITEM_GC_I(self, op): |
| 708 |
|
| 709 |
arrayinfo = self.ensure_ptr_info_arg0_array(op) |
| 710 |
indexb = self.getintbound(op.getarg(1)) |
| 711 |
if indexb.is_constant() and indexb.get_constant_int() >= 0: |
| 712 |
index = indexb.get_constant_int() |
| 713 |
cf = self.arrayitem_cache(op.getdescr(), index) |
| 714 |
arrayinfo.setitem(op.getdescr(), index, |
| 715 |
get_box_replacement(op.getarg(0)), |
| 716 |
get_box_replacement(op), optheap=self, |
| 717 |
cf=cf) |
| 718 |
else: |
| 719 |
|
| 720 |
submap = self.arrayitem_submap(op.getdescr()) |
| 721 |
submap.cache_varindex_read(arrayinfo, get_box_replacement(op.getarg(1)), get_box_replacement(op)) |
| 722 |
optimize_GETARRAYITEM_GC_R = optimize_GETARRAYITEM_GC_I |
| 723 |
optimize_GETARRAYITEM_GC_F = optimize_GETARRAYITEM_GC_I |
| 724 |
|
| 725 |
postprocess_GETARRAYITEM_GC_R = postprocess_GETARRAYITEM_GC_I |
| 726 |
postprocess_GETARRAYITEM_GC_F = postprocess_GETARRAYITEM_GC_I |
| 727 |
|
| 728 |
def optimize_GETARRAYITEM_GC_PURE_I(self, op): |
| 729 |
arrayinfo = self.ensure_ptr_info_arg0_array(op) |
| 730 |
indexb = self.getintbound(op.getarg(1)) |
| 731 |
cf = None |
| 732 |
if indexb.is_constant() and indexb.get_constant_int() >= 0: |
| 733 |
index = indexb.get_constant_int() |
| 734 |
arrayinfo.getlenbound(None).make_gt_const(index) |
| 735 |
|
| 736 |
cf = self.arrayitem_cache(op.getdescr(), index) |
| 737 |
fieldvalue = cf.getfield_from_cache(self, arrayinfo, op.getdescr()) |
| 738 |
if fieldvalue is not None: |
| 739 |
self.make_equal_to(op, fieldvalue) |
| 740 |
return |
| 741 |
else: |
| 742 |
|
| 743 |
self.force_lazy_setarrayitem(op.getdescr(), self.getintbound(op.getarg(1))) |
| 744 |
|
| 745 |
self.make_nonnull(op.getarg(0)) |
| 746 |
return self.emit(op) |
| 747 |
|
| 748 |
optimize_GETARRAYITEM_GC_PURE_R = optimize_GETARRAYITEM_GC_PURE_I |
| 749 |
optimize_GETARRAYITEM_GC_PURE_F = optimize_GETARRAYITEM_GC_PURE_I |
| 750 |
|
| 751 |
def optimize_SETARRAYITEM_GC(self, op): |
| 752 |
indexb = self.getintbound(op.getarg(1)) |
| 753 |
arrayinfo = self.ensure_ptr_info_arg0_array(op) |
| 754 |
submap = self.arrayitem_submap(op.getdescr()) |
| 755 |
if indexb.is_constant() and indexb.get_constant_int() >= 0: |
| 756 |
index = indexb.get_constant_int() |
| 757 |
|
| 758 |
arrayinfo.getlenbound(None).make_gt_const(index) |
| 759 |
cf = self.arrayitem_cache(op.getdescr(), index) |
| 760 |
cf.do_setfield(self, op) |
| 761 |
submap.clear_varindex() |
| 762 |
else: |
| 763 |
|
| 764 |
self.force_lazy_setarrayitem(op.getdescr(), indexb, can_cache=False) |
| 765 |
|
| 766 |
submap.cache_varindex_write(arrayinfo, get_box_replacement(op.getarg(1)), get_box_replacement(op.getarg(2))) |
| 767 |
return self.emit(op) |
| 768 |
|
| 769 |
def optimize_GC_LOAD_I(self, op): |
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
self.force_all_lazy_sets() |
| 774 |
self.make_nonnull(op.getarg(0)) |
| 775 |
return self.emit(op) |
| 776 |
optimize_GC_LOAD_R = optimize_GC_LOAD_I |
| 777 |
optimize_GC_LOAD_F = optimize_GC_LOAD_I |
| 778 |
|
| 779 |
optimize_GC_LOAD_INDEXED_I = optimize_GC_LOAD_I |
| 780 |
optimize_GC_LOAD_INDEXED_R = optimize_GC_LOAD_I |
| 781 |
optimize_GC_LOAD_INDEXED_F = optimize_GC_LOAD_I |
| 782 |
|
| 783 |
def optimize_QUASIIMMUT_FIELD(self, op): |
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
|
| 792 |
|
| 793 |
structvalue = self.ensure_ptr_info_arg0(op) |
| 794 |
if not structvalue.is_constant(): |
| 795 |
return |
| 796 |
|
| 797 |
from rpython.jit.metainterp.quasiimmut import QuasiImmutDescr |
| 798 |
qmutdescr = op.getdescr() |
| 799 |
assert isinstance(qmutdescr, QuasiImmutDescr) |
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
if not qmutdescr.is_still_valid_for(get_box_replacement(op.getarg(0))): |
| 804 |
raise InvalidLoop('quasi immutable field changed during tracing') |
| 805 |
|
| 806 |
if self.optimizer.quasi_immutable_deps is None: |
| 807 |
self.optimizer.quasi_immutable_deps = {} |
| 808 |
self.optimizer.quasi_immutable_deps[qmutdescr.qmut] = None |
| 809 |
|
| 810 |
def optimize_GUARD_NOT_INVALIDATED(self, op): |
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
if self._seen_guard_not_invalidated: |
| 823 |
return |
| 824 |
self._seen_guard_not_invalidated = True |
| 825 |
return self.emit(op) |
| 826 |
|
| 827 |
def serialize_optheap(self, available_boxes): |
| 828 |
result_getfield = [] |
| 829 |
for descr, cf in self.cached_fields.iteritems(): |
| 830 |
if descr.get_descr_index() == -1: |
| 831 |
continue |
| 832 |
if cf._lazy_set: |
| 833 |
continue |
| 834 |
parent_descr = descr.get_parent_descr() |
| 835 |
if not parent_descr.is_object(): |
| 836 |
continue |
| 837 |
for i, box1 in enumerate(cf.cached_structs): |
| 838 |
if not box1.is_constant() and box1 not in available_boxes: |
| 839 |
continue |
| 840 |
structinfo = cf.cached_infos[i] |
| 841 |
box2 = structinfo.getfield(descr) |
| 842 |
if box2 is None: |
| 843 |
|
| 844 |
|
| 845 |
continue |
| 846 |
box2 = box2.get_box_replacement() |
| 847 |
if box2.is_constant() or box2 in available_boxes: |
| 848 |
result_getfield.append((box1, descr, box2)) |
| 849 |
result_array = [] |
| 850 |
for descr, indexdict in self.cached_arrayitems.iteritems(): |
| 851 |
if descr.get_descr_index() == -1: |
| 852 |
continue |
| 853 |
for index, cf in indexdict.const_indexes.iteritems(): |
| 854 |
if cf._lazy_set: |
| 855 |
continue |
| 856 |
for i, box1 in enumerate(cf.cached_structs): |
| 857 |
if not box1.is_constant() and box1 not in available_boxes: |
| 858 |
continue |
| 859 |
arrayinfo = cf.cached_infos[i] |
| 860 |
if index >= 2**15: |
| 861 |
continue |
| 862 |
box2 = arrayinfo.getitem(descr, index) |
| 863 |
if box2 is None: |
| 864 |
|
| 865 |
|
| 866 |
continue |
| 867 |
box2 = box2.get_box_replacement() |
| 868 |
if box2.is_constant() or box2 in available_boxes: |
| 869 |
result_array.append((box1, index, descr, box2)) |
| 870 |
return result_getfield, result_array |
| 871 |
|
| 872 |
def deserialize_optheap(self, triples_struct, triples_array): |
| 873 |
for box1, descr, box2 in triples_struct: |
| 874 |
parent_descr = descr.get_parent_descr() |
| 875 |
assert parent_descr.is_object() |
| 876 |
if box1.is_constant(): |
| 877 |
structinfo = info.ConstPtrInfo(box1) |
| 878 |
else: |
| 879 |
structinfo = box1.get_forwarded() |
| 880 |
if not isinstance(structinfo, info.AbstractVirtualPtrInfo): |
| 881 |
structinfo = info.InstancePtrInfo(parent_descr) |
| 882 |
structinfo.init_fields(parent_descr, descr.get_index()) |
| 883 |
box1.set_forwarded(structinfo) |
| 884 |
cf = self.field_cache(descr) |
| 885 |
structinfo.setfield(descr, box1, box2, optheap=self, cf=cf) |
| 886 |
|
| 887 |
for box1, index, descr, box2 in triples_array: |
| 888 |
if box1.is_constant(): |
| 889 |
arrayinfo = info.ConstPtrInfo(box1) |
| 890 |
else: |
| 891 |
arrayinfo = box1.get_forwarded() |
| 892 |
if not isinstance(arrayinfo, info.AbstractVirtualPtrInfo): |
| 893 |
arrayinfo = info.ArrayPtrInfo(descr) |
| 894 |
box1.set_forwarded(arrayinfo) |
| 895 |
cf = self.arrayitem_cache(descr, index) |
| 896 |
arrayinfo.setitem(descr, index, box1, box2, optheap=self, cf=cf) |
| 897 |
|
| 898 |
|
| 899 |
dispatch_opt = make_dispatcher_method(OptHeap, 'optimize_', |
| 900 |
default=OptHeap.emit) |
| 901 |
OptHeap.propagate_forward = dispatch_opt |
| 902 |
dispatch_postprocess = make_dispatcher_method(OptHeap, 'postprocess_') |
| 903 |
OptHeap.propagate_postprocess = dispatch_postprocess |
| 904 |
OptHeap.have_postprocess_op = have_dispatcher_method(OptHeap, 'postprocess_') |