blob: 37dd6ae5d6be240229cbf6b35a3d0816cb1dc1a5 [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"
10#include "llvm/Function.h"
11#include "llvm/Pass.h"
Anand Shukla854c3022002-02-26 19:02:16 +000012#include "llvm/BasicBlock.h"
Anand Shukla2d3d20b2002-07-08 19:37:06 +000013#include "llvm/InstrTypes.h"
Anand Shukla21906892002-06-25 21:14:58 +000014#include "llvm/Transforms/Instrumentation/Graph.h"
Anand Shuklafd61c602002-07-18 20:56:47 +000015#include "llvm/iTerminators.h"
Anand Shukla854c3022002-02-26 19:02:16 +000016#include <algorithm>
Chris Lattner5328c6f2002-02-26 19:40:28 +000017#include <iostream>
Anand Shukla2d3d20b2002-07-08 19:37:06 +000018#include <sstream>
19#include <string>
Chris Lattner5328c6f2002-02-26 19:40:28 +000020
Anand Shukla21906892002-06-25 21:14:58 +000021//using std::list;
Chris Lattner5328c6f2002-02-26 19:40:28 +000022using std::map;
23using std::vector;
24using std::cerr;
Anand Shukla854c3022002-02-26 19:02:16 +000025
26//check if 2 edges are equal (same endpoints and same weight)
Anand Shukla854c3022002-02-26 19:02:16 +000027static bool edgesEqual(Edge ed1, Edge ed2){
28 return ((ed1==ed2) && ed1.getWeight()==ed2.getWeight());
29}
30
31//Get the vector of edges that are to be instrumented in the graph
Chris Lattner570b8e12002-02-26 19:49:45 +000032static void getChords(vector<Edge > &chords, Graph &g, Graph st){
Anand Shukla854c3022002-02-26 19:02:16 +000033 //make sure the spanning tree is directional
34 //iterate over ALL the edges of the graph
Anand Shukla21906892002-06-25 21:14:58 +000035 vector<Node *> allNodes=g.getAllNodes();
36 for(vector<Node *>::iterator NI=allNodes.begin(), NE=allNodes.end(); NI!=NE;
Anand Shukla854c3022002-02-26 19:02:16 +000037 ++NI){
38 Graph::nodeList node_list=g.getNodeList(*NI);
39 for(Graph::nodeList::iterator NLI=node_list.begin(), NLE=node_list.end();
40 NLI!=NLE; ++NLI){
Anand Shukla21906892002-06-25 21:14:58 +000041 Edge f(*NI, NLI->element,NLI->weight, NLI->randId);
Anand Shukla854c3022002-02-26 19:02:16 +000042 if(!(st.hasEdgeAndWt(f)))//addnl
43 chords.push_back(f);
44 }
45 }
46}
47
48//Given a tree t, and a "directed graph" g
49//replace the edges in the tree t with edges that exist in graph
50//The tree is formed from "undirectional" copy of graph
51//So whatever edges the tree has, the undirectional graph
52//would have too. This function corrects some of the directions in
53//the tree so that now, all edge directions in the tree match
54//the edge directions of corresponding edges in the directed graph
Chris Lattner570b8e12002-02-26 19:49:45 +000055static void removeTreeEdges(Graph &g, Graph& t){
Anand Shukla21906892002-06-25 21:14:58 +000056 vector<Node* > allNodes=t.getAllNodes();
57 for(vector<Node *>::iterator NI=allNodes.begin(), NE=allNodes.end(); NI!=NE;
Anand Shukla854c3022002-02-26 19:02:16 +000058 ++NI){
59 Graph::nodeList nl=t.getNodeList(*NI);
60 for(Graph::nodeList::iterator NLI=nl.begin(), NLE=nl.end(); NLI!=NLE;++NLI){
61 Edge ed(NLI->element, *NI, NLI->weight);
Anand Shukla854c3022002-02-26 19:02:16 +000062 if(!g.hasEdgeAndWt(ed)) t.removeEdge(ed);//tree has only one edge
63 //between any pair of vertices, so no need to delete by edge wt
64 }
65 }
66}
67
68//Assign a value to all the edges in the graph
69//such that if we traverse along any path from root to exit, and
70//add up the edge values, we get a path number that uniquely
71//refers to the path we travelled
Anand Shuklafd61c602002-07-18 20:56:47 +000072int valueAssignmentToEdges(Graph& g, map<Node *, int> nodePriority){
Anand Shukla21906892002-06-25 21:14:58 +000073 vector<Node *> revtop=g.reverseTopologicalSort();
Anand Shukla854c3022002-02-26 19:02:16 +000074 map<Node *,int > NumPaths;
Anand Shukla2d3d20b2002-07-08 19:37:06 +000075 for(vector<Node *>::iterator RI=revtop.begin(), RE=revtop.end();
76 RI!=RE; ++RI){
Anand Shukla854c3022002-02-26 19:02:16 +000077 if(g.isLeaf(*RI))
78 NumPaths[*RI]=1;
79 else{
80 NumPaths[*RI]=0;
Anand Shukla2d3d20b2002-07-08 19:37:06 +000081
Anand Shukla21906892002-06-25 21:14:58 +000082 Graph::nodeList &nlist=g.getNodeList(*RI);
83 //sort nodelist by increasing order of numpaths
84
85 int sz=nlist.size();
Anand Shukla2d3d20b2002-07-08 19:37:06 +000086
Anand Shukla21906892002-06-25 21:14:58 +000087 for(int i=0;i<sz-1; i++){
88 int min=i;
Anand Shukla2d3d20b2002-07-08 19:37:06 +000089 for(int j=i+1; j<sz; j++){
90 BasicBlock *bb1 = nlist[j].element->getElement();
91 BasicBlock *bb2 = nlist[min].element->getElement();
Anand Shuklafd61c602002-07-18 20:56:47 +000092
93 if(bb1 == bb2) continue;
94
95 if(*RI == g.getRoot()){
96 assert(nodePriority[nlist[min].element]!=
97 nodePriority[nlist[j].element]
98 && "priorities can't be same!");
99
100 if(nodePriority[nlist[j].element] <
101 nodePriority[nlist[min].element])
102 min = j;
103 }
Anand Shukla2d3d20b2002-07-08 19:37:06 +0000104
Anand Shuklafd61c602002-07-18 20:56:47 +0000105 else{
106 TerminatorInst *tti = (*RI)->getElement()->getTerminator();
107 //std::cerr<<*tti<<std::endl;
108 BranchInst *ti = cast<BranchInst>(tti);
109 assert(ti && "not a branch");
110 assert(ti->getNumSuccessors()==2 && "less successors!");
111
112 BasicBlock *tB = ti->getSuccessor(0);
113 BasicBlock *fB = ti->getSuccessor(1);
114
115 if(tB == bb1 || fB == bb2)
Anand Shukla2d3d20b2002-07-08 19:37:06 +0000116 min = j;
Anand Shukla2d3d20b2002-07-08 19:37:06 +0000117 }
118
119 }
Anand Shukla21906892002-06-25 21:14:58 +0000120 graphListElement tempEl=nlist[min];
121 nlist[min]=nlist[i];
122 nlist[i]=tempEl;
123 }
Anand Shukla2d3d20b2002-07-08 19:37:06 +0000124
Anand Shukla21906892002-06-25 21:14:58 +0000125 //sorted now!
Anand Shuklafd61c602002-07-18 20:56:47 +0000126 //std::cerr<<"Considering Order-----\n";
Anand Shukla21906892002-06-25 21:14:58 +0000127 for(Graph::nodeList::iterator GLI=nlist.begin(), GLE=nlist.end();
128 GLI!=GLE; ++GLI){
Anand Shuklafd61c602002-07-18 20:56:47 +0000129 //std::cerr<<GLI->element->getElement()->getName()<<"->";
Anand Shukla21906892002-06-25 21:14:58 +0000130 GLI->weight=NumPaths[*RI];
131 NumPaths[*RI]+=NumPaths[GLI->element];
Anand Shukla854c3022002-02-26 19:02:16 +0000132 }
Anand Shuklafd61c602002-07-18 20:56:47 +0000133 //std::cerr<<"\nend order $$$$$$$$$$$$$$$$$$$$$$$$\n";
Anand Shukla854c3022002-02-26 19:02:16 +0000134 }
135 }
136 return NumPaths[g.getRoot()];
137}
138
139//This is a helper function to get the edge increments
140//This is used in conjuntion with inc_DFS
141//to get the edge increments
142//Edge increment implies assigning a value to all the edges in the graph
143//such that if we traverse along any path from root to exit, and
144//add up the edge values, we get a path number that uniquely
145//refers to the path we travelled
146//inc_Dir tells whether 2 edges are in same, or in different directions
147//if same direction, return 1, else -1
Chris Lattner570b8e12002-02-26 19:49:45 +0000148static int inc_Dir(Edge e, Edge f){
Anand Shukla854c3022002-02-26 19:02:16 +0000149 if(e.isNull())
150 return 1;
151
152 //check that the edges must have atleast one common endpoint
153 assert(*(e.getFirst())==*(f.getFirst()) ||
154 *(e.getFirst())==*(f.getSecond()) ||
155 *(e.getSecond())==*(f.getFirst()) ||
156 *(e.getSecond())==*(f.getSecond()));
157
158 if(*(e.getFirst())==*(f.getSecond()) ||
159 *(e.getSecond())==*(f.getFirst()))
160 return 1;
161
162 return -1;
163}
164
Anand Shukla21906892002-06-25 21:14:58 +0000165
Anand Shukla854c3022002-02-26 19:02:16 +0000166//used for getting edge increments (read comments above in inc_Dir)
167//inc_DFS is a modification of DFS
Anand Shukla21906892002-06-25 21:14:58 +0000168static void inc_DFS(Graph& g,Graph& t,map<Edge, int, EdgeCompare>& Increment,
Anand Shukla854c3022002-02-26 19:02:16 +0000169 int events, Node *v, Edge e){
170
Anand Shukla21906892002-06-25 21:14:58 +0000171 vector<Node *> allNodes=t.getAllNodes();
172
Anand Shukla21906892002-06-25 21:14:58 +0000173 for(vector<Node *>::iterator NI=allNodes.begin(), NE=allNodes.end(); NI!=NE;
Anand Shukla854c3022002-02-26 19:02:16 +0000174 ++NI){
175 Graph::nodeList node_list=t.getNodeList(*NI);
176 for(Graph::nodeList::iterator NLI=node_list.begin(), NLE=node_list.end();
177 NLI!= NLE; ++NLI){
Anand Shukla21906892002-06-25 21:14:58 +0000178 Edge f(*NI, NLI->element,NLI->weight, NLI->randId);
Anand Shukla854c3022002-02-26 19:02:16 +0000179 if(!edgesEqual(f,e) && *v==*(f.getSecond())){
180 int dir_count=inc_Dir(e,f);
181 int wt=1*f.getWeight();
182 inc_DFS(g,t, Increment, dir_count*events+wt, f.getFirst(), f);
183 }
184 }
185 }
186
Anand Shukla21906892002-06-25 21:14:58 +0000187 for(vector<Node *>::iterator NI=allNodes.begin(), NE=allNodes.end(); NI!=NE;
Anand Shukla854c3022002-02-26 19:02:16 +0000188 ++NI){
189 Graph::nodeList node_list=t.getNodeList(*NI);
190 for(Graph::nodeList::iterator NLI=node_list.begin(), NLE=node_list.end();
191 NLI!=NLE; ++NLI){
Anand Shukla21906892002-06-25 21:14:58 +0000192 Edge f(*NI, NLI->element,NLI->weight, NLI->randId);
Anand Shukla854c3022002-02-26 19:02:16 +0000193 if(!edgesEqual(f,e) && *v==*(f.getFirst())){
194 int dir_count=inc_Dir(e,f);
Anand Shukla21906892002-06-25 21:14:58 +0000195 int wt=f.getWeight();
Anand Shukla854c3022002-02-26 19:02:16 +0000196 inc_DFS(g,t, Increment, dir_count*events+wt,
197 f.getSecond(), f);
198 }
199 }
200 }
201
202 allNodes=g.getAllNodes();
Anand Shukla21906892002-06-25 21:14:58 +0000203 for(vector<Node *>::iterator NI=allNodes.begin(), NE=allNodes.end(); NI!=NE;
Anand Shukla854c3022002-02-26 19:02:16 +0000204 ++NI){
205 Graph::nodeList node_list=g.getNodeList(*NI);
206 for(Graph::nodeList::iterator NLI=node_list.begin(), NLE=node_list.end();
207 NLI!=NLE; ++NLI){
Anand Shukla21906892002-06-25 21:14:58 +0000208 Edge f(*NI, NLI->element,NLI->weight, NLI->randId);
Anand Shukla854c3022002-02-26 19:02:16 +0000209 if(!(t.hasEdgeAndWt(f)) && (*v==*(f.getSecond()) ||
210 *v==*(f.getFirst()))){
211 int dir_count=inc_Dir(e,f);
212 Increment[f]+=dir_count*events;
213 }
214 }
215 }
216}
217
218//Now we select a subset of all edges
219//and assign them some values such that
220//if we consider just this subset, it still represents
221//the path sum along any path in the graph
Anand Shukla21906892002-06-25 21:14:58 +0000222static map<Edge, int, EdgeCompare> getEdgeIncrements(Graph& g, Graph& t){
Anand Shukla854c3022002-02-26 19:02:16 +0000223 //get all edges in g-t
Anand Shukla21906892002-06-25 21:14:58 +0000224 map<Edge, int, EdgeCompare> Increment;
Anand Shukla854c3022002-02-26 19:02:16 +0000225
Anand Shukla21906892002-06-25 21:14:58 +0000226 vector<Node *> allNodes=g.getAllNodes();
Anand Shukla854c3022002-02-26 19:02:16 +0000227
Anand Shukla21906892002-06-25 21:14:58 +0000228 for(vector<Node *>::iterator NI=allNodes.begin(), NE=allNodes.end(); NI!=NE;
Anand Shukla854c3022002-02-26 19:02:16 +0000229 ++NI){
230 Graph::nodeList node_list=g.getNodeList(*NI);
231 for(Graph::nodeList::iterator NLI=node_list.begin(), NLE=node_list.end();
232 NLI!=NLE; ++NLI){
Anand Shukla21906892002-06-25 21:14:58 +0000233 Edge ed(*NI, NLI->element,NLI->weight,NLI->randId);
234 if(!(t.hasEdgeAndWt(ed))){
Anand Shukla854c3022002-02-26 19:02:16 +0000235 Increment[ed]=0;;
236 }
237 }
238 }
239
240 Edge *ed=new Edge();
241 inc_DFS(g,t,Increment, 0, g.getRoot(), *ed);
242
Anand Shukla21906892002-06-25 21:14:58 +0000243 for(vector<Node *>::iterator NI=allNodes.begin(), NE=allNodes.end(); NI!=NE;
Anand Shukla854c3022002-02-26 19:02:16 +0000244 ++NI){
245 Graph::nodeList node_list=g.getNodeList(*NI);
246 for(Graph::nodeList::iterator NLI=node_list.begin(), NLE=node_list.end();
247 NLI!=NLE; ++NLI){
Anand Shukla21906892002-06-25 21:14:58 +0000248 Edge ed(*NI, NLI->element,NLI->weight, NLI->randId);
249 if(!(t.hasEdgeAndWt(ed))){
Anand Shukla854c3022002-02-26 19:02:16 +0000250 int wt=ed.getWeight();
251 Increment[ed]+=wt;
252 }
253 }
254 }
255
256 return Increment;
257}
258
Anand Shukla21906892002-06-25 21:14:58 +0000259//push it up: TODO
260const graphListElement *findNodeInList(const Graph::nodeList &NL,
261 Node *N);
262
263graphListElement *findNodeInList(Graph::nodeList &NL, Node *N);
264//end TODO
265
Anand Shukla854c3022002-02-26 19:02:16 +0000266//Based on edgeIncrements (above), now obtain
267//the kind of code to be inserted along an edge
268//The idea here is to minimize the computation
269//by inserting only the needed code
Anand Shukla21906892002-06-25 21:14:58 +0000270static void getCodeInsertions(Graph &g, map<Edge, getEdgeCode *, EdgeCompare> &instr,
Chris Lattner5328c6f2002-02-26 19:40:28 +0000271 vector<Edge > &chords,
Anand Shukla21906892002-06-25 21:14:58 +0000272 map<Edge,int, EdgeCompare> &edIncrements){
Anand Shukla854c3022002-02-26 19:02:16 +0000273
274 //Register initialization code
275 vector<Node *> ws;
276 ws.push_back(g.getRoot());
277 while(ws.size()>0){
Chris Lattner5328c6f2002-02-26 19:40:28 +0000278 Node *v=ws.back();
279 ws.pop_back();
Anand Shukla854c3022002-02-26 19:02:16 +0000280 //for each edge v->w
281 Graph::nodeList succs=g.getNodeList(v);
282
283 for(Graph::nodeList::iterator nl=succs.begin(), ne=succs.end();
284 nl!=ne; ++nl){
285 int edgeWt=nl->weight;
286 Node *w=nl->element;
287 //if chords has v->w
Anand Shukla21906892002-06-25 21:14:58 +0000288 Edge ed(v,w, edgeWt, nl->randId);
Anand Shukla854c3022002-02-26 19:02:16 +0000289 bool hasEdge=false;
290 for(vector<Edge>::iterator CI=chords.begin(), CE=chords.end();
291 CI!=CE && !hasEdge;++CI){
Anand Shukla21906892002-06-25 21:14:58 +0000292 if(*CI==ed && CI->getWeight()==edgeWt){//modf
Anand Shukla854c3022002-02-26 19:02:16 +0000293 hasEdge=true;
294 }
295 }
Anand Shukla21906892002-06-25 21:14:58 +0000296
297 if(hasEdge){//so its a chord edge
Anand Shukla854c3022002-02-26 19:02:16 +0000298 getEdgeCode *edCd=new getEdgeCode();
299 edCd->setCond(1);
300 edCd->setInc(edIncrements[ed]);
Chris Lattner5328c6f2002-02-26 19:40:28 +0000301 instr[ed]=edCd;
Anand Shukla854c3022002-02-26 19:02:16 +0000302 }
Anand Shukla21906892002-06-25 21:14:58 +0000303 else if(g.getNumberOfIncomingEdges(w)==1){
Anand Shukla854c3022002-02-26 19:02:16 +0000304 ws.push_back(w);
Anand Shukla21906892002-06-25 21:14:58 +0000305 //std::cerr<<"Added w\n";
Anand Shukla854c3022002-02-26 19:02:16 +0000306 }
307 else{
308 getEdgeCode *edCd=new getEdgeCode();
309 edCd->setCond(2);
310 edCd->setInc(0);
Chris Lattner5328c6f2002-02-26 19:40:28 +0000311 instr[ed]=edCd;
Anand Shukla21906892002-06-25 21:14:58 +0000312 //std::cerr<<"Case 2\n";
Anand Shukla854c3022002-02-26 19:02:16 +0000313 }
314 }
315 }
316
317 /////Memory increment code
318 ws.push_back(g.getExit());
319
Chris Lattner5328c6f2002-02-26 19:40:28 +0000320 while(!ws.empty()) {
321 Node *w=ws.back();
322 ws.pop_back();
Anand Shukla21906892002-06-25 21:14:58 +0000323
324
325 ///////
326 //vector<Node *> lt;
327 vector<Node *> lllt=g.getAllNodes();
328 for(vector<Node *>::iterator EII=lllt.begin(); EII!=lllt.end() ;++EII){
329 Node *lnode=*EII;
330 Graph::nodeList &nl = g.getNodeList(lnode);
Anand Shukla21906892002-06-25 21:14:58 +0000331 graphListElement *N = findNodeInList(nl, w);
Anand Shukla2d3d20b2002-07-08 19:37:06 +0000332 if (N){
Anand Shukla21906892002-06-25 21:14:58 +0000333 Node *v=lnode;
Anand Shukla2d3d20b2002-07-08 19:37:06 +0000334
Anand Shukla21906892002-06-25 21:14:58 +0000335 //if chords has v->w
Anand Shukla21906892002-06-25 21:14:58 +0000336 Edge ed(v,w, N->weight, N->randId);
337 getEdgeCode *edCd=new getEdgeCode();
338 bool hasEdge=false;
339 for(vector<Edge>::iterator CI=chords.begin(), CE=chords.end(); CI!=CE;
340 ++CI){
341 if(*CI==ed && CI->getWeight()==N->weight){
342 hasEdge=true;
343 break;
344 }
Anand Shukla854c3022002-02-26 19:02:16 +0000345 }
Anand Shukla21906892002-06-25 21:14:58 +0000346 if(hasEdge){
347 char str[100];
348 if(instr[ed]!=NULL && instr[ed]->getCond()==1){
349 instr[ed]->setCond(4);
350 }
351 else{
352 edCd->setCond(5);
353 edCd->setInc(edIncrements[ed]);
354 instr[ed]=edCd;
355 }
356
Anand Shukla854c3022002-02-26 19:02:16 +0000357 }
Anand Shukla21906892002-06-25 21:14:58 +0000358 else if(g.getNumberOfOutgoingEdges(v)==1)
359 ws.push_back(v);
Anand Shukla854c3022002-02-26 19:02:16 +0000360 else{
Anand Shukla21906892002-06-25 21:14:58 +0000361 edCd->setCond(6);
Chris Lattner5328c6f2002-02-26 19:40:28 +0000362 instr[ed]=edCd;
Anand Shukla854c3022002-02-26 19:02:16 +0000363 }
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,
419 map<Edge, getEdgeCode *, EdgeCompare> &insertions,
420 Graph &g){
421 typedef vector<Edge >::iterator vec_iter;
Anand Shukla854c3022002-02-26 19:02:16 +0000422
Anand Shukla21906892002-06-25 21:14:58 +0000423 map<Edge,getEdgeCode *, EdgeCompare> temp;
424 //iterate over edges with code
Chris Lattner5328c6f2002-02-26 19:40:28 +0000425 std::vector<Edge> toErase;
Anand Shukla21906892002-06-25 21:14:58 +0000426 for(map<Edge,getEdgeCode *, EdgeCompare>::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
465 for(map<Edge,getEdgeCode *, EdgeCompare>::iterator MI=temp.begin(),
466 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 Shuklafd61c602002-07-18 20:56:47 +0000486 int numPaths, int MethNo){
Anand Shukla21906892002-06-25 21:14:58 +0000487
Chris Lattner18ff9452002-02-26 19:43:49 +0000488 //Given a graph: with exit->root edge, do the following in seq:
489 //1. get back edges
490 //2. insert dummy edges and remove back edges
491 //3. get edge assignments
492 //4. Get Max spanning tree of graph:
493 // -Make graph g2=g undirectional
494 // -Get Max spanning tree t
495 // -Make t undirectional
496 // -remove edges from t not in graph g
497 //5. Get edge increments
498 //6. Get code insertions
499 //7. move code on dummy edges over to the back edges
500
501
502 //This is used as maximum "weight" for
503 //priority queue
504 //This would hold all
505 //right as long as number of paths in the graph
506 //is less than this
507 const int INFINITY=99999999;
508
509
510 //step 1-3 are already done on the graph when this function is called
Chris Lattnere1fc2d92002-05-22 21:56:32 +0000511 DEBUG(printGraph(g));
512
Chris Lattner18ff9452002-02-26 19:43:49 +0000513 //step 4: Get Max spanning tree of graph
514
515 //now insert exit to root edge
516 //if its there earlier, remove it!
517 //assign it weight INFINITY
518 //so that this edge IS ALWAYS IN spanning tree
519 //Note than edges in spanning tree do not get
520 //instrumented: and we do not want the
521 //edge exit->root to get instrumented
522 //as it MAY BE a dummy edge
523 Edge ed(g.getExit(),g.getRoot(),INFINITY);
524 g.addEdge(ed,INFINITY);
525 Graph g2=g;
526
527 //make g2 undirectional: this gives a better
528 //maximal spanning tree
529 g2.makeUnDirectional();
Chris Lattnere1fc2d92002-05-22 21:56:32 +0000530 DEBUG(printGraph(g2));
531
Chris Lattner18ff9452002-02-26 19:43:49 +0000532 Graph *t=g2.getMaxSpanningTree();
Anand Shukla2d3d20b2002-07-08 19:37:06 +0000533#ifdef DEBUG_PATH_PROFILES
534 std::cerr<<"Original maxspanning tree\n";
535 printGraph(*t);
536#endif
Chris Lattner18ff9452002-02-26 19:43:49 +0000537 //now edges of tree t have weights reversed
538 //(negative) because the algorithm used
539 //to find max spanning tree is
540 //actually for finding min spanning tree
541 //so get back the original weights
542 t->reverseWts();
543
544 //Ordinarily, the graph is directional
545 //lets converts the graph into an
546 //undirectional graph
547 //This is done by adding an edge
548 //v->u for all existing edges u->v
549 t->makeUnDirectional();
550
551 //Given a tree t, and a "directed graph" g
552 //replace the edges in the tree t with edges that exist in graph
553 //The tree is formed from "undirectional" copy of graph
554 //So whatever edges the tree has, the undirectional graph
555 //would have too. This function corrects some of the directions in
556 //the tree so that now, all edge directions in the tree match
557 //the edge directions of corresponding edges in the directed graph
558 removeTreeEdges(g, *t);
Chris Lattnere1fc2d92002-05-22 21:56:32 +0000559
Anand Shukla21906892002-06-25 21:14:58 +0000560#ifdef DEBUG_PATH_PROFILES
561 cerr<<"Final Spanning tree---------\n";
562 printGraph(*t);
563 cerr<<"-------end spanning tree\n";
564#endif
Chris Lattnere1fc2d92002-05-22 21:56:32 +0000565
Chris Lattner18ff9452002-02-26 19:43:49 +0000566 //now remove the exit->root node
567 //and re-add it with weight 0
568 //since infinite weight is kinda confusing
569 g.removeEdge(ed);
570 Edge edNew(g.getExit(), g.getRoot(),0);
571 g.addEdge(edNew,0);
572 if(t->hasEdge(ed)){
573 t->removeEdge(ed);
574 t->addEdge(edNew,0);
575 }
576
Chris Lattnere1fc2d92002-05-22 21:56:32 +0000577 DEBUG(printGraph(g);
578 printGraph(*t));
579
Chris Lattner18ff9452002-02-26 19:43:49 +0000580 //step 5: Get edge increments
581
582 //Now we select a subset of all edges
583 //and assign them some values such that
584 //if we consider just this subset, it still represents
585 //the path sum along any path in the graph
Chris Lattnere1fc2d92002-05-22 21:56:32 +0000586
Anand Shukla21906892002-06-25 21:14:58 +0000587 map<Edge, int, EdgeCompare> increment=getEdgeIncrements(g,*t);
588#ifdef DEBUG_PATH_PROFILES
589 //print edge increments for debugging
590
591 for(map<Edge, int, EdgeCompare>::iterator M_I=increment.begin(), M_E=increment.end();
592 M_I!=M_E; ++M_I){
593 printEdge(M_I->first);
594 cerr<<"Increment for above:"<<M_I->second<<"\n";
595 }
596#endif
597
Chris Lattner18ff9452002-02-26 19:43:49 +0000598
599 //step 6: Get code insertions
600
601 //Based on edgeIncrements (above), now obtain
602 //the kind of code to be inserted along an edge
603 //The idea here is to minimize the computation
604 //by inserting only the needed code
605 vector<Edge> chords;
606 getChords(chords, g, *t);
607
Anand Shukla21906892002-06-25 21:14:58 +0000608
609 //cerr<<"Graph before getCodeInsertion:\n";
610 //printGraph(g);
611 map<Edge, getEdgeCode *, EdgeCompare> codeInsertions;
Chris Lattner18ff9452002-02-26 19:43:49 +0000612 getCodeInsertions(g, codeInsertions, chords,increment);
613
Anand Shukla21906892002-06-25 21:14:58 +0000614#ifdef DEBUG_PATH_PROFILES
615 //print edges with code for debugging
616 cerr<<"Code inserted in following---------------\n";
617 for(map<Edge, getEdgeCode *>::iterator cd_i=codeInsertions.begin(),
618 cd_e=codeInsertions.end(); cd_i!=cd_e; ++cd_i){
619 printEdge(cd_i->first);
620 cerr<<cd_i->second->getCond()<<":"<<cd_i->second->getInc()<<"\n";
621 }
622 cerr<<"-----end insertions\n";
623#endif
Chris Lattnere1fc2d92002-05-22 21:56:32 +0000624
Chris Lattner18ff9452002-02-26 19:43:49 +0000625 //step 7: move code on dummy edges over to the back edges
626
627 //Move the incoming dummy edge code and outgoing dummy
628 //edge code over to the corresponding back edge
Anand Shukla21906892002-06-25 21:14:58 +0000629
630 moveDummyCode(stDummy, exDummy, be, codeInsertions, g);
Anand Shukla2d3d20b2002-07-08 19:37:06 +0000631
Anand Shukla21906892002-06-25 21:14:58 +0000632#ifdef DEBUG_PATH_PROFILES
633 //debugging info
634 cerr<<"After moving dummy code\n";
635 for(map<Edge, getEdgeCode *>::iterator cd_i=codeInsertions.begin(),
636 cd_e=codeInsertions.end(); cd_i != cd_e; ++cd_i){
637 printEdge(cd_i->first);
638 cerr<<cd_i->second->getCond()<<":"
639 <<cd_i->second->getInc()<<"\n";
640 }
641 cerr<<"Dummy end------------\n";
642#endif
643
Chris Lattner18ff9452002-02-26 19:43:49 +0000644
645 //see what it looks like...
646 //now insert code along edges which have codes on them
647 for(map<Edge, getEdgeCode *>::iterator MI=codeInsertions.begin(),
648 ME=codeInsertions.end(); MI!=ME; ++MI){
649 Edge ed=MI->first;
Anand Shukla21906892002-06-25 21:14:58 +0000650 insertBB(ed, MI->second, rInst, countInst, numPaths, MethNo);
Chris Lattner18ff9452002-02-26 19:43:49 +0000651 }
652}
653
Anand Shukla854c3022002-02-26 19:02:16 +0000654//print the graph (for debugging)
Chris Lattner570b8e12002-02-26 19:49:45 +0000655void printGraph(Graph &g){
Anand Shukla21906892002-06-25 21:14:58 +0000656 vector<Node *> lt=g.getAllNodes();
Anand Shukla854c3022002-02-26 19:02:16 +0000657 cerr<<"Graph---------------------\n";
Anand Shukla21906892002-06-25 21:14:58 +0000658 for(vector<Node *>::iterator LI=lt.begin();
Anand Shukla854c3022002-02-26 19:02:16 +0000659 LI!=lt.end(); ++LI){
660 cerr<<((*LI)->getElement())->getName()<<"->";
661 Graph::nodeList nl=g.getNodeList(*LI);
662 for(Graph::nodeList::iterator NI=nl.begin();
663 NI!=nl.end(); ++NI){
664 cerr<<":"<<"("<<(NI->element->getElement())
Anand Shukla21906892002-06-25 21:14:58 +0000665 ->getName()<<":"<<NI->element->getWeight()<<","<<NI->weight<<","
666 <<NI->randId<<")";
Anand Shukla854c3022002-02-26 19:02:16 +0000667 }
668 cerr<<"\n";
669 }
670 cerr<<"--------------------Graph\n";
671}