blob: af2928b0fce91351d4e438d819011aba62e8b503 [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"
Bill Wendling62c804a2006-11-26 09:17:06 +000026#include "llvm/Support/Streams.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 {
Andrew Lenharthbb227c12005-11-28 18:00:38 +000033 class FunctionProfiler : public RSProfilers_std {
Chris Lattnerb12914b2004-09-20 04:48:05 +000034 bool runOnModule(Module &M);
Chris Lattner94245752003-10-29 21:24:22 +000035 };
36
Chris Lattner7f8897f2006-08-27 22:42:52 +000037 RegisterPass<FunctionProfiler> X("insert-function-profiling",
Chris Lattner94245752003-10-29 21:24:22 +000038 "Insert instrumentation for function profiling");
Chris Lattnera5370172006-08-28 00:42:29 +000039 RegisterAnalysisGroup<RSProfilers> XG(X);
Andrew Lenharthbb227c12005-11-28 18:00:38 +000040
Chris Lattner94245752003-10-29 21:24:22 +000041}
42
Misha Brukman60766f72005-01-07 07:05:34 +000043ModulePass *llvm::createFunctionProfilerPass() {
44 return new FunctionProfiler();
Jeff Cohend9ed8c82005-01-07 06:57:28 +000045}
46
Chris Lattnerb12914b2004-09-20 04:48:05 +000047bool FunctionProfiler::runOnModule(Module &M) {
Chris Lattner94245752003-10-29 21:24:22 +000048 Function *Main = M.getMainFunction();
49 if (Main == 0) {
Bill Wendling62c804a2006-11-26 09:17:06 +000050 llvm_cerr << "WARNING: cannot insert function profiling into a module"
Chris Lattner94245752003-10-29 21:24:22 +000051 << " with no main function!\n";
52 return false; // No main, no instrumentation!
53 }
54
55 unsigned NumFunctions = 0;
Misha Brukmanfd939082005-04-21 23:48:37 +000056 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattner94245752003-10-29 21:24:22 +000057 if (!I->isExternal())
58 ++NumFunctions;
59
60 const Type *ATy = ArrayType::get(Type::UIntTy, NumFunctions);
61 GlobalVariable *Counters =
62 new GlobalVariable(ATy, false, GlobalValue::InternalLinkage,
63 Constant::getNullValue(ATy), "FuncProfCounters", &M);
64
Chris Lattner94245752003-10-29 21:24:22 +000065 // Instrument all of the functions...
66 unsigned i = 0;
Misha Brukmanfd939082005-04-21 23:48:37 +000067 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattner94245752003-10-29 21:24:22 +000068 if (!I->isExternal())
69 // Insert counter at the start of the function
Reid Spencer518310c2004-07-18 00:44:37 +000070 IncrementCounterInBlock(I->begin(), i++, Counters);
Chris Lattner94245752003-10-29 21:24:22 +000071
72 // Add the initialization call to main.
Chris Lattner467dd2e2004-03-08 17:06:13 +000073 InsertProfilingInitCall(Main, "llvm_start_func_profiling", Counters);
Chris Lattner94245752003-10-29 21:24:22 +000074 return true;
75}
76
77
78namespace {
Andrew Lenharthbb227c12005-11-28 18:00:38 +000079 class BlockProfiler : public RSProfilers_std {
Chris Lattnerb12914b2004-09-20 04:48:05 +000080 bool runOnModule(Module &M);
Chris Lattner94245752003-10-29 21:24:22 +000081 };
82
Chris Lattner7f8897f2006-08-27 22:42:52 +000083 RegisterPass<BlockProfiler> Y("insert-block-profiling",
84 "Insert instrumentation for block profiling");
Chris Lattnera5370172006-08-28 00:42:29 +000085 RegisterAnalysisGroup<RSProfilers> YG(Y);
Chris Lattner94245752003-10-29 21:24:22 +000086}
87
Jeff Cohend9ed8c82005-01-07 06:57:28 +000088ModulePass *llvm::createBlockProfilerPass() { return new BlockProfiler(); }
89
Chris Lattnerb12914b2004-09-20 04:48:05 +000090bool BlockProfiler::runOnModule(Module &M) {
Chris Lattner94245752003-10-29 21:24:22 +000091 Function *Main = M.getMainFunction();
92 if (Main == 0) {
Bill Wendling62c804a2006-11-26 09:17:06 +000093 llvm_cerr << "WARNING: cannot insert block profiling into a module"
Chris Lattner94245752003-10-29 21:24:22 +000094 << " with no main function!\n";
95 return false; // No main, no instrumentation!
96 }
97
98 unsigned NumBlocks = 0;
Misha Brukmanfd939082005-04-21 23:48:37 +000099 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattner94245752003-10-29 21:24:22 +0000100 NumBlocks += I->size();
101
102 const Type *ATy = ArrayType::get(Type::UIntTy, NumBlocks);
103 GlobalVariable *Counters =
104 new GlobalVariable(ATy, false, GlobalValue::InternalLinkage,
105 Constant::getNullValue(ATy), "BlockProfCounters", &M);
106
Chris Lattner94245752003-10-29 21:24:22 +0000107 // Instrument all of the blocks...
108 unsigned i = 0;
Misha Brukmanfd939082005-04-21 23:48:37 +0000109 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattner94245752003-10-29 21:24:22 +0000110 for (Function::iterator BB = I->begin(), E = I->end(); BB != E; ++BB)
111 // Insert counter at the start of the block
Reid Spencer518310c2004-07-18 00:44:37 +0000112 IncrementCounterInBlock(BB, i++, Counters);
Chris Lattner94245752003-10-29 21:24:22 +0000113
114 // Add the initialization call to main.
Chris Lattner467dd2e2004-03-08 17:06:13 +0000115 InsertProfilingInitCall(Main, "llvm_start_block_profiling", Counters);
Chris Lattner94245752003-10-29 21:24:22 +0000116 return true;
117}
Brian Gaeked0fde302003-11-11 22:41:34 +0000118