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