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"//llvm/ConstantVals.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,
38 //cerr<<"In trigger code"<<endl;
42 sprintf(gstr,"globalVar%d",i);
43 std::string globalVarName=gstr;
44 SymbolTable *ST = M->getSymbolTable();
45 vector<const Type*> args;
46 args.push_back(PointerType::get(Type::SByteTy));
47 args.push_back(Type::IntTy);
48 args.push_back(Type::IntTy);
49 args.push_back(Type::IntTy);
50 const FunctionType *MTy =
51 FunctionType::get(Type::VoidTy, args, false);
53 // Function *triggerMeth = M->getOrInsertFunction("trigger", MTy);
54 Function *trigMeth = M->getOrInsertFunction("trigger", MTy);
55 assert(trigMeth && "trigger method could not be inserted!");
56 //if (Value *triggerMeth = ST->lookup(PointerType::get(MTy), "trigger")) {
57 //Function *trigMeth = cast<Function>(triggerMeth);
58 vector<Value *> trargs;
60 //pred_iterator piter=BB->pred_begin();
61 std::string predName=BB->getName();
62 Constant *bbName=ConstantArray::get(predName);//BB->getName());
63 GlobalVariable *gbl=new GlobalVariable(ArrayType::get(Type::SByteTy,
65 true, true, bbName, gstr);
66 M->getGlobalList().push_back(gbl);
68 vector<Value *> elargs;
69 elargs.push_back(ConstantUInt::get(Type::UIntTy, 0));
70 elargs.push_back(ConstantUInt::get(Type::UIntTy, 0));
72 Instruction *getElmntInst=new GetElementPtrInst(gbl,elargs,"elmntInst");
74 //trargs.push_back(ConstantArray::get(BB->getName()));
75 trargs.push_back(getElmntInst);
76 trargs.push_back(ConstantSInt::get(Type::IntTy,MethNo));
78 //trargs.push_back(ConstantSInt::get(Type::IntTy,-1));//erase this
79 trargs.push_back(pathNo);
80 trargs.push_back(cnt);
81 Instruction *callInst=new CallInst(trigMeth,trargs);
83 BasicBlock::InstListType& instList=BB->getInstList();
84 BasicBlock::iterator here=instList.begin();
85 here = ++instList.insert(here, getElmntInst);
86 instList.insert(here,callInst);
89 //insert trigger method
91 //assert(0&&"No method trigger");
96 //get the code to be inserted on the edge
97 //This is determined from cond (1-6)
98 void getEdgeCode::getCode(Instruction *rInst,
99 Instruction *countInst,
101 BasicBlock *BB, int numPaths, int MethNo){
103 BasicBlock::InstListType& instList=BB->getInstList();
104 BasicBlock::iterator here=instList.begin();
106 //case: r=k code to be inserted
109 Value *val=ConstantSInt::get(Type::IntTy,inc);
111 Instruction *stInst=new StoreInst(val, rInst);
112 here = ++instList.insert(here,stInst);
117 //case: r=0 to be inserted
119 Value *val=ConstantSInt::get(Type::IntTy,0);
121 Instruction *stInst=new StoreInst(val, rInst);
122 here = ++instList.insert(here,stInst);
130 Instruction *ldInst=new LoadInst(rInst, "ti1");
131 Value *val=ConstantSInt::get(Type::IntTy,inc);
132 Instruction *addIn=BinaryOperator::
133 create(Instruction::Add, ldInst, val,"ti2");
135 Instruction *stInst=new StoreInst(addIn, rInst);
137 here = ++instList.insert(here,ldInst);
138 here = ++instList.insert(here,addIn);
140 here = ++instList.insert(here,stInst);
148 assert(inc>=0 && inc<=numPaths && "inc out of bound!");
150 Instruction *ldInst=new
151 LoadInst(countInst,vector<Value *>
152 (1,ConstantUInt::get(Type::UIntTy, inc)), "ti1");
153 Value *val=ConstantSInt::get(Type::IntTy,1);
154 Instruction *addIn=BinaryOperator::
155 create(Instruction::Add, ldInst, val,"ti2");
158 getTriggerCode(M->getParent(), BB, MethNo,
159 ConstantSInt::get(Type::IntTy,inc), addIn);
160 here=instList.begin();
163 assert(inc>=0 && "IT MUST BE POSITIVE NOW");
165 Instruction *stInst=new
166 StoreInst(addIn, countInst, vector<Value *>
167 (1, ConstantUInt::get(Type::UIntTy,inc)));
169 here = ++instList.insert(here,ldInst);
170 here = ++instList.insert(here,addIn);
172 here = ++instList.insert(here,stInst);
177 //case: count[r+inc]++
181 Instruction *ldIndex=new LoadInst(rInst, "ti1");
182 Value *val=ConstantSInt::get(Type::IntTy,inc);
183 Instruction *addIndex=BinaryOperator::
184 create(Instruction::Add, ldIndex, val,"ti2");
185 //erase following 1 line
186 //Value *valtemp=ConstantSInt::get(Type::IntTy,999);
187 //now load count[addIndex]
189 Instruction *castInst=new CastInst(addIndex,
190 Type::UIntTy,"ctin");
191 Instruction *ldInst=new
192 LoadInst(countInst, vector<Value *>(1,castInst), "ti3");
193 Value *cons=ConstantSInt::get(Type::IntTy,1);
195 Instruction *addIn=BinaryOperator::
196 create(Instruction::Add, ldInst, cons,"ti4");
199 getTriggerCode(M->getParent(), BB, MethNo, addIndex, addIn);
200 here=instList.begin();
205 Instruction *stInst=new
206 StoreInst(addIn, countInst,
207 vector<Value *>(1,castInst));
210 here = ++instList.insert(here,ldIndex);
211 here = ++instList.insert(here,addIndex);
212 here = ++instList.insert(here,castInst);
213 here = ++instList.insert(here,ldInst);
214 here = ++instList.insert(here,addIn);
216 here = ++instList.insert(here,stInst);
225 Instruction *ldIndex=new LoadInst(rInst, "ti1");
227 //now load count[addIndex]
228 Instruction *castInst2=new
229 CastInst(ldIndex, Type::UIntTy,"ctin");
230 Instruction *ldInst=new
231 LoadInst(countInst, vector<Value *>(1,castInst2), "ti2");
232 Value *cons=ConstantSInt::get(Type::IntTy,1);
235 Instruction *addIn=BinaryOperator::
236 create(Instruction::Add, ldInst, cons,"ti3");
239 getTriggerCode(M->getParent(), BB, MethNo, ldIndex, addIn);
240 here=instList.begin();
243 Instruction *stInst=new
244 StoreInst(addIn, countInst, vector<Value *>(1,castInst2));
246 here = ++instList.insert(here,ldIndex);
247 here = ++instList.insert(here,castInst2);
248 here = instList.insert(here,ldInst);
249 here = instList.insert(here,addIn);
251 here = instList.insert(here,stInst);
257 //now check for cdIn and cdOut
260 cdIn->getCode(rInst, countInst, M, BB, numPaths, MethNo);
263 cdOut->getCode(rInst, countInst, M, BB, numPaths, MethNo);
269 //Insert the initialization code in the top BB
270 //this includes initializing r, and count
271 //r is like an accumulator, that
272 //keeps on adding increments as we traverse along a path
273 //and at the end of the path, r contains the path
274 //number of that path
275 //Count is an array, where Count[k] represents
276 //the number of executions of path k
277 void insertInTopBB(BasicBlock *front,
280 Instruction *countVar){
281 //rVar is variable r,
282 //countVar is array Count, and these are allocatted outside
284 //store uint 0, uint *%R, uint 0
286 idx.push_back(ConstantUInt::get(Type::UIntTy, 0));
287 Instruction *stInstr=new StoreInst(ConstantInt::get(Type::IntTy, 0), rVar,
290 //now push all instructions in front of the BB
291 BasicBlock::InstListType& instList=front->getInstList();
292 BasicBlock::iterator here=instList.begin();
293 here=++front->getInstList().insert(here, rVar);
294 here=++front->getInstList().insert(here,countVar);
296 //Initialize Count[...] with 0
298 for(int i=0;i<k; i++){
299 Instruction *stInstrC=new
300 StoreInst(ConstantInt::get(Type::IntTy, 0),
301 countVar, std::vector<Value *>
302 (1,ConstantUInt::get(Type::UIntTy, i)));
303 here=++front->getInstList().insert(here,stInstrC);
306 here = ++front->getInstList().insert(here,stInstr);
310 //insert a basic block with appropriate code
312 void insertBB(Edge ed,
313 getEdgeCode *edgeCode,
315 Instruction *countInst,
316 int numPaths, int Methno){
319 BasicBlock* BB1=ed.getFirst()->getElement();
320 BasicBlock* BB2=ed.getSecond()->getElement();
322 #ifdef DEBUG_PATH_PROFILES
324 cerr<<"Edges with codes ######################\n";
325 cerr<<BB1->getName()<<"->"<<BB2->getName()<<"\n";
326 cerr<<"########################\n";
329 char counterstr[100];
330 sprintf(counterstr,"counter%d",i);
331 std::string ctr=counterstr;
333 //We need to insert a BB between BB1 and BB2
334 TerminatorInst *TI=BB1->getTerminator();
335 BasicBlock *newBB=new BasicBlock(ctr, BB1->getParent());
337 //get code for the new BB
338 edgeCode->getCode(rInst, countInst, BB1->getParent(), newBB, numPaths, Methno);
340 //Is terminator a branch instruction?
341 //then we need to change branch destinations to include new BB
343 BranchInst *BI=cast<BranchInst>(TI);
345 if(BI->isUnconditional()){
346 BI->setUnconditionalDest(newBB);
347 Instruction *newBI2=new BranchInst(BB2);
348 newBB->getInstList().push_back(newBI2);
351 if(BI->getSuccessor(0)==BB2)
352 BI->setSuccessor(0, newBB);
354 if(BI->getSuccessor(1)==BB2)
355 BI->setSuccessor(1, newBB);
357 Instruction *newBI2=new BranchInst(BB2);
358 newBB->getInstList().push_back(newBI2);
361 //get code for the new BB
362 //now iterate over BB2, and set its Phi nodes right
363 for(BasicBlock::iterator BB2Inst = BB2->begin(), BBend = BB2->end();
364 BB2Inst != BBend; ++BB2Inst){
366 if(PHINode *phiInst=dyn_cast<PHINode>(&*BB2Inst)){
367 int bbIndex=phiInst->getBasicBlockIndex(BB1);
369 phiInst->setIncomingBlock(bbIndex, newBB);