Fix warning
[oota-llvm.git] / lib / Transforms / Instrumentation / ProfilePaths / ProfilePaths.cpp
index 98430f9c7a11d0ecc3f7d4acfc0088802cc3bfce..bc815c6e23f6095dce883cb1ea4ea976a6babf6a 100644 (file)
 
 #include "llvm/Transforms/Instrumentation/ProfilePaths.h"
 #include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
+#include "llvm/Transforms/Instrumentation/Graph.h"
 #include "llvm/Support/CFG.h"
 #include "llvm/Constants.h"
 #include "llvm/DerivedTypes.h"
 #include "llvm/iMemory.h"
-#include "llvm/Transforms/Instrumentation/Graph.h"
+#include "llvm/GlobalVariable.h"
+#include "llvm/Module.h"
 #include <iostream>
 #include <fstream>
 
@@ -43,7 +45,7 @@ struct ProfilePaths : public FunctionPass {
   // entry and only one exit node for the function in the CFG of the function
   //
   void ProfilePaths::getAnalysisUsage(AnalysisUsage &AU) const {
-    AU.addRequired(UnifyFunctionExitNodes::ID);
+    AU.addRequired<UnifyFunctionExitNodes>();
   }
 };
 
@@ -74,14 +76,9 @@ bool ProfilePaths::runOnFunction(Function &F){
     return false;
   }
  
-  //std::cerr<<"Instrumenting\n-----------------\n";
-  //std::cerr<<F;
   //increment counter for instrumented functions. mn is now function#
   mn++;
   
-  //std::cerr<<"MN = "<<mn<<"\n";;
-  //std::cerr<<F;
-
   // Transform the cfg s.t. we have just one exit node
   BasicBlock *ExitNode = getAnalysis<UnifyFunctionExitNodes>().getExitNode();  
 
@@ -90,7 +87,7 @@ bool ProfilePaths::runOnFunction(Function &F){
   std::vector<Edge> edges;
 
   Node *tmp;
-  Node *exitNode, *startNode;
+  Node *exitNode = 0, *startNode = 0;
 
   // The nodes must be uniquesly identified:
   // That is, no two nodes must hav same BB*
@@ -120,10 +117,10 @@ bool ProfilePaths::runOnFunction(Function &F){
   
   Graph g(nodes,edges, startNode, exitNode);
 
-  //#ifdef DEBUG_PATH_PROFILES  
-  //std::cerr<<"Original graph\n";
-  //printGraph(g);
-  //#endif
+#ifdef DEBUG_PATH_PROFILES  
+  std::cerr<<"Original graph\n";
+  printGraph(g);
+#endif
 
   BasicBlock *fr = &F.front();
   
@@ -132,19 +129,15 @@ bool ProfilePaths::runOnFunction(Function &F){
   vector<Edge> be;
   std::map<Node *, int> nodePriority; //it ranks nodes in depth first order traversal
   g.getBackEdges(be, nodePriority);
-  /*
-  std::cerr<<"Node priority--------------\n";
-  for(std::map<Node *, int>::iterator MI = nodePriority.begin(), 
-        ME = nodePriority.end(); MI!=ME; ++MI)
-    std::cerr<<MI->first->getElement()->getName()<<"->"<<MI->second<<"\n";
-  std::cerr<<"End Node priority--------------\n";
-  */
-  //std::cerr<<"BackEdges-------------\n";
-  //   for(vector<Edge>::iterator VI=be.begin(); VI!=be.end(); ++VI){
-  //printEdge(*VI);
-  //cerr<<"\n";
-  //}
-  //std::cerr<<"------\n";
+  
+#ifdef DEBUG_PATH_PROFILES
+  std::cerr<<"BackEdges-------------\n";
+  for(vector<Edge>::iterator VI=be.begin(); VI!=be.end(); ++VI){
+    printEdge(*VI);
+    cerr<<"\n";
+  }
+  std::cerr<<"------\n";
+#endif
 
 #ifdef DEBUG_PATH_PROFILES
   cerr<<"Backedges:"<<be.size()<<endl;
@@ -159,20 +152,25 @@ bool ProfilePaths::runOnFunction(Function &F){
   vector<Edge> exDummy;
   addDummyEdges(stDummy, exDummy, g, be);
 
-  //std::cerr<<"After adding dummy edges\n";
-  //printGraph(g);
-    
+#ifdef DEBUG_PATH_PROFILES
+  std::cerr<<"After adding dummy edges\n";
+  printGraph(g);
+#endif
+
   // Now, every edge in the graph is assigned a weight
   // This weight later adds on to assign path
   // numbers to different paths in the graph
   //  All paths for now are acyclic,
   // since no back edges in the graph now
   // numPaths is the number of acyclic paths in the graph
-  int numPaths=valueAssignmentToEdges(g, nodePriority);
+  int numPaths=valueAssignmentToEdges(g, nodePriority, be);
 
   if(numPaths<=1 || numPaths >5000) return false;
-  //std::cerr<<"Numpaths="<<numPaths<<std::endl;
-  //printGraph(g);
+  
+#ifdef DEBUG_PATH_PROFILES  
+  printGraph(g);
+#endif
+
   //create instruction allocation r and count
   //r is the variable that'll act like an accumulator
   //all along the path, we just add edge values to r
@@ -181,22 +179,36 @@ bool ProfilePaths::runOnFunction(Function &F){
   //the number of executions of path numbered x
 
   Instruction *rVar=new 
-    AllocaInst(PointerType::get(Type::IntTy)
+    AllocaInst(Type::IntTy
                ConstantUInt::get(Type::UIntTy,1),"R");
-    
+
   Instruction *countVar=new 
-    AllocaInst(PointerType::get(Type::IntTy)
+    AllocaInst(Type::IntTy
                ConstantUInt::get(Type::UIntTy, numPaths), "Count");
-    
+  
+  static GlobalVariable *threshold = NULL;
+  static bool insertedThreshold = false;
+
+  if(!insertedThreshold){
+    threshold = new GlobalVariable(Type::IntTy, false, false, 0,
+                                                   "reopt_threshold");
+
+    F.getParent()->getGlobalList().push_back(threshold);
+    insertedThreshold = true;
+  }
+
+  assert(threshold && "GlobalVariable threshold not defined!");
+
   // insert initialization code in first (entry) BB
   // this includes initializing r and count
-  insertInTopBB(&F.getEntryNode(),numPaths, rVar, countVar);
+  insertInTopBB(&F.getEntryNode(),numPaths, rVar, countVar, threshold);
     
   //now process the graph: get path numbers,
   //get increments along different paths,
   //and assign "increments" and "updates" (to r and count)
   //"optimally". Finally, insert llvm code along various edges
-  processGraph(g, rVar, countVar, be, stDummy, exDummy, numPaths, mn);    
+  processGraph(g, rVar, countVar, be, stDummy, exDummy, numPaths, mn, 
+               threshold);    
    
   return true;  // Always modifies function
 }