-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathConstantPropAnalysis.cpp
More file actions
executable file
·564 lines (461 loc) · 16 KB
/
ConstantPropAnalysis.cpp
File metadata and controls
executable file
·564 lines (461 loc) · 16 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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
//
// ConstantPropAnalysisPass.cpp
//
//
// Created by Costas Zarifis 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 "ConstantPropAnalysis.h"
// You can actually check out the opcodes in this address:
// http://code.woboq.org/userspace/llvm/include/llvm/IR/Instruction.def.html
#define ADD 8 //This is the opcode for the add instruction
#define FADD 9 //This is the opcode for the add instruction
#define SUB 10 //This is the opcode for the sub instruction
#define FSUB 11 //This is the opcode for the floating point sub instruction
#define MUL 12 //This is the opcode for the mul instruction
#define FMUL 13 //This is the opcode for the floating point mul instruction
#define SDIV 15 //This is the opcode for the signed div instruction
#define FDIV 16 //This is the opcode for the float div instruction
#define UREM 17 //This is the opcode for the unsigned mod instruction
#define SREM 18 //This is the opcode for the signed mod instruction
#define FREM 19 //This is the opcode for the floating point mod instruction
#define SHL 20 //This is the opcode for the Shift left (logical) instruction
#define LSHR 21 //This is the opcode for the Shift right (logical) instruction
#define ASHR 22 //This is the opcode for the Shift right (arithmetic) instruction
// MEMORY/POINTER OPCODES RETURN TOP
#define ALLOCA 26
#define LOAD 27
#define STORE 28
#define ELPTR 29
#define FENCE 30
#define ATCMP 31
#define ATRMW 32
#define PTRTOINT 42
#define INTTOPTR 43
#define TRUNC 33 // Truncate integers
#define ZEXT 34 // Zero extend integers
#define SEXT 35 // Sign extend integers
#define FPTOUI 36 //This is the opcode for the int to float cast instruction
#define FPTOSI 37 //This is the opcode for the float to integer cast instruction
#define UITOFP 38 //UInt -> floating point
#define SITOFP 39 // SInt -> floating point
#define FPTRUNC 40 //Truncate floating point
#define FPEXT 41 // Extend floating point
#define PHI 48 // Extend floating point
/*
* 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.
*/
Flow* ConstantPropAnalysis::executeFlowFunction(Flow *in, Instruction *inst, int NodeId){
ConstantPropAnalysisFlow* inFlow =
static_cast<ConstantPropAnalysisFlow*>(in);
ConstantPropAnalysisFlow * output;
switch (inst->getOpcode()) {
case ADD:
case SUB:
case MUL:
case SDIV:
case SREM:
case SHL:
case LSHR:
case ASHR:
output = executeOpInst(inFlow, inst, inst->getOpcode());
break;
case FADD:
case FSUB:
case FMUL:
case FDIV:
case FREM:
output = executeFOpInst(inFlow, inst, inst->getOpcode());
break;
case TRUNC:
case ZEXT:
case SEXT:
case FPTOSI:
case FPTOUI:
case UITOFP:
case SITOFP:
case FPTRUNC:
case FPEXT:
output = executeCastInst(inFlow, inst);
break;
case PHI:
output = executePhiInst(inFlow, inst);
break;
case ALLOCA:
case LOAD:
case STORE:
case ELPTR:
case FENCE:
case ATCMP:
case ATRMW:
case PTRTOINT:
case INTTOPTR:
// If I reach this point it means that I have a pointer...
// Return TOP!
output = retTop();
break;
default:
output = new ConstantPropAnalysisFlow(inFlow);
break;
}
//errs() << "Instruction : " << *inst << ", Flow value : " << output->jsonString() << "\n";
return output;
}
ConstantPropAnalysisFlow* ConstantPropAnalysis::retTop() {
ConstantPropAnalysisFlow* f = new ConstantPropAnalysisFlow("top");
f->basic="top";
return f;
}
ConstantPropAnalysisFlow* ConstantPropAnalysis::executeCastInst(
ConstantPropAnalysisFlow* in, Instruction* instruction) {
ConstantPropAnalysisFlow* f = new ConstantPropAnalysisFlow(in);
//Value *leftOperand = instruction->getOperand(0);
//Value *rightOperand = instruction->getOperand(1);
map<string, float> value;
Value *retVal = instruction;
string regName = retVal->getName();
CastInst *castInst = dyn_cast<CastInst>(instruction);
Value* casting = instruction->getOperand(0); //RO
if (!dyn_cast<Constant>(retVal)) {
if (!dyn_cast<Constant>(casting)) {
// Cool they are both variables. We just need to forward the value
if (f->value.find(casting->getName()) == f->value.end()) {
// Oh no! Read the error message!
} else {
// Hmm, I guess we're good...
float forwardVal = f->value.find(casting->getName())->second;
ConstantPropAnalysisFlow* ff = new ConstantPropAnalysisFlow();
Type* ttype = castInst->getDestTy();
if (ttype->isDoubleTy())
forwardVal = (float) forwardVal;
else if(ttype->isFloatingPointTy())
forwardVal = (float) forwardVal;
else if (ttype->isIntegerTy()){
forwardVal = (int) forwardVal;
}
value[retVal->getName()] = forwardVal;
ff->value = value;
ConstantPropAnalysisFlow* tmp =
static_cast<ConstantPropAnalysisFlow*>(ff->join(f));
delete ff;
delete f;
f = tmp;
}
} else {
// Hmm, I guess we're good...
if (ConstantFP *cfp = dyn_cast<ConstantFP>(casting)) {
float forwardVal = cfp->getValueAPF().convertToFloat();
//float forwardVal = f->value.find(casting->getName())->second;
ConstantPropAnalysisFlow* ff = new ConstantPropAnalysisFlow();
value[retVal->getName()] = forwardVal;
ff->value = value;
ConstantPropAnalysisFlow* tmp =
static_cast<ConstantPropAnalysisFlow*>(ff->join(f));
delete ff;
delete f;
f = tmp;
} else if (ConstantInt *cfp = dyn_cast<ConstantInt>(casting)) {
float forwardVal = cfp->getZExtValue();
ConstantPropAnalysisFlow* ff = new ConstantPropAnalysisFlow();
value[retVal->getName()] = forwardVal;
ff->value = value;
ConstantPropAnalysisFlow* tmp =
static_cast<ConstantPropAnalysisFlow*>(ff->join(f));
delete ff;
delete f;
f = tmp;
}
}
}
return f;
}
float ConstantPropAnalysis::computeOp(float leftVal, float rightVal,
unsigned opcode) {
float resVal = 0;
int ASHRVAL, ASHRMASK;
switch (opcode) {
case ADD:
case FADD:
resVal = leftVal + rightVal;
break;
case SUB:
case FSUB:
resVal = leftVal - rightVal;
break;
case FDIV:
case SDIV:
resVal = leftVal / rightVal;
break;
case FMUL:
case MUL:
resVal = leftVal * rightVal;
break;
case FREM:
case SREM:
resVal = (int) leftVal % (int) rightVal;
break;
case SHL:
resVal = (int) leftVal << (int) rightVal;
break;
case LSHR:
resVal = (int) leftVal >> (int) rightVal;
break;
case ASHR:
ASHRMASK = (int)leftVal;
ASHRMASK &= 0x80000000;
ASHRVAL = (int)leftVal;
ASHRVAL &= 0x7fffffff;
ASHRMASK |=( ASHRVAL >> (int) rightVal);
resVal = (int)ASHRMASK;
break;
}
return resVal;
}
ConstantPropAnalysisFlow* ConstantPropAnalysis::executePhiInst(
ConstantPropAnalysisFlow* in, Instruction* instruction) {
ConstantPropAnalysisFlow* f = new ConstantPropAnalysisFlow(in);
Value *leftOperand = instruction->getOperand(0);
Value *rightOperand = instruction->getOperand(1);
map<string, float> value;
Value *K = instruction;
string regName = K->getName();
// Ok, cool! Both the right and the left operand is a variable...
if ((f->value.find(leftOperand->getName()) == f->value.end())
| (f->value.find(rightOperand->getName()) == f->value.end())) {
// Oh no! Read the error message!
} else {
// Hmm, I guess we're good...
float leftVal = f->value.find(leftOperand->getName())->second;
float rightVal = f->value.find(rightOperand->getName())->second;
errs() << "leftVal: " << leftVal << "rightVal" << rightVal << "\n";
// If the variables are not the same in the two branches then
// we can't propagate the constant.
if (leftVal == rightVal){
float resVal = leftVal;
ConstantPropAnalysisFlow* ff = new ConstantPropAnalysisFlow();
value[K->getName()] = resVal;
ff->value = value;
ConstantPropAnalysisFlow* tmp =
static_cast<ConstantPropAnalysisFlow*>(ff->join(f));
delete ff;
delete f;
f = tmp;
}
}
return f;
}
ConstantPropAnalysisFlow* ConstantPropAnalysis::executeFOpInst(
ConstantPropAnalysisFlow* in, Instruction* instruction,
unsigned opcode) {
ConstantPropAnalysisFlow* f = new ConstantPropAnalysisFlow(in);
Value *leftOperand = instruction->getOperand(0);
Value *rightOperand = instruction->getOperand(1);
map<string, float> value;
Value *K = instruction;
string regName = K->getName();
// Checking if left operand is a constant
if (ConstantFP *CILeft = dyn_cast<ConstantFP>(leftOperand)) {
if (ConstantFP *CIRight = dyn_cast<ConstantFP>(rightOperand)) {
// Cool they are both constants.
float leftVal = CILeft->getValueAPF().convertToFloat();
float rightVal = CIRight->getValueAPF().convertToFloat();
float resVal = computeOp(leftVal, rightVal, opcode);
ConstantPropAnalysisFlow* ff = new ConstantPropAnalysisFlow();
//errs() << leftVal << " " << rightVal << "\n";
//errs() << "outcome: " << resVal << "\n";
value[K->getName()] = resVal;
ff->value = value;
ConstantPropAnalysisFlow* tmp =
static_cast<ConstantPropAnalysisFlow*>(ff->join(f));
delete ff;
delete f;
f = tmp;
} else {
// ok so the right operand is a variable
if (f->value.find(rightOperand->getName()) == f->value.end()) {
// Oh no! Read the error message!
}
else {
// Hmm, I guess we're good...
float leftVal = CILeft->getValueAPF().convertToFloat();
float rightVal = f->value.find(rightOperand->getName())->second;
float resVal = computeOp(leftVal, rightVal, opcode);
ConstantPropAnalysisFlow* ff = new ConstantPropAnalysisFlow();
//errs() << leftVal << " " << rightVal << "\n";
//errs() << "outcome: " << resVal << "\n";
value[K->getName()] = resVal;
ff->value = value;
ConstantPropAnalysisFlow* tmp =
static_cast<ConstantPropAnalysisFlow*>(ff->join(f));
delete ff;
delete f;
f = tmp;
}
}
} else {
// So, the left part of the addition is a variable. We'll have to check the input set to get the value
// this variable has at the moment.
if (ConstantFP *CIRight = dyn_cast<ConstantFP>(rightOperand)) {
// Ok, cool! the right part is a constant...
//leftOperand->getName()
//int leftVal = CILeft->getZExtValue();
if (f->value.find(leftOperand->getName()) == f->value.end()) {
} else {
// Hmm, I guess we're good...
float leftVal = f->value.find(leftOperand->getName())->second;
float rightVal = CIRight->getValueAPF().convertToFloat();
//float resVal = leftVal + rightVal;
float resVal = computeOp(leftVal, rightVal, opcode);
ConstantPropAnalysisFlow* ff = new ConstantPropAnalysisFlow();
//errs() << leftVal << " " << rightVal << "\n";
//errs() << "outcome: " << resVal << "\n";
value[K->getName()] = resVal;
ff->value = value;
ConstantPropAnalysisFlow* tmp =
static_cast<ConstantPropAnalysisFlow*>(ff->join(f));
delete ff;
delete f;
f = tmp;
}
} else {
// Ok, cool! Both the right and the left operand is a variable...
if ((f->value.find(leftOperand->getName()) == f->value.end())
| (f->value.find(rightOperand->getName()) == f->value.end())) {
} else {
// Hmm, I guess we're good...
float leftVal = f->value.find(leftOperand->getName())->second;
float rightVal = f->value.find(rightOperand->getName())->second;
float resVal = computeOp(leftVal, rightVal, opcode);
ConstantPropAnalysisFlow* ff = new ConstantPropAnalysisFlow();
//errs() << leftVal << " " << rightVal << "\n";
//errs() << "outcome: " << resVal << "\n";
value[K->getName()] = resVal;
ff->value = value;
ConstantPropAnalysisFlow* tmp =
static_cast<ConstantPropAnalysisFlow*>(ff->join(f));
delete ff;
delete f;
f = tmp;
}
}
}
return f;
}
ConstantPropAnalysisFlow* ConstantPropAnalysis::executeOpInst(
ConstantPropAnalysisFlow* in, Instruction* instruction,
unsigned opcode) {
ConstantPropAnalysisFlow* f = new ConstantPropAnalysisFlow(in);
Value *leftOperand = instruction->getOperand(0);
Value *rightOperand = instruction->getOperand(1);
map<string, float> value;
Value *K = instruction;
string regName = K->getName();
//errs() << "Instruction : " << regName << " left " << leftOperand->getName()
// << " right " << rightOperand->getName() << "\n";
// Checking if left operand is a constant
if (ConstantInt *CILeft = dyn_cast<ConstantInt>(leftOperand)) {
if (ConstantInt *CIRight = dyn_cast<ConstantInt>(rightOperand)) {
// Cool they are both constants.
float leftVal = CILeft->getZExtValue();
float rightVal = CIRight->getZExtValue();
float resVal = computeOp(leftVal, rightVal, opcode);
//float resVal = leftVal + rightVal;
ConstantPropAnalysisFlow* ff = new ConstantPropAnalysisFlow();
//errs() << leftVal << " " << rightVal << "\n";
//errs() << "outcome: " << resVal << "\n";
value[K->getName()] = resVal;
ff->value = value;
ConstantPropAnalysisFlow* tmp =
static_cast<ConstantPropAnalysisFlow*>(ff->join(f));
delete ff;
delete f;
f = tmp;
} else {
// ok so the right operand is a variable
if (f->value.find(rightOperand->getName()) == f->value.end()) {
}
else {
// Hmm, I guess we're good...
float leftVal = CILeft->getZExtValue();
float rightVal = f->value.find(rightOperand->getName())->second;
float resVal = computeOp(leftVal, rightVal, opcode);
//float resVal = leftVal + rightVal;
ConstantPropAnalysisFlow* ff = new ConstantPropAnalysisFlow();
//errs() << leftVal << " " << rightVal << "\n";
//errs() << "outcome: " << resVal << "\n";
value[K->getName()] = resVal;
ff->value = value;
ConstantPropAnalysisFlow* tmp =
static_cast<ConstantPropAnalysisFlow*>(ff->join(f));
delete ff;
delete f;
f = tmp;
}
}
} else {
// So, the left part of the addition is a variable. We'll have to check the input set to get the value
// this variable has at the moment.
if (ConstantInt *CIRight = dyn_cast<ConstantInt>(rightOperand)) {
// Ok, cool! the right part is a constant...
//leftOperand->getName()
//int leftVal = CILeft->getZExtValue();
if (f->value.find(leftOperand->getName()) == f->value.end()) {
} else {
// Hmm, I guess we're good...
float leftVal = f->value.find(leftOperand->getName())->second;
float rightVal = CIRight->getZExtValue();
float resVal = computeOp(leftVal, rightVal, opcode);
//float resVal = leftVal + rightVal;
ConstantPropAnalysisFlow* ff = new ConstantPropAnalysisFlow();
//errs() << leftVal << " " << rightVal << "\n";
//errs() << "outcome: " << resVal << "\n";
value[K->getName()] = resVal;
ff->value = value;
ConstantPropAnalysisFlow* tmp =
static_cast<ConstantPropAnalysisFlow*>(ff->join(f));
delete ff;
delete f;
f = tmp;
}
} else {
// Ok, cool! Both the right and the left operand is a variable...
if ((f->value.find(leftOperand->getName()) == f->value.end())
| (f->value.find(rightOperand->getName()) == f->value.end())) {
} else {
// Hmm, I guess we're good...
float leftVal = f->value.find(leftOperand->getName())->second;
float rightVal = f->value.find(rightOperand->getName())->second;
float resVal = computeOp(leftVal, rightVal, opcode);
//float resVal = leftVal + rightVal;
ConstantPropAnalysisFlow* ff = new ConstantPropAnalysisFlow();
//errs() << leftVal << " " << rightVal << "\n";
//errs() << "outcome: " << resVal << "\n";
value[K->getName()] = resVal;
ff->value = value;
ConstantPropAnalysisFlow* tmp =
static_cast<ConstantPropAnalysisFlow*>(ff->join(f));
delete ff;
delete f;
f = tmp;
}
//break;
}
}
return f;
}
Flow * ConstantPropAnalysis::initialize() {
return new ConstantPropAnalysisFlow(ConstantPropAnalysisFlow::BOTTOM);
}
ConstantPropAnalysis::ConstantPropAnalysis(Function & F) :
StaticAnalysis() {
this->top = new ConstantPropAnalysisFlow(ConstantPropAnalysisFlow::TOP);//Should be changed by subclasses of Flow to an instance of the subclass
this->bottom = new ConstantPropAnalysisFlow(
ConstantPropAnalysisFlow::BOTTOM);//Should be changed by subclasses of Flow to an instance of the subclass
this->functionName = F.getName();
buildCFG(F);
}