blob: fd0844e14b9e976d7faa5e4cca4f31645dc0eccb [file] [log] [blame]
Chris Lattner44d2c352003-10-13 03:32:08 +00001//===- RetracePath.cpp ----------------------------------------------------===//
John Criswell482202a2003-10-20 19:43:21 +00002//
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 Shuklaea77a492002-09-18 03:55:26 +00009//
10// Retraces a path of BasicBlock, given a path number and a graph!
11//
12//===----------------------------------------------------------------------===//
13
Anand Shuklaea77a492002-09-18 03:55:26 +000014#include "llvm/Module.h"
Misha Brukman63b38bd2004-07-29 17:30:56 +000015#include "llvm/Instructions.h"
Chris Lattner2f04a0d2003-01-14 22:33:56 +000016#include "llvm/Support/CFG.h"
17#include "Graph.h"
Reid Spencereb04d9b2004-07-04 12:19:56 +000018#include <iostream>
Anand Shuklaea77a492002-09-18 03:55:26 +000019
20using std::vector;
21using std::map;
22using std::cerr;
23
Brian Gaeke960707c2003-11-11 22:41:34 +000024namespace llvm {
25
Anand Shuklaea77a492002-09-18 03:55:26 +000026//Routines to get the path trace!
27
Anand Shuklaf8c09ee2003-02-14 20:41:53 +000028void getPathFrmNode(Node *n, vector<BasicBlock*> &vBB, int pathNo, Graph &g,
Anand Shuklaea77a492002-09-18 03:55:26 +000029 vector<Edge> &stDummy, vector<Edge> &exDummy,
30 vector<Edge> &be,
31 double strand){
Anand Shuklaf8c09ee2003-02-14 20:41:53 +000032 Graph::nodeList &nlist = g.getNodeList(n);
Anand Shuklaea77a492002-09-18 03:55:26 +000033
Anand Shuklaf8c09ee2003-02-14 20:41:53 +000034 //printGraph(g);
35 //std::cerr<<"Path No: "<<pathNo<<"\n";
Anand Shuklaea77a492002-09-18 03:55:26 +000036 int maxCount=-9999999;
37 bool isStart=false;
38
39 if(*n==*g.getRoot())//its root: so first node of path
40 isStart=true;
41
42 double edgeRnd=0;
43 Node *nextRoot=n;
Anand Shuklaf8c09ee2003-02-14 20:41:53 +000044 for(Graph::nodeList::iterator NLI = nlist.begin(), NLE=nlist.end(); NLI!=NLE;
Anand Shuklaea77a492002-09-18 03:55:26 +000045 ++NLI){
46 if(NLI->weight>maxCount && NLI->weight<=pathNo){
47 maxCount=NLI->weight;
48 nextRoot=NLI->element;
49 edgeRnd=NLI->randId;
50 if(isStart)
51 strand=NLI->randId;
52 }
53 }
54
55 if(!isStart)
56 assert(strand!=-1 && "strand not assigned!");
57
58 assert(!(*nextRoot==*n && pathNo>0) && "No more BBs to go");
59 assert(!(*nextRoot==*g.getExit() && pathNo-maxCount!=0) && "Reached exit");
60
61 vBB.push_back(n->getElement());
62
63 if(pathNo-maxCount==0 && *nextRoot==*g.getExit()){
64
65 //look for strnd and edgeRnd now:
66 bool has1=false, has2=false;
67 //check if exit has it
68 for(vector<Edge>::iterator VI=exDummy.begin(), VE=exDummy.end(); VI!=VE;
69 ++VI){
70 if(VI->getRandId()==edgeRnd){
71 has2=true;
72 break;
73 }
74 }
75
76 //check if start has it
77 for(vector<Edge>::iterator VI=stDummy.begin(), VE=stDummy.end(); VI!=VE;
78 ++VI){
79 if(VI->getRandId()==strand){
80 has1=true;
81 break;
82 }
83 }
84
85 if(has1){
86 //find backedge with endpoint vBB[1]
87 for(vector<Edge>::iterator VI=be.begin(), VE=be.end(); VI!=VE; ++VI){
88 assert(vBB.size()>0 && "vector too small");
89 if( VI->getSecond()->getElement() == vBB[1] ){
90 //vBB[0]=VI->getFirst()->getElement();
91 vBB.erase(vBB.begin());
92 break;
93 }
94 }
95 }
96
97 if(has2){
98 //find backedge with startpoint vBB[vBB.size()-1]
99 for(vector<Edge>::iterator VI=be.begin(), VE=be.end(); VI!=VE; ++VI){
100 assert(vBB.size()>0 && "vector too small");
101 if( VI->getFirst()->getElement() == vBB[vBB.size()-1] &&
102 VI->getSecond()->getElement() == vBB[0]){
103 //vBB.push_back(VI->getSecond()->getElement());
104 break;
105 }
106 }
107 }
108 else
109 vBB.push_back(nextRoot->getElement());
110
111 return;
112 }
113
114 assert(pathNo-maxCount>=0);
115
116 return getPathFrmNode(nextRoot, vBB, pathNo-maxCount, g, stDummy,
117 exDummy, be, strand);
118}
119
120
121static Node *findBB(std::vector<Node *> &st, BasicBlock *BB){
122 for(std::vector<Node *>::iterator si=st.begin(); si!=st.end(); ++si){
123 if(((*si)->getElement())==BB){
124 return *si;
125 }
126 }
127 return NULL;
128}
129
Anand Shuklaf8c09ee2003-02-14 20:41:53 +0000130void getBBtrace(vector<BasicBlock *> &vBB, int pathNo, Function *M){//,
131 // vector<Instruction *> &instToErase){
Anand Shuklaea77a492002-09-18 03:55:26 +0000132 //step 1: create graph
133 //Transform the cfg s.t. we have just one exit node
134
135 std::vector<Node *> nodes;
136 std::vector<Edge> edges;
137 Node *tmp;
138 Node *exitNode=0, *startNode=0;
Anand Shuklaf8c09ee2003-02-14 20:41:53 +0000139
140 //Creat cfg just once for each function!
141 static std::map<Function *, Graph *> graphMap;
142
143 //get backedges, exit and start edges for the graphs and store them
Anand Shuklaea77a492002-09-18 03:55:26 +0000144 static std::map<Function *, vector<Edge> > stMap, exMap, beMap;
Anand Shuklaf8c09ee2003-02-14 20:41:53 +0000145 static std::map<Function *, Value *> pathReg; //path register
146
Anand Shuklaea77a492002-09-18 03:55:26 +0000147
148 if(!graphMap[M]){
149 BasicBlock *ExitNode = 0;
150 for (Function::iterator I = M->begin(), E = M->end(); I != E; ++I){
151 if (isa<ReturnInst>(I->getTerminator())) {
Chris Lattner889f6202003-04-23 16:37:45 +0000152 ExitNode = I;
Anand Shuklaea77a492002-09-18 03:55:26 +0000153 break;
154 }
155 }
156
157 assert(ExitNode!=0 && "exitnode not found");
158
159 //iterating over BBs and making graph
160 //The nodes must be uniquely identified:
161 //That is, no two nodes must hav same BB*
162
Anand Shuklaf8c09ee2003-02-14 20:41:53 +0000163 //keep a map for trigger basicblocks!
164 std::map<BasicBlock *, unsigned char> triggerBBs;
Anand Shuklaea77a492002-09-18 03:55:26 +0000165 //First enter just nodes: later enter edges
166 for(Function::iterator BB = M->begin(), BE=M->end(); BB != BE; ++BB){
Anand Shuklaf8c09ee2003-02-14 20:41:53 +0000167 bool cont = false;
168
169 if(BB->size()==3 || BB->size() ==2){
170 for(BasicBlock::iterator II = BB->begin(), IE = BB->end();
171 II != IE; ++II){
Chris Lattner889f6202003-04-23 16:37:45 +0000172 if(CallInst *callInst = dyn_cast<CallInst>(II)){
Anand Shuklaf8c09ee2003-02-14 20:41:53 +0000173 //std::cerr<<*callInst;
174 Function *calledFunction = callInst->getCalledFunction();
175 if(calledFunction && calledFunction->getName() == "trigger"){
176 triggerBBs[BB] = 9;
177 cont = true;
178 //std::cerr<<"Found trigger!\n";
179 break;
180 }
181 }
Anand Shuklaea77a492002-09-18 03:55:26 +0000182 }
183 }
Anand Shuklaf8c09ee2003-02-14 20:41:53 +0000184
185 if(cont)
186 continue;
187
188 // const Instruction *inst = BB->getInstList().begin();
189 // if(isa<CallInst>(inst)){
190 // Instruction *ii1 = BB->getInstList().begin();
191 // CallInst *callInst = dyn_cast<CallInst>(ii1);
192 // if(callInst->getCalledFunction()->getName()=="trigger")
193 // continue;
194 // }
195
Anand Shuklaea77a492002-09-18 03:55:26 +0000196 Node *nd=new Node(BB);
197 nodes.push_back(nd);
198 if(&*BB==ExitNode)
199 exitNode=nd;
200 if(&*BB==&M->front())
201 startNode=nd;
202 }
203
204 assert(exitNode!=0 && startNode!=0 && "Start or exit not found!");
205
206 for (Function::iterator BB = M->begin(), BE=M->end(); BB != BE; ++BB){
Anand Shuklaf8c09ee2003-02-14 20:41:53 +0000207 if(triggerBBs[BB] == 9)
208 continue;
209
210 //if(BB->size()==3)
Chris Lattner889f6202003-04-23 16:37:45 +0000211 //if(CallInst *callInst = dyn_cast<CallInst>(BB->getInstList().begin()))
Anand Shuklaf8c09ee2003-02-14 20:41:53 +0000212 //if(callInst->getCalledFunction()->getName() == "trigger")
213 //continue;
214
215 // if(BB->size()==2){
216 // const Instruction *inst = BB->getInstList().begin();
217 // if(isa<CallInst>(inst)){
218 // Instruction *ii1 = BB->getInstList().begin();
219 // CallInst *callInst = dyn_cast<CallInst>(ii1);
220 // if(callInst->getCalledFunction()->getName()=="trigger")
221 // continue;
222 // }
223 // }
224
Anand Shuklaea77a492002-09-18 03:55:26 +0000225 Node *nd=findBB(nodes, BB);
226 assert(nd && "No node for this edge!");
Anand Shuklaf8c09ee2003-02-14 20:41:53 +0000227
Chris Lattnera9400952003-09-24 22:06:25 +0000228 for(succ_iterator s=succ_begin(BB), se=succ_end(BB); s!=se; ++s){
Anand Shuklaf8c09ee2003-02-14 20:41:53 +0000229
230 if(triggerBBs[*s] == 9){
231 //if(!pathReg[M]){ //Get the path register for this!
232 //if(BB->size()>8)
Chris Lattner889f6202003-04-23 16:37:45 +0000233 // if(LoadInst *ldInst = dyn_cast<LoadInst>(BB->getInstList().begin()))
Anand Shuklaf8c09ee2003-02-14 20:41:53 +0000234 // pathReg[M] = ldInst->getPointerOperand();
235 //}
236 continue;
Anand Shuklaea77a492002-09-18 03:55:26 +0000237 }
Anand Shuklaf8c09ee2003-02-14 20:41:53 +0000238 //if((*s)->size()==3)
239 //if(CallInst *callInst =
Chris Lattner889f6202003-04-23 16:37:45 +0000240 // dyn_cast<CallInst>((*s)->getInstList().begin()))
Anand Shuklaf8c09ee2003-02-14 20:41:53 +0000241 // if(callInst->getCalledFunction()->getName() == "trigger")
242 // continue;
243
244 // if((*s)->size()==2){
245 // const Instruction *inst = (*s)->getInstList().begin();
246 // if(isa<CallInst>(inst)){
247 // Instruction *ii1 = (*s)->getInstList().begin();
248 // CallInst *callInst = dyn_cast<CallInst>(ii1);
249 // if(callInst->getCalledFunction()->getName()=="trigger")
250 // continue;
251 // }
252 // }
253
254 Node *nd2 = findBB(nodes,*s);
Anand Shuklaea77a492002-09-18 03:55:26 +0000255 assert(nd2 && "No node for this edge!");
256 Edge ed(nd,nd2,0);
257 edges.push_back(ed);
258 }
259 }
260
261 graphMap[M]= new Graph(nodes,edges, startNode, exitNode);
262
263 Graph *g = graphMap[M];
264
265 if (M->size() <= 1) return; //uninstrumented
Anand Shuklaf8c09ee2003-02-14 20:41:53 +0000266
Anand Shuklaea77a492002-09-18 03:55:26 +0000267 //step 2: getBackEdges
268 //vector<Edge> be;
269 std::map<Node *, int> nodePriority;
270 g->getBackEdges(beMap[M], nodePriority);
Anand Shuklaf8c09ee2003-02-14 20:41:53 +0000271
Anand Shuklaea77a492002-09-18 03:55:26 +0000272 //step 3: add dummy edges
273 //vector<Edge> stDummy;
274 //vector<Edge> exDummy;
275 addDummyEdges(stMap[M], exMap[M], *g, beMap[M]);
Anand Shuklaf8c09ee2003-02-14 20:41:53 +0000276
Anand Shuklaea77a492002-09-18 03:55:26 +0000277 //step 4: value assgn to edges
278 int numPaths = valueAssignmentToEdges(*g, nodePriority, beMap[M]);
279 }
Anand Shuklaf8c09ee2003-02-14 20:41:53 +0000280
281
Anand Shuklaea77a492002-09-18 03:55:26 +0000282 //step 5: now travel from root, select max(edge) < pathNo,
283 //and go on until reach the exit
Anand Shuklaf8c09ee2003-02-14 20:41:53 +0000284 getPathFrmNode(graphMap[M]->getRoot(), vBB, pathNo, *graphMap[M],
285 stMap[M], exMap[M], beMap[M], -1);
286
287
288 //post process vBB to locate instructions to be erased
289 /*
290 if(pathReg[M]){
291 for(vector<BasicBlock *>::iterator VBI = vBB.begin(), VBE = vBB.end();
292 VBI != VBE; ++VBI){
293 for(BasicBlock::iterator BBI = (*VBI)->begin(), BBE = (*VBI)->end();
294 BBI != BBE; ++BBI){
Chris Lattner889f6202003-04-23 16:37:45 +0000295 if(LoadInst *ldInst = dyn_cast<LoadInst>(BBI)){
Anand Shuklaf8c09ee2003-02-14 20:41:53 +0000296 if(pathReg[M] == ldInst->getPointerOperand())
297 instToErase.push_back(ldInst);
298 }
Chris Lattner889f6202003-04-23 16:37:45 +0000299 else if(StoreInst *stInst = dyn_cast<StoreInst>(BBI)){
Anand Shuklaf8c09ee2003-02-14 20:41:53 +0000300 if(pathReg[M] == stInst->getPointerOperand())
301 instToErase.push_back(stInst);
302 }
303 }
304 }
305 }
306 */
Anand Shuklaea77a492002-09-18 03:55:26 +0000307}
Brian Gaeke960707c2003-11-11 22:41:34 +0000308
309} // End llvm namespace