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