blob: 211a6d628bee4982542f89dfb58701142ffa0cd1 [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
Dan Gohmanf17a25c2007-07-18 16:29:46 +000022#include "llvm/DerivedTypes.h"
23#include "llvm/Module.h"
24#include "llvm/Pass.h"
Benjamin Kramer0588d2d2009-08-23 11:37:21 +000025#include "llvm/Support/raw_ostream.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000026#include "llvm/Transforms/Instrumentation.h"
27#include "RSProfiling.h"
28#include "ProfilingUtils.h"
29using namespace llvm;
30
31namespace {
Nick Lewycky492d06e2009-10-25 06:33:48 +000032 class FunctionProfiler : public RSProfilers_std {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033 public:
34 static char ID;
35 bool runOnModule(Module &M);
36 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +000037}
38
Dan Gohman089efff2008-05-13 00:00:25 +000039char FunctionProfiler::ID = 0;
40
41static RegisterPass<FunctionProfiler>
42X("insert-function-profiling",
43 "Insert instrumentation for function profiling");
44static RegisterAnalysisGroup<RSProfilers> XG(X);
45
Dan Gohmanf17a25c2007-07-18 16:29:46 +000046ModulePass *llvm::createFunctionProfilerPass() {
47 return new FunctionProfiler();
48}
49
50bool FunctionProfiler::runOnModule(Module &M) {
51 Function *Main = M.getFunction("main");
52 if (Main == 0) {
Benjamin Kramer0588d2d2009-08-23 11:37:21 +000053 errs() << "WARNING: cannot insert function profiling into a module"
54 << " with no main function!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000055 return false; // No main, no instrumentation!
56 }
57
58 unsigned NumFunctions = 0;
59 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
60 if (!I->isDeclaration())
61 ++NumFunctions;
62
Owen Anderson35b47072009-08-13 21:58:54 +000063 const Type *ATy = ArrayType::get(Type::getInt32Ty(M.getContext()),
64 NumFunctions);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000065 GlobalVariable *Counters =
Owen Andersone17fc1d2009-07-08 19:03:57 +000066 new GlobalVariable(M, ATy, false, GlobalValue::InternalLinkage,
Owen Andersonaac28372009-07-31 20:28:14 +000067 Constant::getNullValue(ATy), "FuncProfCounters");
Dan Gohmanf17a25c2007-07-18 16:29:46 +000068
69 // Instrument all of the functions...
70 unsigned i = 0;
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
Dan Gohman10454122008-10-21 03:10:28 +000074 IncrementCounterInBlock(&I->getEntryBlock(), i++, Counters);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000075
76 // Add the initialization call to main.
77 InsertProfilingInitCall(Main, "llvm_start_func_profiling", Counters);
78 return true;
79}
80
81
82namespace {
83 class BlockProfiler : public RSProfilers_std {
84 bool runOnModule(Module &M);
85 public:
86 static char ID;
87 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +000088}
89
Dan Gohman089efff2008-05-13 00:00:25 +000090char BlockProfiler::ID = 0;
91static RegisterPass<BlockProfiler>
92Y("insert-block-profiling", "Insert instrumentation for block profiling");
93static RegisterAnalysisGroup<RSProfilers> YG(Y);
94
Dan Gohmanf17a25c2007-07-18 16:29:46 +000095ModulePass *llvm::createBlockProfilerPass() { return new BlockProfiler(); }
96
97bool BlockProfiler::runOnModule(Module &M) {
98 Function *Main = M.getFunction("main");
99 if (Main == 0) {
Benjamin Kramer0588d2d2009-08-23 11:37:21 +0000100 errs() << "WARNING: cannot insert block profiling into a module"
101 << " with no main function!\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000102 return false; // No main, no instrumentation!
103 }
104
105 unsigned NumBlocks = 0;
106 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Daniel Dunbar4ae20272009-08-08 17:43:09 +0000107 if (!I->isDeclaration())
108 NumBlocks += I->size();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000109
Owen Anderson35b47072009-08-13 21:58:54 +0000110 const Type *ATy = ArrayType::get(Type::getInt32Ty(M.getContext()), NumBlocks);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000111 GlobalVariable *Counters =
Owen Andersone17fc1d2009-07-08 19:03:57 +0000112 new GlobalVariable(M, ATy, false, GlobalValue::InternalLinkage,
Owen Andersonaac28372009-07-31 20:28:14 +0000113 Constant::getNullValue(ATy), "BlockProfCounters");
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000114
115 // Instrument all of the blocks...
116 unsigned i = 0;
Daniel Dunbar4ae20272009-08-08 17:43:09 +0000117 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
118 if (I->isDeclaration()) continue;
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000119 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);
Daniel Dunbar4ae20272009-08-08 17:43:09 +0000122 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000123
124 // Add the initialization call to main.
125 InsertProfilingInitCall(Main, "llvm_start_block_profiling", Counters);
126 return true;
127}
128