ObjCARC.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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-c/Initialization.h"
  16. #include "llvm/Analysis/ObjCARCUtil.h"
  17. #include "llvm/IR/IRBuilder.h"
  18. #include "llvm/IR/InlineAsm.h"
  19. #include "llvm/IR/Instructions.h"
  20. #include "llvm/InitializePasses.h"
  21. #include "llvm/Transforms/Utils/BasicBlockUtils.h"
  22. namespace llvm {
  23. class PassRegistry;
  24. }
  25. using namespace llvm;
  26. using namespace llvm::objcarc;
  27. /// initializeObjCARCOptsPasses - Initialize all passes linked into the
  28. /// ObjCARCOpts library.
  29. void llvm::initializeObjCARCOpts(PassRegistry &Registry) {
  30. initializeObjCARCAAWrapperPassPass(Registry);
  31. initializeObjCARCAPElimPass(Registry);
  32. initializeObjCARCExpandPass(Registry);
  33. initializeObjCARCContractLegacyPassPass(Registry);
  34. initializeObjCARCOptLegacyPassPass(Registry);
  35. initializePAEvalPass(Registry);
  36. }
  37. void LLVMInitializeObjCARCOpts(LLVMPassRegistryRef R) {
  38. initializeObjCARCOpts(*unwrap(R));
  39. }
  40. CallInst *objcarc::createCallInstWithColors(
  41. FunctionCallee Func, ArrayRef<Value *> Args, const Twine &NameStr,
  42. Instruction *InsertBefore,
  43. const DenseMap<BasicBlock *, ColorVector> &BlockColors) {
  44. FunctionType *FTy = Func.getFunctionType();
  45. Value *Callee = Func.getCallee();
  46. SmallVector<OperandBundleDef, 1> OpBundles;
  47. if (!BlockColors.empty()) {
  48. const ColorVector &CV = BlockColors.find(InsertBefore->getParent())->second;
  49. assert(CV.size() == 1 && "non-unique color for block!");
  50. Instruction *EHPad = CV.front()->getFirstNonPHI();
  51. if (EHPad->isEHPad())
  52. OpBundles.emplace_back("funclet", EHPad);
  53. }
  54. return CallInst::Create(FTy, Callee, Args, OpBundles, NameStr, InsertBefore);
  55. }
  56. std::pair<bool, bool>
  57. BundledRetainClaimRVs::insertAfterInvokes(Function &F, DominatorTree *DT) {
  58. bool Changed = false, CFGChanged = false;
  59. for (BasicBlock &BB : F) {
  60. auto *I = dyn_cast<InvokeInst>(BB.getTerminator());
  61. if (!I)
  62. continue;
  63. if (!objcarc::hasAttachedCallOpBundle(I))
  64. continue;
  65. BasicBlock *DestBB = I->getNormalDest();
  66. if (!DestBB->getSinglePredecessor()) {
  67. assert(I->getSuccessor(0) == DestBB &&
  68. "the normal dest is expected to be the first successor");
  69. DestBB = SplitCriticalEdge(I, 0, CriticalEdgeSplittingOptions(DT));
  70. CFGChanged = true;
  71. }
  72. // We don't have to call insertRVCallWithColors since DestBB is the normal
  73. // destination of the invoke.
  74. insertRVCall(&*DestBB->getFirstInsertionPt(), I);
  75. Changed = true;
  76. }
  77. return std::make_pair(Changed, CFGChanged);
  78. }
  79. CallInst *BundledRetainClaimRVs::insertRVCall(Instruction *InsertPt,
  80. CallBase *AnnotatedCall) {
  81. DenseMap<BasicBlock *, ColorVector> BlockColors;
  82. return insertRVCallWithColors(InsertPt, AnnotatedCall, BlockColors);
  83. }
  84. CallInst *BundledRetainClaimRVs::insertRVCallWithColors(
  85. Instruction *InsertPt, CallBase *AnnotatedCall,
  86. const DenseMap<BasicBlock *, ColorVector> &BlockColors) {
  87. IRBuilder<> Builder(InsertPt);
  88. Function *Func = *objcarc::getAttachedARCFunction(AnnotatedCall);
  89. assert(Func && "operand isn't a Function");
  90. Type *ParamTy = Func->getArg(0)->getType();
  91. Value *CallArg = Builder.CreateBitCast(AnnotatedCall, ParamTy);
  92. auto *Call =
  93. createCallInstWithColors(Func, CallArg, "", InsertPt, BlockColors);
  94. RVCalls[Call] = AnnotatedCall;
  95. return Call;
  96. }
  97. BundledRetainClaimRVs::~BundledRetainClaimRVs() {
  98. for (auto P : RVCalls) {
  99. if (ContractPass) {
  100. CallBase *CB = P.second;
  101. // At this point, we know that the annotated calls can't be tail calls
  102. // as they are followed by marker instructions and retainRV/claimRV
  103. // calls. Mark them as notail so that the backend knows these calls
  104. // can't be tail calls.
  105. if (auto *CI = dyn_cast<CallInst>(CB))
  106. CI->setTailCallKind(CallInst::TCK_NoTail);
  107. }
  108. EraseInstruction(P.first);
  109. }
  110. RVCalls.clear();
  111. }