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