ProfilingUtils.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. #include "ProfilingUtils.h"
  17. #include "llvm/Constants.h"
  18. #include "llvm/DerivedTypes.h"
  19. #include "llvm/Instructions.h"
  20. #include "llvm/LLVMContext.h"
  21. #include "llvm/Module.h"
  22. void llvm::InsertProfilingInitCall(Function *MainFn, const char *FnName,
  23. GlobalValue *Array,
  24. PointerType *arrayType) {
  25. LLVMContext &Context = MainFn->getContext();
  26. const Type *ArgVTy =
  27. PointerType::getUnqual(Type::getInt8PtrTy(Context));
  28. const PointerType *UIntPtr = arrayType ? arrayType :
  29. Type::getInt32PtrTy(Context);
  30. Module &M = *MainFn->getParent();
  31. Constant *InitFn = M.getOrInsertFunction(FnName, Type::getInt32Ty(Context),
  32. Type::getInt32Ty(Context),
  33. ArgVTy, UIntPtr,
  34. Type::getInt32Ty(Context),
  35. (Type *)0);
  36. // This could force argc and argv into programs that wouldn't otherwise have
  37. // them, but instead we just pass null values in.
  38. std::vector<Value*> Args(4);
  39. Args[0] = Constant::getNullValue(Type::getInt32Ty(Context));
  40. Args[1] = Constant::getNullValue(ArgVTy);
  41. // Skip over any allocas in the entry block.
  42. BasicBlock *Entry = MainFn->begin();
  43. BasicBlock::iterator InsertPos = Entry->begin();
  44. while (isa<AllocaInst>(InsertPos)) ++InsertPos;
  45. std::vector<Constant*> GEPIndices(2,
  46. Constant::getNullValue(Type::getInt32Ty(Context)));
  47. unsigned NumElements = 0;
  48. if (Array) {
  49. Args[2] = ConstantExpr::getGetElementPtr(Array, &GEPIndices[0],
  50. GEPIndices.size());
  51. NumElements =
  52. cast<ArrayType>(Array->getType()->getElementType())->getNumElements();
  53. } else {
  54. // If this profiling instrumentation doesn't have a constant array, just
  55. // pass null.
  56. Args[2] = ConstantPointerNull::get(UIntPtr);
  57. }
  58. Args[3] = ConstantInt::get(Type::getInt32Ty(Context), NumElements);
  59. CallInst *InitCall = CallInst::Create(InitFn, Args.begin(), Args.end(),
  60. "newargc", InsertPos);
  61. // If argc or argv are not available in main, just pass null values in.
  62. Function::arg_iterator AI;
  63. switch (MainFn->arg_size()) {
  64. default:
  65. case 2:
  66. AI = MainFn->arg_begin(); ++AI;
  67. if (AI->getType() != ArgVTy) {
  68. Instruction::CastOps opcode = CastInst::getCastOpcode(AI, false, ArgVTy,
  69. false);
  70. InitCall->setArgOperand(1,
  71. CastInst::Create(opcode, AI, ArgVTy, "argv.cast", InitCall));
  72. } else {
  73. InitCall->setArgOperand(1, AI);
  74. }
  75. /* FALL THROUGH */
  76. case 1:
  77. AI = MainFn->arg_begin();
  78. // If the program looked at argc, have it look at the return value of the
  79. // init call instead.
  80. if (!AI->getType()->isIntegerTy(32)) {
  81. Instruction::CastOps opcode;
  82. if (!AI->use_empty()) {
  83. opcode = CastInst::getCastOpcode(InitCall, true, AI->getType(), true);
  84. AI->replaceAllUsesWith(
  85. CastInst::Create(opcode, InitCall, AI->getType(), "", InsertPos));
  86. }
  87. opcode = CastInst::getCastOpcode(AI, true,
  88. Type::getInt32Ty(Context), true);
  89. InitCall->setArgOperand(0,
  90. CastInst::Create(opcode, AI, Type::getInt32Ty(Context),
  91. "argc.cast", InitCall));
  92. } else {
  93. AI->replaceAllUsesWith(InitCall);
  94. InitCall->setArgOperand(0, AI);
  95. }
  96. case 0: break;
  97. }
  98. }
  99. void llvm::IncrementCounterInBlock(BasicBlock *BB, unsigned CounterNum,
  100. GlobalValue *CounterArray, bool beginning) {
  101. // Insert the increment after any alloca or PHI instructions...
  102. BasicBlock::iterator InsertPos = beginning ? BB->getFirstNonPHI() :
  103. BB->getTerminator();
  104. while (isa<AllocaInst>(InsertPos))
  105. ++InsertPos;
  106. LLVMContext &Context = BB->getContext();
  107. // Create the getelementptr constant expression
  108. std::vector<Constant*> Indices(2);
  109. Indices[0] = Constant::getNullValue(Type::getInt32Ty(Context));
  110. Indices[1] = ConstantInt::get(Type::getInt32Ty(Context), CounterNum);
  111. Constant *ElementPtr =
  112. ConstantExpr::getGetElementPtr(CounterArray, &Indices[0], Indices.size());
  113. // Load, increment and store the value back.
  114. Value *OldVal = new LoadInst(ElementPtr, "OldFuncCounter", InsertPos);
  115. Value *NewVal = BinaryOperator::Create(Instruction::Add, OldVal,
  116. ConstantInt::get(Type::getInt32Ty(Context), 1),
  117. "NewFuncCounter", InsertPos);
  118. new StoreInst(NewVal, ElementPtr, InsertPos);
  119. }
  120. void llvm::InsertProfilingShutdownCall(Function *Callee, Module *Mod) {
  121. // llvm.global_dtors is an array of type { i32, void ()* }. Prepare those
  122. // types.
  123. const Type *GlobalDtorElems[2] = {
  124. Type::getInt32Ty(Mod->getContext()),
  125. FunctionType::get(Type::getVoidTy(Mod->getContext()), false)->getPointerTo()
  126. };
  127. const StructType *GlobalDtorElemTy =
  128. StructType::get(Mod->getContext(), GlobalDtorElems, false);
  129. // Construct the new element we'll be adding.
  130. Constant *Elem[2] = {
  131. ConstantInt::get(Type::getInt32Ty(Mod->getContext()), 65535),
  132. ConstantExpr::getBitCast(Callee, GlobalDtorElems[1])
  133. };
  134. // If llvm.global_dtors exists, make a copy of the things in its list and
  135. // delete it, to replace it with one that has a larger array type.
  136. std::vector<Constant *> dtors;
  137. if (GlobalVariable *GlobalDtors = Mod->getNamedGlobal("llvm.global_dtors")) {
  138. if (ConstantArray *InitList =
  139. dyn_cast<ConstantArray>(GlobalDtors->getInitializer())) {
  140. for (unsigned i = 0, e = InitList->getType()->getNumElements();
  141. i != e; ++i)
  142. dtors.push_back(cast<Constant>(InitList->getOperand(i)));
  143. }
  144. GlobalDtors->eraseFromParent();
  145. }
  146. // Build up llvm.global_dtors with our new item in it.
  147. GlobalVariable *GlobalDtors = new GlobalVariable(
  148. *Mod, ArrayType::get(GlobalDtorElemTy, 1), false,
  149. GlobalValue::AppendingLinkage, NULL, "llvm.global_dtors");
  150. dtors.push_back(ConstantStruct::get(GlobalDtorElemTy, Elem));
  151. GlobalDtors->setInitializer(ConstantArray::get(
  152. cast<ArrayType>(GlobalDtors->getType()->getElementType()), dtors));
  153. }