[pypy-svn] r65911 - in pypy/branch/pyjitpl5/pypy/jit/metainterp: . test

arigo at codespeak.net arigo at codespeak.net
Wed Jun 24 16:06:23 CEST 2009


Author: arigo
Date: Wed Jun 24 16:06:21 2009
New Revision: 65911

Added:
   pypy/branch/pyjitpl5/pypy/jit/metainterp/optimize4.py
      - copied, changed from r65892, pypy/branch/pyjitpl5/pypy/jit/metainterp/optimize.py
   pypy/branch/pyjitpl5/pypy/jit/metainterp/test/test_optimize4.py
      - copied, changed from r65892, pypy/branch/pyjitpl5/pypy/jit/metainterp/test/test_optimize.py
Log:
Copy optimize.py to optimize4.py and clean it up by removing
all commented-out code about virtualizable, as well as all
code about lists.


Copied: pypy/branch/pyjitpl5/pypy/jit/metainterp/optimize4.py (from r65892, pypy/branch/pyjitpl5/pypy/jit/metainterp/optimize.py)
==============================================================================
--- pypy/branch/pyjitpl5/pypy/jit/metainterp/optimize.py	(original)
+++ pypy/branch/pyjitpl5/pypy/jit/metainterp/optimize4.py	Wed Jun 24 16:06:21 2009
@@ -3,33 +3,14 @@
                                          ResOperation, AbstractDescr,
                                          Options, AbstractValue, ConstPtr,
                                          ConstObj)
-from pypy.jit.metainterp.specnode import (FixedClassSpecNode,
-   VirtualInstanceSpecNode, VirtualizableSpecNode, NotSpecNode,
-   VirtualFixedListSpecNode, VirtualizableListSpecNode,
-   MatchEverythingSpecNode)
+from pypy.jit.metainterp.specnode import (FixedClassSpecNode, NotSpecNode,
+                                          VirtualInstanceSpecNode)
 from pypy.jit.metainterp import executor
 from pypy.rlib.objectmodel import we_are_translated
 from pypy.rpython.lltypesystem import lltype, llmemory
 from pypy.rpython.ootypesystem import ootype
 from pypy.rlib.objectmodel import r_dict
 
-class FixedList(AbstractValue):
-    def __init__(self, arraydescr):
-        self.arraydescr = arraydescr
-
-    def equals(self, other):
-        # it's all really impossible, it should default to True simply
-        assert isinstance(other, FixedList)
-        assert other.arraydescr == self.arraydescr
-        return True
-
-class FixedClass(Const):
-    def equals(self, other):
-        assert isinstance(other, FixedClass)
-        return True
-
-#class CancelInefficientLoop(Exception):
-#    pass
 
 def av_eq(self, other):
     return self.sort_key() == other.sort_key()
@@ -37,14 +18,6 @@
 def av_hash(self):
     return self.sort_key()
 
-def av_list_in(lst, key):
-    # lst is a list of about 2 elements in the typical case, so no
-    # point in making a dict lookup
-    for l in lst:
-        if key.sort_key() == l.sort_key():
-            return True
-    return False
-
 class InstanceNode(object):
     def __init__(self, source, escaped=True, startbox=False, const=False):
         if isinstance(source, Const):
@@ -54,21 +27,15 @@
         self.startbox = startbox
         self.virtual = False
         self.const = const
-        self.nonzero = False     # NB. never set to True so far
         self.cls = None
         self.origfields = r_dict(av_eq, av_hash)
         self.curfields = r_dict(av_eq, av_hash)
-        #self.cleanfields = r_dict(av_eq, av_hash)
-        #self.dirtyfields = r_dict(av_eq, av_hash)
-        #self.expanded_fields = r_dict(av_eq, av_hash)
-        self.cursize = -1
-        self.allfields = None
 
     def is_nonzero(self):
-        return self.cls is not None or self.nonzero
+        return self.cls is not None or (self.const and self.source.get_() != 0)
 
     def is_zero(self):
-        return self.const and not self.source.getptr_base()
+        return self.const and self.source.get_() == 0
 
     def escape_if_startbox(self, memo, cpu):
         if self in memo:
@@ -76,66 +43,29 @@
         memo[self] = None
         if self.startbox:
             self.escaped = True
