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
2f22e708
Commit
2f22e708
authored
13 years ago
by
Taddeus Kroes
Browse files
Options
Downloads
Patches
Plain Diff
Moved main optimization functions to __init__ file.
parent
fb30dd2f
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/__init__.py
+96
-0
96 additions, 0 deletions
src/optimize/__init__.py
src/optimize/main.py
+0
-95
0 additions, 95 deletions
src/optimize/main.py
tests/test_optimize.py
+1
-1
1 addition, 1 deletion
tests/test_optimize.py
with
97 additions
and
96 deletions
src/optimize/__init__.py
+
96
−
0
View file @
2f22e708
from
src.dataflow
import
find_basic_blocks
from
glob
import
redundant_move_1
,
redundant_move_2
,
\
redundant_move_3
,
redundant_move_4
,
redundant_load
,
\
redundant_shift
,
redundant_add
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
'
,
'
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
])
#def optimize_blocks(blocks):
# """Call the optimizer for each basic block. Do this several times until
# no more optimizations are achieved."""
# for block in blocks:
# optimize_block(block)
#
# return blocks
def
optimize_block
(
statements
):
"""
Optimize a basic block.
"""
glob
=
[
redundant_move_1
,
redundant_move_2
,
redundant_move_3
,
\
redundant_move_4
,
redundant_load
,
redundant_shift
,
redundant_add
]
old_len
=
-
1
while
old_len
!=
len
(
statements
):
old_len
=
len
(
statements
)
while
not
statements
.
end
():
s
=
statements
.
read
()
cont
=
False
for
callback
in
glob
:
if
callback
(
s
,
statements
):
cont
=
True
break
if
cont
:
continue
# Other optimizations...
def
optimize
(
statements
,
verbose
=
0
):
"""
optimization wrapper function, calls global and basic-block level
optimization functions.
"""
# Optimize on a global level
o
=
len
(
statements
)
optimize_global
(
statements
)
g
=
len
(
statements
)
# Optimize basic blocks
blocks
=
find_basic_blocks
(
statements
)
map
(
optimize_block
,
blocks
)
block_statements
=
map
(
lambda
b
:
b
.
statements
,
blocks
)
opt_blocks
=
reduce
(
lambda
a
,
b
:
a
+
b
,
block_statements
)
b
=
len
(
opt_blocks
)
# - Common subexpression elimination
# - Constant folding
# - Copy propagation
# - Dead-code elimination
# - Temporary variable renaming
# - Interchange of independent statements
if
verbose
:
print
'
Original statements: %d
'
%
o
print
'
After global optimization: %d
'
%
g
print
'
After basic blocks optimization: %d
'
%
b
print
'
Optimization: %d (%d%%)
'
\
%
(
b
-
o
,
int
((
b
-
o
)
/
float
(
o
)
*
100
))
return
opt_blocks
This diff is collapsed.
Click to expand it.
src/optimize/main.py
deleted
100644 → 0
+
0
−
95
View file @
fb30dd2f
from
src.dataflow
import
find_basic_blocks
from
glob
import
redundant_move_1
,
redundant_move_2
,
\
redundant_move_3
,
redundant_move_4
,
redundant_load
,
\
redundant_shift
,
redundant_add
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
'
,
'
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
])
#def optimize_blocks(blocks):
# """Call the optimizer for each basic block. Do this several times until
# no more optimizations are achieved."""
# for block in blocks:
# optimize_block(block)
#
# return blocks
def
optimize_block
(
statements
):
"""
Optimize a basic block.
"""
glob
=
[
redundant_move_1
,
redundant_move_2
,
redundant_move_3
,
\
redundant_move_4
,
redundant_load
,
redundant_shift
,
redundant_add
]
old_len
=
-
1
while
old_len
!=
len
(
statements
):
old_len
=
len
(
statements
)
while
not
statements
.
end
():
s
=
statements
.
read
()
cont
=
False
for
callback
in
glob
:
if
callback
(
s
,
statements
):
cont
=
True
break
if
cont
:
continue
# Other optimizations...
def
optimize
(
statements
,
verbose
=
0
):
"""
optimization wrapper function, calls global and basic-block level
optimization functions.
"""
# Optimize on a global level
o
=
len
(
statements
)
optimize_global
(
statements
)
g
=
len
(
statements
)
# Optimize basic blocks
blocks
=
find_basic_blocks
(
statements
)
map
(
optimize_block
,
blocks
)
block_statements
=
map
(
lambda
b
:
b
.
statements
,
blocks
)
opt_blocks
=
reduce
(
lambda
a
,
b
:
a
+
b
,
block_statements
)
b
=
len
(
opt_blocks
)
# - Common subexpression elimination
# - Constant folding
# - Copy propagation
# - Dead-code elimination
# - Temporary variable renaming
# - Interchange of independent statements
if
verbose
:
print
'
Original statements: %d
'
%
o
print
'
After global optimization: %d
'
%
g
print
'
After basic blocks optimization: %d
'
%
b
print
'
Optimization: %d (%d%%)
'
\
%
(
b
-
o
,
int
((
b
-
o
)
/
float
(
o
)
*
100
))
return
opt_blocks
This diff is collapsed.
Click to expand it.
tests/test_optimize.py
+
1
−
1
View file @
2f22e708
import
unittest
from
src.optimize
.main
import
optimize_global
,
optimize_block
#, optimize_blocks
from
src.optimize
import
optimize_global
,
optimize_block
#, optimize_blocks
from
src.statement
import
Statement
as
S
,
Block
as
B
...
...
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