Michael Gottesman | 79d8d81 | 2013-01-28 01:35:51 +0000 | [diff] [blame] | 1 | //===- ObjCARCOpts.cpp - ObjC ARC Optimization ----------------------------===// |
John McCall | d935e9c | 2011-06-15 23:37:01 +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 | //===----------------------------------------------------------------------===// |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 9 | /// \file |
| 10 | /// This file defines ObjC ARC optimizations. ARC stands for Automatic |
| 11 | /// Reference Counting and is a system for managing reference counts for objects |
| 12 | /// in Objective C. |
| 13 | /// |
| 14 | /// The optimizations performed include elimination of redundant, partially |
| 15 | /// redundant, and inconsequential reference count operations, elimination of |
Michael Gottesman | 697d8b9 | 2013-02-07 04:12:57 +0000 | [diff] [blame] | 16 | /// redundant weak pointer operations, and numerous minor simplifications. |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 17 | /// |
| 18 | /// WARNING: This file knows about certain library functions. It recognizes them |
| 19 | /// by name, and hardwires knowledge of their semantics. |
| 20 | /// |
| 21 | /// WARNING: This file knows about how certain Objective-C library functions are |
| 22 | /// used. Naive LLVM IR transformations which would otherwise be |
| 23 | /// behavior-preserving may break these assumptions. |
| 24 | /// |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 25 | //===----------------------------------------------------------------------===// |
| 26 | |
Michael Gottesman | 08904e3 | 2013-01-28 03:28:38 +0000 | [diff] [blame] | 27 | #define DEBUG_TYPE "objc-arc-opts" |
| 28 | #include "ObjCARC.h" |
Michael Gottesman | 278266f | 2013-01-29 04:20:52 +0000 | [diff] [blame] | 29 | #include "DependencyAnalysis.h" |
Michael Gottesman | 294e7da | 2013-01-28 05:51:54 +0000 | [diff] [blame] | 30 | #include "ObjCARCAliasAnalysis.h" |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 31 | #include "ProvenanceAnalysis.h" |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/DenseMap.h" |
Michael Gottesman | 5a91bbf | 2013-05-24 20:44:02 +0000 | [diff] [blame^] | 33 | #include "llvm/ADT/DenseSet.h" |
Michael Gottesman | fa0939f | 2013-01-28 04:12:07 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/STLExtras.h" |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/SmallPtrSet.h" |
Michael Gottesman | 278266f | 2013-01-29 04:20:52 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/Statistic.h" |
Michael Gottesman | cd4de0f | 2013-03-26 00:42:09 +0000 | [diff] [blame] | 37 | #include "llvm/IR/IRBuilder.h" |
Michael Gottesman | 278266f | 2013-01-29 04:20:52 +0000 | [diff] [blame] | 38 | #include "llvm/IR/LLVMContext.h" |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 39 | #include "llvm/Support/CFG.h" |
Michael Gottesman | 13a5f1a | 2013-01-29 04:51:59 +0000 | [diff] [blame] | 40 | #include "llvm/Support/Debug.h" |
Timur Iskhodzhanov | 5d7ff00 | 2013-01-29 09:09:27 +0000 | [diff] [blame] | 41 | #include "llvm/Support/raw_ostream.h" |
Michael Gottesman | fa0939f | 2013-01-28 04:12:07 +0000 | [diff] [blame] | 42 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 43 | using namespace llvm; |
Michael Gottesman | 08904e3 | 2013-01-28 03:28:38 +0000 | [diff] [blame] | 44 | using namespace llvm::objcarc; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 45 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 46 | /// \defgroup MiscUtils Miscellaneous utilities that are not ARC specific. |
| 47 | /// @{ |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 48 | |
| 49 | namespace { |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 50 | /// \brief An associative container with fast insertion-order (deterministic) |
| 51 | /// iteration over its elements. Plus the special blot operation. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 52 | template<class KeyT, class ValueT> |
| 53 | class MapVector { |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 54 | /// Map keys to indices in Vector. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 55 | typedef DenseMap<KeyT, size_t> MapTy; |
| 56 | MapTy Map; |
| 57 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 58 | typedef std::vector<std::pair<KeyT, ValueT> > VectorTy; |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 59 | /// Keys and values. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 60 | VectorTy Vector; |
| 61 | |
| 62 | public: |
| 63 | typedef typename VectorTy::iterator iterator; |
| 64 | typedef typename VectorTy::const_iterator const_iterator; |
| 65 | iterator begin() { return Vector.begin(); } |
| 66 | iterator end() { return Vector.end(); } |
| 67 | const_iterator begin() const { return Vector.begin(); } |
| 68 | const_iterator end() const { return Vector.end(); } |
| 69 | |
| 70 | #ifdef XDEBUG |
| 71 | ~MapVector() { |
| 72 | assert(Vector.size() >= Map.size()); // May differ due to blotting. |
| 73 | for (typename MapTy::const_iterator I = Map.begin(), E = Map.end(); |
| 74 | I != E; ++I) { |
| 75 | assert(I->second < Vector.size()); |
| 76 | assert(Vector[I->second].first == I->first); |
| 77 | } |
| 78 | for (typename VectorTy::const_iterator I = Vector.begin(), |
| 79 | E = Vector.end(); I != E; ++I) |
| 80 | assert(!I->first || |
| 81 | (Map.count(I->first) && |
| 82 | Map[I->first] == size_t(I - Vector.begin()))); |
| 83 | } |
| 84 | #endif |
| 85 | |
Dan Gohman | 55b0674 | 2012-03-02 01:13:53 +0000 | [diff] [blame] | 86 | ValueT &operator[](const KeyT &Arg) { |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 87 | std::pair<typename MapTy::iterator, bool> Pair = |
| 88 | Map.insert(std::make_pair(Arg, size_t(0))); |
| 89 | if (Pair.second) { |
Dan Gohman | 55b0674 | 2012-03-02 01:13:53 +0000 | [diff] [blame] | 90 | size_t Num = Vector.size(); |
| 91 | Pair.first->second = Num; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 92 | Vector.push_back(std::make_pair(Arg, ValueT())); |
Dan Gohman | 55b0674 | 2012-03-02 01:13:53 +0000 | [diff] [blame] | 93 | return Vector[Num].second; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 94 | } |
| 95 | return Vector[Pair.first->second].second; |
| 96 | } |
| 97 | |
| 98 | std::pair<iterator, bool> |
| 99 | insert(const std::pair<KeyT, ValueT> &InsertPair) { |
| 100 | std::pair<typename MapTy::iterator, bool> Pair = |
| 101 | Map.insert(std::make_pair(InsertPair.first, size_t(0))); |
| 102 | if (Pair.second) { |
Dan Gohman | 55b0674 | 2012-03-02 01:13:53 +0000 | [diff] [blame] | 103 | size_t Num = Vector.size(); |
| 104 | Pair.first->second = Num; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 105 | Vector.push_back(InsertPair); |
Dan Gohman | 55b0674 | 2012-03-02 01:13:53 +0000 | [diff] [blame] | 106 | return std::make_pair(Vector.begin() + Num, true); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 107 | } |
| 108 | return std::make_pair(Vector.begin() + Pair.first->second, false); |
| 109 | } |
| 110 | |
Michael Gottesman | a76143ee | 2013-05-13 23:49:42 +0000 | [diff] [blame] | 111 | iterator find(const KeyT &Key) { |
| 112 | typename MapTy::iterator It = Map.find(Key); |
| 113 | if (It == Map.end()) return Vector.end(); |
| 114 | return Vector.begin() + It->second; |
| 115 | } |
| 116 | |
Dan Gohman | 55b0674 | 2012-03-02 01:13:53 +0000 | [diff] [blame] | 117 | const_iterator find(const KeyT &Key) const { |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 118 | typename MapTy::const_iterator It = Map.find(Key); |
| 119 | if (It == Map.end()) return Vector.end(); |
| 120 | return Vector.begin() + It->second; |
| 121 | } |
| 122 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 123 | /// This is similar to erase, but instead of removing the element from the |
| 124 | /// vector, it just zeros out the key in the vector. This leaves iterators |
| 125 | /// intact, but clients must be prepared for zeroed-out keys when iterating. |
Dan Gohman | 55b0674 | 2012-03-02 01:13:53 +0000 | [diff] [blame] | 126 | void blot(const KeyT &Key) { |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 127 | typename MapTy::iterator It = Map.find(Key); |
| 128 | if (It == Map.end()) return; |
| 129 | Vector[It->second].first = KeyT(); |
| 130 | Map.erase(It); |
| 131 | } |
| 132 | |
| 133 | void clear() { |
| 134 | Map.clear(); |
| 135 | Vector.clear(); |
| 136 | } |
| 137 | }; |
| 138 | } |
| 139 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 140 | /// @} |
| 141 | /// |
| 142 | /// \defgroup ARCUtilities Utility declarations/definitions specific to ARC. |
| 143 | /// @{ |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 144 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 145 | /// \brief This is similar to StripPointerCastsAndObjCCalls but it stops as soon |
| 146 | /// as it finds a value with multiple uses. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 147 | static const Value *FindSingleUseIdentifiedObject(const Value *Arg) { |
| 148 | if (Arg->hasOneUse()) { |
| 149 | if (const BitCastInst *BC = dyn_cast<BitCastInst>(Arg)) |
| 150 | return FindSingleUseIdentifiedObject(BC->getOperand(0)); |
| 151 | if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Arg)) |
| 152 | if (GEP->hasAllZeroIndices()) |
| 153 | return FindSingleUseIdentifiedObject(GEP->getPointerOperand()); |
| 154 | if (IsForwarding(GetBasicInstructionClass(Arg))) |
| 155 | return FindSingleUseIdentifiedObject( |
| 156 | cast<CallInst>(Arg)->getArgOperand(0)); |
| 157 | if (!IsObjCIdentifiedObject(Arg)) |
| 158 | return 0; |
| 159 | return Arg; |
| 160 | } |
| 161 | |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 162 | // If we found an identifiable object but it has multiple uses, but they are |
| 163 | // trivial uses, we can still consider this to be a single-use value. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 164 | if (IsObjCIdentifiedObject(Arg)) { |
| 165 | for (Value::const_use_iterator UI = Arg->use_begin(), UE = Arg->use_end(); |
| 166 | UI != UE; ++UI) { |
| 167 | const User *U = *UI; |
| 168 | if (!U->use_empty() || StripPointerCastsAndObjCCalls(U) != Arg) |
| 169 | return 0; |
| 170 | } |
| 171 | |
| 172 | return Arg; |
| 173 | } |
| 174 | |
| 175 | return 0; |
| 176 | } |
| 177 | |
Michael Gottesman | 774d2c0 | 2013-01-29 21:00:52 +0000 | [diff] [blame] | 178 | /// \brief Test whether the given retainable object pointer escapes. |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 179 | /// |
| 180 | /// This differs from regular escape analysis in that a use as an |
| 181 | /// argument to a call is not considered an escape. |
| 182 | /// |
Michael Gottesman | 774d2c0 | 2013-01-29 21:00:52 +0000 | [diff] [blame] | 183 | static bool DoesRetainableObjPtrEscape(const User *Ptr) { |
Michael Gottesman | 774d2c0 | 2013-01-29 21:00:52 +0000 | [diff] [blame] | 184 | DEBUG(dbgs() << "DoesRetainableObjPtrEscape: Target: " << *Ptr << "\n"); |
Michael Gottesman | 1a89fe5 | 2013-01-13 07:47:32 +0000 | [diff] [blame] | 185 | |
Dan Gohman | 728db49 | 2012-01-13 00:39:07 +0000 | [diff] [blame] | 186 | // Walk the def-use chains. |
| 187 | SmallVector<const Value *, 4> Worklist; |
Michael Gottesman | 774d2c0 | 2013-01-29 21:00:52 +0000 | [diff] [blame] | 188 | Worklist.push_back(Ptr); |
| 189 | // If Ptr has any operands add them as well. |
Michael Gottesman | 23cda0c | 2013-01-29 21:07:53 +0000 | [diff] [blame] | 190 | for (User::const_op_iterator I = Ptr->op_begin(), E = Ptr->op_end(); I != E; |
| 191 | ++I) { |
Michael Gottesman | 774d2c0 | 2013-01-29 21:00:52 +0000 | [diff] [blame] | 192 | Worklist.push_back(*I); |
| 193 | } |
Michael Gottesman | f15c0bb | 2013-01-13 22:12:06 +0000 | [diff] [blame] | 194 | |
| 195 | // Ensure we do not visit any value twice. |
Michael Gottesman | 774d2c0 | 2013-01-29 21:00:52 +0000 | [diff] [blame] | 196 | SmallPtrSet<const Value *, 8> VisitedSet; |
Michael Gottesman | f15c0bb | 2013-01-13 22:12:06 +0000 | [diff] [blame] | 197 | |
Dan Gohman | 728db49 | 2012-01-13 00:39:07 +0000 | [diff] [blame] | 198 | do { |
| 199 | const Value *V = Worklist.pop_back_val(); |
Michael Gottesman | 1a89fe5 | 2013-01-13 07:47:32 +0000 | [diff] [blame] | 200 | |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 201 | DEBUG(dbgs() << "Visiting: " << *V << "\n"); |
Michael Gottesman | 1a89fe5 | 2013-01-13 07:47:32 +0000 | [diff] [blame] | 202 | |
Dan Gohman | 728db49 | 2012-01-13 00:39:07 +0000 | [diff] [blame] | 203 | for (Value::const_use_iterator UI = V->use_begin(), UE = V->use_end(); |
| 204 | UI != UE; ++UI) { |
| 205 | const User *UUser = *UI; |
Michael Gottesman | 1a89fe5 | 2013-01-13 07:47:32 +0000 | [diff] [blame] | 206 | |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 207 | DEBUG(dbgs() << "User: " << *UUser << "\n"); |
Michael Gottesman | 1a89fe5 | 2013-01-13 07:47:32 +0000 | [diff] [blame] | 208 | |
Dan Gohman | 728db49 | 2012-01-13 00:39:07 +0000 | [diff] [blame] | 209 | // Special - Use by a call (callee or argument) is not considered |
| 210 | // to be an escape. |
Dan Gohman | e1e352a | 2012-04-13 18:28:58 +0000 | [diff] [blame] | 211 | switch (GetBasicInstructionClass(UUser)) { |
| 212 | case IC_StoreWeak: |
| 213 | case IC_InitWeak: |
| 214 | case IC_StoreStrong: |
| 215 | case IC_Autorelease: |
Michael Gottesman | 1a89fe5 | 2013-01-13 07:47:32 +0000 | [diff] [blame] | 216 | case IC_AutoreleaseRV: { |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 217 | DEBUG(dbgs() << "User copies pointer arguments. Pointer Escapes!\n"); |
Dan Gohman | e1e352a | 2012-04-13 18:28:58 +0000 | [diff] [blame] | 218 | // These special functions make copies of their pointer arguments. |
| 219 | return true; |
Michael Gottesman | 1a89fe5 | 2013-01-13 07:47:32 +0000 | [diff] [blame] | 220 | } |
John McCall | 20182ac | 2013-03-22 21:38:36 +0000 | [diff] [blame] | 221 | case IC_IntrinsicUser: |
| 222 | // Use by the use intrinsic is not an escape. |
| 223 | continue; |
Dan Gohman | e1e352a | 2012-04-13 18:28:58 +0000 | [diff] [blame] | 224 | case IC_User: |
| 225 | case IC_None: |
| 226 | // Use by an instruction which copies the value is an escape if the |
| 227 | // result is an escape. |
| 228 | if (isa<BitCastInst>(UUser) || isa<GetElementPtrInst>(UUser) || |
| 229 | isa<PHINode>(UUser) || isa<SelectInst>(UUser)) { |
Michael Gottesman | f15c0bb | 2013-01-13 22:12:06 +0000 | [diff] [blame] | 230 | |
Michael Gottesman | f4b7761 | 2013-02-23 00:31:32 +0000 | [diff] [blame] | 231 | if (VisitedSet.insert(UUser)) { |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 232 | DEBUG(dbgs() << "User copies value. Ptr escapes if result escapes." |
| 233 | " Adding to list.\n"); |
Michael Gottesman | f15c0bb | 2013-01-13 22:12:06 +0000 | [diff] [blame] | 234 | Worklist.push_back(UUser); |
| 235 | } else { |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 236 | DEBUG(dbgs() << "Already visited node.\n"); |
Michael Gottesman | f15c0bb | 2013-01-13 22:12:06 +0000 | [diff] [blame] | 237 | } |
Dan Gohman | e1e352a | 2012-04-13 18:28:58 +0000 | [diff] [blame] | 238 | continue; |
| 239 | } |
| 240 | // Use by a load is not an escape. |
| 241 | if (isa<LoadInst>(UUser)) |
| 242 | continue; |
| 243 | // Use by a store is not an escape if the use is the address. |
| 244 | if (const StoreInst *SI = dyn_cast<StoreInst>(UUser)) |
| 245 | if (V != SI->getValueOperand()) |
| 246 | continue; |
| 247 | break; |
| 248 | default: |
| 249 | // Regular calls and other stuff are not considered escapes. |
Dan Gohman | 728db49 | 2012-01-13 00:39:07 +0000 | [diff] [blame] | 250 | continue; |
| 251 | } |
Dan Gohman | eb6e015 | 2012-02-13 22:57:02 +0000 | [diff] [blame] | 252 | // Otherwise, conservatively assume an escape. |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 253 | DEBUG(dbgs() << "Assuming ptr escapes.\n"); |
Dan Gohman | 728db49 | 2012-01-13 00:39:07 +0000 | [diff] [blame] | 254 | return true; |
| 255 | } |
| 256 | } while (!Worklist.empty()); |
| 257 | |
| 258 | // No escapes found. |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 259 | DEBUG(dbgs() << "Ptr does not escape.\n"); |
Dan Gohman | 728db49 | 2012-01-13 00:39:07 +0000 | [diff] [blame] | 260 | return false; |
| 261 | } |
| 262 | |
Michael Gottesman | a76143ee | 2013-05-13 23:49:42 +0000 | [diff] [blame] | 263 | /// This is a wrapper around getUnderlyingObjCPtr along the lines of |
| 264 | /// GetUnderlyingObjects except that it returns early when it sees the first |
| 265 | /// alloca. |
| 266 | static inline bool AreAnyUnderlyingObjectsAnAlloca(const Value *V) { |
| 267 | SmallPtrSet<const Value *, 4> Visited; |
| 268 | SmallVector<const Value *, 4> Worklist; |
| 269 | Worklist.push_back(V); |
| 270 | do { |
| 271 | const Value *P = Worklist.pop_back_val(); |
| 272 | P = GetUnderlyingObjCPtr(P); |
Michael Gottesman | 0c8b562 | 2013-05-14 06:40:10 +0000 | [diff] [blame] | 273 | |
Michael Gottesman | a76143ee | 2013-05-13 23:49:42 +0000 | [diff] [blame] | 274 | if (isa<AllocaInst>(P)) |
| 275 | return true; |
Michael Gottesman | 0c8b562 | 2013-05-14 06:40:10 +0000 | [diff] [blame] | 276 | |
Michael Gottesman | a76143ee | 2013-05-13 23:49:42 +0000 | [diff] [blame] | 277 | if (!Visited.insert(P)) |
| 278 | continue; |
Michael Gottesman | 0c8b562 | 2013-05-14 06:40:10 +0000 | [diff] [blame] | 279 | |
Michael Gottesman | a76143ee | 2013-05-13 23:49:42 +0000 | [diff] [blame] | 280 | if (const SelectInst *SI = dyn_cast<const SelectInst>(P)) { |
| 281 | Worklist.push_back(SI->getTrueValue()); |
| 282 | Worklist.push_back(SI->getFalseValue()); |
| 283 | continue; |
| 284 | } |
Michael Gottesman | 0c8b562 | 2013-05-14 06:40:10 +0000 | [diff] [blame] | 285 | |
Michael Gottesman | a76143ee | 2013-05-13 23:49:42 +0000 | [diff] [blame] | 286 | if (const PHINode *PN = dyn_cast<const PHINode>(P)) { |
| 287 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 288 | Worklist.push_back(PN->getIncomingValue(i)); |
| 289 | continue; |
| 290 | } |
| 291 | } while (!Worklist.empty()); |
| 292 | |
| 293 | return false; |
| 294 | } |
| 295 | |
| 296 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 297 | /// @} |
| 298 | /// |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 299 | /// \defgroup ARCOpt ARC Optimization. |
| 300 | /// @{ |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 301 | |
| 302 | // TODO: On code like this: |
| 303 | // |
| 304 | // objc_retain(%x) |
| 305 | // stuff_that_cannot_release() |
| 306 | // objc_autorelease(%x) |
| 307 | // stuff_that_cannot_release() |
| 308 | // objc_retain(%x) |
| 309 | // stuff_that_cannot_release() |
| 310 | // objc_autorelease(%x) |
| 311 | // |
| 312 | // The second retain and autorelease can be deleted. |
| 313 | |
| 314 | // TODO: It should be possible to delete |
| 315 | // objc_autoreleasePoolPush and objc_autoreleasePoolPop |
| 316 | // pairs if nothing is actually autoreleased between them. Also, autorelease |
| 317 | // calls followed by objc_autoreleasePoolPop calls (perhaps in ObjC++ code |
| 318 | // after inlining) can be turned into plain release calls. |
| 319 | |
| 320 | // TODO: Critical-edge splitting. If the optimial insertion point is |
| 321 | // a critical edge, the current algorithm has to fail, because it doesn't |
| 322 | // know how to split edges. It should be possible to make the optimizer |
| 323 | // think in terms of edges, rather than blocks, and then split critical |
| 324 | // edges on demand. |
| 325 | |
| 326 | // TODO: OptimizeSequences could generalized to be Interprocedural. |
| 327 | |
| 328 | // TODO: Recognize that a bunch of other objc runtime calls have |
| 329 | // non-escaping arguments and non-releasing arguments, and may be |
| 330 | // non-autoreleasing. |
| 331 | |
| 332 | // TODO: Sink autorelease calls as far as possible. Unfortunately we |
| 333 | // usually can't sink them past other calls, which would be the main |
| 334 | // case where it would be useful. |
| 335 | |
Dan Gohman | b389401 | 2011-08-19 00:26:36 +0000 | [diff] [blame] | 336 | // TODO: The pointer returned from objc_loadWeakRetained is retained. |
| 337 | |
| 338 | // TODO: Delete release+retain pairs (rare). |
Dan Gohman | ceaac7c | 2011-06-20 23:20:43 +0000 | [diff] [blame] | 339 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 340 | STATISTIC(NumNoops, "Number of no-op objc calls eliminated"); |
| 341 | STATISTIC(NumPartialNoops, "Number of partially no-op objc calls eliminated"); |
| 342 | STATISTIC(NumAutoreleases,"Number of autoreleases converted to releases"); |
| 343 | STATISTIC(NumRets, "Number of return value forwarding " |
Michael Gottesman | b4e7f4d | 2013-05-15 17:43:03 +0000 | [diff] [blame] | 344 | "retain+autoreleases eliminated"); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 345 | STATISTIC(NumRRs, "Number of retain+release paths eliminated"); |
| 346 | STATISTIC(NumPeeps, "Number of calls peephole-optimized"); |
Matt Beaumont-Gay | e55d949 | 2013-05-13 21:10:49 +0000 | [diff] [blame] | 347 | #ifndef NDEBUG |
Michael Gottesman | 9c11815 | 2013-04-29 06:16:57 +0000 | [diff] [blame] | 348 | STATISTIC(NumRetainsBeforeOpt, |
Michael Gottesman | b4e7f4d | 2013-05-15 17:43:03 +0000 | [diff] [blame] | 349 | "Number of retains before optimization"); |
Michael Gottesman | 9c11815 | 2013-04-29 06:16:57 +0000 | [diff] [blame] | 350 | STATISTIC(NumReleasesBeforeOpt, |
Michael Gottesman | b4e7f4d | 2013-05-15 17:43:03 +0000 | [diff] [blame] | 351 | "Number of releases before optimization"); |
Michael Gottesman | 9c11815 | 2013-04-29 06:16:57 +0000 | [diff] [blame] | 352 | STATISTIC(NumRetainsAfterOpt, |
Michael Gottesman | b4e7f4d | 2013-05-15 17:43:03 +0000 | [diff] [blame] | 353 | "Number of retains after optimization"); |
Michael Gottesman | 9c11815 | 2013-04-29 06:16:57 +0000 | [diff] [blame] | 354 | STATISTIC(NumReleasesAfterOpt, |
Michael Gottesman | b4e7f4d | 2013-05-15 17:43:03 +0000 | [diff] [blame] | 355 | "Number of releases after optimization"); |
Michael Gottesman | 03cf3c8 | 2013-04-29 07:29:08 +0000 | [diff] [blame] | 356 | #endif |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 357 | |
| 358 | namespace { |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 359 | /// \enum Sequence |
| 360 | /// |
| 361 | /// \brief A sequence of states that a pointer may go through in which an |
| 362 | /// objc_retain and objc_release are actually needed. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 363 | enum Sequence { |
| 364 | S_None, |
Michael Gottesman | 53fd20b | 2013-01-29 21:07:51 +0000 | [diff] [blame] | 365 | S_Retain, ///< objc_retain(x). |
| 366 | S_CanRelease, ///< foo(x) -- x could possibly see a ref count decrement. |
| 367 | S_Use, ///< any use of x. |
Michael Gottesman | 386241c | 2013-01-29 21:39:02 +0000 | [diff] [blame] | 368 | S_Stop, ///< like S_Release, but code motion is stopped. |
Michael Gottesman | 53fd20b | 2013-01-29 21:07:51 +0000 | [diff] [blame] | 369 | S_Release, ///< objc_release(x). |
Michael Gottesman | 9bdab2b | 2013-01-29 21:41:44 +0000 | [diff] [blame] | 370 | S_MovableRelease ///< objc_release(x), !clang.imprecise_release. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 371 | }; |
Michael Gottesman | 53fd20b | 2013-01-29 21:07:51 +0000 | [diff] [blame] | 372 | |
| 373 | raw_ostream &operator<<(raw_ostream &OS, const Sequence S) |
| 374 | LLVM_ATTRIBUTE_UNUSED; |
| 375 | raw_ostream &operator<<(raw_ostream &OS, const Sequence S) { |
| 376 | switch (S) { |
| 377 | case S_None: |
| 378 | return OS << "S_None"; |
| 379 | case S_Retain: |
| 380 | return OS << "S_Retain"; |
| 381 | case S_CanRelease: |
| 382 | return OS << "S_CanRelease"; |
| 383 | case S_Use: |
| 384 | return OS << "S_Use"; |
| 385 | case S_Release: |
| 386 | return OS << "S_Release"; |
| 387 | case S_MovableRelease: |
| 388 | return OS << "S_MovableRelease"; |
| 389 | case S_Stop: |
| 390 | return OS << "S_Stop"; |
| 391 | } |
| 392 | llvm_unreachable("Unknown sequence type."); |
| 393 | } |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | static Sequence MergeSeqs(Sequence A, Sequence B, bool TopDown) { |
| 397 | // The easy cases. |
| 398 | if (A == B) |
| 399 | return A; |
| 400 | if (A == S_None || B == S_None) |
| 401 | return S_None; |
| 402 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 403 | if (A > B) std::swap(A, B); |
| 404 | if (TopDown) { |
| 405 | // Choose the side which is further along in the sequence. |
Dan Gohman | 1213027 | 2011-08-12 00:26:31 +0000 | [diff] [blame] | 406 | if ((A == S_Retain || A == S_CanRelease) && |
| 407 | (B == S_CanRelease || B == S_Use)) |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 408 | return B; |
| 409 | } else { |
| 410 | // Choose the side which is further along in the sequence. |
| 411 | if ((A == S_Use || A == S_CanRelease) && |
Dan Gohman | 1213027 | 2011-08-12 00:26:31 +0000 | [diff] [blame] | 412 | (B == S_Use || B == S_Release || B == S_Stop || B == S_MovableRelease)) |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 413 | return A; |
| 414 | // If both sides are releases, choose the more conservative one. |
| 415 | if (A == S_Stop && (B == S_Release || B == S_MovableRelease)) |
| 416 | return A; |
| 417 | if (A == S_Release && B == S_MovableRelease) |
| 418 | return A; |
| 419 | } |
| 420 | |
| 421 | return S_None; |
| 422 | } |
| 423 | |
| 424 | namespace { |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 425 | /// \brief Unidirectional information about either a |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 426 | /// retain-decrement-use-release sequence or release-use-decrement-retain |
Bob Wilson | 798a770 | 2013-04-09 22:15:51 +0000 | [diff] [blame] | 427 | /// reverse sequence. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 428 | struct RRInfo { |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 429 | /// After an objc_retain, the reference count of the referenced |
Dan Gohman | b389401 | 2011-08-19 00:26:36 +0000 | [diff] [blame] | 430 | /// object is known to be positive. Similarly, before an objc_release, the |
| 431 | /// reference count of the referenced object is known to be positive. If |
| 432 | /// there are retain-release pairs in code regions where the retain count |
| 433 | /// is known to be positive, they can be eliminated, regardless of any side |
| 434 | /// effects between them. |
| 435 | /// |
| 436 | /// Also, a retain+release pair nested within another retain+release |
| 437 | /// pair all on the known same pointer value can be eliminated, regardless |
| 438 | /// of any intervening side effects. |
| 439 | /// |
| 440 | /// KnownSafe is true when either of these conditions is satisfied. |
| 441 | bool KnownSafe; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 442 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 443 | /// True of the objc_release calls are all marked with the "tail" keyword. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 444 | bool IsTailCallRelease; |
| 445 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 446 | /// If the Calls are objc_release calls and they all have a |
| 447 | /// clang.imprecise_release tag, this is the metadata tag. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 448 | MDNode *ReleaseMetadata; |
| 449 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 450 | /// For a top-down sequence, the set of objc_retains or |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 451 | /// objc_retainBlocks. For bottom-up, the set of objc_releases. |
| 452 | SmallPtrSet<Instruction *, 2> Calls; |
| 453 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 454 | /// The set of optimal insert positions for moving calls in the opposite |
| 455 | /// sequence. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 456 | SmallPtrSet<Instruction *, 2> ReverseInsertPts; |
| 457 | |
| 458 | RRInfo() : |
Michael Gottesman | 5a91bbf | 2013-05-24 20:44:02 +0000 | [diff] [blame^] | 459 | KnownSafe(false), IsTailCallRelease(false), ReleaseMetadata(0) {} |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 460 | |
| 461 | void clear(); |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 462 | |
Michael Gottesman | 1d8d257 | 2013-04-05 22:54:28 +0000 | [diff] [blame] | 463 | bool IsTrackingImpreciseReleases() { |
| 464 | return ReleaseMetadata != 0; |
| 465 | } |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 466 | }; |
| 467 | } |
| 468 | |
| 469 | void RRInfo::clear() { |
Dan Gohman | b389401 | 2011-08-19 00:26:36 +0000 | [diff] [blame] | 470 | KnownSafe = false; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 471 | IsTailCallRelease = false; |
| 472 | ReleaseMetadata = 0; |
| 473 | Calls.clear(); |
| 474 | ReverseInsertPts.clear(); |
| 475 | } |
| 476 | |
| 477 | namespace { |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 478 | /// \brief This class summarizes several per-pointer runtime properties which |
| 479 | /// are propogated through the flow graph. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 480 | class PtrState { |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 481 | /// True if the reference count is known to be incremented. |
Dan Gohman | 62079b4 | 2012-04-25 00:50:46 +0000 | [diff] [blame] | 482 | bool KnownPositiveRefCount; |
| 483 | |
Bob Wilson | 798a770 | 2013-04-09 22:15:51 +0000 | [diff] [blame] | 484 | /// True if we've seen an opportunity for partial RR elimination, such as |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 485 | /// pushing calls into a CFG triangle or into one side of a CFG diamond. |
Dan Gohman | 62079b4 | 2012-04-25 00:50:46 +0000 | [diff] [blame] | 486 | bool Partial; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 487 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 488 | /// The current position in the sequence. |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 489 | Sequence Seq : 8; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 490 | |
| 491 | public: |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 492 | /// Unidirectional information about the current sequence. |
| 493 | /// |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 494 | /// TODO: Encapsulate this better. |
| 495 | RRInfo RRI; |
| 496 | |
Dan Gohman | df476e5 | 2012-09-04 23:16:20 +0000 | [diff] [blame] | 497 | PtrState() : KnownPositiveRefCount(false), Partial(false), |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 498 | Seq(S_None) {} |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 499 | |
Michael Gottesman | 415ddd7 | 2013-02-05 19:32:18 +0000 | [diff] [blame] | 500 | void SetKnownPositiveRefCount() { |
Michael Gottesman | f3f9e3b | 2013-05-14 00:08:09 +0000 | [diff] [blame] | 501 | DEBUG(dbgs() << "Setting Known Positive.\n"); |
Dan Gohman | 62079b4 | 2012-04-25 00:50:46 +0000 | [diff] [blame] | 502 | KnownPositiveRefCount = true; |
Dan Gohman | 1213027 | 2011-08-12 00:26:31 +0000 | [diff] [blame] | 503 | } |
| 504 | |
Michael Gottesman | 764b1cf | 2013-03-23 05:46:19 +0000 | [diff] [blame] | 505 | void ClearKnownPositiveRefCount() { |
Michael Gottesman | f3f9e3b | 2013-05-14 00:08:09 +0000 | [diff] [blame] | 506 | DEBUG(dbgs() << "Clearing Known Positive.\n"); |
Dan Gohman | 62079b4 | 2012-04-25 00:50:46 +0000 | [diff] [blame] | 507 | KnownPositiveRefCount = false; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 508 | } |
| 509 | |
Michael Gottesman | 07beea4 | 2013-03-23 05:31:01 +0000 | [diff] [blame] | 510 | bool HasKnownPositiveRefCount() const { |
Dan Gohman | 62079b4 | 2012-04-25 00:50:46 +0000 | [diff] [blame] | 511 | return KnownPositiveRefCount; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 512 | } |
| 513 | |
Michael Gottesman | 415ddd7 | 2013-02-05 19:32:18 +0000 | [diff] [blame] | 514 | void SetSeq(Sequence NewSeq) { |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 515 | DEBUG(dbgs() << "Old: " << Seq << "; New: " << NewSeq << "\n"); |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 516 | Seq = NewSeq; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 517 | } |
| 518 | |
Michael Gottesman | 415ddd7 | 2013-02-05 19:32:18 +0000 | [diff] [blame] | 519 | Sequence GetSeq() const { |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 520 | return Seq; |
| 521 | } |
| 522 | |
Michael Gottesman | 415ddd7 | 2013-02-05 19:32:18 +0000 | [diff] [blame] | 523 | void ClearSequenceProgress() { |
Dan Gohman | 62079b4 | 2012-04-25 00:50:46 +0000 | [diff] [blame] | 524 | ResetSequenceProgress(S_None); |
| 525 | } |
| 526 | |
Michael Gottesman | 415ddd7 | 2013-02-05 19:32:18 +0000 | [diff] [blame] | 527 | void ResetSequenceProgress(Sequence NewSeq) { |
Michael Gottesman | 01338a4 | 2013-04-20 23:36:57 +0000 | [diff] [blame] | 528 | DEBUG(dbgs() << "Resetting sequence progress.\n"); |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 529 | SetSeq(NewSeq); |
Dan Gohman | 62079b4 | 2012-04-25 00:50:46 +0000 | [diff] [blame] | 530 | Partial = false; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 531 | RRI.clear(); |
| 532 | } |
| 533 | |
| 534 | void Merge(const PtrState &Other, bool TopDown); |
| 535 | }; |
| 536 | } |
| 537 | |
| 538 | void |
| 539 | PtrState::Merge(const PtrState &Other, bool TopDown) { |
| 540 | Seq = MergeSeqs(Seq, Other.Seq, TopDown); |
Dan Gohman | 62079b4 | 2012-04-25 00:50:46 +0000 | [diff] [blame] | 541 | KnownPositiveRefCount = KnownPositiveRefCount && Other.KnownPositiveRefCount; |
Michael Gottesman | 60f6b28 | 2013-03-29 05:13:07 +0000 | [diff] [blame] | 542 | |
Dan Gohman | 1736c14 | 2011-10-17 18:48:25 +0000 | [diff] [blame] | 543 | // If we're not in a sequence (anymore), drop all associated state. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 544 | if (Seq == S_None) { |
Dan Gohman | 62079b4 | 2012-04-25 00:50:46 +0000 | [diff] [blame] | 545 | Partial = false; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 546 | RRI.clear(); |
Dan Gohman | 62079b4 | 2012-04-25 00:50:46 +0000 | [diff] [blame] | 547 | } else if (Partial || Other.Partial) { |
Dan Gohman | 1736c14 | 2011-10-17 18:48:25 +0000 | [diff] [blame] | 548 | // If we're doing a merge on a path that's previously seen a partial |
| 549 | // merge, conservatively drop the sequence, to avoid doing partial |
| 550 | // RR elimination. If the branch predicates for the two merge differ, |
| 551 | // mixing them is unsafe. |
Dan Gohman | 62079b4 | 2012-04-25 00:50:46 +0000 | [diff] [blame] | 552 | ClearSequenceProgress(); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 553 | } else { |
| 554 | // Conservatively merge the ReleaseMetadata information. |
| 555 | if (RRI.ReleaseMetadata != Other.RRI.ReleaseMetadata) |
| 556 | RRI.ReleaseMetadata = 0; |
| 557 | |
Dan Gohman | b389401 | 2011-08-19 00:26:36 +0000 | [diff] [blame] | 558 | RRI.KnownSafe = RRI.KnownSafe && Other.RRI.KnownSafe; |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 559 | RRI.IsTailCallRelease = RRI.IsTailCallRelease && |
| 560 | Other.RRI.IsTailCallRelease; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 561 | RRI.Calls.insert(Other.RRI.Calls.begin(), Other.RRI.Calls.end()); |
Dan Gohman | 1736c14 | 2011-10-17 18:48:25 +0000 | [diff] [blame] | 562 | |
| 563 | // Merge the insert point sets. If there are any differences, |
| 564 | // that makes this a partial merge. |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 565 | Partial = RRI.ReverseInsertPts.size() != Other.RRI.ReverseInsertPts.size(); |
Dan Gohman | 1736c14 | 2011-10-17 18:48:25 +0000 | [diff] [blame] | 566 | for (SmallPtrSet<Instruction *, 2>::const_iterator |
| 567 | I = Other.RRI.ReverseInsertPts.begin(), |
| 568 | E = Other.RRI.ReverseInsertPts.end(); I != E; ++I) |
Dan Gohman | 62079b4 | 2012-04-25 00:50:46 +0000 | [diff] [blame] | 569 | Partial |= RRI.ReverseInsertPts.insert(*I); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 570 | } |
| 571 | } |
| 572 | |
| 573 | namespace { |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 574 | /// \brief Per-BasicBlock state. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 575 | class BBState { |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 576 | /// The number of unique control paths from the entry which can reach this |
| 577 | /// block. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 578 | unsigned TopDownPathCount; |
| 579 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 580 | /// The number of unique control paths to exits from this block. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 581 | unsigned BottomUpPathCount; |
| 582 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 583 | /// A type for PerPtrTopDown and PerPtrBottomUp. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 584 | typedef MapVector<const Value *, PtrState> MapTy; |
| 585 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 586 | /// The top-down traversal uses this to record information known about a |
| 587 | /// pointer at the bottom of each block. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 588 | MapTy PerPtrTopDown; |
| 589 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 590 | /// The bottom-up traversal uses this to record information known about a |
| 591 | /// pointer at the top of each block. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 592 | MapTy PerPtrBottomUp; |
| 593 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 594 | /// Effective predecessors of the current block ignoring ignorable edges and |
| 595 | /// ignored backedges. |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 596 | SmallVector<BasicBlock *, 2> Preds; |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 597 | /// Effective successors of the current block ignoring ignorable edges and |
| 598 | /// ignored backedges. |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 599 | SmallVector<BasicBlock *, 2> Succs; |
| 600 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 601 | public: |
| 602 | BBState() : TopDownPathCount(0), BottomUpPathCount(0) {} |
| 603 | |
| 604 | typedef MapTy::iterator ptr_iterator; |
| 605 | typedef MapTy::const_iterator ptr_const_iterator; |
| 606 | |
| 607 | ptr_iterator top_down_ptr_begin() { return PerPtrTopDown.begin(); } |
| 608 | ptr_iterator top_down_ptr_end() { return PerPtrTopDown.end(); } |
| 609 | ptr_const_iterator top_down_ptr_begin() const { |
| 610 | return PerPtrTopDown.begin(); |
| 611 | } |
| 612 | ptr_const_iterator top_down_ptr_end() const { |
| 613 | return PerPtrTopDown.end(); |
| 614 | } |
| 615 | |
| 616 | ptr_iterator bottom_up_ptr_begin() { return PerPtrBottomUp.begin(); } |
| 617 | ptr_iterator bottom_up_ptr_end() { return PerPtrBottomUp.end(); } |
| 618 | ptr_const_iterator bottom_up_ptr_begin() const { |
| 619 | return PerPtrBottomUp.begin(); |
| 620 | } |
| 621 | ptr_const_iterator bottom_up_ptr_end() const { |
| 622 | return PerPtrBottomUp.end(); |
| 623 | } |
| 624 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 625 | /// Mark this block as being an entry block, which has one path from the |
| 626 | /// entry by definition. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 627 | void SetAsEntry() { TopDownPathCount = 1; } |
| 628 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 629 | /// Mark this block as being an exit block, which has one path to an exit by |
| 630 | /// definition. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 631 | void SetAsExit() { BottomUpPathCount = 1; } |
| 632 | |
Michael Gottesman | 993fbf7 | 2013-05-13 19:40:39 +0000 | [diff] [blame] | 633 | /// Attempt to find the PtrState object describing the top down state for |
| 634 | /// pointer Arg. Return a new initialized PtrState describing the top down |
| 635 | /// state for Arg if we do not find one. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 636 | PtrState &getPtrTopDownState(const Value *Arg) { |
| 637 | return PerPtrTopDown[Arg]; |
| 638 | } |
| 639 | |
Michael Gottesman | 993fbf7 | 2013-05-13 19:40:39 +0000 | [diff] [blame] | 640 | /// Attempt to find the PtrState object describing the bottom up state for |
| 641 | /// pointer Arg. Return a new initialized PtrState describing the bottom up |
| 642 | /// state for Arg if we do not find one. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 643 | PtrState &getPtrBottomUpState(const Value *Arg) { |
| 644 | return PerPtrBottomUp[Arg]; |
| 645 | } |
| 646 | |
Michael Gottesman | a76143ee | 2013-05-13 23:49:42 +0000 | [diff] [blame] | 647 | /// Attempt to find the PtrState object describing the bottom up state for |
| 648 | /// pointer Arg. |
| 649 | ptr_iterator findPtrBottomUpState(const Value *Arg) { |
| 650 | return PerPtrBottomUp.find(Arg); |
| 651 | } |
| 652 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 653 | void clearBottomUpPointers() { |
Evan Cheng | e4df6a2 | 2011-08-04 18:40:26 +0000 | [diff] [blame] | 654 | PerPtrBottomUp.clear(); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 655 | } |
| 656 | |
| 657 | void clearTopDownPointers() { |
| 658 | PerPtrTopDown.clear(); |
| 659 | } |
| 660 | |
| 661 | void InitFromPred(const BBState &Other); |
| 662 | void InitFromSucc(const BBState &Other); |
| 663 | void MergePred(const BBState &Other); |
| 664 | void MergeSucc(const BBState &Other); |
| 665 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 666 | /// Return the number of possible unique paths from an entry to an exit |
| 667 | /// which pass through this block. This is only valid after both the |
| 668 | /// top-down and bottom-up traversals are complete. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 669 | unsigned GetAllPathCount() const { |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 670 | assert(TopDownPathCount != 0); |
| 671 | assert(BottomUpPathCount != 0); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 672 | return TopDownPathCount * BottomUpPathCount; |
| 673 | } |
Dan Gohman | 1213027 | 2011-08-12 00:26:31 +0000 | [diff] [blame] | 674 | |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 675 | // Specialized CFG utilities. |
Dan Gohman | dae3349 | 2012-04-27 18:56:31 +0000 | [diff] [blame] | 676 | typedef SmallVectorImpl<BasicBlock *>::const_iterator edge_iterator; |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 677 | edge_iterator pred_begin() { return Preds.begin(); } |
| 678 | edge_iterator pred_end() { return Preds.end(); } |
| 679 | edge_iterator succ_begin() { return Succs.begin(); } |
| 680 | edge_iterator succ_end() { return Succs.end(); } |
| 681 | |
| 682 | void addSucc(BasicBlock *Succ) { Succs.push_back(Succ); } |
| 683 | void addPred(BasicBlock *Pred) { Preds.push_back(Pred); } |
| 684 | |
| 685 | bool isExit() const { return Succs.empty(); } |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 686 | }; |
| 687 | } |
| 688 | |
| 689 | void BBState::InitFromPred(const BBState &Other) { |
| 690 | PerPtrTopDown = Other.PerPtrTopDown; |
| 691 | TopDownPathCount = Other.TopDownPathCount; |
| 692 | } |
| 693 | |
| 694 | void BBState::InitFromSucc(const BBState &Other) { |
| 695 | PerPtrBottomUp = Other.PerPtrBottomUp; |
| 696 | BottomUpPathCount = Other.BottomUpPathCount; |
| 697 | } |
| 698 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 699 | /// The top-down traversal uses this to merge information about predecessors to |
| 700 | /// form the initial state for a new block. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 701 | void BBState::MergePred(const BBState &Other) { |
| 702 | // Other.TopDownPathCount can be 0, in which case it is either dead or a |
| 703 | // loop backedge. Loop backedges are special. |
| 704 | TopDownPathCount += Other.TopDownPathCount; |
| 705 | |
Michael Gottesman | 4385edf | 2013-01-14 01:47:53 +0000 | [diff] [blame] | 706 | // Check for overflow. If we have overflow, fall back to conservative |
| 707 | // behavior. |
Dan Gohman | 7c84dad | 2012-09-12 20:45:17 +0000 | [diff] [blame] | 708 | if (TopDownPathCount < Other.TopDownPathCount) { |
| 709 | clearTopDownPointers(); |
| 710 | return; |
| 711 | } |
| 712 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 713 | // For each entry in the other set, if our set has an entry with the same key, |
| 714 | // merge the entries. Otherwise, copy the entry and merge it with an empty |
| 715 | // entry. |
| 716 | for (ptr_const_iterator MI = Other.top_down_ptr_begin(), |
| 717 | ME = Other.top_down_ptr_end(); MI != ME; ++MI) { |
| 718 | std::pair<ptr_iterator, bool> Pair = PerPtrTopDown.insert(*MI); |
| 719 | Pair.first->second.Merge(Pair.second ? PtrState() : MI->second, |
| 720 | /*TopDown=*/true); |
| 721 | } |
| 722 | |
Dan Gohman | 7e315fc3 | 2011-08-11 21:06:32 +0000 | [diff] [blame] | 723 | // For each entry in our set, if the other set doesn't have an entry with the |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 724 | // same key, force it to merge with an empty entry. |
| 725 | for (ptr_iterator MI = top_down_ptr_begin(), |
| 726 | ME = top_down_ptr_end(); MI != ME; ++MI) |
| 727 | if (Other.PerPtrTopDown.find(MI->first) == Other.PerPtrTopDown.end()) |
| 728 | MI->second.Merge(PtrState(), /*TopDown=*/true); |
| 729 | } |
| 730 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 731 | /// The bottom-up traversal uses this to merge information about successors to |
| 732 | /// form the initial state for a new block. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 733 | void BBState::MergeSucc(const BBState &Other) { |
| 734 | // Other.BottomUpPathCount can be 0, in which case it is either dead or a |
| 735 | // loop backedge. Loop backedges are special. |
| 736 | BottomUpPathCount += Other.BottomUpPathCount; |
| 737 | |
Michael Gottesman | 4385edf | 2013-01-14 01:47:53 +0000 | [diff] [blame] | 738 | // Check for overflow. If we have overflow, fall back to conservative |
| 739 | // behavior. |
Dan Gohman | 7c84dad | 2012-09-12 20:45:17 +0000 | [diff] [blame] | 740 | if (BottomUpPathCount < Other.BottomUpPathCount) { |
| 741 | clearBottomUpPointers(); |
| 742 | return; |
| 743 | } |
| 744 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 745 | // For each entry in the other set, if our set has an entry with the |
| 746 | // same key, merge the entries. Otherwise, copy the entry and merge |
| 747 | // it with an empty entry. |
| 748 | for (ptr_const_iterator MI = Other.bottom_up_ptr_begin(), |
| 749 | ME = Other.bottom_up_ptr_end(); MI != ME; ++MI) { |
| 750 | std::pair<ptr_iterator, bool> Pair = PerPtrBottomUp.insert(*MI); |
| 751 | Pair.first->second.Merge(Pair.second ? PtrState() : MI->second, |
| 752 | /*TopDown=*/false); |
| 753 | } |
| 754 | |
Dan Gohman | 7e315fc3 | 2011-08-11 21:06:32 +0000 | [diff] [blame] | 755 | // For each entry in our set, if the other set doesn't have an entry |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 756 | // with the same key, force it to merge with an empty entry. |
| 757 | for (ptr_iterator MI = bottom_up_ptr_begin(), |
| 758 | ME = bottom_up_ptr_end(); MI != ME; ++MI) |
| 759 | if (Other.PerPtrBottomUp.find(MI->first) == Other.PerPtrBottomUp.end()) |
| 760 | MI->second.Merge(PtrState(), /*TopDown=*/false); |
| 761 | } |
| 762 | |
Michael Gottesman | 81b1d43 | 2013-03-26 00:42:04 +0000 | [diff] [blame] | 763 | // Only enable ARC Annotations if we are building a debug version of |
| 764 | // libObjCARCOpts. |
| 765 | #ifndef NDEBUG |
| 766 | #define ARC_ANNOTATIONS |
| 767 | #endif |
| 768 | |
| 769 | // Define some macros along the lines of DEBUG and some helper functions to make |
| 770 | // it cleaner to create annotations in the source code and to no-op when not |
| 771 | // building in debug mode. |
| 772 | #ifdef ARC_ANNOTATIONS |
| 773 | |
| 774 | #include "llvm/Support/CommandLine.h" |
| 775 | |
| 776 | /// Enable/disable ARC sequence annotations. |
| 777 | static cl::opt<bool> |
Michael Gottesman | 6806b51 | 2013-04-17 20:48:03 +0000 | [diff] [blame] | 778 | EnableARCAnnotations("enable-objc-arc-annotations", cl::init(false), |
| 779 | cl::desc("Enable emission of arc data flow analysis " |
| 780 | "annotations")); |
Michael Gottesman | ffef24f | 2013-04-17 20:48:01 +0000 | [diff] [blame] | 781 | static cl::opt<bool> |
Michael Gottesman | adb921a | 2013-04-17 21:03:53 +0000 | [diff] [blame] | 782 | DisableCheckForCFGHazards("disable-objc-arc-checkforcfghazards", cl::init(false), |
| 783 | cl::desc("Disable check for cfg hazards when " |
| 784 | "annotating")); |
Michael Gottesman | 4e88ce6 | 2013-04-17 21:59:41 +0000 | [diff] [blame] | 785 | static cl::opt<std::string> |
| 786 | ARCAnnotationTargetIdentifier("objc-arc-annotation-target-identifier", |
| 787 | cl::init(""), |
| 788 | cl::desc("filter out all data flow annotations " |
| 789 | "but those that apply to the given " |
| 790 | "target llvm identifier.")); |
Michael Gottesman | 81b1d43 | 2013-03-26 00:42:04 +0000 | [diff] [blame] | 791 | |
| 792 | /// This function appends a unique ARCAnnotationProvenanceSourceMDKind id to an |
| 793 | /// instruction so that we can track backwards when post processing via the llvm |
| 794 | /// arc annotation processor tool. If the function is an |
| 795 | static MDString *AppendMDNodeToSourcePtr(unsigned NodeId, |
| 796 | Value *Ptr) { |
| 797 | MDString *Hash = 0; |
| 798 | |
| 799 | // If pointer is a result of an instruction and it does not have a source |
| 800 | // MDNode it, attach a new MDNode onto it. If pointer is a result of |
| 801 | // an instruction and does have a source MDNode attached to it, return a |
| 802 | // reference to said Node. Otherwise just return 0. |
| 803 | if (Instruction *Inst = dyn_cast<Instruction>(Ptr)) { |
| 804 | MDNode *Node; |
| 805 | if (!(Node = Inst->getMetadata(NodeId))) { |
| 806 | // We do not have any node. Generate and attatch the hash MDString to the |
| 807 | // instruction. |
| 808 | |
| 809 | // We just use an MDString to ensure that this metadata gets written out |
| 810 | // of line at the module level and to provide a very simple format |
| 811 | // encoding the information herein. Both of these makes it simpler to |
| 812 | // parse the annotations by a simple external program. |
| 813 | std::string Str; |
| 814 | raw_string_ostream os(Str); |
| 815 | os << "(" << Inst->getParent()->getParent()->getName() << ",%" |
| 816 | << Inst->getName() << ")"; |
| 817 | |
| 818 | Hash = MDString::get(Inst->getContext(), os.str()); |
| 819 | Inst->setMetadata(NodeId, MDNode::get(Inst->getContext(),Hash)); |
| 820 | } else { |
| 821 | // We have a node. Grab its hash and return it. |
| 822 | assert(Node->getNumOperands() == 1 && |
| 823 | "An ARCAnnotationProvenanceSourceMDKind can only have 1 operand."); |
| 824 | Hash = cast<MDString>(Node->getOperand(0)); |
| 825 | } |
| 826 | } else if (Argument *Arg = dyn_cast<Argument>(Ptr)) { |
| 827 | std::string str; |
| 828 | raw_string_ostream os(str); |
| 829 | os << "(" << Arg->getParent()->getName() << ",%" << Arg->getName() |
| 830 | << ")"; |
| 831 | Hash = MDString::get(Arg->getContext(), os.str()); |
| 832 | } |
| 833 | |
| 834 | return Hash; |
| 835 | } |
| 836 | |
Michael Gottesman | cd4de0f | 2013-03-26 00:42:09 +0000 | [diff] [blame] | 837 | static std::string SequenceToString(Sequence A) { |
| 838 | std::string str; |
| 839 | raw_string_ostream os(str); |
| 840 | os << A; |
| 841 | return os.str(); |
| 842 | } |
| 843 | |
Michael Gottesman | 81b1d43 | 2013-03-26 00:42:04 +0000 | [diff] [blame] | 844 | /// Helper function to change a Sequence into a String object using our overload |
| 845 | /// for raw_ostream so we only have printing code in one location. |
| 846 | static MDString *SequenceToMDString(LLVMContext &Context, |
| 847 | Sequence A) { |
Michael Gottesman | cd4de0f | 2013-03-26 00:42:09 +0000 | [diff] [blame] | 848 | return MDString::get(Context, SequenceToString(A)); |
Michael Gottesman | 81b1d43 | 2013-03-26 00:42:04 +0000 | [diff] [blame] | 849 | } |
| 850 | |
| 851 | /// A simple function to generate a MDNode which describes the change in state |
| 852 | /// for Value *Ptr caused by Instruction *Inst. |
| 853 | static void AppendMDNodeToInstForPtr(unsigned NodeId, |
| 854 | Instruction *Inst, |
| 855 | Value *Ptr, |
| 856 | MDString *PtrSourceMDNodeID, |
| 857 | Sequence OldSeq, |
| 858 | Sequence NewSeq) { |
| 859 | MDNode *Node = 0; |
| 860 | Value *tmp[3] = {PtrSourceMDNodeID, |
| 861 | SequenceToMDString(Inst->getContext(), |
| 862 | OldSeq), |
| 863 | SequenceToMDString(Inst->getContext(), |
| 864 | NewSeq)}; |
| 865 | Node = MDNode::get(Inst->getContext(), |
| 866 | ArrayRef<Value*>(tmp, 3)); |
| 867 | |
| 868 | Inst->setMetadata(NodeId, Node); |
| 869 | } |
| 870 | |
Michael Gottesman | cd4de0f | 2013-03-26 00:42:09 +0000 | [diff] [blame] | 871 | /// Add to the beginning of the basic block llvm.ptr.annotations which show the |
| 872 | /// state of a pointer at the entrance to a basic block. |
| 873 | static void GenerateARCBBEntranceAnnotation(const char *Name, BasicBlock *BB, |
| 874 | Value *Ptr, Sequence Seq) { |
Michael Gottesman | 4e88ce6 | 2013-04-17 21:59:41 +0000 | [diff] [blame] | 875 | // If we have a target identifier, make sure that we match it before |
| 876 | // continuing. |
| 877 | if(!ARCAnnotationTargetIdentifier.empty() && |
| 878 | !Ptr->getName().equals(ARCAnnotationTargetIdentifier)) |
| 879 | return; |
Michael Gottesman | 9e51813 | 2013-04-18 04:34:11 +0000 | [diff] [blame] | 880 | |
Michael Gottesman | cd4de0f | 2013-03-26 00:42:09 +0000 | [diff] [blame] | 881 | Module *M = BB->getParent()->getParent(); |
| 882 | LLVMContext &C = M->getContext(); |
| 883 | Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C)); |
| 884 | Type *I8XX = PointerType::getUnqual(I8X); |
| 885 | Type *Params[] = {I8XX, I8XX}; |
| 886 | FunctionType *FTy = FunctionType::get(Type::getVoidTy(C), |
| 887 | ArrayRef<Type*>(Params, 2), |
| 888 | /*isVarArg=*/false); |
| 889 | Constant *Callee = M->getOrInsertFunction(Name, FTy); |
Michael Gottesman | 60f6b28 | 2013-03-29 05:13:07 +0000 | [diff] [blame] | 890 | |
| 891 | IRBuilder<> Builder(BB, BB->getFirstInsertionPt()); |
| 892 | |
Michael Gottesman | cd4de0f | 2013-03-26 00:42:09 +0000 | [diff] [blame] | 893 | Value *PtrName; |
| 894 | StringRef Tmp = Ptr->getName(); |
| 895 | if (0 == (PtrName = M->getGlobalVariable(Tmp, true))) { |
| 896 | Value *ActualPtrName = Builder.CreateGlobalStringPtr(Tmp, |
| 897 | Tmp + "_STR"); |
| 898 | PtrName = new GlobalVariable(*M, I8X, true, GlobalVariable::InternalLinkage, |
Michael Gottesman | 60f6b28 | 2013-03-29 05:13:07 +0000 | [diff] [blame] | 899 | cast<Constant>(ActualPtrName), Tmp); |
Michael Gottesman | cd4de0f | 2013-03-26 00:42:09 +0000 | [diff] [blame] | 900 | } |
| 901 | |
| 902 | Value *S; |
| 903 | std::string SeqStr = SequenceToString(Seq); |
| 904 | if (0 == (S = M->getGlobalVariable(SeqStr, true))) { |
| 905 | Value *ActualPtrName = Builder.CreateGlobalStringPtr(SeqStr, |
| 906 | SeqStr + "_STR"); |
| 907 | S = new GlobalVariable(*M, I8X, true, GlobalVariable::InternalLinkage, |
| 908 | cast<Constant>(ActualPtrName), SeqStr); |
| 909 | } |
| 910 | |
| 911 | Builder.CreateCall2(Callee, PtrName, S); |
| 912 | } |
| 913 | |
| 914 | /// Add to the end of the basic block llvm.ptr.annotations which show the state |
| 915 | /// of the pointer at the bottom of the basic block. |
| 916 | static void GenerateARCBBTerminatorAnnotation(const char *Name, BasicBlock *BB, |
| 917 | Value *Ptr, Sequence Seq) { |
Michael Gottesman | 4e88ce6 | 2013-04-17 21:59:41 +0000 | [diff] [blame] | 918 | // If we have a target identifier, make sure that we match it before emitting |
| 919 | // an annotation. |
| 920 | if(!ARCAnnotationTargetIdentifier.empty() && |
| 921 | !Ptr->getName().equals(ARCAnnotationTargetIdentifier)) |
| 922 | return; |
Michael Gottesman | 9e51813 | 2013-04-18 04:34:11 +0000 | [diff] [blame] | 923 | |
Michael Gottesman | cd4de0f | 2013-03-26 00:42:09 +0000 | [diff] [blame] | 924 | Module *M = BB->getParent()->getParent(); |
| 925 | LLVMContext &C = M->getContext(); |
| 926 | Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C)); |
| 927 | Type *I8XX = PointerType::getUnqual(I8X); |
| 928 | Type *Params[] = {I8XX, I8XX}; |
| 929 | FunctionType *FTy = FunctionType::get(Type::getVoidTy(C), |
| 930 | ArrayRef<Type*>(Params, 2), |
| 931 | /*isVarArg=*/false); |
| 932 | Constant *Callee = M->getOrInsertFunction(Name, FTy); |
Michael Gottesman | 60f6b28 | 2013-03-29 05:13:07 +0000 | [diff] [blame] | 933 | |
| 934 | IRBuilder<> Builder(BB, llvm::prior(BB->end())); |
| 935 | |
Michael Gottesman | cd4de0f | 2013-03-26 00:42:09 +0000 | [diff] [blame] | 936 | Value *PtrName; |
| 937 | StringRef Tmp = Ptr->getName(); |
| 938 | if (0 == (PtrName = M->getGlobalVariable(Tmp, true))) { |
| 939 | Value *ActualPtrName = Builder.CreateGlobalStringPtr(Tmp, |
| 940 | Tmp + "_STR"); |
| 941 | PtrName = new GlobalVariable(*M, I8X, true, GlobalVariable::InternalLinkage, |
Michael Gottesman | 60f6b28 | 2013-03-29 05:13:07 +0000 | [diff] [blame] | 942 | cast<Constant>(ActualPtrName), Tmp); |
Michael Gottesman | cd4de0f | 2013-03-26 00:42:09 +0000 | [diff] [blame] | 943 | } |
| 944 | |
| 945 | Value *S; |
| 946 | std::string SeqStr = SequenceToString(Seq); |
| 947 | if (0 == (S = M->getGlobalVariable(SeqStr, true))) { |
| 948 | Value *ActualPtrName = Builder.CreateGlobalStringPtr(SeqStr, |
| 949 | SeqStr + "_STR"); |
| 950 | S = new GlobalVariable(*M, I8X, true, GlobalVariable::InternalLinkage, |
| 951 | cast<Constant>(ActualPtrName), SeqStr); |
| 952 | } |
Michael Gottesman | 60f6b28 | 2013-03-29 05:13:07 +0000 | [diff] [blame] | 953 | Builder.CreateCall2(Callee, PtrName, S); |
Michael Gottesman | cd4de0f | 2013-03-26 00:42:09 +0000 | [diff] [blame] | 954 | } |
| 955 | |
Michael Gottesman | 81b1d43 | 2013-03-26 00:42:04 +0000 | [diff] [blame] | 956 | /// Adds a source annotation to pointer and a state change annotation to Inst |
| 957 | /// referencing the source annotation and the old/new state of pointer. |
| 958 | static void GenerateARCAnnotation(unsigned InstMDId, |
| 959 | unsigned PtrMDId, |
| 960 | Instruction *Inst, |
| 961 | Value *Ptr, |
| 962 | Sequence OldSeq, |
| 963 | Sequence NewSeq) { |
| 964 | if (EnableARCAnnotations) { |
Michael Gottesman | 4e88ce6 | 2013-04-17 21:59:41 +0000 | [diff] [blame] | 965 | // If we have a target identifier, make sure that we match it before |
| 966 | // emitting an annotation. |
| 967 | if(!ARCAnnotationTargetIdentifier.empty() && |
| 968 | !Ptr->getName().equals(ARCAnnotationTargetIdentifier)) |
| 969 | return; |
Michael Gottesman | 9e51813 | 2013-04-18 04:34:11 +0000 | [diff] [blame] | 970 | |
Michael Gottesman | 81b1d43 | 2013-03-26 00:42:04 +0000 | [diff] [blame] | 971 | // First generate the source annotation on our pointer. This will return an |
| 972 | // MDString* if Ptr actually comes from an instruction implying we can put |
| 973 | // in a source annotation. If AppendMDNodeToSourcePtr returns 0 (i.e. NULL), |
| 974 | // then we know that our pointer is from an Argument so we put a reference |
| 975 | // to the argument number. |
| 976 | // |
| 977 | // The point of this is to make it easy for the |
| 978 | // llvm-arc-annotation-processor tool to cross reference where the source |
| 979 | // pointer is in the LLVM IR since the LLVM IR parser does not submit such |
| 980 | // information via debug info for backends to use (since why would anyone |
| 981 | // need such a thing from LLVM IR besides in non standard cases |
| 982 | // [i.e. this]). |
| 983 | MDString *SourcePtrMDNode = |
| 984 | AppendMDNodeToSourcePtr(PtrMDId, Ptr); |
| 985 | AppendMDNodeToInstForPtr(InstMDId, Inst, Ptr, SourcePtrMDNode, OldSeq, |
| 986 | NewSeq); |
| 987 | } |
| 988 | } |
| 989 | |
| 990 | // The actual interface for accessing the above functionality is defined via |
| 991 | // some simple macros which are defined below. We do this so that the user does |
| 992 | // not need to pass in what metadata id is needed resulting in cleaner code and |
| 993 | // additionally since it provides an easy way to conditionally no-op all |
| 994 | // annotation support in a non-debug build. |
| 995 | |
| 996 | /// Use this macro to annotate a sequence state change when processing |
| 997 | /// instructions bottom up, |
| 998 | #define ANNOTATE_BOTTOMUP(inst, ptr, old, new) \ |
| 999 | GenerateARCAnnotation(ARCAnnotationBottomUpMDKind, \ |
| 1000 | ARCAnnotationProvenanceSourceMDKind, (inst), \ |
| 1001 | const_cast<Value*>(ptr), (old), (new)) |
| 1002 | /// Use this macro to annotate a sequence state change when processing |
| 1003 | /// instructions top down. |
| 1004 | #define ANNOTATE_TOPDOWN(inst, ptr, old, new) \ |
| 1005 | GenerateARCAnnotation(ARCAnnotationTopDownMDKind, \ |
| 1006 | ARCAnnotationProvenanceSourceMDKind, (inst), \ |
| 1007 | const_cast<Value*>(ptr), (old), (new)) |
| 1008 | |
Michael Gottesman | 43e7e00 | 2013-04-03 22:41:59 +0000 | [diff] [blame] | 1009 | #define ANNOTATE_BB(_states, _bb, _name, _type, _direction) \ |
| 1010 | do { \ |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1011 | if (EnableARCAnnotations) { \ |
| 1012 | for(BBState::ptr_const_iterator I = (_states)._direction##_ptr_begin(), \ |
Michael Gottesman | 43e7e00 | 2013-04-03 22:41:59 +0000 | [diff] [blame] | 1013 | E = (_states)._direction##_ptr_end(); I != E; ++I) { \ |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1014 | Value *Ptr = const_cast<Value*>(I->first); \ |
| 1015 | Sequence Seq = I->second.GetSeq(); \ |
| 1016 | GenerateARCBB ## _type ## Annotation(_name, (_bb), Ptr, Seq); \ |
| 1017 | } \ |
Michael Gottesman | 43e7e00 | 2013-04-03 22:41:59 +0000 | [diff] [blame] | 1018 | } \ |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1019 | } while (0) |
Michael Gottesman | 43e7e00 | 2013-04-03 22:41:59 +0000 | [diff] [blame] | 1020 | |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1021 | #define ANNOTATE_BOTTOMUP_BBSTART(_states, _basicblock) \ |
Michael Gottesman | 43e7e00 | 2013-04-03 22:41:59 +0000 | [diff] [blame] | 1022 | ANNOTATE_BB(_states, _basicblock, "llvm.arc.annotation.bottomup.bbstart", \ |
| 1023 | Entrance, bottom_up) |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1024 | #define ANNOTATE_BOTTOMUP_BBEND(_states, _basicblock) \ |
| 1025 | ANNOTATE_BB(_states, _basicblock, "llvm.arc.annotation.bottomup.bbend", \ |
Michael Gottesman | 43e7e00 | 2013-04-03 22:41:59 +0000 | [diff] [blame] | 1026 | Terminator, bottom_up) |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1027 | #define ANNOTATE_TOPDOWN_BBSTART(_states, _basicblock) \ |
| 1028 | ANNOTATE_BB(_states, _basicblock, "llvm.arc.annotation.topdown.bbstart", \ |
Michael Gottesman | 43e7e00 | 2013-04-03 22:41:59 +0000 | [diff] [blame] | 1029 | Entrance, top_down) |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1030 | #define ANNOTATE_TOPDOWN_BBEND(_states, _basicblock) \ |
| 1031 | ANNOTATE_BB(_states, _basicblock, "llvm.arc.annotation.topdown.bbend", \ |
Michael Gottesman | 43e7e00 | 2013-04-03 22:41:59 +0000 | [diff] [blame] | 1032 | Terminator, top_down) |
| 1033 | |
Michael Gottesman | 81b1d43 | 2013-03-26 00:42:04 +0000 | [diff] [blame] | 1034 | #else // !ARC_ANNOTATION |
| 1035 | // If annotations are off, noop. |
| 1036 | #define ANNOTATE_BOTTOMUP(inst, ptr, old, new) |
| 1037 | #define ANNOTATE_TOPDOWN(inst, ptr, old, new) |
Michael Gottesman | 43e7e00 | 2013-04-03 22:41:59 +0000 | [diff] [blame] | 1038 | #define ANNOTATE_BOTTOMUP_BBSTART(states, basicblock) |
| 1039 | #define ANNOTATE_BOTTOMUP_BBEND(states, basicblock) |
| 1040 | #define ANNOTATE_TOPDOWN_BBSTART(states, basicblock) |
| 1041 | #define ANNOTATE_TOPDOWN_BBEND(states, basicblock) |
Michael Gottesman | 81b1d43 | 2013-03-26 00:42:04 +0000 | [diff] [blame] | 1042 | #endif // !ARC_ANNOTATION |
| 1043 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1044 | namespace { |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 1045 | /// \brief The main ARC optimization pass. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1046 | class ObjCARCOpt : public FunctionPass { |
| 1047 | bool Changed; |
| 1048 | ProvenanceAnalysis PA; |
| 1049 | |
Michael Gottesman | 5a91bbf | 2013-05-24 20:44:02 +0000 | [diff] [blame^] | 1050 | // This is used to track if a pointer is stored into an alloca. |
| 1051 | DenseSet<const Value *> MultiOwnersSet; |
| 1052 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 1053 | /// A flag indicating whether this optimization pass should run. |
Dan Gohman | ceaac7c | 2011-06-20 23:20:43 +0000 | [diff] [blame] | 1054 | bool Run; |
| 1055 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 1056 | /// Declarations for ObjC runtime functions, for use in creating calls to |
| 1057 | /// them. These are initialized lazily to avoid cluttering up the Module |
| 1058 | /// with unused declarations. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1059 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 1060 | /// Declaration for ObjC runtime function objc_autoreleaseReturnValue. |
| 1061 | Constant *AutoreleaseRVCallee; |
| 1062 | /// Declaration for ObjC runtime function objc_release. |
| 1063 | Constant *ReleaseCallee; |
| 1064 | /// Declaration for ObjC runtime function objc_retain. |
| 1065 | Constant *RetainCallee; |
| 1066 | /// Declaration for ObjC runtime function objc_retainBlock. |
| 1067 | Constant *RetainBlockCallee; |
| 1068 | /// Declaration for ObjC runtime function objc_autorelease. |
| 1069 | Constant *AutoreleaseCallee; |
| 1070 | |
| 1071 | /// Flags which determine whether each of the interesting runtine functions |
| 1072 | /// is in fact used in the current function. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1073 | unsigned UsedInThisFunction; |
| 1074 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 1075 | /// The Metadata Kind for clang.imprecise_release metadata. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1076 | unsigned ImpreciseReleaseMDKind; |
| 1077 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 1078 | /// The Metadata Kind for clang.arc.copy_on_escape metadata. |
Dan Gohman | a7107f9 | 2011-10-17 22:53:25 +0000 | [diff] [blame] | 1079 | unsigned CopyOnEscapeMDKind; |
| 1080 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 1081 | /// The Metadata Kind for clang.arc.no_objc_arc_exceptions metadata. |
Dan Gohman | 0155f30 | 2012-02-17 18:59:53 +0000 | [diff] [blame] | 1082 | unsigned NoObjCARCExceptionsMDKind; |
| 1083 | |
Michael Gottesman | 81b1d43 | 2013-03-26 00:42:04 +0000 | [diff] [blame] | 1084 | #ifdef ARC_ANNOTATIONS |
| 1085 | /// The Metadata Kind for llvm.arc.annotation.bottomup metadata. |
| 1086 | unsigned ARCAnnotationBottomUpMDKind; |
| 1087 | /// The Metadata Kind for llvm.arc.annotation.topdown metadata. |
| 1088 | unsigned ARCAnnotationTopDownMDKind; |
| 1089 | /// The Metadata Kind for llvm.arc.annotation.provenancesource metadata. |
| 1090 | unsigned ARCAnnotationProvenanceSourceMDKind; |
| 1091 | #endif // ARC_ANNOATIONS |
| 1092 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1093 | Constant *getAutoreleaseRVCallee(Module *M); |
| 1094 | Constant *getReleaseCallee(Module *M); |
| 1095 | Constant *getRetainCallee(Module *M); |
Dan Gohman | 6320f52 | 2011-07-22 22:29:21 +0000 | [diff] [blame] | 1096 | Constant *getRetainBlockCallee(Module *M); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1097 | Constant *getAutoreleaseCallee(Module *M); |
| 1098 | |
Dan Gohman | 728db49 | 2012-01-13 00:39:07 +0000 | [diff] [blame] | 1099 | bool IsRetainBlockOptimizable(const Instruction *Inst); |
| 1100 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1101 | bool OptimizeRetainRVCall(Function &F, Instruction *RetainRV); |
Michael Gottesman | 556ff61 | 2013-01-12 01:25:19 +0000 | [diff] [blame] | 1102 | void OptimizeAutoreleaseRVCall(Function &F, Instruction *AutoreleaseRV, |
| 1103 | InstructionClass &Class); |
Michael Gottesman | 158fdf6 | 2013-03-28 20:11:19 +0000 | [diff] [blame] | 1104 | bool OptimizeRetainBlockCall(Function &F, Instruction *RetainBlock, |
| 1105 | InstructionClass &Class); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1106 | void OptimizeIndividualCalls(Function &F); |
| 1107 | |
| 1108 | void CheckForCFGHazards(const BasicBlock *BB, |
| 1109 | DenseMap<const BasicBlock *, BBState> &BBStates, |
| 1110 | BBState &MyStates) const; |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1111 | bool VisitInstructionBottomUp(Instruction *Inst, |
Dan Gohman | 5c70fad | 2012-03-23 17:47:54 +0000 | [diff] [blame] | 1112 | BasicBlock *BB, |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1113 | MapVector<Value *, RRInfo> &Retains, |
| 1114 | BBState &MyStates); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1115 | bool VisitBottomUp(BasicBlock *BB, |
| 1116 | DenseMap<const BasicBlock *, BBState> &BBStates, |
| 1117 | MapVector<Value *, RRInfo> &Retains); |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1118 | bool VisitInstructionTopDown(Instruction *Inst, |
| 1119 | DenseMap<Value *, RRInfo> &Releases, |
| 1120 | BBState &MyStates); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1121 | bool VisitTopDown(BasicBlock *BB, |
| 1122 | DenseMap<const BasicBlock *, BBState> &BBStates, |
| 1123 | DenseMap<Value *, RRInfo> &Releases); |
| 1124 | bool Visit(Function &F, |
| 1125 | DenseMap<const BasicBlock *, BBState> &BBStates, |
| 1126 | MapVector<Value *, RRInfo> &Retains, |
| 1127 | DenseMap<Value *, RRInfo> &Releases); |
| 1128 | |
| 1129 | void MoveCalls(Value *Arg, RRInfo &RetainsToMove, RRInfo &ReleasesToMove, |
| 1130 | MapVector<Value *, RRInfo> &Retains, |
| 1131 | DenseMap<Value *, RRInfo> &Releases, |
Dan Gohman | 6320f52 | 2011-07-22 22:29:21 +0000 | [diff] [blame] | 1132 | SmallVectorImpl<Instruction *> &DeadInsts, |
| 1133 | Module *M); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1134 | |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 1135 | bool ConnectTDBUTraversals(DenseMap<const BasicBlock *, BBState> &BBStates, |
| 1136 | MapVector<Value *, RRInfo> &Retains, |
| 1137 | DenseMap<Value *, RRInfo> &Releases, |
| 1138 | Module *M, |
| 1139 | SmallVector<Instruction *, 4> &NewRetains, |
| 1140 | SmallVector<Instruction *, 4> &NewReleases, |
| 1141 | SmallVector<Instruction *, 8> &DeadInsts, |
| 1142 | RRInfo &RetainsToMove, |
| 1143 | RRInfo &ReleasesToMove, |
| 1144 | Value *Arg, |
| 1145 | bool KnownSafe, |
| 1146 | bool &AnyPairsCompletelyEliminated); |
| 1147 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1148 | bool PerformCodePlacement(DenseMap<const BasicBlock *, BBState> &BBStates, |
| 1149 | MapVector<Value *, RRInfo> &Retains, |
Dan Gohman | 6320f52 | 2011-07-22 22:29:21 +0000 | [diff] [blame] | 1150 | DenseMap<Value *, RRInfo> &Releases, |
| 1151 | Module *M); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1152 | |
| 1153 | void OptimizeWeakCalls(Function &F); |
| 1154 | |
| 1155 | bool OptimizeSequences(Function &F); |
| 1156 | |
| 1157 | void OptimizeReturns(Function &F); |
| 1158 | |
Michael Gottesman | 9c11815 | 2013-04-29 06:16:57 +0000 | [diff] [blame] | 1159 | #ifndef NDEBUG |
| 1160 | void GatherStatistics(Function &F, bool AfterOptimization = false); |
| 1161 | #endif |
| 1162 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1163 | virtual void getAnalysisUsage(AnalysisUsage &AU) const; |
| 1164 | virtual bool doInitialization(Module &M); |
| 1165 | virtual bool runOnFunction(Function &F); |
| 1166 | virtual void releaseMemory(); |
| 1167 | |
| 1168 | public: |
| 1169 | static char ID; |
| 1170 | ObjCARCOpt() : FunctionPass(ID) { |
| 1171 | initializeObjCARCOptPass(*PassRegistry::getPassRegistry()); |
| 1172 | } |
| 1173 | }; |
| 1174 | } |
| 1175 | |
| 1176 | char ObjCARCOpt::ID = 0; |
| 1177 | INITIALIZE_PASS_BEGIN(ObjCARCOpt, |
| 1178 | "objc-arc", "ObjC ARC optimization", false, false) |
| 1179 | INITIALIZE_PASS_DEPENDENCY(ObjCARCAliasAnalysis) |
| 1180 | INITIALIZE_PASS_END(ObjCARCOpt, |
| 1181 | "objc-arc", "ObjC ARC optimization", false, false) |
| 1182 | |
| 1183 | Pass *llvm::createObjCARCOptPass() { |
| 1184 | return new ObjCARCOpt(); |
| 1185 | } |
| 1186 | |
| 1187 | void ObjCARCOpt::getAnalysisUsage(AnalysisUsage &AU) const { |
| 1188 | AU.addRequired<ObjCARCAliasAnalysis>(); |
| 1189 | AU.addRequired<AliasAnalysis>(); |
| 1190 | // ARC optimization doesn't currently split critical edges. |
| 1191 | AU.setPreservesCFG(); |
| 1192 | } |
| 1193 | |
Dan Gohman | 728db49 | 2012-01-13 00:39:07 +0000 | [diff] [blame] | 1194 | bool ObjCARCOpt::IsRetainBlockOptimizable(const Instruction *Inst) { |
| 1195 | // Without the magic metadata tag, we have to assume this might be an |
| 1196 | // objc_retainBlock call inserted to convert a block pointer to an id, |
| 1197 | // in which case it really is needed. |
| 1198 | if (!Inst->getMetadata(CopyOnEscapeMDKind)) |
| 1199 | return false; |
| 1200 | |
| 1201 | // If the pointer "escapes" (not including being used in a call), |
| 1202 | // the copy may be needed. |
Michael Gottesman | 774d2c0 | 2013-01-29 21:00:52 +0000 | [diff] [blame] | 1203 | if (DoesRetainableObjPtrEscape(Inst)) |
Dan Gohman | 728db49 | 2012-01-13 00:39:07 +0000 | [diff] [blame] | 1204 | return false; |
| 1205 | |
| 1206 | // Otherwise, it's not needed. |
| 1207 | return true; |
| 1208 | } |
| 1209 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1210 | Constant *ObjCARCOpt::getAutoreleaseRVCallee(Module *M) { |
| 1211 | if (!AutoreleaseRVCallee) { |
| 1212 | LLVMContext &C = M->getContext(); |
Jay Foad | b804a2b | 2011-07-12 14:06:48 +0000 | [diff] [blame] | 1213 | Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C)); |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 1214 | Type *Params[] = { I8X }; |
| 1215 | FunctionType *FTy = FunctionType::get(I8X, Params, /*isVarArg=*/false); |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 1216 | AttributeSet Attribute = |
Bill Wendling | 09175b3 | 2013-01-22 21:15:51 +0000 | [diff] [blame] | 1217 | AttributeSet().addAttribute(M->getContext(), AttributeSet::FunctionIndex, |
| 1218 | Attribute::NoUnwind); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1219 | AutoreleaseRVCallee = |
| 1220 | M->getOrInsertFunction("objc_autoreleaseReturnValue", FTy, |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 1221 | Attribute); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1222 | } |
| 1223 | return AutoreleaseRVCallee; |
| 1224 | } |
| 1225 | |
| 1226 | Constant *ObjCARCOpt::getReleaseCallee(Module *M) { |
| 1227 | if (!ReleaseCallee) { |
| 1228 | LLVMContext &C = M->getContext(); |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 1229 | Type *Params[] = { PointerType::getUnqual(Type::getInt8Ty(C)) }; |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 1230 | AttributeSet Attribute = |
Bill Wendling | 09175b3 | 2013-01-22 21:15:51 +0000 | [diff] [blame] | 1231 | AttributeSet().addAttribute(M->getContext(), AttributeSet::FunctionIndex, |
| 1232 | Attribute::NoUnwind); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1233 | ReleaseCallee = |
| 1234 | M->getOrInsertFunction( |
| 1235 | "objc_release", |
| 1236 | FunctionType::get(Type::getVoidTy(C), Params, /*isVarArg=*/false), |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 1237 | Attribute); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1238 | } |
| 1239 | return ReleaseCallee; |
| 1240 | } |
| 1241 | |
| 1242 | Constant *ObjCARCOpt::getRetainCallee(Module *M) { |
| 1243 | if (!RetainCallee) { |
| 1244 | LLVMContext &C = M->getContext(); |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 1245 | Type *Params[] = { PointerType::getUnqual(Type::getInt8Ty(C)) }; |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 1246 | AttributeSet Attribute = |
Bill Wendling | 09175b3 | 2013-01-22 21:15:51 +0000 | [diff] [blame] | 1247 | AttributeSet().addAttribute(M->getContext(), AttributeSet::FunctionIndex, |
| 1248 | Attribute::NoUnwind); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1249 | RetainCallee = |
| 1250 | M->getOrInsertFunction( |
| 1251 | "objc_retain", |
| 1252 | FunctionType::get(Params[0], Params, /*isVarArg=*/false), |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 1253 | Attribute); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1254 | } |
| 1255 | return RetainCallee; |
| 1256 | } |
| 1257 | |
Dan Gohman | 6320f52 | 2011-07-22 22:29:21 +0000 | [diff] [blame] | 1258 | Constant *ObjCARCOpt::getRetainBlockCallee(Module *M) { |
| 1259 | if (!RetainBlockCallee) { |
| 1260 | LLVMContext &C = M->getContext(); |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 1261 | Type *Params[] = { PointerType::getUnqual(Type::getInt8Ty(C)) }; |
Dan Gohman | fca43c2 | 2011-09-14 18:33:34 +0000 | [diff] [blame] | 1262 | // objc_retainBlock is not nounwind because it calls user copy constructors |
| 1263 | // which could theoretically throw. |
Dan Gohman | 6320f52 | 2011-07-22 22:29:21 +0000 | [diff] [blame] | 1264 | RetainBlockCallee = |
| 1265 | M->getOrInsertFunction( |
| 1266 | "objc_retainBlock", |
| 1267 | FunctionType::get(Params[0], Params, /*isVarArg=*/false), |
Bill Wendling | e94d843 | 2012-12-07 23:16:57 +0000 | [diff] [blame] | 1268 | AttributeSet()); |
Dan Gohman | 6320f52 | 2011-07-22 22:29:21 +0000 | [diff] [blame] | 1269 | } |
| 1270 | return RetainBlockCallee; |
| 1271 | } |
| 1272 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1273 | Constant *ObjCARCOpt::getAutoreleaseCallee(Module *M) { |
| 1274 | if (!AutoreleaseCallee) { |
| 1275 | LLVMContext &C = M->getContext(); |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 1276 | Type *Params[] = { PointerType::getUnqual(Type::getInt8Ty(C)) }; |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 1277 | AttributeSet Attribute = |
Bill Wendling | 09175b3 | 2013-01-22 21:15:51 +0000 | [diff] [blame] | 1278 | AttributeSet().addAttribute(M->getContext(), AttributeSet::FunctionIndex, |
| 1279 | Attribute::NoUnwind); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1280 | AutoreleaseCallee = |
| 1281 | M->getOrInsertFunction( |
| 1282 | "objc_autorelease", |
| 1283 | FunctionType::get(Params[0], Params, /*isVarArg=*/false), |
Bill Wendling | 3d7b0b8 | 2012-12-19 07:18:57 +0000 | [diff] [blame] | 1284 | Attribute); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1285 | } |
| 1286 | return AutoreleaseCallee; |
| 1287 | } |
| 1288 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 1289 | /// Turn objc_retainAutoreleasedReturnValue into objc_retain if the operand is |
| 1290 | /// not a return value. Or, if it can be paired with an |
| 1291 | /// objc_autoreleaseReturnValue, delete the pair and return true. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1292 | bool |
| 1293 | ObjCARCOpt::OptimizeRetainRVCall(Function &F, Instruction *RetainRV) { |
Dan Gohman | e3ed2b0 | 2012-03-23 18:09:00 +0000 | [diff] [blame] | 1294 | // Check for the argument being from an immediately preceding call or invoke. |
Dan Gohman | dae3349 | 2012-04-27 18:56:31 +0000 | [diff] [blame] | 1295 | const Value *Arg = GetObjCArg(RetainRV); |
| 1296 | ImmutableCallSite CS(Arg); |
| 1297 | if (const Instruction *Call = CS.getInstruction()) { |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1298 | if (Call->getParent() == RetainRV->getParent()) { |
Dan Gohman | dae3349 | 2012-04-27 18:56:31 +0000 | [diff] [blame] | 1299 | BasicBlock::const_iterator I = Call; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1300 | ++I; |
Michael Gottesman | 65c2481 | 2013-03-25 09:27:43 +0000 | [diff] [blame] | 1301 | while (IsNoopInstruction(I)) ++I; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1302 | if (&*I == RetainRV) |
| 1303 | return false; |
Dan Gohman | dae3349 | 2012-04-27 18:56:31 +0000 | [diff] [blame] | 1304 | } else if (const InvokeInst *II = dyn_cast<InvokeInst>(Call)) { |
Dan Gohman | e3ed2b0 | 2012-03-23 18:09:00 +0000 | [diff] [blame] | 1305 | BasicBlock *RetainRVParent = RetainRV->getParent(); |
| 1306 | if (II->getNormalDest() == RetainRVParent) { |
Dan Gohman | dae3349 | 2012-04-27 18:56:31 +0000 | [diff] [blame] | 1307 | BasicBlock::const_iterator I = RetainRVParent->begin(); |
Michael Gottesman | 65c2481 | 2013-03-25 09:27:43 +0000 | [diff] [blame] | 1308 | while (IsNoopInstruction(I)) ++I; |
Dan Gohman | e3ed2b0 | 2012-03-23 18:09:00 +0000 | [diff] [blame] | 1309 | if (&*I == RetainRV) |
| 1310 | return false; |
| 1311 | } |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1312 | } |
Dan Gohman | e3ed2b0 | 2012-03-23 18:09:00 +0000 | [diff] [blame] | 1313 | } |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1314 | |
| 1315 | // Check for being preceded by an objc_autoreleaseReturnValue on the same |
| 1316 | // pointer. In this case, we can delete the pair. |
| 1317 | BasicBlock::iterator I = RetainRV, Begin = RetainRV->getParent()->begin(); |
| 1318 | if (I != Begin) { |
Michael Gottesman | 65c2481 | 2013-03-25 09:27:43 +0000 | [diff] [blame] | 1319 | do --I; while (I != Begin && IsNoopInstruction(I)); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1320 | if (GetBasicInstructionClass(I) == IC_AutoreleaseRV && |
| 1321 | GetObjCArg(I) == Arg) { |
| 1322 | Changed = true; |
| 1323 | ++NumPeeps; |
Michael Gottesman | 10426b5 | 2013-01-07 21:26:07 +0000 | [diff] [blame] | 1324 | |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1325 | DEBUG(dbgs() << "Erasing autoreleaseRV,retainRV pair: " << *I << "\n" |
| 1326 | << "Erasing " << *RetainRV << "\n"); |
Michael Gottesman | 10426b5 | 2013-01-07 21:26:07 +0000 | [diff] [blame] | 1327 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1328 | EraseInstruction(I); |
| 1329 | EraseInstruction(RetainRV); |
| 1330 | return true; |
| 1331 | } |
| 1332 | } |
| 1333 | |
| 1334 | // Turn it to a plain objc_retain. |
| 1335 | Changed = true; |
| 1336 | ++NumPeeps; |
Michael Gottesman | 10426b5 | 2013-01-07 21:26:07 +0000 | [diff] [blame] | 1337 | |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1338 | DEBUG(dbgs() << "Transforming objc_retainAutoreleasedReturnValue => " |
Michael Gottesman | def07bb | 2013-01-05 17:55:42 +0000 | [diff] [blame] | 1339 | "objc_retain since the operand is not a return value.\n" |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1340 | "Old = " << *RetainRV << "\n"); |
Michael Gottesman | 10426b5 | 2013-01-07 21:26:07 +0000 | [diff] [blame] | 1341 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1342 | cast<CallInst>(RetainRV)->setCalledFunction(getRetainCallee(F.getParent())); |
Michael Gottesman | def07bb | 2013-01-05 17:55:42 +0000 | [diff] [blame] | 1343 | |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1344 | DEBUG(dbgs() << "New = " << *RetainRV << "\n"); |
Michael Gottesman | def07bb | 2013-01-05 17:55:42 +0000 | [diff] [blame] | 1345 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1346 | return false; |
| 1347 | } |
| 1348 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 1349 | /// Turn objc_autoreleaseReturnValue into objc_autorelease if the result is not |
| 1350 | /// used as a return value. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1351 | void |
Michael Gottesman | 556ff61 | 2013-01-12 01:25:19 +0000 | [diff] [blame] | 1352 | ObjCARCOpt::OptimizeAutoreleaseRVCall(Function &F, Instruction *AutoreleaseRV, |
| 1353 | InstructionClass &Class) { |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1354 | // Check for a return of the pointer value. |
| 1355 | const Value *Ptr = GetObjCArg(AutoreleaseRV); |
Dan Gohman | 10a18d5 | 2011-08-12 00:36:31 +0000 | [diff] [blame] | 1356 | SmallVector<const Value *, 2> Users; |
| 1357 | Users.push_back(Ptr); |
| 1358 | do { |
| 1359 | Ptr = Users.pop_back_val(); |
| 1360 | for (Value::const_use_iterator UI = Ptr->use_begin(), UE = Ptr->use_end(); |
| 1361 | UI != UE; ++UI) { |
| 1362 | const User *I = *UI; |
| 1363 | if (isa<ReturnInst>(I) || GetBasicInstructionClass(I) == IC_RetainRV) |
| 1364 | return; |
| 1365 | if (isa<BitCastInst>(I)) |
| 1366 | Users.push_back(I); |
| 1367 | } |
| 1368 | } while (!Users.empty()); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1369 | |
| 1370 | Changed = true; |
| 1371 | ++NumPeeps; |
Michael Gottesman | 1bf6908 | 2013-01-06 21:07:11 +0000 | [diff] [blame] | 1372 | |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1373 | DEBUG(dbgs() << "Transforming objc_autoreleaseReturnValue => " |
Michael Gottesman | 1bf6908 | 2013-01-06 21:07:11 +0000 | [diff] [blame] | 1374 | "objc_autorelease since its operand is not used as a return " |
| 1375 | "value.\n" |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1376 | "Old = " << *AutoreleaseRV << "\n"); |
Michael Gottesman | 1bf6908 | 2013-01-06 21:07:11 +0000 | [diff] [blame] | 1377 | |
Michael Gottesman | c9656fa | 2013-01-12 01:25:15 +0000 | [diff] [blame] | 1378 | CallInst *AutoreleaseRVCI = cast<CallInst>(AutoreleaseRV); |
| 1379 | AutoreleaseRVCI-> |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1380 | setCalledFunction(getAutoreleaseCallee(F.getParent())); |
Michael Gottesman | c9656fa | 2013-01-12 01:25:15 +0000 | [diff] [blame] | 1381 | AutoreleaseRVCI->setTailCall(false); // Never tail call objc_autorelease. |
Michael Gottesman | 556ff61 | 2013-01-12 01:25:19 +0000 | [diff] [blame] | 1382 | Class = IC_Autorelease; |
Michael Gottesman | 10426b5 | 2013-01-07 21:26:07 +0000 | [diff] [blame] | 1383 | |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1384 | DEBUG(dbgs() << "New: " << *AutoreleaseRV << "\n"); |
Michael Gottesman | 10426b5 | 2013-01-07 21:26:07 +0000 | [diff] [blame] | 1385 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1386 | } |
| 1387 | |
Michael Gottesman | 158fdf6 | 2013-03-28 20:11:19 +0000 | [diff] [blame] | 1388 | // \brief Attempt to strength reduce objc_retainBlock calls to objc_retain |
| 1389 | // calls. |
| 1390 | // |
| 1391 | // Specifically: If an objc_retainBlock call has the copy_on_escape metadata and |
| 1392 | // does not escape (following the rules of block escaping), strength reduce the |
| 1393 | // objc_retainBlock to an objc_retain. |
| 1394 | // |
| 1395 | // TODO: If an objc_retainBlock call is dominated period by a previous |
| 1396 | // objc_retainBlock call, strength reduce the objc_retainBlock to an |
| 1397 | // objc_retain. |
| 1398 | bool |
| 1399 | ObjCARCOpt::OptimizeRetainBlockCall(Function &F, Instruction *Inst, |
| 1400 | InstructionClass &Class) { |
| 1401 | assert(GetBasicInstructionClass(Inst) == Class); |
| 1402 | assert(IC_RetainBlock == Class); |
Michael Gottesman | 60f6b28 | 2013-03-29 05:13:07 +0000 | [diff] [blame] | 1403 | |
Michael Gottesman | 158fdf6 | 2013-03-28 20:11:19 +0000 | [diff] [blame] | 1404 | // If we can not optimize Inst, return false. |
| 1405 | if (!IsRetainBlockOptimizable(Inst)) |
| 1406 | return false; |
Michael Gottesman | 60f6b28 | 2013-03-29 05:13:07 +0000 | [diff] [blame] | 1407 | |
Michael Gottesman | 3eab2e4 | 2013-04-21 00:50:27 +0000 | [diff] [blame] | 1408 | Changed = true; |
| 1409 | ++NumPeeps; |
| 1410 | |
| 1411 | DEBUG(dbgs() << "Strength reduced retainBlock => retain.\n"); |
| 1412 | DEBUG(dbgs() << "Old: " << *Inst << "\n"); |
Michael Gottesman | 158fdf6 | 2013-03-28 20:11:19 +0000 | [diff] [blame] | 1413 | CallInst *RetainBlock = cast<CallInst>(Inst); |
| 1414 | RetainBlock->setCalledFunction(getRetainCallee(F.getParent())); |
| 1415 | // Remove copy_on_escape metadata. |
| 1416 | RetainBlock->setMetadata(CopyOnEscapeMDKind, 0); |
| 1417 | Class = IC_Retain; |
Michael Gottesman | 3eab2e4 | 2013-04-21 00:50:27 +0000 | [diff] [blame] | 1418 | DEBUG(dbgs() << "New: " << *Inst << "\n"); |
Michael Gottesman | 158fdf6 | 2013-03-28 20:11:19 +0000 | [diff] [blame] | 1419 | return true; |
| 1420 | } |
| 1421 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 1422 | /// Visit each call, one at a time, and make simplifications without doing any |
| 1423 | /// additional analysis. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1424 | void ObjCARCOpt::OptimizeIndividualCalls(Function &F) { |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1425 | DEBUG(dbgs() << "\n== ObjCARCOpt::OptimizeIndividualCalls ==\n"); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1426 | // Reset all the flags in preparation for recomputing them. |
| 1427 | UsedInThisFunction = 0; |
| 1428 | |
| 1429 | // Visit all objc_* calls in F. |
| 1430 | for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) { |
| 1431 | Instruction *Inst = &*I++; |
Michael Gottesman | 3f146e2 | 2013-01-01 16:05:48 +0000 | [diff] [blame] | 1432 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1433 | InstructionClass Class = GetBasicInstructionClass(Inst); |
| 1434 | |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1435 | DEBUG(dbgs() << "Visiting: Class: " << Class << "; " << *Inst << "\n"); |
Michael Gottesman | 782e344 | 2013-01-17 18:32:34 +0000 | [diff] [blame] | 1436 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1437 | switch (Class) { |
| 1438 | default: break; |
| 1439 | |
| 1440 | // Delete no-op casts. These function calls have special semantics, but |
| 1441 | // the semantics are entirely implemented via lowering in the front-end, |
| 1442 | // so by the time they reach the optimizer, they are just no-op calls |
| 1443 | // which return their argument. |
| 1444 | // |
| 1445 | // There are gray areas here, as the ability to cast reference-counted |
| 1446 | // pointers to raw void* and back allows code to break ARC assumptions, |
| 1447 | // however these are currently considered to be unimportant. |
| 1448 | case IC_NoopCast: |
| 1449 | Changed = true; |
| 1450 | ++NumNoops; |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1451 | DEBUG(dbgs() << "Erasing no-op cast: " << *Inst << "\n"); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1452 | EraseInstruction(Inst); |
| 1453 | continue; |
| 1454 | |
| 1455 | // If the pointer-to-weak-pointer is null, it's undefined behavior. |
| 1456 | case IC_StoreWeak: |
| 1457 | case IC_LoadWeak: |
| 1458 | case IC_LoadWeakRetained: |
| 1459 | case IC_InitWeak: |
| 1460 | case IC_DestroyWeak: { |
| 1461 | CallInst *CI = cast<CallInst>(Inst); |
Michael Gottesman | 65c2481 | 2013-03-25 09:27:43 +0000 | [diff] [blame] | 1462 | if (IsNullOrUndef(CI->getArgOperand(0))) { |
Dan Gohman | 670f937 | 2012-04-13 18:57:48 +0000 | [diff] [blame] | 1463 | Changed = true; |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1464 | Type *Ty = CI->getArgOperand(0)->getType(); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1465 | new StoreInst(UndefValue::get(cast<PointerType>(Ty)->getElementType()), |
| 1466 | Constant::getNullValue(Ty), |
| 1467 | CI); |
Michael Gottesman | 10426b5 | 2013-01-07 21:26:07 +0000 | [diff] [blame] | 1468 | llvm::Value *NewValue = UndefValue::get(CI->getType()); |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1469 | DEBUG(dbgs() << "A null pointer-to-weak-pointer is undefined behavior." |
| 1470 | "\nOld = " << *CI << "\nNew = " << *NewValue << "\n"); |
Michael Gottesman | fec61c0 | 2013-01-06 21:54:30 +0000 | [diff] [blame] | 1471 | CI->replaceAllUsesWith(NewValue); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1472 | CI->eraseFromParent(); |
| 1473 | continue; |
| 1474 | } |
| 1475 | break; |
| 1476 | } |
| 1477 | case IC_CopyWeak: |
| 1478 | case IC_MoveWeak: { |
| 1479 | CallInst *CI = cast<CallInst>(Inst); |
Michael Gottesman | 65c2481 | 2013-03-25 09:27:43 +0000 | [diff] [blame] | 1480 | if (IsNullOrUndef(CI->getArgOperand(0)) || |
| 1481 | IsNullOrUndef(CI->getArgOperand(1))) { |
Dan Gohman | 670f937 | 2012-04-13 18:57:48 +0000 | [diff] [blame] | 1482 | Changed = true; |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1483 | Type *Ty = CI->getArgOperand(0)->getType(); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1484 | new StoreInst(UndefValue::get(cast<PointerType>(Ty)->getElementType()), |
| 1485 | Constant::getNullValue(Ty), |
| 1486 | CI); |
Michael Gottesman | fec61c0 | 2013-01-06 21:54:30 +0000 | [diff] [blame] | 1487 | |
| 1488 | llvm::Value *NewValue = UndefValue::get(CI->getType()); |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1489 | DEBUG(dbgs() << "A null pointer-to-weak-pointer is undefined behavior." |
| 1490 | "\nOld = " << *CI << "\nNew = " << *NewValue << "\n"); |
Michael Gottesman | 10426b5 | 2013-01-07 21:26:07 +0000 | [diff] [blame] | 1491 | |
Michael Gottesman | fec61c0 | 2013-01-06 21:54:30 +0000 | [diff] [blame] | 1492 | CI->replaceAllUsesWith(NewValue); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1493 | CI->eraseFromParent(); |
| 1494 | continue; |
| 1495 | } |
| 1496 | break; |
| 1497 | } |
Michael Gottesman | 158fdf6 | 2013-03-28 20:11:19 +0000 | [diff] [blame] | 1498 | case IC_RetainBlock: |
Michael Gottesman | 1e43004 | 2013-04-21 00:44:46 +0000 | [diff] [blame] | 1499 | // If we strength reduce an objc_retainBlock to an objc_retain, continue |
Michael Gottesman | 158fdf6 | 2013-03-28 20:11:19 +0000 | [diff] [blame] | 1500 | // onto the objc_retain peephole optimizations. Otherwise break. |
Michael Gottesman | 9fc50b8 | 2013-05-13 18:29:07 +0000 | [diff] [blame] | 1501 | OptimizeRetainBlockCall(F, Inst, Class); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1502 | break; |
| 1503 | case IC_RetainRV: |
| 1504 | if (OptimizeRetainRVCall(F, Inst)) |
| 1505 | continue; |
| 1506 | break; |
| 1507 | case IC_AutoreleaseRV: |
Michael Gottesman | 556ff61 | 2013-01-12 01:25:19 +0000 | [diff] [blame] | 1508 | OptimizeAutoreleaseRVCall(F, Inst, Class); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1509 | break; |
| 1510 | } |
| 1511 | |
Michael Gottesman | b8c8836 | 2013-04-03 02:57:24 +0000 | [diff] [blame] | 1512 | // objc_autorelease(x) -> objc_release(x) if x is otherwise unused. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1513 | if (IsAutorelease(Class) && Inst->use_empty()) { |
| 1514 | CallInst *Call = cast<CallInst>(Inst); |
| 1515 | const Value *Arg = Call->getArgOperand(0); |
| 1516 | Arg = FindSingleUseIdentifiedObject(Arg); |
| 1517 | if (Arg) { |
| 1518 | Changed = true; |
| 1519 | ++NumAutoreleases; |
| 1520 | |
| 1521 | // Create the declaration lazily. |
| 1522 | LLVMContext &C = Inst->getContext(); |
| 1523 | CallInst *NewCall = |
| 1524 | CallInst::Create(getReleaseCallee(F.getParent()), |
| 1525 | Call->getArgOperand(0), "", Call); |
Dmitri Gribenko | 3238fb7 | 2013-05-05 00:40:33 +0000 | [diff] [blame] | 1526 | NewCall->setMetadata(ImpreciseReleaseMDKind, MDNode::get(C, None)); |
Michael Gottesman | 10426b5 | 2013-01-07 21:26:07 +0000 | [diff] [blame] | 1527 | |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1528 | DEBUG(dbgs() << "Replacing autorelease{,RV}(x) with objc_release(x) " |
| 1529 | "since x is otherwise unused.\nOld: " << *Call << "\nNew: " |
| 1530 | << *NewCall << "\n"); |
Michael Gottesman | 10426b5 | 2013-01-07 21:26:07 +0000 | [diff] [blame] | 1531 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1532 | EraseInstruction(Call); |
| 1533 | Inst = NewCall; |
| 1534 | Class = IC_Release; |
| 1535 | } |
| 1536 | } |
| 1537 | |
| 1538 | // For functions which can never be passed stack arguments, add |
| 1539 | // a tail keyword. |
| 1540 | if (IsAlwaysTail(Class)) { |
| 1541 | Changed = true; |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1542 | DEBUG(dbgs() << "Adding tail keyword to function since it can never be " |
| 1543 | "passed stack args: " << *Inst << "\n"); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1544 | cast<CallInst>(Inst)->setTailCall(); |
| 1545 | } |
| 1546 | |
Michael Gottesman | c9656fa | 2013-01-12 01:25:15 +0000 | [diff] [blame] | 1547 | // Ensure that functions that can never have a "tail" keyword due to the |
| 1548 | // semantics of ARC truly do not do so. |
| 1549 | if (IsNeverTail(Class)) { |
| 1550 | Changed = true; |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1551 | DEBUG(dbgs() << "Removing tail keyword from function: " << *Inst << |
Michael Gottesman | c9656fa | 2013-01-12 01:25:15 +0000 | [diff] [blame] | 1552 | "\n"); |
| 1553 | cast<CallInst>(Inst)->setTailCall(false); |
| 1554 | } |
| 1555 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1556 | // Set nounwind as needed. |
| 1557 | if (IsNoThrow(Class)) { |
| 1558 | Changed = true; |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1559 | DEBUG(dbgs() << "Found no throw class. Setting nounwind on: " << *Inst |
| 1560 | << "\n"); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1561 | cast<CallInst>(Inst)->setDoesNotThrow(); |
| 1562 | } |
| 1563 | |
| 1564 | if (!IsNoopOnNull(Class)) { |
| 1565 | UsedInThisFunction |= 1 << Class; |
| 1566 | continue; |
| 1567 | } |
| 1568 | |
| 1569 | const Value *Arg = GetObjCArg(Inst); |
| 1570 | |
| 1571 | // ARC calls with null are no-ops. Delete them. |
Michael Gottesman | 65c2481 | 2013-03-25 09:27:43 +0000 | [diff] [blame] | 1572 | if (IsNullOrUndef(Arg)) { |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1573 | Changed = true; |
| 1574 | ++NumNoops; |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1575 | DEBUG(dbgs() << "ARC calls with null are no-ops. Erasing: " << *Inst |
| 1576 | << "\n"); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1577 | EraseInstruction(Inst); |
| 1578 | continue; |
| 1579 | } |
| 1580 | |
| 1581 | // Keep track of which of retain, release, autorelease, and retain_block |
| 1582 | // are actually present in this function. |
| 1583 | UsedInThisFunction |= 1 << Class; |
| 1584 | |
| 1585 | // If Arg is a PHI, and one or more incoming values to the |
| 1586 | // PHI are null, and the call is control-equivalent to the PHI, and there |
| 1587 | // are no relevant side effects between the PHI and the call, the call |
| 1588 | // could be pushed up to just those paths with non-null incoming values. |
| 1589 | // For now, don't bother splitting critical edges for this. |
| 1590 | SmallVector<std::pair<Instruction *, const Value *>, 4> Worklist; |
| 1591 | Worklist.push_back(std::make_pair(Inst, Arg)); |
| 1592 | do { |
| 1593 | std::pair<Instruction *, const Value *> Pair = Worklist.pop_back_val(); |
| 1594 | Inst = Pair.first; |
| 1595 | Arg = Pair.second; |
| 1596 | |
| 1597 | const PHINode *PN = dyn_cast<PHINode>(Arg); |
| 1598 | if (!PN) continue; |
| 1599 | |
| 1600 | // Determine if the PHI has any null operands, or any incoming |
| 1601 | // critical edges. |
| 1602 | bool HasNull = false; |
| 1603 | bool HasCriticalEdges = false; |
| 1604 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 1605 | Value *Incoming = |
| 1606 | StripPointerCastsAndObjCCalls(PN->getIncomingValue(i)); |
Michael Gottesman | 65c2481 | 2013-03-25 09:27:43 +0000 | [diff] [blame] | 1607 | if (IsNullOrUndef(Incoming)) |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1608 | HasNull = true; |
| 1609 | else if (cast<TerminatorInst>(PN->getIncomingBlock(i)->back()) |
| 1610 | .getNumSuccessors() != 1) { |
| 1611 | HasCriticalEdges = true; |
| 1612 | break; |
| 1613 | } |
| 1614 | } |
| 1615 | // If we have null operands and no critical edges, optimize. |
| 1616 | if (!HasCriticalEdges && HasNull) { |
| 1617 | SmallPtrSet<Instruction *, 4> DependingInstructions; |
| 1618 | SmallPtrSet<const BasicBlock *, 4> Visited; |
| 1619 | |
| 1620 | // Check that there is nothing that cares about the reference |
| 1621 | // count between the call and the phi. |
Dan Gohman | 8478d76 | 2012-04-13 00:59:57 +0000 | [diff] [blame] | 1622 | switch (Class) { |
| 1623 | case IC_Retain: |
| 1624 | case IC_RetainBlock: |
| 1625 | // These can always be moved up. |
| 1626 | break; |
| 1627 | case IC_Release: |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 1628 | // These can't be moved across things that care about the retain |
| 1629 | // count. |
Dan Gohman | 8478d76 | 2012-04-13 00:59:57 +0000 | [diff] [blame] | 1630 | FindDependencies(NeedsPositiveRetainCount, Arg, |
| 1631 | Inst->getParent(), Inst, |
| 1632 | DependingInstructions, Visited, PA); |
| 1633 | break; |
| 1634 | case IC_Autorelease: |
| 1635 | // These can't be moved across autorelease pool scope boundaries. |
| 1636 | FindDependencies(AutoreleasePoolBoundary, Arg, |
| 1637 | Inst->getParent(), Inst, |
| 1638 | DependingInstructions, Visited, PA); |
| 1639 | break; |
| 1640 | case IC_RetainRV: |
| 1641 | case IC_AutoreleaseRV: |
| 1642 | // Don't move these; the RV optimization depends on the autoreleaseRV |
| 1643 | // being tail called, and the retainRV being immediately after a call |
| 1644 | // (which might still happen if we get lucky with codegen layout, but |
| 1645 | // it's not worth taking the chance). |
| 1646 | continue; |
| 1647 | default: |
| 1648 | llvm_unreachable("Invalid dependence flavor"); |
| 1649 | } |
| 1650 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1651 | if (DependingInstructions.size() == 1 && |
| 1652 | *DependingInstructions.begin() == PN) { |
| 1653 | Changed = true; |
| 1654 | ++NumPartialNoops; |
| 1655 | // Clone the call into each predecessor that has a non-null value. |
| 1656 | CallInst *CInst = cast<CallInst>(Inst); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1657 | Type *ParamTy = CInst->getArgOperand(0)->getType(); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1658 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 1659 | Value *Incoming = |
| 1660 | StripPointerCastsAndObjCCalls(PN->getIncomingValue(i)); |
Michael Gottesman | 65c2481 | 2013-03-25 09:27:43 +0000 | [diff] [blame] | 1661 | if (!IsNullOrUndef(Incoming)) { |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1662 | CallInst *Clone = cast<CallInst>(CInst->clone()); |
| 1663 | Value *Op = PN->getIncomingValue(i); |
| 1664 | Instruction *InsertPos = &PN->getIncomingBlock(i)->back(); |
| 1665 | if (Op->getType() != ParamTy) |
| 1666 | Op = new BitCastInst(Op, ParamTy, "", InsertPos); |
| 1667 | Clone->setArgOperand(0, Op); |
| 1668 | Clone->insertBefore(InsertPos); |
Michael Gottesman | c189a39 | 2013-01-09 19:23:24 +0000 | [diff] [blame] | 1669 | |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1670 | DEBUG(dbgs() << "Cloning " |
Michael Gottesman | c189a39 | 2013-01-09 19:23:24 +0000 | [diff] [blame] | 1671 | << *CInst << "\n" |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1672 | "And inserting clone at " << *InsertPos << "\n"); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1673 | Worklist.push_back(std::make_pair(Clone, Incoming)); |
| 1674 | } |
| 1675 | } |
| 1676 | // Erase the original call. |
Michael Gottesman | c189a39 | 2013-01-09 19:23:24 +0000 | [diff] [blame] | 1677 | DEBUG(dbgs() << "Erasing: " << *CInst << "\n"); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1678 | EraseInstruction(CInst); |
| 1679 | continue; |
| 1680 | } |
| 1681 | } |
| 1682 | } while (!Worklist.empty()); |
| 1683 | } |
| 1684 | } |
| 1685 | |
Michael Gottesman | 323964c | 2013-04-18 05:39:45 +0000 | [diff] [blame] | 1686 | /// If we have a top down pointer in the S_Use state, make sure that there are |
| 1687 | /// no CFG hazards by checking the states of various bottom up pointers. |
| 1688 | static void CheckForUseCFGHazard(const Sequence SuccSSeq, |
| 1689 | const bool SuccSRRIKnownSafe, |
| 1690 | PtrState &S, |
| 1691 | bool &SomeSuccHasSame, |
| 1692 | bool &AllSuccsHaveSame, |
| 1693 | bool &ShouldContinue) { |
| 1694 | switch (SuccSSeq) { |
| 1695 | case S_CanRelease: { |
| 1696 | if (!S.RRI.KnownSafe && !SuccSRRIKnownSafe) { |
| 1697 | S.ClearSequenceProgress(); |
| 1698 | break; |
| 1699 | } |
| 1700 | ShouldContinue = true; |
| 1701 | break; |
| 1702 | } |
| 1703 | case S_Use: |
| 1704 | SomeSuccHasSame = true; |
| 1705 | break; |
| 1706 | case S_Stop: |
| 1707 | case S_Release: |
| 1708 | case S_MovableRelease: |
| 1709 | if (!S.RRI.KnownSafe && !SuccSRRIKnownSafe) |
| 1710 | AllSuccsHaveSame = false; |
| 1711 | break; |
| 1712 | case S_Retain: |
| 1713 | llvm_unreachable("bottom-up pointer in retain state!"); |
| 1714 | case S_None: |
| 1715 | llvm_unreachable("This should have been handled earlier."); |
| 1716 | } |
| 1717 | } |
| 1718 | |
| 1719 | /// If we have a Top Down pointer in the S_CanRelease state, make sure that |
| 1720 | /// there are no CFG hazards by checking the states of various bottom up |
| 1721 | /// pointers. |
| 1722 | static void CheckForCanReleaseCFGHazard(const Sequence SuccSSeq, |
| 1723 | const bool SuccSRRIKnownSafe, |
| 1724 | PtrState &S, |
| 1725 | bool &SomeSuccHasSame, |
| 1726 | bool &AllSuccsHaveSame) { |
| 1727 | switch (SuccSSeq) { |
| 1728 | case S_CanRelease: |
| 1729 | SomeSuccHasSame = true; |
| 1730 | break; |
| 1731 | case S_Stop: |
| 1732 | case S_Release: |
| 1733 | case S_MovableRelease: |
| 1734 | case S_Use: |
| 1735 | if (!S.RRI.KnownSafe && !SuccSRRIKnownSafe) |
| 1736 | AllSuccsHaveSame = false; |
| 1737 | break; |
| 1738 | case S_Retain: |
| 1739 | llvm_unreachable("bottom-up pointer in retain state!"); |
| 1740 | case S_None: |
| 1741 | llvm_unreachable("This should have been handled earlier."); |
| 1742 | } |
| 1743 | } |
| 1744 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 1745 | /// Check for critical edges, loop boundaries, irreducible control flow, or |
| 1746 | /// other CFG structures where moving code across the edge would result in it |
| 1747 | /// being executed more. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1748 | void |
| 1749 | ObjCARCOpt::CheckForCFGHazards(const BasicBlock *BB, |
| 1750 | DenseMap<const BasicBlock *, BBState> &BBStates, |
| 1751 | BBState &MyStates) const { |
| 1752 | // If any top-down local-use or possible-dec has a succ which is earlier in |
| 1753 | // the sequence, forget it. |
Dan Gohman | 55b0674 | 2012-03-02 01:13:53 +0000 | [diff] [blame] | 1754 | for (BBState::ptr_iterator I = MyStates.top_down_ptr_begin(), |
Michael Gottesman | 323964c | 2013-04-18 05:39:45 +0000 | [diff] [blame] | 1755 | E = MyStates.top_down_ptr_end(); I != E; ++I) { |
| 1756 | PtrState &S = I->second; |
| 1757 | const Sequence Seq = I->second.GetSeq(); |
Dan Gohman | 0155f30 | 2012-02-17 18:59:53 +0000 | [diff] [blame] | 1758 | |
Michael Gottesman | 323964c | 2013-04-18 05:39:45 +0000 | [diff] [blame] | 1759 | // We only care about S_Retain, S_CanRelease, and S_Use. |
| 1760 | if (Seq == S_None) |
| 1761 | continue; |
Dan Gohman | 0155f30 | 2012-02-17 18:59:53 +0000 | [diff] [blame] | 1762 | |
Michael Gottesman | 323964c | 2013-04-18 05:39:45 +0000 | [diff] [blame] | 1763 | // Make sure that if extra top down states are added in the future that this |
| 1764 | // code is updated to handle it. |
| 1765 | assert((Seq == S_Retain || Seq == S_CanRelease || Seq == S_Use) && |
| 1766 | "Unknown top down sequence state."); |
| 1767 | |
| 1768 | const Value *Arg = I->first; |
| 1769 | const TerminatorInst *TI = cast<TerminatorInst>(&BB->back()); |
| 1770 | bool SomeSuccHasSame = false; |
| 1771 | bool AllSuccsHaveSame = true; |
| 1772 | |
| 1773 | succ_const_iterator SI(TI), SE(TI, false); |
| 1774 | |
| 1775 | for (; SI != SE; ++SI) { |
| 1776 | // If VisitBottomUp has pointer information for this successor, take |
| 1777 | // what we know about it. |
| 1778 | const DenseMap<const BasicBlock *, BBState>::iterator BBI = |
| 1779 | BBStates.find(*SI); |
| 1780 | assert(BBI != BBStates.end()); |
| 1781 | const PtrState &SuccS = BBI->second.getPtrBottomUpState(Arg); |
| 1782 | const Sequence SuccSSeq = SuccS.GetSeq(); |
| 1783 | |
| 1784 | // If bottom up, the pointer is in an S_None state, clear the sequence |
| 1785 | // progress since the sequence in the bottom up state finished |
| 1786 | // suggesting a mismatch in between retains/releases. This is true for |
| 1787 | // all three cases that we are handling here: S_Retain, S_Use, and |
| 1788 | // S_CanRelease. |
| 1789 | if (SuccSSeq == S_None) { |
Dan Gohman | 1213027 | 2011-08-12 00:26:31 +0000 | [diff] [blame] | 1790 | S.ClearSequenceProgress(); |
Michael Gottesman | 323964c | 2013-04-18 05:39:45 +0000 | [diff] [blame] | 1791 | continue; |
| 1792 | } |
| 1793 | |
| 1794 | // If we have S_Use or S_CanRelease, perform our check for cfg hazard |
| 1795 | // checks. |
| 1796 | const bool SuccSRRIKnownSafe = SuccS.RRI.KnownSafe; |
| 1797 | |
| 1798 | // *NOTE* We do not use Seq from above here since we are allowing for |
| 1799 | // S.GetSeq() to change while we are visiting basic blocks. |
| 1800 | switch(S.GetSeq()) { |
| 1801 | case S_Use: { |
| 1802 | bool ShouldContinue = false; |
| 1803 | CheckForUseCFGHazard(SuccSSeq, SuccSRRIKnownSafe, S, |
| 1804 | SomeSuccHasSame, AllSuccsHaveSame, |
| 1805 | ShouldContinue); |
| 1806 | if (ShouldContinue) |
| 1807 | continue; |
| 1808 | break; |
| 1809 | } |
| 1810 | case S_CanRelease: { |
| 1811 | CheckForCanReleaseCFGHazard(SuccSSeq, SuccSRRIKnownSafe, |
| 1812 | S, SomeSuccHasSame, |
| 1813 | AllSuccsHaveSame); |
| 1814 | break; |
| 1815 | } |
| 1816 | case S_Retain: |
| 1817 | case S_None: |
| 1818 | case S_Stop: |
| 1819 | case S_Release: |
| 1820 | case S_MovableRelease: |
| 1821 | break; |
| 1822 | } |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1823 | } |
Michael Gottesman | 323964c | 2013-04-18 05:39:45 +0000 | [diff] [blame] | 1824 | |
| 1825 | // If the state at the other end of any of the successor edges |
| 1826 | // matches the current state, require all edges to match. This |
| 1827 | // guards against loops in the middle of a sequence. |
| 1828 | if (SomeSuccHasSame && !AllSuccsHaveSame) |
| 1829 | S.ClearSequenceProgress(); |
| 1830 | } |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1831 | } |
| 1832 | |
| 1833 | bool |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1834 | ObjCARCOpt::VisitInstructionBottomUp(Instruction *Inst, |
Dan Gohman | 5c70fad | 2012-03-23 17:47:54 +0000 | [diff] [blame] | 1835 | BasicBlock *BB, |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1836 | MapVector<Value *, RRInfo> &Retains, |
| 1837 | BBState &MyStates) { |
| 1838 | bool NestingDetected = false; |
| 1839 | InstructionClass Class = GetInstructionClass(Inst); |
| 1840 | const Value *Arg = 0; |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 1841 | |
Michael Gottesman | 1d8d257 | 2013-04-05 22:54:28 +0000 | [diff] [blame] | 1842 | DEBUG(dbgs() << "Class: " << Class << "\n"); |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 1843 | |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1844 | switch (Class) { |
| 1845 | case IC_Release: { |
| 1846 | Arg = GetObjCArg(Inst); |
| 1847 | |
| 1848 | PtrState &S = MyStates.getPtrBottomUpState(Arg); |
| 1849 | |
| 1850 | // If we see two releases in a row on the same pointer. If so, make |
| 1851 | // a note, and we'll cicle back to revisit it after we've |
| 1852 | // hopefully eliminated the second release, which may allow us to |
| 1853 | // eliminate the first release too. |
| 1854 | // Theoretically we could implement removal of nested retain+release |
| 1855 | // pairs by making PtrState hold a stack of states, but this is |
| 1856 | // simple and avoids adding overhead for the non-nested case. |
Michael Gottesman | af2113f | 2013-01-13 07:00:51 +0000 | [diff] [blame] | 1857 | if (S.GetSeq() == S_Release || S.GetSeq() == S_MovableRelease) { |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1858 | DEBUG(dbgs() << "Found nested releases (i.e. a release pair)\n"); |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1859 | NestingDetected = true; |
Michael Gottesman | af2113f | 2013-01-13 07:00:51 +0000 | [diff] [blame] | 1860 | } |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1861 | |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1862 | MDNode *ReleaseMetadata = Inst->getMetadata(ImpreciseReleaseMDKind); |
Michael Gottesman | 81b1d43 | 2013-03-26 00:42:04 +0000 | [diff] [blame] | 1863 | Sequence NewSeq = ReleaseMetadata ? S_MovableRelease : S_Release; |
| 1864 | ANNOTATE_BOTTOMUP(Inst, Arg, S.GetSeq(), NewSeq); |
| 1865 | S.ResetSequenceProgress(NewSeq); |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1866 | S.RRI.ReleaseMetadata = ReleaseMetadata; |
Michael Gottesman | 07beea4 | 2013-03-23 05:31:01 +0000 | [diff] [blame] | 1867 | S.RRI.KnownSafe = S.HasKnownPositiveRefCount(); |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1868 | S.RRI.IsTailCallRelease = cast<CallInst>(Inst)->isTailCall(); |
| 1869 | S.RRI.Calls.insert(Inst); |
Dan Gohman | df476e5 | 2012-09-04 23:16:20 +0000 | [diff] [blame] | 1870 | S.SetKnownPositiveRefCount(); |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1871 | break; |
| 1872 | } |
| 1873 | case IC_RetainBlock: |
Michael Gottesman | 158fdf6 | 2013-03-28 20:11:19 +0000 | [diff] [blame] | 1874 | // In OptimizeIndividualCalls, we have strength reduced all optimizable |
| 1875 | // objc_retainBlocks to objc_retains. Thus at this point any |
| 1876 | // objc_retainBlocks that we see are not optimizable. |
| 1877 | break; |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1878 | case IC_Retain: |
| 1879 | case IC_RetainRV: { |
| 1880 | Arg = GetObjCArg(Inst); |
| 1881 | |
| 1882 | PtrState &S = MyStates.getPtrBottomUpState(Arg); |
Dan Gohman | 62079b4 | 2012-04-25 00:50:46 +0000 | [diff] [blame] | 1883 | S.SetKnownPositiveRefCount(); |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1884 | |
Michael Gottesman | 81b1d43 | 2013-03-26 00:42:04 +0000 | [diff] [blame] | 1885 | Sequence OldSeq = S.GetSeq(); |
| 1886 | switch (OldSeq) { |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1887 | case S_Stop: |
| 1888 | case S_Release: |
| 1889 | case S_MovableRelease: |
| 1890 | case S_Use: |
Michael Gottesman | 1d8d257 | 2013-04-05 22:54:28 +0000 | [diff] [blame] | 1891 | // If OldSeq is not S_Use or OldSeq is S_Use and we are tracking an |
| 1892 | // imprecise release, clear our reverse insertion points. |
| 1893 | if (OldSeq != S_Use || S.RRI.IsTrackingImpreciseReleases()) |
| 1894 | S.RRI.ReverseInsertPts.clear(); |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1895 | // FALL THROUGH |
| 1896 | case S_CanRelease: |
| 1897 | // Don't do retain+release tracking for IC_RetainRV, because it's |
| 1898 | // better to let it remain as the first instruction after a call. |
Michael Gottesman | ba64859 | 2013-03-28 23:08:44 +0000 | [diff] [blame] | 1899 | if (Class != IC_RetainRV) |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1900 | Retains[Inst] = S.RRI; |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1901 | S.ClearSequenceProgress(); |
| 1902 | break; |
| 1903 | case S_None: |
| 1904 | break; |
| 1905 | case S_Retain: |
| 1906 | llvm_unreachable("bottom-up pointer in retain state!"); |
| 1907 | } |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 1908 | ANNOTATE_BOTTOMUP(Inst, Arg, OldSeq, S.GetSeq()); |
Michael Gottesman | 31ba23a | 2013-04-05 22:54:32 +0000 | [diff] [blame] | 1909 | // A retain moving bottom up can be a use. |
| 1910 | break; |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1911 | } |
| 1912 | case IC_AutoreleasepoolPop: |
| 1913 | // Conservatively, clear MyStates for all known pointers. |
| 1914 | MyStates.clearBottomUpPointers(); |
| 1915 | return NestingDetected; |
| 1916 | case IC_AutoreleasepoolPush: |
| 1917 | case IC_None: |
| 1918 | // These are irrelevant. |
| 1919 | return NestingDetected; |
Michael Gottesman | a76143ee | 2013-05-13 23:49:42 +0000 | [diff] [blame] | 1920 | case IC_User: |
| 1921 | // If we have a store into an alloca of a pointer we are tracking, the |
| 1922 | // pointer has multiple owners implying that we must be more conservative. |
| 1923 | // |
| 1924 | // This comes up in the context of a pointer being ``KnownSafe''. In the |
| 1925 | // presense of a block being initialized, the frontend will emit the |
| 1926 | // objc_retain on the original pointer and the release on the pointer loaded |
| 1927 | // from the alloca. The optimizer will through the provenance analysis |
| 1928 | // realize that the two are related, but since we only require KnownSafe in |
| 1929 | // one direction, will match the inner retain on the original pointer with |
| 1930 | // the guard release on the original pointer. This is fixed by ensuring that |
| 1931 | // in the presense of allocas we only unconditionally remove pointers if |
| 1932 | // both our retain and our release are KnownSafe. |
| 1933 | if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) { |
| 1934 | if (AreAnyUnderlyingObjectsAnAlloca(SI->getPointerOperand())) { |
| 1935 | BBState::ptr_iterator I = MyStates.findPtrBottomUpState( |
| 1936 | StripPointerCastsAndObjCCalls(SI->getValueOperand())); |
| 1937 | if (I != MyStates.bottom_up_ptr_end()) |
Michael Gottesman | 5a91bbf | 2013-05-24 20:44:02 +0000 | [diff] [blame^] | 1938 | MultiOwnersSet.insert(I->first); |
Michael Gottesman | a76143ee | 2013-05-13 23:49:42 +0000 | [diff] [blame] | 1939 | } |
| 1940 | } |
| 1941 | break; |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1942 | default: |
| 1943 | break; |
| 1944 | } |
| 1945 | |
| 1946 | // Consider any other possible effects of this instruction on each |
| 1947 | // pointer being tracked. |
| 1948 | for (BBState::ptr_iterator MI = MyStates.bottom_up_ptr_begin(), |
| 1949 | ME = MyStates.bottom_up_ptr_end(); MI != ME; ++MI) { |
| 1950 | const Value *Ptr = MI->first; |
| 1951 | if (Ptr == Arg) |
| 1952 | continue; // Handled above. |
| 1953 | PtrState &S = MI->second; |
| 1954 | Sequence Seq = S.GetSeq(); |
| 1955 | |
| 1956 | // Check for possible releases. |
| 1957 | if (CanAlterRefCount(Inst, Ptr, PA, Class)) { |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1958 | DEBUG(dbgs() << "CanAlterRefCount: Seq: " << Seq << "; " << *Ptr |
| 1959 | << "\n"); |
Michael Gottesman | 764b1cf | 2013-03-23 05:46:19 +0000 | [diff] [blame] | 1960 | S.ClearKnownPositiveRefCount(); |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1961 | switch (Seq) { |
| 1962 | case S_Use: |
| 1963 | S.SetSeq(S_CanRelease); |
Michael Gottesman | 81b1d43 | 2013-03-26 00:42:04 +0000 | [diff] [blame] | 1964 | ANNOTATE_BOTTOMUP(Inst, Ptr, Seq, S.GetSeq()); |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1965 | continue; |
| 1966 | case S_CanRelease: |
| 1967 | case S_Release: |
| 1968 | case S_MovableRelease: |
| 1969 | case S_Stop: |
| 1970 | case S_None: |
| 1971 | break; |
| 1972 | case S_Retain: |
| 1973 | llvm_unreachable("bottom-up pointer in retain state!"); |
| 1974 | } |
| 1975 | } |
| 1976 | |
| 1977 | // Check for possible direct uses. |
| 1978 | switch (Seq) { |
| 1979 | case S_Release: |
| 1980 | case S_MovableRelease: |
| 1981 | if (CanUse(Inst, Ptr, PA, Class)) { |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1982 | DEBUG(dbgs() << "CanUse: Seq: " << Seq << "; " << *Ptr |
| 1983 | << "\n"); |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1984 | assert(S.RRI.ReverseInsertPts.empty()); |
Dan Gohman | 5c70fad | 2012-03-23 17:47:54 +0000 | [diff] [blame] | 1985 | // If this is an invoke instruction, we're scanning it as part of |
| 1986 | // one of its successor blocks, since we can't insert code after it |
| 1987 | // in its own block, and we don't want to split critical edges. |
| 1988 | if (isa<InvokeInst>(Inst)) |
| 1989 | S.RRI.ReverseInsertPts.insert(BB->getFirstInsertionPt()); |
| 1990 | else |
Francois Pichet | 4b9ab74 | 2012-03-24 01:36:37 +0000 | [diff] [blame] | 1991 | S.RRI.ReverseInsertPts.insert(llvm::next(BasicBlock::iterator(Inst))); |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1992 | S.SetSeq(S_Use); |
Michael Gottesman | 81b1d43 | 2013-03-26 00:42:04 +0000 | [diff] [blame] | 1993 | ANNOTATE_BOTTOMUP(Inst, Ptr, Seq, S_Use); |
John McCall | 20182ac | 2013-03-22 21:38:36 +0000 | [diff] [blame] | 1994 | } else if (Seq == S_Release && IsUser(Class)) { |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1995 | DEBUG(dbgs() << "PreciseReleaseUse: Seq: " << Seq << "; " << *Ptr |
| 1996 | << "\n"); |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1997 | // Non-movable releases depend on any possible objc pointer use. |
| 1998 | S.SetSeq(S_Stop); |
Michael Gottesman | 81b1d43 | 2013-03-26 00:42:04 +0000 | [diff] [blame] | 1999 | ANNOTATE_BOTTOMUP(Inst, Ptr, S_Release, S_Stop); |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 2000 | assert(S.RRI.ReverseInsertPts.empty()); |
Dan Gohman | 5c70fad | 2012-03-23 17:47:54 +0000 | [diff] [blame] | 2001 | // As above; handle invoke specially. |
| 2002 | if (isa<InvokeInst>(Inst)) |
| 2003 | S.RRI.ReverseInsertPts.insert(BB->getFirstInsertionPt()); |
| 2004 | else |
Francois Pichet | 4b9ab74 | 2012-03-24 01:36:37 +0000 | [diff] [blame] | 2005 | S.RRI.ReverseInsertPts.insert(llvm::next(BasicBlock::iterator(Inst))); |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 2006 | } |
| 2007 | break; |
| 2008 | case S_Stop: |
Michael Gottesman | 81b1d43 | 2013-03-26 00:42:04 +0000 | [diff] [blame] | 2009 | if (CanUse(Inst, Ptr, PA, Class)) { |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 2010 | DEBUG(dbgs() << "PreciseStopUse: Seq: " << Seq << "; " << *Ptr |
| 2011 | << "\n"); |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 2012 | S.SetSeq(S_Use); |
Michael Gottesman | 81b1d43 | 2013-03-26 00:42:04 +0000 | [diff] [blame] | 2013 | ANNOTATE_BOTTOMUP(Inst, Ptr, Seq, S_Use); |
| 2014 | } |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 2015 | break; |
| 2016 | case S_CanRelease: |
| 2017 | case S_Use: |
| 2018 | case S_None: |
| 2019 | break; |
| 2020 | case S_Retain: |
| 2021 | llvm_unreachable("bottom-up pointer in retain state!"); |
| 2022 | } |
| 2023 | } |
| 2024 | |
| 2025 | return NestingDetected; |
| 2026 | } |
| 2027 | |
| 2028 | bool |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2029 | ObjCARCOpt::VisitBottomUp(BasicBlock *BB, |
| 2030 | DenseMap<const BasicBlock *, BBState> &BBStates, |
| 2031 | MapVector<Value *, RRInfo> &Retains) { |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 2032 | |
| 2033 | DEBUG(dbgs() << "\n== ObjCARCOpt::VisitBottomUp ==\n"); |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 2034 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2035 | bool NestingDetected = false; |
| 2036 | BBState &MyStates = BBStates[BB]; |
| 2037 | |
| 2038 | // Merge the states from each successor to compute the initial state |
| 2039 | // for the current block. |
Dan Gohman | 10c82ce | 2012-08-27 18:31:36 +0000 | [diff] [blame] | 2040 | BBState::edge_iterator SI(MyStates.succ_begin()), |
| 2041 | SE(MyStates.succ_end()); |
| 2042 | if (SI != SE) { |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 2043 | const BasicBlock *Succ = *SI; |
| 2044 | DenseMap<const BasicBlock *, BBState>::iterator I = BBStates.find(Succ); |
| 2045 | assert(I != BBStates.end()); |
| 2046 | MyStates.InitFromSucc(I->second); |
| 2047 | ++SI; |
| 2048 | for (; SI != SE; ++SI) { |
| 2049 | Succ = *SI; |
| 2050 | I = BBStates.find(Succ); |
| 2051 | assert(I != BBStates.end()); |
| 2052 | MyStates.MergeSucc(I->second); |
| 2053 | } |
Michael Gottesman | 60f6b28 | 2013-03-29 05:13:07 +0000 | [diff] [blame] | 2054 | } |
Michael Gottesman | cd4de0f | 2013-03-26 00:42:09 +0000 | [diff] [blame] | 2055 | |
Michael Gottesman | 43e7e00 | 2013-04-03 22:41:59 +0000 | [diff] [blame] | 2056 | // If ARC Annotations are enabled, output the current state of pointers at the |
Michael Gottesman | c2d5bf5 | 2013-04-03 23:07:45 +0000 | [diff] [blame] | 2057 | // bottom of the basic block. |
Michael Gottesman | 43e7e00 | 2013-04-03 22:41:59 +0000 | [diff] [blame] | 2058 | ANNOTATE_BOTTOMUP_BBEND(MyStates, BB); |
Michael Gottesman | c2d5bf5 | 2013-04-03 23:07:45 +0000 | [diff] [blame] | 2059 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2060 | // Visit all the instructions, bottom-up. |
| 2061 | for (BasicBlock::iterator I = BB->end(), E = BB->begin(); I != E; --I) { |
| 2062 | Instruction *Inst = llvm::prior(I); |
Dan Gohman | 5c70fad | 2012-03-23 17:47:54 +0000 | [diff] [blame] | 2063 | |
| 2064 | // Invoke instructions are visited as part of their successors (below). |
| 2065 | if (isa<InvokeInst>(Inst)) |
| 2066 | continue; |
| 2067 | |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 2068 | DEBUG(dbgs() << "Visiting " << *Inst << "\n"); |
Michael Gottesman | af2113f | 2013-01-13 07:00:51 +0000 | [diff] [blame] | 2069 | |
Dan Gohman | 5c70fad | 2012-03-23 17:47:54 +0000 | [diff] [blame] | 2070 | NestingDetected |= VisitInstructionBottomUp(Inst, BB, Retains, MyStates); |
| 2071 | } |
| 2072 | |
Dan Gohman | dae3349 | 2012-04-27 18:56:31 +0000 | [diff] [blame] | 2073 | // If there's a predecessor with an invoke, visit the invoke as if it were |
| 2074 | // part of this block, since we can't insert code after an invoke in its own |
| 2075 | // block, and we don't want to split critical edges. |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 2076 | for (BBState::edge_iterator PI(MyStates.pred_begin()), |
| 2077 | PE(MyStates.pred_end()); PI != PE; ++PI) { |
Dan Gohman | 5c70fad | 2012-03-23 17:47:54 +0000 | [diff] [blame] | 2078 | BasicBlock *Pred = *PI; |
Dan Gohman | dae3349 | 2012-04-27 18:56:31 +0000 | [diff] [blame] | 2079 | if (InvokeInst *II = dyn_cast<InvokeInst>(&Pred->back())) |
| 2080 | NestingDetected |= VisitInstructionBottomUp(II, BB, Retains, MyStates); |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 2081 | } |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2082 | |
Michael Gottesman | 43e7e00 | 2013-04-03 22:41:59 +0000 | [diff] [blame] | 2083 | // If ARC Annotations are enabled, output the current state of pointers at the |
| 2084 | // top of the basic block. |
| 2085 | ANNOTATE_BOTTOMUP_BBSTART(MyStates, BB); |
Michael Gottesman | c2d5bf5 | 2013-04-03 23:07:45 +0000 | [diff] [blame] | 2086 | |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 2087 | return NestingDetected; |
| 2088 | } |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2089 | |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 2090 | bool |
| 2091 | ObjCARCOpt::VisitInstructionTopDown(Instruction *Inst, |
| 2092 | DenseMap<Value *, RRInfo> &Releases, |
| 2093 | BBState &MyStates) { |
| 2094 | bool NestingDetected = false; |
| 2095 | InstructionClass Class = GetInstructionClass(Inst); |
| 2096 | const Value *Arg = 0; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2097 | |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 2098 | switch (Class) { |
| 2099 | case IC_RetainBlock: |
Michael Gottesman | 158fdf6 | 2013-03-28 20:11:19 +0000 | [diff] [blame] | 2100 | // In OptimizeIndividualCalls, we have strength reduced all optimizable |
| 2101 | // objc_retainBlocks to objc_retains. Thus at this point any |
| 2102 | // objc_retainBlocks that we see are not optimizable. |
| 2103 | break; |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 2104 | case IC_Retain: |
| 2105 | case IC_RetainRV: { |
| 2106 | Arg = GetObjCArg(Inst); |
| 2107 | |
| 2108 | PtrState &S = MyStates.getPtrTopDownState(Arg); |
| 2109 | |
| 2110 | // Don't do retain+release tracking for IC_RetainRV, because it's |
| 2111 | // better to let it remain as the first instruction after a call. |
| 2112 | if (Class != IC_RetainRV) { |
| 2113 | // If we see two retains in a row on the same pointer. If so, make |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2114 | // a note, and we'll cicle back to revisit it after we've |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 2115 | // hopefully eliminated the second retain, which may allow us to |
| 2116 | // eliminate the first retain too. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2117 | // Theoretically we could implement removal of nested retain+release |
| 2118 | // pairs by making PtrState hold a stack of states, but this is |
| 2119 | // simple and avoids adding overhead for the non-nested case. |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 2120 | if (S.GetSeq() == S_Retain) |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2121 | NestingDetected = true; |
| 2122 | |
Michael Gottesman | 81b1d43 | 2013-03-26 00:42:04 +0000 | [diff] [blame] | 2123 | ANNOTATE_TOPDOWN(Inst, Arg, S.GetSeq(), S_Retain); |
Dan Gohman | 62079b4 | 2012-04-25 00:50:46 +0000 | [diff] [blame] | 2124 | S.ResetSequenceProgress(S_Retain); |
Michael Gottesman | 07beea4 | 2013-03-23 05:31:01 +0000 | [diff] [blame] | 2125 | S.RRI.KnownSafe = S.HasKnownPositiveRefCount(); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2126 | S.RRI.Calls.insert(Inst); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2127 | } |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2128 | |
Dan Gohman | df476e5 | 2012-09-04 23:16:20 +0000 | [diff] [blame] | 2129 | S.SetKnownPositiveRefCount(); |
Dan Gohman | f64ff8e | 2012-07-23 19:27:31 +0000 | [diff] [blame] | 2130 | |
| 2131 | // A retain can be a potential use; procede to the generic checking |
| 2132 | // code below. |
| 2133 | break; |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 2134 | } |
| 2135 | case IC_Release: { |
| 2136 | Arg = GetObjCArg(Inst); |
| 2137 | |
| 2138 | PtrState &S = MyStates.getPtrTopDownState(Arg); |
Michael Gottesman | 764b1cf | 2013-03-23 05:46:19 +0000 | [diff] [blame] | 2139 | S.ClearKnownPositiveRefCount(); |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 2140 | |
Michael Gottesman | 1d8d257 | 2013-04-05 22:54:28 +0000 | [diff] [blame] | 2141 | Sequence OldSeq = S.GetSeq(); |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 2142 | |
Michael Gottesman | 1d8d257 | 2013-04-05 22:54:28 +0000 | [diff] [blame] | 2143 | MDNode *ReleaseMetadata = Inst->getMetadata(ImpreciseReleaseMDKind); |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 2144 | |
Michael Gottesman | 1d8d257 | 2013-04-05 22:54:28 +0000 | [diff] [blame] | 2145 | switch (OldSeq) { |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 2146 | case S_Retain: |
| 2147 | case S_CanRelease: |
Michael Gottesman | 1d8d257 | 2013-04-05 22:54:28 +0000 | [diff] [blame] | 2148 | if (OldSeq == S_Retain || ReleaseMetadata != 0) |
| 2149 | S.RRI.ReverseInsertPts.clear(); |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 2150 | // FALL THROUGH |
| 2151 | case S_Use: |
Michael Gottesman | 1d8d257 | 2013-04-05 22:54:28 +0000 | [diff] [blame] | 2152 | S.RRI.ReleaseMetadata = ReleaseMetadata; |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 2153 | S.RRI.IsTailCallRelease = cast<CallInst>(Inst)->isTailCall(); |
| 2154 | Releases[Inst] = S.RRI; |
Michael Gottesman | 81b1d43 | 2013-03-26 00:42:04 +0000 | [diff] [blame] | 2155 | ANNOTATE_TOPDOWN(Inst, Arg, S.GetSeq(), S_None); |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 2156 | S.ClearSequenceProgress(); |
| 2157 | break; |
| 2158 | case S_None: |
| 2159 | break; |
| 2160 | case S_Stop: |
| 2161 | case S_Release: |
| 2162 | case S_MovableRelease: |
| 2163 | llvm_unreachable("top-down pointer in release state!"); |
| 2164 | } |
| 2165 | break; |
| 2166 | } |
| 2167 | case IC_AutoreleasepoolPop: |
| 2168 | // Conservatively, clear MyStates for all known pointers. |
| 2169 | MyStates.clearTopDownPointers(); |
| 2170 | return NestingDetected; |
| 2171 | case IC_AutoreleasepoolPush: |
| 2172 | case IC_None: |
| 2173 | // These are irrelevant. |
| 2174 | return NestingDetected; |
| 2175 | default: |
| 2176 | break; |
| 2177 | } |
| 2178 | |
| 2179 | // Consider any other possible effects of this instruction on each |
| 2180 | // pointer being tracked. |
| 2181 | for (BBState::ptr_iterator MI = MyStates.top_down_ptr_begin(), |
| 2182 | ME = MyStates.top_down_ptr_end(); MI != ME; ++MI) { |
| 2183 | const Value *Ptr = MI->first; |
| 2184 | if (Ptr == Arg) |
| 2185 | continue; // Handled above. |
| 2186 | PtrState &S = MI->second; |
| 2187 | Sequence Seq = S.GetSeq(); |
| 2188 | |
| 2189 | // Check for possible releases. |
| 2190 | if (CanAlterRefCount(Inst, Ptr, PA, Class)) { |
Michael Gottesman | bab49e97 | 2013-04-05 18:26:08 +0000 | [diff] [blame] | 2191 | DEBUG(dbgs() << "CanAlterRefCount: Seq: " << Seq << "; " << *Ptr |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 2192 | << "\n"); |
Michael Gottesman | 764b1cf | 2013-03-23 05:46:19 +0000 | [diff] [blame] | 2193 | S.ClearKnownPositiveRefCount(); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2194 | switch (Seq) { |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 2195 | case S_Retain: |
| 2196 | S.SetSeq(S_CanRelease); |
Michael Gottesman | 81b1d43 | 2013-03-26 00:42:04 +0000 | [diff] [blame] | 2197 | ANNOTATE_TOPDOWN(Inst, Ptr, Seq, S_CanRelease); |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 2198 | assert(S.RRI.ReverseInsertPts.empty()); |
| 2199 | S.RRI.ReverseInsertPts.insert(Inst); |
| 2200 | |
| 2201 | // One call can't cause a transition from S_Retain to S_CanRelease |
| 2202 | // and S_CanRelease to S_Use. If we've made the first transition, |
| 2203 | // we're done. |
| 2204 | continue; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2205 | case S_Use: |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 2206 | case S_CanRelease: |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2207 | case S_None: |
| 2208 | break; |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 2209 | case S_Stop: |
| 2210 | case S_Release: |
| 2211 | case S_MovableRelease: |
| 2212 | llvm_unreachable("top-down pointer in release state!"); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2213 | } |
| 2214 | } |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 2215 | |
| 2216 | // Check for possible direct uses. |
| 2217 | switch (Seq) { |
| 2218 | case S_CanRelease: |
Michael Gottesman | 81b1d43 | 2013-03-26 00:42:04 +0000 | [diff] [blame] | 2219 | if (CanUse(Inst, Ptr, PA, Class)) { |
Michael Gottesman | bab49e97 | 2013-04-05 18:26:08 +0000 | [diff] [blame] | 2220 | DEBUG(dbgs() << "CanUse: Seq: " << Seq << "; " << *Ptr |
| 2221 | << "\n"); |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 2222 | S.SetSeq(S_Use); |
Michael Gottesman | 81b1d43 | 2013-03-26 00:42:04 +0000 | [diff] [blame] | 2223 | ANNOTATE_TOPDOWN(Inst, Ptr, Seq, S_Use); |
| 2224 | } |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 2225 | break; |
| 2226 | case S_Retain: |
| 2227 | case S_Use: |
| 2228 | case S_None: |
| 2229 | break; |
| 2230 | case S_Stop: |
| 2231 | case S_Release: |
| 2232 | case S_MovableRelease: |
| 2233 | llvm_unreachable("top-down pointer in release state!"); |
| 2234 | } |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2235 | } |
| 2236 | |
| 2237 | return NestingDetected; |
| 2238 | } |
| 2239 | |
| 2240 | bool |
| 2241 | ObjCARCOpt::VisitTopDown(BasicBlock *BB, |
| 2242 | DenseMap<const BasicBlock *, BBState> &BBStates, |
| 2243 | DenseMap<Value *, RRInfo> &Releases) { |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 2244 | DEBUG(dbgs() << "\n== ObjCARCOpt::VisitTopDown ==\n"); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2245 | bool NestingDetected = false; |
| 2246 | BBState &MyStates = BBStates[BB]; |
| 2247 | |
| 2248 | // Merge the states from each predecessor to compute the initial state |
| 2249 | // for the current block. |
Dan Gohman | 10c82ce | 2012-08-27 18:31:36 +0000 | [diff] [blame] | 2250 | BBState::edge_iterator PI(MyStates.pred_begin()), |
| 2251 | PE(MyStates.pred_end()); |
| 2252 | if (PI != PE) { |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 2253 | const BasicBlock *Pred = *PI; |
| 2254 | DenseMap<const BasicBlock *, BBState>::iterator I = BBStates.find(Pred); |
| 2255 | assert(I != BBStates.end()); |
| 2256 | MyStates.InitFromPred(I->second); |
| 2257 | ++PI; |
| 2258 | for (; PI != PE; ++PI) { |
| 2259 | Pred = *PI; |
| 2260 | I = BBStates.find(Pred); |
| 2261 | assert(I != BBStates.end()); |
| 2262 | MyStates.MergePred(I->second); |
| 2263 | } |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 2264 | } |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2265 | |
Michael Gottesman | 43e7e00 | 2013-04-03 22:41:59 +0000 | [diff] [blame] | 2266 | // If ARC Annotations are enabled, output the current state of pointers at the |
| 2267 | // top of the basic block. |
| 2268 | ANNOTATE_TOPDOWN_BBSTART(MyStates, BB); |
Michael Gottesman | c2d5bf5 | 2013-04-03 23:07:45 +0000 | [diff] [blame] | 2269 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2270 | // Visit all the instructions, top-down. |
| 2271 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) { |
| 2272 | Instruction *Inst = I; |
Michael Gottesman | af2113f | 2013-01-13 07:00:51 +0000 | [diff] [blame] | 2273 | |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 2274 | DEBUG(dbgs() << "Visiting " << *Inst << "\n"); |
Michael Gottesman | af2113f | 2013-01-13 07:00:51 +0000 | [diff] [blame] | 2275 | |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 2276 | NestingDetected |= VisitInstructionTopDown(Inst, Releases, MyStates); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2277 | } |
Michael Gottesman | c2d5bf5 | 2013-04-03 23:07:45 +0000 | [diff] [blame] | 2278 | |
Michael Gottesman | 43e7e00 | 2013-04-03 22:41:59 +0000 | [diff] [blame] | 2279 | // If ARC Annotations are enabled, output the current state of pointers at the |
| 2280 | // bottom of the basic block. |
| 2281 | ANNOTATE_TOPDOWN_BBEND(MyStates, BB); |
Michael Gottesman | c2d5bf5 | 2013-04-03 23:07:45 +0000 | [diff] [blame] | 2282 | |
Michael Gottesman | ffef24f | 2013-04-17 20:48:01 +0000 | [diff] [blame] | 2283 | #ifdef ARC_ANNOTATIONS |
Michael Gottesman | adb921a | 2013-04-17 21:03:53 +0000 | [diff] [blame] | 2284 | if (!(EnableARCAnnotations && DisableCheckForCFGHazards)) |
Michael Gottesman | ffef24f | 2013-04-17 20:48:01 +0000 | [diff] [blame] | 2285 | #endif |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2286 | CheckForCFGHazards(BB, BBStates, MyStates); |
| 2287 | return NestingDetected; |
| 2288 | } |
| 2289 | |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 2290 | static void |
| 2291 | ComputePostOrders(Function &F, |
| 2292 | SmallVectorImpl<BasicBlock *> &PostOrder, |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 2293 | SmallVectorImpl<BasicBlock *> &ReverseCFGPostOrder, |
| 2294 | unsigned NoObjCARCExceptionsMDKind, |
| 2295 | DenseMap<const BasicBlock *, BBState> &BBStates) { |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 2296 | /// The visited set, for doing DFS walks. |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 2297 | SmallPtrSet<BasicBlock *, 16> Visited; |
| 2298 | |
| 2299 | // Do DFS, computing the PostOrder. |
| 2300 | SmallPtrSet<BasicBlock *, 16> OnStack; |
| 2301 | SmallVector<std::pair<BasicBlock *, succ_iterator>, 16> SuccStack; |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 2302 | |
| 2303 | // Functions always have exactly one entry block, and we don't have |
| 2304 | // any other block that we treat like an entry block. |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 2305 | BasicBlock *EntryBB = &F.getEntryBlock(); |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 2306 | BBState &MyStates = BBStates[EntryBB]; |
| 2307 | MyStates.SetAsEntry(); |
| 2308 | TerminatorInst *EntryTI = cast<TerminatorInst>(&EntryBB->back()); |
| 2309 | SuccStack.push_back(std::make_pair(EntryBB, succ_iterator(EntryTI))); |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 2310 | Visited.insert(EntryBB); |
| 2311 | OnStack.insert(EntryBB); |
| 2312 | do { |
| 2313 | dfs_next_succ: |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 2314 | BasicBlock *CurrBB = SuccStack.back().first; |
| 2315 | TerminatorInst *TI = cast<TerminatorInst>(&CurrBB->back()); |
| 2316 | succ_iterator SE(TI, false); |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 2317 | |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 2318 | while (SuccStack.back().second != SE) { |
| 2319 | BasicBlock *SuccBB = *SuccStack.back().second++; |
| 2320 | if (Visited.insert(SuccBB)) { |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 2321 | TerminatorInst *TI = cast<TerminatorInst>(&SuccBB->back()); |
| 2322 | SuccStack.push_back(std::make_pair(SuccBB, succ_iterator(TI))); |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 2323 | BBStates[CurrBB].addSucc(SuccBB); |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 2324 | BBState &SuccStates = BBStates[SuccBB]; |
| 2325 | SuccStates.addPred(CurrBB); |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 2326 | OnStack.insert(SuccBB); |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 2327 | goto dfs_next_succ; |
| 2328 | } |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 2329 | |
| 2330 | if (!OnStack.count(SuccBB)) { |
| 2331 | BBStates[CurrBB].addSucc(SuccBB); |
| 2332 | BBStates[SuccBB].addPred(CurrBB); |
| 2333 | } |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 2334 | } |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 2335 | OnStack.erase(CurrBB); |
| 2336 | PostOrder.push_back(CurrBB); |
| 2337 | SuccStack.pop_back(); |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 2338 | } while (!SuccStack.empty()); |
| 2339 | |
| 2340 | Visited.clear(); |
| 2341 | |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 2342 | // Do reverse-CFG DFS, computing the reverse-CFG PostOrder. |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 2343 | // Functions may have many exits, and there also blocks which we treat |
| 2344 | // as exits due to ignored edges. |
| 2345 | SmallVector<std::pair<BasicBlock *, BBState::edge_iterator>, 16> PredStack; |
| 2346 | for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) { |
| 2347 | BasicBlock *ExitBB = I; |
| 2348 | BBState &MyStates = BBStates[ExitBB]; |
| 2349 | if (!MyStates.isExit()) |
| 2350 | continue; |
| 2351 | |
Dan Gohman | dae3349 | 2012-04-27 18:56:31 +0000 | [diff] [blame] | 2352 | MyStates.SetAsExit(); |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 2353 | |
| 2354 | PredStack.push_back(std::make_pair(ExitBB, MyStates.pred_begin())); |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 2355 | Visited.insert(ExitBB); |
| 2356 | while (!PredStack.empty()) { |
| 2357 | reverse_dfs_next_succ: |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 2358 | BBState::edge_iterator PE = BBStates[PredStack.back().first].pred_end(); |
| 2359 | while (PredStack.back().second != PE) { |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 2360 | BasicBlock *BB = *PredStack.back().second++; |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 2361 | if (Visited.insert(BB)) { |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 2362 | PredStack.push_back(std::make_pair(BB, BBStates[BB].pred_begin())); |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 2363 | goto reverse_dfs_next_succ; |
| 2364 | } |
| 2365 | } |
| 2366 | ReverseCFGPostOrder.push_back(PredStack.pop_back_val().first); |
| 2367 | } |
| 2368 | } |
| 2369 | } |
| 2370 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 2371 | // Visit the function both top-down and bottom-up. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2372 | bool |
| 2373 | ObjCARCOpt::Visit(Function &F, |
| 2374 | DenseMap<const BasicBlock *, BBState> &BBStates, |
| 2375 | MapVector<Value *, RRInfo> &Retains, |
| 2376 | DenseMap<Value *, RRInfo> &Releases) { |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 2377 | |
| 2378 | // Use reverse-postorder traversals, because we magically know that loops |
| 2379 | // will be well behaved, i.e. they won't repeatedly call retain on a single |
| 2380 | // pointer without doing a release. We can't use the ReversePostOrderTraversal |
| 2381 | // class here because we want the reverse-CFG postorder to consider each |
| 2382 | // function exit point, and we want to ignore selected cycle edges. |
| 2383 | SmallVector<BasicBlock *, 16> PostOrder; |
| 2384 | SmallVector<BasicBlock *, 16> ReverseCFGPostOrder; |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 2385 | ComputePostOrders(F, PostOrder, ReverseCFGPostOrder, |
| 2386 | NoObjCARCExceptionsMDKind, |
| 2387 | BBStates); |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 2388 | |
| 2389 | // Use reverse-postorder on the reverse CFG for bottom-up. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2390 | bool BottomUpNestingDetected = false; |
Dan Gohman | c57b58c | 2011-08-18 21:27:42 +0000 | [diff] [blame] | 2391 | for (SmallVectorImpl<BasicBlock *>::const_reverse_iterator I = |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 2392 | ReverseCFGPostOrder.rbegin(), E = ReverseCFGPostOrder.rend(); |
| 2393 | I != E; ++I) |
| 2394 | BottomUpNestingDetected |= VisitBottomUp(*I, BBStates, Retains); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2395 | |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 2396 | // Use reverse-postorder for top-down. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2397 | bool TopDownNestingDetected = false; |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 2398 | for (SmallVectorImpl<BasicBlock *>::const_reverse_iterator I = |
| 2399 | PostOrder.rbegin(), E = PostOrder.rend(); |
| 2400 | I != E; ++I) |
| 2401 | TopDownNestingDetected |= VisitTopDown(*I, BBStates, Releases); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2402 | |
| 2403 | return TopDownNestingDetected && BottomUpNestingDetected; |
| 2404 | } |
| 2405 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 2406 | /// Move the calls in RetainsToMove and ReleasesToMove. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2407 | void ObjCARCOpt::MoveCalls(Value *Arg, |
| 2408 | RRInfo &RetainsToMove, |
| 2409 | RRInfo &ReleasesToMove, |
| 2410 | MapVector<Value *, RRInfo> &Retains, |
| 2411 | DenseMap<Value *, RRInfo> &Releases, |
Dan Gohman | 6320f52 | 2011-07-22 22:29:21 +0000 | [diff] [blame] | 2412 | SmallVectorImpl<Instruction *> &DeadInsts, |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 2413 | Module *M) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2414 | Type *ArgTy = Arg->getType(); |
Dan Gohman | 6320f52 | 2011-07-22 22:29:21 +0000 | [diff] [blame] | 2415 | Type *ParamTy = PointerType::getUnqual(Type::getInt8Ty(ArgTy->getContext())); |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 2416 | |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 2417 | DEBUG(dbgs() << "== ObjCARCOpt::MoveCalls ==\n"); |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 2418 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2419 | // Insert the new retain and release calls. |
| 2420 | for (SmallPtrSet<Instruction *, 2>::const_iterator |
| 2421 | PI = ReleasesToMove.ReverseInsertPts.begin(), |
| 2422 | PE = ReleasesToMove.ReverseInsertPts.end(); PI != PE; ++PI) { |
| 2423 | Instruction *InsertPt = *PI; |
| 2424 | Value *MyArg = ArgTy == ParamTy ? Arg : |
| 2425 | new BitCastInst(Arg, ParamTy, "", InsertPt); |
| 2426 | CallInst *Call = |
Michael Gottesman | ba64859 | 2013-03-28 23:08:44 +0000 | [diff] [blame] | 2427 | CallInst::Create(getRetainCallee(M), MyArg, "", InsertPt); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2428 | Call->setDoesNotThrow(); |
Michael Gottesman | ba64859 | 2013-03-28 23:08:44 +0000 | [diff] [blame] | 2429 | Call->setTailCall(); |
Michael Gottesman | 60f6b28 | 2013-03-29 05:13:07 +0000 | [diff] [blame] | 2430 | |
Michael Gottesman | df110ac | 2013-04-21 00:30:50 +0000 | [diff] [blame] | 2431 | DEBUG(dbgs() << "Inserting new Retain: " << *Call << "\n" |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 2432 | "At insertion point: " << *InsertPt << "\n"); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2433 | } |
| 2434 | for (SmallPtrSet<Instruction *, 2>::const_iterator |
| 2435 | PI = RetainsToMove.ReverseInsertPts.begin(), |
| 2436 | PE = RetainsToMove.ReverseInsertPts.end(); PI != PE; ++PI) { |
Dan Gohman | 5c70fad | 2012-03-23 17:47:54 +0000 | [diff] [blame] | 2437 | Instruction *InsertPt = *PI; |
| 2438 | Value *MyArg = ArgTy == ParamTy ? Arg : |
| 2439 | new BitCastInst(Arg, ParamTy, "", InsertPt); |
| 2440 | CallInst *Call = CallInst::Create(getReleaseCallee(M), MyArg, |
| 2441 | "", InsertPt); |
| 2442 | // Attach a clang.imprecise_release metadata tag, if appropriate. |
| 2443 | if (MDNode *M = ReleasesToMove.ReleaseMetadata) |
| 2444 | Call->setMetadata(ImpreciseReleaseMDKind, M); |
| 2445 | Call->setDoesNotThrow(); |
| 2446 | if (ReleasesToMove.IsTailCallRelease) |
| 2447 | Call->setTailCall(); |
Michael Gottesman | c189a39 | 2013-01-09 19:23:24 +0000 | [diff] [blame] | 2448 | |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 2449 | DEBUG(dbgs() << "Inserting new Release: " << *Call << "\n" |
| 2450 | "At insertion point: " << *InsertPt << "\n"); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2451 | } |
| 2452 | |
| 2453 | // Delete the original retain and release calls. |
| 2454 | for (SmallPtrSet<Instruction *, 2>::const_iterator |
| 2455 | AI = RetainsToMove.Calls.begin(), |
| 2456 | AE = RetainsToMove.Calls.end(); AI != AE; ++AI) { |
| 2457 | Instruction *OrigRetain = *AI; |
| 2458 | Retains.blot(OrigRetain); |
| 2459 | DeadInsts.push_back(OrigRetain); |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 2460 | DEBUG(dbgs() << "Deleting retain: " << *OrigRetain << "\n"); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2461 | } |
| 2462 | for (SmallPtrSet<Instruction *, 2>::const_iterator |
| 2463 | AI = ReleasesToMove.Calls.begin(), |
| 2464 | AE = ReleasesToMove.Calls.end(); AI != AE; ++AI) { |
| 2465 | Instruction *OrigRelease = *AI; |
| 2466 | Releases.erase(OrigRelease); |
| 2467 | DeadInsts.push_back(OrigRelease); |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 2468 | DEBUG(dbgs() << "Deleting release: " << *OrigRelease << "\n"); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2469 | } |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 2470 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2471 | } |
| 2472 | |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 2473 | bool |
| 2474 | ObjCARCOpt::ConnectTDBUTraversals(DenseMap<const BasicBlock *, BBState> |
| 2475 | &BBStates, |
| 2476 | MapVector<Value *, RRInfo> &Retains, |
| 2477 | DenseMap<Value *, RRInfo> &Releases, |
| 2478 | Module *M, |
| 2479 | SmallVector<Instruction *, 4> &NewRetains, |
| 2480 | SmallVector<Instruction *, 4> &NewReleases, |
| 2481 | SmallVector<Instruction *, 8> &DeadInsts, |
| 2482 | RRInfo &RetainsToMove, |
| 2483 | RRInfo &ReleasesToMove, |
| 2484 | Value *Arg, |
| 2485 | bool KnownSafe, |
| 2486 | bool &AnyPairsCompletelyEliminated) { |
| 2487 | // If a pair happens in a region where it is known that the reference count |
Michael Gottesman | a76143ee | 2013-05-13 23:49:42 +0000 | [diff] [blame] | 2488 | // is already incremented, we can similarly ignore possible decrements unless |
| 2489 | // we are dealing with a retainable object with multiple provenance sources. |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 2490 | bool KnownSafeTD = true, KnownSafeBU = true; |
Michael Gottesman | a76143ee | 2013-05-13 23:49:42 +0000 | [diff] [blame] | 2491 | bool MultipleOwners = false; |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 2492 | |
| 2493 | // Connect the dots between the top-down-collected RetainsToMove and |
| 2494 | // bottom-up-collected ReleasesToMove to form sets of related calls. |
| 2495 | // This is an iterative process so that we connect multiple releases |
| 2496 | // to multiple retains if needed. |
| 2497 | unsigned OldDelta = 0; |
| 2498 | unsigned NewDelta = 0; |
| 2499 | unsigned OldCount = 0; |
| 2500 | unsigned NewCount = 0; |
| 2501 | bool FirstRelease = true; |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 2502 | for (;;) { |
| 2503 | for (SmallVectorImpl<Instruction *>::const_iterator |
| 2504 | NI = NewRetains.begin(), NE = NewRetains.end(); NI != NE; ++NI) { |
| 2505 | Instruction *NewRetain = *NI; |
| 2506 | MapVector<Value *, RRInfo>::const_iterator It = Retains.find(NewRetain); |
| 2507 | assert(It != Retains.end()); |
| 2508 | const RRInfo &NewRetainRRI = It->second; |
| 2509 | KnownSafeTD &= NewRetainRRI.KnownSafe; |
Michael Gottesman | 5a91bbf | 2013-05-24 20:44:02 +0000 | [diff] [blame^] | 2510 | MultipleOwners = |
| 2511 | MultipleOwners || MultiOwnersSet.count(GetObjCArg(NewRetain)); |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 2512 | for (SmallPtrSet<Instruction *, 2>::const_iterator |
| 2513 | LI = NewRetainRRI.Calls.begin(), |
| 2514 | LE = NewRetainRRI.Calls.end(); LI != LE; ++LI) { |
| 2515 | Instruction *NewRetainRelease = *LI; |
| 2516 | DenseMap<Value *, RRInfo>::const_iterator Jt = |
| 2517 | Releases.find(NewRetainRelease); |
| 2518 | if (Jt == Releases.end()) |
| 2519 | return false; |
| 2520 | const RRInfo &NewRetainReleaseRRI = Jt->second; |
| 2521 | assert(NewRetainReleaseRRI.Calls.count(NewRetain)); |
| 2522 | if (ReleasesToMove.Calls.insert(NewRetainRelease)) { |
| 2523 | OldDelta -= |
| 2524 | BBStates[NewRetainRelease->getParent()].GetAllPathCount(); |
| 2525 | |
| 2526 | // Merge the ReleaseMetadata and IsTailCallRelease values. |
| 2527 | if (FirstRelease) { |
| 2528 | ReleasesToMove.ReleaseMetadata = |
| 2529 | NewRetainReleaseRRI.ReleaseMetadata; |
| 2530 | ReleasesToMove.IsTailCallRelease = |
| 2531 | NewRetainReleaseRRI.IsTailCallRelease; |
| 2532 | FirstRelease = false; |
| 2533 | } else { |
| 2534 | if (ReleasesToMove.ReleaseMetadata != |
| 2535 | NewRetainReleaseRRI.ReleaseMetadata) |
| 2536 | ReleasesToMove.ReleaseMetadata = 0; |
| 2537 | if (ReleasesToMove.IsTailCallRelease != |
| 2538 | NewRetainReleaseRRI.IsTailCallRelease) |
| 2539 | ReleasesToMove.IsTailCallRelease = false; |
| 2540 | } |
| 2541 | |
| 2542 | // Collect the optimal insertion points. |
| 2543 | if (!KnownSafe) |
| 2544 | for (SmallPtrSet<Instruction *, 2>::const_iterator |
| 2545 | RI = NewRetainReleaseRRI.ReverseInsertPts.begin(), |
| 2546 | RE = NewRetainReleaseRRI.ReverseInsertPts.end(); |
| 2547 | RI != RE; ++RI) { |
| 2548 | Instruction *RIP = *RI; |
| 2549 | if (ReleasesToMove.ReverseInsertPts.insert(RIP)) |
| 2550 | NewDelta -= BBStates[RIP->getParent()].GetAllPathCount(); |
| 2551 | } |
| 2552 | NewReleases.push_back(NewRetainRelease); |
| 2553 | } |
| 2554 | } |
| 2555 | } |
| 2556 | NewRetains.clear(); |
| 2557 | if (NewReleases.empty()) break; |
| 2558 | |
| 2559 | // Back the other way. |
| 2560 | for (SmallVectorImpl<Instruction *>::const_iterator |
| 2561 | NI = NewReleases.begin(), NE = NewReleases.end(); NI != NE; ++NI) { |
| 2562 | Instruction *NewRelease = *NI; |
| 2563 | DenseMap<Value *, RRInfo>::const_iterator It = |
| 2564 | Releases.find(NewRelease); |
| 2565 | assert(It != Releases.end()); |
| 2566 | const RRInfo &NewReleaseRRI = It->second; |
| 2567 | KnownSafeBU &= NewReleaseRRI.KnownSafe; |
| 2568 | for (SmallPtrSet<Instruction *, 2>::const_iterator |
| 2569 | LI = NewReleaseRRI.Calls.begin(), |
| 2570 | LE = NewReleaseRRI.Calls.end(); LI != LE; ++LI) { |
| 2571 | Instruction *NewReleaseRetain = *LI; |
| 2572 | MapVector<Value *, RRInfo>::const_iterator Jt = |
| 2573 | Retains.find(NewReleaseRetain); |
| 2574 | if (Jt == Retains.end()) |
| 2575 | return false; |
| 2576 | const RRInfo &NewReleaseRetainRRI = Jt->second; |
| 2577 | assert(NewReleaseRetainRRI.Calls.count(NewRelease)); |
| 2578 | if (RetainsToMove.Calls.insert(NewReleaseRetain)) { |
| 2579 | unsigned PathCount = |
| 2580 | BBStates[NewReleaseRetain->getParent()].GetAllPathCount(); |
| 2581 | OldDelta += PathCount; |
| 2582 | OldCount += PathCount; |
| 2583 | |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 2584 | // Collect the optimal insertion points. |
| 2585 | if (!KnownSafe) |
| 2586 | for (SmallPtrSet<Instruction *, 2>::const_iterator |
| 2587 | RI = NewReleaseRetainRRI.ReverseInsertPts.begin(), |
| 2588 | RE = NewReleaseRetainRRI.ReverseInsertPts.end(); |
| 2589 | RI != RE; ++RI) { |
| 2590 | Instruction *RIP = *RI; |
| 2591 | if (RetainsToMove.ReverseInsertPts.insert(RIP)) { |
| 2592 | PathCount = BBStates[RIP->getParent()].GetAllPathCount(); |
| 2593 | NewDelta += PathCount; |
| 2594 | NewCount += PathCount; |
| 2595 | } |
| 2596 | } |
| 2597 | NewRetains.push_back(NewReleaseRetain); |
| 2598 | } |
| 2599 | } |
| 2600 | } |
| 2601 | NewReleases.clear(); |
| 2602 | if (NewRetains.empty()) break; |
| 2603 | } |
| 2604 | |
Michael Gottesman | a76143ee | 2013-05-13 23:49:42 +0000 | [diff] [blame] | 2605 | // If the pointer is known incremented in 1 direction and we do not have |
| 2606 | // MultipleOwners, we can safely remove the retain/releases. Otherwise we need |
| 2607 | // to be known safe in both directions. |
| 2608 | bool UnconditionallySafe = (KnownSafeTD && KnownSafeBU) || |
| 2609 | ((KnownSafeTD || KnownSafeBU) && !MultipleOwners); |
| 2610 | if (UnconditionallySafe) { |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 2611 | RetainsToMove.ReverseInsertPts.clear(); |
| 2612 | ReleasesToMove.ReverseInsertPts.clear(); |
| 2613 | NewCount = 0; |
| 2614 | } else { |
| 2615 | // Determine whether the new insertion points we computed preserve the |
| 2616 | // balance of retain and release calls through the program. |
| 2617 | // TODO: If the fully aggressive solution isn't valid, try to find a |
| 2618 | // less aggressive solution which is. |
| 2619 | if (NewDelta != 0) |
| 2620 | return false; |
| 2621 | } |
| 2622 | |
| 2623 | // Determine whether the original call points are balanced in the retain and |
| 2624 | // release calls through the program. If not, conservatively don't touch |
| 2625 | // them. |
| 2626 | // TODO: It's theoretically possible to do code motion in this case, as |
| 2627 | // long as the existing imbalances are maintained. |
| 2628 | if (OldDelta != 0) |
| 2629 | return false; |
Michael Gottesman | 8005ad3 | 2013-04-29 06:16:55 +0000 | [diff] [blame] | 2630 | |
Michael Gottesman | a87bb8f | 2013-04-29 05:13:13 +0000 | [diff] [blame] | 2631 | #ifdef ARC_ANNOTATIONS |
| 2632 | // Do not move calls if ARC annotations are requested. |
Michael Gottesman | 3e3977c | 2013-04-29 05:25:39 +0000 | [diff] [blame] | 2633 | if (EnableARCAnnotations) |
| 2634 | return false; |
Michael Gottesman | a87bb8f | 2013-04-29 05:13:13 +0000 | [diff] [blame] | 2635 | #endif // ARC_ANNOTATIONS |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 2636 | |
| 2637 | Changed = true; |
| 2638 | assert(OldCount != 0 && "Unreachable code?"); |
| 2639 | NumRRs += OldCount - NewCount; |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 2640 | // Set to true if we completely removed any RR pairs. |
Michael Gottesman | 8b5515f | 2013-01-22 21:53:43 +0000 | [diff] [blame] | 2641 | AnyPairsCompletelyEliminated = NewCount == 0; |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 2642 | |
| 2643 | // We can move calls! |
| 2644 | return true; |
| 2645 | } |
| 2646 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 2647 | /// Identify pairings between the retains and releases, and delete and/or move |
| 2648 | /// them. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2649 | bool |
| 2650 | ObjCARCOpt::PerformCodePlacement(DenseMap<const BasicBlock *, BBState> |
| 2651 | &BBStates, |
| 2652 | MapVector<Value *, RRInfo> &Retains, |
Dan Gohman | 6320f52 | 2011-07-22 22:29:21 +0000 | [diff] [blame] | 2653 | DenseMap<Value *, RRInfo> &Releases, |
| 2654 | Module *M) { |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 2655 | DEBUG(dbgs() << "\n== ObjCARCOpt::PerformCodePlacement ==\n"); |
| 2656 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2657 | bool AnyPairsCompletelyEliminated = false; |
| 2658 | RRInfo RetainsToMove; |
| 2659 | RRInfo ReleasesToMove; |
| 2660 | SmallVector<Instruction *, 4> NewRetains; |
| 2661 | SmallVector<Instruction *, 4> NewReleases; |
| 2662 | SmallVector<Instruction *, 8> DeadInsts; |
| 2663 | |
Dan Gohman | 670f937 | 2012-04-13 18:57:48 +0000 | [diff] [blame] | 2664 | // Visit each retain. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2665 | for (MapVector<Value *, RRInfo>::const_iterator I = Retains.begin(), |
Dan Gohman | 2053a5d | 2011-09-29 22:25:23 +0000 | [diff] [blame] | 2666 | E = Retains.end(); I != E; ++I) { |
| 2667 | Value *V = I->first; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2668 | if (!V) continue; // blotted |
| 2669 | |
| 2670 | Instruction *Retain = cast<Instruction>(V); |
Michael Gottesman | c189a39 | 2013-01-09 19:23:24 +0000 | [diff] [blame] | 2671 | |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 2672 | DEBUG(dbgs() << "Visiting: " << *Retain << "\n"); |
Michael Gottesman | c189a39 | 2013-01-09 19:23:24 +0000 | [diff] [blame] | 2673 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2674 | Value *Arg = GetObjCArg(Retain); |
| 2675 | |
Dan Gohman | 728db49 | 2012-01-13 00:39:07 +0000 | [diff] [blame] | 2676 | // If the object being released is in static or stack storage, we know it's |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2677 | // not being managed by ObjC reference counting, so we can delete pairs |
| 2678 | // regardless of what possible decrements or uses lie between them. |
Dan Gohman | 728db49 | 2012-01-13 00:39:07 +0000 | [diff] [blame] | 2679 | bool KnownSafe = isa<Constant>(Arg) || isa<AllocaInst>(Arg); |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 2680 | |
Dan Gohman | 56e1cef | 2011-08-22 17:29:11 +0000 | [diff] [blame] | 2681 | // A constant pointer can't be pointing to an object on the heap. It may |
| 2682 | // be reference-counted, but it won't be deleted. |
| 2683 | if (const LoadInst *LI = dyn_cast<LoadInst>(Arg)) |
| 2684 | if (const GlobalVariable *GV = |
| 2685 | dyn_cast<GlobalVariable>( |
| 2686 | StripPointerCastsAndObjCCalls(LI->getPointerOperand()))) |
| 2687 | if (GV->isConstant()) |
| 2688 | KnownSafe = true; |
| 2689 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2690 | // Connect the dots between the top-down-collected RetainsToMove and |
| 2691 | // bottom-up-collected ReleasesToMove to form sets of related calls. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2692 | NewRetains.push_back(Retain); |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 2693 | bool PerformMoveCalls = |
| 2694 | ConnectTDBUTraversals(BBStates, Retains, Releases, M, NewRetains, |
| 2695 | NewReleases, DeadInsts, RetainsToMove, |
| 2696 | ReleasesToMove, Arg, KnownSafe, |
| 2697 | AnyPairsCompletelyEliminated); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2698 | |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 2699 | if (PerformMoveCalls) { |
| 2700 | // Ok, everything checks out and we're all set. Let's move/delete some |
| 2701 | // code! |
| 2702 | MoveCalls(Arg, RetainsToMove, ReleasesToMove, |
| 2703 | Retains, Releases, DeadInsts, M); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2704 | } |
| 2705 | |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 2706 | // Clean up state for next retain. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2707 | NewReleases.clear(); |
| 2708 | NewRetains.clear(); |
| 2709 | RetainsToMove.clear(); |
| 2710 | ReleasesToMove.clear(); |
| 2711 | } |
| 2712 | |
| 2713 | // Now that we're done moving everything, we can delete the newly dead |
| 2714 | // instructions, as we no longer need them as insert points. |
| 2715 | while (!DeadInsts.empty()) |
| 2716 | EraseInstruction(DeadInsts.pop_back_val()); |
| 2717 | |
| 2718 | return AnyPairsCompletelyEliminated; |
| 2719 | } |
| 2720 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 2721 | /// Weak pointer optimizations. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2722 | void ObjCARCOpt::OptimizeWeakCalls(Function &F) { |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 2723 | DEBUG(dbgs() << "\n== ObjCARCOpt::OptimizeWeakCalls ==\n"); |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 2724 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2725 | // First, do memdep-style RLE and S2L optimizations. We can't use memdep |
| 2726 | // itself because it uses AliasAnalysis and we need to do provenance |
| 2727 | // queries instead. |
| 2728 | for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) { |
| 2729 | Instruction *Inst = &*I++; |
Michael Gottesman | 3f146e2 | 2013-01-01 16:05:48 +0000 | [diff] [blame] | 2730 | |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 2731 | DEBUG(dbgs() << "Visiting: " << *Inst << "\n"); |
Michael Gottesman | 3f146e2 | 2013-01-01 16:05:48 +0000 | [diff] [blame] | 2732 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2733 | InstructionClass Class = GetBasicInstructionClass(Inst); |
| 2734 | if (Class != IC_LoadWeak && Class != IC_LoadWeakRetained) |
| 2735 | continue; |
| 2736 | |
| 2737 | // Delete objc_loadWeak calls with no users. |
| 2738 | if (Class == IC_LoadWeak && Inst->use_empty()) { |
| 2739 | Inst->eraseFromParent(); |
| 2740 | continue; |
| 2741 | } |
| 2742 | |
| 2743 | // TODO: For now, just look for an earlier available version of this value |
| 2744 | // within the same block. Theoretically, we could do memdep-style non-local |
| 2745 | // analysis too, but that would want caching. A better approach would be to |
| 2746 | // use the technique that EarlyCSE uses. |
| 2747 | inst_iterator Current = llvm::prior(I); |
| 2748 | BasicBlock *CurrentBB = Current.getBasicBlockIterator(); |
| 2749 | for (BasicBlock::iterator B = CurrentBB->begin(), |
| 2750 | J = Current.getInstructionIterator(); |
| 2751 | J != B; --J) { |
| 2752 | Instruction *EarlierInst = &*llvm::prior(J); |
| 2753 | InstructionClass EarlierClass = GetInstructionClass(EarlierInst); |
| 2754 | switch (EarlierClass) { |
| 2755 | case IC_LoadWeak: |
| 2756 | case IC_LoadWeakRetained: { |
| 2757 | // If this is loading from the same pointer, replace this load's value |
| 2758 | // with that one. |
| 2759 | CallInst *Call = cast<CallInst>(Inst); |
| 2760 | CallInst *EarlierCall = cast<CallInst>(EarlierInst); |
| 2761 | Value *Arg = Call->getArgOperand(0); |
| 2762 | Value *EarlierArg = EarlierCall->getArgOperand(0); |
| 2763 | switch (PA.getAA()->alias(Arg, EarlierArg)) { |
| 2764 | case AliasAnalysis::MustAlias: |
| 2765 | Changed = true; |
| 2766 | // If the load has a builtin retain, insert a plain retain for it. |
| 2767 | if (Class == IC_LoadWeakRetained) { |
| 2768 | CallInst *CI = |
| 2769 | CallInst::Create(getRetainCallee(F.getParent()), EarlierCall, |
| 2770 | "", Call); |
| 2771 | CI->setTailCall(); |
| 2772 | } |
| 2773 | // Zap the fully redundant load. |
| 2774 | Call->replaceAllUsesWith(EarlierCall); |
| 2775 | Call->eraseFromParent(); |
| 2776 | goto clobbered; |
| 2777 | case AliasAnalysis::MayAlias: |
| 2778 | case AliasAnalysis::PartialAlias: |
| 2779 | goto clobbered; |
| 2780 | case AliasAnalysis::NoAlias: |
| 2781 | break; |
| 2782 | } |
| 2783 | break; |
| 2784 | } |
| 2785 | case IC_StoreWeak: |
| 2786 | case IC_InitWeak: { |
| 2787 | // If this is storing to the same pointer and has the same size etc. |
| 2788 | // replace this load's value with the stored value. |
| 2789 | CallInst *Call = cast<CallInst>(Inst); |
| 2790 | CallInst *EarlierCall = cast<CallInst>(EarlierInst); |
| 2791 | Value *Arg = Call->getArgOperand(0); |
| 2792 | Value *EarlierArg = EarlierCall->getArgOperand(0); |
| 2793 | switch (PA.getAA()->alias(Arg, EarlierArg)) { |
| 2794 | case AliasAnalysis::MustAlias: |
| 2795 | Changed = true; |
| 2796 | // If the load has a builtin retain, insert a plain retain for it. |
| 2797 | if (Class == IC_LoadWeakRetained) { |
| 2798 | CallInst *CI = |
| 2799 | CallInst::Create(getRetainCallee(F.getParent()), EarlierCall, |
| 2800 | "", Call); |
| 2801 | CI->setTailCall(); |
| 2802 | } |
| 2803 | // Zap the fully redundant load. |
| 2804 | Call->replaceAllUsesWith(EarlierCall->getArgOperand(1)); |
| 2805 | Call->eraseFromParent(); |
| 2806 | goto clobbered; |
| 2807 | case AliasAnalysis::MayAlias: |
| 2808 | case AliasAnalysis::PartialAlias: |
| 2809 | goto clobbered; |
| 2810 | case AliasAnalysis::NoAlias: |
| 2811 | break; |
| 2812 | } |
| 2813 | break; |
| 2814 | } |
| 2815 | case IC_MoveWeak: |
| 2816 | case IC_CopyWeak: |
| 2817 | // TOOD: Grab the copied value. |
| 2818 | goto clobbered; |
| 2819 | case IC_AutoreleasepoolPush: |
| 2820 | case IC_None: |
John McCall | 20182ac | 2013-03-22 21:38:36 +0000 | [diff] [blame] | 2821 | case IC_IntrinsicUser: |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2822 | case IC_User: |
| 2823 | // Weak pointers are only modified through the weak entry points |
| 2824 | // (and arbitrary calls, which could call the weak entry points). |
| 2825 | break; |
| 2826 | default: |
| 2827 | // Anything else could modify the weak pointer. |
| 2828 | goto clobbered; |
| 2829 | } |
| 2830 | } |
| 2831 | clobbered:; |
| 2832 | } |
| 2833 | |
| 2834 | // Then, for each destroyWeak with an alloca operand, check to see if |
| 2835 | // the alloca and all its users can be zapped. |
| 2836 | for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) { |
| 2837 | Instruction *Inst = &*I++; |
| 2838 | InstructionClass Class = GetBasicInstructionClass(Inst); |
| 2839 | if (Class != IC_DestroyWeak) |
| 2840 | continue; |
| 2841 | |
| 2842 | CallInst *Call = cast<CallInst>(Inst); |
| 2843 | Value *Arg = Call->getArgOperand(0); |
| 2844 | if (AllocaInst *Alloca = dyn_cast<AllocaInst>(Arg)) { |
| 2845 | for (Value::use_iterator UI = Alloca->use_begin(), |
| 2846 | UE = Alloca->use_end(); UI != UE; ++UI) { |
Dan Gohman | dae3349 | 2012-04-27 18:56:31 +0000 | [diff] [blame] | 2847 | const Instruction *UserInst = cast<Instruction>(*UI); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2848 | switch (GetBasicInstructionClass(UserInst)) { |
| 2849 | case IC_InitWeak: |
| 2850 | case IC_StoreWeak: |
| 2851 | case IC_DestroyWeak: |
| 2852 | continue; |
| 2853 | default: |
| 2854 | goto done; |
| 2855 | } |
| 2856 | } |
| 2857 | Changed = true; |
| 2858 | for (Value::use_iterator UI = Alloca->use_begin(), |
| 2859 | UE = Alloca->use_end(); UI != UE; ) { |
| 2860 | CallInst *UserInst = cast<CallInst>(*UI++); |
Dan Gohman | 14862c3 | 2012-05-18 22:17:29 +0000 | [diff] [blame] | 2861 | switch (GetBasicInstructionClass(UserInst)) { |
| 2862 | case IC_InitWeak: |
| 2863 | case IC_StoreWeak: |
| 2864 | // These functions return their second argument. |
| 2865 | UserInst->replaceAllUsesWith(UserInst->getArgOperand(1)); |
| 2866 | break; |
| 2867 | case IC_DestroyWeak: |
| 2868 | // No return value. |
| 2869 | break; |
| 2870 | default: |
Dan Gohman | 9c97eea0 | 2012-05-21 17:41:28 +0000 | [diff] [blame] | 2871 | llvm_unreachable("alloca really is used!"); |
Dan Gohman | 14862c3 | 2012-05-18 22:17:29 +0000 | [diff] [blame] | 2872 | } |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2873 | UserInst->eraseFromParent(); |
| 2874 | } |
| 2875 | Alloca->eraseFromParent(); |
| 2876 | done:; |
| 2877 | } |
| 2878 | } |
| 2879 | } |
| 2880 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 2881 | /// Identify program paths which execute sequences of retains and releases which |
| 2882 | /// can be eliminated. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2883 | bool ObjCARCOpt::OptimizeSequences(Function &F) { |
Michael Gottesman | 740db97 | 2013-05-23 02:35:21 +0000 | [diff] [blame] | 2884 | // Releases, Retains - These are used to store the results of the main flow |
| 2885 | // analysis. These use Value* as the key instead of Instruction* so that the |
| 2886 | // map stays valid when we get around to rewriting code and calls get |
| 2887 | // replaced by arguments. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2888 | DenseMap<Value *, RRInfo> Releases; |
| 2889 | MapVector<Value *, RRInfo> Retains; |
| 2890 | |
Michael Gottesman | 740db97 | 2013-05-23 02:35:21 +0000 | [diff] [blame] | 2891 | // This is used during the traversal of the function to track the |
| 2892 | // states for each identified object at each block. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2893 | DenseMap<const BasicBlock *, BBState> BBStates; |
| 2894 | |
| 2895 | // Analyze the CFG of the function, and all instructions. |
| 2896 | bool NestingDetected = Visit(F, BBStates, Retains, Releases); |
| 2897 | |
| 2898 | // Transform. |
Michael Gottesman | 5a91bbf | 2013-05-24 20:44:02 +0000 | [diff] [blame^] | 2899 | bool AnyPairsCompletelyEliminated = PerformCodePlacement(BBStates, Retains, |
| 2900 | Releases, |
| 2901 | F.getParent()); |
| 2902 | |
| 2903 | // Cleanup. |
| 2904 | MultiOwnersSet.clear(); |
| 2905 | |
| 2906 | return AnyPairsCompletelyEliminated && NestingDetected; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2907 | } |
| 2908 | |
Michael Gottesman | c2d5bf5 | 2013-04-03 23:07:45 +0000 | [diff] [blame] | 2909 | /// Check if there is a dependent call earlier that does not have anything in |
| 2910 | /// between the Retain and the call that can affect the reference count of their |
| 2911 | /// shared pointer argument. Note that Retain need not be in BB. |
Michael Gottesman | 54dc7fd | 2013-04-03 23:04:28 +0000 | [diff] [blame] | 2912 | static bool |
| 2913 | HasSafePathToPredecessorCall(const Value *Arg, Instruction *Retain, |
| 2914 | SmallPtrSet<Instruction *, 4> &DepInsts, |
| 2915 | SmallPtrSet<const BasicBlock *, 4> &Visited, |
| 2916 | ProvenanceAnalysis &PA) { |
| 2917 | FindDependencies(CanChangeRetainCount, Arg, Retain->getParent(), Retain, |
| 2918 | DepInsts, Visited, PA); |
| 2919 | if (DepInsts.size() != 1) |
| 2920 | return false; |
Michael Gottesman | c2d5bf5 | 2013-04-03 23:07:45 +0000 | [diff] [blame] | 2921 | |
Michael Gottesman | 54dc7fd | 2013-04-03 23:04:28 +0000 | [diff] [blame] | 2922 | CallInst *Call = |
| 2923 | dyn_cast_or_null<CallInst>(*DepInsts.begin()); |
Michael Gottesman | c2d5bf5 | 2013-04-03 23:07:45 +0000 | [diff] [blame] | 2924 | |
Michael Gottesman | 54dc7fd | 2013-04-03 23:04:28 +0000 | [diff] [blame] | 2925 | // Check that the pointer is the return value of the call. |
| 2926 | if (!Call || Arg != Call) |
| 2927 | return false; |
Michael Gottesman | c2d5bf5 | 2013-04-03 23:07:45 +0000 | [diff] [blame] | 2928 | |
Michael Gottesman | 54dc7fd | 2013-04-03 23:04:28 +0000 | [diff] [blame] | 2929 | // Check that the call is a regular call. |
| 2930 | InstructionClass Class = GetBasicInstructionClass(Call); |
| 2931 | if (Class != IC_CallOrUser && Class != IC_Call) |
| 2932 | return false; |
Michael Gottesman | c2d5bf5 | 2013-04-03 23:07:45 +0000 | [diff] [blame] | 2933 | |
Michael Gottesman | 54dc7fd | 2013-04-03 23:04:28 +0000 | [diff] [blame] | 2934 | return true; |
| 2935 | } |
| 2936 | |
Michael Gottesman | 6908db1 | 2013-04-03 23:16:05 +0000 | [diff] [blame] | 2937 | /// Find a dependent retain that precedes the given autorelease for which there |
| 2938 | /// is nothing in between the two instructions that can affect the ref count of |
| 2939 | /// Arg. |
| 2940 | static CallInst * |
| 2941 | FindPredecessorRetainWithSafePath(const Value *Arg, BasicBlock *BB, |
| 2942 | Instruction *Autorelease, |
| 2943 | SmallPtrSet<Instruction *, 4> &DepInsts, |
| 2944 | SmallPtrSet<const BasicBlock *, 4> &Visited, |
| 2945 | ProvenanceAnalysis &PA) { |
| 2946 | FindDependencies(CanChangeRetainCount, Arg, |
| 2947 | BB, Autorelease, DepInsts, Visited, PA); |
| 2948 | if (DepInsts.size() != 1) |
| 2949 | return 0; |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 2950 | |
Michael Gottesman | 6908db1 | 2013-04-03 23:16:05 +0000 | [diff] [blame] | 2951 | CallInst *Retain = |
| 2952 | dyn_cast_or_null<CallInst>(*DepInsts.begin()); |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 2953 | |
Michael Gottesman | 6908db1 | 2013-04-03 23:16:05 +0000 | [diff] [blame] | 2954 | // Check that we found a retain with the same argument. |
| 2955 | if (!Retain || |
| 2956 | !IsRetain(GetBasicInstructionClass(Retain)) || |
| 2957 | GetObjCArg(Retain) != Arg) { |
| 2958 | return 0; |
| 2959 | } |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 2960 | |
Michael Gottesman | 6908db1 | 2013-04-03 23:16:05 +0000 | [diff] [blame] | 2961 | return Retain; |
| 2962 | } |
| 2963 | |
Michael Gottesman | 21a4ed3 | 2013-04-03 23:39:14 +0000 | [diff] [blame] | 2964 | /// Look for an ``autorelease'' instruction dependent on Arg such that there are |
| 2965 | /// no instructions dependent on Arg that need a positive ref count in between |
| 2966 | /// the autorelease and the ret. |
| 2967 | static CallInst * |
| 2968 | FindPredecessorAutoreleaseWithSafePath(const Value *Arg, BasicBlock *BB, |
| 2969 | ReturnInst *Ret, |
| 2970 | SmallPtrSet<Instruction *, 4> &DepInsts, |
| 2971 | SmallPtrSet<const BasicBlock *, 4> &V, |
| 2972 | ProvenanceAnalysis &PA) { |
| 2973 | FindDependencies(NeedsPositiveRetainCount, Arg, |
| 2974 | BB, Ret, DepInsts, V, PA); |
| 2975 | if (DepInsts.size() != 1) |
| 2976 | return 0; |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 2977 | |
Michael Gottesman | 21a4ed3 | 2013-04-03 23:39:14 +0000 | [diff] [blame] | 2978 | CallInst *Autorelease = |
| 2979 | dyn_cast_or_null<CallInst>(*DepInsts.begin()); |
| 2980 | if (!Autorelease) |
| 2981 | return 0; |
| 2982 | InstructionClass AutoreleaseClass = GetBasicInstructionClass(Autorelease); |
| 2983 | if (!IsAutorelease(AutoreleaseClass)) |
| 2984 | return 0; |
| 2985 | if (GetObjCArg(Autorelease) != Arg) |
| 2986 | return 0; |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 2987 | |
Michael Gottesman | 21a4ed3 | 2013-04-03 23:39:14 +0000 | [diff] [blame] | 2988 | return Autorelease; |
| 2989 | } |
| 2990 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 2991 | /// Look for this pattern: |
Dmitri Gribenko | 5485acd | 2012-09-14 14:57:36 +0000 | [diff] [blame] | 2992 | /// \code |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2993 | /// %call = call i8* @something(...) |
| 2994 | /// %2 = call i8* @objc_retain(i8* %call) |
| 2995 | /// %3 = call i8* @objc_autorelease(i8* %2) |
| 2996 | /// ret i8* %3 |
Dmitri Gribenko | 5485acd | 2012-09-14 14:57:36 +0000 | [diff] [blame] | 2997 | /// \endcode |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2998 | /// And delete the retain and autorelease. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2999 | void ObjCARCOpt::OptimizeReturns(Function &F) { |
| 3000 | if (!F.getReturnType()->isPointerTy()) |
| 3001 | return; |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 3002 | |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 3003 | DEBUG(dbgs() << "\n== ObjCARCOpt::OptimizeReturns ==\n"); |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 3004 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3005 | SmallPtrSet<Instruction *, 4> DependingInstructions; |
| 3006 | SmallPtrSet<const BasicBlock *, 4> Visited; |
| 3007 | for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI) { |
| 3008 | BasicBlock *BB = FI; |
| 3009 | ReturnInst *Ret = dyn_cast<ReturnInst>(&BB->back()); |
Michael Gottesman | 3f146e2 | 2013-01-01 16:05:48 +0000 | [diff] [blame] | 3010 | |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 3011 | DEBUG(dbgs() << "Visiting: " << *Ret << "\n"); |
Michael Gottesman | 3f146e2 | 2013-01-01 16:05:48 +0000 | [diff] [blame] | 3012 | |
Michael Gottesman | 21a4ed3 | 2013-04-03 23:39:14 +0000 | [diff] [blame] | 3013 | if (!Ret) |
| 3014 | continue; |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 3015 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3016 | const Value *Arg = StripPointerCastsAndObjCCalls(Ret->getOperand(0)); |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 3017 | |
Michael Gottesman | cdb7c15 | 2013-04-21 00:25:04 +0000 | [diff] [blame] | 3018 | // Look for an ``autorelease'' instruction that is a predecessor of Ret and |
Michael Gottesman | 21a4ed3 | 2013-04-03 23:39:14 +0000 | [diff] [blame] | 3019 | // dependent on Arg such that there are no instructions dependent on Arg |
| 3020 | // that need a positive ref count in between the autorelease and Ret. |
| 3021 | CallInst *Autorelease = |
| 3022 | FindPredecessorAutoreleaseWithSafePath(Arg, BB, Ret, |
| 3023 | DependingInstructions, Visited, |
| 3024 | PA); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3025 | DependingInstructions.clear(); |
| 3026 | Visited.clear(); |
Michael Gottesman | fb9ece9 | 2013-04-21 00:25:01 +0000 | [diff] [blame] | 3027 | |
| 3028 | if (!Autorelease) |
| 3029 | continue; |
| 3030 | |
| 3031 | CallInst *Retain = |
| 3032 | FindPredecessorRetainWithSafePath(Arg, BB, Autorelease, |
| 3033 | DependingInstructions, Visited, PA); |
| 3034 | DependingInstructions.clear(); |
| 3035 | Visited.clear(); |
| 3036 | |
| 3037 | if (!Retain) |
| 3038 | continue; |
| 3039 | |
| 3040 | // Check that there is nothing that can affect the reference count |
| 3041 | // between the retain and the call. Note that Retain need not be in BB. |
| 3042 | bool HasSafePathToCall = HasSafePathToPredecessorCall(Arg, Retain, |
| 3043 | DependingInstructions, |
| 3044 | Visited, PA); |
| 3045 | DependingInstructions.clear(); |
| 3046 | Visited.clear(); |
| 3047 | |
| 3048 | if (!HasSafePathToCall) |
| 3049 | continue; |
| 3050 | |
| 3051 | // If so, we can zap the retain and autorelease. |
| 3052 | Changed = true; |
| 3053 | ++NumRets; |
| 3054 | DEBUG(dbgs() << "Erasing: " << *Retain << "\nErasing: " |
| 3055 | << *Autorelease << "\n"); |
| 3056 | EraseInstruction(Retain); |
| 3057 | EraseInstruction(Autorelease); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3058 | } |
| 3059 | } |
| 3060 | |
Michael Gottesman | 9c11815 | 2013-04-29 06:16:57 +0000 | [diff] [blame] | 3061 | #ifndef NDEBUG |
| 3062 | void |
| 3063 | ObjCARCOpt::GatherStatistics(Function &F, bool AfterOptimization) { |
| 3064 | llvm::Statistic &NumRetains = |
| 3065 | AfterOptimization? NumRetainsAfterOpt : NumRetainsBeforeOpt; |
| 3066 | llvm::Statistic &NumReleases = |
| 3067 | AfterOptimization? NumReleasesAfterOpt : NumReleasesBeforeOpt; |
| 3068 | |
| 3069 | for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) { |
| 3070 | Instruction *Inst = &*I++; |
| 3071 | switch (GetBasicInstructionClass(Inst)) { |
| 3072 | default: |
| 3073 | break; |
| 3074 | case IC_Retain: |
| 3075 | ++NumRetains; |
| 3076 | break; |
| 3077 | case IC_Release: |
| 3078 | ++NumReleases; |
| 3079 | break; |
| 3080 | } |
| 3081 | } |
| 3082 | } |
| 3083 | #endif |
| 3084 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3085 | bool ObjCARCOpt::doInitialization(Module &M) { |
| 3086 | if (!EnableARCOpts) |
| 3087 | return false; |
| 3088 | |
Dan Gohman | 670f937 | 2012-04-13 18:57:48 +0000 | [diff] [blame] | 3089 | // If nothing in the Module uses ARC, don't do anything. |
Dan Gohman | ceaac7c | 2011-06-20 23:20:43 +0000 | [diff] [blame] | 3090 | Run = ModuleHasARC(M); |
| 3091 | if (!Run) |
| 3092 | return false; |
| 3093 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3094 | // Identify the imprecise release metadata kind. |
| 3095 | ImpreciseReleaseMDKind = |
| 3096 | M.getContext().getMDKindID("clang.imprecise_release"); |
Dan Gohman | a7107f9 | 2011-10-17 22:53:25 +0000 | [diff] [blame] | 3097 | CopyOnEscapeMDKind = |
| 3098 | M.getContext().getMDKindID("clang.arc.copy_on_escape"); |
Dan Gohman | 0155f30 | 2012-02-17 18:59:53 +0000 | [diff] [blame] | 3099 | NoObjCARCExceptionsMDKind = |
| 3100 | M.getContext().getMDKindID("clang.arc.no_objc_arc_exceptions"); |
Michael Gottesman | 81b1d43 | 2013-03-26 00:42:04 +0000 | [diff] [blame] | 3101 | #ifdef ARC_ANNOTATIONS |
| 3102 | ARCAnnotationBottomUpMDKind = |
| 3103 | M.getContext().getMDKindID("llvm.arc.annotation.bottomup"); |
| 3104 | ARCAnnotationTopDownMDKind = |
| 3105 | M.getContext().getMDKindID("llvm.arc.annotation.topdown"); |
| 3106 | ARCAnnotationProvenanceSourceMDKind = |
| 3107 | M.getContext().getMDKindID("llvm.arc.annotation.provenancesource"); |
| 3108 | #endif // ARC_ANNOTATIONS |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3109 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3110 | // Intuitively, objc_retain and others are nocapture, however in practice |
| 3111 | // they are not, because they return their argument value. And objc_release |
Dan Gohman | dae3349 | 2012-04-27 18:56:31 +0000 | [diff] [blame] | 3112 | // calls finalizers which can have arbitrary side effects. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3113 | |
| 3114 | // These are initialized lazily. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3115 | AutoreleaseRVCallee = 0; |
| 3116 | ReleaseCallee = 0; |
| 3117 | RetainCallee = 0; |
Dan Gohman | 6320f52 | 2011-07-22 22:29:21 +0000 | [diff] [blame] | 3118 | RetainBlockCallee = 0; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3119 | AutoreleaseCallee = 0; |
| 3120 | |
| 3121 | return false; |
| 3122 | } |
| 3123 | |
| 3124 | bool ObjCARCOpt::runOnFunction(Function &F) { |
| 3125 | if (!EnableARCOpts) |
| 3126 | return false; |
| 3127 | |
Dan Gohman | ceaac7c | 2011-06-20 23:20:43 +0000 | [diff] [blame] | 3128 | // If nothing in the Module uses ARC, don't do anything. |
| 3129 | if (!Run) |
| 3130 | return false; |
| 3131 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3132 | Changed = false; |
| 3133 | |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 3134 | DEBUG(dbgs() << "<<< ObjCARCOpt: Visiting Function: " << F.getName() << " >>>" |
| 3135 | "\n"); |
Michael Gottesman | b24bdef | 2013-01-12 02:57:16 +0000 | [diff] [blame] | 3136 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3137 | PA.setAA(&getAnalysis<AliasAnalysis>()); |
| 3138 | |
Michael Gottesman | 9fc50b8 | 2013-05-13 18:29:07 +0000 | [diff] [blame] | 3139 | #ifndef NDEBUG |
| 3140 | if (AreStatisticsEnabled()) { |
| 3141 | GatherStatistics(F, false); |
| 3142 | } |
| 3143 | #endif |
| 3144 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3145 | // This pass performs several distinct transformations. As a compile-time aid |
| 3146 | // when compiling code that isn't ObjC, skip these if the relevant ObjC |
| 3147 | // library functions aren't declared. |
| 3148 | |
Michael Gottesman | cd5b027 | 2013-04-24 22:18:15 +0000 | [diff] [blame] | 3149 | // Preliminary optimizations. This also computes UsedInThisFunction. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3150 | OptimizeIndividualCalls(F); |
| 3151 | |
| 3152 | // Optimizations for weak pointers. |
| 3153 | if (UsedInThisFunction & ((1 << IC_LoadWeak) | |
| 3154 | (1 << IC_LoadWeakRetained) | |
| 3155 | (1 << IC_StoreWeak) | |
| 3156 | (1 << IC_InitWeak) | |
| 3157 | (1 << IC_CopyWeak) | |
| 3158 | (1 << IC_MoveWeak) | |
| 3159 | (1 << IC_DestroyWeak))) |
| 3160 | OptimizeWeakCalls(F); |
| 3161 | |
| 3162 | // Optimizations for retain+release pairs. |
| 3163 | if (UsedInThisFunction & ((1 << IC_Retain) | |
| 3164 | (1 << IC_RetainRV) | |
| 3165 | (1 << IC_RetainBlock))) |
| 3166 | if (UsedInThisFunction & (1 << IC_Release)) |
| 3167 | // Run OptimizeSequences until it either stops making changes or |
| 3168 | // no retain+release pair nesting is detected. |
| 3169 | while (OptimizeSequences(F)) {} |
| 3170 | |
| 3171 | // Optimizations if objc_autorelease is used. |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 3172 | if (UsedInThisFunction & ((1 << IC_Autorelease) | |
| 3173 | (1 << IC_AutoreleaseRV))) |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3174 | OptimizeReturns(F); |
| 3175 | |
Michael Gottesman | 9c11815 | 2013-04-29 06:16:57 +0000 | [diff] [blame] | 3176 | // Gather statistics after optimization. |
| 3177 | #ifndef NDEBUG |
| 3178 | if (AreStatisticsEnabled()) { |
| 3179 | GatherStatistics(F, true); |
| 3180 | } |
| 3181 | #endif |
| 3182 | |
Michael Gottesman | b24bdef | 2013-01-12 02:57:16 +0000 | [diff] [blame] | 3183 | DEBUG(dbgs() << "\n"); |
| 3184 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 3185 | return Changed; |
| 3186 | } |
| 3187 | |
| 3188 | void ObjCARCOpt::releaseMemory() { |
| 3189 | PA.clear(); |
| 3190 | } |
| 3191 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 3192 | /// @} |
| 3193 | /// |