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