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