Michael Gottesman | 79d8d81 | 2013-01-28 01:35:51 +0000 | [diff] [blame] | 1 | //===- ObjCARCOpts.cpp - ObjC ARC Optimization ----------------------------===// |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 9 | /// \file |
| 10 | /// This file defines ObjC ARC optimizations. ARC stands for Automatic |
| 11 | /// Reference Counting and is a system for managing reference counts for objects |
| 12 | /// in Objective C. |
| 13 | /// |
| 14 | /// The optimizations performed include elimination of redundant, partially |
| 15 | /// redundant, and inconsequential reference count operations, elimination of |
Michael Gottesman | 697d8b9 | 2013-02-07 04:12:57 +0000 | [diff] [blame] | 16 | /// redundant weak pointer operations, and numerous minor simplifications. |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 17 | /// |
| 18 | /// WARNING: This file knows about certain library functions. It recognizes them |
| 19 | /// by name, and hardwires knowledge of their semantics. |
| 20 | /// |
| 21 | /// WARNING: This file knows about how certain Objective-C library functions are |
| 22 | /// used. Naive LLVM IR transformations which would otherwise be |
| 23 | /// behavior-preserving may break these assumptions. |
| 24 | /// |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 25 | //===----------------------------------------------------------------------===// |
| 26 | |
Michael Gottesman | 08904e3 | 2013-01-28 03:28:38 +0000 | [diff] [blame] | 27 | #include "ObjCARC.h" |
Michael Gottesman | 14acfac | 2013-07-06 01:39:23 +0000 | [diff] [blame] | 28 | #include "ARCRuntimeEntryPoints.h" |
Michael Gottesman | 278266f | 2013-01-29 04:20:52 +0000 | [diff] [blame] | 29 | #include "DependencyAnalysis.h" |
Michael Gottesman | 294e7da | 2013-01-28 05:51:54 +0000 | [diff] [blame] | 30 | #include "ObjCARCAliasAnalysis.h" |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 31 | #include "ProvenanceAnalysis.h" |
Michael Gottesman | 0be6920 | 2015-03-05 23:28:58 +0000 | [diff] [blame^] | 32 | #include "BlotMapVector.h" |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/DenseMap.h" |
Michael Gottesman | 5a91bbf | 2013-05-24 20:44:02 +0000 | [diff] [blame] | 34 | #include "llvm/ADT/DenseSet.h" |
Michael Gottesman | fa0939f | 2013-01-28 04:12:07 +0000 | [diff] [blame] | 35 | #include "llvm/ADT/STLExtras.h" |
Michael Gottesman | 778138e | 2013-01-29 03:03:03 +0000 | [diff] [blame] | 36 | #include "llvm/ADT/SmallPtrSet.h" |
Michael Gottesman | 278266f | 2013-01-29 04:20:52 +0000 | [diff] [blame] | 37 | #include "llvm/ADT/Statistic.h" |
Chandler Carruth | 1305dc3 | 2014-03-04 11:45:46 +0000 | [diff] [blame] | 38 | #include "llvm/IR/CFG.h" |
Michael Gottesman | cd4de0f | 2013-03-26 00:42:09 +0000 | [diff] [blame] | 39 | #include "llvm/IR/IRBuilder.h" |
Michael Gottesman | 278266f | 2013-01-29 04:20:52 +0000 | [diff] [blame] | 40 | #include "llvm/IR/LLVMContext.h" |
Michael Gottesman | 13a5f1a | 2013-01-29 04:51:59 +0000 | [diff] [blame] | 41 | #include "llvm/Support/Debug.h" |
Timur Iskhodzhanov | 5d7ff00 | 2013-01-29 09:09:27 +0000 | [diff] [blame] | 42 | #include "llvm/Support/raw_ostream.h" |
Michael Gottesman | fa0939f | 2013-01-28 04:12:07 +0000 | [diff] [blame] | 43 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 44 | using namespace llvm; |
Michael Gottesman | 08904e3 | 2013-01-28 03:28:38 +0000 | [diff] [blame] | 45 | using namespace llvm::objcarc; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 46 | |
Chandler Carruth | 964daaa | 2014-04-22 02:55:47 +0000 | [diff] [blame] | 47 | #define DEBUG_TYPE "objc-arc-opts" |
| 48 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 49 | /// \defgroup ARCUtilities Utility declarations/definitions specific to ARC. |
| 50 | /// @{ |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 51 | |
Michael Gottesman | e5ad66f | 2015-02-19 00:42:38 +0000 | [diff] [blame] | 52 | /// \brief This is similar to GetRCIdentityRoot but it stops as soon |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 53 | /// as it finds a value with multiple uses. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 54 | static const Value *FindSingleUseIdentifiedObject(const Value *Arg) { |
| 55 | if (Arg->hasOneUse()) { |
| 56 | if (const BitCastInst *BC = dyn_cast<BitCastInst>(Arg)) |
| 57 | return FindSingleUseIdentifiedObject(BC->getOperand(0)); |
| 58 | if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Arg)) |
| 59 | if (GEP->hasAllZeroIndices()) |
| 60 | return FindSingleUseIdentifiedObject(GEP->getPointerOperand()); |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 61 | if (IsForwarding(GetBasicARCInstKind(Arg))) |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 62 | return FindSingleUseIdentifiedObject( |
| 63 | cast<CallInst>(Arg)->getArgOperand(0)); |
| 64 | if (!IsObjCIdentifiedObject(Arg)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 65 | return nullptr; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 66 | return Arg; |
| 67 | } |
| 68 | |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 69 | // If we found an identifiable object but it has multiple uses, but they are |
| 70 | // 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] | 71 | if (IsObjCIdentifiedObject(Arg)) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 72 | for (const User *U : Arg->users()) |
Michael Gottesman | e5ad66f | 2015-02-19 00:42:38 +0000 | [diff] [blame] | 73 | if (!U->use_empty() || GetRCIdentityRoot(U) != Arg) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 74 | return nullptr; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 75 | |
| 76 | return Arg; |
| 77 | } |
| 78 | |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 79 | return nullptr; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Michael Gottesman | a76143ee | 2013-05-13 23:49:42 +0000 | [diff] [blame] | 82 | /// This is a wrapper around getUnderlyingObjCPtr along the lines of |
| 83 | /// GetUnderlyingObjects except that it returns early when it sees the first |
| 84 | /// alloca. |
| 85 | static inline bool AreAnyUnderlyingObjectsAnAlloca(const Value *V) { |
| 86 | SmallPtrSet<const Value *, 4> Visited; |
| 87 | SmallVector<const Value *, 4> Worklist; |
| 88 | Worklist.push_back(V); |
| 89 | do { |
| 90 | const Value *P = Worklist.pop_back_val(); |
| 91 | P = GetUnderlyingObjCPtr(P); |
Michael Gottesman | 0c8b562 | 2013-05-14 06:40:10 +0000 | [diff] [blame] | 92 | |
Michael Gottesman | a76143ee | 2013-05-13 23:49:42 +0000 | [diff] [blame] | 93 | if (isa<AllocaInst>(P)) |
| 94 | return true; |
Michael Gottesman | 0c8b562 | 2013-05-14 06:40:10 +0000 | [diff] [blame] | 95 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 96 | if (!Visited.insert(P).second) |
Michael Gottesman | a76143ee | 2013-05-13 23:49:42 +0000 | [diff] [blame] | 97 | continue; |
Michael Gottesman | 0c8b562 | 2013-05-14 06:40:10 +0000 | [diff] [blame] | 98 | |
Michael Gottesman | a76143ee | 2013-05-13 23:49:42 +0000 | [diff] [blame] | 99 | if (const SelectInst *SI = dyn_cast<const SelectInst>(P)) { |
| 100 | Worklist.push_back(SI->getTrueValue()); |
| 101 | Worklist.push_back(SI->getFalseValue()); |
| 102 | continue; |
| 103 | } |
Michael Gottesman | 0c8b562 | 2013-05-14 06:40:10 +0000 | [diff] [blame] | 104 | |
Michael Gottesman | a76143ee | 2013-05-13 23:49:42 +0000 | [diff] [blame] | 105 | if (const PHINode *PN = dyn_cast<const PHINode>(P)) { |
| 106 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 107 | Worklist.push_back(PN->getIncomingValue(i)); |
| 108 | continue; |
| 109 | } |
| 110 | } while (!Worklist.empty()); |
| 111 | |
| 112 | return false; |
| 113 | } |
| 114 | |
| 115 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 116 | /// @} |
| 117 | /// |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 118 | /// \defgroup ARCOpt ARC Optimization. |
| 119 | /// @{ |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 120 | |
| 121 | // TODO: On code like this: |
| 122 | // |
| 123 | // objc_retain(%x) |
| 124 | // stuff_that_cannot_release() |
| 125 | // objc_autorelease(%x) |
| 126 | // stuff_that_cannot_release() |
| 127 | // objc_retain(%x) |
| 128 | // stuff_that_cannot_release() |
| 129 | // objc_autorelease(%x) |
| 130 | // |
| 131 | // The second retain and autorelease can be deleted. |
| 132 | |
| 133 | // TODO: It should be possible to delete |
| 134 | // objc_autoreleasePoolPush and objc_autoreleasePoolPop |
| 135 | // pairs if nothing is actually autoreleased between them. Also, autorelease |
| 136 | // calls followed by objc_autoreleasePoolPop calls (perhaps in ObjC++ code |
| 137 | // after inlining) can be turned into plain release calls. |
| 138 | |
| 139 | // TODO: Critical-edge splitting. If the optimial insertion point is |
| 140 | // a critical edge, the current algorithm has to fail, because it doesn't |
| 141 | // know how to split edges. It should be possible to make the optimizer |
| 142 | // think in terms of edges, rather than blocks, and then split critical |
| 143 | // edges on demand. |
| 144 | |
| 145 | // TODO: OptimizeSequences could generalized to be Interprocedural. |
| 146 | |
| 147 | // TODO: Recognize that a bunch of other objc runtime calls have |
| 148 | // non-escaping arguments and non-releasing arguments, and may be |
| 149 | // non-autoreleasing. |
| 150 | |
| 151 | // TODO: Sink autorelease calls as far as possible. Unfortunately we |
| 152 | // usually can't sink them past other calls, which would be the main |
| 153 | // case where it would be useful. |
| 154 | |
Dan Gohman | b389401 | 2011-08-19 00:26:36 +0000 | [diff] [blame] | 155 | // TODO: The pointer returned from objc_loadWeakRetained is retained. |
| 156 | |
| 157 | // TODO: Delete release+retain pairs (rare). |
Dan Gohman | ceaac7c | 2011-06-20 23:20:43 +0000 | [diff] [blame] | 158 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 159 | STATISTIC(NumNoops, "Number of no-op objc calls eliminated"); |
| 160 | STATISTIC(NumPartialNoops, "Number of partially no-op objc calls eliminated"); |
| 161 | STATISTIC(NumAutoreleases,"Number of autoreleases converted to releases"); |
| 162 | STATISTIC(NumRets, "Number of return value forwarding " |
Michael Gottesman | b4e7f4d | 2013-05-15 17:43:03 +0000 | [diff] [blame] | 163 | "retain+autoreleases eliminated"); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 164 | STATISTIC(NumRRs, "Number of retain+release paths eliminated"); |
| 165 | STATISTIC(NumPeeps, "Number of calls peephole-optimized"); |
Matt Beaumont-Gay | e55d949 | 2013-05-13 21:10:49 +0000 | [diff] [blame] | 166 | #ifndef NDEBUG |
Michael Gottesman | 9c11815 | 2013-04-29 06:16:57 +0000 | [diff] [blame] | 167 | STATISTIC(NumRetainsBeforeOpt, |
Michael Gottesman | b4e7f4d | 2013-05-15 17:43:03 +0000 | [diff] [blame] | 168 | "Number of retains before optimization"); |
Michael Gottesman | 9c11815 | 2013-04-29 06:16:57 +0000 | [diff] [blame] | 169 | STATISTIC(NumReleasesBeforeOpt, |
Michael Gottesman | b4e7f4d | 2013-05-15 17:43:03 +0000 | [diff] [blame] | 170 | "Number of releases before optimization"); |
Michael Gottesman | 9c11815 | 2013-04-29 06:16:57 +0000 | [diff] [blame] | 171 | STATISTIC(NumRetainsAfterOpt, |
Michael Gottesman | b4e7f4d | 2013-05-15 17:43:03 +0000 | [diff] [blame] | 172 | "Number of retains after optimization"); |
Michael Gottesman | 9c11815 | 2013-04-29 06:16:57 +0000 | [diff] [blame] | 173 | STATISTIC(NumReleasesAfterOpt, |
Michael Gottesman | b4e7f4d | 2013-05-15 17:43:03 +0000 | [diff] [blame] | 174 | "Number of releases after optimization"); |
Michael Gottesman | 03cf3c8 | 2013-04-29 07:29:08 +0000 | [diff] [blame] | 175 | #endif |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 176 | |
| 177 | namespace { |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 178 | /// \enum Sequence |
| 179 | /// |
| 180 | /// \brief A sequence of states that a pointer may go through in which an |
| 181 | /// objc_retain and objc_release are actually needed. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 182 | enum Sequence { |
| 183 | S_None, |
Michael Gottesman | 53fd20b | 2013-01-29 21:07:51 +0000 | [diff] [blame] | 184 | S_Retain, ///< objc_retain(x). |
| 185 | S_CanRelease, ///< foo(x) -- x could possibly see a ref count decrement. |
| 186 | S_Use, ///< any use of x. |
Michael Gottesman | 386241c | 2013-01-29 21:39:02 +0000 | [diff] [blame] | 187 | S_Stop, ///< like S_Release, but code motion is stopped. |
Michael Gottesman | 53fd20b | 2013-01-29 21:07:51 +0000 | [diff] [blame] | 188 | S_Release, ///< objc_release(x). |
Michael Gottesman | 9bdab2b | 2013-01-29 21:41:44 +0000 | [diff] [blame] | 189 | S_MovableRelease ///< objc_release(x), !clang.imprecise_release. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 190 | }; |
Michael Gottesman | 53fd20b | 2013-01-29 21:07:51 +0000 | [diff] [blame] | 191 | |
| 192 | raw_ostream &operator<<(raw_ostream &OS, const Sequence S) |
| 193 | LLVM_ATTRIBUTE_UNUSED; |
| 194 | raw_ostream &operator<<(raw_ostream &OS, const Sequence S) { |
| 195 | switch (S) { |
| 196 | case S_None: |
| 197 | return OS << "S_None"; |
| 198 | case S_Retain: |
| 199 | return OS << "S_Retain"; |
| 200 | case S_CanRelease: |
| 201 | return OS << "S_CanRelease"; |
| 202 | case S_Use: |
| 203 | return OS << "S_Use"; |
| 204 | case S_Release: |
| 205 | return OS << "S_Release"; |
| 206 | case S_MovableRelease: |
| 207 | return OS << "S_MovableRelease"; |
| 208 | case S_Stop: |
| 209 | return OS << "S_Stop"; |
| 210 | } |
| 211 | llvm_unreachable("Unknown sequence type."); |
| 212 | } |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | static Sequence MergeSeqs(Sequence A, Sequence B, bool TopDown) { |
| 216 | // The easy cases. |
| 217 | if (A == B) |
| 218 | return A; |
| 219 | if (A == S_None || B == S_None) |
| 220 | return S_None; |
| 221 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 222 | if (A > B) std::swap(A, B); |
| 223 | if (TopDown) { |
| 224 | // Choose the side which is further along in the sequence. |
Dan Gohman | 1213027 | 2011-08-12 00:26:31 +0000 | [diff] [blame] | 225 | if ((A == S_Retain || A == S_CanRelease) && |
| 226 | (B == S_CanRelease || B == S_Use)) |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 227 | return B; |
| 228 | } else { |
| 229 | // Choose the side which is further along in the sequence. |
| 230 | if ((A == S_Use || A == S_CanRelease) && |
Dan Gohman | 1213027 | 2011-08-12 00:26:31 +0000 | [diff] [blame] | 231 | (B == S_Use || B == S_Release || B == S_Stop || B == S_MovableRelease)) |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 232 | return A; |
| 233 | // If both sides are releases, choose the more conservative one. |
| 234 | if (A == S_Stop && (B == S_Release || B == S_MovableRelease)) |
| 235 | return A; |
| 236 | if (A == S_Release && B == S_MovableRelease) |
| 237 | return A; |
| 238 | } |
| 239 | |
| 240 | return S_None; |
| 241 | } |
| 242 | |
| 243 | namespace { |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 244 | /// \brief Unidirectional information about either a |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 245 | /// retain-decrement-use-release sequence or release-use-decrement-retain |
Bob Wilson | 798a770 | 2013-04-09 22:15:51 +0000 | [diff] [blame] | 246 | /// reverse sequence. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 247 | struct RRInfo { |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 248 | /// After an objc_retain, the reference count of the referenced |
Dan Gohman | b389401 | 2011-08-19 00:26:36 +0000 | [diff] [blame] | 249 | /// object is known to be positive. Similarly, before an objc_release, the |
| 250 | /// reference count of the referenced object is known to be positive. If |
| 251 | /// there are retain-release pairs in code regions where the retain count |
| 252 | /// is known to be positive, they can be eliminated, regardless of any side |
| 253 | /// effects between them. |
| 254 | /// |
| 255 | /// Also, a retain+release pair nested within another retain+release |
| 256 | /// pair all on the known same pointer value can be eliminated, regardless |
| 257 | /// of any intervening side effects. |
| 258 | /// |
| 259 | /// KnownSafe is true when either of these conditions is satisfied. |
| 260 | bool KnownSafe; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 261 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 262 | /// True of the objc_release calls are all marked with the "tail" keyword. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 263 | bool IsTailCallRelease; |
| 264 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 265 | /// If the Calls are objc_release calls and they all have a |
| 266 | /// clang.imprecise_release tag, this is the metadata tag. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 267 | MDNode *ReleaseMetadata; |
| 268 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 269 | /// For a top-down sequence, the set of objc_retains or |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 270 | /// objc_retainBlocks. For bottom-up, the set of objc_releases. |
| 271 | SmallPtrSet<Instruction *, 2> Calls; |
| 272 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 273 | /// The set of optimal insert positions for moving calls in the opposite |
| 274 | /// sequence. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 275 | SmallPtrSet<Instruction *, 2> ReverseInsertPts; |
| 276 | |
Michael Gottesman | e67f40c | 2013-05-24 20:44:05 +0000 | [diff] [blame] | 277 | /// If this is true, we cannot perform code motion but can still remove |
| 278 | /// retain/release pairs. |
| 279 | bool CFGHazardAfflicted; |
| 280 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 281 | RRInfo() : |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 282 | KnownSafe(false), IsTailCallRelease(false), ReleaseMetadata(nullptr), |
Michael Gottesman | e67f40c | 2013-05-24 20:44:05 +0000 | [diff] [blame] | 283 | CFGHazardAfflicted(false) {} |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 284 | |
| 285 | void clear(); |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 286 | |
Michael Gottesman | 4773a10 | 2013-06-21 05:42:08 +0000 | [diff] [blame] | 287 | /// Conservatively merge the two RRInfo. Returns true if a partial merge has |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 288 | /// occurred, false otherwise. |
Michael Gottesman | 4773a10 | 2013-06-21 05:42:08 +0000 | [diff] [blame] | 289 | bool Merge(const RRInfo &Other); |
| 290 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 291 | }; |
| 292 | } |
| 293 | |
| 294 | void RRInfo::clear() { |
Dan Gohman | b389401 | 2011-08-19 00:26:36 +0000 | [diff] [blame] | 295 | KnownSafe = false; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 296 | IsTailCallRelease = false; |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 297 | ReleaseMetadata = nullptr; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 298 | Calls.clear(); |
| 299 | ReverseInsertPts.clear(); |
Michael Gottesman | e67f40c | 2013-05-24 20:44:05 +0000 | [diff] [blame] | 300 | CFGHazardAfflicted = false; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 301 | } |
| 302 | |
Michael Gottesman | 4773a10 | 2013-06-21 05:42:08 +0000 | [diff] [blame] | 303 | bool RRInfo::Merge(const RRInfo &Other) { |
| 304 | // Conservatively merge the ReleaseMetadata information. |
| 305 | if (ReleaseMetadata != Other.ReleaseMetadata) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 306 | ReleaseMetadata = nullptr; |
Michael Gottesman | 4773a10 | 2013-06-21 05:42:08 +0000 | [diff] [blame] | 307 | |
| 308 | // Conservatively merge the boolean state. |
| 309 | KnownSafe &= Other.KnownSafe; |
| 310 | IsTailCallRelease &= Other.IsTailCallRelease; |
| 311 | CFGHazardAfflicted |= Other.CFGHazardAfflicted; |
| 312 | |
| 313 | // Merge the call sets. |
| 314 | Calls.insert(Other.Calls.begin(), Other.Calls.end()); |
| 315 | |
| 316 | // Merge the insert point sets. If there are any differences, |
| 317 | // that makes this a partial merge. |
| 318 | bool Partial = ReverseInsertPts.size() != Other.ReverseInsertPts.size(); |
Craig Topper | 4627679 | 2014-08-24 23:23:06 +0000 | [diff] [blame] | 319 | for (Instruction *Inst : Other.ReverseInsertPts) |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 320 | Partial |= ReverseInsertPts.insert(Inst).second; |
Michael Gottesman | 4773a10 | 2013-06-21 05:42:08 +0000 | [diff] [blame] | 321 | return Partial; |
| 322 | } |
| 323 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 324 | namespace { |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 325 | /// \brief This class summarizes several per-pointer runtime properties which |
| 326 | /// are propogated through the flow graph. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 327 | class PtrState { |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 328 | /// True if the reference count is known to be incremented. |
Dan Gohman | 62079b4 | 2012-04-25 00:50:46 +0000 | [diff] [blame] | 329 | bool KnownPositiveRefCount; |
| 330 | |
Bob Wilson | 798a770 | 2013-04-09 22:15:51 +0000 | [diff] [blame] | 331 | /// True if we've seen an opportunity for partial RR elimination, such as |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 332 | /// pushing calls into a CFG triangle or into one side of a CFG diamond. |
Dan Gohman | 62079b4 | 2012-04-25 00:50:46 +0000 | [diff] [blame] | 333 | bool Partial; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 334 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 335 | /// The current position in the sequence. |
Bill Wendling | 2798f1e | 2013-12-01 03:36:07 +0000 | [diff] [blame] | 336 | unsigned char Seq : 8; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 337 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 338 | /// Unidirectional information about the current sequence. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 339 | RRInfo RRI; |
| 340 | |
Michael Gottesman | e3943d0 | 2013-06-21 19:44:30 +0000 | [diff] [blame] | 341 | public: |
Dan Gohman | df476e5 | 2012-09-04 23:16:20 +0000 | [diff] [blame] | 342 | PtrState() : KnownPositiveRefCount(false), Partial(false), |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 343 | Seq(S_None) {} |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 344 | |
Michael Gottesman | 9313225 | 2013-06-21 06:59:02 +0000 | [diff] [blame] | 345 | |
| 346 | bool IsKnownSafe() const { |
Michael Gottesman | 01df450 | 2013-07-06 01:41:35 +0000 | [diff] [blame] | 347 | return RRI.KnownSafe; |
Michael Gottesman | 9313225 | 2013-06-21 06:59:02 +0000 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | void SetKnownSafe(const bool NewValue) { |
| 351 | RRI.KnownSafe = NewValue; |
| 352 | } |
| 353 | |
Michael Gottesman | b82a179 | 2013-06-21 07:00:44 +0000 | [diff] [blame] | 354 | bool IsTailCallRelease() const { |
| 355 | return RRI.IsTailCallRelease; |
| 356 | } |
| 357 | |
| 358 | void SetTailCallRelease(const bool NewValue) { |
| 359 | RRI.IsTailCallRelease = NewValue; |
| 360 | } |
| 361 | |
Michael Gottesman | 9799cf7 | 2013-06-21 20:52:49 +0000 | [diff] [blame] | 362 | bool IsTrackingImpreciseReleases() const { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 363 | return RRI.ReleaseMetadata != nullptr; |
Michael Gottesman | f040118 | 2013-06-21 19:12:38 +0000 | [diff] [blame] | 364 | } |
| 365 | |
Michael Gottesman | f701d3f | 2013-06-21 07:03:07 +0000 | [diff] [blame] | 366 | const MDNode *GetReleaseMetadata() const { |
| 367 | return RRI.ReleaseMetadata; |
| 368 | } |
| 369 | |
| 370 | void SetReleaseMetadata(MDNode *NewValue) { |
| 371 | RRI.ReleaseMetadata = NewValue; |
| 372 | } |
| 373 | |
Michael Gottesman | 2f29459 | 2013-06-21 19:12:36 +0000 | [diff] [blame] | 374 | bool IsCFGHazardAfflicted() const { |
| 375 | return RRI.CFGHazardAfflicted; |
| 376 | } |
| 377 | |
| 378 | void SetCFGHazardAfflicted(const bool NewValue) { |
| 379 | RRI.CFGHazardAfflicted = NewValue; |
| 380 | } |
| 381 | |
Michael Gottesman | 415ddd7 | 2013-02-05 19:32:18 +0000 | [diff] [blame] | 382 | void SetKnownPositiveRefCount() { |
Michael Gottesman | f3f9e3b | 2013-05-14 00:08:09 +0000 | [diff] [blame] | 383 | DEBUG(dbgs() << "Setting Known Positive.\n"); |
Dan Gohman | 62079b4 | 2012-04-25 00:50:46 +0000 | [diff] [blame] | 384 | KnownPositiveRefCount = true; |
Dan Gohman | 1213027 | 2011-08-12 00:26:31 +0000 | [diff] [blame] | 385 | } |
| 386 | |
Michael Gottesman | 764b1cf | 2013-03-23 05:46:19 +0000 | [diff] [blame] | 387 | void ClearKnownPositiveRefCount() { |
Michael Gottesman | f3f9e3b | 2013-05-14 00:08:09 +0000 | [diff] [blame] | 388 | DEBUG(dbgs() << "Clearing Known Positive.\n"); |
Dan Gohman | 62079b4 | 2012-04-25 00:50:46 +0000 | [diff] [blame] | 389 | KnownPositiveRefCount = false; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Michael Gottesman | 07beea4 | 2013-03-23 05:31:01 +0000 | [diff] [blame] | 392 | bool HasKnownPositiveRefCount() const { |
Dan Gohman | 62079b4 | 2012-04-25 00:50:46 +0000 | [diff] [blame] | 393 | return KnownPositiveRefCount; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 394 | } |
| 395 | |
Michael Gottesman | 415ddd7 | 2013-02-05 19:32:18 +0000 | [diff] [blame] | 396 | void SetSeq(Sequence NewSeq) { |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 397 | DEBUG(dbgs() << "Old: " << Seq << "; New: " << NewSeq << "\n"); |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 398 | Seq = NewSeq; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 399 | } |
| 400 | |
Michael Gottesman | 415ddd7 | 2013-02-05 19:32:18 +0000 | [diff] [blame] | 401 | Sequence GetSeq() const { |
Bill Wendling | 2798f1e | 2013-12-01 03:36:07 +0000 | [diff] [blame] | 402 | return static_cast<Sequence>(Seq); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Michael Gottesman | 415ddd7 | 2013-02-05 19:32:18 +0000 | [diff] [blame] | 405 | void ClearSequenceProgress() { |
Dan Gohman | 62079b4 | 2012-04-25 00:50:46 +0000 | [diff] [blame] | 406 | ResetSequenceProgress(S_None); |
| 407 | } |
| 408 | |
Michael Gottesman | 415ddd7 | 2013-02-05 19:32:18 +0000 | [diff] [blame] | 409 | void ResetSequenceProgress(Sequence NewSeq) { |
Michael Gottesman | 01338a4 | 2013-04-20 23:36:57 +0000 | [diff] [blame] | 410 | DEBUG(dbgs() << "Resetting sequence progress.\n"); |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 411 | SetSeq(NewSeq); |
Dan Gohman | 62079b4 | 2012-04-25 00:50:46 +0000 | [diff] [blame] | 412 | Partial = false; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 413 | RRI.clear(); |
| 414 | } |
| 415 | |
| 416 | void Merge(const PtrState &Other, bool TopDown); |
Michael Gottesman | 4f6ef11 | 2013-06-21 19:44:27 +0000 | [diff] [blame] | 417 | |
| 418 | void InsertCall(Instruction *I) { |
| 419 | RRI.Calls.insert(I); |
| 420 | } |
| 421 | |
| 422 | void InsertReverseInsertPt(Instruction *I) { |
| 423 | RRI.ReverseInsertPts.insert(I); |
| 424 | } |
| 425 | |
| 426 | void ClearReverseInsertPts() { |
| 427 | RRI.ReverseInsertPts.clear(); |
| 428 | } |
| 429 | |
| 430 | bool HasReverseInsertPts() const { |
| 431 | return !RRI.ReverseInsertPts.empty(); |
| 432 | } |
Michael Gottesman | e3943d0 | 2013-06-21 19:44:30 +0000 | [diff] [blame] | 433 | |
| 434 | const RRInfo &GetRRInfo() const { |
| 435 | return RRI; |
| 436 | } |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 437 | }; |
| 438 | } |
| 439 | |
| 440 | void |
| 441 | PtrState::Merge(const PtrState &Other, bool TopDown) { |
Bill Wendling | cbcb02c | 2013-12-01 03:40:42 +0000 | [diff] [blame] | 442 | Seq = MergeSeqs(GetSeq(), Other.GetSeq(), TopDown); |
Michael Gottesman | b7deb4c | 2013-06-21 06:54:31 +0000 | [diff] [blame] | 443 | KnownPositiveRefCount &= Other.KnownPositiveRefCount; |
Michael Gottesman | 60f6b28 | 2013-03-29 05:13:07 +0000 | [diff] [blame] | 444 | |
Dan Gohman | 1736c14 | 2011-10-17 18:48:25 +0000 | [diff] [blame] | 445 | // If we're not in a sequence (anymore), drop all associated state. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 446 | if (Seq == S_None) { |
Dan Gohman | 62079b4 | 2012-04-25 00:50:46 +0000 | [diff] [blame] | 447 | Partial = false; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 448 | RRI.clear(); |
Dan Gohman | 62079b4 | 2012-04-25 00:50:46 +0000 | [diff] [blame] | 449 | } else if (Partial || Other.Partial) { |
Dan Gohman | 1736c14 | 2011-10-17 18:48:25 +0000 | [diff] [blame] | 450 | // If we're doing a merge on a path that's previously seen a partial |
| 451 | // merge, conservatively drop the sequence, to avoid doing partial |
| 452 | // RR elimination. If the branch predicates for the two merge differ, |
| 453 | // mixing them is unsafe. |
Dan Gohman | 62079b4 | 2012-04-25 00:50:46 +0000 | [diff] [blame] | 454 | ClearSequenceProgress(); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 455 | } else { |
Michael Gottesman | b7deb4c | 2013-06-21 06:54:31 +0000 | [diff] [blame] | 456 | // Otherwise merge the other PtrState's RRInfo into our RRInfo. At this |
| 457 | // point, we know that currently we are not partial. Stash whether or not |
| 458 | // the merge operation caused us to undergo a partial merging of reverse |
| 459 | // insertion points. |
Michael Gottesman | 4773a10 | 2013-06-21 05:42:08 +0000 | [diff] [blame] | 460 | Partial = RRI.Merge(Other.RRI); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 461 | } |
| 462 | } |
| 463 | |
| 464 | namespace { |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 465 | /// \brief Per-BasicBlock state. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 466 | class BBState { |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 467 | /// The number of unique control paths from the entry which can reach this |
| 468 | /// block. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 469 | unsigned TopDownPathCount; |
| 470 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 471 | /// The number of unique control paths to exits from this block. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 472 | unsigned BottomUpPathCount; |
| 473 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 474 | /// A type for PerPtrTopDown and PerPtrBottomUp. |
Michael Gottesman | 0be6920 | 2015-03-05 23:28:58 +0000 | [diff] [blame^] | 475 | typedef BlotMapVector<const Value *, PtrState> MapTy; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 476 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 477 | /// The top-down traversal uses this to record information known about a |
| 478 | /// pointer at the bottom of each block. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 479 | MapTy PerPtrTopDown; |
| 480 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 481 | /// The bottom-up traversal uses this to record information known about a |
| 482 | /// pointer at the top of each block. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 483 | MapTy PerPtrBottomUp; |
| 484 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 485 | /// Effective predecessors of the current block ignoring ignorable edges and |
| 486 | /// ignored backedges. |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 487 | SmallVector<BasicBlock *, 2> Preds; |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 488 | /// Effective successors of the current block ignoring ignorable edges and |
| 489 | /// ignored backedges. |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 490 | SmallVector<BasicBlock *, 2> Succs; |
| 491 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 492 | public: |
Michael Gottesman | d6ce6cb | 2013-08-09 23:22:27 +0000 | [diff] [blame] | 493 | static const unsigned OverflowOccurredValue; |
| 494 | |
| 495 | BBState() : TopDownPathCount(0), BottomUpPathCount(0) { } |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 496 | |
| 497 | typedef MapTy::iterator ptr_iterator; |
| 498 | typedef MapTy::const_iterator ptr_const_iterator; |
| 499 | |
| 500 | ptr_iterator top_down_ptr_begin() { return PerPtrTopDown.begin(); } |
| 501 | ptr_iterator top_down_ptr_end() { return PerPtrTopDown.end(); } |
| 502 | ptr_const_iterator top_down_ptr_begin() const { |
| 503 | return PerPtrTopDown.begin(); |
| 504 | } |
| 505 | ptr_const_iterator top_down_ptr_end() const { |
| 506 | return PerPtrTopDown.end(); |
| 507 | } |
| 508 | |
| 509 | ptr_iterator bottom_up_ptr_begin() { return PerPtrBottomUp.begin(); } |
| 510 | ptr_iterator bottom_up_ptr_end() { return PerPtrBottomUp.end(); } |
| 511 | ptr_const_iterator bottom_up_ptr_begin() const { |
| 512 | return PerPtrBottomUp.begin(); |
| 513 | } |
| 514 | ptr_const_iterator bottom_up_ptr_end() const { |
| 515 | return PerPtrBottomUp.end(); |
| 516 | } |
| 517 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 518 | /// Mark this block as being an entry block, which has one path from the |
| 519 | /// entry by definition. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 520 | void SetAsEntry() { TopDownPathCount = 1; } |
| 521 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 522 | /// Mark this block as being an exit block, which has one path to an exit by |
| 523 | /// definition. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 524 | void SetAsExit() { BottomUpPathCount = 1; } |
| 525 | |
Michael Gottesman | 993fbf7 | 2013-05-13 19:40:39 +0000 | [diff] [blame] | 526 | /// Attempt to find the PtrState object describing the top down state for |
| 527 | /// pointer Arg. Return a new initialized PtrState describing the top down |
| 528 | /// state for Arg if we do not find one. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 529 | PtrState &getPtrTopDownState(const Value *Arg) { |
| 530 | return PerPtrTopDown[Arg]; |
| 531 | } |
| 532 | |
Michael Gottesman | 993fbf7 | 2013-05-13 19:40:39 +0000 | [diff] [blame] | 533 | /// Attempt to find the PtrState object describing the bottom up state for |
| 534 | /// pointer Arg. Return a new initialized PtrState describing the bottom up |
| 535 | /// state for Arg if we do not find one. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 536 | PtrState &getPtrBottomUpState(const Value *Arg) { |
| 537 | return PerPtrBottomUp[Arg]; |
| 538 | } |
| 539 | |
Michael Gottesman | a76143ee | 2013-05-13 23:49:42 +0000 | [diff] [blame] | 540 | /// Attempt to find the PtrState object describing the bottom up state for |
| 541 | /// pointer Arg. |
| 542 | ptr_iterator findPtrBottomUpState(const Value *Arg) { |
| 543 | return PerPtrBottomUp.find(Arg); |
| 544 | } |
| 545 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 546 | void clearBottomUpPointers() { |
Evan Cheng | e4df6a2 | 2011-08-04 18:40:26 +0000 | [diff] [blame] | 547 | PerPtrBottomUp.clear(); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | void clearTopDownPointers() { |
| 551 | PerPtrTopDown.clear(); |
| 552 | } |
| 553 | |
| 554 | void InitFromPred(const BBState &Other); |
| 555 | void InitFromSucc(const BBState &Other); |
| 556 | void MergePred(const BBState &Other); |
| 557 | void MergeSucc(const BBState &Other); |
| 558 | |
Michael Gottesman | 9e7261c | 2013-06-07 06:16:49 +0000 | [diff] [blame] | 559 | /// 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] | 560 | /// which pass through this block. This is only valid after both the |
| 561 | /// top-down and bottom-up traversals are complete. |
Michael Gottesman | 9e7261c | 2013-06-07 06:16:49 +0000 | [diff] [blame] | 562 | /// |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 563 | /// Returns true if overflow occurred. Returns false if overflow did not |
Michael Gottesman | 9e7261c | 2013-06-07 06:16:49 +0000 | [diff] [blame] | 564 | /// occur. |
| 565 | bool GetAllPathCountWithOverflow(unsigned &PathCount) const { |
Michael Gottesman | d6ce6cb | 2013-08-09 23:22:27 +0000 | [diff] [blame] | 566 | if (TopDownPathCount == OverflowOccurredValue || |
| 567 | BottomUpPathCount == OverflowOccurredValue) |
| 568 | return true; |
Michael Gottesman | 9e7261c | 2013-06-07 06:16:49 +0000 | [diff] [blame] | 569 | unsigned long long Product = |
| 570 | (unsigned long long)TopDownPathCount*BottomUpPathCount; |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 571 | // 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] | 572 | // the lower bits of Product are all set. |
| 573 | return (Product >> 32) || |
| 574 | ((PathCount = Product) == OverflowOccurredValue); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 575 | } |
Dan Gohman | 1213027 | 2011-08-12 00:26:31 +0000 | [diff] [blame] | 576 | |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 577 | // Specialized CFG utilities. |
Dan Gohman | dae3349 | 2012-04-27 18:56:31 +0000 | [diff] [blame] | 578 | typedef SmallVectorImpl<BasicBlock *>::const_iterator edge_iterator; |
Michael Gottesman | 0fecf98 | 2013-08-07 23:56:34 +0000 | [diff] [blame] | 579 | edge_iterator pred_begin() const { return Preds.begin(); } |
| 580 | edge_iterator pred_end() const { return Preds.end(); } |
| 581 | edge_iterator succ_begin() const { return Succs.begin(); } |
| 582 | edge_iterator succ_end() const { return Succs.end(); } |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 583 | |
| 584 | void addSucc(BasicBlock *Succ) { Succs.push_back(Succ); } |
| 585 | void addPred(BasicBlock *Pred) { Preds.push_back(Pred); } |
| 586 | |
| 587 | bool isExit() const { return Succs.empty(); } |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 588 | }; |
Michael Gottesman | d6ce6cb | 2013-08-09 23:22:27 +0000 | [diff] [blame] | 589 | |
| 590 | const unsigned BBState::OverflowOccurredValue = 0xffffffff; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 591 | } |
| 592 | |
| 593 | void BBState::InitFromPred(const BBState &Other) { |
| 594 | PerPtrTopDown = Other.PerPtrTopDown; |
| 595 | TopDownPathCount = Other.TopDownPathCount; |
| 596 | } |
| 597 | |
| 598 | void BBState::InitFromSucc(const BBState &Other) { |
| 599 | PerPtrBottomUp = Other.PerPtrBottomUp; |
| 600 | BottomUpPathCount = Other.BottomUpPathCount; |
| 601 | } |
| 602 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 603 | /// The top-down traversal uses this to merge information about predecessors to |
| 604 | /// form the initial state for a new block. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 605 | void BBState::MergePred(const BBState &Other) { |
Michael Gottesman | d6ce6cb | 2013-08-09 23:22:27 +0000 | [diff] [blame] | 606 | if (TopDownPathCount == OverflowOccurredValue) |
| 607 | return; |
| 608 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 609 | // Other.TopDownPathCount can be 0, in which case it is either dead or a |
| 610 | // loop backedge. Loop backedges are special. |
| 611 | TopDownPathCount += Other.TopDownPathCount; |
| 612 | |
Michael Gottesman | d6ce6cb | 2013-08-09 23:22:27 +0000 | [diff] [blame] | 613 | // In order to be consistent, we clear the top down pointers when by adding |
| 614 | // TopDownPathCount becomes OverflowOccurredValue even though "true" overflow |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 615 | // has not occurred. |
Michael Gottesman | d6ce6cb | 2013-08-09 23:22:27 +0000 | [diff] [blame] | 616 | if (TopDownPathCount == OverflowOccurredValue) { |
| 617 | clearTopDownPointers(); |
| 618 | return; |
| 619 | } |
| 620 | |
Michael Gottesman | 4385edf | 2013-01-14 01:47:53 +0000 | [diff] [blame] | 621 | // Check for overflow. If we have overflow, fall back to conservative |
| 622 | // behavior. |
Dan Gohman | 7c84dad | 2012-09-12 20:45:17 +0000 | [diff] [blame] | 623 | if (TopDownPathCount < Other.TopDownPathCount) { |
Michael Gottesman | d6ce6cb | 2013-08-09 23:22:27 +0000 | [diff] [blame] | 624 | TopDownPathCount = OverflowOccurredValue; |
Dan Gohman | 7c84dad | 2012-09-12 20:45:17 +0000 | [diff] [blame] | 625 | clearTopDownPointers(); |
| 626 | return; |
| 627 | } |
| 628 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 629 | // For each entry in the other set, if our set has an entry with the same key, |
| 630 | // merge the entries. Otherwise, copy the entry and merge it with an empty |
| 631 | // entry. |
| 632 | for (ptr_const_iterator MI = Other.top_down_ptr_begin(), |
| 633 | ME = Other.top_down_ptr_end(); MI != ME; ++MI) { |
| 634 | std::pair<ptr_iterator, bool> Pair = PerPtrTopDown.insert(*MI); |
| 635 | Pair.first->second.Merge(Pair.second ? PtrState() : MI->second, |
| 636 | /*TopDown=*/true); |
| 637 | } |
| 638 | |
Dan Gohman | 7e315fc3 | 2011-08-11 21:06:32 +0000 | [diff] [blame] | 639 | // 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] | 640 | // same key, force it to merge with an empty entry. |
| 641 | for (ptr_iterator MI = top_down_ptr_begin(), |
| 642 | ME = top_down_ptr_end(); MI != ME; ++MI) |
| 643 | if (Other.PerPtrTopDown.find(MI->first) == Other.PerPtrTopDown.end()) |
| 644 | MI->second.Merge(PtrState(), /*TopDown=*/true); |
| 645 | } |
| 646 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 647 | /// The bottom-up traversal uses this to merge information about successors to |
| 648 | /// form the initial state for a new block. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 649 | void BBState::MergeSucc(const BBState &Other) { |
Michael Gottesman | d6ce6cb | 2013-08-09 23:22:27 +0000 | [diff] [blame] | 650 | if (BottomUpPathCount == OverflowOccurredValue) |
| 651 | return; |
| 652 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 653 | // Other.BottomUpPathCount can be 0, in which case it is either dead or a |
| 654 | // loop backedge. Loop backedges are special. |
| 655 | BottomUpPathCount += Other.BottomUpPathCount; |
| 656 | |
Michael Gottesman | d6ce6cb | 2013-08-09 23:22:27 +0000 | [diff] [blame] | 657 | // In order to be consistent, we clear the top down pointers when by adding |
| 658 | // BottomUpPathCount becomes OverflowOccurredValue even though "true" overflow |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 659 | // has not occurred. |
Michael Gottesman | d6ce6cb | 2013-08-09 23:22:27 +0000 | [diff] [blame] | 660 | if (BottomUpPathCount == OverflowOccurredValue) { |
| 661 | clearBottomUpPointers(); |
| 662 | return; |
| 663 | } |
| 664 | |
Michael Gottesman | 4385edf | 2013-01-14 01:47:53 +0000 | [diff] [blame] | 665 | // Check for overflow. If we have overflow, fall back to conservative |
| 666 | // behavior. |
Dan Gohman | 7c84dad | 2012-09-12 20:45:17 +0000 | [diff] [blame] | 667 | if (BottomUpPathCount < Other.BottomUpPathCount) { |
Michael Gottesman | d6ce6cb | 2013-08-09 23:22:27 +0000 | [diff] [blame] | 668 | BottomUpPathCount = OverflowOccurredValue; |
Dan Gohman | 7c84dad | 2012-09-12 20:45:17 +0000 | [diff] [blame] | 669 | clearBottomUpPointers(); |
| 670 | return; |
| 671 | } |
| 672 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 673 | // For each entry in the other set, if our set has an entry with the |
| 674 | // same key, merge the entries. Otherwise, copy the entry and merge |
| 675 | // it with an empty entry. |
| 676 | for (ptr_const_iterator MI = Other.bottom_up_ptr_begin(), |
| 677 | ME = Other.bottom_up_ptr_end(); MI != ME; ++MI) { |
| 678 | std::pair<ptr_iterator, bool> Pair = PerPtrBottomUp.insert(*MI); |
| 679 | Pair.first->second.Merge(Pair.second ? PtrState() : MI->second, |
| 680 | /*TopDown=*/false); |
| 681 | } |
| 682 | |
Dan Gohman | 7e315fc3 | 2011-08-11 21:06:32 +0000 | [diff] [blame] | 683 | // 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] | 684 | // with the same key, force it to merge with an empty entry. |
| 685 | for (ptr_iterator MI = bottom_up_ptr_begin(), |
| 686 | ME = bottom_up_ptr_end(); MI != ME; ++MI) |
| 687 | if (Other.PerPtrBottomUp.find(MI->first) == Other.PerPtrBottomUp.end()) |
| 688 | MI->second.Merge(PtrState(), /*TopDown=*/false); |
| 689 | } |
| 690 | |
Michael Gottesman | 81b1d43 | 2013-03-26 00:42:04 +0000 | [diff] [blame] | 691 | // Only enable ARC Annotations if we are building a debug version of |
| 692 | // libObjCARCOpts. |
| 693 | #ifndef NDEBUG |
| 694 | #define ARC_ANNOTATIONS |
| 695 | #endif |
| 696 | |
| 697 | // Define some macros along the lines of DEBUG and some helper functions to make |
| 698 | // it cleaner to create annotations in the source code and to no-op when not |
| 699 | // building in debug mode. |
| 700 | #ifdef ARC_ANNOTATIONS |
| 701 | |
| 702 | #include "llvm/Support/CommandLine.h" |
| 703 | |
| 704 | /// Enable/disable ARC sequence annotations. |
| 705 | static cl::opt<bool> |
Michael Gottesman | 6806b51 | 2013-04-17 20:48:03 +0000 | [diff] [blame] | 706 | EnableARCAnnotations("enable-objc-arc-annotations", cl::init(false), |
| 707 | cl::desc("Enable emission of arc data flow analysis " |
| 708 | "annotations")); |
Michael Gottesman | ffef24f | 2013-04-17 20:48:01 +0000 | [diff] [blame] | 709 | static cl::opt<bool> |
Michael Gottesman | adb921a | 2013-04-17 21:03:53 +0000 | [diff] [blame] | 710 | DisableCheckForCFGHazards("disable-objc-arc-checkforcfghazards", cl::init(false), |
| 711 | cl::desc("Disable check for cfg hazards when " |
| 712 | "annotating")); |
Michael Gottesman | 4e88ce6 | 2013-04-17 21:59:41 +0000 | [diff] [blame] | 713 | static cl::opt<std::string> |
| 714 | ARCAnnotationTargetIdentifier("objc-arc-annotation-target-identifier", |
| 715 | cl::init(""), |
| 716 | cl::desc("filter out all data flow annotations " |
| 717 | "but those that apply to the given " |
| 718 | "target llvm identifier.")); |
Michael Gottesman | 81b1d43 | 2013-03-26 00:42:04 +0000 | [diff] [blame] | 719 | |
| 720 | /// This function appends a unique ARCAnnotationProvenanceSourceMDKind id to an |
| 721 | /// instruction so that we can track backwards when post processing via the llvm |
| 722 | /// arc annotation processor tool. If the function is an |
| 723 | static MDString *AppendMDNodeToSourcePtr(unsigned NodeId, |
| 724 | Value *Ptr) { |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 725 | MDString *Hash = nullptr; |
Michael Gottesman | 81b1d43 | 2013-03-26 00:42:04 +0000 | [diff] [blame] | 726 | |
| 727 | // If pointer is a result of an instruction and it does not have a source |
| 728 | // MDNode it, attach a new MDNode onto it. If pointer is a result of |
| 729 | // an instruction and does have a source MDNode attached to it, return a |
| 730 | // reference to said Node. Otherwise just return 0. |
| 731 | if (Instruction *Inst = dyn_cast<Instruction>(Ptr)) { |
| 732 | MDNode *Node; |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 733 | if (!(Node = Inst->getMetadata(NodeId))) { |
Michael Gottesman | 81b1d43 | 2013-03-26 00:42:04 +0000 | [diff] [blame] | 734 | // We do not have any node. Generate and attatch the hash MDString to the |
| 735 | // instruction. |
| 736 | |
| 737 | // We just use an MDString to ensure that this metadata gets written out |
| 738 | // of line at the module level and to provide a very simple format |
| 739 | // encoding the information herein. Both of these makes it simpler to |
| 740 | // parse the annotations by a simple external program. |
Alp Toker | e69170a | 2014-06-26 22:52:05 +0000 | [diff] [blame] | 741 | std::string Str; |
| 742 | raw_string_ostream os(Str); |
Michael Gottesman | 81b1d43 | 2013-03-26 00:42:04 +0000 | [diff] [blame] | 743 | os << "(" << Inst->getParent()->getParent()->getName() << ",%" |
| 744 | << Inst->getName() << ")"; |
| 745 | |
| 746 | Hash = MDString::get(Inst->getContext(), os.str()); |
| 747 | Inst->setMetadata(NodeId, MDNode::get(Inst->getContext(),Hash)); |
| 748 | } else { |
| 749 | // We have a node. Grab its hash and return it. |
| 750 | assert(Node->getNumOperands() == 1 && |
| 751 | "An ARCAnnotationProvenanceSourceMDKind can only have 1 operand."); |
| 752 | Hash = cast<MDString>(Node->getOperand(0)); |
| 753 | } |
| 754 | } else if (Argument *Arg = dyn_cast<Argument>(Ptr)) { |
Alp Toker | e69170a | 2014-06-26 22:52:05 +0000 | [diff] [blame] | 755 | std::string str; |
| 756 | raw_string_ostream os(str); |
Michael Gottesman | 81b1d43 | 2013-03-26 00:42:04 +0000 | [diff] [blame] | 757 | os << "(" << Arg->getParent()->getName() << ",%" << Arg->getName() |
| 758 | << ")"; |
| 759 | Hash = MDString::get(Arg->getContext(), os.str()); |
| 760 | } |
| 761 | |
| 762 | return Hash; |
| 763 | } |
| 764 | |
Michael Gottesman | cd4de0f | 2013-03-26 00:42:09 +0000 | [diff] [blame] | 765 | static std::string SequenceToString(Sequence A) { |
Alp Toker | e69170a | 2014-06-26 22:52:05 +0000 | [diff] [blame] | 766 | std::string str; |
| 767 | raw_string_ostream os(str); |
Michael Gottesman | cd4de0f | 2013-03-26 00:42:09 +0000 | [diff] [blame] | 768 | os << A; |
| 769 | return os.str(); |
| 770 | } |
| 771 | |
Michael Gottesman | 81b1d43 | 2013-03-26 00:42:04 +0000 | [diff] [blame] | 772 | /// Helper function to change a Sequence into a String object using our overload |
| 773 | /// for raw_ostream so we only have printing code in one location. |
| 774 | static MDString *SequenceToMDString(LLVMContext &Context, |
| 775 | Sequence A) { |
Michael Gottesman | cd4de0f | 2013-03-26 00:42:09 +0000 | [diff] [blame] | 776 | return MDString::get(Context, SequenceToString(A)); |
Michael Gottesman | 81b1d43 | 2013-03-26 00:42:04 +0000 | [diff] [blame] | 777 | } |
| 778 | |
| 779 | /// A simple function to generate a MDNode which describes the change in state |
| 780 | /// for Value *Ptr caused by Instruction *Inst. |
| 781 | static void AppendMDNodeToInstForPtr(unsigned NodeId, |
| 782 | Instruction *Inst, |
| 783 | Value *Ptr, |
| 784 | MDString *PtrSourceMDNodeID, |
| 785 | Sequence OldSeq, |
| 786 | Sequence NewSeq) { |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 787 | MDNode *Node = nullptr; |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 788 | Metadata *tmp[3] = {PtrSourceMDNodeID, |
| 789 | SequenceToMDString(Inst->getContext(), OldSeq), |
| 790 | SequenceToMDString(Inst->getContext(), NewSeq)}; |
Craig Topper | e1d1294 | 2014-08-27 05:25:25 +0000 | [diff] [blame] | 791 | Node = MDNode::get(Inst->getContext(), tmp); |
Michael Gottesman | 81b1d43 | 2013-03-26 00:42:04 +0000 | [diff] [blame] | 792 | |
| 793 | Inst->setMetadata(NodeId, Node); |
| 794 | } |
| 795 | |
Michael Gottesman | cd4de0f | 2013-03-26 00:42:09 +0000 | [diff] [blame] | 796 | /// Add to the beginning of the basic block llvm.ptr.annotations which show the |
| 797 | /// state of a pointer at the entrance to a basic block. |
| 798 | static void GenerateARCBBEntranceAnnotation(const char *Name, BasicBlock *BB, |
| 799 | Value *Ptr, Sequence Seq) { |
Michael Gottesman | 4e88ce6 | 2013-04-17 21:59:41 +0000 | [diff] [blame] | 800 | // If we have a target identifier, make sure that we match it before |
| 801 | // continuing. |
| 802 | if(!ARCAnnotationTargetIdentifier.empty() && |
| 803 | !Ptr->getName().equals(ARCAnnotationTargetIdentifier)) |
| 804 | return; |
Michael Gottesman | 9e51813 | 2013-04-18 04:34:11 +0000 | [diff] [blame] | 805 | |
Michael Gottesman | cd4de0f | 2013-03-26 00:42:09 +0000 | [diff] [blame] | 806 | Module *M = BB->getParent()->getParent(); |
| 807 | LLVMContext &C = M->getContext(); |
| 808 | Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C)); |
| 809 | Type *I8XX = PointerType::getUnqual(I8X); |
| 810 | Type *Params[] = {I8XX, I8XX}; |
Craig Topper | e1d1294 | 2014-08-27 05:25:25 +0000 | [diff] [blame] | 811 | FunctionType *FTy = FunctionType::get(Type::getVoidTy(C), Params, |
Michael Gottesman | cd4de0f | 2013-03-26 00:42:09 +0000 | [diff] [blame] | 812 | /*isVarArg=*/false); |
| 813 | Constant *Callee = M->getOrInsertFunction(Name, FTy); |
Michael Gottesman | 60f6b28 | 2013-03-29 05:13:07 +0000 | [diff] [blame] | 814 | |
| 815 | IRBuilder<> Builder(BB, BB->getFirstInsertionPt()); |
| 816 | |
Michael Gottesman | cd4de0f | 2013-03-26 00:42:09 +0000 | [diff] [blame] | 817 | Value *PtrName; |
| 818 | StringRef Tmp = Ptr->getName(); |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 819 | if (nullptr == (PtrName = M->getGlobalVariable(Tmp, true))) { |
Michael Gottesman | cd4de0f | 2013-03-26 00:42:09 +0000 | [diff] [blame] | 820 | Value *ActualPtrName = Builder.CreateGlobalStringPtr(Tmp, |
| 821 | Tmp + "_STR"); |
| 822 | PtrName = new GlobalVariable(*M, I8X, true, GlobalVariable::InternalLinkage, |
Michael Gottesman | 60f6b28 | 2013-03-29 05:13:07 +0000 | [diff] [blame] | 823 | cast<Constant>(ActualPtrName), Tmp); |
Michael Gottesman | cd4de0f | 2013-03-26 00:42:09 +0000 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | Value *S; |
| 827 | std::string SeqStr = SequenceToString(Seq); |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 828 | if (nullptr == (S = M->getGlobalVariable(SeqStr, true))) { |
Michael Gottesman | cd4de0f | 2013-03-26 00:42:09 +0000 | [diff] [blame] | 829 | Value *ActualPtrName = Builder.CreateGlobalStringPtr(SeqStr, |
| 830 | SeqStr + "_STR"); |
| 831 | S = new GlobalVariable(*M, I8X, true, GlobalVariable::InternalLinkage, |
| 832 | cast<Constant>(ActualPtrName), SeqStr); |
| 833 | } |
| 834 | |
| 835 | Builder.CreateCall2(Callee, PtrName, S); |
| 836 | } |
| 837 | |
| 838 | /// Add to the end of the basic block llvm.ptr.annotations which show the state |
| 839 | /// of the pointer at the bottom of the basic block. |
| 840 | static void GenerateARCBBTerminatorAnnotation(const char *Name, BasicBlock *BB, |
| 841 | Value *Ptr, Sequence Seq) { |
Michael Gottesman | 4e88ce6 | 2013-04-17 21:59:41 +0000 | [diff] [blame] | 842 | // If we have a target identifier, make sure that we match it before emitting |
| 843 | // an annotation. |
| 844 | if(!ARCAnnotationTargetIdentifier.empty() && |
| 845 | !Ptr->getName().equals(ARCAnnotationTargetIdentifier)) |
| 846 | return; |
Michael Gottesman | 9e51813 | 2013-04-18 04:34:11 +0000 | [diff] [blame] | 847 | |
Michael Gottesman | cd4de0f | 2013-03-26 00:42:09 +0000 | [diff] [blame] | 848 | Module *M = BB->getParent()->getParent(); |
| 849 | LLVMContext &C = M->getContext(); |
| 850 | Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C)); |
| 851 | Type *I8XX = PointerType::getUnqual(I8X); |
| 852 | Type *Params[] = {I8XX, I8XX}; |
Craig Topper | e1d1294 | 2014-08-27 05:25:25 +0000 | [diff] [blame] | 853 | FunctionType *FTy = FunctionType::get(Type::getVoidTy(C), Params, |
Michael Gottesman | cd4de0f | 2013-03-26 00:42:09 +0000 | [diff] [blame] | 854 | /*isVarArg=*/false); |
| 855 | Constant *Callee = M->getOrInsertFunction(Name, FTy); |
Michael Gottesman | 60f6b28 | 2013-03-29 05:13:07 +0000 | [diff] [blame] | 856 | |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 857 | IRBuilder<> Builder(BB, std::prev(BB->end())); |
Michael Gottesman | 60f6b28 | 2013-03-29 05:13:07 +0000 | [diff] [blame] | 858 | |
Michael Gottesman | cd4de0f | 2013-03-26 00:42:09 +0000 | [diff] [blame] | 859 | Value *PtrName; |
| 860 | StringRef Tmp = Ptr->getName(); |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 861 | if (nullptr == (PtrName = M->getGlobalVariable(Tmp, true))) { |
Michael Gottesman | cd4de0f | 2013-03-26 00:42:09 +0000 | [diff] [blame] | 862 | Value *ActualPtrName = Builder.CreateGlobalStringPtr(Tmp, |
| 863 | Tmp + "_STR"); |
| 864 | PtrName = new GlobalVariable(*M, I8X, true, GlobalVariable::InternalLinkage, |
Michael Gottesman | 60f6b28 | 2013-03-29 05:13:07 +0000 | [diff] [blame] | 865 | cast<Constant>(ActualPtrName), Tmp); |
Michael Gottesman | cd4de0f | 2013-03-26 00:42:09 +0000 | [diff] [blame] | 866 | } |
| 867 | |
| 868 | Value *S; |
| 869 | std::string SeqStr = SequenceToString(Seq); |
Craig Topper | e73658d | 2014-04-28 04:05:08 +0000 | [diff] [blame] | 870 | if (nullptr == (S = M->getGlobalVariable(SeqStr, true))) { |
Michael Gottesman | cd4de0f | 2013-03-26 00:42:09 +0000 | [diff] [blame] | 871 | Value *ActualPtrName = Builder.CreateGlobalStringPtr(SeqStr, |
| 872 | SeqStr + "_STR"); |
| 873 | S = new GlobalVariable(*M, I8X, true, GlobalVariable::InternalLinkage, |
| 874 | cast<Constant>(ActualPtrName), SeqStr); |
| 875 | } |
Michael Gottesman | 60f6b28 | 2013-03-29 05:13:07 +0000 | [diff] [blame] | 876 | Builder.CreateCall2(Callee, PtrName, S); |
Michael Gottesman | cd4de0f | 2013-03-26 00:42:09 +0000 | [diff] [blame] | 877 | } |
| 878 | |
Michael Gottesman | 81b1d43 | 2013-03-26 00:42:04 +0000 | [diff] [blame] | 879 | /// Adds a source annotation to pointer and a state change annotation to Inst |
| 880 | /// referencing the source annotation and the old/new state of pointer. |
| 881 | static void GenerateARCAnnotation(unsigned InstMDId, |
| 882 | unsigned PtrMDId, |
| 883 | Instruction *Inst, |
| 884 | Value *Ptr, |
| 885 | Sequence OldSeq, |
| 886 | Sequence NewSeq) { |
| 887 | if (EnableARCAnnotations) { |
Michael Gottesman | 4e88ce6 | 2013-04-17 21:59:41 +0000 | [diff] [blame] | 888 | // If we have a target identifier, make sure that we match it before |
| 889 | // emitting an annotation. |
| 890 | if(!ARCAnnotationTargetIdentifier.empty() && |
| 891 | !Ptr->getName().equals(ARCAnnotationTargetIdentifier)) |
| 892 | return; |
Michael Gottesman | 9e51813 | 2013-04-18 04:34:11 +0000 | [diff] [blame] | 893 | |
Michael Gottesman | 81b1d43 | 2013-03-26 00:42:04 +0000 | [diff] [blame] | 894 | // First generate the source annotation on our pointer. This will return an |
| 895 | // MDString* if Ptr actually comes from an instruction implying we can put |
| 896 | // in a source annotation. If AppendMDNodeToSourcePtr returns 0 (i.e. NULL), |
| 897 | // then we know that our pointer is from an Argument so we put a reference |
| 898 | // to the argument number. |
| 899 | // |
| 900 | // The point of this is to make it easy for the |
| 901 | // llvm-arc-annotation-processor tool to cross reference where the source |
| 902 | // pointer is in the LLVM IR since the LLVM IR parser does not submit such |
| 903 | // information via debug info for backends to use (since why would anyone |
Alp Toker | f907b89 | 2013-12-05 05:44:44 +0000 | [diff] [blame] | 904 | // need such a thing from LLVM IR besides in non-standard cases |
Michael Gottesman | 81b1d43 | 2013-03-26 00:42:04 +0000 | [diff] [blame] | 905 | // [i.e. this]). |
| 906 | MDString *SourcePtrMDNode = |
| 907 | AppendMDNodeToSourcePtr(PtrMDId, Ptr); |
| 908 | AppendMDNodeToInstForPtr(InstMDId, Inst, Ptr, SourcePtrMDNode, OldSeq, |
| 909 | NewSeq); |
| 910 | } |
| 911 | } |
| 912 | |
| 913 | // The actual interface for accessing the above functionality is defined via |
| 914 | // some simple macros which are defined below. We do this so that the user does |
| 915 | // not need to pass in what metadata id is needed resulting in cleaner code and |
| 916 | // additionally since it provides an easy way to conditionally no-op all |
| 917 | // annotation support in a non-debug build. |
| 918 | |
| 919 | /// Use this macro to annotate a sequence state change when processing |
| 920 | /// instructions bottom up, |
| 921 | #define ANNOTATE_BOTTOMUP(inst, ptr, old, new) \ |
| 922 | GenerateARCAnnotation(ARCAnnotationBottomUpMDKind, \ |
| 923 | ARCAnnotationProvenanceSourceMDKind, (inst), \ |
| 924 | const_cast<Value*>(ptr), (old), (new)) |
| 925 | /// Use this macro to annotate a sequence state change when processing |
| 926 | /// instructions top down. |
| 927 | #define ANNOTATE_TOPDOWN(inst, ptr, old, new) \ |
| 928 | GenerateARCAnnotation(ARCAnnotationTopDownMDKind, \ |
| 929 | ARCAnnotationProvenanceSourceMDKind, (inst), \ |
| 930 | const_cast<Value*>(ptr), (old), (new)) |
| 931 | |
Michael Gottesman | 43e7e00 | 2013-04-03 22:41:59 +0000 | [diff] [blame] | 932 | #define ANNOTATE_BB(_states, _bb, _name, _type, _direction) \ |
| 933 | do { \ |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 934 | if (EnableARCAnnotations) { \ |
| 935 | for(BBState::ptr_const_iterator I = (_states)._direction##_ptr_begin(), \ |
Michael Gottesman | 43e7e00 | 2013-04-03 22:41:59 +0000 | [diff] [blame] | 936 | E = (_states)._direction##_ptr_end(); I != E; ++I) { \ |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 937 | Value *Ptr = const_cast<Value*>(I->first); \ |
| 938 | Sequence Seq = I->second.GetSeq(); \ |
| 939 | GenerateARCBB ## _type ## Annotation(_name, (_bb), Ptr, Seq); \ |
| 940 | } \ |
Michael Gottesman | 43e7e00 | 2013-04-03 22:41:59 +0000 | [diff] [blame] | 941 | } \ |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 942 | } while (0) |
Michael Gottesman | 43e7e00 | 2013-04-03 22:41:59 +0000 | [diff] [blame] | 943 | |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 944 | #define ANNOTATE_BOTTOMUP_BBSTART(_states, _basicblock) \ |
Michael Gottesman | 43e7e00 | 2013-04-03 22:41:59 +0000 | [diff] [blame] | 945 | ANNOTATE_BB(_states, _basicblock, "llvm.arc.annotation.bottomup.bbstart", \ |
| 946 | Entrance, bottom_up) |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 947 | #define ANNOTATE_BOTTOMUP_BBEND(_states, _basicblock) \ |
| 948 | ANNOTATE_BB(_states, _basicblock, "llvm.arc.annotation.bottomup.bbend", \ |
Michael Gottesman | 43e7e00 | 2013-04-03 22:41:59 +0000 | [diff] [blame] | 949 | Terminator, bottom_up) |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 950 | #define ANNOTATE_TOPDOWN_BBSTART(_states, _basicblock) \ |
| 951 | ANNOTATE_BB(_states, _basicblock, "llvm.arc.annotation.topdown.bbstart", \ |
Michael Gottesman | 43e7e00 | 2013-04-03 22:41:59 +0000 | [diff] [blame] | 952 | Entrance, top_down) |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 953 | #define ANNOTATE_TOPDOWN_BBEND(_states, _basicblock) \ |
| 954 | ANNOTATE_BB(_states, _basicblock, "llvm.arc.annotation.topdown.bbend", \ |
Michael Gottesman | 43e7e00 | 2013-04-03 22:41:59 +0000 | [diff] [blame] | 955 | Terminator, top_down) |
| 956 | |
Michael Gottesman | 81b1d43 | 2013-03-26 00:42:04 +0000 | [diff] [blame] | 957 | #else // !ARC_ANNOTATION |
| 958 | // If annotations are off, noop. |
| 959 | #define ANNOTATE_BOTTOMUP(inst, ptr, old, new) |
| 960 | #define ANNOTATE_TOPDOWN(inst, ptr, old, new) |
Michael Gottesman | 43e7e00 | 2013-04-03 22:41:59 +0000 | [diff] [blame] | 961 | #define ANNOTATE_BOTTOMUP_BBSTART(states, basicblock) |
| 962 | #define ANNOTATE_BOTTOMUP_BBEND(states, basicblock) |
| 963 | #define ANNOTATE_TOPDOWN_BBSTART(states, basicblock) |
| 964 | #define ANNOTATE_TOPDOWN_BBEND(states, basicblock) |
Michael Gottesman | 81b1d43 | 2013-03-26 00:42:04 +0000 | [diff] [blame] | 965 | #endif // !ARC_ANNOTATION |
| 966 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 967 | namespace { |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 968 | /// \brief The main ARC optimization pass. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 969 | class ObjCARCOpt : public FunctionPass { |
| 970 | bool Changed; |
| 971 | ProvenanceAnalysis PA; |
Michael Gottesman | 14acfac | 2013-07-06 01:39:23 +0000 | [diff] [blame] | 972 | ARCRuntimeEntryPoints EP; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 973 | |
Michael Gottesman | 5a91bbf | 2013-05-24 20:44:02 +0000 | [diff] [blame] | 974 | // This is used to track if a pointer is stored into an alloca. |
| 975 | DenseSet<const Value *> MultiOwnersSet; |
| 976 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 977 | /// A flag indicating whether this optimization pass should run. |
Dan Gohman | ceaac7c | 2011-06-20 23:20:43 +0000 | [diff] [blame] | 978 | bool Run; |
| 979 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 980 | /// Flags which determine whether each of the interesting runtine functions |
| 981 | /// is in fact used in the current function. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 982 | unsigned UsedInThisFunction; |
| 983 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 984 | /// The Metadata Kind for clang.imprecise_release metadata. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 985 | unsigned ImpreciseReleaseMDKind; |
| 986 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 987 | /// The Metadata Kind for clang.arc.copy_on_escape metadata. |
Dan Gohman | a7107f9 | 2011-10-17 22:53:25 +0000 | [diff] [blame] | 988 | unsigned CopyOnEscapeMDKind; |
| 989 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 990 | /// The Metadata Kind for clang.arc.no_objc_arc_exceptions metadata. |
Dan Gohman | 0155f30 | 2012-02-17 18:59:53 +0000 | [diff] [blame] | 991 | unsigned NoObjCARCExceptionsMDKind; |
| 992 | |
Michael Gottesman | 81b1d43 | 2013-03-26 00:42:04 +0000 | [diff] [blame] | 993 | #ifdef ARC_ANNOTATIONS |
| 994 | /// The Metadata Kind for llvm.arc.annotation.bottomup metadata. |
| 995 | unsigned ARCAnnotationBottomUpMDKind; |
| 996 | /// The Metadata Kind for llvm.arc.annotation.topdown metadata. |
| 997 | unsigned ARCAnnotationTopDownMDKind; |
| 998 | /// The Metadata Kind for llvm.arc.annotation.provenancesource metadata. |
| 999 | unsigned ARCAnnotationProvenanceSourceMDKind; |
| 1000 | #endif // ARC_ANNOATIONS |
| 1001 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1002 | bool OptimizeRetainRVCall(Function &F, Instruction *RetainRV); |
Michael Gottesman | 556ff61 | 2013-01-12 01:25:19 +0000 | [diff] [blame] | 1003 | void OptimizeAutoreleaseRVCall(Function &F, Instruction *AutoreleaseRV, |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 1004 | ARCInstKind &Class); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1005 | void OptimizeIndividualCalls(Function &F); |
| 1006 | |
| 1007 | void CheckForCFGHazards(const BasicBlock *BB, |
| 1008 | DenseMap<const BasicBlock *, BBState> &BBStates, |
| 1009 | BBState &MyStates) const; |
Michael Gottesman | 0be6920 | 2015-03-05 23:28:58 +0000 | [diff] [blame^] | 1010 | bool VisitInstructionBottomUp(Instruction *Inst, BasicBlock *BB, |
| 1011 | BlotMapVector<Value *, RRInfo> &Retains, |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1012 | BBState &MyStates); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1013 | bool VisitBottomUp(BasicBlock *BB, |
| 1014 | DenseMap<const BasicBlock *, BBState> &BBStates, |
Michael Gottesman | 0be6920 | 2015-03-05 23:28:58 +0000 | [diff] [blame^] | 1015 | BlotMapVector<Value *, RRInfo> &Retains); |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1016 | bool VisitInstructionTopDown(Instruction *Inst, |
| 1017 | DenseMap<Value *, RRInfo> &Releases, |
| 1018 | BBState &MyStates); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1019 | bool VisitTopDown(BasicBlock *BB, |
| 1020 | DenseMap<const BasicBlock *, BBState> &BBStates, |
| 1021 | DenseMap<Value *, RRInfo> &Releases); |
Michael Gottesman | 0be6920 | 2015-03-05 23:28:58 +0000 | [diff] [blame^] | 1022 | bool Visit(Function &F, DenseMap<const BasicBlock *, BBState> &BBStates, |
| 1023 | BlotMapVector<Value *, RRInfo> &Retains, |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1024 | DenseMap<Value *, RRInfo> &Releases); |
| 1025 | |
| 1026 | void MoveCalls(Value *Arg, RRInfo &RetainsToMove, RRInfo &ReleasesToMove, |
Michael Gottesman | 0be6920 | 2015-03-05 23:28:58 +0000 | [diff] [blame^] | 1027 | BlotMapVector<Value *, RRInfo> &Retains, |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1028 | DenseMap<Value *, RRInfo> &Releases, |
Michael Gottesman | 0be6920 | 2015-03-05 23:28:58 +0000 | [diff] [blame^] | 1029 | SmallVectorImpl<Instruction *> &DeadInsts, Module *M); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1030 | |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 1031 | bool ConnectTDBUTraversals(DenseMap<const BasicBlock *, BBState> &BBStates, |
Michael Gottesman | 0be6920 | 2015-03-05 23:28:58 +0000 | [diff] [blame^] | 1032 | BlotMapVector<Value *, RRInfo> &Retains, |
| 1033 | DenseMap<Value *, RRInfo> &Releases, Module *M, |
Craig Topper | b94011f | 2013-07-14 04:42:23 +0000 | [diff] [blame] | 1034 | SmallVectorImpl<Instruction *> &NewRetains, |
| 1035 | SmallVectorImpl<Instruction *> &NewReleases, |
| 1036 | SmallVectorImpl<Instruction *> &DeadInsts, |
Michael Gottesman | 0be6920 | 2015-03-05 23:28:58 +0000 | [diff] [blame^] | 1037 | RRInfo &RetainsToMove, RRInfo &ReleasesToMove, |
| 1038 | Value *Arg, bool KnownSafe, |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 1039 | bool &AnyPairsCompletelyEliminated); |
| 1040 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1041 | bool PerformCodePlacement(DenseMap<const BasicBlock *, BBState> &BBStates, |
Michael Gottesman | 0be6920 | 2015-03-05 23:28:58 +0000 | [diff] [blame^] | 1042 | BlotMapVector<Value *, RRInfo> &Retains, |
| 1043 | DenseMap<Value *, RRInfo> &Releases, Module *M); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1044 | |
| 1045 | void OptimizeWeakCalls(Function &F); |
| 1046 | |
| 1047 | bool OptimizeSequences(Function &F); |
| 1048 | |
| 1049 | void OptimizeReturns(Function &F); |
| 1050 | |
Michael Gottesman | 9c11815 | 2013-04-29 06:16:57 +0000 | [diff] [blame] | 1051 | #ifndef NDEBUG |
| 1052 | void GatherStatistics(Function &F, bool AfterOptimization = false); |
| 1053 | #endif |
| 1054 | |
Craig Topper | 3e4c697 | 2014-03-05 09:10:37 +0000 | [diff] [blame] | 1055 | void getAnalysisUsage(AnalysisUsage &AU) const override; |
| 1056 | bool doInitialization(Module &M) override; |
| 1057 | bool runOnFunction(Function &F) override; |
| 1058 | void releaseMemory() override; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1059 | |
| 1060 | public: |
| 1061 | static char ID; |
| 1062 | ObjCARCOpt() : FunctionPass(ID) { |
| 1063 | initializeObjCARCOptPass(*PassRegistry::getPassRegistry()); |
| 1064 | } |
| 1065 | }; |
| 1066 | } |
| 1067 | |
| 1068 | char ObjCARCOpt::ID = 0; |
| 1069 | INITIALIZE_PASS_BEGIN(ObjCARCOpt, |
| 1070 | "objc-arc", "ObjC ARC optimization", false, false) |
| 1071 | INITIALIZE_PASS_DEPENDENCY(ObjCARCAliasAnalysis) |
| 1072 | INITIALIZE_PASS_END(ObjCARCOpt, |
| 1073 | "objc-arc", "ObjC ARC optimization", false, false) |
| 1074 | |
| 1075 | Pass *llvm::createObjCARCOptPass() { |
| 1076 | return new ObjCARCOpt(); |
| 1077 | } |
| 1078 | |
| 1079 | void ObjCARCOpt::getAnalysisUsage(AnalysisUsage &AU) const { |
| 1080 | AU.addRequired<ObjCARCAliasAnalysis>(); |
| 1081 | AU.addRequired<AliasAnalysis>(); |
| 1082 | // ARC optimization doesn't currently split critical edges. |
| 1083 | AU.setPreservesCFG(); |
| 1084 | } |
| 1085 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 1086 | /// Turn objc_retainAutoreleasedReturnValue into objc_retain if the operand is |
| 1087 | /// not a return value. Or, if it can be paired with an |
| 1088 | /// objc_autoreleaseReturnValue, delete the pair and return true. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1089 | bool |
| 1090 | ObjCARCOpt::OptimizeRetainRVCall(Function &F, Instruction *RetainRV) { |
Dan Gohman | e3ed2b0 | 2012-03-23 18:09:00 +0000 | [diff] [blame] | 1091 | // Check for the argument being from an immediately preceding call or invoke. |
Michael Gottesman | e5ad66f | 2015-02-19 00:42:38 +0000 | [diff] [blame] | 1092 | const Value *Arg = GetArgRCIdentityRoot(RetainRV); |
Dan Gohman | dae3349 | 2012-04-27 18:56:31 +0000 | [diff] [blame] | 1093 | ImmutableCallSite CS(Arg); |
| 1094 | if (const Instruction *Call = CS.getInstruction()) { |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1095 | if (Call->getParent() == RetainRV->getParent()) { |
Dan Gohman | dae3349 | 2012-04-27 18:56:31 +0000 | [diff] [blame] | 1096 | BasicBlock::const_iterator I = Call; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1097 | ++I; |
Michael Gottesman | 65c2481 | 2013-03-25 09:27:43 +0000 | [diff] [blame] | 1098 | while (IsNoopInstruction(I)) ++I; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1099 | if (&*I == RetainRV) |
| 1100 | return false; |
Dan Gohman | dae3349 | 2012-04-27 18:56:31 +0000 | [diff] [blame] | 1101 | } else if (const InvokeInst *II = dyn_cast<InvokeInst>(Call)) { |
Dan Gohman | e3ed2b0 | 2012-03-23 18:09:00 +0000 | [diff] [blame] | 1102 | BasicBlock *RetainRVParent = RetainRV->getParent(); |
| 1103 | if (II->getNormalDest() == RetainRVParent) { |
Dan Gohman | dae3349 | 2012-04-27 18:56:31 +0000 | [diff] [blame] | 1104 | BasicBlock::const_iterator I = RetainRVParent->begin(); |
Michael Gottesman | 65c2481 | 2013-03-25 09:27:43 +0000 | [diff] [blame] | 1105 | while (IsNoopInstruction(I)) ++I; |
Dan Gohman | e3ed2b0 | 2012-03-23 18:09:00 +0000 | [diff] [blame] | 1106 | if (&*I == RetainRV) |
| 1107 | return false; |
| 1108 | } |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1109 | } |
Dan Gohman | e3ed2b0 | 2012-03-23 18:09:00 +0000 | [diff] [blame] | 1110 | } |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1111 | |
| 1112 | // Check for being preceded by an objc_autoreleaseReturnValue on the same |
| 1113 | // pointer. In this case, we can delete the pair. |
| 1114 | BasicBlock::iterator I = RetainRV, Begin = RetainRV->getParent()->begin(); |
| 1115 | if (I != Begin) { |
Michael Gottesman | 65c2481 | 2013-03-25 09:27:43 +0000 | [diff] [blame] | 1116 | do --I; while (I != Begin && IsNoopInstruction(I)); |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 1117 | if (GetBasicARCInstKind(I) == ARCInstKind::AutoreleaseRV && |
Michael Gottesman | e5ad66f | 2015-02-19 00:42:38 +0000 | [diff] [blame] | 1118 | GetArgRCIdentityRoot(I) == Arg) { |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1119 | Changed = true; |
| 1120 | ++NumPeeps; |
Michael Gottesman | 10426b5 | 2013-01-07 21:26:07 +0000 | [diff] [blame] | 1121 | |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1122 | DEBUG(dbgs() << "Erasing autoreleaseRV,retainRV pair: " << *I << "\n" |
| 1123 | << "Erasing " << *RetainRV << "\n"); |
Michael Gottesman | 10426b5 | 2013-01-07 21:26:07 +0000 | [diff] [blame] | 1124 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1125 | EraseInstruction(I); |
| 1126 | EraseInstruction(RetainRV); |
| 1127 | return true; |
| 1128 | } |
| 1129 | } |
| 1130 | |
| 1131 | // Turn it to a plain objc_retain. |
| 1132 | Changed = true; |
| 1133 | ++NumPeeps; |
Michael Gottesman | 10426b5 | 2013-01-07 21:26:07 +0000 | [diff] [blame] | 1134 | |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1135 | DEBUG(dbgs() << "Transforming objc_retainAutoreleasedReturnValue => " |
Michael Gottesman | def07bb | 2013-01-05 17:55:42 +0000 | [diff] [blame] | 1136 | "objc_retain since the operand is not a return value.\n" |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1137 | "Old = " << *RetainRV << "\n"); |
Michael Gottesman | 10426b5 | 2013-01-07 21:26:07 +0000 | [diff] [blame] | 1138 | |
Michael Gottesman | 14acfac | 2013-07-06 01:39:23 +0000 | [diff] [blame] | 1139 | Constant *NewDecl = EP.get(ARCRuntimeEntryPoints::EPT_Retain); |
| 1140 | cast<CallInst>(RetainRV)->setCalledFunction(NewDecl); |
Michael Gottesman | def07bb | 2013-01-05 17:55:42 +0000 | [diff] [blame] | 1141 | |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1142 | DEBUG(dbgs() << "New = " << *RetainRV << "\n"); |
Michael Gottesman | def07bb | 2013-01-05 17:55:42 +0000 | [diff] [blame] | 1143 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1144 | return false; |
| 1145 | } |
| 1146 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 1147 | /// Turn objc_autoreleaseReturnValue into objc_autorelease if the result is not |
| 1148 | /// used as a return value. |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 1149 | void ObjCARCOpt::OptimizeAutoreleaseRVCall(Function &F, |
| 1150 | Instruction *AutoreleaseRV, |
| 1151 | ARCInstKind &Class) { |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1152 | // Check for a return of the pointer value. |
Michael Gottesman | e5ad66f | 2015-02-19 00:42:38 +0000 | [diff] [blame] | 1153 | const Value *Ptr = GetArgRCIdentityRoot(AutoreleaseRV); |
Dan Gohman | 10a18d5 | 2011-08-12 00:36:31 +0000 | [diff] [blame] | 1154 | SmallVector<const Value *, 2> Users; |
| 1155 | Users.push_back(Ptr); |
| 1156 | do { |
| 1157 | Ptr = Users.pop_back_val(); |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 1158 | for (const User *U : Ptr->users()) { |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 1159 | if (isa<ReturnInst>(U) || GetBasicARCInstKind(U) == ARCInstKind::RetainRV) |
Dan Gohman | 10a18d5 | 2011-08-12 00:36:31 +0000 | [diff] [blame] | 1160 | return; |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 1161 | if (isa<BitCastInst>(U)) |
| 1162 | Users.push_back(U); |
Dan Gohman | 10a18d5 | 2011-08-12 00:36:31 +0000 | [diff] [blame] | 1163 | } |
| 1164 | } while (!Users.empty()); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1165 | |
| 1166 | Changed = true; |
| 1167 | ++NumPeeps; |
Michael Gottesman | 1bf6908 | 2013-01-06 21:07:11 +0000 | [diff] [blame] | 1168 | |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1169 | DEBUG(dbgs() << "Transforming objc_autoreleaseReturnValue => " |
Michael Gottesman | 1bf6908 | 2013-01-06 21:07:11 +0000 | [diff] [blame] | 1170 | "objc_autorelease since its operand is not used as a return " |
| 1171 | "value.\n" |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1172 | "Old = " << *AutoreleaseRV << "\n"); |
Michael Gottesman | 1bf6908 | 2013-01-06 21:07:11 +0000 | [diff] [blame] | 1173 | |
Michael Gottesman | c9656fa | 2013-01-12 01:25:15 +0000 | [diff] [blame] | 1174 | CallInst *AutoreleaseRVCI = cast<CallInst>(AutoreleaseRV); |
Michael Gottesman | 14acfac | 2013-07-06 01:39:23 +0000 | [diff] [blame] | 1175 | Constant *NewDecl = EP.get(ARCRuntimeEntryPoints::EPT_Autorelease); |
| 1176 | AutoreleaseRVCI->setCalledFunction(NewDecl); |
Michael Gottesman | c9656fa | 2013-01-12 01:25:15 +0000 | [diff] [blame] | 1177 | AutoreleaseRVCI->setTailCall(false); // Never tail call objc_autorelease. |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 1178 | Class = ARCInstKind::Autorelease; |
Michael Gottesman | 10426b5 | 2013-01-07 21:26:07 +0000 | [diff] [blame] | 1179 | |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1180 | DEBUG(dbgs() << "New: " << *AutoreleaseRV << "\n"); |
Michael Gottesman | 10426b5 | 2013-01-07 21:26:07 +0000 | [diff] [blame] | 1181 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1182 | } |
| 1183 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 1184 | /// Visit each call, one at a time, and make simplifications without doing any |
| 1185 | /// additional analysis. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1186 | void ObjCARCOpt::OptimizeIndividualCalls(Function &F) { |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1187 | DEBUG(dbgs() << "\n== ObjCARCOpt::OptimizeIndividualCalls ==\n"); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1188 | // Reset all the flags in preparation for recomputing them. |
| 1189 | UsedInThisFunction = 0; |
| 1190 | |
| 1191 | // Visit all objc_* calls in F. |
| 1192 | for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) { |
| 1193 | Instruction *Inst = &*I++; |
Michael Gottesman | 3f146e2 | 2013-01-01 16:05:48 +0000 | [diff] [blame] | 1194 | |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 1195 | ARCInstKind Class = GetBasicARCInstKind(Inst); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1196 | |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1197 | DEBUG(dbgs() << "Visiting: Class: " << Class << "; " << *Inst << "\n"); |
Michael Gottesman | 782e344 | 2013-01-17 18:32:34 +0000 | [diff] [blame] | 1198 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1199 | switch (Class) { |
| 1200 | default: break; |
| 1201 | |
| 1202 | // Delete no-op casts. These function calls have special semantics, but |
| 1203 | // the semantics are entirely implemented via lowering in the front-end, |
| 1204 | // so by the time they reach the optimizer, they are just no-op calls |
| 1205 | // which return their argument. |
| 1206 | // |
| 1207 | // There are gray areas here, as the ability to cast reference-counted |
| 1208 | // pointers to raw void* and back allows code to break ARC assumptions, |
| 1209 | // however these are currently considered to be unimportant. |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 1210 | case ARCInstKind::NoopCast: |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1211 | Changed = true; |
| 1212 | ++NumNoops; |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1213 | DEBUG(dbgs() << "Erasing no-op cast: " << *Inst << "\n"); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1214 | EraseInstruction(Inst); |
| 1215 | continue; |
| 1216 | |
| 1217 | // If the pointer-to-weak-pointer is null, it's undefined behavior. |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 1218 | case ARCInstKind::StoreWeak: |
| 1219 | case ARCInstKind::LoadWeak: |
| 1220 | case ARCInstKind::LoadWeakRetained: |
| 1221 | case ARCInstKind::InitWeak: |
| 1222 | case ARCInstKind::DestroyWeak: { |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1223 | CallInst *CI = cast<CallInst>(Inst); |
Michael Gottesman | 65c2481 | 2013-03-25 09:27:43 +0000 | [diff] [blame] | 1224 | if (IsNullOrUndef(CI->getArgOperand(0))) { |
Dan Gohman | 670f937 | 2012-04-13 18:57:48 +0000 | [diff] [blame] | 1225 | Changed = true; |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1226 | Type *Ty = CI->getArgOperand(0)->getType(); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1227 | new StoreInst(UndefValue::get(cast<PointerType>(Ty)->getElementType()), |
| 1228 | Constant::getNullValue(Ty), |
| 1229 | CI); |
Michael Gottesman | 10426b5 | 2013-01-07 21:26:07 +0000 | [diff] [blame] | 1230 | llvm::Value *NewValue = UndefValue::get(CI->getType()); |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1231 | DEBUG(dbgs() << "A null pointer-to-weak-pointer is undefined behavior." |
| 1232 | "\nOld = " << *CI << "\nNew = " << *NewValue << "\n"); |
Michael Gottesman | fec61c0 | 2013-01-06 21:54:30 +0000 | [diff] [blame] | 1233 | CI->replaceAllUsesWith(NewValue); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1234 | CI->eraseFromParent(); |
| 1235 | continue; |
| 1236 | } |
| 1237 | break; |
| 1238 | } |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 1239 | case ARCInstKind::CopyWeak: |
| 1240 | case ARCInstKind::MoveWeak: { |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1241 | CallInst *CI = cast<CallInst>(Inst); |
Michael Gottesman | 65c2481 | 2013-03-25 09:27:43 +0000 | [diff] [blame] | 1242 | if (IsNullOrUndef(CI->getArgOperand(0)) || |
| 1243 | IsNullOrUndef(CI->getArgOperand(1))) { |
Dan Gohman | 670f937 | 2012-04-13 18:57:48 +0000 | [diff] [blame] | 1244 | Changed = true; |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1245 | Type *Ty = CI->getArgOperand(0)->getType(); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1246 | new StoreInst(UndefValue::get(cast<PointerType>(Ty)->getElementType()), |
| 1247 | Constant::getNullValue(Ty), |
| 1248 | CI); |
Michael Gottesman | fec61c0 | 2013-01-06 21:54:30 +0000 | [diff] [blame] | 1249 | |
| 1250 | llvm::Value *NewValue = UndefValue::get(CI->getType()); |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1251 | DEBUG(dbgs() << "A null pointer-to-weak-pointer is undefined behavior." |
| 1252 | "\nOld = " << *CI << "\nNew = " << *NewValue << "\n"); |
Michael Gottesman | 10426b5 | 2013-01-07 21:26:07 +0000 | [diff] [blame] | 1253 | |
Michael Gottesman | fec61c0 | 2013-01-06 21:54:30 +0000 | [diff] [blame] | 1254 | CI->replaceAllUsesWith(NewValue); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1255 | CI->eraseFromParent(); |
| 1256 | continue; |
| 1257 | } |
| 1258 | break; |
| 1259 | } |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 1260 | case ARCInstKind::RetainRV: |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1261 | if (OptimizeRetainRVCall(F, Inst)) |
| 1262 | continue; |
| 1263 | break; |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 1264 | case ARCInstKind::AutoreleaseRV: |
Michael Gottesman | 556ff61 | 2013-01-12 01:25:19 +0000 | [diff] [blame] | 1265 | OptimizeAutoreleaseRVCall(F, Inst, Class); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1266 | break; |
| 1267 | } |
| 1268 | |
Michael Gottesman | b8c8836 | 2013-04-03 02:57:24 +0000 | [diff] [blame] | 1269 | // objc_autorelease(x) -> objc_release(x) if x is otherwise unused. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1270 | if (IsAutorelease(Class) && Inst->use_empty()) { |
| 1271 | CallInst *Call = cast<CallInst>(Inst); |
| 1272 | const Value *Arg = Call->getArgOperand(0); |
| 1273 | Arg = FindSingleUseIdentifiedObject(Arg); |
| 1274 | if (Arg) { |
| 1275 | Changed = true; |
| 1276 | ++NumAutoreleases; |
| 1277 | |
| 1278 | // Create the declaration lazily. |
| 1279 | LLVMContext &C = Inst->getContext(); |
Michael Gottesman | 01df450 | 2013-07-06 01:41:35 +0000 | [diff] [blame] | 1280 | |
Michael Gottesman | 14acfac | 2013-07-06 01:39:23 +0000 | [diff] [blame] | 1281 | Constant *Decl = EP.get(ARCRuntimeEntryPoints::EPT_Release); |
| 1282 | CallInst *NewCall = CallInst::Create(Decl, Call->getArgOperand(0), "", |
| 1283 | Call); |
Dmitri Gribenko | 3238fb7 | 2013-05-05 00:40:33 +0000 | [diff] [blame] | 1284 | NewCall->setMetadata(ImpreciseReleaseMDKind, MDNode::get(C, None)); |
Michael Gottesman | 10426b5 | 2013-01-07 21:26:07 +0000 | [diff] [blame] | 1285 | |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1286 | DEBUG(dbgs() << "Replacing autorelease{,RV}(x) with objc_release(x) " |
| 1287 | "since x is otherwise unused.\nOld: " << *Call << "\nNew: " |
| 1288 | << *NewCall << "\n"); |
Michael Gottesman | 10426b5 | 2013-01-07 21:26:07 +0000 | [diff] [blame] | 1289 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1290 | EraseInstruction(Call); |
| 1291 | Inst = NewCall; |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 1292 | Class = ARCInstKind::Release; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1293 | } |
| 1294 | } |
| 1295 | |
| 1296 | // For functions which can never be passed stack arguments, add |
| 1297 | // a tail keyword. |
| 1298 | if (IsAlwaysTail(Class)) { |
| 1299 | Changed = true; |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1300 | DEBUG(dbgs() << "Adding tail keyword to function since it can never be " |
| 1301 | "passed stack args: " << *Inst << "\n"); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1302 | cast<CallInst>(Inst)->setTailCall(); |
| 1303 | } |
| 1304 | |
Michael Gottesman | c9656fa | 2013-01-12 01:25:15 +0000 | [diff] [blame] | 1305 | // Ensure that functions that can never have a "tail" keyword due to the |
| 1306 | // semantics of ARC truly do not do so. |
| 1307 | if (IsNeverTail(Class)) { |
| 1308 | Changed = true; |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1309 | DEBUG(dbgs() << "Removing tail keyword from function: " << *Inst << |
Michael Gottesman | c9656fa | 2013-01-12 01:25:15 +0000 | [diff] [blame] | 1310 | "\n"); |
| 1311 | cast<CallInst>(Inst)->setTailCall(false); |
| 1312 | } |
| 1313 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1314 | // Set nounwind as needed. |
| 1315 | if (IsNoThrow(Class)) { |
| 1316 | Changed = true; |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1317 | DEBUG(dbgs() << "Found no throw class. Setting nounwind on: " << *Inst |
| 1318 | << "\n"); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1319 | cast<CallInst>(Inst)->setDoesNotThrow(); |
| 1320 | } |
| 1321 | |
| 1322 | if (!IsNoopOnNull(Class)) { |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 1323 | UsedInThisFunction |= 1 << unsigned(Class); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1324 | continue; |
| 1325 | } |
| 1326 | |
Michael Gottesman | e5ad66f | 2015-02-19 00:42:38 +0000 | [diff] [blame] | 1327 | const Value *Arg = GetArgRCIdentityRoot(Inst); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1328 | |
| 1329 | // ARC calls with null are no-ops. Delete them. |
Michael Gottesman | 65c2481 | 2013-03-25 09:27:43 +0000 | [diff] [blame] | 1330 | if (IsNullOrUndef(Arg)) { |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1331 | Changed = true; |
| 1332 | ++NumNoops; |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1333 | DEBUG(dbgs() << "ARC calls with null are no-ops. Erasing: " << *Inst |
| 1334 | << "\n"); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1335 | EraseInstruction(Inst); |
| 1336 | continue; |
| 1337 | } |
| 1338 | |
| 1339 | // Keep track of which of retain, release, autorelease, and retain_block |
| 1340 | // are actually present in this function. |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 1341 | UsedInThisFunction |= 1 << unsigned(Class); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1342 | |
| 1343 | // If Arg is a PHI, and one or more incoming values to the |
| 1344 | // PHI are null, and the call is control-equivalent to the PHI, and there |
| 1345 | // are no relevant side effects between the PHI and the call, the call |
| 1346 | // could be pushed up to just those paths with non-null incoming values. |
| 1347 | // For now, don't bother splitting critical edges for this. |
| 1348 | SmallVector<std::pair<Instruction *, const Value *>, 4> Worklist; |
| 1349 | Worklist.push_back(std::make_pair(Inst, Arg)); |
| 1350 | do { |
| 1351 | std::pair<Instruction *, const Value *> Pair = Worklist.pop_back_val(); |
| 1352 | Inst = Pair.first; |
| 1353 | Arg = Pair.second; |
| 1354 | |
| 1355 | const PHINode *PN = dyn_cast<PHINode>(Arg); |
| 1356 | if (!PN) continue; |
| 1357 | |
| 1358 | // Determine if the PHI has any null operands, or any incoming |
| 1359 | // critical edges. |
| 1360 | bool HasNull = false; |
| 1361 | bool HasCriticalEdges = false; |
| 1362 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 1363 | Value *Incoming = |
Michael Gottesman | e5ad66f | 2015-02-19 00:42:38 +0000 | [diff] [blame] | 1364 | GetRCIdentityRoot(PN->getIncomingValue(i)); |
Michael Gottesman | 65c2481 | 2013-03-25 09:27:43 +0000 | [diff] [blame] | 1365 | if (IsNullOrUndef(Incoming)) |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1366 | HasNull = true; |
| 1367 | else if (cast<TerminatorInst>(PN->getIncomingBlock(i)->back()) |
| 1368 | .getNumSuccessors() != 1) { |
| 1369 | HasCriticalEdges = true; |
| 1370 | break; |
| 1371 | } |
| 1372 | } |
| 1373 | // If we have null operands and no critical edges, optimize. |
| 1374 | if (!HasCriticalEdges && HasNull) { |
| 1375 | SmallPtrSet<Instruction *, 4> DependingInstructions; |
| 1376 | SmallPtrSet<const BasicBlock *, 4> Visited; |
| 1377 | |
| 1378 | // Check that there is nothing that cares about the reference |
| 1379 | // count between the call and the phi. |
Dan Gohman | 8478d76 | 2012-04-13 00:59:57 +0000 | [diff] [blame] | 1380 | switch (Class) { |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 1381 | case ARCInstKind::Retain: |
| 1382 | case ARCInstKind::RetainBlock: |
Dan Gohman | 8478d76 | 2012-04-13 00:59:57 +0000 | [diff] [blame] | 1383 | // These can always be moved up. |
| 1384 | break; |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 1385 | case ARCInstKind::Release: |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 1386 | // These can't be moved across things that care about the retain |
| 1387 | // count. |
Dan Gohman | 8478d76 | 2012-04-13 00:59:57 +0000 | [diff] [blame] | 1388 | FindDependencies(NeedsPositiveRetainCount, Arg, |
| 1389 | Inst->getParent(), Inst, |
| 1390 | DependingInstructions, Visited, PA); |
| 1391 | break; |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 1392 | case ARCInstKind::Autorelease: |
Dan Gohman | 8478d76 | 2012-04-13 00:59:57 +0000 | [diff] [blame] | 1393 | // These can't be moved across autorelease pool scope boundaries. |
| 1394 | FindDependencies(AutoreleasePoolBoundary, Arg, |
| 1395 | Inst->getParent(), Inst, |
| 1396 | DependingInstructions, Visited, PA); |
| 1397 | break; |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 1398 | case ARCInstKind::RetainRV: |
| 1399 | case ARCInstKind::AutoreleaseRV: |
Dan Gohman | 8478d76 | 2012-04-13 00:59:57 +0000 | [diff] [blame] | 1400 | // Don't move these; the RV optimization depends on the autoreleaseRV |
| 1401 | // being tail called, and the retainRV being immediately after a call |
| 1402 | // (which might still happen if we get lucky with codegen layout, but |
| 1403 | // it's not worth taking the chance). |
| 1404 | continue; |
| 1405 | default: |
| 1406 | llvm_unreachable("Invalid dependence flavor"); |
| 1407 | } |
| 1408 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1409 | if (DependingInstructions.size() == 1 && |
| 1410 | *DependingInstructions.begin() == PN) { |
| 1411 | Changed = true; |
| 1412 | ++NumPartialNoops; |
| 1413 | // Clone the call into each predecessor that has a non-null value. |
| 1414 | CallInst *CInst = cast<CallInst>(Inst); |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 1415 | Type *ParamTy = CInst->getArgOperand(0)->getType(); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1416 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 1417 | Value *Incoming = |
Michael Gottesman | e5ad66f | 2015-02-19 00:42:38 +0000 | [diff] [blame] | 1418 | GetRCIdentityRoot(PN->getIncomingValue(i)); |
Michael Gottesman | 65c2481 | 2013-03-25 09:27:43 +0000 | [diff] [blame] | 1419 | if (!IsNullOrUndef(Incoming)) { |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1420 | CallInst *Clone = cast<CallInst>(CInst->clone()); |
| 1421 | Value *Op = PN->getIncomingValue(i); |
| 1422 | Instruction *InsertPos = &PN->getIncomingBlock(i)->back(); |
| 1423 | if (Op->getType() != ParamTy) |
| 1424 | Op = new BitCastInst(Op, ParamTy, "", InsertPos); |
| 1425 | Clone->setArgOperand(0, Op); |
| 1426 | Clone->insertBefore(InsertPos); |
Michael Gottesman | c189a39 | 2013-01-09 19:23:24 +0000 | [diff] [blame] | 1427 | |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1428 | DEBUG(dbgs() << "Cloning " |
Michael Gottesman | c189a39 | 2013-01-09 19:23:24 +0000 | [diff] [blame] | 1429 | << *CInst << "\n" |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1430 | "And inserting clone at " << *InsertPos << "\n"); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1431 | Worklist.push_back(std::make_pair(Clone, Incoming)); |
| 1432 | } |
| 1433 | } |
| 1434 | // Erase the original call. |
Michael Gottesman | c189a39 | 2013-01-09 19:23:24 +0000 | [diff] [blame] | 1435 | DEBUG(dbgs() << "Erasing: " << *CInst << "\n"); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1436 | EraseInstruction(CInst); |
| 1437 | continue; |
| 1438 | } |
| 1439 | } |
| 1440 | } while (!Worklist.empty()); |
| 1441 | } |
| 1442 | } |
| 1443 | |
Michael Gottesman | 323964c | 2013-04-18 05:39:45 +0000 | [diff] [blame] | 1444 | /// If we have a top down pointer in the S_Use state, make sure that there are |
| 1445 | /// no CFG hazards by checking the states of various bottom up pointers. |
| 1446 | static void CheckForUseCFGHazard(const Sequence SuccSSeq, |
| 1447 | const bool SuccSRRIKnownSafe, |
| 1448 | PtrState &S, |
| 1449 | bool &SomeSuccHasSame, |
| 1450 | bool &AllSuccsHaveSame, |
Michael Gottesman | e67f40c | 2013-05-24 20:44:05 +0000 | [diff] [blame] | 1451 | bool &NotAllSeqEqualButKnownSafe, |
Michael Gottesman | 323964c | 2013-04-18 05:39:45 +0000 | [diff] [blame] | 1452 | bool &ShouldContinue) { |
| 1453 | switch (SuccSSeq) { |
| 1454 | case S_CanRelease: { |
Michael Gottesman | 9313225 | 2013-06-21 06:59:02 +0000 | [diff] [blame] | 1455 | if (!S.IsKnownSafe() && !SuccSRRIKnownSafe) { |
Michael Gottesman | 323964c | 2013-04-18 05:39:45 +0000 | [diff] [blame] | 1456 | S.ClearSequenceProgress(); |
| 1457 | break; |
| 1458 | } |
Michael Gottesman | 2f29459 | 2013-06-21 19:12:36 +0000 | [diff] [blame] | 1459 | S.SetCFGHazardAfflicted(true); |
Michael Gottesman | 323964c | 2013-04-18 05:39:45 +0000 | [diff] [blame] | 1460 | ShouldContinue = true; |
| 1461 | break; |
| 1462 | } |
| 1463 | case S_Use: |
| 1464 | SomeSuccHasSame = true; |
| 1465 | break; |
| 1466 | case S_Stop: |
| 1467 | case S_Release: |
| 1468 | case S_MovableRelease: |
Michael Gottesman | 9313225 | 2013-06-21 06:59:02 +0000 | [diff] [blame] | 1469 | if (!S.IsKnownSafe() && !SuccSRRIKnownSafe) |
Michael Gottesman | 323964c | 2013-04-18 05:39:45 +0000 | [diff] [blame] | 1470 | AllSuccsHaveSame = false; |
Michael Gottesman | e67f40c | 2013-05-24 20:44:05 +0000 | [diff] [blame] | 1471 | else |
| 1472 | NotAllSeqEqualButKnownSafe = true; |
Michael Gottesman | 323964c | 2013-04-18 05:39:45 +0000 | [diff] [blame] | 1473 | break; |
| 1474 | case S_Retain: |
| 1475 | llvm_unreachable("bottom-up pointer in retain state!"); |
| 1476 | case S_None: |
| 1477 | llvm_unreachable("This should have been handled earlier."); |
| 1478 | } |
| 1479 | } |
| 1480 | |
| 1481 | /// If we have a Top Down pointer in the S_CanRelease state, make sure that |
| 1482 | /// there are no CFG hazards by checking the states of various bottom up |
| 1483 | /// pointers. |
| 1484 | static void CheckForCanReleaseCFGHazard(const Sequence SuccSSeq, |
| 1485 | const bool SuccSRRIKnownSafe, |
| 1486 | PtrState &S, |
| 1487 | bool &SomeSuccHasSame, |
Michael Gottesman | e67f40c | 2013-05-24 20:44:05 +0000 | [diff] [blame] | 1488 | bool &AllSuccsHaveSame, |
| 1489 | bool &NotAllSeqEqualButKnownSafe) { |
Michael Gottesman | 323964c | 2013-04-18 05:39:45 +0000 | [diff] [blame] | 1490 | switch (SuccSSeq) { |
| 1491 | case S_CanRelease: |
| 1492 | SomeSuccHasSame = true; |
| 1493 | break; |
| 1494 | case S_Stop: |
| 1495 | case S_Release: |
| 1496 | case S_MovableRelease: |
| 1497 | case S_Use: |
Michael Gottesman | 9313225 | 2013-06-21 06:59:02 +0000 | [diff] [blame] | 1498 | if (!S.IsKnownSafe() && !SuccSRRIKnownSafe) |
Michael Gottesman | 323964c | 2013-04-18 05:39:45 +0000 | [diff] [blame] | 1499 | AllSuccsHaveSame = false; |
Michael Gottesman | e67f40c | 2013-05-24 20:44:05 +0000 | [diff] [blame] | 1500 | else |
| 1501 | NotAllSeqEqualButKnownSafe = true; |
Michael Gottesman | 323964c | 2013-04-18 05:39:45 +0000 | [diff] [blame] | 1502 | break; |
| 1503 | case S_Retain: |
| 1504 | llvm_unreachable("bottom-up pointer in retain state!"); |
| 1505 | case S_None: |
| 1506 | llvm_unreachable("This should have been handled earlier."); |
| 1507 | } |
| 1508 | } |
| 1509 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 1510 | /// Check for critical edges, loop boundaries, irreducible control flow, or |
| 1511 | /// other CFG structures where moving code across the edge would result in it |
| 1512 | /// being executed more. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1513 | void |
| 1514 | ObjCARCOpt::CheckForCFGHazards(const BasicBlock *BB, |
| 1515 | DenseMap<const BasicBlock *, BBState> &BBStates, |
| 1516 | BBState &MyStates) const { |
| 1517 | // If any top-down local-use or possible-dec has a succ which is earlier in |
| 1518 | // the sequence, forget it. |
Dan Gohman | 55b0674 | 2012-03-02 01:13:53 +0000 | [diff] [blame] | 1519 | for (BBState::ptr_iterator I = MyStates.top_down_ptr_begin(), |
Michael Gottesman | 323964c | 2013-04-18 05:39:45 +0000 | [diff] [blame] | 1520 | E = MyStates.top_down_ptr_end(); I != E; ++I) { |
| 1521 | PtrState &S = I->second; |
| 1522 | const Sequence Seq = I->second.GetSeq(); |
Dan Gohman | 0155f30 | 2012-02-17 18:59:53 +0000 | [diff] [blame] | 1523 | |
Michael Gottesman | 323964c | 2013-04-18 05:39:45 +0000 | [diff] [blame] | 1524 | // We only care about S_Retain, S_CanRelease, and S_Use. |
| 1525 | if (Seq == S_None) |
| 1526 | continue; |
Dan Gohman | 0155f30 | 2012-02-17 18:59:53 +0000 | [diff] [blame] | 1527 | |
Michael Gottesman | 323964c | 2013-04-18 05:39:45 +0000 | [diff] [blame] | 1528 | // Make sure that if extra top down states are added in the future that this |
| 1529 | // code is updated to handle it. |
| 1530 | assert((Seq == S_Retain || Seq == S_CanRelease || Seq == S_Use) && |
| 1531 | "Unknown top down sequence state."); |
| 1532 | |
| 1533 | const Value *Arg = I->first; |
| 1534 | const TerminatorInst *TI = cast<TerminatorInst>(&BB->back()); |
| 1535 | bool SomeSuccHasSame = false; |
| 1536 | bool AllSuccsHaveSame = true; |
Michael Gottesman | e67f40c | 2013-05-24 20:44:05 +0000 | [diff] [blame] | 1537 | bool NotAllSeqEqualButKnownSafe = false; |
Michael Gottesman | 323964c | 2013-04-18 05:39:45 +0000 | [diff] [blame] | 1538 | |
| 1539 | succ_const_iterator SI(TI), SE(TI, false); |
| 1540 | |
| 1541 | for (; SI != SE; ++SI) { |
| 1542 | // If VisitBottomUp has pointer information for this successor, take |
| 1543 | // what we know about it. |
| 1544 | const DenseMap<const BasicBlock *, BBState>::iterator BBI = |
| 1545 | BBStates.find(*SI); |
| 1546 | assert(BBI != BBStates.end()); |
| 1547 | const PtrState &SuccS = BBI->second.getPtrBottomUpState(Arg); |
| 1548 | const Sequence SuccSSeq = SuccS.GetSeq(); |
| 1549 | |
| 1550 | // If bottom up, the pointer is in an S_None state, clear the sequence |
| 1551 | // progress since the sequence in the bottom up state finished |
| 1552 | // suggesting a mismatch in between retains/releases. This is true for |
| 1553 | // all three cases that we are handling here: S_Retain, S_Use, and |
| 1554 | // S_CanRelease. |
| 1555 | if (SuccSSeq == S_None) { |
Dan Gohman | 1213027 | 2011-08-12 00:26:31 +0000 | [diff] [blame] | 1556 | S.ClearSequenceProgress(); |
Michael Gottesman | 323964c | 2013-04-18 05:39:45 +0000 | [diff] [blame] | 1557 | continue; |
| 1558 | } |
| 1559 | |
| 1560 | // If we have S_Use or S_CanRelease, perform our check for cfg hazard |
| 1561 | // checks. |
Michael Gottesman | 9313225 | 2013-06-21 06:59:02 +0000 | [diff] [blame] | 1562 | const bool SuccSRRIKnownSafe = SuccS.IsKnownSafe(); |
Michael Gottesman | 323964c | 2013-04-18 05:39:45 +0000 | [diff] [blame] | 1563 | |
| 1564 | // *NOTE* We do not use Seq from above here since we are allowing for |
| 1565 | // S.GetSeq() to change while we are visiting basic blocks. |
| 1566 | switch(S.GetSeq()) { |
| 1567 | case S_Use: { |
| 1568 | bool ShouldContinue = false; |
Michael Gottesman | e67f40c | 2013-05-24 20:44:05 +0000 | [diff] [blame] | 1569 | CheckForUseCFGHazard(SuccSSeq, SuccSRRIKnownSafe, S, SomeSuccHasSame, |
| 1570 | AllSuccsHaveSame, NotAllSeqEqualButKnownSafe, |
Michael Gottesman | 323964c | 2013-04-18 05:39:45 +0000 | [diff] [blame] | 1571 | ShouldContinue); |
| 1572 | if (ShouldContinue) |
| 1573 | continue; |
| 1574 | break; |
| 1575 | } |
| 1576 | case S_CanRelease: { |
Michael Gottesman | e67f40c | 2013-05-24 20:44:05 +0000 | [diff] [blame] | 1577 | CheckForCanReleaseCFGHazard(SuccSSeq, SuccSRRIKnownSafe, S, |
| 1578 | SomeSuccHasSame, AllSuccsHaveSame, |
| 1579 | NotAllSeqEqualButKnownSafe); |
Michael Gottesman | 323964c | 2013-04-18 05:39:45 +0000 | [diff] [blame] | 1580 | break; |
| 1581 | } |
| 1582 | case S_Retain: |
| 1583 | case S_None: |
| 1584 | case S_Stop: |
| 1585 | case S_Release: |
| 1586 | case S_MovableRelease: |
| 1587 | break; |
| 1588 | } |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1589 | } |
Michael Gottesman | 323964c | 2013-04-18 05:39:45 +0000 | [diff] [blame] | 1590 | |
| 1591 | // If the state at the other end of any of the successor edges |
| 1592 | // matches the current state, require all edges to match. This |
| 1593 | // guards against loops in the middle of a sequence. |
Michael Gottesman | e67f40c | 2013-05-24 20:44:05 +0000 | [diff] [blame] | 1594 | if (SomeSuccHasSame && !AllSuccsHaveSame) { |
Michael Gottesman | 323964c | 2013-04-18 05:39:45 +0000 | [diff] [blame] | 1595 | S.ClearSequenceProgress(); |
Michael Gottesman | e67f40c | 2013-05-24 20:44:05 +0000 | [diff] [blame] | 1596 | } else if (NotAllSeqEqualButKnownSafe) { |
| 1597 | // If we would have cleared the state foregoing the fact that we are known |
| 1598 | // safe, stop code motion. This is because whether or not it is safe to |
| 1599 | // remove RR pairs via KnownSafe is an orthogonal concept to whether we |
| 1600 | // are allowed to perform code motion. |
Michael Gottesman | 2f29459 | 2013-06-21 19:12:36 +0000 | [diff] [blame] | 1601 | S.SetCFGHazardAfflicted(true); |
Michael Gottesman | e67f40c | 2013-05-24 20:44:05 +0000 | [diff] [blame] | 1602 | } |
Michael Gottesman | 323964c | 2013-04-18 05:39:45 +0000 | [diff] [blame] | 1603 | } |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1604 | } |
| 1605 | |
Michael Gottesman | 0be6920 | 2015-03-05 23:28:58 +0000 | [diff] [blame^] | 1606 | bool ObjCARCOpt::VisitInstructionBottomUp( |
| 1607 | Instruction *Inst, BasicBlock *BB, BlotMapVector<Value *, RRInfo> &Retains, |
| 1608 | BBState &MyStates) { |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1609 | bool NestingDetected = false; |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 1610 | ARCInstKind Class = GetARCInstKind(Inst); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1611 | const Value *Arg = nullptr; |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 1612 | |
Michael Gottesman | 1d8d257 | 2013-04-05 22:54:28 +0000 | [diff] [blame] | 1613 | DEBUG(dbgs() << "Class: " << Class << "\n"); |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 1614 | |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1615 | switch (Class) { |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 1616 | case ARCInstKind::Release: { |
Michael Gottesman | e5ad66f | 2015-02-19 00:42:38 +0000 | [diff] [blame] | 1617 | Arg = GetArgRCIdentityRoot(Inst); |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1618 | |
| 1619 | PtrState &S = MyStates.getPtrBottomUpState(Arg); |
| 1620 | |
| 1621 | // If we see two releases in a row on the same pointer. If so, make |
| 1622 | // a note, and we'll cicle back to revisit it after we've |
| 1623 | // hopefully eliminated the second release, which may allow us to |
| 1624 | // eliminate the first release too. |
| 1625 | // Theoretically we could implement removal of nested retain+release |
| 1626 | // pairs by making PtrState hold a stack of states, but this is |
| 1627 | // simple and avoids adding overhead for the non-nested case. |
Michael Gottesman | af2113f | 2013-01-13 07:00:51 +0000 | [diff] [blame] | 1628 | if (S.GetSeq() == S_Release || S.GetSeq() == S_MovableRelease) { |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1629 | DEBUG(dbgs() << "Found nested releases (i.e. a release pair)\n"); |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1630 | NestingDetected = true; |
Michael Gottesman | af2113f | 2013-01-13 07:00:51 +0000 | [diff] [blame] | 1631 | } |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1632 | |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 1633 | MDNode *ReleaseMetadata = Inst->getMetadata(ImpreciseReleaseMDKind); |
Michael Gottesman | 81b1d43 | 2013-03-26 00:42:04 +0000 | [diff] [blame] | 1634 | Sequence NewSeq = ReleaseMetadata ? S_MovableRelease : S_Release; |
| 1635 | ANNOTATE_BOTTOMUP(Inst, Arg, S.GetSeq(), NewSeq); |
| 1636 | S.ResetSequenceProgress(NewSeq); |
Michael Gottesman | f701d3f | 2013-06-21 07:03:07 +0000 | [diff] [blame] | 1637 | S.SetReleaseMetadata(ReleaseMetadata); |
Michael Gottesman | 9313225 | 2013-06-21 06:59:02 +0000 | [diff] [blame] | 1638 | S.SetKnownSafe(S.HasKnownPositiveRefCount()); |
Michael Gottesman | b82a179 | 2013-06-21 07:00:44 +0000 | [diff] [blame] | 1639 | S.SetTailCallRelease(cast<CallInst>(Inst)->isTailCall()); |
Michael Gottesman | 4f6ef11 | 2013-06-21 19:44:27 +0000 | [diff] [blame] | 1640 | S.InsertCall(Inst); |
Dan Gohman | df476e5 | 2012-09-04 23:16:20 +0000 | [diff] [blame] | 1641 | S.SetKnownPositiveRefCount(); |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1642 | break; |
| 1643 | } |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 1644 | case ARCInstKind::RetainBlock: |
Michael Gottesman | 158fdf6 | 2013-03-28 20:11:19 +0000 | [diff] [blame] | 1645 | // In OptimizeIndividualCalls, we have strength reduced all optimizable |
| 1646 | // objc_retainBlocks to objc_retains. Thus at this point any |
| 1647 | // objc_retainBlocks that we see are not optimizable. |
| 1648 | break; |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 1649 | case ARCInstKind::Retain: |
| 1650 | case ARCInstKind::RetainRV: { |
Michael Gottesman | e5ad66f | 2015-02-19 00:42:38 +0000 | [diff] [blame] | 1651 | Arg = GetArgRCIdentityRoot(Inst); |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1652 | |
| 1653 | PtrState &S = MyStates.getPtrBottomUpState(Arg); |
Dan Gohman | 62079b4 | 2012-04-25 00:50:46 +0000 | [diff] [blame] | 1654 | S.SetKnownPositiveRefCount(); |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1655 | |
Michael Gottesman | 81b1d43 | 2013-03-26 00:42:04 +0000 | [diff] [blame] | 1656 | Sequence OldSeq = S.GetSeq(); |
| 1657 | switch (OldSeq) { |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1658 | case S_Stop: |
| 1659 | case S_Release: |
| 1660 | case S_MovableRelease: |
| 1661 | case S_Use: |
Michael Gottesman | 1d8d257 | 2013-04-05 22:54:28 +0000 | [diff] [blame] | 1662 | // If OldSeq is not S_Use or OldSeq is S_Use and we are tracking an |
| 1663 | // imprecise release, clear our reverse insertion points. |
Michael Gottesman | f040118 | 2013-06-21 19:12:38 +0000 | [diff] [blame] | 1664 | if (OldSeq != S_Use || S.IsTrackingImpreciseReleases()) |
Michael Gottesman | 4f6ef11 | 2013-06-21 19:44:27 +0000 | [diff] [blame] | 1665 | S.ClearReverseInsertPts(); |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1666 | // FALL THROUGH |
| 1667 | case S_CanRelease: |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 1668 | // Don't do retain+release tracking for ARCInstKind::RetainRV, |
| 1669 | // because it's |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1670 | // better to let it remain as the first instruction after a call. |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 1671 | if (Class != ARCInstKind::RetainRV) |
Michael Gottesman | e3943d0 | 2013-06-21 19:44:30 +0000 | [diff] [blame] | 1672 | Retains[Inst] = S.GetRRInfo(); |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1673 | S.ClearSequenceProgress(); |
| 1674 | break; |
| 1675 | case S_None: |
| 1676 | break; |
| 1677 | case S_Retain: |
| 1678 | llvm_unreachable("bottom-up pointer in retain state!"); |
| 1679 | } |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 1680 | ANNOTATE_BOTTOMUP(Inst, Arg, OldSeq, S.GetSeq()); |
Michael Gottesman | 31ba23a | 2013-04-05 22:54:32 +0000 | [diff] [blame] | 1681 | // A retain moving bottom up can be a use. |
| 1682 | break; |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1683 | } |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 1684 | case ARCInstKind::AutoreleasepoolPop: |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1685 | // Conservatively, clear MyStates for all known pointers. |
| 1686 | MyStates.clearBottomUpPointers(); |
| 1687 | return NestingDetected; |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 1688 | case ARCInstKind::AutoreleasepoolPush: |
| 1689 | case ARCInstKind::None: |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1690 | // These are irrelevant. |
| 1691 | return NestingDetected; |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 1692 | case ARCInstKind::User: |
Michael Gottesman | a76143ee | 2013-05-13 23:49:42 +0000 | [diff] [blame] | 1693 | // If we have a store into an alloca of a pointer we are tracking, the |
| 1694 | // pointer has multiple owners implying that we must be more conservative. |
| 1695 | // |
| 1696 | // This comes up in the context of a pointer being ``KnownSafe''. In the |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 1697 | // presence of a block being initialized, the frontend will emit the |
Michael Gottesman | a76143ee | 2013-05-13 23:49:42 +0000 | [diff] [blame] | 1698 | // objc_retain on the original pointer and the release on the pointer loaded |
| 1699 | // from the alloca. The optimizer will through the provenance analysis |
| 1700 | // realize that the two are related, but since we only require KnownSafe in |
| 1701 | // one direction, will match the inner retain on the original pointer with |
| 1702 | // the guard release on the original pointer. This is fixed by ensuring that |
Alp Toker | cb40291 | 2014-01-24 17:20:08 +0000 | [diff] [blame] | 1703 | // in the presence of allocas we only unconditionally remove pointers if |
Michael Gottesman | a76143ee | 2013-05-13 23:49:42 +0000 | [diff] [blame] | 1704 | // both our retain and our release are KnownSafe. |
| 1705 | if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) { |
| 1706 | if (AreAnyUnderlyingObjectsAnAlloca(SI->getPointerOperand())) { |
| 1707 | BBState::ptr_iterator I = MyStates.findPtrBottomUpState( |
Michael Gottesman | e5ad66f | 2015-02-19 00:42:38 +0000 | [diff] [blame] | 1708 | GetRCIdentityRoot(SI->getValueOperand())); |
Michael Gottesman | a76143ee | 2013-05-13 23:49:42 +0000 | [diff] [blame] | 1709 | if (I != MyStates.bottom_up_ptr_end()) |
Michael Gottesman | 5a91bbf | 2013-05-24 20:44:02 +0000 | [diff] [blame] | 1710 | MultiOwnersSet.insert(I->first); |
Michael Gottesman | a76143ee | 2013-05-13 23:49:42 +0000 | [diff] [blame] | 1711 | } |
| 1712 | } |
| 1713 | break; |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1714 | default: |
| 1715 | break; |
| 1716 | } |
| 1717 | |
| 1718 | // Consider any other possible effects of this instruction on each |
| 1719 | // pointer being tracked. |
| 1720 | for (BBState::ptr_iterator MI = MyStates.bottom_up_ptr_begin(), |
| 1721 | ME = MyStates.bottom_up_ptr_end(); MI != ME; ++MI) { |
| 1722 | const Value *Ptr = MI->first; |
| 1723 | if (Ptr == Arg) |
| 1724 | continue; // Handled above. |
| 1725 | PtrState &S = MI->second; |
| 1726 | Sequence Seq = S.GetSeq(); |
| 1727 | |
| 1728 | // Check for possible releases. |
| 1729 | if (CanAlterRefCount(Inst, Ptr, PA, Class)) { |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1730 | DEBUG(dbgs() << "CanAlterRefCount: Seq: " << Seq << "; " << *Ptr |
| 1731 | << "\n"); |
Michael Gottesman | 764b1cf | 2013-03-23 05:46:19 +0000 | [diff] [blame] | 1732 | S.ClearKnownPositiveRefCount(); |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1733 | switch (Seq) { |
| 1734 | case S_Use: |
| 1735 | S.SetSeq(S_CanRelease); |
Michael Gottesman | 81b1d43 | 2013-03-26 00:42:04 +0000 | [diff] [blame] | 1736 | ANNOTATE_BOTTOMUP(Inst, Ptr, Seq, S.GetSeq()); |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1737 | continue; |
| 1738 | case S_CanRelease: |
| 1739 | case S_Release: |
| 1740 | case S_MovableRelease: |
| 1741 | case S_Stop: |
| 1742 | case S_None: |
| 1743 | break; |
| 1744 | case S_Retain: |
| 1745 | llvm_unreachable("bottom-up pointer in retain state!"); |
| 1746 | } |
| 1747 | } |
| 1748 | |
| 1749 | // Check for possible direct uses. |
| 1750 | switch (Seq) { |
| 1751 | case S_Release: |
| 1752 | case S_MovableRelease: |
| 1753 | if (CanUse(Inst, Ptr, PA, Class)) { |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1754 | DEBUG(dbgs() << "CanUse: Seq: " << Seq << "; " << *Ptr |
| 1755 | << "\n"); |
Michael Gottesman | 4f6ef11 | 2013-06-21 19:44:27 +0000 | [diff] [blame] | 1756 | assert(!S.HasReverseInsertPts()); |
Dan Gohman | 5c70fad | 2012-03-23 17:47:54 +0000 | [diff] [blame] | 1757 | // If this is an invoke instruction, we're scanning it as part of |
| 1758 | // one of its successor blocks, since we can't insert code after it |
| 1759 | // in its own block, and we don't want to split critical edges. |
| 1760 | if (isa<InvokeInst>(Inst)) |
Michael Gottesman | 4f6ef11 | 2013-06-21 19:44:27 +0000 | [diff] [blame] | 1761 | S.InsertReverseInsertPt(BB->getFirstInsertionPt()); |
Dan Gohman | 5c70fad | 2012-03-23 17:47:54 +0000 | [diff] [blame] | 1762 | else |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 1763 | S.InsertReverseInsertPt(std::next(BasicBlock::iterator(Inst))); |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1764 | S.SetSeq(S_Use); |
Michael Gottesman | 81b1d43 | 2013-03-26 00:42:04 +0000 | [diff] [blame] | 1765 | ANNOTATE_BOTTOMUP(Inst, Ptr, Seq, S_Use); |
John McCall | 20182ac | 2013-03-22 21:38:36 +0000 | [diff] [blame] | 1766 | } else if (Seq == S_Release && IsUser(Class)) { |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1767 | DEBUG(dbgs() << "PreciseReleaseUse: Seq: " << Seq << "; " << *Ptr |
| 1768 | << "\n"); |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1769 | // Non-movable releases depend on any possible objc pointer use. |
| 1770 | S.SetSeq(S_Stop); |
Michael Gottesman | 81b1d43 | 2013-03-26 00:42:04 +0000 | [diff] [blame] | 1771 | ANNOTATE_BOTTOMUP(Inst, Ptr, S_Release, S_Stop); |
Michael Gottesman | 4f6ef11 | 2013-06-21 19:44:27 +0000 | [diff] [blame] | 1772 | assert(!S.HasReverseInsertPts()); |
Dan Gohman | 5c70fad | 2012-03-23 17:47:54 +0000 | [diff] [blame] | 1773 | // As above; handle invoke specially. |
| 1774 | if (isa<InvokeInst>(Inst)) |
Michael Gottesman | 4f6ef11 | 2013-06-21 19:44:27 +0000 | [diff] [blame] | 1775 | S.InsertReverseInsertPt(BB->getFirstInsertionPt()); |
Dan Gohman | 5c70fad | 2012-03-23 17:47:54 +0000 | [diff] [blame] | 1776 | else |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 1777 | S.InsertReverseInsertPt(std::next(BasicBlock::iterator(Inst))); |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1778 | } |
| 1779 | break; |
| 1780 | case S_Stop: |
Michael Gottesman | 81b1d43 | 2013-03-26 00:42:04 +0000 | [diff] [blame] | 1781 | if (CanUse(Inst, Ptr, PA, Class)) { |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1782 | DEBUG(dbgs() << "PreciseStopUse: Seq: " << Seq << "; " << *Ptr |
| 1783 | << "\n"); |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1784 | S.SetSeq(S_Use); |
Michael Gottesman | 81b1d43 | 2013-03-26 00:42:04 +0000 | [diff] [blame] | 1785 | ANNOTATE_BOTTOMUP(Inst, Ptr, Seq, S_Use); |
| 1786 | } |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1787 | break; |
| 1788 | case S_CanRelease: |
| 1789 | case S_Use: |
| 1790 | case S_None: |
| 1791 | break; |
| 1792 | case S_Retain: |
| 1793 | llvm_unreachable("bottom-up pointer in retain state!"); |
| 1794 | } |
| 1795 | } |
| 1796 | |
| 1797 | return NestingDetected; |
| 1798 | } |
| 1799 | |
Michael Gottesman | 0be6920 | 2015-03-05 23:28:58 +0000 | [diff] [blame^] | 1800 | bool ObjCARCOpt::VisitBottomUp(BasicBlock *BB, |
| 1801 | DenseMap<const BasicBlock *, BBState> &BBStates, |
| 1802 | BlotMapVector<Value *, RRInfo> &Retains) { |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1803 | |
| 1804 | DEBUG(dbgs() << "\n== ObjCARCOpt::VisitBottomUp ==\n"); |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 1805 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1806 | bool NestingDetected = false; |
| 1807 | BBState &MyStates = BBStates[BB]; |
| 1808 | |
| 1809 | // Merge the states from each successor to compute the initial state |
| 1810 | // for the current block. |
Dan Gohman | 10c82ce | 2012-08-27 18:31:36 +0000 | [diff] [blame] | 1811 | BBState::edge_iterator SI(MyStates.succ_begin()), |
| 1812 | SE(MyStates.succ_end()); |
| 1813 | if (SI != SE) { |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 1814 | const BasicBlock *Succ = *SI; |
| 1815 | DenseMap<const BasicBlock *, BBState>::iterator I = BBStates.find(Succ); |
| 1816 | assert(I != BBStates.end()); |
| 1817 | MyStates.InitFromSucc(I->second); |
| 1818 | ++SI; |
| 1819 | for (; SI != SE; ++SI) { |
| 1820 | Succ = *SI; |
| 1821 | I = BBStates.find(Succ); |
| 1822 | assert(I != BBStates.end()); |
| 1823 | MyStates.MergeSucc(I->second); |
| 1824 | } |
Michael Gottesman | 60f6b28 | 2013-03-29 05:13:07 +0000 | [diff] [blame] | 1825 | } |
Michael Gottesman | cd4de0f | 2013-03-26 00:42:09 +0000 | [diff] [blame] | 1826 | |
Michael Gottesman | 43e7e00 | 2013-04-03 22:41:59 +0000 | [diff] [blame] | 1827 | // If ARC Annotations are enabled, output the current state of pointers at the |
Michael Gottesman | c2d5bf5 | 2013-04-03 23:07:45 +0000 | [diff] [blame] | 1828 | // bottom of the basic block. |
Michael Gottesman | 43e7e00 | 2013-04-03 22:41:59 +0000 | [diff] [blame] | 1829 | ANNOTATE_BOTTOMUP_BBEND(MyStates, BB); |
Michael Gottesman | c2d5bf5 | 2013-04-03 23:07:45 +0000 | [diff] [blame] | 1830 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1831 | // Visit all the instructions, bottom-up. |
| 1832 | for (BasicBlock::iterator I = BB->end(), E = BB->begin(); I != E; --I) { |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 1833 | Instruction *Inst = std::prev(I); |
Dan Gohman | 5c70fad | 2012-03-23 17:47:54 +0000 | [diff] [blame] | 1834 | |
| 1835 | // Invoke instructions are visited as part of their successors (below). |
| 1836 | if (isa<InvokeInst>(Inst)) |
| 1837 | continue; |
| 1838 | |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 1839 | DEBUG(dbgs() << "Visiting " << *Inst << "\n"); |
Michael Gottesman | af2113f | 2013-01-13 07:00:51 +0000 | [diff] [blame] | 1840 | |
Dan Gohman | 5c70fad | 2012-03-23 17:47:54 +0000 | [diff] [blame] | 1841 | NestingDetected |= VisitInstructionBottomUp(Inst, BB, Retains, MyStates); |
| 1842 | } |
| 1843 | |
Dan Gohman | dae3349 | 2012-04-27 18:56:31 +0000 | [diff] [blame] | 1844 | // If there's a predecessor with an invoke, visit the invoke as if it were |
| 1845 | // part of this block, since we can't insert code after an invoke in its own |
| 1846 | // block, and we don't want to split critical edges. |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 1847 | for (BBState::edge_iterator PI(MyStates.pred_begin()), |
| 1848 | PE(MyStates.pred_end()); PI != PE; ++PI) { |
Dan Gohman | 5c70fad | 2012-03-23 17:47:54 +0000 | [diff] [blame] | 1849 | BasicBlock *Pred = *PI; |
Dan Gohman | dae3349 | 2012-04-27 18:56:31 +0000 | [diff] [blame] | 1850 | if (InvokeInst *II = dyn_cast<InvokeInst>(&Pred->back())) |
| 1851 | NestingDetected |= VisitInstructionBottomUp(II, BB, Retains, MyStates); |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1852 | } |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1853 | |
Michael Gottesman | 43e7e00 | 2013-04-03 22:41:59 +0000 | [diff] [blame] | 1854 | // If ARC Annotations are enabled, output the current state of pointers at the |
| 1855 | // top of the basic block. |
| 1856 | ANNOTATE_BOTTOMUP_BBSTART(MyStates, BB); |
Michael Gottesman | c2d5bf5 | 2013-04-03 23:07:45 +0000 | [diff] [blame] | 1857 | |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1858 | return NestingDetected; |
| 1859 | } |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1860 | |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1861 | bool |
| 1862 | ObjCARCOpt::VisitInstructionTopDown(Instruction *Inst, |
| 1863 | DenseMap<Value *, RRInfo> &Releases, |
| 1864 | BBState &MyStates) { |
| 1865 | bool NestingDetected = false; |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 1866 | ARCInstKind Class = GetARCInstKind(Inst); |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1867 | const Value *Arg = nullptr; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1868 | |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1869 | switch (Class) { |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 1870 | case ARCInstKind::RetainBlock: |
Michael Gottesman | 158fdf6 | 2013-03-28 20:11:19 +0000 | [diff] [blame] | 1871 | // In OptimizeIndividualCalls, we have strength reduced all optimizable |
| 1872 | // objc_retainBlocks to objc_retains. Thus at this point any |
| 1873 | // objc_retainBlocks that we see are not optimizable. |
| 1874 | break; |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 1875 | case ARCInstKind::Retain: |
| 1876 | case ARCInstKind::RetainRV: { |
Michael Gottesman | e5ad66f | 2015-02-19 00:42:38 +0000 | [diff] [blame] | 1877 | Arg = GetArgRCIdentityRoot(Inst); |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1878 | |
| 1879 | PtrState &S = MyStates.getPtrTopDownState(Arg); |
| 1880 | |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 1881 | // Don't do retain+release tracking for ARCInstKind::RetainRV, because |
| 1882 | // it's |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1883 | // better to let it remain as the first instruction after a call. |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 1884 | if (Class != ARCInstKind::RetainRV) { |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1885 | // If we see two retains in a row on the same pointer. If so, make |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1886 | // a note, and we'll cicle back to revisit it after we've |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1887 | // hopefully eliminated the second retain, which may allow us to |
| 1888 | // eliminate the first retain too. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1889 | // Theoretically we could implement removal of nested retain+release |
| 1890 | // pairs by making PtrState hold a stack of states, but this is |
| 1891 | // simple and avoids adding overhead for the non-nested case. |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1892 | if (S.GetSeq() == S_Retain) |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1893 | NestingDetected = true; |
| 1894 | |
Michael Gottesman | 81b1d43 | 2013-03-26 00:42:04 +0000 | [diff] [blame] | 1895 | ANNOTATE_TOPDOWN(Inst, Arg, S.GetSeq(), S_Retain); |
Dan Gohman | 62079b4 | 2012-04-25 00:50:46 +0000 | [diff] [blame] | 1896 | S.ResetSequenceProgress(S_Retain); |
Michael Gottesman | 9313225 | 2013-06-21 06:59:02 +0000 | [diff] [blame] | 1897 | S.SetKnownSafe(S.HasKnownPositiveRefCount()); |
Michael Gottesman | 4f6ef11 | 2013-06-21 19:44:27 +0000 | [diff] [blame] | 1898 | S.InsertCall(Inst); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1899 | } |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1900 | |
Dan Gohman | df476e5 | 2012-09-04 23:16:20 +0000 | [diff] [blame] | 1901 | S.SetKnownPositiveRefCount(); |
Dan Gohman | f64ff8e | 2012-07-23 19:27:31 +0000 | [diff] [blame] | 1902 | |
| 1903 | // A retain can be a potential use; procede to the generic checking |
| 1904 | // code below. |
| 1905 | break; |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1906 | } |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 1907 | case ARCInstKind::Release: { |
Michael Gottesman | e5ad66f | 2015-02-19 00:42:38 +0000 | [diff] [blame] | 1908 | Arg = GetArgRCIdentityRoot(Inst); |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1909 | |
| 1910 | PtrState &S = MyStates.getPtrTopDownState(Arg); |
Michael Gottesman | 764b1cf | 2013-03-23 05:46:19 +0000 | [diff] [blame] | 1911 | S.ClearKnownPositiveRefCount(); |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 1912 | |
Michael Gottesman | 1d8d257 | 2013-04-05 22:54:28 +0000 | [diff] [blame] | 1913 | Sequence OldSeq = S.GetSeq(); |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 1914 | |
Duncan P. N. Exon Smith | de36e80 | 2014-11-11 21:30:22 +0000 | [diff] [blame] | 1915 | MDNode *ReleaseMetadata = Inst->getMetadata(ImpreciseReleaseMDKind); |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 1916 | |
Michael Gottesman | 1d8d257 | 2013-04-05 22:54:28 +0000 | [diff] [blame] | 1917 | switch (OldSeq) { |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1918 | case S_Retain: |
| 1919 | case S_CanRelease: |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 1920 | if (OldSeq == S_Retain || ReleaseMetadata != nullptr) |
Michael Gottesman | 4f6ef11 | 2013-06-21 19:44:27 +0000 | [diff] [blame] | 1921 | S.ClearReverseInsertPts(); |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1922 | // FALL THROUGH |
| 1923 | case S_Use: |
Michael Gottesman | f701d3f | 2013-06-21 07:03:07 +0000 | [diff] [blame] | 1924 | S.SetReleaseMetadata(ReleaseMetadata); |
Michael Gottesman | b82a179 | 2013-06-21 07:00:44 +0000 | [diff] [blame] | 1925 | S.SetTailCallRelease(cast<CallInst>(Inst)->isTailCall()); |
Michael Gottesman | e3943d0 | 2013-06-21 19:44:30 +0000 | [diff] [blame] | 1926 | Releases[Inst] = S.GetRRInfo(); |
Michael Gottesman | 81b1d43 | 2013-03-26 00:42:04 +0000 | [diff] [blame] | 1927 | ANNOTATE_TOPDOWN(Inst, Arg, S.GetSeq(), S_None); |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1928 | S.ClearSequenceProgress(); |
| 1929 | break; |
| 1930 | case S_None: |
| 1931 | break; |
| 1932 | case S_Stop: |
| 1933 | case S_Release: |
| 1934 | case S_MovableRelease: |
| 1935 | llvm_unreachable("top-down pointer in release state!"); |
| 1936 | } |
| 1937 | break; |
| 1938 | } |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 1939 | case ARCInstKind::AutoreleasepoolPop: |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1940 | // Conservatively, clear MyStates for all known pointers. |
| 1941 | MyStates.clearTopDownPointers(); |
| 1942 | return NestingDetected; |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 1943 | case ARCInstKind::AutoreleasepoolPush: |
| 1944 | case ARCInstKind::None: |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1945 | // These are irrelevant. |
| 1946 | return NestingDetected; |
| 1947 | default: |
| 1948 | break; |
| 1949 | } |
| 1950 | |
| 1951 | // Consider any other possible effects of this instruction on each |
| 1952 | // pointer being tracked. |
| 1953 | for (BBState::ptr_iterator MI = MyStates.top_down_ptr_begin(), |
| 1954 | ME = MyStates.top_down_ptr_end(); MI != ME; ++MI) { |
| 1955 | const Value *Ptr = MI->first; |
| 1956 | if (Ptr == Arg) |
| 1957 | continue; // Handled above. |
| 1958 | PtrState &S = MI->second; |
| 1959 | Sequence Seq = S.GetSeq(); |
| 1960 | |
| 1961 | // Check for possible releases. |
| 1962 | if (CanAlterRefCount(Inst, Ptr, PA, Class)) { |
Michael Gottesman | bab49e97 | 2013-04-05 18:26:08 +0000 | [diff] [blame] | 1963 | DEBUG(dbgs() << "CanAlterRefCount: Seq: " << Seq << "; " << *Ptr |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 1964 | << "\n"); |
Michael Gottesman | 764b1cf | 2013-03-23 05:46:19 +0000 | [diff] [blame] | 1965 | S.ClearKnownPositiveRefCount(); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1966 | switch (Seq) { |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1967 | case S_Retain: |
| 1968 | S.SetSeq(S_CanRelease); |
Michael Gottesman | 81b1d43 | 2013-03-26 00:42:04 +0000 | [diff] [blame] | 1969 | ANNOTATE_TOPDOWN(Inst, Ptr, Seq, S_CanRelease); |
Michael Gottesman | 4f6ef11 | 2013-06-21 19:44:27 +0000 | [diff] [blame] | 1970 | assert(!S.HasReverseInsertPts()); |
| 1971 | S.InsertReverseInsertPt(Inst); |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1972 | |
| 1973 | // One call can't cause a transition from S_Retain to S_CanRelease |
| 1974 | // and S_CanRelease to S_Use. If we've made the first transition, |
| 1975 | // we're done. |
| 1976 | continue; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1977 | case S_Use: |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1978 | case S_CanRelease: |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1979 | case S_None: |
| 1980 | break; |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1981 | case S_Stop: |
| 1982 | case S_Release: |
| 1983 | case S_MovableRelease: |
| 1984 | llvm_unreachable("top-down pointer in release state!"); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 1985 | } |
| 1986 | } |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1987 | |
| 1988 | // Check for possible direct uses. |
| 1989 | switch (Seq) { |
| 1990 | case S_CanRelease: |
Michael Gottesman | 81b1d43 | 2013-03-26 00:42:04 +0000 | [diff] [blame] | 1991 | if (CanUse(Inst, Ptr, PA, Class)) { |
Michael Gottesman | bab49e97 | 2013-04-05 18:26:08 +0000 | [diff] [blame] | 1992 | DEBUG(dbgs() << "CanUse: Seq: " << Seq << "; " << *Ptr |
| 1993 | << "\n"); |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1994 | S.SetSeq(S_Use); |
Michael Gottesman | 81b1d43 | 2013-03-26 00:42:04 +0000 | [diff] [blame] | 1995 | ANNOTATE_TOPDOWN(Inst, Ptr, Seq, S_Use); |
| 1996 | } |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 1997 | break; |
| 1998 | case S_Retain: |
| 1999 | case S_Use: |
| 2000 | case S_None: |
| 2001 | break; |
| 2002 | case S_Stop: |
| 2003 | case S_Release: |
| 2004 | case S_MovableRelease: |
| 2005 | llvm_unreachable("top-down pointer in release state!"); |
| 2006 | } |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2007 | } |
| 2008 | |
| 2009 | return NestingDetected; |
| 2010 | } |
| 2011 | |
| 2012 | bool |
| 2013 | ObjCARCOpt::VisitTopDown(BasicBlock *BB, |
| 2014 | DenseMap<const BasicBlock *, BBState> &BBStates, |
| 2015 | DenseMap<Value *, RRInfo> &Releases) { |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 2016 | DEBUG(dbgs() << "\n== ObjCARCOpt::VisitTopDown ==\n"); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2017 | bool NestingDetected = false; |
| 2018 | BBState &MyStates = BBStates[BB]; |
| 2019 | |
| 2020 | // Merge the states from each predecessor to compute the initial state |
| 2021 | // for the current block. |
Dan Gohman | 10c82ce | 2012-08-27 18:31:36 +0000 | [diff] [blame] | 2022 | BBState::edge_iterator PI(MyStates.pred_begin()), |
| 2023 | PE(MyStates.pred_end()); |
| 2024 | if (PI != PE) { |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 2025 | const BasicBlock *Pred = *PI; |
| 2026 | DenseMap<const BasicBlock *, BBState>::iterator I = BBStates.find(Pred); |
| 2027 | assert(I != BBStates.end()); |
| 2028 | MyStates.InitFromPred(I->second); |
| 2029 | ++PI; |
| 2030 | for (; PI != PE; ++PI) { |
| 2031 | Pred = *PI; |
| 2032 | I = BBStates.find(Pred); |
| 2033 | assert(I != BBStates.end()); |
| 2034 | MyStates.MergePred(I->second); |
| 2035 | } |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 2036 | } |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2037 | |
Michael Gottesman | 43e7e00 | 2013-04-03 22:41:59 +0000 | [diff] [blame] | 2038 | // If ARC Annotations are enabled, output the current state of pointers at the |
| 2039 | // top of the basic block. |
| 2040 | ANNOTATE_TOPDOWN_BBSTART(MyStates, BB); |
Michael Gottesman | c2d5bf5 | 2013-04-03 23:07:45 +0000 | [diff] [blame] | 2041 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2042 | // Visit all the instructions, top-down. |
| 2043 | for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) { |
| 2044 | Instruction *Inst = I; |
Michael Gottesman | af2113f | 2013-01-13 07:00:51 +0000 | [diff] [blame] | 2045 | |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 2046 | DEBUG(dbgs() << "Visiting " << *Inst << "\n"); |
Michael Gottesman | af2113f | 2013-01-13 07:00:51 +0000 | [diff] [blame] | 2047 | |
Dan Gohman | 817a7c6 | 2012-03-22 18:24:56 +0000 | [diff] [blame] | 2048 | NestingDetected |= VisitInstructionTopDown(Inst, Releases, MyStates); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2049 | } |
Michael Gottesman | c2d5bf5 | 2013-04-03 23:07:45 +0000 | [diff] [blame] | 2050 | |
Michael Gottesman | 43e7e00 | 2013-04-03 22:41:59 +0000 | [diff] [blame] | 2051 | // If ARC Annotations are enabled, output the current state of pointers at the |
| 2052 | // bottom of the basic block. |
| 2053 | ANNOTATE_TOPDOWN_BBEND(MyStates, BB); |
Michael Gottesman | c2d5bf5 | 2013-04-03 23:07:45 +0000 | [diff] [blame] | 2054 | |
Michael Gottesman | ffef24f | 2013-04-17 20:48:01 +0000 | [diff] [blame] | 2055 | #ifdef ARC_ANNOTATIONS |
Michael Gottesman | adb921a | 2013-04-17 21:03:53 +0000 | [diff] [blame] | 2056 | if (!(EnableARCAnnotations && DisableCheckForCFGHazards)) |
Michael Gottesman | ffef24f | 2013-04-17 20:48:01 +0000 | [diff] [blame] | 2057 | #endif |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2058 | CheckForCFGHazards(BB, BBStates, MyStates); |
| 2059 | return NestingDetected; |
| 2060 | } |
| 2061 | |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 2062 | static void |
| 2063 | ComputePostOrders(Function &F, |
| 2064 | SmallVectorImpl<BasicBlock *> &PostOrder, |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 2065 | SmallVectorImpl<BasicBlock *> &ReverseCFGPostOrder, |
| 2066 | unsigned NoObjCARCExceptionsMDKind, |
| 2067 | DenseMap<const BasicBlock *, BBState> &BBStates) { |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 2068 | /// The visited set, for doing DFS walks. |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 2069 | SmallPtrSet<BasicBlock *, 16> Visited; |
| 2070 | |
| 2071 | // Do DFS, computing the PostOrder. |
| 2072 | SmallPtrSet<BasicBlock *, 16> OnStack; |
| 2073 | SmallVector<std::pair<BasicBlock *, succ_iterator>, 16> SuccStack; |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 2074 | |
| 2075 | // Functions always have exactly one entry block, and we don't have |
| 2076 | // any other block that we treat like an entry block. |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 2077 | BasicBlock *EntryBB = &F.getEntryBlock(); |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 2078 | BBState &MyStates = BBStates[EntryBB]; |
| 2079 | MyStates.SetAsEntry(); |
| 2080 | TerminatorInst *EntryTI = cast<TerminatorInst>(&EntryBB->back()); |
| 2081 | SuccStack.push_back(std::make_pair(EntryBB, succ_iterator(EntryTI))); |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 2082 | Visited.insert(EntryBB); |
| 2083 | OnStack.insert(EntryBB); |
| 2084 | do { |
| 2085 | dfs_next_succ: |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 2086 | BasicBlock *CurrBB = SuccStack.back().first; |
| 2087 | TerminatorInst *TI = cast<TerminatorInst>(&CurrBB->back()); |
| 2088 | succ_iterator SE(TI, false); |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 2089 | |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 2090 | while (SuccStack.back().second != SE) { |
| 2091 | BasicBlock *SuccBB = *SuccStack.back().second++; |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 2092 | if (Visited.insert(SuccBB).second) { |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 2093 | TerminatorInst *TI = cast<TerminatorInst>(&SuccBB->back()); |
| 2094 | SuccStack.push_back(std::make_pair(SuccBB, succ_iterator(TI))); |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 2095 | BBStates[CurrBB].addSucc(SuccBB); |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 2096 | BBState &SuccStates = BBStates[SuccBB]; |
| 2097 | SuccStates.addPred(CurrBB); |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 2098 | OnStack.insert(SuccBB); |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 2099 | goto dfs_next_succ; |
| 2100 | } |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 2101 | |
| 2102 | if (!OnStack.count(SuccBB)) { |
| 2103 | BBStates[CurrBB].addSucc(SuccBB); |
| 2104 | BBStates[SuccBB].addPred(CurrBB); |
| 2105 | } |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 2106 | } |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 2107 | OnStack.erase(CurrBB); |
| 2108 | PostOrder.push_back(CurrBB); |
| 2109 | SuccStack.pop_back(); |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 2110 | } while (!SuccStack.empty()); |
| 2111 | |
| 2112 | Visited.clear(); |
| 2113 | |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 2114 | // Do reverse-CFG DFS, computing the reverse-CFG PostOrder. |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 2115 | // Functions may have many exits, and there also blocks which we treat |
| 2116 | // as exits due to ignored edges. |
| 2117 | SmallVector<std::pair<BasicBlock *, BBState::edge_iterator>, 16> PredStack; |
| 2118 | for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) { |
| 2119 | BasicBlock *ExitBB = I; |
| 2120 | BBState &MyStates = BBStates[ExitBB]; |
| 2121 | if (!MyStates.isExit()) |
| 2122 | continue; |
| 2123 | |
Dan Gohman | dae3349 | 2012-04-27 18:56:31 +0000 | [diff] [blame] | 2124 | MyStates.SetAsExit(); |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 2125 | |
| 2126 | PredStack.push_back(std::make_pair(ExitBB, MyStates.pred_begin())); |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 2127 | Visited.insert(ExitBB); |
| 2128 | while (!PredStack.empty()) { |
| 2129 | reverse_dfs_next_succ: |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 2130 | BBState::edge_iterator PE = BBStates[PredStack.back().first].pred_end(); |
| 2131 | while (PredStack.back().second != PE) { |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 2132 | BasicBlock *BB = *PredStack.back().second++; |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 2133 | if (Visited.insert(BB).second) { |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 2134 | PredStack.push_back(std::make_pair(BB, BBStates[BB].pred_begin())); |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 2135 | goto reverse_dfs_next_succ; |
| 2136 | } |
| 2137 | } |
| 2138 | ReverseCFGPostOrder.push_back(PredStack.pop_back_val().first); |
| 2139 | } |
| 2140 | } |
| 2141 | } |
| 2142 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 2143 | // Visit the function both top-down and bottom-up. |
Michael Gottesman | 0be6920 | 2015-03-05 23:28:58 +0000 | [diff] [blame^] | 2144 | bool ObjCARCOpt::Visit(Function &F, |
| 2145 | DenseMap<const BasicBlock *, BBState> &BBStates, |
| 2146 | BlotMapVector<Value *, RRInfo> &Retains, |
| 2147 | DenseMap<Value *, RRInfo> &Releases) { |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 2148 | |
| 2149 | // Use reverse-postorder traversals, because we magically know that loops |
| 2150 | // will be well behaved, i.e. they won't repeatedly call retain on a single |
| 2151 | // pointer without doing a release. We can't use the ReversePostOrderTraversal |
| 2152 | // class here because we want the reverse-CFG postorder to consider each |
| 2153 | // function exit point, and we want to ignore selected cycle edges. |
| 2154 | SmallVector<BasicBlock *, 16> PostOrder; |
| 2155 | SmallVector<BasicBlock *, 16> ReverseCFGPostOrder; |
Dan Gohman | c24c66f | 2012-04-24 22:53:18 +0000 | [diff] [blame] | 2156 | ComputePostOrders(F, PostOrder, ReverseCFGPostOrder, |
| 2157 | NoObjCARCExceptionsMDKind, |
| 2158 | BBStates); |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 2159 | |
| 2160 | // Use reverse-postorder on the reverse CFG for bottom-up. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2161 | bool BottomUpNestingDetected = false; |
Dan Gohman | c57b58c | 2011-08-18 21:27:42 +0000 | [diff] [blame] | 2162 | for (SmallVectorImpl<BasicBlock *>::const_reverse_iterator I = |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 2163 | ReverseCFGPostOrder.rbegin(), E = ReverseCFGPostOrder.rend(); |
| 2164 | I != E; ++I) |
| 2165 | BottomUpNestingDetected |= VisitBottomUp(*I, BBStates, Retains); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2166 | |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 2167 | // Use reverse-postorder for top-down. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2168 | bool TopDownNestingDetected = false; |
Dan Gohman | a53a12c | 2011-12-12 19:42:25 +0000 | [diff] [blame] | 2169 | for (SmallVectorImpl<BasicBlock *>::const_reverse_iterator I = |
| 2170 | PostOrder.rbegin(), E = PostOrder.rend(); |
| 2171 | I != E; ++I) |
| 2172 | TopDownNestingDetected |= VisitTopDown(*I, BBStates, Releases); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2173 | |
| 2174 | return TopDownNestingDetected && BottomUpNestingDetected; |
| 2175 | } |
| 2176 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 2177 | /// Move the calls in RetainsToMove and ReleasesToMove. |
Michael Gottesman | 0be6920 | 2015-03-05 23:28:58 +0000 | [diff] [blame^] | 2178 | void ObjCARCOpt::MoveCalls(Value *Arg, RRInfo &RetainsToMove, |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2179 | RRInfo &ReleasesToMove, |
Michael Gottesman | 0be6920 | 2015-03-05 23:28:58 +0000 | [diff] [blame^] | 2180 | BlotMapVector<Value *, RRInfo> &Retains, |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2181 | DenseMap<Value *, RRInfo> &Releases, |
Dan Gohman | 6320f52 | 2011-07-22 22:29:21 +0000 | [diff] [blame] | 2182 | SmallVectorImpl<Instruction *> &DeadInsts, |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 2183 | Module *M) { |
Chris Lattner | 229907c | 2011-07-18 04:54:35 +0000 | [diff] [blame] | 2184 | Type *ArgTy = Arg->getType(); |
Dan Gohman | 6320f52 | 2011-07-22 22:29:21 +0000 | [diff] [blame] | 2185 | Type *ParamTy = PointerType::getUnqual(Type::getInt8Ty(ArgTy->getContext())); |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 2186 | |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 2187 | DEBUG(dbgs() << "== ObjCARCOpt::MoveCalls ==\n"); |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 2188 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2189 | // Insert the new retain and release calls. |
Craig Topper | 4627679 | 2014-08-24 23:23:06 +0000 | [diff] [blame] | 2190 | for (Instruction *InsertPt : ReleasesToMove.ReverseInsertPts) { |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2191 | Value *MyArg = ArgTy == ParamTy ? Arg : |
| 2192 | new BitCastInst(Arg, ParamTy, "", InsertPt); |
Michael Gottesman | 14acfac | 2013-07-06 01:39:23 +0000 | [diff] [blame] | 2193 | Constant *Decl = EP.get(ARCRuntimeEntryPoints::EPT_Retain); |
| 2194 | CallInst *Call = CallInst::Create(Decl, MyArg, "", InsertPt); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2195 | Call->setDoesNotThrow(); |
Michael Gottesman | ba64859 | 2013-03-28 23:08:44 +0000 | [diff] [blame] | 2196 | Call->setTailCall(); |
Michael Gottesman | 60f6b28 | 2013-03-29 05:13:07 +0000 | [diff] [blame] | 2197 | |
Michael Gottesman | df110ac | 2013-04-21 00:30:50 +0000 | [diff] [blame] | 2198 | DEBUG(dbgs() << "Inserting new Retain: " << *Call << "\n" |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 2199 | "At insertion point: " << *InsertPt << "\n"); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2200 | } |
Craig Topper | 4627679 | 2014-08-24 23:23:06 +0000 | [diff] [blame] | 2201 | for (Instruction *InsertPt : RetainsToMove.ReverseInsertPts) { |
Dan Gohman | 5c70fad | 2012-03-23 17:47:54 +0000 | [diff] [blame] | 2202 | Value *MyArg = ArgTy == ParamTy ? Arg : |
| 2203 | new BitCastInst(Arg, ParamTy, "", InsertPt); |
Michael Gottesman | 14acfac | 2013-07-06 01:39:23 +0000 | [diff] [blame] | 2204 | Constant *Decl = EP.get(ARCRuntimeEntryPoints::EPT_Release); |
| 2205 | CallInst *Call = CallInst::Create(Decl, MyArg, "", InsertPt); |
Dan Gohman | 5c70fad | 2012-03-23 17:47:54 +0000 | [diff] [blame] | 2206 | // Attach a clang.imprecise_release metadata tag, if appropriate. |
| 2207 | if (MDNode *M = ReleasesToMove.ReleaseMetadata) |
| 2208 | Call->setMetadata(ImpreciseReleaseMDKind, M); |
| 2209 | Call->setDoesNotThrow(); |
| 2210 | if (ReleasesToMove.IsTailCallRelease) |
| 2211 | Call->setTailCall(); |
Michael Gottesman | c189a39 | 2013-01-09 19:23:24 +0000 | [diff] [blame] | 2212 | |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 2213 | DEBUG(dbgs() << "Inserting new Release: " << *Call << "\n" |
| 2214 | "At insertion point: " << *InsertPt << "\n"); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2215 | } |
| 2216 | |
| 2217 | // Delete the original retain and release calls. |
Craig Topper | 4627679 | 2014-08-24 23:23:06 +0000 | [diff] [blame] | 2218 | for (Instruction *OrigRetain : RetainsToMove.Calls) { |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2219 | Retains.blot(OrigRetain); |
| 2220 | DeadInsts.push_back(OrigRetain); |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 2221 | DEBUG(dbgs() << "Deleting retain: " << *OrigRetain << "\n"); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2222 | } |
Craig Topper | 4627679 | 2014-08-24 23:23:06 +0000 | [diff] [blame] | 2223 | for (Instruction *OrigRelease : ReleasesToMove.Calls) { |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2224 | Releases.erase(OrigRelease); |
| 2225 | DeadInsts.push_back(OrigRelease); |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 2226 | DEBUG(dbgs() << "Deleting release: " << *OrigRelease << "\n"); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2227 | } |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 2228 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2229 | } |
| 2230 | |
Michael Gottesman | 0be6920 | 2015-03-05 23:28:58 +0000 | [diff] [blame^] | 2231 | bool ObjCARCOpt::ConnectTDBUTraversals( |
| 2232 | DenseMap<const BasicBlock *, BBState> &BBStates, |
| 2233 | BlotMapVector<Value *, RRInfo> &Retains, |
| 2234 | DenseMap<Value *, RRInfo> &Releases, Module *M, |
| 2235 | SmallVectorImpl<Instruction *> &NewRetains, |
| 2236 | SmallVectorImpl<Instruction *> &NewReleases, |
| 2237 | SmallVectorImpl<Instruction *> &DeadInsts, RRInfo &RetainsToMove, |
| 2238 | RRInfo &ReleasesToMove, Value *Arg, bool KnownSafe, |
| 2239 | bool &AnyPairsCompletelyEliminated) { |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 2240 | // 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] | 2241 | // is already incremented, we can similarly ignore possible decrements unless |
| 2242 | // we are dealing with a retainable object with multiple provenance sources. |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 2243 | bool KnownSafeTD = true, KnownSafeBU = true; |
Michael Gottesman | a76143ee | 2013-05-13 23:49:42 +0000 | [diff] [blame] | 2244 | bool MultipleOwners = false; |
Michael Gottesman | e67f40c | 2013-05-24 20:44:05 +0000 | [diff] [blame] | 2245 | bool CFGHazardAfflicted = false; |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 2246 | |
| 2247 | // Connect the dots between the top-down-collected RetainsToMove and |
| 2248 | // bottom-up-collected ReleasesToMove to form sets of related calls. |
| 2249 | // This is an iterative process so that we connect multiple releases |
| 2250 | // to multiple retains if needed. |
| 2251 | unsigned OldDelta = 0; |
| 2252 | unsigned NewDelta = 0; |
| 2253 | unsigned OldCount = 0; |
| 2254 | unsigned NewCount = 0; |
| 2255 | bool FirstRelease = true; |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 2256 | for (;;) { |
| 2257 | for (SmallVectorImpl<Instruction *>::const_iterator |
| 2258 | NI = NewRetains.begin(), NE = NewRetains.end(); NI != NE; ++NI) { |
| 2259 | Instruction *NewRetain = *NI; |
Michael Gottesman | 0be6920 | 2015-03-05 23:28:58 +0000 | [diff] [blame^] | 2260 | BlotMapVector<Value *, RRInfo>::const_iterator It = |
| 2261 | Retains.find(NewRetain); |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 2262 | assert(It != Retains.end()); |
| 2263 | const RRInfo &NewRetainRRI = It->second; |
| 2264 | KnownSafeTD &= NewRetainRRI.KnownSafe; |
Michael Gottesman | 5a91bbf | 2013-05-24 20:44:02 +0000 | [diff] [blame] | 2265 | MultipleOwners = |
Michael Gottesman | e5ad66f | 2015-02-19 00:42:38 +0000 | [diff] [blame] | 2266 | MultipleOwners || MultiOwnersSet.count(GetArgRCIdentityRoot(NewRetain)); |
Craig Topper | 4627679 | 2014-08-24 23:23:06 +0000 | [diff] [blame] | 2267 | for (Instruction *NewRetainRelease : NewRetainRRI.Calls) { |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 2268 | DenseMap<Value *, RRInfo>::const_iterator Jt = |
| 2269 | Releases.find(NewRetainRelease); |
| 2270 | if (Jt == Releases.end()) |
| 2271 | return false; |
| 2272 | const RRInfo &NewRetainReleaseRRI = Jt->second; |
Michael Gottesman | 24b2f6f | 2013-11-05 16:02:40 +0000 | [diff] [blame] | 2273 | |
| 2274 | // If the release does not have a reference to the retain as well, |
| 2275 | // something happened which is unaccounted for. Do not do anything. |
| 2276 | // |
| 2277 | // This can happen if we catch an additive overflow during path count |
| 2278 | // merging. |
| 2279 | if (!NewRetainReleaseRRI.Calls.count(NewRetain)) |
| 2280 | return false; |
| 2281 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 2282 | if (ReleasesToMove.Calls.insert(NewRetainRelease).second) { |
Michael Gottesman | 9e7261c | 2013-06-07 06:16:49 +0000 | [diff] [blame] | 2283 | |
| 2284 | // If we overflow when we compute the path count, don't remove/move |
| 2285 | // anything. |
| 2286 | const BBState &NRRBBState = BBStates[NewRetainRelease->getParent()]; |
Michael Gottesman | d6ce6cb | 2013-08-09 23:22:27 +0000 | [diff] [blame] | 2287 | unsigned PathCount = BBState::OverflowOccurredValue; |
Michael Gottesman | 9e7261c | 2013-06-07 06:16:49 +0000 | [diff] [blame] | 2288 | if (NRRBBState.GetAllPathCountWithOverflow(PathCount)) |
| 2289 | return false; |
Michael Gottesman | d6ce6cb | 2013-08-09 23:22:27 +0000 | [diff] [blame] | 2290 | assert(PathCount != BBState::OverflowOccurredValue && |
| 2291 | "PathCount at this point can not be " |
| 2292 | "OverflowOccurredValue."); |
Michael Gottesman | 9e7261c | 2013-06-07 06:16:49 +0000 | [diff] [blame] | 2293 | OldDelta -= PathCount; |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 2294 | |
| 2295 | // Merge the ReleaseMetadata and IsTailCallRelease values. |
| 2296 | if (FirstRelease) { |
| 2297 | ReleasesToMove.ReleaseMetadata = |
| 2298 | NewRetainReleaseRRI.ReleaseMetadata; |
| 2299 | ReleasesToMove.IsTailCallRelease = |
| 2300 | NewRetainReleaseRRI.IsTailCallRelease; |
| 2301 | FirstRelease = false; |
| 2302 | } else { |
| 2303 | if (ReleasesToMove.ReleaseMetadata != |
| 2304 | NewRetainReleaseRRI.ReleaseMetadata) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2305 | ReleasesToMove.ReleaseMetadata = nullptr; |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 2306 | if (ReleasesToMove.IsTailCallRelease != |
| 2307 | NewRetainReleaseRRI.IsTailCallRelease) |
| 2308 | ReleasesToMove.IsTailCallRelease = false; |
| 2309 | } |
| 2310 | |
| 2311 | // Collect the optimal insertion points. |
| 2312 | if (!KnownSafe) |
Craig Topper | 4627679 | 2014-08-24 23:23:06 +0000 | [diff] [blame] | 2313 | for (Instruction *RIP : NewRetainReleaseRRI.ReverseInsertPts) { |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 2314 | if (ReleasesToMove.ReverseInsertPts.insert(RIP).second) { |
Michael Gottesman | 9e7261c | 2013-06-07 06:16:49 +0000 | [diff] [blame] | 2315 | // If we overflow when we compute the path count, don't |
| 2316 | // remove/move anything. |
| 2317 | const BBState &RIPBBState = BBStates[RIP->getParent()]; |
Michael Gottesman | d6ce6cb | 2013-08-09 23:22:27 +0000 | [diff] [blame] | 2318 | PathCount = BBState::OverflowOccurredValue; |
Michael Gottesman | 9e7261c | 2013-06-07 06:16:49 +0000 | [diff] [blame] | 2319 | if (RIPBBState.GetAllPathCountWithOverflow(PathCount)) |
| 2320 | return false; |
Michael Gottesman | d6ce6cb | 2013-08-09 23:22:27 +0000 | [diff] [blame] | 2321 | assert(PathCount != BBState::OverflowOccurredValue && |
| 2322 | "PathCount at this point can not be " |
| 2323 | "OverflowOccurredValue."); |
Michael Gottesman | 9e7261c | 2013-06-07 06:16:49 +0000 | [diff] [blame] | 2324 | NewDelta -= PathCount; |
| 2325 | } |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 2326 | } |
| 2327 | NewReleases.push_back(NewRetainRelease); |
| 2328 | } |
| 2329 | } |
| 2330 | } |
| 2331 | NewRetains.clear(); |
| 2332 | if (NewReleases.empty()) break; |
| 2333 | |
| 2334 | // Back the other way. |
| 2335 | for (SmallVectorImpl<Instruction *>::const_iterator |
| 2336 | NI = NewReleases.begin(), NE = NewReleases.end(); NI != NE; ++NI) { |
| 2337 | Instruction *NewRelease = *NI; |
| 2338 | DenseMap<Value *, RRInfo>::const_iterator It = |
| 2339 | Releases.find(NewRelease); |
| 2340 | assert(It != Releases.end()); |
| 2341 | const RRInfo &NewReleaseRRI = It->second; |
| 2342 | KnownSafeBU &= NewReleaseRRI.KnownSafe; |
Michael Gottesman | e67f40c | 2013-05-24 20:44:05 +0000 | [diff] [blame] | 2343 | CFGHazardAfflicted |= NewReleaseRRI.CFGHazardAfflicted; |
Craig Topper | 4627679 | 2014-08-24 23:23:06 +0000 | [diff] [blame] | 2344 | for (Instruction *NewReleaseRetain : NewReleaseRRI.Calls) { |
Michael Gottesman | 0be6920 | 2015-03-05 23:28:58 +0000 | [diff] [blame^] | 2345 | BlotMapVector<Value *, RRInfo>::const_iterator Jt = |
| 2346 | Retains.find(NewReleaseRetain); |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 2347 | if (Jt == Retains.end()) |
| 2348 | return false; |
| 2349 | const RRInfo &NewReleaseRetainRRI = Jt->second; |
Michael Gottesman | 9e7261c | 2013-06-07 06:16:49 +0000 | [diff] [blame] | 2350 | |
Michael Gottesman | 24b2f6f | 2013-11-05 16:02:40 +0000 | [diff] [blame] | 2351 | // If the retain does not have a reference to the release as well, |
| 2352 | // something happened which is unaccounted for. Do not do anything. |
| 2353 | // |
| 2354 | // This can happen if we catch an additive overflow during path count |
| 2355 | // merging. |
| 2356 | if (!NewReleaseRetainRRI.Calls.count(NewRelease)) |
| 2357 | return false; |
| 2358 | |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 2359 | if (RetainsToMove.Calls.insert(NewReleaseRetain).second) { |
Michael Gottesman | 9e7261c | 2013-06-07 06:16:49 +0000 | [diff] [blame] | 2360 | // If we overflow when we compute the path count, don't remove/move |
| 2361 | // anything. |
| 2362 | const BBState &NRRBBState = BBStates[NewReleaseRetain->getParent()]; |
Michael Gottesman | d6ce6cb | 2013-08-09 23:22:27 +0000 | [diff] [blame] | 2363 | unsigned PathCount = BBState::OverflowOccurredValue; |
Michael Gottesman | 9e7261c | 2013-06-07 06:16:49 +0000 | [diff] [blame] | 2364 | if (NRRBBState.GetAllPathCountWithOverflow(PathCount)) |
| 2365 | return false; |
Michael Gottesman | d6ce6cb | 2013-08-09 23:22:27 +0000 | [diff] [blame] | 2366 | assert(PathCount != BBState::OverflowOccurredValue && |
| 2367 | "PathCount at this point can not be " |
| 2368 | "OverflowOccurredValue."); |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 2369 | OldDelta += PathCount; |
| 2370 | OldCount += PathCount; |
| 2371 | |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 2372 | // Collect the optimal insertion points. |
| 2373 | if (!KnownSafe) |
Craig Topper | 4627679 | 2014-08-24 23:23:06 +0000 | [diff] [blame] | 2374 | for (Instruction *RIP : NewReleaseRetainRRI.ReverseInsertPts) { |
David Blaikie | 70573dc | 2014-11-19 07:49:26 +0000 | [diff] [blame] | 2375 | if (RetainsToMove.ReverseInsertPts.insert(RIP).second) { |
Michael Gottesman | 9e7261c | 2013-06-07 06:16:49 +0000 | [diff] [blame] | 2376 | // If we overflow when we compute the path count, don't |
| 2377 | // remove/move anything. |
| 2378 | const BBState &RIPBBState = BBStates[RIP->getParent()]; |
Michael Gottesman | d6ce6cb | 2013-08-09 23:22:27 +0000 | [diff] [blame] | 2379 | |
| 2380 | PathCount = BBState::OverflowOccurredValue; |
Michael Gottesman | 9e7261c | 2013-06-07 06:16:49 +0000 | [diff] [blame] | 2381 | if (RIPBBState.GetAllPathCountWithOverflow(PathCount)) |
| 2382 | return false; |
Michael Gottesman | d6ce6cb | 2013-08-09 23:22:27 +0000 | [diff] [blame] | 2383 | assert(PathCount != BBState::OverflowOccurredValue && |
| 2384 | "PathCount at this point can not be " |
| 2385 | "OverflowOccurredValue."); |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 2386 | NewDelta += PathCount; |
| 2387 | NewCount += PathCount; |
| 2388 | } |
| 2389 | } |
| 2390 | NewRetains.push_back(NewReleaseRetain); |
| 2391 | } |
| 2392 | } |
| 2393 | } |
| 2394 | NewReleases.clear(); |
| 2395 | if (NewRetains.empty()) break; |
| 2396 | } |
| 2397 | |
Michael Gottesman | a76143ee | 2013-05-13 23:49:42 +0000 | [diff] [blame] | 2398 | // If the pointer is known incremented in 1 direction and we do not have |
| 2399 | // MultipleOwners, we can safely remove the retain/releases. Otherwise we need |
| 2400 | // to be known safe in both directions. |
| 2401 | bool UnconditionallySafe = (KnownSafeTD && KnownSafeBU) || |
| 2402 | ((KnownSafeTD || KnownSafeBU) && !MultipleOwners); |
| 2403 | if (UnconditionallySafe) { |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 2404 | RetainsToMove.ReverseInsertPts.clear(); |
| 2405 | ReleasesToMove.ReverseInsertPts.clear(); |
| 2406 | NewCount = 0; |
| 2407 | } else { |
| 2408 | // Determine whether the new insertion points we computed preserve the |
| 2409 | // balance of retain and release calls through the program. |
| 2410 | // TODO: If the fully aggressive solution isn't valid, try to find a |
| 2411 | // less aggressive solution which is. |
| 2412 | if (NewDelta != 0) |
| 2413 | return false; |
Michael Gottesman | e67f40c | 2013-05-24 20:44:05 +0000 | [diff] [blame] | 2414 | |
| 2415 | // At this point, we are not going to remove any RR pairs, but we still are |
| 2416 | // able to move RR pairs. If one of our pointers is afflicted with |
| 2417 | // CFGHazards, we cannot perform such code motion so exit early. |
| 2418 | const bool WillPerformCodeMotion = RetainsToMove.ReverseInsertPts.size() || |
| 2419 | ReleasesToMove.ReverseInsertPts.size(); |
| 2420 | if (CFGHazardAfflicted && WillPerformCodeMotion) |
| 2421 | return false; |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 2422 | } |
| 2423 | |
| 2424 | // Determine whether the original call points are balanced in the retain and |
| 2425 | // release calls through the program. If not, conservatively don't touch |
| 2426 | // them. |
| 2427 | // TODO: It's theoretically possible to do code motion in this case, as |
| 2428 | // long as the existing imbalances are maintained. |
| 2429 | if (OldDelta != 0) |
| 2430 | return false; |
Michael Gottesman | 8005ad3 | 2013-04-29 06:16:55 +0000 | [diff] [blame] | 2431 | |
Michael Gottesman | a87bb8f | 2013-04-29 05:13:13 +0000 | [diff] [blame] | 2432 | #ifdef ARC_ANNOTATIONS |
| 2433 | // Do not move calls if ARC annotations are requested. |
Michael Gottesman | 3e3977c | 2013-04-29 05:25:39 +0000 | [diff] [blame] | 2434 | if (EnableARCAnnotations) |
| 2435 | return false; |
Michael Gottesman | a87bb8f | 2013-04-29 05:13:13 +0000 | [diff] [blame] | 2436 | #endif // ARC_ANNOTATIONS |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 2437 | |
| 2438 | Changed = true; |
| 2439 | assert(OldCount != 0 && "Unreachable code?"); |
| 2440 | NumRRs += OldCount - NewCount; |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 2441 | // Set to true if we completely removed any RR pairs. |
Michael Gottesman | 8b5515f | 2013-01-22 21:53:43 +0000 | [diff] [blame] | 2442 | AnyPairsCompletelyEliminated = NewCount == 0; |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 2443 | |
| 2444 | // We can move calls! |
| 2445 | return true; |
| 2446 | } |
| 2447 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 2448 | /// Identify pairings between the retains and releases, and delete and/or move |
| 2449 | /// them. |
Michael Gottesman | 0be6920 | 2015-03-05 23:28:58 +0000 | [diff] [blame^] | 2450 | bool ObjCARCOpt::PerformCodePlacement( |
| 2451 | DenseMap<const BasicBlock *, BBState> &BBStates, |
| 2452 | BlotMapVector<Value *, RRInfo> &Retains, |
| 2453 | DenseMap<Value *, RRInfo> &Releases, Module *M) { |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 2454 | DEBUG(dbgs() << "\n== ObjCARCOpt::PerformCodePlacement ==\n"); |
| 2455 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2456 | bool AnyPairsCompletelyEliminated = false; |
| 2457 | RRInfo RetainsToMove; |
| 2458 | RRInfo ReleasesToMove; |
| 2459 | SmallVector<Instruction *, 4> NewRetains; |
| 2460 | SmallVector<Instruction *, 4> NewReleases; |
| 2461 | SmallVector<Instruction *, 8> DeadInsts; |
| 2462 | |
Dan Gohman | 670f937 | 2012-04-13 18:57:48 +0000 | [diff] [blame] | 2463 | // Visit each retain. |
Michael Gottesman | 0be6920 | 2015-03-05 23:28:58 +0000 | [diff] [blame^] | 2464 | for (BlotMapVector<Value *, RRInfo>::const_iterator I = Retains.begin(), |
| 2465 | E = Retains.end(); |
| 2466 | I != E; ++I) { |
Dan Gohman | 2053a5d | 2011-09-29 22:25:23 +0000 | [diff] [blame] | 2467 | Value *V = I->first; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2468 | if (!V) continue; // blotted |
| 2469 | |
| 2470 | Instruction *Retain = cast<Instruction>(V); |
Michael Gottesman | c189a39 | 2013-01-09 19:23:24 +0000 | [diff] [blame] | 2471 | |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 2472 | DEBUG(dbgs() << "Visiting: " << *Retain << "\n"); |
Michael Gottesman | c189a39 | 2013-01-09 19:23:24 +0000 | [diff] [blame] | 2473 | |
Michael Gottesman | e5ad66f | 2015-02-19 00:42:38 +0000 | [diff] [blame] | 2474 | Value *Arg = GetArgRCIdentityRoot(Retain); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2475 | |
Dan Gohman | 728db49 | 2012-01-13 00:39:07 +0000 | [diff] [blame] | 2476 | // 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] | 2477 | // not being managed by ObjC reference counting, so we can delete pairs |
| 2478 | // regardless of what possible decrements or uses lie between them. |
Dan Gohman | 728db49 | 2012-01-13 00:39:07 +0000 | [diff] [blame] | 2479 | bool KnownSafe = isa<Constant>(Arg) || isa<AllocaInst>(Arg); |
Dan Gohman | 41375a3 | 2012-05-08 23:39:44 +0000 | [diff] [blame] | 2480 | |
Dan Gohman | 56e1cef | 2011-08-22 17:29:11 +0000 | [diff] [blame] | 2481 | // A constant pointer can't be pointing to an object on the heap. It may |
| 2482 | // be reference-counted, but it won't be deleted. |
| 2483 | if (const LoadInst *LI = dyn_cast<LoadInst>(Arg)) |
| 2484 | if (const GlobalVariable *GV = |
| 2485 | dyn_cast<GlobalVariable>( |
Michael Gottesman | e5ad66f | 2015-02-19 00:42:38 +0000 | [diff] [blame] | 2486 | GetRCIdentityRoot(LI->getPointerOperand()))) |
Dan Gohman | 56e1cef | 2011-08-22 17:29:11 +0000 | [diff] [blame] | 2487 | if (GV->isConstant()) |
| 2488 | KnownSafe = true; |
| 2489 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2490 | // Connect the dots between the top-down-collected RetainsToMove and |
| 2491 | // bottom-up-collected ReleasesToMove to form sets of related calls. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2492 | NewRetains.push_back(Retain); |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 2493 | bool PerformMoveCalls = |
| 2494 | ConnectTDBUTraversals(BBStates, Retains, Releases, M, NewRetains, |
| 2495 | NewReleases, DeadInsts, RetainsToMove, |
| 2496 | ReleasesToMove, Arg, KnownSafe, |
| 2497 | AnyPairsCompletelyEliminated); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2498 | |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 2499 | if (PerformMoveCalls) { |
| 2500 | // Ok, everything checks out and we're all set. Let's move/delete some |
| 2501 | // code! |
| 2502 | MoveCalls(Arg, RetainsToMove, ReleasesToMove, |
| 2503 | Retains, Releases, DeadInsts, M); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2504 | } |
| 2505 | |
Michael Gottesman | 9de6f96 | 2013-01-22 21:49:00 +0000 | [diff] [blame] | 2506 | // Clean up state for next retain. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2507 | NewReleases.clear(); |
| 2508 | NewRetains.clear(); |
| 2509 | RetainsToMove.clear(); |
| 2510 | ReleasesToMove.clear(); |
| 2511 | } |
| 2512 | |
| 2513 | // Now that we're done moving everything, we can delete the newly dead |
| 2514 | // instructions, as we no longer need them as insert points. |
| 2515 | while (!DeadInsts.empty()) |
| 2516 | EraseInstruction(DeadInsts.pop_back_val()); |
| 2517 | |
| 2518 | return AnyPairsCompletelyEliminated; |
| 2519 | } |
| 2520 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 2521 | /// Weak pointer optimizations. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2522 | void ObjCARCOpt::OptimizeWeakCalls(Function &F) { |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 2523 | DEBUG(dbgs() << "\n== ObjCARCOpt::OptimizeWeakCalls ==\n"); |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 2524 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2525 | // First, do memdep-style RLE and S2L optimizations. We can't use memdep |
| 2526 | // itself because it uses AliasAnalysis and we need to do provenance |
| 2527 | // queries instead. |
| 2528 | for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) { |
| 2529 | Instruction *Inst = &*I++; |
Michael Gottesman | 3f146e2 | 2013-01-01 16:05:48 +0000 | [diff] [blame] | 2530 | |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 2531 | DEBUG(dbgs() << "Visiting: " << *Inst << "\n"); |
Michael Gottesman | 3f146e2 | 2013-01-01 16:05:48 +0000 | [diff] [blame] | 2532 | |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 2533 | ARCInstKind Class = GetBasicARCInstKind(Inst); |
| 2534 | if (Class != ARCInstKind::LoadWeak && |
| 2535 | Class != ARCInstKind::LoadWeakRetained) |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2536 | continue; |
| 2537 | |
| 2538 | // Delete objc_loadWeak calls with no users. |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 2539 | if (Class == ARCInstKind::LoadWeak && Inst->use_empty()) { |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2540 | Inst->eraseFromParent(); |
| 2541 | continue; |
| 2542 | } |
| 2543 | |
| 2544 | // TODO: For now, just look for an earlier available version of this value |
| 2545 | // within the same block. Theoretically, we could do memdep-style non-local |
| 2546 | // analysis too, but that would want caching. A better approach would be to |
| 2547 | // use the technique that EarlyCSE uses. |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 2548 | inst_iterator Current = std::prev(I); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2549 | BasicBlock *CurrentBB = Current.getBasicBlockIterator(); |
| 2550 | for (BasicBlock::iterator B = CurrentBB->begin(), |
| 2551 | J = Current.getInstructionIterator(); |
| 2552 | J != B; --J) { |
Benjamin Kramer | b6d0bd4 | 2014-03-02 12:27:27 +0000 | [diff] [blame] | 2553 | Instruction *EarlierInst = &*std::prev(J); |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 2554 | ARCInstKind EarlierClass = GetARCInstKind(EarlierInst); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2555 | switch (EarlierClass) { |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 2556 | case ARCInstKind::LoadWeak: |
| 2557 | case ARCInstKind::LoadWeakRetained: { |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2558 | // If this is loading from the same pointer, replace this load's value |
| 2559 | // with that one. |
| 2560 | CallInst *Call = cast<CallInst>(Inst); |
| 2561 | CallInst *EarlierCall = cast<CallInst>(EarlierInst); |
| 2562 | Value *Arg = Call->getArgOperand(0); |
| 2563 | Value *EarlierArg = EarlierCall->getArgOperand(0); |
| 2564 | switch (PA.getAA()->alias(Arg, EarlierArg)) { |
| 2565 | case AliasAnalysis::MustAlias: |
| 2566 | Changed = true; |
| 2567 | // 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] | 2568 | if (Class == ARCInstKind::LoadWeakRetained) { |
Michael Gottesman | 14acfac | 2013-07-06 01:39:23 +0000 | [diff] [blame] | 2569 | Constant *Decl = EP.get(ARCRuntimeEntryPoints::EPT_Retain); |
| 2570 | CallInst *CI = CallInst::Create(Decl, EarlierCall, "", Call); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2571 | CI->setTailCall(); |
| 2572 | } |
| 2573 | // Zap the fully redundant load. |
| 2574 | Call->replaceAllUsesWith(EarlierCall); |
| 2575 | Call->eraseFromParent(); |
| 2576 | goto clobbered; |
| 2577 | case AliasAnalysis::MayAlias: |
| 2578 | case AliasAnalysis::PartialAlias: |
| 2579 | goto clobbered; |
| 2580 | case AliasAnalysis::NoAlias: |
| 2581 | break; |
| 2582 | } |
| 2583 | break; |
| 2584 | } |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 2585 | case ARCInstKind::StoreWeak: |
| 2586 | case ARCInstKind::InitWeak: { |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2587 | // If this is storing to the same pointer and has the same size etc. |
| 2588 | // replace this load's value with the stored value. |
| 2589 | CallInst *Call = cast<CallInst>(Inst); |
| 2590 | CallInst *EarlierCall = cast<CallInst>(EarlierInst); |
| 2591 | Value *Arg = Call->getArgOperand(0); |
| 2592 | Value *EarlierArg = EarlierCall->getArgOperand(0); |
| 2593 | switch (PA.getAA()->alias(Arg, EarlierArg)) { |
| 2594 | case AliasAnalysis::MustAlias: |
| 2595 | Changed = true; |
| 2596 | // 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] | 2597 | if (Class == ARCInstKind::LoadWeakRetained) { |
Michael Gottesman | 14acfac | 2013-07-06 01:39:23 +0000 | [diff] [blame] | 2598 | Constant *Decl = EP.get(ARCRuntimeEntryPoints::EPT_Retain); |
| 2599 | CallInst *CI = CallInst::Create(Decl, EarlierCall, "", Call); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2600 | CI->setTailCall(); |
| 2601 | } |
| 2602 | // Zap the fully redundant load. |
| 2603 | Call->replaceAllUsesWith(EarlierCall->getArgOperand(1)); |
| 2604 | Call->eraseFromParent(); |
| 2605 | goto clobbered; |
| 2606 | case AliasAnalysis::MayAlias: |
| 2607 | case AliasAnalysis::PartialAlias: |
| 2608 | goto clobbered; |
| 2609 | case AliasAnalysis::NoAlias: |
| 2610 | break; |
| 2611 | } |
| 2612 | break; |
| 2613 | } |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 2614 | case ARCInstKind::MoveWeak: |
| 2615 | case ARCInstKind::CopyWeak: |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2616 | // TOOD: Grab the copied value. |
| 2617 | goto clobbered; |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 2618 | case ARCInstKind::AutoreleasepoolPush: |
| 2619 | case ARCInstKind::None: |
| 2620 | case ARCInstKind::IntrinsicUser: |
| 2621 | case ARCInstKind::User: |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2622 | // Weak pointers are only modified through the weak entry points |
| 2623 | // (and arbitrary calls, which could call the weak entry points). |
| 2624 | break; |
| 2625 | default: |
| 2626 | // Anything else could modify the weak pointer. |
| 2627 | goto clobbered; |
| 2628 | } |
| 2629 | } |
| 2630 | clobbered:; |
| 2631 | } |
| 2632 | |
| 2633 | // Then, for each destroyWeak with an alloca operand, check to see if |
| 2634 | // the alloca and all its users can be zapped. |
| 2635 | for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) { |
| 2636 | Instruction *Inst = &*I++; |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 2637 | ARCInstKind Class = GetBasicARCInstKind(Inst); |
| 2638 | if (Class != ARCInstKind::DestroyWeak) |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2639 | continue; |
| 2640 | |
| 2641 | CallInst *Call = cast<CallInst>(Inst); |
| 2642 | Value *Arg = Call->getArgOperand(0); |
| 2643 | if (AllocaInst *Alloca = dyn_cast<AllocaInst>(Arg)) { |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 2644 | for (User *U : Alloca->users()) { |
| 2645 | const Instruction *UserInst = cast<Instruction>(U); |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 2646 | switch (GetBasicARCInstKind(UserInst)) { |
| 2647 | case ARCInstKind::InitWeak: |
| 2648 | case ARCInstKind::StoreWeak: |
| 2649 | case ARCInstKind::DestroyWeak: |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2650 | continue; |
| 2651 | default: |
| 2652 | goto done; |
| 2653 | } |
| 2654 | } |
| 2655 | Changed = true; |
Chandler Carruth | cdf4788 | 2014-03-09 03:16:01 +0000 | [diff] [blame] | 2656 | for (auto UI = Alloca->user_begin(), UE = Alloca->user_end(); UI != UE;) { |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2657 | CallInst *UserInst = cast<CallInst>(*UI++); |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 2658 | switch (GetBasicARCInstKind(UserInst)) { |
| 2659 | case ARCInstKind::InitWeak: |
| 2660 | case ARCInstKind::StoreWeak: |
Dan Gohman | 14862c3 | 2012-05-18 22:17:29 +0000 | [diff] [blame] | 2661 | // These functions return their second argument. |
| 2662 | UserInst->replaceAllUsesWith(UserInst->getArgOperand(1)); |
| 2663 | break; |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 2664 | case ARCInstKind::DestroyWeak: |
Dan Gohman | 14862c3 | 2012-05-18 22:17:29 +0000 | [diff] [blame] | 2665 | // No return value. |
| 2666 | break; |
| 2667 | default: |
Dan Gohman | 9c97eea0 | 2012-05-21 17:41:28 +0000 | [diff] [blame] | 2668 | llvm_unreachable("alloca really is used!"); |
Dan Gohman | 14862c3 | 2012-05-18 22:17:29 +0000 | [diff] [blame] | 2669 | } |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2670 | UserInst->eraseFromParent(); |
| 2671 | } |
| 2672 | Alloca->eraseFromParent(); |
| 2673 | done:; |
| 2674 | } |
| 2675 | } |
| 2676 | } |
| 2677 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 2678 | /// Identify program paths which execute sequences of retains and releases which |
| 2679 | /// can be eliminated. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2680 | bool ObjCARCOpt::OptimizeSequences(Function &F) { |
Michael Gottesman | 740db97 | 2013-05-23 02:35:21 +0000 | [diff] [blame] | 2681 | // Releases, Retains - These are used to store the results of the main flow |
| 2682 | // analysis. These use Value* as the key instead of Instruction* so that the |
| 2683 | // map stays valid when we get around to rewriting code and calls get |
| 2684 | // replaced by arguments. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2685 | DenseMap<Value *, RRInfo> Releases; |
Michael Gottesman | 0be6920 | 2015-03-05 23:28:58 +0000 | [diff] [blame^] | 2686 | BlotMapVector<Value *, RRInfo> Retains; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2687 | |
Michael Gottesman | 740db97 | 2013-05-23 02:35:21 +0000 | [diff] [blame] | 2688 | // This is used during the traversal of the function to track the |
| 2689 | // states for each identified object at each block. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2690 | DenseMap<const BasicBlock *, BBState> BBStates; |
| 2691 | |
| 2692 | // Analyze the CFG of the function, and all instructions. |
| 2693 | bool NestingDetected = Visit(F, BBStates, Retains, Releases); |
| 2694 | |
| 2695 | // Transform. |
Michael Gottesman | 5a91bbf | 2013-05-24 20:44:02 +0000 | [diff] [blame] | 2696 | bool AnyPairsCompletelyEliminated = PerformCodePlacement(BBStates, Retains, |
| 2697 | Releases, |
| 2698 | F.getParent()); |
| 2699 | |
| 2700 | // Cleanup. |
| 2701 | MultiOwnersSet.clear(); |
| 2702 | |
| 2703 | return AnyPairsCompletelyEliminated && NestingDetected; |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2704 | } |
| 2705 | |
Michael Gottesman | c2d5bf5 | 2013-04-03 23:07:45 +0000 | [diff] [blame] | 2706 | /// Check if there is a dependent call earlier that does not have anything in |
| 2707 | /// between the Retain and the call that can affect the reference count of their |
| 2708 | /// shared pointer argument. Note that Retain need not be in BB. |
Michael Gottesman | 54dc7fd | 2013-04-03 23:04:28 +0000 | [diff] [blame] | 2709 | static bool |
| 2710 | HasSafePathToPredecessorCall(const Value *Arg, Instruction *Retain, |
Craig Topper | 71b7b68 | 2014-08-21 05:55:13 +0000 | [diff] [blame] | 2711 | SmallPtrSetImpl<Instruction *> &DepInsts, |
| 2712 | SmallPtrSetImpl<const BasicBlock *> &Visited, |
Michael Gottesman | 54dc7fd | 2013-04-03 23:04:28 +0000 | [diff] [blame] | 2713 | ProvenanceAnalysis &PA) { |
| 2714 | FindDependencies(CanChangeRetainCount, Arg, Retain->getParent(), Retain, |
| 2715 | DepInsts, Visited, PA); |
| 2716 | if (DepInsts.size() != 1) |
| 2717 | return false; |
Michael Gottesman | c2d5bf5 | 2013-04-03 23:07:45 +0000 | [diff] [blame] | 2718 | |
Michael Gottesman | 54dc7fd | 2013-04-03 23:04:28 +0000 | [diff] [blame] | 2719 | CallInst *Call = |
| 2720 | dyn_cast_or_null<CallInst>(*DepInsts.begin()); |
Michael Gottesman | c2d5bf5 | 2013-04-03 23:07:45 +0000 | [diff] [blame] | 2721 | |
Michael Gottesman | 54dc7fd | 2013-04-03 23:04:28 +0000 | [diff] [blame] | 2722 | // Check that the pointer is the return value of the call. |
| 2723 | if (!Call || Arg != Call) |
| 2724 | return false; |
Michael Gottesman | c2d5bf5 | 2013-04-03 23:07:45 +0000 | [diff] [blame] | 2725 | |
Michael Gottesman | 54dc7fd | 2013-04-03 23:04:28 +0000 | [diff] [blame] | 2726 | // Check that the call is a regular call. |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 2727 | ARCInstKind Class = GetBasicARCInstKind(Call); |
| 2728 | if (Class != ARCInstKind::CallOrUser && Class != ARCInstKind::Call) |
Michael Gottesman | 54dc7fd | 2013-04-03 23:04:28 +0000 | [diff] [blame] | 2729 | return false; |
Michael Gottesman | c2d5bf5 | 2013-04-03 23:07:45 +0000 | [diff] [blame] | 2730 | |
Michael Gottesman | 54dc7fd | 2013-04-03 23:04:28 +0000 | [diff] [blame] | 2731 | return true; |
| 2732 | } |
| 2733 | |
Michael Gottesman | 6908db1 | 2013-04-03 23:16:05 +0000 | [diff] [blame] | 2734 | /// Find a dependent retain that precedes the given autorelease for which there |
| 2735 | /// is nothing in between the two instructions that can affect the ref count of |
| 2736 | /// Arg. |
| 2737 | static CallInst * |
| 2738 | FindPredecessorRetainWithSafePath(const Value *Arg, BasicBlock *BB, |
| 2739 | Instruction *Autorelease, |
Craig Topper | 71b7b68 | 2014-08-21 05:55:13 +0000 | [diff] [blame] | 2740 | SmallPtrSetImpl<Instruction *> &DepInsts, |
| 2741 | SmallPtrSetImpl<const BasicBlock *> &Visited, |
Michael Gottesman | 6908db1 | 2013-04-03 23:16:05 +0000 | [diff] [blame] | 2742 | ProvenanceAnalysis &PA) { |
| 2743 | FindDependencies(CanChangeRetainCount, Arg, |
| 2744 | BB, Autorelease, DepInsts, Visited, PA); |
| 2745 | if (DepInsts.size() != 1) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2746 | return nullptr; |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 2747 | |
Michael Gottesman | 6908db1 | 2013-04-03 23:16:05 +0000 | [diff] [blame] | 2748 | CallInst *Retain = |
| 2749 | dyn_cast_or_null<CallInst>(*DepInsts.begin()); |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 2750 | |
Michael Gottesman | 6908db1 | 2013-04-03 23:16:05 +0000 | [diff] [blame] | 2751 | // Check that we found a retain with the same argument. |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 2752 | if (!Retain || !IsRetain(GetBasicARCInstKind(Retain)) || |
Michael Gottesman | e5ad66f | 2015-02-19 00:42:38 +0000 | [diff] [blame] | 2753 | GetArgRCIdentityRoot(Retain) != Arg) { |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2754 | return nullptr; |
Michael Gottesman | 6908db1 | 2013-04-03 23:16:05 +0000 | [diff] [blame] | 2755 | } |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 2756 | |
Michael Gottesman | 6908db1 | 2013-04-03 23:16:05 +0000 | [diff] [blame] | 2757 | return Retain; |
| 2758 | } |
| 2759 | |
Michael Gottesman | 21a4ed3 | 2013-04-03 23:39:14 +0000 | [diff] [blame] | 2760 | /// Look for an ``autorelease'' instruction dependent on Arg such that there are |
| 2761 | /// no instructions dependent on Arg that need a positive ref count in between |
| 2762 | /// the autorelease and the ret. |
| 2763 | static CallInst * |
| 2764 | FindPredecessorAutoreleaseWithSafePath(const Value *Arg, BasicBlock *BB, |
| 2765 | ReturnInst *Ret, |
Craig Topper | 71b7b68 | 2014-08-21 05:55:13 +0000 | [diff] [blame] | 2766 | SmallPtrSetImpl<Instruction *> &DepInsts, |
| 2767 | SmallPtrSetImpl<const BasicBlock *> &V, |
Michael Gottesman | 21a4ed3 | 2013-04-03 23:39:14 +0000 | [diff] [blame] | 2768 | ProvenanceAnalysis &PA) { |
| 2769 | FindDependencies(NeedsPositiveRetainCount, Arg, |
| 2770 | BB, Ret, DepInsts, V, PA); |
| 2771 | if (DepInsts.size() != 1) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2772 | return nullptr; |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 2773 | |
Michael Gottesman | 21a4ed3 | 2013-04-03 23:39:14 +0000 | [diff] [blame] | 2774 | CallInst *Autorelease = |
| 2775 | dyn_cast_or_null<CallInst>(*DepInsts.begin()); |
| 2776 | if (!Autorelease) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2777 | return nullptr; |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 2778 | ARCInstKind AutoreleaseClass = GetBasicARCInstKind(Autorelease); |
Michael Gottesman | 21a4ed3 | 2013-04-03 23:39:14 +0000 | [diff] [blame] | 2779 | if (!IsAutorelease(AutoreleaseClass)) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2780 | return nullptr; |
Michael Gottesman | e5ad66f | 2015-02-19 00:42:38 +0000 | [diff] [blame] | 2781 | if (GetArgRCIdentityRoot(Autorelease) != Arg) |
Craig Topper | f40110f | 2014-04-25 05:29:35 +0000 | [diff] [blame] | 2782 | return nullptr; |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 2783 | |
Michael Gottesman | 21a4ed3 | 2013-04-03 23:39:14 +0000 | [diff] [blame] | 2784 | return Autorelease; |
| 2785 | } |
| 2786 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 2787 | /// Look for this pattern: |
Dmitri Gribenko | 5485acd | 2012-09-14 14:57:36 +0000 | [diff] [blame] | 2788 | /// \code |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2789 | /// %call = call i8* @something(...) |
| 2790 | /// %2 = call i8* @objc_retain(i8* %call) |
| 2791 | /// %3 = call i8* @objc_autorelease(i8* %2) |
| 2792 | /// ret i8* %3 |
Dmitri Gribenko | 5485acd | 2012-09-14 14:57:36 +0000 | [diff] [blame] | 2793 | /// \endcode |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2794 | /// And delete the retain and autorelease. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2795 | void ObjCARCOpt::OptimizeReturns(Function &F) { |
| 2796 | if (!F.getReturnType()->isPointerTy()) |
| 2797 | return; |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 2798 | |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 2799 | DEBUG(dbgs() << "\n== ObjCARCOpt::OptimizeReturns ==\n"); |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 2800 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2801 | SmallPtrSet<Instruction *, 4> DependingInstructions; |
| 2802 | SmallPtrSet<const BasicBlock *, 4> Visited; |
| 2803 | for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI) { |
| 2804 | BasicBlock *BB = FI; |
| 2805 | ReturnInst *Ret = dyn_cast<ReturnInst>(&BB->back()); |
Michael Gottesman | 3f146e2 | 2013-01-01 16:05:48 +0000 | [diff] [blame] | 2806 | |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 2807 | DEBUG(dbgs() << "Visiting: " << *Ret << "\n"); |
Michael Gottesman | 3f146e2 | 2013-01-01 16:05:48 +0000 | [diff] [blame] | 2808 | |
Michael Gottesman | 21a4ed3 | 2013-04-03 23:39:14 +0000 | [diff] [blame] | 2809 | if (!Ret) |
| 2810 | continue; |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 2811 | |
Michael Gottesman | e5ad66f | 2015-02-19 00:42:38 +0000 | [diff] [blame] | 2812 | const Value *Arg = GetRCIdentityRoot(Ret->getOperand(0)); |
Michael Gottesman | 7924997 | 2013-04-05 23:46:45 +0000 | [diff] [blame] | 2813 | |
Michael Gottesman | cdb7c15 | 2013-04-21 00:25:04 +0000 | [diff] [blame] | 2814 | // Look for an ``autorelease'' instruction that is a predecessor of Ret and |
Michael Gottesman | 21a4ed3 | 2013-04-03 23:39:14 +0000 | [diff] [blame] | 2815 | // dependent on Arg such that there are no instructions dependent on Arg |
| 2816 | // that need a positive ref count in between the autorelease and Ret. |
| 2817 | CallInst *Autorelease = |
| 2818 | FindPredecessorAutoreleaseWithSafePath(Arg, BB, Ret, |
| 2819 | DependingInstructions, Visited, |
| 2820 | PA); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2821 | DependingInstructions.clear(); |
| 2822 | Visited.clear(); |
Michael Gottesman | fb9ece9 | 2013-04-21 00:25:01 +0000 | [diff] [blame] | 2823 | |
| 2824 | if (!Autorelease) |
| 2825 | continue; |
| 2826 | |
| 2827 | CallInst *Retain = |
| 2828 | FindPredecessorRetainWithSafePath(Arg, BB, Autorelease, |
| 2829 | DependingInstructions, Visited, PA); |
| 2830 | DependingInstructions.clear(); |
| 2831 | Visited.clear(); |
| 2832 | |
| 2833 | if (!Retain) |
| 2834 | continue; |
| 2835 | |
| 2836 | // Check that there is nothing that can affect the reference count |
| 2837 | // between the retain and the call. Note that Retain need not be in BB. |
| 2838 | bool HasSafePathToCall = HasSafePathToPredecessorCall(Arg, Retain, |
| 2839 | DependingInstructions, |
| 2840 | Visited, PA); |
| 2841 | DependingInstructions.clear(); |
| 2842 | Visited.clear(); |
| 2843 | |
| 2844 | if (!HasSafePathToCall) |
| 2845 | continue; |
| 2846 | |
| 2847 | // If so, we can zap the retain and autorelease. |
| 2848 | Changed = true; |
| 2849 | ++NumRets; |
| 2850 | DEBUG(dbgs() << "Erasing: " << *Retain << "\nErasing: " |
| 2851 | << *Autorelease << "\n"); |
| 2852 | EraseInstruction(Retain); |
| 2853 | EraseInstruction(Autorelease); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2854 | } |
| 2855 | } |
| 2856 | |
Michael Gottesman | 9c11815 | 2013-04-29 06:16:57 +0000 | [diff] [blame] | 2857 | #ifndef NDEBUG |
| 2858 | void |
| 2859 | ObjCARCOpt::GatherStatistics(Function &F, bool AfterOptimization) { |
| 2860 | llvm::Statistic &NumRetains = |
| 2861 | AfterOptimization? NumRetainsAfterOpt : NumRetainsBeforeOpt; |
| 2862 | llvm::Statistic &NumReleases = |
| 2863 | AfterOptimization? NumReleasesAfterOpt : NumReleasesBeforeOpt; |
| 2864 | |
| 2865 | for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) { |
| 2866 | Instruction *Inst = &*I++; |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 2867 | switch (GetBasicARCInstKind(Inst)) { |
Michael Gottesman | 9c11815 | 2013-04-29 06:16:57 +0000 | [diff] [blame] | 2868 | default: |
| 2869 | break; |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 2870 | case ARCInstKind::Retain: |
Michael Gottesman | 9c11815 | 2013-04-29 06:16:57 +0000 | [diff] [blame] | 2871 | ++NumRetains; |
| 2872 | break; |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 2873 | case ARCInstKind::Release: |
Michael Gottesman | 9c11815 | 2013-04-29 06:16:57 +0000 | [diff] [blame] | 2874 | ++NumReleases; |
| 2875 | break; |
| 2876 | } |
| 2877 | } |
| 2878 | } |
| 2879 | #endif |
| 2880 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2881 | bool ObjCARCOpt::doInitialization(Module &M) { |
| 2882 | if (!EnableARCOpts) |
| 2883 | return false; |
| 2884 | |
Dan Gohman | 670f937 | 2012-04-13 18:57:48 +0000 | [diff] [blame] | 2885 | // If nothing in the Module uses ARC, don't do anything. |
Dan Gohman | ceaac7c | 2011-06-20 23:20:43 +0000 | [diff] [blame] | 2886 | Run = ModuleHasARC(M); |
| 2887 | if (!Run) |
| 2888 | return false; |
| 2889 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2890 | // Identify the imprecise release metadata kind. |
| 2891 | ImpreciseReleaseMDKind = |
| 2892 | M.getContext().getMDKindID("clang.imprecise_release"); |
Dan Gohman | a7107f9 | 2011-10-17 22:53:25 +0000 | [diff] [blame] | 2893 | CopyOnEscapeMDKind = |
| 2894 | M.getContext().getMDKindID("clang.arc.copy_on_escape"); |
Dan Gohman | 0155f30 | 2012-02-17 18:59:53 +0000 | [diff] [blame] | 2895 | NoObjCARCExceptionsMDKind = |
| 2896 | M.getContext().getMDKindID("clang.arc.no_objc_arc_exceptions"); |
Michael Gottesman | 81b1d43 | 2013-03-26 00:42:04 +0000 | [diff] [blame] | 2897 | #ifdef ARC_ANNOTATIONS |
| 2898 | ARCAnnotationBottomUpMDKind = |
| 2899 | M.getContext().getMDKindID("llvm.arc.annotation.bottomup"); |
| 2900 | ARCAnnotationTopDownMDKind = |
| 2901 | M.getContext().getMDKindID("llvm.arc.annotation.topdown"); |
| 2902 | ARCAnnotationProvenanceSourceMDKind = |
| 2903 | M.getContext().getMDKindID("llvm.arc.annotation.provenancesource"); |
| 2904 | #endif // ARC_ANNOTATIONS |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2905 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2906 | // Intuitively, objc_retain and others are nocapture, however in practice |
| 2907 | // they are not, because they return their argument value. And objc_release |
Dan Gohman | dae3349 | 2012-04-27 18:56:31 +0000 | [diff] [blame] | 2908 | // calls finalizers which can have arbitrary side effects. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2909 | |
Michael Gottesman | 14acfac | 2013-07-06 01:39:23 +0000 | [diff] [blame] | 2910 | // Initialize our runtime entry point cache. |
| 2911 | EP.Initialize(&M); |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2912 | |
| 2913 | return false; |
| 2914 | } |
| 2915 | |
| 2916 | bool ObjCARCOpt::runOnFunction(Function &F) { |
| 2917 | if (!EnableARCOpts) |
| 2918 | return false; |
| 2919 | |
Dan Gohman | ceaac7c | 2011-06-20 23:20:43 +0000 | [diff] [blame] | 2920 | // If nothing in the Module uses ARC, don't do anything. |
| 2921 | if (!Run) |
| 2922 | return false; |
| 2923 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2924 | Changed = false; |
| 2925 | |
Michael Gottesman | 89279f8 | 2013-04-05 18:10:41 +0000 | [diff] [blame] | 2926 | DEBUG(dbgs() << "<<< ObjCARCOpt: Visiting Function: " << F.getName() << " >>>" |
| 2927 | "\n"); |
Michael Gottesman | b24bdef | 2013-01-12 02:57:16 +0000 | [diff] [blame] | 2928 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2929 | PA.setAA(&getAnalysis<AliasAnalysis>()); |
| 2930 | |
Michael Gottesman | 9fc50b8 | 2013-05-13 18:29:07 +0000 | [diff] [blame] | 2931 | #ifndef NDEBUG |
| 2932 | if (AreStatisticsEnabled()) { |
| 2933 | GatherStatistics(F, false); |
| 2934 | } |
| 2935 | #endif |
| 2936 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2937 | // This pass performs several distinct transformations. As a compile-time aid |
| 2938 | // when compiling code that isn't ObjC, skip these if the relevant ObjC |
| 2939 | // library functions aren't declared. |
| 2940 | |
Michael Gottesman | cd5b027 | 2013-04-24 22:18:15 +0000 | [diff] [blame] | 2941 | // Preliminary optimizations. This also computes UsedInThisFunction. |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2942 | OptimizeIndividualCalls(F); |
| 2943 | |
| 2944 | // Optimizations for weak pointers. |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 2945 | if (UsedInThisFunction & ((1 << unsigned(ARCInstKind::LoadWeak)) | |
| 2946 | (1 << unsigned(ARCInstKind::LoadWeakRetained)) | |
| 2947 | (1 << unsigned(ARCInstKind::StoreWeak)) | |
| 2948 | (1 << unsigned(ARCInstKind::InitWeak)) | |
| 2949 | (1 << unsigned(ARCInstKind::CopyWeak)) | |
| 2950 | (1 << unsigned(ARCInstKind::MoveWeak)) | |
| 2951 | (1 << unsigned(ARCInstKind::DestroyWeak)))) |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2952 | OptimizeWeakCalls(F); |
| 2953 | |
| 2954 | // Optimizations for retain+release pairs. |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 2955 | if (UsedInThisFunction & ((1 << unsigned(ARCInstKind::Retain)) | |
| 2956 | (1 << unsigned(ARCInstKind::RetainRV)) | |
| 2957 | (1 << unsigned(ARCInstKind::RetainBlock)))) |
| 2958 | if (UsedInThisFunction & (1 << unsigned(ARCInstKind::Release))) |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2959 | // Run OptimizeSequences until it either stops making changes or |
| 2960 | // no retain+release pair nesting is detected. |
| 2961 | while (OptimizeSequences(F)) {} |
| 2962 | |
| 2963 | // Optimizations if objc_autorelease is used. |
Michael Gottesman | 6f729fa | 2015-02-19 19:51:32 +0000 | [diff] [blame] | 2964 | if (UsedInThisFunction & ((1 << unsigned(ARCInstKind::Autorelease)) | |
| 2965 | (1 << unsigned(ARCInstKind::AutoreleaseRV)))) |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2966 | OptimizeReturns(F); |
| 2967 | |
Michael Gottesman | 9c11815 | 2013-04-29 06:16:57 +0000 | [diff] [blame] | 2968 | // Gather statistics after optimization. |
| 2969 | #ifndef NDEBUG |
| 2970 | if (AreStatisticsEnabled()) { |
| 2971 | GatherStatistics(F, true); |
| 2972 | } |
| 2973 | #endif |
| 2974 | |
Michael Gottesman | b24bdef | 2013-01-12 02:57:16 +0000 | [diff] [blame] | 2975 | DEBUG(dbgs() << "\n"); |
| 2976 | |
John McCall | d935e9c | 2011-06-15 23:37:01 +0000 | [diff] [blame] | 2977 | return Changed; |
| 2978 | } |
| 2979 | |
| 2980 | void ObjCARCOpt::releaseMemory() { |
| 2981 | PA.clear(); |
| 2982 | } |
| 2983 | |
Michael Gottesman | 97e3df0 | 2013-01-14 00:35:14 +0000 | [diff] [blame] | 2984 | /// @} |
| 2985 | /// |