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
e0b4e598
Commit
e0b4e598
authored
Dec 30, 2011
by
Taddeus Kroes
Browse files
Options
Browse Files
Download
Plain Diff
Worked on optimization loop.
parents
e6324624
15bbae43
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
108 additions
and
77 deletions
+108
-77
src/optimize/__init__.py
src/optimize/__init__.py
+1
-0
src/optimize/redundancies.py
src/optimize/redundancies.py
+36
-36
src/program.py
src/program.py
+16
-33
tests/test_liveness.py
tests/test_liveness.py
+46
-0
tests/test_optimize.py
tests/test_optimize.py
+9
-8
No files found.
src/optimize/__init__.py
View file @
e0b4e598
...
...
@@ -14,6 +14,7 @@ def optimize(program, verbose=0):
changed
=
True
while
changed
:
print
'main iteration'
changed
=
False
# Optimize on a global level
...
...
src/optimize/redundancies.py
View file @
e0b4e598
...
...
@@ -124,30 +124,29 @@ def add_lw(add, statements):
return
True
def
remove_redundant_jumps
(
statements
):
"""Remove jump if label follows immediatly."""
old_len
=
-
1
while
old_len
!=
len
(
statements
):
old_len
=
len
(
statements
)
"""Remove jump if label follows immediately."""
changed
=
False
statements
.
reset
()
while
not
statements
.
end
():
s
=
statements
.
read
()
# j $Lx -> $Lx:
# $Lx:
if
s
.
is_command
(
'j'
):
following
=
statements
.
peek
(
2
)
following
=
statements
.
peek
(
)
if
following
.
is_label
(
s
[
0
]):
statements
.
replace
(
1
,
[])
changed
=
True
return
True
def
remove_redundant_branch_jumps
(
statements
):
"""Optimize statement sequences on a global level."""
old_len
=
-
1
while
old_len
!=
len
(
statements
):
old_len
=
len
(
statements
)
changed
=
False
statements
.
reset
()
while
not
statements
.
end
():
s
=
statements
.
read
()
...
...
@@ -164,5 +163,6 @@ def remove_redundant_branch_jumps(statements):
s
.
name
=
'bne'
if
s
.
is_command
(
'beq'
)
else
'beq'
s
[
2
]
=
j
[
0
]
statements
.
replace
(
3
,
[
s
,
label
])
changed
=
True
statements
.
reset
()
return
changed
src/program.py
View file @
e0b4e598
from
statement
import
Statement
as
S
,
Block
from
dataflow
import
find_basic_blocks
,
generate_flow_graph
from
optimize.redundancies
import
remove_redundant_jumps
,
remove_redundancies
from
optimize.redundancies
import
remove_redundant_jumps
,
remove_redundancies
,
\
remove_redundant_branch_jumps
from
optimize.advanced
import
eliminate_common_subexpressions
,
\
fold_constants
,
copy_propagation
,
eliminate_dead_code
from
writer
import
write_statements
...
...
@@ -65,42 +66,24 @@ class Program(Block):
if
not
hasattr
(
self
,
'statements'
):
self
.
statements
=
self
.
get_statements
()
return
remove_redundant_jumps
(
self
)
return
remove_redundant_jumps
(
self
)
\
|
remove_redundant_branch_jumps
(
self
)
def
optimize_blocks
(
self
):
"""Optimize on block level. Keep executing all optimizations until no
more changes occur."""
self
.
program_iterations
=
self
.
block_iterations
=
0
program_changed
=
True
while
program_changed
:
self
.
program_iterations
+=
1
program_changed
=
False
changed
=
False
for
block
in
self
.
blocks
:
self
.
block_iterations
+=
1
block_changed
=
True
while
block_changed
:
block_changed
=
False
if
remove_redundancies
(
block
):
block_changed
=
True
if
eliminate_common_subexpressions
(
block
):
block_changed
=
True
if
fold_constants
(
block
):
block_changed
=
True
if
copy_propagation
(
block
):
block_changed
=
True
if
eliminate_dead_code
(
block
):
block_changed
=
True
if
block_changed
:
program_changed
=
True
print
'block iteration'
if
remove_redundancies
(
block
)
\
|
eliminate_common_subexpressions
(
block
)
\
|
fold_constants
(
block
)
\
|
copy_propagation
(
block
)
\
|
eliminate_dead_code
(
block
):
changed
=
True
return
changed
def
find_basic_blocks
(
self
):
"""Divide the statement list into basic blocks."""
...
...
tests/test_liveness.py
View file @
e0b4e598
...
...
@@ -63,3 +63,49 @@ class TestLiveness(unittest.TestCase):
self
.
assertEqual
(
b2
.
live_out
,
set
([
'b'
,
'd'
]))
self
.
assertEqual
(
b3
.
live_in
,
set
([
'b'
,
'd'
]))
self
.
assertEqual
(
b3
.
live_out
,
set
())
# def test_create_in_out_two(self):
# s11 = S('command', 'subu', 'i', 'm', '0x00000001')
# s12 = S('command', 'move', 'j', 'n')
# s13 = S('command', 'move', 'a', 'u1')
# s14 = S('command', 'subu', 'i', 'm', '0x00000001')
# s15 = S('command', 'j', 'L1')
# s16 = S('')
#
# s21 = S('label', 'L1')
# s22 = S('command', 'addi', 'i', '0x00000001')
# s23 = S('command', 'subi', 'j', '0x00000001')
# s24 = S('command', 'j', 'L2')
#
# s31 = S('label', 'L2')
# s32 = S('command', 'move', 'a', 'u2')
# s33 = S('command', 'j', 'L1')
# s41 = S('label', 'L3')
# s42 = S('command', 'move', 'i', 'u3')
# s43 = S('command', 'beq', 'g', 'd', 'L4')
#
# s51 = S('label', 'L4')
# s52 = S('command', 'addu', 'b', 'i', 'a')
#
# b1, b2, b3, b4 = find_basic_blocks([s11, s12, s13, s14, s15, s21, s22,\
# s31, s32, s33, s41, s42, s43, s51])
# generate_flow_graph([b1, b2, b3, b4, b5])
# create_in_out([b1, b2, b3, b4, b5])
#
#
#self.assertEqual(b1.)
tests/test_optimize.py
View file @
e0b4e598
import
unittest
from
src.optimize.redundancies
import
remove_redundancies
,
remove_redundant_jumps
from
src.optimize.redundancies
import
remove_redundancies
,
\
remove_redundant_jumps
,
remove_redundant_branch_jumps
from
src.program
import
Program
from
src.statement
import
Statement
as
S
,
Block
as
B
...
...
@@ -200,11 +201,11 @@ class TestOptimize(unittest.TestCase):
S
(
'label'
,
'$L1'
),
self
.
bar
])
remove_redundancie
s
(
block
)
remove_redundant_jump
s
(
block
)
self
.
assertEqual
(
block
.
statements
,
B
(
[
self
.
foo
,
S
(
'
command'
,
'j
'
,
'$L1'
),
self
.
bar
])
)
self
.
assertEqual
(
block
.
statements
,
[
self
.
foo
,
S
(
'
label
'
,
'$L1'
),
self
.
bar
])
def
test_remove_redundant_jumps_false
(
self
):
arguments
=
[
self
.
foo
,
...
...
@@ -213,7 +214,7 @@ class TestOptimize(unittest.TestCase):
self
.
bar
]
block
=
B
(
arguments
)
remove_redundancie
s
(
block
)
remove_redundant_jump
s
(
block
)
self
.
assertEqual
(
block
.
statements
,
arguments
)
...
...
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