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 class FunctionProfiler : public Pass {
32 void insertInitializationCall(Function *MainFn, const char *FnName,
36 RegisterOpt<FunctionProfiler> X("insert-function-profiling",
37 "Insert instrumentation for function profiling");
41 bool FunctionProfiler::run(Module &M) {
42 Function *Main = M.getMainFunction();
44 std::cerr << "WARNING: cannot insert function profiling into a module"
45 << " with no main function!\n";
46 return false; // No main, no instrumentation!
49 unsigned NumFunctions = 0;
50 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
54 const Type *ATy = ArrayType::get(Type::UIntTy, NumFunctions);
55 GlobalVariable *Counters =
56 new GlobalVariable(ATy, false, GlobalValue::InternalLinkage,
57 Constant::getNullValue(ATy), "FuncProfCounters", &M);
59 ConstantPointerRef *CounterCPR = ConstantPointerRef::get(Counters);
60 std::vector<Constant*> GEPIndices;
62 GEPIndices[0] = Constant::getNullValue(Type::LongTy);
64 // Instrument all of the functions...
66 for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
67 if (!I->isExternal()) {
68 // Insert counter at the start of the function, but after any allocas.
69 BasicBlock *Entry = I->begin();
70 BasicBlock::iterator InsertPos = Entry->begin();
71 while (isa<AllocaInst>(InsertPos)) ++InsertPos;
73 GEPIndices[1] = ConstantSInt::get(Type::LongTy, i++);
74 Constant *ElementPtr =
75 ConstantExpr::getGetElementPtr(CounterCPR, GEPIndices);
77 Value *OldVal = new LoadInst(ElementPtr, "OldFuncCounter", InsertPos);
78 Value *NewVal = BinaryOperator::create(Instruction::Add, OldVal,
79 ConstantInt::get(Type::UIntTy, 1),
80 "NewFuncCounter", InsertPos);
81 new StoreInst(NewVal, ElementPtr, InsertPos);
84 // Add the initialization call to main.
85 insertInitializationCall(Main, "llvm_start_func_profiling", Counters);
89 void FunctionProfiler::insertInitializationCall(Function *MainFn,
92 const Type *ArgVTy = PointerType::get(PointerType::get(Type::SByteTy));
93 const Type *UIntPtr = PointerType::get(Type::UIntTy);
94 Module &M = *MainFn->getParent();
95 Function *InitFn = M.getOrInsertFunction(FnName, Type::VoidTy, Type::IntTy,
96 ArgVTy, UIntPtr, Type::UIntTy, 0);
98 // This could force argc and argv into programs that wouldn't otherwise have
99 // them, but instead we just pass null values in.
100 std::vector<Value*> Args(4);
101 Args[0] = Constant::getNullValue(Type::IntTy);
102 Args[1] = Constant::getNullValue(ArgVTy);
104 /* FIXME: We should pass in the command line arguments here! */
105 switch (MainFn->asize()) {
113 ConstantPointerRef *ArrayCPR = ConstantPointerRef::get(Array);
114 std::vector<Constant*> GEPIndices(2, Constant::getNullValue(Type::LongTy));
115 Args[2] = ConstantExpr::getGetElementPtr(ArrayCPR, GEPIndices);
117 unsigned NumElements =
118 cast<ArrayType>(Array->getType()->getElementType())->getNumElements();
119 Args[3] = ConstantUInt::get(Type::UIntTy, NumElements);
121 // Skip over any allocas in the entry block.
122 BasicBlock *Entry = MainFn->begin();
123 BasicBlock::iterator InsertPos = Entry->begin();
124 while (isa<AllocaInst>(InsertPos)) ++InsertPos;
126 new CallInst(InitFn, Args, "", InsertPos);