blob: b57bbf60a07ab051db08ec803727bd30794d1a07 [file] [log] [blame]
Chris Lattner467dd2e2004-03-08 17:06:13 +00001//===- ProfilingUtils.cpp - Helper functions shared by profilers ----------===//
Misha Brukmanfd939082005-04-21 23:48:37 +00002//
Chris Lattner467dd2e2004-03-08 17:06:13 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
Chris Lattner467dd2e2004-03-08 17:06:13 +00008//===----------------------------------------------------------------------===//
9//
Gabor Greif29d8aa72008-03-06 10:36:00 +000010// This file implements a few helper functions which are used by profile
Chris Lattner467dd2e2004-03-08 17:06:13 +000011// 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 Anderson50895512009-07-06 18:42:36 +000021#include "llvm/LLVMContext.h"
Chris Lattner467dd2e2004-03-08 17:06:13 +000022#include "llvm/Module.h"
23
24void llvm::InsertProfilingInitCall(Function *MainFn, const char *FnName,
Andrew Trick04317cc2011-01-29 01:09:53 +000025 GlobalValue *Array,
26 PointerType *arrayType) {
Owen Anderson1d0be152009-08-13 21:58:54 +000027 LLVMContext &Context = MainFn->getContext();
Andrew Trick04317cc2011-01-29 01:09:53 +000028 const Type *ArgVTy =
Duncan Sandsac53a0b2009-10-06 15:40:36 +000029 PointerType::getUnqual(Type::getInt8PtrTy(Context));
Andrew Trick04317cc2011-01-29 01:09:53 +000030 const PointerType *UIntPtr = arrayType ? arrayType :
31 Type::getInt32PtrTy(Context);
Chris Lattner467dd2e2004-03-08 17:06:13 +000032 Module &M = *MainFn->getParent();
Owen Anderson1d0be152009-08-13 21:58:54 +000033 Constant *InitFn = M.getOrInsertFunction(FnName, Type::getInt32Ty(Context),
34 Type::getInt32Ty(Context),
35 ArgVTy, UIntPtr,
36 Type::getInt32Ty(Context),
Jeff Cohen66c5fd62005-10-23 04:37:20 +000037 (Type *)0);
Chris Lattner467dd2e2004-03-08 17:06:13 +000038
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 Anderson1d0be152009-08-13 21:58:54 +000042 Args[0] = Constant::getNullValue(Type::getInt32Ty(Context));
Owen Andersona7235ea2009-07-31 20:28:14 +000043 Args[1] = Constant::getNullValue(ArgVTy);
Chris Lattner467dd2e2004-03-08 17:06:13 +000044
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 Anderson1d0be152009-08-13 21:58:54 +000050 std::vector<Constant*> GEPIndices(2,
51 Constant::getNullValue(Type::getInt32Ty(Context)));
Brian Gaekeebbc0e92004-05-03 22:06:33 +000052 unsigned NumElements = 0;
53 if (Array) {
Owen Andersonbaf3c402009-07-29 18:55:55 +000054 Args[2] = ConstantExpr::getGetElementPtr(Array, &GEPIndices[0],
Chris Lattnerec1f7522007-02-19 07:34:47 +000055 GEPIndices.size());
Brian Gaekeebbc0e92004-05-03 22:06:33 +000056 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 Anderson9e9a0d52009-07-30 23:03:37 +000061 Args[2] = ConstantPointerNull::get(UIntPtr);
Brian Gaekeebbc0e92004-05-03 22:06:33 +000062 }
Owen Anderson1d0be152009-08-13 21:58:54 +000063 Args[3] = ConstantInt::get(Type::getInt32Ty(Context), NumElements);
Misha Brukmanfd939082005-04-21 23:48:37 +000064
Gabor Greif18c383f2010-06-28 12:31:35 +000065 CallInst *InitCall = CallInst::Create(InitFn, Args.begin(), Args.end(),
66 "newargc", InsertPos);
Chris Lattner467dd2e2004-03-08 17:06:13 +000067
68 // If argc or argv are not available in main, just pass null values in.
Chris Lattnere4d5c442005-03-15 04:54:21 +000069 Function::arg_iterator AI;
70 switch (MainFn->arg_size()) {
Chris Lattner467dd2e2004-03-08 17:06:13 +000071 default:
72 case 2:
Chris Lattnere4d5c442005-03-15 04:54:21 +000073 AI = MainFn->arg_begin(); ++AI;
Chris Lattner467dd2e2004-03-08 17:06:13 +000074 if (AI->getType() != ArgVTy) {
Andrew Trick04317cc2011-01-29 01:09:53 +000075 Instruction::CastOps opcode = CastInst::getCastOpcode(AI, false, ArgVTy,
Reid Spencer06e3f4e2006-12-21 07:49:49 +000076 false);
Andrew Trick04317cc2011-01-29 01:09:53 +000077 InitCall->setArgOperand(1,
Gabor Greif7cbd8a32008-05-16 19:29:10 +000078 CastInst::Create(opcode, AI, ArgVTy, "argv.cast", InitCall));
Chris Lattner467dd2e2004-03-08 17:06:13 +000079 } else {
Gabor Greif18c383f2010-06-28 12:31:35 +000080 InitCall->setArgOperand(1, AI);
Chris Lattner467dd2e2004-03-08 17:06:13 +000081 }
Reid Spencer7b06bd52006-12-13 00:50:17 +000082 /* FALL THROUGH */
Chris Lattner467dd2e2004-03-08 17:06:13 +000083
84 case 1:
Chris Lattnere4d5c442005-03-15 04:54:21 +000085 AI = MainFn->arg_begin();
Chris Lattner467dd2e2004-03-08 17:06:13 +000086 // If the program looked at argc, have it look at the return value of the
87 // init call instead.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000088 if (!AI->getType()->isIntegerTy(32)) {
Reid Spencer8a903db2006-12-18 08:47:13 +000089 Instruction::CastOps opcode;
90 if (!AI->use_empty()) {
Reid Spencer06e3f4e2006-12-21 07:49:49 +000091 opcode = CastInst::getCastOpcode(InitCall, true, AI->getType(), true);
Reid Spencer3da59db2006-11-27 01:05:10 +000092 AI->replaceAllUsesWith(
Gabor Greif7cbd8a32008-05-16 19:29:10 +000093 CastInst::Create(opcode, InitCall, AI->getType(), "", InsertPos));
Reid Spencer8a903db2006-12-18 08:47:13 +000094 }
Owen Anderson1d0be152009-08-13 21:58:54 +000095 opcode = CastInst::getCastOpcode(AI, true,
96 Type::getInt32Ty(Context), true);
Andrew Trick04317cc2011-01-29 01:09:53 +000097 InitCall->setArgOperand(0,
Owen Anderson1d0be152009-08-13 21:58:54 +000098 CastInst::Create(opcode, AI, Type::getInt32Ty(Context),
99 "argc.cast", InitCall));
Chris Lattner467dd2e2004-03-08 17:06:13 +0000100 } else {
101 AI->replaceAllUsesWith(InitCall);
Gabor Greif18c383f2010-06-28 12:31:35 +0000102 InitCall->setArgOperand(0, AI);
Chris Lattner467dd2e2004-03-08 17:06:13 +0000103 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000104
Chris Lattner467dd2e2004-03-08 17:06:13 +0000105 case 0: break;
106 }
107}
108
109void llvm::IncrementCounterInBlock(BasicBlock *BB, unsigned CounterNum,
Andrew Trick04317cc2011-01-29 01:09:53 +0000110 GlobalValue *CounterArray, bool beginning) {
Chris Lattner467dd2e2004-03-08 17:06:13 +0000111 // Insert the increment after any alloca or PHI instructions...
Andrew Trick04317cc2011-01-29 01:09:53 +0000112 BasicBlock::iterator InsertPos = beginning ? BB->getFirstNonPHI() :
113 BB->getTerminator();
Dan Gohman02dea8b2008-05-23 21:05:58 +0000114 while (isa<AllocaInst>(InsertPos))
Chris Lattner467dd2e2004-03-08 17:06:13 +0000115 ++InsertPos;
116
Owen Anderson1d0be152009-08-13 21:58:54 +0000117 LLVMContext &Context = BB->getContext();
118
Chris Lattner467dd2e2004-03-08 17:06:13 +0000119 // Create the getelementptr constant expression
120 std::vector<Constant*> Indices(2);
Owen Anderson1d0be152009-08-13 21:58:54 +0000121 Indices[0] = Constant::getNullValue(Type::getInt32Ty(Context));
122 Indices[1] = ConstantInt::get(Type::getInt32Ty(Context), CounterNum);
Andrew Trick04317cc2011-01-29 01:09:53 +0000123 Constant *ElementPtr =
Owen Andersonbaf3c402009-07-29 18:55:55 +0000124 ConstantExpr::getGetElementPtr(CounterArray, &Indices[0],
Owen Anderson50895512009-07-06 18:42:36 +0000125 Indices.size());
Chris Lattner467dd2e2004-03-08 17:06:13 +0000126
127 // Load, increment and store the value back.
128 Value *OldVal = new LoadInst(ElementPtr, "OldFuncCounter", InsertPos);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000129 Value *NewVal = BinaryOperator::Create(Instruction::Add, OldVal,
Owen Anderson1d0be152009-08-13 21:58:54 +0000130 ConstantInt::get(Type::getInt32Ty(Context), 1),
Chris Lattner467dd2e2004-03-08 17:06:13 +0000131 "NewFuncCounter", InsertPos);
132 new StoreInst(NewVal, ElementPtr, InsertPos);
133}