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" |
| 17 | #include "llvm/Constants.h" |
| 18 | #include "llvm/Function.h" |
| 19 | #include "llvm/LLVMContext.h" |
| 20 | #include "llvm/Module.h" |
| 21 | #include "llvm/Pass.h" |
| 22 | #include "llvm/Analysis/Passes.h" |
| 23 | #include "llvm/Analysis/ProfileInfo.h" |
| 24 | #include "llvm/Support/Compiler.h" |
| 25 | #include "llvm/Support/Debug.h" |
| 26 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 27 | #include "llvm/Transforms/Instrumentation.h" |
| 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 { |
| 36 | class VISIBILITY_HIDDEN OptimalEdgeProfiler : public ModulePass { |
| 37 | bool runOnModule(Module &M); |
| 38 | ProfileInfo *PI; |
| 39 | public: |
| 40 | static char ID; // Pass identification, replacement for typeid |
| 41 | OptimalEdgeProfiler() : ModulePass(&ID) {} |
| 42 | |
| 43 | void getAnalysisUsage(AnalysisUsage &AU) const { |
| 44 | AU.addRequiredID(ProfileEstimatorPassID); |
| 45 | AU.addRequired<ProfileInfo>(); |
| 46 | } |
| 47 | |
| 48 | virtual const char *getPassName() const { |
| 49 | return "Optimal Edge Profiler"; |
| 50 | } |
| 51 | }; |
| 52 | } |
| 53 | |
| 54 | char OptimalEdgeProfiler::ID = 0; |
| 55 | static RegisterPass<OptimalEdgeProfiler> |
| 56 | X("insert-optimal-edge-profiling", |
| 57 | "Insert optimal instrumentation for edge profiling"); |
| 58 | |
| 59 | ModulePass *llvm::createOptimalEdgeProfilerPass() { |
| 60 | return new OptimalEdgeProfiler(); |
| 61 | } |
| 62 | |
| 63 | inline static void printEdgeCounter(ProfileInfo::Edge e, |
| 64 | BasicBlock* b, |
| 65 | unsigned i) { |
| 66 | DEBUG(errs() << "--Edge Counter for " << (e) << " in " \ |
| 67 | << ((b)?(b)->getNameStr():"0") << " (# " << (i) << ")\n"); |
| 68 | } |
| 69 | |
| 70 | bool OptimalEdgeProfiler::runOnModule(Module &M) { |
| 71 | Function *Main = M.getFunction("main"); |
| 72 | if (Main == 0) { |
| 73 | errs() << "WARNING: cannot insert edge profiling into a module" |
| 74 | << " with no main function!\n"; |
| 75 | return false; // No main, no instrumentation! |
| 76 | } |
| 77 | |
| 78 | std::set<BasicBlock*> BlocksToInstrument; |
| 79 | unsigned NumEdges = 0; |
| 80 | for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) { |
| 81 | if (F->isDeclaration()) continue; |
| 82 | // Reserve space for (0,entry) edge. |
| 83 | ++NumEdges; |
| 84 | for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) { |
| 85 | // Keep track of which blocks need to be instrumented. We don't want to |
| 86 | // instrument blocks that are added as the result of breaking critical |
| 87 | // edges! |
| 88 | BlocksToInstrument.insert(BB); |
| 89 | if (BB->getTerminator()->getNumSuccessors() == 0) { |
| 90 | // Reserve space for (BB,0) edge. |
| 91 | ++NumEdges; |
| 92 | } else { |
| 93 | NumEdges += BB->getTerminator()->getNumSuccessors(); |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | const Type *Int32 = Type::getInt32Ty(M.getContext()); |
| 99 | const ArrayType *ATy = ArrayType::get(Int32, NumEdges); |
| 100 | GlobalVariable *Counters = |
| 101 | new GlobalVariable(M, ATy, false, GlobalValue::InternalLinkage, |
| 102 | Constant::getNullValue(ATy), "OptEdgeProfCounters"); |
| 103 | NumEdgesInserted = 0; |
| 104 | |
| 105 | std::vector<Constant*> Initializer(NumEdges); |
| 106 | Constant* zeroc = ConstantInt::get(Int32, 0); |
| 107 | Constant* minusonec = ConstantInt::get(Int32, ProfileInfo::MissingValue); |
| 108 | |
| 109 | // Instrument all of the edges not in MST... |
| 110 | unsigned i = 0; |
| 111 | for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) { |
| 112 | if (F->isDeclaration()) continue; |
| 113 | DEBUG(errs()<<"Working on "<<F->getNameStr()<<"\n"); |
| 114 | |
| 115 | PI = &getAnalysisID<ProfileInfo>(ProfileEstimatorPassID,*F); |
| 116 | MaximumSpanningTree MST = MaximumSpanningTree(&(*F),PI,true); |
| 117 | |
| 118 | // Create counter for (0,entry) edge. |
| 119 | BasicBlock *entry = &(F->getEntryBlock()); |
| 120 | ProfileInfo::Edge edge = ProfileInfo::getEdge(0,entry); |
| 121 | if (std::binary_search(MST.begin(),MST.end(),edge)) { |
| 122 | printEdgeCounter(edge,entry,i); |
| 123 | IncrementCounterInBlock(entry, i, Counters); NumEdgesInserted++; |
| 124 | Initializer[i++] = (zeroc); |
| 125 | } else{ |
| 126 | Initializer[i++] = (minusonec); |
| 127 | } |
| 128 | |
| 129 | for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) { |
| 130 | if (!BlocksToInstrument.count(BB)) continue; // Don't count new blocks |
| 131 | // Okay, we have to add a counter of each outgoing edge not in MST. If |
| 132 | // the outgoing edge is not critical don't split it, just insert the |
| 133 | // counter in the source or destination of the edge. |
| 134 | TerminatorInst *TI = BB->getTerminator(); |
| 135 | if (TI->getNumSuccessors() == 0) { |
| 136 | // Create counter for (BB,0), edge. |
| 137 | ProfileInfo::Edge edge = ProfileInfo::getEdge(BB,0); |
| 138 | if (std::binary_search(MST.begin(),MST.end(),edge)) { |
| 139 | printEdgeCounter(edge,BB,i); |
| 140 | IncrementCounterInBlock(BB, i, Counters); NumEdgesInserted++; |
| 141 | Initializer[i++] = (zeroc); |
| 142 | } else{ |
| 143 | Initializer[i++] = (minusonec); |
| 144 | } |
| 145 | } |
| 146 | for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) { |
| 147 | BasicBlock *Succ = TI->getSuccessor(s); |
| 148 | ProfileInfo::Edge edge = ProfileInfo::getEdge(BB,Succ); |
| 149 | if (std::binary_search(MST.begin(),MST.end(),edge)) { |
| 150 | |
| 151 | // If the edge is critical, split it. |
| 152 | SplitCriticalEdge(TI,s,this); |
| 153 | Succ = TI->getSuccessor(s); |
| 154 | |
| 155 | // Okay, we are guaranteed that the edge is no longer critical. If we |
| 156 | // only have a single successor, insert the counter in this block, |
| 157 | // otherwise insert it in the successor block. |
| 158 | if (TI->getNumSuccessors() == 1) { |
| 159 | // Insert counter at the start of the block |
| 160 | printEdgeCounter(edge,BB,i); |
| 161 | IncrementCounterInBlock(BB, i, Counters); NumEdgesInserted++; |
| 162 | } else { |
| 163 | // Insert counter at the start of the block |
| 164 | printEdgeCounter(edge,Succ,i); |
| 165 | IncrementCounterInBlock(Succ, i, Counters); NumEdgesInserted++; |
| 166 | } |
| 167 | Initializer[i++] = (zeroc); |
| 168 | } else { |
| 169 | Initializer[i++] = (minusonec); |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | // check if indeed all counters have been used |
| 176 | assert(i==NumEdges && "the number of edges in counting array is wrong"); |
| 177 | |
| 178 | // assign initialiser to array |
| 179 | Constant *init = ConstantArray::get(ATy, Initializer); |
| 180 | Counters->setInitializer(init); |
| 181 | |
| 182 | // Add the initialization call to main. |
| 183 | InsertProfilingInitCall(Main, "llvm_start_opt_edge_profiling", Counters); |
| 184 | return true; |
| 185 | } |
| 186 | |