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