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 | |
| 22 | #include "llvm/Constants.h" |
| 23 | #include "llvm/DerivedTypes.h" |
Owen Anderson | d4d90a0 | 2009-07-06 18:42:36 +0000 | [diff] [blame] | 24 | #include "llvm/LLVMContext.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 25 | #include "llvm/Module.h" |
| 26 | #include "llvm/Pass.h" |
| 27 | #include "llvm/Support/Compiler.h" |
| 28 | #include "llvm/Support/Streams.h" |
| 29 | #include "llvm/Transforms/Instrumentation.h" |
| 30 | #include "RSProfiling.h" |
| 31 | #include "ProfilingUtils.h" |
| 32 | using namespace llvm; |
| 33 | |
| 34 | namespace { |
| 35 | class VISIBILITY_HIDDEN FunctionProfiler : public RSProfilers_std { |
| 36 | public: |
| 37 | static char ID; |
| 38 | bool runOnModule(Module &M); |
| 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 FunctionProfiler::ID = 0; |
| 43 | |
| 44 | static RegisterPass<FunctionProfiler> |
| 45 | X("insert-function-profiling", |
| 46 | "Insert instrumentation for function profiling"); |
| 47 | static RegisterAnalysisGroup<RSProfilers> XG(X); |
| 48 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 49 | ModulePass *llvm::createFunctionProfilerPass() { |
| 50 | return new FunctionProfiler(); |
| 51 | } |
| 52 | |
| 53 | bool FunctionProfiler::runOnModule(Module &M) { |
| 54 | Function *Main = M.getFunction("main"); |
| 55 | if (Main == 0) { |
| 56 | cerr << "WARNING: cannot insert function profiling into a module" |
| 57 | << " with no main function!\n"; |
| 58 | return false; // No main, no instrumentation! |
| 59 | } |
| 60 | |
| 61 | unsigned NumFunctions = 0; |
| 62 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
| 63 | if (!I->isDeclaration()) |
| 64 | ++NumFunctions; |
| 65 | |
Owen Anderson | 175b654 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 66 | const Type *ATy = M.getContext().getArrayType(Type::Int32Ty, NumFunctions); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 67 | GlobalVariable *Counters = |
Owen Anderson | e17fc1d | 2009-07-08 19:03:57 +0000 | [diff] [blame] | 68 | new GlobalVariable(M, ATy, false, GlobalValue::InternalLinkage, |
Owen Anderson | 175b654 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 69 | M.getContext().getNullValue(ATy), "FuncProfCounters"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 70 | |
| 71 | // Instrument all of the functions... |
| 72 | unsigned i = 0; |
| 73 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
| 74 | if (!I->isDeclaration()) |
| 75 | // Insert counter at the start of the function |
Dan Gohman | 1045412 | 2008-10-21 03:10:28 +0000 | [diff] [blame] | 76 | IncrementCounterInBlock(&I->getEntryBlock(), i++, Counters); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 77 | |
| 78 | // Add the initialization call to main. |
| 79 | InsertProfilingInitCall(Main, "llvm_start_func_profiling", Counters); |
| 80 | return true; |
| 81 | } |
| 82 | |
| 83 | |
| 84 | namespace { |
| 85 | class BlockProfiler : public RSProfilers_std { |
| 86 | bool runOnModule(Module &M); |
| 87 | public: |
| 88 | static char ID; |
| 89 | }; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 92 | char BlockProfiler::ID = 0; |
| 93 | static RegisterPass<BlockProfiler> |
| 94 | Y("insert-block-profiling", "Insert instrumentation for block profiling"); |
| 95 | static RegisterAnalysisGroup<RSProfilers> YG(Y); |
| 96 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 97 | ModulePass *llvm::createBlockProfilerPass() { return new BlockProfiler(); } |
| 98 | |
| 99 | bool BlockProfiler::runOnModule(Module &M) { |
| 100 | Function *Main = M.getFunction("main"); |
| 101 | if (Main == 0) { |
| 102 | cerr << "WARNING: cannot insert block profiling into a module" |
| 103 | << " with no main function!\n"; |
| 104 | return false; // No main, no instrumentation! |
| 105 | } |
| 106 | |
| 107 | unsigned NumBlocks = 0; |
| 108 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
| 109 | NumBlocks += I->size(); |
| 110 | |
Owen Anderson | 175b654 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 111 | const Type *ATy = M.getContext().getArrayType(Type::Int32Ty, 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 | 175b654 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 114 | M.getContext().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; |
| 118 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
| 119 | for (Function::iterator BB = I->begin(), E = I->end(); BB != E; ++BB) |
| 120 | // Insert counter at the start of the block |
| 121 | IncrementCounterInBlock(BB, i++, Counters); |
| 122 | |
| 123 | // Add the initialization call to main. |
| 124 | InsertProfilingInitCall(Main, "llvm_start_block_profiling", Counters); |
| 125 | return true; |
| 126 | } |
| 127 | |