blob: 0263c75c75259d042429fb21caf9ce49c99adc66 [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();
Chris Lattnerdb125cf2011-07-18 04:54:35 +000028 Type *ArgVTy =
Duncan Sandsac53a0b2009-10-06 15:40:36 +000029 PointerType::getUnqual(Type::getInt8PtrTy(Context));
Chris Lattnerdb125cf2011-07-18 04:54:35 +000030 PointerType *UIntPtr = arrayType ? arrayType :
Andrew Trick04317cc2011-01-29 01:09:53 +000031 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) {
Jay Foaddab3d292011-07-21 14:31:17 +000054 Args[2] = ConstantExpr::getGetElementPtr(Array, GEPIndices);
Brian Gaekeebbc0e92004-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 Anderson9e9a0d52009-07-30 23:03:37 +000060 Args[2] = ConstantPointerNull::get(UIntPtr);
Brian Gaekeebbc0e92004-05-03 22:06:33 +000061 }
Owen Anderson1d0be152009-08-13 21:58:54 +000062 Args[3] = ConstantInt::get(Type::getInt32Ty(Context), NumElements);
Misha Brukmanfd939082005-04-21 23:48:37 +000063
Jay Foada3efbb12011-07-15 08:37:34 +000064 CallInst *InitCall = CallInst::Create(InitFn, Args, "newargc", InsertPos);
Chris Lattner467dd2e2004-03-08 17:06:13 +000065
66 // If argc or argv are not available in main, just pass null values in.
Chris Lattnere4d5c442005-03-15 04:54:21 +000067 Function::arg_iterator AI;
68 switch (MainFn->arg_size()) {
Chris Lattner467dd2e2004-03-08 17:06:13 +000069 default:
70 case 2:
Chris Lattnere4d5c442005-03-15 04:54:21 +000071 AI = MainFn->arg_begin(); ++AI;
Chris Lattner467dd2e2004-03-08 17:06:13 +000072 if (AI->getType() != ArgVTy) {
Andrew Trick04317cc2011-01-29 01:09:53 +000073 Instruction::CastOps opcode = CastInst::getCastOpcode(AI, false, ArgVTy,
Reid Spencer06e3f4e2006-12-21 07:49:49 +000074 false);
Andrew Trick04317cc2011-01-29 01:09:53 +000075 InitCall->setArgOperand(1,
Gabor Greif7cbd8a32008-05-16 19:29:10 +000076 CastInst::Create(opcode, AI, ArgVTy, "argv.cast", InitCall));
Chris Lattner467dd2e2004-03-08 17:06:13 +000077 } else {
Gabor Greif18c383f2010-06-28 12:31:35 +000078 InitCall->setArgOperand(1, AI);
Chris Lattner467dd2e2004-03-08 17:06:13 +000079 }
Reid Spencer7b06bd52006-12-13 00:50:17 +000080 /* FALL THROUGH */
Chris Lattner467dd2e2004-03-08 17:06:13 +000081
82 case 1:
Chris Lattnere4d5c442005-03-15 04:54:21 +000083 AI = MainFn->arg_begin();
Chris Lattner467dd2e2004-03-08 17:06:13 +000084 // If the program looked at argc, have it look at the return value of the
85 // init call instead.
Duncan Sandsb0bc6c32010-02-15 16:12:20 +000086 if (!AI->getType()->isIntegerTy(32)) {
Reid Spencer8a903db2006-12-18 08:47:13 +000087 Instruction::CastOps opcode;
88 if (!AI->use_empty()) {
Reid Spencer06e3f4e2006-12-21 07:49:49 +000089 opcode = CastInst::getCastOpcode(InitCall, true, AI->getType(), true);
Reid Spencer3da59db2006-11-27 01:05:10 +000090 AI->replaceAllUsesWith(
Gabor Greif7cbd8a32008-05-16 19:29:10 +000091 CastInst::Create(opcode, InitCall, AI->getType(), "", InsertPos));
Reid Spencer8a903db2006-12-18 08:47:13 +000092 }
Owen Anderson1d0be152009-08-13 21:58:54 +000093 opcode = CastInst::getCastOpcode(AI, true,
94 Type::getInt32Ty(Context), true);
Andrew Trick04317cc2011-01-29 01:09:53 +000095 InitCall->setArgOperand(0,
Owen Anderson1d0be152009-08-13 21:58:54 +000096 CastInst::Create(opcode, AI, Type::getInt32Ty(Context),
97 "argc.cast", InitCall));
Chris Lattner467dd2e2004-03-08 17:06:13 +000098 } else {
99 AI->replaceAllUsesWith(InitCall);
Gabor Greif18c383f2010-06-28 12:31:35 +0000100 InitCall->setArgOperand(0, AI);
Chris Lattner467dd2e2004-03-08 17:06:13 +0000101 }
Misha Brukmanfd939082005-04-21 23:48:37 +0000102
Chris Lattner467dd2e2004-03-08 17:06:13 +0000103 case 0: break;
104 }
105}
106
107void llvm::IncrementCounterInBlock(BasicBlock *BB, unsigned CounterNum,
Andrew Trick04317cc2011-01-29 01:09:53 +0000108 GlobalValue *CounterArray, bool beginning) {
Chris Lattner467dd2e2004-03-08 17:06:13 +0000109 // Insert the increment after any alloca or PHI instructions...
Andrew Trick04317cc2011-01-29 01:09:53 +0000110 BasicBlock::iterator InsertPos = beginning ? BB->getFirstNonPHI() :
Nick Lewyckyceb465a2011-04-12 01:02:45 +0000111 BB->getTerminator();
Dan Gohman02dea8b2008-05-23 21:05:58 +0000112 while (isa<AllocaInst>(InsertPos))
Chris Lattner467dd2e2004-03-08 17:06:13 +0000113 ++InsertPos;
114
Owen Anderson1d0be152009-08-13 21:58:54 +0000115 LLVMContext &Context = BB->getContext();
116
Chris Lattner467dd2e2004-03-08 17:06:13 +0000117 // Create the getelementptr constant expression
118 std::vector<Constant*> Indices(2);
Owen Anderson1d0be152009-08-13 21:58:54 +0000119 Indices[0] = Constant::getNullValue(Type::getInt32Ty(Context));
120 Indices[1] = ConstantInt::get(Type::getInt32Ty(Context), CounterNum);
Andrew Trick04317cc2011-01-29 01:09:53 +0000121 Constant *ElementPtr =
Jay Foaddab3d292011-07-21 14:31:17 +0000122 ConstantExpr::getGetElementPtr(CounterArray, Indices);
Chris Lattner467dd2e2004-03-08 17:06:13 +0000123
124 // Load, increment and store the value back.
125 Value *OldVal = new LoadInst(ElementPtr, "OldFuncCounter", InsertPos);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000126 Value *NewVal = BinaryOperator::Create(Instruction::Add, OldVal,
Owen Anderson1d0be152009-08-13 21:58:54 +0000127 ConstantInt::get(Type::getInt32Ty(Context), 1),
Chris Lattner467dd2e2004-03-08 17:06:13 +0000128 "NewFuncCounter", InsertPos);
129 new StoreInst(NewVal, ElementPtr, InsertPos);
130}
Nick Lewycky918035f2011-04-08 22:19:52 +0000131
132void llvm::InsertProfilingShutdownCall(Function *Callee, Module *Mod) {
133 // llvm.global_dtors is an array of type { i32, void ()* }. Prepare those
134 // types.
Jay Foad5fdd6c82011-07-12 14:06:48 +0000135 Type *GlobalDtorElems[2] = {
Nick Lewycky918035f2011-04-08 22:19:52 +0000136 Type::getInt32Ty(Mod->getContext()),
137 FunctionType::get(Type::getVoidTy(Mod->getContext()), false)->getPointerTo()
138 };
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000139 StructType *GlobalDtorElemTy =
Nick Lewycky918035f2011-04-08 22:19:52 +0000140 StructType::get(Mod->getContext(), GlobalDtorElems, false);
141
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])
146 };
147
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")) {
Nick Lewyckyceb465a2011-04-12 01:02:45 +0000152 if (ConstantArray *InitList =
153 dyn_cast<ConstantArray>(GlobalDtors->getInitializer())) {
154 for (unsigned i = 0, e = InitList->getType()->getNumElements();
155 i != e; ++i)
156 dtors.push_back(cast<Constant>(InitList->getOperand(i)));
157 }
Nick Lewycky918035f2011-04-08 22:19:52 +0000158 GlobalDtors->eraseFromParent();
159 }
160
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");
Chris Lattnerb065b062011-06-20 04:01:31 +0000165
166 dtors.push_back(ConstantStruct::get(GlobalDtorElemTy, Elem));
Nick Lewycky918035f2011-04-08 22:19:52 +0000167 GlobalDtors->setInitializer(ConstantArray::get(
168 cast<ArrayType>(GlobalDtors->getType()->getElementType()), dtors));
169}