LLVM 22.0.0git
ObjCARCAnalysisUtils.h
Go to the documentation of this file.
1//===- ObjCARCAnalysisUtils.h - ObjC ARC Analysis Utilities -----*- 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 analysis utilities used by the ObjC ARC Optimizer.
10/// ARC stands for Automatic Reference Counting and is a system for managing
11/// 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_ANALYSIS_OBJCARCANALYSISUTILS_H
23#define LLVM_ANALYSIS_OBJCARCANALYSISUTILS_H
24
27#include "llvm/IR/Constants.h"
28#include "llvm/IR/Module.h"
29#include "llvm/IR/ValueHandle.h"
30#include <optional>
31
32namespace llvm {
33
34class AAResults;
35
36namespace objcarc {
37
38/// A handy option to enable/disable all ARC Optimizations.
39extern bool EnableARCOpts;
40
41/// Test if the given module looks interesting to run ARC optimization
42/// on.
43inline bool ModuleHasARC(const Module &M) {
44 std::initializer_list<Intrinsic::ID> Intrinsics = {
45 Intrinsic::objc_retain,
46 Intrinsic::objc_release,
47 Intrinsic::objc_autorelease,
48 Intrinsic::objc_retainAutoreleasedReturnValue,
49 Intrinsic::objc_retainBlock,
50 Intrinsic::objc_autoreleaseReturnValue,
51 Intrinsic::objc_autoreleasePoolPush,
52 Intrinsic::objc_loadWeakRetained,
53 Intrinsic::objc_loadWeak,
54 Intrinsic::objc_destroyWeak,
55 Intrinsic::objc_initWeak,
56 Intrinsic::objc_copyWeak,
57 Intrinsic::objc_retainedObject,
58 Intrinsic::objc_unretainedObject,
59 Intrinsic::objc_unretainedPointer,
60 Intrinsic::objc_clang_arc_noop_use,
61 Intrinsic::objc_clang_arc_use,
62 };
63#ifndef NDEBUG
64 for (Intrinsic::ID IID : Intrinsics)
66 "Can only check non-overloaded intrinsics");
67#endif
68 for (Intrinsic::ID IID : Intrinsics)
70 return true;
71 return false;
72}
73
74/// This is a wrapper around getUnderlyingObject which also knows how to
75/// look through objc_retain and objc_autorelease calls, which we know to return
76/// their argument verbatim.
77inline const Value *GetUnderlyingObjCPtr(const Value *V) {
78 for (;;) {
81 break;
82 V = cast<CallInst>(V)->getArgOperand(0);
83 }
84
85 return V;
86}
87
88/// A wrapper for GetUnderlyingObjCPtr used for results memoization.
90 const Value *V,
91 DenseMap<const Value *, std::pair<WeakVH, WeakTrackingVH>> &Cache) {
92 // The entry is invalid if either value handle is null.
93 auto InCache = Cache.lookup(V);
94 if (InCache.first && InCache.second)
95 return InCache.second;
96
97 const Value *Computed = GetUnderlyingObjCPtr(V);
98 Cache[V] =
99 std::make_pair(const_cast<Value *>(V), const_cast<Value *>(Computed));
100 return Computed;
101}
102
103/// The RCIdentity root of a value \p V is a dominating value U for which
104/// retaining or releasing U is equivalent to retaining or releasing V. In other
105/// words, ARC operations on \p V are equivalent to ARC operations on \p U.
106///
107/// We use this in the ARC optimizer to make it easier to match up ARC
108/// operations by always mapping ARC operations to RCIdentityRoots instead of
109/// pointers themselves.
110///
111/// The two ways that we see RCIdentical values in ObjC are via:
112///
113/// 1. PointerCasts
114/// 2. Forwarding Calls that return their argument verbatim.
115///
116/// Thus this function strips off pointer casts and forwarding calls. *NOTE*
117/// This implies that two RCIdentical values must alias.
118inline const Value *GetRCIdentityRoot(const Value *V) {
119 for (;;) {
120 V = V->stripPointerCasts();
122 break;
123 V = cast<CallInst>(V)->getArgOperand(0);
124 }
125 return V;
126}
127
128/// Helper which calls const Value *GetRCIdentityRoot(const Value *V) and just
129/// casts away the const of the result. For documentation about what an
130/// RCIdentityRoot (and by extension GetRCIdentityRoot is) look at that
131/// function.
133 return const_cast<Value *>(GetRCIdentityRoot((const Value *)V));
134}
135
136/// Assuming the given instruction is one of the special calls such as
137/// objc_retain or objc_release, return the RCIdentity root of the argument of
138/// the call.
140 return GetRCIdentityRoot(cast<CallInst>(Inst)->getArgOperand(0));
141}
142
143inline bool IsNullOrUndef(const Value *V) {
144 return isa<ConstantPointerNull>(V) || isa<UndefValue>(V);
145}
146
147inline bool IsNoopInstruction(const Instruction *I) {
148 return isa<BitCastInst>(I) ||
149 (isa<GetElementPtrInst>(I) &&
150 cast<GetElementPtrInst>(I)->hasAllZeroIndices());
151}
152
153/// Test whether the given value is possible a retainable object pointer.
155 // Pointers to static or stack storage are not valid retainable object
156 // pointers.
157 if (isa<Constant>(Op) || isa<AllocaInst>(Op))
158 return false;
159 // Special arguments can not be a valid retainable object pointer.
160 if (const Argument *Arg = dyn_cast<Argument>(Op))
161 if (Arg->hasPassPointeeByValueCopyAttr() || Arg->hasNestAttr() ||
162 Arg->hasStructRetAttr())
163 return false;
164 // Only consider values with pointer types.
165 //
166 // It seemes intuitive to exclude function pointer types as well, since
167 // functions are never retainable object pointers, however clang occasionally
168 // bitcasts retainable object pointers to function-pointer type temporarily.
169 PointerType *Ty = dyn_cast<PointerType>(Op->getType());
170 if (!Ty)
171 return false;
172 // Conservatively assume anything else is a potential retainable object
173 // pointer.
174 return true;
175}
176
178
179/// Helper for GetARCInstKind. Determines what kind of construct CS
180/// is.
182 for (const Use &U : CB.args())
185
187}
188
189/// Return true if this value refers to a distinct and identifiable
190/// object.
191///
192/// This is similar to AliasAnalysis's isIdentifiedObject, except that it uses
193/// special knowledge of ObjC conventions.
194inline bool IsObjCIdentifiedObject(const Value *V) {
195 // Assume that call results and arguments have their own "provenance".
196 // Constants (including GlobalVariables) and Allocas are never
197 // reference-counted.
198 if (isa<CallInst>(V) || isa<InvokeInst>(V) ||
199 isa<Argument>(V) || isa<Constant>(V) ||
200 isa<AllocaInst>(V))
201 return true;
202
203 if (const LoadInst *LI = dyn_cast<LoadInst>(V)) {
204 const Value *Pointer =
205 GetRCIdentityRoot(LI->getPointerOperand());
206 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Pointer)) {
207 // A constant pointer can't be pointing to an object on the heap. It may
208 // be reference-counted, but it won't be deleted.
209 if (GV->isConstant())
210 return true;
211 StringRef Name = GV->getName();
212 // These special variables are known to hold values which are not
213 // reference-counted pointers.
214 if (Name.starts_with("\01l_objc_msgSend_fixup_"))
215 return true;
216
217 StringRef Section = GV->getSection();
218 if (Section.contains("__message_refs") ||
219 Section.contains("__objc_classrefs") ||
220 Section.contains("__objc_superrefs") ||
221 Section.contains("__objc_methname") || Section.contains("__cstring"))
222 return true;
223 }
224 }
225
226 return false;
227}
228
229enum class ARCMDKindID {
233};
234
235/// A cache of MDKinds used by various ARC optimizations.
237 Module *M;
238
239 /// The Metadata Kind for clang.imprecise_release metadata.
240 std::optional<unsigned> ImpreciseReleaseMDKind;
241
242 /// The Metadata Kind for clang.arc.copy_on_escape metadata.
243 std::optional<unsigned> CopyOnEscapeMDKind;
244
245 /// The Metadata Kind for clang.arc.no_objc_arc_exceptions metadata.
246 std::optional<unsigned> NoObjCARCExceptionsMDKind;
247
248public:
249 void init(Module *Mod) {
250 M = Mod;
251 ImpreciseReleaseMDKind = std::nullopt;
252 CopyOnEscapeMDKind = std::nullopt;
253 NoObjCARCExceptionsMDKind = std::nullopt;
254 }
255
256 unsigned get(ARCMDKindID ID) {
257 switch (ID) {
259 if (!ImpreciseReleaseMDKind)
260 ImpreciseReleaseMDKind =
261 M->getContext().getMDKindID("clang.imprecise_release");
262 return *ImpreciseReleaseMDKind;
264 if (!CopyOnEscapeMDKind)
265 CopyOnEscapeMDKind =
266 M->getContext().getMDKindID("clang.arc.copy_on_escape");
267 return *CopyOnEscapeMDKind;
269 if (!NoObjCARCExceptionsMDKind)
270 NoObjCARCExceptionsMDKind =
271 M->getContext().getMDKindID("clang.arc.no_objc_arc_exceptions");
272 return *NoObjCARCExceptionsMDKind;
273 }
274 llvm_unreachable("Covered switch isn't covered?!");
275 }
276};
277
278} // end namespace objcarc
279} // end namespace llvm
280
281#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
std::string Name
Module.h This file contains the declarations for the Module class.
#define I(x, y, z)
Definition: MD5.cpp:58
A private abstract base class describing the concept of an individual alias analysis implementation.
This class represents an incoming formal argument to a Function.
Definition: Argument.h:32
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1116
bool onlyReadsMemory(unsigned OpNo) const
Definition: InstrTypes.h:1751
iterator_range< User::op_iterator > args()
Iteration adapter for range-for loops.
Definition: InstrTypes.h:1283
This class represents an Operation in the Expression.
An instruction for reading from memory.
Definition: Instructions.h:180
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
Class to represent pointers.
Definition: DerivedTypes.h:700
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:55
A Use represents the edge between a Value definition and its users.
Definition: Use.h:35
LLVM Value Representation.
Definition: Value.h:75
A cache of MDKinds used by various ARC optimizations.
unsigned get(ARCMDKindID ID)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
LLVM_ABI Function * getDeclarationIfExists(const Module *M, ID id)
Look up the Function declaration of the intrinsic id in the Module M and return it if it exists.
Definition: Intrinsics.cpp:762
LLVM_ABI bool isOverloaded(ID id)
Returns true if the intrinsic can be overloaded.
Definition: Intrinsics.cpp:618
bool IsPotentialRetainableObjPtr(const Value *Op)
Test whether the given value is possible a retainable object pointer.
bool ModuleHasARC(const Module &M)
Test if the given module looks interesting to run ARC optimization on.
ARCInstKind GetCallSiteClass(const CallBase &CB)
Helper for GetARCInstKind.
bool IsNullOrUndef(const Value *V)
ARCInstKind
Equivalence classes of instructions in the ARC Model.
@ CallOrUser
could call objc_release and/or "use" pointers
@ None
anything that is inert from an ARC perspective.
@ User
could "use" a pointer
@ Call
could call objc_release
bool IsObjCIdentifiedObject(const Value *V)
Return true if this value refers to a distinct and identifiable object.
bool EnableARCOpts
A handy option to enable/disable all ARC Optimizations.
bool IsForwarding(ARCInstKind Class)
Test if the given class represents instructions which return their argument verbatim.
bool IsNoopInstruction(const Instruction *I)
ARCInstKind GetBasicARCInstKind(const Value *V)
Determine which objc runtime call instruction class V belongs to.
const Value * GetUnderlyingObjCPtrCached(const Value *V, DenseMap< const Value *, std::pair< WeakVH, WeakTrackingVH > > &Cache)
A wrapper for GetUnderlyingObjCPtr used for results memoization.
Value * GetArgRCIdentityRoot(Value *Inst)
Assuming the given instruction is one of the special calls such as objc_retain or objc_release,...
const Value * GetUnderlyingObjCPtr(const Value *V)
This is a wrapper around getUnderlyingObject which also knows how to look through objc_retain and obj...
const Value * GetRCIdentityRoot(const Value *V)
The RCIdentity root of a value V is a dominating value U for which retaining or releasing U is equiva...
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Mod
The access may modify the value stored in memory.
LLVM_ABI const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=MaxLookupSearchDepth)
This method strips off any GEP address adjustments, pointer casts or llvm.threadlocal....