blob: b43ca0a4c5cb904274cbab08b28d83693b22205c [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) {
Owen Andersone922c022009-07-22 00:24:57 +000026 LLVMContext &Context = MainFn->getContext();
Christopher Lamb43ad6b32007-12-17 01:12:55 +000027 const Type *ArgVTy =
Owen Andersone922c022009-07-22 00:24:57 +000028 Context.getPointerTypeUnqual(Context.getPointerTypeUnqual(Type::Int8Ty));
29 const PointerType *UIntPtr = Context.getPointerTypeUnqual(Type::Int32Ty);
Chris Lattner467dd2e2004-03-08 17:06:13 +000030 Module &M = *MainFn->getParent();
Chris Lattnerfebe5f12007-01-07 07:22:20 +000031 Constant *InitFn = M.getOrInsertFunction(FnName, Type::Int32Ty, Type::Int32Ty,
Reid Spencerc5b206b2006-12-31 05:48:39 +000032 ArgVTy, UIntPtr, Type::Int32Ty,
Jeff Cohen66c5fd62005-10-23 04:37:20 +000033 (Type *)0);
Chris Lattner467dd2e2004-03-08 17:06:13 +000034
35 // This could force argc and argv into programs that wouldn't otherwise have
36 // them, but instead we just pass null values in.
37 std::vector<Value*> Args(4);
Owen Andersone922c022009-07-22 00:24:57 +000038 Args[0] = Context.getNullValue(Type::Int32Ty);
39 Args[1] = Context.getNullValue(ArgVTy);
Chris Lattner467dd2e2004-03-08 17:06:13 +000040
41 // Skip over any allocas in the entry block.
42 BasicBlock *Entry = MainFn->begin();
43 BasicBlock::iterator InsertPos = Entry->begin();
44 while (isa<AllocaInst>(InsertPos)) ++InsertPos;
45
Owen Andersone922c022009-07-22 00:24:57 +000046 std::vector<Constant*> GEPIndices(2, Context.getNullValue(Type::Int32Ty));
Brian Gaekeebbc0e92004-05-03 22:06:33 +000047 unsigned NumElements = 0;
48 if (Array) {
Owen Andersone922c022009-07-22 00:24:57 +000049 Args[2] = Context.getConstantExprGetElementPtr(Array, &GEPIndices[0],
Chris Lattnerec1f7522007-02-19 07:34:47 +000050 GEPIndices.size());
Brian Gaekeebbc0e92004-05-03 22:06:33 +000051 NumElements =
52 cast<ArrayType>(Array->getType()->getElementType())->getNumElements();
53 } else {
54 // If this profiling instrumentation doesn't have a constant array, just
55 // pass null.
Owen Andersone922c022009-07-22 00:24:57 +000056 Args[2] = Context.getConstantPointerNull(UIntPtr);
Brian Gaekeebbc0e92004-05-03 22:06:33 +000057 }
Owen Andersone922c022009-07-22 00:24:57 +000058 Args[3] = Context.getConstantInt(Type::Int32Ty, NumElements);
Misha Brukmanfd939082005-04-21 23:48:37 +000059
Gabor Greif051a9502008-04-06 20:25:17 +000060 Instruction *InitCall = CallInst::Create(InitFn, Args.begin(), Args.end(),
61 "newargc", InsertPos);
Chris Lattner467dd2e2004-03-08 17:06:13 +000062
63 // If argc or argv are not available in main, just pass null values in.
Chris Lattnere4d5c442005-03-15 04:54:21 +000064 Function::arg_iterator AI;
65 switch (MainFn->arg_size()) {
Chris Lattner467dd2e2004-03-08 17:06:13 +000066 default:
67 case 2:
Chris Lattnere4d5c442005-03-15 04:54:21 +000068 AI = MainFn->arg_begin(); ++AI;
Chris Lattner467dd2e2004-03-08 17:06:13 +000069 if (AI->getType() != ArgVTy) {
Reid Spencer06e3f4e2006-12-21 07:49:49 +000070 Instruction::CastOps opcode = CastInst::getCastOpcode(AI, false, ArgVTy,
71 false);
Reid Spencer3da59db2006-11-27 01:05:10 +000072 InitCall->setOperand(2,
Gabor Greif7cbd8a32008-05-16 19:29:10 +000073 CastInst::Create(opcode, AI, ArgVTy, "argv.cast", InitCall));
Chris Lattner467dd2e2004-03-08 17:06:13 +000074 } else {
75 InitCall->setOperand(2, AI);
76 }
Reid Spencer7b06bd52006-12-13 00:50:17 +000077 /* FALL THROUGH */
Chris Lattner467dd2e2004-03-08 17:06:13 +000078
79 case 1:
Chris Lattnere4d5c442005-03-15 04:54:21 +000080 AI = MainFn->arg_begin();
Chris Lattner467dd2e2004-03-08 17:06:13 +000081 // If the program looked at argc, have it look at the return value of the
82 // init call instead.
Reid Spencerc5b206b2006-12-31 05:48:39 +000083 if (AI->getType() != Type::Int32Ty) {
Reid Spencer8a903db2006-12-18 08:47:13 +000084 Instruction::CastOps opcode;
85 if (!AI->use_empty()) {
Reid Spencer06e3f4e2006-12-21 07:49:49 +000086 opcode = CastInst::getCastOpcode(InitCall, true, AI->getType(), true);
Reid Spencer3da59db2006-11-27 01:05:10 +000087 AI->replaceAllUsesWith(
Gabor Greif7cbd8a32008-05-16 19:29:10 +000088 CastInst::Create(opcode, InitCall, AI->getType(), "", InsertPos));
Reid Spencer8a903db2006-12-18 08:47:13 +000089 }
Reid Spencerc5b206b2006-12-31 05:48:39 +000090 opcode = CastInst::getCastOpcode(AI, true, Type::Int32Ty, true);
Reid Spencer3da59db2006-11-27 01:05:10 +000091 InitCall->setOperand(1,
Gabor Greif7cbd8a32008-05-16 19:29:10 +000092 CastInst::Create(opcode, AI, Type::Int32Ty, "argc.cast", InitCall));
Chris Lattner467dd2e2004-03-08 17:06:13 +000093 } else {
94 AI->replaceAllUsesWith(InitCall);
95 InitCall->setOperand(1, AI);
96 }
Misha Brukmanfd939082005-04-21 23:48:37 +000097
Chris Lattner467dd2e2004-03-08 17:06:13 +000098 case 0: break;
99 }
100}
101
102void llvm::IncrementCounterInBlock(BasicBlock *BB, unsigned CounterNum,
Reid Spencer518310c2004-07-18 00:44:37 +0000103 GlobalValue *CounterArray) {
Owen Andersone922c022009-07-22 00:24:57 +0000104 LLVMContext &Context = BB->getContext();
Owen Anderson50895512009-07-06 18:42:36 +0000105
Chris Lattner467dd2e2004-03-08 17:06:13 +0000106 // Insert the increment after any alloca or PHI instructions...
Dan Gohman02dea8b2008-05-23 21:05:58 +0000107 BasicBlock::iterator InsertPos = BB->getFirstNonPHI();
108 while (isa<AllocaInst>(InsertPos))
Chris Lattner467dd2e2004-03-08 17:06:13 +0000109 ++InsertPos;
110
111 // Create the getelementptr constant expression
112 std::vector<Constant*> Indices(2);
Owen Andersone922c022009-07-22 00:24:57 +0000113 Indices[0] = Context.getNullValue(Type::Int32Ty);
114 Indices[1] = Context.getConstantInt(Type::Int32Ty, CounterNum);
Chris Lattnerec1f7522007-02-19 07:34:47 +0000115 Constant *ElementPtr =
Owen Andersone922c022009-07-22 00:24:57 +0000116 Context.getConstantExprGetElementPtr(CounterArray, &Indices[0],
Owen Anderson50895512009-07-06 18:42:36 +0000117 Indices.size());
Chris Lattner467dd2e2004-03-08 17:06:13 +0000118
119 // Load, increment and store the value back.
120 Value *OldVal = new LoadInst(ElementPtr, "OldFuncCounter", InsertPos);
Gabor Greif7cbd8a32008-05-16 19:29:10 +0000121 Value *NewVal = BinaryOperator::Create(Instruction::Add, OldVal,
Owen Andersone922c022009-07-22 00:24:57 +0000122 Context.getConstantInt(Type::Int32Ty, 1),
Chris Lattner467dd2e2004-03-08 17:06:13 +0000123 "NewFuncCounter", InsertPos);
124 new StoreInst(NewVal, ElementPtr, InsertPos);
125}