Chris Lattner | c8ba067 | 2003-10-28 18:59:04 +0000 | [diff] [blame^] | 1 | //===- 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 | |
| 28 | namespace { |
| 29 | class FunctionProfiler : public Pass { |
| 30 | bool run(Module &M); |
| 31 | |
| 32 | void insertInitializationCall(Function *MainFn, const char *FnName, |
| 33 | GlobalValue *Array); |
| 34 | }; |
| 35 | |
| 36 | RegisterOpt<FunctionProfiler> X("insert-function-profiling", |
| 37 | "Insert instrumentation for function profiling"); |
| 38 | } |
| 39 | |
| 40 | |
| 41 | bool FunctionProfiler::run(Module &M) { |
| 42 | Function *Main = M.getMainFunction(); |
| 43 | if (Main == 0) { |
| 44 | std::cerr << "WARNING: cannot insert function profiling into a module" |
| 45 | << " with no main function!\n"; |
| 46 | return false; // No main, no instrumentation! |
| 47 | } |
| 48 | |
| 49 | unsigned NumFunctions = 0; |
| 50 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
| 51 | if (!I->isExternal()) |
| 52 | ++NumFunctions; |
| 53 | |
| 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); |
| 58 | |
| 59 | ConstantPointerRef *CounterCPR = ConstantPointerRef::get(Counters); |
| 60 | std::vector<Constant*> GEPIndices; |
| 61 | GEPIndices.resize(2); |
| 62 | GEPIndices[0] = Constant::getNullValue(Type::LongTy); |
| 63 | |
| 64 | // Instrument all of the functions... |
| 65 | unsigned i = 0; |
| 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; |
| 72 | |
| 73 | GEPIndices[1] = ConstantSInt::get(Type::LongTy, i++); |
| 74 | Constant *ElementPtr = |
| 75 | ConstantExpr::getGetElementPtr(CounterCPR, GEPIndices); |
| 76 | |
| 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); |
| 82 | } |
| 83 | |
| 84 | // Add the initialization call to main. |
| 85 | insertInitializationCall(Main, "llvm_start_func_profiling", Counters); |
| 86 | return true; |
| 87 | } |
| 88 | |
| 89 | void FunctionProfiler::insertInitializationCall(Function *MainFn, |
| 90 | const char *FnName, |
| 91 | GlobalValue *Array) { |
| 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); |
| 97 | |
| 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); |
| 103 | |
| 104 | /* FIXME: We should pass in the command line arguments here! */ |
| 105 | switch (MainFn->asize()) { |
| 106 | default: |
| 107 | case 2: |
| 108 | case 1: |
| 109 | case 0: |
| 110 | break; |
| 111 | } |
| 112 | |
| 113 | ConstantPointerRef *ArrayCPR = ConstantPointerRef::get(Array); |
| 114 | std::vector<Constant*> GEPIndices(2, Constant::getNullValue(Type::LongTy)); |
| 115 | Args[2] = ConstantExpr::getGetElementPtr(ArrayCPR, GEPIndices); |
| 116 | |
| 117 | unsigned NumElements = |
| 118 | cast<ArrayType>(Array->getType()->getElementType())->getNumElements(); |
| 119 | Args[3] = ConstantUInt::get(Type::UIntTy, NumElements); |
| 120 | |
| 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; |
| 125 | |
| 126 | new CallInst(InitFn, Args, "", InsertPos); |
| 127 | } |