1 //===- BlockProfiling.cpp - Insert counters for block profiling -----------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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.
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
20 //===----------------------------------------------------------------------===//
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"
29 static void insertInitializationCall(Function *MainFn, const char *FnName,
31 const Type *ArgVTy = PointerType::get(PointerType::get(Type::SByteTy));
32 const Type *UIntPtr = PointerType::get(Type::UIntTy);
33 Module &M = *MainFn->getParent();
34 Function *InitFn = M.getOrInsertFunction(FnName, Type::VoidTy, Type::IntTy,
35 ArgVTy, UIntPtr, Type::UIntTy, 0);
37 // This could force argc and argv into programs that wouldn't otherwise have
38 // them, but instead we just pass null values in.
39 std::vector<Value*> Args(4);
40 Args[0] = Constant::getNullValue(Type::IntTy);
41 Args[1] = Constant::getNullValue(ArgVTy);
43 // Skip over any allocas in the entry block.
44 BasicBlock *Entry = MainFn->begin();
45 BasicBlock::iterator InsertPos = Entry->begin();
46 while (isa<AllocaInst>(InsertPos)) ++InsertPos;
48 Function::aiterator AI;
49 switch (MainFn->asize()) {
52 AI = MainFn->abegin(); ++AI;
53 if (AI->getType() != ArgVTy) {
54 Args[1] = new CastInst(AI, ArgVTy, "argv.cast", InsertPos);
60 AI = MainFn->abegin();
61 if (AI->getType() != Type::IntTy) {
62 Args[0] = new CastInst(AI, Type::IntTy, "argc.cast", InsertPos);
71 ConstantPointerRef *ArrayCPR = ConstantPointerRef::get(Array);
72 std::vector<Constant*> GEPIndices(2, Constant::getNullValue(Type::LongTy));
73 Args[2] = ConstantExpr::getGetElementPtr(ArrayCPR, GEPIndices);
75 unsigned NumElements =
76 cast<ArrayType>(Array->getType()->getElementType())->getNumElements();
77 Args[3] = ConstantUInt::get(Type::UIntTy, NumElements);
79 new CallInst(InitFn, Args, "", InsertPos);
82 static void IncrementCounterInBlock(BasicBlock *BB, unsigned CounterNum,
83 ConstantPointerRef *CounterArray) {
84 // Insert the increment after any alloca or PHI instructions...
85 BasicBlock::iterator InsertPos = BB->begin();
86 while (isa<AllocaInst>(InsertPos) || isa<PHINode>(InsertPos))
89 // Create the getelementptr constant expression
90 std::vector<Constant*> Indices(2);
91 Indices[0] = Constant::getNullValue(Type::LongTy);
92 Indices[1] = ConstantSInt::get(Type::LongTy, CounterNum);
93 Constant *ElementPtr = ConstantExpr::getGetElementPtr(CounterArray, Indices);
95 // Load, increment and store the value back.
96 Value *OldVal = new LoadInst(ElementPtr, "OldFuncCounter", InsertPos);
97 Value *NewVal = BinaryOperator::create(Instruction::Add, OldVal,
98 ConstantInt::get(Type::UIntTy, 1),
99 "NewFuncCounter", InsertPos);
100 new StoreInst(NewVal, ElementPtr, InsertPos);
105 class FunctionProfiler : public Pass {
109 RegisterOpt<FunctionProfiler> X("insert-function-profiling",
110 "Insert instrumentation for function profiling");
113 bool FunctionProfiler::run(Module &M) {
114 Function *Main = M.getMainFunction();
116 std::cerr << "WARNING: cannot insert function profiling into a module"
117 << " with no main function!\n";
118 return false; // No main, no instrumentation!
121 unsigned NumFunctions = 0;
122 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
123 if (!I->isExternal())
126 const Type *ATy = ArrayType::get(Type::UIntTy, NumFunctions);
127 GlobalVariable *Counters =
128 new GlobalVariable(ATy, false, GlobalValue::InternalLinkage,
129 Constant::getNullValue(ATy), "FuncProfCounters", &M);
131 ConstantPointerRef *CounterCPR = ConstantPointerRef::get(Counters);
133 // Instrument all of the functions...
135 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
136 if (!I->isExternal())
137 // Insert counter at the start of the function
138 IncrementCounterInBlock(I->begin(), i++, CounterCPR);
140 // Add the initialization call to main.
141 insertInitializationCall(Main, "llvm_start_func_profiling", Counters);
147 class BlockProfiler : public Pass {
151 RegisterOpt<BlockProfiler> Y("insert-block-profiling",
152 "Insert instrumentation for block profiling");
155 bool BlockProfiler::run(Module &M) {
156 Function *Main = M.getMainFunction();
158 std::cerr << "WARNING: cannot insert block profiling into a module"
159 << " with no main function!\n";
160 return false; // No main, no instrumentation!
163 unsigned NumBlocks = 0;
164 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
165 NumBlocks += I->size();
167 const Type *ATy = ArrayType::get(Type::UIntTy, NumBlocks);
168 GlobalVariable *Counters =
169 new GlobalVariable(ATy, false, GlobalValue::InternalLinkage,
170 Constant::getNullValue(ATy), "BlockProfCounters", &M);
172 ConstantPointerRef *CounterCPR = ConstantPointerRef::get(Counters);
174 // Instrument all of the blocks...
176 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
177 for (Function::iterator BB = I->begin(), E = I->end(); BB != E; ++BB)
178 // Insert counter at the start of the block
179 IncrementCounterInBlock(BB, i++, CounterCPR);
181 // Add the initialization call to main.
182 insertInitializationCall(Main, "llvm_start_block_profiling", Counters);