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 //===----------------------------------------------------------------------===//
28 #include "ARCRuntimeEntryPoints.h"
29 #include "DependencyAnalysis.h"
30 #include "ObjCARCAliasAnalysis.h"
31 #include "ProvenanceAnalysis.h"
32 #include "llvm/ADT/DenseMap.h"
33 #include "llvm/ADT/DenseSet.h"
34 #include "llvm/ADT/STLExtras.h"
35 #include "llvm/ADT/SmallPtrSet.h"
36 #include "llvm/ADT/Statistic.h"
37 #include "llvm/IR/CFG.h"
38 #include "llvm/IR/IRBuilder.h"
39 #include "llvm/IR/LLVMContext.h"
40 #include "llvm/Support/Debug.h"
41 #include "llvm/Support/raw_ostream.h"
44 using namespace llvm::objcarc;
46 #define DEBUG_TYPE "objc-arc-opts"
48 /// \defgroup MiscUtils Miscellaneous utilities that are not ARC specific.
52 /// \brief An associative container with fast insertion-order (deterministic)
53 /// iteration over its elements. Plus the special blot operation.
54 template<class KeyT, class ValueT>
56 /// Map keys to indices in Vector.
57 typedef DenseMap<KeyT, size_t> MapTy;
60 typedef std::vector<std::pair<KeyT, ValueT> > VectorTy;
65 typedef typename VectorTy::iterator iterator;
66 typedef typename VectorTy::const_iterator const_iterator;
67 iterator begin() { return Vector.begin(); }
68 iterator end() { return Vector.end(); }
69 const_iterator begin() const { return Vector.begin(); }
70 const_iterator end() const { return Vector.end(); }
74 assert(Vector.size() >= Map.size()); // May differ due to blotting.
75 for (typename MapTy::const_iterator I = Map.begin(), E = Map.end();
77 assert(I->second < Vector.size());
78 assert(Vector[I->second].first == I->first);
80 for (typename VectorTy::const_iterator I = Vector.begin(),
81 E = Vector.end(); I != E; ++I)
83 (Map.count(I->first) &&
84 Map[I->first] == size_t(I - Vector.begin())));
88 ValueT &operator[](const KeyT &Arg) {
89 std::pair<typename MapTy::iterator, bool> Pair =
90 Map.insert(std::make_pair(Arg, size_t(0)));
92 size_t Num = Vector.size();
93 Pair.first->second = Num;
94 Vector.push_back(std::make_pair(Arg, ValueT()));
95 return Vector[Num].second;
97 return Vector[Pair.first->second].second;
100 std::pair<iterator, bool>
101 insert(const std::pair<KeyT, ValueT> &InsertPair) {
102 std::pair<typename MapTy::iterator, bool> Pair =
103 Map.insert(std::make_pair(InsertPair.first, size_t(0)));
105 size_t Num = Vector.size();
106 Pair.first->second = Num;
107 Vector.push_back(InsertPair);
108 return std::make_pair(Vector.begin() + Num, true);
110 return std::make_pair(Vector.begin() + Pair.first->second, false);
113 iterator find(const KeyT &Key) {
114 typename MapTy::iterator It = Map.find(Key);
115 if (It == Map.end()) return Vector.end();
116 return Vector.begin() + It->second;
119 const_iterator find(const KeyT &Key) const {
120 typename MapTy::const_iterator It = Map.find(Key);
121 if (It == Map.end()) return Vector.end();
122 return Vector.begin() + It->second;
125 /// This is similar to erase, but instead of removing the element from the
126 /// vector, it just zeros out the key in the vector. This leaves iterators
127 /// intact, but clients must be prepared for zeroed-out keys when iterating.
128 void blot(const KeyT &Key) {
129 typename MapTy::iterator It = Map.find(Key);
130 if (It == Map.end()) return;
131 Vector[It->second].first = KeyT();
144 /// \defgroup ARCUtilities Utility declarations/definitions specific to ARC.
147 /// \brief This is similar to StripPointerCastsAndObjCCalls but it stops as soon
148 /// as it finds a value with multiple uses.
149 static const Value *FindSingleUseIdentifiedObject(const Value *Arg) {
150 if (Arg->hasOneUse()) {
151 if (const BitCastInst *BC = dyn_cast<BitCastInst>(Arg))
152 return FindSingleUseIdentifiedObject(BC->getOperand(0));
153 if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Arg))
154 if (GEP->hasAllZeroIndices())
155 return FindSingleUseIdentifiedObject(GEP->getPointerOperand());
156 if (IsForwarding(GetBasicInstructionClass(Arg)))
157 return FindSingleUseIdentifiedObject(
158 cast<CallInst>(Arg)->getArgOperand(0));
159 if (!IsObjCIdentifiedObject(Arg))
164 // If we found an identifiable object but it has multiple uses, but they are
165 // trivial uses, we can still consider this to be a single-use value.
166 if (IsObjCIdentifiedObject(Arg)) {
167 for (const User *U : Arg->users())
168 if (!U->use_empty() || StripPointerCastsAndObjCCalls(U) != Arg)
177 /// This is a wrapper around getUnderlyingObjCPtr along the lines of
178 /// GetUnderlyingObjects except that it returns early when it sees the first
180 static inline bool AreAnyUnderlyingObjectsAnAlloca(const Value *V) {
181 SmallPtrSet<const Value *, 4> Visited;
182 SmallVector<const Value *, 4> Worklist;
183 Worklist.push_back(V);
185 const Value *P = Worklist.pop_back_val();
186 P = GetUnderlyingObjCPtr(P);
188 if (isa<AllocaInst>(P))
191 if (!Visited.insert(P))
194 if (const SelectInst *SI = dyn_cast<const SelectInst>(P)) {
195 Worklist.push_back(SI->getTrueValue());
196 Worklist.push_back(SI->getFalseValue());
200 if (const PHINode *PN = dyn_cast<const PHINode>(P)) {
201 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
202 Worklist.push_back(PN->getIncomingValue(i));
205 } while (!Worklist.empty());
213 /// \defgroup ARCOpt ARC Optimization.
216 // TODO: On code like this:
219 // stuff_that_cannot_release()
220 // objc_autorelease(%x)
221 // stuff_that_cannot_release()
223 // stuff_that_cannot_release()
224 // objc_autorelease(%x)
226 // The second retain and autorelease can be deleted.
228 // TODO: It should be possible to delete
229 // objc_autoreleasePoolPush and objc_autoreleasePoolPop
230 // pairs if nothing is actually autoreleased between them. Also, autorelease
231 // calls followed by objc_autoreleasePoolPop calls (perhaps in ObjC++ code
232 // after inlining) can be turned into plain release calls.
234 // TODO: Critical-edge splitting. If the optimial insertion point is
235 // a critical edge, the current algorithm has to fail, because it doesn't
236 // know how to split edges. It should be possible to make the optimizer
237 // think in terms of edges, rather than blocks, and then split critical
240 // TODO: OptimizeSequences could generalized to be Interprocedural.
242 // TODO: Recognize that a bunch of other objc runtime calls have
243 // non-escaping arguments and non-releasing arguments, and may be
244 // non-autoreleasing.
246 // TODO: Sink autorelease calls as far as possible. Unfortunately we
247 // usually can't sink them past other calls, which would be the main
248 // case where it would be useful.
250 // TODO: The pointer returned from objc_loadWeakRetained is retained.
252 // TODO: Delete release+retain pairs (rare).
254 STATISTIC(NumNoops, "Number of no-op objc calls eliminated");
255 STATISTIC(NumPartialNoops, "Number of partially no-op objc calls eliminated");
256 STATISTIC(NumAutoreleases,"Number of autoreleases converted to releases");
257 STATISTIC(NumRets, "Number of return value forwarding "
258 "retain+autoreleases eliminated");
259 STATISTIC(NumRRs, "Number of retain+release paths eliminated");
260 STATISTIC(NumPeeps, "Number of calls peephole-optimized");
262 STATISTIC(NumRetainsBeforeOpt,
263 "Number of retains before optimization");
264 STATISTIC(NumReleasesBeforeOpt,
265 "Number of releases before optimization");
266 STATISTIC(NumRetainsAfterOpt,
267 "Number of retains after optimization");
268 STATISTIC(NumReleasesAfterOpt,
269 "Number of releases after optimization");
275 /// \brief A sequence of states that a pointer may go through in which an
276 /// objc_retain and objc_release are actually needed.
279 S_Retain, ///< objc_retain(x).
280 S_CanRelease, ///< foo(x) -- x could possibly see a ref count decrement.
281 S_Use, ///< any use of x.
282 S_Stop, ///< like S_Release, but code motion is stopped.
283 S_Release, ///< objc_release(x).
284 S_MovableRelease ///< objc_release(x), !clang.imprecise_release.
287 raw_ostream &operator<<(raw_ostream &OS, const Sequence S)
288 LLVM_ATTRIBUTE_UNUSED;
289 raw_ostream &operator<<(raw_ostream &OS, const Sequence S) {
292 return OS << "S_None";
294 return OS << "S_Retain";
296 return OS << "S_CanRelease";
298 return OS << "S_Use";
300 return OS << "S_Release";
301 case S_MovableRelease:
302 return OS << "S_MovableRelease";
304 return OS << "S_Stop";
306 llvm_unreachable("Unknown sequence type.");
310 static Sequence MergeSeqs(Sequence A, Sequence B, bool TopDown) {
314 if (A == S_None || B == S_None)
317 if (A > B) std::swap(A, B);
319 // Choose the side which is further along in the sequence.
320 if ((A == S_Retain || A == S_CanRelease) &&
321 (B == S_CanRelease || B == S_Use))
324 // Choose the side which is further along in the sequence.
325 if ((A == S_Use || A == S_CanRelease) &&
326 (B == S_Use || B == S_Release || B == S_Stop || B == S_MovableRelease))
328 // If both sides are releases, choose the more conservative one.
329 if (A == S_Stop && (B == S_Release || B == S_MovableRelease))
331 if (A == S_Release && B == S_MovableRelease)
339 /// \brief Unidirectional information about either a
340 /// retain-decrement-use-release sequence or release-use-decrement-retain
341 /// reverse sequence.
343 /// After an objc_retain, the reference count of the referenced
344 /// object is known to be positive. Similarly, before an objc_release, the
345 /// reference count of the referenced object is known to be positive. If
346 /// there are retain-release pairs in code regions where the retain count
347 /// is known to be positive, they can be eliminated, regardless of any side
348 /// effects between them.
350 /// Also, a retain+release pair nested within another retain+release
351 /// pair all on the known same pointer value can be eliminated, regardless
352 /// of any intervening side effects.
354 /// KnownSafe is true when either of these conditions is satisfied.
357 /// True of the objc_release calls are all marked with the "tail" keyword.
358 bool IsTailCallRelease;
360 /// If the Calls are objc_release calls and they all have a
361 /// clang.imprecise_release tag, this is the metadata tag.
362 MDNode *ReleaseMetadata;
364 /// For a top-down sequence, the set of objc_retains or
365 /// objc_retainBlocks. For bottom-up, the set of objc_releases.
366 SmallPtrSet<Instruction *, 2> Calls;
368 /// The set of optimal insert positions for moving calls in the opposite
370 SmallPtrSet<Instruction *, 2> ReverseInsertPts;
372 /// If this is true, we cannot perform code motion but can still remove
373 /// retain/release pairs.
374 bool CFGHazardAfflicted;
377 KnownSafe(false), IsTailCallRelease(false), ReleaseMetadata(nullptr),
378 CFGHazardAfflicted(false) {}
382 /// Conservatively merge the two RRInfo. Returns true if a partial merge has
383 /// occurred, false otherwise.
384 bool Merge(const RRInfo &Other);
389 void RRInfo::clear() {
391 IsTailCallRelease = false;
392 ReleaseMetadata = nullptr;
394 ReverseInsertPts.clear();
395 CFGHazardAfflicted = false;
398 bool RRInfo::Merge(const RRInfo &Other) {
399 // Conservatively merge the ReleaseMetadata information.
400 if (ReleaseMetadata != Other.ReleaseMetadata)
401 ReleaseMetadata = nullptr;
403 // Conservatively merge the boolean state.
404 KnownSafe &= Other.KnownSafe;
405 IsTailCallRelease &= Other.IsTailCallRelease;
406 CFGHazardAfflicted |= Other.CFGHazardAfflicted;
408 // Merge the call sets.
409 Calls.insert(Other.Calls.begin(), Other.Calls.end());
411 // Merge the insert point sets. If there are any differences,
412 // that makes this a partial merge.
413 bool Partial = ReverseInsertPts.size() != Other.ReverseInsertPts.size();
414 for (SmallPtrSet<Instruction *, 2>::const_iterator
415 I = Other.ReverseInsertPts.begin(),
416 E = Other.ReverseInsertPts.end(); I != E; ++I)
417 Partial |= ReverseInsertPts.insert(*I);
422 /// \brief This class summarizes several per-pointer runtime properties which
423 /// are propogated through the flow graph.
425 /// True if the reference count is known to be incremented.
426 bool KnownPositiveRefCount;
428 /// True if we've seen an opportunity for partial RR elimination, such as
429 /// pushing calls into a CFG triangle or into one side of a CFG diamond.
432 /// The current position in the sequence.
433 unsigned char Seq : 8;
435 /// Unidirectional information about the current sequence.
439 PtrState() : KnownPositiveRefCount(false), Partial(false),
443 bool IsKnownSafe() const {
444 return RRI.KnownSafe;
447 void SetKnownSafe(const bool NewValue) {
448 RRI.KnownSafe = NewValue;
451 bool IsTailCallRelease() const {
452 return RRI.IsTailCallRelease;
455 void SetTailCallRelease(const bool NewValue) {
456 RRI.IsTailCallRelease = NewValue;
459 bool IsTrackingImpreciseReleases() const {
460 return RRI.ReleaseMetadata != nullptr;
463 const MDNode *GetReleaseMetadata() const {
464 return RRI.ReleaseMetadata;
467 void SetReleaseMetadata(MDNode *NewValue) {
468 RRI.ReleaseMetadata = NewValue;
471 bool IsCFGHazardAfflicted() const {
472 return RRI.CFGHazardAfflicted;
475 void SetCFGHazardAfflicted(const bool NewValue) {
476 RRI.CFGHazardAfflicted = NewValue;
479 void SetKnownPositiveRefCount() {
480 DEBUG(dbgs() << "Setting Known Positive.\n");
481 KnownPositiveRefCount = true;
484 void ClearKnownPositiveRefCount() {
485 DEBUG(dbgs() << "Clearing Known Positive.\n");
486 KnownPositiveRefCount = false;
489 bool HasKnownPositiveRefCount() const {
490 return KnownPositiveRefCount;
493 void SetSeq(Sequence NewSeq) {
494 DEBUG(dbgs() << "Old: " << Seq << "; New: " << NewSeq << "\n");
498 Sequence GetSeq() const {
499 return static_cast<Sequence>(Seq);
502 void ClearSequenceProgress() {
503 ResetSequenceProgress(S_None);
506 void ResetSequenceProgress(Sequence NewSeq) {
507 DEBUG(dbgs() << "Resetting sequence progress.\n");
513 void Merge(const PtrState &Other, bool TopDown);
515 void InsertCall(Instruction *I) {
519 void InsertReverseInsertPt(Instruction *I) {
520 RRI.ReverseInsertPts.insert(I);
523 void ClearReverseInsertPts() {
524 RRI.ReverseInsertPts.clear();
527 bool HasReverseInsertPts() const {
528 return !RRI.ReverseInsertPts.empty();
531 const RRInfo &GetRRInfo() const {
538 PtrState::Merge(const PtrState &Other, bool TopDown) {
539 Seq = MergeSeqs(GetSeq(), Other.GetSeq(), TopDown);
540 KnownPositiveRefCount &= Other.KnownPositiveRefCount;
542 // If we're not in a sequence (anymore), drop all associated state.
546 } else if (Partial || Other.Partial) {
547 // If we're doing a merge on a path that's previously seen a partial
548 // merge, conservatively drop the sequence, to avoid doing partial
549 // RR elimination. If the branch predicates for the two merge differ,
550 // mixing them is unsafe.
551 ClearSequenceProgress();
553 // Otherwise merge the other PtrState's RRInfo into our RRInfo. At this
554 // point, we know that currently we are not partial. Stash whether or not
555 // the merge operation caused us to undergo a partial merging of reverse
557 Partial = RRI.Merge(Other.RRI);
562 /// \brief Per-BasicBlock state.
564 /// The number of unique control paths from the entry which can reach this
566 unsigned TopDownPathCount;
568 /// The number of unique control paths to exits from this block.
569 unsigned BottomUpPathCount;
571 /// A type for PerPtrTopDown and PerPtrBottomUp.
572 typedef MapVector<const Value *, PtrState> MapTy;
574 /// The top-down traversal uses this to record information known about a
575 /// pointer at the bottom of each block.
578 /// The bottom-up traversal uses this to record information known about a
579 /// pointer at the top of each block.
580 MapTy PerPtrBottomUp;
582 /// Effective predecessors of the current block ignoring ignorable edges and
583 /// ignored backedges.
584 SmallVector<BasicBlock *, 2> Preds;
585 /// Effective successors of the current block ignoring ignorable edges and
586 /// ignored backedges.
587 SmallVector<BasicBlock *, 2> Succs;
590 static const unsigned OverflowOccurredValue;
592 BBState() : TopDownPathCount(0), BottomUpPathCount(0) { }
594 typedef MapTy::iterator ptr_iterator;
595 typedef MapTy::const_iterator ptr_const_iterator;
597 ptr_iterator top_down_ptr_begin() { return PerPtrTopDown.begin(); }
598 ptr_iterator top_down_ptr_end() { return PerPtrTopDown.end(); }
599 ptr_const_iterator top_down_ptr_begin() const {
600 return PerPtrTopDown.begin();
602 ptr_const_iterator top_down_ptr_end() const {
603 return PerPtrTopDown.end();
606 ptr_iterator bottom_up_ptr_begin() { return PerPtrBottomUp.begin(); }
607 ptr_iterator bottom_up_ptr_end() { return PerPtrBottomUp.end(); }
608 ptr_const_iterator bottom_up_ptr_begin() const {
609 return PerPtrBottomUp.begin();
611 ptr_const_iterator bottom_up_ptr_end() const {
612 return PerPtrBottomUp.end();
615 /// Mark this block as being an entry block, which has one path from the
616 /// entry by definition.
617 void SetAsEntry() { TopDownPathCount = 1; }
619 /// Mark this block as being an exit block, which has one path to an exit by
621 void SetAsExit() { BottomUpPathCount = 1; }
623 /// Attempt to find the PtrState object describing the top down state for
624 /// pointer Arg. Return a new initialized PtrState describing the top down
625 /// state for Arg if we do not find one.
626 PtrState &getPtrTopDownState(const Value *Arg) {
627 return PerPtrTopDown[Arg];
630 /// Attempt to find the PtrState object describing the bottom up state for
631 /// pointer Arg. Return a new initialized PtrState describing the bottom up
632 /// state for Arg if we do not find one.
633 PtrState &getPtrBottomUpState(const Value *Arg) {
634 return PerPtrBottomUp[Arg];
637 /// Attempt to find the PtrState object describing the bottom up state for
639 ptr_iterator findPtrBottomUpState(const Value *Arg) {
640 return PerPtrBottomUp.find(Arg);
643 void clearBottomUpPointers() {
644 PerPtrBottomUp.clear();
647 void clearTopDownPointers() {
648 PerPtrTopDown.clear();
651 void InitFromPred(const BBState &Other);
652 void InitFromSucc(const BBState &Other);
653 void MergePred(const BBState &Other);
654 void MergeSucc(const BBState &Other);
656 /// Compute the number of possible unique paths from an entry to an exit
657 /// which pass through this block. This is only valid after both the
658 /// top-down and bottom-up traversals are complete.
660 /// Returns true if overflow occurred. Returns false if overflow did not
662 bool GetAllPathCountWithOverflow(unsigned &PathCount) const {
663 if (TopDownPathCount == OverflowOccurredValue ||
664 BottomUpPathCount == OverflowOccurredValue)
666 unsigned long long Product =
667 (unsigned long long)TopDownPathCount*BottomUpPathCount;
668 // Overflow occurred if any of the upper bits of Product are set or if all
669 // the lower bits of Product are all set.
670 return (Product >> 32) ||
671 ((PathCount = Product) == OverflowOccurredValue);
674 // Specialized CFG utilities.
675 typedef SmallVectorImpl<BasicBlock *>::const_iterator edge_iterator;
676 edge_iterator pred_begin() const { return Preds.begin(); }
677 edge_iterator pred_end() const { return Preds.end(); }
678 edge_iterator succ_begin() const { return Succs.begin(); }
679 edge_iterator succ_end() const { return Succs.end(); }
681 void addSucc(BasicBlock *Succ) { Succs.push_back(Succ); }
682 void addPred(BasicBlock *Pred) { Preds.push_back(Pred); }
684 bool isExit() const { return Succs.empty(); }
687 const unsigned BBState::OverflowOccurredValue = 0xffffffff;
690 void BBState::InitFromPred(const BBState &Other) {
691 PerPtrTopDown = Other.PerPtrTopDown;
692 TopDownPathCount = Other.TopDownPathCount;
695 void BBState::InitFromSucc(const BBState &Other) {
696 PerPtrBottomUp = Other.PerPtrBottomUp;
697 BottomUpPathCount = Other.BottomUpPathCount;
700 /// The top-down traversal uses this to merge information about predecessors to
701 /// form the initial state for a new block.
702 void BBState::MergePred(const BBState &Other) {
703 if (TopDownPathCount == OverflowOccurredValue)
706 // Other.TopDownPathCount can be 0, in which case it is either dead or a
707 // loop backedge. Loop backedges are special.
708 TopDownPathCount += Other.TopDownPathCount;
710 // In order to be consistent, we clear the top down pointers when by adding
711 // TopDownPathCount becomes OverflowOccurredValue even though "true" overflow
713 if (TopDownPathCount == OverflowOccurredValue) {
714 clearTopDownPointers();
718 // Check for overflow. If we have overflow, fall back to conservative
720 if (TopDownPathCount < Other.TopDownPathCount) {
721 TopDownPathCount = OverflowOccurredValue;
722 clearTopDownPointers();
726 // For each entry in the other set, if our set has an entry with the same key,
727 // merge the entries. Otherwise, copy the entry and merge it with an empty
729 for (ptr_const_iterator MI = Other.top_down_ptr_begin(),
730 ME = Other.top_down_ptr_end(); MI != ME; ++MI) {
731 std::pair<ptr_iterator, bool> Pair = PerPtrTopDown.insert(*MI);
732 Pair.first->second.Merge(Pair.second ? PtrState() : MI->second,
736 // For each entry in our set, if the other set doesn't have an entry with the
737 // same key, force it to merge with an empty entry.
738 for (ptr_iterator MI = top_down_ptr_begin(),
739 ME = top_down_ptr_end(); MI != ME; ++MI)
740 if (Other.PerPtrTopDown.find(MI->first) == Other.PerPtrTopDown.end())
741 MI->second.Merge(PtrState(), /*TopDown=*/true);
744 /// The bottom-up traversal uses this to merge information about successors to
745 /// form the initial state for a new block.
746 void BBState::MergeSucc(const BBState &Other) {
747 if (BottomUpPathCount == OverflowOccurredValue)
750 // Other.BottomUpPathCount can be 0, in which case it is either dead or a
751 // loop backedge. Loop backedges are special.
752 BottomUpPathCount += Other.BottomUpPathCount;
754 // In order to be consistent, we clear the top down pointers when by adding
755 // BottomUpPathCount becomes OverflowOccurredValue even though "true" overflow
757 if (BottomUpPathCount == OverflowOccurredValue) {
758 clearBottomUpPointers();
762 // Check for overflow. If we have overflow, fall back to conservative
764 if (BottomUpPathCount < Other.BottomUpPathCount) {
765 BottomUpPathCount = OverflowOccurredValue;
766 clearBottomUpPointers();
770 // For each entry in the other set, if our set has an entry with the
771 // same key, merge the entries. Otherwise, copy the entry and merge
772 // it with an empty entry.
773 for (ptr_const_iterator MI = Other.bottom_up_ptr_begin(),
774 ME = Other.bottom_up_ptr_end(); MI != ME; ++MI) {
775 std::pair<ptr_iterator, bool> Pair = PerPtrBottomUp.insert(*MI);
776 Pair.first->second.Merge(Pair.second ? PtrState() : MI->second,
780 // For each entry in our set, if the other set doesn't have an entry
781 // with the same key, force it to merge with an empty entry.
782 for (ptr_iterator MI = bottom_up_ptr_begin(),
783 ME = bottom_up_ptr_end(); MI != ME; ++MI)
784 if (Other.PerPtrBottomUp.find(MI->first) == Other.PerPtrBottomUp.end())
785 MI->second.Merge(PtrState(), /*TopDown=*/false);
788 // Only enable ARC Annotations if we are building a debug version of
791 #define ARC_ANNOTATIONS
794 // Define some macros along the lines of DEBUG and some helper functions to make
795 // it cleaner to create annotations in the source code and to no-op when not
796 // building in debug mode.
797 #ifdef ARC_ANNOTATIONS
799 #include "llvm/Support/CommandLine.h"
801 /// Enable/disable ARC sequence annotations.
803 EnableARCAnnotations("enable-objc-arc-annotations", cl::init(false),
804 cl::desc("Enable emission of arc data flow analysis "
807 DisableCheckForCFGHazards("disable-objc-arc-checkforcfghazards", cl::init(false),
808 cl::desc("Disable check for cfg hazards when "
810 static cl::opt<std::string>
811 ARCAnnotationTargetIdentifier("objc-arc-annotation-target-identifier",
813 cl::desc("filter out all data flow annotations "
814 "but those that apply to the given "
815 "target llvm identifier."));
817 /// This function appends a unique ARCAnnotationProvenanceSourceMDKind id to an
818 /// instruction so that we can track backwards when post processing via the llvm
819 /// arc annotation processor tool. If the function is an
820 static MDString *AppendMDNodeToSourcePtr(unsigned NodeId,
822 MDString *Hash = nullptr;
824 // If pointer is a result of an instruction and it does not have a source
825 // MDNode it, attach a new MDNode onto it. If pointer is a result of
826 // an instruction and does have a source MDNode attached to it, return a
827 // reference to said Node. Otherwise just return 0.
828 if (Instruction *Inst = dyn_cast<Instruction>(Ptr)) {
830 if (!(Node = Inst->getMetadata(NodeId))) {
831 // We do not have any node. Generate and attatch the hash MDString to the
834 // We just use an MDString to ensure that this metadata gets written out
835 // of line at the module level and to provide a very simple format
836 // encoding the information herein. Both of these makes it simpler to
837 // parse the annotations by a simple external program.
839 raw_string_ostream os(Str);
840 os << "(" << Inst->getParent()->getParent()->getName() << ",%"
841 << Inst->getName() << ")";
843 Hash = MDString::get(Inst->getContext(), os.str());
844 Inst->setMetadata(NodeId, MDNode::get(Inst->getContext(),Hash));
846 // We have a node. Grab its hash and return it.
847 assert(Node->getNumOperands() == 1 &&
848 "An ARCAnnotationProvenanceSourceMDKind can only have 1 operand.");
849 Hash = cast<MDString>(Node->getOperand(0));
851 } else if (Argument *Arg = dyn_cast<Argument>(Ptr)) {
853 raw_string_ostream os(str);
854 os << "(" << Arg->getParent()->getName() << ",%" << Arg->getName()
856 Hash = MDString::get(Arg->getContext(), os.str());
862 static std::string SequenceToString(Sequence A) {
864 raw_string_ostream os(str);
869 /// Helper function to change a Sequence into a String object using our overload
870 /// for raw_ostream so we only have printing code in one location.
871 static MDString *SequenceToMDString(LLVMContext &Context,
873 return MDString::get(Context, SequenceToString(A));
876 /// A simple function to generate a MDNode which describes the change in state
877 /// for Value *Ptr caused by Instruction *Inst.
878 static void AppendMDNodeToInstForPtr(unsigned NodeId,
881 MDString *PtrSourceMDNodeID,
884 MDNode *Node = nullptr;
885 Value *tmp[3] = {PtrSourceMDNodeID,
886 SequenceToMDString(Inst->getContext(),
888 SequenceToMDString(Inst->getContext(),
890 Node = MDNode::get(Inst->getContext(),
891 ArrayRef<Value*>(tmp, 3));
893 Inst->setMetadata(NodeId, Node);
896 /// Add to the beginning of the basic block llvm.ptr.annotations which show the
897 /// state of a pointer at the entrance to a basic block.
898 static void GenerateARCBBEntranceAnnotation(const char *Name, BasicBlock *BB,
899 Value *Ptr, Sequence Seq) {
900 // If we have a target identifier, make sure that we match it before
902 if(!ARCAnnotationTargetIdentifier.empty() &&
903 !Ptr->getName().equals(ARCAnnotationTargetIdentifier))
906 Module *M = BB->getParent()->getParent();
907 LLVMContext &C = M->getContext();
908 Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C));
909 Type *I8XX = PointerType::getUnqual(I8X);
910 Type *Params[] = {I8XX, I8XX};
911 FunctionType *FTy = FunctionType::get(Type::getVoidTy(C),
912 ArrayRef<Type*>(Params, 2),
914 Constant *Callee = M->getOrInsertFunction(Name, FTy);
916 IRBuilder<> Builder(BB, BB->getFirstInsertionPt());
919 StringRef Tmp = Ptr->getName();
920 if (nullptr == (PtrName = M->getGlobalVariable(Tmp, true))) {
921 Value *ActualPtrName = Builder.CreateGlobalStringPtr(Tmp,
923 PtrName = new GlobalVariable(*M, I8X, true, GlobalVariable::InternalLinkage,
924 cast<Constant>(ActualPtrName), Tmp);
928 std::string SeqStr = SequenceToString(Seq);
929 if (nullptr == (S = M->getGlobalVariable(SeqStr, true))) {
930 Value *ActualPtrName = Builder.CreateGlobalStringPtr(SeqStr,
932 S = new GlobalVariable(*M, I8X, true, GlobalVariable::InternalLinkage,
933 cast<Constant>(ActualPtrName), SeqStr);
936 Builder.CreateCall2(Callee, PtrName, S);
939 /// Add to the end of the basic block llvm.ptr.annotations which show the state
940 /// of the pointer at the bottom of the basic block.
941 static void GenerateARCBBTerminatorAnnotation(const char *Name, BasicBlock *BB,
942 Value *Ptr, Sequence Seq) {
943 // If we have a target identifier, make sure that we match it before emitting
945 if(!ARCAnnotationTargetIdentifier.empty() &&
946 !Ptr->getName().equals(ARCAnnotationTargetIdentifier))
949 Module *M = BB->getParent()->getParent();
950 LLVMContext &C = M->getContext();
951 Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C));
952 Type *I8XX = PointerType::getUnqual(I8X);
953 Type *Params[] = {I8XX, I8XX};
954 FunctionType *FTy = FunctionType::get(Type::getVoidTy(C),
955 ArrayRef<Type*>(Params, 2),
957 Constant *Callee = M->getOrInsertFunction(Name, FTy);
959 IRBuilder<> Builder(BB, std::prev(BB->end()));
962 StringRef Tmp = Ptr->getName();
963 if (nullptr == (PtrName = M->getGlobalVariable(Tmp, true))) {
964 Value *ActualPtrName = Builder.CreateGlobalStringPtr(Tmp,
966 PtrName = new GlobalVariable(*M, I8X, true, GlobalVariable::InternalLinkage,
967 cast<Constant>(ActualPtrName), Tmp);
971 std::string SeqStr = SequenceToString(Seq);
972 if (nullptr == (S = M->getGlobalVariable(SeqStr, true))) {
973 Value *ActualPtrName = Builder.CreateGlobalStringPtr(SeqStr,
975 S = new GlobalVariable(*M, I8X, true, GlobalVariable::InternalLinkage,
976 cast<Constant>(ActualPtrName), SeqStr);
978 Builder.CreateCall2(Callee, PtrName, S);
981 /// Adds a source annotation to pointer and a state change annotation to Inst
982 /// referencing the source annotation and the old/new state of pointer.
983 static void GenerateARCAnnotation(unsigned InstMDId,
989 if (EnableARCAnnotations) {
990 // If we have a target identifier, make sure that we match it before
991 // emitting an annotation.
992 if(!ARCAnnotationTargetIdentifier.empty() &&
993 !Ptr->getName().equals(ARCAnnotationTargetIdentifier))
996 // First generate the source annotation on our pointer. This will return an
997 // MDString* if Ptr actually comes from an instruction implying we can put
998 // in a source annotation. If AppendMDNodeToSourcePtr returns 0 (i.e. NULL),
999 // then we know that our pointer is from an Argument so we put a reference
1000 // to the argument number.
1002 // The point of this is to make it easy for the
1003 // llvm-arc-annotation-processor tool to cross reference where the source
1004 // pointer is in the LLVM IR since the LLVM IR parser does not submit such
1005 // information via debug info for backends to use (since why would anyone
1006 // need such a thing from LLVM IR besides in non-standard cases
1008 MDString *SourcePtrMDNode =
1009 AppendMDNodeToSourcePtr(PtrMDId, Ptr);
1010 AppendMDNodeToInstForPtr(InstMDId, Inst, Ptr, SourcePtrMDNode, OldSeq,
1015 // The actual interface for accessing the above functionality is defined via
1016 // some simple macros which are defined below. We do this so that the user does
1017 // not need to pass in what metadata id is needed resulting in cleaner code and
1018 // additionally since it provides an easy way to conditionally no-op all
1019 // annotation support in a non-debug build.
1021 /// Use this macro to annotate a sequence state change when processing
1022 /// instructions bottom up,
1023 #define ANNOTATE_BOTTOMUP(inst, ptr, old, new) \
1024 GenerateARCAnnotation(ARCAnnotationBottomUpMDKind, \
1025 ARCAnnotationProvenanceSourceMDKind, (inst), \
1026 const_cast<Value*>(ptr), (old), (new))
1027 /// Use this macro to annotate a sequence state change when processing
1028 /// instructions top down.
1029 #define ANNOTATE_TOPDOWN(inst, ptr, old, new) \
1030 GenerateARCAnnotation(ARCAnnotationTopDownMDKind, \
1031 ARCAnnotationProvenanceSourceMDKind, (inst), \
1032 const_cast<Value*>(ptr), (old), (new))
1034 #define ANNOTATE_BB(_states, _bb, _name, _type, _direction) \
1036 if (EnableARCAnnotations) { \
1037 for(BBState::ptr_const_iterator I = (_states)._direction##_ptr_begin(), \
1038 E = (_states)._direction##_ptr_end(); I != E; ++I) { \
1039 Value *Ptr = const_cast<Value*>(I->first); \
1040 Sequence Seq = I->second.GetSeq(); \
1041 GenerateARCBB ## _type ## Annotation(_name, (_bb), Ptr, Seq); \
1046 #define ANNOTATE_BOTTOMUP_BBSTART(_states, _basicblock) \
1047 ANNOTATE_BB(_states, _basicblock, "llvm.arc.annotation.bottomup.bbstart", \
1048 Entrance, bottom_up)
1049 #define ANNOTATE_BOTTOMUP_BBEND(_states, _basicblock) \
1050 ANNOTATE_BB(_states, _basicblock, "llvm.arc.annotation.bottomup.bbend", \
1051 Terminator, bottom_up)
1052 #define ANNOTATE_TOPDOWN_BBSTART(_states, _basicblock) \
1053 ANNOTATE_BB(_states, _basicblock, "llvm.arc.annotation.topdown.bbstart", \
1055 #define ANNOTATE_TOPDOWN_BBEND(_states, _basicblock) \
1056 ANNOTATE_BB(_states, _basicblock, "llvm.arc.annotation.topdown.bbend", \
1057 Terminator, top_down)
1059 #else // !ARC_ANNOTATION
1060 // If annotations are off, noop.
1061 #define ANNOTATE_BOTTOMUP(inst, ptr, old, new)
1062 #define ANNOTATE_TOPDOWN(inst, ptr, old, new)
1063 #define ANNOTATE_BOTTOMUP_BBSTART(states, basicblock)
1064 #define ANNOTATE_BOTTOMUP_BBEND(states, basicblock)
1065 #define ANNOTATE_TOPDOWN_BBSTART(states, basicblock)
1066 #define ANNOTATE_TOPDOWN_BBEND(states, basicblock)
1067 #endif // !ARC_ANNOTATION
1070 /// \brief The main ARC optimization pass.
1071 class ObjCARCOpt : public FunctionPass {
1073 ProvenanceAnalysis PA;
1074 ARCRuntimeEntryPoints EP;
1076 // This is used to track if a pointer is stored into an alloca.
1077 DenseSet<const Value *> MultiOwnersSet;
1079 /// A flag indicating whether this optimization pass should run.
1082 /// Flags which determine whether each of the interesting runtine functions
1083 /// is in fact used in the current function.
1084 unsigned UsedInThisFunction;
1086 /// The Metadata Kind for clang.imprecise_release metadata.
1087 unsigned ImpreciseReleaseMDKind;
1089 /// The Metadata Kind for clang.arc.copy_on_escape metadata.
1090 unsigned CopyOnEscapeMDKind;
1092 /// The Metadata Kind for clang.arc.no_objc_arc_exceptions metadata.
1093 unsigned NoObjCARCExceptionsMDKind;
1095 #ifdef ARC_ANNOTATIONS
1096 /// The Metadata Kind for llvm.arc.annotation.bottomup metadata.
1097 unsigned ARCAnnotationBottomUpMDKind;
1098 /// The Metadata Kind for llvm.arc.annotation.topdown metadata.
1099 unsigned ARCAnnotationTopDownMDKind;
1100 /// The Metadata Kind for llvm.arc.annotation.provenancesource metadata.
1101 unsigned ARCAnnotationProvenanceSourceMDKind;
1102 #endif // ARC_ANNOATIONS
1104 bool OptimizeRetainRVCall(Function &F, Instruction *RetainRV);
1105 void OptimizeAutoreleaseRVCall(Function &F, Instruction *AutoreleaseRV,
1106 InstructionClass &Class);
1107 void OptimizeIndividualCalls(Function &F);
1109 void CheckForCFGHazards(const BasicBlock *BB,
1110 DenseMap<const BasicBlock *, BBState> &BBStates,
1111 BBState &MyStates) const;
1112 bool VisitInstructionBottomUp(Instruction *Inst,
1114 MapVector<Value *, RRInfo> &Retains,
1116 bool VisitBottomUp(BasicBlock *BB,
1117 DenseMap<const BasicBlock *, BBState> &BBStates,
1118 MapVector<Value *, RRInfo> &Retains);
1119 bool VisitInstructionTopDown(Instruction *Inst,
1120 DenseMap<Value *, RRInfo> &Releases,
1122 bool VisitTopDown(BasicBlock *BB,
1123 DenseMap<const BasicBlock *, BBState> &BBStates,
1124 DenseMap<Value *, RRInfo> &Releases);
1125 bool Visit(Function &F,
1126 DenseMap<const BasicBlock *, BBState> &BBStates,
1127 MapVector<Value *, RRInfo> &Retains,
1128 DenseMap<Value *, RRInfo> &Releases);
1130 void MoveCalls(Value *Arg, RRInfo &RetainsToMove, RRInfo &ReleasesToMove,
1131 MapVector<Value *, RRInfo> &Retains,
1132 DenseMap<Value *, RRInfo> &Releases,
1133 SmallVectorImpl<Instruction *> &DeadInsts,
1136 bool ConnectTDBUTraversals(DenseMap<const BasicBlock *, BBState> &BBStates,
1137 MapVector<Value *, RRInfo> &Retains,
1138 DenseMap<Value *, RRInfo> &Releases,
1140 SmallVectorImpl<Instruction *> &NewRetains,
1141 SmallVectorImpl<Instruction *> &NewReleases,
1142 SmallVectorImpl<Instruction *> &DeadInsts,
1143 RRInfo &RetainsToMove,
1144 RRInfo &ReleasesToMove,
1147 bool &AnyPairsCompletelyEliminated);
1149 bool PerformCodePlacement(DenseMap<const BasicBlock *, BBState> &BBStates,
1150 MapVector<Value *, RRInfo> &Retains,
1151 DenseMap<Value *, RRInfo> &Releases,
1154 void OptimizeWeakCalls(Function &F);
1156 bool OptimizeSequences(Function &F);
1158 void OptimizeReturns(Function &F);
1161 void GatherStatistics(Function &F, bool AfterOptimization = false);
1164 void getAnalysisUsage(AnalysisUsage &AU) const override;
1165 bool doInitialization(Module &M) override;
1166 bool runOnFunction(Function &F) override;
1167 void releaseMemory() override;
1171 ObjCARCOpt() : FunctionPass(ID) {
1172 initializeObjCARCOptPass(*PassRegistry::getPassRegistry());
1177 char ObjCARCOpt::ID = 0;
1178 INITIALIZE_PASS_BEGIN(ObjCARCOpt,
1179 "objc-arc", "ObjC ARC optimization", false, false)
1180 INITIALIZE_PASS_DEPENDENCY(ObjCARCAliasAnalysis)
1181 INITIALIZE_PASS_END(ObjCARCOpt,
1182 "objc-arc", "ObjC ARC optimization", false, false)
1184 Pass *llvm::createObjCARCOptPass() {
1185 return new ObjCARCOpt();
1188 void ObjCARCOpt::getAnalysisUsage(AnalysisUsage &AU) const {
1189 AU.addRequired<ObjCARCAliasAnalysis>();
1190 AU.addRequired<AliasAnalysis>();
1191 // ARC optimization doesn't currently split critical edges.
1192 AU.setPreservesCFG();
1195 /// Turn objc_retainAutoreleasedReturnValue into objc_retain if the operand is
1196 /// not a return value. Or, if it can be paired with an
1197 /// objc_autoreleaseReturnValue, delete the pair and return true.
1199 ObjCARCOpt::OptimizeRetainRVCall(Function &F, Instruction *RetainRV) {
1200 // Check for the argument being from an immediately preceding call or invoke.
1201 const Value *Arg = GetObjCArg(RetainRV);
1202 ImmutableCallSite CS(Arg);
1203 if (const Instruction *Call = CS.getInstruction()) {
1204 if (Call->getParent() == RetainRV->getParent()) {
1205 BasicBlock::const_iterator I = Call;
1207 while (IsNoopInstruction(I)) ++I;
1208 if (&*I == RetainRV)
1210 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(Call)) {
1211 BasicBlock *RetainRVParent = RetainRV->getParent();
1212 if (II->getNormalDest() == RetainRVParent) {
1213 BasicBlock::const_iterator I = RetainRVParent->begin();
1214 while (IsNoopInstruction(I)) ++I;
1215 if (&*I == RetainRV)
1221 // Check for being preceded by an objc_autoreleaseReturnValue on the same
1222 // pointer. In this case, we can delete the pair.
1223 BasicBlock::iterator I = RetainRV, Begin = RetainRV->getParent()->begin();
1225 do --I; while (I != Begin && IsNoopInstruction(I));
1226 if (GetBasicInstructionClass(I) == IC_AutoreleaseRV &&
1227 GetObjCArg(I) == Arg) {
1231 DEBUG(dbgs() << "Erasing autoreleaseRV,retainRV pair: " << *I << "\n"
1232 << "Erasing " << *RetainRV << "\n");
1234 EraseInstruction(I);
1235 EraseInstruction(RetainRV);
1240 // Turn it to a plain objc_retain.
1244 DEBUG(dbgs() << "Transforming objc_retainAutoreleasedReturnValue => "
1245 "objc_retain since the operand is not a return value.\n"
1246 "Old = " << *RetainRV << "\n");
1248 Constant *NewDecl = EP.get(ARCRuntimeEntryPoints::EPT_Retain);
1249 cast<CallInst>(RetainRV)->setCalledFunction(NewDecl);
1251 DEBUG(dbgs() << "New = " << *RetainRV << "\n");
1256 /// Turn objc_autoreleaseReturnValue into objc_autorelease if the result is not
1257 /// used as a return value.
1259 ObjCARCOpt::OptimizeAutoreleaseRVCall(Function &F, Instruction *AutoreleaseRV,
1260 InstructionClass &Class) {
1261 // Check for a return of the pointer value.
1262 const Value *Ptr = GetObjCArg(AutoreleaseRV);
1263 SmallVector<const Value *, 2> Users;
1264 Users.push_back(Ptr);
1266 Ptr = Users.pop_back_val();
1267 for (const User *U : Ptr->users()) {
1268 if (isa<ReturnInst>(U) || GetBasicInstructionClass(U) == IC_RetainRV)
1270 if (isa<BitCastInst>(U))
1273 } while (!Users.empty());
1278 DEBUG(dbgs() << "Transforming objc_autoreleaseReturnValue => "
1279 "objc_autorelease since its operand is not used as a return "
1281 "Old = " << *AutoreleaseRV << "\n");
1283 CallInst *AutoreleaseRVCI = cast<CallInst>(AutoreleaseRV);
1284 Constant *NewDecl = EP.get(ARCRuntimeEntryPoints::EPT_Autorelease);
1285 AutoreleaseRVCI->setCalledFunction(NewDecl);
1286 AutoreleaseRVCI->setTailCall(false); // Never tail call objc_autorelease.
1287 Class = IC_Autorelease;
1289 DEBUG(dbgs() << "New: " << *AutoreleaseRV << "\n");
1293 /// Visit each call, one at a time, and make simplifications without doing any
1294 /// additional analysis.
1295 void ObjCARCOpt::OptimizeIndividualCalls(Function &F) {
1296 DEBUG(dbgs() << "\n== ObjCARCOpt::OptimizeIndividualCalls ==\n");
1297 // Reset all the flags in preparation for recomputing them.
1298 UsedInThisFunction = 0;
1300 // Visit all objc_* calls in F.
1301 for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
1302 Instruction *Inst = &*I++;
1304 InstructionClass Class = GetBasicInstructionClass(Inst);
1306 DEBUG(dbgs() << "Visiting: Class: " << Class << "; " << *Inst << "\n");
1311 // Delete no-op casts. These function calls have special semantics, but
1312 // the semantics are entirely implemented via lowering in the front-end,
1313 // so by the time they reach the optimizer, they are just no-op calls
1314 // which return their argument.
1316 // There are gray areas here, as the ability to cast reference-counted
1317 // pointers to raw void* and back allows code to break ARC assumptions,
1318 // however these are currently considered to be unimportant.
1322 DEBUG(dbgs() << "Erasing no-op cast: " << *Inst << "\n");
1323 EraseInstruction(Inst);
1326 // If the pointer-to-weak-pointer is null, it's undefined behavior.
1329 case IC_LoadWeakRetained:
1331 case IC_DestroyWeak: {
1332 CallInst *CI = cast<CallInst>(Inst);
1333 if (IsNullOrUndef(CI->getArgOperand(0))) {
1335 Type *Ty = CI->getArgOperand(0)->getType();
1336 new StoreInst(UndefValue::get(cast<PointerType>(Ty)->getElementType()),
1337 Constant::getNullValue(Ty),
1339 llvm::Value *NewValue = UndefValue::get(CI->getType());
1340 DEBUG(dbgs() << "A null pointer-to-weak-pointer is undefined behavior."
1341 "\nOld = " << *CI << "\nNew = " << *NewValue << "\n");
1342 CI->replaceAllUsesWith(NewValue);
1343 CI->eraseFromParent();
1350 CallInst *CI = cast<CallInst>(Inst);
1351 if (IsNullOrUndef(CI->getArgOperand(0)) ||
1352 IsNullOrUndef(CI->getArgOperand(1))) {
1354 Type *Ty = CI->getArgOperand(0)->getType();
1355 new StoreInst(UndefValue::get(cast<PointerType>(Ty)->getElementType()),
1356 Constant::getNullValue(Ty),
1359 llvm::Value *NewValue = UndefValue::get(CI->getType());
1360 DEBUG(dbgs() << "A null pointer-to-weak-pointer is undefined behavior."
1361 "\nOld = " << *CI << "\nNew = " << *NewValue << "\n");
1363 CI->replaceAllUsesWith(NewValue);
1364 CI->eraseFromParent();
1370 if (OptimizeRetainRVCall(F, Inst))
1373 case IC_AutoreleaseRV:
1374 OptimizeAutoreleaseRVCall(F, Inst, Class);
1378 // objc_autorelease(x) -> objc_release(x) if x is otherwise unused.
1379 if (IsAutorelease(Class) && Inst->use_empty()) {
1380 CallInst *Call = cast<CallInst>(Inst);
1381 const Value *Arg = Call->getArgOperand(0);
1382 Arg = FindSingleUseIdentifiedObject(Arg);
1387 // Create the declaration lazily.
1388 LLVMContext &C = Inst->getContext();
1390 Constant *Decl = EP.get(ARCRuntimeEntryPoints::EPT_Release);
1391 CallInst *NewCall = CallInst::Create(Decl, Call->getArgOperand(0), "",
1393 NewCall->setMetadata(ImpreciseReleaseMDKind, MDNode::get(C, None));
1395 DEBUG(dbgs() << "Replacing autorelease{,RV}(x) with objc_release(x) "
1396 "since x is otherwise unused.\nOld: " << *Call << "\nNew: "
1397 << *NewCall << "\n");
1399 EraseInstruction(Call);
1405 // For functions which can never be passed stack arguments, add
1407 if (IsAlwaysTail(Class)) {
1409 DEBUG(dbgs() << "Adding tail keyword to function since it can never be "
1410 "passed stack args: " << *Inst << "\n");
1411 cast<CallInst>(Inst)->setTailCall();
1414 // Ensure that functions that can never have a "tail" keyword due to the
1415 // semantics of ARC truly do not do so.
1416 if (IsNeverTail(Class)) {
1418 DEBUG(dbgs() << "Removing tail keyword from function: " << *Inst <<
1420 cast<CallInst>(Inst)->setTailCall(false);
1423 // Set nounwind as needed.
1424 if (IsNoThrow(Class)) {
1426 DEBUG(dbgs() << "Found no throw class. Setting nounwind on: " << *Inst
1428 cast<CallInst>(Inst)->setDoesNotThrow();
1431 if (!IsNoopOnNull(Class)) {
1432 UsedInThisFunction |= 1 << Class;
1436 const Value *Arg = GetObjCArg(Inst);
1438 // ARC calls with null are no-ops. Delete them.
1439 if (IsNullOrUndef(Arg)) {
1442 DEBUG(dbgs() << "ARC calls with null are no-ops. Erasing: " << *Inst
1444 EraseInstruction(Inst);
1448 // Keep track of which of retain, release, autorelease, and retain_block
1449 // are actually present in this function.
1450 UsedInThisFunction |= 1 << Class;
1452 // If Arg is a PHI, and one or more incoming values to the
1453 // PHI are null, and the call is control-equivalent to the PHI, and there
1454 // are no relevant side effects between the PHI and the call, the call
1455 // could be pushed up to just those paths with non-null incoming values.
1456 // For now, don't bother splitting critical edges for this.
1457 SmallVector<std::pair<Instruction *, const Value *>, 4> Worklist;
1458 Worklist.push_back(std::make_pair(Inst, Arg));
1460 std::pair<Instruction *, const Value *> Pair = Worklist.pop_back_val();
1464 const PHINode *PN = dyn_cast<PHINode>(Arg);
1467 // Determine if the PHI has any null operands, or any incoming
1469 bool HasNull = false;
1470 bool HasCriticalEdges = false;
1471 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
1473 StripPointerCastsAndObjCCalls(PN->getIncomingValue(i));
1474 if (IsNullOrUndef(Incoming))
1476 else if (cast<TerminatorInst>(PN->getIncomingBlock(i)->back())
1477 .getNumSuccessors() != 1) {
1478 HasCriticalEdges = true;
1482 // If we have null operands and no critical edges, optimize.
1483 if (!HasCriticalEdges && HasNull) {
1484 SmallPtrSet<Instruction *, 4> DependingInstructions;
1485 SmallPtrSet<const BasicBlock *, 4> Visited;
1487 // Check that there is nothing that cares about the reference
1488 // count between the call and the phi.
1491 case IC_RetainBlock:
1492 // These can always be moved up.
1495 // These can't be moved across things that care about the retain
1497 FindDependencies(NeedsPositiveRetainCount, Arg,
1498 Inst->getParent(), Inst,
1499 DependingInstructions, Visited, PA);
1501 case IC_Autorelease:
1502 // These can't be moved across autorelease pool scope boundaries.
1503 FindDependencies(AutoreleasePoolBoundary, Arg,
1504 Inst->getParent(), Inst,
1505 DependingInstructions, Visited, PA);
1508 case IC_AutoreleaseRV:
1509 // Don't move these; the RV optimization depends on the autoreleaseRV
1510 // being tail called, and the retainRV being immediately after a call
1511 // (which might still happen if we get lucky with codegen layout, but
1512 // it's not worth taking the chance).
1515 llvm_unreachable("Invalid dependence flavor");
1518 if (DependingInstructions.size() == 1 &&
1519 *DependingInstructions.begin() == PN) {
1522 // Clone the call into each predecessor that has a non-null value.
1523 CallInst *CInst = cast<CallInst>(Inst);
1524 Type *ParamTy = CInst->getArgOperand(0)->getType();
1525 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
1527 StripPointerCastsAndObjCCalls(PN->getIncomingValue(i));
1528 if (!IsNullOrUndef(Incoming)) {
1529 CallInst *Clone = cast<CallInst>(CInst->clone());
1530 Value *Op = PN->getIncomingValue(i);
1531 Instruction *InsertPos = &PN->getIncomingBlock(i)->back();
1532 if (Op->getType() != ParamTy)
1533 Op = new BitCastInst(Op, ParamTy, "", InsertPos);
1534 Clone->setArgOperand(0, Op);
1535 Clone->insertBefore(InsertPos);
1537 DEBUG(dbgs() << "Cloning "
1539 "And inserting clone at " << *InsertPos << "\n");
1540 Worklist.push_back(std::make_pair(Clone, Incoming));
1543 // Erase the original call.
1544 DEBUG(dbgs() << "Erasing: " << *CInst << "\n");
1545 EraseInstruction(CInst);
1549 } while (!Worklist.empty());
1553 /// If we have a top down pointer in the S_Use state, make sure that there are
1554 /// no CFG hazards by checking the states of various bottom up pointers.
1555 static void CheckForUseCFGHazard(const Sequence SuccSSeq,
1556 const bool SuccSRRIKnownSafe,
1558 bool &SomeSuccHasSame,
1559 bool &AllSuccsHaveSame,
1560 bool &NotAllSeqEqualButKnownSafe,
1561 bool &ShouldContinue) {
1563 case S_CanRelease: {
1564 if (!S.IsKnownSafe() && !SuccSRRIKnownSafe) {
1565 S.ClearSequenceProgress();
1568 S.SetCFGHazardAfflicted(true);
1569 ShouldContinue = true;
1573 SomeSuccHasSame = true;
1577 case S_MovableRelease:
1578 if (!S.IsKnownSafe() && !SuccSRRIKnownSafe)
1579 AllSuccsHaveSame = false;
1581 NotAllSeqEqualButKnownSafe = true;
1584 llvm_unreachable("bottom-up pointer in retain state!");
1586 llvm_unreachable("This should have been handled earlier.");
1590 /// If we have a Top Down pointer in the S_CanRelease state, make sure that
1591 /// there are no CFG hazards by checking the states of various bottom up
1593 static void CheckForCanReleaseCFGHazard(const Sequence SuccSSeq,
1594 const bool SuccSRRIKnownSafe,
1596 bool &SomeSuccHasSame,
1597 bool &AllSuccsHaveSame,
1598 bool &NotAllSeqEqualButKnownSafe) {
1601 SomeSuccHasSame = true;
1605 case S_MovableRelease:
1607 if (!S.IsKnownSafe() && !SuccSRRIKnownSafe)
1608 AllSuccsHaveSame = false;
1610 NotAllSeqEqualButKnownSafe = true;
1613 llvm_unreachable("bottom-up pointer in retain state!");
1615 llvm_unreachable("This should have been handled earlier.");
1619 /// Check for critical edges, loop boundaries, irreducible control flow, or
1620 /// other CFG structures where moving code across the edge would result in it
1621 /// being executed more.
1623 ObjCARCOpt::CheckForCFGHazards(const BasicBlock *BB,
1624 DenseMap<const BasicBlock *, BBState> &BBStates,
1625 BBState &MyStates) const {
1626 // If any top-down local-use or possible-dec has a succ which is earlier in
1627 // the sequence, forget it.
1628 for (BBState::ptr_iterator I = MyStates.top_down_ptr_begin(),
1629 E = MyStates.top_down_ptr_end(); I != E; ++I) {
1630 PtrState &S = I->second;
1631 const Sequence Seq = I->second.GetSeq();
1633 // We only care about S_Retain, S_CanRelease, and S_Use.
1637 // Make sure that if extra top down states are added in the future that this
1638 // code is updated to handle it.
1639 assert((Seq == S_Retain || Seq == S_CanRelease || Seq == S_Use) &&
1640 "Unknown top down sequence state.");
1642 const Value *Arg = I->first;
1643 const TerminatorInst *TI = cast<TerminatorInst>(&BB->back());
1644 bool SomeSuccHasSame = false;
1645 bool AllSuccsHaveSame = true;
1646 bool NotAllSeqEqualButKnownSafe = false;
1648 succ_const_iterator SI(TI), SE(TI, false);
1650 for (; SI != SE; ++SI) {
1651 // If VisitBottomUp has pointer information for this successor, take
1652 // what we know about it.
1653 const DenseMap<const BasicBlock *, BBState>::iterator BBI =
1655 assert(BBI != BBStates.end());
1656 const PtrState &SuccS = BBI->second.getPtrBottomUpState(Arg);
1657 const Sequence SuccSSeq = SuccS.GetSeq();
1659 // If bottom up, the pointer is in an S_None state, clear the sequence
1660 // progress since the sequence in the bottom up state finished
1661 // suggesting a mismatch in between retains/releases. This is true for
1662 // all three cases that we are handling here: S_Retain, S_Use, and
1664 if (SuccSSeq == S_None) {
1665 S.ClearSequenceProgress();
1669 // If we have S_Use or S_CanRelease, perform our check for cfg hazard
1671 const bool SuccSRRIKnownSafe = SuccS.IsKnownSafe();
1673 // *NOTE* We do not use Seq from above here since we are allowing for
1674 // S.GetSeq() to change while we are visiting basic blocks.
1675 switch(S.GetSeq()) {
1677 bool ShouldContinue = false;
1678 CheckForUseCFGHazard(SuccSSeq, SuccSRRIKnownSafe, S, SomeSuccHasSame,
1679 AllSuccsHaveSame, NotAllSeqEqualButKnownSafe,
1685 case S_CanRelease: {
1686 CheckForCanReleaseCFGHazard(SuccSSeq, SuccSRRIKnownSafe, S,
1687 SomeSuccHasSame, AllSuccsHaveSame,
1688 NotAllSeqEqualButKnownSafe);
1695 case S_MovableRelease:
1700 // If the state at the other end of any of the successor edges
1701 // matches the current state, require all edges to match. This
1702 // guards against loops in the middle of a sequence.
1703 if (SomeSuccHasSame && !AllSuccsHaveSame) {
1704 S.ClearSequenceProgress();
1705 } else if (NotAllSeqEqualButKnownSafe) {
1706 // If we would have cleared the state foregoing the fact that we are known
1707 // safe, stop code motion. This is because whether or not it is safe to
1708 // remove RR pairs via KnownSafe is an orthogonal concept to whether we
1709 // are allowed to perform code motion.
1710 S.SetCFGHazardAfflicted(true);
1716 ObjCARCOpt::VisitInstructionBottomUp(Instruction *Inst,
1718 MapVector<Value *, RRInfo> &Retains,
1719 BBState &MyStates) {
1720 bool NestingDetected = false;
1721 InstructionClass Class = GetInstructionClass(Inst);
1722 const Value *Arg = nullptr;
1724 DEBUG(dbgs() << "Class: " << Class << "\n");
1728 Arg = GetObjCArg(Inst);
1730 PtrState &S = MyStates.getPtrBottomUpState(Arg);
1732 // If we see two releases in a row on the same pointer. If so, make
1733 // a note, and we'll cicle back to revisit it after we've
1734 // hopefully eliminated the second release, which may allow us to
1735 // eliminate the first release too.
1736 // Theoretically we could implement removal of nested retain+release
1737 // pairs by making PtrState hold a stack of states, but this is
1738 // simple and avoids adding overhead for the non-nested case.
1739 if (S.GetSeq() == S_Release || S.GetSeq() == S_MovableRelease) {
1740 DEBUG(dbgs() << "Found nested releases (i.e. a release pair)\n");
1741 NestingDetected = true;
1744 MDNode *ReleaseMetadata = Inst->getMetadata(ImpreciseReleaseMDKind);
1745 Sequence NewSeq = ReleaseMetadata ? S_MovableRelease : S_Release;
1746 ANNOTATE_BOTTOMUP(Inst, Arg, S.GetSeq(), NewSeq);
1747 S.ResetSequenceProgress(NewSeq);
1748 S.SetReleaseMetadata(ReleaseMetadata);
1749 S.SetKnownSafe(S.HasKnownPositiveRefCount());
1750 S.SetTailCallRelease(cast<CallInst>(Inst)->isTailCall());
1752 S.SetKnownPositiveRefCount();
1755 case IC_RetainBlock:
1756 // In OptimizeIndividualCalls, we have strength reduced all optimizable
1757 // objc_retainBlocks to objc_retains. Thus at this point any
1758 // objc_retainBlocks that we see are not optimizable.
1762 Arg = GetObjCArg(Inst);
1764 PtrState &S = MyStates.getPtrBottomUpState(Arg);
1765 S.SetKnownPositiveRefCount();
1767 Sequence OldSeq = S.GetSeq();
1771 case S_MovableRelease:
1773 // If OldSeq is not S_Use or OldSeq is S_Use and we are tracking an
1774 // imprecise release, clear our reverse insertion points.
1775 if (OldSeq != S_Use || S.IsTrackingImpreciseReleases())
1776 S.ClearReverseInsertPts();
1779 // Don't do retain+release tracking for IC_RetainRV, because it's
1780 // better to let it remain as the first instruction after a call.
1781 if (Class != IC_RetainRV)
1782 Retains[Inst] = S.GetRRInfo();
1783 S.ClearSequenceProgress();
1788 llvm_unreachable("bottom-up pointer in retain state!");
1790 ANNOTATE_BOTTOMUP(Inst, Arg, OldSeq, S.GetSeq());
1791 // A retain moving bottom up can be a use.
1794 case IC_AutoreleasepoolPop:
1795 // Conservatively, clear MyStates for all known pointers.
1796 MyStates.clearBottomUpPointers();
1797 return NestingDetected;
1798 case IC_AutoreleasepoolPush:
1800 // These are irrelevant.
1801 return NestingDetected;
1803 // If we have a store into an alloca of a pointer we are tracking, the
1804 // pointer has multiple owners implying that we must be more conservative.
1806 // This comes up in the context of a pointer being ``KnownSafe''. In the
1807 // presence of a block being initialized, the frontend will emit the
1808 // objc_retain on the original pointer and the release on the pointer loaded
1809 // from the alloca. The optimizer will through the provenance analysis
1810 // realize that the two are related, but since we only require KnownSafe in
1811 // one direction, will match the inner retain on the original pointer with
1812 // the guard release on the original pointer. This is fixed by ensuring that
1813 // in the presence of allocas we only unconditionally remove pointers if
1814 // both our retain and our release are KnownSafe.
1815 if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
1816 if (AreAnyUnderlyingObjectsAnAlloca(SI->getPointerOperand())) {
1817 BBState::ptr_iterator I = MyStates.findPtrBottomUpState(
1818 StripPointerCastsAndObjCCalls(SI->getValueOperand()));
1819 if (I != MyStates.bottom_up_ptr_end())
1820 MultiOwnersSet.insert(I->first);
1828 // Consider any other possible effects of this instruction on each
1829 // pointer being tracked.
1830 for (BBState::ptr_iterator MI = MyStates.bottom_up_ptr_begin(),
1831 ME = MyStates.bottom_up_ptr_end(); MI != ME; ++MI) {
1832 const Value *Ptr = MI->first;
1834 continue; // Handled above.
1835 PtrState &S = MI->second;
1836 Sequence Seq = S.GetSeq();
1838 // Check for possible releases.
1839 if (CanAlterRefCount(Inst, Ptr, PA, Class)) {
1840 DEBUG(dbgs() << "CanAlterRefCount: Seq: " << Seq << "; " << *Ptr
1842 S.ClearKnownPositiveRefCount();
1845 S.SetSeq(S_CanRelease);
1846 ANNOTATE_BOTTOMUP(Inst, Ptr, Seq, S.GetSeq());
1850 case S_MovableRelease:
1855 llvm_unreachable("bottom-up pointer in retain state!");
1859 // Check for possible direct uses.
1862 case S_MovableRelease:
1863 if (CanUse(Inst, Ptr, PA, Class)) {
1864 DEBUG(dbgs() << "CanUse: Seq: " << Seq << "; " << *Ptr
1866 assert(!S.HasReverseInsertPts());
1867 // If this is an invoke instruction, we're scanning it as part of
1868 // one of its successor blocks, since we can't insert code after it
1869 // in its own block, and we don't want to split critical edges.
1870 if (isa<InvokeInst>(Inst))
1871 S.InsertReverseInsertPt(BB->getFirstInsertionPt());
1873 S.InsertReverseInsertPt(std::next(BasicBlock::iterator(Inst)));
1875 ANNOTATE_BOTTOMUP(Inst, Ptr, Seq, S_Use);
1876 } else if (Seq == S_Release && IsUser(Class)) {
1877 DEBUG(dbgs() << "PreciseReleaseUse: Seq: " << Seq << "; " << *Ptr
1879 // Non-movable releases depend on any possible objc pointer use.
1881 ANNOTATE_BOTTOMUP(Inst, Ptr, S_Release, S_Stop);
1882 assert(!S.HasReverseInsertPts());
1883 // As above; handle invoke specially.
1884 if (isa<InvokeInst>(Inst))
1885 S.InsertReverseInsertPt(BB->getFirstInsertionPt());
1887 S.InsertReverseInsertPt(std::next(BasicBlock::iterator(Inst)));
1891 if (CanUse(Inst, Ptr, PA, Class)) {
1892 DEBUG(dbgs() << "PreciseStopUse: Seq: " << Seq << "; " << *Ptr
1895 ANNOTATE_BOTTOMUP(Inst, Ptr, Seq, S_Use);
1903 llvm_unreachable("bottom-up pointer in retain state!");
1907 return NestingDetected;
1911 ObjCARCOpt::VisitBottomUp(BasicBlock *BB,
1912 DenseMap<const BasicBlock *, BBState> &BBStates,
1913 MapVector<Value *, RRInfo> &Retains) {
1915 DEBUG(dbgs() << "\n== ObjCARCOpt::VisitBottomUp ==\n");
1917 bool NestingDetected = false;
1918 BBState &MyStates = BBStates[BB];
1920 // Merge the states from each successor to compute the initial state
1921 // for the current block.
1922 BBState::edge_iterator SI(MyStates.succ_begin()),
1923 SE(MyStates.succ_end());
1925 const BasicBlock *Succ = *SI;
1926 DenseMap<const BasicBlock *, BBState>::iterator I = BBStates.find(Succ);
1927 assert(I != BBStates.end());
1928 MyStates.InitFromSucc(I->second);
1930 for (; SI != SE; ++SI) {
1932 I = BBStates.find(Succ);
1933 assert(I != BBStates.end());
1934 MyStates.MergeSucc(I->second);
1938 // If ARC Annotations are enabled, output the current state of pointers at the
1939 // bottom of the basic block.
1940 ANNOTATE_BOTTOMUP_BBEND(MyStates, BB);
1942 // Visit all the instructions, bottom-up.
1943 for (BasicBlock::iterator I = BB->end(), E = BB->begin(); I != E; --I) {
1944 Instruction *Inst = std::prev(I);
1946 // Invoke instructions are visited as part of their successors (below).
1947 if (isa<InvokeInst>(Inst))
1950 DEBUG(dbgs() << "Visiting " << *Inst << "\n");
1952 NestingDetected |= VisitInstructionBottomUp(Inst, BB, Retains, MyStates);
1955 // If there's a predecessor with an invoke, visit the invoke as if it were
1956 // part of this block, since we can't insert code after an invoke in its own
1957 // block, and we don't want to split critical edges.
1958 for (BBState::edge_iterator PI(MyStates.pred_begin()),
1959 PE(MyStates.pred_end()); PI != PE; ++PI) {
1960 BasicBlock *Pred = *PI;
1961 if (InvokeInst *II = dyn_cast<InvokeInst>(&Pred->back()))
1962 NestingDetected |= VisitInstructionBottomUp(II, BB, Retains, MyStates);
1965 // If ARC Annotations are enabled, output the current state of pointers at the
1966 // top of the basic block.
1967 ANNOTATE_BOTTOMUP_BBSTART(MyStates, BB);
1969 return NestingDetected;
1973 ObjCARCOpt::VisitInstructionTopDown(Instruction *Inst,
1974 DenseMap<Value *, RRInfo> &Releases,
1975 BBState &MyStates) {
1976 bool NestingDetected = false;
1977 InstructionClass Class = GetInstructionClass(Inst);
1978 const Value *Arg = nullptr;
1981 case IC_RetainBlock:
1982 // In OptimizeIndividualCalls, we have strength reduced all optimizable
1983 // objc_retainBlocks to objc_retains. Thus at this point any
1984 // objc_retainBlocks that we see are not optimizable.
1988 Arg = GetObjCArg(Inst);
1990 PtrState &S = MyStates.getPtrTopDownState(Arg);
1992 // Don't do retain+release tracking for IC_RetainRV, because it's
1993 // better to let it remain as the first instruction after a call.
1994 if (Class != IC_RetainRV) {
1995 // If we see two retains in a row on the same pointer. If so, make
1996 // a note, and we'll cicle back to revisit it after we've
1997 // hopefully eliminated the second retain, which may allow us to
1998 // eliminate the first retain too.
1999 // Theoretically we could implement removal of nested retain+release
2000 // pairs by making PtrState hold a stack of states, but this is
2001 // simple and avoids adding overhead for the non-nested case.
2002 if (S.GetSeq() == S_Retain)
2003 NestingDetected = true;
2005 ANNOTATE_TOPDOWN(Inst, Arg, S.GetSeq(), S_Retain);
2006 S.ResetSequenceProgress(S_Retain);
2007 S.SetKnownSafe(S.HasKnownPositiveRefCount());
2011 S.SetKnownPositiveRefCount();
2013 // A retain can be a potential use; procede to the generic checking
2018 Arg = GetObjCArg(Inst);
2020 PtrState &S = MyStates.getPtrTopDownState(Arg);
2021 S.ClearKnownPositiveRefCount();
2023 Sequence OldSeq = S.GetSeq();
2025 MDNode *ReleaseMetadata = Inst->getMetadata(ImpreciseReleaseMDKind);
2030 if (OldSeq == S_Retain || ReleaseMetadata != nullptr)
2031 S.ClearReverseInsertPts();
2034 S.SetReleaseMetadata(ReleaseMetadata);
2035 S.SetTailCallRelease(cast<CallInst>(Inst)->isTailCall());
2036 Releases[Inst] = S.GetRRInfo();
2037 ANNOTATE_TOPDOWN(Inst, Arg, S.GetSeq(), S_None);
2038 S.ClearSequenceProgress();
2044 case S_MovableRelease:
2045 llvm_unreachable("top-down pointer in release state!");
2049 case IC_AutoreleasepoolPop:
2050 // Conservatively, clear MyStates for all known pointers.
2051 MyStates.clearTopDownPointers();
2052 return NestingDetected;
2053 case IC_AutoreleasepoolPush:
2055 // These are irrelevant.
2056 return NestingDetected;
2061 // Consider any other possible effects of this instruction on each
2062 // pointer being tracked.
2063 for (BBState::ptr_iterator MI = MyStates.top_down_ptr_begin(),
2064 ME = MyStates.top_down_ptr_end(); MI != ME; ++MI) {
2065 const Value *Ptr = MI->first;
2067 continue; // Handled above.
2068 PtrState &S = MI->second;
2069 Sequence Seq = S.GetSeq();
2071 // Check for possible releases.
2072 if (CanAlterRefCount(Inst, Ptr, PA, Class)) {
2073 DEBUG(dbgs() << "CanAlterRefCount: Seq: " << Seq << "; " << *Ptr
2075 S.ClearKnownPositiveRefCount();
2078 S.SetSeq(S_CanRelease);
2079 ANNOTATE_TOPDOWN(Inst, Ptr, Seq, S_CanRelease);
2080 assert(!S.HasReverseInsertPts());
2081 S.InsertReverseInsertPt(Inst);
2083 // One call can't cause a transition from S_Retain to S_CanRelease
2084 // and S_CanRelease to S_Use. If we've made the first transition,
2093 case S_MovableRelease:
2094 llvm_unreachable("top-down pointer in release state!");
2098 // Check for possible direct uses.
2101 if (CanUse(Inst, Ptr, PA, Class)) {
2102 DEBUG(dbgs() << "CanUse: Seq: " << Seq << "; " << *Ptr
2105 ANNOTATE_TOPDOWN(Inst, Ptr, Seq, S_Use);
2114 case S_MovableRelease:
2115 llvm_unreachable("top-down pointer in release state!");
2119 return NestingDetected;
2123 ObjCARCOpt::VisitTopDown(BasicBlock *BB,
2124 DenseMap<const BasicBlock *, BBState> &BBStates,
2125 DenseMap<Value *, RRInfo> &Releases) {
2126 DEBUG(dbgs() << "\n== ObjCARCOpt::VisitTopDown ==\n");
2127 bool NestingDetected = false;
2128 BBState &MyStates = BBStates[BB];
2130 // Merge the states from each predecessor to compute the initial state
2131 // for the current block.
2132 BBState::edge_iterator PI(MyStates.pred_begin()),
2133 PE(MyStates.pred_end());
2135 const BasicBlock *Pred = *PI;
2136 DenseMap<const BasicBlock *, BBState>::iterator I = BBStates.find(Pred);
2137 assert(I != BBStates.end());
2138 MyStates.InitFromPred(I->second);
2140 for (; PI != PE; ++PI) {
2142 I = BBStates.find(Pred);
2143 assert(I != BBStates.end());
2144 MyStates.MergePred(I->second);
2148 // If ARC Annotations are enabled, output the current state of pointers at the
2149 // top of the basic block.
2150 ANNOTATE_TOPDOWN_BBSTART(MyStates, BB);
2152 // Visit all the instructions, top-down.
2153 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
2154 Instruction *Inst = I;
2156 DEBUG(dbgs() << "Visiting " << *Inst << "\n");
2158 NestingDetected |= VisitInstructionTopDown(Inst, Releases, MyStates);
2161 // If ARC Annotations are enabled, output the current state of pointers at the
2162 // bottom of the basic block.
2163 ANNOTATE_TOPDOWN_BBEND(MyStates, BB);
2165 #ifdef ARC_ANNOTATIONS
2166 if (!(EnableARCAnnotations && DisableCheckForCFGHazards))
2168 CheckForCFGHazards(BB, BBStates, MyStates);
2169 return NestingDetected;
2173 ComputePostOrders(Function &F,
2174 SmallVectorImpl<BasicBlock *> &PostOrder,
2175 SmallVectorImpl<BasicBlock *> &ReverseCFGPostOrder,
2176 unsigned NoObjCARCExceptionsMDKind,
2177 DenseMap<const BasicBlock *, BBState> &BBStates) {
2178 /// The visited set, for doing DFS walks.
2179 SmallPtrSet<BasicBlock *, 16> Visited;
2181 // Do DFS, computing the PostOrder.
2182 SmallPtrSet<BasicBlock *, 16> OnStack;
2183 SmallVector<std::pair<BasicBlock *, succ_iterator>, 16> SuccStack;
2185 // Functions always have exactly one entry block, and we don't have
2186 // any other block that we treat like an entry block.
2187 BasicBlock *EntryBB = &F.getEntryBlock();
2188 BBState &MyStates = BBStates[EntryBB];
2189 MyStates.SetAsEntry();
2190 TerminatorInst *EntryTI = cast<TerminatorInst>(&EntryBB->back());
2191 SuccStack.push_back(std::make_pair(EntryBB, succ_iterator(EntryTI)));
2192 Visited.insert(EntryBB);
2193 OnStack.insert(EntryBB);
2196 BasicBlock *CurrBB = SuccStack.back().first;
2197 TerminatorInst *TI = cast<TerminatorInst>(&CurrBB->back());
2198 succ_iterator SE(TI, false);
2200 while (SuccStack.back().second != SE) {
2201 BasicBlock *SuccBB = *SuccStack.back().second++;
2202 if (Visited.insert(SuccBB)) {
2203 TerminatorInst *TI = cast<TerminatorInst>(&SuccBB->back());
2204 SuccStack.push_back(std::make_pair(SuccBB, succ_iterator(TI)));
2205 BBStates[CurrBB].addSucc(SuccBB);
2206 BBState &SuccStates = BBStates[SuccBB];
2207 SuccStates.addPred(CurrBB);
2208 OnStack.insert(SuccBB);
2212 if (!OnStack.count(SuccBB)) {
2213 BBStates[CurrBB].addSucc(SuccBB);
2214 BBStates[SuccBB].addPred(CurrBB);
2217 OnStack.erase(CurrBB);
2218 PostOrder.push_back(CurrBB);
2219 SuccStack.pop_back();
2220 } while (!SuccStack.empty());
2224 // Do reverse-CFG DFS, computing the reverse-CFG PostOrder.
2225 // Functions may have many exits, and there also blocks which we treat
2226 // as exits due to ignored edges.
2227 SmallVector<std::pair<BasicBlock *, BBState::edge_iterator>, 16> PredStack;
2228 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
2229 BasicBlock *ExitBB = I;
2230 BBState &MyStates = BBStates[ExitBB];
2231 if (!MyStates.isExit())
2234 MyStates.SetAsExit();
2236 PredStack.push_back(std::make_pair(ExitBB, MyStates.pred_begin()));
2237 Visited.insert(ExitBB);
2238 while (!PredStack.empty()) {
2239 reverse_dfs_next_succ:
2240 BBState::edge_iterator PE = BBStates[PredStack.back().first].pred_end();
2241 while (PredStack.back().second != PE) {
2242 BasicBlock *BB = *PredStack.back().second++;
2243 if (Visited.insert(BB)) {
2244 PredStack.push_back(std::make_pair(BB, BBStates[BB].pred_begin()));
2245 goto reverse_dfs_next_succ;
2248 ReverseCFGPostOrder.push_back(PredStack.pop_back_val().first);
2253 // Visit the function both top-down and bottom-up.
2255 ObjCARCOpt::Visit(Function &F,
2256 DenseMap<const BasicBlock *, BBState> &BBStates,
2257 MapVector<Value *, RRInfo> &Retains,
2258 DenseMap<Value *, RRInfo> &Releases) {
2260 // Use reverse-postorder traversals, because we magically know that loops
2261 // will be well behaved, i.e. they won't repeatedly call retain on a single
2262 // pointer without doing a release. We can't use the ReversePostOrderTraversal
2263 // class here because we want the reverse-CFG postorder to consider each
2264 // function exit point, and we want to ignore selected cycle edges.
2265 SmallVector<BasicBlock *, 16> PostOrder;
2266 SmallVector<BasicBlock *, 16> ReverseCFGPostOrder;
2267 ComputePostOrders(F, PostOrder, ReverseCFGPostOrder,
2268 NoObjCARCExceptionsMDKind,
2271 // Use reverse-postorder on the reverse CFG for bottom-up.
2272 bool BottomUpNestingDetected = false;
2273 for (SmallVectorImpl<BasicBlock *>::const_reverse_iterator I =
2274 ReverseCFGPostOrder.rbegin(), E = ReverseCFGPostOrder.rend();
2276 BottomUpNestingDetected |= VisitBottomUp(*I, BBStates, Retains);
2278 // Use reverse-postorder for top-down.
2279 bool TopDownNestingDetected = false;
2280 for (SmallVectorImpl<BasicBlock *>::const_reverse_iterator I =
2281 PostOrder.rbegin(), E = PostOrder.rend();
2283 TopDownNestingDetected |= VisitTopDown(*I, BBStates, Releases);
2285 return TopDownNestingDetected && BottomUpNestingDetected;
2288 /// Move the calls in RetainsToMove and ReleasesToMove.
2289 void ObjCARCOpt::MoveCalls(Value *Arg,
2290 RRInfo &RetainsToMove,
2291 RRInfo &ReleasesToMove,
2292 MapVector<Value *, RRInfo> &Retains,
2293 DenseMap<Value *, RRInfo> &Releases,
2294 SmallVectorImpl<Instruction *> &DeadInsts,
2296 Type *ArgTy = Arg->getType();
2297 Type *ParamTy = PointerType::getUnqual(Type::getInt8Ty(ArgTy->getContext()));
2299 DEBUG(dbgs() << "== ObjCARCOpt::MoveCalls ==\n");
2301 // Insert the new retain and release calls.
2302 for (SmallPtrSet<Instruction *, 2>::const_iterator
2303 PI = ReleasesToMove.ReverseInsertPts.begin(),
2304 PE = ReleasesToMove.ReverseInsertPts.end(); PI != PE; ++PI) {
2305 Instruction *InsertPt = *PI;
2306 Value *MyArg = ArgTy == ParamTy ? Arg :
2307 new BitCastInst(Arg, ParamTy, "", InsertPt);
2308 Constant *Decl = EP.get(ARCRuntimeEntryPoints::EPT_Retain);
2309 CallInst *Call = CallInst::Create(Decl, MyArg, "", InsertPt);
2310 Call->setDoesNotThrow();
2311 Call->setTailCall();
2313 DEBUG(dbgs() << "Inserting new Retain: " << *Call << "\n"
2314 "At insertion point: " << *InsertPt << "\n");
2316 for (SmallPtrSet<Instruction *, 2>::const_iterator
2317 PI = RetainsToMove.ReverseInsertPts.begin(),
2318 PE = RetainsToMove.ReverseInsertPts.end(); PI != PE; ++PI) {
2319 Instruction *InsertPt = *PI;
2320 Value *MyArg = ArgTy == ParamTy ? Arg :
2321 new BitCastInst(Arg, ParamTy, "", InsertPt);
2322 Constant *Decl = EP.get(ARCRuntimeEntryPoints::EPT_Release);
2323 CallInst *Call = CallInst::Create(Decl, MyArg, "", InsertPt);
2324 // Attach a clang.imprecise_release metadata tag, if appropriate.
2325 if (MDNode *M = ReleasesToMove.ReleaseMetadata)
2326 Call->setMetadata(ImpreciseReleaseMDKind, M);
2327 Call->setDoesNotThrow();
2328 if (ReleasesToMove.IsTailCallRelease)
2329 Call->setTailCall();
2331 DEBUG(dbgs() << "Inserting new Release: " << *Call << "\n"
2332 "At insertion point: " << *InsertPt << "\n");
2335 // Delete the original retain and release calls.
2336 for (SmallPtrSet<Instruction *, 2>::const_iterator
2337 AI = RetainsToMove.Calls.begin(),
2338 AE = RetainsToMove.Calls.end(); AI != AE; ++AI) {
2339 Instruction *OrigRetain = *AI;
2340 Retains.blot(OrigRetain);
2341 DeadInsts.push_back(OrigRetain);
2342 DEBUG(dbgs() << "Deleting retain: " << *OrigRetain << "\n");
2344 for (SmallPtrSet<Instruction *, 2>::const_iterator
2345 AI = ReleasesToMove.Calls.begin(),
2346 AE = ReleasesToMove.Calls.end(); AI != AE; ++AI) {
2347 Instruction *OrigRelease = *AI;
2348 Releases.erase(OrigRelease);
2349 DeadInsts.push_back(OrigRelease);
2350 DEBUG(dbgs() << "Deleting release: " << *OrigRelease << "\n");
2356 ObjCARCOpt::ConnectTDBUTraversals(DenseMap<const BasicBlock *, BBState>
2358 MapVector<Value *, RRInfo> &Retains,
2359 DenseMap<Value *, RRInfo> &Releases,
2361 SmallVectorImpl<Instruction *> &NewRetains,
2362 SmallVectorImpl<Instruction *> &NewReleases,
2363 SmallVectorImpl<Instruction *> &DeadInsts,
2364 RRInfo &RetainsToMove,
2365 RRInfo &ReleasesToMove,
2368 bool &AnyPairsCompletelyEliminated) {
2369 // If a pair happens in a region where it is known that the reference count
2370 // is already incremented, we can similarly ignore possible decrements unless
2371 // we are dealing with a retainable object with multiple provenance sources.
2372 bool KnownSafeTD = true, KnownSafeBU = true;
2373 bool MultipleOwners = false;
2374 bool CFGHazardAfflicted = false;
2376 // Connect the dots between the top-down-collected RetainsToMove and
2377 // bottom-up-collected ReleasesToMove to form sets of related calls.
2378 // This is an iterative process so that we connect multiple releases
2379 // to multiple retains if needed.
2380 unsigned OldDelta = 0;
2381 unsigned NewDelta = 0;
2382 unsigned OldCount = 0;
2383 unsigned NewCount = 0;
2384 bool FirstRelease = true;
2386 for (SmallVectorImpl<Instruction *>::const_iterator
2387 NI = NewRetains.begin(), NE = NewRetains.end(); NI != NE; ++NI) {
2388 Instruction *NewRetain = *NI;
2389 MapVector<Value *, RRInfo>::const_iterator It = Retains.find(NewRetain);
2390 assert(It != Retains.end());
2391 const RRInfo &NewRetainRRI = It->second;
2392 KnownSafeTD &= NewRetainRRI.KnownSafe;
2394 MultipleOwners || MultiOwnersSet.count(GetObjCArg(NewRetain));
2395 for (SmallPtrSet<Instruction *, 2>::const_iterator
2396 LI = NewRetainRRI.Calls.begin(),
2397 LE = NewRetainRRI.Calls.end(); LI != LE; ++LI) {
2398 Instruction *NewRetainRelease = *LI;
2399 DenseMap<Value *, RRInfo>::const_iterator Jt =
2400 Releases.find(NewRetainRelease);
2401 if (Jt == Releases.end())
2403 const RRInfo &NewRetainReleaseRRI = Jt->second;
2405 // If the release does not have a reference to the retain as well,
2406 // something happened which is unaccounted for. Do not do anything.
2408 // This can happen if we catch an additive overflow during path count
2410 if (!NewRetainReleaseRRI.Calls.count(NewRetain))
2413 if (ReleasesToMove.Calls.insert(NewRetainRelease)) {
2415 // If we overflow when we compute the path count, don't remove/move
2417 const BBState &NRRBBState = BBStates[NewRetainRelease->getParent()];
2418 unsigned PathCount = BBState::OverflowOccurredValue;
2419 if (NRRBBState.GetAllPathCountWithOverflow(PathCount))
2421 assert(PathCount != BBState::OverflowOccurredValue &&
2422 "PathCount at this point can not be "
2423 "OverflowOccurredValue.");
2424 OldDelta -= PathCount;
2426 // Merge the ReleaseMetadata and IsTailCallRelease values.
2428 ReleasesToMove.ReleaseMetadata =
2429 NewRetainReleaseRRI.ReleaseMetadata;
2430 ReleasesToMove.IsTailCallRelease =
2431 NewRetainReleaseRRI.IsTailCallRelease;
2432 FirstRelease = false;
2434 if (ReleasesToMove.ReleaseMetadata !=
2435 NewRetainReleaseRRI.ReleaseMetadata)
2436 ReleasesToMove.ReleaseMetadata = nullptr;
2437 if (ReleasesToMove.IsTailCallRelease !=
2438 NewRetainReleaseRRI.IsTailCallRelease)
2439 ReleasesToMove.IsTailCallRelease = false;
2442 // Collect the optimal insertion points.
2444 for (SmallPtrSet<Instruction *, 2>::const_iterator
2445 RI = NewRetainReleaseRRI.ReverseInsertPts.begin(),
2446 RE = NewRetainReleaseRRI.ReverseInsertPts.end();
2448 Instruction *RIP = *RI;
2449 if (ReleasesToMove.ReverseInsertPts.insert(RIP)) {
2450 // If we overflow when we compute the path count, don't
2451 // remove/move anything.
2452 const BBState &RIPBBState = BBStates[RIP->getParent()];
2453 PathCount = BBState::OverflowOccurredValue;
2454 if (RIPBBState.GetAllPathCountWithOverflow(PathCount))
2456 assert(PathCount != BBState::OverflowOccurredValue &&
2457 "PathCount at this point can not be "
2458 "OverflowOccurredValue.");
2459 NewDelta -= PathCount;
2462 NewReleases.push_back(NewRetainRelease);
2467 if (NewReleases.empty()) break;
2469 // Back the other way.
2470 for (SmallVectorImpl<Instruction *>::const_iterator
2471 NI = NewReleases.begin(), NE = NewReleases.end(); NI != NE; ++NI) {
2472 Instruction *NewRelease = *NI;
2473 DenseMap<Value *, RRInfo>::const_iterator It =
2474 Releases.find(NewRelease);
2475 assert(It != Releases.end());
2476 const RRInfo &NewReleaseRRI = It->second;
2477 KnownSafeBU &= NewReleaseRRI.KnownSafe;
2478 CFGHazardAfflicted |= NewReleaseRRI.CFGHazardAfflicted;
2479 for (SmallPtrSet<Instruction *, 2>::const_iterator
2480 LI = NewReleaseRRI.Calls.begin(),
2481 LE = NewReleaseRRI.Calls.end(); LI != LE; ++LI) {
2482 Instruction *NewReleaseRetain = *LI;
2483 MapVector<Value *, RRInfo>::const_iterator Jt =
2484 Retains.find(NewReleaseRetain);
2485 if (Jt == Retains.end())
2487 const RRInfo &NewReleaseRetainRRI = Jt->second;
2489 // If the retain does not have a reference to the release as well,
2490 // something happened which is unaccounted for. Do not do anything.
2492 // This can happen if we catch an additive overflow during path count
2494 if (!NewReleaseRetainRRI.Calls.count(NewRelease))
2497 if (RetainsToMove.Calls.insert(NewReleaseRetain)) {
2498 // If we overflow when we compute the path count, don't remove/move
2500 const BBState &NRRBBState = BBStates[NewReleaseRetain->getParent()];
2501 unsigned PathCount = BBState::OverflowOccurredValue;
2502 if (NRRBBState.GetAllPathCountWithOverflow(PathCount))
2504 assert(PathCount != BBState::OverflowOccurredValue &&
2505 "PathCount at this point can not be "
2506 "OverflowOccurredValue.");
2507 OldDelta += PathCount;
2508 OldCount += PathCount;
2510 // Collect the optimal insertion points.
2512 for (SmallPtrSet<Instruction *, 2>::const_iterator
2513 RI = NewReleaseRetainRRI.ReverseInsertPts.begin(),
2514 RE = NewReleaseRetainRRI.ReverseInsertPts.end();
2516 Instruction *RIP = *RI;
2517 if (RetainsToMove.ReverseInsertPts.insert(RIP)) {
2518 // If we overflow when we compute the path count, don't
2519 // remove/move anything.
2520 const BBState &RIPBBState = BBStates[RIP->getParent()];
2522 PathCount = BBState::OverflowOccurredValue;
2523 if (RIPBBState.GetAllPathCountWithOverflow(PathCount))
2525 assert(PathCount != BBState::OverflowOccurredValue &&
2526 "PathCount at this point can not be "
2527 "OverflowOccurredValue.");
2528 NewDelta += PathCount;
2529 NewCount += PathCount;
2532 NewRetains.push_back(NewReleaseRetain);
2536 NewReleases.clear();
2537 if (NewRetains.empty()) break;
2540 // If the pointer is known incremented in 1 direction and we do not have
2541 // MultipleOwners, we can safely remove the retain/releases. Otherwise we need
2542 // to be known safe in both directions.
2543 bool UnconditionallySafe = (KnownSafeTD && KnownSafeBU) ||
2544 ((KnownSafeTD || KnownSafeBU) && !MultipleOwners);
2545 if (UnconditionallySafe) {
2546 RetainsToMove.ReverseInsertPts.clear();
2547 ReleasesToMove.ReverseInsertPts.clear();
2550 // Determine whether the new insertion points we computed preserve the
2551 // balance of retain and release calls through the program.
2552 // TODO: If the fully aggressive solution isn't valid, try to find a
2553 // less aggressive solution which is.
2557 // At this point, we are not going to remove any RR pairs, but we still are
2558 // able to move RR pairs. If one of our pointers is afflicted with
2559 // CFGHazards, we cannot perform such code motion so exit early.
2560 const bool WillPerformCodeMotion = RetainsToMove.ReverseInsertPts.size() ||
2561 ReleasesToMove.ReverseInsertPts.size();
2562 if (CFGHazardAfflicted && WillPerformCodeMotion)
2566 // Determine whether the original call points are balanced in the retain and
2567 // release calls through the program. If not, conservatively don't touch
2569 // TODO: It's theoretically possible to do code motion in this case, as
2570 // long as the existing imbalances are maintained.
2574 #ifdef ARC_ANNOTATIONS
2575 // Do not move calls if ARC annotations are requested.
2576 if (EnableARCAnnotations)
2578 #endif // ARC_ANNOTATIONS
2581 assert(OldCount != 0 && "Unreachable code?");
2582 NumRRs += OldCount - NewCount;
2583 // Set to true if we completely removed any RR pairs.
2584 AnyPairsCompletelyEliminated = NewCount == 0;
2586 // We can move calls!
2590 /// Identify pairings between the retains and releases, and delete and/or move
2593 ObjCARCOpt::PerformCodePlacement(DenseMap<const BasicBlock *, BBState>
2595 MapVector<Value *, RRInfo> &Retains,
2596 DenseMap<Value *, RRInfo> &Releases,
2598 DEBUG(dbgs() << "\n== ObjCARCOpt::PerformCodePlacement ==\n");
2600 bool AnyPairsCompletelyEliminated = false;
2601 RRInfo RetainsToMove;
2602 RRInfo ReleasesToMove;
2603 SmallVector<Instruction *, 4> NewRetains;
2604 SmallVector<Instruction *, 4> NewReleases;
2605 SmallVector<Instruction *, 8> DeadInsts;
2607 // Visit each retain.
2608 for (MapVector<Value *, RRInfo>::const_iterator I = Retains.begin(),
2609 E = Retains.end(); I != E; ++I) {
2610 Value *V = I->first;
2611 if (!V) continue; // blotted
2613 Instruction *Retain = cast<Instruction>(V);
2615 DEBUG(dbgs() << "Visiting: " << *Retain << "\n");
2617 Value *Arg = GetObjCArg(Retain);
2619 // If the object being released is in static or stack storage, we know it's
2620 // not being managed by ObjC reference counting, so we can delete pairs
2621 // regardless of what possible decrements or uses lie between them.
2622 bool KnownSafe = isa<Constant>(Arg) || isa<AllocaInst>(Arg);
2624 // A constant pointer can't be pointing to an object on the heap. It may
2625 // be reference-counted, but it won't be deleted.
2626 if (const LoadInst *LI = dyn_cast<LoadInst>(Arg))
2627 if (const GlobalVariable *GV =
2628 dyn_cast<GlobalVariable>(
2629 StripPointerCastsAndObjCCalls(LI->getPointerOperand())))
2630 if (GV->isConstant())
2633 // Connect the dots between the top-down-collected RetainsToMove and
2634 // bottom-up-collected ReleasesToMove to form sets of related calls.
2635 NewRetains.push_back(Retain);
2636 bool PerformMoveCalls =
2637 ConnectTDBUTraversals(BBStates, Retains, Releases, M, NewRetains,
2638 NewReleases, DeadInsts, RetainsToMove,
2639 ReleasesToMove, Arg, KnownSafe,
2640 AnyPairsCompletelyEliminated);
2642 if (PerformMoveCalls) {
2643 // Ok, everything checks out and we're all set. Let's move/delete some
2645 MoveCalls(Arg, RetainsToMove, ReleasesToMove,
2646 Retains, Releases, DeadInsts, M);
2649 // Clean up state for next retain.
2650 NewReleases.clear();
2652 RetainsToMove.clear();
2653 ReleasesToMove.clear();
2656 // Now that we're done moving everything, we can delete the newly dead
2657 // instructions, as we no longer need them as insert points.
2658 while (!DeadInsts.empty())
2659 EraseInstruction(DeadInsts.pop_back_val());
2661 return AnyPairsCompletelyEliminated;
2664 /// Weak pointer optimizations.
2665 void ObjCARCOpt::OptimizeWeakCalls(Function &F) {
2666 DEBUG(dbgs() << "\n== ObjCARCOpt::OptimizeWeakCalls ==\n");
2668 // First, do memdep-style RLE and S2L optimizations. We can't use memdep
2669 // itself because it uses AliasAnalysis and we need to do provenance
2671 for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
2672 Instruction *Inst = &*I++;
2674 DEBUG(dbgs() << "Visiting: " << *Inst << "\n");
2676 InstructionClass Class = GetBasicInstructionClass(Inst);
2677 if (Class != IC_LoadWeak && Class != IC_LoadWeakRetained)
2680 // Delete objc_loadWeak calls with no users.
2681 if (Class == IC_LoadWeak && Inst->use_empty()) {
2682 Inst->eraseFromParent();
2686 // TODO: For now, just look for an earlier available version of this value
2687 // within the same block. Theoretically, we could do memdep-style non-local
2688 // analysis too, but that would want caching. A better approach would be to
2689 // use the technique that EarlyCSE uses.
2690 inst_iterator Current = std::prev(I);
2691 BasicBlock *CurrentBB = Current.getBasicBlockIterator();
2692 for (BasicBlock::iterator B = CurrentBB->begin(),
2693 J = Current.getInstructionIterator();
2695 Instruction *EarlierInst = &*std::prev(J);
2696 InstructionClass EarlierClass = GetInstructionClass(EarlierInst);
2697 switch (EarlierClass) {
2699 case IC_LoadWeakRetained: {
2700 // If this is loading from the same pointer, replace this load's value
2702 CallInst *Call = cast<CallInst>(Inst);
2703 CallInst *EarlierCall = cast<CallInst>(EarlierInst);
2704 Value *Arg = Call->getArgOperand(0);
2705 Value *EarlierArg = EarlierCall->getArgOperand(0);
2706 switch (PA.getAA()->alias(Arg, EarlierArg)) {
2707 case AliasAnalysis::MustAlias:
2709 // If the load has a builtin retain, insert a plain retain for it.
2710 if (Class == IC_LoadWeakRetained) {
2711 Constant *Decl = EP.get(ARCRuntimeEntryPoints::EPT_Retain);
2712 CallInst *CI = CallInst::Create(Decl, EarlierCall, "", Call);
2715 // Zap the fully redundant load.
2716 Call->replaceAllUsesWith(EarlierCall);
2717 Call->eraseFromParent();
2719 case AliasAnalysis::MayAlias:
2720 case AliasAnalysis::PartialAlias:
2722 case AliasAnalysis::NoAlias:
2729 // If this is storing to the same pointer and has the same size etc.
2730 // replace this load's value with the stored value.
2731 CallInst *Call = cast<CallInst>(Inst);
2732 CallInst *EarlierCall = cast<CallInst>(EarlierInst);
2733 Value *Arg = Call->getArgOperand(0);
2734 Value *EarlierArg = EarlierCall->getArgOperand(0);
2735 switch (PA.getAA()->alias(Arg, EarlierArg)) {
2736 case AliasAnalysis::MustAlias:
2738 // If the load has a builtin retain, insert a plain retain for it.
2739 if (Class == IC_LoadWeakRetained) {
2740 Constant *Decl = EP.get(ARCRuntimeEntryPoints::EPT_Retain);
2741 CallInst *CI = CallInst::Create(Decl, EarlierCall, "", Call);
2744 // Zap the fully redundant load.
2745 Call->replaceAllUsesWith(EarlierCall->getArgOperand(1));
2746 Call->eraseFromParent();
2748 case AliasAnalysis::MayAlias:
2749 case AliasAnalysis::PartialAlias:
2751 case AliasAnalysis::NoAlias:
2758 // TOOD: Grab the copied value.
2760 case IC_AutoreleasepoolPush:
2762 case IC_IntrinsicUser:
2764 // Weak pointers are only modified through the weak entry points
2765 // (and arbitrary calls, which could call the weak entry points).
2768 // Anything else could modify the weak pointer.
2775 // Then, for each destroyWeak with an alloca operand, check to see if
2776 // the alloca and all its users can be zapped.
2777 for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
2778 Instruction *Inst = &*I++;
2779 InstructionClass Class = GetBasicInstructionClass(Inst);
2780 if (Class != IC_DestroyWeak)
2783 CallInst *Call = cast<CallInst>(Inst);
2784 Value *Arg = Call->getArgOperand(0);
2785 if (AllocaInst *Alloca = dyn_cast<AllocaInst>(Arg)) {
2786 for (User *U : Alloca->users()) {
2787 const Instruction *UserInst = cast<Instruction>(U);
2788 switch (GetBasicInstructionClass(UserInst)) {
2791 case IC_DestroyWeak:
2798 for (auto UI = Alloca->user_begin(), UE = Alloca->user_end(); UI != UE;) {
2799 CallInst *UserInst = cast<CallInst>(*UI++);
2800 switch (GetBasicInstructionClass(UserInst)) {
2803 // These functions return their second argument.
2804 UserInst->replaceAllUsesWith(UserInst->getArgOperand(1));
2806 case IC_DestroyWeak:
2810 llvm_unreachable("alloca really is used!");
2812 UserInst->eraseFromParent();
2814 Alloca->eraseFromParent();
2820 /// Identify program paths which execute sequences of retains and releases which
2821 /// can be eliminated.
2822 bool ObjCARCOpt::OptimizeSequences(Function &F) {
2823 // Releases, Retains - These are used to store the results of the main flow
2824 // analysis. These use Value* as the key instead of Instruction* so that the
2825 // map stays valid when we get around to rewriting code and calls get
2826 // replaced by arguments.
2827 DenseMap<Value *, RRInfo> Releases;
2828 MapVector<Value *, RRInfo> Retains;
2830 // This is used during the traversal of the function to track the
2831 // states for each identified object at each block.
2832 DenseMap<const BasicBlock *, BBState> BBStates;
2834 // Analyze the CFG of the function, and all instructions.
2835 bool NestingDetected = Visit(F, BBStates, Retains, Releases);
2838 bool AnyPairsCompletelyEliminated = PerformCodePlacement(BBStates, Retains,
2843 MultiOwnersSet.clear();
2845 return AnyPairsCompletelyEliminated && NestingDetected;
2848 /// Check if there is a dependent call earlier that does not have anything in
2849 /// between the Retain and the call that can affect the reference count of their
2850 /// shared pointer argument. Note that Retain need not be in BB.
2852 HasSafePathToPredecessorCall(const Value *Arg, Instruction *Retain,
2853 SmallPtrSet<Instruction *, 4> &DepInsts,
2854 SmallPtrSet<const BasicBlock *, 4> &Visited,
2855 ProvenanceAnalysis &PA) {
2856 FindDependencies(CanChangeRetainCount, Arg, Retain->getParent(), Retain,
2857 DepInsts, Visited, PA);
2858 if (DepInsts.size() != 1)
2862 dyn_cast_or_null<CallInst>(*DepInsts.begin());
2864 // Check that the pointer is the return value of the call.
2865 if (!Call || Arg != Call)
2868 // Check that the call is a regular call.
2869 InstructionClass Class = GetBasicInstructionClass(Call);
2870 if (Class != IC_CallOrUser && Class != IC_Call)
2876 /// Find a dependent retain that precedes the given autorelease for which there
2877 /// is nothing in between the two instructions that can affect the ref count of
2880 FindPredecessorRetainWithSafePath(const Value *Arg, BasicBlock *BB,
2881 Instruction *Autorelease,
2882 SmallPtrSet<Instruction *, 4> &DepInsts,
2883 SmallPtrSet<const BasicBlock *, 4> &Visited,
2884 ProvenanceAnalysis &PA) {
2885 FindDependencies(CanChangeRetainCount, Arg,
2886 BB, Autorelease, DepInsts, Visited, PA);
2887 if (DepInsts.size() != 1)
2891 dyn_cast_or_null<CallInst>(*DepInsts.begin());
2893 // Check that we found a retain with the same argument.
2895 !IsRetain(GetBasicInstructionClass(Retain)) ||
2896 GetObjCArg(Retain) != Arg) {
2903 /// Look for an ``autorelease'' instruction dependent on Arg such that there are
2904 /// no instructions dependent on Arg that need a positive ref count in between
2905 /// the autorelease and the ret.
2907 FindPredecessorAutoreleaseWithSafePath(const Value *Arg, BasicBlock *BB,
2909 SmallPtrSet<Instruction *, 4> &DepInsts,
2910 SmallPtrSet<const BasicBlock *, 4> &V,
2911 ProvenanceAnalysis &PA) {
2912 FindDependencies(NeedsPositiveRetainCount, Arg,
2913 BB, Ret, DepInsts, V, PA);
2914 if (DepInsts.size() != 1)
2917 CallInst *Autorelease =
2918 dyn_cast_or_null<CallInst>(*DepInsts.begin());
2921 InstructionClass AutoreleaseClass = GetBasicInstructionClass(Autorelease);
2922 if (!IsAutorelease(AutoreleaseClass))
2924 if (GetObjCArg(Autorelease) != Arg)
2930 /// Look for this pattern:
2932 /// %call = call i8* @something(...)
2933 /// %2 = call i8* @objc_retain(i8* %call)
2934 /// %3 = call i8* @objc_autorelease(i8* %2)
2937 /// And delete the retain and autorelease.
2938 void ObjCARCOpt::OptimizeReturns(Function &F) {
2939 if (!F.getReturnType()->isPointerTy())
2942 DEBUG(dbgs() << "\n== ObjCARCOpt::OptimizeReturns ==\n");
2944 SmallPtrSet<Instruction *, 4> DependingInstructions;
2945 SmallPtrSet<const BasicBlock *, 4> Visited;
2946 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI) {
2947 BasicBlock *BB = FI;
2948 ReturnInst *Ret = dyn_cast<ReturnInst>(&BB->back());
2950 DEBUG(dbgs() << "Visiting: " << *Ret << "\n");
2955 const Value *Arg = StripPointerCastsAndObjCCalls(Ret->getOperand(0));
2957 // Look for an ``autorelease'' instruction that is a predecessor of Ret and
2958 // dependent on Arg such that there are no instructions dependent on Arg
2959 // that need a positive ref count in between the autorelease and Ret.
2960 CallInst *Autorelease =
2961 FindPredecessorAutoreleaseWithSafePath(Arg, BB, Ret,
2962 DependingInstructions, Visited,
2964 DependingInstructions.clear();
2971 FindPredecessorRetainWithSafePath(Arg, BB, Autorelease,
2972 DependingInstructions, Visited, PA);
2973 DependingInstructions.clear();
2979 // Check that there is nothing that can affect the reference count
2980 // between the retain and the call. Note that Retain need not be in BB.
2981 bool HasSafePathToCall = HasSafePathToPredecessorCall(Arg, Retain,
2982 DependingInstructions,
2984 DependingInstructions.clear();
2987 if (!HasSafePathToCall)
2990 // If so, we can zap the retain and autorelease.
2993 DEBUG(dbgs() << "Erasing: " << *Retain << "\nErasing: "
2994 << *Autorelease << "\n");
2995 EraseInstruction(Retain);
2996 EraseInstruction(Autorelease);
3002 ObjCARCOpt::GatherStatistics(Function &F, bool AfterOptimization) {
3003 llvm::Statistic &NumRetains =
3004 AfterOptimization? NumRetainsAfterOpt : NumRetainsBeforeOpt;
3005 llvm::Statistic &NumReleases =
3006 AfterOptimization? NumReleasesAfterOpt : NumReleasesBeforeOpt;
3008 for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
3009 Instruction *Inst = &*I++;
3010 switch (GetBasicInstructionClass(Inst)) {
3024 bool ObjCARCOpt::doInitialization(Module &M) {
3028 // If nothing in the Module uses ARC, don't do anything.
3029 Run = ModuleHasARC(M);
3033 // Identify the imprecise release metadata kind.
3034 ImpreciseReleaseMDKind =
3035 M.getContext().getMDKindID("clang.imprecise_release");
3036 CopyOnEscapeMDKind =
3037 M.getContext().getMDKindID("clang.arc.copy_on_escape");
3038 NoObjCARCExceptionsMDKind =
3039 M.getContext().getMDKindID("clang.arc.no_objc_arc_exceptions");
3040 #ifdef ARC_ANNOTATIONS
3041 ARCAnnotationBottomUpMDKind =
3042 M.getContext().getMDKindID("llvm.arc.annotation.bottomup");
3043 ARCAnnotationTopDownMDKind =
3044 M.getContext().getMDKindID("llvm.arc.annotation.topdown");
3045 ARCAnnotationProvenanceSourceMDKind =
3046 M.getContext().getMDKindID("llvm.arc.annotation.provenancesource");
3047 #endif // ARC_ANNOTATIONS
3049 // Intuitively, objc_retain and others are nocapture, however in practice
3050 // they are not, because they return their argument value. And objc_release
3051 // calls finalizers which can have arbitrary side effects.
3053 // Initialize our runtime entry point cache.
3059 bool ObjCARCOpt::runOnFunction(Function &F) {
3063 // If nothing in the Module uses ARC, don't do anything.
3069 DEBUG(dbgs() << "<<< ObjCARCOpt: Visiting Function: " << F.getName() << " >>>"
3072 PA.setAA(&getAnalysis<AliasAnalysis>());
3075 if (AreStatisticsEnabled()) {
3076 GatherStatistics(F, false);
3080 // This pass performs several distinct transformations. As a compile-time aid
3081 // when compiling code that isn't ObjC, skip these if the relevant ObjC
3082 // library functions aren't declared.
3084 // Preliminary optimizations. This also computes UsedInThisFunction.
3085 OptimizeIndividualCalls(F);
3087 // Optimizations for weak pointers.
3088 if (UsedInThisFunction & ((1 << IC_LoadWeak) |
3089 (1 << IC_LoadWeakRetained) |
3090 (1 << IC_StoreWeak) |
3091 (1 << IC_InitWeak) |
3092 (1 << IC_CopyWeak) |
3093 (1 << IC_MoveWeak) |
3094 (1 << IC_DestroyWeak)))
3095 OptimizeWeakCalls(F);
3097 // Optimizations for retain+release pairs.
3098 if (UsedInThisFunction & ((1 << IC_Retain) |
3099 (1 << IC_RetainRV) |
3100 (1 << IC_RetainBlock)))
3101 if (UsedInThisFunction & (1 << IC_Release))
3102 // Run OptimizeSequences until it either stops making changes or
3103 // no retain+release pair nesting is detected.
3104 while (OptimizeSequences(F)) {}
3106 // Optimizations if objc_autorelease is used.
3107 if (UsedInThisFunction & ((1 << IC_Autorelease) |
3108 (1 << IC_AutoreleaseRV)))
3111 // Gather statistics after optimization.
3113 if (AreStatisticsEnabled()) {
3114 GatherStatistics(F, true);
3118 DEBUG(dbgs() << "\n");
3123 void ObjCARCOpt::releaseMemory() {