transforms.dead_code_eliminator: also remove dead instructions.

This commit is contained in:
whitequark 2015-12-16 23:38:55 +08:00
parent 5e38cad64c
commit 5dd1fc993e
1 changed files with 23 additions and 6 deletions

View File

@ -14,12 +14,24 @@ class DeadCodeEliminator:
self.process_function(func) self.process_function(func)
def process_function(self, func): def process_function(self, func):
for block in list(func.basic_blocks): modified = True
if not any(block.predecessors()) and block != func.entry(): while modified:
for use in set(block.uses): modified = False
if isinstance(use, ir.SetLocal): for block in list(func.basic_blocks):
use.erase() if not any(block.predecessors()) and block != func.entry():
self.remove_block(block) self.remove_block(block)
modified = True
modified = True
while modified:
modified = False
for insn in func.instructions():
if isinstance(insn, (ir.Phi, ir.Alloc, ir.GetLocal, ir.GetConstructor,
ir.GetAttr, ir.GetElem, ir.Coerce, ir.Arith,
ir.Compare, ir.Closure, ir.Select, ir.Quote)) \
and not any(insn.uses):
insn.erase()
modified = True
def remove_block(self, block): def remove_block(self, block):
# block.uses are updated while iterating # block.uses are updated while iterating
@ -28,6 +40,9 @@ class DeadCodeEliminator:
use.remove_incoming_block(block) use.remove_incoming_block(block)
if not any(use.operands): if not any(use.operands):
self.remove_instruction(use) self.remove_instruction(use)
elif isinstance(use, ir.SetLocal):
# setlocal %env, %block is only used for lowering finally
use.erase()
else: else:
assert False assert False
@ -39,5 +54,7 @@ class DeadCodeEliminator:
use.remove_incoming_value(insn) use.remove_incoming_value(insn)
if not any(use.operands): if not any(use.operands):
self.remove_instruction(use) self.remove_instruction(use)
else:
assert False
insn.erase() insn.erase()