Michael Gottesman | 6eb95dc | 2013-07-10 18:49:00 +0000 | [diff] [blame] | 1 | //===- ObjCARC.h - ObjC ARC Optimization --------------*- C++ -*-----------===// |
Michael Gottesman | 08904e3 | 2013-01-28 03:28:38 +0000 | [diff] [blame] | 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 definitions/declarations used by the ObjC ARC |
| 11 | /// Optimizer. ARC stands for Automatic Reference Counting and is a system for |
| 12 | /// managing 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 | |
Benjamin Kramer | a7c40ef | 2014-08-13 16:26:38 +0000 | [diff] [blame] | 23 | #ifndef LLVM_LIB_TRANSFORMS_OBJCARC_OBJCARC_H |
| 24 | #define LLVM_LIB_TRANSFORMS_OBJCARC_OBJCARC_H |
Michael Gottesman | 08904e3 | 2013-01-28 03:28:38 +0000 | [diff] [blame] | 25 | |
| 26 | #include "llvm/ADT/StringSwitch.h" |
| 27 | #include "llvm/Analysis/AliasAnalysis.h" |
| 28 | #include "llvm/Analysis/Passes.h" |
Michael Gottesman | 294e7da | 2013-01-28 05:51:54 +0000 | [diff] [blame] | 29 | #include "llvm/Analysis/ValueTracking.h" |
Chandler Carruth | 219b89b | 2014-03-04 11:01:28 +0000 | [diff] [blame] | 30 | #include "llvm/IR/CallSite.h" |
Chandler Carruth | 8394857 | 2014-03-04 10:30:26 +0000 | [diff] [blame] | 31 | #include "llvm/IR/InstIterator.h" |
Michael Gottesman | 08904e3 | 2013-01-28 03:28:38 +0000 | [diff] [blame] | 32 | #include "llvm/IR/Module.h" |
| 33 | #include "llvm/Pass.h" |
Michael Gottesman | 08904e3 | 2013-01-28 03:28:38 +0000 | [diff] [blame] | 34 | #include "llvm/Transforms/ObjCARC.h" |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 35 | #include "llvm/Transforms/Utils/Local.h" |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame^] | 36 | #include "ARCInstKind.h" |
Michael Gottesman | 08904e3 | 2013-01-28 03:28:38 +0000 | [diff] [blame] | 37 | |
| 38 | namespace llvm { |
Michael Gottesman | 13a5f1a | 2013-01-29 04:51:59 +0000 | [diff] [blame] | 39 | class raw_ostream; |
| 40 | } |
| 41 | |
| 42 | namespace llvm { |
Michael Gottesman | 08904e3 | 2013-01-28 03:28:38 +0000 | [diff] [blame] | 43 | namespace objcarc { |
| 44 | |
| 45 | /// \brief A handy option to enable/disable all ARC Optimizations. |
| 46 | extern bool EnableARCOpts; |
| 47 | |
| 48 | /// \brief Test if the given module looks interesting to run ARC optimization |
| 49 | /// on. |
| 50 | static inline bool ModuleHasARC(const Module &M) { |
| 51 | return |
| 52 | M.getNamedValue("objc_retain") || |
| 53 | M.getNamedValue("objc_release") || |
| 54 | M.getNamedValue("objc_autorelease") || |
| 55 | M.getNamedValue("objc_retainAutoreleasedReturnValue") || |
| 56 | M.getNamedValue("objc_retainBlock") || |
| 57 | M.getNamedValue("objc_autoreleaseReturnValue") || |
| 58 | M.getNamedValue("objc_autoreleasePoolPush") || |
| 59 | M.getNamedValue("objc_loadWeakRetained") || |
| 60 | M.getNamedValue("objc_loadWeak") || |
| 61 | M.getNamedValue("objc_destroyWeak") || |
| 62 | M.getNamedValue("objc_storeWeak") || |
| 63 | M.getNamedValue("objc_initWeak") || |
| 64 | M.getNamedValue("objc_moveWeak") || |
| 65 | M.getNamedValue("objc_copyWeak") || |
| 66 | M.getNamedValue("objc_retainedObject") || |
| 67 | M.getNamedValue("objc_unretainedObject") || |
Michael Gottesman | 3b8f877 | 2013-03-29 21:15:23 +0000 | [diff] [blame] | 68 | M.getNamedValue("objc_unretainedPointer") || |
| 69 | M.getNamedValue("clang.arc.use"); |
Michael Gottesman | 08904e3 | 2013-01-28 03:28:38 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Michael Gottesman | 294e7da | 2013-01-28 05:51:54 +0000 | [diff] [blame] | 72 | /// \brief This is a wrapper around getUnderlyingObject which also knows how to |
| 73 | /// look through objc_retain and objc_autorelease calls, which we know to return |
| 74 | /// their argument verbatim. |
| 75 | static inline const Value *GetUnderlyingObjCPtr(const Value *V) { |
| 76 | for (;;) { |
| 77 | V = GetUnderlyingObject(V); |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame^] | 78 | if (!IsForwarding(GetBasicARCInstKind(V))) |
Michael Gottesman | 294e7da | 2013-01-28 05:51:54 +0000 | [diff] [blame] | 79 | break; |
| 80 | V = cast<CallInst>(V)->getArgOperand(0); |
| 81 | } |
| 82 | |
| 83 | return V; |
| 84 | } |
| 85 | |
Michael Gottesman | e5ad66f | 2015-02-19 00:42:38 +0000 | [diff] [blame] | 86 | /// The RCIdentity root of a value \p V is a dominating value U for which |
| 87 | /// retaining or releasing U is equivalent to retaining or releasing V. In other |
| 88 | /// words, ARC operations on \p V are equivalent to ARC operations on \p U. |
| 89 | /// |
| 90 | /// We use this in the ARC optimizer to make it easier to match up ARC |
| 91 | /// operations by always mapping ARC operations to RCIdentityRoots instead of |
| 92 | /// pointers themselves. |
| 93 | /// |
| 94 | /// The two ways that we see RCIdentical values in ObjC are via: |
| 95 | /// |
| 96 | /// 1. PointerCasts |
| 97 | /// 2. Forwarding Calls that return their argument verbatim. |
| 98 | /// |
| 99 | /// Thus this function strips off pointer casts and forwarding calls. *NOTE* |
| 100 | /// This implies that two RCIdentical values must alias. |
| 101 | static inline const Value *GetRCIdentityRoot(const Value *V) { |
Michael Gottesman | 294e7da | 2013-01-28 05:51:54 +0000 | [diff] [blame] | 102 | for (;;) { |
| 103 | V = V->stripPointerCasts(); |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame^] | 104 | if (!IsForwarding(GetBasicARCInstKind(V))) |
Michael Gottesman | 294e7da | 2013-01-28 05:51:54 +0000 | [diff] [blame] | 105 | break; |
| 106 | V = cast<CallInst>(V)->getArgOperand(0); |
| 107 | } |
| 108 | return V; |
| 109 | } |
| 110 | |
Michael Gottesman | e5ad66f | 2015-02-19 00:42:38 +0000 | [diff] [blame] | 111 | /// Helper which calls const Value *GetRCIdentityRoot(const Value *V) and just |
| 112 | /// casts away the const of the result. For documentation about what an |
| 113 | /// RCIdentityRoot (and by extension GetRCIdentityRoot is) look at that |
| 114 | /// function. |
| 115 | static inline Value *GetRCIdentityRoot(Value *V) { |
| 116 | return const_cast<Value *>(GetRCIdentityRoot((const Value *)V)); |
Michael Gottesman | 294e7da | 2013-01-28 05:51:54 +0000 | [diff] [blame] | 117 | } |
| 118 | |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 119 | /// \brief Assuming the given instruction is one of the special calls such as |
Michael Gottesman | e5ad66f | 2015-02-19 00:42:38 +0000 | [diff] [blame] | 120 | /// objc_retain or objc_release, return the RCIdentity root of the argument of |
| 121 | /// the call. |
| 122 | static inline Value *GetArgRCIdentityRoot(Value *Inst) { |
| 123 | return GetRCIdentityRoot(cast<CallInst>(Inst)->getArgOperand(0)); |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Michael Gottesman | 65c2481 | 2013-03-25 09:27:43 +0000 | [diff] [blame] | 126 | static inline bool IsNullOrUndef(const Value *V) { |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 127 | return isa<ConstantPointerNull>(V) || isa<UndefValue>(V); |
| 128 | } |
| 129 | |
Michael Gottesman | 65c2481 | 2013-03-25 09:27:43 +0000 | [diff] [blame] | 130 | static inline bool IsNoopInstruction(const Instruction *I) { |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 131 | return isa<BitCastInst>(I) || |
| 132 | (isa<GetElementPtrInst>(I) && |
| 133 | cast<GetElementPtrInst>(I)->hasAllZeroIndices()); |
| 134 | } |
| 135 | |
| 136 | |
| 137 | /// \brief Erase the given instruction. |
| 138 | /// |
| 139 | /// Many ObjC calls return their argument verbatim, |
| 140 | /// so if it's such a call and the return value has users, replace them with the |
| 141 | /// argument value. |
| 142 | /// |
| 143 | static inline void EraseInstruction(Instruction *CI) { |
| 144 | Value *OldArg = cast<CallInst>(CI)->getArgOperand(0); |
| 145 | |
| 146 | bool Unused = CI->use_empty(); |
| 147 | |
| 148 | if (!Unused) { |
| 149 | // Replace the return value with the argument. |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame^] | 150 | assert((IsForwarding(GetBasicARCInstKind(CI)) || |
| 151 | (IsNoopOnNull(GetBasicARCInstKind(CI)) && |
Michael Gottesman | c1b648f | 2013-07-08 23:30:23 +0000 | [diff] [blame] | 152 | isa<ConstantPointerNull>(OldArg))) && |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 153 | "Can't delete non-forwarding instruction with users!"); |
| 154 | CI->replaceAllUsesWith(OldArg); |
| 155 | } |
| 156 | |
| 157 | CI->eraseFromParent(); |
| 158 | |
| 159 | if (Unused) |
| 160 | RecursivelyDeleteTriviallyDeadInstructions(OldArg); |
| 161 | } |
| 162 | |
| 163 | /// \brief Test whether the given value is possible a retainable object pointer. |
| 164 | static inline bool IsPotentialRetainableObjPtr(const Value *Op) { |
Michael Gottesman | 23a1ee5 | 2013-01-29 04:58:30 +0000 | [diff] [blame] | 165 | // Pointers to static or stack storage are not valid retainable object |
| 166 | // pointers. |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 167 | if (isa<Constant>(Op) || isa<AllocaInst>(Op)) |
| 168 | return false; |
| 169 | // Special arguments can not be a valid retainable object pointer. |
| 170 | if (const Argument *Arg = dyn_cast<Argument>(Op)) |
| 171 | if (Arg->hasByValAttr() || |
Reid Kleckner | 26af2ca | 2014-01-28 02:38:36 +0000 | [diff] [blame] | 172 | Arg->hasInAllocaAttr() || |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 173 | Arg->hasNestAttr() || |
| 174 | Arg->hasStructRetAttr()) |
| 175 | return false; |
| 176 | // Only consider values with pointer types. |
| 177 | // |
| 178 | // It seemes intuitive to exclude function pointer types as well, since |
| 179 | // functions are never retainable object pointers, however clang occasionally |
| 180 | // bitcasts retainable object pointers to function-pointer type temporarily. |
| 181 | PointerType *Ty = dyn_cast<PointerType>(Op->getType()); |
| 182 | if (!Ty) |
| 183 | return false; |
Michael Gottesman | 23a1ee5 | 2013-01-29 04:58:30 +0000 | [diff] [blame] | 184 | // Conservatively assume anything else is a potential retainable object |
| 185 | // pointer. |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 186 | return true; |
| 187 | } |
| 188 | |
| 189 | static inline bool IsPotentialRetainableObjPtr(const Value *Op, |
| 190 | AliasAnalysis &AA) { |
| 191 | // First make the rudimentary check. |
| 192 | if (!IsPotentialRetainableObjPtr(Op)) |
| 193 | return false; |
| 194 | |
| 195 | // Objects in constant memory are not reference-counted. |
| 196 | if (AA.pointsToConstantMemory(Op)) |
| 197 | return false; |
| 198 | |
| 199 | // Pointers in constant memory are not pointing to reference-counted objects. |
| 200 | if (const LoadInst *LI = dyn_cast<LoadInst>(Op)) |
| 201 | if (AA.pointsToConstantMemory(LI->getPointerOperand())) |
| 202 | return false; |
| 203 | |
| 204 | // Otherwise assume the worst. |
| 205 | return true; |
| 206 | } |
| 207 | |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame^] | 208 | /// \brief Helper for GetARCInstKind. Determines what kind of construct CS |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 209 | /// is. |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame^] | 210 | static inline ARCInstKind GetCallSiteClass(ImmutableCallSite CS) { |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 211 | for (ImmutableCallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end(); |
| 212 | I != E; ++I) |
| 213 | if (IsPotentialRetainableObjPtr(*I)) |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame^] | 214 | return CS.onlyReadsMemory() ? ARCInstKind::User : ARCInstKind::CallOrUser; |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 215 | |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame^] | 216 | return CS.onlyReadsMemory() ? ARCInstKind::None : ARCInstKind::Call; |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | /// \brief Return true if this value refers to a distinct and identifiable |
| 220 | /// object. |
| 221 | /// |
| 222 | /// This is similar to AliasAnalysis's isIdentifiedObject, except that it uses |
| 223 | /// special knowledge of ObjC conventions. |
| 224 | static inline bool IsObjCIdentifiedObject(const Value *V) { |
| 225 | // Assume that call results and arguments have their own "provenance". |
| 226 | // Constants (including GlobalVariables) and Allocas are never |
| 227 | // reference-counted. |
| 228 | if (isa<CallInst>(V) || isa<InvokeInst>(V) || |
| 229 | isa<Argument>(V) || isa<Constant>(V) || |
| 230 | isa<AllocaInst>(V)) |
| 231 | return true; |
| 232 | |
| 233 | if (const LoadInst *LI = dyn_cast<LoadInst>(V)) { |
| 234 | const Value *Pointer = |
Michael Gottesman | e5ad66f | 2015-02-19 00:42:38 +0000 | [diff] [blame] | 235 | GetRCIdentityRoot(LI->getPointerOperand()); |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 236 | if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(Pointer)) { |
| 237 | // A constant pointer can't be pointing to an object on the heap. It may |
| 238 | // be reference-counted, but it won't be deleted. |
| 239 | if (GV->isConstant()) |
| 240 | return true; |
| 241 | StringRef Name = GV->getName(); |
| 242 | // These special variables are known to hold values which are not |
| 243 | // reference-counted pointers. |
Rafael Espindola | a3b5b60 | 2014-11-17 02:28:27 +0000 | [diff] [blame] | 244 | if (Name.startswith("\01l_objc_msgSend_fixup_")) |
| 245 | return true; |
| 246 | |
| 247 | StringRef Section = GV->getSection(); |
| 248 | if (Section.find("__message_refs") != StringRef::npos || |
| 249 | Section.find("__objc_classrefs") != StringRef::npos || |
| 250 | Section.find("__objc_superrefs") != StringRef::npos || |
| 251 | Section.find("__objc_methname") != StringRef::npos || |
| 252 | Section.find("__cstring") != StringRef::npos) |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 253 | return true; |
| 254 | } |
| 255 | } |
| 256 | |
| 257 | return false; |
| 258 | } |
Michael Gottesman | 294e7da | 2013-01-28 05:51:54 +0000 | [diff] [blame] | 259 | |
Michael Gottesman | 08904e3 | 2013-01-28 03:28:38 +0000 | [diff] [blame] | 260 | } // end namespace objcarc |
| 261 | } // end namespace llvm |
| 262 | |
Benjamin Kramer | a7c40ef | 2014-08-13 16:26:38 +0000 | [diff] [blame] | 263 | #endif |