forked from tkoptimizer/Assembly-code-optimizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subExpressionElimination.py
78 lines (56 loc) · 2.31 KB
/
subExpressionElimination.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
"""
File: subExpressionElimination.py
Authors: Tim van Deurzen, Koos van Strien
Date: 26-02-2010
"""
from basicblock import *
from operations_new import *
from optimizationClass import *
class subExpressionElimination(optimizationClass):
"""
Remove expressions that are duplicated while the result is still stored in a
variable.
"""
def __init__(self, blocks):
"""
Initializes all the necessary variables.
"""
self.name = "Common subexpression elimination"
self.optimizedBlocks = blocks
self.output = []
self.expressions = []
def findCommonExpression(self, operation):
for expression in self.expressions:
if expression.operation == operation.operation and \
expression.getArguments() == operation.getArguments():
return expression
return None
def getUpdatingExpression(self, registers):
for register in registers:
for expression in self.expressions:
arguments = expression.getArguments()
if not registers == arguments and register in arguments:
return expression
return None
def analyseBasicBlock(self, block):
self.expressions = []
for operation in block.operations:
if operation.included == False:
self.output.append(" {{ operation previously excluded: " +
operation.code + " }}")
continue
if operation.hasArguments():
updatingExpression = \
self.getUpdatingExpression(operation.getArguments())
else:
updatingExpression = None
if operation.type in (operation.INT_ARITHMETIC,
operation.FLOAT_ARITHMETIC):
commonExpression = self.findCommonExpression(operation)
if commonExpression is not None:
operation.exclude()
elif updatingExpression is None:
self.expressions.append(operation)
if operation.type == operation.LOAD:
if updatingExpression is not None:
self.expressions.remove(updatingExpression)