Chris Lattner | baa2007 | 2003-10-28 18:59:04 +0000 | [diff] [blame] | 1 | //===- BlockProfiling.cpp - Insert counters for block profiling -----------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | baa2007 | 2003-10-28 18:59:04 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 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. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | baa2007 | 2003-10-28 18:59:04 +0000 | [diff] [blame] | 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 | |
Chris Lattner | baa2007 | 2003-10-28 18:59:04 +0000 | [diff] [blame] | 22 | #include "llvm/DerivedTypes.h" |
Chris Lattner | baa2007 | 2003-10-28 18:59:04 +0000 | [diff] [blame] | 23 | #include "llvm/Module.h" |
| 24 | #include "llvm/Pass.h" |
Reid Spencer | 9133fe2 | 2007-02-05 23:32:05 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Compiler.h" |
Benjamin Kramer | cfa6ec9 | 2009-08-23 11:37:21 +0000 | [diff] [blame^] | 26 | #include "llvm/Support/raw_ostream.h" |
Jeff Cohen | d9ed8c8 | 2005-01-07 06:57:28 +0000 | [diff] [blame] | 27 | #include "llvm/Transforms/Instrumentation.h" |
Andrew Lenharth | bb227c1 | 2005-11-28 18:00:38 +0000 | [diff] [blame] | 28 | #include "RSProfiling.h" |
Chris Lattner | 467dd2e | 2004-03-08 17:06:13 +0000 | [diff] [blame] | 29 | #include "ProfilingUtils.h" |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 30 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 31 | |
Chris Lattner | 9424575 | 2003-10-29 21:24:22 +0000 | [diff] [blame] | 32 | namespace { |
Reid Spencer | 9133fe2 | 2007-02-05 23:32:05 +0000 | [diff] [blame] | 33 | class VISIBILITY_HIDDEN FunctionProfiler : public RSProfilers_std { |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 34 | public: |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 35 | static char ID; |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 36 | bool runOnModule(Module &M); |
Chris Lattner | 9424575 | 2003-10-29 21:24:22 +0000 | [diff] [blame] | 37 | }; |
Chris Lattner | 9424575 | 2003-10-29 21:24:22 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Dan Gohman | 844731a | 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 | |
Misha Brukman | 60766f7 | 2005-01-07 07:05:34 +0000 | [diff] [blame] | 47 | ModulePass *llvm::createFunctionProfilerPass() { |
| 48 | return new FunctionProfiler(); |
Jeff Cohen | d9ed8c8 | 2005-01-07 06:57:28 +0000 | [diff] [blame] | 49 | } |
| 50 | |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 51 | bool FunctionProfiler::runOnModule(Module &M) { |
Reid Spencer | 688b049 | 2007-02-05 21:19:13 +0000 | [diff] [blame] | 52 | Function *Main = M.getFunction("main"); |
Chris Lattner | 9424575 | 2003-10-29 21:24:22 +0000 | [diff] [blame] | 53 | if (Main == 0) { |
Benjamin Kramer | cfa6ec9 | 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"; |
Chris Lattner | 9424575 | 2003-10-29 21:24:22 +0000 | [diff] [blame] | 56 | return false; // No main, no instrumentation! |
| 57 | } |
| 58 | |
| 59 | unsigned NumFunctions = 0; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 60 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 61 | if (!I->isDeclaration()) |
Chris Lattner | 9424575 | 2003-10-29 21:24:22 +0000 | [diff] [blame] | 62 | ++NumFunctions; |
| 63 | |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 64 | const Type *ATy = ArrayType::get(Type::getInt32Ty(M.getContext()), |
| 65 | NumFunctions); |
Chris Lattner | 9424575 | 2003-10-29 21:24:22 +0000 | [diff] [blame] | 66 | GlobalVariable *Counters = |
Owen Anderson | e9b11b4 | 2009-07-08 19:03:57 +0000 | [diff] [blame] | 67 | new GlobalVariable(M, ATy, false, GlobalValue::InternalLinkage, |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 68 | Constant::getNullValue(ATy), "FuncProfCounters"); |
Chris Lattner | 9424575 | 2003-10-29 21:24:22 +0000 | [diff] [blame] | 69 | |
Chris Lattner | 9424575 | 2003-10-29 21:24:22 +0000 | [diff] [blame] | 70 | // Instrument all of the functions... |
| 71 | unsigned i = 0; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 72 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 73 | if (!I->isDeclaration()) |
Chris Lattner | 9424575 | 2003-10-29 21:24:22 +0000 | [diff] [blame] | 74 | // Insert counter at the start of the function |
Dan Gohman | 0db69dc | 2008-10-21 03:10:28 +0000 | [diff] [blame] | 75 | IncrementCounterInBlock(&I->getEntryBlock(), i++, Counters); |
Chris Lattner | 9424575 | 2003-10-29 21:24:22 +0000 | [diff] [blame] | 76 | |
| 77 | // Add the initialization call to main. |
Chris Lattner | 467dd2e | 2004-03-08 17:06:13 +0000 | [diff] [blame] | 78 | InsertProfilingInitCall(Main, "llvm_start_func_profiling", Counters); |
Chris Lattner | 9424575 | 2003-10-29 21:24:22 +0000 | [diff] [blame] | 79 | return true; |
| 80 | } |
| 81 | |
| 82 | |
| 83 | namespace { |
Andrew Lenharth | bb227c1 | 2005-11-28 18:00:38 +0000 | [diff] [blame] | 84 | class BlockProfiler : public RSProfilers_std { |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 85 | bool runOnModule(Module &M); |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 86 | public: |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 87 | static char ID; |
Chris Lattner | 9424575 | 2003-10-29 21:24:22 +0000 | [diff] [blame] | 88 | }; |
Chris Lattner | 9424575 | 2003-10-29 21:24:22 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Dan Gohman | 844731a | 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 | |
Jeff Cohen | d9ed8c8 | 2005-01-07 06:57:28 +0000 | [diff] [blame] | 96 | ModulePass *llvm::createBlockProfilerPass() { return new BlockProfiler(); } |
| 97 | |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 98 | bool BlockProfiler::runOnModule(Module &M) { |
Reid Spencer | 688b049 | 2007-02-05 21:19:13 +0000 | [diff] [blame] | 99 | Function *Main = M.getFunction("main"); |
Chris Lattner | 9424575 | 2003-10-29 21:24:22 +0000 | [diff] [blame] | 100 | if (Main == 0) { |
Benjamin Kramer | cfa6ec9 | 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"; |
Chris Lattner | 9424575 | 2003-10-29 21:24:22 +0000 | [diff] [blame] | 103 | return false; // No main, no instrumentation! |
| 104 | } |
| 105 | |
| 106 | unsigned NumBlocks = 0; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 107 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
Daniel Dunbar | caaa493 | 2009-08-08 17:43:09 +0000 | [diff] [blame] | 108 | if (!I->isDeclaration()) |
| 109 | NumBlocks += I->size(); |
Chris Lattner | 9424575 | 2003-10-29 21:24:22 +0000 | [diff] [blame] | 110 | |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 111 | const Type *ATy = ArrayType::get(Type::getInt32Ty(M.getContext()), NumBlocks); |
Chris Lattner | 9424575 | 2003-10-29 21:24:22 +0000 | [diff] [blame] | 112 | GlobalVariable *Counters = |
Owen Anderson | e9b11b4 | 2009-07-08 19:03:57 +0000 | [diff] [blame] | 113 | new GlobalVariable(M, ATy, false, GlobalValue::InternalLinkage, |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 114 | Constant::getNullValue(ATy), "BlockProfCounters"); |
Chris Lattner | 9424575 | 2003-10-29 21:24:22 +0000 | [diff] [blame] | 115 | |
Chris Lattner | 9424575 | 2003-10-29 21:24:22 +0000 | [diff] [blame] | 116 | // Instrument all of the blocks... |
| 117 | unsigned i = 0; |
Daniel Dunbar | caaa493 | 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; |
Chris Lattner | 9424575 | 2003-10-29 21:24:22 +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 |
Reid Spencer | 518310c | 2004-07-18 00:44:37 +0000 | [diff] [blame] | 122 | IncrementCounterInBlock(BB, i++, Counters); |
Daniel Dunbar | caaa493 | 2009-08-08 17:43:09 +0000 | [diff] [blame] | 123 | } |
Chris Lattner | 9424575 | 2003-10-29 21:24:22 +0000 | [diff] [blame] | 124 | |
| 125 | // Add the initialization call to main. |
Chris Lattner | 467dd2e | 2004-03-08 17:06:13 +0000 | [diff] [blame] | 126 | InsertProfilingInitCall(Main, "llvm_start_block_profiling", Counters); |
Chris Lattner | 9424575 | 2003-10-29 21:24:22 +0000 | [diff] [blame] | 127 | return true; |
| 128 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 129 | |