LLVM 19.0.0git
Go to the documentation of this file.
1//===- ObjCARC.h - ObjC ARC Optimization --------------*- C++ -*-----------===//
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
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.
13/// WARNING: This file knows about certain library functions. It recognizes them
14/// by name, and hardwires knowledge of their semantics.
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.
30namespace llvm {
31namespace objcarc {
33/// Erase the given instruction.
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.
39static inline void EraseInstruction(Instruction *CI) {
40 Value *OldArg = cast<CallInst>(CI)->getArgOperand(0);
42 bool Unused = CI->use_empty();
44 if (!Unused) {
45 // Replace the return value with the argument.
48 IsNullOrUndef(OldArg->stripPointerCasts()))) &&
49 "Can't delete non-forwarding instruction with users!");
50 CI->replaceAllUsesWith(OldArg);
51 }
53 CI->eraseFromParent();
55 if (Unused)
59/// If Inst is a ReturnRV and its operand is a call or invoke, return the
60/// operand. Otherwise return null.
61static inline const Instruction *getreturnRVOperand(const Instruction &Inst,
62 ARCInstKind Class) {
63 if (Class != ARCInstKind::RetainRV)
64 return nullptr;
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);
72/// Return the list of PHI nodes that are equivalent to PN.
73template<class PHINodeTy, class VectorTy>
74void 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 }
92static inline MDString *getRVInstMarker(Module &M) {
93 const char *MarkerKey = getRVMarkerModuleFlagStr();
94 return dyn_cast_or_null<MDString>(M.getModuleFlag(MarkerKey));
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.
101 FunctionCallee Func, ArrayRef<Value *> Args, const Twine &NameStr,
102 BasicBlock::iterator InsertBefore,
103 const DenseMap<BasicBlock *, ColorVector> &BlockColors);
107 BundledRetainClaimRVs(bool ContractPass) : ContractPass(ContractPass) {}
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);
115 /// Insert a retainRV/claimRV call.
117 CallBase *AnnotatedCall);
119 /// Insert a retainRV/claimRV call with colors.
121 BasicBlock::iterator InsertPt, CallBase *AnnotatedCall,
122 const DenseMap<BasicBlock *, ColorVector> &BlockColors);
124 /// See if an instruction is a bundled retainRV/claimRV call.
125 bool contains(const Instruction *I) const {
126 if (auto *CI = dyn_cast<CallInst>(I))
127 return RVCalls.count(CI);
128 return false;
129 }
131 /// Remove a retainRV/claimRV call entirely.
132 void eraseInst(CallInst *CI) {
133 auto It = RVCalls.find(CI);
134 if (It != RVCalls.end()) {
135 // Remove call to @llvm.objc.clang.arc.noop.use.
136 for (User *U : It->second->users())
137 if (auto *CI = dyn_cast<CallInst>(U))
138 if (CI->getIntrinsicID() == Intrinsic::objc_clang_arc_noop_use) {
139 CI->eraseFromParent();
140 break;
141 }
143 auto *NewCall = CallBase::removeOperandBundle(
145 It->second->getIterator());
146 NewCall->copyMetadata(*It->second);
147 It->second->replaceAllUsesWith(NewCall);
148 It->second->eraseFromParent();
149 RVCalls.erase(It);
150 }
152 }
155 /// A map of inserted retainRV/claimRV calls to annotated calls/invokes.
158 bool ContractPass;
161} // end namespace objcarc
162} // end namespace llvm
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
This file defines common analysis utilities used by the ObjC ARC Optimizer.
This file defines ARC utility functions which are used by various parts of the compiler.
#define P(N)
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
InstListType::iterator iterator
Instruction iterators...
Definition: BasicBlock.h:165
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1494
static CallBase * removeOperandBundle(CallBase *CB, uint32_t ID, Instruction *InsertPt=nullptr)
Create a clone of CB with operand bundle ID removed.
Intrinsic::ID getIntrinsicID() const
Returns the intrinsic ID of the intrinsic called or Intrinsic::not_intrinsic if the called function i...
This class represents a function call, abstracting a target machine's calling convention.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition: Dominators.h:162
A handy container for a FunctionType+Callee-pointer pair, which can be passed around as a single enti...
Definition: DerivedTypes.h:168
InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
A single uniqued string.
Definition: Metadata.h:720
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
Value * getOperand(unsigned i) const
Definition: User.h:169
LLVM Value Representation.
Definition: Value.h:74
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition: Value.cpp:534
const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs and address space casts.
Definition: Value.cpp:693
bool use_empty() const
Definition: Value.h:344
bool contains(const Instruction *I) const
See if an instruction is a bundled retainRV/claimRV call.
Definition: ObjCARC.h:125
std::pair< bool, bool > insertAfterInvokes(Function &F, DominatorTree *DT)
Insert a retainRV/claimRV call to the normal destination blocks of invokes with operand bundle "clang...
Definition: ObjCARC.cpp:44
CallInst * insertRVCall(BasicBlock::iterator InsertPt, CallBase *AnnotatedCall)
Insert a retainRV/claimRV call.
Definition: ObjCARC.cpp:74
void eraseInst(CallInst *CI)
Remove a retainRV/claimRV call entirely.
Definition: ObjCARC.h:132
BundledRetainClaimRVs(bool ContractPass)
Definition: ObjCARC.h:107
CallInst * insertRVCallWithColors(BasicBlock::iterator InsertPt, CallBase *AnnotatedCall, const DenseMap< BasicBlock *, ColorVector > &BlockColors)
Insert a retainRV/claimRV call with colors.
Definition: ObjCARC.cpp:80
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
static const Instruction * getreturnRVOperand(const Instruction &Inst, ARCInstKind Class)
If Inst is a ReturnRV and its operand is a call or invoke, return the operand.
Definition: ObjCARC.h:61
const char * getRVMarkerModuleFlagStr()
Definition: ObjCARCUtil.h:25
bool IsNullOrUndef(const Value *V)
Equivalence classes of instructions in the ARC Model.
@ RetainRV
CallInst * createCallInstWithColors(FunctionCallee Func, ArrayRef< Value * > Args, const Twine &NameStr, BasicBlock::iterator InsertBefore, const DenseMap< BasicBlock *, ColorVector > &BlockColors)
Create a call instruction with the correct funclet token.
Definition: ObjCARC.cpp:24
void getEquivalentPHIs(PHINodeTy &PN, VectorTy &PHIList)
Return the list of PHI nodes that are equivalent to PN.
Definition: ObjCARC.h:74
bool IsForwarding(ARCInstKind Class)
Test if the given class represents instructions which return their argument verbatim.
ARCInstKind GetBasicARCInstKind(const Value *V)
Determine which objc runtime call instruction class V belongs to.
bool IsNoopOnNull(ARCInstKind Class)
Test if the given class represents instructions which do nothing if passed a null pointer.
static MDString * getRVInstMarker(Module &M)
Definition: ObjCARC.h:92
static void EraseInstruction(Instruction *CI)
Erase the given instruction.
Definition: ObjCARC.h:39
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
bool RecursivelyDeleteTriviallyDeadInstructions(Value *V, const TargetLibraryInfo *TLI=nullptr, MemorySSAUpdater *MSSAU=nullptr, std::function< void(Value *)> AboutToDeleteCallback=std::function< void(Value *)>())
If the specified value is a trivially dead instruction, delete it.
Definition: Local.cpp:539