aboutsummaryrefslogtreecommitdiff
path: root/lib/Transforms/Instrumentation/ProfilePaths/EdgeCode.cpp
blob: 2e5c0e7ffe65b4ad2712122e52aa955de8a8d161 (plain)
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
//===-- EdgeCode.cpp - generate LLVM instrumentation code -----------------===//
//It implements the class EdgeCode: which provides 
//support for inserting "appropriate" instrumentation at
//designated points in the graph
//
//It also has methods to insert initialization code in 
//top block of cfg
//===----------------------------------------------------------------------===//

#include "llvm/Transforms/Instrumentation/Graph.h"
#include "llvm/Constants.h"
#include "llvm/DerivedTypes.h"
#include "llvm/iMemory.h"
#include "llvm/iTerminators.h"
#include "llvm/iOther.h"
#include "llvm/iOperators.h"
#include "llvm/iPHINode.h"
#include "llvm/Module.h"
#include <string.h>
#include <stdio.h>

#define INSERT_LOAD_COUNT
#define INSERT_STORE

using std::vector;


static void getTriggerCode(Module *M, BasicBlock *BB, int MethNo, Value *pathNo,
                           Value *cnt, Instruction *InsertPos){ 
  static int i=-1;
  i++;
  char gstr[100];
  sprintf(gstr,"globalVar%d",i);
  std::string globalVarName=gstr;
  vector<const Type*> args;
  //args.push_back(PointerType::get(Type::SByteTy));
  args.push_back(Type::IntTy);
  args.push_back(Type::IntTy);
  args.push_back(Type::IntTy);
  const FunctionType *MTy = FunctionType::get(Type::VoidTy, args, false);

  //  Function *triggerMeth = M->getOrInsertFunction("trigger", MTy);
  Function *trigMeth = M->getOrInsertFunction("trigger", MTy);
  assert(trigMeth && "trigger method could not be inserted!");
  //if (Value *triggerMeth = ST->lookup(PointerType::get(MTy), "trigger")) {
  //Function *trigMeth = cast<Function>(triggerMeth);
  vector<Value *> trargs;

  //pred_iterator piter=BB->pred_begin();
  //std::string predName = "uu";//BB->getName();
  //Constant *bbName=ConstantArray::get(predName);//BB->getName());
  //GlobalVariable *gbl=new GlobalVariable(ArrayType::get(Type::SByteTy, 
  //					predName.size()+1), 
  //				 true, true, bbName, gstr);
  
  //M->getGlobalList().push_back(gbl);

  vector<Value *> elargs;
  elargs.push_back(ConstantUInt::get(Type::UIntTy, 0));
  elargs.push_back(ConstantUInt::get(Type::UIntTy, 0));

  // commented out bb name frm which its called
  //Instruction *getElmntInst=new GetElementPtrInst(gbl,elargs,"elmntInst");
  
  //trargs.push_back(ConstantArray::get(BB->getName()));
  
  //trargs.push_back(getElmntInst);
  //trargs.push_back(bbName);

  trargs.push_back(ConstantSInt::get(Type::IntTy,MethNo));
    
  //trargs.push_back(ConstantSInt::get(Type::IntTy,-1));//erase this
  trargs.push_back(pathNo);
  trargs.push_back(cnt);
  Instruction *callInst=new CallInst(trigMeth, trargs, "", InsertPos);
}


