We won't use automake
[oota-llvm.git] / lib / Transforms / Instrumentation / ProfilePaths / ProfilePaths.cpp
index 138f58238a1ea2af1434a10f2d895d77405a1247..ef7b9b381864230c7eb756720a7a34716251fac0 100644 (file)
@@ -1,41 +1,47 @@
-//===-- ProfilePaths.cpp - interface to insert instrumentation ---*- C++ -*--=//
+//===-- ProfilePaths.cpp - interface to insert instrumentation --*- C++ -*-===//
+// 
+//                     The LLVM Compiler Infrastructure
 //
-// This inserts intrumentation for counting
-// execution of paths though a given function
-// Its implemented as a "Function" Pass, and called using opt
+// This file was developed by the LLVM research group and is distributed under
+// the University of Illinois Open Source License. See LICENSE.TXT for details.
+// 
+//===----------------------------------------------------------------------===//
+//
+// This inserts instrumentation for counting execution of paths though a given
+// function Its implemented as a "Function" Pass, and called using opt
 //
 // This pass is implemented by using algorithms similar to 
 // 1."Efficient Path Profiling": Ball, T. and Larus, J. R., 
-// Proceedings of Micro-29, Dec 1996, Paris, France.
+//    Proceedings of Micro-29, Dec 1996, Paris, France.
 // 2."Efficiently Counting Program events with support for on-line
 //   "queries": Ball T., ACM Transactions on Programming Languages
-//   and systems, Sep 1994.
+//    and systems, Sep 1994.
 //
-// The algorithms work on a Graph constructed over the nodes
-// made from Basic Blocks: The transformations then take place on
-// the constucted graph (implementation in Graph.cpp and GraphAuxillary.cpp)
-// and finally, appropriate instrumentation is placed over suitable edges.
-// (code inserted through EdgeCode.cpp).
+// The algorithms work on a Graph constructed over the nodes made from Basic
+// Blocks: The transformations then take place on the constructed graph
+// (implementation in Graph.cpp and GraphAuxiliary.cpp) and finally, appropriate
+// instrumentation is placed over suitable edges.  (code inserted through
+// EdgeCode.cpp).
 // 
-// The algorithm inserts code such that every acyclic path in the CFG
-// of a function is identified through a unique number. the code insertion
-// is optimal in the sense that its inserted over a minimal set of edges. Also,
-// the algorithm makes sure than initialization, path increment and counter
-// update can be collapsed into minimum number of edges.
+// The algorithm inserts code such that every acyclic path in the CFG of a
+// function is identified through a unique number. the code insertion is optimal
+// in the sense that its inserted over a minimal set of edges. Also, the
+// algorithm makes sure than initialization, path increment and counter update
+// can be collapsed into minimum number of edges.
+//
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h"
 #include "llvm/Support/CFG.h"
 #include "llvm/Constants.h"
 #include "llvm/DerivedTypes.h"
-#include "llvm/iMemory.h"
-#include "llvm/iOperators.h"
-#include "llvm/iOther.h"
+#include "llvm/Instructions.h"
 #include "llvm/Module.h"
 #include "Graph.h"
 #include <fstream>
