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 | |
Chris Lattner | 2f04a0d | 2003-01-14 22:33:56 +0000 | [diff] [blame] | 10 | #include "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 | |
| 20 | #define INSERT_LOAD_COUNT |
| 21 | #define INSERT_STORE |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 22 | |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 23 | using std::vector; |
| 24 | |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 25 | |
Chris Lattner | 5c44786 | 2002-09-10 17:03:06 +0000 | [diff] [blame] | 26 | static void getTriggerCode(Module *M, BasicBlock *BB, int MethNo, Value *pathNo, |
Anand Shukla | f8c09ee | 2003-02-14 20:41:53 +0000 | [diff] [blame] | 27 | Value *cnt, Instruction *rInst){ |
Anand Shukla | f94ad68 | 2002-09-16 05:26:51 +0000 | [diff] [blame] | 28 | |
Anand Shukla | f8c09ee | 2003-02-14 20:41:53 +0000 | [diff] [blame] | 29 | vector<Value *> tmpVec; |
| 30 | tmpVec.push_back(Constant::getNullValue(Type::LongTy)); |
| 31 | tmpVec.push_back(Constant::getNullValue(Type::LongTy)); |
| 32 | Instruction *Idx = new GetElementPtrInst(cnt, tmpVec, "");//, |
| 33 | BB->getInstList().push_back(Idx); |
| 34 | |
Chris Lattner | ea27751 | 2003-08-31 00:21:05 +0000 | [diff] [blame] | 35 | const Type *PIntTy = PointerType::get(Type::IntTy); |
| 36 | Function *trigMeth = M->getOrInsertFunction("trigger", Type::VoidTy, |
| 37 | Type::IntTy, Type::IntTy, |
| 38 | PIntTy, PIntTy, 0); |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 39 | assert(trigMeth && "trigger method could not be inserted!"); |
Anand Shukla | f94ad68 | 2002-09-16 05:26:51 +0000 | [diff] [blame] | 40 | |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 41 | vector<Value *> trargs; |
| 42 | |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 43 | trargs.push_back(ConstantSInt::get(Type::IntTy,MethNo)); |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 44 | trargs.push_back(pathNo); |
Anand Shukla | f8c09ee | 2003-02-14 20:41:53 +0000 | [diff] [blame] | 45 | trargs.push_back(Idx); |
| 46 | trargs.push_back(rInst); |
Anand Shukla | f94ad68 | 2002-09-16 05:26:51 +0000 | [diff] [blame] | 47 | |
Anand Shukla | 77dca14 | 2002-09-20 16:44:35 +0000 | [diff] [blame] | 48 | Instruction *callInst=new CallInst(trigMeth, trargs, "");//, BB->begin()); |
| 49 | BB->getInstList().push_back(callInst); |
| 50 | //triggerInst = new CallInst(trigMeth, trargs, "");//, InsertPos); |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 54 | //get the code to be inserted on the edge |
| 55 | //This is determined from cond (1-6) |
Anand Shukla | f8c09ee | 2003-02-14 20:41:53 +0000 | [diff] [blame] | 56 | void getEdgeCode::getCode(Instruction *rInst, Value *countInst, |
Anand Shukla | 77dca14 | 2002-09-20 16:44:35 +0000 | [diff] [blame] | 57 | Function *M, BasicBlock *BB, |
| 58 | vector<Value *> &retVec){ |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 59 | |
Anand Shukla | 77dca14 | 2002-09-20 16:44:35 +0000 | [diff] [blame] | 60 | //Instruction *InsertPos = BB->getInstList().begin(); |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 61 | |
Anand Shukla | 77dca14 | 2002-09-20 16:44:35 +0000 | [diff] [blame] | 62 | //now check for cdIn and cdOut |
| 63 | //first put cdOut |
| 64 | if(cdOut!=NULL){ |
| 65 | cdOut->getCode(rInst, countInst, M, BB, retVec); |
| 66 | } |
| 67 | |
| 68 | if(cdIn!=NULL){ |
| 69 | cdIn->getCode(rInst, countInst, M, BB, retVec); |
| 70 | } |
| 71 | |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 72 | //case: r=k code to be inserted |
| 73 | switch(cond){ |
| 74 | case 1:{ |
| 75 | Value *val=ConstantSInt::get(Type::IntTy,inc); |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 76 | #ifdef INSERT_STORE |
Anand Shukla | 77dca14 | 2002-09-20 16:44:35 +0000 | [diff] [blame] | 77 | Instruction *stInst=new StoreInst(val, rInst);//, InsertPos); |
| 78 | BB->getInstList().push_back(stInst); |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 79 | #endif |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 80 | break; |
| 81 | } |
| 82 | |
| 83 | //case: r=0 to be inserted |
Anand Shukla | 77dca14 | 2002-09-20 16:44:35 +0000 | [diff] [blame] | 84 | case 2:{ |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 85 | #ifdef INSERT_STORE |
Anand Shukla | 77dca14 | 2002-09-20 16:44:35 +0000 | [diff] [blame] | 86 | Instruction *stInst = new StoreInst(ConstantSInt::getNullValue(Type::IntTy), rInst);//, InsertPos); |
| 87 | BB->getInstList().push_back(stInst); |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 88 | #endif |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 89 | break; |
Anand Shukla | 77dca14 | 2002-09-20 16:44:35 +0000 | [diff] [blame] | 90 | } |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 91 | |
| 92 | //r+=k |
| 93 | case 3:{ |
Anand Shukla | 77dca14 | 2002-09-20 16:44:35 +0000 | [diff] [blame] | 94 | Instruction *ldInst = new LoadInst(rInst, "ti1");//, InsertPos); |
| 95 | BB->getInstList().push_back(ldInst); |
Chris Lattner | 5c44786 | 2002-09-10 17:03:06 +0000 | [diff] [blame] | 96 | Value *val = ConstantSInt::get(Type::IntTy,inc); |
Anand Shukla | 77dca14 | 2002-09-20 16:44:35 +0000 | [diff] [blame] | 97 | Instruction *addIn = BinaryOperator::create(Instruction::Add, ldInst, val, |
| 98 | "ti2");//, InsertPos); |
| 99 | BB->getInstList().push_back(addIn); |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 100 | #ifdef INSERT_STORE |
Anand Shukla | 77dca14 | 2002-09-20 16:44:35 +0000 | [diff] [blame] | 101 | Instruction *stInst = new StoreInst(addIn, rInst);//, InsertPos); |
| 102 | BB->getInstList().push_back(stInst); |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 103 | #endif |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 104 | break; |
| 105 | } |
| 106 | |
| 107 | //count[inc]++ |
| 108 | case 4:{ |
Anand Shukla | f8c09ee | 2003-02-14 20:41:53 +0000 | [diff] [blame] | 109 | vector<Value *> tmpVec; |
| 110 | tmpVec.push_back(Constant::getNullValue(Type::LongTy)); |
| 111 | tmpVec.push_back(ConstantSInt::get(Type::LongTy, inc)); |
| 112 | Instruction *Idx = new GetElementPtrInst(countInst, tmpVec, "");//, |
| 113 | |
| 114 | //Instruction *Idx = new GetElementPtrInst(countInst, |
| 115 | // vector<Value*>(1,ConstantSInt::get(Type::LongTy, inc)), |
| 116 | // "");//, InsertPos); |
Anand Shukla | 77dca14 | 2002-09-20 16:44:35 +0000 | [diff] [blame] | 117 | BB->getInstList().push_back(Idx); |
Chris Lattner | e49f299 | 2002-08-21 22:11:33 +0000 | [diff] [blame] | 118 | |
Anand Shukla | 77dca14 | 2002-09-20 16:44:35 +0000 | [diff] [blame] | 119 | Instruction *ldInst=new LoadInst(Idx, "ti1");//, InsertPos); |
| 120 | BB->getInstList().push_back(ldInst); |
Chris Lattner | e49f299 | 2002-08-21 22:11:33 +0000 | [diff] [blame] | 121 | |
| 122 | Value *val = ConstantSInt::get(Type::IntTy, 1); |
Anand Shukla | 77dca14 | 2002-09-20 16:44:35 +0000 | [diff] [blame] | 123 | //Instruction *addIn = |
| 124 | Instruction *newCount = |
| 125 | BinaryOperator::create(Instruction::Add, ldInst, val,"ti2"); |
| 126 | BB->getInstList().push_back(newCount); |
| 127 | |
Chris Lattner | 5c44786 | 2002-09-10 17:03:06 +0000 | [diff] [blame] | 128 | |
| 129 | #ifdef INSERT_STORE |
Anand Shukla | 77dca14 | 2002-09-20 16:44:35 +0000 | [diff] [blame] | 130 | //Instruction *stInst=new StoreInst(addIn, Idx, InsertPos); |
| 131 | Instruction *stInst=new StoreInst(newCount, Idx);//, InsertPos); |
| 132 | BB->getInstList().push_back(stInst); |
Chris Lattner | 5c44786 | 2002-09-10 17:03:06 +0000 | [diff] [blame] | 133 | #endif |
Anand Shukla | 77dca14 | 2002-09-20 16:44:35 +0000 | [diff] [blame] | 134 | |
| 135 | Value *trAddIndex = ConstantSInt::get(Type::IntTy,inc); |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 136 | |
Anand Shukla | 77dca14 | 2002-09-20 16:44:35 +0000 | [diff] [blame] | 137 | retVec.push_back(newCount); |
| 138 | retVec.push_back(trAddIndex); |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 139 | //insert trigger |
Anand Shukla | 77dca14 | 2002-09-20 16:44:35 +0000 | [diff] [blame] | 140 | //getTriggerCode(M->getParent(), BB, MethNo, |
| 141 | // ConstantSInt::get(Type::IntTy,inc), newCount, triggerInst); |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 142 | //end trigger code |
| 143 | |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 144 | assert(inc>=0 && "IT MUST BE POSITIVE NOW"); |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 145 | break; |
| 146 | } |
| 147 | |
| 148 | //case: count[r+inc]++ |
| 149 | case 5:{ |
Chris Lattner | 5c44786 | 2002-09-10 17:03:06 +0000 | [diff] [blame] | 150 | |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 151 | //ti1=inc+r |
Anand Shukla | 77dca14 | 2002-09-20 16:44:35 +0000 | [diff] [blame] | 152 | Instruction *ldIndex=new LoadInst(rInst, "ti1");//, InsertPos); |
| 153 | BB->getInstList().push_back(ldIndex); |
| 154 | |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 155 | Value *val=ConstantSInt::get(Type::IntTy,inc); |
| 156 | Instruction *addIndex=BinaryOperator:: |
Anand Shukla | 77dca14 | 2002-09-20 16:44:35 +0000 | [diff] [blame] | 157 | create(Instruction::Add, ldIndex, val,"ti2");//, InsertPos); |
| 158 | BB->getInstList().push_back(addIndex); |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 159 | |
Anand Shukla | f94ad68 | 2002-09-16 05:26:51 +0000 | [diff] [blame] | 160 | //now load count[addIndex] |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 161 | Instruction *castInst=new CastInst(addIndex, |
Anand Shukla | 77dca14 | 2002-09-20 16:44:35 +0000 | [diff] [blame] | 162 | Type::LongTy,"ctin");//, InsertPos); |
| 163 | BB->getInstList().push_back(castInst); |
Chris Lattner | e49f299 | 2002-08-21 22:11:33 +0000 | [diff] [blame] | 164 | |
Anand Shukla | f8c09ee | 2003-02-14 20:41:53 +0000 | [diff] [blame] | 165 | vector<Value *> tmpVec; |
| 166 | tmpVec.push_back(Constant::getNullValue(Type::LongTy)); |
| 167 | tmpVec.push_back(castInst); |
| 168 | Instruction *Idx = new GetElementPtrInst(countInst, tmpVec, "");//, |
Anand Shukla | 77dca14 | 2002-09-20 16:44:35 +0000 | [diff] [blame] | 169 | // InsertPos); |
| 170 | BB->getInstList().push_back(Idx); |
| 171 | |
| 172 | Instruction *ldInst=new LoadInst(Idx, "ti3");//, InsertPos); |
| 173 | BB->getInstList().push_back(ldInst); |
| 174 | |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 175 | Value *cons=ConstantSInt::get(Type::IntTy,1); |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 176 | //count[addIndex]++ |
Anand Shukla | f8c09ee | 2003-02-14 20:41:53 +0000 | [diff] [blame] | 177 | //std::cerr<<"Type ldInst:"<<ldInst->getType()<<"\t cons:"<<cons->getType()<<"\n"; |
Anand Shukla | 77dca14 | 2002-09-20 16:44:35 +0000 | [diff] [blame] | 178 | Instruction *newCount = BinaryOperator::create(Instruction::Add, ldInst, |
| 179 | cons,""); |
| 180 | BB->getInstList().push_back(newCount); |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 181 | |
| 182 | #ifdef INSERT_STORE |
Anand Shukla | 77dca14 | 2002-09-20 16:44:35 +0000 | [diff] [blame] | 183 | Instruction *stInst = new StoreInst(newCount, Idx);//, InsertPos); |
| 184 | BB->getInstList().push_back(stInst); |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 185 | #endif |
Chris Lattner | 5c44786 | 2002-09-10 17:03:06 +0000 | [diff] [blame] | 186 | |
Anand Shukla | 77dca14 | 2002-09-20 16:44:35 +0000 | [diff] [blame] | 187 | retVec.push_back(newCount); |
| 188 | retVec.push_back(addIndex); |
Chris Lattner | 5c44786 | 2002-09-10 17:03:06 +0000 | [diff] [blame] | 189 | //insert trigger |
Anand Shukla | 77dca14 | 2002-09-20 16:44:35 +0000 | [diff] [blame] | 190 | //getTriggerCode(M->getParent(), BB, MethNo, addIndex, newCount, triggerInst); |
Chris Lattner | 5c44786 | 2002-09-10 17:03:06 +0000 | [diff] [blame] | 191 | //end trigger code |
| 192 | |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 193 | break; |
| 194 | } |
| 195 | |
| 196 | //case: count[r]+ |
| 197 | case 6:{ |
| 198 | //ti1=inc+r |
Anand Shukla | 77dca14 | 2002-09-20 16:44:35 +0000 | [diff] [blame] | 199 | Instruction *ldIndex=new LoadInst(rInst, "ti1");//, InsertPos); |
| 200 | BB->getInstList().push_back(ldIndex); |
| 201 | |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 202 | //now load count[addIndex] |
Anand Shukla | 77dca14 | 2002-09-20 16:44:35 +0000 | [diff] [blame] | 203 | Instruction *castInst2=new CastInst(ldIndex, Type::LongTy,"ctin"); |
| 204 | BB->getInstList().push_back(castInst2); |
| 205 | |
Anand Shukla | f8c09ee | 2003-02-14 20:41:53 +0000 | [diff] [blame] | 206 | vector<Value *> tmpVec; |
| 207 | tmpVec.push_back(Constant::getNullValue(Type::LongTy)); |
| 208 | tmpVec.push_back(castInst2); |
| 209 | Instruction *Idx = new GetElementPtrInst(countInst, tmpVec, "");//, |
| 210 | |
| 211 | //Instruction *Idx = new GetElementPtrInst(countInst, |
| 212 | // vector<Value*>(1,castInst2), ""); |
| 213 | |
Anand Shukla | 77dca14 | 2002-09-20 16:44:35 +0000 | [diff] [blame] | 214 | BB->getInstList().push_back(Idx); |
Chris Lattner | e49f299 | 2002-08-21 22:11:33 +0000 | [diff] [blame] | 215 | |
Anand Shukla | 77dca14 | 2002-09-20 16:44:35 +0000 | [diff] [blame] | 216 | Instruction *ldInst=new LoadInst(Idx, "ti2");//, InsertPos); |
| 217 | BB->getInstList().push_back(ldInst); |
| 218 | |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 219 | Value *cons=ConstantSInt::get(Type::IntTy,1); |
| 220 | |
| 221 | //count[addIndex]++ |
Anand Shukla | 77dca14 | 2002-09-20 16:44:35 +0000 | [diff] [blame] | 222 | Instruction *newCount = BinaryOperator::create(Instruction::Add, ldInst, |
| 223 | cons,"ti3"); |
| 224 | BB->getInstList().push_back(newCount); |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 225 | |
Chris Lattner | 5c44786 | 2002-09-10 17:03:06 +0000 | [diff] [blame] | 226 | #ifdef INSERT_STORE |
Anand Shukla | 77dca14 | 2002-09-20 16:44:35 +0000 | [diff] [blame] | 227 | Instruction *stInst = new StoreInst(newCount, Idx);//, InsertPos); |
| 228 | BB->getInstList().push_back(stInst); |
Chris Lattner | 5c44786 | 2002-09-10 17:03:06 +0000 | [diff] [blame] | 229 | #endif |
Anand Shukla | 77dca14 | 2002-09-20 16:44:35 +0000 | [diff] [blame] | 230 | |
| 231 | retVec.push_back(newCount); |
| 232 | retVec.push_back(ldIndex); |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 233 | break; |
| 234 | } |
| 235 | |
| 236 | } |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | |
| 240 | |
| 241 | //Insert the initialization code in the top BB |
| 242 | //this includes initializing r, and count |
| 243 | //r is like an accumulator, that |
| 244 | //keeps on adding increments as we traverse along a path |
| 245 | //and at the end of the path, r contains the path |
| 246 | //number of that path |
| 247 | //Count is an array, where Count[k] represents |
| 248 | //the number of executions of path k |
| 249 | void insertInTopBB(BasicBlock *front, |
| 250 | int k, |
Anand Shukla | f8c09ee | 2003-02-14 20:41:53 +0000 | [diff] [blame] | 251 | Instruction *rVar, Value *threshold){ |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 252 | //rVar is variable r, |
Anand Shukla | f8c09ee | 2003-02-14 20:41:53 +0000 | [diff] [blame] | 253 | //countVar is count[] |
Chris Lattner | e49f299 | 2002-08-21 22:11:33 +0000 | [diff] [blame] | 254 | |
| 255 | Value *Int0 = ConstantInt::get(Type::IntTy, 0); |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 256 | |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 257 | //now push all instructions in front of the BB |
Chris Lattner | 5c44786 | 2002-09-10 17:03:06 +0000 | [diff] [blame] | 258 | BasicBlock::iterator here=front->begin(); |
| 259 | front->getInstList().insert(here, rVar); |
Anand Shukla | f8c09ee | 2003-02-14 20:41:53 +0000 | [diff] [blame] | 260 | //front->getInstList().insert(here,countVar); |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 261 | |
| 262 | //Initialize Count[...] with 0 |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 263 | |
Anand Shukla | 77dca14 | 2002-09-20 16:44:35 +0000 | [diff] [blame] | 264 | //for (int i=0;i<k; i++){ |
| 265 | //Value *GEP2 = new GetElementPtrInst(countVar, |
| 266 | // vector<Value *>(1,ConstantSInt::get(Type::LongTy, i)), |
| 267 | // "", here); |
| 268 | //new StoreInst(Int0, GEP2, here); |
| 269 | //} |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 270 | |
Chris Lattner | b9d9e0f | 2002-09-11 01:21:29 +0000 | [diff] [blame] | 271 | //store uint 0, uint *%R |
| 272 | new StoreInst(Int0, rVar, here); |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | |
| 276 | //insert a basic block with appropriate code |
| 277 | //along a given edge |
| 278 | void insertBB(Edge ed, |
| 279 | getEdgeCode *edgeCode, |
| 280 | Instruction *rInst, |
Anand Shukla | f8c09ee | 2003-02-14 20:41:53 +0000 | [diff] [blame] | 281 | Value *countInst, |
Anand Shukla | 77dca14 | 2002-09-20 16:44:35 +0000 | [diff] [blame] | 282 | int numPaths, int Methno, Value *threshold){ |
Anand Shukla | f94ad68 | 2002-09-16 05:26:51 +0000 | [diff] [blame] | 283 | |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 284 | BasicBlock* BB1=ed.getFirst()->getElement(); |
| 285 | BasicBlock* BB2=ed.getSecond()->getElement(); |
| 286 | |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 287 | #ifdef DEBUG_PATH_PROFILES |
| 288 | //debugging info |
| 289 | cerr<<"Edges with codes ######################\n"; |
| 290 | cerr<<BB1->getName()<<"->"<<BB2->getName()<<"\n"; |
| 291 | cerr<<"########################\n"; |
| 292 | #endif |
| 293 | |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 294 | //We need to insert a BB between BB1 and BB2 |
| 295 | TerminatorInst *TI=BB1->getTerminator(); |
Anand Shukla | f94ad68 | 2002-09-16 05:26:51 +0000 | [diff] [blame] | 296 | BasicBlock *newBB=new BasicBlock("counter", BB1->getParent()); |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 297 | |
Anand Shukla | 77dca14 | 2002-09-20 16:44:35 +0000 | [diff] [blame] | 298 | //get code for the new BB |
| 299 | vector<Value *> retVec; |
| 300 | |
| 301 | edgeCode->getCode(rInst, countInst, BB1->getParent(), newBB, retVec); |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 302 | |
Anand Shukla | ff72c79 | 2002-07-08 19:36:39 +0000 | [diff] [blame] | 303 | BranchInst *BI = cast<BranchInst>(TI); |
| 304 | |
Anand Shukla | 77dca14 | 2002-09-20 16:44:35 +0000 | [diff] [blame] | 305 | //Is terminator a branch instruction? |
| 306 | //then we need to change branch destinations to include new BB |
| 307 | |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 308 | if(BI->isUnconditional()){ |
| 309 | BI->setUnconditionalDest(newBB); |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 310 | } |
| 311 | else{ |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 312 | if(BI->getSuccessor(0)==BB2) |
| 313 | BI->setSuccessor(0, newBB); |
| 314 | |
| 315 | if(BI->getSuccessor(1)==BB2) |
| 316 | BI->setSuccessor(1, newBB); |
Anand Shukla | 77dca14 | 2002-09-20 16:44:35 +0000 | [diff] [blame] | 317 | } |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 318 | |
Anand Shukla | 77dca14 | 2002-09-20 16:44:35 +0000 | [diff] [blame] | 319 | BasicBlock *triggerBB = NULL; |
| 320 | if(retVec.size()>0){ |
| 321 | triggerBB = new BasicBlock("trigger", BB1->getParent()); |
| 322 | getTriggerCode(BB1->getParent()->getParent(), triggerBB, Methno, |
Anand Shukla | f8c09ee | 2003-02-14 20:41:53 +0000 | [diff] [blame] | 323 | retVec[1], countInst, rInst);//retVec[0]); |
Anand Shukla | 77dca14 | 2002-09-20 16:44:35 +0000 | [diff] [blame] | 324 | |
| 325 | //Instruction *castInst = new CastInst(retVec[0], Type::IntTy, ""); |
| 326 | Instruction *etr = new LoadInst(threshold, "threshold"); |
| 327 | |
| 328 | //std::cerr<<"type1: "<<etr->getType()<<" type2: "<<retVec[0]->getType()<<"\n"; |
| 329 | Instruction *cmpInst = new SetCondInst(Instruction::SetLE, etr, |
| 330 | retVec[0], ""); |
| 331 | Instruction *newBI2 = new BranchInst(triggerBB, BB2, cmpInst); |
| 332 | //newBB->getInstList().push_back(castInst); |
| 333 | newBB->getInstList().push_back(etr); |
| 334 | newBB->getInstList().push_back(cmpInst); |
| 335 | newBB->getInstList().push_back(newBI2); |
| 336 | |
| 337 | //triggerBB->getInstList().push_back(triggerInst); |
| 338 | Instruction *triggerBranch = new BranchInst(BB2); |
| 339 | triggerBB->getInstList().push_back(triggerBranch); |
| 340 | } |
| 341 | else{ |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 342 | Instruction *newBI2=new BranchInst(BB2); |
| 343 | newBB->getInstList().push_back(newBI2); |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 344 | } |
Chris Lattner | 154cf64 | 2002-09-14 19:33:16 +0000 | [diff] [blame] | 345 | |
Anand Shukla | f94ad68 | 2002-09-16 05:26:51 +0000 | [diff] [blame] | 346 | //now iterate over BB2, and set its Phi nodes right |
Chris Lattner | 7076ff2 | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 347 | for(BasicBlock::iterator BB2Inst = BB2->begin(), BBend = BB2->end(); |
| 348 | BB2Inst != BBend; ++BB2Inst){ |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 349 | |
Chris Lattner | 889f620 | 2003-04-23 16:37:45 +0000 | [diff] [blame] | 350 | if(PHINode *phiInst=dyn_cast<PHINode>(BB2Inst)){ |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 351 | int bbIndex=phiInst->getBasicBlockIndex(BB1); |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 352 | assert(bbIndex>=0); |
| 353 | phiInst->setIncomingBlock(bbIndex, newBB); |
Anand Shukla | 77dca14 | 2002-09-20 16:44:35 +0000 | [diff] [blame] | 354 | |
| 355 | ///check if trigger!=null, then add value corresponding to it too! |
| 356 | if(retVec.size()>0){ |
| 357 | assert(triggerBB && "BasicBlock with trigger should not be null!"); |
| 358 | Value *vl = phiInst->getIncomingValue((unsigned int)bbIndex); |
| 359 | phiInst->addIncoming(vl, triggerBB); |
| 360 | } |
Anand Shukla | d0f8c88 | 2002-02-26 18:59:46 +0000 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | } |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 364 | |