blob: 91b8ec2c5c935c14e257a3cc18b997e76d2cc3c2 [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//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanfd939082005-04-21 23:48:37 +00007//
Chris Lattner467dd2e2004-03-08 17:06:13 +00008//===----------------------------------------------------------------------===//
9//
10// This files 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
13// it.
14//
15//===----------------------------------------------------------------------===//
16
17#include "ProfilingUtils.h"
18#include "llvm/Constants.h"
19#include "llvm/DerivedTypes.h"
20#include "llvm/Instructions.h"
21#include "llvm/Module.h"
22
23void llvm::InsertProfilingInitCall(Function *MainFn, const char *FnName,
24 GlobalValue *Array) {
Reid Spencerc5b206b2006-12-31 05:48:39 +000025 const Type *ArgVTy = PointerType::get(PointerType::get(Type::Int8Ty));
26 const PointerType *UIntPtr = PointerType::get(Type::Int32Ty);
Chris Lattner467dd2e2004-03-08 17:06:13 +000027 Module &M = *MainFn->getParent();
Chris Lattnerfebe5f12007-01-07 07:22:20 +000028 Constant *InitFn = M.getOrInsertFunction(FnName, Type::Int32Ty, Type::Int32Ty,
Reid Spencerc5b206b2006-12-31 05:48:39 +000029 ArgVTy, UIntPtr, Type::Int32Ty,
Jeff Cohen66c5fd62005-10-23 04:37:20 +000030 (Type *)0);
Chris Lattner467dd2e2004-03-08 17:06:13 +000031
32 // This could force argc and argv into programs that wouldn't otherwise have
33 // them, but instead we just pass null values in.
34 std::vector<Value*> Args(4);
Reid Spencerc5b206b2006-12-31 05:48:39 +000035 Args[0] = Constant::getNullValue(Type::Int32Ty);
Chris Lattner467dd2e2004-03-08 17:06:13 +000036 Args[1] = Constant::getNullValue(ArgVTy);
37
38 // Skip over any allocas in the entry block.
39 BasicBlock *Entry = MainFn->begin();
40 BasicBlock::iterator InsertPos = Entry->begin();
41 while (isa<AllocaInst>(InsertPos)) ++InsertPos;
42
Reid Spencerc5b206b2006-12-31 05:48:39 +000043 std::vector<Constant*> GEPIndices(2, Constant::getNullValue(Type::Int32Ty));
Brian Gaekeebbc0e92004-05-03 22:06:33 +000044 unsigned NumElements = 0;
45 if (Array) {
Chris Lattnerec1f7522007-02-19 07:34:47 +000046 Args[2] = ConstantExpr::getGetElementPtr(Array, &GEPIndices[0],
47 GEPIndices.size());
Brian Gaekeebbc0e92004-05-03 22:06:33 +000048 NumElements =
49 cast<ArrayType>(Array->getType()->getElementType())->getNumElements();
50 } else {
51 // If this profiling instrumentation doesn't have a constant array, just
52 // pass null.
53 Args[2] = ConstantPointerNull::get(UIntPtr);
54 }
Reid Spencerc5b206b2006-12-31 05:48:39 +000055 Args[3] = ConstantInt::get(Type::Int32Ty, NumElements);
Misha Brukmanfd939082005-04-21 23:48:37 +000056
David Greene52eec542007-08-01 03:43:44 +000057 Instruction *InitCall = new CallInst(InitFn, Args.begin(), Args.end(),
Chris Lattner93e985f2007-02-13 02:10:56 +000058 "newargc", InsertPos);
Chris Lattner467dd2e2004-03-08 17:06:13 +000059
60 // If argc or argv are not available in main, just pass null values in.
Chris Lattnere4d5c442005-03-15 04:54:21 +000061 Function::arg_iterator AI;
62 switch (MainFn->arg_size()) {
Chris Lattner467dd2e2004-03-08 17:06:13 +000063 default:
64 case 2:
Chris Lattnere4d5c442005-03-15 04:54:21 +000065 AI = MainFn->arg_begin(); ++AI;
Chris Lattner467dd2e2004-03-08 17:06:13 +000066 if (AI->getType() != ArgVTy) {
Reid Spencer06e3f4e2006-12-21 07:49:49 +000067 Instruction::CastOps opcode = CastInst::getCastOpcode(AI, false, ArgVTy,
68 false);
Reid Spencer3da59db2006-11-27 01:05:10 +000069 InitCall->setOperand(2,
Reid Spencer8a903db2006-12-18 08:47:13 +000070 CastInst::create(opcode, AI, ArgVTy, "argv.cast", InitCall));
Chris Lattner467dd2e2004-03-08 17:06:13 +000071 } else {
72 InitCall->setOperand(2, AI);
73 }
Reid Spencer7b06bd52006-12-13 00:50:17 +000074 /* FALL THROUGH */
Chris Lattner467dd2e2004-03-08 17:06:13 +000075
76 case 1:
Chris Lattnere4d5c442005-03-15 04:54:21 +000077 AI = MainFn->arg_begin();
Chris Lattner467dd2e2004-03-08 17:06:13 +000078 // If the program looked at argc, have it look at the return value of the
79 // init call instead.
Reid Spencerc5b206b2006-12-31 05:48:39 +000080 if (AI->getType() != Type::Int32Ty) {
Reid Spencer8a903db2006-12-18 08:47:13 +000081 Instruction::CastOps opcode;
82 if (!AI->use_empty()) {
Reid Spencer06e3f4e2006-12-21 07:49:49 +000083 opcode = CastInst::getCastOpcode(InitCall, true, AI->getType(), true);
Reid Spencer3da59db2006-11-27 01:05:10 +000084 AI->replaceAllUsesWith(
Reid Spencer8a903db2006-12-18 08:47:13 +000085 CastInst::create(opcode, InitCall, AI->getType(), "", InsertPos));
86 }
Reid Spencerc5b206b2006-12-31 05:48:39 +000087 opcode = CastInst::getCastOpcode(AI, true, Type::Int32Ty, true);
Reid Spencer3da59db2006-11-27 01:05:10 +000088 InitCall->setOperand(1,
Reid Spencerc5b206b2006-12-31 05:48:39 +000089 CastInst::create(opcode, AI, Type::Int32Ty, "argc.cast", InitCall));
Chris Lattner467dd2e2004-03-08 17:06:13 +000090 } else {
91 AI->replaceAllUsesWith(InitCall);
92 InitCall->setOperand(1, AI);
93 }
Misha Brukmanfd939082005-04-21 23:48:37 +000094
Chris Lattner467dd2e2004-03-08 17:06:13 +000095 case 0: break;
96 }
97}
98
99void llvm::IncrementCounterInBlock(BasicBlock *BB, unsigned CounterNum,
Reid Spencer518310c2004-07-18 00:44:37 +0000100 GlobalValue *CounterArray) {
Chris Lattner467dd2e2004-03-08 17:06:13 +0000101 // Insert the increment after any alloca or PHI instructions...
102 BasicBlock::iterator InsertPos = BB->begin();
103 while (isa<AllocaInst>(InsertPos) || isa<PHINode>(InsertPos))
104 ++InsertPos;
105
106 // Create the getelementptr constant expression
107 std::vector<Constant*> Indices(2);
Reid Spencerc5b206b2006-12-31 05:48:39 +0000108 Indices[0] = Constant::getNullValue(Type::Int32Ty);
109 Indices[1] = ConstantInt::get(Type::Int32Ty, CounterNum);
Chris Lattnerec1f7522007-02-19 07:34:47 +0000110 Constant *ElementPtr =
111 ConstantExpr::getGetElementPtr(CounterArray, &Indices[0], Indices.size());
Chris Lattner467dd2e2004-03-08 17:06:13 +0000112
113 // Load, increment and store the value back.
114 Value *OldVal = new LoadInst(ElementPtr, "OldFuncCounter", InsertPos);
115 Value *NewVal = BinaryOperator::create(Instruction::Add, OldVal,
Reid Spencerc5b206b2006-12-31 05:48:39 +0000116 ConstantInt::get(Type::Int32Ty, 1),
Chris Lattner467dd2e2004-03-08 17:06:13 +0000117 "NewFuncCounter", InsertPos);
118 new StoreInst(NewVal, ElementPtr, InsertPos);
119}