-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathPointerAnalysis.cpp
More file actions
executable file
·239 lines (227 loc) · 7.65 KB
/
PointerAnalysis.cpp
File metadata and controls
executable file
·239 lines (227 loc) · 7.65 KB
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
//
// StaticAnalysisPass.cpp
//
//
// Created by Jules Testard on 22/05/2014.
//
//
/*
* Note : use the errs() instead of std::cout in this file to output to the console (if your name is not mike and you don't have a fancy debugger that
* took hours to install :).
*
*
*
*
*
*/
#include "PointerAnalysis.h"
/*
* For basic static analysis, flow is just "assigned to top", which just means the basic string from the Flow general class will be top.
* This method is expected to do much more when overloaded.
* We use the presence of names in the instructions in LLVM.
*/
Flow* PointerAnalysis::executeFlowFunction(Flow *in, Instruction *inst, int NodeId){
PointerAnalysisFlow* inFlow = static_cast<PointerAnalysisFlow*>(in);
PointerAnalysisFlow * output;
switch(inst->getOpcode()) {
case 29 : // getelementptr instruction. Used when incrementing a pointer.
output = execute_X_equals_NULL(inFlow,inst);
break;
case 28 : // Store instructions are X = &Y
output = execute_X_equals_refY(inFlow,inst);
break;
case 27 : //Load instruction
//If one load followed by one store, then is X = Y
//If two loads followed by one store, then is *X = Y
{
Instruction* nInst = inst->getNextNode();
Instruction* nnInst = nInst->getNextNode();
if(isa<StoreInst>(nInst)) {
output = execute_X_equals_Y(inFlow,inst);
} else if (isa<LoadInst>(nInst) && isa<StoreInst>(nnInst)) {
if (inst->getOperand(0)->getName()!="" && nInst->getOperand(0)->getName()!="")
output = execute_ptrX_equals_Y(inFlow,inst);
else if (inst->getOperand(0)->getName()!="" && nnInst->getOperand(1)->getName()!="")
output = execute_X_equals_ptrY(inFlow,inst);
else
output = new PointerAnalysisFlow(inFlow);
} else {
output = new PointerAnalysisFlow(inFlow);
}
break;
}
default:
output = new PointerAnalysisFlow(inFlow);
break;
}
return output;
}
/**
* C CODE : X = &Y
* LLVM CODE :
* store float* %Y, float** %X, align 4
*/
PointerAnalysisFlow* PointerAnalysis::execute_X_equals_refY(PointerAnalysisFlow* in, Instruction* instruction) {
//Check that left operand is not null.
if (isa<ConstantPointerNull>(instruction->getOperand(0))) {
return execute_X_equals_NULL(in,instruction);
}
StoreInst* store = static_cast<StoreInst*>(instruction);
PointerAnalysisFlow* f = new PointerAnalysisFlow(in);
// X = &Y
//Check if right operand is a pointer
if (store->getOperand(1)->getType()->isPointerTy()) {
//Check if left & right operand names are non empty.
if (store->getOperand(0)->getName()!="" && store->getOperand(1)->getName()!="") {
if (madeByLLVM(store->getOperand(0)->getName())) {
return f;
}
PointerAnalysisFlow* ff = new PointerAnalysisFlow();
set<string> s;
map<string, set<string> >value;
s.insert(store->getOperand(0)->getName());
value[store->getOperand(1)->getName()] = s;
// X now points to Y.
ff->value = value;
PointerAnalysisFlow* tmp = static_cast<PointerAnalysisFlow*>(ff->join(f));
delete ff;
delete f;
f = tmp;
}
}
return f;
}
/**
* C CODE : X = Y
* LLVM CODE :
* %0 = load float** %Y, align 4
* store float* %0, float** %X, align 4
*/
PointerAnalysisFlow* PointerAnalysis::execute_X_equals_Y(PointerAnalysisFlow* in, Instruction* instruction) {
LoadInst* load = static_cast<LoadInst*>(instruction);
PointerAnalysisFlow* f = new PointerAnalysisFlow(in);
Value* Y = load->getOperand(0); //RO
Value* X = load->getNextNode()->getOperand(1); //LO
// X = Y
// Check if both operands (X & Y) are pointers.
if (Y->getType()->isPointerTy() && X->getType()->isPointerTy()) {
if (Y->getName()!="" && X->getName()!="") {
//Everything Y points to, X points to now as well.
PointerAnalysisFlow* ff = new PointerAnalysisFlow();
map<string, set<string> > value;
//Get the set of everything Y points from the in and make X point to it.
value[X->getName()] = in->value[Y->getName()];;
ff->value = value;
PointerAnalysisFlow* tmp = static_cast<PointerAnalysisFlow*>(ff->join(f));
delete ff;
delete f;
f = tmp;
}
}
return f;
}
/**
* C CODE : *X = Y
* LLVM CODE :
* %0 = load float** %Y, align 4
* %1 = load float*** %X, align 4
* store float* %0, float** %1, align 4
*/
PointerAnalysisFlow* PointerAnalysis::execute_ptrX_equals_Y(PointerAnalysisFlow* in, Instruction* instruction){
PointerAnalysisFlow* f = new PointerAnalysisFlow(in);
Value* Y = instruction->getOperand(0); //RO
Value* X = instruction->getNextNode()->getOperand(0); //LO
//Check that both operands are pointers.
if (Y->getType()->isPointerTy() && X->getType()->isPointerTy()) {
if (Y->getName()!="" && X->getName()!="") {
//Everything Y points to, *X points to now as well.
PointerAnalysisFlow* ff = new PointerAnalysisFlow();
set<string> pointedByX = in->value[X->getName()];
map<string, set<string> > value;
for (set<string>::iterator it = pointedByX.begin() ; it != pointedByX.end() ; it++) {
//if X points to W, then W points to everything Y points to.
string W = *it;
value[W] = in->value[Y->getName()];
}
ff->value = value;
PointerAnalysisFlow* tmp = static_cast<PointerAnalysisFlow*>(ff->join(f));
delete ff;
delete f;
f = tmp;
}
}
return f;
}
/**
* C CODE : X = *Y
* LLVM CODE :
* %0 = load float*** %Y, align 4
* %1 = load float** %0, align 4
* store float* %1, float** %X, align 4
*/
PointerAnalysisFlow* PointerAnalysis::execute_X_equals_ptrY(PointerAnalysisFlow* in, Instruction* instruction) {
PointerAnalysisFlow* f = new PointerAnalysisFlow(in);
Value* Y = instruction->getOperand(0);
Value* X = instruction->getNextNode()->getNextNode()->getOperand(1);
if (Y->getType()->isPointerTy() && X->getType()->isPointerTy()) {
if (Y->getName()!="" && X->getName()!="") {
PointerAnalysisFlow* ff = new PointerAnalysisFlow();
//For all W pointed by Y, X points to Z, where Z is pointed by W.
set<string> pointedByY = in->value[Y->getName()]; //Set of Ws.
for (set<string>::iterator W = pointedByY.begin(); W != pointedByY.end() ; W++) {
for (set<string>::iterator Z = in->value[*W].begin() ; Z != in->value[*W].end() ; Z++) {
ff->value[X->getName()].insert(*Z);
}
}
PointerAnalysisFlow* tmp = static_cast<PointerAnalysisFlow*>(ff->join(f));
delete ff;
delete f;
f = tmp;
}
}
return f;
}
/**
* C CODE : X = NULL
* store float* null, float** %X, align 4
* C CODE : X = Y+i
* %add.ptr = getelementptr inbounds float** %Y, i32 i
* store float** %add.ptr, float** %X, align 4
*/
PointerAnalysisFlow* PointerAnalysis::execute_X_equals_NULL(PointerAnalysisFlow* in, Instruction* instruction) {
PointerAnalysisFlow* f = new PointerAnalysisFlow(in);
// if C CODE : X = Y+i
if (isa<GetElementPtrInst>(instruction)) {
Value* X = instruction->getNextNode()->getOperand(1);
if (X->getType()->isPointerTy() && X->getName() != "") {
f->value.erase(X->getName());
}
return f;
}
//if C CODE : X = NULL
else if (isa<StoreInst>(instruction)) {
Value* X = instruction->getOperand(1);
if (X->getType()->isPointerTy() && X->getName() != "") {
f->value.erase(X->getName());
}
return f;
} else {
return f;
}
}
bool PointerAnalysis::madeByLLVM(string name) {
if(name.substr(name.find_last_of(".") + 1) == "ptr") {
return true;
} else {
return false;
}
}
Flow* PointerAnalysis::initialize(){
return new PointerAnalysisFlow(PointerAnalysisFlow::BOTTOM);
}
PointerAnalysis::PointerAnalysis(Function &F) : StaticAnalysis() {
this->top = new PointerAnalysisFlow(PointerAnalysisFlow::TOP);//Should be changed by subclasses of Flow to an instance of the subclass
this->bottom = new PointerAnalysisFlow(PointerAnalysisFlow::BOTTOM);//Should be changed by subclasses of Flow to an instance of the subclass
this->functionName = F.getName();
buildCFG(F);
}