Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===- EdgeProfiling.cpp - Insert counters for edge profiling -------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 081ce94 | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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 | // Note that this implementation is very naive. We insert a counter for *every* |
| 15 | // edge in the program, instead of using control flow information to prune the |
| 16 | // number of counters inserted. |
| 17 | // |
| 18 | //===----------------------------------------------------------------------===// |
| 19 | |
| 20 | #include "ProfilingUtils.h" |
| 21 | #include "llvm/Constants.h" |
| 22 | #include "llvm/DerivedTypes.h" |
Owen Anderson | d4d90a0 | 2009-07-06 18:42:36 +0000 | [diff] [blame] | 23 | #include "llvm/LLVMContext.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 24 | #include "llvm/Module.h" |
| 25 | #include "llvm/Pass.h" |
| 26 | #include "llvm/Support/Compiler.h" |
| 27 | #include "llvm/Support/Streams.h" |
| 28 | #include "llvm/Transforms/Utils/BasicBlockUtils.h" |
| 29 | #include "llvm/Transforms/Instrumentation.h" |
| 30 | #include <set> |
| 31 | using namespace llvm; |
| 32 | |
| 33 | namespace { |
| 34 | class VISIBILITY_HIDDEN EdgeProfiler : public ModulePass { |
| 35 | bool runOnModule(Module &M); |
| 36 | public: |
| 37 | static char ID; // Pass identification, replacement for typeid |
Dan Gohman | 26f8c27 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 38 | EdgeProfiler() : ModulePass(&ID) {} |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 39 | }; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 42 | char EdgeProfiler::ID = 0; |
| 43 | static RegisterPass<EdgeProfiler> |
| 44 | X("insert-edge-profiling", "Insert instrumentation for edge profiling"); |
| 45 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 46 | ModulePass *llvm::createEdgeProfilerPass() { return new EdgeProfiler(); } |
| 47 | |
| 48 | bool EdgeProfiler::runOnModule(Module &M) { |
| 49 | Function *Main = M.getFunction("main"); |
| 50 | if (Main == 0) { |
| 51 | cerr << "WARNING: cannot insert edge profiling into a module" |
| 52 | << " with no main function!\n"; |
| 53 | return false; // No main, no instrumentation! |
| 54 | } |
| 55 | |
| 56 | std::set<BasicBlock*> BlocksToInstrument; |
| 57 | unsigned NumEdges = 0; |
Daniel Dunbar | 4ae2027 | 2009-08-08 17:43:09 +0000 | [diff] [blame^] | 58 | for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) { |
| 59 | if (F->isDeclaration()) continue; |
| 60 | // Reserve space for (0,entry) edge. |
| 61 | ++NumEdges; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 62 | for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) { |
| 63 | // Keep track of which blocks need to be instrumented. We don't want to |
| 64 | // instrument blocks that are added as the result of breaking critical |
| 65 | // edges! |
| 66 | BlocksToInstrument.insert(BB); |
| 67 | NumEdges += BB->getTerminator()->getNumSuccessors(); |
| 68 | } |
Daniel Dunbar | 4ae2027 | 2009-08-08 17:43:09 +0000 | [diff] [blame^] | 69 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 70 | |
Owen Anderson | 6b6e2d9 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 71 | const Type *ATy = ArrayType::get(Type::Int32Ty, NumEdges); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 72 | GlobalVariable *Counters = |
Owen Anderson | e17fc1d | 2009-07-08 19:03:57 +0000 | [diff] [blame] | 73 | new GlobalVariable(M, ATy, false, GlobalValue::InternalLinkage, |
Owen Anderson | aac2837 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 74 | Constant::getNullValue(ATy), "EdgeProfCounters"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 75 | |
| 76 | // Instrument all of the edges... |
| 77 | unsigned i = 0; |
Daniel Dunbar | 4ae2027 | 2009-08-08 17:43:09 +0000 | [diff] [blame^] | 78 | for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F) { |
| 79 | if (F->isDeclaration()) continue; |
| 80 | // Create counter for (0,entry) edge. |
| 81 | IncrementCounterInBlock(&F->getEntryBlock(), i++, Counters); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 82 | for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB) |
| 83 | if (BlocksToInstrument.count(BB)) { // Don't instrument inserted blocks |
| 84 | // Okay, we have to add a counter of each outgoing edge. If the |
| 85 | // outgoing edge is not critical don't split it, just insert the counter |
| 86 | // in the source or destination of the edge. |
| 87 | TerminatorInst *TI = BB->getTerminator(); |
| 88 | for (unsigned s = 0, e = TI->getNumSuccessors(); s != e; ++s) { |
| 89 | // If the edge is critical, split it. |
| 90 | SplitCriticalEdge(TI, s, this); |
| 91 | |
| 92 | // Okay, we are guaranteed that the edge is no longer critical. If we |
| 93 | // only have a single successor, insert the counter in this block, |
| 94 | // otherwise insert it in the successor block. |
Chris Lattner | a0b4c94 | 2007-12-13 00:04:46 +0000 | [diff] [blame] | 95 | if (TI->getNumSuccessors() == 1) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 96 | // Insert counter at the start of the block |
| 97 | IncrementCounterInBlock(BB, i++, Counters); |
| 98 | } else { |
| 99 | // Insert counter at the start of the block |
| 100 | IncrementCounterInBlock(TI->getSuccessor(s), i++, Counters); |
| 101 | } |
| 102 | } |
| 103 | } |
Daniel Dunbar | 4ae2027 | 2009-08-08 17:43:09 +0000 | [diff] [blame^] | 104 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 105 | |
| 106 | // Add the initialization call to main. |
| 107 | InsertProfilingInitCall(Main, "llvm_start_edge_profiling", Counters); |
| 108 | return true; |
| 109 | } |
| 110 | |