blob: f772dd490b172942424cc0dfbbe466381cf81e39 [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//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source 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
22#include "llvm/Constants.h"
23#include "llvm/DerivedTypes.h"
Chris Lattnerbaa20072003-10-28 18:59:04 +000024#include "llvm/Module.h"
25#include "llvm/Pass.h"
Reid Spencer9133fe22007-02-05 23:32:05 +000026#include "llvm/Support/Compiler.h"
Bill Wendling62c804a2006-11-26 09:17:06 +000027#include "llvm/Support/Streams.h"
Jeff Cohend9ed8c82005-01-07 06:57:28 +000028#include "llvm/Transforms/Instrumentation.h"
Andrew Lenharthbb227c12005-11-28 18:00:38 +000029#include "RSProfiling.h"
Chris Lattner467dd2e2004-03-08 17:06:13 +000030#include "ProfilingUtils.h"
Chris Lattnerf7703df2004-01-09 06:12:26 +000031using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000032
Chris Lattner94245752003-10-29 21:24:22 +000033namespace {
Reid Spencer9133fe22007-02-05 23:32:05 +000034 class VISIBILITY_HIDDEN FunctionProfiler : public RSProfilers_std {
Devang Patel794fd752007-05-01 21:15:47 +000035 public:
Devang Patel19974732007-05-03 01:11:54 +000036 static char ID;
Chris Lattnerb12914b2004-09-20 04:48:05 +000037 bool runOnModule(Module &M);
Chris Lattner94245752003-10-29 21:24:22 +000038 };
39
Devang Patel19974732007-05-03 01:11:54 +000040 char FunctionProfiler::ID = 0;
Devang Patel794fd752007-05-01 21:15:47 +000041
Chris Lattner7f8897f2006-08-27 22:42:52 +000042 RegisterPass<FunctionProfiler> X("insert-function-profiling",
Chris Lattner94245752003-10-29 21:24:22 +000043 "Insert instrumentation for function profiling");
Chris Lattnera5370172006-08-28 00:42:29 +000044 RegisterAnalysisGroup<RSProfilers> XG(X);
Andrew Lenharthbb227c12005-11-28 18:00:38 +000045
Chris Lattner94245752003-10-29 21:24:22 +000046}
47
Misha Brukman60766f72005-01-07 07:05:34 +000048ModulePass *llvm::createFunctionProfilerPass() {
49 return new FunctionProfiler();
Jeff Cohend9ed8c82005-01-07 06:57:28 +000050}
51
Chris Lattnerb12914b2004-09-20 04:48:05 +000052bool FunctionProfiler::runOnModule(Module &M) {
Reid Spencer688b0492007-02-05 21:19:13 +000053 Function *Main = M.getFunction("main");
Chris Lattner94245752003-10-29 21:24:22 +000054 if (Main == 0) {
Bill Wendlinge8156192006-12-07 01:30:32 +000055 cerr << "WARNING: cannot insert function profiling into a module"
56 << " with no main function!\n";
Chris Lattner94245752003-10-29 21:24:22 +000057 return false; // No main, no instrumentation!
58 }
59
60 unsigned NumFunctions = 0;
Misha Brukmanfd939082005-04-21 23:48:37 +000061 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Reid Spencer5cbf9852007-01-30 20:08:39 +000062 if (!I->isDeclaration())
Chris Lattner94245752003-10-29 21:24:22 +000063 ++NumFunctions;
64
Reid Spencerc5b206b2006-12-31 05:48:39 +000065 const Type *ATy = ArrayType::get(Type::Int32Ty, NumFunctions);
Chris Lattner94245752003-10-29 21:24:22 +000066 GlobalVariable *Counters =
67 new GlobalVariable(ATy, false, GlobalValue::InternalLinkage,
68 Constant::getNullValue(ATy), "FuncProfCounters", &M);
69
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
Reid Spencer518310c2004-07-18 00:44:37 +000075 IncrementCounterInBlock(I->begin(), 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 };
89
Devang Patel19974732007-05-03 01:11:54 +000090 char BlockProfiler::ID = 0;
Chris Lattner7f8897f2006-08-27 22:42:52 +000091 RegisterPass<BlockProfiler> Y("insert-block-profiling",
92 "Insert instrumentation for block profiling");
Chris Lattnera5370172006-08-28 00:42:29 +000093 RegisterAnalysisGroup<RSProfilers> YG(Y);
Chris Lattner94245752003-10-29 21:24:22 +000094}
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) {
Bill Wendlinge8156192006-12-07 01:30:32 +0000101 cerr << "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)
Chris Lattner94245752003-10-29 21:24:22 +0000108 NumBlocks += I->size();
109
Reid Spencerc5b206b2006-12-31 05:48:39 +0000110 const Type *ATy = ArrayType::get(Type::Int32Ty, NumBlocks);
Chris Lattner94245752003-10-29 21:24:22 +0000111 GlobalVariable *Counters =
112 new GlobalVariable(ATy, false, GlobalValue::InternalLinkage,
113 Constant::getNullValue(ATy), "BlockProfCounters", &M);
114
Chris Lattner94245752003-10-29 21:24:22 +0000115 // Instrument all of the blocks...
116 unsigned i = 0;
Misha Brukmanfd939082005-04-21 23:48:37 +0000117 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattner94245752003-10-29 21:24:22 +0000118 for (Function::iterator BB = I->begin(), E = I->end(); BB != E; ++BB)
119 // Insert counter at the start of the block
Reid Spencer518310c2004-07-18 00:44:37 +0000120 IncrementCounterInBlock(BB, i++, Counters);
Chris Lattner94245752003-10-29 21:24:22 +0000121
122 // Add the initialization call to main.
Chris Lattner467dd2e2004-03-08 17:06:13 +0000123 InsertProfilingInitCall(Main, "llvm_start_block_profiling", Counters);
Chris Lattner94245752003-10-29 21:24:22 +0000124 return true;
125}
Brian Gaeked0fde302003-11-11 22:41:34 +0000126