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"
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"
38
39namespace llvm {
40class raw_ostream;
41}
42
43namespace llvm {
44namespace objcarc {
45
46/// A handy option to enable/disable all ARC Optimizations.
47extern bool EnableARCOpts;
48
49/// Test if the given module looks interesting to run ARC optimization
50/// on.
51inline 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.
77inline const Value *GetUnderlyingObjCPtr(const Value *V,
78                                                const DataLayout &DL) {
79  for (;;) {
80    V = GetUnderlyingObject(V, DL);
81    if (!IsForwarding(GetBasicARCInstKind(V)))
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.
90inline const Value *
91GetUnderlyingObjCPtrCached(const Value *V, const DataLayout &DL,
92                           DenseMap<const Value *, WeakTrackingVH> &Cache) {
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.
116inline const Value *GetRCIdentityRoot(const Value *V) {
117  for (;;) {
118    V = V->stripPointerCasts();
119    if (!IsForwarding(GetBasicARCInstKind(V)))
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.
130inline Value *GetRCIdentityRoot(Value *V) {
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.
137inline Value *GetArgRCIdentityRoot(Value *Inst) {
138  return GetRCIdentityRoot(cast<CallInst>(Inst)->getArgOperand(0));
139}
140
141inline bool IsNullOrUndef(const Value *V) {
142  return isa<ConstantPointerNull>(V) || isa<UndefValue>(V);
143}
144
145inline 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.
152inline 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
177inline bool IsPotentialRetainableObjPtr(const Value *Op,
178                                               AliasAnalysis &AA) {
179  // First make the rudimentary check.
180  if (!IsPotentialRetainableObjPtr(Op))
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.
198inline ARCInstKind GetCallSiteClass(ImmutableCallSite CS) {
199  for (ImmutableCallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end();
200       I != E; ++I)
201    if (IsPotentialRetainableObjPtr(*I))
202      return CS.onlyReadsMemory() ? ARCInstKind::User : ARCInstKind::CallOrUser;
203
204  return CS.onlyReadsMemory() ? ARCInstKind::None : ARCInstKind::Call;
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.
212inline 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
248enum class ARCMDKindID {
249  ImpreciseRelease,
250  CopyOnEscape,
251  NoObjCARCExceptions,
252};
253
254/// A cache of MDKinds used by various ARC optimizations.
255class ARCMDKindCache {
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
267public:
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) {
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)
284        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;
292    }
293    llvm_unreachable("Covered switch isn't covered?!");
294  }
295};
296
297} // end namespace objcarc
298} // end namespace llvm
299
300#endif
301