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 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source 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 | |
| 22 | #include "llvm/Constants.h" |
| 23 | #include "llvm/DerivedTypes.h" |
Chris Lattner | baa2007 | 2003-10-28 18:59:04 +0000 | [diff] [blame] | 24 | #include "llvm/Module.h" |
| 25 | #include "llvm/Pass.h" |
Jeff Cohen | d9ed8c8 | 2005-01-07 06:57:28 +0000 | [diff] [blame] | 26 | #include "llvm/Transforms/Instrumentation.h" |
Andrew Lenharth | bb227c1 | 2005-11-28 18:00:38 +0000 | [diff] [blame] | 27 | #include "RSProfiling.h" |
Chris Lattner | 467dd2e | 2004-03-08 17:06:13 +0000 | [diff] [blame] | 28 | #include "ProfilingUtils.h" |
Reid Spencer | 954da37 | 2004-07-04 12:19:56 +0000 | [diff] [blame] | 29 | #include <iostream> |
| 30 | |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 31 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 32 | |
Chris Lattner | 9424575 | 2003-10-29 21:24:22 +0000 | [diff] [blame] | 33 | namespace { |
Andrew Lenharth | bb227c1 | 2005-11-28 18:00:38 +0000 | [diff] [blame] | 34 | class FunctionProfiler : public RSProfilers_std { |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 35 | bool runOnModule(Module &M); |
Chris Lattner | 9424575 | 2003-10-29 21:24:22 +0000 | [diff] [blame] | 36 | }; |
| 37 | |
Chris Lattner | 7f8897f | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 38 | RegisterPass<FunctionProfiler> X("insert-function-profiling", |
Chris Lattner | 9424575 | 2003-10-29 21:24:22 +0000 | [diff] [blame] | 39 | "Insert instrumentation for function profiling"); |
Chris Lattner | a537017 | 2006-08-28 00:42:29 +0000 | [diff] [blame] | 40 | RegisterAnalysisGroup<RSProfilers> XG(X); |
Andrew Lenharth | bb227c1 | 2005-11-28 18:00:38 +0000 | [diff] [blame] | 41 | |
Chris Lattner | 9424575 | 2003-10-29 21:24:22 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Misha Brukman | 60766f7 | 2005-01-07 07:05:34 +0000 | [diff] [blame] | 44 | ModulePass *llvm::createFunctionProfilerPass() { |
| 45 | return new FunctionProfiler(); |
Jeff Cohen | d9ed8c8 | 2005-01-07 06:57:28 +0000 | [diff] [blame] | 46 | } |
| 47 | |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 48 | bool FunctionProfiler::runOnModule(Module &M) { |
Chris Lattner | 9424575 | 2003-10-29 21:24:22 +0000 | [diff] [blame] | 49 | Function *Main = M.getMainFunction(); |
| 50 | if (Main == 0) { |
| 51 | std::cerr << "WARNING: cannot insert function profiling into a module" |
| 52 | << " with no main function!\n"; |
| 53 | return false; // No main, no instrumentation! |
| 54 | } |
| 55 | |
| 56 | unsigned NumFunctions = 0; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 57 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
Chris Lattner | 9424575 | 2003-10-29 21:24:22 +0000 | [diff] [blame] | 58 | if (!I->isExternal()) |
| 59 | ++NumFunctions; |
| 60 | |
| 61 | const Type *ATy = ArrayType::get(Type::UIntTy, NumFunctions); |
| 62 | GlobalVariable *Counters = |
| 63 | new GlobalVariable(ATy, false, GlobalValue::InternalLinkage, |
| 64 | Constant::getNullValue(ATy), "FuncProfCounters", &M); |
| 65 | |
Chris Lattner | 9424575 | 2003-10-29 21:24:22 +0000 | [diff] [blame] | 66 | // Instrument all of the functions... |
| 67 | unsigned i = 0; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 68 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
Chris Lattner | 9424575 | 2003-10-29 21:24:22 +0000 | [diff] [blame] | 69 | if (!I->isExternal()) |
| 70 | // Insert counter at the start of the function |
Reid Spencer | 518310c | 2004-07-18 00:44:37 +0000 | [diff] [blame] | 71 | IncrementCounterInBlock(I->begin(), i++, Counters); |
Chris Lattner | 9424575 | 2003-10-29 21:24:22 +0000 | [diff] [blame] | 72 | |
| 73 | // Add the initialization call to main. |
Chris Lattner | 467dd2e | 2004-03-08 17:06:13 +0000 | [diff] [blame] | 74 | InsertProfilingInitCall(Main, "llvm_start_func_profiling", Counters); |
Chris Lattner | 9424575 | 2003-10-29 21:24:22 +0000 | [diff] [blame] | 75 | return true; |
| 76 | } |
| 77 | |
| 78 | |
| 79 | namespace { |
Andrew Lenharth | bb227c1 | 2005-11-28 18:00:38 +0000 | [diff] [blame] | 80 | class BlockProfiler : public RSProfilers_std { |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 81 | bool runOnModule(Module &M); |
Chris Lattner | 9424575 | 2003-10-29 21:24:22 +0000 | [diff] [blame] | 82 | }; |
| 83 | |
Chris Lattner | 7f8897f | 2006-08-27 22:42:52 +0000 | [diff] [blame] | 84 | RegisterPass<BlockProfiler> Y("insert-block-profiling", |
| 85 | "Insert instrumentation for block profiling"); |
Chris Lattner | a537017 | 2006-08-28 00:42:29 +0000 | [diff] [blame] | 86 | RegisterAnalysisGroup<RSProfilers> YG(Y); |
Chris Lattner | 9424575 | 2003-10-29 21:24:22 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Jeff Cohen | d9ed8c8 | 2005-01-07 06:57:28 +0000 | [diff] [blame] | 89 | ModulePass *llvm::createBlockProfilerPass() { return new BlockProfiler(); } |
| 90 | |
Chris Lattner | b12914b | 2004-09-20 04:48:05 +0000 | [diff] [blame] | 91 | bool BlockProfiler::runOnModule(Module &M) { |
Chris Lattner | 9424575 | 2003-10-29 21:24:22 +0000 | [diff] [blame] | 92 | Function *Main = M.getMainFunction(); |
| 93 | if (Main == 0) { |
| 94 | std::cerr << "WARNING: cannot insert block profiling into a module" |
| 95 | << " with no main function!\n"; |
| 96 | return false; // No main, no instrumentation! |
| 97 | } |
| 98 | |
| 99 | unsigned NumBlocks = 0; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 100 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
Chris Lattner | 9424575 | 2003-10-29 21:24:22 +0000 | [diff] [blame] | 101 | NumBlocks += I->size(); |
| 102 | |
| 103 | const Type *ATy = ArrayType::get(Type::UIntTy, NumBlocks); |
| 104 | GlobalVariable *Counters = |
| 105 | new GlobalVariable(ATy, false, GlobalValue::InternalLinkage, |
| 106 | Constant::getNullValue(ATy), "BlockProfCounters", &M); |
| 107 | |
Chris Lattner | 9424575 | 2003-10-29 21:24:22 +0000 | [diff] [blame] | 108 | // Instrument all of the blocks... |
| 109 | unsigned i = 0; |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 110 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
Chris Lattner | 9424575 | 2003-10-29 21:24:22 +0000 | [diff] [blame] | 111 | for (Function::iterator BB = I->begin(), E = I->end(); BB != E; ++BB) |
| 112 | // Insert counter at the start of the block |
Reid Spencer | 518310c | 2004-07-18 00:44:37 +0000 | [diff] [blame] | 113 | IncrementCounterInBlock(BB, i++, Counters); |
Chris Lattner | 9424575 | 2003-10-29 21:24:22 +0000 | [diff] [blame] | 114 | |
| 115 | // Add the initialization call to main. |
Chris Lattner | 467dd2e | 2004-03-08 17:06:13 +0000 | [diff] [blame] | 116 | InsertProfilingInitCall(Main, "llvm_start_block_profiling", Counters); |
Chris Lattner | 9424575 | 2003-10-29 21:24:22 +0000 | [diff] [blame] | 117 | return true; |
| 118 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 119 | |