Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===- BlockProfiling.cpp - Insert counters for block 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 basic block or |
| 11 | // function profiling. This is the most basic form of profiling, which can tell |
| 12 | // which blocks are hot, but cannot reliably detect hot paths through the CFG. |
| 13 | // Block profiling counts the number of times each basic block executes, and |
| 14 | // function profiling counts the number of times each function is called. |
| 15 | // |
| 16 | // Note that this implementation is very naive. Control equivalent regions of |
| 17 | // the CFG should not require duplicate counters, but we do put duplicate |
| 18 | // counters in. |
| 19 | // |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 22 | #include "llvm/DerivedTypes.h" |
| 23 | #include "llvm/Module.h" |
| 24 | #include "llvm/Pass.h" |
| 25 | #include "llvm/Support/Compiler.h" |
Benjamin Kramer | 0588d2d | 2009-08-23 11:37:21 +0000 | [diff] [blame^] | 26 | #include "llvm/Support/raw_ostream.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 27 | #include "llvm/Transforms/Instrumentation.h" |
| 28 | #include "RSProfiling.h" |
| 29 | #include "ProfilingUtils.h" |
| 30 | using namespace llvm; |
| 31 | |
| 32 | namespace { |
| 33 | class VISIBILITY_HIDDEN FunctionProfiler : public RSProfilers_std { |
| 34 | public: |
| 35 | static char ID; |
| 36 | bool runOnModule(Module &M); |
| 37 | }; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 40 | char FunctionProfiler::ID = 0; |
| 41 | |
| 42 | static RegisterPass<FunctionProfiler> |
| 43 | X("insert-function-profiling", |
| 44 | "Insert instrumentation for function profiling"); |
| 45 | static RegisterAnalysisGroup<RSProfilers> XG(X); |
| 46 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 47 | ModulePass *llvm::createFunctionProfilerPass() { |
| 48 | return new FunctionProfiler(); |
| 49 | } |
| 50 | |
| 51 | bool FunctionProfiler::runOnModule(Module &M) { |
| 52 | Function *Main = M.getFunction("main"); |
| 53 | if (Main == 0) { |
Benjamin Kramer | 0588d2d | 2009-08-23 11:37:21 +0000 | [diff] [blame^] | 54 | errs() << "WARNING: cannot insert function profiling into a module" |
| 55 | << " with no main function!\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 56 | return false; // No main, no instrumentation! |
| 57 | } |
| 58 | |
| 59 | unsigned NumFunctions = 0; |
| 60 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
| 61 | if (!I->isDeclaration()) |
| 62 | ++NumFunctions; |
| 63 | |
Owen Anderson | 35b4707 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 64 | const Type *ATy = ArrayType::get(Type::getInt32Ty(M.getContext()), |
| 65 | NumFunctions); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 66 | GlobalVariable *Counters = |
Owen Anderson | e17fc1d | 2009-07-08 19:03:57 +0000 | [diff] [blame] | 67 | new GlobalVariable(M, ATy, false, GlobalValue::InternalLinkage, |
Owen Anderson | aac2837 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 68 | Constant::getNullValue(ATy), "FuncProfCounters"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 69 | |
| 70 | // Instrument all of the functions... |
| 71 | unsigned i = 0; |
| 72 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
| 73 | if (!I->isDeclaration()) |
| 74 | // Insert counter at the start of the function |
Dan Gohman | 1045412 | 2008-10-21 03:10:28 +0000 | [diff] [blame] | 75 | IncrementCounterInBlock(&I->getEntryBlock(), i++, Counters); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 76 | |
| 77 | // Add the initialization call to main. |
| 78 | InsertProfilingInitCall(Main, "llvm_start_func_profiling", Counters); |
| 79 | return true; |
| 80 | } |
| 81 | |
| 82 | |
| 83 | namespace { |
| 84 | class BlockProfiler : public RSProfilers_std { |
| 85 | bool runOnModule(Module &M); |
| 86 | public: |
| 87 | static char ID; |
| 88 | }; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 91 | char BlockProfiler::ID = 0; |
| 92 | static RegisterPass<BlockProfiler> |
| 93 | Y("insert-block-profiling", "Insert instrumentation for block profiling"); |
| 94 | static RegisterAnalysisGroup<RSProfilers> YG(Y); |
| 95 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 96 | ModulePass *llvm::createBlockProfilerPass() { return new BlockProfiler(); } |
| 97 | |
| 98 | bool BlockProfiler::runOnModule(Module &M) { |
| 99 | Function *Main = M.getFunction("main"); |
| 100 | if (Main == 0) { |
Benjamin Kramer | 0588d2d | 2009-08-23 11:37:21 +0000 | [diff] [blame^] | 101 | errs() << "WARNING: cannot insert block profiling into a module" |
| 102 | << " with no main function!\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 103 | return false; // No main, no instrumentation! |
| 104 | } |
| 105 | |
| 106 | unsigned NumBlocks = 0; |
| 107 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
Daniel Dunbar | 4ae2027 | 2009-08-08 17:43:09 +0000 | [diff] [blame] | 108 | if (!I->isDeclaration()) |
| 109 | NumBlocks += I->size(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 110 | |
Owen Anderson | 35b4707 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 111 | const Type *ATy = ArrayType::get(Type::getInt32Ty(M.getContext()), NumBlocks); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 112 | GlobalVariable *Counters = |
Owen Anderson | e17fc1d | 2009-07-08 19:03:57 +0000 | [diff] [blame] | 113 | new GlobalVariable(M, ATy, false, GlobalValue::InternalLinkage, |
Owen Anderson | aac2837 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 114 | Constant::getNullValue(ATy), "BlockProfCounters"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 115 | |
| 116 | // Instrument all of the blocks... |
| 117 | unsigned i = 0; |
Daniel Dunbar | 4ae2027 | 2009-08-08 17:43:09 +0000 | [diff] [blame] | 118 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) { |
| 119 | if (I->isDeclaration()) continue; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 120 | for (Function::iterator BB = I->begin(), E = I->end(); BB != E; ++BB) |
| 121 | // Insert counter at the start of the block |
| 122 | IncrementCounterInBlock(BB, i++, Counters); |
Daniel Dunbar | 4ae2027 | 2009-08-08 17:43:09 +0000 | [diff] [blame] | 123 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 124 | |
| 125 | // Add the initialization call to main. |
| 126 | InsertProfilingInitCall(Main, "llvm_start_block_profiling", Counters); |
| 127 | return true; |
| 128 | } |
| 129 | |