blob: 4068392884022c3308a5acb591630190667b2266 [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) {
25 const Type *ArgVTy = PointerType::get(PointerType::get(Type::SByteTy));
Brian Gaekeebbc0e92004-05-03 22:06:33 +000026 const PointerType *UIntPtr = PointerType::get(Type::UIntTy);
Chris Lattner467dd2e2004-03-08 17:06:13 +000027 Module &M = *MainFn->getParent();
28 Function *InitFn = M.getOrInsertFunction(FnName, Type::IntTy, Type::IntTy,
Jeff Cohen66c5fd62005-10-23 04:37:20 +000029 ArgVTy, UIntPtr, Type::UIntTy,
30 (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);
35 Args[0] = Constant::getNullValue(Type::IntTy);
36 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
Chris Lattner28977af2004-04-05 01:30:19 +000043 std::vector<Constant*> GEPIndices(2, Constant::getNullValue(Type::IntTy));
Brian Gaekeebbc0e92004-05-03 22:06:33 +000044 unsigned NumElements = 0;
45 if (Array) {
Reid Spencer518310c2004-07-18 00:44:37 +000046 Args[2] = ConstantExpr::getGetElementPtr(Array, GEPIndices);
Brian Gaekeebbc0e92004-05-03 22:06:33 +000047 NumElements =
48 cast<ArrayType>(Array->getType()->getElementType())->getNumElements();
49 } else {
50 // If this profiling instrumentation doesn't have a constant array, just
51 // pass null.
52 Args[2] = ConstantPointerNull::get(UIntPtr);
53 }
Reid Spencerb83eb642006-10-20 07:07:24 +000054 Args[3] = ConstantInt::get(Type::UIntTy, NumElements);
Misha Brukmanfd939082005-04-21 23:48:37 +000055
Chris Lattner467dd2e2004-03-08 17:06:13 +000056 Instruction *InitCall = new CallInst(InitFn, Args, "newargc", InsertPos);
57
58 // If argc or argv are not available in main, just pass null values in.
Chris Lattnere4d5c442005-03-15 04:54:21 +000059 Function::arg_iterator AI;
60 switch (MainFn->arg_size()) {
Chris Lattner467dd2e2004-03-08 17:06:13 +000061 default:
62 case 2:
Chris Lattnere4d5c442005-03-15 04:54:21 +000063 AI = MainFn->arg_begin(); ++AI;
Chris Lattner467dd2e2004-03-08 17:06:13 +000064 if (AI->getType() != ArgVTy) {
Reid Spencer06e3f4e2006-12-21 07:49:49 +000065 Instruction::CastOps opcode = CastInst::getCastOpcode(AI, false, ArgVTy,
66 false);
Reid Spencer3da59db2006-11-27 01:05:10 +000067 InitCall->setOperand(2,
Reid Spencer8a903db2006-12-18 08:47:13 +000068 CastInst::create(opcode, AI, ArgVTy, "argv.cast", InitCall));
Chris Lattner467dd2e2004-03-08 17:06:13 +000069 } else {
70 InitCall->setOperand(2, AI);
71 }
Reid Spencer7b06bd52006-12-13 00:50:17 +000072 /* FALL THROUGH */
Chris Lattner467dd2e2004-03-08 17:06:13 +000073
74 case 1:
Chris Lattnere4d5c442005-03-15 04:54:21 +000075 AI = MainFn->arg_begin();
Chris Lattner467dd2e2004-03-08 17:06:13 +000076 // If the program looked at argc, have it look at the return value of the
77 // init call instead.
78 if (AI->getType() != Type::IntTy) {
Reid Spencer8a903db2006-12-18 08:47:13 +000079 Instruction::CastOps opcode;
80 if (!AI->use_empty()) {
Reid Spencer06e3f4e2006-12-21 07:49:49 +000081 opcode = CastInst::getCastOpcode(InitCall, true, AI->getType(), true);
Reid Spencer3da59db2006-11-27 01:05:10 +000082 AI->replaceAllUsesWith(
Reid Spencer8a903db2006-12-18 08:47:13 +000083 CastInst::create(opcode, InitCall, AI->getType(), "", InsertPos));
84 }
Reid Spencer06e3f4e2006-12-21 07:49:49 +000085 opcode = CastInst::getCastOpcode(AI, true, Type::IntTy, true);
Reid Spencer3da59db2006-11-27 01:05:10 +000086 InitCall->setOperand(1,
Reid Spencer8a903db2006-12-18 08:47:13 +000087 CastInst::create(opcode, AI, Type::IntTy, "argc.cast", InitCall));
Chris Lattner467dd2e2004-03-08 17:06:13 +000088 } else {
89 AI->replaceAllUsesWith(InitCall);
90 InitCall->setOperand(1, AI);
91 }
Misha Brukmanfd939082005-04-21 23:48:37 +000092
Chris Lattner467dd2e2004-03-08 17:06:13 +000093 case 0: break;
94 }
95}
96
97void llvm::IncrementCounterInBlock(BasicBlock *BB, unsigned CounterNum,
Reid Spencer518310c2004-07-18 00:44:37 +000098 GlobalValue *CounterArray) {
Chris Lattner467dd2e2004-03-08 17:06:13 +000099 // Insert the increment after any alloca or PHI instructions...
100 BasicBlock::iterator InsertPos = BB->begin();
101 while (isa<AllocaInst>(InsertPos) || isa<PHINode>(InsertPos))
102 ++InsertPos;
103
104 // Create the getelementptr constant expression
105 std::vector<Constant*> Indices(2);
Chris Lattner28977af2004-04-05 01:30:19 +0000106 Indices[0] = Constant::getNullValue(Type::IntTy);
Reid Spencerb83eb642006-10-20 07:07:24 +0000107 Indices[1] = ConstantInt::get(Type::IntTy, CounterNum);
Chris Lattner467dd2e2004-03-08 17:06:13 +0000108 Constant *ElementPtr = ConstantExpr::getGetElementPtr(CounterArray, Indices);
109
110 // Load, increment and store the value back.
111 Value *OldVal = new LoadInst(ElementPtr, "OldFuncCounter", InsertPos);
112 Value *NewVal = BinaryOperator::create(Instruction::Add, OldVal,
113 ConstantInt::get(Type::UIntTy, 1),
114 "NewFuncCounter", InsertPos);
115 new StoreInst(NewVal, ElementPtr, InsertPos);
116}