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
24c91ff6
Commit
24c91ff6
authored
Dec 31, 2011
by
Taddeus Kroes
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Moved optimization loop to program class.
parent
69a922ff
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
67 additions
and
66 deletions
+67
-66
main.py
main.py
+24
-17
src/optimize/__init__.py
src/optimize/__init__.py
+0
-49
src/program.py
src/program.py
+43
-0
No files found.
main.py
View file @
24c91ff6
#!/usr/bin/python
from
sys
import
argv
,
exit
from
src.parser
import
parse_file
from
src.optimize
import
optimize
if
__name__
==
'__main__'
:
from
sys
import
argv
,
exit
verbose_level
=
1
# Parse arguments
if
len
(
argv
)
<
2
:
print
'Usage: python %s SOURCE_FILE [ OUT_FILE [ SOURCE_OUT_FILE ] ]'
\
%
argv
[
0
]
exit
(
1
)
# Parse file
program
=
parse_file
(
argv
[
1
])
program
.
verbose
=
verbose_level
if
len
(
argv
)
<
2
:
print
'Usage: python %s SOURCE_FILE [ OUT_FILE [ SOURCE_OUT_FILE ] ]'
\
%
argv
[
0
]
exit
(
1
)
# Save input assembly in new file for easy comparison
if
len
(
argv
)
>
3
:
program
.
save
(
argv
[
3
])
# Parse file
program
=
parse_file
(
argv
[
1
])
program
.
verbose
=
1
if
len
(
argv
)
>
3
:
# Save input assembly in new file for easy comparison
program
.
save
(
argv
[
3
])
# Perform optimizations
program
.
optimize
()
optimize
(
program
,
verbose
=
2
)
if
len
(
argv
)
>
2
:
# Save output assembly
program
.
save
(
argv
[
2
])
# Save output assembly
if
len
(
argv
)
>
2
:
program
.
save
(
argv
[
2
])
src/optimize/__init__.py
View file @
24c91ff6
from
src.dataflow
import
find_basic_blocks
,
generate_flow_graph
from
redundancies
import
remove_redundancies
from
advanced
import
eliminate_common_subexpressions
,
fold_constants
,
\
copy_propagation
,
eliminate_dead_code
import
src.liveness
as
liveness
import
src.reaching_definitions
as
reaching_definitions
def
optimize
(
program
,
verbose
=
0
):
"""Optimization wrapper function, calls global and basic-block level
optimization functions."""
# Remember original number of statements
o
=
program
.
count_instructions
()
changed
=
True
iterations
=
0
while
changed
:
iterations
+=
1
if
verbose
>
1
:
print
'main iteration %d'
,
iterations
changed
=
False
# Optimize on a global level
if
program
.
optimize_global
():
if
verbose
>
1
:
print
'changed on global level'
changed
=
True
# Perform dataflow analysis on new blocks
program
.
perform_dataflow_analysis
()
# Optimize basic blocks
if
program
.
optimize_blocks
():
if
verbose
>
1
:
print
'changed on block level'
changed
=
True
# Count number of instructions after optimization
b
=
program
.
count_instructions
()
# Print results
if
verbose
:
print
'Original statements: %d'
%
o
print
'Statements removed: %d (%d%%)'
\
%
(
o
-
b
,
int
((
o
-
b
)
/
float
(
b
)
*
100
))
src/program.py
View file @
24c91ff6
...
...
@@ -137,3 +137,46 @@ class Program(Block):
f
.
write
(
write_statements
(
self
.
get_statements
(
True
),
verbose
=
self
.
verbose
))
f
.
close
()
def
optimize
(
self
):
"""Optimization wrapper function, calls global and basic-block level
optimization functions."""
# Remember original number of statements
o
=
self
.
count_instructions
()
changed
=
True
iterations
=
0
while
changed
:
iterations
+=
1
if
self
.
verbose
>
1
:
print
'main iteration %d'
,
iterations
changed
=
False
# Optimize on a global level
if
self
.
optimize_global
():
if
self
.
verbose
>
1
:
print
'changed on global level'
changed
=
True
# Perform dataflow analysis on new blocks
self
.
perform_dataflow_analysis
()
# Optimize basic blocks
if
self
.
optimize_blocks
():
if
self
.
verbose
>
1
:
print
'changed on block level'
changed
=
True
# Count number of instructions after optimization
b
=
self
.
count_instructions
()
# Print results
if
self
.
verbose
:
print
'Original statements: %d'
%
o
print
'Statements removed: %d (%d%%)'
\
%
(
o
-
b
,
int
((
o
-
b
)
/
float
(
b
)
*
100
))
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