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" |
| 21 | #include "llvm/Module.h" |
| 22 | |
| 23 | void llvm::InsertProfilingInitCall(Function *MainFn, const char *FnName, |
| 24 | GlobalValue *Array) { |
Christopher Lamb | 43ad6b3 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 25 | const Type *ArgVTy = |
| 26 | PointerType::getUnqual(PointerType::getUnqual(Type::Int8Ty)); |
| 27 | const PointerType *UIntPtr = PointerType::getUnqual(Type::Int32Ty); |
Chris Lattner | 467dd2e | 2004-03-08 17:06:13 +0000 | [diff] [blame] | 28 | Module &M = *MainFn->getParent(); |
Chris Lattner | febe5f1 | 2007-01-07 07:22:20 +0000 | [diff] [blame] | 29 | Constant *InitFn = M.getOrInsertFunction(FnName, Type::Int32Ty, Type::Int32Ty, |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 30 | ArgVTy, UIntPtr, Type::Int32Ty, |
Jeff Cohen | 66c5fd6 | 2005-10-23 04:37:20 +0000 | [diff] [blame] | 31 | (Type *)0); |
Chris Lattner | 467dd2e | 2004-03-08 17:06:13 +0000 | [diff] [blame] | 32 | |
| 33 | // This could force argc and argv into programs that wouldn't otherwise have |
| 34 | // them, but instead we just pass null values in. |
| 35 | std::vector<Value*> Args(4); |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 36 | Args[0] = Constant::getNullValue(Type::Int32Ty); |
Chris Lattner | 467dd2e | 2004-03-08 17:06:13 +0000 | [diff] [blame] | 37 | Args[1] = Constant::getNullValue(ArgVTy); |
| 38 | |
| 39 | // Skip over any allocas in the entry block. |
| 40 | BasicBlock *Entry = MainFn->begin(); |
| 41 | BasicBlock::iterator InsertPos = Entry->begin(); |
| 42 | while (isa<AllocaInst>(InsertPos)) ++InsertPos; |
| 43 | |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 44 | std::vector<Constant*> GEPIndices(2, Constant::getNullValue(Type::Int32Ty)); |
Brian Gaeke | ebbc0e9 | 2004-05-03 22:06:33 +0000 | [diff] [blame] | 45 | unsigned NumElements = 0; |
| 46 | if (Array) { |
Chris Lattner | ec1f752 | 2007-02-19 07:34:47 +0000 | [diff] [blame] | 47 | Args[2] = ConstantExpr::getGetElementPtr(Array, &GEPIndices[0], |
| 48 | GEPIndices.size()); |
Brian Gaeke | ebbc0e9 | 2004-05-03 22:06:33 +0000 | [diff] [blame] | 49 | NumElements = |
| 50 | cast<ArrayType>(Array->getType()->getElementType())->getNumElements(); |
| 51 | } else { |
| 52 | // If this profiling instrumentation doesn't have a constant array, just |
| 53 | // pass null. |
| 54 | Args[2] = ConstantPointerNull::get(UIntPtr); |
| 55 | } |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 56 | Args[3] = ConstantInt::get(Type::Int32Ty, NumElements); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 57 | |
Gabor Greif | 051a950 | 2008-04-06 20:25:17 +0000 | [diff] [blame^] | 58 | Instruction *InitCall = CallInst::Create(InitFn, Args.begin(), Args.end(), |
| 59 | "newargc", InsertPos); |
Chris Lattner | 467dd2e | 2004-03-08 17:06:13 +0000 | [diff] [blame] | 60 | |
| 61 | // 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] | 62 | Function::arg_iterator AI; |
| 63 | switch (MainFn->arg_size()) { |
Chris Lattner | 467dd2e | 2004-03-08 17:06:13 +0000 | [diff] [blame] | 64 | default: |
| 65 | case 2: |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 66 | AI = MainFn->arg_begin(); ++AI; |
Chris Lattner | 467dd2e | 2004-03-08 17:06:13 +0000 | [diff] [blame] | 67 | if (AI->getType() != ArgVTy) { |
Reid Spencer | 06e3f4e | 2006-12-21 07:49:49 +0000 | [diff] [blame] | 68 | Instruction::CastOps opcode = CastInst::getCastOpcode(AI, false, ArgVTy, |
| 69 | false); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 70 | InitCall->setOperand(2, |
Reid Spencer | 8a903db | 2006-12-18 08:47:13 +0000 | [diff] [blame] | 71 | CastInst::create(opcode, AI, ArgVTy, "argv.cast", InitCall)); |
Chris Lattner | 467dd2e | 2004-03-08 17:06:13 +0000 | [diff] [blame] | 72 | } else { |
| 73 | InitCall->setOperand(2, AI); |
| 74 | } |
Reid Spencer | 7b06bd5 | 2006-12-13 00:50:17 +0000 | [diff] [blame] | 75 | /* FALL THROUGH */ |
Chris Lattner | 467dd2e | 2004-03-08 17:06:13 +0000 | [diff] [blame] | 76 | |
| 77 | case 1: |
Chris Lattner | e4d5c44 | 2005-03-15 04:54:21 +0000 | [diff] [blame] | 78 | AI = MainFn->arg_begin(); |
Chris Lattner | 467dd2e | 2004-03-08 17:06:13 +0000 | [diff] [blame] | 79 | // If the program looked at argc, have it look at the return value of the |
| 80 | // init call instead. |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 81 | if (AI->getType() != Type::Int32Ty) { |
Reid Spencer | 8a903db | 2006-12-18 08:47:13 +0000 | [diff] [blame] | 82 | Instruction::CastOps opcode; |
| 83 | if (!AI->use_empty()) { |
Reid Spencer | 06e3f4e | 2006-12-21 07:49:49 +0000 | [diff] [blame] | 84 | opcode = CastInst::getCastOpcode(InitCall, true, AI->getType(), true); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 85 | AI->replaceAllUsesWith( |
Reid Spencer | 8a903db | 2006-12-18 08:47:13 +0000 | [diff] [blame] | 86 | CastInst::create(opcode, InitCall, AI->getType(), "", InsertPos)); |
| 87 | } |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 88 | opcode = CastInst::getCastOpcode(AI, true, Type::Int32Ty, true); |
Reid Spencer | 3da59db | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 89 | InitCall->setOperand(1, |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 90 | CastInst::create(opcode, AI, Type::Int32Ty, "argc.cast", InitCall)); |
Chris Lattner | 467dd2e | 2004-03-08 17:06:13 +0000 | [diff] [blame] | 91 | } else { |
| 92 | AI->replaceAllUsesWith(InitCall); |
| 93 | InitCall->setOperand(1, AI); |
| 94 | } |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 95 | |
Chris Lattner | 467dd2e | 2004-03-08 17:06:13 +0000 | [diff] [blame] | 96 | case 0: break; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | void llvm::IncrementCounterInBlock(BasicBlock *BB, unsigned CounterNum, |
Reid Spencer | 518310c | 2004-07-18 00:44:37 +0000 | [diff] [blame] | 101 | GlobalValue *CounterArray) { |
Chris Lattner | 467dd2e | 2004-03-08 17:06:13 +0000 | [diff] [blame] | 102 | // Insert the increment after any alloca or PHI instructions... |
| 103 | BasicBlock::iterator InsertPos = BB->begin(); |
| 104 | while (isa<AllocaInst>(InsertPos) || isa<PHINode>(InsertPos)) |
| 105 | ++InsertPos; |
| 106 | |
| 107 | // Create the getelementptr constant expression |
| 108 | std::vector<Constant*> Indices(2); |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 109 | Indices[0] = Constant::getNullValue(Type::Int32Ty); |
| 110 | Indices[1] = ConstantInt::get(Type::Int32Ty, CounterNum); |
Chris Lattner | ec1f752 | 2007-02-19 07:34:47 +0000 | [diff] [blame] | 111 | Constant *ElementPtr = |
| 112 | ConstantExpr::getGetElementPtr(CounterArray, &Indices[0], Indices.size()); |
Chris Lattner | 467dd2e | 2004-03-08 17:06:13 +0000 | [diff] [blame] | 113 | |
| 114 | // Load, increment and store the value back. |
| 115 | Value *OldVal = new LoadInst(ElementPtr, "OldFuncCounter", InsertPos); |
| 116 | Value *NewVal = BinaryOperator::create(Instruction::Add, OldVal, |
Reid Spencer | c5b206b | 2006-12-31 05:48:39 +0000 | [diff] [blame] | 117 | ConstantInt::get(Type::Int32Ty, 1), |
Chris Lattner | 467dd2e | 2004-03-08 17:06:13 +0000 | [diff] [blame] | 118 | "NewFuncCounter", InsertPos); |
| 119 | new StoreInst(NewVal, ElementPtr, InsertPos); |
| 120 | } |