Anand Shukla | 70a6138 | 2002-02-26 19:00:48 +0000 | [diff] [blame] | 1 | //===-- ProfilePaths.cpp - interface to insert instrumentation ---*- C++ -*--=// |
| 2 | // |
| 3 | // This inserts intrumentation for counting |
Chris Lattner | 62b7fd1 | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 4 | // execution of paths though a given function |
| 5 | // Its implemented as a "Function" Pass, and called using opt |
Anand Shukla | 70a6138 | 2002-02-26 19:00:48 +0000 | [diff] [blame] | 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 |
Chris Lattner | 62b7fd1 | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 21 | // of a function is identified through a unique number. the code insertion |
Anand Shukla | 70a6138 | 2002-02-26 19:00:48 +0000 | [diff] [blame] | 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 |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 24 | // update can be collapsed into minimum number of edges. |
Anand Shukla | 70a6138 | 2002-02-26 19:00:48 +0000 | [diff] [blame] | 25 | //===----------------------------------------------------------------------===// |
| 26 | |
| 27 | #include "llvm/Transforms/Instrumentation/ProfilePaths.h" |
Chris Lattner | 15435fd | 2002-05-07 19:18:48 +0000 | [diff] [blame] | 28 | #include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h" |
Anand Shukla | 70a6138 | 2002-02-26 19:00:48 +0000 | [diff] [blame] | 29 | #include "llvm/Support/CFG.h" |
Chris Lattner | ca14237 | 2002-04-28 19:55:58 +0000 | [diff] [blame] | 30 | #include "llvm/Constants.h" |
Anand Shukla | 70a6138 | 2002-02-26 19:00:48 +0000 | [diff] [blame] | 31 | #include "llvm/DerivedTypes.h" |
| 32 | #include "llvm/iMemory.h" |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 33 | #include "llvm/Transforms/Instrumentation/Graph.h" |
| 34 | #include <iostream> |
| 35 | #include <fstream> |
Anand Shukla | 70a6138 | 2002-02-26 19:00:48 +0000 | [diff] [blame] | 36 | |
| 37 | using std::vector; |
| 38 | |
Chris Lattner | 37104aa | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 39 | struct ProfilePaths : public FunctionPass { |
Chris Lattner | 7076ff2 | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 40 | bool runOnFunction(Function &F); |
Chris Lattner | d209550 | 2002-02-26 20:04:59 +0000 | [diff] [blame] | 41 | |
| 42 | // Before this pass, make sure that there is only one |
Chris Lattner | 62b7fd1 | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 43 | // entry and only one exit node for the function in the CFG of the function |
Chris Lattner | d209550 | 2002-02-26 20:04:59 +0000 | [diff] [blame] | 44 | // |
Chris Lattner | c8e6654 | 2002-04-27 06:56:12 +0000 | [diff] [blame] | 45 | void ProfilePaths::getAnalysisUsage(AnalysisUsage &AU) const { |
Chris Lattner | f998685 | 2002-04-27 07:27:19 +0000 | [diff] [blame] | 46 | AU.addRequired(UnifyFunctionExitNodes::ID); |
Chris Lattner | d209550 | 2002-02-26 20:04:59 +0000 | [diff] [blame] | 47 | } |
| 48 | }; |
| 49 | |
Chris Lattner | b28b680 | 2002-07-23 18:06:35 +0000 | [diff] [blame^] | 50 | static RegisterPass<ProfilePaths> X("paths", "Profile Paths"); |
| 51 | |
Chris Lattner | d209550 | 2002-02-26 20:04:59 +0000 | [diff] [blame] | 52 | // createProfilePathsPass - Create a new pass to add path profiling |
| 53 | // |
| 54 | Pass *createProfilePathsPass() { |
| 55 | return new ProfilePaths(); |
| 56 | } |
| 57 | |
| 58 | |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 59 | static Node *findBB(std::vector<Node *> &st, BasicBlock *BB){ |
| 60 | for(std::vector<Node *>::iterator si=st.begin(); si!=st.end(); ++si){ |
Anand Shukla | 70a6138 | 2002-02-26 19:00:48 +0000 | [diff] [blame] | 61 | if(((*si)->getElement())==BB){ |
| 62 | return *si; |
| 63 | } |
| 64 | } |
| 65 | return NULL; |
| 66 | } |
| 67 | |
Chris Lattner | 62b7fd1 | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 68 | //Per function pass for inserting counters and trigger code |
Chris Lattner | 7076ff2 | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 69 | bool ProfilePaths::runOnFunction(Function &F){ |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 70 | |
| 71 | static int mn = -1; |
Anand Shukla | 11f4262 | 2002-07-08 19:36:01 +0000 | [diff] [blame] | 72 | |
Anand Shukla | fd61c60 | 2002-07-18 20:56:47 +0000 | [diff] [blame] | 73 | if(F.isExternal()) { |
Anand Shukla | 11f4262 | 2002-07-08 19:36:01 +0000 | [diff] [blame] | 74 | return false; |
| 75 | } |
| 76 | |
Anand Shukla | fd61c60 | 2002-07-18 20:56:47 +0000 | [diff] [blame] | 77 | //std::cerr<<"Instrumenting\n-----------------\n"; |
| 78 | //std::cerr<<F; |
Anand Shukla | 11f4262 | 2002-07-08 19:36:01 +0000 | [diff] [blame] | 79 | //increment counter for instrumented functions. mn is now function# |
| 80 | mn++; |
| 81 | |
| 82 | //std::cerr<<"MN = "<<mn<<"\n";; |
| 83 | //std::cerr<<F; |
| 84 | |
Chris Lattner | f998685 | 2002-04-27 07:27:19 +0000 | [diff] [blame] | 85 | // Transform the cfg s.t. we have just one exit node |
| 86 | BasicBlock *ExitNode = getAnalysis<UnifyFunctionExitNodes>().getExitNode(); |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 87 | |
| 88 | //iterating over BBs and making graph |
| 89 | std::vector<Node *> nodes; |
| 90 | std::vector<Edge> edges; |
| 91 | |
Anand Shukla | 70a6138 | 2002-02-26 19:00:48 +0000 | [diff] [blame] | 92 | Node *tmp; |
| 93 | Node *exitNode, *startNode; |
| 94 | |
Chris Lattner | f998685 | 2002-04-27 07:27:19 +0000 | [diff] [blame] | 95 | // The nodes must be uniquesly identified: |
| 96 | // That is, no two nodes must hav same BB* |
Anand Shukla | 70a6138 | 2002-02-26 19:00:48 +0000 | [diff] [blame] | 97 | |
Chris Lattner | 7076ff2 | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 98 | for (Function::iterator BB = F.begin(), BE = F.end(); BB != BE; ++BB) { |
| 99 | Node *nd=new Node(BB); |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 100 | nodes.push_back(nd); |
Chris Lattner | 7076ff2 | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 101 | if(&*BB == ExitNode) |
Anand Shukla | 70a6138 | 2002-02-26 19:00:48 +0000 | [diff] [blame] | 102 | exitNode=nd; |
Chris Lattner | 7076ff2 | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 103 | if(&*BB==F.begin()) |
Anand Shukla | 70a6138 | 2002-02-26 19:00:48 +0000 | [diff] [blame] | 104 | startNode=nd; |
| 105 | } |
| 106 | |
Chris Lattner | f998685 | 2002-04-27 07:27:19 +0000 | [diff] [blame] | 107 | // now do it againto insert edges |
Chris Lattner | 7076ff2 | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 108 | for (Function::iterator BB = F.begin(), BE = F.end(); BB != BE; ++BB){ |
| 109 | Node *nd=findBB(nodes, BB); |
Anand Shukla | 70a6138 | 2002-02-26 19:00:48 +0000 | [diff] [blame] | 110 | assert(nd && "No node for this edge!"); |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 111 | |
Chris Lattner | 7076ff2 | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 112 | for(BasicBlock::succ_iterator s=succ_begin(BB), se=succ_end(BB); |
Anand Shukla | 70a6138 | 2002-02-26 19:00:48 +0000 | [diff] [blame] | 113 | s!=se; ++s){ |
| 114 | Node *nd2=findBB(nodes,*s); |
| 115 | assert(nd2 && "No node for this edge!"); |
| 116 | Edge ed(nd,nd2,0); |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 117 | edges.push_back(ed); |
Anand Shukla | 70a6138 | 2002-02-26 19:00:48 +0000 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
| 121 | Graph g(nodes,edges, startNode, exitNode); |
| 122 | |
Anand Shukla | fd61c60 | 2002-07-18 20:56:47 +0000 | [diff] [blame] | 123 | //#ifdef DEBUG_PATH_PROFILES |
| 124 | //std::cerr<<"Original graph\n"; |
| 125 | //printGraph(g); |
| 126 | //#endif |
Anand Shukla | 70a6138 | 2002-02-26 19:00:48 +0000 | [diff] [blame] | 127 | |
Anand Shukla | 11f4262 | 2002-07-08 19:36:01 +0000 | [diff] [blame] | 128 | BasicBlock *fr = &F.front(); |
Anand Shukla | 70a6138 | 2002-02-26 19:00:48 +0000 | [diff] [blame] | 129 | |
Anand Shukla | 11f4262 | 2002-07-08 19:36:01 +0000 | [diff] [blame] | 130 | // The graph is made acyclic: this is done |
| 131 | // by removing back edges for now, and adding them later on |
| 132 | vector<Edge> be; |
Anand Shukla | fd61c60 | 2002-07-18 20:56:47 +0000 | [diff] [blame] | 133 | std::map<Node *, int> nodePriority; //it ranks nodes in depth first order traversal |
| 134 | g.getBackEdges(be, nodePriority); |
| 135 | /* |
| 136 | std::cerr<<"Node priority--------------\n"; |
| 137 | for(std::map<Node *, int>::iterator MI = nodePriority.begin(), |
| 138 | ME = nodePriority.end(); MI!=ME; ++MI) |
| 139 | std::cerr<<MI->first->getElement()->getName()<<"->"<<MI->second<<"\n"; |
| 140 | std::cerr<<"End Node priority--------------\n"; |
| 141 | */ |
Anand Shukla | 11f4262 | 2002-07-08 19:36:01 +0000 | [diff] [blame] | 142 | //std::cerr<<"BackEdges-------------\n"; |
| 143 | // for(vector<Edge>::iterator VI=be.begin(); VI!=be.end(); ++VI){ |
| 144 | //printEdge(*VI); |
| 145 | //cerr<<"\n"; |
| 146 | //} |
| 147 | //std::cerr<<"------\n"; |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 148 | |
| 149 | #ifdef DEBUG_PATH_PROFILES |
Anand Shukla | 11f4262 | 2002-07-08 19:36:01 +0000 | [diff] [blame] | 150 | cerr<<"Backedges:"<<be.size()<<endl; |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 151 | #endif |
Anand Shukla | 11f4262 | 2002-07-08 19:36:01 +0000 | [diff] [blame] | 152 | //Now we need to reflect the effect of back edges |
| 153 | //This is done by adding dummy edges |
| 154 | //If a->b is a back edge |
| 155 | //Then we add 2 back edges for it: |
| 156 | //1. from root->b (in vector stDummy) |
| 157 | //and 2. from a->exit (in vector exDummy) |
| 158 | vector<Edge> stDummy; |
| 159 | vector<Edge> exDummy; |
| 160 | addDummyEdges(stDummy, exDummy, g, be); |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 161 | |
Anand Shukla | 11f4262 | 2002-07-08 19:36:01 +0000 | [diff] [blame] | 162 | //std::cerr<<"After adding dummy edges\n"; |
| 163 | //printGraph(g); |
Anand Shukla | 70a6138 | 2002-02-26 19:00:48 +0000 | [diff] [blame] | 164 | |
Anand Shukla | 11f4262 | 2002-07-08 19:36:01 +0000 | [diff] [blame] | 165 | // Now, every edge in the graph is assigned a weight |
| 166 | // This weight later adds on to assign path |
| 167 | // numbers to different paths in the graph |
| 168 | // All paths for now are acyclic, |
| 169 | // since no back edges in the graph now |
| 170 | // numPaths is the number of acyclic paths in the graph |
Anand Shukla | fd61c60 | 2002-07-18 20:56:47 +0000 | [diff] [blame] | 171 | int numPaths=valueAssignmentToEdges(g, nodePriority); |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 172 | |
Anand Shukla | fd61c60 | 2002-07-18 20:56:47 +0000 | [diff] [blame] | 173 | if(numPaths<=1 || numPaths >5000) return false; |
Anand Shukla | 11f4262 | 2002-07-08 19:36:01 +0000 | [diff] [blame] | 174 | //std::cerr<<"Numpaths="<<numPaths<<std::endl; |
| 175 | //printGraph(g); |
| 176 | //create instruction allocation r and count |
| 177 | //r is the variable that'll act like an accumulator |
| 178 | //all along the path, we just add edge values to r |
| 179 | //and at the end, r reflects the path number |
| 180 | //count is an array: count[x] would store |
| 181 | //the number of executions of path numbered x |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 182 | |
Anand Shukla | 11f4262 | 2002-07-08 19:36:01 +0000 | [diff] [blame] | 183 | Instruction *rVar=new |
| 184 | AllocaInst(PointerType::get(Type::IntTy), |
| 185 | ConstantUInt::get(Type::UIntTy,1),"R"); |
Anand Shukla | 70a6138 | 2002-02-26 19:00:48 +0000 | [diff] [blame] | 186 | |
Anand Shukla | 11f4262 | 2002-07-08 19:36:01 +0000 | [diff] [blame] | 187 | Instruction *countVar=new |
| 188 | AllocaInst(PointerType::get(Type::IntTy), |
| 189 | ConstantUInt::get(Type::UIntTy, numPaths), "Count"); |
Anand Shukla | 70a6138 | 2002-02-26 19:00:48 +0000 | [diff] [blame] | 190 | |
Anand Shukla | 11f4262 | 2002-07-08 19:36:01 +0000 | [diff] [blame] | 191 | // insert initialization code in first (entry) BB |
| 192 | // this includes initializing r and count |
| 193 | insertInTopBB(&F.getEntryNode(),numPaths, rVar, countVar); |
Anand Shukla | 70a6138 | 2002-02-26 19:00:48 +0000 | [diff] [blame] | 194 | |
Anand Shukla | 11f4262 | 2002-07-08 19:36:01 +0000 | [diff] [blame] | 195 | //now process the graph: get path numbers, |
| 196 | //get increments along different paths, |
| 197 | //and assign "increments" and "updates" (to r and count) |
| 198 | //"optimally". Finally, insert llvm code along various edges |
Anand Shukla | fd61c60 | 2002-07-18 20:56:47 +0000 | [diff] [blame] | 199 | processGraph(g, rVar, countVar, be, stDummy, exDummy, numPaths, mn); |
| 200 | |
Chris Lattner | 62b7fd1 | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 201 | return true; // Always modifies function |
Anand Shukla | 70a6138 | 2002-02-26 19:00:48 +0000 | [diff] [blame] | 202 | } |