Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
peephole
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
Taddeüs Kroes
peephole
Commits
24c91ff6
Commit
24c91ff6
authored
13 years ago
by
Taddeus Kroes
Browse files
Options
Downloads
Patches
Plain Diff
Moved optimization loop to program class.
parent
69a922ff
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
main.py
+24
-17
24 additions, 17 deletions
main.py
src/optimize/__init__.py
+0
-49
0 additions, 49 deletions
src/optimize/__init__.py
src/program.py
+43
-0
43 additions, 0 deletions
src/program.py
with
67 additions
and
66 deletions
main.py
+
24
−
17
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
])
This diff is collapsed.
Click to expand it.
src/optimize/__init__.py
+
0
−
49
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
))
This diff is collapsed.
Click to expand it.
src/program.py
+
43
−
0
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
))
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment