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