blob: 4617fbbd809c319529c2492b6f795b5f281218e4 [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"
21#include "llvm/Module.h"
22
23void llvm::InsertProfilingInitCall(Function *MainFn, const char *FnName,
24 GlobalValue *Array) {
Christopher Lamb43ad6b32007-12-17 01:12:55 +000025 const Type *ArgVTy =
26 PointerType::getUnqual(PointerType::getUnqual(Type::Int8Ty));
27 const PointerType *UIntPtr = PointerType::getUnqual(Type::Int32Ty);
Chris Lattner467dd2e2004-03-08 17:06:13 +000028 Module &M = *MainFn->getParent();
Chris Lattnerfebe5f12007-01-07 07:22:20 +000029 Constant *InitFn = M.getOrInsertFunction(FnName, Type::Int32Ty, Type::Int32Ty,
Reid Spencerc5b206b2006-12-31 05:48:39 +000030 ArgVTy, UIntPtr, Type::Int32Ty,
Jeff Cohen66c5fd62005-10-23 04:37:20 +000031 (Type *)0);
Chris Lattner467dd2e2004-03-08 17:06:13 +000032
33 // This could force argc and argv into programs that wouldn't otherwise have
34 // them, but instead we just pass null values in.
35 std::vector<Value*> Args(4);
Reid Spencerc5b206b2006-12-31 05:48:39 +000036 Args[0] = Constant::getNullValue(Type::Int32Ty);
Chris Lattner467dd2e2004-03-08 17:06:13 +000037 Args[1] = Constant::getNullValue(ArgVTy);
38
39 // Skip over any allocas in the entry block.
40 BasicBlock *Entry = MainFn->begin();
41 BasicBlock::iterator InsertPos = Entry->begin();
42 while (isa<AllocaInst>(InsertPos)) ++InsertPos;
43
Reid Spencerc5b206b2006-12-31 05:48:39 +000044 std::vector<Constant*> GEPIndices(2, Constant::getNullValue(Type::Int32Ty));
Brian Gaekeebbc0e92004-05-03 22:06:33 +000045 unsigned NumElements = 0;
46 if (Array) {
Chris Lattnerec1f7522007-02-19 07:34:47 +000047 Args[2] = ConstantExpr::getGetElementPtr(Array, &GEPIndices[0],
48 GEPIndices.size());
Brian Gaekeebbc0e92004-05-03 22:06:33 +000049 NumElements =
50 cast<ArrayType>(Array->getType()->getElementType())->getNumElements();
51 } else {
52 // If this profiling instrumentation doesn't have a constant array, just
53 // pass null.
54 Args[2] = ConstantPointerNull::get(UIntPtr);
55 }
Reid Spencerc5b206b2006-12-31 05:48:39 +000056 Args[3] = ConstantInt::get(Type::Int32Ty, NumElements);
Misha Brukmanfd939082005-04-21 23:48:37 +000057
Gabor Greif051a9502008-04-06 20:25:17 +000058 Instruction *InitCall = CallInst::Create(InitFn, Args.begin(), Args.end(),
59 "newargc", InsertPos);
Chris Lattner467dd2e2004-03-08 17:06:13 +000060
61 // If argc or argv are not available in main, just pass null values in.
Chris Lattnere4d5c442005-03-15 04:54:21 +000062 Function::arg_iterator AI;
63 switch (MainFn->arg_size()) {
Chris Lattner467dd2e2004-03-08 17:06:13 +000064 default:
65 case 2:
Chris Lattnere4d5c442005-03-15 04:54:21 +000066 AI = MainFn->arg_begin(); ++AI;
Chris Lattner467dd2e2004-03-08 17:06:13 +000067 if (AI->getType() != ArgVTy) {
Reid Spencer06e3f4e2006-12-21 07:49:49 +000068 Instruction::CastOps opcode = CastInst::getCastOpcode(AI, false, ArgVTy,
69 false);
Reid Spencer3da59db2006-11-27 01:05:10 +000070 InitCall->setOperand(2,
Reid Spencer8a903db2006-12-18 08:47:13 +000071 CastInst::create(opcode, AI, ArgVTy, "argv.cast", InitCall));
Chris Lattner467dd2e2004-03-08 17:06:13 +000072 } else {
73 InitCall->setOperand(2, AI);
74 }
Reid Spencer7b06bd52006-12-13 00:50:17 +000075 /* FALL THROUGH */
Chris Lattner467dd2e2004-03-08 17:06:13 +000076
77 case 1:
Chris Lattnere4d5c442005-03-15 04:54:21 +000078 AI = MainFn->arg_begin();
Chris Lattner467dd2e2004-03-08 17:06:13 +000079 // If the program looked at argc, have it look at the return value of the
80 // init call instead.
Reid Spencerc5b206b2006-12-31 05:48:39 +000081 if (AI->getType() != Type::Int32Ty) {
Reid Spencer8a903db2006-12-18 08:47:13 +000082 Instruction::CastOps opcode;
83 if (!AI->use_empty()) {
Reid Spencer06e3f4e2006-12-21 07:49:49 +000084 opcode = CastInst::getCastOpcode(InitCall, true, AI->getType(), true);
Reid Spencer3da59db2006-11-27 01:05:10 +000085 AI->replaceAllUsesWith(
Reid Spencer8a903db2006-12-18 08:47:13 +000086 CastInst::create(opcode, InitCall, AI->getType(), "", InsertPos));
87 }
Reid Spencerc5b206b2006-12-31 05:48:39 +000088 opcode = CastInst::getCastOpcode(AI, true, Type::Int32Ty, true);
Reid Spencer3da59db2006-11-27 01:05:10 +000089 InitCall->setOperand(1,
Reid Spencerc5b206b2006-12-31 05:48:39 +000090 CastInst::create(opcode, AI, Type::Int32Ty, "argc.cast", InitCall));
Chris Lattner467dd2e2004-03-08 17:06:13 +000091 } else {
92 AI->replaceAllUsesWith(InitCall);
93 InitCall->setOperand(1, AI);
94 }
Misha Brukmanfd939082005-04-21 23:48:37 +000095
Chris Lattner467dd2e2004-03-08 17:06:13 +000096 case 0: break;
97 }
98}
99
100void llvm::IncrementCounterInBlock(BasicBlock *BB, unsigned CounterNum,
Reid Spencer518310c2004-07-18 00:44:37 +0000101 GlobalValue *CounterArray) {
Chris Lattner467dd2e2004-03-08 17:06:13 +0000102 // Insert the increment after any alloca or PHI instructions...
103 BasicBlock::iterator InsertPos = BB->begin();
104 while (isa<AllocaInst>(InsertPos) || isa<PHINode>(InsertPos))
105 ++InsertPos;
106
107 // Create the getelementptr constant expression
108 std::vector<Constant*> Indices(2);
Reid Spencerc5b206b2006-12-31 05:48:39 +0000109 Indices[0] = Constant::getNullValue(Type::Int32Ty);
110 Indices[1] = ConstantInt::get(Type::Int32Ty, CounterNum);
Chris Lattnerec1f7522007-02-19 07:34:47 +0000111 Constant *ElementPtr =
112 ConstantExpr::getGetElementPtr(CounterArray, &Indices[0], Indices.size());
Chris Lattner467dd2e2004-03-08 17:06:13 +0000113
114 // Load, increment and store the value back.
115 Value *OldVal = new LoadInst(ElementPtr, "OldFuncCounter", InsertPos);
116 Value *NewVal = BinaryOperator::create(Instruction::Add, OldVal,
Reid Spencerc5b206b2006-12-31 05:48:39 +0000117 ConstantInt::get(Type::Int32Ty, 1),
Chris Lattner467dd2e2004-03-08 17:06:13 +0000118 "NewFuncCounter", InsertPos);
119 new StoreInst(NewVal, ElementPtr, InsertPos);
120}