blob: f92f0ef6f0362d461cff16bceb8276c85a36925c [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"
Chris Lattner467dd2e2004-03-08 17:06:13 +000027#include "ProfilingUtils.h"
Reid Spencer954da372004-07-04 12:19:56 +000028#include <iostream>
29
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 {
Chris Lattnerb12914b2004-09-20 04:48:05 +000033 class FunctionProfiler : public ModulePass {
34 bool runOnModule(Module &M);
Chris Lattner94245752003-10-29 21:24:22 +000035 };
36
37 RegisterOpt<FunctionProfiler> X("insert-function-profiling",
38 "Insert instrumentation for function profiling");
39}
40
Misha Brukman60766f72005-01-07 07:05:34 +000041ModulePass *llvm::createFunctionProfilerPass() {
42 return new FunctionProfiler();
Jeff Cohend9ed8c82005-01-07 06:57:28 +000043}
44
Chris Lattnerb12914b2004-09-20 04:48:05 +000045bool FunctionProfiler::runOnModule(Module &M) {
Chris Lattner94245752003-10-29 21:24:22 +000046 Function *Main = M.getMainFunction();
47 if (Main == 0) {
48 std::cerr << "WARNING: cannot insert function profiling into a module"
49 << " with no main function!\n";
50 return false; // No main, no instrumentation!
51 }
52
53 unsigned NumFunctions = 0;
Misha Brukmanfd939082005-04-21 23:48:37 +000054 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattner94245752003-10-29 21:24:22 +000055 if (!I->isExternal())
56 ++NumFunctions;
57
58 const Type *ATy = ArrayType::get(Type::UIntTy, NumFunctions);
59 GlobalVariable *Counters =
60 new GlobalVariable(ATy, false, GlobalValue::InternalLinkage,
61 Constant::getNullValue(ATy), "FuncProfCounters", &M);
62
Chris Lattner94245752003-10-29 21:24:22 +000063 // Instrument all of the functions...
64 unsigned i = 0;
Misha Brukmanfd939082005-04-21 23:48:37 +000065 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattner94245752003-10-29 21:24:22 +000066 if (!I->isExternal())
67 // Insert counter at the start of the function
Reid Spencer518310c2004-07-18 00:44:37 +000068 IncrementCounterInBlock(I->begin(), i++, Counters);
Chris Lattner94245752003-10-29 21:24:22 +000069
70 // Add the initialization call to main.
Chris Lattner467dd2e2004-03-08 17:06:13 +000071 InsertProfilingInitCall(Main, "llvm_start_func_profiling", Counters);
Chris Lattner94245752003-10-29 21:24:22 +000072 return true;
73}
74
75
76namespace {
Chris Lattnerb12914b2004-09-20 04:48:05 +000077 class BlockProfiler : public ModulePass {
78 bool runOnModule(Module &M);
Chris Lattner94245752003-10-29 21:24:22 +000079 };
80
81 RegisterOpt<BlockProfiler> Y("insert-block-profiling",
82 "Insert instrumentation for block profiling");
83}
84
Jeff Cohend9ed8c82005-01-07 06:57:28 +000085ModulePass *llvm::createBlockProfilerPass() { return new BlockProfiler(); }
86
Chris Lattnerb12914b2004-09-20 04:48:05 +000087bool BlockProfiler::runOnModule(Module &M) {
Chris Lattner94245752003-10-29 21:24:22 +000088 Function *Main = M.getMainFunction();
89 if (Main == 0) {
90 std::cerr << "WARNING: cannot insert block profiling into a module"
91 << " with no main function!\n";
92 return false; // No main, no instrumentation!
93 }
94
95 unsigned NumBlocks = 0;
Misha Brukmanfd939082005-04-21 23:48:37 +000096 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattner94245752003-10-29 21:24:22 +000097 NumBlocks += I->size();
98
99 const Type *ATy = ArrayType::get(Type::UIntTy, NumBlocks);
100 GlobalVariable *Counters =
101 new GlobalVariable(ATy, false, GlobalValue::InternalLinkage,
102 Constant::getNullValue(ATy), "BlockProfCounters", &M);
103
Chris Lattner94245752003-10-29 21:24:22 +0000104 // Instrument all of the blocks...
105 unsigned i = 0;
Misha Brukmanfd939082005-04-21 23:48:37 +0000106 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
Chris Lattner94245752003-10-29 21:24:22 +0000107 for (Function::iterator BB = I->begin(), E = I->end(); BB != E; ++BB)
108 // Insert counter at the start of the block
Reid Spencer518310c2004-07-18 00:44:37 +0000109 IncrementCounterInBlock(BB, i++, Counters);
Chris Lattner94245752003-10-29 21:24:22 +0000110
111 // Add the initialization call to main.
Chris Lattner467dd2e2004-03-08 17:06:13 +0000112 InsertProfilingInitCall(Main, "llvm_start_block_profiling", Counters);
Chris Lattner94245752003-10-29 21:24:22 +0000113 return true;
114}
Brian Gaeked0fde302003-11-11 22:41:34 +0000115