1 //===- BlockProfiling.cpp - Insert counters for block profiling -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // 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/DerivedTypes.h"
23 #include "llvm/Module.h"
24 #include "llvm/Pass.h"
25 #include "llvm/Support/raw_ostream.h"
26 #include "llvm/Transforms/Instrumentation.h"
27 #include "RSProfiling.h"
28 #include "ProfilingUtils.h"
32 class FunctionProfiler
: public RSProfilers_std
{
35 bool runOnModule(Module
&M
);
39 char FunctionProfiler::ID
= 0;
41 static RegisterPass
<FunctionProfiler
>
42 X("insert-function-profiling",
43 "Insert instrumentation for function profiling");
44 static RegisterAnalysisGroup
<RSProfilers
> XG(X
);
46 ModulePass
*llvm::createFunctionProfilerPass() {
47 return new FunctionProfiler();
50 bool FunctionProfiler::runOnModule(Module
&M
) {
51 Function
*Main
= M
.getFunction("main");
53 errs() << "WARNING: cannot insert function profiling into a module"
54 << " with no main function!\n";
55 return false; // No main, no instrumentation!
58 unsigned NumFunctions
= 0;
59 for (Module::iterator I
= M
.begin(), E
= M
.end(); I
!= E
; ++I
)
60 if (!I
->isDeclaration())
63 const Type
*ATy
= ArrayType::get(Type::getInt32Ty(M
.getContext()),
65 GlobalVariable
*Counters
=
66 new GlobalVariable(M
, ATy
, false, GlobalValue::InternalLinkage
,
67 Constant::getNullValue(ATy
), "FuncProfCounters");
69 // Instrument all of the functions...
71 for (Module::iterator I
= M
.begin(), E
= M
.end(); I
!= E
; ++I
)
72 if (!I
->isDeclaration())
73 // Insert counter at the start of the function
74 IncrementCounterInBlock(&I
->getEntryBlock(), i
++, Counters
);
76 // Add the initialization call to main.
77 InsertProfilingInitCall(Main
, "llvm_start_func_profiling", Counters
);
83 class BlockProfiler
: public RSProfilers_std
{
84 bool runOnModule(Module
&M
);
90 char BlockProfiler::ID
= 0;
91 static RegisterPass
<BlockProfiler
>
92 Y("insert-block-profiling", "Insert instrumentation for block profiling");
93 static RegisterAnalysisGroup
<RSProfilers
> YG(Y
);
95 ModulePass
*llvm::createBlockProfilerPass() { return new BlockProfiler(); }
97 bool BlockProfiler::runOnModule(Module
&M
) {
98 Function
*Main
= M
.getFunction("main");
100 errs() << "WARNING: cannot insert block profiling into a module"
101 << " with no main function!\n";
102 return false; // No main, no instrumentation!
105 unsigned NumBlocks
= 0;
106 for (Module::iterator I
= M
.begin(), E
= M
.end(); I
!= E
; ++I
)
107 if (!I
->isDeclaration())
108 NumBlocks
+= I
->size();
110 const Type
*ATy
= ArrayType::get(Type::getInt32Ty(M
.getContext()), NumBlocks
);
111 GlobalVariable
*Counters
=
112 new GlobalVariable(M
, ATy
, false, GlobalValue::InternalLinkage
,
113 Constant::getNullValue(ATy
), "BlockProfCounters");
115 // Instrument all of the blocks...
117 for (Module::iterator I
= M
.begin(), E
= M
.end(); I
!= E
; ++I
) {
118 if (I
->isDeclaration()) continue;
119 for (Function::iterator BB
= I
->begin(), E
= I
->end(); BB
!= E
; ++BB
)
120 // Insert counter at the start of the block
121 IncrementCounterInBlock(BB
, i
++, Counters
);
124 // Add the initialization call to main.
125 InsertProfilingInitCall(Main
, "llvm_start_block_profiling", Counters
);