For PR950:
[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 was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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
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
23 void llvm::InsertProfilingInitCall(Function *MainFn, const char *FnName,
24                                    GlobalValue *Array) {
25   const Type *ArgVTy = PointerType::get(PointerType::get(Type::SByteTy));
26   const PointerType *UIntPtr = PointerType::get(Type::UIntTy);
27   Module &M = *MainFn->getParent();
28   Function *InitFn = M.getOrInsertFunction(FnName, Type::IntTy, Type::IntTy,
29                                            ArgVTy, UIntPtr, Type::UIntTy,
30                                            (Type *)0);
31
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::IntTy);
36   Args[1] = Constant::getNullValue(ArgVTy);
37
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;
42
43   std::vector<Constant*> GEPIndices(2, Constant::getNullValue(Type::IntTy));
44   unsigned NumElements = 0;
45   if (Array) {
46     Args[2] = ConstantExpr::getGetElementPtr(Array, GEPIndices);
47     NumElements =
48       cast<ArrayType>(Array->getType()->getElementType())->getNumElements();
49   } else {
50     // If this profiling instrumentation doesn't have a constant array, just
51     // pass null.
52     Args[2] = ConstantPointerNull::get(UIntPtr);
53   }
54   Args[3] = ConstantInt::get(Type::UIntTy, NumElements);
55
56   Instruction *InitCall = new CallInst(InitFn, Args, "newargc", InsertPos);
57
58   // If argc or argv are not available in main, just pass null values in.
59   Function::arg_iterator AI;
60   switch (MainFn->arg_size()) {
61   default:
62   case 2:
63     AI = MainFn->arg_begin(); ++AI;
64     if (AI->getType() != ArgVTy) {
65       InitCall->setOperand(2, new CastInst(AI, ArgVTy, "argv.cast", InitCall));
66     } else {
67       InitCall->setOperand(2, AI);
68     }
69
70   case 1:
71     AI = MainFn->arg_begin();
72     // If the program looked at argc, have it look at the return value of the
73     // init call instead.
74     if (AI->getType() != Type::IntTy) {
75       if (!AI->use_empty())
76         AI->replaceAllUsesWith(new CastInst(InitCall, AI->getType(), "",
77                                             InsertPos));
78       InitCall->setOperand(1, new CastInst(AI, Type::IntTy, "argc.cast",
79                                            InitCall));
80     } else {
81       AI->replaceAllUsesWith(InitCall);
82       InitCall->setOperand(1, AI);
83     }
84
85   case 0: break;
86   }
87 }
88
89 void llvm::IncrementCounterInBlock(BasicBlock *BB, unsigned CounterNum,
90                                    GlobalValue *CounterArray) {
91   // Insert the increment after any alloca or PHI instructions...
92   BasicBlock::iterator InsertPos = BB->begin();
93   while (isa<AllocaInst>(InsertPos) || isa<PHINode>(InsertPos))
94     ++InsertPos;
95
96   // Create the getelementptr constant expression
97   std::vector<Constant*> Indices(2);
98   Indices[0] = Constant::getNullValue(Type::IntTy);
99   Indices[1] = ConstantInt::get(Type::IntTy, CounterNum);
100   Constant *ElementPtr = ConstantExpr::getGetElementPtr(CounterArray, Indices);
101
102   // Load, increment and store the value back.
103   Value *OldVal = new LoadInst(ElementPtr, "OldFuncCounter", InsertPos);
104   Value *NewVal = BinaryOperator::create(Instruction::Add, OldVal,
105                                          ConstantInt::get(Type::UIntTy, 1),
106                                          "NewFuncCounter", InsertPos);
107   new StoreInst(NewVal, ElementPtr, InsertPos);
108 }