1 //===- ObjCARCAnalysisUtils.h - ObjC ARC Analysis Utilities -----*- 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
7 //===----------------------------------------------------------------------===//
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.
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.
20 //===----------------------------------------------------------------------===//
22 #ifndef LLVM_LIB_ANALYSIS_OBJCARCANALYSISUTILS_H
23 #define LLVM_LIB_ANALYSIS_OBJCARCANALYSISUTILS_H
25 #include "llvm/ADT/Optional.h"
26 #include "llvm/ADT/StringSwitch.h"
27 #include "llvm/Analysis/AliasAnalysis.h"
28 #include "llvm/Analysis/ObjCARCInstKind.h"
29 #include "llvm/Analysis/Passes.h"
30 #include "llvm/Analysis/ValueTracking.h"
31 #include "llvm/IR/CallSite.h"
32 #include "llvm/IR/Constants.h"
33 #include "llvm/IR/InstIterator.h"
34 #include "llvm/IR/LLVMContext.h"
35 #include "llvm/IR/Module.h"
36 #include "llvm/IR/ValueHandle.h"
37 #include "llvm/Pass.h"
46 /// A handy option to enable/disable all ARC Optimizations.
47 extern bool EnableARCOpts
;
49 /// Test if the given module looks interesting to run ARC optimization
51 inline bool ModuleHasARC(const Module
&M
) {
53 M
.getNamedValue("llvm.objc.retain") ||
54 M
.getNamedValue("llvm.objc.release") ||
55 M
.getNamedValue("llvm.objc.autorelease") ||
56 M
.getNamedValue("llvm.objc.retainAutoreleasedReturnValue") ||
57 M
.getNamedValue("llvm.objc.unsafeClaimAutoreleasedReturnValue") ||
58 M
.getNamedValue("llvm.objc.retainBlock") ||
59 M
.getNamedValue("llvm.objc.autoreleaseReturnValue") ||
60 M
.getNamedValue("llvm.objc.autoreleasePoolPush") ||
61 M
.getNamedValue("llvm.objc.loadWeakRetained") ||
62 M
.getNamedValue("llvm.objc.loadWeak") ||
63 M
.getNamedValue("llvm.objc.destroyWeak") ||
64 M
.getNamedValue("llvm.objc.storeWeak") ||
65 M
.getNamedValue("llvm.objc.initWeak") ||
66 M
.getNamedValue("llvm.objc.moveWeak") ||
67 M
.getNamedValue("llvm.objc.copyWeak") ||
68 M
.getNamedValue("llvm.objc.retainedObject") ||
69 M
.getNamedValue("llvm.objc.unretainedObject") ||
70 M
.getNamedValue("llvm.objc.unretainedPointer") ||
71 M
.getNamedValue("llvm.objc.clang.arc.use");
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.
77 inline const Value
*GetUnderlyingObjCPtr(const Value
*V
,
78 const DataLayout
&DL
) {
80 V
= GetUnderlyingObject(V
, DL
);
81 if (!IsForwarding(GetBasicARCInstKind(V
)))
83 V
= cast
<CallInst
>(V
)->getArgOperand(0);
89 /// A wrapper for GetUnderlyingObjCPtr used for results memoization.
91 GetUnderlyingObjCPtrCached(const Value
*V
, const DataLayout
&DL
,
92 DenseMap
<const Value
*, WeakTrackingVH
> &Cache
) {
93 if (auto InCache
= Cache
.lookup(V
))
96 const Value
*Computed
= GetUnderlyingObjCPtr(V
, DL
);
97 Cache
[V
] = const_cast<Value
*>(Computed
);
101 /// The RCIdentity root of a value \p V is a dominating value U for which
102 /// retaining or releasing U is equivalent to retaining or releasing V. In other
103 /// words, ARC operations on \p V are equivalent to ARC operations on \p U.
105 /// We use this in the ARC optimizer to make it easier to match up ARC
106 /// operations by always mapping ARC operations to RCIdentityRoots instead of
107 /// pointers themselves.
109 /// The two ways that we see RCIdentical values in ObjC are via:
112 /// 2. Forwarding Calls that return their argument verbatim.
114 /// Thus this function strips off pointer casts and forwarding calls. *NOTE*
115 /// This implies that two RCIdentical values must alias.
116 inline const Value
*GetRCIdentityRoot(const Value
*V
) {
118 V
= V
->stripPointerCasts();
119 if (!IsForwarding(GetBasicARCInstKind(V
)))
121 V
= cast
<CallInst
>(V
)->getArgOperand(0);
126 /// Helper which calls const Value *GetRCIdentityRoot(const Value *V) and just
127 /// casts away the const of the result. For documentation about what an
128 /// RCIdentityRoot (and by extension GetRCIdentityRoot is) look at that
130 inline Value
*GetRCIdentityRoot(Value
*V
) {
131 return const_cast<Value
*>(GetRCIdentityRoot((const Value
*)V
));
134 /// Assuming the given instruction is one of the special calls such as
135 /// objc_retain or objc_release, return the RCIdentity root of the argument of
137 inline Value
*GetArgRCIdentityRoot(Value
*Inst
) {
138 return GetRCIdentityRoot(cast
<CallInst
>(Inst
)->getArgOperand(0));
141 inline bool IsNullOrUndef(const Value
*V
) {
142 return isa
<ConstantPointerNull
>(V
) || isa
<UndefValue
>(V
);
145 inline bool IsNoopInstruction(const Instruction
*I
) {
146 return isa
<BitCastInst
>(I
) ||
147 (isa
<GetElementPtrInst
>(I
) &&
148 cast
<GetElementPtrInst
>(I
)->hasAllZeroIndices());
151 /// Test whether the given value is possible a retainable object pointer.
152 inline bool IsPotentialRetainableObjPtr(const Value
*Op
) {
153 // Pointers to static or stack storage are not valid retainable object
155 if (isa
<Constant
>(Op
) || isa
<AllocaInst
>(Op
))
157 // Special arguments can not be a valid retainable object pointer.
158 if (const Argument
*Arg
= dyn_cast
<Argument
>(Op
))
159 if (Arg
->hasByValAttr() ||
160 Arg
->hasInAllocaAttr() ||
161 Arg
->hasNestAttr() ||
162 Arg
->hasStructRetAttr())
164 // Only consider values with pointer types.
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());
172 // Conservatively assume anything else is a potential retainable object
177 inline bool IsPotentialRetainableObjPtr(const Value
*Op
,
179 // First make the rudimentary check.
180 if (!IsPotentialRetainableObjPtr(Op
))
183 // Objects in constant memory are not reference-counted.
184 if (AA
.pointsToConstantMemory(Op
))
187 // Pointers in constant memory are not pointing to reference-counted objects.
188 if (const LoadInst
*LI
= dyn_cast
<LoadInst
>(Op
))
189 if (AA
.pointsToConstantMemory(LI
->getPointerOperand()))
192 // Otherwise assume the worst.
196 /// Helper for GetARCInstKind. Determines what kind of construct CS
198 inline ARCInstKind
GetCallSiteClass(ImmutableCallSite CS
) {
199 for (ImmutableCallSite::arg_iterator I
= CS
.arg_begin(), E
= CS
.arg_end();
201 if (IsPotentialRetainableObjPtr(*I
))
202 return CS
.onlyReadsMemory() ? ARCInstKind::User
: ARCInstKind::CallOrUser
;
204 return CS
.onlyReadsMemory() ? ARCInstKind::None
: ARCInstKind::Call
;
207 /// Return true if this value refers to a distinct and identifiable
210 /// This is similar to AliasAnalysis's isIdentifiedObject, except that it uses
211 /// special knowledge of ObjC conventions.
212 inline bool IsObjCIdentifiedObject(const Value
*V
) {
213 // Assume that call results and arguments have their own "provenance".
214 // Constants (including GlobalVariables) and Allocas are never
215 // reference-counted.
216 if (isa
<CallInst
>(V
) || isa
<InvokeInst
>(V
) ||
217 isa
<Argument
>(V
) || isa
<Constant
>(V
) ||
221 if (const LoadInst
*LI
= dyn_cast
<LoadInst
>(V
)) {
222 const Value
*Pointer
=
223 GetRCIdentityRoot(LI
->getPointerOperand());
224 if (const GlobalVariable
*GV
= dyn_cast
<GlobalVariable
>(Pointer
)) {
225 // A constant pointer can't be pointing to an object on the heap. It may
226 // be reference-counted, but it won't be deleted.
227 if (GV
->isConstant())
229 StringRef Name
= GV
->getName();
230 // These special variables are known to hold values which are not
231 // reference-counted pointers.
232 if (Name
.startswith("\01l_objc_msgSend_fixup_"))
235 StringRef Section
= GV
->getSection();
236 if (Section
.find("__message_refs") != StringRef::npos
||
237 Section
.find("__objc_classrefs") != StringRef::npos
||
238 Section
.find("__objc_superrefs") != StringRef::npos
||
239 Section
.find("__objc_methname") != StringRef::npos
||
240 Section
.find("__cstring") != StringRef::npos
)
248 enum class ARCMDKindID
{
254 /// A cache of MDKinds used by various ARC optimizations.
255 class ARCMDKindCache
{
258 /// The Metadata Kind for clang.imprecise_release metadata.
259 llvm::Optional
<unsigned> ImpreciseReleaseMDKind
;
261 /// The Metadata Kind for clang.arc.copy_on_escape metadata.
262 llvm::Optional
<unsigned> CopyOnEscapeMDKind
;
264 /// The Metadata Kind for clang.arc.no_objc_arc_exceptions metadata.
265 llvm::Optional
<unsigned> NoObjCARCExceptionsMDKind
;
268 void init(Module
*Mod
) {
270 ImpreciseReleaseMDKind
= NoneType::None
;
271 CopyOnEscapeMDKind
= NoneType::None
;
272 NoObjCARCExceptionsMDKind
= NoneType::None
;
275 unsigned get(ARCMDKindID ID
) {
277 case ARCMDKindID::ImpreciseRelease
:
278 if (!ImpreciseReleaseMDKind
)
279 ImpreciseReleaseMDKind
=
280 M
->getContext().getMDKindID("clang.imprecise_release");
281 return *ImpreciseReleaseMDKind
;
282 case ARCMDKindID::CopyOnEscape
:
283 if (!CopyOnEscapeMDKind
)
285 M
->getContext().getMDKindID("clang.arc.copy_on_escape");
286 return *CopyOnEscapeMDKind
;
287 case ARCMDKindID::NoObjCARCExceptions
:
288 if (!NoObjCARCExceptionsMDKind
)
289 NoObjCARCExceptionsMDKind
=
290 M
->getContext().getMDKindID("clang.arc.no_objc_arc_exceptions");
291 return *NoObjCARCExceptionsMDKind
;
293 llvm_unreachable("Covered switch isn't covered?!");
297 } // end namespace objcarc
298 } // end namespace llvm