1 //===- ProfilingUtils.cpp - Helper functions shared by profilers ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file 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/LLVMContext.h"
22 #include "llvm/Module.h"
24 void llvm::InsertProfilingInitCall(Function *MainFn, const char *FnName,
27 PointerType::getUnqual(PointerType::getUnqual(Type::Int8Ty));
28 const PointerType *UIntPtr = PointerType::getUnqual(Type::Int32Ty);
29 Module &M = *MainFn->getParent();
30 Constant *InitFn = M.getOrInsertFunction(FnName, Type::Int32Ty, Type::Int32Ty,
31 ArgVTy, UIntPtr, Type::Int32Ty,
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);
37 Args[0] = Constant::getNullValue(Type::Int32Ty);
38 Args[1] = Constant::getNullValue(ArgVTy);
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;
45 std::vector<Constant*> GEPIndices(2, Constant::getNullValue(Type::Int32Ty));
46 unsigned NumElements = 0;
48 Args[2] = ConstantExpr::getGetElementPtr(Array, &GEPIndices[0],
51 cast<ArrayType>(Array->getType()->getElementType())->getNumElements();
53 // If this profiling instrumentation doesn't have a constant array, just
55 Args[2] = ConstantPointerNull::get(UIntPtr);
57 Args[3] = ConstantInt::get(Type::Int32Ty, NumElements);
59 Instruction *InitCall = CallInst::Create(InitFn, Args.begin(), Args.end(),
60 "newargc", InsertPos);
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()) {
67 AI = MainFn->arg_begin(); ++AI;
68 if (AI->getType() != ArgVTy) {
69 Instruction::CastOps opcode = CastInst::getCastOpcode(AI, false, ArgVTy,
71 InitCall->setOperand(2,
72 CastInst::Create(opcode, AI, ArgVTy, "argv.cast", InitCall));
74 InitCall->setOperand(2, AI);
79 AI = MainFn->arg_begin();
80 // If the program looked at argc, have it look at the return value of the
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(
87 CastInst::Create(opcode, InitCall, AI->getType(), "", InsertPos));
89 opcode = CastInst::getCastOpcode(AI, true, Type::Int32Ty, true);
90 InitCall->setOperand(1,
91 CastInst::Create(opcode, AI, Type::Int32Ty, "argc.cast", InitCall));
93 AI->replaceAllUsesWith(InitCall);
94 InitCall->setOperand(1, AI);
101 void llvm::IncrementCounterInBlock(BasicBlock *BB, unsigned CounterNum,
102 GlobalValue *CounterArray) {
103 // Insert the increment after any alloca or PHI instructions...
104 BasicBlock::iterator InsertPos = BB->getFirstNonPHI();
105 while (isa<AllocaInst>(InsertPos))
108 // Create the getelementptr constant expression
109 std::vector<Constant*> Indices(2);
110 Indices[0] = Constant::getNullValue(Type::Int32Ty);
111 Indices[1] = ConstantInt::get(Type::Int32Ty, CounterNum);
112 Constant *ElementPtr =
113 ConstantExpr::getGetElementPtr(CounterArray, &Indices[0],
116 // Load, increment and store the value back.
117 Value *OldVal = new LoadInst(ElementPtr, "OldFuncCounter", InsertPos);
118 Value *NewVal = BinaryOperator::Create(Instruction::Add, OldVal,
119 ConstantInt::get(Type::Int32Ty, 1),
120 "NewFuncCounter", InsertPos);
121 new StoreInst(NewVal, ElementPtr, InsertPos);