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