Author: Carl Friedrich Bolz <cfb...@gmx.de>
Branch: better-storesink
Changeset: r87162:4570a9abf4c0
Date: 2016-09-09 13:41 +0200
http://bitbucket.org/pypy/pypy/changeset/4570a9abf4c0/

Log:    compute the merged result more systematically

diff --git a/rpython/translator/backendopt/cse.py 
b/rpython/translator/backendopt/cse.py
--- a/rpython/translator/backendopt/cse.py
+++ b/rpython/translator/backendopt/cse.py
@@ -55,24 +55,31 @@
         listargs[index] = self._var_rep(var)
         return (opname, concretetype, tuple(listargs))
 
+    def _find_new_res(self, results):
+        # helper function for _merge_results
+        first = self._var_rep(results[0])
+        newres = None
+        for result in results:
+            if newres is None and isinstance(result, Variable):
+                # some extra work to get nice var names
+                newres = result.copy()
+            result = self._var_rep(result)
+            if result != first:
+                break
+        else:
+            # all the same!
+            return first, False
+        if newres is None:
+            newres = Variable()
+            newres.concretetype = first.concretetype
+        return newres, True
+
     def _merge_results(self, tuples, results, backedges):
         assert len(results) == len(tuples)
-        for result in results:
-            if isinstance(result, Variable):
-                newres = result.copy()
-                break
-        else:
-            # all constants! check if all the same
-            const = results[0]
-            for result in results:
-                if result != const:
-                    newres = Variable()
-                    newres.concretetype = const.concretetype
-                    break
-            else:
-                # all the same
-                return const
-        for linkindex, (link, cache) in enumerate(tuples):
+        newres, needs_adding = self._find_new_res(results)
+        if not needs_adding:
+            return newres
+        for linkindex, (link, _) in enumerate(tuples):
             link.args.append(results[linkindex])
         tuples[0][0].target.inputargs.append(newres)
         for backedge in backedges:
diff --git a/rpython/translator/backendopt/test/test_cse.py 
b/rpython/translator/backendopt/test/test_cse.py
--- a/rpython/translator/backendopt/test/test_cse.py
+++ b/rpython/translator/backendopt/test/test_cse.py
@@ -1,6 +1,7 @@
 import pytest
 from rpython.translator.translator import TranslationContext, graphof
-from rpython.translator.backendopt.cse import CSE
+from rpython.translator.backendopt.cse import CSE, Cache
+from rpython.flowspace.model import Variable, Constant
 from rpython.translator.backendopt import removenoops
 from rpython.flowspace.model import checkgraph, summary
 from rpython.conftest import option
@@ -350,3 +351,56 @@
             return res
         self.check(read, [int, int])
 
+def fakevar(name='v'):
+    var = Variable(name)
+    var.concretetype = "fake concrete type"
+    return var
+
+def fakeconst(val):
+    const = Constant(val)
+    const.concretetype = "fake concrete type"
+    return const
+
+def test_find_new_res():
+    # unit test for _find_new_res
+
+    class FakeFamilies(object):
+        def find_rep(self, var):
+            return reps.get(var, var)
+
+    reps = {}
+    c = Cache(FakeFamilies(), None)
+
+    # two different vars
+    v1 = fakevar()
+    v2 = fakevar()
+    res, needs_adding = c._find_new_res([v1, v2])
+    assert needs_adding
+    assert isinstance(res, Variable)
+    assert res.concretetype == v1.concretetype
+
+    # the same var
+    res, needs_adding = c._find_new_res([v1, v1])
+    assert not needs_adding
+    assert res is v1
+
+    # different vars, but same reps
+    reps = {v2: v1}
+    res, needs_adding = c._find_new_res([v1, v2])
+    assert not needs_adding
+    assert res is v1
+
+    # two different consts
+    c1 = fakeconst(1)
+    c2 = fakeconst(2)
+    res, needs_adding = c._find_new_res([c1, c2])
+    assert needs_adding
+    assert isinstance(res, Variable)
+    assert res.concretetype == v1.concretetype
+
+    # the same const
+    c1 = fakeconst(1)
+    res, needs_adding = c._find_new_res([c1, c1])
+    assert not needs_adding
+    assert res is c1
+
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to