dc34bf70a311502ecb9533a792c5a313cb021e46
[oota-llvm.git] / lib / Transforms / Instrumentation / ProfilingUtils.cpp
1 //===- ProfilingUtils.cpp - Helper functions shared by profilers ----------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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
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/LLVMContext.h"
22 #include "llvm/Module.h"
23
24 void llvm::InsertProfilingInitCall(Function *MainFn, const char *FnName,
25                                    GlobalValue *Array) {
26   const Type *ArgVTy = 
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,
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);
37   Args[0] = Constant::getNullValue(Type::Int32Ty);
38   Args[1] = Constant::getNullValue(ArgVTy);
39
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
45   std::vector<Constant*> GEPIndices(2, Constant::getNullValue(Type::Int32Ty));
46   unsigned NumElements = 0;
47   if (Array) {
48     Args[2] = ConstantExpr::getGetElementPtr(Array, &GEPIndices[0],
49                                              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.
55     Args[2] = ConstantPointerNull::get(UIntPtr);
56   }
57   Args[3] = ConstantInt::get(Type::Int32Ty, NumElements);
58
59   Instruction *InitCall = CallInst::Create(InitFn, Args.begin(), Args.end(),
60                                            "newargc", InsertPos);
61
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, 
72           CastInst::Create(opcode, AI, ArgVTy, "argv.cast", InitCall));
73     } 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(
87           CastInst::Create(opcode, InitCall, AI->getType(), "", InsertPos));
88       }
89       opcode = CastInst::getCastOpcode(AI, true, Type::Int32Ty, true);
90       InitCall->setOperand(1, 
91           CastInst::Create(opcode, AI, Type::Int32Ty, "argc.cast", InitCall));
92     } else {
93       AI->replaceAllUsesWith(InitCall);
94       InitCall->setOperand(1, AI);
95     }
96
97   case 0: break;
98   }
99 }
100
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))
106     ++InsertPos;
107
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],
114                                           Indices.size());
115
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);
122 }