ObjCARC.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. //===-- ObjCARC.cpp -------------------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements common infrastructure for libLLVMObjCARCOpts.a, which
  10. // implements several scalar transformations over the LLVM intermediate
  11. // representation, including the C bindings for that library.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "ObjCARC.h"
  15. #include "llvm/Analysis/ObjCARCUtil.h"
  16. #include "llvm/IR/IRBuilder.h"
  17. #include "llvm/IR/Instructions.h"
  18. #include "llvm/Transforms/Utils/BasicBlockUtils.h"
  19. using namespace llvm;
  20. using namespace llvm::objcarc;
  21. CallInst *objcarc::createCallInstWithColors(
  22. FunctionCallee Func, ArrayRef<Value *> Args, const Twine &NameStr,
  23. Instruction *InsertBefore,
  24. const DenseMap<BasicBlock *, ColorVector> &BlockColors) {
  25. FunctionType *FTy = Func.getFunctionType();
  26. Value *Callee = Func.getCallee();
  27. SmallVector<OperandBundleDef, 1> OpBundles;
  28. if (!BlockColors.empty()) {
  29. const ColorVector &CV = BlockColors.find(InsertBefore->getParent())->second;
  30. assert(CV.size() == 1 && "non-unique color for block!");
  31. Instruction *EHPad = CV.front()->getFirstNonPHI();
  32. if (EHPad->isEHPad())
  33. OpBundles.emplace_back("funclet", EHPad);
  34. }
  35. return CallInst::Create(FTy, Callee, Args, OpBundles, NameStr, InsertBefore);
  36. }
  37. std::pair<bool, bool>
  38. BundledRetainClaimRVs::insertAfterInvokes(Function &F, DominatorTree *DT) {
  39. bool Changed = false, CFGChanged = false;
  40. for (BasicBlock &BB : F) {
  41. auto *I = dyn_cast<InvokeInst>(BB.getTerminator());
  42. if (!I)
  43. continue;
  44. if (!objcarc::hasAttachedCallOpBundle(I))
  45. continue;
  46. BasicBlock *DestBB = I->getNormalDest();
  47. if (!DestBB->getSinglePredecessor()) {
  48. assert(I->getSuccessor(0) == DestBB &&
  49. "the normal dest is expected to be the first successor");
  50. DestBB = SplitCriticalEdge(I, 0, CriticalEdgeSplittingOptions(DT));
  51. CFGChanged = true;
  52. }
  53. // We don't have to call insertRVCallWithColors since DestBB is the normal
  54. // destination of the invoke.
  55. insertRVCall(&*DestBB->getFirstInsertionPt(), I);
  56. Changed = true;
  57. }
  58. return std::make_pair(Changed, CFGChanged);
  59. }
  60. CallInst *BundledRetainClaimRVs::insertRVCall(Instruction *InsertPt,
  61. CallBase *AnnotatedCall) {
  62. DenseMap<BasicBlock *, ColorVector> BlockColors;
  63. return insertRVCallWithColors(InsertPt, AnnotatedCall, BlockColors);
  64. }
  65. CallInst *BundledRetainClaimRVs::insertRVCallWithColors(
  66. Instruction *InsertPt, CallBase *AnnotatedCall,
  67. const DenseMap<BasicBlock *, ColorVector> &BlockColors) {
  68. IRBuilder<> Builder(InsertPt);
  69. Function *Func = *objcarc::getAttachedARCFunction(AnnotatedCall);
  70. assert(Func && "operand isn't a Function");
  71. Type *ParamTy = Func->getArg(0)->getType();
  72. Value *CallArg = Builder.CreateBitCast(AnnotatedCall, ParamTy);
  73. auto *Call =
  74. createCallInstWithColors(Func, CallArg, "", InsertPt, BlockColors);
  75. RVCalls[Call] = AnnotatedCall;
  76. return Call;
  77. }
  78. BundledRetainClaimRVs::~BundledRetainClaimRVs() {
  79. for (auto P : RVCalls) {
  80. if (ContractPass) {
  81. CallBase *CB = P.second;
  82. // At this point, we know that the annotated calls can't be tail calls
  83. // as they are followed by marker instructions and retainRV/claimRV
  84. // calls. Mark them as notail so that the backend knows these calls
  85. // can't be tail calls.
  86. if (auto *CI = dyn_cast<CallInst>(CB))
  87. CI->setTailCallKind(CallInst::TCK_NoTail);
  88. }
  89. EraseInstruction(P.first);
  90. }
  91. RVCalls.clear();
  92. }