blob: 3a34131c88954aeafba86cb51e7c4f6df0c48e02 [file] [log] [blame]
Anand Shukla854c3022002-02-26 19:02:16 +00001//===-- GrapAuxillary.cpp- Auxillary functions on graph ----------*- C++ -*--=//
2//
3//auxillary function associated with graph: they
4//all operate on graph, and help in inserting
5//instrumentation for trace generation
6//
7//===----------------------------------------------------------------------===//
8
Anand Shukla21906892002-06-25 21:14:58 +00009#include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
Anand Shuklaf94ad682002-09-16 05:26:51 +000010#include "llvm/Transforms/Instrumentation/Graph.h"
Anand Shukla21906892002-06-25 21:14:58 +000011#include "llvm/Pass.h"
Anand Shuklaf94ad682002-09-16 05:26:51 +000012#include "llvm/Module.h"
Anand Shuklafd61c602002-07-18 20:56:47 +000013#include "llvm/iTerminators.h"
Chris Lattner3cf37822002-10-01 22:38:37 +000014#include "Support/Statistic.h"
Anand Shukla854c3022002-02-26 19:02:16 +000015#include <algorithm>
Chris Lattner5328c6f2002-02-26 19:40:28 +000016
Anand Shukla21906892002-06-25 21:14:58 +000017//using std::list;
Chris Lattner5328c6f2002-02-26 19:40:28 +000018using std::map;
19using std::vector;
20using std::cerr;
Anand Shukla854c3022002-02-26 19:02:16 +000021
22//check if 2 edges are equal (same endpoints and same weight)
Anand Shukla854c3022002-02-26 19:02:16 +000023static bool edgesEqual(Edge ed1, Edge ed2){
24 return ((ed1==ed2) && ed1.getWeight()==ed2.getWeight());
25}
26
27//Get the vector of edges that are to be instrumented in the graph
Chris Lattner570b8e12002-02-26 19:49:45 +000028static void getChords(vector<Edge > &chords, Graph &g, Graph st){
Anand Shukla854c3022002-02-26 19:02:16 +000029 //make sure the spanning tree is directional
30 //iterate over ALL the edges of the graph
Anand Shukla21906892002-06-25 21:14:58 +000031 vector<Node *> allNodes=g.getAllNodes();
32 for(vector<Node *>::iterator NI=allNodes.begin(), NE=allNodes.end(); NI!=NE;
Anand Shukla854c3022002-02-26 19:02:16 +000033 ++NI){
34 Graph::nodeList node_list=g.getNodeList(*NI);
35 for(Graph::nodeList::iterator NLI=node_list.begin(), NLE=node_list.end();
36 NLI!=NLE; ++NLI){
Anand Shukla21906892002-06-25 21:14:58 +000037 Edge f(*NI, NLI->element,NLI->weight, NLI->randId);
Anand Shukla854c3022002-02-26 19:02:16 +000038 if(!(st.hasEdgeAndWt(f)))//addnl
39 chords.push_back(f);
40 }
41 }
42}
43
44//Given a tree t, and a "directed graph" g
45//replace the edges in the tree t with edges that exist in graph
46//The tree is formed from "undirectional" copy of graph
47//So whatever edges the tree has, the undirectional graph
48//would have too. This function corrects some of the directions in
49//the tree so that now, all edge directions in the tree match
50//the edge directions of corresponding edges in the directed graph
Chris Lattner570b8e12002-02-26 19:49:45 +000051static void removeTreeEdges(Graph &g, Graph& t){
Anand Shukla21906892002-06-25 21:14:58 +000052 vector<Node* > allNodes=t.getAllNodes();
53 for(vector<Node *>::iterator NI=allNodes.begin(), NE=allNodes.end(); NI!=NE;
Anand Shukla854c3022002-02-26 19:02:16 +000054 ++NI){
55 Graph::nodeList nl=t.getNodeList(*NI);
56 for(Graph::nodeList::iterator NLI=nl.begin(), NLE=nl.end(); NLI!=NLE;++NLI){
57 Edge ed(NLI->element, *NI, NLI->weight);
Anand Shukla854c3022002-02-26 19:02:16 +000058 if(!g.hasEdgeAndWt(ed)) t.removeEdge(ed);//tree has only one edge
59 //between any pair of vertices, so no need to delete by edge wt
60 }
61 }
62}
63
64//Assign a value to all the edges in the graph
65//such that if we traverse along any path from root to exit, and
66//add up the edge values, we get a path number that uniquely
67//refers to the path we travelled
Anand Shuklaf94ad682002-09-16 05:26:51 +000068int valueAssignmentToEdges(Graph& g, map<Node *, int> nodePriority,
69 vector<Edge> &be){
Anand Shukla21906892002-06-25 21:14:58 +000070 vector<Node *> revtop=g.reverseTopologicalSort();
Anand Shukla854c3022002-02-26 19:02:16 +000071 map<Node *,int > NumPaths;
Anand Shukla2d3d20b2002-07-08 19:37:06 +000072 for(vector<Node *>::iterator RI=revtop.begin(), RE=revtop.end();
73 RI!=RE; ++RI){
Anand Shukla854c3022002-02-26 19:02:16 +000074 if(g.isLeaf(*RI))
75 NumPaths[*RI]=1;
76 else{
77 NumPaths[*RI]=0;
Anand Shukla2d3d20b2002-07-08 19:37:06 +000078
Anand Shuklaf94ad682002-09-16 05:26:51 +000079 // Modified Graph::nodeList &nlist=g.getNodeList(*RI);
80 Graph::nodeList &nlist=g.getSortedNodeList(*RI, be);
81
Anand Shukla21906892002-06-25 21:14:58 +000082 //sort nodelist by increasing order of numpaths
83
84 int sz=nlist.size();
Anand Shukla2d3d20b2002-07-08 19:37:06 +000085
Anand Shukla21906892002-06-25 21:14:58 +000086 for(int i=0;i<sz-1; i++){
87 int min=i;
Anand Shukla2d3d20b2002-07-08 19:37:06 +000088 for(int j=i+1; j<sz; j++){
89 BasicBlock *bb1 = nlist[j].element->getElement();
90 BasicBlock *bb2 = nlist[min].element->getElement();
Anand Shuklafd61c602002-07-18 20:56:47 +000091
92 if(bb1 == bb2) continue;
93
94 if(*RI == g.getRoot()){
95 assert(nodePriority[nlist[min].element]!=
96 nodePriority[nlist[j].element]
97 && "priorities can't be same!");
98
99 if(nodePriority[nlist[j].element] <
100 nodePriority[nlist[min].element])
101 min = j;
102 }
Anand Shukla2d3d20b2002-07-08 19:37:06 +0000103
Anand Shuklafd61c602002-07-18 20:56:47 +0000104 else{
105 TerminatorInst *tti = (*RI)->getElement()->getTerminator();
Anand Shuklaf94ad682002-09-16 05:26:51 +0000106
Anand Shuklafd61c602002-07-18 20:56:47 +0000107 BranchInst *ti = cast<BranchInst>(tti);
108 assert(ti && "not a branch");
109 assert(ti->getNumSuccessors()==2 && "less successors!");
110
111 BasicBlock *tB = ti->getSuccessor(0);
112 BasicBlock *fB = ti->getSuccessor(1);
113
114 if(tB == bb1 || fB == bb2)
Anand Shukla2d3d20b2002-07-08 19:37:06 +0000115 min = j;
Anand Shukla2d3d20b2002-07-08 19:37:06 +0000116 }
117
118 }
Anand Shukla21906892002-06-25 21:14:58 +0000119 graphListElement tempEl=nlist[min];
120 nlist[min]=nlist[i];
121 nlist[i]=tempEl;
122 }
Anand Shukla2d3d20b2002-07-08 19:37:06 +0000123
Anand Shukla21906892002-06-25 21:14:58 +0000124 //sorted now!
Anand Shukla21906892002-06-25 21:14:58 +0000125 for(Graph::nodeList::iterator GLI=nlist.begin(), GLE=nlist.end();
126 GLI!=GLE; ++GLI){
Anand Shuklaf94ad682002-09-16 05:26:51 +0000127 GLI->weight=NumPaths[*RI];
Anand Shukla21906892002-06-25 21:14:58 +0000128 NumPaths[*RI]+=NumPaths[GLI->element];
Anand Shukla854c3022002-02-26 19:02:16 +0000129 }
130 }
131 }
132 return NumPaths[g.getRoot()];
133}
134
135//This is a helper function to get the edge increments
136//This is used in conjuntion with inc_DFS
137//to get the edge increments
138//Edge increment implies assigning a value to all the edges in the graph
139//such that if we traverse along any path from root to exit, and
140//add up the edge values, we get a path number that uniquely
141//refers to the path we travelled
142//inc_Dir tells whether 2 edges are in same, or in different directions
143//if same direction, return 1, else -1
Chris Lattner570b8e12002-02-26 19:49:45 +0000144static int inc_Dir(Edge e, Edge f){
Anand Shukla854c3022002-02-26 19:02:16 +0000145 if(e.isNull())
146 return 1;
147
148 //check that the edges must have atleast one common endpoint
149 assert(*(e.getFirst())==*(f.getFirst()) ||
150 *(e.getFirst())==*(f.getSecond()) ||
151 *(e.getSecond())==*(f.getFirst()) ||
152 *(e.getSecond())==*(f.getSecond()));
153
154 if(*(e.getFirst())==*(f.getSecond()) ||
155 *(e.getSecond())==*(f.getFirst()))
156 return 1;
157
158 return -1;
159}
160
Anand Shukla21906892002-06-25 21:14:58 +0000161
Anand Shukla854c3022002-02-26 19:02:16 +0000162//used for getting edge increments (read comments above in inc_Dir)
163//inc_DFS is a modification of DFS
Anand Shuklaf94ad682002-09-16 05:26:51 +0000164static void inc_DFS(Graph& g,Graph& t,map<Edge, int, EdgeCompare2>& Increment,
Anand Shukla854c3022002-02-26 19:02:16 +0000165 int events, Node *v, Edge e){
166
Anand Shukla21906892002-06-25 21:14:58 +0000167 vector<Node *> allNodes=t.getAllNodes();
168
Anand Shukla21906892002-06-25 21:14:58 +0000169 for(vector<Node *>::iterator NI=allNodes.begin(), NE=allNodes.end(); NI!=NE;
Anand Shukla854c3022002-02-26 19:02:16 +0000170 ++NI){
171 Graph::nodeList node_list=t.getNodeList(*NI);
172 for(Graph::nodeList::iterator NLI=node_list.begin(), NLE=node_list.end();
173 NLI!= NLE; ++NLI){
Anand Shukla21906892002-06-25 21:14:58 +0000174 Edge f(*NI, NLI->element,NLI->weight, NLI->randId);
Anand Shukla854c3022002-02-26 19:02:16 +0000175 if(!edgesEqual(f,e) && *v==*(f.getSecond())){
176 int dir_count=inc_Dir(e,f);
177 int wt=1*f.getWeight();
178 inc_DFS(g,t, Increment, dir_count*events+wt, f.getFirst(), f);
179 }
180 }
181 }
182
Anand Shukla21906892002-06-25 21:14:58 +0000183 for(vector<Node *>::iterator NI=allNodes.begin(), NE=allNodes.end(); NI!=NE;
Anand Shukla854c3022002-02-26 19:02:16 +0000184 ++NI){
185 Graph::nodeList node_list=t.getNodeList(*NI);
186 for(Graph::nodeList::iterator NLI=node_list.begin(), NLE=node_list.end();
187 NLI!=NLE; ++NLI){
Anand Shukla21906892002-06-25 21:14:58 +0000188 Edge f(*NI, NLI->element,NLI->weight, NLI->randId);
Anand Shukla854c3022002-02-26 19:02:16 +0000189 if(!edgesEqual(f,e) && *v==*(f.getFirst())){
190 int dir_count=inc_Dir(e,f);
Anand Shukla21906892002-06-25 21:14:58 +0000191 int wt=f.getWeight();
Anand Shukla854c3022002-02-26 19:02:16 +0000192 inc_DFS(g,t, Increment, dir_count*events+wt,
193 f.getSecond(), f);
194 }
195 }
196 }
197
198 allNodes=g.getAllNodes();
Anand Shukla21906892002-06-25 21:14:58 +0000199 for(vector<Node *>::iterator NI=allNodes.begin(), NE=allNodes.end(); NI!=NE;
Anand Shukla854c3022002-02-26 19:02:16 +0000200 ++NI){
201 Graph::nodeList node_list=g.getNodeList(*NI);
202 for(Graph::nodeList::iterator NLI=node_list.begin(), NLE=node_list.end();
203 NLI!=NLE; ++NLI){
Anand Shukla21906892002-06-25 21:14:58 +0000204 Edge f(*NI, NLI->element,NLI->weight, NLI->randId);
Anand Shukla854c3022002-02-26 19:02:16 +0000205 if(!(t.hasEdgeAndWt(f)) && (*v==*(f.getSecond()) ||
206 *v==*(f.getFirst()))){
207 int dir_count=inc_Dir(e,f);
208 Increment[f]+=dir_count*events;
209 }
210 }
211 }
212}
213
214//Now we select a subset of all edges
215//and assign them some values such that
216//if we consider just this subset, it still represents
217//the path sum along any path in the graph
Anand Shuklaf94ad682002-09-16 05:26:51 +0000218static map<Edge, int, EdgeCompare2> getEdgeIncrements(Graph& g, Graph& t,
219 vector<Edge> &be){
Anand Shukla854c3022002-02-26 19:02:16 +0000220 //get all edges in g-t
Anand Shuklaf94ad682002-09-16 05:26:51 +0000221 map<Edge, int, EdgeCompare2> Increment;
Anand Shukla854c3022002-02-26 19:02:16 +0000222
Anand Shukla21906892002-06-25 21:14:58 +0000223 vector<Node *> allNodes=g.getAllNodes();
Anand Shukla854c3022002-02-26 19:02:16 +0000224
Anand Shukla21906892002-06-25 21:14:58 +0000225 for(vector<Node *>::iterator NI=allNodes.begin(), NE=allNodes.end(); NI!=NE;
Anand Shukla854c3022002-02-26 19:02:16 +0000226 ++NI){
Anand Shuklaf94ad682002-09-16 05:26:51 +0000227 Graph::nodeList node_list=g.getSortedNodeList(*NI, be);
228 //modified g.getNodeList(*NI);
Anand Shukla854c3022002-02-26 19:02:16 +0000229 for(Graph::nodeList::iterator NLI=node_list.begin(), NLE=node_list.end();
230 NLI!=NLE; ++NLI){
Anand Shukla21906892002-06-25 21:14:58 +0000231 Edge ed(*NI, NLI->element,NLI->weight,NLI->randId);
232 if(!(t.hasEdgeAndWt(ed))){
Anand Shukla854c3022002-02-26 19:02:16 +0000233 Increment[ed]=0;;
234 }
235 }
236 }
237
238 Edge *ed=new Edge();
239 inc_DFS(g,t,Increment, 0, g.getRoot(), *ed);
240
Anand Shukla21906892002-06-25 21:14:58 +0000241 for(vector<Node *>::iterator NI=allNodes.begin(), NE=allNodes.end(); NI!=NE;
Anand Shukla854c3022002-02-26 19:02:16 +0000242 ++NI){
Anand Shuklaf94ad682002-09-16 05:26:51 +0000243 Graph::nodeList node_list=g.getSortedNodeList(*NI, be);
244 //modified g.getNodeList(*NI);
Anand Shukla854c3022002-02-26 19:02:16 +0000245 for(Graph::nodeList::iterator NLI=node_list.begin(), NLE=node_list.end();
246 NLI!=NLE; ++NLI){
Anand Shukla21906892002-06-25 21:14:58 +0000247 Edge ed(*NI, NLI->element,NLI->weight, NLI->randId);
248 if(!(t.hasEdgeAndWt(ed))){
Anand Shukla854c3022002-02-26 19:02:16 +0000249 int wt=ed.getWeight();
250 Increment[ed]+=wt;
251 }
252 }
253 }
254
255 return Increment;
256}
257
Anand Shukla21906892002-06-25 21:14:58 +0000258//push it up: TODO
259const graphListElement *findNodeInList(const Graph::nodeList &NL,
260 Node *N);
261
262graphListElement *findNodeInList(Graph::nodeList &NL, Node *N);
263//end TODO
264
Anand Shukla854c3022002-02-26 19:02:16 +0000265//Based on edgeIncrements (above), now obtain
266//the kind of code to be inserted along an edge
267//The idea here is to minimize the computation
268//by inserting only the needed code
Anand Shuklaf94ad682002-09-16 05:26:51 +0000269static void getCodeInsertions(Graph &g, map<Edge, getEdgeCode *, EdgeCompare2> &instr,
Chris Lattner5328c6f2002-02-26 19:40:28 +0000270 vector<Edge > &chords,
Anand Shuklaf94ad682002-09-16 05:26:51 +0000271 map<Edge,int, EdgeCompare2> &edIncrements){
Anand Shukla854c3022002-02-26 19:02:16 +0000272
273 //Register initialization code
274 vector<Node *> ws;
275 ws.push_back(g.getRoot());
276 while(ws.size()>0){
Chris Lattner5328c6f2002-02-26 19:40:28 +0000277 Node *v=ws.back();
278 ws.pop_back();
Anand Shukla854c3022002-02-26 19:02:16 +0000279 //for each edge v->w
280 Graph::nodeList succs=g.getNodeList(v);
281
282 for(Graph::nodeList::iterator nl=succs.begin(), ne=succs.end();
283 nl!=ne; ++nl){
284 int edgeWt=nl->weight;
285 Node *w=nl->element;
286 //if chords has v->w
Anand Shukla21906892002-06-25 21:14:58 +0000287 Edge ed(v,w, edgeWt, nl->randId);
Anand Shukla854c3022002-02-26 19:02:16 +0000288 bool hasEdge=false;
289 for(vector<Edge>::iterator CI=chords.begin(), CE=chords.end();
290 CI!=CE && !hasEdge;++CI){
Anand Shukla21906892002-06-25 21:14:58 +0000291 if(*CI==ed && CI->getWeight()==edgeWt){//modf
Anand Shukla854c3022002-02-26 19:02:16 +0000292 hasEdge=true;
293 }
294 }
Anand Shukla21906892002-06-25 21:14:58 +0000295
296 if(hasEdge){//so its a chord edge
Anand Shukla854c3022002-02-26 19:02:16 +0000297 getEdgeCode *edCd=new getEdgeCode();
298 edCd->setCond(1);
299 edCd->setInc(edIncrements[ed]);
Chris Lattner5328c6f2002-02-26 19:40:28 +0000300 instr[ed]=edCd;
Anand Shukla854c3022002-02-26 19:02:16 +0000301 }
Anand Shukla21906892002-06-25 21:14:58 +0000302 else if(g.getNumberOfIncomingEdges(w)==1){
Anand Shukla854c3022002-02-26 19:02:16 +0000303 ws.push_back(w);
304 }
305 else{
306 getEdgeCode *edCd=new getEdgeCode();
307 edCd->setCond(2);
308 edCd->setInc(0);
Chris Lattner5328c6f2002-02-26 19:40:28 +0000309 instr[ed]=edCd;
Anand Shukla854c3022002-02-26 19:02:16 +0000310 }
311 }
312 }
313
314 /////Memory increment code
315 ws.push_back(g.getExit());
316
Chris Lattner5328c6f2002-02-26 19:40:28 +0000317 while(!ws.empty()) {
318 Node *w=ws.back();
319 ws.pop_back();
Anand Shukla21906892002-06-25 21:14:58 +0000320
321
322 ///////
323 //vector<Node *> lt;
324 vector<Node *> lllt=g.getAllNodes();
325 for(vector<Node *>::iterator EII=lllt.begin(); EII!=lllt.end() ;++EII){
326 Node *lnode=*EII;
327 Graph::nodeList &nl = g.getNodeList(lnode);
Anand Shuklaf94ad682002-09-16 05:26:51 +0000328 //graphListElement *N = findNodeInList(nl, w);
329 for(Graph::nodeList::const_iterator N = nl.begin(),
330 NNEN = nl.end(); N!= NNEN; ++N){
331 if (*N->element == *w){
332 Node *v=lnode;
333
334 //if chords has v->w
335 Edge ed(v,w, N->weight, N->randId);
336 getEdgeCode *edCd=new getEdgeCode();
337 bool hasEdge=false;
338 for(vector<Edge>::iterator CI=chords.begin(), CE=chords.end(); CI!=CE;
339 ++CI){
340 if(*CI==ed && CI->getWeight()==N->weight){
341 hasEdge=true;
342 break;
343 }
344 }
345 if(hasEdge){
346 //char str[100];
347 if(instr[ed]!=NULL && instr[ed]->getCond()==1){
348 instr[ed]->setCond(4);
349 }
350 else{
351 edCd->setCond(5);
352 edCd->setInc(edIncrements[ed]);
353 instr[ed]=edCd;
354 }
355
356 }
357 else if(g.getNumberOfOutgoingEdges(v)==1)
358 ws.push_back(v);
359 else{
360 edCd->setCond(6);
361 instr[ed]=edCd;
362 }
363 }
Anand Shukla854c3022002-02-26 19:02:16 +0000364 }
365 }
366 }
Anand Shukla854c3022002-02-26 19:02:16 +0000367 ///// Register increment code
368 for(vector<Edge>::iterator CI=chords.begin(), CE=chords.end(); CI!=CE; ++CI){
369 getEdgeCode *edCd=new getEdgeCode();
Chris Lattner5328c6f2002-02-26 19:40:28 +0000370 if(instr[*CI]==NULL){
371 edCd->setCond(3);
372 edCd->setInc(edIncrements[*CI]);
373 instr[*CI]=edCd;
374 }
Anand Shukla854c3022002-02-26 19:02:16 +0000375 }
Anand Shukla854c3022002-02-26 19:02:16 +0000376}
377
378//Add dummy edges corresponding to the back edges
379//If a->b is a backedge
380//then incoming dummy edge is root->b
381//and outgoing dummy edge is a->exit
Anand Shukla21906892002-06-25 21:14:58 +0000382//changed
Anand Shukla854c3022002-02-26 19:02:16 +0000383void addDummyEdges(vector<Edge > &stDummy,
384 vector<Edge > &exDummy,
Chris Lattner5328c6f2002-02-26 19:40:28 +0000385 Graph &g, vector<Edge> &be){
Anand Shukla854c3022002-02-26 19:02:16 +0000386 for(vector<Edge >::iterator VI=be.begin(), VE=be.end(); VI!=VE; ++VI){
387 Edge ed=*VI;
388 Node *first=ed.getFirst();
389 Node *second=ed.getSecond();
390 g.removeEdge(ed);
391
392 if(!(*second==*(g.getRoot()))){
Anand Shukla21906892002-06-25 21:14:58 +0000393 Edge *st=new Edge(g.getRoot(), second, ed.getWeight(), ed.getRandId());
394 stDummy.push_back(*st);
Chris Lattner5328c6f2002-02-26 19:40:28 +0000395 g.addEdgeForce(*st);
Anand Shukla854c3022002-02-26 19:02:16 +0000396 }
397
398 if(!(*first==*(g.getExit()))){
Anand Shukla21906892002-06-25 21:14:58 +0000399 Edge *ex=new Edge(first, g.getExit(), ed.getWeight(), ed.getRandId());
400 exDummy.push_back(*ex);
401 g.addEdgeForce(*ex);
Anand Shukla854c3022002-02-26 19:02:16 +0000402 }
403 }
404}
405
406//print a given edge in the form BB1Label->BB2Label
407void printEdge(Edge ed){
408 cerr<<((ed.getFirst())->getElement())
409 ->getName()<<"->"<<((ed.getSecond())
410 ->getElement())->getName()<<
Anand Shukla21906892002-06-25 21:14:58 +0000411 ":"<<ed.getWeight()<<" rndId::"<<ed.getRandId()<<"\n";
Anand Shukla854c3022002-02-26 19:02:16 +0000412}
413
414//Move the incoming dummy edge code and outgoing dummy
415//edge code over to the corresponding back edge
Anand Shukla21906892002-06-25 21:14:58 +0000416static void moveDummyCode(vector<Edge> &stDummy,
417 vector<Edge> &exDummy,
418 vector<Edge> &be,
Anand Shuklaf94ad682002-09-16 05:26:51 +0000419 map<Edge, getEdgeCode *, EdgeCompare2> &insertions,
Anand Shukla21906892002-06-25 21:14:58 +0000420 Graph &g){
421 typedef vector<Edge >::iterator vec_iter;
Anand Shukla854c3022002-02-26 19:02:16 +0000422
Anand Shuklaf94ad682002-09-16 05:26:51 +0000423 map<Edge,getEdgeCode *, EdgeCompare2> temp;
Anand Shukla21906892002-06-25 21:14:58 +0000424 //iterate over edges with code
Chris Lattner5328c6f2002-02-26 19:40:28 +0000425 std::vector<Edge> toErase;
Anand Shuklaf94ad682002-09-16 05:26:51 +0000426 for(map<Edge,getEdgeCode *, EdgeCompare2>::iterator MI=insertions.begin(),
Anand Shukla854c3022002-02-26 19:02:16 +0000427 ME=insertions.end(); MI!=ME; ++MI){
428 Edge ed=MI->first;
429 getEdgeCode *edCd=MI->second;
Anand Shukla21906892002-06-25 21:14:58 +0000430
431 ///---new code
432 //iterate over be, and check if its starts and end vertices hv code
433 for(vector<Edge>::iterator BEI=be.begin(), BEE=be.end(); BEI!=BEE; ++BEI){
434 if(ed.getRandId()==BEI->getRandId()){
435
Anand Shukla21906892002-06-25 21:14:58 +0000436 if(temp[*BEI]==0)
437 temp[*BEI]=new getEdgeCode();
438
439 //so ed is either in st, or ex!
440 if(ed.getFirst()==g.getRoot()){
Anand Shukla2d3d20b2002-07-08 19:37:06 +0000441
Anand Shukla21906892002-06-25 21:14:58 +0000442 //so its in stDummy
443 temp[*BEI]->setCdIn(edCd);
444 toErase.push_back(ed);
445 }
446 else if(ed.getSecond()==g.getExit()){
Anand Shukla2d3d20b2002-07-08 19:37:06 +0000447
Anand Shukla21906892002-06-25 21:14:58 +0000448 //so its in exDummy
449 toErase.push_back(ed);
450 temp[*BEI]->setCdOut(edCd);
451 }
452 else{
453 assert(false && "Not found in either start or end! Rand failed?");
454 }
455 }
456 }
457 }
458
459 for(vector<Edge >::iterator vmi=toErase.begin(), vme=toErase.end(); vmi!=vme;
460 ++vmi){
461 insertions.erase(*vmi);
Anand Shukla21906892002-06-25 21:14:58 +0000462 g.removeEdgeWithWt(*vmi);
463 }
464
Anand Shuklaf94ad682002-09-16 05:26:51 +0000465 for(map<Edge,getEdgeCode *, EdgeCompare2>::iterator MI=temp.begin(),
Anand Shukla21906892002-06-25 21:14:58 +0000466 ME=temp.end(); MI!=ME; ++MI){
467 insertions[MI->first]=MI->second;
Anand Shukla21906892002-06-25 21:14:58 +0000468 }
Anand Shukla2d3d20b2002-07-08 19:37:06 +0000469
Anand Shukla21906892002-06-25 21:14:58 +0000470#ifdef DEBUG_PATH_PROFILES
471 cerr<<"size of deletions: "<<toErase.size()<<"\n";
Anand Shukla21906892002-06-25 21:14:58 +0000472 cerr<<"SIZE OF INSERTIONS AFTER DEL "<<insertions.size()<<"\n";
473#endif
Anand Shukla854c3022002-02-26 19:02:16 +0000474
Anand Shukla854c3022002-02-26 19:02:16 +0000475}
476
Chris Lattner18ff9452002-02-26 19:43:49 +0000477//Do graph processing: to determine minimal edge increments,
478//appropriate code insertions etc and insert the code at
479//appropriate locations
480void processGraph(Graph &g,
481 Instruction *rInst,
482 Instruction *countInst,
483 vector<Edge >& be,
484 vector<Edge >& stDummy,
Anand Shukla21906892002-06-25 21:14:58 +0000485 vector<Edge >& exDummy,
Anand Shukla77dca142002-09-20 16:44:35 +0000486 int numPaths, int MethNo,
487 Value *threshold){
Anand Shukla21906892002-06-25 21:14:58 +0000488
Chris Lattner18ff9452002-02-26 19:43:49 +0000489 //Given a graph: with exit->root edge, do the following in seq:
490 //1. get back edges
491 //2. insert dummy edges and remove back edges
492 //3. get edge assignments
493 //4. Get Max spanning tree of graph:
494 // -Make graph g2=g undirectional
495 // -Get Max spanning tree t
496 // -Make t undirectional
497 // -remove edges from t not in graph g
498 //5. Get edge increments
499 //6. Get code insertions
500 //7. move code on dummy edges over to the back edges
501
502
503 //This is used as maximum "weight" for
504 //priority queue
505 //This would hold all
506 //right as long as number of paths in the graph
507 //is less than this
Chris Lattner22cbac62002-09-17 23:46:33 +0000508 const int Infinity=99999999;
Chris Lattner18ff9452002-02-26 19:43:49 +0000509
510
511 //step 1-3 are already done on the graph when this function is called
Chris Lattnere1fc2d92002-05-22 21:56:32 +0000512 DEBUG(printGraph(g));
513
Chris Lattner18ff9452002-02-26 19:43:49 +0000514 //step 4: Get Max spanning tree of graph
515
516 //now insert exit to root edge
517 //if its there earlier, remove it!
Chris Lattner22cbac62002-09-17 23:46:33 +0000518 //assign it weight Infinity
Chris Lattner18ff9452002-02-26 19:43:49 +0000519 //so that this edge IS ALWAYS IN spanning tree
520 //Note than edges in spanning tree do not get
521 //instrumented: and we do not want the
522 //edge exit->root to get instrumented
523 //as it MAY BE a dummy edge
Chris Lattner22cbac62002-09-17 23:46:33 +0000524 Edge ed(g.getExit(),g.getRoot(),Infinity);
525 g.addEdge(ed,Infinity);
Chris Lattner18ff9452002-02-26 19:43:49 +0000526 Graph g2=g;
527
528 //make g2 undirectional: this gives a better
529 //maximal spanning tree
530 g2.makeUnDirectional();
Chris Lattnere1fc2d92002-05-22 21:56:32 +0000531 DEBUG(printGraph(g2));
532
Chris Lattner18ff9452002-02-26 19:43:49 +0000533 Graph *t=g2.getMaxSpanningTree();
Anand Shukla2d3d20b2002-07-08 19:37:06 +0000534#ifdef DEBUG_PATH_PROFILES
535 std::cerr<<"Original maxspanning tree\n";
536 printGraph(*t);
537#endif
Chris Lattner18ff9452002-02-26 19:43:49 +0000538 //now edges of tree t have weights reversed
539 //(negative) because the algorithm used
540 //to find max spanning tree is
541 //actually for finding min spanning tree
542 //so get back the original weights
543 t->reverseWts();
544
545 //Ordinarily, the graph is directional
546 //lets converts the graph into an
547 //undirectional graph
548 //This is done by adding an edge
549 //v->u for all existing edges u->v
550 t->makeUnDirectional();
551
552 //Given a tree t, and a "directed graph" g
553 //replace the edges in the tree t with edges that exist in graph
554 //The tree is formed from "undirectional" copy of graph
555 //So whatever edges the tree has, the undirectional graph
556 //would have too. This function corrects some of the directions in
557 //the tree so that now, all edge directions in the tree match
558 //the edge directions of corresponding edges in the directed graph
559 removeTreeEdges(g, *t);
Chris Lattnere1fc2d92002-05-22 21:56:32 +0000560
Anand Shukla21906892002-06-25 21:14:58 +0000561#ifdef DEBUG_PATH_PROFILES
562 cerr<<"Final Spanning tree---------\n";
563 printGraph(*t);
564 cerr<<"-------end spanning tree\n";
565#endif
Chris Lattnere1fc2d92002-05-22 21:56:32 +0000566
Chris Lattner18ff9452002-02-26 19:43:49 +0000567 //now remove the exit->root node
568 //and re-add it with weight 0
569 //since infinite weight is kinda confusing
570 g.removeEdge(ed);
571 Edge edNew(g.getExit(), g.getRoot(),0);
572 g.addEdge(edNew,0);
573 if(t->hasEdge(ed)){
574 t->removeEdge(ed);
575 t->addEdge(edNew,0);
576 }
577
Chris Lattnere1fc2d92002-05-22 21:56:32 +0000578 DEBUG(printGraph(g);
579 printGraph(*t));
580
Chris Lattner18ff9452002-02-26 19:43:49 +0000581 //step 5: Get edge increments
582
583 //Now we select a subset of all edges
584 //and assign them some values such that
585 //if we consider just this subset, it still represents
586 //the path sum along any path in the graph
Chris Lattnere1fc2d92002-05-22 21:56:32 +0000587
Anand Shuklaf94ad682002-09-16 05:26:51 +0000588 map<Edge, int, EdgeCompare2> increment=getEdgeIncrements(g,*t, be);
Anand Shukla21906892002-06-25 21:14:58 +0000589#ifdef DEBUG_PATH_PROFILES
590 //print edge increments for debugging
Anand Shuklaf94ad682002-09-16 05:26:51 +0000591 std::cerr<<"Edge Increments------\n";
592 for(map<Edge, int, EdgeCompare2>::iterator MMI=increment.begin(), MME=increment.end(); MMI != MME; ++MMI){
593 printEdge(MMI->first);
594 std::cerr<<"Increment for above:"<<MMI->second<<"\n";
Anand Shukla21906892002-06-25 21:14:58 +0000595 }
Anand Shuklaf94ad682002-09-16 05:26:51 +0000596 std::cerr<<"-------end of edge increments\n";
Anand Shukla21906892002-06-25 21:14:58 +0000597#endif
598
Chris Lattner18ff9452002-02-26 19:43:49 +0000599
600 //step 6: Get code insertions
601
602 //Based on edgeIncrements (above), now obtain
603 //the kind of code to be inserted along an edge
604 //The idea here is to minimize the computation
605 //by inserting only the needed code
606 vector<Edge> chords;
607 getChords(chords, g, *t);
608
Anand Shukla21906892002-06-25 21:14:58 +0000609
Anand Shuklaf94ad682002-09-16 05:26:51 +0000610 map<Edge, getEdgeCode *, EdgeCompare2> codeInsertions;
Chris Lattner18ff9452002-02-26 19:43:49 +0000611 getCodeInsertions(g, codeInsertions, chords,increment);
612
Anand Shukla21906892002-06-25 21:14:58 +0000613#ifdef DEBUG_PATH_PROFILES
614 //print edges with code for debugging
615 cerr<<"Code inserted in following---------------\n";
Anand Shuklaf94ad682002-09-16 05:26:51 +0000616 for(map<Edge, getEdgeCode *, EdgeCompare2>::iterator cd_i=codeInsertions.begin(),
Anand Shukla21906892002-06-25 21:14:58 +0000617 cd_e=codeInsertions.end(); cd_i!=cd_e; ++cd_i){
618 printEdge(cd_i->first);
619 cerr<<cd_i->second->getCond()<<":"<<cd_i->second->getInc()<<"\n";
620 }
621 cerr<<"-----end insertions\n";
622#endif
Chris Lattnere1fc2d92002-05-22 21:56:32 +0000623
Chris Lattner18ff9452002-02-26 19:43:49 +0000624 //step 7: move code on dummy edges over to the back edges
625
626 //Move the incoming dummy edge code and outgoing dummy
627 //edge code over to the corresponding back edge
Anand Shukla21906892002-06-25 21:14:58 +0000628
629 moveDummyCode(stDummy, exDummy, be, codeInsertions, g);
Anand Shukla2d3d20b2002-07-08 19:37:06 +0000630
Anand Shukla21906892002-06-25 21:14:58 +0000631#ifdef DEBUG_PATH_PROFILES
632 //debugging info
633 cerr<<"After moving dummy code\n";
634 for(map<Edge, getEdgeCode *>::iterator cd_i=codeInsertions.begin(),
635 cd_e=codeInsertions.end(); cd_i != cd_e; ++cd_i){
636 printEdge(cd_i->first);
637 cerr<<cd_i->second->getCond()<<":"
638 <<cd_i->second->getInc()<<"\n";
639 }
640 cerr<<"Dummy end------------\n";
641#endif
642
Chris Lattner18ff9452002-02-26 19:43:49 +0000643
644 //see what it looks like...
645 //now insert code along edges which have codes on them
646 for(map<Edge, getEdgeCode *>::iterator MI=codeInsertions.begin(),
647 ME=codeInsertions.end(); MI!=ME; ++MI){
648 Edge ed=MI->first;
Anand Shukla77dca142002-09-20 16:44:35 +0000649 insertBB(ed, MI->second, rInst, countInst, numPaths, MethNo, threshold);
Chris Lattner18ff9452002-02-26 19:43:49 +0000650 }
651}
652
Anand Shukla854c3022002-02-26 19:02:16 +0000653//print the graph (for debugging)
Chris Lattner570b8e12002-02-26 19:49:45 +0000654void printGraph(Graph &g){
Anand Shukla21906892002-06-25 21:14:58 +0000655 vector<Node *> lt=g.getAllNodes();
Anand Shukla854c3022002-02-26 19:02:16 +0000656 cerr<<"Graph---------------------\n";
Anand Shukla21906892002-06-25 21:14:58 +0000657 for(vector<Node *>::iterator LI=lt.begin();
Anand Shukla854c3022002-02-26 19:02:16 +0000658 LI!=lt.end(); ++LI){
659 cerr<<((*LI)->getElement())->getName()<<"->";
660 Graph::nodeList nl=g.getNodeList(*LI);
661 for(Graph::nodeList::iterator NI=nl.begin();
662 NI!=nl.end(); ++NI){
663 cerr<<":"<<"("<<(NI->element->getElement())
Anand Shukla21906892002-06-25 21:14:58 +0000664 ->getName()<<":"<<NI->element->getWeight()<<","<<NI->weight<<","
665 <<NI->randId<<")";
Anand Shukla854c3022002-02-26 19:02:16 +0000666 }
667 cerr<<"\n";
668 }
669 cerr<<"--------------------Graph\n";
670}