Optimize.py 13.2 KB
Newer Older
1 2
import Nodes
import ExprNodes
3
import PyrexTypes
4
import Visitor
5 6 7 8 9
import Builtin
import UtilNodes
import TypeSlots
import Symtab
from StringEncoding import EncodedString
10

11 12 13 14
def unwrap_node(node):
    while isinstance(node, ExprNodes.PersistentNode):
        node = node.arg
    return node
15 16

def is_common_value(a, b):
17 18
    a = unwrap_node(a)
    b = unwrap_node(b)
19 20 21
    if isinstance(a, ExprNodes.NameNode) and isinstance(b, ExprNodes.NameNode):
        return a.name == b.name
    if isinstance(a, ExprNodes.AttributeNode) and isinstance(b, ExprNodes.AttributeNode):
22
        return not a.is_py_attr and is_common_value(a.obj, b.obj) and a.attribute == b.attribute
23 24 25
    return False


26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
class DictIterTransform(Visitor.VisitorTransform):
    """Transform a for-in-dict loop into a while loop calling PyDict_Next().
    """
    PyDict_Next_func_type = PyrexTypes.CFuncType(
        PyrexTypes.c_bint_type, [
            PyrexTypes.CFuncTypeArg("dict",  PyrexTypes.py_object_type, None),
            PyrexTypes.CFuncTypeArg("pos",   PyrexTypes.c_py_ssize_t_ptr_type, None),
            PyrexTypes.CFuncTypeArg("key",   PyrexTypes.CPtrType(PyrexTypes.py_object_type), None),
            PyrexTypes.CFuncTypeArg("value", PyrexTypes.CPtrType(PyrexTypes.py_object_type), None)
            ])

    PyDict_Next_name = EncodedString("PyDict_Next")

    PyDict_Next_entry = Symtab.Entry(
        PyDict_Next_name, PyDict_Next_name, PyDict_Next_func_type)

    def visit_ForInStatNode(self, node):
        self.visitchildren(node)
        iterator = node.iterator.sequence
        if not isinstance(iterator, ExprNodes.SimpleCallNode):
            return node
        function = iterator.function
        if not isinstance(function, ExprNodes.AttributeNode):
            return node
        if function.obj.type != Builtin.dict_type:
            return node
        dict_obj = function.obj
        method = function.attribute

        keys = values = False
        if method == 'iterkeys':
            keys = True
        elif method == 'itervalues':
            values = True
        elif method == 'iteritems':
            keys = values = True
        else:
            return node

        py_object_ptr = PyrexTypes.c_void_ptr_type

        temps = []
        pos_temp = node.iterator.counter
        pos_temp_addr = ExprNodes.AmpersandNode(
            node.pos, operand=pos_temp,
            type=PyrexTypes.c_ptr_type(PyrexTypes.c_py_ssize_t_type))
        if keys:
            temp = UtilNodes.TempHandle(py_object_ptr)
            temps.append(temp)
            key_temp = temp.ref(node.target.pos)
            key_temp_addr = ExprNodes.AmpersandNode(
                node.target.pos, operand=key_temp,
                type=PyrexTypes.c_ptr_type(py_object_ptr))
        else:
            key_temp_addr = key_temp = ExprNodes.NullNode(
                pos=node.target.pos)
        if values:
            temp = UtilNodes.TempHandle(py_object_ptr)
            temps.append(temp)
            value_temp = temp.ref(node.target.pos)
            value_temp_addr = ExprNodes.AmpersandNode(
                node.target.pos, operand=value_temp,
                type=PyrexTypes.c_ptr_type(py_object_ptr))
        else:
            value_temp_addr = value_temp = ExprNodes.NullNode(
                pos=node.target.pos)

        key_target = value_target = node.target
        tuple_target = None
        if keys and values:
            if node.target.is_sequence_constructor:
                if len(node.target.args) == 2:
                    key_target, value_target = node.target.args
                else:
                    # FIXME ...
                    return node
            else:
                tuple_target = node.target

        if keys:
            key_cast = ExprNodes.TypecastNode(
                pos = key_target.pos,
                operand = key_temp,
                type = key_target.type)
        if values:
            value_cast = ExprNodes.TypecastNode(
                pos = value_target.pos,
                operand = value_temp,
                type = value_target.type)

        if isinstance(node.body, Nodes.StatListNode):
            body = node.body
        else:
            body = Nodes.StatListNode(pos = node.body.pos,
                                      stats = [node.body])

        if tuple_target:
123 124 125 126 127 128 129 130 131
            temp = UtilNodes.TempHandle(py_object_ptr)
            temps.append(temp)
            temp_tuple = temp.ref(tuple_target.pos)
            class TempTupleNode(ExprNodes.TupleNode):
                # FIXME: remove this after result-code refactoring
                def result(self):
                    return temp_tuple.result()

            tuple_result = TempTupleNode(
132
                pos = tuple_target.pos,
133 134 135
                args = [key_cast, value_cast],
                is_temp = 1,
                type = Builtin.tuple_type,
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
                )
            body.stats.insert(0, Nodes.SingleAssignmentNode(
                    pos = tuple_target.pos,
                    lhs = tuple_target,
                    rhs = tuple_result))
        else:
            if values:
                body.stats.insert(
                    0, Nodes.SingleAssignmentNode(
                        pos = value_target.pos,
                        lhs = value_target,
                        rhs = value_cast))
            if keys:
                body.stats.insert(
                    0, Nodes.SingleAssignmentNode(
                        pos = key_target.pos,
                        lhs = key_target,
                        rhs = key_cast))

        result_code = [
            Nodes.SingleAssignmentNode(
                pos = node.pos,
                lhs = pos_temp,
                rhs = ExprNodes.IntNode(node.pos, value=0)),
            Nodes.WhileStatNode(
                pos = node.pos,
                condition = ExprNodes.SimpleCallNode(
                    pos = dict_obj.pos,
                    type = PyrexTypes.c_bint_type,
                    function = ExprNodes.NameNode(
                        pos=dict_obj.pos, name=self.PyDict_Next_name,
                        type = self.PyDict_Next_func_type,
                        entry = self.PyDict_Next_entry),
                    args = [dict_obj, pos_temp_addr,
                            key_temp_addr, value_temp_addr]
                    ),
                body = body,
                else_clause = node.else_clause
                )
            ]

        return UtilNodes.TempsBlockNode(
            node.pos, temps=temps,
            body=Nodes.StatListNode(
                pos = node.pos,
                stats = result_code
                ))

    def visit_Node(self, node):
        self.visitchildren(node)
        return node


189 190 191 192
class SwitchTransform(Visitor.VisitorTransform):
    """
    This transformation tries to turn long if statements into C switch statements. 
    The requirement is that every clause be an (or of) var == value, where the var
Robert Bradshaw's avatar
Robert Bradshaw committed
193
    is common among all clauses and both var and value are ints. 
194
    """
195 196 197 198
    def extract_conditions(self, cond):
    
        if isinstance(cond, ExprNodes.CoerceToTempNode):
            cond = cond.arg
199 200 201 202

        if isinstance(cond, ExprNodes.TypecastNode):
            cond = cond.operand
    
203 204 205 206 207 208 209
        if (isinstance(cond, ExprNodes.PrimaryCmpNode) 
                and cond.cascade is None 
                and cond.operator == '=='
                and not cond.is_python_comparison()):
            if is_common_value(cond.operand1, cond.operand1):
                if isinstance(cond.operand2, ExprNodes.ConstNode):
                    return cond.operand1, [cond.operand2]
210
                elif hasattr(cond.operand2, 'entry') and cond.operand2.entry and cond.operand2.entry.is_const:
211 212 213 214
                    return cond.operand1, [cond.operand2]
            if is_common_value(cond.operand2, cond.operand2):
                if isinstance(cond.operand1, ExprNodes.ConstNode):
                    return cond.operand2, [cond.operand1]
215
                elif hasattr(cond.operand1, 'entry') and cond.operand1.entry and cond.operand1.entry.is_const:
216 217 218 219 220 221 222 223 224 225
                    return cond.operand2, [cond.operand1]
        elif (isinstance(cond, ExprNodes.BoolBinopNode) 
                and cond.operator == 'or'):
            t1, c1 = self.extract_conditions(cond.operand1)
            t2, c2 = self.extract_conditions(cond.operand2)
            if is_common_value(t1, t2):
                return t1, c1+c2
        return None, None
        
    def visit_IfStatNode(self, node):
226
        self.visitchildren(node)
227
        common_var = None
228
        case_count = 0
229 230 231 232 233
        cases = []
        for if_clause in node.if_clauses:
            var, conditions = self.extract_conditions(if_clause.condition)
            if var is None:
                return node
234
            elif common_var is not None and not is_common_value(var, common_var):
235
                return node
Robert Bradshaw's avatar
Robert Bradshaw committed
236 237
            elif not var.type.is_int or sum([not cond.type.is_int for cond in conditions]):
                return node
238 239
            else:
                common_var = var
240
                case_count += len(conditions)
241 242 243
                cases.append(Nodes.SwitchCaseNode(pos = if_clause.pos,
                                                  conditions = conditions,
                                                  body = if_clause.body))
244 245
        if case_count < 2:
            return node
Robert Bradshaw's avatar
Robert Bradshaw committed
246 247
        
        common_var = unwrap_node(common_var)
248 249 250 251
        return Nodes.SwitchStatNode(pos = node.pos,
                                    test = common_var,
                                    cases = cases,
                                    else_clause = node.else_clause)
252 253


254 255 256 257
    def visit_Node(self, node):
        self.visitchildren(node)
        return node
                              
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276

class FlattenInListTransform(Visitor.VisitorTransform):
    """
    This transformation flattens "x in [val1, ..., valn]" into a sequential list
    of comparisons. 
    """
    
    def visit_PrimaryCmpNode(self, node):
        self.visitchildren(node)
        if node.cascade is not None:
            return node
        elif node.operator == 'in':
            conjunction = 'or'
            eq_or_neq = '=='
        elif node.operator == 'not_in':
            conjunction = 'and'
            eq_or_neq = '!='
        else:
            return node
277

278
        if isinstance(node.operand2, ExprNodes.TupleNode) or isinstance(node.operand2, ExprNodes.ListNode):
279
            args = node.operand2.args
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
            if len(args) == 0:
                return ExprNodes.BoolNode(pos = node.pos, value = node.operator == 'not_in')
            else:
                lhs = ExprNodes.PersistentNode(node.operand1, len(args))
                conds = []
                for arg in args:
                    cond = ExprNodes.PrimaryCmpNode(
                                        pos = node.pos,
                                        operand1 = lhs,
                                        operator = eq_or_neq,
                                        operand2 = arg,
                                        cascade = None)
                    conds.append(ExprNodes.TypecastNode(
                                        pos = node.pos, 
                                        operand = cond,
                                        type = PyrexTypes.c_bint_type))
                def concat(left, right):
                    return ExprNodes.BoolBinopNode(
                                        pos = node.pos, 
                                        operator = conjunction,
                                        operand1 = left,
                                        operand2 = right)
                return reduce(concat, conds)
        else:
            return node
        
    def visit_Node(self, node):
        self.visitchildren(node)
        return node
309 310


311 312 313 314 315 316 317 318 319
class FinalOptimizePhase(Visitor.CythonTransform):
    """
    This visitor handles several commuting optimizations, and is run
    just before the C code generation phase. 
    
    The optimizations currently implemented in this class are: 
        - Eliminate None assignment and refcounting for first assignment. 
        - isinstance -> typecheck for cdef types
    """
320 321 322
    def visit_SingleAssignmentNode(self, node):
        if node.first:
            lhs = node.lhs
323
            lhs.lhs_of_first_assignment = True
324 325 326 327 328
            if isinstance(lhs, ExprNodes.NameNode) and lhs.entry.type.is_pyobject:
                # Have variable initialized to 0 rather than None
                lhs.entry.init_to_none = False
                lhs.entry.init = 0
        return node
329

330 331
    def visit_SimpleCallNode(self, node):
        self.visitchildren(node)
Robert Bradshaw's avatar
Robert Bradshaw committed
332
        if node.function.type.is_cfunction and isinstance(node.function, ExprNodes.NameNode):
333 334 335 336 337
            if node.function.name == 'isinstance':
                type_arg = node.args[1]
                if type_arg.type.is_builtin_type and type_arg.type.name == 'type':
                    object_module = self.context.find_module('python_object')
                    node.function.entry = object_module.lookup('PyObject_TypeCheck')
338 339
                    if node.function.entry is None:
                        return node # only happens when there was an error earlier
340 341 342 343
                    node.function.type = node.function.entry.type
                    PyTypeObjectPtr = PyrexTypes.CPtrType(object_module.lookup('PyTypeObject').type)
                    node.args[1] = ExprNodes.CastNode(node.args[1], PyTypeObjectPtr)
        return node