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 /// This is a wrapper around getUnderlyingObjCPtr along the lines of
180 /// GetUnderlyingObjects except that it returns early when it sees the first
182 static inline bool AreAnyUnderlyingObjectsAnAlloca(const Value *V) {
183 SmallPtrSet<const Value *, 4> Visited;
184 SmallVector<const Value *, 4> Worklist;
185 Worklist.push_back(V);
187 const Value *P = Worklist.pop_back_val();
188 P = GetUnderlyingObjCPtr(P);
190 if (isa<AllocaInst>(P))
193 if (!Visited.insert(P))
196 if (const SelectInst *SI = dyn_cast<const SelectInst>(P)) {
197 Worklist.push_back(SI->getTrueValue());
198 Worklist.push_back(SI->getFalseValue());
202 if (const PHINode *PN = dyn_cast<const PHINode>(P)) {
203 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
204 Worklist.push_back(PN->getIncomingValue(i));
207 } while (!Worklist.empty());
215 /// \defgroup ARCOpt ARC Optimization.
218 // TODO: On code like this:
221 // stuff_that_cannot_release()
222 // objc_autorelease(%x)
223 // stuff_that_cannot_release()
225 // stuff_that_cannot_release()
226 // objc_autorelease(%x)
228 // The second retain and autorelease can be deleted.
230 // TODO: It should be possible to delete
231 // objc_autoreleasePoolPush and objc_autoreleasePoolPop
232 // pairs if nothing is actually autoreleased between them. Also, autorelease
233 // calls followed by objc_autoreleasePoolPop calls (perhaps in ObjC++ code
234 // after inlining) can be turned into plain release calls.
236 // TODO: Critical-edge splitting. If the optimial insertion point is
237 // a critical edge, the current algorithm has to fail, because it doesn't
238 // know how to split edges. It should be possible to make the optimizer
239 // think in terms of edges, rather than blocks, and then split critical
242 // TODO: OptimizeSequences could generalized to be Interprocedural.
244 // TODO: Recognize that a bunch of other objc runtime calls have
245 // non-escaping arguments and non-releasing arguments, and may be
246 // non-autoreleasing.
248 // TODO: Sink autorelease calls as far as possible. Unfortunately we
249 // usually can't sink them past other calls, which would be the main
250 // case where it would be useful.
252 // TODO: The pointer returned from objc_loadWeakRetained is retained.
254 // TODO: Delete release+retain pairs (rare).
256 STATISTIC(NumNoops, "Number of no-op objc calls eliminated");
257 STATISTIC(NumPartialNoops, "Number of partially no-op objc calls eliminated");
258 STATISTIC(NumAutoreleases,"Number of autoreleases converted to releases");
259 STATISTIC(NumRets, "Number of return value forwarding "
260 "retain+autoreleases eliminated");
261 STATISTIC(NumRRs, "Number of retain+release paths eliminated");
262 STATISTIC(NumPeeps, "Number of calls peephole-optimized");
264 STATISTIC(NumRetainsBeforeOpt,
265 "Number of retains before optimization");
266 STATISTIC(NumReleasesBeforeOpt,
267 "Number of releases before optimization");
268 STATISTIC(NumRetainsAfterOpt,
269 "Number of retains after optimization");
270 STATISTIC(NumReleasesAfterOpt,
271 "Number of releases after optimization");
277 /// \brief A sequence of states that a pointer may go through in which an
278 /// objc_retain and objc_release are actually needed.
281 S_Retain, ///< objc_retain(x).
282 S_CanRelease, ///< foo(x) -- x could possibly see a ref count decrement.
283 S_Use, ///< any use of x.
284 S_Stop, ///< like S_Release, but code motion is stopped.
285 S_Release, ///< objc_release(x).
286 S_MovableRelease ///< objc_release(x), !clang.imprecise_release.
289 raw_ostream &operator<<(raw_ostream &OS, const Sequence S)
290 LLVM_ATTRIBUTE_UNUSED;
291 raw_ostream &operator<<(raw_ostream &OS, const Sequence S) {
294 return OS << "S_None";
296 return OS << "S_Retain";
298 return OS << "S_CanRelease";
300 return OS << "S_Use";
302 return OS << "S_Release";
303 case S_MovableRelease:
304 return OS << "S_MovableRelease";
306 return OS << "S_Stop";
308 llvm_unreachable("Unknown sequence type.");
312 static Sequence MergeSeqs(Sequence A, Sequence B, bool TopDown) {
316 if (A == S_None || B == S_None)
319 if (A > B) std::swap(A, B);
321 // Choose the side which is further along in the sequence.
322 if ((A == S_Retain || A == S_CanRelease) &&
323 (B == S_CanRelease || B == S_Use))
326 // Choose the side which is further along in the sequence.
327 if ((A == S_Use || A == S_CanRelease) &&
328 (B == S_Use || B == S_Release || B == S_Stop || B == S_MovableRelease))
330 // If both sides are releases, choose the more conservative one.
331 if (A == S_Stop && (B == S_Release || B == S_MovableRelease))
333 if (A == S_Release && B == S_MovableRelease)
341 /// \brief Unidirectional information about either a
342 /// retain-decrement-use-release sequence or release-use-decrement-retain
343 /// reverse sequence.
345 /// After an objc_retain, the reference count of the referenced
346 /// object is known to be positive. Similarly, before an objc_release, the
347 /// reference count of the referenced object is known to be positive. If
348 /// there are retain-release pairs in code regions where the retain count
349 /// is known to be positive, they can be eliminated, regardless of any side
350 /// effects between them.
352 /// Also, a retain+release pair nested within another retain+release
353 /// pair all on the known same pointer value can be eliminated, regardless
354 /// of any intervening side effects.
356 /// KnownSafe is true when either of these conditions is satisfied.
359 /// True of the objc_release calls are all marked with the "tail" keyword.
360 bool IsTailCallRelease;
362 /// If the Calls are objc_release calls and they all have a
363 /// clang.imprecise_release tag, this is the metadata tag.
364 MDNode *ReleaseMetadata;
366 /// For a top-down sequence, the set of objc_retains or
367 /// objc_retainBlocks. For bottom-up, the set of objc_releases.
368 SmallPtrSet<Instruction *, 2> Calls;
370 /// The set of optimal insert positions for moving calls in the opposite
372 SmallPtrSet<Instruction *, 2> ReverseInsertPts;
374 /// If this is true, we cannot perform code motion but can still remove
375 /// retain/release pairs.
376 bool CFGHazardAfflicted;
379 KnownSafe(false), IsTailCallRelease(false), ReleaseMetadata(0),
380 CFGHazardAfflicted(false) {}
384 /// Conservatively merge the two RRInfo. Returns true if a partial merge has
385 /// occured, false otherwise.
386 bool Merge(const RRInfo &Other);
391 void RRInfo::clear() {
393 IsTailCallRelease = false;
396 ReverseInsertPts.clear();
397 CFGHazardAfflicted = false;
400 bool RRInfo::Merge(const RRInfo &Other) {
401 // Conservatively merge the ReleaseMetadata information.
402 if (ReleaseMetadata != Other.ReleaseMetadata)
405 // Conservatively merge the boolean state.
406 KnownSafe &= Other.KnownSafe;
407 IsTailCallRelease &= Other.IsTailCallRelease;
408 CFGHazardAfflicted |= Other.CFGHazardAfflicted;
410 // Merge the call sets.
411 Calls.insert(Other.Calls.begin(), Other.Calls.end());
413 // Merge the insert point sets. If there are any differences,
414 // that makes this a partial merge.
415 bool Partial = ReverseInsertPts.size() != Other.ReverseInsertPts.size();
416 for (SmallPtrSet<Instruction *, 2>::const_iterator
417 I = Other.ReverseInsertPts.begin(),
418 E = Other.ReverseInsertPts.end(); I != E; ++I)
419 Partial |= ReverseInsertPts.insert(*I);
424 /// \brief This class summarizes several per-pointer runtime properties which
425 /// are propogated through the flow graph.
427 /// True if the reference count is known to be incremented.
428 bool KnownPositiveRefCount;
430 /// True if we've seen an opportunity for partial RR elimination, such as
431 /// pushing calls into a CFG triangle or into one side of a CFG diamond.
434 /// The current position in the sequence.
435 unsigned char Seq : 8;
437 /// Unidirectional information about the current sequence.
441 PtrState() : KnownPositiveRefCount(false), Partial(false),
445 bool IsKnownSafe() const {
446 return RRI.KnownSafe;
449 void SetKnownSafe(const bool NewValue) {
450 RRI.KnownSafe = NewValue;
453 bool IsTailCallRelease() const {
454 return RRI.IsTailCallRelease;
457 void SetTailCallRelease(const bool NewValue) {
458 RRI.IsTailCallRelease = NewValue;
461 bool IsTrackingImpreciseReleases() const {
462 return RRI.ReleaseMetadata != 0;
465 const MDNode *GetReleaseMetadata() const {
466 return RRI.ReleaseMetadata;
469 void SetReleaseMetadata(MDNode *NewValue) {
470 RRI.ReleaseMetadata = NewValue;
473 bool IsCFGHazardAfflicted() const {
474 return RRI.CFGHazardAfflicted;
477 void SetCFGHazardAfflicted(const bool NewValue) {
478 RRI.CFGHazardAfflicted = NewValue;
481 void SetKnownPositiveRefCount() {
482 DEBUG(dbgs() << "Setting Known Positive.\n");
483 KnownPositiveRefCount = true;
486 void ClearKnownPositiveRefCount() {
487 DEBUG(dbgs() << "Clearing Known Positive.\n");
488 KnownPositiveRefCount = false;
491 bool HasKnownPositiveRefCount() const {
492 return KnownPositiveRefCount;
495 void SetSeq(Sequence NewSeq) {
496 DEBUG(dbgs() << "Old: " << Seq << "; New: " << NewSeq << "\n");
500 Sequence GetSeq() const {
501 return static_cast<Sequence>(Seq);
504 void ClearSequenceProgress() {
505 ResetSequenceProgress(S_None);
508 void ResetSequenceProgress(Sequence NewSeq) {
509 DEBUG(dbgs() << "Resetting sequence progress.\n");
515 void Merge(const PtrState &Other, bool TopDown);
517 void InsertCall(Instruction *I) {
521 void InsertReverseInsertPt(Instruction *I) {
522 RRI.ReverseInsertPts.insert(I);
525 void ClearReverseInsertPts() {
526 RRI.ReverseInsertPts.clear();
529 bool HasReverseInsertPts() const {
530 return !RRI.ReverseInsertPts.empty();
533 const RRInfo &GetRRInfo() const {
540 PtrState::Merge(const PtrState &Other, bool TopDown) {
541 Seq = MergeSeqs(static_cast<Sequence>(Seq), static_cast<Sequence>(Other.Seq),
543 KnownPositiveRefCount &= Other.KnownPositiveRefCount;
545 // If we're not in a sequence (anymore), drop all associated state.
549 } else if (Partial || Other.Partial) {
550 // If we're doing a merge on a path that's previously seen a partial
551 // merge, conservatively drop the sequence, to avoid doing partial
552 // RR elimination. If the branch predicates for the two merge differ,
553 // mixing them is unsafe.
554 ClearSequenceProgress();
556 // Otherwise merge the other PtrState's RRInfo into our RRInfo. At this
557 // point, we know that currently we are not partial. Stash whether or not
558 // the merge operation caused us to undergo a partial merging of reverse
560 Partial = RRI.Merge(Other.RRI);
565 /// \brief Per-BasicBlock state.
567 /// The number of unique control paths from the entry which can reach this
569 unsigned TopDownPathCount;
571 /// The number of unique control paths to exits from this block.
572 unsigned BottomUpPathCount;
574 /// A type for PerPtrTopDown and PerPtrBottomUp.
575 typedef MapVector<const Value *, PtrState> MapTy;
577 /// The top-down traversal uses this to record information known about a
578 /// pointer at the bottom of each block.
581 /// The bottom-up traversal uses this to record information known about a
582 /// pointer at the top of each block.
583 MapTy PerPtrBottomUp;
585 /// Effective predecessors of the current block ignoring ignorable edges and
586 /// ignored backedges.
587 SmallVector<BasicBlock *, 2> Preds;
588 /// Effective successors of the current block ignoring ignorable edges and
589 /// ignored backedges.
590 SmallVector<BasicBlock *, 2> Succs;
593 static const unsigned OverflowOccurredValue;
595 BBState() : TopDownPathCount(0), BottomUpPathCount(0) { }
597 typedef MapTy::iterator ptr_iterator;
598 typedef MapTy::const_iterator ptr_const_iterator;
600 ptr_iterator top_down_ptr_begin() { return PerPtrTopDown.begin(); }
601 ptr_iterator top_down_ptr_end() { return PerPtrTopDown.end(); }
602 ptr_const_iterator top_down_ptr_begin() const {
603 return PerPtrTopDown.begin();
605 ptr_const_iterator top_down_ptr_end() const {
606 return PerPtrTopDown.end();
609 ptr_iterator bottom_up_ptr_begin() { return PerPtrBottomUp.begin(); }
610 ptr_iterator bottom_up_ptr_end() { return PerPtrBottomUp.end(); }
611 ptr_const_iterator bottom_up_ptr_begin() const {
612 return PerPtrBottomUp.begin();
614 ptr_const_iterator bottom_up_ptr_end() const {
615 return PerPtrBottomUp.end();
618 /// Mark this block as being an entry block, which has one path from the
619 /// entry by definition.
620 void SetAsEntry() { TopDownPathCount = 1; }
622 /// Mark this block as being an exit block, which has one path to an exit by
624 void SetAsExit() { BottomUpPathCount = 1; }
626 /// Attempt to find the PtrState object describing the top down state for
627 /// pointer Arg. Return a new initialized PtrState describing the top down
628 /// state for Arg if we do not find one.
629 PtrState &getPtrTopDownState(const Value *Arg) {
630 return PerPtrTopDown[Arg];
633 /// Attempt to find the PtrState object describing the bottom up state for
634 /// pointer Arg. Return a new initialized PtrState describing the bottom up
635 /// state for Arg if we do not find one.
636 PtrState &getPtrBottomUpState(const Value *Arg) {
637 return PerPtrBottomUp[Arg];
640 /// Attempt to find the PtrState object describing the bottom up state for
642 ptr_iterator findPtrBottomUpState(const Value *Arg) {
643 return PerPtrBottomUp.find(Arg);
646 void clearBottomUpPointers() {
647 PerPtrBottomUp.clear();
650 void clearTopDownPointers() {
651 PerPtrTopDown.clear();
654 void InitFromPred(const BBState &Other);
655 void InitFromSucc(const BBState &Other);
656 void MergePred(const BBState &Other);
657 void MergeSucc(const BBState &Other);
659 /// Compute the number of possible unique paths from an entry to an exit
660 /// which pass through this block. This is only valid after both the
661 /// top-down and bottom-up traversals are complete.
663 /// Returns true if overflow occured. Returns false if overflow did not
665 bool GetAllPathCountWithOverflow(unsigned &PathCount) const {
666 if (TopDownPathCount == OverflowOccurredValue ||
667 BottomUpPathCount == OverflowOccurredValue)
669 unsigned long long Product =
670 (unsigned long long)TopDownPathCount*BottomUpPathCount;
671 // Overflow occured if any of the upper bits of Product are set or if all
672 // the lower bits of Product are all set.
673 return (Product >> 32) ||
674 ((PathCount = Product) == OverflowOccurredValue);
677 // Specialized CFG utilities.
678 typedef SmallVectorImpl<BasicBlock *>::const_iterator edge_iterator;
679 edge_iterator pred_begin() const { return Preds.begin(); }
680 edge_iterator pred_end() const { return Preds.end(); }
681 edge_iterator succ_begin() const { return Succs.begin(); }
682 edge_iterator succ_end() const { return Succs.end(); }
684 void addSucc(BasicBlock *Succ) { Succs.push_back(Succ); }
685 void addPred(BasicBlock *Pred) { Preds.push_back(Pred); }
687 bool isExit() const { return Succs.empty(); }
690 const unsigned BBState::OverflowOccurredValue = 0xffffffff;
693 void BBState::InitFromPred(const BBState &Other) {
694 PerPtrTopDown = Other.PerPtrTopDown;
695 TopDownPathCount = Other.TopDownPathCount;
698 void BBState::InitFromSucc(const BBState &Other) {
699 PerPtrBottomUp = Other.PerPtrBottomUp;
700 BottomUpPathCount = Other.BottomUpPathCount;
703 /// The top-down traversal uses this to merge information about predecessors to
704 /// form the initial state for a new block.
705 void BBState::MergePred(const BBState &Other) {
706 if (TopDownPathCount == OverflowOccurredValue)
709 // Other.TopDownPathCount can be 0, in which case it is either dead or a
710 // loop backedge. Loop backedges are special.
711 TopDownPathCount += Other.TopDownPathCount;
713 // In order to be consistent, we clear the top down pointers when by adding
714 // TopDownPathCount becomes OverflowOccurredValue even though "true" overflow
716 if (TopDownPathCount == OverflowOccurredValue) {
717 clearTopDownPointers();
721 // Check for overflow. If we have overflow, fall back to conservative
723 if (TopDownPathCount < Other.TopDownPathCount) {
724 TopDownPathCount = OverflowOccurredValue;
725 clearTopDownPointers();
729 // For each entry in the other set, if our set has an entry with the same key,
730 // merge the entries. Otherwise, copy the entry and merge it with an empty
732 for (ptr_const_iterator MI = Other.top_down_ptr_begin(),
733 ME = Other.top_down_ptr_end(); MI != ME; ++MI) {
734 std::pair<ptr_iterator, bool> Pair = PerPtrTopDown.insert(*MI);
735 Pair.first->second.Merge(Pair.second ? PtrState() : MI->second,
739 // For each entry in our set, if the other set doesn't have an entry with the
740 // same key, force it to merge with an empty entry.
741 for (ptr_iterator MI = top_down_ptr_begin(),
742 ME = top_down_ptr_end(); MI != ME; ++MI)
743 if (Other.PerPtrTopDown.find(MI->first) == Other.PerPtrTopDown.end())
744 MI->second.Merge(PtrState(), /*TopDown=*/true);
747 /// The bottom-up traversal uses this to merge information about successors to
748 /// form the initial state for a new block.
749 void BBState::MergeSucc(const BBState &Other) {
750 if (BottomUpPathCount == OverflowOccurredValue)
753 // Other.BottomUpPathCount can be 0, in which case it is either dead or a
754 // loop backedge. Loop backedges are special.
755 BottomUpPathCount += Other.BottomUpPathCount;
757 // In order to be consistent, we clear the top down pointers when by adding
758 // BottomUpPathCount becomes OverflowOccurredValue even though "true" overflow
760 if (BottomUpPathCount == OverflowOccurredValue) {
761 clearBottomUpPointers();
765 // Check for overflow. If we have overflow, fall back to conservative
767 if (BottomUpPathCount < Other.BottomUpPathCount) {
768 BottomUpPathCount = OverflowOccurredValue;
769 clearBottomUpPointers();
773 // For each entry in the other set, if our set has an entry with the
774 // same key, merge the entries. Otherwise, copy the entry and merge
775 // it with an empty entry.
776 for (ptr_const_iterator MI = Other.bottom_up_ptr_begin(),
777 ME = Other.bottom_up_ptr_end(); MI != ME; ++MI) {
778 std::pair<ptr_iterator, bool> Pair = PerPtrBottomUp.insert(*MI);
779 Pair.first->second.Merge(Pair.second ? PtrState() : MI->second,
783 // For each entry in our set, if the other set doesn't have an entry
784 // with the same key, force it to merge with an empty entry.
785 for (ptr_iterator MI = bottom_up_ptr_begin(),
786 ME = bottom_up_ptr_end(); MI != ME; ++MI)
787 if (Other.PerPtrBottomUp.find(MI->first) == Other.PerPtrBottomUp.end())
788 MI->second.Merge(PtrState(), /*TopDown=*/false);
791 // Only enable ARC Annotations if we are building a debug version of
794 #define ARC_ANNOTATIONS
797 // Define some macros along the lines of DEBUG and some helper functions to make
798 // it cleaner to create annotations in the source code and to no-op when not
799 // building in debug mode.
800 #ifdef ARC_ANNOTATIONS
802 #include "llvm/Support/CommandLine.h"
804 /// Enable/disable ARC sequence annotations.
806 EnableARCAnnotations("enable-objc-arc-annotations", cl::init(false),
807 cl::desc("Enable emission of arc data flow analysis "
810 DisableCheckForCFGHazards("disable-objc-arc-checkforcfghazards", cl::init(false),
811 cl::desc("Disable check for cfg hazards when "
813 static cl::opt<std::string>
814 ARCAnnotationTargetIdentifier("objc-arc-annotation-target-identifier",
816 cl::desc("filter out all data flow annotations "
817 "but those that apply to the given "
818 "target llvm identifier."));
820 /// This function appends a unique ARCAnnotationProvenanceSourceMDKind id to an
821 /// instruction so that we can track backwards when post processing via the llvm
822 /// arc annotation processor tool. If the function is an
823 static MDString *AppendMDNodeToSourcePtr(unsigned NodeId,
827 // If pointer is a result of an instruction and it does not have a source
828 // MDNode it, attach a new MDNode onto it. If pointer is a result of
829 // an instruction and does have a source MDNode attached to it, return a
830 // reference to said Node. Otherwise just return 0.
831 if (Instruction *Inst = dyn_cast<Instruction>(Ptr)) {
833 if (!(Node = Inst->getMetadata(NodeId))) {
834 // We do not have any node. Generate and attatch the hash MDString to the
837 // We just use an MDString to ensure that this metadata gets written out
838 // of line at the module level and to provide a very simple format
839 // encoding the information herein. Both of these makes it simpler to
840 // parse the annotations by a simple external program.
842 raw_string_ostream os(Str);
843 os << "(" << Inst->getParent()->getParent()->getName() << ",%"
844 << Inst->getName() << ")";
846 Hash = MDString::get(Inst->getContext(), os.str());
847 Inst->setMetadata(NodeId, MDNode::get(Inst->getContext(),Hash));
849 // We have a node. Grab its hash and return it.
850 assert(Node->getNumOperands() == 1 &&
851 "An ARCAnnotationProvenanceSourceMDKind can only have 1 operand.");
852 Hash = cast<MDString>(Node->getOperand(0));
854 } else if (Argument *Arg = dyn_cast<Argument>(Ptr)) {
856 raw_string_ostream os(str);
857 os << "(" << Arg->getParent()->getName() << ",%" << Arg->getName()
859 Hash = MDString::get(Arg->getContext(), os.str());
865 static std::string SequenceToString(Sequence A) {
867 raw_string_ostream os(str);
872 /// Helper function to change a Sequence into a String object using our overload
873 /// for raw_ostream so we only have printing code in one location.
874 static MDString *SequenceToMDString(LLVMContext &Context,
876 return MDString::get(Context, SequenceToString(A));
879 /// A simple function to generate a MDNode which describes the change in state
880 /// for Value *Ptr caused by Instruction *Inst.
881 static void AppendMDNodeToInstForPtr(unsigned NodeId,
884 MDString *PtrSourceMDNodeID,
888 Value *tmp[3] = {PtrSourceMDNodeID,
889 SequenceToMDString(Inst->getContext(),
891 SequenceToMDString(Inst->getContext(),
893 Node = MDNode::get(Inst->getContext(),
894 ArrayRef<Value*>(tmp, 3));
896 Inst->setMetadata(NodeId, Node);
899 /// Add to the beginning of the basic block llvm.ptr.annotations which show the
900 /// state of a pointer at the entrance to a basic block.
901 static void GenerateARCBBEntranceAnnotation(const char *Name, BasicBlock *BB,
902 Value *Ptr, Sequence Seq) {
903 // If we have a target identifier, make sure that we match it before
905 if(!ARCAnnotationTargetIdentifier.empty() &&
906 !Ptr->getName().equals(ARCAnnotationTargetIdentifier))
909 Module *M = BB->getParent()->getParent();
910 LLVMContext &C = M->getContext();
911 Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C));
912 Type *I8XX = PointerType::getUnqual(I8X);
913 Type *Params[] = {I8XX, I8XX};
914 FunctionType *FTy = FunctionType::get(Type::getVoidTy(C),
915 ArrayRef<Type*>(Params, 2),
917 Constant *Callee = M->getOrInsertFunction(Name, FTy);
919 IRBuilder<> Builder(BB, BB->getFirstInsertionPt());
922 StringRef Tmp = Ptr->getName();
923 if (0 == (PtrName = M->getGlobalVariable(Tmp, true))) {
924 Value *ActualPtrName = Builder.CreateGlobalStringPtr(Tmp,
926 PtrName = new GlobalVariable(*M, I8X, true, GlobalVariable::InternalLinkage,
927 cast<Constant>(ActualPtrName), Tmp);
931 std::string SeqStr = SequenceToString(Seq);
932 if (0 == (S = M->getGlobalVariable(SeqStr, true))) {
933 Value *ActualPtrName = Builder.CreateGlobalStringPtr(SeqStr,
935 S = new GlobalVariable(*M, I8X, true, GlobalVariable::InternalLinkage,
936 cast<Constant>(ActualPtrName), SeqStr);
939 Builder.CreateCall2(Callee, PtrName, S);
942 /// Add to the end of the basic block llvm.ptr.annotations which show the state
943 /// of the pointer at the bottom of the basic block.
944 static void GenerateARCBBTerminatorAnnotation(const char *Name, BasicBlock *BB,
945 Value *Ptr, Sequence Seq) {
946 // If we have a target identifier, make sure that we match it before emitting
948 if(!ARCAnnotationTargetIdentifier.empty() &&
949 !Ptr->getName().equals(ARCAnnotationTargetIdentifier))
952 Module *M = BB->getParent()->getParent();
953 LLVMContext &C = M->getContext();
954 Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C));
955 Type *I8XX = PointerType::getUnqual(I8X);
956 Type *Params[] = {I8XX, I8XX};
957 FunctionType *FTy = FunctionType::get(Type::getVoidTy(C),
958 ArrayRef<Type*>(Params, 2),
960 Constant *Callee = M->getOrInsertFunction(Name, FTy);
962 IRBuilder<> Builder(BB, llvm::prior(BB->end()));
965 StringRef Tmp = Ptr->getName();
966 if (0 == (PtrName = M->getGlobalVariable(Tmp, true))) {
967 Value *ActualPtrName = Builder.CreateGlobalStringPtr(Tmp,
969 PtrName = new GlobalVariable(*M, I8X, true, GlobalVariable::InternalLinkage,
970 cast<Constant>(ActualPtrName), Tmp);
974 std::string SeqStr = SequenceToString(Seq);
975 if (0 == (S = M->getGlobalVariable(SeqStr, true))) {
976 Value *ActualPtrName = Builder.CreateGlobalStringPtr(SeqStr,
978 S = new GlobalVariable(*M, I8X, true, GlobalVariable::InternalLinkage,
979 cast<Constant>(ActualPtrName), SeqStr);
981 Builder.CreateCall2(Callee, PtrName, S);
984 /// Adds a source annotation to pointer and a state change annotation to Inst
985 /// referencing the source annotation and the old/new state of pointer.
986 static void GenerateARCAnnotation(unsigned InstMDId,
992 if (EnableARCAnnotations) {
993 // If we have a target identifier, make sure that we match it before
994 // emitting an annotation.
995 if(!ARCAnnotationTargetIdentifier.empty() &&
996 !Ptr->getName().equals(ARCAnnotationTargetIdentifier))
999 // First generate the source annotation on our pointer. This will return an
1000 // MDString* if Ptr actually comes from an instruction implying we can put
1001 // in a source annotation. If AppendMDNodeToSourcePtr returns 0 (i.e. NULL),
1002 // then we know that our pointer is from an Argument so we put a reference
1003 // to the argument number.
1005 // The point of this is to make it easy for the
1006 // llvm-arc-annotation-processor tool to cross reference where the source
1007 // pointer is in the LLVM IR since the LLVM IR parser does not submit such
1008 // information via debug info for backends to use (since why would anyone
1009 // need such a thing from LLVM IR besides in non standard cases
1011 MDString *SourcePtrMDNode =
1012 AppendMDNodeToSourcePtr(PtrMDId, Ptr);
1013 AppendMDNodeToInstForPtr(InstMDId, Inst, Ptr, SourcePtrMDNode, OldSeq,
1018 // The actual interface for accessing the above functionality is defined via
1019 // some simple macros which are defined below. We do this so that the user does
1020 // not need to pass in what metadata id is needed resulting in cleaner code and
1021 // additionally since it provides an easy way to conditionally no-op all
1022 // annotation support in a non-debug build.
1024 /// Use this macro to annotate a sequence state change when processing
1025 /// instructions bottom up,
1026 #define ANNOTATE_BOTTOMUP(inst, ptr, old, new) \
1027 GenerateARCAnnotation(ARCAnnotationBottomUpMDKind, \
1028 ARCAnnotationProvenanceSourceMDKind, (inst), \
1029 const_cast<Value*>(ptr), (old), (new))
1030 /// Use this macro to annotate a sequence state change when processing
1031 /// instructions top down.
1032 #define ANNOTATE_TOPDOWN(inst, ptr, old, new) \
1033 GenerateARCAnnotation(ARCAnnotationTopDownMDKind, \
1034 ARCAnnotationProvenanceSourceMDKind, (inst), \
1035 const_cast<Value*>(ptr), (old), (new))
1037 #define ANNOTATE_BB(_states, _bb, _name, _type, _direction) \
1039 if (EnableARCAnnotations) { \
1040 for(BBState::ptr_const_iterator I = (_states)._direction##_ptr_begin(), \
1041 E = (_states)._direction##_ptr_end(); I != E; ++I) { \
1042 Value *Ptr = const_cast<Value*>(I->first); \
1043 Sequence Seq = I->second.GetSeq(); \
1044 GenerateARCBB ## _type ## Annotation(_name, (_bb), Ptr, Seq); \
1049 #define ANNOTATE_BOTTOMUP_BBSTART(_states, _basicblock) \
1050 ANNOTATE_BB(_states, _basicblock, "llvm.arc.annotation.bottomup.bbstart", \
1051 Entrance, bottom_up)
1052 #define ANNOTATE_BOTTOMUP_BBEND(_states, _basicblock) \
1053 ANNOTATE_BB(_states, _basicblock, "llvm.arc.annotation.bottomup.bbend", \
1054 Terminator, bottom_up)
1055 #define ANNOTATE_TOPDOWN_BBSTART(_states, _basicblock) \
1056 ANNOTATE_BB(_states, _basicblock, "llvm.arc.annotation.topdown.bbstart", \
1058 #define ANNOTATE_TOPDOWN_BBEND(_states, _basicblock) \
1059 ANNOTATE_BB(_states, _basicblock, "llvm.arc.annotation.topdown.bbend", \
1060 Terminator, top_down)
1062 #else // !ARC_ANNOTATION
1063 // If annotations are off, noop.
1064 #define ANNOTATE_BOTTOMUP(inst, ptr, old, new)
1065 #define ANNOTATE_TOPDOWN(inst, ptr, old, new)
1066 #define ANNOTATE_BOTTOMUP_BBSTART(states, basicblock)
1067 #define ANNOTATE_BOTTOMUP_BBEND(states, basicblock)
1068 #define ANNOTATE_TOPDOWN_BBSTART(states, basicblock)
1069 #define ANNOTATE_TOPDOWN_BBEND(states, basicblock)
1070 #endif // !ARC_ANNOTATION
1073 /// \brief The main ARC optimization pass.
1074 class ObjCARCOpt : public FunctionPass {
1076 ProvenanceAnalysis PA;
1077 ARCRuntimeEntryPoints EP;
1079 // This is used to track if a pointer is stored into an alloca.
1080 DenseSet<const Value *> MultiOwnersSet;
1082 /// A flag indicating whether this optimization pass should run.
1085 /// Flags which determine whether each of the interesting runtine functions
1086 /// is in fact used in the current function.
1087 unsigned UsedInThisFunction;
1089 /// The Metadata Kind for clang.imprecise_release metadata.
1090 unsigned ImpreciseReleaseMDKind;
1092 /// The Metadata Kind for clang.arc.copy_on_escape metadata.
1093 unsigned CopyOnEscapeMDKind;
1095 /// The Metadata Kind for clang.arc.no_objc_arc_exceptions metadata.
1096 unsigned NoObjCARCExceptionsMDKind;
1098 #ifdef ARC_ANNOTATIONS
1099 /// The Metadata Kind for llvm.arc.annotation.bottomup metadata.
1100 unsigned ARCAnnotationBottomUpMDKind;
1101 /// The Metadata Kind for llvm.arc.annotation.topdown metadata.
1102 unsigned ARCAnnotationTopDownMDKind;
1103 /// The Metadata Kind for llvm.arc.annotation.provenancesource metadata.
1104 unsigned ARCAnnotationProvenanceSourceMDKind;
1105 #endif // ARC_ANNOATIONS
1107 bool OptimizeRetainRVCall(Function &F, Instruction *RetainRV);
1108 void OptimizeAutoreleaseRVCall(Function &F, Instruction *AutoreleaseRV,
1109 InstructionClass &Class);
1110 void OptimizeIndividualCalls(Function &F);
1112 void CheckForCFGHazards(const BasicBlock *BB,
1113 DenseMap<const BasicBlock *, BBState> &BBStates,
1114 BBState &MyStates) const;
1115 bool VisitInstructionBottomUp(Instruction *Inst,
1117 MapVector<Value *, RRInfo> &Retains,
1119 bool VisitBottomUp(BasicBlock *BB,
1120 DenseMap<const BasicBlock *, BBState> &BBStates,
1121 MapVector<Value *, RRInfo> &Retains);
1122 bool VisitInstructionTopDown(Instruction *Inst,
1123 DenseMap<Value *, RRInfo> &Releases,
1125 bool VisitTopDown(BasicBlock *BB,
1126 DenseMap<const BasicBlock *, BBState> &BBStates,
1127 DenseMap<Value *, RRInfo> &Releases);
1128 bool Visit(Function &F,
1129 DenseMap<const BasicBlock *, BBState> &BBStates,
1130 MapVector<Value *, RRInfo> &Retains,
1131 DenseMap<Value *, RRInfo> &Releases);
1133 void MoveCalls(Value *Arg, RRInfo &RetainsToMove, RRInfo &ReleasesToMove,
1134 MapVector<Value *, RRInfo> &Retains,
1135 DenseMap<Value *, RRInfo> &Releases,
1136 SmallVectorImpl<Instruction *> &DeadInsts,
1139 bool ConnectTDBUTraversals(DenseMap<const BasicBlock *, BBState> &BBStates,
1140 MapVector<Value *, RRInfo> &Retains,
1141 DenseMap<Value *, RRInfo> &Releases,
1143 SmallVectorImpl<Instruction *> &NewRetains,
1144 SmallVectorImpl<Instruction *> &NewReleases,
1145 SmallVectorImpl<Instruction *> &DeadInsts,
1146 RRInfo &RetainsToMove,
1147 RRInfo &ReleasesToMove,
1150 bool &AnyPairsCompletelyEliminated);
1152 bool PerformCodePlacement(DenseMap<const BasicBlock *, BBState> &BBStates,
1153 MapVector<Value *, RRInfo> &Retains,
1154 DenseMap<Value *, RRInfo> &Releases,
1157 void OptimizeWeakCalls(Function &F);
1159 bool OptimizeSequences(Function &F);
1161 void OptimizeReturns(Function &F);
1164 void GatherStatistics(Function &F, bool AfterOptimization = false);
1167 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
1168 virtual bool doInitialization(Module &M);
1169 virtual bool runOnFunction(Function &F);
1170 virtual void releaseMemory();
1174 ObjCARCOpt() : FunctionPass(ID) {
1175 initializeObjCARCOptPass(*PassRegistry::getPassRegistry());
1180 char ObjCARCOpt::ID = 0;
1181 INITIALIZE_PASS_BEGIN(ObjCARCOpt,
1182 "objc-arc", "ObjC ARC optimization", false, false)
1183 INITIALIZE_PASS_DEPENDENCY(ObjCARCAliasAnalysis)
1184 INITIALIZE_PASS_END(ObjCARCOpt,
1185 "objc-arc", "ObjC ARC optimization", false, false)
1187 Pass *llvm::createObjCARCOptPass() {
1188 return new ObjCARCOpt();
1191 void ObjCARCOpt::getAnalysisUsage(AnalysisUsage &AU) const {
1192 AU.addRequired<ObjCARCAliasAnalysis>();
1193 AU.addRequired<AliasAnalysis>();
1194 // ARC optimization doesn't currently split critical edges.
1195 AU.setPreservesCFG();
1198 /// Turn objc_retainAutoreleasedReturnValue into objc_retain if the operand is
1199 /// not a return value. Or, if it can be paired with an
1200 /// objc_autoreleaseReturnValue, delete the pair and return true.
1202 ObjCARCOpt::OptimizeRetainRVCall(Function &F, Instruction *RetainRV) {
1203 // Check for the argument being from an immediately preceding call or invoke.
1204 const Value *Arg = GetObjCArg(RetainRV);
1205 ImmutableCallSite CS(Arg);
1206 if (const Instruction *Call = CS.getInstruction()) {
1207 if (Call->getParent() == RetainRV->getParent()) {
1208 BasicBlock::const_iterator I = Call;
1210 while (IsNoopInstruction(I)) ++I;
1211 if (&*I == RetainRV)
1213 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(Call)) {
1214 BasicBlock *RetainRVParent = RetainRV->getParent();
1215 if (II->getNormalDest() == RetainRVParent) {
1216 BasicBlock::const_iterator I = RetainRVParent->begin();
1217 while (IsNoopInstruction(I)) ++I;
1218 if (&*I == RetainRV)
1224 // Check for being preceded by an objc_autoreleaseReturnValue on the same
1225 // pointer. In this case, we can delete the pair.
1226 BasicBlock::iterator I = RetainRV, Begin = RetainRV->getParent()->begin();
1228 do --I; while (I != Begin && IsNoopInstruction(I));
1229 if (GetBasicInstructionClass(I) == IC_AutoreleaseRV &&
1230 GetObjCArg(I) == Arg) {
1234 DEBUG(dbgs() << "Erasing autoreleaseRV,retainRV pair: " << *I << "\n"
1235 << "Erasing " << *RetainRV << "\n");
1237 EraseInstruction(I);
1238 EraseInstruction(RetainRV);
1243 // Turn it to a plain objc_retain.
1247 DEBUG(dbgs() << "Transforming objc_retainAutoreleasedReturnValue => "
1248 "objc_retain since the operand is not a return value.\n"
1249 "Old = " << *RetainRV << "\n");
1251 Constant *NewDecl = EP.get(ARCRuntimeEntryPoints::EPT_Retain);
1252 cast<CallInst>(RetainRV)->setCalledFunction(NewDecl);
1254 DEBUG(dbgs() << "New = " << *RetainRV << "\n");
1259 /// Turn objc_autoreleaseReturnValue into objc_autorelease if the result is not
1260 /// used as a return value.
1262 ObjCARCOpt::OptimizeAutoreleaseRVCall(Function &F, Instruction *AutoreleaseRV,
1263 InstructionClass &Class) {
1264 // Check for a return of the pointer value.
1265 const Value *Ptr = GetObjCArg(AutoreleaseRV);
1266 SmallVector<const Value *, 2> Users;
1267 Users.push_back(Ptr);
1269 Ptr = Users.pop_back_val();
1270 for (Value::const_use_iterator UI = Ptr->use_begin(), UE = Ptr->use_end();
1272 const User *I = *UI;
1273 if (isa<ReturnInst>(I) || GetBasicInstructionClass(I) == IC_RetainRV)
1275 if (isa<BitCastInst>(I))
1278 } while (!Users.empty());
1283 DEBUG(dbgs() << "Transforming objc_autoreleaseReturnValue => "
1284 "objc_autorelease since its operand is not used as a return "
1286 "Old = " << *AutoreleaseRV << "\n");
1288 CallInst *AutoreleaseRVCI = cast<CallInst>(AutoreleaseRV);
1289 Constant *NewDecl = EP.get(ARCRuntimeEntryPoints::EPT_Autorelease);
1290 AutoreleaseRVCI->setCalledFunction(NewDecl);
1291 AutoreleaseRVCI->setTailCall(false); // Never tail call objc_autorelease.
1292 Class = IC_Autorelease;
1294 DEBUG(dbgs() << "New: " << *AutoreleaseRV << "\n");
1298 /// Visit each call, one at a time, and make simplifications without doing any
1299 /// additional analysis.
1300 void ObjCARCOpt::OptimizeIndividualCalls(Function &F) {
1301 DEBUG(dbgs() << "\n== ObjCARCOpt::OptimizeIndividualCalls ==\n");
1302 // Reset all the flags in preparation for recomputing them.
1303 UsedInThisFunction = 0;
1305 // Visit all objc_* calls in F.
1306 for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
1307 Instruction *Inst = &*I++;
1309 InstructionClass Class = GetBasicInstructionClass(Inst);
1311 DEBUG(dbgs() << "Visiting: Class: " << Class << "; " << *Inst << "\n");
1316 // Delete no-op casts. These function calls have special semantics, but
1317 // the semantics are entirely implemented via lowering in the front-end,
1318 // so by the time they reach the optimizer, they are just no-op calls
1319 // which return their argument.
1321 // There are gray areas here, as the ability to cast reference-counted
1322 // pointers to raw void* and back allows code to break ARC assumptions,
1323 // however these are currently considered to be unimportant.
1327 DEBUG(dbgs() << "Erasing no-op cast: " << *Inst << "\n");
1328 EraseInstruction(Inst);
1331 // If the pointer-to-weak-pointer is null, it's undefined behavior.
1334 case IC_LoadWeakRetained:
1336 case IC_DestroyWeak: {
1337 CallInst *CI = cast<CallInst>(Inst);
1338 if (IsNullOrUndef(CI->getArgOperand(0))) {
1340 Type *Ty = CI->getArgOperand(0)->getType();
1341 new StoreInst(UndefValue::get(cast<PointerType>(Ty)->getElementType()),
1342 Constant::getNullValue(Ty),
1344 llvm::Value *NewValue = UndefValue::get(CI->getType());
1345 DEBUG(dbgs() << "A null pointer-to-weak-pointer is undefined behavior."
1346 "\nOld = " << *CI << "\nNew = " << *NewValue << "\n");
1347 CI->replaceAllUsesWith(NewValue);
1348 CI->eraseFromParent();
1355 CallInst *CI = cast<CallInst>(Inst);
1356 if (IsNullOrUndef(CI->getArgOperand(0)) ||
1357 IsNullOrUndef(CI->getArgOperand(1))) {
1359 Type *Ty = CI->getArgOperand(0)->getType();
1360 new StoreInst(UndefValue::get(cast<PointerType>(Ty)->getElementType()),
1361 Constant::getNullValue(Ty),
1364 llvm::Value *NewValue = UndefValue::get(CI->getType());
1365 DEBUG(dbgs() << "A null pointer-to-weak-pointer is undefined behavior."
1366 "\nOld = " << *CI << "\nNew = " << *NewValue << "\n");
1368 CI->replaceAllUsesWith(NewValue);
1369 CI->eraseFromParent();
1375 if (OptimizeRetainRVCall(F, Inst))
1378 case IC_AutoreleaseRV:
1379 OptimizeAutoreleaseRVCall(F, Inst, Class);
1383 // objc_autorelease(x) -> objc_release(x) if x is otherwise unused.
1384 if (IsAutorelease(Class) && Inst->use_empty()) {
1385 CallInst *Call = cast<CallInst>(Inst);
1386 const Value *Arg = Call->getArgOperand(0);
1387 Arg = FindSingleUseIdentifiedObject(Arg);
1392 // Create the declaration lazily.
1393 LLVMContext &C = Inst->getContext();
1395 Constant *Decl = EP.get(ARCRuntimeEntryPoints::EPT_Release);
1396 CallInst *NewCall = CallInst::Create(Decl, Call->getArgOperand(0), "",
1398 NewCall->setMetadata(ImpreciseReleaseMDKind, MDNode::get(C, None));
1400 DEBUG(dbgs() << "Replacing autorelease{,RV}(x) with objc_release(x) "
1401 "since x is otherwise unused.\nOld: " << *Call << "\nNew: "
1402 << *NewCall << "\n");
1404 EraseInstruction(Call);
1410 // For functions which can never be passed stack arguments, add
1412 if (IsAlwaysTail(Class)) {
1414 DEBUG(dbgs() << "Adding tail keyword to function since it can never be "
1415 "passed stack args: " << *Inst << "\n");
1416 cast<CallInst>(Inst)->setTailCall();
1419 // Ensure that functions that can never have a "tail" keyword due to the
1420 // semantics of ARC truly do not do so.
1421 if (IsNeverTail(Class)) {
1423 DEBUG(dbgs() << "Removing tail keyword from function: " << *Inst <<
1425 cast<CallInst>(Inst)->setTailCall(false);
1428 // Set nounwind as needed.
1429 if (IsNoThrow(Class)) {
1431 DEBUG(dbgs() << "Found no throw class. Setting nounwind on: " << *Inst
1433 cast<CallInst>(Inst)->setDoesNotThrow();
1436 if (!IsNoopOnNull(Class)) {
1437 UsedInThisFunction |= 1 << Class;
1441 const Value *Arg = GetObjCArg(Inst);
1443 // ARC calls with null are no-ops. Delete them.
1444 if (IsNullOrUndef(Arg)) {
1447 DEBUG(dbgs() << "ARC calls with null are no-ops. Erasing: " << *Inst
1449 EraseInstruction(Inst);
1453 // Keep track of which of retain, release, autorelease, and retain_block
1454 // are actually present in this function.
1455 UsedInThisFunction |= 1 << Class;
1457 // If Arg is a PHI, and one or more incoming values to the
1458 // PHI are null, and the call is control-equivalent to the PHI, and there
1459 // are no relevant side effects between the PHI and the call, the call
1460 // could be pushed up to just those paths with non-null incoming values.
1461 // For now, don't bother splitting critical edges for this.
1462 SmallVector<std::pair<Instruction *, const Value *>, 4> Worklist;
1463 Worklist.push_back(std::make_pair(Inst, Arg));
1465 std::pair<Instruction *, const Value *> Pair = Worklist.pop_back_val();
1469 const PHINode *PN = dyn_cast<PHINode>(Arg);
1472 // Determine if the PHI has any null operands, or any incoming
1474 bool HasNull = false;
1475 bool HasCriticalEdges = false;
1476 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
1478 StripPointerCastsAndObjCCalls(PN->getIncomingValue(i));
1479 if (IsNullOrUndef(Incoming))
1481 else if (cast<TerminatorInst>(PN->getIncomingBlock(i)->back())
1482 .getNumSuccessors() != 1) {
1483 HasCriticalEdges = true;
1487 // If we have null operands and no critical edges, optimize.
1488 if (!HasCriticalEdges && HasNull) {
1489 SmallPtrSet<Instruction *, 4> DependingInstructions;
1490 SmallPtrSet<const BasicBlock *, 4> Visited;
1492 // Check that there is nothing that cares about the reference
1493 // count between the call and the phi.
1496 case IC_RetainBlock:
1497 // These can always be moved up.
1500 // These can't be moved across things that care about the retain
1502 FindDependencies(NeedsPositiveRetainCount, Arg,
1503 Inst->getParent(), Inst,
1504 DependingInstructions, Visited, PA);
1506 case IC_Autorelease:
1507 // These can't be moved across autorelease pool scope boundaries.
1508 FindDependencies(AutoreleasePoolBoundary, Arg,
1509 Inst->getParent(), Inst,
1510 DependingInstructions, Visited, PA);
1513 case IC_AutoreleaseRV:
1514 // Don't move these; the RV optimization depends on the autoreleaseRV
1515 // being tail called, and the retainRV being immediately after a call
1516 // (which might still happen if we get lucky with codegen layout, but
1517 // it's not worth taking the chance).
1520 llvm_unreachable("Invalid dependence flavor");
1523 if (DependingInstructions.size() == 1 &&
1524 *DependingInstructions.begin() == PN) {
1527 // Clone the call into each predecessor that has a non-null value.
1528 CallInst *CInst = cast<CallInst>(Inst);
1529 Type *ParamTy = CInst->getArgOperand(0)->getType();
1530 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
1532 StripPointerCastsAndObjCCalls(PN->getIncomingValue(i));
1533 if (!IsNullOrUndef(Incoming)) {
1534 CallInst *Clone = cast<CallInst>(CInst->clone());
1535 Value *Op = PN->getIncomingValue(i);
1536 Instruction *InsertPos = &PN->getIncomingBlock(i)->back();
1537 if (Op->getType() != ParamTy)
1538 Op = new BitCastInst(Op, ParamTy, "", InsertPos);
1539 Clone->setArgOperand(0, Op);
1540 Clone->insertBefore(InsertPos);
1542 DEBUG(dbgs() << "Cloning "
1544 "And inserting clone at " << *InsertPos << "\n");
1545 Worklist.push_back(std::make_pair(Clone, Incoming));
1548 // Erase the original call.
1549 DEBUG(dbgs() << "Erasing: " << *CInst << "\n");
1550 EraseInstruction(CInst);
1554 } while (!Worklist.empty());
1558 /// If we have a top down pointer in the S_Use state, make sure that there are
1559 /// no CFG hazards by checking the states of various bottom up pointers.
1560 static void CheckForUseCFGHazard(const Sequence SuccSSeq,
1561 const bool SuccSRRIKnownSafe,
1563 bool &SomeSuccHasSame,
1564 bool &AllSuccsHaveSame,
1565 bool &NotAllSeqEqualButKnownSafe,
1566 bool &ShouldContinue) {
1568 case S_CanRelease: {
1569 if (!S.IsKnownSafe() && !SuccSRRIKnownSafe) {
1570 S.ClearSequenceProgress();
1573 S.SetCFGHazardAfflicted(true);
1574 ShouldContinue = true;
1578 SomeSuccHasSame = true;
1582 case S_MovableRelease:
1583 if (!S.IsKnownSafe() && !SuccSRRIKnownSafe)
1584 AllSuccsHaveSame = false;
1586 NotAllSeqEqualButKnownSafe = true;
1589 llvm_unreachable("bottom-up pointer in retain state!");
1591 llvm_unreachable("This should have been handled earlier.");
1595 /// If we have a Top Down pointer in the S_CanRelease state, make sure that
1596 /// there are no CFG hazards by checking the states of various bottom up
1598 static void CheckForCanReleaseCFGHazard(const Sequence SuccSSeq,
1599 const bool SuccSRRIKnownSafe,
1601 bool &SomeSuccHasSame,
1602 bool &AllSuccsHaveSame,
1603 bool &NotAllSeqEqualButKnownSafe) {
1606 SomeSuccHasSame = true;
1610 case S_MovableRelease:
1612 if (!S.IsKnownSafe() && !SuccSRRIKnownSafe)
1613 AllSuccsHaveSame = false;
1615 NotAllSeqEqualButKnownSafe = true;
1618 llvm_unreachable("bottom-up pointer in retain state!");
1620 llvm_unreachable("This should have been handled earlier.");
1624 /// Check for critical edges, loop boundaries, irreducible control flow, or
1625 /// other CFG structures where moving code across the edge would result in it
1626 /// being executed more.
1628 ObjCARCOpt::CheckForCFGHazards(const BasicBlock *BB,
1629 DenseMap<const BasicBlock *, BBState> &BBStates,
1630 BBState &MyStates) const {
1631 // If any top-down local-use or possible-dec has a succ which is earlier in
1632 // the sequence, forget it.
1633 for (BBState::ptr_iterator I = MyStates.top_down_ptr_begin(),
1634 E = MyStates.top_down_ptr_end(); I != E; ++I) {
1635 PtrState &S = I->second;
1636 const Sequence Seq = I->second.GetSeq();
1638 // We only care about S_Retain, S_CanRelease, and S_Use.
1642 // Make sure that if extra top down states are added in the future that this
1643 // code is updated to handle it.
1644 assert((Seq == S_Retain || Seq == S_CanRelease || Seq == S_Use) &&
1645 "Unknown top down sequence state.");
1647 const Value *Arg = I->first;
1648 const TerminatorInst *TI = cast<TerminatorInst>(&BB->back());
1649 bool SomeSuccHasSame = false;
1650 bool AllSuccsHaveSame = true;
1651 bool NotAllSeqEqualButKnownSafe = false;
1653 succ_const_iterator SI(TI), SE(TI, false);
1655 for (; SI != SE; ++SI) {
1656 // If VisitBottomUp has pointer information for this successor, take
1657 // what we know about it.
1658 const DenseMap<const BasicBlock *, BBState>::iterator BBI =
1660 assert(BBI != BBStates.end());
1661 const PtrState &SuccS = BBI->second.getPtrBottomUpState(Arg);
1662 const Sequence SuccSSeq = SuccS.GetSeq();
1664 // If bottom up, the pointer is in an S_None state, clear the sequence
1665 // progress since the sequence in the bottom up state finished
1666 // suggesting a mismatch in between retains/releases. This is true for
1667 // all three cases that we are handling here: S_Retain, S_Use, and
1669 if (SuccSSeq == S_None) {
1670 S.ClearSequenceProgress();
1674 // If we have S_Use or S_CanRelease, perform our check for cfg hazard
1676 const bool SuccSRRIKnownSafe = SuccS.IsKnownSafe();
1678 // *NOTE* We do not use Seq from above here since we are allowing for
1679 // S.GetSeq() to change while we are visiting basic blocks.
1680 switch(S.GetSeq()) {
1682 bool ShouldContinue = false;
1683 CheckForUseCFGHazard(SuccSSeq, SuccSRRIKnownSafe, S, SomeSuccHasSame,
1684 AllSuccsHaveSame, NotAllSeqEqualButKnownSafe,
1690 case S_CanRelease: {
1691 CheckForCanReleaseCFGHazard(SuccSSeq, SuccSRRIKnownSafe, S,
1692 SomeSuccHasSame, AllSuccsHaveSame,
1693 NotAllSeqEqualButKnownSafe);
1700 case S_MovableRelease:
1705 // If the state at the other end of any of the successor edges
1706 // matches the current state, require all edges to match. This
1707 // guards against loops in the middle of a sequence.
1708 if (SomeSuccHasSame && !AllSuccsHaveSame) {
1709 S.ClearSequenceProgress();
1710 } else if (NotAllSeqEqualButKnownSafe) {
1711 // If we would have cleared the state foregoing the fact that we are known
1712 // safe, stop code motion. This is because whether or not it is safe to
1713 // remove RR pairs via KnownSafe is an orthogonal concept to whether we
1714 // are allowed to perform code motion.
1715 S.SetCFGHazardAfflicted(true);
1721 ObjCARCOpt::VisitInstructionBottomUp(Instruction *Inst,
1723 MapVector<Value *, RRInfo> &Retains,
1724 BBState &MyStates) {
1725 bool NestingDetected = false;
1726 InstructionClass Class = GetInstructionClass(Inst);
1727 const Value *Arg = 0;
1729 DEBUG(dbgs() << "Class: " << Class << "\n");
1733 Arg = GetObjCArg(Inst);
1735 PtrState &S = MyStates.getPtrBottomUpState(Arg);
1737 // If we see two releases in a row on the same pointer. If so, make
1738 // a note, and we'll cicle back to revisit it after we've
1739 // hopefully eliminated the second release, which may allow us to
1740 // eliminate the first release too.
1741 // Theoretically we could implement removal of nested retain+release
1742 // pairs by making PtrState hold a stack of states, but this is
1743 // simple and avoids adding overhead for the non-nested case.
1744 if (S.GetSeq() == S_Release || S.GetSeq() == S_MovableRelease) {
1745 DEBUG(dbgs() << "Found nested releases (i.e. a release pair)\n");
1746 NestingDetected = true;
1749 MDNode *ReleaseMetadata = Inst->getMetadata(ImpreciseReleaseMDKind);
1750 Sequence NewSeq = ReleaseMetadata ? S_MovableRelease : S_Release;
1751 ANNOTATE_BOTTOMUP(Inst, Arg, S.GetSeq(), NewSeq);
1752 S.ResetSequenceProgress(NewSeq);
1753 S.SetReleaseMetadata(ReleaseMetadata);
1754 S.SetKnownSafe(S.HasKnownPositiveRefCount());
1755 S.SetTailCallRelease(cast<CallInst>(Inst)->isTailCall());
1757 S.SetKnownPositiveRefCount();
1760 case IC_RetainBlock:
1761 // In OptimizeIndividualCalls, we have strength reduced all optimizable
1762 // objc_retainBlocks to objc_retains. Thus at this point any
1763 // objc_retainBlocks that we see are not optimizable.
1767 Arg = GetObjCArg(Inst);
1769 PtrState &S = MyStates.getPtrBottomUpState(Arg);
1770 S.SetKnownPositiveRefCount();
1772 Sequence OldSeq = S.GetSeq();
1776 case S_MovableRelease:
1778 // If OldSeq is not S_Use or OldSeq is S_Use and we are tracking an
1779 // imprecise release, clear our reverse insertion points.
1780 if (OldSeq != S_Use || S.IsTrackingImpreciseReleases())
1781 S.ClearReverseInsertPts();
1784 // Don't do retain+release tracking for IC_RetainRV, because it's
1785 // better to let it remain as the first instruction after a call.
1786 if (Class != IC_RetainRV)
1787 Retains[Inst] = S.GetRRInfo();
1788 S.ClearSequenceProgress();
1793 llvm_unreachable("bottom-up pointer in retain state!");
1795 ANNOTATE_BOTTOMUP(Inst, Arg, OldSeq, S.GetSeq());
1796 // A retain moving bottom up can be a use.
1799 case IC_AutoreleasepoolPop:
1800 // Conservatively, clear MyStates for all known pointers.
1801 MyStates.clearBottomUpPointers();
1802 return NestingDetected;
1803 case IC_AutoreleasepoolPush:
1805 // These are irrelevant.
1806 return NestingDetected;
1808 // If we have a store into an alloca of a pointer we are tracking, the
1809 // pointer has multiple owners implying that we must be more conservative.
1811 // This comes up in the context of a pointer being ``KnownSafe''. In the
1812 // presense of a block being initialized, the frontend will emit the
1813 // objc_retain on the original pointer and the release on the pointer loaded
1814 // from the alloca. The optimizer will through the provenance analysis
1815 // realize that the two are related, but since we only require KnownSafe in
1816 // one direction, will match the inner retain on the original pointer with
1817 // the guard release on the original pointer. This is fixed by ensuring that
1818 // in the presense of allocas we only unconditionally remove pointers if
1819 // both our retain and our release are KnownSafe.
1820 if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
1821 if (AreAnyUnderlyingObjectsAnAlloca(SI->getPointerOperand())) {
1822 BBState::ptr_iterator I = MyStates.findPtrBottomUpState(
1823 StripPointerCastsAndObjCCalls(SI->getValueOperand()));
1824 if (I != MyStates.bottom_up_ptr_end())
1825 MultiOwnersSet.insert(I->first);
1833 // Consider any other possible effects of this instruction on each
1834 // pointer being tracked.
1835 for (BBState::ptr_iterator MI = MyStates.bottom_up_ptr_begin(),
1836 ME = MyStates.bottom_up_ptr_end(); MI != ME; ++MI) {
1837 const Value *Ptr = MI->first;
1839 continue; // Handled above.
1840 PtrState &S = MI->second;
1841 Sequence Seq = S.GetSeq();
1843 // Check for possible releases.
1844 if (CanAlterRefCount(Inst, Ptr, PA, Class)) {
1845 DEBUG(dbgs() << "CanAlterRefCount: Seq: " << Seq << "; " << *Ptr
1847 S.ClearKnownPositiveRefCount();
1850 S.SetSeq(S_CanRelease);
1851 ANNOTATE_BOTTOMUP(Inst, Ptr, Seq, S.GetSeq());
1855 case S_MovableRelease:
1860 llvm_unreachable("bottom-up pointer in retain state!");
1864 // Check for possible direct uses.
1867 case S_MovableRelease:
1868 if (CanUse(Inst, Ptr, PA, Class)) {
1869 DEBUG(dbgs() << "CanUse: Seq: " << Seq << "; " << *Ptr
1871 assert(!S.HasReverseInsertPts());
1872 // If this is an invoke instruction, we're scanning it as part of
1873 // one of its successor blocks, since we can't insert code after it
1874 // in its own block, and we don't want to split critical edges.
1875 if (isa<InvokeInst>(Inst))
1876 S.InsertReverseInsertPt(BB->getFirstInsertionPt());
1878 S.InsertReverseInsertPt(llvm::next(BasicBlock::iterator(Inst)));
1880 ANNOTATE_BOTTOMUP(Inst, Ptr, Seq, S_Use);
1881 } else if (Seq == S_Release && IsUser(Class)) {
1882 DEBUG(dbgs() << "PreciseReleaseUse: Seq: " << Seq << "; " << *Ptr
1884 // Non-movable releases depend on any possible objc pointer use.
1886 ANNOTATE_BOTTOMUP(Inst, Ptr, S_Release, S_Stop);
1887 assert(!S.HasReverseInsertPts());
1888 // As above; handle invoke specially.
1889 if (isa<InvokeInst>(Inst))
1890 S.InsertReverseInsertPt(BB->getFirstInsertionPt());
1892 S.InsertReverseInsertPt(llvm::next(BasicBlock::iterator(Inst)));
1896 if (CanUse(Inst, Ptr, PA, Class)) {
1897 DEBUG(dbgs() << "PreciseStopUse: Seq: " << Seq << "; " << *Ptr
1900 ANNOTATE_BOTTOMUP(Inst, Ptr, Seq, S_Use);
1908 llvm_unreachable("bottom-up pointer in retain state!");
1912 return NestingDetected;
1916 ObjCARCOpt::VisitBottomUp(BasicBlock *BB,
1917 DenseMap<const BasicBlock *, BBState> &BBStates,
1918 MapVector<Value *, RRInfo> &Retains) {
1920 DEBUG(dbgs() << "\n== ObjCARCOpt::VisitBottomUp ==\n");
1922 bool NestingDetected = false;
1923 BBState &MyStates = BBStates[BB];
1925 // Merge the states from each successor to compute the initial state
1926 // for the current block.
1927 BBState::edge_iterator SI(MyStates.succ_begin()),
1928 SE(MyStates.succ_end());
1930 const BasicBlock *Succ = *SI;
1931 DenseMap<const BasicBlock *, BBState>::iterator I = BBStates.find(Succ);
1932 assert(I != BBStates.end());
1933 MyStates.InitFromSucc(I->second);
1935 for (; SI != SE; ++SI) {
1937 I = BBStates.find(Succ);
1938 assert(I != BBStates.end());
1939 MyStates.MergeSucc(I->second);
1943 // If ARC Annotations are enabled, output the current state of pointers at the
1944 // bottom of the basic block.
1945 ANNOTATE_BOTTOMUP_BBEND(MyStates, BB);
1947 // Visit all the instructions, bottom-up.
1948 for (BasicBlock::iterator I = BB->end(), E = BB->begin(); I != E; --I) {
1949 Instruction *Inst = llvm::prior(I);
1951 // Invoke instructions are visited as part of their successors (below).
1952 if (isa<InvokeInst>(Inst))
1955 DEBUG(dbgs() << "Visiting " << *Inst << "\n");
1957 NestingDetected |= VisitInstructionBottomUp(Inst, BB, Retains, MyStates);
1960 // If there's a predecessor with an invoke, visit the invoke as if it were
1961 // part of this block, since we can't insert code after an invoke in its own
1962 // block, and we don't want to split critical edges.
1963 for (BBState::edge_iterator PI(MyStates.pred_begin()),
1964 PE(MyStates.pred_end()); PI != PE; ++PI) {
1965 BasicBlock *Pred = *PI;
1966 if (InvokeInst *II = dyn_cast<InvokeInst>(&Pred->back()))
1967 NestingDetected |= VisitInstructionBottomUp(II, BB, Retains, MyStates);
1970 // If ARC Annotations are enabled, output the current state of pointers at the
1971 // top of the basic block.
1972 ANNOTATE_BOTTOMUP_BBSTART(MyStates, BB);
1974 return NestingDetected;
1978 ObjCARCOpt::VisitInstructionTopDown(Instruction *Inst,
1979 DenseMap<Value *, RRInfo> &Releases,
1980 BBState &MyStates) {
1981 bool NestingDetected = false;
1982 InstructionClass Class = GetInstructionClass(Inst);
1983 const Value *Arg = 0;
1986 case IC_RetainBlock:
1987 // In OptimizeIndividualCalls, we have strength reduced all optimizable
1988 // objc_retainBlocks to objc_retains. Thus at this point any
1989 // objc_retainBlocks that we see are not optimizable.
1993 Arg = GetObjCArg(Inst);
1995 PtrState &S = MyStates.getPtrTopDownState(Arg);
1997 // Don't do retain+release tracking for IC_RetainRV, because it's
1998 // better to let it remain as the first instruction after a call.
1999 if (Class != IC_RetainRV) {
2000 // If we see two retains in a row on the same pointer. If so, make
2001 // a note, and we'll cicle back to revisit it after we've
2002 // hopefully eliminated the second retain, which may allow us to
2003 // eliminate the first retain too.
2004 // Theoretically we could implement removal of nested retain+release
2005 // pairs by making PtrState hold a stack of states, but this is
2006 // simple and avoids adding overhead for the non-nested case.
2007 if (S.GetSeq() == S_Retain)
2008 NestingDetected = true;
2010 ANNOTATE_TOPDOWN(Inst, Arg, S.GetSeq(), S_Retain);
2011 S.ResetSequenceProgress(S_Retain);
2012 S.SetKnownSafe(S.HasKnownPositiveRefCount());
2016 S.SetKnownPositiveRefCount();
2018 // A retain can be a potential use; procede to the generic checking
2023 Arg = GetObjCArg(Inst);
2025 PtrState &S = MyStates.getPtrTopDownState(Arg);
2026 S.ClearKnownPositiveRefCount();
2028 Sequence OldSeq = S.GetSeq();
2030 MDNode *ReleaseMetadata = Inst->getMetadata(ImpreciseReleaseMDKind);
2035 if (OldSeq == S_Retain || ReleaseMetadata != 0)
2036 S.ClearReverseInsertPts();
2039 S.SetReleaseMetadata(ReleaseMetadata);
2040 S.SetTailCallRelease(cast<CallInst>(Inst)->isTailCall());
2041 Releases[Inst] = S.GetRRInfo();
2042 ANNOTATE_TOPDOWN(Inst, Arg, S.GetSeq(), S_None);
2043 S.ClearSequenceProgress();
2049 case S_MovableRelease:
2050 llvm_unreachable("top-down pointer in release state!");
2054 case IC_AutoreleasepoolPop:
2055 // Conservatively, clear MyStates for all known pointers.
2056 MyStates.clearTopDownPointers();
2057 return NestingDetected;
2058 case IC_AutoreleasepoolPush:
2060 // These are irrelevant.
2061 return NestingDetected;
2066 // Consider any other possible effects of this instruction on each
2067 // pointer being tracked.
2068 for (BBState::ptr_iterator MI = MyStates.top_down_ptr_begin(),
2069 ME = MyStates.top_down_ptr_end(); MI != ME; ++MI) {
2070 const Value *Ptr = MI->first;
2072 continue; // Handled above.
2073 PtrState &S = MI->second;
2074 Sequence Seq = S.GetSeq();
2076 // Check for possible releases.
2077 if (CanAlterRefCount(Inst, Ptr, PA, Class)) {
2078 DEBUG(dbgs() << "CanAlterRefCount: Seq: " << Seq << "; " << *Ptr
2080 S.ClearKnownPositiveRefCount();
2083 S.SetSeq(S_CanRelease);
2084 ANNOTATE_TOPDOWN(Inst, Ptr, Seq, S_CanRelease);
2085 assert(!S.HasReverseInsertPts());
2086 S.InsertReverseInsertPt(Inst);
2088 // One call can't cause a transition from S_Retain to S_CanRelease
2089 // and S_CanRelease to S_Use. If we've made the first transition,
2098 case S_MovableRelease:
2099 llvm_unreachable("top-down pointer in release state!");
2103 // Check for possible direct uses.
2106 if (CanUse(Inst, Ptr, PA, Class)) {
2107 DEBUG(dbgs() << "CanUse: Seq: " << Seq << "; " << *Ptr
2110 ANNOTATE_TOPDOWN(Inst, Ptr, Seq, S_Use);
2119 case S_MovableRelease:
2120 llvm_unreachable("top-down pointer in release state!");
2124 return NestingDetected;
2128 ObjCARCOpt::VisitTopDown(BasicBlock *BB,
2129 DenseMap<const BasicBlock *, BBState> &BBStates,
2130 DenseMap<Value *, RRInfo> &Releases) {
2131 DEBUG(dbgs() << "\n== ObjCARCOpt::VisitTopDown ==\n");
2132 bool NestingDetected = false;
2133 BBState &MyStates = BBStates[BB];
2135 // Merge the states from each predecessor to compute the initial state
2136 // for the current block.
2137 BBState::edge_iterator PI(MyStates.pred_begin()),
2138 PE(MyStates.pred_end());
2140 const BasicBlock *Pred = *PI;
2141 DenseMap<const BasicBlock *, BBState>::iterator I = BBStates.find(Pred);
2142 assert(I != BBStates.end());
2143 MyStates.InitFromPred(I->second);
2145 for (; PI != PE; ++PI) {
2147 I = BBStates.find(Pred);
2148 assert(I != BBStates.end());
2149 MyStates.MergePred(I->second);
2153 // If ARC Annotations are enabled, output the current state of pointers at the
2154 // top of the basic block.
2155 ANNOTATE_TOPDOWN_BBSTART(MyStates, BB);
2157 // Visit all the instructions, top-down.
2158 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
2159 Instruction *Inst = I;
2161 DEBUG(dbgs() << "Visiting " << *Inst << "\n");
2163 NestingDetected |= VisitInstructionTopDown(Inst, Releases, MyStates);
2166 // If ARC Annotations are enabled, output the current state of pointers at the
2167 // bottom of the basic block.
2168 ANNOTATE_TOPDOWN_BBEND(MyStates, BB);
2170 #ifdef ARC_ANNOTATIONS
2171 if (!(EnableARCAnnotations && DisableCheckForCFGHazards))
2173 CheckForCFGHazards(BB, BBStates, MyStates);
2174 return NestingDetected;
2178 ComputePostOrders(Function &F,
2179 SmallVectorImpl<BasicBlock *> &PostOrder,
2180 SmallVectorImpl<BasicBlock *> &ReverseCFGPostOrder,
2181 unsigned NoObjCARCExceptionsMDKind,
2182 DenseMap<const BasicBlock *, BBState> &BBStates) {
2183 /// The visited set, for doing DFS walks.
2184 SmallPtrSet<BasicBlock *, 16> Visited;
2186 // Do DFS, computing the PostOrder.
2187 SmallPtrSet<BasicBlock *, 16> OnStack;
2188 SmallVector<std::pair<BasicBlock *, succ_iterator>, 16> SuccStack;
2190 // Functions always have exactly one entry block, and we don't have
2191 // any other block that we treat like an entry block.
2192 BasicBlock *EntryBB = &F.getEntryBlock();
2193 BBState &MyStates = BBStates[EntryBB];
2194 MyStates.SetAsEntry();
2195 TerminatorInst *EntryTI = cast<TerminatorInst>(&EntryBB->back());
2196 SuccStack.push_back(std::make_pair(EntryBB, succ_iterator(EntryTI)));
2197 Visited.insert(EntryBB);
2198 OnStack.insert(EntryBB);
2201 BasicBlock *CurrBB = SuccStack.back().first;
2202 TerminatorInst *TI = cast<TerminatorInst>(&CurrBB->back());
2203 succ_iterator SE(TI, false);
2205 while (SuccStack.back().second != SE) {
2206 BasicBlock *SuccBB = *SuccStack.back().second++;
2207 if (Visited.insert(SuccBB)) {
2208 TerminatorInst *TI = cast<TerminatorInst>(&SuccBB->back());
2209 SuccStack.push_back(std::make_pair(SuccBB, succ_iterator(TI)));
2210 BBStates[CurrBB].addSucc(SuccBB);
2211 BBState &SuccStates = BBStates[SuccBB];
2212 SuccStates.addPred(CurrBB);
2213 OnStack.insert(SuccBB);
2217 if (!OnStack.count(SuccBB)) {
2218 BBStates[CurrBB].addSucc(SuccBB);
2219 BBStates[SuccBB].addPred(CurrBB);
2222 OnStack.erase(CurrBB);
2223 PostOrder.push_back(CurrBB);
2224 SuccStack.pop_back();
2225 } while (!SuccStack.empty());
2229 // Do reverse-CFG DFS, computing the reverse-CFG PostOrder.
2230 // Functions may have many exits, and there also blocks which we treat
2231 // as exits due to ignored edges.
2232 SmallVector<std::pair<BasicBlock *, BBState::edge_iterator>, 16> PredStack;
2233 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
2234 BasicBlock *ExitBB = I;
2235 BBState &MyStates = BBStates[ExitBB];
2236 if (!MyStates.isExit())
2239 MyStates.SetAsExit();
2241 PredStack.push_back(std::make_pair(ExitBB, MyStates.pred_begin()));
2242 Visited.insert(ExitBB);
2243 while (!PredStack.empty()) {
2244 reverse_dfs_next_succ:
2245 BBState::edge_iterator PE = BBStates[PredStack.back().first].pred_end();
2246 while (PredStack.back().second != PE) {
2247 BasicBlock *BB = *PredStack.back().second++;
2248 if (Visited.insert(BB)) {
2249 PredStack.push_back(std::make_pair(BB, BBStates[BB].pred_begin()));
2250 goto reverse_dfs_next_succ;
2253 ReverseCFGPostOrder.push_back(PredStack.pop_back_val().first);
2258 // Visit the function both top-down and bottom-up.
2260 ObjCARCOpt::Visit(Function &F,
2261 DenseMap<const BasicBlock *, BBState> &BBStates,
2262 MapVector<Value *, RRInfo> &Retains,
2263 DenseMap<Value *, RRInfo> &Releases) {
2265 // Use reverse-postorder traversals, because we magically know that loops
2266 // will be well behaved, i.e. they won't repeatedly call retain on a single
2267 // pointer without doing a release. We can't use the ReversePostOrderTraversal
2268 // class here because we want the reverse-CFG postorder to consider each
2269 // function exit point, and we want to ignore selected cycle edges.
2270 SmallVector<BasicBlock *, 16> PostOrder;
2271 SmallVector<BasicBlock *, 16> ReverseCFGPostOrder;
2272 ComputePostOrders(F, PostOrder, ReverseCFGPostOrder,
2273 NoObjCARCExceptionsMDKind,
2276 // Use reverse-postorder on the reverse CFG for bottom-up.
2277 bool BottomUpNestingDetected = false;
2278 for (SmallVectorImpl<BasicBlock *>::const_reverse_iterator I =
2279 ReverseCFGPostOrder.rbegin(), E = ReverseCFGPostOrder.rend();
2281 BottomUpNestingDetected |= VisitBottomUp(*I, BBStates, Retains);
2283 // Use reverse-postorder for top-down.
2284 bool TopDownNestingDetected = false;
2285 for (SmallVectorImpl<BasicBlock *>::const_reverse_iterator I =
2286 PostOrder.rbegin(), E = PostOrder.rend();
2288 TopDownNestingDetected |= VisitTopDown(*I, BBStates, Releases);
2290 return TopDownNestingDetected && BottomUpNestingDetected;
2293 /// Move the calls in RetainsToMove and ReleasesToMove.
2294 void ObjCARCOpt::MoveCalls(Value *Arg,
2295 RRInfo &RetainsToMove,
2296 RRInfo &ReleasesToMove,
2297 MapVector<Value *, RRInfo> &Retains,
2298 DenseMap<Value *, RRInfo> &Releases,
2299 SmallVectorImpl<Instruction *> &DeadInsts,
2301 Type *ArgTy = Arg->getType();
2302 Type *ParamTy = PointerType::getUnqual(Type::getInt8Ty(ArgTy->getContext()));
2304 DEBUG(dbgs() << "== ObjCARCOpt::MoveCalls ==\n");
2306 // Insert the new retain and release calls.
2307 for (SmallPtrSet<Instruction *, 2>::const_iterator
2308 PI = ReleasesToMove.ReverseInsertPts.begin(),
2309 PE = ReleasesToMove.ReverseInsertPts.end(); PI != PE; ++PI) {
2310 Instruction *InsertPt = *PI;
2311 Value *MyArg = ArgTy == ParamTy ? Arg :
2312 new BitCastInst(Arg, ParamTy, "", InsertPt);
2313 Constant *Decl = EP.get(ARCRuntimeEntryPoints::EPT_Retain);
2314 CallInst *Call = CallInst::Create(Decl, MyArg, "", InsertPt);
2315 Call->setDoesNotThrow();
2316 Call->setTailCall();
2318 DEBUG(dbgs() << "Inserting new Retain: " << *Call << "\n"
2319 "At insertion point: " << *InsertPt << "\n");
2321 for (SmallPtrSet<Instruction *, 2>::const_iterator
2322 PI = RetainsToMove.ReverseInsertPts.begin(),
2323 PE = RetainsToMove.ReverseInsertPts.end(); PI != PE; ++PI) {
2324 Instruction *InsertPt = *PI;
2325 Value *MyArg = ArgTy == ParamTy ? Arg :
2326 new BitCastInst(Arg, ParamTy, "", InsertPt);
2327 Constant *Decl = EP.get(ARCRuntimeEntryPoints::EPT_Release);
2328 CallInst *Call = CallInst::Create(Decl, MyArg, "", InsertPt);
2329 // Attach a clang.imprecise_release metadata tag, if appropriate.
2330 if (MDNode *M = ReleasesToMove.ReleaseMetadata)
2331 Call->setMetadata(ImpreciseReleaseMDKind, M);
2332 Call->setDoesNotThrow();
2333 if (ReleasesToMove.IsTailCallRelease)
2334 Call->setTailCall();
2336 DEBUG(dbgs() << "Inserting new Release: " << *Call << "\n"
2337 "At insertion point: " << *InsertPt << "\n");
2340 // Delete the original retain and release calls.
2341 for (SmallPtrSet<Instruction *, 2>::const_iterator
2342 AI = RetainsToMove.Calls.begin(),
2343 AE = RetainsToMove.Calls.end(); AI != AE; ++AI) {
2344 Instruction *OrigRetain = *AI;
2345 Retains.blot(OrigRetain);
2346 DeadInsts.push_back(OrigRetain);
2347 DEBUG(dbgs() << "Deleting retain: " << *OrigRetain << "\n");
2349 for (SmallPtrSet<Instruction *, 2>::const_iterator
2350 AI = ReleasesToMove.Calls.begin(),
2351 AE = ReleasesToMove.Calls.end(); AI != AE; ++AI) {
2352 Instruction *OrigRelease = *AI;
2353 Releases.erase(OrigRelease);
2354 DeadInsts.push_back(OrigRelease);
2355 DEBUG(dbgs() << "Deleting release: " << *OrigRelease << "\n");
2361 ObjCARCOpt::ConnectTDBUTraversals(DenseMap<const BasicBlock *, BBState>
2363 MapVector<Value *, RRInfo> &Retains,
2364 DenseMap<Value *, RRInfo> &Releases,
2366 SmallVectorImpl<Instruction *> &NewRetains,
2367 SmallVectorImpl<Instruction *> &NewReleases,
2368 SmallVectorImpl<Instruction *> &DeadInsts,
2369 RRInfo &RetainsToMove,
2370 RRInfo &ReleasesToMove,
2373 bool &AnyPairsCompletelyEliminated) {
2374 // If a pair happens in a region where it is known that the reference count
2375 // is already incremented, we can similarly ignore possible decrements unless
2376 // we are dealing with a retainable object with multiple provenance sources.
2377 bool KnownSafeTD = true, KnownSafeBU = true;
2378 bool MultipleOwners = false;
2379 bool CFGHazardAfflicted = false;
2381 // Connect the dots between the top-down-collected RetainsToMove and
2382 // bottom-up-collected ReleasesToMove to form sets of related calls.
2383 // This is an iterative process so that we connect multiple releases
2384 // to multiple retains if needed.
2385 unsigned OldDelta = 0;
2386 unsigned NewDelta = 0;
2387 unsigned OldCount = 0;
2388 unsigned NewCount = 0;
2389 bool FirstRelease = true;
2391 for (SmallVectorImpl<Instruction *>::const_iterator
2392 NI = NewRetains.begin(), NE = NewRetains.end(); NI != NE; ++NI) {
2393 Instruction *NewRetain = *NI;
2394 MapVector<Value *, RRInfo>::const_iterator It = Retains.find(NewRetain);
2395 assert(It != Retains.end());
2396 const RRInfo &NewRetainRRI = It->second;
2397 KnownSafeTD &= NewRetainRRI.KnownSafe;
2399 MultipleOwners || MultiOwnersSet.count(GetObjCArg(NewRetain));
2400 for (SmallPtrSet<Instruction *, 2>::const_iterator
2401 LI = NewRetainRRI.Calls.begin(),
2402 LE = NewRetainRRI.Calls.end(); LI != LE; ++LI) {
2403 Instruction *NewRetainRelease = *LI;
2404 DenseMap<Value *, RRInfo>::const_iterator Jt =
2405 Releases.find(NewRetainRelease);
2406 if (Jt == Releases.end())
2408 const RRInfo &NewRetainReleaseRRI = Jt->second;
2410 // If the release does not have a reference to the retain as well,
2411 // something happened which is unaccounted for. Do not do anything.
2413 // This can happen if we catch an additive overflow during path count
2415 if (!NewRetainReleaseRRI.Calls.count(NewRetain))
2418 if (ReleasesToMove.Calls.insert(NewRetainRelease)) {
2420 // If we overflow when we compute the path count, don't remove/move
2422 const BBState &NRRBBState = BBStates[NewRetainRelease->getParent()];
2423 unsigned PathCount = BBState::OverflowOccurredValue;
2424 if (NRRBBState.GetAllPathCountWithOverflow(PathCount))
2426 assert(PathCount != BBState::OverflowOccurredValue &&
2427 "PathCount at this point can not be "
2428 "OverflowOccurredValue.");
2429 OldDelta -= PathCount;
2431 // Merge the ReleaseMetadata and IsTailCallRelease values.
2433 ReleasesToMove.ReleaseMetadata =
2434 NewRetainReleaseRRI.ReleaseMetadata;
2435 ReleasesToMove.IsTailCallRelease =
2436 NewRetainReleaseRRI.IsTailCallRelease;
2437 FirstRelease = false;
2439 if (ReleasesToMove.ReleaseMetadata !=
2440 NewRetainReleaseRRI.ReleaseMetadata)
2441 ReleasesToMove.ReleaseMetadata = 0;
2442 if (ReleasesToMove.IsTailCallRelease !=
2443 NewRetainReleaseRRI.IsTailCallRelease)
2444 ReleasesToMove.IsTailCallRelease = false;
2447 // Collect the optimal insertion points.
2449 for (SmallPtrSet<Instruction *, 2>::const_iterator
2450 RI = NewRetainReleaseRRI.ReverseInsertPts.begin(),
2451 RE = NewRetainReleaseRRI.ReverseInsertPts.end();
2453 Instruction *RIP = *RI;
2454 if (ReleasesToMove.ReverseInsertPts.insert(RIP)) {
2455 // If we overflow when we compute the path count, don't
2456 // remove/move anything.
2457 const BBState &RIPBBState = BBStates[RIP->getParent()];
2458 PathCount = BBState::OverflowOccurredValue;
2459 if (RIPBBState.GetAllPathCountWithOverflow(PathCount))
2461 assert(PathCount != BBState::OverflowOccurredValue &&
2462 "PathCount at this point can not be "
2463 "OverflowOccurredValue.");
2464 NewDelta -= PathCount;
2467 NewReleases.push_back(NewRetainRelease);
2472 if (NewReleases.empty()) break;
2474 // Back the other way.
2475 for (SmallVectorImpl<Instruction *>::const_iterator
2476 NI = NewReleases.begin(), NE = NewReleases.end(); NI != NE; ++NI) {
2477 Instruction *NewRelease = *NI;
2478 DenseMap<Value *, RRInfo>::const_iterator It =
2479 Releases.find(NewRelease);
2480 assert(It != Releases.end());
2481 const RRInfo &NewReleaseRRI = It->second;
2482 KnownSafeBU &= NewReleaseRRI.KnownSafe;
2483 CFGHazardAfflicted |= NewReleaseRRI.CFGHazardAfflicted;
2484 for (SmallPtrSet<Instruction *, 2>::const_iterator
2485 LI = NewReleaseRRI.Calls.begin(),
2486 LE = NewReleaseRRI.Calls.end(); LI != LE; ++LI) {
2487 Instruction *NewReleaseRetain = *LI;
2488 MapVector<Value *, RRInfo>::const_iterator Jt =
2489 Retains.find(NewReleaseRetain);
2490 if (Jt == Retains.end())
2492 const RRInfo &NewReleaseRetainRRI = Jt->second;
2494 // If the retain does not have a reference to the release as well,
2495 // something happened which is unaccounted for. Do not do anything.
2497 // This can happen if we catch an additive overflow during path count
2499 if (!NewReleaseRetainRRI.Calls.count(NewRelease))
2502 if (RetainsToMove.Calls.insert(NewReleaseRetain)) {
2503 // If we overflow when we compute the path count, don't remove/move
2505 const BBState &NRRBBState = BBStates[NewReleaseRetain->getParent()];
2506 unsigned PathCount = BBState::OverflowOccurredValue;
2507 if (NRRBBState.GetAllPathCountWithOverflow(PathCount))
2509 assert(PathCount != BBState::OverflowOccurredValue &&
2510 "PathCount at this point can not be "
2511 "OverflowOccurredValue.");
2512 OldDelta += PathCount;
2513 OldCount += PathCount;
2515 // Collect the optimal insertion points.
2517 for (SmallPtrSet<Instruction *, 2>::const_iterator
2518 RI = NewReleaseRetainRRI.ReverseInsertPts.begin(),
2519 RE = NewReleaseRetainRRI.ReverseInsertPts.end();
2521 Instruction *RIP = *RI;
2522 if (RetainsToMove.ReverseInsertPts.insert(RIP)) {
2523 // If we overflow when we compute the path count, don't
2524 // remove/move anything.
2525 const BBState &RIPBBState = BBStates[RIP->getParent()];
2527 PathCount = BBState::OverflowOccurredValue;
2528 if (RIPBBState.GetAllPathCountWithOverflow(PathCount))
2530 assert(PathCount != BBState::OverflowOccurredValue &&
2531 "PathCount at this point can not be "
2532 "OverflowOccurredValue.");
2533 NewDelta += PathCount;
2534 NewCount += PathCount;
2537 NewRetains.push_back(NewReleaseRetain);
2541 NewReleases.clear();
2542 if (NewRetains.empty()) break;
2545 // If the pointer is known incremented in 1 direction and we do not have
2546 // MultipleOwners, we can safely remove the retain/releases. Otherwise we need
2547 // to be known safe in both directions.
2548 bool UnconditionallySafe = (KnownSafeTD && KnownSafeBU) ||
2549 ((KnownSafeTD || KnownSafeBU) && !MultipleOwners);
2550 if (UnconditionallySafe) {
2551 RetainsToMove.ReverseInsertPts.clear();
2552 ReleasesToMove.ReverseInsertPts.clear();
2555 // Determine whether the new insertion points we computed preserve the
2556 // balance of retain and release calls through the program.
2557 // TODO: If the fully aggressive solution isn't valid, try to find a
2558 // less aggressive solution which is.
2562 // At this point, we are not going to remove any RR pairs, but we still are
2563 // able to move RR pairs. If one of our pointers is afflicted with
2564 // CFGHazards, we cannot perform such code motion so exit early.
2565 const bool WillPerformCodeMotion = RetainsToMove.ReverseInsertPts.size() ||
2566 ReleasesToMove.ReverseInsertPts.size();
2567 if (CFGHazardAfflicted && WillPerformCodeMotion)
2571 // Determine whether the original call points are balanced in the retain and
2572 // release calls through the program. If not, conservatively don't touch
2574 // TODO: It's theoretically possible to do code motion in this case, as
2575 // long as the existing imbalances are maintained.
2579 #ifdef ARC_ANNOTATIONS
2580 // Do not move calls if ARC annotations are requested.
2581 if (EnableARCAnnotations)
2583 #endif // ARC_ANNOTATIONS
2586 assert(OldCount != 0 && "Unreachable code?");
2587 NumRRs += OldCount - NewCount;
2588 // Set to true if we completely removed any RR pairs.
2589 AnyPairsCompletelyEliminated = NewCount == 0;
2591 // We can move calls!
2595 /// Identify pairings between the retains and releases, and delete and/or move
2598 ObjCARCOpt::PerformCodePlacement(DenseMap<const BasicBlock *, BBState>
2600 MapVector<Value *, RRInfo> &Retains,
2601 DenseMap<Value *, RRInfo> &Releases,
2603 DEBUG(dbgs() << "\n== ObjCARCOpt::PerformCodePlacement ==\n");
2605 bool AnyPairsCompletelyEliminated = false;
2606 RRInfo RetainsToMove;
2607 RRInfo ReleasesToMove;
2608 SmallVector<Instruction *, 4> NewRetains;
2609 SmallVector<Instruction *, 4> NewReleases;
2610 SmallVector<Instruction *, 8> DeadInsts;
2612 // Visit each retain.
2613 for (MapVector<Value *, RRInfo>::const_iterator I = Retains.begin(),
2614 E = Retains.end(); I != E; ++I) {
2615 Value *V = I->first;
2616 if (!V) continue; // blotted
2618 Instruction *Retain = cast<Instruction>(V);
2620 DEBUG(dbgs() << "Visiting: " << *Retain << "\n");
2622 Value *Arg = GetObjCArg(Retain);
2624 // If the object being released is in static or stack storage, we know it's
2625 // not being managed by ObjC reference counting, so we can delete pairs
2626 // regardless of what possible decrements or uses lie between them.
2627 bool KnownSafe = isa<Constant>(Arg) || isa<AllocaInst>(Arg);
2629 // A constant pointer can't be pointing to an object on the heap. It may
2630 // be reference-counted, but it won't be deleted.
2631 if (const LoadInst *LI = dyn_cast<LoadInst>(Arg))
2632 if (const GlobalVariable *GV =
2633 dyn_cast<GlobalVariable>(
2634 StripPointerCastsAndObjCCalls(LI->getPointerOperand())))
2635 if (GV->isConstant())
2638 // Connect the dots between the top-down-collected RetainsToMove and
2639 // bottom-up-collected ReleasesToMove to form sets of related calls.
2640 NewRetains.push_back(Retain);
2641 bool PerformMoveCalls =
2642 ConnectTDBUTraversals(BBStates, Retains, Releases, M, NewRetains,
2643 NewReleases, DeadInsts, RetainsToMove,
2644 ReleasesToMove, Arg, KnownSafe,
2645 AnyPairsCompletelyEliminated);
2647 if (PerformMoveCalls) {
2648 // Ok, everything checks out and we're all set. Let's move/delete some
2650 MoveCalls(Arg, RetainsToMove, ReleasesToMove,
2651 Retains, Releases, DeadInsts, M);
2654 // Clean up state for next retain.
2655 NewReleases.clear();
2657 RetainsToMove.clear();
2658 ReleasesToMove.clear();
2661 // Now that we're done moving everything, we can delete the newly dead
2662 // instructions, as we no longer need them as insert points.
2663 while (!DeadInsts.empty())
2664 EraseInstruction(DeadInsts.pop_back_val());
2666 return AnyPairsCompletelyEliminated;
2669 /// Weak pointer optimizations.
2670 void ObjCARCOpt::OptimizeWeakCalls(Function &F) {
2671 DEBUG(dbgs() << "\n== ObjCARCOpt::OptimizeWeakCalls ==\n");
2673 // First, do memdep-style RLE and S2L optimizations. We can't use memdep
2674 // itself because it uses AliasAnalysis and we need to do provenance
2676 for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
2677 Instruction *Inst = &*I++;
2679 DEBUG(dbgs() << "Visiting: " << *Inst << "\n");
2681 InstructionClass Class = GetBasicInstructionClass(Inst);
2682 if (Class != IC_LoadWeak && Class != IC_LoadWeakRetained)
2685 // Delete objc_loadWeak calls with no users.
2686 if (Class == IC_LoadWeak && Inst->use_empty()) {
2687 Inst->eraseFromParent();
2691 // TODO: For now, just look for an earlier available version of this value
2692 // within the same block. Theoretically, we could do memdep-style non-local
2693 // analysis too, but that would want caching. A better approach would be to
2694 // use the technique that EarlyCSE uses.
2695 inst_iterator Current = llvm::prior(I);
2696 BasicBlock *CurrentBB = Current.getBasicBlockIterator();
2697 for (BasicBlock::iterator B = CurrentBB->begin(),
2698 J = Current.getInstructionIterator();
2700 Instruction *EarlierInst = &*llvm::prior(J);
2701 InstructionClass EarlierClass = GetInstructionClass(EarlierInst);
2702 switch (EarlierClass) {
2704 case IC_LoadWeakRetained: {
2705 // If this is loading from the same pointer, replace this load's value
2707 CallInst *Call = cast<CallInst>(Inst);
2708 CallInst *EarlierCall = cast<CallInst>(EarlierInst);
2709 Value *Arg = Call->getArgOperand(0);
2710 Value *EarlierArg = EarlierCall->getArgOperand(0);
2711 switch (PA.getAA()->alias(Arg, EarlierArg)) {
2712 case AliasAnalysis::MustAlias:
2714 // If the load has a builtin retain, insert a plain retain for it.
2715 if (Class == IC_LoadWeakRetained) {
2716 Constant *Decl = EP.get(ARCRuntimeEntryPoints::EPT_Retain);
2717 CallInst *CI = CallInst::Create(Decl, EarlierCall, "", Call);
2720 // Zap the fully redundant load.
2721 Call->replaceAllUsesWith(EarlierCall);
2722 Call->eraseFromParent();
2724 case AliasAnalysis::MayAlias:
2725 case AliasAnalysis::PartialAlias:
2727 case AliasAnalysis::NoAlias:
2734 // If this is storing to the same pointer and has the same size etc.
2735 // replace this load's value with the stored value.
2736 CallInst *Call = cast<CallInst>(Inst);
2737 CallInst *EarlierCall = cast<CallInst>(EarlierInst);
2738 Value *Arg = Call->getArgOperand(0);
2739 Value *EarlierArg = EarlierCall->getArgOperand(0);
2740 switch (PA.getAA()->alias(Arg, EarlierArg)) {
2741 case AliasAnalysis::MustAlias:
2743 // If the load has a builtin retain, insert a plain retain for it.
2744 if (Class == IC_LoadWeakRetained) {
2745 Constant *Decl = EP.get(ARCRuntimeEntryPoints::EPT_Retain);
2746 CallInst *CI = CallInst::Create(Decl, EarlierCall, "", Call);
2749 // Zap the fully redundant load.
2750 Call->replaceAllUsesWith(EarlierCall->getArgOperand(1));
2751 Call->eraseFromParent();
2753 case AliasAnalysis::MayAlias:
2754 case AliasAnalysis::PartialAlias:
2756 case AliasAnalysis::NoAlias:
2763 // TOOD: Grab the copied value.
2765 case IC_AutoreleasepoolPush:
2767 case IC_IntrinsicUser:
2769 // Weak pointers are only modified through the weak entry points
2770 // (and arbitrary calls, which could call the weak entry points).
2773 // Anything else could modify the weak pointer.
2780 // Then, for each destroyWeak with an alloca operand, check to see if
2781 // the alloca and all its users can be zapped.
2782 for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
2783 Instruction *Inst = &*I++;
2784 InstructionClass Class = GetBasicInstructionClass(Inst);
2785 if (Class != IC_DestroyWeak)
2788 CallInst *Call = cast<CallInst>(Inst);
2789 Value *Arg = Call->getArgOperand(0);
2790 if (AllocaInst *Alloca = dyn_cast<AllocaInst>(Arg)) {
2791 for (Value::use_iterator UI = Alloca->use_begin(),
2792 UE = Alloca->use_end(); UI != UE; ++UI) {
2793 const Instruction *UserInst = cast<Instruction>(*UI);
2794 switch (GetBasicInstructionClass(UserInst)) {
2797 case IC_DestroyWeak:
2804 for (Value::use_iterator UI = Alloca->use_begin(),
2805 UE = Alloca->use_end(); UI != UE; ) {
2806 CallInst *UserInst = cast<CallInst>(*UI++);
2807 switch (GetBasicInstructionClass(UserInst)) {
2810 // These functions return their second argument.
2811 UserInst->replaceAllUsesWith(UserInst->getArgOperand(1));
2813 case IC_DestroyWeak:
2817 llvm_unreachable("alloca really is used!");
2819 UserInst->eraseFromParent();
2821 Alloca->eraseFromParent();
2827 /// Identify program paths which execute sequences of retains and releases which
2828 /// can be eliminated.
2829 bool ObjCARCOpt::OptimizeSequences(Function &F) {
2830 // Releases, Retains - These are used to store the results of the main flow
2831 // analysis. These use Value* as the key instead of Instruction* so that the
2832 // map stays valid when we get around to rewriting code and calls get
2833 // replaced by arguments.
2834 DenseMap<Value *, RRInfo> Releases;
2835 MapVector<Value *, RRInfo> Retains;
2837 // This is used during the traversal of the function to track the
2838 // states for each identified object at each block.
2839 DenseMap<const BasicBlock *, BBState> BBStates;
2841 // Analyze the CFG of the function, and all instructions.
2842 bool NestingDetected = Visit(F, BBStates, Retains, Releases);
2845 bool AnyPairsCompletelyEliminated = PerformCodePlacement(BBStates, Retains,
2850 MultiOwnersSet.clear();
2852 return AnyPairsCompletelyEliminated && NestingDetected;
2855 /// Check if there is a dependent call earlier that does not have anything in
2856 /// between the Retain and the call that can affect the reference count of their
2857 /// shared pointer argument. Note that Retain need not be in BB.
2859 HasSafePathToPredecessorCall(const Value *Arg, Instruction *Retain,
2860 SmallPtrSet<Instruction *, 4> &DepInsts,
2861 SmallPtrSet<const BasicBlock *, 4> &Visited,
2862 ProvenanceAnalysis &PA) {
2863 FindDependencies(CanChangeRetainCount, Arg, Retain->getParent(), Retain,
2864 DepInsts, Visited, PA);
2865 if (DepInsts.size() != 1)
2869 dyn_cast_or_null<CallInst>(*DepInsts.begin());
2871 // Check that the pointer is the return value of the call.
2872 if (!Call || Arg != Call)
2875 // Check that the call is a regular call.
2876 InstructionClass Class = GetBasicInstructionClass(Call);
2877 if (Class != IC_CallOrUser && Class != IC_Call)
2883 /// Find a dependent retain that precedes the given autorelease for which there
2884 /// is nothing in between the two instructions that can affect the ref count of
2887 FindPredecessorRetainWithSafePath(const Value *Arg, BasicBlock *BB,
2888 Instruction *Autorelease,
2889 SmallPtrSet<Instruction *, 4> &DepInsts,
2890 SmallPtrSet<const BasicBlock *, 4> &Visited,
2891 ProvenanceAnalysis &PA) {
2892 FindDependencies(CanChangeRetainCount, Arg,
2893 BB, Autorelease, DepInsts, Visited, PA);
2894 if (DepInsts.size() != 1)
2898 dyn_cast_or_null<CallInst>(*DepInsts.begin());
2900 // Check that we found a retain with the same argument.
2902 !IsRetain(GetBasicInstructionClass(Retain)) ||
2903 GetObjCArg(Retain) != Arg) {
2910 /// Look for an ``autorelease'' instruction dependent on Arg such that there are
2911 /// no instructions dependent on Arg that need a positive ref count in between
2912 /// the autorelease and the ret.
2914 FindPredecessorAutoreleaseWithSafePath(const Value *Arg, BasicBlock *BB,
2916 SmallPtrSet<Instruction *, 4> &DepInsts,
2917 SmallPtrSet<const BasicBlock *, 4> &V,
2918 ProvenanceAnalysis &PA) {
2919 FindDependencies(NeedsPositiveRetainCount, Arg,
2920 BB, Ret, DepInsts, V, PA);
2921 if (DepInsts.size() != 1)
2924 CallInst *Autorelease =
2925 dyn_cast_or_null<CallInst>(*DepInsts.begin());
2928 InstructionClass AutoreleaseClass = GetBasicInstructionClass(Autorelease);
2929 if (!IsAutorelease(AutoreleaseClass))
2931 if (GetObjCArg(Autorelease) != Arg)
2937 /// Look for this pattern:
2939 /// %call = call i8* @something(...)
2940 /// %2 = call i8* @objc_retain(i8* %call)
2941 /// %3 = call i8* @objc_autorelease(i8* %2)
2944 /// And delete the retain and autorelease.
2945 void ObjCARCOpt::OptimizeReturns(Function &F) {
2946 if (!F.getReturnType()->isPointerTy())
2949 DEBUG(dbgs() << "\n== ObjCARCOpt::OptimizeReturns ==\n");
2951 SmallPtrSet<Instruction *, 4> DependingInstructions;
2952 SmallPtrSet<const BasicBlock *, 4> Visited;
2953 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI) {
2954 BasicBlock *BB = FI;
2955 ReturnInst *Ret = dyn_cast<ReturnInst>(&BB->back());
2957 DEBUG(dbgs() << "Visiting: " << *Ret << "\n");
2962 const Value *Arg = StripPointerCastsAndObjCCalls(Ret->getOperand(0));
2964 // Look for an ``autorelease'' instruction that is a predecessor of Ret and
2965 // dependent on Arg such that there are no instructions dependent on Arg
2966 // that need a positive ref count in between the autorelease and Ret.
2967 CallInst *Autorelease =
2968 FindPredecessorAutoreleaseWithSafePath(Arg, BB, Ret,
2969 DependingInstructions, Visited,
2971 DependingInstructions.clear();
2978 FindPredecessorRetainWithSafePath(Arg, BB, Autorelease,
2979 DependingInstructions, Visited, PA);
2980 DependingInstructions.clear();
2986 // Check that there is nothing that can affect the reference count
2987 // between the retain and the call. Note that Retain need not be in BB.
2988 bool HasSafePathToCall = HasSafePathToPredecessorCall(Arg, Retain,
2989 DependingInstructions,
2991 DependingInstructions.clear();
2994 if (!HasSafePathToCall)
2997 // If so, we can zap the retain and autorelease.
3000 DEBUG(dbgs() << "Erasing: " << *Retain << "\nErasing: "
3001 << *Autorelease << "\n");
3002 EraseInstruction(Retain);
3003 EraseInstruction(Autorelease);
3009 ObjCARCOpt::GatherStatistics(Function &F, bool AfterOptimization) {
3010 llvm::Statistic &NumRetains =
3011 AfterOptimization? NumRetainsAfterOpt : NumRetainsBeforeOpt;
3012 llvm::Statistic &NumReleases =
3013 AfterOptimization? NumReleasesAfterOpt : NumReleasesBeforeOpt;
3015 for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
3016 Instruction *Inst = &*I++;
3017 switch (GetBasicInstructionClass(Inst)) {
3031 bool ObjCARCOpt::doInitialization(Module &M) {
3035 // If nothing in the Module uses ARC, don't do anything.
3036 Run = ModuleHasARC(M);
3040 // Identify the imprecise release metadata kind.
3041 ImpreciseReleaseMDKind =
3042 M.getContext().getMDKindID("clang.imprecise_release");
3043 CopyOnEscapeMDKind =
3044 M.getContext().getMDKindID("clang.arc.copy_on_escape");
3045 NoObjCARCExceptionsMDKind =
3046 M.getContext().getMDKindID("clang.arc.no_objc_arc_exceptions");
3047 #ifdef ARC_ANNOTATIONS
3048 ARCAnnotationBottomUpMDKind =
3049 M.getContext().getMDKindID("llvm.arc.annotation.bottomup");
3050 ARCAnnotationTopDownMDKind =
3051 M.getContext().getMDKindID("llvm.arc.annotation.topdown");
3052 ARCAnnotationProvenanceSourceMDKind =
3053 M.getContext().getMDKindID("llvm.arc.annotation.provenancesource");
3054 #endif // ARC_ANNOTATIONS
3056 // Intuitively, objc_retain and others are nocapture, however in practice
3057 // they are not, because they return their argument value. And objc_release
3058 // calls finalizers which can have arbitrary side effects.
3060 // Initialize our runtime entry point cache.
3066 bool ObjCARCOpt::runOnFunction(Function &F) {
3070 // If nothing in the Module uses ARC, don't do anything.
3076 DEBUG(dbgs() << "<<< ObjCARCOpt: Visiting Function: " << F.getName() << " >>>"
3079 PA.setAA(&getAnalysis<AliasAnalysis>());
3082 if (AreStatisticsEnabled()) {
3083 GatherStatistics(F, false);
3087 // This pass performs several distinct transformations. As a compile-time aid
3088 // when compiling code that isn't ObjC, skip these if the relevant ObjC
3089 // library functions aren't declared.
3091 // Preliminary optimizations. This also computes UsedInThisFunction.
3092 OptimizeIndividualCalls(F);
3094 // Optimizations for weak pointers.
3095 if (UsedInThisFunction & ((1 << IC_LoadWeak) |
3096 (1 << IC_LoadWeakRetained) |
3097 (1 << IC_StoreWeak) |
3098 (1 << IC_InitWeak) |
3099 (1 << IC_CopyWeak) |
3100 (1 << IC_MoveWeak) |
3101 (1 << IC_DestroyWeak)))
3102 OptimizeWeakCalls(F);
3104 // Optimizations for retain+release pairs.
3105 if (UsedInThisFunction & ((1 << IC_Retain) |
3106 (1 << IC_RetainRV) |
3107 (1 << IC_RetainBlock)))
3108 if (UsedInThisFunction & (1 << IC_Release))
3109 // Run OptimizeSequences until it either stops making changes or
3110 // no retain+release pair nesting is detected.
3111 while (OptimizeSequences(F)) {}
3113 // Optimizations if objc_autorelease is used.
3114 if (UsedInThisFunction & ((1 << IC_Autorelease) |
3115 (1 << IC_AutoreleaseRV)))
3118 // Gather statistics after optimization.
3120 if (AreStatisticsEnabled()) {
3121 GatherStatistics(F, true);
3125 DEBUG(dbgs() << "\n");
3130 void ObjCARCOpt::releaseMemory() {