blob: 2976df6b9de1e558930fce9cff105e14632a61b7 [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#define DEBUG_TYPE "objc-arc-opts"
28#include "ObjCARC.h"
Michael Gottesman14acfac2013-07-06 01:39:23 +000029#include "ARCRuntimeEntryPoints.h"
Michael Gottesman278266f2013-01-29 04:20:52 +000030#include "DependencyAnalysis.h"
Michael Gottesman294e7da2013-01-28 05:51:54 +000031#include "ObjCARCAliasAnalysis.h"
Michael Gottesman778138e2013-01-29 03:03:03 +000032#include "ProvenanceAnalysis.h"
John McCalld935e9c2011-06-15 23:37:01 +000033#include "llvm/ADT/DenseMap.h"
Michael Gottesman5a91bbf2013-05-24 20:44:02 +000034#include "llvm/ADT/DenseSet.h"
Michael Gottesmanfa0939f2013-01-28 04:12:07 +000035#include "llvm/ADT/STLExtras.h"
Michael Gottesman778138e2013-01-29 03:03:03 +000036#include "llvm/ADT/SmallPtrSet.h"
Michael Gottesman278266f2013-01-29 04:20:52 +000037#include "llvm/ADT/Statistic.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 Gottesman778138e2013-01-29 03:03:03 +000040#include "llvm/Support/CFG.h"
Michael Gottesman13a5f1a2013-01-29 04:51:59 +000041#include "llvm/Support/Debug.h"
Timur Iskhodzhanov5d7ff002013-01-29 09:09:27 +000042#include "llvm/Support/raw_ostream.h"
Michael Gottesmanfa0939f2013-01-28 04:12:07 +000043
John McCalld935e9c2011-06-15 23:37:01 +000044using namespace llvm;
Michael Gottesman08904e32013-01-28 03:28:38 +000045using namespace llvm::objcarc;
John McCalld935e9c2011-06-15 23:37:01 +000046
Michael Gottesman97e3df02013-01-14 00:35:14 +000047/// \defgroup MiscUtils Miscellaneous utilities that are not ARC specific.
48/// @{
John McCalld935e9c2011-06-15 23:37:01 +000049
50namespace {
Michael Gottesman97e3df02013-01-14 00:35:14 +000051 /// \brief An associative container with fast insertion-order (deterministic)
52 /// iteration over its elements. Plus the special blot operation.
John McCalld935e9c2011-06-15 23:37:01 +000053 template<class KeyT, class ValueT>
54 class MapVector {
Michael Gottesman97e3df02013-01-14 00:35:14 +000055 /// Map keys to indices in Vector.
John McCalld935e9c2011-06-15 23:37:01 +000056 typedef DenseMap<KeyT, size_t> MapTy;
57 MapTy Map;
58
John McCalld935e9c2011-06-15 23:37:01 +000059 typedef std::vector<std::pair<KeyT, ValueT> > VectorTy;
Michael Gottesman97e3df02013-01-14 00:35:14 +000060 /// Keys and values.
John McCalld935e9c2011-06-15 23:37:01 +000061 VectorTy Vector;
62
63 public:
64 typedef typename VectorTy::iterator iterator;
65 typedef typename VectorTy::const_iterator const_iterator;
66 iterator begin() { return Vector.begin(); }
67 iterator end() { return Vector.end(); }
68 const_iterator begin() const { return Vector.begin(); }
69 const_iterator end() const { return Vector.end(); }
70
71#ifdef XDEBUG
72 ~MapVector() {
73 assert(Vector.size() >= Map.size()); // May differ due to blotting.
74 for (typename MapTy::const_iterator I = Map.begin(), E = Map.end();
75 I != E; ++I) {
76 assert(I->second < Vector.size());
77 assert(Vector[I->second].first == I->first);
78 }
79 for (typename VectorTy::const_iterator I = Vector.begin(),
80 E = Vector.end(); I != E; ++I)
81 assert(!I->first ||
82 (Map.count(I->first) &&
83 Map[I->first] == size_t(I - Vector.begin())));
84 }
85#endif
86
Dan Gohman55b06742012-03-02 01:13:53 +000087 ValueT &operator[](const KeyT &Arg) {
John McCalld935e9c2011-06-15 23:37:01 +000088 std::pair<typename MapTy::iterator, bool> Pair =
89 Map.insert(std::make_pair(Arg, size_t(0)));
90 if (Pair.second) {
Dan Gohman55b06742012-03-02 01:13:53 +000091 size_t Num = Vector.size();
92 Pair.first->second = Num;
John McCalld935e9c2011-06-15 23:37:01 +000093 Vector.push_back(std::make_pair(Arg, ValueT()));
Dan Gohman55b06742012-03-02 01:13:53 +000094 return Vector[Num].second;
John McCalld935e9c2011-06-15 23:37:01 +000095 }
96 return Vector[Pair.first->second].second;
97 }
98
99 std::pair<iterator, bool>
100 insert(const std::pair<KeyT, ValueT> &InsertPair) {
101 std::pair<typename MapTy::iterator, bool> Pair =
102 Map.insert(std::make_pair(InsertPair.first, size_t(0)));
103 if (Pair.second) {
Dan Gohman55b06742012-03-02 01:13:53 +0000104 size_t Num = Vector.size();
105 Pair.first->second = Num;
John McCalld935e9c2011-06-15 23:37:01 +0000106 Vector.push_back(InsertPair);
Dan Gohman55b06742012-03-02 01:13:53 +0000107 return std::make_pair(Vector.begin() + Num, true);
John McCalld935e9c2011-06-15 23:37:01 +0000108 }
109 return std::make_pair(Vector.begin() + Pair.first->second, false);
110 }
111
Michael Gottesmana76143ee2013-05-13 23:49:42 +0000112 iterator find(const KeyT &Key) {
113 typename MapTy::iterator It = Map.find(Key);
114 if (It == Map.end()) return Vector.end();
115 return Vector.begin() + It->second;
116 }
117
Dan Gohman55b06742012-03-02 01:13:53 +0000118 const_iterator find(const KeyT &Key) const {
John McCalld935e9c2011-06-15 23:37:01 +0000119 typename MapTy::const_iterator It = Map.find(Key);
120 if (It == Map.end()) return Vector.end();
121 return Vector.begin() + It->second;
122 }
123
Michael Gottesman97e3df02013-01-14 00:35:14 +0000124 /// This is similar to erase, but instead of removing the element from the
125 /// vector, it just zeros out the key in the vector. This leaves iterators
126 /// intact, but clients must be prepared for zeroed-out keys when iterating.
Dan Gohman55b06742012-03-02 01:13:53 +0000127 void blot(const KeyT &Key) {
John McCalld935e9c2011-06-15 23:37:01 +0000128 typename MapTy::iterator It = Map.find(Key);
129 if (It == Map.end()) return;
130 Vector[It->second].first = KeyT();
131 Map.erase(It);
132 }
133
134 void clear() {
135 Map.clear();
136 Vector.clear();
137 }
138 };
139}
140
Michael Gottesman97e3df02013-01-14 00:35:14 +0000141/// @}
142///
143/// \defgroup ARCUtilities Utility declarations/definitions specific to ARC.
144/// @{
John McCalld935e9c2011-06-15 23:37:01 +0000145
Michael Gottesman97e3df02013-01-14 00:35:14 +0000146/// \brief This is similar to StripPointerCastsAndObjCCalls but it stops as soon
147/// as it finds a value with multiple uses.
John McCalld935e9c2011-06-15 23:37:01 +0000148static const Value *FindSingleUseIdentifiedObject(const Value *Arg) {
149 if (Arg->hasOneUse()) {
150 if (const BitCastInst *BC = dyn_cast<BitCastInst>(Arg))
151 return FindSingleUseIdentifiedObject(BC->getOperand(0));
152 if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Arg))
153 if (GEP->hasAllZeroIndices())
154 return FindSingleUseIdentifiedObject(GEP->getPointerOperand());
155 if (IsForwarding(GetBasicInstructionClass(Arg)))
156 return FindSingleUseIdentifiedObject(
157 cast<CallInst>(Arg)->getArgOperand(0));
158 if (!IsObjCIdentifiedObject(Arg))
159 return 0;
160 return Arg;
161 }
162
Dan Gohman41375a32012-05-08 23:39:44 +0000163 // If we found an identifiable object but it has multiple uses, but they are
164 // trivial uses, we can still consider this to be a single-use value.
John McCalld935e9c2011-06-15 23:37:01 +0000165 if (IsObjCIdentifiedObject(Arg)) {
166 for (Value::const_use_iterator UI = Arg->use_begin(), UE = Arg->use_end();
167 UI != UE; ++UI) {
168 const User *U = *UI;
169 if (!U->use_empty() || StripPointerCastsAndObjCCalls(U) != Arg)
170 return 0;
171 }
172
173 return Arg;
174 }
175
176 return 0;
177}
178
Michael Gottesmana76143ee2013-05-13 23:49:42 +0000179/// This is a wrapper around getUnderlyingObjCPtr along the lines of
180/// GetUnderlyingObjects except that it returns early when it sees the first
181/// alloca.
182static inline bool AreAnyUnderlyingObjectsAnAlloca(const Value *V) {
183 SmallPtrSet<const Value *, 4> Visited;
184 SmallVector<const Value *, 4> Worklist;
185 Worklist.push_back(V);
186 do {
187 const Value *P = Worklist.pop_back_val();
188 P = GetUnderlyingObjCPtr(P);
Michael Gottesman0c8b5622013-05-14 06:40:10 +0000189
Michael Gottesmana76143ee2013-05-13 23:49:42 +0000190 if (isa<AllocaInst>(P))
191 return true;
Michael Gottesman0c8b5622013-05-14 06:40:10 +0000192
Michael Gottesmana76143ee2013-05-13 23:49:42 +0000193 if (!Visited.insert(P))
194 continue;
Michael Gottesman0c8b5622013-05-14 06:40:10 +0000195
Michael Gottesmana76143ee2013-05-13 23:49:42 +0000196 if (const SelectInst *SI = dyn_cast<const SelectInst>(P)) {
197 Worklist.push_back(SI->getTrueValue());
198 Worklist.push_back(SI->getFalseValue());
199 continue;
200 }
Michael Gottesman0c8b5622013-05-14 06:40:10 +0000201
Michael Gottesmana76143ee2013-05-13 23:49:42 +0000202 if (const PHINode *PN = dyn_cast<const PHINode>(P)) {
203 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
204 Worklist.push_back(PN->getIncomingValue(i));
205 continue;
206 }
207 } while (!Worklist.empty());
208
209 return false;
210}
211
212
Michael Gottesman97e3df02013-01-14 00:35:14 +0000213/// @}
214///
Michael Gottesman97e3df02013-01-14 00:35:14 +0000215/// \defgroup ARCOpt ARC Optimization.
216/// @{
John McCalld935e9c2011-06-15 23:37:01 +0000217
218// TODO: On code like this:
219//
220// objc_retain(%x)
221// stuff_that_cannot_release()
222// objc_autorelease(%x)
223// stuff_that_cannot_release()
224// objc_retain(%x)
225// stuff_that_cannot_release()
226// objc_autorelease(%x)
227//
228// The second retain and autorelease can be deleted.
229
230// TODO: It should be possible to delete
231// objc_autoreleasePoolPush and objc_autoreleasePoolPop
232// pairs if nothing is actually autoreleased between them. Also, autorelease
233// calls followed by objc_autoreleasePoolPop calls (perhaps in ObjC++ code
234// after inlining) can be turned into plain release calls.
235
236// TODO: Critical-edge splitting. If the optimial insertion point is
237// a critical edge, the current algorithm has to fail, because it doesn't
238// know how to split edges. It should be possible to make the optimizer
239// think in terms of edges, rather than blocks, and then split critical
240// edges on demand.
241
242// TODO: OptimizeSequences could generalized to be Interprocedural.
243
244// TODO: Recognize that a bunch of other objc runtime calls have
245// non-escaping arguments and non-releasing arguments, and may be
246// non-autoreleasing.
247
248// TODO: Sink autorelease calls as far as possible. Unfortunately we
249// usually can't sink them past other calls, which would be the main
250// case where it would be useful.
251
Dan Gohmanb3894012011-08-19 00:26:36 +0000252// TODO: The pointer returned from objc_loadWeakRetained is retained.
253
254// TODO: Delete release+retain pairs (rare).
Dan Gohmanceaac7c2011-06-20 23:20:43 +0000255
John McCalld935e9c2011-06-15 23:37:01 +0000256STATISTIC(NumNoops, "Number of no-op objc calls eliminated");
257STATISTIC(NumPartialNoops, "Number of partially no-op objc calls eliminated");
258STATISTIC(NumAutoreleases,"Number of autoreleases converted to releases");
259STATISTIC(NumRets, "Number of return value forwarding "
Michael Gottesmanb4e7f4d2013-05-15 17:43:03 +0000260 "retain+autoreleases eliminated");
John McCalld935e9c2011-06-15 23:37:01 +0000261STATISTIC(NumRRs, "Number of retain+release paths eliminated");
262STATISTIC(NumPeeps, "Number of calls peephole-optimized");
Matt Beaumont-Gaye55d9492013-05-13 21:10:49 +0000263#ifndef NDEBUG
Michael Gottesman9c118152013-04-29 06:16:57 +0000264STATISTIC(NumRetainsBeforeOpt,
Michael Gottesmanb4e7f4d2013-05-15 17:43:03 +0000265 "Number of retains before optimization");
Michael Gottesman9c118152013-04-29 06:16:57 +0000266STATISTIC(NumReleasesBeforeOpt,
Michael Gottesmanb4e7f4d2013-05-15 17:43:03 +0000267 "Number of releases before optimization");
Michael Gottesman9c118152013-04-29 06:16:57 +0000268STATISTIC(NumRetainsAfterOpt,
Michael Gottesmanb4e7f4d2013-05-15 17:43:03 +0000269 "Number of retains after optimization");
Michael Gottesman9c118152013-04-29 06:16:57 +0000270STATISTIC(NumReleasesAfterOpt,
Michael Gottesmanb4e7f4d2013-05-15 17:43:03 +0000271 "Number of releases after optimization");
Michael Gottesman03cf3c82013-04-29 07:29:08 +0000272#endif
John McCalld935e9c2011-06-15 23:37:01 +0000273
274namespace {
Michael Gottesman97e3df02013-01-14 00:35:14 +0000275 /// \enum Sequence
276 ///
277 /// \brief A sequence of states that a pointer may go through in which an
278 /// objc_retain and objc_release are actually needed.
John McCalld935e9c2011-06-15 23:37:01 +0000279 enum Sequence {
280 S_None,
Michael Gottesman53fd20b2013-01-29 21:07:51 +0000281 S_Retain, ///< objc_retain(x).
282 S_CanRelease, ///< foo(x) -- x could possibly see a ref count decrement.
283 S_Use, ///< any use of x.
Michael Gottesman386241c2013-01-29 21:39:02 +0000284 S_Stop, ///< like S_Release, but code motion is stopped.
Michael Gottesman53fd20b2013-01-29 21:07:51 +0000285 S_Release, ///< objc_release(x).
Michael Gottesman9bdab2b2013-01-29 21:41:44 +0000286 S_MovableRelease ///< objc_release(x), !clang.imprecise_release.
John McCalld935e9c2011-06-15 23:37:01 +0000287 };
Michael Gottesman53fd20b2013-01-29 21:07:51 +0000288
289 raw_ostream &operator<<(raw_ostream &OS, const Sequence S)
290 LLVM_ATTRIBUTE_UNUSED;
291 raw_ostream &operator<<(raw_ostream &OS, const Sequence S) {
292 switch (S) {
293 case S_None:
294 return OS << "S_None";
295 case S_Retain:
296 return OS << "S_Retain";
297 case S_CanRelease:
298 return OS << "S_CanRelease";
299 case S_Use:
300 return OS << "S_Use";
301 case S_Release:
302 return OS << "S_Release";
303 case S_MovableRelease:
304 return OS << "S_MovableRelease";
305 case S_Stop:
306 return OS << "S_Stop";
307 }
308 llvm_unreachable("Unknown sequence type.");
309 }
John McCalld935e9c2011-06-15 23:37:01 +0000310}
311
312static Sequence MergeSeqs(Sequence A, Sequence B, bool TopDown) {
313 // The easy cases.
314 if (A == B)
315 return A;
316 if (A == S_None || B == S_None)
317 return S_None;
318
John McCalld935e9c2011-06-15 23:37:01 +0000319 if (A > B) std::swap(A, B);
320 if (TopDown) {
321 // Choose the side which is further along in the sequence.
Dan Gohman12130272011-08-12 00:26:31 +0000322 if ((A == S_Retain || A == S_CanRelease) &&
323 (B == S_CanRelease || B == S_Use))
John McCalld935e9c2011-06-15 23:37:01 +0000324 return B;
325 } else {
326 // Choose the side which is further along in the sequence.
327 if ((A == S_Use || A == S_CanRelease) &&
Dan Gohman12130272011-08-12 00:26:31 +0000328 (B == S_Use || B == S_Release || B == S_Stop || B == S_MovableRelease))
John McCalld935e9c2011-06-15 23:37:01 +0000329 return A;
330 // If both sides are releases, choose the more conservative one.
331 if (A == S_Stop && (B == S_Release || B == S_MovableRelease))
332 return A;
333 if (A == S_Release && B == S_MovableRelease)
334 return A;
335 }
336
337 return S_None;
338}
339
340namespace {
Michael Gottesman97e3df02013-01-14 00:35:14 +0000341 /// \brief Unidirectional information about either a
John McCalld935e9c2011-06-15 23:37:01 +0000342 /// retain-decrement-use-release sequence or release-use-decrement-retain
Bob Wilson798a7702013-04-09 22:15:51 +0000343 /// reverse sequence.
John McCalld935e9c2011-06-15 23:37:01 +0000344 struct RRInfo {
Michael Gottesman97e3df02013-01-14 00:35:14 +0000345 /// After an objc_retain, the reference count of the referenced
Dan Gohmanb3894012011-08-19 00:26:36 +0000346 /// object is known to be positive. Similarly, before an objc_release, the
347 /// reference count of the referenced object is known to be positive. If
348 /// there are retain-release pairs in code regions where the retain count
349 /// is known to be positive, they can be eliminated, regardless of any side
350 /// effects between them.
351 ///
352 /// Also, a retain+release pair nested within another retain+release
353 /// pair all on the known same pointer value can be eliminated, regardless
354 /// of any intervening side effects.
355 ///
356 /// KnownSafe is true when either of these conditions is satisfied.
357 bool KnownSafe;
John McCalld935e9c2011-06-15 23:37:01 +0000358
Michael Gottesman97e3df02013-01-14 00:35:14 +0000359 /// True of the objc_release calls are all marked with the "tail" keyword.
John McCalld935e9c2011-06-15 23:37:01 +0000360 bool IsTailCallRelease;
361
Michael Gottesman97e3df02013-01-14 00:35:14 +0000362 /// If the Calls are objc_release calls and they all have a
363 /// clang.imprecise_release tag, this is the metadata tag.
John McCalld935e9c2011-06-15 23:37:01 +0000364 MDNode *ReleaseMetadata;
365
Michael Gottesman97e3df02013-01-14 00:35:14 +0000366 /// For a top-down sequence, the set of objc_retains or
John McCalld935e9c2011-06-15 23:37:01 +0000367 /// objc_retainBlocks. For bottom-up, the set of objc_releases.
368 SmallPtrSet<Instruction *, 2> Calls;
369
Michael Gottesman97e3df02013-01-14 00:35:14 +0000370 /// The set of optimal insert positions for moving calls in the opposite
371 /// sequence.
John McCalld935e9c2011-06-15 23:37:01 +0000372 SmallPtrSet<Instruction *, 2> ReverseInsertPts;
373
Michael Gottesmane67f40c2013-05-24 20:44:05 +0000374 /// If this is true, we cannot perform code motion but can still remove
375 /// retain/release pairs.
376 bool CFGHazardAfflicted;
377
John McCalld935e9c2011-06-15 23:37:01 +0000378 RRInfo() :
Michael Gottesmane67f40c2013-05-24 20:44:05 +0000379 KnownSafe(false), IsTailCallRelease(false), ReleaseMetadata(0),
380 CFGHazardAfflicted(false) {}
John McCalld935e9c2011-06-15 23:37:01 +0000381
382 void clear();
Michael Gottesman79249972013-04-05 23:46:45 +0000383
Michael Gottesman4773a102013-06-21 05:42:08 +0000384 /// Conservatively merge the two RRInfo. Returns true if a partial merge has
385 /// occured, false otherwise.
386 bool Merge(const RRInfo &Other);
387
John McCalld935e9c2011-06-15 23:37:01 +0000388 };
389}
390
391void RRInfo::clear() {
Dan Gohmanb3894012011-08-19 00:26:36 +0000392 KnownSafe = false;
John McCalld935e9c2011-06-15 23:37:01 +0000393 IsTailCallRelease = false;
394 ReleaseMetadata = 0;
395 Calls.clear();
396 ReverseInsertPts.clear();
Michael Gottesmane67f40c2013-05-24 20:44:05 +0000397 CFGHazardAfflicted = false;
John McCalld935e9c2011-06-15 23:37:01 +0000398}
399
Michael Gottesman4773a102013-06-21 05:42:08 +0000400bool RRInfo::Merge(const RRInfo &Other) {
401 // Conservatively merge the ReleaseMetadata information.
402 if (ReleaseMetadata != Other.ReleaseMetadata)
403 ReleaseMetadata = 0;
404
405 // Conservatively merge the boolean state.
406 KnownSafe &= Other.KnownSafe;
407 IsTailCallRelease &= Other.IsTailCallRelease;
408 CFGHazardAfflicted |= Other.CFGHazardAfflicted;
409
410 // Merge the call sets.
411 Calls.insert(Other.Calls.begin(), Other.Calls.end());
412
413 // Merge the insert point sets. If there are any differences,
414 // that makes this a partial merge.
415 bool Partial = ReverseInsertPts.size() != Other.ReverseInsertPts.size();
416 for (SmallPtrSet<Instruction *, 2>::const_iterator
417 I = Other.ReverseInsertPts.begin(),
418 E = Other.ReverseInsertPts.end(); I != E; ++I)
419 Partial |= ReverseInsertPts.insert(*I);
420 return Partial;
421}
422
John McCalld935e9c2011-06-15 23:37:01 +0000423namespace {
Michael Gottesman97e3df02013-01-14 00:35:14 +0000424 /// \brief This class summarizes several per-pointer runtime properties which
425 /// are propogated through the flow graph.
John McCalld935e9c2011-06-15 23:37:01 +0000426 class PtrState {
Michael Gottesman97e3df02013-01-14 00:35:14 +0000427 /// True if the reference count is known to be incremented.
Dan Gohman62079b42012-04-25 00:50:46 +0000428 bool KnownPositiveRefCount;
429
Bob Wilson798a7702013-04-09 22:15:51 +0000430 /// True if we've seen an opportunity for partial RR elimination, such as
Michael Gottesman97e3df02013-01-14 00:35:14 +0000431 /// pushing calls into a CFG triangle or into one side of a CFG diamond.
Dan Gohman62079b42012-04-25 00:50:46 +0000432 bool Partial;
John McCalld935e9c2011-06-15 23:37:01 +0000433
Michael Gottesman97e3df02013-01-14 00:35:14 +0000434 /// The current position in the sequence.
Bill Wendling2798f1e2013-12-01 03:36:07 +0000435 unsigned char Seq : 8;
John McCalld935e9c2011-06-15 23:37:01 +0000436
Michael Gottesman97e3df02013-01-14 00:35:14 +0000437 /// Unidirectional information about the current sequence.
John McCalld935e9c2011-06-15 23:37:01 +0000438 RRInfo RRI;
439
Michael Gottesmane3943d02013-06-21 19:44:30 +0000440 public:
Dan Gohmandf476e52012-09-04 23:16:20 +0000441 PtrState() : KnownPositiveRefCount(false), Partial(false),
Dan Gohman41375a32012-05-08 23:39:44 +0000442 Seq(S_None) {}
John McCalld935e9c2011-06-15 23:37:01 +0000443
Michael Gottesman93132252013-06-21 06:59:02 +0000444
445 bool IsKnownSafe() const {
Michael Gottesman01df4502013-07-06 01:41:35 +0000446 return RRI.KnownSafe;
Michael Gottesman93132252013-06-21 06:59:02 +0000447 }
448
449 void SetKnownSafe(const bool NewValue) {
450 RRI.KnownSafe = NewValue;
451 }
452
Michael Gottesmanb82a1792013-06-21 07:00:44 +0000453 bool IsTailCallRelease() const {
454 return RRI.IsTailCallRelease;
455 }
456
457 void SetTailCallRelease(const bool NewValue) {
458 RRI.IsTailCallRelease = NewValue;
459 }
460
Michael Gottesman9799cf72013-06-21 20:52:49 +0000461 bool IsTrackingImpreciseReleases() const {
Michael Gottesmanf0401182013-06-21 19:12:38 +0000462 return RRI.ReleaseMetadata != 0;
463 }
464
Michael Gottesmanf701d3f2013-06-21 07:03:07 +0000465 const MDNode *GetReleaseMetadata() const {
466 return RRI.ReleaseMetadata;
467 }
468
469 void SetReleaseMetadata(MDNode *NewValue) {
470 RRI.ReleaseMetadata = NewValue;
471 }
472
Michael Gottesman2f294592013-06-21 19:12:36 +0000473 bool IsCFGHazardAfflicted() const {
474 return RRI.CFGHazardAfflicted;
475 }
476
477 void SetCFGHazardAfflicted(const bool NewValue) {
478 RRI.CFGHazardAfflicted = NewValue;
479 }
480
Michael Gottesman415ddd72013-02-05 19:32:18 +0000481 void SetKnownPositiveRefCount() {
Michael Gottesmanf3f9e3b2013-05-14 00:08:09 +0000482 DEBUG(dbgs() << "Setting Known Positive.\n");
Dan Gohman62079b42012-04-25 00:50:46 +0000483 KnownPositiveRefCount = true;
Dan Gohman12130272011-08-12 00:26:31 +0000484 }
485
Michael Gottesman764b1cf2013-03-23 05:46:19 +0000486 void ClearKnownPositiveRefCount() {
Michael Gottesmanf3f9e3b2013-05-14 00:08:09 +0000487 DEBUG(dbgs() << "Clearing Known Positive.\n");
Dan Gohman62079b42012-04-25 00:50:46 +0000488 KnownPositiveRefCount = false;
John McCalld935e9c2011-06-15 23:37:01 +0000489 }
490
Michael Gottesman07beea42013-03-23 05:31:01 +0000491 bool HasKnownPositiveRefCount() const {
Dan Gohman62079b42012-04-25 00:50:46 +0000492 return KnownPositiveRefCount;
John McCalld935e9c2011-06-15 23:37:01 +0000493 }
494
Michael Gottesman415ddd72013-02-05 19:32:18 +0000495 void SetSeq(Sequence NewSeq) {
Michael Gottesman89279f82013-04-05 18:10:41 +0000496 DEBUG(dbgs() << "Old: " << Seq << "; New: " << NewSeq << "\n");
Michael Gottesman79249972013-04-05 23:46:45 +0000497 Seq = NewSeq;
John McCalld935e9c2011-06-15 23:37:01 +0000498 }
499
Michael Gottesman415ddd72013-02-05 19:32:18 +0000500 Sequence GetSeq() const {
Bill Wendling2798f1e2013-12-01 03:36:07 +0000501 return static_cast<Sequence>(Seq);
John McCalld935e9c2011-06-15 23:37:01 +0000502 }
503
Michael Gottesman415ddd72013-02-05 19:32:18 +0000504 void ClearSequenceProgress() {
Dan Gohman62079b42012-04-25 00:50:46 +0000505 ResetSequenceProgress(S_None);
506 }
507
Michael Gottesman415ddd72013-02-05 19:32:18 +0000508 void ResetSequenceProgress(Sequence NewSeq) {
Michael Gottesman01338a42013-04-20 23:36:57 +0000509 DEBUG(dbgs() << "Resetting sequence progress.\n");
Michael Gottesman89279f82013-04-05 18:10:41 +0000510 SetSeq(NewSeq);
Dan Gohman62079b42012-04-25 00:50:46 +0000511 Partial = false;
John McCalld935e9c2011-06-15 23:37:01 +0000512 RRI.clear();
513 }
514
515 void Merge(const PtrState &Other, bool TopDown);
Michael Gottesman4f6ef112013-06-21 19:44:27 +0000516
517 void InsertCall(Instruction *I) {
518 RRI.Calls.insert(I);
519 }
520
521 void InsertReverseInsertPt(Instruction *I) {
522 RRI.ReverseInsertPts.insert(I);
523 }
524
525 void ClearReverseInsertPts() {
526 RRI.ReverseInsertPts.clear();
527 }
528
529 bool HasReverseInsertPts() const {
530 return !RRI.ReverseInsertPts.empty();
531 }
Michael Gottesmane3943d02013-06-21 19:44:30 +0000532
533 const RRInfo &GetRRInfo() const {
534 return RRI;
535 }
John McCalld935e9c2011-06-15 23:37:01 +0000536 };
537}
538
539void
540PtrState::Merge(const PtrState &Other, bool TopDown) {
Bill Wendling2798f1e2013-12-01 03:36:07 +0000541 Seq = MergeSeqs(static_cast<Sequence>(Seq), static_cast<Sequence>(Other.Seq),
542 TopDown);
Michael Gottesmanb7deb4c2013-06-21 06:54:31 +0000543 KnownPositiveRefCount &= Other.KnownPositiveRefCount;
Michael Gottesman60f6b282013-03-29 05:13:07 +0000544
Dan Gohman1736c142011-10-17 18:48:25 +0000545 // If we're not in a sequence (anymore), drop all associated state.
John McCalld935e9c2011-06-15 23:37:01 +0000546 if (Seq == S_None) {
Dan Gohman62079b42012-04-25 00:50:46 +0000547 Partial = false;
John McCalld935e9c2011-06-15 23:37:01 +0000548 RRI.clear();
Dan Gohman62079b42012-04-25 00:50:46 +0000549 } else if (Partial || Other.Partial) {
Dan Gohman1736c142011-10-17 18:48:25 +0000550 // If we're doing a merge on a path that's previously seen a partial
551 // merge, conservatively drop the sequence, to avoid doing partial
552 // RR elimination. If the branch predicates for the two merge differ,
553 // mixing them is unsafe.
Dan Gohman62079b42012-04-25 00:50:46 +0000554 ClearSequenceProgress();
John McCalld935e9c2011-06-15 23:37:01 +0000555 } else {
Michael Gottesmanb7deb4c2013-06-21 06:54:31 +0000556 // Otherwise merge the other PtrState's RRInfo into our RRInfo. At this
557 // point, we know that currently we are not partial. Stash whether or not
558 // the merge operation caused us to undergo a partial merging of reverse
559 // insertion points.
Michael Gottesman4773a102013-06-21 05:42:08 +0000560 Partial = RRI.Merge(Other.RRI);
John McCalld935e9c2011-06-15 23:37:01 +0000561 }
562}
563
564namespace {
Michael Gottesman97e3df02013-01-14 00:35:14 +0000565 /// \brief Per-BasicBlock state.
John McCalld935e9c2011-06-15 23:37:01 +0000566 class BBState {
Michael Gottesman97e3df02013-01-14 00:35:14 +0000567 /// The number of unique control paths from the entry which can reach this
568 /// block.
John McCalld935e9c2011-06-15 23:37:01 +0000569 unsigned TopDownPathCount;
570
Michael Gottesman97e3df02013-01-14 00:35:14 +0000571 /// The number of unique control paths to exits from this block.
John McCalld935e9c2011-06-15 23:37:01 +0000572 unsigned BottomUpPathCount;
573
Michael Gottesman97e3df02013-01-14 00:35:14 +0000574 /// A type for PerPtrTopDown and PerPtrBottomUp.
John McCalld935e9c2011-06-15 23:37:01 +0000575 typedef MapVector<const Value *, PtrState> MapTy;
576
Michael Gottesman97e3df02013-01-14 00:35:14 +0000577 /// The top-down traversal uses this to record information known about a
578 /// pointer at the bottom of each block.
John McCalld935e9c2011-06-15 23:37:01 +0000579 MapTy PerPtrTopDown;
580
Michael Gottesman97e3df02013-01-14 00:35:14 +0000581 /// The bottom-up traversal uses this to record information known about a
582 /// pointer at the top of each block.
John McCalld935e9c2011-06-15 23:37:01 +0000583 MapTy PerPtrBottomUp;
584
Michael Gottesman97e3df02013-01-14 00:35:14 +0000585 /// Effective predecessors of the current block ignoring ignorable edges and
586 /// ignored backedges.
Dan Gohmanc24c66f2012-04-24 22:53:18 +0000587 SmallVector<BasicBlock *, 2> Preds;
Michael Gottesman97e3df02013-01-14 00:35:14 +0000588 /// Effective successors of the current block ignoring ignorable edges and
589 /// ignored backedges.
Dan Gohmanc24c66f2012-04-24 22:53:18 +0000590 SmallVector<BasicBlock *, 2> Succs;
591
John McCalld935e9c2011-06-15 23:37:01 +0000592 public:
Michael Gottesmand6ce6cb2013-08-09 23:22:27 +0000593 static const unsigned OverflowOccurredValue;
594
595 BBState() : TopDownPathCount(0), BottomUpPathCount(0) { }
John McCalld935e9c2011-06-15 23:37:01 +0000596
597 typedef MapTy::iterator ptr_iterator;
598 typedef MapTy::const_iterator ptr_const_iterator;
599
600 ptr_iterator top_down_ptr_begin() { return PerPtrTopDown.begin(); }
601 ptr_iterator top_down_ptr_end() { return PerPtrTopDown.end(); }
602 ptr_const_iterator top_down_ptr_begin() const {
603 return PerPtrTopDown.begin();
604 }
605 ptr_const_iterator top_down_ptr_end() const {
606 return PerPtrTopDown.end();
607 }
608
609 ptr_iterator bottom_up_ptr_begin() { return PerPtrBottomUp.begin(); }
610 ptr_iterator bottom_up_ptr_end() { return PerPtrBottomUp.end(); }
611 ptr_const_iterator bottom_up_ptr_begin() const {
612 return PerPtrBottomUp.begin();
613 }
614 ptr_const_iterator bottom_up_ptr_end() const {
615 return PerPtrBottomUp.end();
616 }
617
Michael Gottesman97e3df02013-01-14 00:35:14 +0000618 /// Mark this block as being an entry block, which has one path from the
619 /// entry by definition.
John McCalld935e9c2011-06-15 23:37:01 +0000620 void SetAsEntry() { TopDownPathCount = 1; }
621
Michael Gottesman97e3df02013-01-14 00:35:14 +0000622 /// Mark this block as being an exit block, which has one path to an exit by
623 /// definition.
John McCalld935e9c2011-06-15 23:37:01 +0000624 void SetAsExit() { BottomUpPathCount = 1; }
625
Michael Gottesman993fbf72013-05-13 19:40:39 +0000626 /// Attempt to find the PtrState object describing the top down state for
627 /// pointer Arg. Return a new initialized PtrState describing the top down
628 /// state for Arg if we do not find one.
John McCalld935e9c2011-06-15 23:37:01 +0000629 PtrState &getPtrTopDownState(const Value *Arg) {
630 return PerPtrTopDown[Arg];
631 }
632
Michael Gottesman993fbf72013-05-13 19:40:39 +0000633 /// Attempt to find the PtrState object describing the bottom up state for
634 /// pointer Arg. Return a new initialized PtrState describing the bottom up
635 /// state for Arg if we do not find one.
John McCalld935e9c2011-06-15 23:37:01 +0000636 PtrState &getPtrBottomUpState(const Value *Arg) {
637 return PerPtrBottomUp[Arg];
638 }
639
Michael Gottesmana76143ee2013-05-13 23:49:42 +0000640 /// Attempt to find the PtrState object describing the bottom up state for
641 /// pointer Arg.
642 ptr_iterator findPtrBottomUpState(const Value *Arg) {
643 return PerPtrBottomUp.find(Arg);
644 }
645
John McCalld935e9c2011-06-15 23:37:01 +0000646 void clearBottomUpPointers() {
Evan Chenge4df6a22011-08-04 18:40:26 +0000647 PerPtrBottomUp.clear();
John McCalld935e9c2011-06-15 23:37:01 +0000648 }
649
650 void clearTopDownPointers() {
651 PerPtrTopDown.clear();
652 }
653
654 void InitFromPred(const BBState &Other);
655 void InitFromSucc(const BBState &Other);
656 void MergePred(const BBState &Other);
657 void MergeSucc(const BBState &Other);
658
Michael Gottesman9e7261c2013-06-07 06:16:49 +0000659 /// Compute the number of possible unique paths from an entry to an exit
Michael Gottesman97e3df02013-01-14 00:35:14 +0000660 /// which pass through this block. This is only valid after both the
661 /// top-down and bottom-up traversals are complete.
Michael Gottesman9e7261c2013-06-07 06:16:49 +0000662 ///
663 /// Returns true if overflow occured. Returns false if overflow did not
664 /// occur.
665 bool GetAllPathCountWithOverflow(unsigned &PathCount) const {
Michael Gottesmand6ce6cb2013-08-09 23:22:27 +0000666 if (TopDownPathCount == OverflowOccurredValue ||
667 BottomUpPathCount == OverflowOccurredValue)
668 return true;
Michael Gottesman9e7261c2013-06-07 06:16:49 +0000669 unsigned long long Product =
670 (unsigned long long)TopDownPathCount*BottomUpPathCount;
Michael Gottesmand6ce6cb2013-08-09 23:22:27 +0000671 // Overflow occured if any of the upper bits of Product are set or if all
672 // the lower bits of Product are all set.
673 return (Product >> 32) ||
674 ((PathCount = Product) == OverflowOccurredValue);
John McCalld935e9c2011-06-15 23:37:01 +0000675 }
Dan Gohman12130272011-08-12 00:26:31 +0000676
Dan Gohmanc24c66f2012-04-24 22:53:18 +0000677 // Specialized CFG utilities.
Dan Gohmandae33492012-04-27 18:56:31 +0000678 typedef SmallVectorImpl<BasicBlock *>::const_iterator edge_iterator;
Michael Gottesman0fecf982013-08-07 23:56:34 +0000679 edge_iterator pred_begin() const { return Preds.begin(); }
680 edge_iterator pred_end() const { return Preds.end(); }
681 edge_iterator succ_begin() const { return Succs.begin(); }
682 edge_iterator succ_end() const { return Succs.end(); }
Dan Gohmanc24c66f2012-04-24 22:53:18 +0000683
684 void addSucc(BasicBlock *Succ) { Succs.push_back(Succ); }
685 void addPred(BasicBlock *Pred) { Preds.push_back(Pred); }
686
687 bool isExit() const { return Succs.empty(); }
John McCalld935e9c2011-06-15 23:37:01 +0000688 };
Michael Gottesmand6ce6cb2013-08-09 23:22:27 +0000689
690 const unsigned BBState::OverflowOccurredValue = 0xffffffff;
John McCalld935e9c2011-06-15 23:37:01 +0000691}
692
693void BBState::InitFromPred(const BBState &Other) {
694 PerPtrTopDown = Other.PerPtrTopDown;
695 TopDownPathCount = Other.TopDownPathCount;
696}
697
698void BBState::InitFromSucc(const BBState &Other) {
699 PerPtrBottomUp = Other.PerPtrBottomUp;
700 BottomUpPathCount = Other.BottomUpPathCount;
701}
702
Michael Gottesman97e3df02013-01-14 00:35:14 +0000703/// The top-down traversal uses this to merge information about predecessors to
704/// form the initial state for a new block.
John McCalld935e9c2011-06-15 23:37:01 +0000705void BBState::MergePred(const BBState &Other) {
Michael Gottesmand6ce6cb2013-08-09 23:22:27 +0000706 if (TopDownPathCount == OverflowOccurredValue)
707 return;
708
John McCalld935e9c2011-06-15 23:37:01 +0000709 // Other.TopDownPathCount can be 0, in which case it is either dead or a
710 // loop backedge. Loop backedges are special.
711 TopDownPathCount += Other.TopDownPathCount;
712
Michael Gottesmand6ce6cb2013-08-09 23:22:27 +0000713 // In order to be consistent, we clear the top down pointers when by adding
714 // TopDownPathCount becomes OverflowOccurredValue even though "true" overflow
715 // has not occured.
716 if (TopDownPathCount == OverflowOccurredValue) {
717 clearTopDownPointers();
718 return;
719 }
720
Michael Gottesman4385edf2013-01-14 01:47:53 +0000721 // Check for overflow. If we have overflow, fall back to conservative
722 // behavior.
Dan Gohman7c84dad2012-09-12 20:45:17 +0000723 if (TopDownPathCount < Other.TopDownPathCount) {
Michael Gottesmand6ce6cb2013-08-09 23:22:27 +0000724 TopDownPathCount = OverflowOccurredValue;
Dan Gohman7c84dad2012-09-12 20:45:17 +0000725 clearTopDownPointers();
726 return;
727 }
728
John McCalld935e9c2011-06-15 23:37:01 +0000729 // For each entry in the other set, if our set has an entry with the same key,
730 // merge the entries. Otherwise, copy the entry and merge it with an empty
731 // entry.
732 for (ptr_const_iterator MI = Other.top_down_ptr_begin(),
733 ME = Other.top_down_ptr_end(); MI != ME; ++MI) {
734 std::pair<ptr_iterator, bool> Pair = PerPtrTopDown.insert(*MI);
735 Pair.first->second.Merge(Pair.second ? PtrState() : MI->second,
736 /*TopDown=*/true);
737 }
738
Dan Gohman7e315fc32011-08-11 21:06:32 +0000739 // For each entry in our set, if the other set doesn't have an entry with the
John McCalld935e9c2011-06-15 23:37:01 +0000740 // same key, force it to merge with an empty entry.
741 for (ptr_iterator MI = top_down_ptr_begin(),
742 ME = top_down_ptr_end(); MI != ME; ++MI)
743 if (Other.PerPtrTopDown.find(MI->first) == Other.PerPtrTopDown.end())
744 MI->second.Merge(PtrState(), /*TopDown=*/true);
745}
746
Michael Gottesman97e3df02013-01-14 00:35:14 +0000747/// The bottom-up traversal uses this to merge information about successors to
748/// form the initial state for a new block.
John McCalld935e9c2011-06-15 23:37:01 +0000749void BBState::MergeSucc(const BBState &Other) {
Michael Gottesmand6ce6cb2013-08-09 23:22:27 +0000750 if (BottomUpPathCount == OverflowOccurredValue)
751 return;
752
John McCalld935e9c2011-06-15 23:37:01 +0000753 // Other.BottomUpPathCount can be 0, in which case it is either dead or a
754 // loop backedge. Loop backedges are special.
755 BottomUpPathCount += Other.BottomUpPathCount;
756
Michael Gottesmand6ce6cb2013-08-09 23:22:27 +0000757 // In order to be consistent, we clear the top down pointers when by adding
758 // BottomUpPathCount becomes OverflowOccurredValue even though "true" overflow
759 // has not occured.
760 if (BottomUpPathCount == OverflowOccurredValue) {
761 clearBottomUpPointers();
762 return;
763 }
764
Michael Gottesman4385edf2013-01-14 01:47:53 +0000765 // Check for overflow. If we have overflow, fall back to conservative
766 // behavior.
Dan Gohman7c84dad2012-09-12 20:45:17 +0000767 if (BottomUpPathCount < Other.BottomUpPathCount) {
Michael Gottesmand6ce6cb2013-08-09 23:22:27 +0000768 BottomUpPathCount = OverflowOccurredValue;
Dan Gohman7c84dad2012-09-12 20:45:17 +0000769 clearBottomUpPointers();
770 return;
771 }
772
John McCalld935e9c2011-06-15 23:37:01 +0000773 // For each entry in the other set, if our set has an entry with the
774 // same key, merge the entries. Otherwise, copy the entry and merge
775 // it with an empty entry.
776 for (ptr_const_iterator MI = Other.bottom_up_ptr_begin(),
777 ME = Other.bottom_up_ptr_end(); MI != ME; ++MI) {
778 std::pair<ptr_iterator, bool> Pair = PerPtrBottomUp.insert(*MI);
779 Pair.first->second.Merge(Pair.second ? PtrState() : MI->second,
780 /*TopDown=*/false);
781 }
782
Dan Gohman7e315fc32011-08-11 21:06:32 +0000783 // For each entry in our set, if the other set doesn't have an entry
John McCalld935e9c2011-06-15 23:37:01 +0000784 // with the same key, force it to merge with an empty entry.
785 for (ptr_iterator MI = bottom_up_ptr_begin(),
786 ME = bottom_up_ptr_end(); MI != ME; ++MI)
787 if (Other.PerPtrBottomUp.find(MI->first) == Other.PerPtrBottomUp.end())
788 MI->second.Merge(PtrState(), /*TopDown=*/false);
789}
790
Michael Gottesman81b1d432013-03-26 00:42:04 +0000791// Only enable ARC Annotations if we are building a debug version of
792// libObjCARCOpts.
793#ifndef NDEBUG
794#define ARC_ANNOTATIONS
795#endif
796
797// Define some macros along the lines of DEBUG and some helper functions to make
798// it cleaner to create annotations in the source code and to no-op when not
799// building in debug mode.
800#ifdef ARC_ANNOTATIONS
801
802#include "llvm/Support/CommandLine.h"
803
804/// Enable/disable ARC sequence annotations.
805static cl::opt<bool>
Michael Gottesman6806b512013-04-17 20:48:03 +0000806EnableARCAnnotations("enable-objc-arc-annotations", cl::init(false),
807 cl::desc("Enable emission of arc data flow analysis "
808 "annotations"));
Michael Gottesmanffef24f2013-04-17 20:48:01 +0000809static cl::opt<bool>
Michael Gottesmanadb921a2013-04-17 21:03:53 +0000810DisableCheckForCFGHazards("disable-objc-arc-checkforcfghazards", cl::init(false),
811 cl::desc("Disable check for cfg hazards when "
812 "annotating"));
Michael Gottesman4e88ce62013-04-17 21:59:41 +0000813static cl::opt<std::string>
814ARCAnnotationTargetIdentifier("objc-arc-annotation-target-identifier",
815 cl::init(""),
816 cl::desc("filter out all data flow annotations "
817 "but those that apply to the given "
818 "target llvm identifier."));
Michael Gottesman81b1d432013-03-26 00:42:04 +0000819
820/// This function appends a unique ARCAnnotationProvenanceSourceMDKind id to an
821/// instruction so that we can track backwards when post processing via the llvm
822/// arc annotation processor tool. If the function is an
823static MDString *AppendMDNodeToSourcePtr(unsigned NodeId,
824 Value *Ptr) {
825 MDString *Hash = 0;
826
827 // If pointer is a result of an instruction and it does not have a source
828 // MDNode it, attach a new MDNode onto it. If pointer is a result of
829 // an instruction and does have a source MDNode attached to it, return a
830 // reference to said Node. Otherwise just return 0.
831 if (Instruction *Inst = dyn_cast<Instruction>(Ptr)) {
832 MDNode *Node;
833 if (!(Node = Inst->getMetadata(NodeId))) {
834 // We do not have any node. Generate and attatch the hash MDString to the
835 // instruction.
836
837 // We just use an MDString to ensure that this metadata gets written out
838 // of line at the module level and to provide a very simple format
839 // encoding the information herein. Both of these makes it simpler to
840 // parse the annotations by a simple external program.
841 std::string Str;
842 raw_string_ostream os(Str);
843 os << "(" << Inst->getParent()->getParent()->getName() << ",%"
844 << Inst->getName() << ")";
845
846 Hash = MDString::get(Inst->getContext(), os.str());
847 Inst->setMetadata(NodeId, MDNode::get(Inst->getContext(),Hash));
848 } else {
849 // We have a node. Grab its hash and return it.
850 assert(Node->getNumOperands() == 1 &&
851 "An ARCAnnotationProvenanceSourceMDKind can only have 1 operand.");
852 Hash = cast<MDString>(Node->getOperand(0));
853 }
854 } else if (Argument *Arg = dyn_cast<Argument>(Ptr)) {
855 std::string str;
856 raw_string_ostream os(str);
857 os << "(" << Arg->getParent()->getName() << ",%" << Arg->getName()
858 << ")";
859 Hash = MDString::get(Arg->getContext(), os.str());
860 }
861
862 return Hash;
863}
864
Michael Gottesmancd4de0f2013-03-26 00:42:09 +0000865static std::string SequenceToString(Sequence A) {
866 std::string str;
867 raw_string_ostream os(str);
868 os << A;
869 return os.str();
870}
871
Michael Gottesman81b1d432013-03-26 00:42:04 +0000872/// Helper function to change a Sequence into a String object using our overload
873/// for raw_ostream so we only have printing code in one location.
874static MDString *SequenceToMDString(LLVMContext &Context,
875 Sequence A) {
Michael Gottesmancd4de0f2013-03-26 00:42:09 +0000876 return MDString::get(Context, SequenceToString(A));
Michael Gottesman81b1d432013-03-26 00:42:04 +0000877}
878
879/// A simple function to generate a MDNode which describes the change in state
880/// for Value *Ptr caused by Instruction *Inst.
881static void AppendMDNodeToInstForPtr(unsigned NodeId,
882 Instruction *Inst,
883 Value *Ptr,
884 MDString *PtrSourceMDNodeID,
885 Sequence OldSeq,
886 Sequence NewSeq) {
887 MDNode *Node = 0;
888 Value *tmp[3] = {PtrSourceMDNodeID,
889 SequenceToMDString(Inst->getContext(),
890 OldSeq),
891 SequenceToMDString(Inst->getContext(),
892 NewSeq)};
893 Node = MDNode::get(Inst->getContext(),
894 ArrayRef<Value*>(tmp, 3));
895
896 Inst->setMetadata(NodeId, Node);
897}
898
Michael Gottesmancd4de0f2013-03-26 00:42:09 +0000899/// Add to the beginning of the basic block llvm.ptr.annotations which show the
900/// state of a pointer at the entrance to a basic block.
901static void GenerateARCBBEntranceAnnotation(const char *Name, BasicBlock *BB,
902 Value *Ptr, Sequence Seq) {
Michael Gottesman4e88ce62013-04-17 21:59:41 +0000903 // If we have a target identifier, make sure that we match it before
904 // continuing.
905 if(!ARCAnnotationTargetIdentifier.empty() &&
906 !Ptr->getName().equals(ARCAnnotationTargetIdentifier))
907 return;
Michael Gottesman9e518132013-04-18 04:34:11 +0000908
Michael Gottesmancd4de0f2013-03-26 00:42:09 +0000909 Module *M = BB->getParent()->getParent();
910 LLVMContext &C = M->getContext();
911 Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C));
912 Type *I8XX = PointerType::getUnqual(I8X);
913 Type *Params[] = {I8XX, I8XX};
914 FunctionType *FTy = FunctionType::get(Type::getVoidTy(C),
915 ArrayRef<Type*>(Params, 2),
916 /*isVarArg=*/false);
917 Constant *Callee = M->getOrInsertFunction(Name, FTy);
Michael Gottesman60f6b282013-03-29 05:13:07 +0000918
919 IRBuilder<> Builder(BB, BB->getFirstInsertionPt());
920
Michael Gottesmancd4de0f2013-03-26 00:42:09 +0000921 Value *PtrName;
922 StringRef Tmp = Ptr->getName();
923 if (0 == (PtrName = M->getGlobalVariable(Tmp, true))) {
924 Value *ActualPtrName = Builder.CreateGlobalStringPtr(Tmp,
925 Tmp + "_STR");
926 PtrName = new GlobalVariable(*M, I8X, true, GlobalVariable::InternalLinkage,
Michael Gottesman60f6b282013-03-29 05:13:07 +0000927 cast<Constant>(ActualPtrName), Tmp);
Michael Gottesmancd4de0f2013-03-26 00:42:09 +0000928 }
929
930 Value *S;
931 std::string SeqStr = SequenceToString(Seq);
932 if (0 == (S = M->getGlobalVariable(SeqStr, true))) {
933 Value *ActualPtrName = Builder.CreateGlobalStringPtr(SeqStr,
934 SeqStr + "_STR");
935 S = new GlobalVariable(*M, I8X, true, GlobalVariable::InternalLinkage,
936 cast<Constant>(ActualPtrName), SeqStr);
937 }
938
939 Builder.CreateCall2(Callee, PtrName, S);
940}
941
942/// Add to the end of the basic block llvm.ptr.annotations which show the state
943/// of the pointer at the bottom of the basic block.
944static void GenerateARCBBTerminatorAnnotation(const char *Name, BasicBlock *BB,
945 Value *Ptr, Sequence Seq) {
Michael Gottesman4e88ce62013-04-17 21:59:41 +0000946 // If we have a target identifier, make sure that we match it before emitting
947 // an annotation.
948 if(!ARCAnnotationTargetIdentifier.empty() &&
949 !Ptr->getName().equals(ARCAnnotationTargetIdentifier))
950 return;
Michael Gottesman9e518132013-04-18 04:34:11 +0000951
Michael Gottesmancd4de0f2013-03-26 00:42:09 +0000952 Module *M = BB->getParent()->getParent();
953 LLVMContext &C = M->getContext();
954 Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C));
955 Type *I8XX = PointerType::getUnqual(I8X);
956 Type *Params[] = {I8XX, I8XX};
957 FunctionType *FTy = FunctionType::get(Type::getVoidTy(C),
958 ArrayRef<Type*>(Params, 2),
959 /*isVarArg=*/false);
960 Constant *Callee = M->getOrInsertFunction(Name, FTy);
Michael Gottesman60f6b282013-03-29 05:13:07 +0000961
962 IRBuilder<> Builder(BB, llvm::prior(BB->end()));
963
Michael Gottesmancd4de0f2013-03-26 00:42:09 +0000964 Value *PtrName;
965 StringRef Tmp = Ptr->getName();
966 if (0 == (PtrName = M->getGlobalVariable(Tmp, true))) {
967 Value *ActualPtrName = Builder.CreateGlobalStringPtr(Tmp,
968 Tmp + "_STR");
969 PtrName = new GlobalVariable(*M, I8X, true, GlobalVariable::InternalLinkage,
Michael Gottesman60f6b282013-03-29 05:13:07 +0000970 cast<Constant>(ActualPtrName), Tmp);
Michael Gottesmancd4de0f2013-03-26 00:42:09 +0000971 }
972
973 Value *S;
974 std::string SeqStr = SequenceToString(Seq);
975 if (0 == (S = M->getGlobalVariable(SeqStr, true))) {
976 Value *ActualPtrName = Builder.CreateGlobalStringPtr(SeqStr,
977 SeqStr + "_STR");
978 S = new GlobalVariable(*M, I8X, true, GlobalVariable::InternalLinkage,
979 cast<Constant>(ActualPtrName), SeqStr);
980 }
Michael Gottesman60f6b282013-03-29 05:13:07 +0000981 Builder.CreateCall2(Callee, PtrName, S);
Michael Gottesmancd4de0f2013-03-26 00:42:09 +0000982}
983
Michael Gottesman81b1d432013-03-26 00:42:04 +0000984/// Adds a source annotation to pointer and a state change annotation to Inst
985/// referencing the source annotation and the old/new state of pointer.
986static void GenerateARCAnnotation(unsigned InstMDId,
987 unsigned PtrMDId,
988 Instruction *Inst,
989 Value *Ptr,
990 Sequence OldSeq,
991 Sequence NewSeq) {
992 if (EnableARCAnnotations) {
Michael Gottesman4e88ce62013-04-17 21:59:41 +0000993 // If we have a target identifier, make sure that we match it before
994 // emitting an annotation.
995 if(!ARCAnnotationTargetIdentifier.empty() &&
996 !Ptr->getName().equals(ARCAnnotationTargetIdentifier))
997 return;
Michael Gottesman9e518132013-04-18 04:34:11 +0000998
Michael Gottesman81b1d432013-03-26 00:42:04 +0000999 // First generate the source annotation on our pointer. This will return an
1000 // MDString* if Ptr actually comes from an instruction implying we can put
1001 // in a source annotation. If AppendMDNodeToSourcePtr returns 0 (i.e. NULL),
1002 // then we know that our pointer is from an Argument so we put a reference
1003 // to the argument number.
1004 //
1005 // The point of this is to make it easy for the
1006 // llvm-arc-annotation-processor tool to cross reference where the source
1007 // pointer is in the LLVM IR since the LLVM IR parser does not submit such
1008 // information via debug info for backends to use (since why would anyone
1009 // need such a thing from LLVM IR besides in non standard cases
1010 // [i.e. this]).
1011 MDString *SourcePtrMDNode =
1012 AppendMDNodeToSourcePtr(PtrMDId, Ptr);
1013 AppendMDNodeToInstForPtr(InstMDId, Inst, Ptr, SourcePtrMDNode, OldSeq,
1014 NewSeq);
1015 }
1016}
1017
1018// The actual interface for accessing the above functionality is defined via
1019// some simple macros which are defined below. We do this so that the user does
1020// not need to pass in what metadata id is needed resulting in cleaner code and
1021// additionally since it provides an easy way to conditionally no-op all
1022// annotation support in a non-debug build.
1023
1024/// Use this macro to annotate a sequence state change when processing
1025/// instructions bottom up,
1026#define ANNOTATE_BOTTOMUP(inst, ptr, old, new) \
1027 GenerateARCAnnotation(ARCAnnotationBottomUpMDKind, \
1028 ARCAnnotationProvenanceSourceMDKind, (inst), \
1029 const_cast<Value*>(ptr), (old), (new))
1030/// Use this macro to annotate a sequence state change when processing
1031/// instructions top down.
1032#define ANNOTATE_TOPDOWN(inst, ptr, old, new) \
1033 GenerateARCAnnotation(ARCAnnotationTopDownMDKind, \
1034 ARCAnnotationProvenanceSourceMDKind, (inst), \
1035 const_cast<Value*>(ptr), (old), (new))
1036
Michael Gottesman43e7e002013-04-03 22:41:59 +00001037#define ANNOTATE_BB(_states, _bb, _name, _type, _direction) \
1038 do { \
Michael Gottesman89279f82013-04-05 18:10:41 +00001039 if (EnableARCAnnotations) { \
1040 for(BBState::ptr_const_iterator I = (_states)._direction##_ptr_begin(), \
Michael Gottesman43e7e002013-04-03 22:41:59 +00001041 E = (_states)._direction##_ptr_end(); I != E; ++I) { \
Michael Gottesman89279f82013-04-05 18:10:41 +00001042 Value *Ptr = const_cast<Value*>(I->first); \
1043 Sequence Seq = I->second.GetSeq(); \
1044 GenerateARCBB ## _type ## Annotation(_name, (_bb), Ptr, Seq); \
1045 } \
Michael Gottesman43e7e002013-04-03 22:41:59 +00001046 } \
Michael Gottesman89279f82013-04-05 18:10:41 +00001047 } while (0)
Michael Gottesman43e7e002013-04-03 22:41:59 +00001048
Michael Gottesman89279f82013-04-05 18:10:41 +00001049#define ANNOTATE_BOTTOMUP_BBSTART(_states, _basicblock) \
Michael Gottesman43e7e002013-04-03 22:41:59 +00001050 ANNOTATE_BB(_states, _basicblock, "llvm.arc.annotation.bottomup.bbstart", \
1051 Entrance, bottom_up)
Michael Gottesman89279f82013-04-05 18:10:41 +00001052#define ANNOTATE_BOTTOMUP_BBEND(_states, _basicblock) \
1053 ANNOTATE_BB(_states, _basicblock, "llvm.arc.annotation.bottomup.bbend", \
Michael Gottesman43e7e002013-04-03 22:41:59 +00001054 Terminator, bottom_up)
Michael Gottesman89279f82013-04-05 18:10:41 +00001055#define ANNOTATE_TOPDOWN_BBSTART(_states, _basicblock) \
1056 ANNOTATE_BB(_states, _basicblock, "llvm.arc.annotation.topdown.bbstart", \
Michael Gottesman43e7e002013-04-03 22:41:59 +00001057 Entrance, top_down)
Michael Gottesman89279f82013-04-05 18:10:41 +00001058#define ANNOTATE_TOPDOWN_BBEND(_states, _basicblock) \
1059 ANNOTATE_BB(_states, _basicblock, "llvm.arc.annotation.topdown.bbend", \
Michael Gottesman43e7e002013-04-03 22:41:59 +00001060 Terminator, top_down)
1061
Michael Gottesman81b1d432013-03-26 00:42:04 +00001062#else // !ARC_ANNOTATION
1063// If annotations are off, noop.
1064#define ANNOTATE_BOTTOMUP(inst, ptr, old, new)
1065#define ANNOTATE_TOPDOWN(inst, ptr, old, new)
Michael Gottesman43e7e002013-04-03 22:41:59 +00001066#define ANNOTATE_BOTTOMUP_BBSTART(states, basicblock)
1067#define ANNOTATE_BOTTOMUP_BBEND(states, basicblock)
1068#define ANNOTATE_TOPDOWN_BBSTART(states, basicblock)
1069#define ANNOTATE_TOPDOWN_BBEND(states, basicblock)
Michael Gottesman81b1d432013-03-26 00:42:04 +00001070#endif // !ARC_ANNOTATION
1071
John McCalld935e9c2011-06-15 23:37:01 +00001072namespace {
Michael Gottesman97e3df02013-01-14 00:35:14 +00001073 /// \brief The main ARC optimization pass.
John McCalld935e9c2011-06-15 23:37:01 +00001074 class ObjCARCOpt : public FunctionPass {
1075 bool Changed;
1076 ProvenanceAnalysis PA;
Michael Gottesman14acfac2013-07-06 01:39:23 +00001077 ARCRuntimeEntryPoints EP;
John McCalld935e9c2011-06-15 23:37:01 +00001078
Michael Gottesman5a91bbf2013-05-24 20:44:02 +00001079 // This is used to track if a pointer is stored into an alloca.
1080 DenseSet<const Value *> MultiOwnersSet;
1081
Michael Gottesman97e3df02013-01-14 00:35:14 +00001082 /// A flag indicating whether this optimization pass should run.
Dan Gohmanceaac7c2011-06-20 23:20:43 +00001083 bool Run;
1084
Michael Gottesman97e3df02013-01-14 00:35:14 +00001085 /// Flags which determine whether each of the interesting runtine functions
1086 /// is in fact used in the current function.
John McCalld935e9c2011-06-15 23:37:01 +00001087 unsigned UsedInThisFunction;
1088
Michael Gottesman97e3df02013-01-14 00:35:14 +00001089 /// The Metadata Kind for clang.imprecise_release metadata.
John McCalld935e9c2011-06-15 23:37:01 +00001090 unsigned ImpreciseReleaseMDKind;
1091
Michael Gottesman97e3df02013-01-14 00:35:14 +00001092 /// The Metadata Kind for clang.arc.copy_on_escape metadata.
Dan Gohmana7107f92011-10-17 22:53:25 +00001093 unsigned CopyOnEscapeMDKind;
1094
Michael Gottesman97e3df02013-01-14 00:35:14 +00001095 /// The Metadata Kind for clang.arc.no_objc_arc_exceptions metadata.
Dan Gohman0155f302012-02-17 18:59:53 +00001096 unsigned NoObjCARCExceptionsMDKind;
1097
Michael Gottesman81b1d432013-03-26 00:42:04 +00001098#ifdef ARC_ANNOTATIONS
1099 /// The Metadata Kind for llvm.arc.annotation.bottomup metadata.
1100 unsigned ARCAnnotationBottomUpMDKind;
1101 /// The Metadata Kind for llvm.arc.annotation.topdown metadata.
1102 unsigned ARCAnnotationTopDownMDKind;
1103 /// The Metadata Kind for llvm.arc.annotation.provenancesource metadata.
1104 unsigned ARCAnnotationProvenanceSourceMDKind;
1105#endif // ARC_ANNOATIONS
1106
John McCalld935e9c2011-06-15 23:37:01 +00001107 bool OptimizeRetainRVCall(Function &F, Instruction *RetainRV);
Michael Gottesman556ff612013-01-12 01:25:19 +00001108 void OptimizeAutoreleaseRVCall(Function &F, Instruction *AutoreleaseRV,
1109 InstructionClass &Class);
John McCalld935e9c2011-06-15 23:37:01 +00001110 void OptimizeIndividualCalls(Function &F);
1111
1112 void CheckForCFGHazards(const BasicBlock *BB,
1113 DenseMap<const BasicBlock *, BBState> &BBStates,
1114 BBState &MyStates) const;
Dan Gohman817a7c62012-03-22 18:24:56 +00001115 bool VisitInstructionBottomUp(Instruction *Inst,
Dan Gohman5c70fad2012-03-23 17:47:54 +00001116 BasicBlock *BB,
Dan Gohman817a7c62012-03-22 18:24:56 +00001117 MapVector<Value *, RRInfo> &Retains,
1118 BBState &MyStates);
John McCalld935e9c2011-06-15 23:37:01 +00001119 bool VisitBottomUp(BasicBlock *BB,
1120 DenseMap<const BasicBlock *, BBState> &BBStates,
1121 MapVector<Value *, RRInfo> &Retains);
Dan Gohman817a7c62012-03-22 18:24:56 +00001122 bool VisitInstructionTopDown(Instruction *Inst,
1123 DenseMap<Value *, RRInfo> &Releases,
1124 BBState &MyStates);
John McCalld935e9c2011-06-15 23:37:01 +00001125 bool VisitTopDown(BasicBlock *BB,
1126 DenseMap<const BasicBlock *, BBState> &BBStates,
1127 DenseMap<Value *, RRInfo> &Releases);
1128 bool Visit(Function &F,
1129 DenseMap<const BasicBlock *, BBState> &BBStates,
1130 MapVector<Value *, RRInfo> &Retains,
1131 DenseMap<Value *, RRInfo> &Releases);
1132
1133 void MoveCalls(Value *Arg, RRInfo &RetainsToMove, RRInfo &ReleasesToMove,
1134 MapVector<Value *, RRInfo> &Retains,
1135 DenseMap<Value *, RRInfo> &Releases,
Dan Gohman6320f522011-07-22 22:29:21 +00001136 SmallVectorImpl<Instruction *> &DeadInsts,
1137 Module *M);
John McCalld935e9c2011-06-15 23:37:01 +00001138
Michael Gottesman9de6f962013-01-22 21:49:00 +00001139 bool ConnectTDBUTraversals(DenseMap<const BasicBlock *, BBState> &BBStates,
1140 MapVector<Value *, RRInfo> &Retains,
1141 DenseMap<Value *, RRInfo> &Releases,
1142 Module *M,
Craig Topperb94011f2013-07-14 04:42:23 +00001143 SmallVectorImpl<Instruction *> &NewRetains,
1144 SmallVectorImpl<Instruction *> &NewReleases,
1145 SmallVectorImpl<Instruction *> &DeadInsts,
Michael Gottesman9de6f962013-01-22 21:49:00 +00001146 RRInfo &RetainsToMove,
1147 RRInfo &ReleasesToMove,
1148 Value *Arg,
1149 bool KnownSafe,
1150 bool &AnyPairsCompletelyEliminated);
1151
John McCalld935e9c2011-06-15 23:37:01 +00001152 bool PerformCodePlacement(DenseMap<const BasicBlock *, BBState> &BBStates,
1153 MapVector<Value *, RRInfo> &Retains,
Dan Gohman6320f522011-07-22 22:29:21 +00001154 DenseMap<Value *, RRInfo> &Releases,
1155 Module *M);
John McCalld935e9c2011-06-15 23:37:01 +00001156
1157 void OptimizeWeakCalls(Function &F);
1158
1159 bool OptimizeSequences(Function &F);
1160
1161 void OptimizeReturns(Function &F);
1162
Michael Gottesman9c118152013-04-29 06:16:57 +00001163#ifndef NDEBUG
1164 void GatherStatistics(Function &F, bool AfterOptimization = false);
1165#endif
1166
John McCalld935e9c2011-06-15 23:37:01 +00001167 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
1168 virtual bool doInitialization(Module &M);
1169 virtual bool runOnFunction(Function &F);
1170 virtual void releaseMemory();
1171
1172 public:
1173 static char ID;
1174 ObjCARCOpt() : FunctionPass(ID) {
1175 initializeObjCARCOptPass(*PassRegistry::getPassRegistry());
1176 }
1177 };
1178}
1179
1180char ObjCARCOpt::ID = 0;
1181INITIALIZE_PASS_BEGIN(ObjCARCOpt,
1182 "objc-arc", "ObjC ARC optimization", false, false)
1183INITIALIZE_PASS_DEPENDENCY(ObjCARCAliasAnalysis)
1184INITIALIZE_PASS_END(ObjCARCOpt,
1185 "objc-arc", "ObjC ARC optimization", false, false)
1186
1187Pass *llvm::createObjCARCOptPass() {
1188 return new ObjCARCOpt();
1189}
1190
1191void ObjCARCOpt::getAnalysisUsage(AnalysisUsage &AU) const {
1192 AU.addRequired<ObjCARCAliasAnalysis>();
1193 AU.addRequired<AliasAnalysis>();
1194 // ARC optimization doesn't currently split critical edges.
1195 AU.setPreservesCFG();
1196}
1197
Michael Gottesman97e3df02013-01-14 00:35:14 +00001198/// Turn objc_retainAutoreleasedReturnValue into objc_retain if the operand is
1199/// not a return value. Or, if it can be paired with an
1200/// objc_autoreleaseReturnValue, delete the pair and return true.
John McCalld935e9c2011-06-15 23:37:01 +00001201bool
1202ObjCARCOpt::OptimizeRetainRVCall(Function &F, Instruction *RetainRV) {
Dan Gohmane3ed2b02012-03-23 18:09:00 +00001203 // Check for the argument being from an immediately preceding call or invoke.
Dan Gohmandae33492012-04-27 18:56:31 +00001204 const Value *Arg = GetObjCArg(RetainRV);
1205 ImmutableCallSite CS(Arg);
1206 if (const Instruction *Call = CS.getInstruction()) {
John McCalld935e9c2011-06-15 23:37:01 +00001207 if (Call->getParent() == RetainRV->getParent()) {
Dan Gohmandae33492012-04-27 18:56:31 +00001208 BasicBlock::const_iterator I = Call;
John McCalld935e9c2011-06-15 23:37:01 +00001209 ++I;
Michael Gottesman65c24812013-03-25 09:27:43 +00001210 while (IsNoopInstruction(I)) ++I;
John McCalld935e9c2011-06-15 23:37:01 +00001211 if (&*I == RetainRV)
1212 return false;
Dan Gohmandae33492012-04-27 18:56:31 +00001213 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(Call)) {
Dan Gohmane3ed2b02012-03-23 18:09:00 +00001214 BasicBlock *RetainRVParent = RetainRV->getParent();
1215 if (II->getNormalDest() == RetainRVParent) {
Dan Gohmandae33492012-04-27 18:56:31 +00001216 BasicBlock::const_iterator I = RetainRVParent->begin();
Michael Gottesman65c24812013-03-25 09:27:43 +00001217 while (IsNoopInstruction(I)) ++I;
Dan Gohmane3ed2b02012-03-23 18:09:00 +00001218 if (&*I == RetainRV)
1219 return false;
1220 }
John McCalld935e9c2011-06-15 23:37:01 +00001221 }
Dan Gohmane3ed2b02012-03-23 18:09:00 +00001222 }
John McCalld935e9c2011-06-15 23:37:01 +00001223
1224 // Check for being preceded by an objc_autoreleaseReturnValue on the same
1225 // pointer. In this case, we can delete the pair.
1226 BasicBlock::iterator I = RetainRV, Begin = RetainRV->getParent()->begin();
1227 if (I != Begin) {
Michael Gottesman65c24812013-03-25 09:27:43 +00001228 do --I; while (I != Begin && IsNoopInstruction(I));
John McCalld935e9c2011-06-15 23:37:01 +00001229 if (GetBasicInstructionClass(I) == IC_AutoreleaseRV &&
1230 GetObjCArg(I) == Arg) {
1231 Changed = true;
1232 ++NumPeeps;
Michael Gottesman10426b52013-01-07 21:26:07 +00001233
Michael Gottesman89279f82013-04-05 18:10:41 +00001234 DEBUG(dbgs() << "Erasing autoreleaseRV,retainRV pair: " << *I << "\n"
1235 << "Erasing " << *RetainRV << "\n");
Michael Gottesman10426b52013-01-07 21:26:07 +00001236
John McCalld935e9c2011-06-15 23:37:01 +00001237 EraseInstruction(I);
1238 EraseInstruction(RetainRV);
1239 return true;
1240 }
1241 }
1242
1243 // Turn it to a plain objc_retain.
1244 Changed = true;
1245 ++NumPeeps;
Michael Gottesman10426b52013-01-07 21:26:07 +00001246
Michael Gottesman89279f82013-04-05 18:10:41 +00001247 DEBUG(dbgs() << "Transforming objc_retainAutoreleasedReturnValue => "
Michael Gottesmandef07bb2013-01-05 17:55:42 +00001248 "objc_retain since the operand is not a return value.\n"
Michael Gottesman89279f82013-04-05 18:10:41 +00001249 "Old = " << *RetainRV << "\n");
Michael Gottesman10426b52013-01-07 21:26:07 +00001250
Michael Gottesman14acfac2013-07-06 01:39:23 +00001251 Constant *NewDecl = EP.get(ARCRuntimeEntryPoints::EPT_Retain);
1252 cast<CallInst>(RetainRV)->setCalledFunction(NewDecl);
Michael Gottesmandef07bb2013-01-05 17:55:42 +00001253
Michael Gottesman89279f82013-04-05 18:10:41 +00001254 DEBUG(dbgs() << "New = " << *RetainRV << "\n");
Michael Gottesmandef07bb2013-01-05 17:55:42 +00001255
John McCalld935e9c2011-06-15 23:37:01 +00001256 return false;
1257}
1258
Michael Gottesman97e3df02013-01-14 00:35:14 +00001259/// Turn objc_autoreleaseReturnValue into objc_autorelease if the result is not
1260/// used as a return value.
John McCalld935e9c2011-06-15 23:37:01 +00001261void
Michael Gottesman556ff612013-01-12 01:25:19 +00001262ObjCARCOpt::OptimizeAutoreleaseRVCall(Function &F, Instruction *AutoreleaseRV,
1263 InstructionClass &Class) {
John McCalld935e9c2011-06-15 23:37:01 +00001264 // Check for a return of the pointer value.
1265 const Value *Ptr = GetObjCArg(AutoreleaseRV);
Dan Gohman10a18d52011-08-12 00:36:31 +00001266 SmallVector<const Value *, 2> Users;
1267 Users.push_back(Ptr);
1268 do {
1269 Ptr = Users.pop_back_val();
1270 for (Value::const_use_iterator UI = Ptr->use_begin(), UE = Ptr->use_end();
1271 UI != UE; ++UI) {
1272 const User *I = *UI;
1273 if (isa<ReturnInst>(I) || GetBasicInstructionClass(I) == IC_RetainRV)
1274 return;
1275 if (isa<BitCastInst>(I))
1276 Users.push_back(I);
1277 }
1278 } while (!Users.empty());
John McCalld935e9c2011-06-15 23:37:01 +00001279
1280 Changed = true;
1281 ++NumPeeps;
Michael Gottesman1bf69082013-01-06 21:07:11 +00001282
Michael Gottesman89279f82013-04-05 18:10:41 +00001283 DEBUG(dbgs() << "Transforming objc_autoreleaseReturnValue => "
Michael Gottesman1bf69082013-01-06 21:07:11 +00001284 "objc_autorelease since its operand is not used as a return "
1285 "value.\n"
Michael Gottesman89279f82013-04-05 18:10:41 +00001286 "Old = " << *AutoreleaseRV << "\n");
Michael Gottesman1bf69082013-01-06 21:07:11 +00001287
Michael Gottesmanc9656fa2013-01-12 01:25:15 +00001288 CallInst *AutoreleaseRVCI = cast<CallInst>(AutoreleaseRV);
Michael Gottesman14acfac2013-07-06 01:39:23 +00001289 Constant *NewDecl = EP.get(ARCRuntimeEntryPoints::EPT_Autorelease);
1290 AutoreleaseRVCI->setCalledFunction(NewDecl);
Michael Gottesmanc9656fa2013-01-12 01:25:15 +00001291 AutoreleaseRVCI->setTailCall(false); // Never tail call objc_autorelease.
Michael Gottesman556ff612013-01-12 01:25:19 +00001292 Class = IC_Autorelease;
Michael Gottesman10426b52013-01-07 21:26:07 +00001293
Michael Gottesman89279f82013-04-05 18:10:41 +00001294 DEBUG(dbgs() << "New: " << *AutoreleaseRV << "\n");
Michael Gottesman10426b52013-01-07 21:26:07 +00001295
John McCalld935e9c2011-06-15 23:37:01 +00001296}
1297
Michael Gottesman97e3df02013-01-14 00:35:14 +00001298/// Visit each call, one at a time, and make simplifications without doing any
1299/// additional analysis.
John McCalld935e9c2011-06-15 23:37:01 +00001300void ObjCARCOpt::OptimizeIndividualCalls(Function &F) {
Michael Gottesman89279f82013-04-05 18:10:41 +00001301 DEBUG(dbgs() << "\n== ObjCARCOpt::OptimizeIndividualCalls ==\n");
John McCalld935e9c2011-06-15 23:37:01 +00001302 // Reset all the flags in preparation for recomputing them.
1303 UsedInThisFunction = 0;
1304
1305 // Visit all objc_* calls in F.
1306 for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
1307 Instruction *Inst = &*I++;
Michael Gottesman3f146e22013-01-01 16:05:48 +00001308
John McCalld935e9c2011-06-15 23:37:01 +00001309 InstructionClass Class = GetBasicInstructionClass(Inst);
1310
Michael Gottesman89279f82013-04-05 18:10:41 +00001311 DEBUG(dbgs() << "Visiting: Class: " << Class << "; " << *Inst << "\n");
Michael Gottesman782e3442013-01-17 18:32:34 +00001312
John McCalld935e9c2011-06-15 23:37:01 +00001313 switch (Class) {
1314 default: break;
1315
1316 // Delete no-op casts. These function calls have special semantics, but
1317 // the semantics are entirely implemented via lowering in the front-end,
1318 // so by the time they reach the optimizer, they are just no-op calls
1319 // which return their argument.
1320 //
1321 // There are gray areas here, as the ability to cast reference-counted
1322 // pointers to raw void* and back allows code to break ARC assumptions,
1323 // however these are currently considered to be unimportant.
1324 case IC_NoopCast:
1325 Changed = true;
1326 ++NumNoops;
Michael Gottesman89279f82013-04-05 18:10:41 +00001327 DEBUG(dbgs() << "Erasing no-op cast: " << *Inst << "\n");
John McCalld935e9c2011-06-15 23:37:01 +00001328 EraseInstruction(Inst);
1329 continue;
1330
1331 // If the pointer-to-weak-pointer is null, it's undefined behavior.
1332 case IC_StoreWeak:
1333 case IC_LoadWeak:
1334 case IC_LoadWeakRetained:
1335 case IC_InitWeak:
1336 case IC_DestroyWeak: {
1337 CallInst *CI = cast<CallInst>(Inst);
Michael Gottesman65c24812013-03-25 09:27:43 +00001338 if (IsNullOrUndef(CI->getArgOperand(0))) {
Dan Gohman670f9372012-04-13 18:57:48 +00001339 Changed = true;
Chris Lattner229907c2011-07-18 04:54:35 +00001340 Type *Ty = CI->getArgOperand(0)->getType();
John McCalld935e9c2011-06-15 23:37:01 +00001341 new StoreInst(UndefValue::get(cast<PointerType>(Ty)->getElementType()),
1342 Constant::getNullValue(Ty),
1343 CI);
Michael Gottesman10426b52013-01-07 21:26:07 +00001344 llvm::Value *NewValue = UndefValue::get(CI->getType());
Michael Gottesman89279f82013-04-05 18:10:41 +00001345 DEBUG(dbgs() << "A null pointer-to-weak-pointer is undefined behavior."
1346 "\nOld = " << *CI << "\nNew = " << *NewValue << "\n");
Michael Gottesmanfec61c02013-01-06 21:54:30 +00001347 CI->replaceAllUsesWith(NewValue);
John McCalld935e9c2011-06-15 23:37:01 +00001348 CI->eraseFromParent();
1349 continue;
1350 }
1351 break;
1352 }
1353 case IC_CopyWeak:
1354 case IC_MoveWeak: {
1355 CallInst *CI = cast<CallInst>(Inst);
Michael Gottesman65c24812013-03-25 09:27:43 +00001356 if (IsNullOrUndef(CI->getArgOperand(0)) ||
1357 IsNullOrUndef(CI->getArgOperand(1))) {
Dan Gohman670f9372012-04-13 18:57:48 +00001358 Changed = true;
Chris Lattner229907c2011-07-18 04:54:35 +00001359 Type *Ty = CI->getArgOperand(0)->getType();
John McCalld935e9c2011-06-15 23:37:01 +00001360 new StoreInst(UndefValue::get(cast<PointerType>(Ty)->getElementType()),
1361 Constant::getNullValue(Ty),
1362 CI);
Michael Gottesmanfec61c02013-01-06 21:54:30 +00001363
1364 llvm::Value *NewValue = UndefValue::get(CI->getType());
Michael Gottesman89279f82013-04-05 18:10:41 +00001365 DEBUG(dbgs() << "A null pointer-to-weak-pointer is undefined behavior."
1366 "\nOld = " << *CI << "\nNew = " << *NewValue << "\n");
Michael Gottesman10426b52013-01-07 21:26:07 +00001367
Michael Gottesmanfec61c02013-01-06 21:54:30 +00001368 CI->replaceAllUsesWith(NewValue);
John McCalld935e9c2011-06-15 23:37:01 +00001369 CI->eraseFromParent();
1370 continue;
1371 }
1372 break;
1373 }
John McCalld935e9c2011-06-15 23:37:01 +00001374 case IC_RetainRV:
1375 if (OptimizeRetainRVCall(F, Inst))
1376 continue;
1377 break;
1378 case IC_AutoreleaseRV:
Michael Gottesman556ff612013-01-12 01:25:19 +00001379 OptimizeAutoreleaseRVCall(F, Inst, Class);
John McCalld935e9c2011-06-15 23:37:01 +00001380 break;
1381 }
1382
Michael Gottesmanb8c88362013-04-03 02:57:24 +00001383 // objc_autorelease(x) -> objc_release(x) if x is otherwise unused.
John McCalld935e9c2011-06-15 23:37:01 +00001384 if (IsAutorelease(Class) && Inst->use_empty()) {
1385 CallInst *Call = cast<CallInst>(Inst);
1386 const Value *Arg = Call->getArgOperand(0);
1387 Arg = FindSingleUseIdentifiedObject(Arg);
1388 if (Arg) {
1389 Changed = true;
1390 ++NumAutoreleases;
1391
1392 // Create the declaration lazily.
1393 LLVMContext &C = Inst->getContext();
Michael Gottesman01df4502013-07-06 01:41:35 +00001394
Michael Gottesman14acfac2013-07-06 01:39:23 +00001395 Constant *Decl = EP.get(ARCRuntimeEntryPoints::EPT_Release);
1396 CallInst *NewCall = CallInst::Create(Decl, Call->getArgOperand(0), "",
1397 Call);
Dmitri Gribenko3238fb72013-05-05 00:40:33 +00001398 NewCall->setMetadata(ImpreciseReleaseMDKind, MDNode::get(C, None));
Michael Gottesman10426b52013-01-07 21:26:07 +00001399
Michael Gottesman89279f82013-04-05 18:10:41 +00001400 DEBUG(dbgs() << "Replacing autorelease{,RV}(x) with objc_release(x) "
1401 "since x is otherwise unused.\nOld: " << *Call << "\nNew: "
1402 << *NewCall << "\n");
Michael Gottesman10426b52013-01-07 21:26:07 +00001403
John McCalld935e9c2011-06-15 23:37:01 +00001404 EraseInstruction(Call);
1405 Inst = NewCall;
1406 Class = IC_Release;
1407 }
1408 }
1409
1410 // For functions which can never be passed stack arguments, add
1411 // a tail keyword.
1412 if (IsAlwaysTail(Class)) {
1413 Changed = true;
Michael Gottesman89279f82013-04-05 18:10:41 +00001414 DEBUG(dbgs() << "Adding tail keyword to function since it can never be "
1415 "passed stack args: " << *Inst << "\n");
John McCalld935e9c2011-06-15 23:37:01 +00001416 cast<CallInst>(Inst)->setTailCall();
1417 }
1418
Michael Gottesmanc9656fa2013-01-12 01:25:15 +00001419 // Ensure that functions that can never have a "tail" keyword due to the
1420 // semantics of ARC truly do not do so.
1421 if (IsNeverTail(Class)) {
1422 Changed = true;
Michael Gottesman89279f82013-04-05 18:10:41 +00001423 DEBUG(dbgs() << "Removing tail keyword from function: " << *Inst <<
Michael Gottesmanc9656fa2013-01-12 01:25:15 +00001424 "\n");
1425 cast<CallInst>(Inst)->setTailCall(false);
1426 }
1427
John McCalld935e9c2011-06-15 23:37:01 +00001428 // Set nounwind as needed.
1429 if (IsNoThrow(Class)) {
1430 Changed = true;
Michael Gottesman89279f82013-04-05 18:10:41 +00001431 DEBUG(dbgs() << "Found no throw class. Setting nounwind on: " << *Inst
1432 << "\n");
John McCalld935e9c2011-06-15 23:37:01 +00001433 cast<CallInst>(Inst)->setDoesNotThrow();
1434 }
1435
1436 if (!IsNoopOnNull(Class)) {
1437 UsedInThisFunction |= 1 << Class;
1438 continue;
1439 }
1440
1441 const Value *Arg = GetObjCArg(Inst);
1442
1443 // ARC calls with null are no-ops. Delete them.
Michael Gottesman65c24812013-03-25 09:27:43 +00001444 if (IsNullOrUndef(Arg)) {
John McCalld935e9c2011-06-15 23:37:01 +00001445 Changed = true;
1446 ++NumNoops;
Michael Gottesman89279f82013-04-05 18:10:41 +00001447 DEBUG(dbgs() << "ARC calls with null are no-ops. Erasing: " << *Inst
1448 << "\n");
John McCalld935e9c2011-06-15 23:37:01 +00001449 EraseInstruction(Inst);
1450 continue;
1451 }
1452
1453 // Keep track of which of retain, release, autorelease, and retain_block
1454 // are actually present in this function.
1455 UsedInThisFunction |= 1 << Class;
1456
1457 // If Arg is a PHI, and one or more incoming values to the
1458 // PHI are null, and the call is control-equivalent to the PHI, and there
1459 // are no relevant side effects between the PHI and the call, the call
1460 // could be pushed up to just those paths with non-null incoming values.
1461 // For now, don't bother splitting critical edges for this.
1462 SmallVector<std::pair<Instruction *, const Value *>, 4> Worklist;
1463 Worklist.push_back(std::make_pair(Inst, Arg));
1464 do {
1465 std::pair<Instruction *, const Value *> Pair = Worklist.pop_back_val();
1466 Inst = Pair.first;
1467 Arg = Pair.second;
1468
1469 const PHINode *PN = dyn_cast<PHINode>(Arg);
1470 if (!PN) continue;
1471
1472 // Determine if the PHI has any null operands, or any incoming
1473 // critical edges.
1474 bool HasNull = false;
1475 bool HasCriticalEdges = false;
1476 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
1477 Value *Incoming =
1478 StripPointerCastsAndObjCCalls(PN->getIncomingValue(i));
Michael Gottesman65c24812013-03-25 09:27:43 +00001479 if (IsNullOrUndef(Incoming))
John McCalld935e9c2011-06-15 23:37:01 +00001480 HasNull = true;
1481 else if (cast<TerminatorInst>(PN->getIncomingBlock(i)->back())
1482 .getNumSuccessors() != 1) {
1483 HasCriticalEdges = true;
1484 break;
1485 }
1486 }
1487 // If we have null operands and no critical edges, optimize.
1488 if (!HasCriticalEdges && HasNull) {
1489 SmallPtrSet<Instruction *, 4> DependingInstructions;
1490 SmallPtrSet<const BasicBlock *, 4> Visited;
1491
1492 // Check that there is nothing that cares about the reference
1493 // count between the call and the phi.
Dan Gohman8478d762012-04-13 00:59:57 +00001494 switch (Class) {
1495 case IC_Retain:
1496 case IC_RetainBlock:
1497 // These can always be moved up.
1498 break;
1499 case IC_Release:
Dan Gohman41375a32012-05-08 23:39:44 +00001500 // These can't be moved across things that care about the retain
1501 // count.
Dan Gohman8478d762012-04-13 00:59:57 +00001502 FindDependencies(NeedsPositiveRetainCount, Arg,
1503 Inst->getParent(), Inst,
1504 DependingInstructions, Visited, PA);
1505 break;
1506 case IC_Autorelease:
1507 // These can't be moved across autorelease pool scope boundaries.
1508 FindDependencies(AutoreleasePoolBoundary, Arg,
1509 Inst->getParent(), Inst,
1510 DependingInstructions, Visited, PA);
1511 break;
1512 case IC_RetainRV:
1513 case IC_AutoreleaseRV:
1514 // Don't move these; the RV optimization depends on the autoreleaseRV
1515 // being tail called, and the retainRV being immediately after a call
1516 // (which might still happen if we get lucky with codegen layout, but
1517 // it's not worth taking the chance).
1518 continue;
1519 default:
1520 llvm_unreachable("Invalid dependence flavor");
1521 }
1522
John McCalld935e9c2011-06-15 23:37:01 +00001523 if (DependingInstructions.size() == 1 &&
1524 *DependingInstructions.begin() == PN) {
1525 Changed = true;
1526 ++NumPartialNoops;
1527 // Clone the call into each predecessor that has a non-null value.
1528 CallInst *CInst = cast<CallInst>(Inst);
Chris Lattner229907c2011-07-18 04:54:35 +00001529 Type *ParamTy = CInst->getArgOperand(0)->getType();
John McCalld935e9c2011-06-15 23:37:01 +00001530 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
1531 Value *Incoming =
1532 StripPointerCastsAndObjCCalls(PN->getIncomingValue(i));
Michael Gottesman65c24812013-03-25 09:27:43 +00001533 if (!IsNullOrUndef(Incoming)) {
John McCalld935e9c2011-06-15 23:37:01 +00001534 CallInst *Clone = cast<CallInst>(CInst->clone());
1535 Value *Op = PN->getIncomingValue(i);
1536 Instruction *InsertPos = &PN->getIncomingBlock(i)->back();
1537 if (Op->getType() != ParamTy)
1538 Op = new BitCastInst(Op, ParamTy, "", InsertPos);
1539 Clone->setArgOperand(0, Op);
1540 Clone->insertBefore(InsertPos);
Michael Gottesmanc189a392013-01-09 19:23:24 +00001541
Michael Gottesman89279f82013-04-05 18:10:41 +00001542 DEBUG(dbgs() << "Cloning "
Michael Gottesmanc189a392013-01-09 19:23:24 +00001543 << *CInst << "\n"
Michael Gottesman89279f82013-04-05 18:10:41 +00001544 "And inserting clone at " << *InsertPos << "\n");
John McCalld935e9c2011-06-15 23:37:01 +00001545 Worklist.push_back(std::make_pair(Clone, Incoming));
1546 }
1547 }
1548 // Erase the original call.
Michael Gottesmanc189a392013-01-09 19:23:24 +00001549 DEBUG(dbgs() << "Erasing: " << *CInst << "\n");
John McCalld935e9c2011-06-15 23:37:01 +00001550 EraseInstruction(CInst);
1551 continue;
1552 }
1553 }
1554 } while (!Worklist.empty());
1555 }
1556}
1557
Michael Gottesman323964c2013-04-18 05:39:45 +00001558/// If we have a top down pointer in the S_Use state, make sure that there are
1559/// no CFG hazards by checking the states of various bottom up pointers.
1560static void CheckForUseCFGHazard(const Sequence SuccSSeq,
1561 const bool SuccSRRIKnownSafe,
1562 PtrState &S,
1563 bool &SomeSuccHasSame,
1564 bool &AllSuccsHaveSame,
Michael Gottesmane67f40c2013-05-24 20:44:05 +00001565 bool &NotAllSeqEqualButKnownSafe,
Michael Gottesman323964c2013-04-18 05:39:45 +00001566 bool &ShouldContinue) {
1567 switch (SuccSSeq) {
1568 case S_CanRelease: {
Michael Gottesman93132252013-06-21 06:59:02 +00001569 if (!S.IsKnownSafe() && !SuccSRRIKnownSafe) {
Michael Gottesman323964c2013-04-18 05:39:45 +00001570 S.ClearSequenceProgress();
1571 break;
1572 }
Michael Gottesman2f294592013-06-21 19:12:36 +00001573 S.SetCFGHazardAfflicted(true);
Michael Gottesman323964c2013-04-18 05:39:45 +00001574 ShouldContinue = true;
1575 break;
1576 }
1577 case S_Use:
1578 SomeSuccHasSame = true;
1579 break;
1580 case S_Stop:
1581 case S_Release:
1582 case S_MovableRelease:
Michael Gottesman93132252013-06-21 06:59:02 +00001583 if (!S.IsKnownSafe() && !SuccSRRIKnownSafe)
Michael Gottesman323964c2013-04-18 05:39:45 +00001584 AllSuccsHaveSame = false;
Michael Gottesmane67f40c2013-05-24 20:44:05 +00001585 else
1586 NotAllSeqEqualButKnownSafe = true;
Michael Gottesman323964c2013-04-18 05:39:45 +00001587 break;
1588 case S_Retain:
1589 llvm_unreachable("bottom-up pointer in retain state!");
1590 case S_None:
1591 llvm_unreachable("This should have been handled earlier.");
1592 }
1593}
1594
1595/// If we have a Top Down pointer in the S_CanRelease state, make sure that
1596/// there are no CFG hazards by checking the states of various bottom up
1597/// pointers.
1598static void CheckForCanReleaseCFGHazard(const Sequence SuccSSeq,
1599 const bool SuccSRRIKnownSafe,
1600 PtrState &S,
1601 bool &SomeSuccHasSame,
Michael Gottesmane67f40c2013-05-24 20:44:05 +00001602 bool &AllSuccsHaveSame,
1603 bool &NotAllSeqEqualButKnownSafe) {
Michael Gottesman323964c2013-04-18 05:39:45 +00001604 switch (SuccSSeq) {
1605 case S_CanRelease:
1606 SomeSuccHasSame = true;
1607 break;
1608 case S_Stop:
1609 case S_Release:
1610 case S_MovableRelease:
1611 case S_Use:
Michael Gottesman93132252013-06-21 06:59:02 +00001612 if (!S.IsKnownSafe() && !SuccSRRIKnownSafe)
Michael Gottesman323964c2013-04-18 05:39:45 +00001613 AllSuccsHaveSame = false;
Michael Gottesmane67f40c2013-05-24 20:44:05 +00001614 else
1615 NotAllSeqEqualButKnownSafe = true;
Michael Gottesman323964c2013-04-18 05:39:45 +00001616 break;
1617 case S_Retain:
1618 llvm_unreachable("bottom-up pointer in retain state!");
1619 case S_None:
1620 llvm_unreachable("This should have been handled earlier.");
1621 }
1622}
1623
Michael Gottesman97e3df02013-01-14 00:35:14 +00001624/// Check for critical edges, loop boundaries, irreducible control flow, or
1625/// other CFG structures where moving code across the edge would result in it
1626/// being executed more.
John McCalld935e9c2011-06-15 23:37:01 +00001627void
1628ObjCARCOpt::CheckForCFGHazards(const BasicBlock *BB,
1629 DenseMap<const BasicBlock *, BBState> &BBStates,
1630 BBState &MyStates) const {
1631 // If any top-down local-use or possible-dec has a succ which is earlier in
1632 // the sequence, forget it.
Dan Gohman55b06742012-03-02 01:13:53 +00001633 for (BBState::ptr_iterator I = MyStates.top_down_ptr_begin(),
Michael Gottesman323964c2013-04-18 05:39:45 +00001634 E = MyStates.top_down_ptr_end(); I != E; ++I) {
1635 PtrState &S = I->second;
1636 const Sequence Seq = I->second.GetSeq();
Dan Gohman0155f302012-02-17 18:59:53 +00001637
Michael Gottesman323964c2013-04-18 05:39:45 +00001638 // We only care about S_Retain, S_CanRelease, and S_Use.
1639 if (Seq == S_None)
1640 continue;
Dan Gohman0155f302012-02-17 18:59:53 +00001641
Michael Gottesman323964c2013-04-18 05:39:45 +00001642 // Make sure that if extra top down states are added in the future that this
1643 // code is updated to handle it.
1644 assert((Seq == S_Retain || Seq == S_CanRelease || Seq == S_Use) &&
1645 "Unknown top down sequence state.");
1646
1647 const Value *Arg = I->first;
1648 const TerminatorInst *TI = cast<TerminatorInst>(&BB->back());
1649 bool SomeSuccHasSame = false;
1650 bool AllSuccsHaveSame = true;
Michael Gottesmane67f40c2013-05-24 20:44:05 +00001651 bool NotAllSeqEqualButKnownSafe = false;
Michael Gottesman323964c2013-04-18 05:39:45 +00001652
1653 succ_const_iterator SI(TI), SE(TI, false);
1654
1655 for (; SI != SE; ++SI) {
1656 // If VisitBottomUp has pointer information for this successor, take
1657 // what we know about it.
1658 const DenseMap<const BasicBlock *, BBState>::iterator BBI =
1659 BBStates.find(*SI);
1660 assert(BBI != BBStates.end());
1661 const PtrState &SuccS = BBI->second.getPtrBottomUpState(Arg);
1662 const Sequence SuccSSeq = SuccS.GetSeq();
1663
1664 // If bottom up, the pointer is in an S_None state, clear the sequence
1665 // progress since the sequence in the bottom up state finished
1666 // suggesting a mismatch in between retains/releases. This is true for
1667 // all three cases that we are handling here: S_Retain, S_Use, and
1668 // S_CanRelease.
1669 if (SuccSSeq == S_None) {
Dan Gohman12130272011-08-12 00:26:31 +00001670 S.ClearSequenceProgress();
Michael Gottesman323964c2013-04-18 05:39:45 +00001671 continue;
1672 }
1673
1674 // If we have S_Use or S_CanRelease, perform our check for cfg hazard
1675 // checks.
Michael Gottesman93132252013-06-21 06:59:02 +00001676 const bool SuccSRRIKnownSafe = SuccS.IsKnownSafe();
Michael Gottesman323964c2013-04-18 05:39:45 +00001677
1678 // *NOTE* We do not use Seq from above here since we are allowing for
1679 // S.GetSeq() to change while we are visiting basic blocks.
1680 switch(S.GetSeq()) {
1681 case S_Use: {
1682 bool ShouldContinue = false;
Michael Gottesmane67f40c2013-05-24 20:44:05 +00001683 CheckForUseCFGHazard(SuccSSeq, SuccSRRIKnownSafe, S, SomeSuccHasSame,
1684 AllSuccsHaveSame, NotAllSeqEqualButKnownSafe,
Michael Gottesman323964c2013-04-18 05:39:45 +00001685 ShouldContinue);
1686 if (ShouldContinue)
1687 continue;
1688 break;
1689 }
1690 case S_CanRelease: {
Michael Gottesmane67f40c2013-05-24 20:44:05 +00001691 CheckForCanReleaseCFGHazard(SuccSSeq, SuccSRRIKnownSafe, S,
1692 SomeSuccHasSame, AllSuccsHaveSame,
1693 NotAllSeqEqualButKnownSafe);
Michael Gottesman323964c2013-04-18 05:39:45 +00001694 break;
1695 }
1696 case S_Retain:
1697 case S_None:
1698 case S_Stop:
1699 case S_Release:
1700 case S_MovableRelease:
1701 break;
1702 }
John McCalld935e9c2011-06-15 23:37:01 +00001703 }
Michael Gottesman323964c2013-04-18 05:39:45 +00001704
1705 // If the state at the other end of any of the successor edges
1706 // matches the current state, require all edges to match. This
1707 // guards against loops in the middle of a sequence.
Michael Gottesmane67f40c2013-05-24 20:44:05 +00001708 if (SomeSuccHasSame && !AllSuccsHaveSame) {
Michael Gottesman323964c2013-04-18 05:39:45 +00001709 S.ClearSequenceProgress();
Michael Gottesmane67f40c2013-05-24 20:44:05 +00001710 } else if (NotAllSeqEqualButKnownSafe) {
1711 // If we would have cleared the state foregoing the fact that we are known
1712 // safe, stop code motion. This is because whether or not it is safe to
1713 // remove RR pairs via KnownSafe is an orthogonal concept to whether we
1714 // are allowed to perform code motion.
Michael Gottesman2f294592013-06-21 19:12:36 +00001715 S.SetCFGHazardAfflicted(true);
Michael Gottesmane67f40c2013-05-24 20:44:05 +00001716 }
Michael Gottesman323964c2013-04-18 05:39:45 +00001717 }
John McCalld935e9c2011-06-15 23:37:01 +00001718}
1719
1720bool
Dan Gohman817a7c62012-03-22 18:24:56 +00001721ObjCARCOpt::VisitInstructionBottomUp(Instruction *Inst,
Dan Gohman5c70fad2012-03-23 17:47:54 +00001722 BasicBlock *BB,
Dan Gohman817a7c62012-03-22 18:24:56 +00001723 MapVector<Value *, RRInfo> &Retains,
1724 BBState &MyStates) {
1725 bool NestingDetected = false;
1726 InstructionClass Class = GetInstructionClass(Inst);
1727 const Value *Arg = 0;
Michael Gottesman79249972013-04-05 23:46:45 +00001728
Michael Gottesman1d8d2572013-04-05 22:54:28 +00001729 DEBUG(dbgs() << "Class: " << Class << "\n");
Michael Gottesman79249972013-04-05 23:46:45 +00001730
Dan Gohman817a7c62012-03-22 18:24:56 +00001731 switch (Class) {
1732 case IC_Release: {
1733 Arg = GetObjCArg(Inst);
1734
1735 PtrState &S = MyStates.getPtrBottomUpState(Arg);
1736
1737 // If we see two releases in a row on the same pointer. If so, make
1738 // a note, and we'll cicle back to revisit it after we've
1739 // hopefully eliminated the second release, which may allow us to
1740 // eliminate the first release too.
1741 // Theoretically we could implement removal of nested retain+release
1742 // pairs by making PtrState hold a stack of states, but this is
1743 // simple and avoids adding overhead for the non-nested case.
Michael Gottesmanaf2113f2013-01-13 07:00:51 +00001744 if (S.GetSeq() == S_Release || S.GetSeq() == S_MovableRelease) {
Michael Gottesman89279f82013-04-05 18:10:41 +00001745 DEBUG(dbgs() << "Found nested releases (i.e. a release pair)\n");
Dan Gohman817a7c62012-03-22 18:24:56 +00001746 NestingDetected = true;
Michael Gottesmanaf2113f2013-01-13 07:00:51 +00001747 }
Dan Gohman817a7c62012-03-22 18:24:56 +00001748
Dan Gohman817a7c62012-03-22 18:24:56 +00001749 MDNode *ReleaseMetadata = Inst->getMetadata(ImpreciseReleaseMDKind);
Michael Gottesman81b1d432013-03-26 00:42:04 +00001750 Sequence NewSeq = ReleaseMetadata ? S_MovableRelease : S_Release;
1751 ANNOTATE_BOTTOMUP(Inst, Arg, S.GetSeq(), NewSeq);
1752 S.ResetSequenceProgress(NewSeq);
Michael Gottesmanf701d3f2013-06-21 07:03:07 +00001753 S.SetReleaseMetadata(ReleaseMetadata);
Michael Gottesman93132252013-06-21 06:59:02 +00001754 S.SetKnownSafe(S.HasKnownPositiveRefCount());
Michael Gottesmanb82a1792013-06-21 07:00:44 +00001755 S.SetTailCallRelease(cast<CallInst>(Inst)->isTailCall());
Michael Gottesman4f6ef112013-06-21 19:44:27 +00001756 S.InsertCall(Inst);
Dan Gohmandf476e52012-09-04 23:16:20 +00001757 S.SetKnownPositiveRefCount();
Dan Gohman817a7c62012-03-22 18:24:56 +00001758 break;
1759 }
1760 case IC_RetainBlock:
Michael Gottesman158fdf62013-03-28 20:11:19 +00001761 // In OptimizeIndividualCalls, we have strength reduced all optimizable
1762 // objc_retainBlocks to objc_retains. Thus at this point any
1763 // objc_retainBlocks that we see are not optimizable.
1764 break;
Dan Gohman817a7c62012-03-22 18:24:56 +00001765 case IC_Retain:
1766 case IC_RetainRV: {
1767 Arg = GetObjCArg(Inst);
1768
1769 PtrState &S = MyStates.getPtrBottomUpState(Arg);
Dan Gohman62079b42012-04-25 00:50:46 +00001770 S.SetKnownPositiveRefCount();
Dan Gohman817a7c62012-03-22 18:24:56 +00001771
Michael Gottesman81b1d432013-03-26 00:42:04 +00001772 Sequence OldSeq = S.GetSeq();
1773 switch (OldSeq) {
Dan Gohman817a7c62012-03-22 18:24:56 +00001774 case S_Stop:
1775 case S_Release:
1776 case S_MovableRelease:
1777 case S_Use:
Michael Gottesman1d8d2572013-04-05 22:54:28 +00001778 // If OldSeq is not S_Use or OldSeq is S_Use and we are tracking an
1779 // imprecise release, clear our reverse insertion points.
Michael Gottesmanf0401182013-06-21 19:12:38 +00001780 if (OldSeq != S_Use || S.IsTrackingImpreciseReleases())
Michael Gottesman4f6ef112013-06-21 19:44:27 +00001781 S.ClearReverseInsertPts();
Dan Gohman817a7c62012-03-22 18:24:56 +00001782 // FALL THROUGH
1783 case S_CanRelease:
1784 // Don't do retain+release tracking for IC_RetainRV, because it's
1785 // better to let it remain as the first instruction after a call.
Michael Gottesmanba648592013-03-28 23:08:44 +00001786 if (Class != IC_RetainRV)
Michael Gottesmane3943d02013-06-21 19:44:30 +00001787 Retains[Inst] = S.GetRRInfo();
Dan Gohman817a7c62012-03-22 18:24:56 +00001788 S.ClearSequenceProgress();
1789 break;
1790 case S_None:
1791 break;
1792 case S_Retain:
1793 llvm_unreachable("bottom-up pointer in retain state!");
1794 }
Michael Gottesman79249972013-04-05 23:46:45 +00001795 ANNOTATE_BOTTOMUP(Inst, Arg, OldSeq, S.GetSeq());
Michael Gottesman31ba23a2013-04-05 22:54:32 +00001796 // A retain moving bottom up can be a use.
1797 break;
Dan Gohman817a7c62012-03-22 18:24:56 +00001798 }
1799 case IC_AutoreleasepoolPop:
1800 // Conservatively, clear MyStates for all known pointers.
1801 MyStates.clearBottomUpPointers();
1802 return NestingDetected;
1803 case IC_AutoreleasepoolPush:
1804 case IC_None:
1805 // These are irrelevant.
1806 return NestingDetected;
Michael Gottesmana76143ee2013-05-13 23:49:42 +00001807 case IC_User:
1808 // If we have a store into an alloca of a pointer we are tracking, the
1809 // pointer has multiple owners implying that we must be more conservative.
1810 //
1811 // This comes up in the context of a pointer being ``KnownSafe''. In the
1812 // presense of a block being initialized, the frontend will emit the
1813 // objc_retain on the original pointer and the release on the pointer loaded
1814 // from the alloca. The optimizer will through the provenance analysis
1815 // realize that the two are related, but since we only require KnownSafe in
1816 // one direction, will match the inner retain on the original pointer with
1817 // the guard release on the original pointer. This is fixed by ensuring that
1818 // in the presense of allocas we only unconditionally remove pointers if
1819 // both our retain and our release are KnownSafe.
1820 if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
1821 if (AreAnyUnderlyingObjectsAnAlloca(SI->getPointerOperand())) {
1822 BBState::ptr_iterator I = MyStates.findPtrBottomUpState(
1823 StripPointerCastsAndObjCCalls(SI->getValueOperand()));
1824 if (I != MyStates.bottom_up_ptr_end())
Michael Gottesman5a91bbf2013-05-24 20:44:02 +00001825 MultiOwnersSet.insert(I->first);
Michael Gottesmana76143ee2013-05-13 23:49:42 +00001826 }
1827 }
1828 break;
Dan Gohman817a7c62012-03-22 18:24:56 +00001829 default:
1830 break;
1831 }
1832
1833 // Consider any other possible effects of this instruction on each
1834 // pointer being tracked.
1835 for (BBState::ptr_iterator MI = MyStates.bottom_up_ptr_begin(),
1836 ME = MyStates.bottom_up_ptr_end(); MI != ME; ++MI) {
1837 const Value *Ptr = MI->first;
1838 if (Ptr == Arg)
1839 continue; // Handled above.
1840 PtrState &S = MI->second;
1841 Sequence Seq = S.GetSeq();
1842
1843 // Check for possible releases.
1844 if (CanAlterRefCount(Inst, Ptr, PA, Class)) {
Michael Gottesman89279f82013-04-05 18:10:41 +00001845 DEBUG(dbgs() << "CanAlterRefCount: Seq: " << Seq << "; " << *Ptr
1846 << "\n");
Michael Gottesman764b1cf2013-03-23 05:46:19 +00001847 S.ClearKnownPositiveRefCount();
Dan Gohman817a7c62012-03-22 18:24:56 +00001848 switch (Seq) {
1849 case S_Use:
1850 S.SetSeq(S_CanRelease);
Michael Gottesman81b1d432013-03-26 00:42:04 +00001851 ANNOTATE_BOTTOMUP(Inst, Ptr, Seq, S.GetSeq());
Dan Gohman817a7c62012-03-22 18:24:56 +00001852 continue;
1853 case S_CanRelease:
1854 case S_Release:
1855 case S_MovableRelease:
1856 case S_Stop:
1857 case S_None:
1858 break;
1859 case S_Retain:
1860 llvm_unreachable("bottom-up pointer in retain state!");
1861 }
1862 }
1863
1864 // Check for possible direct uses.
1865 switch (Seq) {
1866 case S_Release:
1867 case S_MovableRelease:
1868 if (CanUse(Inst, Ptr, PA, Class)) {
Michael Gottesman89279f82013-04-05 18:10:41 +00001869 DEBUG(dbgs() << "CanUse: Seq: " << Seq << "; " << *Ptr
1870 << "\n");
Michael Gottesman4f6ef112013-06-21 19:44:27 +00001871 assert(!S.HasReverseInsertPts());
Dan Gohman5c70fad2012-03-23 17:47:54 +00001872 // If this is an invoke instruction, we're scanning it as part of
1873 // one of its successor blocks, since we can't insert code after it
1874 // in its own block, and we don't want to split critical edges.
1875 if (isa<InvokeInst>(Inst))
Michael Gottesman4f6ef112013-06-21 19:44:27 +00001876 S.InsertReverseInsertPt(BB->getFirstInsertionPt());
Dan Gohman5c70fad2012-03-23 17:47:54 +00001877 else
Michael Gottesman4f6ef112013-06-21 19:44:27 +00001878 S.InsertReverseInsertPt(llvm::next(BasicBlock::iterator(Inst)));
Dan Gohman817a7c62012-03-22 18:24:56 +00001879 S.SetSeq(S_Use);
Michael Gottesman81b1d432013-03-26 00:42:04 +00001880 ANNOTATE_BOTTOMUP(Inst, Ptr, Seq, S_Use);
John McCall20182ac2013-03-22 21:38:36 +00001881 } else if (Seq == S_Release && IsUser(Class)) {
Michael Gottesman89279f82013-04-05 18:10:41 +00001882 DEBUG(dbgs() << "PreciseReleaseUse: Seq: " << Seq << "; " << *Ptr
1883 << "\n");
Dan Gohman817a7c62012-03-22 18:24:56 +00001884 // Non-movable releases depend on any possible objc pointer use.
1885 S.SetSeq(S_Stop);
Michael Gottesman81b1d432013-03-26 00:42:04 +00001886 ANNOTATE_BOTTOMUP(Inst, Ptr, S_Release, S_Stop);
Michael Gottesman4f6ef112013-06-21 19:44:27 +00001887 assert(!S.HasReverseInsertPts());
Dan Gohman5c70fad2012-03-23 17:47:54 +00001888 // As above; handle invoke specially.
1889 if (isa<InvokeInst>(Inst))
Michael Gottesman4f6ef112013-06-21 19:44:27 +00001890 S.InsertReverseInsertPt(BB->getFirstInsertionPt());
Dan Gohman5c70fad2012-03-23 17:47:54 +00001891 else
Michael Gottesman4f6ef112013-06-21 19:44:27 +00001892 S.InsertReverseInsertPt(llvm::next(BasicBlock::iterator(Inst)));
Dan Gohman817a7c62012-03-22 18:24:56 +00001893 }
1894 break;
1895 case S_Stop:
Michael Gottesman81b1d432013-03-26 00:42:04 +00001896 if (CanUse(Inst, Ptr, PA, Class)) {
Michael Gottesman89279f82013-04-05 18:10:41 +00001897 DEBUG(dbgs() << "PreciseStopUse: Seq: " << Seq << "; " << *Ptr
1898 << "\n");
Dan Gohman817a7c62012-03-22 18:24:56 +00001899 S.SetSeq(S_Use);
Michael Gottesman81b1d432013-03-26 00:42:04 +00001900 ANNOTATE_BOTTOMUP(Inst, Ptr, Seq, S_Use);
1901 }
Dan Gohman817a7c62012-03-22 18:24:56 +00001902 break;
1903 case S_CanRelease:
1904 case S_Use:
1905 case S_None:
1906 break;
1907 case S_Retain:
1908 llvm_unreachable("bottom-up pointer in retain state!");
1909 }
1910 }
1911
1912 return NestingDetected;
1913}
1914
1915bool
John McCalld935e9c2011-06-15 23:37:01 +00001916ObjCARCOpt::VisitBottomUp(BasicBlock *BB,
1917 DenseMap<const BasicBlock *, BBState> &BBStates,
1918 MapVector<Value *, RRInfo> &Retains) {
Michael Gottesman89279f82013-04-05 18:10:41 +00001919
1920 DEBUG(dbgs() << "\n== ObjCARCOpt::VisitBottomUp ==\n");
Michael Gottesman79249972013-04-05 23:46:45 +00001921
John McCalld935e9c2011-06-15 23:37:01 +00001922 bool NestingDetected = false;
1923 BBState &MyStates = BBStates[BB];
1924
1925 // Merge the states from each successor to compute the initial state
1926 // for the current block.
Dan Gohman10c82ce2012-08-27 18:31:36 +00001927 BBState::edge_iterator SI(MyStates.succ_begin()),
1928 SE(MyStates.succ_end());
1929 if (SI != SE) {
Dan Gohmanc24c66f2012-04-24 22:53:18 +00001930 const BasicBlock *Succ = *SI;
1931 DenseMap<const BasicBlock *, BBState>::iterator I = BBStates.find(Succ);
1932 assert(I != BBStates.end());
1933 MyStates.InitFromSucc(I->second);
1934 ++SI;
1935 for (; SI != SE; ++SI) {
1936 Succ = *SI;
1937 I = BBStates.find(Succ);
1938 assert(I != BBStates.end());
1939 MyStates.MergeSucc(I->second);
1940 }
Michael Gottesman60f6b282013-03-29 05:13:07 +00001941 }
Michael Gottesmancd4de0f2013-03-26 00:42:09 +00001942
Michael Gottesman43e7e002013-04-03 22:41:59 +00001943 // If ARC Annotations are enabled, output the current state of pointers at the
Michael Gottesmanc2d5bf52013-04-03 23:07:45 +00001944 // bottom of the basic block.
Michael Gottesman43e7e002013-04-03 22:41:59 +00001945 ANNOTATE_BOTTOMUP_BBEND(MyStates, BB);
Michael Gottesmanc2d5bf52013-04-03 23:07:45 +00001946
John McCalld935e9c2011-06-15 23:37:01 +00001947 // Visit all the instructions, bottom-up.
1948 for (BasicBlock::iterator I = BB->end(), E = BB->begin(); I != E; --I) {
1949 Instruction *Inst = llvm::prior(I);
Dan Gohman5c70fad2012-03-23 17:47:54 +00001950
1951 // Invoke instructions are visited as part of their successors (below).
1952 if (isa<InvokeInst>(Inst))
1953 continue;
1954
Michael Gottesman89279f82013-04-05 18:10:41 +00001955 DEBUG(dbgs() << "Visiting " << *Inst << "\n");
Michael Gottesmanaf2113f2013-01-13 07:00:51 +00001956
Dan Gohman5c70fad2012-03-23 17:47:54 +00001957 NestingDetected |= VisitInstructionBottomUp(Inst, BB, Retains, MyStates);
1958 }
1959
Dan Gohmandae33492012-04-27 18:56:31 +00001960 // If there's a predecessor with an invoke, visit the invoke as if it were
1961 // part of this block, since we can't insert code after an invoke in its own
1962 // block, and we don't want to split critical edges.
Dan Gohmanc24c66f2012-04-24 22:53:18 +00001963 for (BBState::edge_iterator PI(MyStates.pred_begin()),
1964 PE(MyStates.pred_end()); PI != PE; ++PI) {
Dan Gohman5c70fad2012-03-23 17:47:54 +00001965 BasicBlock *Pred = *PI;
Dan Gohmandae33492012-04-27 18:56:31 +00001966 if (InvokeInst *II = dyn_cast<InvokeInst>(&Pred->back()))
1967 NestingDetected |= VisitInstructionBottomUp(II, BB, Retains, MyStates);
Dan Gohman817a7c62012-03-22 18:24:56 +00001968 }
John McCalld935e9c2011-06-15 23:37:01 +00001969
Michael Gottesman43e7e002013-04-03 22:41:59 +00001970 // If ARC Annotations are enabled, output the current state of pointers at the
1971 // top of the basic block.
1972 ANNOTATE_BOTTOMUP_BBSTART(MyStates, BB);
Michael Gottesmanc2d5bf52013-04-03 23:07:45 +00001973
Dan Gohman817a7c62012-03-22 18:24:56 +00001974 return NestingDetected;
1975}
John McCalld935e9c2011-06-15 23:37:01 +00001976
Dan Gohman817a7c62012-03-22 18:24:56 +00001977bool
1978ObjCARCOpt::VisitInstructionTopDown(Instruction *Inst,
1979 DenseMap<Value *, RRInfo> &Releases,
1980 BBState &MyStates) {
1981 bool NestingDetected = false;
1982 InstructionClass Class = GetInstructionClass(Inst);
1983 const Value *Arg = 0;
John McCalld935e9c2011-06-15 23:37:01 +00001984
Dan Gohman817a7c62012-03-22 18:24:56 +00001985 switch (Class) {
1986 case IC_RetainBlock:
Michael Gottesman158fdf62013-03-28 20:11:19 +00001987 // In OptimizeIndividualCalls, we have strength reduced all optimizable
1988 // objc_retainBlocks to objc_retains. Thus at this point any
1989 // objc_retainBlocks that we see are not optimizable.
1990 break;
Dan Gohman817a7c62012-03-22 18:24:56 +00001991 case IC_Retain:
1992 case IC_RetainRV: {
1993 Arg = GetObjCArg(Inst);
1994
1995 PtrState &S = MyStates.getPtrTopDownState(Arg);
1996
1997 // Don't do retain+release tracking for IC_RetainRV, because it's
1998 // better to let it remain as the first instruction after a call.
1999 if (Class != IC_RetainRV) {
2000 // If we see two retains in a row on the same pointer. If so, make
John McCalld935e9c2011-06-15 23:37:01 +00002001 // a note, and we'll cicle back to revisit it after we've
Dan Gohman817a7c62012-03-22 18:24:56 +00002002 // hopefully eliminated the second retain, which may allow us to
2003 // eliminate the first retain too.
John McCalld935e9c2011-06-15 23:37:01 +00002004 // Theoretically we could implement removal of nested retain+release
2005 // pairs by making PtrState hold a stack of states, but this is
2006 // simple and avoids adding overhead for the non-nested case.
Dan Gohman817a7c62012-03-22 18:24:56 +00002007 if (S.GetSeq() == S_Retain)
John McCalld935e9c2011-06-15 23:37:01 +00002008 NestingDetected = true;
2009
Michael Gottesman81b1d432013-03-26 00:42:04 +00002010 ANNOTATE_TOPDOWN(Inst, Arg, S.GetSeq(), S_Retain);
Dan Gohman62079b42012-04-25 00:50:46 +00002011 S.ResetSequenceProgress(S_Retain);
Michael Gottesman93132252013-06-21 06:59:02 +00002012 S.SetKnownSafe(S.HasKnownPositiveRefCount());
Michael Gottesman4f6ef112013-06-21 19:44:27 +00002013 S.InsertCall(Inst);
John McCalld935e9c2011-06-15 23:37:01 +00002014 }
John McCalld935e9c2011-06-15 23:37:01 +00002015
Dan Gohmandf476e52012-09-04 23:16:20 +00002016 S.SetKnownPositiveRefCount();
Dan Gohmanf64ff8e2012-07-23 19:27:31 +00002017
2018 // A retain can be a potential use; procede to the generic checking
2019 // code below.
2020 break;
Dan Gohman817a7c62012-03-22 18:24:56 +00002021 }
2022 case IC_Release: {
2023 Arg = GetObjCArg(Inst);
2024
2025 PtrState &S = MyStates.getPtrTopDownState(Arg);
Michael Gottesman764b1cf2013-03-23 05:46:19 +00002026 S.ClearKnownPositiveRefCount();
Michael Gottesman79249972013-04-05 23:46:45 +00002027
Michael Gottesman1d8d2572013-04-05 22:54:28 +00002028 Sequence OldSeq = S.GetSeq();
Michael Gottesman79249972013-04-05 23:46:45 +00002029
Michael Gottesman1d8d2572013-04-05 22:54:28 +00002030 MDNode *ReleaseMetadata = Inst->getMetadata(ImpreciseReleaseMDKind);
Michael Gottesman79249972013-04-05 23:46:45 +00002031
Michael Gottesman1d8d2572013-04-05 22:54:28 +00002032 switch (OldSeq) {
Dan Gohman817a7c62012-03-22 18:24:56 +00002033 case S_Retain:
2034 case S_CanRelease:
Michael Gottesman1d8d2572013-04-05 22:54:28 +00002035 if (OldSeq == S_Retain || ReleaseMetadata != 0)
Michael Gottesman4f6ef112013-06-21 19:44:27 +00002036 S.ClearReverseInsertPts();
Dan Gohman817a7c62012-03-22 18:24:56 +00002037 // FALL THROUGH
2038 case S_Use:
Michael Gottesmanf701d3f2013-06-21 07:03:07 +00002039 S.SetReleaseMetadata(ReleaseMetadata);
Michael Gottesmanb82a1792013-06-21 07:00:44 +00002040 S.SetTailCallRelease(cast<CallInst>(Inst)->isTailCall());
Michael Gottesmane3943d02013-06-21 19:44:30 +00002041 Releases[Inst] = S.GetRRInfo();
Michael Gottesman81b1d432013-03-26 00:42:04 +00002042 ANNOTATE_TOPDOWN(Inst, Arg, S.GetSeq(), S_None);
Dan Gohman817a7c62012-03-22 18:24:56 +00002043 S.ClearSequenceProgress();
2044 break;
2045 case S_None:
2046 break;
2047 case S_Stop:
2048 case S_Release:
2049 case S_MovableRelease:
2050 llvm_unreachable("top-down pointer in release state!");
2051 }
2052 break;
2053 }
2054 case IC_AutoreleasepoolPop:
2055 // Conservatively, clear MyStates for all known pointers.
2056 MyStates.clearTopDownPointers();
2057 return NestingDetected;
2058 case IC_AutoreleasepoolPush:
2059 case IC_None:
2060 // These are irrelevant.
2061 return NestingDetected;
2062 default:
2063 break;
2064 }
2065
2066 // Consider any other possible effects of this instruction on each
2067 // pointer being tracked.
2068 for (BBState::ptr_iterator MI = MyStates.top_down_ptr_begin(),
2069 ME = MyStates.top_down_ptr_end(); MI != ME; ++MI) {
2070 const Value *Ptr = MI->first;
2071 if (Ptr == Arg)
2072 continue; // Handled above.
2073 PtrState &S = MI->second;
2074 Sequence Seq = S.GetSeq();
2075
2076 // Check for possible releases.
2077 if (CanAlterRefCount(Inst, Ptr, PA, Class)) {
Michael Gottesmanbab49e972013-04-05 18:26:08 +00002078 DEBUG(dbgs() << "CanAlterRefCount: Seq: " << Seq << "; " << *Ptr
Michael Gottesman79249972013-04-05 23:46:45 +00002079 << "\n");
Michael Gottesman764b1cf2013-03-23 05:46:19 +00002080 S.ClearKnownPositiveRefCount();
John McCalld935e9c2011-06-15 23:37:01 +00002081 switch (Seq) {
Dan Gohman817a7c62012-03-22 18:24:56 +00002082 case S_Retain:
2083 S.SetSeq(S_CanRelease);
Michael Gottesman81b1d432013-03-26 00:42:04 +00002084 ANNOTATE_TOPDOWN(Inst, Ptr, Seq, S_CanRelease);
Michael Gottesman4f6ef112013-06-21 19:44:27 +00002085 assert(!S.HasReverseInsertPts());
2086 S.InsertReverseInsertPt(Inst);
Dan Gohman817a7c62012-03-22 18:24:56 +00002087
2088 // One call can't cause a transition from S_Retain to S_CanRelease
2089 // and S_CanRelease to S_Use. If we've made the first transition,
2090 // we're done.
2091 continue;
John McCalld935e9c2011-06-15 23:37:01 +00002092 case S_Use:
Dan Gohman817a7c62012-03-22 18:24:56 +00002093 case S_CanRelease:
John McCalld935e9c2011-06-15 23:37:01 +00002094 case S_None:
2095 break;
Dan Gohman817a7c62012-03-22 18:24:56 +00002096 case S_Stop:
2097 case S_Release:
2098 case S_MovableRelease:
2099 llvm_unreachable("top-down pointer in release state!");
John McCalld935e9c2011-06-15 23:37:01 +00002100 }
2101 }
Dan Gohman817a7c62012-03-22 18:24:56 +00002102
2103 // Check for possible direct uses.
2104 switch (Seq) {
2105 case S_CanRelease:
Michael Gottesman81b1d432013-03-26 00:42:04 +00002106 if (CanUse(Inst, Ptr, PA, Class)) {
Michael Gottesmanbab49e972013-04-05 18:26:08 +00002107 DEBUG(dbgs() << "CanUse: Seq: " << Seq << "; " << *Ptr
2108 << "\n");
Dan Gohman817a7c62012-03-22 18:24:56 +00002109 S.SetSeq(S_Use);
Michael Gottesman81b1d432013-03-26 00:42:04 +00002110 ANNOTATE_TOPDOWN(Inst, Ptr, Seq, S_Use);
2111 }
Dan Gohman817a7c62012-03-22 18:24:56 +00002112 break;
2113 case S_Retain:
2114 case S_Use:
2115 case S_None:
2116 break;
2117 case S_Stop:
2118 case S_Release:
2119 case S_MovableRelease:
2120 llvm_unreachable("top-down pointer in release state!");
2121 }
John McCalld935e9c2011-06-15 23:37:01 +00002122 }
2123
2124 return NestingDetected;
2125}
2126
2127bool
2128ObjCARCOpt::VisitTopDown(BasicBlock *BB,
2129 DenseMap<const BasicBlock *, BBState> &BBStates,
2130 DenseMap<Value *, RRInfo> &Releases) {
Michael Gottesman89279f82013-04-05 18:10:41 +00002131 DEBUG(dbgs() << "\n== ObjCARCOpt::VisitTopDown ==\n");
John McCalld935e9c2011-06-15 23:37:01 +00002132 bool NestingDetected = false;
2133 BBState &MyStates = BBStates[BB];
2134
2135 // Merge the states from each predecessor to compute the initial state
2136 // for the current block.
Dan Gohman10c82ce2012-08-27 18:31:36 +00002137 BBState::edge_iterator PI(MyStates.pred_begin()),
2138 PE(MyStates.pred_end());
2139 if (PI != PE) {
Dan Gohmanc24c66f2012-04-24 22:53:18 +00002140 const BasicBlock *Pred = *PI;
2141 DenseMap<const BasicBlock *, BBState>::iterator I = BBStates.find(Pred);
2142 assert(I != BBStates.end());
2143 MyStates.InitFromPred(I->second);
2144 ++PI;
2145 for (; PI != PE; ++PI) {
2146 Pred = *PI;
2147 I = BBStates.find(Pred);
2148 assert(I != BBStates.end());
2149 MyStates.MergePred(I->second);
2150 }
Dan Gohmanc24c66f2012-04-24 22:53:18 +00002151 }
John McCalld935e9c2011-06-15 23:37:01 +00002152
Michael Gottesman43e7e002013-04-03 22:41:59 +00002153 // If ARC Annotations are enabled, output the current state of pointers at the
2154 // top of the basic block.
2155 ANNOTATE_TOPDOWN_BBSTART(MyStates, BB);
Michael Gottesmanc2d5bf52013-04-03 23:07:45 +00002156
John McCalld935e9c2011-06-15 23:37:01 +00002157 // Visit all the instructions, top-down.
2158 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
2159 Instruction *Inst = I;
Michael Gottesmanaf2113f2013-01-13 07:00:51 +00002160
Michael Gottesman89279f82013-04-05 18:10:41 +00002161 DEBUG(dbgs() << "Visiting " << *Inst << "\n");
Michael Gottesmanaf2113f2013-01-13 07:00:51 +00002162
Dan Gohman817a7c62012-03-22 18:24:56 +00002163 NestingDetected |= VisitInstructionTopDown(Inst, Releases, MyStates);
John McCalld935e9c2011-06-15 23:37:01 +00002164 }
Michael Gottesmanc2d5bf52013-04-03 23:07:45 +00002165
Michael Gottesman43e7e002013-04-03 22:41:59 +00002166 // If ARC Annotations are enabled, output the current state of pointers at the
2167 // bottom of the basic block.
2168 ANNOTATE_TOPDOWN_BBEND(MyStates, BB);
Michael Gottesmanc2d5bf52013-04-03 23:07:45 +00002169
Michael Gottesmanffef24f2013-04-17 20:48:01 +00002170#ifdef ARC_ANNOTATIONS
Michael Gottesmanadb921a2013-04-17 21:03:53 +00002171 if (!(EnableARCAnnotations && DisableCheckForCFGHazards))
Michael Gottesmanffef24f2013-04-17 20:48:01 +00002172#endif
John McCalld935e9c2011-06-15 23:37:01 +00002173 CheckForCFGHazards(BB, BBStates, MyStates);
2174 return NestingDetected;
2175}
2176
Dan Gohmana53a12c2011-12-12 19:42:25 +00002177static void
2178ComputePostOrders(Function &F,
2179 SmallVectorImpl<BasicBlock *> &PostOrder,
Dan Gohmanc24c66f2012-04-24 22:53:18 +00002180 SmallVectorImpl<BasicBlock *> &ReverseCFGPostOrder,
2181 unsigned NoObjCARCExceptionsMDKind,
2182 DenseMap<const BasicBlock *, BBState> &BBStates) {
Michael Gottesman97e3df02013-01-14 00:35:14 +00002183 /// The visited set, for doing DFS walks.
Dan Gohmana53a12c2011-12-12 19:42:25 +00002184 SmallPtrSet<BasicBlock *, 16> Visited;
2185
2186 // Do DFS, computing the PostOrder.
2187 SmallPtrSet<BasicBlock *, 16> OnStack;
2188 SmallVector<std::pair<BasicBlock *, succ_iterator>, 16> SuccStack;
Dan Gohmanc24c66f2012-04-24 22:53:18 +00002189
2190 // Functions always have exactly one entry block, and we don't have
2191 // any other block that we treat like an entry block.
Dan Gohmana53a12c2011-12-12 19:42:25 +00002192 BasicBlock *EntryBB = &F.getEntryBlock();
Dan Gohman41375a32012-05-08 23:39:44 +00002193 BBState &MyStates = BBStates[EntryBB];
2194 MyStates.SetAsEntry();
2195 TerminatorInst *EntryTI = cast<TerminatorInst>(&EntryBB->back());
2196 SuccStack.push_back(std::make_pair(EntryBB, succ_iterator(EntryTI)));
Dan Gohmana53a12c2011-12-12 19:42:25 +00002197 Visited.insert(EntryBB);
2198 OnStack.insert(EntryBB);
2199 do {
2200 dfs_next_succ:
Dan Gohmanc24c66f2012-04-24 22:53:18 +00002201 BasicBlock *CurrBB = SuccStack.back().first;
2202 TerminatorInst *TI = cast<TerminatorInst>(&CurrBB->back());
2203 succ_iterator SE(TI, false);
Dan Gohman41375a32012-05-08 23:39:44 +00002204
Dan Gohmanc24c66f2012-04-24 22:53:18 +00002205 while (SuccStack.back().second != SE) {
2206 BasicBlock *SuccBB = *SuccStack.back().second++;
2207 if (Visited.insert(SuccBB)) {
Dan Gohman41375a32012-05-08 23:39:44 +00002208 TerminatorInst *TI = cast<TerminatorInst>(&SuccBB->back());
2209 SuccStack.push_back(std::make_pair(SuccBB, succ_iterator(TI)));
Dan Gohmanc24c66f2012-04-24 22:53:18 +00002210 BBStates[CurrBB].addSucc(SuccBB);
Dan Gohman41375a32012-05-08 23:39:44 +00002211 BBState &SuccStates = BBStates[SuccBB];
2212 SuccStates.addPred(CurrBB);
Dan Gohmanc24c66f2012-04-24 22:53:18 +00002213 OnStack.insert(SuccBB);
Dan Gohmana53a12c2011-12-12 19:42:25 +00002214 goto dfs_next_succ;
2215 }
Dan Gohmanc24c66f2012-04-24 22:53:18 +00002216
2217 if (!OnStack.count(SuccBB)) {
2218 BBStates[CurrBB].addSucc(SuccBB);
2219 BBStates[SuccBB].addPred(CurrBB);
2220 }
Dan Gohmana53a12c2011-12-12 19:42:25 +00002221 }
Dan Gohmanc24c66f2012-04-24 22:53:18 +00002222 OnStack.erase(CurrBB);
2223 PostOrder.push_back(CurrBB);
2224 SuccStack.pop_back();
Dan Gohmana53a12c2011-12-12 19:42:25 +00002225 } while (!SuccStack.empty());
2226
2227 Visited.clear();
2228
Dan Gohmana53a12c2011-12-12 19:42:25 +00002229 // Do reverse-CFG DFS, computing the reverse-CFG PostOrder.
Dan Gohmanc24c66f2012-04-24 22:53:18 +00002230 // Functions may have many exits, and there also blocks which we treat
2231 // as exits due to ignored edges.
2232 SmallVector<std::pair<BasicBlock *, BBState::edge_iterator>, 16> PredStack;
2233 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
2234 BasicBlock *ExitBB = I;
2235 BBState &MyStates = BBStates[ExitBB];
2236 if (!MyStates.isExit())
2237 continue;
2238
Dan Gohmandae33492012-04-27 18:56:31 +00002239 MyStates.SetAsExit();
Dan Gohmanc24c66f2012-04-24 22:53:18 +00002240
2241 PredStack.push_back(std::make_pair(ExitBB, MyStates.pred_begin()));
Dan Gohmana53a12c2011-12-12 19:42:25 +00002242 Visited.insert(ExitBB);
2243 while (!PredStack.empty()) {
2244 reverse_dfs_next_succ:
Dan Gohmanc24c66f2012-04-24 22:53:18 +00002245 BBState::edge_iterator PE = BBStates[PredStack.back().first].pred_end();
2246 while (PredStack.back().second != PE) {
Dan Gohmana53a12c2011-12-12 19:42:25 +00002247 BasicBlock *BB = *PredStack.back().second++;
Dan Gohmana53a12c2011-12-12 19:42:25 +00002248 if (Visited.insert(BB)) {
Dan Gohmanc24c66f2012-04-24 22:53:18 +00002249 PredStack.push_back(std::make_pair(BB, BBStates[BB].pred_begin()));
Dan Gohmana53a12c2011-12-12 19:42:25 +00002250 goto reverse_dfs_next_succ;
2251 }
2252 }
2253 ReverseCFGPostOrder.push_back(PredStack.pop_back_val().first);
2254 }
2255 }
2256}
2257
Michael Gottesman97e3df02013-01-14 00:35:14 +00002258// Visit the function both top-down and bottom-up.
John McCalld935e9c2011-06-15 23:37:01 +00002259bool
2260ObjCARCOpt::Visit(Function &F,
2261 DenseMap<const BasicBlock *, BBState> &BBStates,
2262 MapVector<Value *, RRInfo> &Retains,
2263 DenseMap<Value *, RRInfo> &Releases) {
Dan Gohmana53a12c2011-12-12 19:42:25 +00002264
2265 // Use reverse-postorder traversals, because we magically know that loops
2266 // will be well behaved, i.e. they won't repeatedly call retain on a single
2267 // pointer without doing a release. We can't use the ReversePostOrderTraversal
2268 // class here because we want the reverse-CFG postorder to consider each
2269 // function exit point, and we want to ignore selected cycle edges.
2270 SmallVector<BasicBlock *, 16> PostOrder;
2271 SmallVector<BasicBlock *, 16> ReverseCFGPostOrder;
Dan Gohmanc24c66f2012-04-24 22:53:18 +00002272 ComputePostOrders(F, PostOrder, ReverseCFGPostOrder,
2273 NoObjCARCExceptionsMDKind,
2274 BBStates);
Dan Gohmana53a12c2011-12-12 19:42:25 +00002275
2276 // Use reverse-postorder on the reverse CFG for bottom-up.
John McCalld935e9c2011-06-15 23:37:01 +00002277 bool BottomUpNestingDetected = false;
Dan Gohmanc57b58c2011-08-18 21:27:42 +00002278 for (SmallVectorImpl<BasicBlock *>::const_reverse_iterator I =
Dan Gohmana53a12c2011-12-12 19:42:25 +00002279 ReverseCFGPostOrder.rbegin(), E = ReverseCFGPostOrder.rend();
2280 I != E; ++I)
2281 BottomUpNestingDetected |= VisitBottomUp(*I, BBStates, Retains);
John McCalld935e9c2011-06-15 23:37:01 +00002282
Dan Gohmana53a12c2011-12-12 19:42:25 +00002283 // Use reverse-postorder for top-down.
John McCalld935e9c2011-06-15 23:37:01 +00002284 bool TopDownNestingDetected = false;
Dan Gohmana53a12c2011-12-12 19:42:25 +00002285 for (SmallVectorImpl<BasicBlock *>::const_reverse_iterator I =
2286 PostOrder.rbegin(), E = PostOrder.rend();
2287 I != E; ++I)
2288 TopDownNestingDetected |= VisitTopDown(*I, BBStates, Releases);
John McCalld935e9c2011-06-15 23:37:01 +00002289
2290 return TopDownNestingDetected && BottomUpNestingDetected;
2291}
2292
Michael Gottesman97e3df02013-01-14 00:35:14 +00002293/// Move the calls in RetainsToMove and ReleasesToMove.
John McCalld935e9c2011-06-15 23:37:01 +00002294void ObjCARCOpt::MoveCalls(Value *Arg,
2295 RRInfo &RetainsToMove,
2296 RRInfo &ReleasesToMove,
2297 MapVector<Value *, RRInfo> &Retains,
2298 DenseMap<Value *, RRInfo> &Releases,
Dan Gohman6320f522011-07-22 22:29:21 +00002299 SmallVectorImpl<Instruction *> &DeadInsts,
Michael Gottesman79249972013-04-05 23:46:45 +00002300 Module *M) {
Chris Lattner229907c2011-07-18 04:54:35 +00002301 Type *ArgTy = Arg->getType();
Dan Gohman6320f522011-07-22 22:29:21 +00002302 Type *ParamTy = PointerType::getUnqual(Type::getInt8Ty(ArgTy->getContext()));
Michael Gottesman79249972013-04-05 23:46:45 +00002303
Michael Gottesman89279f82013-04-05 18:10:41 +00002304 DEBUG(dbgs() << "== ObjCARCOpt::MoveCalls ==\n");
Michael Gottesman79249972013-04-05 23:46:45 +00002305
John McCalld935e9c2011-06-15 23:37:01 +00002306 // Insert the new retain and release calls.
2307 for (SmallPtrSet<Instruction *, 2>::const_iterator
2308 PI = ReleasesToMove.ReverseInsertPts.begin(),
2309 PE = ReleasesToMove.ReverseInsertPts.end(); PI != PE; ++PI) {
2310 Instruction *InsertPt = *PI;
2311 Value *MyArg = ArgTy == ParamTy ? Arg :
2312 new BitCastInst(Arg, ParamTy, "", InsertPt);
Michael Gottesman14acfac2013-07-06 01:39:23 +00002313 Constant *Decl = EP.get(ARCRuntimeEntryPoints::EPT_Retain);
2314 CallInst *Call = CallInst::Create(Decl, MyArg, "", InsertPt);
John McCalld935e9c2011-06-15 23:37:01 +00002315 Call->setDoesNotThrow();
Michael Gottesmanba648592013-03-28 23:08:44 +00002316 Call->setTailCall();
Michael Gottesman60f6b282013-03-29 05:13:07 +00002317
Michael Gottesmandf110ac2013-04-21 00:30:50 +00002318 DEBUG(dbgs() << "Inserting new Retain: " << *Call << "\n"
Michael Gottesman89279f82013-04-05 18:10:41 +00002319 "At insertion point: " << *InsertPt << "\n");
John McCalld935e9c2011-06-15 23:37:01 +00002320 }
2321 for (SmallPtrSet<Instruction *, 2>::const_iterator
2322 PI = RetainsToMove.ReverseInsertPts.begin(),
2323 PE = RetainsToMove.ReverseInsertPts.end(); PI != PE; ++PI) {
Dan Gohman5c70fad2012-03-23 17:47:54 +00002324 Instruction *InsertPt = *PI;
2325 Value *MyArg = ArgTy == ParamTy ? Arg :
2326 new BitCastInst(Arg, ParamTy, "", InsertPt);
Michael Gottesman14acfac2013-07-06 01:39:23 +00002327 Constant *Decl = EP.get(ARCRuntimeEntryPoints::EPT_Release);
2328 CallInst *Call = CallInst::Create(Decl, MyArg, "", InsertPt);
Dan Gohman5c70fad2012-03-23 17:47:54 +00002329 // Attach a clang.imprecise_release metadata tag, if appropriate.
2330 if (MDNode *M = ReleasesToMove.ReleaseMetadata)
2331 Call->setMetadata(ImpreciseReleaseMDKind, M);
2332 Call->setDoesNotThrow();
2333 if (ReleasesToMove.IsTailCallRelease)
2334 Call->setTailCall();
Michael Gottesmanc189a392013-01-09 19:23:24 +00002335
Michael Gottesman89279f82013-04-05 18:10:41 +00002336 DEBUG(dbgs() << "Inserting new Release: " << *Call << "\n"
2337 "At insertion point: " << *InsertPt << "\n");
John McCalld935e9c2011-06-15 23:37:01 +00002338 }
2339
2340 // Delete the original retain and release calls.
2341 for (SmallPtrSet<Instruction *, 2>::const_iterator
2342 AI = RetainsToMove.Calls.begin(),
2343 AE = RetainsToMove.Calls.end(); AI != AE; ++AI) {
2344 Instruction *OrigRetain = *AI;
2345 Retains.blot(OrigRetain);
2346 DeadInsts.push_back(OrigRetain);
Michael Gottesman89279f82013-04-05 18:10:41 +00002347 DEBUG(dbgs() << "Deleting retain: " << *OrigRetain << "\n");
John McCalld935e9c2011-06-15 23:37:01 +00002348 }
2349 for (SmallPtrSet<Instruction *, 2>::const_iterator
2350 AI = ReleasesToMove.Calls.begin(),
2351 AE = ReleasesToMove.Calls.end(); AI != AE; ++AI) {
2352 Instruction *OrigRelease = *AI;
2353 Releases.erase(OrigRelease);
2354 DeadInsts.push_back(OrigRelease);
Michael Gottesman89279f82013-04-05 18:10:41 +00002355 DEBUG(dbgs() << "Deleting release: " << *OrigRelease << "\n");
John McCalld935e9c2011-06-15 23:37:01 +00002356 }
Michael Gottesman79249972013-04-05 23:46:45 +00002357
John McCalld935e9c2011-06-15 23:37:01 +00002358}
2359
Michael Gottesman9de6f962013-01-22 21:49:00 +00002360bool
2361ObjCARCOpt::ConnectTDBUTraversals(DenseMap<const BasicBlock *, BBState>
2362 &BBStates,
2363 MapVector<Value *, RRInfo> &Retains,
2364 DenseMap<Value *, RRInfo> &Releases,
2365 Module *M,
Craig Topperb94011f2013-07-14 04:42:23 +00002366 SmallVectorImpl<Instruction *> &NewRetains,
2367 SmallVectorImpl<Instruction *> &NewReleases,
2368 SmallVectorImpl<Instruction *> &DeadInsts,
Michael Gottesman9de6f962013-01-22 21:49:00 +00002369 RRInfo &RetainsToMove,
2370 RRInfo &ReleasesToMove,
2371 Value *Arg,
2372 bool KnownSafe,
2373 bool &AnyPairsCompletelyEliminated) {
2374 // If a pair happens in a region where it is known that the reference count
Michael Gottesmana76143ee2013-05-13 23:49:42 +00002375 // is already incremented, we can similarly ignore possible decrements unless
2376 // we are dealing with a retainable object with multiple provenance sources.
Michael Gottesman9de6f962013-01-22 21:49:00 +00002377 bool KnownSafeTD = true, KnownSafeBU = true;
Michael Gottesmana76143ee2013-05-13 23:49:42 +00002378 bool MultipleOwners = false;
Michael Gottesmane67f40c2013-05-24 20:44:05 +00002379 bool CFGHazardAfflicted = false;
Michael Gottesman9de6f962013-01-22 21:49:00 +00002380
2381 // Connect the dots between the top-down-collected RetainsToMove and
2382 // bottom-up-collected ReleasesToMove to form sets of related calls.
2383 // This is an iterative process so that we connect multiple releases
2384 // to multiple retains if needed.
2385 unsigned OldDelta = 0;
2386 unsigned NewDelta = 0;
2387 unsigned OldCount = 0;
2388 unsigned NewCount = 0;
2389 bool FirstRelease = true;
Michael Gottesman9de6f962013-01-22 21:49:00 +00002390 for (;;) {
2391 for (SmallVectorImpl<Instruction *>::const_iterator
2392 NI = NewRetains.begin(), NE = NewRetains.end(); NI != NE; ++NI) {
2393 Instruction *NewRetain = *NI;
2394 MapVector<Value *, RRInfo>::const_iterator It = Retains.find(NewRetain);
2395 assert(It != Retains.end());
2396 const RRInfo &NewRetainRRI = It->second;
2397 KnownSafeTD &= NewRetainRRI.KnownSafe;
Michael Gottesman5a91bbf2013-05-24 20:44:02 +00002398 MultipleOwners =
2399 MultipleOwners || MultiOwnersSet.count(GetObjCArg(NewRetain));
Michael Gottesman9de6f962013-01-22 21:49:00 +00002400 for (SmallPtrSet<Instruction *, 2>::const_iterator
2401 LI = NewRetainRRI.Calls.begin(),
2402 LE = NewRetainRRI.Calls.end(); LI != LE; ++LI) {
2403 Instruction *NewRetainRelease = *LI;
2404 DenseMap<Value *, RRInfo>::const_iterator Jt =
2405 Releases.find(NewRetainRelease);
2406 if (Jt == Releases.end())
2407 return false;
2408 const RRInfo &NewRetainReleaseRRI = Jt->second;
Michael Gottesman24b2f6f2013-11-05 16:02:40 +00002409
2410 // If the release does not have a reference to the retain as well,
2411 // something happened which is unaccounted for. Do not do anything.
2412 //
2413 // This can happen if we catch an additive overflow during path count
2414 // merging.
2415 if (!NewRetainReleaseRRI.Calls.count(NewRetain))
2416 return false;
2417
Michael Gottesman9de6f962013-01-22 21:49:00 +00002418 if (ReleasesToMove.Calls.insert(NewRetainRelease)) {
Michael Gottesman9e7261c2013-06-07 06:16:49 +00002419
2420 // If we overflow when we compute the path count, don't remove/move
2421 // anything.
2422 const BBState &NRRBBState = BBStates[NewRetainRelease->getParent()];
Michael Gottesmand6ce6cb2013-08-09 23:22:27 +00002423 unsigned PathCount = BBState::OverflowOccurredValue;
Michael Gottesman9e7261c2013-06-07 06:16:49 +00002424 if (NRRBBState.GetAllPathCountWithOverflow(PathCount))
2425 return false;
Michael Gottesmand6ce6cb2013-08-09 23:22:27 +00002426 assert(PathCount != BBState::OverflowOccurredValue &&
2427 "PathCount at this point can not be "
2428 "OverflowOccurredValue.");
Michael Gottesman9e7261c2013-06-07 06:16:49 +00002429 OldDelta -= PathCount;
Michael Gottesman9de6f962013-01-22 21:49:00 +00002430
2431 // Merge the ReleaseMetadata and IsTailCallRelease values.
2432 if (FirstRelease) {
2433 ReleasesToMove.ReleaseMetadata =
2434 NewRetainReleaseRRI.ReleaseMetadata;
2435 ReleasesToMove.IsTailCallRelease =
2436 NewRetainReleaseRRI.IsTailCallRelease;
2437 FirstRelease = false;
2438 } else {
2439 if (ReleasesToMove.ReleaseMetadata !=
2440 NewRetainReleaseRRI.ReleaseMetadata)
2441 ReleasesToMove.ReleaseMetadata = 0;
2442 if (ReleasesToMove.IsTailCallRelease !=
2443 NewRetainReleaseRRI.IsTailCallRelease)
2444 ReleasesToMove.IsTailCallRelease = false;
2445 }
2446
2447 // Collect the optimal insertion points.
2448 if (!KnownSafe)
2449 for (SmallPtrSet<Instruction *, 2>::const_iterator
2450 RI = NewRetainReleaseRRI.ReverseInsertPts.begin(),
2451 RE = NewRetainReleaseRRI.ReverseInsertPts.end();
2452 RI != RE; ++RI) {
2453 Instruction *RIP = *RI;
Michael Gottesman9e7261c2013-06-07 06:16:49 +00002454 if (ReleasesToMove.ReverseInsertPts.insert(RIP)) {
2455 // If we overflow when we compute the path count, don't
2456 // remove/move anything.
2457 const BBState &RIPBBState = BBStates[RIP->getParent()];
Michael Gottesmand6ce6cb2013-08-09 23:22:27 +00002458 PathCount = BBState::OverflowOccurredValue;
Michael Gottesman9e7261c2013-06-07 06:16:49 +00002459 if (RIPBBState.GetAllPathCountWithOverflow(PathCount))
2460 return false;
Michael Gottesmand6ce6cb2013-08-09 23:22:27 +00002461 assert(PathCount != BBState::OverflowOccurredValue &&
2462 "PathCount at this point can not be "
2463 "OverflowOccurredValue.");
Michael Gottesman9e7261c2013-06-07 06:16:49 +00002464 NewDelta -= PathCount;
2465 }
Michael Gottesman9de6f962013-01-22 21:49:00 +00002466 }
2467 NewReleases.push_back(NewRetainRelease);
2468 }
2469 }
2470 }
2471 NewRetains.clear();
2472 if (NewReleases.empty()) break;
2473
2474 // Back the other way.
2475 for (SmallVectorImpl<Instruction *>::const_iterator
2476 NI = NewReleases.begin(), NE = NewReleases.end(); NI != NE; ++NI) {
2477 Instruction *NewRelease = *NI;
2478 DenseMap<Value *, RRInfo>::const_iterator It =
2479 Releases.find(NewRelease);
2480 assert(It != Releases.end());
2481 const RRInfo &NewReleaseRRI = It->second;
2482 KnownSafeBU &= NewReleaseRRI.KnownSafe;
Michael Gottesmane67f40c2013-05-24 20:44:05 +00002483 CFGHazardAfflicted |= NewReleaseRRI.CFGHazardAfflicted;
Michael Gottesman9de6f962013-01-22 21:49:00 +00002484 for (SmallPtrSet<Instruction *, 2>::const_iterator
2485 LI = NewReleaseRRI.Calls.begin(),
2486 LE = NewReleaseRRI.Calls.end(); LI != LE; ++LI) {
2487 Instruction *NewReleaseRetain = *LI;
2488 MapVector<Value *, RRInfo>::const_iterator Jt =
2489 Retains.find(NewReleaseRetain);
2490 if (Jt == Retains.end())
2491 return false;
2492 const RRInfo &NewReleaseRetainRRI = Jt->second;
Michael Gottesman9e7261c2013-06-07 06:16:49 +00002493
Michael Gottesman24b2f6f2013-11-05 16:02:40 +00002494 // If the retain does not have a reference to the release as well,
2495 // something happened which is unaccounted for. Do not do anything.
2496 //
2497 // This can happen if we catch an additive overflow during path count
2498 // merging.
2499 if (!NewReleaseRetainRRI.Calls.count(NewRelease))
2500 return false;
2501
2502 if (RetainsToMove.Calls.insert(NewReleaseRetain)) {
Michael Gottesman9e7261c2013-06-07 06:16:49 +00002503 // If we overflow when we compute the path count, don't remove/move
2504 // anything.
2505 const BBState &NRRBBState = BBStates[NewReleaseRetain->getParent()];
Michael Gottesmand6ce6cb2013-08-09 23:22:27 +00002506 unsigned PathCount = BBState::OverflowOccurredValue;
Michael Gottesman9e7261c2013-06-07 06:16:49 +00002507 if (NRRBBState.GetAllPathCountWithOverflow(PathCount))
2508 return false;
Michael Gottesmand6ce6cb2013-08-09 23:22:27 +00002509 assert(PathCount != BBState::OverflowOccurredValue &&
2510 "PathCount at this point can not be "
2511 "OverflowOccurredValue.");
Michael Gottesman9de6f962013-01-22 21:49:00 +00002512 OldDelta += PathCount;
2513 OldCount += PathCount;
2514
Michael Gottesman9de6f962013-01-22 21:49:00 +00002515 // Collect the optimal insertion points.
2516 if (!KnownSafe)
2517 for (SmallPtrSet<Instruction *, 2>::const_iterator
2518 RI = NewReleaseRetainRRI.ReverseInsertPts.begin(),
2519 RE = NewReleaseRetainRRI.ReverseInsertPts.end();
2520 RI != RE; ++RI) {
2521 Instruction *RIP = *RI;
2522 if (RetainsToMove.ReverseInsertPts.insert(RIP)) {
Michael Gottesman9e7261c2013-06-07 06:16:49 +00002523 // If we overflow when we compute the path count, don't
2524 // remove/move anything.
2525 const BBState &RIPBBState = BBStates[RIP->getParent()];
Michael Gottesmand6ce6cb2013-08-09 23:22:27 +00002526
2527 PathCount = BBState::OverflowOccurredValue;
Michael Gottesman9e7261c2013-06-07 06:16:49 +00002528 if (RIPBBState.GetAllPathCountWithOverflow(PathCount))
2529 return false;
Michael Gottesmand6ce6cb2013-08-09 23:22:27 +00002530 assert(PathCount != BBState::OverflowOccurredValue &&
2531 "PathCount at this point can not be "
2532 "OverflowOccurredValue.");
Michael Gottesman9de6f962013-01-22 21:49:00 +00002533 NewDelta += PathCount;
2534 NewCount += PathCount;
2535 }
2536 }
2537 NewRetains.push_back(NewReleaseRetain);
2538 }
2539 }
2540 }
2541 NewReleases.clear();
2542 if (NewRetains.empty()) break;
2543 }
2544
Michael Gottesmana76143ee2013-05-13 23:49:42 +00002545 // If the pointer is known incremented in 1 direction and we do not have
2546 // MultipleOwners, we can safely remove the retain/releases. Otherwise we need
2547 // to be known safe in both directions.
2548 bool UnconditionallySafe = (KnownSafeTD && KnownSafeBU) ||
2549 ((KnownSafeTD || KnownSafeBU) && !MultipleOwners);
2550 if (UnconditionallySafe) {
Michael Gottesman9de6f962013-01-22 21:49:00 +00002551 RetainsToMove.ReverseInsertPts.clear();
2552 ReleasesToMove.ReverseInsertPts.clear();
2553 NewCount = 0;
2554 } else {
2555 // Determine whether the new insertion points we computed preserve the
2556 // balance of retain and release calls through the program.
2557 // TODO: If the fully aggressive solution isn't valid, try to find a
2558 // less aggressive solution which is.
2559 if (NewDelta != 0)
2560 return false;
Michael Gottesmane67f40c2013-05-24 20:44:05 +00002561
2562 // At this point, we are not going to remove any RR pairs, but we still are
2563 // able to move RR pairs. If one of our pointers is afflicted with
2564 // CFGHazards, we cannot perform such code motion so exit early.
2565 const bool WillPerformCodeMotion = RetainsToMove.ReverseInsertPts.size() ||
2566 ReleasesToMove.ReverseInsertPts.size();
2567 if (CFGHazardAfflicted && WillPerformCodeMotion)
2568 return false;
Michael Gottesman9de6f962013-01-22 21:49:00 +00002569 }
2570
2571 // Determine whether the original call points are balanced in the retain and
2572 // release calls through the program. If not, conservatively don't touch
2573 // them.
2574 // TODO: It's theoretically possible to do code motion in this case, as
2575 // long as the existing imbalances are maintained.
2576 if (OldDelta != 0)
2577 return false;
Michael Gottesman8005ad32013-04-29 06:16:55 +00002578
Michael Gottesmana87bb8f2013-04-29 05:13:13 +00002579#ifdef ARC_ANNOTATIONS
2580 // Do not move calls if ARC annotations are requested.
Michael Gottesman3e3977c2013-04-29 05:25:39 +00002581 if (EnableARCAnnotations)
2582 return false;
Michael Gottesmana87bb8f2013-04-29 05:13:13 +00002583#endif // ARC_ANNOTATIONS
Michael Gottesman9de6f962013-01-22 21:49:00 +00002584
2585 Changed = true;
2586 assert(OldCount != 0 && "Unreachable code?");
2587 NumRRs += OldCount - NewCount;
Michael Gottesman9de6f962013-01-22 21:49:00 +00002588 // Set to true if we completely removed any RR pairs.
Michael Gottesman8b5515f2013-01-22 21:53:43 +00002589 AnyPairsCompletelyEliminated = NewCount == 0;
Michael Gottesman9de6f962013-01-22 21:49:00 +00002590
2591 // We can move calls!
2592 return true;
2593}
2594
Michael Gottesman97e3df02013-01-14 00:35:14 +00002595/// Identify pairings between the retains and releases, and delete and/or move
2596/// them.
John McCalld935e9c2011-06-15 23:37:01 +00002597bool
2598ObjCARCOpt::PerformCodePlacement(DenseMap<const BasicBlock *, BBState>
2599 &BBStates,
2600 MapVector<Value *, RRInfo> &Retains,
Dan Gohman6320f522011-07-22 22:29:21 +00002601 DenseMap<Value *, RRInfo> &Releases,
2602 Module *M) {
Michael Gottesman89279f82013-04-05 18:10:41 +00002603 DEBUG(dbgs() << "\n== ObjCARCOpt::PerformCodePlacement ==\n");
2604
John McCalld935e9c2011-06-15 23:37:01 +00002605 bool AnyPairsCompletelyEliminated = false;
2606 RRInfo RetainsToMove;
2607 RRInfo ReleasesToMove;
2608 SmallVector<Instruction *, 4> NewRetains;
2609 SmallVector<Instruction *, 4> NewReleases;
2610 SmallVector<Instruction *, 8> DeadInsts;
2611
Dan Gohman670f9372012-04-13 18:57:48 +00002612 // Visit each retain.
John McCalld935e9c2011-06-15 23:37:01 +00002613 for (MapVector<Value *, RRInfo>::const_iterator I = Retains.begin(),
Dan Gohman2053a5d2011-09-29 22:25:23 +00002614 E = Retains.end(); I != E; ++I) {
2615 Value *V = I->first;
John McCalld935e9c2011-06-15 23:37:01 +00002616 if (!V) continue; // blotted
2617
2618 Instruction *Retain = cast<Instruction>(V);
Michael Gottesmanc189a392013-01-09 19:23:24 +00002619
Michael Gottesman89279f82013-04-05 18:10:41 +00002620 DEBUG(dbgs() << "Visiting: " << *Retain << "\n");
Michael Gottesmanc189a392013-01-09 19:23:24 +00002621
John McCalld935e9c2011-06-15 23:37:01 +00002622 Value *Arg = GetObjCArg(Retain);
2623
Dan Gohman728db492012-01-13 00:39:07 +00002624 // If the object being released is in static or stack storage, we know it's
John McCalld935e9c2011-06-15 23:37:01 +00002625 // not being managed by ObjC reference counting, so we can delete pairs
2626 // regardless of what possible decrements or uses lie between them.
Dan Gohman728db492012-01-13 00:39:07 +00002627 bool KnownSafe = isa<Constant>(Arg) || isa<AllocaInst>(Arg);
Dan Gohman41375a32012-05-08 23:39:44 +00002628
Dan Gohman56e1cef2011-08-22 17:29:11 +00002629 // A constant pointer can't be pointing to an object on the heap. It may
2630 // be reference-counted, but it won't be deleted.
2631 if (const LoadInst *LI = dyn_cast<LoadInst>(Arg))
2632 if (const GlobalVariable *GV =
2633 dyn_cast<GlobalVariable>(
2634 StripPointerCastsAndObjCCalls(LI->getPointerOperand())))
2635 if (GV->isConstant())
2636 KnownSafe = true;
2637
John McCalld935e9c2011-06-15 23:37:01 +00002638 // Connect the dots between the top-down-collected RetainsToMove and
2639 // bottom-up-collected ReleasesToMove to form sets of related calls.
John McCalld935e9c2011-06-15 23:37:01 +00002640 NewRetains.push_back(Retain);
Michael Gottesman9de6f962013-01-22 21:49:00 +00002641 bool PerformMoveCalls =
2642 ConnectTDBUTraversals(BBStates, Retains, Releases, M, NewRetains,
2643 NewReleases, DeadInsts, RetainsToMove,
2644 ReleasesToMove, Arg, KnownSafe,
2645 AnyPairsCompletelyEliminated);
John McCalld935e9c2011-06-15 23:37:01 +00002646
Michael Gottesman9de6f962013-01-22 21:49:00 +00002647 if (PerformMoveCalls) {
2648 // Ok, everything checks out and we're all set. Let's move/delete some
2649 // code!
2650 MoveCalls(Arg, RetainsToMove, ReleasesToMove,
2651 Retains, Releases, DeadInsts, M);
John McCalld935e9c2011-06-15 23:37:01 +00002652 }
2653
Michael Gottesman9de6f962013-01-22 21:49:00 +00002654 // Clean up state for next retain.
John McCalld935e9c2011-06-15 23:37:01 +00002655 NewReleases.clear();
2656 NewRetains.clear();
2657 RetainsToMove.clear();
2658 ReleasesToMove.clear();
2659 }
2660
2661 // Now that we're done moving everything, we can delete the newly dead
2662 // instructions, as we no longer need them as insert points.
2663 while (!DeadInsts.empty())
2664 EraseInstruction(DeadInsts.pop_back_val());
2665
2666 return AnyPairsCompletelyEliminated;
2667}
2668
Michael Gottesman97e3df02013-01-14 00:35:14 +00002669/// Weak pointer optimizations.
John McCalld935e9c2011-06-15 23:37:01 +00002670void ObjCARCOpt::OptimizeWeakCalls(Function &F) {
Michael Gottesman89279f82013-04-05 18:10:41 +00002671 DEBUG(dbgs() << "\n== ObjCARCOpt::OptimizeWeakCalls ==\n");
Michael Gottesman79249972013-04-05 23:46:45 +00002672
John McCalld935e9c2011-06-15 23:37:01 +00002673 // First, do memdep-style RLE and S2L optimizations. We can't use memdep
2674 // itself because it uses AliasAnalysis and we need to do provenance
2675 // queries instead.
2676 for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
2677 Instruction *Inst = &*I++;
Michael Gottesman3f146e22013-01-01 16:05:48 +00002678
Michael Gottesman89279f82013-04-05 18:10:41 +00002679 DEBUG(dbgs() << "Visiting: " << *Inst << "\n");
Michael Gottesman3f146e22013-01-01 16:05:48 +00002680
John McCalld935e9c2011-06-15 23:37:01 +00002681 InstructionClass Class = GetBasicInstructionClass(Inst);
2682 if (Class != IC_LoadWeak && Class != IC_LoadWeakRetained)
2683 continue;
2684
2685 // Delete objc_loadWeak calls with no users.
2686 if (Class == IC_LoadWeak && Inst->use_empty()) {
2687 Inst->eraseFromParent();
2688 continue;
2689 }
2690
2691 // TODO: For now, just look for an earlier available version of this value
2692 // within the same block. Theoretically, we could do memdep-style non-local
2693 // analysis too, but that would want caching. A better approach would be to
2694 // use the technique that EarlyCSE uses.
2695 inst_iterator Current = llvm::prior(I);
2696 BasicBlock *CurrentBB = Current.getBasicBlockIterator();
2697 for (BasicBlock::iterator B = CurrentBB->begin(),
2698 J = Current.getInstructionIterator();
2699 J != B; --J) {
2700 Instruction *EarlierInst = &*llvm::prior(J);
2701 InstructionClass EarlierClass = GetInstructionClass(EarlierInst);
2702 switch (EarlierClass) {
2703 case IC_LoadWeak:
2704 case IC_LoadWeakRetained: {
2705 // If this is loading from the same pointer, replace this load's value
2706 // with that one.
2707 CallInst *Call = cast<CallInst>(Inst);
2708 CallInst *EarlierCall = cast<CallInst>(EarlierInst);
2709 Value *Arg = Call->getArgOperand(0);
2710 Value *EarlierArg = EarlierCall->getArgOperand(0);
2711 switch (PA.getAA()->alias(Arg, EarlierArg)) {
2712 case AliasAnalysis::MustAlias:
2713 Changed = true;
2714 // If the load has a builtin retain, insert a plain retain for it.
2715 if (Class == IC_LoadWeakRetained) {
Michael Gottesman14acfac2013-07-06 01:39:23 +00002716 Constant *Decl = EP.get(ARCRuntimeEntryPoints::EPT_Retain);
2717 CallInst *CI = CallInst::Create(Decl, EarlierCall, "", Call);
John McCalld935e9c2011-06-15 23:37:01 +00002718 CI->setTailCall();
2719 }
2720 // Zap the fully redundant load.
2721 Call->replaceAllUsesWith(EarlierCall);
2722 Call->eraseFromParent();
2723 goto clobbered;
2724 case AliasAnalysis::MayAlias:
2725 case AliasAnalysis::PartialAlias:
2726 goto clobbered;
2727 case AliasAnalysis::NoAlias:
2728 break;
2729 }
2730 break;
2731 }
2732 case IC_StoreWeak:
2733 case IC_InitWeak: {
2734 // If this is storing to the same pointer and has the same size etc.
2735 // replace this load's value with the stored value.
2736 CallInst *Call = cast<CallInst>(Inst);
2737 CallInst *EarlierCall = cast<CallInst>(EarlierInst);
2738 Value *Arg = Call->getArgOperand(0);
2739 Value *EarlierArg = EarlierCall->getArgOperand(0);
2740 switch (PA.getAA()->alias(Arg, EarlierArg)) {
2741 case AliasAnalysis::MustAlias:
2742 Changed = true;
2743 // If the load has a builtin retain, insert a plain retain for it.
2744 if (Class == IC_LoadWeakRetained) {
Michael Gottesman14acfac2013-07-06 01:39:23 +00002745 Constant *Decl = EP.get(ARCRuntimeEntryPoints::EPT_Retain);
2746 CallInst *CI = CallInst::Create(Decl, EarlierCall, "", Call);
John McCalld935e9c2011-06-15 23:37:01 +00002747 CI->setTailCall();
2748 }
2749 // Zap the fully redundant load.
2750 Call->replaceAllUsesWith(EarlierCall->getArgOperand(1));
2751 Call->eraseFromParent();
2752 goto clobbered;
2753 case AliasAnalysis::MayAlias:
2754 case AliasAnalysis::PartialAlias:
2755 goto clobbered;
2756 case AliasAnalysis::NoAlias:
2757 break;
2758 }
2759 break;
2760 }
2761 case IC_MoveWeak:
2762 case IC_CopyWeak:
2763 // TOOD: Grab the copied value.
2764 goto clobbered;
2765 case IC_AutoreleasepoolPush:
2766 case IC_None:
John McCall20182ac2013-03-22 21:38:36 +00002767 case IC_IntrinsicUser:
John McCalld935e9c2011-06-15 23:37:01 +00002768 case IC_User:
2769 // Weak pointers are only modified through the weak entry points
2770 // (and arbitrary calls, which could call the weak entry points).
2771 break;
2772 default:
2773 // Anything else could modify the weak pointer.
2774 goto clobbered;
2775 }
2776 }
2777 clobbered:;
2778 }
2779
2780 // Then, for each destroyWeak with an alloca operand, check to see if
2781 // the alloca and all its users can be zapped.
2782 for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
2783 Instruction *Inst = &*I++;
2784 InstructionClass Class = GetBasicInstructionClass(Inst);
2785 if (Class != IC_DestroyWeak)
2786 continue;
2787
2788 CallInst *Call = cast<CallInst>(Inst);
2789 Value *Arg = Call->getArgOperand(0);
2790 if (AllocaInst *Alloca = dyn_cast<AllocaInst>(Arg)) {
2791 for (Value::use_iterator UI = Alloca->use_begin(),
2792 UE = Alloca->use_end(); UI != UE; ++UI) {
Dan Gohmandae33492012-04-27 18:56:31 +00002793 const Instruction *UserInst = cast<Instruction>(*UI);
John McCalld935e9c2011-06-15 23:37:01 +00002794 switch (GetBasicInstructionClass(UserInst)) {
2795 case IC_InitWeak:
2796 case IC_StoreWeak:
2797 case IC_DestroyWeak:
2798 continue;
2799 default:
2800 goto done;
2801 }
2802 }
2803 Changed = true;
2804 for (Value::use_iterator UI = Alloca->use_begin(),
2805 UE = Alloca->use_end(); UI != UE; ) {
2806 CallInst *UserInst = cast<CallInst>(*UI++);
Dan Gohman14862c32012-05-18 22:17:29 +00002807 switch (GetBasicInstructionClass(UserInst)) {
2808 case IC_InitWeak:
2809 case IC_StoreWeak:
2810 // These functions return their second argument.
2811 UserInst->replaceAllUsesWith(UserInst->getArgOperand(1));
2812 break;
2813 case IC_DestroyWeak:
2814 // No return value.
2815 break;
2816 default:
Dan Gohman9c97eea02012-05-21 17:41:28 +00002817 llvm_unreachable("alloca really is used!");
Dan Gohman14862c32012-05-18 22:17:29 +00002818 }
John McCalld935e9c2011-06-15 23:37:01 +00002819 UserInst->eraseFromParent();
2820 }
2821 Alloca->eraseFromParent();
2822 done:;
2823 }
2824 }
2825}
2826
Michael Gottesman97e3df02013-01-14 00:35:14 +00002827/// Identify program paths which execute sequences of retains and releases which
2828/// can be eliminated.
John McCalld935e9c2011-06-15 23:37:01 +00002829bool ObjCARCOpt::OptimizeSequences(Function &F) {
Michael Gottesman740db972013-05-23 02:35:21 +00002830 // Releases, Retains - These are used to store the results of the main flow
2831 // analysis. These use Value* as the key instead of Instruction* so that the
2832 // map stays valid when we get around to rewriting code and calls get
2833 // replaced by arguments.
John McCalld935e9c2011-06-15 23:37:01 +00002834 DenseMap<Value *, RRInfo> Releases;
2835 MapVector<Value *, RRInfo> Retains;
2836
Michael Gottesman740db972013-05-23 02:35:21 +00002837 // This is used during the traversal of the function to track the
2838 // states for each identified object at each block.
John McCalld935e9c2011-06-15 23:37:01 +00002839 DenseMap<const BasicBlock *, BBState> BBStates;
2840
2841 // Analyze the CFG of the function, and all instructions.
2842 bool NestingDetected = Visit(F, BBStates, Retains, Releases);
2843
2844 // Transform.
Michael Gottesman5a91bbf2013-05-24 20:44:02 +00002845 bool AnyPairsCompletelyEliminated = PerformCodePlacement(BBStates, Retains,
2846 Releases,
2847 F.getParent());
2848
2849 // Cleanup.
2850 MultiOwnersSet.clear();
2851
2852 return AnyPairsCompletelyEliminated && NestingDetected;
John McCalld935e9c2011-06-15 23:37:01 +00002853}
2854
Michael Gottesmanc2d5bf52013-04-03 23:07:45 +00002855/// Check if there is a dependent call earlier that does not have anything in
2856/// between the Retain and the call that can affect the reference count of their
2857/// shared pointer argument. Note that Retain need not be in BB.
Michael Gottesman54dc7fd2013-04-03 23:04:28 +00002858static bool
2859HasSafePathToPredecessorCall(const Value *Arg, Instruction *Retain,
2860 SmallPtrSet<Instruction *, 4> &DepInsts,
2861 SmallPtrSet<const BasicBlock *, 4> &Visited,
2862 ProvenanceAnalysis &PA) {
2863 FindDependencies(CanChangeRetainCount, Arg, Retain->getParent(), Retain,
2864 DepInsts, Visited, PA);
2865 if (DepInsts.size() != 1)
2866 return false;
Michael Gottesmanc2d5bf52013-04-03 23:07:45 +00002867
Michael Gottesman54dc7fd2013-04-03 23:04:28 +00002868 CallInst *Call =
2869 dyn_cast_or_null<CallInst>(*DepInsts.begin());
Michael Gottesmanc2d5bf52013-04-03 23:07:45 +00002870
Michael Gottesman54dc7fd2013-04-03 23:04:28 +00002871 // Check that the pointer is the return value of the call.
2872 if (!Call || Arg != Call)
2873 return false;
Michael Gottesmanc2d5bf52013-04-03 23:07:45 +00002874
Michael Gottesman54dc7fd2013-04-03 23:04:28 +00002875 // Check that the call is a regular call.
2876 InstructionClass Class = GetBasicInstructionClass(Call);
2877 if (Class != IC_CallOrUser && Class != IC_Call)
2878 return false;
Michael Gottesmanc2d5bf52013-04-03 23:07:45 +00002879
Michael Gottesman54dc7fd2013-04-03 23:04:28 +00002880 return true;
2881}
2882
Michael Gottesman6908db12013-04-03 23:16:05 +00002883/// Find a dependent retain that precedes the given autorelease for which there
2884/// is nothing in between the two instructions that can affect the ref count of
2885/// Arg.
2886static CallInst *
2887FindPredecessorRetainWithSafePath(const Value *Arg, BasicBlock *BB,
2888 Instruction *Autorelease,
2889 SmallPtrSet<Instruction *, 4> &DepInsts,
2890 SmallPtrSet<const BasicBlock *, 4> &Visited,
2891 ProvenanceAnalysis &PA) {
2892 FindDependencies(CanChangeRetainCount, Arg,
2893 BB, Autorelease, DepInsts, Visited, PA);
2894 if (DepInsts.size() != 1)
2895 return 0;
Michael Gottesman79249972013-04-05 23:46:45 +00002896
Michael Gottesman6908db12013-04-03 23:16:05 +00002897 CallInst *Retain =
2898 dyn_cast_or_null<CallInst>(*DepInsts.begin());
Michael Gottesman79249972013-04-05 23:46:45 +00002899
Michael Gottesman6908db12013-04-03 23:16:05 +00002900 // Check that we found a retain with the same argument.
2901 if (!Retain ||
2902 !IsRetain(GetBasicInstructionClass(Retain)) ||
2903 GetObjCArg(Retain) != Arg) {
2904 return 0;
2905 }
Michael Gottesman79249972013-04-05 23:46:45 +00002906
Michael Gottesman6908db12013-04-03 23:16:05 +00002907 return Retain;
2908}
2909
Michael Gottesman21a4ed32013-04-03 23:39:14 +00002910/// Look for an ``autorelease'' instruction dependent on Arg such that there are
2911/// no instructions dependent on Arg that need a positive ref count in between
2912/// the autorelease and the ret.
2913static CallInst *
2914FindPredecessorAutoreleaseWithSafePath(const Value *Arg, BasicBlock *BB,
2915 ReturnInst *Ret,
2916 SmallPtrSet<Instruction *, 4> &DepInsts,
2917 SmallPtrSet<const BasicBlock *, 4> &V,
2918 ProvenanceAnalysis &PA) {
2919 FindDependencies(NeedsPositiveRetainCount, Arg,
2920 BB, Ret, DepInsts, V, PA);
2921 if (DepInsts.size() != 1)
2922 return 0;
Michael Gottesman79249972013-04-05 23:46:45 +00002923
Michael Gottesman21a4ed32013-04-03 23:39:14 +00002924 CallInst *Autorelease =
2925 dyn_cast_or_null<CallInst>(*DepInsts.begin());
2926 if (!Autorelease)
2927 return 0;
2928 InstructionClass AutoreleaseClass = GetBasicInstructionClass(Autorelease);
2929 if (!IsAutorelease(AutoreleaseClass))
2930 return 0;
2931 if (GetObjCArg(Autorelease) != Arg)
2932 return 0;
Michael Gottesman79249972013-04-05 23:46:45 +00002933
Michael Gottesman21a4ed32013-04-03 23:39:14 +00002934 return Autorelease;
2935}
2936
Michael Gottesman97e3df02013-01-14 00:35:14 +00002937/// Look for this pattern:
Dmitri Gribenko5485acd2012-09-14 14:57:36 +00002938/// \code
John McCalld935e9c2011-06-15 23:37:01 +00002939/// %call = call i8* @something(...)
2940/// %2 = call i8* @objc_retain(i8* %call)
2941/// %3 = call i8* @objc_autorelease(i8* %2)
2942/// ret i8* %3
Dmitri Gribenko5485acd2012-09-14 14:57:36 +00002943/// \endcode
John McCalld935e9c2011-06-15 23:37:01 +00002944/// And delete the retain and autorelease.
John McCalld935e9c2011-06-15 23:37:01 +00002945void ObjCARCOpt::OptimizeReturns(Function &F) {
2946 if (!F.getReturnType()->isPointerTy())
2947 return;
Michael Gottesman79249972013-04-05 23:46:45 +00002948
Michael Gottesman89279f82013-04-05 18:10:41 +00002949 DEBUG(dbgs() << "\n== ObjCARCOpt::OptimizeReturns ==\n");
Michael Gottesman79249972013-04-05 23:46:45 +00002950
John McCalld935e9c2011-06-15 23:37:01 +00002951 SmallPtrSet<Instruction *, 4> DependingInstructions;
2952 SmallPtrSet<const BasicBlock *, 4> Visited;
2953 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI) {
2954 BasicBlock *BB = FI;
2955 ReturnInst *Ret = dyn_cast<ReturnInst>(&BB->back());
Michael Gottesman3f146e22013-01-01 16:05:48 +00002956
Michael Gottesman89279f82013-04-05 18:10:41 +00002957 DEBUG(dbgs() << "Visiting: " << *Ret << "\n");
Michael Gottesman3f146e22013-01-01 16:05:48 +00002958
Michael Gottesman21a4ed32013-04-03 23:39:14 +00002959 if (!Ret)
2960 continue;
Michael Gottesman79249972013-04-05 23:46:45 +00002961
John McCalld935e9c2011-06-15 23:37:01 +00002962 const Value *Arg = StripPointerCastsAndObjCCalls(Ret->getOperand(0));
Michael Gottesman79249972013-04-05 23:46:45 +00002963
Michael Gottesmancdb7c152013-04-21 00:25:04 +00002964 // Look for an ``autorelease'' instruction that is a predecessor of Ret and
Michael Gottesman21a4ed32013-04-03 23:39:14 +00002965 // dependent on Arg such that there are no instructions dependent on Arg
2966 // that need a positive ref count in between the autorelease and Ret.
2967 CallInst *Autorelease =
2968 FindPredecessorAutoreleaseWithSafePath(Arg, BB, Ret,
2969 DependingInstructions, Visited,
2970 PA);
John McCalld935e9c2011-06-15 23:37:01 +00002971 DependingInstructions.clear();
2972 Visited.clear();
Michael Gottesmanfb9ece92013-04-21 00:25:01 +00002973
2974 if (!Autorelease)
2975 continue;
2976
2977 CallInst *Retain =
2978 FindPredecessorRetainWithSafePath(Arg, BB, Autorelease,
2979 DependingInstructions, Visited, PA);
2980 DependingInstructions.clear();
2981 Visited.clear();
2982
2983 if (!Retain)
2984 continue;
2985
2986 // Check that there is nothing that can affect the reference count
2987 // between the retain and the call. Note that Retain need not be in BB.
2988 bool HasSafePathToCall = HasSafePathToPredecessorCall(Arg, Retain,
2989 DependingInstructions,
2990 Visited, PA);
2991 DependingInstructions.clear();
2992 Visited.clear();
2993
2994 if (!HasSafePathToCall)
2995 continue;
2996
2997 // If so, we can zap the retain and autorelease.
2998 Changed = true;
2999 ++NumRets;
3000 DEBUG(dbgs() << "Erasing: " << *Retain << "\nErasing: "
3001 << *Autorelease << "\n");
3002 EraseInstruction(Retain);
3003 EraseInstruction(Autorelease);
John McCalld935e9c2011-06-15 23:37:01 +00003004 }
3005}
3006
Michael Gottesman9c118152013-04-29 06:16:57 +00003007#ifndef NDEBUG
3008void
3009ObjCARCOpt::GatherStatistics(Function &F, bool AfterOptimization) {
3010 llvm::Statistic &NumRetains =
3011 AfterOptimization? NumRetainsAfterOpt : NumRetainsBeforeOpt;
3012 llvm::Statistic &NumReleases =
3013 AfterOptimization? NumReleasesAfterOpt : NumReleasesBeforeOpt;
3014
3015 for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
3016 Instruction *Inst = &*I++;
3017 switch (GetBasicInstructionClass(Inst)) {
3018 default:
3019 break;
3020 case IC_Retain:
3021 ++NumRetains;
3022 break;
3023 case IC_Release:
3024 ++NumReleases;
3025 break;
3026 }
3027 }
3028}
3029#endif
3030
John McCalld935e9c2011-06-15 23:37:01 +00003031bool ObjCARCOpt::doInitialization(Module &M) {
3032 if (!EnableARCOpts)
3033 return false;
3034
Dan Gohman670f9372012-04-13 18:57:48 +00003035 // If nothing in the Module uses ARC, don't do anything.
Dan Gohmanceaac7c2011-06-20 23:20:43 +00003036 Run = ModuleHasARC(M);
3037 if (!Run)
3038 return false;
3039
John McCalld935e9c2011-06-15 23:37:01 +00003040 // Identify the imprecise release metadata kind.
3041 ImpreciseReleaseMDKind =
3042 M.getContext().getMDKindID("clang.imprecise_release");
Dan Gohmana7107f92011-10-17 22:53:25 +00003043 CopyOnEscapeMDKind =
3044 M.getContext().getMDKindID("clang.arc.copy_on_escape");
Dan Gohman0155f302012-02-17 18:59:53 +00003045 NoObjCARCExceptionsMDKind =
3046 M.getContext().getMDKindID("clang.arc.no_objc_arc_exceptions");
Michael Gottesman81b1d432013-03-26 00:42:04 +00003047#ifdef ARC_ANNOTATIONS
3048 ARCAnnotationBottomUpMDKind =
3049 M.getContext().getMDKindID("llvm.arc.annotation.bottomup");
3050 ARCAnnotationTopDownMDKind =
3051 M.getContext().getMDKindID("llvm.arc.annotation.topdown");
3052 ARCAnnotationProvenanceSourceMDKind =
3053 M.getContext().getMDKindID("llvm.arc.annotation.provenancesource");
3054#endif // ARC_ANNOTATIONS
John McCalld935e9c2011-06-15 23:37:01 +00003055
John McCalld935e9c2011-06-15 23:37:01 +00003056 // Intuitively, objc_retain and others are nocapture, however in practice
3057 // they are not, because they return their argument value. And objc_release
Dan Gohmandae33492012-04-27 18:56:31 +00003058 // calls finalizers which can have arbitrary side effects.
John McCalld935e9c2011-06-15 23:37:01 +00003059
Michael Gottesman14acfac2013-07-06 01:39:23 +00003060 // Initialize our runtime entry point cache.
3061 EP.Initialize(&M);
John McCalld935e9c2011-06-15 23:37:01 +00003062
3063 return false;
3064}
3065
3066bool ObjCARCOpt::runOnFunction(Function &F) {
3067 if (!EnableARCOpts)
3068 return false;
3069
Dan Gohmanceaac7c2011-06-20 23:20:43 +00003070 // If nothing in the Module uses ARC, don't do anything.
3071 if (!Run)
3072 return false;
3073
John McCalld935e9c2011-06-15 23:37:01 +00003074 Changed = false;
3075
Michael Gottesman89279f82013-04-05 18:10:41 +00003076 DEBUG(dbgs() << "<<< ObjCARCOpt: Visiting Function: " << F.getName() << " >>>"
3077 "\n");
Michael Gottesmanb24bdef2013-01-12 02:57:16 +00003078
John McCalld935e9c2011-06-15 23:37:01 +00003079 PA.setAA(&getAnalysis<AliasAnalysis>());
3080
Michael Gottesman9fc50b82013-05-13 18:29:07 +00003081#ifndef NDEBUG
3082 if (AreStatisticsEnabled()) {
3083 GatherStatistics(F, false);
3084 }
3085#endif
3086
John McCalld935e9c2011-06-15 23:37:01 +00003087 // This pass performs several distinct transformations. As a compile-time aid
3088 // when compiling code that isn't ObjC, skip these if the relevant ObjC
3089 // library functions aren't declared.
3090
Michael Gottesmancd5b0272013-04-24 22:18:15 +00003091 // Preliminary optimizations. This also computes UsedInThisFunction.
John McCalld935e9c2011-06-15 23:37:01 +00003092 OptimizeIndividualCalls(F);
3093
3094 // Optimizations for weak pointers.
3095 if (UsedInThisFunction & ((1 << IC_LoadWeak) |
3096 (1 << IC_LoadWeakRetained) |
3097 (1 << IC_StoreWeak) |
3098 (1 << IC_InitWeak) |
3099 (1 << IC_CopyWeak) |
3100 (1 << IC_MoveWeak) |
3101 (1 << IC_DestroyWeak)))
3102 OptimizeWeakCalls(F);
3103
3104 // Optimizations for retain+release pairs.
3105 if (UsedInThisFunction & ((1 << IC_Retain) |
3106 (1 << IC_RetainRV) |
3107 (1 << IC_RetainBlock)))
3108 if (UsedInThisFunction & (1 << IC_Release))
3109 // Run OptimizeSequences until it either stops making changes or
3110 // no retain+release pair nesting is detected.
3111 while (OptimizeSequences(F)) {}
3112
3113 // Optimizations if objc_autorelease is used.
Dan Gohman41375a32012-05-08 23:39:44 +00003114 if (UsedInThisFunction & ((1 << IC_Autorelease) |
3115 (1 << IC_AutoreleaseRV)))
John McCalld935e9c2011-06-15 23:37:01 +00003116 OptimizeReturns(F);
3117
Michael Gottesman9c118152013-04-29 06:16:57 +00003118 // Gather statistics after optimization.
3119#ifndef NDEBUG
3120 if (AreStatisticsEnabled()) {
3121 GatherStatistics(F, true);
3122 }
3123#endif
3124
Michael Gottesmanb24bdef2013-01-12 02:57:16 +00003125 DEBUG(dbgs() << "\n");
3126
John McCalld935e9c2011-06-15 23:37:01 +00003127 return Changed;
3128}
3129
3130void ObjCARCOpt::releaseMemory() {
3131 PA.clear();
3132}
3133
Michael Gottesman97e3df02013-01-14 00:35:14 +00003134/// @}
3135///