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