1 //===- ObjCARCOpts.cpp - ObjC ARC Optimization ----------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 /// This file defines ObjC ARC optimizations. ARC stands for Automatic
11 /// Reference Counting and is a system for managing reference counts for objects
14 /// The optimizations performed include elimination of redundant, partially
15 /// redundant, and inconsequential reference count operations, elimination of
16 /// redundant weak pointer operations, and numerous minor simplifications.
18 /// WARNING: This file knows about certain library functions. It recognizes them
19 /// by name, and hardwires knowledge of their semantics.
21 /// WARNING: This file knows about how certain Objective-C library functions are
22 /// used. Naive LLVM IR transformations which would otherwise be
23 /// behavior-preserving may break these assumptions.
25 //===----------------------------------------------------------------------===//
27 #define DEBUG_TYPE "objc-arc-opts"
29 #include "ARCRuntimeEntryPoints.h"
30 #include "DependencyAnalysis.h"
31 #include "ObjCARCAliasAnalysis.h"
32 #include "ProvenanceAnalysis.h"
33 #include "llvm/ADT/DenseMap.h"
34 #include "llvm/ADT/DenseSet.h"
35 #include "llvm/ADT/STLExtras.h"
36 #include "llvm/ADT/SmallPtrSet.h"
37 #include "llvm/ADT/Statistic.h"
38 #include "llvm/IR/IRBuilder.h"
39 #include "llvm/IR/LLVMContext.h"
40 #include "llvm/Support/CFG.h"
41 #include "llvm/Support/Debug.h"
42 #include "llvm/Support/raw_ostream.h"
45 using namespace llvm::objcarc;
47 /// \defgroup MiscUtils Miscellaneous utilities that are not ARC specific.
51 /// \brief An associative container with fast insertion-order (deterministic)
52 /// iteration over its elements. Plus the special blot operation.
53 template<class KeyT, class ValueT>
55 /// Map keys to indices in Vector.
56 typedef DenseMap<KeyT, size_t> MapTy;
59 typedef std::vector<std::pair<KeyT, ValueT> > VectorTy;
64 typedef typename VectorTy::iterator iterator;
65 typedef typename VectorTy::const_iterator const_iterator;
66 iterator begin() { return Vector.begin(); }
67 iterator end() { return Vector.end(); }
68 const_iterator begin() const { return Vector.begin(); }
69 const_iterator end() const { return Vector.end(); }
73 assert(Vector.size() >= Map.size()); // May differ due to blotting.
74 for (typename MapTy::const_iterator I = Map.begin(), E = Map.end();
76 assert(I->second < Vector.size());
77 assert(Vector[I->second].first == I->first);
79 for (typename VectorTy::const_iterator I = Vector.begin(),
80 E = Vector.end(); I != E; ++I)
82 (Map.count(I->first) &&
83 Map[I->first] == size_t(I - Vector.begin())));
87 ValueT &operator[](const KeyT &Arg) {
88 std::pair<typename MapTy::iterator, bool> Pair =
89 Map.insert(std::make_pair(Arg, size_t(0)));
91 size_t Num = Vector.size();
92 Pair.first->second = Num;
93 Vector.push_back(std::make_pair(Arg, ValueT()));
94 return Vector[Num].second;
96 return Vector[Pair.first->second].second;
99 std::pair<iterator, bool>
100 insert(const std::pair<KeyT, ValueT> &InsertPair) {
101 std::pair<typename MapTy::iterator, bool> Pair =
102 Map.insert(std::make_pair(InsertPair.first, size_t(0)));
104 size_t Num = Vector.size();
105 Pair.first->second = Num;
106 Vector.push_back(InsertPair);
107 return std::make_pair(Vector.begin() + Num, true);
109 return std::make_pair(Vector.begin() + Pair.first->second, false);
112 iterator find(const KeyT &Key) {
113 typename MapTy::iterator It = Map.find(Key);
114 if (It == Map.end()) return Vector.end();
115 return Vector.begin() + It->second;
118 const_iterator find(const KeyT &Key) const {
119 typename MapTy::const_iterator It = Map.find(Key);
120 if (It == Map.end()) return Vector.end();
121 return Vector.begin() + It->second;
124 /// This is similar to erase, but instead of removing the element from the
125 /// vector, it just zeros out the key in the vector. This leaves iterators
126 /// intact, but clients must be prepared for zeroed-out keys when iterating.
127 void blot(const KeyT &Key) {
128 typename MapTy::iterator It = Map.find(Key);
129 if (It == Map.end()) return;
130 Vector[It->second].first = KeyT();
143 /// \defgroup ARCUtilities Utility declarations/definitions specific to ARC.
146 /// \brief This is similar to StripPointerCastsAndObjCCalls but it stops as soon
147 /// as it finds a value with multiple uses.
148 static const Value *FindSingleUseIdentifiedObject(const Value *Arg) {
149 if (Arg->hasOneUse()) {
150 if (const BitCastInst *BC = dyn_cast<BitCastInst>(Arg))
151 return FindSingleUseIdentifiedObject(BC->getOperand(0));
152 if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Arg))
153 if (GEP->hasAllZeroIndices())
154 return FindSingleUseIdentifiedObject(GEP->getPointerOperand());
155 if (IsForwarding(GetBasicInstructionClass(Arg)))
156 return FindSingleUseIdentifiedObject(
157 cast<CallInst>(Arg)->getArgOperand(0));
158 if (!IsObjCIdentifiedObject(Arg))
163 // If we found an identifiable object but it has multiple uses, but they are
164 // trivial uses, we can still consider this to be a single-use value.
165 if (IsObjCIdentifiedObject(Arg)) {
166 for (Value::const_use_iterator UI = Arg->use_begin(), UE = Arg->use_end();
169 if (!U->use_empty() || StripPointerCastsAndObjCCalls(U) != Arg)
179 /// \brief Test whether the given retainable object pointer escapes.
181 /// This differs from regular escape analysis in that a use as an
182 /// argument to a call is not considered an escape.
184 static bool DoesRetainableObjPtrEscape(const User *Ptr) {
185 DEBUG(dbgs() << "DoesRetainableObjPtrEscape: Target: " << *Ptr << "\n");
187 // Walk the def-use chains.
188 SmallVector<const Value *, 4> Worklist;
189 Worklist.push_back(Ptr);
190 // If Ptr has any operands add them as well.
191 for (User::const_op_iterator I = Ptr->op_begin(), E = Ptr->op_end(); I != E;
193 Worklist.push_back(*I);
196 // Ensure we do not visit any value twice.
197 SmallPtrSet<const Value *, 8> VisitedSet;
200 const Value *V = Worklist.pop_back_val();
202 DEBUG(dbgs() << "Visiting: " << *V << "\n");
204 for (Value::const_use_iterator UI = V->use_begin(), UE = V->use_end();
206 const User *UUser = *UI;
208 DEBUG(dbgs() << "User: " << *UUser << "\n");
210 // Special - Use by a call (callee or argument) is not considered
212 switch (GetBasicInstructionClass(UUser)) {
217 case IC_AutoreleaseRV: {
218 DEBUG(dbgs() << "User copies pointer arguments. Pointer Escapes!\n");
219 // These special functions make copies of their pointer arguments.
222 case IC_IntrinsicUser:
223 // Use by the use intrinsic is not an escape.
227 // Use by an instruction which copies the value is an escape if the
228 // result is an escape.
229 if (isa<BitCastInst>(UUser) || isa<GetElementPtrInst>(UUser) ||
230 isa<PHINode>(UUser) || isa<SelectInst>(UUser)) {
232 if (VisitedSet.insert(UUser)) {
233 DEBUG(dbgs() << "User copies value. Ptr escapes if result escapes."
234 " Adding to list.\n");
235 Worklist.push_back(UUser);
237 DEBUG(dbgs() << "Already visited node.\n");
241 // Use by a load is not an escape.
242 if (isa<LoadInst>(UUser))
244 // Use by a store is not an escape if the use is the address.
245 if (const StoreInst *SI = dyn_cast<StoreInst>(UUser))
246 if (V != SI->getValueOperand())
250 // Regular calls and other stuff are not considered escapes.
253 // Otherwise, conservatively assume an escape.
254 DEBUG(dbgs() << "Assuming ptr escapes.\n");
257 } while (!Worklist.empty());
260 DEBUG(dbgs() << "Ptr does not escape.\n");
264 /// This is a wrapper around getUnderlyingObjCPtr along the lines of
265 /// GetUnderlyingObjects except that it returns early when it sees the first
267 static inline bool AreAnyUnderlyingObjectsAnAlloca(const Value *V) {
268 SmallPtrSet<const Value *, 4> Visited;
269 SmallVector<const Value *, 4> Worklist;
270 Worklist.push_back(V);
272 const Value *P = Worklist.pop_back_val();
273 P = GetUnderlyingObjCPtr(P);
275 if (isa<AllocaInst>(P))
278 if (!Visited.insert(P))
281 if (const SelectInst *SI = dyn_cast<const SelectInst>(P)) {
282 Worklist.push_back(SI->getTrueValue());
283 Worklist.push_back(SI->getFalseValue());
287 if (const PHINode *PN = dyn_cast<const PHINode>(P)) {
288 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
289 Worklist.push_back(PN->getIncomingValue(i));
292 } while (!Worklist.empty());
300 /// \defgroup ARCOpt ARC Optimization.
303 // TODO: On code like this:
306 // stuff_that_cannot_release()
307 // objc_autorelease(%x)
308 // stuff_that_cannot_release()
310 // stuff_that_cannot_release()
311 // objc_autorelease(%x)
313 // The second retain and autorelease can be deleted.
315 // TODO: It should be possible to delete
316 // objc_autoreleasePoolPush and objc_autoreleasePoolPop
317 // pairs if nothing is actually autoreleased between them. Also, autorelease
318 // calls followed by objc_autoreleasePoolPop calls (perhaps in ObjC++ code
319 // after inlining) can be turned into plain release calls.
321 // TODO: Critical-edge splitting. If the optimial insertion point is
322 // a critical edge, the current algorithm has to fail, because it doesn't
323 // know how to split edges. It should be possible to make the optimizer
324 // think in terms of edges, rather than blocks, and then split critical
327 // TODO: OptimizeSequences could generalized to be Interprocedural.
329 // TODO: Recognize that a bunch of other objc runtime calls have
330 // non-escaping arguments and non-releasing arguments, and may be
331 // non-autoreleasing.
333 // TODO: Sink autorelease calls as far as possible. Unfortunately we
334 // usually can't sink them past other calls, which would be the main
335 // case where it would be useful.
337 // TODO: The pointer returned from objc_loadWeakRetained is retained.
339 // TODO: Delete release+retain pairs (rare).
341 STATISTIC(NumNoops, "Number of no-op objc calls eliminated");
342 STATISTIC(NumPartialNoops, "Number of partially no-op objc calls eliminated");
343 STATISTIC(NumAutoreleases,"Number of autoreleases converted to releases");
344 STATISTIC(NumRets, "Number of return value forwarding "
345 "retain+autoreleases eliminated");
346 STATISTIC(NumRRs, "Number of retain+release paths eliminated");
347 STATISTIC(NumPeeps, "Number of calls peephole-optimized");
349 STATISTIC(NumRetainsBeforeOpt,
350 "Number of retains before optimization");
351 STATISTIC(NumReleasesBeforeOpt,
352 "Number of releases before optimization");
353 STATISTIC(NumRetainsAfterOpt,
354 "Number of retains after optimization");
355 STATISTIC(NumReleasesAfterOpt,
356 "Number of releases after optimization");
362 /// \brief A sequence of states that a pointer may go through in which an
363 /// objc_retain and objc_release are actually needed.
366 S_Retain, ///< objc_retain(x).
367 S_CanRelease, ///< foo(x) -- x could possibly see a ref count decrement.
368 S_Use, ///< any use of x.
369 S_Stop, ///< like S_Release, but code motion is stopped.
370 S_Release, ///< objc_release(x).
371 S_MovableRelease ///< objc_release(x), !clang.imprecise_release.
374 raw_ostream &operator<<(raw_ostream &OS, const Sequence S)
375 LLVM_ATTRIBUTE_UNUSED;
376 raw_ostream &operator<<(raw_ostream &OS, const Sequence S) {
379 return OS << "S_None";
381 return OS << "S_Retain";
383 return OS << "S_CanRelease";
385 return OS << "S_Use";
387 return OS << "S_Release";
388 case S_MovableRelease:
389 return OS << "S_MovableRelease";
391 return OS << "S_Stop";
393 llvm_unreachable("Unknown sequence type.");
397 static Sequence MergeSeqs(Sequence A, Sequence B, bool TopDown) {
401 if (A == S_None || B == S_None)
404 if (A > B) std::swap(A, B);
406 // Choose the side which is further along in the sequence.
407 if ((A == S_Retain || A == S_CanRelease) &&
408 (B == S_CanRelease || B == S_Use))
411 // Choose the side which is further along in the sequence.
412 if ((A == S_Use || A == S_CanRelease) &&
413 (B == S_Use || B == S_Release || B == S_Stop || B == S_MovableRelease))
415 // If both sides are releases, choose the more conservative one.
416 if (A == S_Stop && (B == S_Release || B == S_MovableRelease))
418 if (A == S_Release && B == S_MovableRelease)
426 /// \brief Unidirectional information about either a
427 /// retain-decrement-use-release sequence or release-use-decrement-retain
428 /// reverse sequence.
430 /// After an objc_retain, the reference count of the referenced
431 /// object is known to be positive. Similarly, before an objc_release, the
432 /// reference count of the referenced object is known to be positive. If
433 /// there are retain-release pairs in code regions where the retain count
434 /// is known to be positive, they can be eliminated, regardless of any side
435 /// effects between them.
437 /// Also, a retain+release pair nested within another retain+release
438 /// pair all on the known same pointer value can be eliminated, regardless
439 /// of any intervening side effects.
441 /// KnownSafe is true when either of these conditions is satisfied.
444 /// True of the objc_release calls are all marked with the "tail" keyword.
445 bool IsTailCallRelease;
447 /// If the Calls are objc_release calls and they all have a
448 /// clang.imprecise_release tag, this is the metadata tag.
449 MDNode *ReleaseMetadata;
451 /// For a top-down sequence, the set of objc_retains or
452 /// objc_retainBlocks. For bottom-up, the set of objc_releases.
453 SmallPtrSet<Instruction *, 2> Calls;
455 /// The set of optimal insert positions for moving calls in the opposite
457 SmallPtrSet<Instruction *, 2> ReverseInsertPts;
459 /// If this is true, we cannot perform code motion but can still remove
460 /// retain/release pairs.
461 bool CFGHazardAfflicted;
464 KnownSafe(false), IsTailCallRelease(false), ReleaseMetadata(0),
465 CFGHazardAfflicted(false) {}
469 /// Conservatively merge the two RRInfo. Returns true if a partial merge has
470 /// occured, false otherwise.
471 bool Merge(const RRInfo &Other);
476 void RRInfo::clear() {
478 IsTailCallRelease = false;
481 ReverseInsertPts.clear();
482 CFGHazardAfflicted = false;
485 bool RRInfo::Merge(const RRInfo &Other) {
486 // Conservatively merge the ReleaseMetadata information.
487 if (ReleaseMetadata != Other.ReleaseMetadata)
490 // Conservatively merge the boolean state.
491 KnownSafe &= Other.KnownSafe;
492 IsTailCallRelease &= Other.IsTailCallRelease;
493 CFGHazardAfflicted |= Other.CFGHazardAfflicted;
495 // Merge the call sets.
496 Calls.insert(Other.Calls.begin(), Other.Calls.end());
498 // Merge the insert point sets. If there are any differences,
499 // that makes this a partial merge.
500 bool Partial = ReverseInsertPts.size() != Other.ReverseInsertPts.size();
501 for (SmallPtrSet<Instruction *, 2>::const_iterator
502 I = Other.ReverseInsertPts.begin(),
503 E = Other.ReverseInsertPts.end(); I != E; ++I)
504 Partial |= ReverseInsertPts.insert(*I);
509 /// \brief This class summarizes several per-pointer runtime properties which
510 /// are propogated through the flow graph.
512 /// True if the reference count is known to be incremented.
513 bool KnownPositiveRefCount;
515 /// True if we've seen an opportunity for partial RR elimination, such as
516 /// pushing calls into a CFG triangle or into one side of a CFG diamond.
519 /// The current position in the sequence.
522 /// Unidirectional information about the current sequence.
526 PtrState() : KnownPositiveRefCount(false), Partial(false),
530 bool IsKnownSafe() const {
531 return RRI.KnownSafe;
534 void SetKnownSafe(const bool NewValue) {
535 RRI.KnownSafe = NewValue;
538 bool IsTailCallRelease() const {
539 return RRI.IsTailCallRelease;
542 void SetTailCallRelease(const bool NewValue) {
543 RRI.IsTailCallRelease = NewValue;
546 bool IsTrackingImpreciseReleases() const {
547 return RRI.ReleaseMetadata != 0;
550 const MDNode *GetReleaseMetadata() const {
551 return RRI.ReleaseMetadata;
554 void SetReleaseMetadata(MDNode *NewValue) {
555 RRI.ReleaseMetadata = NewValue;
558 bool IsCFGHazardAfflicted() const {
559 return RRI.CFGHazardAfflicted;
562 void SetCFGHazardAfflicted(const bool NewValue) {
563 RRI.CFGHazardAfflicted = NewValue;
566 void SetKnownPositiveRefCount() {
567 DEBUG(dbgs() << "Setting Known Positive.\n");
568 KnownPositiveRefCount = true;
571 void ClearKnownPositiveRefCount() {
572 DEBUG(dbgs() << "Clearing Known Positive.\n");
573 KnownPositiveRefCount = false;
576 bool HasKnownPositiveRefCount() const {
577 return KnownPositiveRefCount;
580 void SetSeq(Sequence NewSeq) {
581 DEBUG(dbgs() << "Old: " << Seq << "; New: " << NewSeq << "\n");
585 Sequence GetSeq() const {
589 void ClearSequenceProgress() {
590 ResetSequenceProgress(S_None);
593 void ResetSequenceProgress(Sequence NewSeq) {
594 DEBUG(dbgs() << "Resetting sequence progress.\n");
600 void Merge(const PtrState &Other, bool TopDown);
602 void InsertCall(Instruction *I) {
606 void InsertReverseInsertPt(Instruction *I) {
607 RRI.ReverseInsertPts.insert(I);
610 void ClearReverseInsertPts() {
611 RRI.ReverseInsertPts.clear();
614 bool HasReverseInsertPts() const {
615 return !RRI.ReverseInsertPts.empty();
618 const RRInfo &GetRRInfo() const {
625 PtrState::Merge(const PtrState &Other, bool TopDown) {
626 Seq = MergeSeqs(Seq, Other.Seq, TopDown);
627 KnownPositiveRefCount &= Other.KnownPositiveRefCount;
629 // If we're not in a sequence (anymore), drop all associated state.
633 } else if (Partial || Other.Partial) {
634 // If we're doing a merge on a path that's previously seen a partial
635 // merge, conservatively drop the sequence, to avoid doing partial
636 // RR elimination. If the branch predicates for the two merge differ,
637 // mixing them is unsafe.
638 ClearSequenceProgress();
640 // Otherwise merge the other PtrState's RRInfo into our RRInfo. At this
641 // point, we know that currently we are not partial. Stash whether or not
642 // the merge operation caused us to undergo a partial merging of reverse
644 Partial = RRI.Merge(Other.RRI);
649 /// \brief Per-BasicBlock state.
651 /// The number of unique control paths from the entry which can reach this
653 unsigned TopDownPathCount;
655 /// The number of unique control paths to exits from this block.
656 unsigned BottomUpPathCount;
658 /// A type for PerPtrTopDown and PerPtrBottomUp.
659 typedef MapVector<const Value *, PtrState> MapTy;
661 /// The top-down traversal uses this to record information known about a
662 /// pointer at the bottom of each block.
665 /// The bottom-up traversal uses this to record information known about a
666 /// pointer at the top of each block.
667 MapTy PerPtrBottomUp;
669 /// Effective predecessors of the current block ignoring ignorable edges and
670 /// ignored backedges.
671 SmallVector<BasicBlock *, 2> Preds;
672 /// Effective successors of the current block ignoring ignorable edges and
673 /// ignored backedges.
674 SmallVector<BasicBlock *, 2> Succs;
677 BBState() : TopDownPathCount(0), BottomUpPathCount(0) {}
679 typedef MapTy::iterator ptr_iterator;
680 typedef MapTy::const_iterator ptr_const_iterator;
682 ptr_iterator top_down_ptr_begin() { return PerPtrTopDown.begin(); }
683 ptr_iterator top_down_ptr_end() { return PerPtrTopDown.end(); }
684 ptr_const_iterator top_down_ptr_begin() const {
685 return PerPtrTopDown.begin();
687 ptr_const_iterator top_down_ptr_end() const {
688 return PerPtrTopDown.end();
691 ptr_iterator bottom_up_ptr_begin() { return PerPtrBottomUp.begin(); }
692 ptr_iterator bottom_up_ptr_end() { return PerPtrBottomUp.end(); }
693 ptr_const_iterator bottom_up_ptr_begin() const {
694 return PerPtrBottomUp.begin();
696 ptr_const_iterator bottom_up_ptr_end() const {
697 return PerPtrBottomUp.end();
700 /// Mark this block as being an entry block, which has one path from the
701 /// entry by definition.
702 void SetAsEntry() { TopDownPathCount = 1; }
704 /// Mark this block as being an exit block, which has one path to an exit by
706 void SetAsExit() { BottomUpPathCount = 1; }
708 /// Attempt to find the PtrState object describing the top down state for
709 /// pointer Arg. Return a new initialized PtrState describing the top down
710 /// state for Arg if we do not find one.
711 PtrState &getPtrTopDownState(const Value *Arg) {
712 return PerPtrTopDown[Arg];
715 /// Attempt to find the PtrState object describing the bottom up state for
716 /// pointer Arg. Return a new initialized PtrState describing the bottom up
717 /// state for Arg if we do not find one.
718 PtrState &getPtrBottomUpState(const Value *Arg) {
719 return PerPtrBottomUp[Arg];
722 /// Attempt to find the PtrState object describing the bottom up state for
724 ptr_iterator findPtrBottomUpState(const Value *Arg) {
725 return PerPtrBottomUp.find(Arg);
728 void clearBottomUpPointers() {
729 PerPtrBottomUp.clear();
732 void clearTopDownPointers() {
733 PerPtrTopDown.clear();
736 void InitFromPred(const BBState &Other);
737 void InitFromSucc(const BBState &Other);
738 void MergePred(const BBState &Other);
739 void MergeSucc(const BBState &Other);
741 /// Compute the number of possible unique paths from an entry to an exit
742 /// which pass through this block. This is only valid after both the
743 /// top-down and bottom-up traversals are complete.
745 /// Returns true if overflow occured. Returns false if overflow did not
747 bool GetAllPathCountWithOverflow(unsigned &PathCount) const {
748 assert(TopDownPathCount != 0);
749 assert(BottomUpPathCount != 0);
750 unsigned long long Product =
751 (unsigned long long)TopDownPathCount*BottomUpPathCount;
753 // Overflow occured if any of the upper bits of Product are set.
754 return Product >> 32;
757 // Specialized CFG utilities.
758 typedef SmallVectorImpl<BasicBlock *>::const_iterator edge_iterator;
759 edge_iterator pred_begin() const { return Preds.begin(); }
760 edge_iterator pred_end() const { return Preds.end(); }
761 edge_iterator succ_begin() const { return Succs.begin(); }
762 edge_iterator succ_end() const { return Succs.end(); }
764 void addSucc(BasicBlock *Succ) { Succs.push_back(Succ); }
765 void addPred(BasicBlock *Pred) { Preds.push_back(Pred); }
767 bool isExit() const { return Succs.empty(); }
771 void BBState::InitFromPred(const BBState &Other) {
772 PerPtrTopDown = Other.PerPtrTopDown;
773 TopDownPathCount = Other.TopDownPathCount;
776 void BBState::InitFromSucc(const BBState &Other) {
777 PerPtrBottomUp = Other.PerPtrBottomUp;
778 BottomUpPathCount = Other.BottomUpPathCount;
781 /// The top-down traversal uses this to merge information about predecessors to
782 /// form the initial state for a new block.
783 void BBState::MergePred(const BBState &Other) {
784 // Other.TopDownPathCount can be 0, in which case it is either dead or a
785 // loop backedge. Loop backedges are special.
786 TopDownPathCount += Other.TopDownPathCount;
788 // Check for overflow. If we have overflow, fall back to conservative
790 if (TopDownPathCount < Other.TopDownPathCount) {
791 clearTopDownPointers();
795 // For each entry in the other set, if our set has an entry with the same key,
796 // merge the entries. Otherwise, copy the entry and merge it with an empty
798 for (ptr_const_iterator MI = Other.top_down_ptr_begin(),
799 ME = Other.top_down_ptr_end(); MI != ME; ++MI) {
800 std::pair<ptr_iterator, bool> Pair = PerPtrTopDown.insert(*MI);
801 Pair.first->second.Merge(Pair.second ? PtrState() : MI->second,
805 // For each entry in our set, if the other set doesn't have an entry with the
806 // same key, force it to merge with an empty entry.
807 for (ptr_iterator MI = top_down_ptr_begin(),
808 ME = top_down_ptr_end(); MI != ME; ++MI)
809 if (Other.PerPtrTopDown.find(MI->first) == Other.PerPtrTopDown.end())
810 MI->second.Merge(PtrState(), /*TopDown=*/true);
813 /// The bottom-up traversal uses this to merge information about successors to
814 /// form the initial state for a new block.
815 void BBState::MergeSucc(const BBState &Other) {
816 // Other.BottomUpPathCount can be 0, in which case it is either dead or a
817 // loop backedge. Loop backedges are special.
818 BottomUpPathCount += Other.BottomUpPathCount;
820 // Check for overflow. If we have overflow, fall back to conservative
822 if (BottomUpPathCount < Other.BottomUpPathCount) {
823 clearBottomUpPointers();
827 // For each entry in the other set, if our set has an entry with the
828 // same key, merge the entries. Otherwise, copy the entry and merge
829 // it with an empty entry.
830 for (ptr_const_iterator MI = Other.bottom_up_ptr_begin(),
831 ME = Other.bottom_up_ptr_end(); MI != ME; ++MI) {
832 std::pair<ptr_iterator, bool> Pair = PerPtrBottomUp.insert(*MI);
833 Pair.first->second.Merge(Pair.second ? PtrState() : MI->second,
837 // For each entry in our set, if the other set doesn't have an entry
838 // with the same key, force it to merge with an empty entry.
839 for (ptr_iterator MI = bottom_up_ptr_begin(),
840 ME = bottom_up_ptr_end(); MI != ME; ++MI)
841 if (Other.PerPtrBottomUp.find(MI->first) == Other.PerPtrBottomUp.end())
842 MI->second.Merge(PtrState(), /*TopDown=*/false);
845 // Only enable ARC Annotations if we are building a debug version of
848 #define ARC_ANNOTATIONS
851 // Define some macros along the lines of DEBUG and some helper functions to make
852 // it cleaner to create annotations in the source code and to no-op when not
853 // building in debug mode.
854 #ifdef ARC_ANNOTATIONS
856 #include "llvm/Support/CommandLine.h"
858 /// Enable/disable ARC sequence annotations.
860 EnableARCAnnotations("enable-objc-arc-annotations", cl::init(false),
861 cl::desc("Enable emission of arc data flow analysis "
864 DisableCheckForCFGHazards("disable-objc-arc-checkforcfghazards", cl::init(false),
865 cl::desc("Disable check for cfg hazards when "
867 static cl::opt<std::string>
868 ARCAnnotationTargetIdentifier("objc-arc-annotation-target-identifier",
870 cl::desc("filter out all data flow annotations "
871 "but those that apply to the given "
872 "target llvm identifier."));
874 /// This function appends a unique ARCAnnotationProvenanceSourceMDKind id to an
875 /// instruction so that we can track backwards when post processing via the llvm
876 /// arc annotation processor tool. If the function is an
877 static MDString *AppendMDNodeToSourcePtr(unsigned NodeId,
881 // If pointer is a result of an instruction and it does not have a source
882 // MDNode it, attach a new MDNode onto it. If pointer is a result of
883 // an instruction and does have a source MDNode attached to it, return a
884 // reference to said Node. Otherwise just return 0.
885 if (Instruction *Inst = dyn_cast<Instruction>(Ptr)) {
887 if (!(Node = Inst->getMetadata(NodeId))) {
888 // We do not have any node. Generate and attatch the hash MDString to the
891 // We just use an MDString to ensure that this metadata gets written out
892 // of line at the module level and to provide a very simple format
893 // encoding the information herein. Both of these makes it simpler to
894 // parse the annotations by a simple external program.
896 raw_string_ostream os(Str);
897 os << "(" << Inst->getParent()->getParent()->getName() << ",%"
898 << Inst->getName() << ")";
900 Hash = MDString::get(Inst->getContext(), os.str());
901 Inst->setMetadata(NodeId, MDNode::get(Inst->getContext(),Hash));
903 // We have a node. Grab its hash and return it.
904 assert(Node->getNumOperands() == 1 &&
905 "An ARCAnnotationProvenanceSourceMDKind can only have 1 operand.");
906 Hash = cast<MDString>(Node->getOperand(0));
908 } else if (Argument *Arg = dyn_cast<Argument>(Ptr)) {
910 raw_string_ostream os(str);
911 os << "(" << Arg->getParent()->getName() << ",%" << Arg->getName()
913 Hash = MDString::get(Arg->getContext(), os.str());
919 static std::string SequenceToString(Sequence A) {
921 raw_string_ostream os(str);
926 /// Helper function to change a Sequence into a String object using our overload
927 /// for raw_ostream so we only have printing code in one location.
928 static MDString *SequenceToMDString(LLVMContext &Context,
930 return MDString::get(Context, SequenceToString(A));
933 /// A simple function to generate a MDNode which describes the change in state
934 /// for Value *Ptr caused by Instruction *Inst.
935 static void AppendMDNodeToInstForPtr(unsigned NodeId,
938 MDString *PtrSourceMDNodeID,
942 Value *tmp[3] = {PtrSourceMDNodeID,
943 SequenceToMDString(Inst->getContext(),
945 SequenceToMDString(Inst->getContext(),
947 Node = MDNode::get(Inst->getContext(),
948 ArrayRef<Value*>(tmp, 3));
950 Inst->setMetadata(NodeId, Node);
953 /// Add to the beginning of the basic block llvm.ptr.annotations which show the
954 /// state of a pointer at the entrance to a basic block.
955 static void GenerateARCBBEntranceAnnotation(const char *Name, BasicBlock *BB,
956 Value *Ptr, Sequence Seq) {
957 // If we have a target identifier, make sure that we match it before
959 if(!ARCAnnotationTargetIdentifier.empty() &&
960 !Ptr->getName().equals(ARCAnnotationTargetIdentifier))
963 Module *M = BB->getParent()->getParent();
964 LLVMContext &C = M->getContext();
965 Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C));
966 Type *I8XX = PointerType::getUnqual(I8X);
967 Type *Params[] = {I8XX, I8XX};
968 FunctionType *FTy = FunctionType::get(Type::getVoidTy(C),
969 ArrayRef<Type*>(Params, 2),
971 Constant *Callee = M->getOrInsertFunction(Name, FTy);
973 IRBuilder<> Builder(BB, BB->getFirstInsertionPt());
976 StringRef Tmp = Ptr->getName();
977 if (0 == (PtrName = M->getGlobalVariable(Tmp, true))) {
978 Value *ActualPtrName = Builder.CreateGlobalStringPtr(Tmp,
980 PtrName = new GlobalVariable(*M, I8X, true, GlobalVariable::InternalLinkage,
981 cast<Constant>(ActualPtrName), Tmp);
985 std::string SeqStr = SequenceToString(Seq);
986 if (0 == (S = M->getGlobalVariable(SeqStr, true))) {
987 Value *ActualPtrName = Builder.CreateGlobalStringPtr(SeqStr,
989 S = new GlobalVariable(*M, I8X, true, GlobalVariable::InternalLinkage,
990 cast<Constant>(ActualPtrName), SeqStr);
993 Builder.CreateCall2(Callee, PtrName, S);
996 /// Add to the end of the basic block llvm.ptr.annotations which show the state
997 /// of the pointer at the bottom of the basic block.
998 static void GenerateARCBBTerminatorAnnotation(const char *Name, BasicBlock *BB,
999 Value *Ptr, Sequence Seq) {
1000 // If we have a target identifier, make sure that we match it before emitting
1002 if(!ARCAnnotationTargetIdentifier.empty() &&
1003 !Ptr->getName().equals(ARCAnnotationTargetIdentifier))
1006 Module *M = BB->getParent()->getParent();
1007 LLVMContext &C = M->getContext();
1008 Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C));
1009 Type *I8XX = PointerType::getUnqual(I8X);
1010 Type *Params[] = {I8XX, I8XX};
1011 FunctionType *FTy = FunctionType::get(Type::getVoidTy(C),
1012 ArrayRef<Type*>(Params, 2),
1013 /*isVarArg=*/false);
1014 Constant *Callee = M->getOrInsertFunction(Name, FTy);
1016 IRBuilder<> Builder(BB, llvm::prior(BB->end()));
1019 StringRef Tmp = Ptr->getName();
1020 if (0 == (PtrName = M->getGlobalVariable(Tmp, true))) {
1021 Value *ActualPtrName = Builder.CreateGlobalStringPtr(Tmp,
1023 PtrName = new GlobalVariable(*M, I8X, true, GlobalVariable::InternalLinkage,
1024 cast<Constant>(ActualPtrName), Tmp);
1028 std::string SeqStr = SequenceToString(Seq);
1029 if (0 == (S = M->getGlobalVariable(SeqStr, true))) {
1030 Value *ActualPtrName = Builder.CreateGlobalStringPtr(SeqStr,
1032 S = new GlobalVariable(*M, I8X, true, GlobalVariable::InternalLinkage,
1033 cast<Constant>(ActualPtrName), SeqStr);
1035 Builder.CreateCall2(Callee, PtrName, S);
1038 /// Adds a source annotation to pointer and a state change annotation to Inst
1039 /// referencing the source annotation and the old/new state of pointer.
1040 static void GenerateARCAnnotation(unsigned InstMDId,
1046 if (EnableARCAnnotations) {
1047 // If we have a target identifier, make sure that we match it before
1048 // emitting an annotation.
1049 if(!ARCAnnotationTargetIdentifier.empty() &&
1050 !Ptr->getName().equals(ARCAnnotationTargetIdentifier))
1053 // First generate the source annotation on our pointer. This will return an
1054 // MDString* if Ptr actually comes from an instruction implying we can put
1055 // in a source annotation. If AppendMDNodeToSourcePtr returns 0 (i.e. NULL),
1056 // then we know that our pointer is from an Argument so we put a reference
1057 // to the argument number.
1059 // The point of this is to make it easy for the
1060 // llvm-arc-annotation-processor tool to cross reference where the source
1061 // pointer is in the LLVM IR since the LLVM IR parser does not submit such
1062 // information via debug info for backends to use (since why would anyone
1063 // need such a thing from LLVM IR besides in non standard cases
1065 MDString *SourcePtrMDNode =
1066 AppendMDNodeToSourcePtr(PtrMDId, Ptr);
1067 AppendMDNodeToInstForPtr(InstMDId, Inst, Ptr, SourcePtrMDNode, OldSeq,
1072 // The actual interface for accessing the above functionality is defined via
1073 // some simple macros which are defined below. We do this so that the user does
1074 // not need to pass in what metadata id is needed resulting in cleaner code and
1075 // additionally since it provides an easy way to conditionally no-op all
1076 // annotation support in a non-debug build.
1078 /// Use this macro to annotate a sequence state change when processing
1079 /// instructions bottom up,
1080 #define ANNOTATE_BOTTOMUP(inst, ptr, old, new) \
1081 GenerateARCAnnotation(ARCAnnotationBottomUpMDKind, \
1082 ARCAnnotationProvenanceSourceMDKind, (inst), \
1083 const_cast<Value*>(ptr), (old), (new))
1084 /// Use this macro to annotate a sequence state change when processing
1085 /// instructions top down.
1086 #define ANNOTATE_TOPDOWN(inst, ptr, old, new) \
1087 GenerateARCAnnotation(ARCAnnotationTopDownMDKind, \
1088 ARCAnnotationProvenanceSourceMDKind, (inst), \
1089 const_cast<Value*>(ptr), (old), (new))
1091 #define ANNOTATE_BB(_states, _bb, _name, _type, _direction) \
1093 if (EnableARCAnnotations) { \
1094 for(BBState::ptr_const_iterator I = (_states)._direction##_ptr_begin(), \
1095 E = (_states)._direction##_ptr_end(); I != E; ++I) { \
1096 Value *Ptr = const_cast<Value*>(I->first); \
1097 Sequence Seq = I->second.GetSeq(); \
1098 GenerateARCBB ## _type ## Annotation(_name, (_bb), Ptr, Seq); \
1103 #define ANNOTATE_BOTTOMUP_BBSTART(_states, _basicblock) \
1104 ANNOTATE_BB(_states, _basicblock, "llvm.arc.annotation.bottomup.bbstart", \
1105 Entrance, bottom_up)
1106 #define ANNOTATE_BOTTOMUP_BBEND(_states, _basicblock) \
1107 ANNOTATE_BB(_states, _basicblock, "llvm.arc.annotation.bottomup.bbend", \
1108 Terminator, bottom_up)
1109 #define ANNOTATE_TOPDOWN_BBSTART(_states, _basicblock) \
1110 ANNOTATE_BB(_states, _basicblock, "llvm.arc.annotation.topdown.bbstart", \
1112 #define ANNOTATE_TOPDOWN_BBEND(_states, _basicblock) \
1113 ANNOTATE_BB(_states, _basicblock, "llvm.arc.annotation.topdown.bbend", \
1114 Terminator, top_down)
1116 #else // !ARC_ANNOTATION
1117 // If annotations are off, noop.
1118 #define ANNOTATE_BOTTOMUP(inst, ptr, old, new)
1119 #define ANNOTATE_TOPDOWN(inst, ptr, old, new)
1120 #define ANNOTATE_BOTTOMUP_BBSTART(states, basicblock)
1121 #define ANNOTATE_BOTTOMUP_BBEND(states, basicblock)
1122 #define ANNOTATE_TOPDOWN_BBSTART(states, basicblock)
1123 #define ANNOTATE_TOPDOWN_BBEND(states, basicblock)
1124 #endif // !ARC_ANNOTATION
1127 /// \brief The main ARC optimization pass.
1128 class ObjCARCOpt : public FunctionPass {
1130 ProvenanceAnalysis PA;
1131 ARCRuntimeEntryPoints EP;
1133 // This is used to track if a pointer is stored into an alloca.
1134 DenseSet<const Value *> MultiOwnersSet;
1136 /// A flag indicating whether this optimization pass should run.
1139 /// Flags which determine whether each of the interesting runtine functions
1140 /// is in fact used in the current function.
1141 unsigned UsedInThisFunction;
1143 /// The Metadata Kind for clang.imprecise_release metadata.
1144 unsigned ImpreciseReleaseMDKind;
1146 /// The Metadata Kind for clang.arc.copy_on_escape metadata.
1147 unsigned CopyOnEscapeMDKind;
1149 /// The Metadata Kind for clang.arc.no_objc_arc_exceptions metadata.
1150 unsigned NoObjCARCExceptionsMDKind;
1152 #ifdef ARC_ANNOTATIONS
1153 /// The Metadata Kind for llvm.arc.annotation.bottomup metadata.
1154 unsigned ARCAnnotationBottomUpMDKind;
1155 /// The Metadata Kind for llvm.arc.annotation.topdown metadata.
1156 unsigned ARCAnnotationTopDownMDKind;
1157 /// The Metadata Kind for llvm.arc.annotation.provenancesource metadata.
1158 unsigned ARCAnnotationProvenanceSourceMDKind;
1159 #endif // ARC_ANNOATIONS
1161 bool IsRetainBlockOptimizable(const Instruction *Inst);
1163 bool OptimizeRetainRVCall(Function &F, Instruction *RetainRV);
1164 void OptimizeAutoreleaseRVCall(Function &F, Instruction *AutoreleaseRV,
1165 InstructionClass &Class);
1166 bool OptimizeRetainBlockCall(Function &F, Instruction *RetainBlock,
1167 InstructionClass &Class);
1168 void OptimizeIndividualCalls(Function &F);
1170 void CheckForCFGHazards(const BasicBlock *BB,
1171 DenseMap<const BasicBlock *, BBState> &BBStates,
1172 BBState &MyStates) const;
1173 bool VisitInstructionBottomUp(Instruction *Inst,
1175 MapVector<Value *, RRInfo> &Retains,
1177 bool VisitBottomUp(BasicBlock *BB,
1178 DenseMap<const BasicBlock *, BBState> &BBStates,
1179 MapVector<Value *, RRInfo> &Retains);
1180 bool VisitInstructionTopDown(Instruction *Inst,
1181 DenseMap<Value *, RRInfo> &Releases,
1183 bool VisitTopDown(BasicBlock *BB,
1184 DenseMap<const BasicBlock *, BBState> &BBStates,
1185 DenseMap<Value *, RRInfo> &Releases);
1186 bool Visit(Function &F,
1187 DenseMap<const BasicBlock *, BBState> &BBStates,
1188 MapVector<Value *, RRInfo> &Retains,
1189 DenseMap<Value *, RRInfo> &Releases);
1191 void MoveCalls(Value *Arg, RRInfo &RetainsToMove, RRInfo &ReleasesToMove,
1192 MapVector<Value *, RRInfo> &Retains,
1193 DenseMap<Value *, RRInfo> &Releases,
1194 SmallVectorImpl<Instruction *> &DeadInsts,
1197 bool ConnectTDBUTraversals(DenseMap<const BasicBlock *, BBState> &BBStates,
1198 MapVector<Value *, RRInfo> &Retains,
1199 DenseMap<Value *, RRInfo> &Releases,
1201 SmallVectorImpl<Instruction *> &NewRetains,
1202 SmallVectorImpl<Instruction *> &NewReleases,
1203 SmallVectorImpl<Instruction *> &DeadInsts,
1204 RRInfo &RetainsToMove,
1205 RRInfo &ReleasesToMove,
1208 bool &AnyPairsCompletelyEliminated);
1210 bool PerformCodePlacement(DenseMap<const BasicBlock *, BBState> &BBStates,
1211 MapVector<Value *, RRInfo> &Retains,
1212 DenseMap<Value *, RRInfo> &Releases,
1215 void OptimizeWeakCalls(Function &F);
1217 bool OptimizeSequences(Function &F);
1219 void OptimizeReturns(Function &F);
1222 void GatherStatistics(Function &F, bool AfterOptimization = false);
1225 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
1226 virtual bool doInitialization(Module &M);
1227 virtual bool runOnFunction(Function &F);
1228 virtual void releaseMemory();
1232 ObjCARCOpt() : FunctionPass(ID) {
1233 initializeObjCARCOptPass(*PassRegistry::getPassRegistry());
1238 char ObjCARCOpt::ID = 0;
1239 INITIALIZE_PASS_BEGIN(ObjCARCOpt,
1240 "objc-arc", "ObjC ARC optimization", false, false)
1241 INITIALIZE_PASS_DEPENDENCY(ObjCARCAliasAnalysis)
1242 INITIALIZE_PASS_END(ObjCARCOpt,
1243 "objc-arc", "ObjC ARC optimization", false, false)
1245 Pass *llvm::createObjCARCOptPass() {
1246 return new ObjCARCOpt();
1249 void ObjCARCOpt::getAnalysisUsage(AnalysisUsage &AU) const {
1250 AU.addRequired<ObjCARCAliasAnalysis>();
1251 AU.addRequired<AliasAnalysis>();
1252 // ARC optimization doesn't currently split critical edges.
1253 AU.setPreservesCFG();
1256 bool ObjCARCOpt::IsRetainBlockOptimizable(const Instruction *Inst) {
1257 // Without the magic metadata tag, we have to assume this might be an
1258 // objc_retainBlock call inserted to convert a block pointer to an id,
1259 // in which case it really is needed.
1260 if (!Inst->getMetadata(CopyOnEscapeMDKind))
1263 // If the pointer "escapes" (not including being used in a call),
1264 // the copy may be needed.
1265 if (DoesRetainableObjPtrEscape(Inst))
1268 // Otherwise, it's not needed.
1272 /// Turn objc_retainAutoreleasedReturnValue into objc_retain if the operand is
1273 /// not a return value. Or, if it can be paired with an
1274 /// objc_autoreleaseReturnValue, delete the pair and return true.
1276 ObjCARCOpt::OptimizeRetainRVCall(Function &F, Instruction *RetainRV) {
1277 // Check for the argument being from an immediately preceding call or invoke.
1278 const Value *Arg = GetObjCArg(RetainRV);
1279 ImmutableCallSite CS(Arg);
1280 if (const Instruction *Call = CS.getInstruction()) {
1281 if (Call->getParent() == RetainRV->getParent()) {
1282 BasicBlock::const_iterator I = Call;
1284 while (IsNoopInstruction(I)) ++I;
1285 if (&*I == RetainRV)
1287 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(Call)) {
1288 BasicBlock *RetainRVParent = RetainRV->getParent();
1289 if (II->getNormalDest() == RetainRVParent) {
1290 BasicBlock::const_iterator I = RetainRVParent->begin();
1291 while (IsNoopInstruction(I)) ++I;
1292 if (&*I == RetainRV)
1298 // Check for being preceded by an objc_autoreleaseReturnValue on the same
1299 // pointer. In this case, we can delete the pair.
1300 BasicBlock::iterator I = RetainRV, Begin = RetainRV->getParent()->begin();
1302 do --I; while (I != Begin && IsNoopInstruction(I));
1303 if (GetBasicInstructionClass(I) == IC_AutoreleaseRV &&
1304 GetObjCArg(I) == Arg) {
1308 DEBUG(dbgs() << "Erasing autoreleaseRV,retainRV pair: " << *I << "\n"
1309 << "Erasing " << *RetainRV << "\n");
1311 EraseInstruction(I);
1312 EraseInstruction(RetainRV);
1317 // Turn it to a plain objc_retain.
1321 DEBUG(dbgs() << "Transforming objc_retainAutoreleasedReturnValue => "
1322 "objc_retain since the operand is not a return value.\n"
1323 "Old = " << *RetainRV << "\n");
1325 Constant *NewDecl = EP.get(ARCRuntimeEntryPoints::EPT_Retain);
1326 cast<CallInst>(RetainRV)->setCalledFunction(NewDecl);
1328 DEBUG(dbgs() << "New = " << *RetainRV << "\n");
1333 /// Turn objc_autoreleaseReturnValue into objc_autorelease if the result is not
1334 /// used as a return value.
1336 ObjCARCOpt::OptimizeAutoreleaseRVCall(Function &F, Instruction *AutoreleaseRV,
1337 InstructionClass &Class) {
1338 // Check for a return of the pointer value.
1339 const Value *Ptr = GetObjCArg(AutoreleaseRV);
1340 SmallVector<const Value *, 2> Users;
1341 Users.push_back(Ptr);
1343 Ptr = Users.pop_back_val();
1344 for (Value::const_use_iterator UI = Ptr->use_begin(), UE = Ptr->use_end();
1346 const User *I = *UI;
1347 if (isa<ReturnInst>(I) || GetBasicInstructionClass(I) == IC_RetainRV)
1349 if (isa<BitCastInst>(I))
1352 } while (!Users.empty());
1357 DEBUG(dbgs() << "Transforming objc_autoreleaseReturnValue => "
1358 "objc_autorelease since its operand is not used as a return "
1360 "Old = " << *AutoreleaseRV << "\n");
1362 CallInst *AutoreleaseRVCI = cast<CallInst>(AutoreleaseRV);
1363 Constant *NewDecl = EP.get(ARCRuntimeEntryPoints::EPT_Autorelease);
1364 AutoreleaseRVCI->setCalledFunction(NewDecl);
1365 AutoreleaseRVCI->setTailCall(false); // Never tail call objc_autorelease.
1366 Class = IC_Autorelease;
1368 DEBUG(dbgs() << "New: " << *AutoreleaseRV << "\n");
1372 // \brief Attempt to strength reduce objc_retainBlock calls to objc_retain
1375 // Specifically: If an objc_retainBlock call has the copy_on_escape metadata and
1376 // does not escape (following the rules of block escaping), strength reduce the
1377 // objc_retainBlock to an objc_retain.
1379 // TODO: If an objc_retainBlock call is dominated period by a previous
1380 // objc_retainBlock call, strength reduce the objc_retainBlock to an
1383 ObjCARCOpt::OptimizeRetainBlockCall(Function &F, Instruction *Inst,
1384 InstructionClass &Class) {
1385 assert(GetBasicInstructionClass(Inst) == Class);
1386 assert(IC_RetainBlock == Class);
1388 // If we can not optimize Inst, return false.
1389 if (!IsRetainBlockOptimizable(Inst))
1395 DEBUG(dbgs() << "Strength reduced retainBlock => retain.\n");
1396 DEBUG(dbgs() << "Old: " << *Inst << "\n");
1397 CallInst *RetainBlock = cast<CallInst>(Inst);
1398 Constant *NewDecl = EP.get(ARCRuntimeEntryPoints::EPT_Retain);
1399 RetainBlock->setCalledFunction(NewDecl);
1400 // Remove copy_on_escape metadata.
1401 RetainBlock->setMetadata(CopyOnEscapeMDKind, 0);
1403 DEBUG(dbgs() << "New: " << *Inst << "\n");
1407 /// Visit each call, one at a time, and make simplifications without doing any
1408 /// additional analysis.
1409 void ObjCARCOpt::OptimizeIndividualCalls(Function &F) {
1410 DEBUG(dbgs() << "\n== ObjCARCOpt::OptimizeIndividualCalls ==\n");
1411 // Reset all the flags in preparation for recomputing them.
1412 UsedInThisFunction = 0;
1414 // Visit all objc_* calls in F.
1415 for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
1416 Instruction *Inst = &*I++;
1418 InstructionClass Class = GetBasicInstructionClass(Inst);
1420 DEBUG(dbgs() << "Visiting: Class: " << Class << "; " << *Inst << "\n");
1425 // Delete no-op casts. These function calls have special semantics, but
1426 // the semantics are entirely implemented via lowering in the front-end,
1427 // so by the time they reach the optimizer, they are just no-op calls
1428 // which return their argument.
1430 // There are gray areas here, as the ability to cast reference-counted
1431 // pointers to raw void* and back allows code to break ARC assumptions,
1432 // however these are currently considered to be unimportant.
1436 DEBUG(dbgs() << "Erasing no-op cast: " << *Inst << "\n");
1437 EraseInstruction(Inst);
1440 // If the pointer-to-weak-pointer is null, it's undefined behavior.
1443 case IC_LoadWeakRetained:
1445 case IC_DestroyWeak: {
1446 CallInst *CI = cast<CallInst>(Inst);
1447 if (IsNullOrUndef(CI->getArgOperand(0))) {
1449 Type *Ty = CI->getArgOperand(0)->getType();
1450 new StoreInst(UndefValue::get(cast<PointerType>(Ty)->getElementType()),
1451 Constant::getNullValue(Ty),
1453 llvm::Value *NewValue = UndefValue::get(CI->getType());
1454 DEBUG(dbgs() << "A null pointer-to-weak-pointer is undefined behavior."
1455 "\nOld = " << *CI << "\nNew = " << *NewValue << "\n");
1456 CI->replaceAllUsesWith(NewValue);
1457 CI->eraseFromParent();
1464 CallInst *CI = cast<CallInst>(Inst);
1465 if (IsNullOrUndef(CI->getArgOperand(0)) ||
1466 IsNullOrUndef(CI->getArgOperand(1))) {
1468 Type *Ty = CI->getArgOperand(0)->getType();
1469 new StoreInst(UndefValue::get(cast<PointerType>(Ty)->getElementType()),
1470 Constant::getNullValue(Ty),
1473 llvm::Value *NewValue = UndefValue::get(CI->getType());
1474 DEBUG(dbgs() << "A null pointer-to-weak-pointer is undefined behavior."
1475 "\nOld = " << *CI << "\nNew = " << *NewValue << "\n");
1477 CI->replaceAllUsesWith(NewValue);
1478 CI->eraseFromParent();
1483 case IC_RetainBlock:
1484 // If we strength reduce an objc_retainBlock to an objc_retain, continue
1485 // onto the objc_retain peephole optimizations. Otherwise break.
1486 OptimizeRetainBlockCall(F, Inst, Class);
1489 if (OptimizeRetainRVCall(F, Inst))
1492 case IC_AutoreleaseRV:
1493 OptimizeAutoreleaseRVCall(F, Inst, Class);
1497 // objc_autorelease(x) -> objc_release(x) if x is otherwise unused.
1498 if (IsAutorelease(Class) && Inst->use_empty()) {
1499 CallInst *Call = cast<CallInst>(Inst);
1500 const Value *Arg = Call->getArgOperand(0);
1501 Arg = FindSingleUseIdentifiedObject(Arg);
1506 // Create the declaration lazily.
1507 LLVMContext &C = Inst->getContext();
1509 Constant *Decl = EP.get(ARCRuntimeEntryPoints::EPT_Release);
1510 CallInst *NewCall = CallInst::Create(Decl, Call->getArgOperand(0), "",
1512 NewCall->setMetadata(ImpreciseReleaseMDKind, MDNode::get(C, None));
1514 DEBUG(dbgs() << "Replacing autorelease{,RV}(x) with objc_release(x) "
1515 "since x is otherwise unused.\nOld: " << *Call << "\nNew: "
1516 << *NewCall << "\n");
1518 EraseInstruction(Call);
1524 // For functions which can never be passed stack arguments, add
1526 if (IsAlwaysTail(Class)) {
1528 DEBUG(dbgs() << "Adding tail keyword to function since it can never be "
1529 "passed stack args: " << *Inst << "\n");
1530 cast<CallInst>(Inst)->setTailCall();
1533 // Ensure that functions that can never have a "tail" keyword due to the
1534 // semantics of ARC truly do not do so.
1535 if (IsNeverTail(Class)) {
1537 DEBUG(dbgs() << "Removing tail keyword from function: " << *Inst <<
1539 cast<CallInst>(Inst)->setTailCall(false);
1542 // Set nounwind as needed.
1543 if (IsNoThrow(Class)) {
1545 DEBUG(dbgs() << "Found no throw class. Setting nounwind on: " << *Inst
1547 cast<CallInst>(Inst)->setDoesNotThrow();
1550 if (!IsNoopOnNull(Class)) {
1551 UsedInThisFunction |= 1 << Class;
1555 const Value *Arg = GetObjCArg(Inst);
1557 // ARC calls with null are no-ops. Delete them.
1558 if (IsNullOrUndef(Arg)) {
1561 DEBUG(dbgs() << "ARC calls with null are no-ops. Erasing: " << *Inst
1563 EraseInstruction(Inst);
1567 // Keep track of which of retain, release, autorelease, and retain_block
1568 // are actually present in this function.
1569 UsedInThisFunction |= 1 << Class;
1571 // If Arg is a PHI, and one or more incoming values to the
1572 // PHI are null, and the call is control-equivalent to the PHI, and there
1573 // are no relevant side effects between the PHI and the call, the call
1574 // could be pushed up to just those paths with non-null incoming values.
1575 // For now, don't bother splitting critical edges for this.
1576 SmallVector<std::pair<Instruction *, const Value *>, 4> Worklist;
1577 Worklist.push_back(std::make_pair(Inst, Arg));
1579 std::pair<Instruction *, const Value *> Pair = Worklist.pop_back_val();
1583 const PHINode *PN = dyn_cast<PHINode>(Arg);
1586 // Determine if the PHI has any null operands, or any incoming
1588 bool HasNull = false;
1589 bool HasCriticalEdges = false;
1590 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
1592 StripPointerCastsAndObjCCalls(PN->getIncomingValue(i));
1593 if (IsNullOrUndef(Incoming))
1595 else if (cast<TerminatorInst>(PN->getIncomingBlock(i)->back())
1596 .getNumSuccessors() != 1) {
1597 HasCriticalEdges = true;
1601 // If we have null operands and no critical edges, optimize.
1602 if (!HasCriticalEdges && HasNull) {
1603 SmallPtrSet<Instruction *, 4> DependingInstructions;
1604 SmallPtrSet<const BasicBlock *, 4> Visited;
1606 // Check that there is nothing that cares about the reference
1607 // count between the call and the phi.
1610 case IC_RetainBlock:
1611 // These can always be moved up.
1614 // These can't be moved across things that care about the retain
1616 FindDependencies(NeedsPositiveRetainCount, Arg,
1617 Inst->getParent(), Inst,
1618 DependingInstructions, Visited, PA);
1620 case IC_Autorelease:
1621 // These can't be moved across autorelease pool scope boundaries.
1622 FindDependencies(AutoreleasePoolBoundary, Arg,
1623 Inst->getParent(), Inst,
1624 DependingInstructions, Visited, PA);
1627 case IC_AutoreleaseRV:
1628 // Don't move these; the RV optimization depends on the autoreleaseRV
1629 // being tail called, and the retainRV being immediately after a call
1630 // (which might still happen if we get lucky with codegen layout, but
1631 // it's not worth taking the chance).
1634 llvm_unreachable("Invalid dependence flavor");
1637 if (DependingInstructions.size() == 1 &&
1638 *DependingInstructions.begin() == PN) {
1641 // Clone the call into each predecessor that has a non-null value.
1642 CallInst *CInst = cast<CallInst>(Inst);
1643 Type *ParamTy = CInst->getArgOperand(0)->getType();
1644 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
1646 StripPointerCastsAndObjCCalls(PN->getIncomingValue(i));
1647 if (!IsNullOrUndef(Incoming)) {
1648 CallInst *Clone = cast<CallInst>(CInst->clone());
1649 Value *Op = PN->getIncomingValue(i);
1650 Instruction *InsertPos = &PN->getIncomingBlock(i)->back();
1651 if (Op->getType() != ParamTy)
1652 Op = new BitCastInst(Op, ParamTy, "", InsertPos);
1653 Clone->setArgOperand(0, Op);
1654 Clone->insertBefore(InsertPos);
1656 DEBUG(dbgs() << "Cloning "
1658 "And inserting clone at " << *InsertPos << "\n");
1659 Worklist.push_back(std::make_pair(Clone, Incoming));
1662 // Erase the original call.
1663 DEBUG(dbgs() << "Erasing: " << *CInst << "\n");
1664 EraseInstruction(CInst);
1668 } while (!Worklist.empty());
1672 /// If we have a top down pointer in the S_Use state, make sure that there are
1673 /// no CFG hazards by checking the states of various bottom up pointers.
1674 static void CheckForUseCFGHazard(const Sequence SuccSSeq,
1675 const bool SuccSRRIKnownSafe,
1677 bool &SomeSuccHasSame,
1678 bool &AllSuccsHaveSame,
1679 bool &NotAllSeqEqualButKnownSafe,
1680 bool &ShouldContinue) {
1682 case S_CanRelease: {
1683 if (!S.IsKnownSafe() && !SuccSRRIKnownSafe) {
1684 S.ClearSequenceProgress();
1687 S.SetCFGHazardAfflicted(true);
1688 ShouldContinue = true;
1692 SomeSuccHasSame = true;
1696 case S_MovableRelease:
1697 if (!S.IsKnownSafe() && !SuccSRRIKnownSafe)
1698 AllSuccsHaveSame = false;
1700 NotAllSeqEqualButKnownSafe = true;
1703 llvm_unreachable("bottom-up pointer in retain state!");
1705 llvm_unreachable("This should have been handled earlier.");
1709 /// If we have a Top Down pointer in the S_CanRelease state, make sure that
1710 /// there are no CFG hazards by checking the states of various bottom up
1712 static void CheckForCanReleaseCFGHazard(const Sequence SuccSSeq,
1713 const bool SuccSRRIKnownSafe,
1715 bool &SomeSuccHasSame,
1716 bool &AllSuccsHaveSame,
1717 bool &NotAllSeqEqualButKnownSafe) {
1720 SomeSuccHasSame = true;
1724 case S_MovableRelease:
1726 if (!S.IsKnownSafe() && !SuccSRRIKnownSafe)
1727 AllSuccsHaveSame = false;
1729 NotAllSeqEqualButKnownSafe = true;
1732 llvm_unreachable("bottom-up pointer in retain state!");
1734 llvm_unreachable("This should have been handled earlier.");
1738 /// Check for critical edges, loop boundaries, irreducible control flow, or
1739 /// other CFG structures where moving code across the edge would result in it
1740 /// being executed more.
1742 ObjCARCOpt::CheckForCFGHazards(const BasicBlock *BB,
1743 DenseMap<const BasicBlock *, BBState> &BBStates,
1744 BBState &MyStates) const {
1745 // If any top-down local-use or possible-dec has a succ which is earlier in
1746 // the sequence, forget it.
1747 for (BBState::ptr_iterator I = MyStates.top_down_ptr_begin(),
1748 E = MyStates.top_down_ptr_end(); I != E; ++I) {
1749 PtrState &S = I->second;
1750 const Sequence Seq = I->second.GetSeq();
1752 // We only care about S_Retain, S_CanRelease, and S_Use.
1756 // Make sure that if extra top down states are added in the future that this
1757 // code is updated to handle it.
1758 assert((Seq == S_Retain || Seq == S_CanRelease || Seq == S_Use) &&
1759 "Unknown top down sequence state.");
1761 const Value *Arg = I->first;
1762 const TerminatorInst *TI = cast<TerminatorInst>(&BB->back());
1763 bool SomeSuccHasSame = false;
1764 bool AllSuccsHaveSame = true;
1765 bool NotAllSeqEqualButKnownSafe = false;
1767 succ_const_iterator SI(TI), SE(TI, false);
1769 for (; SI != SE; ++SI) {
1770 // If VisitBottomUp has pointer information for this successor, take
1771 // what we know about it.
1772 const DenseMap<const BasicBlock *, BBState>::iterator BBI =
1774 assert(BBI != BBStates.end());
1775 const PtrState &SuccS = BBI->second.getPtrBottomUpState(Arg);
1776 const Sequence SuccSSeq = SuccS.GetSeq();
1778 // If bottom up, the pointer is in an S_None state, clear the sequence
1779 // progress since the sequence in the bottom up state finished
1780 // suggesting a mismatch in between retains/releases. This is true for
1781 // all three cases that we are handling here: S_Retain, S_Use, and
1783 if (SuccSSeq == S_None) {
1784 S.ClearSequenceProgress();
1788 // If we have S_Use or S_CanRelease, perform our check for cfg hazard
1790 const bool SuccSRRIKnownSafe = SuccS.IsKnownSafe();
1792 // *NOTE* We do not use Seq from above here since we are allowing for
1793 // S.GetSeq() to change while we are visiting basic blocks.
1794 switch(S.GetSeq()) {
1796 bool ShouldContinue = false;
1797 CheckForUseCFGHazard(SuccSSeq, SuccSRRIKnownSafe, S, SomeSuccHasSame,
1798 AllSuccsHaveSame, NotAllSeqEqualButKnownSafe,
1804 case S_CanRelease: {
1805 CheckForCanReleaseCFGHazard(SuccSSeq, SuccSRRIKnownSafe, S,
1806 SomeSuccHasSame, AllSuccsHaveSame,
1807 NotAllSeqEqualButKnownSafe);
1814 case S_MovableRelease:
1819 // If the state at the other end of any of the successor edges
1820 // matches the current state, require all edges to match. This
1821 // guards against loops in the middle of a sequence.
1822 if (SomeSuccHasSame && !AllSuccsHaveSame) {
1823 S.ClearSequenceProgress();
1824 } else if (NotAllSeqEqualButKnownSafe) {
1825 // If we would have cleared the state foregoing the fact that we are known
1826 // safe, stop code motion. This is because whether or not it is safe to
1827 // remove RR pairs via KnownSafe is an orthogonal concept to whether we
1828 // are allowed to perform code motion.
1829 S.SetCFGHazardAfflicted(true);
1835 ObjCARCOpt::VisitInstructionBottomUp(Instruction *Inst,
1837 MapVector<Value *, RRInfo> &Retains,
1838 BBState &MyStates) {
1839 bool NestingDetected = false;
1840 InstructionClass Class = GetInstructionClass(Inst);
1841 const Value *Arg = 0;
1843 DEBUG(dbgs() << "Class: " << Class << "\n");
1847 Arg = GetObjCArg(Inst);
1849 PtrState &S = MyStates.getPtrBottomUpState(Arg);
1851 // If we see two releases in a row on the same pointer. If so, make
1852 // a note, and we'll cicle back to revisit it after we've
1853 // hopefully eliminated the second release, which may allow us to
1854 // eliminate the first release too.
1855 // Theoretically we could implement removal of nested retain+release
1856 // pairs by making PtrState hold a stack of states, but this is
1857 // simple and avoids adding overhead for the non-nested case.
1858 if (S.GetSeq() == S_Release || S.GetSeq() == S_MovableRelease) {
1859 DEBUG(dbgs() << "Found nested releases (i.e. a release pair)\n");
1860 NestingDetected = true;
1863 MDNode *ReleaseMetadata = Inst->getMetadata(ImpreciseReleaseMDKind);
1864 Sequence NewSeq = ReleaseMetadata ? S_MovableRelease : S_Release;
1865 ANNOTATE_BOTTOMUP(Inst, Arg, S.GetSeq(), NewSeq);
1866 S.ResetSequenceProgress(NewSeq);
1867 S.SetReleaseMetadata(ReleaseMetadata);
1868 S.SetKnownSafe(S.HasKnownPositiveRefCount());
1869 S.SetTailCallRelease(cast<CallInst>(Inst)->isTailCall());
1871 S.SetKnownPositiveRefCount();
1874 case IC_RetainBlock:
1875 // In OptimizeIndividualCalls, we have strength reduced all optimizable
1876 // objc_retainBlocks to objc_retains. Thus at this point any
1877 // objc_retainBlocks that we see are not optimizable.
1881 Arg = GetObjCArg(Inst);
1883 PtrState &S = MyStates.getPtrBottomUpState(Arg);
1884 S.SetKnownPositiveRefCount();
1886 Sequence OldSeq = S.GetSeq();
1890 case S_MovableRelease:
1892 // If OldSeq is not S_Use or OldSeq is S_Use and we are tracking an
1893 // imprecise release, clear our reverse insertion points.
1894 if (OldSeq != S_Use || S.IsTrackingImpreciseReleases())
1895 S.ClearReverseInsertPts();
1898 // Don't do retain+release tracking for IC_RetainRV, because it's
1899 // better to let it remain as the first instruction after a call.
1900 if (Class != IC_RetainRV)
1901 Retains[Inst] = S.GetRRInfo();
1902 S.ClearSequenceProgress();
1907 llvm_unreachable("bottom-up pointer in retain state!");
1909 ANNOTATE_BOTTOMUP(Inst, Arg, OldSeq, S.GetSeq());
1910 // A retain moving bottom up can be a use.
1913 case IC_AutoreleasepoolPop:
1914 // Conservatively, clear MyStates for all known pointers.
1915 MyStates.clearBottomUpPointers();
1916 return NestingDetected;
1917 case IC_AutoreleasepoolPush:
1919 // These are irrelevant.
1920 return NestingDetected;
1922 // If we have a store into an alloca of a pointer we are tracking, the
1923 // pointer has multiple owners implying that we must be more conservative.
1925 // This comes up in the context of a pointer being ``KnownSafe''. In the
1926 // presense of a block being initialized, the frontend will emit the
1927 // objc_retain on the original pointer and the release on the pointer loaded
1928 // from the alloca. The optimizer will through the provenance analysis
1929 // realize that the two are related, but since we only require KnownSafe in
1930 // one direction, will match the inner retain on the original pointer with
1931 // the guard release on the original pointer. This is fixed by ensuring that
1932 // in the presense of allocas we only unconditionally remove pointers if
1933 // both our retain and our release are KnownSafe.
1934 if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
1935 if (AreAnyUnderlyingObjectsAnAlloca(SI->getPointerOperand())) {
1936 BBState::ptr_iterator I = MyStates.findPtrBottomUpState(
1937 StripPointerCastsAndObjCCalls(SI->getValueOperand()));
1938 if (I != MyStates.bottom_up_ptr_end())
1939 MultiOwnersSet.insert(I->first);
1947 // Consider any other possible effects of this instruction on each
1948 // pointer being tracked.
1949 for (BBState::ptr_iterator MI = MyStates.bottom_up_ptr_begin(),
1950 ME = MyStates.bottom_up_ptr_end(); MI != ME; ++MI) {
1951 const Value *Ptr = MI->first;
1953 continue; // Handled above.
1954 PtrState &S = MI->second;
1955 Sequence Seq = S.GetSeq();
1957 // Check for possible releases.
1958 if (CanAlterRefCount(Inst, Ptr, PA, Class)) {
1959 DEBUG(dbgs() << "CanAlterRefCount: Seq: " << Seq << "; " << *Ptr
1961 S.ClearKnownPositiveRefCount();
1964 S.SetSeq(S_CanRelease);
1965 ANNOTATE_BOTTOMUP(Inst, Ptr, Seq, S.GetSeq());
1969 case S_MovableRelease:
1974 llvm_unreachable("bottom-up pointer in retain state!");
1978 // Check for possible direct uses.
1981 case S_MovableRelease:
1982 if (CanUse(Inst, Ptr, PA, Class)) {
1983 DEBUG(dbgs() << "CanUse: Seq: " << Seq << "; " << *Ptr
1985 assert(!S.HasReverseInsertPts());
1986 // If this is an invoke instruction, we're scanning it as part of
1987 // one of its successor blocks, since we can't insert code after it
1988 // in its own block, and we don't want to split critical edges.
1989 if (isa<InvokeInst>(Inst))
1990 S.InsertReverseInsertPt(BB->getFirstInsertionPt());
1992 S.InsertReverseInsertPt(llvm::next(BasicBlock::iterator(Inst)));
1994 ANNOTATE_BOTTOMUP(Inst, Ptr, Seq, S_Use);
1995 } else if (Seq == S_Release && IsUser(Class)) {
1996 DEBUG(dbgs() << "PreciseReleaseUse: Seq: " << Seq << "; " << *Ptr
1998 // Non-movable releases depend on any possible objc pointer use.
2000 ANNOTATE_BOTTOMUP(Inst, Ptr, S_Release, S_Stop);
2001 assert(!S.HasReverseInsertPts());
2002 // As above; handle invoke specially.
2003 if (isa<InvokeInst>(Inst))
2004 S.InsertReverseInsertPt(BB->getFirstInsertionPt());
2006 S.InsertReverseInsertPt(llvm::next(BasicBlock::iterator(Inst)));
2010 if (CanUse(Inst, Ptr, PA, Class)) {
2011 DEBUG(dbgs() << "PreciseStopUse: Seq: " << Seq << "; " << *Ptr
2014 ANNOTATE_BOTTOMUP(Inst, Ptr, Seq, S_Use);
2022 llvm_unreachable("bottom-up pointer in retain state!");
2026 return NestingDetected;
2030 ObjCARCOpt::VisitBottomUp(BasicBlock *BB,
2031 DenseMap<const BasicBlock *, BBState> &BBStates,
2032 MapVector<Value *, RRInfo> &Retains) {
2034 DEBUG(dbgs() << "\n== ObjCARCOpt::VisitBottomUp ==\n");
2036 bool NestingDetected = false;
2037 BBState &MyStates = BBStates[BB];
2039 // Merge the states from each successor to compute the initial state
2040 // for the current block.
2041 BBState::edge_iterator SI(MyStates.succ_begin()),
2042 SE(MyStates.succ_end());
2044 const BasicBlock *Succ = *SI;
2045 DenseMap<const BasicBlock *, BBState>::iterator I = BBStates.find(Succ);
2046 assert(I != BBStates.end());
2047 MyStates.InitFromSucc(I->second);
2049 for (; SI != SE; ++SI) {
2051 I = BBStates.find(Succ);
2052 assert(I != BBStates.end());
2053 MyStates.MergeSucc(I->second);
2057 // If ARC Annotations are enabled, output the current state of pointers at the
2058 // bottom of the basic block.
2059 ANNOTATE_BOTTOMUP_BBEND(MyStates, BB);
2061 // Visit all the instructions, bottom-up.
2062 for (BasicBlock::iterator I = BB->end(), E = BB->begin(); I != E; --I) {
2063 Instruction *Inst = llvm::prior(I);
2065 // Invoke instructions are visited as part of their successors (below).
2066 if (isa<InvokeInst>(Inst))
2069 DEBUG(dbgs() << "Visiting " << *Inst << "\n");
2071 NestingDetected |= VisitInstructionBottomUp(Inst, BB, Retains, MyStates);
2074 // If there's a predecessor with an invoke, visit the invoke as if it were
2075 // part of this block, since we can't insert code after an invoke in its own
2076 // block, and we don't want to split critical edges.
2077 for (BBState::edge_iterator PI(MyStates.pred_begin()),
2078 PE(MyStates.pred_end()); PI != PE; ++PI) {
2079 BasicBlock *Pred = *PI;
2080 if (InvokeInst *II = dyn_cast<InvokeInst>(&Pred->back()))
2081 NestingDetected |= VisitInstructionBottomUp(II, BB, Retains, MyStates);
2084 // If ARC Annotations are enabled, output the current state of pointers at the
2085 // top of the basic block.
2086 ANNOTATE_BOTTOMUP_BBSTART(MyStates, BB);
2088 return NestingDetected;
2092 ObjCARCOpt::VisitInstructionTopDown(Instruction *Inst,
2093 DenseMap<Value *, RRInfo> &Releases,
2094 BBState &MyStates) {
2095 bool NestingDetected = false;
2096 InstructionClass Class = GetInstructionClass(Inst);
2097 const Value *Arg = 0;
2100 case IC_RetainBlock:
2101 // In OptimizeIndividualCalls, we have strength reduced all optimizable
2102 // objc_retainBlocks to objc_retains. Thus at this point any
2103 // objc_retainBlocks that we see are not optimizable.
2107 Arg = GetObjCArg(Inst);
2109 PtrState &S = MyStates.getPtrTopDownState(Arg);
2111 // Don't do retain+release tracking for IC_RetainRV, because it's
2112 // better to let it remain as the first instruction after a call.
2113 if (Class != IC_RetainRV) {
2114 // If we see two retains in a row on the same pointer. If so, make
2115 // a note, and we'll cicle back to revisit it after we've
2116 // hopefully eliminated the second retain, which may allow us to
2117 // eliminate the first retain too.
2118 // Theoretically we could implement removal of nested retain+release
2119 // pairs by making PtrState hold a stack of states, but this is
2120 // simple and avoids adding overhead for the non-nested case.
2121 if (S.GetSeq() == S_Retain)
2122 NestingDetected = true;
2124 ANNOTATE_TOPDOWN(Inst, Arg, S.GetSeq(), S_Retain);
2125 S.ResetSequenceProgress(S_Retain);
2126 S.SetKnownSafe(S.HasKnownPositiveRefCount());
2130 S.SetKnownPositiveRefCount();
2132 // A retain can be a potential use; procede to the generic checking
2137 Arg = GetObjCArg(Inst);
2139 PtrState &S = MyStates.getPtrTopDownState(Arg);
2140 S.ClearKnownPositiveRefCount();
2142 Sequence OldSeq = S.GetSeq();
2144 MDNode *ReleaseMetadata = Inst->getMetadata(ImpreciseReleaseMDKind);
2149 if (OldSeq == S_Retain || ReleaseMetadata != 0)
2150 S.ClearReverseInsertPts();
2153 S.SetReleaseMetadata(ReleaseMetadata);
2154 S.SetTailCallRelease(cast<CallInst>(Inst)->isTailCall());
2155 Releases[Inst] = S.GetRRInfo();
2156 ANNOTATE_TOPDOWN(Inst, Arg, S.GetSeq(), S_None);
2157 S.ClearSequenceProgress();
2163 case S_MovableRelease:
2164 llvm_unreachable("top-down pointer in release state!");
2168 case IC_AutoreleasepoolPop:
2169 // Conservatively, clear MyStates for all known pointers.
2170 MyStates.clearTopDownPointers();
2171 return NestingDetected;
2172 case IC_AutoreleasepoolPush:
2174 // These are irrelevant.
2175 return NestingDetected;
2180 // Consider any other possible effects of this instruction on each
2181 // pointer being tracked.
2182 for (BBState::ptr_iterator MI = MyStates.top_down_ptr_begin(),
2183 ME = MyStates.top_down_ptr_end(); MI != ME; ++MI) {
2184 const Value *Ptr = MI->first;
2186 continue; // Handled above.
2187 PtrState &S = MI->second;
2188 Sequence Seq = S.GetSeq();
2190 // Check for possible releases.
2191 if (CanAlterRefCount(Inst, Ptr, PA, Class)) {
2192 DEBUG(dbgs() << "CanAlterRefCount: Seq: " << Seq << "; " << *Ptr
2194 S.ClearKnownPositiveRefCount();
2197 S.SetSeq(S_CanRelease);
2198 ANNOTATE_TOPDOWN(Inst, Ptr, Seq, S_CanRelease);
2199 assert(!S.HasReverseInsertPts());
2200 S.InsertReverseInsertPt(Inst);
2202 // One call can't cause a transition from S_Retain to S_CanRelease
2203 // and S_CanRelease to S_Use. If we've made the first transition,
2212 case S_MovableRelease:
2213 llvm_unreachable("top-down pointer in release state!");
2217 // Check for possible direct uses.
2220 if (CanUse(Inst, Ptr, PA, Class)) {
2221 DEBUG(dbgs() << "CanUse: Seq: " << Seq << "; " << *Ptr
2224 ANNOTATE_TOPDOWN(Inst, Ptr, Seq, S_Use);
2233 case S_MovableRelease:
2234 llvm_unreachable("top-down pointer in release state!");
2238 return NestingDetected;
2242 ObjCARCOpt::VisitTopDown(BasicBlock *BB,
2243 DenseMap<const BasicBlock *, BBState> &BBStates,
2244 DenseMap<Value *, RRInfo> &Releases) {
2245 DEBUG(dbgs() << "\n== ObjCARCOpt::VisitTopDown ==\n");
2246 bool NestingDetected = false;
2247 BBState &MyStates = BBStates[BB];
2249 // Merge the states from each predecessor to compute the initial state
2250 // for the current block.
2251 BBState::edge_iterator PI(MyStates.pred_begin()),
2252 PE(MyStates.pred_end());
2254 const BasicBlock *Pred = *PI;
2255 DenseMap<const BasicBlock *, BBState>::iterator I = BBStates.find(Pred);
2256 assert(I != BBStates.end());
2257 MyStates.InitFromPred(I->second);
2259 for (; PI != PE; ++PI) {
2261 I = BBStates.find(Pred);
2262 assert(I != BBStates.end());
2263 MyStates.MergePred(I->second);
2267 // If ARC Annotations are enabled, output the current state of pointers at the
2268 // top of the basic block.
2269 ANNOTATE_TOPDOWN_BBSTART(MyStates, BB);
2271 // Visit all the instructions, top-down.
2272 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
2273 Instruction *Inst = I;
2275 DEBUG(dbgs() << "Visiting " << *Inst << "\n");
2277 NestingDetected |= VisitInstructionTopDown(Inst, Releases, MyStates);
2280 // If ARC Annotations are enabled, output the current state of pointers at the
2281 // bottom of the basic block.
2282 ANNOTATE_TOPDOWN_BBEND(MyStates, BB);
2284 #ifdef ARC_ANNOTATIONS
2285 if (!(EnableARCAnnotations && DisableCheckForCFGHazards))
2287 CheckForCFGHazards(BB, BBStates, MyStates);
2288 return NestingDetected;
2292 ComputePostOrders(Function &F,
2293 SmallVectorImpl<BasicBlock *> &PostOrder,
2294 SmallVectorImpl<BasicBlock *> &ReverseCFGPostOrder,
2295 unsigned NoObjCARCExceptionsMDKind,
2296 DenseMap<const BasicBlock *, BBState> &BBStates) {
2297 /// The visited set, for doing DFS walks.
2298 SmallPtrSet<BasicBlock *, 16> Visited;
2300 // Do DFS, computing the PostOrder.
2301 SmallPtrSet<BasicBlock *, 16> OnStack;
2302 SmallVector<std::pair<BasicBlock *, succ_iterator>, 16> SuccStack;
2304 // Functions always have exactly one entry block, and we don't have
2305 // any other block that we treat like an entry block.
2306 BasicBlock *EntryBB = &F.getEntryBlock();
2307 BBState &MyStates = BBStates[EntryBB];
2308 MyStates.SetAsEntry();
2309 TerminatorInst *EntryTI = cast<TerminatorInst>(&EntryBB->back());
2310 SuccStack.push_back(std::make_pair(EntryBB, succ_iterator(EntryTI)));
2311 Visited.insert(EntryBB);
2312 OnStack.insert(EntryBB);
2315 BasicBlock *CurrBB = SuccStack.back().first;
2316 TerminatorInst *TI = cast<TerminatorInst>(&CurrBB->back());
2317 succ_iterator SE(TI, false);
2319 while (SuccStack.back().second != SE) {
2320 BasicBlock *SuccBB = *SuccStack.back().second++;
2321 if (Visited.insert(SuccBB)) {
2322 TerminatorInst *TI = cast<TerminatorInst>(&SuccBB->back());
2323 SuccStack.push_back(std::make_pair(SuccBB, succ_iterator(TI)));
2324 BBStates[CurrBB].addSucc(SuccBB);
2325 BBState &SuccStates = BBStates[SuccBB];
2326 SuccStates.addPred(CurrBB);
2327 OnStack.insert(SuccBB);
2331 if (!OnStack.count(SuccBB)) {
2332 BBStates[CurrBB].addSucc(SuccBB);
2333 BBStates[SuccBB].addPred(CurrBB);
2336 OnStack.erase(CurrBB);
2337 PostOrder.push_back(CurrBB);
2338 SuccStack.pop_back();
2339 } while (!SuccStack.empty());
2343 // Do reverse-CFG DFS, computing the reverse-CFG PostOrder.
2344 // Functions may have many exits, and there also blocks which we treat
2345 // as exits due to ignored edges.
2346 SmallVector<std::pair<BasicBlock *, BBState::edge_iterator>, 16> PredStack;
2347 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
2348 BasicBlock *ExitBB = I;
2349 BBState &MyStates = BBStates[ExitBB];
2350 if (!MyStates.isExit())
2353 MyStates.SetAsExit();
2355 PredStack.push_back(std::make_pair(ExitBB, MyStates.pred_begin()));
2356 Visited.insert(ExitBB);
2357 while (!PredStack.empty()) {
2358 reverse_dfs_next_succ:
2359 BBState::edge_iterator PE = BBStates[PredStack.back().first].pred_end();
2360 while (PredStack.back().second != PE) {
2361 BasicBlock *BB = *PredStack.back().second++;
2362 if (Visited.insert(BB)) {
2363 PredStack.push_back(std::make_pair(BB, BBStates[BB].pred_begin()));
2364 goto reverse_dfs_next_succ;
2367 ReverseCFGPostOrder.push_back(PredStack.pop_back_val().first);
2372 // Visit the function both top-down and bottom-up.
2374 ObjCARCOpt::Visit(Function &F,
2375 DenseMap<const BasicBlock *, BBState> &BBStates,
2376 MapVector<Value *, RRInfo> &Retains,
2377 DenseMap<Value *, RRInfo> &Releases) {
2379 // Use reverse-postorder traversals, because we magically know that loops
2380 // will be well behaved, i.e. they won't repeatedly call retain on a single
2381 // pointer without doing a release. We can't use the ReversePostOrderTraversal
2382 // class here because we want the reverse-CFG postorder to consider each
2383 // function exit point, and we want to ignore selected cycle edges.
2384 SmallVector<BasicBlock *, 16> PostOrder;
2385 SmallVector<BasicBlock *, 16> ReverseCFGPostOrder;
2386 ComputePostOrders(F, PostOrder, ReverseCFGPostOrder,
2387 NoObjCARCExceptionsMDKind,
2390 // Use reverse-postorder on the reverse CFG for bottom-up.
2391 bool BottomUpNestingDetected = false;
2392 for (SmallVectorImpl<BasicBlock *>::const_reverse_iterator I =
2393 ReverseCFGPostOrder.rbegin(), E = ReverseCFGPostOrder.rend();
2395 BottomUpNestingDetected |= VisitBottomUp(*I, BBStates, Retains);
2397 // Use reverse-postorder for top-down.
2398 bool TopDownNestingDetected = false;
2399 for (SmallVectorImpl<BasicBlock *>::const_reverse_iterator I =
2400 PostOrder.rbegin(), E = PostOrder.rend();
2402 TopDownNestingDetected |= VisitTopDown(*I, BBStates, Releases);
2404 return TopDownNestingDetected && BottomUpNestingDetected;
2407 /// Move the calls in RetainsToMove and ReleasesToMove.
2408 void ObjCARCOpt::MoveCalls(Value *Arg,
2409 RRInfo &RetainsToMove,
2410 RRInfo &ReleasesToMove,
2411 MapVector<Value *, RRInfo> &Retains,
2412 DenseMap<Value *, RRInfo> &Releases,
2413 SmallVectorImpl<Instruction *> &DeadInsts,
2415 Type *ArgTy = Arg->getType();
2416 Type *ParamTy = PointerType::getUnqual(Type::getInt8Ty(ArgTy->getContext()));
2418 DEBUG(dbgs() << "== ObjCARCOpt::MoveCalls ==\n");
2420 // Insert the new retain and release calls.
2421 for (SmallPtrSet<Instruction *, 2>::const_iterator
2422 PI = ReleasesToMove.ReverseInsertPts.begin(),
2423 PE = ReleasesToMove.ReverseInsertPts.end(); PI != PE; ++PI) {
2424 Instruction *InsertPt = *PI;
2425 Value *MyArg = ArgTy == ParamTy ? Arg :
2426 new BitCastInst(Arg, ParamTy, "", InsertPt);
2427 Constant *Decl = EP.get(ARCRuntimeEntryPoints::EPT_Retain);
2428 CallInst *Call = CallInst::Create(Decl, MyArg, "", InsertPt);
2429 Call->setDoesNotThrow();
2430 Call->setTailCall();
2432 DEBUG(dbgs() << "Inserting new Retain: " << *Call << "\n"
2433 "At insertion point: " << *InsertPt << "\n");
2435 for (SmallPtrSet<Instruction *, 2>::const_iterator
2436 PI = RetainsToMove.ReverseInsertPts.begin(),
2437 PE = RetainsToMove.ReverseInsertPts.end(); PI != PE; ++PI) {
2438 Instruction *InsertPt = *PI;
2439 Value *MyArg = ArgTy == ParamTy ? Arg :
2440 new BitCastInst(Arg, ParamTy, "", InsertPt);
2441 Constant *Decl = EP.get(ARCRuntimeEntryPoints::EPT_Release);
2442 CallInst *Call = CallInst::Create(Decl, MyArg, "", InsertPt);
2443 // Attach a clang.imprecise_release metadata tag, if appropriate.
2444 if (MDNode *M = ReleasesToMove.ReleaseMetadata)
2445 Call->setMetadata(ImpreciseReleaseMDKind, M);
2446 Call->setDoesNotThrow();
2447 if (ReleasesToMove.IsTailCallRelease)
2448 Call->setTailCall();
2450 DEBUG(dbgs() << "Inserting new Release: " << *Call << "\n"
2451 "At insertion point: " << *InsertPt << "\n");
2454 // Delete the original retain and release calls.
2455 for (SmallPtrSet<Instruction *, 2>::const_iterator
2456 AI = RetainsToMove.Calls.begin(),
2457 AE = RetainsToMove.Calls.end(); AI != AE; ++AI) {
2458 Instruction *OrigRetain = *AI;
2459 Retains.blot(OrigRetain);
2460 DeadInsts.push_back(OrigRetain);
2461 DEBUG(dbgs() << "Deleting retain: " << *OrigRetain << "\n");
2463 for (SmallPtrSet<Instruction *, 2>::const_iterator
2464 AI = ReleasesToMove.Calls.begin(),
2465 AE = ReleasesToMove.Calls.end(); AI != AE; ++AI) {
2466 Instruction *OrigRelease = *AI;
2467 Releases.erase(OrigRelease);
2468 DeadInsts.push_back(OrigRelease);
2469 DEBUG(dbgs() << "Deleting release: " << *OrigRelease << "\n");
2475 ObjCARCOpt::ConnectTDBUTraversals(DenseMap<const BasicBlock *, BBState>
2477 MapVector<Value *, RRInfo> &Retains,
2478 DenseMap<Value *, RRInfo> &Releases,
2480 SmallVectorImpl<Instruction *> &NewRetains,
2481 SmallVectorImpl<Instruction *> &NewReleases,
2482 SmallVectorImpl<Instruction *> &DeadInsts,
2483 RRInfo &RetainsToMove,
2484 RRInfo &ReleasesToMove,
2487 bool &AnyPairsCompletelyEliminated) {
2488 // If a pair happens in a region where it is known that the reference count
2489 // is already incremented, we can similarly ignore possible decrements unless
2490 // we are dealing with a retainable object with multiple provenance sources.
2491 bool KnownSafeTD = true, KnownSafeBU = true;
2492 bool MultipleOwners = false;
2493 bool CFGHazardAfflicted = false;
2495 // Connect the dots between the top-down-collected RetainsToMove and
2496 // bottom-up-collected ReleasesToMove to form sets of related calls.
2497 // This is an iterative process so that we connect multiple releases
2498 // to multiple retains if needed.
2499 unsigned OldDelta = 0;
2500 unsigned NewDelta = 0;
2501 unsigned OldCount = 0;
2502 unsigned NewCount = 0;
2503 bool FirstRelease = true;
2505 for (SmallVectorImpl<Instruction *>::const_iterator
2506 NI = NewRetains.begin(), NE = NewRetains.end(); NI != NE; ++NI) {
2507 Instruction *NewRetain = *NI;
2508 MapVector<Value *, RRInfo>::const_iterator It = Retains.find(NewRetain);
2509 assert(It != Retains.end());
2510 const RRInfo &NewRetainRRI = It->second;
2511 KnownSafeTD &= NewRetainRRI.KnownSafe;
2513 MultipleOwners || MultiOwnersSet.count(GetObjCArg(NewRetain));
2514 for (SmallPtrSet<Instruction *, 2>::const_iterator
2515 LI = NewRetainRRI.Calls.begin(),
2516 LE = NewRetainRRI.Calls.end(); LI != LE; ++LI) {
2517 Instruction *NewRetainRelease = *LI;
2518 DenseMap<Value *, RRInfo>::const_iterator Jt =
2519 Releases.find(NewRetainRelease);
2520 if (Jt == Releases.end())
2522 const RRInfo &NewRetainReleaseRRI = Jt->second;
2523 assert(NewRetainReleaseRRI.Calls.count(NewRetain));
2524 if (ReleasesToMove.Calls.insert(NewRetainRelease)) {
2526 // If we overflow when we compute the path count, don't remove/move
2528 const BBState &NRRBBState = BBStates[NewRetainRelease->getParent()];
2530 if (NRRBBState.GetAllPathCountWithOverflow(PathCount))
2532 OldDelta -= PathCount;
2534 // Merge the ReleaseMetadata and IsTailCallRelease values.
2536 ReleasesToMove.ReleaseMetadata =
2537 NewRetainReleaseRRI.ReleaseMetadata;
2538 ReleasesToMove.IsTailCallRelease =
2539 NewRetainReleaseRRI.IsTailCallRelease;
2540 FirstRelease = false;
2542 if (ReleasesToMove.ReleaseMetadata !=
2543 NewRetainReleaseRRI.ReleaseMetadata)
2544 ReleasesToMove.ReleaseMetadata = 0;
2545 if (ReleasesToMove.IsTailCallRelease !=
2546 NewRetainReleaseRRI.IsTailCallRelease)
2547 ReleasesToMove.IsTailCallRelease = false;
2550 // Collect the optimal insertion points.
2552 for (SmallPtrSet<Instruction *, 2>::const_iterator
2553 RI = NewRetainReleaseRRI.ReverseInsertPts.begin(),
2554 RE = NewRetainReleaseRRI.ReverseInsertPts.end();
2556 Instruction *RIP = *RI;
2557 if (ReleasesToMove.ReverseInsertPts.insert(RIP)) {
2558 // If we overflow when we compute the path count, don't
2559 // remove/move anything.
2560 const BBState &RIPBBState = BBStates[RIP->getParent()];
2561 if (RIPBBState.GetAllPathCountWithOverflow(PathCount))
2563 NewDelta -= PathCount;
2566 NewReleases.push_back(NewRetainRelease);
2571 if (NewReleases.empty()) break;
2573 // Back the other way.
2574 for (SmallVectorImpl<Instruction *>::const_iterator
2575 NI = NewReleases.begin(), NE = NewReleases.end(); NI != NE; ++NI) {
2576 Instruction *NewRelease = *NI;
2577 DenseMap<Value *, RRInfo>::const_iterator It =
2578 Releases.find(NewRelease);
2579 assert(It != Releases.end());
2580 const RRInfo &NewReleaseRRI = It->second;
2581 KnownSafeBU &= NewReleaseRRI.KnownSafe;
2582 CFGHazardAfflicted |= NewReleaseRRI.CFGHazardAfflicted;
2583 for (SmallPtrSet<Instruction *, 2>::const_iterator
2584 LI = NewReleaseRRI.Calls.begin(),
2585 LE = NewReleaseRRI.Calls.end(); LI != LE; ++LI) {
2586 Instruction *NewReleaseRetain = *LI;
2587 MapVector<Value *, RRInfo>::const_iterator Jt =
2588 Retains.find(NewReleaseRetain);
2589 if (Jt == Retains.end())
2591 const RRInfo &NewReleaseRetainRRI = Jt->second;
2592 assert(NewReleaseRetainRRI.Calls.count(NewRelease));
2593 if (RetainsToMove.Calls.insert(NewReleaseRetain)) {
2595 // If we overflow when we compute the path count, don't remove/move
2597 const BBState &NRRBBState = BBStates[NewReleaseRetain->getParent()];
2599 if (NRRBBState.GetAllPathCountWithOverflow(PathCount))
2601 OldDelta += PathCount;
2602 OldCount += PathCount;
2604 // Collect the optimal insertion points.
2606 for (SmallPtrSet<Instruction *, 2>::const_iterator
2607 RI = NewReleaseRetainRRI.ReverseInsertPts.begin(),
2608 RE = NewReleaseRetainRRI.ReverseInsertPts.end();
2610 Instruction *RIP = *RI;
2611 if (RetainsToMove.ReverseInsertPts.insert(RIP)) {
2612 // If we overflow when we compute the path count, don't
2613 // remove/move anything.
2614 const BBState &RIPBBState = BBStates[RIP->getParent()];
2615 if (RIPBBState.GetAllPathCountWithOverflow(PathCount))
2617 NewDelta += PathCount;
2618 NewCount += PathCount;
2621 NewRetains.push_back(NewReleaseRetain);
2625 NewReleases.clear();
2626 if (NewRetains.empty()) break;
2629 // If the pointer is known incremented in 1 direction and we do not have
2630 // MultipleOwners, we can safely remove the retain/releases. Otherwise we need
2631 // to be known safe in both directions.
2632 bool UnconditionallySafe = (KnownSafeTD && KnownSafeBU) ||
2633 ((KnownSafeTD || KnownSafeBU) && !MultipleOwners);
2634 if (UnconditionallySafe) {
2635 RetainsToMove.ReverseInsertPts.clear();
2636 ReleasesToMove.ReverseInsertPts.clear();
2639 // Determine whether the new insertion points we computed preserve the
2640 // balance of retain and release calls through the program.
2641 // TODO: If the fully aggressive solution isn't valid, try to find a
2642 // less aggressive solution which is.
2646 // At this point, we are not going to remove any RR pairs, but we still are
2647 // able to move RR pairs. If one of our pointers is afflicted with
2648 // CFGHazards, we cannot perform such code motion so exit early.
2649 const bool WillPerformCodeMotion = RetainsToMove.ReverseInsertPts.size() ||
2650 ReleasesToMove.ReverseInsertPts.size();
2651 if (CFGHazardAfflicted && WillPerformCodeMotion)
2655 // Determine whether the original call points are balanced in the retain and
2656 // release calls through the program. If not, conservatively don't touch
2658 // TODO: It's theoretically possible to do code motion in this case, as
2659 // long as the existing imbalances are maintained.
2663 #ifdef ARC_ANNOTATIONS
2664 // Do not move calls if ARC annotations are requested.
2665 if (EnableARCAnnotations)
2667 #endif // ARC_ANNOTATIONS
2670 assert(OldCount != 0 && "Unreachable code?");
2671 NumRRs += OldCount - NewCount;
2672 // Set to true if we completely removed any RR pairs.
2673 AnyPairsCompletelyEliminated = NewCount == 0;
2675 // We can move calls!
2679 /// Identify pairings between the retains and releases, and delete and/or move
2682 ObjCARCOpt::PerformCodePlacement(DenseMap<const BasicBlock *, BBState>
2684 MapVector<Value *, RRInfo> &Retains,
2685 DenseMap<Value *, RRInfo> &Releases,
2687 DEBUG(dbgs() << "\n== ObjCARCOpt::PerformCodePlacement ==\n");
2689 bool AnyPairsCompletelyEliminated = false;
2690 RRInfo RetainsToMove;
2691 RRInfo ReleasesToMove;
2692 SmallVector<Instruction *, 4> NewRetains;
2693 SmallVector<Instruction *, 4> NewReleases;
2694 SmallVector<Instruction *, 8> DeadInsts;
2696 // Visit each retain.
2697 for (MapVector<Value *, RRInfo>::const_iterator I = Retains.begin(),
2698 E = Retains.end(); I != E; ++I) {
2699 Value *V = I->first;
2700 if (!V) continue; // blotted
2702 Instruction *Retain = cast<Instruction>(V);
2704 DEBUG(dbgs() << "Visiting: " << *Retain << "\n");
2706 Value *Arg = GetObjCArg(Retain);
2708 // If the object being released is in static or stack storage, we know it's
2709 // not being managed by ObjC reference counting, so we can delete pairs
2710 // regardless of what possible decrements or uses lie between them.
2711 bool KnownSafe = isa<Constant>(Arg) || isa<AllocaInst>(Arg);
2713 // A constant pointer can't be pointing to an object on the heap. It may
2714 // be reference-counted, but it won't be deleted.
2715 if (const LoadInst *LI = dyn_cast<LoadInst>(Arg))
2716 if (const GlobalVariable *GV =
2717 dyn_cast<GlobalVariable>(
2718 StripPointerCastsAndObjCCalls(LI->getPointerOperand())))
2719 if (GV->isConstant())
2722 // Connect the dots between the top-down-collected RetainsToMove and
2723 // bottom-up-collected ReleasesToMove to form sets of related calls.
2724 NewRetains.push_back(Retain);
2725 bool PerformMoveCalls =
2726 ConnectTDBUTraversals(BBStates, Retains, Releases, M, NewRetains,
2727 NewReleases, DeadInsts, RetainsToMove,
2728 ReleasesToMove, Arg, KnownSafe,
2729 AnyPairsCompletelyEliminated);
2731 if (PerformMoveCalls) {
2732 // Ok, everything checks out and we're all set. Let's move/delete some
2734 MoveCalls(Arg, RetainsToMove, ReleasesToMove,
2735 Retains, Releases, DeadInsts, M);
2738 // Clean up state for next retain.
2739 NewReleases.clear();
2741 RetainsToMove.clear();
2742 ReleasesToMove.clear();
2745 // Now that we're done moving everything, we can delete the newly dead
2746 // instructions, as we no longer need them as insert points.
2747 while (!DeadInsts.empty())
2748 EraseInstruction(DeadInsts.pop_back_val());
2750 return AnyPairsCompletelyEliminated;
2753 /// Weak pointer optimizations.
2754 void ObjCARCOpt::OptimizeWeakCalls(Function &F) {
2755 DEBUG(dbgs() << "\n== ObjCARCOpt::OptimizeWeakCalls ==\n");
2757 // First, do memdep-style RLE and S2L optimizations. We can't use memdep
2758 // itself because it uses AliasAnalysis and we need to do provenance
2760 for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
2761 Instruction *Inst = &*I++;
2763 DEBUG(dbgs() << "Visiting: " << *Inst << "\n");
2765 InstructionClass Class = GetBasicInstructionClass(Inst);
2766 if (Class != IC_LoadWeak && Class != IC_LoadWeakRetained)
2769 // Delete objc_loadWeak calls with no users.
2770 if (Class == IC_LoadWeak && Inst->use_empty()) {
2771 Inst->eraseFromParent();
2775 // TODO: For now, just look for an earlier available version of this value
2776 // within the same block. Theoretically, we could do memdep-style non-local
2777 // analysis too, but that would want caching. A better approach would be to
2778 // use the technique that EarlyCSE uses.
2779 inst_iterator Current = llvm::prior(I);
2780 BasicBlock *CurrentBB = Current.getBasicBlockIterator();
2781 for (BasicBlock::iterator B = CurrentBB->begin(),
2782 J = Current.getInstructionIterator();
2784 Instruction *EarlierInst = &*llvm::prior(J);
2785 InstructionClass EarlierClass = GetInstructionClass(EarlierInst);
2786 switch (EarlierClass) {
2788 case IC_LoadWeakRetained: {
2789 // If this is loading from the same pointer, replace this load's value
2791 CallInst *Call = cast<CallInst>(Inst);
2792 CallInst *EarlierCall = cast<CallInst>(EarlierInst);
2793 Value *Arg = Call->getArgOperand(0);
2794 Value *EarlierArg = EarlierCall->getArgOperand(0);
2795 switch (PA.getAA()->alias(Arg, EarlierArg)) {
2796 case AliasAnalysis::MustAlias:
2798 // If the load has a builtin retain, insert a plain retain for it.
2799 if (Class == IC_LoadWeakRetained) {
2800 Constant *Decl = EP.get(ARCRuntimeEntryPoints::EPT_Retain);
2801 CallInst *CI = CallInst::Create(Decl, EarlierCall, "", Call);
2804 // Zap the fully redundant load.
2805 Call->replaceAllUsesWith(EarlierCall);
2806 Call->eraseFromParent();
2808 case AliasAnalysis::MayAlias:
2809 case AliasAnalysis::PartialAlias:
2811 case AliasAnalysis::NoAlias:
2818 // If this is storing to the same pointer and has the same size etc.
2819 // replace this load's value with the stored value.
2820 CallInst *Call = cast<CallInst>(Inst);
2821 CallInst *EarlierCall = cast<CallInst>(EarlierInst);
2822 Value *Arg = Call->getArgOperand(0);
2823 Value *EarlierArg = EarlierCall->getArgOperand(0);
2824 switch (PA.getAA()->alias(Arg, EarlierArg)) {
2825 case AliasAnalysis::MustAlias:
2827 // If the load has a builtin retain, insert a plain retain for it.
2828 if (Class == IC_LoadWeakRetained) {
2829 Constant *Decl = EP.get(ARCRuntimeEntryPoints::EPT_Retain);
2830 CallInst *CI = CallInst::Create(Decl, EarlierCall, "", Call);
2833 // Zap the fully redundant load.
2834 Call->replaceAllUsesWith(EarlierCall->getArgOperand(1));
2835 Call->eraseFromParent();
2837 case AliasAnalysis::MayAlias:
2838 case AliasAnalysis::PartialAlias:
2840 case AliasAnalysis::NoAlias:
2847 // TOOD: Grab the copied value.
2849 case IC_AutoreleasepoolPush:
2851 case IC_IntrinsicUser:
2853 // Weak pointers are only modified through the weak entry points
2854 // (and arbitrary calls, which could call the weak entry points).
2857 // Anything else could modify the weak pointer.
2864 // Then, for each destroyWeak with an alloca operand, check to see if
2865 // the alloca and all its users can be zapped.
2866 for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
2867 Instruction *Inst = &*I++;
2868 InstructionClass Class = GetBasicInstructionClass(Inst);
2869 if (Class != IC_DestroyWeak)
2872 CallInst *Call = cast<CallInst>(Inst);
2873 Value *Arg = Call->getArgOperand(0);
2874 if (AllocaInst *Alloca = dyn_cast<AllocaInst>(Arg)) {
2875 for (Value::use_iterator UI = Alloca->use_begin(),
2876 UE = Alloca->use_end(); UI != UE; ++UI) {
2877 const Instruction *UserInst = cast<Instruction>(*UI);
2878 switch (GetBasicInstructionClass(UserInst)) {
2881 case IC_DestroyWeak:
2888 for (Value::use_iterator UI = Alloca->use_begin(),
2889 UE = Alloca->use_end(); UI != UE; ) {
2890 CallInst *UserInst = cast<CallInst>(*UI++);
2891 switch (GetBasicInstructionClass(UserInst)) {
2894 // These functions return their second argument.
2895 UserInst->replaceAllUsesWith(UserInst->getArgOperand(1));
2897 case IC_DestroyWeak:
2901 llvm_unreachable("alloca really is used!");
2903 UserInst->eraseFromParent();
2905 Alloca->eraseFromParent();
2911 /// Identify program paths which execute sequences of retains and releases which
2912 /// can be eliminated.
2913 bool ObjCARCOpt::OptimizeSequences(Function &F) {
2914 // Releases, Retains - These are used to store the results of the main flow
2915 // analysis. These use Value* as the key instead of Instruction* so that the
2916 // map stays valid when we get around to rewriting code and calls get
2917 // replaced by arguments.
2918 DenseMap<Value *, RRInfo> Releases;
2919 MapVector<Value *, RRInfo> Retains;
2921 // This is used during the traversal of the function to track the
2922 // states for each identified object at each block.
2923 DenseMap<const BasicBlock *, BBState> BBStates;
2925 // Analyze the CFG of the function, and all instructions.
2926 bool NestingDetected = Visit(F, BBStates, Retains, Releases);
2929 bool AnyPairsCompletelyEliminated = PerformCodePlacement(BBStates, Retains,
2934 MultiOwnersSet.clear();
2936 return AnyPairsCompletelyEliminated && NestingDetected;
2939 /// Check if there is a dependent call earlier that does not have anything in
2940 /// between the Retain and the call that can affect the reference count of their
2941 /// shared pointer argument. Note that Retain need not be in BB.
2943 HasSafePathToPredecessorCall(const Value *Arg, Instruction *Retain,
2944 SmallPtrSet<Instruction *, 4> &DepInsts,
2945 SmallPtrSet<const BasicBlock *, 4> &Visited,
2946 ProvenanceAnalysis &PA) {
2947 FindDependencies(CanChangeRetainCount, Arg, Retain->getParent(), Retain,
2948 DepInsts, Visited, PA);
2949 if (DepInsts.size() != 1)
2953 dyn_cast_or_null<CallInst>(*DepInsts.begin());
2955 // Check that the pointer is the return value of the call.
2956 if (!Call || Arg != Call)
2959 // Check that the call is a regular call.
2960 InstructionClass Class = GetBasicInstructionClass(Call);
2961 if (Class != IC_CallOrUser && Class != IC_Call)
2967 /// Find a dependent retain that precedes the given autorelease for which there
2968 /// is nothing in between the two instructions that can affect the ref count of
2971 FindPredecessorRetainWithSafePath(const Value *Arg, BasicBlock *BB,
2972 Instruction *Autorelease,
2973 SmallPtrSet<Instruction *, 4> &DepInsts,
2974 SmallPtrSet<const BasicBlock *, 4> &Visited,
2975 ProvenanceAnalysis &PA) {
2976 FindDependencies(CanChangeRetainCount, Arg,
2977 BB, Autorelease, DepInsts, Visited, PA);
2978 if (DepInsts.size() != 1)
2982 dyn_cast_or_null<CallInst>(*DepInsts.begin());
2984 // Check that we found a retain with the same argument.
2986 !IsRetain(GetBasicInstructionClass(Retain)) ||
2987 GetObjCArg(Retain) != Arg) {
2994 /// Look for an ``autorelease'' instruction dependent on Arg such that there are
2995 /// no instructions dependent on Arg that need a positive ref count in between
2996 /// the autorelease and the ret.
2998 FindPredecessorAutoreleaseWithSafePath(const Value *Arg, BasicBlock *BB,
3000 SmallPtrSet<Instruction *, 4> &DepInsts,
3001 SmallPtrSet<const BasicBlock *, 4> &V,
3002 ProvenanceAnalysis &PA) {
3003 FindDependencies(NeedsPositiveRetainCount, Arg,
3004 BB, Ret, DepInsts, V, PA);
3005 if (DepInsts.size() != 1)
3008 CallInst *Autorelease =
3009 dyn_cast_or_null<CallInst>(*DepInsts.begin());
3012 InstructionClass AutoreleaseClass = GetBasicInstructionClass(Autorelease);
3013 if (!IsAutorelease(AutoreleaseClass))
3015 if (GetObjCArg(Autorelease) != Arg)
3021 /// Look for this pattern:
3023 /// %call = call i8* @something(...)
3024 /// %2 = call i8* @objc_retain(i8* %call)
3025 /// %3 = call i8* @objc_autorelease(i8* %2)
3028 /// And delete the retain and autorelease.
3029 void ObjCARCOpt::OptimizeReturns(Function &F) {
3030 if (!F.getReturnType()->isPointerTy())
3033 DEBUG(dbgs() << "\n== ObjCARCOpt::OptimizeReturns ==\n");
3035 SmallPtrSet<Instruction *, 4> DependingInstructions;
3036 SmallPtrSet<const BasicBlock *, 4> Visited;
3037 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI) {
3038 BasicBlock *BB = FI;
3039 ReturnInst *Ret = dyn_cast<ReturnInst>(&BB->back());
3041 DEBUG(dbgs() << "Visiting: " << *Ret << "\n");
3046 const Value *Arg = StripPointerCastsAndObjCCalls(Ret->getOperand(0));
3048 // Look for an ``autorelease'' instruction that is a predecessor of Ret and
3049 // dependent on Arg such that there are no instructions dependent on Arg
3050 // that need a positive ref count in between the autorelease and Ret.
3051 CallInst *Autorelease =
3052 FindPredecessorAutoreleaseWithSafePath(Arg, BB, Ret,
3053 DependingInstructions, Visited,
3055 DependingInstructions.clear();
3062 FindPredecessorRetainWithSafePath(Arg, BB, Autorelease,
3063 DependingInstructions, Visited, PA);
3064 DependingInstructions.clear();
3070 // Check that there is nothing that can affect the reference count
3071 // between the retain and the call. Note that Retain need not be in BB.
3072 bool HasSafePathToCall = HasSafePathToPredecessorCall(Arg, Retain,
3073 DependingInstructions,
3075 DependingInstructions.clear();
3078 if (!HasSafePathToCall)
3081 // If so, we can zap the retain and autorelease.
3084 DEBUG(dbgs() << "Erasing: " << *Retain << "\nErasing: "
3085 << *Autorelease << "\n");
3086 EraseInstruction(Retain);
3087 EraseInstruction(Autorelease);
3093 ObjCARCOpt::GatherStatistics(Function &F, bool AfterOptimization) {
3094 llvm::Statistic &NumRetains =
3095 AfterOptimization? NumRetainsAfterOpt : NumRetainsBeforeOpt;
3096 llvm::Statistic &NumReleases =
3097 AfterOptimization? NumReleasesAfterOpt : NumReleasesBeforeOpt;
3099 for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
3100 Instruction *Inst = &*I++;
3101 switch (GetBasicInstructionClass(Inst)) {
3115 bool ObjCARCOpt::doInitialization(Module &M) {
3119 // If nothing in the Module uses ARC, don't do anything.
3120 Run = ModuleHasARC(M);
3124 // Identify the imprecise release metadata kind.
3125 ImpreciseReleaseMDKind =
3126 M.getContext().getMDKindID("clang.imprecise_release");
3127 CopyOnEscapeMDKind =
3128 M.getContext().getMDKindID("clang.arc.copy_on_escape");
3129 NoObjCARCExceptionsMDKind =
3130 M.getContext().getMDKindID("clang.arc.no_objc_arc_exceptions");
3131 #ifdef ARC_ANNOTATIONS
3132 ARCAnnotationBottomUpMDKind =
3133 M.getContext().getMDKindID("llvm.arc.annotation.bottomup");
3134 ARCAnnotationTopDownMDKind =
3135 M.getContext().getMDKindID("llvm.arc.annotation.topdown");
3136 ARCAnnotationProvenanceSourceMDKind =
3137 M.getContext().getMDKindID("llvm.arc.annotation.provenancesource");
3138 #endif // ARC_ANNOTATIONS
3140 // Intuitively, objc_retain and others are nocapture, however in practice
3141 // they are not, because they return their argument value. And objc_release
3142 // calls finalizers which can have arbitrary side effects.
3144 // Initialize our runtime entry point cache.
3150 bool ObjCARCOpt::runOnFunction(Function &F) {
3154 // If nothing in the Module uses ARC, don't do anything.
3160 DEBUG(dbgs() << "<<< ObjCARCOpt: Visiting Function: " << F.getName() << " >>>"
3163 PA.setAA(&getAnalysis<AliasAnalysis>());
3166 if (AreStatisticsEnabled()) {
3167 GatherStatistics(F, false);
3171 // This pass performs several distinct transformations. As a compile-time aid
3172 // when compiling code that isn't ObjC, skip these if the relevant ObjC
3173 // library functions aren't declared.
3175 // Preliminary optimizations. This also computes UsedInThisFunction.
3176 OptimizeIndividualCalls(F);
3178 // Optimizations for weak pointers.
3179 if (UsedInThisFunction & ((1 << IC_LoadWeak) |
3180 (1 << IC_LoadWeakRetained) |
3181 (1 << IC_StoreWeak) |
3182 (1 << IC_InitWeak) |
3183 (1 << IC_CopyWeak) |
3184 (1 << IC_MoveWeak) |
3185 (1 << IC_DestroyWeak)))
3186 OptimizeWeakCalls(F);
3188 // Optimizations for retain+release pairs.
3189 if (UsedInThisFunction & ((1 << IC_Retain) |
3190 (1 << IC_RetainRV) |
3191 (1 << IC_RetainBlock)))
3192 if (UsedInThisFunction & (1 << IC_Release))
3193 // Run OptimizeSequences until it either stops making changes or
3194 // no retain+release pair nesting is detected.
3195 while (OptimizeSequences(F)) {}
3197 // Optimizations if objc_autorelease is used.
3198 if (UsedInThisFunction & ((1 << IC_Autorelease) |
3199 (1 << IC_AutoreleaseRV)))
3202 // Gather statistics after optimization.
3204 if (AreStatisticsEnabled()) {
3205 GatherStatistics(F, true);
3209 DEBUG(dbgs() << "\n");
3214 void ObjCARCOpt::releaseMemory() {