1 //===-- ProfilePaths.cpp - interface to insert instrumentation ---*- C++ -*--=//
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
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.
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).
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 minimum number of edges.
25 //===----------------------------------------------------------------------===//
27 #include "llvm/Transforms/Instrumentation/ProfilePaths.h"
28 #include "llvm/Transforms/Utils/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 "llvm/Transforms/Instrumentation/Graph.h"
39 struct ProfilePaths : public FunctionPass {
40 const char *getPassName() const { return "ProfilePaths"; }
42 bool runOnFunction(Function &F);
44 // Before this pass, make sure that there is only one
45 // entry and only one exit node for the function in the CFG of the function
47 void ProfilePaths::getAnalysisUsage(AnalysisUsage &AU) const {
48 AU.addRequired(UnifyFunctionExitNodes::ID);
52 // createProfilePathsPass - Create a new pass to add path profiling
54 Pass *createProfilePathsPass() {
55 return new ProfilePaths();
59 static Node *findBB(std::vector<Node *> &st, BasicBlock *BB){
60 for(std::vector<Node *>::iterator si=st.begin(); si!=st.end(); ++si){
61 if(((*si)->getElement())==BB){
68 //Per function pass for inserting counters and trigger code
69 bool ProfilePaths::runOnFunction(Function &F){
77 //increment counter for instrumented functions. mn is now function#
80 //std::cerr<<"MN = "<<mn<<"\n";;
83 // Transform the cfg s.t. we have just one exit node
84 BasicBlock *ExitNode = getAnalysis<UnifyFunctionExitNodes>().getExitNode();
86 //iterating over BBs and making graph
87 std::vector<Node *> nodes;
88 std::vector<Edge> edges;
91 Node *exitNode, *startNode;
93 // The nodes must be uniquesly identified:
94 // That is, no two nodes must hav same BB*
96 for (Function::iterator BB = F.begin(), BE = F.end(); BB != BE; ++BB) {
97 Node *nd=new Node(BB);
105 // now do it againto insert edges
106 for (Function::iterator BB = F.begin(), BE = F.end(); BB != BE; ++BB){
107 Node *nd=findBB(nodes, BB);
108 assert(nd && "No node for this edge!");
110 for(BasicBlock::succ_iterator s=succ_begin(BB), se=succ_end(BB);
112 Node *nd2=findBB(nodes,*s);
113 assert(nd2 && "No node for this edge!");
119 Graph g(nodes,edges, startNode, exitNode);
121 #ifdef DEBUG_PATH_PROFILES
122 std::cerr<<"Original graph\n";
126 BasicBlock *fr = &F.front();
128 // The graph is made acyclic: this is done
129 // by removing back edges for now, and adding them later on
133 //std::cerr<<"BackEdges-------------\n";
134 // for(vector<Edge>::iterator VI=be.begin(); VI!=be.end(); ++VI){
138 //std::cerr<<"------\n";
140 #ifdef DEBUG_PATH_PROFILES
141 cerr<<"Backedges:"<<be.size()<<endl;
143 //Now we need to reflect the effect of back edges
144 //This is done by adding dummy edges
145 //If a->b is a back edge
146 //Then we add 2 back edges for it:
147 //1. from root->b (in vector stDummy)
148 //and 2. from a->exit (in vector exDummy)
149 vector<Edge> stDummy;
150 vector<Edge> exDummy;
151 addDummyEdges(stDummy, exDummy, g, be);
153 //std::cerr<<"After adding dummy edges\n";
156 // Now, every edge in the graph is assigned a weight
157 // This weight later adds on to assign path
158 // numbers to different paths in the graph
159 // All paths for now are acyclic,
160 // since no back edges in the graph now
161 // numPaths is the number of acyclic paths in the graph
162 int numPaths=valueAssignmentToEdges(g);
164 //std::cerr<<"Numpaths="<<numPaths<<std::endl;
166 //create instruction allocation r and count
167 //r is the variable that'll act like an accumulator
168 //all along the path, we just add edge values to r
169 //and at the end, r reflects the path number
170 //count is an array: count[x] would store
171 //the number of executions of path numbered x
173 Instruction *rVar=new
174 AllocaInst(PointerType::get(Type::IntTy),
175 ConstantUInt::get(Type::UIntTy,1),"R");
177 Instruction *countVar=new
178 AllocaInst(PointerType::get(Type::IntTy),
179 ConstantUInt::get(Type::UIntTy, numPaths), "Count");
181 // insert initialization code in first (entry) BB
182 // this includes initializing r and count
183 insertInTopBB(&F.getEntryNode(),numPaths, rVar, countVar);
185 //now process the graph: get path numbers,
186 //get increments along different paths,
187 //and assign "increments" and "updates" (to r and count)
188 //"optimally". Finally, insert llvm code along various edges
189 processGraph(g, rVar, countVar, be, stDummy, exDummy, numPaths);
192 static std::ofstream to("paths.sizes");
193 static std::ofstream bbs("paths.look");
194 assert(to && "Cannot open file\n");
195 assert(bbs && "Cannot open file\n");
196 for(int i=0;i<numPaths; ++i){
197 std::vector<BasicBlock *> vBB;
199 getBBtrace(vBB, i, M);
200 //get total size of vector
202 bbs<<"Meth:"<<mn<<" Path:"<<i<<"\n-------------\n";
203 for(vector<BasicBlock *>::iterator VBI=vBB.begin(); VBI!=vBB.end();
209 bbs<<BB->getName()<<"->";
211 bbs<<"\n--------------\n";
212 to<<"::::: "<<mn<<" "<<i<<" "<<size<<"\n";
217 return true; // Always modifies function