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 | |
Chris Lattner | 15435fd | 2002-05-07 19:18:48 +0000 | [diff] [blame] | 27 | #include "llvm/Transforms/Utils/UnifyFunctionExitNodes.h" |
Anand Shukla | 70a6138 | 2002-02-26 19:00:48 +0000 | [diff] [blame] | 28 | #include "llvm/Support/CFG.h" |
Chris Lattner | ca14237 | 2002-04-28 19:55:58 +0000 | [diff] [blame] | 29 | #include "llvm/Constants.h" |
Anand Shukla | 70a6138 | 2002-02-26 19:00:48 +0000 | [diff] [blame] | 30 | #include "llvm/DerivedTypes.h" |
| 31 | #include "llvm/iMemory.h" |
Anand Shukla | 2fd8edd | 2003-06-05 06:02:46 +0000 | [diff] [blame] | 32 | #include "llvm/iOperators.h" |
| 33 | #include "llvm/iOther.h" |
Anand Shukla | 77dca14 | 2002-09-20 16:44:35 +0000 | [diff] [blame] | 34 | #include "llvm/Module.h" |
Chris Lattner | 2f04a0d | 2003-01-14 22:33:56 +0000 | [diff] [blame] | 35 | #include "Graph.h" |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 36 | #include <fstream> |
John Criswell | 3ef61af | 2003-06-30 21:59:07 +0000 | [diff] [blame] | 37 | #include "Config/stdio.h" |
Anand Shukla | 70a6138 | 2002-02-26 19:00:48 +0000 | [diff] [blame] | 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 | 40eb9da | 2002-08-08 19:01:28 +0000 | [diff] [blame] | 46 | AU.addRequired<UnifyFunctionExitNodes>(); |
Chris Lattner | d209550 | 2002-02-26 20:04:59 +0000 | [diff] [blame] | 47 | } |
| 48 | }; |
| 49 | |
Chris Lattner | a2c0985 | 2002-07-26 21:12:44 +0000 | [diff] [blame] | 50 | static RegisterOpt<ProfilePaths> X("paths", "Profile Paths"); |
Chris Lattner | b28b680 | 2002-07-23 18:06:35 +0000 | [diff] [blame] | 51 | |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 52 | static Node *findBB(std::vector<Node *> &st, BasicBlock *BB){ |
| 53 | for(std::vector<Node *>::iterator si=st.begin(); si!=st.end(); ++si){ |
Anand Shukla | 70a6138 | 2002-02-26 19:00:48 +0000 | [diff] [blame] | 54 | if(((*si)->getElement())==BB){ |
| 55 | return *si; |
| 56 | } |
| 57 | } |
| 58 | return NULL; |
| 59 | } |
| 60 | |
Chris Lattner | 62b7fd1 | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 61 | //Per function pass for inserting counters and trigger code |
Chris Lattner | 7076ff2 | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 62 | bool ProfilePaths::runOnFunction(Function &F){ |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 63 | |
| 64 | static int mn = -1; |
Anand Shukla | 2fd8edd | 2003-06-05 06:02:46 +0000 | [diff] [blame] | 65 | static int CountCounter = 1; |
Anand Shukla | fd61c60 | 2002-07-18 20:56:47 +0000 | [diff] [blame] | 66 | if(F.isExternal()) { |
Anand Shukla | 11f4262 | 2002-07-08 19:36:01 +0000 | [diff] [blame] | 67 | return false; |
| 68 | } |
| 69 | |
| 70 | //increment counter for instrumented functions. mn is now function# |
| 71 | mn++; |
| 72 | |
Chris Lattner | f998685 | 2002-04-27 07:27:19 +0000 | [diff] [blame] | 73 | // Transform the cfg s.t. we have just one exit node |
Chris Lattner | 10a032a | 2003-09-10 20:35:33 +0000 | [diff] [blame] | 74 | BasicBlock *ExitNode = |
| 75 | getAnalysis<UnifyFunctionExitNodes>().getReturnBlock(); |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 76 | |
| 77 | //iterating over BBs and making graph |
| 78 | std::vector<Node *> nodes; |
| 79 | std::vector<Edge> edges; |
| 80 | |
Anand Shukla | 70a6138 | 2002-02-26 19:00:48 +0000 | [diff] [blame] | 81 | Node *tmp; |
Chris Lattner | a2620ac | 2002-11-09 00:49:43 +0000 | [diff] [blame] | 82 | Node *exitNode = 0, *startNode = 0; |
Anand Shukla | 70a6138 | 2002-02-26 19:00:48 +0000 | [diff] [blame] | 83 | |
Chris Lattner | f998685 | 2002-04-27 07:27:19 +0000 | [diff] [blame] | 84 | // The nodes must be uniquesly identified: |
| 85 | // That is, no two nodes must hav same BB* |
Anand Shukla | 70a6138 | 2002-02-26 19:00:48 +0000 | [diff] [blame] | 86 | |
Chris Lattner | 7076ff2 | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 87 | for (Function::iterator BB = F.begin(), BE = F.end(); BB != BE; ++BB) { |
| 88 | Node *nd=new Node(BB); |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 89 | nodes.push_back(nd); |
Chris Lattner | 7076ff2 | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 90 | if(&*BB == ExitNode) |
Anand Shukla | 70a6138 | 2002-02-26 19:00:48 +0000 | [diff] [blame] | 91 | exitNode=nd; |
Chris Lattner | 889f620 | 2003-04-23 16:37:45 +0000 | [diff] [blame] | 92 | if(BB==F.begin()) |
Anand Shukla | 70a6138 | 2002-02-26 19:00:48 +0000 | [diff] [blame] | 93 | startNode=nd; |
| 94 | } |
| 95 | |
Chris Lattner | f998685 | 2002-04-27 07:27:19 +0000 | [diff] [blame] | 96 | // now do it againto insert edges |
Chris Lattner | 7076ff2 | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 97 | for (Function::iterator BB = F.begin(), BE = F.end(); BB != BE; ++BB){ |
| 98 | Node *nd=findBB(nodes, BB); |
Anand Shukla | 70a6138 | 2002-02-26 19:00:48 +0000 | [diff] [blame] | 99 | assert(nd && "No node for this edge!"); |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 100 | |
Chris Lattner | 7076ff2 | 2002-06-25 16:13:21 +0000 | [diff] [blame] | 101 | for(BasicBlock::succ_iterator s=succ_begin(BB), se=succ_end(BB); |
Anand Shukla | 70a6138 | 2002-02-26 19:00:48 +0000 | [diff] [blame] | 102 | s!=se; ++s){ |
| 103 | Node *nd2=findBB(nodes,*s); |
| 104 | assert(nd2 && "No node for this edge!"); |
| 105 | Edge ed(nd,nd2,0); |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 106 | edges.push_back(ed); |
Anand Shukla | 70a6138 | 2002-02-26 19:00:48 +0000 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | |
| 110 | Graph g(nodes,edges, startNode, exitNode); |
| 111 | |
Anand Shukla | d9a6af0 | 2002-09-16 05:24:49 +0000 | [diff] [blame] | 112 | #ifdef DEBUG_PATH_PROFILES |
| 113 | std::cerr<<"Original graph\n"; |
| 114 | printGraph(g); |
| 115 | #endif |
Anand Shukla | 70a6138 | 2002-02-26 19:00:48 +0000 | [diff] [blame] | 116 | |
Anand Shukla | 11f4262 | 2002-07-08 19:36:01 +0000 | [diff] [blame] | 117 | BasicBlock *fr = &F.front(); |
Anand Shukla | 70a6138 | 2002-02-26 19:00:48 +0000 | [diff] [blame] | 118 | |
Anand Shukla | 11f4262 | 2002-07-08 19:36:01 +0000 | [diff] [blame] | 119 | // The graph is made acyclic: this is done |
| 120 | // by removing back edges for now, and adding them later on |
Chris Lattner | 10a032a | 2003-09-10 20:35:33 +0000 | [diff] [blame] | 121 | std::vector<Edge> be; |
Anand Shukla | fd61c60 | 2002-07-18 20:56:47 +0000 | [diff] [blame] | 122 | std::map<Node *, int> nodePriority; //it ranks nodes in depth first order traversal |
| 123 | g.getBackEdges(be, nodePriority); |
Anand Shukla | d9a6af0 | 2002-09-16 05:24:49 +0000 | [diff] [blame] | 124 | |
| 125 | #ifdef DEBUG_PATH_PROFILES |
| 126 | std::cerr<<"BackEdges-------------\n"; |
Chris Lattner | 10a032a | 2003-09-10 20:35:33 +0000 | [diff] [blame] | 127 | for (std::vector<Edge>::iterator VI=be.begin(); VI!=be.end(); ++VI){ |
Anand Shukla | d9a6af0 | 2002-09-16 05:24:49 +0000 | [diff] [blame] | 128 | printEdge(*VI); |
| 129 | cerr<<"\n"; |
| 130 | } |
| 131 | std::cerr<<"------\n"; |
| 132 | #endif |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 133 | |
| 134 | #ifdef DEBUG_PATH_PROFILES |
Anand Shukla | 11f4262 | 2002-07-08 19:36:01 +0000 | [diff] [blame] | 135 | cerr<<"Backedges:"<<be.size()<<endl; |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 136 | #endif |
Anand Shukla | 11f4262 | 2002-07-08 19:36:01 +0000 | [diff] [blame] | 137 | //Now we need to reflect the effect of back edges |
| 138 | //This is done by adding dummy edges |
| 139 | //If a->b is a back edge |
| 140 | //Then we add 2 back edges for it: |
| 141 | //1. from root->b (in vector stDummy) |
| 142 | //and 2. from a->exit (in vector exDummy) |
Chris Lattner | 10a032a | 2003-09-10 20:35:33 +0000 | [diff] [blame] | 143 | std::vector<Edge> stDummy; |
| 144 | std::vector<Edge> exDummy; |
Anand Shukla | 11f4262 | 2002-07-08 19:36:01 +0000 | [diff] [blame] | 145 | addDummyEdges(stDummy, exDummy, g, be); |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 146 | |
Anand Shukla | d9a6af0 | 2002-09-16 05:24:49 +0000 | [diff] [blame] | 147 | #ifdef DEBUG_PATH_PROFILES |
| 148 | std::cerr<<"After adding dummy edges\n"; |
| 149 | printGraph(g); |
| 150 | #endif |
| 151 | |
Anand Shukla | 11f4262 | 2002-07-08 19:36:01 +0000 | [diff] [blame] | 152 | // Now, every edge in the graph is assigned a weight |
| 153 | // This weight later adds on to assign path |
| 154 | // numbers to different paths in the graph |
| 155 | // All paths for now are acyclic, |
| 156 | // since no back edges in the graph now |
| 157 | // numPaths is the number of acyclic paths in the graph |
Anand Shukla | d9a6af0 | 2002-09-16 05:24:49 +0000 | [diff] [blame] | 158 | int numPaths=valueAssignmentToEdges(g, nodePriority, be); |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 159 | |
Anand Shukla | f8c09ee | 2003-02-14 20:41:53 +0000 | [diff] [blame] | 160 | //if(numPaths<=1) return false; |
| 161 | |
Anand Shukla | 2fd8edd | 2003-06-05 06:02:46 +0000 | [diff] [blame] | 162 | static GlobalVariable *threshold = NULL; |
| 163 | static bool insertedThreshold = false; |
| 164 | |
| 165 | if(!insertedThreshold){ |
| 166 | threshold = new GlobalVariable(Type::IntTy, false, |
| 167 | GlobalValue::ExternalLinkage, 0, |
| 168 | "reopt_threshold"); |
| 169 | |
| 170 | F.getParent()->getGlobalList().push_back(threshold); |
| 171 | insertedThreshold = true; |
| 172 | } |
| 173 | |
| 174 | assert(threshold && "GlobalVariable threshold not defined!"); |
| 175 | |
| 176 | |
| 177 | if(fr->getParent()->getName() == "main"){ |
| 178 | //intialize threshold |
Chris Lattner | 25bc3f8 | 2003-08-31 00:21:59 +0000 | [diff] [blame] | 179 | |
| 180 | // FIXME: THIS IS HORRIBLY BROKEN. FUNCTION PASSES CANNOT DO THIS, EXCEPT |
| 181 | // IN THEIR INITIALIZE METHOD!! |
| 182 | Function *initialize = |
| 183 | F.getParent()->getOrInsertFunction("reoptimizerInitialize", Type::VoidTy, |
| 184 | PointerType::get(Type::IntTy), 0); |
Anand Shukla | 2fd8edd | 2003-06-05 06:02:46 +0000 | [diff] [blame] | 185 | |
Chris Lattner | 10a032a | 2003-09-10 20:35:33 +0000 | [diff] [blame] | 186 | std::vector<Value *> trargs; |
Anand Shukla | 2fd8edd | 2003-06-05 06:02:46 +0000 | [diff] [blame] | 187 | trargs.push_back(threshold); |
Chris Lattner | 25bc3f8 | 2003-08-31 00:21:59 +0000 | [diff] [blame] | 188 | new CallInst(initialize, trargs, "", fr->begin()); |
Anand Shukla | 2fd8edd | 2003-06-05 06:02:46 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | |
Anand Shukla | fd61c60 | 2002-07-18 20:56:47 +0000 | [diff] [blame] | 192 | if(numPaths<=1 || numPaths >5000) return false; |
Anand Shukla | d9a6af0 | 2002-09-16 05:24:49 +0000 | [diff] [blame] | 193 | |
| 194 | #ifdef DEBUG_PATH_PROFILES |
| 195 | printGraph(g); |
| 196 | #endif |
| 197 | |
Anand Shukla | 11f4262 | 2002-07-08 19:36:01 +0000 | [diff] [blame] | 198 | //create instruction allocation r and count |
| 199 | //r is the variable that'll act like an accumulator |
| 200 | //all along the path, we just add edge values to r |
| 201 | //and at the end, r reflects the path number |
| 202 | //count is an array: count[x] would store |
| 203 | //the number of executions of path numbered x |
Anand Shukla | 2190689 | 2002-06-25 21:14:58 +0000 | [diff] [blame] | 204 | |
Anand Shukla | 11f4262 | 2002-07-08 19:36:01 +0000 | [diff] [blame] | 205 | Instruction *rVar=new |
Anand Shukla | d9a6af0 | 2002-09-16 05:24:49 +0000 | [diff] [blame] | 206 | AllocaInst(Type::IntTy, |
Anand Shukla | 11f4262 | 2002-07-08 19:36:01 +0000 | [diff] [blame] | 207 | ConstantUInt::get(Type::UIntTy,1),"R"); |
Anand Shukla | d9a6af0 | 2002-09-16 05:24:49 +0000 | [diff] [blame] | 208 | |
Anand Shukla | f8c09ee | 2003-02-14 20:41:53 +0000 | [diff] [blame] | 209 | //Instruction *countVar=new |
| 210 | //AllocaInst(Type::IntTy, |
| 211 | // ConstantUInt::get(Type::UIntTy, numPaths), "Count"); |
| 212 | |
| 213 | //initialize counter array! |
| 214 | std::vector<Constant*> arrayInitialize; |
| 215 | for(int xi=0; xi<numPaths; xi++) |
| 216 | arrayInitialize.push_back(ConstantSInt::get(Type::IntTy, 0)); |
| 217 | |
Chris Lattner | 379a8d2 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 218 | const ArrayType *ATy = ArrayType::get(Type::IntTy, numPaths); |
| 219 | Constant *initializer = ConstantArray::get(ATy, arrayInitialize); |
Anand Shukla | 2fd8edd | 2003-06-05 06:02:46 +0000 | [diff] [blame] | 220 | char tempChar[20]; |
| 221 | sprintf(tempChar, "Count%d", CountCounter); |
| 222 | CountCounter++; |
| 223 | std::string countStr = tempChar; |
Chris Lattner | 379a8d2 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 224 | GlobalVariable *countVar = new GlobalVariable(ATy, false, |
| 225 | GlobalValue::InternalLinkage, |
Anand Shukla | 2fd8edd | 2003-06-05 06:02:46 +0000 | [diff] [blame] | 226 | initializer, countStr, |
Chris Lattner | 379a8d2 | 2003-04-16 20:28:45 +0000 | [diff] [blame] | 227 | F.getParent()); |
Anand Shukla | 2fd8edd | 2003-06-05 06:02:46 +0000 | [diff] [blame] | 228 | |
Anand Shukla | 11f4262 | 2002-07-08 19:36:01 +0000 | [diff] [blame] | 229 | // insert initialization code in first (entry) BB |
| 230 | // this includes initializing r and count |
Chris Lattner | 5dac64f | 2003-09-20 14:39:18 +0000 | [diff] [blame] | 231 | insertInTopBB(&F.getEntryBlock(), numPaths, rVar, threshold); |
Anand Shukla | 70a6138 | 2002-02-26 19:00:48 +0000 | [diff] [blame] | 232 | |
Anand Shukla | 11f4262 | 2002-07-08 19:36:01 +0000 | [diff] [blame] | 233 | //now process the graph: get path numbers, |
| 234 | //get increments along different paths, |
| 235 | //and assign "increments" and "updates" (to r and count) |
| 236 | //"optimally". Finally, insert llvm code along various edges |
Anand Shukla | 77dca14 | 2002-09-20 16:44:35 +0000 | [diff] [blame] | 237 | processGraph(g, rVar, countVar, be, stDummy, exDummy, numPaths, mn, |
| 238 | threshold); |
Anand Shukla | fd61c60 | 2002-07-18 20:56:47 +0000 | [diff] [blame] | 239 | |
Chris Lattner | 62b7fd1 | 2002-04-07 20:49:59 +0000 | [diff] [blame] | 240 | return true; // Always modifies function |
Anand Shukla | 70a6138 | 2002-02-26 19:00:48 +0000 | [diff] [blame] | 241 | } |