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
ead1b572
Commit
ead1b572
authored
13 years ago
by
Taddeus Kroes
Browse files
Options
Downloads
Patches
Plain Diff
Code cleanup.
parent
42be1bbf
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/optimizer.py
+18
-25
18 additions, 25 deletions
src/optimizer.py
with
18 additions
and
25 deletions
src/optimizer.py
+
18
−
25
View file @
ead1b572
def
equal_mov
(
s
tatement
):
def
equal_mov
(
s
):
'''
Check for useless move operations.
'''
'''
Check for useless move operations.
'''
stype
,
name
,
args
=
statement
return
s
.
is_command
()
and
s
.
name
==
'
move
'
and
s
[
0
]
==
s
[
1
]
return
stype
==
'
command
'
and
name
==
'
move
'
\
and
args
[
'
args
'
][
0
]
==
args
[
'
args
'
][
1
]
def
empty_shift
(
s
):
def
empty_shift
(
statement
):
'''
Check for useless shift operations.
'''
'''
Check for useless shift operations.
'''
shift_types
=
[
'
sll
'
,
'
sla
'
,
'
srl
'
,
'
sra
'
]
return
s
.
is_shift
()
and
s
[
0
]
==
s
[
1
]
and
s
[
2
]
==
0
stype
,
name
,
args
=
statement
return
stype
==
'
command
'
and
name
in
shift_types
and
\
args
[
'
args
'
][
0
]
==
args
[
'
args
'
][
1
]
and
args
[
'
args
'
][
2
]
==
0
def
optimize_branch_jump_label
(
statements
):
def
optimize_branch_jump_label
(
statements
):
'''
Optimize jumps after branches.
'''
'''
Optimize jumps after branches.
'''
out_statements
=
[]
out_statements
=
[]
for
i
in
xrange
(
len
(
statements
)):
for
i
in
xrange
(
len
(
statements
)):
if
i
+
3
>
len
(
statements
):
if
i
+
3
>
len
(
statements
):
out_statements
.
append
(
statements
[
i
])
out_statements
.
append
(
statements
[
i
])
continue
continue
stype
,
name
,
args
=
statements
[
i
]
stype
,
name
,
args
=
statements
[
i
]
stype2
,
name2
,
args2
=
statements
[
i
+
1
]
stype2
,
name2
,
args2
=
statements
[
i
+
1
]
stype3
,
name3
,
args3
=
statements
[
i
+
2
]
stype3
,
name3
,
args3
=
statements
[
i
+
2
]
if
stype
==
'
command
'
and
name
==
'
beq
'
and
\
if
stype
==
'
command
'
and
name
==
'
beq
'
and
\
stype2
==
'
command
'
and
name2
in
[
'
j
'
,
'
jal
'
]
and
\
stype2
==
'
command
'
and
name2
in
[
'
j
'
,
'
jal
'
]
and
\
stype3
==
'
label
'
and
name3
==
args
[
'
args
'
][
2
]:
stype3
==
'
label
'
and
name3
==
args
[
'
args
'
][
2
]:
...
@@ -33,45 +28,43 @@ def optimize_branch_jump_label(statements):
...
@@ -33,45 +28,43 @@ def optimize_branch_jump_label(statements):
i
+=
3
i
+=
3
else
:
else
:
out_statements
.
append
(
statements
[
i
])
out_statements
.
append
(
statements
[
i
])
return
out_statements
return
out_statements
def
optimize_global
(
statements
):
def
optimize_global
(
statements
):
'''
Optimize one-line statements in entire code.
'''
'''
Optimize one-line statements in entire code.
'''
statements
=
optimize_branch_jump_label
(
statements
)
statements
=
optimize_branch_jump_label
(
statements
)
return
filter
(
lambda
s
:
not
equal_mov
(
s
)
and
not
empty_shift
(
s
),
statements
)
return
filter
(
lambda
s
:
not
equal_mov
(
s
)
and
not
empty_shift
(
s
),
statements
)
def
optimize_blocks
(
blocks
):
def
optimize_blocks
(
blocks
):
'''
Call the optimizer for each basic block. Do this several times until
'''
Call the optimizer for each basic block. Do this several times until
no more optimizations are achieved.
'''
no more optimizations are achieved.
'''
changed
=
True
changed
=
True
while
changed
:
while
changed
:
changed
=
False
changed
=
False
optimized
=
[]
optimized
=
[]
for
block
in
blocks
:
for
block
in
blocks
:
block_changed
,
b
=
optimize_block
(
block
)
block_changed
,
b
=
optimize_block
(
block
)
optimized
.
append
(
b
)
optimized
.
append
(
b
)
if
block_changed
:
if
block_changed
:
changed
=
True
changed
=
True
blocks
=
optimized
blocks
=
optimized
return
reduce
(
lambda
a
,
b
:
a
+
b
,
blocks
,
[])
return
reduce
(
lambda
a
,
b
:
a
+
b
,
blocks
,
[])
def
optimize_block
(
statements
):
def
optimize_block
(
statements
):
'''
Optimize a basic block.
'''
'''
Optimize a basic block.
'''
changed
=
False
changed
=
False
output_statements
=
[]
output_statements
=
[]
for
statement
in
statements
:
for
statement
in
statements
:
new_statement
=
statement
new_statement
=
statement
output_statements
.
append
(
new_statement
)
output_statements
.
append
(
new_statement
)
return
changed
,
output_statements
return
changed
,
output_statements
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