blob: 76932e6b600bcee57d06834b0c12042c41b779ed [file] [log] [blame]
Michael Gottesman79d8d812013-01-28 01:35:51 +00001//===- ObjCARCOpts.cpp - ObjC ARC Optimization ----------------------------===//
John McCalld935e9c2011-06-15 23:37:01 +00002//
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 Gottesman97e3df02013-01-14 00:35:14 +00009/// \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 Gottesman697d8b92013-02-07 04:12:57 +000016/// redundant weak pointer operations, and numerous minor simplifications.
Michael Gottesman97e3df02013-01-14 00:35:14 +000017///
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 McCalld935e9c2011-06-15 23:37:01 +000025//===----------------------------------------------------------------------===//
26
Michael Gottesman08904e32013-01-28 03:28:38 +000027#include "ObjCARC.h"
Michael Gottesman14acfac2013-07-06 01:39:23 +000028#include "ARCRuntimeEntryPoints.h"
Michael Gottesman278266f2013-01-29 04:20:52 +000029#include "DependencyAnalysis.h"
Michael Gottesman294e7da2013-01-28 05:51:54 +000030#include "ObjCARCAliasAnalysis.h"
Michael Gottesman778138e2013-01-29 03:03:03 +000031#include "ProvenanceAnalysis.h"
John McCalld935e9c2011-06-15 23:37:01 +000032#include "llvm/ADT/DenseMap.h"
Michael Gottesman5a91bbf2013-05-24 20:44:02 +000033#include "llvm/ADT/DenseSet.h"
Michael Gottesmanfa0939f2013-01-28 04:12:07 +000034#include "llvm/ADT/STLExtras.h"
Michael Gottesman778138e2013-01-29 03:03:03 +000035#include "llvm/ADT/SmallPtrSet.h"
Michael Gottesman278266f2013-01-29 04:20:52 +000036#include "llvm/ADT/Statistic.h"
Chandler Carruth1305dc32014-03-04 11:45:46 +000037#include "llvm/IR/CFG.h"
Michael Gottesmancd4de0f2013-03-26 00:42:09 +000038#include "llvm/IR/IRBuilder.h"
Michael Gottesman278266f2013-01-29 04:20:52 +000039#include "llvm/IR/LLVMContext.h"
Michael Gottesman13a5f1a2013-01-29 04:51:59 +000040#include "llvm/Support/Debug.h"
Timur Iskhodzhanov5d7ff002013-01-29 09:09:27 +000041#include "llvm/Support/raw_ostream.h"
Michael Gottesmanfa0939f2013-01-28 04:12:07 +000042
John McCalld935e9c2011-06-15 23:37:01 +000043using namespace llvm;
Michael Gottesman08904e32013-01-28 03:28:38 +000044using namespace llvm::objcarc;
John McCalld935e9c2011-06-15 23:37:01 +000045
Chandler Carruth964daaa2014-04-22 02:55:47 +000046#define DEBUG_TYPE "objc-arc-opts"
47
Michael Gottesman97e3df02013-01-14 00:35:14 +000048/// \defgroup MiscUtils Miscellaneous utilities that are not ARC specific.
49/// @{
John McCalld935e9c2011-06-15 23:37:01 +000050
51namespace {
Michael Gottesman97e3df02013-01-14 00:35:14 +000052 /// \brief An associative container with fast insertion-order (deterministic)
53 /// iteration over its elements. Plus the special blot operation.
John McCalld935e9c2011-06-15 23:37:01 +000054 template<class KeyT, class ValueT>
55 class MapVector {
Michael Gottesman97e3df02013-01-14 00:35:14 +000056 /// Map keys to indices in Vector.
John McCalld935e9c2011-06-15 23:37:01 +000057 typedef DenseMap<KeyT, size_t> MapTy;
58 MapTy Map;
59
John McCalld935e9c2011-06-15 23:37:01 +000060 typedef std::vector<std::pair<KeyT, ValueT> > VectorTy;
Michael Gottesman97e3df02013-01-14 00:35:14 +000061 /// Keys and values.
John McCalld935e9c2011-06-15 23:37:01 +000062 VectorTy Vector;
63
64 public:
65 typedef typename VectorTy::iterator iterator;
66 typedef typename VectorTy::const_iterator const_iterator;
67 iterator begin() { return Vector.begin(); }
68 iterator end() { return Vector.end(); }
69 const_iterator begin() const { return Vector.begin(); }
70 const_iterator end() const { return Vector.end(); }
71
72#ifdef XDEBUG
73 ~MapVector() {
74 assert(Vector.size() >= Map.size()); // May differ due to blotting.
75 for (typename MapTy::const_iterator I = Map.begin(), E = Map.end();
76 I != E; ++I) {
77 assert(I->second < Vector.size());
78 assert(Vector[I->second].first == I->first);
79 }
80 for (typename VectorTy::const_iterator I = Vector.begin(),
81 E = Vector.end(); I != E; ++I)
82 assert(!I->first ||
83 (Map.count(I->first) &&
84 Map[I->first] == size_t(I - Vector.begin())));
85 }
86#endif
87
Dan Gohman55b06742012-03-02 01:13:53 +000088 ValueT &operator[](const KeyT &Arg) {
John McCalld935e9c2011-06-15 23:37:01 +000089 std::pair<typename MapTy::iterator, bool> Pair =
90 Map.insert(std::make_pair(Arg, size_t(0)));
91 if (Pair.second) {
Dan Gohman55b06742012-03-02 01:13:53 +000092 size_t Num = Vector.size();
93 Pair.first->second = Num;
John McCalld935e9c2011-06-15 23:37:01 +000094 Vector.push_back(std::make_pair(Arg, ValueT()));
Dan Gohman55b06742012-03-02 01:13:53 +000095 return Vector[Num].second;
John McCalld935e9c2011-06-15 23:37:01 +000096 }
97 return Vector[Pair.first->second].second;
98 }
99
100 std::pair<iterator, bool>
101 insert(const std::pair<KeyT, ValueT> &InsertPair) {
102 std::pair<typename MapTy::iterator, bool> Pair =
103 Map.insert(std::make_pair(InsertPair.first, size_t(0)));
104 if (Pair.second) {
Dan Gohman55b06742012-03-02 01:13:53 +0000105 size_t Num = Vector.size();
106 Pair.first->second = Num;
John McCalld935e9c2011-06-15 23:37:01 +0000107 Vector.push_back(InsertPair);
Dan Gohman55b06742012-03-02 01:13:53 +0000108 return std::make_pair(Vector.begin() + Num, true);
John McCalld935e9c2011-06-15 23:37:01 +0000109 }
110 return std::make_pair(Vector.begin() + Pair.first->second, false);
111 }
112
Michael Gottesmana76143ee2013-05-13 23:49:42 +0000113 iterator find(const KeyT &Key) {
114 typename MapTy::iterator It = Map.find(Key);
115 if (It == Map.end()) return Vector.end();
116 return Vector.begin() + It->second;
117 }
118
Dan Gohman55b06742012-03-02 01:13:53 +0000119 const_iterator find(const KeyT &Key) const {
John McCalld935e9c2011-06-15 23:37:01 +0000120 typename MapTy::const_iterator It = Map.find(Key);
121 if (It == Map.end()) return Vector.end();
122 return Vector.begin() + It->second;
123 }
124
Michael Gottesman97e3df02013-01-14 00:35:14 +0000125 /// This is similar to erase, but instead of removing the element from the
126 /// vector, it just zeros out the key in the vector. This leaves iterators
127 /// intact, but clients must be prepared for zeroed-out keys when iterating.
Dan Gohman55b06742012-03-02 01:13:53 +0000128 void blot(const KeyT &Key) {
John McCalld935e9c2011-06-15 23:37:01 +0000129 typename MapTy::iterator It = Map.find(Key);
130 if (It == Map.end()) return;
131 Vector[It->second].first = KeyT();
132 Map.erase(It);
133 }
134
135 void clear() {
136 Map.clear();
137 Vector.clear();
138 }
139 };
140}
141
Michael Gottesman97e3df02013-01-14 00:35:14 +0000142/// @}
143///
144/// \defgroup ARCUtilities Utility declarations/definitions specific to ARC.
145/// @{
John McCalld935e9c2011-06-15 23:37:01 +0000146
Michael Gottesman97e3df02013-01-14 00:35:14 +0000147/// \brief This is similar to StripPointerCastsAndObjCCalls but it stops as soon
148/// as it finds a value with multiple uses.
John McCalld935e9c2011-06-15 23:37:01 +0000149static const Value *FindSingleUseIdentifiedObject(const Value *Arg) {
150 if (Arg->hasOneUse()) {
151 if (const BitCastInst *BC = dyn_cast<BitCastInst>(Arg))
152 return FindSingleUseIdentifiedObject(BC->getOperand(0));
153 if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Arg))
154 if (GEP->hasAllZeroIndices())
155 return FindSingleUseIdentifiedObject(GEP->getPointerOperand());
156 if (IsForwarding(GetBasicInstructionClass(Arg)))
157 return FindSingleUseIdentifiedObject(
158 cast<CallInst>(Arg)->getArgOperand(0));
159 if (!IsObjCIdentifiedObject(Arg))
Craig Topperf40110f2014-04-25 05:29:35 +0000160 return nullptr;
John McCalld935e9c2011-06-15 23:37:01 +0000161 return Arg;
162 }
163
Dan Gohman41375a32012-05-08 23:39:44 +0000164 // If we found an identifiable object but it has multiple uses, but they are
165 // trivial uses, we can still consider this to be a single-use value.
John McCalld935e9c2011-06-15 23:37:01 +0000166 if (IsObjCIdentifiedObject(Arg)) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000167 for (const User *U : Arg->users())
John McCalld935e9c2011-06-15 23:37:01 +0000168 if (!U->use_empty() || StripPointerCastsAndObjCCalls(U) != Arg)
Craig Topperf40110f2014-04-25 05:29:35 +0000169 return nullptr;
John McCalld935e9c2011-06-15 23:37:01 +0000170
171 return Arg;
172 }
173
Craig Topperf40110f2014-04-25 05:29:35 +0000174 return nullptr;
John McCalld935e9c2011-06-15 23:37:01 +0000175}
176
Michael Gottesmana76143ee2013-05-13 23:49:42 +0000177/// This is a wrapper around getUnderlyingObjCPtr along the lines of
178/// GetUnderlyingObjects except that it returns early when it sees the first
179/// alloca.
180static inline bool AreAnyUnderlyingObjectsAnAlloca(const Value *V) {
181 SmallPtrSet<const Value *, 4> Visited;
182 SmallVector<const Value *, 4> Worklist;
183 Worklist.push_back(V);
184 do {
185 const Value *P = Worklist.pop_back_val();
186 P = GetUnderlyingObjCPtr(P);
Michael Gottesman0c8b5622013-05-14 06:40:10 +0000187
Michael Gottesmana76143ee2013-05-13 23:49:42 +0000188 if (isa<AllocaInst>(P))
189 return true;
Michael Gottesman0c8b5622013-05-14 06:40:10 +0000190
David Blaikie70573dc2014-11-19 07:49:26 +0000191 if (!Visited.insert(P).second)
Michael Gottesmana76143ee2013-05-13 23:49:42 +0000192 continue;
Michael Gottesman0c8b5622013-05-14 06:40:10 +0000193
Michael Gottesmana76143ee2013-05-13 23:49:42 +0000194 if (const SelectInst *SI = dyn_cast<const SelectInst>(P)) {
195 Worklist.push_back(SI->getTrueValue());
196 Worklist.push_back(SI->getFalseValue());
197 continue;
198 }
Michael Gottesman0c8b5622013-05-14 06:40:10 +0000199
Michael Gottesmana76143ee2013-05-13 23:49:42 +0000200 if (const PHINode *PN = dyn_cast<const PHINode>(P)) {
201 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
202 Worklist.push_back(PN->getIncomingValue(i));
203 continue;
204 }
205 } while (!Worklist.empty());
206
207 return false;
208}
209
210
Michael Gottesman97e3df02013-01-14 00:35:14 +0000211/// @}
212///
Michael Gottesman97e3df02013-01-14 00:35:14 +0000213/// \defgroup ARCOpt ARC Optimization.
214/// @{
John McCalld935e9c2011-06-15 23:37:01 +0000215
216// TODO: On code like this:
217//
218// objc_retain(%x)
219// stuff_that_cannot_release()
220// objc_autorelease(%x)
221// stuff_that_cannot_release()
222// objc_retain(%x)
223// stuff_that_cannot_release()
224// objc_autorelease(%x)
225//
226// The second retain and autorelease can be deleted.
227
228// TODO: It should be possible to delete
229// objc_autoreleasePoolPush and objc_autoreleasePoolPop
230// pairs if nothing is actually autoreleased between them. Also, autorelease
231// calls followed by objc_autoreleasePoolPop calls (perhaps in ObjC++ code
232// after inlining) can be turned into plain release calls.
233
234// TODO: Critical-edge splitting. If the optimial insertion point is
235// a critical edge, the current algorithm has to fail, because it doesn't
236// know how to split edges. It should be possible to make the optimizer
237// think in terms of edges, rather than blocks, and then split critical
238// edges on demand.
239
240// TODO: OptimizeSequences could generalized to be Interprocedural.
241
242// TODO: Recognize that a bunch of other objc runtime calls have
243// non-escaping arguments and non-releasing arguments, and may be
244// non-autoreleasing.
245
246// TODO: Sink autorelease calls as far as possible. Unfortunately we
247// usually can't sink them past other calls, which would be the main
248// case where it would be useful.
249
Dan Gohmanb3894012011-08-19 00:26:36 +0000250// TODO: The pointer returned from objc_loadWeakRetained is retained.
251
252// TODO: Delete release+retain pairs (rare).
Dan Gohmanceaac7c2011-06-20 23:20:43 +0000253
John McCalld935e9c2011-06-15 23:37:01 +0000254STATISTIC(NumNoops, "Number of no-op objc calls eliminated");
255STATISTIC(NumPartialNoops, "Number of partially no-op objc calls eliminated");
256STATISTIC(NumAutoreleases,"Number of autoreleases converted to releases");
257STATISTIC(NumRets, "Number of return value forwarding "
Michael Gottesmanb4e7f4d2013-05-15 17:43:03 +0000258 "retain+autoreleases eliminated");
John McCalld935e9c2011-06-15 23:37:01 +0000259STATISTIC(NumRRs, "Number of retain+release paths eliminated");
260STATISTIC(NumPeeps, "Number of calls peephole-optimized");
Matt Beaumont-Gaye55d9492013-05-13 21:10:49 +0000261#ifndef NDEBUG
Michael Gottesman9c118152013-04-29 06:16:57 +0000262STATISTIC(NumRetainsBeforeOpt,
Michael Gottesmanb4e7f4d2013-05-15 17:43:03 +0000263 "Number of retains before optimization");
Michael Gottesman9c118152013-04-29 06:16:57 +0000264STATISTIC(NumReleasesBeforeOpt,
Michael Gottesmanb4e7f4d2013-05-15 17:43:03 +0000265 "Number of releases before optimization");
Michael Gottesman9c118152013-04-29 06:16:57 +0000266STATISTIC(NumRetainsAfterOpt,
Michael Gottesmanb4e7f4d2013-05-15 17:43:03 +0000267 "Number of retains after optimization");
Michael Gottesman9c118152013-04-29 06:16:57 +0000268STATISTIC(NumReleasesAfterOpt,
Michael Gottesmanb4e7f4d2013-05-15 17:43:03 +0000269 "Number of releases after optimization");
Michael Gottesman03cf3c82013-04-29 07:29:08 +0000270#endif
John McCalld935e9c2011-06-15 23:37:01 +0000271
272namespace {
Michael Gottesman97e3df02013-01-14 00:35:14 +0000273 /// \enum Sequence
274 ///
275 /// \brief A sequence of states that a pointer may go through in which an
276 /// objc_retain and objc_release are actually needed.
John McCalld935e9c2011-06-15 23:37:01 +0000277 enum Sequence {
278 S_None,
Michael Gottesman53fd20b2013-01-29 21:07:51 +0000279 S_Retain, ///< objc_retain(x).
280 S_CanRelease, ///< foo(x) -- x could possibly see a ref count decrement.
281 S_Use, ///< any use of x.
Michael Gottesman386241c2013-01-29 21:39:02 +0000282 S_Stop, ///< like S_Release, but code motion is stopped.
Michael Gottesman53fd20b2013-01-29 21:07:51 +0000283 S_Release, ///< objc_release(x).
Michael Gottesman9bdab2b2013-01-29 21:41:44 +0000284 S_MovableRelease ///< objc_release(x), !clang.imprecise_release.
John McCalld935e9c2011-06-15 23:37:01 +0000285 };
Michael Gottesman53fd20b2013-01-29 21:07:51 +0000286
287 raw_ostream &operator<<(raw_ostream &OS, const Sequence S)
288 LLVM_ATTRIBUTE_UNUSED;
289 raw_ostream &operator<<(raw_ostream &OS, const Sequence S) {
290 switch (S) {
291 case S_None:
292 return OS << "S_None";
293 case S_Retain:
294 return OS << "S_Retain";
295 case S_CanRelease:
296 return OS << "S_CanRelease";
297 case S_Use:
298 return OS << "S_Use";
299 case S_Release:
300 return OS << "S_Release";
301 case S_MovableRelease:
302 return OS << "S_MovableRelease";
303 case S_Stop:
304 return OS << "S_Stop";
305 }
306 llvm_unreachable("Unknown sequence type.");
307 }
John McCalld935e9c2011-06-15 23:37:01 +0000308}
309
310static Sequence MergeSeqs(Sequence A, Sequence B, bool TopDown) {
311 // The easy cases.
312 if (A == B)
313 return A;
314 if (A == S_None || B == S_None)
315 return S_None;
316
John McCalld935e9c2011-06-15 23:37:01 +0000317 if (A > B) std::swap(A, B);
318 if (TopDown) {
319 // Choose the side which is further along in the sequence.
Dan Gohman12130272011-08-12 00:26:31 +0000320 if ((A == S_Retain || A == S_CanRelease) &&
321 (B == S_CanRelease || B == S_Use))
John McCalld935e9c2011-06-15 23:37:01 +0000322 return B;
323 } else {
324 // Choose the side which is further along in the sequence.
325 if ((A == S_Use || A == S_CanRelease) &&
Dan Gohman12130272011-08-12 00:26:31 +0000326 (B == S_Use || B == S_Release || B == S_Stop || B == S_MovableRelease))
John McCalld935e9c2011-06-15 23:37:01 +0000327 return A;
328 // If both sides are releases, choose the more conservative one.
329 if (A == S_Stop && (B == S_Release || B == S_MovableRelease))
330 return A;
331 if (A == S_Release && B == S_MovableRelease)
332 return A;
333 }
334
335 return S_None;
336}
337
338namespace {
Michael Gottesman97e3df02013-01-14 00:35:14 +0000339 /// \brief Unidirectional information about either a
John McCalld935e9c2011-06-15 23:37:01 +0000340 /// retain-decrement-use-release sequence or release-use-decrement-retain
Bob Wilson798a7702013-04-09 22:15:51 +0000341 /// reverse sequence.
John McCalld935e9c2011-06-15 23:37:01 +0000342 struct RRInfo {
Michael Gottesman97e3df02013-01-14 00:35:14 +0000343 /// After an objc_retain, the reference count of the referenced
Dan Gohmanb3894012011-08-19 00:26:36 +0000344 /// object is known to be positive. Similarly, before an objc_release, the
345 /// reference count of the referenced object is known to be positive. If
346 /// there are retain-release pairs in code regions where the retain count
347 /// is known to be positive, they can be eliminated, regardless of any side
348 /// effects between them.
349 ///
350 /// Also, a retain+release pair nested within another retain+release
351 /// pair all on the known same pointer value can be eliminated, regardless
352 /// of any intervening side effects.
353 ///
354 /// KnownSafe is true when either of these conditions is satisfied.
355 bool KnownSafe;
John McCalld935e9c2011-06-15 23:37:01 +0000356
Michael Gottesman97e3df02013-01-14 00:35:14 +0000357 /// True of the objc_release calls are all marked with the "tail" keyword.
John McCalld935e9c2011-06-15 23:37:01 +0000358 bool IsTailCallRelease;
359
Michael Gottesman97e3df02013-01-14 00:35:14 +0000360 /// If the Calls are objc_release calls and they all have a
361 /// clang.imprecise_release tag, this is the metadata tag.
John McCalld935e9c2011-06-15 23:37:01 +0000362 MDNode *ReleaseMetadata;
363
Michael Gottesman97e3df02013-01-14 00:35:14 +0000364 /// For a top-down sequence, the set of objc_retains or
John McCalld935e9c2011-06-15 23:37:01 +0000365 /// objc_retainBlocks. For bottom-up, the set of objc_releases.
366 SmallPtrSet<Instruction *, 2> Calls;
367
Michael Gottesman97e3df02013-01-14 00:35:14 +0000368 /// The set of optimal insert positions for moving calls in the opposite
369 /// sequence.
John McCalld935e9c2011-06-15 23:37:01 +0000370 SmallPtrSet<Instruction *, 2> ReverseInsertPts;
371
Michael Gottesmane67f40c2013-05-24 20:44:05 +0000372 /// If this is true, we cannot perform code motion but can still remove
373 /// retain/release pairs.
374 bool CFGHazardAfflicted;
375
John McCalld935e9c2011-06-15 23:37:01 +0000376 RRInfo() :
Craig Topperf40110f2014-04-25 05:29:35 +0000377 KnownSafe(false), IsTailCallRelease(false), ReleaseMetadata(nullptr),
Michael Gottesmane67f40c2013-05-24 20:44:05 +0000378 CFGHazardAfflicted(false) {}
John McCalld935e9c2011-06-15 23:37:01 +0000379
380 void clear();
Michael Gottesman79249972013-04-05 23:46:45 +0000381
Michael Gottesman4773a102013-06-21 05:42:08 +0000382 /// Conservatively merge the two RRInfo. Returns true if a partial merge has
Alp Tokercb402912014-01-24 17:20:08 +0000383 /// occurred, false otherwise.
Michael Gottesman4773a102013-06-21 05:42:08 +0000384 bool Merge(const RRInfo &Other);
385
John McCalld935e9c2011-06-15 23:37:01 +0000386 };
387}
388
389void RRInfo::clear() {
Dan Gohmanb3894012011-08-19 00:26:36 +0000390 KnownSafe = false;
John McCalld935e9c2011-06-15 23:37:01 +0000391 IsTailCallRelease = false;
Craig Topperf40110f2014-04-25 05:29:35 +0000392 ReleaseMetadata = nullptr;
John McCalld935e9c2011-06-15 23:37:01 +0000393 Calls.clear();
394 ReverseInsertPts.clear();
Michael Gottesmane67f40c2013-05-24 20:44:05 +0000395 CFGHazardAfflicted = false;
John McCalld935e9c2011-06-15 23:37:01 +0000396}
397
Michael Gottesman4773a102013-06-21 05:42:08 +0000398bool RRInfo::Merge(const RRInfo &Other) {
399 // Conservatively merge the ReleaseMetadata information.
400 if (ReleaseMetadata != Other.ReleaseMetadata)
Craig Topperf40110f2014-04-25 05:29:35 +0000401 ReleaseMetadata = nullptr;
Michael Gottesman4773a102013-06-21 05:42:08 +0000402
403 // Conservatively merge the boolean state.
404 KnownSafe &= Other.KnownSafe;
405 IsTailCallRelease &= Other.IsTailCallRelease;
406 CFGHazardAfflicted |= Other.CFGHazardAfflicted;
407
408 // Merge the call sets.
409 Calls.insert(Other.Calls.begin(), Other.Calls.end());
410
411 // Merge the insert point sets. If there are any differences,
412 // that makes this a partial merge.
413 bool Partial = ReverseInsertPts.size() != Other.ReverseInsertPts.size();
Craig Topper46276792014-08-24 23:23:06 +0000414 for (Instruction *Inst : Other.ReverseInsertPts)
David Blaikie70573dc2014-11-19 07:49:26 +0000415 Partial |= ReverseInsertPts.insert(Inst).second;
Michael Gottesman4773a102013-06-21 05:42:08 +0000416 return Partial;
417}
418
John McCalld935e9c2011-06-15 23:37:01 +0000419namespace {
Michael Gottesman97e3df02013-01-14 00:35:14 +0000420 /// \brief This class summarizes several per-pointer runtime properties which
421 /// are propogated through the flow graph.
John McCalld935e9c2011-06-15 23:37:01 +0000422 class PtrState {
Michael Gottesman97e3df02013-01-14 00:35:14 +0000423 /// True if the reference count is known to be incremented.
Dan Gohman62079b42012-04-25 00:50:46 +0000424 bool KnownPositiveRefCount;
425
Bob Wilson798a7702013-04-09 22:15:51 +0000426 /// True if we've seen an opportunity for partial RR elimination, such as
Michael Gottesman97e3df02013-01-14 00:35:14 +0000427 /// pushing calls into a CFG triangle or into one side of a CFG diamond.
Dan Gohman62079b42012-04-25 00:50:46 +0000428 bool Partial;
John McCalld935e9c2011-06-15 23:37:01 +0000429
Michael Gottesman97e3df02013-01-14 00:35:14 +0000430 /// The current position in the sequence.
Bill Wendling2798f1e2013-12-01 03:36:07 +0000431 unsigned char Seq : 8;
John McCalld935e9c2011-06-15 23:37:01 +0000432
Michael Gottesman97e3df02013-01-14 00:35:14 +0000433 /// Unidirectional information about the current sequence.
John McCalld935e9c2011-06-15 23:37:01 +0000434 RRInfo RRI;
435
Michael Gottesmane3943d02013-06-21 19:44:30 +0000436 public:
Dan Gohmandf476e52012-09-04 23:16:20 +0000437 PtrState() : KnownPositiveRefCount(false), Partial(false),
Dan Gohman41375a32012-05-08 23:39:44 +0000438 Seq(S_None) {}
John McCalld935e9c2011-06-15 23:37:01 +0000439
Michael Gottesman93132252013-06-21 06:59:02 +0000440
441 bool IsKnownSafe() const {
Michael Gottesman01df4502013-07-06 01:41:35 +0000442 return RRI.KnownSafe;
Michael Gottesman93132252013-06-21 06:59:02 +0000443 }
444
445 void SetKnownSafe(const bool NewValue) {
446 RRI.KnownSafe = NewValue;
447 }
448
Michael Gottesmanb82a1792013-06-21 07:00:44 +0000449 bool IsTailCallRelease() const {
450 return RRI.IsTailCallRelease;
451 }
452
453 void SetTailCallRelease(const bool NewValue) {
454 RRI.IsTailCallRelease = NewValue;
455 }
456
Michael Gottesman9799cf72013-06-21 20:52:49 +0000457 bool IsTrackingImpreciseReleases() const {
Craig Topperf40110f2014-04-25 05:29:35 +0000458 return RRI.ReleaseMetadata != nullptr;
Michael Gottesmanf0401182013-06-21 19:12:38 +0000459 }
460
Michael Gottesmanf701d3f2013-06-21 07:03:07 +0000461 const MDNode *GetReleaseMetadata() const {
462 return RRI.ReleaseMetadata;
463 }
464
465 void SetReleaseMetadata(MDNode *NewValue) {
466 RRI.ReleaseMetadata = NewValue;
467 }
468
Michael Gottesman2f294592013-06-21 19:12:36 +0000469 bool IsCFGHazardAfflicted() const {
470 return RRI.CFGHazardAfflicted;
471 }
472
473 void SetCFGHazardAfflicted(const bool NewValue) {
474 RRI.CFGHazardAfflicted = NewValue;
475 }
476
Michael Gottesman415ddd72013-02-05 19:32:18 +0000477 void SetKnownPositiveRefCount() {
Michael Gottesmanf3f9e3b2013-05-14 00:08:09 +0000478 DEBUG(dbgs() << "Setting Known Positive.\n");
Dan Gohman62079b42012-04-25 00:50:46 +0000479 KnownPositiveRefCount = true;
Dan Gohman12130272011-08-12 00:26:31 +0000480 }
481
Michael Gottesman764b1cf2013-03-23 05:46:19 +0000482 void ClearKnownPositiveRefCount() {
Michael Gottesmanf3f9e3b2013-05-14 00:08:09 +0000483 DEBUG(dbgs() << "Clearing Known Positive.\n");
Dan Gohman62079b42012-04-25 00:50:46 +0000484 KnownPositiveRefCount = false;
John McCalld935e9c2011-06-15 23:37:01 +0000485 }
486
Michael Gottesman07beea42013-03-23 05:31:01 +0000487 bool HasKnownPositiveRefCount() const {
Dan Gohman62079b42012-04-25 00:50:46 +0000488 return KnownPositiveRefCount;
John McCalld935e9c2011-06-15 23:37:01 +0000489 }
490
Michael Gottesman415ddd72013-02-05 19:32:18 +0000491 void SetSeq(Sequence NewSeq) {
Michael Gottesman89279f82013-04-05 18:10:41 +0000492 DEBUG(dbgs() << "Old: " << Seq << "; New: " << NewSeq << "\n");
Michael Gottesman79249972013-04-05 23:46:45 +0000493 Seq = NewSeq;
John McCalld935e9c2011-06-15 23:37:01 +0000494 }
495
Michael Gottesman415ddd72013-02-05 19:32:18 +0000496 Sequence GetSeq() const {
Bill Wendling2798f1e2013-12-01 03:36:07 +0000497 return static_cast<Sequence>(Seq);
John McCalld935e9c2011-06-15 23:37:01 +0000498 }
499
Michael Gottesman415ddd72013-02-05 19:32:18 +0000500 void ClearSequenceProgress() {
Dan Gohman62079b42012-04-25 00:50:46 +0000501 ResetSequenceProgress(S_None);
502 }
503
Michael Gottesman415ddd72013-02-05 19:32:18 +0000504 void ResetSequenceProgress(Sequence NewSeq) {
Michael Gottesman01338a42013-04-20 23:36:57 +0000505 DEBUG(dbgs() << "Resetting sequence progress.\n");
Michael Gottesman89279f82013-04-05 18:10:41 +0000506 SetSeq(NewSeq);
Dan Gohman62079b42012-04-25 00:50:46 +0000507 Partial = false;
John McCalld935e9c2011-06-15 23:37:01 +0000508 RRI.clear();
509 }
510
511 void Merge(const PtrState &Other, bool TopDown);
Michael Gottesman4f6ef112013-06-21 19:44:27 +0000512
513 void InsertCall(Instruction *I) {
514 RRI.Calls.insert(I);
515 }
516
517 void InsertReverseInsertPt(Instruction *I) {
518 RRI.ReverseInsertPts.insert(I);
519 }
520
521 void ClearReverseInsertPts() {
522 RRI.ReverseInsertPts.clear();
523 }
524
525 bool HasReverseInsertPts() const {
526 return !RRI.ReverseInsertPts.empty();
527 }
Michael Gottesmane3943d02013-06-21 19:44:30 +0000528
529 const RRInfo &GetRRInfo() const {
530 return RRI;
531 }
John McCalld935e9c2011-06-15 23:37:01 +0000532 };
533}
534
535void
536PtrState::Merge(const PtrState &Other, bool TopDown) {
Bill Wendlingcbcb02c2013-12-01 03:40:42 +0000537 Seq = MergeSeqs(GetSeq(), Other.GetSeq(), TopDown);
Michael Gottesmanb7deb4c2013-06-21 06:54:31 +0000538 KnownPositiveRefCount &= Other.KnownPositiveRefCount;
Michael Gottesman60f6b282013-03-29 05:13:07 +0000539
Dan Gohman1736c142011-10-17 18:48:25 +0000540 // If we're not in a sequence (anymore), drop all associated state.
John McCalld935e9c2011-06-15 23:37:01 +0000541 if (Seq == S_None) {
Dan Gohman62079b42012-04-25 00:50:46 +0000542 Partial = false;
John McCalld935e9c2011-06-15 23:37:01 +0000543 RRI.clear();
Dan Gohman62079b42012-04-25 00:50:46 +0000544 } else if (Partial || Other.Partial) {
Dan Gohman1736c142011-10-17 18:48:25 +0000545 // If we're doing a merge on a path that's previously seen a partial
546 // merge, conservatively drop the sequence, to avoid doing partial
547 // RR elimination. If the branch predicates for the two merge differ,
548 // mixing them is unsafe.
Dan Gohman62079b42012-04-25 00:50:46 +0000549 ClearSequenceProgress();
John McCalld935e9c2011-06-15 23:37:01 +0000550 } else {
Michael Gottesmanb7deb4c2013-06-21 06:54:31 +0000551 // Otherwise merge the other PtrState's RRInfo into our RRInfo. At this
552 // point, we know that currently we are not partial. Stash whether or not
553 // the merge operation caused us to undergo a partial merging of reverse
554 // insertion points.
Michael Gottesman4773a102013-06-21 05:42:08 +0000555 Partial = RRI.Merge(Other.RRI);
John McCalld935e9c2011-06-15 23:37:01 +0000556 }
557}
558
559namespace {
Michael Gottesman97e3df02013-01-14 00:35:14 +0000560 /// \brief Per-BasicBlock state.
John McCalld935e9c2011-06-15 23:37:01 +0000561 class BBState {
Michael Gottesman97e3df02013-01-14 00:35:14 +0000562 /// The number of unique control paths from the entry which can reach this
563 /// block.
John McCalld935e9c2011-06-15 23:37:01 +0000564 unsigned TopDownPathCount;
565
Michael Gottesman97e3df02013-01-14 00:35:14 +0000566 /// The number of unique control paths to exits from this block.
John McCalld935e9c2011-06-15 23:37:01 +0000567 unsigned BottomUpPathCount;
568
Michael Gottesman97e3df02013-01-14 00:35:14 +0000569 /// A type for PerPtrTopDown and PerPtrBottomUp.
John McCalld935e9c2011-06-15 23:37:01 +0000570 typedef MapVector<const Value *, PtrState> MapTy;
571
Michael Gottesman97e3df02013-01-14 00:35:14 +0000572 /// The top-down traversal uses this to record information known about a
573 /// pointer at the bottom of each block.
John McCalld935e9c2011-06-15 23:37:01 +0000574 MapTy PerPtrTopDown;
575
Michael Gottesman97e3df02013-01-14 00:35:14 +0000576 /// The bottom-up traversal uses this to record information known about a
577 /// pointer at the top of each block.
John McCalld935e9c2011-06-15 23:37:01 +0000578 MapTy PerPtrBottomUp;
579
Michael Gottesman97e3df02013-01-14 00:35:14 +0000580 /// Effective predecessors of the current block ignoring ignorable edges and
581 /// ignored backedges.
Dan Gohmanc24c66f2012-04-24 22:53:18 +0000582 SmallVector<BasicBlock *, 2> Preds;
Michael Gottesman97e3df02013-01-14 00:35:14 +0000583 /// Effective successors of the current block ignoring ignorable edges and
584 /// ignored backedges.
Dan Gohmanc24c66f2012-04-24 22:53:18 +0000585 SmallVector<BasicBlock *, 2> Succs;
586
John McCalld935e9c2011-06-15 23:37:01 +0000587 public:
Michael Gottesmand6ce6cb2013-08-09 23:22:27 +0000588 static const unsigned OverflowOccurredValue;
589
590 BBState() : TopDownPathCount(0), BottomUpPathCount(0) { }
John McCalld935e9c2011-06-15 23:37:01 +0000591
592 typedef MapTy::iterator ptr_iterator;
593 typedef MapTy::const_iterator ptr_const_iterator;
594
595 ptr_iterator top_down_ptr_begin() { return PerPtrTopDown.begin(); }
596 ptr_iterator top_down_ptr_end() { return PerPtrTopDown.end(); }
597 ptr_const_iterator top_down_ptr_begin() const {
598 return PerPtrTopDown.begin();
599 }
600 ptr_const_iterator top_down_ptr_end() const {
601 return PerPtrTopDown.end();
602 }
603
604 ptr_iterator bottom_up_ptr_begin() { return PerPtrBottomUp.begin(); }
605 ptr_iterator bottom_up_ptr_end() { return PerPtrBottomUp.end(); }
606 ptr_const_iterator bottom_up_ptr_begin() const {
607 return PerPtrBottomUp.begin();
608 }
609 ptr_const_iterator bottom_up_ptr_end() const {
610 return PerPtrBottomUp.end();
611 }
612
Michael Gottesman97e3df02013-01-14 00:35:14 +0000613 /// Mark this block as being an entry block, which has one path from the
614 /// entry by definition.
John McCalld935e9c2011-06-15 23:37:01 +0000615 void SetAsEntry() { TopDownPathCount = 1; }
616
Michael Gottesman97e3df02013-01-14 00:35:14 +0000617 /// Mark this block as being an exit block, which has one path to an exit by
618 /// definition.
John McCalld935e9c2011-06-15 23:37:01 +0000619 void SetAsExit() { BottomUpPathCount = 1; }
620
Michael Gottesman993fbf72013-05-13 19:40:39 +0000621 /// Attempt to find the PtrState object describing the top down state for
622 /// pointer Arg. Return a new initialized PtrState describing the top down
623 /// state for Arg if we do not find one.
John McCalld935e9c2011-06-15 23:37:01 +0000624 PtrState &getPtrTopDownState(const Value *Arg) {
625 return PerPtrTopDown[Arg];
626 }
627
Michael Gottesman993fbf72013-05-13 19:40:39 +0000628 /// Attempt to find the PtrState object describing the bottom up state for
629 /// pointer Arg. Return a new initialized PtrState describing the bottom up
630 /// state for Arg if we do not find one.
John McCalld935e9c2011-06-15 23:37:01 +0000631 PtrState &getPtrBottomUpState(const Value *Arg) {
632 return PerPtrBottomUp[Arg];
633 }
634
Michael Gottesmana76143ee2013-05-13 23:49:42 +0000635 /// Attempt to find the PtrState object describing the bottom up state for
636 /// pointer Arg.
637 ptr_iterator findPtrBottomUpState(const Value *Arg) {
638 return PerPtrBottomUp.find(Arg);
639 }
640
John McCalld935e9c2011-06-15 23:37:01 +0000641 void clearBottomUpPointers() {
Evan Chenge4df6a22011-08-04 18:40:26 +0000642 PerPtrBottomUp.clear();
John McCalld935e9c2011-06-15 23:37:01 +0000643 }
644
645 void clearTopDownPointers() {
646 PerPtrTopDown.clear();
647 }
648
649 void InitFromPred(const BBState &Other);
650 void InitFromSucc(const BBState &Other);
651 void MergePred(const BBState &Other);
652 void MergeSucc(const BBState &Other);
653
Michael Gottesman9e7261c2013-06-07 06:16:49 +0000654 /// Compute the number of possible unique paths from an entry to an exit
Michael Gottesman97e3df02013-01-14 00:35:14 +0000655 /// which pass through this block. This is only valid after both the
656 /// top-down and bottom-up traversals are complete.
Michael Gottesman9e7261c2013-06-07 06:16:49 +0000657 ///
Alp Tokercb402912014-01-24 17:20:08 +0000658 /// Returns true if overflow occurred. Returns false if overflow did not
Michael Gottesman9e7261c2013-06-07 06:16:49 +0000659 /// occur.
660 bool GetAllPathCountWithOverflow(unsigned &PathCount) const {
Michael Gottesmand6ce6cb2013-08-09 23:22:27 +0000661 if (TopDownPathCount == OverflowOccurredValue ||
662 BottomUpPathCount == OverflowOccurredValue)
663 return true;
Michael Gottesman9e7261c2013-06-07 06:16:49 +0000664 unsigned long long Product =
665 (unsigned long long)TopDownPathCount*BottomUpPathCount;
Alp Tokercb402912014-01-24 17:20:08 +0000666 // Overflow occurred if any of the upper bits of Product are set or if all
Michael Gottesmand6ce6cb2013-08-09 23:22:27 +0000667 // the lower bits of Product are all set.
668 return (Product >> 32) ||
669 ((PathCount = Product) == OverflowOccurredValue);
John McCalld935e9c2011-06-15 23:37:01 +0000670 }
Dan Gohman12130272011-08-12 00:26:31 +0000671
Dan Gohmanc24c66f2012-04-24 22:53:18 +0000672 // Specialized CFG utilities.
Dan Gohmandae33492012-04-27 18:56:31 +0000673 typedef SmallVectorImpl<BasicBlock *>::const_iterator edge_iterator;
Michael Gottesman0fecf982013-08-07 23:56:34 +0000674 edge_iterator pred_begin() const { return Preds.begin(); }
675 edge_iterator pred_end() const { return Preds.end(); }
676 edge_iterator succ_begin() const { return Succs.begin(); }
677 edge_iterator succ_end() const { return Succs.end(); }
Dan Gohmanc24c66f2012-04-24 22:53:18 +0000678
679 void addSucc(BasicBlock *Succ) { Succs.push_back(Succ); }
680 void addPred(BasicBlock *Pred) { Preds.push_back(Pred); }
681
682 bool isExit() const { return Succs.empty(); }
John McCalld935e9c2011-06-15 23:37:01 +0000683 };
Michael Gottesmand6ce6cb2013-08-09 23:22:27 +0000684
685 const unsigned BBState::OverflowOccurredValue = 0xffffffff;
John McCalld935e9c2011-06-15 23:37:01 +0000686}
687
688void BBState::InitFromPred(const BBState &Other) {
689 PerPtrTopDown = Other.PerPtrTopDown;
690 TopDownPathCount = Other.TopDownPathCount;
691}
692
693void BBState::InitFromSucc(const BBState &Other) {
694 PerPtrBottomUp = Other.PerPtrBottomUp;
695 BottomUpPathCount = Other.BottomUpPathCount;
696}
697
Michael Gottesman97e3df02013-01-14 00:35:14 +0000698/// The top-down traversal uses this to merge information about predecessors to
699/// form the initial state for a new block.
John McCalld935e9c2011-06-15 23:37:01 +0000700void BBState::MergePred(const BBState &Other) {
Michael Gottesmand6ce6cb2013-08-09 23:22:27 +0000701 if (TopDownPathCount == OverflowOccurredValue)
702 return;
703
John McCalld935e9c2011-06-15 23:37:01 +0000704 // Other.TopDownPathCount can be 0, in which case it is either dead or a
705 // loop backedge. Loop backedges are special.
706 TopDownPathCount += Other.TopDownPathCount;
707
Michael Gottesmand6ce6cb2013-08-09 23:22:27 +0000708 // In order to be consistent, we clear the top down pointers when by adding
709 // TopDownPathCount becomes OverflowOccurredValue even though "true" overflow
Alp Tokercb402912014-01-24 17:20:08 +0000710 // has not occurred.
Michael Gottesmand6ce6cb2013-08-09 23:22:27 +0000711 if (TopDownPathCount == OverflowOccurredValue) {
712 clearTopDownPointers();
713 return;
714 }
715
Michael Gottesman4385edf2013-01-14 01:47:53 +0000716 // Check for overflow. If we have overflow, fall back to conservative
717 // behavior.
Dan Gohman7c84dad2012-09-12 20:45:17 +0000718 if (TopDownPathCount < Other.TopDownPathCount) {
Michael Gottesmand6ce6cb2013-08-09 23:22:27 +0000719 TopDownPathCount = OverflowOccurredValue;
Dan Gohman7c84dad2012-09-12 20:45:17 +0000720 clearTopDownPointers();
721 return;
722 }
723
John McCalld935e9c2011-06-15 23:37:01 +0000724 // For each entry in the other set, if our set has an entry with the same key,
725 // merge the entries. Otherwise, copy the entry and merge it with an empty
726 // entry.
727 for (ptr_const_iterator MI = Other.top_down_ptr_begin(),
728 ME = Other.top_down_ptr_end(); MI != ME; ++MI) {
729 std::pair<ptr_iterator, bool> Pair = PerPtrTopDown.insert(*MI);
730 Pair.first->second.Merge(Pair.second ? PtrState() : MI->second,
731 /*TopDown=*/true);
732 }
733
Dan Gohman7e315fc32011-08-11 21:06:32 +0000734 // For each entry in our set, if the other set doesn't have an entry with the
John McCalld935e9c2011-06-15 23:37:01 +0000735 // same key, force it to merge with an empty entry.
736 for (ptr_iterator MI = top_down_ptr_begin(),
737 ME = top_down_ptr_end(); MI != ME; ++MI)
738 if (Other.PerPtrTopDown.find(MI->first) == Other.PerPtrTopDown.end())
739 MI->second.Merge(PtrState(), /*TopDown=*/true);
740}
741
Michael Gottesman97e3df02013-01-14 00:35:14 +0000742/// The bottom-up traversal uses this to merge information about successors to
743/// form the initial state for a new block.
John McCalld935e9c2011-06-15 23:37:01 +0000744void BBState::MergeSucc(const BBState &Other) {
Michael Gottesmand6ce6cb2013-08-09 23:22:27 +0000745 if (BottomUpPathCount == OverflowOccurredValue)
746 return;
747
John McCalld935e9c2011-06-15 23:37:01 +0000748 // Other.BottomUpPathCount can be 0, in which case it is either dead or a
749 // loop backedge. Loop backedges are special.
750 BottomUpPathCount += Other.BottomUpPathCount;
751
Michael Gottesmand6ce6cb2013-08-09 23:22:27 +0000752 // In order to be consistent, we clear the top down pointers when by adding
753 // BottomUpPathCount becomes OverflowOccurredValue even though "true" overflow
Alp Tokercb402912014-01-24 17:20:08 +0000754 // has not occurred.
Michael Gottesmand6ce6cb2013-08-09 23:22:27 +0000755 if (BottomUpPathCount == OverflowOccurredValue) {
756 clearBottomUpPointers();
757 return;
758 }
759
Michael Gottesman4385edf2013-01-14 01:47:53 +0000760 // Check for overflow. If we have overflow, fall back to conservative
761 // behavior.
Dan Gohman7c84dad2012-09-12 20:45:17 +0000762 if (BottomUpPathCount < Other.BottomUpPathCount) {
Michael Gottesmand6ce6cb2013-08-09 23:22:27 +0000763 BottomUpPathCount = OverflowOccurredValue;
Dan Gohman7c84dad2012-09-12 20:45:17 +0000764 clearBottomUpPointers();
765 return;
766 }
767
John McCalld935e9c2011-06-15 23:37:01 +0000768 // For each entry in the other set, if our set has an entry with the
769 // same key, merge the entries. Otherwise, copy the entry and merge
770 // it with an empty entry.
771 for (ptr_const_iterator MI = Other.bottom_up_ptr_begin(),
772 ME = Other.bottom_up_ptr_end(); MI != ME; ++MI) {
773 std::pair<ptr_iterator, bool> Pair = PerPtrBottomUp.insert(*MI);
774 Pair.first->second.Merge(Pair.second ? PtrState() : MI->second,
775 /*TopDown=*/false);
776 }
777
Dan Gohman7e315fc32011-08-11 21:06:32 +0000778 // For each entry in our set, if the other set doesn't have an entry
John McCalld935e9c2011-06-15 23:37:01 +0000779 // with the same key, force it to merge with an empty entry.
780 for (ptr_iterator MI = bottom_up_ptr_begin(),
781 ME = bottom_up_ptr_end(); MI != ME; ++MI)
782 if (Other.PerPtrBottomUp.find(MI->first) == Other.PerPtrBottomUp.end())
783 MI->second.Merge(PtrState(), /*TopDown=*/false);
784}
785
Michael Gottesman81b1d432013-03-26 00:42:04 +0000786// Only enable ARC Annotations if we are building a debug version of
787// libObjCARCOpts.
788#ifndef NDEBUG
789#define ARC_ANNOTATIONS
790#endif
791
792// Define some macros along the lines of DEBUG and some helper functions to make
793// it cleaner to create annotations in the source code and to no-op when not
794// building in debug mode.
795#ifdef ARC_ANNOTATIONS
796
797#include "llvm/Support/CommandLine.h"
798
799/// Enable/disable ARC sequence annotations.
800static cl::opt<bool>
Michael Gottesman6806b512013-04-17 20:48:03 +0000801EnableARCAnnotations("enable-objc-arc-annotations", cl::init(false),
802 cl::desc("Enable emission of arc data flow analysis "
803 "annotations"));
Michael Gottesmanffef24f2013-04-17 20:48:01 +0000804static cl::opt<bool>
Michael Gottesmanadb921a2013-04-17 21:03:53 +0000805DisableCheckForCFGHazards("disable-objc-arc-checkforcfghazards", cl::init(false),
806 cl::desc("Disable check for cfg hazards when "
807 "annotating"));
Michael Gottesman4e88ce62013-04-17 21:59:41 +0000808static cl::opt<std::string>
809ARCAnnotationTargetIdentifier("objc-arc-annotation-target-identifier",
810 cl::init(""),
811 cl::desc("filter out all data flow annotations "
812 "but those that apply to the given "
813 "target llvm identifier."));
Michael Gottesman81b1d432013-03-26 00:42:04 +0000814
815/// This function appends a unique ARCAnnotationProvenanceSourceMDKind id to an
816/// instruction so that we can track backwards when post processing via the llvm
817/// arc annotation processor tool. If the function is an
818static MDString *AppendMDNodeToSourcePtr(unsigned NodeId,
819 Value *Ptr) {
Craig Toppere73658d2014-04-28 04:05:08 +0000820 MDString *Hash = nullptr;
Michael Gottesman81b1d432013-03-26 00:42:04 +0000821
822 // If pointer is a result of an instruction and it does not have a source
823 // MDNode it, attach a new MDNode onto it. If pointer is a result of
824 // an instruction and does have a source MDNode attached to it, return a
825 // reference to said Node. Otherwise just return 0.
826 if (Instruction *Inst = dyn_cast<Instruction>(Ptr)) {
827 MDNode *Node;
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000828 if (!(Node = Inst->getMetadata(NodeId))) {
Michael Gottesman81b1d432013-03-26 00:42:04 +0000829 // We do not have any node. Generate and attatch the hash MDString to the
830 // instruction.
831
832 // We just use an MDString to ensure that this metadata gets written out
833 // of line at the module level and to provide a very simple format
834 // encoding the information herein. Both of these makes it simpler to
835 // parse the annotations by a simple external program.
Alp Tokere69170a2014-06-26 22:52:05 +0000836 std::string Str;
837 raw_string_ostream os(Str);
Michael Gottesman81b1d432013-03-26 00:42:04 +0000838 os << "(" << Inst->getParent()->getParent()->getName() << ",%"
839 << Inst->getName() << ")";
840
841 Hash = MDString::get(Inst->getContext(), os.str());
842 Inst->setMetadata(NodeId, MDNode::get(Inst->getContext(),Hash));
843 } else {
844 // We have a node. Grab its hash and return it.
845 assert(Node->getNumOperands() == 1 &&
846 "An ARCAnnotationProvenanceSourceMDKind can only have 1 operand.");
847 Hash = cast<MDString>(Node->getOperand(0));
848 }
849 } else if (Argument *Arg = dyn_cast<Argument>(Ptr)) {
Alp Tokere69170a2014-06-26 22:52:05 +0000850 std::string str;
851 raw_string_ostream os(str);
Michael Gottesman81b1d432013-03-26 00:42:04 +0000852 os << "(" << Arg->getParent()->getName() << ",%" << Arg->getName()
853 << ")";
854 Hash = MDString::get(Arg->getContext(), os.str());
855 }
856
857 return Hash;
858}
859
Michael Gottesmancd4de0f2013-03-26 00:42:09 +0000860static std::string SequenceToString(Sequence A) {
Alp Tokere69170a2014-06-26 22:52:05 +0000861 std::string str;
862 raw_string_ostream os(str);
Michael Gottesmancd4de0f2013-03-26 00:42:09 +0000863 os << A;
864 return os.str();
865}
866
Michael Gottesman81b1d432013-03-26 00:42:04 +0000867/// Helper function to change a Sequence into a String object using our overload
868/// for raw_ostream so we only have printing code in one location.
869static MDString *SequenceToMDString(LLVMContext &Context,
870 Sequence A) {
Michael Gottesmancd4de0f2013-03-26 00:42:09 +0000871 return MDString::get(Context, SequenceToString(A));
Michael Gottesman81b1d432013-03-26 00:42:04 +0000872}
873
874/// A simple function to generate a MDNode which describes the change in state
875/// for Value *Ptr caused by Instruction *Inst.
876static void AppendMDNodeToInstForPtr(unsigned NodeId,
877 Instruction *Inst,
878 Value *Ptr,
879 MDString *PtrSourceMDNodeID,
880 Sequence OldSeq,
881 Sequence NewSeq) {
Craig Toppere73658d2014-04-28 04:05:08 +0000882 MDNode *Node = nullptr;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000883 Metadata *tmp[3] = {PtrSourceMDNodeID,
884 SequenceToMDString(Inst->getContext(), OldSeq),
885 SequenceToMDString(Inst->getContext(), NewSeq)};
Craig Toppere1d12942014-08-27 05:25:25 +0000886 Node = MDNode::get(Inst->getContext(), tmp);
Michael Gottesman81b1d432013-03-26 00:42:04 +0000887
888 Inst->setMetadata(NodeId, Node);
889}
890
Michael Gottesmancd4de0f2013-03-26 00:42:09 +0000891/// Add to the beginning of the basic block llvm.ptr.annotations which show the
892/// state of a pointer at the entrance to a basic block.
893static void GenerateARCBBEntranceAnnotation(const char *Name, BasicBlock *BB,
894 Value *Ptr, Sequence Seq) {
Michael Gottesman4e88ce62013-04-17 21:59:41 +0000895 // If we have a target identifier, make sure that we match it before
896 // continuing.
897 if(!ARCAnnotationTargetIdentifier.empty() &&
898 !Ptr->getName().equals(ARCAnnotationTargetIdentifier))
899 return;
Michael Gottesman9e518132013-04-18 04:34:11 +0000900
Michael Gottesmancd4de0f2013-03-26 00:42:09 +0000901 Module *M = BB->getParent()->getParent();
902 LLVMContext &C = M->getContext();
903 Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C));
904 Type *I8XX = PointerType::getUnqual(I8X);
905 Type *Params[] = {I8XX, I8XX};
Craig Toppere1d12942014-08-27 05:25:25 +0000906 FunctionType *FTy = FunctionType::get(Type::getVoidTy(C), Params,
Michael Gottesmancd4de0f2013-03-26 00:42:09 +0000907 /*isVarArg=*/false);
908 Constant *Callee = M->getOrInsertFunction(Name, FTy);
Michael Gottesman60f6b282013-03-29 05:13:07 +0000909
910 IRBuilder<> Builder(BB, BB->getFirstInsertionPt());
911
Michael Gottesmancd4de0f2013-03-26 00:42:09 +0000912 Value *PtrName;
913 StringRef Tmp = Ptr->getName();
Craig Toppere73658d2014-04-28 04:05:08 +0000914 if (nullptr == (PtrName = M->getGlobalVariable(Tmp, true))) {
Michael Gottesmancd4de0f2013-03-26 00:42:09 +0000915 Value *ActualPtrName = Builder.CreateGlobalStringPtr(Tmp,
916 Tmp + "_STR");
917 PtrName = new GlobalVariable(*M, I8X, true, GlobalVariable::InternalLinkage,
Michael Gottesman60f6b282013-03-29 05:13:07 +0000918 cast<Constant>(ActualPtrName), Tmp);
Michael Gottesmancd4de0f2013-03-26 00:42:09 +0000919 }
920
921 Value *S;
922 std::string SeqStr = SequenceToString(Seq);
Craig Toppere73658d2014-04-28 04:05:08 +0000923 if (nullptr == (S = M->getGlobalVariable(SeqStr, true))) {
Michael Gottesmancd4de0f2013-03-26 00:42:09 +0000924 Value *ActualPtrName = Builder.CreateGlobalStringPtr(SeqStr,
925 SeqStr + "_STR");
926 S = new GlobalVariable(*M, I8X, true, GlobalVariable::InternalLinkage,
927 cast<Constant>(ActualPtrName), SeqStr);
928 }
929
930 Builder.CreateCall2(Callee, PtrName, S);
931}
932
933/// Add to the end of the basic block llvm.ptr.annotations which show the state
934/// of the pointer at the bottom of the basic block.
935static void GenerateARCBBTerminatorAnnotation(const char *Name, BasicBlock *BB,
936 Value *Ptr, Sequence Seq) {
Michael Gottesman4e88ce62013-04-17 21:59:41 +0000937 // If we have a target identifier, make sure that we match it before emitting
938 // an annotation.
939 if(!ARCAnnotationTargetIdentifier.empty() &&
940 !Ptr->getName().equals(ARCAnnotationTargetIdentifier))
941 return;
Michael Gottesman9e518132013-04-18 04:34:11 +0000942
Michael Gottesmancd4de0f2013-03-26 00:42:09 +0000943 Module *M = BB->getParent()->getParent();
944 LLVMContext &C = M->getContext();
945 Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C));
946 Type *I8XX = PointerType::getUnqual(I8X);
947 Type *Params[] = {I8XX, I8XX};
Craig Toppere1d12942014-08-27 05:25:25 +0000948 FunctionType *FTy = FunctionType::get(Type::getVoidTy(C), Params,
Michael Gottesmancd4de0f2013-03-26 00:42:09 +0000949 /*isVarArg=*/false);
950 Constant *Callee = M->getOrInsertFunction(Name, FTy);
Michael Gottesman60f6b282013-03-29 05:13:07 +0000951
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +0000952 IRBuilder<> Builder(BB, std::prev(BB->end()));
Michael Gottesman60f6b282013-03-29 05:13:07 +0000953
Michael Gottesmancd4de0f2013-03-26 00:42:09 +0000954 Value *PtrName;
955 StringRef Tmp = Ptr->getName();
Craig Toppere73658d2014-04-28 04:05:08 +0000956 if (nullptr == (PtrName = M->getGlobalVariable(Tmp, true))) {
Michael Gottesmancd4de0f2013-03-26 00:42:09 +0000957 Value *ActualPtrName = Builder.CreateGlobalStringPtr(Tmp,
958 Tmp + "_STR");
959 PtrName = new GlobalVariable(*M, I8X, true, GlobalVariable::InternalLinkage,
Michael Gottesman60f6b282013-03-29 05:13:07 +0000960 cast<Constant>(ActualPtrName), Tmp);
Michael Gottesmancd4de0f2013-03-26 00:42:09 +0000961 }
962
963 Value *S;
964 std::string SeqStr = SequenceToString(Seq);
Craig Toppere73658d2014-04-28 04:05:08 +0000965 if (nullptr == (S = M->getGlobalVariable(SeqStr, true))) {
Michael Gottesmancd4de0f2013-03-26 00:42:09 +0000966 Value *ActualPtrName = Builder.CreateGlobalStringPtr(SeqStr,
967 SeqStr + "_STR");
968 S = new GlobalVariable(*M, I8X, true, GlobalVariable::InternalLinkage,
969 cast<Constant>(ActualPtrName), SeqStr);
970 }
Michael Gottesman60f6b282013-03-29 05:13:07 +0000971 Builder.CreateCall2(Callee, PtrName, S);
Michael Gottesmancd4de0f2013-03-26 00:42:09 +0000972}
973
Michael Gottesman81b1d432013-03-26 00:42:04 +0000974/// Adds a source annotation to pointer and a state change annotation to Inst
975/// referencing the source annotation and the old/new state of pointer.
976static void GenerateARCAnnotation(unsigned InstMDId,
977 unsigned PtrMDId,
978 Instruction *Inst,
979 Value *Ptr,
980 Sequence OldSeq,
981 Sequence NewSeq) {
982 if (EnableARCAnnotations) {
Michael Gottesman4e88ce62013-04-17 21:59:41 +0000983 // If we have a target identifier, make sure that we match it before
984 // emitting an annotation.
985 if(!ARCAnnotationTargetIdentifier.empty() &&
986 !Ptr->getName().equals(ARCAnnotationTargetIdentifier))
987 return;
Michael Gottesman9e518132013-04-18 04:34:11 +0000988
Michael Gottesman81b1d432013-03-26 00:42:04 +0000989 // First generate the source annotation on our pointer. This will return an
990 // MDString* if Ptr actually comes from an instruction implying we can put
991 // in a source annotation. If AppendMDNodeToSourcePtr returns 0 (i.e. NULL),
992 // then we know that our pointer is from an Argument so we put a reference
993 // to the argument number.
994 //
995 // The point of this is to make it easy for the
996 // llvm-arc-annotation-processor tool to cross reference where the source
997 // pointer is in the LLVM IR since the LLVM IR parser does not submit such
998 // information via debug info for backends to use (since why would anyone
Alp Tokerf907b892013-12-05 05:44:44 +0000999 // need such a thing from LLVM IR besides in non-standard cases
Michael Gottesman81b1d432013-03-26 00:42:04 +00001000 // [i.e. this]).
1001 MDString *SourcePtrMDNode =
1002 AppendMDNodeToSourcePtr(PtrMDId, Ptr);
1003 AppendMDNodeToInstForPtr(InstMDId, Inst, Ptr, SourcePtrMDNode, OldSeq,
1004 NewSeq);
1005 }
1006}
1007
1008// The actual interface for accessing the above functionality is defined via
1009// some simple macros which are defined below. We do this so that the user does
1010// not need to pass in what metadata id is needed resulting in cleaner code and
1011// additionally since it provides an easy way to conditionally no-op all
1012// annotation support in a non-debug build.
1013
1014/// Use this macro to annotate a sequence state change when processing
1015/// instructions bottom up,
1016#define ANNOTATE_BOTTOMUP(inst, ptr, old, new) \
1017 GenerateARCAnnotation(ARCAnnotationBottomUpMDKind, \
1018 ARCAnnotationProvenanceSourceMDKind, (inst), \
1019 const_cast<Value*>(ptr), (old), (new))
1020/// Use this macro to annotate a sequence state change when processing
1021/// instructions top down.
1022#define ANNOTATE_TOPDOWN(inst, ptr, old, new) \
1023 GenerateARCAnnotation(ARCAnnotationTopDownMDKind, \
1024 ARCAnnotationProvenanceSourceMDKind, (inst), \
1025 const_cast<Value*>(ptr), (old), (new))
1026
Michael Gottesman43e7e002013-04-03 22:41:59 +00001027#define ANNOTATE_BB(_states, _bb, _name, _type, _direction) \
1028 do { \
Michael Gottesman89279f82013-04-05 18:10:41 +00001029 if (EnableARCAnnotations) { \
1030 for(BBState::ptr_const_iterator I = (_states)._direction##_ptr_begin(), \
Michael Gottesman43e7e002013-04-03 22:41:59 +00001031 E = (_states)._direction##_ptr_end(); I != E; ++I) { \
Michael Gottesman89279f82013-04-05 18:10:41 +00001032 Value *Ptr = const_cast<Value*>(I->first); \
1033 Sequence Seq = I->second.GetSeq(); \
1034 GenerateARCBB ## _type ## Annotation(_name, (_bb), Ptr, Seq); \
1035 } \
Michael Gottesman43e7e002013-04-03 22:41:59 +00001036 } \
Michael Gottesman89279f82013-04-05 18:10:41 +00001037 } while (0)
Michael Gottesman43e7e002013-04-03 22:41:59 +00001038
Michael Gottesman89279f82013-04-05 18:10:41 +00001039#define ANNOTATE_BOTTOMUP_BBSTART(_states, _basicblock) \
Michael Gottesman43e7e002013-04-03 22:41:59 +00001040 ANNOTATE_BB(_states, _basicblock, "llvm.arc.annotation.bottomup.bbstart", \
1041 Entrance, bottom_up)
Michael Gottesman89279f82013-04-05 18:10:41 +00001042#define ANNOTATE_BOTTOMUP_BBEND(_states, _basicblock) \
1043 ANNOTATE_BB(_states, _basicblock, "llvm.arc.annotation.bottomup.bbend", \
Michael Gottesman43e7e002013-04-03 22:41:59 +00001044 Terminator, bottom_up)
Michael Gottesman89279f82013-04-05 18:10:41 +00001045#define ANNOTATE_TOPDOWN_BBSTART(_states, _basicblock) \
1046 ANNOTATE_BB(_states, _basicblock, "llvm.arc.annotation.topdown.bbstart", \
Michael Gottesman43e7e002013-04-03 22:41:59 +00001047 Entrance, top_down)
Michael Gottesman89279f82013-04-05 18:10:41 +00001048#define ANNOTATE_TOPDOWN_BBEND(_states, _basicblock) \
1049 ANNOTATE_BB(_states, _basicblock, "llvm.arc.annotation.topdown.bbend", \
Michael Gottesman43e7e002013-04-03 22:41:59 +00001050 Terminator, top_down)
1051
Michael Gottesman81b1d432013-03-26 00:42:04 +00001052#else // !ARC_ANNOTATION
1053// If annotations are off, noop.
1054#define ANNOTATE_BOTTOMUP(inst, ptr, old, new)
1055#define ANNOTATE_TOPDOWN(inst, ptr, old, new)
Michael Gottesman43e7e002013-04-03 22:41:59 +00001056#define ANNOTATE_BOTTOMUP_BBSTART(states, basicblock)
1057#define ANNOTATE_BOTTOMUP_BBEND(states, basicblock)
1058#define ANNOTATE_TOPDOWN_BBSTART(states, basicblock)
1059#define ANNOTATE_TOPDOWN_BBEND(states, basicblock)
Michael Gottesman81b1d432013-03-26 00:42:04 +00001060#endif // !ARC_ANNOTATION
1061
John McCalld935e9c2011-06-15 23:37:01 +00001062namespace {
Michael Gottesman97e3df02013-01-14 00:35:14 +00001063 /// \brief The main ARC optimization pass.
John McCalld935e9c2011-06-15 23:37:01 +00001064 class ObjCARCOpt : public FunctionPass {
1065 bool Changed;
1066 ProvenanceAnalysis PA;
Michael Gottesman14acfac2013-07-06 01:39:23 +00001067 ARCRuntimeEntryPoints EP;
John McCalld935e9c2011-06-15 23:37:01 +00001068
Michael Gottesman5a91bbf2013-05-24 20:44:02 +00001069 // This is used to track if a pointer is stored into an alloca.
1070 DenseSet<const Value *> MultiOwnersSet;
1071
Michael Gottesman97e3df02013-01-14 00:35:14 +00001072 /// A flag indicating whether this optimization pass should run.
Dan Gohmanceaac7c2011-06-20 23:20:43 +00001073 bool Run;
1074
Michael Gottesman97e3df02013-01-14 00:35:14 +00001075 /// Flags which determine whether each of the interesting runtine functions
1076 /// is in fact used in the current function.
John McCalld935e9c2011-06-15 23:37:01 +00001077 unsigned UsedInThisFunction;
1078
Michael Gottesman97e3df02013-01-14 00:35:14 +00001079 /// The Metadata Kind for clang.imprecise_release metadata.
John McCalld935e9c2011-06-15 23:37:01 +00001080 unsigned ImpreciseReleaseMDKind;
1081
Michael Gottesman97e3df02013-01-14 00:35:14 +00001082 /// The Metadata Kind for clang.arc.copy_on_escape metadata.
Dan Gohmana7107f92011-10-17 22:53:25 +00001083 unsigned CopyOnEscapeMDKind;
1084
Michael Gottesman97e3df02013-01-14 00:35:14 +00001085 /// The Metadata Kind for clang.arc.no_objc_arc_exceptions metadata.
Dan Gohman0155f302012-02-17 18:59:53 +00001086 unsigned NoObjCARCExceptionsMDKind;
1087
Michael Gottesman81b1d432013-03-26 00:42:04 +00001088#ifdef ARC_ANNOTATIONS
1089 /// The Metadata Kind for llvm.arc.annotation.bottomup metadata.
1090 unsigned ARCAnnotationBottomUpMDKind;
1091 /// The Metadata Kind for llvm.arc.annotation.topdown metadata.
1092 unsigned ARCAnnotationTopDownMDKind;
1093 /// The Metadata Kind for llvm.arc.annotation.provenancesource metadata.
1094 unsigned ARCAnnotationProvenanceSourceMDKind;
1095#endif // ARC_ANNOATIONS
1096
John McCalld935e9c2011-06-15 23:37:01 +00001097 bool OptimizeRetainRVCall(Function &F, Instruction *RetainRV);
Michael Gottesman556ff612013-01-12 01:25:19 +00001098 void OptimizeAutoreleaseRVCall(Function &F, Instruction *AutoreleaseRV,
1099 InstructionClass &Class);
John McCalld935e9c2011-06-15 23:37:01 +00001100 void OptimizeIndividualCalls(Function &F);
1101
1102 void CheckForCFGHazards(const BasicBlock *BB,
1103 DenseMap<const BasicBlock *, BBState> &BBStates,
1104 BBState &MyStates) const;
Dan Gohman817a7c62012-03-22 18:24:56 +00001105 bool VisitInstructionBottomUp(Instruction *Inst,
Dan Gohman5c70fad2012-03-23 17:47:54 +00001106 BasicBlock *BB,
Dan Gohman817a7c62012-03-22 18:24:56 +00001107 MapVector<Value *, RRInfo> &Retains,
1108 BBState &MyStates);
John McCalld935e9c2011-06-15 23:37:01 +00001109 bool VisitBottomUp(BasicBlock *BB,
1110 DenseMap<const BasicBlock *, BBState> &BBStates,
1111 MapVector<Value *, RRInfo> &Retains);
Dan Gohman817a7c62012-03-22 18:24:56 +00001112 bool VisitInstructionTopDown(Instruction *Inst,
1113 DenseMap<Value *, RRInfo> &Releases,
1114 BBState &MyStates);
John McCalld935e9c2011-06-15 23:37:01 +00001115 bool VisitTopDown(BasicBlock *BB,
1116 DenseMap<const BasicBlock *, BBState> &BBStates,
1117 DenseMap<Value *, RRInfo> &Releases);
1118 bool Visit(Function &F,
1119 DenseMap<const BasicBlock *, BBState> &BBStates,
1120 MapVector<Value *, RRInfo> &Retains,
1121 DenseMap<Value *, RRInfo> &Releases);
1122
1123 void MoveCalls(Value *Arg, RRInfo &RetainsToMove, RRInfo &ReleasesToMove,
1124 MapVector<Value *, RRInfo> &Retains,
1125 DenseMap<Value *, RRInfo> &Releases,
Dan Gohman6320f522011-07-22 22:29:21 +00001126 SmallVectorImpl<Instruction *> &DeadInsts,
1127 Module *M);
John McCalld935e9c2011-06-15 23:37:01 +00001128
Michael Gottesman9de6f962013-01-22 21:49:00 +00001129 bool ConnectTDBUTraversals(DenseMap<const BasicBlock *, BBState> &BBStates,
1130 MapVector<Value *, RRInfo> &Retains,
1131 DenseMap<Value *, RRInfo> &Releases,
1132 Module *M,
Craig Topperb94011f2013-07-14 04:42:23 +00001133 SmallVectorImpl<Instruction *> &NewRetains,
1134 SmallVectorImpl<Instruction *> &NewReleases,
1135 SmallVectorImpl<Instruction *> &DeadInsts,
Michael Gottesman9de6f962013-01-22 21:49:00 +00001136 RRInfo &RetainsToMove,
1137 RRInfo &ReleasesToMove,
1138 Value *Arg,
1139 bool KnownSafe,
1140 bool &AnyPairsCompletelyEliminated);
1141
John McCalld935e9c2011-06-15 23:37:01 +00001142 bool PerformCodePlacement(DenseMap<const BasicBlock *, BBState> &BBStates,
1143 MapVector<Value *, RRInfo> &Retains,
Dan Gohman6320f522011-07-22 22:29:21 +00001144 DenseMap<Value *, RRInfo> &Releases,
1145 Module *M);
John McCalld935e9c2011-06-15 23:37:01 +00001146
1147 void OptimizeWeakCalls(Function &F);
1148
1149 bool OptimizeSequences(Function &F);
1150
1151 void OptimizeReturns(Function &F);
1152
Michael Gottesman9c118152013-04-29 06:16:57 +00001153#ifndef NDEBUG
1154 void GatherStatistics(Function &F, bool AfterOptimization = false);
1155#endif
1156
Craig Topper3e4c6972014-03-05 09:10:37 +00001157 void getAnalysisUsage(AnalysisUsage &AU) const override;
1158 bool doInitialization(Module &M) override;
1159 bool runOnFunction(Function &F) override;
1160 void releaseMemory() override;
John McCalld935e9c2011-06-15 23:37:01 +00001161
1162 public:
1163 static char ID;
1164 ObjCARCOpt() : FunctionPass(ID) {
1165 initializeObjCARCOptPass(*PassRegistry::getPassRegistry());
1166 }
1167 };
1168}
1169
1170char ObjCARCOpt::ID = 0;
1171INITIALIZE_PASS_BEGIN(ObjCARCOpt,
1172 "objc-arc", "ObjC ARC optimization", false, false)
1173INITIALIZE_PASS_DEPENDENCY(ObjCARCAliasAnalysis)
1174INITIALIZE_PASS_END(ObjCARCOpt,
1175 "objc-arc", "ObjC ARC optimization", false, false)
1176
1177Pass *llvm::createObjCARCOptPass() {
1178 return new ObjCARCOpt();
1179}
1180
1181void ObjCARCOpt::getAnalysisUsage(AnalysisUsage &AU) const {
1182 AU.addRequired<ObjCARCAliasAnalysis>();
1183 AU.addRequired<AliasAnalysis>();
1184 // ARC optimization doesn't currently split critical edges.
1185 AU.setPreservesCFG();
1186}
1187
Michael Gottesman97e3df02013-01-14 00:35:14 +00001188/// Turn objc_retainAutoreleasedReturnValue into objc_retain if the operand is
1189/// not a return value. Or, if it can be paired with an
1190/// objc_autoreleaseReturnValue, delete the pair and return true.
John McCalld935e9c2011-06-15 23:37:01 +00001191bool
1192ObjCARCOpt::OptimizeRetainRVCall(Function &F, Instruction *RetainRV) {
Dan Gohmane3ed2b02012-03-23 18:09:00 +00001193 // Check for the argument being from an immediately preceding call or invoke.
Dan Gohmandae33492012-04-27 18:56:31 +00001194 const Value *Arg = GetObjCArg(RetainRV);
1195 ImmutableCallSite CS(Arg);
1196 if (const Instruction *Call = CS.getInstruction()) {
John McCalld935e9c2011-06-15 23:37:01 +00001197 if (Call->getParent() == RetainRV->getParent()) {
Dan Gohmandae33492012-04-27 18:56:31 +00001198 BasicBlock::const_iterator I = Call;
John McCalld935e9c2011-06-15 23:37:01 +00001199 ++I;
Michael Gottesman65c24812013-03-25 09:27:43 +00001200 while (IsNoopInstruction(I)) ++I;
John McCalld935e9c2011-06-15 23:37:01 +00001201 if (&*I == RetainRV)
1202 return false;
Dan Gohmandae33492012-04-27 18:56:31 +00001203 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(Call)) {
Dan Gohmane3ed2b02012-03-23 18:09:00 +00001204 BasicBlock *RetainRVParent = RetainRV->getParent();
1205 if (II->getNormalDest() == RetainRVParent) {
Dan Gohmandae33492012-04-27 18:56:31 +00001206 BasicBlock::const_iterator I = RetainRVParent->begin();
Michael Gottesman65c24812013-03-25 09:27:43 +00001207 while (IsNoopInstruction(I)) ++I;
Dan Gohmane3ed2b02012-03-23 18:09:00 +00001208 if (&*I == RetainRV)
1209 return false;
1210 }
John McCalld935e9c2011-06-15 23:37:01 +00001211 }
Dan Gohmane3ed2b02012-03-23 18:09:00 +00001212 }
John McCalld935e9c2011-06-15 23:37:01 +00001213
1214 // Check for being preceded by an objc_autoreleaseReturnValue on the same
1215 // pointer. In this case, we can delete the pair.
1216 BasicBlock::iterator I = RetainRV, Begin = RetainRV->getParent()->begin();
1217 if (I != Begin) {
Michael Gottesman65c24812013-03-25 09:27:43 +00001218 do --I; while (I != Begin && IsNoopInstruction(I));
John McCalld935e9c2011-06-15 23:37:01 +00001219 if (GetBasicInstructionClass(I) == IC_AutoreleaseRV &&
1220 GetObjCArg(I) == Arg) {
1221 Changed = true;
1222 ++NumPeeps;
Michael Gottesman10426b52013-01-07 21:26:07 +00001223
Michael Gottesman89279f82013-04-05 18:10:41 +00001224 DEBUG(dbgs() << "Erasing autoreleaseRV,retainRV pair: " << *I << "\n"
1225 << "Erasing " << *RetainRV << "\n");
Michael Gottesman10426b52013-01-07 21:26:07 +00001226
John McCalld935e9c2011-06-15 23:37:01 +00001227 EraseInstruction(I);
1228 EraseInstruction(RetainRV);
1229 return true;
1230 }
1231 }
1232
1233 // Turn it to a plain objc_retain.
1234 Changed = true;
1235 ++NumPeeps;
Michael Gottesman10426b52013-01-07 21:26:07 +00001236
Michael Gottesman89279f82013-04-05 18:10:41 +00001237 DEBUG(dbgs() << "Transforming objc_retainAutoreleasedReturnValue => "
Michael Gottesmandef07bb2013-01-05 17:55:42 +00001238 "objc_retain since the operand is not a return value.\n"
Michael Gottesman89279f82013-04-05 18:10:41 +00001239 "Old = " << *RetainRV << "\n");
Michael Gottesman10426b52013-01-07 21:26:07 +00001240
Michael Gottesman14acfac2013-07-06 01:39:23 +00001241 Constant *NewDecl = EP.get(ARCRuntimeEntryPoints::EPT_Retain);
1242 cast<CallInst>(RetainRV)->setCalledFunction(NewDecl);
Michael Gottesmandef07bb2013-01-05 17:55:42 +00001243
Michael Gottesman89279f82013-04-05 18:10:41 +00001244 DEBUG(dbgs() << "New = " << *RetainRV << "\n");
Michael Gottesmandef07bb2013-01-05 17:55:42 +00001245
John McCalld935e9c2011-06-15 23:37:01 +00001246 return false;
1247}
1248
Michael Gottesman97e3df02013-01-14 00:35:14 +00001249/// Turn objc_autoreleaseReturnValue into objc_autorelease if the result is not
1250/// used as a return value.
John McCalld935e9c2011-06-15 23:37:01 +00001251void
Michael Gottesman556ff612013-01-12 01:25:19 +00001252ObjCARCOpt::OptimizeAutoreleaseRVCall(Function &F, Instruction *AutoreleaseRV,
1253 InstructionClass &Class) {
John McCalld935e9c2011-06-15 23:37:01 +00001254 // Check for a return of the pointer value.
1255 const Value *Ptr = GetObjCArg(AutoreleaseRV);
Dan Gohman10a18d52011-08-12 00:36:31 +00001256 SmallVector<const Value *, 2> Users;
1257 Users.push_back(Ptr);
1258 do {
1259 Ptr = Users.pop_back_val();
Chandler Carruthcdf47882014-03-09 03:16:01 +00001260 for (const User *U : Ptr->users()) {
1261 if (isa<ReturnInst>(U) || GetBasicInstructionClass(U) == IC_RetainRV)
Dan Gohman10a18d52011-08-12 00:36:31 +00001262 return;
Chandler Carruthcdf47882014-03-09 03:16:01 +00001263 if (isa<BitCastInst>(U))
1264 Users.push_back(U);
Dan Gohman10a18d52011-08-12 00:36:31 +00001265 }
1266 } while (!Users.empty());
John McCalld935e9c2011-06-15 23:37:01 +00001267
1268 Changed = true;
1269 ++NumPeeps;
Michael Gottesman1bf69082013-01-06 21:07:11 +00001270
Michael Gottesman89279f82013-04-05 18:10:41 +00001271 DEBUG(dbgs() << "Transforming objc_autoreleaseReturnValue => "
Michael Gottesman1bf69082013-01-06 21:07:11 +00001272 "objc_autorelease since its operand is not used as a return "
1273 "value.\n"
Michael Gottesman89279f82013-04-05 18:10:41 +00001274 "Old = " << *AutoreleaseRV << "\n");
Michael Gottesman1bf69082013-01-06 21:07:11 +00001275
Michael Gottesmanc9656fa2013-01-12 01:25:15 +00001276 CallInst *AutoreleaseRVCI = cast<CallInst>(AutoreleaseRV);
Michael Gottesman14acfac2013-07-06 01:39:23 +00001277 Constant *NewDecl = EP.get(ARCRuntimeEntryPoints::EPT_Autorelease);
1278 AutoreleaseRVCI->setCalledFunction(NewDecl);
Michael Gottesmanc9656fa2013-01-12 01:25:15 +00001279 AutoreleaseRVCI->setTailCall(false); // Never tail call objc_autorelease.
Michael Gottesman556ff612013-01-12 01:25:19 +00001280 Class = IC_Autorelease;
Michael Gottesman10426b52013-01-07 21:26:07 +00001281
Michael Gottesman89279f82013-04-05 18:10:41 +00001282 DEBUG(dbgs() << "New: " << *AutoreleaseRV << "\n");
Michael Gottesman10426b52013-01-07 21:26:07 +00001283
John McCalld935e9c2011-06-15 23:37:01 +00001284}
1285
Michael Gottesman97e3df02013-01-14 00:35:14 +00001286/// Visit each call, one at a time, and make simplifications without doing any
1287/// additional analysis.
John McCalld935e9c2011-06-15 23:37:01 +00001288void ObjCARCOpt::OptimizeIndividualCalls(Function &F) {
Michael Gottesman89279f82013-04-05 18:10:41 +00001289 DEBUG(dbgs() << "\n== ObjCARCOpt::OptimizeIndividualCalls ==\n");
John McCalld935e9c2011-06-15 23:37:01 +00001290 // Reset all the flags in preparation for recomputing them.
1291 UsedInThisFunction = 0;
1292
1293 // Visit all objc_* calls in F.
1294 for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
1295 Instruction *Inst = &*I++;
Michael Gottesman3f146e22013-01-01 16:05:48 +00001296
John McCalld935e9c2011-06-15 23:37:01 +00001297 InstructionClass Class = GetBasicInstructionClass(Inst);
1298
Michael Gottesman89279f82013-04-05 18:10:41 +00001299 DEBUG(dbgs() << "Visiting: Class: " << Class << "; " << *Inst << "\n");
Michael Gottesman782e3442013-01-17 18:32:34 +00001300
John McCalld935e9c2011-06-15 23:37:01 +00001301 switch (Class) {
1302 default: break;
1303
1304 // Delete no-op casts. These function calls have special semantics, but
1305 // the semantics are entirely implemented via lowering in the front-end,
1306 // so by the time they reach the optimizer, they are just no-op calls
1307 // which return their argument.
1308 //
1309 // There are gray areas here, as the ability to cast reference-counted
1310 // pointers to raw void* and back allows code to break ARC assumptions,
1311 // however these are currently considered to be unimportant.
1312 case IC_NoopCast:
1313 Changed = true;
1314 ++NumNoops;
Michael Gottesman89279f82013-04-05 18:10:41 +00001315 DEBUG(dbgs() << "Erasing no-op cast: " << *Inst << "\n");
John McCalld935e9c2011-06-15 23:37:01 +00001316 EraseInstruction(Inst);
1317 continue;
1318
1319 // If the pointer-to-weak-pointer is null, it's undefined behavior.
1320 case IC_StoreWeak:
1321 case IC_LoadWeak:
1322 case IC_LoadWeakRetained:
1323 case IC_InitWeak:
1324 case IC_DestroyWeak: {
1325 CallInst *CI = cast<CallInst>(Inst);
Michael Gottesman65c24812013-03-25 09:27:43 +00001326 if (IsNullOrUndef(CI->getArgOperand(0))) {
Dan Gohman670f9372012-04-13 18:57:48 +00001327 Changed = true;
Chris Lattner229907c2011-07-18 04:54:35 +00001328 Type *Ty = CI->getArgOperand(0)->getType();
John McCalld935e9c2011-06-15 23:37:01 +00001329 new StoreInst(UndefValue::get(cast<PointerType>(Ty)->getElementType()),
1330 Constant::getNullValue(Ty),
1331 CI);
Michael Gottesman10426b52013-01-07 21:26:07 +00001332 llvm::Value *NewValue = UndefValue::get(CI->getType());
Michael Gottesman89279f82013-04-05 18:10:41 +00001333 DEBUG(dbgs() << "A null pointer-to-weak-pointer is undefined behavior."
1334 "\nOld = " << *CI << "\nNew = " << *NewValue << "\n");
Michael Gottesmanfec61c02013-01-06 21:54:30 +00001335 CI->replaceAllUsesWith(NewValue);
John McCalld935e9c2011-06-15 23:37:01 +00001336 CI->eraseFromParent();
1337 continue;
1338 }
1339 break;
1340 }
1341 case IC_CopyWeak:
1342 case IC_MoveWeak: {
1343 CallInst *CI = cast<CallInst>(Inst);
Michael Gottesman65c24812013-03-25 09:27:43 +00001344 if (IsNullOrUndef(CI->getArgOperand(0)) ||
1345 IsNullOrUndef(CI->getArgOperand(1))) {
Dan Gohman670f9372012-04-13 18:57:48 +00001346 Changed = true;
Chris Lattner229907c2011-07-18 04:54:35 +00001347 Type *Ty = CI->getArgOperand(0)->getType();
John McCalld935e9c2011-06-15 23:37:01 +00001348 new StoreInst(UndefValue::get(cast<PointerType>(Ty)->getElementType()),
1349 Constant::getNullValue(Ty),
1350 CI);
Michael Gottesmanfec61c02013-01-06 21:54:30 +00001351
1352 llvm::Value *NewValue = UndefValue::get(CI->getType());
Michael Gottesman89279f82013-04-05 18:10:41 +00001353 DEBUG(dbgs() << "A null pointer-to-weak-pointer is undefined behavior."
1354 "\nOld = " << *CI << "\nNew = " << *NewValue << "\n");
Michael Gottesman10426b52013-01-07 21:26:07 +00001355
Michael Gottesmanfec61c02013-01-06 21:54:30 +00001356 CI->replaceAllUsesWith(NewValue);
John McCalld935e9c2011-06-15 23:37:01 +00001357 CI->eraseFromParent();
1358 continue;
1359 }
1360 break;
1361 }
John McCalld935e9c2011-06-15 23:37:01 +00001362 case IC_RetainRV:
1363 if (OptimizeRetainRVCall(F, Inst))
1364 continue;
1365 break;
1366 case IC_AutoreleaseRV:
Michael Gottesman556ff612013-01-12 01:25:19 +00001367 OptimizeAutoreleaseRVCall(F, Inst, Class);
John McCalld935e9c2011-06-15 23:37:01 +00001368 break;
1369 }
1370
Michael Gottesmanb8c88362013-04-03 02:57:24 +00001371 // objc_autorelease(x) -> objc_release(x) if x is otherwise unused.
John McCalld935e9c2011-06-15 23:37:01 +00001372 if (IsAutorelease(Class) && Inst->use_empty()) {
1373 CallInst *Call = cast<CallInst>(Inst);
1374 const Value *Arg = Call->getArgOperand(0);
1375 Arg = FindSingleUseIdentifiedObject(Arg);
1376 if (Arg) {
1377 Changed = true;
1378 ++NumAutoreleases;
1379
1380 // Create the declaration lazily.
1381 LLVMContext &C = Inst->getContext();
Michael Gottesman01df4502013-07-06 01:41:35 +00001382
Michael Gottesman14acfac2013-07-06 01:39:23 +00001383 Constant *Decl = EP.get(ARCRuntimeEntryPoints::EPT_Release);
1384 CallInst *NewCall = CallInst::Create(Decl, Call->getArgOperand(0), "",
1385 Call);
Dmitri Gribenko3238fb72013-05-05 00:40:33 +00001386 NewCall->setMetadata(ImpreciseReleaseMDKind, MDNode::get(C, None));
Michael Gottesman10426b52013-01-07 21:26:07 +00001387
Michael Gottesman89279f82013-04-05 18:10:41 +00001388 DEBUG(dbgs() << "Replacing autorelease{,RV}(x) with objc_release(x) "
1389 "since x is otherwise unused.\nOld: " << *Call << "\nNew: "
1390 << *NewCall << "\n");
Michael Gottesman10426b52013-01-07 21:26:07 +00001391
John McCalld935e9c2011-06-15 23:37:01 +00001392 EraseInstruction(Call);
1393 Inst = NewCall;
1394 Class = IC_Release;
1395 }
1396 }
1397
1398 // For functions which can never be passed stack arguments, add
1399 // a tail keyword.
1400 if (IsAlwaysTail(Class)) {
1401 Changed = true;
Michael Gottesman89279f82013-04-05 18:10:41 +00001402 DEBUG(dbgs() << "Adding tail keyword to function since it can never be "
1403 "passed stack args: " << *Inst << "\n");
John McCalld935e9c2011-06-15 23:37:01 +00001404 cast<CallInst>(Inst)->setTailCall();
1405 }
1406
Michael Gottesmanc9656fa2013-01-12 01:25:15 +00001407 // Ensure that functions that can never have a "tail" keyword due to the
1408 // semantics of ARC truly do not do so.
1409 if (IsNeverTail(Class)) {
1410 Changed = true;
Michael Gottesman89279f82013-04-05 18:10:41 +00001411 DEBUG(dbgs() << "Removing tail keyword from function: " << *Inst <<
Michael Gottesmanc9656fa2013-01-12 01:25:15 +00001412 "\n");
1413 cast<CallInst>(Inst)->setTailCall(false);
1414 }
1415
John McCalld935e9c2011-06-15 23:37:01 +00001416 // Set nounwind as needed.
1417 if (IsNoThrow(Class)) {
1418 Changed = true;
Michael Gottesman89279f82013-04-05 18:10:41 +00001419 DEBUG(dbgs() << "Found no throw class. Setting nounwind on: " << *Inst
1420 << "\n");
John McCalld935e9c2011-06-15 23:37:01 +00001421 cast<CallInst>(Inst)->setDoesNotThrow();
1422 }
1423
1424 if (!IsNoopOnNull(Class)) {
1425 UsedInThisFunction |= 1 << Class;
1426 continue;
1427 }
1428
1429 const Value *Arg = GetObjCArg(Inst);
1430
1431 // ARC calls with null are no-ops. Delete them.
Michael Gottesman65c24812013-03-25 09:27:43 +00001432 if (IsNullOrUndef(Arg)) {
John McCalld935e9c2011-06-15 23:37:01 +00001433 Changed = true;
1434 ++NumNoops;
Michael Gottesman89279f82013-04-05 18:10:41 +00001435 DEBUG(dbgs() << "ARC calls with null are no-ops. Erasing: " << *Inst
1436 << "\n");
John McCalld935e9c2011-06-15 23:37:01 +00001437 EraseInstruction(Inst);
1438 continue;
1439 }
1440
1441 // Keep track of which of retain, release, autorelease, and retain_block
1442 // are actually present in this function.
1443 UsedInThisFunction |= 1 << Class;
1444
1445 // If Arg is a PHI, and one or more incoming values to the
1446 // PHI are null, and the call is control-equivalent to the PHI, and there
1447 // are no relevant side effects between the PHI and the call, the call
1448 // could be pushed up to just those paths with non-null incoming values.
1449 // For now, don't bother splitting critical edges for this.
1450 SmallVector<std::pair<Instruction *, const Value *>, 4> Worklist;
1451 Worklist.push_back(std::make_pair(Inst, Arg));
1452 do {
1453 std::pair<Instruction *, const Value *> Pair = Worklist.pop_back_val();
1454 Inst = Pair.first;
1455 Arg = Pair.second;
1456
1457 const PHINode *PN = dyn_cast<PHINode>(Arg);
1458 if (!PN) continue;
1459
1460 // Determine if the PHI has any null operands, or any incoming
1461 // critical edges.
1462 bool HasNull = false;
1463 bool HasCriticalEdges = false;
1464 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
1465 Value *Incoming =
1466 StripPointerCastsAndObjCCalls(PN->getIncomingValue(i));
Michael Gottesman65c24812013-03-25 09:27:43 +00001467 if (IsNullOrUndef(Incoming))
John McCalld935e9c2011-06-15 23:37:01 +00001468 HasNull = true;
1469 else if (cast<TerminatorInst>(PN->getIncomingBlock(i)->back())
1470 .getNumSuccessors() != 1) {
1471 HasCriticalEdges = true;
1472 break;
1473 }
1474 }
1475 // If we have null operands and no critical edges, optimize.
1476 if (!HasCriticalEdges && HasNull) {
1477 SmallPtrSet<Instruction *, 4> DependingInstructions;
1478 SmallPtrSet<const BasicBlock *, 4> Visited;
1479
1480 // Check that there is nothing that cares about the reference
1481 // count between the call and the phi.
Dan Gohman8478d762012-04-13 00:59:57 +00001482 switch (Class) {
1483 case IC_Retain:
1484 case IC_RetainBlock:
1485 // These can always be moved up.
1486 break;
1487 case IC_Release:
Dan Gohman41375a32012-05-08 23:39:44 +00001488 // These can't be moved across things that care about the retain
1489 // count.
Dan Gohman8478d762012-04-13 00:59:57 +00001490 FindDependencies(NeedsPositiveRetainCount, Arg,
1491 Inst->getParent(), Inst,
1492 DependingInstructions, Visited, PA);
1493 break;
1494 case IC_Autorelease:
1495 // These can't be moved across autorelease pool scope boundaries.
1496 FindDependencies(AutoreleasePoolBoundary, Arg,
1497 Inst->getParent(), Inst,
1498 DependingInstructions, Visited, PA);
1499 break;
1500 case IC_RetainRV:
1501 case IC_AutoreleaseRV:
1502 // Don't move these; the RV optimization depends on the autoreleaseRV
1503 // being tail called, and the retainRV being immediately after a call
1504 // (which might still happen if we get lucky with codegen layout, but
1505 // it's not worth taking the chance).
1506 continue;
1507 default:
1508 llvm_unreachable("Invalid dependence flavor");
1509 }
1510
John McCalld935e9c2011-06-15 23:37:01 +00001511 if (DependingInstructions.size() == 1 &&
1512 *DependingInstructions.begin() == PN) {
1513 Changed = true;
1514 ++NumPartialNoops;
1515 // Clone the call into each predecessor that has a non-null value.
1516 CallInst *CInst = cast<CallInst>(Inst);
Chris Lattner229907c2011-07-18 04:54:35 +00001517 Type *ParamTy = CInst->getArgOperand(0)->getType();
John McCalld935e9c2011-06-15 23:37:01 +00001518 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
1519 Value *Incoming =
1520 StripPointerCastsAndObjCCalls(PN->getIncomingValue(i));
Michael Gottesman65c24812013-03-25 09:27:43 +00001521 if (!IsNullOrUndef(Incoming)) {
John McCalld935e9c2011-06-15 23:37:01 +00001522 CallInst *Clone = cast<CallInst>(CInst->clone());
1523 Value *Op = PN->getIncomingValue(i);
1524 Instruction *InsertPos = &PN->getIncomingBlock(i)->back();
1525 if (Op->getType() != ParamTy)
1526 Op = new BitCastInst(Op, ParamTy, "", InsertPos);
1527 Clone->setArgOperand(0, Op);
1528 Clone->insertBefore(InsertPos);
Michael Gottesmanc189a392013-01-09 19:23:24 +00001529
Michael Gottesman89279f82013-04-05 18:10:41 +00001530 DEBUG(dbgs() << "Cloning "
Michael Gottesmanc189a392013-01-09 19:23:24 +00001531 << *CInst << "\n"
Michael Gottesman89279f82013-04-05 18:10:41 +00001532 "And inserting clone at " << *InsertPos << "\n");
John McCalld935e9c2011-06-15 23:37:01 +00001533 Worklist.push_back(std::make_pair(Clone, Incoming));
1534 }
1535 }
1536 // Erase the original call.
Michael Gottesmanc189a392013-01-09 19:23:24 +00001537 DEBUG(dbgs() << "Erasing: " << *CInst << "\n");
John McCalld935e9c2011-06-15 23:37:01 +00001538 EraseInstruction(CInst);
1539 continue;
1540 }
1541 }
1542 } while (!Worklist.empty());
1543 }
1544}
1545
Michael Gottesman323964c2013-04-18 05:39:45 +00001546/// If we have a top down pointer in the S_Use state, make sure that there are
1547/// no CFG hazards by checking the states of various bottom up pointers.
1548static void CheckForUseCFGHazard(const Sequence SuccSSeq,
1549 const bool SuccSRRIKnownSafe,
1550 PtrState &S,
1551 bool &SomeSuccHasSame,
1552 bool &AllSuccsHaveSame,
Michael Gottesmane67f40c2013-05-24 20:44:05 +00001553 bool &NotAllSeqEqualButKnownSafe,
Michael Gottesman323964c2013-04-18 05:39:45 +00001554 bool &ShouldContinue) {
1555 switch (SuccSSeq) {
1556 case S_CanRelease: {
Michael Gottesman93132252013-06-21 06:59:02 +00001557 if (!S.IsKnownSafe() && !SuccSRRIKnownSafe) {
Michael Gottesman323964c2013-04-18 05:39:45 +00001558 S.ClearSequenceProgress();
1559 break;
1560 }
Michael Gottesman2f294592013-06-21 19:12:36 +00001561 S.SetCFGHazardAfflicted(true);
Michael Gottesman323964c2013-04-18 05:39:45 +00001562 ShouldContinue = true;
1563 break;
1564 }
1565 case S_Use:
1566 SomeSuccHasSame = true;
1567 break;
1568 case S_Stop:
1569 case S_Release:
1570 case S_MovableRelease:
Michael Gottesman93132252013-06-21 06:59:02 +00001571 if (!S.IsKnownSafe() && !SuccSRRIKnownSafe)
Michael Gottesman323964c2013-04-18 05:39:45 +00001572 AllSuccsHaveSame = false;
Michael Gottesmane67f40c2013-05-24 20:44:05 +00001573 else
1574 NotAllSeqEqualButKnownSafe = true;
Michael Gottesman323964c2013-04-18 05:39:45 +00001575 break;
1576 case S_Retain:
1577 llvm_unreachable("bottom-up pointer in retain state!");
1578 case S_None:
1579 llvm_unreachable("This should have been handled earlier.");
1580 }
1581}
1582
1583/// If we have a Top Down pointer in the S_CanRelease state, make sure that
1584/// there are no CFG hazards by checking the states of various bottom up
1585/// pointers.
1586static void CheckForCanReleaseCFGHazard(const Sequence SuccSSeq,
1587 const bool SuccSRRIKnownSafe,
1588 PtrState &S,
1589 bool &SomeSuccHasSame,
Michael Gottesmane67f40c2013-05-24 20:44:05 +00001590 bool &AllSuccsHaveSame,
1591 bool &NotAllSeqEqualButKnownSafe) {
Michael Gottesman323964c2013-04-18 05:39:45 +00001592 switch (SuccSSeq) {
1593 case S_CanRelease:
1594 SomeSuccHasSame = true;
1595 break;
1596 case S_Stop:
1597 case S_Release:
1598 case S_MovableRelease:
1599 case S_Use:
Michael Gottesman93132252013-06-21 06:59:02 +00001600 if (!S.IsKnownSafe() && !SuccSRRIKnownSafe)
Michael Gottesman323964c2013-04-18 05:39:45 +00001601 AllSuccsHaveSame = false;
Michael Gottesmane67f40c2013-05-24 20:44:05 +00001602 else
1603 NotAllSeqEqualButKnownSafe = true;
Michael Gottesman323964c2013-04-18 05:39:45 +00001604 break;
1605 case S_Retain:
1606 llvm_unreachable("bottom-up pointer in retain state!");
1607 case S_None:
1608 llvm_unreachable("This should have been handled earlier.");
1609 }
1610}
1611
Michael Gottesman97e3df02013-01-14 00:35:14 +00001612/// Check for critical edges, loop boundaries, irreducible control flow, or
1613/// other CFG structures where moving code across the edge would result in it
1614/// being executed more.
John McCalld935e9c2011-06-15 23:37:01 +00001615void
1616ObjCARCOpt::CheckForCFGHazards(const BasicBlock *BB,
1617 DenseMap<const BasicBlock *, BBState> &BBStates,
1618 BBState &MyStates) const {
1619 // If any top-down local-use or possible-dec has a succ which is earlier in
1620 // the sequence, forget it.
Dan Gohman55b06742012-03-02 01:13:53 +00001621 for (BBState::ptr_iterator I = MyStates.top_down_ptr_begin(),
Michael Gottesman323964c2013-04-18 05:39:45 +00001622 E = MyStates.top_down_ptr_end(); I != E; ++I) {
1623 PtrState &S = I->second;
1624 const Sequence Seq = I->second.GetSeq();
Dan Gohman0155f302012-02-17 18:59:53 +00001625
Michael Gottesman323964c2013-04-18 05:39:45 +00001626 // We only care about S_Retain, S_CanRelease, and S_Use.
1627 if (Seq == S_None)
1628 continue;
Dan Gohman0155f302012-02-17 18:59:53 +00001629
Michael Gottesman323964c2013-04-18 05:39:45 +00001630 // Make sure that if extra top down states are added in the future that this
1631 // code is updated to handle it.
1632 assert((Seq == S_Retain || Seq == S_CanRelease || Seq == S_Use) &&
1633 "Unknown top down sequence state.");
1634
1635 const Value *Arg = I->first;
1636 const TerminatorInst *TI = cast<TerminatorInst>(&BB->back());
1637 bool SomeSuccHasSame = false;
1638 bool AllSuccsHaveSame = true;
Michael Gottesmane67f40c2013-05-24 20:44:05 +00001639 bool NotAllSeqEqualButKnownSafe = false;
Michael Gottesman323964c2013-04-18 05:39:45 +00001640
1641 succ_const_iterator SI(TI), SE(TI, false);
1642
1643 for (; SI != SE; ++SI) {
1644 // If VisitBottomUp has pointer information for this successor, take
1645 // what we know about it.
1646 const DenseMap<const BasicBlock *, BBState>::iterator BBI =
1647 BBStates.find(*SI);
1648 assert(BBI != BBStates.end());
1649 const PtrState &SuccS = BBI->second.getPtrBottomUpState(Arg);
1650 const Sequence SuccSSeq = SuccS.GetSeq();
1651
1652 // If bottom up, the pointer is in an S_None state, clear the sequence
1653 // progress since the sequence in the bottom up state finished
1654 // suggesting a mismatch in between retains/releases. This is true for
1655 // all three cases that we are handling here: S_Retain, S_Use, and
1656 // S_CanRelease.
1657 if (SuccSSeq == S_None) {
Dan Gohman12130272011-08-12 00:26:31 +00001658 S.ClearSequenceProgress();
Michael Gottesman323964c2013-04-18 05:39:45 +00001659 continue;
1660 }
1661
1662 // If we have S_Use or S_CanRelease, perform our check for cfg hazard
1663 // checks.
Michael Gottesman93132252013-06-21 06:59:02 +00001664 const bool SuccSRRIKnownSafe = SuccS.IsKnownSafe();
Michael Gottesman323964c2013-04-18 05:39:45 +00001665
1666 // *NOTE* We do not use Seq from above here since we are allowing for
1667 // S.GetSeq() to change while we are visiting basic blocks.
1668 switch(S.GetSeq()) {
1669 case S_Use: {
1670 bool ShouldContinue = false;
Michael Gottesmane67f40c2013-05-24 20:44:05 +00001671 CheckForUseCFGHazard(SuccSSeq, SuccSRRIKnownSafe, S, SomeSuccHasSame,
1672 AllSuccsHaveSame, NotAllSeqEqualButKnownSafe,
Michael Gottesman323964c2013-04-18 05:39:45 +00001673 ShouldContinue);
1674 if (ShouldContinue)
1675 continue;
1676 break;
1677 }
1678 case S_CanRelease: {
Michael Gottesmane67f40c2013-05-24 20:44:05 +00001679 CheckForCanReleaseCFGHazard(SuccSSeq, SuccSRRIKnownSafe, S,
1680 SomeSuccHasSame, AllSuccsHaveSame,
1681 NotAllSeqEqualButKnownSafe);
Michael Gottesman323964c2013-04-18 05:39:45 +00001682 break;
1683 }
1684 case S_Retain:
1685 case S_None:
1686 case S_Stop:
1687 case S_Release:
1688 case S_MovableRelease:
1689 break;
1690 }
John McCalld935e9c2011-06-15 23:37:01 +00001691 }
Michael Gottesman323964c2013-04-18 05:39:45 +00001692
1693 // If the state at the other end of any of the successor edges
1694 // matches the current state, require all edges to match. This
1695 // guards against loops in the middle of a sequence.
Michael Gottesmane67f40c2013-05-24 20:44:05 +00001696 if (SomeSuccHasSame && !AllSuccsHaveSame) {
Michael Gottesman323964c2013-04-18 05:39:45 +00001697 S.ClearSequenceProgress();
Michael Gottesmane67f40c2013-05-24 20:44:05 +00001698 } else if (NotAllSeqEqualButKnownSafe) {
1699 // If we would have cleared the state foregoing the fact that we are known
1700 // safe, stop code motion. This is because whether or not it is safe to
1701 // remove RR pairs via KnownSafe is an orthogonal concept to whether we
1702 // are allowed to perform code motion.
Michael Gottesman2f294592013-06-21 19:12:36 +00001703 S.SetCFGHazardAfflicted(true);
Michael Gottesmane67f40c2013-05-24 20:44:05 +00001704 }
Michael Gottesman323964c2013-04-18 05:39:45 +00001705 }
John McCalld935e9c2011-06-15 23:37:01 +00001706}
1707
1708bool
Dan Gohman817a7c62012-03-22 18:24:56 +00001709ObjCARCOpt::VisitInstructionBottomUp(Instruction *Inst,
Dan Gohman5c70fad2012-03-23 17:47:54 +00001710 BasicBlock *BB,
Dan Gohman817a7c62012-03-22 18:24:56 +00001711 MapVector<Value *, RRInfo> &Retains,
1712 BBState &MyStates) {
1713 bool NestingDetected = false;
1714 InstructionClass Class = GetInstructionClass(Inst);
Craig Topperf40110f2014-04-25 05:29:35 +00001715 const Value *Arg = nullptr;
Michael Gottesman79249972013-04-05 23:46:45 +00001716
Michael Gottesman1d8d2572013-04-05 22:54:28 +00001717 DEBUG(dbgs() << "Class: " << Class << "\n");
Michael Gottesman79249972013-04-05 23:46:45 +00001718
Dan Gohman817a7c62012-03-22 18:24:56 +00001719 switch (Class) {
1720 case IC_Release: {
1721 Arg = GetObjCArg(Inst);
1722
1723 PtrState &S = MyStates.getPtrBottomUpState(Arg);
1724
1725 // If we see two releases in a row on the same pointer. If so, make
1726 // a note, and we'll cicle back to revisit it after we've
1727 // hopefully eliminated the second release, which may allow us to
1728 // eliminate the first release too.
1729 // Theoretically we could implement removal of nested retain+release
1730 // pairs by making PtrState hold a stack of states, but this is
1731 // simple and avoids adding overhead for the non-nested case.
Michael Gottesmanaf2113f2013-01-13 07:00:51 +00001732 if (S.GetSeq() == S_Release || S.GetSeq() == S_MovableRelease) {
Michael Gottesman89279f82013-04-05 18:10:41 +00001733 DEBUG(dbgs() << "Found nested releases (i.e. a release pair)\n");
Dan Gohman817a7c62012-03-22 18:24:56 +00001734 NestingDetected = true;
Michael Gottesmanaf2113f2013-01-13 07:00:51 +00001735 }
Dan Gohman817a7c62012-03-22 18:24:56 +00001736
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00001737 MDNode *ReleaseMetadata = Inst->getMetadata(ImpreciseReleaseMDKind);
Michael Gottesman81b1d432013-03-26 00:42:04 +00001738 Sequence NewSeq = ReleaseMetadata ? S_MovableRelease : S_Release;
1739 ANNOTATE_BOTTOMUP(Inst, Arg, S.GetSeq(), NewSeq);
1740 S.ResetSequenceProgress(NewSeq);
Michael Gottesmanf701d3f2013-06-21 07:03:07 +00001741 S.SetReleaseMetadata(ReleaseMetadata);
Michael Gottesman93132252013-06-21 06:59:02 +00001742 S.SetKnownSafe(S.HasKnownPositiveRefCount());
Michael Gottesmanb82a1792013-06-21 07:00:44 +00001743 S.SetTailCallRelease(cast<CallInst>(Inst)->isTailCall());
Michael Gottesman4f6ef112013-06-21 19:44:27 +00001744 S.InsertCall(Inst);
Dan Gohmandf476e52012-09-04 23:16:20 +00001745 S.SetKnownPositiveRefCount();
Dan Gohman817a7c62012-03-22 18:24:56 +00001746 break;
1747 }
1748 case IC_RetainBlock:
Michael Gottesman158fdf62013-03-28 20:11:19 +00001749 // In OptimizeIndividualCalls, we have strength reduced all optimizable
1750 // objc_retainBlocks to objc_retains. Thus at this point any
1751 // objc_retainBlocks that we see are not optimizable.
1752 break;
Dan Gohman817a7c62012-03-22 18:24:56 +00001753 case IC_Retain:
1754 case IC_RetainRV: {
1755 Arg = GetObjCArg(Inst);
1756
1757 PtrState &S = MyStates.getPtrBottomUpState(Arg);
Dan Gohman62079b42012-04-25 00:50:46 +00001758 S.SetKnownPositiveRefCount();
Dan Gohman817a7c62012-03-22 18:24:56 +00001759
Michael Gottesman81b1d432013-03-26 00:42:04 +00001760 Sequence OldSeq = S.GetSeq();
1761 switch (OldSeq) {
Dan Gohman817a7c62012-03-22 18:24:56 +00001762 case S_Stop:
1763 case S_Release:
1764 case S_MovableRelease:
1765 case S_Use:
Michael Gottesman1d8d2572013-04-05 22:54:28 +00001766 // If OldSeq is not S_Use or OldSeq is S_Use and we are tracking an
1767 // imprecise release, clear our reverse insertion points.
Michael Gottesmanf0401182013-06-21 19:12:38 +00001768 if (OldSeq != S_Use || S.IsTrackingImpreciseReleases())
Michael Gottesman4f6ef112013-06-21 19:44:27 +00001769 S.ClearReverseInsertPts();
Dan Gohman817a7c62012-03-22 18:24:56 +00001770 // FALL THROUGH
1771 case S_CanRelease:
1772 // Don't do retain+release tracking for IC_RetainRV, because it's
1773 // better to let it remain as the first instruction after a call.
Michael Gottesmanba648592013-03-28 23:08:44 +00001774 if (Class != IC_RetainRV)
Michael Gottesmane3943d02013-06-21 19:44:30 +00001775 Retains[Inst] = S.GetRRInfo();
Dan Gohman817a7c62012-03-22 18:24:56 +00001776 S.ClearSequenceProgress();
1777 break;
1778 case S_None:
1779 break;
1780 case S_Retain:
1781 llvm_unreachable("bottom-up pointer in retain state!");
1782 }
Michael Gottesman79249972013-04-05 23:46:45 +00001783 ANNOTATE_BOTTOMUP(Inst, Arg, OldSeq, S.GetSeq());
Michael Gottesman31ba23a2013-04-05 22:54:32 +00001784 // A retain moving bottom up can be a use.
1785 break;
Dan Gohman817a7c62012-03-22 18:24:56 +00001786 }
1787 case IC_AutoreleasepoolPop:
1788 // Conservatively, clear MyStates for all known pointers.
1789 MyStates.clearBottomUpPointers();
1790 return NestingDetected;
1791 case IC_AutoreleasepoolPush:
1792 case IC_None:
1793 // These are irrelevant.
1794 return NestingDetected;
Michael Gottesmana76143ee2013-05-13 23:49:42 +00001795 case IC_User:
1796 // If we have a store into an alloca of a pointer we are tracking, the
1797 // pointer has multiple owners implying that we must be more conservative.
1798 //
1799 // This comes up in the context of a pointer being ``KnownSafe''. In the
Alp Tokercb402912014-01-24 17:20:08 +00001800 // presence of a block being initialized, the frontend will emit the
Michael Gottesmana76143ee2013-05-13 23:49:42 +00001801 // objc_retain on the original pointer and the release on the pointer loaded
1802 // from the alloca. The optimizer will through the provenance analysis
1803 // realize that the two are related, but since we only require KnownSafe in
1804 // one direction, will match the inner retain on the original pointer with
1805 // the guard release on the original pointer. This is fixed by ensuring that
Alp Tokercb402912014-01-24 17:20:08 +00001806 // in the presence of allocas we only unconditionally remove pointers if
Michael Gottesmana76143ee2013-05-13 23:49:42 +00001807 // both our retain and our release are KnownSafe.
1808 if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
1809 if (AreAnyUnderlyingObjectsAnAlloca(SI->getPointerOperand())) {
1810 BBState::ptr_iterator I = MyStates.findPtrBottomUpState(
1811 StripPointerCastsAndObjCCalls(SI->getValueOperand()));
1812 if (I != MyStates.bottom_up_ptr_end())
Michael Gottesman5a91bbf2013-05-24 20:44:02 +00001813 MultiOwnersSet.insert(I->first);
Michael Gottesmana76143ee2013-05-13 23:49:42 +00001814 }
1815 }
1816 break;
Dan Gohman817a7c62012-03-22 18:24:56 +00001817 default:
1818 break;
1819 }
1820
1821 // Consider any other possible effects of this instruction on each
1822 // pointer being tracked.
1823 for (BBState::ptr_iterator MI = MyStates.bottom_up_ptr_begin(),
1824 ME = MyStates.bottom_up_ptr_end(); MI != ME; ++MI) {
1825 const Value *Ptr = MI->first;
1826 if (Ptr == Arg)
1827 continue; // Handled above.
1828 PtrState &S = MI->second;
1829 Sequence Seq = S.GetSeq();
1830
1831 // Check for possible releases.
1832 if (CanAlterRefCount(Inst, Ptr, PA, Class)) {
Michael Gottesman89279f82013-04-05 18:10:41 +00001833 DEBUG(dbgs() << "CanAlterRefCount: Seq: " << Seq << "; " << *Ptr
1834 << "\n");
Michael Gottesman764b1cf2013-03-23 05:46:19 +00001835 S.ClearKnownPositiveRefCount();
Dan Gohman817a7c62012-03-22 18:24:56 +00001836 switch (Seq) {
1837 case S_Use:
1838 S.SetSeq(S_CanRelease);
Michael Gottesman81b1d432013-03-26 00:42:04 +00001839 ANNOTATE_BOTTOMUP(Inst, Ptr, Seq, S.GetSeq());
Dan Gohman817a7c62012-03-22 18:24:56 +00001840 continue;
1841 case S_CanRelease:
1842 case S_Release:
1843 case S_MovableRelease:
1844 case S_Stop:
1845 case S_None:
1846 break;
1847 case S_Retain:
1848 llvm_unreachable("bottom-up pointer in retain state!");
1849 }
1850 }
1851
1852 // Check for possible direct uses.
1853 switch (Seq) {
1854 case S_Release:
1855 case S_MovableRelease:
1856 if (CanUse(Inst, Ptr, PA, Class)) {
Michael Gottesman89279f82013-04-05 18:10:41 +00001857 DEBUG(dbgs() << "CanUse: Seq: " << Seq << "; " << *Ptr
1858 << "\n");
Michael Gottesman4f6ef112013-06-21 19:44:27 +00001859 assert(!S.HasReverseInsertPts());
Dan Gohman5c70fad2012-03-23 17:47:54 +00001860 // If this is an invoke instruction, we're scanning it as part of
1861 // one of its successor blocks, since we can't insert code after it
1862 // in its own block, and we don't want to split critical edges.
1863 if (isa<InvokeInst>(Inst))
Michael Gottesman4f6ef112013-06-21 19:44:27 +00001864 S.InsertReverseInsertPt(BB->getFirstInsertionPt());
Dan Gohman5c70fad2012-03-23 17:47:54 +00001865 else
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001866 S.InsertReverseInsertPt(std::next(BasicBlock::iterator(Inst)));
Dan Gohman817a7c62012-03-22 18:24:56 +00001867 S.SetSeq(S_Use);
Michael Gottesman81b1d432013-03-26 00:42:04 +00001868 ANNOTATE_BOTTOMUP(Inst, Ptr, Seq, S_Use);
John McCall20182ac2013-03-22 21:38:36 +00001869 } else if (Seq == S_Release && IsUser(Class)) {
Michael Gottesman89279f82013-04-05 18:10:41 +00001870 DEBUG(dbgs() << "PreciseReleaseUse: Seq: " << Seq << "; " << *Ptr
1871 << "\n");
Dan Gohman817a7c62012-03-22 18:24:56 +00001872 // Non-movable releases depend on any possible objc pointer use.
1873 S.SetSeq(S_Stop);
Michael Gottesman81b1d432013-03-26 00:42:04 +00001874 ANNOTATE_BOTTOMUP(Inst, Ptr, S_Release, S_Stop);
Michael Gottesman4f6ef112013-06-21 19:44:27 +00001875 assert(!S.HasReverseInsertPts());
Dan Gohman5c70fad2012-03-23 17:47:54 +00001876 // As above; handle invoke specially.
1877 if (isa<InvokeInst>(Inst))
Michael Gottesman4f6ef112013-06-21 19:44:27 +00001878 S.InsertReverseInsertPt(BB->getFirstInsertionPt());
Dan Gohman5c70fad2012-03-23 17:47:54 +00001879 else
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001880 S.InsertReverseInsertPt(std::next(BasicBlock::iterator(Inst)));
Dan Gohman817a7c62012-03-22 18:24:56 +00001881 }
1882 break;
1883 case S_Stop:
Michael Gottesman81b1d432013-03-26 00:42:04 +00001884 if (CanUse(Inst, Ptr, PA, Class)) {
Michael Gottesman89279f82013-04-05 18:10:41 +00001885 DEBUG(dbgs() << "PreciseStopUse: Seq: " << Seq << "; " << *Ptr
1886 << "\n");
Dan Gohman817a7c62012-03-22 18:24:56 +00001887 S.SetSeq(S_Use);
Michael Gottesman81b1d432013-03-26 00:42:04 +00001888 ANNOTATE_BOTTOMUP(Inst, Ptr, Seq, S_Use);
1889 }
Dan Gohman817a7c62012-03-22 18:24:56 +00001890 break;
1891 case S_CanRelease:
1892 case S_Use:
1893 case S_None:
1894 break;
1895 case S_Retain:
1896 llvm_unreachable("bottom-up pointer in retain state!");
1897 }
1898 }
1899
1900 return NestingDetected;
1901}
1902
1903bool
John McCalld935e9c2011-06-15 23:37:01 +00001904ObjCARCOpt::VisitBottomUp(BasicBlock *BB,
1905 DenseMap<const BasicBlock *, BBState> &BBStates,
1906 MapVector<Value *, RRInfo> &Retains) {
Michael Gottesman89279f82013-04-05 18:10:41 +00001907
1908 DEBUG(dbgs() << "\n== ObjCARCOpt::VisitBottomUp ==\n");
Michael Gottesman79249972013-04-05 23:46:45 +00001909
John McCalld935e9c2011-06-15 23:37:01 +00001910 bool NestingDetected = false;
1911 BBState &MyStates = BBStates[BB];
1912
1913 // Merge the states from each successor to compute the initial state
1914 // for the current block.
Dan Gohman10c82ce2012-08-27 18:31:36 +00001915 BBState::edge_iterator SI(MyStates.succ_begin()),
1916 SE(MyStates.succ_end());
1917 if (SI != SE) {
Dan Gohmanc24c66f2012-04-24 22:53:18 +00001918 const BasicBlock *Succ = *SI;
1919 DenseMap<const BasicBlock *, BBState>::iterator I = BBStates.find(Succ);
1920 assert(I != BBStates.end());
1921 MyStates.InitFromSucc(I->second);
1922 ++SI;
1923 for (; SI != SE; ++SI) {
1924 Succ = *SI;
1925 I = BBStates.find(Succ);
1926 assert(I != BBStates.end());
1927 MyStates.MergeSucc(I->second);
1928 }
Michael Gottesman60f6b282013-03-29 05:13:07 +00001929 }
Michael Gottesmancd4de0f2013-03-26 00:42:09 +00001930
Michael Gottesman43e7e002013-04-03 22:41:59 +00001931 // If ARC Annotations are enabled, output the current state of pointers at the
Michael Gottesmanc2d5bf52013-04-03 23:07:45 +00001932 // bottom of the basic block.
Michael Gottesman43e7e002013-04-03 22:41:59 +00001933 ANNOTATE_BOTTOMUP_BBEND(MyStates, BB);
Michael Gottesmanc2d5bf52013-04-03 23:07:45 +00001934
John McCalld935e9c2011-06-15 23:37:01 +00001935 // Visit all the instructions, bottom-up.
1936 for (BasicBlock::iterator I = BB->end(), E = BB->begin(); I != E; --I) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00001937 Instruction *Inst = std::prev(I);
Dan Gohman5c70fad2012-03-23 17:47:54 +00001938
1939 // Invoke instructions are visited as part of their successors (below).
1940 if (isa<InvokeInst>(Inst))
1941 continue;
1942
Michael Gottesman89279f82013-04-05 18:10:41 +00001943 DEBUG(dbgs() << "Visiting " << *Inst << "\n");
Michael Gottesmanaf2113f2013-01-13 07:00:51 +00001944
Dan Gohman5c70fad2012-03-23 17:47:54 +00001945 NestingDetected |= VisitInstructionBottomUp(Inst, BB, Retains, MyStates);
1946 }
1947
Dan Gohmandae33492012-04-27 18:56:31 +00001948 // If there's a predecessor with an invoke, visit the invoke as if it were
1949 // part of this block, since we can't insert code after an invoke in its own
1950 // block, and we don't want to split critical edges.
Dan Gohmanc24c66f2012-04-24 22:53:18 +00001951 for (BBState::edge_iterator PI(MyStates.pred_begin()),
1952 PE(MyStates.pred_end()); PI != PE; ++PI) {
Dan Gohman5c70fad2012-03-23 17:47:54 +00001953 BasicBlock *Pred = *PI;
Dan Gohmandae33492012-04-27 18:56:31 +00001954 if (InvokeInst *II = dyn_cast<InvokeInst>(&Pred->back()))
1955 NestingDetected |= VisitInstructionBottomUp(II, BB, Retains, MyStates);
Dan Gohman817a7c62012-03-22 18:24:56 +00001956 }
John McCalld935e9c2011-06-15 23:37:01 +00001957
Michael Gottesman43e7e002013-04-03 22:41:59 +00001958 // If ARC Annotations are enabled, output the current state of pointers at the
1959 // top of the basic block.
1960 ANNOTATE_BOTTOMUP_BBSTART(MyStates, BB);
Michael Gottesmanc2d5bf52013-04-03 23:07:45 +00001961
Dan Gohman817a7c62012-03-22 18:24:56 +00001962 return NestingDetected;
1963}
John McCalld935e9c2011-06-15 23:37:01 +00001964
Dan Gohman817a7c62012-03-22 18:24:56 +00001965bool
1966ObjCARCOpt::VisitInstructionTopDown(Instruction *Inst,
1967 DenseMap<Value *, RRInfo> &Releases,
1968 BBState &MyStates) {
1969 bool NestingDetected = false;
1970 InstructionClass Class = GetInstructionClass(Inst);
Craig Topperf40110f2014-04-25 05:29:35 +00001971 const Value *Arg = nullptr;
John McCalld935e9c2011-06-15 23:37:01 +00001972
Dan Gohman817a7c62012-03-22 18:24:56 +00001973 switch (Class) {
1974 case IC_RetainBlock:
Michael Gottesman158fdf62013-03-28 20:11:19 +00001975 // In OptimizeIndividualCalls, we have strength reduced all optimizable
1976 // objc_retainBlocks to objc_retains. Thus at this point any
1977 // objc_retainBlocks that we see are not optimizable.
1978 break;
Dan Gohman817a7c62012-03-22 18:24:56 +00001979 case IC_Retain:
1980 case IC_RetainRV: {
1981 Arg = GetObjCArg(Inst);
1982
1983 PtrState &S = MyStates.getPtrTopDownState(Arg);
1984
1985 // Don't do retain+release tracking for IC_RetainRV, because it's
1986 // better to let it remain as the first instruction after a call.
1987 if (Class != IC_RetainRV) {
1988 // If we see two retains in a row on the same pointer. If so, make
John McCalld935e9c2011-06-15 23:37:01 +00001989 // a note, and we'll cicle back to revisit it after we've
Dan Gohman817a7c62012-03-22 18:24:56 +00001990 // hopefully eliminated the second retain, which may allow us to
1991 // eliminate the first retain too.
John McCalld935e9c2011-06-15 23:37:01 +00001992 // Theoretically we could implement removal of nested retain+release
1993 // pairs by making PtrState hold a stack of states, but this is
1994 // simple and avoids adding overhead for the non-nested case.
Dan Gohman817a7c62012-03-22 18:24:56 +00001995 if (S.GetSeq() == S_Retain)
John McCalld935e9c2011-06-15 23:37:01 +00001996 NestingDetected = true;
1997
Michael Gottesman81b1d432013-03-26 00:42:04 +00001998 ANNOTATE_TOPDOWN(Inst, Arg, S.GetSeq(), S_Retain);
Dan Gohman62079b42012-04-25 00:50:46 +00001999 S.ResetSequenceProgress(S_Retain);
Michael Gottesman93132252013-06-21 06:59:02 +00002000 S.SetKnownSafe(S.HasKnownPositiveRefCount());
Michael Gottesman4f6ef112013-06-21 19:44:27 +00002001 S.InsertCall(Inst);
John McCalld935e9c2011-06-15 23:37:01 +00002002 }
John McCalld935e9c2011-06-15 23:37:01 +00002003
Dan Gohmandf476e52012-09-04 23:16:20 +00002004 S.SetKnownPositiveRefCount();
Dan Gohmanf64ff8e2012-07-23 19:27:31 +00002005
2006 // A retain can be a potential use; procede to the generic checking
2007 // code below.
2008 break;
Dan Gohman817a7c62012-03-22 18:24:56 +00002009 }
2010 case IC_Release: {
2011 Arg = GetObjCArg(Inst);
2012
2013 PtrState &S = MyStates.getPtrTopDownState(Arg);
Michael Gottesman764b1cf2013-03-23 05:46:19 +00002014 S.ClearKnownPositiveRefCount();
Michael Gottesman79249972013-04-05 23:46:45 +00002015
Michael Gottesman1d8d2572013-04-05 22:54:28 +00002016 Sequence OldSeq = S.GetSeq();
Michael Gottesman79249972013-04-05 23:46:45 +00002017
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +00002018 MDNode *ReleaseMetadata = Inst->getMetadata(ImpreciseReleaseMDKind);
Michael Gottesman79249972013-04-05 23:46:45 +00002019
Michael Gottesman1d8d2572013-04-05 22:54:28 +00002020 switch (OldSeq) {
Dan Gohman817a7c62012-03-22 18:24:56 +00002021 case S_Retain:
2022 case S_CanRelease:
Craig Topperf40110f2014-04-25 05:29:35 +00002023 if (OldSeq == S_Retain || ReleaseMetadata != nullptr)
Michael Gottesman4f6ef112013-06-21 19:44:27 +00002024 S.ClearReverseInsertPts();
Dan Gohman817a7c62012-03-22 18:24:56 +00002025 // FALL THROUGH
2026 case S_Use:
Michael Gottesmanf701d3f2013-06-21 07:03:07 +00002027 S.SetReleaseMetadata(ReleaseMetadata);
Michael Gottesmanb82a1792013-06-21 07:00:44 +00002028 S.SetTailCallRelease(cast<CallInst>(Inst)->isTailCall());
Michael Gottesmane3943d02013-06-21 19:44:30 +00002029 Releases[Inst] = S.GetRRInfo();
Michael Gottesman81b1d432013-03-26 00:42:04 +00002030 ANNOTATE_TOPDOWN(Inst, Arg, S.GetSeq(), S_None);
Dan Gohman817a7c62012-03-22 18:24:56 +00002031 S.ClearSequenceProgress();
2032 break;
2033 case S_None:
2034 break;
2035 case S_Stop:
2036 case S_Release:
2037 case S_MovableRelease:
2038 llvm_unreachable("top-down pointer in release state!");
2039 }
2040 break;
2041 }
2042 case IC_AutoreleasepoolPop:
2043 // Conservatively, clear MyStates for all known pointers.
2044 MyStates.clearTopDownPointers();
2045 return NestingDetected;
2046 case IC_AutoreleasepoolPush:
2047 case IC_None:
2048 // These are irrelevant.
2049 return NestingDetected;
2050 default:
2051 break;
2052 }
2053
2054 // Consider any other possible effects of this instruction on each
2055 // pointer being tracked.
2056 for (BBState::ptr_iterator MI = MyStates.top_down_ptr_begin(),
2057 ME = MyStates.top_down_ptr_end(); MI != ME; ++MI) {
2058 const Value *Ptr = MI->first;
2059 if (Ptr == Arg)
2060 continue; // Handled above.
2061 PtrState &S = MI->second;
2062 Sequence Seq = S.GetSeq();
2063
2064 // Check for possible releases.
2065 if (CanAlterRefCount(Inst, Ptr, PA, Class)) {
Michael Gottesmanbab49e972013-04-05 18:26:08 +00002066 DEBUG(dbgs() << "CanAlterRefCount: Seq: " << Seq << "; " << *Ptr
Michael Gottesman79249972013-04-05 23:46:45 +00002067 << "\n");
Michael Gottesman764b1cf2013-03-23 05:46:19 +00002068 S.ClearKnownPositiveRefCount();
John McCalld935e9c2011-06-15 23:37:01 +00002069 switch (Seq) {
Dan Gohman817a7c62012-03-22 18:24:56 +00002070 case S_Retain:
2071 S.SetSeq(S_CanRelease);
Michael Gottesman81b1d432013-03-26 00:42:04 +00002072 ANNOTATE_TOPDOWN(Inst, Ptr, Seq, S_CanRelease);
Michael Gottesman4f6ef112013-06-21 19:44:27 +00002073 assert(!S.HasReverseInsertPts());
2074 S.InsertReverseInsertPt(Inst);
Dan Gohman817a7c62012-03-22 18:24:56 +00002075
2076 // One call can't cause a transition from S_Retain to S_CanRelease
2077 // and S_CanRelease to S_Use. If we've made the first transition,
2078 // we're done.
2079 continue;
John McCalld935e9c2011-06-15 23:37:01 +00002080 case S_Use:
Dan Gohman817a7c62012-03-22 18:24:56 +00002081 case S_CanRelease:
John McCalld935e9c2011-06-15 23:37:01 +00002082 case S_None:
2083 break;
Dan Gohman817a7c62012-03-22 18:24:56 +00002084 case S_Stop:
2085 case S_Release:
2086 case S_MovableRelease:
2087 llvm_unreachable("top-down pointer in release state!");
John McCalld935e9c2011-06-15 23:37:01 +00002088 }
2089 }
Dan Gohman817a7c62012-03-22 18:24:56 +00002090
2091 // Check for possible direct uses.
2092 switch (Seq) {
2093 case S_CanRelease:
Michael Gottesman81b1d432013-03-26 00:42:04 +00002094 if (CanUse(Inst, Ptr, PA, Class)) {
Michael Gottesmanbab49e972013-04-05 18:26:08 +00002095 DEBUG(dbgs() << "CanUse: Seq: " << Seq << "; " << *Ptr
2096 << "\n");
Dan Gohman817a7c62012-03-22 18:24:56 +00002097 S.SetSeq(S_Use);
Michael Gottesman81b1d432013-03-26 00:42:04 +00002098 ANNOTATE_TOPDOWN(Inst, Ptr, Seq, S_Use);
2099 }
Dan Gohman817a7c62012-03-22 18:24:56 +00002100 break;
2101 case S_Retain:
2102 case S_Use:
2103 case S_None:
2104 break;
2105 case S_Stop:
2106 case S_Release:
2107 case S_MovableRelease:
2108 llvm_unreachable("top-down pointer in release state!");
2109 }
John McCalld935e9c2011-06-15 23:37:01 +00002110 }
2111
2112 return NestingDetected;
2113}
2114
2115bool
2116ObjCARCOpt::VisitTopDown(BasicBlock *BB,
2117 DenseMap<const BasicBlock *, BBState> &BBStates,
2118 DenseMap<Value *, RRInfo> &Releases) {
Michael Gottesman89279f82013-04-05 18:10:41 +00002119 DEBUG(dbgs() << "\n== ObjCARCOpt::VisitTopDown ==\n");
John McCalld935e9c2011-06-15 23:37:01 +00002120 bool NestingDetected = false;
2121 BBState &MyStates = BBStates[BB];
2122
2123 // Merge the states from each predecessor to compute the initial state
2124 // for the current block.
Dan Gohman10c82ce2012-08-27 18:31:36 +00002125 BBState::edge_iterator PI(MyStates.pred_begin()),
2126 PE(MyStates.pred_end());
2127 if (PI != PE) {
Dan Gohmanc24c66f2012-04-24 22:53:18 +00002128 const BasicBlock *Pred = *PI;
2129 DenseMap<const BasicBlock *, BBState>::iterator I = BBStates.find(Pred);
2130 assert(I != BBStates.end());
2131 MyStates.InitFromPred(I->second);
2132 ++PI;
2133 for (; PI != PE; ++PI) {
2134 Pred = *PI;
2135 I = BBStates.find(Pred);
2136 assert(I != BBStates.end());
2137 MyStates.MergePred(I->second);
2138 }
Dan Gohmanc24c66f2012-04-24 22:53:18 +00002139 }
John McCalld935e9c2011-06-15 23:37:01 +00002140
Michael Gottesman43e7e002013-04-03 22:41:59 +00002141 // If ARC Annotations are enabled, output the current state of pointers at the
2142 // top of the basic block.
2143 ANNOTATE_TOPDOWN_BBSTART(MyStates, BB);
Michael Gottesmanc2d5bf52013-04-03 23:07:45 +00002144
John McCalld935e9c2011-06-15 23:37:01 +00002145 // Visit all the instructions, top-down.
2146 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
2147 Instruction *Inst = I;
Michael Gottesmanaf2113f2013-01-13 07:00:51 +00002148
Michael Gottesman89279f82013-04-05 18:10:41 +00002149 DEBUG(dbgs() << "Visiting " << *Inst << "\n");
Michael Gottesmanaf2113f2013-01-13 07:00:51 +00002150
Dan Gohman817a7c62012-03-22 18:24:56 +00002151 NestingDetected |= VisitInstructionTopDown(Inst, Releases, MyStates);
John McCalld935e9c2011-06-15 23:37:01 +00002152 }
Michael Gottesmanc2d5bf52013-04-03 23:07:45 +00002153
Michael Gottesman43e7e002013-04-03 22:41:59 +00002154 // If ARC Annotations are enabled, output the current state of pointers at the
2155 // bottom of the basic block.
2156 ANNOTATE_TOPDOWN_BBEND(MyStates, BB);
Michael Gottesmanc2d5bf52013-04-03 23:07:45 +00002157
Michael Gottesmanffef24f2013-04-17 20:48:01 +00002158#ifdef ARC_ANNOTATIONS
Michael Gottesmanadb921a2013-04-17 21:03:53 +00002159 if (!(EnableARCAnnotations && DisableCheckForCFGHazards))
Michael Gottesmanffef24f2013-04-17 20:48:01 +00002160#endif
John McCalld935e9c2011-06-15 23:37:01 +00002161 CheckForCFGHazards(BB, BBStates, MyStates);
2162 return NestingDetected;
2163}
2164
Dan Gohmana53a12c2011-12-12 19:42:25 +00002165static void
2166ComputePostOrders(Function &F,
2167 SmallVectorImpl<BasicBlock *> &PostOrder,
Dan Gohmanc24c66f2012-04-24 22:53:18 +00002168 SmallVectorImpl<BasicBlock *> &ReverseCFGPostOrder,
2169 unsigned NoObjCARCExceptionsMDKind,
2170 DenseMap<const BasicBlock *, BBState> &BBStates) {
Michael Gottesman97e3df02013-01-14 00:35:14 +00002171 /// The visited set, for doing DFS walks.
Dan Gohmana53a12c2011-12-12 19:42:25 +00002172 SmallPtrSet<BasicBlock *, 16> Visited;
2173
2174 // Do DFS, computing the PostOrder.
2175 SmallPtrSet<BasicBlock *, 16> OnStack;
2176 SmallVector<std::pair<BasicBlock *, succ_iterator>, 16> SuccStack;
Dan Gohmanc24c66f2012-04-24 22:53:18 +00002177
2178 // Functions always have exactly one entry block, and we don't have
2179 // any other block that we treat like an entry block.
Dan Gohmana53a12c2011-12-12 19:42:25 +00002180 BasicBlock *EntryBB = &F.getEntryBlock();
Dan Gohman41375a32012-05-08 23:39:44 +00002181 BBState &MyStates = BBStates[EntryBB];
2182 MyStates.SetAsEntry();
2183 TerminatorInst *EntryTI = cast<TerminatorInst>(&EntryBB->back());
2184 SuccStack.push_back(std::make_pair(EntryBB, succ_iterator(EntryTI)));
Dan Gohmana53a12c2011-12-12 19:42:25 +00002185 Visited.insert(EntryBB);
2186 OnStack.insert(EntryBB);
2187 do {
2188 dfs_next_succ:
Dan Gohmanc24c66f2012-04-24 22:53:18 +00002189 BasicBlock *CurrBB = SuccStack.back().first;
2190 TerminatorInst *TI = cast<TerminatorInst>(&CurrBB->back());
2191 succ_iterator SE(TI, false);
Dan Gohman41375a32012-05-08 23:39:44 +00002192
Dan Gohmanc24c66f2012-04-24 22:53:18 +00002193 while (SuccStack.back().second != SE) {
2194 BasicBlock *SuccBB = *SuccStack.back().second++;
David Blaikie70573dc2014-11-19 07:49:26 +00002195 if (Visited.insert(SuccBB).second) {
Dan Gohman41375a32012-05-08 23:39:44 +00002196 TerminatorInst *TI = cast<TerminatorInst>(&SuccBB->back());
2197 SuccStack.push_back(std::make_pair(SuccBB, succ_iterator(TI)));
Dan Gohmanc24c66f2012-04-24 22:53:18 +00002198 BBStates[CurrBB].addSucc(SuccBB);
Dan Gohman41375a32012-05-08 23:39:44 +00002199 BBState &SuccStates = BBStates[SuccBB];
2200 SuccStates.addPred(CurrBB);
Dan Gohmanc24c66f2012-04-24 22:53:18 +00002201 OnStack.insert(SuccBB);
Dan Gohmana53a12c2011-12-12 19:42:25 +00002202 goto dfs_next_succ;
2203 }
Dan Gohmanc24c66f2012-04-24 22:53:18 +00002204
2205 if (!OnStack.count(SuccBB)) {
2206 BBStates[CurrBB].addSucc(SuccBB);
2207 BBStates[SuccBB].addPred(CurrBB);
2208 }
Dan Gohmana53a12c2011-12-12 19:42:25 +00002209 }
Dan Gohmanc24c66f2012-04-24 22:53:18 +00002210 OnStack.erase(CurrBB);
2211 PostOrder.push_back(CurrBB);
2212 SuccStack.pop_back();
Dan Gohmana53a12c2011-12-12 19:42:25 +00002213 } while (!SuccStack.empty());
2214
2215 Visited.clear();
2216
Dan Gohmana53a12c2011-12-12 19:42:25 +00002217 // Do reverse-CFG DFS, computing the reverse-CFG PostOrder.
Dan Gohmanc24c66f2012-04-24 22:53:18 +00002218 // Functions may have many exits, and there also blocks which we treat
2219 // as exits due to ignored edges.
2220 SmallVector<std::pair<BasicBlock *, BBState::edge_iterator>, 16> PredStack;
2221 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
2222 BasicBlock *ExitBB = I;
2223 BBState &MyStates = BBStates[ExitBB];
2224 if (!MyStates.isExit())
2225 continue;
2226
Dan Gohmandae33492012-04-27 18:56:31 +00002227 MyStates.SetAsExit();
Dan Gohmanc24c66f2012-04-24 22:53:18 +00002228
2229 PredStack.push_back(std::make_pair(ExitBB, MyStates.pred_begin()));
Dan Gohmana53a12c2011-12-12 19:42:25 +00002230 Visited.insert(ExitBB);
2231 while (!PredStack.empty()) {
2232 reverse_dfs_next_succ:
Dan Gohmanc24c66f2012-04-24 22:53:18 +00002233 BBState::edge_iterator PE = BBStates[PredStack.back().first].pred_end();
2234 while (PredStack.back().second != PE) {
Dan Gohmana53a12c2011-12-12 19:42:25 +00002235 BasicBlock *BB = *PredStack.back().second++;
David Blaikie70573dc2014-11-19 07:49:26 +00002236 if (Visited.insert(BB).second) {
Dan Gohmanc24c66f2012-04-24 22:53:18 +00002237 PredStack.push_back(std::make_pair(BB, BBStates[BB].pred_begin()));
Dan Gohmana53a12c2011-12-12 19:42:25 +00002238 goto reverse_dfs_next_succ;
2239 }
2240 }
2241 ReverseCFGPostOrder.push_back(PredStack.pop_back_val().first);
2242 }
2243 }
2244}
2245
Michael Gottesman97e3df02013-01-14 00:35:14 +00002246// Visit the function both top-down and bottom-up.
John McCalld935e9c2011-06-15 23:37:01 +00002247bool
2248ObjCARCOpt::Visit(Function &F,
2249 DenseMap<const BasicBlock *, BBState> &BBStates,
2250 MapVector<Value *, RRInfo> &Retains,
2251 DenseMap<Value *, RRInfo> &Releases) {
Dan Gohmana53a12c2011-12-12 19:42:25 +00002252
2253 // Use reverse-postorder traversals, because we magically know that loops
2254 // will be well behaved, i.e. they won't repeatedly call retain on a single
2255 // pointer without doing a release. We can't use the ReversePostOrderTraversal
2256 // class here because we want the reverse-CFG postorder to consider each
2257 // function exit point, and we want to ignore selected cycle edges.
2258 SmallVector<BasicBlock *, 16> PostOrder;
2259 SmallVector<BasicBlock *, 16> ReverseCFGPostOrder;
Dan Gohmanc24c66f2012-04-24 22:53:18 +00002260 ComputePostOrders(F, PostOrder, ReverseCFGPostOrder,
2261 NoObjCARCExceptionsMDKind,
2262 BBStates);
Dan Gohmana53a12c2011-12-12 19:42:25 +00002263
2264 // Use reverse-postorder on the reverse CFG for bottom-up.
John McCalld935e9c2011-06-15 23:37:01 +00002265 bool BottomUpNestingDetected = false;
Dan Gohmanc57b58c2011-08-18 21:27:42 +00002266 for (SmallVectorImpl<BasicBlock *>::const_reverse_iterator I =
Dan Gohmana53a12c2011-12-12 19:42:25 +00002267 ReverseCFGPostOrder.rbegin(), E = ReverseCFGPostOrder.rend();
2268 I != E; ++I)
2269 BottomUpNestingDetected |= VisitBottomUp(*I, BBStates, Retains);
John McCalld935e9c2011-06-15 23:37:01 +00002270
Dan Gohmana53a12c2011-12-12 19:42:25 +00002271 // Use reverse-postorder for top-down.
John McCalld935e9c2011-06-15 23:37:01 +00002272 bool TopDownNestingDetected = false;
Dan Gohmana53a12c2011-12-12 19:42:25 +00002273 for (SmallVectorImpl<BasicBlock *>::const_reverse_iterator I =
2274 PostOrder.rbegin(), E = PostOrder.rend();
2275 I != E; ++I)
2276 TopDownNestingDetected |= VisitTopDown(*I, BBStates, Releases);
John McCalld935e9c2011-06-15 23:37:01 +00002277
2278 return TopDownNestingDetected && BottomUpNestingDetected;
2279}
2280
Michael Gottesman97e3df02013-01-14 00:35:14 +00002281/// Move the calls in RetainsToMove and ReleasesToMove.
John McCalld935e9c2011-06-15 23:37:01 +00002282void ObjCARCOpt::MoveCalls(Value *Arg,
2283 RRInfo &RetainsToMove,
2284 RRInfo &ReleasesToMove,
2285 MapVector<Value *, RRInfo> &Retains,
2286 DenseMap<Value *, RRInfo> &Releases,
Dan Gohman6320f522011-07-22 22:29:21 +00002287 SmallVectorImpl<Instruction *> &DeadInsts,
Michael Gottesman79249972013-04-05 23:46:45 +00002288 Module *M) {
Chris Lattner229907c2011-07-18 04:54:35 +00002289 Type *ArgTy = Arg->getType();
Dan Gohman6320f522011-07-22 22:29:21 +00002290 Type *ParamTy = PointerType::getUnqual(Type::getInt8Ty(ArgTy->getContext()));
Michael Gottesman79249972013-04-05 23:46:45 +00002291
Michael Gottesman89279f82013-04-05 18:10:41 +00002292 DEBUG(dbgs() << "== ObjCARCOpt::MoveCalls ==\n");
Michael Gottesman79249972013-04-05 23:46:45 +00002293
John McCalld935e9c2011-06-15 23:37:01 +00002294 // Insert the new retain and release calls.
Craig Topper46276792014-08-24 23:23:06 +00002295 for (Instruction *InsertPt : ReleasesToMove.ReverseInsertPts) {
John McCalld935e9c2011-06-15 23:37:01 +00002296 Value *MyArg = ArgTy == ParamTy ? Arg :
2297 new BitCastInst(Arg, ParamTy, "", InsertPt);
Michael Gottesman14acfac2013-07-06 01:39:23 +00002298 Constant *Decl = EP.get(ARCRuntimeEntryPoints::EPT_Retain);
2299 CallInst *Call = CallInst::Create(Decl, MyArg, "", InsertPt);
John McCalld935e9c2011-06-15 23:37:01 +00002300 Call->setDoesNotThrow();
Michael Gottesmanba648592013-03-28 23:08:44 +00002301 Call->setTailCall();
Michael Gottesman60f6b282013-03-29 05:13:07 +00002302
Michael Gottesmandf110ac2013-04-21 00:30:50 +00002303 DEBUG(dbgs() << "Inserting new Retain: " << *Call << "\n"
Michael Gottesman89279f82013-04-05 18:10:41 +00002304 "At insertion point: " << *InsertPt << "\n");
John McCalld935e9c2011-06-15 23:37:01 +00002305 }
Craig Topper46276792014-08-24 23:23:06 +00002306 for (Instruction *InsertPt : RetainsToMove.ReverseInsertPts) {
Dan Gohman5c70fad2012-03-23 17:47:54 +00002307 Value *MyArg = ArgTy == ParamTy ? Arg :
2308 new BitCastInst(Arg, ParamTy, "", InsertPt);
Michael Gottesman14acfac2013-07-06 01:39:23 +00002309 Constant *Decl = EP.get(ARCRuntimeEntryPoints::EPT_Release);
2310 CallInst *Call = CallInst::Create(Decl, MyArg, "", InsertPt);
Dan Gohman5c70fad2012-03-23 17:47:54 +00002311 // Attach a clang.imprecise_release metadata tag, if appropriate.
2312 if (MDNode *M = ReleasesToMove.ReleaseMetadata)
2313 Call->setMetadata(ImpreciseReleaseMDKind, M);
2314 Call->setDoesNotThrow();
2315 if (ReleasesToMove.IsTailCallRelease)
2316 Call->setTailCall();
Michael Gottesmanc189a392013-01-09 19:23:24 +00002317
Michael Gottesman89279f82013-04-05 18:10:41 +00002318 DEBUG(dbgs() << "Inserting new Release: " << *Call << "\n"
2319 "At insertion point: " << *InsertPt << "\n");
John McCalld935e9c2011-06-15 23:37:01 +00002320 }
2321
2322 // Delete the original retain and release calls.
Craig Topper46276792014-08-24 23:23:06 +00002323 for (Instruction *OrigRetain : RetainsToMove.Calls) {
John McCalld935e9c2011-06-15 23:37:01 +00002324 Retains.blot(OrigRetain);
2325 DeadInsts.push_back(OrigRetain);
Michael Gottesman89279f82013-04-05 18:10:41 +00002326 DEBUG(dbgs() << "Deleting retain: " << *OrigRetain << "\n");
John McCalld935e9c2011-06-15 23:37:01 +00002327 }
Craig Topper46276792014-08-24 23:23:06 +00002328 for (Instruction *OrigRelease : ReleasesToMove.Calls) {
John McCalld935e9c2011-06-15 23:37:01 +00002329 Releases.erase(OrigRelease);
2330 DeadInsts.push_back(OrigRelease);
Michael Gottesman89279f82013-04-05 18:10:41 +00002331 DEBUG(dbgs() << "Deleting release: " << *OrigRelease << "\n");
John McCalld935e9c2011-06-15 23:37:01 +00002332 }
Michael Gottesman79249972013-04-05 23:46:45 +00002333
John McCalld935e9c2011-06-15 23:37:01 +00002334}
2335
Michael Gottesman9de6f962013-01-22 21:49:00 +00002336bool
2337ObjCARCOpt::ConnectTDBUTraversals(DenseMap<const BasicBlock *, BBState>
2338 &BBStates,
2339 MapVector<Value *, RRInfo> &Retains,
2340 DenseMap<Value *, RRInfo> &Releases,
2341 Module *M,
Craig Topperb94011f2013-07-14 04:42:23 +00002342 SmallVectorImpl<Instruction *> &NewRetains,
2343 SmallVectorImpl<Instruction *> &NewReleases,
2344 SmallVectorImpl<Instruction *> &DeadInsts,
Michael Gottesman9de6f962013-01-22 21:49:00 +00002345 RRInfo &RetainsToMove,
2346 RRInfo &ReleasesToMove,
2347 Value *Arg,
2348 bool KnownSafe,
2349 bool &AnyPairsCompletelyEliminated) {
2350 // If a pair happens in a region where it is known that the reference count
Michael Gottesmana76143ee2013-05-13 23:49:42 +00002351 // is already incremented, we can similarly ignore possible decrements unless
2352 // we are dealing with a retainable object with multiple provenance sources.
Michael Gottesman9de6f962013-01-22 21:49:00 +00002353 bool KnownSafeTD = true, KnownSafeBU = true;
Michael Gottesmana76143ee2013-05-13 23:49:42 +00002354 bool MultipleOwners = false;
Michael Gottesmane67f40c2013-05-24 20:44:05 +00002355 bool CFGHazardAfflicted = false;
Michael Gottesman9de6f962013-01-22 21:49:00 +00002356
2357 // Connect the dots between the top-down-collected RetainsToMove and
2358 // bottom-up-collected ReleasesToMove to form sets of related calls.
2359 // This is an iterative process so that we connect multiple releases
2360 // to multiple retains if needed.
2361 unsigned OldDelta = 0;
2362 unsigned NewDelta = 0;
2363 unsigned OldCount = 0;
2364 unsigned NewCount = 0;
2365 bool FirstRelease = true;
Michael Gottesman9de6f962013-01-22 21:49:00 +00002366 for (;;) {
2367 for (SmallVectorImpl<Instruction *>::const_iterator
2368 NI = NewRetains.begin(), NE = NewRetains.end(); NI != NE; ++NI) {
2369 Instruction *NewRetain = *NI;
2370 MapVector<Value *, RRInfo>::const_iterator It = Retains.find(NewRetain);
2371 assert(It != Retains.end());
2372 const RRInfo &NewRetainRRI = It->second;
2373 KnownSafeTD &= NewRetainRRI.KnownSafe;
Michael Gottesman5a91bbf2013-05-24 20:44:02 +00002374 MultipleOwners =
2375 MultipleOwners || MultiOwnersSet.count(GetObjCArg(NewRetain));
Craig Topper46276792014-08-24 23:23:06 +00002376 for (Instruction *NewRetainRelease : NewRetainRRI.Calls) {
Michael Gottesman9de6f962013-01-22 21:49:00 +00002377 DenseMap<Value *, RRInfo>::const_iterator Jt =
2378 Releases.find(NewRetainRelease);
2379 if (Jt == Releases.end())
2380 return false;
2381 const RRInfo &NewRetainReleaseRRI = Jt->second;
Michael Gottesman24b2f6f2013-11-05 16:02:40 +00002382
2383 // If the release does not have a reference to the retain as well,
2384 // something happened which is unaccounted for. Do not do anything.
2385 //
2386 // This can happen if we catch an additive overflow during path count
2387 // merging.
2388 if (!NewRetainReleaseRRI.Calls.count(NewRetain))
2389 return false;
2390
David Blaikie70573dc2014-11-19 07:49:26 +00002391 if (ReleasesToMove.Calls.insert(NewRetainRelease).second) {
Michael Gottesman9e7261c2013-06-07 06:16:49 +00002392
2393 // If we overflow when we compute the path count, don't remove/move
2394 // anything.
2395 const BBState &NRRBBState = BBStates[NewRetainRelease->getParent()];
Michael Gottesmand6ce6cb2013-08-09 23:22:27 +00002396 unsigned PathCount = BBState::OverflowOccurredValue;
Michael Gottesman9e7261c2013-06-07 06:16:49 +00002397 if (NRRBBState.GetAllPathCountWithOverflow(PathCount))
2398 return false;
Michael Gottesmand6ce6cb2013-08-09 23:22:27 +00002399 assert(PathCount != BBState::OverflowOccurredValue &&
2400 "PathCount at this point can not be "
2401 "OverflowOccurredValue.");
Michael Gottesman9e7261c2013-06-07 06:16:49 +00002402 OldDelta -= PathCount;
Michael Gottesman9de6f962013-01-22 21:49:00 +00002403
2404 // Merge the ReleaseMetadata and IsTailCallRelease values.
2405 if (FirstRelease) {
2406 ReleasesToMove.ReleaseMetadata =
2407 NewRetainReleaseRRI.ReleaseMetadata;
2408 ReleasesToMove.IsTailCallRelease =
2409 NewRetainReleaseRRI.IsTailCallRelease;
2410 FirstRelease = false;
2411 } else {
2412 if (ReleasesToMove.ReleaseMetadata !=
2413 NewRetainReleaseRRI.ReleaseMetadata)
Craig Topperf40110f2014-04-25 05:29:35 +00002414 ReleasesToMove.ReleaseMetadata = nullptr;
Michael Gottesman9de6f962013-01-22 21:49:00 +00002415 if (ReleasesToMove.IsTailCallRelease !=
2416 NewRetainReleaseRRI.IsTailCallRelease)
2417 ReleasesToMove.IsTailCallRelease = false;
2418 }
2419
2420 // Collect the optimal insertion points.
2421 if (!KnownSafe)
Craig Topper46276792014-08-24 23:23:06 +00002422 for (Instruction *RIP : NewRetainReleaseRRI.ReverseInsertPts) {
David Blaikie70573dc2014-11-19 07:49:26 +00002423 if (ReleasesToMove.ReverseInsertPts.insert(RIP).second) {
Michael Gottesman9e7261c2013-06-07 06:16:49 +00002424 // If we overflow when we compute the path count, don't
2425 // remove/move anything.
2426 const BBState &RIPBBState = BBStates[RIP->getParent()];
Michael Gottesmand6ce6cb2013-08-09 23:22:27 +00002427 PathCount = BBState::OverflowOccurredValue;
Michael Gottesman9e7261c2013-06-07 06:16:49 +00002428 if (RIPBBState.GetAllPathCountWithOverflow(PathCount))
2429 return false;
Michael Gottesmand6ce6cb2013-08-09 23:22:27 +00002430 assert(PathCount != BBState::OverflowOccurredValue &&
2431 "PathCount at this point can not be "
2432 "OverflowOccurredValue.");
Michael Gottesman9e7261c2013-06-07 06:16:49 +00002433 NewDelta -= PathCount;
2434 }
Michael Gottesman9de6f962013-01-22 21:49:00 +00002435 }
2436 NewReleases.push_back(NewRetainRelease);
2437 }
2438 }
2439 }
2440 NewRetains.clear();
2441 if (NewReleases.empty()) break;
2442
2443 // Back the other way.
2444 for (SmallVectorImpl<Instruction *>::const_iterator
2445 NI = NewReleases.begin(), NE = NewReleases.end(); NI != NE; ++NI) {
2446 Instruction *NewRelease = *NI;
2447 DenseMap<Value *, RRInfo>::const_iterator It =
2448 Releases.find(NewRelease);
2449 assert(It != Releases.end());
2450 const RRInfo &NewReleaseRRI = It->second;
2451 KnownSafeBU &= NewReleaseRRI.KnownSafe;
Michael Gottesmane67f40c2013-05-24 20:44:05 +00002452 CFGHazardAfflicted |= NewReleaseRRI.CFGHazardAfflicted;
Craig Topper46276792014-08-24 23:23:06 +00002453 for (Instruction *NewReleaseRetain : NewReleaseRRI.Calls) {
Michael Gottesman9de6f962013-01-22 21:49:00 +00002454 MapVector<Value *, RRInfo>::const_iterator Jt =
2455 Retains.find(NewReleaseRetain);
2456 if (Jt == Retains.end())
2457 return false;
2458 const RRInfo &NewReleaseRetainRRI = Jt->second;
Michael Gottesman9e7261c2013-06-07 06:16:49 +00002459
Michael Gottesman24b2f6f2013-11-05 16:02:40 +00002460 // If the retain does not have a reference to the release as well,
2461 // something happened which is unaccounted for. Do not do anything.
2462 //
2463 // This can happen if we catch an additive overflow during path count
2464 // merging.
2465 if (!NewReleaseRetainRRI.Calls.count(NewRelease))
2466 return false;
2467
David Blaikie70573dc2014-11-19 07:49:26 +00002468 if (RetainsToMove.Calls.insert(NewReleaseRetain).second) {
Michael Gottesman9e7261c2013-06-07 06:16:49 +00002469 // If we overflow when we compute the path count, don't remove/move
2470 // anything.
2471 const BBState &NRRBBState = BBStates[NewReleaseRetain->getParent()];
Michael Gottesmand6ce6cb2013-08-09 23:22:27 +00002472 unsigned PathCount = BBState::OverflowOccurredValue;
Michael Gottesman9e7261c2013-06-07 06:16:49 +00002473 if (NRRBBState.GetAllPathCountWithOverflow(PathCount))
2474 return false;
Michael Gottesmand6ce6cb2013-08-09 23:22:27 +00002475 assert(PathCount != BBState::OverflowOccurredValue &&
2476 "PathCount at this point can not be "
2477 "OverflowOccurredValue.");
Michael Gottesman9de6f962013-01-22 21:49:00 +00002478 OldDelta += PathCount;
2479 OldCount += PathCount;
2480
Michael Gottesman9de6f962013-01-22 21:49:00 +00002481 // Collect the optimal insertion points.
2482 if (!KnownSafe)
Craig Topper46276792014-08-24 23:23:06 +00002483 for (Instruction *RIP : NewReleaseRetainRRI.ReverseInsertPts) {
David Blaikie70573dc2014-11-19 07:49:26 +00002484 if (RetainsToMove.ReverseInsertPts.insert(RIP).second) {
Michael Gottesman9e7261c2013-06-07 06:16:49 +00002485 // If we overflow when we compute the path count, don't
2486 // remove/move anything.
2487 const BBState &RIPBBState = BBStates[RIP->getParent()];
Michael Gottesmand6ce6cb2013-08-09 23:22:27 +00002488
2489 PathCount = BBState::OverflowOccurredValue;
Michael Gottesman9e7261c2013-06-07 06:16:49 +00002490 if (RIPBBState.GetAllPathCountWithOverflow(PathCount))
2491 return false;
Michael Gottesmand6ce6cb2013-08-09 23:22:27 +00002492 assert(PathCount != BBState::OverflowOccurredValue &&
2493 "PathCount at this point can not be "
2494 "OverflowOccurredValue.");
Michael Gottesman9de6f962013-01-22 21:49:00 +00002495 NewDelta += PathCount;
2496 NewCount += PathCount;
2497 }
2498 }
2499 NewRetains.push_back(NewReleaseRetain);
2500 }
2501 }
2502 }
2503 NewReleases.clear();
2504 if (NewRetains.empty()) break;
2505 }
2506
Michael Gottesmana76143ee2013-05-13 23:49:42 +00002507 // If the pointer is known incremented in 1 direction and we do not have
2508 // MultipleOwners, we can safely remove the retain/releases. Otherwise we need
2509 // to be known safe in both directions.
2510 bool UnconditionallySafe = (KnownSafeTD && KnownSafeBU) ||
2511 ((KnownSafeTD || KnownSafeBU) && !MultipleOwners);
2512 if (UnconditionallySafe) {
Michael Gottesman9de6f962013-01-22 21:49:00 +00002513 RetainsToMove.ReverseInsertPts.clear();
2514 ReleasesToMove.ReverseInsertPts.clear();
2515 NewCount = 0;
2516 } else {
2517 // Determine whether the new insertion points we computed preserve the
2518 // balance of retain and release calls through the program.
2519 // TODO: If the fully aggressive solution isn't valid, try to find a
2520 // less aggressive solution which is.
2521 if (NewDelta != 0)
2522 return false;
Michael Gottesmane67f40c2013-05-24 20:44:05 +00002523
2524 // At this point, we are not going to remove any RR pairs, but we still are
2525 // able to move RR pairs. If one of our pointers is afflicted with
2526 // CFGHazards, we cannot perform such code motion so exit early.
2527 const bool WillPerformCodeMotion = RetainsToMove.ReverseInsertPts.size() ||
2528 ReleasesToMove.ReverseInsertPts.size();
2529 if (CFGHazardAfflicted && WillPerformCodeMotion)
2530 return false;
Michael Gottesman9de6f962013-01-22 21:49:00 +00002531 }
2532
2533 // Determine whether the original call points are balanced in the retain and
2534 // release calls through the program. If not, conservatively don't touch
2535 // them.
2536 // TODO: It's theoretically possible to do code motion in this case, as
2537 // long as the existing imbalances are maintained.
2538 if (OldDelta != 0)
2539 return false;
Michael Gottesman8005ad32013-04-29 06:16:55 +00002540
Michael Gottesmana87bb8f2013-04-29 05:13:13 +00002541#ifdef ARC_ANNOTATIONS
2542 // Do not move calls if ARC annotations are requested.
Michael Gottesman3e3977c2013-04-29 05:25:39 +00002543 if (EnableARCAnnotations)
2544 return false;
Michael Gottesmana87bb8f2013-04-29 05:13:13 +00002545#endif // ARC_ANNOTATIONS
Michael Gottesman9de6f962013-01-22 21:49:00 +00002546
2547 Changed = true;
2548 assert(OldCount != 0 && "Unreachable code?");
2549 NumRRs += OldCount - NewCount;
Michael Gottesman9de6f962013-01-22 21:49:00 +00002550 // Set to true if we completely removed any RR pairs.
Michael Gottesman8b5515f2013-01-22 21:53:43 +00002551 AnyPairsCompletelyEliminated = NewCount == 0;
Michael Gottesman9de6f962013-01-22 21:49:00 +00002552
2553 // We can move calls!
2554 return true;
2555}
2556
Michael Gottesman97e3df02013-01-14 00:35:14 +00002557/// Identify pairings between the retains and releases, and delete and/or move
2558/// them.
John McCalld935e9c2011-06-15 23:37:01 +00002559bool
2560ObjCARCOpt::PerformCodePlacement(DenseMap<const BasicBlock *, BBState>
2561 &BBStates,
2562 MapVector<Value *, RRInfo> &Retains,
Dan Gohman6320f522011-07-22 22:29:21 +00002563 DenseMap<Value *, RRInfo> &Releases,
2564 Module *M) {
Michael Gottesman89279f82013-04-05 18:10:41 +00002565 DEBUG(dbgs() << "\n== ObjCARCOpt::PerformCodePlacement ==\n");
2566
John McCalld935e9c2011-06-15 23:37:01 +00002567 bool AnyPairsCompletelyEliminated = false;
2568 RRInfo RetainsToMove;
2569 RRInfo ReleasesToMove;
2570 SmallVector<Instruction *, 4> NewRetains;
2571 SmallVector<Instruction *, 4> NewReleases;
2572 SmallVector<Instruction *, 8> DeadInsts;
2573
Dan Gohman670f9372012-04-13 18:57:48 +00002574 // Visit each retain.
John McCalld935e9c2011-06-15 23:37:01 +00002575 for (MapVector<Value *, RRInfo>::const_iterator I = Retains.begin(),
Dan Gohman2053a5d2011-09-29 22:25:23 +00002576 E = Retains.end(); I != E; ++I) {
2577 Value *V = I->first;
John McCalld935e9c2011-06-15 23:37:01 +00002578 if (!V) continue; // blotted
2579
2580 Instruction *Retain = cast<Instruction>(V);
Michael Gottesmanc189a392013-01-09 19:23:24 +00002581
Michael Gottesman89279f82013-04-05 18:10:41 +00002582 DEBUG(dbgs() << "Visiting: " << *Retain << "\n");
Michael Gottesmanc189a392013-01-09 19:23:24 +00002583
John McCalld935e9c2011-06-15 23:37:01 +00002584 Value *Arg = GetObjCArg(Retain);
2585
Dan Gohman728db492012-01-13 00:39:07 +00002586 // If the object being released is in static or stack storage, we know it's
John McCalld935e9c2011-06-15 23:37:01 +00002587 // not being managed by ObjC reference counting, so we can delete pairs
2588 // regardless of what possible decrements or uses lie between them.
Dan Gohman728db492012-01-13 00:39:07 +00002589 bool KnownSafe = isa<Constant>(Arg) || isa<AllocaInst>(Arg);
Dan Gohman41375a32012-05-08 23:39:44 +00002590
Dan Gohman56e1cef2011-08-22 17:29:11 +00002591 // A constant pointer can't be pointing to an object on the heap. It may
2592 // be reference-counted, but it won't be deleted.
2593 if (const LoadInst *LI = dyn_cast<LoadInst>(Arg))
2594 if (const GlobalVariable *GV =
2595 dyn_cast<GlobalVariable>(
2596 StripPointerCastsAndObjCCalls(LI->getPointerOperand())))
2597 if (GV->isConstant())
2598 KnownSafe = true;
2599
John McCalld935e9c2011-06-15 23:37:01 +00002600 // Connect the dots between the top-down-collected RetainsToMove and
2601 // bottom-up-collected ReleasesToMove to form sets of related calls.
John McCalld935e9c2011-06-15 23:37:01 +00002602 NewRetains.push_back(Retain);
Michael Gottesman9de6f962013-01-22 21:49:00 +00002603 bool PerformMoveCalls =
2604 ConnectTDBUTraversals(BBStates, Retains, Releases, M, NewRetains,
2605 NewReleases, DeadInsts, RetainsToMove,
2606 ReleasesToMove, Arg, KnownSafe,
2607 AnyPairsCompletelyEliminated);
John McCalld935e9c2011-06-15 23:37:01 +00002608
Michael Gottesman9de6f962013-01-22 21:49:00 +00002609 if (PerformMoveCalls) {
2610 // Ok, everything checks out and we're all set. Let's move/delete some
2611 // code!
2612 MoveCalls(Arg, RetainsToMove, ReleasesToMove,
2613 Retains, Releases, DeadInsts, M);
John McCalld935e9c2011-06-15 23:37:01 +00002614 }
2615
Michael Gottesman9de6f962013-01-22 21:49:00 +00002616 // Clean up state for next retain.
John McCalld935e9c2011-06-15 23:37:01 +00002617 NewReleases.clear();
2618 NewRetains.clear();
2619 RetainsToMove.clear();
2620 ReleasesToMove.clear();
2621 }
2622
2623 // Now that we're done moving everything, we can delete the newly dead
2624 // instructions, as we no longer need them as insert points.
2625 while (!DeadInsts.empty())
2626 EraseInstruction(DeadInsts.pop_back_val());
2627
2628 return AnyPairsCompletelyEliminated;
2629}
2630
Michael Gottesman97e3df02013-01-14 00:35:14 +00002631/// Weak pointer optimizations.
John McCalld935e9c2011-06-15 23:37:01 +00002632void ObjCARCOpt::OptimizeWeakCalls(Function &F) {
Michael Gottesman89279f82013-04-05 18:10:41 +00002633 DEBUG(dbgs() << "\n== ObjCARCOpt::OptimizeWeakCalls ==\n");
Michael Gottesman79249972013-04-05 23:46:45 +00002634
John McCalld935e9c2011-06-15 23:37:01 +00002635 // First, do memdep-style RLE and S2L optimizations. We can't use memdep
2636 // itself because it uses AliasAnalysis and we need to do provenance
2637 // queries instead.
2638 for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
2639 Instruction *Inst = &*I++;
Michael Gottesman3f146e22013-01-01 16:05:48 +00002640
Michael Gottesman89279f82013-04-05 18:10:41 +00002641 DEBUG(dbgs() << "Visiting: " << *Inst << "\n");
Michael Gottesman3f146e22013-01-01 16:05:48 +00002642
John McCalld935e9c2011-06-15 23:37:01 +00002643 InstructionClass Class = GetBasicInstructionClass(Inst);
2644 if (Class != IC_LoadWeak && Class != IC_LoadWeakRetained)
2645 continue;
2646
2647 // Delete objc_loadWeak calls with no users.
2648 if (Class == IC_LoadWeak && Inst->use_empty()) {
2649 Inst->eraseFromParent();
2650 continue;
2651 }
2652
2653 // TODO: For now, just look for an earlier available version of this value
2654 // within the same block. Theoretically, we could do memdep-style non-local
2655 // analysis too, but that would want caching. A better approach would be to
2656 // use the technique that EarlyCSE uses.
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00002657 inst_iterator Current = std::prev(I);
John McCalld935e9c2011-06-15 23:37:01 +00002658 BasicBlock *CurrentBB = Current.getBasicBlockIterator();
2659 for (BasicBlock::iterator B = CurrentBB->begin(),
2660 J = Current.getInstructionIterator();
2661 J != B; --J) {
Benjamin Kramerb6d0bd42014-03-02 12:27:27 +00002662 Instruction *EarlierInst = &*std::prev(J);
John McCalld935e9c2011-06-15 23:37:01 +00002663 InstructionClass EarlierClass = GetInstructionClass(EarlierInst);
2664 switch (EarlierClass) {
2665 case IC_LoadWeak:
2666 case IC_LoadWeakRetained: {
2667 // If this is loading from the same pointer, replace this load's value
2668 // with that one.
2669 CallInst *Call = cast<CallInst>(Inst);
2670 CallInst *EarlierCall = cast<CallInst>(EarlierInst);
2671 Value *Arg = Call->getArgOperand(0);
2672 Value *EarlierArg = EarlierCall->getArgOperand(0);
2673 switch (PA.getAA()->alias(Arg, EarlierArg)) {
2674 case AliasAnalysis::MustAlias:
2675 Changed = true;
2676 // If the load has a builtin retain, insert a plain retain for it.
2677 if (Class == IC_LoadWeakRetained) {
Michael Gottesman14acfac2013-07-06 01:39:23 +00002678 Constant *Decl = EP.get(ARCRuntimeEntryPoints::EPT_Retain);
2679 CallInst *CI = CallInst::Create(Decl, EarlierCall, "", Call);
John McCalld935e9c2011-06-15 23:37:01 +00002680 CI->setTailCall();
2681 }
2682 // Zap the fully redundant load.
2683 Call->replaceAllUsesWith(EarlierCall);
2684 Call->eraseFromParent();
2685 goto clobbered;
2686 case AliasAnalysis::MayAlias:
2687 case AliasAnalysis::PartialAlias:
2688 goto clobbered;
2689 case AliasAnalysis::NoAlias:
2690 break;
2691 }
2692 break;
2693 }
2694 case IC_StoreWeak:
2695 case IC_InitWeak: {
2696 // If this is storing to the same pointer and has the same size etc.
2697 // replace this load's value with the stored value.
2698 CallInst *Call = cast<CallInst>(Inst);
2699 CallInst *EarlierCall = cast<CallInst>(EarlierInst);
2700 Value *Arg = Call->getArgOperand(0);
2701 Value *EarlierArg = EarlierCall->getArgOperand(0);
2702 switch (PA.getAA()->alias(Arg, EarlierArg)) {
2703 case AliasAnalysis::MustAlias:
2704 Changed = true;
2705 // If the load has a builtin retain, insert a plain retain for it.
2706 if (Class == IC_LoadWeakRetained) {
Michael Gottesman14acfac2013-07-06 01:39:23 +00002707 Constant *Decl = EP.get(ARCRuntimeEntryPoints::EPT_Retain);
2708 CallInst *CI = CallInst::Create(Decl, EarlierCall, "", Call);
John McCalld935e9c2011-06-15 23:37:01 +00002709 CI->setTailCall();
2710 }
2711 // Zap the fully redundant load.
2712 Call->replaceAllUsesWith(EarlierCall->getArgOperand(1));
2713 Call->eraseFromParent();
2714 goto clobbered;
2715 case AliasAnalysis::MayAlias:
2716 case AliasAnalysis::PartialAlias:
2717 goto clobbered;
2718 case AliasAnalysis::NoAlias:
2719 break;
2720 }
2721 break;
2722 }
2723 case IC_MoveWeak:
2724 case IC_CopyWeak:
2725 // TOOD: Grab the copied value.
2726 goto clobbered;
2727 case IC_AutoreleasepoolPush:
2728 case IC_None:
John McCall20182ac2013-03-22 21:38:36 +00002729 case IC_IntrinsicUser:
John McCalld935e9c2011-06-15 23:37:01 +00002730 case IC_User:
2731 // Weak pointers are only modified through the weak entry points
2732 // (and arbitrary calls, which could call the weak entry points).
2733 break;
2734 default:
2735 // Anything else could modify the weak pointer.
2736 goto clobbered;
2737 }
2738 }
2739 clobbered:;
2740 }
2741
2742 // Then, for each destroyWeak with an alloca operand, check to see if
2743 // the alloca and all its users can be zapped.
2744 for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
2745 Instruction *Inst = &*I++;
2746 InstructionClass Class = GetBasicInstructionClass(Inst);
2747 if (Class != IC_DestroyWeak)
2748 continue;
2749
2750 CallInst *Call = cast<CallInst>(Inst);
2751 Value *Arg = Call->getArgOperand(0);
2752 if (AllocaInst *Alloca = dyn_cast<AllocaInst>(Arg)) {
Chandler Carruthcdf47882014-03-09 03:16:01 +00002753 for (User *U : Alloca->users()) {
2754 const Instruction *UserInst = cast<Instruction>(U);
John McCalld935e9c2011-06-15 23:37:01 +00002755 switch (GetBasicInstructionClass(UserInst)) {
2756 case IC_InitWeak:
2757 case IC_StoreWeak:
2758 case IC_DestroyWeak:
2759 continue;
2760 default:
2761 goto done;
2762 }
2763 }
2764 Changed = true;
Chandler Carruthcdf47882014-03-09 03:16:01 +00002765 for (auto UI = Alloca->user_begin(), UE = Alloca->user_end(); UI != UE;) {
John McCalld935e9c2011-06-15 23:37:01 +00002766 CallInst *UserInst = cast<CallInst>(*UI++);
Dan Gohman14862c32012-05-18 22:17:29 +00002767 switch (GetBasicInstructionClass(UserInst)) {
2768 case IC_InitWeak:
2769 case IC_StoreWeak:
2770 // These functions return their second argument.
2771 UserInst->replaceAllUsesWith(UserInst->getArgOperand(1));
2772 break;
2773 case IC_DestroyWeak:
2774 // No return value.
2775 break;
2776 default:
Dan Gohman9c97eea02012-05-21 17:41:28 +00002777 llvm_unreachable("alloca really is used!");
Dan Gohman14862c32012-05-18 22:17:29 +00002778 }
John McCalld935e9c2011-06-15 23:37:01 +00002779 UserInst->eraseFromParent();
2780 }
2781 Alloca->eraseFromParent();
2782 done:;
2783 }
2784 }
2785}
2786
Michael Gottesman97e3df02013-01-14 00:35:14 +00002787/// Identify program paths which execute sequences of retains and releases which
2788/// can be eliminated.
John McCalld935e9c2011-06-15 23:37:01 +00002789bool ObjCARCOpt::OptimizeSequences(Function &F) {
Michael Gottesman740db972013-05-23 02:35:21 +00002790 // Releases, Retains - These are used to store the results of the main flow
2791 // analysis. These use Value* as the key instead of Instruction* so that the
2792 // map stays valid when we get around to rewriting code and calls get
2793 // replaced by arguments.
John McCalld935e9c2011-06-15 23:37:01 +00002794 DenseMap<Value *, RRInfo> Releases;
2795 MapVector<Value *, RRInfo> Retains;
2796
Michael Gottesman740db972013-05-23 02:35:21 +00002797 // This is used during the traversal of the function to track the
2798 // states for each identified object at each block.
John McCalld935e9c2011-06-15 23:37:01 +00002799 DenseMap<const BasicBlock *, BBState> BBStates;
2800
2801 // Analyze the CFG of the function, and all instructions.
2802 bool NestingDetected = Visit(F, BBStates, Retains, Releases);
2803
2804 // Transform.
Michael Gottesman5a91bbf2013-05-24 20:44:02 +00002805 bool AnyPairsCompletelyEliminated = PerformCodePlacement(BBStates, Retains,
2806 Releases,
2807 F.getParent());
2808
2809 // Cleanup.
2810 MultiOwnersSet.clear();
2811
2812 return AnyPairsCompletelyEliminated && NestingDetected;
John McCalld935e9c2011-06-15 23:37:01 +00002813}
2814
Michael Gottesmanc2d5bf52013-04-03 23:07:45 +00002815/// Check if there is a dependent call earlier that does not have anything in
2816/// between the Retain and the call that can affect the reference count of their
2817/// shared pointer argument. Note that Retain need not be in BB.
Michael Gottesman54dc7fd2013-04-03 23:04:28 +00002818static bool
2819HasSafePathToPredecessorCall(const Value *Arg, Instruction *Retain,
Craig Topper71b7b682014-08-21 05:55:13 +00002820 SmallPtrSetImpl<Instruction *> &DepInsts,
2821 SmallPtrSetImpl<const BasicBlock *> &Visited,
Michael Gottesman54dc7fd2013-04-03 23:04:28 +00002822 ProvenanceAnalysis &PA) {
2823 FindDependencies(CanChangeRetainCount, Arg, Retain->getParent(), Retain,
2824 DepInsts, Visited, PA);
2825 if (DepInsts.size() != 1)
2826 return false;
Michael Gottesmanc2d5bf52013-04-03 23:07:45 +00002827
Michael Gottesman54dc7fd2013-04-03 23:04:28 +00002828 CallInst *Call =
2829 dyn_cast_or_null<CallInst>(*DepInsts.begin());
Michael Gottesmanc2d5bf52013-04-03 23:07:45 +00002830
Michael Gottesman54dc7fd2013-04-03 23:04:28 +00002831 // Check that the pointer is the return value of the call.
2832 if (!Call || Arg != Call)
2833 return false;
Michael Gottesmanc2d5bf52013-04-03 23:07:45 +00002834
Michael Gottesman54dc7fd2013-04-03 23:04:28 +00002835 // Check that the call is a regular call.
2836 InstructionClass Class = GetBasicInstructionClass(Call);
2837 if (Class != IC_CallOrUser && Class != IC_Call)
2838 return false;
Michael Gottesmanc2d5bf52013-04-03 23:07:45 +00002839
Michael Gottesman54dc7fd2013-04-03 23:04:28 +00002840 return true;
2841}
2842
Michael Gottesman6908db12013-04-03 23:16:05 +00002843/// Find a dependent retain that precedes the given autorelease for which there
2844/// is nothing in between the two instructions that can affect the ref count of
2845/// Arg.
2846static CallInst *
2847FindPredecessorRetainWithSafePath(const Value *Arg, BasicBlock *BB,
2848 Instruction *Autorelease,
Craig Topper71b7b682014-08-21 05:55:13 +00002849 SmallPtrSetImpl<Instruction *> &DepInsts,
2850 SmallPtrSetImpl<const BasicBlock *> &Visited,
Michael Gottesman6908db12013-04-03 23:16:05 +00002851 ProvenanceAnalysis &PA) {
2852 FindDependencies(CanChangeRetainCount, Arg,
2853 BB, Autorelease, DepInsts, Visited, PA);
2854 if (DepInsts.size() != 1)
Craig Topperf40110f2014-04-25 05:29:35 +00002855 return nullptr;
Michael Gottesman79249972013-04-05 23:46:45 +00002856
Michael Gottesman6908db12013-04-03 23:16:05 +00002857 CallInst *Retain =
2858 dyn_cast_or_null<CallInst>(*DepInsts.begin());
Michael Gottesman79249972013-04-05 23:46:45 +00002859
Michael Gottesman6908db12013-04-03 23:16:05 +00002860 // Check that we found a retain with the same argument.
2861 if (!Retain ||
2862 !IsRetain(GetBasicInstructionClass(Retain)) ||
2863 GetObjCArg(Retain) != Arg) {
Craig Topperf40110f2014-04-25 05:29:35 +00002864 return nullptr;
Michael Gottesman6908db12013-04-03 23:16:05 +00002865 }
Michael Gottesman79249972013-04-05 23:46:45 +00002866
Michael Gottesman6908db12013-04-03 23:16:05 +00002867 return Retain;
2868}
2869
Michael Gottesman21a4ed32013-04-03 23:39:14 +00002870/// Look for an ``autorelease'' instruction dependent on Arg such that there are
2871/// no instructions dependent on Arg that need a positive ref count in between
2872/// the autorelease and the ret.
2873static CallInst *
2874FindPredecessorAutoreleaseWithSafePath(const Value *Arg, BasicBlock *BB,
2875 ReturnInst *Ret,
Craig Topper71b7b682014-08-21 05:55:13 +00002876 SmallPtrSetImpl<Instruction *> &DepInsts,
2877 SmallPtrSetImpl<const BasicBlock *> &V,
Michael Gottesman21a4ed32013-04-03 23:39:14 +00002878 ProvenanceAnalysis &PA) {
2879 FindDependencies(NeedsPositiveRetainCount, Arg,
2880 BB, Ret, DepInsts, V, PA);
2881 if (DepInsts.size() != 1)
Craig Topperf40110f2014-04-25 05:29:35 +00002882 return nullptr;
Michael Gottesman79249972013-04-05 23:46:45 +00002883
Michael Gottesman21a4ed32013-04-03 23:39:14 +00002884 CallInst *Autorelease =
2885 dyn_cast_or_null<CallInst>(*DepInsts.begin());
2886 if (!Autorelease)
Craig Topperf40110f2014-04-25 05:29:35 +00002887 return nullptr;
Michael Gottesman21a4ed32013-04-03 23:39:14 +00002888 InstructionClass AutoreleaseClass = GetBasicInstructionClass(Autorelease);
2889 if (!IsAutorelease(AutoreleaseClass))
Craig Topperf40110f2014-04-25 05:29:35 +00002890 return nullptr;
Michael Gottesman21a4ed32013-04-03 23:39:14 +00002891 if (GetObjCArg(Autorelease) != Arg)
Craig Topperf40110f2014-04-25 05:29:35 +00002892 return nullptr;
Michael Gottesman79249972013-04-05 23:46:45 +00002893
Michael Gottesman21a4ed32013-04-03 23:39:14 +00002894 return Autorelease;
2895}
2896
Michael Gottesman97e3df02013-01-14 00:35:14 +00002897/// Look for this pattern:
Dmitri Gribenko5485acd2012-09-14 14:57:36 +00002898/// \code
John McCalld935e9c2011-06-15 23:37:01 +00002899/// %call = call i8* @something(...)
2900/// %2 = call i8* @objc_retain(i8* %call)
2901/// %3 = call i8* @objc_autorelease(i8* %2)
2902/// ret i8* %3
Dmitri Gribenko5485acd2012-09-14 14:57:36 +00002903/// \endcode
John McCalld935e9c2011-06-15 23:37:01 +00002904/// And delete the retain and autorelease.
John McCalld935e9c2011-06-15 23:37:01 +00002905void ObjCARCOpt::OptimizeReturns(Function &F) {
2906 if (!F.getReturnType()->isPointerTy())
2907 return;
Michael Gottesman79249972013-04-05 23:46:45 +00002908
Michael Gottesman89279f82013-04-05 18:10:41 +00002909 DEBUG(dbgs() << "\n== ObjCARCOpt::OptimizeReturns ==\n");
Michael Gottesman79249972013-04-05 23:46:45 +00002910
John McCalld935e9c2011-06-15 23:37:01 +00002911 SmallPtrSet<Instruction *, 4> DependingInstructions;
2912 SmallPtrSet<const BasicBlock *, 4> Visited;
2913 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI) {
2914 BasicBlock *BB = FI;
2915 ReturnInst *Ret = dyn_cast<ReturnInst>(&BB->back());
Michael Gottesman3f146e22013-01-01 16:05:48 +00002916
Michael Gottesman89279f82013-04-05 18:10:41 +00002917 DEBUG(dbgs() << "Visiting: " << *Ret << "\n");
Michael Gottesman3f146e22013-01-01 16:05:48 +00002918
Michael Gottesman21a4ed32013-04-03 23:39:14 +00002919 if (!Ret)
2920 continue;
Michael Gottesman79249972013-04-05 23:46:45 +00002921
John McCalld935e9c2011-06-15 23:37:01 +00002922 const Value *Arg = StripPointerCastsAndObjCCalls(Ret->getOperand(0));
Michael Gottesman79249972013-04-05 23:46:45 +00002923
Michael Gottesmancdb7c152013-04-21 00:25:04 +00002924 // Look for an ``autorelease'' instruction that is a predecessor of Ret and
Michael Gottesman21a4ed32013-04-03 23:39:14 +00002925 // dependent on Arg such that there are no instructions dependent on Arg
2926 // that need a positive ref count in between the autorelease and Ret.
2927 CallInst *Autorelease =
2928 FindPredecessorAutoreleaseWithSafePath(Arg, BB, Ret,
2929 DependingInstructions, Visited,
2930 PA);
John McCalld935e9c2011-06-15 23:37:01 +00002931 DependingInstructions.clear();
2932 Visited.clear();
Michael Gottesmanfb9ece92013-04-21 00:25:01 +00002933
2934 if (!Autorelease)
2935 continue;
2936
2937 CallInst *Retain =
2938 FindPredecessorRetainWithSafePath(Arg, BB, Autorelease,
2939 DependingInstructions, Visited, PA);
2940 DependingInstructions.clear();
2941 Visited.clear();
2942
2943 if (!Retain)
2944 continue;
2945
2946 // Check that there is nothing that can affect the reference count
2947 // between the retain and the call. Note that Retain need not be in BB.
2948 bool HasSafePathToCall = HasSafePathToPredecessorCall(Arg, Retain,
2949 DependingInstructions,
2950 Visited, PA);
2951 DependingInstructions.clear();
2952 Visited.clear();
2953
2954 if (!HasSafePathToCall)
2955 continue;
2956
2957 // If so, we can zap the retain and autorelease.
2958 Changed = true;
2959 ++NumRets;
2960 DEBUG(dbgs() << "Erasing: " << *Retain << "\nErasing: "
2961 << *Autorelease << "\n");
2962 EraseInstruction(Retain);
2963 EraseInstruction(Autorelease);
John McCalld935e9c2011-06-15 23:37:01 +00002964 }
2965}
2966
Michael Gottesman9c118152013-04-29 06:16:57 +00002967#ifndef NDEBUG
2968void
2969ObjCARCOpt::GatherStatistics(Function &F, bool AfterOptimization) {
2970 llvm::Statistic &NumRetains =
2971 AfterOptimization? NumRetainsAfterOpt : NumRetainsBeforeOpt;
2972 llvm::Statistic &NumReleases =
2973 AfterOptimization? NumReleasesAfterOpt : NumReleasesBeforeOpt;
2974
2975 for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
2976 Instruction *Inst = &*I++;
2977 switch (GetBasicInstructionClass(Inst)) {
2978 default:
2979 break;
2980 case IC_Retain:
2981 ++NumRetains;
2982 break;
2983 case IC_Release:
2984 ++NumReleases;
2985 break;
2986 }
2987 }
2988}
2989#endif
2990
John McCalld935e9c2011-06-15 23:37:01 +00002991bool ObjCARCOpt::doInitialization(Module &M) {
2992 if (!EnableARCOpts)
2993 return false;
2994
Dan Gohman670f9372012-04-13 18:57:48 +00002995 // If nothing in the Module uses ARC, don't do anything.
Dan Gohmanceaac7c2011-06-20 23:20:43 +00002996 Run = ModuleHasARC(M);
2997 if (!Run)
2998 return false;
2999
John McCalld935e9c2011-06-15 23:37:01 +00003000 // Identify the imprecise release metadata kind.
3001 ImpreciseReleaseMDKind =
3002 M.getContext().getMDKindID("clang.imprecise_release");
Dan Gohmana7107f92011-10-17 22:53:25 +00003003 CopyOnEscapeMDKind =
3004 M.getContext().getMDKindID("clang.arc.copy_on_escape");
Dan Gohman0155f302012-02-17 18:59:53 +00003005 NoObjCARCExceptionsMDKind =
3006 M.getContext().getMDKindID("clang.arc.no_objc_arc_exceptions");
Michael Gottesman81b1d432013-03-26 00:42:04 +00003007#ifdef ARC_ANNOTATIONS
3008 ARCAnnotationBottomUpMDKind =
3009 M.getContext().getMDKindID("llvm.arc.annotation.bottomup");
3010 ARCAnnotationTopDownMDKind =
3011 M.getContext().getMDKindID("llvm.arc.annotation.topdown");
3012 ARCAnnotationProvenanceSourceMDKind =
3013 M.getContext().getMDKindID("llvm.arc.annotation.provenancesource");
3014#endif // ARC_ANNOTATIONS
John McCalld935e9c2011-06-15 23:37:01 +00003015
John McCalld935e9c2011-06-15 23:37:01 +00003016 // Intuitively, objc_retain and others are nocapture, however in practice
3017 // they are not, because they return their argument value. And objc_release
Dan Gohmandae33492012-04-27 18:56:31 +00003018 // calls finalizers which can have arbitrary side effects.
John McCalld935e9c2011-06-15 23:37:01 +00003019
Michael Gottesman14acfac2013-07-06 01:39:23 +00003020 // Initialize our runtime entry point cache.
3021 EP.Initialize(&M);
John McCalld935e9c2011-06-15 23:37:01 +00003022
3023 return false;
3024}
3025
3026bool ObjCARCOpt::runOnFunction(Function &F) {
3027 if (!EnableARCOpts)
3028 return false;
3029
Dan Gohmanceaac7c2011-06-20 23:20:43 +00003030 // If nothing in the Module uses ARC, don't do anything.
3031 if (!Run)
3032 return false;
3033
John McCalld935e9c2011-06-15 23:37:01 +00003034 Changed = false;
3035
Michael Gottesman89279f82013-04-05 18:10:41 +00003036 DEBUG(dbgs() << "<<< ObjCARCOpt: Visiting Function: " << F.getName() << " >>>"
3037 "\n");
Michael Gottesmanb24bdef2013-01-12 02:57:16 +00003038
John McCalld935e9c2011-06-15 23:37:01 +00003039 PA.setAA(&getAnalysis<AliasAnalysis>());
3040
Michael Gottesman9fc50b82013-05-13 18:29:07 +00003041#ifndef NDEBUG
3042 if (AreStatisticsEnabled()) {
3043 GatherStatistics(F, false);
3044 }
3045#endif
3046
John McCalld935e9c2011-06-15 23:37:01 +00003047 // This pass performs several distinct transformations. As a compile-time aid
3048 // when compiling code that isn't ObjC, skip these if the relevant ObjC
3049 // library functions aren't declared.
3050
Michael Gottesmancd5b0272013-04-24 22:18:15 +00003051 // Preliminary optimizations. This also computes UsedInThisFunction.
John McCalld935e9c2011-06-15 23:37:01 +00003052 OptimizeIndividualCalls(F);
3053
3054 // Optimizations for weak pointers.
3055 if (UsedInThisFunction & ((1 << IC_LoadWeak) |
3056 (1 << IC_LoadWeakRetained) |
3057 (1 << IC_StoreWeak) |
3058 (1 << IC_InitWeak) |
3059 (1 << IC_CopyWeak) |
3060 (1 << IC_MoveWeak) |
3061 (1 << IC_DestroyWeak)))
3062 OptimizeWeakCalls(F);
3063
3064 // Optimizations for retain+release pairs.
3065 if (UsedInThisFunction & ((1 << IC_Retain) |
3066 (1 << IC_RetainRV) |
3067 (1 << IC_RetainBlock)))
3068 if (UsedInThisFunction & (1 << IC_Release))
3069 // Run OptimizeSequences until it either stops making changes or
3070 // no retain+release pair nesting is detected.
3071 while (OptimizeSequences(F)) {}
3072
3073 // Optimizations if objc_autorelease is used.
Dan Gohman41375a32012-05-08 23:39:44 +00003074 if (UsedInThisFunction & ((1 << IC_Autorelease) |
3075 (1 << IC_AutoreleaseRV)))
John McCalld935e9c2011-06-15 23:37:01 +00003076 OptimizeReturns(F);
3077
Michael Gottesman9c118152013-04-29 06:16:57 +00003078 // Gather statistics after optimization.
3079#ifndef NDEBUG
3080 if (AreStatisticsEnabled()) {
3081 GatherStatistics(F, true);
3082 }
3083#endif
3084
Michael Gottesmanb24bdef2013-01-12 02:57:16 +00003085 DEBUG(dbgs() << "\n");
3086
John McCalld935e9c2011-06-15 23:37:01 +00003087 return Changed;
3088}
3089
3090void ObjCARCOpt::releaseMemory() {
3091 PA.clear();
3092}
3093
Michael Gottesman97e3df02013-01-14 00:35:14 +00003094/// @}
3095///