blob: dc34bf70a311502ecb9533a792c5a313cb021e46 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- ProfilingUtils.cpp - Helper functions shared by profilers ----------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
Gabor Greif4750dac2008-03-06 10:36:00 +000010// This file implements a few helper functions which are used by profile
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Andersond4d90a02009-07-06 18:42:36 +000021#include "llvm/LLVMContext.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000022#include "llvm/Module.h"
23
24void llvm::InsertProfilingInitCall(Function *MainFn, const char *FnName,
25 GlobalValue *Array) {
Christopher Lambbb2f2222007-12-17 01:12:55 +000026 const Type *ArgVTy =
Owen Anderson6b6e2d92009-07-29 22:17:13 +000027 PointerType::getUnqual(PointerType::getUnqual(Type::Int8Ty));
28 const PointerType *UIntPtr = PointerType::getUnqual(Type::Int32Ty);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000029 Module &M = *MainFn->getParent();
30 Constant *InitFn = M.getOrInsertFunction(FnName, Type::Int32Ty, Type::Int32Ty,
31 ArgVTy, UIntPtr, Type::Int32Ty,
32 (Type *)0);
33
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 Andersonaac28372009-07-31 20:28:14 +000037 Args[0] = Constant::getNullValue(Type::Int32Ty);
38 Args[1] = Constant::getNullValue(ArgVTy);
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Andersonaac28372009-07-31 20:28:14 +000045 std::vector<Constant*> GEPIndices(2, Constant::getNullValue(Type::Int32Ty));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000046 unsigned NumElements = 0;
47 if (Array) {
Owen Anderson02b48c32009-07-29 18:55:55 +000048 Args[2] = ConstantExpr::getGetElementPtr(Array, &GEPIndices[0],
Dan Gohmanf17a25c2007-07-18 16:29:46 +000049 GEPIndices.size());
50 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 Andersonb99ecca2009-07-30 23:03:37 +000055 Args[2] = ConstantPointerNull::get(UIntPtr);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000056 }
Owen Andersoneacb44d2009-07-24 23:12:02 +000057 Args[3] = ConstantInt::get(Type::Int32Ty, NumElements);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000058
Gabor Greifd6da1d02008-04-06 20:25:17 +000059 Instruction *InitCall = CallInst::Create(InitFn, Args.begin(), Args.end(),
60 "newargc", InsertPos);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000061
62 // If argc or argv are not available in main, just pass null values in.
63 Function::arg_iterator AI;
64 switch (MainFn->arg_size()) {
65 default:
66 case 2:
67 AI = MainFn->arg_begin(); ++AI;
68 if (AI->getType() != ArgVTy) {
69 Instruction::CastOps opcode = CastInst::getCastOpcode(AI, false, ArgVTy,
70 false);
71 InitCall->setOperand(2,
Gabor Greifa645dd32008-05-16 19:29:10 +000072 CastInst::Create(opcode, AI, ArgVTy, "argv.cast", InitCall));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000073 } else {
74 InitCall->setOperand(2, AI);
75 }
76 /* FALL THROUGH */
77
78 case 1:
79 AI = MainFn->arg_begin();
80 // If the program looked at argc, have it look at the return value of the
81 // init call instead.
82 if (AI->getType() != Type::Int32Ty) {
83 Instruction::CastOps opcode;
84 if (!AI->use_empty()) {
85 opcode = CastInst::getCastOpcode(InitCall, true, AI->getType(), true);
86 AI->replaceAllUsesWith(
Gabor Greifa645dd32008-05-16 19:29:10 +000087 CastInst::Create(opcode, InitCall, AI->getType(), "", InsertPos));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000088 }
89 opcode = CastInst::getCastOpcode(AI, true, Type::Int32Ty, true);
90 InitCall->setOperand(1,
Gabor Greifa645dd32008-05-16 19:29:10 +000091 CastInst::Create(opcode, AI, Type::Int32Ty, "argc.cast", InitCall));
Dan Gohmanf17a25c2007-07-18 16:29:46 +000092 } else {
93 AI->replaceAllUsesWith(InitCall);
94 InitCall->setOperand(1, AI);
95 }
96
97 case 0: break;
98 }
99}
100
101void llvm::IncrementCounterInBlock(BasicBlock *BB, unsigned CounterNum,
102 GlobalValue *CounterArray) {
103 // Insert the increment after any alloca or PHI instructions...
Dan Gohman514277c2008-05-23 21:05:58 +0000104 BasicBlock::iterator InsertPos = BB->getFirstNonPHI();
105 while (isa<AllocaInst>(InsertPos))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000106 ++InsertPos;
107
108 // Create the getelementptr constant expression
109 std::vector<Constant*> Indices(2);
Owen Andersonaac28372009-07-31 20:28:14 +0000110 Indices[0] = Constant::getNullValue(Type::Int32Ty);
Owen Andersoneacb44d2009-07-24 23:12:02 +0000111 Indices[1] = ConstantInt::get(Type::Int32Ty, CounterNum);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000112 Constant *ElementPtr =
Owen Anderson02b48c32009-07-29 18:55:55 +0000113 ConstantExpr::getGetElementPtr(CounterArray, &Indices[0],
Owen Andersond4d90a02009-07-06 18:42:36 +0000114 Indices.size());
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000115
116 // Load, increment and store the value back.
117 Value *OldVal = new LoadInst(ElementPtr, "OldFuncCounter", InsertPos);
Gabor Greifa645dd32008-05-16 19:29:10 +0000118 Value *NewVal = BinaryOperator::Create(Instruction::Add, OldVal,
Owen Andersoneacb44d2009-07-24 23:12:02 +0000119 ConstantInt::get(Type::Int32Ty, 1),
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000120 "NewFuncCounter", InsertPos);
121 new StoreInst(NewVal, ElementPtr, InsertPos);
122}