| 1 |
from rpython.jit.metainterp.history import Const, ConstInt, ConstPtr |
| 2 |
from rpython.jit.metainterp.history import FrontendOp, RefFrontendOp |
| 3 |
from rpython.jit.metainterp.history import new_ref_dict |
| 4 |
from rpython.jit.metainterp.resoperation import rop, OpHelpers |
| 5 |
from rpython.jit.metainterp.executor import constant_from_op |
| 6 |
from rpython.rlib.rarithmetic import r_uint32, r_uint |
| 7 |
from rpython.rlib.objectmodel import always_inline, specialize |
| 8 |
|
| 9 |
""" A big note: we don't do heap caches on Consts, because it used |
| 10 |
to be done with the identity of the Const instance. This gives very wonky |
| 11 |
results at best, so we decided to not do it at all. Can be fixed with |
| 12 |
interning of Consts (already done on trace anyway) |
| 13 |
""" |
| 14 |
|
| 15 |
|
| 16 |
HF_LIKELY_VIRTUAL = 0x01 |
| 17 |
HF_KNOWN_CLASS = 0x02 |
| 18 |
HF_KNOWN_NULLITY = 0x04 |
| 19 |
HF_SEEN_ALLOCATION = 0x08 |
| 20 |
HF_IS_UNESCAPED = 0x10 |
| 21 |
HF_NONSTD_VABLE = 0x20 |
| 22 |
|
| 23 |
_HF_VERSION_INC = 0x40 |
| 24 |
_HF_VERSION_MAX = r_uint(2 ** 32 - _HF_VERSION_INC) |
| 25 |
|
| 26 |
@always_inline |
| 27 |
def add_flags(ref_frontend_op, flags): |
| 28 |
f = ref_frontend_op._get_heapc_flags() |
| 29 |
f |= r_uint(flags) |
| 30 |
ref_frontend_op._set_heapc_flags(f) |
| 31 |
|
| 32 |
@always_inline |
| 33 |
def remove_flags(ref_frontend_op, flags): |
| 34 |
f = ref_frontend_op._get_heapc_flags() |
| 35 |
f &= r_uint(~flags) |
| 36 |
ref_frontend_op._set_heapc_flags(f) |
| 37 |
|
| 38 |
@always_inline |
| 39 |
def test_flags(ref_frontend_op, flags): |
| 40 |
f = ref_frontend_op._get_heapc_flags() |
| 41 |
return bool(f & r_uint(flags)) |
| 42 |
|
| 43 |
def maybe_replace_with_const(box): |
| 44 |
if not isinstance(box, Const) and box.is_replaced_with_const(): |
| 45 |
return constant_from_op(box) |
| 46 |
else: |
| 47 |
return box |
| 48 |
|
| 49 |
|
| 50 |
class CacheEntry(object): |
| 51 |
def __init__(self, heapcache): |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
self.heapcache = heapcache |
| 58 |
self.cache_anything = {} |
| 59 |
self.cache_seen_allocation = {} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
self.quasiimmut_seen = None |
| 64 |
|
| 65 |
|
| 66 |
self.quasiimmut_seen_refs = None |
| 67 |
|
| 68 |
self.last_const_box = None |
| 69 |
|
| 70 |
def _clear_cache_on_write(self, seen_allocation_of_target): |
| 71 |
if not seen_allocation_of_target: |
| 72 |
self.cache_seen_allocation.clear() |
| 73 |
self.cache_anything.clear() |
| 74 |
if self.quasiimmut_seen is not None: |
| 75 |
self.quasiimmut_seen.clear() |
| 76 |
if self.quasiimmut_seen_refs is not None: |
| 77 |
self.quasiimmut_seen_refs.clear() |
| 78 |
|
| 79 |
def _seen_alloc(self, ref_box): |
| 80 |
if not isinstance(ref_box, RefFrontendOp): |
| 81 |
return False |
| 82 |
return self.heapcache._check_flag(ref_box, HF_SEEN_ALLOCATION) |
| 83 |
|
| 84 |
def _getdict(self, seen_alloc): |
| 85 |
if seen_alloc: |
| 86 |
return self.cache_seen_allocation |
| 87 |
else: |
| 88 |
return self.cache_anything |
| 89 |
|
| 90 |
def do_write_with_aliasing(self, ref_box, fieldbox): |
| 91 |
ref_box = self._unique_const_heuristic(ref_box) |
| 92 |
seen_alloc = self._seen_alloc(ref_box) |
| 93 |
self._clear_cache_on_write(seen_alloc) |
| 94 |
self._getdict(seen_alloc)[ref_box] = fieldbox |
| 95 |
|
| 96 |
def _unique_const_heuristic(self, ref_box): |
| 97 |
|
| 98 |
|
| 99 |
if isinstance(ref_box, ConstPtr): |
| 100 |
if self.last_const_box and self.last_const_box.same_constant(ref_box): |
| 101 |
ref_box = self.last_const_box |
| 102 |
else: |
| 103 |
self.last_const_box = ref_box |
| 104 |
return ref_box |
| 105 |
|
| 106 |
def read(self, ref_box): |
| 107 |
ref_box = self._unique_const_heuristic(ref_box) |
| 108 |
dict = self._getdict(self._seen_alloc(ref_box)) |
| 109 |
try: |
| 110 |
res_box = dict[ref_box] |
| 111 |
except KeyError: |
| 112 |
return None |
| 113 |
return maybe_replace_with_const(res_box) |
| 114 |
|
| 115 |
def read_now_known(self, ref_box, fieldbox): |
| 116 |
ref_box = self._unique_const_heuristic(ref_box) |
| 117 |
self._getdict(self._seen_alloc(ref_box))[ref_box] = fieldbox |
| 118 |
|
| 119 |
def invalidate_unescaped(self): |
| 120 |
self._invalidate_unescaped(self.cache_anything) |
| 121 |
self._invalidate_unescaped(self.cache_seen_allocation) |
| 122 |
if self.quasiimmut_seen is not None: |
| 123 |
self.quasiimmut_seen.clear() |
| 124 |
if self.quasiimmut_seen_refs is not None: |
| 125 |
self.quasiimmut_seen_refs.clear() |
| 126 |
|
| 127 |
def _invalidate_unescaped(self, d): |
| 128 |
for ref_box in d.keys(): |
| 129 |
if not self.heapcache.is_unescaped(ref_box): |
| 130 |
del d[ref_box] |
| 131 |
|
| 132 |
|
| 133 |
class FieldUpdater(object): |
| 134 |
def __init__(self, ref_box, cache, fieldbox): |
| 135 |
self.ref_box = ref_box |
| 136 |
self.cache = cache |
| 137 |
self.currfieldbox = fieldbox |
| 138 |
|
| 139 |
def getfield_now_known(self, fieldbox): |
| 140 |
self.cache.read_now_known(self.ref_box, fieldbox) |
| 141 |
|
| 142 |
def setfield(self, fieldbox): |
| 143 |
self.cache.do_write_with_aliasing(self.ref_box, fieldbox) |
| 144 |
|
| 145 |
|
| 146 |
class HeapCache(object): |
| 147 |
def __init__(self): |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
self.head_version = r_uint(0) |
| 160 |
self.likely_virtual_version = r_uint(0) |
| 161 |
self.reset() |
| 162 |
|
| 163 |
def reset(self): |
| 164 |
|
| 165 |
|
| 166 |
assert self.head_version < _HF_VERSION_MAX |
| 167 |
self.head_version += _HF_VERSION_INC |
| 168 |
self.likely_virtual_version = self.head_version |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
self.heap_cache = {} |
| 173 |
|
| 174 |
|
| 175 |
self.heap_array_cache = {} |
| 176 |
self.need_guard_not_invalidated = True |
| 177 |
|
| 178 |
|
| 179 |
self.loop_invariant_result = None |
| 180 |
self.loop_invariant_descr = None |
| 181 |
self.loop_invariant_arg0int = -1 |
| 182 |
|
| 183 |
def reset_keep_likely_virtuals(self): |
| 184 |
|
| 185 |
|
| 186 |
assert self.head_version < _HF_VERSION_MAX |
| 187 |
self.head_version += _HF_VERSION_INC |
| 188 |
self.heap_cache = {} |
| 189 |
self.heap_array_cache = {} |
| 190 |
|
| 191 |
@always_inline |
| 192 |
def test_head_version(self, ref_frontend_op): |
| 193 |
return ref_frontend_op._get_heapc_flags() >= self.head_version |
| 194 |
|
| 195 |
@always_inline |
| 196 |
def test_likely_virtual_version(self, ref_frontend_op): |
| 197 |
return ref_frontend_op._get_heapc_flags() >= self.likely_virtual_version |
| 198 |
|
| 199 |
def update_version(self, ref_frontend_op): |
| 200 |
"""Ensure the version of 'ref_frontend_op' is current. If not, |
| 201 |
it will update 'ref_frontend_op' (removing most flags currently set). |
| 202 |
""" |
| 203 |
if not self.test_head_version(ref_frontend_op): |
| 204 |
f = self.head_version |
| 205 |
if (self.test_likely_virtual_version(ref_frontend_op) and |
| 206 |
test_flags(ref_frontend_op, HF_LIKELY_VIRTUAL)): |
| 207 |
f |= HF_LIKELY_VIRTUAL |
| 208 |
ref_frontend_op._set_heapc_flags(f) |
| 209 |
ref_frontend_op._heapc_deps = None |
| 210 |
|
| 211 |
def invalidate_caches_varargs(self, opnum, descr, argboxes): |
| 212 |
self.mark_escaped_varargs(opnum, descr, argboxes) |
| 213 |
if self.clear_caches_not_necessary(opnum, descr): |
| 214 |
return |
| 215 |
self.clear_caches_varargs(opnum, descr, argboxes) |
| 216 |
|
| 217 |
@specialize.arg(1) |
| 218 |
def invalidate_caches(self, opnum, descr, *argboxes): |
| 219 |
self.mark_escaped(opnum, descr, *argboxes) |
| 220 |
if self.clear_caches_not_necessary(opnum, descr): |
| 221 |
return |
| 222 |
self.clear_caches(opnum, descr, *argboxes) |
| 223 |
|
| 224 |
def _escape_from_write(self, box, fieldbox): |
| 225 |
if self.is_unescaped(box) and self.is_unescaped(fieldbox): |
| 226 |
deps = self._get_deps(box) |
| 227 |
deps.append(fieldbox) |
| 228 |
elif fieldbox is not None: |
| 229 |
self._escape_box(fieldbox) |
| 230 |
|
| 231 |
@specialize.arg(1) |
| 232 |
def mark_escaped(self, opnum, descr, *argboxes): |
| 233 |
assert opnum != rop.CALL_N |
| 234 |
if opnum == rop.SETFIELD_GC: |
| 235 |
assert len(argboxes) == 2 |
| 236 |
box, fieldbox = argboxes |
| 237 |
self._escape_from_write(box, fieldbox) |
| 238 |
elif opnum == rop.SETARRAYITEM_GC: |
| 239 |
assert len(argboxes) == 3 |
| 240 |
box, indexbox, fieldbox = argboxes |
| 241 |
self._escape_from_write(box, fieldbox) |
| 242 |
|
| 243 |
elif (opnum != rop.GETFIELD_GC_R and |
| 244 |
opnum != rop.GETFIELD_GC_I and |
| 245 |
opnum != rop.GETFIELD_GC_F and |
| 246 |
opnum != rop.PTR_EQ and |
| 247 |
opnum != rop.PTR_NE and |
| 248 |
opnum != rop.INSTANCE_PTR_EQ and |
| 249 |
opnum != rop.INSTANCE_PTR_NE and |
| 250 |
opnum != rop.ASSERT_NOT_NONE): |
| 251 |
self._escape_argboxes(*argboxes) |
| 252 |
|
| 253 |
def _escape_argboxes(self, *argboxes): |
| 254 |
if len(argboxes) == 0: |
| 255 |
return |
| 256 |
self._escape_box(argboxes[0]) |
| 257 |
self._escape_argboxes(*argboxes[1:]) |
| 258 |
|
| 259 |
def mark_escaped_varargs(self, opnum, descr, argboxes): |
| 260 |
|
| 261 |
|
| 262 |
assert (opnum != rop.SETFIELD_GC and |
| 263 |
opnum != rop.SETARRAYITEM_GC and |
| 264 |
opnum != rop.GETFIELD_GC_R and |
| 265 |
opnum != rop.GETFIELD_GC_I and |
| 266 |
opnum != rop.GETFIELD_GC_F and |
| 267 |
opnum != rop.PTR_EQ and |
| 268 |
opnum != rop.PTR_NE and |
| 269 |
opnum != rop.INSTANCE_PTR_EQ and |
| 270 |
opnum != rop.INSTANCE_PTR_NE and |
| 271 |
opnum != rop.ASSERT_NOT_NONE) |
| 272 |
if (opnum == rop.CALL_N and |
| 273 |
descr.get_extra_info().oopspecindex == descr.get_extra_info().OS_ARRAYCOPY and |
| 274 |
isinstance(argboxes[3], ConstInt) and |
| 275 |
isinstance(argboxes[4], ConstInt) and |
| 276 |
isinstance(argboxes[5], ConstInt) and |
| 277 |
descr.get_extra_info().single_write_descr_array is not None): |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
pass |
| 282 |
elif (opnum == rop.CALL_N and |
| 283 |
descr.get_extra_info().oopspecindex == descr.get_extra_info().OS_ARRAYMOVE and |
| 284 |
isinstance(argboxes[2], ConstInt) and |
| 285 |
isinstance(argboxes[3], ConstInt) and |
| 286 |
isinstance(argboxes[4], ConstInt) and |
| 287 |
descr.get_extra_info().single_write_descr_array is not None): |
| 288 |
|
| 289 |
|
| 290 |
pass |
| 291 |
else: |
| 292 |
for box in argboxes: |
| 293 |
self._escape_box(box) |
| 294 |
|
| 295 |
def _escape_box(self, box): |
| 296 |
if isinstance(box, RefFrontendOp): |
| 297 |
remove_flags(box, HF_LIKELY_VIRTUAL | HF_IS_UNESCAPED) |
| 298 |
deps = box._heapc_deps |
| 299 |
if deps is not None: |
| 300 |
if not self.test_head_version(box): |
| 301 |
box._heapc_deps = None |
| 302 |
else: |
| 303 |
|
| 304 |
if deps[0] is None: |
| 305 |
box._heapc_deps = None |
| 306 |
else: |
| 307 |
box._heapc_deps = [deps[0]] |
| 308 |
for i in range(1, len(deps)): |
| 309 |
self._escape_box(deps[i]) |
| 310 |
|
| 311 |
@specialize.arg_or_var(1) |
| 312 |
def clear_caches_not_necessary(self, opnum, descr): |
| 313 |
if (opnum == rop.SETFIELD_GC or |
| 314 |
opnum == rop.SETARRAYITEM_GC or |
| 315 |
opnum == rop.SETFIELD_RAW or |
| 316 |
opnum == rop.SETARRAYITEM_RAW or |
| 317 |
opnum == rop.SETINTERIORFIELD_GC or |
| 318 |
opnum == rop.COPYSTRCONTENT or |
| 319 |
opnum == rop.COPYUNICODECONTENT or |
| 320 |
opnum == rop.STRSETITEM or |
| 321 |
opnum == rop.UNICODESETITEM or |
| 322 |
opnum == rop.SETFIELD_RAW or |
| 323 |
opnum == rop.SETARRAYITEM_RAW or |
| 324 |
opnum == rop.SETINTERIORFIELD_RAW or |
| 325 |
opnum == rop.RECORD_EXACT_CLASS or |
| 326 |
opnum == rop.RAW_STORE or |
| 327 |
opnum == rop.ASSERT_NOT_NONE or |
| 328 |
opnum == rop.RECORD_EXACT_CLASS or |
| 329 |
opnum == rop.RECORD_EXACT_VALUE_I or |
| 330 |
opnum == rop.RECORD_EXACT_VALUE_R): |
| 331 |
return True |
| 332 |
if (rop._OVF_FIRST <= opnum <= rop._OVF_LAST or |
| 333 |
rop._NOSIDEEFFECT_FIRST <= opnum <= rop._NOSIDEEFFECT_LAST or |
| 334 |
rop._GUARD_FIRST <= opnum <= rop._GUARD_LAST): |
| 335 |
return True |
| 336 |
return False |
| 337 |
|
| 338 |
def clear_caches(self, opnum, descr, *argboxes): |
| 339 |
self.clear_caches_varargs(opnum, descr, list(argboxes)) |
| 340 |
|
| 341 |
def clear_caches_varargs(self, opnum, descr, argboxes): |
| 342 |
self.need_guard_not_invalidated = True |
| 343 |
if (OpHelpers.is_plain_call(opnum) or |
| 344 |
OpHelpers.is_call_loopinvariant(opnum) or |
| 345 |
OpHelpers.is_cond_call_value(opnum) or |
| 346 |
opnum == rop.COND_CALL): |
| 347 |
effectinfo = descr.get_extra_info() |
| 348 |
ef = effectinfo.extraeffect |
| 349 |
if (ef == effectinfo.EF_LOOPINVARIANT or |
| 350 |
ef == effectinfo.EF_ELIDABLE_CANNOT_RAISE or |
| 351 |
ef == effectinfo.EF_ELIDABLE_OR_MEMORYERROR or |
| 352 |
ef == effectinfo.EF_ELIDABLE_CAN_RAISE): |
| 353 |
return |
| 354 |
|
| 355 |
|
| 356 |
elif effectinfo.oopspecindex == effectinfo.OS_ARRAYCOPY: |
| 357 |
self._clear_caches_arraycopy(opnum, descr, argboxes, effectinfo) |
| 358 |
return |
| 359 |
elif effectinfo.oopspecindex == effectinfo.OS_ARRAYMOVE: |
| 360 |
self._clear_caches_arraymove(opnum, descr, argboxes, effectinfo) |
| 361 |
return |
| 362 |
else: |
| 363 |
|
| 364 |
|
| 365 |
for descr, cache in self.heap_cache.iteritems(): |
| 366 |
cache.invalidate_unescaped() |
| 367 |
for descr, indices in self.heap_array_cache.iteritems(): |
| 368 |
for cache in indices.itervalues(): |
| 369 |
cache.invalidate_unescaped() |
| 370 |
return |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
self.reset_keep_likely_virtuals() |
| 377 |
|
| 378 |
def _clear_caches_arraycopy(self, opnum, descr, argboxes, effectinfo): |
| 379 |
self._clear_caches_arrayop(argboxes[1], argboxes[2], |
| 380 |
argboxes[3], argboxes[4], argboxes[5], |
| 381 |
effectinfo) |
| 382 |
|
| 383 |
def _clear_caches_arraymove(self, opnum, descr, argboxes, effectinfo): |
| 384 |
self._clear_caches_arrayop(argboxes[1], argboxes[1], |
| 385 |
argboxes[2], argboxes[3], argboxes[4], |
| 386 |
effectinfo) |
| 387 |
|
| 388 |
def _clear_caches_arrayop(self, source_box, dest_box, |
| 389 |
source_start_box, dest_start_box, length_box, |
| 390 |
effectinfo): |
| 391 |
seen_allocation_of_target = self._check_flag(dest_box, |
| 392 |
HF_SEEN_ALLOCATION) |
| 393 |
if ( |
| 394 |
isinstance(source_start_box, ConstInt) and |
| 395 |
isinstance(dest_start_box, ConstInt) and |
| 396 |
isinstance(length_box, ConstInt) and |
| 397 |
effectinfo.single_write_descr_array is not None |
| 398 |
): |
| 399 |
descr = effectinfo.single_write_descr_array |
| 400 |
cache = self.heap_array_cache.get(descr, None) |
| 401 |
srcstart = source_start_box.getint() |
| 402 |
dststart = dest_start_box.getint() |
| 403 |
length = length_box.getint() |
| 404 |
|
| 405 |
index_current = 0 |
| 406 |
index_delta = +1 |
| 407 |
index_stop = length |
| 408 |
if srcstart < dststart: |
| 409 |
index_current = index_stop - 1 |
| 410 |
index_delta = -1 |
| 411 |
index_stop = -1 |
| 412 |
|
| 413 |
while index_current != index_stop: |
| 414 |
i = index_current |
| 415 |
index_current += index_delta |
| 416 |
assert i >= 0 |
| 417 |
|
| 418 |
value = self.getarrayitem( |
| 419 |
source_box, |
| 420 |
ConstInt(srcstart + i), |
| 421 |
descr, |
| 422 |
) |
| 423 |
if value is not None: |
| 424 |
self.setarrayitem( |
| 425 |
dest_box, |
| 426 |
ConstInt(dststart + i), |
| 427 |
value, |
| 428 |
descr, |
| 429 |
) |
| 430 |
elif cache is not None: |
| 431 |
try: |
| 432 |
idx_cache = cache[dststart + i] |
| 433 |
except KeyError: |
| 434 |
pass |
| 435 |
else: |
| 436 |
idx_cache._clear_cache_on_write(seen_allocation_of_target) |
| 437 |
return |
| 438 |
elif ( |
| 439 |
effectinfo.single_write_descr_array is not None |
| 440 |
): |
| 441 |
|
| 442 |
cache = self.heap_array_cache.get(effectinfo.single_write_descr_array, None) |
| 443 |
if cache is not None: |
| 444 |
for idx, cache in cache.iteritems(): |
| 445 |
cache._clear_cache_on_write(seen_allocation_of_target) |
| 446 |
return |
| 447 |
self.reset_keep_likely_virtuals() |
| 448 |
|
| 449 |
def _get_deps(self, box): |
| 450 |
if not isinstance(box, RefFrontendOp): |
| 451 |
return None |
| 452 |
self.update_version(box) |
| 453 |
if box._heapc_deps is None: |
| 454 |
box._heapc_deps = [None] |
| 455 |
return box._heapc_deps |
| 456 |
|
| 457 |
def _check_flag(self, box, flag): |
| 458 |
return (isinstance(box, RefFrontendOp) and |
| 459 |
self.test_head_version(box) and |
| 460 |
test_flags(box, flag)) |
| 461 |
|
| 462 |
def _set_flag(self, box, flag): |
| 463 |
assert isinstance(box, RefFrontendOp) |
| 464 |
self.update_version(box) |
| 465 |
add_flags(box, flag) |
| 466 |
|
| 467 |
def is_class_known(self, box): |
| 468 |
return self._check_flag(box, HF_KNOWN_CLASS) |
| 469 |
|
| 470 |
def class_now_known(self, box): |
| 471 |
if isinstance(box, Const): |
| 472 |
return |
| 473 |
self._set_flag(box, HF_KNOWN_CLASS | HF_KNOWN_NULLITY) |
| 474 |
|
| 475 |
def is_nullity_known(self, box): |
| 476 |
if isinstance(box, Const): |
| 477 |
return bool(box.getref_base()) |
| 478 |
return self._check_flag(box, HF_KNOWN_NULLITY) |
| 479 |
|
| 480 |
def nullity_now_known(self, box): |
| 481 |
if isinstance(box, Const): |
| 482 |
return |
| 483 |
self._set_flag(box, HF_KNOWN_NULLITY) |
| 484 |
|
| 485 |
def is_known_nonstandard_virtualizable(self, box): |
| 486 |
return self._check_flag(box, HF_NONSTD_VABLE) or self._check_flag(box, HF_SEEN_ALLOCATION) |
| 487 |
|
| 488 |
def nonstandard_virtualizables_now_known(self, box): |
| 489 |
if isinstance(box, Const): |
| 490 |
return |
| 491 |
self._set_flag(box, HF_NONSTD_VABLE) |
| 492 |
|
| 493 |
def is_unescaped(self, box): |
| 494 |
return self._check_flag(box, HF_IS_UNESCAPED) |
| 495 |
|
| 496 |
def is_likely_virtual(self, box): |
| 497 |
|
| 498 |
return (isinstance(box, RefFrontendOp) and |
| 499 |
self.test_likely_virtual_version(box) and |
| 500 |
test_flags(box, HF_LIKELY_VIRTUAL)) |
| 501 |
|
| 502 |
def new(self, box): |
| 503 |
assert isinstance(box, RefFrontendOp) |
| 504 |
self.update_version(box) |
| 505 |
add_flags(box, HF_LIKELY_VIRTUAL | HF_SEEN_ALLOCATION | HF_IS_UNESCAPED |
| 506 |
| HF_KNOWN_NULLITY) |
| 507 |
|
| 508 |
def new_array(self, box, lengthbox): |
| 509 |
assert isinstance(box, RefFrontendOp) |
| 510 |
self.update_version(box) |
| 511 |
flags = HF_SEEN_ALLOCATION | HF_KNOWN_NULLITY |
| 512 |
if isinstance(lengthbox, Const): |
| 513 |
|
| 514 |
flags |= HF_LIKELY_VIRTUAL | HF_IS_UNESCAPED |
| 515 |
add_flags(box, flags) |
| 516 |
self.arraylen_now_known(box, lengthbox) |
| 517 |
|
| 518 |
def getfield(self, box, descr): |
| 519 |
cache = self.heap_cache.get(descr, None) |
| 520 |
if cache: |
| 521 |
return cache.read(box) |
| 522 |
return None |
| 523 |
|
| 524 |
@always_inline |
| 525 |
def get_field_updater(self, box, descr): |
| 526 |
cache = self.heap_cache.get(descr, None) |
| 527 |
if cache is None: |
| 528 |
cache = self.heap_cache[descr] = CacheEntry(self) |
| 529 |
fieldbox = None |
| 530 |
else: |
| 531 |
fieldbox = cache.read(box) |
| 532 |
return FieldUpdater(box, cache, fieldbox) |
| 533 |
|
| 534 |
def getfield_now_known(self, box, descr, fieldbox): |
| 535 |
upd = self.get_field_updater(box, descr) |
| 536 |
upd.getfield_now_known(fieldbox) |
| 537 |
|
| 538 |
def setfield(self, box, fieldbox, descr): |
| 539 |
upd = self.get_field_updater(box, descr) |
| 540 |
upd.setfield(fieldbox) |
| 541 |
|
| 542 |
def getarrayitem(self, box, indexbox, descr): |
| 543 |
if not isinstance(indexbox, ConstInt): |
| 544 |
return None |
| 545 |
index = indexbox.getint() |
| 546 |
cache = self.heap_array_cache.get(descr, None) |
| 547 |
if cache: |
| 548 |
indexcache = cache.get(index, None) |
| 549 |
if indexcache is not None: |
| 550 |
return indexcache.read(box) |
| 551 |
return None |
| 552 |
|
| 553 |
def _get_or_make_array_cache_entry(self, indexbox, descr): |
| 554 |
if not isinstance(indexbox, ConstInt): |
| 555 |
return None |
| 556 |
index = indexbox.getint() |
| 557 |
cache = self.heap_array_cache.setdefault(descr, {}) |
| 558 |
indexcache = cache.get(index, None) |
| 559 |
if indexcache is None: |
| 560 |
cache[index] = indexcache = CacheEntry(self) |
| 561 |
return indexcache |
| 562 |
|
| 563 |
|
| 564 |
def getarrayitem_now_known(self, box, indexbox, fieldbox, descr): |
| 565 |
indexcache = self._get_or_make_array_cache_entry(indexbox, descr) |
| 566 |
if indexcache: |
| 567 |
indexcache.read_now_known(box, fieldbox) |
| 568 |
|
| 569 |
def setarrayitem(self, box, indexbox, fieldbox, descr): |
| 570 |
if not isinstance(indexbox, ConstInt): |
| 571 |
cache = self.heap_array_cache.get(descr, None) |
| 572 |
if cache is not None: |
| 573 |
cache.clear() |
| 574 |
return |
| 575 |
indexcache = self._get_or_make_array_cache_entry(indexbox, descr) |
| 576 |
if indexcache: |
| 577 |
indexcache.do_write_with_aliasing(box, fieldbox) |
| 578 |
|
| 579 |
def arraylen(self, box): |
| 580 |
if (isinstance(box, RefFrontendOp) and |
| 581 |
self.test_head_version(box) and |
| 582 |
box._heapc_deps is not None): |
| 583 |
res_box = box._heapc_deps[0] |
| 584 |
if res_box is not None: |
| 585 |
return maybe_replace_with_const(res_box) |
| 586 |
return None |
| 587 |
|
| 588 |
def arraylen_now_known(self, box, lengthbox): |
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
if isinstance(box, Const): |
| 593 |
return |
| 594 |
deps = self._get_deps(box) |
| 595 |
assert deps is not None |
| 596 |
deps[0] = lengthbox |
| 597 |
|
| 598 |
def replace_box(self, oldbox, newbox): |
| 599 |
|
| 600 |
if isinstance(oldbox, FrontendOp) and isinstance(newbox, Const): |
| 601 |
assert newbox.same_constant(constant_from_op(oldbox)) |
| 602 |
oldbox.set_replaced_with_const() |
| 603 |
|
| 604 |
def is_quasi_immut_known(self, fielddescr, box): |
| 605 |
cache = self.heap_cache.get(fielddescr, None) |
| 606 |
if cache is not None: |
| 607 |
if isinstance(box, Const): |
| 608 |
if cache.quasiimmut_seen_refs is not None: |
| 609 |
return box.getref_base() in cache.quasiimmut_seen_refs |
| 610 |
else: |
| 611 |
if cache.quasiimmut_seen is not None: |
| 612 |
return box in cache.quasiimmut_seen |
| 613 |
return False |
| 614 |
|
| 615 |
def quasi_immut_now_known(self, fielddescr, box): |
| 616 |
cache = self.heap_cache.get(fielddescr, None) |
| 617 |
if cache is None: |
| 618 |
cache = self.heap_cache[fielddescr] = CacheEntry(self) |
| 619 |
if isinstance(box, Const): |
| 620 |
if cache.quasiimmut_seen_refs is None: |
| 621 |
cache.quasiimmut_seen_refs = new_ref_dict() |
| 622 |
cache.quasiimmut_seen_refs[box.getref_base()] = None |
| 623 |
else: |
| 624 |
if cache.quasiimmut_seen is not None: |
| 625 |
cache.quasiimmut_seen[box] = None |
| 626 |
else: |
| 627 |
cache.quasiimmut_seen = {box: None} |
| 628 |
|
| 629 |
def call_loopinvariant_known_result(self, allboxes, descr): |
| 630 |
if self.loop_invariant_descr is not descr: |
| 631 |
return None |
| 632 |
if self.loop_invariant_arg0int != allboxes[0].getint(): |
| 633 |
return None |
| 634 |
return self.loop_invariant_result |
| 635 |
|
| 636 |
def call_loopinvariant_now_known(self, allboxes, descr, res): |
| 637 |
self.loop_invariant_descr = descr |
| 638 |
self.loop_invariant_arg0int = allboxes[0].getint() |
| 639 |
self.loop_invariant_result = res |