Andreas Neustifter | f771dae | 2009-09-01 19:03:44 +0000 | [diff] [blame] | 1 | //===- OptimalEdgeProfiling.cpp - Insert counters for opt. edge profiling -===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This pass instruments the specified program with counters for edge profiling. |
| 11 | // Edge profiling can give a reasonable approximation of the hot paths through a |
| 12 | // program, and is used for a wide variety of program transformations. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | #define DEBUG_TYPE "insert-optimal-edge-profiling" |
| 16 | #include "ProfilingUtils.h" |
Andreas Neustifter | f771dae | 2009-09-01 19:03:44 +0000 | [diff] [blame] | 17 | #include "llvm/Module.h" |
| 18 | #include "llvm/Pass.h" |
| 19 | #include "llvm/Analysis/Passes.h" |
Andreas Neustifter | ed1ac4a | 2009-09-04 12:34:44 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/ProfileInfo.h" |
Andreas Neustifter | 9233272 | 2009-09-16 11:35:50 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/ProfileInfoLoader.h" |
Andreas Neustifter | 9341cdc | 2009-09-02 12:38:39 +0000 | [diff] [blame] | 22 | #include "llvm/Support/raw_ostream.h" |
Andreas Neustifter | f771dae | 2009-09-01 19:03:44 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Debug.h" |
| 24 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 25 | #include "llvm/Transforms/Instrumentation.h" |
Andreas Neustifter | 3985943 | 2009-09-03 08:52:52 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/DenseSet.h" |
Andreas Neustifter | f771dae | 2009-09-01 19:03:44 +0000 | [diff] [blame] | 27 | #include "llvm/ADT/Statistic.h" |
| 28 | #include "MaximumSpanningTree.h" |
| 29 | #include <set> |
| 30 | using namespace llvm; |
| 31 | |
| 32 | STATISTIC(NumEdgesInserted, "The # of edges inserted."); |
| 33 | |
| 34 | namespace { |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 35 | class OptimalEdgeProfiler : public ModulePass { |
Andreas Neustifter | f771dae | 2009-09-01 19:03:44 +0000 | [diff] [blame] | 36 | bool runOnModule(Module &M); |
Andreas Neustifter | f771dae | 2009-09-01 19:03:44 +0000 | [diff] [blame] | 37 | public: |
| 38 | static char ID; // Pass identification, replacement for typeid |
| 39 | OptimalEdgeProfiler() : ModulePass(&ID) {} |
| 40 | |
| 41 | void getAnalysisUsage(AnalysisUsage &AU) const { |
| 42 | AU.addRequiredID(ProfileEstimatorPassID); |
| 43 | AU.addRequired<ProfileInfo>(); |
| 44 | } |
| 45 | |
| 46 | virtual const char *getPassName() const { |
| 47 | return "Optimal Edge Profiler"; |
| 48 | } |
| 49 | }; |
| 50 | } |
| 51 | |
| 52 | char OptimalEdgeProfiler::ID = 0; |
| 53 | static RegisterPass<OptimalEdgeProfiler> |
| 54 | X("insert-optimal-edge-profiling", |
| 55 | "Insert optimal instrumentation for edge profiling"); |
| 56 | |
| 57 | ModulePass *llvm::createOptimalEdgeProfilerPass() { |
| 58 | return new OptimalEdgeProfiler(); |
| 59 | } |
| 60 | |
| 61 | inline static void printEdgeCounter(ProfileInfo::Edge e, |
| 62 | BasicBlock* b, |
| 63 | unsigned i) { |
David Greene | 0b9afb4 | 2010-01-05 01:27:01 +0000 | [diff] [blame] | 64 | DEBUG(dbgs() << "--Edge Counter for " << (e) << " in " \ |
Andreas Neustifter | f771dae | 2009-09-01 19:03:44 +0000 | [diff] [blame] | 65 | << ((b)?(b)->getNameStr():"0") << " (# " << (i) << ")\n"); |
| 66 | } |
| 67 | |
| 68 | bool OptimalEdgeProfiler::runOnModule(Module &M) { |
| 69 | Function *Main = M.getFunction("main"); |
| 70 | if (Main == 0) { |
| 71 | errs() << "WARNING: cannot insert edge profiling into a module" |
| 72 | << " with no main function!\n"; |
| 73 | return false; // No main, no instrumentation! |
| 74 | } |
| 75 | |
Andreas Neustifter | 9341cdc | 2009-09-02 12:38:39 +0000 | [diff] [blame] | 76 | // NumEdges counts all the edges that may be instrumented. Later on its |
| 77 | // decided which edges to actually instrument, to achieve optimal profiling. |
| 78 | // For the entry block a virtual edge (0,entry) is reserved, for each block |
| 79 | // with no successors an edge (BB,0) is reserved. These edges are necessary |
| 80 | // to calculate a truly optimal maximum spanning tree and thus an optimal |
| 81 | // instrumentation. |
Andreas Neustifter | f771dae | 2009-09-01 19:03:44 +0000 | [diff] [blame] | 82 | unsigned NumEdges = 0; |
Andreas Neustifter | 9341cdc | 2009-09-02 12:38:39 +0000 | [diff] [blame] | 83 | |
Andreas Neustifter | f771dae | 2009-09-01 19:03:44 +0000 | [diff] [blame] | 84 | for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) { |
| 85 | if (F->isDeclaration()) continue; |
| 86 | // Reserve space for (0,entry) edge. |
| 87 | ++NumEdges; |
| 88 | for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) { |
| 89 | // Keep track of which blocks need to be instrumented. We don't want to |
| 90 | // instrument blocks that are added as the result of breaking critical |
| 91 | // edges! |
Andreas Neustifter | f771dae | 2009-09-01 19:03:44 +0000 | [diff] [blame] | 92 | if (BB->getTerminator()->getNumSuccessors() == 0) { |
| 93 | // Reserve space for (BB,0) edge. |
| 94 | ++NumEdges; |
| 95 | } else { |
| 96 | NumEdges += BB->getTerminator()->getNumSuccessors(); |
| 97 | } |
| 98 | } |
| 99 | } |
| 100 | |
Andreas Neustifter | 9341cdc | 2009-09-02 12:38:39 +0000 | [diff] [blame] | 101 | // In the profiling output a counter for each edge is reserved, but only few |
| 102 | // are used. This is done to be able to read back in the profile without |
| 103 | // calulating the maximum spanning tree again, instead each edge counter that |
| 104 | // is not used is initialised with -1 to signal that this edge counter has to |
| 105 | // be calculated from other edge counters on reading the profile info back |
| 106 | // in. |
| 107 | |
Andreas Neustifter | f771dae | 2009-09-01 19:03:44 +0000 | [diff] [blame] | 108 | const Type *Int32 = Type::getInt32Ty(M.getContext()); |
| 109 | const ArrayType *ATy = ArrayType::get(Int32, NumEdges); |
| 110 | GlobalVariable *Counters = |
| 111 | new GlobalVariable(M, ATy, false, GlobalValue::InternalLinkage, |
| 112 | Constant::getNullValue(ATy), "OptEdgeProfCounters"); |
| 113 | NumEdgesInserted = 0; |
| 114 | |
| 115 | std::vector<Constant*> Initializer(NumEdges); |
Andreas Neustifter | 9233272 | 2009-09-16 11:35:50 +0000 | [diff] [blame] | 116 | Constant* Zero = ConstantInt::get(Int32, 0); |
| 117 | Constant* Uncounted = ConstantInt::get(Int32, ProfileInfoLoader::Uncounted); |
Andreas Neustifter | f771dae | 2009-09-01 19:03:44 +0000 | [diff] [blame] | 118 | |
| 119 | // Instrument all of the edges not in MST... |
| 120 | unsigned i = 0; |
| 121 | for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) { |
| 122 | if (F->isDeclaration()) continue; |
David Greene | 0b9afb4 | 2010-01-05 01:27:01 +0000 | [diff] [blame] | 123 | DEBUG(dbgs()<<"Working on "<<F->getNameStr()<<"\n"); |
Andreas Neustifter | f771dae | 2009-09-01 19:03:44 +0000 | [diff] [blame] | 124 | |
Andreas Neustifter | 9341cdc | 2009-09-02 12:38:39 +0000 | [diff] [blame] | 125 | // Calculate a Maximum Spanning Tree with the edge weights determined by |
| 126 | // ProfileEstimator. ProfileEstimator also assign weights to the virtual |
| 127 | // edges (0,entry) and (BB,0) (for blocks with no successors) and this |
| 128 | // edges also participate in the maximum spanning tree calculation. |
| 129 | // The third parameter of MaximumSpanningTree() has the effect that not the |
| 130 | // actual MST is returned but the edges _not_ in the MST. |
Andreas Neustifter | f771dae | 2009-09-01 19:03:44 +0000 | [diff] [blame] | 131 | |
Andreas Neustifter | 3985943 | 2009-09-03 08:52:52 +0000 | [diff] [blame] | 132 | ProfileInfo::EdgeWeights ECs = |
Chris Lattner | 0527344 | 2010-01-20 23:30:28 +0000 | [diff] [blame] | 133 | getAnalysis<ProfileInfo>(*F).getEdgeWeights(F); |
Andreas Neustifter | 3985943 | 2009-09-03 08:52:52 +0000 | [diff] [blame] | 134 | std::vector<ProfileInfo::EdgeWeight> EdgeVector(ECs.begin(), ECs.end()); |
Andreas Neustifter | ed1ac4a | 2009-09-04 12:34:44 +0000 | [diff] [blame] | 135 | MaximumSpanningTree<BasicBlock> MST (EdgeVector); |
| 136 | std::stable_sort(MST.begin(),MST.end()); |
Andreas Neustifter | 9341cdc | 2009-09-02 12:38:39 +0000 | [diff] [blame] | 137 | |
| 138 | // Check if (0,entry) not in the MST. If not, instrument edge |
| 139 | // (IncrementCounterInBlock()) and set the counter initially to zero, if |
| 140 | // the edge is in the MST the counter is initialised to -1. |
| 141 | |
Andreas Neustifter | f771dae | 2009-09-01 19:03:44 +0000 | [diff] [blame] | 142 | BasicBlock *entry = &(F->getEntryBlock()); |
| 143 | ProfileInfo::Edge edge = ProfileInfo::getEdge(0,entry); |
Andreas Neustifter | 3985943 | 2009-09-03 08:52:52 +0000 | [diff] [blame] | 144 | if (!std::binary_search(MST.begin(), MST.end(), edge)) { |
Andreas Neustifter | f771dae | 2009-09-01 19:03:44 +0000 | [diff] [blame] | 145 | printEdgeCounter(edge,entry,i); |
Dan Gohman | fe60104 | 2010-06-22 15:08:57 +0000 | [diff] [blame^] | 146 | IncrementCounterInBlock(entry, i, Counters); ++NumEdgesInserted; |
Andreas Neustifter | 9233272 | 2009-09-16 11:35:50 +0000 | [diff] [blame] | 147 | Initializer[i++] = (Zero); |
Andreas Neustifter | f771dae | 2009-09-01 19:03:44 +0000 | [diff] [blame] | 148 | } else{ |
Andreas Neustifter | 9233272 | 2009-09-16 11:35:50 +0000 | [diff] [blame] | 149 | Initializer[i++] = (Uncounted); |
Andreas Neustifter | f771dae | 2009-09-01 19:03:44 +0000 | [diff] [blame] | 150 | } |
| 151 | |
Andreas Neustifter | 8f123b8 | 2009-09-02 13:59:05 +0000 | [diff] [blame] | 152 | // InsertedBlocks contains all blocks that were inserted for splitting an |
| 153 | // edge, this blocks do not have to be instrumented. |
Andreas Neustifter | 3985943 | 2009-09-03 08:52:52 +0000 | [diff] [blame] | 154 | DenseSet<BasicBlock*> InsertedBlocks; |
Andreas Neustifter | f771dae | 2009-09-01 19:03:44 +0000 | [diff] [blame] | 155 | for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) { |
Andreas Neustifter | 8f123b8 | 2009-09-02 13:59:05 +0000 | [diff] [blame] | 156 | // Check if block was not inserted and thus does not have to be |
| 157 | // instrumented. |
| 158 | if (InsertedBlocks.count(BB)) continue; |
Andreas Neustifter | 9341cdc | 2009-09-02 12:38:39 +0000 | [diff] [blame] | 159 | |
Andreas Neustifter | f771dae | 2009-09-01 19:03:44 +0000 | [diff] [blame] | 160 | // Okay, we have to add a counter of each outgoing edge not in MST. If |
| 161 | // the outgoing edge is not critical don't split it, just insert the |
Andreas Neustifter | 9341cdc | 2009-09-02 12:38:39 +0000 | [diff] [blame] | 162 | // counter in the source or destination of the edge. Also, if the block |
| 163 | // has no successors, the virtual edge (BB,0) is processed. |
Andreas Neustifter | f771dae | 2009-09-01 19:03:44 +0000 | [diff] [blame] | 164 | TerminatorInst *TI = BB->getTerminator(); |
| 165 | if (TI->getNumSuccessors() == 0) { |
Andreas Neustifter | f771dae | 2009-09-01 19:03:44 +0000 | [diff] [blame] | 166 | ProfileInfo::Edge edge = ProfileInfo::getEdge(BB,0); |
Andreas Neustifter | 3985943 | 2009-09-03 08:52:52 +0000 | [diff] [blame] | 167 | if (!std::binary_search(MST.begin(), MST.end(), edge)) { |
Andreas Neustifter | f771dae | 2009-09-01 19:03:44 +0000 | [diff] [blame] | 168 | printEdgeCounter(edge,BB,i); |
Dan Gohman | fe60104 | 2010-06-22 15:08:57 +0000 | [diff] [blame^] | 169 | IncrementCounterInBlock(BB, i, Counters); ++NumEdgesInserted; |
Andreas Neustifter | 9233272 | 2009-09-16 11:35:50 +0000 | [diff] [blame] | 170 | Initializer[i++] = (Zero); |
Andreas Neustifter | f771dae | 2009-09-01 19:03:44 +0000 | [diff] [blame] | 171 | } else{ |
Andreas Neustifter | 9233272 | 2009-09-16 11:35:50 +0000 | [diff] [blame] | 172 | Initializer[i++] = (Uncounted); |
Andreas Neustifter | f771dae | 2009-09-01 19:03:44 +0000 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) { |
| 176 | BasicBlock *Succ = TI->getSuccessor(s); |
| 177 | ProfileInfo::Edge edge = ProfileInfo::getEdge(BB,Succ); |
Andreas Neustifter | 3985943 | 2009-09-03 08:52:52 +0000 | [diff] [blame] | 178 | if (!std::binary_search(MST.begin(), MST.end(), edge)) { |
Andreas Neustifter | f771dae | 2009-09-01 19:03:44 +0000 | [diff] [blame] | 179 | |
| 180 | // If the edge is critical, split it. |
Andreas Neustifter | 8f123b8 | 2009-09-02 13:59:05 +0000 | [diff] [blame] | 181 | bool wasInserted = SplitCriticalEdge(TI, s, this); |
Andreas Neustifter | f771dae | 2009-09-01 19:03:44 +0000 | [diff] [blame] | 182 | Succ = TI->getSuccessor(s); |
Andreas Neustifter | 3985943 | 2009-09-03 08:52:52 +0000 | [diff] [blame] | 183 | if (wasInserted) |
Andreas Neustifter | 8f123b8 | 2009-09-02 13:59:05 +0000 | [diff] [blame] | 184 | InsertedBlocks.insert(Succ); |
Andreas Neustifter | f771dae | 2009-09-01 19:03:44 +0000 | [diff] [blame] | 185 | |
Andreas Neustifter | 9341cdc | 2009-09-02 12:38:39 +0000 | [diff] [blame] | 186 | // Okay, we are guaranteed that the edge is no longer critical. If |
| 187 | // we only have a single successor, insert the counter in this block, |
Andreas Neustifter | f771dae | 2009-09-01 19:03:44 +0000 | [diff] [blame] | 188 | // otherwise insert it in the successor block. |
| 189 | if (TI->getNumSuccessors() == 1) { |
| 190 | // Insert counter at the start of the block |
| 191 | printEdgeCounter(edge,BB,i); |
Dan Gohman | fe60104 | 2010-06-22 15:08:57 +0000 | [diff] [blame^] | 192 | IncrementCounterInBlock(BB, i, Counters); ++NumEdgesInserted; |
Andreas Neustifter | f771dae | 2009-09-01 19:03:44 +0000 | [diff] [blame] | 193 | } else { |
| 194 | // Insert counter at the start of the block |
| 195 | printEdgeCounter(edge,Succ,i); |
Dan Gohman | fe60104 | 2010-06-22 15:08:57 +0000 | [diff] [blame^] | 196 | IncrementCounterInBlock(Succ, i, Counters); ++NumEdgesInserted; |
Andreas Neustifter | f771dae | 2009-09-01 19:03:44 +0000 | [diff] [blame] | 197 | } |
Andreas Neustifter | 9233272 | 2009-09-16 11:35:50 +0000 | [diff] [blame] | 198 | Initializer[i++] = (Zero); |
Andreas Neustifter | f771dae | 2009-09-01 19:03:44 +0000 | [diff] [blame] | 199 | } else { |
Andreas Neustifter | 9233272 | 2009-09-16 11:35:50 +0000 | [diff] [blame] | 200 | Initializer[i++] = (Uncounted); |
Andreas Neustifter | f771dae | 2009-09-01 19:03:44 +0000 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | |
Andreas Neustifter | 9341cdc | 2009-09-02 12:38:39 +0000 | [diff] [blame] | 206 | // Check if the number of edges counted at first was the number of edges we |
| 207 | // considered for instrumentation. |
Andreas Neustifter | f771dae | 2009-09-01 19:03:44 +0000 | [diff] [blame] | 208 | assert(i==NumEdges && "the number of edges in counting array is wrong"); |
| 209 | |
Andreas Neustifter | 9341cdc | 2009-09-02 12:38:39 +0000 | [diff] [blame] | 210 | // Assing the now completely defined initialiser to the array. |
Andreas Neustifter | f771dae | 2009-09-01 19:03:44 +0000 | [diff] [blame] | 211 | Constant *init = ConstantArray::get(ATy, Initializer); |
| 212 | Counters->setInitializer(init); |
| 213 | |
| 214 | // Add the initialization call to main. |
| 215 | InsertProfilingInitCall(Main, "llvm_start_opt_edge_profiling", Counters); |
| 216 | return true; |
| 217 | } |
| 218 | |