| 1 |
from rpython.flowspace.model import Variable, Constant |
| 2 |
from rpython.translator.backendopt.support import log |
| 3 |
from rpython.translator import simplify |
| 4 |
|
| 5 |
|
| 6 |
def remove_unaryops(graph, opnames): |
| 7 |
"""Removes unary low-level ops with a name appearing in the opnames list. |
| 8 |
""" |
| 9 |
positions = [] |
| 10 |
touched_blocks = set() |
| 11 |
for block in graph.iterblocks(): |
| 12 |
for i, op in enumerate(block.operations): |
| 13 |
if op.opname in opnames: |
| 14 |
positions.append((block, i)) |
| 15 |
while positions: |
| 16 |
block, index = positions.pop() |
| 17 |
op_result = block.operations[index].result |
| 18 |
op_arg = block.operations[index].args[0] |
| 19 |
|
| 20 |
|
| 21 |
for op in block.operations[index:]: |
| 22 |
if op is not None: |
| 23 |
for i in range(len(op.args)): |
| 24 |
if op.args[i] == op_result: |
| 25 |
op.args[i] = op_arg |
| 26 |
if (op.opname == "indirect_call" |
| 27 |
and isinstance(op.args[0], Constant)): |
| 28 |
op.opname = "direct_call" |
| 29 |
op.args = op.args[:-1] |
| 30 |
for link in block.exits: |
| 31 |
for i in range(len(link.args)): |
| 32 |
if link.args[i] == op_result: |
| 33 |
link.args[i] = op_arg |
| 34 |
if block.exitswitch == op_result: |
| 35 |
if isinstance(op_arg, Variable): |
| 36 |
block.exitswitch = op_arg |
| 37 |
else: |
| 38 |
simplify.replace_exitswitch_by_constant(block, op_arg) |
| 39 |
block.operations[index] = None |
| 40 |
touched_blocks.add(block) |
| 41 |
|
| 42 |
|
| 43 |
for block in touched_blocks: |
| 44 |
if block.operations: |
| 45 |
block.operations[:] = filter(None, block.operations) |
| 46 |
|
| 47 |
def remove_same_as(graph): |
| 48 |
remove_unaryops(graph, ["same_as"]) |
| 49 |
|
| 50 |
def remove_duplicate_casts(graph, translator): |
| 51 |
simplify.join_blocks(graph) |
| 52 |
num_removed = 0 |
| 53 |
|
| 54 |
for block in graph.iterblocks(): |
| 55 |
comes_from = {} |
| 56 |
for op in block.operations: |
| 57 |
if op.opname == "cast_pointer": |
| 58 |
if op.args[0] in comes_from: |
| 59 |
from_var = comes_from[op.args[0]] |
| 60 |
comes_from[op.result] = from_var |
| 61 |
if from_var.concretetype == op.result.concretetype: |
| 62 |
op.opname = "same_as" |
| 63 |
op.args = [from_var] |
| 64 |
num_removed += 1 |
| 65 |
else: |
| 66 |
op.args = [from_var] |
| 67 |
else: |
| 68 |
comes_from[op.result] = op.args[0] |
| 69 |
if num_removed: |
| 70 |
remove_same_as(graph) |
| 71 |
|
| 72 |
for block in graph.iterblocks(): |
| 73 |
available = {} |
| 74 |
for op in block.operations: |
| 75 |
if op.opname == "cast_pointer": |
| 76 |
key = (op.args[0], op.result.concretetype) |
| 77 |
if key in available: |
| 78 |
op.opname = "same_as" |
| 79 |
op.args = [available[key]] |
| 80 |
num_removed += 1 |
| 81 |
else: |
| 82 |
available[key] = op.result |
| 83 |
if num_removed: |
| 84 |
remove_same_as(graph) |
| 85 |
|
| 86 |
for block in graph.iterblocks(): |
| 87 |
used = {} |
| 88 |
for link in block.exits: |
| 89 |
for arg in link.args: |
| 90 |
used[arg] = True |
| 91 |
for i, op in list(enumerate(block.operations))[::-1]: |
| 92 |
if op.opname == "cast_pointer" and op.result not in used: |
| 93 |
del block.operations[i] |
| 94 |
num_removed += 1 |
| 95 |
else: |
| 96 |
for arg in op.args: |
| 97 |
used[arg] = True |
| 98 |
if translator.config.translation.verbose: |
| 99 |
log.removecasts( |
| 100 |
"removed %s cast_pointers in %s" % (num_removed, graph.name)) |
| 101 |
return num_removed |
| 102 |
|
| 103 |
def remove_debug_assert(graph): |
| 104 |
for block in graph.iterblocks(): |
| 105 |
for i, op in list(enumerate(block.operations))[::-1]: |
| 106 |
if op.opname == "debug_assert": |
| 107 |
del block.operations[i] |