Chris Lattner | 5c44786 | 2002-09-10 17:03:06 +0000 | [diff] [blame] | 1 | //===-- EdgeCode.cpp - generate LLVM instrumentation code -----------------===// |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 2 | //It implements the class EdgeCode: which provides |
| 3 | //support for inserting "appropriate" instrumentation at |
| 4 | //designated points in the graph |
| 5 | // |
| 6 | //It also has methods to insert initialization code in |
| 7 | //top block of cfg |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 10 | #include "llvm/Transforms/Instrumentation/Graph.h" |
Chris Lattner | ca14237 | 2002-04-28 19:55:58 +0000 | [diff] [blame] | 11 | #include "llvm/Constants.h" |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 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" |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 18 | #include "llvm/Module.h" |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 19 | #include <string.h> |
| 20 | #include <stdio.h> |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 21 | |
| 22 | #define INSERT_LOAD_COUNT |
| 23 | #define INSERT_STORE |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 24 | |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 25 | using std::vector; |
| 26 | |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 27 | |
Chris Lattner | 5c44786 | 2002-09-10 17:03:06 +0000 | [diff] [blame] | 28 | static void getTriggerCode(Module *M, BasicBlock *BB, int MethNo, Value *pathNo, |
| 29 | Value *cnt, Instruction *InsertPos){ |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 30 | static int i=-1; |
| 31 | i++; |
| 32 | char gstr[100]; |
| 33 | sprintf(gstr,"globalVar%d",i); |
| 34 | std::string globalVarName=gstr; |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 35 | vector<const Type*> args; |
Anand Shukla | ff72c79 | 2002-07-08 19:36:39 +0000 | [diff] [blame] | 36 | //args.push_back(PointerType::get(Type::SByteTy)); |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 37 | args.push_back(Type::IntTy); |
| 38 | args.push_back(Type::IntTy); |
| 39 | args.push_back(Type::IntTy); |
Chris Lattner | 5c44786 | 2002-09-10 17:03:06 +0000 | [diff] [blame] | 40 | const FunctionType *MTy = FunctionType::get(Type::VoidTy, args, false); |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 41 | |
| 42 | // Function *triggerMeth = M->getOrInsertFunction("trigger", MTy); |
| 43 | Function *trigMeth = M->getOrInsertFunction("trigger", MTy); |
| 44 | assert(trigMeth && "trigger method could not be inserted!"); |
| 45 | //if (Value *triggerMeth = ST->lookup(PointerType::get(MTy), "trigger")) { |
| 46 | //Function *trigMeth = cast<Function>(triggerMeth); |
| 47 | vector<Value *> trargs; |
| 48 | |
| 49 | //pred_iterator piter=BB->pred_begin(); |
Anand Shukla | ff72c79 | 2002-07-08 19:36:39 +0000 | [diff] [blame] | 50 | //std::string predName = "uu";//BB->getName(); |
| 51 | //Constant *bbName=ConstantArray::get(predName);//BB->getName()); |
| 52 | //GlobalVariable *gbl=new GlobalVariable(ArrayType::get(Type::SByteTy, |
| 53 | // predName.size()+1), |
| 54 | // true, true, bbName, gstr); |
| 55 | |
| 56 | //M->getGlobalList().push_back(gbl); |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 57 | |
Chris Lattner | b9d9e0f | 2002-09-11 01:21:29 +0000 | [diff] [blame] | 58 | //vector<Value *> elargs; |
| 59 | //elargs.push_back(ConstantSInt::get(Type::LongTy, 0)); |
| 60 | //elargs.push_back(ConstantSInt::get(Type::LongTy, 0)); |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 61 | |
Anand Shukla | ff72c79 | 2002-07-08 19:36:39 +0000 | [diff] [blame] | 62 | // commented out bb name frm which its called |
| 63 | //Instruction *getElmntInst=new GetElementPtrInst(gbl,elargs,"elmntInst"); |
| 64 | |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 65 | //trargs.push_back(ConstantArray::get(BB->getName())); |
Anand Shukla | ff72c79 | 2002-07-08 19:36:39 +0000 | [diff] [blame] | 66 | |
| 67 | //trargs.push_back(getElmntInst); |
| 68 | //trargs.push_back(bbName); |
| 69 | |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 70 | trargs.push_back(ConstantSInt::get(Type::IntTy,MethNo)); |
| 71 | |
| 72 | //trargs.push_back(ConstantSInt::get(Type::IntTy,-1));//erase this |
| 73 | trargs.push_back(pathNo); |
| 74 | trargs.push_back(cnt); |
Chris Lattner | 5c44786 | 2002-09-10 17:03:06 +0000 | [diff] [blame] | 75 | Instruction *callInst=new CallInst(trigMeth, trargs, "", InsertPos); |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 79 | //get the code to be inserted on the edge |
| 80 | //This is determined from cond (1-6) |
| 81 | void getEdgeCode::getCode(Instruction *rInst, |
| 82 | Instruction *countInst, |
Chris Lattner | f8e4dc3 | 2002-04-08 22:03:00 +0000 | [diff] [blame] | 83 | Function *M, |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 84 | BasicBlock *BB, int numPaths, int MethNo){ |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 85 | |
Chris Lattner | 5c44786 | 2002-09-10 17:03:06 +0000 | [diff] [blame] | 86 | Instruction *InsertPos = BB->begin(); |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 87 | |
| 88 | //case: r=k code to be inserted |
| 89 | switch(cond){ |
| 90 | case 1:{ |
| 91 | Value *val=ConstantSInt::get(Type::IntTy,inc); |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 92 | #ifdef INSERT_STORE |
Chris Lattner | 5c44786 | 2002-09-10 17:03:06 +0000 | [diff] [blame] | 93 | Instruction *stInst=new StoreInst(val, rInst, InsertPos); |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 94 | #endif |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 95 | break; |
| 96 | } |
| 97 | |
| 98 | //case: r=0 to be inserted |
Chris Lattner | 5c44786 | 2002-09-10 17:03:06 +0000 | [diff] [blame] | 99 | case 2: |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 100 | #ifdef INSERT_STORE |
Chris Lattner | 5c44786 | 2002-09-10 17:03:06 +0000 | [diff] [blame] | 101 | new StoreInst(ConstantSInt::getNullValue(Type::IntTy), rInst, InsertPos); |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 102 | #endif |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 103 | break; |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 104 | |
| 105 | //r+=k |
| 106 | case 3:{ |
Chris Lattner | 5c44786 | 2002-09-10 17:03:06 +0000 | [diff] [blame] | 107 | Instruction *ldInst = new LoadInst(rInst, "ti1", InsertPos); |
| 108 | Value *val = ConstantSInt::get(Type::IntTy,inc); |
| 109 | Value *addIn = BinaryOperator::create(Instruction::Add, ldInst, val, |
| 110 | "ti2", InsertPos); |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 111 | #ifdef INSERT_STORE |
Chris Lattner | 5c44786 | 2002-09-10 17:03:06 +0000 | [diff] [blame] | 112 | new StoreInst(addIn, rInst, InsertPos); |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 113 | #endif |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 114 | break; |
| 115 | } |
| 116 | |
| 117 | //count[inc]++ |
| 118 | case 4:{ |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 119 | assert(inc>=0 && inc<=numPaths && "inc out of bound!"); |
| 120 | |
Chris Lattner | e49f299 | 2002-08-21 22:11:33 +0000 | [diff] [blame] | 121 | Instruction *Idx = new GetElementPtrInst(countInst, |
Chris Lattner | b9d9e0f | 2002-09-11 01:21:29 +0000 | [diff] [blame] | 122 | vector<Value*>(1,ConstantSInt::get(Type::LongTy, inc)), |
Chris Lattner | 5c44786 | 2002-09-10 17:03:06 +0000 | [diff] [blame] | 123 | "", InsertPos); |
Chris Lattner | e49f299 | 2002-08-21 22:11:33 +0000 | [diff] [blame] | 124 | |
Chris Lattner | 5c44786 | 2002-09-10 17:03:06 +0000 | [diff] [blame] | 125 | Instruction *ldInst=new LoadInst(Idx, "ti1", InsertPos); |
Chris Lattner | e49f299 | 2002-08-21 22:11:33 +0000 | [diff] [blame] | 126 | |
| 127 | Value *val = ConstantSInt::get(Type::IntTy, 1); |
| 128 | Instruction *addIn = |
Chris Lattner | 5c44786 | 2002-09-10 17:03:06 +0000 | [diff] [blame] | 129 | BinaryOperator::create(Instruction::Add, ldInst, val,"ti2", InsertPos); |
| 130 | |
| 131 | #ifdef INSERT_STORE |
| 132 | Instruction *stInst=new StoreInst(addIn, Idx, InsertPos); |
| 133 | #endif |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 134 | |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 135 | //insert trigger |
| 136 | getTriggerCode(M->getParent(), BB, MethNo, |
Chris Lattner | 5c44786 | 2002-09-10 17:03:06 +0000 | [diff] [blame] | 137 | ConstantSInt::get(Type::IntTy,inc), addIn, InsertPos); |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 138 | //end trigger code |
| 139 | |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 140 | assert(inc>=0 && "IT MUST BE POSITIVE NOW"); |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 141 | break; |
| 142 | } |
| 143 | |
| 144 | //case: count[r+inc]++ |
| 145 | case 5:{ |
Chris Lattner | 5c44786 | 2002-09-10 17:03:06 +0000 | [diff] [blame] | 146 | |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 147 | //ti1=inc+r |
Chris Lattner | 5c44786 | 2002-09-10 17:03:06 +0000 | [diff] [blame] | 148 | Instruction *ldIndex=new LoadInst(rInst, "ti1", InsertPos); |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 149 | Value *val=ConstantSInt::get(Type::IntTy,inc); |
| 150 | Instruction *addIndex=BinaryOperator:: |
Chris Lattner | 5c44786 | 2002-09-10 17:03:06 +0000 | [diff] [blame] | 151 | create(Instruction::Add, ldIndex, val,"ti2", InsertPos); |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 152 | //erase following 1 line |
| 153 | //Value *valtemp=ConstantSInt::get(Type::IntTy,999); |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 154 | //now load count[addIndex] |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 155 | |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 156 | Instruction *castInst=new CastInst(addIndex, |
Chris Lattner | b9d9e0f | 2002-09-11 01:21:29 +0000 | [diff] [blame] | 157 | Type::LongTy,"ctin", InsertPos); |
Chris Lattner | e49f299 | 2002-08-21 22:11:33 +0000 | [diff] [blame] | 158 | Instruction *Idx = new GetElementPtrInst(countInst, |
Chris Lattner | 5c44786 | 2002-09-10 17:03:06 +0000 | [diff] [blame] | 159 | vector<Value*>(1,castInst), "", |
| 160 | InsertPos); |
Chris Lattner | e49f299 | 2002-08-21 22:11:33 +0000 | [diff] [blame] | 161 | |
Chris Lattner | 5c44786 | 2002-09-10 17:03:06 +0000 | [diff] [blame] | 162 | Instruction *ldInst=new LoadInst(Idx, "ti3", InsertPos); |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 163 | Value *cons=ConstantSInt::get(Type::IntTy,1); |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 164 | //count[addIndex]++ |
Chris Lattner | 5c44786 | 2002-09-10 17:03:06 +0000 | [diff] [blame] | 165 | Value *addIn = BinaryOperator::create(Instruction::Add, ldInst, cons, |
| 166 | "ti4", InsertPos); |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 167 | |
| 168 | #ifdef INSERT_STORE |
| 169 | ///* |
Chris Lattner | 5c44786 | 2002-09-10 17:03:06 +0000 | [diff] [blame] | 170 | new StoreInst(addIn, Idx, InsertPos); |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 171 | //*/ |
| 172 | #endif |
Chris Lattner | 5c44786 | 2002-09-10 17:03:06 +0000 | [diff] [blame] | 173 | |
| 174 | //insert trigger |
| 175 | getTriggerCode(M->getParent(), BB, MethNo, addIndex, addIn, InsertPos); |
| 176 | //end trigger code |
| 177 | |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 178 | break; |
| 179 | } |
| 180 | |
| 181 | //case: count[r]+ |
| 182 | case 6:{ |
| 183 | //ti1=inc+r |
Chris Lattner | 5c44786 | 2002-09-10 17:03:06 +0000 | [diff] [blame] | 184 | Instruction *ldIndex=new LoadInst(rInst, "ti1", InsertPos); |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 185 | |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 186 | //now load count[addIndex] |
Chris Lattner | b9d9e0f | 2002-09-11 01:21:29 +0000 | [diff] [blame] | 187 | Instruction *castInst2=new CastInst(ldIndex, Type::LongTy,"ctin",InsertPos); |
Chris Lattner | e49f299 | 2002-08-21 22:11:33 +0000 | [diff] [blame] | 188 | Instruction *Idx = new GetElementPtrInst(countInst, |
Chris Lattner | 5c44786 | 2002-09-10 17:03:06 +0000 | [diff] [blame] | 189 | vector<Value*>(1,castInst2), "", |
| 190 | InsertPos); |
Chris Lattner | e49f299 | 2002-08-21 22:11:33 +0000 | [diff] [blame] | 191 | |
Chris Lattner | 5c44786 | 2002-09-10 17:03:06 +0000 | [diff] [blame] | 192 | Instruction *ldInst=new LoadInst(Idx, "ti2", InsertPos); |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 193 | Value *cons=ConstantSInt::get(Type::IntTy,1); |
| 194 | |
| 195 | //count[addIndex]++ |
Chris Lattner | e49f299 | 2002-08-21 22:11:33 +0000 | [diff] [blame] | 196 | Instruction *addIn=BinaryOperator::create(Instruction::Add, ldInst, |
Chris Lattner | 5c44786 | 2002-09-10 17:03:06 +0000 | [diff] [blame] | 197 | cons,"ti3", InsertPos); |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 198 | |
Chris Lattner | 5c44786 | 2002-09-10 17:03:06 +0000 | [diff] [blame] | 199 | #ifdef INSERT_STORE |
| 200 | new StoreInst(addIn, Idx, InsertPos); |
| 201 | #endif |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 202 | //insert trigger |
Chris Lattner | 5c44786 | 2002-09-10 17:03:06 +0000 | [diff] [blame] | 203 | getTriggerCode(M->getParent(), BB, MethNo, ldIndex, addIn, InsertPos); |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 204 | //end trigger code |
Chris Lattner | 5c44786 | 2002-09-10 17:03:06 +0000 | [diff] [blame] | 205 | |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 206 | break; |
| 207 | } |
| 208 | |
| 209 | } |
| 210 | //now check for cdIn and cdOut |
| 211 | //first put cdOut |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 212 | if(cdIn!=NULL){ |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 213 | cdIn->getCode(rInst, countInst, M, BB, numPaths, MethNo); |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 214 | } |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 215 | if(cdOut!=NULL){ |
| 216 | cdOut->getCode(rInst, countInst, M, BB, numPaths, MethNo); |
| 217 | } |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | |
| 221 | |
| 222 | //Insert the initialization code in the top BB |
| 223 | //this includes initializing r, and count |
| 224 | //r is like an accumulator, that |
| 225 | //keeps on adding increments as we traverse along a path |
| 226 | //and at the end of the path, r contains the path |
| 227 | //number of that path |
| 228 | //Count is an array, where Count[k] represents |
| 229 | //the number of executions of path k |
| 230 | void insertInTopBB(BasicBlock *front, |
| 231 | int k, |
| 232 | Instruction *rVar, |
| 233 | Instruction *countVar){ |
| 234 | //rVar is variable r, |
| 235 | //countVar is array Count, and these are allocatted outside |
Chris Lattner | e49f299 | 2002-08-21 22:11:33 +0000 | [diff] [blame] | 236 | |
| 237 | Value *Int0 = ConstantInt::get(Type::IntTy, 0); |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 238 | |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 239 | //now push all instructions in front of the BB |
Chris Lattner | 5c44786 | 2002-09-10 17:03:06 +0000 | [diff] [blame] | 240 | BasicBlock::iterator here=front->begin(); |
| 241 | front->getInstList().insert(here, rVar); |
| 242 | front->getInstList().insert(here,countVar); |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 243 | |
| 244 | //Initialize Count[...] with 0 |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 245 | |
Chris Lattner | 5c44786 | 2002-09-10 17:03:06 +0000 | [diff] [blame] | 246 | for (int i=0;i<k; i++){ |
| 247 | Value *GEP2 = new GetElementPtrInst(countVar, |
Chris Lattner | b9d9e0f | 2002-09-11 01:21:29 +0000 | [diff] [blame] | 248 | vector<Value *>(1,ConstantSInt::get(Type::LongTy, i)), |
Chris Lattner | 5c44786 | 2002-09-10 17:03:06 +0000 | [diff] [blame] | 249 | "", here); |
| 250 | new StoreInst(Int0, GEP2, here); |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 251 | } |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 252 | |
Chris Lattner | b9d9e0f | 2002-09-11 01:21:29 +0000 | [diff] [blame] | 253 | //store uint 0, uint *%R |
| 254 | new StoreInst(Int0, rVar, here); |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | |
| 258 | //insert a basic block with appropriate code |
| 259 | //along a given edge |
| 260 | void insertBB(Edge ed, |
| 261 | getEdgeCode *edgeCode, |
| 262 | Instruction *rInst, |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 263 | Instruction *countInst, |
| 264 | int numPaths, int Methno){ |
| 265 | static int i=-1; |
| 266 | i++; |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 267 | BasicBlock* BB1=ed.getFirst()->getElement(); |
| 268 | BasicBlock* BB2=ed.getSecond()->getElement(); |
| 269 | |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 270 | #ifdef DEBUG_PATH_PROFILES |
| 271 | //debugging info |
| 272 | cerr<<"Edges with codes ######################\n"; |
| 273 | cerr<<BB1->getName()<<"->"<<BB2->getName()<<"\n"; |
| 274 | cerr<<"########################\n"; |
| 275 | #endif |
| 276 | |
| 277 | char counterstr[100]; |
| 278 | sprintf(counterstr,"counter%d",i); |
| 279 | std::string ctr=counterstr; |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 280 | |
| 281 | //We need to insert a BB between BB1 and BB2 |
| 282 | TerminatorInst *TI=BB1->getTerminator(); |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 283 | BasicBlock *newBB=new BasicBlock(ctr, BB1->getParent()); |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 284 | |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 285 | //Is terminator a branch instruction? |
| 286 | //then we need to change branch destinations to include new BB |
| 287 | |
Anand Shukla | ff72c79 | 2002-07-08 19:36:39 +0000 | [diff] [blame] | 288 | //std::cerr<<"before cast!\n"; |
Anand Shukla | 2a78633 | 2002-07-21 09:41:18 +0000 | [diff] [blame] | 289 | //std::cerr<<"Method no in Edgecode:"<<Methno<<"\n"; |
| 290 | //std::cerr<<"Instruction\n"; |
| 291 | //std::cerr<<*TI; |
Anand Shukla | ff72c79 | 2002-07-08 19:36:39 +0000 | [diff] [blame] | 292 | BranchInst *BI = cast<BranchInst>(TI); |
| 293 | |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 294 | if(BI->isUnconditional()){ |
| 295 | BI->setUnconditionalDest(newBB); |
| 296 | Instruction *newBI2=new BranchInst(BB2); |
| 297 | newBB->getInstList().push_back(newBI2); |
| 298 | } |
| 299 | else{ |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 300 | if(BI->getSuccessor(0)==BB2) |
| 301 | BI->setSuccessor(0, newBB); |
| 302 | |
| 303 | if(BI->getSuccessor(1)==BB2) |
| 304 | BI->setSuccessor(1, newBB); |
| 305 | |
| 306 | Instruction *newBI2=new BranchInst(BB2); |
| 307 | newBB->getInstList().push_back(newBI2); |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 308 | } |
Chris Lattner | 154cf64 | 2002-09-14 19:33:16 +0000 | [diff] [blame^] | 309 | |
| 310 | //get code for the new BB |
| 311 | edgeCode->getCode(rInst, countInst, BB1->getParent(), newBB, numPaths, Methno); |
| 312 | |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 313 | |
Anand Shukla | ff72c79 | 2002-07-08 19:36:39 +0000 | [diff] [blame] | 314 | //std::cerr<<"After casting\n"; |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 315 | //get code for the new BB |
| 316 | //now iterate over BB2, and set its Phi nodes right |
Chris Lattner | 7076ff2 | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 317 | for(BasicBlock::iterator BB2Inst = BB2->begin(), BBend = BB2->end(); |
| 318 | BB2Inst != BBend; ++BB2Inst){ |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 319 | |
Chris Lattner | 7076ff2 | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 320 | if(PHINode *phiInst=dyn_cast<PHINode>(&*BB2Inst)){ |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 321 | int bbIndex=phiInst->getBasicBlockIndex(BB1); |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 322 | assert(bbIndex>=0); |
| 323 | phiInst->setIncomingBlock(bbIndex, newBB); |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 324 | } |
| 325 | } |
| 326 | } |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 327 | |