-        if 1: ## not self.virtualized:
-            for node in self.curfields.values():
-                node.escape_if_startbox(memo, cpu)
-##        else:
-##            for key, node in self.curfields.items():
-##                if self.vdesc is not None and av_list_in(self.vdesc, key):
-##                    node.initialize_virtualizable(cpu)
-##                node.escape_if_startbox(memo, cpu)
-##            # we also need to escape fields that are only read, never written,
-##            # if they're not marked specifically as ones that does not escape
-##            for key, node in self.origfields.items():
-##                if key not in self.curfields:
-##                    if self.vdesc is not None and av_list_in(self.vdesc, key):
-##                        node.initialize_virtualizable(cpu)
-##                    node.escape_if_startbox(memo, cpu)
-    
-##    def initialize_virtualizable(self, cpu):
-##        self.virtualized = True
-##        if self.cls is None or not isinstance(self.cls.source, FixedList):
-##            # XXX this is of course wrong, but let's at least not
-##            #     explode
-##            self.allfields = self.origfields.keys()
-##            if self.cls is None:
-##                self.cls = InstanceNode(FixedClass(), const=True)
-##        else:
-##            fx = self.cls.source
-##            assert isinstance(fx, FixedList)
-##            ad = fx.arraydescr
-##            lgtbox = cpu.do_arraylen_gc([self.source], ad)
-##            self.allfields = [ConstInt(i) for i in range(lgtbox.getint())]
+        for node in self.curfields.values():
+            node.escape_if_startbox(memo, cpu)
 
     def add_to_dependency_graph(self, other, dep_graph):
         dep_graph.append((self, other))
         for ofs, node in self.origfields.items():
             if ofs in other.curfields:
                 node.add_to_dependency_graph(other.curfields[ofs], dep_graph)
-##            if (self.virtualized and self.vdesc is not None and
-##                av_list_in(self.vdesc, ofs)):
-##                node.add_to_dependency_graph(other.origfields[ofs], dep_graph)
 
     def intersect(self, other, nodes):
         if not other.cls:
             return NotSpecNode()
         if self.cls:
             if not self.cls.source.equals(other.cls.source):
-                #raise CancelInefficientLoop
                 return NotSpecNode()
             known_class = self.cls.source
         else:
             known_class = other.cls.source
-        if (other.escaped): ## and not other.virtualized):# and
-            #not self.expanded_fields):
-            ## assert not self.virtualized
+        if other.escaped:
             if self.cls is None:
                 return NotSpecNode()
-            if isinstance(known_class, FixedList):
-                return NotSpecNode()
             return FixedClassSpecNode(known_class)
         else:
-##        if not other.escaped:
             assert self is not other
             fields = []
             d = other.curfields
@@ -150,45 +80,7 @@
                     nodes[box] = self.origfields[ofs]
                 specnode = self.origfields[ofs].intersect(node, nodes)
                 fields.append((ofs, specnode))
-            if isinstance(known_class, FixedList):
-                return VirtualFixedListSpecNode(known_class, fields,
-                                                other.cursize)
             return VirtualInstanceSpecNode(known_class, fields)
-        #if not other.virtualized and self.expanded_fields:
-        #    fields = []
-        #    lst = self.expanded_fields.keys()
-        #    sort_descrs(lst)
-        #    for ofs in lst:
-        #        specnode = SpecNodeWithBox(self.origfields[ofs].source)
-        #        fields.append((ofs, specnode))
-        #    if isinstance(known_class, FixedList):
-        #        return DelayedFixedListSpecNode(known_class, fields)
-        #    return DelayedSpecNode(known_class, fields)
-##        assert other.virtualized
-##        assert self is other
-##        offsets = self.allfields
-##        if offsets is None:
-##            return None
-##        sort_descrs(offsets)
-##        fields = []
-##        for ofs in offsets:
-##            if ofs in other.curfields:
-##                node = other.curfields[ofs]
-##                if ofs not in self.origfields:
-##                    box = node.source.clonebox()
-##                    self.origfields[ofs] = InstanceNode(box, escaped=False)
-##                    self.origfields[ofs].cls = node.cls
-##                    nodes[box] = self.origfields[ofs]
-##                specnode = self.origfields[ofs].intersect(node, nodes)
-##            elif ofs in self.origfields:
-##                node = self.origfields[ofs]
-##                specnode = node.intersect(node, nodes)
-##            else:
-##                specnode = MatchEverythingSpecNode()
-##            fields.append((ofs, specnode))
-##        if isinstance(known_class, FixedList):
-##            return VirtualizableListSpecNode(known_class, fields)
-##        return VirtualizableSpecNode(known_class, fields)
 
     def __repr__(self):
         flags = ''
