blob: 9139c92235baad1804ab19e612bd147e05d44820 [file] [log] [blame]
Chris Lattnerc8ba0672003-10-28 18:59:04 +00001//===- BlockProfiling.cpp - Insert counters for block profiling -----------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
Chris Lattnerc8ba0672003-10-28 18:59:04 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Brukmanb1c93172005-04-21 23:48:37 +00007//
Chris Lattnerc8ba0672003-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"
Owen Anderson39f00cc2009-07-06 18:42:36 +000024#include "llvm/LLVMContext.h"
Chris Lattnerc8ba0672003-10-28 18:59:04 +000025#include "llvm/Module.h"
26#include "llvm/Pass.h"
Reid Spencer557ab152007-02-05 23:32:05 +000027#include "llvm/Support/Compiler.h"
Bill Wendlinga7459ca2006-11-26 09:17:06 +000028#include "llvm/Support/Streams.h"
Jeff Cohen9a7ac162005-01-07 06:57:28 +000029#include "llvm/Transforms/Instrumentation.h"
Andrew Lenharth517caef2005-11-28 18:00:38 +000030#include "RSProfiling.h"
Chris Lattnerdae48f92004-03-08 17:06:13 +000031#include "ProfilingUtils.h"
Chris Lattnerdf3c3422004-01-09 06:12:26 +000032using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000033
Chris Lattner34201372003-10-29 21:24:22 +000034namespace {
Reid Spencer557ab152007-02-05 23:32:05 +000035 class VISIBILITY_HIDDEN FunctionProfiler : public RSProfilers_std {
Devang Patel09f162c2007-05-01 21:15:47 +000036 public:
Devang Patel8c78a0b2007-05-03 01:11:54 +000037 static char ID;
Chris Lattner4f2cf032004-09-20 04:48:05 +000038 bool runOnModule(Module &M);
Chris Lattner34201372003-10-29 21:24:22 +000039 };
Chris Lattner34201372003-10-29 21:24:22 +000040}
41
Dan Gohmand78c4002008-05-13 00:00:25 +000042char FunctionProfiler::ID = 0;
43
44static RegisterPass<FunctionProfiler>
45X("insert-function-profiling",
46 "Insert instrumentation for function profiling");
47static RegisterAnalysisGroup<RSProfilers> XG(X);
48
Misha Brukman417ca172005-01-07 07:05:34 +000049ModulePass *llvm::createFunctionProfilerPass() {
50 return new FunctionProfiler();
Jeff Cohen9a7ac162005-01-07 06:57:28 +000051}
52
Chris Lattner4f2cf032004-09-20 04:48:05 +000053bool FunctionProfiler::runOnModule(Module &M) {
Reid Spencer1241d6d2007-02-05 21:19:13 +000054 Function *Main = M.getFunction("main");
Chris Lattner34201372003-10-29 21:24:22 +000055 if (Main == 0) {
Bill Wendlingf3baad32006-12-07 01:30:32 +000056 cerr << "WARNING: cannot insert function profiling into a module"
57 << " with no main function!\n";
Chris Lattner34201372003-10-29 21:24:22 +000058 return false; // No main, no instrumentation!
59 }
60
61 unsigned NumFunctions = 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +000062 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Reid Spencer5301e7c2007-01-30 20:08:39 +000063 if (!I->isDeclaration())
Chris Lattner34201372003-10-29 21:24:22 +000064 ++NumFunctions;
65
Owen Anderson4056ca92009-07-29 22:17:13 +000066 const Type *ATy = ArrayType::get(Type::Int32Ty, NumFunctions);
Chris Lattner34201372003-10-29 21:24:22 +000067 GlobalVariable *Counters =
Owen Andersonb17f3292009-07-08 19:03:57 +000068 new GlobalVariable(M, ATy, false, GlobalValue::InternalLinkage,
Owen Anderson5a1acd92009-07-31 20:28:14 +000069 Constant::getNullValue(ATy), "FuncProfCounters");
Chris Lattner34201372003-10-29 21:24:22 +000070
Chris Lattner34201372003-10-29 21:24:22 +000071 // Instrument all of the functions...
72 unsigned i = 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +000073 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Reid Spencer5301e7c2007-01-30 20:08:39 +000074 if (!I->isDeclaration())
Chris Lattner34201372003-10-29 21:24:22 +000075 // Insert counter at the start of the function
Dan Gohman72e66ee2008-10-21 03:10:28 +000076 IncrementCounterInBlock(&I->getEntryBlock(), i++, Counters);
Chris Lattner34201372003-10-29 21:24:22 +000077
78 // Add the initialization call to main.
Chris Lattnerdae48f92004-03-08 17:06:13 +000079 InsertProfilingInitCall(Main, "llvm_start_func_profiling", Counters);
Chris Lattner34201372003-10-29 21:24:22 +000080 return true;
81}
82
83
84namespace {
Andrew Lenharth517caef2005-11-28 18:00:38 +000085 class BlockProfiler : public RSProfilers_std {
Chris Lattner4f2cf032004-09-20 04:48:05 +000086 bool runOnModule(Module &M);
Devang Patel09f162c2007-05-01 21:15:47 +000087 public:
Devang Patel8c78a0b2007-05-03 01:11:54 +000088 static char ID;
Chris Lattner34201372003-10-29 21:24:22 +000089 };
Chris Lattner34201372003-10-29 21:24:22 +000090}
91
Dan Gohmand78c4002008-05-13 00:00:25 +000092char BlockProfiler::ID = 0;
93static RegisterPass<BlockProfiler>
94Y("insert-block-profiling", "Insert instrumentation for block profiling");
95static RegisterAnalysisGroup<RSProfilers> YG(Y);
96
Jeff Cohen9a7ac162005-01-07 06:57:28 +000097ModulePass *llvm::createBlockProfilerPass() { return new BlockProfiler(); }
98
Chris Lattner4f2cf032004-09-20 04:48:05 +000099bool BlockProfiler::runOnModule(Module &M) {
Reid Spencer1241d6d2007-02-05 21:19:13 +0000100 Function *Main = M.getFunction("main");
Chris Lattner34201372003-10-29 21:24:22 +0000101 if (Main == 0) {
Bill Wendlingf3baad32006-12-07 01:30:32 +0000102 cerr << "WARNING: cannot insert block profiling into a module"
103 << " with no main function!\n";
Chris Lattner34201372003-10-29 21:24:22 +0000104 return false; // No main, no instrumentation!
105 }
106
107 unsigned NumBlocks = 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000108 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattner34201372003-10-29 21:24:22 +0000109 NumBlocks += I->size();
110
Owen Anderson4056ca92009-07-29 22:17:13 +0000111 const Type *ATy = ArrayType::get(Type::Int32Ty, NumBlocks);
Chris Lattner34201372003-10-29 21:24:22 +0000112 GlobalVariable *Counters =
Owen Andersonb17f3292009-07-08 19:03:57 +0000113 new GlobalVariable(M, ATy, false, GlobalValue::InternalLinkage,
Owen Anderson5a1acd92009-07-31 20:28:14 +0000114 Constant::getNullValue(ATy), "BlockProfCounters");
Chris Lattner34201372003-10-29 21:24:22 +0000115
Chris Lattner34201372003-10-29 21:24:22 +0000116 // Instrument all of the blocks...
117 unsigned i = 0;
Misha Brukmanb1c93172005-04-21 23:48:37 +0000118 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattner34201372003-10-29 21:24:22 +0000119 for (Function::iterator BB = I->begin(), E = I->end(); BB != E; ++BB)
120 // Insert counter at the start of the block
Reid Spencercb3fb5d2004-07-18 00:44:37 +0000121 IncrementCounterInBlock(BB, i++, Counters);
Chris Lattner34201372003-10-29 21:24:22 +0000122
123 // Add the initialization call to main.
Chris Lattnerdae48f92004-03-08 17:06:13 +0000124 InsertProfilingInitCall(Main, "llvm_start_block_profiling", Counters);
Chris Lattner34201372003-10-29 21:24:22 +0000125 return true;
126}
Brian Gaeke960707c2003-11-11 22:41:34 +0000127