Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
peephole
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Taddeüs Kroes
peephole
Commits
2f22e708
Commit
2f22e708
authored
Dec 28, 2011
by
Taddeus Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Moved main optimization functions to __init__ file.
parent
fb30dd2f
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
97 additions
and
96 deletions
+97
-96
src/optimize/__init__.py
src/optimize/__init__.py
+96
-0
src/optimize/main.py
src/optimize/main.py
+0
-95
tests/test_optimize.py
tests/test_optimize.py
+1
-1
No files found.
src/optimize/__init__.py
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
src/optimize/main.py
deleted
100644 → 0
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
tests/test_optimize.py
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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment