| 1 |
from rpython.flowspace.model import Constant, checkgraph |
| 2 |
from rpython.rtyper.lltypesystem import lltype |
| 3 |
from rpython.rtyper.rtyper import LowLevelOpList, inputconst |
| 4 |
from rpython.translator.backendopt.support import log |
| 5 |
from rpython.translator.simplify import eliminate_empty_blocks, join_blocks |
| 6 |
|
| 7 |
|
| 8 |
def remove_asserts(translator, graphs): |
| 9 |
rtyper = translator.rtyper |
| 10 |
excdata = rtyper.exceptiondata |
| 11 |
clsdef = translator.annotator.bookkeeper.getuniqueclassdef(AssertionError) |
| 12 |
ll_AssertionError = excdata.get_standard_ll_exc_instance(rtyper, clsdef) |
| 13 |
total_count = [0, 0] |
| 14 |
|
| 15 |
for graph in graphs: |
| 16 |
count = 0 |
| 17 |
morework = True |
| 18 |
while morework: |
| 19 |
morework = False |
| 20 |
eliminate_empty_blocks(graph) |
| 21 |
join_blocks(graph) |
| 22 |
for link in graph.iterlinks(): |
| 23 |
if (link.target is graph.exceptblock |
| 24 |
and isinstance(link.args[1], Constant) |
| 25 |
and link.args[1].value == ll_AssertionError): |
| 26 |
if kill_assertion_link(graph, link): |
| 27 |
count += 1 |
| 28 |
morework = True |
| 29 |
break |
| 30 |
else: |
| 31 |
total_count[0] += 1 |
| 32 |
if translator.config.translation.verbose: |
| 33 |
log.removeassert("cannot remove an assert from %s" % (graph.name,)) |
| 34 |
if count: |
| 35 |
|
| 36 |
|
| 37 |
total_count[1] += count |
| 38 |
if translator.config.translation.verbose: |
| 39 |
log.removeassert("removed %d asserts in %s" % (count, graph.name)) |
| 40 |
checkgraph(graph) |
| 41 |
total_count = tuple(total_count) |
| 42 |
if total_count[0] == 0: |
| 43 |
if total_count[1] == 0: |
| 44 |
msg = None |
| 45 |
else: |
| 46 |
msg = "Removed %d asserts" % (total_count[1],) |
| 47 |
else: |
| 48 |
if total_count[1] == 0: |
| 49 |
msg = "Could not remove %d asserts" % (total_count[0],) |
| 50 |
else: |
| 51 |
msg = "Could not remove %d asserts, but removed %d asserts." % total_count |
| 52 |
if msg is not None: |
| 53 |
log.removeassert(msg) |
| 54 |
|
| 55 |
|
| 56 |
def kill_assertion_link(graph, link): |
| 57 |
block = link.prevblock |
| 58 |
exits = list(block.exits) |
| 59 |
if len(exits) <= 1: |
| 60 |
return False |
| 61 |
remove_condition = len(exits) == 2 |
| 62 |
if block.canraise: |
| 63 |
if link is exits[0]: |
| 64 |
return False |
| 65 |
else: |
| 66 |
if block.exitswitch.concretetype is not lltype.Bool: |
| 67 |
remove_condition = False |
| 68 |
else: |
| 69 |
|
| 70 |
|
| 71 |
assert remove_condition |
| 72 |
newops = LowLevelOpList() |
| 73 |
if link.exitcase: |
| 74 |
v = newops.genop('bool_not', [block.exitswitch], |
| 75 |
resulttype=lltype.Bool) |
| 76 |
else: |
| 77 |
v = block.exitswitch |
| 78 |
msg = "assertion failed in %s" % (graph.name,) |
| 79 |
c_msg = inputconst(lltype.Void, msg) |
| 80 |
newops.genop('debug_assert', [v, c_msg]) |
| 81 |
block.operations.extend(newops) |
| 82 |
|
| 83 |
exits.remove(link) |
| 84 |
if remove_condition: |
| 85 |
|
| 86 |
block.exitswitch = None |
| 87 |
exits[0].exitcase = None |
| 88 |
exits[0].llexitcase = None |
| 89 |
block.recloseblock(*exits) |
| 90 |
return True |