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
8c1d9515
Commit
8c1d9515
authored
Dec 28, 2011
by
Taddeus Kroes
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'master' of github.com:taddeus/peephole
parents
312aa1f0
8303fe0b
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
38 additions
and
1 deletion
+38
-1
src/optimize/advanced.py
src/optimize/advanced.py
+27
-0
tests/test_optimize_advanced.py
tests/test_optimize_advanced.py
+11
-1
No files found.
src/optimize/advanced.py
View file @
8c1d9515
...
@@ -208,3 +208,30 @@ def copy_propagation(block):
...
@@ -208,3 +208,30 @@ def copy_propagation(block):
changed
=
True
changed
=
True
return
changed
return
changed
def
algebraic_transformations
(
block
):
"""
Change ineffective or useless algebraic transformations. Handled are:
- x = x + 0 -> remove
- x = x - 0 -> remove
- x = x * 1 -> remove
- x = x * 2 -> x = x << 1
"""
changed
=
False
while
not
block
.
end
():
changed
=
True
s
=
block
.
read
()
if
(
s
.
is_command
(
'addu'
)
or
s
.
is_command
(
'subu'
))
and
s
[
2
]
==
0
:
block
.
replace
(
1
,
[])
elif
s
.
is_command
(
'mult'
)
and
s
[
2
]
==
1
:
block
.
replace
(
1
,
[])
elif
s
.
is_command
(
'mult'
)
and
s
[
2
]
==
2
:
new_command
=
S
([
'command'
,
'sll'
,
s
[
0
],
s
[
1
],
1
])
block
.
replace
(
1
,
[
new_command
])
else
:
changed
=
False
return
changed
tests/test_optimize_advanced.py
View file @
8c1d9515
import
unittest
import
unittest
from
src.optimize.advanced
import
eliminate_common_subexpressions
,
\
from
src.optimize.advanced
import
eliminate_common_subexpressions
,
\
fold_constants
,
copy_propagation
fold_constants
,
copy_propagation
,
algebraic_transformations
from
src.statement
import
Statement
as
S
,
Block
as
B
from
src.statement
import
Statement
as
S
,
Block
as
B
...
@@ -70,3 +70,13 @@ class TestOptimizeAdvanced(unittest.TestCase):
...
@@ -70,3 +70,13 @@ class TestOptimizeAdvanced(unittest.TestCase):
block
=
B
(
arguments
)
block
=
B
(
arguments
)
self
.
assertFalse
(
copy_propagation
(
block
))
self
.
assertFalse
(
copy_propagation
(
block
))
self
.
assertEqual
(
block
.
statements
,
arguments
)
self
.
assertEqual
(
block
.
statements
,
arguments
)
def
test_algebraic_transforms_add0
(
self
):
block
=
B
([
self
.
foo
,
S
(
'command'
,
'addu'
,
'$1'
,
'$2'
,
0
),
self
.
bar
])
# self.assertTrue(copy_propagation(block))
algebraic_transformations
(
block
)
self
.
assertEqual
(
block
.
statements
,
[
self
.
foo
,
self
.
bar
])
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