LLVM  6.0.0svn
ObjCARCAnalysisUtils.h
Go to the documentation of this file.
1 //===- ObjCARCAnalysisUtils.h - ObjC ARC Analysis Utilities -----*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 /// \file
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.
13 ///
14 /// WARNING: This file knows about certain library functions. It recognizes them
15 /// by name, and hardwires knowledge of their semantics.
16 ///
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.
20 ///
21 //===----------------------------------------------------------------------===//
22 
23 #ifndef LLVM_LIB_ANALYSIS_OBJCARCANALYSISUTILS_H
24 #define LLVM_LIB_ANALYSIS_OBJCARCANALYSISUTILS_H
25 
26 #include "llvm/ADT/Optional.h"
27 #include "llvm/ADT/StringSwitch.h"
30 #include "llvm/Analysis/Passes.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/Pass.h"
38 
39 namespace llvm {
40 class raw_ostream;
41 }
42 
43 namespace llvm {
44 namespace objcarc {
45 
46 /// \brief A handy option to enable/disable all ARC Optimizations.
47 extern bool EnableARCOpts;
48 
49 /// \brief 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("objc_retain") ||
54  M.getNamedValue("objc_release") ||
55  M.getNamedValue("objc_autorelease") ||
56  M.getNamedValue("objc_retainAutoreleasedReturnValue") ||
57  M.getNamedValue("objc_unsafeClaimAutoreleasedReturnValue") ||
58  M.getNamedValue("objc_retainBlock") ||
59  M.getNamedValue("objc_autoreleaseReturnValue") ||
60  M.getNamedValue("objc_autoreleasePoolPush") ||
61  M.getNamedValue("objc_loadWeakRetained") ||
62  M.getNamedValue("objc_loadWeak") ||
63  M.getNamedValue("objc_destroyWeak") ||
64  M.getNamedValue("objc_storeWeak") ||
65  M.getNamedValue("objc_initWeak") ||
66  M.getNamedValue("objc_moveWeak") ||
67  M.getNamedValue("objc_copyWeak") ||
68  M.getNamedValue("objc_retainedObject") ||
69  M.getNamedValue("objc_unretainedObject") ||
70  M.getNamedValue("objc_unretainedPointer") ||
71  M.getNamedValue("clang.arc.use");
72 }
73 
74 /// \brief 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 /// The RCIdentity root of a value \p V is a dominating value U for which
90 /// retaining or releasing U is equivalent to retaining or releasing V. In other
91 /// words, ARC operations on \p V are equivalent to ARC operations on \p U.
92 ///
93 /// We use this in the ARC optimizer to make it easier to match up ARC
94 /// operations by always mapping ARC operations to RCIdentityRoots instead of
95 /// pointers themselves.
96 ///
97 /// The two ways that we see RCIdentical values in ObjC are via:
98 ///
99 /// 1. PointerCasts
100 /// 2. Forwarding Calls that return their argument verbatim.
101 ///
102 /// Thus this function strips off pointer casts and forwarding calls. *NOTE*
103 /// This implies that two RCIdentical values must alias.
104 inline const Value *GetRCIdentityRoot(const Value *V) {
105  for (;;) {
106  V = V->stripPointerCasts();
108  break;
109  V = cast<CallInst>(V)->getArgOperand(0);
110  }
111  return V;
112 }
113 
114 /// Helper which calls const Value *GetRCIdentityRoot(const Value *V) and just
115 /// casts away the const of the result. For documentation about what an
116 /// RCIdentityRoot (and by extension GetRCIdentityRoot is) look at that
117 /// function.
119  return const_cast<Value *>(GetRCIdentityRoot((const Value *)V));
120 }
121 
122 /// \brief Assuming the given instruction is one of the special calls such as
123 /// objc_retain or objc_release, return the RCIdentity root of the argument of
124 /// the call.
126  return GetRCIdentityRoot(cast<CallInst>(Inst)->getArgOperand(0));
127 }
128 
129 inline bool IsNullOrUndef(const Value *V) {
130  return isa<ConstantPointerNull>(V) || isa<UndefValue>(V);
131 }
132 
133 inline bool IsNoopInstruction(const Instruction *I) {
134  return isa<BitCastInst>(I) ||
135  (isa<GetElementPtrInst>(I) &&
136  cast<GetElementPtrInst>(I)->hasAllZeroIndices());
137 }
138 
139 /// \brief Test whether the given value is possible a retainable object pointer.
140 inline bool IsPotentialRetainableObjPtr(const Value *Op) {
141  // Pointers to static or stack storage are not valid retainable object
142  // pointers.
143  if (isa<Constant>(Op) || isa<AllocaInst>(Op))
144  return false;
145  // Special arguments can not be a valid retainable object pointer.
146  if (const Argument *Arg = dyn_cast<Argument>(Op))
147  if (Arg->hasByValAttr() ||
148  Arg->hasInAllocaAttr() ||
149  Arg->hasNestAttr() ||
150  Arg->hasStructRetAttr())
151  return false;
152  // Only consider values with pointer types.
153  //
154  // It seemes intuitive to exclude function pointer types as well, since
155  // functions are never retainable object pointers, however clang occasionally
156  // bitcasts retainable object pointers to function-pointer type temporarily.
157  PointerType *Ty = dyn_cast<PointerType>(Op->getType());
158  if (!Ty)
159  return false;
160  // Conservatively assume anything else is a potential retainable object
161  // pointer.
162  return true;
163 }
164 
166  AliasAnalysis &AA) {
167  // First make the rudimentary check.
169  return false;
170 
171  // Objects in constant memory are not reference-counted.
172  if (AA.pointsToConstantMemory(Op))
173  return false;
174 
175  // Pointers in constant memory are not pointing to reference-counted objects.
176  if (const LoadInst *LI = dyn_cast<LoadInst>(Op))
177  if (AA.pointsToConstantMemory(LI->getPointerOperand()))
178  return false;
179 
180  // Otherwise assume the worst.
181  return true;
182 }
183 
184 /// \brief Helper for GetARCInstKind. Determines what kind of construct CS
185 /// is.
188  I != E; ++I)
191 
193 }
194 
195 /// \brief Return true if this value refers to a distinct and identifiable
196 /// object.
197 ///
198 /// This is similar to AliasAnalysis's isIdentifiedObject, except that it uses
199 /// special knowledge of ObjC conventions.
200 inline bool IsObjCIdentifiedObject(const Value *V) {
201  // Assume that call results and arguments have their own "provenance".
202  // Constants (including GlobalVariables) and Allocas are never
203  // reference-counted.
204  if (isa<CallInst>(V) || isa<InvokeInst>(V) ||
205  isa<Argument>(V) || isa<Constant>(V) ||
206  isa<AllocaInst>(V))
207  return true;
208 
209  if (const LoadInst *LI = dyn_cast<LoadInst>(V)) {
210  const Value *Pointer =
211  GetRCIdentityRoot(LI->getPointerOperand());
212  if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Pointer)) {
213  // A constant pointer can't be pointing to an object on the heap. It may
214  // be reference-counted, but it won't be deleted.
215  if (GV->isConstant())
216  return true;
217  StringRef Name = GV->getName();
218  // These special variables are known to hold values which are not
219  // reference-counted pointers.
220  if (Name.startswith("\01l_objc_msgSend_fixup_"))
221  return true;
222 
223  StringRef Section = GV->getSection();
224  if (Section.find("__message_refs") != StringRef::npos ||
225  Section.find("__objc_classrefs") != StringRef::npos ||
226  Section.find("__objc_superrefs") != StringRef::npos ||
227  Section.find("__objc_methname") != StringRef::npos ||
228  Section.find("__cstring") != StringRef::npos)
229  return true;
230  }
231  }
232 
233  return false;
234 }
235 
236 enum class ARCMDKindID {
238  CopyOnEscape,
240 };
241 
242 /// A cache of MDKinds used by various ARC optimizations.
244  Module *M;
245 
246  /// The Metadata Kind for clang.imprecise_release metadata.
247  llvm::Optional<unsigned> ImpreciseReleaseMDKind;
248 
249  /// The Metadata Kind for clang.arc.copy_on_escape metadata.
250  llvm::Optional<unsigned> CopyOnEscapeMDKind;
251 
252  /// The Metadata Kind for clang.arc.no_objc_arc_exceptions metadata.
253  llvm::Optional<unsigned> NoObjCARCExceptionsMDKind;
254 
255 public:
256  void init(Module *Mod) {
257  M = Mod;
258  ImpreciseReleaseMDKind = NoneType::None;
259  CopyOnEscapeMDKind = NoneType::None;
260  NoObjCARCExceptionsMDKind = NoneType::None;
261  }
262 
263  unsigned get(ARCMDKindID ID) {
264  switch (ID) {
266  if (!ImpreciseReleaseMDKind)
267  ImpreciseReleaseMDKind =
268  M->getContext().getMDKindID("clang.imprecise_release");
269  return *ImpreciseReleaseMDKind;
271  if (!CopyOnEscapeMDKind)
272  CopyOnEscapeMDKind =
273  M->getContext().getMDKindID("clang.arc.copy_on_escape");
274  return *CopyOnEscapeMDKind;
276  if (!NoObjCARCExceptionsMDKind)
277  NoObjCARCExceptionsMDKind =
278  M->getContext().getMDKindID("clang.arc.no_objc_arc_exceptions");
279  return *NoObjCARCExceptionsMDKind;
280  }
281  llvm_unreachable("Covered switch isn't covered?!");
282  }
283 };
284 
285 } // end namespace objcarc
286 } // end namespace llvm
287 
288 #endif
A parsed version of the target data layout string in and methods for querying it. ...
Definition: DataLayout.h:109
This class represents an incoming formal argument to a Function.
Definition: Argument.h:30
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
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:63
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:164
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.
bool onlyReadsMemory() const
Determine if the call does not access or only reads memory.
Definition: CallSite.h:454
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:237
A Use represents the edge between a Value definition and its users.
Definition: Use.h:56
IterTy arg_end() const
Definition: CallSite.h:575
bool IsNullOrUndef(const Value *V)
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:267
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:112
bool ModuleHasARC(const Module &M)
Test if the given module looks interesting to run ARC optimization on.
Class to represent pointers.
Definition: DerivedTypes.h:467
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...
A cache of MDKinds used by various ARC optimizations.
const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs, and aliases.
Definition: Value.cpp:558
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.
IterTy arg_begin() const
Definition: CallSite.h:571
ARCInstKind
Equivalence classes of instructions in the ARC Model.
Module.h This file contains the declarations for the Module class.
amdgpu Simplify well known AMD library false Value Value * Arg
could "use" a pointer
static const size_t npos
Definition: StringRef.h:51
Establish a view to a call site for examination.
Definition: CallSite.h:713
#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:323
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...
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:49
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t find(char C, size_t From=0) const
Search for the first character C in the string.
Definition: StringRef.h:298
bool IsNoopInstruction(const Instruction *I)