Commit 24c91ff6 authored by Taddeus Kroes's avatar Taddeus Kroes

Moved optimization loop to program class.

parent 69a922ff
#!/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])
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))
......@@ -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))
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment