blob: 8fc6a7912729c7465057d36dde1fa85d8526d899 [file] [log] [blame]
Chris Lattnerbaa20072003-10-28 18:59:04 +00001//===- BlockProfiling.cpp - Insert counters for block profiling -----------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
Chris Lattnerbaa20072003-10-28 18:59:04 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
Chris Lattnerbaa20072003-10-28 18:59:04 +00008//===----------------------------------------------------------------------===//
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
Chris Lattnerbaa20072003-10-28 18:59:04 +000022#include "llvm/DerivedTypes.h"
Chris Lattnerbaa20072003-10-28 18:59:04 +000023#include "llvm/Module.h"
24#include "llvm/Pass.h"
Reid Spencer9133fe22007-02-05 23:32:05 +000025#include "llvm/Support/Compiler.h"
Benjamin Kramercfa6ec92009-08-23 11:37:21 +000026#include "llvm/Support/raw_ostream.h"
Jeff Cohend9ed8c82005-01-07 06:57:28 +000027#include "llvm/Transforms/Instrumentation.h"
Andrew Lenharthbb227c12005-11-28 18:00:38 +000028#include "RSProfiling.h"
Chris Lattner467dd2e2004-03-08 17:06:13 +000029#include "ProfilingUtils.h"
Chris Lattnerf7703df2004-01-09 06:12:26 +000030using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000031
Chris Lattner94245752003-10-29 21:24:22 +000032namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000033 class FunctionProfiler : public RSProfilers_std {
Devang Patel794fd752007-05-01 21:15:47 +000034 public:
Devang Patel19974732007-05-03 01:11:54 +000035 static char ID;
Chris Lattnerb12914b2004-09-20 04:48:05 +000036 bool runOnModule(Module &M);
Chris Lattner94245752003-10-29 21:24:22 +000037 };
Chris Lattner94245752003-10-29 21:24:22 +000038}
39
Dan Gohman844731a2008-05-13 00:00:25 +000040char FunctionProfiler::ID = 0;
41
42static RegisterPass<FunctionProfiler>
43X("insert-function-profiling",
44 "Insert instrumentation for function profiling");
45static RegisterAnalysisGroup<RSProfilers> XG(X);
46
Misha Brukman60766f72005-01-07 07:05:34 +000047ModulePass *llvm::createFunctionProfilerPass() {
48 return new FunctionProfiler();
Jeff Cohend9ed8c82005-01-07 06:57:28 +000049}
50
Chris Lattnerb12914b2004-09-20 04:48:05 +000051bool FunctionProfiler::runOnModule(Module &M) {
Reid Spencer688b0492007-02-05 21:19:13 +000052 Function *Main = M.getFunction("main");
Chris Lattner94245752003-10-29 21:24:22 +000053 if (Main == 0) {
Benjamin Kramercfa6ec92009-08-23 11:37:21 +000054 errs() << "WARNING: cannot insert function profiling into a module"
55 << " with no main function!\n";
Chris Lattner94245752003-10-29 21:24:22 +000056 return false; // No main, no instrumentation!
57 }
58
59 unsigned NumFunctions = 0;
Misha Brukmanfd939082005-04-21 23:48:37 +000060 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Reid Spencer5cbf9852007-01-30 20:08:39 +000061 if (!I->isDeclaration())
Chris Lattner94245752003-10-29 21:24:22 +000062 ++NumFunctions;
63
Owen Anderson1d0be152009-08-13 21:58:54 +000064 const Type *ATy = ArrayType::get(Type::getInt32Ty(M.getContext()),
65 NumFunctions);
Chris Lattner94245752003-10-29 21:24:22 +000066 GlobalVariable *Counters =
Owen Andersone9b11b42009-07-08 19:03:57 +000067 new GlobalVariable(M, ATy, false, GlobalValue::InternalLinkage,
Owen Andersona7235ea2009-07-31 20:28:14 +000068 Constant::getNullValue(ATy), "FuncProfCounters");
Chris Lattner94245752003-10-29 21:24:22 +000069
Chris Lattner94245752003-10-29 21:24:22 +000070 // Instrument all of the functions...
71 unsigned i = 0;
Misha Brukmanfd939082005-04-21 23:48:37 +000072 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Reid Spencer5cbf9852007-01-30 20:08:39 +000073 if (!I->isDeclaration())
Chris Lattner94245752003-10-29 21:24:22 +000074 // Insert counter at the start of the function
Dan Gohman0db69dc2008-10-21 03:10:28 +000075 IncrementCounterInBlock(&I->getEntryBlock(), i++, Counters);
Chris Lattner94245752003-10-29 21:24:22 +000076
77 // Add the initialization call to main.
Chris Lattner467dd2e2004-03-08 17:06:13 +000078 InsertProfilingInitCall(Main, "llvm_start_func_profiling", Counters);
Chris Lattner94245752003-10-29 21:24:22 +000079 return true;
80}
81
82
83namespace {
Andrew Lenharthbb227c12005-11-28 18:00:38 +000084 class BlockProfiler : public RSProfilers_std {
Chris Lattnerb12914b2004-09-20 04:48:05 +000085 bool runOnModule(Module &M);
Devang Patel794fd752007-05-01 21:15:47 +000086 public:
Devang Patel19974732007-05-03 01:11:54 +000087 static char ID;
Chris Lattner94245752003-10-29 21:24:22 +000088 };
Chris Lattner94245752003-10-29 21:24:22 +000089}
90
Dan Gohman844731a2008-05-13 00:00:25 +000091char BlockProfiler::ID = 0;
92static RegisterPass<BlockProfiler>
93Y("insert-block-profiling", "Insert instrumentation for block profiling");
94static RegisterAnalysisGroup<RSProfilers> YG(Y);
95
Jeff Cohend9ed8c82005-01-07 06:57:28 +000096ModulePass *llvm::createBlockProfilerPass() { return new BlockProfiler(); }
97
Chris Lattnerb12914b2004-09-20 04:48:05 +000098bool BlockProfiler::runOnModule(Module &M) {
Reid Spencer688b0492007-02-05 21:19:13 +000099 Function *Main = M.getFunction("main");
Chris Lattner94245752003-10-29 21:24:22 +0000100 if (Main == 0) {
Benjamin Kramercfa6ec92009-08-23 11:37:21 +0000101 errs() << "WARNING: cannot insert block profiling into a module"
102 << " with no main function!\n";
Chris Lattner94245752003-10-29 21:24:22 +0000103 return false; // No main, no instrumentation!
104 }
105
106 unsigned NumBlocks = 0;
Misha Brukmanfd939082005-04-21 23:48:37 +0000107 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Daniel Dunbarcaaa4932009-08-08 17:43:09 +0000108 if (!I->isDeclaration())
109 NumBlocks += I->size();
Chris Lattner94245752003-10-29 21:24:22 +0000110
Owen Anderson1d0be152009-08-13 21:58:54 +0000111 const Type *ATy = ArrayType::get(Type::getInt32Ty(M.getContext()), NumBlocks);
Chris Lattner94245752003-10-29 21:24:22 +0000112 GlobalVariable *Counters =
Owen Andersone9b11b42009-07-08 19:03:57 +0000113 new GlobalVariable(M, ATy, false, GlobalValue::InternalLinkage,
Owen Andersona7235ea2009-07-31 20:28:14 +0000114 Constant::getNullValue(ATy), "BlockProfCounters");
Chris Lattner94245752003-10-29 21:24:22 +0000115
Chris Lattner94245752003-10-29 21:24:22 +0000116 // Instrument all of the blocks...
117 unsigned i = 0;
Daniel Dunbarcaaa4932009-08-08 17:43:09 +0000118 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
119 if (I->isDeclaration()) continue;
Chris Lattner94245752003-10-29 21:24:22 +0000120 for (Function::iterator BB = I->begin(), E = I->end(); BB != E; ++BB)
121 // Insert counter at the start of the block
Reid Spencer518310c2004-07-18 00:44:37 +0000122 IncrementCounterInBlock(BB, i++, Counters);
Daniel Dunbarcaaa4932009-08-08 17:43:09 +0000123 }
Chris Lattner94245752003-10-29 21:24:22 +0000124
125 // Add the initialization call to main.
Chris Lattner467dd2e2004-03-08 17:06:13 +0000126 InsertProfilingInitCall(Main, "llvm_start_block_profiling", Counters);
Chris Lattner94245752003-10-29 21:24:22 +0000127 return true;
128}
Brian Gaeked0fde302003-11-11 22:41:34 +0000129