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