1 //===- ObjCARCAnalysisUtils.h - ObjC ARC Analysis Utilities -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 /// This file defines common analysis utilities used by the ObjC ARC Optimizer.
11 /// ARC stands for Automatic Reference Counting and is a system for managing
12 /// reference counts for objects in Objective C.
14 /// WARNING: This file knows about certain library functions. It recognizes them
15 /// by name, and hardwires knowledge of their semantics.
17 /// WARNING: This file knows about how certain Objective-C library functions are
18 /// used. Naive LLVM IR transformations which would otherwise be
19 /// behavior-preserving may break these assumptions.
21 //===----------------------------------------------------------------------===//
23 #ifndef LLVM_LIB_ANALYSIS_OBJCARCANALYSISUTILS_H
24 #define LLVM_LIB_ANALYSIS_OBJCARCANALYSISUTILS_H
26 #include "llvm/ADT/Optional.h"
27 #include "llvm/ADT/StringSwitch.h"
28 #include "llvm/Analysis/AliasAnalysis.h"
29 #include "llvm/Analysis/ObjCARCInstKind.h"
30 #include "llvm/Analysis/Passes.h"
31 #include "llvm/Analysis/ValueTracking.h"
32 #include "llvm/IR/CallSite.h"
33 #include "llvm/IR/Constants.h"
34 #include "llvm/IR/InstIterator.h"
35 #include "llvm/IR/LLVMContext.h"
36 #include "llvm/IR/Module.h"
37 #include "llvm/IR/ValueHandle.h"
38 #include "llvm/Pass.h"
47 /// A handy option to enable/disable all ARC Optimizations.
48 extern bool EnableARCOpts;
50 /// Test if the given module looks interesting to run ARC optimization
52 inline bool ModuleHasARC(const Module &M) {
54 M.getNamedValue("llvm.objc.retain") ||
55 M.getNamedValue("llvm.objc.release") ||
56 M.getNamedValue("llvm.objc.autorelease") ||
57 M.getNamedValue("llvm.objc.retainAutoreleasedReturnValue") ||
58 M.getNamedValue("llvm.objc.unsafeClaimAutoreleasedReturnValue") ||
59 M.getNamedValue("llvm.objc.retainBlock") ||
60 M.getNamedValue("llvm.objc.autoreleaseReturnValue") ||
61 M.getNamedValue("llvm.objc.autoreleasePoolPush") ||
62 M.getNamedValue("llvm.objc.loadWeakRetained") ||
63 M.getNamedValue("llvm.objc.loadWeak") ||
64 M.getNamedValue("llvm.objc.destroyWeak") ||
65 M.getNamedValue("llvm.objc.storeWeak") ||
66 M.getNamedValue("llvm.objc.initWeak") ||
67 M.getNamedValue("llvm.objc.moveWeak") ||
68 M.getNamedValue("llvm.objc.copyWeak") ||
69 M.getNamedValue("llvm.objc.retainedObject") ||
70 M.getNamedValue("llvm.objc.unretainedObject") ||
71 M.getNamedValue("llvm.objc.unretainedPointer") ||
72 M.getNamedValue("llvm.objc.clang.arc.use");
75 /// This is a wrapper around getUnderlyingObject which also knows how to
76 /// look through objc_retain and objc_autorelease calls, which we know to return
77 /// their argument verbatim.
78 inline const Value *GetUnderlyingObjCPtr(const Value *V,
79 const DataLayout &DL) {
81 V = GetUnderlyingObject(V, DL);
82 if (!IsForwarding(GetBasicARCInstKind(V)))
84 V = cast<CallInst>(V)->getArgOperand(0);
90 /// A wrapper for GetUnderlyingObjCPtr used for results memoization.
92 GetUnderlyingObjCPtrCached(const Value *V, const DataLayout &DL,
93 DenseMap<const Value *, WeakTrackingVH> &Cache) {
94 if (auto InCache = Cache.lookup(V))
97 const Value *Computed = GetUnderlyingObjCPtr(V, DL);
98 Cache[V] = const_cast<Value *>(Computed);
102 /// The RCIdentity root of a value \p V is a dominating value U for which
103 /// retaining or releasing U is equivalent to retaining or releasing V. In other
104 /// words, ARC operations on \p V are equivalent to ARC operations on \p U.
106 /// We use this in the ARC optimizer to make it easier to match up ARC
107 /// operations by always mapping ARC operations to RCIdentityRoots instead of
108 /// pointers themselves.
110 /// The two ways that we see RCIdentical values in ObjC are via:
113 /// 2. Forwarding Calls that return their argument verbatim.
115 /// Thus this function strips off pointer casts and forwarding calls. *NOTE*
116 /// This implies that two RCIdentical values must alias.
117 inline const Value *GetRCIdentityRoot(const Value *V) {
119 V = V->stripPointerCasts();
120 if (!IsForwarding(GetBasicARCInstKind(V)))
122 V = cast<CallInst>(V)->getArgOperand(0);
127 /// Helper which calls const Value *GetRCIdentityRoot(const Value *V) and just
128 /// casts away the const of the result. For documentation about what an
129 /// RCIdentityRoot (and by extension GetRCIdentityRoot is) look at that
131 inline Value *GetRCIdentityRoot(Value *V) {
132 return const_cast<Value *>(GetRCIdentityRoot((const Value *)V));
135 /// Assuming the given instruction is one of the special calls such as
136 /// objc_retain or objc_release, return the RCIdentity root of the argument of
138 inline Value *GetArgRCIdentityRoot(Value *Inst) {
139 return GetRCIdentityRoot(cast<CallInst>(Inst)->getArgOperand(0));
142 inline bool IsNullOrUndef(const Value *V) {
143 return isa<ConstantPointerNull>(V) || isa<UndefValue>(V);
146 inline bool IsNoopInstruction(const Instruction *I) {
147 return isa<BitCastInst>(I) ||
148 (isa<GetElementPtrInst>(I) &&
149 cast<GetElementPtrInst>(I)->hasAllZeroIndices());
152 /// Test whether the given value is possible a retainable object pointer.
153 inline bool IsPotentialRetainableObjPtr(const Value *Op) {
154 // Pointers to static or stack storage are not valid retainable object
156 if (isa<Constant>(Op) || isa<AllocaInst>(Op))
158 // Special arguments can not be a valid retainable object pointer.
159 if (const Argument *Arg = dyn_cast<Argument>(Op))
160 if (Arg->hasByValAttr() ||
161 Arg->hasInAllocaAttr() ||
162 Arg->hasNestAttr() ||
163 Arg->hasStructRetAttr())
165 // Only consider values with pointer types.
167 // It seemes intuitive to exclude function pointer types as well, since
168 // functions are never retainable object pointers, however clang occasionally
169 // bitcasts retainable object pointers to function-pointer type temporarily.
170 PointerType *Ty = dyn_cast<PointerType>(Op->getType());
173 // Conservatively assume anything else is a potential retainable object
178 inline bool IsPotentialRetainableObjPtr(const Value *Op,
180 // First make the rudimentary check.
181 if (!IsPotentialRetainableObjPtr(Op))
184 // Objects in constant memory are not reference-counted.
185 if (AA.pointsToConstantMemory(Op))
188 // Pointers in constant memory are not pointing to reference-counted objects.
189 if (const LoadInst *LI = dyn_cast<LoadInst>(Op))
190 if (AA.pointsToConstantMemory(LI->getPointerOperand()))
193 // Otherwise assume the worst.
197 /// Helper for GetARCInstKind. Determines what kind of construct CS
199 inline ARCInstKind GetCallSiteClass(ImmutableCallSite CS) {
200 for (ImmutableCallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end();
202 if (IsPotentialRetainableObjPtr(*I))
203 return CS.onlyReadsMemory() ? ARCInstKind::User : ARCInstKind::CallOrUser;
205 return CS.onlyReadsMemory() ? ARCInstKind::None : ARCInstKind::Call;
208 /// Return true if this value refers to a distinct and identifiable
211 /// This is similar to AliasAnalysis's isIdentifiedObject, except that it uses
212 /// special knowledge of ObjC conventions.
213 inline bool IsObjCIdentifiedObject(const Value *V) {
214 // Assume that call results and arguments have their own "provenance".
215 // Constants (including GlobalVariables) and Allocas are never
216 // reference-counted.
217 if (isa<CallInst>(V) || isa<InvokeInst>(V) ||
218 isa<Argument>(V) || isa<Constant>(V) ||
222 if (const LoadInst *LI = dyn_cast<LoadInst>(V)) {
223 const Value *Pointer =
224 GetRCIdentityRoot(LI->getPointerOperand());
225 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Pointer)) {
226 // A constant pointer can't be pointing to an object on the heap. It may
227 // be reference-counted, but it won't be deleted.
228 if (GV->isConstant())
230 StringRef Name = GV->getName();
231 // These special variables are known to hold values which are not
232 // reference-counted pointers.
233 if (Name.startswith("\01l_objc_msgSend_fixup_"))
236 StringRef Section = GV->getSection();
237 if (Section.find("__message_refs") != StringRef::npos ||
238 Section.find("__objc_classrefs") != StringRef::npos ||
239 Section.find("__objc_superrefs") != StringRef::npos ||
240 Section.find("__objc_methname") != StringRef::npos ||
241 Section.find("__cstring") != StringRef::npos)
249 enum class ARCMDKindID {
255 /// A cache of MDKinds used by various ARC optimizations.
256 class ARCMDKindCache {
259 /// The Metadata Kind for clang.imprecise_release metadata.
260 llvm::Optional<unsigned> ImpreciseReleaseMDKind;
262 /// The Metadata Kind for clang.arc.copy_on_escape metadata.
263 llvm::Optional<unsigned> CopyOnEscapeMDKind;
265 /// The Metadata Kind for clang.arc.no_objc_arc_exceptions metadata.
266 llvm::Optional<unsigned> NoObjCARCExceptionsMDKind;
269 void init(Module *Mod) {
271 ImpreciseReleaseMDKind = NoneType::None;
272 CopyOnEscapeMDKind = NoneType::None;
273 NoObjCARCExceptionsMDKind = NoneType::None;
276 unsigned get(ARCMDKindID ID) {
278 case ARCMDKindID::ImpreciseRelease:
279 if (!ImpreciseReleaseMDKind)
280 ImpreciseReleaseMDKind =
281 M->getContext().getMDKindID("clang.imprecise_release");
282 return *ImpreciseReleaseMDKind;
283 case ARCMDKindID::CopyOnEscape:
284 if (!CopyOnEscapeMDKind)
286 M->getContext().getMDKindID("clang.arc.copy_on_escape");
287 return *CopyOnEscapeMDKind;
288 case ARCMDKindID::NoObjCARCExceptions:
289 if (!NoObjCARCExceptionsMDKind)
290 NoObjCARCExceptionsMDKind =
291 M->getContext().getMDKindID("clang.arc.no_objc_arc_exceptions");
292 return *NoObjCARCExceptionsMDKind;
294 llvm_unreachable("Covered switch isn't covered?!");
298 } // end namespace objcarc
299 } // end namespace llvm