1 //===-- EdgeCode.cpp - generate LLVM instrumentation code -----------------===//
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 //===----------------------------------------------------------------------===//
11 #include "llvm/Constants.h"
12 #include "llvm/DerivedTypes.h"
13 #include "llvm/iMemory.h"
14 #include "llvm/iTerminators.h"
15 #include "llvm/iOther.h"
16 #include "llvm/iOperators.h"
17 #include "llvm/iPHINode.h"
18 #include "llvm/Module.h"
21 #define INSERT_LOAD_COUNT
27 static void getTriggerCode(Module *M, BasicBlock *BB, int MethNo, Value *pathNo,
30 vector<const Type*> args;
31 //args.push_back(PointerType::get(Type::SByteTy));
32 args.push_back(Type::IntTy);
33 args.push_back(Type::IntTy);
34 //args.push_back(Type::IntTy);
35 args.push_back(PointerType::get(Type::IntTy));
36 const FunctionType *MTy = FunctionType::get(Type::VoidTy, args, false);
38 Function *trigMeth = M->getOrInsertFunction("trigger", MTy);
39 assert(trigMeth && "trigger method could not be inserted!");
41 vector<Value *> trargs;
43 trargs.push_back(ConstantSInt::get(Type::IntTy,MethNo));
44 trargs.push_back(pathNo);
45 trargs.push_back(cnt);
47 Instruction *callInst=new CallInst(trigMeth, trargs, "");//, BB->begin());
48 BB->getInstList().push_back(callInst);
49 //triggerInst = new CallInst(trigMeth, trargs, "");//, InsertPos);
53 //get the code to be inserted on the edge
54 //This is determined from cond (1-6)
55 void getEdgeCode::getCode(Instruction *rInst, Instruction *countInst,
56 Function *M, BasicBlock *BB,
57 vector<Value *> &retVec){
59 //Instruction *InsertPos = BB->getInstList().begin();
61 //now check for cdIn and cdOut
64 cdOut->getCode(rInst, countInst, M, BB, retVec);
68 cdIn->getCode(rInst, countInst, M, BB, retVec);
71 //case: r=k code to be inserted
74 Value *val=ConstantSInt::get(Type::IntTy,inc);
76 Instruction *stInst=new StoreInst(val, rInst);//, InsertPos);
77 BB->getInstList().push_back(stInst);
82 //case: r=0 to be inserted
85 Instruction *stInst = new StoreInst(ConstantSInt::getNullValue(Type::IntTy), rInst);//, InsertPos);
86 BB->getInstList().push_back(stInst);
93 Instruction *ldInst = new LoadInst(rInst, "ti1");//, InsertPos);
94 BB->getInstList().push_back(ldInst);
95 Value *val = ConstantSInt::get(Type::IntTy,inc);
96 Instruction *addIn = BinaryOperator::create(Instruction::Add, ldInst, val,
97 "ti2");//, InsertPos);
98 BB->getInstList().push_back(addIn);
100 Instruction *stInst = new StoreInst(addIn, rInst);//, InsertPos);
101 BB->getInstList().push_back(stInst);
108 Instruction *Idx = new GetElementPtrInst(countInst,
109 vector<Value*>(1,ConstantSInt::get(Type::LongTy, inc)),
111 BB->getInstList().push_back(Idx);
113 Instruction *ldInst=new LoadInst(Idx, "ti1");//, InsertPos);
114 BB->getInstList().push_back(ldInst);
116 Value *val = ConstantSInt::get(Type::IntTy, 1);
117 //Instruction *addIn =
118 Instruction *newCount =
119 BinaryOperator::create(Instruction::Add, ldInst, val,"ti2");
120 BB->getInstList().push_back(newCount);
124 //Instruction *stInst=new StoreInst(addIn, Idx, InsertPos);
125 Instruction *stInst=new StoreInst(newCount, Idx);//, InsertPos);
126 BB->getInstList().push_back(stInst);
129 Value *trAddIndex = ConstantSInt::get(Type::IntTy,inc);
131 retVec.push_back(newCount);
132 retVec.push_back(trAddIndex);
134 //getTriggerCode(M->getParent(), BB, MethNo,
135 // ConstantSInt::get(Type::IntTy,inc), newCount, triggerInst);
138 assert(inc>=0 && "IT MUST BE POSITIVE NOW");
142 //case: count[r+inc]++
146 Instruction *ldIndex=new LoadInst(rInst, "ti1");//, InsertPos);
147 BB->getInstList().push_back(ldIndex);
149 Value *val=ConstantSInt::get(Type::IntTy,inc);
150 Instruction *addIndex=BinaryOperator::
151 create(Instruction::Add, ldIndex, val,"ti2");//, InsertPos);
152 BB->getInstList().push_back(addIndex);
154 //now load count[addIndex]
155 Instruction *castInst=new CastInst(addIndex,
156 Type::LongTy,"ctin");//, InsertPos);
157 BB->getInstList().push_back(castInst);
159 Instruction *Idx = new GetElementPtrInst(countInst,
160 vector<Value*>(1,castInst), "");//,
162 BB->getInstList().push_back(Idx);
164 Instruction *ldInst=new LoadInst(Idx, "ti3");//, InsertPos);
165 BB->getInstList().push_back(ldInst);
167 Value *cons=ConstantSInt::get(Type::IntTy,1);
169 Instruction *newCount = BinaryOperator::create(Instruction::Add, ldInst,
171 BB->getInstList().push_back(newCount);
174 Instruction *stInst = new StoreInst(newCount, Idx);//, InsertPos);
175 BB->getInstList().push_back(stInst);
178 retVec.push_back(newCount);
179 retVec.push_back(addIndex);
181 //getTriggerCode(M->getParent(), BB, MethNo, addIndex, newCount, triggerInst);
190 Instruction *ldIndex=new LoadInst(rInst, "ti1");//, InsertPos);
191 BB->getInstList().push_back(ldIndex);
193 //now load count[addIndex]
194 Instruction *castInst2=new CastInst(ldIndex, Type::LongTy,"ctin");
195 BB->getInstList().push_back(castInst2);
197 Instruction *Idx = new GetElementPtrInst(countInst,
198 vector<Value*>(1,castInst2), "");
199 BB->getInstList().push_back(Idx);
201 Instruction *ldInst=new LoadInst(Idx, "ti2");//, InsertPos);
202 BB->getInstList().push_back(ldInst);
204 Value *cons=ConstantSInt::get(Type::IntTy,1);
207 Instruction *newCount = BinaryOperator::create(Instruction::Add, ldInst,
209 BB->getInstList().push_back(newCount);
212 Instruction *stInst = new StoreInst(newCount, Idx);//, InsertPos);
213 BB->getInstList().push_back(stInst);
216 retVec.push_back(newCount);
217 retVec.push_back(ldIndex);
226 //Insert the initialization code in the top BB
227 //this includes initializing r, and count
228 //r is like an accumulator, that
229 //keeps on adding increments as we traverse along a path
230 //and at the end of the path, r contains the path
231 //number of that path
232 //Count is an array, where Count[k] represents
233 //the number of executions of path k
234 void insertInTopBB(BasicBlock *front,
237 Instruction *countVar, Value *threshold){
238 //rVar is variable r,
239 //countVar is array Count, and these are allocatted outside
241 Value *Int0 = ConstantInt::get(Type::IntTy, 0);
243 //now push all instructions in front of the BB
244 BasicBlock::iterator here=front->begin();
245 front->getInstList().insert(here, rVar);
246 front->getInstList().insert(here,countVar);
248 //Initialize Count[...] with 0
250 //for (int i=0;i<k; i++){
251 //Value *GEP2 = new GetElementPtrInst(countVar,
252 // vector<Value *>(1,ConstantSInt::get(Type::LongTy, i)),
254 //new StoreInst(Int0, GEP2, here);
257 //store uint 0, uint *%R
258 new StoreInst(Int0, rVar, here);
260 //insert initialize function for initializing
261 vector<const Type*> inCountArgs;
262 inCountArgs.push_back(PointerType::get(Type::IntTy));
263 inCountArgs.push_back(Type::IntTy);
265 const FunctionType *cFty = FunctionType::get(Type::VoidTy, inCountArgs,
267 Function *inCountMth = front->getParent()->getParent()->getOrInsertFunction("llvmInitializeCounter", cFty);
268 assert(inCountMth && "Initialize method could not be inserted!");
270 vector<Value *> iniArgs;
271 iniArgs.push_back(countVar);
272 iniArgs.push_back(ConstantSInt::get(Type::IntTy, k));
273 new CallInst(inCountMth, iniArgs, "", here);
276 if(front->getParent()->getName() == "main"){
277 //intialize threshold
278 vector<const Type*> initialize_args;
279 initialize_args.push_back(PointerType::get(Type::IntTy));
281 const FunctionType *Fty = FunctionType::get(Type::VoidTy, initialize_args,
283 Function *initialMeth = front->getParent()->getParent()->getOrInsertFunction("reoptimizerInitialize", Fty);
284 assert(initialMeth && "Initialize method could not be inserted!");
286 vector<Value *> trargs;
287 trargs.push_back(threshold);
289 new CallInst(initialMeth, trargs, "", here);
294 //insert a basic block with appropriate code
296 void insertBB(Edge ed,
297 getEdgeCode *edgeCode,
299 Instruction *countInst,
300 int numPaths, int Methno, Value *threshold){
302 BasicBlock* BB1=ed.getFirst()->getElement();
303 BasicBlock* BB2=ed.getSecond()->getElement();
305 #ifdef DEBUG_PATH_PROFILES
307 cerr<<"Edges with codes ######################\n";
308 cerr<<BB1->getName()<<"->"<<BB2->getName()<<"\n";
309 cerr<<"########################\n";
312 //We need to insert a BB between BB1 and BB2
313 TerminatorInst *TI=BB1->getTerminator();
314 BasicBlock *newBB=new BasicBlock("counter", BB1->getParent());
316 //get code for the new BB
317 vector<Value *> retVec;
319 edgeCode->getCode(rInst, countInst, BB1->getParent(), newBB, retVec);
321 BranchInst *BI = cast<BranchInst>(TI);
323 //Is terminator a branch instruction?
324 //then we need to change branch destinations to include new BB
326 if(BI->isUnconditional()){
327 BI->setUnconditionalDest(newBB);
330 if(BI->getSuccessor(0)==BB2)
331 BI->setSuccessor(0, newBB);
333 if(BI->getSuccessor(1)==BB2)
334 BI->setSuccessor(1, newBB);
337 BasicBlock *triggerBB = NULL;
339 triggerBB = new BasicBlock("trigger", BB1->getParent());
340 getTriggerCode(BB1->getParent()->getParent(), triggerBB, Methno,
341 retVec[1], countInst);//retVec[0]);
343 //Instruction *castInst = new CastInst(retVec[0], Type::IntTy, "");
344 Instruction *etr = new LoadInst(threshold, "threshold");
346 //std::cerr<<"type1: "<<etr->getType()<<" type2: "<<retVec[0]->getType()<<"\n";
347 Instruction *cmpInst = new SetCondInst(Instruction::SetLE, etr,
349 Instruction *newBI2 = new BranchInst(triggerBB, BB2, cmpInst);
350 //newBB->getInstList().push_back(castInst);
351 newBB->getInstList().push_back(etr);
352 newBB->getInstList().push_back(cmpInst);
353 newBB->getInstList().push_back(newBI2);
355 //triggerBB->getInstList().push_back(triggerInst);
356 Instruction *triggerBranch = new BranchInst(BB2);
357 triggerBB->getInstList().push_back(triggerBranch);
360 Instruction *newBI2=new BranchInst(BB2);
361 newBB->getInstList().push_back(newBI2);
364 //now iterate over BB2, and set its Phi nodes right
365 for(BasicBlock::iterator BB2Inst = BB2->begin(), BBend = BB2->end();
366 BB2Inst != BBend; ++BB2Inst){
368 if(PHINode *phiInst=dyn_cast<PHINode>(&*BB2Inst)){
369 int bbIndex=phiInst->getBasicBlockIndex(BB1);
371 phiInst->setIncomingBlock(bbIndex, newBB);
373 ///check if trigger!=null, then add value corresponding to it too!
375 assert(triggerBB && "BasicBlock with trigger should not be null!");
376 Value *vl = phiInst->getIncomingValue((unsigned int)bbIndex);
377 phiInst->addIncoming(vl, triggerBB);