blob: 76109822d15bd820e5bb4495ef4705bf6b0df1ee [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 Shukla2d3d20b2002-07-08 19:37:06 +000013#include "llvm/InstrTypes.h"
Anand Shuklafd61c602002-07-18 20:56:47 +000014#include "llvm/iTerminators.h"
Anand Shukla854c3022002-02-26 19:02:16 +000015#include <algorithm>
Chris Lattner5328c6f2002-02-26 19:40:28 +000016#include <iostream>
Anand Shukla2d3d20b2002-07-08 19:37:06 +000017#include <sstream>
Anand Shuklaf94ad682002-09-16 05:26:51 +000018#include <vector>
Chris Lattner5328c6f2002-02-26 19:40:28 +000019
Anand Shukla21906892002-06-25 21:14:58 +000020//using std::list;
Chris Lattner5328c6f2002-02-26 19:40:28 +000021using std::map;
22using std::vector;
23using std::cerr;
Anand Shukla854c3022002-02-26 19:02:16 +000024
25//check if 2 edges are equal (same endpoints and same weight)
Anand Shukla854c3022002-02-26 19:02:16 +000026static bool edgesEqual(Edge ed1, Edge ed2){
27 return ((ed1==ed2) && ed1.getWeight()==ed2.getWeight());
28}
29
30//Get the vector of edges that are to be instrumented in the graph
Chris Lattner570b8e12002-02-26 19:49:45 +000031static void getChords(vector<Edge > &chords, Graph &g, Graph st){
Anand Shukla854c3022002-02-26 19:02:16 +000032 //make sure the spanning tree is directional
33 //iterate over ALL the edges of the graph
Anand Shukla21906892002-06-25 21:14:58 +000034 vector<Node *> allNodes=g.getAllNodes();
35 for(vector<Node *>::iterator NI=allNodes.begin(), NE=allNodes.end(); NI!=NE;
Anand Shukla854c3022002-02-26 19:02:16 +000036 ++NI){
37 Graph::nodeList node_list=g.getNodeList(*NI);
38 for(Graph::nodeList::iterator NLI=node_list.begin(), NLE=node_list.end();
39 NLI!=NLE; ++NLI){
Anand Shukla21906892002-06-25 21:14:58 +000040 Edge f(*NI, NLI->element,NLI->weight, NLI->randId);
Anand Shukla854c3022002-02-26 19:02:16 +000041 if(!(st.hasEdgeAndWt(f)))//addnl
42 chords.push_back(f);
43 }
44 }
45}
46
47//Given a tree t, and a "directed graph" g
48//replace the edges in the tree t with edges that exist in graph
49//The tree is formed from "undirectional" copy of graph
50//So whatever edges the tree has, the undirectional graph
51//would have too. This function corrects some of the directions in
52//the tree so that now, all edge directions in the tree match
53//the edge directions of corresponding edges in the directed graph
Chris Lattner570b8e12002-02-26 19:49:45 +000054static void removeTreeEdges(Graph &g, Graph& t){
Anand Shukla21906892002-06-25 21:14:58 +000055 vector<Node* > allNodes=t.getAllNodes();
56 for(vector<Node *>::iterator NI=allNodes.begin(), NE=allNodes.end(); NI!=NE;
Anand Shukla854c3022002-02-26 19:02:16 +000057 ++NI){
58 Graph::nodeList nl=t.getNodeList(*NI);
59 for(Graph::nodeList::iterator NLI=nl.begin(), NLE=nl.end(); NLI!=NLE;++NLI){
60 Edge ed(NLI->element, *NI, NLI->weight);
Anand Shukla854c3022002-02-26 19:02:16 +000061 if(!g.hasEdgeAndWt(ed)) t.removeEdge(ed);//tree has only one edge
62 //between any pair of vertices, so no need to delete by edge wt
63 }
64 }
65}
66
67//Assign a value to all the edges in the graph
68//such that if we traverse along any path from root to exit, and
69//add up the edge values, we get a path number that uniquely
70//refers to the path we travelled
Anand Shuklaf94ad682002-09-16 05:26:51 +000071int valueAssignmentToEdges(Graph& g, map<Node *, int> nodePriority,
72 vector<Edge> &be){
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 Shuklaf94ad682002-09-16 05:26:51 +000082 // Modified Graph::nodeList &nlist=g.getNodeList(*RI);
83 Graph::nodeList &nlist=g.getSortedNodeList(*RI, be);
84
Anand Shukla21906892002-06-25 21:14:58 +000085 //sort nodelist by increasing order of numpaths
86
87 int sz=nlist.size();
Anand Shukla2d3d20b2002-07-08 19:37:06 +000088
Anand Shukla21906892002-06-25 21:14:58 +000089 for(int i=0;i<sz-1; i++){
90 int min=i;
Anand Shukla2d3d20b2002-07-08 19:37:06 +000091 for(int j=i+1; j<sz; j++){
92 BasicBlock *bb1 = nlist[j].element->getElement();
93 BasicBlock *bb2 = nlist[min].element->getElement();
Anand Shuklafd61c602002-07-18 20:56:47 +000094
95 if(bb1 == bb2) continue;
96
97 if(*RI == g.getRoot()){
98 assert(nodePriority[nlist[min].element]!=
99 nodePriority[nlist[j].element]
100 && "priorities can't be same!");
101
102 if(nodePriority[nlist[j].element] <
103 nodePriority[nlist[min].element])
104 min = j;
105 }
Anand Shukla2d3d20b2002-07-08 19:37:06 +0000106
Anand Shuklafd61c602002-07-18 20:56:47 +0000107 else{
108 TerminatorInst *tti = (*RI)->getElement()->getTerminator();
Anand Shuklaf94ad682002-09-16 05:26:51 +0000109
Anand Shuklafd61c602002-07-18 20:56:47 +0000110 BranchInst *ti = cast<BranchInst>(tti);
111 assert(ti && "not a branch");
112 assert(ti->getNumSuccessors()==2 && "less successors!");
113
114 BasicBlock *tB = ti->getSuccessor(0);
115 BasicBlock *fB = ti->getSuccessor(1);
116
117 if(tB == bb1 || fB == bb2)
Anand Shukla2d3d20b2002-07-08 19:37:06 +0000118 min = j;
Anand Shukla2d3d20b2002-07-08 19:37:06 +0000119 }
120
121 }
Anand Shukla21906892002-06-25 21:14:58 +0000122 graphListElement tempEl=nlist[min];
123 nlist[min]=nlist[i];
124 nlist[i]=tempEl;
125 }
Anand Shukla2d3d20b2002-07-08 19:37:06 +0000126
Anand Shukla21906892002-06-25 21:14:58 +0000127 //sorted now!
Anand Shukla21906892002-06-25 21:14:58 +0000128 for(Graph::nodeList::iterator GLI=nlist.begin(), GLE=nlist.end();
129 GLI!=GLE; ++GLI){
Anand Shuklaf94ad682002-09-16 05:26:51 +0000130 GLI->weight=NumPaths[*RI];
Anand Shukla21906892002-06-25 21:14:58 +0000131 NumPaths[*RI]+=NumPaths[GLI->element];
Anand Shukla854c3022002-02-26 19:02:16 +0000132 }
133 }
134 }
135 return NumPaths[g.getRoot()];
136}
137
138//This is a helper function to get the edge increments
139//This is used in conjuntion with inc_DFS
140//to get the edge increments
141//Edge increment implies assigning a value to all the edges in the graph
142//such that if we traverse along any path from root to exit, and
143//add up the edge values, we get a path number that uniquely
144//refers to the path we travelled
145//inc_Dir tells whether 2 edges are in same, or in different directions
146//if same direction, return 1, else -1
Chris Lattner570b8e12002-02-26 19:49:45 +0000147static int inc_Dir(Edge e, Edge f){
Anand Shukla854c3022002-02-26 19:02:16 +0000148 if(e.isNull())
149 return 1;
150
151 //check that the edges must have atleast one common endpoint
152 assert(*(e.getFirst())==*(f.getFirst()) ||
153 *(e.getFirst())==*(f.getSecond()) ||
154 *(e.getSecond())==*(f.getFirst()) ||
155 *(e.getSecond())==*(f.getSecond()));
156
157 if(*(e.getFirst())==*(f.getSecond()) ||
158 *(e.getSecond())==*(f.getFirst()))
159 return 1;
160
161 return -1;
162}
163
Anand Shukla21906892002-06-25 21:14:58 +0000164
Anand Shukla854c3022002-02-26 19:02:16 +0000165//used for getting edge increments (read comments above in inc_Dir)
166//inc_DFS is a modification of DFS
Anand Shuklaf94ad682002-09-16 05:26:51 +0000167static void inc_DFS(Graph& g,Graph& t,map<Edge, int, EdgeCompare2>& Increment,
Anand Shukla854c3022002-02-26 19:02:16 +0000168 int events, Node *v, Edge e){
169
Anand Shukla21906892002-06-25 21:14:58 +0000170 vector<Node *> allNodes=t.getAllNodes();
171
Anand Shukla21906892002-06-25 21:14:58 +0000172 for(vector<Node *>::iterator NI=allNodes.begin(), NE=allNodes.end(); NI!=NE;
Anand Shukla854c3022002-02-26 19:02:16 +0000173 ++NI){
174 Graph::nodeList node_list=t.getNodeList(*NI);
175 for(Graph::nodeList::iterator NLI=node_list.begin(), NLE=node_list.end();
176 NLI!= NLE; ++NLI){
Anand Shukla21906892002-06-25 21:14:58 +0000177 Edge f(*NI, NLI->element,NLI->weight, NLI->randId);
Anand Shukla854c3022002-02-26 19:02:16 +0000178 if(!edgesEqual(f,e) && *v==*(f.getSecond())){
179 int dir_count=inc_Dir(e,f);
180 int wt=1*f.getWeight();
181 inc_DFS(g,t, Increment, dir_count*events+wt, f.getFirst(), f);
182 }
183 }
184 }
185
Anand Shukla21906892002-06-25 21:14:58 +0000186 for(vector<Node *>::iterator NI=allNodes.begin(), NE=allNodes.end(); NI!=NE;
Anand Shukla854c3022002-02-26 19:02:16 +0000187 ++NI){
188 Graph::nodeList node_list=t.getNodeList(*NI);
189 for(Graph::nodeList::iterator NLI=node_list.begin(), NLE=node_list.end();
190 NLI!=NLE; ++NLI){
Anand Shukla21906892002-06-25 21:14:58 +0000191 Edge f(*NI, NLI->element,NLI->weight, NLI->randId);
Anand Shukla854c3022002-02-26 19:02:16 +0000192 if(!edgesEqual(f,e) && *v==*(f.getFirst())){
193 int dir_count=inc_Dir(e,f);
Anand Shukla21906892002-06-25 21:14:58 +0000194 int wt=f.getWeight();
Anand Shukla854c3022002-02-26 19:02:16 +0000195 inc_DFS(g,t, Increment, dir_count*events+wt,
196 f.getSecond(), f);
197 }
198 }
199 }
200
201 allNodes=g.getAllNodes();
Anand Shukla21906892002-06-25 21:14:58 +0000202 for(vector<Node *>::iterator NI=allNodes.begin(), NE=allNodes.end(); NI!=NE;
Anand Shukla854c3022002-02-26 19:02:16 +0000203 ++NI){
204 Graph::nodeList node_list=g.getNodeList(*NI);
205 for(Graph::nodeList::iterator NLI=node_list.begin(), NLE=node_list.end();
206 NLI!=NLE; ++NLI){
Anand Shukla21906892002-06-25 21:14:58 +0000207 Edge f(*NI, NLI->element,NLI->weight, NLI->randId);
Anand Shukla854c3022002-02-26 19:02:16 +0000208 if(!(t.hasEdgeAndWt(f)) && (*v==*(f.getSecond()) ||
209 *v==*(f.getFirst()))){
210 int dir_count=inc_Dir(e,f);
211 Increment[f]+=dir_count*events;
212 }
213 }
214 }
215}
216
217//Now we select a subset of all edges
218//and assign them some values such that
219//if we consider just this subset, it still represents
220//the path sum along any path in the graph
Anand Shuklaf94ad682002-09-16 05:26:51 +0000221static map<Edge, int, EdgeCompare2> getEdgeIncrements(Graph& g, Graph& t,
222 vector<Edge> &be){
Anand Shukla854c3022002-02-26 19:02:16 +0000223 //get all edges in g-t
Anand Shuklaf94ad682002-09-16 05:26:51 +0000224 map<Edge, int, EdgeCompare2> 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){
Anand Shuklaf94ad682002-09-16 05:26:51 +0000230 Graph::nodeList node_list=g.getSortedNodeList(*NI, be);
231 //modified g.getNodeList(*NI);
Anand Shukla854c3022002-02-26 19:02:16 +0000232 for(Graph::nodeList::iterator NLI=node_list.begin(), NLE=node_list.end();
233 NLI!=NLE; ++NLI){
Anand Shukla21906892002-06-25 21:14:58 +0000234 Edge ed(*NI, NLI->element,NLI->weight,NLI->randId);
235 if(!(t.hasEdgeAndWt(ed))){
Anand Shukla854c3022002-02-26 19:02:16 +0000236 Increment[ed]=0;;
237 }
238 }
239 }
240
241 Edge *ed=new Edge();
242 inc_DFS(g,t,Increment, 0, g.getRoot(), *ed);
243
Anand Shukla21906892002-06-25 21:14:58 +0000244 for(vector<Node *>::iterator NI=allNodes.begin(), NE=allNodes.end(); NI!=NE;
Anand Shukla854c3022002-02-26 19:02:16 +0000245 ++NI){
Anand Shuklaf94ad682002-09-16 05:26:51 +0000246 Graph::nodeList node_list=g.getSortedNodeList(*NI, be);
247 //modified g.getNodeList(*NI);
Anand Shukla854c3022002-02-26 19:02:16 +0000248 for(Graph::nodeList::iterator NLI=node_list.begin(), NLE=node_list.end();
249 NLI!=NLE; ++NLI){
Anand Shukla21906892002-06-25 21:14:58 +0000250 Edge ed(*NI, NLI->element,NLI->weight, NLI->randId);
251 if(!(t.hasEdgeAndWt(ed))){
Anand Shukla854c3022002-02-26 19:02:16 +0000252 int wt=ed.getWeight();
253 Increment[ed]+=wt;
254 }
255 }
256 }
257
258 return Increment;
259}
260
Anand Shukla21906892002-06-25 21:14:58 +0000261//push it up: TODO
262const graphListElement *findNodeInList(const Graph::nodeList &NL,
263 Node *N);
264
265graphListElement *findNodeInList(Graph::nodeList &NL, Node *N);
266//end TODO
267
Anand Shukla854c3022002-02-26 19:02:16 +0000268//Based on edgeIncrements (above), now obtain
269//the kind of code to be inserted along an edge
270//The idea here is to minimize the computation
271//by inserting only the needed code
Anand Shuklaf94ad682002-09-16 05:26:51 +0000272static void getCodeInsertions(Graph &g, map<Edge, getEdgeCode *, EdgeCompare2> &instr,
Chris Lattner5328c6f2002-02-26 19:40:28 +0000273 vector<Edge > &chords,
Anand Shuklaf94ad682002-09-16 05:26:51 +0000274 map<Edge,int, EdgeCompare2> &edIncrements){
Anand Shukla854c3022002-02-26 19:02:16 +0000275
276 //Register initialization code
277 vector<Node *> ws;
278 ws.push_back(g.getRoot());
279 while(ws.size()>0){
Chris Lattner5328c6f2002-02-26 19:40:28 +0000280 Node *v=ws.back();
281 ws.pop_back();
Anand Shukla854c3022002-02-26 19:02:16 +0000282 //for each edge v->w
283 Graph::nodeList succs=g.getNodeList(v);
284
285 for(Graph::nodeList::iterator nl=succs.begin(), ne=succs.end();
286 nl!=ne; ++nl){
287 int edgeWt=nl->weight;
288 Node *w=nl->element;
289 //if chords has v->w
Anand Shukla21906892002-06-25 21:14:58 +0000290 Edge ed(v,w, edgeWt, nl->randId);
Anand Shukla854c3022002-02-26 19:02:16 +0000291 bool hasEdge=false;
292 for(vector<Edge>::iterator CI=chords.begin(), CE=chords.end();
293 CI!=CE && !hasEdge;++CI){
Anand Shukla21906892002-06-25 21:14:58 +0000294 if(*CI==ed && CI->getWeight()==edgeWt){//modf
Anand Shukla854c3022002-02-26 19:02:16 +0000295 hasEdge=true;
296 }
297 }
Anand Shukla21906892002-06-25 21:14:58 +0000298
299 if(hasEdge){//so its a chord edge
Anand Shukla854c3022002-02-26 19:02:16 +0000300 getEdgeCode *edCd=new getEdgeCode();
301 edCd->setCond(1);
302 edCd->setInc(edIncrements[ed]);
Chris Lattner5328c6f2002-02-26 19:40:28 +0000303 instr[ed]=edCd;
Anand Shukla854c3022002-02-26 19:02:16 +0000304 }
Anand Shukla21906892002-06-25 21:14:58 +0000305 else if(g.getNumberOfIncomingEdges(w)==1){
Anand Shukla854c3022002-02-26 19:02:16 +0000306 ws.push_back(w);
307 }
308 else{
309 getEdgeCode *edCd=new getEdgeCode();
310 edCd->setCond(2);
311 edCd->setInc(0);
Chris Lattner5328c6f2002-02-26 19:40:28 +0000312 instr[ed]=edCd;
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 Shuklaf94ad682002-09-16 05:26:51 +0000331 //graphListElement *N = findNodeInList(nl, w);
332 for(Graph::nodeList::const_iterator N = nl.begin(),
333 NNEN = nl.end(); N!= NNEN; ++N){
334 if (*N->element == *w){
335 Node *v=lnode;
336
337 //if chords has v->w
338 Edge ed(v,w, N->weight, N->randId);
339 getEdgeCode *edCd=new getEdgeCode();
340 bool hasEdge=false;
341 for(vector<Edge>::iterator CI=chords.begin(), CE=chords.end(); CI!=CE;
342 ++CI){
343 if(*CI==ed && CI->getWeight()==N->weight){
344 hasEdge=true;
345 break;
346 }
347 }
348 if(hasEdge){
349 //char str[100];
350 if(instr[ed]!=NULL && instr[ed]->getCond()==1){
351 instr[ed]->setCond(4);
352 }
353 else{
354 edCd->setCond(5);
355 edCd->setInc(edIncrements[ed]);
356 instr[ed]=edCd;
357 }
358
359 }
360 else if(g.getNumberOfOutgoingEdges(v)==1)
361 ws.push_back(v);
362 else{
363 edCd->setCond(6);
364 instr[ed]=edCd;
365 }
366 }
Anand Shukla854c3022002-02-26 19:02:16 +0000367 }
368 }
369 }
Anand Shukla854c3022002-02-26 19:02:16 +0000370 ///// Register increment code
371 for(vector<Edge>::iterator CI=chords.begin(), CE=chords.end(); CI!=CE; ++CI){
372 getEdgeCode *edCd=new getEdgeCode();
Chris Lattner5328c6f2002-02-26 19:40:28 +0000373 if(instr[*CI]==NULL){
374 edCd->setCond(3);
375 edCd->setInc(edIncrements[*CI]);
376 instr[*CI]=edCd;
377 }
Anand Shukla854c3022002-02-26 19:02:16 +0000378 }
Anand Shukla854c3022002-02-26 19:02:16 +0000379}
380
381//Add dummy edges corresponding to the back edges
382//If a->b is a backedge
383//then incoming dummy edge is root->b
384//and outgoing dummy edge is a->exit
Anand Shukla21906892002-06-25 21:14:58 +0000385//changed
Anand Shukla854c3022002-02-26 19:02:16 +0000386void addDummyEdges(vector<Edge > &stDummy,
387 vector<Edge > &exDummy,
Chris Lattner5328c6f2002-02-26 19:40:28 +0000388 Graph &g, vector<Edge> &be){
Anand Shukla854c3022002-02-26 19:02:16 +0000389 for(vector<Edge >::iterator VI=be.begin(), VE=be.end(); VI!=VE; ++VI){
390 Edge ed=*VI;
391 Node *first=ed.getFirst();
392 Node *second=ed.getSecond();
393 g.removeEdge(ed);
394
395 if(!(*second==*(g.getRoot()))){
Anand Shukla21906892002-06-25 21:14:58 +0000396 Edge *st=new Edge(g.getRoot(), second, ed.getWeight(), ed.getRandId());
397 stDummy.push_back(*st);
Chris Lattner5328c6f2002-02-26 19:40:28 +0000398 g.addEdgeForce(*st);
Anand Shukla854c3022002-02-26 19:02:16 +0000399 }
400
401 if(!(*first==*(g.getExit()))){
Anand Shukla21906892002-06-25 21:14:58 +0000402 Edge *ex=new Edge(first, g.getExit(), ed.getWeight(), ed.getRandId());
403 exDummy.push_back(*ex);
404 g.addEdgeForce(*ex);
Anand Shukla854c3022002-02-26 19:02:16 +0000405 }
406 }
407}
408
409//print a given edge in the form BB1Label->BB2Label
410void printEdge(Edge ed){
411 cerr<<((ed.getFirst())->getElement())
412 ->getName()<<"->"<<((ed.getSecond())
413 ->getElement())->getName()<<
Anand Shukla21906892002-06-25 21:14:58 +0000414 ":"<<ed.getWeight()<<" rndId::"<<ed.getRandId()<<"\n";
Anand Shukla854c3022002-02-26 19:02:16 +0000415}
416
417//Move the incoming dummy edge code and outgoing dummy
418//edge code over to the corresponding back edge
Anand Shukla21906892002-06-25 21:14:58 +0000419static void moveDummyCode(vector<Edge> &stDummy,
420 vector<Edge> &exDummy,
421 vector<Edge> &be,
Anand Shuklaf94ad682002-09-16 05:26:51 +0000422 map<Edge, getEdgeCode *, EdgeCompare2> &insertions,
Anand Shukla21906892002-06-25 21:14:58 +0000423 Graph &g){
424 typedef vector<Edge >::iterator vec_iter;
Anand Shukla854c3022002-02-26 19:02:16 +0000425
Anand Shuklaf94ad682002-09-16 05:26:51 +0000426 map<Edge,getEdgeCode *, EdgeCompare2> temp;
Anand Shukla21906892002-06-25 21:14:58 +0000427 //iterate over edges with code
Chris Lattner5328c6f2002-02-26 19:40:28 +0000428 std::vector<Edge> toErase;
Anand Shuklaf94ad682002-09-16 05:26:51 +0000429 for(map<Edge,getEdgeCode *, EdgeCompare2>::iterator MI=insertions.begin(),
Anand Shukla854c3022002-02-26 19:02:16 +0000430 ME=insertions.end(); MI!=ME; ++MI){
431 Edge ed=MI->first;
432 getEdgeCode *edCd=MI->second;
Anand Shukla21906892002-06-25 21:14:58 +0000433
434 ///---new code
435 //iterate over be, and check if its starts and end vertices hv code
436 for(vector<Edge>::iterator BEI=be.begin(), BEE=be.end(); BEI!=BEE; ++BEI){
437 if(ed.getRandId()==BEI->getRandId()){
438
Anand Shukla21906892002-06-25 21:14:58 +0000439 if(temp[*BEI]==0)
440 temp[*BEI]=new getEdgeCode();
441
442 //so ed is either in st, or ex!
443 if(ed.getFirst()==g.getRoot()){
Anand Shukla2d3d20b2002-07-08 19:37:06 +0000444
Anand Shukla21906892002-06-25 21:14:58 +0000445 //so its in stDummy
446 temp[*BEI]->setCdIn(edCd);
447 toErase.push_back(ed);
448 }
449 else if(ed.getSecond()==g.getExit()){
Anand Shukla2d3d20b2002-07-08 19:37:06 +0000450
Anand Shukla21906892002-06-25 21:14:58 +0000451 //so its in exDummy
452 toErase.push_back(ed);
453 temp[*BEI]->setCdOut(edCd);
454 }
455 else{
456 assert(false && "Not found in either start or end! Rand failed?");
457 }
458 }
459 }
460 }
461
462 for(vector<Edge >::iterator vmi=toErase.begin(), vme=toErase.end(); vmi!=vme;
463 ++vmi){
464 insertions.erase(*vmi);
Anand Shukla21906892002-06-25 21:14:58 +0000465 g.removeEdgeWithWt(*vmi);
466 }
467
Anand Shuklaf94ad682002-09-16 05:26:51 +0000468 for(map<Edge,getEdgeCode *, EdgeCompare2>::iterator MI=temp.begin(),
Anand Shukla21906892002-06-25 21:14:58 +0000469 ME=temp.end(); MI!=ME; ++MI){
470 insertions[MI->first]=MI->second;
Anand Shukla21906892002-06-25 21:14:58 +0000471 }
Anand Shukla2d3d20b2002-07-08 19:37:06 +0000472
Anand Shukla21906892002-06-25 21:14:58 +0000473#ifdef DEBUG_PATH_PROFILES
474 cerr<<"size of deletions: "<<toErase.size()<<"\n";
Anand Shukla21906892002-06-25 21:14:58 +0000475 cerr<<"SIZE OF INSERTIONS AFTER DEL "<<insertions.size()<<"\n";
476#endif
Anand Shukla854c3022002-02-26 19:02:16 +0000477
Anand Shukla854c3022002-02-26 19:02:16 +0000478}
479
Chris Lattner18ff9452002-02-26 19:43:49 +0000480//Do graph processing: to determine minimal edge increments,
481//appropriate code insertions etc and insert the code at
482//appropriate locations
483void processGraph(Graph &g,
484 Instruction *rInst,
485 Instruction *countInst,
486 vector<Edge >& be,
487 vector<Edge >& stDummy,
Anand Shukla21906892002-06-25 21:14:58 +0000488 vector<Edge >& exDummy,
Anand Shukla77dca142002-09-20 16:44:35 +0000489 int numPaths, int MethNo,
490 Value *threshold){
Anand Shukla21906892002-06-25 21:14:58 +0000491
Chris Lattner18ff9452002-02-26 19:43:49 +0000492 //Given a graph: with exit->root edge, do the following in seq:
493 //1. get back edges
494 //2. insert dummy edges and remove back edges
495 //3. get edge assignments
496 //4. Get Max spanning tree of graph:
497 // -Make graph g2=g undirectional
498 // -Get Max spanning tree t
499 // -Make t undirectional
500 // -remove edges from t not in graph g
501 //5. Get edge increments
502 //6. Get code insertions
503 //7. move code on dummy edges over to the back edges
504
505
506 //This is used as maximum "weight" for
507 //priority queue
508 //This would hold all
509 //right as long as number of paths in the graph
510 //is less than this
Chris Lattner22cbac62002-09-17 23:46:33 +0000511 const int Infinity=99999999;
Chris Lattner18ff9452002-02-26 19:43:49 +0000512
513
514 //step 1-3 are already done on the graph when this function is called
Chris Lattnere1fc2d92002-05-22 21:56:32 +0000515 DEBUG(printGraph(g));
516
Chris Lattner18ff9452002-02-26 19:43:49 +0000517 //step 4: Get Max spanning tree of graph
518
519 //now insert exit to root edge
520 //if its there earlier, remove it!
Chris Lattner22cbac62002-09-17 23:46:33 +0000521 //assign it weight Infinity
Chris Lattner18ff9452002-02-26 19:43:49 +0000522 //so that this edge IS ALWAYS IN spanning tree
523 //Note than edges in spanning tree do not get
524 //instrumented: and we do not want the
525 //edge exit->root to get instrumented
526 //as it MAY BE a dummy edge
Chris Lattner22cbac62002-09-17 23:46:33 +0000527 Edge ed(g.getExit(),g.getRoot(),Infinity);
528 g.addEdge(ed,Infinity);
Chris Lattner18ff9452002-02-26 19:43:49 +0000529 Graph g2=g;
530
531 //make g2 undirectional: this gives a better
532 //maximal spanning tree
533 g2.makeUnDirectional();
Chris Lattnere1fc2d92002-05-22 21:56:32 +0000534 DEBUG(printGraph(g2));
535
Chris Lattner18ff9452002-02-26 19:43:49 +0000536 Graph *t=g2.getMaxSpanningTree();
Anand Shukla2d3d20b2002-07-08 19:37:06 +0000537#ifdef DEBUG_PATH_PROFILES
538 std::cerr<<"Original maxspanning tree\n";
539 printGraph(*t);
540#endif
Chris Lattner18ff9452002-02-26 19:43:49 +0000541 //now edges of tree t have weights reversed
542 //(negative) because the algorithm used
543 //to find max spanning tree is
544 //actually for finding min spanning tree
545 //so get back the original weights
546 t->reverseWts();
547
548 //Ordinarily, the graph is directional
549 //lets converts the graph into an
550 //undirectional graph
551 //This is done by adding an edge
552 //v->u for all existing edges u->v
553 t->makeUnDirectional();
554
555 //Given a tree t, and a "directed graph" g
556 //replace the edges in the tree t with edges that exist in graph
557 //The tree is formed from "undirectional" copy of graph
558 //So whatever edges the tree has, the undirectional graph
559 //would have too. This function corrects some of the directions in
560 //the tree so that now, all edge directions in the tree match
561 //the edge directions of corresponding edges in the directed graph
562 removeTreeEdges(g, *t);
Chris Lattnere1fc2d92002-05-22 21:56:32 +0000563
Anand Shukla21906892002-06-25 21:14:58 +0000564#ifdef DEBUG_PATH_PROFILES
565 cerr<<"Final Spanning tree---------\n";
566 printGraph(*t);
567 cerr<<"-------end spanning tree\n";
568#endif
Chris Lattnere1fc2d92002-05-22 21:56:32 +0000569
Chris Lattner18ff9452002-02-26 19:43:49 +0000570 //now remove the exit->root node
571 //and re-add it with weight 0
572 //since infinite weight is kinda confusing
573 g.removeEdge(ed);
574 Edge edNew(g.getExit(), g.getRoot(),0);
575 g.addEdge(edNew,0);
576 if(t->hasEdge(ed)){
577 t->removeEdge(ed);
578 t->addEdge(edNew,0);
579 }
580
Chris Lattnere1fc2d92002-05-22 21:56:32 +0000581 DEBUG(printGraph(g);
582 printGraph(*t));
583
Chris Lattner18ff9452002-02-26 19:43:49 +0000584 //step 5: Get edge increments
585
586 //Now we select a subset of all edges
587 //and assign them some values such that
588 //if we consider just this subset, it still represents
589 //the path sum along any path in the graph
Chris Lattnere1fc2d92002-05-22 21:56:32 +0000590
Anand Shuklaf94ad682002-09-16 05:26:51 +0000591 map<Edge, int, EdgeCompare2> increment=getEdgeIncrements(g,*t, be);
Anand Shukla21906892002-06-25 21:14:58 +0000592#ifdef DEBUG_PATH_PROFILES
593 //print edge increments for debugging
Anand Shuklaf94ad682002-09-16 05:26:51 +0000594 std::cerr<<"Edge Increments------\n";
595 for(map<Edge, int, EdgeCompare2>::iterator MMI=increment.begin(), MME=increment.end(); MMI != MME; ++MMI){
596 printEdge(MMI->first);
597 std::cerr<<"Increment for above:"<<MMI->second<<"\n";
Anand Shukla21906892002-06-25 21:14:58 +0000598 }
Anand Shuklaf94ad682002-09-16 05:26:51 +0000599 std::cerr<<"-------end of edge increments\n";
Anand Shukla21906892002-06-25 21:14:58 +0000600#endif
601
Chris Lattner18ff9452002-02-26 19:43:49 +0000602
603 //step 6: Get code insertions
604
605 //Based on edgeIncrements (above), now obtain
606 //the kind of code to be inserted along an edge
607 //The idea here is to minimize the computation
608 //by inserting only the needed code
609 vector<Edge> chords;
610 getChords(chords, g, *t);
611
Anand Shukla21906892002-06-25 21:14:58 +0000612
Anand Shuklaf94ad682002-09-16 05:26:51 +0000613 map<Edge, getEdgeCode *, EdgeCompare2> codeInsertions;
Chris Lattner18ff9452002-02-26 19:43:49 +0000614 getCodeInsertions(g, codeInsertions, chords,increment);
615
Anand Shukla21906892002-06-25 21:14:58 +0000616#ifdef DEBUG_PATH_PROFILES
617 //print edges with code for debugging
618 cerr<<"Code inserted in following---------------\n";
Anand Shuklaf94ad682002-09-16 05:26:51 +0000619 for(map<Edge, getEdgeCode *, EdgeCompare2>::iterator cd_i=codeInsertions.begin(),
Anand Shukla21906892002-06-25 21:14:58 +0000620 cd_e=codeInsertions.end(); cd_i!=cd_e; ++cd_i){
621 printEdge(cd_i->first);
622 cerr<<cd_i->second->getCond()<<":"<<cd_i->second->getInc()<<"\n";
623 }
624 cerr<<"-----end insertions\n";
625#endif
Chris Lattnere1fc2d92002-05-22 21:56:32 +0000626
Chris Lattner18ff9452002-02-26 19:43:49 +0000627 //step 7: move code on dummy edges over to the back edges
628
629 //Move the incoming dummy edge code and outgoing dummy
630 //edge code over to the corresponding back edge
Anand Shukla21906892002-06-25 21:14:58 +0000631
632 moveDummyCode(stDummy, exDummy, be, codeInsertions, g);
Anand Shukla2d3d20b2002-07-08 19:37:06 +0000633
Anand Shukla21906892002-06-25 21:14:58 +0000634#ifdef DEBUG_PATH_PROFILES
635 //debugging info
636 cerr<<"After moving dummy code\n";
637 for(map<Edge, getEdgeCode *>::iterator cd_i=codeInsertions.begin(),
638 cd_e=codeInsertions.end(); cd_i != cd_e; ++cd_i){
639 printEdge(cd_i->first);
640 cerr<<cd_i->second->getCond()<<":"
641 <<cd_i->second->getInc()<<"\n";
642 }
643 cerr<<"Dummy end------------\n";
644#endif
645
Chris Lattner18ff9452002-02-26 19:43:49 +0000646
647 //see what it looks like...
648 //now insert code along edges which have codes on them
649 for(map<Edge, getEdgeCode *>::iterator MI=codeInsertions.begin(),
650 ME=codeInsertions.end(); MI!=ME; ++MI){
651 Edge ed=MI->first;
Anand Shukla77dca142002-09-20 16:44:35 +0000652 insertBB(ed, MI->second, rInst, countInst, numPaths, MethNo, threshold);
Chris Lattner18ff9452002-02-26 19:43:49 +0000653 }
654}
655
Anand Shukla854c3022002-02-26 19:02:16 +0000656//print the graph (for debugging)
Chris Lattner570b8e12002-02-26 19:49:45 +0000657void printGraph(Graph &g){
Anand Shukla21906892002-06-25 21:14:58 +0000658 vector<Node *> lt=g.getAllNodes();
Anand Shukla854c3022002-02-26 19:02:16 +0000659 cerr<<"Graph---------------------\n";
Anand Shukla21906892002-06-25 21:14:58 +0000660 for(vector<Node *>::iterator LI=lt.begin();
Anand Shukla854c3022002-02-26 19:02:16 +0000661 LI!=lt.end(); ++LI){
662 cerr<<((*LI)->getElement())->getName()<<"->";
663 Graph::nodeList nl=g.getNodeList(*LI);
664 for(Graph::nodeList::iterator NI=nl.begin();
665 NI!=nl.end(); ++NI){
666 cerr<<":"<<"("<<(NI->element->getElement())
Anand Shukla21906892002-06-25 21:14:58 +0000667 ->getName()<<":"<<NI->element->getWeight()<<","<<NI->weight<<","
668 <<NI->randId<<")";
Anand Shukla854c3022002-02-26 19:02:16 +0000669 }
670 cerr<<"\n";
671 }
672 cerr<<"--------------------Graph\n";
673}