1 //===-- EdgeCode.cpp - generate LLVM instrumentation code -----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
9 //It implements the class EdgeCode: which provides
10 //support for inserting "appropriate" instrumentation at
11 //designated points in the graph
13 //It also has methods to insert initialization code in
15 //===----------------------------------------------------------------------===//
18 #include "llvm/Constants.h"
19 #include "llvm/DerivedTypes.h"
20 #include "llvm/Instructions.h"
21 #include "llvm/Module.h"
23 #define INSERT_LOAD_COUNT
31 static void getTriggerCode(Module *M, BasicBlock *BB, int MethNo, Value *pathNo,
32 Value *cnt, Instruction *rInst){
34 vector<Value *> tmpVec;
35 tmpVec.push_back(Constant::getNullValue(Type::LongTy));
36 tmpVec.push_back(Constant::getNullValue(Type::LongTy));
37 Instruction *Idx = new GetElementPtrInst(cnt, tmpVec, "");//,
38 BB->getInstList().push_back(Idx);
40 const Type *PIntTy = PointerType::get(Type::IntTy);
41 Function *trigMeth = M->getOrInsertFunction("trigger", Type::VoidTy,
42 Type::IntTy, Type::IntTy,
44 assert(trigMeth && "trigger method could not be inserted!");
46 vector<Value *> trargs;
48 trargs.push_back(ConstantSInt::get(Type::IntTy,MethNo));
49 trargs.push_back(pathNo);
50 trargs.push_back(Idx);
51 trargs.push_back(rInst);
53 Instruction *callInst=new CallInst(trigMeth, trargs, "");//, BB->begin());
54 BB->getInstList().push_back(callInst);
55 //triggerInst = new CallInst(trigMeth, trargs, "");//, InsertPos);
59 //get the code to be inserted on the edge
60 //This is determined from cond (1-6)
61 void getEdgeCode::getCode(Instruction *rInst, Value *countInst,
62 Function *M, BasicBlock *BB,
63 vector<Value *> &retVec){
65 //Instruction *InsertPos = BB->getInstList().begin();
67 //now check for cdIn and cdOut
70 cdOut->getCode(rInst, countInst, M, BB, retVec);
74 cdIn->getCode(rInst, countInst, M, BB, retVec);
77 //case: r=k code to be inserted
80 Value *val=ConstantSInt::get(Type::IntTy,inc);
82 Instruction *stInst=new StoreInst(val, rInst);//, InsertPos);
83 BB->getInstList().push_back(stInst);
88 //case: r=0 to be inserted
91 Instruction *stInst = new StoreInst(ConstantSInt::getNullValue(Type::IntTy), rInst);//, InsertPos);
92 BB->getInstList().push_back(stInst);
99 Instruction *ldInst = new LoadInst(rInst, "ti1");//, InsertPos);
100 BB->getInstList().push_back(ldInst);
101 Value *val = ConstantSInt::get(Type::IntTy,inc);
102 Instruction *addIn = BinaryOperator::create(Instruction::Add, ldInst, val,
103 "ti2");//, InsertPos);
104 BB->getInstList().push_back(addIn);
106 Instruction *stInst = new StoreInst(addIn, rInst);//, InsertPos);
107 BB->getInstList().push_back(stInst);
114 vector<Value *> tmpVec;
115 tmpVec.push_back(Constant::getNullValue(Type::LongTy));
116 tmpVec.push_back(ConstantSInt::get(Type::LongTy, inc));
117 Instruction *Idx = new GetElementPtrInst(countInst, tmpVec, "");//,
119 //Instruction *Idx = new GetElementPtrInst(countInst,
120 // vector<Value*>(1,ConstantSInt::get(Type::LongTy, inc)),
121 // "");//, InsertPos);
122 BB->getInstList().push_back(Idx);
124 Instruction *ldInst=new LoadInst(Idx, "ti1");//, InsertPos);
125 BB->getInstList().push_back(ldInst);
127 Value *val = ConstantSInt::get(Type::IntTy, 1);
128 //Instruction *addIn =
129 Instruction *newCount =
130 BinaryOperator::create(Instruction::Add, ldInst, val,"ti2");
131 BB->getInstList().push_back(newCount);
135 //Instruction *stInst=new StoreInst(addIn, Idx, InsertPos);
136 Instruction *stInst=new StoreInst(newCount, Idx);//, InsertPos);
137 BB->getInstList().push_back(stInst);
140 Value *trAddIndex = ConstantSInt::get(Type::IntTy,inc);
142 retVec.push_back(newCount);
143 retVec.push_back(trAddIndex);
145 //getTriggerCode(M->getParent(), BB, MethNo,
146 // ConstantSInt::get(Type::IntTy,inc), newCount, triggerInst);
149 assert(inc>=0 && "IT MUST BE POSITIVE NOW");
153 //case: count[r+inc]++
157 Instruction *ldIndex=new LoadInst(rInst, "ti1");//, InsertPos);
158 BB->getInstList().push_back(ldIndex);
160 Value *val=ConstantSInt::get(Type::IntTy,inc);
161 Instruction *addIndex=BinaryOperator::
162 create(Instruction::Add, ldIndex, val,"ti2");//, InsertPos);
163 BB->getInstList().push_back(addIndex);
165 //now load count[addIndex]
166 Instruction *castInst=new CastInst(addIndex,
167 Type::LongTy,"ctin");//, InsertPos);
168 BB->getInstList().push_back(castInst);
170 vector<Value *> tmpVec;
171 tmpVec.push_back(Constant::getNullValue(Type::LongTy));
172 tmpVec.push_back(castInst);
173 Instruction *Idx = new GetElementPtrInst(countInst, tmpVec, "");//,
175 BB->getInstList().push_back(Idx);
177 Instruction *ldInst=new LoadInst(Idx, "ti3");//, InsertPos);
178 BB->getInstList().push_back(ldInst);
180 Value *cons=ConstantSInt::get(Type::IntTy,1);
182 //std::cerr<<"Type ldInst:"<<ldInst->getType()<<"\t cons:"<<cons->getType()<<"\n";
183 Instruction *newCount = BinaryOperator::create(Instruction::Add, ldInst,
185 BB->getInstList().push_back(newCount);
188 Instruction *stInst = new StoreInst(newCount, Idx);//, InsertPos);
189 BB->getInstList().push_back(stInst);
192 retVec.push_back(newCount);
193 retVec.push_back(addIndex);
195 //getTriggerCode(M->getParent(), BB, MethNo, addIndex, newCount, triggerInst);
204 Instruction *ldIndex=new LoadInst(rInst, "ti1");//, InsertPos);
205 BB->getInstList().push_back(ldIndex);
207 //now load count[addIndex]
208 Instruction *castInst2=new CastInst(ldIndex, Type::LongTy,"ctin");
209 BB->getInstList().push_back(castInst2);
211 vector<Value *> tmpVec;
212 tmpVec.push_back(Constant::getNullValue(Type::LongTy));
213 tmpVec.push_back(castInst2);
214 Instruction *Idx = new GetElementPtrInst(countInst, tmpVec, "");//,
216 //Instruction *Idx = new GetElementPtrInst(countInst,
217 // vector<Value*>(1,castInst2), "");
219 BB->getInstList().push_back(Idx);
221 Instruction *ldInst=new LoadInst(Idx, "ti2");//, InsertPos);
222 BB->getInstList().push_back(ldInst);
224 Value *cons=ConstantSInt::get(Type::IntTy,1);
227 Instruction *newCount = BinaryOperator::create(Instruction::Add, ldInst,
229 BB->getInstList().push_back(newCount);
232 Instruction *stInst = new StoreInst(newCount, Idx);//, InsertPos);
233 BB->getInstList().push_back(stInst);
236 retVec.push_back(newCount);
237 retVec.push_back(ldIndex);
246 //Insert the initialization code in the top BB
247 //this includes initializing r, and count
248 //r is like an accumulator, that
249 //keeps on adding increments as we traverse along a path
250 //and at the end of the path, r contains the path
251 //number of that path
252 //Count is an array, where Count[k] represents
253 //the number of executions of path k
254 void insertInTopBB(BasicBlock *front,
256 Instruction *rVar, Value *threshold){
257 //rVar is variable r,
258 //countVar is count[]
260 Value *Int0 = ConstantInt::get(Type::IntTy, 0);
262 //now push all instructions in front of the BB
263 BasicBlock::iterator here=front->begin();
264 front->getInstList().insert(here, rVar);
265 //front->getInstList().insert(here,countVar);
267 //Initialize Count[...] with 0
269 //for (int i=0;i<k; i++){
270 //Value *GEP2 = new GetElementPtrInst(countVar,
271 // vector<Value *>(1,ConstantSInt::get(Type::LongTy, i)),
273 //new StoreInst(Int0, GEP2, here);
276 //store uint 0, uint *%R
277 new StoreInst(Int0, rVar, here);
281 //insert a basic block with appropriate code
283 void insertBB(Edge ed,
284 getEdgeCode *edgeCode,
287 int numPaths, int Methno, Value *threshold){
289 BasicBlock* BB1=ed.getFirst()->getElement();
290 BasicBlock* BB2=ed.getSecond()->getElement();
292 #ifdef DEBUG_PATH_PROFILES
294 cerr<<"Edges with codes ######################\n";
295 cerr<<BB1->getName()<<"->"<<BB2->getName()<<"\n";
296 cerr<<"########################\n";
299 //We need to insert a BB between BB1 and BB2
300 TerminatorInst *TI=BB1->getTerminator();
301 BasicBlock *newBB=new BasicBlock("counter", BB1->getParent());
303 //get code for the new BB
304 vector<Value *> retVec;
306 edgeCode->getCode(rInst, countInst, BB1->getParent(), newBB, retVec);
308 BranchInst *BI = cast<BranchInst>(TI);
310 //Is terminator a branch instruction?
311 //then we need to change branch destinations to include new BB
313 if(BI->isUnconditional()){
314 BI->setUnconditionalDest(newBB);
317 if(BI->getSuccessor(0)==BB2)
318 BI->setSuccessor(0, newBB);
320 if(BI->getSuccessor(1)==BB2)
321 BI->setSuccessor(1, newBB);
324 BasicBlock *triggerBB = NULL;
326 triggerBB = new BasicBlock("trigger", BB1->getParent());
327 getTriggerCode(BB1->getParent()->getParent(), triggerBB, Methno,
328 retVec[1], countInst, rInst);//retVec[0]);
330 //Instruction *castInst = new CastInst(retVec[0], Type::IntTy, "");
331 Instruction *etr = new LoadInst(threshold, "threshold");
333 //std::cerr<<"type1: "<<etr->getType()<<" type2: "<<retVec[0]->getType()<<"\n";
334 Instruction *cmpInst = new SetCondInst(Instruction::SetLE, etr,
336 Instruction *newBI2 = new BranchInst(triggerBB, BB2, cmpInst);
337 //newBB->getInstList().push_back(castInst);
338 newBB->getInstList().push_back(etr);
339 newBB->getInstList().push_back(cmpInst);
340 newBB->getInstList().push_back(newBI2);
342 //triggerBB->getInstList().push_back(triggerInst);
343 new BranchInst(BB2, 0, 0, triggerBB);
346 new BranchInst(BB2, 0, 0, newBB);
349 //now iterate over BB2, and set its Phi nodes right
350 for(BasicBlock::iterator BB2Inst = BB2->begin(), BBend = BB2->end();
351 BB2Inst != BBend; ++BB2Inst){
353 if(PHINode *phiInst=dyn_cast<PHINode>(BB2Inst)){
354 int bbIndex=phiInst->getBasicBlockIndex(BB1);
356 phiInst->setIncomingBlock(bbIndex, newBB);
358 ///check if trigger!=null, then add value corresponding to it too!
360 assert(triggerBB && "BasicBlock with trigger should not be null!");
361 Value *vl = phiInst->getIncomingValue((unsigned int)bbIndex);
362 phiInst->addIncoming(vl, triggerBB);
368 } // End llvm namespace