Commit 8e3915f6 authored by Stefan Behnel's avatar Stefan Behnel

code cleanup for if-const dead code removal

parent e80d0678
...@@ -2582,23 +2582,21 @@ class ConstantFolding(Visitor.VisitorTransform, SkipDeclarations): ...@@ -2582,23 +2582,21 @@ class ConstantFolding(Visitor.VisitorTransform, SkipDeclarations):
self.visitchildren(node) self.visitchildren(node)
# eliminate dead code based on constant condition results # eliminate dead code based on constant condition results
if_clauses = [] if_clauses = []
condition_result = None
for if_clause in node.if_clauses: for if_clause in node.if_clauses:
condition_result = if_clause.get_constant_condition_result() condition_result = if_clause.get_constant_condition_result()
# condition_result is True, False or None (unknown) if condition_result is None:
if condition_result != False: # unknown result => normal runtime evaluation
if_clauses.append(if_clause) if_clauses.append(if_clause)
if condition_result == True: elif condition_result == True:
# other conditions can no longer apply # subsequent clauses can safely be dropped
node.else_clause = None node.else_clause = if_clause.body
break break
else:
assert condition_result == False
if not if_clauses: if not if_clauses:
return node.else_clause # if None, deletes the node completely return node.else_clause
elif len(if_clauses) == 1 and condition_result == True: node.if_clauses = if_clauses
return if_clauses[0].body return node
else:
node.if_clauses = if_clauses
return node
# in the future, other nodes can have their own handler method here # in the future, other nodes can have their own handler method here
# that can replace them with a constant result node # that can replace them with a constant result node
......
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