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

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

parent 6d2ccb85
...@@ -3871,18 +3871,36 @@ class IfStatNode(StatNode): ...@@ -3871,18 +3871,36 @@ class IfStatNode(StatNode):
if_clause.analyse_expressions(env) if_clause.analyse_expressions(env)
if self.else_clause: if self.else_clause:
self.else_clause.analyse_expressions(env) 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): def generate_execution_code(self, code):
code.mark_pos(self.pos) code.mark_pos(self.pos)
end_label = code.new_label() if self.if_clauses:
for if_clause in self.if_clauses: end_label = code.new_label()
if_clause.generate_execution_code(code, end_label) for if_clause in self.if_clauses:
if self.else_clause: if_clause.generate_execution_code(code, end_label)
code.putln("/*else*/ {") if self.else_clause:
code.putln("/*else*/ {")
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) self.else_clause.generate_execution_code(code)
code.putln("}")
code.put_label(end_label)
def annotate(self, code): def annotate(self, code):
for if_clause in self.if_clauses: for if_clause in self.if_clauses:
if_clause.annotate(code) if_clause.annotate(code)
...@@ -3909,7 +3927,13 @@ class IfClauseNode(Node): ...@@ -3909,7 +3927,13 @@ class IfClauseNode(Node):
self.condition = \ self.condition = \
self.condition.analyse_temp_boolean_expression(env) self.condition.analyse_temp_boolean_expression(env)
self.body.analyse_expressions(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): def generate_execution_code(self, code, end_label):
self.condition.generate_evaluation_code(code) self.condition.generate_evaluation_code(code)
code.putln( 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