blob: 7e1f5cc76ca8f8fba705377ac8a0c2daf771b06a [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"
Jeff Cohend9ed8c82005-01-07 06:57:28 +000026#include "llvm/Transforms/Instrumentation.h"
Andrew Lenharthbb227c12005-11-28 18:00:38 +000027#include "RSProfiling.h"
Chris Lattner467dd2e2004-03-08 17:06:13 +000028#include "ProfilingUtils.h"
Reid Spencer954da372004-07-04 12:19:56 +000029#include <iostream>
30
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 {
Andrew Lenharthbb227c12005-11-28 18:00:38 +000034 class FunctionProfiler : public RSProfilers_std {
Chris Lattnerb12914b2004-09-20 04:48:05 +000035 bool runOnModule(Module &M);
Chris Lattner94245752003-10-29 21:24:22 +000036 };
37
Chris Lattner7f8897f2006-08-27 22:42:52 +000038 RegisterPass<FunctionProfiler> X("insert-function-profiling",
Chris Lattner94245752003-10-29 21:24:22 +000039 "Insert instrumentation for function profiling");
Chris Lattnera5370172006-08-28 00:42:29 +000040 RegisterAnalysisGroup<RSProfilers> XG(X);
Andrew Lenharthbb227c12005-11-28 18:00:38 +000041
Chris Lattner94245752003-10-29 21:24:22 +000042}
43
Misha Brukman60766f72005-01-07 07:05:34 +000044ModulePass *llvm::createFunctionProfilerPass() {
45 return new FunctionProfiler();
Jeff Cohend9ed8c82005-01-07 06:57:28 +000046}
47
Chris Lattnerb12914b2004-09-20 04:48:05 +000048bool FunctionProfiler::runOnModule(Module &M) {
Chris Lattner94245752003-10-29 21:24:22 +000049 Function *Main = M.getMainFunction();
50 if (Main == 0) {
51 std::cerr << "WARNING: cannot insert function profiling into a module"
52 << " with no main function!\n";
53 return false; // No main, no instrumentation!
54 }
55
56 unsigned NumFunctions = 0;
Misha Brukmanfd939082005-04-21 23:48:37 +000057 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattner94245752003-10-29 21:24:22 +000058 if (!I->isExternal())
59 ++NumFunctions;
60
61 const Type *ATy = ArrayType::get(Type::UIntTy, NumFunctions);
62 GlobalVariable *Counters =
63 new GlobalVariable(ATy, false, GlobalValue::InternalLinkage,
64 Constant::getNullValue(ATy), "FuncProfCounters", &M);
65
Chris Lattner94245752003-10-29 21:24:22 +000066 // Instrument all of the functions...
67 unsigned i = 0;
Misha Brukmanfd939082005-04-21 23:48:37 +000068 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattner94245752003-10-29 21:24:22 +000069 if (!I->isExternal())
70 // Insert counter at the start of the function
Reid Spencer518310c2004-07-18 00:44:37 +000071 IncrementCounterInBlock(I->begin(), i++, Counters);
Chris Lattner94245752003-10-29 21:24:22 +000072
73 // Add the initialization call to main.
Chris Lattner467dd2e2004-03-08 17:06:13 +000074 InsertProfilingInitCall(Main, "llvm_start_func_profiling", Counters);
Chris Lattner94245752003-10-29 21:24:22 +000075 return true;
76}
77
78
79namespace {
Andrew Lenharthbb227c12005-11-28 18:00:38 +000080 class BlockProfiler : public RSProfilers_std {
Chris Lattnerb12914b2004-09-20 04:48:05 +000081 bool runOnModule(Module &M);
Chris Lattner94245752003-10-29 21:24:22 +000082 };
83
Chris Lattner7f8897f2006-08-27 22:42:52 +000084 RegisterPass<BlockProfiler> Y("insert-block-profiling",
85 "Insert instrumentation for block profiling");
Chris Lattnera5370172006-08-28 00:42:29 +000086 RegisterAnalysisGroup<RSProfilers> YG(Y);
Chris Lattner94245752003-10-29 21:24:22 +000087}
88
Jeff Cohend9ed8c82005-01-07 06:57:28 +000089ModulePass *llvm::createBlockProfilerPass() { return new BlockProfiler(); }
90
Chris Lattnerb12914b2004-09-20 04:48:05 +000091bool BlockProfiler::runOnModule(Module &M) {
Chris Lattner94245752003-10-29 21:24:22 +000092 Function *Main = M.getMainFunction();
93 if (Main == 0) {
94 std::cerr << "WARNING: cannot insert block profiling into a module"
95 << " with no main function!\n";
96 return false; // No main, no instrumentation!
97 }
98
99 unsigned NumBlocks = 0;
Misha Brukmanfd939082005-04-21 23:48:37 +0000100 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattner94245752003-10-29 21:24:22 +0000101 NumBlocks += I->size();
102
103 const Type *ATy = ArrayType::get(Type::UIntTy, NumBlocks);
104 GlobalVariable *Counters =
105 new GlobalVariable(ATy, false, GlobalValue::InternalLinkage,
106 Constant::getNullValue(ATy), "BlockProfCounters", &M);
107
Chris Lattner94245752003-10-29 21:24:22 +0000108 // Instrument all of the blocks...
109 unsigned i = 0;
Misha Brukmanfd939082005-04-21 23:48:37 +0000110 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattner94245752003-10-29 21:24:22 +0000111 for (Function::iterator BB = I->begin(), E = I->end(); BB != E; ++BB)
112 // Insert counter at the start of the block
Reid Spencer518310c2004-07-18 00:44:37 +0000113 IncrementCounterInBlock(BB, i++, Counters);
Chris Lattner94245752003-10-29 21:24:22 +0000114
115 // Add the initialization call to main.
Chris Lattner467dd2e2004-03-08 17:06:13 +0000116 InsertProfilingInitCall(Main, "llvm_start_block_profiling", Counters);
Chris Lattner94245752003-10-29 21:24:22 +0000117 return true;
118}
Brian Gaeked0fde302003-11-11 22:41:34 +0000119