[pypy-svn] r73732 - in pypy/branch/blackhole-improvement/pypy/tool/algo: . test

arigo at codespeak.net arigo at codespeak.net
Wed Apr 14 13:27:53 CEST 2010


Author: arigo
Date: Wed Apr 14 13:27:50 2010
New Revision: 73732

Modified:
   pypy/branch/blackhole-improvement/pypy/tool/algo/color.py
   pypy/branch/blackhole-improvement/pypy/tool/algo/test/test_color.py
Log:
Perfect elimination order.


Modified: pypy/branch/blackhole-improvement/pypy/tool/algo/color.py
==============================================================================
--- pypy/branch/blackhole-improvement/pypy/tool/algo/color.py	(original)
+++ pypy/branch/blackhole-improvement/pypy/tool/algo/color.py	Wed Apr 14 13:27:50 2010
@@ -1,10 +1,59 @@
+"""
+the whole algorithm is based on the following paper:
+http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.86.1578&rep=rep1&type=pdf
+
+and the source code of libFIRM (file ir/be/becopyheur.c):
+http://pp.info.uni-karlsruhe.de/firm/Main_Page
+"""
+
 
 class DependencyGraph(object):
-    """ A dependency graph for a given control flow graph (CFG).
 
-    Each variable is an node in a graph and we have an edge
-    if two variables are alive at the same point of time
+    def __init__(self):
+        self.neighbours = {}
+
+    def add_node(self, v):
+        assert v not in self.neighbours, "duplicate vertex %r" % (v,)
+        self.neighbours[v] = set()
+
+    def add_edge(self, v1, v2):
+        self.neighbours[v1].add(v2)
+        self.neighbours[v2].add(v1)
+
+    def perfect_elimination_order(self):
+        sigma = [set(self.neighbours)]
+        result = []
+        while sigma:
+            v = sigma[0].pop()
+            result.append(v)
+            newsigma = []
+            neighb = self.neighbours[v]
+            for s in sigma:
+                s1 = set()
+                s2 = set()
+                for x in s:
+                    if x in neighb:
+                        s1.add(x)
+                    else:
+                        s2.add(x)
+                if s1:
+                    newsigma.append(s1)
+                if s2:
+                    newsigma.append(s2)
+            sigma = newsigma
+        result.reverse()
+        return result
+
+
+_emptyset = frozenset()
+
+
+##class Unit(object):
+##    """An optimization unit.  Represents a phi instruction."""
+##    def __init__(self, result, args):
+##        self.result = result
+##        self.args = args
 
-    the whole algorithm is based on the following paper:
-    http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.86.1578&rep=rep1&type=pdf
-    """
+##    def optimize(self, depgraph):
+##        self.queue = []
+##        self.insert_qnode(QNode(...

Modified: pypy/branch/blackhole-improvement/pypy/tool/algo/test/test_color.py
==============================================================================
--- pypy/branch/blackhole-improvement/pypy/tool/algo/test/test_color.py	(original)
+++ pypy/branch/blackhole-improvement/pypy/tool/algo/test/test_color.py	Wed Apr 14 13:27:50 2010
@@ -1,16 +1,25 @@
-
-from pypy.translator.translator import graphof
-from pypy.annotation.annrpython import RPythonAnnotator
 from pypy.tool.algo.color import DependencyGraph
 
-def test_one():
-    def f(a, b, c):
-        d = a + b
-        e = b + c
-        f = d + e
-        return d + e + f
 
-    a = RPythonAnnotator()
-    a.build_types(f, [int, int, int])
-    graph = graphof(a.translator, f)
-    dep_graph = DependencyGraph(graph)
+def test_perfect_elimination_order():
+    dg = DependencyGraph()
+    dg.add_node('a')
+    dg.add_node('b')
+    dg.add_node('c')
+    dg.add_node('d')
+    dg.add_node('e')
+    dg.add_edge('a', 'b')
+    dg.add_edge('a', 'd')
+    dg.add_edge('d', 'b')
+    dg.add_edge('d', 'e')
+    dg.add_edge('b', 'c')
+    dg.add_edge('b', 'e')
+    dg.add_edge('e', 'c')
+    order = list(dg.perfect_elimination_order())
+    assert len(order) == 5
+    assert ''.join(order) in [
+        'adbce', 'adbec', 'adcbe', 'adceb', 'adebc', 'adecb',
+        'acbde', 'acbed', 'acdbe', 'acdeb', 'acebd', 'acedb',
+        'cebad', 'cebda', 'ceabd', 'ceadb', 'cedba', 'cedab',
+        'cabde', 'cabed', 'cadbe', 'cadeb', 'caebd', 'caedb',
+        ]



More information about the Pypy-commit mailing list