-#include <stdio.h>
-using std::vector;
+#include <cstdio>
+
+namespace llvm {
 
 struct ProfilePaths : public FunctionPass {
   bool runOnFunction(Function &F);
@@ -72,7 +78,8 @@ bool ProfilePaths::runOnFunction(Function &F){
   mn++;
   
   // Transform the cfg s.t. we have just one exit node
-  BasicBlock *ExitNode = getAnalysis<UnifyFunctionExitNodes>().getExitNode();  
+  BasicBlock *ExitNode = 
+    getAnalysis<UnifyFunctionExitNodes>().getReturnBlock();  
 
   //iterating over BBs and making graph
   std::vector<Node *> nodes;
@@ -81,7 +88,7 @@ bool ProfilePaths::runOnFunction(Function &F){
   Node *tmp;
   Node *exitNode = 0, *startNode = 0;
 
-  // The nodes must be uniquesly identified:
+  // The nodes must be uniquely identified:
   // That is, no two nodes must hav same BB*
   
   for (Function::iterator BB = F.begin(), BE = F.end(); BB != BE; ++BB) {
@@ -93,13 +100,12 @@ bool ProfilePaths::runOnFunction(Function &F){
       startNode=nd;
   }
 
-  // now do it againto insert edges
+  // now do it again to insert edges
   for (Function::iterator BB = F.begin(), BE = F.end(); BB != BE; ++BB){
     Node *nd=findBB(nodes, BB);
     assert(nd && "No node for this edge!");
 
-    for(BasicBlock::succ_iterator s=succ_begin(BB), se=succ_end(BB); 
-       s!=se; ++s){
+    for(succ_iterator s=succ_begin(BB), se=succ_end(BB); s!=se; ++s){
       Node *nd2=findBB(nodes,*s);
       assert(nd2 && "No node for this edge!");
       Edge ed(nd,nd2,0);
@@ -118,13 +124,13 @@ bool ProfilePaths::runOnFunction(Function &F){
   
   // The graph is made acyclic: this is done
   // by removing back edges for now, and adding them later on
-  vector<Edge> be;
+  std::vector<Edge> be;
   std::map<Node *, int> nodePriority; //it ranks nodes in depth first order traversal
   g.getBackEdges(be, nodePriority);
   
 #ifdef DEBUG_PATH_PROFILES
   std::cerr<<"BackEdges-------------\n";
-  for(vector<Edge>::iterator VI=be.begin(); VI!=be.end(); ++VI){
+  for (std::vector<Edge>::iterator VI=be.begin(); VI!=be.end(); ++VI){
     printEdge(*VI);
     cerr<<"\n";
   }
@@ -140,8 +146,8 @@ bool ProfilePaths::runOnFunction(Function &F){
   //Then we add 2 back edges for it:
   //1. from root->b (in vector stDummy)
   //and 2. from a->exit (in vector exDummy)
-  vector<Edge> stDummy;
-  vector<Edge> exDummy;
+  std::vector<Edge> stDummy;
+  std::vector<Edge> exDummy;
   addDummyEdges(stDummy, exDummy, g, be);
 
 #ifdef DEBUG_PATH_PROFILES
@@ -175,19 +181,17 @@ bool ProfilePaths::runOnFunction(Function &F){
 
 
   if(fr->getParent()->getName() == "main"){
-    //intialize threshold
-    vector<const Type*> initialize_args;
-    initialize_args.push_back(PointerType::get(Type::IntTy));
-    
-    const FunctionType *Fty = FunctionType::get(Type::VoidTy, initialize_args,
-                                                false);
-    Function *initialMeth = fr->getParent()->getParent()->getOrInsertFunction("reoptimizerInitialize", Fty);
-    assert(initialMeth && "Initialize method could not be inserted!");
+    //initialize threshold
+
+    // FIXME: THIS IS HORRIBLY BROKEN.  FUNCTION PASSES CANNOT DO THIS, EXCEPT
+    // IN THEIR INITIALIZE METHOD!!
+    Function *initialize =
+      F.getParent()->getOrInsertFunction("reoptimizerInitialize", Type::VoidTy,
+                                         PointerType::get(Type::IntTy), 0);
     
-    vector<Value *> trargs;
+    std::vector<Value *> trargs;
     trargs.push_back(threshold);
-  
-    new CallInst(initialMeth, trargs, "", fr->begin());
+    new CallInst(initialize, trargs, "", fr->begin());
   }
 
 
@@ -230,7 +234,7 @@ bool ProfilePaths::runOnFunction(Function &F){
   
   // insert initialization code in first (entry) BB
   // this includes initializing r and count
-  insertInTopBB(&F.getEntryNode(),numPaths, rVar, threshold);
+  insertInTopBB(&F.getEntryBlock(), numPaths, rVar, threshold);
     
   //now process the graph: get path numbers,
   //get increments along different paths,
@@ -241,3 +245,5 @@ bool ProfilePaths::runOnFunction(Function &F){
    
   return true;  // Always modifies function
 }
+
+} // End llvm namespace