| 1 |
|
| 2 |
from rpython.rtyper.lltypesystem.lloperation import llop |
| 3 |
from rpython.rtyper.lltypesystem import lltype |
| 4 |
from rpython.flowspace.model import mkentrymap, Variable, Constant |
| 5 |
from rpython.translator.backendopt import removenoops |
| 6 |
from rpython.translator import simplify |
| 7 |
|
| 8 |
OK_OPS = frozenset(['debug_assert', 'debug_assert_not_none', 'jit_force_virtualizable']) |
| 9 |
|
| 10 |
def has_side_effects(op): |
| 11 |
if op.opname in OK_OPS: |
| 12 |
return False |
| 13 |
try: |
| 14 |
return getattr(llop, op.opname).sideeffects |
| 15 |
except AttributeError: |
| 16 |
return True |
| 17 |
|
| 18 |
|
| 19 |
def storesink_graph(graph): |
| 20 |
""" remove superfluous getfields. use a super-local method: all non-join |
| 21 |
blocks inherit the heap information from their (single) predecessor |
| 22 |
""" |
| 23 |
added_some_same_as = False |
| 24 |
entrymap = mkentrymap(graph) |
| 25 |
|
| 26 |
|
| 27 |
todo = [(block, None, None) for (block, prev_blocks) in entrymap.iteritems() |
| 28 |
if len(prev_blocks) > 1 or block is graph.startblock] |
| 29 |
|
| 30 |
visited = 0 |
| 31 |
|
| 32 |
while todo: |
| 33 |
block, cache, inputlink = todo.pop() |
| 34 |
visited += 1 |
| 35 |
if cache is None: |
| 36 |
cache = {} |
| 37 |
|
| 38 |
if block.operations: |
| 39 |
changed_block = _storesink_block(block, cache, inputlink) |
| 40 |
added_some_same_as = changed_block or added_some_same_as |
| 41 |
for link in block.exits: |
| 42 |
if len(entrymap[link.target]) == 1: |
| 43 |
new_cache = _translate_cache(cache, link) |
| 44 |
todo.append((link.target, new_cache, link)) |
| 45 |
|
| 46 |
assert visited == len(entrymap) |
| 47 |
if added_some_same_as: |
| 48 |
removenoops.remove_same_as(graph) |
| 49 |
simplify.transform_dead_op_vars(graph) |
| 50 |
|
| 51 |
def _translate_cache(cache, link): |
| 52 |
if link.target.operations == (): |
| 53 |
return {} |
| 54 |
block = link.target |
| 55 |
local_versions = {var1: var2 for var1, var2 in zip(link.args, block.inputargs)} |
| 56 |
def _translate_arg(arg): |
| 57 |
if isinstance(arg, Variable): |
| 58 |
res = local_versions.get(arg, None) |
| 59 |
if res is None: |
| 60 |
res = Variable(arg) |
| 61 |
res.concretetype = arg.concretetype |
| 62 |
link.args.append(arg) |
| 63 |
block.inputargs.append(res) |
| 64 |
local_versions[arg] = res |
| 65 |
return res |
| 66 |
else: |
| 67 |
return arg |
| 68 |
new_cache = {} |
| 69 |
for (var, field), res in cache.iteritems(): |
| 70 |
if var in local_versions or not isinstance(var, Variable): |
| 71 |
new_cache[_translate_arg(var), field] = _translate_arg(res) |
| 72 |
return new_cache |
| 73 |
|
| 74 |
def _storesink_block(block, cache, inputlink): |
| 75 |
def clear_cache_for(cache, concretetype, fieldname): |
| 76 |
for k in cache.keys(): |
| 77 |
if k[0].concretetype == concretetype and k[1] == fieldname: |
| 78 |
del cache[k] |
| 79 |
replacements = {} |
| 80 |
def replace(op, res): |
| 81 |
op.opname = 'same_as' |
| 82 |
op.args = [res] |
| 83 |
replacements[op.result] = res |
| 84 |
|
| 85 |
def get_rep(arg): |
| 86 |
return replacements.get(arg, arg) |
| 87 |
|
| 88 |
added_some_same_as = False |
| 89 |
for op in block.operations: |
| 90 |
if op.opname == 'getfield': |
| 91 |
arg0 = get_rep(op.args[0]) |
| 92 |
field = op.args[1].value |
| 93 |
if ( |
| 94 |
isinstance(arg0, Constant) and |
| 95 |
arg0.concretetype.TO._immutable_field(field) and |
| 96 |
arg0.value and |
| 97 |
not isinstance(arg0.value._obj, int) |
| 98 |
): |
| 99 |
|
| 100 |
llres = getattr(arg0.value, field) |
| 101 |
concretetype = getattr(arg0.concretetype.TO, field) |
| 102 |
res = Constant(llres, concretetype) |
| 103 |
replace(op, res) |
| 104 |
else: |
| 105 |
tup = (arg0, op.args[1].value) |
| 106 |
res = cache.get(tup, None) |
| 107 |
if res is not None: |
| 108 |
replace(op, res) |
| 109 |
else: |
| 110 |
cache[tup] = op.result |
| 111 |
elif op.opname == 'cast_pointer': |
| 112 |
arg0 = get_rep(op.args[0]) |
| 113 |
if isinstance(arg0, Constant): |
| 114 |
try: |
| 115 |
llres = lltype.cast_pointer(op.result.concretetype, arg0.value) |
| 116 |
except RuntimeError: |
| 117 |
|
| 118 |
|
| 119 |
pass |
| 120 |
else: |
| 121 |
res = Constant(llres, op.result.concretetype) |
| 122 |
replace(op, res) |
| 123 |
else: |
| 124 |
tup = (arg0, op.result.concretetype) |
| 125 |
res = cache.get(tup, None) |
| 126 |
if res is not None: |
| 127 |
replace(op, res) |
| 128 |
else: |
| 129 |
cache[tup] = op.result |
| 130 |
elif op.opname in ('setarrayitem', 'setinteriorfield', "malloc", "malloc_varsize"): |
| 131 |
pass |
| 132 |
elif op.opname == 'setfield': |
| 133 |
target = get_rep(op.args[0]) |
| 134 |
field = op.args[1].value |
| 135 |
clear_cache_for(cache, target.concretetype, field) |
| 136 |
cache[target, field] = op.args[2] |
| 137 |
elif has_side_effects(op): |
| 138 |
cache.clear() |
| 139 |
return bool(replacements) |