Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===- ProfilingUtils.cpp - Helper functions shared by profilers ----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 081ce94 | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Gabor Greif | 4750dac | 2008-03-06 10:36:00 +0000 | [diff] [blame] | 10 | // This file implements a few helper functions which are used by profile |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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" |
Owen Anderson | d4d90a0 | 2009-07-06 18:42:36 +0000 | [diff] [blame] | 21 | #include "llvm/LLVMContext.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 22 | #include "llvm/Module.h" |
| 23 | |
| 24 | void llvm::InsertProfilingInitCall(Function *MainFn, const char *FnName, |
| 25 | GlobalValue *Array) { |
Owen Anderson | 175b654 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 26 | LLVMContext &Context = MainFn->getContext(); |
Christopher Lamb | bb2f222 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 27 | const Type *ArgVTy = |
Owen Anderson | 175b654 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 28 | Context.getPointerTypeUnqual(Context.getPointerTypeUnqual(Type::Int8Ty)); |
| 29 | const PointerType *UIntPtr = Context.getPointerTypeUnqual(Type::Int32Ty); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 30 | Module &M = *MainFn->getParent(); |
| 31 | Constant *InitFn = M.getOrInsertFunction(FnName, Type::Int32Ty, Type::Int32Ty, |
| 32 | ArgVTy, UIntPtr, Type::Int32Ty, |
| 33 | (Type *)0); |
| 34 | |
| 35 | // This could force argc and argv into programs that wouldn't otherwise have |
| 36 | // them, but instead we just pass null values in. |
| 37 | std::vector<Value*> Args(4); |
Owen Anderson | 175b654 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 38 | Args[0] = Context.getNullValue(Type::Int32Ty); |
| 39 | Args[1] = Context.getNullValue(ArgVTy); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 40 | |
| 41 | // Skip over any allocas in the entry block. |
| 42 | BasicBlock *Entry = MainFn->begin(); |
| 43 | BasicBlock::iterator InsertPos = Entry->begin(); |
| 44 | while (isa<AllocaInst>(InsertPos)) ++InsertPos; |
| 45 | |
Owen Anderson | 175b654 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 46 | std::vector<Constant*> GEPIndices(2, Context.getNullValue(Type::Int32Ty)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 47 | unsigned NumElements = 0; |
| 48 | if (Array) { |
Owen Anderson | 02b48c3 | 2009-07-29 18:55:55 +0000 | [diff] [blame^] | 49 | Args[2] = ConstantExpr::getGetElementPtr(Array, &GEPIndices[0], |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 50 | GEPIndices.size()); |
| 51 | NumElements = |
| 52 | cast<ArrayType>(Array->getType()->getElementType())->getNumElements(); |
| 53 | } else { |
| 54 | // If this profiling instrumentation doesn't have a constant array, just |
| 55 | // pass null. |
Owen Anderson | 175b654 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 56 | Args[2] = Context.getConstantPointerNull(UIntPtr); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 57 | } |
Owen Anderson | eacb44d | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 58 | Args[3] = ConstantInt::get(Type::Int32Ty, NumElements); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 59 | |
Gabor Greif | d6da1d0 | 2008-04-06 20:25:17 +0000 | [diff] [blame] | 60 | Instruction *InitCall = CallInst::Create(InitFn, Args.begin(), Args.end(), |
| 61 | "newargc", InsertPos); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 62 | |
| 63 | // If argc or argv are not available in main, just pass null values in. |
| 64 | Function::arg_iterator AI; |
| 65 | switch (MainFn->arg_size()) { |
| 66 | default: |
| 67 | case 2: |
| 68 | AI = MainFn->arg_begin(); ++AI; |
| 69 | if (AI->getType() != ArgVTy) { |
| 70 | Instruction::CastOps opcode = CastInst::getCastOpcode(AI, false, ArgVTy, |
| 71 | false); |
| 72 | InitCall->setOperand(2, |
Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 73 | CastInst::Create(opcode, AI, ArgVTy, "argv.cast", InitCall)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 74 | } else { |
| 75 | InitCall->setOperand(2, AI); |
| 76 | } |
| 77 | /* FALL THROUGH */ |
| 78 | |
| 79 | case 1: |
| 80 | AI = MainFn->arg_begin(); |
| 81 | // If the program looked at argc, have it look at the return value of the |
| 82 | // init call instead. |
| 83 | if (AI->getType() != Type::Int32Ty) { |
| 84 | Instruction::CastOps opcode; |
| 85 | if (!AI->use_empty()) { |
| 86 | opcode = CastInst::getCastOpcode(InitCall, true, AI->getType(), true); |
| 87 | AI->replaceAllUsesWith( |
Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 88 | CastInst::Create(opcode, InitCall, AI->getType(), "", InsertPos)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 89 | } |
| 90 | opcode = CastInst::getCastOpcode(AI, true, Type::Int32Ty, true); |
| 91 | InitCall->setOperand(1, |
Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 92 | CastInst::Create(opcode, AI, Type::Int32Ty, "argc.cast", InitCall)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 93 | } else { |
| 94 | AI->replaceAllUsesWith(InitCall); |
| 95 | InitCall->setOperand(1, AI); |
| 96 | } |
| 97 | |
| 98 | case 0: break; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | void llvm::IncrementCounterInBlock(BasicBlock *BB, unsigned CounterNum, |
| 103 | GlobalValue *CounterArray) { |
Owen Anderson | 175b654 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 104 | LLVMContext &Context = BB->getContext(); |
Owen Anderson | d4d90a0 | 2009-07-06 18:42:36 +0000 | [diff] [blame] | 105 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 106 | // Insert the increment after any alloca or PHI instructions... |
Dan Gohman | 514277c | 2008-05-23 21:05:58 +0000 | [diff] [blame] | 107 | BasicBlock::iterator InsertPos = BB->getFirstNonPHI(); |
| 108 | while (isa<AllocaInst>(InsertPos)) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 109 | ++InsertPos; |
| 110 | |
| 111 | // Create the getelementptr constant expression |
| 112 | std::vector<Constant*> Indices(2); |
Owen Anderson | 175b654 | 2009-07-22 00:24:57 +0000 | [diff] [blame] | 113 | Indices[0] = Context.getNullValue(Type::Int32Ty); |
Owen Anderson | eacb44d | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 114 | Indices[1] = ConstantInt::get(Type::Int32Ty, CounterNum); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 115 | Constant *ElementPtr = |
Owen Anderson | 02b48c3 | 2009-07-29 18:55:55 +0000 | [diff] [blame^] | 116 | ConstantExpr::getGetElementPtr(CounterArray, &Indices[0], |
Owen Anderson | d4d90a0 | 2009-07-06 18:42:36 +0000 | [diff] [blame] | 117 | Indices.size()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 118 | |
| 119 | // Load, increment and store the value back. |
| 120 | Value *OldVal = new LoadInst(ElementPtr, "OldFuncCounter", InsertPos); |
Gabor Greif | a645dd3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 121 | Value *NewVal = BinaryOperator::Create(Instruction::Add, OldVal, |
Owen Anderson | eacb44d | 2009-07-24 23:12:02 +0000 | [diff] [blame] | 122 | ConstantInt::get(Type::Int32Ty, 1), |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 123 | "NewFuncCounter", InsertPos); |
| 124 | new StoreInst(NewVal, ElementPtr, InsertPos); |
| 125 | } |