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
Hide 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):
...
@@ -14,6 +14,7 @@ def optimize(program, verbose=0):
changed
=
True
changed
=
True
while
changed
:
while
changed
:
print
'main iteration'
changed
=
False
changed
=
False
# Optimize on a global level
# Optimize on a global level
...
...
src/optimize/redundancies.py
View file @
e0b4e598
...
@@ -124,45 +124,45 @@ def add_lw(add, statements):
...
@@ -124,45 +124,45 @@ def add_lw(add, statements):
return
True
return
True
def
remove_redundant_jumps
(
statements
):
def
remove_redundant_jumps
(
statements
):
"""Remove jump if label follows immediatly."""
"""Remove jump if label follows immediately."""
old_len
=
-
1
changed
=
False
while
old_len
!=
len
(
statements
):
old_len
=
len
(
statements
)
while
not
statements
.
end
():
statements
.
reset
()
s
=
statements
.
read
()
while
not
statements
.
end
():
s
=
statements
.
read
()
# j $Lx -> $Lx:
# $Lx:
# j $Lx -> $Lx:
if
s
.
is_command
(
'j'
):
# $Lx:
following
=
statements
.
peek
(
2
)
if
s
.
is_command
(
'j'
):
following
=
statements
.
peek
()
if
following
.
is_label
(
s
[
0
]):
statements
.
replace
(
1
,
[])
if
following
.
is_label
(
s
[
0
]):
statements
.
replace
(
1
,
[])
changed
=
True
return
True
def
remove_redundant_branch_jumps
(
statements
):
def
remove_redundant_branch_jumps
(
statements
):
"""Optimize statement sequences on a global level."""
"""Optimize statement sequences on a global level."""
old_len
=
-
1
changed
=
False
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
])
statements
.
reset
()
statements
.
reset
()
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
])
changed
=
True
return
changed
src/program.py
View file @
e0b4e598
from
statement
import
Statement
as
S
,
Block
from
statement
import
Statement
as
S
,
Block
from
dataflow
import
find_basic_blocks
,
generate_flow_graph
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
,
\
from
optimize.advanced
import
eliminate_common_subexpressions
,
\
fold_constants
,
copy_propagation
,
eliminate_dead_code
fold_constants
,
copy_propagation
,
eliminate_dead_code
from
writer
import
write_statements
from
writer
import
write_statements
...
@@ -65,42 +66,24 @@ class Program(Block):
...
@@ -65,42 +66,24 @@ class Program(Block):
if
not
hasattr
(
self
,
'statements'
):
if
not
hasattr
(
self
,
'statements'
):
self
.
statements
=
self
.
get_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
):
def
optimize_blocks
(
self
):
"""Optimize on block level. Keep executing all optimizations until no
"""Optimize on block level. Keep executing all optimizations until no
more changes occur."""
more changes occur."""
self
.
program_iterations
=
self
.
block_iterations
=
0
changed
=
False
program_changed
=
True
for
block
in
self
.
blocks
:
while
program_changed
:
print
'block iteration'
self
.
program_iterations
+=
1
if
remove_redundancies
(
block
)
\
program_changed
=
False
|
eliminate_common_subexpressions
(
block
)
\
|
fold_constants
(
block
)
\
for
block
in
self
.
blocks
:
|
copy_propagation
(
block
)
\
self
.
block_iterations
+=
1
|
eliminate_dead_code
(
block
):
block_changed
=
True
changed
=
True
while
block_changed
:
return
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
def
find_basic_blocks
(
self
):
def
find_basic_blocks
(
self
):
"""Divide the statement list into basic blocks."""
"""Divide the statement list into basic blocks."""
...
...
tests/test_liveness.py
View file @
e0b4e598
...
@@ -63,3 +63,49 @@ class TestLiveness(unittest.TestCase):
...
@@ -63,3 +63,49 @@ class TestLiveness(unittest.TestCase):
self
.
assertEqual
(
b2
.
live_out
,
set
([
'b'
,
'd'
]))
self
.
assertEqual
(
b2
.
live_out
,
set
([
'b'
,
'd'
]))
self
.
assertEqual
(
b3
.
live_in
,
set
([
'b'
,
'd'
]))
self
.
assertEqual
(
b3
.
live_in
,
set
([
'b'
,
'd'
]))
self
.
assertEqual
(
b3
.
live_out
,
set
())
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
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.program
import
Program
from
src.statement
import
Statement
as
S
,
Block
as
B
from
src.statement
import
Statement
as
S
,
Block
as
B
...
@@ -199,12 +200,12 @@ class TestOptimize(unittest.TestCase):
...
@@ -199,12 +200,12 @@ class TestOptimize(unittest.TestCase):
S
(
'command'
,
'j'
,
'$L1'
),
S
(
'command'
,
'j'
,
'$L1'
),
S
(
'label'
,
'$L1'
),
S
(
'label'
,
'$L1'
),
self
.
bar
])
self
.
bar
])
remove_redundancie
s
(
block
)
remove_redundant_jump
s
(
block
)
self
.
assertEqual
(
block
.
statements
,
B
(
[
self
.
foo
,
self
.
assertEqual
(
block
.
statements
,
[
self
.
foo
,
S
(
'
command'
,
'j
'
,
'$L1'
),
S
(
'
label
'
,
'$L1'
),
self
.
bar
])
)
self
.
bar
])
def
test_remove_redundant_jumps_false
(
self
):
def
test_remove_redundant_jumps_false
(
self
):
arguments
=
[
self
.
foo
,
arguments
=
[
self
.
foo
,
...
@@ -213,9 +214,9 @@ class TestOptimize(unittest.TestCase):
...
@@ -213,9 +214,9 @@ class TestOptimize(unittest.TestCase):
self
.
bar
]
self
.
bar
]
block
=
B
(
arguments
)
block
=
B
(
arguments
)
remove_redundancie
s
(
block
)
remove_redundant_jump
s
(
block
)
self
.
assertEqual
(
block
.
statements
,
arguments
)
self
.
assertEqual
(
block
.
statements
,
arguments
)
def
test_remove_redundant_branch_jumps_beq_j_true
(
self
):
def
test_remove_redundant_branch_jumps_beq_j_true
(
self
):
block
=
B
([
self
.
foo
,
block
=
B
([
self
.
foo
,
...
...
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