xref: /freebsd/contrib/llvm-project/llvm/lib/Transforms/ObjCARC/ObjCARC.h (revision 7ef62cebc2f965b0f640263e179276928885e33d)
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 
22 #ifndef LLVM_LIB_TRANSFORMS_OBJCARC_OBJCARC_H
23 #define LLVM_LIB_TRANSFORMS_OBJCARC_OBJCARC_H
24 
25 #include "llvm/Analysis/EHPersonalities.h"
26 #include "llvm/Analysis/ObjCARCAnalysisUtils.h"
27 #include "llvm/Analysis/ObjCARCUtil.h"
28 #include "llvm/Transforms/Utils/Local.h"
29 
30 namespace llvm {
31 namespace objcarc {
32 
33 /// Erase the given instruction.
34 ///
35 /// Many ObjC calls return their argument verbatim,
36 /// so if it's such a call and the return value has users, replace them with the
37 /// argument value.
38 ///
39 static inline void EraseInstruction(Instruction *CI) {
40   Value *OldArg = cast<CallInst>(CI)->getArgOperand(0);
41 
42   bool Unused = CI->use_empty();
43 
44   if (!Unused) {
45     // Replace the return value with the argument.
46     assert((IsForwarding(GetBasicARCInstKind(CI)) ||
47             (IsNoopOnNull(GetBasicARCInstKind(CI)) &&
48              IsNullOrUndef(OldArg->stripPointerCasts()))) &&
49            "Can't delete non-forwarding instruction with users!");
50     CI->replaceAllUsesWith(OldArg);
51   }
52 
53   CI->eraseFromParent();
54 
55   if (Unused)
56     RecursivelyDeleteTriviallyDeadInstructions(OldArg);
57 }
58 
59 /// If Inst is a ReturnRV and its operand is a call or invoke, return the
60 /// operand. Otherwise return null.
61 static inline const Instruction *getreturnRVOperand(const Instruction &Inst,
62                                                     ARCInstKind Class) {
63   if (Class != ARCInstKind::RetainRV)
64     return nullptr;
65 
66   const auto *Opnd = Inst.getOperand(0)->stripPointerCasts();
67   if (const auto *C = dyn_cast<CallInst>(Opnd))
68     return C;
69   return dyn_cast<InvokeInst>(Opnd);
70 }
71 
72 /// Return the list of PHI nodes that are equivalent to PN.
73 template<class PHINodeTy, class VectorTy>
74 void getEquivalentPHIs(PHINodeTy &PN, VectorTy &PHIList) {
75   auto *BB = PN.getParent();
76   for (auto &P : BB->phis()) {
77     if (&P == &PN) // Do not add PN to the list.
78       continue;
79     unsigned I = 0, E = PN.getNumIncomingValues();
80     for (; I < E; ++I) {
81       auto *BB = PN.getIncomingBlock(I);
82       auto *PNOpnd = PN.getIncomingValue(I)->stripPointerCasts();
83       auto *POpnd = P.getIncomingValueForBlock(BB)->stripPointerCasts();
84       if (PNOpnd != POpnd)
85         break;
86     }
87     if (I == E)
88       PHIList.push_back(&P);
89   }
90 }
91 
92 static inline MDString *getRVInstMarker(Module &M) {
93   const char *MarkerKey = getRVMarkerModuleFlagStr();
94   return dyn_cast_or_null<MDString>(M.getModuleFlag(MarkerKey));
95 }
96 
97 /// Create a call instruction with the correct funclet token. This should be
98 /// called instead of calling CallInst::Create directly unless the call is
99 /// going to be removed from the IR before WinEHPrepare.
100 CallInst *createCallInstWithColors(
101     FunctionCallee Func, ArrayRef<Value *> Args, const Twine &NameStr,
102     Instruction *InsertBefore,
103     const DenseMap<BasicBlock *, ColorVector> &BlockColors);
104 
105 class BundledRetainClaimRVs {
106 public:
107   BundledRetainClaimRVs(bool ContractPass) : ContractPass(ContractPass) {}
108   ~BundledRetainClaimRVs();
109 
110   /// Insert a retainRV/claimRV call to the normal destination blocks of invokes
111   /// with operand bundle "clang.arc.attachedcall". If the edge to the normal
112   /// destination block is a critical edge, split it.
113   std::pair<bool, bool> insertAfterInvokes(Function &F, DominatorTree *DT);
114 
115   /// Insert a retainRV/claimRV call.
116   CallInst *insertRVCall(Instruction *InsertPt, CallBase *AnnotatedCall);
117 
118   /// Insert a retainRV/claimRV call with colors.
119   CallInst *insertRVCallWithColors(
120       Instruction *InsertPt, CallBase *AnnotatedCall,
121       const DenseMap<BasicBlock *, ColorVector> &BlockColors);
122 
123   /// See if an instruction is a bundled retainRV/claimRV call.
124   bool contains(const Instruction *I) const {
125     if (auto *CI = dyn_cast<CallInst>(I))
126       return RVCalls.count(CI);
127     return false;
128   }
129 
130   /// Remove a retainRV/claimRV call entirely.
131   void eraseInst(CallInst *CI) {
132     auto It = RVCalls.find(CI);
133     if (It != RVCalls.end()) {
134       // Remove call to @llvm.objc.clang.arc.noop.use.
135       for (User *U : It->second->users())
136         if (auto *CI = dyn_cast<CallInst>(U))
137           if (CI->getIntrinsicID() == Intrinsic::objc_clang_arc_noop_use) {
138             CI->eraseFromParent();
139             break;
140           }
141 
142       auto *NewCall = CallBase::removeOperandBundle(
143           It->second, LLVMContext::OB_clang_arc_attachedcall, It->second);
144       NewCall->copyMetadata(*It->second);
145       It->second->replaceAllUsesWith(NewCall);
146       It->second->eraseFromParent();
147       RVCalls.erase(It);
148     }
149     EraseInstruction(CI);
150   }
151 
152 private:
153   /// A map of inserted retainRV/claimRV calls to annotated calls/invokes.
154   DenseMap<CallInst *, CallBase *> RVCalls;
155 
156   bool ContractPass;
157 };
158 
159 } // end namespace objcarc
160 } // end namespace llvm
161 
162 #endif
163