-
Notifications
You must be signed in to change notification settings - Fork 0
/
task6_integration_of_optimisation.py
92 lines (77 loc) · 2.37 KB
/
task6_integration_of_optimisation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
from common import *
from task1_control_flow_graph import *
from task2_unreachable_code_elimination import *
from task3_jump_elimination import *
from task4_constant_propagation import *
from task6_integration_of_optimisation import *
def optSequence(graph, debug): #takes in original code, runs it though each optimisation
if debug:
print '--------------------------------'
print 'original code'
print optimisedCode(graph)
print
print '--------------------------------'
print 'trimming unreachable code'
task2 = trimNodes(graph)
task2 = optimisedCode(task2)
print task2
print
print '--------------------------------'
print 'optimising jumps'
task3 = jump_elimination(task2)
# task3 = parse(task2)
print optimisedCode(task3)
print
print '--------------------------------'
print 'constant propagation and folding'
task4 = processConstants(task3)
print optimisedCode(task4)
print
print '--------------------------------'
print 'dead code elimination'
task5 = optimisedCode(task4)
print
print '--------------------------------'
else:
return optimisedCode (
processConstants (
jump_elimination (
optimisedCode(trimNodes(graph))
)
)
)
return task5
def optimiseAll(graph, debug = False): #takes in the original graph of the code, prints out each phase of optimisation
# debug = True
print '--original code---'
print optimisedCode(graph)
print '#of lines =', optimisedCode(graph).count('\n')
print '######beginning optimisation######'
optCode = [optSequence(graph, debug)]
print 'iteration #1'
print optCode[-1]
print
print '#of lines =', optCode[-1].count('\n')
print '---------------'
optCode += [optSequence(parse(optCode[-1]), debug)]
print 'iteration #2'
print optCode[-1]
print
print '#of lines =', optCode[-1].count('\n')
print '---------------'
iterationCount = 2
while optCode[-1].count('\n') < optCode[-2].count('\n'): #hill climbing
iterationCount += 1
optCode += [optSequence(parse(optCode[-1]), debug)]
print 'iteration #' + str(iterationCount)
print optCode[-1]
print
print '#of lines =', optCode[-1].count('\n')
print '---------------'
print '-----last iteration reached-----'
print optCode[-1]
print
print '#of lines =', optCode[-1].count('\n')
return optCode[-1]
if __name__ == "__main__":
optimiseAll(parse(testinput3))