Commit 397ce8a2 authored by Stefan Behnel's avatar Stefan Behnel

constant-fold expressions generated for type inference during control flow analysis

parent d7453bd0
...@@ -96,6 +96,7 @@ cdef check_definitions(ControlFlow flow, dict compiler_directives) ...@@ -96,6 +96,7 @@ cdef check_definitions(ControlFlow flow, dict compiler_directives)
@cython.final @cython.final
cdef class ControlFlowAnalysis(CythonTransform): cdef class ControlFlowAnalysis(CythonTransform):
cdef object gv_ctx cdef object gv_ctx
cdef object constant_folder
cdef set reductions cdef set reductions
cdef list env_stack cdef list env_stack
cdef list stack cdef list stack
......
...@@ -16,6 +16,8 @@ from . import PyrexTypes ...@@ -16,6 +16,8 @@ from . import PyrexTypes
from .Visitor import TreeVisitor, CythonTransform from .Visitor import TreeVisitor, CythonTransform
from .Errors import error, warning, InternalError from .Errors import error, warning, InternalError
from .Optimize import ConstantFolding
class TypedExprNode(ExprNodes.ExprNode): class TypedExprNode(ExprNodes.ExprNode):
# Used for declaring assignments of a specified type without a known entry. # Used for declaring assignments of a specified type without a known entry.
...@@ -674,6 +676,7 @@ class ControlFlowAnalysis(CythonTransform): ...@@ -674,6 +676,7 @@ class ControlFlowAnalysis(CythonTransform):
def visit_ModuleNode(self, node): def visit_ModuleNode(self, node):
self.gv_ctx = GVContext() self.gv_ctx = GVContext()
self.constant_folder = ConstantFolding()
# Set of NameNode reductions # Set of NameNode reductions
self.reductions = set() self.reductions = set()
...@@ -830,7 +833,7 @@ class ControlFlowAnalysis(CythonTransform): ...@@ -830,7 +833,7 @@ class ControlFlowAnalysis(CythonTransform):
self.in_inplace_assignment = True self.in_inplace_assignment = True
self.visitchildren(node) self.visitchildren(node)
self.in_inplace_assignment = False self.in_inplace_assignment = False
self.mark_assignment(node.lhs, node.create_binop_node()) self.mark_assignment(node.lhs, self.constant_folder(node.create_binop_node()))
return node return node
def visit_DelStatNode(self, node): def visit_DelStatNode(self, node):
...@@ -973,12 +976,11 @@ class ControlFlowAnalysis(CythonTransform): ...@@ -973,12 +976,11 @@ class ControlFlowAnalysis(CythonTransform):
for arg in sequence.args[:2]: for arg in sequence.args[:2]:
self.mark_assignment(target, arg) self.mark_assignment(target, arg)
if len(sequence.args) > 2: if len(sequence.args) > 2:
self.mark_assignment( self.mark_assignment(target, self.constant_folder(
target,
ExprNodes.binop_node(node.pos, ExprNodes.binop_node(node.pos,
'+', '+',
sequence.args[0], sequence.args[0],
sequence.args[2])) sequence.args[2])))
if not is_special: if not is_special:
# A for-loop basically translates to subsequent calls to # A for-loop basically translates to subsequent calls to
...@@ -1077,9 +1079,8 @@ class ControlFlowAnalysis(CythonTransform): ...@@ -1077,9 +1079,8 @@ class ControlFlowAnalysis(CythonTransform):
self.flow.nextblock() self.flow.nextblock()
self.mark_assignment(node.target, node.bound1) self.mark_assignment(node.target, node.bound1)
if node.step is not None: if node.step is not None:
self.mark_assignment(node.target, self.mark_assignment(node.target, self.constant_folder(
ExprNodes.binop_node(node.pos, '+', ExprNodes.binop_node(node.pos, '+', node.bound1, node.step)))
node.bound1, node.step))
# Body block # Body block
self.flow.nextblock() self.flow.nextblock()
self._visit(node.body) self._visit(node.body)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment