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, pattern-matching and replacement of
17 /// low-level operations into higher-level operations, and numerous minor
20 /// This file also defines a simple ARC-aware AliasAnalysis.
22 /// WARNING: This file knows about certain library functions. It recognizes them
23 /// by name, and hardwires knowledge of their semantics.
25 /// WARNING: This file knows about how certain Objective-C library functions are
26 /// used. Naive LLVM IR transformations which would otherwise be
27 /// behavior-preserving may break these assumptions.
29 //===----------------------------------------------------------------------===//
31 #define DEBUG_TYPE "objc-arc-opts"
33 #include "DependencyAnalysis.h"
34 #include "ObjCARCAliasAnalysis.h"
35 #include "ProvenanceAnalysis.h"
36 #include "llvm/ADT/DenseMap.h"
37 #include "llvm/ADT/STLExtras.h"
38 #include "llvm/ADT/SmallPtrSet.h"
39 #include "llvm/ADT/Statistic.h"
40 #include "llvm/IR/LLVMContext.h"
41 #include "llvm/Support/CFG.h"
44 using namespace llvm::objcarc;
46 /// \defgroup MiscUtils Miscellaneous utilities that are not ARC specific.
50 /// \brief An associative container with fast insertion-order (deterministic)
51 /// iteration over its elements. Plus the special blot operation.
52 template<class KeyT, class ValueT>
54 /// Map keys to indices in Vector.
55 typedef DenseMap<KeyT, size_t> MapTy;
58 typedef std::vector<std::pair<KeyT, ValueT> > VectorTy;
63 typedef typename VectorTy::iterator iterator;
64 typedef typename VectorTy::const_iterator const_iterator;
65 iterator begin() { return Vector.begin(); }
66 iterator end() { return Vector.end(); }
67 const_iterator begin() const { return Vector.begin(); }
68 const_iterator end() const { return Vector.end(); }
72 assert(Vector.size() >= Map.size()); // May differ due to blotting.
73 for (typename MapTy::const_iterator I = Map.begin(), E = Map.end();
75 assert(I->second < Vector.size());
76 assert(Vector[I->second].first == I->first);
78 for (typename VectorTy::const_iterator I = Vector.begin(),
79 E = Vector.end(); I != E; ++I)
81 (Map.count(I->first) &&
82 Map[I->first] == size_t(I - Vector.begin())));
86 ValueT &operator[](const KeyT &Arg) {
87 std::pair<typename MapTy::iterator, bool> Pair =
88 Map.insert(std::make_pair(Arg, size_t(0)));
90 size_t Num = Vector.size();
91 Pair.first->second = Num;
92 Vector.push_back(std::make_pair(Arg, ValueT()));
93 return Vector[Num].second;
95 return Vector[Pair.first->second].second;
98 std::pair<iterator, bool>
99 insert(const std::pair<KeyT, ValueT> &InsertPair) {
100 std::pair<typename MapTy::iterator, bool> Pair =
101 Map.insert(std::make_pair(InsertPair.first, size_t(0)));
103 size_t Num = Vector.size();
104 Pair.first->second = Num;
105 Vector.push_back(InsertPair);
106 return std::make_pair(Vector.begin() + Num, true);
108 return std::make_pair(Vector.begin() + Pair.first->second, false);
111 const_iterator find(const KeyT &Key) const {
112 typename MapTy::const_iterator It = Map.find(Key);
113 if (It == Map.end()) return Vector.end();
114 return Vector.begin() + It->second;
117 /// This is similar to erase, but instead of removing the element from the
118 /// vector, it just zeros out the key in the vector. This leaves iterators
119 /// intact, but clients must be prepared for zeroed-out keys when iterating.
120 void blot(const KeyT &Key) {
121 typename MapTy::iterator It = Map.find(Key);
122 if (It == Map.end()) return;
123 Vector[It->second].first = KeyT();
136 /// \defgroup ARCUtilities Utility declarations/definitions specific to ARC.
139 /// \brief This is similar to StripPointerCastsAndObjCCalls but it stops as soon
140 /// as it finds a value with multiple uses.
141 static const Value *FindSingleUseIdentifiedObject(const Value *Arg) {
142 if (Arg->hasOneUse()) {
143 if (const BitCastInst *BC = dyn_cast<BitCastInst>(Arg))
144 return FindSingleUseIdentifiedObject(BC->getOperand(0));
145 if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Arg))
146 if (GEP->hasAllZeroIndices())
147 return FindSingleUseIdentifiedObject(GEP->getPointerOperand());
148 if (IsForwarding(GetBasicInstructionClass(Arg)))
149 return FindSingleUseIdentifiedObject(
150 cast<CallInst>(Arg)->getArgOperand(0));
151 if (!IsObjCIdentifiedObject(Arg))
156 // If we found an identifiable object but it has multiple uses, but they are
157 // trivial uses, we can still consider this to be a single-use value.
158 if (IsObjCIdentifiedObject(Arg)) {
159 for (Value::const_use_iterator UI = Arg->use_begin(), UE = Arg->use_end();
162 if (!U->use_empty() || StripPointerCastsAndObjCCalls(U) != Arg)
172 /// \brief Test whether the given pointer, which is an Objective C block
173 /// pointer, does not "escape".
175 /// This differs from regular escape analysis in that a use as an
176 /// argument to a call is not considered an escape.
178 static bool DoesObjCBlockEscape(const Value *BlockPtr) {
180 DEBUG(dbgs() << "DoesObjCBlockEscape: Target: " << *BlockPtr << "\n");
182 // Walk the def-use chains.
183 SmallVector<const Value *, 4> Worklist;
184 Worklist.push_back(BlockPtr);
186 // Ensure we do not visit any value twice.
187 SmallPtrSet<const Value *, 4> VisitedSet;
190 const Value *V = Worklist.pop_back_val();
192 DEBUG(dbgs() << "DoesObjCBlockEscape: Visiting: " << *V << "\n");
194 for (Value::const_use_iterator UI = V->use_begin(), UE = V->use_end();
196 const User *UUser = *UI;
198 DEBUG(dbgs() << "DoesObjCBlockEscape: User: " << *UUser << "\n");
200 // Special - Use by a call (callee or argument) is not considered
202 switch (GetBasicInstructionClass(UUser)) {
207 case IC_AutoreleaseRV: {
208 DEBUG(dbgs() << "DoesObjCBlockEscape: User copies pointer arguments. "
210 // These special functions make copies of their pointer arguments.
215 // Use by an instruction which copies the value is an escape if the
216 // result is an escape.
217 if (isa<BitCastInst>(UUser) || isa<GetElementPtrInst>(UUser) ||
218 isa<PHINode>(UUser) || isa<SelectInst>(UUser)) {
220 if (!VisitedSet.insert(UUser)) {
221 DEBUG(dbgs() << "DoesObjCBlockEscape: User copies value. Escapes "
222 "if result escapes. Adding to list.\n");
223 Worklist.push_back(UUser);
225 DEBUG(dbgs() << "DoesObjCBlockEscape: Already visited node.\n");
229 // Use by a load is not an escape.
230 if (isa<LoadInst>(UUser))
232 // Use by a store is not an escape if the use is the address.
233 if (const StoreInst *SI = dyn_cast<StoreInst>(UUser))
234 if (V != SI->getValueOperand())
238 // Regular calls and other stuff are not considered escapes.
241 // Otherwise, conservatively assume an escape.
242 DEBUG(dbgs() << "DoesObjCBlockEscape: Assuming block escapes.\n");
245 } while (!Worklist.empty());
248 DEBUG(dbgs() << "DoesObjCBlockEscape: Block does not escape.\n");
254 /// \defgroup ARCOpt ARC Optimization.
257 // TODO: On code like this:
260 // stuff_that_cannot_release()
261 // objc_autorelease(%x)
262 // stuff_that_cannot_release()
264 // stuff_that_cannot_release()
265 // objc_autorelease(%x)
267 // The second retain and autorelease can be deleted.
269 // TODO: It should be possible to delete
270 // objc_autoreleasePoolPush and objc_autoreleasePoolPop
271 // pairs if nothing is actually autoreleased between them. Also, autorelease
272 // calls followed by objc_autoreleasePoolPop calls (perhaps in ObjC++ code
273 // after inlining) can be turned into plain release calls.
275 // TODO: Critical-edge splitting. If the optimial insertion point is
276 // a critical edge, the current algorithm has to fail, because it doesn't
277 // know how to split edges. It should be possible to make the optimizer
278 // think in terms of edges, rather than blocks, and then split critical
281 // TODO: OptimizeSequences could generalized to be Interprocedural.
283 // TODO: Recognize that a bunch of other objc runtime calls have
284 // non-escaping arguments and non-releasing arguments, and may be
285 // non-autoreleasing.
287 // TODO: Sink autorelease calls as far as possible. Unfortunately we
288 // usually can't sink them past other calls, which would be the main
289 // case where it would be useful.
291 // TODO: The pointer returned from objc_loadWeakRetained is retained.
293 // TODO: Delete release+retain pairs (rare).
295 STATISTIC(NumNoops, "Number of no-op objc calls eliminated");
296 STATISTIC(NumPartialNoops, "Number of partially no-op objc calls eliminated");
297 STATISTIC(NumAutoreleases,"Number of autoreleases converted to releases");
298 STATISTIC(NumRets, "Number of return value forwarding "
299 "retain+autoreleaes eliminated");
300 STATISTIC(NumRRs, "Number of retain+release paths eliminated");
301 STATISTIC(NumPeeps, "Number of calls peephole-optimized");
306 /// \brief A sequence of states that a pointer may go through in which an
307 /// objc_retain and objc_release are actually needed.
310 S_Retain, ///< objc_retain(x)
311 S_CanRelease, ///< foo(x) -- x could possibly see a ref count decrement
312 S_Use, ///< any use of x
313 S_Stop, ///< like S_Release, but code motion is stopped
314 S_Release, ///< objc_release(x)
315 S_MovableRelease ///< objc_release(x), !clang.imprecise_release
319 static Sequence MergeSeqs(Sequence A, Sequence B, bool TopDown) {
323 if (A == S_None || B == S_None)
326 if (A > B) std::swap(A, B);
328 // Choose the side which is further along in the sequence.
329 if ((A == S_Retain || A == S_CanRelease) &&
330 (B == S_CanRelease || B == S_Use))
333 // Choose the side which is further along in the sequence.
334 if ((A == S_Use || A == S_CanRelease) &&
335 (B == S_Use || B == S_Release || B == S_Stop || B == S_MovableRelease))
337 // If both sides are releases, choose the more conservative one.
338 if (A == S_Stop && (B == S_Release || B == S_MovableRelease))
340 if (A == S_Release && B == S_MovableRelease)
348 /// \brief Unidirectional information about either a
349 /// retain-decrement-use-release sequence or release-use-decrement-retain
350 /// reverese sequence.
352 /// After an objc_retain, the reference count of the referenced
353 /// object is known to be positive. Similarly, before an objc_release, the
354 /// reference count of the referenced object is known to be positive. If
355 /// there are retain-release pairs in code regions where the retain count
356 /// is known to be positive, they can be eliminated, regardless of any side
357 /// effects between them.
359 /// Also, a retain+release pair nested within another retain+release
360 /// pair all on the known same pointer value can be eliminated, regardless
361 /// of any intervening side effects.
363 /// KnownSafe is true when either of these conditions is satisfied.
366 /// True if the Calls are objc_retainBlock calls (as opposed to objc_retain
370 /// True of the objc_release calls are all marked with the "tail" keyword.
371 bool IsTailCallRelease;
373 /// If the Calls are objc_release calls and they all have a
374 /// clang.imprecise_release tag, this is the metadata tag.
375 MDNode *ReleaseMetadata;
377 /// For a top-down sequence, the set of objc_retains or
378 /// objc_retainBlocks. For bottom-up, the set of objc_releases.
379 SmallPtrSet<Instruction *, 2> Calls;
381 /// The set of optimal insert positions for moving calls in the opposite
383 SmallPtrSet<Instruction *, 2> ReverseInsertPts;
386 KnownSafe(false), IsRetainBlock(false),
387 IsTailCallRelease(false),
388 ReleaseMetadata(0) {}
394 void RRInfo::clear() {
396 IsRetainBlock = false;
397 IsTailCallRelease = false;
400 ReverseInsertPts.clear();
404 /// \brief This class summarizes several per-pointer runtime properties which
405 /// are propogated through the flow graph.
407 /// True if the reference count is known to be incremented.
408 bool KnownPositiveRefCount;
410 /// True of we've seen an opportunity for partial RR elimination, such as
411 /// pushing calls into a CFG triangle or into one side of a CFG diamond.
414 /// The current position in the sequence.
418 /// Unidirectional information about the current sequence.
420 /// TODO: Encapsulate this better.
423 PtrState() : KnownPositiveRefCount(false), Partial(false),
426 void SetKnownPositiveRefCount() {
427 KnownPositiveRefCount = true;
430 void ClearRefCount() {
431 KnownPositiveRefCount = false;
434 bool IsKnownIncremented() const {
435 return KnownPositiveRefCount;
438 void SetSeq(Sequence NewSeq) {
442 Sequence GetSeq() const {
446 void ClearSequenceProgress() {
447 ResetSequenceProgress(S_None);
450 void ResetSequenceProgress(Sequence NewSeq) {
456 void Merge(const PtrState &Other, bool TopDown);
461 PtrState::Merge(const PtrState &Other, bool TopDown) {
462 Seq = MergeSeqs(Seq, Other.Seq, TopDown);
463 KnownPositiveRefCount = KnownPositiveRefCount && Other.KnownPositiveRefCount;
465 // We can't merge a plain objc_retain with an objc_retainBlock.
466 if (RRI.IsRetainBlock != Other.RRI.IsRetainBlock)
469 // If we're not in a sequence (anymore), drop all associated state.
473 } else if (Partial || Other.Partial) {
474 // If we're doing a merge on a path that's previously seen a partial
475 // merge, conservatively drop the sequence, to avoid doing partial
476 // RR elimination. If the branch predicates for the two merge differ,
477 // mixing them is unsafe.
478 ClearSequenceProgress();
480 // Conservatively merge the ReleaseMetadata information.
481 if (RRI.ReleaseMetadata != Other.RRI.ReleaseMetadata)
482 RRI.ReleaseMetadata = 0;
484 RRI.KnownSafe = RRI.KnownSafe && Other.RRI.KnownSafe;
485 RRI.IsTailCallRelease = RRI.IsTailCallRelease &&
486 Other.RRI.IsTailCallRelease;
487 RRI.Calls.insert(Other.RRI.Calls.begin(), Other.RRI.Calls.end());
489 // Merge the insert point sets. If there are any differences,
490 // that makes this a partial merge.
491 Partial = RRI.ReverseInsertPts.size() != Other.RRI.ReverseInsertPts.size();
492 for (SmallPtrSet<Instruction *, 2>::const_iterator
493 I = Other.RRI.ReverseInsertPts.begin(),
494 E = Other.RRI.ReverseInsertPts.end(); I != E; ++I)
495 Partial |= RRI.ReverseInsertPts.insert(*I);
500 /// \brief Per-BasicBlock state.
502 /// The number of unique control paths from the entry which can reach this
504 unsigned TopDownPathCount;
506 /// The number of unique control paths to exits from this block.
507 unsigned BottomUpPathCount;
509 /// A type for PerPtrTopDown and PerPtrBottomUp.
510 typedef MapVector<const Value *, PtrState> MapTy;
512 /// The top-down traversal uses this to record information known about a
513 /// pointer at the bottom of each block.
516 /// The bottom-up traversal uses this to record information known about a
517 /// pointer at the top of each block.
518 MapTy PerPtrBottomUp;
520 /// Effective predecessors of the current block ignoring ignorable edges and
521 /// ignored backedges.
522 SmallVector<BasicBlock *, 2> Preds;
523 /// Effective successors of the current block ignoring ignorable edges and
524 /// ignored backedges.
525 SmallVector<BasicBlock *, 2> Succs;
528 BBState() : TopDownPathCount(0), BottomUpPathCount(0) {}
530 typedef MapTy::iterator ptr_iterator;
531 typedef MapTy::const_iterator ptr_const_iterator;
533 ptr_iterator top_down_ptr_begin() { return PerPtrTopDown.begin(); }
534 ptr_iterator top_down_ptr_end() { return PerPtrTopDown.end(); }
535 ptr_const_iterator top_down_ptr_begin() const {
536 return PerPtrTopDown.begin();
538 ptr_const_iterator top_down_ptr_end() const {
539 return PerPtrTopDown.end();
542 ptr_iterator bottom_up_ptr_begin() { return PerPtrBottomUp.begin(); }
543 ptr_iterator bottom_up_ptr_end() { return PerPtrBottomUp.end(); }
544 ptr_const_iterator bottom_up_ptr_begin() const {
545 return PerPtrBottomUp.begin();
547 ptr_const_iterator bottom_up_ptr_end() const {
548 return PerPtrBottomUp.end();
551 /// Mark this block as being an entry block, which has one path from the
552 /// entry by definition.
553 void SetAsEntry() { TopDownPathCount = 1; }
555 /// Mark this block as being an exit block, which has one path to an exit by
557 void SetAsExit() { BottomUpPathCount = 1; }
559 PtrState &getPtrTopDownState(const Value *Arg) {
560 return PerPtrTopDown[Arg];
563 PtrState &getPtrBottomUpState(const Value *Arg) {
564 return PerPtrBottomUp[Arg];
567 void clearBottomUpPointers() {
568 PerPtrBottomUp.clear();
571 void clearTopDownPointers() {
572 PerPtrTopDown.clear();
575 void InitFromPred(const BBState &Other);
576 void InitFromSucc(const BBState &Other);
577 void MergePred(const BBState &Other);
578 void MergeSucc(const BBState &Other);
580 /// Return the number of possible unique paths from an entry to an exit
581 /// which pass through this block. This is only valid after both the
582 /// top-down and bottom-up traversals are complete.
583 unsigned GetAllPathCount() const {
584 assert(TopDownPathCount != 0);
585 assert(BottomUpPathCount != 0);
586 return TopDownPathCount * BottomUpPathCount;
589 // Specialized CFG utilities.
590 typedef SmallVectorImpl<BasicBlock *>::const_iterator edge_iterator;
591 edge_iterator pred_begin() { return Preds.begin(); }
592 edge_iterator pred_end() { return Preds.end(); }
593 edge_iterator succ_begin() { return Succs.begin(); }
594 edge_iterator succ_end() { return Succs.end(); }
596 void addSucc(BasicBlock *Succ) { Succs.push_back(Succ); }
597 void addPred(BasicBlock *Pred) { Preds.push_back(Pred); }
599 bool isExit() const { return Succs.empty(); }
603 void BBState::InitFromPred(const BBState &Other) {
604 PerPtrTopDown = Other.PerPtrTopDown;
605 TopDownPathCount = Other.TopDownPathCount;
608 void BBState::InitFromSucc(const BBState &Other) {
609 PerPtrBottomUp = Other.PerPtrBottomUp;
610 BottomUpPathCount = Other.BottomUpPathCount;
613 /// The top-down traversal uses this to merge information about predecessors to
614 /// form the initial state for a new block.
615 void BBState::MergePred(const BBState &Other) {
616 // Other.TopDownPathCount can be 0, in which case it is either dead or a
617 // loop backedge. Loop backedges are special.
618 TopDownPathCount += Other.TopDownPathCount;
620 // Check for overflow. If we have overflow, fall back to conservative
622 if (TopDownPathCount < Other.TopDownPathCount) {
623 clearTopDownPointers();
627 // For each entry in the other set, if our set has an entry with the same key,
628 // merge the entries. Otherwise, copy the entry and merge it with an empty
630 for (ptr_const_iterator MI = Other.top_down_ptr_begin(),
631 ME = Other.top_down_ptr_end(); MI != ME; ++MI) {
632 std::pair<ptr_iterator, bool> Pair = PerPtrTopDown.insert(*MI);
633 Pair.first->second.Merge(Pair.second ? PtrState() : MI->second,
637 // For each entry in our set, if the other set doesn't have an entry with the
638 // same key, force it to merge with an empty entry.
639 for (ptr_iterator MI = top_down_ptr_begin(),
640 ME = top_down_ptr_end(); MI != ME; ++MI)
641 if (Other.PerPtrTopDown.find(MI->first) == Other.PerPtrTopDown.end())
642 MI->second.Merge(PtrState(), /*TopDown=*/true);
645 /// The bottom-up traversal uses this to merge information about successors to
646 /// form the initial state for a new block.
647 void BBState::MergeSucc(const BBState &Other) {
648 // Other.BottomUpPathCount can be 0, in which case it is either dead or a
649 // loop backedge. Loop backedges are special.
650 BottomUpPathCount += Other.BottomUpPathCount;
652 // Check for overflow. If we have overflow, fall back to conservative
654 if (BottomUpPathCount < Other.BottomUpPathCount) {
655 clearBottomUpPointers();
659 // For each entry in the other set, if our set has an entry with the
660 // same key, merge the entries. Otherwise, copy the entry and merge
661 // it with an empty entry.
662 for (ptr_const_iterator MI = Other.bottom_up_ptr_begin(),
663 ME = Other.bottom_up_ptr_end(); MI != ME; ++MI) {
664 std::pair<ptr_iterator, bool> Pair = PerPtrBottomUp.insert(*MI);
665 Pair.first->second.Merge(Pair.second ? PtrState() : MI->second,
669 // For each entry in our set, if the other set doesn't have an entry
670 // with the same key, force it to merge with an empty entry.
671 for (ptr_iterator MI = bottom_up_ptr_begin(),
672 ME = bottom_up_ptr_end(); MI != ME; ++MI)
673 if (Other.PerPtrBottomUp.find(MI->first) == Other.PerPtrBottomUp.end())
674 MI->second.Merge(PtrState(), /*TopDown=*/false);
678 /// \brief The main ARC optimization pass.
679 class ObjCARCOpt : public FunctionPass {
681 ProvenanceAnalysis PA;
683 /// A flag indicating whether this optimization pass should run.
686 /// Declarations for ObjC runtime functions, for use in creating calls to
687 /// them. These are initialized lazily to avoid cluttering up the Module
688 /// with unused declarations.
690 /// Declaration for ObjC runtime function
691 /// objc_retainAutoreleasedReturnValue.
692 Constant *RetainRVCallee;
693 /// Declaration for ObjC runtime function objc_autoreleaseReturnValue.
694 Constant *AutoreleaseRVCallee;
695 /// Declaration for ObjC runtime function objc_release.
696 Constant *ReleaseCallee;
697 /// Declaration for ObjC runtime function objc_retain.
698 Constant *RetainCallee;
699 /// Declaration for ObjC runtime function objc_retainBlock.
700 Constant *RetainBlockCallee;
701 /// Declaration for ObjC runtime function objc_autorelease.
702 Constant *AutoreleaseCallee;
704 /// Flags which determine whether each of the interesting runtine functions
705 /// is in fact used in the current function.
706 unsigned UsedInThisFunction;
708 /// The Metadata Kind for clang.imprecise_release metadata.
709 unsigned ImpreciseReleaseMDKind;
711 /// The Metadata Kind for clang.arc.copy_on_escape metadata.
712 unsigned CopyOnEscapeMDKind;
714 /// The Metadata Kind for clang.arc.no_objc_arc_exceptions metadata.
715 unsigned NoObjCARCExceptionsMDKind;
717 Constant *getRetainRVCallee(Module *M);
718 Constant *getAutoreleaseRVCallee(Module *M);
719 Constant *getReleaseCallee(Module *M);
720 Constant *getRetainCallee(Module *M);
721 Constant *getRetainBlockCallee(Module *M);
722 Constant *getAutoreleaseCallee(Module *M);
724 bool IsRetainBlockOptimizable(const Instruction *Inst);
726 void OptimizeRetainCall(Function &F, Instruction *Retain);
727 bool OptimizeRetainRVCall(Function &F, Instruction *RetainRV);
728 void OptimizeAutoreleaseRVCall(Function &F, Instruction *AutoreleaseRV,
729 InstructionClass &Class);
730 void OptimizeIndividualCalls(Function &F);
732 void CheckForCFGHazards(const BasicBlock *BB,
733 DenseMap<const BasicBlock *, BBState> &BBStates,
734 BBState &MyStates) const;
735 bool VisitInstructionBottomUp(Instruction *Inst,
737 MapVector<Value *, RRInfo> &Retains,
739 bool VisitBottomUp(BasicBlock *BB,
740 DenseMap<const BasicBlock *, BBState> &BBStates,
741 MapVector<Value *, RRInfo> &Retains);
742 bool VisitInstructionTopDown(Instruction *Inst,
743 DenseMap<Value *, RRInfo> &Releases,
745 bool VisitTopDown(BasicBlock *BB,
746 DenseMap<const BasicBlock *, BBState> &BBStates,
747 DenseMap<Value *, RRInfo> &Releases);
748 bool Visit(Function &F,
749 DenseMap<const BasicBlock *, BBState> &BBStates,
750 MapVector<Value *, RRInfo> &Retains,
751 DenseMap<Value *, RRInfo> &Releases);
753 void MoveCalls(Value *Arg, RRInfo &RetainsToMove, RRInfo &ReleasesToMove,
754 MapVector<Value *, RRInfo> &Retains,
755 DenseMap<Value *, RRInfo> &Releases,
756 SmallVectorImpl<Instruction *> &DeadInsts,
759 bool ConnectTDBUTraversals(DenseMap<const BasicBlock *, BBState> &BBStates,
760 MapVector<Value *, RRInfo> &Retains,
761 DenseMap<Value *, RRInfo> &Releases,
763 SmallVector<Instruction *, 4> &NewRetains,
764 SmallVector<Instruction *, 4> &NewReleases,
765 SmallVector<Instruction *, 8> &DeadInsts,
766 RRInfo &RetainsToMove,
767 RRInfo &ReleasesToMove,
770 bool &AnyPairsCompletelyEliminated);
772 bool PerformCodePlacement(DenseMap<const BasicBlock *, BBState> &BBStates,
773 MapVector<Value *, RRInfo> &Retains,
774 DenseMap<Value *, RRInfo> &Releases,
777 void OptimizeWeakCalls(Function &F);
779 bool OptimizeSequences(Function &F);
781 void OptimizeReturns(Function &F);
783 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
784 virtual bool doInitialization(Module &M);
785 virtual bool runOnFunction(Function &F);
786 virtual void releaseMemory();
790 ObjCARCOpt() : FunctionPass(ID) {
791 initializeObjCARCOptPass(*PassRegistry::getPassRegistry());
796 char ObjCARCOpt::ID = 0;
797 INITIALIZE_PASS_BEGIN(ObjCARCOpt,
798 "objc-arc", "ObjC ARC optimization", false, false)
799 INITIALIZE_PASS_DEPENDENCY(ObjCARCAliasAnalysis)
800 INITIALIZE_PASS_END(ObjCARCOpt,
801 "objc-arc", "ObjC ARC optimization", false, false)
803 Pass *llvm::createObjCARCOptPass() {
804 return new ObjCARCOpt();
807 void ObjCARCOpt::getAnalysisUsage(AnalysisUsage &AU) const {
808 AU.addRequired<ObjCARCAliasAnalysis>();
809 AU.addRequired<AliasAnalysis>();
810 // ARC optimization doesn't currently split critical edges.
811 AU.setPreservesCFG();
814 bool ObjCARCOpt::IsRetainBlockOptimizable(const Instruction *Inst) {
815 // Without the magic metadata tag, we have to assume this might be an
816 // objc_retainBlock call inserted to convert a block pointer to an id,
817 // in which case it really is needed.
818 if (!Inst->getMetadata(CopyOnEscapeMDKind))
821 // If the pointer "escapes" (not including being used in a call),
822 // the copy may be needed.
823 if (DoesObjCBlockEscape(Inst))
826 // Otherwise, it's not needed.
830 Constant *ObjCARCOpt::getRetainRVCallee(Module *M) {
831 if (!RetainRVCallee) {
832 LLVMContext &C = M->getContext();
833 Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C));
834 Type *Params[] = { I8X };
835 FunctionType *FTy = FunctionType::get(I8X, Params, /*isVarArg=*/false);
836 AttributeSet Attribute =
837 AttributeSet().addAttribute(M->getContext(), AttributeSet::FunctionIndex,
838 Attribute::NoUnwind);
840 M->getOrInsertFunction("objc_retainAutoreleasedReturnValue", FTy,
843 return RetainRVCallee;
846 Constant *ObjCARCOpt::getAutoreleaseRVCallee(Module *M) {
847 if (!AutoreleaseRVCallee) {
848 LLVMContext &C = M->getContext();
849 Type *I8X = PointerType::getUnqual(Type::getInt8Ty(C));
850 Type *Params[] = { I8X };
851 FunctionType *FTy = FunctionType::get(I8X, Params, /*isVarArg=*/false);
852 AttributeSet Attribute =
853 AttributeSet().addAttribute(M->getContext(), AttributeSet::FunctionIndex,
854 Attribute::NoUnwind);
855 AutoreleaseRVCallee =
856 M->getOrInsertFunction("objc_autoreleaseReturnValue", FTy,
859 return AutoreleaseRVCallee;
862 Constant *ObjCARCOpt::getReleaseCallee(Module *M) {
863 if (!ReleaseCallee) {
864 LLVMContext &C = M->getContext();
865 Type *Params[] = { PointerType::getUnqual(Type::getInt8Ty(C)) };
866 AttributeSet Attribute =
867 AttributeSet().addAttribute(M->getContext(), AttributeSet::FunctionIndex,
868 Attribute::NoUnwind);
870 M->getOrInsertFunction(
872 FunctionType::get(Type::getVoidTy(C), Params, /*isVarArg=*/false),
875 return ReleaseCallee;
878 Constant *ObjCARCOpt::getRetainCallee(Module *M) {
880 LLVMContext &C = M->getContext();
881 Type *Params[] = { PointerType::getUnqual(Type::getInt8Ty(C)) };
882 AttributeSet Attribute =
883 AttributeSet().addAttribute(M->getContext(), AttributeSet::FunctionIndex,
884 Attribute::NoUnwind);
886 M->getOrInsertFunction(
888 FunctionType::get(Params[0], Params, /*isVarArg=*/false),
894 Constant *ObjCARCOpt::getRetainBlockCallee(Module *M) {
895 if (!RetainBlockCallee) {
896 LLVMContext &C = M->getContext();
897 Type *Params[] = { PointerType::getUnqual(Type::getInt8Ty(C)) };
898 // objc_retainBlock is not nounwind because it calls user copy constructors
899 // which could theoretically throw.
901 M->getOrInsertFunction(
903 FunctionType::get(Params[0], Params, /*isVarArg=*/false),
906 return RetainBlockCallee;
909 Constant *ObjCARCOpt::getAutoreleaseCallee(Module *M) {
910 if (!AutoreleaseCallee) {
911 LLVMContext &C = M->getContext();
912 Type *Params[] = { PointerType::getUnqual(Type::getInt8Ty(C)) };
913 AttributeSet Attribute =
914 AttributeSet().addAttribute(M->getContext(), AttributeSet::FunctionIndex,
915 Attribute::NoUnwind);
917 M->getOrInsertFunction(
919 FunctionType::get(Params[0], Params, /*isVarArg=*/false),
922 return AutoreleaseCallee;
925 /// Turn objc_retain into objc_retainAutoreleasedReturnValue if the operand is a
928 ObjCARCOpt::OptimizeRetainCall(Function &F, Instruction *Retain) {
929 ImmutableCallSite CS(GetObjCArg(Retain));
930 const Instruction *Call = CS.getInstruction();
932 if (Call->getParent() != Retain->getParent()) return;
934 // Check that the call is next to the retain.
935 BasicBlock::const_iterator I = Call;
937 while (isNoopInstruction(I)) ++I;
941 // Turn it to an objc_retainAutoreleasedReturnValue..
945 DEBUG(dbgs() << "ObjCARCOpt::OptimizeRetainCall: Transforming "
946 "objc_retain => objc_retainAutoreleasedReturnValue"
947 " since the operand is a return value.\n"
951 cast<CallInst>(Retain)->setCalledFunction(getRetainRVCallee(F.getParent()));
953 DEBUG(dbgs() << " New: "
957 /// Turn objc_retainAutoreleasedReturnValue into objc_retain if the operand is
958 /// not a return value. Or, if it can be paired with an
959 /// objc_autoreleaseReturnValue, delete the pair and return true.
961 ObjCARCOpt::OptimizeRetainRVCall(Function &F, Instruction *RetainRV) {
962 // Check for the argument being from an immediately preceding call or invoke.
963 const Value *Arg = GetObjCArg(RetainRV);
964 ImmutableCallSite CS(Arg);
965 if (const Instruction *Call = CS.getInstruction()) {
966 if (Call->getParent() == RetainRV->getParent()) {
967 BasicBlock::const_iterator I = Call;
969 while (isNoopInstruction(I)) ++I;
972 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(Call)) {
973 BasicBlock *RetainRVParent = RetainRV->getParent();
974 if (II->getNormalDest() == RetainRVParent) {
975 BasicBlock::const_iterator I = RetainRVParent->begin();
976 while (isNoopInstruction(I)) ++I;
983 // Check for being preceded by an objc_autoreleaseReturnValue on the same
984 // pointer. In this case, we can delete the pair.
985 BasicBlock::iterator I = RetainRV, Begin = RetainRV->getParent()->begin();
987 do --I; while (I != Begin && isNoopInstruction(I));
988 if (GetBasicInstructionClass(I) == IC_AutoreleaseRV &&
989 GetObjCArg(I) == Arg) {
993 DEBUG(dbgs() << "ObjCARCOpt::OptimizeRetainRVCall: Erasing " << *I << "\n"
994 << " Erasing " << *RetainRV
998 EraseInstruction(RetainRV);
1003 // Turn it to a plain objc_retain.
1007 DEBUG(dbgs() << "ObjCARCOpt::OptimizeRetainRVCall: Transforming "
1008 "objc_retainAutoreleasedReturnValue => "
1009 "objc_retain since the operand is not a return value.\n"
1011 << *RetainRV << "\n");
1013 cast<CallInst>(RetainRV)->setCalledFunction(getRetainCallee(F.getParent()));
1015 DEBUG(dbgs() << " New: "
1016 << *RetainRV << "\n");
1021 /// Turn objc_autoreleaseReturnValue into objc_autorelease if the result is not
1022 /// used as a return value.
1024 ObjCARCOpt::OptimizeAutoreleaseRVCall(Function &F, Instruction *AutoreleaseRV,
1025 InstructionClass &Class) {
1026 // Check for a return of the pointer value.
1027 const Value *Ptr = GetObjCArg(AutoreleaseRV);
1028 SmallVector<const Value *, 2> Users;
1029 Users.push_back(Ptr);
1031 Ptr = Users.pop_back_val();
1032 for (Value::const_use_iterator UI = Ptr->use_begin(), UE = Ptr->use_end();
1034 const User *I = *UI;
1035 if (isa<ReturnInst>(I) || GetBasicInstructionClass(I) == IC_RetainRV)
1037 if (isa<BitCastInst>(I))
1040 } while (!Users.empty());
1045 DEBUG(dbgs() << "ObjCARCOpt::OptimizeAutoreleaseRVCall: Transforming "
1046 "objc_autoreleaseReturnValue => "
1047 "objc_autorelease since its operand is not used as a return "
1050 << *AutoreleaseRV << "\n");
1052 CallInst *AutoreleaseRVCI = cast<CallInst>(AutoreleaseRV);
1054 setCalledFunction(getAutoreleaseCallee(F.getParent()));
1055 AutoreleaseRVCI->setTailCall(false); // Never tail call objc_autorelease.
1056 Class = IC_Autorelease;
1058 DEBUG(dbgs() << " New: "
1059 << *AutoreleaseRV << "\n");
1063 /// Visit each call, one at a time, and make simplifications without doing any
1064 /// additional analysis.
1065 void ObjCARCOpt::OptimizeIndividualCalls(Function &F) {
1066 // Reset all the flags in preparation for recomputing them.
1067 UsedInThisFunction = 0;
1069 // Visit all objc_* calls in F.
1070 for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
1071 Instruction *Inst = &*I++;
1073 InstructionClass Class = GetBasicInstructionClass(Inst);
1075 DEBUG(dbgs() << "ObjCARCOpt::OptimizeIndividualCalls: Visiting: Class: "
1076 << Class << "; " << *Inst << "\n");
1081 // Delete no-op casts. These function calls have special semantics, but
1082 // the semantics are entirely implemented via lowering in the front-end,
1083 // so by the time they reach the optimizer, they are just no-op calls
1084 // which return their argument.
1086 // There are gray areas here, as the ability to cast reference-counted
1087 // pointers to raw void* and back allows code to break ARC assumptions,
1088 // however these are currently considered to be unimportant.
1092 DEBUG(dbgs() << "ObjCARCOpt::OptimizeIndividualCalls: Erasing no-op cast:"
1093 " " << *Inst << "\n");
1094 EraseInstruction(Inst);
1097 // If the pointer-to-weak-pointer is null, it's undefined behavior.
1100 case IC_LoadWeakRetained:
1102 case IC_DestroyWeak: {
1103 CallInst *CI = cast<CallInst>(Inst);
1104 if (isNullOrUndef(CI->getArgOperand(0))) {
1106 Type *Ty = CI->getArgOperand(0)->getType();
1107 new StoreInst(UndefValue::get(cast<PointerType>(Ty)->getElementType()),
1108 Constant::getNullValue(Ty),
1110 llvm::Value *NewValue = UndefValue::get(CI->getType());
1111 DEBUG(dbgs() << "ObjCARCOpt::OptimizeIndividualCalls: A null "
1112 "pointer-to-weak-pointer is undefined behavior.\n"
1116 CI->replaceAllUsesWith(NewValue);
1117 CI->eraseFromParent();
1124 CallInst *CI = cast<CallInst>(Inst);
1125 if (isNullOrUndef(CI->getArgOperand(0)) ||
1126 isNullOrUndef(CI->getArgOperand(1))) {
1128 Type *Ty = CI->getArgOperand(0)->getType();
1129 new StoreInst(UndefValue::get(cast<PointerType>(Ty)->getElementType()),
1130 Constant::getNullValue(Ty),
1133 llvm::Value *NewValue = UndefValue::get(CI->getType());
1134 DEBUG(dbgs() << "ObjCARCOpt::OptimizeIndividualCalls: A null "
1135 "pointer-to-weak-pointer is undefined behavior.\n"
1140 CI->replaceAllUsesWith(NewValue);
1141 CI->eraseFromParent();
1147 OptimizeRetainCall(F, Inst);
1150 if (OptimizeRetainRVCall(F, Inst))
1153 case IC_AutoreleaseRV:
1154 OptimizeAutoreleaseRVCall(F, Inst, Class);
1158 // objc_autorelease(x) -> objc_release(x) if x is otherwise unused.
1159 if (IsAutorelease(Class) && Inst->use_empty()) {
1160 CallInst *Call = cast<CallInst>(Inst);
1161 const Value *Arg = Call->getArgOperand(0);
1162 Arg = FindSingleUseIdentifiedObject(Arg);
1167 // Create the declaration lazily.
1168 LLVMContext &C = Inst->getContext();
1170 CallInst::Create(getReleaseCallee(F.getParent()),
1171 Call->getArgOperand(0), "", Call);
1172 NewCall->setMetadata(ImpreciseReleaseMDKind,
1173 MDNode::get(C, ArrayRef<Value *>()));
1175 DEBUG(dbgs() << "ObjCARCOpt::OptimizeIndividualCalls: Replacing "
1176 "objc_autorelease(x) with objc_release(x) since x is "
1177 "otherwise unused.\n"
1178 " Old: " << *Call <<
1182 EraseInstruction(Call);
1188 // For functions which can never be passed stack arguments, add
1190 if (IsAlwaysTail(Class)) {
1192 DEBUG(dbgs() << "ObjCARCOpt::OptimizeIndividualCalls: Adding tail keyword"
1193 " to function since it can never be passed stack args: " << *Inst <<
1195 cast<CallInst>(Inst)->setTailCall();
1198 // Ensure that functions that can never have a "tail" keyword due to the
1199 // semantics of ARC truly do not do so.
1200 if (IsNeverTail(Class)) {
1202 DEBUG(dbgs() << "ObjCARCOpt::OptimizeIndividualCalls: Removing tail "
1203 "keyword from function: " << *Inst <<
1205 cast<CallInst>(Inst)->setTailCall(false);
1208 // Set nounwind as needed.
1209 if (IsNoThrow(Class)) {
1211 DEBUG(dbgs() << "ObjCARCOpt::OptimizeIndividualCalls: Found no throw"
1212 " class. Setting nounwind on: " << *Inst << "\n");
1213 cast<CallInst>(Inst)->setDoesNotThrow();
1216 if (!IsNoopOnNull(Class)) {
1217 UsedInThisFunction |= 1 << Class;
1221 const Value *Arg = GetObjCArg(Inst);
1223 // ARC calls with null are no-ops. Delete them.
1224 if (isNullOrUndef(Arg)) {
1227 DEBUG(dbgs() << "ObjCARCOpt::OptimizeIndividualCalls: ARC calls with "
1228 " null are no-ops. Erasing: " << *Inst << "\n");
1229 EraseInstruction(Inst);
1233 // Keep track of which of retain, release, autorelease, and retain_block
1234 // are actually present in this function.
1235 UsedInThisFunction |= 1 << Class;
1237 // If Arg is a PHI, and one or more incoming values to the
1238 // PHI are null, and the call is control-equivalent to the PHI, and there
1239 // are no relevant side effects between the PHI and the call, the call
1240 // could be pushed up to just those paths with non-null incoming values.
1241 // For now, don't bother splitting critical edges for this.
1242 SmallVector<std::pair<Instruction *, const Value *>, 4> Worklist;
1243 Worklist.push_back(std::make_pair(Inst, Arg));
1245 std::pair<Instruction *, const Value *> Pair = Worklist.pop_back_val();
1249 const PHINode *PN = dyn_cast<PHINode>(Arg);
1252 // Determine if the PHI has any null operands, or any incoming
1254 bool HasNull = false;
1255 bool HasCriticalEdges = false;
1256 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
1258 StripPointerCastsAndObjCCalls(PN->getIncomingValue(i));
1259 if (isNullOrUndef(Incoming))
1261 else if (cast<TerminatorInst>(PN->getIncomingBlock(i)->back())
1262 .getNumSuccessors() != 1) {
1263 HasCriticalEdges = true;
1267 // If we have null operands and no critical edges, optimize.
1268 if (!HasCriticalEdges && HasNull) {
1269 SmallPtrSet<Instruction *, 4> DependingInstructions;
1270 SmallPtrSet<const BasicBlock *, 4> Visited;
1272 // Check that there is nothing that cares about the reference
1273 // count between the call and the phi.
1276 case IC_RetainBlock:
1277 // These can always be moved up.
1280 // These can't be moved across things that care about the retain
1282 FindDependencies(NeedsPositiveRetainCount, Arg,
1283 Inst->getParent(), Inst,
1284 DependingInstructions, Visited, PA);
1286 case IC_Autorelease:
1287 // These can't be moved across autorelease pool scope boundaries.
1288 FindDependencies(AutoreleasePoolBoundary, Arg,
1289 Inst->getParent(), Inst,
1290 DependingInstructions, Visited, PA);
1293 case IC_AutoreleaseRV:
1294 // Don't move these; the RV optimization depends on the autoreleaseRV
1295 // being tail called, and the retainRV being immediately after a call
1296 // (which might still happen if we get lucky with codegen layout, but
1297 // it's not worth taking the chance).
1300 llvm_unreachable("Invalid dependence flavor");
1303 if (DependingInstructions.size() == 1 &&
1304 *DependingInstructions.begin() == PN) {
1307 // Clone the call into each predecessor that has a non-null value.
1308 CallInst *CInst = cast<CallInst>(Inst);
1309 Type *ParamTy = CInst->getArgOperand(0)->getType();
1310 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
1312 StripPointerCastsAndObjCCalls(PN->getIncomingValue(i));
1313 if (!isNullOrUndef(Incoming)) {
1314 CallInst *Clone = cast<CallInst>(CInst->clone());
1315 Value *Op = PN->getIncomingValue(i);
1316 Instruction *InsertPos = &PN->getIncomingBlock(i)->back();
1317 if (Op->getType() != ParamTy)
1318 Op = new BitCastInst(Op, ParamTy, "", InsertPos);
1319 Clone->setArgOperand(0, Op);
1320 Clone->insertBefore(InsertPos);
1322 DEBUG(dbgs() << "ObjCARCOpt::OptimizeIndividualCalls: Cloning "
1325 "clone at " << *InsertPos << "\n");
1326 Worklist.push_back(std::make_pair(Clone, Incoming));
1329 // Erase the original call.
1330 DEBUG(dbgs() << "Erasing: " << *CInst << "\n");
1331 EraseInstruction(CInst);
1335 } while (!Worklist.empty());
1337 DEBUG(dbgs() << "ObjCARCOpt::OptimizeIndividualCalls: Finished List.\n");
1340 /// Check for critical edges, loop boundaries, irreducible control flow, or
1341 /// other CFG structures where moving code across the edge would result in it
1342 /// being executed more.
1344 ObjCARCOpt::CheckForCFGHazards(const BasicBlock *BB,
1345 DenseMap<const BasicBlock *, BBState> &BBStates,
1346 BBState &MyStates) const {
1347 // If any top-down local-use or possible-dec has a succ which is earlier in
1348 // the sequence, forget it.
1349 for (BBState::ptr_iterator I = MyStates.top_down_ptr_begin(),
1350 E = MyStates.top_down_ptr_end(); I != E; ++I)
1351 switch (I->second.GetSeq()) {
1354 const Value *Arg = I->first;
1355 const TerminatorInst *TI = cast<TerminatorInst>(&BB->back());
1356 bool SomeSuccHasSame = false;
1357 bool AllSuccsHaveSame = true;
1358 PtrState &S = I->second;
1359 succ_const_iterator SI(TI), SE(TI, false);
1361 for (; SI != SE; ++SI) {
1362 Sequence SuccSSeq = S_None;
1363 bool SuccSRRIKnownSafe = false;
1364 // If VisitBottomUp has pointer information for this successor, take
1365 // what we know about it.
1366 DenseMap<const BasicBlock *, BBState>::iterator BBI =
1368 assert(BBI != BBStates.end());
1369 const PtrState &SuccS = BBI->second.getPtrBottomUpState(Arg);
1370 SuccSSeq = SuccS.GetSeq();
1371 SuccSRRIKnownSafe = SuccS.RRI.KnownSafe;
1374 case S_CanRelease: {
1375 if (!S.RRI.KnownSafe && !SuccSRRIKnownSafe) {
1376 S.ClearSequenceProgress();
1382 SomeSuccHasSame = true;
1386 case S_MovableRelease:
1387 if (!S.RRI.KnownSafe && !SuccSRRIKnownSafe)
1388 AllSuccsHaveSame = false;
1391 llvm_unreachable("bottom-up pointer in retain state!");
1394 // If the state at the other end of any of the successor edges
1395 // matches the current state, require all edges to match. This
1396 // guards against loops in the middle of a sequence.
1397 if (SomeSuccHasSame && !AllSuccsHaveSame)
1398 S.ClearSequenceProgress();
1401 case S_CanRelease: {
1402 const Value *Arg = I->first;
1403 const TerminatorInst *TI = cast<TerminatorInst>(&BB->back());
1404 bool SomeSuccHasSame = false;
1405 bool AllSuccsHaveSame = true;
1406 PtrState &S = I->second;
1407 succ_const_iterator SI(TI), SE(TI, false);
1409 for (; SI != SE; ++SI) {
1410 Sequence SuccSSeq = S_None;
1411 bool SuccSRRIKnownSafe = false;
1412 // If VisitBottomUp has pointer information for this successor, take
1413 // what we know about it.
1414 DenseMap<const BasicBlock *, BBState>::iterator BBI =
1416 assert(BBI != BBStates.end());
1417 const PtrState &SuccS = BBI->second.getPtrBottomUpState(Arg);
1418 SuccSSeq = SuccS.GetSeq();
1419 SuccSRRIKnownSafe = SuccS.RRI.KnownSafe;
1422 if (!S.RRI.KnownSafe && !SuccSRRIKnownSafe) {
1423 S.ClearSequenceProgress();
1429 SomeSuccHasSame = true;
1433 case S_MovableRelease:
1435 if (!S.RRI.KnownSafe && !SuccSRRIKnownSafe)
1436 AllSuccsHaveSame = false;
1439 llvm_unreachable("bottom-up pointer in retain state!");
1442 // If the state at the other end of any of the successor edges
1443 // matches the current state, require all edges to match. This
1444 // guards against loops in the middle of a sequence.
1445 if (SomeSuccHasSame && !AllSuccsHaveSame)
1446 S.ClearSequenceProgress();
1453 ObjCARCOpt::VisitInstructionBottomUp(Instruction *Inst,
1455 MapVector<Value *, RRInfo> &Retains,
1456 BBState &MyStates) {
1457 bool NestingDetected = false;
1458 InstructionClass Class = GetInstructionClass(Inst);
1459 const Value *Arg = 0;
1463 Arg = GetObjCArg(Inst);
1465 PtrState &S = MyStates.getPtrBottomUpState(Arg);
1467 // If we see two releases in a row on the same pointer. If so, make
1468 // a note, and we'll cicle back to revisit it after we've
1469 // hopefully eliminated the second release, which may allow us to
1470 // eliminate the first release too.
1471 // Theoretically we could implement removal of nested retain+release
1472 // pairs by making PtrState hold a stack of states, but this is
1473 // simple and avoids adding overhead for the non-nested case.
1474 if (S.GetSeq() == S_Release || S.GetSeq() == S_MovableRelease) {
1475 DEBUG(dbgs() << "ObjCARCOpt::VisitInstructionBottomUp: Found nested "
1476 "releases (i.e. a release pair)\n");
1477 NestingDetected = true;
1480 MDNode *ReleaseMetadata = Inst->getMetadata(ImpreciseReleaseMDKind);
1481 S.ResetSequenceProgress(ReleaseMetadata ? S_MovableRelease : S_Release);
1482 S.RRI.ReleaseMetadata = ReleaseMetadata;
1483 S.RRI.KnownSafe = S.IsKnownIncremented();
1484 S.RRI.IsTailCallRelease = cast<CallInst>(Inst)->isTailCall();
1485 S.RRI.Calls.insert(Inst);
1487 S.SetKnownPositiveRefCount();
1490 case IC_RetainBlock:
1491 // An objc_retainBlock call with just a use may need to be kept,
1492 // because it may be copying a block from the stack to the heap.
1493 if (!IsRetainBlockOptimizable(Inst))
1498 Arg = GetObjCArg(Inst);
1500 PtrState &S = MyStates.getPtrBottomUpState(Arg);
1501 S.SetKnownPositiveRefCount();
1503 switch (S.GetSeq()) {
1506 case S_MovableRelease:
1508 S.RRI.ReverseInsertPts.clear();
1511 // Don't do retain+release tracking for IC_RetainRV, because it's
1512 // better to let it remain as the first instruction after a call.
1513 if (Class != IC_RetainRV) {
1514 S.RRI.IsRetainBlock = Class == IC_RetainBlock;
1515 Retains[Inst] = S.RRI;
1517 S.ClearSequenceProgress();
1522 llvm_unreachable("bottom-up pointer in retain state!");
1524 return NestingDetected;
1526 case IC_AutoreleasepoolPop:
1527 // Conservatively, clear MyStates for all known pointers.
1528 MyStates.clearBottomUpPointers();
1529 return NestingDetected;
1530 case IC_AutoreleasepoolPush:
1532 // These are irrelevant.
1533 return NestingDetected;
1538 // Consider any other possible effects of this instruction on each
1539 // pointer being tracked.
1540 for (BBState::ptr_iterator MI = MyStates.bottom_up_ptr_begin(),
1541 ME = MyStates.bottom_up_ptr_end(); MI != ME; ++MI) {
1542 const Value *Ptr = MI->first;
1544 continue; // Handled above.
1545 PtrState &S = MI->second;
1546 Sequence Seq = S.GetSeq();
1548 // Check for possible releases.
1549 if (CanAlterRefCount(Inst, Ptr, PA, Class)) {
1553 S.SetSeq(S_CanRelease);
1557 case S_MovableRelease:
1562 llvm_unreachable("bottom-up pointer in retain state!");
1566 // Check for possible direct uses.
1569 case S_MovableRelease:
1570 if (CanUse(Inst, Ptr, PA, Class)) {
1571 assert(S.RRI.ReverseInsertPts.empty());
1572 // If this is an invoke instruction, we're scanning it as part of
1573 // one of its successor blocks, since we can't insert code after it
1574 // in its own block, and we don't want to split critical edges.
1575 if (isa<InvokeInst>(Inst))
1576 S.RRI.ReverseInsertPts.insert(BB->getFirstInsertionPt());
1578 S.RRI.ReverseInsertPts.insert(llvm::next(BasicBlock::iterator(Inst)));
1580 } else if (Seq == S_Release &&
1581 (Class == IC_User || Class == IC_CallOrUser)) {
1582 // Non-movable releases depend on any possible objc pointer use.
1584 assert(S.RRI.ReverseInsertPts.empty());
1585 // As above; handle invoke specially.
1586 if (isa<InvokeInst>(Inst))
1587 S.RRI.ReverseInsertPts.insert(BB->getFirstInsertionPt());
1589 S.RRI.ReverseInsertPts.insert(llvm::next(BasicBlock::iterator(Inst)));
1593 if (CanUse(Inst, Ptr, PA, Class))
1601 llvm_unreachable("bottom-up pointer in retain state!");
1605 return NestingDetected;
1609 ObjCARCOpt::VisitBottomUp(BasicBlock *BB,
1610 DenseMap<const BasicBlock *, BBState> &BBStates,
1611 MapVector<Value *, RRInfo> &Retains) {
1612 bool NestingDetected = false;
1613 BBState &MyStates = BBStates[BB];
1615 // Merge the states from each successor to compute the initial state
1616 // for the current block.
1617 BBState::edge_iterator SI(MyStates.succ_begin()),
1618 SE(MyStates.succ_end());
1620 const BasicBlock *Succ = *SI;
1621 DenseMap<const BasicBlock *, BBState>::iterator I = BBStates.find(Succ);
1622 assert(I != BBStates.end());
1623 MyStates.InitFromSucc(I->second);
1625 for (; SI != SE; ++SI) {
1627 I = BBStates.find(Succ);
1628 assert(I != BBStates.end());
1629 MyStates.MergeSucc(I->second);
1633 // Visit all the instructions, bottom-up.
1634 for (BasicBlock::iterator I = BB->end(), E = BB->begin(); I != E; --I) {
1635 Instruction *Inst = llvm::prior(I);
1637 // Invoke instructions are visited as part of their successors (below).
1638 if (isa<InvokeInst>(Inst))
1641 DEBUG(dbgs() << "ObjCARCOpt::VisitButtonUp: Visiting " << *Inst << "\n");
1643 NestingDetected |= VisitInstructionBottomUp(Inst, BB, Retains, MyStates);
1646 // If there's a predecessor with an invoke, visit the invoke as if it were
1647 // part of this block, since we can't insert code after an invoke in its own
1648 // block, and we don't want to split critical edges.
1649 for (BBState::edge_iterator PI(MyStates.pred_begin()),
1650 PE(MyStates.pred_end()); PI != PE; ++PI) {
1651 BasicBlock *Pred = *PI;
1652 if (InvokeInst *II = dyn_cast<InvokeInst>(&Pred->back()))
1653 NestingDetected |= VisitInstructionBottomUp(II, BB, Retains, MyStates);
1656 return NestingDetected;
1660 ObjCARCOpt::VisitInstructionTopDown(Instruction *Inst,
1661 DenseMap<Value *, RRInfo> &Releases,
1662 BBState &MyStates) {
1663 bool NestingDetected = false;
1664 InstructionClass Class = GetInstructionClass(Inst);
1665 const Value *Arg = 0;
1668 case IC_RetainBlock:
1669 // An objc_retainBlock call with just a use may need to be kept,
1670 // because it may be copying a block from the stack to the heap.
1671 if (!IsRetainBlockOptimizable(Inst))
1676 Arg = GetObjCArg(Inst);
1678 PtrState &S = MyStates.getPtrTopDownState(Arg);
1680 // Don't do retain+release tracking for IC_RetainRV, because it's
1681 // better to let it remain as the first instruction after a call.
1682 if (Class != IC_RetainRV) {
1683 // If we see two retains in a row on the same pointer. If so, make
1684 // a note, and we'll cicle back to revisit it after we've
1685 // hopefully eliminated the second retain, which may allow us to
1686 // eliminate the first retain too.
1687 // Theoretically we could implement removal of nested retain+release
1688 // pairs by making PtrState hold a stack of states, but this is
1689 // simple and avoids adding overhead for the non-nested case.
1690 if (S.GetSeq() == S_Retain)
1691 NestingDetected = true;
1693 S.ResetSequenceProgress(S_Retain);
1694 S.RRI.IsRetainBlock = Class == IC_RetainBlock;
1695 S.RRI.KnownSafe = S.IsKnownIncremented();
1696 S.RRI.Calls.insert(Inst);
1699 S.SetKnownPositiveRefCount();
1701 // A retain can be a potential use; procede to the generic checking
1706 Arg = GetObjCArg(Inst);
1708 PtrState &S = MyStates.getPtrTopDownState(Arg);
1711 switch (S.GetSeq()) {
1714 S.RRI.ReverseInsertPts.clear();
1717 S.RRI.ReleaseMetadata = Inst->getMetadata(ImpreciseReleaseMDKind);
1718 S.RRI.IsTailCallRelease = cast<CallInst>(Inst)->isTailCall();
1719 Releases[Inst] = S.RRI;
1720 S.ClearSequenceProgress();
1726 case S_MovableRelease:
1727 llvm_unreachable("top-down pointer in release state!");
1731 case IC_AutoreleasepoolPop:
1732 // Conservatively, clear MyStates for all known pointers.
1733 MyStates.clearTopDownPointers();
1734 return NestingDetected;
1735 case IC_AutoreleasepoolPush:
1737 // These are irrelevant.
1738 return NestingDetected;
1743 // Consider any other possible effects of this instruction on each
1744 // pointer being tracked.
1745 for (BBState::ptr_iterator MI = MyStates.top_down_ptr_begin(),
1746 ME = MyStates.top_down_ptr_end(); MI != ME; ++MI) {
1747 const Value *Ptr = MI->first;
1749 continue; // Handled above.
1750 PtrState &S = MI->second;
1751 Sequence Seq = S.GetSeq();
1753 // Check for possible releases.
1754 if (CanAlterRefCount(Inst, Ptr, PA, Class)) {
1758 S.SetSeq(S_CanRelease);
1759 assert(S.RRI.ReverseInsertPts.empty());
1760 S.RRI.ReverseInsertPts.insert(Inst);
1762 // One call can't cause a transition from S_Retain to S_CanRelease
1763 // and S_CanRelease to S_Use. If we've made the first transition,
1772 case S_MovableRelease:
1773 llvm_unreachable("top-down pointer in release state!");
1777 // Check for possible direct uses.
1780 if (CanUse(Inst, Ptr, PA, Class))
1789 case S_MovableRelease:
1790 llvm_unreachable("top-down pointer in release state!");
1794 return NestingDetected;
1798 ObjCARCOpt::VisitTopDown(BasicBlock *BB,
1799 DenseMap<const BasicBlock *, BBState> &BBStates,
1800 DenseMap<Value *, RRInfo> &Releases) {
1801 bool NestingDetected = false;
1802 BBState &MyStates = BBStates[BB];
1804 // Merge the states from each predecessor to compute the initial state
1805 // for the current block.
1806 BBState::edge_iterator PI(MyStates.pred_begin()),
1807 PE(MyStates.pred_end());
1809 const BasicBlock *Pred = *PI;
1810 DenseMap<const BasicBlock *, BBState>::iterator I = BBStates.find(Pred);
1811 assert(I != BBStates.end());
1812 MyStates.InitFromPred(I->second);
1814 for (; PI != PE; ++PI) {
1816 I = BBStates.find(Pred);
1817 assert(I != BBStates.end());
1818 MyStates.MergePred(I->second);
1822 // Visit all the instructions, top-down.
1823 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
1824 Instruction *Inst = I;
1826 DEBUG(dbgs() << "ObjCARCOpt::VisitTopDown: Visiting " << *Inst << "\n");
1828 NestingDetected |= VisitInstructionTopDown(Inst, Releases, MyStates);
1831 CheckForCFGHazards(BB, BBStates, MyStates);
1832 return NestingDetected;
1836 ComputePostOrders(Function &F,
1837 SmallVectorImpl<BasicBlock *> &PostOrder,
1838 SmallVectorImpl<BasicBlock *> &ReverseCFGPostOrder,
1839 unsigned NoObjCARCExceptionsMDKind,
1840 DenseMap<const BasicBlock *, BBState> &BBStates) {
1841 /// The visited set, for doing DFS walks.
1842 SmallPtrSet<BasicBlock *, 16> Visited;
1844 // Do DFS, computing the PostOrder.
1845 SmallPtrSet<BasicBlock *, 16> OnStack;
1846 SmallVector<std::pair<BasicBlock *, succ_iterator>, 16> SuccStack;
1848 // Functions always have exactly one entry block, and we don't have
1849 // any other block that we treat like an entry block.
1850 BasicBlock *EntryBB = &F.getEntryBlock();
1851 BBState &MyStates = BBStates[EntryBB];
1852 MyStates.SetAsEntry();
1853 TerminatorInst *EntryTI = cast<TerminatorInst>(&EntryBB->back());
1854 SuccStack.push_back(std::make_pair(EntryBB, succ_iterator(EntryTI)));
1855 Visited.insert(EntryBB);
1856 OnStack.insert(EntryBB);
1859 BasicBlock *CurrBB = SuccStack.back().first;
1860 TerminatorInst *TI = cast<TerminatorInst>(&CurrBB->back());
1861 succ_iterator SE(TI, false);
1863 while (SuccStack.back().second != SE) {
1864 BasicBlock *SuccBB = *SuccStack.back().second++;
1865 if (Visited.insert(SuccBB)) {
1866 TerminatorInst *TI = cast<TerminatorInst>(&SuccBB->back());
1867 SuccStack.push_back(std::make_pair(SuccBB, succ_iterator(TI)));
1868 BBStates[CurrBB].addSucc(SuccBB);
1869 BBState &SuccStates = BBStates[SuccBB];
1870 SuccStates.addPred(CurrBB);
1871 OnStack.insert(SuccBB);
1875 if (!OnStack.count(SuccBB)) {
1876 BBStates[CurrBB].addSucc(SuccBB);
1877 BBStates[SuccBB].addPred(CurrBB);
1880 OnStack.erase(CurrBB);
1881 PostOrder.push_back(CurrBB);
1882 SuccStack.pop_back();
1883 } while (!SuccStack.empty());
1887 // Do reverse-CFG DFS, computing the reverse-CFG PostOrder.
1888 // Functions may have many exits, and there also blocks which we treat
1889 // as exits due to ignored edges.
1890 SmallVector<std::pair<BasicBlock *, BBState::edge_iterator>, 16> PredStack;
1891 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
1892 BasicBlock *ExitBB = I;
1893 BBState &MyStates = BBStates[ExitBB];
1894 if (!MyStates.isExit())
1897 MyStates.SetAsExit();
1899 PredStack.push_back(std::make_pair(ExitBB, MyStates.pred_begin()));
1900 Visited.insert(ExitBB);
1901 while (!PredStack.empty()) {
1902 reverse_dfs_next_succ:
1903 BBState::edge_iterator PE = BBStates[PredStack.back().first].pred_end();
1904 while (PredStack.back().second != PE) {
1905 BasicBlock *BB = *PredStack.back().second++;
1906 if (Visited.insert(BB)) {
1907 PredStack.push_back(std::make_pair(BB, BBStates[BB].pred_begin()));
1908 goto reverse_dfs_next_succ;
1911 ReverseCFGPostOrder.push_back(PredStack.pop_back_val().first);
1916 // Visit the function both top-down and bottom-up.
1918 ObjCARCOpt::Visit(Function &F,
1919 DenseMap<const BasicBlock *, BBState> &BBStates,
1920 MapVector<Value *, RRInfo> &Retains,
1921 DenseMap<Value *, RRInfo> &Releases) {
1923 // Use reverse-postorder traversals, because we magically know that loops
1924 // will be well behaved, i.e. they won't repeatedly call retain on a single
1925 // pointer without doing a release. We can't use the ReversePostOrderTraversal
1926 // class here because we want the reverse-CFG postorder to consider each
1927 // function exit point, and we want to ignore selected cycle edges.
1928 SmallVector<BasicBlock *, 16> PostOrder;
1929 SmallVector<BasicBlock *, 16> ReverseCFGPostOrder;
1930 ComputePostOrders(F, PostOrder, ReverseCFGPostOrder,
1931 NoObjCARCExceptionsMDKind,
1934 // Use reverse-postorder on the reverse CFG for bottom-up.
1935 bool BottomUpNestingDetected = false;
1936 for (SmallVectorImpl<BasicBlock *>::const_reverse_iterator I =
1937 ReverseCFGPostOrder.rbegin(), E = ReverseCFGPostOrder.rend();
1939 BottomUpNestingDetected |= VisitBottomUp(*I, BBStates, Retains);
1941 // Use reverse-postorder for top-down.
1942 bool TopDownNestingDetected = false;
1943 for (SmallVectorImpl<BasicBlock *>::const_reverse_iterator I =
1944 PostOrder.rbegin(), E = PostOrder.rend();
1946 TopDownNestingDetected |= VisitTopDown(*I, BBStates, Releases);
1948 return TopDownNestingDetected && BottomUpNestingDetected;
1951 /// Move the calls in RetainsToMove and ReleasesToMove.
1952 void ObjCARCOpt::MoveCalls(Value *Arg,
1953 RRInfo &RetainsToMove,
1954 RRInfo &ReleasesToMove,
1955 MapVector<Value *, RRInfo> &Retains,
1956 DenseMap<Value *, RRInfo> &Releases,
1957 SmallVectorImpl<Instruction *> &DeadInsts,
1959 Type *ArgTy = Arg->getType();
1960 Type *ParamTy = PointerType::getUnqual(Type::getInt8Ty(ArgTy->getContext()));
1962 // Insert the new retain and release calls.
1963 for (SmallPtrSet<Instruction *, 2>::const_iterator
1964 PI = ReleasesToMove.ReverseInsertPts.begin(),
1965 PE = ReleasesToMove.ReverseInsertPts.end(); PI != PE; ++PI) {
1966 Instruction *InsertPt = *PI;
1967 Value *MyArg = ArgTy == ParamTy ? Arg :
1968 new BitCastInst(Arg, ParamTy, "", InsertPt);
1970 CallInst::Create(RetainsToMove.IsRetainBlock ?
1971 getRetainBlockCallee(M) : getRetainCallee(M),
1972 MyArg, "", InsertPt);
1973 Call->setDoesNotThrow();
1974 if (RetainsToMove.IsRetainBlock)
1975 Call->setMetadata(CopyOnEscapeMDKind,
1976 MDNode::get(M->getContext(), ArrayRef<Value *>()));
1978 Call->setTailCall();
1980 DEBUG(dbgs() << "ObjCARCOpt::MoveCalls: Inserting new Release: " << *Call
1982 " At insertion point: " << *InsertPt
1985 for (SmallPtrSet<Instruction *, 2>::const_iterator
1986 PI = RetainsToMove.ReverseInsertPts.begin(),
1987 PE = RetainsToMove.ReverseInsertPts.end(); PI != PE; ++PI) {
1988 Instruction *InsertPt = *PI;
1989 Value *MyArg = ArgTy == ParamTy ? Arg :
1990 new BitCastInst(Arg, ParamTy, "", InsertPt);
1991 CallInst *Call = CallInst::Create(getReleaseCallee(M), MyArg,
1993 // Attach a clang.imprecise_release metadata tag, if appropriate.
1994 if (MDNode *M = ReleasesToMove.ReleaseMetadata)
1995 Call->setMetadata(ImpreciseReleaseMDKind, M);
1996 Call->setDoesNotThrow();
1997 if (ReleasesToMove.IsTailCallRelease)
1998 Call->setTailCall();
2000 DEBUG(dbgs() << "ObjCARCOpt::MoveCalls: Inserting new Retain: " << *Call
2002 " At insertion point: " << *InsertPt
2006 // Delete the original retain and release calls.
2007 for (SmallPtrSet<Instruction *, 2>::const_iterator
2008 AI = RetainsToMove.Calls.begin(),
2009 AE = RetainsToMove.Calls.end(); AI != AE; ++AI) {
2010 Instruction *OrigRetain = *AI;
2011 Retains.blot(OrigRetain);
2012 DeadInsts.push_back(OrigRetain);
2013 DEBUG(dbgs() << "ObjCARCOpt::MoveCalls: Deleting retain: " << *OrigRetain <<
2016 for (SmallPtrSet<Instruction *, 2>::const_iterator
2017 AI = ReleasesToMove.Calls.begin(),
2018 AE = ReleasesToMove.Calls.end(); AI != AE; ++AI) {
2019 Instruction *OrigRelease = *AI;
2020 Releases.erase(OrigRelease);
2021 DeadInsts.push_back(OrigRelease);
2022 DEBUG(dbgs() << "ObjCARCOpt::MoveCalls: Deleting release: " << *OrigRelease
2028 ObjCARCOpt::ConnectTDBUTraversals(DenseMap<const BasicBlock *, BBState>
2030 MapVector<Value *, RRInfo> &Retains,
2031 DenseMap<Value *, RRInfo> &Releases,
2033 SmallVector<Instruction *, 4> &NewRetains,
2034 SmallVector<Instruction *, 4> &NewReleases,
2035 SmallVector<Instruction *, 8> &DeadInsts,
2036 RRInfo &RetainsToMove,
2037 RRInfo &ReleasesToMove,
2040 bool &AnyPairsCompletelyEliminated) {
2041 // If a pair happens in a region where it is known that the reference count
2042 // is already incremented, we can similarly ignore possible decrements.
2043 bool KnownSafeTD = true, KnownSafeBU = true;
2045 // Connect the dots between the top-down-collected RetainsToMove and
2046 // bottom-up-collected ReleasesToMove to form sets of related calls.
2047 // This is an iterative process so that we connect multiple releases
2048 // to multiple retains if needed.
2049 unsigned OldDelta = 0;
2050 unsigned NewDelta = 0;
2051 unsigned OldCount = 0;
2052 unsigned NewCount = 0;
2053 bool FirstRelease = true;
2054 bool FirstRetain = true;
2056 for (SmallVectorImpl<Instruction *>::const_iterator
2057 NI = NewRetains.begin(), NE = NewRetains.end(); NI != NE; ++NI) {
2058 Instruction *NewRetain = *NI;
2059 MapVector<Value *, RRInfo>::const_iterator It = Retains.find(NewRetain);
2060 assert(It != Retains.end());
2061 const RRInfo &NewRetainRRI = It->second;
2062 KnownSafeTD &= NewRetainRRI.KnownSafe;
2063 for (SmallPtrSet<Instruction *, 2>::const_iterator
2064 LI = NewRetainRRI.Calls.begin(),
2065 LE = NewRetainRRI.Calls.end(); LI != LE; ++LI) {
2066 Instruction *NewRetainRelease = *LI;
2067 DenseMap<Value *, RRInfo>::const_iterator Jt =
2068 Releases.find(NewRetainRelease);
2069 if (Jt == Releases.end())
2071 const RRInfo &NewRetainReleaseRRI = Jt->second;
2072 assert(NewRetainReleaseRRI.Calls.count(NewRetain));
2073 if (ReleasesToMove.Calls.insert(NewRetainRelease)) {
2075 BBStates[NewRetainRelease->getParent()].GetAllPathCount();
2077 // Merge the ReleaseMetadata and IsTailCallRelease values.
2079 ReleasesToMove.ReleaseMetadata =
2080 NewRetainReleaseRRI.ReleaseMetadata;
2081 ReleasesToMove.IsTailCallRelease =
2082 NewRetainReleaseRRI.IsTailCallRelease;
2083 FirstRelease = false;
2085 if (ReleasesToMove.ReleaseMetadata !=
2086 NewRetainReleaseRRI.ReleaseMetadata)
2087 ReleasesToMove.ReleaseMetadata = 0;
2088 if (ReleasesToMove.IsTailCallRelease !=
2089 NewRetainReleaseRRI.IsTailCallRelease)
2090 ReleasesToMove.IsTailCallRelease = false;
2093 // Collect the optimal insertion points.
2095 for (SmallPtrSet<Instruction *, 2>::const_iterator
2096 RI = NewRetainReleaseRRI.ReverseInsertPts.begin(),
2097 RE = NewRetainReleaseRRI.ReverseInsertPts.end();
2099 Instruction *RIP = *RI;
2100 if (ReleasesToMove.ReverseInsertPts.insert(RIP))
2101 NewDelta -= BBStates[RIP->getParent()].GetAllPathCount();
2103 NewReleases.push_back(NewRetainRelease);
2108 if (NewReleases.empty()) break;
2110 // Back the other way.
2111 for (SmallVectorImpl<Instruction *>::const_iterator
2112 NI = NewReleases.begin(), NE = NewReleases.end(); NI != NE; ++NI) {
2113 Instruction *NewRelease = *NI;
2114 DenseMap<Value *, RRInfo>::const_iterator It =
2115 Releases.find(NewRelease);
2116 assert(It != Releases.end());
2117 const RRInfo &NewReleaseRRI = It->second;
2118 KnownSafeBU &= NewReleaseRRI.KnownSafe;
2119 for (SmallPtrSet<Instruction *, 2>::const_iterator
2120 LI = NewReleaseRRI.Calls.begin(),
2121 LE = NewReleaseRRI.Calls.end(); LI != LE; ++LI) {
2122 Instruction *NewReleaseRetain = *LI;
2123 MapVector<Value *, RRInfo>::const_iterator Jt =
2124 Retains.find(NewReleaseRetain);
2125 if (Jt == Retains.end())
2127 const RRInfo &NewReleaseRetainRRI = Jt->second;
2128 assert(NewReleaseRetainRRI.Calls.count(NewRelease));
2129 if (RetainsToMove.Calls.insert(NewReleaseRetain)) {
2130 unsigned PathCount =
2131 BBStates[NewReleaseRetain->getParent()].GetAllPathCount();
2132 OldDelta += PathCount;
2133 OldCount += PathCount;
2135 // Merge the IsRetainBlock values.
2137 RetainsToMove.IsRetainBlock = NewReleaseRetainRRI.IsRetainBlock;
2138 FirstRetain = false;
2139 } else if (ReleasesToMove.IsRetainBlock !=
2140 NewReleaseRetainRRI.IsRetainBlock)
2141 // It's not possible to merge the sequences if one uses
2142 // objc_retain and the other uses objc_retainBlock.
2145 // Collect the optimal insertion points.
2147 for (SmallPtrSet<Instruction *, 2>::const_iterator
2148 RI = NewReleaseRetainRRI.ReverseInsertPts.begin(),
2149 RE = NewReleaseRetainRRI.ReverseInsertPts.end();
2151 Instruction *RIP = *RI;
2152 if (RetainsToMove.ReverseInsertPts.insert(RIP)) {
2153 PathCount = BBStates[RIP->getParent()].GetAllPathCount();
2154 NewDelta += PathCount;
2155 NewCount += PathCount;
2158 NewRetains.push_back(NewReleaseRetain);
2162 NewReleases.clear();
2163 if (NewRetains.empty()) break;
2166 // If the pointer is known incremented or nested, we can safely delete the
2167 // pair regardless of what's between them.
2168 if (KnownSafeTD || KnownSafeBU) {
2169 RetainsToMove.ReverseInsertPts.clear();
2170 ReleasesToMove.ReverseInsertPts.clear();
2173 // Determine whether the new insertion points we computed preserve the
2174 // balance of retain and release calls through the program.
2175 // TODO: If the fully aggressive solution isn't valid, try to find a
2176 // less aggressive solution which is.
2181 // Determine whether the original call points are balanced in the retain and
2182 // release calls through the program. If not, conservatively don't touch
2184 // TODO: It's theoretically possible to do code motion in this case, as
2185 // long as the existing imbalances are maintained.
2190 assert(OldCount != 0 && "Unreachable code?");
2191 NumRRs += OldCount - NewCount;
2192 // Set to true if we completely removed any RR pairs.
2193 AnyPairsCompletelyEliminated = NewCount == 0;
2195 // We can move calls!
2199 /// Identify pairings between the retains and releases, and delete and/or move
2202 ObjCARCOpt::PerformCodePlacement(DenseMap<const BasicBlock *, BBState>
2204 MapVector<Value *, RRInfo> &Retains,
2205 DenseMap<Value *, RRInfo> &Releases,
2207 bool AnyPairsCompletelyEliminated = false;
2208 RRInfo RetainsToMove;
2209 RRInfo ReleasesToMove;
2210 SmallVector<Instruction *, 4> NewRetains;
2211 SmallVector<Instruction *, 4> NewReleases;
2212 SmallVector<Instruction *, 8> DeadInsts;
2214 // Visit each retain.
2215 for (MapVector<Value *, RRInfo>::const_iterator I = Retains.begin(),
2216 E = Retains.end(); I != E; ++I) {
2217 Value *V = I->first;
2218 if (!V) continue; // blotted
2220 Instruction *Retain = cast<Instruction>(V);
2222 DEBUG(dbgs() << "ObjCARCOpt::PerformCodePlacement: Visiting: " << *Retain
2225 Value *Arg = GetObjCArg(Retain);
2227 // If the object being released is in static or stack storage, we know it's
2228 // not being managed by ObjC reference counting, so we can delete pairs
2229 // regardless of what possible decrements or uses lie between them.
2230 bool KnownSafe = isa<Constant>(Arg) || isa<AllocaInst>(Arg);
2232 // A constant pointer can't be pointing to an object on the heap. It may
2233 // be reference-counted, but it won't be deleted.
2234 if (const LoadInst *LI = dyn_cast<LoadInst>(Arg))
2235 if (const GlobalVariable *GV =
2236 dyn_cast<GlobalVariable>(
2237 StripPointerCastsAndObjCCalls(LI->getPointerOperand())))
2238 if (GV->isConstant())
2241 // Connect the dots between the top-down-collected RetainsToMove and
2242 // bottom-up-collected ReleasesToMove to form sets of related calls.
2243 NewRetains.push_back(Retain);
2244 bool PerformMoveCalls =
2245 ConnectTDBUTraversals(BBStates, Retains, Releases, M, NewRetains,
2246 NewReleases, DeadInsts, RetainsToMove,
2247 ReleasesToMove, Arg, KnownSafe,
2248 AnyPairsCompletelyEliminated);
2250 if (PerformMoveCalls) {
2251 // Ok, everything checks out and we're all set. Let's move/delete some
2253 MoveCalls(Arg, RetainsToMove, ReleasesToMove,
2254 Retains, Releases, DeadInsts, M);
2257 // Clean up state for next retain.
2258 NewReleases.clear();
2260 RetainsToMove.clear();
2261 ReleasesToMove.clear();
2264 // Now that we're done moving everything, we can delete the newly dead
2265 // instructions, as we no longer need them as insert points.
2266 while (!DeadInsts.empty())
2267 EraseInstruction(DeadInsts.pop_back_val());
2269 return AnyPairsCompletelyEliminated;
2272 /// Weak pointer optimizations.
2273 void ObjCARCOpt::OptimizeWeakCalls(Function &F) {
2274 // First, do memdep-style RLE and S2L optimizations. We can't use memdep
2275 // itself because it uses AliasAnalysis and we need to do provenance
2277 for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
2278 Instruction *Inst = &*I++;
2280 DEBUG(dbgs() << "ObjCARCOpt::OptimizeWeakCalls: Visiting: " << *Inst <<
2283 InstructionClass Class = GetBasicInstructionClass(Inst);
2284 if (Class != IC_LoadWeak && Class != IC_LoadWeakRetained)
2287 // Delete objc_loadWeak calls with no users.
2288 if (Class == IC_LoadWeak && Inst->use_empty()) {
2289 Inst->eraseFromParent();
2293 // TODO: For now, just look for an earlier available version of this value
2294 // within the same block. Theoretically, we could do memdep-style non-local
2295 // analysis too, but that would want caching. A better approach would be to
2296 // use the technique that EarlyCSE uses.
2297 inst_iterator Current = llvm::prior(I);
2298 BasicBlock *CurrentBB = Current.getBasicBlockIterator();
2299 for (BasicBlock::iterator B = CurrentBB->begin(),
2300 J = Current.getInstructionIterator();
2302 Instruction *EarlierInst = &*llvm::prior(J);
2303 InstructionClass EarlierClass = GetInstructionClass(EarlierInst);
2304 switch (EarlierClass) {
2306 case IC_LoadWeakRetained: {
2307 // If this is loading from the same pointer, replace this load's value
2309 CallInst *Call = cast<CallInst>(Inst);
2310 CallInst *EarlierCall = cast<CallInst>(EarlierInst);
2311 Value *Arg = Call->getArgOperand(0);
2312 Value *EarlierArg = EarlierCall->getArgOperand(0);
2313 switch (PA.getAA()->alias(Arg, EarlierArg)) {
2314 case AliasAnalysis::MustAlias:
2316 // If the load has a builtin retain, insert a plain retain for it.
2317 if (Class == IC_LoadWeakRetained) {
2319 CallInst::Create(getRetainCallee(F.getParent()), EarlierCall,
2323 // Zap the fully redundant load.
2324 Call->replaceAllUsesWith(EarlierCall);
2325 Call->eraseFromParent();
2327 case AliasAnalysis::MayAlias:
2328 case AliasAnalysis::PartialAlias:
2330 case AliasAnalysis::NoAlias:
2337 // If this is storing to the same pointer and has the same size etc.
2338 // replace this load's value with the stored value.
2339 CallInst *Call = cast<CallInst>(Inst);
2340 CallInst *EarlierCall = cast<CallInst>(EarlierInst);
2341 Value *Arg = Call->getArgOperand(0);
2342 Value *EarlierArg = EarlierCall->getArgOperand(0);
2343 switch (PA.getAA()->alias(Arg, EarlierArg)) {
2344 case AliasAnalysis::MustAlias:
2346 // If the load has a builtin retain, insert a plain retain for it.
2347 if (Class == IC_LoadWeakRetained) {
2349 CallInst::Create(getRetainCallee(F.getParent()), EarlierCall,
2353 // Zap the fully redundant load.
2354 Call->replaceAllUsesWith(EarlierCall->getArgOperand(1));
2355 Call->eraseFromParent();
2357 case AliasAnalysis::MayAlias:
2358 case AliasAnalysis::PartialAlias:
2360 case AliasAnalysis::NoAlias:
2367 // TOOD: Grab the copied value.
2369 case IC_AutoreleasepoolPush:
2372 // Weak pointers are only modified through the weak entry points
2373 // (and arbitrary calls, which could call the weak entry points).
2376 // Anything else could modify the weak pointer.
2383 // Then, for each destroyWeak with an alloca operand, check to see if
2384 // the alloca and all its users can be zapped.
2385 for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
2386 Instruction *Inst = &*I++;
2387 InstructionClass Class = GetBasicInstructionClass(Inst);
2388 if (Class != IC_DestroyWeak)
2391 CallInst *Call = cast<CallInst>(Inst);
2392 Value *Arg = Call->getArgOperand(0);
2393 if (AllocaInst *Alloca = dyn_cast<AllocaInst>(Arg)) {
2394 for (Value::use_iterator UI = Alloca->use_begin(),
2395 UE = Alloca->use_end(); UI != UE; ++UI) {
2396 const Instruction *UserInst = cast<Instruction>(*UI);
2397 switch (GetBasicInstructionClass(UserInst)) {
2400 case IC_DestroyWeak:
2407 for (Value::use_iterator UI = Alloca->use_begin(),
2408 UE = Alloca->use_end(); UI != UE; ) {
2409 CallInst *UserInst = cast<CallInst>(*UI++);
2410 switch (GetBasicInstructionClass(UserInst)) {
2413 // These functions return their second argument.
2414 UserInst->replaceAllUsesWith(UserInst->getArgOperand(1));
2416 case IC_DestroyWeak:
2420 llvm_unreachable("alloca really is used!");
2422 UserInst->eraseFromParent();
2424 Alloca->eraseFromParent();
2429 DEBUG(dbgs() << "ObjCARCOpt::OptimizeWeakCalls: Finished List.\n\n");
2433 /// Identify program paths which execute sequences of retains and releases which
2434 /// can be eliminated.
2435 bool ObjCARCOpt::OptimizeSequences(Function &F) {
2436 /// Releases, Retains - These are used to store the results of the main flow
2437 /// analysis. These use Value* as the key instead of Instruction* so that the
2438 /// map stays valid when we get around to rewriting code and calls get
2439 /// replaced by arguments.
2440 DenseMap<Value *, RRInfo> Releases;
2441 MapVector<Value *, RRInfo> Retains;
2443 /// This is used during the traversal of the function to track the
2444 /// states for each identified object at each block.
2445 DenseMap<const BasicBlock *, BBState> BBStates;
2447 // Analyze the CFG of the function, and all instructions.
2448 bool NestingDetected = Visit(F, BBStates, Retains, Releases);
2451 return PerformCodePlacement(BBStates, Retains, Releases, F.getParent()) &&
2455 /// Look for this pattern:
2457 /// %call = call i8* @something(...)
2458 /// %2 = call i8* @objc_retain(i8* %call)
2459 /// %3 = call i8* @objc_autorelease(i8* %2)
2462 /// And delete the retain and autorelease.
2464 /// Otherwise if it's just this:
2466 /// %3 = call i8* @objc_autorelease(i8* %2)
2469 /// convert the autorelease to autoreleaseRV.
2470 void ObjCARCOpt::OptimizeReturns(Function &F) {
2471 if (!F.getReturnType()->isPointerTy())
2474 SmallPtrSet<Instruction *, 4> DependingInstructions;
2475 SmallPtrSet<const BasicBlock *, 4> Visited;
2476 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI) {
2477 BasicBlock *BB = FI;
2478 ReturnInst *Ret = dyn_cast<ReturnInst>(&BB->back());
2480 DEBUG(dbgs() << "ObjCARCOpt::OptimizeReturns: Visiting: " << *Ret << "\n");
2484 const Value *Arg = StripPointerCastsAndObjCCalls(Ret->getOperand(0));
2485 FindDependencies(NeedsPositiveRetainCount, Arg,
2486 BB, Ret, DependingInstructions, Visited, PA);
2487 if (DependingInstructions.size() != 1)
2491 CallInst *Autorelease =
2492 dyn_cast_or_null<CallInst>(*DependingInstructions.begin());
2495 InstructionClass AutoreleaseClass = GetBasicInstructionClass(Autorelease);
2496 if (!IsAutorelease(AutoreleaseClass))
2498 if (GetObjCArg(Autorelease) != Arg)
2501 DependingInstructions.clear();
2504 // Check that there is nothing that can affect the reference
2505 // count between the autorelease and the retain.
2506 FindDependencies(CanChangeRetainCount, Arg,
2507 BB, Autorelease, DependingInstructions, Visited, PA);
2508 if (DependingInstructions.size() != 1)
2513 dyn_cast_or_null<CallInst>(*DependingInstructions.begin());
2515 // Check that we found a retain with the same argument.
2517 !IsRetain(GetBasicInstructionClass(Retain)) ||
2518 GetObjCArg(Retain) != Arg)
2521 DependingInstructions.clear();
2524 // Convert the autorelease to an autoreleaseRV, since it's
2525 // returning the value.
2526 if (AutoreleaseClass == IC_Autorelease) {
2527 DEBUG(dbgs() << "ObjCARCOpt::OptimizeReturns: Converting autorelease "
2528 "=> autoreleaseRV since it's returning a value.\n"
2529 " In: " << *Autorelease
2531 Autorelease->setCalledFunction(getAutoreleaseRVCallee(F.getParent()));
2532 DEBUG(dbgs() << " Out: " << *Autorelease
2534 Autorelease->setTailCall(); // Always tail call autoreleaseRV.
2535 AutoreleaseClass = IC_AutoreleaseRV;
2538 // Check that there is nothing that can affect the reference
2539 // count between the retain and the call.
2540 // Note that Retain need not be in BB.
2541 FindDependencies(CanChangeRetainCount, Arg, Retain->getParent(), Retain,
2542 DependingInstructions, Visited, PA);
2543 if (DependingInstructions.size() != 1)
2548 dyn_cast_or_null<CallInst>(*DependingInstructions.begin());
2550 // Check that the pointer is the return value of the call.
2551 if (!Call || Arg != Call)
2554 // Check that the call is a regular call.
2555 InstructionClass Class = GetBasicInstructionClass(Call);
2556 if (Class != IC_CallOrUser && Class != IC_Call)
2559 // If so, we can zap the retain and autorelease.
2562 DEBUG(dbgs() << "ObjCARCOpt::OptimizeReturns: Erasing: " << *Retain
2564 << *Autorelease << "\n");
2565 EraseInstruction(Retain);
2566 EraseInstruction(Autorelease);
2572 DependingInstructions.clear();
2576 DEBUG(dbgs() << "ObjCARCOpt::OptimizeReturns: Finished List.\n\n");
2580 bool ObjCARCOpt::doInitialization(Module &M) {
2584 // If nothing in the Module uses ARC, don't do anything.
2585 Run = ModuleHasARC(M);
2589 // Identify the imprecise release metadata kind.
2590 ImpreciseReleaseMDKind =
2591 M.getContext().getMDKindID("clang.imprecise_release");
2592 CopyOnEscapeMDKind =
2593 M.getContext().getMDKindID("clang.arc.copy_on_escape");
2594 NoObjCARCExceptionsMDKind =
2595 M.getContext().getMDKindID("clang.arc.no_objc_arc_exceptions");
2597 // Intuitively, objc_retain and others are nocapture, however in practice
2598 // they are not, because they return their argument value. And objc_release
2599 // calls finalizers which can have arbitrary side effects.
2601 // These are initialized lazily.
2603 AutoreleaseRVCallee = 0;
2606 RetainBlockCallee = 0;
2607 AutoreleaseCallee = 0;
2612 bool ObjCARCOpt::runOnFunction(Function &F) {
2616 // If nothing in the Module uses ARC, don't do anything.
2622 DEBUG(dbgs() << "ObjCARCOpt: Visiting Function: " << F.getName() << "\n");
2624 PA.setAA(&getAnalysis<AliasAnalysis>());
2626 // This pass performs several distinct transformations. As a compile-time aid
2627 // when compiling code that isn't ObjC, skip these if the relevant ObjC
2628 // library functions aren't declared.
2630 // Preliminary optimizations. This also computs UsedInThisFunction.
2631 OptimizeIndividualCalls(F);
2633 // Optimizations for weak pointers.
2634 if (UsedInThisFunction & ((1 << IC_LoadWeak) |
2635 (1 << IC_LoadWeakRetained) |
2636 (1 << IC_StoreWeak) |
2637 (1 << IC_InitWeak) |
2638 (1 << IC_CopyWeak) |
2639 (1 << IC_MoveWeak) |
2640 (1 << IC_DestroyWeak)))
2641 OptimizeWeakCalls(F);
2643 // Optimizations for retain+release pairs.
2644 if (UsedInThisFunction & ((1 << IC_Retain) |
2645 (1 << IC_RetainRV) |
2646 (1 << IC_RetainBlock)))
2647 if (UsedInThisFunction & (1 << IC_Release))
2648 // Run OptimizeSequences until it either stops making changes or
2649 // no retain+release pair nesting is detected.
2650 while (OptimizeSequences(F)) {}
2652 // Optimizations if objc_autorelease is used.
2653 if (UsedInThisFunction & ((1 << IC_Autorelease) |
2654 (1 << IC_AutoreleaseRV)))
2657 DEBUG(dbgs() << "\n");
2662 void ObjCARCOpt::releaseMemory() {