//get the code to be inserted on the edge
//This is determined from cond (1-6)
void getEdgeCode::getCode(Instruction *rInst, 
			  Instruction *countInst, 
			  Function *M, 
			  BasicBlock *BB, int numPaths, int MethNo){
  
  Instruction *InsertPos = BB->begin();
  
  //case: r=k code to be inserted
  switch(cond){
  case 1:{
    Value *val=ConstantSInt::get(Type::IntTy,inc);
#ifdef INSERT_STORE
    Instruction *stInst=new StoreInst(val, rInst, InsertPos);
#endif
    break;
    }

  //case: r=0 to be inserted
  case 2:
#ifdef INSERT_STORE
    new StoreInst(ConstantSInt::getNullValue(Type::IntTy), rInst, InsertPos);
#endif
    break;
    
  //r+=k
  case 3:{
    Instruction *ldInst = new LoadInst(rInst, "ti1", InsertPos);
    Value *val = ConstantSInt::get(Type::IntTy,inc);
    Value *addIn = BinaryOperator::create(Instruction::Add, ldInst, val,
                                          "ti2", InsertPos);
#ifdef INSERT_STORE
    new StoreInst(addIn, rInst, InsertPos);
#endif
    break;
  }

  //count[inc]++
  case 4:{
    assert(inc>=0 && inc<=numPaths && "inc out of bound!");
   
    Instruction *Idx = new GetElementPtrInst(countInst, 
                 vector<Value*>(1,ConstantUInt::get(Type::UIntTy, inc)),
                                             "", InsertPos);

    Instruction *ldInst=new LoadInst(Idx, "ti1", InsertPos);
 
    Value *val = ConstantSInt::get(Type::IntTy, 1);
    Instruction *addIn =
      BinaryOperator::create(Instruction::Add, ldInst, val,"ti2", InsertPos);

#ifdef INSERT_STORE
    Instruction *stInst=new StoreInst(addIn, Idx, InsertPos);
#endif

    //insert trigger
    getTriggerCode(M->getParent(), BB, MethNo, 
		   ConstantSInt::get(Type::IntTy,inc), addIn, InsertPos);
    //end trigger code

    assert(inc>=0 && "IT MUST BE POSITIVE NOW");
    break;
  }

  //case: count[r+inc]++
  case 5:{
   
    //ti1=inc+r
    Instruction *ldIndex=new LoadInst(rInst, "ti1", InsertPos);
    Value *val=ConstantSInt::get(Type::IntTy,inc);
    Instruction *addIndex=BinaryOperator::
      create(Instruction::Add, ldIndex, val,"ti2", InsertPos);
    //erase following 1 line
    //Value *valtemp=ConstantSInt::get(Type::IntTy,999);
    //now load count[addIndex]
    
    Instruction *castInst=new CastInst(addIndex, 
				       Type::UIntTy,"ctin", InsertPos);
    Instruction *Idx = new GetElementPtrInst(countInst, 
                                             vector<Value*>(1,castInst), "",
                                             InsertPos);

    Instruction *ldInst=new LoadInst(Idx, "ti3", InsertPos);
    Value *cons=ConstantSInt::get(Type::IntTy,1);
    //count[addIndex]++
    Value *addIn = BinaryOperator::create(Instruction::Add, ldInst, cons,
                                          "ti4", InsertPos);
    
#ifdef INSERT_STORE
    ///*
    new StoreInst(addIn, Idx, InsertPos);
    //*/
#endif

    //insert trigger
    getTriggerCode(M->getParent(), BB, MethNo, addIndex, addIn, InsertPos);
    //end trigger code

    break;
  }

    //case: count[r]+
  case 6:{
    //ti1=inc+r
    Instruction *ldIndex=new LoadInst(rInst, "ti1", InsertPos);
    
    //now load count[addIndex]
    Instruction *castInst2=new CastInst(ldIndex, Type::UIntTy,"ctin",InsertPos);
    Instruction *Idx = new GetElementPtrInst(countInst, 
                                             vector<Value*>(1,castInst2), "",
                                             InsertPos);
    
    Instruction *ldInst=new LoadInst(Idx, "ti2", InsertPos);
    Value *cons=ConstantSInt::get(Type::IntTy,1);

    //count[addIndex]++
    Instruction *addIn=BinaryOperator::create(Instruction::Add, ldInst,
                                              cons,"ti3", InsertPos);

#ifdef INSERT_STORE
    new StoreInst(addIn, Idx, InsertPos);
#endif
    //insert trigger
    getTriggerCode(M->getParent(), BB, MethNo, ldIndex, addIn, InsertPos);
    //end trigger code
    
    break;
  }
    
  }
  //now check for cdIn and cdOut
  //first put cdOut
  if(cdIn!=NULL){
    cdIn->getCode(rInst, countInst, M, BB, numPaths, MethNo);
  }
  if(cdOut!=NULL){
    cdOut->getCode(rInst, countInst, M, BB, numPaths, MethNo);
  }
}



//Insert the initialization code in the top BB
//this includes initializing r, and count
//r is like an accumulator, that 
//keeps on adding increments as we traverse along a path
//and at the end of the path, r contains the path
//number of that path
//Count is an array, where Count[k] represents
//the number of executions of path k
void insertInTopBB(BasicBlock *front, 
		   int k, 
		   Instruction *rVar, 
		   Instruction *countVar){
  //rVar is variable r, 
  //countVar is array Count, and these are allocatted outside

  Value *Int0 = ConstantInt::get(Type::IntTy, 0);
  
  //store uint 0, uint *%R, uint 0
  vector<Value *> idx;
  idx.push_back(ConstantUInt::get(Type::UIntTy, 0));

  //now push all instructions in front of the BB
  BasicBlock::iterator here=front->begin();
  front->getInstList().insert(here, rVar);
  front->getInstList().insert(here,countVar);
  
  //Initialize Count[...] with 0

  for (int i=0;i<k; i++){
    Value *GEP2 = new GetElementPtrInst(countVar,
                          vector<Value *>(1,ConstantUInt::get(Type::UIntTy, i)),
                                        "", here);
    new StoreInst(Int0, GEP2, here);
  }

  Instruction *GEP = new GetElementPtrInst(rVar, idx, "", here);
  new StoreInst(Int0, GEP, here);
}


//insert a basic block with appropriate code