Commit 491aa659 authored by Stefan Behnel's avatar Stefan Behnel

avoid generating code for 'if' conditions that are known to be False

parent 6d2ccb85
......@@ -3872,8 +3872,24 @@ class IfStatNode(StatNode):
if self.else_clause:
self.else_clause.analyse_expressions(env)
# eliminate dead code based on constant condition results
if_clauses = []
condition_result = None
for if_clause in self.if_clauses:
condition_result = if_clause.get_constant_condition_result()
if condition_result != False:
if_clauses.append(if_clause)
if condition_result == True:
# other conditions can no longer apply
self.else_clause = None
break
self.if_clauses = if_clauses
# FIXME: if only one active code body is left here, we can
# replace the whole node
def generate_execution_code(self, code):
code.mark_pos(self.pos)
if self.if_clauses:
end_label = code.new_label()
for if_clause in self.if_clauses:
if_clause.generate_execution_code(code, end_label)
......@@ -3882,6 +3898,8 @@ class IfStatNode(StatNode):
self.else_clause.generate_execution_code(code)
code.putln("}")
code.put_label(end_label)
elif self.else_clause:
self.else_clause.generate_execution_code(code)
def annotate(self, code):
for if_clause in self.if_clauses:
......@@ -3910,6 +3928,12 @@ class IfClauseNode(Node):
self.condition.analyse_temp_boolean_expression(env)
self.body.analyse_expressions(env)
def get_constant_condition_result(self):
if self.condition.has_constant_result():
return self.condition.constant_result
else:
return None
def generate_execution_code(self, code, end_label):
self.condition.generate_evaluation_code(code)
code.putln(
......
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