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
302f98ab
Commit
302f98ab
authored
13 years ago
by
Taddeus Kroes
Browse files
Options
Downloads
Patches
Plain Diff
Improved local Copy Propagation.
parent
86b4d428
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
src/optimize/advanced.py
+106
-39
106 additions, 39 deletions
src/optimize/advanced.py
src/statement.py
+3
-2
3 additions, 2 deletions
src/statement.py
tests/test_optimize_advanced.py
+24
-24
24 additions, 24 deletions
tests/test_optimize_advanced.py
with
133 additions
and
65 deletions
src/optimize/advanced.py
+
106
−
39
View file @
302f98ab
from
math
import
log
import
re
from
src.statement
import
Statement
as
S
from
src.liveness
import
is_reg_dead_after
...
...
@@ -83,7 +83,7 @@ def eliminate_common_subexpressions(block):
# Replace all occurrences with a move statement
message
=
'
Common subexpression reference: %s %s
'
\
%
(
s
.
name
,
'
,
'
.
join
(
map
(
str
,
[
new_reg
]
+
s
[
1
:])))
%
(
s
.
name
,
'
,
'
.
join
(
map
(
str
,
[
new_reg
]
+
s
[
1
:])))
for
occurrence
in
occurrences
:
rd
=
block
[
occurrence
][
0
]
...
...
@@ -93,7 +93,7 @@ def eliminate_common_subexpressions(block):
# Insert the calculation before the original with the new
# destination address
message
=
'
Common subexpression: %s %s
'
\
%
(
s
.
name
,
'
,
'
.
join
(
map
(
str
,
s
)))
%
(
s
.
name
,
'
,
'
.
join
(
map
(
str
,
s
)))
block
.
insert
(
S
(
'
command
'
,
s
.
name
,
*
([
new_reg
]
+
args
)),
\
index
=
occurrences
[
0
],
message
=
message
)
changed
=
True
...
...
@@ -207,7 +207,7 @@ def fold_constants(block):
# Replace the multiplication with two immidiate loads to the
# Hi/Lo registers
block
.
replace
(
1
,
[
S
(
'
command
'
,
'
li
'
,
'
$hi
'
,
hi
),
S
(
'
command
'
,
'
li
'
,
'
$lo
'
,
l
i
)],
S
(
'
command
'
,
'
li
'
,
'
$lo
'
,
l
o
)],
message
=
message
)
register
[
'
$lo
'
],
register
[
'
$hi
'
]
=
lo
,
hi
...
...
@@ -276,11 +276,72 @@ def fold_constants(block):
known
.
append
((
reg
,
'
unknown
'
))
if
block
.
verbose
and
len
(
known
):
s
.
set_
inline_comment
(
'
,
'
.
join
([
'
%s = %s
'
%
k
for
k
in
known
]))
s
.
set_
message
(
'
,
'
.
join
([
'
%s = %s
'
%
k
for
k
in
known
]))
return
changed
#def copy_propagation(block):
# """
# Unpack a move instruction, by replacing its destination
# address with its source address in the code following the move instruction.
# This way, the move statement might be a target for dead code elimination.
#
# move $regA, $regB move $regA, $regB
# ... ...
# Code not writing $regA, -> ...
# $regB ...
# ... ...
# addu $regC, $regA, ... addu $regC, $regB, ...
# """
# moves_from = []
# moves_to = []
# changed = False
#
# block.reset()
#
# while not block.end():
# s = block.read()
#
# 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])
# 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]
# continue
# elif (len(s) == 3 or s.is_command('mlfo') or s.is_load()) \
# and (s[0] in moves_to or s[0] in moves_from):
# # One of the registers gets overwritten, so remove the data from
# # the list.
# i = 0
#
# while i < len(moves_to):
# if moves_to[i] == s[0] or moves_to[i] == s[1]:
# del moves_to[i]
# del moves_from[i]
# else:
# i += 1
# elif len(s) == 3 and (s[1] in moves_to or s[2] in moves_to):
# # 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]
# continue
#
# if s[2] == moves_to[i]:
# s[2] = moves_from[i]
# continue
#
# changed = True
#
# return changed
def
copy_propagation
(
block
):
"""
Unpack a move instruction, by replacing its destination
...
...
@@ -294,50 +355,56 @@ def copy_propagation(block):
...
...
addu
$
regC
,
$
regA
,
...
addu
$
regC
,
$
regB
,
...
"""
moves_from
=
[]
moves_to
=
[]
changed
=
False
moves
=
{}
block
.
reset
()
while
not
block
.
end
():
s
=
block
.
read
()
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
])
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
]
if
not
s
.
is_command
(
)
:
continue
if
s
.
name
==
'
move
'
:
# Register the move
reg_to
,
reg_from
=
s
if
reg_from
in
moves
:
if
moves
[
reg
_from
]
=
=
reg_to
:
continue
elif
(
len
(
s
)
==
3
or
s
.
is_command
(
'
mlfo
'
)
or
s
.
is_load
())
\
and
(
s
[
0
]
in
moves_to
or
s
[
0
]
in
moves_from
):
# One of the registers gets overwritten, so remove the data from
# the list.
i
=
0
while
i
<
len
(
moves_to
):
if
moves_to
[
i
]
==
s
[
0
]
or
moves_to
[
i
]
==
s
[
1
]:
del
moves_to
[
i
]
del
moves_from
[
i
]
else
:
i
+=
1
elif
len
(
s
)
==
3
and
(
s
[
1
]
in
moves_to
or
s
[
2
]
in
moves_to
):
# 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
]
continue
moves
[
reg_to
]
=
moves
[
reg_from
]
elif
reg_to
==
reg_from
:
del
moves
[
reg_to
]
else
:
moves
[
reg_to
]
=
reg_from
if
s
[
2
]
==
moves_to
[
i
]:
s
[
2
]
=
moves_from
[
i
]
continue
s
.
set_message
(
'
Move: %s = %s
'
%
(
reg_to
,
moves
[
reg_to
]))
continue
changed
=
True
# Replace used registers with moved equivalents when possible
for
i
,
reg
in
s
.
get_use
(
True
):
if
reg
in
moves
:
s
[
i
]
=
re
.
sub
(
'
\\
'
+
reg
+
'
(?!\d)
'
,
moves
[
reg
],
s
[
i
])
s
.
set_message
(
'
Replaced %s with %s
'
%
(
reg
,
moves
[
reg
]))
changed
=
True
# If a known moved register is overwritten, remove it from the
# registration
defined
=
s
.
get_def
()
delete
=
[]
for
move_to
,
move_from
in
moves
.
iteritems
():
if
move_to
in
defined
or
move_from
in
defined
:
delete
.
append
(
move_to
)
if
len
(
delete
):
s
.
set_message
(
'
Moves deleted: %s
'
%
'
,
'
.
join
(
delete
))
for
reg
in
delete
:
del
moves
[
reg
]
return
changed
...
...
@@ -362,7 +429,7 @@ def eliminate_dead_code(block):
if
block
.
verbose
:
s
.
stype
=
'
comment
'
s
.
options
[
'
block
'
]
=
False
s
.
set_
inline_comment
(
'
dead register %s
'
%
reg
)
s
.
set_
message
(
'
dead register %s
'
%
reg
)
s
.
name
=
'
Dead:
\t
%s
\t
%s
'
%
(
s
.
name
,
'
,
'
.
join
(
map
(
str
,
s
)))
else
:
s
.
remove
=
True
...
...
This diff is collapsed.
Click to expand it.
src/statement.py
+
3
−
2
View file @
302f98ab
...
...
@@ -36,7 +36,8 @@ class Statement:
%
(
self
.
sid
,
self
.
stype
,
self
.
name
,
self
.
args
)
def
__repr__
(
self
):
# pragma: nocover
return
str
(
self
)
return
'
<Statement type=%s name=%s args=%s>
'
\
%
(
self
.
stype
,
self
.
name
,
self
.
args
)
def
set_message
(
self
,
message
):
self
.
options
[
'
message
'
]
=
message
...
...
@@ -247,7 +248,7 @@ class Statement:
class
Block
:
bid
=
1
def
__init__
(
self
,
statements
=
[],
verbose
=
False
):
def
__init__
(
self
,
statements
=
[],
verbose
=
0
):
self
.
statements
=
statements
self
.
pointer
=
0
...
...
This diff is collapsed.
Click to expand it.
tests/test_optimize_advanced.py
+
24
−
24
View file @
302f98ab
...
...
@@ -40,19 +40,19 @@ class TestOptimizeAdvanced(unittest.TestCase):
def
test_fold_constants
(
self
):
pass
def
test_copy_propagation_true
(
self
):
block
=
B
([
self
.
foo
,
S
(
'
command
'
,
'
move
'
,
'
$1
'
,
'
$2
'
),
self
.
foo
,
S
(
'
command
'
,
'
addu
'
,
'
$3
'
,
'
$1
'
,
'
$4
'
),
self
.
bar
])
#
def test_copy_propagation_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
.
assertEqual
(
block
.
statements
,
[
self
.
foo
,
S
(
'
command
'
,
'
move
'
,
'
$1
'
,
'
$2
'
),
self
.
foo
,
S
(
'
command
'
,
'
addu
'
,
'
$3
'
,
'
$2
'
,
'
$4
'
),
self
.
bar
])
#
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])
def
test_copy_propagation_other_arg
(
self
):
block
=
B
([
self
.
foo
,
...
...
@@ -68,19 +68,19 @@ class TestOptimizeAdvanced(unittest.TestCase):
S
(
'
command
'
,
'
addu
'
,
'
$3
'
,
'
$4
'
,
'
$2
'
),
self
.
bar
])
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
])
#
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
])
#
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
,
...
...
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