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