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/Compiler.h"
26 #include "llvm/Support/raw_ostream.h"
27 #include "llvm/Transforms/Instrumentation.h"
28 #include "RSProfiling.h"
29 #include "ProfilingUtils.h"
33 class VISIBILITY_HIDDEN FunctionProfiler
: public RSProfilers_std
{
36 bool runOnModule(Module
&M
);
40 char FunctionProfiler::ID
= 0;
42 static RegisterPass
<FunctionProfiler
>
43 X("insert-function-profiling",
44 "Insert instrumentation for function profiling");
45 static RegisterAnalysisGroup
<RSProfilers
> XG(X
);
47 ModulePass
*llvm::createFunctionProfilerPass() {
48 return new FunctionProfiler();
51 bool FunctionProfiler::runOnModule(Module
&M
) {
52 Function
*Main
= M
.getFunction("main");
54 errs() << "WARNING: cannot insert function profiling into a module"
55 << " with no main function!\n";
56 return false; // No main, no instrumentation!
59 unsigned NumFunctions
= 0;
60 for (Module::iterator I
= M
.begin(), E
= M
.end(); I
!= E
; ++I
)
61 if (!I
->isDeclaration())
64 const Type
*ATy
= ArrayType::get(Type::getInt32Ty(M
.getContext()),
66 GlobalVariable
*Counters
=
67 new GlobalVariable(M
, ATy
, false, GlobalValue::InternalLinkage
,
68 Constant::getNullValue(ATy
), "FuncProfCounters");
70 // Instrument all of the functions...
72 for (Module::iterator I
= M
.begin(), E
= M
.end(); I
!= E
; ++I
)
73 if (!I
->isDeclaration())
74 // Insert counter at the start of the function
75 IncrementCounterInBlock(&I
->getEntryBlock(), i
++, Counters
);
77 // Add the initialization call to main.
78 InsertProfilingInitCall(Main
, "llvm_start_func_profiling", Counters
);
84 class BlockProfiler
: public RSProfilers_std
{
85 bool runOnModule(Module
&M
);
91 char BlockProfiler::ID
= 0;
92 static RegisterPass
<BlockProfiler
>
93 Y("insert-block-profiling", "Insert instrumentation for block profiling");
94 static RegisterAnalysisGroup
<RSProfilers
> YG(Y
);
96 ModulePass
*llvm::createBlockProfilerPass() { return new BlockProfiler(); }
98 bool BlockProfiler::runOnModule(Module
&M
) {
99 Function
*Main
= M
.getFunction("main");
101 errs() << "WARNING: cannot insert block profiling into a module"
102 << " with no main function!\n";
103 return false; // No main, no instrumentation!
106 unsigned NumBlocks
= 0;
107 for (Module::iterator I
= M
.begin(), E
= M
.end(); I
!= E
; ++I
)
108 if (!I
->isDeclaration())
109 NumBlocks
+= I
->size();
111 const Type
*ATy
= ArrayType::get(Type::getInt32Ty(M
.getContext()), NumBlocks
);
112 GlobalVariable
*Counters
=
113 new GlobalVariable(M
, ATy
, false, GlobalValue::InternalLinkage
,
114 Constant::getNullValue(ATy
), "BlockProfCounters");
116 // Instrument all of the blocks...
118 for (Module::iterator I
= M
.begin(), E
= M
.end(); I
!= E
; ++I
) {
119 if (I
->isDeclaration()) continue;
120 for (Function::iterator BB
= I
->begin(), E
= I
->end(); BB
!= E
; ++BB
)
121 // Insert counter at the start of the block
122 IncrementCounterInBlock(BB
, i
++, Counters
);
125 // Add the initialization call to main.
126 InsertProfilingInitCall(Main
, "llvm_start_block_profiling", Counters
);