Eliminate duplicate or unneccesary #include's
[oota-llvm.git] / lib / Transforms / Instrumentation / ProfilePaths / ProfilePaths.cpp
1 //===-- ProfilePaths.cpp - interface to insert instrumentation ---*- C++ -*--=//
2 //
3 // This inserts intrumentation for counting
4 // execution of paths though a given function
5 // Its implemented as a "Function" Pass, and called using opt
6 //
7 // This pass is implemented by using algorithms similar to 
8 // 1."Efficient Path Profiling": Ball, T. and Larus, J. R., 
9 // Proceedings of Micro-29, Dec 1996, Paris, France.
10 // 2."Efficiently Counting Program events with support for on-line
11 //   "queries": Ball T., ACM Transactions on Programming Languages
12 //   and systems, Sep 1994.
13 //
14 // The algorithms work on a Graph constructed over the nodes
15 // made from Basic Blocks: The transformations then take place on
16 // the constucted graph (implementation in Graph.cpp and GraphAuxillary.cpp)
17 // and finally, appropriate instrumentation is placed over suitable edges.
18 // (code inserted through EdgeCode.cpp).
19 // 
20 // The algorithm inserts code such that every acyclic path in the CFG
21 // of a function is identified through a unique number. the code insertion
22 // is optimal in the sense that its inserted over a minimal set of edges. Also,
23 // the algorithm makes sure than initialization, path increment and counter
24 // update can be collapsed into minmimum number of edges.
25 //===----------------------------------------------------------------------===//
26
27 #include "llvm/Transforms/Instrumentation/ProfilePaths.h"
28 #include "llvm/Transforms/UnifyFunctionExitNodes.h"
29 #include "llvm/Support/CFG.h"
30 #include "llvm/Constants.h"
31 #include "llvm/DerivedTypes.h"
32 #include "llvm/iMemory.h"
33 #include "Graph.h"
34
35 using std::vector;
36
37 struct ProfilePaths : public FunctionPass {
38   const char *getPassName() const { return "ProfilePaths"; }
39
40   bool runOnFunction(Function *F);
41
42   // Before this pass, make sure that there is only one 
43   // entry and only one exit node for the function in the CFG of the function
44   //
45   void ProfilePaths::getAnalysisUsage(AnalysisUsage &AU) const {
46     AU.addRequired(UnifyFunctionExitNodes::ID);
47   }
48 };
49
50 // createProfilePathsPass - Create a new pass to add path profiling
51 //
52 Pass *createProfilePathsPass() {
53   return new ProfilePaths();
54 }
55
56
57 static Node *findBB(std::set<Node *> &st, BasicBlock *BB){
58   for(std::set<Node *>::iterator si=st.begin(); si!=st.end(); ++si){
59     if(((*si)->getElement())==BB){
60       return *si;
61     }
62   }
63   return NULL;
64 }
65
66 //Per function pass for inserting counters and trigger code
67 bool ProfilePaths::runOnFunction(Function *M){
68   // Transform the cfg s.t. we have just one exit node
69   BasicBlock *ExitNode = getAnalysis<UnifyFunctionExitNodes>().getExitNode();  
70   
71   // iterating over BBs and making graph
72   std::set<Node *> nodes;
73   std::set<Edge> edges;
74   Node *tmp;
75   Node *exitNode, *startNode;
76
77   // The nodes must be uniquesly identified:
78   // That is, no two nodes must hav same BB*
79   
80   // First enter just nodes: later enter edges
81   for (Function::iterator BB = M->begin(), BE=M->end(); BB != BE; ++BB){
82     Node *nd=new Node(*BB);
83     nodes.insert(nd); 
84     if(*BB==ExitNode)
85       exitNode=nd;
86     if(*BB==M->front())
87       startNode=nd;
88   }
89
90   // now do it againto insert edges
91   for (Function::iterator BB = M->begin(), BE=M->end(); BB != BE; ++BB){
92     Node *nd=findBB(nodes, *BB);
93     assert(nd && "No node for this edge!");
94     for(BasicBlock::succ_iterator s=succ_begin(*BB), se=succ_end(*BB); 
95         s!=se; ++s){
96       Node *nd2=findBB(nodes,*s);
97       assert(nd2 && "No node for this edge!");
98       Edge ed(nd,nd2,0);
99       edges.insert(ed);
100     }
101   }
102   
103   Graph g(nodes,edges, startNode, exitNode);
104
105 #ifdef DEBUG_PATH_PROFILES  
106   printGraph(g);
107 #endif
108
109   BasicBlock *fr=M->front();
110   
111   // If only one BB, don't instrument
112   if (M->getBasicBlocks().size() == 1) {    
113     // The graph is made acyclic: this is done
114     // by removing back edges for now, and adding them later on
115     vector<Edge> be;
116     g.getBackEdges(be);
117 #ifdef DEBUG_PATH_PROFILES
118     cerr<<"Backedges:"<<be.size()<<endl;
119 #endif
120     // Now we need to reflect the effect of back edges
121     // This is done by adding dummy edges
122     // If a->b is a back edge
123     // Then we add 2 back edges for it:
124     // 1. from root->b (in vector stDummy)
125     // and 2. from a->exit (in vector exDummy)
126     vector<Edge> stDummy;
127     vector<Edge> exDummy;
128     addDummyEdges(stDummy, exDummy, g, be);
129     
130     // Now, every edge in the graph is assigned a weight
131     // This weight later adds on to assign path
132     // numbers to different paths in the graph
133     //  All paths for now are acyclic,
134     // since no back edges in the graph now
135     // numPaths is the number of acyclic paths in the graph
136     int numPaths=valueAssignmentToEdges(g);
137     
138     // create instruction allocation r and count
139     // r is the variable that'll act like an accumulator
140     // all along the path, we just add edge values to r
141     // and at the end, r reflects the path number
142     // count is an array: count[x] would store
143     // the number of executions of path numbered x
144     Instruction *rVar=new 
145       AllocaInst(PointerType::get(Type::IntTy), 
146                  ConstantUInt::get(Type::UIntTy,1),"R");
147     
148     Instruction *countVar=new 
149       AllocaInst(PointerType::get(Type::IntTy), 
150                  ConstantUInt::get(Type::UIntTy, numPaths), "Count");
151     
152     // insert initialization code in first (entry) BB
153     // this includes initializing r and count
154     insertInTopBB(M->getEntryNode(),numPaths, rVar, countVar);
155     
156     // now process the graph: get path numbers,
157     // get increments along different paths,
158     // and assign "increments" and "updates" (to r and count)
159     // "optimally". Finally, insert llvm code along various edges
160     processGraph(g, rVar, countVar, be, stDummy, exDummy);
161   }
162
163   return true;  // Always modifies function
164 }