Chris Lattner | baa2007 | 2003-10-28 18:59:04 +0000 | [diff] [blame] | 1 | //===- BlockProfiling.cpp - Insert counters for block profiling -----------===// |
| 2 | // |
| 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. |
| 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" |
Chris Lattner | baa2007 | 2003-10-28 18:59:04 +0000 | [diff] [blame] | 24 | #include "llvm/Module.h" |
| 25 | #include "llvm/Pass.h" |
Chris Lattner | 467dd2e | 2004-03-08 17:06:13 +0000 | [diff] [blame] | 26 | #include "ProfilingUtils.h" |
Reid Spencer | 954da37 | 2004-07-04 12:19:56 +0000 | [diff] [blame] | 27 | #include <iostream> |
| 28 | |
Chris Lattner | f7703df | 2004-01-09 06:12:26 +0000 | [diff] [blame] | 29 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 30 | |
Chris Lattner | 9424575 | 2003-10-29 21:24:22 +0000 | [diff] [blame] | 31 | namespace { |
| 32 | class FunctionProfiler : public Pass { |
| 33 | bool run(Module &M); |
| 34 | }; |
| 35 | |
| 36 | RegisterOpt<FunctionProfiler> X("insert-function-profiling", |
| 37 | "Insert instrumentation for function profiling"); |
| 38 | } |
| 39 | |
| 40 | bool FunctionProfiler::run(Module &M) { |
| 41 | Function *Main = M.getMainFunction(); |
| 42 | if (Main == 0) { |
| 43 | std::cerr << "WARNING: cannot insert function profiling into a module" |
| 44 | << " with no main function!\n"; |
| 45 | return false; // No main, no instrumentation! |
| 46 | } |
| 47 | |
| 48 | unsigned NumFunctions = 0; |
| 49 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
| 50 | if (!I->isExternal()) |
| 51 | ++NumFunctions; |
| 52 | |
| 53 | const Type *ATy = ArrayType::get(Type::UIntTy, NumFunctions); |
| 54 | GlobalVariable *Counters = |
| 55 | new GlobalVariable(ATy, false, GlobalValue::InternalLinkage, |
| 56 | Constant::getNullValue(ATy), "FuncProfCounters", &M); |
| 57 | |
| 58 | ConstantPointerRef *CounterCPR = ConstantPointerRef::get(Counters); |
| 59 | |
| 60 | // Instrument all of the functions... |
| 61 | unsigned i = 0; |
| 62 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
| 63 | if (!I->isExternal()) |
| 64 | // Insert counter at the start of the function |
| 65 | IncrementCounterInBlock(I->begin(), i++, CounterCPR); |
| 66 | |
| 67 | // Add the initialization call to main. |
Chris Lattner | 467dd2e | 2004-03-08 17:06:13 +0000 | [diff] [blame] | 68 | InsertProfilingInitCall(Main, "llvm_start_func_profiling", Counters); |
Chris Lattner | 9424575 | 2003-10-29 21:24:22 +0000 | [diff] [blame] | 69 | return true; |
| 70 | } |
| 71 | |
| 72 | |
| 73 | namespace { |
| 74 | class BlockProfiler : public Pass { |
| 75 | bool run(Module &M); |
| 76 | }; |
| 77 | |
| 78 | RegisterOpt<BlockProfiler> Y("insert-block-profiling", |
| 79 | "Insert instrumentation for block profiling"); |
| 80 | } |
| 81 | |
| 82 | bool BlockProfiler::run(Module &M) { |
| 83 | Function *Main = M.getMainFunction(); |
| 84 | if (Main == 0) { |
| 85 | std::cerr << "WARNING: cannot insert block profiling into a module" |
| 86 | << " with no main function!\n"; |
| 87 | return false; // No main, no instrumentation! |
| 88 | } |
| 89 | |
| 90 | unsigned NumBlocks = 0; |
| 91 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
| 92 | NumBlocks += I->size(); |
| 93 | |
| 94 | const Type *ATy = ArrayType::get(Type::UIntTy, NumBlocks); |
| 95 | GlobalVariable *Counters = |
| 96 | new GlobalVariable(ATy, false, GlobalValue::InternalLinkage, |
| 97 | Constant::getNullValue(ATy), "BlockProfCounters", &M); |
| 98 | |
| 99 | ConstantPointerRef *CounterCPR = ConstantPointerRef::get(Counters); |
| 100 | |
| 101 | // Instrument all of the blocks... |
| 102 | unsigned i = 0; |
| 103 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
| 104 | for (Function::iterator BB = I->begin(), E = I->end(); BB != E; ++BB) |
| 105 | // Insert counter at the start of the block |
| 106 | IncrementCounterInBlock(BB, i++, CounterCPR); |
| 107 | |
| 108 | // Add the initialization call to main. |
Chris Lattner | 467dd2e | 2004-03-08 17:06:13 +0000 | [diff] [blame] | 109 | InsertProfilingInitCall(Main, "llvm_start_block_profiling", Counters); |
Chris Lattner | 9424575 | 2003-10-29 21:24:22 +0000 | [diff] [blame] | 110 | return true; |
| 111 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 112 | |