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
a2a424d0
Commit
a2a424d0
authored
Dec 28, 2011
by
Jayke Meijer
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Tested copy_propagation.
parent
2fdd48ad
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
70 additions
and
38 deletions
+70
-38
report/report.tex
report/report.tex
+11
-0
src/optimize/advanced.py
src/optimize/advanced.py
+22
-23
tests/test_optimize_advanced.py
tests/test_optimize_advanced.py
+37
-15
No files found.
report/report.tex
View file @
a2a424d0
...
@@ -236,5 +236,16 @@ addu $regA, $regB, 4 addu $regD, $regB, 4
...
@@ -236,5 +236,16 @@ addu $regA, $regB, 4 addu $regD, $regB, 4
Code not writing
$
regB -> ...
Code not writing
$
regB -> ...
... ...
... ...
addu
$
regC,
$
regB, 4 move
$
regC,
$
regD
addu
$
regC,
$
regB, 4 move
$
regC,
$
regD
# Constant folding
# Copy propagation
move
$
regA,
$
regB move
$
regA,
$
regB
... ...
Code not writing
$
regA,
-
> ...
$
regB ...
... ...
addu
$
regC,
$
regA, ... addu
$
regC,
$
regB, ...
\end{verbatim}
\end{verbatim}
\end{document}
\end{document}
src/optimize/advanced.py
View file @
a2a424d0
...
@@ -153,50 +153,49 @@ def fold_constants(block):
...
@@ -153,50 +153,49 @@ def fold_constants(block):
def
copy_propagation
(
block
):
def
copy_propagation
(
block
):
"""
"""
Rename values that were copied to there original, so the copy statement
Replace a variable with its original variable after a move if possible, by
might be useless, allowing it to be removed by dead code elimination.
walking through the code, storing move operations and checking whether it
changes or whether a variable can be replaced. This way, the move statement
might be a target for dead code elimination.
"""
"""
moves_from
=
[]
moves_from
=
[]
moves_to
=
[]
moves_to
=
[]
changed
=
False
while
not
block
.
end
():
while
not
block
.
end
():
s
=
block
.
read
()
s
=
block
.
read
()
if
len
(
s
)
==
3
:
print
"s[0] = "
,
s
[
0
]
print
"s[1] = "
,
s
[
1
]
print
"s[2] = "
,
s
[
2
]
if
moves_from
:
print
"fr: "
,
moves_from
print
"to: "
,
moves_to
if
s
.
is_command
(
'move'
)
and
s
[
0
]
not
in
moves_to
:
if
s
.
is_command
(
'move'
)
and
s
[
0
]
not
in
moves_to
:
# Add this move to the lists, because it is not yet there.
moves_from
.
append
(
s
[
1
])
moves_from
.
append
(
s
[
1
])
moves_to
.
append
(
s
[
0
])
moves_to
.
append
(
s
[
0
])
print
"Added move to list."
elif
s
.
is_command
(
'move'
)
and
s
[
0
]
in
moves_to
:
elif
s
.
is_command
(
'move'
):
# This move is already in the lists, so only update it
for
i
in
xrange
(
len
(
moves_to
)):
for
i
in
xrange
(
len
(
moves_to
)):
if
moves_to
[
i
]
==
s
[
0
]:
if
moves_to
[
i
]
==
s
[
0
]:
moves_from
[
i
]
=
s
[
1
]
moves_from
[
i
]
=
s
[
1
]
break
elif
len
(
s
)
==
3
and
s
[
0
]
in
moves_to
:
elif
len
(
s
)
==
3
and
s
[
0
]
in
moves_to
:
print
len
(
s
)
# The result gets overwritten, so remove the data from the list.
print
len
(
moves_to
)
i
=
0
for
i
in
xrange
(
len
(
moves_to
)
):
while
i
<
len
(
moves_to
):
if
moves_to
[
i
]
==
s
[
0
]:
if
moves_to
[
i
]
==
s
[
0
]:
del
moves_to
[
i
]
del
moves_to
[
i
]
del
moves_from
[
i
]
del
moves_from
[
i
]
"Removed move from list."
else
:
i
+=
1
elif
len
(
s
)
==
3
and
(
s
[
1
]
in
moves_to
or
s
[
2
]
in
moves_to
):
elif
len
(
s
)
==
3
and
(
s
[
1
]
in
moves_to
or
s
[
2
]
in
moves_to
):
print
"Have to propagate."
# Check where the result of the move is used and replace it with
# the original variable.
for
i
in
xrange
(
len
(
moves_to
)):
for
i
in
xrange
(
len
(
moves_to
)):
if
s
[
1
]
==
moves_to
[
i
]:
if
s
[
1
]
==
moves_to
[
i
]:
s
[
1
]
=
moves_from
[
i
]
s
[
1
]
=
moves_from
[
i
]
print
"Propagated"
break
if
s
[
2
]
==
moves_to
[
i
]:
if
s
[
2
]
==
moves_to
[
i
]:
s
[
2
]
=
moves_from
[
i
]
s
[
2
]
=
moves_from
[
i
]
print
"Propagated"
break
print
""
changed
=
True
return
False
return
changed
tests/test_optimize_advanced.py
View file @
a2a424d0
...
@@ -14,30 +14,52 @@ class TestOptimizeAdvanced(unittest.TestCase):
...
@@ -14,30 +14,52 @@ class TestOptimizeAdvanced(unittest.TestCase):
def
test_eliminate_common_subexpressions
(
self
):
def
test_eliminate_common_subexpressions
(
self
):
pass
pass
def
test_copy_propagation_true
(
self
):
def
test_copy_propagation_true
(
self
):
print
"testing true"
block
=
B
([
self
.
foo
,
block
=
B
([
self
.
foo
,
S
(
'command'
,
'move'
,
'$1'
,
'$2'
),
S
(
'command'
,
'move'
,
'$1'
,
'$2'
),
self
.
foo
,
self
.
foo
,
S
(
'command'
,
'addu'
,
'$3'
,
'$1'
,
'$4'
),
S
(
'command'
,
'addu'
,
'$3'
,
'$1'
,
'$4'
),
self
.
bar
])
self
.
bar
])
copy_propagation
(
block
)
self
.
assertTrue
(
copy_propagation
(
block
)
)
self
.
assertEqual
(
block
.
statements
,
[
self
.
foo
,
self
.
assertEqual
(
block
.
statements
,
[
self
.
foo
,
S
(
'command'
,
'move'
,
'$1'
,
'$2'
),
S
(
'command'
,
'move'
,
'$1'
,
'$2'
),
self
.
foo
,
self
.
foo
,
S
(
'command'
,
'addu'
,
'$3'
,
'$2'
,
'$4'
),
S
(
'command'
,
'addu'
,
'$3'
,
'$2'
,
'$4'
),
self
.
bar
])
self
.
bar
])
print
"Test true succesfull"
def
test_copy_propagation_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
.
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_copy_propagation_false
(
self
):
# print "Testing false"
arguments
=
[
self
.
foo
,
# arguments = [self.foo,
S
(
'command'
,
'move'
,
'$1'
,
'$2'
),
# S('command', 'move', '$1', '$2'),
S
(
'command'
,
'move'
,
'$10'
,
'$20'
),
# S('command', 'move', '$10', '$20'),
S
(
'command'
,
'addu'
,
'$1'
,
'$5'
,
1
),
# S('command', 'addu', '$1', '$5', 1),
S
(
'command'
,
'addu'
,
'$3'
,
'$1'
,
'$4'
),
# S('command', 'addu', '$3', '$1', '$4'),
self
.
bar
]
# self.bar]
block
=
B
(
arguments
)
# block = B(arguments)
self
.
assertFalse
(
copy_propagation
(
block
))
# copy_propagation(block)
self
.
assertEqual
(
block
.
statements
,
arguments
)
# self.assertEqual(block.statements, arguments)
def
test_copy_propagation_false_severalmoves
(
self
):
arguments
=
[
self
.
foo
,
S
(
'command'
,
'move'
,
'$1'
,
'$2'
),
self
.
foo
,
S
(
'command'
,
'addu'
,
'$1'
,
'$5'
,
1
),
S
(
'command'
,
'addu'
,
'$3'
,
'$1'
,
'$4'
),
self
.
bar
]
block
=
B
(
arguments
)
self
.
assertFalse
(
copy_propagation
(
block
))
self
.
assertEqual
(
block
.
statements
,
arguments
)
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