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