Skip to content
Snippets Groups Projects
Commit 6181d33a authored by Taddeus Kroes's avatar Taddeus Kroes
Browse files

Fixed copy propagation name conflicts.

parent 24c91ff6
No related branches found
No related tags found
No related merge requests found
......@@ -59,35 +59,35 @@ def create_in_out(blocks):
#create_sets(blocks[0], True)
def propagate_copies(block):
changed = False
# For each copy statement s: x = y do
for s in block:
if s.is_command('move'):
x, y = s
# Determine the uses of x reached by this definition of x
uses = filter(lambda suc: s.sid in suc.reach_in, block.edges_to)
# Determine if for each of those uses this is the only
# definition reaching it -> s in in[B_use]
only_def = True
for b_use in uses:
if (x, y) not in b_use.copy_in:
only_def = False
# If so, remove s and replace the uses of x by uses of y
if only_def:
for use in uses:
print 'use:', use
for statement in use:
if statement.uses(x):
statement.replace_usage(x, y)
message = ' Replaced %s with %s' % (x, y)
print message
statement.set_inline_comment(message)
changed = True
return changed
#def propagate_copies(block):
# changed = False
#
# # For each copy statement s: x = y do
# for s in block:
# if s.is_command('move'):
# x, y = s
#
# # Determine the uses of x reached by this definition of x
# uses = filter(lambda suc: s.sid in suc.reach_in, block.edges_to)
#
# # Determine if for each of those uses this is the only
# # definition reaching it -> s in in[B_use]
# only_def = True
#
# for b_use in uses:
# if (x, y) not in b_use.copy_in:
# only_def = False
#
# # If so, remove s and replace the uses of x by uses of y
# if only_def:
# for use in uses:
# print 'use:', use
# for statement in use:
# if statement.uses(x):
# statement.replace_usage(x, y)
# message = ' Replaced %s with %s' % (x, y)
# print message
# statement.set_inline_comment(message)
# changed = True
#
# return changed
......@@ -282,7 +282,7 @@ def fold_constants(block):
return changed
#def copy_propagation(block):
#def propagate_copies(block):
# """
# Unpack a move instruction, by replacing its destination
# address with its source address in the code following the move instruction.
......@@ -343,7 +343,7 @@ def fold_constants(block):
# return changed
#def copy_propagation(block):
#def propagate_copies(block):
# """
# Unpack a move instruction, by replacing its destination
# address with its source address in the code following the move instruction.
......@@ -420,7 +420,7 @@ def fold_constants(block):
# return changed
def copy_propagation(block):
def propagate_copies(block):
"""
Unpack a move instruction, by replacing its destination
address with its source address in the code following the move instruction.
......
......@@ -4,11 +4,11 @@ from dataflow import find_basic_blocks, generate_flow_graph
from optimize.redundancies import remove_redundant_jumps, remove_redundancies,\
remove_redundant_branch_jumps
from optimize.advanced import eliminate_common_subexpressions, \
fold_constants, copy_propagation, eliminate_dead_code
fold_constants, propagate_copies, eliminate_dead_code
import liveness
import reaching_definitions
#import copy_propagation as copy_propagation_flow
import copy_propagation
from writer import write_statements
......@@ -95,7 +95,7 @@ class Program(Block):
if fold_constants(block):
changed = True
if copy_propagation(block):
if propagate_copies(block):
changed = True
if eliminate_dead_code(block):
......@@ -104,7 +104,7 @@ class Program(Block):
#if remove_redundancies(block) \
# | eliminate_common_subexpressions(block) \
# | fold_constants(block) \
# | copy_propagation(block) \
# | propagate_copies(block) \
# | eliminate_dead_code(block):
# changed = True
......@@ -129,7 +129,7 @@ class Program(Block):
generate_flow_graph(self.blocks)
liveness.create_in_out(self.blocks)
reaching_definitions.create_in_out(self.blocks)
#copy_propagation_flow.create_in_out(self.blocks)
copy_propagation.create_in_out(self.blocks)
def save(self, filename):
"""Save the program in the specified file."""
......
......@@ -2,7 +2,7 @@ import unittest
from copy import copy
from src.optimize.advanced import eliminate_common_subexpressions, \
fold_constants, copy_propagation
fold_constants, propagate_copies
from src.statement import Statement as S
from src.dataflow import BasicBlock as B, generate_flow_graph
import src.liveness as liveness
......@@ -40,49 +40,49 @@ class TestOptimizeAdvanced(unittest.TestCase):
def test_fold_constants(self):
pass
#def test_copy_propagation_true(self):
#def test_propagate_copies_true(self):
# block = B([self.foo,
# S('command', 'move', '$1', '$2'),
# self.foo,
# S('command', 'addu', '$3', '$1', '$4'),
# self.bar])
# self.assertTrue(copy_propagation(block))
# self.assertTrue(propagate_copies(block))
# self.assertEqual(block.statements, [self.foo,
# S('command', 'move', '$1', '$2'),
# self.foo,
# S('command', 'addu', '$3', '$2', '$4'),
# self.bar])
def test_copy_propagation_other_arg(self):
def test_propagate_copies_other_arg(self):
block = B([self.foo,
S('command', 'move', '$1', '$2'),
self.foo,
S('command', 'addu', '$3', '$4', '$1'),
self.bar])
self.assertTrue(copy_propagation(block))
self.assertTrue(propagate_copies(block))
self.assertEqual(block.statements, [self.foo,
S('command', 'move', '$1', '$2'),
self.foo,
S('command', 'addu', '$3', '$4', '$2'),
self.bar])
#def test_copy_propagation_overwrite(self):
#def test_propagate_copies_overwrite(self):
# block = B([self.foo,
# S('command', 'move', '$1', '$2'),
# S('command', 'move', '$1', '$5'),
# S('command', 'addu', '$3', '$1', '$4'),
# self.bar])
# self.assertTrue(copy_propagation(block))
# self.assertTrue(propagate_copies(block))
# self.assertEqual(block.statements, [self.foo,
# S('command', 'move', '$1', '$2'),
# S('command', 'move', '$1', '$5'),
# S('command', 'addu', '$3', '$5', '$4'),
# self.bar])
def test_copy_propagation_false(self):
def test_propagate_copies_false(self):
arguments = [self.foo,
S('command', 'move', '$1', '$2'),
S('command', 'move', '$10', '$20'),
......@@ -90,10 +90,10 @@ class TestOptimizeAdvanced(unittest.TestCase):
S('command', 'addu', '$3', '$1', '$4'),
self.bar]
block = B(arguments)
self.assertFalse(copy_propagation(block))
self.assertFalse(propagate_copies(block))
self.assertEqual(block.statements, arguments)
def test_copy_propagation_false_severalmoves(self):
def test_propagate_copies_false_severalmoves(self):
arguments = [self.foo,
S('command', 'move', '$1', '$2'),
self.foo,
......@@ -101,7 +101,7 @@ class TestOptimizeAdvanced(unittest.TestCase):
S('command', 'addu', '$3', '$1', '$4'),
self.bar]
block = B(arguments)
self.assertFalse(copy_propagation(block))
self.assertFalse(propagate_copies(block))
self.assertEqual(block.statements, arguments)
#def test_algebraic_transforms_add0(self):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment