Commit 59664cd7 authored by Taddeus Kroes's avatar Taddeus Kroes

Implemented dead code elimination with liveness.

parent 620544cc
from copy import copy from copy import copy
RESERVED_REGISTERS = ['$fp', '$sp']
def is_reg_dead_after(reg, block, index):
"""Check if a register is dead after a certain point in a basic block."""
if reg in RESERVED_REGISTERS:
return False
if index < len(block) - 1:
for s in block[index + 1:]:
# If used, the previous definition is live
if s.uses(reg):
return False
# If redefined, the previous definition is dead
if s.defines(reg):
return True
# If dead within the same block, check if the register is in the block's
# live_out set
return reg not in block.live_out
def create_use_def(block): def create_use_def(block):
used = set() used = set()
defined = set() defined = set()
......
from src.statement import Statement as S
from math import log from math import log
from src.statement import Statement as S
from src.liveness import is_reg_dead_after
def reg_can_be_used_in(reg, block, start, end): def reg_can_be_used_in(reg, block, start, end):
"""Check if a register addres safely be used in a block section using local """Check if a register addres safely be used in a block section using local
...@@ -394,25 +396,41 @@ def eliminate_dead_code(block): ...@@ -394,25 +396,41 @@ def eliminate_dead_code(block):
is not used in the rest of the block, and is not in the `out' set of the is not used in the rest of the block, and is not in the `out' set of the
block. block.
""" """
# TODO: Finish
changed = False changed = False
unused = set()
for s in reversed(block): for n, s in enumerate(block):
for reg in s.get_def(): for reg in s.get_def():
if reg in unused: if is_reg_dead_after(reg, block, n):
# Statement is redefined later, so this statement is useless # Statement is redefined later, so this statement is useless
if block.debug: if block.debug:
s.stype = 'comment' s.stype = 'comment'
s.options['block'] = False s.options['block'] = False
s.name = ' Dead code: %s %s' \ s.name = ' Dead:\t%s\t%s' \
% (s.name, ', '.join(map(str, s))) % (s.name, ','.join(map(str, s)))
else: else:
s.remove = True s.remove = True
else:
unused.add(reg)
unused -= set(s.get_use()) changed = True
#unused = set()
#for s in reversed(block):
# for reg in s.get_def():
# if reg in unused:
# # Statement is redefined later, so this statement is useless
# if block.debug:
# s.stype = 'comment'
# s.options['block'] = False
# s.name = ' Dead:\t%s\t%s' \
# % (s.name, ','.join(map(str, s)))
# else:
# s.remove = True
# changed = True
# else:
# unused.add(reg)
# unused -= set(s.get_use())
if not block.debug: if not block.debug:
block.apply_filter(lambda s: not hasattr(s, 'remove')) block.apply_filter(lambda s: not hasattr(s, 'remove'))
......
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