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/IR/Constants.h" 00019 #include "llvm/IR/DerivedTypes.h" 00020 #include "llvm/IR/Instructions.h" 00021 #include "llvm/IR/LLVMContext.h" 00022 #include "llvm/IR/Module.h" 00023 00024 void llvm::InsertProfilingInitCall(Function *MainFn, const char *FnName, 00025 GlobalValue *Array, 00026 PointerType *arrayType) { 00027 LLVMContext &Context = MainFn->getContext(); 00028 Type *ArgVTy = 00029 PointerType::getUnqual(Type::getInt8PtrTy(Context)); 00030 PointerType *UIntPtr = arrayType ? arrayType : 00031 Type::getInt32PtrTy(Context); 00032 Module &M = *MainFn->getParent(); 00033 Constant *InitFn = M.getOrInsertFunction(FnName, Type::getInt32Ty(Context), 00034 Type::getInt32Ty(Context), 00035 ArgVTy, UIntPtr, 00036 Type::getInt32Ty(Context), 00037 (Type *)0); 00038 00039 // This could force argc and argv into programs that wouldn't otherwise have 00040 // them, but instead we just pass null values in. 00041 std::vector<Value*> Args(4); 00042 Args[0] = Constant::getNullValue(Type::getInt32Ty(Context)); 00043 Args[1] = Constant::getNullValue(ArgVTy); 00044 00045 // Skip over any allocas in the entry block. 00046 BasicBlock *Entry = MainFn->begin(); 00047 BasicBlock::iterator InsertPos = Entry->begin(); 00048 while (isa<AllocaInst>(InsertPos)) ++InsertPos; 00049 00050 std::vector<Constant*> GEPIndices(2, 00051 Constant::getNullValue(Type::getInt32Ty(Context))); 00052 unsigned NumElements = 0; 00053 if (Array) { 00054 Args[2] = ConstantExpr::getGetElementPtr(Array, GEPIndices); 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 CallInst *InitCall = CallInst::Create(InitFn, Args, "newargc", InsertPos); 00065 00066 // If argc or argv are not available in main, just pass null values in. 00067 Function::arg_iterator AI; 00068 switch (MainFn->arg_size()) { 00069 default: 00070 case 2: 00071 AI = MainFn->arg_begin(); ++AI; 00072 if (AI->getType() != ArgVTy) { 00073 Instruction::CastOps opcode = CastInst::getCastOpcode(AI, false, ArgVTy, 00074 false); 00075 InitCall->setArgOperand(1, 00076 CastInst::Create(opcode, AI, ArgVTy, "argv.cast", InitCall)); 00077 } else { 00078 InitCall->setArgOperand(1, AI); 00079 } 00080 /* FALL THROUGH */ 00081 00082 case 1: 00083 AI = MainFn->arg_begin(); 00084 // If the program looked at argc, have it look at the return value of the 00085 // init call instead. 00086 if (!AI->getType()->isIntegerTy(32)) { 00087 Instruction::CastOps opcode; 00088 if (!AI->use_empty()) { 00089 opcode = CastInst::getCastOpcode(InitCall, true, AI->getType(), true); 00090 AI->replaceAllUsesWith( 00091 CastInst::Create(opcode, InitCall, AI->getType(), "", InsertPos)); 00092 } 00093 opcode = CastInst::getCastOpcode(AI, true, 00094 Type::getInt32Ty(Context), true); 00095 InitCall->setArgOperand(0, 00096 CastInst::Create(opcode, AI, Type::getInt32Ty(Context), 00097 "argc.cast", InitCall)); 00098 } else { 00099 AI->replaceAllUsesWith(InitCall); 00100 InitCall->setArgOperand(0, AI); 00101 } 00102 00103 case 0: break; 00104 } 00105 } 00106 00107 void llvm::IncrementCounterInBlock(BasicBlock *BB, unsigned CounterNum, 00108 GlobalValue *CounterArray, bool beginning) { 00109 // Insert the increment after any alloca or PHI instructions... 00110 BasicBlock::iterator InsertPos = beginning ? BB->getFirstInsertionPt() : 00111 BB->getTerminator(); 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); 00123 00124 // Load, increment and store the value back. 00125 Value *OldVal = new LoadInst(ElementPtr, "OldFuncCounter", InsertPos); 00126 Value *NewVal = BinaryOperator::Create(Instruction::Add, OldVal, 00127 ConstantInt::get(Type::getInt32Ty(Context), 1), 00128 "NewFuncCounter", InsertPos); 00129 new StoreInst(NewVal, ElementPtr, InsertPos); 00130 } 00131 00132 void llvm::InsertProfilingShutdownCall(Function *Callee, Module *Mod) { 00133 // llvm.global_dtors is an array of type { i32, void ()* }. Prepare those 00134 // types. 00135 Type *GlobalDtorElems[2] = { 00136 Type::getInt32Ty(Mod->getContext()), 00137 FunctionType::get(Type::getVoidTy(Mod->getContext()), false)->getPointerTo() 00138 }; 00139 StructType *GlobalDtorElemTy = 00140 StructType::get(Mod->getContext(), GlobalDtorElems, false); 00141 00142 // Construct the new element we'll be adding. 00143 Constant *Elem[2] = { 00144 ConstantInt::get(Type::getInt32Ty(Mod->getContext()), 65535), 00145 ConstantExpr::getBitCast(Callee, GlobalDtorElems[1]) 00146 }; 00147 00148 // If llvm.global_dtors exists, make a copy of the things in its list and 00149 // delete it, to replace it with one that has a larger array type. 00150 std::vector<Constant *> dtors; 00151 if (GlobalVariable *GlobalDtors = Mod->getNamedGlobal("llvm.global_dtors")) { 00152 if (ConstantArray *InitList = 00153 dyn_cast<ConstantArray>(GlobalDtors->getInitializer())) { 00154 for (unsigned i = 0, e = InitList->getType()->getNumElements(); 00155 i != e; ++i) 00156 dtors.push_back(cast<Constant>(InitList->getOperand(i))); 00157 } 00158 GlobalDtors->eraseFromParent(); 00159 } 00160 00161 // Build up llvm.global_dtors with our new item in it. 00162 GlobalVariable *GlobalDtors = new GlobalVariable( 00163 *Mod, ArrayType::get(GlobalDtorElemTy, 1), false, 00164 GlobalValue::AppendingLinkage, NULL, "llvm.global_dtors"); 00165 00166 dtors.push_back(ConstantStruct::get(GlobalDtorElemTy, Elem)); 00167 GlobalDtors->setInitializer(ConstantArray::get( 00168 cast<ArrayType>(GlobalDtors->getType()->getElementType()), dtors)); 00169 }