blob: 1a30e9ba288b34a47ab002991d771e52cb3cc07d [file] [log] [blame]
Chris Lattnerdae48f92004-03-08 17:06:13 +00001//===- ProfilingUtils.cpp - Helper functions shared by profilers ----------===//
Misha Brukmanb1c93172005-04-21 23:48:37 +00002//
Chris Lattnerdae48f92004-03-08 17:06:13 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Brukmanb1c93172005-04-21 23:48:37 +00007//
Chris Lattnerdae48f92004-03-08 17:06:13 +00008//===----------------------------------------------------------------------===//
9//
Gabor Greifad19df02008-03-06 10:36:00 +000010// This file implements a few helper functions which are used by profile
Chris Lattnerdae48f92004-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 Anderson39f00cc2009-07-06 18:42:36 +000021#include "llvm/LLVMContext.h"
Chris Lattnerdae48f92004-03-08 17:06:13 +000022#include "llvm/Module.h"
23
24void llvm::InsertProfilingInitCall(Function *MainFn, const char *FnName,
25 GlobalValue *Array) {
Owen Anderson55f1c092009-08-13 21:58:54 +000026 LLVMContext &Context = MainFn->getContext();
Christopher Lambedf07882007-12-17 01:12:55 +000027 const Type *ArgVTy =
Duncan Sands9ed7b162009-10-06 15:40:36 +000028 PointerType::getUnqual(Type::getInt8PtrTy(Context));
Owen Anderson55f1c092009-08-13 21:58:54 +000029 const PointerType *UIntPtr =
Duncan Sands9ed7b162009-10-06 15:40:36 +000030 Type::getInt32PtrTy(Context);
Chris Lattnerdae48f92004-03-08 17:06:13 +000031 Module &M = *MainFn->getParent();
Owen Anderson55f1c092009-08-13 21:58:54 +000032 Constant *InitFn = M.getOrInsertFunction(FnName, Type::getInt32Ty(Context),
33 Type::getInt32Ty(Context),
34 ArgVTy, UIntPtr,
35 Type::getInt32Ty(Context),
Jeff Cohen11e26b52005-10-23 04:37:20 +000036 (Type *)0);
Chris Lattnerdae48f92004-03-08 17:06:13 +000037
38 // This could force argc and argv into programs that wouldn't otherwise have
39 // them, but instead we just pass null values in.
40 std::vector<Value*> Args(4);
Owen Anderson55f1c092009-08-13 21:58:54 +000041 Args[0] = Constant::getNullValue(Type::getInt32Ty(Context));
Owen Anderson5a1acd92009-07-31 20:28:14 +000042 Args[1] = Constant::getNullValue(ArgVTy);
Chris Lattnerdae48f92004-03-08 17:06:13 +000043
44 // Skip over any allocas in the entry block.
45 BasicBlock *Entry = MainFn->begin();
46 BasicBlock::iterator InsertPos = Entry->begin();
47 while (isa<AllocaInst>(InsertPos)) ++InsertPos;
48
Owen Anderson55f1c092009-08-13 21:58:54 +000049 std::vector<Constant*> GEPIndices(2,
50 Constant::getNullValue(Type::getInt32Ty(Context)));
Brian Gaekee9619602004-05-03 22:06:33 +000051 unsigned NumElements = 0;
52 if (Array) {
Owen Anderson487375e2009-07-29 18:55:55 +000053 Args[2] = ConstantExpr::getGetElementPtr(Array, &GEPIndices[0],
Chris Lattnerb5f6d0c2007-02-19 07:34:47 +000054 GEPIndices.size());
Brian Gaekee9619602004-05-03 22:06:33 +000055 NumElements =
56 cast<ArrayType>(Array->getType()->getElementType())->getNumElements();
57 } else {
58 // If this profiling instrumentation doesn't have a constant array, just
59 // pass null.
Owen Andersonb292b8c2009-07-30 23:03:37 +000060 Args[2] = ConstantPointerNull::get(UIntPtr);
Brian Gaekee9619602004-05-03 22:06:33 +000061 }
Owen Anderson55f1c092009-08-13 21:58:54 +000062 Args[3] = ConstantInt::get(Type::getInt32Ty(Context), NumElements);
Misha Brukmanb1c93172005-04-21 23:48:37 +000063
Gabor Greif2dd43072010-06-28 12:31:35 +000064 CallInst *InitCall = CallInst::Create(InitFn, Args.begin(), Args.end(),
65 "newargc", InsertPos);
Chris Lattnerdae48f92004-03-08 17:06:13 +000066
67 // If argc or argv are not available in main, just pass null values in.
Chris Lattner531f9e92005-03-15 04:54:21 +000068 Function::arg_iterator AI;
69 switch (MainFn->arg_size()) {
Chris Lattnerdae48f92004-03-08 17:06:13 +000070 default:
71 case 2:
Chris Lattner531f9e92005-03-15 04:54:21 +000072 AI = MainFn->arg_begin(); ++AI;
Chris Lattnerdae48f92004-03-08 17:06:13 +000073 if (AI->getType() != ArgVTy) {
Reid Spencera276d092006-12-21 07:49:49 +000074 Instruction::CastOps opcode = CastInst::getCastOpcode(AI, false, ArgVTy,
75 false);
Gabor Greif2dd43072010-06-28 12:31:35 +000076 InitCall->setArgOperand(1,
Gabor Greife1f6e4b2008-05-16 19:29:10 +000077 CastInst::Create(opcode, AI, ArgVTy, "argv.cast", InitCall));
Chris Lattnerdae48f92004-03-08 17:06:13 +000078 } else {
Gabor Greif2dd43072010-06-28 12:31:35 +000079 InitCall->setArgOperand(1, AI);
Chris Lattnerdae48f92004-03-08 17:06:13 +000080 }
Reid Spencerbfe26ff2006-12-13 00:50:17 +000081 /* FALL THROUGH */
Chris Lattnerdae48f92004-03-08 17:06:13 +000082
83 case 1:
Chris Lattner531f9e92005-03-15 04:54:21 +000084 AI = MainFn->arg_begin();
Chris Lattnerdae48f92004-03-08 17:06:13 +000085 // If the program looked at argc, have it look at the return value of the
86 // init call instead.
Duncan Sands9dff9be2010-02-15 16:12:20 +000087 if (!AI->getType()->isIntegerTy(32)) {
Reid Spencer668d90f2006-12-18 08:47:13 +000088 Instruction::CastOps opcode;
89 if (!AI->use_empty()) {
Reid Spencera276d092006-12-21 07:49:49 +000090 opcode = CastInst::getCastOpcode(InitCall, true, AI->getType(), true);
Reid Spencer6c38f0b2006-11-27 01:05:10 +000091 AI->replaceAllUsesWith(
Gabor Greife1f6e4b2008-05-16 19:29:10 +000092 CastInst::Create(opcode, InitCall, AI->getType(), "", InsertPos));
Reid Spencer668d90f2006-12-18 08:47:13 +000093 }
Owen Anderson55f1c092009-08-13 21:58:54 +000094 opcode = CastInst::getCastOpcode(AI, true,
95 Type::getInt32Ty(Context), true);
Gabor Greif2dd43072010-06-28 12:31:35 +000096 InitCall->setArgOperand(0,
Owen Anderson55f1c092009-08-13 21:58:54 +000097 CastInst::Create(opcode, AI, Type::getInt32Ty(Context),
98 "argc.cast", InitCall));
Chris Lattnerdae48f92004-03-08 17:06:13 +000099 } else {
100 AI->replaceAllUsesWith(InitCall);
Gabor Greif2dd43072010-06-28 12:31:35 +0000101 InitCall->setArgOperand(0, AI);
Chris Lattnerdae48f92004-03-08 17:06:13 +0000102 }
Misha Brukmanb1c93172005-04-21 23:48:37 +0000103
Chris Lattnerdae48f92004-03-08 17:06:13 +0000104 case 0: break;
105 }
106}
107
108void llvm::IncrementCounterInBlock(BasicBlock *BB, unsigned CounterNum,
Reid Spencercb3fb5d2004-07-18 00:44:37 +0000109 GlobalValue *CounterArray) {
Chris Lattnerdae48f92004-03-08 17:06:13 +0000110 // Insert the increment after any alloca or PHI instructions...
Dan Gohmanf96e1372008-05-23 21:05:58 +0000111 BasicBlock::iterator InsertPos = BB->getFirstNonPHI();
112 while (isa<AllocaInst>(InsertPos))
Chris Lattnerdae48f92004-03-08 17:06:13 +0000113 ++InsertPos;
114
Owen Anderson55f1c092009-08-13 21:58:54 +0000115 LLVMContext &Context = BB->getContext();
116
Chris Lattnerdae48f92004-03-08 17:06:13 +0000117 // Create the getelementptr constant expression
118 std::vector<Constant*> Indices(2);
Owen Anderson55f1c092009-08-13 21:58:54 +0000119 Indices[0] = Constant::getNullValue(Type::getInt32Ty(Context));
120 Indices[1] = ConstantInt::get(Type::getInt32Ty(Context), CounterNum);
Chris Lattnerb5f6d0c2007-02-19 07:34:47 +0000121 Constant *ElementPtr =
Owen Anderson487375e2009-07-29 18:55:55 +0000122 ConstantExpr::getGetElementPtr(CounterArray, &Indices[0],
Owen Anderson39f00cc2009-07-06 18:42:36 +0000123 Indices.size());
Chris Lattnerdae48f92004-03-08 17:06:13 +0000124
125 // Load, increment and store the value back.
126 Value *OldVal = new LoadInst(ElementPtr, "OldFuncCounter", InsertPos);
Gabor Greife1f6e4b2008-05-16 19:29:10 +0000127 Value *NewVal = BinaryOperator::Create(Instruction::Add, OldVal,
Owen Anderson55f1c092009-08-13 21:58:54 +0000128 ConstantInt::get(Type::getInt32Ty(Context), 1),
Chris Lattnerdae48f92004-03-08 17:06:13 +0000129 "NewFuncCounter", InsertPos);
130 new StoreInst(NewVal, ElementPtr, InsertPos);
131}