1 //===- ObjCARCOpts.cpp - ObjC ARC Optimization ----------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 /// \file
10 /// This file defines ObjC ARC optimizations. ARC stands for Automatic
11 /// Reference Counting and is a system for managing reference counts for objects
12 /// in Objective C.
13 ///
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.
17 ///
18 /// WARNING: This file knows about certain library functions. It recognizes them
19 /// by name, and hardwires knowledge of their semantics.
20 ///
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.
24 ///
25 //===----------------------------------------------------------------------===//
26 
27 #include "ObjCARC.h"
28 #include "ARCRuntimeEntryPoints.h"
29 #include "BlotMapVector.h"
30 #include "DependencyAnalysis.h"
31 #include "ProvenanceAnalysis.h"
32 #include "PtrState.h"
33 #include "llvm/ADT/DenseMap.h"
34 #include "llvm/ADT/DenseSet.h"
35 #include "llvm/ADT/STLExtras.h"
36 #include "llvm/ADT/SmallPtrSet.h"
37 #include "llvm/ADT/Statistic.h"
38 #include "llvm/Analysis/ObjCARCAliasAnalysis.h"
39 #include "llvm/IR/CFG.h"
40 #include "llvm/IR/IRBuilder.h"
41 #include "llvm/IR/LLVMContext.h"
42 #include "llvm/Support/Debug.h"
43 #include "llvm/Support/raw_ostream.h"
44 
45 using namespace llvm;
46 using namespace llvm::objcarc;
47 
48 #define DEBUG_TYPE "objc-arc-opts"
49 
50 /// \defgroup ARCUtilities Utility declarations/definitions specific to ARC.
51 /// @{
52 
53 /// \brief This is similar to GetRCIdentityRoot but it stops as soon
54 /// as it finds a value with multiple uses.
55 static const Value *FindSingleUseIdentifiedObject(const Value *Arg) {
56   // ConstantData (like ConstantPointerNull and UndefValue) is used across
57   // modules.  It's never a single-use value.
58   if (isa<ConstantData>(Arg))
59     return nullptr;
60 
61   if (Arg->hasOneUse()) {
62     if (const BitCastInst *BC = dyn_cast<BitCastInst>(Arg))
63       return FindSingleUseIdentifiedObject(BC->getOperand(0));
64     if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Arg))
65       if (GEP->hasAllZeroIndices())
66         return FindSingleUseIdentifiedObject(GEP->getPointerOperand());
67     if (IsForwarding(GetBasicARCInstKind(Arg)))
68       return FindSingleUseIdentifiedObject(
69                cast<CallInst>(Arg)->getArgOperand(0));
70     if (!IsObjCIdentifiedObject(Arg))
71       return nullptr;
72     return Arg;
73   }
74 
75   // If we found an identifiable object but it has multiple uses, but they are
76   // trivial uses, we can still consider this to be a single-use value.
77   if (IsObjCIdentifiedObject(Arg)) {
78     for (const User *U : Arg->users())
79       if (!U->use_empty() || GetRCIdentityRoot(U) != Arg)
80          return nullptr;
81 
82     return Arg;
83   }
84 
85   return nullptr;
86 }
87 
88 /// This is a wrapper around getUnderlyingObjCPtr along the lines of
89 /// GetUnderlyingObjects except that it returns early when it sees the first
90 /// alloca.
91 static inline bool AreAnyUnderlyingObjectsAnAlloca(const Value *V,
92                                                    const DataLayout &DL) {
93   SmallPtrSet<const Value *, 4> Visited;
94   SmallVector<const Value *, 4> Worklist;
95   Worklist.push_back(V);
96   do {
97     const Value *P = Worklist.pop_back_val();
98     P = GetUnderlyingObjCPtr(P, DL);
99 
100     if (isa<AllocaInst>(P))
101       return true;
102 
103     if (!Visited.insert(P).second)
104       continue;
105 
106     if (const SelectInst *SI = dyn_cast<const SelectInst>(P)) {
107       Worklist.push_back(SI->getTrueValue());
108       Worklist.push_back(SI->getFalseValue());
109       continue;
110     }
111 
112     if (const PHINode *PN = dyn_cast<const PHINode>(P)) {
113       for (Value *IncValue : PN->incoming_values())
114         Worklist.push_back(IncValue);
115       continue;
116     }
117   } while (!Worklist.empty());
118 
119   return false;
120 }
121 
122 
123 /// @}
124 ///
125 /// \defgroup ARCOpt ARC Optimization.
126 /// @{
127 
128 // TODO: On code like this:
129 //
130 // objc_retain(%x)
131 // stuff_that_cannot_release()
132 // objc_autorelease(%x)
133 // stuff_that_cannot_release()
134 // objc_retain(%x)
135 // stuff_that_cannot_release()
136 // objc_autorelease(%x)
137 //
138 // The second retain and autorelease can be deleted.
139 
140 // TODO: It should be possible to delete
141 // objc_autoreleasePoolPush and objc_autoreleasePoolPop
142 // pairs if nothing is actually autoreleased between them. Also, autorelease
143 // calls followed by objc_autoreleasePoolPop calls (perhaps in ObjC++ code
144 // after inlining) can be turned into plain release calls.
145 
146 // TODO: Critical-edge splitting. If the optimial insertion point is
147 // a critical edge, the current algorithm has to fail, because it doesn't
148 // know how to split edges. It should be possible to make the optimizer
149 // think in terms of edges, rather than blocks, and then split critical
150 // edges on demand.
151 
152 // TODO: OptimizeSequences could generalized to be Interprocedural.
153 
154 // TODO: Recognize that a bunch of other objc runtime calls have
155 // non-escaping arguments and non-releasing arguments, and may be
156 // non-autoreleasing.
157 
158 // TODO: Sink autorelease calls as far as possible. Unfortunately we
159 // usually can't sink them past other calls, which would be the main
160 // case where it would be useful.
161 
162 // TODO: The pointer returned from objc_loadWeakRetained is retained.
163 
164 // TODO: Delete release+retain pairs (rare).
165 
166 STATISTIC(NumNoops,       "Number of no-op objc calls eliminated");
167 STATISTIC(NumPartialNoops, "Number of partially no-op objc calls eliminated");
168 STATISTIC(NumAutoreleases,"Number of autoreleases converted to releases");
169 STATISTIC(NumRets,        "Number of return value forwarding "
170                           "retain+autoreleases eliminated");
171 STATISTIC(NumRRs,         "Number of retain+release paths eliminated");
172 STATISTIC(NumPeeps,       "Number of calls peephole-optimized");
173 #ifndef NDEBUG
174 STATISTIC(NumRetainsBeforeOpt,
175           "Number of retains before optimization");
176 STATISTIC(NumReleasesBeforeOpt,
177           "Number of releases before optimization");
178 STATISTIC(NumRetainsAfterOpt,
179           "Number of retains after optimization");
180 STATISTIC(NumReleasesAfterOpt,
181           "Number of releases after optimization");
182 #endif
183 
184 namespace {
185   /// \brief Per-BasicBlock state.
186   class BBState {
187     /// The number of unique control paths from the entry which can reach this
188     /// block.
189     unsigned TopDownPathCount;
190 
191     /// The number of unique control paths to exits from this block.
192     unsigned BottomUpPathCount;
193 
194     /// The top-down traversal uses this to record information known about a
195     /// pointer at the bottom of each block.
196     BlotMapVector<const Value *, TopDownPtrState> PerPtrTopDown;
197 
198     /// The bottom-up traversal uses this to record information known about a
199     /// pointer at the top of each block.
200     BlotMapVector<const Value *, BottomUpPtrState> PerPtrBottomUp;
201 
202     /// Effective predecessors of the current block ignoring ignorable edges and
203     /// ignored backedges.
204     SmallVector<BasicBlock *, 2> Preds;
205 
206     /// Effective successors of the current block ignoring ignorable edges and
207     /// ignored backedges.
208     SmallVector<BasicBlock *, 2> Succs;
209 
210   public:
211     static const unsigned OverflowOccurredValue;
212 
213     BBState() : TopDownPathCount(0), BottomUpPathCount(0) { }
214 
215     typedef decltype(PerPtrTopDown)::iterator top_down_ptr_iterator;
216     typedef decltype(PerPtrTopDown)::const_iterator const_top_down_ptr_iterator;
217 
218     top_down_ptr_iterator top_down_ptr_begin() { return PerPtrTopDown.begin(); }
219     top_down_ptr_iterator top_down_ptr_end() { return PerPtrTopDown.end(); }
220     const_top_down_ptr_iterator top_down_ptr_begin() const {
221       return PerPtrTopDown.begin();
222     }
223     const_top_down_ptr_iterator top_down_ptr_end() const {
224       return PerPtrTopDown.end();
225     }
226     bool hasTopDownPtrs() const {
227       return !PerPtrTopDown.empty();
228     }
229 
230     typedef decltype(PerPtrBottomUp)::iterator bottom_up_ptr_iterator;
231     typedef decltype(
232         PerPtrBottomUp)::const_iterator const_bottom_up_ptr_iterator;
233 
234     bottom_up_ptr_iterator bottom_up_ptr_begin() {
235       return PerPtrBottomUp.begin();
236     }
237     bottom_up_ptr_iterator bottom_up_ptr_end() { return PerPtrBottomUp.end(); }
238     const_bottom_up_ptr_iterator bottom_up_ptr_begin() const {
239       return PerPtrBottomUp.begin();
240     }
241     const_bottom_up_ptr_iterator bottom_up_ptr_end() const {
242       return PerPtrBottomUp.end();
243     }
244     bool hasBottomUpPtrs() const {
245       return !PerPtrBottomUp.empty();
246     }
247 
248     /// Mark this block as being an entry block, which has one path from the
249     /// entry by definition.
250     void SetAsEntry() { TopDownPathCount = 1; }
251 
252     /// Mark this block as being an exit block, which has one path to an exit by
253     /// definition.
254     void SetAsExit()  { BottomUpPathCount = 1; }
255 
256     /// Attempt to find the PtrState object describing the top down state for
257     /// pointer Arg. Return a new initialized PtrState describing the top down
258     /// state for Arg if we do not find one.
259     TopDownPtrState &getPtrTopDownState(const Value *Arg) {
260       return PerPtrTopDown[Arg];
261     }
262 
263     /// Attempt to find the PtrState object describing the bottom up state for
264     /// pointer Arg. Return a new initialized PtrState describing the bottom up
265     /// state for Arg if we do not find one.
266     BottomUpPtrState &getPtrBottomUpState(const Value *Arg) {
267       return PerPtrBottomUp[Arg];
268     }
269 
270     /// Attempt to find the PtrState object describing the bottom up state for
271     /// pointer Arg.
272     bottom_up_ptr_iterator findPtrBottomUpState(const Value *Arg) {
273       return PerPtrBottomUp.find(Arg);
274     }
275 
276     void clearBottomUpPointers() {
277       PerPtrBottomUp.clear();
278     }
279 
280     void clearTopDownPointers() {
281       PerPtrTopDown.clear();
282     }
283 
284     void InitFromPred(const BBState &Other);
285     void InitFromSucc(const BBState &Other);
286     void MergePred(const BBState &Other);
287     void MergeSucc(const BBState &Other);
288 
289     /// Compute the number of possible unique paths from an entry to an exit
290     /// which pass through this block. This is only valid after both the
291     /// top-down and bottom-up traversals are complete.
292     ///
293     /// Returns true if overflow occurred. Returns false if overflow did not
294     /// occur.
295     bool GetAllPathCountWithOverflow(unsigned &PathCount) const {
296       if (TopDownPathCount == OverflowOccurredValue ||
297           BottomUpPathCount == OverflowOccurredValue)
298         return true;
299       unsigned long long Product =
300         (unsigned long long)TopDownPathCount*BottomUpPathCount;
301       // Overflow occurred if any of the upper bits of Product are set or if all
302       // the lower bits of Product are all set.
303       return (Product >> 32) ||
304              ((PathCount = Product) == OverflowOccurredValue);
305     }
306 
307     // Specialized CFG utilities.
308     typedef SmallVectorImpl<BasicBlock *>::const_iterator edge_iterator;
309     edge_iterator pred_begin() const { return Preds.begin(); }
310     edge_iterator pred_end() const { return Preds.end(); }
311     edge_iterator succ_begin() const { return Succs.begin(); }
312     edge_iterator succ_end() const { return Succs.end(); }
313 
314     void addSucc(BasicBlock *Succ) { Succs.push_back(Succ); }
315     void addPred(BasicBlock *Pred) { Preds.push_back(Pred); }
316 
317     bool isExit() const { return Succs.empty(); }
318   };
319 
320   const unsigned BBState::OverflowOccurredValue = 0xffffffff;
321 }
322 
323 namespace llvm {
324 raw_ostream &operator<<(raw_ostream &OS,
325                         BBState &BBState) LLVM_ATTRIBUTE_UNUSED;
326 }
327 
328 void BBState::InitFromPred(const BBState &Other) {
329   PerPtrTopDown = Other.PerPtrTopDown;
330   TopDownPathCount = Other.TopDownPathCount;
331 }
332 
333 void BBState::InitFromSucc(const BBState &Other) {
334   PerPtrBottomUp = Other.PerPtrBottomUp;
335   BottomUpPathCount = Other.BottomUpPathCount;
336 }
337 
338 /// The top-down traversal uses this to merge information about predecessors to
339 /// form the initial state for a new block.
340 void BBState::MergePred(const BBState &Other) {
341   if (TopDownPathCount == OverflowOccurredValue)
342     return;
343 
344   // Other.TopDownPathCount can be 0, in which case it is either dead or a
345   // loop backedge. Loop backedges are special.
346   TopDownPathCount += Other.TopDownPathCount;
347 
348   // In order to be consistent, we clear the top down pointers when by adding
349   // TopDownPathCount becomes OverflowOccurredValue even though "true" overflow
350   // has not occurred.
351   if (TopDownPathCount == OverflowOccurredValue) {
352     clearTopDownPointers();
353     return;
354   }
355 
356   // Check for overflow. If we have overflow, fall back to conservative
357   // behavior.
358   if (TopDownPathCount < Other.TopDownPathCount) {
359     TopDownPathCount = OverflowOccurredValue;
360     clearTopDownPointers();
361     return;
362   }
363 
364   // For each entry in the other set, if our set has an entry with the same key,
365   // merge the entries. Otherwise, copy the entry and merge it with an empty
366   // entry.
367   for (auto MI = Other.top_down_ptr_begin(), ME = Other.top_down_ptr_end();
368        MI != ME; ++MI) {
369     auto Pair = PerPtrTopDown.insert(*MI);
370     Pair.first->second.Merge(Pair.second ? TopDownPtrState() : MI->second,
371                              /*TopDown=*/true);
372   }
373 
374   // For each entry in our set, if the other set doesn't have an entry with the
375   // same key, force it to merge with an empty entry.
376   for (auto MI = top_down_ptr_begin(), ME = top_down_ptr_end(); MI != ME; ++MI)
377     if (Other.PerPtrTopDown.find(MI->first) == Other.PerPtrTopDown.end())
378       MI->second.Merge(TopDownPtrState(), /*TopDown=*/true);
379 }
380 
381 /// The bottom-up traversal uses this to merge information about successors to
382 /// form the initial state for a new block.
383 void BBState::MergeSucc(const BBState &Other) {
384   if (BottomUpPathCount == OverflowOccurredValue)
385     return;
386 
387   // Other.BottomUpPathCount can be 0, in which case it is either dead or a
388   // loop backedge. Loop backedges are special.
389   BottomUpPathCount += Other.BottomUpPathCount;
390 
391   // In order to be consistent, we clear the top down pointers when by adding
392   // BottomUpPathCount becomes OverflowOccurredValue even though "true" overflow
393   // has not occurred.
394   if (BottomUpPathCount == OverflowOccurredValue) {
395     clearBottomUpPointers();
396     return;
397   }
398 
399   // Check for overflow. If we have overflow, fall back to conservative
400   // behavior.
401   if (BottomUpPathCount < Other.BottomUpPathCount) {
402     BottomUpPathCount = OverflowOccurredValue;
403     clearBottomUpPointers();
404     return;
405   }
406 
407   // For each entry in the other set, if our set has an entry with the
408   // same key, merge the entries. Otherwise, copy the entry and merge
409   // it with an empty entry.
410   for (auto MI = Other.bottom_up_ptr_begin(), ME = Other.bottom_up_ptr_end();
411        MI != ME; ++MI) {
412     auto Pair = PerPtrBottomUp.insert(*MI);
413     Pair.first->second.Merge(Pair.second ? BottomUpPtrState() : MI->second,
414                              /*TopDown=*/false);
415   }
416 
417   // For each entry in our set, if the other set doesn't have an entry
418   // with the same key, force it to merge with an empty entry.
419   for (auto MI = bottom_up_ptr_begin(), ME = bottom_up_ptr_end(); MI != ME;
420        ++MI)
421     if (Other.PerPtrBottomUp.find(MI->first) == Other.PerPtrBottomUp.end())
422       MI->second.Merge(BottomUpPtrState(), /*TopDown=*/false);
423 }
424 
425 raw_ostream &llvm::operator<<(raw_ostream &OS, BBState &BBInfo) {
426   // Dump the pointers we are tracking.
427   OS << "    TopDown State:\n";
428   if (!BBInfo.hasTopDownPtrs()) {
429     DEBUG(llvm::dbgs() << "        NONE!\n");
430   } else {
431     for (auto I = BBInfo.top_down_ptr_begin(), E = BBInfo.top_down_ptr_end();
432          I != E; ++I) {
433       const PtrState &P = I->second;
434       OS << "        Ptr: " << *I->first
435          << "\n            KnownSafe:        " << (P.IsKnownSafe()?"true":"false")
436          << "\n            ImpreciseRelease: "
437            << (P.IsTrackingImpreciseReleases()?"true":"false") << "\n"
438          << "            HasCFGHazards:    "
439            << (P.IsCFGHazardAfflicted()?"true":"false") << "\n"
440          << "            KnownPositive:    "
441            << (P.HasKnownPositiveRefCount()?"true":"false") << "\n"
442          << "            Seq:              "
443          << P.GetSeq() << "\n";
444     }
445   }
446 
447   OS << "    BottomUp State:\n";
448   if (!BBInfo.hasBottomUpPtrs()) {
449     DEBUG(llvm::dbgs() << "        NONE!\n");
450   } else {
451     for (auto I = BBInfo.bottom_up_ptr_begin(), E = BBInfo.bottom_up_ptr_end();
452          I != E; ++I) {
453       const PtrState &P = I->second;
454       OS << "        Ptr: " << *I->first
455          << "\n            KnownSafe:        " << (P.IsKnownSafe()?"true":"false")
456          << "\n            ImpreciseRelease: "
457            << (P.IsTrackingImpreciseReleases()?"true":"false") << "\n"
458          << "            HasCFGHazards:    "
459            << (P.IsCFGHazardAfflicted()?"true":"false") << "\n"
460          << "            KnownPositive:    "
461            << (P.HasKnownPositiveRefCount()?"true":"false") << "\n"
462          << "            Seq:              "
463          << P.GetSeq() << "\n";
464     }
465   }
466 
467   return OS;
468 }
469 
470 namespace {
471 
472   /// \brief The main ARC optimization pass.
473   class ObjCARCOpt : public FunctionPass {
474     bool Changed;
475     ProvenanceAnalysis PA;
476 
477     /// A cache of references to runtime entry point constants.
478     ARCRuntimeEntryPoints EP;
479 
480     /// A cache of MDKinds that can be passed into other functions to propagate
481     /// MDKind identifiers.
482     ARCMDKindCache MDKindCache;
483 
484     // This is used to track if a pointer is stored into an alloca.
485     DenseSet<const Value *> MultiOwnersSet;
486 
487     /// A flag indicating whether this optimization pass should run.
488     bool Run;
489 
490     /// Flags which determine whether each of the interesting runtime functions
491     /// is in fact used in the current function.
492     unsigned UsedInThisFunction;
493 
494     bool OptimizeRetainRVCall(Function &F, Instruction *RetainRV);
495     void OptimizeAutoreleaseRVCall(Function &F, Instruction *AutoreleaseRV,
496                                    ARCInstKind &Class);
497     void OptimizeIndividualCalls(Function &F);
498 
499     void CheckForCFGHazards(const BasicBlock *BB,
500                             DenseMap<const BasicBlock *, BBState> &BBStates,
501                             BBState &MyStates) const;
502     bool VisitInstructionBottomUp(Instruction *Inst, BasicBlock *BB,
503                                   BlotMapVector<Value *, RRInfo> &Retains,
504                                   BBState &MyStates);
505     bool VisitBottomUp(BasicBlock *BB,
506                        DenseMap<const BasicBlock *, BBState> &BBStates,
507                        BlotMapVector<Value *, RRInfo> &Retains);
508     bool VisitInstructionTopDown(Instruction *Inst,
509                                  DenseMap<Value *, RRInfo> &Releases,
510                                  BBState &MyStates);
511     bool VisitTopDown(BasicBlock *BB,
512                       DenseMap<const BasicBlock *, BBState> &BBStates,
513                       DenseMap<Value *, RRInfo> &Releases);
514     bool Visit(Function &F, DenseMap<const BasicBlock *, BBState> &BBStates,
515                BlotMapVector<Value *, RRInfo> &Retains,
516                DenseMap<Value *, RRInfo> &Releases);
517 
518     void MoveCalls(Value *Arg, RRInfo &RetainsToMove, RRInfo &ReleasesToMove,
519                    BlotMapVector<Value *, RRInfo> &Retains,
520                    DenseMap<Value *, RRInfo> &Releases,
521                    SmallVectorImpl<Instruction *> &DeadInsts, Module *M);
522 
523     bool
524     PairUpRetainsAndReleases(DenseMap<const BasicBlock *, BBState> &BBStates,
525                              BlotMapVector<Value *, RRInfo> &Retains,
526                              DenseMap<Value *, RRInfo> &Releases, Module *M,
527                              SmallVectorImpl<Instruction *> &NewRetains,
528                              SmallVectorImpl<Instruction *> &NewReleases,
529                              SmallVectorImpl<Instruction *> &DeadInsts,
530                              RRInfo &RetainsToMove, RRInfo &ReleasesToMove,
531                              Value *Arg, bool KnownSafe,
532                              bool &AnyPairsCompletelyEliminated);
533 
534     bool PerformCodePlacement(DenseMap<const BasicBlock *, BBState> &BBStates,
535                               BlotMapVector<Value *, RRInfo> &Retains,
536                               DenseMap<Value *, RRInfo> &Releases, Module *M);
537 
538     void OptimizeWeakCalls(Function &F);
539 
540     bool OptimizeSequences(Function &F);
541 
542     void OptimizeReturns(Function &F);
543 
544 #ifndef NDEBUG
545     void GatherStatistics(Function &F, bool AfterOptimization = false);
546 #endif
547 
548     void getAnalysisUsage(AnalysisUsage &AU) const override;
549     bool doInitialization(Module &M) override;
550     bool runOnFunction(Function &F) override;
551     void releaseMemory() override;
552 
553   public:
554     static char ID;
555     ObjCARCOpt() : FunctionPass(ID) {
556       initializeObjCARCOptPass(*PassRegistry::getPassRegistry());
557     }
558   };
559 }
560 
561 char ObjCARCOpt::ID = 0;
562 INITIALIZE_PASS_BEGIN(ObjCARCOpt,
563                       "objc-arc", "ObjC ARC optimization", false, false)
564 INITIALIZE_PASS_DEPENDENCY(ObjCARCAAWrapperPass)
565 INITIALIZE_PASS_END(ObjCARCOpt,
566                     "objc-arc", "ObjC ARC optimization", false, false)
567 
568 Pass *llvm::createObjCARCOptPass() {
569   return new ObjCARCOpt();
570 }
571 
572 void ObjCARCOpt::getAnalysisUsage(AnalysisUsage &AU) const {
573   AU.addRequired<ObjCARCAAWrapperPass>();
574   AU.addRequired<AAResultsWrapperPass>();
575   // ARC optimization doesn't currently split critical edges.
576   AU.setPreservesCFG();
577 }
578 
579 /// Turn objc_retainAutoreleasedReturnValue into objc_retain if the operand is
580 /// not a return value.  Or, if it can be paired with an
581 /// objc_autoreleaseReturnValue, delete the pair and return true.
582 bool
583 ObjCARCOpt::OptimizeRetainRVCall(Function &F, Instruction *RetainRV) {
584   // Check for the argument being from an immediately preceding call or invoke.
585   const Value *Arg = GetArgRCIdentityRoot(RetainRV);
586   ImmutableCallSite CS(Arg);
587   if (const Instruction *Call = CS.getInstruction()) {
588     if (Call->getParent() == RetainRV->getParent()) {
589       BasicBlock::const_iterator I(Call);
590       ++I;
591       while (IsNoopInstruction(&*I))
592         ++I;
593       if (&*I == RetainRV)
594         return false;
595     } else if (const InvokeInst *II = dyn_cast<InvokeInst>(Call)) {
596       BasicBlock *RetainRVParent = RetainRV->getParent();
597       if (II->getNormalDest() == RetainRVParent) {
598         BasicBlock::const_iterator I = RetainRVParent->begin();
599         while (IsNoopInstruction(&*I))
600           ++I;
601         if (&*I == RetainRV)
602           return false;
603       }
604     }
605   }
606 
607   // Check for being preceded by an objc_autoreleaseReturnValue on the same
608   // pointer. In this case, we can delete the pair.
609   BasicBlock::iterator I = RetainRV->getIterator(),
610                        Begin = RetainRV->getParent()->begin();
611   if (I != Begin) {
612     do
613       --I;
614     while (I != Begin && IsNoopInstruction(&*I));
615     if (GetBasicARCInstKind(&*I) == ARCInstKind::AutoreleaseRV &&
616         GetArgRCIdentityRoot(&*I) == Arg) {
617       Changed = true;
618       ++NumPeeps;
619 
620       DEBUG(dbgs() << "Erasing autoreleaseRV,retainRV pair: " << *I << "\n"
621                    << "Erasing " << *RetainRV << "\n");
622 
623       EraseInstruction(&*I);
624       EraseInstruction(RetainRV);
625       return true;
626     }
627   }
628 
629   // Turn it to a plain objc_retain.
630   Changed = true;
631   ++NumPeeps;
632 
633   DEBUG(dbgs() << "Transforming objc_retainAutoreleasedReturnValue => "
634                   "objc_retain since the operand is not a return value.\n"
635                   "Old = " << *RetainRV << "\n");
636 
637   Constant *NewDecl = EP.get(ARCRuntimeEntryPointKind::Retain);
638   cast<CallInst>(RetainRV)->setCalledFunction(NewDecl);
639 
640   DEBUG(dbgs() << "New = " << *RetainRV << "\n");
641 
642   return false;
643 }
644 
645 /// Turn objc_autoreleaseReturnValue into objc_autorelease if the result is not
646 /// used as a return value.
647 void ObjCARCOpt::OptimizeAutoreleaseRVCall(Function &F,
648                                            Instruction *AutoreleaseRV,
649                                            ARCInstKind &Class) {
650   // Check for a return of the pointer value.
651   const Value *Ptr = GetArgRCIdentityRoot(AutoreleaseRV);
652 
653   // If the argument is ConstantPointerNull or UndefValue, its other users
654   // aren't actually interesting to look at.
655   if (isa<ConstantData>(Ptr))
656     return;
657 
658   SmallVector<const Value *, 2> Users;
659   Users.push_back(Ptr);
660   do {
661     Ptr = Users.pop_back_val();
662     for (const User *U : Ptr->users()) {
663       if (isa<ReturnInst>(U) || GetBasicARCInstKind(U) == ARCInstKind::RetainRV)
664         return;
665       if (isa<BitCastInst>(U))
666         Users.push_back(U);
667     }
668   } while (!Users.empty());
669 
670   Changed = true;
671   ++NumPeeps;
672 
673   DEBUG(dbgs() << "Transforming objc_autoreleaseReturnValue => "
674                   "objc_autorelease since its operand is not used as a return "
675                   "value.\n"
676                   "Old = " << *AutoreleaseRV << "\n");
677 
678   CallInst *AutoreleaseRVCI = cast<CallInst>(AutoreleaseRV);
679   Constant *NewDecl = EP.get(ARCRuntimeEntryPointKind::Autorelease);
680   AutoreleaseRVCI->setCalledFunction(NewDecl);
681   AutoreleaseRVCI->setTailCall(false); // Never tail call objc_autorelease.
682   Class = ARCInstKind::Autorelease;
683 
684   DEBUG(dbgs() << "New: " << *AutoreleaseRV << "\n");
685 
686 }
687 
688 /// Visit each call, one at a time, and make simplifications without doing any
689 /// additional analysis.
690 void ObjCARCOpt::OptimizeIndividualCalls(Function &F) {
691   DEBUG(dbgs() << "\n== ObjCARCOpt::OptimizeIndividualCalls ==\n");
692   // Reset all the flags in preparation for recomputing them.
693   UsedInThisFunction = 0;
694 
695   // Visit all objc_* calls in F.
696   for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
697     Instruction *Inst = &*I++;
698 
699     ARCInstKind Class = GetBasicARCInstKind(Inst);
700 
701     DEBUG(dbgs() << "Visiting: Class: " << Class << "; " << *Inst << "\n");
702 
703     switch (Class) {
704     default: break;
705 
706     // Delete no-op casts. These function calls have special semantics, but
707     // the semantics are entirely implemented via lowering in the front-end,
708     // so by the time they reach the optimizer, they are just no-op calls
709     // which return their argument.
710     //
711     // There are gray areas here, as the ability to cast reference-counted
712     // pointers to raw void* and back allows code to break ARC assumptions,
713     // however these are currently considered to be unimportant.
714     case ARCInstKind::NoopCast:
715       Changed = true;
716       ++NumNoops;
717       DEBUG(dbgs() << "Erasing no-op cast: " << *Inst << "\n");
718       EraseInstruction(Inst);
719       continue;
720 
721     // If the pointer-to-weak-pointer is null, it's undefined behavior.
722     case ARCInstKind::StoreWeak:
723     case ARCInstKind::LoadWeak:
724     case ARCInstKind::LoadWeakRetained:
725     case ARCInstKind::InitWeak:
726     case ARCInstKind::DestroyWeak: {
727       CallInst *CI = cast<CallInst>(Inst);
728       if (IsNullOrUndef(CI->getArgOperand(0))) {
729         Changed = true;
730         Type *Ty = CI->getArgOperand(0)->getType();
731         new StoreInst(UndefValue::get(cast<PointerType>(Ty)->getElementType()),
732                       Constant::getNullValue(Ty),
733                       CI);
734         llvm::Value *NewValue = UndefValue::get(CI->getType());
735         DEBUG(dbgs() << "A null pointer-to-weak-pointer is undefined behavior."
736                        "\nOld = " << *CI << "\nNew = " << *NewValue << "\n");
737         CI->replaceAllUsesWith(NewValue);
738         CI->eraseFromParent();
739         continue;
740       }
741       break;
742     }
743     case ARCInstKind::CopyWeak:
744     case ARCInstKind::MoveWeak: {
745       CallInst *CI = cast<CallInst>(Inst);
746       if (IsNullOrUndef(CI->getArgOperand(0)) ||
747           IsNullOrUndef(CI->getArgOperand(1))) {
748         Changed = true;
749         Type *Ty = CI->getArgOperand(0)->getType();
750         new StoreInst(UndefValue::get(cast<PointerType>(Ty)->getElementType()),
751                       Constant::getNullValue(Ty),
752                       CI);
753 
754         llvm::Value *NewValue = UndefValue::get(CI->getType());
755         DEBUG(dbgs() << "A null pointer-to-weak-pointer is undefined behavior."
756                         "\nOld = " << *CI << "\nNew = " << *NewValue << "\n");
757 
758         CI->replaceAllUsesWith(NewValue);
759         CI->eraseFromParent();
760         continue;
761       }
762       break;
763     }
764     case ARCInstKind::RetainRV:
765       if (OptimizeRetainRVCall(F, Inst))
766         continue;
767       break;
768     case ARCInstKind::AutoreleaseRV:
769       OptimizeAutoreleaseRVCall(F, Inst, Class);
770       break;
771     }
772 
773     // objc_autorelease(x) -> objc_release(x) if x is otherwise unused.
774     if (IsAutorelease(Class) && Inst->use_empty()) {
775       CallInst *Call = cast<CallInst>(Inst);
776       const Value *Arg = Call->getArgOperand(0);
777       Arg = FindSingleUseIdentifiedObject(Arg);
778       if (Arg) {
779         Changed = true;
780         ++NumAutoreleases;
781 
782         // Create the declaration lazily.
783         LLVMContext &C = Inst->getContext();
784 
785         Constant *Decl = EP.get(ARCRuntimeEntryPointKind::Release);
786         CallInst *NewCall = CallInst::Create(Decl, Call->getArgOperand(0), "",
787                                              Call);
788         NewCall->setMetadata(MDKindCache.get(ARCMDKindID::ImpreciseRelease),
789                              MDNode::get(C, None));
790 
791         DEBUG(dbgs() << "Replacing autorelease{,RV}(x) with objc_release(x) "
792               "since x is otherwise unused.\nOld: " << *Call << "\nNew: "
793               << *NewCall << "\n");
794 
795         EraseInstruction(Call);
796         Inst = NewCall;
797         Class = ARCInstKind::Release;
798       }
799     }
800 
801     // For functions which can never be passed stack arguments, add
802     // a tail keyword.
803     if (IsAlwaysTail(Class)) {
804       Changed = true;
805       DEBUG(dbgs() << "Adding tail keyword to function since it can never be "
806                       "passed stack args: " << *Inst << "\n");
807       cast<CallInst>(Inst)->setTailCall();
808     }
809 
810     // Ensure that functions that can never have a "tail" keyword due to the
811     // semantics of ARC truly do not do so.
812     if (IsNeverTail(Class)) {
813       Changed = true;
814       DEBUG(dbgs() << "Removing tail keyword from function: " << *Inst <<
815             "\n");
816       cast<CallInst>(Inst)->setTailCall(false);
817     }
818 
819     // Set nounwind as needed.
820     if (IsNoThrow(Class)) {
821       Changed = true;
822       DEBUG(dbgs() << "Found no throw class. Setting nounwind on: " << *Inst
823                    << "\n");
824       cast<CallInst>(Inst)->setDoesNotThrow();
825     }
826 
827     if (!IsNoopOnNull(Class)) {
828       UsedInThisFunction |= 1 << unsigned(Class);
829       continue;
830     }
831 
832     const Value *Arg = GetArgRCIdentityRoot(Inst);
833 
834     // ARC calls with null are no-ops. Delete them.
835     if (IsNullOrUndef(Arg)) {
836       Changed = true;
837       ++NumNoops;
838       DEBUG(dbgs() << "ARC calls with  null are no-ops. Erasing: " << *Inst
839             << "\n");
840       EraseInstruction(Inst);
841       continue;
842     }
843 
844     // Keep track of which of retain, release, autorelease, and retain_block
845     // are actually present in this function.
846     UsedInThisFunction |= 1 << unsigned(Class);
847 
848     // If Arg is a PHI, and one or more incoming values to the
849     // PHI are null, and the call is control-equivalent to the PHI, and there
850     // are no relevant side effects between the PHI and the call, the call
851     // could be pushed up to just those paths with non-null incoming values.
852     // For now, don't bother splitting critical edges for this.
853     SmallVector<std::pair<Instruction *, const Value *>, 4> Worklist;
854     Worklist.push_back(std::make_pair(Inst, Arg));
855     do {
856       std::pair<Instruction *, const Value *> Pair = Worklist.pop_back_val();
857       Inst = Pair.first;
858       Arg = Pair.second;
859 
860       const PHINode *PN = dyn_cast<PHINode>(Arg);
861       if (!PN) continue;
862 
863       // Determine if the PHI has any null operands, or any incoming
864       // critical edges.
865       bool HasNull = false;
866       bool HasCriticalEdges = false;
867       for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
868         Value *Incoming =
869           GetRCIdentityRoot(PN->getIncomingValue(i));
870         if (IsNullOrUndef(Incoming))
871           HasNull = true;
872         else if (cast<TerminatorInst>(PN->getIncomingBlock(i)->back())
873                    .getNumSuccessors() != 1) {
874           HasCriticalEdges = true;
875           break;
876         }
877       }
878       // If we have null operands and no critical edges, optimize.
879       if (!HasCriticalEdges && HasNull) {
880         SmallPtrSet<Instruction *, 4> DependingInstructions;
881         SmallPtrSet<const BasicBlock *, 4> Visited;
882 
883         // Check that there is nothing that cares about the reference
884         // count between the call and the phi.
885         switch (Class) {
886         case ARCInstKind::Retain:
887         case ARCInstKind::RetainBlock:
888           // These can always be moved up.
889           break;
890         case ARCInstKind::Release:
891           // These can't be moved across things that care about the retain
892           // count.
893           FindDependencies(NeedsPositiveRetainCount, Arg,
894                            Inst->getParent(), Inst,
895                            DependingInstructions, Visited, PA);
896           break;
897         case ARCInstKind::Autorelease:
898           // These can't be moved across autorelease pool scope boundaries.
899           FindDependencies(AutoreleasePoolBoundary, Arg,
900                            Inst->getParent(), Inst,
901                            DependingInstructions, Visited, PA);
902           break;
903         case ARCInstKind::ClaimRV:
904         case ARCInstKind::RetainRV:
905         case ARCInstKind::AutoreleaseRV:
906           // Don't move these; the RV optimization depends on the autoreleaseRV
907           // being tail called, and the retainRV being immediately after a call
908           // (which might still happen if we get lucky with codegen layout, but
909           // it's not worth taking the chance).
910           continue;
911         default:
912           llvm_unreachable("Invalid dependence flavor");
913         }
914 
915         if (DependingInstructions.size() == 1 &&
916             *DependingInstructions.begin() == PN) {
917           Changed = true;
918           ++NumPartialNoops;
919           // Clone the call into each predecessor that has a non-null value.
920           CallInst *CInst = cast<CallInst>(Inst);
921           Type *ParamTy = CInst->getArgOperand(0)->getType();
922           for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
923             Value *Incoming =
924               GetRCIdentityRoot(PN->getIncomingValue(i));
925             if (!IsNullOrUndef(Incoming)) {
926               CallInst *Clone = cast<CallInst>(CInst->clone());
927               Value *Op = PN->getIncomingValue(i);
928               Instruction *InsertPos = &PN->getIncomingBlock(i)->back();
929               if (Op->getType() != ParamTy)
930                 Op = new BitCastInst(Op, ParamTy, "", InsertPos);
931               Clone->setArgOperand(0, Op);
932               Clone->insertBefore(InsertPos);
933 
934               DEBUG(dbgs() << "Cloning "
935                            << *CInst << "\n"
936                            "And inserting clone at " << *InsertPos << "\n");
937               Worklist.push_back(std::make_pair(Clone, Incoming));
938             }
939           }
940           // Erase the original call.
941           DEBUG(dbgs() << "Erasing: " << *CInst << "\n");
942           EraseInstruction(CInst);
943           continue;
944         }
945       }
946     } while (!Worklist.empty());
947   }
948 }
949 
950 /// If we have a top down pointer in the S_Use state, make sure that there are
951 /// no CFG hazards by checking the states of various bottom up pointers.
952 static void CheckForUseCFGHazard(const Sequence SuccSSeq,
953                                  const bool SuccSRRIKnownSafe,
954                                  TopDownPtrState &S,
955                                  bool &SomeSuccHasSame,
956                                  bool &AllSuccsHaveSame,
957                                  bool &NotAllSeqEqualButKnownSafe,
958                                  bool &ShouldContinue) {
959   switch (SuccSSeq) {
960   case S_CanRelease: {
961     if (!S.IsKnownSafe() && !SuccSRRIKnownSafe) {
962       S.ClearSequenceProgress();
963       break;
964     }
965     S.SetCFGHazardAfflicted(true);
966     ShouldContinue = true;
967     break;
968   }
969   case S_Use:
970     SomeSuccHasSame = true;
971     break;
972   case S_Stop:
973   case S_Release:
974   case S_MovableRelease:
975     if (!S.IsKnownSafe() && !SuccSRRIKnownSafe)
976       AllSuccsHaveSame = false;
977     else
978       NotAllSeqEqualButKnownSafe = true;
979     break;
980   case S_Retain:
981     llvm_unreachable("bottom-up pointer in retain state!");
982   case S_None:
983     llvm_unreachable("This should have been handled earlier.");
984   }
985 }
986 
987 /// If we have a Top Down pointer in the S_CanRelease state, make sure that
988 /// there are no CFG hazards by checking the states of various bottom up
989 /// pointers.
990 static void CheckForCanReleaseCFGHazard(const Sequence SuccSSeq,
991                                         const bool SuccSRRIKnownSafe,
992                                         TopDownPtrState &S,
993                                         bool &SomeSuccHasSame,
994                                         bool &AllSuccsHaveSame,
995                                         bool &NotAllSeqEqualButKnownSafe) {
996   switch (SuccSSeq) {
997   case S_CanRelease:
998     SomeSuccHasSame = true;
999     break;
1000   case S_Stop:
1001   case S_Release:
1002   case S_MovableRelease:
1003   case S_Use:
1004     if (!S.IsKnownSafe() && !SuccSRRIKnownSafe)
1005       AllSuccsHaveSame = false;
1006     else
1007       NotAllSeqEqualButKnownSafe = true;
1008     break;
1009   case S_Retain:
1010     llvm_unreachable("bottom-up pointer in retain state!");
1011   case S_None:
1012     llvm_unreachable("This should have been handled earlier.");
1013   }
1014 }
1015 
1016 /// Check for critical edges, loop boundaries, irreducible control flow, or
1017 /// other CFG structures where moving code across the edge would result in it
1018 /// being executed more.
1019 void
1020 ObjCARCOpt::CheckForCFGHazards(const BasicBlock *BB,
1021                                DenseMap<const BasicBlock *, BBState> &BBStates,
1022                                BBState &MyStates) const {
1023   // If any top-down local-use or possible-dec has a succ which is earlier in
1024   // the sequence, forget it.
1025   for (auto I = MyStates.top_down_ptr_begin(), E = MyStates.top_down_ptr_end();
1026        I != E; ++I) {
1027     TopDownPtrState &S = I->second;
1028     const Sequence Seq = I->second.GetSeq();
1029 
1030     // We only care about S_Retain, S_CanRelease, and S_Use.
1031     if (Seq == S_None)
1032       continue;
1033 
1034     // Make sure that if extra top down states are added in the future that this
1035     // code is updated to handle it.
1036     assert((Seq == S_Retain || Seq == S_CanRelease || Seq == S_Use) &&
1037            "Unknown top down sequence state.");
1038 
1039     const Value *Arg = I->first;
1040     const TerminatorInst *TI = cast<TerminatorInst>(&BB->back());
1041     bool SomeSuccHasSame = false;
1042     bool AllSuccsHaveSame = true;
1043     bool NotAllSeqEqualButKnownSafe = false;
1044 
1045     succ_const_iterator SI(TI), SE(TI, false);
1046 
1047     for (; SI != SE; ++SI) {
1048       // If VisitBottomUp has pointer information for this successor, take
1049       // what we know about it.
1050       const DenseMap<const BasicBlock *, BBState>::iterator BBI =
1051         BBStates.find(*SI);
1052       assert(BBI != BBStates.end());
1053       const BottomUpPtrState &SuccS = BBI->second.getPtrBottomUpState(Arg);
1054       const Sequence SuccSSeq = SuccS.GetSeq();
1055 
1056       // If bottom up, the pointer is in an S_None state, clear the sequence
1057       // progress since the sequence in the bottom up state finished
1058       // suggesting a mismatch in between retains/releases. This is true for
1059       // all three cases that we are handling here: S_Retain, S_Use, and
1060       // S_CanRelease.
1061       if (SuccSSeq == S_None) {
1062         S.ClearSequenceProgress();
1063         continue;
1064       }
1065 
1066       // If we have S_Use or S_CanRelease, perform our check for cfg hazard
1067       // checks.
1068       const bool SuccSRRIKnownSafe = SuccS.IsKnownSafe();
1069 
1070       // *NOTE* We do not use Seq from above here since we are allowing for
1071       // S.GetSeq() to change while we are visiting basic blocks.
1072       switch(S.GetSeq()) {
1073       case S_Use: {
1074         bool ShouldContinue = false;
1075         CheckForUseCFGHazard(SuccSSeq, SuccSRRIKnownSafe, S, SomeSuccHasSame,
1076                              AllSuccsHaveSame, NotAllSeqEqualButKnownSafe,
1077                              ShouldContinue);
1078         if (ShouldContinue)
1079           continue;
1080         break;
1081       }
1082       case S_CanRelease: {
1083         CheckForCanReleaseCFGHazard(SuccSSeq, SuccSRRIKnownSafe, S,
1084                                     SomeSuccHasSame, AllSuccsHaveSame,
1085                                     NotAllSeqEqualButKnownSafe);
1086         break;
1087       }
1088       case S_Retain:
1089       case S_None:
1090       case S_Stop:
1091       case S_Release:
1092       case S_MovableRelease:
1093         break;
1094       }
1095     }
1096 
1097     // If the state at the other end of any of the successor edges
1098     // matches the current state, require all edges to match. This
1099     // guards against loops in the middle of a sequence.
1100     if (SomeSuccHasSame && !AllSuccsHaveSame) {
1101       S.ClearSequenceProgress();
1102     } else if (NotAllSeqEqualButKnownSafe) {
1103       // If we would have cleared the state foregoing the fact that we are known
1104       // safe, stop code motion. This is because whether or not it is safe to
1105       // remove RR pairs via KnownSafe is an orthogonal concept to whether we
1106       // are allowed to perform code motion.
1107       S.SetCFGHazardAfflicted(true);
1108     }
1109   }
1110 }
1111 
1112 bool ObjCARCOpt::VisitInstructionBottomUp(
1113     Instruction *Inst, BasicBlock *BB, BlotMapVector<Value *, RRInfo> &Retains,
1114     BBState &MyStates) {
1115   bool NestingDetected = false;
1116   ARCInstKind Class = GetARCInstKind(Inst);
1117   const Value *Arg = nullptr;
1118 
1119   DEBUG(dbgs() << "        Class: " << Class << "\n");
1120 
1121   switch (Class) {
1122   case ARCInstKind::Release: {
1123     Arg = GetArgRCIdentityRoot(Inst);
1124 
1125     BottomUpPtrState &S = MyStates.getPtrBottomUpState(Arg);
1126     NestingDetected |= S.InitBottomUp(MDKindCache, Inst);
1127     break;
1128   }
1129   case ARCInstKind::RetainBlock:
1130     // In OptimizeIndividualCalls, we have strength reduced all optimizable
1131     // objc_retainBlocks to objc_retains. Thus at this point any
1132     // objc_retainBlocks that we see are not optimizable.
1133     break;
1134   case ARCInstKind::Retain:
1135   case ARCInstKind::RetainRV: {
1136     Arg = GetArgRCIdentityRoot(Inst);
1137     BottomUpPtrState &S = MyStates.getPtrBottomUpState(Arg);
1138     if (S.MatchWithRetain()) {
1139       // Don't do retain+release tracking for ARCInstKind::RetainRV, because
1140       // it's better to let it remain as the first instruction after a call.
1141       if (Class != ARCInstKind::RetainRV) {
1142         DEBUG(llvm::dbgs() << "        Matching with: " << *Inst << "\n");
1143         Retains[Inst] = S.GetRRInfo();
1144       }
1145       S.ClearSequenceProgress();
1146     }
1147     // A retain moving bottom up can be a use.
1148     break;
1149   }
1150   case ARCInstKind::AutoreleasepoolPop:
1151     // Conservatively, clear MyStates for all known pointers.
1152     MyStates.clearBottomUpPointers();
1153     return NestingDetected;
1154   case ARCInstKind::AutoreleasepoolPush:
1155   case ARCInstKind::None:
1156     // These are irrelevant.
1157     return NestingDetected;
1158   case ARCInstKind::User:
1159     // If we have a store into an alloca of a pointer we are tracking, the
1160     // pointer has multiple owners implying that we must be more conservative.
1161     //
1162     // This comes up in the context of a pointer being ``KnownSafe''. In the
1163     // presence of a block being initialized, the frontend will emit the
1164     // objc_retain on the original pointer and the release on the pointer loaded
1165     // from the alloca. The optimizer will through the provenance analysis
1166     // realize that the two are related, but since we only require KnownSafe in
1167     // one direction, will match the inner retain on the original pointer with
1168     // the guard release on the original pointer. This is fixed by ensuring that
1169     // in the presence of allocas we only unconditionally remove pointers if
1170     // both our retain and our release are KnownSafe.
1171     if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
1172       const DataLayout &DL = BB->getModule()->getDataLayout();
1173       if (AreAnyUnderlyingObjectsAnAlloca(SI->getPointerOperand(), DL)) {
1174         auto I = MyStates.findPtrBottomUpState(
1175             GetRCIdentityRoot(SI->getValueOperand()));
1176         if (I != MyStates.bottom_up_ptr_end())
1177           MultiOwnersSet.insert(I->first);
1178       }
1179     }
1180     break;
1181   default:
1182     break;
1183   }
1184 
1185   // Consider any other possible effects of this instruction on each
1186   // pointer being tracked.
1187   for (auto MI = MyStates.bottom_up_ptr_begin(),
1188             ME = MyStates.bottom_up_ptr_end();
1189        MI != ME; ++MI) {
1190     const Value *Ptr = MI->first;
1191     if (Ptr == Arg)
1192       continue; // Handled above.
1193     BottomUpPtrState &S = MI->second;
1194 
1195     if (S.HandlePotentialAlterRefCount(Inst, Ptr, PA, Class))
1196       continue;
1197 
1198     S.HandlePotentialUse(BB, Inst, Ptr, PA, Class);
1199   }
1200 
1201   return NestingDetected;
1202 }
1203 
1204 bool ObjCARCOpt::VisitBottomUp(BasicBlock *BB,
1205                                DenseMap<const BasicBlock *, BBState> &BBStates,
1206                                BlotMapVector<Value *, RRInfo> &Retains) {
1207 
1208   DEBUG(dbgs() << "\n== ObjCARCOpt::VisitBottomUp ==\n");
1209 
1210   bool NestingDetected = false;
1211   BBState &MyStates = BBStates[BB];
1212 
1213   // Merge the states from each successor to compute the initial state
1214   // for the current block.
1215   BBState::edge_iterator SI(MyStates.succ_begin()),
1216                          SE(MyStates.succ_end());
1217   if (SI != SE) {
1218     const BasicBlock *Succ = *SI;
1219     DenseMap<const BasicBlock *, BBState>::iterator I = BBStates.find(Succ);
1220     assert(I != BBStates.end());
1221     MyStates.InitFromSucc(I->second);
1222     ++SI;
1223     for (; SI != SE; ++SI) {
1224       Succ = *SI;
1225       I = BBStates.find(Succ);
1226       assert(I != BBStates.end());
1227       MyStates.MergeSucc(I->second);
1228     }
1229   }
1230 
1231   DEBUG(llvm::dbgs() << "Before:\n" << BBStates[BB] << "\n"
1232                      << "Performing Dataflow:\n");
1233 
1234   // Visit all the instructions, bottom-up.
1235   for (BasicBlock::iterator I = BB->end(), E = BB->begin(); I != E; --I) {
1236     Instruction *Inst = &*std::prev(I);
1237 
1238     // Invoke instructions are visited as part of their successors (below).
1239     if (isa<InvokeInst>(Inst))
1240       continue;
1241 
1242     DEBUG(dbgs() << "    Visiting " << *Inst << "\n");
1243 
1244     NestingDetected |= VisitInstructionBottomUp(Inst, BB, Retains, MyStates);
1245   }
1246 
1247   // If there's a predecessor with an invoke, visit the invoke as if it were
1248   // part of this block, since we can't insert code after an invoke in its own
1249   // block, and we don't want to split critical edges.
1250   for (BBState::edge_iterator PI(MyStates.pred_begin()),
1251        PE(MyStates.pred_end()); PI != PE; ++PI) {
1252     BasicBlock *Pred = *PI;
1253     if (InvokeInst *II = dyn_cast<InvokeInst>(&Pred->back()))
1254       NestingDetected |= VisitInstructionBottomUp(II, BB, Retains, MyStates);
1255   }
1256 
1257   DEBUG(llvm::dbgs() << "\nFinal State:\n" << BBStates[BB] << "\n");
1258 
1259   return NestingDetected;
1260 }
1261 
1262 bool
1263 ObjCARCOpt::VisitInstructionTopDown(Instruction *Inst,
1264                                     DenseMap<Value *, RRInfo> &Releases,
1265                                     BBState &MyStates) {
1266   bool NestingDetected = false;
1267   ARCInstKind Class = GetARCInstKind(Inst);
1268   const Value *Arg = nullptr;
1269 
1270   DEBUG(llvm::dbgs() << "        Class: " << Class << "\n");
1271 
1272   switch (Class) {
1273   case ARCInstKind::RetainBlock:
1274     // In OptimizeIndividualCalls, we have strength reduced all optimizable
1275     // objc_retainBlocks to objc_retains. Thus at this point any
1276     // objc_retainBlocks that we see are not optimizable. We need to break since
1277     // a retain can be a potential use.
1278     break;
1279   case ARCInstKind::Retain:
1280   case ARCInstKind::RetainRV: {
1281     Arg = GetArgRCIdentityRoot(Inst);
1282     TopDownPtrState &S = MyStates.getPtrTopDownState(Arg);
1283     NestingDetected |= S.InitTopDown(Class, Inst);
1284     // A retain can be a potential use; proceed to the generic checking
1285     // code below.
1286     break;
1287   }
1288   case ARCInstKind::Release: {
1289     Arg = GetArgRCIdentityRoot(Inst);
1290     TopDownPtrState &S = MyStates.getPtrTopDownState(Arg);
1291     // Try to form a tentative pair in between this release instruction and the
1292     // top down pointers that we are tracking.
1293     if (S.MatchWithRelease(MDKindCache, Inst)) {
1294       // If we succeed, copy S's RRInfo into the Release -> {Retain Set
1295       // Map}. Then we clear S.
1296       DEBUG(llvm::dbgs() << "        Matching with: " << *Inst << "\n");
1297       Releases[Inst] = S.GetRRInfo();
1298       S.ClearSequenceProgress();
1299     }
1300     break;
1301   }
1302   case ARCInstKind::AutoreleasepoolPop:
1303     // Conservatively, clear MyStates for all known pointers.
1304     MyStates.clearTopDownPointers();
1305     return false;
1306   case ARCInstKind::AutoreleasepoolPush:
1307   case ARCInstKind::None:
1308     // These can not be uses of
1309     return false;
1310   default:
1311     break;
1312   }
1313 
1314   // Consider any other possible effects of this instruction on each
1315   // pointer being tracked.
1316   for (auto MI = MyStates.top_down_ptr_begin(),
1317             ME = MyStates.top_down_ptr_end();
1318        MI != ME; ++MI) {
1319     const Value *Ptr = MI->first;
1320     if (Ptr == Arg)
1321       continue; // Handled above.
1322     TopDownPtrState &S = MI->second;
1323     if (S.HandlePotentialAlterRefCount(Inst, Ptr, PA, Class))
1324       continue;
1325 
1326     S.HandlePotentialUse(Inst, Ptr, PA, Class);
1327   }
1328 
1329   return NestingDetected;
1330 }
1331 
1332 bool
1333 ObjCARCOpt::VisitTopDown(BasicBlock *BB,
1334                          DenseMap<const BasicBlock *, BBState> &BBStates,
1335                          DenseMap<Value *, RRInfo> &Releases) {
1336   DEBUG(dbgs() << "\n== ObjCARCOpt::VisitTopDown ==\n");
1337   bool NestingDetected = false;
1338   BBState &MyStates = BBStates[BB];
1339 
1340   // Merge the states from each predecessor to compute the initial state
1341   // for the current block.
1342   BBState::edge_iterator PI(MyStates.pred_begin()),
1343                          PE(MyStates.pred_end());
1344   if (PI != PE) {
1345     const BasicBlock *Pred = *PI;
1346     DenseMap<const BasicBlock *, BBState>::iterator I = BBStates.find(Pred);
1347     assert(I != BBStates.end());
1348     MyStates.InitFromPred(I->second);
1349     ++PI;
1350     for (; PI != PE; ++PI) {
1351       Pred = *PI;
1352       I = BBStates.find(Pred);
1353       assert(I != BBStates.end());
1354       MyStates.MergePred(I->second);
1355     }
1356   }
1357 
1358   DEBUG(llvm::dbgs() << "Before:\n" << BBStates[BB]  << "\n"
1359                      << "Performing Dataflow:\n");
1360 
1361   // Visit all the instructions, top-down.
1362   for (Instruction &Inst : *BB) {
1363     DEBUG(dbgs() << "    Visiting " << Inst << "\n");
1364 
1365     NestingDetected |= VisitInstructionTopDown(&Inst, Releases, MyStates);
1366   }
1367 
1368   DEBUG(llvm::dbgs() << "\nState Before Checking for CFG Hazards:\n"
1369                      << BBStates[BB] << "\n\n");
1370   CheckForCFGHazards(BB, BBStates, MyStates);
1371   DEBUG(llvm::dbgs() << "Final State:\n" << BBStates[BB] << "\n");
1372   return NestingDetected;
1373 }
1374 
1375 static void
1376 ComputePostOrders(Function &F,
1377                   SmallVectorImpl<BasicBlock *> &PostOrder,
1378                   SmallVectorImpl<BasicBlock *> &ReverseCFGPostOrder,
1379                   unsigned NoObjCARCExceptionsMDKind,
1380                   DenseMap<const BasicBlock *, BBState> &BBStates) {
1381   /// The visited set, for doing DFS walks.
1382   SmallPtrSet<BasicBlock *, 16> Visited;
1383 
1384   // Do DFS, computing the PostOrder.
1385   SmallPtrSet<BasicBlock *, 16> OnStack;
1386   SmallVector<std::pair<BasicBlock *, succ_iterator>, 16> SuccStack;
1387 
1388   // Functions always have exactly one entry block, and we don't have
1389   // any other block that we treat like an entry block.
1390   BasicBlock *EntryBB = &F.getEntryBlock();
1391   BBState &MyStates = BBStates[EntryBB];
1392   MyStates.SetAsEntry();
1393   TerminatorInst *EntryTI = cast<TerminatorInst>(&EntryBB->back());
1394   SuccStack.push_back(std::make_pair(EntryBB, succ_iterator(EntryTI)));
1395   Visited.insert(EntryBB);
1396   OnStack.insert(EntryBB);
1397   do {
1398   dfs_next_succ:
1399     BasicBlock *CurrBB = SuccStack.back().first;
1400     TerminatorInst *TI = cast<TerminatorInst>(&CurrBB->back());
1401     succ_iterator SE(TI, false);
1402 
1403     while (SuccStack.back().second != SE) {
1404       BasicBlock *SuccBB = *SuccStack.back().second++;
1405       if (Visited.insert(SuccBB).second) {
1406         TerminatorInst *TI = cast<TerminatorInst>(&SuccBB->back());
1407         SuccStack.push_back(std::make_pair(SuccBB, succ_iterator(TI)));
1408         BBStates[CurrBB].addSucc(SuccBB);
1409         BBState &SuccStates = BBStates[SuccBB];
1410         SuccStates.addPred(CurrBB);
1411         OnStack.insert(SuccBB);
1412         goto dfs_next_succ;
1413       }
1414 
1415       if (!OnStack.count(SuccBB)) {
1416         BBStates[CurrBB].addSucc(SuccBB);
1417         BBStates[SuccBB].addPred(CurrBB);
1418       }
1419     }
1420     OnStack.erase(CurrBB);
1421     PostOrder.push_back(CurrBB);
1422     SuccStack.pop_back();
1423   } while (!SuccStack.empty());
1424 
1425   Visited.clear();
1426 
1427   // Do reverse-CFG DFS, computing the reverse-CFG PostOrder.
1428   // Functions may have many exits, and there also blocks which we treat
1429   // as exits due to ignored edges.
1430   SmallVector<std::pair<BasicBlock *, BBState::edge_iterator>, 16> PredStack;
1431   for (BasicBlock &ExitBB : F) {
1432     BBState &MyStates = BBStates[&ExitBB];
1433     if (!MyStates.isExit())
1434       continue;
1435 
1436     MyStates.SetAsExit();
1437 
1438     PredStack.push_back(std::make_pair(&ExitBB, MyStates.pred_begin()));
1439     Visited.insert(&ExitBB);
1440     while (!PredStack.empty()) {
1441     reverse_dfs_next_succ:
1442       BBState::edge_iterator PE = BBStates[PredStack.back().first].pred_end();
1443       while (PredStack.back().second != PE) {
1444         BasicBlock *BB = *PredStack.back().second++;
1445         if (Visited.insert(BB).second) {
1446           PredStack.push_back(std::make_pair(BB, BBStates[BB].pred_begin()));
1447           goto reverse_dfs_next_succ;
1448         }
1449       }
1450       ReverseCFGPostOrder.push_back(PredStack.pop_back_val().first);
1451     }
1452   }
1453 }
1454 
1455 // Visit the function both top-down and bottom-up.
1456 bool ObjCARCOpt::Visit(Function &F,
1457                        DenseMap<const BasicBlock *, BBState> &BBStates,
1458                        BlotMapVector<Value *, RRInfo> &Retains,
1459                        DenseMap<Value *, RRInfo> &Releases) {
1460 
1461   // Use reverse-postorder traversals, because we magically know that loops
1462   // will be well behaved, i.e. they won't repeatedly call retain on a single
1463   // pointer without doing a release. We can't use the ReversePostOrderTraversal
1464   // class here because we want the reverse-CFG postorder to consider each
1465   // function exit point, and we want to ignore selected cycle edges.
1466   SmallVector<BasicBlock *, 16> PostOrder;
1467   SmallVector<BasicBlock *, 16> ReverseCFGPostOrder;
1468   ComputePostOrders(F, PostOrder, ReverseCFGPostOrder,
1469                     MDKindCache.get(ARCMDKindID::NoObjCARCExceptions),
1470                     BBStates);
1471 
1472   // Use reverse-postorder on the reverse CFG for bottom-up.
1473   bool BottomUpNestingDetected = false;
1474   for (BasicBlock *BB : reverse(ReverseCFGPostOrder))
1475     BottomUpNestingDetected |= VisitBottomUp(BB, BBStates, Retains);
1476 
1477   // Use reverse-postorder for top-down.
1478   bool TopDownNestingDetected = false;
1479   for (BasicBlock *BB : reverse(PostOrder))
1480     TopDownNestingDetected |= VisitTopDown(BB, BBStates, Releases);
1481 
1482   return TopDownNestingDetected && BottomUpNestingDetected;
1483 }
1484 
1485 /// Move the calls in RetainsToMove and ReleasesToMove.
1486 void ObjCARCOpt::MoveCalls(Value *Arg, RRInfo &RetainsToMove,
1487                            RRInfo &ReleasesToMove,
1488                            BlotMapVector<Value *, RRInfo> &Retains,
1489                            DenseMap<Value *, RRInfo> &Releases,
1490                            SmallVectorImpl<Instruction *> &DeadInsts,
1491                            Module *M) {
1492   Type *ArgTy = Arg->getType();
1493   Type *ParamTy = PointerType::getUnqual(Type::getInt8Ty(ArgTy->getContext()));
1494 
1495   DEBUG(dbgs() << "== ObjCARCOpt::MoveCalls ==\n");
1496 
1497   // Insert the new retain and release calls.
1498   for (Instruction *InsertPt : ReleasesToMove.ReverseInsertPts) {
1499     Value *MyArg = ArgTy == ParamTy ? Arg :
1500                    new BitCastInst(Arg, ParamTy, "", InsertPt);
1501     Constant *Decl = EP.get(ARCRuntimeEntryPointKind::Retain);
1502     CallInst *Call = CallInst::Create(Decl, MyArg, "", InsertPt);
1503     Call->setDoesNotThrow();
1504     Call->setTailCall();
1505 
1506     DEBUG(dbgs() << "Inserting new Retain: " << *Call << "\n"
1507                     "At insertion point: " << *InsertPt << "\n");
1508   }
1509   for (Instruction *InsertPt : RetainsToMove.ReverseInsertPts) {
1510     Value *MyArg = ArgTy == ParamTy ? Arg :
1511                    new BitCastInst(Arg, ParamTy, "", InsertPt);
1512     Constant *Decl = EP.get(ARCRuntimeEntryPointKind::Release);
1513     CallInst *Call = CallInst::Create(Decl, MyArg, "", InsertPt);
1514     // Attach a clang.imprecise_release metadata tag, if appropriate.
1515     if (MDNode *M = ReleasesToMove.ReleaseMetadata)
1516       Call->setMetadata(MDKindCache.get(ARCMDKindID::ImpreciseRelease), M);
1517     Call->setDoesNotThrow();
1518     if (ReleasesToMove.IsTailCallRelease)
1519       Call->setTailCall();
1520 
1521     DEBUG(dbgs() << "Inserting new Release: " << *Call << "\n"
1522                     "At insertion point: " << *InsertPt << "\n");
1523   }
1524 
1525   // Delete the original retain and release calls.
1526   for (Instruction *OrigRetain : RetainsToMove.Calls) {
1527     Retains.blot(OrigRetain);
1528     DeadInsts.push_back(OrigRetain);
1529     DEBUG(dbgs() << "Deleting retain: " << *OrigRetain << "\n");
1530   }
1531   for (Instruction *OrigRelease : ReleasesToMove.Calls) {
1532     Releases.erase(OrigRelease);
1533     DeadInsts.push_back(OrigRelease);
1534     DEBUG(dbgs() << "Deleting release: " << *OrigRelease << "\n");
1535   }
1536 
1537 }
1538 
1539 bool ObjCARCOpt::PairUpRetainsAndReleases(
1540     DenseMap<const BasicBlock *, BBState> &BBStates,
1541     BlotMapVector<Value *, RRInfo> &Retains,
1542     DenseMap<Value *, RRInfo> &Releases, Module *M,
1543     SmallVectorImpl<Instruction *> &NewRetains,
1544     SmallVectorImpl<Instruction *> &NewReleases,
1545     SmallVectorImpl<Instruction *> &DeadInsts, RRInfo &RetainsToMove,
1546     RRInfo &ReleasesToMove, Value *Arg, bool KnownSafe,
1547     bool &AnyPairsCompletelyEliminated) {
1548   // If a pair happens in a region where it is known that the reference count
1549   // is already incremented, we can similarly ignore possible decrements unless
1550   // we are dealing with a retainable object with multiple provenance sources.
1551   bool KnownSafeTD = true, KnownSafeBU = true;
1552   bool MultipleOwners = false;
1553   bool CFGHazardAfflicted = false;
1554 
1555   // Connect the dots between the top-down-collected RetainsToMove and
1556   // bottom-up-collected ReleasesToMove to form sets of related calls.
1557   // This is an iterative process so that we connect multiple releases
1558   // to multiple retains if needed.
1559   unsigned OldDelta = 0;
1560   unsigned NewDelta = 0;
1561   unsigned OldCount = 0;
1562   unsigned NewCount = 0;
1563   bool FirstRelease = true;
1564   for (;;) {
1565     for (Instruction *NewRetain : NewRetains) {
1566       auto It = Retains.find(NewRetain);
1567       assert(It != Retains.end());
1568       const RRInfo &NewRetainRRI = It->second;
1569       KnownSafeTD &= NewRetainRRI.KnownSafe;
1570       MultipleOwners =
1571         MultipleOwners || MultiOwnersSet.count(GetArgRCIdentityRoot(NewRetain));
1572       for (Instruction *NewRetainRelease : NewRetainRRI.Calls) {
1573         auto Jt = Releases.find(NewRetainRelease);
1574         if (Jt == Releases.end())
1575           return false;
1576         const RRInfo &NewRetainReleaseRRI = Jt->second;
1577 
1578         // If the release does not have a reference to the retain as well,
1579         // something happened which is unaccounted for. Do not do anything.
1580         //
1581         // This can happen if we catch an additive overflow during path count
1582         // merging.
1583         if (!NewRetainReleaseRRI.Calls.count(NewRetain))
1584           return false;
1585 
1586         if (ReleasesToMove.Calls.insert(NewRetainRelease).second) {
1587 
1588           // If we overflow when we compute the path count, don't remove/move
1589           // anything.
1590           const BBState &NRRBBState = BBStates[NewRetainRelease->getParent()];
1591           unsigned PathCount = BBState::OverflowOccurredValue;
1592           if (NRRBBState.GetAllPathCountWithOverflow(PathCount))
1593             return false;
1594           assert(PathCount != BBState::OverflowOccurredValue &&
1595                  "PathCount at this point can not be "
1596                  "OverflowOccurredValue.");
1597           OldDelta -= PathCount;
1598 
1599           // Merge the ReleaseMetadata and IsTailCallRelease values.
1600           if (FirstRelease) {
1601             ReleasesToMove.ReleaseMetadata =
1602               NewRetainReleaseRRI.ReleaseMetadata;
1603             ReleasesToMove.IsTailCallRelease =
1604               NewRetainReleaseRRI.IsTailCallRelease;
1605             FirstRelease = false;
1606           } else {
1607             if (ReleasesToMove.ReleaseMetadata !=
1608                 NewRetainReleaseRRI.ReleaseMetadata)
1609               ReleasesToMove.ReleaseMetadata = nullptr;
1610             if (ReleasesToMove.IsTailCallRelease !=
1611                 NewRetainReleaseRRI.IsTailCallRelease)
1612               ReleasesToMove.IsTailCallRelease = false;
1613           }
1614 
1615           // Collect the optimal insertion points.
1616           if (!KnownSafe)
1617             for (Instruction *RIP : NewRetainReleaseRRI.ReverseInsertPts) {
1618               if (ReleasesToMove.ReverseInsertPts.insert(RIP).second) {
1619                 // If we overflow when we compute the path count, don't
1620                 // remove/move anything.
1621                 const BBState &RIPBBState = BBStates[RIP->getParent()];
1622                 PathCount = BBState::OverflowOccurredValue;
1623                 if (RIPBBState.GetAllPathCountWithOverflow(PathCount))
1624                   return false;
1625                 assert(PathCount != BBState::OverflowOccurredValue &&
1626                        "PathCount at this point can not be "
1627                        "OverflowOccurredValue.");
1628                 NewDelta -= PathCount;
1629               }
1630             }
1631           NewReleases.push_back(NewRetainRelease);
1632         }
1633       }
1634     }
1635     NewRetains.clear();
1636     if (NewReleases.empty()) break;
1637 
1638     // Back the other way.
1639     for (Instruction *NewRelease : NewReleases) {
1640       auto It = Releases.find(NewRelease);
1641       assert(It != Releases.end());
1642       const RRInfo &NewReleaseRRI = It->second;
1643       KnownSafeBU &= NewReleaseRRI.KnownSafe;
1644       CFGHazardAfflicted |= NewReleaseRRI.CFGHazardAfflicted;
1645       for (Instruction *NewReleaseRetain : NewReleaseRRI.Calls) {
1646         auto Jt = Retains.find(NewReleaseRetain);
1647         if (Jt == Retains.end())
1648           return false;
1649         const RRInfo &NewReleaseRetainRRI = Jt->second;
1650 
1651         // If the retain does not have a reference to the release as well,
1652         // something happened which is unaccounted for. Do not do anything.
1653         //
1654         // This can happen if we catch an additive overflow during path count
1655         // merging.
1656         if (!NewReleaseRetainRRI.Calls.count(NewRelease))
1657           return false;
1658 
1659         if (RetainsToMove.Calls.insert(NewReleaseRetain).second) {
1660           // If we overflow when we compute the path count, don't remove/move
1661           // anything.
1662           const BBState &NRRBBState = BBStates[NewReleaseRetain->getParent()];
1663           unsigned PathCount = BBState::OverflowOccurredValue;
1664           if (NRRBBState.GetAllPathCountWithOverflow(PathCount))
1665             return false;
1666           assert(PathCount != BBState::OverflowOccurredValue &&
1667                  "PathCount at this point can not be "
1668                  "OverflowOccurredValue.");
1669           OldDelta += PathCount;
1670           OldCount += PathCount;
1671 
1672           // Collect the optimal insertion points.
1673           if (!KnownSafe)
1674             for (Instruction *RIP : NewReleaseRetainRRI.ReverseInsertPts) {
1675               if (RetainsToMove.ReverseInsertPts.insert(RIP).second) {
1676                 // If we overflow when we compute the path count, don't
1677                 // remove/move anything.
1678                 const BBState &RIPBBState = BBStates[RIP->getParent()];
1679 
1680                 PathCount = BBState::OverflowOccurredValue;
1681                 if (RIPBBState.GetAllPathCountWithOverflow(PathCount))
1682                   return false;
1683                 assert(PathCount != BBState::OverflowOccurredValue &&
1684                        "PathCount at this point can not be "
1685                        "OverflowOccurredValue.");
1686                 NewDelta += PathCount;
1687                 NewCount += PathCount;
1688               }
1689             }
1690           NewRetains.push_back(NewReleaseRetain);
1691         }
1692       }
1693     }
1694     NewReleases.clear();
1695     if (NewRetains.empty()) break;
1696   }
1697 
1698   // We can only remove pointers if we are known safe in both directions.
1699   bool UnconditionallySafe = KnownSafeTD && KnownSafeBU;
1700   if (UnconditionallySafe) {
1701     RetainsToMove.ReverseInsertPts.clear();
1702     ReleasesToMove.ReverseInsertPts.clear();
1703     NewCount = 0;
1704   } else {
1705     // Determine whether the new insertion points we computed preserve the
1706     // balance of retain and release calls through the program.
1707     // TODO: If the fully aggressive solution isn't valid, try to find a
1708     // less aggressive solution which is.
1709     if (NewDelta != 0)
1710       return false;
1711 
1712     // At this point, we are not going to remove any RR pairs, but we still are
1713     // able to move RR pairs. If one of our pointers is afflicted with
1714     // CFGHazards, we cannot perform such code motion so exit early.
1715     const bool WillPerformCodeMotion = RetainsToMove.ReverseInsertPts.size() ||
1716       ReleasesToMove.ReverseInsertPts.size();
1717     if (CFGHazardAfflicted && WillPerformCodeMotion)
1718       return false;
1719   }
1720 
1721   // Determine whether the original call points are balanced in the retain and
1722   // release calls through the program. If not, conservatively don't touch
1723   // them.
1724   // TODO: It's theoretically possible to do code motion in this case, as
1725   // long as the existing imbalances are maintained.
1726   if (OldDelta != 0)
1727     return false;
1728 
1729   Changed = true;
1730   assert(OldCount != 0 && "Unreachable code?");
1731   NumRRs += OldCount - NewCount;
1732   // Set to true if we completely removed any RR pairs.
1733   AnyPairsCompletelyEliminated = NewCount == 0;
1734 
1735   // We can move calls!
1736   return true;
1737 }
1738 
1739 /// Identify pairings between the retains and releases, and delete and/or move
1740 /// them.
1741 bool ObjCARCOpt::PerformCodePlacement(
1742     DenseMap<const BasicBlock *, BBState> &BBStates,
1743     BlotMapVector<Value *, RRInfo> &Retains,
1744     DenseMap<Value *, RRInfo> &Releases, Module *M) {
1745   DEBUG(dbgs() << "\n== ObjCARCOpt::PerformCodePlacement ==\n");
1746 
1747   bool AnyPairsCompletelyEliminated = false;
1748   RRInfo RetainsToMove;
1749   RRInfo ReleasesToMove;
1750   SmallVector<Instruction *, 4> NewRetains;
1751   SmallVector<Instruction *, 4> NewReleases;
1752   SmallVector<Instruction *, 8> DeadInsts;
1753 
1754   // Visit each retain.
1755   for (BlotMapVector<Value *, RRInfo>::const_iterator I = Retains.begin(),
1756                                                       E = Retains.end();
1757        I != E; ++I) {
1758     Value *V = I->first;
1759     if (!V) continue; // blotted
1760 
1761     Instruction *Retain = cast<Instruction>(V);
1762 
1763     DEBUG(dbgs() << "Visiting: " << *Retain << "\n");
1764 
1765     Value *Arg = GetArgRCIdentityRoot(Retain);
1766 
1767     // If the object being released is in static or stack storage, we know it's
1768     // not being managed by ObjC reference counting, so we can delete pairs
1769     // regardless of what possible decrements or uses lie between them.
1770     bool KnownSafe = isa<Constant>(Arg) || isa<AllocaInst>(Arg);
1771 
1772     // A constant pointer can't be pointing to an object on the heap. It may
1773     // be reference-counted, but it won't be deleted.
1774     if (const LoadInst *LI = dyn_cast<LoadInst>(Arg))
1775       if (const GlobalVariable *GV =
1776             dyn_cast<GlobalVariable>(
1777               GetRCIdentityRoot(LI->getPointerOperand())))
1778         if (GV->isConstant())
1779           KnownSafe = true;
1780 
1781     // Connect the dots between the top-down-collected RetainsToMove and
1782     // bottom-up-collected ReleasesToMove to form sets of related calls.
1783     NewRetains.push_back(Retain);
1784     bool PerformMoveCalls = PairUpRetainsAndReleases(
1785         BBStates, Retains, Releases, M, NewRetains, NewReleases, DeadInsts,
1786         RetainsToMove, ReleasesToMove, Arg, KnownSafe,
1787         AnyPairsCompletelyEliminated);
1788 
1789     if (PerformMoveCalls) {
1790       // Ok, everything checks out and we're all set. Let's move/delete some
1791       // code!
1792       MoveCalls(Arg, RetainsToMove, ReleasesToMove,
1793                 Retains, Releases, DeadInsts, M);
1794     }
1795 
1796     // Clean up state for next retain.
1797     NewReleases.clear();
1798     NewRetains.clear();
1799     RetainsToMove.clear();
1800     ReleasesToMove.clear();
1801   }
1802 
1803   // Now that we're done moving everything, we can delete the newly dead
1804   // instructions, as we no longer need them as insert points.
1805   while (!DeadInsts.empty())
1806     EraseInstruction(DeadInsts.pop_back_val());
1807 
1808   return AnyPairsCompletelyEliminated;
1809 }
1810 
1811 /// Weak pointer optimizations.
1812 void ObjCARCOpt::OptimizeWeakCalls(Function &F) {
1813   DEBUG(dbgs() << "\n== ObjCARCOpt::OptimizeWeakCalls ==\n");
1814 
1815   // First, do memdep-style RLE and S2L optimizations. We can't use memdep
1816   // itself because it uses AliasAnalysis and we need to do provenance
1817   // queries instead.
1818   for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
1819     Instruction *Inst = &*I++;
1820 
1821     DEBUG(dbgs() << "Visiting: " << *Inst << "\n");
1822 
1823     ARCInstKind Class = GetBasicARCInstKind(Inst);
1824     if (Class != ARCInstKind::LoadWeak &&
1825         Class != ARCInstKind::LoadWeakRetained)
1826       continue;
1827 
1828     // Delete objc_loadWeak calls with no users.
1829     if (Class == ARCInstKind::LoadWeak && Inst->use_empty()) {
1830       Inst->eraseFromParent();
1831       continue;
1832     }
1833 
1834     // TODO: For now, just look for an earlier available version of this value
1835     // within the same block. Theoretically, we could do memdep-style non-local
1836     // analysis too, but that would want caching. A better approach would be to
1837     // use the technique that EarlyCSE uses.
1838     inst_iterator Current = std::prev(I);
1839     BasicBlock *CurrentBB = &*Current.getBasicBlockIterator();
1840     for (BasicBlock::iterator B = CurrentBB->begin(),
1841                               J = Current.getInstructionIterator();
1842          J != B; --J) {
1843       Instruction *EarlierInst = &*std::prev(J);
1844       ARCInstKind EarlierClass = GetARCInstKind(EarlierInst);
1845       switch (EarlierClass) {
1846       case ARCInstKind::LoadWeak:
1847       case ARCInstKind::LoadWeakRetained: {
1848         // If this is loading from the same pointer, replace this load's value
1849         // with that one.
1850         CallInst *Call = cast<CallInst>(Inst);
1851         CallInst *EarlierCall = cast<CallInst>(EarlierInst);
1852         Value *Arg = Call->getArgOperand(0);
1853         Value *EarlierArg = EarlierCall->getArgOperand(0);
1854         switch (PA.getAA()->alias(Arg, EarlierArg)) {
1855         case MustAlias:
1856           Changed = true;
1857           // If the load has a builtin retain, insert a plain retain for it.
1858           if (Class == ARCInstKind::LoadWeakRetained) {
1859             Constant *Decl = EP.get(ARCRuntimeEntryPointKind::Retain);
1860             CallInst *CI = CallInst::Create(Decl, EarlierCall, "", Call);
1861             CI->setTailCall();
1862           }
1863           // Zap the fully redundant load.
1864           Call->replaceAllUsesWith(EarlierCall);
1865           Call->eraseFromParent();
1866           goto clobbered;
1867         case MayAlias:
1868         case PartialAlias:
1869           goto clobbered;
1870         case NoAlias:
1871           break;
1872         }
1873         break;
1874       }
1875       case ARCInstKind::StoreWeak:
1876       case ARCInstKind::InitWeak: {
1877         // If this is storing to the same pointer and has the same size etc.
1878         // replace this load's value with the stored value.
1879         CallInst *Call = cast<CallInst>(Inst);
1880         CallInst *EarlierCall = cast<CallInst>(EarlierInst);
1881         Value *Arg = Call->getArgOperand(0);
1882         Value *EarlierArg = EarlierCall->getArgOperand(0);
1883         switch (PA.getAA()->alias(Arg, EarlierArg)) {
1884         case MustAlias:
1885           Changed = true;
1886           // If the load has a builtin retain, insert a plain retain for it.
1887           if (Class == ARCInstKind::LoadWeakRetained) {
1888             Constant *Decl = EP.get(ARCRuntimeEntryPointKind::Retain);
1889             CallInst *CI = CallInst::Create(Decl, EarlierCall, "", Call);
1890             CI->setTailCall();
1891           }
1892           // Zap the fully redundant load.
1893           Call->replaceAllUsesWith(EarlierCall->getArgOperand(1));
1894           Call->eraseFromParent();
1895           goto clobbered;
1896         case MayAlias:
1897         case PartialAlias:
1898           goto clobbered;
1899         case NoAlias:
1900           break;
1901         }
1902         break;
1903       }
1904       case ARCInstKind::MoveWeak:
1905       case ARCInstKind::CopyWeak:
1906         // TOOD: Grab the copied value.
1907         goto clobbered;
1908       case ARCInstKind::AutoreleasepoolPush:
1909       case ARCInstKind::None:
1910       case ARCInstKind::IntrinsicUser:
1911       case ARCInstKind::User:
1912         // Weak pointers are only modified through the weak entry points
1913         // (and arbitrary calls, which could call the weak entry points).
1914         break;
1915       default:
1916         // Anything else could modify the weak pointer.
1917         goto clobbered;
1918       }
1919     }
1920   clobbered:;
1921   }
1922 
1923   // Then, for each destroyWeak with an alloca operand, check to see if
1924   // the alloca and all its users can be zapped.
1925   for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
1926     Instruction *Inst = &*I++;
1927     ARCInstKind Class = GetBasicARCInstKind(Inst);
1928     if (Class != ARCInstKind::DestroyWeak)
1929       continue;
1930 
1931     CallInst *Call = cast<CallInst>(Inst);
1932     Value *Arg = Call->getArgOperand(0);
1933     if (AllocaInst *Alloca = dyn_cast<AllocaInst>(Arg)) {
1934       for (User *U : Alloca->users()) {
1935         const Instruction *UserInst = cast<Instruction>(U);
1936         switch (GetBasicARCInstKind(UserInst)) {
1937         case ARCInstKind::InitWeak:
1938         case ARCInstKind::StoreWeak:
1939         case ARCInstKind::DestroyWeak:
1940           continue;
1941         default:
1942           goto done;
1943         }
1944       }
1945       Changed = true;
1946       for (auto UI = Alloca->user_begin(), UE = Alloca->user_end(); UI != UE;) {
1947         CallInst *UserInst = cast<CallInst>(*UI++);
1948         switch (GetBasicARCInstKind(UserInst)) {
1949         case ARCInstKind::InitWeak:
1950         case ARCInstKind::StoreWeak:
1951           // These functions return their second argument.
1952           UserInst->replaceAllUsesWith(UserInst->getArgOperand(1));
1953           break;
1954         case ARCInstKind::DestroyWeak:
1955           // No return value.
1956           break;
1957         default:
1958           llvm_unreachable("alloca really is used!");
1959         }
1960         UserInst->eraseFromParent();
1961       }
1962       Alloca->eraseFromParent();
1963     done:;
1964     }
1965   }
1966 }
1967 
1968 /// Identify program paths which execute sequences of retains and releases which
1969 /// can be eliminated.
1970 bool ObjCARCOpt::OptimizeSequences(Function &F) {
1971   // Releases, Retains - These are used to store the results of the main flow
1972   // analysis. These use Value* as the key instead of Instruction* so that the
1973   // map stays valid when we get around to rewriting code and calls get
1974   // replaced by arguments.
1975   DenseMap<Value *, RRInfo> Releases;
1976   BlotMapVector<Value *, RRInfo> Retains;
1977 
1978   // This is used during the traversal of the function to track the
1979   // states for each identified object at each block.
1980   DenseMap<const BasicBlock *, BBState> BBStates;
1981 
1982   // Analyze the CFG of the function, and all instructions.
1983   bool NestingDetected = Visit(F, BBStates, Retains, Releases);
1984 
1985   // Transform.
1986   bool AnyPairsCompletelyEliminated = PerformCodePlacement(BBStates, Retains,
1987                                                            Releases,
1988                                                            F.getParent());
1989 
1990   // Cleanup.
1991   MultiOwnersSet.clear();
1992 
1993   return AnyPairsCompletelyEliminated && NestingDetected;
1994 }
1995 
1996 /// Check if there is a dependent call earlier that does not have anything in
1997 /// between the Retain and the call that can affect the reference count of their
1998 /// shared pointer argument. Note that Retain need not be in BB.
1999 static bool
2000 HasSafePathToPredecessorCall(const Value *Arg, Instruction *Retain,
2001                              SmallPtrSetImpl<Instruction *> &DepInsts,
2002                              SmallPtrSetImpl<const BasicBlock *> &Visited,
2003                              ProvenanceAnalysis &PA) {
2004   FindDependencies(CanChangeRetainCount, Arg, Retain->getParent(), Retain,
2005                    DepInsts, Visited, PA);
2006   if (DepInsts.size() != 1)
2007     return false;
2008 
2009   auto *Call = dyn_cast_or_null<CallInst>(*DepInsts.begin());
2010 
2011   // Check that the pointer is the return value of the call.
2012   if (!Call || Arg != Call)
2013     return false;
2014 
2015   // Check that the call is a regular call.
2016   ARCInstKind Class = GetBasicARCInstKind(Call);
2017   return Class == ARCInstKind::CallOrUser || Class == ARCInstKind::Call;
2018 }
2019 
2020 /// Find a dependent retain that precedes the given autorelease for which there
2021 /// is nothing in between the two instructions that can affect the ref count of
2022 /// Arg.
2023 static CallInst *
2024 FindPredecessorRetainWithSafePath(const Value *Arg, BasicBlock *BB,
2025                                   Instruction *Autorelease,
2026                                   SmallPtrSetImpl<Instruction *> &DepInsts,
2027                                   SmallPtrSetImpl<const BasicBlock *> &Visited,
2028                                   ProvenanceAnalysis &PA) {
2029   FindDependencies(CanChangeRetainCount, Arg,
2030                    BB, Autorelease, DepInsts, Visited, PA);
2031   if (DepInsts.size() != 1)
2032     return nullptr;
2033 
2034   auto *Retain = dyn_cast_or_null<CallInst>(*DepInsts.begin());
2035 
2036   // Check that we found a retain with the same argument.
2037   if (!Retain || !IsRetain(GetBasicARCInstKind(Retain)) ||
2038       GetArgRCIdentityRoot(Retain) != Arg) {
2039     return nullptr;
2040   }
2041 
2042   return Retain;
2043 }
2044 
2045 /// Look for an ``autorelease'' instruction dependent on Arg such that there are
2046 /// no instructions dependent on Arg that need a positive ref count in between
2047 /// the autorelease and the ret.
2048 static CallInst *
2049 FindPredecessorAutoreleaseWithSafePath(const Value *Arg, BasicBlock *BB,
2050                                        ReturnInst *Ret,
2051                                        SmallPtrSetImpl<Instruction *> &DepInsts,
2052                                        SmallPtrSetImpl<const BasicBlock *> &V,
2053                                        ProvenanceAnalysis &PA) {
2054   FindDependencies(NeedsPositiveRetainCount, Arg,
2055                    BB, Ret, DepInsts, V, PA);
2056   if (DepInsts.size() != 1)
2057     return nullptr;
2058 
2059   auto *Autorelease = dyn_cast_or_null<CallInst>(*DepInsts.begin());
2060   if (!Autorelease)
2061     return nullptr;
2062   ARCInstKind AutoreleaseClass = GetBasicARCInstKind(Autorelease);
2063   if (!IsAutorelease(AutoreleaseClass))
2064     return nullptr;
2065   if (GetArgRCIdentityRoot(Autorelease) != Arg)
2066     return nullptr;
2067 
2068   return Autorelease;
2069 }
2070 
2071 /// Look for this pattern:
2072 /// \code
2073 ///    %call = call i8* @something(...)
2074 ///    %2 = call i8* @objc_retain(i8* %call)
2075 ///    %3 = call i8* @objc_autorelease(i8* %2)
2076 ///    ret i8* %3
2077 /// \endcode
2078 /// And delete the retain and autorelease.
2079 void ObjCARCOpt::OptimizeReturns(Function &F) {
2080   if (!F.getReturnType()->isPointerTy())
2081     return;
2082 
2083   DEBUG(dbgs() << "\n== ObjCARCOpt::OptimizeReturns ==\n");
2084 
2085   SmallPtrSet<Instruction *, 4> DependingInstructions;
2086   SmallPtrSet<const BasicBlock *, 4> Visited;
2087   for (BasicBlock &BB: F) {
2088     ReturnInst *Ret = dyn_cast<ReturnInst>(&BB.back());
2089     if (!Ret)
2090       continue;
2091 
2092     DEBUG(dbgs() << "Visiting: " << *Ret << "\n");
2093 
2094     const Value *Arg = GetRCIdentityRoot(Ret->getOperand(0));
2095 
2096     // Look for an ``autorelease'' instruction that is a predecessor of Ret and
2097     // dependent on Arg such that there are no instructions dependent on Arg
2098     // that need a positive ref count in between the autorelease and Ret.
2099     CallInst *Autorelease = FindPredecessorAutoreleaseWithSafePath(
2100         Arg, &BB, Ret, DependingInstructions, Visited, PA);
2101     DependingInstructions.clear();
2102     Visited.clear();
2103 
2104     if (!Autorelease)
2105       continue;
2106 
2107     CallInst *Retain = FindPredecessorRetainWithSafePath(
2108         Arg, &BB, Autorelease, DependingInstructions, Visited, PA);
2109     DependingInstructions.clear();
2110     Visited.clear();
2111 
2112     if (!Retain)
2113       continue;
2114 
2115     // Check that there is nothing that can affect the reference count
2116     // between the retain and the call.  Note that Retain need not be in BB.
2117     bool HasSafePathToCall = HasSafePathToPredecessorCall(Arg, Retain,
2118                                                           DependingInstructions,
2119                                                           Visited, PA);
2120     DependingInstructions.clear();
2121     Visited.clear();
2122 
2123     if (!HasSafePathToCall)
2124       continue;
2125 
2126     // If so, we can zap the retain and autorelease.
2127     Changed = true;
2128     ++NumRets;
2129     DEBUG(dbgs() << "Erasing: " << *Retain << "\nErasing: "
2130           << *Autorelease << "\n");
2131     EraseInstruction(Retain);
2132     EraseInstruction(Autorelease);
2133   }
2134 }
2135 
2136 #ifndef NDEBUG
2137 void
2138 ObjCARCOpt::GatherStatistics(Function &F, bool AfterOptimization) {
2139   llvm::Statistic &NumRetains =
2140     AfterOptimization? NumRetainsAfterOpt : NumRetainsBeforeOpt;
2141   llvm::Statistic &NumReleases =
2142     AfterOptimization? NumReleasesAfterOpt : NumReleasesBeforeOpt;
2143 
2144   for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
2145     Instruction *Inst = &*I++;
2146     switch (GetBasicARCInstKind(Inst)) {
2147     default:
2148       break;
2149     case ARCInstKind::Retain:
2150       ++NumRetains;
2151       break;
2152     case ARCInstKind::Release:
2153       ++NumReleases;
2154       break;
2155     }
2156   }
2157 }
2158 #endif
2159 
2160 bool ObjCARCOpt::doInitialization(Module &M) {
2161   if (!EnableARCOpts)
2162     return false;
2163 
2164   // If nothing in the Module uses ARC, don't do anything.
2165   Run = ModuleHasARC(M);
2166   if (!Run)
2167     return false;
2168 
2169   // Intuitively, objc_retain and others are nocapture, however in practice
2170   // they are not, because they return their argument value. And objc_release
2171   // calls finalizers which can have arbitrary side effects.
2172   MDKindCache.init(&M);
2173 
2174   // Initialize our runtime entry point cache.
2175   EP.init(&M);
2176 
2177   return false;
2178 }
2179 
2180 bool ObjCARCOpt::runOnFunction(Function &F) {
2181   if (!EnableARCOpts)
2182     return false;
2183 
2184   // If nothing in the Module uses ARC, don't do anything.
2185   if (!Run)
2186     return false;
2187 
2188   Changed = false;
2189 
2190   DEBUG(dbgs() << "<<< ObjCARCOpt: Visiting Function: " << F.getName() << " >>>"
2191         "\n");
2192 
2193   PA.setAA(&getAnalysis<AAResultsWrapperPass>().getAAResults());
2194 
2195 #ifndef NDEBUG
2196   if (AreStatisticsEnabled()) {
2197     GatherStatistics(F, false);
2198   }
2199 #endif
2200 
2201   // This pass performs several distinct transformations. As a compile-time aid
2202   // when compiling code that isn't ObjC, skip these if the relevant ObjC
2203   // library functions aren't declared.
2204 
2205   // Preliminary optimizations. This also computes UsedInThisFunction.
2206   OptimizeIndividualCalls(F);
2207 
2208   // Optimizations for weak pointers.
2209   if (UsedInThisFunction & ((1 << unsigned(ARCInstKind::LoadWeak)) |
2210                             (1 << unsigned(ARCInstKind::LoadWeakRetained)) |
2211                             (1 << unsigned(ARCInstKind::StoreWeak)) |
2212                             (1 << unsigned(ARCInstKind::InitWeak)) |
2213                             (1 << unsigned(ARCInstKind::CopyWeak)) |
2214                             (1 << unsigned(ARCInstKind::MoveWeak)) |
2215                             (1 << unsigned(ARCInstKind::DestroyWeak))))
2216     OptimizeWeakCalls(F);
2217 
2218   // Optimizations for retain+release pairs.
2219   if (UsedInThisFunction & ((1 << unsigned(ARCInstKind::Retain)) |
2220                             (1 << unsigned(ARCInstKind::RetainRV)) |
2221                             (1 << unsigned(ARCInstKind::RetainBlock))))
2222     if (UsedInThisFunction & (1 << unsigned(ARCInstKind::Release)))
2223       // Run OptimizeSequences until it either stops making changes or
2224       // no retain+release pair nesting is detected.
2225       while (OptimizeSequences(F)) {}
2226 
2227   // Optimizations if objc_autorelease is used.
2228   if (UsedInThisFunction & ((1 << unsigned(ARCInstKind::Autorelease)) |
2229                             (1 << unsigned(ARCInstKind::AutoreleaseRV))))
2230     OptimizeReturns(F);
2231 
2232   // Gather statistics after optimization.
2233 #ifndef NDEBUG
2234   if (AreStatisticsEnabled()) {
2235     GatherStatistics(F, true);
2236   }
2237 #endif
2238 
2239   DEBUG(dbgs() << "\n");
2240 
2241   return Changed;
2242 }
2243 
2244 void ObjCARCOpt::releaseMemory() {
2245   PA.clear();
2246 }
2247 
2248 /// @}
2249 ///
2250