Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
peephole
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
Taddeüs Kroes
peephole
Commits
bf493e78
Commit
bf493e78
authored
13 years ago
by
Taddeus Kroes
Browse files
Options
Downloads
Plain Diff
Merged conflicts.
parents
d0af1443
a2133db3
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
report/report.tex
+11
-0
11 additions, 0 deletions
report/report.tex
src/optimize/advanced.py
+23
-24
23 additions, 24 deletions
src/optimize/advanced.py
tests/test_optimize_advanced.py
+41
-15
41 additions, 15 deletions
tests/test_optimize_advanced.py
with
75 additions
and
39 deletions
report/report.tex
+
11
−
0
View file @
bf493e78
...
...
@@ -236,5 +236,16 @@ addu $regA, $regB, 4 addu $regD, $regB, 4
Code not writing
$
regB -> ...
... ...
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{document}
This diff is collapsed.
Click to expand it.
src/optimize/advanced.py
+
23
−
24
View file @
bf493e78
...
...
@@ -162,50 +162,49 @@ def fold_constants(block):
def
copy_propagation
(
block
):
"""
Rename values that were copied to there original, so the copy statement
might be useless, allowing it to be removed by dead code elimination.
Replace a variable with its original variable after a move if possible, by
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_to
=
[]
changed
=
False
while
not
block
.
end
():
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
:
# Add this move to the lists, because it is not yet there.
moves_from
.
append
(
s
[
1
])
moves_to
.
append
(
s
[
0
])
print
"
Added move to list.
"
elif
s
.
is_command
(
'
move
'
):
elif
s
.
is_command
(
'
move
'
)
and
s
[
0
]
in
moves_to
:
# This move is already in the lists, so only update it
for
i
in
xrange
(
len
(
moves_to
)):
if
moves_to
[
i
]
==
s
[
0
]:
moves_from
[
i
]
=
s
[
1
]
break
elif
len
(
s
)
==
3
and
s
[
0
]
in
moves_to
:
print
len
(
s
)
print
len
(
moves_to
)
for
i
in
xrange
(
len
(
moves_to
)
)
:
# The result gets overwritten, so remove the data from the list.
i
=
0
while
i
<
len
(
moves_to
):
if
moves_to
[
i
]
==
s
[
0
]:
del
moves_to
[
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
):
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
)):
if
s
[
1
]
==
moves_to
[
i
]:
s
[
1
]
=
moves_from
[
i
]
print
"
Propagated
"
break
if
s
[
2
]
==
moves_to
[
i
]:
s
[
2
]
=
moves_from
[
i
]
print
"
Propagated
"
print
""
return
False
break
changed
=
True
return
changed
This diff is collapsed.
Click to expand it.
tests/test_optimize_advanced.py
+
41
−
15
View file @
bf493e78
...
...
@@ -11,6 +11,10 @@ class TestOptimizeAdvanced(unittest.TestCase):
self
.
foo
=
S
(
'
command
'
,
'
foo
'
)
self
.
bar
=
S
(
'
command
'
,
'
bar
'
)
def
tearDown
(
self
):
del
self
.
foo
del
self
.
bar
def
test_eliminate_common_subexpressions
(
self
):
pass
...
...
@@ -18,29 +22,51 @@ class TestOptimizeAdvanced(unittest.TestCase):
pass
def
test_copy_propagation_true
(
self
):
print
"
testing true
"
block
=
B
([
self
.
foo
,
S
(
'
command
'
,
'
move
'
,
'
$1
'
,
'
$2
'
),
self
.
foo
,
S
(
'
command
'
,
'
addu
'
,
'
$3
'
,
'
$1
'
,
'
$4
'
),
self
.
bar
])
copy_propagation
(
block
)
self
.
assertTrue
(
copy_propagation
(
block
)
)
self
.
assertEqual
(
block
.
statements
,
[
self
.
foo
,
S
(
'
command
'
,
'
move
'
,
'
$1
'
,
'
$2
'
),
self
.
foo
,
S
(
'
command
'
,
'
addu
'
,
'
$3
'
,
'
$2
'
,
'
$4
'
),
self
.
bar
])
print
"
Test true succesfull
"
# def test_copy_propagation_false(self):
# print "Testing false"
# arguments = [self.foo,
# S('command', 'move', '$1', '$2'),
# S('command', 'move', '$10', '$20'),
# S('command', 'addu', '$1', '$5', 1),
# S('command', 'addu', '$3', '$1', '$4'),
# self.bar]
# block = B(arguments)
# copy_propagation(block)
# self.assertEqual(block.statements, arguments)
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
):
arguments
=
[
self
.
foo
,
S
(
'
command
'
,
'
move
'
,
'
$1
'
,
'
$2
'
),
S
(
'
command
'
,
'
move
'
,
'
$10
'
,
'
$20
'
),
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
)
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
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment