-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCSETracker.cpp
193 lines (175 loc) · 5.55 KB
/
CSETracker.cpp
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
//
// Created by tmpark on 2/15/16.
//
#include "CSETracker.h"
CSETracker:: CSETracker()
{
}
shared_ptr<IRFormat> CSETracker::findExistingCommonSub(shared_ptr<IRFormat> targetInst)
{
IROP irOp = targetInst->getIROP();
vector<Result> operands = targetInst->operands;
shared_ptr<IRFormat> currentInstPtr = getCurrentInstPtr(irOp);
while(currentInstPtr != NULL)
{
//Same thing can exist(skip)
if(targetInst->getLineNo() == currentInstPtr->getLineNo()) {
currentInstPtr = currentInstPtr->getPreviousSameOpInst();
continue;
}
if(irOp == IR_load)
{
if(currentInstPtr->getIROP() == IR_store) {
//if the variable of array is the same kill(load var, store ~,var)
if (operands.at(0).getVariableName() == currentInstPtr->operands.at(1).getVariableName())
return NULL;
else {
currentInstPtr = currentInstPtr->getPreviousSameOpInst();
continue;
}
}
//else let them go
}
bool sameInst = isCommonSub(targetInst,currentInstPtr);
//For all operands they should be same
/*
bool sameOperand = true;
for(int i = 0 ; i < operands.size() ;i++)
{
if(!isSameOperand(currentInstPtr->operands.at(i),operands.at(i)))
sameOperand = false;
}
//Special Case handling(for add and mul, reverse order also same)
if(irOp == IR_mul || irOp == IR_add)
{
if(isSameOperand(currentInstPtr->operands.at(0),operands.at(1)) &&
isSameOperand(currentInstPtr->operands.at(1),operands.at(0)))
sameOperand = true;
}*/
//Same operand
if(sameInst)
{
return currentInstPtr;
}
currentInstPtr = currentInstPtr->getPreviousSameOpInst();
}
return NULL;
}
//Fixme: Negate case insert
shared_ptr<IRFormat> CSETracker::getCurrentInstPtr(IROP irOp)
{
switch(irOp)
{
case IR_add:
return currentAddInst;
// case IR_adda:
// return currentAddaInst;
case IR_sub:
return currentSubInst;
case IR_mul:
return currentMulInst;
case IR_div:
return currentDivInst;
case IR_neg:
return currentNegInst;
case IR_cmp:
return currentCmpInst;
case IR_load:
return currentLoadInst;
case IR_store:
return currentLoadInst;
default:
return NULL;
}
}
void CSETracker::setCurrentInst(IROP irOp, shared_ptr<IRFormat> currentInst)
{
switch(irOp)
{
case IR_add:
currentAddInst = currentInst;
break;
// case IR_adda:
// currentAddaInst = currentInst;
// break;
case IR_sub:
currentSubInst = currentInst;
break;
case IR_mul:
currentMulInst = currentInst;
break;
case IR_div:
currentDivInst = currentInst;
break;
case IR_neg:
currentNegInst = currentInst;
break;
case IR_cmp:
currentCmpInst = currentInst;
break;
case IR_load:
currentLoadInst = currentInst;
break;
case IR_store:
currentLoadInst = currentInst;
break;
default:
break;
}
}
void CSETracker:: revertToOuter(int blockNum, bool endOfInnerBlock)
{
while(!(currentAddInst == NULL || currentAddInst->getBlkNo() <= blockNum))
currentAddInst = currentAddInst->getPreviousSameOpInst();
while(!(currentSubInst == NULL || currentSubInst->getBlkNo() <= blockNum))
currentSubInst = currentSubInst->getPreviousSameOpInst();
while(!(currentMulInst == NULL || currentMulInst->getBlkNo() <= blockNum))
currentMulInst = currentMulInst->getPreviousSameOpInst();
while(!(currentDivInst == NULL || currentDivInst->getBlkNo() <= blockNum))
currentDivInst = currentDivInst->getPreviousSameOpInst();
// while(!(currentAddaInst == NULL || currentAddaInst->getBlkNo() <= blockNum))
// currentAddaInst = currentAddaInst->getPreviousSameOpInst();
while(!(currentNegInst == NULL || currentNegInst->getBlkNo() <= blockNum))
currentNegInst = currentNegInst->getPreviousSameOpInst();
while(!(currentCmpInst == NULL || currentCmpInst->getBlkNo() <= blockNum))
currentCmpInst = currentCmpInst->getPreviousSameOpInst();
while(!(currentLoadInst == NULL || currentLoadInst->getBlkNo() <= blockNum)) {
if(currentLoadInst->getIROP() == IR_store)
killingStores.push_back(currentLoadInst);
currentLoadInst = currentLoadInst->getPreviousSameOpInst();
}
if(endOfInnerBlock)
{
for(auto store : killingStores)
{
store->setPreviousSameOpInst(currentLoadInst);
currentLoadInst = store;
}
killingStores.clear();
}
}
/*
void CSETracker::revertToOuter(IROP irOp)
{
switch(irOp)
{
case IR_add:
currentAddInst.pop();
break;
case IR_adda:
currentAddaInst.pop();
break;
case IR_sub:
currentSubInst.pop();
break;
case IR_mul:
currentMulInst.pop();
break;
case IR_div:
currentDivInst.pop();
break;
default:
break;
}
}
*/