Fix warning
[oota-llvm.git] / lib / Transforms / Instrumentation / ProfilePaths / GraphAuxiliary.cpp
index 47d4d239d0bc16d7392c4e3cceff1286aff59a06..3a34131c88954aeafba86cb51e7c4f6df0c48e02 100644 (file)
@@ -7,15 +7,12 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
-#include "llvm/Function.h"
-#include "llvm/Pass.h"
-#include "llvm/BasicBlock.h"
-#include "llvm/InstrTypes.h"
 #include "llvm/Transforms/Instrumentation/Graph.h"
+#include "llvm/Pass.h"
+#include "llvm/Module.h"
+#include "llvm/iTerminators.h"
+#include "Support/Statistic.h"
 #include <algorithm>
-#include <iostream>
-#include <sstream>
-#include <string>
 
 //using std::list;
 using std::map;
@@ -68,7 +65,8 @@ static void removeTreeEdges(Graph &g, Graph& t){
 //such that if we traverse along any path from root to exit, and
 //add up the edge values, we get a path number that uniquely
 //refers to the path we travelled
-int valueAssignmentToEdges(Graph& g){
+int valueAssignmentToEdges(Graph& g,  map<Node *, int> nodePriority, 
+                           vector<Edge> &be){
   vector<Node *> revtop=g.reverseTopologicalSort();
   map<Node *,int > NumPaths;
   for(vector<Node *>::iterator RI=revtop.begin(), RE=revtop.end(); 
@@ -78,36 +76,43 @@ int valueAssignmentToEdges(Graph& g){
     else{
       NumPaths[*RI]=0;
 
-      Graph::nodeList &nlist=g.getNodeList(*RI);
+      // Modified Graph::nodeList &nlist=g.getNodeList(*RI);
+      Graph::nodeList &nlist=g.getSortedNodeList(*RI, be);
+
       //sort nodelist by increasing order of numpaths
       
       int sz=nlist.size();
       
-      //printing BB list
-      //std::cerr<<"node list------------\n";
-      //for(Graph::nodeList::iterator NLI=nlist.begin(), NLE=nlist.end(); 
-      //  NLI!=NLE; ++NLI)
-      //std::cerr<<NLI->element->getElement()->getName()<<"->";
-      
-      //std::cerr<<"\n-----------\n";
-
       for(int i=0;i<sz-1; i++){
        int min=i;
        for(int j=i+1; j<sz; j++){
           BasicBlock *bb1 = nlist[j].element->getElement();
           BasicBlock *bb2 = nlist[min].element->getElement();
-          assert(bb1->getParent() == bb2->getParent() && 
-                 "BBs with diff parents"); 
-          TerminatorInst *ti = bb1->getTerminator();
+          
+          if(bb1 == bb2) continue;
+          
+          if(*RI == g.getRoot()){
+            assert(nodePriority[nlist[min].element]!= 
+                   nodePriority[nlist[j].element] 
+                   && "priorities can't be same!");
+            
+            if(nodePriority[nlist[j].element] < 
+               nodePriority[nlist[min].element])
+              min = j; 
+          }
 
-          //compare the order of BBs in the terminator instruction
-          for(int x=0, y = ti->getNumSuccessors(); x < y; x++){
-            if(ti->getSuccessor(x) == bb1){ //bb1 occurs first
+          else{
+            TerminatorInst *tti = (*RI)->getElement()->getTerminator();
+            
+            BranchInst *ti =  cast<BranchInst>(tti);
+            assert(ti && "not a branch");
+            assert(ti->getNumSuccessors()==2 && "less successors!");
+            
+            BasicBlock *tB = ti->getSuccessor(0);
+            BasicBlock *fB = ti->getSuccessor(1);
+            
+            if(tB == bb1 || fB == bb2)
               min = j;
-              break;
-            }
-            if(ti->getSuccessor(x) == bb2) //bb2 occurs first
-              break;
           }
           
         }
@@ -119,7 +124,7 @@ int valueAssignmentToEdges(Graph& g){
       //sorted now!
       for(Graph::nodeList::iterator GLI=nlist.begin(), GLE=nlist.end();
          GLI!=GLE; ++GLI){
-       GLI->weight=NumPaths[*RI];
+               GLI->weight=NumPaths[*RI];
        NumPaths[*RI]+=NumPaths[GLI->element];
       }
     }
@@ -156,7 +161,7 @@ static int inc_Dir(Edge e, Edge f){
 
 //used for getting edge increments (read comments above in inc_Dir)
 //inc_DFS is a modification of DFS 
-static void inc_DFS(Graph& g,Graph& t,map<Edge, int, EdgeCompare>& Increment, 
+static void inc_DFS(Graph& g,Graph& t,map<Edge, int, EdgeCompare2>& Increment, 
             int events, Node *v, Edge e){
   
   vector<Node *> allNodes=t.getAllNodes();
@@ -210,15 +215,17 @@ static void inc_DFS(Graph& g,Graph& t,map<Edge, int, EdgeCompare>& Increment,
 //and assign them some values such that 
 //if we consider just this subset, it still represents
 //the path sum along any path in the graph
-static map<Edge, int, EdgeCompare> getEdgeIncrements(Graph& g, Graph& t){
+static map<Edge, int, EdgeCompare2> getEdgeIncrements(Graph& g, Graph& t, 
+                                                      vector<Edge> &be){
   //get all edges in g-t
-  map<Edge, int, EdgeCompare> Increment;
+  map<Edge, int, EdgeCompare2> Increment;
 
   vector<Node *> allNodes=g.getAllNodes();
  
   for(vector<Node *>::iterator NI=allNodes.begin(), NE=allNodes.end(); NI!=NE; 
       ++NI){
-    Graph::nodeList node_list=g.getNodeList(*NI);
+    Graph::nodeList node_list=g.getSortedNodeList(*NI, be);
+    //modified g.getNodeList(*NI);
     for(Graph::nodeList::iterator NLI=node_list.begin(), NLE=node_list.end(); 
        NLI!=NLE; ++NLI){
       Edge ed(*NI, NLI->element,NLI->weight,NLI->randId);
@@ -233,7 +240,8 @@ static map<Edge, int, EdgeCompare> getEdgeIncrements(Graph& g, Graph& t){
 
   for(vector<Node *>::iterator NI=allNodes.begin(), NE=allNodes.end(); NI!=NE; 
       ++NI){
-    Graph::nodeList node_list=g.getNodeList(*NI);
+    Graph::nodeList node_list=g.getSortedNodeList(*NI, be);
+    //modified g.getNodeList(*NI);
     for(Graph::nodeList::iterator NLI=node_list.begin(), NLE=node_list.end(); 
        NLI!=NLE; ++NLI){
       Edge ed(*NI, NLI->element,NLI->weight, NLI->randId);
@@ -258,9 +266,9 @@ graphListElement *findNodeInList(Graph::nodeList &NL, Node *N);
 //the kind of code to be inserted along an edge
 //The idea here is to minimize the computation
 //by inserting only the needed code
-static void getCodeInsertions(Graph &g, map<Edge, getEdgeCode *, EdgeCompare> &instr,
+static void getCodeInsertions(Graph &g, map<Edge, getEdgeCode *, EdgeCompare2> &instr,
                               vector<Edge > &chords, 
-                              map<Edge,int, EdgeCompare> &edIncrements){
+                              map<Edge,int, EdgeCompare2> &edIncrements){
 
   //Register initialization code
   vector<Node *> ws;
@@ -293,14 +301,12 @@ static void getCodeInsertions(Graph &g, map<Edge, getEdgeCode *, EdgeCompare> &i
       }
       else if(g.getNumberOfIncomingEdges(w)==1){
        ws.push_back(w);
-       //std::cerr<<"Added w\n";
       }
       else{
        getEdgeCode *edCd=new getEdgeCode();
        edCd->setCond(2);
        edCd->setInc(0);
        instr[ed]=edCd;
-       //std::cerr<<"Case 2\n";
       }
     }
   }
@@ -319,39 +325,42 @@ static void getCodeInsertions(Graph &g, map<Edge, getEdgeCode *, EdgeCompare> &i
     for(vector<Node *>::iterator EII=lllt.begin(); EII!=lllt.end() ;++EII){
       Node *lnode=*EII;
       Graph::nodeList &nl = g.getNodeList(lnode);
-      graphListElement *N = findNodeInList(nl, w);
-      if (N){  
-       Node *v=lnode;
-
-       //if chords has v->w
-       Edge ed(v,w, N->weight, N->randId);
-       getEdgeCode *edCd=new getEdgeCode();
-       bool hasEdge=false;
-       for(vector<Edge>::iterator CI=chords.begin(), CE=chords.end(); CI!=CE;
-           ++CI){
-         if(*CI==ed && CI->getWeight()==N->weight){
-           hasEdge=true;
-           break;
-         }
-       }
-       if(hasEdge){
-         char str[100];
-         if(instr[ed]!=NULL && instr[ed]->getCond()==1){
-           instr[ed]->setCond(4);
-         }
-         else{
-           edCd->setCond(5);
-           edCd->setInc(edIncrements[ed]);
-           instr[ed]=edCd;
-         }
-         
-       }
-       else if(g.getNumberOfOutgoingEdges(v)==1)
-         ws.push_back(v);
-       else{
-         edCd->setCond(6);
-         instr[ed]=edCd;
-       }
+      //graphListElement *N = findNodeInList(nl, w);
+      for(Graph::nodeList::const_iterator N = nl.begin(), 
+            NNEN = nl.end(); N!= NNEN; ++N){
+        if (*N->element == *w){        
+          Node *v=lnode;
+          
+          //if chords has v->w
+          Edge ed(v,w, N->weight, N->randId);
+          getEdgeCode *edCd=new getEdgeCode();
+          bool hasEdge=false;
+          for(vector<Edge>::iterator CI=chords.begin(), CE=chords.end(); CI!=CE;
+              ++CI){
+            if(*CI==ed && CI->getWeight()==N->weight){
+              hasEdge=true;
+              break;
+            }
+          }
+          if(hasEdge){
+            //char str[100];
+            if(instr[ed]!=NULL && instr[ed]->getCond()==1){
+              instr[ed]->setCond(4);
+            }
+            else{
+              edCd->setCond(5);
+              edCd->setInc(edIncrements[ed]);
+              instr[ed]=edCd;
+            }
+            
+          }
+          else if(g.getNumberOfOutgoingEdges(v)==1)
+            ws.push_back(v);
+          else{
+            edCd->setCond(6);
+            instr[ed]=edCd;
+          }
+        }
       }
     }
   }
@@ -407,14 +416,14 @@ void printEdge(Edge ed){
 static void moveDummyCode(vector<Edge> &stDummy, 
                           vector<Edge> &exDummy, 
                           vector<Edge> &be,  
-                          map<Edge, getEdgeCode *, EdgeCompare> &insertions, 
+                          map<Edge, getEdgeCode *, EdgeCompare2> &insertions, 
                          Graph &g){
   typedef vector<Edge >::iterator vec_iter;
   
-  map<Edge,getEdgeCode *, EdgeCompare> temp;
+  map<Edge,getEdgeCode *, EdgeCompare2> temp;
   //iterate over edges with code
   std::vector<Edge> toErase;
-  for(map<Edge,getEdgeCode *, EdgeCompare>::iterator MI=insertions.begin(), 
+  for(map<Edge,getEdgeCode *, EdgeCompare2>::iterator MI=insertions.begin(), 
        ME=insertions.end(); MI!=ME; ++MI){
     Edge ed=MI->first;
     getEdgeCode *edCd=MI->second;
@@ -453,7 +462,7 @@ static void moveDummyCode(vector<Edge> &stDummy,
     g.removeEdgeWithWt(*vmi);
   }
   
-  for(map<Edge,getEdgeCode *, EdgeCompare>::iterator MI=temp.begin(), 
+  for(map<Edge,getEdgeCode *, EdgeCompare2>::iterator MI=temp.begin(), 
       ME=temp.end(); MI!=ME; ++MI){
     insertions[MI->first]=MI->second;
   }
@@ -474,10 +483,9 @@ void processGraph(Graph &g,
                  vector<Edge >& be, 
                  vector<Edge >& stDummy, 
                  vector<Edge >& exDummy, 
-                 int numPaths){
+                 int numPaths, int MethNo, 
+                  Value *threshold){
 
-  static int MethNo=-1;
-  MethNo++;
   //Given a graph: with exit->root edge, do the following in seq:
   //1. get back edges
   //2. insert dummy edges and remove back edges
@@ -497,7 +505,7 @@ void processGraph(Graph &g,
   //This would hold all 
   //right as long as number of paths in the graph
   //is less than this
-  const int INFINITY=99999999;
+  const int Infinity=99999999;
 
 
   //step 1-3 are already done on the graph when this function is called
@@ -507,14 +515,14 @@ void processGraph(Graph &g,
 
   //now insert exit to root edge
   //if its there earlier, remove it!
-  //assign it weight INFINITY
+  //assign it weight Infinity
   //so that this edge IS ALWAYS IN spanning tree
   //Note than edges in spanning tree do not get 
   //instrumented: and we do not want the
   //edge exit->root to get instrumented
   //as it MAY BE a dummy edge
-  Edge ed(g.getExit(),g.getRoot(),INFINITY);
-  g.addEdge(ed,INFINITY);
+  Edge ed(g.getExit(),g.getRoot(),Infinity);
+  g.addEdge(ed,Infinity);
   Graph g2=g;
 
   //make g2 undirectional: this gives a better
@@ -577,15 +585,15 @@ void processGraph(Graph &g,
   //if we consider just this subset, it still represents
   //the path sum along any path in the graph
 
-  map<Edge, int, EdgeCompare> increment=getEdgeIncrements(g,*t);
+  map<Edge, int, EdgeCompare2> increment=getEdgeIncrements(g,*t, be);
 #ifdef DEBUG_PATH_PROFILES
   //print edge increments for debugging
-  
-  for(map<Edge, int, EdgeCompare>::iterator M_I=increment.begin(), M_E=increment.end(); 
-      M_I!=M_E; ++M_I){
-    printEdge(M_I->first);
-    cerr<<"Increment for above:"<<M_I->second<<"\n";
+  std::cerr<<"Edge Increments------\n";
+  for(map<Edge, int, EdgeCompare2>::iterator MMI=increment.begin(), MME=increment.end(); MMI != MME; ++MMI){
+    printEdge(MMI->first);
+    std::cerr<<"Increment for above:"<<MMI->second<<"\n";
   }
+  std::cerr<<"-------end of edge increments\n";
 #endif
 
  
@@ -599,15 +607,13 @@ void processGraph(Graph &g,
   getChords(chords, g, *t);
 
 
-  //cerr<<"Graph before getCodeInsertion:\n";
-  //printGraph(g);
-  map<Edge, getEdgeCode *, EdgeCompare> codeInsertions;
+  map<Edge, getEdgeCode *, EdgeCompare2> codeInsertions;
   getCodeInsertions(g, codeInsertions, chords,increment);
   
 #ifdef DEBUG_PATH_PROFILES
   //print edges with code for debugging
   cerr<<"Code inserted in following---------------\n";
-  for(map<Edge, getEdgeCode *>::iterator cd_i=codeInsertions.begin(), 
+  for(map<Edge, getEdgeCode *, EdgeCompare2>::iterator cd_i=codeInsertions.begin(), 
        cd_e=codeInsertions.end(); cd_i!=cd_e; ++cd_i){
     printEdge(cd_i->first);
     cerr<<cd_i->second->getCond()<<":"<<cd_i->second->getInc()<<"\n";
@@ -640,7 +646,7 @@ void processGraph(Graph &g,
   for(map<Edge, getEdgeCode *>::iterator MI=codeInsertions.begin(), 
        ME=codeInsertions.end(); MI!=ME; ++MI){
     Edge ed=MI->first;
-    insertBB(ed, MI->second, rInst, countInst, numPaths, MethNo);
+    insertBB(ed, MI->second, rInst, countInst, numPaths, MethNo, threshold);
   } 
 }