[pypy-svn] r73936 - in pypy/branch/blackhole-improvement/pypy/jit/codewriter: . test

arigo at codespeak.net arigo at codespeak.net
Wed Apr 21 14:24:52 CEST 2010


Author: arigo
Date: Wed Apr 21 14:24:50 2010
New Revision: 73936

Modified:
   pypy/branch/blackhole-improvement/pypy/jit/codewriter/flatten.py
   pypy/branch/blackhole-improvement/pypy/jit/codewriter/test/test_flatten.py
   pypy/branch/blackhole-improvement/pypy/jit/codewriter/test/test_regalloc.py
Log:
Generate <kind>_copy operations instead of just a <kind>_rename
whenever possible.


Modified: pypy/branch/blackhole-improvement/pypy/jit/codewriter/flatten.py
==============================================================================
--- pypy/branch/blackhole-improvement/pypy/jit/codewriter/flatten.py	(original)
+++ pypy/branch/blackhole-improvement/pypy/jit/codewriter/flatten.py	Wed Apr 21 14:24:50 2010
@@ -146,8 +146,7 @@
         raise CannotOptimize   # variable is not produced in cur block
 
     def insert_renamings(self, link):
-        renamings_from = []
-        renamings_to = []
+        renamings = {}
         lst = [(v, self.getcolor(link.target.inputargs[i]))
                for i, v in enumerate(link.args)]
         lst.sort(key=lambda(v, w): w.index)
@@ -156,10 +155,20 @@
                 v = self.getcolor(v)
                 if v == w:
                     continue
-            renamings_from.append(v)
-            renamings_to.append(w)
-        if renamings_from:
-            self.emitline('int_rename', renamings_from, renamings_to)
+            frm, to = renamings.setdefault(w.kind, ([], []))
+            frm.append(v)
+            to.append(w)
+        for kind in KINDS:
+            if kind in renamings:
+                frm, to = renamings[kind]
+                # If there is no cycle among the renamings, produce a series
+                # of %s_copy.  Otherwise, just one %s_rename.
+                result = reorder_renaming_list(frm, to)
+                if result is not None:
+                    for v, w in result:
+                        self.emitline('%s_copy' % kind, v, w)
+                else:
+                    self.emitline('%s_rename' % kind, frm, to)
 
     def emitline(self, *line):
         self.assembler.insns.append(line)
@@ -182,3 +191,21 @@
         except KeyError:
             r = self.registers[kind, col] = Register(kind, col)
         return r
+
+# ____________________________________________________________
+
+def reorder_renaming_list(frm, to):
+    result = []
+    pending_indices = range(len(to))
+    while pending_indices:
+        not_read = dict.fromkeys([frm[i] for i in pending_indices])
+        still_pending_indices = []
+        for i in pending_indices:
+            if to[i] not in not_read:
+                result.append((frm[i], to[i]))
+            else:
+                still_pending_indices.append(i)
+        if len(pending_indices) == len(still_pending_indices):
+            return None    # no progress -- there is a cycle
+        pending_indices = still_pending_indices
+    return result

Modified: pypy/branch/blackhole-improvement/pypy/jit/codewriter/test/test_flatten.py
==============================================================================
--- pypy/branch/blackhole-improvement/pypy/jit/codewriter/test/test_flatten.py	(original)
+++ pypy/branch/blackhole-improvement/pypy/jit/codewriter/test/test_flatten.py	Wed Apr 21 14:24:50 2010
@@ -1,6 +1,6 @@
 import py
 from pypy.jit.codewriter import support
-from pypy.jit.codewriter.flatten import flatten_graph
+from pypy.jit.codewriter.flatten import flatten_graph, reorder_renaming_list
 from pypy.jit.codewriter.format import format_assembler
 
 
@@ -15,6 +15,18 @@
             self.num_colors += 1
         return self.seen[v]
 
+def test_reorder_renaming_list():
+    result = reorder_renaming_list([], [])
+    assert result == []
+    result = reorder_renaming_list([1, 2, 3], [4, 5, 6])
+    assert result == [(1, 4), (2, 5), (3, 6)]
+    result = reorder_renaming_list([4, 5, 1, 2], [1, 2, 3, 4])
+    assert result == [(1, 3), (4, 1), (2, 4), (5, 2)]
+    result = reorder_renaming_list([1, 2], [2, 1])
+    assert result == None
+    result = reorder_renaming_list([4, 3, 1, 2, 6], [1, 2, 3, 4, 5])
+    assert result == None
+
 
 class TestFlatten:
 
@@ -46,14 +58,17 @@
                 a -= 1
             return b
         self.encoding_test(f, [5, 6], """
-            int_rename [%i0, %i1], [%i2, %i3]
+            int_copy %i0, %i2
+            int_copy %i1, %i3
             L1:
             int_gt %i2, $0, %i4
             goto_if_not L2, %i4
-            int_rename [%i2, %i3], [%i5, %i6]
+            int_copy %i2, %i5
+            int_copy %i3, %i6
             int_add %i6, %i5, %i7
             int_sub %i5, $1, %i8
-            int_rename [%i8, %i7], [%i2, %i3]
+            int_copy %i8, %i2
+            int_copy %i7, %i3
             goto L1
             L2:
             int_return %i3

Modified: pypy/branch/blackhole-improvement/pypy/jit/codewriter/test/test_regalloc.py
==============================================================================
--- pypy/branch/blackhole-improvement/pypy/jit/codewriter/test/test_regalloc.py	(original)
+++ pypy/branch/blackhole-improvement/pypy/jit/codewriter/test/test_regalloc.py	Wed Apr 21 14:24:50 2010
@@ -85,7 +85,8 @@
             L1:
             int_gt %i0, $0, %i0
             goto_if_not L2, %i0
-            int_rename [%i1, $2], [%i0, %i1]
+            int_copy %i1, %i0
+            int_copy $2, %i1
             goto L1
             L2:
             int_return %i1
@@ -117,7 +118,7 @@
             L1:
             int_gt %i0, $0, %i3
             goto_if_not L2, %i3
-            int_rename [%i2], [%i1]
+            int_copy %i2, %i1
             goto L1
             L2:
             int_return %i1



More information about the Pypy-commit mailing list