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
45898435
Commit
45898435
authored
13 years ago
by
Jayke Meijer
Browse files
Options
Downloads
Patches
Plain Diff
Moved non-jump optimizations to block optimization.
parent
1ab45a98
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.py
+48
-52
48 additions, 52 deletions
src/optimize.py
src/statement.py
+4
-1
4 additions, 1 deletion
src/statement.py
tests/test_optimize.py
+66
-27
66 additions, 27 deletions
tests/test_optimize.py
with
118 additions
and
80 deletions
src/optimize.py
+
48
−
52
View file @
45898435
...
...
@@ -7,6 +7,45 @@ def optimize_global(statements):
"""
Optimize statement sequences on a global level.
"""
old_len
=
-
1
while
old_len
!=
len
(
statements
):
old_len
=
len
(
statements
)
while
not
statements
.
end
():
s
=
statements
.
read
()
# beq/bne ..., $Lx -> bne/beq ..., $Ly
# j $Ly $Lx:
# $Lx:
if
s
.
is_command
(
'
beq
'
)
or
s
.
is_command
(
'
bne
'
):
following
=
statements
.
peek
(
2
)
if
len
(
following
)
==
2
:
j
,
label
=
following
if
j
.
is_command
(
'
j
'
)
and
label
.
is_label
(
s
[
2
]):
if
s
.
is_command
(
'
beq
'
):
s
.
name
=
'
bne
'
else
:
s
.
name
=
'
beq
'
s
[
2
]
=
j
[
0
]
statements
.
replace
(
3
,
[
s
,
label
])
def
optimize_blocks
(
blocks
):
"""
Call the optimizer for each basic block. Do this several times until
no more optimizations are achieved.
"""
optimized
=
[]
for
block
in
blocks
:
optimize_block
(
block
)
return
blocks
def
optimize_block
(
statements
):
"""
Optimize a basic block.
"""
old_len
=
-
1
while
old_len
!=
len
(
statements
):
old_len
=
len
(
statements
)
...
...
@@ -67,57 +106,15 @@ def optimize_global(statements):
lw
[
-
1
]
=
str
(
s
[
2
])
+
lw
[
-
1
][
1
:]
statements
.
replace
(
2
,
[
lw
])
continue
# move $RegA, $RegB -> move $RegA, $RegB
# move $RegB, $RegA
if
s
.
is_command
(
'
move
'
):
move
=
statements
.
peek
()
# beq/bne ..., $Lx -> bne/beq ..., $Ly
# j $Ly $Lx:
# $Lx:
if
s
.
is_command
(
'
beq
'
)
or
s
.
is_command
(
'
bne
'
):
following
=
statements
.
peek
(
2
)
if
len
(
following
)
==
2
:
j
,
label
=
following
if
j
.
is_command
(
'
j
'
)
and
label
.
is_label
(
s
[
2
]):
if
s
.
is_command
(
'
beq
'
):
s
.
name
=
'
bne
'
else
:
s
.
name
=
'
beq
'
s
[
2
]
=
j
[
0
]
statements
.
replace
(
3
,
[
s
,
label
])
def
optimize_blocks
(
blocks
):
"""
Call the optimizer for each basic block. Do this several times until
no more optimizations are achieved.
"""
changed
=
True
while
changed
:
changed
=
False
optimized
=
[]
for
block
in
blocks
:
block_changed
,
b
=
optimize_block
(
block
)
optimized
.
append
(
b
)
if
block_changed
:
changed
=
True
blocks
=
optimized
return
reduce
(
lambda
a
,
b
:
a
+
b
,
blocks
,
[])
def
optimize_block
(
statements
):
"""
Optimize a basic block.
"""
changed
=
False
output_statements
=
[]
for
statement
in
statements
:
new_statement
=
statement
output_statements
.
append
(
new_statement
)
return
changed
,
output_statements
if
move
.
is_command
(
'
move
'
)
and
move
[
0
]
==
s
[
1
]
and
\
move
[
1
]
==
s
[
0
]:
statements
.
replace
(
2
,
[
s
])
def
optimize
(
statements
,
verbose
=
0
):
...
...
@@ -130,8 +127,7 @@ def optimize(statements, verbose=0):
# Optimize basic blocks
basic_blocks
=
find_basic_blocks
(
statements
)
# blocks = optimize_blocks(basic_blocks)
blocks
=
basic_blocks
blocks
=
optimize_blocks
(
basic_blocks
)
block_statements
=
map
(
lambda
b
:
b
.
statements
,
blocks
)
opt_blocks
=
reduce
(
lambda
a
,
b
:
a
+
b
,
block_statements
)
b
=
len
(
opt_blocks
)
...
...
This diff is collapsed.
Click to expand it.
src/statement.py
+
4
−
1
View file @
45898435
...
...
@@ -110,7 +110,10 @@ class Block:
def
peek
(
self
,
count
=
1
):
"""
Read the statements until an offset from the current pointer
position.
"""
position.
"""
if
self
.
end
():
return
Statement
(
'
empty
'
,
''
)
if
count
==
1
else
[]
return
self
.
statements
[
self
.
pointer
]
if
count
==
1
\
else
self
.
statements
[
self
.
pointer
:
self
.
pointer
+
count
]
...
...
This diff is collapsed.
Click to expand it.
tests/test_optimize.py
+
66
−
27
View file @
45898435
import
unittest
from
src.optimize
import
optimize_global
from
src.optimize
import
optimize_global
,
optimize_block
,
optimize_blocks
from
src.statement
import
Statement
as
S
,
Block
as
B
...
...
@@ -9,26 +9,26 @@ class TestOptimize(unittest.TestCase):
def
setUp
(
self
):
pass
def
test_optimize_
g
lo
bal
_movaa
(
self
):
def
test_optimize_
b
lo
ck
_movaa
(
self
):
foo
=
S
(
'
command
'
,
'
foo
'
)
bar
=
S
(
'
command
'
,
'
bar
'
)
block
=
B
([
foo
,
S
(
'
command
'
,
'
move
'
,
'
$regA
'
,
'
$regA
'
),
bar
])
optimize_
g
lo
bal
(
block
)
optimize_
b
lo
ck
(
block
)
self
.
assertEquals
(
block
.
statements
,
[
foo
,
bar
])
def
test_optimize_
g
lo
bal
_movab
(
self
):
def
test_optimize_
b
lo
ck
_movab
(
self
):
foo
=
S
(
'
command
'
,
'
foo
'
)
move
=
S
(
'
command
'
,
'
move
'
,
'
$regA
'
,
'
$regB
'
)
bar
=
S
(
'
command
'
,
'
bar
'
)
block
=
B
([
foo
,
move
,
bar
])
optimize_
g
lo
bal
(
block
)
optimize_
b
lo
ck
(
block
)
self
.
assertEquals
(
block
.
statements
,
[
foo
,
move
,
bar
])
def
test_optimize_
g
lo
bal
_movinst_true
(
self
):
def
test_optimize_
b
lo
ck
_movinst_true
(
self
):
foo
=
S
(
'
command
'
,
'
foo
'
)
bar
=
S
(
'
command
'
,
'
bar
'
)
...
...
@@ -36,12 +36,12 @@ class TestOptimize(unittest.TestCase):
S
(
'
command
'
,
'
move
'
,
'
$regA
'
,
'
$regB
'
),
S
(
'
command
'
,
'
addu
'
,
'
$regA
'
,
'
$regA
'
,
2
),
bar
])
optimize_
g
lo
bal
(
block
)
optimize_
b
lo
ck
(
block
)
self
.
assertEquals
(
block
.
statements
,
[
foo
,
S
(
'
command
'
,
'
addu
'
,
'
$regA
'
,
'
$regB
'
,
2
),
bar
])
def
test_optimize_
g
lo
bal
_movinst_false
(
self
):
def
test_optimize_
b
lo
ck
_movinst_false
(
self
):
foo
=
S
(
'
command
'
,
'
foo
'
)
bar
=
S
(
'
command
'
,
'
bar
'
)
statements
=
[
foo
,
\
...
...
@@ -50,10 +50,10 @@ class TestOptimize(unittest.TestCase):
bar
]
block
=
B
(
statements
)
optimize_
g
lo
bal
(
block
)
optimize_
b
lo
ck
(
block
)
self
.
assertEquals
(
block
.
statements
,
statements
)
def
test_optimize_
g
lo
bal
_instr_mov_jal_true
(
self
):
def
test_optimize_
b
lo
ck
_instr_mov_jal_true
(
self
):
foo
=
S
(
'
command
'
,
'
foo
'
)
bar
=
S
(
'
command
'
,
'
bar
'
)
...
...
@@ -62,14 +62,14 @@ class TestOptimize(unittest.TestCase):
S
(
'
command
'
,
'
move
'
,
'
$4
'
,
'
$regA
'
),
S
(
'
command
'
,
'
jal
'
,
'
L1
'
),
bar
])
optimize_
g
lo
bal
(
block
)
optimize_
b
lo
ck
(
block
)
self
.
assertEquals
(
block
.
statements
,
[
foo
,
S
(
'
command
'
,
'
addu
'
,
'
$4
'
,
'
$regC
'
,
2
),
S
(
'
command
'
,
'
jal
'
,
'
L1
'
),
bar
])
def
test_optimize_
g
lo
bal
_instr_mov_jal_false
(
self
):
def
test_optimize_
b
lo
ck
_instr_mov_jal_false
(
self
):
foo
=
S
(
'
command
'
,
'
foo
'
)
bar
=
S
(
'
command
'
,
'
bar
'
)
...
...
@@ -79,11 +79,11 @@ class TestOptimize(unittest.TestCase):
S
(
'
command
'
,
'
jal
'
,
'
L1
'
),
\
bar
]
block
=
B
(
arguments
)
optimize_
g
lo
bal
(
block
)
optimize_
b
lo
ck
(
block
)
self
.
assertEquals
(
block
.
statements
,
arguments
)
def
test_optimize_
g
lo
bal
_sw_ld_true
(
self
):
def
test_optimize_
b
lo
ck
_sw_ld_true
(
self
):
foo
=
S
(
'
command
'
,
'
foo
'
)
bar
=
S
(
'
command
'
,
'
bar
'
)
...
...
@@ -91,13 +91,13 @@ class TestOptimize(unittest.TestCase):
S
(
'
command
'
,
'
sw
'
,
'
$regA
'
,
'
$regB
'
),
S
(
'
command
'
,
'
ld
'
,
'
$regA
'
,
'
$regC
'
),
bar
])
optimize_
g
lo
bal
(
block
)
optimize_
b
lo
ck
(
block
)
self
.
assertEquals
(
block
.
statements
,
[
foo
,
S
(
'
command
'
,
'
sw
'
,
'
$regA
'
,
'
$regB
'
),
bar
])
def
test_optimize_
g
lo
bal
_sw_ld_false
(
self
):
def
test_optimize_
b
lo
ck
_sw_ld_false
(
self
):
foo
=
S
(
'
command
'
,
'
foo
'
)
bar
=
S
(
'
command
'
,
'
bar
'
)
...
...
@@ -106,23 +106,23 @@ class TestOptimize(unittest.TestCase):
S
(
'
command
'
,
'
ld
'
,
'
$regD
'
,
'
$regC
'
),
\
bar
]
block
=
B
(
arguments
)
optimize_
g
lo
bal
(
block
)
optimize_
b
lo
ck
(
block
)
self
.
assertEquals
(
block
.
statements
,
arguments
)
def
test_optimize_
g
lo
bal
_shift_true
(
self
):
def
test_optimize_
b
lo
ck
_shift_true
(
self
):
foo
=
S
(
'
command
'
,
'
foo
'
)
bar
=
S
(
'
command
'
,
'
bar
'
)
block
=
B
([
foo
,
S
(
'
command
'
,
'
sll
'
,
'
$regA
'
,
'
$regA
'
,
0
),
bar
])
optimize_
g
lo
bal
(
block
)
optimize_
b
lo
ck
(
block
)
self
.
assertEquals
(
block
.
statements
,
[
foo
,
bar
])
def
test_optimize_
g
lo
bal
_shift_false
(
self
):
def
test_optimize_
b
lo
ck
_shift_false
(
self
):
foo
=
S
(
'
command
'
,
'
foo
'
)
bar
=
S
(
'
command
'
,
'
bar
'
)
...
...
@@ -130,7 +130,7 @@ class TestOptimize(unittest.TestCase):
S
(
'
command
'
,
'
sll
'
,
'
$regA
'
,
'
$regB
'
,
0
),
\
bar
]
block
=
B
(
arguments
)
optimize_
g
lo
bal
(
block
)
optimize_
b
lo
ck
(
block
)
self
.
assertEquals
(
block
.
statements
,
arguments
)
...
...
@@ -138,11 +138,11 @@ class TestOptimize(unittest.TestCase):
S
(
'
command
'
,
'
sll
'
,
'
$regA
'
,
'
$regA
'
,
1
),
\
bar
]
block2
=
B
(
arguments2
)
optimize_
g
lo
bal
(
block2
)
optimize_
b
lo
ck
(
block2
)
self
.
assertEquals
(
block2
.
statements
,
arguments2
)
def
test_optimize_
g
lo
bal
_add_lw_true
(
self
):
def
test_optimize_
b
lo
ck
_add_lw_true
(
self
):
foo
=
S
(
'
command
'
,
'
foo
'
)
bar
=
S
(
'
command
'
,
'
bar
'
)
...
...
@@ -150,13 +150,13 @@ class TestOptimize(unittest.TestCase):
S
(
'
command
'
,
'
add
'
,
'
$regA
'
,
'
$regA
'
,
10
),
S
(
'
command
'
,
'
lw
'
,
'
$regB
'
,
'
0($regA)
'
),
bar
])
optimize_
g
lo
bal
(
block
)
optimize_
b
lo
ck
(
block
)
self
.
assertEquals
(
block
.
statements
,
[
foo
,
S
(
'
command
'
,
'
lw
'
,
'
$regB
'
,
'
10($regA)
'
),
bar
])
def
test_optimize_
g
lo
bal
_add_lw_false
(
self
):
def
test_optimize_
b
lo
ck
_add_lw_false
(
self
):
foo
=
S
(
'
command
'
,
'
foo
'
)
bar
=
S
(
'
command
'
,
'
bar
'
)
...
...
@@ -165,7 +165,7 @@ class TestOptimize(unittest.TestCase):
S
(
'
command
'
,
'
lw
'
,
'
$regB
'
,
'
0($regC)
'
),
\
bar
]
block
=
B
(
arguments
)
optimize_
g
lo
bal
(
block
)
optimize_
b
lo
ck
(
block
)
arguments2
=
[
foo
,
\
S
(
'
command
'
,
'
add
'
,
'
$regA
'
,
'
$regB
'
,
10
),
\
...
...
@@ -178,7 +178,7 @@ class TestOptimize(unittest.TestCase):
S
(
'
command
'
,
'
lw
'
,
'
$regB
'
,
'
1($regA)
'
),
\
bar
]
block3
=
B
(
arguments3
)
optimize_
g
lo
bal
(
block3
)
optimize_
b
lo
ck
(
block3
)
self
.
assertEquals
(
block
.
statements
,
arguments
)
self
.
assertEquals
(
block2
.
statements
,
arguments2
)
...
...
@@ -243,3 +243,42 @@ class TestOptimize(unittest.TestCase):
optimize_global
(
block
)
self
.
assertEquals
(
block
.
statements
,
arguments
)
def
test_optimize_block_move_move_true
(
self
):
foo
=
S
(
'
command
'
,
'
foo
'
)
bar
=
S
(
'
command
'
,
'
bar
'
)
block
=
B
([
foo
,
S
(
'
command
'
,
'
move
'
,
'
$regA
'
,
'
$regB
'
),
S
(
'
command
'
,
'
move
'
,
'
$regB
'
,
'
$regA
'
),
bar
])
optimize_block
(
block
)
self
.
assertEquals
(
block
.
statements
,
[
foo
,
S
(
'
command
'
,
'
move
'
,
'
$regA
'
,
'
$regB
'
),
bar
])
def
test_optimize_block_mov_mov_false
(
self
):
foo
=
S
(
'
command
'
,
'
foo
'
)
bar
=
S
(
'
command
'
,
'
bar
'
)
arguments
=
[
foo
,
\
S
(
'
command
'
,
'
move
'
,
'
$regA
'
,
'
$regB
'
),
\
S
(
'
command
'
,
'
move
'
,
'
$regB
'
,
'
$regC
'
),
\
bar
]
block
=
B
(
arguments
)
optimize_block
(
block
)
self
.
assertEquals
(
block
.
statements
,
arguments
)
def
test_optimize_blocks
(
self
):
foo
=
S
(
'
command
'
,
'
foo
'
)
bar
=
S
(
'
command
'
,
'
bar
'
)
block1
=
B
([
foo
,
bar
])
block2
=
B
([
bar
,
foo
])
blocks_in
=
[
block1
,
block2
];
blocks_out
=
optimize_blocks
(
blocks_in
)
self
.
assertEquals
(
blocks_in
,
blocks_out
)
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