1 //===- ProfilingUtils.cpp - Helper functions shared by profilers ----------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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
15 //===----------------------------------------------------------------------===//
17 #include "ProfilingUtils.h"
18 #include "llvm/Constants.h"
19 #include "llvm/DerivedTypes.h"
20 #include "llvm/Instructions.h"
21 #include "llvm/Module.h"
23 void llvm::InsertProfilingInitCall(Function *MainFn, const char *FnName,
25 const Type *ArgVTy = PointerType::get(PointerType::get(Type::Int8Ty));
26 const PointerType *UIntPtr = PointerType::get(Type::Int32Ty);
27 Module &M = *MainFn->getParent();
28 Constant *InitFn = M.getOrInsertFunction(FnName, Type::Int32Ty, Type::Int32Ty,
29 ArgVTy, UIntPtr, Type::Int32Ty,
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::Int32Ty);
36 Args[1] = Constant::getNullValue(ArgVTy);
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;
43 std::vector<Constant*> GEPIndices(2, Constant::getNullValue(Type::Int32Ty));
44 unsigned NumElements = 0;
46 Args[2] = ConstantExpr::getGetElementPtr(Array, GEPIndices);
48 cast<ArrayType>(Array->getType()->getElementType())->getNumElements();
50 // If this profiling instrumentation doesn't have a constant array, just
52 Args[2] = ConstantPointerNull::get(UIntPtr);
54 Args[3] = ConstantInt::get(Type::Int32Ty, NumElements);
56 Instruction *InitCall = new CallInst(InitFn, &Args[0], Args.size(),
57 "newargc", InsertPos);
59 // If argc or argv are not available in main, just pass null values in.
60 Function::arg_iterator AI;
61 switch (MainFn->arg_size()) {
64 AI = MainFn->arg_begin(); ++AI;
65 if (AI->getType() != ArgVTy) {
66 Instruction::CastOps opcode = CastInst::getCastOpcode(AI, false, ArgVTy,
68 InitCall->setOperand(2,
69 CastInst::create(opcode, AI, ArgVTy, "argv.cast", InitCall));
71 InitCall->setOperand(2, AI);
76 AI = MainFn->arg_begin();
77 // If the program looked at argc, have it look at the return value of the
79 if (AI->getType() != Type::Int32Ty) {
80 Instruction::CastOps opcode;
81 if (!AI->use_empty()) {
82 opcode = CastInst::getCastOpcode(InitCall, true, AI->getType(), true);
83 AI->replaceAllUsesWith(
84 CastInst::create(opcode, InitCall, AI->getType(), "", InsertPos));
86 opcode = CastInst::getCastOpcode(AI, true, Type::Int32Ty, true);
87 InitCall->setOperand(1,
88 CastInst::create(opcode, AI, Type::Int32Ty, "argc.cast", InitCall));
90 AI->replaceAllUsesWith(InitCall);
91 InitCall->setOperand(1, AI);
98 void llvm::IncrementCounterInBlock(BasicBlock *BB, unsigned CounterNum,
99 GlobalValue *CounterArray) {
100 // Insert the increment after any alloca or PHI instructions...
101 BasicBlock::iterator InsertPos = BB->begin();
102 while (isa<AllocaInst>(InsertPos) || isa<PHINode>(InsertPos))
105 // Create the getelementptr constant expression
106 std::vector<Constant*> Indices(2);
107 Indices[0] = Constant::getNullValue(Type::Int32Ty);
108 Indices[1] = ConstantInt::get(Type::Int32Ty, CounterNum);
109 Constant *ElementPtr = ConstantExpr::getGetElementPtr(CounterArray, Indices);
111 // Load, increment and store the value back.
112 Value *OldVal = new LoadInst(ElementPtr, "OldFuncCounter", InsertPos);
113 Value *NewVal = BinaryOperator::create(Instruction::Add, OldVal,
114 ConstantInt::get(Type::Int32Ty, 1),
115 "NewFuncCounter", InsertPos);
116 new StoreInst(NewVal, ElementPtr, InsertPos);