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
effe7267
Commit
effe7267
authored
Dec 28, 2011
by
Taddeus Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Added common subexpression elimination (unfinished and untested).
parent
c308e7b7
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
96 additions
and
22 deletions
+96
-22
src/optimize/__init__.py
src/optimize/__init__.py
+16
-22
src/optimize/advanced.py
src/optimize/advanced.py
+56
-0
src/statement.py
src/statement.py
+11
-0
tests/test_optimize_advanced.py
tests/test_optimize_advanced.py
+13
-0
No files found.
src/optimize/__init__.py
View file @
effe7267
...
@@ -3,6 +3,7 @@ from src.dataflow import find_basic_blocks
...
@@ -3,6 +3,7 @@ from src.dataflow import find_basic_blocks
from
standard
import
redundant_move_1
,
redundant_move_2
,
\
from
standard
import
redundant_move_1
,
redundant_move_2
,
\
redundant_move_3
,
redundant_move_4
,
redundant_load
,
\
redundant_move_3
,
redundant_move_4
,
redundant_load
,
\
redundant_shift
,
redundant_add
redundant_shift
,
redundant_add
from
advanced
import
eliminate_common_subexpressions
,
fold_constants
def
optimize_global
(
statements
):
def
optimize_global
(
statements
):
...
@@ -30,41 +31,34 @@ def optimize_global(statements):
...
@@ -30,41 +31,34 @@ def optimize_global(statements):
statements
.
replace
(
3
,
[
s
,
label
])
statements
.
replace
(
3
,
[
s
,
label
])
#def optimize_blocks(blocks):
def
optimize_block
(
block
):
# """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."""
"""Optimize a basic block."""
standard
=
[
redundant_move_1
,
redundant_move_2
,
redundant_move_3
,
\
standard
=
[
redundant_move_1
,
redundant_move_2
,
redundant_move_3
,
\
redundant_move_4
,
redundant_load
,
redundant_shift
,
\
redundant_move_4
,
redundant_load
,
redundant_shift
,
\
redundant_add
]
redundant_add
]
old_len
=
-
1
old_len
=
-
1
while
old_len
!=
len
(
statements
):
# Standard optimizations
old_len
=
len
(
statements
)
while
old_len
!=
len
(
block
):
old_len
=
len
(
block
)
while
not
statements
.
end
():
s
=
statements
.
read
()
#
while
not
block
.
end
():
cont
=
False
s
=
block
.
read
()
for
callback
in
standard
:
for
callback
in
standard
:
if
callback
(
s
,
statements
):
if
callback
(
s
,
block
):
cont
=
True
break
break
if
cont
:
# Advanced optimizations
contin
ue
#changed = Tr
ue
# Other optimizations...
#while changed:
# changed = eliminate_common_subexpressions(block) \
# or fold_constants(block)
while
eliminate_common_subexpressions
(
block
)
\
|
fold_constants
(
block
):
pass
def
optimize
(
statements
,
verbose
=
0
):
def
optimize
(
statements
,
verbose
=
0
):
"""optimization wrapper function, calls global and basic-block level
"""optimization wrapper function, calls global and basic-block level
...
...
src/optimize/advanced.py
0 → 100644
View file @
effe7267
from
src.statement
import
Statement
as
S
def
eliminate_common_subexpressions
(
block
):
"""
Common subexpression elimination:
- Traverse through the statements in reverse order.
- If the statement can be possibly be eliminated, walk further collecting
all other occurrences of the expression until one of the arguments is
assigned in a statement, or the start of the block has been reached.
- If one or more occurrences were found, insert the expression with a new
destination address before the last found occurrence and change all
occurrences to a move instruction from that address.
"""
found
=
False
block
.
reverse_statements
()
while
not
block
.
end
():
s
=
block
.
read
()
if
s
.
is_arith
():
pointer
=
block
.
pointer
last
=
False
args
=
s
[
1
:]
# Collect similar statements
while
not
block
.
end
():
s2
=
block
.
read
()
# Stop if one of the arguments is assigned
if
len
(
s2
)
and
s2
[
0
]
in
args
:
break
# Replace a similar expression by a move instruction
if
s2
.
name
==
s
.
name
and
s2
[
1
:]
==
args
:
block
.
replace
(
1
,
[
S
(
'command'
,
'move'
,
s2
[
0
],
new_reg
)])
last
=
block
.
pointer
# Insert an additional expression with a new destination address
if
last
:
block
.
insert
(
S
(
'command'
,
s
.
name
,
[
new_reg
]
+
args
),
last
)
found
=
True
# Reset pointer to and continue from the original statement
block
.
pointer
=
pointer
block
.
reverse_statements
()
return
found
def
fold_constants
(
block
):
"""
Constant folding:
"""
return
False
src/statement.py
View file @
effe7267
...
@@ -139,7 +139,18 @@ class Block:
...
@@ -139,7 +139,18 @@ class Block:
self.statements = before + replacement + after
self.statements = before + replacement + after
self.pointer = start + len(replacement)
self.pointer = start + len(replacement)
def insert(self, statement, index=None):
if index == None:
index = self.pointer
self.statements.insert(index, statement)
def apply_filter(self, callback):
def apply_filter(self, callback):
"""Apply a filter to the statement list. If the callback returns True,
"""Apply a filter to the statement list. If the callback returns True,
the statement will remain in the list.."""
the statement will remain in the list.."""
self.statements = filter(callback, self.statements)
self.statements = filter(callback, self.statements)
def reverse_statements(self):
"""Reverse the statement list and reset the pointer."""
self.statements = self.statements[::-1]
self.pointer = 0
tests/test_optimize_advanced.py
0 → 100644
View file @
effe7267
import
unittest
from
src.optimize.advanced
import
eliminate_common_subexpressions
from
src.statement
import
Statement
as
S
,
Block
as
B
class
TestOptimizeAdvanced
(
unittest
.
TestCase
):
def
setUp
(
self
):
pass
def
test_eliminate_common_subexpressions
(
self
):
pass
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