Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
peephole
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Taddeüs Kroes
peephole
Commits
6181d33a
Commit
6181d33a
authored
Dec 31, 2011
by
Taddeus Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixed copy propagation name conflicts.
parent
24c91ff6
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
51 additions
and
51 deletions
+51
-51
src/copy_propagation.py
src/copy_propagation.py
+32
-32
src/optimize/advanced.py
src/optimize/advanced.py
+3
-3
src/program.py
src/program.py
+5
-5
tests/test_optimize_advanced.py
tests/test_optimize_advanced.py
+11
-11
No files found.
src/copy_propagation.py
View file @
6181d33a
...
...
@@ -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
src/optimize/advanced.py
View file @
6181d33a
...
...
@@ -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.
...
...
src/program.py
View file @
6181d33a
...
...
@@ -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."""
...
...
tests/test_optimize_advanced.py
View file @
6181d33a
...
...
@@ -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):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment