[pypy-svn] r34381 - in pypy/dist/pypy/translator/backendopt: . test

arigo at codespeak.net arigo at codespeak.net
Wed Nov 8 17:47:43 CET 2006


Author: arigo
Date: Wed Nov  8 17:47:42 2006
New Revision: 34381

Modified:
   pypy/dist/pypy/translator/backendopt/removeassert.py
   pypy/dist/pypy/translator/backendopt/test/test_removeassert.py
Log:
- Add logging.
- A test and a fix for it.


Modified: pypy/dist/pypy/translator/backendopt/removeassert.py
==============================================================================
--- pypy/dist/pypy/translator/backendopt/removeassert.py	(original)
+++ pypy/dist/pypy/translator/backendopt/removeassert.py	Wed Nov  8 17:47:42 2006
@@ -1,8 +1,9 @@
-from pypy.objspace.flow.model import Constant
+from pypy.objspace.flow.model import Constant, checkgraph, c_last_exception
 from pypy.translator.simplify import eliminate_empty_blocks, join_blocks
 from pypy.translator.simplify import transform_dead_op_vars
 from pypy.rpython.lltypesystem import lltype
 from pypy.rpython.lltypesystem import rclass
+from pypy.translator.backendopt.support import log
 
 
 def remove_asserts(translator, graphs):
@@ -11,10 +12,11 @@
     r_AssertionError = rclass.getclassrepr(rtyper, clsdef)
     ll_AssertionError = r_AssertionError.convert_const(AssertionError)
 
-    modified = {}
-    while graphs:
-        pending = []
-        for graph in graphs:
+    for graph in graphs:
+        count = 0
+        morework = True
+        while morework:
+            morework = False
             eliminate_empty_blocks(graph)
             join_blocks(graph)
             for link in graph.iterlinks():
@@ -22,26 +24,35 @@
                     and isinstance(link.args[0], Constant)
                     and link.args[0].value == ll_AssertionError):
                     if kill_assertion_link(graph, link):
-                        modified[graph] = True
-                        pending.append(graph)
+                        count += 1
+                        morework = True
                         break
-        graphs = pending
-    # now melt away the (hopefully) dead operation that compute the condition
-    for graph in modified:
-        transform_dead_op_vars(graph, translator)
+        if count:
+            # now melt away the (hopefully) dead operation that compute
+            # the condition
+            log.removeassert("removed %d asserts in %s" % (count, graph.name))
+            checkgraph(graph)
+            transform_dead_op_vars(graph, translator)
 
 
 def kill_assertion_link(graph, link):
     block = link.prevblock
     exits = list(block.exits)
-    if len(exits) > 1:
-        exits.remove(link)
-        if len(exits) == 1 and block.exitswitch.concretetype is lltype.Bool:
-            # condition no longer necessary
-            block.exitswitch = None
-            exits[0].exitcase = None
-            exits[0].llexitcase = None
-        block.recloseblock(*exits)
-        return True
-    else:
+    if len(exits) <= 1:
         return False
+    remove_condition = len(exits) == 2
+    if block.exitswitch == c_last_exception:
+        if link is exits[0]:
+            return False       # cannot remove the non-exceptional path
+    else:
+        if block.exitswitch.concretetype is not lltype.Bool:   # a switch
+            remove_condition = False
+
+    exits.remove(link)
+    if remove_condition:
+        # condition no longer necessary
+        block.exitswitch = None
+        exits[0].exitcase = None
+        exits[0].llexitcase = None
+    block.recloseblock(*exits)
+    return True

Modified: pypy/dist/pypy/translator/backendopt/test/test_removeassert.py
==============================================================================
--- pypy/dist/pypy/translator/backendopt/test/test_removeassert.py	(original)
+++ pypy/dist/pypy/translator/backendopt/test/test_removeassert.py	Wed Nov  8 17:47:42 2006
@@ -17,11 +17,11 @@
     else:
         return False
 
-def check(fn, args, expected_result):
+def check(fn, args, expected_result, remaining_raise=False):
     signature = [int] * len(args)   # for now
     graph, t = get_graph(fn, signature)
     remove_asserts(t, [graph])
-    assert not contains_raise(graph)
+    assert contains_raise(graph) == remaining_raise
     check_graph(graph, args, expected_result, t)
 
 
@@ -70,3 +70,15 @@
         assert isinstance(x, B)
         return x.value
     check(fn, [5], 321)
+
+def test_with_exception():
+    def g(n):
+        if n < 0:
+            raise ValueError
+    def fn(n):
+        try:
+            g(n)
+            assert False
+        except ValueError:
+            return 42
+    check(fn, [-8], 42, remaining_raise=True)



More information about the Pypy-commit mailing list