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

pedronis at codespeak.net pedronis at codespeak.net
Tue May 31 17:55:08 CEST 2005


Author: pedronis
Date: Tue May 31 17:55:08 2005
New Revision: 12935

Modified:
   pypy/dist/pypy/translator/simplify.py
   pypy/dist/pypy/translator/test/test_annrpython.py
Log:
coalesce chained is_true with some tests



Modified: pypy/dist/pypy/translator/simplify.py
==============================================================================
--- pypy/dist/pypy/translator/simplify.py	(original)
+++ pypy/dist/pypy/translator/simplify.py	Tue May 31 17:55:08 2005
@@ -472,12 +472,59 @@
                         consider_blocks[link.target] = True
                     break
 
+def coalesce_is_true(graph):
+    """coalesce paths that go through an is_true and a directly successive
+       is_true both on the same value, transforming the link into the
+       second is_true from the first to directly jump to the correct
+       target out of the second."""
+    candidates = []
+
+    def has_is_true_exitpath(block):
+        tgts = []
+        start_op = block.operations[-1]
+        cond_v = start_op.args[0]
+        if block.exitswitch == start_op.result:
+            for exit in block.exits:
+                tgt = exit.target
+                if tgt == block:
+                    continue
+                rrenaming = dict(zip(tgt.inputargs,exit.args))
+                if len(tgt.operations) == 1 and tgt.operations[0].opname == 'is_true':
+                    tgt_op = tgt.operations[0]
+                    if tgt.exitswitch == tgt_op.result and rrenaming.get(tgt_op.args[0]) == cond_v:
+                        tgts.append((exit.exitcase, tgt))
+        return tgts
+
+    def visit(block):
+        if isinstance(block, Block) and block.operations and block.operations[-1].opname == 'is_true':
+            tgts = has_is_true_exitpath(block)
+            if tgts:
+                candidates.append((block, tgts))
+    traverse(visit, graph)
+
+    while candidates:
+        cand, tgts = candidates.pop()
+        newexits = list(cand.exits) 
+        for case, tgt in tgts:
+            exit = cand.exits[case]
+            rrenaming = dict(zip(tgt.inputargs,exit.args))
+            rrenaming[tgt.operations[0].result] = cand.operations[-1].result
+            def rename(v):
+                return rrenaming.get(v,v)
+            newlink = tgt.exits[case].copy(rename)
+            newexits[case] = newlink
+        cand.recloseblock(*newexits)
+        newtgts = has_is_true_exitpath(cand)
+        if newtgts:
+            candidates.append((cand, newtgts))
+            
 # ____ all passes & simplify_graph
 
 all_passes = [
     eliminate_empty_blocks,
     remove_assertion_errors,
     join_blocks,
+    coalesce_is_true,
     transform_dead_op_vars,
     remove_identical_vars,
     transform_ovfcheck,

Modified: pypy/dist/pypy/translator/test/test_annrpython.py
==============================================================================
--- pypy/dist/pypy/translator/test/test_annrpython.py	(original)
+++ pypy/dist/pypy/translator/test/test_annrpython.py	Tue May 31 17:55:08 2005
@@ -1057,6 +1057,36 @@
         s = a.build_types(snippet.prime, [int])
         assert s == annmodel.SomeBool()
 
+    def test_and_is_true_coalesce(self):
+        def f(a,b,c,d,e):
+            x = a and b
+            if x:
+                return d,c
+            return e,c
+        a = self.RPythonAnnotator()
+        s = a.build_types(f, [int, str, a.bookkeeper.immutablevalue(1.0), a.bookkeeper.immutablevalue('d'), a.bookkeeper.immutablevalue('e')])
+        assert s == annmodel.SomeTuple([annmodel.SomeChar(), a.bookkeeper.immutablevalue(1.0)])
+        assert not [b for b in a.bindings.itervalues() if b.__class__ == annmodel.SomeObject]
+
+    def test_is_true_coalesce2(self):
+        def f(a,b,a1,b1,c,d,e):
+            x = (a or  b) and (a1 or b1)
+            if x:
+                return d,c
+            return e,c
+        a = self.RPythonAnnotator()
+        s = a.build_types(f, [int, str, float, list,  a.bookkeeper.immutablevalue(1.0), a.bookkeeper.immutablevalue('d'), a.bookkeeper.immutablevalue('e')])
+        assert s == annmodel.SomeTuple([annmodel.SomeChar(), a.bookkeeper.immutablevalue(1.0)])
+        assert not [b for b in a.bindings.itervalues() if b.__class__ == annmodel.SomeObject]
+        a.translator.view()
+
+    def test_is_true_coalesce_sanity(self):
+        def f(a):
+            while a:
+                pass
+        a = self.RPythonAnnotator()
+        s = a.build_types(f, [int])
+        assert s == a.bookkeeper.immutablevalue(None)
 
 def g(n):
     return [0,1,2,n]



More information about the Pypy-commit mailing list