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