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