blob: 45ceadaa930763b650fdfb6aed96dc86f0580c2f [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/Function.h"
12#include "llvm/Pass.h"
Anand Shuklaf94ad682002-09-16 05:26:51 +000013#include "llvm/Module.h"
14#include "llvm/Function.h"
Anand Shukla854c3022002-02-26 19:02:16 +000015#include "llvm/BasicBlock.h"
Anand Shukla2d3d20b2002-07-08 19:37:06 +000016#include "llvm/InstrTypes.h"
Anand Shuklafd61c602002-07-18 20:56:47 +000017#include "llvm/iTerminators.h"
Anand Shukla854c3022002-02-26 19:02:16 +000018#include <algorithm>
Chris Lattner5328c6f2002-02-26 19:40:28 +000019#include <iostream>
Anand Shukla2d3d20b2002-07-08 19:37:06 +000020#include <sstream>
Anand Shuklaf94ad682002-09-16 05:26:51 +000021#include <vector>
Anand Shukla2d3d20b2002-07-08 19:37:06 +000022#include <string>
Chris Lattner5328c6f2002-02-26 19:40:28 +000023
Anand Shukla21906892002-06-25 21:14:58 +000024//using std::list;
Chris Lattner5328c6f2002-02-26 19:40:28 +000025using std::map;
26using std::vector;
27using std::cerr;
Anand Shukla854c3022002-02-26 19:02:16 +000028
29//check if 2 edges are equal (same endpoints and same weight)
Anand Shukla854c3022002-02-26 19:02:16 +000030static bool edgesEqual(Edge ed1, Edge ed2){
31 return ((ed1==ed2) && ed1.getWeight()==ed2.getWeight());
32}
33
34//Get the vector of edges that are to be instrumented in the graph
Chris Lattner570b8e12002-02-26 19:49:45 +000035static void getChords(vector<Edge > &chords, Graph &g, Graph st){
Anand Shukla854c3022002-02-26 19:02:16 +000036 //make sure the spanning tree is directional
37 //iterate over ALL the edges of the graph
Anand Shukla21906892002-06-25 21:14:58 +000038 vector<Node *> allNodes=g.getAllNodes();
39 for(vector<Node *>::iterator NI=allNodes.begin(), NE=allNodes.end(); NI!=NE;
Anand Shukla854c3022002-02-26 19:02:16 +000040 ++NI){
41 Graph::nodeList node_list=g.getNodeList(*NI);
42 for(Graph::nodeList::iterator NLI=node_list.begin(), NLE=node_list.end();
43 NLI!=NLE; ++NLI){
Anand Shukla21906892002-06-25 21:14:58 +000044 Edge f(*NI, NLI->element,NLI->weight, NLI->randId);
Anand Shukla854c3022002-02-26 19:02:16 +000045 if(!(st.hasEdgeAndWt(f)))//addnl
46 chords.push_back(f);
47 }
48 }
49}
50
51//Given a tree t, and a "directed graph" g
52//replace the edges in the tree t with edges that exist in graph
53//The tree is formed from "undirectional" copy of graph
54//So whatever edges the tree has, the undirectional graph
55//would have too. This function corrects some of the directions in
56//the tree so that now, all edge directions in the tree match
57//the edge directions of corresponding edges in the directed graph
Chris Lattner570b8e12002-02-26 19:49:45 +000058static void removeTreeEdges(Graph &g, Graph& t){
Anand Shukla21906892002-06-25 21:14:58 +000059 vector<Node* > allNodes=t.getAllNodes();
60 for(vector<Node *>::iterator NI=allNodes.begin(), NE=allNodes.end(); NI!=NE;
Anand Shukla854c3022002-02-26 19:02:16 +000061 ++NI){
62 Graph::nodeList nl=t.getNodeList(*NI);
63 for(Graph::nodeList::iterator NLI=nl.begin(), NLE=nl.end(); NLI!=NLE;++NLI){
64 Edge ed(NLI->element, *NI, NLI->weight);
Anand Shukla854c3022002-02-26 19:02:16 +000065 if(!g.hasEdgeAndWt(ed)) t.removeEdge(ed);//tree has only one edge
66 //between any pair of vertices, so no need to delete by edge wt
67 }
68 }
69}
70
71//Assign a value to all the edges in the graph
72//such that if we traverse along any path from root to exit, and
73//add up the edge values, we get a path number that uniquely
74//refers to the path we travelled
Anand Shuklaf94ad682002-09-16 05:26:51 +000075int valueAssignmentToEdges(Graph& g, map<Node *, int> nodePriority,
76 vector<Edge> &be){
Anand Shukla21906892002-06-25 21:14:58 +000077 vector<Node *> revtop=g.reverseTopologicalSort();
Anand Shukla854c3022002-02-26 19:02:16 +000078 map<Node *,int > NumPaths;
Anand Shukla2d3d20b2002-07-08 19:37:06 +000079 for(vector<Node *>::iterator RI=revtop.begin(), RE=revtop.end();
80 RI!=RE; ++RI){
Anand Shukla854c3022002-02-26 19:02:16 +000081 if(g.isLeaf(*RI))
82 NumPaths[*RI]=1;
83 else{
84 NumPaths[*RI]=0;
Anand Shukla2d3d20b2002-07-08 19:37:06 +000085
Anand Shuklaf94ad682002-09-16 05:26:51 +000086 // Modified Graph::nodeList &nlist=g.getNodeList(*RI);
87 Graph::nodeList &nlist=g.getSortedNodeList(*RI, be);
88
Anand Shukla21906892002-06-25 21:14:58 +000089 //sort nodelist by increasing order of numpaths
90
91 int sz=nlist.size();
Anand Shukla2d3d20b2002-07-08 19:37:06 +000092
Anand Shukla21906892002-06-25 21:14:58 +000093 for(int i=0;i<sz-1; i++){
94 int min=i;
Anand Shukla2d3d20b2002-07-08 19:37:06 +000095 for(int j=i+1; j<sz; j++){
96 BasicBlock *bb1 = nlist[j].element->getElement();
97 BasicBlock *bb2 = nlist[min].element->getElement();
Anand Shuklafd61c602002-07-18 20:56:47 +000098
99 if(bb1 == bb2) continue;
100
101 if(*RI == g.getRoot()){
102 assert(nodePriority[nlist[min].element]!=
103 nodePriority[nlist[j].element]
104 && "priorities can't be same!");
105
106 if(nodePriority[nlist[j].element] <
107 nodePriority[nlist[min].element])
108 min = j;
109 }
Anand Shukla2d3d20b2002-07-08 19:37:06 +0000110
Anand Shuklafd61c602002-07-18 20:56:47 +0000111 else{
112 TerminatorInst *tti = (*RI)->getElement()->getTerminator();
Anand Shuklaf94ad682002-09-16 05:26:51 +0000113
Anand Shuklafd61c602002-07-18 20:56:47 +0000114 BranchInst *ti = cast<BranchInst>(tti);
115 assert(ti && "not a branch");
116 assert(ti->getNumSuccessors()==2 && "less successors!");
117
118 BasicBlock *tB = ti->getSuccessor(0);
119 BasicBlock *fB = ti->getSuccessor(1);
120
121 if(tB == bb1 || fB == bb2)
Anand Shukla2d3d20b2002-07-08 19:37:06 +0000122 min = j;
Anand Shukla2d3d20b2002-07-08 19:37:06 +0000123 }
124
125 }
Anand Shukla21906892002-06-25 21:14:58 +0000126 graphListElement tempEl=nlist[min];
127 nlist[min]=nlist[i];
128 nlist[i]=tempEl;
129 }
Anand Shukla2d3d20b2002-07-08 19:37:06 +0000130
Anand Shukla21906892002-06-25 21:14:58 +0000131 //sorted now!
Anand Shukla21906892002-06-25 21:14:58 +0000132 for(Graph::nodeList::iterator GLI=nlist.begin(), GLE=nlist.end();
133 GLI!=GLE; ++GLI){
Anand Shuklaf94ad682002-09-16 05:26:51 +0000134 GLI->weight=NumPaths[*RI];
Anand Shukla21906892002-06-25 21:14:58 +0000135 NumPaths[*RI]+=NumPaths[GLI->element];
Anand Shukla854c3022002-02-26 19:02:16 +0000136 }
137 }
138 }
139 return NumPaths[g.getRoot()];
140}
141
142//This is a helper function to get the edge increments
143//This is used in conjuntion with inc_DFS
144//to get the edge increments
145//Edge increment implies assigning a value to all the edges in the graph
146//such that if we traverse along any path from root to exit, and
147//add up the edge values, we get a path number that uniquely
148//refers to the path we travelled
149//inc_Dir tells whether 2 edges are in same, or in different directions
150//if same direction, return 1, else -1
Chris Lattner570b8e12002-02-26 19:49:45 +0000151static int inc_Dir(Edge e, Edge f){
Anand Shukla854c3022002-02-26 19:02:16 +0000152 if(e.isNull())
153 return 1;
154
155 //check that the edges must have atleast one common endpoint
156 assert(*(e.getFirst())==*(f.getFirst()) ||
157 *(e.getFirst())==*(f.getSecond()) ||
158 *(e.getSecond())==*(f.getFirst()) ||
159 *(e.getSecond())==*(f.getSecond()));
160
161 if(*(e.getFirst())==*(f.getSecond()) ||
162 *(e.getSecond())==*(f.getFirst()))
163 return 1;
164
165 return -1;
166}
167
Anand Shukla21906892002-06-25 21:14:58 +0000168
Anand Shukla854c3022002-02-26 19:02:16 +0000169//used for getting edge increments (read comments above in inc_Dir)
170//inc_DFS is a modification of DFS
Anand Shuklaf94ad682002-09-16 05:26:51 +0000171static void inc_DFS(Graph& g,Graph& t,map<Edge, int, EdgeCompare2>& Increment,
Anand Shukla854c3022002-02-26 19:02:16 +0000172 int events, Node *v, Edge e){
173
Anand Shukla21906892002-06-25 21:14:58 +0000174 vector<Node *> allNodes=t.getAllNodes();
175
Anand Shukla21906892002-06-25 21:14:58 +0000176 for(vector<Node *>::iterator NI=allNodes.begin(), NE=allNodes.end(); NI!=NE;
Anand Shukla854c3022002-02-26 19:02:16 +0000177 ++NI){
178 Graph::nodeList node_list=t.getNodeList(*NI);
179 for(Graph::nodeList::iterator NLI=node_list.begin(), NLE=node_list.end();
180 NLI!= NLE; ++NLI){
Anand Shukla21906892002-06-25 21:14:58 +0000181 Edge f(*NI, NLI->element,NLI->weight, NLI->randId);
Anand Shukla854c3022002-02-26 19:02:16 +0000182 if(!edgesEqual(f,e) && *v==*(f.getSecond())){
183 int dir_count=inc_Dir(e,f);
184 int wt=1*f.getWeight();
185 inc_DFS(g,t, Increment, dir_count*events+wt, f.getFirst(), f);
186 }
187 }
188 }
189
Anand Shukla21906892002-06-25 21:14:58 +0000190 for(vector<Node *>::iterator NI=allNodes.begin(), NE=allNodes.end(); NI!=NE;
Anand Shukla854c3022002-02-26 19:02:16 +0000191 ++NI){
192 Graph::nodeList node_list=t.getNodeList(*NI);
193 for(Graph::nodeList::iterator NLI=node_list.begin(), NLE=node_list.end();
194 NLI!=NLE; ++NLI){
Anand Shukla21906892002-06-25 21:14:58 +0000195 Edge f(*NI, NLI->element,NLI->weight, NLI->randId);
Anand Shukla854c3022002-02-26 19:02:16 +0000196 if(!edgesEqual(f,e) && *v==*(f.getFirst())){
197 int dir_count=inc_Dir(e,f);
Anand Shukla21906892002-06-25 21:14:58 +0000198 int wt=f.getWeight();
Anand Shukla854c3022002-02-26 19:02:16 +0000199 inc_DFS(g,t, Increment, dir_count*events+wt,
200 f.getSecond(), f);
201 }
202 }
203 }
204
205 allNodes=g.getAllNodes();
Anand Shukla21906892002-06-25 21:14:58 +0000206 for(vector<Node *>::iterator NI=allNodes.begin(), NE=allNodes.end(); NI!=NE;
Anand Shukla854c3022002-02-26 19:02:16 +0000207 ++NI){
208 Graph::nodeList node_list=g.getNodeList(*NI);
209 for(Graph::nodeList::iterator NLI=node_list.begin(), NLE=node_list.end();
210 NLI!=NLE; ++NLI){
Anand Shukla21906892002-06-25 21:14:58 +0000211 Edge f(*NI, NLI->element,NLI->weight, NLI->randId);
Anand Shukla854c3022002-02-26 19:02:16 +0000212 if(!(t.hasEdgeAndWt(f)) && (*v==*(f.getSecond()) ||
213 *v==*(f.getFirst()))){
214 int dir_count=inc_Dir(e,f);
215 Increment[f]+=dir_count*events;
216 }
217 }
218 }
219}
220
221//Now we select a subset of all edges
222//and assign them some values such that
223//if we consider just this subset, it still represents
224//the path sum along any path in the graph
Anand Shuklaf94ad682002-09-16 05:26:51 +0000225static map<Edge, int, EdgeCompare2> getEdgeIncrements(Graph& g, Graph& t,
226 vector<Edge> &be){
Anand Shukla854c3022002-02-26 19:02:16 +0000227 //get all edges in g-t
Anand Shuklaf94ad682002-09-16 05:26:51 +0000228 map<Edge, int, EdgeCompare2> Increment;
Anand Shukla854c3022002-02-26 19:02:16 +0000229
Anand Shukla21906892002-06-25 21:14:58 +0000230 vector<Node *> allNodes=g.getAllNodes();
Anand Shukla854c3022002-02-26 19:02:16 +0000231
Anand Shukla21906892002-06-25 21:14:58 +0000232 for(vector<Node *>::iterator NI=allNodes.begin(), NE=allNodes.end(); NI!=NE;
Anand Shukla854c3022002-02-26 19:02:16 +0000233 ++NI){
Anand Shuklaf94ad682002-09-16 05:26:51 +0000234 Graph::nodeList node_list=g.getSortedNodeList(*NI, be);
235 //modified g.getNodeList(*NI);
Anand Shukla854c3022002-02-26 19:02:16 +0000236 for(Graph::nodeList::iterator NLI=node_list.begin(), NLE=node_list.end();
237 NLI!=NLE; ++NLI){
Anand Shukla21906892002-06-25 21:14:58 +0000238 Edge ed(*NI, NLI->element,NLI->weight,NLI->randId);
239 if(!(t.hasEdgeAndWt(ed))){
Anand Shukla854c3022002-02-26 19:02:16 +0000240 Increment[ed]=0;;
241 }
242 }
243 }
244
245 Edge *ed=new Edge();
246 inc_DFS(g,t,Increment, 0, g.getRoot(), *ed);
247
Anand Shukla21906892002-06-25 21:14:58 +0000248 for(vector<Node *>::iterator NI=allNodes.begin(), NE=allNodes.end(); NI!=NE;
Anand Shukla854c3022002-02-26 19:02:16 +0000249 ++NI){
Anand Shuklaf94ad682002-09-16 05:26:51 +0000250 Graph::nodeList node_list=g.getSortedNodeList(*NI, be);
251 //modified g.getNodeList(*NI);
Anand Shukla854c3022002-02-26 19:02:16 +0000252 for(Graph::nodeList::iterator NLI=node_list.begin(), NLE=node_list.end();
253 NLI!=NLE; ++NLI){
Anand Shukla21906892002-06-25 21:14:58 +0000254 Edge ed(*NI, NLI->element,NLI->weight, NLI->randId);
255 if(!(t.hasEdgeAndWt(ed))){
Anand Shukla854c3022002-02-26 19:02:16 +0000256 int wt=ed.getWeight();
257 Increment[ed]+=wt;
258 }
259 }
260 }
261
262 return Increment;
263}
264
Anand Shukla21906892002-06-25 21:14:58 +0000265//push it up: TODO
266const graphListElement *findNodeInList(const Graph::nodeList &NL,
267 Node *N);
268
269graphListElement *findNodeInList(Graph::nodeList &NL, Node *N);
270//end TODO
271
Anand Shukla854c3022002-02-26 19:02:16 +0000272//Based on edgeIncrements (above), now obtain
273//the kind of code to be inserted along an edge
274//The idea here is to minimize the computation
275//by inserting only the needed code
Anand Shuklaf94ad682002-09-16 05:26:51 +0000276static void getCodeInsertions(Graph &g, map<Edge, getEdgeCode *, EdgeCompare2> &instr,
Chris Lattner5328c6f2002-02-26 19:40:28 +0000277 vector<Edge > &chords,
Anand Shuklaf94ad682002-09-16 05:26:51 +0000278 map<Edge,int, EdgeCompare2> &edIncrements){
Anand Shukla854c3022002-02-26 19:02:16 +0000279
280 //Register initialization code
281 vector<Node *> ws;
282 ws.push_back(g.getRoot());
283 while(ws.size()>0){
Chris Lattner5328c6f2002-02-26 19:40:28 +0000284 Node *v=ws.back();
285 ws.pop_back();
Anand Shukla854c3022002-02-26 19:02:16 +0000286 //for each edge v->w
287 Graph::nodeList succs=g.getNodeList(v);
288
289 for(Graph::nodeList::iterator nl=succs.begin(), ne=succs.end();
290 nl!=ne; ++nl){
291 int edgeWt=nl->weight;
292 Node *w=nl->element;
293 //if chords has v->w
Anand Shukla21906892002-06-25 21:14:58 +0000294 Edge ed(v,w, edgeWt, nl->randId);
Anand Shukla854c3022002-02-26 19:02:16 +0000295 bool hasEdge=false;
296 for(vector<Edge>::iterator CI=chords.begin(), CE=chords.end();
297 CI!=CE && !hasEdge;++CI){
Anand Shukla21906892002-06-25 21:14:58 +0000298 if(*CI==ed && CI->getWeight()==edgeWt){//modf
Anand Shukla854c3022002-02-26 19:02:16 +0000299 hasEdge=true;
300 }
301 }
Anand Shukla21906892002-06-25 21:14:58 +0000302
303 if(hasEdge){//so its a chord edge
Anand Shukla854c3022002-02-26 19:02:16 +0000304 getEdgeCode *edCd=new getEdgeCode();
305 edCd->setCond(1);
306 edCd->setInc(edIncrements[ed]);
Chris Lattner5328c6f2002-02-26 19:40:28 +0000307 instr[ed]=edCd;
Anand Shukla854c3022002-02-26 19:02:16 +0000308 }
Anand Shukla21906892002-06-25 21:14:58 +0000309 else if(g.getNumberOfIncomingEdges(w)==1){
Anand Shukla854c3022002-02-26 19:02:16 +0000310 ws.push_back(w);
311 }
312 else{
313 getEdgeCode *edCd=new getEdgeCode();
314 edCd->setCond(2);
315 edCd->setInc(0);
Chris Lattner5328c6f2002-02-26 19:40:28 +0000316 instr[ed]=edCd;
Anand Shukla854c3022002-02-26 19:02:16 +0000317 }
318 }
319 }
320
321 /////Memory increment code
322 ws.push_back(g.getExit());
323
Chris Lattner5328c6f2002-02-26 19:40:28 +0000324 while(!ws.empty()) {
325 Node *w=ws.back();
326 ws.pop_back();
Anand Shukla21906892002-06-25 21:14:58 +0000327
328
329 ///////
330 //vector<Node *> lt;
331 vector<Node *> lllt=g.getAllNodes();
332 for(vector<Node *>::iterator EII=lllt.begin(); EII!=lllt.end() ;++EII){
333 Node *lnode=*EII;
334 Graph::nodeList &nl = g.getNodeList(lnode);
Anand Shuklaf94ad682002-09-16 05:26:51 +0000335 //graphListElement *N = findNodeInList(nl, w);
336 for(Graph::nodeList::const_iterator N = nl.begin(),
337 NNEN = nl.end(); N!= NNEN; ++N){
338 if (*N->element == *w){
339 Node *v=lnode;
340
341 //if chords has v->w
342 Edge ed(v,w, N->weight, N->randId);
343 getEdgeCode *edCd=new getEdgeCode();
344 bool hasEdge=false;
345 for(vector<Edge>::iterator CI=chords.begin(), CE=chords.end(); CI!=CE;
346 ++CI){
347 if(*CI==ed && CI->getWeight()==N->weight){
348 hasEdge=true;
349 break;
350 }
351 }
352 if(hasEdge){
353 //char str[100];
354 if(instr[ed]!=NULL && instr[ed]->getCond()==1){
355 instr[ed]->setCond(4);
356 }
357 else{
358 edCd->setCond(5);
359 edCd->setInc(edIncrements[ed]);
360 instr[ed]=edCd;
361 }
362
363 }
364 else if(g.getNumberOfOutgoingEdges(v)==1)
365 ws.push_back(v);
366 else{
367 edCd->setCond(6);
368 instr[ed]=edCd;
369 }
370 }
Anand Shukla854c3022002-02-26 19:02:16 +0000371 }
372 }
373 }
Anand Shukla854c3022002-02-26 19:02:16 +0000374 ///// Register increment code
375 for(vector<Edge>::iterator CI=chords.begin(), CE=chords.end(); CI!=CE; ++CI){
376 getEdgeCode *edCd=new getEdgeCode();
Chris Lattner5328c6f2002-02-26 19:40:28 +0000377 if(instr[*CI]==NULL){
378 edCd->setCond(3);
379 edCd->setInc(edIncrements[*CI]);
380 instr[*CI]=edCd;
381 }
Anand Shukla854c3022002-02-26 19:02:16 +0000382 }
Anand Shukla854c3022002-02-26 19:02:16 +0000383}
384
385//Add dummy edges corresponding to the back edges
386//If a->b is a backedge
387//then incoming dummy edge is root->b
388//and outgoing dummy edge is a->exit
Anand Shukla21906892002-06-25 21:14:58 +0000389//changed
Anand Shukla854c3022002-02-26 19:02:16 +0000390void addDummyEdges(vector<Edge > &stDummy,
391 vector<Edge > &exDummy,
Chris Lattner5328c6f2002-02-26 19:40:28 +0000392 Graph &g, vector<Edge> &be){
Anand Shukla854c3022002-02-26 19:02:16 +0000393 for(vector<Edge >::iterator VI=be.begin(), VE=be.end(); VI!=VE; ++VI){
394 Edge ed=*VI;
395 Node *first=ed.getFirst();
396 Node *second=ed.getSecond();
397 g.removeEdge(ed);
398
399 if(!(*second==*(g.getRoot()))){
Anand Shukla21906892002-06-25 21:14:58 +0000400 Edge *st=new Edge(g.getRoot(), second, ed.getWeight(), ed.getRandId());
401 stDummy.push_back(*st);
Chris Lattner5328c6f2002-02-26 19:40:28 +0000402 g.addEdgeForce(*st);
Anand Shukla854c3022002-02-26 19:02:16 +0000403 }
404
405 if(!(*first==*(g.getExit()))){
Anand Shukla21906892002-06-25 21:14:58 +0000406 Edge *ex=new Edge(first, g.getExit(), ed.getWeight(), ed.getRandId());
407 exDummy.push_back(*ex);
408 g.addEdgeForce(*ex);
Anand Shukla854c3022002-02-26 19:02:16 +0000409 }
410 }
411}
412
413//print a given edge in the form BB1Label->BB2Label
414void printEdge(Edge ed){
415 cerr<<((ed.getFirst())->getElement())
416 ->getName()<<"->"<<((ed.getSecond())
417 ->getElement())->getName()<<
Anand Shukla21906892002-06-25 21:14:58 +0000418 ":"<<ed.getWeight()<<" rndId::"<<ed.getRandId()<<"\n";
Anand Shukla854c3022002-02-26 19:02:16 +0000419}
420
421//Move the incoming dummy edge code and outgoing dummy
422//edge code over to the corresponding back edge
Anand Shukla21906892002-06-25 21:14:58 +0000423static void moveDummyCode(vector<Edge> &stDummy,
424 vector<Edge> &exDummy,
425 vector<Edge> &be,
Anand Shuklaf94ad682002-09-16 05:26:51 +0000426 map<Edge, getEdgeCode *, EdgeCompare2> &insertions,
Anand Shukla21906892002-06-25 21:14:58 +0000427 Graph &g){
428 typedef vector<Edge >::iterator vec_iter;
Anand Shukla854c3022002-02-26 19:02:16 +0000429
Anand Shuklaf94ad682002-09-16 05:26:51 +0000430 map<Edge,getEdgeCode *, EdgeCompare2> temp;
Anand Shukla21906892002-06-25 21:14:58 +0000431 //iterate over edges with code
Chris Lattner5328c6f2002-02-26 19:40:28 +0000432 std::vector<Edge> toErase;
Anand Shuklaf94ad682002-09-16 05:26:51 +0000433 for(map<Edge,getEdgeCode *, EdgeCompare2>::iterator MI=insertions.begin(),
Anand Shukla854c3022002-02-26 19:02:16 +0000434 ME=insertions.end(); MI!=ME; ++MI){
435 Edge ed=MI->first;
436 getEdgeCode *edCd=MI->second;
Anand Shukla21906892002-06-25 21:14:58 +0000437
438 ///---new code
439 //iterate over be, and check if its starts and end vertices hv code
440 for(vector<Edge>::iterator BEI=be.begin(), BEE=be.end(); BEI!=BEE; ++BEI){
441 if(ed.getRandId()==BEI->getRandId()){
442
Anand Shukla21906892002-06-25 21:14:58 +0000443 if(temp[*BEI]==0)
444 temp[*BEI]=new getEdgeCode();
445
446 //so ed is either in st, or ex!
447 if(ed.getFirst()==g.getRoot()){
Anand Shukla2d3d20b2002-07-08 19:37:06 +0000448
Anand Shukla21906892002-06-25 21:14:58 +0000449 //so its in stDummy
450 temp[*BEI]->setCdIn(edCd);
451 toErase.push_back(ed);
452 }
453 else if(ed.getSecond()==g.getExit()){
Anand Shukla2d3d20b2002-07-08 19:37:06 +0000454
Anand Shukla21906892002-06-25 21:14:58 +0000455 //so its in exDummy
456 toErase.push_back(ed);
457 temp[*BEI]->setCdOut(edCd);
458 }
459 else{
460 assert(false && "Not found in either start or end! Rand failed?");
461 }
462 }
463 }
464 }
465
466 for(vector<Edge >::iterator vmi=toErase.begin(), vme=toErase.end(); vmi!=vme;
467 ++vmi){
468 insertions.erase(*vmi);
Anand Shukla21906892002-06-25 21:14:58 +0000469 g.removeEdgeWithWt(*vmi);
470 }
471
Anand Shuklaf94ad682002-09-16 05:26:51 +0000472 for(map<Edge,getEdgeCode *, EdgeCompare2>::iterator MI=temp.begin(),
Anand Shukla21906892002-06-25 21:14:58 +0000473 ME=temp.end(); MI!=ME; ++MI){
474 insertions[MI->first]=MI->second;
Anand Shukla21906892002-06-25 21:14:58 +0000475 }
Anand Shukla2d3d20b2002-07-08 19:37:06 +0000476
Anand Shukla21906892002-06-25 21:14:58 +0000477#ifdef DEBUG_PATH_PROFILES
478 cerr<<"size of deletions: "<<toErase.size()<<"\n";
Anand Shukla21906892002-06-25 21:14:58 +0000479 cerr<<"SIZE OF INSERTIONS AFTER DEL "<<insertions.size()<<"\n";
480#endif
Anand Shukla854c3022002-02-26 19:02:16 +0000481
Anand Shukla854c3022002-02-26 19:02:16 +0000482}
483
Chris Lattner18ff9452002-02-26 19:43:49 +0000484//Do graph processing: to determine minimal edge increments,
485//appropriate code insertions etc and insert the code at
486//appropriate locations
487void processGraph(Graph &g,
488 Instruction *rInst,
489 Instruction *countInst,
490 vector<Edge >& be,
491 vector<Edge >& stDummy,
Anand Shukla21906892002-06-25 21:14:58 +0000492 vector<Edge >& exDummy,
Anand Shuklafd61c602002-07-18 20:56:47 +0000493 int numPaths, int MethNo){
Anand Shukla21906892002-06-25 21:14:58 +0000494
Chris Lattner18ff9452002-02-26 19:43:49 +0000495 //Given a graph: with exit->root edge, do the following in seq:
496 //1. get back edges
497 //2. insert dummy edges and remove back edges
498 //3. get edge assignments
499 //4. Get Max spanning tree of graph:
500 // -Make graph g2=g undirectional
501 // -Get Max spanning tree t
502 // -Make t undirectional
503 // -remove edges from t not in graph g
504 //5. Get edge increments
505 //6. Get code insertions
506 //7. move code on dummy edges over to the back edges
507
508
509 //This is used as maximum "weight" for
510 //priority queue
511 //This would hold all
512 //right as long as number of paths in the graph
513 //is less than this
514 const int INFINITY=99999999;
515
516
517 //step 1-3 are already done on the graph when this function is called
Chris Lattnere1fc2d92002-05-22 21:56:32 +0000518 DEBUG(printGraph(g));
519
Chris Lattner18ff9452002-02-26 19:43:49 +0000520 //step 4: Get Max spanning tree of graph
521
522 //now insert exit to root edge
523 //if its there earlier, remove it!
524 //assign it weight INFINITY
525 //so that this edge IS ALWAYS IN spanning tree
526 //Note than edges in spanning tree do not get
527 //instrumented: and we do not want the
528 //edge exit->root to get instrumented
529 //as it MAY BE a dummy edge
530 Edge ed(g.getExit(),g.getRoot(),INFINITY);
531 g.addEdge(ed,INFINITY);
532 Graph g2=g;
533
534 //make g2 undirectional: this gives a better
535 //maximal spanning tree
536 g2.makeUnDirectional();
Chris Lattnere1fc2d92002-05-22 21:56:32 +0000537 DEBUG(printGraph(g2));
538
Chris Lattner18ff9452002-02-26 19:43:49 +0000539 Graph *t=g2.getMaxSpanningTree();
Anand Shukla2d3d20b2002-07-08 19:37:06 +0000540#ifdef DEBUG_PATH_PROFILES
541 std::cerr<<"Original maxspanning tree\n";
542 printGraph(*t);
543#endif
Chris Lattner18ff9452002-02-26 19:43:49 +0000544 //now edges of tree t have weights reversed
545 //(negative) because the algorithm used
546 //to find max spanning tree is
547 //actually for finding min spanning tree
548 //so get back the original weights
549 t->reverseWts();
550
551 //Ordinarily, the graph is directional
552 //lets converts the graph into an
553 //undirectional graph
554 //This is done by adding an edge
555 //v->u for all existing edges u->v
556 t->makeUnDirectional();
557
558 //Given a tree t, and a "directed graph" g
559 //replace the edges in the tree t with edges that exist in graph
560 //The tree is formed from "undirectional" copy of graph
561 //So whatever edges the tree has, the undirectional graph
562 //would have too. This function corrects some of the directions in
563 //the tree so that now, all edge directions in the tree match
564 //the edge directions of corresponding edges in the directed graph
565 removeTreeEdges(g, *t);
Chris Lattnere1fc2d92002-05-22 21:56:32 +0000566
Anand Shukla21906892002-06-25 21:14:58 +0000567#ifdef DEBUG_PATH_PROFILES
568 cerr<<"Final Spanning tree---------\n";
569 printGraph(*t);
570 cerr<<"-------end spanning tree\n";
571#endif
Chris Lattnere1fc2d92002-05-22 21:56:32 +0000572
Chris Lattner18ff9452002-02-26 19:43:49 +0000573 //now remove the exit->root node
574 //and re-add it with weight 0
575 //since infinite weight is kinda confusing
576 g.removeEdge(ed);
577 Edge edNew(g.getExit(), g.getRoot(),0);
578 g.addEdge(edNew,0);
579 if(t->hasEdge(ed)){
580 t->removeEdge(ed);
581 t->addEdge(edNew,0);
582 }
583
Chris Lattnere1fc2d92002-05-22 21:56:32 +0000584 DEBUG(printGraph(g);
585 printGraph(*t));
586
Chris Lattner18ff9452002-02-26 19:43:49 +0000587 //step 5: Get edge increments
588
589 //Now we select a subset of all edges
590 //and assign them some values such that
591 //if we consider just this subset, it still represents
592 //the path sum along any path in the graph
Chris Lattnere1fc2d92002-05-22 21:56:32 +0000593
Anand Shuklaf94ad682002-09-16 05:26:51 +0000594 map<Edge, int, EdgeCompare2> increment=getEdgeIncrements(g,*t, be);
Anand Shukla21906892002-06-25 21:14:58 +0000595#ifdef DEBUG_PATH_PROFILES
596 //print edge increments for debugging
Anand Shuklaf94ad682002-09-16 05:26:51 +0000597 std::cerr<<"Edge Increments------\n";
598 for(map<Edge, int, EdgeCompare2>::iterator MMI=increment.begin(), MME=increment.end(); MMI != MME; ++MMI){
599 printEdge(MMI->first);
600 std::cerr<<"Increment for above:"<<MMI->second<<"\n";
Anand Shukla21906892002-06-25 21:14:58 +0000601 }
Anand Shuklaf94ad682002-09-16 05:26:51 +0000602 std::cerr<<"-------end of edge increments\n";
Anand Shukla21906892002-06-25 21:14:58 +0000603#endif
604
Chris Lattner18ff9452002-02-26 19:43:49 +0000605
606 //step 6: Get code insertions
607
608 //Based on edgeIncrements (above), now obtain
609 //the kind of code to be inserted along an edge
610 //The idea here is to minimize the computation
611 //by inserting only the needed code
612 vector<Edge> chords;
613 getChords(chords, g, *t);
614
Anand Shukla21906892002-06-25 21:14:58 +0000615
Anand Shuklaf94ad682002-09-16 05:26:51 +0000616 map<Edge, getEdgeCode *, EdgeCompare2> codeInsertions;
Chris Lattner18ff9452002-02-26 19:43:49 +0000617 getCodeInsertions(g, codeInsertions, chords,increment);
618
Anand Shukla21906892002-06-25 21:14:58 +0000619#ifdef DEBUG_PATH_PROFILES
620 //print edges with code for debugging
621 cerr<<"Code inserted in following---------------\n";
Anand Shuklaf94ad682002-09-16 05:26:51 +0000622 for(map<Edge, getEdgeCode *, EdgeCompare2>::iterator cd_i=codeInsertions.begin(),
Anand Shukla21906892002-06-25 21:14:58 +0000623 cd_e=codeInsertions.end(); cd_i!=cd_e; ++cd_i){
624 printEdge(cd_i->first);
625 cerr<<cd_i->second->getCond()<<":"<<cd_i->second->getInc()<<"\n";
626 }
627 cerr<<"-----end insertions\n";
628#endif
Chris Lattnere1fc2d92002-05-22 21:56:32 +0000629
Chris Lattner18ff9452002-02-26 19:43:49 +0000630 //step 7: move code on dummy edges over to the back edges
631
632 //Move the incoming dummy edge code and outgoing dummy
633 //edge code over to the corresponding back edge
Anand Shukla21906892002-06-25 21:14:58 +0000634
635 moveDummyCode(stDummy, exDummy, be, codeInsertions, g);
Anand Shukla2d3d20b2002-07-08 19:37:06 +0000636
Anand Shukla21906892002-06-25 21:14:58 +0000637#ifdef DEBUG_PATH_PROFILES
638 //debugging info
639 cerr<<"After moving dummy code\n";
640 for(map<Edge, getEdgeCode *>::iterator cd_i=codeInsertions.begin(),
641 cd_e=codeInsertions.end(); cd_i != cd_e; ++cd_i){
642 printEdge(cd_i->first);
643 cerr<<cd_i->second->getCond()<<":"
644 <<cd_i->second->getInc()<<"\n";
645 }
646 cerr<<"Dummy end------------\n";
647#endif
648
Chris Lattner18ff9452002-02-26 19:43:49 +0000649
650 //see what it looks like...
651 //now insert code along edges which have codes on them
652 for(map<Edge, getEdgeCode *>::iterator MI=codeInsertions.begin(),
653 ME=codeInsertions.end(); MI!=ME; ++MI){
654 Edge ed=MI->first;
Anand Shukla21906892002-06-25 21:14:58 +0000655 insertBB(ed, MI->second, rInst, countInst, numPaths, MethNo);
Chris Lattner18ff9452002-02-26 19:43:49 +0000656 }
657}
658
Anand Shukla854c3022002-02-26 19:02:16 +0000659//print the graph (for debugging)
Chris Lattner570b8e12002-02-26 19:49:45 +0000660void printGraph(Graph &g){
Anand Shukla21906892002-06-25 21:14:58 +0000661 vector<Node *> lt=g.getAllNodes();
Anand Shukla854c3022002-02-26 19:02:16 +0000662 cerr<<"Graph---------------------\n";
Anand Shukla21906892002-06-25 21:14:58 +0000663 for(vector<Node *>::iterator LI=lt.begin();
Anand Shukla854c3022002-02-26 19:02:16 +0000664 LI!=lt.end(); ++LI){
665 cerr<<((*LI)->getElement())->getName()<<"->";
666 Graph::nodeList nl=g.getNodeList(*LI);
667 for(Graph::nodeList::iterator NI=nl.begin();
668 NI!=nl.end(); ++NI){
669 cerr<<":"<<"("<<(NI->element->getElement())
Anand Shukla21906892002-06-25 21:14:58 +0000670 ->getName()<<":"<<NI->element->getWeight()<<","<<NI->weight<<","
671 <<NI->randId<<")";
Anand Shukla854c3022002-02-26 19:02:16 +0000672 }
673 cerr<<"\n";
674 }
675 cerr<<"--------------------Graph\n";
676}