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