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