LLVM API Documentation
00001 //===- ProfilingUtils.cpp - Helper functions shared by profilers ----------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements a few helper functions which are used by profile 00011 // instrumentation code to instrument the code. This allows the profiler pass 00012 // to worry about *what* to insert, and these functions take care of *how* to do 00013 // it. 00014 // 00015 //===----------------------------------------------------------------------===// 00016 00017 #include "ProfilingUtils.h" 00018 #include "llvm/Constants.h" 00019 #include "llvm/DerivedTypes.h" 00020 #include "llvm/Instructions.h" 00021 #include "llvm/LLVMContext.h" 00022 #include "llvm/Module.h" 00023 00024 void llvm::InsertProfilingInitCall(Function *MainFn, const char *FnName, 00025 GlobalValue *Array) { 00026 LLVMContext &Context = MainFn->getContext(); 00027 const Type *ArgVTy = 00028 PointerType::getUnqual(PointerType::getUnqual(Type::getInt8Ty(Context))); 00029 const PointerType *UIntPtr = 00030 PointerType::getUnqual(Type::getInt32Ty(Context)); 00031 Module &M = *MainFn->getParent(); 00032 Constant *InitFn = M.getOrInsertFunction(FnName, Type::getInt32Ty(Context), 00033 Type::getInt32Ty(Context), 00034 ArgVTy, UIntPtr, 00035 Type::getInt32Ty(Context), 00036 (Type *)0); 00037 00038 // This could force argc and argv into programs that wouldn't otherwise have 00039 // them, but instead we just pass null values in. 00040 std::vector<Value*> Args(4); 00041 Args[0] = Constant::getNullValue(Type::getInt32Ty(Context)); 00042 Args[1] = Constant::getNullValue(ArgVTy); 00043 00044 // Skip over any allocas in the entry block. 00045 BasicBlock *Entry = MainFn->begin(); 00046 BasicBlock::iterator InsertPos = Entry->begin(); 00047 while (isa<AllocaInst>(InsertPos)) ++InsertPos; 00048 00049 std::vector<Constant*> GEPIndices(2, 00050 Constant::getNullValue(Type::getInt32Ty(Context))); 00051 unsigned NumElements = 0; 00052 if (Array) { 00053 Args[2] = ConstantExpr::getGetElementPtr(Array, &GEPIndices[0], 00054 GEPIndices.size()); 00055 NumElements = 00056 cast<ArrayType>(Array->getType()->getElementType())->getNumElements(); 00057 } else { 00058 // If this profiling instrumentation doesn't have a constant array, just 00059 // pass null. 00060 Args[2] = ConstantPointerNull::get(UIntPtr); 00061 } 00062 Args[3] = ConstantInt::get(Type::getInt32Ty(Context), NumElements); 00063 00064 Instruction *InitCall = CallInst::Create(InitFn, Args.begin(), Args.end(), 00065 "newargc", InsertPos); 00066 00067 // If argc or argv are not available in main, just pass null values in. 00068 Function::arg_iterator AI; 00069 switch (MainFn->arg_size()) { 00070 default: 00071 case 2: 00072 AI = MainFn->arg_begin(); ++AI; 00073 if (AI->getType() != ArgVTy) { 00074 Instruction::CastOps opcode = CastInst::getCastOpcode(AI, false, ArgVTy, 00075 false); 00076 InitCall->setOperand(2, 00077 CastInst::Create(opcode, AI, ArgVTy, "argv.cast", InitCall)); 00078 } else { 00079 InitCall->setOperand(2, AI); 00080 } 00081 /* FALL THROUGH */ 00082 00083 case 1: 00084 AI = MainFn->arg_begin(); 00085 // If the program looked at argc, have it look at the return value of the 00086 // init call instead. 00087 if (AI->getType() != Type::getInt32Ty(Context)) { 00088 Instruction::CastOps opcode; 00089 if (!AI->use_empty()) { 00090 opcode = CastInst::getCastOpcode(InitCall, true, AI->getType(), true); 00091 AI->replaceAllUsesWith( 00092 CastInst::Create(opcode, InitCall, AI->getType(), "", InsertPos)); 00093 } 00094 opcode = CastInst::getCastOpcode(AI, true, 00095 Type::getInt32Ty(Context), true); 00096 InitCall->setOperand(1, 00097 CastInst::Create(opcode, AI, Type::getInt32Ty(Context), 00098 "argc.cast", InitCall)); 00099 } else { 00100 AI->replaceAllUsesWith(InitCall); 00101 InitCall->setOperand(1, AI); 00102 } 00103 00104 case 0: break; 00105 } 00106 } 00107 00108 void llvm::IncrementCounterInBlock(BasicBlock *BB, unsigned CounterNum, 00109 GlobalValue *CounterArray) { 00110 // Insert the increment after any alloca or PHI instructions... 00111 BasicBlock::iterator InsertPos = BB->getFirstNonPHI(); 00112 while (isa<AllocaInst>(InsertPos)) 00113 ++InsertPos; 00114 00115 LLVMContext &Context = BB->getContext(); 00116 00117 // Create the getelementptr constant expression 00118 std::vector<Constant*> Indices(2); 00119 Indices[0] = Constant::getNullValue(Type::getInt32Ty(Context)); 00120 Indices[1] = ConstantInt::get(Type::getInt32Ty(Context), CounterNum); 00121 Constant *ElementPtr = 00122 ConstantExpr::getGetElementPtr(CounterArray, &Indices[0], 00123 Indices.size()); 00124 00125 // Load, increment and store the value back. 00126 Value *OldVal = new LoadInst(ElementPtr, "OldFuncCounter", InsertPos); 00127 Value *NewVal = BinaryOperator::Create(Instruction::Add, OldVal, 00128 ConstantInt::get(Type::getInt32Ty(Context), 1), 00129 "NewFuncCounter", InsertPos); 00130 new StoreInst(NewVal, ElementPtr, InsertPos); 00131 }