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

arigo at codespeak.net arigo at codespeak.net
Thu Jun 29 20:40:41 CEST 2006


Author: arigo
Date: Thu Jun 29 20:40:39 2006
New Revision: 29510

Modified:
   pypy/dist/pypy/translator/backendopt/constfold.py
   pypy/dist/pypy/translator/backendopt/test/test_constfold.py
Log:
(arigo, bugged by pedronis)

Keepalive fix in constfold.py.



Modified: pypy/dist/pypy/translator/backendopt/constfold.py
==============================================================================
--- pypy/dist/pypy/translator/backendopt/constfold.py	(original)
+++ pypy/dist/pypy/translator/backendopt/constfold.py	Thu Jun 29 20:40:39 2006
@@ -12,6 +12,7 @@
     newops = []
     keepalives = []
     folded_count = 0
+    first_sideeffect_index = None
     for spaceop in operations:
         vargsmodif = False
         vargs = []
@@ -24,13 +25,13 @@
                 vargsmodif = True
                 args.append(v.value)
             vargs.append(v)
-        if len(args) == len(vargs):
-            RESTYPE = spaceop.result.concretetype
-            try:
-                op = getattr(llop, spaceop.opname)
-            except AttributeError:
-                pass
-            else:
+        try:
+            op = getattr(llop, spaceop.opname)
+        except AttributeError:
+            sideeffects = True
+        else:
+            if len(args) == len(vargs):
+                RESTYPE = spaceop.result.concretetype
                 try:
                     result = op(RESTYPE, *args)
                 except TypeError:
@@ -45,10 +46,11 @@
                     constants[spaceop.result] = Constant(result, RESTYPE)
                     folded_count += 1
                     continue
+            sideeffects = op.sideeffects
         # failed to fold an operation, exit early if requested
         if exit_early:
             return folded_count
-        if spaceop.opname == 'keepalive':
+        if spaceop.opname == 'keepalive' and first_sideeffect_index is None:
             if vargsmodif:
                 continue    # keepalive(constant) is not useful
             keepalives.append(spaceop)
@@ -61,6 +63,8 @@
                 else:
                     spaceop = SpaceOperation(spaceop.opname, vargs,
                                              spaceop.result)
+            if sideeffects and first_sideeffect_index is None:
+                first_sideeffect_index = len(newops)
             newops.append(spaceop)
     # end
     if exit_early:
@@ -68,11 +72,11 @@
     else:
         # move the keepalives to the end of the block, which makes the life
         # of prepare_constant_fold_link() easier.  Don't put them past the
-        # exception-raising operation, though.
-        if exc_catch:
-            exc_raising_op = newops.pop()
-            keepalives.append(exc_raising_op)
-        newops.extend(keepalives)
+        # exception-raising operation, though.  There is also no point in
+        # moving them past the first sideeffect-ing operation.
+        if first_sideeffect_index is None:
+            first_sideeffect_index = len(newops) - exc_catch
+        newops[first_sideeffect_index:first_sideeffect_index] = keepalives
         return newops
 
 def constant_fold_block(block):

Modified: pypy/dist/pypy/translator/backendopt/test/test_constfold.py
==============================================================================
--- pypy/dist/pypy/translator/backendopt/test/test_constfold.py	(original)
+++ pypy/dist/pypy/translator/backendopt/test/test_constfold.py	Thu Jun 29 20:40:39 2006
@@ -2,6 +2,7 @@
 from pypy.translator.translator import TranslationContext, graphof
 from pypy.rpython.llinterp import LLInterpreter
 from pypy.rpython.lltypesystem import lltype
+from pypy.rpython import objectmodel
 from pypy.translator.backendopt.constfold import constant_fold_graph
 from pypy import conftest
 
@@ -130,6 +131,19 @@
     check_graph(graph, [12], 12, t)
 
 
+def test_malloc():
+    S1 = lltype.GcStruct('S1', ('x', lltype.Signed), hints={'immutable': True})
+    def fn():
+        s = lltype.malloc(S1)
+        s.x = 12
+        objectmodel.keepalive_until_here(s)
+        return s.x
+
+    graph, t = get_graph(fn, [])
+    constant_fold_graph(graph)
+    check_graph(graph, [], 12, t)
+
+
 def xxx_test_later_along_link():
     S1 = lltype.GcStruct('S1', ('x', lltype.Signed), hints={'immutable': True})
     s1 = lltype.malloc(S1)



More information about the Pypy-commit mailing list