109467b48Spatrick //===- ObjCARC.h - ObjC ARC Optimization --------------*- C++ -*-----------===//
209467b48Spatrick //
309467b48Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
409467b48Spatrick // See https://llvm.org/LICENSE.txt for license information.
509467b48Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
609467b48Spatrick //
709467b48Spatrick //===----------------------------------------------------------------------===//
809467b48Spatrick /// \file
909467b48Spatrick /// This file defines common definitions/declarations used by the ObjC ARC
1009467b48Spatrick /// Optimizer. ARC stands for Automatic Reference Counting and is a system for
1109467b48Spatrick /// managing reference counts for objects in Objective C.
1209467b48Spatrick ///
1309467b48Spatrick /// WARNING: This file knows about certain library functions. It recognizes them
1409467b48Spatrick /// by name, and hardwires knowledge of their semantics.
1509467b48Spatrick ///
1609467b48Spatrick /// WARNING: This file knows about how certain Objective-C library functions are
1709467b48Spatrick /// used. Naive LLVM IR transformations which would otherwise be
1809467b48Spatrick /// behavior-preserving may break these assumptions.
1909467b48Spatrick ///
2009467b48Spatrick //===----------------------------------------------------------------------===//
2109467b48Spatrick
2209467b48Spatrick #ifndef LLVM_LIB_TRANSFORMS_OBJCARC_OBJCARC_H
2309467b48Spatrick #define LLVM_LIB_TRANSFORMS_OBJCARC_OBJCARC_H
2409467b48Spatrick
2573471bf0Spatrick #include "llvm/Analysis/EHPersonalities.h"
2609467b48Spatrick #include "llvm/Analysis/ObjCARCAnalysisUtils.h"
2773471bf0Spatrick #include "llvm/Analysis/ObjCARCUtil.h"
2809467b48Spatrick #include "llvm/Transforms/Utils/Local.h"
2909467b48Spatrick
3009467b48Spatrick namespace llvm {
3109467b48Spatrick namespace objcarc {
3209467b48Spatrick
3309467b48Spatrick /// Erase the given instruction.
3409467b48Spatrick ///
3509467b48Spatrick /// Many ObjC calls return their argument verbatim,
3609467b48Spatrick /// so if it's such a call and the return value has users, replace them with the
3709467b48Spatrick /// argument value.
3809467b48Spatrick ///
EraseInstruction(Instruction * CI)3909467b48Spatrick static inline void EraseInstruction(Instruction *CI) {
4009467b48Spatrick Value *OldArg = cast<CallInst>(CI)->getArgOperand(0);
4109467b48Spatrick
4209467b48Spatrick bool Unused = CI->use_empty();
4309467b48Spatrick
4409467b48Spatrick if (!Unused) {
4509467b48Spatrick // Replace the return value with the argument.
4609467b48Spatrick assert((IsForwarding(GetBasicARCInstKind(CI)) ||
4709467b48Spatrick (IsNoopOnNull(GetBasicARCInstKind(CI)) &&
4809467b48Spatrick IsNullOrUndef(OldArg->stripPointerCasts()))) &&
4909467b48Spatrick "Can't delete non-forwarding instruction with users!");
5009467b48Spatrick CI->replaceAllUsesWith(OldArg);
5109467b48Spatrick }
5209467b48Spatrick
5309467b48Spatrick CI->eraseFromParent();
5409467b48Spatrick
5509467b48Spatrick if (Unused)
5609467b48Spatrick RecursivelyDeleteTriviallyDeadInstructions(OldArg);
5709467b48Spatrick }
5809467b48Spatrick
5909467b48Spatrick /// If Inst is a ReturnRV and its operand is a call or invoke, return the
6009467b48Spatrick /// operand. Otherwise return null.
getreturnRVOperand(const Instruction & Inst,ARCInstKind Class)6109467b48Spatrick static inline const Instruction *getreturnRVOperand(const Instruction &Inst,
6209467b48Spatrick ARCInstKind Class) {
6309467b48Spatrick if (Class != ARCInstKind::RetainRV)
6409467b48Spatrick return nullptr;
6509467b48Spatrick
6609467b48Spatrick const auto *Opnd = Inst.getOperand(0)->stripPointerCasts();
6709467b48Spatrick if (const auto *C = dyn_cast<CallInst>(Opnd))
6809467b48Spatrick return C;
6909467b48Spatrick return dyn_cast<InvokeInst>(Opnd);
7009467b48Spatrick }
7109467b48Spatrick
7209467b48Spatrick /// Return the list of PHI nodes that are equivalent to PN.
7309467b48Spatrick template<class PHINodeTy, class VectorTy>
getEquivalentPHIs(PHINodeTy & PN,VectorTy & PHIList)7409467b48Spatrick void getEquivalentPHIs(PHINodeTy &PN, VectorTy &PHIList) {
7509467b48Spatrick auto *BB = PN.getParent();
7609467b48Spatrick for (auto &P : BB->phis()) {
7709467b48Spatrick if (&P == &PN) // Do not add PN to the list.
7809467b48Spatrick continue;
7909467b48Spatrick unsigned I = 0, E = PN.getNumIncomingValues();
8009467b48Spatrick for (; I < E; ++I) {
8109467b48Spatrick auto *BB = PN.getIncomingBlock(I);
8209467b48Spatrick auto *PNOpnd = PN.getIncomingValue(I)->stripPointerCasts();
8309467b48Spatrick auto *POpnd = P.getIncomingValueForBlock(BB)->stripPointerCasts();
8409467b48Spatrick if (PNOpnd != POpnd)
8509467b48Spatrick break;
8609467b48Spatrick }
8709467b48Spatrick if (I == E)
8809467b48Spatrick PHIList.push_back(&P);
8909467b48Spatrick }
9009467b48Spatrick }
9109467b48Spatrick
getRVInstMarker(Module & M)9273471bf0Spatrick static inline MDString *getRVInstMarker(Module &M) {
9373471bf0Spatrick const char *MarkerKey = getRVMarkerModuleFlagStr();
9473471bf0Spatrick return dyn_cast_or_null<MDString>(M.getModuleFlag(MarkerKey));
9573471bf0Spatrick }
9673471bf0Spatrick
9773471bf0Spatrick /// Create a call instruction with the correct funclet token. This should be
9873471bf0Spatrick /// called instead of calling CallInst::Create directly unless the call is
9973471bf0Spatrick /// going to be removed from the IR before WinEHPrepare.
10073471bf0Spatrick CallInst *createCallInstWithColors(
10173471bf0Spatrick FunctionCallee Func, ArrayRef<Value *> Args, const Twine &NameStr,
10273471bf0Spatrick Instruction *InsertBefore,
10373471bf0Spatrick const DenseMap<BasicBlock *, ColorVector> &BlockColors);
10473471bf0Spatrick
10573471bf0Spatrick class BundledRetainClaimRVs {
10673471bf0Spatrick public:
BundledRetainClaimRVs(bool ContractPass)107*d415bd75Srobert BundledRetainClaimRVs(bool ContractPass) : ContractPass(ContractPass) {}
10873471bf0Spatrick ~BundledRetainClaimRVs();
10973471bf0Spatrick
11073471bf0Spatrick /// Insert a retainRV/claimRV call to the normal destination blocks of invokes
11173471bf0Spatrick /// with operand bundle "clang.arc.attachedcall". If the edge to the normal
11273471bf0Spatrick /// destination block is a critical edge, split it.
11373471bf0Spatrick std::pair<bool, bool> insertAfterInvokes(Function &F, DominatorTree *DT);
11473471bf0Spatrick
11573471bf0Spatrick /// Insert a retainRV/claimRV call.
11673471bf0Spatrick CallInst *insertRVCall(Instruction *InsertPt, CallBase *AnnotatedCall);
11773471bf0Spatrick
11873471bf0Spatrick /// Insert a retainRV/claimRV call with colors.
11973471bf0Spatrick CallInst *insertRVCallWithColors(
12073471bf0Spatrick Instruction *InsertPt, CallBase *AnnotatedCall,
12173471bf0Spatrick const DenseMap<BasicBlock *, ColorVector> &BlockColors);
12273471bf0Spatrick
12373471bf0Spatrick /// See if an instruction is a bundled retainRV/claimRV call.
contains(const Instruction * I)12473471bf0Spatrick bool contains(const Instruction *I) const {
12573471bf0Spatrick if (auto *CI = dyn_cast<CallInst>(I))
12673471bf0Spatrick return RVCalls.count(CI);
12773471bf0Spatrick return false;
12873471bf0Spatrick }
12973471bf0Spatrick
13073471bf0Spatrick /// Remove a retainRV/claimRV call entirely.
eraseInst(CallInst * CI)13173471bf0Spatrick void eraseInst(CallInst *CI) {
13273471bf0Spatrick auto It = RVCalls.find(CI);
13373471bf0Spatrick if (It != RVCalls.end()) {
13473471bf0Spatrick // Remove call to @llvm.objc.clang.arc.noop.use.
135*d415bd75Srobert for (User *U : It->second->users())
136*d415bd75Srobert if (auto *CI = dyn_cast<CallInst>(U))
13773471bf0Spatrick if (CI->getIntrinsicID() == Intrinsic::objc_clang_arc_noop_use) {
13873471bf0Spatrick CI->eraseFromParent();
13973471bf0Spatrick break;
14073471bf0Spatrick }
14173471bf0Spatrick
14273471bf0Spatrick auto *NewCall = CallBase::removeOperandBundle(
14373471bf0Spatrick It->second, LLVMContext::OB_clang_arc_attachedcall, It->second);
14473471bf0Spatrick NewCall->copyMetadata(*It->second);
14573471bf0Spatrick It->second->replaceAllUsesWith(NewCall);
14673471bf0Spatrick It->second->eraseFromParent();
14773471bf0Spatrick RVCalls.erase(It);
14873471bf0Spatrick }
14973471bf0Spatrick EraseInstruction(CI);
15073471bf0Spatrick }
15173471bf0Spatrick
15273471bf0Spatrick private:
15373471bf0Spatrick /// A map of inserted retainRV/claimRV calls to annotated calls/invokes.
15473471bf0Spatrick DenseMap<CallInst *, CallBase *> RVCalls;
15573471bf0Spatrick
15673471bf0Spatrick bool ContractPass;
15773471bf0Spatrick };
15873471bf0Spatrick
15909467b48Spatrick } // end namespace objcarc
16009467b48Spatrick } // end namespace llvm
16109467b48Spatrick
16209467b48Spatrick #endif
163