ProfilingUtils.cpp revision 52eec548206d0b135b55ba52dd0e82e978f15ae5
1//===- ProfilingUtils.cpp - Helper functions shared by profilers ----------===// 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 files implements a few helper functions which are used by profile 11// instrumentation code to instrument the code. This allows the profiler pass 12// to worry about *what* to insert, and these functions take care of *how* to do 13// it. 14// 15//===----------------------------------------------------------------------===// 16 17#include "ProfilingUtils.h" 18#include "llvm/Constants.h" 19#include "llvm/DerivedTypes.h" 20#include "llvm/Instructions.h" 21#include "llvm/Module.h" 22 23void llvm::InsertProfilingInitCall(Function *MainFn, const char *FnName, 24 GlobalValue *Array) { 25 const Type *ArgVTy = PointerType::get(PointerType::get(Type::Int8Ty)); 26 const PointerType *UIntPtr = PointerType::get(Type::Int32Ty); 27 Module &M = *MainFn->getParent(); 28 Constant *InitFn = M.getOrInsertFunction(FnName, Type::Int32Ty, Type::Int32Ty, 29 ArgVTy, UIntPtr, Type::Int32Ty, 30 (Type *)0); 31 32 // This could force argc and argv into programs that wouldn't otherwise have 33 // them, but instead we just pass null values in. 34 std::vector<Value*> Args(4); 35 Args[0] = Constant::getNullValue(Type::Int32Ty); 36 Args[1] = Constant::getNullValue(ArgVTy); 37 38 // Skip over any allocas in the entry block. 39 BasicBlock *Entry = MainFn->begin(); 40 BasicBlock::iterator InsertPos = Entry->begin(); 41 while (isa<AllocaInst>(InsertPos)) ++InsertPos; 42 43 std::vector<Constant*> GEPIndices(2, Constant::getNullValue(Type::Int32Ty)); 44 unsigned NumElements = 0; 45 if (Array) { 46 Args[2] = ConstantExpr::getGetElementPtr(Array, &GEPIndices[0], 47 GEPIndices.size()); 48 NumElements = 49 cast<ArrayType>(Array->getType()->getElementType())->getNumElements(); 50 } else { 51 // If this profiling instrumentation doesn't have a constant array, just 52 // pass null. 53 Args[2] = ConstantPointerNull::get(UIntPtr); 54 } 55 Args[3] = ConstantInt::get(Type::Int32Ty, NumElements); 56 57 Instruction *InitCall = new CallInst(InitFn, Args.begin(), Args.end(), 58 "newargc", InsertPos); 59 60 // If argc or argv are not available in main, just pass null values in. 61 Function::arg_iterator AI; 62 switch (MainFn->arg_size()) { 63 default: 64 case 2: 65 AI = MainFn->arg_begin(); ++AI; 66 if (AI->getType() != ArgVTy) { 67 Instruction::CastOps opcode = CastInst::getCastOpcode(AI, false, ArgVTy, 68 false); 69 InitCall->setOperand(2, 70 CastInst::create(opcode, AI, ArgVTy, "argv.cast", InitCall)); 71 } else { 72 InitCall->setOperand(2, AI); 73 } 74 /* FALL THROUGH */ 75 76 case 1: 77 AI = MainFn->arg_begin(); 78 // If the program looked at argc, have it look at the return value of the 79 // init call instead. 80 if (AI->getType() != Type::Int32Ty) { 81 Instruction::CastOps opcode; 82 if (!AI->use_empty()) { 83 opcode = CastInst::getCastOpcode(InitCall, true, AI->getType(), true); 84 AI->replaceAllUsesWith( 85 CastInst::create(opcode, InitCall, AI->getType(), "", InsertPos)); 86 } 87 opcode = CastInst::getCastOpcode(AI, true, Type::Int32Ty, true); 88 InitCall->setOperand(1, 89 CastInst::create(opcode, AI, Type::Int32Ty, "argc.cast", InitCall)); 90 } else { 91 AI->replaceAllUsesWith(InitCall); 92 InitCall->setOperand(1, AI); 93 } 94 95 case 0: break; 96 } 97} 98 99void llvm::IncrementCounterInBlock(BasicBlock *BB, unsigned CounterNum, 100 GlobalValue *CounterArray) { 101 // Insert the increment after any alloca or PHI instructions... 102 BasicBlock::iterator InsertPos = BB->begin(); 103 while (isa<AllocaInst>(InsertPos) || isa<PHINode>(InsertPos)) 104 ++InsertPos; 105 106 // Create the getelementptr constant expression 107 std::vector<Constant*> Indices(2); 108 Indices[0] = Constant::getNullValue(Type::Int32Ty); 109 Indices[1] = ConstantInt::get(Type::Int32Ty, CounterNum); 110 Constant *ElementPtr = 111 ConstantExpr::getGetElementPtr(CounterArray, &Indices[0], Indices.size()); 112 113 // Load, increment and store the value back. 114 Value *OldVal = new LoadInst(ElementPtr, "OldFuncCounter", InsertPos); 115 Value *NewVal = BinaryOperator::create(Instruction::Add, OldVal, 116 ConstantInt::get(Type::Int32Ty, 1), 117 "NewFuncCounter", InsertPos); 118 new StoreInst(NewVal, ElementPtr, InsertPos); 119} 120