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 "ProfilingUtils.h"
33 class FunctionProfiler : public ModulePass {
34 bool runOnModule(Module &M);
37 RegisterOpt<FunctionProfiler> X("insert-function-profiling",
38 "Insert instrumentation for function profiling");
41 ModulePass *llvm::createFunctionProfilerPass()
43 return new FunctionProfiler();
46 bool FunctionProfiler::runOnModule(Module &M) {
47 Function *Main = M.getMainFunction();
49 std::cerr << "WARNING: cannot insert function profiling into a module"
50 << " with no main function!\n";
51 return false; // No main, no instrumentation!
54 unsigned NumFunctions = 0;
55 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
59 const Type *ATy = ArrayType::get(Type::UIntTy, NumFunctions);
60 GlobalVariable *Counters =
61 new GlobalVariable(ATy, false, GlobalValue::InternalLinkage,
62 Constant::getNullValue(ATy), "FuncProfCounters", &M);
64 // Instrument all of the functions...
66 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
68 // Insert counter at the start of the function
69 IncrementCounterInBlock(I->begin(), i++, Counters);
71 // Add the initialization call to main.
72 InsertProfilingInitCall(Main, "llvm_start_func_profiling", Counters);
78 class BlockProfiler : public ModulePass {
79 bool runOnModule(Module &M);
82 RegisterOpt<BlockProfiler> Y("insert-block-profiling",
83 "Insert instrumentation for block profiling");
86 ModulePass *llvm::createBlockProfilerPass() { return new BlockProfiler(); }
88 bool BlockProfiler::runOnModule(Module &M) {
89 Function *Main = M.getMainFunction();
91 std::cerr << "WARNING: cannot insert block profiling into a module"
92 << " with no main function!\n";
93 return false; // No main, no instrumentation!
96 unsigned NumBlocks = 0;
97 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
98 NumBlocks += I->size();
100 const Type *ATy = ArrayType::get(Type::UIntTy, NumBlocks);
101 GlobalVariable *Counters =
102 new GlobalVariable(ATy, false, GlobalValue::InternalLinkage,
103 Constant::getNullValue(ATy), "BlockProfCounters", &M);
105 // Instrument all of the blocks...
107 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
108 for (Function::iterator BB = I->begin(), E = I->end(); BB != E; ++BB)
109 // Insert counter at the start of the block
110 IncrementCounterInBlock(BB, i++, Counters);
112 // Add the initialization call to main.
113 InsertProfilingInitCall(Main, "llvm_start_block_profiling", Counters);