ObjCARC.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. //===- ObjCARC.h - ObjC ARC Optimization --------------*- C++ -*-----------===//
  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. /// \file
  9. /// This file defines common definitions/declarations used by the ObjC ARC
  10. /// Optimizer. ARC stands for Automatic Reference Counting and is a system for
  11. /// managing reference counts for objects in Objective C.
  12. ///
  13. /// WARNING: This file knows about certain library functions. It recognizes them
  14. /// by name, and hardwires knowledge of their semantics.
  15. ///
  16. /// WARNING: This file knows about how certain Objective-C library functions are
  17. /// used. Naive LLVM IR transformations which would otherwise be
  18. /// behavior-preserving may break these assumptions.
  19. ///
  20. //===----------------------------------------------------------------------===//
  21. #ifndef LLVM_LIB_TRANSFORMS_OBJCARC_OBJCARC_H
  22. #define LLVM_LIB_TRANSFORMS_OBJCARC_OBJCARC_H
  23. #include "ARCRuntimeEntryPoints.h"
  24. #include "llvm/Analysis/EHPersonalities.h"
  25. #include "llvm/Analysis/ObjCARCAnalysisUtils.h"
  26. #include "llvm/Analysis/ObjCARCUtil.h"
  27. #include "llvm/Transforms/Utils/Local.h"
  28. namespace llvm {
  29. namespace objcarc {
  30. /// Erase the given instruction.
  31. ///
  32. /// Many ObjC calls return their argument verbatim,
  33. /// so if it's such a call and the return value has users, replace them with the
  34. /// argument value.
  35. ///
  36. static inline void EraseInstruction(Instruction *CI) {
  37. Value *OldArg = cast<CallInst>(CI)->getArgOperand(0);
  38. bool Unused = CI->use_empty();
  39. if (!Unused) {
  40. // Replace the return value with the argument.
  41. assert((IsForwarding(GetBasicARCInstKind(CI)) ||
  42. (IsNoopOnNull(GetBasicARCInstKind(CI)) &&
  43. IsNullOrUndef(OldArg->stripPointerCasts()))) &&
  44. "Can't delete non-forwarding instruction with users!");
  45. CI->replaceAllUsesWith(OldArg);
  46. }
  47. CI->eraseFromParent();
  48. if (Unused)
  49. RecursivelyDeleteTriviallyDeadInstructions(OldArg);
  50. }
  51. /// If Inst is a ReturnRV and its operand is a call or invoke, return the
  52. /// operand. Otherwise return null.
  53. static inline const Instruction *getreturnRVOperand(const Instruction &Inst,
  54. ARCInstKind Class) {
  55. if (Class != ARCInstKind::RetainRV)
  56. return nullptr;
  57. const auto *Opnd = Inst.getOperand(0)->stripPointerCasts();
  58. if (const auto *C = dyn_cast<CallInst>(Opnd))
  59. return C;
  60. return dyn_cast<InvokeInst>(Opnd);
  61. }
  62. /// Return the list of PHI nodes that are equivalent to PN.
  63. template<class PHINodeTy, class VectorTy>
  64. void getEquivalentPHIs(PHINodeTy &PN, VectorTy &PHIList) {
  65. auto *BB = PN.getParent();
  66. for (auto &P : BB->phis()) {
  67. if (&P == &PN) // Do not add PN to the list.
  68. continue;
  69. unsigned I = 0, E = PN.getNumIncomingValues();
  70. for (; I < E; ++I) {
  71. auto *BB = PN.getIncomingBlock(I);
  72. auto *PNOpnd = PN.getIncomingValue(I)->stripPointerCasts();
  73. auto *POpnd = P.getIncomingValueForBlock(BB)->stripPointerCasts();
  74. if (PNOpnd != POpnd)
  75. break;
  76. }
  77. if (I == E)
  78. PHIList.push_back(&P);
  79. }
  80. }
  81. static inline MDString *getRVInstMarker(Module &M) {
  82. const char *MarkerKey = getRVMarkerModuleFlagStr();
  83. return dyn_cast_or_null<MDString>(M.getModuleFlag(MarkerKey));
  84. }
  85. /// Create a call instruction with the correct funclet token. This should be
  86. /// called instead of calling CallInst::Create directly unless the call is
  87. /// going to be removed from the IR before WinEHPrepare.
  88. CallInst *createCallInstWithColors(
  89. FunctionCallee Func, ArrayRef<Value *> Args, const Twine &NameStr,
  90. Instruction *InsertBefore,
  91. const DenseMap<BasicBlock *, ColorVector> &BlockColors);
  92. class BundledRetainClaimRVs {
  93. public:
  94. BundledRetainClaimRVs(bool ContractPass) : ContractPass(ContractPass) {}
  95. ~BundledRetainClaimRVs();
  96. /// Insert a retainRV/claimRV call to the normal destination blocks of invokes
  97. /// with operand bundle "clang.arc.attachedcall". If the edge to the normal
  98. /// destination block is a critical edge, split it.
  99. std::pair<bool, bool> insertAfterInvokes(Function &F, DominatorTree *DT);
  100. /// Insert a retainRV/claimRV call.
  101. CallInst *insertRVCall(Instruction *InsertPt, CallBase *AnnotatedCall);
  102. /// Insert a retainRV/claimRV call with colors.
  103. CallInst *insertRVCallWithColors(
  104. Instruction *InsertPt, CallBase *AnnotatedCall,
  105. const DenseMap<BasicBlock *, ColorVector> &BlockColors);
  106. /// See if an instruction is a bundled retainRV/claimRV call.
  107. bool contains(const Instruction *I) const {
  108. if (auto *CI = dyn_cast<CallInst>(I))
  109. return RVCalls.count(CI);
  110. return false;
  111. }
  112. /// Remove a retainRV/claimRV call entirely.
  113. void eraseInst(CallInst *CI) {
  114. auto It = RVCalls.find(CI);
  115. if (It != RVCalls.end()) {
  116. // Remove call to @llvm.objc.clang.arc.noop.use.
  117. for (auto U = It->second->user_begin(), E = It->second->user_end(); U != E; ++U)
  118. if (auto *CI = dyn_cast<CallInst>(*U))
  119. if (CI->getIntrinsicID() == Intrinsic::objc_clang_arc_noop_use) {
  120. CI->eraseFromParent();
  121. break;
  122. }
  123. auto *NewCall = CallBase::removeOperandBundle(
  124. It->second, LLVMContext::OB_clang_arc_attachedcall, It->second);
  125. NewCall->copyMetadata(*It->second);
  126. It->second->replaceAllUsesWith(NewCall);
  127. It->second->eraseFromParent();
  128. RVCalls.erase(It);
  129. }
  130. EraseInstruction(CI);
  131. }
  132. private:
  133. /// A map of inserted retainRV/claimRV calls to annotated calls/invokes.
  134. DenseMap<CallInst *, CallBase *> RVCalls;
  135. bool ContractPass;
  136. };
  137. } // end namespace objcarc
  138. } // end namespace llvm
  139. #endif