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