1 //===-- ------------------------llvm/graph.h ---------------------*- C++ -*--=//
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
7 //===----------------------------------------------------------------------===//
12 #include "Support/StatisticReporter.h"
18 #include "llvm/BasicBlock.h"
26 //It forms the vertex for the graph
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; }
44 //Denotes an edge in the graph
53 inline Edge(Node *f,Node *s, int wt=0){
61 inline Edge(Node *f,Node *s, int wt, double rd){
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; }
76 inline int getWeight() { assert(!isNull()); return weight; }
77 inline void setWeight(int n) { assert(!isNull()); weight=n; }
79 inline void setFirst(Node *&f) { assert(!isNull()); first=f; }
80 inline void setSecond(Node *&s) { assert(!isNull()); second=s; }
83 inline bool isNull() const { return isnull;}
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())
90 return (*first<*(ed.getFirst()))||
91 (*first==*(ed.getFirst()) && *second<*(ed.getSecond()));
94 inline bool operator==(const Edge& ed) const{
95 return !(*this<ed) && !(ed<*this);
98 inline bool operator!=(const Edge& ed) const{return !(*this==ed);}
103 //This forms the "adjacency list element" of a
104 //vertex adjacency list in graph
105 struct graphListElement{
109 inline graphListElement(Node *n, int w, double rand){
118 struct less<Node *> : public binary_function<Node *, Node *,bool> {
119 bool operator()(Node *n1, Node *n2) const {
120 return n1->getElement() < n2->getElement();
124 struct less<Edge> : public binary_function<Edge,Edge,bool> {
125 bool operator()(Edge e1, Edge e2) const {
126 assert(!e1.isNull() && !e2.isNull());
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));
138 bool operator()(BasicBlock *BB1, BasicBlock *BB2) const{
139 std::string name1=BB1->getName();
140 std::string name2=BB2->getName();
146 bool operator()(graphListElement BB1, graphListElement BB2) const{
147 std::string name1=BB1.element->getElement()->getName();
148 std::string name2=BB2.element->getElement()->getName();
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));
167 //this is used to color vertices
176 //For path profiling,
177 //We assume that the graph is connected (which is true for
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
185 //graph uses adjacency list representation
188 //typedef std::map<Node*, std::list<graphListElement> > nodeMapTy;
189 typedef std::map<Node*, std::vector<graphListElement> > nodeMapTy;//chng
191 //the adjacency list of a vertex or node
194 //the start or root node
200 //a private method for doing DFS traversal of graph
201 //this is used in determining the reverse topological sort
203 void DFS_Visit(Node *nd, std::vector<Node *> &toReturn);
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
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,
222 typedef nodeMapTy::iterator elementIterator;
223 typedef nodeMapTy::const_iterator constElementIterator;
224 typedef std::vector<graphListElement > nodeList;//chng
225 //typedef std::vector<graphListElement > nodeList;
229 //empty constructor: then add edges and nodes later on
232 //constructor with root and exit node specified
233 Graph(std::vector<Node*> n,
234 std::vector<Edge> e, Node *rt, Node *lt);
237 void addNode(Node *nd);
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
244 void addEdge(Edge ed, int w);
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);
252 //set the weight of an edge
253 void setWeight(Edge ed);
256 //Note that it removes just one edge,
257 //the first edge that is encountered
258 void removeEdge(Edge ed);
260 //remove edge with given wt
261 void removeEdgeWithWt(Edge ed);
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
267 bool hasEdge(Edge ed);
269 //check whether graph has an edge, with a given wt
270 bool hasEdgeAndWt(Edge ed);
272 //get the list of successor nodes
273 std::vector<Node *> getSuccNodes(Node *nd);
275 //get the number of outgoing edges
276 int getNumberOfOutgoingEdges(Node *nd) const;
278 //get the list of predecessor nodes
279 std::vector<Node *> getPredNodes(Node *nd);
282 //to get the no of incoming edges
283 int getNumberOfIncomingEdges(Node *nd);
285 //get the list of all the vertices in graph
286 std::vector<Node *> getAllNodes() const;
287 std::vector<Node *> getAllNodes();
289 //get a list of nodes in the graph
290 //in r-topological sorted order
291 //note that we assumed graph to be connected
292 std::vector<Node *> reverseTopologicalSort();
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
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();
306 //print graph: for debugging
309 //get a vector of back edges in the graph
310 void getBackEdges(std::vector<Edge> &be, std::map<Node *, int> &d);
312 nodeList &sortNodeList(Node *par, nodeList &nl);
314 //Get the Maximal spanning tree (also a graph)
316 Graph* getMaxSpanningTree();
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
321 inline nodeList &getNodeList(Node *nd) {
322 elementIterator nli = nodes.find(nd);
323 assert(nli != nodes.end() && "Node must be in nodes map");
324 return nodes[nd];//sortNodeList(nd, nli->second);
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]);
333 //get the root of the graph
334 inline Node *getRoot() {return strt; }
335 inline Node * const getRoot() const {return strt; }
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); }
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); }
348 //This class is used to generate
349 //"appropriate" code to be inserted
351 //The code to be inserted can be of six different types
353 //1: r=k (where k is some constant)
362 //"kind" of code is to be inserted
365 //inc is the increment: eg k, or 0
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
375 //incoming dummy edge, if any
378 //outgoing dummy edge, if any
390 inline void setCond(int n) {cond=n;}
393 inline int getCond() { return cond;}
396 inline void setInc(int n) {inc=n;}
399 inline int getInc() {return inc;}
401 //set CdIn (only used for backedges)
402 inline void setCdIn(getEdgeCode *gd){ cdIn=gd;}
404 //set CdOut (only used for backedges)
405 inline void setCdOut(getEdgeCode *gd){ cdOut=gd;}
407 //get the code to be inserted on the edge
408 //This is determined from cond (1-6)
409 void getCode(Instruction *a, Instruction *b, Function *M, BasicBlock *BB,
410 int numPaths, int MethNo);
414 //auxillary functions on graph
416 //print a given edge in the form BB1Label->BB2Label
417 void printEdge(Edge ed);
419 //Do graph processing: to determine minimal edge increments,
420 //appropriate code insertions etc and insert the code at
421 //appropriate locations
422 void processGraph(Graph &g, Instruction *rInst, Instruction *countInst, std::vector<Edge> &be, std::vector<Edge> &stDummy, std::vector<Edge> &exDummy, int n, int MethNo);
424 //print the graph (for debugging)
425 void printGraph(Graph &g);
428 //void printGraph(const Graph g);
429 //insert a basic block with appropriate code
431 void insertBB(Edge ed, getEdgeCode *edgeCode, Instruction *rInst, Instruction *countInst, int n, int Methno);
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
441 void insertInTopBB(BasicBlock *front, int k, Instruction *rVar, Instruction *countVar);
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
447 void addDummyEdges(std::vector<Edge> &stDummy, std::vector<Edge> &exDummy, Graph &g, std::vector<Edge> &be);
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
453 int valueAssignmentToEdges(Graph& g, std::map<Node *, int> nodePriority);
455 void getBBtrace(std::vector<BasicBlock *> &vBB, int pathNo, Function *M);