1 //===- ObjCARCOpts.cpp - ObjC ARC Optimization ----------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 /// This file defines ObjC ARC optimizations. ARC stands for Automatic
11 /// Reference Counting and is a system for managing reference counts for objects
14 /// The optimizations performed include elimination of redundant, partially
15 /// redundant, and inconsequential reference count operations, elimination of
16 /// redundant weak pointer operations, and numerous minor simplifications.
18 /// WARNING: This file knows about certain library functions. It recognizes them
19 /// by name, and hardwires knowledge of their semantics.
21 /// WARNING: This file knows about how certain Objective-C library functions are
22 /// used. Naive LLVM IR transformations which would otherwise be
23 /// behavior-preserving may break these assumptions.
25 //===----------------------------------------------------------------------===//
28 #include "ARCRuntimeEntryPoints.h"
29 #include "BlotMapVector.h"
30 #include "DependencyAnalysis.h"
31 #include "ObjCARCAliasAnalysis.h"
32 #include "ProvenanceAnalysis.h"
34 #include "llvm/ADT/DenseMap.h"
35 #include "llvm/ADT/DenseSet.h"
36 #include "llvm/ADT/STLExtras.h"
37 #include "llvm/ADT/SmallPtrSet.h"
38 #include "llvm/ADT/Statistic.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"
46 using namespace llvm::objcarc;
48 #define DEBUG_TYPE "objc-arc-opts"
50 /// \defgroup ARCUtilities Utility declarations/definitions specific to ARC.
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 if (Arg->hasOneUse()) {
57 if (const BitCastInst *BC = dyn_cast<BitCastInst>(Arg))
58 return FindSingleUseIdentifiedObject(BC->getOperand(0));
59 if (const GetElementPtrInst *GEP = dyn_cast<GetElementPtrInst>(Arg))
60 if (GEP->hasAllZeroIndices())
61 return FindSingleUseIdentifiedObject(GEP->getPointerOperand());
62 if (IsForwarding(GetBasicARCInstKind(Arg)))
63 return FindSingleUseIdentifiedObject(
64 cast<CallInst>(Arg)->getArgOperand(0));
65 if (!IsObjCIdentifiedObject(Arg))
70 // If we found an identifiable object but it has multiple uses, but they are
71 // trivial uses, we can still consider this to be a single-use value.
72 if (IsObjCIdentifiedObject(Arg)) {
73 for (const User *U : Arg->users())
74 if (!U->use_empty() || GetRCIdentityRoot(U) != Arg)
83 /// This is a wrapper around getUnderlyingObjCPtr along the lines of
84 /// GetUnderlyingObjects except that it returns early when it sees the first
86 static inline bool AreAnyUnderlyingObjectsAnAlloca(const Value *V,
87 const DataLayout &DL) {
88 SmallPtrSet<const Value *, 4> Visited;
89 SmallVector<const Value *, 4> Worklist;
90 Worklist.push_back(V);
92 const Value *P = Worklist.pop_back_val();
93 P = GetUnderlyingObjCPtr(P, DL);
95 if (isa<AllocaInst>(P))
98 if (!Visited.insert(P).second)
101 if (const SelectInst *SI = dyn_cast<const SelectInst>(P)) {
102 Worklist.push_back(SI->getTrueValue());
103 Worklist.push_back(SI->getFalseValue());
107 if (const PHINode *PN = dyn_cast<const PHINode>(P)) {
108 for (Value *IncValue : PN->incoming_values())
109 Worklist.push_back(IncValue);
112 } while (!Worklist.empty());
120 /// \defgroup ARCOpt ARC Optimization.
123 // TODO: On code like this:
126 // stuff_that_cannot_release()
127 // objc_autorelease(%x)
128 // stuff_that_cannot_release()
130 // stuff_that_cannot_release()
131 // objc_autorelease(%x)
133 // The second retain and autorelease can be deleted.
135 // TODO: It should be possible to delete
136 // objc_autoreleasePoolPush and objc_autoreleasePoolPop
137 // pairs if nothing is actually autoreleased between them. Also, autorelease
138 // calls followed by objc_autoreleasePoolPop calls (perhaps in ObjC++ code
139 // after inlining) can be turned into plain release calls.
141 // TODO: Critical-edge splitting. If the optimial insertion point is
142 // a critical edge, the current algorithm has to fail, because it doesn't
143 // know how to split edges. It should be possible to make the optimizer
144 // think in terms of edges, rather than blocks, and then split critical
147 // TODO: OptimizeSequences could generalized to be Interprocedural.
149 // TODO: Recognize that a bunch of other objc runtime calls have
150 // non-escaping arguments and non-releasing arguments, and may be
151 // non-autoreleasing.
153 // TODO: Sink autorelease calls as far as possible. Unfortunately we
154 // usually can't sink them past other calls, which would be the main
155 // case where it would be useful.
157 // TODO: The pointer returned from objc_loadWeakRetained is retained.
159 // TODO: Delete release+retain pairs (rare).
161 STATISTIC(NumNoops, "Number of no-op objc calls eliminated");
162 STATISTIC(NumPartialNoops, "Number of partially no-op objc calls eliminated");
163 STATISTIC(NumAutoreleases,"Number of autoreleases converted to releases");
164 STATISTIC(NumRets, "Number of return value forwarding "
165 "retain+autoreleases eliminated");
166 STATISTIC(NumRRs, "Number of retain+release paths eliminated");
167 STATISTIC(NumPeeps, "Number of calls peephole-optimized");
169 STATISTIC(NumRetainsBeforeOpt,
170 "Number of retains before optimization");
171 STATISTIC(NumReleasesBeforeOpt,
172 "Number of releases before optimization");
173 STATISTIC(NumRetainsAfterOpt,
174 "Number of retains after optimization");
175 STATISTIC(NumReleasesAfterOpt,
176 "Number of releases after optimization");
180 /// \brief Per-BasicBlock state.
182 /// The number of unique control paths from the entry which can reach this
184 unsigned TopDownPathCount;
186 /// The number of unique control paths to exits from this block.
187 unsigned BottomUpPathCount;
189 /// The top-down traversal uses this to record information known about a
190 /// pointer at the bottom of each block.
191 BlotMapVector<const Value *, TopDownPtrState> PerPtrTopDown;
193 /// The bottom-up traversal uses this to record information known about a
194 /// pointer at the top of each block.
195 BlotMapVector<const Value *, BottomUpPtrState> PerPtrBottomUp;
197 /// Effective predecessors of the current block ignoring ignorable edges and
198 /// ignored backedges.
199 SmallVector<BasicBlock *, 2> Preds;
201 /// Effective successors of the current block ignoring ignorable edges and
202 /// ignored backedges.
203 SmallVector<BasicBlock *, 2> Succs;
206 static const unsigned OverflowOccurredValue;
208 BBState() : TopDownPathCount(0), BottomUpPathCount(0) { }
210 typedef decltype(PerPtrTopDown)::iterator top_down_ptr_iterator;
211 typedef decltype(PerPtrTopDown)::const_iterator const_top_down_ptr_iterator;
213 top_down_ptr_iterator top_down_ptr_begin() { return PerPtrTopDown.begin(); }
214 top_down_ptr_iterator top_down_ptr_end() { return PerPtrTopDown.end(); }
215 const_top_down_ptr_iterator top_down_ptr_begin() const {
216 return PerPtrTopDown.begin();
218 const_top_down_ptr_iterator top_down_ptr_end() const {
219 return PerPtrTopDown.end();
221 bool hasTopDownPtrs() const {
222 return !PerPtrTopDown.empty();
225 typedef decltype(PerPtrBottomUp)::iterator bottom_up_ptr_iterator;
227 PerPtrBottomUp)::const_iterator const_bottom_up_ptr_iterator;
229 bottom_up_ptr_iterator bottom_up_ptr_begin() {
230 return PerPtrBottomUp.begin();
232 bottom_up_ptr_iterator bottom_up_ptr_end() { return PerPtrBottomUp.end(); }
233 const_bottom_up_ptr_iterator bottom_up_ptr_begin() const {
234 return PerPtrBottomUp.begin();
236 const_bottom_up_ptr_iterator bottom_up_ptr_end() const {
237 return PerPtrBottomUp.end();
239 bool hasBottomUpPtrs() const {
240 return !PerPtrBottomUp.empty();
243 /// Mark this block as being an entry block, which has one path from the
244 /// entry by definition.
245 void SetAsEntry() { TopDownPathCount = 1; }
247 /// Mark this block as being an exit block, which has one path to an exit by
249 void SetAsExit() { BottomUpPathCount = 1; }
251 /// Attempt to find the PtrState object describing the top down state for
252 /// pointer Arg. Return a new initialized PtrState describing the top down
253 /// state for Arg if we do not find one.
254 TopDownPtrState &getPtrTopDownState(const Value *Arg) {
255 return PerPtrTopDown[Arg];
258 /// Attempt to find the PtrState object describing the bottom up state for
259 /// pointer Arg. Return a new initialized PtrState describing the bottom up
260 /// state for Arg if we do not find one.
261 BottomUpPtrState &getPtrBottomUpState(const Value *Arg) {
262 return PerPtrBottomUp[Arg];
265 /// Attempt to find the PtrState object describing the bottom up state for
267 bottom_up_ptr_iterator findPtrBottomUpState(const Value *Arg) {
268 return PerPtrBottomUp.find(Arg);
271 void clearBottomUpPointers() {
272 PerPtrBottomUp.clear();
275 void clearTopDownPointers() {
276 PerPtrTopDown.clear();
279 void InitFromPred(const BBState &Other);
280 void InitFromSucc(const BBState &Other);
281 void MergePred(const BBState &Other);
282 void MergeSucc(const BBState &Other);
284 /// Compute the number of possible unique paths from an entry to an exit
285 /// which pass through this block. This is only valid after both the
286 /// top-down and bottom-up traversals are complete.
288 /// Returns true if overflow occurred. Returns false if overflow did not
290 bool GetAllPathCountWithOverflow(unsigned &PathCount) const {
291 if (TopDownPathCount == OverflowOccurredValue ||
292 BottomUpPathCount == OverflowOccurredValue)
294 unsigned long long Product =
295 (unsigned long long)TopDownPathCount*BottomUpPathCount;
296 // Overflow occurred if any of the upper bits of Product are set or if all
297 // the lower bits of Product are all set.
298 return (Product >> 32) ||
299 ((PathCount = Product) == OverflowOccurredValue);
302 // Specialized CFG utilities.
303 typedef SmallVectorImpl<BasicBlock *>::const_iterator edge_iterator;
304 edge_iterator pred_begin() const { return Preds.begin(); }
305 edge_iterator pred_end() const { return Preds.end(); }
306 edge_iterator succ_begin() const { return Succs.begin(); }
307 edge_iterator succ_end() const { return Succs.end(); }
309 void addSucc(BasicBlock *Succ) { Succs.push_back(Succ); }
310 void addPred(BasicBlock *Pred) { Preds.push_back(Pred); }
312 bool isExit() const { return Succs.empty(); }
315 const unsigned BBState::OverflowOccurredValue = 0xffffffff;
319 raw_ostream &operator<<(raw_ostream &OS,
320 BBState &BBState) LLVM_ATTRIBUTE_UNUSED;
323 void BBState::InitFromPred(const BBState &Other) {
324 PerPtrTopDown = Other.PerPtrTopDown;
325 TopDownPathCount = Other.TopDownPathCount;
328 void BBState::InitFromSucc(const BBState &Other) {
329 PerPtrBottomUp = Other.PerPtrBottomUp;
330 BottomUpPathCount = Other.BottomUpPathCount;
333 /// The top-down traversal uses this to merge information about predecessors to
334 /// form the initial state for a new block.
335 void BBState::MergePred(const BBState &Other) {
336 if (TopDownPathCount == OverflowOccurredValue)
339 // Other.TopDownPathCount can be 0, in which case it is either dead or a
340 // loop backedge. Loop backedges are special.
341 TopDownPathCount += Other.TopDownPathCount;
343 // In order to be consistent, we clear the top down pointers when by adding
344 // TopDownPathCount becomes OverflowOccurredValue even though "true" overflow
346 if (TopDownPathCount == OverflowOccurredValue) {
347 clearTopDownPointers();
351 // Check for overflow. If we have overflow, fall back to conservative
353 if (TopDownPathCount < Other.TopDownPathCount) {
354 TopDownPathCount = OverflowOccurredValue;
355 clearTopDownPointers();
359 // For each entry in the other set, if our set has an entry with the same key,
360 // merge the entries. Otherwise, copy the entry and merge it with an empty
362 for (auto MI = Other.top_down_ptr_begin(), ME = Other.top_down_ptr_end();
364 auto Pair = PerPtrTopDown.insert(*MI);
365 Pair.first->second.Merge(Pair.second ? TopDownPtrState() : MI->second,
369 // For each entry in our set, if the other set doesn't have an entry with the
370 // same key, force it to merge with an empty entry.
371 for (auto MI = top_down_ptr_begin(), ME = top_down_ptr_end(); MI != ME; ++MI)
372 if (Other.PerPtrTopDown.find(MI->first) == Other.PerPtrTopDown.end())
373 MI->second.Merge(TopDownPtrState(), /*TopDown=*/true);
376 /// The bottom-up traversal uses this to merge information about successors to
377 /// form the initial state for a new block.
378 void BBState::MergeSucc(const BBState &Other) {
379 if (BottomUpPathCount == OverflowOccurredValue)
382 // Other.BottomUpPathCount can be 0, in which case it is either dead or a
383 // loop backedge. Loop backedges are special.
384 BottomUpPathCount += Other.BottomUpPathCount;
386 // In order to be consistent, we clear the top down pointers when by adding
387 // BottomUpPathCount becomes OverflowOccurredValue even though "true" overflow
389 if (BottomUpPathCount == OverflowOccurredValue) {
390 clearBottomUpPointers();
394 // Check for overflow. If we have overflow, fall back to conservative
396 if (BottomUpPathCount < Other.BottomUpPathCount) {
397 BottomUpPathCount = OverflowOccurredValue;
398 clearBottomUpPointers();
402 // For each entry in the other set, if our set has an entry with the
403 // same key, merge the entries. Otherwise, copy the entry and merge
404 // it with an empty entry.
405 for (auto MI = Other.bottom_up_ptr_begin(), ME = Other.bottom_up_ptr_end();
407 auto Pair = PerPtrBottomUp.insert(*MI);
408 Pair.first->second.Merge(Pair.second ? BottomUpPtrState() : MI->second,
412 // For each entry in our set, if the other set doesn't have an entry
413 // with the same key, force it to merge with an empty entry.
414 for (auto MI = bottom_up_ptr_begin(), ME = bottom_up_ptr_end(); MI != ME;
416 if (Other.PerPtrBottomUp.find(MI->first) == Other.PerPtrBottomUp.end())
417 MI->second.Merge(BottomUpPtrState(), /*TopDown=*/false);
420 raw_ostream &llvm::operator<<(raw_ostream &OS, BBState &BBInfo) {
421 // Dump the pointers we are tracking.
422 OS << " TopDown State:\n";
423 if (!BBInfo.hasTopDownPtrs()) {
424 DEBUG(llvm::dbgs() << " NONE!\n");
426 for (auto I = BBInfo.top_down_ptr_begin(), E = BBInfo.top_down_ptr_end();
428 const PtrState &P = I->second;
429 OS << " Ptr: " << *I->first
430 << "\n KnownSafe: " << (P.IsKnownSafe()?"true":"false")
431 << "\n ImpreciseRelease: "
432 << (P.IsTrackingImpreciseReleases()?"true":"false") << "\n"
433 << " HasCFGHazards: "
434 << (P.IsCFGHazardAfflicted()?"true":"false") << "\n"
435 << " KnownPositive: "
436 << (P.HasKnownPositiveRefCount()?"true":"false") << "\n"
438 << P.GetSeq() << "\n";
442 OS << " BottomUp State:\n";
443 if (!BBInfo.hasBottomUpPtrs()) {
444 DEBUG(llvm::dbgs() << " NONE!\n");
446 for (auto I = BBInfo.bottom_up_ptr_begin(), E = BBInfo.bottom_up_ptr_end();
448 const PtrState &P = I->second;
449 OS << " Ptr: " << *I->first
450 << "\n KnownSafe: " << (P.IsKnownSafe()?"true":"false")
451 << "\n ImpreciseRelease: "
452 << (P.IsTrackingImpreciseReleases()?"true":"false") << "\n"
453 << " HasCFGHazards: "
454 << (P.IsCFGHazardAfflicted()?"true":"false") << "\n"
455 << " KnownPositive: "
456 << (P.HasKnownPositiveRefCount()?"true":"false") << "\n"
458 << P.GetSeq() << "\n";
467 /// \brief The main ARC optimization pass.
468 class ObjCARCOpt : public FunctionPass {
470 ProvenanceAnalysis PA;
472 /// A cache of references to runtime entry point constants.
473 ARCRuntimeEntryPoints EP;
475 /// A cache of MDKinds that can be passed into other functions to propagate
476 /// MDKind identifiers.
477 ARCMDKindCache MDKindCache;
479 // This is used to track if a pointer is stored into an alloca.
480 DenseSet<const Value *> MultiOwnersSet;
482 /// A flag indicating whether this optimization pass should run.
485 /// Flags which determine whether each of the interesting runtine functions
486 /// is in fact used in the current function.
487 unsigned UsedInThisFunction;
489 bool OptimizeRetainRVCall(Function &F, Instruction *RetainRV);
490 void OptimizeAutoreleaseRVCall(Function &F, Instruction *AutoreleaseRV,
492 void OptimizeIndividualCalls(Function &F);
494 void CheckForCFGHazards(const BasicBlock *BB,
495 DenseMap<const BasicBlock *, BBState> &BBStates,
496 BBState &MyStates) const;
497 bool VisitInstructionBottomUp(Instruction *Inst, BasicBlock *BB,
498 BlotMapVector<Value *, RRInfo> &Retains,
500 bool VisitBottomUp(BasicBlock *BB,
501 DenseMap<const BasicBlock *, BBState> &BBStates,
502 BlotMapVector<Value *, RRInfo> &Retains);
503 bool VisitInstructionTopDown(Instruction *Inst,
504 DenseMap<Value *, RRInfo> &Releases,
506 bool VisitTopDown(BasicBlock *BB,
507 DenseMap<const BasicBlock *, BBState> &BBStates,
508 DenseMap<Value *, RRInfo> &Releases);
509 bool Visit(Function &F, DenseMap<const BasicBlock *, BBState> &BBStates,
510 BlotMapVector<Value *, RRInfo> &Retains,
511 DenseMap<Value *, RRInfo> &Releases);
513 void MoveCalls(Value *Arg, RRInfo &RetainsToMove, RRInfo &ReleasesToMove,
514 BlotMapVector<Value *, RRInfo> &Retains,
515 DenseMap<Value *, RRInfo> &Releases,
516 SmallVectorImpl<Instruction *> &DeadInsts, Module *M);
519 PairUpRetainsAndReleases(DenseMap<const BasicBlock *, BBState> &BBStates,
520 BlotMapVector<Value *, RRInfo> &Retains,
521 DenseMap<Value *, RRInfo> &Releases, Module *M,
522 SmallVectorImpl<Instruction *> &NewRetains,
523 SmallVectorImpl<Instruction *> &NewReleases,
524 SmallVectorImpl<Instruction *> &DeadInsts,
525 RRInfo &RetainsToMove, RRInfo &ReleasesToMove,
526 Value *Arg, bool KnownSafe,
527 bool &AnyPairsCompletelyEliminated);
529 bool PerformCodePlacement(DenseMap<const BasicBlock *, BBState> &BBStates,
530 BlotMapVector<Value *, RRInfo> &Retains,
531 DenseMap<Value *, RRInfo> &Releases, Module *M);
533 void OptimizeWeakCalls(Function &F);
535 bool OptimizeSequences(Function &F);
537 void OptimizeReturns(Function &F);
540 void GatherStatistics(Function &F, bool AfterOptimization = false);
543 void getAnalysisUsage(AnalysisUsage &AU) const override;
544 bool doInitialization(Module &M) override;
545 bool runOnFunction(Function &F) override;
546 void releaseMemory() override;
550 ObjCARCOpt() : FunctionPass(ID) {
551 initializeObjCARCOptPass(*PassRegistry::getPassRegistry());
556 char ObjCARCOpt::ID = 0;
557 INITIALIZE_PASS_BEGIN(ObjCARCOpt,
558 "objc-arc", "ObjC ARC optimization", false, false)
559 INITIALIZE_PASS_DEPENDENCY(ObjCARCAliasAnalysis)
560 INITIALIZE_PASS_END(ObjCARCOpt,
561 "objc-arc", "ObjC ARC optimization", false, false)
563 Pass *llvm::createObjCARCOptPass() {
564 return new ObjCARCOpt();
567 void ObjCARCOpt::getAnalysisUsage(AnalysisUsage &AU) const {
568 AU.addRequired<ObjCARCAliasAnalysis>();
569 AU.addRequired<AliasAnalysis>();
570 // ARC optimization doesn't currently split critical edges.
571 AU.setPreservesCFG();
574 /// Turn objc_retainAutoreleasedReturnValue into objc_retain if the operand is
575 /// not a return value. Or, if it can be paired with an
576 /// objc_autoreleaseReturnValue, delete the pair and return true.
578 ObjCARCOpt::OptimizeRetainRVCall(Function &F, Instruction *RetainRV) {
579 // Check for the argument being from an immediately preceding call or invoke.
580 const Value *Arg = GetArgRCIdentityRoot(RetainRV);
581 ImmutableCallSite CS(Arg);
582 if (const Instruction *Call = CS.getInstruction()) {
583 if (Call->getParent() == RetainRV->getParent()) {
584 BasicBlock::const_iterator I = Call;
586 while (IsNoopInstruction(I)) ++I;
589 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(Call)) {
590 BasicBlock *RetainRVParent = RetainRV->getParent();
591 if (II->getNormalDest() == RetainRVParent) {
592 BasicBlock::const_iterator I = RetainRVParent->begin();
593 while (IsNoopInstruction(I)) ++I;
600 // Check for being preceded by an objc_autoreleaseReturnValue on the same
601 // pointer. In this case, we can delete the pair.
602 BasicBlock::iterator I = RetainRV, Begin = RetainRV->getParent()->begin();
604 do --I; while (I != Begin && IsNoopInstruction(I));
605 if (GetBasicARCInstKind(I) == ARCInstKind::AutoreleaseRV &&
606 GetArgRCIdentityRoot(I) == Arg) {
610 DEBUG(dbgs() << "Erasing autoreleaseRV,retainRV pair: " << *I << "\n"
611 << "Erasing " << *RetainRV << "\n");
614 EraseInstruction(RetainRV);
619 // Turn it to a plain objc_retain.
623 DEBUG(dbgs() << "Transforming objc_retainAutoreleasedReturnValue => "
624 "objc_retain since the operand is not a return value.\n"
625 "Old = " << *RetainRV << "\n");
627 Constant *NewDecl = EP.get(ARCRuntimeEntryPointKind::Retain);
628 cast<CallInst>(RetainRV)->setCalledFunction(NewDecl);
630 DEBUG(dbgs() << "New = " << *RetainRV << "\n");
635 /// Turn objc_autoreleaseReturnValue into objc_autorelease if the result is not
636 /// used as a return value.
637 void ObjCARCOpt::OptimizeAutoreleaseRVCall(Function &F,
638 Instruction *AutoreleaseRV,
639 ARCInstKind &Class) {
640 // Check for a return of the pointer value.
641 const Value *Ptr = GetArgRCIdentityRoot(AutoreleaseRV);
642 SmallVector<const Value *, 2> Users;
643 Users.push_back(Ptr);
645 Ptr = Users.pop_back_val();
646 for (const User *U : Ptr->users()) {
647 if (isa<ReturnInst>(U) || GetBasicARCInstKind(U) == ARCInstKind::RetainRV)
649 if (isa<BitCastInst>(U))
652 } while (!Users.empty());
657 DEBUG(dbgs() << "Transforming objc_autoreleaseReturnValue => "
658 "objc_autorelease since its operand is not used as a return "
660 "Old = " << *AutoreleaseRV << "\n");
662 CallInst *AutoreleaseRVCI = cast<CallInst>(AutoreleaseRV);
663 Constant *NewDecl = EP.get(ARCRuntimeEntryPointKind::Autorelease);
664 AutoreleaseRVCI->setCalledFunction(NewDecl);
665 AutoreleaseRVCI->setTailCall(false); // Never tail call objc_autorelease.
666 Class = ARCInstKind::Autorelease;
668 DEBUG(dbgs() << "New: " << *AutoreleaseRV << "\n");
672 /// Visit each call, one at a time, and make simplifications without doing any
673 /// additional analysis.
674 void ObjCARCOpt::OptimizeIndividualCalls(Function &F) {
675 DEBUG(dbgs() << "\n== ObjCARCOpt::OptimizeIndividualCalls ==\n");
676 // Reset all the flags in preparation for recomputing them.
677 UsedInThisFunction = 0;
679 // Visit all objc_* calls in F.
680 for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
681 Instruction *Inst = &*I++;
683 ARCInstKind Class = GetBasicARCInstKind(Inst);
685 DEBUG(dbgs() << "Visiting: Class: " << Class << "; " << *Inst << "\n");
690 // Delete no-op casts. These function calls have special semantics, but
691 // the semantics are entirely implemented via lowering in the front-end,
692 // so by the time they reach the optimizer, they are just no-op calls
693 // which return their argument.
695 // There are gray areas here, as the ability to cast reference-counted
696 // pointers to raw void* and back allows code to break ARC assumptions,
697 // however these are currently considered to be unimportant.
698 case ARCInstKind::NoopCast:
701 DEBUG(dbgs() << "Erasing no-op cast: " << *Inst << "\n");
702 EraseInstruction(Inst);
705 // If the pointer-to-weak-pointer is null, it's undefined behavior.
706 case ARCInstKind::StoreWeak:
707 case ARCInstKind::LoadWeak:
708 case ARCInstKind::LoadWeakRetained:
709 case ARCInstKind::InitWeak:
710 case ARCInstKind::DestroyWeak: {
711 CallInst *CI = cast<CallInst>(Inst);
712 if (IsNullOrUndef(CI->getArgOperand(0))) {
714 Type *Ty = CI->getArgOperand(0)->getType();
715 new StoreInst(UndefValue::get(cast<PointerType>(Ty)->getElementType()),
716 Constant::getNullValue(Ty),
718 llvm::Value *NewValue = UndefValue::get(CI->getType());
719 DEBUG(dbgs() << "A null pointer-to-weak-pointer is undefined behavior."
720 "\nOld = " << *CI << "\nNew = " << *NewValue << "\n");
721 CI->replaceAllUsesWith(NewValue);
722 CI->eraseFromParent();
727 case ARCInstKind::CopyWeak:
728 case ARCInstKind::MoveWeak: {
729 CallInst *CI = cast<CallInst>(Inst);
730 if (IsNullOrUndef(CI->getArgOperand(0)) ||
731 IsNullOrUndef(CI->getArgOperand(1))) {
733 Type *Ty = CI->getArgOperand(0)->getType();
734 new StoreInst(UndefValue::get(cast<PointerType>(Ty)->getElementType()),
735 Constant::getNullValue(Ty),
738 llvm::Value *NewValue = UndefValue::get(CI->getType());
739 DEBUG(dbgs() << "A null pointer-to-weak-pointer is undefined behavior."
740 "\nOld = " << *CI << "\nNew = " << *NewValue << "\n");
742 CI->replaceAllUsesWith(NewValue);
743 CI->eraseFromParent();
748 case ARCInstKind::RetainRV:
749 if (OptimizeRetainRVCall(F, Inst))
752 case ARCInstKind::AutoreleaseRV:
753 OptimizeAutoreleaseRVCall(F, Inst, Class);
757 // objc_autorelease(x) -> objc_release(x) if x is otherwise unused.
758 if (IsAutorelease(Class) && Inst->use_empty()) {
759 CallInst *Call = cast<CallInst>(Inst);
760 const Value *Arg = Call->getArgOperand(0);
761 Arg = FindSingleUseIdentifiedObject(Arg);
766 // Create the declaration lazily.
767 LLVMContext &C = Inst->getContext();
769 Constant *Decl = EP.get(ARCRuntimeEntryPointKind::Release);
770 CallInst *NewCall = CallInst::Create(Decl, Call->getArgOperand(0), "",
772 NewCall->setMetadata(MDKindCache.get(ARCMDKindID::ImpreciseRelease),
773 MDNode::get(C, None));
775 DEBUG(dbgs() << "Replacing autorelease{,RV}(x) with objc_release(x) "
776 "since x is otherwise unused.\nOld: " << *Call << "\nNew: "
777 << *NewCall << "\n");
779 EraseInstruction(Call);
781 Class = ARCInstKind::Release;
785 // For functions which can never be passed stack arguments, add
787 if (IsAlwaysTail(Class)) {
789 DEBUG(dbgs() << "Adding tail keyword to function since it can never be "
790 "passed stack args: " << *Inst << "\n");
791 cast<CallInst>(Inst)->setTailCall();
794 // Ensure that functions that can never have a "tail" keyword due to the
795 // semantics of ARC truly do not do so.
796 if (IsNeverTail(Class)) {
798 DEBUG(dbgs() << "Removing tail keyword from function: " << *Inst <<
800 cast<CallInst>(Inst)->setTailCall(false);
803 // Set nounwind as needed.
804 if (IsNoThrow(Class)) {
806 DEBUG(dbgs() << "Found no throw class. Setting nounwind on: " << *Inst
808 cast<CallInst>(Inst)->setDoesNotThrow();
811 if (!IsNoopOnNull(Class)) {
812 UsedInThisFunction |= 1 << unsigned(Class);
816 const Value *Arg = GetArgRCIdentityRoot(Inst);
818 // ARC calls with null are no-ops. Delete them.
819 if (IsNullOrUndef(Arg)) {
822 DEBUG(dbgs() << "ARC calls with null are no-ops. Erasing: " << *Inst
824 EraseInstruction(Inst);
828 // Keep track of which of retain, release, autorelease, and retain_block
829 // are actually present in this function.
830 UsedInThisFunction |= 1 << unsigned(Class);
832 // If Arg is a PHI, and one or more incoming values to the
833 // PHI are null, and the call is control-equivalent to the PHI, and there
834 // are no relevant side effects between the PHI and the call, the call
835 // could be pushed up to just those paths with non-null incoming values.
836 // For now, don't bother splitting critical edges for this.
837 SmallVector<std::pair<Instruction *, const Value *>, 4> Worklist;
838 Worklist.push_back(std::make_pair(Inst, Arg));
840 std::pair<Instruction *, const Value *> Pair = Worklist.pop_back_val();
844 const PHINode *PN = dyn_cast<PHINode>(Arg);
847 // Determine if the PHI has any null operands, or any incoming
849 bool HasNull = false;
850 bool HasCriticalEdges = false;
851 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
853 GetRCIdentityRoot(PN->getIncomingValue(i));
854 if (IsNullOrUndef(Incoming))
856 else if (cast<TerminatorInst>(PN->getIncomingBlock(i)->back())
857 .getNumSuccessors() != 1) {
858 HasCriticalEdges = true;
862 // If we have null operands and no critical edges, optimize.
863 if (!HasCriticalEdges && HasNull) {
864 SmallPtrSet<Instruction *, 4> DependingInstructions;
865 SmallPtrSet<const BasicBlock *, 4> Visited;
867 // Check that there is nothing that cares about the reference
868 // count between the call and the phi.
870 case ARCInstKind::Retain:
871 case ARCInstKind::RetainBlock:
872 // These can always be moved up.
874 case ARCInstKind::Release:
875 // These can't be moved across things that care about the retain
877 FindDependencies(NeedsPositiveRetainCount, Arg,
878 Inst->getParent(), Inst,
879 DependingInstructions, Visited, PA);
881 case ARCInstKind::Autorelease:
882 // These can't be moved across autorelease pool scope boundaries.
883 FindDependencies(AutoreleasePoolBoundary, Arg,
884 Inst->getParent(), Inst,
885 DependingInstructions, Visited, PA);
887 case ARCInstKind::RetainRV:
888 case ARCInstKind::AutoreleaseRV:
889 // Don't move these; the RV optimization depends on the autoreleaseRV
890 // being tail called, and the retainRV being immediately after a call
891 // (which might still happen if we get lucky with codegen layout, but
892 // it's not worth taking the chance).
895 llvm_unreachable("Invalid dependence flavor");
898 if (DependingInstructions.size() == 1 &&
899 *DependingInstructions.begin() == PN) {
902 // Clone the call into each predecessor that has a non-null value.
903 CallInst *CInst = cast<CallInst>(Inst);
904 Type *ParamTy = CInst->getArgOperand(0)->getType();
905 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
907 GetRCIdentityRoot(PN->getIncomingValue(i));
908 if (!IsNullOrUndef(Incoming)) {
909 CallInst *Clone = cast<CallInst>(CInst->clone());
910 Value *Op = PN->getIncomingValue(i);
911 Instruction *InsertPos = &PN->getIncomingBlock(i)->back();
912 if (Op->getType() != ParamTy)
913 Op = new BitCastInst(Op, ParamTy, "", InsertPos);
914 Clone->setArgOperand(0, Op);
915 Clone->insertBefore(InsertPos);
917 DEBUG(dbgs() << "Cloning "
919 "And inserting clone at " << *InsertPos << "\n");
920 Worklist.push_back(std::make_pair(Clone, Incoming));
923 // Erase the original call.
924 DEBUG(dbgs() << "Erasing: " << *CInst << "\n");
925 EraseInstruction(CInst);
929 } while (!Worklist.empty());
933 /// If we have a top down pointer in the S_Use state, make sure that there are
934 /// no CFG hazards by checking the states of various bottom up pointers.
935 static void CheckForUseCFGHazard(const Sequence SuccSSeq,
936 const bool SuccSRRIKnownSafe,
938 bool &SomeSuccHasSame,
939 bool &AllSuccsHaveSame,
940 bool &NotAllSeqEqualButKnownSafe,
941 bool &ShouldContinue) {
944 if (!S.IsKnownSafe() && !SuccSRRIKnownSafe) {
945 S.ClearSequenceProgress();
948 S.SetCFGHazardAfflicted(true);
949 ShouldContinue = true;
953 SomeSuccHasSame = true;
957 case S_MovableRelease:
958 if (!S.IsKnownSafe() && !SuccSRRIKnownSafe)
959 AllSuccsHaveSame = false;
961 NotAllSeqEqualButKnownSafe = true;
964 llvm_unreachable("bottom-up pointer in retain state!");
966 llvm_unreachable("This should have been handled earlier.");
970 /// If we have a Top Down pointer in the S_CanRelease state, make sure that
971 /// there are no CFG hazards by checking the states of various bottom up
973 static void CheckForCanReleaseCFGHazard(const Sequence SuccSSeq,
974 const bool SuccSRRIKnownSafe,
976 bool &SomeSuccHasSame,
977 bool &AllSuccsHaveSame,
978 bool &NotAllSeqEqualButKnownSafe) {
981 SomeSuccHasSame = true;
985 case S_MovableRelease:
987 if (!S.IsKnownSafe() && !SuccSRRIKnownSafe)
988 AllSuccsHaveSame = false;
990 NotAllSeqEqualButKnownSafe = true;
993 llvm_unreachable("bottom-up pointer in retain state!");
995 llvm_unreachable("This should have been handled earlier.");
999 /// Check for critical edges, loop boundaries, irreducible control flow, or
1000 /// other CFG structures where moving code across the edge would result in it
1001 /// being executed more.
1003 ObjCARCOpt::CheckForCFGHazards(const BasicBlock *BB,
1004 DenseMap<const BasicBlock *, BBState> &BBStates,
1005 BBState &MyStates) const {
1006 // If any top-down local-use or possible-dec has a succ which is earlier in
1007 // the sequence, forget it.
1008 for (auto I = MyStates.top_down_ptr_begin(), E = MyStates.top_down_ptr_end();
1010 TopDownPtrState &S = I->second;
1011 const Sequence Seq = I->second.GetSeq();
1013 // We only care about S_Retain, S_CanRelease, and S_Use.
1017 // Make sure that if extra top down states are added in the future that this
1018 // code is updated to handle it.
1019 assert((Seq == S_Retain || Seq == S_CanRelease || Seq == S_Use) &&
1020 "Unknown top down sequence state.");
1022 const Value *Arg = I->first;
1023 const TerminatorInst *TI = cast<TerminatorInst>(&BB->back());
1024 bool SomeSuccHasSame = false;
1025 bool AllSuccsHaveSame = true;
1026 bool NotAllSeqEqualButKnownSafe = false;
1028 succ_const_iterator SI(TI), SE(TI, false);
1030 for (; SI != SE; ++SI) {
1031 // If VisitBottomUp has pointer information for this successor, take
1032 // what we know about it.
1033 const DenseMap<const BasicBlock *, BBState>::iterator BBI =
1035 assert(BBI != BBStates.end());
1036 const BottomUpPtrState &SuccS = BBI->second.getPtrBottomUpState(Arg);
1037 const Sequence SuccSSeq = SuccS.GetSeq();
1039 // If bottom up, the pointer is in an S_None state, clear the sequence
1040 // progress since the sequence in the bottom up state finished
1041 // suggesting a mismatch in between retains/releases. This is true for
1042 // all three cases that we are handling here: S_Retain, S_Use, and
1044 if (SuccSSeq == S_None) {
1045 S.ClearSequenceProgress();
1049 // If we have S_Use or S_CanRelease, perform our check for cfg hazard
1051 const bool SuccSRRIKnownSafe = SuccS.IsKnownSafe();
1053 // *NOTE* We do not use Seq from above here since we are allowing for
1054 // S.GetSeq() to change while we are visiting basic blocks.
1055 switch(S.GetSeq()) {
1057 bool ShouldContinue = false;
1058 CheckForUseCFGHazard(SuccSSeq, SuccSRRIKnownSafe, S, SomeSuccHasSame,
1059 AllSuccsHaveSame, NotAllSeqEqualButKnownSafe,
1065 case S_CanRelease: {
1066 CheckForCanReleaseCFGHazard(SuccSSeq, SuccSRRIKnownSafe, S,
1067 SomeSuccHasSame, AllSuccsHaveSame,
1068 NotAllSeqEqualButKnownSafe);
1075 case S_MovableRelease:
1080 // If the state at the other end of any of the successor edges
1081 // matches the current state, require all edges to match. This
1082 // guards against loops in the middle of a sequence.
1083 if (SomeSuccHasSame && !AllSuccsHaveSame) {
1084 S.ClearSequenceProgress();
1085 } else if (NotAllSeqEqualButKnownSafe) {
1086 // If we would have cleared the state foregoing the fact that we are known
1087 // safe, stop code motion. This is because whether or not it is safe to
1088 // remove RR pairs via KnownSafe is an orthogonal concept to whether we
1089 // are allowed to perform code motion.
1090 S.SetCFGHazardAfflicted(true);
1095 bool ObjCARCOpt::VisitInstructionBottomUp(
1096 Instruction *Inst, BasicBlock *BB, BlotMapVector<Value *, RRInfo> &Retains,
1097 BBState &MyStates) {
1098 bool NestingDetected = false;
1099 ARCInstKind Class = GetARCInstKind(Inst);
1100 const Value *Arg = nullptr;
1102 DEBUG(dbgs() << " Class: " << Class << "\n");
1105 case ARCInstKind::Release: {
1106 Arg = GetArgRCIdentityRoot(Inst);
1108 BottomUpPtrState &S = MyStates.getPtrBottomUpState(Arg);
1109 NestingDetected |= S.InitBottomUp(MDKindCache, Inst);
1112 case ARCInstKind::RetainBlock:
1113 // In OptimizeIndividualCalls, we have strength reduced all optimizable
1114 // objc_retainBlocks to objc_retains. Thus at this point any
1115 // objc_retainBlocks that we see are not optimizable.
1117 case ARCInstKind::Retain:
1118 case ARCInstKind::RetainRV: {
1119 Arg = GetArgRCIdentityRoot(Inst);
1120 BottomUpPtrState &S = MyStates.getPtrBottomUpState(Arg);
1121 if (S.MatchWithRetain()) {
1122 // Don't do retain+release tracking for ARCInstKind::RetainRV, because
1123 // it's better to let it remain as the first instruction after a call.
1124 if (Class != ARCInstKind::RetainRV) {
1125 DEBUG(llvm::dbgs() << " Matching with: " << *Inst << "\n");
1126 Retains[Inst] = S.GetRRInfo();
1128 S.ClearSequenceProgress();
1130 // A retain moving bottom up can be a use.
1133 case ARCInstKind::AutoreleasepoolPop:
1134 // Conservatively, clear MyStates for all known pointers.
1135 MyStates.clearBottomUpPointers();
1136 return NestingDetected;
1137 case ARCInstKind::AutoreleasepoolPush:
1138 case ARCInstKind::None:
1139 // These are irrelevant.
1140 return NestingDetected;
1141 case ARCInstKind::User:
1142 // If we have a store into an alloca of a pointer we are tracking, the
1143 // pointer has multiple owners implying that we must be more conservative.
1145 // This comes up in the context of a pointer being ``KnownSafe''. In the
1146 // presence of a block being initialized, the frontend will emit the
1147 // objc_retain on the original pointer and the release on the pointer loaded
1148 // from the alloca. The optimizer will through the provenance analysis
1149 // realize that the two are related, but since we only require KnownSafe in
1150 // one direction, will match the inner retain on the original pointer with
1151 // the guard release on the original pointer. This is fixed by ensuring that
1152 // in the presence of allocas we only unconditionally remove pointers if
1153 // both our retain and our release are KnownSafe.
1154 if (StoreInst *SI = dyn_cast<StoreInst>(Inst)) {
1155 const DataLayout &DL = BB->getModule()->getDataLayout();
1156 if (AreAnyUnderlyingObjectsAnAlloca(SI->getPointerOperand(), DL)) {
1157 auto I = MyStates.findPtrBottomUpState(
1158 GetRCIdentityRoot(SI->getValueOperand()));
1159 if (I != MyStates.bottom_up_ptr_end())
1160 MultiOwnersSet.insert(I->first);
1168 // Consider any other possible effects of this instruction on each
1169 // pointer being tracked.
1170 for (auto MI = MyStates.bottom_up_ptr_begin(),
1171 ME = MyStates.bottom_up_ptr_end();
1173 const Value *Ptr = MI->first;
1175 continue; // Handled above.
1176 BottomUpPtrState &S = MI->second;
1178 if (S.HandlePotentialAlterRefCount(Inst, Ptr, PA, Class))
1181 S.HandlePotentialUse(BB, Inst, Ptr, PA, Class);
1184 return NestingDetected;
1187 bool ObjCARCOpt::VisitBottomUp(BasicBlock *BB,
1188 DenseMap<const BasicBlock *, BBState> &BBStates,
1189 BlotMapVector<Value *, RRInfo> &Retains) {
1191 DEBUG(dbgs() << "\n== ObjCARCOpt::VisitBottomUp ==\n");
1193 bool NestingDetected = false;
1194 BBState &MyStates = BBStates[BB];
1196 // Merge the states from each successor to compute the initial state
1197 // for the current block.
1198 BBState::edge_iterator SI(MyStates.succ_begin()),
1199 SE(MyStates.succ_end());
1201 const BasicBlock *Succ = *SI;
1202 DenseMap<const BasicBlock *, BBState>::iterator I = BBStates.find(Succ);
1203 assert(I != BBStates.end());
1204 MyStates.InitFromSucc(I->second);
1206 for (; SI != SE; ++SI) {
1208 I = BBStates.find(Succ);
1209 assert(I != BBStates.end());
1210 MyStates.MergeSucc(I->second);
1214 DEBUG(llvm::dbgs() << "Before:\n" << BBStates[BB] << "\n"
1215 << "Performing Dataflow:\n");
1217 // Visit all the instructions, bottom-up.
1218 for (BasicBlock::iterator I = BB->end(), E = BB->begin(); I != E; --I) {
1219 Instruction *Inst = std::prev(I);
1221 // Invoke instructions are visited as part of their successors (below).
1222 if (isa<InvokeInst>(Inst))
1225 DEBUG(dbgs() << " Visiting " << *Inst << "\n");
1227 NestingDetected |= VisitInstructionBottomUp(Inst, BB, Retains, MyStates);
1230 // If there's a predecessor with an invoke, visit the invoke as if it were
1231 // part of this block, since we can't insert code after an invoke in its own
1232 // block, and we don't want to split critical edges.
1233 for (BBState::edge_iterator PI(MyStates.pred_begin()),
1234 PE(MyStates.pred_end()); PI != PE; ++PI) {
1235 BasicBlock *Pred = *PI;
1236 if (InvokeInst *II = dyn_cast<InvokeInst>(&Pred->back()))
1237 NestingDetected |= VisitInstructionBottomUp(II, BB, Retains, MyStates);
1240 DEBUG(llvm::dbgs() << "\nFinal State:\n" << BBStates[BB] << "\n");
1242 return NestingDetected;
1246 ObjCARCOpt::VisitInstructionTopDown(Instruction *Inst,
1247 DenseMap<Value *, RRInfo> &Releases,
1248 BBState &MyStates) {
1249 bool NestingDetected = false;
1250 ARCInstKind Class = GetARCInstKind(Inst);
1251 const Value *Arg = nullptr;
1253 DEBUG(llvm::dbgs() << " Class: " << Class << "\n");
1256 case ARCInstKind::RetainBlock:
1257 // In OptimizeIndividualCalls, we have strength reduced all optimizable
1258 // objc_retainBlocks to objc_retains. Thus at this point any
1259 // objc_retainBlocks that we see are not optimizable. We need to break since
1260 // a retain can be a potential use.
1262 case ARCInstKind::Retain:
1263 case ARCInstKind::RetainRV: {
1264 Arg = GetArgRCIdentityRoot(Inst);
1265 TopDownPtrState &S = MyStates.getPtrTopDownState(Arg);
1266 NestingDetected |= S.InitTopDown(Class, Inst);
1267 // A retain can be a potential use; procede to the generic checking
1271 case ARCInstKind::Release: {
1272 Arg = GetArgRCIdentityRoot(Inst);
1273 TopDownPtrState &S = MyStates.getPtrTopDownState(Arg);
1274 // Try to form a tentative pair in between this release instruction and the
1275 // top down pointers that we are tracking.
1276 if (S.MatchWithRelease(MDKindCache, Inst)) {
1277 // If we succeed, copy S's RRInfo into the Release -> {Retain Set
1278 // Map}. Then we clear S.
1279 DEBUG(llvm::dbgs() << " Matching with: " << *Inst << "\n");
1280 Releases[Inst] = S.GetRRInfo();
1281 S.ClearSequenceProgress();
1285 case ARCInstKind::AutoreleasepoolPop:
1286 // Conservatively, clear MyStates for all known pointers.
1287 MyStates.clearTopDownPointers();
1289 case ARCInstKind::AutoreleasepoolPush:
1290 case ARCInstKind::None:
1291 // These can not be uses of
1297 // Consider any other possible effects of this instruction on each
1298 // pointer being tracked.
1299 for (auto MI = MyStates.top_down_ptr_begin(),
1300 ME = MyStates.top_down_ptr_end();
1302 const Value *Ptr = MI->first;
1304 continue; // Handled above.
1305 TopDownPtrState &S = MI->second;
1306 if (S.HandlePotentialAlterRefCount(Inst, Ptr, PA, Class))
1309 S.HandlePotentialUse(Inst, Ptr, PA, Class);
1312 return NestingDetected;
1316 ObjCARCOpt::VisitTopDown(BasicBlock *BB,
1317 DenseMap<const BasicBlock *, BBState> &BBStates,
1318 DenseMap<Value *, RRInfo> &Releases) {
1319 DEBUG(dbgs() << "\n== ObjCARCOpt::VisitTopDown ==\n");
1320 bool NestingDetected = false;
1321 BBState &MyStates = BBStates[BB];
1323 // Merge the states from each predecessor to compute the initial state
1324 // for the current block.
1325 BBState::edge_iterator PI(MyStates.pred_begin()),
1326 PE(MyStates.pred_end());
1328 const BasicBlock *Pred = *PI;
1329 DenseMap<const BasicBlock *, BBState>::iterator I = BBStates.find(Pred);
1330 assert(I != BBStates.end());
1331 MyStates.InitFromPred(I->second);
1333 for (; PI != PE; ++PI) {
1335 I = BBStates.find(Pred);
1336 assert(I != BBStates.end());
1337 MyStates.MergePred(I->second);
1341 DEBUG(llvm::dbgs() << "Before:\n" << BBStates[BB] << "\n"
1342 << "Performing Dataflow:\n");
1344 // Visit all the instructions, top-down.
1345 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
1346 Instruction *Inst = I;
1348 DEBUG(dbgs() << " Visiting " << *Inst << "\n");
1350 NestingDetected |= VisitInstructionTopDown(Inst, Releases, MyStates);
1353 DEBUG(llvm::dbgs() << "\nState Before Checking for CFG Hazards:\n"
1354 << BBStates[BB] << "\n\n");
1355 CheckForCFGHazards(BB, BBStates, MyStates);
1356 DEBUG(llvm::dbgs() << "Final State:\n" << BBStates[BB] << "\n");
1357 return NestingDetected;
1361 ComputePostOrders(Function &F,
1362 SmallVectorImpl<BasicBlock *> &PostOrder,
1363 SmallVectorImpl<BasicBlock *> &ReverseCFGPostOrder,
1364 unsigned NoObjCARCExceptionsMDKind,
1365 DenseMap<const BasicBlock *, BBState> &BBStates) {
1366 /// The visited set, for doing DFS walks.
1367 SmallPtrSet<BasicBlock *, 16> Visited;
1369 // Do DFS, computing the PostOrder.
1370 SmallPtrSet<BasicBlock *, 16> OnStack;
1371 SmallVector<std::pair<BasicBlock *, succ_iterator>, 16> SuccStack;
1373 // Functions always have exactly one entry block, and we don't have
1374 // any other block that we treat like an entry block.
1375 BasicBlock *EntryBB = &F.getEntryBlock();
1376 BBState &MyStates = BBStates[EntryBB];
1377 MyStates.SetAsEntry();
1378 TerminatorInst *EntryTI = cast<TerminatorInst>(&EntryBB->back());
1379 SuccStack.push_back(std::make_pair(EntryBB, succ_iterator(EntryTI)));
1380 Visited.insert(EntryBB);
1381 OnStack.insert(EntryBB);
1384 BasicBlock *CurrBB = SuccStack.back().first;
1385 TerminatorInst *TI = cast<TerminatorInst>(&CurrBB->back());
1386 succ_iterator SE(TI, false);
1388 while (SuccStack.back().second != SE) {
1389 BasicBlock *SuccBB = *SuccStack.back().second++;
1390 if (Visited.insert(SuccBB).second) {
1391 TerminatorInst *TI = cast<TerminatorInst>(&SuccBB->back());
1392 SuccStack.push_back(std::make_pair(SuccBB, succ_iterator(TI)));
1393 BBStates[CurrBB].addSucc(SuccBB);
1394 BBState &SuccStates = BBStates[SuccBB];
1395 SuccStates.addPred(CurrBB);
1396 OnStack.insert(SuccBB);
1400 if (!OnStack.count(SuccBB)) {
1401 BBStates[CurrBB].addSucc(SuccBB);
1402 BBStates[SuccBB].addPred(CurrBB);
1405 OnStack.erase(CurrBB);
1406 PostOrder.push_back(CurrBB);
1407 SuccStack.pop_back();
1408 } while (!SuccStack.empty());
1412 // Do reverse-CFG DFS, computing the reverse-CFG PostOrder.
1413 // Functions may have many exits, and there also blocks which we treat
1414 // as exits due to ignored edges.
1415 SmallVector<std::pair<BasicBlock *, BBState::edge_iterator>, 16> PredStack;
1416 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
1417 BasicBlock *ExitBB = I;
1418 BBState &MyStates = BBStates[ExitBB];
1419 if (!MyStates.isExit())
1422 MyStates.SetAsExit();
1424 PredStack.push_back(std::make_pair(ExitBB, MyStates.pred_begin()));
1425 Visited.insert(ExitBB);
1426 while (!PredStack.empty()) {
1427 reverse_dfs_next_succ:
1428 BBState::edge_iterator PE = BBStates[PredStack.back().first].pred_end();
1429 while (PredStack.back().second != PE) {
1430 BasicBlock *BB = *PredStack.back().second++;
1431 if (Visited.insert(BB).second) {
1432 PredStack.push_back(std::make_pair(BB, BBStates[BB].pred_begin()));
1433 goto reverse_dfs_next_succ;
1436 ReverseCFGPostOrder.push_back(PredStack.pop_back_val().first);
1441 // Visit the function both top-down and bottom-up.
1442 bool ObjCARCOpt::Visit(Function &F,
1443 DenseMap<const BasicBlock *, BBState> &BBStates,
1444 BlotMapVector<Value *, RRInfo> &Retains,
1445 DenseMap<Value *, RRInfo> &Releases) {
1447 // Use reverse-postorder traversals, because we magically know that loops
1448 // will be well behaved, i.e. they won't repeatedly call retain on a single
1449 // pointer without doing a release. We can't use the ReversePostOrderTraversal
1450 // class here because we want the reverse-CFG postorder to consider each
1451 // function exit point, and we want to ignore selected cycle edges.
1452 SmallVector<BasicBlock *, 16> PostOrder;
1453 SmallVector<BasicBlock *, 16> ReverseCFGPostOrder;
1454 ComputePostOrders(F, PostOrder, ReverseCFGPostOrder,
1455 MDKindCache.get(ARCMDKindID::NoObjCARCExceptions),
1458 // Use reverse-postorder on the reverse CFG for bottom-up.
1459 bool BottomUpNestingDetected = false;
1460 for (SmallVectorImpl<BasicBlock *>::const_reverse_iterator I =
1461 ReverseCFGPostOrder.rbegin(), E = ReverseCFGPostOrder.rend();
1463 BottomUpNestingDetected |= VisitBottomUp(*I, BBStates, Retains);
1465 // Use reverse-postorder for top-down.
1466 bool TopDownNestingDetected = false;
1467 for (SmallVectorImpl<BasicBlock *>::const_reverse_iterator I =
1468 PostOrder.rbegin(), E = PostOrder.rend();
1470 TopDownNestingDetected |= VisitTopDown(*I, BBStates, Releases);
1472 return TopDownNestingDetected && BottomUpNestingDetected;
1475 /// Move the calls in RetainsToMove and ReleasesToMove.
1476 void ObjCARCOpt::MoveCalls(Value *Arg, RRInfo &RetainsToMove,
1477 RRInfo &ReleasesToMove,
1478 BlotMapVector<Value *, RRInfo> &Retains,
1479 DenseMap<Value *, RRInfo> &Releases,
1480 SmallVectorImpl<Instruction *> &DeadInsts,
1482 Type *ArgTy = Arg->getType();
1483 Type *ParamTy = PointerType::getUnqual(Type::getInt8Ty(ArgTy->getContext()));
1485 DEBUG(dbgs() << "== ObjCARCOpt::MoveCalls ==\n");
1487 // Insert the new retain and release calls.
1488 for (Instruction *InsertPt : ReleasesToMove.ReverseInsertPts) {
1489 Value *MyArg = ArgTy == ParamTy ? Arg :
1490 new BitCastInst(Arg, ParamTy, "", InsertPt);
1491 Constant *Decl = EP.get(ARCRuntimeEntryPointKind::Retain);
1492 CallInst *Call = CallInst::Create(Decl, MyArg, "", InsertPt);
1493 Call->setDoesNotThrow();
1494 Call->setTailCall();
1496 DEBUG(dbgs() << "Inserting new Retain: " << *Call << "\n"
1497 "At insertion point: " << *InsertPt << "\n");
1499 for (Instruction *InsertPt : RetainsToMove.ReverseInsertPts) {
1500 Value *MyArg = ArgTy == ParamTy ? Arg :
1501 new BitCastInst(Arg, ParamTy, "", InsertPt);
1502 Constant *Decl = EP.get(ARCRuntimeEntryPointKind::Release);
1503 CallInst *Call = CallInst::Create(Decl, MyArg, "", InsertPt);
1504 // Attach a clang.imprecise_release metadata tag, if appropriate.
1505 if (MDNode *M = ReleasesToMove.ReleaseMetadata)
1506 Call->setMetadata(MDKindCache.get(ARCMDKindID::ImpreciseRelease), M);
1507 Call->setDoesNotThrow();
1508 if (ReleasesToMove.IsTailCallRelease)
1509 Call->setTailCall();
1511 DEBUG(dbgs() << "Inserting new Release: " << *Call << "\n"
1512 "At insertion point: " << *InsertPt << "\n");
1515 // Delete the original retain and release calls.
1516 for (Instruction *OrigRetain : RetainsToMove.Calls) {
1517 Retains.blot(OrigRetain);
1518 DeadInsts.push_back(OrigRetain);
1519 DEBUG(dbgs() << "Deleting retain: " << *OrigRetain << "\n");
1521 for (Instruction *OrigRelease : ReleasesToMove.Calls) {
1522 Releases.erase(OrigRelease);
1523 DeadInsts.push_back(OrigRelease);
1524 DEBUG(dbgs() << "Deleting release: " << *OrigRelease << "\n");
1529 bool ObjCARCOpt::PairUpRetainsAndReleases(
1530 DenseMap<const BasicBlock *, BBState> &BBStates,
1531 BlotMapVector<Value *, RRInfo> &Retains,
1532 DenseMap<Value *, RRInfo> &Releases, Module *M,
1533 SmallVectorImpl<Instruction *> &NewRetains,
1534 SmallVectorImpl<Instruction *> &NewReleases,
1535 SmallVectorImpl<Instruction *> &DeadInsts, RRInfo &RetainsToMove,
1536 RRInfo &ReleasesToMove, Value *Arg, bool KnownSafe,
1537 bool &AnyPairsCompletelyEliminated) {
1538 // If a pair happens in a region where it is known that the reference count
1539 // is already incremented, we can similarly ignore possible decrements unless
1540 // we are dealing with a retainable object with multiple provenance sources.
1541 bool KnownSafeTD = true, KnownSafeBU = true;
1542 bool MultipleOwners = false;
1543 bool CFGHazardAfflicted = false;
1545 // Connect the dots between the top-down-collected RetainsToMove and
1546 // bottom-up-collected ReleasesToMove to form sets of related calls.
1547 // This is an iterative process so that we connect multiple releases
1548 // to multiple retains if needed.
1549 unsigned OldDelta = 0;
1550 unsigned NewDelta = 0;
1551 unsigned OldCount = 0;
1552 unsigned NewCount = 0;
1553 bool FirstRelease = true;
1555 for (SmallVectorImpl<Instruction *>::const_iterator
1556 NI = NewRetains.begin(), NE = NewRetains.end(); NI != NE; ++NI) {
1557 Instruction *NewRetain = *NI;
1558 auto It = Retains.find(NewRetain);
1559 assert(It != Retains.end());
1560 const RRInfo &NewRetainRRI = It->second;
1561 KnownSafeTD &= NewRetainRRI.KnownSafe;
1563 MultipleOwners || MultiOwnersSet.count(GetArgRCIdentityRoot(NewRetain));
1564 for (Instruction *NewRetainRelease : NewRetainRRI.Calls) {
1565 auto Jt = Releases.find(NewRetainRelease);
1566 if (Jt == Releases.end())
1568 const RRInfo &NewRetainReleaseRRI = Jt->second;
1570 // If the release does not have a reference to the retain as well,
1571 // something happened which is unaccounted for. Do not do anything.
1573 // This can happen if we catch an additive overflow during path count
1575 if (!NewRetainReleaseRRI.Calls.count(NewRetain))
1578 if (ReleasesToMove.Calls.insert(NewRetainRelease).second) {
1580 // If we overflow when we compute the path count, don't remove/move
1582 const BBState &NRRBBState = BBStates[NewRetainRelease->getParent()];
1583 unsigned PathCount = BBState::OverflowOccurredValue;
1584 if (NRRBBState.GetAllPathCountWithOverflow(PathCount))
1586 assert(PathCount != BBState::OverflowOccurredValue &&
1587 "PathCount at this point can not be "
1588 "OverflowOccurredValue.");
1589 OldDelta -= PathCount;
1591 // Merge the ReleaseMetadata and IsTailCallRelease values.
1593 ReleasesToMove.ReleaseMetadata =
1594 NewRetainReleaseRRI.ReleaseMetadata;
1595 ReleasesToMove.IsTailCallRelease =
1596 NewRetainReleaseRRI.IsTailCallRelease;
1597 FirstRelease = false;
1599 if (ReleasesToMove.ReleaseMetadata !=
1600 NewRetainReleaseRRI.ReleaseMetadata)
1601 ReleasesToMove.ReleaseMetadata = nullptr;
1602 if (ReleasesToMove.IsTailCallRelease !=
1603 NewRetainReleaseRRI.IsTailCallRelease)
1604 ReleasesToMove.IsTailCallRelease = false;
1607 // Collect the optimal insertion points.
1609 for (Instruction *RIP : NewRetainReleaseRRI.ReverseInsertPts) {
1610 if (ReleasesToMove.ReverseInsertPts.insert(RIP).second) {
1611 // If we overflow when we compute the path count, don't
1612 // remove/move anything.
1613 const BBState &RIPBBState = BBStates[RIP->getParent()];
1614 PathCount = BBState::OverflowOccurredValue;
1615 if (RIPBBState.GetAllPathCountWithOverflow(PathCount))
1617 assert(PathCount != BBState::OverflowOccurredValue &&
1618 "PathCount at this point can not be "
1619 "OverflowOccurredValue.");
1620 NewDelta -= PathCount;
1623 NewReleases.push_back(NewRetainRelease);
1628 if (NewReleases.empty()) break;
1630 // Back the other way.
1631 for (SmallVectorImpl<Instruction *>::const_iterator
1632 NI = NewReleases.begin(), NE = NewReleases.end(); NI != NE; ++NI) {
1633 Instruction *NewRelease = *NI;
1634 auto It = Releases.find(NewRelease);
1635 assert(It != Releases.end());
1636 const RRInfo &NewReleaseRRI = It->second;
1637 KnownSafeBU &= NewReleaseRRI.KnownSafe;
1638 CFGHazardAfflicted |= NewReleaseRRI.CFGHazardAfflicted;
1639 for (Instruction *NewReleaseRetain : NewReleaseRRI.Calls) {
1640 auto Jt = Retains.find(NewReleaseRetain);
1641 if (Jt == Retains.end())
1643 const RRInfo &NewReleaseRetainRRI = Jt->second;
1645 // If the retain does not have a reference to the release as well,
1646 // something happened which is unaccounted for. Do not do anything.
1648 // This can happen if we catch an additive overflow during path count
1650 if (!NewReleaseRetainRRI.Calls.count(NewRelease))
1653 if (RetainsToMove.Calls.insert(NewReleaseRetain).second) {
1654 // If we overflow when we compute the path count, don't remove/move
1656 const BBState &NRRBBState = BBStates[NewReleaseRetain->getParent()];
1657 unsigned PathCount = BBState::OverflowOccurredValue;
1658 if (NRRBBState.GetAllPathCountWithOverflow(PathCount))
1660 assert(PathCount != BBState::OverflowOccurredValue &&
1661 "PathCount at this point can not be "
1662 "OverflowOccurredValue.");
1663 OldDelta += PathCount;
1664 OldCount += PathCount;
1666 // Collect the optimal insertion points.
1668 for (Instruction *RIP : NewReleaseRetainRRI.ReverseInsertPts) {
1669 if (RetainsToMove.ReverseInsertPts.insert(RIP).second) {
1670 // If we overflow when we compute the path count, don't
1671 // remove/move anything.
1672 const BBState &RIPBBState = BBStates[RIP->getParent()];
1674 PathCount = BBState::OverflowOccurredValue;
1675 if (RIPBBState.GetAllPathCountWithOverflow(PathCount))
1677 assert(PathCount != BBState::OverflowOccurredValue &&
1678 "PathCount at this point can not be "
1679 "OverflowOccurredValue.");
1680 NewDelta += PathCount;
1681 NewCount += PathCount;
1684 NewRetains.push_back(NewReleaseRetain);
1688 NewReleases.clear();
1689 if (NewRetains.empty()) break;
1692 // We can only remove pointers if we are known safe in both directions.
1693 bool UnconditionallySafe = KnownSafeTD && KnownSafeBU;
1694 if (UnconditionallySafe) {
1695 RetainsToMove.ReverseInsertPts.clear();
1696 ReleasesToMove.ReverseInsertPts.clear();
1699 // Determine whether the new insertion points we computed preserve the
1700 // balance of retain and release calls through the program.
1701 // TODO: If the fully aggressive solution isn't valid, try to find a
1702 // less aggressive solution which is.
1706 // At this point, we are not going to remove any RR pairs, but we still are
1707 // able to move RR pairs. If one of our pointers is afflicted with
1708 // CFGHazards, we cannot perform such code motion so exit early.
1709 const bool WillPerformCodeMotion = RetainsToMove.ReverseInsertPts.size() ||
1710 ReleasesToMove.ReverseInsertPts.size();
1711 if (CFGHazardAfflicted && WillPerformCodeMotion)
1715 // Determine whether the original call points are balanced in the retain and
1716 // release calls through the program. If not, conservatively don't touch
1718 // TODO: It's theoretically possible to do code motion in this case, as
1719 // long as the existing imbalances are maintained.
1724 assert(OldCount != 0 && "Unreachable code?");
1725 NumRRs += OldCount - NewCount;
1726 // Set to true if we completely removed any RR pairs.
1727 AnyPairsCompletelyEliminated = NewCount == 0;
1729 // We can move calls!
1733 /// Identify pairings between the retains and releases, and delete and/or move
1735 bool ObjCARCOpt::PerformCodePlacement(
1736 DenseMap<const BasicBlock *, BBState> &BBStates,
1737 BlotMapVector<Value *, RRInfo> &Retains,
1738 DenseMap<Value *, RRInfo> &Releases, Module *M) {
1739 DEBUG(dbgs() << "\n== ObjCARCOpt::PerformCodePlacement ==\n");
1741 bool AnyPairsCompletelyEliminated = false;
1742 RRInfo RetainsToMove;
1743 RRInfo ReleasesToMove;
1744 SmallVector<Instruction *, 4> NewRetains;
1745 SmallVector<Instruction *, 4> NewReleases;
1746 SmallVector<Instruction *, 8> DeadInsts;
1748 // Visit each retain.
1749 for (BlotMapVector<Value *, RRInfo>::const_iterator I = Retains.begin(),
1752 Value *V = I->first;
1753 if (!V) continue; // blotted
1755 Instruction *Retain = cast<Instruction>(V);
1757 DEBUG(dbgs() << "Visiting: " << *Retain << "\n");
1759 Value *Arg = GetArgRCIdentityRoot(Retain);
1761 // If the object being released is in static or stack storage, we know it's
1762 // not being managed by ObjC reference counting, so we can delete pairs
1763 // regardless of what possible decrements or uses lie between them.
1764 bool KnownSafe = isa<Constant>(Arg) || isa<AllocaInst>(Arg);
1766 // A constant pointer can't be pointing to an object on the heap. It may
1767 // be reference-counted, but it won't be deleted.
1768 if (const LoadInst *LI = dyn_cast<LoadInst>(Arg))
1769 if (const GlobalVariable *GV =
1770 dyn_cast<GlobalVariable>(
1771 GetRCIdentityRoot(LI->getPointerOperand())))
1772 if (GV->isConstant())
1775 // Connect the dots between the top-down-collected RetainsToMove and
1776 // bottom-up-collected ReleasesToMove to form sets of related calls.
1777 NewRetains.push_back(Retain);
1778 bool PerformMoveCalls = PairUpRetainsAndReleases(
1779 BBStates, Retains, Releases, M, NewRetains, NewReleases, DeadInsts,
1780 RetainsToMove, ReleasesToMove, Arg, KnownSafe,
1781 AnyPairsCompletelyEliminated);
1783 if (PerformMoveCalls) {
1784 // Ok, everything checks out and we're all set. Let's move/delete some
1786 MoveCalls(Arg, RetainsToMove, ReleasesToMove,
1787 Retains, Releases, DeadInsts, M);
1790 // Clean up state for next retain.
1791 NewReleases.clear();
1793 RetainsToMove.clear();
1794 ReleasesToMove.clear();
1797 // Now that we're done moving everything, we can delete the newly dead
1798 // instructions, as we no longer need them as insert points.
1799 while (!DeadInsts.empty())
1800 EraseInstruction(DeadInsts.pop_back_val());
1802 return AnyPairsCompletelyEliminated;
1805 /// Weak pointer optimizations.
1806 void ObjCARCOpt::OptimizeWeakCalls(Function &F) {
1807 DEBUG(dbgs() << "\n== ObjCARCOpt::OptimizeWeakCalls ==\n");
1809 // First, do memdep-style RLE and S2L optimizations. We can't use memdep
1810 // itself because it uses AliasAnalysis and we need to do provenance
1812 for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
1813 Instruction *Inst = &*I++;
1815 DEBUG(dbgs() << "Visiting: " << *Inst << "\n");
1817 ARCInstKind Class = GetBasicARCInstKind(Inst);
1818 if (Class != ARCInstKind::LoadWeak &&
1819 Class != ARCInstKind::LoadWeakRetained)
1822 // Delete objc_loadWeak calls with no users.
1823 if (Class == ARCInstKind::LoadWeak && Inst->use_empty()) {
1824 Inst->eraseFromParent();
1828 // TODO: For now, just look for an earlier available version of this value
1829 // within the same block. Theoretically, we could do memdep-style non-local
1830 // analysis too, but that would want caching. A better approach would be to
1831 // use the technique that EarlyCSE uses.
1832 inst_iterator Current = std::prev(I);
1833 BasicBlock *CurrentBB = Current.getBasicBlockIterator();
1834 for (BasicBlock::iterator B = CurrentBB->begin(),
1835 J = Current.getInstructionIterator();
1837 Instruction *EarlierInst = &*std::prev(J);
1838 ARCInstKind EarlierClass = GetARCInstKind(EarlierInst);
1839 switch (EarlierClass) {
1840 case ARCInstKind::LoadWeak:
1841 case ARCInstKind::LoadWeakRetained: {
1842 // If this is loading from the same pointer, replace this load's value
1844 CallInst *Call = cast<CallInst>(Inst);
1845 CallInst *EarlierCall = cast<CallInst>(EarlierInst);
1846 Value *Arg = Call->getArgOperand(0);
1847 Value *EarlierArg = EarlierCall->getArgOperand(0);
1848 switch (PA.getAA()->alias(Arg, EarlierArg)) {
1851 // If the load has a builtin retain, insert a plain retain for it.
1852 if (Class == ARCInstKind::LoadWeakRetained) {
1853 Constant *Decl = EP.get(ARCRuntimeEntryPointKind::Retain);
1854 CallInst *CI = CallInst::Create(Decl, EarlierCall, "", Call);
1857 // Zap the fully redundant load.
1858 Call->replaceAllUsesWith(EarlierCall);
1859 Call->eraseFromParent();
1869 case ARCInstKind::StoreWeak:
1870 case ARCInstKind::InitWeak: {
1871 // If this is storing to the same pointer and has the same size etc.
1872 // replace this load's value with the stored value.
1873 CallInst *Call = cast<CallInst>(Inst);
1874 CallInst *EarlierCall = cast<CallInst>(EarlierInst);
1875 Value *Arg = Call->getArgOperand(0);
1876 Value *EarlierArg = EarlierCall->getArgOperand(0);
1877 switch (PA.getAA()->alias(Arg, EarlierArg)) {
1880 // If the load has a builtin retain, insert a plain retain for it.
1881 if (Class == ARCInstKind::LoadWeakRetained) {
1882 Constant *Decl = EP.get(ARCRuntimeEntryPointKind::Retain);
1883 CallInst *CI = CallInst::Create(Decl, EarlierCall, "", Call);
1886 // Zap the fully redundant load.
1887 Call->replaceAllUsesWith(EarlierCall->getArgOperand(1));
1888 Call->eraseFromParent();
1898 case ARCInstKind::MoveWeak:
1899 case ARCInstKind::CopyWeak:
1900 // TOOD: Grab the copied value.
1902 case ARCInstKind::AutoreleasepoolPush:
1903 case ARCInstKind::None:
1904 case ARCInstKind::IntrinsicUser:
1905 case ARCInstKind::User:
1906 // Weak pointers are only modified through the weak entry points
1907 // (and arbitrary calls, which could call the weak entry points).
1910 // Anything else could modify the weak pointer.
1917 // Then, for each destroyWeak with an alloca operand, check to see if
1918 // the alloca and all its users can be zapped.
1919 for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
1920 Instruction *Inst = &*I++;
1921 ARCInstKind Class = GetBasicARCInstKind(Inst);
1922 if (Class != ARCInstKind::DestroyWeak)
1925 CallInst *Call = cast<CallInst>(Inst);
1926 Value *Arg = Call->getArgOperand(0);
1927 if (AllocaInst *Alloca = dyn_cast<AllocaInst>(Arg)) {
1928 for (User *U : Alloca->users()) {
1929 const Instruction *UserInst = cast<Instruction>(U);
1930 switch (GetBasicARCInstKind(UserInst)) {
1931 case ARCInstKind::InitWeak:
1932 case ARCInstKind::StoreWeak:
1933 case ARCInstKind::DestroyWeak:
1940 for (auto UI = Alloca->user_begin(), UE = Alloca->user_end(); UI != UE;) {
1941 CallInst *UserInst = cast<CallInst>(*UI++);
1942 switch (GetBasicARCInstKind(UserInst)) {
1943 case ARCInstKind::InitWeak:
1944 case ARCInstKind::StoreWeak:
1945 // These functions return their second argument.
1946 UserInst->replaceAllUsesWith(UserInst->getArgOperand(1));
1948 case ARCInstKind::DestroyWeak:
1952 llvm_unreachable("alloca really is used!");
1954 UserInst->eraseFromParent();
1956 Alloca->eraseFromParent();
1962 /// Identify program paths which execute sequences of retains and releases which
1963 /// can be eliminated.
1964 bool ObjCARCOpt::OptimizeSequences(Function &F) {
1965 // Releases, Retains - These are used to store the results of the main flow
1966 // analysis. These use Value* as the key instead of Instruction* so that the
1967 // map stays valid when we get around to rewriting code and calls get
1968 // replaced by arguments.
1969 DenseMap<Value *, RRInfo> Releases;
1970 BlotMapVector<Value *, RRInfo> Retains;
1972 // This is used during the traversal of the function to track the
1973 // states for each identified object at each block.
1974 DenseMap<const BasicBlock *, BBState> BBStates;
1976 // Analyze the CFG of the function, and all instructions.
1977 bool NestingDetected = Visit(F, BBStates, Retains, Releases);
1980 bool AnyPairsCompletelyEliminated = PerformCodePlacement(BBStates, Retains,
1985 MultiOwnersSet.clear();
1987 return AnyPairsCompletelyEliminated && NestingDetected;
1990 /// Check if there is a dependent call earlier that does not have anything in
1991 /// between the Retain and the call that can affect the reference count of their
1992 /// shared pointer argument. Note that Retain need not be in BB.
1994 HasSafePathToPredecessorCall(const Value *Arg, Instruction *Retain,
1995 SmallPtrSetImpl<Instruction *> &DepInsts,
1996 SmallPtrSetImpl<const BasicBlock *> &Visited,
1997 ProvenanceAnalysis &PA) {
1998 FindDependencies(CanChangeRetainCount, Arg, Retain->getParent(), Retain,
1999 DepInsts, Visited, PA);
2000 if (DepInsts.size() != 1)
2003 auto *Call = dyn_cast_or_null<CallInst>(*DepInsts.begin());
2005 // Check that the pointer is the return value of the call.
2006 if (!Call || Arg != Call)
2009 // Check that the call is a regular call.
2010 ARCInstKind Class = GetBasicARCInstKind(Call);
2011 if (Class != ARCInstKind::CallOrUser && Class != ARCInstKind::Call)
2017 /// Find a dependent retain that precedes the given autorelease for which there
2018 /// is nothing in between the two instructions that can affect the ref count of
2021 FindPredecessorRetainWithSafePath(const Value *Arg, BasicBlock *BB,
2022 Instruction *Autorelease,
2023 SmallPtrSetImpl<Instruction *> &DepInsts,
2024 SmallPtrSetImpl<const BasicBlock *> &Visited,
2025 ProvenanceAnalysis &PA) {
2026 FindDependencies(CanChangeRetainCount, Arg,
2027 BB, Autorelease, DepInsts, Visited, PA);
2028 if (DepInsts.size() != 1)
2031 auto *Retain = dyn_cast_or_null<CallInst>(*DepInsts.begin());
2033 // Check that we found a retain with the same argument.
2034 if (!Retain || !IsRetain(GetBasicARCInstKind(Retain)) ||
2035 GetArgRCIdentityRoot(Retain) != Arg) {
2042 /// Look for an ``autorelease'' instruction dependent on Arg such that there are
2043 /// no instructions dependent on Arg that need a positive ref count in between
2044 /// the autorelease and the ret.
2046 FindPredecessorAutoreleaseWithSafePath(const Value *Arg, BasicBlock *BB,
2048 SmallPtrSetImpl<Instruction *> &DepInsts,
2049 SmallPtrSetImpl<const BasicBlock *> &V,
2050 ProvenanceAnalysis &PA) {
2051 FindDependencies(NeedsPositiveRetainCount, Arg,
2052 BB, Ret, DepInsts, V, PA);
2053 if (DepInsts.size() != 1)
2056 auto *Autorelease = dyn_cast_or_null<CallInst>(*DepInsts.begin());
2059 ARCInstKind AutoreleaseClass = GetBasicARCInstKind(Autorelease);
2060 if (!IsAutorelease(AutoreleaseClass))
2062 if (GetArgRCIdentityRoot(Autorelease) != Arg)
2068 /// Look for this pattern:
2070 /// %call = call i8* @something(...)
2071 /// %2 = call i8* @objc_retain(i8* %call)
2072 /// %3 = call i8* @objc_autorelease(i8* %2)
2075 /// And delete the retain and autorelease.
2076 void ObjCARCOpt::OptimizeReturns(Function &F) {
2077 if (!F.getReturnType()->isPointerTy())
2080 DEBUG(dbgs() << "\n== ObjCARCOpt::OptimizeReturns ==\n");
2082 SmallPtrSet<Instruction *, 4> DependingInstructions;
2083 SmallPtrSet<const BasicBlock *, 4> Visited;
2084 for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ++FI) {
2085 BasicBlock *BB = FI;
2086 ReturnInst *Ret = dyn_cast<ReturnInst>(&BB->back());
2088 DEBUG(dbgs() << "Visiting: " << *Ret << "\n");
2093 const Value *Arg = GetRCIdentityRoot(Ret->getOperand(0));
2095 // Look for an ``autorelease'' instruction that is a predecessor of Ret and
2096 // dependent on Arg such that there are no instructions dependent on Arg
2097 // that need a positive ref count in between the autorelease and Ret.
2098 CallInst *Autorelease =
2099 FindPredecessorAutoreleaseWithSafePath(Arg, BB, Ret,
2100 DependingInstructions, Visited,
2102 DependingInstructions.clear();
2109 FindPredecessorRetainWithSafePath(Arg, BB, Autorelease,
2110 DependingInstructions, Visited, PA);
2111 DependingInstructions.clear();
2117 // Check that there is nothing that can affect the reference count
2118 // between the retain and the call. Note that Retain need not be in BB.
2119 bool HasSafePathToCall = HasSafePathToPredecessorCall(Arg, Retain,
2120 DependingInstructions,
2122 DependingInstructions.clear();
2125 if (!HasSafePathToCall)
2128 // If so, we can zap the retain and autorelease.
2131 DEBUG(dbgs() << "Erasing: " << *Retain << "\nErasing: "
2132 << *Autorelease << "\n");
2133 EraseInstruction(Retain);
2134 EraseInstruction(Autorelease);
2140 ObjCARCOpt::GatherStatistics(Function &F, bool AfterOptimization) {
2141 llvm::Statistic &NumRetains =
2142 AfterOptimization? NumRetainsAfterOpt : NumRetainsBeforeOpt;
2143 llvm::Statistic &NumReleases =
2144 AfterOptimization? NumReleasesAfterOpt : NumReleasesBeforeOpt;
2146 for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
2147 Instruction *Inst = &*I++;
2148 switch (GetBasicARCInstKind(Inst)) {
2151 case ARCInstKind::Retain:
2154 case ARCInstKind::Release:
2162 bool ObjCARCOpt::doInitialization(Module &M) {
2166 // If nothing in the Module uses ARC, don't do anything.
2167 Run = ModuleHasARC(M);
2171 // Intuitively, objc_retain and others are nocapture, however in practice
2172 // they are not, because they return their argument value. And objc_release
2173 // calls finalizers which can have arbitrary side effects.
2174 MDKindCache.init(&M);
2176 // Initialize our runtime entry point cache.
2182 bool ObjCARCOpt::runOnFunction(Function &F) {
2186 // If nothing in the Module uses ARC, don't do anything.
2192 DEBUG(dbgs() << "<<< ObjCARCOpt: Visiting Function: " << F.getName() << " >>>"
2195 PA.setAA(&getAnalysis<AliasAnalysis>());
2198 if (AreStatisticsEnabled()) {
2199 GatherStatistics(F, false);
2203 // This pass performs several distinct transformations. As a compile-time aid
2204 // when compiling code that isn't ObjC, skip these if the relevant ObjC
2205 // library functions aren't declared.
2207 // Preliminary optimizations. This also computes UsedInThisFunction.
2208 OptimizeIndividualCalls(F);
2210 // Optimizations for weak pointers.
2211 if (UsedInThisFunction & ((1 << unsigned(ARCInstKind::LoadWeak)) |
2212 (1 << unsigned(ARCInstKind::LoadWeakRetained)) |
2213 (1 << unsigned(ARCInstKind::StoreWeak)) |
2214 (1 << unsigned(ARCInstKind::InitWeak)) |
2215 (1 << unsigned(ARCInstKind::CopyWeak)) |
2216 (1 << unsigned(ARCInstKind::MoveWeak)) |
2217 (1 << unsigned(ARCInstKind::DestroyWeak))))
2218 OptimizeWeakCalls(F);
2220 // Optimizations for retain+release pairs.
2221 if (UsedInThisFunction & ((1 << unsigned(ARCInstKind::Retain)) |
2222 (1 << unsigned(ARCInstKind::RetainRV)) |
2223 (1 << unsigned(ARCInstKind::RetainBlock))))
2224 if (UsedInThisFunction & (1 << unsigned(ARCInstKind::Release)))
2225 // Run OptimizeSequences until it either stops making changes or
2226 // no retain+release pair nesting is detected.
2227 while (OptimizeSequences(F)) {}
2229 // Optimizations if objc_autorelease is used.
2230 if (UsedInThisFunction & ((1 << unsigned(ARCInstKind::Autorelease)) |
2231 (1 << unsigned(ARCInstKind::AutoreleaseRV))))
2234 // Gather statistics after optimization.
2236 if (AreStatisticsEnabled()) {
2237 GatherStatistics(F, true);
2241 DEBUG(dbgs() << "\n");
2246 void ObjCARCOpt::releaseMemory() {