EdgeCode.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. //===-- EdgeCode.cpp - generate LLVM instrumentation code -----------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  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.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //It implements the class EdgeCode: which provides
  10. //support for inserting "appropriate" instrumentation at
  11. //designated points in the graph
  12. //
  13. //It also has methods to insert initialization code in
  14. //top block of cfg
  15. //===----------------------------------------------------------------------===//
  16. #include "Graph.h"
  17. #include "llvm/Constants.h"
  18. #include "llvm/DerivedTypes.h"
  19. #include "llvm/Instructions.h"
  20. #include "llvm/Module.h"
  21. #define INSERT_LOAD_COUNT
  22. #define INSERT_STORE
  23. using std::vector;
  24. namespace llvm {
  25. static void getTriggerCode(Module *M, BasicBlock *BB, int MethNo, Value *pathNo,
  26. Value *cnt, Instruction *rInst){
  27. vector<Value *> tmpVec;
  28. tmpVec.push_back(Constant::getNullValue(Type::LongTy));
  29. tmpVec.push_back(Constant::getNullValue(Type::LongTy));
  30. Instruction *Idx = new GetElementPtrInst(cnt, tmpVec, "");//,
  31. BB->getInstList().push_back(Idx);
  32. const Type *PIntTy = PointerType::get(Type::IntTy);
  33. Function *trigMeth = M->getOrInsertFunction("trigger", Type::VoidTy,
  34. Type::IntTy, Type::IntTy,
  35. PIntTy, PIntTy, (Type *)0);
  36. assert(trigMeth && "trigger method could not be inserted!");
  37. vector<Value *> trargs;
  38. trargs.push_back(ConstantSInt::get(Type::IntTy,MethNo));
  39. trargs.push_back(pathNo);
  40. trargs.push_back(Idx);
  41. trargs.push_back(rInst);
  42. Instruction *callInst=new CallInst(trigMeth, trargs, "");//, BB->begin());
  43. BB->getInstList().push_back(callInst);
  44. //triggerInst = new CallInst(trigMeth, trargs, "");//, InsertPos);
  45. }
  46. //get the code to be inserted on the edge
  47. //This is determined from cond (1-6)
  48. void getEdgeCode::getCode(Instruction *rInst, Value *countInst,
  49. Function *M, BasicBlock *BB,
  50. vector<Value *> &retVec){
  51. //Instruction *InsertPos = BB->getInstList().begin();
  52. //now check for cdIn and cdOut
  53. //first put cdOut
  54. if(cdOut!=NULL){
  55. cdOut->getCode(rInst, countInst, M, BB, retVec);
  56. }
  57. if(cdIn!=NULL){
  58. cdIn->getCode(rInst, countInst, M, BB, retVec);
  59. }
  60. //case: r=k code to be inserted
  61. switch(cond){
  62. case 1:{
  63. Value *val=ConstantSInt::get(Type::IntTy,inc);
  64. #ifdef INSERT_STORE
  65. Instruction *stInst=new StoreInst(val, rInst);//, InsertPos);
  66. BB->getInstList().push_back(stInst);
  67. #endif
  68. break;
  69. }
  70. //case: r=0 to be inserted
  71. case 2:{
  72. #ifdef INSERT_STORE
  73. Instruction *stInst = new StoreInst(ConstantSInt::getNullValue(Type::IntTy), rInst);//, InsertPos);
  74. BB->getInstList().push_back(stInst);
  75. #endif
  76. break;
  77. }
  78. //r+=k
  79. case 3:{
  80. Instruction *ldInst = new LoadInst(rInst, "ti1");//, InsertPos);
  81. BB->getInstList().push_back(ldInst);
  82. Value *val = ConstantSInt::get(Type::IntTy,inc);
  83. Instruction *addIn = BinaryOperator::create(Instruction::Add, ldInst, val,
  84. "ti2");//, InsertPos);
  85. BB->getInstList().push_back(addIn);
  86. #ifdef INSERT_STORE
  87. Instruction *stInst = new StoreInst(addIn, rInst);//, InsertPos);
  88. BB->getInstList().push_back(stInst);
  89. #endif
  90. break;
  91. }
  92. //count[inc]++
  93. case 4:{
  94. vector<Value *> tmpVec;
  95. tmpVec.push_back(Constant::getNullValue(Type::LongTy));
  96. tmpVec.push_back(ConstantSInt::get(Type::LongTy, inc));
  97. Instruction *Idx = new GetElementPtrInst(countInst, tmpVec, "");//,
  98. //Instruction *Idx = new GetElementPtrInst(countInst,
  99. // vector<Value*>(1,ConstantSInt::get(Type::LongTy, inc)),
  100. // "");//, InsertPos);
  101. BB->getInstList().push_back(Idx);
  102. Instruction *ldInst=new LoadInst(Idx, "ti1");//, InsertPos);
  103. BB->getInstList().push_back(ldInst);
  104. Value *val = ConstantSInt::get(Type::IntTy, 1);
  105. //Instruction *addIn =
  106. Instruction *newCount =
  107. BinaryOperator::create(Instruction::Add, ldInst, val,"ti2");
  108. BB->getInstList().push_back(newCount);
  109. #ifdef INSERT_STORE
  110. //Instruction *stInst=new StoreInst(addIn, Idx, InsertPos);
  111. Instruction *stInst=new StoreInst(newCount, Idx);//, InsertPos);
  112. BB->getInstList().push_back(stInst);
  113. #endif
  114. Value *trAddIndex = ConstantSInt::get(Type::IntTy,inc);
  115. retVec.push_back(newCount);
  116. retVec.push_back(trAddIndex);
  117. //insert trigger
  118. //getTriggerCode(M->getParent(), BB, MethNo,
  119. // ConstantSInt::get(Type::IntTy,inc), newCount, triggerInst);
  120. //end trigger code
  121. assert(inc>=0 && "IT MUST BE POSITIVE NOW");
  122. break;
  123. }
  124. //case: count[r+inc]++
  125. case 5:{
  126. //ti1=inc+r
  127. Instruction *ldIndex=new LoadInst(rInst, "ti1");//, InsertPos);
  128. BB->getInstList().push_back(ldIndex);
  129. Value *val=ConstantSInt::get(Type::IntTy,inc);
  130. Instruction *addIndex=BinaryOperator::
  131. create(Instruction::Add, ldIndex, val,"ti2");//, InsertPos);
  132. BB->getInstList().push_back(addIndex);
  133. //now load count[addIndex]
  134. Instruction *castInst=new CastInst(addIndex,
  135. Type::LongTy,"ctin");//, InsertPos);
  136. BB->getInstList().push_back(castInst);
  137. vector<Value *> tmpVec;
  138. tmpVec.push_back(Constant::getNullValue(Type::LongTy));
  139. tmpVec.push_back(castInst);
  140. Instruction *Idx = new GetElementPtrInst(countInst, tmpVec, "");//,
  141. // InsertPos);
  142. BB->getInstList().push_back(Idx);
  143. Instruction *ldInst=new LoadInst(Idx, "ti3");//, InsertPos);
  144. BB->getInstList().push_back(ldInst);
  145. Value *cons=ConstantSInt::get(Type::IntTy,1);
  146. //count[addIndex]++
  147. //std::cerr<<"Type ldInst:"<<ldInst->getType()<<"\t cons:"<<cons->getType()<<"\n";
  148. Instruction *newCount = BinaryOperator::create(Instruction::Add, ldInst,
  149. cons,"");
  150. BB->getInstList().push_back(newCount);
  151. #ifdef INSERT_STORE
  152. Instruction *stInst = new StoreInst(newCount, Idx);//, InsertPos);
  153. BB->getInstList().push_back(stInst);
  154. #endif
  155. retVec.push_back(newCount);
  156. retVec.push_back(addIndex);
  157. //insert trigger
  158. //getTriggerCode(M->getParent(), BB, MethNo, addIndex, newCount, triggerInst);
  159. //end trigger code
  160. break;
  161. }
  162. //case: count[r]+
  163. case 6:{
  164. //ti1=inc+r
  165. Instruction *ldIndex=new LoadInst(rInst, "ti1");//, InsertPos);
  166. BB->getInstList().push_back(ldIndex);
  167. //now load count[addIndex]
  168. Instruction *castInst2=new CastInst(ldIndex, Type::LongTy,"ctin");
  169. BB->getInstList().push_back(castInst2);
  170. vector<Value *> tmpVec;
  171. tmpVec.push_back(Constant::getNullValue(Type::LongTy));
  172. tmpVec.push_back(castInst2);
  173. Instruction *Idx = new GetElementPtrInst(countInst, tmpVec, "");//,
  174. //Instruction *Idx = new GetElementPtrInst(countInst,
  175. // vector<Value*>(1,castInst2), "");
  176. BB->getInstList().push_back(Idx);
  177. Instruction *ldInst=new LoadInst(Idx, "ti2");//, InsertPos);
  178. BB->getInstList().push_back(ldInst);
  179. Value *cons=ConstantSInt::get(Type::IntTy,1);
  180. //count[addIndex]++
  181. Instruction *newCount = BinaryOperator::create(Instruction::Add, ldInst,
  182. cons,"ti3");
  183. BB->getInstList().push_back(newCount);
  184. #ifdef INSERT_STORE
  185. Instruction *stInst = new StoreInst(newCount, Idx);//, InsertPos);
  186. BB->getInstList().push_back(stInst);
  187. #endif
  188. retVec.push_back(newCount);
  189. retVec.push_back(ldIndex);
  190. break;
  191. }
  192. }
  193. }
  194. //Insert the initialization code in the top BB
  195. //this includes initializing r, and count
  196. //r is like an accumulator, that
  197. //keeps on adding increments as we traverse along a path
  198. //and at the end of the path, r contains the path
  199. //number of that path
  200. //Count is an array, where Count[k] represents
  201. //the number of executions of path k
  202. void insertInTopBB(BasicBlock *front,
  203. int k,
  204. Instruction *rVar, Value *threshold){
  205. //rVar is variable r,
  206. //countVar is count[]
  207. Value *Int0 = ConstantInt::get(Type::IntTy, 0);
  208. //now push all instructions in front of the BB
  209. BasicBlock::iterator here=front->begin();
  210. front->getInstList().insert(here, rVar);
  211. //front->getInstList().insert(here,countVar);
  212. //Initialize Count[...] with 0
  213. //for (int i=0;i<k; i++){
  214. //Value *GEP2 = new GetElementPtrInst(countVar,
  215. // vector<Value *>(1,ConstantSInt::get(Type::LongTy, i)),
  216. // "", here);
  217. //new StoreInst(Int0, GEP2, here);
  218. //}
  219. //store uint 0, uint *%R
  220. new StoreInst(Int0, rVar, here);
  221. }
  222. //insert a basic block with appropriate code
  223. //along a given edge
  224. void insertBB(Edge ed,
  225. getEdgeCode *edgeCode,
  226. Instruction *rInst,
  227. Value *countInst,
  228. int numPaths, int Methno, Value *threshold){
  229. BasicBlock* BB1=ed.getFirst()->getElement();
  230. BasicBlock* BB2=ed.getSecond()->getElement();
  231. #ifdef DEBUG_PATH_PROFILES
  232. //debugging info
  233. cerr<<"Edges with codes ######################\n";
  234. cerr<<BB1->getName()<<"->"<<BB2->getName()<<"\n";
  235. cerr<<"########################\n";
  236. #endif
  237. //We need to insert a BB between BB1 and BB2
  238. TerminatorInst *TI=BB1->getTerminator();
  239. BasicBlock *newBB=new BasicBlock("counter", BB1->getParent());
  240. //get code for the new BB
  241. vector<Value *> retVec;
  242. edgeCode->getCode(rInst, countInst, BB1->getParent(), newBB, retVec);
  243. BranchInst *BI = cast<BranchInst>(TI);
  244. //Is terminator a branch instruction?
  245. //then we need to change branch destinations to include new BB
  246. if(BI->isUnconditional()){
  247. BI->setUnconditionalDest(newBB);
  248. }
  249. else{
  250. if(BI->getSuccessor(0)==BB2)
  251. BI->setSuccessor(0, newBB);
  252. if(BI->getSuccessor(1)==BB2)
  253. BI->setSuccessor(1, newBB);
  254. }
  255. BasicBlock *triggerBB = NULL;
  256. if(retVec.size()>0){
  257. triggerBB = new BasicBlock("trigger", BB1->getParent());
  258. getTriggerCode(BB1->getParent()->getParent(), triggerBB, Methno,
  259. retVec[1], countInst, rInst);//retVec[0]);
  260. //Instruction *castInst = new CastInst(retVec[0], Type::IntTy, "");
  261. Instruction *etr = new LoadInst(threshold, "threshold");
  262. //std::cerr<<"type1: "<<etr->getType()<<" type2: "<<retVec[0]->getType()<<"\n";
  263. Instruction *cmpInst = new SetCondInst(Instruction::SetLE, etr,
  264. retVec[0], "");
  265. Instruction *newBI2 = new BranchInst(triggerBB, BB2, cmpInst);
  266. //newBB->getInstList().push_back(castInst);
  267. newBB->getInstList().push_back(etr);
  268. newBB->getInstList().push_back(cmpInst);
  269. newBB->getInstList().push_back(newBI2);
  270. //triggerBB->getInstList().push_back(triggerInst);
  271. new BranchInst(BB2, 0, 0, triggerBB);
  272. }
  273. else{
  274. new BranchInst(BB2, 0, 0, newBB);
  275. }
  276. //now iterate over BB2, and set its Phi nodes right
  277. for(BasicBlock::iterator BB2Inst = BB2->begin(), BBend = BB2->end();
  278. BB2Inst != BBend; ++BB2Inst){
  279. if(PHINode *phiInst=dyn_cast<PHINode>(BB2Inst)){
  280. int bbIndex=phiInst->getBasicBlockIndex(BB1);
  281. assert(bbIndex>=0);
  282. phiInst->setIncomingBlock(bbIndex, newBB);
  283. ///check if trigger!=null, then add value corresponding to it too!
  284. if(retVec.size()>0){
  285. assert(triggerBB && "BasicBlock with trigger should not be null!");
  286. Value *vl = phiInst->getIncomingValue((unsigned int)bbIndex);
  287. phiInst->addIncoming(vl, triggerBB);
  288. }
  289. }
  290. }
  291. }
  292. } // End llvm namespace