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" |
Benjamin Kramer | 799003b | 2015-03-23 19:32:43 +0000 | [diff] [blame] | 29 | #include "BlotMapVector.h" |
Michael Gottesman | 278266f | 2013-01-29 04:20:52 +0000 | [diff] [blame] | 30 | #include "DependencyAnalysis.h" |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 31 | #include "ProvenanceAnalysis.h" |
Michael Gottesman | 68b91db | 2015-03-05 23:29:03 +0000 | [diff] [blame] | 32 | #include "PtrState.h" |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/DenseMap.h" |
Michael Gottesman | 5a91bbf | 2013-05-24 20:44:02 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/DenseSet.h" |
Michael Gottesman | fa0939f | 2013-01-28 04:12:07 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/STLExtras.h" |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/SmallPtrSet.h" |
Michael Gottesman | 278266f | 2013-01-29 04:20:52 +0000 | [diff] [blame] | 37 | #include "llvm/ADT/Statistic.h" |
Chandler Carruth | 0f79218 | 2015-08-20 08:06:03 +0000 | [diff] [blame] | 38 | #include "llvm/Analysis/ObjCARCAliasAnalysis.h" |
Chandler Carruth | 1305dc3 | 2014-03-04 11:45:46 +0000 | [diff] [blame] | 39 | #include "llvm/IR/CFG.h" |
Michael Gottesman | cd4de0f | 2013-03-26 00:42:09 +0000 | [diff] [blame] | 40 | #include "llvm/IR/IRBuilder.h" |
Michael Gottesman | 278266f | 2013-01-29 04:20:52 +0000 | [diff] [blame] | 41 | #include "llvm/IR/LLVMContext.h" |
Michael Gottesman | 13a5f1a | 2013-01-29 04:51:59 +0000 | [diff] [blame] | 42 | #include "llvm/Support/Debug.h" |
Timur Iskhodzhanov | 5d7ff00 | 2013-01-29 09:09:27 +0000 | [diff] [blame] | 43 | #include "llvm/Support/raw_ostream.h" |
Michael Gottesman | fa0939f | 2013-01-28 04:12:07 +0000 | [diff] [blame] | 44 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 45 | using namespace llvm; |
Michael Gottesman | 08904e3 | 2013-01-28 03:28:38 +0000 | [diff] [blame] | 46 | using namespace llvm::objcarc; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 47 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 48 | #define DEBUG_TYPE "objc-arc-opts" |
| 49 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 50 | /// \defgroup ARCUtilities Utility declarations/definitions specific to ARC. |
| 51 | /// @{ |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 52 | |
Michael Gottesman | e5ad66f | 2015-02-19 00:42:38 +0000 | [diff] [blame] | 53 | /// \brief This is similar to GetRCIdentityRoot but it stops as soon |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 54 | /// as it finds a value with multiple uses. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 55 | static const Value *FindSingleUseIdentifiedObject(const Value *Arg) { |
| 56 | if (Arg->hasOneUse()) { |
| 57 | if (const BitCastInst *BC = dyn_cast<BitCastInst>(Arg)) |
| 58 | return FindSingleUseIdentifiedObject(BC->getOperand(0)); |
| 59 | if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Arg)) |
| 60 | if (GEP->hasAllZeroIndices()) |
| 61 | return FindSingleUseIdentifiedObject(GEP->getPointerOperand()); |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 62 | if (IsForwarding(GetBasicARCInstKind(Arg))) |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 63 | return FindSingleUseIdentifiedObject( |
| 64 | cast<CallInst>(Arg)->getArgOperand(0)); |
| 65 | if (!IsObjCIdentifiedObject(Arg)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 66 | return nullptr; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 67 | return Arg; |
| 68 | } |
| 69 | |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 70 | // If we found an identifiable object but it has multiple uses, but they are |
| 71 | // 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] | 72 | if (IsObjCIdentifiedObject(Arg)) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 73 | for (const User *U : Arg->users()) |
Michael Gottesman | e5ad66f | 2015-02-19 00:42:38 +0000 | [diff] [blame] | 74 | if (!U->use_empty() || GetRCIdentityRoot(U) != Arg) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 75 | return nullptr; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 76 | |
| 77 | return Arg; |
| 78 | } |
| 79 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 80 | return nullptr; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Michael Gottesman | a76143ee | 2013-05-13 23:49:42 +0000 | [diff] [blame] | 83 | /// This is a wrapper around getUnderlyingObjCPtr along the lines of |
| 84 | /// GetUnderlyingObjects except that it returns early when it sees the first |
| 85 | /// alloca. |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 86 | static inline bool AreAnyUnderlyingObjectsAnAlloca(const Value *V, |
| 87 | const DataLayout &DL) { |
Michael Gottesman | a76143ee | 2013-05-13 23:49:42 +0000 | [diff] [blame] | 88 | SmallPtrSet<const Value *, 4> Visited; |
| 89 | SmallVector<const Value *, 4> Worklist; |
| 90 | Worklist.push_back(V); |
| 91 | do { |
| 92 | const Value *P = Worklist.pop_back_val(); |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 93 | P = GetUnderlyingObjCPtr(P, DL); |
Michael Gottesman | 0c8b562 | 2013-05-14 06:40:10 +0000 | [diff] [blame] | 94 | |
Michael Gottesman | a76143ee | 2013-05-13 23:49:42 +0000 | [diff] [blame] | 95 | if (isa<AllocaInst>(P)) |
| 96 | return true; |
Michael Gottesman | 0c8b562 | 2013-05-14 06:40:10 +0000 | [diff] [blame] | 97 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 98 | if (!Visited.insert(P).second) |
Michael Gottesman | a76143ee | 2013-05-13 23:49:42 +0000 | [diff] [blame] | 99 | continue; |
Michael Gottesman | 0c8b562 | 2013-05-14 06:40:10 +0000 | [diff] [blame] | 100 | |
Michael Gottesman | a76143ee | 2013-05-13 23:49:42 +0000 | [diff] [blame] | 101 | if (const SelectInst *SI = dyn_cast<const SelectInst>(P)) { |
| 102 | Worklist.push_back(SI->getTrueValue()); |
| 103 | Worklist.push_back(SI->getFalseValue()); |
| 104 | continue; |
| 105 | } |
Michael Gottesman | 0c8b562 | 2013-05-14 06:40:10 +0000 | [diff] [blame] | 106 | |
Michael Gottesman | a76143ee | 2013-05-13 23:49:42 +0000 | [diff] [blame] | 107 | if (const PHINode *PN = dyn_cast<const PHINode>(P)) { |
Pete Cooper | 833f34d | 2015-05-12 20:05:31 +0000 | [diff] [blame] | 108 | for (Value *IncValue : PN->incoming_values()) |
| 109 | Worklist.push_back(IncValue); |
Michael Gottesman | a76143ee | 2013-05-13 23:49:42 +0000 | [diff] [blame] | 110 | continue; |
| 111 | } |
| 112 | } while (!Worklist.empty()); |
| 113 | |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 118 | /// @} |
| 119 | /// |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 120 | /// \defgroup ARCOpt ARC Optimization. |
| 121 | /// @{ |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 122 | |
| 123 | // TODO: On code like this: |
| 124 | // |
| 125 | // objc_retain(%x) |
| 126 | // stuff_that_cannot_release() |
| 127 | // objc_autorelease(%x) |
| 128 | // stuff_that_cannot_release() |
| 129 | // objc_retain(%x) |
| 130 | // stuff_that_cannot_release() |
| 131 | // objc_autorelease(%x) |
| 132 | // |
| 133 | // The second retain and autorelease can be deleted. |
| 134 | |
| 135 | // TODO: It should be possible to delete |
| 136 | // objc_autoreleasePoolPush and objc_autoreleasePoolPop |
| 137 | // pairs if nothing is actually autoreleased between them. Also, autorelease |
| 138 | // calls followed by objc_autoreleasePoolPop calls (perhaps in ObjC++ code |
| 139 | // after inlining) can be turned into plain release calls. |
| 140 | |
| 141 | // TODO: Critical-edge splitting. If the optimial insertion point is |
| 142 | // a critical edge, the current algorithm has to fail, because it doesn't |
| 143 | // know how to split edges. It should be possible to make the optimizer |
| 144 | // think in terms of edges, rather than blocks, and then split critical |
| 145 | // edges on demand. |
| 146 | |
| 147 | // TODO: OptimizeSequences could generalized to be Interprocedural. |
| 148 | |
| 149 | // TODO: Recognize that a bunch of other objc runtime calls have |
| 150 | // non-escaping arguments and non-releasing arguments, and may be |
| 151 | // non-autoreleasing. |
| 152 | |
| 153 | // TODO: Sink autorelease calls as far as possible. Unfortunately we |
| 154 | // usually can't sink them past other calls, which would be the main |
| 155 | // case where it would be useful. |
| 156 | |
Dan Gohman | b389401 | 2011-08-19 00:26:36 +0000 | [diff] [blame] | 157 | // TODO: The pointer returned from objc_loadWeakRetained is retained. |
| 158 | |
| 159 | // TODO: Delete release+retain pairs (rare). |
Dan Gohman | ceaac7c | 2011-06-20 23:20:43 +0000 | [diff] [blame] | 160 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 161 | STATISTIC(NumNoops, "Number of no-op objc calls eliminated"); |
| 162 | STATISTIC(NumPartialNoops, "Number of partially no-op objc calls eliminated"); |
| 163 | STATISTIC(NumAutoreleases,"Number of autoreleases converted to releases"); |
| 164 | STATISTIC(NumRets, "Number of return value forwarding " |
Michael Gottesman | b4e7f4d | 2013-05-15 17:43:03 +0000 | [diff] [blame] | 165 | "retain+autoreleases eliminated"); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 166 | STATISTIC(NumRRs, "Number of retain+release paths eliminated"); |
| 167 | STATISTIC(NumPeeps, "Number of calls peephole-optimized"); |
Matt Beaumont-Gay | e55d949 | 2013-05-13 21:10:49 +0000 | [diff] [blame] | 168 | #ifndef NDEBUG |
Michael Gottesman | 9c11815 | 2013-04-29 06:16:57 +0000 | [diff] [blame] | 169 | STATISTIC(NumRetainsBeforeOpt, |
Michael Gottesman | b4e7f4d | 2013-05-15 17:43:03 +0000 | [diff] [blame] | 170 | "Number of retains before optimization"); |
Michael Gottesman | 9c11815 | 2013-04-29 06:16:57 +0000 | [diff] [blame] | 171 | STATISTIC(NumReleasesBeforeOpt, |
Michael Gottesman | b4e7f4d | 2013-05-15 17:43:03 +0000 | [diff] [blame] | 172 | "Number of releases before optimization"); |
Michael Gottesman | 9c11815 | 2013-04-29 06:16:57 +0000 | [diff] [blame] | 173 | STATISTIC(NumRetainsAfterOpt, |
Michael Gottesman | b4e7f4d | 2013-05-15 17:43:03 +0000 | [diff] [blame] | 174 | "Number of retains after optimization"); |
Michael Gottesman | 9c11815 | 2013-04-29 06:16:57 +0000 | [diff] [blame] | 175 | STATISTIC(NumReleasesAfterOpt, |
Michael Gottesman | b4e7f4d | 2013-05-15 17:43:03 +0000 | [diff] [blame] | 176 | "Number of releases after optimization"); |
Michael Gottesman | 03cf3c8 | 2013-04-29 07:29:08 +0000 | [diff] [blame] | 177 | #endif |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 178 | |
| 179 | namespace { |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 180 | /// \brief Per-BasicBlock state. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 181 | class BBState { |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 182 | /// The number of unique control paths from the entry which can reach this |
| 183 | /// block. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 184 | unsigned TopDownPathCount; |
| 185 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 186 | /// The number of unique control paths to exits from this block. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 187 | unsigned BottomUpPathCount; |
| 188 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 189 | /// The top-down traversal uses this to record information known about a |
| 190 | /// pointer at the bottom of each block. |
Michael Gottesman | feb138e | 2015-03-06 00:34:36 +0000 | [diff] [blame] | 191 | BlotMapVector<const Value *, TopDownPtrState> PerPtrTopDown; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 192 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 193 | /// The bottom-up traversal uses this to record information known about a |
| 194 | /// pointer at the top of each block. |
Michael Gottesman | feb138e | 2015-03-06 00:34:36 +0000 | [diff] [blame] | 195 | BlotMapVector<const Value *, BottomUpPtrState> PerPtrBottomUp; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 196 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 197 | /// Effective predecessors of the current block ignoring ignorable edges and |
| 198 | /// ignored backedges. |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 199 | SmallVector<BasicBlock *, 2> Preds; |
Michael Gottesman | feb138e | 2015-03-06 00:34:36 +0000 | [diff] [blame] | 200 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 201 | /// Effective successors of the current block ignoring ignorable edges and |
| 202 | /// ignored backedges. |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 203 | SmallVector<BasicBlock *, 2> Succs; |
| 204 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 205 | public: |
Michael Gottesman | d6ce6cb | 2013-08-09 23:22:27 +0000 | [diff] [blame] | 206 | static const unsigned OverflowOccurredValue; |
| 207 | |
| 208 | BBState() : TopDownPathCount(0), BottomUpPathCount(0) { } |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 209 | |
Michael Gottesman | feb138e | 2015-03-06 00:34:36 +0000 | [diff] [blame] | 210 | typedef decltype(PerPtrTopDown)::iterator top_down_ptr_iterator; |
| 211 | typedef decltype(PerPtrTopDown)::const_iterator const_top_down_ptr_iterator; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 212 | |
Michael Gottesman | feb138e | 2015-03-06 00:34:36 +0000 | [diff] [blame] | 213 | top_down_ptr_iterator top_down_ptr_begin() { return PerPtrTopDown.begin(); } |
| 214 | top_down_ptr_iterator top_down_ptr_end() { return PerPtrTopDown.end(); } |
| 215 | const_top_down_ptr_iterator top_down_ptr_begin() const { |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 216 | return PerPtrTopDown.begin(); |
| 217 | } |
Michael Gottesman | feb138e | 2015-03-06 00:34:36 +0000 | [diff] [blame] | 218 | const_top_down_ptr_iterator top_down_ptr_end() const { |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 219 | return PerPtrTopDown.end(); |
| 220 | } |
Michael Gottesman | c01ab51 | 2015-03-16 07:02:39 +0000 | [diff] [blame] | 221 | bool hasTopDownPtrs() const { |
| 222 | return !PerPtrTopDown.empty(); |
| 223 | } |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 224 | |
Michael Gottesman | feb138e | 2015-03-06 00:34:36 +0000 | [diff] [blame] | 225 | typedef decltype(PerPtrBottomUp)::iterator bottom_up_ptr_iterator; |
| 226 | typedef decltype( |
| 227 | PerPtrBottomUp)::const_iterator const_bottom_up_ptr_iterator; |
| 228 | |
| 229 | bottom_up_ptr_iterator bottom_up_ptr_begin() { |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 230 | return PerPtrBottomUp.begin(); |
| 231 | } |
Michael Gottesman | feb138e | 2015-03-06 00:34:36 +0000 | [diff] [blame] | 232 | bottom_up_ptr_iterator bottom_up_ptr_end() { return PerPtrBottomUp.end(); } |
| 233 | const_bottom_up_ptr_iterator bottom_up_ptr_begin() const { |
| 234 | return PerPtrBottomUp.begin(); |
| 235 | } |
| 236 | const_bottom_up_ptr_iterator bottom_up_ptr_end() const { |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 237 | return PerPtrBottomUp.end(); |
| 238 | } |
Michael Gottesman | c01ab51 | 2015-03-16 07:02:39 +0000 | [diff] [blame] | 239 | bool hasBottomUpPtrs() const { |
| 240 | return !PerPtrBottomUp.empty(); |
| 241 | } |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 242 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 243 | /// Mark this block as being an entry block, which has one path from the |
| 244 | /// entry by definition. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 245 | void SetAsEntry() { TopDownPathCount = 1; } |
| 246 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 247 | /// Mark this block as being an exit block, which has one path to an exit by |
| 248 | /// definition. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 249 | void SetAsExit() { BottomUpPathCount = 1; } |
| 250 | |
Michael Gottesman | 993fbf7 | 2013-05-13 19:40:39 +0000 | [diff] [blame] | 251 | /// Attempt to find the PtrState object describing the top down state for |
| 252 | /// pointer Arg. Return a new initialized PtrState describing the top down |
| 253 | /// state for Arg if we do not find one. |
Michael Gottesman | feb138e | 2015-03-06 00:34:36 +0000 | [diff] [blame] | 254 | TopDownPtrState &getPtrTopDownState(const Value *Arg) { |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 255 | return PerPtrTopDown[Arg]; |
| 256 | } |
| 257 | |
Michael Gottesman | 993fbf7 | 2013-05-13 19:40:39 +0000 | [diff] [blame] | 258 | /// Attempt to find the PtrState object describing the bottom up state for |
| 259 | /// pointer Arg. Return a new initialized PtrState describing the bottom up |
| 260 | /// state for Arg if we do not find one. |
Michael Gottesman | feb138e | 2015-03-06 00:34:36 +0000 | [diff] [blame] | 261 | BottomUpPtrState &getPtrBottomUpState(const Value *Arg) { |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 262 | return PerPtrBottomUp[Arg]; |
| 263 | } |
| 264 | |
Michael Gottesman | a76143ee | 2013-05-13 23:49:42 +0000 | [diff] [blame] | 265 | /// Attempt to find the PtrState object describing the bottom up state for |
| 266 | /// pointer Arg. |
Michael Gottesman | feb138e | 2015-03-06 00:34:36 +0000 | [diff] [blame] | 267 | bottom_up_ptr_iterator findPtrBottomUpState(const Value *Arg) { |
Michael Gottesman | a76143ee | 2013-05-13 23:49:42 +0000 | [diff] [blame] | 268 | return PerPtrBottomUp.find(Arg); |
| 269 | } |
| 270 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 271 | void clearBottomUpPointers() { |
Evan Cheng | e4df6a2 | 2011-08-04 18:40:26 +0000 | [diff] [blame] | 272 | PerPtrBottomUp.clear(); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | void clearTopDownPointers() { |
| 276 | PerPtrTopDown.clear(); |
| 277 | } |
| 278 | |
| 279 | void InitFromPred(const BBState &Other); |
| 280 | void InitFromSucc(const BBState &Other); |
| 281 | void MergePred(const BBState &Other); |
| 282 | void MergeSucc(const BBState &Other); |
| 283 | |
Michael Gottesman | 9e7261c | 2013-06-07 06:16:49 +0000 | [diff] [blame] | 284 | /// 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] | 285 | /// which pass through this block. This is only valid after both the |
| 286 | /// top-down and bottom-up traversals are complete. |
Michael Gottesman | 9e7261c | 2013-06-07 06:16:49 +0000 | [diff] [blame] | 287 | /// |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 288 | /// Returns true if overflow occurred. Returns false if overflow did not |
Michael Gottesman | 9e7261c | 2013-06-07 06:16:49 +0000 | [diff] [blame] | 289 | /// occur. |
| 290 | bool GetAllPathCountWithOverflow(unsigned &PathCount) const { |
Michael Gottesman | d6ce6cb | 2013-08-09 23:22:27 +0000 | [diff] [blame] | 291 | if (TopDownPathCount == OverflowOccurredValue || |
| 292 | BottomUpPathCount == OverflowOccurredValue) |
| 293 | return true; |
Michael Gottesman | 9e7261c | 2013-06-07 06:16:49 +0000 | [diff] [blame] | 294 | unsigned long long Product = |
| 295 | (unsigned long long)TopDownPathCount*BottomUpPathCount; |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 296 | // 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] | 297 | // the lower bits of Product are all set. |
| 298 | return (Product >> 32) || |
| 299 | ((PathCount = Product) == OverflowOccurredValue); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 300 | } |
Dan Gohman | 1213027 | 2011-08-12 00:26:31 +0000 | [diff] [blame] | 301 | |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 302 | // Specialized CFG utilities. |
Dan Gohman | dae3349 | 2012-04-27 18:56:31 +0000 | [diff] [blame] | 303 | typedef SmallVectorImpl<BasicBlock *>::const_iterator edge_iterator; |
Michael Gottesman | 0fecf98 | 2013-08-07 23:56:34 +0000 | [diff] [blame] | 304 | edge_iterator pred_begin() const { return Preds.begin(); } |
| 305 | edge_iterator pred_end() const { return Preds.end(); } |
| 306 | edge_iterator succ_begin() const { return Succs.begin(); } |
| 307 | edge_iterator succ_end() const { return Succs.end(); } |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 308 | |
| 309 | void addSucc(BasicBlock *Succ) { Succs.push_back(Succ); } |
| 310 | void addPred(BasicBlock *Pred) { Preds.push_back(Pred); } |
| 311 | |
| 312 | bool isExit() const { return Succs.empty(); } |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 313 | }; |
Michael Gottesman | d6ce6cb | 2013-08-09 23:22:27 +0000 | [diff] [blame] | 314 | |
| 315 | const unsigned BBState::OverflowOccurredValue = 0xffffffff; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 316 | } |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 317 | |
Michael Gottesman | c01ab51 | 2015-03-16 07:02:39 +0000 | [diff] [blame] | 318 | namespace llvm { |
Michael Gottesman | d63436f | 2015-03-16 08:00:27 +0000 | [diff] [blame] | 319 | raw_ostream &operator<<(raw_ostream &OS, |
| 320 | BBState &BBState) LLVM_ATTRIBUTE_UNUSED; |
Michael Gottesman | c01ab51 | 2015-03-16 07:02:39 +0000 | [diff] [blame] | 321 | } |
| 322 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 323 | void BBState::InitFromPred(const BBState &Other) { |
| 324 | PerPtrTopDown = Other.PerPtrTopDown; |
| 325 | TopDownPathCount = Other.TopDownPathCount; |
| 326 | } |
| 327 | |
| 328 | void BBState::InitFromSucc(const BBState &Other) { |
| 329 | PerPtrBottomUp = Other.PerPtrBottomUp; |
| 330 | BottomUpPathCount = Other.BottomUpPathCount; |
| 331 | } |
| 332 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 333 | /// The top-down traversal uses this to merge information about predecessors to |
| 334 | /// form the initial state for a new block. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 335 | void BBState::MergePred(const BBState &Other) { |
Michael Gottesman | d6ce6cb | 2013-08-09 23:22:27 +0000 | [diff] [blame] | 336 | if (TopDownPathCount == OverflowOccurredValue) |
| 337 | return; |
| 338 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 339 | // Other.TopDownPathCount can be 0, in which case it is either dead or a |
| 340 | // loop backedge. Loop backedges are special. |
| 341 | TopDownPathCount += Other.TopDownPathCount; |
| 342 | |
Michael Gottesman | d6ce6cb | 2013-08-09 23:22:27 +0000 | [diff] [blame] | 343 | // In order to be consistent, we clear the top down pointers when by adding |
| 344 | // TopDownPathCount becomes OverflowOccurredValue even though "true" overflow |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 345 | // has not occurred. |
Michael Gottesman | d6ce6cb | 2013-08-09 23:22:27 +0000 | [diff] [blame] | 346 | if (TopDownPathCount == OverflowOccurredValue) { |
| 347 | clearTopDownPointers(); |
| 348 | return; |
| 349 | } |
| 350 | |
Michael Gottesman | 4385edf | 2013-01-14 01:47:53 +0000 | [diff] [blame] | 351 | // Check for overflow. If we have overflow, fall back to conservative |
| 352 | // behavior. |
Dan Gohman | 7c84dad | 2012-09-12 20:45:17 +0000 | [diff] [blame] | 353 | if (TopDownPathCount < Other.TopDownPathCount) { |
Michael Gottesman | d6ce6cb | 2013-08-09 23:22:27 +0000 | [diff] [blame] | 354 | TopDownPathCount = OverflowOccurredValue; |
Dan Gohman | 7c84dad | 2012-09-12 20:45:17 +0000 | [diff] [blame] | 355 | clearTopDownPointers(); |
| 356 | return; |
| 357 | } |
| 358 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 359 | // For each entry in the other set, if our set has an entry with the same key, |
| 360 | // merge the entries. Otherwise, copy the entry and merge it with an empty |
| 361 | // entry. |
Michael Gottesman | a9fc016 | 2015-03-05 23:29:06 +0000 | [diff] [blame] | 362 | for (auto MI = Other.top_down_ptr_begin(), ME = Other.top_down_ptr_end(); |
| 363 | MI != ME; ++MI) { |
Michael Gottesman | feb138e | 2015-03-06 00:34:36 +0000 | [diff] [blame] | 364 | auto Pair = PerPtrTopDown.insert(*MI); |
| 365 | Pair.first->second.Merge(Pair.second ? TopDownPtrState() : MI->second, |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 366 | /*TopDown=*/true); |
| 367 | } |
| 368 | |
Dan Gohman | 7e315fc3 | 2011-08-11 21:06:32 +0000 | [diff] [blame] | 369 | // 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] | 370 | // same key, force it to merge with an empty entry. |
Michael Gottesman | a9fc016 | 2015-03-05 23:29:06 +0000 | [diff] [blame] | 371 | for (auto MI = top_down_ptr_begin(), ME = top_down_ptr_end(); MI != ME; ++MI) |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 372 | if (Other.PerPtrTopDown.find(MI->first) == Other.PerPtrTopDown.end()) |
Michael Gottesman | feb138e | 2015-03-06 00:34:36 +0000 | [diff] [blame] | 373 | MI->second.Merge(TopDownPtrState(), /*TopDown=*/true); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 374 | } |
| 375 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 376 | /// The bottom-up traversal uses this to merge information about successors to |
| 377 | /// form the initial state for a new block. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 378 | void BBState::MergeSucc(const BBState &Other) { |
Michael Gottesman | d6ce6cb | 2013-08-09 23:22:27 +0000 | [diff] [blame] | 379 | if (BottomUpPathCount == OverflowOccurredValue) |
| 380 | return; |
| 381 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 382 | // Other.BottomUpPathCount can be 0, in which case it is either dead or a |
| 383 | // loop backedge. Loop backedges are special. |
| 384 | BottomUpPathCount += Other.BottomUpPathCount; |
| 385 | |
Michael Gottesman | d6ce6cb | 2013-08-09 23:22:27 +0000 | [diff] [blame] | 386 | // In order to be consistent, we clear the top down pointers when by adding |
| 387 | // BottomUpPathCount becomes OverflowOccurredValue even though "true" overflow |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 388 | // has not occurred. |
Michael Gottesman | d6ce6cb | 2013-08-09 23:22:27 +0000 | [diff] [blame] | 389 | if (BottomUpPathCount == OverflowOccurredValue) { |
| 390 | clearBottomUpPointers(); |
| 391 | return; |
| 392 | } |
| 393 | |
Michael Gottesman | 4385edf | 2013-01-14 01:47:53 +0000 | [diff] [blame] | 394 | // Check for overflow. If we have overflow, fall back to conservative |
| 395 | // behavior. |
Dan Gohman | 7c84dad | 2012-09-12 20:45:17 +0000 | [diff] [blame] | 396 | if (BottomUpPathCount < Other.BottomUpPathCount) { |
Michael Gottesman | d6ce6cb | 2013-08-09 23:22:27 +0000 | [diff] [blame] | 397 | BottomUpPathCount = OverflowOccurredValue; |
Dan Gohman | 7c84dad | 2012-09-12 20:45:17 +0000 | [diff] [blame] | 398 | clearBottomUpPointers(); |
| 399 | return; |
| 400 | } |
| 401 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 402 | // For each entry in the other set, if our set has an entry with the |
| 403 | // same key, merge the entries. Otherwise, copy the entry and merge |
| 404 | // it with an empty entry. |
Michael Gottesman | a9fc016 | 2015-03-05 23:29:06 +0000 | [diff] [blame] | 405 | for (auto MI = Other.bottom_up_ptr_begin(), ME = Other.bottom_up_ptr_end(); |
| 406 | MI != ME; ++MI) { |
Michael Gottesman | feb138e | 2015-03-06 00:34:36 +0000 | [diff] [blame] | 407 | auto Pair = PerPtrBottomUp.insert(*MI); |
| 408 | Pair.first->second.Merge(Pair.second ? BottomUpPtrState() : MI->second, |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 409 | /*TopDown=*/false); |
| 410 | } |
| 411 | |
Dan Gohman | 7e315fc3 | 2011-08-11 21:06:32 +0000 | [diff] [blame] | 412 | // 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] | 413 | // with the same key, force it to merge with an empty entry. |
Michael Gottesman | a9fc016 | 2015-03-05 23:29:06 +0000 | [diff] [blame] | 414 | for (auto MI = bottom_up_ptr_begin(), ME = bottom_up_ptr_end(); MI != ME; |
| 415 | ++MI) |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 416 | if (Other.PerPtrBottomUp.find(MI->first) == Other.PerPtrBottomUp.end()) |
Michael Gottesman | feb138e | 2015-03-06 00:34:36 +0000 | [diff] [blame] | 417 | MI->second.Merge(BottomUpPtrState(), /*TopDown=*/false); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Michael Gottesman | c01ab51 | 2015-03-16 07:02:39 +0000 | [diff] [blame] | 420 | raw_ostream &llvm::operator<<(raw_ostream &OS, BBState &BBInfo) { |
| 421 | // Dump the pointers we are tracking. |
| 422 | OS << " TopDown State:\n"; |
| 423 | if (!BBInfo.hasTopDownPtrs()) { |
| 424 | DEBUG(llvm::dbgs() << " NONE!\n"); |
| 425 | } else { |
| 426 | for (auto I = BBInfo.top_down_ptr_begin(), E = BBInfo.top_down_ptr_end(); |
| 427 | I != E; ++I) { |
| 428 | const PtrState &P = I->second; |
| 429 | OS << " Ptr: " << *I->first |
| 430 | << "\n KnownSafe: " << (P.IsKnownSafe()?"true":"false") |
| 431 | << "\n ImpreciseRelease: " |
| 432 | << (P.IsTrackingImpreciseReleases()?"true":"false") << "\n" |
| 433 | << " HasCFGHazards: " |
| 434 | << (P.IsCFGHazardAfflicted()?"true":"false") << "\n" |
| 435 | << " KnownPositive: " |
| 436 | << (P.HasKnownPositiveRefCount()?"true":"false") << "\n" |
| 437 | << " Seq: " |
| 438 | << P.GetSeq() << "\n"; |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | OS << " BottomUp State:\n"; |
| 443 | if (!BBInfo.hasBottomUpPtrs()) { |
| 444 | DEBUG(llvm::dbgs() << " NONE!\n"); |
| 445 | } else { |
| 446 | for (auto I = BBInfo.bottom_up_ptr_begin(), E = BBInfo.bottom_up_ptr_end(); |
| 447 | I != E; ++I) { |
| 448 | const PtrState &P = I->second; |
| 449 | OS << " Ptr: " << *I->first |
| 450 | << "\n KnownSafe: " << (P.IsKnownSafe()?"true":"false") |
| 451 | << "\n ImpreciseRelease: " |
| 452 | << (P.IsTrackingImpreciseReleases()?"true":"false") << "\n" |
| 453 | << " HasCFGHazards: " |
| 454 | << (P.IsCFGHazardAfflicted()?"true":"false") << "\n" |
| 455 | << " KnownPositive: " |
| 456 | << (P.HasKnownPositiveRefCount()?"true":"false") << "\n" |
| 457 | << " Seq: " |
| 458 | << P.GetSeq() << "\n"; |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | return OS; |
| 463 | } |
| 464 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 465 | namespace { |
Michael Gottesman | 41c0100 | 2015-03-06 00:34:33 +0000 | [diff] [blame] | 466 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 467 | /// \brief The main ARC optimization pass. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 468 | class ObjCARCOpt : public FunctionPass { |
| 469 | bool Changed; |
| 470 | ProvenanceAnalysis PA; |
Michael Gottesman | 41c0100 | 2015-03-06 00:34:33 +0000 | [diff] [blame] | 471 | |
| 472 | /// A cache of references to runtime entry point constants. |
Michael Gottesman | 14acfac | 2013-07-06 01:39:23 +0000 | [diff] [blame] | 473 | ARCRuntimeEntryPoints EP; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 474 | |
Michael Gottesman | 41c0100 | 2015-03-06 00:34:33 +0000 | [diff] [blame] | 475 | /// A cache of MDKinds that can be passed into other functions to propagate |
| 476 | /// MDKind identifiers. |
| 477 | ARCMDKindCache MDKindCache; |
| 478 | |
Michael Gottesman | 5a91bbf | 2013-05-24 20:44:02 +0000 | [diff] [blame] | 479 | // This is used to track if a pointer is stored into an alloca. |
| 480 | DenseSet<const Value *> MultiOwnersSet; |
| 481 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 482 | /// A flag indicating whether this optimization pass should run. |
Dan Gohman | ceaac7c | 2011-06-20 23:20:43 +0000 | [diff] [blame] | 483 | bool Run; |
| 484 | |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 485 | /// Flags which determine whether each of the interesting runtime functions |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 486 | /// is in fact used in the current function. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 487 | unsigned UsedInThisFunction; |
| 488 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 489 | bool OptimizeRetainRVCall(Function &F, Instruction *RetainRV); |
Michael Gottesman | 556ff61 | 2013-01-12 01:25:19 +0000 | [diff] [blame] | 490 | void OptimizeAutoreleaseRVCall(Function &F, Instruction *AutoreleaseRV, |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 491 | ARCInstKind &Class); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 492 | void OptimizeIndividualCalls(Function &F); |
| 493 | |
| 494 | void CheckForCFGHazards(const BasicBlock *BB, |
| 495 | DenseMap<const BasicBlock *, BBState> &BBStates, |
| 496 | BBState &MyStates) const; |
Michael Gottesman | 0be6920 | 2015-03-05 23:28:58 +0000 | [diff] [blame] | 497 | bool VisitInstructionBottomUp(Instruction *Inst, BasicBlock *BB, |
| 498 | BlotMapVector<Value *, RRInfo> &Retains, |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 499 | BBState &MyStates); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 500 | bool VisitBottomUp(BasicBlock *BB, |
| 501 | DenseMap<const BasicBlock *, BBState> &BBStates, |
Michael Gottesman | 0be6920 | 2015-03-05 23:28:58 +0000 | [diff] [blame] | 502 | BlotMapVector<Value *, RRInfo> &Retains); |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 503 | bool VisitInstructionTopDown(Instruction *Inst, |
| 504 | DenseMap<Value *, RRInfo> &Releases, |
| 505 | BBState &MyStates); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 506 | bool VisitTopDown(BasicBlock *BB, |
| 507 | DenseMap<const BasicBlock *, BBState> &BBStates, |
| 508 | DenseMap<Value *, RRInfo> &Releases); |
Michael Gottesman | 0be6920 | 2015-03-05 23:28:58 +0000 | [diff] [blame] | 509 | bool Visit(Function &F, DenseMap<const BasicBlock *, BBState> &BBStates, |
| 510 | BlotMapVector<Value *, RRInfo> &Retains, |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 511 | DenseMap<Value *, RRInfo> &Releases); |
| 512 | |
| 513 | void MoveCalls(Value *Arg, RRInfo &RetainsToMove, RRInfo &ReleasesToMove, |
Michael Gottesman | 0be6920 | 2015-03-05 23:28:58 +0000 | [diff] [blame] | 514 | BlotMapVector<Value *, RRInfo> &Retains, |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 515 | DenseMap<Value *, RRInfo> &Releases, |
Michael Gottesman | 0be6920 | 2015-03-05 23:28:58 +0000 | [diff] [blame] | 516 | SmallVectorImpl<Instruction *> &DeadInsts, Module *M); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 517 | |
Michael Gottesman | 6779217 | 2015-03-16 07:02:30 +0000 | [diff] [blame] | 518 | bool |
| 519 | PairUpRetainsAndReleases(DenseMap<const BasicBlock *, BBState> &BBStates, |
| 520 | BlotMapVector<Value *, RRInfo> &Retains, |
| 521 | DenseMap<Value *, RRInfo> &Releases, Module *M, |
| 522 | SmallVectorImpl<Instruction *> &NewRetains, |
| 523 | SmallVectorImpl<Instruction *> &NewReleases, |
| 524 | SmallVectorImpl<Instruction *> &DeadInsts, |
| 525 | RRInfo &RetainsToMove, RRInfo &ReleasesToMove, |
| 526 | Value *Arg, bool KnownSafe, |
| 527 | bool &AnyPairsCompletelyEliminated); |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 528 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 529 | bool PerformCodePlacement(DenseMap<const BasicBlock *, BBState> &BBStates, |
Michael Gottesman | 0be6920 | 2015-03-05 23:28:58 +0000 | [diff] [blame] | 530 | BlotMapVector<Value *, RRInfo> &Retains, |
| 531 | DenseMap<Value *, RRInfo> &Releases, Module *M); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 532 | |
| 533 | void OptimizeWeakCalls(Function &F); |
| 534 | |
| 535 | bool OptimizeSequences(Function &F); |
| 536 | |
| 537 | void OptimizeReturns(Function &F); |
| 538 | |
Michael Gottesman | 9c11815 | 2013-04-29 06:16:57 +0000 | [diff] [blame] | 539 | #ifndef NDEBUG |
| 540 | void GatherStatistics(Function &F, bool AfterOptimization = false); |
| 541 | #endif |
| 542 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 543 | void getAnalysisUsage(AnalysisUsage &AU) const override; |
| 544 | bool doInitialization(Module &M) override; |
| 545 | bool runOnFunction(Function &F) override; |
| 546 | void releaseMemory() override; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 547 | |
| 548 | public: |
| 549 | static char ID; |
| 550 | ObjCARCOpt() : FunctionPass(ID) { |
| 551 | initializeObjCARCOptPass(*PassRegistry::getPassRegistry()); |
| 552 | } |
| 553 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 554 | } |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 555 | |
| 556 | char ObjCARCOpt::ID = 0; |
| 557 | INITIALIZE_PASS_BEGIN(ObjCARCOpt, |
| 558 | "objc-arc", "ObjC ARC optimization", false, false) |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 559 | INITIALIZE_PASS_DEPENDENCY(ObjCARCAAWrapperPass) |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 560 | INITIALIZE_PASS_END(ObjCARCOpt, |
| 561 | "objc-arc", "ObjC ARC optimization", false, false) |
| 562 | |
| 563 | Pass *llvm::createObjCARCOptPass() { |
| 564 | return new ObjCARCOpt(); |
| 565 | } |
| 566 | |
| 567 | void ObjCARCOpt::getAnalysisUsage(AnalysisUsage &AU) const { |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 568 | AU.addRequired<ObjCARCAAWrapperPass>(); |
| 569 | AU.addRequired<AAResultsWrapperPass>(); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 570 | // ARC optimization doesn't currently split critical edges. |
| 571 | AU.setPreservesCFG(); |
| 572 | } |
| 573 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 574 | /// Turn objc_retainAutoreleasedReturnValue into objc_retain if the operand is |
| 575 | /// not a return value. Or, if it can be paired with an |
| 576 | /// objc_autoreleaseReturnValue, delete the pair and return true. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 577 | bool |
| 578 | ObjCARCOpt::OptimizeRetainRVCall(Function &F, Instruction *RetainRV) { |
Dan Gohman | e3ed2b0 | 2012-03-23 18:09:00 +0000 | [diff] [blame] | 579 | // Check for the argument being from an immediately preceding call or invoke. |
Michael Gottesman | e5ad66f | 2015-02-19 00:42:38 +0000 | [diff] [blame] | 580 | const Value *Arg = GetArgRCIdentityRoot(RetainRV); |
Dan Gohman | dae3349 | 2012-04-27 18:56:31 +0000 | [diff] [blame] | 581 | ImmutableCallSite CS(Arg); |
| 582 | if (const Instruction *Call = CS.getInstruction()) { |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 583 | if (Call->getParent() == RetainRV->getParent()) { |
Duncan P. N. Exon Smith | 1e59a66 | 2015-10-19 23:20:14 +0000 | [diff] [blame] | 584 | BasicBlock::const_iterator I(Call); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 585 | ++I; |
Duncan P. N. Exon Smith | 1e59a66 | 2015-10-19 23:20:14 +0000 | [diff] [blame] | 586 | while (IsNoopInstruction(&*I)) |
| 587 | ++I; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 588 | if (&*I == RetainRV) |
| 589 | return false; |
Dan Gohman | dae3349 | 2012-04-27 18:56:31 +0000 | [diff] [blame] | 590 | } else if (const InvokeInst *II = dyn_cast<InvokeInst>(Call)) { |
Dan Gohman | e3ed2b0 | 2012-03-23 18:09:00 +0000 | [diff] [blame] | 591 | BasicBlock *RetainRVParent = RetainRV->getParent(); |
| 592 | if (II->getNormalDest() == RetainRVParent) { |
Dan Gohman | dae3349 | 2012-04-27 18:56:31 +0000 | [diff] [blame] | 593 | BasicBlock::const_iterator I = RetainRVParent->begin(); |
Duncan P. N. Exon Smith | 1e59a66 | 2015-10-19 23:20:14 +0000 | [diff] [blame] | 594 | while (IsNoopInstruction(&*I)) |
| 595 | ++I; |
Dan Gohman | e3ed2b0 | 2012-03-23 18:09:00 +0000 | [diff] [blame] | 596 | if (&*I == RetainRV) |
| 597 | return false; |
| 598 | } |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 599 | } |
Dan Gohman | e3ed2b0 | 2012-03-23 18:09:00 +0000 | [diff] [blame] | 600 | } |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 601 | |
| 602 | // Check for being preceded by an objc_autoreleaseReturnValue on the same |
| 603 | // pointer. In this case, we can delete the pair. |
Duncan P. N. Exon Smith | 1e59a66 | 2015-10-19 23:20:14 +0000 | [diff] [blame] | 604 | BasicBlock::iterator I = RetainRV->getIterator(), |
| 605 | Begin = RetainRV->getParent()->begin(); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 606 | if (I != Begin) { |
Duncan P. N. Exon Smith | 1e59a66 | 2015-10-19 23:20:14 +0000 | [diff] [blame] | 607 | do |
| 608 | --I; |
| 609 | while (I != Begin && IsNoopInstruction(&*I)); |
| 610 | if (GetBasicARCInstKind(&*I) == ARCInstKind::AutoreleaseRV && |
| 611 | GetArgRCIdentityRoot(&*I) == Arg) { |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 612 | Changed = true; |
| 613 | ++NumPeeps; |
Michael Gottesman | 10426b5 | 2013-01-07 21:26:07 +0000 | [diff] [blame] | 614 | |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 615 | DEBUG(dbgs() << "Erasing autoreleaseRV,retainRV pair: " << *I << "\n" |
| 616 | << "Erasing " << *RetainRV << "\n"); |
Michael Gottesman | 10426b5 | 2013-01-07 21:26:07 +0000 | [diff] [blame] | 617 | |
Duncan P. N. Exon Smith | 1e59a66 | 2015-10-19 23:20:14 +0000 | [diff] [blame] | 618 | EraseInstruction(&*I); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 619 | EraseInstruction(RetainRV); |
| 620 | return true; |
| 621 | } |
| 622 | } |
| 623 | |
| 624 | // Turn it to a plain objc_retain. |
| 625 | Changed = true; |
| 626 | ++NumPeeps; |
Michael Gottesman | 10426b5 | 2013-01-07 21:26:07 +0000 | [diff] [blame] | 627 | |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 628 | DEBUG(dbgs() << "Transforming objc_retainAutoreleasedReturnValue => " |
Michael Gottesman | def07bb | 2013-01-05 17:55:42 +0000 | [diff] [blame] | 629 | "objc_retain since the operand is not a return value.\n" |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 630 | "Old = " << *RetainRV << "\n"); |
Michael Gottesman | 10426b5 | 2013-01-07 21:26:07 +0000 | [diff] [blame] | 631 | |
Michael Gottesman | ca3a472 | 2015-03-16 07:02:24 +0000 | [diff] [blame] | 632 | Constant *NewDecl = EP.get(ARCRuntimeEntryPointKind::Retain); |
Michael Gottesman | 14acfac | 2013-07-06 01:39:23 +0000 | [diff] [blame] | 633 | cast<CallInst>(RetainRV)->setCalledFunction(NewDecl); |
Michael Gottesman | def07bb | 2013-01-05 17:55:42 +0000 | [diff] [blame] | 634 | |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 635 | DEBUG(dbgs() << "New = " << *RetainRV << "\n"); |
Michael Gottesman | def07bb | 2013-01-05 17:55:42 +0000 | [diff] [blame] | 636 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 637 | return false; |
| 638 | } |
| 639 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 640 | /// Turn objc_autoreleaseReturnValue into objc_autorelease if the result is not |
| 641 | /// used as a return value. |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 642 | void ObjCARCOpt::OptimizeAutoreleaseRVCall(Function &F, |
| 643 | Instruction *AutoreleaseRV, |
| 644 | ARCInstKind &Class) { |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 645 | // Check for a return of the pointer value. |
Michael Gottesman | e5ad66f | 2015-02-19 00:42:38 +0000 | [diff] [blame] | 646 | const Value *Ptr = GetArgRCIdentityRoot(AutoreleaseRV); |
Dan Gohman | 10a18d5 | 2011-08-12 00:36:31 +0000 | [diff] [blame] | 647 | SmallVector<const Value *, 2> Users; |
| 648 | Users.push_back(Ptr); |
| 649 | do { |
| 650 | Ptr = Users.pop_back_val(); |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 651 | for (const User *U : Ptr->users()) { |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 652 | if (isa<ReturnInst>(U) || GetBasicARCInstKind(U) == ARCInstKind::RetainRV) |
Dan Gohman | 10a18d5 | 2011-08-12 00:36:31 +0000 | [diff] [blame] | 653 | return; |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 654 | if (isa<BitCastInst>(U)) |
| 655 | Users.push_back(U); |
Dan Gohman | 10a18d5 | 2011-08-12 00:36:31 +0000 | [diff] [blame] | 656 | } |
| 657 | } while (!Users.empty()); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 658 | |
| 659 | Changed = true; |
| 660 | ++NumPeeps; |
Michael Gottesman | 1bf6908 | 2013-01-06 21:07:11 +0000 | [diff] [blame] | 661 | |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 662 | DEBUG(dbgs() << "Transforming objc_autoreleaseReturnValue => " |
Michael Gottesman | 1bf6908 | 2013-01-06 21:07:11 +0000 | [diff] [blame] | 663 | "objc_autorelease since its operand is not used as a return " |
| 664 | "value.\n" |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 665 | "Old = " << *AutoreleaseRV << "\n"); |
Michael Gottesman | 1bf6908 | 2013-01-06 21:07:11 +0000 | [diff] [blame] | 666 | |
Michael Gottesman | c9656fa | 2013-01-12 01:25:15 +0000 | [diff] [blame] | 667 | CallInst *AutoreleaseRVCI = cast<CallInst>(AutoreleaseRV); |
Michael Gottesman | ca3a472 | 2015-03-16 07:02:24 +0000 | [diff] [blame] | 668 | Constant *NewDecl = EP.get(ARCRuntimeEntryPointKind::Autorelease); |
Michael Gottesman | 14acfac | 2013-07-06 01:39:23 +0000 | [diff] [blame] | 669 | AutoreleaseRVCI->setCalledFunction(NewDecl); |
Michael Gottesman | c9656fa | 2013-01-12 01:25:15 +0000 | [diff] [blame] | 670 | AutoreleaseRVCI->setTailCall(false); // Never tail call objc_autorelease. |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 671 | Class = ARCInstKind::Autorelease; |
Michael Gottesman | 10426b5 | 2013-01-07 21:26:07 +0000 | [diff] [blame] | 672 | |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 673 | DEBUG(dbgs() << "New: " << *AutoreleaseRV << "\n"); |
Michael Gottesman | 10426b5 | 2013-01-07 21:26:07 +0000 | [diff] [blame] | 674 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 675 | } |
| 676 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 677 | /// Visit each call, one at a time, and make simplifications without doing any |
| 678 | /// additional analysis. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 679 | void ObjCARCOpt::OptimizeIndividualCalls(Function &F) { |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 680 | DEBUG(dbgs() << "\n== ObjCARCOpt::OptimizeIndividualCalls ==\n"); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 681 | // Reset all the flags in preparation for recomputing them. |
| 682 | UsedInThisFunction = 0; |
| 683 | |
| 684 | // Visit all objc_* calls in F. |
| 685 | for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) { |
| 686 | Instruction *Inst = &*I++; |
Michael Gottesman | 3f146e2 | 2013-01-01 16:05:48 +0000 | [diff] [blame] | 687 | |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 688 | ARCInstKind Class = GetBasicARCInstKind(Inst); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 689 | |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 690 | DEBUG(dbgs() << "Visiting: Class: " << Class << "; " << *Inst << "\n"); |
Michael Gottesman | 782e344 | 2013-01-17 18:32:34 +0000 | [diff] [blame] | 691 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 692 | switch (Class) { |
| 693 | default: break; |
| 694 | |
| 695 | // Delete no-op casts. These function calls have special semantics, but |
| 696 | // the semantics are entirely implemented via lowering in the front-end, |
| 697 | // so by the time they reach the optimizer, they are just no-op calls |
| 698 | // which return their argument. |
| 699 | // |
| 700 | // There are gray areas here, as the ability to cast reference-counted |
| 701 | // pointers to raw void* and back allows code to break ARC assumptions, |
| 702 | // however these are currently considered to be unimportant. |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 703 | case ARCInstKind::NoopCast: |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 704 | Changed = true; |
| 705 | ++NumNoops; |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 706 | DEBUG(dbgs() << "Erasing no-op cast: " << *Inst << "\n"); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 707 | EraseInstruction(Inst); |
| 708 | continue; |
| 709 | |
| 710 | // If the pointer-to-weak-pointer is null, it's undefined behavior. |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 711 | case ARCInstKind::StoreWeak: |
| 712 | case ARCInstKind::LoadWeak: |
| 713 | case ARCInstKind::LoadWeakRetained: |
| 714 | case ARCInstKind::InitWeak: |
| 715 | case ARCInstKind::DestroyWeak: { |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 716 | CallInst *CI = cast<CallInst>(Inst); |
Michael Gottesman | 65c2481 | 2013-03-25 09:27:43 +0000 | [diff] [blame] | 717 | if (IsNullOrUndef(CI->getArgOperand(0))) { |
Dan Gohman | 670f937 | 2012-04-13 18:57:48 +0000 | [diff] [blame] | 718 | Changed = true; |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 719 | Type *Ty = CI->getArgOperand(0)->getType(); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 720 | new StoreInst(UndefValue::get(cast<PointerType>(Ty)->getElementType()), |
| 721 | Constant::getNullValue(Ty), |
| 722 | CI); |
Michael Gottesman | 10426b5 | 2013-01-07 21:26:07 +0000 | [diff] [blame] | 723 | llvm::Value *NewValue = UndefValue::get(CI->getType()); |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 724 | DEBUG(dbgs() << "A null pointer-to-weak-pointer is undefined behavior." |
| 725 | "\nOld = " << *CI << "\nNew = " << *NewValue << "\n"); |
Michael Gottesman | fec61c0 | 2013-01-06 21:54:30 +0000 | [diff] [blame] | 726 | CI->replaceAllUsesWith(NewValue); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 727 | CI->eraseFromParent(); |
| 728 | continue; |
| 729 | } |
| 730 | break; |
| 731 | } |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 732 | case ARCInstKind::CopyWeak: |
| 733 | case ARCInstKind::MoveWeak: { |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 734 | CallInst *CI = cast<CallInst>(Inst); |
Michael Gottesman | 65c2481 | 2013-03-25 09:27:43 +0000 | [diff] [blame] | 735 | if (IsNullOrUndef(CI->getArgOperand(0)) || |
| 736 | IsNullOrUndef(CI->getArgOperand(1))) { |
Dan Gohman | 670f937 | 2012-04-13 18:57:48 +0000 | [diff] [blame] | 737 | Changed = true; |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 738 | Type *Ty = CI->getArgOperand(0)->getType(); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 739 | new StoreInst(UndefValue::get(cast<PointerType>(Ty)->getElementType()), |
| 740 | Constant::getNullValue(Ty), |
| 741 | CI); |
Michael Gottesman | fec61c0 | 2013-01-06 21:54:30 +0000 | [diff] [blame] | 742 | |
| 743 | llvm::Value *NewValue = UndefValue::get(CI->getType()); |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 744 | DEBUG(dbgs() << "A null pointer-to-weak-pointer is undefined behavior." |
| 745 | "\nOld = " << *CI << "\nNew = " << *NewValue << "\n"); |
Michael Gottesman | 10426b5 | 2013-01-07 21:26:07 +0000 | [diff] [blame] | 746 | |
Michael Gottesman | fec61c0 | 2013-01-06 21:54:30 +0000 | [diff] [blame] | 747 | CI->replaceAllUsesWith(NewValue); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 748 | CI->eraseFromParent(); |
| 749 | continue; |
| 750 | } |
| 751 | break; |
| 752 | } |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 753 | case ARCInstKind::RetainRV: |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 754 | if (OptimizeRetainRVCall(F, Inst)) |
| 755 | continue; |
| 756 | break; |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 757 | case ARCInstKind::AutoreleaseRV: |
Michael Gottesman | 556ff61 | 2013-01-12 01:25:19 +0000 | [diff] [blame] | 758 | OptimizeAutoreleaseRVCall(F, Inst, Class); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 759 | break; |
| 760 | } |
| 761 | |
Michael Gottesman | b8c8836 | 2013-04-03 02:57:24 +0000 | [diff] [blame] | 762 | // objc_autorelease(x) -> objc_release(x) if x is otherwise unused. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 763 | if (IsAutorelease(Class) && Inst->use_empty()) { |
| 764 | CallInst *Call = cast<CallInst>(Inst); |
| 765 | const Value *Arg = Call->getArgOperand(0); |
| 766 | Arg = FindSingleUseIdentifiedObject(Arg); |
| 767 | if (Arg) { |
| 768 | Changed = true; |
| 769 | ++NumAutoreleases; |
| 770 | |
| 771 | // Create the declaration lazily. |
| 772 | LLVMContext &C = Inst->getContext(); |
Michael Gottesman | 01df450 | 2013-07-06 01:41:35 +0000 | [diff] [blame] | 773 | |
Michael Gottesman | ca3a472 | 2015-03-16 07:02:24 +0000 | [diff] [blame] | 774 | Constant *Decl = EP.get(ARCRuntimeEntryPointKind::Release); |
Michael Gottesman | 14acfac | 2013-07-06 01:39:23 +0000 | [diff] [blame] | 775 | CallInst *NewCall = CallInst::Create(Decl, Call->getArgOperand(0), "", |
| 776 | Call); |
Michael Gottesman | 65cb737 | 2015-03-16 07:02:27 +0000 | [diff] [blame] | 777 | NewCall->setMetadata(MDKindCache.get(ARCMDKindID::ImpreciseRelease), |
Michael Gottesman | 41c0100 | 2015-03-06 00:34:33 +0000 | [diff] [blame] | 778 | MDNode::get(C, None)); |
Michael Gottesman | 10426b5 | 2013-01-07 21:26:07 +0000 | [diff] [blame] | 779 | |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 780 | DEBUG(dbgs() << "Replacing autorelease{,RV}(x) with objc_release(x) " |
| 781 | "since x is otherwise unused.\nOld: " << *Call << "\nNew: " |
| 782 | << *NewCall << "\n"); |
Michael Gottesman | 10426b5 | 2013-01-07 21:26:07 +0000 | [diff] [blame] | 783 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 784 | EraseInstruction(Call); |
| 785 | Inst = NewCall; |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 786 | Class = ARCInstKind::Release; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 787 | } |
| 788 | } |
| 789 | |
| 790 | // For functions which can never be passed stack arguments, add |
| 791 | // a tail keyword. |
| 792 | if (IsAlwaysTail(Class)) { |
| 793 | Changed = true; |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 794 | DEBUG(dbgs() << "Adding tail keyword to function since it can never be " |
| 795 | "passed stack args: " << *Inst << "\n"); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 796 | cast<CallInst>(Inst)->setTailCall(); |
| 797 | } |
| 798 | |
Michael Gottesman | c9656fa | 2013-01-12 01:25:15 +0000 | [diff] [blame] | 799 | // Ensure that functions that can never have a "tail" keyword due to the |
| 800 | // semantics of ARC truly do not do so. |
| 801 | if (IsNeverTail(Class)) { |
| 802 | Changed = true; |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 803 | DEBUG(dbgs() << "Removing tail keyword from function: " << *Inst << |
Michael Gottesman | c9656fa | 2013-01-12 01:25:15 +0000 | [diff] [blame] | 804 | "\n"); |
| 805 | cast<CallInst>(Inst)->setTailCall(false); |
| 806 | } |
| 807 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 808 | // Set nounwind as needed. |
| 809 | if (IsNoThrow(Class)) { |
| 810 | Changed = true; |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 811 | DEBUG(dbgs() << "Found no throw class. Setting nounwind on: " << *Inst |
| 812 | << "\n"); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 813 | cast<CallInst>(Inst)->setDoesNotThrow(); |
| 814 | } |
| 815 | |
| 816 | if (!IsNoopOnNull(Class)) { |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 817 | UsedInThisFunction |= 1 << unsigned(Class); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 818 | continue; |
| 819 | } |
| 820 | |
Michael Gottesman | e5ad66f | 2015-02-19 00:42:38 +0000 | [diff] [blame] | 821 | const Value *Arg = GetArgRCIdentityRoot(Inst); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 822 | |
| 823 | // ARC calls with null are no-ops. Delete them. |
Michael Gottesman | 65c2481 | 2013-03-25 09:27:43 +0000 | [diff] [blame] | 824 | if (IsNullOrUndef(Arg)) { |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 825 | Changed = true; |
| 826 | ++NumNoops; |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 827 | DEBUG(dbgs() << "ARC calls with null are no-ops. Erasing: " << *Inst |
| 828 | << "\n"); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 829 | EraseInstruction(Inst); |
| 830 | continue; |
| 831 | } |
| 832 | |
| 833 | // Keep track of which of retain, release, autorelease, and retain_block |
| 834 | // are actually present in this function. |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 835 | UsedInThisFunction |= 1 << unsigned(Class); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 836 | |
| 837 | // If Arg is a PHI, and one or more incoming values to the |
| 838 | // PHI are null, and the call is control-equivalent to the PHI, and there |
| 839 | // are no relevant side effects between the PHI and the call, the call |
| 840 | // could be pushed up to just those paths with non-null incoming values. |
| 841 | // For now, don't bother splitting critical edges for this. |
| 842 | SmallVector<std::pair<Instruction *, const Value *>, 4> Worklist; |
| 843 | Worklist.push_back(std::make_pair(Inst, Arg)); |
| 844 | do { |
| 845 | std::pair<Instruction *, const Value *> Pair = Worklist.pop_back_val(); |
| 846 | Inst = Pair.first; |
| 847 | Arg = Pair.second; |
| 848 | |
| 849 | const PHINode *PN = dyn_cast<PHINode>(Arg); |
| 850 | if (!PN) continue; |
| 851 | |
| 852 | // Determine if the PHI has any null operands, or any incoming |
| 853 | // critical edges. |
| 854 | bool HasNull = false; |
| 855 | bool HasCriticalEdges = false; |
| 856 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 857 | Value *Incoming = |
Michael Gottesman | e5ad66f | 2015-02-19 00:42:38 +0000 | [diff] [blame] | 858 | GetRCIdentityRoot(PN->getIncomingValue(i)); |
Michael Gottesman | 65c2481 | 2013-03-25 09:27:43 +0000 | [diff] [blame] | 859 | if (IsNullOrUndef(Incoming)) |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 860 | HasNull = true; |
| 861 | else if (cast<TerminatorInst>(PN->getIncomingBlock(i)->back()) |
| 862 | .getNumSuccessors() != 1) { |
| 863 | HasCriticalEdges = true; |
| 864 | break; |
| 865 | } |
| 866 | } |
| 867 | // If we have null operands and no critical edges, optimize. |
| 868 | if (!HasCriticalEdges && HasNull) { |
| 869 | SmallPtrSet<Instruction *, 4> DependingInstructions; |
| 870 | SmallPtrSet<const BasicBlock *, 4> Visited; |
| 871 | |
| 872 | // Check that there is nothing that cares about the reference |
| 873 | // count between the call and the phi. |
Dan Gohman | 8478d76 | 2012-04-13 00:59:57 +0000 | [diff] [blame] | 874 | switch (Class) { |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 875 | case ARCInstKind::Retain: |
| 876 | case ARCInstKind::RetainBlock: |
Dan Gohman | 8478d76 | 2012-04-13 00:59:57 +0000 | [diff] [blame] | 877 | // These can always be moved up. |
| 878 | break; |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 879 | case ARCInstKind::Release: |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 880 | // These can't be moved across things that care about the retain |
| 881 | // count. |
Dan Gohman | 8478d76 | 2012-04-13 00:59:57 +0000 | [diff] [blame] | 882 | FindDependencies(NeedsPositiveRetainCount, Arg, |
| 883 | Inst->getParent(), Inst, |
| 884 | DependingInstructions, Visited, PA); |
| 885 | break; |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 886 | case ARCInstKind::Autorelease: |
Dan Gohman | 8478d76 | 2012-04-13 00:59:57 +0000 | [diff] [blame] | 887 | // These can't be moved across autorelease pool scope boundaries. |
| 888 | FindDependencies(AutoreleasePoolBoundary, Arg, |
| 889 | Inst->getParent(), Inst, |
| 890 | DependingInstructions, Visited, PA); |
| 891 | break; |
Frederic Riss | 009d606 | 2016-02-17 18:51:27 +0000 | [diff] [blame] | 892 | case ARCInstKind::ClaimRV: |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 893 | case ARCInstKind::RetainRV: |
| 894 | case ARCInstKind::AutoreleaseRV: |
Dan Gohman | 8478d76 | 2012-04-13 00:59:57 +0000 | [diff] [blame] | 895 | // Don't move these; the RV optimization depends on the autoreleaseRV |
| 896 | // being tail called, and the retainRV being immediately after a call |
| 897 | // (which might still happen if we get lucky with codegen layout, but |
| 898 | // it's not worth taking the chance). |
| 899 | continue; |
| 900 | default: |
| 901 | llvm_unreachable("Invalid dependence flavor"); |
| 902 | } |
| 903 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 904 | if (DependingInstructions.size() == 1 && |
| 905 | *DependingInstructions.begin() == PN) { |
| 906 | Changed = true; |
| 907 | ++NumPartialNoops; |
| 908 | // Clone the call into each predecessor that has a non-null value. |
| 909 | CallInst *CInst = cast<CallInst>(Inst); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 910 | Type *ParamTy = CInst->getArgOperand(0)->getType(); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 911 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 912 | Value *Incoming = |
Michael Gottesman | e5ad66f | 2015-02-19 00:42:38 +0000 | [diff] [blame] | 913 | GetRCIdentityRoot(PN->getIncomingValue(i)); |
Michael Gottesman | 65c2481 | 2013-03-25 09:27:43 +0000 | [diff] [blame] | 914 | if (!IsNullOrUndef(Incoming)) { |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 915 | CallInst *Clone = cast<CallInst>(CInst->clone()); |
| 916 | Value *Op = PN->getIncomingValue(i); |
| 917 | Instruction *InsertPos = &PN->getIncomingBlock(i)->back(); |
| 918 | if (Op->getType() != ParamTy) |
| 919 | Op = new BitCastInst(Op, ParamTy, "", InsertPos); |
| 920 | Clone->setArgOperand(0, Op); |
| 921 | Clone->insertBefore(InsertPos); |
Michael Gottesman | c189a39 | 2013-01-09 19:23:24 +0000 | [diff] [blame] | 922 | |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 923 | DEBUG(dbgs() << "Cloning " |
Michael Gottesman | c189a39 | 2013-01-09 19:23:24 +0000 | [diff] [blame] | 924 | << *CInst << "\n" |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 925 | "And inserting clone at " << *InsertPos << "\n"); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 926 | Worklist.push_back(std::make_pair(Clone, Incoming)); |
| 927 | } |
| 928 | } |
| 929 | // Erase the original call. |
Michael Gottesman | c189a39 | 2013-01-09 19:23:24 +0000 | [diff] [blame] | 930 | DEBUG(dbgs() << "Erasing: " << *CInst << "\n"); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 931 | EraseInstruction(CInst); |
| 932 | continue; |
| 933 | } |
| 934 | } |
| 935 | } while (!Worklist.empty()); |
| 936 | } |
| 937 | } |
| 938 | |
Michael Gottesman | 323964c | 2013-04-18 05:39:45 +0000 | [diff] [blame] | 939 | /// If we have a top down pointer in the S_Use state, make sure that there are |
| 940 | /// no CFG hazards by checking the states of various bottom up pointers. |
| 941 | static void CheckForUseCFGHazard(const Sequence SuccSSeq, |
| 942 | const bool SuccSRRIKnownSafe, |
Michael Gottesman | feb138e | 2015-03-06 00:34:36 +0000 | [diff] [blame] | 943 | TopDownPtrState &S, |
Michael Gottesman | 323964c | 2013-04-18 05:39:45 +0000 | [diff] [blame] | 944 | bool &SomeSuccHasSame, |
| 945 | bool &AllSuccsHaveSame, |
Michael Gottesman | e67f40c | 2013-05-24 20:44:05 +0000 | [diff] [blame] | 946 | bool &NotAllSeqEqualButKnownSafe, |
Michael Gottesman | 323964c | 2013-04-18 05:39:45 +0000 | [diff] [blame] | 947 | bool &ShouldContinue) { |
| 948 | switch (SuccSSeq) { |
| 949 | case S_CanRelease: { |
Michael Gottesman | 9313225 | 2013-06-21 06:59:02 +0000 | [diff] [blame] | 950 | if (!S.IsKnownSafe() && !SuccSRRIKnownSafe) { |
Michael Gottesman | 323964c | 2013-04-18 05:39:45 +0000 | [diff] [blame] | 951 | S.ClearSequenceProgress(); |
| 952 | break; |
| 953 | } |
Michael Gottesman | 2f29459 | 2013-06-21 19:12:36 +0000 | [diff] [blame] | 954 | S.SetCFGHazardAfflicted(true); |
Michael Gottesman | 323964c | 2013-04-18 05:39:45 +0000 | [diff] [blame] | 955 | ShouldContinue = true; |
| 956 | break; |
| 957 | } |
| 958 | case S_Use: |
| 959 | SomeSuccHasSame = true; |
| 960 | break; |
| 961 | case S_Stop: |
| 962 | case S_Release: |
| 963 | case S_MovableRelease: |
Michael Gottesman | 9313225 | 2013-06-21 06:59:02 +0000 | [diff] [blame] | 964 | if (!S.IsKnownSafe() && !SuccSRRIKnownSafe) |
Michael Gottesman | 323964c | 2013-04-18 05:39:45 +0000 | [diff] [blame] | 965 | AllSuccsHaveSame = false; |
Michael Gottesman | e67f40c | 2013-05-24 20:44:05 +0000 | [diff] [blame] | 966 | else |
| 967 | NotAllSeqEqualButKnownSafe = true; |
Michael Gottesman | 323964c | 2013-04-18 05:39:45 +0000 | [diff] [blame] | 968 | break; |
| 969 | case S_Retain: |
| 970 | llvm_unreachable("bottom-up pointer in retain state!"); |
| 971 | case S_None: |
| 972 | llvm_unreachable("This should have been handled earlier."); |
| 973 | } |
| 974 | } |
| 975 | |
| 976 | /// If we have a Top Down pointer in the S_CanRelease state, make sure that |
| 977 | /// there are no CFG hazards by checking the states of various bottom up |
| 978 | /// pointers. |
| 979 | static void CheckForCanReleaseCFGHazard(const Sequence SuccSSeq, |
| 980 | const bool SuccSRRIKnownSafe, |
Michael Gottesman | feb138e | 2015-03-06 00:34:36 +0000 | [diff] [blame] | 981 | TopDownPtrState &S, |
Michael Gottesman | 323964c | 2013-04-18 05:39:45 +0000 | [diff] [blame] | 982 | bool &SomeSuccHasSame, |
Michael Gottesman | e67f40c | 2013-05-24 20:44:05 +0000 | [diff] [blame] | 983 | bool &AllSuccsHaveSame, |
| 984 | bool &NotAllSeqEqualButKnownSafe) { |
Michael Gottesman | 323964c | 2013-04-18 05:39:45 +0000 | [diff] [blame] | 985 | switch (SuccSSeq) { |
| 986 | case S_CanRelease: |
| 987 | SomeSuccHasSame = true; |
| 988 | break; |
| 989 | case S_Stop: |
| 990 | case S_Release: |
| 991 | case S_MovableRelease: |
| 992 | case S_Use: |
Michael Gottesman | 9313225 | 2013-06-21 06:59:02 +0000 | [diff] [blame] | 993 | if (!S.IsKnownSafe() && !SuccSRRIKnownSafe) |
Michael Gottesman | 323964c | 2013-04-18 05:39:45 +0000 | [diff] [blame] | 994 | AllSuccsHaveSame = false; |
Michael Gottesman | e67f40c | 2013-05-24 20:44:05 +0000 | [diff] [blame] | 995 | else |
| 996 | NotAllSeqEqualButKnownSafe = true; |
Michael Gottesman | 323964c | 2013-04-18 05:39:45 +0000 | [diff] [blame] | 997 | break; |
| 998 | case S_Retain: |
| 999 | llvm_unreachable("bottom-up pointer in retain state!"); |
| 1000 | case S_None: |
| 1001 | llvm_unreachable("This should have been handled earlier."); |
| 1002 | } |
| 1003 | } |
| 1004 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 1005 | /// Check for critical edges, loop boundaries, irreducible control flow, or |
| 1006 | /// other CFG structures where moving code across the edge would result in it |
| 1007 | /// being executed more. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1008 | void |
| 1009 | ObjCARCOpt::CheckForCFGHazards(const BasicBlock *BB, |
| 1010 | DenseMap<const BasicBlock *, BBState> &BBStates, |
| 1011 | BBState &MyStates) const { |
| 1012 | // If any top-down local-use or possible-dec has a succ which is earlier in |
| 1013 | // the sequence, forget it. |
Michael Gottesman | feb138e | 2015-03-06 00:34:36 +0000 | [diff] [blame] | 1014 | for (auto I = MyStates.top_down_ptr_begin(), E = MyStates.top_down_ptr_end(); |
| 1015 | I != E; ++I) { |
| 1016 | TopDownPtrState &S = I->second; |
Michael Gottesman | 323964c | 2013-04-18 05:39:45 +0000 | [diff] [blame] | 1017 | const Sequence Seq = I->second.GetSeq(); |
Dan Gohman | 0155f30 | 2012-02-17 18:59:53 +0000 | [diff] [blame] | 1018 | |
Michael Gottesman | 323964c | 2013-04-18 05:39:45 +0000 | [diff] [blame] | 1019 | // We only care about S_Retain, S_CanRelease, and S_Use. |
| 1020 | if (Seq == S_None) |
| 1021 | continue; |
Dan Gohman | 0155f30 | 2012-02-17 18:59:53 +0000 | [diff] [blame] | 1022 | |
Michael Gottesman | 323964c | 2013-04-18 05:39:45 +0000 | [diff] [blame] | 1023 | // Make sure that if extra top down states are added in the future that this |
| 1024 | // code is updated to handle it. |
| 1025 | assert((Seq == S_Retain || Seq == S_CanRelease || Seq == S_Use) && |
| 1026 | "Unknown top down sequence state."); |
| 1027 | |
| 1028 | const Value *Arg = I->first; |
| 1029 | const TerminatorInst *TI = cast<TerminatorInst>(&BB->back()); |
| 1030 | bool SomeSuccHasSame = false; |
| 1031 | bool AllSuccsHaveSame = true; |
Michael Gottesman | e67f40c | 2013-05-24 20:44:05 +0000 | [diff] [blame] | 1032 | bool NotAllSeqEqualButKnownSafe = false; |
Michael Gottesman | 323964c | 2013-04-18 05:39:45 +0000 | [diff] [blame] | 1033 | |
| 1034 | succ_const_iterator SI(TI), SE(TI, false); |
| 1035 | |
| 1036 | for (; SI != SE; ++SI) { |
| 1037 | // If VisitBottomUp has pointer information for this successor, take |
| 1038 | // what we know about it. |
| 1039 | const DenseMap<const BasicBlock *, BBState>::iterator BBI = |
| 1040 | BBStates.find(*SI); |
| 1041 | assert(BBI != BBStates.end()); |
Michael Gottesman | feb138e | 2015-03-06 00:34:36 +0000 | [diff] [blame] | 1042 | const BottomUpPtrState &SuccS = BBI->second.getPtrBottomUpState(Arg); |
Michael Gottesman | 323964c | 2013-04-18 05:39:45 +0000 | [diff] [blame] | 1043 | const Sequence SuccSSeq = SuccS.GetSeq(); |
| 1044 | |
| 1045 | // If bottom up, the pointer is in an S_None state, clear the sequence |
| 1046 | // progress since the sequence in the bottom up state finished |
| 1047 | // suggesting a mismatch in between retains/releases. This is true for |
| 1048 | // all three cases that we are handling here: S_Retain, S_Use, and |
| 1049 | // S_CanRelease. |
| 1050 | if (SuccSSeq == S_None) { |
Dan Gohman | 1213027 | 2011-08-12 00:26:31 +0000 | [diff] [blame] | 1051 | S.ClearSequenceProgress(); |
Michael Gottesman | 323964c | 2013-04-18 05:39:45 +0000 | [diff] [blame] | 1052 | continue; |
| 1053 | } |
| 1054 | |
| 1055 | // If we have S_Use or S_CanRelease, perform our check for cfg hazard |
| 1056 | // checks. |
Michael Gottesman | 9313225 | 2013-06-21 06:59:02 +0000 | [diff] [blame] | 1057 | const bool SuccSRRIKnownSafe = SuccS.IsKnownSafe(); |
Michael Gottesman | 323964c | 2013-04-18 05:39:45 +0000 | [diff] [blame] | 1058 | |
| 1059 | // *NOTE* We do not use Seq from above here since we are allowing for |
| 1060 | // S.GetSeq() to change while we are visiting basic blocks. |
| 1061 | switch(S.GetSeq()) { |
| 1062 | case S_Use: { |
| 1063 | bool ShouldContinue = false; |
Michael Gottesman | e67f40c | 2013-05-24 20:44:05 +0000 | [diff] [blame] | 1064 | CheckForUseCFGHazard(SuccSSeq, SuccSRRIKnownSafe, S, SomeSuccHasSame, |
| 1065 | AllSuccsHaveSame, NotAllSeqEqualButKnownSafe, |
Michael Gottesman | 323964c | 2013-04-18 05:39:45 +0000 | [diff] [blame] | 1066 | ShouldContinue); |
| 1067 | if (ShouldContinue) |
| 1068 | continue; |
| 1069 | break; |
| 1070 | } |
| 1071 | case S_CanRelease: { |
Michael Gottesman | e67f40c | 2013-05-24 20:44:05 +0000 | [diff] [blame] | 1072 | CheckForCanReleaseCFGHazard(SuccSSeq, SuccSRRIKnownSafe, S, |
| 1073 | SomeSuccHasSame, AllSuccsHaveSame, |
| 1074 | NotAllSeqEqualButKnownSafe); |
Michael Gottesman | 323964c | 2013-04-18 05:39:45 +0000 | [diff] [blame] | 1075 | break; |
| 1076 | } |
| 1077 | case S_Retain: |
| 1078 | case S_None: |
| 1079 | case S_Stop: |
| 1080 | case S_Release: |
| 1081 | case S_MovableRelease: |
| 1082 | break; |
| 1083 | } |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1084 | } |
Michael Gottesman | 323964c | 2013-04-18 05:39:45 +0000 | [diff] [blame] | 1085 | |
| 1086 | // If the state at the other end of any of the successor edges |
| 1087 | // matches the current state, require all edges to match. This |
| 1088 | // guards against loops in the middle of a sequence. |
Michael Gottesman | e67f40c | 2013-05-24 20:44:05 +0000 | [diff] [blame] | 1089 | if (SomeSuccHasSame && !AllSuccsHaveSame) { |
Michael Gottesman | 323964c | 2013-04-18 05:39:45 +0000 | [diff] [blame] | 1090 | S.ClearSequenceProgress(); |
Michael Gottesman | e67f40c | 2013-05-24 20:44:05 +0000 | [diff] [blame] | 1091 | } else if (NotAllSeqEqualButKnownSafe) { |
| 1092 | // If we would have cleared the state foregoing the fact that we are known |
| 1093 | // safe, stop code motion. This is because whether or not it is safe to |
| 1094 | // remove RR pairs via KnownSafe is an orthogonal concept to whether we |
| 1095 | // are allowed to perform code motion. |
Michael Gottesman | 2f29459 | 2013-06-21 19:12:36 +0000 | [diff] [blame] | 1096 | S.SetCFGHazardAfflicted(true); |
Michael Gottesman | e67f40c | 2013-05-24 20:44:05 +0000 | [diff] [blame] | 1097 | } |
Michael Gottesman | 323964c | 2013-04-18 05:39:45 +0000 | [diff] [blame] | 1098 | } |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1099 | } |
| 1100 | |
Michael Gottesman | 0be6920 | 2015-03-05 23:28:58 +0000 | [diff] [blame] | 1101 | bool ObjCARCOpt::VisitInstructionBottomUp( |
| 1102 | Instruction *Inst, BasicBlock *BB, BlotMapVector<Value *, RRInfo> &Retains, |
| 1103 | BBState &MyStates) { |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1104 | bool NestingDetected = false; |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 1105 | ARCInstKind Class = GetARCInstKind(Inst); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1106 | const Value *Arg = nullptr; |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 1107 | |
Michael Gottesman | c01ab51 | 2015-03-16 07:02:39 +0000 | [diff] [blame] | 1108 | DEBUG(dbgs() << " Class: " << Class << "\n"); |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 1109 | |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1110 | switch (Class) { |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 1111 | case ARCInstKind::Release: { |
Michael Gottesman | e5ad66f | 2015-02-19 00:42:38 +0000 | [diff] [blame] | 1112 | Arg = GetArgRCIdentityRoot(Inst); |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1113 | |
Michael Gottesman | feb138e | 2015-03-06 00:34:36 +0000 | [diff] [blame] | 1114 | BottomUpPtrState &S = MyStates.getPtrBottomUpState(Arg); |
Michael Gottesman | 4eae396 | 2015-03-06 00:34:39 +0000 | [diff] [blame] | 1115 | NestingDetected |= S.InitBottomUp(MDKindCache, Inst); |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1116 | break; |
| 1117 | } |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 1118 | case ARCInstKind::RetainBlock: |
Michael Gottesman | 158fdf6 | 2013-03-28 20:11:19 +0000 | [diff] [blame] | 1119 | // In OptimizeIndividualCalls, we have strength reduced all optimizable |
| 1120 | // objc_retainBlocks to objc_retains. Thus at this point any |
| 1121 | // objc_retainBlocks that we see are not optimizable. |
| 1122 | break; |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 1123 | case ARCInstKind::Retain: |
| 1124 | case ARCInstKind::RetainRV: { |
Michael Gottesman | e5ad66f | 2015-02-19 00:42:38 +0000 | [diff] [blame] | 1125 | Arg = GetArgRCIdentityRoot(Inst); |
Michael Gottesman | feb138e | 2015-03-06 00:34:36 +0000 | [diff] [blame] | 1126 | BottomUpPtrState &S = MyStates.getPtrBottomUpState(Arg); |
Michael Gottesman | 6080596 | 2015-03-06 00:34:42 +0000 | [diff] [blame] | 1127 | if (S.MatchWithRetain()) { |
| 1128 | // Don't do retain+release tracking for ARCInstKind::RetainRV, because |
| 1129 | // it's better to let it remain as the first instruction after a call. |
Michael Gottesman | c01ab51 | 2015-03-16 07:02:39 +0000 | [diff] [blame] | 1130 | if (Class != ARCInstKind::RetainRV) { |
| 1131 | DEBUG(llvm::dbgs() << " Matching with: " << *Inst << "\n"); |
Michael Gottesman | e3943d0 | 2013-06-21 19:44:30 +0000 | [diff] [blame] | 1132 | Retains[Inst] = S.GetRRInfo(); |
Michael Gottesman | c01ab51 | 2015-03-16 07:02:39 +0000 | [diff] [blame] | 1133 | } |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1134 | S.ClearSequenceProgress(); |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1135 | } |
Michael Gottesman | 31ba23a | 2013-04-05 22:54:32 +0000 | [diff] [blame] | 1136 | // A retain moving bottom up can be a use. |
| 1137 | break; |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1138 | } |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 1139 | case ARCInstKind::AutoreleasepoolPop: |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1140 | // Conservatively, clear MyStates for all known pointers. |
| 1141 | MyStates.clearBottomUpPointers(); |
| 1142 | return NestingDetected; |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 1143 | case ARCInstKind::AutoreleasepoolPush: |
| 1144 | case ARCInstKind::None: |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1145 | // These are irrelevant. |
| 1146 | return NestingDetected; |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 1147 | case ARCInstKind::User: |
Michael Gottesman | a76143ee | 2013-05-13 23:49:42 +0000 | [diff] [blame] | 1148 | // If we have a store into an alloca of a pointer we are tracking, the |
| 1149 | // pointer has multiple owners implying that we must be more conservative. |
| 1150 | // |
| 1151 | // 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] | 1152 | // presence of a block being initialized, the frontend will emit the |
Michael Gottesman | a76143ee | 2013-05-13 23:49:42 +0000 | [diff] [blame] | 1153 | // objc_retain on the original pointer and the release on the pointer loaded |
| 1154 | // from the alloca. The optimizer will through the provenance analysis |
| 1155 | // realize that the two are related, but since we only require KnownSafe in |
| 1156 | // one direction, will match the inner retain on the original pointer with |
| 1157 | // 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] | 1158 | // in the presence of allocas we only unconditionally remove pointers if |
Michael Gottesman | a76143ee | 2013-05-13 23:49:42 +0000 | [diff] [blame] | 1159 | // both our retain and our release are KnownSafe. |
| 1160 | if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) { |
Mehdi Amini | a28d91d | 2015-03-10 02:37:25 +0000 | [diff] [blame] | 1161 | const DataLayout &DL = BB->getModule()->getDataLayout(); |
| 1162 | if (AreAnyUnderlyingObjectsAnAlloca(SI->getPointerOperand(), DL)) { |
Michael Gottesman | feb138e | 2015-03-06 00:34:36 +0000 | [diff] [blame] | 1163 | auto I = MyStates.findPtrBottomUpState( |
| 1164 | GetRCIdentityRoot(SI->getValueOperand())); |
Michael Gottesman | a76143ee | 2013-05-13 23:49:42 +0000 | [diff] [blame] | 1165 | if (I != MyStates.bottom_up_ptr_end()) |
Michael Gottesman | 5a91bbf | 2013-05-24 20:44:02 +0000 | [diff] [blame] | 1166 | MultiOwnersSet.insert(I->first); |
Michael Gottesman | a76143ee | 2013-05-13 23:49:42 +0000 | [diff] [blame] | 1167 | } |
| 1168 | } |
| 1169 | break; |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1170 | default: |
| 1171 | break; |
| 1172 | } |
| 1173 | |
| 1174 | // Consider any other possible effects of this instruction on each |
| 1175 | // pointer being tracked. |
Michael Gottesman | feb138e | 2015-03-06 00:34:36 +0000 | [diff] [blame] | 1176 | for (auto MI = MyStates.bottom_up_ptr_begin(), |
| 1177 | ME = MyStates.bottom_up_ptr_end(); |
| 1178 | MI != ME; ++MI) { |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1179 | const Value *Ptr = MI->first; |
| 1180 | if (Ptr == Arg) |
| 1181 | continue; // Handled above. |
Michael Gottesman | feb138e | 2015-03-06 00:34:36 +0000 | [diff] [blame] | 1182 | BottomUpPtrState &S = MI->second; |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1183 | |
Michael Gottesman | 16e6a20 | 2015-03-06 02:07:12 +0000 | [diff] [blame] | 1184 | if (S.HandlePotentialAlterRefCount(Inst, Ptr, PA, Class)) |
| 1185 | continue; |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1186 | |
Michael Gottesman | 16e6a20 | 2015-03-06 02:07:12 +0000 | [diff] [blame] | 1187 | S.HandlePotentialUse(BB, Inst, Ptr, PA, Class); |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1188 | } |
| 1189 | |
| 1190 | return NestingDetected; |
| 1191 | } |
| 1192 | |
Michael Gottesman | 0be6920 | 2015-03-05 23:28:58 +0000 | [diff] [blame] | 1193 | bool ObjCARCOpt::VisitBottomUp(BasicBlock *BB, |
| 1194 | DenseMap<const BasicBlock *, BBState> &BBStates, |
| 1195 | BlotMapVector<Value *, RRInfo> &Retains) { |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1196 | |
| 1197 | DEBUG(dbgs() << "\n== ObjCARCOpt::VisitBottomUp ==\n"); |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 1198 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1199 | bool NestingDetected = false; |
| 1200 | BBState &MyStates = BBStates[BB]; |
| 1201 | |
| 1202 | // Merge the states from each successor to compute the initial state |
| 1203 | // for the current block. |
Dan Gohman | 10c82ce | 2012-08-27 18:31:36 +0000 | [diff] [blame] | 1204 | BBState::edge_iterator SI(MyStates.succ_begin()), |
| 1205 | SE(MyStates.succ_end()); |
| 1206 | if (SI != SE) { |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 1207 | const BasicBlock *Succ = *SI; |
| 1208 | DenseMap<const BasicBlock *, BBState>::iterator I = BBStates.find(Succ); |
| 1209 | assert(I != BBStates.end()); |
| 1210 | MyStates.InitFromSucc(I->second); |
| 1211 | ++SI; |
| 1212 | for (; SI != SE; ++SI) { |
| 1213 | Succ = *SI; |
| 1214 | I = BBStates.find(Succ); |
| 1215 | assert(I != BBStates.end()); |
| 1216 | MyStates.MergeSucc(I->second); |
| 1217 | } |
Michael Gottesman | 60f6b28 | 2013-03-29 05:13:07 +0000 | [diff] [blame] | 1218 | } |
Michael Gottesman | cd4de0f | 2013-03-26 00:42:09 +0000 | [diff] [blame] | 1219 | |
Michael Gottesman | c01ab51 | 2015-03-16 07:02:39 +0000 | [diff] [blame] | 1220 | DEBUG(llvm::dbgs() << "Before:\n" << BBStates[BB] << "\n" |
| 1221 | << "Performing Dataflow:\n"); |
| 1222 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1223 | // Visit all the instructions, bottom-up. |
| 1224 | for (BasicBlock::iterator I = BB->end(), E = BB->begin(); I != E; --I) { |
Duncan P. N. Exon Smith | 1e59a66 | 2015-10-19 23:20:14 +0000 | [diff] [blame] | 1225 | Instruction *Inst = &*std::prev(I); |
Dan Gohman | 5c70fad | 2012-03-23 17:47:54 +0000 | [diff] [blame] | 1226 | |
| 1227 | // Invoke instructions are visited as part of their successors (below). |
| 1228 | if (isa<InvokeInst>(Inst)) |
| 1229 | continue; |
| 1230 | |
Michael Gottesman | c01ab51 | 2015-03-16 07:02:39 +0000 | [diff] [blame] | 1231 | DEBUG(dbgs() << " Visiting " << *Inst << "\n"); |
Michael Gottesman | af2113f | 2013-01-13 07:00:51 +0000 | [diff] [blame] | 1232 | |
Dan Gohman | 5c70fad | 2012-03-23 17:47:54 +0000 | [diff] [blame] | 1233 | NestingDetected |= VisitInstructionBottomUp(Inst, BB, Retains, MyStates); |
| 1234 | } |
| 1235 | |
Dan Gohman | dae3349 | 2012-04-27 18:56:31 +0000 | [diff] [blame] | 1236 | // If there's a predecessor with an invoke, visit the invoke as if it were |
| 1237 | // part of this block, since we can't insert code after an invoke in its own |
| 1238 | // block, and we don't want to split critical edges. |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 1239 | for (BBState::edge_iterator PI(MyStates.pred_begin()), |
| 1240 | PE(MyStates.pred_end()); PI != PE; ++PI) { |
Dan Gohman | 5c70fad | 2012-03-23 17:47:54 +0000 | [diff] [blame] | 1241 | BasicBlock *Pred = *PI; |
Dan Gohman | dae3349 | 2012-04-27 18:56:31 +0000 | [diff] [blame] | 1242 | if (InvokeInst *II = dyn_cast<InvokeInst>(&Pred->back())) |
| 1243 | NestingDetected |= VisitInstructionBottomUp(II, BB, Retains, MyStates); |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1244 | } |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1245 | |
Michael Gottesman | c01ab51 | 2015-03-16 07:02:39 +0000 | [diff] [blame] | 1246 | DEBUG(llvm::dbgs() << "\nFinal State:\n" << BBStates[BB] << "\n"); |
| 1247 | |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1248 | return NestingDetected; |
| 1249 | } |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1250 | |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1251 | bool |
| 1252 | ObjCARCOpt::VisitInstructionTopDown(Instruction *Inst, |
| 1253 | DenseMap<Value *, RRInfo> &Releases, |
| 1254 | BBState &MyStates) { |
| 1255 | bool NestingDetected = false; |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 1256 | ARCInstKind Class = GetARCInstKind(Inst); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1257 | const Value *Arg = nullptr; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1258 | |
Michael Gottesman | c01ab51 | 2015-03-16 07:02:39 +0000 | [diff] [blame] | 1259 | DEBUG(llvm::dbgs() << " Class: " << Class << "\n"); |
| 1260 | |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1261 | switch (Class) { |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 1262 | case ARCInstKind::RetainBlock: |
Michael Gottesman | 158fdf6 | 2013-03-28 20:11:19 +0000 | [diff] [blame] | 1263 | // In OptimizeIndividualCalls, we have strength reduced all optimizable |
| 1264 | // objc_retainBlocks to objc_retains. Thus at this point any |
Michael Gottesman | 6080596 | 2015-03-06 00:34:42 +0000 | [diff] [blame] | 1265 | // objc_retainBlocks that we see are not optimizable. We need to break since |
| 1266 | // a retain can be a potential use. |
Michael Gottesman | 158fdf6 | 2013-03-28 20:11:19 +0000 | [diff] [blame] | 1267 | break; |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 1268 | case ARCInstKind::Retain: |
| 1269 | case ARCInstKind::RetainRV: { |
Michael Gottesman | e5ad66f | 2015-02-19 00:42:38 +0000 | [diff] [blame] | 1270 | Arg = GetArgRCIdentityRoot(Inst); |
Michael Gottesman | feb138e | 2015-03-06 00:34:36 +0000 | [diff] [blame] | 1271 | TopDownPtrState &S = MyStates.getPtrTopDownState(Arg); |
Michael Gottesman | 4eae396 | 2015-03-06 00:34:39 +0000 | [diff] [blame] | 1272 | NestingDetected |= S.InitTopDown(Class, Inst); |
Benjamin Kramer | df005cb | 2015-08-08 18:27:36 +0000 | [diff] [blame] | 1273 | // A retain can be a potential use; proceed to the generic checking |
Dan Gohman | f64ff8e | 2012-07-23 19:27:31 +0000 | [diff] [blame] | 1274 | // code below. |
| 1275 | break; |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1276 | } |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 1277 | case ARCInstKind::Release: { |
Michael Gottesman | e5ad66f | 2015-02-19 00:42:38 +0000 | [diff] [blame] | 1278 | Arg = GetArgRCIdentityRoot(Inst); |
Michael Gottesman | feb138e | 2015-03-06 00:34:36 +0000 | [diff] [blame] | 1279 | TopDownPtrState &S = MyStates.getPtrTopDownState(Arg); |
Michael Gottesman | 6080596 | 2015-03-06 00:34:42 +0000 | [diff] [blame] | 1280 | // Try to form a tentative pair in between this release instruction and the |
| 1281 | // top down pointers that we are tracking. |
| 1282 | if (S.MatchWithRelease(MDKindCache, Inst)) { |
| 1283 | // If we succeed, copy S's RRInfo into the Release -> {Retain Set |
| 1284 | // Map}. Then we clear S. |
Michael Gottesman | c01ab51 | 2015-03-16 07:02:39 +0000 | [diff] [blame] | 1285 | DEBUG(llvm::dbgs() << " Matching with: " << *Inst << "\n"); |
Michael Gottesman | e3943d0 | 2013-06-21 19:44:30 +0000 | [diff] [blame] | 1286 | Releases[Inst] = S.GetRRInfo(); |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1287 | S.ClearSequenceProgress(); |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1288 | } |
| 1289 | break; |
| 1290 | } |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 1291 | case ARCInstKind::AutoreleasepoolPop: |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1292 | // Conservatively, clear MyStates for all known pointers. |
| 1293 | MyStates.clearTopDownPointers(); |
Michael Gottesman | 6080596 | 2015-03-06 00:34:42 +0000 | [diff] [blame] | 1294 | return false; |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 1295 | case ARCInstKind::AutoreleasepoolPush: |
| 1296 | case ARCInstKind::None: |
Michael Gottesman | 6080596 | 2015-03-06 00:34:42 +0000 | [diff] [blame] | 1297 | // These can not be uses of |
| 1298 | return false; |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1299 | default: |
| 1300 | break; |
| 1301 | } |
| 1302 | |
| 1303 | // Consider any other possible effects of this instruction on each |
| 1304 | // pointer being tracked. |
Michael Gottesman | feb138e | 2015-03-06 00:34:36 +0000 | [diff] [blame] | 1305 | for (auto MI = MyStates.top_down_ptr_begin(), |
| 1306 | ME = MyStates.top_down_ptr_end(); |
| 1307 | MI != ME; ++MI) { |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1308 | const Value *Ptr = MI->first; |
| 1309 | if (Ptr == Arg) |
| 1310 | continue; // Handled above. |
Michael Gottesman | feb138e | 2015-03-06 00:34:36 +0000 | [diff] [blame] | 1311 | TopDownPtrState &S = MI->second; |
Michael Gottesman | 16e6a20 | 2015-03-06 02:07:12 +0000 | [diff] [blame] | 1312 | if (S.HandlePotentialAlterRefCount(Inst, Ptr, PA, Class)) |
| 1313 | continue; |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1314 | |
Michael Gottesman | 16e6a20 | 2015-03-06 02:07:12 +0000 | [diff] [blame] | 1315 | S.HandlePotentialUse(Inst, Ptr, PA, Class); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1316 | } |
| 1317 | |
| 1318 | return NestingDetected; |
| 1319 | } |
| 1320 | |
| 1321 | bool |
| 1322 | ObjCARCOpt::VisitTopDown(BasicBlock *BB, |
| 1323 | DenseMap<const BasicBlock *, BBState> &BBStates, |
| 1324 | DenseMap<Value *, RRInfo> &Releases) { |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1325 | DEBUG(dbgs() << "\n== ObjCARCOpt::VisitTopDown ==\n"); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1326 | bool NestingDetected = false; |
| 1327 | BBState &MyStates = BBStates[BB]; |
| 1328 | |
| 1329 | // Merge the states from each predecessor to compute the initial state |
| 1330 | // for the current block. |
Dan Gohman | 10c82ce | 2012-08-27 18:31:36 +0000 | [diff] [blame] | 1331 | BBState::edge_iterator PI(MyStates.pred_begin()), |
| 1332 | PE(MyStates.pred_end()); |
| 1333 | if (PI != PE) { |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 1334 | const BasicBlock *Pred = *PI; |
| 1335 | DenseMap<const BasicBlock *, BBState>::iterator I = BBStates.find(Pred); |
| 1336 | assert(I != BBStates.end()); |
| 1337 | MyStates.InitFromPred(I->second); |
| 1338 | ++PI; |
| 1339 | for (; PI != PE; ++PI) { |
| 1340 | Pred = *PI; |
| 1341 | I = BBStates.find(Pred); |
| 1342 | assert(I != BBStates.end()); |
| 1343 | MyStates.MergePred(I->second); |
| 1344 | } |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 1345 | } |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1346 | |
Michael Gottesman | c01ab51 | 2015-03-16 07:02:39 +0000 | [diff] [blame] | 1347 | DEBUG(llvm::dbgs() << "Before:\n" << BBStates[BB] << "\n" |
| 1348 | << "Performing Dataflow:\n"); |
| 1349 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1350 | // Visit all the instructions, top-down. |
Duncan P. N. Exon Smith | 1e59a66 | 2015-10-19 23:20:14 +0000 | [diff] [blame] | 1351 | for (Instruction &Inst : *BB) { |
| 1352 | DEBUG(dbgs() << " Visiting " << Inst << "\n"); |
Michael Gottesman | af2113f | 2013-01-13 07:00:51 +0000 | [diff] [blame] | 1353 | |
Duncan P. N. Exon Smith | 1e59a66 | 2015-10-19 23:20:14 +0000 | [diff] [blame] | 1354 | NestingDetected |= VisitInstructionTopDown(&Inst, Releases, MyStates); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1355 | } |
Michael Gottesman | c2d5bf5 | 2013-04-03 23:07:45 +0000 | [diff] [blame] | 1356 | |
Michael Gottesman | c01ab51 | 2015-03-16 07:02:39 +0000 | [diff] [blame] | 1357 | DEBUG(llvm::dbgs() << "\nState Before Checking for CFG Hazards:\n" |
| 1358 | << BBStates[BB] << "\n\n"); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1359 | CheckForCFGHazards(BB, BBStates, MyStates); |
Michael Gottesman | c01ab51 | 2015-03-16 07:02:39 +0000 | [diff] [blame] | 1360 | DEBUG(llvm::dbgs() << "Final State:\n" << BBStates[BB] << "\n"); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1361 | return NestingDetected; |
| 1362 | } |
| 1363 | |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 1364 | static void |
| 1365 | ComputePostOrders(Function &F, |
| 1366 | SmallVectorImpl<BasicBlock *> &PostOrder, |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 1367 | SmallVectorImpl<BasicBlock *> &ReverseCFGPostOrder, |
| 1368 | unsigned NoObjCARCExceptionsMDKind, |
| 1369 | DenseMap<const BasicBlock *, BBState> &BBStates) { |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 1370 | /// The visited set, for doing DFS walks. |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 1371 | SmallPtrSet<BasicBlock *, 16> Visited; |
| 1372 | |
| 1373 | // Do DFS, computing the PostOrder. |
| 1374 | SmallPtrSet<BasicBlock *, 16> OnStack; |
| 1375 | SmallVector<std::pair<BasicBlock *, succ_iterator>, 16> SuccStack; |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 1376 | |
| 1377 | // Functions always have exactly one entry block, and we don't have |
| 1378 | // any other block that we treat like an entry block. |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 1379 | BasicBlock *EntryBB = &F.getEntryBlock(); |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 1380 | BBState &MyStates = BBStates[EntryBB]; |
| 1381 | MyStates.SetAsEntry(); |
| 1382 | TerminatorInst *EntryTI = cast<TerminatorInst>(&EntryBB->back()); |
| 1383 | SuccStack.push_back(std::make_pair(EntryBB, succ_iterator(EntryTI))); |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 1384 | Visited.insert(EntryBB); |
| 1385 | OnStack.insert(EntryBB); |
| 1386 | do { |
| 1387 | dfs_next_succ: |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 1388 | BasicBlock *CurrBB = SuccStack.back().first; |
| 1389 | TerminatorInst *TI = cast<TerminatorInst>(&CurrBB->back()); |
| 1390 | succ_iterator SE(TI, false); |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 1391 | |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 1392 | while (SuccStack.back().second != SE) { |
| 1393 | BasicBlock *SuccBB = *SuccStack.back().second++; |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 1394 | if (Visited.insert(SuccBB).second) { |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 1395 | TerminatorInst *TI = cast<TerminatorInst>(&SuccBB->back()); |
| 1396 | SuccStack.push_back(std::make_pair(SuccBB, succ_iterator(TI))); |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 1397 | BBStates[CurrBB].addSucc(SuccBB); |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 1398 | BBState &SuccStates = BBStates[SuccBB]; |
| 1399 | SuccStates.addPred(CurrBB); |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 1400 | OnStack.insert(SuccBB); |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 1401 | goto dfs_next_succ; |
| 1402 | } |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 1403 | |
| 1404 | if (!OnStack.count(SuccBB)) { |
| 1405 | BBStates[CurrBB].addSucc(SuccBB); |
| 1406 | BBStates[SuccBB].addPred(CurrBB); |
| 1407 | } |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 1408 | } |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 1409 | OnStack.erase(CurrBB); |
| 1410 | PostOrder.push_back(CurrBB); |
| 1411 | SuccStack.pop_back(); |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 1412 | } while (!SuccStack.empty()); |
| 1413 | |
| 1414 | Visited.clear(); |
| 1415 | |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 1416 | // Do reverse-CFG DFS, computing the reverse-CFG PostOrder. |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 1417 | // Functions may have many exits, and there also blocks which we treat |
| 1418 | // as exits due to ignored edges. |
| 1419 | SmallVector<std::pair<BasicBlock *, BBState::edge_iterator>, 16> PredStack; |
Duncan P. N. Exon Smith | 1e59a66 | 2015-10-19 23:20:14 +0000 | [diff] [blame] | 1420 | for (BasicBlock &ExitBB : F) { |
| 1421 | BBState &MyStates = BBStates[&ExitBB]; |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 1422 | if (!MyStates.isExit()) |
| 1423 | continue; |
| 1424 | |
Dan Gohman | dae3349 | 2012-04-27 18:56:31 +0000 | [diff] [blame] | 1425 | MyStates.SetAsExit(); |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 1426 | |
Duncan P. N. Exon Smith | 1e59a66 | 2015-10-19 23:20:14 +0000 | [diff] [blame] | 1427 | PredStack.push_back(std::make_pair(&ExitBB, MyStates.pred_begin())); |
| 1428 | Visited.insert(&ExitBB); |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 1429 | while (!PredStack.empty()) { |
| 1430 | reverse_dfs_next_succ: |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 1431 | BBState::edge_iterator PE = BBStates[PredStack.back().first].pred_end(); |
| 1432 | while (PredStack.back().second != PE) { |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 1433 | BasicBlock *BB = *PredStack.back().second++; |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 1434 | if (Visited.insert(BB).second) { |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 1435 | PredStack.push_back(std::make_pair(BB, BBStates[BB].pred_begin())); |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 1436 | goto reverse_dfs_next_succ; |
| 1437 | } |
| 1438 | } |
| 1439 | ReverseCFGPostOrder.push_back(PredStack.pop_back_val().first); |
| 1440 | } |
| 1441 | } |
| 1442 | } |
| 1443 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 1444 | // Visit the function both top-down and bottom-up. |
Michael Gottesman | 0be6920 | 2015-03-05 23:28:58 +0000 | [diff] [blame] | 1445 | bool ObjCARCOpt::Visit(Function &F, |
| 1446 | DenseMap<const BasicBlock *, BBState> &BBStates, |
| 1447 | BlotMapVector<Value *, RRInfo> &Retains, |
| 1448 | DenseMap<Value *, RRInfo> &Releases) { |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 1449 | |
| 1450 | // Use reverse-postorder traversals, because we magically know that loops |
| 1451 | // will be well behaved, i.e. they won't repeatedly call retain on a single |
| 1452 | // pointer without doing a release. We can't use the ReversePostOrderTraversal |
| 1453 | // class here because we want the reverse-CFG postorder to consider each |
| 1454 | // function exit point, and we want to ignore selected cycle edges. |
| 1455 | SmallVector<BasicBlock *, 16> PostOrder; |
| 1456 | SmallVector<BasicBlock *, 16> ReverseCFGPostOrder; |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 1457 | ComputePostOrders(F, PostOrder, ReverseCFGPostOrder, |
Michael Gottesman | 65cb737 | 2015-03-16 07:02:27 +0000 | [diff] [blame] | 1458 | MDKindCache.get(ARCMDKindID::NoObjCARCExceptions), |
| 1459 | BBStates); |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 1460 | |
| 1461 | // Use reverse-postorder on the reverse CFG for bottom-up. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1462 | bool BottomUpNestingDetected = false; |
Dan Gohman | c57b58c | 2011-08-18 21:27:42 +0000 | [diff] [blame] | 1463 | for (SmallVectorImpl<BasicBlock *>::const_reverse_iterator I = |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 1464 | ReverseCFGPostOrder.rbegin(), E = ReverseCFGPostOrder.rend(); |
| 1465 | I != E; ++I) |
| 1466 | BottomUpNestingDetected |= VisitBottomUp(*I, BBStates, Retains); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1467 | |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 1468 | // Use reverse-postorder for top-down. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1469 | bool TopDownNestingDetected = false; |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 1470 | for (SmallVectorImpl<BasicBlock *>::const_reverse_iterator I = |
| 1471 | PostOrder.rbegin(), E = PostOrder.rend(); |
| 1472 | I != E; ++I) |
| 1473 | TopDownNestingDetected |= VisitTopDown(*I, BBStates, Releases); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1474 | |
| 1475 | return TopDownNestingDetected && BottomUpNestingDetected; |
| 1476 | } |
| 1477 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 1478 | /// Move the calls in RetainsToMove and ReleasesToMove. |
Michael Gottesman | 0be6920 | 2015-03-05 23:28:58 +0000 | [diff] [blame] | 1479 | void ObjCARCOpt::MoveCalls(Value *Arg, RRInfo &RetainsToMove, |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1480 | RRInfo &ReleasesToMove, |
Michael Gottesman | 0be6920 | 2015-03-05 23:28:58 +0000 | [diff] [blame] | 1481 | BlotMapVector<Value *, RRInfo> &Retains, |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1482 | DenseMap<Value *, RRInfo> &Releases, |
Dan Gohman | 6320f52 | 2011-07-22 22:29:21 +0000 | [diff] [blame] | 1483 | SmallVectorImpl<Instruction *> &DeadInsts, |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 1484 | Module *M) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1485 | Type *ArgTy = Arg->getType(); |
Dan Gohman | 6320f52 | 2011-07-22 22:29:21 +0000 | [diff] [blame] | 1486 | Type *ParamTy = PointerType::getUnqual(Type::getInt8Ty(ArgTy->getContext())); |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 1487 | |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1488 | DEBUG(dbgs() << "== ObjCARCOpt::MoveCalls ==\n"); |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 1489 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1490 | // Insert the new retain and release calls. |
Craig Topper | 4627679 | 2014-08-24 23:23:06 +0000 | [diff] [blame] | 1491 | for (Instruction *InsertPt : ReleasesToMove.ReverseInsertPts) { |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1492 | Value *MyArg = ArgTy == ParamTy ? Arg : |
| 1493 | new BitCastInst(Arg, ParamTy, "", InsertPt); |
Michael Gottesman | ca3a472 | 2015-03-16 07:02:24 +0000 | [diff] [blame] | 1494 | Constant *Decl = EP.get(ARCRuntimeEntryPointKind::Retain); |
Michael Gottesman | 14acfac | 2013-07-06 01:39:23 +0000 | [diff] [blame] | 1495 | CallInst *Call = CallInst::Create(Decl, MyArg, "", InsertPt); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1496 | Call->setDoesNotThrow(); |
Michael Gottesman | ba64859 | 2013-03-28 23:08:44 +0000 | [diff] [blame] | 1497 | Call->setTailCall(); |
Michael Gottesman | 60f6b28 | 2013-03-29 05:13:07 +0000 | [diff] [blame] | 1498 | |
Michael Gottesman | df110ac | 2013-04-21 00:30:50 +0000 | [diff] [blame] | 1499 | DEBUG(dbgs() << "Inserting new Retain: " << *Call << "\n" |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1500 | "At insertion point: " << *InsertPt << "\n"); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1501 | } |
Craig Topper | 4627679 | 2014-08-24 23:23:06 +0000 | [diff] [blame] | 1502 | for (Instruction *InsertPt : RetainsToMove.ReverseInsertPts) { |
Dan Gohman | 5c70fad | 2012-03-23 17:47:54 +0000 | [diff] [blame] | 1503 | Value *MyArg = ArgTy == ParamTy ? Arg : |
| 1504 | new BitCastInst(Arg, ParamTy, "", InsertPt); |
Michael Gottesman | ca3a472 | 2015-03-16 07:02:24 +0000 | [diff] [blame] | 1505 | Constant *Decl = EP.get(ARCRuntimeEntryPointKind::Release); |
Michael Gottesman | 14acfac | 2013-07-06 01:39:23 +0000 | [diff] [blame] | 1506 | CallInst *Call = CallInst::Create(Decl, MyArg, "", InsertPt); |
Dan Gohman | 5c70fad | 2012-03-23 17:47:54 +0000 | [diff] [blame] | 1507 | // Attach a clang.imprecise_release metadata tag, if appropriate. |
| 1508 | if (MDNode *M = ReleasesToMove.ReleaseMetadata) |
Michael Gottesman | 65cb737 | 2015-03-16 07:02:27 +0000 | [diff] [blame] | 1509 | Call->setMetadata(MDKindCache.get(ARCMDKindID::ImpreciseRelease), M); |
Dan Gohman | 5c70fad | 2012-03-23 17:47:54 +0000 | [diff] [blame] | 1510 | Call->setDoesNotThrow(); |
| 1511 | if (ReleasesToMove.IsTailCallRelease) |
| 1512 | Call->setTailCall(); |
Michael Gottesman | c189a39 | 2013-01-09 19:23:24 +0000 | [diff] [blame] | 1513 | |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1514 | DEBUG(dbgs() << "Inserting new Release: " << *Call << "\n" |
| 1515 | "At insertion point: " << *InsertPt << "\n"); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1516 | } |
| 1517 | |
| 1518 | // Delete the original retain and release calls. |
Craig Topper | 4627679 | 2014-08-24 23:23:06 +0000 | [diff] [blame] | 1519 | for (Instruction *OrigRetain : RetainsToMove.Calls) { |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1520 | Retains.blot(OrigRetain); |
| 1521 | DeadInsts.push_back(OrigRetain); |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1522 | DEBUG(dbgs() << "Deleting retain: " << *OrigRetain << "\n"); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1523 | } |
Craig Topper | 4627679 | 2014-08-24 23:23:06 +0000 | [diff] [blame] | 1524 | for (Instruction *OrigRelease : ReleasesToMove.Calls) { |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1525 | Releases.erase(OrigRelease); |
| 1526 | DeadInsts.push_back(OrigRelease); |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1527 | DEBUG(dbgs() << "Deleting release: " << *OrigRelease << "\n"); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1528 | } |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 1529 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1530 | } |
| 1531 | |
Michael Gottesman | 6779217 | 2015-03-16 07:02:30 +0000 | [diff] [blame] | 1532 | bool ObjCARCOpt::PairUpRetainsAndReleases( |
Michael Gottesman | 0be6920 | 2015-03-05 23:28:58 +0000 | [diff] [blame] | 1533 | DenseMap<const BasicBlock *, BBState> &BBStates, |
| 1534 | BlotMapVector<Value *, RRInfo> &Retains, |
| 1535 | DenseMap<Value *, RRInfo> &Releases, Module *M, |
| 1536 | SmallVectorImpl<Instruction *> &NewRetains, |
| 1537 | SmallVectorImpl<Instruction *> &NewReleases, |
| 1538 | SmallVectorImpl<Instruction *> &DeadInsts, RRInfo &RetainsToMove, |
| 1539 | RRInfo &ReleasesToMove, Value *Arg, bool KnownSafe, |
| 1540 | bool &AnyPairsCompletelyEliminated) { |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 1541 | // 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] | 1542 | // is already incremented, we can similarly ignore possible decrements unless |
| 1543 | // we are dealing with a retainable object with multiple provenance sources. |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 1544 | bool KnownSafeTD = true, KnownSafeBU = true; |
Michael Gottesman | a76143ee | 2013-05-13 23:49:42 +0000 | [diff] [blame] | 1545 | bool MultipleOwners = false; |
Michael Gottesman | e67f40c | 2013-05-24 20:44:05 +0000 | [diff] [blame] | 1546 | bool CFGHazardAfflicted = false; |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 1547 | |
| 1548 | // Connect the dots between the top-down-collected RetainsToMove and |
| 1549 | // bottom-up-collected ReleasesToMove to form sets of related calls. |
| 1550 | // This is an iterative process so that we connect multiple releases |
| 1551 | // to multiple retains if needed. |
| 1552 | unsigned OldDelta = 0; |
| 1553 | unsigned NewDelta = 0; |
| 1554 | unsigned OldCount = 0; |
| 1555 | unsigned NewCount = 0; |
| 1556 | bool FirstRelease = true; |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 1557 | for (;;) { |
| 1558 | for (SmallVectorImpl<Instruction *>::const_iterator |
| 1559 | NI = NewRetains.begin(), NE = NewRetains.end(); NI != NE; ++NI) { |
| 1560 | Instruction *NewRetain = *NI; |
Michael Gottesman | 6ff10c9 | 2015-03-06 02:10:03 +0000 | [diff] [blame] | 1561 | auto It = Retains.find(NewRetain); |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 1562 | assert(It != Retains.end()); |
| 1563 | const RRInfo &NewRetainRRI = It->second; |
| 1564 | KnownSafeTD &= NewRetainRRI.KnownSafe; |
Michael Gottesman | 5a91bbf | 2013-05-24 20:44:02 +0000 | [diff] [blame] | 1565 | MultipleOwners = |
Michael Gottesman | e5ad66f | 2015-02-19 00:42:38 +0000 | [diff] [blame] | 1566 | MultipleOwners || MultiOwnersSet.count(GetArgRCIdentityRoot(NewRetain)); |
Craig Topper | 4627679 | 2014-08-24 23:23:06 +0000 | [diff] [blame] | 1567 | for (Instruction *NewRetainRelease : NewRetainRRI.Calls) { |
Michael Gottesman | 6ff10c9 | 2015-03-06 02:10:03 +0000 | [diff] [blame] | 1568 | auto Jt = Releases.find(NewRetainRelease); |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 1569 | if (Jt == Releases.end()) |
| 1570 | return false; |
| 1571 | const RRInfo &NewRetainReleaseRRI = Jt->second; |
Michael Gottesman | 24b2f6f | 2013-11-05 16:02:40 +0000 | [diff] [blame] | 1572 | |
| 1573 | // If the release does not have a reference to the retain as well, |
| 1574 | // something happened which is unaccounted for. Do not do anything. |
| 1575 | // |
| 1576 | // This can happen if we catch an additive overflow during path count |
| 1577 | // merging. |
| 1578 | if (!NewRetainReleaseRRI.Calls.count(NewRetain)) |
| 1579 | return false; |
| 1580 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 1581 | if (ReleasesToMove.Calls.insert(NewRetainRelease).second) { |
Michael Gottesman | 9e7261c | 2013-06-07 06:16:49 +0000 | [diff] [blame] | 1582 | |
| 1583 | // If we overflow when we compute the path count, don't remove/move |
| 1584 | // anything. |
| 1585 | const BBState &NRRBBState = BBStates[NewRetainRelease->getParent()]; |
Michael Gottesman | d6ce6cb | 2013-08-09 23:22:27 +0000 | [diff] [blame] | 1586 | unsigned PathCount = BBState::OverflowOccurredValue; |
Michael Gottesman | 9e7261c | 2013-06-07 06:16:49 +0000 | [diff] [blame] | 1587 | if (NRRBBState.GetAllPathCountWithOverflow(PathCount)) |
| 1588 | return false; |
Michael Gottesman | d6ce6cb | 2013-08-09 23:22:27 +0000 | [diff] [blame] | 1589 | assert(PathCount != BBState::OverflowOccurredValue && |
| 1590 | "PathCount at this point can not be " |
| 1591 | "OverflowOccurredValue."); |
Michael Gottesman | 9e7261c | 2013-06-07 06:16:49 +0000 | [diff] [blame] | 1592 | OldDelta -= PathCount; |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 1593 | |
| 1594 | // Merge the ReleaseMetadata and IsTailCallRelease values. |
| 1595 | if (FirstRelease) { |
| 1596 | ReleasesToMove.ReleaseMetadata = |
| 1597 | NewRetainReleaseRRI.ReleaseMetadata; |
| 1598 | ReleasesToMove.IsTailCallRelease = |
| 1599 | NewRetainReleaseRRI.IsTailCallRelease; |
| 1600 | FirstRelease = false; |
| 1601 | } else { |
| 1602 | if (ReleasesToMove.ReleaseMetadata != |
| 1603 | NewRetainReleaseRRI.ReleaseMetadata) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1604 | ReleasesToMove.ReleaseMetadata = nullptr; |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 1605 | if (ReleasesToMove.IsTailCallRelease != |
| 1606 | NewRetainReleaseRRI.IsTailCallRelease) |
| 1607 | ReleasesToMove.IsTailCallRelease = false; |
| 1608 | } |
| 1609 | |
| 1610 | // Collect the optimal insertion points. |
| 1611 | if (!KnownSafe) |
Craig Topper | 4627679 | 2014-08-24 23:23:06 +0000 | [diff] [blame] | 1612 | for (Instruction *RIP : NewRetainReleaseRRI.ReverseInsertPts) { |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 1613 | if (ReleasesToMove.ReverseInsertPts.insert(RIP).second) { |
Michael Gottesman | 9e7261c | 2013-06-07 06:16:49 +0000 | [diff] [blame] | 1614 | // If we overflow when we compute the path count, don't |
| 1615 | // remove/move anything. |
| 1616 | const BBState &RIPBBState = BBStates[RIP->getParent()]; |
Michael Gottesman | d6ce6cb | 2013-08-09 23:22:27 +0000 | [diff] [blame] | 1617 | PathCount = BBState::OverflowOccurredValue; |
Michael Gottesman | 9e7261c | 2013-06-07 06:16:49 +0000 | [diff] [blame] | 1618 | if (RIPBBState.GetAllPathCountWithOverflow(PathCount)) |
| 1619 | return false; |
Michael Gottesman | d6ce6cb | 2013-08-09 23:22:27 +0000 | [diff] [blame] | 1620 | assert(PathCount != BBState::OverflowOccurredValue && |
| 1621 | "PathCount at this point can not be " |
| 1622 | "OverflowOccurredValue."); |
Michael Gottesman | 9e7261c | 2013-06-07 06:16:49 +0000 | [diff] [blame] | 1623 | NewDelta -= PathCount; |
| 1624 | } |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 1625 | } |
| 1626 | NewReleases.push_back(NewRetainRelease); |
| 1627 | } |
| 1628 | } |
| 1629 | } |
| 1630 | NewRetains.clear(); |
| 1631 | if (NewReleases.empty()) break; |
| 1632 | |
| 1633 | // Back the other way. |
| 1634 | for (SmallVectorImpl<Instruction *>::const_iterator |
| 1635 | NI = NewReleases.begin(), NE = NewReleases.end(); NI != NE; ++NI) { |
| 1636 | Instruction *NewRelease = *NI; |
Michael Gottesman | 6ff10c9 | 2015-03-06 02:10:03 +0000 | [diff] [blame] | 1637 | auto It = Releases.find(NewRelease); |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 1638 | assert(It != Releases.end()); |
| 1639 | const RRInfo &NewReleaseRRI = It->second; |
| 1640 | KnownSafeBU &= NewReleaseRRI.KnownSafe; |
Michael Gottesman | e67f40c | 2013-05-24 20:44:05 +0000 | [diff] [blame] | 1641 | CFGHazardAfflicted |= NewReleaseRRI.CFGHazardAfflicted; |
Craig Topper | 4627679 | 2014-08-24 23:23:06 +0000 | [diff] [blame] | 1642 | for (Instruction *NewReleaseRetain : NewReleaseRRI.Calls) { |
Michael Gottesman | 6ff10c9 | 2015-03-06 02:10:03 +0000 | [diff] [blame] | 1643 | auto Jt = Retains.find(NewReleaseRetain); |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 1644 | if (Jt == Retains.end()) |
| 1645 | return false; |
| 1646 | const RRInfo &NewReleaseRetainRRI = Jt->second; |
Michael Gottesman | 9e7261c | 2013-06-07 06:16:49 +0000 | [diff] [blame] | 1647 | |
Michael Gottesman | 24b2f6f | 2013-11-05 16:02:40 +0000 | [diff] [blame] | 1648 | // If the retain does not have a reference to the release as well, |
| 1649 | // something happened which is unaccounted for. Do not do anything. |
| 1650 | // |
| 1651 | // This can happen if we catch an additive overflow during path count |
| 1652 | // merging. |
| 1653 | if (!NewReleaseRetainRRI.Calls.count(NewRelease)) |
| 1654 | return false; |
| 1655 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 1656 | if (RetainsToMove.Calls.insert(NewReleaseRetain).second) { |
Michael Gottesman | 9e7261c | 2013-06-07 06:16:49 +0000 | [diff] [blame] | 1657 | // If we overflow when we compute the path count, don't remove/move |
| 1658 | // anything. |
| 1659 | const BBState &NRRBBState = BBStates[NewReleaseRetain->getParent()]; |
Michael Gottesman | d6ce6cb | 2013-08-09 23:22:27 +0000 | [diff] [blame] | 1660 | unsigned PathCount = BBState::OverflowOccurredValue; |
Michael Gottesman | 9e7261c | 2013-06-07 06:16:49 +0000 | [diff] [blame] | 1661 | if (NRRBBState.GetAllPathCountWithOverflow(PathCount)) |
| 1662 | return false; |
Michael Gottesman | d6ce6cb | 2013-08-09 23:22:27 +0000 | [diff] [blame] | 1663 | assert(PathCount != BBState::OverflowOccurredValue && |
| 1664 | "PathCount at this point can not be " |
| 1665 | "OverflowOccurredValue."); |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 1666 | OldDelta += PathCount; |
| 1667 | OldCount += PathCount; |
| 1668 | |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 1669 | // Collect the optimal insertion points. |
| 1670 | if (!KnownSafe) |
Craig Topper | 4627679 | 2014-08-24 23:23:06 +0000 | [diff] [blame] | 1671 | for (Instruction *RIP : NewReleaseRetainRRI.ReverseInsertPts) { |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 1672 | if (RetainsToMove.ReverseInsertPts.insert(RIP).second) { |
Michael Gottesman | 9e7261c | 2013-06-07 06:16:49 +0000 | [diff] [blame] | 1673 | // If we overflow when we compute the path count, don't |
| 1674 | // remove/move anything. |
| 1675 | const BBState &RIPBBState = BBStates[RIP->getParent()]; |
Michael Gottesman | d6ce6cb | 2013-08-09 23:22:27 +0000 | [diff] [blame] | 1676 | |
| 1677 | PathCount = BBState::OverflowOccurredValue; |
Michael Gottesman | 9e7261c | 2013-06-07 06:16:49 +0000 | [diff] [blame] | 1678 | if (RIPBBState.GetAllPathCountWithOverflow(PathCount)) |
| 1679 | return false; |
Michael Gottesman | d6ce6cb | 2013-08-09 23:22:27 +0000 | [diff] [blame] | 1680 | assert(PathCount != BBState::OverflowOccurredValue && |
| 1681 | "PathCount at this point can not be " |
| 1682 | "OverflowOccurredValue."); |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 1683 | NewDelta += PathCount; |
| 1684 | NewCount += PathCount; |
| 1685 | } |
| 1686 | } |
| 1687 | NewRetains.push_back(NewReleaseRetain); |
| 1688 | } |
| 1689 | } |
| 1690 | } |
| 1691 | NewReleases.clear(); |
| 1692 | if (NewRetains.empty()) break; |
| 1693 | } |
| 1694 | |
Michael Gottesman | dd60f9b | 2015-03-16 07:02:36 +0000 | [diff] [blame] | 1695 | // We can only remove pointers if we are known safe in both directions. |
| 1696 | bool UnconditionallySafe = KnownSafeTD && KnownSafeBU; |
Michael Gottesman | a76143ee | 2013-05-13 23:49:42 +0000 | [diff] [blame] | 1697 | if (UnconditionallySafe) { |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 1698 | RetainsToMove.ReverseInsertPts.clear(); |
| 1699 | ReleasesToMove.ReverseInsertPts.clear(); |
| 1700 | NewCount = 0; |
| 1701 | } else { |
| 1702 | // Determine whether the new insertion points we computed preserve the |
| 1703 | // balance of retain and release calls through the program. |
| 1704 | // TODO: If the fully aggressive solution isn't valid, try to find a |
| 1705 | // less aggressive solution which is. |
| 1706 | if (NewDelta != 0) |
| 1707 | return false; |
Michael Gottesman | e67f40c | 2013-05-24 20:44:05 +0000 | [diff] [blame] | 1708 | |
| 1709 | // At this point, we are not going to remove any RR pairs, but we still are |
| 1710 | // able to move RR pairs. If one of our pointers is afflicted with |
| 1711 | // CFGHazards, we cannot perform such code motion so exit early. |
| 1712 | const bool WillPerformCodeMotion = RetainsToMove.ReverseInsertPts.size() || |
| 1713 | ReleasesToMove.ReverseInsertPts.size(); |
| 1714 | if (CFGHazardAfflicted && WillPerformCodeMotion) |
| 1715 | return false; |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 1716 | } |
| 1717 | |
| 1718 | // Determine whether the original call points are balanced in the retain and |
| 1719 | // release calls through the program. If not, conservatively don't touch |
| 1720 | // them. |
| 1721 | // TODO: It's theoretically possible to do code motion in this case, as |
| 1722 | // long as the existing imbalances are maintained. |
| 1723 | if (OldDelta != 0) |
| 1724 | return false; |
Michael Gottesman | 8005ad3 | 2013-04-29 06:16:55 +0000 | [diff] [blame] | 1725 | |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 1726 | Changed = true; |
| 1727 | assert(OldCount != 0 && "Unreachable code?"); |
| 1728 | NumRRs += OldCount - NewCount; |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 1729 | // Set to true if we completely removed any RR pairs. |
Michael Gottesman | 8b5515f | 2013-01-22 21:53:43 +0000 | [diff] [blame] | 1730 | AnyPairsCompletelyEliminated = NewCount == 0; |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 1731 | |
| 1732 | // We can move calls! |
| 1733 | return true; |
| 1734 | } |
| 1735 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 1736 | /// Identify pairings between the retains and releases, and delete and/or move |
| 1737 | /// them. |
Michael Gottesman | 0be6920 | 2015-03-05 23:28:58 +0000 | [diff] [blame] | 1738 | bool ObjCARCOpt::PerformCodePlacement( |
| 1739 | DenseMap<const BasicBlock *, BBState> &BBStates, |
| 1740 | BlotMapVector<Value *, RRInfo> &Retains, |
| 1741 | DenseMap<Value *, RRInfo> &Releases, Module *M) { |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1742 | DEBUG(dbgs() << "\n== ObjCARCOpt::PerformCodePlacement ==\n"); |
| 1743 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1744 | bool AnyPairsCompletelyEliminated = false; |
| 1745 | RRInfo RetainsToMove; |
| 1746 | RRInfo ReleasesToMove; |
| 1747 | SmallVector<Instruction *, 4> NewRetains; |
| 1748 | SmallVector<Instruction *, 4> NewReleases; |
| 1749 | SmallVector<Instruction *, 8> DeadInsts; |
| 1750 | |
Dan Gohman | 670f937 | 2012-04-13 18:57:48 +0000 | [diff] [blame] | 1751 | // Visit each retain. |
Michael Gottesman | 0be6920 | 2015-03-05 23:28:58 +0000 | [diff] [blame] | 1752 | for (BlotMapVector<Value *, RRInfo>::const_iterator I = Retains.begin(), |
| 1753 | E = Retains.end(); |
| 1754 | I != E; ++I) { |
Dan Gohman | 2053a5d | 2011-09-29 22:25:23 +0000 | [diff] [blame] | 1755 | Value *V = I->first; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1756 | if (!V) continue; // blotted |
| 1757 | |
| 1758 | Instruction *Retain = cast<Instruction>(V); |
Michael Gottesman | c189a39 | 2013-01-09 19:23:24 +0000 | [diff] [blame] | 1759 | |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1760 | DEBUG(dbgs() << "Visiting: " << *Retain << "\n"); |
Michael Gottesman | c189a39 | 2013-01-09 19:23:24 +0000 | [diff] [blame] | 1761 | |
Michael Gottesman | e5ad66f | 2015-02-19 00:42:38 +0000 | [diff] [blame] | 1762 | Value *Arg = GetArgRCIdentityRoot(Retain); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1763 | |
Dan Gohman | 728db49 | 2012-01-13 00:39:07 +0000 | [diff] [blame] | 1764 | // 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] | 1765 | // not being managed by ObjC reference counting, so we can delete pairs |
| 1766 | // regardless of what possible decrements or uses lie between them. |
Dan Gohman | 728db49 | 2012-01-13 00:39:07 +0000 | [diff] [blame] | 1767 | bool KnownSafe = isa<Constant>(Arg) || isa<AllocaInst>(Arg); |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 1768 | |
Dan Gohman | 56e1cef | 2011-08-22 17:29:11 +0000 | [diff] [blame] | 1769 | // A constant pointer can't be pointing to an object on the heap. It may |
| 1770 | // be reference-counted, but it won't be deleted. |
| 1771 | if (const LoadInst *LI = dyn_cast<LoadInst>(Arg)) |
| 1772 | if (const GlobalVariable *GV = |
| 1773 | dyn_cast<GlobalVariable>( |
Michael Gottesman | e5ad66f | 2015-02-19 00:42:38 +0000 | [diff] [blame] | 1774 | GetRCIdentityRoot(LI->getPointerOperand()))) |
Dan Gohman | 56e1cef | 2011-08-22 17:29:11 +0000 | [diff] [blame] | 1775 | if (GV->isConstant()) |
| 1776 | KnownSafe = true; |
| 1777 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1778 | // Connect the dots between the top-down-collected RetainsToMove and |
| 1779 | // bottom-up-collected ReleasesToMove to form sets of related calls. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1780 | NewRetains.push_back(Retain); |
Michael Gottesman | 6779217 | 2015-03-16 07:02:30 +0000 | [diff] [blame] | 1781 | bool PerformMoveCalls = PairUpRetainsAndReleases( |
| 1782 | BBStates, Retains, Releases, M, NewRetains, NewReleases, DeadInsts, |
| 1783 | RetainsToMove, ReleasesToMove, Arg, KnownSafe, |
| 1784 | AnyPairsCompletelyEliminated); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1785 | |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 1786 | if (PerformMoveCalls) { |
| 1787 | // Ok, everything checks out and we're all set. Let's move/delete some |
| 1788 | // code! |
| 1789 | MoveCalls(Arg, RetainsToMove, ReleasesToMove, |
| 1790 | Retains, Releases, DeadInsts, M); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1791 | } |
| 1792 | |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 1793 | // Clean up state for next retain. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1794 | NewReleases.clear(); |
| 1795 | NewRetains.clear(); |
| 1796 | RetainsToMove.clear(); |
| 1797 | ReleasesToMove.clear(); |
| 1798 | } |
| 1799 | |
| 1800 | // Now that we're done moving everything, we can delete the newly dead |
| 1801 | // instructions, as we no longer need them as insert points. |
| 1802 | while (!DeadInsts.empty()) |
| 1803 | EraseInstruction(DeadInsts.pop_back_val()); |
| 1804 | |
| 1805 | return AnyPairsCompletelyEliminated; |
| 1806 | } |
| 1807 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 1808 | /// Weak pointer optimizations. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1809 | void ObjCARCOpt::OptimizeWeakCalls(Function &F) { |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1810 | DEBUG(dbgs() << "\n== ObjCARCOpt::OptimizeWeakCalls ==\n"); |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 1811 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1812 | // First, do memdep-style RLE and S2L optimizations. We can't use memdep |
| 1813 | // itself because it uses AliasAnalysis and we need to do provenance |
| 1814 | // queries instead. |
| 1815 | for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) { |
| 1816 | Instruction *Inst = &*I++; |
Michael Gottesman | 3f146e2 | 2013-01-01 16:05:48 +0000 | [diff] [blame] | 1817 | |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1818 | DEBUG(dbgs() << "Visiting: " << *Inst << "\n"); |
Michael Gottesman | 3f146e2 | 2013-01-01 16:05:48 +0000 | [diff] [blame] | 1819 | |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 1820 | ARCInstKind Class = GetBasicARCInstKind(Inst); |
| 1821 | if (Class != ARCInstKind::LoadWeak && |
| 1822 | Class != ARCInstKind::LoadWeakRetained) |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1823 | continue; |
| 1824 | |
| 1825 | // Delete objc_loadWeak calls with no users. |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 1826 | if (Class == ARCInstKind::LoadWeak && Inst->use_empty()) { |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1827 | Inst->eraseFromParent(); |
| 1828 | continue; |
| 1829 | } |
| 1830 | |
| 1831 | // TODO: For now, just look for an earlier available version of this value |
| 1832 | // within the same block. Theoretically, we could do memdep-style non-local |
| 1833 | // analysis too, but that would want caching. A better approach would be to |
| 1834 | // use the technique that EarlyCSE uses. |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 1835 | inst_iterator Current = std::prev(I); |
Duncan P. N. Exon Smith | 1e59a66 | 2015-10-19 23:20:14 +0000 | [diff] [blame] | 1836 | BasicBlock *CurrentBB = &*Current.getBasicBlockIterator(); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1837 | for (BasicBlock::iterator B = CurrentBB->begin(), |
| 1838 | J = Current.getInstructionIterator(); |
| 1839 | J != B; --J) { |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 1840 | Instruction *EarlierInst = &*std::prev(J); |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 1841 | ARCInstKind EarlierClass = GetARCInstKind(EarlierInst); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1842 | switch (EarlierClass) { |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 1843 | case ARCInstKind::LoadWeak: |
| 1844 | case ARCInstKind::LoadWeakRetained: { |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1845 | // If this is loading from the same pointer, replace this load's value |
| 1846 | // with that one. |
| 1847 | CallInst *Call = cast<CallInst>(Inst); |
| 1848 | CallInst *EarlierCall = cast<CallInst>(EarlierInst); |
| 1849 | Value *Arg = Call->getArgOperand(0); |
| 1850 | Value *EarlierArg = EarlierCall->getArgOperand(0); |
| 1851 | switch (PA.getAA()->alias(Arg, EarlierArg)) { |
Chandler Carruth | c3f49eb | 2015-06-22 02:16:51 +0000 | [diff] [blame] | 1852 | case MustAlias: |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1853 | Changed = true; |
| 1854 | // If the load has a builtin retain, insert a plain retain for it. |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 1855 | if (Class == ARCInstKind::LoadWeakRetained) { |
Michael Gottesman | ca3a472 | 2015-03-16 07:02:24 +0000 | [diff] [blame] | 1856 | Constant *Decl = EP.get(ARCRuntimeEntryPointKind::Retain); |
Michael Gottesman | 14acfac | 2013-07-06 01:39:23 +0000 | [diff] [blame] | 1857 | CallInst *CI = CallInst::Create(Decl, EarlierCall, "", Call); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1858 | CI->setTailCall(); |
| 1859 | } |
| 1860 | // Zap the fully redundant load. |
| 1861 | Call->replaceAllUsesWith(EarlierCall); |
| 1862 | Call->eraseFromParent(); |
| 1863 | goto clobbered; |
Chandler Carruth | c3f49eb | 2015-06-22 02:16:51 +0000 | [diff] [blame] | 1864 | case MayAlias: |
| 1865 | case PartialAlias: |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1866 | goto clobbered; |
Chandler Carruth | c3f49eb | 2015-06-22 02:16:51 +0000 | [diff] [blame] | 1867 | case NoAlias: |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1868 | break; |
| 1869 | } |
| 1870 | break; |
| 1871 | } |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 1872 | case ARCInstKind::StoreWeak: |
| 1873 | case ARCInstKind::InitWeak: { |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1874 | // If this is storing to the same pointer and has the same size etc. |
| 1875 | // replace this load's value with the stored value. |
| 1876 | CallInst *Call = cast<CallInst>(Inst); |
| 1877 | CallInst *EarlierCall = cast<CallInst>(EarlierInst); |
| 1878 | Value *Arg = Call->getArgOperand(0); |
| 1879 | Value *EarlierArg = EarlierCall->getArgOperand(0); |
| 1880 | switch (PA.getAA()->alias(Arg, EarlierArg)) { |
Chandler Carruth | c3f49eb | 2015-06-22 02:16:51 +0000 | [diff] [blame] | 1881 | case MustAlias: |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1882 | Changed = true; |
| 1883 | // If the load has a builtin retain, insert a plain retain for it. |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 1884 | if (Class == ARCInstKind::LoadWeakRetained) { |
Michael Gottesman | ca3a472 | 2015-03-16 07:02:24 +0000 | [diff] [blame] | 1885 | Constant *Decl = EP.get(ARCRuntimeEntryPointKind::Retain); |
Michael Gottesman | 14acfac | 2013-07-06 01:39:23 +0000 | [diff] [blame] | 1886 | CallInst *CI = CallInst::Create(Decl, EarlierCall, "", Call); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1887 | CI->setTailCall(); |
| 1888 | } |
| 1889 | // Zap the fully redundant load. |
| 1890 | Call->replaceAllUsesWith(EarlierCall->getArgOperand(1)); |
| 1891 | Call->eraseFromParent(); |
| 1892 | goto clobbered; |
Chandler Carruth | c3f49eb | 2015-06-22 02:16:51 +0000 | [diff] [blame] | 1893 | case MayAlias: |
| 1894 | case PartialAlias: |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1895 | goto clobbered; |
Chandler Carruth | c3f49eb | 2015-06-22 02:16:51 +0000 | [diff] [blame] | 1896 | case NoAlias: |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1897 | break; |
| 1898 | } |
| 1899 | break; |
| 1900 | } |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 1901 | case ARCInstKind::MoveWeak: |
| 1902 | case ARCInstKind::CopyWeak: |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1903 | // TOOD: Grab the copied value. |
| 1904 | goto clobbered; |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 1905 | case ARCInstKind::AutoreleasepoolPush: |
| 1906 | case ARCInstKind::None: |
| 1907 | case ARCInstKind::IntrinsicUser: |
| 1908 | case ARCInstKind::User: |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1909 | // Weak pointers are only modified through the weak entry points |
| 1910 | // (and arbitrary calls, which could call the weak entry points). |
| 1911 | break; |
| 1912 | default: |
| 1913 | // Anything else could modify the weak pointer. |
| 1914 | goto clobbered; |
| 1915 | } |
| 1916 | } |
| 1917 | clobbered:; |
| 1918 | } |
| 1919 | |
| 1920 | // Then, for each destroyWeak with an alloca operand, check to see if |
| 1921 | // the alloca and all its users can be zapped. |
| 1922 | for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) { |
| 1923 | Instruction *Inst = &*I++; |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 1924 | ARCInstKind Class = GetBasicARCInstKind(Inst); |
| 1925 | if (Class != ARCInstKind::DestroyWeak) |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1926 | continue; |
| 1927 | |
| 1928 | CallInst *Call = cast<CallInst>(Inst); |
| 1929 | Value *Arg = Call->getArgOperand(0); |
| 1930 | if (AllocaInst *Alloca = dyn_cast<AllocaInst>(Arg)) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 1931 | for (User *U : Alloca->users()) { |
| 1932 | const Instruction *UserInst = cast<Instruction>(U); |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 1933 | switch (GetBasicARCInstKind(UserInst)) { |
| 1934 | case ARCInstKind::InitWeak: |
| 1935 | case ARCInstKind::StoreWeak: |
| 1936 | case ARCInstKind::DestroyWeak: |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1937 | continue; |
| 1938 | default: |
| 1939 | goto done; |
| 1940 | } |
| 1941 | } |
| 1942 | Changed = true; |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 1943 | for (auto UI = Alloca->user_begin(), UE = Alloca->user_end(); UI != UE;) { |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1944 | CallInst *UserInst = cast<CallInst>(*UI++); |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 1945 | switch (GetBasicARCInstKind(UserInst)) { |
| 1946 | case ARCInstKind::InitWeak: |
| 1947 | case ARCInstKind::StoreWeak: |
Dan Gohman | 14862c3 | 2012-05-18 22:17:29 +0000 | [diff] [blame] | 1948 | // These functions return their second argument. |
| 1949 | UserInst->replaceAllUsesWith(UserInst->getArgOperand(1)); |
| 1950 | break; |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 1951 | case ARCInstKind::DestroyWeak: |
Dan Gohman | 14862c3 | 2012-05-18 22:17:29 +0000 | [diff] [blame] | 1952 | // No return value. |
| 1953 | break; |
| 1954 | default: |
Dan Gohman | 9c97eea0 | 2012-05-21 17:41:28 +0000 | [diff] [blame] | 1955 | llvm_unreachable("alloca really is used!"); |
Dan Gohman | 14862c3 | 2012-05-18 22:17:29 +0000 | [diff] [blame] | 1956 | } |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1957 | UserInst->eraseFromParent(); |
| 1958 | } |
| 1959 | Alloca->eraseFromParent(); |
| 1960 | done:; |
| 1961 | } |
| 1962 | } |
| 1963 | } |
| 1964 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 1965 | /// Identify program paths which execute sequences of retains and releases which |
| 1966 | /// can be eliminated. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1967 | bool ObjCARCOpt::OptimizeSequences(Function &F) { |
Michael Gottesman | 740db97 | 2013-05-23 02:35:21 +0000 | [diff] [blame] | 1968 | // Releases, Retains - These are used to store the results of the main flow |
| 1969 | // analysis. These use Value* as the key instead of Instruction* so that the |
| 1970 | // map stays valid when we get around to rewriting code and calls get |
| 1971 | // replaced by arguments. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1972 | DenseMap<Value *, RRInfo> Releases; |
Michael Gottesman | 0be6920 | 2015-03-05 23:28:58 +0000 | [diff] [blame] | 1973 | BlotMapVector<Value *, RRInfo> Retains; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1974 | |
Michael Gottesman | 740db97 | 2013-05-23 02:35:21 +0000 | [diff] [blame] | 1975 | // This is used during the traversal of the function to track the |
| 1976 | // states for each identified object at each block. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1977 | DenseMap<const BasicBlock *, BBState> BBStates; |
| 1978 | |
| 1979 | // Analyze the CFG of the function, and all instructions. |
| 1980 | bool NestingDetected = Visit(F, BBStates, Retains, Releases); |
| 1981 | |
| 1982 | // Transform. |
Michael Gottesman | 5a91bbf | 2013-05-24 20:44:02 +0000 | [diff] [blame] | 1983 | bool AnyPairsCompletelyEliminated = PerformCodePlacement(BBStates, Retains, |
| 1984 | Releases, |
| 1985 | F.getParent()); |
| 1986 | |
| 1987 | // Cleanup. |
| 1988 | MultiOwnersSet.clear(); |
| 1989 | |
| 1990 | return AnyPairsCompletelyEliminated && NestingDetected; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1991 | } |
| 1992 | |
Michael Gottesman | c2d5bf5 | 2013-04-03 23:07:45 +0000 | [diff] [blame] | 1993 | /// Check if there is a dependent call earlier that does not have anything in |
| 1994 | /// between the Retain and the call that can affect the reference count of their |
| 1995 | /// shared pointer argument. Note that Retain need not be in BB. |
Michael Gottesman | 54dc7fd | 2013-04-03 23:04:28 +0000 | [diff] [blame] | 1996 | static bool |
| 1997 | HasSafePathToPredecessorCall(const Value *Arg, Instruction *Retain, |
Craig Topper | 71b7b68 | 2014-08-21 05:55:13 +0000 | [diff] [blame] | 1998 | SmallPtrSetImpl<Instruction *> &DepInsts, |
| 1999 | SmallPtrSetImpl<const BasicBlock *> &Visited, |
Michael Gottesman | 54dc7fd | 2013-04-03 23:04:28 +0000 | [diff] [blame] | 2000 | ProvenanceAnalysis &PA) { |
| 2001 | FindDependencies(CanChangeRetainCount, Arg, Retain->getParent(), Retain, |
| 2002 | DepInsts, Visited, PA); |
| 2003 | if (DepInsts.size() != 1) |
| 2004 | return false; |
Michael Gottesman | c2d5bf5 | 2013-04-03 23:07:45 +0000 | [diff] [blame] | 2005 | |
Michael Gottesman | a9fc016 | 2015-03-05 23:29:06 +0000 | [diff] [blame] | 2006 | auto *Call = dyn_cast_or_null<CallInst>(*DepInsts.begin()); |
Michael Gottesman | c2d5bf5 | 2013-04-03 23:07:45 +0000 | [diff] [blame] | 2007 | |
Michael Gottesman | 54dc7fd | 2013-04-03 23:04:28 +0000 | [diff] [blame] | 2008 | // Check that the pointer is the return value of the call. |
| 2009 | if (!Call || Arg != Call) |
| 2010 | return false; |
Michael Gottesman | c2d5bf5 | 2013-04-03 23:07:45 +0000 | [diff] [blame] | 2011 | |
Michael Gottesman | 54dc7fd | 2013-04-03 23:04:28 +0000 | [diff] [blame] | 2012 | // Check that the call is a regular call. |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 2013 | ARCInstKind Class = GetBasicARCInstKind(Call); |
Alexander Kornienko | d0af3b3 | 2015-12-28 16:19:08 +0000 | [diff] [blame] | 2014 | return Class == ARCInstKind::CallOrUser || Class == ARCInstKind::Call; |
Michael Gottesman | 54dc7fd | 2013-04-03 23:04:28 +0000 | [diff] [blame] | 2015 | } |
| 2016 | |
Michael Gottesman | 6908db1 | 2013-04-03 23:16:05 +0000 | [diff] [blame] | 2017 | /// Find a dependent retain that precedes the given autorelease for which there |
| 2018 | /// is nothing in between the two instructions that can affect the ref count of |
| 2019 | /// Arg. |
| 2020 | static CallInst * |
| 2021 | FindPredecessorRetainWithSafePath(const Value *Arg, BasicBlock *BB, |
| 2022 | Instruction *Autorelease, |
Craig Topper | 71b7b68 | 2014-08-21 05:55:13 +0000 | [diff] [blame] | 2023 | SmallPtrSetImpl<Instruction *> &DepInsts, |
| 2024 | SmallPtrSetImpl<const BasicBlock *> &Visited, |
Michael Gottesman | 6908db1 | 2013-04-03 23:16:05 +0000 | [diff] [blame] | 2025 | ProvenanceAnalysis &PA) { |
| 2026 | FindDependencies(CanChangeRetainCount, Arg, |
| 2027 | BB, Autorelease, DepInsts, Visited, PA); |
| 2028 | if (DepInsts.size() != 1) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2029 | return nullptr; |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 2030 | |
Michael Gottesman | a9fc016 | 2015-03-05 23:29:06 +0000 | [diff] [blame] | 2031 | auto *Retain = dyn_cast_or_null<CallInst>(*DepInsts.begin()); |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 2032 | |
Michael Gottesman | 6908db1 | 2013-04-03 23:16:05 +0000 | [diff] [blame] | 2033 | // Check that we found a retain with the same argument. |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 2034 | if (!Retain || !IsRetain(GetBasicARCInstKind(Retain)) || |
Michael Gottesman | e5ad66f | 2015-02-19 00:42:38 +0000 | [diff] [blame] | 2035 | GetArgRCIdentityRoot(Retain) != Arg) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2036 | return nullptr; |
Michael Gottesman | 6908db1 | 2013-04-03 23:16:05 +0000 | [diff] [blame] | 2037 | } |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 2038 | |
Michael Gottesman | 6908db1 | 2013-04-03 23:16:05 +0000 | [diff] [blame] | 2039 | return Retain; |
| 2040 | } |
| 2041 | |
Michael Gottesman | 21a4ed3 | 2013-04-03 23:39:14 +0000 | [diff] [blame] | 2042 | /// Look for an ``autorelease'' instruction dependent on Arg such that there are |
| 2043 | /// no instructions dependent on Arg that need a positive ref count in between |
| 2044 | /// the autorelease and the ret. |
| 2045 | static CallInst * |
| 2046 | FindPredecessorAutoreleaseWithSafePath(const Value *Arg, BasicBlock *BB, |
| 2047 | ReturnInst *Ret, |
Craig Topper | 71b7b68 | 2014-08-21 05:55:13 +0000 | [diff] [blame] | 2048 | SmallPtrSetImpl<Instruction *> &DepInsts, |
| 2049 | SmallPtrSetImpl<const BasicBlock *> &V, |
Michael Gottesman | 21a4ed3 | 2013-04-03 23:39:14 +0000 | [diff] [blame] | 2050 | ProvenanceAnalysis &PA) { |
| 2051 | FindDependencies(NeedsPositiveRetainCount, Arg, |
| 2052 | BB, Ret, DepInsts, V, PA); |
| 2053 | if (DepInsts.size() != 1) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2054 | return nullptr; |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 2055 | |
Michael Gottesman | a9fc016 | 2015-03-05 23:29:06 +0000 | [diff] [blame] | 2056 | auto *Autorelease = dyn_cast_or_null<CallInst>(*DepInsts.begin()); |
Michael Gottesman | 21a4ed3 | 2013-04-03 23:39:14 +0000 | [diff] [blame] | 2057 | if (!Autorelease) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2058 | return nullptr; |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 2059 | ARCInstKind AutoreleaseClass = GetBasicARCInstKind(Autorelease); |
Michael Gottesman | 21a4ed3 | 2013-04-03 23:39:14 +0000 | [diff] [blame] | 2060 | if (!IsAutorelease(AutoreleaseClass)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2061 | return nullptr; |
Michael Gottesman | e5ad66f | 2015-02-19 00:42:38 +0000 | [diff] [blame] | 2062 | if (GetArgRCIdentityRoot(Autorelease) != Arg) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2063 | return nullptr; |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 2064 | |
Michael Gottesman | 21a4ed3 | 2013-04-03 23:39:14 +0000 | [diff] [blame] | 2065 | return Autorelease; |
| 2066 | } |
| 2067 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 2068 | /// Look for this pattern: |
Dmitri Gribenko | 5485acd | 2012-09-14 14:57:36 +0000 | [diff] [blame] | 2069 | /// \code |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2070 | /// %call = call i8* @something(...) |
| 2071 | /// %2 = call i8* @objc_retain(i8* %call) |
| 2072 | /// %3 = call i8* @objc_autorelease(i8* %2) |
| 2073 | /// ret i8* %3 |
Dmitri Gribenko | 5485acd | 2012-09-14 14:57:36 +0000 | [diff] [blame] | 2074 | /// \endcode |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2075 | /// And delete the retain and autorelease. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2076 | void ObjCARCOpt::OptimizeReturns(Function &F) { |
| 2077 | if (!F.getReturnType()->isPointerTy()) |
| 2078 | return; |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 2079 | |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 2080 | DEBUG(dbgs() << "\n== ObjCARCOpt::OptimizeReturns ==\n"); |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 2081 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2082 | SmallPtrSet<Instruction *, 4> DependingInstructions; |
| 2083 | SmallPtrSet<const BasicBlock *, 4> Visited; |
Duncan P. N. Exon Smith | 1e59a66 | 2015-10-19 23:20:14 +0000 | [diff] [blame] | 2084 | for (BasicBlock &BB: F) { |
| 2085 | ReturnInst *Ret = dyn_cast<ReturnInst>(&BB.back()); |
Michael Gottesman | 3f146e2 | 2013-01-01 16:05:48 +0000 | [diff] [blame] | 2086 | |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 2087 | DEBUG(dbgs() << "Visiting: " << *Ret << "\n"); |
Michael Gottesman | 3f146e2 | 2013-01-01 16:05:48 +0000 | [diff] [blame] | 2088 | |
Michael Gottesman | 21a4ed3 | 2013-04-03 23:39:14 +0000 | [diff] [blame] | 2089 | if (!Ret) |
| 2090 | continue; |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 2091 | |
Michael Gottesman | e5ad66f | 2015-02-19 00:42:38 +0000 | [diff] [blame] | 2092 | const Value *Arg = GetRCIdentityRoot(Ret->getOperand(0)); |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 2093 | |
Michael Gottesman | cdb7c15 | 2013-04-21 00:25:04 +0000 | [diff] [blame] | 2094 | // Look for an ``autorelease'' instruction that is a predecessor of Ret and |
Michael Gottesman | 21a4ed3 | 2013-04-03 23:39:14 +0000 | [diff] [blame] | 2095 | // dependent on Arg such that there are no instructions dependent on Arg |
| 2096 | // that need a positive ref count in between the autorelease and Ret. |
Duncan P. N. Exon Smith | 1e59a66 | 2015-10-19 23:20:14 +0000 | [diff] [blame] | 2097 | CallInst *Autorelease = FindPredecessorAutoreleaseWithSafePath( |
| 2098 | Arg, &BB, Ret, DependingInstructions, Visited, PA); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2099 | DependingInstructions.clear(); |
| 2100 | Visited.clear(); |
Michael Gottesman | fb9ece9 | 2013-04-21 00:25:01 +0000 | [diff] [blame] | 2101 | |
| 2102 | if (!Autorelease) |
| 2103 | continue; |
| 2104 | |
Duncan P. N. Exon Smith | 1e59a66 | 2015-10-19 23:20:14 +0000 | [diff] [blame] | 2105 | CallInst *Retain = FindPredecessorRetainWithSafePath( |
| 2106 | Arg, &BB, Autorelease, DependingInstructions, Visited, PA); |
Michael Gottesman | fb9ece9 | 2013-04-21 00:25:01 +0000 | [diff] [blame] | 2107 | DependingInstructions.clear(); |
| 2108 | Visited.clear(); |
| 2109 | |
| 2110 | if (!Retain) |
| 2111 | continue; |
| 2112 | |
| 2113 | // Check that there is nothing that can affect the reference count |
| 2114 | // between the retain and the call. Note that Retain need not be in BB. |
| 2115 | bool HasSafePathToCall = HasSafePathToPredecessorCall(Arg, Retain, |
| 2116 | DependingInstructions, |
| 2117 | Visited, PA); |
| 2118 | DependingInstructions.clear(); |
| 2119 | Visited.clear(); |
| 2120 | |
| 2121 | if (!HasSafePathToCall) |
| 2122 | continue; |
| 2123 | |
| 2124 | // If so, we can zap the retain and autorelease. |
| 2125 | Changed = true; |
| 2126 | ++NumRets; |
| 2127 | DEBUG(dbgs() << "Erasing: " << *Retain << "\nErasing: " |
| 2128 | << *Autorelease << "\n"); |
| 2129 | EraseInstruction(Retain); |
| 2130 | EraseInstruction(Autorelease); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2131 | } |
| 2132 | } |
| 2133 | |
Michael Gottesman | 9c11815 | 2013-04-29 06:16:57 +0000 | [diff] [blame] | 2134 | #ifndef NDEBUG |
| 2135 | void |
| 2136 | ObjCARCOpt::GatherStatistics(Function &F, bool AfterOptimization) { |
| 2137 | llvm::Statistic &NumRetains = |
| 2138 | AfterOptimization? NumRetainsAfterOpt : NumRetainsBeforeOpt; |
| 2139 | llvm::Statistic &NumReleases = |
| 2140 | AfterOptimization? NumReleasesAfterOpt : NumReleasesBeforeOpt; |
| 2141 | |
| 2142 | for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) { |
| 2143 | Instruction *Inst = &*I++; |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 2144 | switch (GetBasicARCInstKind(Inst)) { |
Michael Gottesman | 9c11815 | 2013-04-29 06:16:57 +0000 | [diff] [blame] | 2145 | default: |
| 2146 | break; |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 2147 | case ARCInstKind::Retain: |
Michael Gottesman | 9c11815 | 2013-04-29 06:16:57 +0000 | [diff] [blame] | 2148 | ++NumRetains; |
| 2149 | break; |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 2150 | case ARCInstKind::Release: |
Michael Gottesman | 9c11815 | 2013-04-29 06:16:57 +0000 | [diff] [blame] | 2151 | ++NumReleases; |
| 2152 | break; |
| 2153 | } |
| 2154 | } |
| 2155 | } |
| 2156 | #endif |
| 2157 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2158 | bool ObjCARCOpt::doInitialization(Module &M) { |
| 2159 | if (!EnableARCOpts) |
| 2160 | return false; |
| 2161 | |
Dan Gohman | 670f937 | 2012-04-13 18:57:48 +0000 | [diff] [blame] | 2162 | // If nothing in the Module uses ARC, don't do anything. |
Dan Gohman | ceaac7c | 2011-06-20 23:20:43 +0000 | [diff] [blame] | 2163 | Run = ModuleHasARC(M); |
| 2164 | if (!Run) |
| 2165 | return false; |
| 2166 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2167 | // Intuitively, objc_retain and others are nocapture, however in practice |
| 2168 | // they are not, because they return their argument value. And objc_release |
Dan Gohman | dae3349 | 2012-04-27 18:56:31 +0000 | [diff] [blame] | 2169 | // calls finalizers which can have arbitrary side effects. |
Michael Gottesman | 65cb737 | 2015-03-16 07:02:27 +0000 | [diff] [blame] | 2170 | MDKindCache.init(&M); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2171 | |
Michael Gottesman | 14acfac | 2013-07-06 01:39:23 +0000 | [diff] [blame] | 2172 | // Initialize our runtime entry point cache. |
Michael Gottesman | 65cb737 | 2015-03-16 07:02:27 +0000 | [diff] [blame] | 2173 | EP.init(&M); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2174 | |
| 2175 | return false; |
| 2176 | } |
| 2177 | |
| 2178 | bool ObjCARCOpt::runOnFunction(Function &F) { |
| 2179 | if (!EnableARCOpts) |
| 2180 | return false; |
| 2181 | |
Dan Gohman | ceaac7c | 2011-06-20 23:20:43 +0000 | [diff] [blame] | 2182 | // If nothing in the Module uses ARC, don't do anything. |
| 2183 | if (!Run) |
| 2184 | return false; |
| 2185 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2186 | Changed = false; |
| 2187 | |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 2188 | DEBUG(dbgs() << "<<< ObjCARCOpt: Visiting Function: " << F.getName() << " >>>" |
| 2189 | "\n"); |
Michael Gottesman | b24bdef | 2013-01-12 02:57:16 +0000 | [diff] [blame] | 2190 | |
Chandler Carruth | 7b560d4 | 2015-09-09 17:55:00 +0000 | [diff] [blame] | 2191 | PA.setAA(&getAnalysis<AAResultsWrapperPass>().getAAResults()); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2192 | |
Michael Gottesman | 9fc50b8 | 2013-05-13 18:29:07 +0000 | [diff] [blame] | 2193 | #ifndef NDEBUG |
| 2194 | if (AreStatisticsEnabled()) { |
| 2195 | GatherStatistics(F, false); |
| 2196 | } |
| 2197 | #endif |
| 2198 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2199 | // This pass performs several distinct transformations. As a compile-time aid |
| 2200 | // when compiling code that isn't ObjC, skip these if the relevant ObjC |
| 2201 | // library functions aren't declared. |
| 2202 | |
Michael Gottesman | cd5b027 | 2013-04-24 22:18:15 +0000 | [diff] [blame] | 2203 | // Preliminary optimizations. This also computes UsedInThisFunction. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2204 | OptimizeIndividualCalls(F); |
| 2205 | |
| 2206 | // Optimizations for weak pointers. |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 2207 | if (UsedInThisFunction & ((1 << unsigned(ARCInstKind::LoadWeak)) | |
| 2208 | (1 << unsigned(ARCInstKind::LoadWeakRetained)) | |
| 2209 | (1 << unsigned(ARCInstKind::StoreWeak)) | |
| 2210 | (1 << unsigned(ARCInstKind::InitWeak)) | |
| 2211 | (1 << unsigned(ARCInstKind::CopyWeak)) | |
| 2212 | (1 << unsigned(ARCInstKind::MoveWeak)) | |
| 2213 | (1 << unsigned(ARCInstKind::DestroyWeak)))) |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2214 | OptimizeWeakCalls(F); |
| 2215 | |
| 2216 | // Optimizations for retain+release pairs. |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 2217 | if (UsedInThisFunction & ((1 << unsigned(ARCInstKind::Retain)) | |
| 2218 | (1 << unsigned(ARCInstKind::RetainRV)) | |
| 2219 | (1 << unsigned(ARCInstKind::RetainBlock)))) |
| 2220 | if (UsedInThisFunction & (1 << unsigned(ARCInstKind::Release))) |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2221 | // Run OptimizeSequences until it either stops making changes or |
| 2222 | // no retain+release pair nesting is detected. |
| 2223 | while (OptimizeSequences(F)) {} |
| 2224 | |
| 2225 | // Optimizations if objc_autorelease is used. |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 2226 | if (UsedInThisFunction & ((1 << unsigned(ARCInstKind::Autorelease)) | |
| 2227 | (1 << unsigned(ARCInstKind::AutoreleaseRV)))) |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2228 | OptimizeReturns(F); |
| 2229 | |
Michael Gottesman | 9c11815 | 2013-04-29 06:16:57 +0000 | [diff] [blame] | 2230 | // Gather statistics after optimization. |
| 2231 | #ifndef NDEBUG |
| 2232 | if (AreStatisticsEnabled()) { |
| 2233 | GatherStatistics(F, true); |
| 2234 | } |
| 2235 | #endif |
| 2236 | |
Michael Gottesman | b24bdef | 2013-01-12 02:57:16 +0000 | [diff] [blame] | 2237 | DEBUG(dbgs() << "\n"); |
| 2238 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2239 | return Changed; |
| 2240 | } |
| 2241 | |
| 2242 | void ObjCARCOpt::releaseMemory() { |
| 2243 | PA.clear(); |
| 2244 | } |
| 2245 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 2246 | /// @} |
| 2247 | /// |