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