@@ -196,7 +88,6 @@
         if self.startbox:          flags += 's'
         if self.const:             flags += 'c'
         if self.virtual:           flags += 'v'
-##        if self.virtualized:       flags += 'V'
         return "<InstanceNode %s (%s)>" % (self.source, flags)
 
 def optimize_loop(options, old_loops, loop, cpu=None):
@@ -280,25 +171,9 @@
             self.dependency_graph.append((instnode, fieldnode))
             instnode.origfields[field] = fieldnode
         self.nodes[box] = fieldnode
-        #if (self.first_escaping_op and instnode.cls):
-        #    instnode.expanded_fields[field] = None
 
-    def find_nodes_getarrayitem(self, op):
-        instnode = self.getnode(op.args[0])
-        if instnode.cls is None:
-            instnode.cls = InstanceNode(FixedList(op.descr))
-        fieldbox = op.args[1]
-        if self.getnode(fieldbox).const:
-            item = self.getsource(fieldbox)
-            self.find_nodes_getfield(instnode, item, op.result)
-        else:
-            instnode.escaped = True
-            self.nodes[op.result] = InstanceNode(op.result,
-                                                 escaped=True)
-        
     def find_nodes(self):
         # Steps (1) and (2)
-        self.first_escaping_op = True
         if self.loop.inputargs is not None:
             for box in self.loop.inputargs:
                 self.nodes[box] = InstanceNode(box, escaped=False,
@@ -316,39 +191,6 @@
                 instnode = InstanceNode(box, escaped=False)
                 instnode.cls = InstanceNode(op.args[0], const=True)
                 self.nodes[box] = instnode
-                self.first_escaping_op = False
-                continue
-            elif opnum == rop.NEW_ARRAY:
-                box = op.result
-                instnode = InstanceNode(box, escaped=False)
-                instnode.cls = InstanceNode(FixedList(op.descr))
-                self.nodes[box] = instnode
-                if self.getnode(op.args[0]).const:
-                    instnode.cursize = op.args[0].getint()
-                else:
-                    instnode.escaped = True
-                continue
-            elif opnum == rop.GETARRAYITEM_GC:
-                self.find_nodes_getarrayitem(op)
-                continue
-            elif opnum == rop.GETARRAYITEM_GC_PURE:
-                instnode = self.getnode(op.args[0])
-                if not instnode.const or not self.getnode(op.args[1]).const:
-                    self.find_nodes_getarrayitem(op)
-                    continue
-            elif opnum == rop.SETARRAYITEM_GC:
-                instnode = self.getnode(op.args[0])
-                if instnode.cls is None:
-                    instnode.cls = InstanceNode(FixedList(op.descr))
-                fieldbox = op.args[1]
-                if self.getnode(fieldbox).const:
-                    item = self.getsource(fieldbox)
-                    self.find_nodes_setfield(instnode, item,
-                                             self.getnode(op.args[2]))
-                else:
-                    instnode.escaped = True
-                    self.dependency_graph.append((instnode,
-                                                 self.getnode(op.args[2])))
                 continue
             elif opnum == rop.SETFIELD_GC:
                 instnode = self.getnode(op.args[0])
@@ -382,16 +224,6 @@
                 #self.nodes[instnode.source] = InstanceNode(op.args[1],
                 #                                           const=True)
                 continue
-##            elif opnum == rop.GUARD_NONVIRTUALIZED:
-##                instnode = self.getnode(op.args[0])
-##                if instnode.startbox:
-##                    instnode.virtualized = True
-##                if instnode.cls is None:
-##                    instnode.cls = InstanceNode(op.args[1], const=True)
-##                    if op.vdesc:
-##                        instnode.vdesc = op.vdesc.virtuals
-##                        instnode.allfields = op.vdesc.fields
-##                continue
             elif op.is_always_pure():
                 is_pure = True
                 for arg in op.args:
@@ -406,7 +238,6 @@
                     continue
             elif not op.has_no_side_effect():
                 # default case
-                self.first_escaping_op = False
                 for box in op.args:
                     if isinstance(box, Box):
                         self.getnode(box).escaped = True
@@ -475,12 +306,7 @@
             return box
         if instnode.virtual:
             ld = instnode.cls.source
-            if isinstance(ld, FixedList):
-                ad = ld.arraydescr
-                sizebox = ConstInt(instnode.cursize)
-                op = ResOperation(rop.NEW_ARRAY, [sizebox], box,
-                                  descr=ad)
-            elif self.cpu.is_oo and isinstance(ld, ConstObj):
+            if self.cpu.is_oo and isinstance(ld, ConstObj):
                 # it's probably a ootype new
                 cls = ld.getobj()
                 typedescr = self.cpu.class_sizes[cls] # XXX this is probably not rpython
@@ -500,31 +326,12 @@
             memo[box] = None
             for ofs, node in instnode.curfields.items():
                 fieldbox = self.prepare_rebuild_ops(node, rebuild_ops, memo)
-                if isinstance(ld, FixedList):
-                    op = ResOperation(rop.SETARRAYITEM_GC,
-                                      [box, ofs, fieldbox],
-                                      None, descr=ld.arraydescr)
-                else:
-                    assert isinstance(ofs, AbstractDescr)
-                    op = ResOperation(rop.SETFIELD_GC, [box, fieldbox],
-                                      None, descr=ofs)
+                assert isinstance(ofs, AbstractDescr)
+                op = ResOperation(rop.SETFIELD_GC, [box, fieldbox],
+                                  None, descr=ofs)
                 rebuild_ops.append(op)
             return box
         memo[box] = None
-##        if instnode.virtualized:
-##            for ofs, node in instnode.curfields.items():
-##                fieldbox = self.prepare_rebuild_ops(node, rebuild_ops, memo)
-##                if instnode.cls and isinstance(instnode.cls.source, FixedList):
-##                    ld = instnode.cls.source
-##                    assert isinstance(ld, FixedList)
-##                    op = ResOperation(rop.SETARRAYITEM_GC,
-##                                      [box, ofs, fieldbox],
-##                                      None, descr=ld.arraydescr)
-##                else:
-##                    assert isinstance(ofs, AbstractDescr)
-##                    op = ResOperation(rop.SETFIELD_GC, [box, fieldbox],
-##                                      None, descr=ofs)
-##                rebuild_ops.append(op)
         return box
 
     def optimize_guard(self, op):
@@ -538,37 +345,6 @@
             if isinstance(box, Const) or box not in self.nodes:
                 continue
             self.prepare_rebuild_ops(self.nodes[box], rebuild_ops, memo, box)
-        # XXX sloooooow!
-##        for node in self.nodes.values():
-##            if node.virtualized:
-##                self.prepare_rebuild_ops(node, rebuild_ops, memo)
-
-#         # start of code for dirtyfields support
-#         for node in self.nodes.values():
-#             for ofs, subnode in node.dirtyfields.items():
-#                 box = node.source
-#                 if box not in memo and isinstance(box, Box):
-#                     memo[box] = box
-#                 #index = (rev_boxes[box] << FLAG_SHIFT) | FLAG_BOXES_FROM_FRAME
-#                 fieldbox = subnode.source
-#                 if fieldbox not in memo and isinstance(fieldbox, Box):
-#                     memo[fieldbox] = fieldbox
-#                 #fieldindex = ((rev_boxes[fieldbox] << FLAG_SHIFT) |
-#                 #              FLAG_BOXES_FROM_FRAME)
-#                 if (node.cls is not None and
-#                     isinstance(node.cls.source, FixedList)):
-#                     ld = node.cls.source
-#                     assert isinstance(ld, FixedList)
-#                     ad = ld.arraydescr
-#                     op1 = ResOperation(rop.SETARRAYITEM_GC,
-#                                        [box, ofs, fieldbox],
-#                                        None, descr=ad)
-#                 else:
-#                     assert isinstance(ofs, AbstractDescr)
-#                     op1 = ResOperation(rop.SETFIELD_GC, [box, fieldbox],
-#                                        None, descr=ofs)
-#                 rebuild_ops.append(op1)
-#         # end of code for dirtyfields support
 
         newboxes = [self.nodes[arg].source for arg in op_fail.args]
         op_fail.args = newboxes
@@ -598,21 +374,6 @@
         if instnode.virtual:
             assert ofs in instnode.curfields
             return True # this means field is never actually
-##        elif instnode.virtualized:
-##            if ofs in instnode.curfields:
-##                return True
-##            # this means field comes from a virtualizable but is never
-##            # written.
-##            node = InstanceNode(box)
-##            self.nodes[box] = node
-##            instnode.curfields[ofs] = node
-##            return True
-        #if ofs in instnode.cleanfields:
-        #    self.nodes[box] = instnode.cleanfields[ofs]
-        #    return True
-        #else:
-        #    instnode.cleanfields[ofs] = InstanceNode(box)
-        #    return False
         return False
 
     def optimize_setfield(self, instnode, ofs, valuenode, valuebox):
@@ -623,8 +384,6 @@
         else:
             assert not valuenode.virtual
             return False
-            #instnode.cleanfields[ofs] = self.nodes[valuebox]
-            #instnode.dirtyfields[ofs] = self.nodes[valuebox]
             # we never perform this operation here, note
 
     def optimize_loop(self):
@@ -649,7 +408,6 @@
                 for arg in args:
                     if arg in self.nodes:
                         assert not self.nodes[arg].virtual
-                #self.cleanup_field_caches(newoperations)
                 op = op.clone()
                 op.args = args
                 newoperations.append(op)
@@ -686,12 +444,6 @@
                 instnode.const = True
                 newoperations.append(self.optimize_guard(op))
                 continue
-##            elif opnum == rop.GUARD_NONVIRTUALIZED:
-##                instnode = self.nodes[op.args[0]]
-##                if instnode.virtualized or instnode.virtual:
-##                    continue
-##                newoperations.append(self.optimize_guard(op))
-##                continue
             elif opnum == rop.GETFIELD_GC:
                 instnode = self.nodes[op.args[0]]
                 if self.optimize_getfield(instnode, op.descr, op.result):
@@ -703,19 +455,6 @@
                 if not instnode.const:
                     if self.optimize_getfield(instnode, op.descr, op.result):
                         continue
-            elif opnum == rop.GETARRAYITEM_GC:
-                instnode = self.nodes[op.args[0]]
-                ofsbox = self.getsource(op.args[1])
-                if isinstance(ofsbox, ConstInt):
-                    if self.optimize_getfield(instnode, ofsbox, op.result):
-                        continue
-            elif opnum == rop.GETARRAYITEM_GC_PURE:
-                instnode = self.nodes[op.args[0]]
-                ofsbox = self.getsource(op.args[1])
-                if not instnode.const:
-                    if isinstance(ofsbox, ConstInt):
-                        if self.optimize_getfield(instnode, ofsbox, op.result):
-                            continue
             elif opnum == rop.NEW_WITH_VTABLE:
                 # self.nodes[op.result] keeps the value from Steps (1,2)
                 instnode = self.nodes[op.result]
@@ -723,35 +462,18 @@
                     instnode.virtual = True
                     assert instnode.cls is not None
                     continue
-            elif opnum == rop.NEW_ARRAY:
-                instnode = self.nodes[op.result]
-                if not instnode.escaped:
-                    instnode.virtual = True
-                    instnode.cursize = op.args[0].getint()
-                    continue
             elif opnum == rop.SETFIELD_GC:
                 instnode = self.nodes[op.args[0]]
                 valuenode = self.nodes[op.args[1]]
                 ofs = op.descr
                 if self.optimize_setfield(instnode, ofs, valuenode, op.args[1]):
                     continue
-            elif opnum == rop.SETARRAYITEM_GC:
-                instnode = self.nodes[op.args[0]]
-                if instnode.cls is None:
-                    instnode.cls = InstanceNode(FixedList(op.descr))
-                ofsbox = self.getsource(op.args[1])
-                if isinstance(ofsbox, ConstInt):
-                    valuenode = self.getnode(op.args[2])
-                    if self.optimize_setfield(instnode, ofsbox, valuenode,
-                                              op.args[2]):
-                        continue
             elif (opnum == rop.OOISNULL or
                   opnum == rop.OONONNULL):
                 instnode = self.getnode(op.args[0])
                 # we know the result is constant if instnode is a virtual,
-                # a constant, or known to be non-zero.
-                # XXX what about virtualizables?
-                if instnode.virtual or instnode.const or instnode.is_nonzero():
+                # or known to be non-zero.
+                if instnode.virtual or instnode.is_nonzero():
                     box = op.result
                     instnode = InstanceNode(box.constbox(), const=True)
                     self.nodes[box] = instnode
@@ -788,12 +510,6 @@
                     instnode = InstanceNode(box.constbox(), const=True)
                     self.nodes[box] = instnode
                     continue
-            #elif (not op.has_no_side_effect()
-            #      and opnum != rop.SETFIELD_GC and
-            #      opnum != rop.SETARRAYITEM_GC):
-                # the setfield operations do not clean up caches, although
-                # they have side effects
-                #self.cleanup_field_caches(newoperations)
             if op.can_raise():
                 exception_might_have_happened = True
             box = op.result
@@ -806,27 +522,6 @@
         self.loop.inputargs = newinputargs
         self.loop.operations = newoperations
 
-#     def cleanup_field_caches(self, newoperations):
-#         # we need to invalidate everything
-#         # XXX looping over self.nodes on each side-effecting operation
-#         # XXX costs too much (quadratic time)
-#         for node in self.nodes.values():
-#             for ofs, valuenode in node.dirtyfields.items():
-#                 # XXX move to InstanceNode eventually
-#                 if (node.cls is not None and
-#                     isinstance(node.cls.source, FixedList)):
-#                     ld = node.cls.source
-#                     assert isinstance(ld, FixedList)
-#                     newoperations.append(ResOperation(rop.SETARRAYITEM_GC,
-#                                           [node.source, ofs, valuenode.source],
-#                                                       None, ld.arraydescr))
-#                 else:
-#                     assert isinstance(ofs, AbstractDescr)
-#                     newoperations.append(ResOperation(rop.SETFIELD_GC,
-#                        [node.source, valuenode.source], None, ofs))
-#             #node.dirtyfields = r_dict(av_eq, av_hash)
-#             #node.cleanfields = r_dict(av_eq, av_hash)
-
     def match_exactly(self, old_loop):
         assert len(old_loop.specnodes) == len(self.specnodes)
         for i in range(len(self.specnodes)):
@@ -918,14 +613,6 @@
         loop.inputargs = self._patch(loop.inputargs, new_inputargs)
         self._patch_loop(loop.operations, new_inputargs, rebuild_ops, loop)
 
-def get_in_list(dict, boxes_or_consts):
-    result = []
-    for box in boxes_or_consts:
-        if isinstance(box, Box):
-            box = dict[box]
-        result.append(box)
-    return result
-
 # ---------------------------------------------------------------
 
 def partition(array, left, right):

Copied: pypy/branch/pyjitpl5/pypy/jit/metainterp/test/test_optimize4.py (from r65892, pypy/branch/pyjitpl5/pypy/jit/metainterp/test/test_optimize.py)
==============================================================================
--- pypy/branch/pyjitpl5/pypy/jit/metainterp/test/test_optimize.py	(original)
+++ pypy/branch/pyjitpl5/pypy/jit/metainterp/test/test_optimize4.py	Wed Jun 24 16:06:21 2009
@@ -9,7 +9,7 @@
 from pypy.jit.metainterp.resoperation import rop
 from pypy.jit.metainterp.history import (BoxInt, BoxPtr, ConstInt, ConstPtr,
                                          ConstAddr, TreeLoop)
-from pypy.jit.metainterp.optimize import (PerfectSpecializer,
+from pypy.jit.metainterp.optimize4 import (PerfectSpecializer,
     VirtualInstanceSpecNode, FixedClassSpecNode,
     NotSpecNode)
 



More information about the Pypy-commit mailing list