LLVM  10.0.0svn
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_LIB_ANALYSIS_OBJCARCANALYSISUTILS_H
23 #define LLVM_LIB_ANALYSIS_OBJCARCANALYSISUTILS_H
24 
25 #include "llvm/ADT/Optional.h"
26 #include "llvm/ADT/StringSwitch.h"
29 #include "llvm/Analysis/Passes.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"
38 
39 namespace llvm {
40 class raw_ostream;
41 }
42 
43 namespace llvm {
44 namespace objcarc {
45 
46 /// A handy option to enable/disable all ARC Optimizations.
47 extern bool EnableARCOpts;
48 
49 /// Test if the given module looks interesting to run ARC optimization
50 /// on.
51 inline bool ModuleHasARC(const Module &M) {
52  return
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");
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.
77 inline const Value *GetUnderlyingObjCPtr(const Value *V,
78  const DataLayout &DL) {
79  for (;;) {
80  V = GetUnderlyingObject(V, DL);
82  break;
83  V = cast<CallInst>(V)->getArgOperand(0);
84  }
85 
86  return V;
87 }
88 
89 /// A wrapper for GetUnderlyingObjCPtr used for results memoization.
90 inline const Value *
93  if (auto InCache = Cache.lookup(V))
94  return InCache;
95 
96  const Value *Computed = GetUnderlyingObjCPtr(V, DL);
97  Cache[V] = const_cast<Value *>(Computed);
98  return Computed;
99 }
100 
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.
104 ///
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.
108 ///
109 /// The two ways that we see RCIdentical values in ObjC are via:
110 ///
111 /// 1. PointerCasts
112 /// 2. Forwarding Calls that return their argument verbatim.
113 ///
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) {
117  for (;;) {
118  V = V->stripPointerCasts();
120  break;
121  V = cast<CallInst>(V)->getArgOperand(0);
122  }
123  return V;
124 }
125 
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
129 /// function.
131  return const_cast<Value *>(GetRCIdentityRoot((const Value *)V));
132 }
133 
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
136 /// the call.
138  return GetRCIdentityRoot(cast<CallInst>(Inst)->getArgOperand(0));
139 }
140 
141 inline bool IsNullOrUndef(const Value *V) {
142  return isa<ConstantPointerNull>(V) || isa<UndefValue>(V);
143 }
144 
145 inline bool IsNoopInstruction(const Instruction *I) {
146  return isa<BitCastInst>(I) ||
147  (isa<GetElementPtrInst>(I) &&
148  cast<GetElementPtrInst>(I)->hasAllZeroIndices());
149 }
150 
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
154  // pointers.
155  if (isa<Constant>(Op) || isa<AllocaInst>(Op))
156  return false;
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())
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  AliasAnalysis &AA) {
179  // First make the rudimentary check.
181  return false;
182 
183  // Objects in constant memory are not reference-counted.
184  if (AA.pointsToConstantMemory(Op))
185  return false;
186 
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()))
190  return false;
191 
192  // Otherwise assume the worst.
193  return true;
194 }
195 
196 /// Helper for GetARCInstKind. Determines what kind of construct CS
197 /// is.
200  I != E; ++I)
203 
205 }
206 
207 /// Return true if this value refers to a distinct and identifiable
208 /// object.
209 ///
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) ||
218  isa<AllocaInst>(V))
219  return true;
220 
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())
228  return true;
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_"))
233  return true;
234 
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)
241  return true;
242  }
243  }
244 
245  return false;
246 }
247 
248 enum class ARCMDKindID {
250  CopyOnEscape,
252 };
253 
254 /// A cache of MDKinds used by various ARC optimizations.
256  Module *M;
257 
258  /// The Metadata Kind for clang.imprecise_release metadata.
259  llvm::Optional<unsigned> ImpreciseReleaseMDKind;
260 
261  /// The Metadata Kind for clang.arc.copy_on_escape metadata.
262  llvm::Optional<unsigned> CopyOnEscapeMDKind;
263 
264  /// The Metadata Kind for clang.arc.no_objc_arc_exceptions metadata.
265  llvm::Optional<unsigned> NoObjCARCExceptionsMDKind;
266 
267 public:
268  void init(Module *Mod) {
269  M = Mod;
270  ImpreciseReleaseMDKind = NoneType::None;
271  CopyOnEscapeMDKind = NoneType::None;
272  NoObjCARCExceptionsMDKind = NoneType::None;
273  }
274 
275  unsigned get(ARCMDKindID ID) {
276  switch (ID) {
278  if (!ImpreciseReleaseMDKind)
279  ImpreciseReleaseMDKind =
280  M->getContext().getMDKindID("clang.imprecise_release");
281  return *ImpreciseReleaseMDKind;
283  if (!CopyOnEscapeMDKind)
284  CopyOnEscapeMDKind =
285  M->getContext().getMDKindID("clang.arc.copy_on_escape");
286  return *CopyOnEscapeMDKind;
288  if (!NoObjCARCExceptionsMDKind)
289  NoObjCARCExceptionsMDKind =
290  M->getContext().getMDKindID("clang.arc.no_objc_arc_exceptions");
291  return *NoObjCARCExceptionsMDKind;
292  }
293  llvm_unreachable("Covered switch isn't covered?!");
294  }
295 };
296 
297 } // end namespace objcarc
298 } // end namespace llvm
299 
300 #endif
IterTy arg_end() const
Definition: CallSite.h:588
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:111
This class represents an incoming formal argument to a Function.
Definition: Argument.h:29
IterTy arg_begin() const
Definition: CallSite.h:584
This class represents lattice values for constants.
Definition: AllocatorList.h:23
could call objc_release and/or "use" pointers
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
LLVM_NODISCARD bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:270
Value * GetArgRCIdentityRoot(Value *Inst)
Assuming the given instruction is one of the special calls such as objc_retain or objc_release...
could call objc_release
An instruction for reading from memory.
Definition: Instructions.h:167
bool IsForwarding(ARCInstKind Class)
Test if the given class represents instructions which return their argument verbatim.
bool IsObjCIdentifiedObject(const Value *V)
Return true if this value refers to a distinct and identifiable object.
unsigned getMDKindID(StringRef Name) const
getMDKindID - Return a unique non-zero ID for the specified metadata kind.
LLVMContext & getContext() const
Get the global data context.
Definition: Module.h:244
A Use represents the edge between a Value definition and its users.
Definition: Use.h:55
bool IsNullOrUndef(const Value *V)
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:245
bool IsPotentialRetainableObjPtr(const Value *Op)
Test whether the given value is possible a retainable object pointer.
bool EnableARCOpts
A handy option to enable/disable all ARC Optimizations.
GlobalValue * getNamedValue(StringRef Name) const
Return the global value in the module with the specified name, of arbitrary type. ...
Definition: Module.cpp:113
bool ModuleHasARC(const Module &M)
Test if the given module looks interesting to run ARC optimization on.
bool onlyReadsMemory() const
Determine if the call does not access or only reads memory.
Definition: CallSite.h:467
Class to represent pointers.
Definition: DerivedTypes.h:544
bool pointsToConstantMemory(const MemoryLocation &Loc, bool OrLocal=false)
Checks whether the given location points to constant memory, or if OrLocal is true whether it points ...
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
LLVM_NODISCARD size_t find(char C, size_t From=0) const
Search for the first character C in the string.
Definition: StringRef.h:299
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
A cache of MDKinds used by various ARC optimizations.
const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs and address space casts.
Definition: Value.cpp:525
anything that is inert from an ARC perspective.
Value * GetUnderlyingObject(Value *V, const DataLayout &DL, unsigned MaxLookup=6)
This method strips off any GEP address adjustments and pointer casts from the specified value...
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
ARCInstKind GetBasicARCInstKind(const Value *V)
Determine which objc runtime call instruction class V belongs to.
ARCInstKind
Equivalence classes of instructions in the ARC Model.
Module.h This file contains the declarations for the Module class.
const Value * GetUnderlyingObjCPtrCached(const Value *V, const DataLayout &DL, DenseMap< const Value *, WeakTrackingVH > &Cache)
A wrapper for GetUnderlyingObjCPtr used for results memoization.
could "use" a pointer
static const size_t npos
Definition: StringRef.h:50
Establish a view to a call site for examination.
Definition: CallSite.h:897
#define I(x, y, z)
Definition: MD5.cpp:58
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:332
ARCInstKind GetCallSiteClass(ImmutableCallSite CS)
Helper for GetARCInstKind.
const Value * GetUnderlyingObjCPtr(const Value *V, const DataLayout &DL)
This is a wrapper around getUnderlyingObject which also knows how to look through objc_retain and obj...
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
Definition: DenseMap.h:185
LLVM Value Representation.
Definition: Value.h:73
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...
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
bool IsNoopInstruction(const Instruction *I)