blob: 029b8fe0b9df07b8d27a3d8c780b549b2c4f0552 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- BlockProfiling.cpp - Insert counters for block profiling -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
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"
Owen Andersond4d90a02009-07-06 18:42:36 +000024#include "llvm/LLVMContext.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000025#include "llvm/Module.h"
26#include "llvm/Pass.h"
27#include "llvm/Support/Compiler.h"
28#include "llvm/Support/Streams.h"
29#include "llvm/Transforms/Instrumentation.h"
30#include "RSProfiling.h"
31#include "ProfilingUtils.h"
32using namespace llvm;
33
34namespace {
35 class VISIBILITY_HIDDEN FunctionProfiler : public RSProfilers_std {
36 public:
37 static char ID;
38 bool runOnModule(Module &M);
39 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +000040}
41
Dan Gohman089efff2008-05-13 00:00:25 +000042char FunctionProfiler::ID = 0;
43
44static RegisterPass<FunctionProfiler>
45X("insert-function-profiling",
46 "Insert instrumentation for function profiling");
47static RegisterAnalysisGroup<RSProfilers> XG(X);
48
Dan Gohmanf17a25c2007-07-18 16:29:46 +000049ModulePass *llvm::createFunctionProfilerPass() {
50 return new FunctionProfiler();
51}
52
53bool FunctionProfiler::runOnModule(Module &M) {
54 Function *Main = M.getFunction("main");
55 if (Main == 0) {
56 cerr << "WARNING: cannot insert function profiling into a module"
57 << " with no main function!\n";
58 return false; // No main, no instrumentation!
59 }
60
61 unsigned NumFunctions = 0;
62 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
63 if (!I->isDeclaration())
64 ++NumFunctions;
65
Owen Anderson35b47072009-08-13 21:58:54 +000066 const Type *ATy = ArrayType::get(Type::getInt32Ty(M.getContext()),
67 NumFunctions);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000068 GlobalVariable *Counters =
Owen Andersone17fc1d2009-07-08 19:03:57 +000069 new GlobalVariable(M, ATy, false, GlobalValue::InternalLinkage,
Owen Andersonaac28372009-07-31 20:28:14 +000070 Constant::getNullValue(ATy), "FuncProfCounters");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000071
72 // Instrument all of the functions...
73 unsigned i = 0;
74 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
75 if (!I->isDeclaration())
76 // Insert counter at the start of the function
Dan Gohman10454122008-10-21 03:10:28 +000077 IncrementCounterInBlock(&I->getEntryBlock(), i++, Counters);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000078
79 // Add the initialization call to main.
80 InsertProfilingInitCall(Main, "llvm_start_func_profiling", Counters);
81 return true;
82}
83
84
85namespace {
86 class BlockProfiler : public RSProfilers_std {
87 bool runOnModule(Module &M);
88 public:
89 static char ID;
90 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +000091}
92
Dan Gohman089efff2008-05-13 00:00:25 +000093char BlockProfiler::ID = 0;
94static RegisterPass<BlockProfiler>
95Y("insert-block-profiling", "Insert instrumentation for block profiling");
96static RegisterAnalysisGroup<RSProfilers> YG(Y);
97
Dan Gohmanf17a25c2007-07-18 16:29:46 +000098ModulePass *llvm::createBlockProfilerPass() { return new BlockProfiler(); }
99
100bool BlockProfiler::runOnModule(Module &M) {
101 Function *Main = M.getFunction("main");
102 if (Main == 0) {
103 cerr << "WARNING: cannot insert block profiling into a module"
104 << " with no main function!\n";
105 return false; // No main, no instrumentation!
106 }
107
108 unsigned NumBlocks = 0;
109 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Daniel Dunbar4ae20272009-08-08 17:43:09 +0000110 if (!I->isDeclaration())
111 NumBlocks += I->size();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000112
Owen Anderson35b47072009-08-13 21:58:54 +0000113 const Type *ATy = ArrayType::get(Type::getInt32Ty(M.getContext()), NumBlocks);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000114 GlobalVariable *Counters =
Owen Andersone17fc1d2009-07-08 19:03:57 +0000115 new GlobalVariable(M, ATy, false, GlobalValue::InternalLinkage,
Owen Andersonaac28372009-07-31 20:28:14 +0000116 Constant::getNullValue(ATy), "BlockProfCounters");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000117
118 // Instrument all of the blocks...
119 unsigned i = 0;
Daniel Dunbar4ae20272009-08-08 17:43:09 +0000120 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
121 if (I->isDeclaration()) continue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000122 for (Function::iterator BB = I->begin(), E = I->end(); BB != E; ++BB)
123 // Insert counter at the start of the block
124 IncrementCounterInBlock(BB, i++, Counters);
Daniel Dunbar4ae20272009-08-08 17:43:09 +0000125 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000126
127 // Add the initialization call to main.
128 InsertProfilingInitCall(Main, "llvm_start_block_profiling", Counters);
129 return true;
130}
131