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
3cadb493
Commit
3cadb493
authored
13 years ago
by
Jayke Meijer
Browse files
Options
Downloads
Patches
Plain Diff
Added proper removal of redundant jumps.
parent
ff70b648
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/redundancies.py
+36
-36
36 additions, 36 deletions
src/optimize/redundancies.py
src/program.py
+3
-1
3 additions, 1 deletion
src/program.py
tests/test_optimize.py
+9
-8
9 additions, 8 deletions
tests/test_optimize.py
with
48 additions
and
45 deletions
src/optimize/redundancies.py
+
36
−
36
View file @
3cadb493
...
...
@@ -124,45 +124,45 @@ def add_lw(add, statements):
return
True
def
remove_redundant_jumps
(
statements
):
"""
Remove jump if label follows immediatly.
"""
old_len
=
-
1
while
old_len
!=
len
(
statements
):
old_len
=
len
(
statements
)
"""
Remove jump if label follows immediately.
"""
changed
=
False
while
not
statements
.
end
():
s
=
statements
.
read
()
# j $Lx -> $Lx:
# $Lx:
if
s
.
is_command
(
'
j
'
):
following
=
statements
.
peek
(
2
)
if
following
.
is_label
(
s
[
0
]):
statements
.
replace
(
1
,
[])
statements
.
reset
()
while
not
statements
.
end
():
s
=
statements
.
read
()
# j $Lx -> $Lx:
# $Lx:
if
s
.
is_command
(
'
j
'
):
following
=
statements
.
peek
()
if
following
.
is_label
(
s
[
0
]):
statements
.
replace
(
1
,
[])
changed
=
True
return
True
def
remove_redundant_branch_jumps
(
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
'
,
'
bne
'
):
following
=
statements
.
peek
(
2
)
if
len
(
following
)
==
2
:
j
,
label
=
following
if
j
.
is_command
(
'
j
'
)
and
label
.
is_label
(
s
[
2
]):
s
.
name
=
'
bne
'
if
s
.
is_command
(
'
beq
'
)
else
'
beq
'
s
[
2
]
=
j
[
0
]
statements
.
replace
(
3
,
[
s
,
label
])
changed
=
False
statements
.
reset
()
while
not
statements
.
end
():
s
=
statements
.
read
()
# beq/bne ..., $Lx -> bne/beq ..., $Ly
# j $Ly $Lx:
# $Lx:
if
s
.
is_command
(
'
beq
'
,
'
bne
'
):
following
=
statements
.
peek
(
2
)
if
len
(
following
)
==
2
:
j
,
label
=
following
if
j
.
is_command
(
'
j
'
)
and
label
.
is_label
(
s
[
2
]):
s
.
name
=
'
bne
'
if
s
.
is_command
(
'
beq
'
)
else
'
beq
'
s
[
2
]
=
j
[
0
]
statements
.
replace
(
3
,
[
s
,
label
])
changed
=
True
return
changed
This diff is collapsed.
Click to expand it.
src/program.py
+
3
−
1
View file @
3cadb493
from
statement
import
Statement
as
S
,
Block
from
dataflow
import
find_basic_blocks
,
generate_flow_graph
from
optimize.redundancies
import
remove_redundant_jumps
,
remove_redundancies
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
from
writer
import
write_statements
...
...
@@ -63,6 +64,7 @@ class Program(Block):
def
optimize_global
(
self
):
"""
Optimize on a global level.
"""
remove_redundant_jumps
(
self
)
remove_redundant_branch_jumps
(
self
)
def
optimize_blocks
(
self
):
"""
Optimize on block level. Keep executing all optimizations until no
...
...
This diff is collapsed.
Click to expand it.
tests/test_optimize.py
+
9
−
8
View file @
3cadb493
import
unittest
from
src.optimize.redundancies
import
remove_redundancies
,
remove_redundant_jumps
from
src.optimize.redundancies
import
remove_redundancies
,
\
remove_redundant_jumps
,
remove_redundant_branch_jumps
from
src.program
import
Program
from
src.statement
import
Statement
as
S
,
Block
as
B
...
...
@@ -199,12 +200,12 @@ class TestOptimize(unittest.TestCase):
S
(
'
command
'
,
'
j
'
,
'
$L1
'
),
S
(
'
label
'
,
'
$L1
'
),
self
.
bar
])
remove_redundan
cie
s
(
block
)
remove_redundan
t_jump
s
(
block
)
self
.
assertEqual
(
block
.
statements
,
B
(
[
self
.
foo
,
S
(
'
command
'
,
'
j
'
,
'
$L1
'
),
self
.
bar
])
)
self
.
assertEqual
(
block
.
statements
,
[
self
.
foo
,
S
(
'
label
'
,
'
$L1
'
),
self
.
bar
])
def
test_remove_redundant_jumps_false
(
self
):
arguments
=
[
self
.
foo
,
...
...
@@ -213,9 +214,9 @@ class TestOptimize(unittest.TestCase):
self
.
bar
]
block
=
B
(
arguments
)
remove_redundan
cie
s
(
block
)
remove_redundan
t_jump
s
(
block
)
self
.
assertEqual
(
block
.
statements
,
arguments
)
self
.
assertEqual
(
block
.
statements
,
arguments
)
def
test_remove_redundant_branch_jumps_beq_j_true
(
self
):
block
=
B
([
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