示例#1
0
def eliminate_dead_code(block):
    """
    Dead code elimination:
    TODO: example...

    The algorithm used is as follows:
    - Traverse through the statements in reverse order.
    - If the statement definition is dead, remove it. A variable is dead if it
      is not used in the rest of the block, and is not in the `out' set of the
      block.
    """
    changed = False

    for n, s in enumerate(block):
        for reg in s.get_def():
            if is_reg_dead_after(reg, block, n):
                # Statement is redefined later, so this statement is useless
                if block.verbose:
                    s.stype = 'comment'
                    s.options['block'] = False
                    s.set_message(' dead register %s' % reg)
                    s.name = ' Dead:\t%s\t%s' % (s.name, ','.join(map(str, s)))
                else:
                    s.remove = True

                changed = True

    if not block.verbose:
        block.apply_filter(lambda s: not hasattr(s, 'remove'))

    return changed
示例#2
0
def find_free_reg(block, start):
    """Find a temporary register that is free in a given list of statements."""
    for i in xrange(8, 16):
        tmp = '$%d' % i

        if is_reg_dead_after(tmp, block, start):
            return tmp

    raise Exception('No temporary register is available.')