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 |
| 24 | // update can be collapsed into minmimum number of edges. |
| 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" |
| 33 | #include "Graph.h" |
| 34 | |
| 35 | using std::vector; |
| 36 | |
Chris Lattner | 37104aa | 2002-04-29 14:57:45 +0000 | [diff] [blame] | 37 | struct ProfilePaths : public FunctionPass { |
| 38 | const char *getPassName() const { return "ProfilePaths"; } |
| 39 | |
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 | |
| 50 | // createProfilePathsPass - Create a new pass to add path profiling |
| 51 | // |
| 52 | Pass *createProfilePathsPass() { |
| 53 | return new ProfilePaths(); |
| 54 | } |
| 55 | |
| 56 | |
Chris Lattner | 5328c6f | 2002-02-26 19:40:28 +0000 | [diff] [blame] | 57 | static Node *findBB(std::set<Node *> &st, BasicBlock *BB){ |
| 58 | for(std::set<Node *>::iterator si=st.begin(); si!=st.end(); ++si){ |
Anand Shukla | 70a6138 | 2002-02-26 19:00:48 +0000 | [diff] [blame] | 59 | if(((*si)->getElement())==BB){ |
| 60 | return *si; |
| 61 | } |
| 62 | } |
| 63 | return NULL; |
| 64 | } |
| 65 | |
Chris Lattner | 62b7fd1 | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 66 | //Per function pass for inserting counters and trigger code |
Chris Lattner | 7076ff2 | 2002-06-25 16:13:21 +0000 | [diff] [blame^] | 67 | bool ProfilePaths::runOnFunction(Function &F){ |
Chris Lattner | f998685 | 2002-04-27 07:27:19 +0000 | [diff] [blame] | 68 | // Transform the cfg s.t. we have just one exit node |
| 69 | BasicBlock *ExitNode = getAnalysis<UnifyFunctionExitNodes>().getExitNode(); |
Anand Shukla | 70a6138 | 2002-02-26 19:00:48 +0000 | [diff] [blame] | 70 | |
Chris Lattner | f998685 | 2002-04-27 07:27:19 +0000 | [diff] [blame] | 71 | // iterating over BBs and making graph |
Anand Shukla | 70a6138 | 2002-02-26 19:00:48 +0000 | [diff] [blame] | 72 | std::set<Node *> nodes; |
| 73 | std::set<Edge> edges; |
| 74 | Node *tmp; |
| 75 | Node *exitNode, *startNode; |
| 76 | |
Chris Lattner | f998685 | 2002-04-27 07:27:19 +0000 | [diff] [blame] | 77 | // The nodes must be uniquesly identified: |
| 78 | // That is, no two nodes must hav same BB* |
Anand Shukla | 70a6138 | 2002-02-26 19:00:48 +0000 | [diff] [blame] | 79 | |
Chris Lattner | f998685 | 2002-04-27 07:27:19 +0000 | [diff] [blame] | 80 | // First enter just nodes: later enter edges |
Chris Lattner | 7076ff2 | 2002-06-25 16:13:21 +0000 | [diff] [blame^] | 81 | for (Function::iterator BB = F.begin(), BE = F.end(); BB != BE; ++BB) { |
| 82 | Node *nd=new Node(BB); |
Anand Shukla | 70a6138 | 2002-02-26 19:00:48 +0000 | [diff] [blame] | 83 | nodes.insert(nd); |
Chris Lattner | 7076ff2 | 2002-06-25 16:13:21 +0000 | [diff] [blame^] | 84 | if(&*BB == ExitNode) |
Anand Shukla | 70a6138 | 2002-02-26 19:00:48 +0000 | [diff] [blame] | 85 | exitNode=nd; |
Chris Lattner | 7076ff2 | 2002-06-25 16:13:21 +0000 | [diff] [blame^] | 86 | if(&*BB==F.begin()) |
Anand Shukla | 70a6138 | 2002-02-26 19:00:48 +0000 | [diff] [blame] | 87 | startNode=nd; |
| 88 | } |
| 89 | |
Chris Lattner | f998685 | 2002-04-27 07:27:19 +0000 | [diff] [blame] | 90 | // now do it againto insert edges |
Chris Lattner | 7076ff2 | 2002-06-25 16:13:21 +0000 | [diff] [blame^] | 91 | for (Function::iterator BB = F.begin(), BE = F.end(); BB != BE; ++BB){ |
| 92 | Node *nd=findBB(nodes, BB); |
Anand Shukla | 70a6138 | 2002-02-26 19:00:48 +0000 | [diff] [blame] | 93 | assert(nd && "No node for this edge!"); |
Chris Lattner | 7076ff2 | 2002-06-25 16:13:21 +0000 | [diff] [blame^] | 94 | for(BasicBlock::succ_iterator s=succ_begin(BB), se=succ_end(BB); |
Anand Shukla | 70a6138 | 2002-02-26 19:00:48 +0000 | [diff] [blame] | 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 | |
Chris Lattner | e1fc2d9 | 2002-05-22 21:56:32 +0000 | [diff] [blame] | 105 | DEBUG(printGraph(g)); |
Anand Shukla | 70a6138 | 2002-02-26 19:00:48 +0000 | [diff] [blame] | 106 | |
Chris Lattner | 7076ff2 | 2002-06-25 16:13:21 +0000 | [diff] [blame^] | 107 | BasicBlock *fr=&F.front(); |
Anand Shukla | 70a6138 | 2002-02-26 19:00:48 +0000 | [diff] [blame] | 108 | |
Chris Lattner | f998685 | 2002-04-27 07:27:19 +0000 | [diff] [blame] | 109 | // If only one BB, don't instrument |
Chris Lattner | 7076ff2 | 2002-06-25 16:13:21 +0000 | [diff] [blame^] | 110 | if (++F.begin() == F.end()) { |
Chris Lattner | f998685 | 2002-04-27 07:27:19 +0000 | [diff] [blame] | 111 | // The graph is made acyclic: this is done |
| 112 | // by removing back edges for now, and adding them later on |
Chris Lattner | 5328c6f | 2002-02-26 19:40:28 +0000 | [diff] [blame] | 113 | vector<Edge> be; |
Anand Shukla | 70a6138 | 2002-02-26 19:00:48 +0000 | [diff] [blame] | 114 | g.getBackEdges(be); |
Chris Lattner | e1fc2d9 | 2002-05-22 21:56:32 +0000 | [diff] [blame] | 115 | DEBUG(cerr << "Backedges:" << be.size() << "\n"); |
| 116 | |
Chris Lattner | f998685 | 2002-04-27 07:27:19 +0000 | [diff] [blame] | 117 | // Now we need to reflect the effect of back edges |
| 118 | // This is done by adding dummy edges |
| 119 | // If a->b is a back edge |
| 120 | // Then we add 2 back edges for it: |
| 121 | // 1. from root->b (in vector stDummy) |
| 122 | // and 2. from a->exit (in vector exDummy) |
Chris Lattner | 5328c6f | 2002-02-26 19:40:28 +0000 | [diff] [blame] | 123 | vector<Edge> stDummy; |
| 124 | vector<Edge> exDummy; |
Anand Shukla | 70a6138 | 2002-02-26 19:00:48 +0000 | [diff] [blame] | 125 | addDummyEdges(stDummy, exDummy, g, be); |
| 126 | |
Chris Lattner | f998685 | 2002-04-27 07:27:19 +0000 | [diff] [blame] | 127 | // Now, every edge in the graph is assigned a weight |
| 128 | // This weight later adds on to assign path |
| 129 | // numbers to different paths in the graph |
| 130 | // All paths for now are acyclic, |
| 131 | // since no back edges in the graph now |
| 132 | // numPaths is the number of acyclic paths in the graph |
Anand Shukla | 70a6138 | 2002-02-26 19:00:48 +0000 | [diff] [blame] | 133 | int numPaths=valueAssignmentToEdges(g); |
| 134 | |
Chris Lattner | f998685 | 2002-04-27 07:27:19 +0000 | [diff] [blame] | 135 | // create instruction allocation r and count |
| 136 | // r is the variable that'll act like an accumulator |
| 137 | // all along the path, we just add edge values to r |
| 138 | // and at the end, r reflects the path number |
| 139 | // count is an array: count[x] would store |
| 140 | // the number of executions of path numbered x |
Anand Shukla | 70a6138 | 2002-02-26 19:00:48 +0000 | [diff] [blame] | 141 | Instruction *rVar=new |
| 142 | AllocaInst(PointerType::get(Type::IntTy), |
| 143 | ConstantUInt::get(Type::UIntTy,1),"R"); |
| 144 | |
| 145 | Instruction *countVar=new |
| 146 | AllocaInst(PointerType::get(Type::IntTy), |
| 147 | ConstantUInt::get(Type::UIntTy, numPaths), "Count"); |
| 148 | |
Chris Lattner | f998685 | 2002-04-27 07:27:19 +0000 | [diff] [blame] | 149 | // insert initialization code in first (entry) BB |
| 150 | // this includes initializing r and count |
Chris Lattner | 7076ff2 | 2002-06-25 16:13:21 +0000 | [diff] [blame^] | 151 | insertInTopBB(&F.getEntryNode(),numPaths, rVar, countVar); |
Anand Shukla | 70a6138 | 2002-02-26 19:00:48 +0000 | [diff] [blame] | 152 | |
Chris Lattner | f998685 | 2002-04-27 07:27:19 +0000 | [diff] [blame] | 153 | // now process the graph: get path numbers, |
| 154 | // get increments along different paths, |
| 155 | // and assign "increments" and "updates" (to r and count) |
| 156 | // "optimally". Finally, insert llvm code along various edges |
Anand Shukla | 70a6138 | 2002-02-26 19:00:48 +0000 | [diff] [blame] | 157 | processGraph(g, rVar, countVar, be, stDummy, exDummy); |
| 158 | } |
| 159 | |
Chris Lattner | 62b7fd1 | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 160 | return true; // Always modifies function |
Anand Shukla | 70a6138 | 2002-02-26 19:00:48 +0000 | [diff] [blame] | 161 | } |