blob: c518821a6dddd8f192d4591ebee492ab7c182d8a [file] [log] [blame]
Chris Lattnerbaa20072003-10-28 18:59:04 +00001//===- BlockProfiling.cpp - Insert counters for block profiling -----------===//
2//
3// 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.
7//
8//===----------------------------------------------------------------------===//
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"
Chris Lattner467dd2e2004-03-08 17:06:13 +000026#include "ProfilingUtils.h"
Chris Lattnerf7703df2004-01-09 06:12:26 +000027using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000028
Chris Lattner94245752003-10-29 21:24:22 +000029namespace {
30 class FunctionProfiler : public Pass {
31 bool run(Module &M);
32 };
33
34 RegisterOpt<FunctionProfiler> X("insert-function-profiling",
35 "Insert instrumentation for function profiling");
36}
37
38bool FunctionProfiler::run(Module &M) {
39 Function *Main = M.getMainFunction();
40 if (Main == 0) {
41 std::cerr << "WARNING: cannot insert function profiling into a module"
42 << " with no main function!\n";
43 return false; // No main, no instrumentation!
44 }
45
46 unsigned NumFunctions = 0;
47 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
48 if (!I->isExternal())
49 ++NumFunctions;
50
51 const Type *ATy = ArrayType::get(Type::UIntTy, NumFunctions);
52 GlobalVariable *Counters =
53 new GlobalVariable(ATy, false, GlobalValue::InternalLinkage,
54 Constant::getNullValue(ATy), "FuncProfCounters", &M);
55
56 ConstantPointerRef *CounterCPR = ConstantPointerRef::get(Counters);
57
58 // Instrument all of the functions...
59 unsigned i = 0;
60 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
61 if (!I->isExternal())
62 // Insert counter at the start of the function
63 IncrementCounterInBlock(I->begin(), i++, CounterCPR);
64
65 // Add the initialization call to main.
Chris Lattner467dd2e2004-03-08 17:06:13 +000066 InsertProfilingInitCall(Main, "llvm_start_func_profiling", Counters);
Chris Lattner94245752003-10-29 21:24:22 +000067 return true;
68}
69
70
71namespace {
72 class BlockProfiler : public Pass {
73 bool run(Module &M);
74 };
75
76 RegisterOpt<BlockProfiler> Y("insert-block-profiling",
77 "Insert instrumentation for block profiling");
78}
79
80bool BlockProfiler::run(Module &M) {
81 Function *Main = M.getMainFunction();
82 if (Main == 0) {
83 std::cerr << "WARNING: cannot insert block profiling into a module"
84 << " with no main function!\n";
85 return false; // No main, no instrumentation!
86 }
87
88 unsigned NumBlocks = 0;
89 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
90 NumBlocks += I->size();
91
92 const Type *ATy = ArrayType::get(Type::UIntTy, NumBlocks);
93 GlobalVariable *Counters =
94 new GlobalVariable(ATy, false, GlobalValue::InternalLinkage,
95 Constant::getNullValue(ATy), "BlockProfCounters", &M);
96
97 ConstantPointerRef *CounterCPR = ConstantPointerRef::get(Counters);
98
99 // Instrument all of the blocks...
100 unsigned i = 0;
101 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
102 for (Function::iterator BB = I->begin(), E = I->end(); BB != E; ++BB)
103 // Insert counter at the start of the block
104 IncrementCounterInBlock(BB, i++, CounterCPR);
105
106 // Add the initialization call to main.
Chris Lattner467dd2e2004-03-08 17:06:13 +0000107 InsertProfilingInitCall(Main, "llvm_start_block_profiling", Counters);
Chris Lattner94245752003-10-29 21:24:22 +0000108 return true;
109}
Brian Gaeked0fde302003-11-11 22:41:34 +0000110