1 //===- BlockProfiling.cpp - Insert counters for block profiling -----------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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.
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
20 //===----------------------------------------------------------------------===//
22 #include "llvm/Constants.h"
23 #include "llvm/DerivedTypes.h"
24 #include "llvm/Module.h"
25 #include "llvm/Pass.h"
26 #include "llvm/Transforms/Instrumentation.h"
27 #include "RSProfiling.h"
28 #include "ProfilingUtils.h"
34 class FunctionProfiler : public RSProfilers_std {
35 bool runOnModule(Module &M);
38 RegisterPass<FunctionProfiler> X("insert-function-profiling",
39 "Insert instrumentation for function profiling");
40 RegisterAnalysisGroup<RSProfilers, FunctionProfiler> XG;
44 ModulePass *llvm::createFunctionProfilerPass() {
45 return new FunctionProfiler();
48 bool FunctionProfiler::runOnModule(Module &M) {
49 Function *Main = M.getMainFunction();
51 std::cerr << "WARNING: cannot insert function profiling into a module"
52 << " with no main function!\n";
53 return false; // No main, no instrumentation!
56 unsigned NumFunctions = 0;
57 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
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);
66 // Instrument all of the functions...
68 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
70 // Insert counter at the start of the function
71 IncrementCounterInBlock(I->begin(), i++, Counters);
73 // Add the initialization call to main.
74 InsertProfilingInitCall(Main, "llvm_start_func_profiling", Counters);
80 class BlockProfiler : public RSProfilers_std {
81 bool runOnModule(Module &M);
84 RegisterPass<BlockProfiler> Y("insert-block-profiling",
85 "Insert instrumentation for block profiling");
86 RegisterAnalysisGroup<RSProfilers, BlockProfiler> YG;
89 ModulePass *llvm::createBlockProfilerPass() { return new BlockProfiler(); }
91 bool BlockProfiler::runOnModule(Module &M) {
92 Function *Main = M.getMainFunction();
94 std::cerr << "WARNING: cannot insert block profiling into a module"
95 << " with no main function!\n";
96 return false; // No main, no instrumentation!
99 unsigned NumBlocks = 0;
100 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
101 NumBlocks += I->size();
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);
108 // Instrument all of the blocks...
110 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
111 for (Function::iterator BB = I->begin(), E = I->end(); BB != E; ++BB)
112 // Insert counter at the start of the block
113 IncrementCounterInBlock(BB, i++, Counters);
115 // Add the initialization call to main.
116 InsertProfilingInitCall(Main, "llvm_start_block_profiling", Counters);