Commits

Hakan Ardo committed 290f86a

better handling of constants

Comments (0)

Files changed (5)

pypy/jit/metainterp/optimizeopt/heap.py

 from pypy.rlib.objectmodel import we_are_translated
 from pypy.jit.metainterp.jitexc import JitException
 from pypy.jit.metainterp.optimizeopt.optimizer import Optimization
-from pypy.jit.metainterp.history import ConstInt
+from pypy.jit.metainterp.history import ConstInt, Const
 
 class CachedField(object):
     def __init__(self):
         for structvalue, op in self._cached_fields_getfield_op.iteritems():
             if op and structvalue in self._cached_fields:
                 potential_ops[op.result] = op
-                    
 
 class CachedArrayItems(object):
     def __init__(self):
         if value is not newvalue:
             for cf in self.cached_fields.itervalues():
                 if value in cf._cached_fields:
-                    cf._cached_fields[newvalue] = cf._cached_fields[value]
+                    if newvalue not in cf._cached_fields:
+                        cf._cached_fields[newvalue] = cf._cached_fields[value]
+                        op = cf._cached_fields_getfield_op[value].clone()
+                        constbox = value.box
+                        assert isinstance(constbox, Const)
+                        op.setarg(0, constbox)
+                        cf._cached_fields_getfield_op[newvalue] = op
+                                
 
     def force_lazy_setfield(self, descr):
         try:

pypy/jit/metainterp/optimizeopt/optimizer.py

         for opt in self.optimizations:
             opt.produce_potential_short_preamble_ops(potential_ops)
 
-    def produce_short_preamble_ops(self, inputargs):
+    def produce_short_preamble_ops(self, surviving_boxes):
         potential_ops = {}
         self.produce_potential_short_preamble_ops(potential_ops)
             
         short_boxes = {}
-        for box in inputargs:
+        for box in surviving_boxes:
             short_boxes[box] = None
 
         for box in potential_ops.keys():

pypy/jit/metainterp/optimizeopt/unroll.py

             values = [self.getvalue(arg) for arg in jump_args]
             inputargs = virtual_state.make_inputargs(values)
 
-            sb = self.optimizer.produce_short_preamble_ops(inputargs)
+            self.constant_inputargs = {}
+            for box in jump_args: 
+                const = self.get_constant_box(box)
+                if const:
+                    self.constant_inputargs[box] = const
+
+            sb = self.optimizer.produce_short_preamble_ops(inputargs +
+                                                 self.constant_inputargs.keys())
             self.short_boxes = sb
             preamble_optimizer = self.optimizer
             loop.preamble.quasi_immutable_deps = (
                 self.optimizer.quasi_immutable_deps)
             self.optimizer = self.optimizer.reconstruct_for_next_iteration(sb, jump_args)
-            
-            self.constant_inputargs = {}
-            loop.quasi_immutable_deps = self.optimizer.quasi_immutable_deps            
-            for box in jump_args:
-                const = self.get_constant_box(box)
-                if const:
-                    self.constant_inputargs[box] = const
+            loop.quasi_immutable_deps = self.optimizer.quasi_immutable_deps
         
             initial_inputargs_len = len(inputargs)
             self.inliner = Inliner(loop.inputargs, jump_args)

pypy/jit/metainterp/optimizeopt/virtualstate.py

                     assert not isinstance(box, Const)
                     inputargs.append(box)
 
-        if not we_are_translated():
-            assert len(set(inputargs)) == len(inputargs)
         assert None not in inputargs
             
         return inputargs
-        
+
 
 class VirtualStateAdder(resume.ResumeDataVirtualAdder):
     def __init__(self, optimizer):

pypy/jit/metainterp/test/test_optimizeopt.py

         """
         self.optimize_loop(ops, expected)
 
-    def test_constant_getfield(self):
+    def test_constant_getfield1(self):
         ops = """
         [p1, p187, i184]
         p188 = getarrayitem_gc(p187, i184, descr=<GcPtrArrayDescr>)
         """
         self.optimize_loop(ops, expected, ops)
         # FIXME: check jumparg 0 == getfield_gc()
+
+    def test_constant_getfield2(self):
+        ops = """
+        [p19]
+        p22 = getfield_gc(p19, descr=otherdescr)
+        guard_value(p19, ConstPtr(myptr)) []
+        jump(p19)
+        """
+        expected = """
+        []
+        jump()
+        """
+        self.optimize_loop(ops, expected)
+
+    def test_constant_getfield3(self):
+        ops = """
+        [p19, p20, p21]
+        p22 = getfield_gc(p19, descr=otherdescr)
+        guard_value(p19, ConstPtr(myptr)) []
+        p23 = getfield_gc(ConstPtr(myptr), descr=otherdescr)
+        jump(p20, p21, p21)
+        """
+        expected = """
+        [p20, p21]
+        p22 = getfield_gc(p20, descr=otherdescr)
+        guard_value(p20, ConstPtr(myptr)) []
+        jump(p21, p21)
+        """
+        self.optimize_loop(ops, expected)
+
+    def test_constant_getfield4(self):
+        ops = """
+        [p19, p20, p21]
+        p22 = getfield_gc(p19, descr=otherdescr)
+        p23 = getfield_gc(ConstPtr(myptr), descr=otherdescr)
+        guard_value(p19, ConstPtr(myptr)) []
+        jump(p20, p21, p21)
+        """
+        expected = """
+        [p20, p21]
+        p22 = getfield_gc(p20, descr=otherdescr)
+        guard_value(p20, ConstPtr(myptr)) []
+        jump(p21, p21)
+        """
+        self.optimize_loop(ops, expected)
+
+    def test_constnats_among_virtual_fileds(self):
+        ops = """
+        [p19, p20, p21]
+        p1 = getfield_gc(p20, descr=valuedescr)
+        p2 = getfield_gc(p1, descr=otherdescr)
+        pv = new_with_vtable(ConstClass(node_vtable))
+        setfield_gc(pv, p19, descr=valuedescr)
+        p22 = getfield_gc(p19, descr=otherdescr)
+        guard_value(p19, ConstPtr(myptr)) []
+        p23 = getfield_gc(ConstPtr(myptr), descr=otherdescr)
+        jump(p21, pv, p21)
+        """
+        expected = """
+        [p20]
+        p22 = getfield_gc(p20, descr=otherdescr)
+        guard_value(p20, ConstPtr(myptr)) []
+        jump(ConstPtr(myptr))
+        """
+        self.optimize_loop(ops, expected)