blob: c371a9fa694be1c9883828d7e7b6bff077591b4e [file] [log] [blame]
Chris Lattnerc8ba0672003-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"
24#include "llvm/Instructions.h"
25#include "llvm/Module.h"
26#include "llvm/Pass.h"
27
Chris Lattner34201372003-10-29 21:24:22 +000028static void insertInitializationCall(Function *MainFn, const char *FnName,
29 GlobalValue *Array) {
Chris Lattnerc8ba0672003-10-28 18:59:04 +000030 const Type *ArgVTy = PointerType::get(PointerType::get(Type::SByteTy));
31 const Type *UIntPtr = PointerType::get(Type::UIntTy);
32 Module &M = *MainFn->getParent();
33 Function *InitFn = M.getOrInsertFunction(FnName, Type::VoidTy, Type::IntTy,
34 ArgVTy, UIntPtr, Type::UIntTy, 0);
35
36 // This could force argc and argv into programs that wouldn't otherwise have
37 // them, but instead we just pass null values in.
38 std::vector<Value*> Args(4);
39 Args[0] = Constant::getNullValue(Type::IntTy);
40 Args[1] = Constant::getNullValue(ArgVTy);
41
Chris Lattner183fa7c2003-10-28 22:42:24 +000042 // Skip over any allocas in the entry block.
43 BasicBlock *Entry = MainFn->begin();
44 BasicBlock::iterator InsertPos = Entry->begin();
45 while (isa<AllocaInst>(InsertPos)) ++InsertPos;
46
47 Function::aiterator AI;
Chris Lattnerc8ba0672003-10-28 18:59:04 +000048 switch (MainFn->asize()) {
49 default:
50 case 2:
Chris Lattner183fa7c2003-10-28 22:42:24 +000051 AI = MainFn->abegin(); ++AI;
52 if (AI->getType() != ArgVTy) {
53 Args[1] = new CastInst(AI, ArgVTy, "argv.cast", InsertPos);
54 } else {
55 Args[1] = AI;
56 }
57
Chris Lattnerc8ba0672003-10-28 18:59:04 +000058 case 1:
Chris Lattner183fa7c2003-10-28 22:42:24 +000059 AI = MainFn->abegin();
60 if (AI->getType() != Type::IntTy) {
61 Args[0] = new CastInst(AI, Type::IntTy, "argc.cast", InsertPos);
62 } else {
63 Args[0] = AI;
64 }
65
Chris Lattnerc8ba0672003-10-28 18:59:04 +000066 case 0:
67 break;
68 }
69
70 ConstantPointerRef *ArrayCPR = ConstantPointerRef::get(Array);
71 std::vector<Constant*> GEPIndices(2, Constant::getNullValue(Type::LongTy));
72 Args[2] = ConstantExpr::getGetElementPtr(ArrayCPR, GEPIndices);
73
74 unsigned NumElements =
75 cast<ArrayType>(Array->getType()->getElementType())->getNumElements();
76 Args[3] = ConstantUInt::get(Type::UIntTy, NumElements);
77
Chris Lattnerc8ba0672003-10-28 18:59:04 +000078 new CallInst(InitFn, Args, "", InsertPos);
79}
Chris Lattner34201372003-10-29 21:24:22 +000080
81static void IncrementCounterInBlock(BasicBlock *BB, unsigned CounterNum,
82 ConstantPointerRef *CounterArray) {
83 // Insert the increment after any alloca or PHI instructions...
84 BasicBlock::iterator InsertPos = BB->begin();
85 while (isa<AllocaInst>(InsertPos) || isa<PHINode>(InsertPos))
86 ++InsertPos;
87
88 // Create the getelementptr constant expression
89 std::vector<Constant*> Indices(2);
90 Indices[0] = Constant::getNullValue(Type::LongTy);
91 Indices[1] = ConstantSInt::get(Type::LongTy, CounterNum);
92 Constant *ElementPtr = ConstantExpr::getGetElementPtr(CounterArray, Indices);
93
94 // Load, increment and store the value back.
95 Value *OldVal = new LoadInst(ElementPtr, "OldFuncCounter", InsertPos);
96 Value *NewVal = BinaryOperator::create(Instruction::Add, OldVal,
97 ConstantInt::get(Type::UIntTy, 1),
98 "NewFuncCounter", InsertPos);
99 new StoreInst(NewVal, ElementPtr, InsertPos);
100}
101
102
103namespace {
104 class FunctionProfiler : public Pass {
105 bool run(Module &M);
106 };
107
108 RegisterOpt<FunctionProfiler> X("insert-function-profiling",
109 "Insert instrumentation for function profiling");
110}
111
112bool FunctionProfiler::run(Module &M) {
113 Function *Main = M.getMainFunction();
114 if (Main == 0) {
115 std::cerr << "WARNING: cannot insert function profiling into a module"
116 << " with no main function!\n";
117 return false; // No main, no instrumentation!
118 }
119
120 unsigned NumFunctions = 0;
121 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
122 if (!I->isExternal())
123 ++NumFunctions;
124
125 const Type *ATy = ArrayType::get(Type::UIntTy, NumFunctions);
126 GlobalVariable *Counters =
127 new GlobalVariable(ATy, false, GlobalValue::InternalLinkage,
128 Constant::getNullValue(ATy), "FuncProfCounters", &M);
129
130 ConstantPointerRef *CounterCPR = ConstantPointerRef::get(Counters);
131
132 // Instrument all of the functions...
133 unsigned i = 0;
134 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
135 if (!I->isExternal())
136 // Insert counter at the start of the function
137 IncrementCounterInBlock(I->begin(), i++, CounterCPR);
138
139 // Add the initialization call to main.
140 insertInitializationCall(Main, "llvm_start_func_profiling", Counters);
141 return true;
142}
143
144
145namespace {
146 class BlockProfiler : public Pass {
147 bool run(Module &M);
148 };
149
150 RegisterOpt<BlockProfiler> Y("insert-block-profiling",
151 "Insert instrumentation for block profiling");
152}
153
154bool BlockProfiler::run(Module &M) {
155 Function *Main = M.getMainFunction();
156 if (Main == 0) {
157 std::cerr << "WARNING: cannot insert block profiling into a module"
158 << " with no main function!\n";
159 return false; // No main, no instrumentation!
160 }
161
162 unsigned NumBlocks = 0;
163 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
164 NumBlocks += I->size();
165
166 const Type *ATy = ArrayType::get(Type::UIntTy, NumBlocks);
167 GlobalVariable *Counters =
168 new GlobalVariable(ATy, false, GlobalValue::InternalLinkage,
169 Constant::getNullValue(ATy), "BlockProfCounters", &M);
170
171 ConstantPointerRef *CounterCPR = ConstantPointerRef::get(Counters);
172
173 // Instrument all of the blocks...
174 unsigned i = 0;
175 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
176 for (Function::iterator BB = I->begin(), E = I->end(); BB != E; ++BB)
177 // Insert counter at the start of the block
178 IncrementCounterInBlock(BB, i++, CounterCPR);
179
180 // Add the initialization call to main.
181 insertInitializationCall(Main, "llvm_start_block_profiling", Counters);
182 return true;
183}