2004-03-09 01:06:13 +08:00
|
|
|
//===- ProfilingUtils.cpp - Helper functions shared by profilers ----------===//
|
2005-04-22 07:48:37 +08:00
|
|
|
//
|
2004-03-09 01:06:13 +08:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 04:36:04 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-22 07:48:37 +08:00
|
|
|
//
|
2004-03-09 01:06:13 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2008-03-06 18:36:00 +08:00
|
|
|
// This file implements a few helper functions which are used by profile
|
2004-03-09 01:06:13 +08:00
|
|
|
// instrumentation code to instrument the code. This allows the profiler pass
|
|
|
|
// to worry about *what* to insert, and these functions take care of *how* to do
|
|
|
|
// it.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "ProfilingUtils.h"
|
|
|
|
#include "llvm/Constants.h"
|
|
|
|
#include "llvm/DerivedTypes.h"
|
|
|
|
#include "llvm/Instructions.h"
|
2009-07-07 02:42:36 +08:00
|
|
|
#include "llvm/LLVMContext.h"
|
2004-03-09 01:06:13 +08:00
|
|
|
#include "llvm/Module.h"
|
|
|
|
|
|
|
|
void llvm::InsertProfilingInitCall(Function *MainFn, const char *FnName,
|
|
|
|
GlobalValue *Array) {
|
2009-07-22 08:24:57 +08:00
|
|
|
LLVMContext &Context = MainFn->getContext();
|
2007-12-17 09:12:55 +08:00
|
|
|
const Type *ArgVTy =
|
2009-07-22 08:24:57 +08:00
|
|
|
Context.getPointerTypeUnqual(Context.getPointerTypeUnqual(Type::Int8Ty));
|
|
|
|
const PointerType *UIntPtr = Context.getPointerTypeUnqual(Type::Int32Ty);
|
2004-03-09 01:06:13 +08:00
|
|
|
Module &M = *MainFn->getParent();
|
2007-01-07 15:22:20 +08:00
|
|
|
Constant *InitFn = M.getOrInsertFunction(FnName, Type::Int32Ty, Type::Int32Ty,
|
2006-12-31 13:48:39 +08:00
|
|
|
ArgVTy, UIntPtr, Type::Int32Ty,
|
2005-10-23 12:37:20 +08:00
|
|
|
(Type *)0);
|
2004-03-09 01:06:13 +08:00
|
|
|
|
|
|
|
// This could force argc and argv into programs that wouldn't otherwise have
|
|
|
|
// them, but instead we just pass null values in.
|
|
|
|
std::vector<Value*> Args(4);
|
2009-07-22 08:24:57 +08:00
|
|
|
Args[0] = Context.getNullValue(Type::Int32Ty);
|
|
|
|
Args[1] = Context.getNullValue(ArgVTy);
|
2004-03-09 01:06:13 +08:00
|
|
|
|
|
|
|
// Skip over any allocas in the entry block.
|
|
|
|
BasicBlock *Entry = MainFn->begin();
|
|
|
|
BasicBlock::iterator InsertPos = Entry->begin();
|
|
|
|
while (isa<AllocaInst>(InsertPos)) ++InsertPos;
|
|
|
|
|
2009-07-22 08:24:57 +08:00
|
|
|
std::vector<Constant*> GEPIndices(2, Context.getNullValue(Type::Int32Ty));
|
2004-05-04 06:06:33 +08:00
|
|
|
unsigned NumElements = 0;
|
|
|
|
if (Array) {
|
2009-07-22 08:24:57 +08:00
|
|
|
Args[2] = Context.getConstantExprGetElementPtr(Array, &GEPIndices[0],
|
2007-02-19 15:34:47 +08:00
|
|
|
GEPIndices.size());
|
2004-05-04 06:06:33 +08:00
|
|
|
NumElements =
|
|
|
|
cast<ArrayType>(Array->getType()->getElementType())->getNumElements();
|
|
|
|
} else {
|
|
|
|
// If this profiling instrumentation doesn't have a constant array, just
|
|
|
|
// pass null.
|
2009-07-22 08:24:57 +08:00
|
|
|
Args[2] = Context.getConstantPointerNull(UIntPtr);
|
2004-05-04 06:06:33 +08:00
|
|
|
}
|
2009-07-25 07:12:02 +08:00
|
|
|
Args[3] = ConstantInt::get(Type::Int32Ty, NumElements);
|
2005-04-22 07:48:37 +08:00
|
|
|
|
2008-04-07 04:25:17 +08:00
|
|
|
Instruction *InitCall = CallInst::Create(InitFn, Args.begin(), Args.end(),
|
|
|
|
"newargc", InsertPos);
|
2004-03-09 01:06:13 +08:00
|
|
|
|
|
|
|
// If argc or argv are not available in main, just pass null values in.
|
2005-03-15 12:54:21 +08:00
|
|
|
Function::arg_iterator AI;
|
|
|
|
switch (MainFn->arg_size()) {
|
2004-03-09 01:06:13 +08:00
|
|
|
default:
|
|
|
|
case 2:
|
2005-03-15 12:54:21 +08:00
|
|
|
AI = MainFn->arg_begin(); ++AI;
|
2004-03-09 01:06:13 +08:00
|
|
|
if (AI->getType() != ArgVTy) {
|
2006-12-21 15:49:49 +08:00
|
|
|
Instruction::CastOps opcode = CastInst::getCastOpcode(AI, false, ArgVTy,
|
|
|
|
false);
|
2006-11-27 09:05:10 +08:00
|
|
|
InitCall->setOperand(2,
|
2008-05-17 03:29:10 +08:00
|
|
|
CastInst::Create(opcode, AI, ArgVTy, "argv.cast", InitCall));
|
2004-03-09 01:06:13 +08:00
|
|
|
} else {
|
|
|
|
InitCall->setOperand(2, AI);
|
|
|
|
}
|
2006-12-13 08:50:17 +08:00
|
|
|
/* FALL THROUGH */
|
2004-03-09 01:06:13 +08:00
|
|
|
|
|
|
|
case 1:
|
2005-03-15 12:54:21 +08:00
|
|
|
AI = MainFn->arg_begin();
|
2004-03-09 01:06:13 +08:00
|
|
|
// If the program looked at argc, have it look at the return value of the
|
|
|
|
// init call instead.
|
2006-12-31 13:48:39 +08:00
|
|
|
if (AI->getType() != Type::Int32Ty) {
|
2006-12-18 16:47:13 +08:00
|
|
|
Instruction::CastOps opcode;
|
|
|
|
if (!AI->use_empty()) {
|
2006-12-21 15:49:49 +08:00
|
|
|
opcode = CastInst::getCastOpcode(InitCall, true, AI->getType(), true);
|
2006-11-27 09:05:10 +08:00
|
|
|
AI->replaceAllUsesWith(
|
2008-05-17 03:29:10 +08:00
|
|
|
CastInst::Create(opcode, InitCall, AI->getType(), "", InsertPos));
|
2006-12-18 16:47:13 +08:00
|
|
|
}
|
2006-12-31 13:48:39 +08:00
|
|
|
opcode = CastInst::getCastOpcode(AI, true, Type::Int32Ty, true);
|
2006-11-27 09:05:10 +08:00
|
|
|
InitCall->setOperand(1,
|
2008-05-17 03:29:10 +08:00
|
|
|
CastInst::Create(opcode, AI, Type::Int32Ty, "argc.cast", InitCall));
|
2004-03-09 01:06:13 +08:00
|
|
|
} else {
|
|
|
|
AI->replaceAllUsesWith(InitCall);
|
|
|
|
InitCall->setOperand(1, AI);
|
|
|
|
}
|
2005-04-22 07:48:37 +08:00
|
|
|
|
2004-03-09 01:06:13 +08:00
|
|
|
case 0: break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void llvm::IncrementCounterInBlock(BasicBlock *BB, unsigned CounterNum,
|
2004-07-18 08:44:37 +08:00
|
|
|
GlobalValue *CounterArray) {
|
2009-07-22 08:24:57 +08:00
|
|
|
LLVMContext &Context = BB->getContext();
|
2009-07-07 02:42:36 +08:00
|
|
|
|
2004-03-09 01:06:13 +08:00
|
|
|
// Insert the increment after any alloca or PHI instructions...
|
2008-05-24 05:05:58 +08:00
|
|
|
BasicBlock::iterator InsertPos = BB->getFirstNonPHI();
|
|
|
|
while (isa<AllocaInst>(InsertPos))
|
2004-03-09 01:06:13 +08:00
|
|
|
++InsertPos;
|
|
|
|
|
|
|
|
// Create the getelementptr constant expression
|
|
|
|
std::vector<Constant*> Indices(2);
|
2009-07-22 08:24:57 +08:00
|
|
|
Indices[0] = Context.getNullValue(Type::Int32Ty);
|
2009-07-25 07:12:02 +08:00
|
|
|
Indices[1] = ConstantInt::get(Type::Int32Ty, CounterNum);
|
2007-02-19 15:34:47 +08:00
|
|
|
Constant *ElementPtr =
|
2009-07-22 08:24:57 +08:00
|
|
|
Context.getConstantExprGetElementPtr(CounterArray, &Indices[0],
|
2009-07-07 02:42:36 +08:00
|
|
|
Indices.size());
|
2004-03-09 01:06:13 +08:00
|
|
|
|
|
|
|
// Load, increment and store the value back.
|
|
|
|
Value *OldVal = new LoadInst(ElementPtr, "OldFuncCounter", InsertPos);
|
2008-05-17 03:29:10 +08:00
|
|
|
Value *NewVal = BinaryOperator::Create(Instruction::Add, OldVal,
|
2009-07-25 07:12:02 +08:00
|
|
|
ConstantInt::get(Type::Int32Ty, 1),
|
2004-03-09 01:06:13 +08:00
|
|
|
"NewFuncCounter", InsertPos);
|
|
|
|
new StoreInst(NewVal, ElementPtr, InsertPos);
|
|
|
|
}
|