Chris Lattner | 467dd2e | 2004-03-08 17:06:13 +0000 | [diff] [blame] | 1 | //===- ProfilingUtils.cpp - Helper functions shared by profilers ----------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | 467dd2e | 2004-03-08 17:06:13 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 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. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | 467dd2e | 2004-03-08 17:06:13 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Gabor Greif | 29d8aa7 | 2008-03-06 10:36:00 +0000 | [diff] [blame] | 10 | // This file implements a few helper functions which are used by profile |
Chris Lattner | 467dd2e | 2004-03-08 17:06:13 +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 | 5089551 | 2009-07-06 18:42:36 +0000 | [diff] [blame] | 21 | #include "llvm/LLVMContext.h" |
Chris Lattner | 467dd2e | 2004-03-08 17:06:13 +0000 | [diff] [blame] | 22 | #include "llvm/Module.h" |
| 23 | |
| 24 | void llvm::InsertProfilingInitCall(Function *MainFn, const char *FnName, |
Andrew Trick | 04317cc | 2011-01-29 01:09:53 +0000 | [diff] [blame] | 25 | GlobalValue *Array, |
| 26 | PointerType *arrayType) { |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 27 | LLVMContext &Context = MainFn->getContext(); |
Andrew Trick | 04317cc | 2011-01-29 01:09:53 +0000 | [diff] [blame] | 28 | const Type *ArgVTy = |
Duncan Sands | ac53a0b | 2009-10-06 15:40:36 +0000 | [diff] [blame] | 29 | PointerType::getUnqual(Type::getInt8PtrTy(Context)); |
Andrew Trick | 04317cc | 2011-01-29 01:09:53 +0000 | [diff] [blame] | 30 | const PointerType *UIntPtr = arrayType ? arrayType : |
| 31 | Type::getInt32PtrTy(Context); |
Chris Lattner | 467dd2e | 2004-03-08 17:06:13 +0000 | [diff] [blame] | 32 | Module &M = *MainFn->getParent(); |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 33 | Constant *InitFn = M.getOrInsertFunction(FnName, Type::getInt32Ty(Context), |
| 34 | Type::getInt32Ty(Context), |
| 35 | ArgVTy, UIntPtr, |
| 36 | Type::getInt32Ty(Context), |
Jeff Cohen | 66c5fd6 | 2005-10-23 04:37:20 +0000 | [diff] [blame] | 37 | (Type *)0); |
Chris Lattner | 467dd2e | 2004-03-08 17:06:13 +0000 | [diff] [blame] | 38 | |
| 39 | // This could force argc and argv into programs that wouldn't otherwise have |
| 40 | // them, but instead we just pass null values in. |
| 41 | std::vector<Value*> Args(4); |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 42 | Args[0] = Constant::getNullValue(Type::getInt32Ty(Context)); |
Owen Anderson | a7235ea | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 43 | Args[1] = Constant::getNullValue(ArgVTy); |
Chris Lattner | 467dd2e | 2004-03-08 17:06:13 +0000 | [diff] [blame] | 44 | |
| 45 | // Skip over any allocas in the entry block. |
| 46 | BasicBlock *Entry = MainFn->begin(); |
| 47 | BasicBlock::iterator InsertPos = Entry->begin(); |
| 48 | while (isa<AllocaInst>(InsertPos)) ++InsertPos; |
| 49 | |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 50 | std::vector<Constant*> GEPIndices(2, |
| 51 | Constant::getNullValue(Type::getInt32Ty(Context))); |
Brian Gaeke | ebbc0e9 | 2004-05-03 22:06:33 +0000 | [diff] [blame] | 52 | unsigned NumElements = 0; |
| 53 | if (Array) { |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 54 | Args[2] = ConstantExpr::getGetElementPtr(Array, &GEPIndices[0], |
Chris Lattner | ec1f752 | 2007-02-19 07:34:47 +0000 | [diff] [blame] | 55 | GEPIndices.size()); |
Brian Gaeke | ebbc0e9 | 2004-05-03 22:06:33 +0000 | [diff] [blame] | 56 | NumElements = |
| 57 | cast<ArrayType>(Array->getType()->getElementType())->getNumElements(); |
| 58 | } else { |
| 59 | // If this profiling instrumentation doesn't have a constant array, just |
| 60 | // pass null. |
Owen Anderson | 9e9a0d5 | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 61 | Args[2] = ConstantPointerNull::get(UIntPtr); |
Brian Gaeke | ebbc0e9 | 2004-05-03 22:06:33 +0000 | [diff] [blame] | 62 | } |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 63 | Args[3] = ConstantInt::get(Type::getInt32Ty(Context), NumElements); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 64 | |
Gabor Greif | 18c383f | 2010-06-28 12:31:35 +0000 | [diff] [blame] | 65 | CallInst *InitCall = CallInst::Create(InitFn, Args.begin(), Args.end(), |
| 66 | "newargc", InsertPos); |
Chris Lattner | 467dd2e | 2004-03-08 17:06:13 +0000 | [diff] [blame] | 67 | |
| 68 | // If argc or argv are not available in main, just pass null values in. |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 69 | Function::arg_iterator AI; |
| 70 | switch (MainFn->arg_size()) { |
Chris Lattner | 467dd2e | 2004-03-08 17:06:13 +0000 | [diff] [blame] | 71 | default: |
| 72 | case 2: |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 73 | AI = MainFn->arg_begin(); ++AI; |
Chris Lattner | 467dd2e | 2004-03-08 17:06:13 +0000 | [diff] [blame] | 74 | if (AI->getType() != ArgVTy) { |
Andrew Trick | 04317cc | 2011-01-29 01:09:53 +0000 | [diff] [blame] | 75 | Instruction::CastOps opcode = CastInst::getCastOpcode(AI, false, ArgVTy, |
Reid Spencer | 06e3f4e | 2006-12-21 07:49:49 +0000 | [diff] [blame] | 76 | false); |
Andrew Trick | 04317cc | 2011-01-29 01:09:53 +0000 | [diff] [blame] | 77 | InitCall->setArgOperand(1, |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 78 | CastInst::Create(opcode, AI, ArgVTy, "argv.cast", InitCall)); |
Chris Lattner | 467dd2e | 2004-03-08 17:06:13 +0000 | [diff] [blame] | 79 | } else { |
Gabor Greif | 18c383f | 2010-06-28 12:31:35 +0000 | [diff] [blame] | 80 | InitCall->setArgOperand(1, AI); |
Chris Lattner | 467dd2e | 2004-03-08 17:06:13 +0000 | [diff] [blame] | 81 | } |
Reid Spencer | 7b06bd5 | 2006-12-13 00:50:17 +0000 | [diff] [blame] | 82 | /* FALL THROUGH */ |
Chris Lattner | 467dd2e | 2004-03-08 17:06:13 +0000 | [diff] [blame] | 83 | |
| 84 | case 1: |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 85 | AI = MainFn->arg_begin(); |
Chris Lattner | 467dd2e | 2004-03-08 17:06:13 +0000 | [diff] [blame] | 86 | // If the program looked at argc, have it look at the return value of the |
| 87 | // init call instead. |
Duncan Sands | b0bc6c3 | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 88 | if (!AI->getType()->isIntegerTy(32)) { |
Reid Spencer | 8a903db | 2006-12-18 08:47:13 +0000 | [diff] [blame] | 89 | Instruction::CastOps opcode; |
| 90 | if (!AI->use_empty()) { |
Reid Spencer | 06e3f4e | 2006-12-21 07:49:49 +0000 | [diff] [blame] | 91 | opcode = CastInst::getCastOpcode(InitCall, true, AI->getType(), true); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 92 | AI->replaceAllUsesWith( |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 93 | CastInst::Create(opcode, InitCall, AI->getType(), "", InsertPos)); |
Reid Spencer | 8a903db | 2006-12-18 08:47:13 +0000 | [diff] [blame] | 94 | } |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 95 | opcode = CastInst::getCastOpcode(AI, true, |
| 96 | Type::getInt32Ty(Context), true); |
Andrew Trick | 04317cc | 2011-01-29 01:09:53 +0000 | [diff] [blame] | 97 | InitCall->setArgOperand(0, |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 98 | CastInst::Create(opcode, AI, Type::getInt32Ty(Context), |
| 99 | "argc.cast", InitCall)); |
Chris Lattner | 467dd2e | 2004-03-08 17:06:13 +0000 | [diff] [blame] | 100 | } else { |
| 101 | AI->replaceAllUsesWith(InitCall); |
Gabor Greif | 18c383f | 2010-06-28 12:31:35 +0000 | [diff] [blame] | 102 | InitCall->setArgOperand(0, AI); |
Chris Lattner | 467dd2e | 2004-03-08 17:06:13 +0000 | [diff] [blame] | 103 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 104 | |
Chris Lattner | 467dd2e | 2004-03-08 17:06:13 +0000 | [diff] [blame] | 105 | case 0: break; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | void llvm::IncrementCounterInBlock(BasicBlock *BB, unsigned CounterNum, |
Andrew Trick | 04317cc | 2011-01-29 01:09:53 +0000 | [diff] [blame] | 110 | GlobalValue *CounterArray, bool beginning) { |
Chris Lattner | 467dd2e | 2004-03-08 17:06:13 +0000 | [diff] [blame] | 111 | // Insert the increment after any alloca or PHI instructions... |
Andrew Trick | 04317cc | 2011-01-29 01:09:53 +0000 | [diff] [blame] | 112 | BasicBlock::iterator InsertPos = beginning ? BB->getFirstNonPHI() : |
| 113 | BB->getTerminator(); |
Dan Gohman | 02dea8b | 2008-05-23 21:05:58 +0000 | [diff] [blame] | 114 | while (isa<AllocaInst>(InsertPos)) |
Chris Lattner | 467dd2e | 2004-03-08 17:06:13 +0000 | [diff] [blame] | 115 | ++InsertPos; |
| 116 | |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 117 | LLVMContext &Context = BB->getContext(); |
| 118 | |
Chris Lattner | 467dd2e | 2004-03-08 17:06:13 +0000 | [diff] [blame] | 119 | // Create the getelementptr constant expression |
| 120 | std::vector<Constant*> Indices(2); |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 121 | Indices[0] = Constant::getNullValue(Type::getInt32Ty(Context)); |
| 122 | Indices[1] = ConstantInt::get(Type::getInt32Ty(Context), CounterNum); |
Andrew Trick | 04317cc | 2011-01-29 01:09:53 +0000 | [diff] [blame] | 123 | Constant *ElementPtr = |
Owen Anderson | baf3c40 | 2009-07-29 18:55:55 +0000 | [diff] [blame] | 124 | ConstantExpr::getGetElementPtr(CounterArray, &Indices[0], |
Owen Anderson | 5089551 | 2009-07-06 18:42:36 +0000 | [diff] [blame] | 125 | Indices.size()); |
Chris Lattner | 467dd2e | 2004-03-08 17:06:13 +0000 | [diff] [blame] | 126 | |
| 127 | // Load, increment and store the value back. |
| 128 | Value *OldVal = new LoadInst(ElementPtr, "OldFuncCounter", InsertPos); |
Gabor Greif | 7cbd8a3 | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 129 | Value *NewVal = BinaryOperator::Create(Instruction::Add, OldVal, |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 130 | ConstantInt::get(Type::getInt32Ty(Context), 1), |
Chris Lattner | 467dd2e | 2004-03-08 17:06:13 +0000 | [diff] [blame] | 131 | "NewFuncCounter", InsertPos); |
| 132 | new StoreInst(NewVal, ElementPtr, InsertPos); |
| 133 | } |