Chris Lattner | 44d2c35 | 2003-10-13 03:32:08 +0000 | [diff] [blame] | 1 | //===- RetracePath.cpp ----------------------------------------------------===// |
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 | ea77a49 | 2002-09-18 03:55:26 +0000 | [diff] [blame] | 9 | // |
| 10 | // Retraces a path of BasicBlock, given a path number and a graph! |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Anand Shukla | ea77a49 | 2002-09-18 03:55:26 +0000 | [diff] [blame] | 14 | #include "llvm/Module.h" |
Anand Shukla | ea77a49 | 2002-09-18 03:55:26 +0000 | [diff] [blame] | 15 | #include "llvm/iTerminators.h" |
Anand Shukla | ea77a49 | 2002-09-18 03:55:26 +0000 | [diff] [blame] | 16 | #include "llvm/iOther.h" |
Chris Lattner | 2f04a0d | 2003-01-14 22:33:56 +0000 | [diff] [blame] | 17 | #include "llvm/Support/CFG.h" |
| 18 | #include "Graph.h" |
Reid Spencer | eb04d9b | 2004-07-04 12:19:56 +0000 | [diff] [blame^] | 19 | #include <iostream> |
Anand Shukla | ea77a49 | 2002-09-18 03:55:26 +0000 | [diff] [blame] | 20 | |
| 21 | using std::vector; |
| 22 | using std::map; |
| 23 | using std::cerr; |
| 24 | |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 25 | namespace llvm { |
| 26 | |
Anand Shukla | ea77a49 | 2002-09-18 03:55:26 +0000 | [diff] [blame] | 27 | //Routines to get the path trace! |
| 28 | |
Anand Shukla | f8c09ee | 2003-02-14 20:41:53 +0000 | [diff] [blame] | 29 | void getPathFrmNode(Node *n, vector<BasicBlock*> &vBB, int pathNo, Graph &g, |
Anand Shukla | ea77a49 | 2002-09-18 03:55:26 +0000 | [diff] [blame] | 30 | vector<Edge> &stDummy, vector<Edge> &exDummy, |
| 31 | vector<Edge> &be, |
| 32 | double strand){ |
Anand Shukla | f8c09ee | 2003-02-14 20:41:53 +0000 | [diff] [blame] | 33 | Graph::nodeList &nlist = g.getNodeList(n); |
Anand Shukla | ea77a49 | 2002-09-18 03:55:26 +0000 | [diff] [blame] | 34 | |
Anand Shukla | f8c09ee | 2003-02-14 20:41:53 +0000 | [diff] [blame] | 35 | //printGraph(g); |
| 36 | //std::cerr<<"Path No: "<<pathNo<<"\n"; |
Anand Shukla | ea77a49 | 2002-09-18 03:55:26 +0000 | [diff] [blame] | 37 | int maxCount=-9999999; |
| 38 | bool isStart=false; |
| 39 | |
| 40 | if(*n==*g.getRoot())//its root: so first node of path |
| 41 | isStart=true; |
| 42 | |
| 43 | double edgeRnd=0; |
| 44 | Node *nextRoot=n; |
Anand Shukla | f8c09ee | 2003-02-14 20:41:53 +0000 | [diff] [blame] | 45 | for(Graph::nodeList::iterator NLI = nlist.begin(), NLE=nlist.end(); NLI!=NLE; |
Anand Shukla | ea77a49 | 2002-09-18 03:55:26 +0000 | [diff] [blame] | 46 | ++NLI){ |
| 47 | if(NLI->weight>maxCount && NLI->weight<=pathNo){ |
| 48 | maxCount=NLI->weight; |
| 49 | nextRoot=NLI->element; |
| 50 | edgeRnd=NLI->randId; |
| 51 | if(isStart) |
| 52 | strand=NLI->randId; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | if(!isStart) |
| 57 | assert(strand!=-1 && "strand not assigned!"); |
| 58 | |
| 59 | assert(!(*nextRoot==*n && pathNo>0) && "No more BBs to go"); |
| 60 | assert(!(*nextRoot==*g.getExit() && pathNo-maxCount!=0) && "Reached exit"); |
| 61 | |
| 62 | vBB.push_back(n->getElement()); |
| 63 | |
| 64 | if(pathNo-maxCount==0 && *nextRoot==*g.getExit()){ |
| 65 | |
| 66 | //look for strnd and edgeRnd now: |
| 67 | bool has1=false, has2=false; |
| 68 | //check if exit has it |
| 69 | for(vector<Edge>::iterator VI=exDummy.begin(), VE=exDummy.end(); VI!=VE; |
| 70 | ++VI){ |
| 71 | if(VI->getRandId()==edgeRnd){ |
| 72 | has2=true; |
| 73 | break; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | //check if start has it |
| 78 | for(vector<Edge>::iterator VI=stDummy.begin(), VE=stDummy.end(); VI!=VE; |
| 79 | ++VI){ |
| 80 | if(VI->getRandId()==strand){ |
| 81 | has1=true; |
| 82 | break; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | if(has1){ |
| 87 | //find backedge with endpoint vBB[1] |
| 88 | for(vector<Edge>::iterator VI=be.begin(), VE=be.end(); VI!=VE; ++VI){ |
| 89 | assert(vBB.size()>0 && "vector too small"); |
| 90 | if( VI->getSecond()->getElement() == vBB[1] ){ |
| 91 | //vBB[0]=VI->getFirst()->getElement(); |
| 92 | vBB.erase(vBB.begin()); |
| 93 | break; |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | if(has2){ |
| 99 | //find backedge with startpoint vBB[vBB.size()-1] |
| 100 | for(vector<Edge>::iterator VI=be.begin(), VE=be.end(); VI!=VE; ++VI){ |
| 101 | assert(vBB.size()>0 && "vector too small"); |
| 102 | if( VI->getFirst()->getElement() == vBB[vBB.size()-1] && |
| 103 | VI->getSecond()->getElement() == vBB[0]){ |
| 104 | //vBB.push_back(VI->getSecond()->getElement()); |
| 105 | break; |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | else |
| 110 | vBB.push_back(nextRoot->getElement()); |
| 111 | |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | assert(pathNo-maxCount>=0); |
| 116 | |
| 117 | return getPathFrmNode(nextRoot, vBB, pathNo-maxCount, g, stDummy, |
| 118 | exDummy, be, strand); |
| 119 | } |
| 120 | |
| 121 | |
| 122 | static Node *findBB(std::vector<Node *> &st, BasicBlock *BB){ |
| 123 | for(std::vector<Node *>::iterator si=st.begin(); si!=st.end(); ++si){ |
| 124 | if(((*si)->getElement())==BB){ |
| 125 | return *si; |
| 126 | } |
| 127 | } |
| 128 | return NULL; |
| 129 | } |
| 130 | |
Anand Shukla | f8c09ee | 2003-02-14 20:41:53 +0000 | [diff] [blame] | 131 | void getBBtrace(vector<BasicBlock *> &vBB, int pathNo, Function *M){//, |
| 132 | // vector<Instruction *> &instToErase){ |
Anand Shukla | ea77a49 | 2002-09-18 03:55:26 +0000 | [diff] [blame] | 133 | //step 1: create graph |
| 134 | //Transform the cfg s.t. we have just one exit node |
| 135 | |
| 136 | std::vector<Node *> nodes; |
| 137 | std::vector<Edge> edges; |
| 138 | Node *tmp; |
| 139 | Node *exitNode=0, *startNode=0; |
Anand Shukla | f8c09ee | 2003-02-14 20:41:53 +0000 | [diff] [blame] | 140 | |
| 141 | //Creat cfg just once for each function! |
| 142 | static std::map<Function *, Graph *> graphMap; |
| 143 | |
| 144 | //get backedges, exit and start edges for the graphs and store them |
Anand Shukla | ea77a49 | 2002-09-18 03:55:26 +0000 | [diff] [blame] | 145 | static std::map<Function *, vector<Edge> > stMap, exMap, beMap; |
Anand Shukla | f8c09ee | 2003-02-14 20:41:53 +0000 | [diff] [blame] | 146 | static std::map<Function *, Value *> pathReg; //path register |
| 147 | |
Anand Shukla | ea77a49 | 2002-09-18 03:55:26 +0000 | [diff] [blame] | 148 | |
| 149 | if(!graphMap[M]){ |
| 150 | BasicBlock *ExitNode = 0; |
| 151 | for (Function::iterator I = M->begin(), E = M->end(); I != E; ++I){ |
| 152 | if (isa<ReturnInst>(I->getTerminator())) { |
Chris Lattner | 889f620 | 2003-04-23 16:37:45 +0000 | [diff] [blame] | 153 | ExitNode = I; |
Anand Shukla | ea77a49 | 2002-09-18 03:55:26 +0000 | [diff] [blame] | 154 | break; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | assert(ExitNode!=0 && "exitnode not found"); |
| 159 | |
| 160 | //iterating over BBs and making graph |
| 161 | //The nodes must be uniquely identified: |
| 162 | //That is, no two nodes must hav same BB* |
| 163 | |
Anand Shukla | f8c09ee | 2003-02-14 20:41:53 +0000 | [diff] [blame] | 164 | //keep a map for trigger basicblocks! |
| 165 | std::map<BasicBlock *, unsigned char> triggerBBs; |
Anand Shukla | ea77a49 | 2002-09-18 03:55:26 +0000 | [diff] [blame] | 166 | //First enter just nodes: later enter edges |
| 167 | for(Function::iterator BB = M->begin(), BE=M->end(); BB != BE; ++BB){ |
Anand Shukla | f8c09ee | 2003-02-14 20:41:53 +0000 | [diff] [blame] | 168 | bool cont = false; |
| 169 | |
| 170 | if(BB->size()==3 || BB->size() ==2){ |
| 171 | for(BasicBlock::iterator II = BB->begin(), IE = BB->end(); |
| 172 | II != IE; ++II){ |
Chris Lattner | 889f620 | 2003-04-23 16:37:45 +0000 | [diff] [blame] | 173 | if(CallInst *callInst = dyn_cast<CallInst>(II)){ |
Anand Shukla | f8c09ee | 2003-02-14 20:41:53 +0000 | [diff] [blame] | 174 | //std::cerr<<*callInst; |
| 175 | Function *calledFunction = callInst->getCalledFunction(); |
| 176 | if(calledFunction && calledFunction->getName() == "trigger"){ |
| 177 | triggerBBs[BB] = 9; |
| 178 | cont = true; |
| 179 | //std::cerr<<"Found trigger!\n"; |
| 180 | break; |
| 181 | } |
| 182 | } |
Anand Shukla | ea77a49 | 2002-09-18 03:55:26 +0000 | [diff] [blame] | 183 | } |
| 184 | } |
Anand Shukla | f8c09ee | 2003-02-14 20:41:53 +0000 | [diff] [blame] | 185 | |
| 186 | if(cont) |
| 187 | continue; |
| 188 | |
| 189 | // const Instruction *inst = BB->getInstList().begin(); |
| 190 | // if(isa<CallInst>(inst)){ |
| 191 | // Instruction *ii1 = BB->getInstList().begin(); |
| 192 | // CallInst *callInst = dyn_cast<CallInst>(ii1); |
| 193 | // if(callInst->getCalledFunction()->getName()=="trigger") |
| 194 | // continue; |
| 195 | // } |
| 196 | |
Anand Shukla | ea77a49 | 2002-09-18 03:55:26 +0000 | [diff] [blame] | 197 | Node *nd=new Node(BB); |
| 198 | nodes.push_back(nd); |
| 199 | if(&*BB==ExitNode) |
| 200 | exitNode=nd; |
| 201 | if(&*BB==&M->front()) |
| 202 | startNode=nd; |
| 203 | } |
| 204 | |
| 205 | assert(exitNode!=0 && startNode!=0 && "Start or exit not found!"); |
| 206 | |
| 207 | for (Function::iterator BB = M->begin(), BE=M->end(); BB != BE; ++BB){ |
Anand Shukla | f8c09ee | 2003-02-14 20:41:53 +0000 | [diff] [blame] | 208 | if(triggerBBs[BB] == 9) |
| 209 | continue; |
| 210 | |
| 211 | //if(BB->size()==3) |
Chris Lattner | 889f620 | 2003-04-23 16:37:45 +0000 | [diff] [blame] | 212 | //if(CallInst *callInst = dyn_cast<CallInst>(BB->getInstList().begin())) |
Anand Shukla | f8c09ee | 2003-02-14 20:41:53 +0000 | [diff] [blame] | 213 | //if(callInst->getCalledFunction()->getName() == "trigger") |
| 214 | //continue; |
| 215 | |
| 216 | // if(BB->size()==2){ |
| 217 | // const Instruction *inst = BB->getInstList().begin(); |
| 218 | // if(isa<CallInst>(inst)){ |
| 219 | // Instruction *ii1 = BB->getInstList().begin(); |
| 220 | // CallInst *callInst = dyn_cast<CallInst>(ii1); |
| 221 | // if(callInst->getCalledFunction()->getName()=="trigger") |
| 222 | // continue; |
| 223 | // } |
| 224 | // } |
| 225 | |
Anand Shukla | ea77a49 | 2002-09-18 03:55:26 +0000 | [diff] [blame] | 226 | Node *nd=findBB(nodes, BB); |
| 227 | assert(nd && "No node for this edge!"); |
Anand Shukla | f8c09ee | 2003-02-14 20:41:53 +0000 | [diff] [blame] | 228 | |
Chris Lattner | a940095 | 2003-09-24 22:06:25 +0000 | [diff] [blame] | 229 | for(succ_iterator s=succ_begin(BB), se=succ_end(BB); s!=se; ++s){ |
Anand Shukla | f8c09ee | 2003-02-14 20:41:53 +0000 | [diff] [blame] | 230 | |
| 231 | if(triggerBBs[*s] == 9){ |
| 232 | //if(!pathReg[M]){ //Get the path register for this! |
| 233 | //if(BB->size()>8) |
Chris Lattner | 889f620 | 2003-04-23 16:37:45 +0000 | [diff] [blame] | 234 | // if(LoadInst *ldInst = dyn_cast<LoadInst>(BB->getInstList().begin())) |
Anand Shukla | f8c09ee | 2003-02-14 20:41:53 +0000 | [diff] [blame] | 235 | // pathReg[M] = ldInst->getPointerOperand(); |
| 236 | //} |
| 237 | continue; |
Anand Shukla | ea77a49 | 2002-09-18 03:55:26 +0000 | [diff] [blame] | 238 | } |
Anand Shukla | f8c09ee | 2003-02-14 20:41:53 +0000 | [diff] [blame] | 239 | //if((*s)->size()==3) |
| 240 | //if(CallInst *callInst = |
Chris Lattner | 889f620 | 2003-04-23 16:37:45 +0000 | [diff] [blame] | 241 | // dyn_cast<CallInst>((*s)->getInstList().begin())) |
Anand Shukla | f8c09ee | 2003-02-14 20:41:53 +0000 | [diff] [blame] | 242 | // if(callInst->getCalledFunction()->getName() == "trigger") |
| 243 | // continue; |
| 244 | |
| 245 | // if((*s)->size()==2){ |
| 246 | // const Instruction *inst = (*s)->getInstList().begin(); |
| 247 | // if(isa<CallInst>(inst)){ |
| 248 | // Instruction *ii1 = (*s)->getInstList().begin(); |
| 249 | // CallInst *callInst = dyn_cast<CallInst>(ii1); |
| 250 | // if(callInst->getCalledFunction()->getName()=="trigger") |
| 251 | // continue; |
| 252 | // } |
| 253 | // } |
| 254 | |
| 255 | Node *nd2 = findBB(nodes,*s); |
Anand Shukla | ea77a49 | 2002-09-18 03:55:26 +0000 | [diff] [blame] | 256 | assert(nd2 && "No node for this edge!"); |
| 257 | Edge ed(nd,nd2,0); |
| 258 | edges.push_back(ed); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | graphMap[M]= new Graph(nodes,edges, startNode, exitNode); |
| 263 | |
| 264 | Graph *g = graphMap[M]; |
| 265 | |
| 266 | if (M->size() <= 1) return; //uninstrumented |
Anand Shukla | f8c09ee | 2003-02-14 20:41:53 +0000 | [diff] [blame] | 267 | |
Anand Shukla | ea77a49 | 2002-09-18 03:55:26 +0000 | [diff] [blame] | 268 | //step 2: getBackEdges |
| 269 | //vector<Edge> be; |
| 270 | std::map<Node *, int> nodePriority; |
| 271 | g->getBackEdges(beMap[M], nodePriority); |
Anand Shukla | f8c09ee | 2003-02-14 20:41:53 +0000 | [diff] [blame] | 272 | |
Anand Shukla | ea77a49 | 2002-09-18 03:55:26 +0000 | [diff] [blame] | 273 | //step 3: add dummy edges |
| 274 | //vector<Edge> stDummy; |
| 275 | //vector<Edge> exDummy; |
| 276 | addDummyEdges(stMap[M], exMap[M], *g, beMap[M]); |
Anand Shukla | f8c09ee | 2003-02-14 20:41:53 +0000 | [diff] [blame] | 277 | |
Anand Shukla | ea77a49 | 2002-09-18 03:55:26 +0000 | [diff] [blame] | 278 | //step 4: value assgn to edges |
| 279 | int numPaths = valueAssignmentToEdges(*g, nodePriority, beMap[M]); |
| 280 | } |
Anand Shukla | f8c09ee | 2003-02-14 20:41:53 +0000 | [diff] [blame] | 281 | |
| 282 | |
Anand Shukla | ea77a49 | 2002-09-18 03:55:26 +0000 | [diff] [blame] | 283 | //step 5: now travel from root, select max(edge) < pathNo, |
| 284 | //and go on until reach the exit |
Anand Shukla | f8c09ee | 2003-02-14 20:41:53 +0000 | [diff] [blame] | 285 | getPathFrmNode(graphMap[M]->getRoot(), vBB, pathNo, *graphMap[M], |
| 286 | stMap[M], exMap[M], beMap[M], -1); |
| 287 | |
| 288 | |
| 289 | //post process vBB to locate instructions to be erased |
| 290 | /* |
| 291 | if(pathReg[M]){ |
| 292 | for(vector<BasicBlock *>::iterator VBI = vBB.begin(), VBE = vBB.end(); |
| 293 | VBI != VBE; ++VBI){ |
| 294 | for(BasicBlock::iterator BBI = (*VBI)->begin(), BBE = (*VBI)->end(); |
| 295 | BBI != BBE; ++BBI){ |
Chris Lattner | 889f620 | 2003-04-23 16:37:45 +0000 | [diff] [blame] | 296 | if(LoadInst *ldInst = dyn_cast<LoadInst>(BBI)){ |
Anand Shukla | f8c09ee | 2003-02-14 20:41:53 +0000 | [diff] [blame] | 297 | if(pathReg[M] == ldInst->getPointerOperand()) |
| 298 | instToErase.push_back(ldInst); |
| 299 | } |
Chris Lattner | 889f620 | 2003-04-23 16:37:45 +0000 | [diff] [blame] | 300 | else if(StoreInst *stInst = dyn_cast<StoreInst>(BBI)){ |
Anand Shukla | f8c09ee | 2003-02-14 20:41:53 +0000 | [diff] [blame] | 301 | if(pathReg[M] == stInst->getPointerOperand()) |
| 302 | instToErase.push_back(stInst); |
| 303 | } |
| 304 | } |
| 305 | } |
| 306 | } |
| 307 | */ |
Anand Shukla | ea77a49 | 2002-09-18 03:55:26 +0000 | [diff] [blame] | 308 | } |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 309 | |
| 310 | } // End llvm namespace |