blob: dc34bf70a311502ecb9533a792c5a313cb021e46 [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,
25 GlobalValue *Array) {
Christopher Lamb43ad6b32007-12-17 01:12:55 +000026 const Type *ArgVTy =
Owen Andersondebcb012009-07-29 22:17:13 +000027 PointerType::getUnqual(PointerType::getUnqual(Type::Int8Ty));
28 const PointerType *UIntPtr = PointerType::getUnqual(Type::Int32Ty);
Chris Lattner467dd2e2004-03-08 17:06:13 +000029 Module &M = *MainFn->getParent();
Chris Lattnerfebe5f12007-01-07 07:22:20 +000030 Constant *InitFn = M.getOrInsertFunction(FnName, Type::Int32Ty, Type::Int32Ty,
Reid Spencerc5b206b2006-12-31 05:48:39 +000031 ArgVTy, UIntPtr, Type::Int32Ty,
Jeff Cohen66c5fd62005-10-23 04:37:20 +000032 (Type *)0);
Chris Lattner467dd2e2004-03-08 17:06:13 +000033
34 // This could force argc and argv into programs that wouldn't otherwise have
35 // them, but instead we just pass null values in.
36 std::vector<Value*> Args(4);
Owen Andersona7235ea2009-07-31 20:28:14 +000037 Args[0] = Constant::getNullValue(Type::Int32Ty);
38 Args[1] = Constant::getNullValue(ArgVTy);
Chris Lattner467dd2e2004-03-08 17:06:13 +000039
40 // Skip over any allocas in the entry block.
41 BasicBlock *Entry = MainFn->begin();
42 BasicBlock::iterator InsertPos = Entry->begin();
43 while (isa<AllocaInst>(InsertPos)) ++InsertPos;
44
Owen Andersona7235ea2009-07-31 20:28:14 +000045 std::vector<Constant*> GEPIndices(2, Constant::getNullValue(Type::Int32Ty));
Brian Gaekeebbc0e92004-05-03 22:06:33 +000046 unsigned NumElements = 0;
47 if (Array) {
Owen Andersonbaf3c402009-07-29 18:55:55 +000048 Args[2] = ConstantExpr::getGetElementPtr(Array, &GEPIndices[0],
Chris Lattnerec1f7522007-02-19 07:34:47 +000049 GEPIndices.size());
Brian Gaekeebbc0e92004-05-03 22:06:33 +000050 NumElements =
51 cast<ArrayType>(Array->getType()->getElementType())->getNumElements();
52 } else {
53 // If this profiling instrumentation doesn't have a constant array, just
54 // pass null.
Owen Anderson9e9a0d52009-07-30 23:03:37 +000055 Args[2] = ConstantPointerNull::get(UIntPtr);
Brian Gaekeebbc0e92004-05-03 22:06:33 +000056 }
Owen Andersoneed707b2009-07-24 23:12:02 +000057 Args[3] = ConstantInt::get(Type::Int32Ty, NumElements);
Misha Brukmanfd939082005-04-21 23:48:37 +000058
Gabor Greif051a9502008-04-06 20:25:17 +000059 Instruction *InitCall = CallInst::Create(InitFn, Args.begin(), Args.end(),
60 "newargc", InsertPos);
Chris Lattner467dd2e2004-03-08 17:06:13 +000061
62 // If argc or argv are not available in main, just pass null values in.
Chris Lattnere4d5c442005-03-15 04:54:21 +000063 Function::arg_iterator AI;
64 switch (MainFn->arg_size()) {
Chris Lattner467dd2e2004-03-08 17:06:13 +000065 default:
66 case 2:
Chris Lattnere4d5c442005-03-15 04:54:21 +000067 AI = MainFn->arg_begin(); ++AI;
Chris Lattner467dd2e2004-03-08 17:06:13 +000068 if (AI->getType() != ArgVTy) {
Reid Spencer06e3f4e2006-12-21 07:49:49 +000069 Instruction::CastOps opcode = CastInst::getCastOpcode(AI, false, ArgVTy,
70 false);
Reid Spencer3da59db2006-11-27 01:05:10 +000071 InitCall->setOperand(2,
Gabor Greif7cbd8a32008-05-16 19:29:10 +000072 CastInst::Create(opcode, AI, ArgVTy, "argv.cast", InitCall));
Chris Lattner467dd2e2004-03-08 17:06:13 +000073 } else {
74 InitCall->setOperand(2, AI);
75 }
Reid Spencer7b06bd52006-12-13 00:50:17 +000076 /* FALL THROUGH */
Chris Lattner467dd2e2004-03-08 17:06:13 +000077
78 case 1:
Chris Lattnere4d5c442005-03-15 04:54:21 +000079 AI = MainFn->arg_begin();
Chris Lattner467dd2e2004-03-08 17:06:13 +000080 // If the program looked at argc, have it look at the return value of the
81 // init call instead.
Reid Spencerc5b206b2006-12-31 05:48:39 +000082 if (AI->getType() != Type::Int32Ty) {
Reid Spencer8a903db2006-12-18 08:47:13 +000083 Instruction::CastOps opcode;
84 if (!AI->use_empty()) {
Reid Spencer06e3f4e2006-12-21 07:49:49 +000085 opcode = CastInst::getCastOpcode(InitCall, true, AI->getType(), true);
Reid Spencer3da59db2006-11-27 01:05:10 +000086 AI->replaceAllUsesWith(
Gabor Greif7cbd8a32008-05-16 19:29:10 +000087 CastInst::Create(opcode, InitCall, AI->getType(), "", InsertPos));
Reid Spencer8a903db2006-12-18 08:47:13 +000088 }
Reid Spencerc5b206b2006-12-31 05:48:39 +000089 opcode = CastInst::getCastOpcode(AI, true, Type::Int32Ty, true);
Reid Spencer3da59db2006-11-27 01:05:10 +000090 InitCall->setOperand(1,
Gabor Greif7cbd8a32008-05-16 19:29:10 +000091 CastInst::Create(opcode, AI, Type::Int32Ty, "argc.cast", InitCall));
Chris Lattner467dd2e2004-03-08 17:06:13 +000092 } else {
93 AI->replaceAllUsesWith(InitCall);
94 InitCall->setOperand(1, AI);
95 }
Misha Brukmanfd939082005-04-21 23:48:37 +000096
Chris Lattner467dd2e2004-03-08 17:06:13 +000097 case 0: break;
98 }
99}
100
101void llvm::IncrementCounterInBlock(BasicBlock *BB, unsigned CounterNum,
Reid Spencer518310c2004-07-18 00:44:37 +0000102 GlobalValue *CounterArray) {
Chris Lattner467dd2e2004-03-08 17:06:13 +0000103 // Insert the increment after any alloca or PHI instructions...
Dan Gohman02dea8b2008-05-23 21:05:58 +0000104 BasicBlock::iterator InsertPos = BB->getFirstNonPHI();
105 while (isa<AllocaInst>(InsertPos))
Chris Lattner467dd2e2004-03-08 17:06:13 +0000106 ++InsertPos;
107
108 // Create the getelementptr constant expression
109 std::vector<Constant*> Indices(2);
Owen Andersona7235ea2009-07-31 20:28:14 +0000110 Indices[0] = Constant::getNullValue(Type::Int32Ty);
Owen Andersoneed707b2009-07-24 23:12:02 +0000111 Indices[1] = ConstantInt::get(Type::Int32Ty, CounterNum);
Chris Lattnerec1f7522007-02-19 07:34:47 +0000112 Constant *ElementPtr =
Owen Andersonbaf3c402009-07-29 18:55:55 +0000113 ConstantExpr::getGetElementPtr(CounterArray, &Indices[0],
Owen Anderson50895512009-07-06 18:42:36 +0000114 Indices.size());
Chris Lattner467dd2e2004-03-08 17:06:13 +0000115
116 // Load, increment and store the value back.
117 Value *OldVal = new LoadInst(ElementPtr, "OldFuncCounter", InsertPos);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000118 Value *NewVal = BinaryOperator::Create(Instruction::Add, OldVal,
Owen Andersoneed707b2009-07-24 23:12:02 +0000119 ConstantInt::get(Type::Int32Ty, 1),
Chris Lattner467dd2e2004-03-08 17:06:13 +0000120 "NewFuncCounter", InsertPos);
121 new StoreInst(NewVal, ElementPtr, InsertPos);
122}