1 //===-- EdgeCode.cpp - generate LLVM instrumentation code --------*- C++ -*--=//
2 //It implements the class EdgeCode: which provides
3 //support for inserting "appropriate" instrumentation at
4 //designated points in the graph
6 //It also has methods to insert initialization code in
8 //===----------------------------------------------------------------------===//
10 #include "llvm/Transforms/Instrumentation/Graph.h"
11 #include "llvm/BasicBlock.h"
12 #include "llvm/Constants.h"
13 #include "llvm/DerivedTypes.h"
14 #include "llvm/iMemory.h"
15 #include "llvm/iTerminators.h"
16 #include "llvm/iOther.h"
17 #include "llvm/iOperators.h"
18 #include "llvm/iPHINode.h"
19 #include "llvm/Module.h"
20 #include "llvm/SymbolTable.h"
21 #include "llvm/GlobalVariable.h"
22 #include "llvm/Constants.h"
23 #include "llvm/BasicBlock.h"
24 #include "llvm/Function.h"
29 #define INSERT_LOAD_COUNT
35 void getTriggerCode(Module *M, BasicBlock *BB, int MethNo, Value *pathNo,
40 sprintf(gstr,"globalVar%d",i);
41 std::string globalVarName=gstr;
42 SymbolTable *ST = M->getSymbolTable();
43 vector<const Type*> args;
44 //args.push_back(PointerType::get(Type::SByteTy));
45 args.push_back(Type::IntTy);
46 args.push_back(Type::IntTy);
47 args.push_back(Type::IntTy);
48 const FunctionType *MTy =
49 FunctionType::get(Type::VoidTy, args, false);
51 // Function *triggerMeth = M->getOrInsertFunction("trigger", MTy);
52 Function *trigMeth = M->getOrInsertFunction("trigger", MTy);
53 assert(trigMeth && "trigger method could not be inserted!");
54 //if (Value *triggerMeth = ST->lookup(PointerType::get(MTy), "trigger")) {
55 //Function *trigMeth = cast<Function>(triggerMeth);
56 vector<Value *> trargs;
58 //pred_iterator piter=BB->pred_begin();
59 //std::string predName = "uu";//BB->getName();
60 //Constant *bbName=ConstantArray::get(predName);//BB->getName());
61 //GlobalVariable *gbl=new GlobalVariable(ArrayType::get(Type::SByteTy,
62 // predName.size()+1),
63 // true, true, bbName, gstr);
65 //M->getGlobalList().push_back(gbl);
67 vector<Value *> elargs;
68 elargs.push_back(ConstantUInt::get(Type::UIntTy, 0));
69 elargs.push_back(ConstantUInt::get(Type::UIntTy, 0));
71 // commented out bb name frm which its called
72 //Instruction *getElmntInst=new GetElementPtrInst(gbl,elargs,"elmntInst");
74 //trargs.push_back(ConstantArray::get(BB->getName()));
76 //trargs.push_back(getElmntInst);
77 //trargs.push_back(bbName);
79 trargs.push_back(ConstantSInt::get(Type::IntTy,MethNo));
81 //trargs.push_back(ConstantSInt::get(Type::IntTy,-1));//erase this
82 trargs.push_back(pathNo);
83 trargs.push_back(cnt);
84 Instruction *callInst=new CallInst(trigMeth,trargs);
86 BasicBlock::InstListType& instList=BB->getInstList();
87 BasicBlock::iterator here=instList.begin();
88 //here = ++instList.insert(here, getElmntInst);
89 instList.insert(here,callInst);
93 //get the code to be inserted on the edge
94 //This is determined from cond (1-6)
95 void getEdgeCode::getCode(Instruction *rInst,
96 Instruction *countInst,
98 BasicBlock *BB, int numPaths, int MethNo){
100 BasicBlock::InstListType& instList=BB->getInstList();
101 BasicBlock::iterator here=instList.begin();
103 //case: r=k code to be inserted
106 Value *val=ConstantSInt::get(Type::IntTy,inc);
108 Instruction *stInst=new StoreInst(val, rInst);
109 here = ++instList.insert(here,stInst);
114 //case: r=0 to be inserted
116 Value *val=ConstantSInt::get(Type::IntTy,0);
118 Instruction *stInst=new StoreInst(val, rInst);
119 here = ++instList.insert(here,stInst);
127 Instruction *ldInst=new LoadInst(rInst, "ti1");
128 Value *val=ConstantSInt::get(Type::IntTy,inc);
129 Instruction *addIn=BinaryOperator::
130 create(Instruction::Add, ldInst, val,"ti2");
132 Instruction *stInst=new StoreInst(addIn, rInst);
134 here = ++instList.insert(here,ldInst);
135 here = ++instList.insert(here,addIn);
137 here = ++instList.insert(here,stInst);
145 assert(inc>=0 && inc<=numPaths && "inc out of bound!");
147 Instruction *ldInst=new
148 LoadInst(countInst,vector<Value *>
149 (1,ConstantUInt::get(Type::UIntTy, inc)), "ti1");
150 Value *val=ConstantSInt::get(Type::IntTy,1);
151 Instruction *addIn=BinaryOperator::
152 create(Instruction::Add, ldInst, val,"ti2");
155 getTriggerCode(M->getParent(), BB, MethNo,
156 ConstantSInt::get(Type::IntTy,inc), addIn);
157 here=instList.begin();
160 assert(inc>=0 && "IT MUST BE POSITIVE NOW");
162 Instruction *stInst=new
163 StoreInst(addIn, countInst, vector<Value *>
164 (1, ConstantUInt::get(Type::UIntTy,inc)));
166 here = ++instList.insert(here,ldInst);
167 here = ++instList.insert(here,addIn);
169 here = ++instList.insert(here,stInst);
174 //case: count[r+inc]++
178 Instruction *ldIndex=new LoadInst(rInst, "ti1");
179 Value *val=ConstantSInt::get(Type::IntTy,inc);
180 Instruction *addIndex=BinaryOperator::
181 create(Instruction::Add, ldIndex, val,"ti2");
182 //erase following 1 line
183 //Value *valtemp=ConstantSInt::get(Type::IntTy,999);
184 //now load count[addIndex]
186 Instruction *castInst=new CastInst(addIndex,
187 Type::UIntTy,"ctin");
188 Instruction *ldInst=new
189 LoadInst(countInst, vector<Value *>(1,castInst), "ti3");
190 Value *cons=ConstantSInt::get(Type::IntTy,1);
192 Instruction *addIn=BinaryOperator::
193 create(Instruction::Add, ldInst, cons,"ti4");
196 getTriggerCode(M->getParent(), BB, MethNo, addIndex, addIn);
197 here=instList.begin();
202 Instruction *stInst=new
203 StoreInst(addIn, countInst,
204 vector<Value *>(1,castInst));
207 here = ++instList.insert(here,ldIndex);
208 here = ++instList.insert(here,addIndex);
209 here = ++instList.insert(here,castInst);
210 here = ++instList.insert(here,ldInst);
211 here = ++instList.insert(here,addIn);
213 here = ++instList.insert(here,stInst);
222 Instruction *ldIndex=new LoadInst(rInst, "ti1");
224 //now load count[addIndex]
225 Instruction *castInst2=new
226 CastInst(ldIndex, Type::UIntTy,"ctin");
227 Instruction *ldInst=new
228 LoadInst(countInst, vector<Value *>(1,castInst2), "ti2");
229 Value *cons=ConstantSInt::get(Type::IntTy,1);
232 Instruction *addIn=BinaryOperator::
233 create(Instruction::Add, ldInst, cons,"ti3");
236 getTriggerCode(M->getParent(), BB, MethNo, ldIndex, addIn);
237 here=instList.begin();
240 Instruction *stInst=new
241 StoreInst(addIn, countInst, vector<Value *>(1,castInst2));
243 here = ++instList.insert(here,ldIndex);
244 here = ++instList.insert(here,castInst2);
245 here = instList.insert(here,ldInst);
246 here = instList.insert(here,addIn);
248 here = instList.insert(here,stInst);
254 //now check for cdIn and cdOut
257 cdIn->getCode(rInst, countInst, M, BB, numPaths, MethNo);
260 cdOut->getCode(rInst, countInst, M, BB, numPaths, MethNo);
266 //Insert the initialization code in the top BB
267 //this includes initializing r, and count
268 //r is like an accumulator, that
269 //keeps on adding increments as we traverse along a path
270 //and at the end of the path, r contains the path
271 //number of that path
272 //Count is an array, where Count[k] represents
273 //the number of executions of path k
274 void insertInTopBB(BasicBlock *front,
277 Instruction *countVar){
278 //rVar is variable r,
279 //countVar is array Count, and these are allocatted outside
281 //store uint 0, uint *%R, uint 0
283 idx.push_back(ConstantUInt::get(Type::UIntTy, 0));
284 Instruction *stInstr=new StoreInst(ConstantInt::get(Type::IntTy, 0), rVar,
287 //now push all instructions in front of the BB
288 BasicBlock::InstListType& instList=front->getInstList();
289 BasicBlock::iterator here=instList.begin();
290 here=++front->getInstList().insert(here, rVar);
291 here=++front->getInstList().insert(here,countVar);
293 //Initialize Count[...] with 0
295 for(int i=0;i<k; i++){
296 Instruction *stInstrC=new
297 StoreInst(ConstantInt::get(Type::IntTy, 0),
298 countVar, std::vector<Value *>
299 (1,ConstantUInt::get(Type::UIntTy, i)));
300 here=++front->getInstList().insert(here,stInstrC);
303 here = ++front->getInstList().insert(here,stInstr);
307 //insert a basic block with appropriate code
309 void insertBB(Edge ed,
310 getEdgeCode *edgeCode,
312 Instruction *countInst,
313 int numPaths, int Methno){
316 BasicBlock* BB1=ed.getFirst()->getElement();
317 BasicBlock* BB2=ed.getSecond()->getElement();
319 #ifdef DEBUG_PATH_PROFILES
321 cerr<<"Edges with codes ######################\n";
322 cerr<<BB1->getName()<<"->"<<BB2->getName()<<"\n";
323 cerr<<"########################\n";
326 char counterstr[100];
327 sprintf(counterstr,"counter%d",i);
328 std::string ctr=counterstr;
330 //We need to insert a BB between BB1 and BB2
331 TerminatorInst *TI=BB1->getTerminator();
332 BasicBlock *newBB=new BasicBlock(ctr, BB1->getParent());
334 //get code for the new BB
335 edgeCode->getCode(rInst, countInst, BB1->getParent(), newBB, numPaths, Methno);
337 //Is terminator a branch instruction?
338 //then we need to change branch destinations to include new BB
340 //std::cerr<<"before cast!\n";
341 BranchInst *BI = cast<BranchInst>(TI);
343 if(BI->isUnconditional()){
344 BI->setUnconditionalDest(newBB);
345 Instruction *newBI2=new BranchInst(BB2);
346 newBB->getInstList().push_back(newBI2);
349 if(BI->getSuccessor(0)==BB2)
350 BI->setSuccessor(0, newBB);
352 if(BI->getSuccessor(1)==BB2)
353 BI->setSuccessor(1, newBB);
355 Instruction *newBI2=new BranchInst(BB2);
356 newBB->getInstList().push_back(newBI2);
359 //std::cerr<<"After casting\n";
360 //get code for the new BB
361 //now iterate over BB2, and set its Phi nodes right
362 for(BasicBlock::iterator BB2Inst = BB2->begin(), BBend = BB2->end();
363 BB2Inst != BBend; ++BB2Inst){
365 if(PHINode *phiInst=dyn_cast<PHINode>(&*BB2Inst)){
366 int bbIndex=phiInst->getBasicBlockIndex(BB1);
368 phiInst->setIncomingBlock(bbIndex, newBB);