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
9be472d7
Commit
9be472d7
authored
13 years ago
by
Taddeus Kroes
Browse files
Options
Downloads
Patches
Plain Diff
Added implementations of some basic optimizations.
parent
d0a4e367
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/optimize.py
+49
-17
49 additions, 17 deletions
src/optimize.py
src/utils.py
+4
-0
4 additions, 0 deletions
src/utils.py
with
53 additions
and
17 deletions
src/optimize.py
+
49
−
17
View file @
9be472d7
from
utils
import
Statement
as
S
,
Block
,
find_basic_blocks
def
equal_mov
(
s
):
"""
Check for useless move operations.
"""
return
s
.
is_command
()
and
s
.
name
==
'
move
'
and
s
[
0
]
==
s
[
1
]
def
empty_shift
(
s
):
"""
Check for useless shift operations.
"""
return
s
.
is_shift
()
and
s
[
0
]
==
s
[
1
]
and
s
[
2
]
==
0
from
utils
import
Statement
as
S
,
find_basic_blocks
def
optimize_branch_jump_label
(
statements
):
...
...
@@ -39,15 +29,57 @@ def optimize_branch_jump_label(statements):
def
optimize_global
(
statements
):
"""
Optimize one-line statements in entire code.
"""
statements
=
optimize_branch_jump_label
(
statements
)
old_len
=
-
1
while
old_len
!=
len
(
statements
):
old_len
=
len
(
statements
)
while
not
statements
.
end
():
s
=
statements
.
read
()
# mov $regA,$regB -> --- remove it
if
s
.
is_command
()
and
s
.
name
==
'
move
'
and
s
[
0
]
==
s
[
1
]:
statements
.
replace
(
1
,
[])
# mov $regA,$regB -> instr $regA, $regB, ...
# instr $regA, $regA, ...
if
s
.
is_command
()
and
s
.
name
==
'
move
'
:
ins
=
statements
.
peek
()
if
ins
and
len
(
ins
)
>=
2
and
ins
[
0
]
==
s
[
0
]
and
ins
[
1
]
==
s
[
0
]:
ins
[
1
]
=
s
[
1
]
# instr $regA, ... -> instr $4, ...
# mov $4, $regA jal XX
# jal XX
if
s
.
is_command
()
and
len
(
s
):
following
=
statements
.
peek
(
2
)
if
len
(
following
)
==
2
:
mov
,
jal
=
following
if
mov
.
name
==
'
move
'
and
mov
[
1
]
==
s
[
0
]
\
and
jal
.
name
==
'
jal
'
:
s
[
0
]
=
mov
[
0
]
statements
.
replace
(
1
,
[],
start
=
statements
.
pointer
+
1
)
# sw $regA, XX -> sw $regA, XX
# ld $regA, XX
# shift $regA, $regA, 0 -> --- remove it
if
s
.
is_shift
()
and
s
[
0
]
==
s
[
1
]
and
s
[
2
]
==
0
:
statements
.
replace
(
1
,
[])
#while not block.end():
#
i, s = block.read(
)
# add $regA, $regA, X -> lw ..., X($regA)
# lw ..., 0($regA
)
# if block.peek():
# block.replace(i, i + 3, [nieuwe statements])
# beq ..., $Lx -> bne ..., $Ly
# j $Ly $Lx:
# $Lx:
#if block.peek(3):
# block.replace(3, [nieuwe statements])
return
filter
(
lambda
s
:
not
equal_mov
(
s
)
and
not
empty_shift
(
s
),
statements
)
return
statements
def
optimize_blocks
(
blocks
):
...
...
This diff is collapsed.
Click to expand it.
src/utils.py
+
4
−
0
View file @
9be472d7
...
...
@@ -11,6 +11,10 @@ class Statement:
"""
Get an argument.
"""
return
self
.
args
[
n
]
def
__setitem__
(
self
,
n
,
value
):
"""
Set an argument.
"""
self
.
args
[
n
]
=
value
def
__eq__
(
self
,
other
):
"""
Check if two statements are equal by comparing their type, name and
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