1
//===- ProfilingUtils.cpp - Helper functions shared by profilers ----------===//
3
// The LLVM Compiler Infrastructure
5
// This file is distributed under the University of Illinois Open Source
6
// License. See LICENSE.TXT for details.
8
//===----------------------------------------------------------------------===//
10
// This file 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
15
//===----------------------------------------------------------------------===//
17
#include "ProfilingUtils.h"
18
#include "llvm/Constants.h"
19
#include "llvm/DerivedTypes.h"
20
#include "llvm/Instructions.h"
21
#include "llvm/LLVMContext.h"
22
#include "llvm/Module.h"
24
void llvm::InsertProfilingInitCall(Function *MainFn, const char *FnName,
26
PointerType *arrayType) {
27
LLVMContext &Context = MainFn->getContext();
29
PointerType::getUnqual(Type::getInt8PtrTy(Context));
30
PointerType *UIntPtr = arrayType ? arrayType :
31
Type::getInt32PtrTy(Context);
32
Module &M = *MainFn->getParent();
33
Constant *InitFn = M.getOrInsertFunction(FnName, Type::getInt32Ty(Context),
34
Type::getInt32Ty(Context),
36
Type::getInt32Ty(Context),
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);
42
Args[0] = Constant::getNullValue(Type::getInt32Ty(Context));
43
Args[1] = Constant::getNullValue(ArgVTy);
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;
50
std::vector<Constant*> GEPIndices(2,
51
Constant::getNullValue(Type::getInt32Ty(Context)));
52
unsigned NumElements = 0;
54
Args[2] = ConstantExpr::getGetElementPtr(Array, GEPIndices);
56
cast<ArrayType>(Array->getType()->getElementType())->getNumElements();
58
// If this profiling instrumentation doesn't have a constant array, just
60
Args[2] = ConstantPointerNull::get(UIntPtr);
62
Args[3] = ConstantInt::get(Type::getInt32Ty(Context), NumElements);
64
CallInst *InitCall = CallInst::Create(InitFn, Args, "newargc", InsertPos);
66
// If argc or argv are not available in main, just pass null values in.
67
Function::arg_iterator AI;
68
switch (MainFn->arg_size()) {
71
AI = MainFn->arg_begin(); ++AI;
72
if (AI->getType() != ArgVTy) {
73
Instruction::CastOps opcode = CastInst::getCastOpcode(AI, false, ArgVTy,
75
InitCall->setArgOperand(1,
76
CastInst::Create(opcode, AI, ArgVTy, "argv.cast", InitCall));
78
InitCall->setArgOperand(1, AI);
83
AI = MainFn->arg_begin();
84
// If the program looked at argc, have it look at the return value of the
86
if (!AI->getType()->isIntegerTy(32)) {
87
Instruction::CastOps opcode;
88
if (!AI->use_empty()) {
89
opcode = CastInst::getCastOpcode(InitCall, true, AI->getType(), true);
90
AI->replaceAllUsesWith(
91
CastInst::Create(opcode, InitCall, AI->getType(), "", InsertPos));
93
opcode = CastInst::getCastOpcode(AI, true,
94
Type::getInt32Ty(Context), true);
95
InitCall->setArgOperand(0,
96
CastInst::Create(opcode, AI, Type::getInt32Ty(Context),
97
"argc.cast", InitCall));
99
AI->replaceAllUsesWith(InitCall);
100
InitCall->setArgOperand(0, AI);
107
void llvm::IncrementCounterInBlock(BasicBlock *BB, unsigned CounterNum,
108
GlobalValue *CounterArray, bool beginning) {
109
// Insert the increment after any alloca or PHI instructions...
110
BasicBlock::iterator InsertPos = beginning ? BB->getFirstInsertionPt() :
112
while (isa<AllocaInst>(InsertPos))
115
LLVMContext &Context = BB->getContext();
117
// Create the getelementptr constant expression
118
std::vector<Constant*> Indices(2);
119
Indices[0] = Constant::getNullValue(Type::getInt32Ty(Context));
120
Indices[1] = ConstantInt::get(Type::getInt32Ty(Context), CounterNum);
121
Constant *ElementPtr =
122
ConstantExpr::getGetElementPtr(CounterArray, Indices);
124
// Load, increment and store the value back.
125
Value *OldVal = new LoadInst(ElementPtr, "OldFuncCounter", InsertPos);
126
Value *NewVal = BinaryOperator::Create(Instruction::Add, OldVal,
127
ConstantInt::get(Type::getInt32Ty(Context), 1),
128
"NewFuncCounter", InsertPos);
129
new StoreInst(NewVal, ElementPtr, InsertPos);
132
void llvm::InsertProfilingShutdownCall(Function *Callee, Module *Mod) {
133
// llvm.global_dtors is an array of type { i32, void ()* }. Prepare those
135
Type *GlobalDtorElems[2] = {
136
Type::getInt32Ty(Mod->getContext()),
137
FunctionType::get(Type::getVoidTy(Mod->getContext()), false)->getPointerTo()
139
StructType *GlobalDtorElemTy =
140
StructType::get(Mod->getContext(), GlobalDtorElems, false);
142
// Construct the new element we'll be adding.
143
Constant *Elem[2] = {
144
ConstantInt::get(Type::getInt32Ty(Mod->getContext()), 65535),
145
ConstantExpr::getBitCast(Callee, GlobalDtorElems[1])
148
// If llvm.global_dtors exists, make a copy of the things in its list and
149
// delete it, to replace it with one that has a larger array type.
150
std::vector<Constant *> dtors;
151
if (GlobalVariable *GlobalDtors = Mod->getNamedGlobal("llvm.global_dtors")) {
152
if (ConstantArray *InitList =
153
dyn_cast<ConstantArray>(GlobalDtors->getInitializer())) {
154
for (unsigned i = 0, e = InitList->getType()->getNumElements();
156
dtors.push_back(cast<Constant>(InitList->getOperand(i)));
158
GlobalDtors->eraseFromParent();
161
// Build up llvm.global_dtors with our new item in it.
162
GlobalVariable *GlobalDtors = new GlobalVariable(
163
*Mod, ArrayType::get(GlobalDtorElemTy, 1), false,
164
GlobalValue::AppendingLinkage, NULL, "llvm.global_dtors");
166
dtors.push_back(ConstantStruct::get(GlobalDtorElemTy, Elem));
167
GlobalDtors->setInitializer(ConstantArray::get(
168
cast<ArrayType>(GlobalDtors->getType()->getElementType()), dtors));