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