blob: 8eb2f724f5eed20bfecf4269fafdb583118aea59 [file] [log] [blame]
Anand Shukla8c1c8542002-06-25 14:28:55 +00001//===-- ------------------------llvm/graph.h ---------------------*- C++ -*--=//
2//
3//Header file for Graph: This Graph is used by
4//PathProfiles class, and is used
5//for detecting proper points in cfg for code insertion
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_GRAPH_H
10#define LLVM_GRAPH_H
11
12#include "Support/StatisticReporter.h"
13
14#include <map>
Anand Shukla8c1c8542002-06-25 14:28:55 +000015#include <vector>
16#include <cstdlib>
17
18#include "llvm/BasicBlock.h"
19
20class BasicBlock;
Anand Shukla8c1c8542002-06-25 14:28:55 +000021class Module;
Anand Shukla8c1c8542002-06-25 14:28:55 +000022class Function;
Anand Shukla8c1c8542002-06-25 14:28:55 +000023class Instruction;
24
25//Class Node
26//It forms the vertex for the graph
27class Node{
28public:
29 BasicBlock* element;
30 int weight;
31public:
32 inline Node(BasicBlock* x) { element=x; weight=0; }
33 inline BasicBlock* &getElement() { return element; }
34 inline BasicBlock* const &getElement() const { return element; }
35 inline int getWeight() { return weight; }
36 inline void setElement(BasicBlock* e) { element=e; }
37 inline void setWeight(int w) { weight=w;}
38 inline bool operator<(Node& nd) const { return element<nd.element; }
39 inline bool operator==(Node& nd) const { return element==nd.element; }
40};
Anand Shuklafd61c602002-07-18 20:56:47 +000041
Anand Shukla8c1c8542002-06-25 14:28:55 +000042
43//Class Edge
44//Denotes an edge in the graph
45class Edge{
46private:
47 Node *first;
48 Node *second;
49 bool isnull;
50 int weight;
51 double randId;
52public:
53 inline Edge(Node *f,Node *s, int wt=0){
54 first=f;
55 second=s;
56 weight=wt;
57 randId=rand();
58 isnull=false;
59 }
60
61 inline Edge(Node *f,Node *s, int wt, double rd){
62 first=f;
63 second=s;
64 weight=wt;
65 randId=rd;
66 isnull=false;
67 }
68
69 inline Edge() { isnull = true; }
70 inline double getRandId(){ return randId; }
71 inline Node* getFirst() { assert(!isNull()); return first; }
72 inline Node* const getFirst() const { assert(!isNull()); return first; }
73 inline Node* getSecond() { assert(!isNull()); return second; }
74 inline Node* const getSecond() const { assert(!isNull()); return second; }
75
76 inline int getWeight() { assert(!isNull()); return weight; }
77 inline void setWeight(int n) { assert(!isNull()); weight=n; }
78
79 inline void setFirst(Node *&f) { assert(!isNull()); first=f; }
80 inline void setSecond(Node *&s) { assert(!isNull()); second=s; }
81
82
83 inline bool isNull() const { return isnull;}
84
85 inline bool operator<(const Edge& ed) const{
86 // Can't be the same if one is null and the other isn't
87 if (isNull() != ed.isNull())
88 return true;
89
90 return (*first<*(ed.getFirst()))||
91 (*first==*(ed.getFirst()) && *second<*(ed.getSecond()));
92 }
93
94 inline bool operator==(const Edge& ed) const{
95 return !(*this<ed) && !(ed<*this);
96 }
97
98 inline bool operator!=(const Edge& ed) const{return !(*this==ed);}
99};
Anand Shuklafd61c602002-07-18 20:56:47 +0000100
Anand Shukla8c1c8542002-06-25 14:28:55 +0000101
102//graphListElement
103//This forms the "adjacency list element" of a
104//vertex adjacency list in graph
105struct graphListElement{
106 Node *element;
107 int weight;
108 double randId;
109 inline graphListElement(Node *n, int w, double rand){
110 element=n;
111 weight=w;
112 randId=rand;
113 }
114};
Anand Shuklafd61c602002-07-18 20:56:47 +0000115
Anand Shukla8c1c8542002-06-25 14:28:55 +0000116
117namespace std {
118 struct less<Node *> : public binary_function<Node *, Node *,bool> {
119 bool operator()(Node *n1, Node *n2) const {
120 return n1->getElement() < n2->getElement();
121 }
122 };
123
124 struct less<Edge> : public binary_function<Edge,Edge,bool> {
125 bool operator()(Edge e1, Edge e2) const {
126 assert(!e1.isNull() && !e2.isNull());
127
128 Node *x1=e1.getFirst();
129 Node *x2=e1.getSecond();
130 Node *y1=e2.getFirst();
131 Node *y2=e2.getSecond();
132 return (*x1<*y1 ||(*x1==*y1 && *x2<*y2));
133 }
134 };
135}
136
137struct BBSort{
138 bool operator()(BasicBlock *BB1, BasicBlock *BB2) const{
139 std::string name1=BB1->getName();
140 std::string name2=BB2->getName();
141 return name1<name2;
142 }
143};
144
145struct NodeListSort{
146 bool operator()(graphListElement BB1, graphListElement BB2) const{
147 std::string name1=BB1.element->getElement()->getName();
148 std::string name2=BB2.element->getElement()->getName();
149 return name1<name2;
150 }
151};
152struct EdgeCompare{
153 bool operator()(Edge e1, Edge e2) const {
154 assert(!e1.isNull() && !e2.isNull());
155 Node *x1=e1.getFirst();
156 Node *x2=e1.getSecond();
157 Node *y1=e2.getFirst();
158 Node *y2=e2.getSecond();
159 int w1=e1.getWeight();
160 int w2=e2.getWeight();
161 return (*x1<*y1 || (*x1==*y1 && *x2<*y2) || (*x1==*y1 && *x2==*y2 && w1<w2));
162 }
163};
164
Anand Shuklafd61c602002-07-18 20:56:47 +0000165
Anand Shukla8c1c8542002-06-25 14:28:55 +0000166
167//this is used to color vertices
168//during DFS
169enum Color{
170 WHITE,
171 GREY,
172 BLACK
173};
174
175
176//For path profiling,
177//We assume that the graph is connected (which is true for
178//any method CFG)
179//We also assume that the graph has single entry and single exit
180//(For this, we make a pass over the graph that ensures this)
181//The graph is a construction over any existing graph of BBs
182//Its a construction "over" existing cfg: with
183//additional features like edges and weights to edges
184
185//graph uses adjacency list representation
186class Graph{
187public:
188 //typedef std::map<Node*, std::list<graphListElement> > nodeMapTy;
189 typedef std::map<Node*, std::vector<graphListElement> > nodeMapTy;//chng
190private:
191 //the adjacency list of a vertex or node
192 nodeMapTy nodes;
193
194 //the start or root node
195 Node *strt;
196
197 //the exit node
198 Node *ext;
199
200 //a private method for doing DFS traversal of graph
201 //this is used in determining the reverse topological sort
202 //of the graph
Anand Shuklafd61c602002-07-18 20:56:47 +0000203 void DFS_Visit(Node *nd, std::vector<Node *> &toReturn);
Anand Shukla8c1c8542002-06-25 14:28:55 +0000204
205 //Its a variation of DFS to get the backedges in the graph
206 //We get back edges by associating a time
207 //and a color with each vertex.
208 //The time of a vertex is the time when it was first visited
209 //The color of a vertex is initially WHITE,
210 //Changes to GREY when it is first visited,
211 //and changes to BLACK when ALL its neighbors
212 //have been visited
213 //So we have a back edge when we meet a successor of
214 //a node with smaller time, and GREY color
215 void getBackEdgesVisit(Node *u,
216 std::vector<Edge > &be,
217 std::map<Node *, Color> &clr,
218 std::map<Node *, int> &d,
Anand Shuklafd61c602002-07-18 20:56:47 +0000219 int &time);
Anand Shukla8c1c8542002-06-25 14:28:55 +0000220
221public:
222 typedef nodeMapTy::iterator elementIterator;
223 typedef nodeMapTy::const_iterator constElementIterator;
224 typedef std::vector<graphListElement > nodeList;//chng
225 //typedef std::vector<graphListElement > nodeList;
226
227 //graph constructors
228
229 //empty constructor: then add edges and nodes later on
230 Graph() {}
231
232 //constructor with root and exit node specified
233 Graph(std::vector<Node*> n,
234 std::vector<Edge> e, Node *rt, Node *lt);
235
236 //add a node
237 void addNode(Node *nd);
238
239 //add an edge
240 //this adds an edge ONLY when
241 //the edge to be added doesn not already exist
242 //we "equate" two edges here only with their
243 //end points
244 void addEdge(Edge ed, int w);
245
246 //add an edge EVEN IF such an edge already exists
247 //this may make a multi-graph
248 //which does happen when we add dummy edges
249 //to the graph, for compensating for back-edges
250 void addEdgeForce(Edge ed);
251
252 //set the weight of an edge
253 void setWeight(Edge ed);
254
255 //remove an edge
256 //Note that it removes just one edge,
257 //the first edge that is encountered
258 void removeEdge(Edge ed);
259
260 //remove edge with given wt
261 void removeEdgeWithWt(Edge ed);
262
263 //check whether graph has an edge
264 //having an edge simply means that there is an edge in the graph
265 //which has same endpoints as the given edge
266 //it may possibly have different weight though
Anand Shuklafd61c602002-07-18 20:56:47 +0000267 bool hasEdge(Edge ed);
Anand Shukla8c1c8542002-06-25 14:28:55 +0000268
269 //check whether graph has an edge, with a given wt
Anand Shuklafd61c602002-07-18 20:56:47 +0000270 bool hasEdgeAndWt(Edge ed);
Anand Shukla8c1c8542002-06-25 14:28:55 +0000271
272 //get the list of successor nodes
Anand Shuklafd61c602002-07-18 20:56:47 +0000273 std::vector<Node *> getSuccNodes(Node *nd);
Anand Shukla8c1c8542002-06-25 14:28:55 +0000274
275 //get the number of outgoing edges
276 int getNumberOfOutgoingEdges(Node *nd) const;
277
278 //get the list of predecessor nodes
Anand Shuklafd61c602002-07-18 20:56:47 +0000279 std::vector<Node *> getPredNodes(Node *nd);
Anand Shukla8c1c8542002-06-25 14:28:55 +0000280
281
282 //to get the no of incoming edges
Anand Shuklafd61c602002-07-18 20:56:47 +0000283 int getNumberOfIncomingEdges(Node *nd);
Anand Shukla8c1c8542002-06-25 14:28:55 +0000284
285 //get the list of all the vertices in graph
286 std::vector<Node *> getAllNodes() const;
287 std::vector<Node *> getAllNodes();
288
289 //get a list of nodes in the graph
290 //in r-topological sorted order
291 //note that we assumed graph to be connected
Anand Shuklafd61c602002-07-18 20:56:47 +0000292 std::vector<Node *> reverseTopologicalSort();
Anand Shukla8c1c8542002-06-25 14:28:55 +0000293
294 //reverse the sign of weights on edges
295 //this way, max-spanning tree could be obtained
296 //usin min-spanning tree, and vice versa
297 void reverseWts();
298
299 //Ordinarily, the graph is directional
300 //this converts the graph into an
301 //undirectional graph
302 //This is done by adding an edge
303 //v->u for all existing edges u->v
304 void makeUnDirectional();
305
306 //print graph: for debugging
307 void printGraph();
308
309 //get a vector of back edges in the graph
Anand Shuklafd61c602002-07-18 20:56:47 +0000310 void getBackEdges(std::vector<Edge> &be, std::map<Node *, int> &d);
311
312 nodeList &sortNodeList(Node *par, nodeList &nl);
Anand Shukla8c1c8542002-06-25 14:28:55 +0000313
314 //Get the Maximal spanning tree (also a graph)
315 //of the graph
316 Graph* getMaxSpanningTree();
317
318 //get the nodeList adjacent to a node
319 //a nodeList element contains a node, and the weight
320 //corresponding to the edge for that element
Anand Shukla8c1c8542002-06-25 14:28:55 +0000321 inline nodeList &getNodeList(Node *nd) {
322 elementIterator nli = nodes.find(nd);
323 assert(nli != nodes.end() && "Node must be in nodes map");
Anand Shuklafd61c602002-07-18 20:56:47 +0000324 return nodes[nd];//sortNodeList(nd, nli->second);
Anand Shukla8c1c8542002-06-25 14:28:55 +0000325 }
Anand Shuklafd61c602002-07-18 20:56:47 +0000326
327 nodeList &getSortedNodeList(Node *nd) {
328 elementIterator nli = nodes.find(nd);
329 assert(nli != nodes.end() && "Node must be in nodes map");
330 return sortNodeList(nd, nodes[nd]);
331 }
332
Anand Shukla8c1c8542002-06-25 14:28:55 +0000333 //get the root of the graph
334 inline Node *getRoot() {return strt; }
335 inline Node * const getRoot() const {return strt; }
336
337 //get exit: we assumed there IS a unique exit :)
338 inline Node *getExit() {return ext; }
339 inline Node * const getExit() const {return ext; }
340 //Check if a given node is the root
341 inline bool isRoot(Node *n) const {return (*n==*strt); }
342
343 //check if a given node is leaf node
344 //here we hv only 1 leaf: which is the exit node
345 inline bool isLeaf(Node *n) const {return (*n==*ext); }
346};
347
348//This class is used to generate
349//"appropriate" code to be inserted
350//along an edge
351//The code to be inserted can be of six different types
352//as given below
353//1: r=k (where k is some constant)
354//2: r=0
355//3: r+=k
356//4: count[k]++
357//5: Count[r+k]++
358//6: Count[r]++
359class getEdgeCode{
360 private:
361 //cond implies which
362 //"kind" of code is to be inserted
363 //(from 1-6 above)
364 int cond;
365 //inc is the increment: eg k, or 0
366 int inc;
367
368 //A backedge must carry the code
369 //of both incoming "dummy" edge
370 //and outgoing "dummy" edge
371 //If a->b is a backedge
372 //then incoming dummy edge is root->b
373 //and outgoing dummy edge is a->exit
374
375 //incoming dummy edge, if any
376 getEdgeCode *cdIn;
377
378 //outgoing dummy edge, if any
379 getEdgeCode *cdOut;
380
381public:
382 getEdgeCode(){
383 cdIn=NULL;
384 cdOut=NULL;
385 inc=0;
386 cond=0;
387 }
388
389 //set condition: 1-6
390 inline void setCond(int n) {cond=n;}
391
392 //get the condition
393 inline int getCond() { return cond;}
394
395 //set increment
396 inline void setInc(int n) {inc=n;}
397
398 //get increment
399 inline int getInc() {return inc;}
400
401 //set CdIn (only used for backedges)
402 inline void setCdIn(getEdgeCode *gd){ cdIn=gd;}
403
404 //set CdOut (only used for backedges)
405 inline void setCdOut(getEdgeCode *gd){ cdOut=gd;}
406
407 //get the code to be inserted on the edge
408 //This is determined from cond (1-6)
Anand Shukla8c1c8542002-06-25 14:28:55 +0000409 void getCode(Instruction *a, Instruction *b, Function *M, BasicBlock *BB,
410 int numPaths, int MethNo);
Anand Shukla8c1c8542002-06-25 14:28:55 +0000411};
412
413
414//auxillary functions on graph
415
416//print a given edge in the form BB1Label->BB2Label
417void printEdge(Edge ed);
418
419//Do graph processing: to determine minimal edge increments,
420//appropriate code insertions etc and insert the code at
421//appropriate locations
Anand Shuklafd61c602002-07-18 20:56:47 +0000422void processGraph(Graph &g, Instruction *rInst, Instruction *countInst, std::vector<Edge> &be, std::vector<Edge> &stDummy, std::vector<Edge> &exDummy, int n, int MethNo);
Anand Shukla8c1c8542002-06-25 14:28:55 +0000423
424//print the graph (for debugging)
425void printGraph(Graph &g);
426
427
428//void printGraph(const Graph g);
429//insert a basic block with appropriate code
430//along a given edge
431void insertBB(Edge ed, getEdgeCode *edgeCode, Instruction *rInst, Instruction *countInst, int n, int Methno);
432
433//Insert the initialization code in the top BB
434//this includes initializing r, and count
435//r is like an accumulator, that
436//keeps on adding increments as we traverse along a path
437//and at the end of the path, r contains the path
438//number of that path
439//Count is an array, where Count[k] represents
440//the number of executions of path k
441void insertInTopBB(BasicBlock *front, int k, Instruction *rVar, Instruction *countVar);
442
443//Add dummy edges corresponding to the back edges
444//If a->b is a backedge
445//then incoming dummy edge is root->b
446//and outgoing dummy edge is a->exit
447void addDummyEdges(std::vector<Edge> &stDummy, std::vector<Edge> &exDummy, Graph &g, std::vector<Edge> &be);
448
449//Assign a value to all the edges in the graph
450//such that if we traverse along any path from root to exit, and
451//add up the edge values, we get a path number that uniquely
452//refers to the path we travelled
Anand Shuklafd61c602002-07-18 20:56:47 +0000453int valueAssignmentToEdges(Graph& g, std::map<Node *, int> nodePriority);
Anand Shukla8c1c8542002-06-25 14:28:55 +0000454
455void getBBtrace(std::vector<BasicBlock *> &vBB, int pathNo, Function *M);
456#endif
457
458