LLVM API Documentation

ObjCARCOpts.cpp
Go to the documentation of this file.
00001 //===- ObjCARCOpts.cpp - ObjC ARC Optimization ----------------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 /// \file
00010 /// This file defines ObjC ARC optimizations. ARC stands for Automatic
00011 /// Reference Counting and is a system for managing reference counts for objects
00012 /// in Objective C.
00013 ///
00014 /// The optimizations performed include elimination of redundant, partially
00015 /// redundant, and inconsequential reference count operations, elimination of
00016 /// redundant weak pointer operations, and numerous minor simplifications.
00017 ///
00018 /// WARNING: This file knows about certain library functions. It recognizes them
00019 /// by name, and hardwires knowledge of their semantics.
00020 ///
00021 /// WARNING: This file knows about how certain Objective-C library functions are
00022 /// used. Naive LLVM IR transformations which would otherwise be
00023 /// behavior-preserving may break these assumptions.
00024 ///
00025 //===----------------------------------------------------------------------===//
00026 
00027 #define DEBUG_TYPE "objc-arc-opts"
00028 #include "ObjCARC.h"
00029 #include "DependencyAnalysis.h"
00030 #include "ObjCARCAliasAnalysis.h"
00031 #include "ProvenanceAnalysis.h"
00032 #include "llvm/ADT/DenseMap.h"
00033 #include "llvm/ADT/DenseSet.h"
00034 #include "llvm/ADT/STLExtras.h"
00035 #include "llvm/ADT/SmallPtrSet.h"
00036 #include "llvm/ADT/Statistic.h"
00037 #include "llvm/IR/IRBuilder.h"
00038 #include "llvm/IR/LLVMContext.h"
00039 #include "llvm/Support/CFG.h"
00040 #include "llvm/Support/Debug.h"
00041 #include "llvm/Support/raw_ostream.h"
00042 
00043 using namespace llvm;
00044 using namespace llvm::objcarc;
00045 
00046 /// \defgroup MiscUtils Miscellaneous utilities that are not ARC specific.
00047 /// @{
00048 
00049 namespace {
00050   /// \brief An associative container with fast insertion-order (deterministic)
00051   /// iteration over its elements. Plus the special blot operation.
00052   template<class KeyT, class ValueT>
00053   class MapVector {
00054     /// Map keys to indices in Vector.
00055     typedef DenseMap<KeyT, size_t> MapTy;
00056     MapTy Map;
00057 
00058     typedef std::vector<std::pair<KeyT, ValueT> > VectorTy;
00059     /// Keys and values.
00060     VectorTy Vector;
00061 
00062   public:
00063     typedef typename VectorTy::iterator iterator;
00064     typedef typename VectorTy::const_iterator const_iterator;
00065     iterator begin() { return Vector.begin(); }
00066     iterator end() { return Vector.end(); }
00067     const_iterator begin() const { return Vector.begin(); }
00068     const_iterator end() const { return Vector.end(); }
00069 
00070 #ifdef XDEBUG
00071     ~MapVector() {
00072       assert(Vector.size() >= Map.size()); // May differ due to blotting.
00073       for (typename MapTy::const_iterator I = Map.begin(), E = Map.end();
00074            I != E; ++I) {
00075         assert(I->second < Vector.size());
00076         assert(Vector[I->second].first == I->first);
00077       }
00078       for (typename VectorTy::const_iterator I = Vector.begin(),
00079            E = Vector.end(); I != E; ++I)
00080         assert(!I->first ||
00081                (Map.count(I->first) &&
00082                 Map[I->first] == size_t(I - Vector.begin())));
00083     }
00084 #endif
00085 
00086     ValueT &operator[](const KeyT &Arg) {
00087       std::pair<typename MapTy::iterator, bool> Pair =
00088         Map.insert(std::make_pair(Arg, size_t(0)));
00089       if (Pair.second) {
00090         size_t Num = Vector.size();
00091         Pair.first->second = Num;
00092         Vector.push_back(std::make_pair(Arg, ValueT()));
00093         return Vector[Num].second;
00094       }
00095       return Vector[Pair.first->second].second;
00096     }
00097 
00098     std::pair<iterator, bool>
00099     insert(const std::pair<KeyT, ValueT> &InsertPair) {
00100       std::pair<typename MapTy::iterator, bool> Pair =
00101         Map.insert(std::make_pair(InsertPair.first, size_t(0)));
00102       if (Pair.second) {
00103         size_t Num = Vector.size();
00104         Pair.first->second = Num;
00105         Vector.push_back(InsertPair);
00106         return std::make_pair(Vector.begin() + Num, true);
00107       }
00108       return std::make_pair(Vector.begin() + Pair.first->second, false);
00109     }
00110 
00111     iterator find(const KeyT &Key) {
00112       typename MapTy::iterator It = Map.find(Key);
00113       if (It == Map.end()) return Vector.end();
00114       return Vector.begin() + It->second;
00115     }
00116 
00117     const_iterator find(const KeyT &Key) const {
00118       typename MapTy::const_iterator It = Map.find(Key);
00119       if (It == Map.end()) return Vector.end();
00120       return Vector.begin() + It->second;
00121     }
00122 
00123     /// This is similar to erase, but instead of removing the element from the
00124     /// vector, it just zeros out the key in the vector. This leaves iterators
00125     /// intact, but clients must be prepared for zeroed-out keys when iterating.
00126     void blot(const KeyT &Key) {
00127       typename MapTy::iterator It = Map.find(Key);
00128       if (It == Map.end()) return;
00129       Vector[It->second].first = KeyT();
00130       Map.erase(It);
00131     }
00132 
00133     void clear() {
00134       Map.clear();
00135       Vector.clear();
00136     }
00137   };
00138 }
00139 
00140 /// @}
00141 ///
00142 /// \defgroup ARCUtilities Utility declarations/definitions specific to ARC.
00143 /// @{
00144 
00145 /// \brief This is similar to StripPointerCastsAndObjCCalls but it stops as soon
00146 /// as it finds a value with multiple uses.
00147 static const Value *FindSingleUseIdentifiedObject(const Value *Arg) {
00148   if (Arg->hasOneUse()) {
00149     if (const BitCastInst *BC = dyn_cast<BitCastInst>(Arg))
00150       return FindSingleUseIdentifiedObject(BC->getOperand(0));
00151     if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Arg))
00152       if (GEP->hasAllZeroIndices())
00153         return FindSingleUseIdentifiedObject(GEP->getPointerOperand());
00154     if (IsForwarding(GetBasicInstructionClass(Arg)))
00155       return FindSingleUseIdentifiedObject(
00156                cast<CallInst>(Arg)->getArgOperand(0));
00157     if (!IsObjCIdentifiedObject(Arg))
00158       return 0;
00159     return Arg;
00160   }
00161 
00162   // If we found an identifiable object but it has multiple uses, but they are
00163   // trivial uses, we can still consider this to be a single-use value.
00164   if (IsObjCIdentifiedObject(Arg)) {
00165     for (Value::const_use_iterator UI = Arg->use_begin(), UE = Arg->use_end();
00166          UI != UE; ++UI) {
00167       const User *U = *UI;
00168       if (!U->use_empty() || StripPointerCastsAndObjCCalls(U) != Arg)
00169          return 0;
00170     }
00171 
00172     return Arg;
00173   }
00174 
00175   return 0;
00176 }
00177 
00178 /// \brief Test whether the given retainable object pointer escapes.
00179 ///
00180 /// This differs from regular escape analysis in that a use as an
00181 /// argument to a call is not considered an escape.
00182 ///
00183 static bool DoesRetainableObjPtrEscape(const User *Ptr) {
00184   DEBUG(dbgs() << "DoesRetainableObjPtrEscape: Target: " << *Ptr << "\n");
00185 
00186   // Walk the def-use chains.
00187   SmallVector<const Value *, 4> Worklist;
00188   Worklist.push_back(Ptr);
00189   // If Ptr has any operands add them as well.
00190   for (User::const_op_iterator I = Ptr->op_begin(), E = Ptr->op_end(); I != E;
00191        ++I) {
00192     Worklist.push_back(*I);
00193   }
00194 
00195   // Ensure we do not visit any value twice.
00196   SmallPtrSet<const Value *, 8> VisitedSet;
00197 
00198   do {
00199     const Value *V = Worklist.pop_back_val();
00200 
00201     DEBUG(dbgs() << "Visiting: " << *V << "\n");
00202 
00203     for (Value::const_use_iterator UI = V->use_begin(), UE = V->use_end();
00204          UI != UE; ++UI) {
00205       const User *UUser = *UI;
00206 
00207       DEBUG(dbgs() << "User: " << *UUser << "\n");
00208 
00209       // Special - Use by a call (callee or argument) is not considered
00210       // to be an escape.
00211       switch (GetBasicInstructionClass(UUser)) {
00212       case IC_StoreWeak:
00213       case IC_InitWeak:
00214       case IC_StoreStrong:
00215       case IC_Autorelease:
00216       case IC_AutoreleaseRV: {
00217         DEBUG(dbgs() << "User copies pointer arguments. Pointer Escapes!\n");
00218         // These special functions make copies of their pointer arguments.
00219         return true;
00220       }
00221       case IC_IntrinsicUser:
00222         // Use by the use intrinsic is not an escape.
00223         continue;
00224       case IC_User:
00225       case IC_None:
00226         // Use by an instruction which copies the value is an escape if the
00227         // result is an escape.
00228         if (isa<BitCastInst>(UUser) || isa<GetElementPtrInst>(UUser) ||
00229             isa<PHINode>(UUser) || isa<SelectInst>(UUser)) {
00230 
00231           if (VisitedSet.insert(UUser)) {
00232             DEBUG(dbgs() << "User copies value. Ptr escapes if result escapes."
00233                   " Adding to list.\n");
00234             Worklist.push_back(UUser);
00235           } else {
00236             DEBUG(dbgs() << "Already visited node.\n");
00237           }
00238           continue;
00239         }
00240         // Use by a load is not an escape.
00241         if (isa<LoadInst>(UUser))
00242           continue;
00243         // Use by a store is not an escape if the use is the address.
00244         if (const StoreInst *SI = dyn_cast<StoreInst>(UUser))
00245           if (V != SI->getValueOperand())
00246             continue;
00247         break;
00248       default:
00249         // Regular calls and other stuff are not considered escapes.
00250         continue;
00251       }
00252       // Otherwise, conservatively assume an escape.
00253       DEBUG(dbgs() << "Assuming ptr escapes.\n");
00254       return true;
00255     }
00256   } while (!Worklist.empty());
00257 
00258   // No escapes found.
00259   DEBUG(dbgs() << "Ptr does not escape.\n");
00260   return false;
00261 }
00262 
00263 /// This is a wrapper around getUnderlyingObjCPtr along the lines of
00264 /// GetUnderlyingObjects except that it returns early when it sees the first
00265 /// alloca.
00266 static inline bool AreAnyUnderlyingObjectsAnAlloca(const Value *V) {
00267   SmallPtrSet<const Value *, 4> Visited;
00268   SmallVector<const Value *, 4> Worklist;
00269   Worklist.push_back(V);
00270   do {
00271     const Value *P = Worklist.pop_back_val();
00272     P = GetUnderlyingObjCPtr(P);
00273 
00274     if (isa<AllocaInst>(P))
00275       return true;
00276 
00277     if (!Visited.insert(P))
00278       continue;
00279 
00280     if (const SelectInst *SI = dyn_cast<const SelectInst>(P)) {
00281       Worklist.push_back(SI->getTrueValue());
00282       Worklist.push_back(SI->getFalseValue());
00283       continue;
00284     }
00285 
00286     if (const PHINode *PN = dyn_cast<const PHINode>(P)) {
00287       for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
00288         Worklist.push_back(PN->getIncomingValue(i));
00289       continue;
00290     }
00291   } while (!Worklist.empty());
00292 
00293   return false;
00294 }
00295 
00296 
00297 /// @}
00298 ///
00299 /// \defgroup ARCOpt ARC Optimization.
00300 /// @{
00301 
00302 // TODO: On code like this:
00303 //
00304 // objc_retain(%x)
00305 // stuff_that_cannot_release()
00306 // objc_autorelease(%x)
00307 // stuff_that_cannot_release()
00308 // objc_retain(%x)
00309 // stuff_that_cannot_release()
00310 // objc_autorelease(%x)
00311 //
00312 // The second retain and autorelease can be deleted.
00313 
00314 // TODO: It should be possible to delete
00315 // objc_autoreleasePoolPush and objc_autoreleasePoolPop
00316 // pairs if nothing is actually autoreleased between them. Also, autorelease
00317 // calls followed by objc_autoreleasePoolPop calls (perhaps in ObjC++ code
00318 // after inlining) can be turned into plain release calls.
00319 
00320 // TODO: Critical-edge splitting. If the optimial insertion point is
00321 // a critical edge, the current algorithm has to fail, because it doesn't
00322 // know how to split edges. It should be possible to make the optimizer
00323 // think in terms of edges, rather than blocks, and then split critical
00324 // edges on demand.
00325 
00326 // TODO: OptimizeSequences could generalized to be Interprocedural.
00327 
00328 // TODO: Recognize that a bunch of other objc runtime calls have
00329 // non-escaping arguments and non-releasing arguments, and may be
00330 // non-autoreleasing.
00331 
00332 // TODO: Sink autorelease calls as far as possible. Unfortunately we
00333 // usually can't sink them past other calls, which would be the main
00334 // case where it would be useful.
00335 
00336 // TODO: The pointer returned from objc_loadWeakRetained is retained.
00337 
00338 // TODO: Delete release+retain pairs (rare).
00339 
00340 STATISTIC(NumNoops,       "Number of no-op objc calls eliminated");
00341 STATISTIC(NumPartialNoops, "Number of partially no-op objc calls eliminated");
00342 STATISTIC(NumAutoreleases,"Number of autoreleases converted to releases");
00343 STATISTIC(NumRets,        "Number of return value forwarding "
00344                           "retain+autoreleases eliminated");
00345 STATISTIC(NumRRs,         "Number of retain+release paths eliminated");
00346 STATISTIC(NumPeeps,       "Number of calls peephole-optimized");
00347 #ifndef NDEBUG
00348 STATISTIC(NumRetainsBeforeOpt,
00349           "Number of retains before optimization");
00350 STATISTIC(NumReleasesBeforeOpt,
00351           "Number of releases before optimization");
00352 STATISTIC(NumRetainsAfterOpt,
00353           "Number of retains after optimization");
00354 STATISTIC(NumReleasesAfterOpt,
00355           "Number of releases after optimization");
00356 #endif
00357 
00358 namespace {
00359   /// \enum Sequence
00360   ///
00361   /// \brief A sequence of states that a pointer may go through in which an
00362   /// objc_retain and objc_release are actually needed.
00363   enum Sequence {
00364     S_None,
00365     S_Retain,         ///< objc_retain(x).
00366     S_CanRelease,     ///< foo(x) -- x could possibly see a ref count decrement.
00367     S_Use,            ///< any use of x.
00368     S_Stop,           ///< like S_Release, but code motion is stopped.
00369     S_Release,        ///< objc_release(x).
00370     S_MovableRelease  ///< objc_release(x), !clang.imprecise_release.
00371   };
00372 
00373   raw_ostream &operator<<(raw_ostream &OS, const Sequence S)
00374     LLVM_ATTRIBUTE_UNUSED;
00375   raw_ostream &operator<<(raw_ostream &OS, const Sequence S) {
00376     switch (S) {
00377     case S_None:
00378       return OS << "S_None";
00379     case S_Retain:
00380       return OS << "S_Retain";
00381     case S_CanRelease:
00382       return OS << "S_CanRelease";
00383     case S_Use:
00384       return OS << "S_Use";
00385     case S_Release:
00386       return OS << "S_Release";
00387     case S_MovableRelease:
00388       return OS << "S_MovableRelease";
00389     case S_Stop:
00390       return OS << "S_Stop";
00391     }
00392     llvm_unreachable("Unknown sequence type.");
00393   }
00394 }
00395 
00396 static Sequence MergeSeqs(Sequence A, Sequence B, bool TopDown) {
00397   // The easy cases.
00398   if (A == B)
00399     return A;
00400   if (A == S_None || B == S_None)
00401     return S_None;
00402 
00403   if (A > B) std::swap(A, B);
00404   if (TopDown) {
00405     // Choose the side which is further along in the sequence.
00406     if ((A == S_Retain || A == S_CanRelease) &&
00407         (B == S_CanRelease || B == S_Use))
00408       return B;
00409   } else {
00410     // Choose the side which is further along in the sequence.
00411     if ((A == S_Use || A == S_CanRelease) &&
00412         (B == S_Use || B == S_Release || B == S_Stop || B == S_MovableRelease))
00413       return A;
00414     // If both sides are releases, choose the more conservative one.
00415     if (A == S_Stop && (B == S_Release || B == S_MovableRelease))
00416       return A;
00417     if (A == S_Release && B == S_MovableRelease)
00418       return A;
00419   }
00420 
00421   return S_None;
00422 }
00423 
00424 namespace {
00425   /// \brief Unidirectional information about either a
00426   /// retain-decrement-use-release sequence or release-use-decrement-retain
00427   /// reverse sequence.
00428   struct RRInfo {
00429     /// After an objc_retain, the reference count of the referenced
00430     /// object is known to be positive. Similarly, before an objc_release, the
00431     /// reference count of the referenced object is known to be positive. If
00432     /// there are retain-release pairs in code regions where the retain count
00433     /// is known to be positive, they can be eliminated, regardless of any side
00434     /// effects between them.
00435     ///
00436     /// Also, a retain+release pair nested within another retain+release
00437     /// pair all on the known same pointer value can be eliminated, regardless
00438     /// of any intervening side effects.
00439     ///
00440     /// KnownSafe is true when either of these conditions is satisfied.
00441     bool KnownSafe;
00442 
00443     /// True of the objc_release calls are all marked with the "tail" keyword.
00444     bool IsTailCallRelease;
00445 
00446     /// If the Calls are objc_release calls and they all have a
00447     /// clang.imprecise_release tag, this is the metadata tag.
00448     MDNode *ReleaseMetadata;
00449 
00450     /// For a top-down sequence, the set of objc_retains or
00451     /// objc_retainBlocks. For bottom-up, the set of objc_releases.
00452     SmallPtrSet<Instruction *, 2> Calls;
00453 
00454     /// The set of optimal insert positions for moving calls in the opposite
00455     /// sequence.
00456     SmallPtrSet<Instruction *, 2> ReverseInsertPts;
00457 
00458     /// If this is true, we cannot perform code motion but can still remove
00459     /// retain/release pairs.
00460     bool CFGHazardAfflicted;
00461 
00462     RRInfo() :
00463       KnownSafe(false), IsTailCallRelease(false), ReleaseMetadata(0),
00464       CFGHazardAfflicted(false) {}
00465 
00466     void clear();
00467 
00468     bool IsTrackingImpreciseReleases() {
00469       return ReleaseMetadata != 0;
00470     }
00471   };
00472 }
00473 
00474 void RRInfo::clear() {
00475   KnownSafe = false;
00476   IsTailCallRelease = false;
00477   ReleaseMetadata = 0;
00478   Calls.clear();
00479   ReverseInsertPts.clear();
00480   CFGHazardAfflicted = false;
00481 }
00482 
00483 namespace {
00484   /// \brief This class summarizes several per-pointer runtime properties which
00485   /// are propogated through the flow graph.
00486   class PtrState {
00487     /// True if the reference count is known to be incremented.
00488     bool KnownPositiveRefCount;
00489 
00490     /// True if we've seen an opportunity for partial RR elimination, such as
00491     /// pushing calls into a CFG triangle or into one side of a CFG diamond.
00492     bool Partial;
00493 
00494     /// The current position in the sequence.
00495     Sequence Seq : 8;
00496 
00497   public:
00498     /// Unidirectional information about the current sequence.
00499     ///
00500     /// TODO: Encapsulate this better.
00501     RRInfo RRI;
00502 
00503     PtrState() : KnownPositiveRefCount(false), Partial(false),
00504                  Seq(S_None) {}
00505 
00506     void SetKnownPositiveRefCount() {
00507       DEBUG(dbgs() << "Setting Known Positive.\n");
00508       KnownPositiveRefCount = true;
00509     }
00510 
00511     void ClearKnownPositiveRefCount() {
00512       DEBUG(dbgs() << "Clearing Known Positive.\n");
00513       KnownPositiveRefCount = false;
00514     }
00515 
00516     bool HasKnownPositiveRefCount() const {
00517       return KnownPositiveRefCount;
00518     }
00519 
00520     void SetSeq(Sequence NewSeq) {
00521       DEBUG(dbgs() << "Old: " << Seq << "; New: " << NewSeq << "\n");
00522       Seq = NewSeq;
00523     }
00524 
00525     Sequence GetSeq() const {
00526       return Seq;
00527     }
00528 
00529     void ClearSequenceProgress() {
00530       ResetSequenceProgress(S_None);
00531     }
00532 
00533     void ResetSequenceProgress(Sequence NewSeq) {
00534       DEBUG(dbgs() << "Resetting sequence progress.\n");
00535       SetSeq(NewSeq);
00536       Partial = false;
00537       RRI.clear();
00538     }
00539 
00540     void Merge(const PtrState &Other, bool TopDown);
00541   };
00542 }
00543 
00544 void
00545 PtrState::Merge(const PtrState &Other, bool TopDown) {
00546   Seq = MergeSeqs(Seq, Other.Seq, TopDown);
00547   KnownPositiveRefCount = KnownPositiveRefCount && Other.KnownPositiveRefCount;
00548 
00549   // If we're not in a sequence (anymore), drop all associated state.
00550   if (Seq == S_None) {
00551     Partial = false;
00552     RRI.clear();
00553   } else if (Partial || Other.Partial) {
00554     // If we're doing a merge on a path that's previously seen a partial
00555     // merge, conservatively drop the sequence, to avoid doing partial
00556     // RR elimination. If the branch predicates for the two merge differ,
00557     // mixing them is unsafe.
00558     ClearSequenceProgress();
00559   } else {
00560     // Conservatively merge the ReleaseMetadata information.
00561     if (RRI.ReleaseMetadata != Other.RRI.ReleaseMetadata)
00562       RRI.ReleaseMetadata = 0;
00563 
00564     RRI.KnownSafe = RRI.KnownSafe && Other.RRI.KnownSafe;
00565     RRI.IsTailCallRelease = RRI.IsTailCallRelease &&
00566                             Other.RRI.IsTailCallRelease;
00567     RRI.Calls.insert(Other.RRI.Calls.begin(), Other.RRI.Calls.end());
00568     RRI.CFGHazardAfflicted |= Other.RRI.CFGHazardAfflicted;
00569 
00570     // Merge the insert point sets. If there are any differences,
00571     // that makes this a partial merge.
00572     Partial = RRI.ReverseInsertPts.size() != Other.RRI.ReverseInsertPts.size();
00573     for (SmallPtrSet<Instruction *, 2>::const_iterator
00574          I = Other.RRI.ReverseInsertPts.begin(),
00575          E = Other.RRI.ReverseInsertPts.end(); I != E; ++I)
00576       Partial |= RRI.ReverseInsertPts.insert(*I);
00577   }
00578 }
00579 
00580 namespace {
00581   /// \brief Per-BasicBlock state.
00582   class BBState {
00583     /// The number of unique control paths from the entry which can reach this
00584     /// block.
00585     unsigned TopDownPathCount;
00586 
00587     /// The number of unique control paths to exits from this block.
00588     unsigned BottomUpPathCount;
00589 
00590     /// A type for PerPtrTopDown and PerPtrBottomUp.
00591     typedef MapVector<const Value *, PtrState> MapTy;
00592 
00593     /// The top-down traversal uses this to record information known about a
00594     /// pointer at the bottom of each block.
00595     MapTy PerPtrTopDown;
00596 
00597     /// The bottom-up traversal uses this to record information known about a
00598     /// pointer at the top of each block.
00599     MapTy PerPtrBottomUp;
00600 
00601     /// Effective predecessors of the current block ignoring ignorable edges and
00602     /// ignored backedges.
00603     SmallVector<BasicBlock *, 2> Preds;
00604     /// Effective successors of the current block ignoring ignorable edges and
00605     /// ignored backedges.
00606     SmallVector<BasicBlock *, 2> Succs;
00607 
00608   public:
00609     BBState() : TopDownPathCount(0), BottomUpPathCount(0) {}
00610 
00611     typedef MapTy::iterator ptr_iterator;
00612     typedef MapTy::const_iterator ptr_const_iterator;
00613 
00614     ptr_iterator top_down_ptr_begin() { return PerPtrTopDown.begin(); }
00615     ptr_iterator top_down_ptr_end() { return PerPtrTopDown.end(); }
00616     ptr_const_iterator top_down_ptr_begin() const {
00617       return PerPtrTopDown.begin();
00618     }
00619     ptr_const_iterator top_down_ptr_end() const {
00620       return PerPtrTopDown.end();
00621     }
00622 
00623     ptr_iterator bottom_up_ptr_begin() { return PerPtrBottomUp.begin(); }
00624     ptr_iterator bottom_up_ptr_end() { return PerPtrBottomUp.end(); }
00625     ptr_const_iterator bottom_up_ptr_begin() const {
00626       return PerPtrBottomUp.begin();
00627     }
00628     ptr_const_iterator bottom_up_ptr_end() const {
00629       return PerPtrBottomUp.end();
00630     }
00631 
00632     /// Mark this block as being an entry block, which has one path from the
00633     /// entry by definition.
00634     void SetAsEntry() { TopDownPathCount = 1; }
00635 
00636     /// Mark this block as being an exit block, which has one path to an exit by
00637     /// definition.
00638     void SetAsExit()  { BottomUpPathCount = 1; }
00639 
00640     /// Attempt to find the PtrState object describing the top down state for
00641     /// pointer Arg. Return a new initialized PtrState describing the top down
00642     /// state for Arg if we do not find one.
00643     PtrState &getPtrTopDownState(const Value *Arg) {
00644       return PerPtrTopDown[Arg];
00645     }
00646 
00647     /// Attempt to find the PtrState object describing the bottom up state for
00648     /// pointer Arg. Return a new initialized PtrState describing the bottom up
00649     /// state for Arg if we do not find one.
00650     PtrState &getPtrBottomUpState(const Value *Arg) {
00651       return PerPtrBottomUp[Arg];
00652     }
00653 
00654     /// Attempt to find the PtrState object describing the bottom up state for
00655     /// pointer Arg.
00656     ptr_iterator findPtrBottomUpState(const Value *Arg) {
00657       return PerPtrBottomUp.find(Arg);
00658     }
00659 
00660     void clearBottomUpPointers() {
00661       PerPtrBottomUp.clear();
00662     }
00663 
00664     void clearTopDownPointers() {
00665       PerPtrTopDown.clear();
00666     }
00667 
00668     void InitFromPred(const BBState &Other);
00669     void InitFromSucc(const BBState &Other);
00670     void MergePred(const BBState &Other);
00671     void MergeSucc(const BBState &Other);
00672 
00673     /// Return the number of possible unique paths from an entry to an exit
00674     /// which pass through this block. This is only valid after both the
00675     /// top-down and bottom-up traversals are complete.
00676     unsigned GetAllPathCount() const {
00677       assert(TopDownPathCount != 0);
00678       assert(BottomUpPathCount != 0);
00679       return TopDownPathCount * BottomUpPathCount;
00680     }
00681 
00682     // Specialized CFG utilities.
00683     typedef SmallVectorImpl<BasicBlock *>::const_iterator edge_iterator;
00684     edge_iterator pred_begin() { return Preds.begin(); }
00685     edge_iterator pred_end() { return Preds.end(); }
00686     edge_iterator succ_begin() { return Succs.begin(); }
00687     edge_iterator succ_end() { return Succs.end(); }
00688 
00689     void addSucc(BasicBlock *Succ) { Succs.push_back(Succ); }
00690     void addPred(BasicBlock *Pred) { Preds.push_back(Pred); }
00691 
00692     bool isExit() const { return Succs.empty(); }
00693   };
00694 }
00695 
00696 void BBState::InitFromPred(const BBState &Other) {
00697   PerPtrTopDown = Other.PerPtrTopDown;
00698   TopDownPathCount = Other.TopDownPathCount;
00699 }
00700 
00701 void BBState::InitFromSucc(const BBState &Other) {
00702   PerPtrBottomUp = Other.PerPtrBottomUp;
00703   BottomUpPathCount = Other.BottomUpPathCount;
00704 }
00705 
00706 /// The top-down traversal uses this to merge information about predecessors to
00707 /// form the initial state for a new block.
00708 void BBState::MergePred(const BBState &Other) {
00709   // Other.TopDownPathCount can be 0, in which case it is either dead or a
00710   // loop backedge. Loop backedges are special.
00711   TopDownPathCount += Other.TopDownPathCount;
00712 
00713   // Check for overflow. If we have overflow, fall back to conservative
00714   // behavior.
00715   if (TopDownPathCount < Other.TopDownPathCount) {
00716     clearTopDownPointers();
00717     return;
00718   }
00719 
00720   // For each entry in the other set, if our set has an entry with the same key,
00721   // merge the entries. Otherwise, copy the entry and merge it with an empty
00722   // entry.
00723   for (ptr_const_iterator MI = Other.top_down_ptr_begin(),
00724        ME = Other.top_down_ptr_end(); MI != ME; ++MI) {
00725     std::pair<ptr_iterator, bool> Pair = PerPtrTopDown.insert(*MI);
00726     Pair.first->second.Merge(Pair.second ? PtrState() : MI->second,
00727                              /*TopDown=*/true);
00728   }
00729 
00730   // For each entry in our set, if the other set doesn't have an entry with the
00731   // same key, force it to merge with an empty entry.
00732   for (ptr_iterator MI = top_down_ptr_begin(),
00733        ME = top_down_ptr_end(); MI != ME; ++MI)
00734     if (Other.PerPtrTopDown.find(MI->first) == Other.PerPtrTopDown.end())
00735       MI->second.Merge(PtrState(), /*TopDown=*/true);
00736 }
00737 
00738 /// The bottom-up traversal uses this to merge information about successors to
00739 /// form the initial state for a new block.
00740 void BBState::MergeSucc(const BBState &Other) {
00741   // Other.BottomUpPathCount can be 0, in which case it is either dead or a
00742   // loop backedge. Loop backedges are special.
00743   BottomUpPathCount += Other.BottomUpPathCount;
00744 
00745   // Check for overflow. If we have overflow, fall back to conservative
00746   // behavior.
00747   if (BottomUpPathCount < Other.BottomUpPathCount) {
00748     clearBottomUpPointers();
00749     return;
00750   }
00751 
00752   // For each entry in the other set, if our set has an entry with the
00753   // same key, merge the entries. Otherwise, copy the entry and merge
00754   // it with an empty entry.
00755   for (ptr_const_iterator MI = Other.bottom_up_ptr_begin(),
00756        ME = Other.bottom_up_ptr_end(); MI != ME; ++MI) {
00757     std::pair<ptr_iterator, bool> Pair = PerPtrBottomUp.insert(*MI);
00758     Pair.first->second.Merge(Pair.second ? PtrState() : MI->second,
00759                              /*TopDown=*/false);
00760   }
00761 
00762   // For each entry in our set, if the other set doesn't have an entry
00763   // with the same key, force it to merge with an empty entry.
00764   for (ptr_iterator MI = bottom_up_ptr_begin(),
00765        ME = bottom_up_ptr_end(); MI != ME; ++MI)
00766     if (Other.PerPtrBottomUp.find(MI->first) == Other.PerPtrBottomUp.end())
00767       MI->second.Merge(PtrState(), /*TopDown=*/false);
00768 }
00769 
00770 // Only enable ARC Annotations if we are building a debug version of
00771 // libObjCARCOpts.
00772 #ifndef NDEBUG
00773 #define ARC_ANNOTATIONS
00774 #endif
00775 
00776 // Define some macros along the lines of DEBUG and some helper functions to make
00777 // it cleaner to create annotations in the source code and to no-op when not
00778 // building in debug mode.
00779 #ifdef ARC_ANNOTATIONS
00780 
00781 #include "llvm/Support/CommandLine.h"
00782 
00783 /// Enable/disable ARC sequence annotations.
00784 static cl::opt<bool>
00785 EnableARCAnnotations("enable-objc-arc-annotations", cl::init(false),
00786                      cl::desc("Enable emission of arc data flow analysis "
00787                               "annotations"));
00788 static cl::opt<bool>
00789 DisableCheckForCFGHazards("disable-objc-arc-checkforcfghazards", cl::init(false),
00790                           cl::desc("Disable check for cfg hazards when "
00791                                    "annotating"));
00792 static cl::opt<std::string>
00793 ARCAnnotationTargetIdentifier("objc-arc-annotation-target-identifier",
00794                               cl::init(""),
00795                               cl::desc("filter out all data flow annotations "
00796                                        "but those that apply to the given "
00797                                        "target llvm identifier."));
00798 
00799 /// This function appends a unique ARCAnnotationProvenanceSourceMDKind id to an
00800 /// instruction so that we can track backwards when post processing via the llvm
00801 /// arc annotation processor tool. If the function is an
00802 static MDString *AppendMDNodeToSourcePtr(unsigned NodeId,
00803                                          Value *Ptr) {
00804   MDString *Hash = 0;
00805 
00806   // If pointer is a result of an instruction and it does not have a source
00807   // MDNode it, attach a new MDNode onto it. If pointer is a result of
00808   // an instruction and does have a source MDNode attached to it, return a
00809   // reference to said Node. Otherwise just return 0.
00810   if (Instruction *Inst = dyn_cast<Instruction>(Ptr)) {
00811     MDNode *Node;
00812     if (!(Node = Inst->getMetadata(NodeId))) {
00813       // We do not have any node. Generate and attatch the hash MDString to the
00814       // instruction.
00815 
00816       // We just use an MDString to ensure that this metadata gets written out
00817       // of line at the module level and to provide a very simple format
00818       // encoding the information herein. Both of these makes it simpler to
00819       // parse the annotations by a simple external program.
00820       std::string Str;
00821       raw_string_ostream os(Str);
00822       os << "(" << Inst->getParent()->getParent()->getName() << ",%"
00823          << Inst->getName() << ")";
00824 
00825       Hash = MDString::get(Inst->getContext(), os.str());
00826       Inst->setMetadata(NodeId, MDNode::get(Inst->getContext(),Hash));
00827     } else {
00828       // We have a node. Grab its hash and return it.
00829       assert(Node->getNumOperands() == 1 &&
00830         "An ARCAnnotationProvenanceSourceMDKind can only have 1 operand.");
00831       Hash = cast<MDString>(Node->getOperand(0));
00832     }
00833   } else if (Argument *Arg = dyn_cast<Argument>(Ptr)) {
00834     std::string str;
00835     raw_string_ostream os(str);
00836     os << "(" << Arg->getParent()->getName() << ",%" << Arg->getName()
00837        << ")";
00838     Hash = MDString::get(Arg->getContext(), os.str());
00839   }
00840 
00841   return Hash;
00842 }
00843 
00844 static std::string SequenceToString(Sequence A) {
00845   std::string str;
00846   raw_string_ostream os(str);
00847   os << A;
00848   return os.str();
00849 }
00850 
00851 /// Helper function to change a Sequence into a String object using our overload
00852 /// for raw_ostream so we only have printing code in one location.
00853 static MDString *SequenceToMDString(LLVMContext &Context,
00854                                     Sequence A) {
00855   return MDString::get(Context, SequenceToString(A));
00856 }
00857 
00858 /// A simple function to generate a MDNode which describes the change in state
00859 /// for Value *Ptr caused by Instruction *Inst.
00860 static void AppendMDNodeToInstForPtr(unsigned NodeId,
00861                                      Instruction *Inst,
00862                                      Value *Ptr,
00863                                      MDString *PtrSourceMDNodeID,
00864                                      Sequence OldSeq,
00865                                      Sequence NewSeq) {
00866   MDNode *Node = 0;
00867   Value *tmp[3] = {PtrSourceMDNodeID,
00868                    SequenceToMDString(Inst->getContext(),
00869                                       OldSeq),
00870                    SequenceToMDString(Inst->getContext(),
00871                                       NewSeq)};
00872   Node = MDNode::get(Inst->getContext(),
00873                      ArrayRef<Value*>(tmp, 3));
00874 
00875   Inst->setMetadata(NodeId, Node);
00876 }
00877 
00878 /// Add to the beginning of the basic block llvm.ptr.annotations which show the
00879 /// state of a pointer at the entrance to a basic block.
00880 static void GenerateARCBBEntranceAnnotation(const char *Name, BasicBlock *BB,
00881                                             Value *Ptr, Sequence Seq) {
00882   // If we have a target identifier, make sure that we match it before
00883   // continuing.
00884   if(!ARCAnnotationTargetIdentifier.empty() &&
00885      !Ptr->getName().equals(ARCAnnotationTargetIdentifier))
00886     return;
00887 
00888   Module *M = BB->getParent()->getParent();
00889   LLVMContext &C = M->getContext();
00890   Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C));
00891   Type *I8XX = PointerType::getUnqual(I8X);
00892   Type *Params[] = {I8XX, I8XX};
00893   FunctionType *FTy = FunctionType::get(Type::getVoidTy(C),
00894                                         ArrayRef<Type*>(Params, 2),
00895                                         /*isVarArg=*/false);
00896   Constant *Callee = M->getOrInsertFunction(Name, FTy);
00897 
00898   IRBuilder<> Builder(BB, BB->getFirstInsertionPt());
00899 
00900   Value *PtrName;
00901   StringRef Tmp = Ptr->getName();
00902   if (0 == (PtrName = M->getGlobalVariable(Tmp, true))) {
00903     Value *ActualPtrName = Builder.CreateGlobalStringPtr(Tmp,
00904                                                          Tmp + "_STR");
00905     PtrName = new GlobalVariable(*M, I8X, true, GlobalVariable::InternalLinkage,
00906                                  cast<Constant>(ActualPtrName), Tmp);
00907   }
00908 
00909   Value *S;
00910   std::string SeqStr = SequenceToString(Seq);
00911   if (0 == (S = M->getGlobalVariable(SeqStr, true))) {
00912     Value *ActualPtrName = Builder.CreateGlobalStringPtr(SeqStr,
00913                                                          SeqStr + "_STR");
00914     S = new GlobalVariable(*M, I8X, true, GlobalVariable::InternalLinkage,
00915                            cast<Constant>(ActualPtrName), SeqStr);
00916   }
00917 
00918   Builder.CreateCall2(Callee, PtrName, S);
00919 }
00920 
00921 /// Add to the end of the basic block llvm.ptr.annotations which show the state
00922 /// of the pointer at the bottom of the basic block.
00923 static void GenerateARCBBTerminatorAnnotation(const char *Name, BasicBlock *BB,
00924                                               Value *Ptr, Sequence Seq) {
00925   // If we have a target identifier, make sure that we match it before emitting
00926   // an annotation.
00927   if(!ARCAnnotationTargetIdentifier.empty() &&
00928      !Ptr->getName().equals(ARCAnnotationTargetIdentifier))
00929     return;
00930 
00931   Module *M = BB->getParent()->getParent();
00932   LLVMContext &C = M->getContext();
00933   Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C));
00934   Type *I8XX = PointerType::getUnqual(I8X);
00935   Type *Params[] = {I8XX, I8XX};
00936   FunctionType *FTy = FunctionType::get(Type::getVoidTy(C),
00937                                         ArrayRef<Type*>(Params, 2),
00938                                         /*isVarArg=*/false);
00939   Constant *Callee = M->getOrInsertFunction(Name, FTy);
00940 
00941   IRBuilder<> Builder(BB, llvm::prior(BB->end()));
00942 
00943   Value *PtrName;
00944   StringRef Tmp = Ptr->getName();
00945   if (0 == (PtrName = M->getGlobalVariable(Tmp, true))) {
00946     Value *ActualPtrName = Builder.CreateGlobalStringPtr(Tmp,
00947                                                          Tmp + "_STR");
00948     PtrName = new GlobalVariable(*M, I8X, true, GlobalVariable::InternalLinkage,
00949                                  cast<Constant>(ActualPtrName), Tmp);
00950   }
00951 
00952   Value *S;
00953   std::string SeqStr = SequenceToString(Seq);
00954   if (0 == (S = M->getGlobalVariable(SeqStr, true))) {
00955     Value *ActualPtrName = Builder.CreateGlobalStringPtr(SeqStr,
00956                                                          SeqStr + "_STR");
00957     S = new GlobalVariable(*M, I8X, true, GlobalVariable::InternalLinkage,
00958                            cast<Constant>(ActualPtrName), SeqStr);
00959   }
00960   Builder.CreateCall2(Callee, PtrName, S);
00961 }
00962 
00963 /// Adds a source annotation to pointer and a state change annotation to Inst
00964 /// referencing the source annotation and the old/new state of pointer.
00965 static void GenerateARCAnnotation(unsigned InstMDId,
00966                                   unsigned PtrMDId,
00967                                   Instruction *Inst,
00968                                   Value *Ptr,
00969                                   Sequence OldSeq,
00970                                   Sequence NewSeq) {
00971   if (EnableARCAnnotations) {
00972     // If we have a target identifier, make sure that we match it before
00973     // emitting an annotation.
00974     if(!ARCAnnotationTargetIdentifier.empty() &&
00975        !Ptr->getName().equals(ARCAnnotationTargetIdentifier))
00976       return;
00977 
00978     // First generate the source annotation on our pointer. This will return an
00979     // MDString* if Ptr actually comes from an instruction implying we can put
00980     // in a source annotation. If AppendMDNodeToSourcePtr returns 0 (i.e. NULL),
00981     // then we know that our pointer is from an Argument so we put a reference
00982     // to the argument number.
00983     //
00984     // The point of this is to make it easy for the
00985     // llvm-arc-annotation-processor tool to cross reference where the source
00986     // pointer is in the LLVM IR since the LLVM IR parser does not submit such
00987     // information via debug info for backends to use (since why would anyone
00988     // need such a thing from LLVM IR besides in non standard cases
00989     // [i.e. this]).
00990     MDString *SourcePtrMDNode =
00991       AppendMDNodeToSourcePtr(PtrMDId, Ptr);
00992     AppendMDNodeToInstForPtr(InstMDId, Inst, Ptr, SourcePtrMDNode, OldSeq,
00993                              NewSeq);
00994   }
00995 }
00996 
00997 // The actual interface for accessing the above functionality is defined via
00998 // some simple macros which are defined below. We do this so that the user does
00999 // not need to pass in what metadata id is needed resulting in cleaner code and
01000 // additionally since it provides an easy way to conditionally no-op all
01001 // annotation support in a non-debug build.
01002 
01003 /// Use this macro to annotate a sequence state change when processing
01004 /// instructions bottom up,
01005 #define ANNOTATE_BOTTOMUP(inst, ptr, old, new)                          \
01006   GenerateARCAnnotation(ARCAnnotationBottomUpMDKind,                    \
01007                         ARCAnnotationProvenanceSourceMDKind, (inst),    \
01008                         const_cast<Value*>(ptr), (old), (new))
01009 /// Use this macro to annotate a sequence state change when processing
01010 /// instructions top down.
01011 #define ANNOTATE_TOPDOWN(inst, ptr, old, new)                           \
01012   GenerateARCAnnotation(ARCAnnotationTopDownMDKind,                     \
01013                         ARCAnnotationProvenanceSourceMDKind, (inst),    \
01014                         const_cast<Value*>(ptr), (old), (new))
01015 
01016 #define ANNOTATE_BB(_states, _bb, _name, _type, _direction)                   \
01017   do {                                                                        \
01018     if (EnableARCAnnotations) {                                               \
01019       for(BBState::ptr_const_iterator I = (_states)._direction##_ptr_begin(), \
01020           E = (_states)._direction##_ptr_end(); I != E; ++I) {                \
01021         Value *Ptr = const_cast<Value*>(I->first);                            \
01022         Sequence Seq = I->second.GetSeq();                                    \
01023         GenerateARCBB ## _type ## Annotation(_name, (_bb), Ptr, Seq);         \
01024       }                                                                       \
01025     }                                                                         \
01026   } while (0)
01027 
01028 #define ANNOTATE_BOTTOMUP_BBSTART(_states, _basicblock)                       \
01029     ANNOTATE_BB(_states, _basicblock, "llvm.arc.annotation.bottomup.bbstart", \
01030                 Entrance, bottom_up)
01031 #define ANNOTATE_BOTTOMUP_BBEND(_states, _basicblock)                         \
01032     ANNOTATE_BB(_states, _basicblock, "llvm.arc.annotation.bottomup.bbend",   \
01033                 Terminator, bottom_up)
01034 #define ANNOTATE_TOPDOWN_BBSTART(_states, _basicblock)                        \
01035     ANNOTATE_BB(_states, _basicblock, "llvm.arc.annotation.topdown.bbstart",  \
01036                 Entrance, top_down)
01037 #define ANNOTATE_TOPDOWN_BBEND(_states, _basicblock)                          \
01038     ANNOTATE_BB(_states, _basicblock, "llvm.arc.annotation.topdown.bbend",    \
01039                 Terminator, top_down)
01040 
01041 #else // !ARC_ANNOTATION
01042 // If annotations are off, noop.
01043 #define ANNOTATE_BOTTOMUP(inst, ptr, old, new)
01044 #define ANNOTATE_TOPDOWN(inst, ptr, old, new)
01045 #define ANNOTATE_BOTTOMUP_BBSTART(states, basicblock)
01046 #define ANNOTATE_BOTTOMUP_BBEND(states, basicblock)
01047 #define ANNOTATE_TOPDOWN_BBSTART(states, basicblock)
01048 #define ANNOTATE_TOPDOWN_BBEND(states, basicblock)
01049 #endif // !ARC_ANNOTATION
01050 
01051 namespace {
01052   /// \brief The main ARC optimization pass.
01053   class ObjCARCOpt : public FunctionPass {
01054     bool Changed;
01055     ProvenanceAnalysis PA;
01056 
01057     // This is used to track if a pointer is stored into an alloca.
01058     DenseSet<const Value *> MultiOwnersSet;
01059 
01060     /// A flag indicating whether this optimization pass should run.
01061     bool Run;
01062 
01063     /// Declarations for ObjC runtime functions, for use in creating calls to
01064     /// them. These are initialized lazily to avoid cluttering up the Module
01065     /// with unused declarations.
01066 
01067     /// Declaration for ObjC runtime function objc_autoreleaseReturnValue.
01068     Constant *AutoreleaseRVCallee;
01069     /// Declaration for ObjC runtime function objc_release.
01070     Constant *ReleaseCallee;
01071     /// Declaration for ObjC runtime function objc_retain.
01072     Constant *RetainCallee;
01073     /// Declaration for ObjC runtime function objc_retainBlock.
01074     Constant *RetainBlockCallee;
01075     /// Declaration for ObjC runtime function objc_autorelease.
01076     Constant *AutoreleaseCallee;
01077 
01078     /// Flags which determine whether each of the interesting runtine functions
01079     /// is in fact used in the current function.
01080     unsigned UsedInThisFunction;
01081 
01082     /// The Metadata Kind for clang.imprecise_release metadata.
01083     unsigned ImpreciseReleaseMDKind;
01084 
01085     /// The Metadata Kind for clang.arc.copy_on_escape metadata.
01086     unsigned CopyOnEscapeMDKind;
01087 
01088     /// The Metadata Kind for clang.arc.no_objc_arc_exceptions metadata.
01089     unsigned NoObjCARCExceptionsMDKind;
01090 
01091 #ifdef ARC_ANNOTATIONS
01092     /// The Metadata Kind for llvm.arc.annotation.bottomup metadata.
01093     unsigned ARCAnnotationBottomUpMDKind;
01094     /// The Metadata Kind for llvm.arc.annotation.topdown metadata.
01095     unsigned ARCAnnotationTopDownMDKind;
01096     /// The Metadata Kind for llvm.arc.annotation.provenancesource metadata.
01097     unsigned ARCAnnotationProvenanceSourceMDKind;
01098 #endif // ARC_ANNOATIONS
01099 
01100     Constant *getAutoreleaseRVCallee(Module *M);
01101     Constant *getReleaseCallee(Module *M);
01102     Constant *getRetainCallee(Module *M);
01103     Constant *getRetainBlockCallee(Module *M);
01104     Constant *getAutoreleaseCallee(Module *M);
01105 
01106     bool IsRetainBlockOptimizable(const Instruction *Inst);
01107 
01108     bool OptimizeRetainRVCall(Function &F, Instruction *RetainRV);
01109     void OptimizeAutoreleaseRVCall(Function &F, Instruction *AutoreleaseRV,
01110                                    InstructionClass &Class);
01111     bool OptimizeRetainBlockCall(Function &F, Instruction *RetainBlock,
01112                                  InstructionClass &Class);
01113     void OptimizeIndividualCalls(Function &F);
01114 
01115     void CheckForCFGHazards(const BasicBlock *BB,
01116                             DenseMap<const BasicBlock *, BBState> &BBStates,
01117                             BBState &MyStates) const;
01118     bool VisitInstructionBottomUp(Instruction *Inst,
01119                                   BasicBlock *BB,
01120                                   MapVector<Value *, RRInfo> &Retains,
01121                                   BBState &MyStates);
01122     bool VisitBottomUp(BasicBlock *BB,
01123                        DenseMap<const BasicBlock *, BBState> &BBStates,
01124                        MapVector<Value *, RRInfo> &Retains);
01125     bool VisitInstructionTopDown(Instruction *Inst,
01126                                  DenseMap<Value *, RRInfo> &Releases,
01127                                  BBState &MyStates);
01128     bool VisitTopDown(BasicBlock *BB,
01129                       DenseMap<const BasicBlock *, BBState> &BBStates,
01130                       DenseMap<Value *, RRInfo> &Releases);
01131     bool Visit(Function &F,
01132                DenseMap<const BasicBlock *, BBState> &BBStates,
01133                MapVector<Value *, RRInfo> &Retains,
01134                DenseMap<Value *, RRInfo> &Releases);
01135 
01136     void MoveCalls(Value *Arg, RRInfo &RetainsToMove, RRInfo &ReleasesToMove,
01137                    MapVector<Value *, RRInfo> &Retains,
01138                    DenseMap<Value *, RRInfo> &Releases,
01139                    SmallVectorImpl<Instruction *> &DeadInsts,
01140                    Module *M);
01141 
01142     bool ConnectTDBUTraversals(DenseMap<const BasicBlock *, BBState> &BBStates,
01143                                MapVector<Value *, RRInfo> &Retains,
01144                                DenseMap<Value *, RRInfo> &Releases,
01145                                Module *M,
01146                                SmallVector<Instruction *, 4> &NewRetains,
01147                                SmallVector<Instruction *, 4> &NewReleases,
01148                                SmallVector<Instruction *, 8> &DeadInsts,
01149                                RRInfo &RetainsToMove,
01150                                RRInfo &ReleasesToMove,
01151                                Value *Arg,
01152                                bool KnownSafe,
01153                                bool &AnyPairsCompletelyEliminated);
01154 
01155     bool PerformCodePlacement(DenseMap<const BasicBlock *, BBState> &BBStates,
01156                               MapVector<Value *, RRInfo> &Retains,
01157                               DenseMap<Value *, RRInfo> &Releases,
01158                               Module *M);
01159 
01160     void OptimizeWeakCalls(Function &F);
01161 
01162     bool OptimizeSequences(Function &F);
01163 
01164     void OptimizeReturns(Function &F);
01165 
01166 #ifndef NDEBUG
01167     void GatherStatistics(Function &F, bool AfterOptimization = false);
01168 #endif
01169 
01170     virtual void getAnalysisUsage(AnalysisUsage &AU) const;
01171     virtual bool doInitialization(Module &M);
01172     virtual bool runOnFunction(Function &F);
01173     virtual void releaseMemory();
01174 
01175   public:
01176     static char ID;
01177     ObjCARCOpt() : FunctionPass(ID) {
01178       initializeObjCARCOptPass(*PassRegistry::getPassRegistry());
01179     }
01180   };
01181 }
01182 
01183 char ObjCARCOpt::ID = 0;
01184 INITIALIZE_PASS_BEGIN(ObjCARCOpt,
01185                       "objc-arc", "ObjC ARC optimization", false, false)
01186 INITIALIZE_PASS_DEPENDENCY(ObjCARCAliasAnalysis)
01187 INITIALIZE_PASS_END(ObjCARCOpt,
01188                     "objc-arc", "ObjC ARC optimization", false, false)
01189 
01190 Pass *llvm::createObjCARCOptPass() {
01191   return new ObjCARCOpt();
01192 }
01193 
01194 void ObjCARCOpt::getAnalysisUsage(AnalysisUsage &AU) const {
01195   AU.addRequired<ObjCARCAliasAnalysis>();
01196   AU.addRequired<AliasAnalysis>();
01197   // ARC optimization doesn't currently split critical edges.
01198   AU.setPreservesCFG();
01199 }
01200 
01201 bool ObjCARCOpt::IsRetainBlockOptimizable(const Instruction *Inst) {
01202   // Without the magic metadata tag, we have to assume this might be an
01203   // objc_retainBlock call inserted to convert a block pointer to an id,
01204   // in which case it really is needed.
01205   if (!Inst->getMetadata(CopyOnEscapeMDKind))
01206     return false;
01207 
01208   // If the pointer "escapes" (not including being used in a call),
01209   // the copy may be needed.
01210   if (DoesRetainableObjPtrEscape(Inst))
01211     return false;
01212 
01213   // Otherwise, it's not needed.
01214   return true;
01215 }
01216 
01217 Constant *ObjCARCOpt::getAutoreleaseRVCallee(Module *M) {
01218   if (!AutoreleaseRVCallee) {
01219     LLVMContext &C = M->getContext();
01220     Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C));
01221     Type *Params[] = { I8X };
01222     FunctionType *FTy = FunctionType::get(I8X, Params, /*isVarArg=*/false);
01223     AttributeSet Attribute =
01224       AttributeSet().addAttribute(M->getContext(), AttributeSet::FunctionIndex,
01225                                   Attribute::NoUnwind);
01226     AutoreleaseRVCallee =
01227       M->getOrInsertFunction("objc_autoreleaseReturnValue", FTy,
01228                              Attribute);
01229   }
01230   return AutoreleaseRVCallee;
01231 }
01232 
01233 Constant *ObjCARCOpt::getReleaseCallee(Module *M) {
01234   if (!ReleaseCallee) {
01235     LLVMContext &C = M->getContext();
01236     Type *Params[] = { PointerType::getUnqual(Type::getInt8Ty(C)) };
01237     AttributeSet Attribute =
01238       AttributeSet().addAttribute(M->getContext(), AttributeSet::FunctionIndex,
01239                                   Attribute::NoUnwind);
01240     ReleaseCallee =
01241       M->getOrInsertFunction(
01242         "objc_release",
01243         FunctionType::get(Type::getVoidTy(C), Params, /*isVarArg=*/false),
01244         Attribute);
01245   }
01246   return ReleaseCallee;
01247 }
01248 
01249 Constant *ObjCARCOpt::getRetainCallee(Module *M) {
01250   if (!RetainCallee) {
01251     LLVMContext &C = M->getContext();
01252     Type *Params[] = { PointerType::getUnqual(Type::getInt8Ty(C)) };
01253     AttributeSet Attribute =
01254       AttributeSet().addAttribute(M->getContext(), AttributeSet::FunctionIndex,
01255                                   Attribute::NoUnwind);
01256     RetainCallee =
01257       M->getOrInsertFunction(
01258         "objc_retain",
01259         FunctionType::get(Params[0], Params, /*isVarArg=*/false),
01260         Attribute);
01261   }
01262   return RetainCallee;
01263 }
01264 
01265 Constant *ObjCARCOpt::getRetainBlockCallee(Module *M) {
01266   if (!RetainBlockCallee) {
01267     LLVMContext &C = M->getContext();
01268     Type *Params[] = { PointerType::getUnqual(Type::getInt8Ty(C)) };
01269     // objc_retainBlock is not nounwind because it calls user copy constructors
01270     // which could theoretically throw.
01271     RetainBlockCallee =
01272       M->getOrInsertFunction(
01273         "objc_retainBlock",
01274         FunctionType::get(Params[0], Params, /*isVarArg=*/false),
01275         AttributeSet());
01276   }
01277   return RetainBlockCallee;
01278 }
01279 
01280 Constant *ObjCARCOpt::getAutoreleaseCallee(Module *M) {
01281   if (!AutoreleaseCallee) {
01282     LLVMContext &C = M->getContext();
01283     Type *Params[] = { PointerType::getUnqual(Type::getInt8Ty(C)) };
01284     AttributeSet Attribute =
01285       AttributeSet().addAttribute(M->getContext(), AttributeSet::FunctionIndex,
01286                                   Attribute::NoUnwind);
01287     AutoreleaseCallee =
01288       M->getOrInsertFunction(
01289         "objc_autorelease",
01290         FunctionType::get(Params[0], Params, /*isVarArg=*/false),
01291         Attribute);
01292   }
01293   return AutoreleaseCallee;
01294 }
01295 
01296 /// Turn objc_retainAutoreleasedReturnValue into objc_retain if the operand is
01297 /// not a return value.  Or, if it can be paired with an
01298 /// objc_autoreleaseReturnValue, delete the pair and return true.
01299 bool
01300 ObjCARCOpt::OptimizeRetainRVCall(Function &F, Instruction *RetainRV) {
01301   // Check for the argument being from an immediately preceding call or invoke.
01302   const Value *Arg = GetObjCArg(RetainRV);
01303   ImmutableCallSite CS(Arg);
01304   if (const Instruction *Call = CS.getInstruction()) {
01305     if (Call->getParent() == RetainRV->getParent()) {
01306       BasicBlock::const_iterator I = Call;
01307       ++I;
01308       while (IsNoopInstruction(I)) ++I;
01309       if (&*I == RetainRV)
01310         return false;
01311     } else if (const InvokeInst *II = dyn_cast<InvokeInst>(Call)) {
01312       BasicBlock *RetainRVParent = RetainRV->getParent();
01313       if (II->getNormalDest() == RetainRVParent) {
01314         BasicBlock::const_iterator I = RetainRVParent->begin();
01315         while (IsNoopInstruction(I)) ++I;
01316         if (&*I == RetainRV)
01317           return false;
01318       }
01319     }
01320   }
01321 
01322   // Check for being preceded by an objc_autoreleaseReturnValue on the same
01323   // pointer. In this case, we can delete the pair.
01324   BasicBlock::iterator I = RetainRV, Begin = RetainRV->getParent()->begin();
01325   if (I != Begin) {
01326     do --I; while (I != Begin && IsNoopInstruction(I));
01327     if (GetBasicInstructionClass(I) == IC_AutoreleaseRV &&
01328         GetObjCArg(I) == Arg) {
01329       Changed = true;
01330       ++NumPeeps;
01331 
01332       DEBUG(dbgs() << "Erasing autoreleaseRV,retainRV pair: " << *I << "\n"
01333                    << "Erasing " << *RetainRV << "\n");
01334 
01335       EraseInstruction(I);
01336       EraseInstruction(RetainRV);
01337       return true;
01338     }
01339   }
01340 
01341   // Turn it to a plain objc_retain.
01342   Changed = true;
01343   ++NumPeeps;
01344 
01345   DEBUG(dbgs() << "Transforming objc_retainAutoreleasedReturnValue => "
01346                   "objc_retain since the operand is not a return value.\n"
01347                   "Old = " << *RetainRV << "\n");
01348 
01349   cast<CallInst>(RetainRV)->setCalledFunction(getRetainCallee(F.getParent()));
01350 
01351   DEBUG(dbgs() << "New = " << *RetainRV << "\n");
01352 
01353   return false;
01354 }
01355 
01356 /// Turn objc_autoreleaseReturnValue into objc_autorelease if the result is not
01357 /// used as a return value.
01358 void
01359 ObjCARCOpt::OptimizeAutoreleaseRVCall(Function &F, Instruction *AutoreleaseRV,
01360                                       InstructionClass &Class) {
01361   // Check for a return of the pointer value.
01362   const Value *Ptr = GetObjCArg(AutoreleaseRV);
01363   SmallVector<const Value *, 2> Users;
01364   Users.push_back(Ptr);
01365   do {
01366     Ptr = Users.pop_back_val();
01367     for (Value::const_use_iterator UI = Ptr->use_begin(), UE = Ptr->use_end();
01368          UI != UE; ++UI) {
01369       const User *I = *UI;
01370       if (isa<ReturnInst>(I) || GetBasicInstructionClass(I) == IC_RetainRV)
01371         return;
01372       if (isa<BitCastInst>(I))
01373         Users.push_back(I);
01374     }
01375   } while (!Users.empty());
01376 
01377   Changed = true;
01378   ++NumPeeps;
01379 
01380   DEBUG(dbgs() << "Transforming objc_autoreleaseReturnValue => "
01381                   "objc_autorelease since its operand is not used as a return "
01382                   "value.\n"
01383                   "Old = " << *AutoreleaseRV << "\n");
01384 
01385   CallInst *AutoreleaseRVCI = cast<CallInst>(AutoreleaseRV);
01386   AutoreleaseRVCI->
01387     setCalledFunction(getAutoreleaseCallee(F.getParent()));
01388   AutoreleaseRVCI->setTailCall(false); // Never tail call objc_autorelease.
01389   Class = IC_Autorelease;
01390 
01391   DEBUG(dbgs() << "New: " << *AutoreleaseRV << "\n");
01392 
01393 }
01394 
01395 // \brief Attempt to strength reduce objc_retainBlock calls to objc_retain
01396 // calls.
01397 //
01398 // Specifically: If an objc_retainBlock call has the copy_on_escape metadata and
01399 // does not escape (following the rules of block escaping), strength reduce the
01400 // objc_retainBlock to an objc_retain.
01401 //
01402 // TODO: If an objc_retainBlock call is dominated period by a previous
01403 // objc_retainBlock call, strength reduce the objc_retainBlock to an
01404 // objc_retain.
01405 bool
01406 ObjCARCOpt::OptimizeRetainBlockCall(Function &F, Instruction *Inst,
01407                                     InstructionClass &Class) {
01408   assert(GetBasicInstructionClass(Inst) == Class);
01409   assert(IC_RetainBlock == Class);
01410 
01411   // If we can not optimize Inst, return false.
01412   if (!IsRetainBlockOptimizable(Inst))
01413     return false;
01414 
01415   Changed = true;
01416   ++NumPeeps;
01417 
01418   DEBUG(dbgs() << "Strength reduced retainBlock => retain.\n");
01419   DEBUG(dbgs() << "Old: " << *Inst << "\n");
01420   CallInst *RetainBlock = cast<CallInst>(Inst);
01421   RetainBlock->setCalledFunction(getRetainCallee(F.getParent()));
01422   // Remove copy_on_escape metadata.
01423   RetainBlock->setMetadata(CopyOnEscapeMDKind, 0);
01424   Class = IC_Retain;
01425   DEBUG(dbgs() << "New: " << *Inst << "\n");
01426   return true;
01427 }
01428 
01429 /// Visit each call, one at a time, and make simplifications without doing any
01430 /// additional analysis.
01431 void ObjCARCOpt::OptimizeIndividualCalls(Function &F) {
01432   DEBUG(dbgs() << "\n== ObjCARCOpt::OptimizeIndividualCalls ==\n");
01433   // Reset all the flags in preparation for recomputing them.
01434   UsedInThisFunction = 0;
01435 
01436   // Visit all objc_* calls in F.
01437   for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
01438     Instruction *Inst = &*I++;
01439 
01440     InstructionClass Class = GetBasicInstructionClass(Inst);
01441 
01442     DEBUG(dbgs() << "Visiting: Class: " << Class << "; " << *Inst << "\n");
01443 
01444     switch (Class) {
01445     default: break;
01446 
01447     // Delete no-op casts. These function calls have special semantics, but
01448     // the semantics are entirely implemented via lowering in the front-end,
01449     // so by the time they reach the optimizer, they are just no-op calls
01450     // which return their argument.
01451     //
01452     // There are gray areas here, as the ability to cast reference-counted
01453     // pointers to raw void* and back allows code to break ARC assumptions,
01454     // however these are currently considered to be unimportant.
01455     case IC_NoopCast:
01456       Changed = true;
01457       ++NumNoops;
01458       DEBUG(dbgs() << "Erasing no-op cast: " << *Inst << "\n");
01459       EraseInstruction(Inst);
01460       continue;
01461 
01462     // If the pointer-to-weak-pointer is null, it's undefined behavior.
01463     case IC_StoreWeak:
01464     case IC_LoadWeak:
01465     case IC_LoadWeakRetained:
01466     case IC_InitWeak:
01467     case IC_DestroyWeak: {
01468       CallInst *CI = cast<CallInst>(Inst);
01469       if (IsNullOrUndef(CI->getArgOperand(0))) {
01470         Changed = true;
01471         Type *Ty = CI->getArgOperand(0)->getType();
01472         new StoreInst(UndefValue::get(cast<PointerType>(Ty)->getElementType()),
01473                       Constant::getNullValue(Ty),
01474                       CI);
01475         llvm::Value *NewValue = UndefValue::get(CI->getType());
01476         DEBUG(dbgs() << "A null pointer-to-weak-pointer is undefined behavior."
01477                        "\nOld = " << *CI << "\nNew = " << *NewValue << "\n");
01478         CI->replaceAllUsesWith(NewValue);
01479         CI->eraseFromParent();
01480         continue;
01481       }
01482       break;
01483     }
01484     case IC_CopyWeak:
01485     case IC_MoveWeak: {
01486       CallInst *CI = cast<CallInst>(Inst);
01487       if (IsNullOrUndef(CI->getArgOperand(0)) ||
01488           IsNullOrUndef(CI->getArgOperand(1))) {
01489         Changed = true;
01490         Type *Ty = CI->getArgOperand(0)->getType();
01491         new StoreInst(UndefValue::get(cast<PointerType>(Ty)->getElementType()),
01492                       Constant::getNullValue(Ty),
01493                       CI);
01494 
01495         llvm::Value *NewValue = UndefValue::get(CI->getType());
01496         DEBUG(dbgs() << "A null pointer-to-weak-pointer is undefined behavior."
01497                         "\nOld = " << *CI << "\nNew = " << *NewValue << "\n");
01498 
01499         CI->replaceAllUsesWith(NewValue);
01500         CI->eraseFromParent();
01501         continue;
01502       }
01503       break;
01504     }
01505     case IC_RetainBlock:
01506       // If we strength reduce an objc_retainBlock to an objc_retain, continue
01507       // onto the objc_retain peephole optimizations. Otherwise break.
01508       OptimizeRetainBlockCall(F, Inst, Class);
01509       break;
01510     case IC_RetainRV:
01511       if (OptimizeRetainRVCall(F, Inst))
01512         continue;
01513       break;
01514     case IC_AutoreleaseRV:
01515       OptimizeAutoreleaseRVCall(F, Inst, Class);
01516       break;
01517     }
01518 
01519     // objc_autorelease(x) -> objc_release(x) if x is otherwise unused.
01520     if (IsAutorelease(Class) && Inst->use_empty()) {
01521       CallInst *Call = cast<CallInst>(Inst);
01522       const Value *Arg = Call->getArgOperand(0);
01523       Arg = FindSingleUseIdentifiedObject(Arg);
01524       if (Arg) {
01525         Changed = true;
01526         ++NumAutoreleases;
01527 
01528         // Create the declaration lazily.
01529         LLVMContext &C = Inst->getContext();
01530         CallInst *NewCall =
01531           CallInst::Create(getReleaseCallee(F.getParent()),
01532                            Call->getArgOperand(0), "", Call);
01533         NewCall->setMetadata(ImpreciseReleaseMDKind, MDNode::get(C, None));
01534 
01535         DEBUG(dbgs() << "Replacing autorelease{,RV}(x) with objc_release(x) "
01536               "since x is otherwise unused.\nOld: " << *Call << "\nNew: "
01537               << *NewCall << "\n");
01538 
01539         EraseInstruction(Call);
01540         Inst = NewCall;
01541         Class = IC_Release;
01542       }
01543     }
01544 
01545     // For functions which can never be passed stack arguments, add
01546     // a tail keyword.
01547     if (IsAlwaysTail(Class)) {
01548       Changed = true;
01549       DEBUG(dbgs() << "Adding tail keyword to function since it can never be "
01550                       "passed stack args: " << *Inst << "\n");
01551       cast<CallInst>(Inst)->setTailCall();
01552     }
01553 
01554     // Ensure that functions that can never have a "tail" keyword due to the
01555     // semantics of ARC truly do not do so.
01556     if (IsNeverTail(Class)) {
01557       Changed = true;
01558       DEBUG(dbgs() << "Removing tail keyword from function: " << *Inst <<
01559             "\n");
01560       cast<CallInst>(Inst)->setTailCall(false);
01561     }
01562 
01563     // Set nounwind as needed.
01564     if (IsNoThrow(Class)) {
01565       Changed = true;
01566       DEBUG(dbgs() << "Found no throw class. Setting nounwind on: " << *Inst
01567                    << "\n");
01568       cast<CallInst>(Inst)->setDoesNotThrow();
01569     }
01570 
01571     if (!IsNoopOnNull(Class)) {
01572       UsedInThisFunction |= 1 << Class;
01573       continue;
01574     }
01575 
01576     const Value *Arg = GetObjCArg(Inst);
01577 
01578     // ARC calls with null are no-ops. Delete them.
01579     if (IsNullOrUndef(Arg)) {
01580       Changed = true;
01581       ++NumNoops;
01582       DEBUG(dbgs() << "ARC calls with  null are no-ops. Erasing: " << *Inst
01583             << "\n");
01584       EraseInstruction(Inst);
01585       continue;
01586     }
01587 
01588     // Keep track of which of retain, release, autorelease, and retain_block
01589     // are actually present in this function.
01590     UsedInThisFunction |= 1 << Class;
01591 
01592     // If Arg is a PHI, and one or more incoming values to the
01593     // PHI are null, and the call is control-equivalent to the PHI, and there
01594     // are no relevant side effects between the PHI and the call, the call
01595     // could be pushed up to just those paths with non-null incoming values.
01596     // For now, don't bother splitting critical edges for this.
01597     SmallVector<std::pair<Instruction *, const Value *>, 4> Worklist;
01598     Worklist.push_back(std::make_pair(Inst, Arg));
01599     do {
01600       std::pair<Instruction *, const Value *> Pair = Worklist.pop_back_val();
01601       Inst = Pair.first;
01602       Arg = Pair.second;
01603 
01604       const PHINode *PN = dyn_cast<PHINode>(Arg);
01605       if (!PN) continue;
01606 
01607       // Determine if the PHI has any null operands, or any incoming
01608       // critical edges.
01609       bool HasNull = false;
01610       bool HasCriticalEdges = false;
01611       for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
01612         Value *Incoming =
01613           StripPointerCastsAndObjCCalls(PN->getIncomingValue(i));
01614         if (IsNullOrUndef(Incoming))
01615           HasNull = true;
01616         else if (cast<TerminatorInst>(PN->getIncomingBlock(i)->back())
01617                    .getNumSuccessors() != 1) {
01618           HasCriticalEdges = true;
01619           break;
01620         }
01621       }
01622       // If we have null operands and no critical edges, optimize.
01623       if (!HasCriticalEdges && HasNull) {
01624         SmallPtrSet<Instruction *, 4> DependingInstructions;
01625         SmallPtrSet<const BasicBlock *, 4> Visited;
01626 
01627         // Check that there is nothing that cares about the reference
01628         // count between the call and the phi.
01629         switch (Class) {
01630         case IC_Retain:
01631         case IC_RetainBlock:
01632           // These can always be moved up.
01633           break;
01634         case IC_Release:
01635           // These can't be moved across things that care about the retain
01636           // count.
01637           FindDependencies(NeedsPositiveRetainCount, Arg,
01638                            Inst->getParent(), Inst,
01639                            DependingInstructions, Visited, PA);
01640           break;
01641         case IC_Autorelease:
01642           // These can't be moved across autorelease pool scope boundaries.
01643           FindDependencies(AutoreleasePoolBoundary, Arg,
01644                            Inst->getParent(), Inst,
01645                            DependingInstructions, Visited, PA);
01646           break;
01647         case IC_RetainRV:
01648         case IC_AutoreleaseRV:
01649           // Don't move these; the RV optimization depends on the autoreleaseRV
01650           // being tail called, and the retainRV being immediately after a call
01651           // (which might still happen if we get lucky with codegen layout, but
01652           // it's not worth taking the chance).
01653           continue;
01654         default:
01655           llvm_unreachable("Invalid dependence flavor");
01656         }
01657 
01658         if (DependingInstructions.size() == 1 &&
01659             *DependingInstructions.begin() == PN) {
01660           Changed = true;
01661           ++NumPartialNoops;
01662           // Clone the call into each predecessor that has a non-null value.
01663           CallInst *CInst = cast<CallInst>(Inst);
01664           Type *ParamTy = CInst->getArgOperand(0)->getType();
01665           for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
01666             Value *Incoming =
01667               StripPointerCastsAndObjCCalls(PN->getIncomingValue(i));
01668             if (!IsNullOrUndef(Incoming)) {
01669               CallInst *Clone = cast<CallInst>(CInst->clone());
01670               Value *Op = PN->getIncomingValue(i);
01671               Instruction *InsertPos = &PN->getIncomingBlock(i)->back();
01672               if (Op->getType() != ParamTy)
01673                 Op = new BitCastInst(Op, ParamTy, "", InsertPos);
01674               Clone->setArgOperand(0, Op);
01675               Clone->insertBefore(InsertPos);
01676 
01677               DEBUG(dbgs() << "Cloning "
01678                            << *CInst << "\n"
01679                            "And inserting clone at " << *InsertPos << "\n");
01680               Worklist.push_back(std::make_pair(Clone, Incoming));
01681             }
01682           }
01683           // Erase the original call.
01684           DEBUG(dbgs() << "Erasing: " << *CInst << "\n");
01685           EraseInstruction(CInst);
01686           continue;
01687         }
01688       }
01689     } while (!Worklist.empty());
01690   }
01691 }
01692 
01693 /// If we have a top down pointer in the S_Use state, make sure that there are
01694 /// no CFG hazards by checking the states of various bottom up pointers.
01695 static void CheckForUseCFGHazard(const Sequence SuccSSeq,
01696                                  const bool SuccSRRIKnownSafe,
01697                                  PtrState &S,
01698                                  bool &SomeSuccHasSame,
01699                                  bool &AllSuccsHaveSame,
01700                                  bool &NotAllSeqEqualButKnownSafe,
01701                                  bool &ShouldContinue) {
01702   switch (SuccSSeq) {
01703   case S_CanRelease: {
01704     if (!S.RRI.KnownSafe && !SuccSRRIKnownSafe) {
01705       S.ClearSequenceProgress();
01706       break;
01707     }
01708     S.RRI.CFGHazardAfflicted = true;
01709     ShouldContinue = true;
01710     break;
01711   }
01712   case S_Use:
01713     SomeSuccHasSame = true;
01714     break;
01715   case S_Stop:
01716   case S_Release:
01717   case S_MovableRelease:
01718     if (!S.RRI.KnownSafe && !SuccSRRIKnownSafe)
01719       AllSuccsHaveSame = false;
01720     else
01721       NotAllSeqEqualButKnownSafe = true;
01722     break;
01723   case S_Retain:
01724     llvm_unreachable("bottom-up pointer in retain state!");
01725   case S_None:
01726     llvm_unreachable("This should have been handled earlier.");
01727   }
01728 }
01729 
01730 /// If we have a Top Down pointer in the S_CanRelease state, make sure that
01731 /// there are no CFG hazards by checking the states of various bottom up
01732 /// pointers.
01733 static void CheckForCanReleaseCFGHazard(const Sequence SuccSSeq,
01734                                         const bool SuccSRRIKnownSafe,
01735                                         PtrState &S,
01736                                         bool &SomeSuccHasSame,
01737                                         bool &AllSuccsHaveSame,
01738                                         bool &NotAllSeqEqualButKnownSafe) {
01739   switch (SuccSSeq) {
01740   case S_CanRelease:
01741     SomeSuccHasSame = true;
01742     break;
01743   case S_Stop:
01744   case S_Release:
01745   case S_MovableRelease:
01746   case S_Use:
01747     if (!S.RRI.KnownSafe && !SuccSRRIKnownSafe)
01748       AllSuccsHaveSame = false;
01749     else
01750       NotAllSeqEqualButKnownSafe = true;
01751     break;
01752   case S_Retain:
01753     llvm_unreachable("bottom-up pointer in retain state!");
01754   case S_None:
01755     llvm_unreachable("This should have been handled earlier.");
01756   }
01757 }
01758 
01759 /// Check for critical edges, loop boundaries, irreducible control flow, or
01760 /// other CFG structures where moving code across the edge would result in it
01761 /// being executed more.
01762 void
01763 ObjCARCOpt::CheckForCFGHazards(const BasicBlock *BB,
01764                                DenseMap<const BasicBlock *, BBState> &BBStates,
01765                                BBState &MyStates) const {
01766   // If any top-down local-use or possible-dec has a succ which is earlier in
01767   // the sequence, forget it.
01768   for (BBState::ptr_iterator I = MyStates.top_down_ptr_begin(),
01769          E = MyStates.top_down_ptr_end(); I != E; ++I) {
01770     PtrState &S = I->second;
01771     const Sequence Seq = I->second.GetSeq();
01772 
01773     // We only care about S_Retain, S_CanRelease, and S_Use.
01774     if (Seq == S_None)
01775       continue;
01776 
01777     // Make sure that if extra top down states are added in the future that this
01778     // code is updated to handle it.
01779     assert((Seq == S_Retain || Seq == S_CanRelease || Seq == S_Use) &&
01780            "Unknown top down sequence state.");
01781 
01782     const Value *Arg = I->first;
01783     const TerminatorInst *TI = cast<TerminatorInst>(&BB->back());
01784     bool SomeSuccHasSame = false;
01785     bool AllSuccsHaveSame = true;
01786     bool NotAllSeqEqualButKnownSafe = false;
01787 
01788     succ_const_iterator SI(TI), SE(TI, false);
01789 
01790     for (; SI != SE; ++SI) {
01791       // If VisitBottomUp has pointer information for this successor, take
01792       // what we know about it.
01793       const DenseMap<const BasicBlock *, BBState>::iterator BBI =
01794         BBStates.find(*SI);
01795       assert(BBI != BBStates.end());
01796       const PtrState &SuccS = BBI->second.getPtrBottomUpState(Arg);
01797       const Sequence SuccSSeq = SuccS.GetSeq();
01798 
01799       // If bottom up, the pointer is in an S_None state, clear the sequence
01800       // progress since the sequence in the bottom up state finished
01801       // suggesting a mismatch in between retains/releases. This is true for
01802       // all three cases that we are handling here: S_Retain, S_Use, and
01803       // S_CanRelease.
01804       if (SuccSSeq == S_None) {
01805         S.ClearSequenceProgress();
01806         continue;
01807       }
01808 
01809       // If we have S_Use or S_CanRelease, perform our check for cfg hazard
01810       // checks.
01811       const bool SuccSRRIKnownSafe = SuccS.RRI.KnownSafe;
01812 
01813       // *NOTE* We do not use Seq from above here since we are allowing for
01814       // S.GetSeq() to change while we are visiting basic blocks.
01815       switch(S.GetSeq()) {
01816       case S_Use: {
01817         bool ShouldContinue = false;
01818         CheckForUseCFGHazard(SuccSSeq, SuccSRRIKnownSafe, S, SomeSuccHasSame,
01819                              AllSuccsHaveSame, NotAllSeqEqualButKnownSafe,
01820                              ShouldContinue);
01821         if (ShouldContinue)
01822           continue;
01823         break;
01824       }
01825       case S_CanRelease: {
01826         CheckForCanReleaseCFGHazard(SuccSSeq, SuccSRRIKnownSafe, S,
01827                                     SomeSuccHasSame, AllSuccsHaveSame,
01828                                     NotAllSeqEqualButKnownSafe);
01829         break;
01830       }
01831       case S_Retain:
01832       case S_None:
01833       case S_Stop:
01834       case S_Release:
01835       case S_MovableRelease:
01836         break;
01837       }
01838     }
01839 
01840     // If the state at the other end of any of the successor edges
01841     // matches the current state, require all edges to match. This
01842     // guards against loops in the middle of a sequence.
01843     if (SomeSuccHasSame && !AllSuccsHaveSame) {
01844       S.ClearSequenceProgress();
01845     } else if (NotAllSeqEqualButKnownSafe) {
01846       // If we would have cleared the state foregoing the fact that we are known
01847       // safe, stop code motion. This is because whether or not it is safe to
01848       // remove RR pairs via KnownSafe is an orthogonal concept to whether we
01849       // are allowed to perform code motion.
01850       S.RRI.CFGHazardAfflicted = true;
01851     }
01852   }
01853 }
01854 
01855 bool
01856 ObjCARCOpt::VisitInstructionBottomUp(Instruction *Inst,
01857                                      BasicBlock *BB,
01858                                      MapVector<Value *, RRInfo> &Retains,
01859                                      BBState &MyStates) {
01860   bool NestingDetected = false;
01861   InstructionClass Class = GetInstructionClass(Inst);
01862   const Value *Arg = 0;
01863 
01864   DEBUG(dbgs() << "Class: " << Class << "\n");
01865 
01866   switch (Class) {
01867   case IC_Release: {
01868     Arg = GetObjCArg(Inst);
01869 
01870     PtrState &S = MyStates.getPtrBottomUpState(Arg);
01871 
01872     // If we see two releases in a row on the same pointer. If so, make
01873     // a note, and we'll cicle back to revisit it after we've
01874     // hopefully eliminated the second release, which may allow us to
01875     // eliminate the first release too.
01876     // Theoretically we could implement removal of nested retain+release
01877     // pairs by making PtrState hold a stack of states, but this is
01878     // simple and avoids adding overhead for the non-nested case.
01879     if (S.GetSeq() == S_Release || S.GetSeq() == S_MovableRelease) {
01880       DEBUG(dbgs() << "Found nested releases (i.e. a release pair)\n");
01881       NestingDetected = true;
01882     }
01883 
01884     MDNode *ReleaseMetadata = Inst->getMetadata(ImpreciseReleaseMDKind);
01885     Sequence NewSeq = ReleaseMetadata ? S_MovableRelease : S_Release;
01886     ANNOTATE_BOTTOMUP(Inst, Arg, S.GetSeq(), NewSeq);
01887     S.ResetSequenceProgress(NewSeq);
01888     S.RRI.ReleaseMetadata = ReleaseMetadata;
01889     S.RRI.KnownSafe = S.HasKnownPositiveRefCount();
01890     S.RRI.IsTailCallRelease = cast<CallInst>(Inst)->isTailCall();
01891     S.RRI.Calls.insert(Inst);
01892     S.SetKnownPositiveRefCount();
01893     break;
01894   }
01895   case IC_RetainBlock:
01896     // In OptimizeIndividualCalls, we have strength reduced all optimizable
01897     // objc_retainBlocks to objc_retains. Thus at this point any
01898     // objc_retainBlocks that we see are not optimizable.
01899     break;
01900   case IC_Retain:
01901   case IC_RetainRV: {
01902     Arg = GetObjCArg(Inst);
01903 
01904     PtrState &S = MyStates.getPtrBottomUpState(Arg);
01905     S.SetKnownPositiveRefCount();
01906 
01907     Sequence OldSeq = S.GetSeq();
01908     switch (OldSeq) {
01909     case S_Stop:
01910     case S_Release:
01911     case S_MovableRelease:
01912     case S_Use:
01913       // If OldSeq is not S_Use or OldSeq is S_Use and we are tracking an
01914       // imprecise release, clear our reverse insertion points.
01915       if (OldSeq != S_Use || S.RRI.IsTrackingImpreciseReleases())
01916         S.RRI.ReverseInsertPts.clear();
01917       // FALL THROUGH
01918     case S_CanRelease:
01919       // Don't do retain+release tracking for IC_RetainRV, because it's
01920       // better to let it remain as the first instruction after a call.
01921       if (Class != IC_RetainRV)
01922         Retains[Inst] = S.RRI;
01923       S.ClearSequenceProgress();
01924       break;
01925     case S_None:
01926       break;
01927     case S_Retain:
01928       llvm_unreachable("bottom-up pointer in retain state!");
01929     }
01930     ANNOTATE_BOTTOMUP(Inst, Arg, OldSeq, S.GetSeq());
01931     // A retain moving bottom up can be a use.
01932     break;
01933   }
01934   case IC_AutoreleasepoolPop:
01935     // Conservatively, clear MyStates for all known pointers.
01936     MyStates.clearBottomUpPointers();
01937     return NestingDetected;
01938   case IC_AutoreleasepoolPush:
01939   case IC_None:
01940     // These are irrelevant.
01941     return NestingDetected;
01942   case IC_User:
01943     // If we have a store into an alloca of a pointer we are tracking, the
01944     // pointer has multiple owners implying that we must be more conservative.
01945     //
01946     // This comes up in the context of a pointer being ``KnownSafe''. In the
01947     // presense of a block being initialized, the frontend will emit the
01948     // objc_retain on the original pointer and the release on the pointer loaded
01949     // from the alloca. The optimizer will through the provenance analysis
01950     // realize that the two are related, but since we only require KnownSafe in
01951     // one direction, will match the inner retain on the original pointer with
01952     // the guard release on the original pointer. This is fixed by ensuring that
01953     // in the presense of allocas we only unconditionally remove pointers if
01954     // both our retain and our release are KnownSafe.
01955     if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
01956       if (AreAnyUnderlyingObjectsAnAlloca(SI->getPointerOperand())) {
01957         BBState::ptr_iterator I = MyStates.findPtrBottomUpState(
01958           StripPointerCastsAndObjCCalls(SI->getValueOperand()));
01959         if (I != MyStates.bottom_up_ptr_end())
01960           MultiOwnersSet.insert(I->first);
01961       }
01962     }
01963     break;
01964   default:
01965     break;
01966   }
01967 
01968   // Consider any other possible effects of this instruction on each
01969   // pointer being tracked.
01970   for (BBState::ptr_iterator MI = MyStates.bottom_up_ptr_begin(),
01971        ME = MyStates.bottom_up_ptr_end(); MI != ME; ++MI) {
01972     const Value *Ptr = MI->first;
01973     if (Ptr == Arg)
01974       continue; // Handled above.
01975     PtrState &S = MI->second;
01976     Sequence Seq = S.GetSeq();
01977 
01978     // Check for possible releases.
01979     if (CanAlterRefCount(Inst, Ptr, PA, Class)) {
01980       DEBUG(dbgs() << "CanAlterRefCount: Seq: " << Seq << "; " << *Ptr
01981             << "\n");
01982       S.ClearKnownPositiveRefCount();
01983       switch (Seq) {
01984       case S_Use:
01985         S.SetSeq(S_CanRelease);
01986         ANNOTATE_BOTTOMUP(Inst, Ptr, Seq, S.GetSeq());
01987         continue;
01988       case S_CanRelease:
01989       case S_Release:
01990       case S_MovableRelease:
01991       case S_Stop:
01992       case S_None:
01993         break;
01994       case S_Retain:
01995         llvm_unreachable("bottom-up pointer in retain state!");
01996       }
01997     }
01998 
01999     // Check for possible direct uses.
02000     switch (Seq) {
02001     case S_Release:
02002     case S_MovableRelease:
02003       if (CanUse(Inst, Ptr, PA, Class)) {
02004         DEBUG(dbgs() << "CanUse: Seq: " << Seq << "; " << *Ptr
02005               << "\n");
02006         assert(S.RRI.ReverseInsertPts.empty());
02007         // If this is an invoke instruction, we're scanning it as part of
02008         // one of its successor blocks, since we can't insert code after it
02009         // in its own block, and we don't want to split critical edges.
02010         if (isa<InvokeInst>(Inst))
02011           S.RRI.ReverseInsertPts.insert(BB->getFirstInsertionPt());
02012         else
02013           S.RRI.ReverseInsertPts.insert(llvm::next(BasicBlock::iterator(Inst)));
02014         S.SetSeq(S_Use);
02015         ANNOTATE_BOTTOMUP(Inst, Ptr, Seq, S_Use);
02016       } else if (Seq == S_Release && IsUser(Class)) {
02017         DEBUG(dbgs() << "PreciseReleaseUse: Seq: " << Seq << "; " << *Ptr
02018               << "\n");
02019         // Non-movable releases depend on any possible objc pointer use.
02020         S.SetSeq(S_Stop);
02021         ANNOTATE_BOTTOMUP(Inst, Ptr, S_Release, S_Stop);
02022         assert(S.RRI.ReverseInsertPts.empty());
02023         // As above; handle invoke specially.
02024         if (isa<InvokeInst>(Inst))
02025           S.RRI.ReverseInsertPts.insert(BB->getFirstInsertionPt());
02026         else
02027           S.RRI.ReverseInsertPts.insert(llvm::next(BasicBlock::iterator(Inst)));
02028       }
02029       break;
02030     case S_Stop:
02031       if (CanUse(Inst, Ptr, PA, Class)) {
02032         DEBUG(dbgs() << "PreciseStopUse: Seq: " << Seq << "; " << *Ptr
02033               << "\n");
02034         S.SetSeq(S_Use);
02035         ANNOTATE_BOTTOMUP(Inst, Ptr, Seq, S_Use);
02036       }
02037       break;
02038     case S_CanRelease:
02039     case S_Use:
02040     case S_None:
02041       break;
02042     case S_Retain:
02043       llvm_unreachable("bottom-up pointer in retain state!");
02044     }
02045   }
02046 
02047   return NestingDetected;
02048 }
02049 
02050 bool
02051 ObjCARCOpt::VisitBottomUp(BasicBlock *BB,
02052                           DenseMap<const BasicBlock *, BBState> &BBStates,
02053                           MapVector<Value *, RRInfo> &Retains) {
02054 
02055   DEBUG(dbgs() << "\n== ObjCARCOpt::VisitBottomUp ==\n");
02056 
02057   bool NestingDetected = false;
02058   BBState &MyStates = BBStates[BB];
02059 
02060   // Merge the states from each successor to compute the initial state
02061   // for the current block.
02062   BBState::edge_iterator SI(MyStates.succ_begin()),
02063                          SE(MyStates.succ_end());
02064   if (SI != SE) {
02065     const BasicBlock *Succ = *SI;
02066     DenseMap<const BasicBlock *, BBState>::iterator I = BBStates.find(Succ);
02067     assert(I != BBStates.end());
02068     MyStates.InitFromSucc(I->second);
02069     ++SI;
02070     for (; SI != SE; ++SI) {
02071       Succ = *SI;
02072       I = BBStates.find(Succ);
02073       assert(I != BBStates.end());
02074       MyStates.MergeSucc(I->second);
02075     }
02076   }
02077 
02078   // If ARC Annotations are enabled, output the current state of pointers at the
02079   // bottom of the basic block.
02080   ANNOTATE_BOTTOMUP_BBEND(MyStates, BB);
02081 
02082   // Visit all the instructions, bottom-up.
02083   for (BasicBlock::iterator I = BB->end(), E = BB->begin(); I != E; --I) {
02084     Instruction *Inst = llvm::prior(I);
02085 
02086     // Invoke instructions are visited as part of their successors (below).
02087     if (isa<InvokeInst>(Inst))
02088       continue;
02089 
02090     DEBUG(dbgs() << "Visiting " << *Inst << "\n");
02091 
02092     NestingDetected |= VisitInstructionBottomUp(Inst, BB, Retains, MyStates);
02093   }
02094 
02095   // If there's a predecessor with an invoke, visit the invoke as if it were
02096   // part of this block, since we can't insert code after an invoke in its own
02097   // block, and we don't want to split critical edges.
02098   for (BBState::edge_iterator PI(MyStates.pred_begin()),
02099        PE(MyStates.pred_end()); PI != PE; ++PI) {
02100     BasicBlock *Pred = *PI;
02101     if (InvokeInst *II = dyn_cast<InvokeInst>(&Pred->back()))
02102       NestingDetected |= VisitInstructionBottomUp(II, BB, Retains, MyStates);
02103   }
02104 
02105   // If ARC Annotations are enabled, output the current state of pointers at the
02106   // top of the basic block.
02107   ANNOTATE_BOTTOMUP_BBSTART(MyStates, BB);
02108 
02109   return NestingDetected;
02110 }
02111 
02112 bool
02113 ObjCARCOpt::VisitInstructionTopDown(Instruction *Inst,
02114                                     DenseMap<Value *, RRInfo> &Releases,
02115                                     BBState &MyStates) {
02116   bool NestingDetected = false;
02117   InstructionClass Class = GetInstructionClass(Inst);
02118   const Value *Arg = 0;
02119 
02120   switch (Class) {
02121   case IC_RetainBlock:
02122     // In OptimizeIndividualCalls, we have strength reduced all optimizable
02123     // objc_retainBlocks to objc_retains. Thus at this point any
02124     // objc_retainBlocks that we see are not optimizable.
02125     break;
02126   case IC_Retain:
02127   case IC_RetainRV: {
02128     Arg = GetObjCArg(Inst);
02129 
02130     PtrState &S = MyStates.getPtrTopDownState(Arg);
02131 
02132     // Don't do retain+release tracking for IC_RetainRV, because it's
02133     // better to let it remain as the first instruction after a call.
02134     if (Class != IC_RetainRV) {
02135       // If we see two retains in a row on the same pointer. If so, make
02136       // a note, and we'll cicle back to revisit it after we've
02137       // hopefully eliminated the second retain, which may allow us to
02138       // eliminate the first retain too.
02139       // Theoretically we could implement removal of nested retain+release
02140       // pairs by making PtrState hold a stack of states, but this is
02141       // simple and avoids adding overhead for the non-nested case.
02142       if (S.GetSeq() == S_Retain)
02143         NestingDetected = true;
02144 
02145       ANNOTATE_TOPDOWN(Inst, Arg, S.GetSeq(), S_Retain);
02146       S.ResetSequenceProgress(S_Retain);
02147       S.RRI.KnownSafe = S.HasKnownPositiveRefCount();
02148       S.RRI.Calls.insert(Inst);
02149     }
02150 
02151     S.SetKnownPositiveRefCount();
02152 
02153     // A retain can be a potential use; procede to the generic checking
02154     // code below.
02155     break;
02156   }
02157   case IC_Release: {
02158     Arg = GetObjCArg(Inst);
02159 
02160     PtrState &S = MyStates.getPtrTopDownState(Arg);
02161     S.ClearKnownPositiveRefCount();
02162 
02163     Sequence OldSeq = S.GetSeq();
02164 
02165     MDNode *ReleaseMetadata = Inst->getMetadata(ImpreciseReleaseMDKind);
02166 
02167     switch (OldSeq) {
02168     case S_Retain:
02169     case S_CanRelease:
02170       if (OldSeq == S_Retain || ReleaseMetadata != 0)
02171         S.RRI.ReverseInsertPts.clear();
02172       // FALL THROUGH
02173     case S_Use:
02174       S.RRI.ReleaseMetadata = ReleaseMetadata;
02175       S.RRI.IsTailCallRelease = cast<CallInst>(Inst)->isTailCall();
02176       Releases[Inst] = S.RRI;
02177       ANNOTATE_TOPDOWN(Inst, Arg, S.GetSeq(), S_None);
02178       S.ClearSequenceProgress();
02179       break;
02180     case S_None:
02181       break;
02182     case S_Stop:
02183     case S_Release:
02184     case S_MovableRelease:
02185       llvm_unreachable("top-down pointer in release state!");
02186     }
02187     break;
02188   }
02189   case IC_AutoreleasepoolPop:
02190     // Conservatively, clear MyStates for all known pointers.
02191     MyStates.clearTopDownPointers();
02192     return NestingDetected;
02193   case IC_AutoreleasepoolPush:
02194   case IC_None:
02195     // These are irrelevant.
02196     return NestingDetected;
02197   default:
02198     break;
02199   }
02200 
02201   // Consider any other possible effects of this instruction on each
02202   // pointer being tracked.
02203   for (BBState::ptr_iterator MI = MyStates.top_down_ptr_begin(),
02204        ME = MyStates.top_down_ptr_end(); MI != ME; ++MI) {
02205     const Value *Ptr = MI->first;
02206     if (Ptr == Arg)
02207       continue; // Handled above.
02208     PtrState &S = MI->second;
02209     Sequence Seq = S.GetSeq();
02210 
02211     // Check for possible releases.
02212     if (CanAlterRefCount(Inst, Ptr, PA, Class)) {
02213       DEBUG(dbgs() << "CanAlterRefCount: Seq: " << Seq << "; " << *Ptr
02214             << "\n");
02215       S.ClearKnownPositiveRefCount();
02216       switch (Seq) {
02217       case S_Retain:
02218         S.SetSeq(S_CanRelease);
02219         ANNOTATE_TOPDOWN(Inst, Ptr, Seq, S_CanRelease);
02220         assert(S.RRI.ReverseInsertPts.empty());
02221         S.RRI.ReverseInsertPts.insert(Inst);
02222 
02223         // One call can't cause a transition from S_Retain to S_CanRelease
02224         // and S_CanRelease to S_Use. If we've made the first transition,
02225         // we're done.
02226         continue;
02227       case S_Use:
02228       case S_CanRelease:
02229       case S_None:
02230         break;
02231       case S_Stop:
02232       case S_Release:
02233       case S_MovableRelease:
02234         llvm_unreachable("top-down pointer in release state!");
02235       }
02236     }
02237 
02238     // Check for possible direct uses.
02239     switch (Seq) {
02240     case S_CanRelease:
02241       if (CanUse(Inst, Ptr, PA, Class)) {
02242         DEBUG(dbgs() << "CanUse: Seq: " << Seq << "; " << *Ptr
02243               << "\n");
02244         S.SetSeq(S_Use);
02245         ANNOTATE_TOPDOWN(Inst, Ptr, Seq, S_Use);
02246       }
02247       break;
02248     case S_Retain:
02249     case S_Use:
02250     case S_None:
02251       break;
02252     case S_Stop:
02253     case S_Release:
02254     case S_MovableRelease:
02255       llvm_unreachable("top-down pointer in release state!");
02256     }
02257   }
02258 
02259   return NestingDetected;
02260 }
02261 
02262 bool
02263 ObjCARCOpt::VisitTopDown(BasicBlock *BB,
02264                          DenseMap<const BasicBlock *, BBState> &BBStates,
02265                          DenseMap<Value *, RRInfo> &Releases) {
02266   DEBUG(dbgs() << "\n== ObjCARCOpt::VisitTopDown ==\n");
02267   bool NestingDetected = false;
02268   BBState &MyStates = BBStates[BB];
02269 
02270   // Merge the states from each predecessor to compute the initial state
02271   // for the current block.
02272   BBState::edge_iterator PI(MyStates.pred_begin()),
02273                          PE(MyStates.pred_end());
02274   if (PI != PE) {
02275     const BasicBlock *Pred = *PI;
02276     DenseMap<const BasicBlock *, BBState>::iterator I = BBStates.find(Pred);
02277     assert(I != BBStates.end());
02278     MyStates.InitFromPred(I->second);
02279     ++PI;
02280     for (; PI != PE; ++PI) {
02281       Pred = *PI;
02282       I = BBStates.find(Pred);
02283       assert(I != BBStates.end());
02284       MyStates.MergePred(I->second);
02285     }
02286   }
02287 
02288   // If ARC Annotations are enabled, output the current state of pointers at the
02289   // top of the basic block.
02290   ANNOTATE_TOPDOWN_BBSTART(MyStates, BB);
02291 
02292   // Visit all the instructions, top-down.
02293   for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
02294     Instruction *Inst = I;
02295 
02296     DEBUG(dbgs() << "Visiting " << *Inst << "\n");
02297 
02298     NestingDetected |= VisitInstructionTopDown(Inst, Releases, MyStates);
02299   }
02300 
02301   // If ARC Annotations are enabled, output the current state of pointers at the
02302   // bottom of the basic block.
02303   ANNOTATE_TOPDOWN_BBEND(MyStates, BB);
02304 
02305 #ifdef ARC_ANNOTATIONS
02306   if (!(EnableARCAnnotations && DisableCheckForCFGHazards))
02307 #endif
02308   CheckForCFGHazards(BB, BBStates, MyStates);
02309   return NestingDetected;
02310 }
02311 
02312 static void
02313 ComputePostOrders(Function &F,
02314                   SmallVectorImpl<BasicBlock *> &PostOrder,
02315                   SmallVectorImpl<BasicBlock *> &ReverseCFGPostOrder,
02316                   unsigned NoObjCARCExceptionsMDKind,
02317                   DenseMap<const BasicBlock *, BBState> &BBStates) {
02318   /// The visited set, for doing DFS walks.
02319   SmallPtrSet<BasicBlock *, 16> Visited;
02320 
02321   // Do DFS, computing the PostOrder.
02322   SmallPtrSet<BasicBlock *, 16> OnStack;
02323   SmallVector<std::pair<BasicBlock *, succ_iterator>, 16> SuccStack;
02324 
02325   // Functions always have exactly one entry block, and we don't have
02326   // any other block that we treat like an entry block.
02327   BasicBlock *EntryBB = &F.getEntryBlock();
02328   BBState &MyStates = BBStates[EntryBB];
02329   MyStates.SetAsEntry();
02330   TerminatorInst *EntryTI = cast<TerminatorInst>(&EntryBB->back());
02331   SuccStack.push_back(std::make_pair(EntryBB, succ_iterator(EntryTI)));
02332   Visited.insert(EntryBB);
02333   OnStack.insert(EntryBB);
02334   do {
02335   dfs_next_succ:
02336     BasicBlock *CurrBB = SuccStack.back().first;
02337     TerminatorInst *TI = cast<TerminatorInst>(&CurrBB->back());
02338     succ_iterator SE(TI, false);
02339 
02340     while (SuccStack.back().second != SE) {
02341       BasicBlock *SuccBB = *SuccStack.back().second++;
02342       if (Visited.insert(SuccBB)) {
02343         TerminatorInst *TI = cast<TerminatorInst>(&SuccBB->back());
02344         SuccStack.push_back(std::make_pair(SuccBB, succ_iterator(TI)));
02345         BBStates[CurrBB].addSucc(SuccBB);
02346         BBState &SuccStates = BBStates[SuccBB];
02347         SuccStates.addPred(CurrBB);
02348         OnStack.insert(SuccBB);
02349         goto dfs_next_succ;
02350       }
02351 
02352       if (!OnStack.count(SuccBB)) {
02353         BBStates[CurrBB].addSucc(SuccBB);
02354         BBStates[SuccBB].addPred(CurrBB);
02355       }
02356     }
02357     OnStack.erase(CurrBB);
02358     PostOrder.push_back(CurrBB);
02359     SuccStack.pop_back();
02360   } while (!SuccStack.empty());
02361 
02362   Visited.clear();
02363 
02364   // Do reverse-CFG DFS, computing the reverse-CFG PostOrder.
02365   // Functions may have many exits, and there also blocks which we treat
02366   // as exits due to ignored edges.
02367   SmallVector<std::pair<BasicBlock *, BBState::edge_iterator>, 16> PredStack;
02368   for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
02369     BasicBlock *ExitBB = I;
02370     BBState &MyStates = BBStates[ExitBB];
02371     if (!MyStates.isExit())
02372       continue;
02373 
02374     MyStates.SetAsExit();
02375 
02376     PredStack.push_back(std::make_pair(ExitBB, MyStates.pred_begin()));
02377     Visited.insert(ExitBB);
02378     while (!PredStack.empty()) {
02379     reverse_dfs_next_succ:
02380       BBState::edge_iterator PE = BBStates[PredStack.back().first].pred_end();
02381       while (PredStack.back().second != PE) {
02382         BasicBlock *BB = *PredStack.back().second++;
02383         if (Visited.insert(BB)) {
02384           PredStack.push_back(std::make_pair(BB, BBStates[BB].pred_begin()));
02385           goto reverse_dfs_next_succ;
02386         }
02387       }
02388       ReverseCFGPostOrder.push_back(PredStack.pop_back_val().first);
02389     }
02390   }
02391 }
02392 
02393 // Visit the function both top-down and bottom-up.
02394 bool
02395 ObjCARCOpt::Visit(Function &F,
02396                   DenseMap<const BasicBlock *, BBState> &BBStates,
02397                   MapVector<Value *, RRInfo> &Retains,
02398                   DenseMap<Value *, RRInfo> &Releases) {
02399 
02400   // Use reverse-postorder traversals, because we magically know that loops
02401   // will be well behaved, i.e. they won't repeatedly call retain on a single
02402   // pointer without doing a release. We can't use the ReversePostOrderTraversal
02403   // class here because we want the reverse-CFG postorder to consider each
02404   // function exit point, and we want to ignore selected cycle edges.
02405   SmallVector<BasicBlock *, 16> PostOrder;
02406   SmallVector<BasicBlock *, 16> ReverseCFGPostOrder;
02407   ComputePostOrders(F, PostOrder, ReverseCFGPostOrder,
02408                     NoObjCARCExceptionsMDKind,
02409                     BBStates);
02410 
02411   // Use reverse-postorder on the reverse CFG for bottom-up.
02412   bool BottomUpNestingDetected = false;
02413   for (SmallVectorImpl<BasicBlock *>::const_reverse_iterator I =
02414        ReverseCFGPostOrder.rbegin(), E = ReverseCFGPostOrder.rend();
02415        I != E; ++I)
02416     BottomUpNestingDetected |= VisitBottomUp(*I, BBStates, Retains);
02417 
02418   // Use reverse-postorder for top-down.
02419   bool TopDownNestingDetected = false;
02420   for (SmallVectorImpl<BasicBlock *>::const_reverse_iterator I =
02421        PostOrder.rbegin(), E = PostOrder.rend();
02422        I != E; ++I)
02423     TopDownNestingDetected |= VisitTopDown(*I, BBStates, Releases);
02424 
02425   return TopDownNestingDetected && BottomUpNestingDetected;
02426 }
02427 
02428 /// Move the calls in RetainsToMove and ReleasesToMove.
02429 void ObjCARCOpt::MoveCalls(Value *Arg,
02430                            RRInfo &RetainsToMove,
02431                            RRInfo &ReleasesToMove,
02432                            MapVector<Value *, RRInfo> &Retains,
02433                            DenseMap<Value *, RRInfo> &Releases,
02434                            SmallVectorImpl<Instruction *> &DeadInsts,
02435                            Module *M) {
02436   Type *ArgTy = Arg->getType();
02437   Type *ParamTy = PointerType::getUnqual(Type::getInt8Ty(ArgTy->getContext()));
02438 
02439   DEBUG(dbgs() << "== ObjCARCOpt::MoveCalls ==\n");
02440 
02441   // Insert the new retain and release calls.
02442   for (SmallPtrSet<Instruction *, 2>::const_iterator
02443        PI = ReleasesToMove.ReverseInsertPts.begin(),
02444        PE = ReleasesToMove.ReverseInsertPts.end(); PI != PE; ++PI) {
02445     Instruction *InsertPt = *PI;
02446     Value *MyArg = ArgTy == ParamTy ? Arg :
02447                    new BitCastInst(Arg, ParamTy, "", InsertPt);
02448     CallInst *Call =
02449       CallInst::Create(getRetainCallee(M), MyArg, "", InsertPt);
02450     Call->setDoesNotThrow();
02451     Call->setTailCall();
02452 
02453     DEBUG(dbgs() << "Inserting new Retain: " << *Call << "\n"
02454                     "At insertion point: " << *InsertPt << "\n");
02455   }
02456   for (SmallPtrSet<Instruction *, 2>::const_iterator
02457        PI = RetainsToMove.ReverseInsertPts.begin(),
02458        PE = RetainsToMove.ReverseInsertPts.end(); PI != PE; ++PI) {
02459     Instruction *InsertPt = *PI;
02460     Value *MyArg = ArgTy == ParamTy ? Arg :
02461                    new BitCastInst(Arg, ParamTy, "", InsertPt);
02462     CallInst *Call = CallInst::Create(getReleaseCallee(M), MyArg,
02463                                       "", InsertPt);
02464     // Attach a clang.imprecise_release metadata tag, if appropriate.
02465     if (MDNode *M = ReleasesToMove.ReleaseMetadata)
02466       Call->setMetadata(ImpreciseReleaseMDKind, M);
02467     Call->setDoesNotThrow();
02468     if (ReleasesToMove.IsTailCallRelease)
02469       Call->setTailCall();
02470 
02471     DEBUG(dbgs() << "Inserting new Release: " << *Call << "\n"
02472                     "At insertion point: " << *InsertPt << "\n");
02473   }
02474 
02475   // Delete the original retain and release calls.
02476   for (SmallPtrSet<Instruction *, 2>::const_iterator
02477        AI = RetainsToMove.Calls.begin(),
02478        AE = RetainsToMove.Calls.end(); AI != AE; ++AI) {
02479     Instruction *OrigRetain = *AI;
02480     Retains.blot(OrigRetain);
02481     DeadInsts.push_back(OrigRetain);
02482     DEBUG(dbgs() << "Deleting retain: " << *OrigRetain << "\n");
02483   }
02484   for (SmallPtrSet<Instruction *, 2>::const_iterator
02485        AI = ReleasesToMove.Calls.begin(),
02486        AE = ReleasesToMove.Calls.end(); AI != AE; ++AI) {
02487     Instruction *OrigRelease = *AI;
02488     Releases.erase(OrigRelease);
02489     DeadInsts.push_back(OrigRelease);
02490     DEBUG(dbgs() << "Deleting release: " << *OrigRelease << "\n");
02491   }
02492 
02493 }
02494 
02495 bool
02496 ObjCARCOpt::ConnectTDBUTraversals(DenseMap<const BasicBlock *, BBState>
02497                                     &BBStates,
02498                                   MapVector<Value *, RRInfo> &Retains,
02499                                   DenseMap<Value *, RRInfo> &Releases,
02500                                   Module *M,
02501                                   SmallVector<Instruction *, 4> &NewRetains,
02502                                   SmallVector<Instruction *, 4> &NewReleases,
02503                                   SmallVector<Instruction *, 8> &DeadInsts,
02504                                   RRInfo &RetainsToMove,
02505                                   RRInfo &ReleasesToMove,
02506                                   Value *Arg,
02507                                   bool KnownSafe,
02508                                   bool &AnyPairsCompletelyEliminated) {
02509   // If a pair happens in a region where it is known that the reference count
02510   // is already incremented, we can similarly ignore possible decrements unless
02511   // we are dealing with a retainable object with multiple provenance sources.
02512   bool KnownSafeTD = true, KnownSafeBU = true;
02513   bool MultipleOwners = false;
02514   bool CFGHazardAfflicted = false;
02515 
02516   // Connect the dots between the top-down-collected RetainsToMove and
02517   // bottom-up-collected ReleasesToMove to form sets of related calls.
02518   // This is an iterative process so that we connect multiple releases
02519   // to multiple retains if needed.
02520   unsigned OldDelta = 0;
02521   unsigned NewDelta = 0;
02522   unsigned OldCount = 0;
02523   unsigned NewCount = 0;
02524   bool FirstRelease = true;
02525   for (;;) {
02526     for (SmallVectorImpl<Instruction *>::const_iterator
02527            NI = NewRetains.begin(), NE = NewRetains.end(); NI != NE; ++NI) {
02528       Instruction *NewRetain = *NI;
02529       MapVector<Value *, RRInfo>::const_iterator It = Retains.find(NewRetain);
02530       assert(It != Retains.end());
02531       const RRInfo &NewRetainRRI = It->second;
02532       KnownSafeTD &= NewRetainRRI.KnownSafe;
02533       MultipleOwners =
02534         MultipleOwners || MultiOwnersSet.count(GetObjCArg(NewRetain));
02535       for (SmallPtrSet<Instruction *, 2>::const_iterator
02536              LI = NewRetainRRI.Calls.begin(),
02537              LE = NewRetainRRI.Calls.end(); LI != LE; ++LI) {
02538         Instruction *NewRetainRelease = *LI;
02539         DenseMap<Value *, RRInfo>::const_iterator Jt =
02540           Releases.find(NewRetainRelease);
02541         if (Jt == Releases.end())
02542           return false;
02543         const RRInfo &NewRetainReleaseRRI = Jt->second;
02544         assert(NewRetainReleaseRRI.Calls.count(NewRetain));
02545         if (ReleasesToMove.Calls.insert(NewRetainRelease)) {
02546           OldDelta -=
02547             BBStates[NewRetainRelease->getParent()].GetAllPathCount();
02548 
02549           // Merge the ReleaseMetadata and IsTailCallRelease values.
02550           if (FirstRelease) {
02551             ReleasesToMove.ReleaseMetadata =
02552               NewRetainReleaseRRI.ReleaseMetadata;
02553             ReleasesToMove.IsTailCallRelease =
02554               NewRetainReleaseRRI.IsTailCallRelease;
02555             FirstRelease = false;
02556           } else {
02557             if (ReleasesToMove.ReleaseMetadata !=
02558                 NewRetainReleaseRRI.ReleaseMetadata)
02559               ReleasesToMove.ReleaseMetadata = 0;
02560             if (ReleasesToMove.IsTailCallRelease !=
02561                 NewRetainReleaseRRI.IsTailCallRelease)
02562               ReleasesToMove.IsTailCallRelease = false;
02563           }
02564 
02565           // Collect the optimal insertion points.
02566           if (!KnownSafe)
02567             for (SmallPtrSet<Instruction *, 2>::const_iterator
02568                    RI = NewRetainReleaseRRI.ReverseInsertPts.begin(),
02569                    RE = NewRetainReleaseRRI.ReverseInsertPts.end();
02570                  RI != RE; ++RI) {
02571               Instruction *RIP = *RI;
02572               if (ReleasesToMove.ReverseInsertPts.insert(RIP))
02573                 NewDelta -= BBStates[RIP->getParent()].GetAllPathCount();
02574             }
02575           NewReleases.push_back(NewRetainRelease);
02576         }
02577       }
02578     }
02579     NewRetains.clear();
02580     if (NewReleases.empty()) break;
02581 
02582     // Back the other way.
02583     for (SmallVectorImpl<Instruction *>::const_iterator
02584            NI = NewReleases.begin(), NE = NewReleases.end(); NI != NE; ++NI) {
02585       Instruction *NewRelease = *NI;
02586       DenseMap<Value *, RRInfo>::const_iterator It =
02587         Releases.find(NewRelease);
02588       assert(It != Releases.end());
02589       const RRInfo &NewReleaseRRI = It->second;
02590       KnownSafeBU &= NewReleaseRRI.KnownSafe;
02591       CFGHazardAfflicted |= NewReleaseRRI.CFGHazardAfflicted;
02592       for (SmallPtrSet<Instruction *, 2>::const_iterator
02593              LI = NewReleaseRRI.Calls.begin(),
02594              LE = NewReleaseRRI.Calls.end(); LI != LE; ++LI) {
02595         Instruction *NewReleaseRetain = *LI;
02596         MapVector<Value *, RRInfo>::const_iterator Jt =
02597           Retains.find(NewReleaseRetain);
02598         if (Jt == Retains.end())
02599           return false;
02600         const RRInfo &NewReleaseRetainRRI = Jt->second;
02601         assert(NewReleaseRetainRRI.Calls.count(NewRelease));
02602         if (RetainsToMove.Calls.insert(NewReleaseRetain)) {
02603           unsigned PathCount =
02604             BBStates[NewReleaseRetain->getParent()].GetAllPathCount();
02605           OldDelta += PathCount;
02606           OldCount += PathCount;
02607 
02608           // Collect the optimal insertion points.
02609           if (!KnownSafe)
02610             for (SmallPtrSet<Instruction *, 2>::const_iterator
02611                    RI = NewReleaseRetainRRI.ReverseInsertPts.begin(),
02612                    RE = NewReleaseRetainRRI.ReverseInsertPts.end();
02613                  RI != RE; ++RI) {
02614               Instruction *RIP = *RI;
02615               if (RetainsToMove.ReverseInsertPts.insert(RIP)) {
02616                 PathCount = BBStates[RIP->getParent()].GetAllPathCount();
02617                 NewDelta += PathCount;
02618                 NewCount += PathCount;
02619               }
02620             }
02621           NewRetains.push_back(NewReleaseRetain);
02622         }
02623       }
02624     }
02625     NewReleases.clear();
02626     if (NewRetains.empty()) break;
02627   }
02628 
02629   // If the pointer is known incremented in 1 direction and we do not have
02630   // MultipleOwners, we can safely remove the retain/releases. Otherwise we need
02631   // to be known safe in both directions.
02632   bool UnconditionallySafe = (KnownSafeTD && KnownSafeBU) ||
02633     ((KnownSafeTD || KnownSafeBU) && !MultipleOwners);
02634   if (UnconditionallySafe) {
02635     RetainsToMove.ReverseInsertPts.clear();
02636     ReleasesToMove.ReverseInsertPts.clear();
02637     NewCount = 0;
02638   } else {
02639     // Determine whether the new insertion points we computed preserve the
02640     // balance of retain and release calls through the program.
02641     // TODO: If the fully aggressive solution isn't valid, try to find a
02642     // less aggressive solution which is.
02643     if (NewDelta != 0)
02644       return false;
02645 
02646     // At this point, we are not going to remove any RR pairs, but we still are
02647     // able to move RR pairs. If one of our pointers is afflicted with
02648     // CFGHazards, we cannot perform such code motion so exit early.
02649     const bool WillPerformCodeMotion = RetainsToMove.ReverseInsertPts.size() ||
02650       ReleasesToMove.ReverseInsertPts.size();
02651     if (CFGHazardAfflicted && WillPerformCodeMotion)
02652       return false;
02653   }
02654 
02655   // Determine whether the original call points are balanced in the retain and
02656   // release calls through the program. If not, conservatively don't touch
02657   // them.
02658   // TODO: It's theoretically possible to do code motion in this case, as
02659   // long as the existing imbalances are maintained.
02660   if (OldDelta != 0)
02661     return false;
02662 
02663 #ifdef ARC_ANNOTATIONS
02664   // Do not move calls if ARC annotations are requested.
02665   if (EnableARCAnnotations)
02666     return false;
02667 #endif // ARC_ANNOTATIONS
02668 
02669   Changed = true;
02670   assert(OldCount != 0 && "Unreachable code?");
02671   NumRRs += OldCount - NewCount;
02672   // Set to true if we completely removed any RR pairs.
02673   AnyPairsCompletelyEliminated = NewCount == 0;
02674 
02675   // We can move calls!
02676   return true;
02677 }
02678 
02679 /// Identify pairings between the retains and releases, and delete and/or move
02680 /// them.
02681 bool
02682 ObjCARCOpt::PerformCodePlacement(DenseMap<const BasicBlock *, BBState>
02683                                    &BBStates,
02684                                  MapVector<Value *, RRInfo> &Retains,
02685                                  DenseMap<Value *, RRInfo> &Releases,
02686                                  Module *M) {
02687   DEBUG(dbgs() << "\n== ObjCARCOpt::PerformCodePlacement ==\n");
02688 
02689   bool AnyPairsCompletelyEliminated = false;
02690   RRInfo RetainsToMove;
02691   RRInfo ReleasesToMove;
02692   SmallVector<Instruction *, 4> NewRetains;
02693   SmallVector<Instruction *, 4> NewReleases;
02694   SmallVector<Instruction *, 8> DeadInsts;
02695 
02696   // Visit each retain.
02697   for (MapVector<Value *, RRInfo>::const_iterator I = Retains.begin(),
02698        E = Retains.end(); I != E; ++I) {
02699     Value *V = I->first;
02700     if (!V) continue; // blotted
02701 
02702     Instruction *Retain = cast<Instruction>(V);
02703 
02704     DEBUG(dbgs() << "Visiting: " << *Retain << "\n");
02705 
02706     Value *Arg = GetObjCArg(Retain);
02707 
02708     // If the object being released is in static or stack storage, we know it's
02709     // not being managed by ObjC reference counting, so we can delete pairs
02710     // regardless of what possible decrements or uses lie between them.
02711     bool KnownSafe = isa<Constant>(Arg) || isa<AllocaInst>(Arg);
02712 
02713     // A constant pointer can't be pointing to an object on the heap. It may
02714     // be reference-counted, but it won't be deleted.
02715     if (const LoadInst *LI = dyn_cast<LoadInst>(Arg))
02716       if (const GlobalVariable *GV =
02717             dyn_cast<GlobalVariable>(
02718               StripPointerCastsAndObjCCalls(LI->getPointerOperand())))
02719         if (GV->isConstant())
02720           KnownSafe = true;
02721 
02722     // Connect the dots between the top-down-collected RetainsToMove and
02723     // bottom-up-collected ReleasesToMove to form sets of related calls.
02724     NewRetains.push_back(Retain);
02725     bool PerformMoveCalls =
02726       ConnectTDBUTraversals(BBStates, Retains, Releases, M, NewRetains,
02727                             NewReleases, DeadInsts, RetainsToMove,
02728                             ReleasesToMove, Arg, KnownSafe,
02729                             AnyPairsCompletelyEliminated);
02730 
02731     if (PerformMoveCalls) {
02732       // Ok, everything checks out and we're all set. Let's move/delete some
02733       // code!
02734       MoveCalls(Arg, RetainsToMove, ReleasesToMove,
02735                 Retains, Releases, DeadInsts, M);
02736     }
02737 
02738     // Clean up state for next retain.
02739     NewReleases.clear();
02740     NewRetains.clear();
02741     RetainsToMove.clear();
02742     ReleasesToMove.clear();
02743   }
02744 
02745   // Now that we're done moving everything, we can delete the newly dead
02746   // instructions, as we no longer need them as insert points.
02747   while (!DeadInsts.empty())
02748     EraseInstruction(DeadInsts.pop_back_val());
02749 
02750   return AnyPairsCompletelyEliminated;
02751 }
02752 
02753 /// Weak pointer optimizations.
02754 void ObjCARCOpt::OptimizeWeakCalls(Function &F) {
02755   DEBUG(dbgs() << "\n== ObjCARCOpt::OptimizeWeakCalls ==\n");
02756 
02757   // First, do memdep-style RLE and S2L optimizations. We can't use memdep
02758   // itself because it uses AliasAnalysis and we need to do provenance
02759   // queries instead.
02760   for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
02761     Instruction *Inst = &*I++;
02762 
02763     DEBUG(dbgs() << "Visiting: " << *Inst << "\n");
02764 
02765     InstructionClass Class = GetBasicInstructionClass(Inst);
02766     if (Class != IC_LoadWeak && Class != IC_LoadWeakRetained)
02767       continue;
02768 
02769     // Delete objc_loadWeak calls with no users.
02770     if (Class == IC_LoadWeak && Inst->use_empty()) {
02771       Inst->eraseFromParent();
02772       continue;
02773     }
02774 
02775     // TODO: For now, just look for an earlier available version of this value
02776     // within the same block. Theoretically, we could do memdep-style non-local
02777     // analysis too, but that would want caching. A better approach would be to
02778     // use the technique that EarlyCSE uses.
02779     inst_iterator Current = llvm::prior(I);
02780     BasicBlock *CurrentBB = Current.getBasicBlockIterator();
02781     for (BasicBlock::iterator B = CurrentBB->begin(),
02782                               J = Current.getInstructionIterator();
02783          J != B; --J) {
02784       Instruction *EarlierInst = &*llvm::prior(J);
02785       InstructionClass EarlierClass = GetInstructionClass(EarlierInst);
02786       switch (EarlierClass) {
02787       case IC_LoadWeak:
02788       case IC_LoadWeakRetained: {
02789         // If this is loading from the same pointer, replace this load's value
02790         // with that one.
02791         CallInst *Call = cast<CallInst>(Inst);
02792         CallInst *EarlierCall = cast<CallInst>(EarlierInst);
02793         Value *Arg = Call->getArgOperand(0);
02794         Value *EarlierArg = EarlierCall->getArgOperand(0);
02795         switch (PA.getAA()->alias(Arg, EarlierArg)) {
02796         case AliasAnalysis::MustAlias:
02797           Changed = true;
02798           // If the load has a builtin retain, insert a plain retain for it.
02799           if (Class == IC_LoadWeakRetained) {
02800             CallInst *CI =
02801               CallInst::Create(getRetainCallee(F.getParent()), EarlierCall,
02802                                "", Call);
02803             CI->setTailCall();
02804           }
02805           // Zap the fully redundant load.
02806           Call->replaceAllUsesWith(EarlierCall);
02807           Call->eraseFromParent();
02808           goto clobbered;
02809         case AliasAnalysis::MayAlias:
02810         case AliasAnalysis::PartialAlias:
02811           goto clobbered;
02812         case AliasAnalysis::NoAlias:
02813           break;
02814         }
02815         break;
02816       }
02817       case IC_StoreWeak:
02818       case IC_InitWeak: {
02819         // If this is storing to the same pointer and has the same size etc.
02820         // replace this load's value with the stored value.
02821         CallInst *Call = cast<CallInst>(Inst);
02822         CallInst *EarlierCall = cast<CallInst>(EarlierInst);
02823         Value *Arg = Call->getArgOperand(0);
02824         Value *EarlierArg = EarlierCall->getArgOperand(0);
02825         switch (PA.getAA()->alias(Arg, EarlierArg)) {
02826         case AliasAnalysis::MustAlias:
02827           Changed = true;
02828           // If the load has a builtin retain, insert a plain retain for it.
02829           if (Class == IC_LoadWeakRetained) {
02830             CallInst *CI =
02831               CallInst::Create(getRetainCallee(F.getParent()), EarlierCall,
02832                                "", Call);
02833             CI->setTailCall();
02834           }
02835           // Zap the fully redundant load.
02836           Call->replaceAllUsesWith(EarlierCall->getArgOperand(1));
02837           Call->eraseFromParent();
02838           goto clobbered;
02839         case AliasAnalysis::MayAlias:
02840         case AliasAnalysis::PartialAlias:
02841           goto clobbered;
02842         case AliasAnalysis::NoAlias:
02843           break;
02844         }
02845         break;
02846       }
02847       case IC_MoveWeak:
02848       case IC_CopyWeak:
02849         // TOOD: Grab the copied value.
02850         goto clobbered;
02851       case IC_AutoreleasepoolPush:
02852       case IC_None:
02853       case IC_IntrinsicUser:
02854       case IC_User:
02855         // Weak pointers are only modified through the weak entry points
02856         // (and arbitrary calls, which could call the weak entry points).
02857         break;
02858       default:
02859         // Anything else could modify the weak pointer.
02860         goto clobbered;
02861       }
02862     }
02863   clobbered:;
02864   }
02865 
02866   // Then, for each destroyWeak with an alloca operand, check to see if
02867   // the alloca and all its users can be zapped.
02868   for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
02869     Instruction *Inst = &*I++;
02870     InstructionClass Class = GetBasicInstructionClass(Inst);
02871     if (Class != IC_DestroyWeak)
02872       continue;
02873 
02874     CallInst *Call = cast<CallInst>(Inst);
02875     Value *Arg = Call->getArgOperand(0);
02876     if (AllocaInst *Alloca = dyn_cast<AllocaInst>(Arg)) {
02877       for (Value::use_iterator UI = Alloca->use_begin(),
02878            UE = Alloca->use_end(); UI != UE; ++UI) {
02879         const Instruction *UserInst = cast<Instruction>(*UI);
02880         switch (GetBasicInstructionClass(UserInst)) {
02881         case IC_InitWeak:
02882         case IC_StoreWeak:
02883         case IC_DestroyWeak:
02884           continue;
02885         default:
02886           goto done;
02887         }
02888       }
02889       Changed = true;
02890       for (Value::use_iterator UI = Alloca->use_begin(),
02891            UE = Alloca->use_end(); UI != UE; ) {
02892         CallInst *UserInst = cast<CallInst>(*UI++);
02893         switch (GetBasicInstructionClass(UserInst)) {
02894         case IC_InitWeak:
02895         case IC_StoreWeak:
02896           // These functions return their second argument.
02897           UserInst->replaceAllUsesWith(UserInst->getArgOperand(1));
02898           break;
02899         case IC_DestroyWeak:
02900           // No return value.
02901           break;
02902         default:
02903           llvm_unreachable("alloca really is used!");
02904         }
02905         UserInst->eraseFromParent();
02906       }
02907       Alloca->eraseFromParent();
02908     done:;
02909     }
02910   }
02911 }
02912 
02913 /// Identify program paths which execute sequences of retains and releases which
02914 /// can be eliminated.
02915 bool ObjCARCOpt::OptimizeSequences(Function &F) {
02916   // Releases, Retains - These are used to store the results of the main flow
02917   // analysis. These use Value* as the key instead of Instruction* so that the
02918   // map stays valid when we get around to rewriting code and calls get
02919   // replaced by arguments.
02920   DenseMap<Value *, RRInfo> Releases;
02921   MapVector<Value *, RRInfo> Retains;
02922 
02923   // This is used during the traversal of the function to track the
02924   // states for each identified object at each block.
02925   DenseMap<const BasicBlock *, BBState> BBStates;
02926 
02927   // Analyze the CFG of the function, and all instructions.
02928   bool NestingDetected = Visit(F, BBStates, Retains, Releases);
02929 
02930   // Transform.
02931   bool AnyPairsCompletelyEliminated = PerformCodePlacement(BBStates, Retains,
02932                                                            Releases,
02933                                                            F.getParent());
02934 
02935   // Cleanup.
02936   MultiOwnersSet.clear();
02937 
02938   return AnyPairsCompletelyEliminated && NestingDetected;
02939 }
02940 
02941 /// Check if there is a dependent call earlier that does not have anything in
02942 /// between the Retain and the call that can affect the reference count of their
02943 /// shared pointer argument. Note that Retain need not be in BB.
02944 static bool
02945 HasSafePathToPredecessorCall(const Value *Arg, Instruction *Retain,
02946                              SmallPtrSet<Instruction *, 4> &DepInsts,
02947                              SmallPtrSet<const BasicBlock *, 4> &Visited,
02948                              ProvenanceAnalysis &PA) {
02949   FindDependencies(CanChangeRetainCount, Arg, Retain->getParent(), Retain,
02950                    DepInsts, Visited, PA);
02951   if (DepInsts.size() != 1)
02952     return false;
02953 
02954   CallInst *Call =
02955     dyn_cast_or_null<CallInst>(*DepInsts.begin());
02956 
02957   // Check that the pointer is the return value of the call.
02958   if (!Call || Arg != Call)
02959     return false;
02960 
02961   // Check that the call is a regular call.
02962   InstructionClass Class = GetBasicInstructionClass(Call);
02963   if (Class != IC_CallOrUser && Class != IC_Call)
02964     return false;
02965 
02966   return true;
02967 }
02968 
02969 /// Find a dependent retain that precedes the given autorelease for which there
02970 /// is nothing in between the two instructions that can affect the ref count of
02971 /// Arg.
02972 static CallInst *
02973 FindPredecessorRetainWithSafePath(const Value *Arg, BasicBlock *BB,
02974                                   Instruction *Autorelease,
02975                                   SmallPtrSet<Instruction *, 4> &DepInsts,
02976                                   SmallPtrSet<const BasicBlock *, 4> &Visited,
02977                                   ProvenanceAnalysis &PA) {
02978   FindDependencies(CanChangeRetainCount, Arg,
02979                    BB, Autorelease, DepInsts, Visited, PA);
02980   if (DepInsts.size() != 1)
02981     return 0;
02982 
02983   CallInst *Retain =
02984     dyn_cast_or_null<CallInst>(*DepInsts.begin());
02985 
02986   // Check that we found a retain with the same argument.
02987   if (!Retain ||
02988       !IsRetain(GetBasicInstructionClass(Retain)) ||
02989       GetObjCArg(Retain) != Arg) {
02990     return 0;
02991   }
02992 
02993   return Retain;
02994 }
02995 
02996 /// Look for an ``autorelease'' instruction dependent on Arg such that there are
02997 /// no instructions dependent on Arg that need a positive ref count in between
02998 /// the autorelease and the ret.
02999 static CallInst *
03000 FindPredecessorAutoreleaseWithSafePath(const Value *Arg, BasicBlock *BB,
03001                                        ReturnInst *Ret,
03002                                        SmallPtrSet<Instruction *, 4> &DepInsts,
03003                                        SmallPtrSet<const BasicBlock *, 4> &V,
03004                                        ProvenanceAnalysis &PA) {
03005   FindDependencies(NeedsPositiveRetainCount, Arg,
03006                    BB, Ret, DepInsts, V, PA);
03007   if (DepInsts.size() != 1)
03008     return 0;
03009 
03010   CallInst *Autorelease =
03011     dyn_cast_or_null<CallInst>(*DepInsts.begin());
03012   if (!Autorelease)
03013     return 0;
03014   InstructionClass AutoreleaseClass = GetBasicInstructionClass(Autorelease);
03015   if (!IsAutorelease(AutoreleaseClass))
03016     return 0;
03017   if (GetObjCArg(Autorelease) != Arg)
03018     return 0;
03019 
03020   return Autorelease;
03021 }
03022 
03023 /// Look for this pattern:
03024 /// \code
03025 ///    %call = call i8* @something(...)
03026 ///    %2 = call i8* @objc_retain(i8* %call)
03027 ///    %3 = call i8* @objc_autorelease(i8* %2)
03028 ///    ret i8* %3
03029 /// \endcode
03030 /// And delete the retain and autorelease.
03031 void ObjCARCOpt::OptimizeReturns(Function &F) {
03032   if (!F.getReturnType()->isPointerTy())
03033     return;
03034 
03035   DEBUG(dbgs() << "\n== ObjCARCOpt::OptimizeReturns ==\n");
03036 
03037   SmallPtrSet<Instruction *, 4> DependingInstructions;
03038   SmallPtrSet<const BasicBlock *, 4> Visited;
03039   for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI) {
03040     BasicBlock *BB = FI;
03041     ReturnInst *Ret = dyn_cast<ReturnInst>(&BB->back());
03042 
03043     DEBUG(dbgs() << "Visiting: " << *Ret << "\n");
03044 
03045     if (!Ret)
03046       continue;
03047 
03048     const Value *Arg = StripPointerCastsAndObjCCalls(Ret->getOperand(0));
03049 
03050     // Look for an ``autorelease'' instruction that is a predecessor of Ret and
03051     // dependent on Arg such that there are no instructions dependent on Arg
03052     // that need a positive ref count in between the autorelease and Ret.
03053     CallInst *Autorelease =
03054       FindPredecessorAutoreleaseWithSafePath(Arg, BB, Ret,
03055                                              DependingInstructions, Visited,
03056                                              PA);
03057     DependingInstructions.clear();
03058     Visited.clear();
03059 
03060     if (!Autorelease)
03061       continue;
03062 
03063     CallInst *Retain =
03064       FindPredecessorRetainWithSafePath(Arg, BB, Autorelease,
03065                                         DependingInstructions, Visited, PA);
03066     DependingInstructions.clear();
03067     Visited.clear();
03068 
03069     if (!Retain)
03070       continue;
03071 
03072     // Check that there is nothing that can affect the reference count
03073     // between the retain and the call.  Note that Retain need not be in BB.
03074     bool HasSafePathToCall = HasSafePathToPredecessorCall(Arg, Retain,
03075                                                           DependingInstructions,
03076                                                           Visited, PA);
03077     DependingInstructions.clear();
03078     Visited.clear();
03079 
03080     if (!HasSafePathToCall)
03081       continue;
03082 
03083     // If so, we can zap the retain and autorelease.
03084     Changed = true;
03085     ++NumRets;
03086     DEBUG(dbgs() << "Erasing: " << *Retain << "\nErasing: "
03087           << *Autorelease << "\n");
03088     EraseInstruction(Retain);
03089     EraseInstruction(Autorelease);
03090   }
03091 }
03092 
03093 #ifndef NDEBUG
03094 void
03095 ObjCARCOpt::GatherStatistics(Function &F, bool AfterOptimization) {
03096   llvm::Statistic &NumRetains =
03097     AfterOptimization? NumRetainsAfterOpt : NumRetainsBeforeOpt;
03098   llvm::Statistic &NumReleases =
03099     AfterOptimization? NumReleasesAfterOpt : NumReleasesBeforeOpt;
03100 
03101   for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
03102     Instruction *Inst = &*I++;
03103     switch (GetBasicInstructionClass(Inst)) {
03104     default:
03105       break;
03106     case IC_Retain:
03107       ++NumRetains;
03108       break;
03109     case IC_Release:
03110       ++NumReleases;
03111       break;
03112     }
03113   }
03114 }
03115 #endif
03116 
03117 bool ObjCARCOpt::doInitialization(Module &M) {
03118   if (!EnableARCOpts)
03119     return false;
03120 
03121   // If nothing in the Module uses ARC, don't do anything.
03122   Run = ModuleHasARC(M);
03123   if (!Run)
03124     return false;
03125 
03126   // Identify the imprecise release metadata kind.
03127   ImpreciseReleaseMDKind =
03128     M.getContext().getMDKindID("clang.imprecise_release");
03129   CopyOnEscapeMDKind =
03130     M.getContext().getMDKindID("clang.arc.copy_on_escape");
03131   NoObjCARCExceptionsMDKind =
03132     M.getContext().getMDKindID("clang.arc.no_objc_arc_exceptions");
03133 #ifdef ARC_ANNOTATIONS
03134   ARCAnnotationBottomUpMDKind =
03135     M.getContext().getMDKindID("llvm.arc.annotation.bottomup");
03136   ARCAnnotationTopDownMDKind =
03137     M.getContext().getMDKindID("llvm.arc.annotation.topdown");
03138   ARCAnnotationProvenanceSourceMDKind =
03139     M.getContext().getMDKindID("llvm.arc.annotation.provenancesource");
03140 #endif // ARC_ANNOTATIONS
03141 
03142   // Intuitively, objc_retain and others are nocapture, however in practice
03143   // they are not, because they return their argument value. And objc_release
03144   // calls finalizers which can have arbitrary side effects.
03145 
03146   // These are initialized lazily.
03147   AutoreleaseRVCallee = 0;
03148   ReleaseCallee = 0;
03149   RetainCallee = 0;
03150   RetainBlockCallee = 0;
03151   AutoreleaseCallee = 0;
03152 
03153   return false;
03154 }
03155 
03156 bool ObjCARCOpt::runOnFunction(Function &F) {
03157   if (!EnableARCOpts)
03158     return false;
03159 
03160   // If nothing in the Module uses ARC, don't do anything.
03161   if (!Run)
03162     return false;
03163 
03164   Changed = false;
03165 
03166   DEBUG(dbgs() << "<<< ObjCARCOpt: Visiting Function: " << F.getName() << " >>>"
03167         "\n");
03168 
03169   PA.setAA(&getAnalysis<AliasAnalysis>());
03170 
03171 #ifndef NDEBUG
03172   if (AreStatisticsEnabled()) {
03173     GatherStatistics(F, false);
03174   }
03175 #endif
03176 
03177   // This pass performs several distinct transformations. As a compile-time aid
03178   // when compiling code that isn't ObjC, skip these if the relevant ObjC
03179   // library functions aren't declared.
03180 
03181   // Preliminary optimizations. This also computes UsedInThisFunction.
03182   OptimizeIndividualCalls(F);
03183 
03184   // Optimizations for weak pointers.
03185   if (UsedInThisFunction & ((1 << IC_LoadWeak) |
03186                             (1 << IC_LoadWeakRetained) |
03187                             (1 << IC_StoreWeak) |
03188                             (1 << IC_InitWeak) |
03189                             (1 << IC_CopyWeak) |
03190                             (1 << IC_MoveWeak) |
03191                             (1 << IC_DestroyWeak)))
03192     OptimizeWeakCalls(F);
03193 
03194   // Optimizations for retain+release pairs.
03195   if (UsedInThisFunction & ((1 << IC_Retain) |
03196                             (1 << IC_RetainRV) |
03197                             (1 << IC_RetainBlock)))
03198     if (UsedInThisFunction & (1 << IC_Release))
03199       // Run OptimizeSequences until it either stops making changes or
03200       // no retain+release pair nesting is detected.
03201       while (OptimizeSequences(F)) {}
03202 
03203   // Optimizations if objc_autorelease is used.
03204   if (UsedInThisFunction & ((1 << IC_Autorelease) |
03205                             (1 << IC_AutoreleaseRV)))
03206     OptimizeReturns(F);
03207 
03208   // Gather statistics after optimization.
03209 #ifndef NDEBUG
03210   if (AreStatisticsEnabled()) {
03211     GatherStatistics(F, true);
03212   }
03213 #endif
03214 
03215   DEBUG(dbgs() << "\n");
03216 
03217   return Changed;
03218 }
03219 
03220 void ObjCARCOpt::releaseMemory() {
03221   PA.clear();
03222 }
03223 
03224 /// @}
03225 ///