2
0
mirror of https://github.com/m-labs/artiq.git synced 2024-12-26 03:38:25 +08:00

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

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):
modified = True
while modified:
modified = False
for block in list(func.basic_blocks): for block in list(func.basic_blocks):
if not any(block.predecessors()) and block != func.entry(): if not any(block.predecessors()) and block != func.entry():
for use in set(block.uses):
if isinstance(use, ir.SetLocal):
use.erase()
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()