LLVM 23.0.0git
ObjCARCOpts.cpp
Go to the documentation of this file.
1//===- ObjCARCOpts.cpp - ObjC ARC Optimization ----------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
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
28#include "BlotMapVector.h"
29#include "DependencyAnalysis.h"
30#include "ObjCARC.h"
31#include "ProvenanceAnalysis.h"
32#include "PtrState.h"
33#include "llvm/ADT/DenseMap.h"
34#include "llvm/ADT/STLExtras.h"
37#include "llvm/ADT/Statistic.h"
43#include "llvm/IR/BasicBlock.h"
44#include "llvm/IR/CFG.h"
45#include "llvm/IR/Constant.h"
46#include "llvm/IR/Constants.h"
49#include "llvm/IR/Function.h"
52#include "llvm/IR/InstrTypes.h"
53#include "llvm/IR/Instruction.h"
55#include "llvm/IR/LLVMContext.h"
56#include "llvm/IR/Metadata.h"
57#include "llvm/IR/Type.h"
58#include "llvm/IR/User.h"
59#include "llvm/IR/Value.h"
63#include "llvm/Support/Debug.h"
67#include <cassert>
68#include <iterator>
69#include <utility>
70
71using namespace llvm;
72using namespace llvm::objcarc;
73
74#define DEBUG_TYPE "objc-arc-opts"
75
76static cl::opt<unsigned> MaxPtrStates("arc-opt-max-ptr-states",
78 cl::desc("Maximum number of ptr states the optimizer keeps track of"),
79 cl::init(4095));
80
81/// \defgroup ARCUtilities Utility declarations/definitions specific to ARC.
82/// @{
83
84/// This is similar to GetRCIdentityRoot but it stops as soon
85/// as it finds a value with multiple uses.
86static const Value *FindSingleUseIdentifiedObject(const Value *Arg) {
87 // ConstantData (like ConstantPointerNull and UndefValue) is used across
88 // modules. It's never a single-use value.
89 if (isa<ConstantData>(Arg))
90 return nullptr;
91
92 if (Arg->hasOneUse()) {
93 if (const BitCastInst *BC = dyn_cast<BitCastInst>(Arg))
94 return FindSingleUseIdentifiedObject(BC->getOperand(0));
96 if (GEP->hasAllZeroIndices())
97 return FindSingleUseIdentifiedObject(GEP->getPointerOperand());
100 cast<CallInst>(Arg)->getArgOperand(0));
101 if (!IsObjCIdentifiedObject(Arg))
102 return nullptr;
103 return Arg;
104 }
105
106 // If we found an identifiable object but it has multiple uses, but they are
107 // trivial uses, we can still consider this to be a single-use value.
108 if (IsObjCIdentifiedObject(Arg)) {
109 for (const User *U : Arg->users())
110 if (!U->use_empty() || GetRCIdentityRoot(U) != Arg)
111 return nullptr;
112
113 return Arg;
114 }
115
116 return nullptr;
117}
118
119/// @}
120///
121/// \defgroup ARCOpt ARC Optimization.
122/// @{
123
124// TODO: On code like this:
125//
126// objc_retain(%x)
127// stuff_that_cannot_release()
128// objc_autorelease(%x)
129// stuff_that_cannot_release()
130// objc_retain(%x)
131// stuff_that_cannot_release()
132// objc_autorelease(%x)
133//
134// The second retain and autorelease can be deleted.
135
136// TODO: Autorelease calls followed by objc_autoreleasePoolPop calls (perhaps in
137// ObjC++ code after inlining) can be turned into plain release calls.
138
139// TODO: Critical-edge splitting. If the optimial insertion point is
140// a critical edge, the current algorithm has to fail, because it doesn't
141// know how to split edges. It should be possible to make the optimizer
142// think in terms of edges, rather than blocks, and then split critical
143// edges on demand.
144
145// TODO: OptimizeSequences could generalized to be Interprocedural.
146
147// TODO: Recognize that a bunch of other objc runtime calls have
148// non-escaping arguments and non-releasing arguments, and may be
149// non-autoreleasing.
150
151// TODO: Sink autorelease calls as far as possible. Unfortunately we
152// usually can't sink them past other calls, which would be the main
153// case where it would be useful.
154
155// TODO: The pointer returned from objc_loadWeakRetained is retained.
156
157// TODO: Delete release+retain pairs (rare).
158
159STATISTIC(NumNoops, "Number of no-op objc calls eliminated");
160STATISTIC(NumPartialNoops, "Number of partially no-op objc calls eliminated");
161STATISTIC(NumAutoreleases,"Number of autoreleases converted to releases");
162STATISTIC(NumRets, "Number of return value forwarding "
163 "retain+autoreleases eliminated");
164STATISTIC(NumRRs, "Number of retain+release paths eliminated");
165STATISTIC(NumPeeps, "Number of calls peephole-optimized");
166#ifndef NDEBUG
167STATISTIC(NumRetainsBeforeOpt,
168 "Number of retains before optimization");
169STATISTIC(NumReleasesBeforeOpt,
170 "Number of releases before optimization");
171STATISTIC(NumRetainsAfterOpt,
172 "Number of retains after optimization");
173STATISTIC(NumReleasesAfterOpt,
174 "Number of releases after optimization");
175#endif
176
177namespace {
178
179 /// Per-BasicBlock state.
180 class BBState {
181 /// The number of unique control paths from the entry which can reach this
182 /// block.
183 unsigned TopDownPathCount = 0;
184
185 /// The number of unique control paths to exits from this block.
186 unsigned BottomUpPathCount = 0;
187
188 /// The top-down traversal uses this to record information known about a
189 /// pointer at the bottom of each block.
191
192 /// The bottom-up traversal uses this to record information known about a
193 /// pointer at the top of each block.
195
196 /// Effective predecessors of the current block ignoring ignorable edges and
197 /// ignored backedges.
199
200 /// Effective successors of the current block ignoring ignorable edges and
201 /// ignored backedges.
203
204 public:
205 static const unsigned OverflowOccurredValue;
206
207 BBState() = default;
208
209 using top_down_ptr_iterator = decltype(PerPtrTopDown)::iterator;
210 using const_top_down_ptr_iterator = decltype(PerPtrTopDown)::const_iterator;
211
212 top_down_ptr_iterator top_down_ptr_begin() { return PerPtrTopDown.begin(); }
213 top_down_ptr_iterator top_down_ptr_end() { return PerPtrTopDown.end(); }
214 const_top_down_ptr_iterator top_down_ptr_begin() const {
215 return PerPtrTopDown.begin();
216 }
217 const_top_down_ptr_iterator top_down_ptr_end() const {
218 return PerPtrTopDown.end();
219 }
220 bool hasTopDownPtrs() const {
221 return !PerPtrTopDown.empty();
222 }
223
224 unsigned top_down_ptr_list_size() const {
225 return std::distance(top_down_ptr_begin(), top_down_ptr_end());
226 }
227
228 using bottom_up_ptr_iterator = decltype(PerPtrBottomUp)::iterator;
229 using const_bottom_up_ptr_iterator =
230 decltype(PerPtrBottomUp)::const_iterator;
231
232 bottom_up_ptr_iterator bottom_up_ptr_begin() {
233 return PerPtrBottomUp.begin();
234 }
235 bottom_up_ptr_iterator bottom_up_ptr_end() { return PerPtrBottomUp.end(); }
236 const_bottom_up_ptr_iterator bottom_up_ptr_begin() const {
237 return PerPtrBottomUp.begin();
238 }
239 const_bottom_up_ptr_iterator bottom_up_ptr_end() const {
240 return PerPtrBottomUp.end();
241 }
242 bool hasBottomUpPtrs() const {
243 return !PerPtrBottomUp.empty();
244 }
245
246 unsigned bottom_up_ptr_list_size() const {
247 return std::distance(bottom_up_ptr_begin(), bottom_up_ptr_end());
248 }
249
250 /// Mark this block as being an entry block, which has one path from the
251 /// entry by definition.
252 void SetAsEntry() { TopDownPathCount = 1; }
253
254 /// Mark this block as being an exit block, which has one path to an exit by
255 /// definition.
256 void SetAsExit() { BottomUpPathCount = 1; }
257
258 /// Attempt to find the PtrState object describing the top down state for
259 /// pointer Arg. Return a new initialized PtrState describing the top down
260 /// state for Arg if we do not find one.
261 TopDownPtrState &getPtrTopDownState(const Value *Arg) {
262 return PerPtrTopDown[Arg];
263 }
264
265 /// Attempt to find the PtrState object describing the bottom up state for
266 /// pointer Arg. Return a new initialized PtrState describing the bottom up
267 /// state for Arg if we do not find one.
268 BottomUpPtrState &getPtrBottomUpState(const Value *Arg) {
269 return PerPtrBottomUp[Arg];
270 }
271
272 /// Attempt to find the PtrState object describing the bottom up state for
273 /// pointer Arg.
274 bottom_up_ptr_iterator findPtrBottomUpState(const Value *Arg) {
275 return PerPtrBottomUp.find(Arg);
276 }
277
278 void clearBottomUpPointers() {
279 PerPtrBottomUp.clear();
280 }
281
282 void clearTopDownPointers() {
283 PerPtrTopDown.clear();
284 }
285
286 void InitFromPred(const BBState &Other);
287 void InitFromSucc(const BBState &Other);
288 void MergePred(const BBState &Other);
289 void MergeSucc(const BBState &Other);
290
291 /// Compute the number of possible unique paths from an entry to an exit
292 /// which pass through this block. This is only valid after both the
293 /// top-down and bottom-up traversals are complete.
294 ///
295 /// Returns true if overflow occurred. Returns false if overflow did not
296 /// occur.
297 bool GetAllPathCountWithOverflow(unsigned &PathCount) const {
298 if (TopDownPathCount == OverflowOccurredValue ||
299 BottomUpPathCount == OverflowOccurredValue)
300 return true;
301 unsigned long long Product =
302 (unsigned long long)TopDownPathCount*BottomUpPathCount;
303 // Overflow occurred if any of the upper bits of Product are set or if all
304 // the lower bits of Product are all set.
305 return (Product >> 32) ||
306 ((PathCount = Product) == OverflowOccurredValue);
307 }
308
309 // Specialized CFG utilities.
311
312 edge_iterator pred_begin() const { return Preds.begin(); }
313 edge_iterator pred_end() const { return Preds.end(); }
314 edge_iterator succ_begin() const { return Succs.begin(); }
315 edge_iterator succ_end() const { return Succs.end(); }
316
317 void addSucc(BasicBlock *Succ) { Succs.push_back(Succ); }
318 void addPred(BasicBlock *Pred) { Preds.push_back(Pred); }
319
320 bool isExit() const { return Succs.empty(); }
321 };
322
323} // end anonymous namespace
324
325const unsigned BBState::OverflowOccurredValue = 0xffffffff;
326
327namespace llvm {
328
329[[maybe_unused]] raw_ostream &operator<<(raw_ostream &OS, BBState &BBState);
330
331} // end namespace llvm
332
333void BBState::InitFromPred(const BBState &Other) {
334 PerPtrTopDown = Other.PerPtrTopDown;
335 TopDownPathCount = Other.TopDownPathCount;
336}
337
338void BBState::InitFromSucc(const BBState &Other) {
339 PerPtrBottomUp = Other.PerPtrBottomUp;
340 BottomUpPathCount = Other.BottomUpPathCount;
341}
342
343/// The top-down traversal uses this to merge information about predecessors to
344/// form the initial state for a new block.
345void BBState::MergePred(const BBState &Other) {
346 if (TopDownPathCount == OverflowOccurredValue)
347 return;
348
349 // Other.TopDownPathCount can be 0, in which case it is either dead or a
350 // loop backedge. Loop backedges are special.
351 TopDownPathCount += Other.TopDownPathCount;
352
353 // In order to be consistent, we clear the top down pointers when by adding
354 // TopDownPathCount becomes OverflowOccurredValue even though "true" overflow
355 // has not occurred.
356 if (TopDownPathCount == OverflowOccurredValue) {
357 clearTopDownPointers();
358 return;
359 }
360
361 // Check for overflow. If we have overflow, fall back to conservative
362 // behavior.
363 if (TopDownPathCount < Other.TopDownPathCount) {
364 TopDownPathCount = OverflowOccurredValue;
365 clearTopDownPointers();
366 return;
367 }
368
369 // For each entry in the other set, if our set has an entry with the same key,
370 // merge the entries. Otherwise, copy the entry and merge it with an empty
371 // entry.
372 for (auto MI = Other.top_down_ptr_begin(), ME = Other.top_down_ptr_end();
373 MI != ME; ++MI) {
374 auto Pair = PerPtrTopDown.insert(*MI);
375 Pair.first->second.Merge(Pair.second ? TopDownPtrState() : MI->second,
376 /*TopDown=*/true);
377 }
378
379 // For each entry in our set, if the other set doesn't have an entry with the
380 // same key, force it to merge with an empty entry.
381 for (auto MI = top_down_ptr_begin(), ME = top_down_ptr_end(); MI != ME; ++MI)
382 if (Other.PerPtrTopDown.find(MI->first) == Other.PerPtrTopDown.end())
383 MI->second.Merge(TopDownPtrState(), /*TopDown=*/true);
384}
385
386/// The bottom-up traversal uses this to merge information about successors to
387/// form the initial state for a new block.
388void BBState::MergeSucc(const BBState &Other) {
389 if (BottomUpPathCount == OverflowOccurredValue)
390 return;
391
392 // Other.BottomUpPathCount can be 0, in which case it is either dead or a
393 // loop backedge. Loop backedges are special.
394 BottomUpPathCount += Other.BottomUpPathCount;
395
396 // In order to be consistent, we clear the top down pointers when by adding
397 // BottomUpPathCount becomes OverflowOccurredValue even though "true" overflow
398 // has not occurred.
399 if (BottomUpPathCount == OverflowOccurredValue) {
400 clearBottomUpPointers();
401 return;
402 }
403
404 // Check for overflow. If we have overflow, fall back to conservative
405 // behavior.
406 if (BottomUpPathCount < Other.BottomUpPathCount) {
407 BottomUpPathCount = OverflowOccurredValue;
408 clearBottomUpPointers();
409 return;
410 }
411
412 // For each entry in the other set, if our set has an entry with the
413 // same key, merge the entries. Otherwise, copy the entry and merge
414 // it with an empty entry.
415 for (auto MI = Other.bottom_up_ptr_begin(), ME = Other.bottom_up_ptr_end();
416 MI != ME; ++MI) {
417 auto Pair = PerPtrBottomUp.insert(*MI);
418 Pair.first->second.Merge(Pair.second ? BottomUpPtrState() : MI->second,
419 /*TopDown=*/false);
420 }
421
422 // For each entry in our set, if the other set doesn't have an entry
423 // with the same key, force it to merge with an empty entry.
424 for (auto MI = bottom_up_ptr_begin(), ME = bottom_up_ptr_end(); MI != ME;
425 ++MI)
426 if (Other.PerPtrBottomUp.find(MI->first) == Other.PerPtrBottomUp.end())
427 MI->second.Merge(BottomUpPtrState(), /*TopDown=*/false);
428}
429
431 // Dump the pointers we are tracking.
432 OS << " TopDown State:\n";
433 if (!BBInfo.hasTopDownPtrs()) {
434 LLVM_DEBUG(dbgs() << " NONE!\n");
435 } else {
436 for (auto I = BBInfo.top_down_ptr_begin(), E = BBInfo.top_down_ptr_end();
437 I != E; ++I) {
438 const PtrState &P = I->second;
439 OS << " Ptr: " << *I->first
440 << "\n KnownSafe: " << (P.IsKnownSafe()?"true":"false")
441 << "\n ImpreciseRelease: "
442 << (P.IsTrackingImpreciseReleases()?"true":"false") << "\n"
443 << " HasCFGHazards: "
444 << (P.IsCFGHazardAfflicted()?"true":"false") << "\n"
445 << " KnownPositive: "
446 << (P.HasKnownPositiveRefCount()?"true":"false") << "\n"
447 << " Seq: "
448 << P.GetSeq() << "\n";
449 }
450 }
451
452 OS << " BottomUp State:\n";
453 if (!BBInfo.hasBottomUpPtrs()) {
454 LLVM_DEBUG(dbgs() << " NONE!\n");
455 } else {
456 for (auto I = BBInfo.bottom_up_ptr_begin(), E = BBInfo.bottom_up_ptr_end();
457 I != E; ++I) {
458 const PtrState &P = I->second;
459 OS << " Ptr: " << *I->first
460 << "\n KnownSafe: " << (P.IsKnownSafe()?"true":"false")
461 << "\n ImpreciseRelease: "
462 << (P.IsTrackingImpreciseReleases()?"true":"false") << "\n"
463 << " HasCFGHazards: "
464 << (P.IsCFGHazardAfflicted()?"true":"false") << "\n"
465 << " KnownPositive: "
466 << (P.HasKnownPositiveRefCount()?"true":"false") << "\n"
467 << " Seq: "
468 << P.GetSeq() << "\n";
469 }
470 }
471
472 return OS;
473}
474
475namespace {
476
477 /// The main ARC optimization pass.
478class ObjCARCOpt {
479 bool Changed = false;
480 bool CFGChanged = false;
482
483 /// A cache of references to runtime entry point constants.
485
486 /// A cache of MDKinds that can be passed into other functions to propagate
487 /// MDKind identifiers.
488 ARCMDKindCache MDKindCache;
489
490 BundledRetainClaimRVs *BundledInsts = nullptr;
491
492 /// A flag indicating whether the optimization that removes or moves
493 /// retain/release pairs should be performed.
494 bool DisableRetainReleasePairing = false;
495
496 /// Flags which determine whether each of the interesting runtime functions
497 /// is in fact used in the current function.
498 unsigned UsedInThisFunction;
499
501
502 bool OptimizeRetainRVCall(Function &F, Instruction *RetainRV);
503 void OptimizeAutoreleaseRVCall(Function &F, Instruction *AutoreleaseRV,
504 ARCInstKind &Class);
505 void OptimizeIndividualCalls(Function &F);
506
507 /// Optimize an individual call, optionally passing the
508 /// GetArgRCIdentityRoot if it has already been computed.
509 void OptimizeIndividualCallImpl(Function &F, Instruction *Inst,
510 ARCInstKind Class, const Value *Arg);
511
512 /// Try to optimize an AutoreleaseRV with a RetainRV or UnsafeClaimRV. If the
513 /// optimization occurs, returns true to indicate that the caller should
514 /// assume the instructions are dead.
515 bool OptimizeInlinedAutoreleaseRVCall(Function &F, Instruction *Inst,
516 const Value *&Arg, ARCInstKind Class,
518 const Value *&AutoreleaseRVArg);
519
520 void CheckForCFGHazards(const BasicBlock *BB,
522 BBState &MyStates) const;
523 bool VisitInstructionBottomUp(Instruction *Inst, BasicBlock *BB,
525 BBState &MyStates);
526 bool VisitBottomUp(BasicBlock *BB,
529 bool VisitInstructionTopDown(
530 Instruction *Inst, DenseMap<Value *, RRInfo> &Releases, BBState &MyStates,
532 &ReleaseInsertPtToRCIdentityRoots);
533 bool VisitTopDown(
537 &ReleaseInsertPtToRCIdentityRoots);
538 bool Visit(Function &F, DenseMap<const BasicBlock *, BBState> &BBStates,
540 DenseMap<Value *, RRInfo> &Releases);
541
542 void MoveCalls(Value *Arg, RRInfo &RetainsToMove, RRInfo &ReleasesToMove,
546
547 bool PairUpRetainsAndReleases(DenseMap<const BasicBlock *, BBState> &BBStates,
549 DenseMap<Value *, RRInfo> &Releases, Module *M,
552 RRInfo &RetainsToMove, RRInfo &ReleasesToMove,
553 Value *Arg, bool KnownSafe,
554 bool &AnyPairsCompletelyEliminated);
555
556 bool PerformCodePlacement(DenseMap<const BasicBlock *, BBState> &BBStates,
558 DenseMap<Value *, RRInfo> &Releases, Module *M);
559
560 void OptimizeWeakCalls(Function &F);
561
562 bool OptimizeSequences(Function &F);
563
564 void OptimizeReturns(Function &F);
565
566 void OptimizeAutoreleasePools(Function &F);
567
568 template <typename PredicateT>
569 static void cloneOpBundlesIf(CallBase *CI,
572 for (unsigned I = 0, E = CI->getNumOperandBundles(); I != E; ++I) {
574 if (Predicate(B))
575 OpBundles.emplace_back(B);
576 }
577 }
578
579 void addOpBundleForFunclet(BasicBlock *BB,
580 SmallVectorImpl<OperandBundleDef> &OpBundles) {
581 if (!BlockEHColors.empty()) {
582 const ColorVector &CV = BlockEHColors.find(BB)->second;
583 assert(CV.size() > 0 && "Uncolored block");
584 for (BasicBlock *EHPadBB : CV)
585 if (auto *EHPad =
586 dyn_cast<FuncletPadInst>(EHPadBB->getFirstNonPHIIt())) {
587 OpBundles.emplace_back("funclet", EHPad);
588 return;
589 }
590 }
591 }
592
593#ifndef NDEBUG
594 void GatherStatistics(Function &F, bool AfterOptimization = false);
595#endif
596
597 public:
598 void init(Function &F);
599 bool run(Function &F, AAResults &AA);
600 bool hasCFGChanged() const { return CFGChanged; }
601};
602} // end anonymous namespace
603
604/// Turn objc_retainAutoreleasedReturnValue into objc_retain if the operand is
605/// not a return value.
606bool
607ObjCARCOpt::OptimizeRetainRVCall(Function &F, Instruction *RetainRV) {
608 // Check for the argument being from an immediately preceding call or invoke.
609 const Value *Arg = GetArgRCIdentityRoot(RetainRV);
610 if (const Instruction *Call = dyn_cast<CallBase>(Arg)) {
611 if (Call->getParent() == RetainRV->getParent()) {
613 ++I;
614 while (IsNoopInstruction(&*I))
615 ++I;
616 if (&*I == RetainRV)
617 return false;
618 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(Call)) {
619 BasicBlock *RetainRVParent = RetainRV->getParent();
620 if (II->getNormalDest() == RetainRVParent) {
621 BasicBlock::const_iterator I = RetainRVParent->begin();
622 while (IsNoopInstruction(&*I))
623 ++I;
624 if (&*I == RetainRV)
625 return false;
626 }
627 }
628 }
629
630 assert(!BundledInsts->contains(RetainRV) &&
631 "a bundled retainRV's argument should be a call");
632
633 // Turn it to a plain objc_retain.
634 Changed = true;
635 ++NumPeeps;
636
637 LLVM_DEBUG(dbgs() << "Transforming objc_retainAutoreleasedReturnValue => "
638 "objc_retain since the operand is not a return value.\n"
639 "Old = "
640 << *RetainRV << "\n");
641
642 Function *NewDecl = EP.get(ARCRuntimeEntryPointKind::Retain);
643 cast<CallInst>(RetainRV)->setCalledFunction(NewDecl);
644
645 LLVM_DEBUG(dbgs() << "New = " << *RetainRV << "\n");
646
647 return false;
648}
649
650bool ObjCARCOpt::OptimizeInlinedAutoreleaseRVCall(
651 Function &F, Instruction *Inst, const Value *&Arg, ARCInstKind Class,
652 Instruction *AutoreleaseRV, const Value *&AutoreleaseRVArg) {
653 if (BundledInsts->contains(Inst))
654 return false;
655
656 // Must be in the same basic block.
657 assert(Inst->getParent() == AutoreleaseRV->getParent());
658
659 // Must operate on the same root.
660 Arg = GetArgRCIdentityRoot(Inst);
661 AutoreleaseRVArg = GetArgRCIdentityRoot(AutoreleaseRV);
662 if (Arg != AutoreleaseRVArg) {
663 // If there isn't an exact match, check if we have equivalent PHIs.
664 const PHINode *PN = dyn_cast<PHINode>(Arg);
665 if (!PN)
666 return false;
667
669 getEquivalentPHIs(*PN, ArgUsers);
670 if (!llvm::is_contained(ArgUsers, AutoreleaseRVArg))
671 return false;
672 }
673
674 // Okay, this is a match. Merge them.
675 ++NumPeeps;
676 LLVM_DEBUG(dbgs() << "Found inlined objc_autoreleaseReturnValue '"
677 << *AutoreleaseRV << "' paired with '" << *Inst << "'\n");
678
679 // Delete the RV pair, starting with the AutoreleaseRV.
680 AutoreleaseRV->replaceAllUsesWith(
681 cast<CallInst>(AutoreleaseRV)->getArgOperand(0));
682 Changed = true;
684 if (Class == ARCInstKind::RetainRV) {
685 // AutoreleaseRV and RetainRV cancel out. Delete the RetainRV.
686 Inst->replaceAllUsesWith(cast<CallInst>(Inst)->getArgOperand(0));
687 EraseInstruction(Inst);
688 return true;
689 }
690
691 // UnsafeClaimRV is a frontend peephole for RetainRV + Release. Since the
692 // AutoreleaseRV and RetainRV cancel out, replace UnsafeClaimRV with Release.
693 assert(Class == ARCInstKind::UnsafeClaimRV);
694 Value *CallArg = cast<CallInst>(Inst)->getArgOperand(0);
695 CallInst *Release =
696 CallInst::Create(EP.get(ARCRuntimeEntryPointKind::Release), CallArg, "",
697 Inst->getIterator());
698 assert(IsAlwaysTail(ARCInstKind::UnsafeClaimRV) &&
699 "Expected UnsafeClaimRV to be safe to tail call");
700 Release->setTailCall();
701 Inst->replaceAllUsesWith(CallArg);
702 EraseInstruction(Inst);
703
704 // Run the normal optimizations on Release.
705 OptimizeIndividualCallImpl(F, Release, ARCInstKind::Release, Arg);
706 return true;
707}
708
709/// Turn objc_autoreleaseReturnValue into objc_autorelease if the result is not
710/// used as a return value.
711void ObjCARCOpt::OptimizeAutoreleaseRVCall(Function &F,
712 Instruction *AutoreleaseRV,
713 ARCInstKind &Class) {
714 // Check for a return of the pointer value.
716
717 // If the argument is ConstantPointerNull or UndefValue, its other users
718 // aren't actually interesting to look at.
719 if (isa<ConstantData>(Ptr))
720 return;
721
722 SmallVector<const Value *, 2> Users;
723 Users.push_back(Ptr);
724
725 // Add PHIs that are equivalent to Ptr to Users.
726 if (const PHINode *PN = dyn_cast<PHINode>(Ptr))
728
729 do {
730 Ptr = Users.pop_back_val();
731 for (const User *U : Ptr->users()) {
732 if (isa<ReturnInst>(U) || GetBasicARCInstKind(U) == ARCInstKind::RetainRV)
733 return;
734 if (isa<BitCastInst>(U))
735 Users.push_back(U);
736 }
737 } while (!Users.empty());
738
739 Changed = true;
740 ++NumPeeps;
741
743 dbgs() << "Transforming objc_autoreleaseReturnValue => "
744 "objc_autorelease since its operand is not used as a return "
745 "value.\n"
746 "Old = "
747 << *AutoreleaseRV << "\n");
748
749 CallInst *AutoreleaseRVCI = cast<CallInst>(AutoreleaseRV);
750 Function *NewDecl = EP.get(ARCRuntimeEntryPointKind::Autorelease);
751 AutoreleaseRVCI->setCalledFunction(NewDecl);
752 AutoreleaseRVCI->setTailCall(false); // Never tail call objc_autorelease.
753 Class = ARCInstKind::Autorelease;
754
755 LLVM_DEBUG(dbgs() << "New: " << *AutoreleaseRV << "\n");
756}
757
758/// Visit each call, one at a time, and make simplifications without doing any
759/// additional analysis.
760void ObjCARCOpt::OptimizeIndividualCalls(Function &F) {
761 LLVM_DEBUG(dbgs() << "\n== ObjCARCOpt::OptimizeIndividualCalls ==\n");
762 // Reset all the flags in preparation for recomputing them.
763 UsedInThisFunction = 0;
764
765 // Store any delayed AutoreleaseRV intrinsics, so they can be easily paired
766 // with RetainRV and UnsafeClaimRV.
767 Instruction *DelayedAutoreleaseRV = nullptr;
768 const Value *DelayedAutoreleaseRVArg = nullptr;
769 auto setDelayedAutoreleaseRV = [&](Instruction *AutoreleaseRV) {
770 assert(!DelayedAutoreleaseRV || !AutoreleaseRV);
771 DelayedAutoreleaseRV = AutoreleaseRV;
772 DelayedAutoreleaseRVArg = nullptr;
773 };
774 auto optimizeDelayedAutoreleaseRV = [&]() {
775 if (!DelayedAutoreleaseRV)
776 return;
777 OptimizeIndividualCallImpl(F, DelayedAutoreleaseRV,
778 ARCInstKind::AutoreleaseRV,
779 DelayedAutoreleaseRVArg);
780 setDelayedAutoreleaseRV(nullptr);
781 };
782 auto shouldDelayAutoreleaseRV = [&](Instruction *NonARCInst) {
783 // Nothing to delay, but we may as well skip the logic below.
784 if (!DelayedAutoreleaseRV)
785 return true;
786
787 // If we hit the end of the basic block we're not going to find an RV-pair.
788 // Stop delaying.
789 if (NonARCInst->isTerminator())
790 return false;
791
792 // Given the frontend rules for emitting AutoreleaseRV, RetainRV, and
793 // UnsafeClaimRV, it's probably safe to skip over even opaque function calls
794 // here since OptimizeInlinedAutoreleaseRVCall will confirm that they
795 // have the same RCIdentityRoot. However, what really matters is
796 // skipping instructions or intrinsics that the inliner could leave behind;
797 // be conservative for now and don't skip over opaque calls, which could
798 // potentially include other ARC calls.
799 auto *CB = dyn_cast<CallBase>(NonARCInst);
800 if (!CB)
801 return true;
802 return CB->getIntrinsicID() != Intrinsic::not_intrinsic;
803 };
804
805 // Visit all objc_* calls in F.
806 for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
807 Instruction *Inst = &*I++;
808
809 if (auto *CI = dyn_cast<CallInst>(Inst))
811 BundledInsts->insertRVCall(I->getIterator(), CI);
812 Changed = true;
813 }
814
816
817 // Skip this loop if this instruction isn't itself an ARC intrinsic.
818 const Value *Arg = nullptr;
819 switch (Class) {
820 default:
821 optimizeDelayedAutoreleaseRV();
822 break;
823 case ARCInstKind::CallOrUser:
824 case ARCInstKind::User:
825 case ARCInstKind::None:
826 // This is a non-ARC instruction. If we're delaying an AutoreleaseRV,
827 // check if it's safe to skip over it; if not, optimize the AutoreleaseRV
828 // now.
829 if (!shouldDelayAutoreleaseRV(Inst))
830 optimizeDelayedAutoreleaseRV();
831 continue;
832 case ARCInstKind::AutoreleaseRV:
833 optimizeDelayedAutoreleaseRV();
834 setDelayedAutoreleaseRV(Inst);
835 continue;
836 case ARCInstKind::RetainRV:
837 case ARCInstKind::UnsafeClaimRV:
838 if (DelayedAutoreleaseRV) {
839 // We have a potential RV pair. Check if they cancel out.
840 if (OptimizeInlinedAutoreleaseRVCall(F, Inst, Arg, Class,
841 DelayedAutoreleaseRV,
842 DelayedAutoreleaseRVArg)) {
843 setDelayedAutoreleaseRV(nullptr);
844 continue;
845 }
846 optimizeDelayedAutoreleaseRV();
847 }
848 break;
849 }
850
851 OptimizeIndividualCallImpl(F, Inst, Class, Arg);
852 }
853
854 // Catch the final delayed AutoreleaseRV.
855 optimizeDelayedAutoreleaseRV();
856}
857
858/// This function returns true if the value is inert. An ObjC ARC runtime call
859/// taking an inert operand can be safely deleted.
860static bool isInertARCValue(Value *V, SmallPtrSet<Value *, 1> &VisitedPhis) {
861 V = V->stripPointerCasts();
862
863 if (IsNullOrUndef(V))
864 return true;
865
866 // See if this is a global attribute annotated with an 'objc_arc_inert'.
867 if (auto *GV = dyn_cast<GlobalVariable>(V))
868 if (GV->hasAttribute("objc_arc_inert"))
869 return true;
870
871 if (auto PN = dyn_cast<PHINode>(V)) {
872 // Ignore this phi if it has already been discovered.
873 if (!VisitedPhis.insert(PN).second)
874 return true;
875 // Look through phis's operands.
876 for (Value *Opnd : PN->incoming_values())
877 if (!isInertARCValue(Opnd, VisitedPhis))
878 return false;
879 return true;
880 }
881
882 return false;
883}
884
885void ObjCARCOpt::OptimizeIndividualCallImpl(Function &F, Instruction *Inst,
886 ARCInstKind Class,
887 const Value *Arg) {
888 LLVM_DEBUG(dbgs() << "Visiting: Class: " << Class << "; " << *Inst << "\n");
889
890 // We can delete this call if it takes an inert value.
891 SmallPtrSet<Value *, 1> VisitedPhis;
892
893 if (BundledInsts->contains(Inst)) {
894 UsedInThisFunction |= 1 << unsigned(Class);
895 return;
896 }
897
898 if (IsNoopOnGlobal(Class))
899 if (isInertARCValue(Inst->getOperand(0), VisitedPhis)) {
900 if (!Inst->getType()->isVoidTy())
901 Inst->replaceAllUsesWith(Inst->getOperand(0));
902 Inst->eraseFromParent();
903 Changed = true;
904 return;
905 }
906
907 switch (Class) {
908 default:
909 break;
910
911 // Delete no-op casts. These function calls have special semantics, but
912 // the semantics are entirely implemented via lowering in the front-end,
913 // so by the time they reach the optimizer, they are just no-op calls
914 // which return their argument.
915 //
916 // There are gray areas here, as the ability to cast reference-counted
917 // pointers to raw void* and back allows code to break ARC assumptions,
918 // however these are currently considered to be unimportant.
919 case ARCInstKind::NoopCast:
920 Changed = true;
921 ++NumNoops;
922 LLVM_DEBUG(dbgs() << "Erasing no-op cast: " << *Inst << "\n");
923 EraseInstruction(Inst);
924 return;
925
926 // If the pointer-to-weak-pointer is null, it's undefined behavior.
927 case ARCInstKind::StoreWeak:
928 case ARCInstKind::LoadWeak:
929 case ARCInstKind::LoadWeakRetained:
930 case ARCInstKind::InitWeak:
931 case ARCInstKind::DestroyWeak: {
932 CallInst *CI = cast<CallInst>(Inst);
933 if (IsNullOrUndef(CI->getArgOperand(0))) {
934 Changed = true;
935 new StoreInst(ConstantInt::getTrue(CI->getContext()),
936 PoisonValue::get(PointerType::getUnqual(CI->getContext())),
937 CI->getIterator());
938 Value *NewValue = PoisonValue::get(CI->getType());
940 dbgs() << "A null pointer-to-weak-pointer is undefined behavior."
941 "\nOld = "
942 << *CI << "\nNew = " << *NewValue << "\n");
943 CI->replaceAllUsesWith(NewValue);
944 CI->eraseFromParent();
945 return;
946 }
947 break;
948 }
949 case ARCInstKind::CopyWeak:
950 case ARCInstKind::MoveWeak: {
951 CallInst *CI = cast<CallInst>(Inst);
952 if (IsNullOrUndef(CI->getArgOperand(0)) ||
954 Changed = true;
955 new StoreInst(ConstantInt::getTrue(CI->getContext()),
956 PoisonValue::get(PointerType::getUnqual(CI->getContext())),
957 CI->getIterator());
958
959 Value *NewValue = PoisonValue::get(CI->getType());
961 dbgs() << "A null pointer-to-weak-pointer is undefined behavior."
962 "\nOld = "
963 << *CI << "\nNew = " << *NewValue << "\n");
964
965 CI->replaceAllUsesWith(NewValue);
966 CI->eraseFromParent();
967 return;
968 }
969 break;
970 }
971 case ARCInstKind::RetainRV:
972 if (OptimizeRetainRVCall(F, Inst))
973 return;
974 break;
975 case ARCInstKind::AutoreleaseRV:
976 OptimizeAutoreleaseRVCall(F, Inst, Class);
977 break;
978 }
979
980 // objc_autorelease(x) -> objc_release(x) if x is otherwise unused.
981 if (IsAutorelease(Class) && Inst->use_empty()) {
982 CallInst *Call = cast<CallInst>(Inst);
983 const Value *Arg = Call->getArgOperand(0);
985 if (Arg) {
986 Changed = true;
987 ++NumAutoreleases;
988
989 // Create the declaration lazily.
990 LLVMContext &C = Inst->getContext();
991
992 Function *Decl = EP.get(ARCRuntimeEntryPointKind::Release);
993 CallInst *NewCall = CallInst::Create(Decl, Call->getArgOperand(0), "",
994 Call->getIterator());
995 NewCall->setMetadata(MDKindCache.get(ARCMDKindID::ImpreciseRelease),
996 MDNode::get(C, {}));
997
998 LLVM_DEBUG(dbgs() << "Replacing autorelease{,RV}(x) with objc_release(x) "
999 "since x is otherwise unused.\nOld: "
1000 << *Call << "\nNew: " << *NewCall << "\n");
1001
1003 Inst = NewCall;
1004 Class = ARCInstKind::Release;
1005 }
1006 }
1007
1008 // For functions which can never be passed stack arguments, add
1009 // a tail keyword.
1010 if (IsAlwaysTail(Class) && !cast<CallInst>(Inst)->isNoTailCall()) {
1011 Changed = true;
1012 LLVM_DEBUG(
1013 dbgs() << "Adding tail keyword to function since it can never be "
1014 "passed stack args: "
1015 << *Inst << "\n");
1016 cast<CallInst>(Inst)->setTailCall();
1017 }
1018
1019 // Ensure that functions that can never have a "tail" keyword due to the
1020 // semantics of ARC truly do not do so.
1021 if (IsNeverTail(Class)) {
1022 Changed = true;
1023 LLVM_DEBUG(dbgs() << "Removing tail keyword from function: " << *Inst
1024 << "\n");
1025 cast<CallInst>(Inst)->setTailCall(false);
1026 }
1027
1028 // Set nounwind as needed.
1029 if (IsNoThrow(Class)) {
1030 Changed = true;
1031 LLVM_DEBUG(dbgs() << "Found no throw class. Setting nounwind on: " << *Inst
1032 << "\n");
1033 cast<CallInst>(Inst)->setDoesNotThrow();
1034 }
1035
1036 // Note: This catches instructions unrelated to ARC.
1037 if (!IsNoopOnNull(Class)) {
1038 UsedInThisFunction |= 1 << unsigned(Class);
1039 return;
1040 }
1041
1042 // If we haven't already looked up the root, look it up now.
1043 if (!Arg)
1044 Arg = GetArgRCIdentityRoot(Inst);
1045
1046 // ARC calls with null are no-ops. Delete them.
1047 if (IsNullOrUndef(Arg)) {
1048 Changed = true;
1049 ++NumNoops;
1050 LLVM_DEBUG(dbgs() << "ARC calls with null are no-ops. Erasing: " << *Inst
1051 << "\n");
1052 EraseInstruction(Inst);
1053 return;
1054 }
1055
1056 // Keep track of which of retain, release, autorelease, and retain_block
1057 // are actually present in this function.
1058 UsedInThisFunction |= 1 << unsigned(Class);
1059
1060 // If Arg is a PHI, and one or more incoming values to the
1061 // PHI are null, and the call is control-equivalent to the PHI, and there
1062 // are no relevant side effects between the PHI and the call, and the call
1063 // is not a release that doesn't have the clang.imprecise_release tag, the
1064 // call could be pushed up to just those paths with non-null incoming
1065 // values. For now, don't bother splitting critical edges for this.
1066 if (Class == ARCInstKind::Release &&
1067 !Inst->getMetadata(MDKindCache.get(ARCMDKindID::ImpreciseRelease)))
1068 return;
1069
1071 Worklist.push_back(std::make_pair(Inst, Arg));
1072 do {
1073 std::pair<Instruction *, const Value *> Pair = Worklist.pop_back_val();
1074 Inst = Pair.first;
1075 Arg = Pair.second;
1076
1077 const PHINode *PN = dyn_cast<PHINode>(Arg);
1078 if (!PN)
1079 continue;
1080
1081 // Determine if the PHI has any null operands, or any incoming
1082 // critical edges.
1083 bool HasNull = false;
1084 bool HasCriticalEdges = false;
1085 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
1086 Value *Incoming = GetRCIdentityRoot(PN->getIncomingValue(i));
1087 if (IsNullOrUndef(Incoming))
1088 HasNull = true;
1089 else if (PN->getIncomingBlock(i)->getTerminator()->getNumSuccessors() !=
1090 1) {
1091 HasCriticalEdges = true;
1092 break;
1093 }
1094 }
1095 // If we have null operands and no critical edges, optimize.
1096 if (HasCriticalEdges)
1097 continue;
1098 if (!HasNull)
1099 continue;
1100
1101 Instruction *DepInst = nullptr;
1102
1103 // Check that there is nothing that cares about the reference
1104 // count between the call and the phi.
1105 switch (Class) {
1106 case ARCInstKind::Retain:
1107 case ARCInstKind::RetainBlock:
1108 // These can always be moved up.
1109 break;
1110 case ARCInstKind::Release:
1111 // These can't be moved across things that care about the retain
1112 // count.
1114 Inst->getParent(), Inst, PA);
1115 break;
1116 case ARCInstKind::Autorelease:
1117 // These can't be moved across autorelease pool scope boundaries.
1119 Inst->getParent(), Inst, PA);
1120 break;
1121 case ARCInstKind::UnsafeClaimRV:
1122 case ARCInstKind::RetainRV:
1123 case ARCInstKind::AutoreleaseRV:
1124 // Don't move these; the RV optimization depends on the autoreleaseRV
1125 // being tail called, and the retainRV being immediately after a call
1126 // (which might still happen if we get lucky with codegen layout, but
1127 // it's not worth taking the chance).
1128 continue;
1129 default:
1130 llvm_unreachable("Invalid dependence flavor");
1131 }
1132
1133 if (DepInst != PN)
1134 continue;
1135
1136 Changed = true;
1137 ++NumPartialNoops;
1138 // Clone the call into each predecessor that has a non-null value.
1139 CallInst *CInst = cast<CallInst>(Inst);
1140 Type *ParamTy = CInst->getArgOperand(0)->getType();
1141 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
1142 Value *Incoming = GetRCIdentityRoot(PN->getIncomingValue(i));
1143 if (IsNullOrUndef(Incoming))
1144 continue;
1145 Value *Op = PN->getIncomingValue(i);
1146 BasicBlock::iterator InsertPos =
1147 PN->getIncomingBlock(i)->back().getIterator();
1149 cloneOpBundlesIf(CInst, OpBundles, [](const OperandBundleUse &B) {
1150 return B.getTagID() != LLVMContext::OB_funclet;
1151 });
1152 addOpBundleForFunclet(InsertPos->getParent(), OpBundles);
1153 CallInst *Clone = CallInst::Create(CInst, OpBundles);
1154 if (Op->getType() != ParamTy)
1155 Op = new BitCastInst(Op, ParamTy, "", InsertPos);
1156 Clone->setArgOperand(0, Op);
1157 Clone->insertBefore(*InsertPos->getParent(), InsertPos);
1158
1159 LLVM_DEBUG(dbgs() << "Cloning " << *CInst << "\n"
1160 "And inserting clone at "
1161 << *InsertPos << "\n");
1162 Worklist.push_back(std::make_pair(Clone, Incoming));
1163 }
1164 // Erase the original call.
1165 LLVM_DEBUG(dbgs() << "Erasing: " << *CInst << "\n");
1166 EraseInstruction(CInst);
1167 } while (!Worklist.empty());
1168}
1169
1170/// If we have a top down pointer in the S_Use state, make sure that there are
1171/// no CFG hazards by checking the states of various bottom up pointers.
1172static void CheckForUseCFGHazard(const Sequence SuccSSeq,
1173 const bool SuccSRRIKnownSafe,
1174 TopDownPtrState &S,
1175 bool &SomeSuccHasSame,
1176 bool &AllSuccsHaveSame,
1177 bool &NotAllSeqEqualButKnownSafe,
1178 bool &ShouldContinue) {
1179 switch (SuccSSeq) {
1180 case S_CanRelease: {
1181 if (!S.IsKnownSafe() && !SuccSRRIKnownSafe) {
1183 break;
1184 }
1185 S.SetCFGHazardAfflicted(true);
1186 ShouldContinue = true;
1187 break;
1188 }
1189 case S_Use:
1190 SomeSuccHasSame = true;
1191 break;
1192 case S_Stop:
1193 case S_MovableRelease:
1194 if (!S.IsKnownSafe() && !SuccSRRIKnownSafe)
1195 AllSuccsHaveSame = false;
1196 else
1197 NotAllSeqEqualButKnownSafe = true;
1198 break;
1199 case S_Retain:
1200 llvm_unreachable("bottom-up pointer in retain state!");
1201 case S_None:
1202 llvm_unreachable("This should have been handled earlier.");
1203 }
1204}
1205
1206/// If we have a Top Down pointer in the S_CanRelease state, make sure that
1207/// there are no CFG hazards by checking the states of various bottom up
1208/// pointers.
1209static void CheckForCanReleaseCFGHazard(const Sequence SuccSSeq,
1210 const bool SuccSRRIKnownSafe,
1211 TopDownPtrState &S,
1212 bool &SomeSuccHasSame,
1213 bool &AllSuccsHaveSame,
1214 bool &NotAllSeqEqualButKnownSafe) {
1215 switch (SuccSSeq) {
1216 case S_CanRelease:
1217 SomeSuccHasSame = true;
1218 break;
1219 case S_Stop:
1220 case S_MovableRelease:
1221 case S_Use:
1222 if (!S.IsKnownSafe() && !SuccSRRIKnownSafe)
1223 AllSuccsHaveSame = false;
1224 else
1225 NotAllSeqEqualButKnownSafe = true;
1226 break;
1227 case S_Retain:
1228 llvm_unreachable("bottom-up pointer in retain state!");
1229 case S_None:
1230 llvm_unreachable("This should have been handled earlier.");
1231 }
1232}
1233
1234/// Check for critical edges, loop boundaries, irreducible control flow, or
1235/// other CFG structures where moving code across the edge would result in it
1236/// being executed more.
1237void
1238ObjCARCOpt::CheckForCFGHazards(const BasicBlock *BB,
1239 DenseMap<const BasicBlock *, BBState> &BBStates,
1240 BBState &MyStates) const {
1241 // If any top-down local-use or possible-dec has a succ which is earlier in
1242 // the sequence, forget it.
1243 for (auto I = MyStates.top_down_ptr_begin(), E = MyStates.top_down_ptr_end();
1244 I != E; ++I) {
1245 TopDownPtrState &S = I->second;
1246 const Sequence Seq = I->second.GetSeq();
1247
1248 // We only care about S_Retain, S_CanRelease, and S_Use.
1249 if (Seq == S_None)
1250 continue;
1251
1252 // Make sure that if extra top down states are added in the future that this
1253 // code is updated to handle it.
1254 assert((Seq == S_Retain || Seq == S_CanRelease || Seq == S_Use) &&
1255 "Unknown top down sequence state.");
1256
1257 const Value *Arg = I->first;
1258 bool SomeSuccHasSame = false;
1259 bool AllSuccsHaveSame = true;
1260 bool NotAllSeqEqualButKnownSafe = false;
1261
1262 for (const BasicBlock *Succ : successors(BB)) {
1263 // If VisitBottomUp has pointer information for this successor, take
1264 // what we know about it.
1265 const DenseMap<const BasicBlock *, BBState>::iterator BBI =
1266 BBStates.find(Succ);
1267 assert(BBI != BBStates.end());
1268 const BottomUpPtrState &SuccS = BBI->second.getPtrBottomUpState(Arg);
1269 const Sequence SuccSSeq = SuccS.GetSeq();
1270
1271 // If bottom up, the pointer is in an S_None state, clear the sequence
1272 // progress since the sequence in the bottom up state finished
1273 // suggesting a mismatch in between retains/releases. This is true for
1274 // all three cases that we are handling here: S_Retain, S_Use, and
1275 // S_CanRelease.
1276 if (SuccSSeq == S_None) {
1278 continue;
1279 }
1280
1281 // If we have S_Use or S_CanRelease, perform our check for cfg hazard
1282 // checks.
1283 const bool SuccSRRIKnownSafe = SuccS.IsKnownSafe();
1284
1285 // *NOTE* We do not use Seq from above here since we are allowing for
1286 // S.GetSeq() to change while we are visiting basic blocks.
1287 switch(S.GetSeq()) {
1288 case S_Use: {
1289 bool ShouldContinue = false;
1290 CheckForUseCFGHazard(SuccSSeq, SuccSRRIKnownSafe, S, SomeSuccHasSame,
1291 AllSuccsHaveSame, NotAllSeqEqualButKnownSafe,
1292 ShouldContinue);
1293 if (ShouldContinue)
1294 continue;
1295 break;
1296 }
1297 case S_CanRelease:
1298 CheckForCanReleaseCFGHazard(SuccSSeq, SuccSRRIKnownSafe, S,
1299 SomeSuccHasSame, AllSuccsHaveSame,
1300 NotAllSeqEqualButKnownSafe);
1301 break;
1302 case S_Retain:
1303 case S_None:
1304 case S_Stop:
1305 case S_MovableRelease:
1306 break;
1307 }
1308 }
1309
1310 // If the state at the other end of any of the successor edges
1311 // matches the current state, require all edges to match. This
1312 // guards against loops in the middle of a sequence.
1313 if (SomeSuccHasSame && !AllSuccsHaveSame) {
1315 } else if (NotAllSeqEqualButKnownSafe) {
1316 // If we would have cleared the state foregoing the fact that we are known
1317 // safe, stop code motion. This is because whether or not it is safe to
1318 // remove RR pairs via KnownSafe is an orthogonal concept to whether we
1319 // are allowed to perform code motion.
1320 S.SetCFGHazardAfflicted(true);
1321 }
1322 }
1323}
1324
1325bool ObjCARCOpt::VisitInstructionBottomUp(
1326 Instruction *Inst, BasicBlock *BB, BlotMapVector<Value *, RRInfo> &Retains,
1327 BBState &MyStates) {
1328 bool NestingDetected = false;
1330 const Value *Arg = nullptr;
1331
1332 LLVM_DEBUG(dbgs() << " Class: " << Class << "\n");
1333
1334 switch (Class) {
1335 case ARCInstKind::Release: {
1336 Arg = GetArgRCIdentityRoot(Inst);
1337
1338 BottomUpPtrState &S = MyStates.getPtrBottomUpState(Arg);
1339 NestingDetected |= S.InitBottomUp(MDKindCache, Inst);
1340 break;
1341 }
1342 case ARCInstKind::RetainBlock:
1343 // In OptimizeIndividualCalls, we have strength reduced all optimizable
1344 // objc_retainBlocks to objc_retains. Thus at this point any
1345 // objc_retainBlocks that we see are not optimizable.
1346 break;
1347 case ARCInstKind::Retain:
1348 case ARCInstKind::RetainRV: {
1349 Arg = GetArgRCIdentityRoot(Inst);
1350 BottomUpPtrState &S = MyStates.getPtrBottomUpState(Arg);
1351 if (S.MatchWithRetain()) {
1352 // Don't do retain+release tracking for ARCInstKind::RetainRV, because
1353 // it's better to let it remain as the first instruction after a call.
1354 if (Class != ARCInstKind::RetainRV) {
1355 LLVM_DEBUG(dbgs() << " Matching with: " << *Inst << "\n");
1356 Retains[Inst] = S.GetRRInfo();
1357 }
1359 }
1360 // A retain moving bottom up can be a use.
1361 break;
1362 }
1363 case ARCInstKind::AutoreleasepoolPop:
1364 // Conservatively, clear MyStates for all known pointers.
1365 MyStates.clearBottomUpPointers();
1366 return NestingDetected;
1367 case ARCInstKind::AutoreleasepoolPush:
1368 case ARCInstKind::None:
1369 // These are irrelevant.
1370 return NestingDetected;
1371 default:
1372 break;
1373 }
1374
1375 // Consider any other possible effects of this instruction on each
1376 // pointer being tracked.
1377 for (auto MI = MyStates.bottom_up_ptr_begin(),
1378 ME = MyStates.bottom_up_ptr_end();
1379 MI != ME; ++MI) {
1380 const Value *Ptr = MI->first;
1381 if (Ptr == Arg)
1382 continue; // Handled above.
1383 BottomUpPtrState &S = MI->second;
1384
1385 if (S.HandlePotentialAlterRefCount(Inst, Ptr, PA, Class))
1386 continue;
1387
1388 S.HandlePotentialUse(BB, Inst, Ptr, PA, Class);
1389 }
1390
1391 return NestingDetected;
1392}
1393
1394bool ObjCARCOpt::VisitBottomUp(BasicBlock *BB,
1395 DenseMap<const BasicBlock *, BBState> &BBStates,
1396 BlotMapVector<Value *, RRInfo> &Retains) {
1397 LLVM_DEBUG(dbgs() << "\n== ObjCARCOpt::VisitBottomUp ==\n");
1398
1399 bool NestingDetected = false;
1400 BBState &MyStates = BBStates[BB];
1401
1402 // Merge the states from each successor to compute the initial state
1403 // for the current block.
1404 BBState::edge_iterator SI(MyStates.succ_begin()),
1405 SE(MyStates.succ_end());
1406 if (SI != SE) {
1407 const BasicBlock *Succ = *SI;
1408 DenseMap<const BasicBlock *, BBState>::iterator I = BBStates.find(Succ);
1409 assert(I != BBStates.end());
1410 MyStates.InitFromSucc(I->second);
1411 ++SI;
1412 for (; SI != SE; ++SI) {
1413 Succ = *SI;
1414 I = BBStates.find(Succ);
1415 assert(I != BBStates.end());
1416 MyStates.MergeSucc(I->second);
1417 }
1418 }
1419
1420 LLVM_DEBUG(dbgs() << "Before:\n"
1421 << BBStates[BB] << "\n"
1422 << "Performing Dataflow:\n");
1423
1424 // Visit all the instructions, bottom-up.
1425 for (BasicBlock::iterator I = BB->end(), E = BB->begin(); I != E; --I) {
1426 Instruction *Inst = &*std::prev(I);
1427
1428 // Invoke instructions are visited as part of their successors (below).
1429 if (isa<InvokeInst>(Inst))
1430 continue;
1431
1432 LLVM_DEBUG(dbgs() << " Visiting " << *Inst << "\n");
1433
1434 NestingDetected |= VisitInstructionBottomUp(Inst, BB, Retains, MyStates);
1435
1436 // Bail out if the number of pointers being tracked becomes too large so
1437 // that this pass can complete in a reasonable amount of time.
1438 if (MyStates.bottom_up_ptr_list_size() > MaxPtrStates) {
1439 DisableRetainReleasePairing = true;
1440 return false;
1441 }
1442 }
1443
1444 // If there's a predecessor with an invoke, visit the invoke as if it were
1445 // part of this block, since we can't insert code after an invoke in its own
1446 // block, and we don't want to split critical edges.
1447 for (BBState::edge_iterator PI(MyStates.pred_begin()),
1448 PE(MyStates.pred_end()); PI != PE; ++PI) {
1449 BasicBlock *Pred = *PI;
1450 if (InvokeInst *II = dyn_cast<InvokeInst>(&Pred->back()))
1451 NestingDetected |= VisitInstructionBottomUp(II, BB, Retains, MyStates);
1452 }
1453
1454 LLVM_DEBUG(dbgs() << "\nFinal State:\n" << BBStates[BB] << "\n");
1455
1456 return NestingDetected;
1457}
1458
1459// Fill ReleaseInsertPtToRCIdentityRoots, which is a map from insertion points
1460// to the set of RC identity roots that would be released by the release calls
1461// moved to the insertion points.
1463 const BlotMapVector<Value *, RRInfo> &Retains,
1465 &ReleaseInsertPtToRCIdentityRoots) {
1466 for (const auto &P : Retains) {
1467 // Retains is a map from an objc_retain call to a RRInfo of the RC identity
1468 // root of the call. Get the RC identity root of the objc_retain call.
1470 Value *Root = GetRCIdentityRoot(Retain->getOperand(0));
1471 // Collect all the insertion points of the objc_release calls that release
1472 // the RC identity root of the objc_retain call.
1473 for (const Instruction *InsertPt : P.second.ReverseInsertPts)
1474 ReleaseInsertPtToRCIdentityRoots[InsertPt].insert(Root);
1475 }
1476}
1477
1478// Get the RC identity roots from an insertion point of an objc_release call.
1479// Return nullptr if the passed instruction isn't an insertion point.
1480static const SmallPtrSet<const Value *, 2> *
1482 const Instruction *InsertPt,
1484 &ReleaseInsertPtToRCIdentityRoots) {
1485 auto I = ReleaseInsertPtToRCIdentityRoots.find(InsertPt);
1486 if (I == ReleaseInsertPtToRCIdentityRoots.end())
1487 return nullptr;
1488 return &I->second;
1489}
1490
1491bool ObjCARCOpt::VisitInstructionTopDown(
1492 Instruction *Inst, DenseMap<Value *, RRInfo> &Releases, BBState &MyStates,
1493 const DenseMap<const Instruction *, SmallPtrSet<const Value *, 2>>
1494 &ReleaseInsertPtToRCIdentityRoots) {
1495 bool NestingDetected = false;
1497 const Value *Arg = nullptr;
1498
1499 // Make sure a call to objc_retain isn't moved past insertion points of calls
1500 // to objc_release.
1501 if (const SmallPtrSet<const Value *, 2> *Roots =
1503 Inst, ReleaseInsertPtToRCIdentityRoots))
1504 for (const auto *Root : *Roots) {
1505 TopDownPtrState &S = MyStates.getPtrTopDownState(Root);
1506 // Disable code motion if the current position is S_Retain to prevent
1507 // moving the objc_retain call past objc_release calls. If it's
1508 // S_CanRelease or larger, it's not necessary to disable code motion as
1509 // the insertion points that prevent the objc_retain call from moving down
1510 // should have been set already.
1511 if (S.GetSeq() == S_Retain)
1512 S.SetCFGHazardAfflicted(true);
1513 }
1514
1515 LLVM_DEBUG(dbgs() << " Class: " << Class << "\n");
1516
1517 switch (Class) {
1518 case ARCInstKind::RetainBlock:
1519 // In OptimizeIndividualCalls, we have strength reduced all optimizable
1520 // objc_retainBlocks to objc_retains. Thus at this point any
1521 // objc_retainBlocks that we see are not optimizable. We need to break since
1522 // a retain can be a potential use.
1523 break;
1524 case ARCInstKind::Retain:
1525 case ARCInstKind::RetainRV: {
1526 Arg = GetArgRCIdentityRoot(Inst);
1527 TopDownPtrState &S = MyStates.getPtrTopDownState(Arg);
1528 NestingDetected |= S.InitTopDown(Class, Inst);
1529 // A retain can be a potential use; proceed to the generic checking
1530 // code below.
1531 break;
1532 }
1533 case ARCInstKind::Release: {
1534 Arg = GetArgRCIdentityRoot(Inst);
1535 TopDownPtrState &S = MyStates.getPtrTopDownState(Arg);
1536 // Try to form a tentative pair in between this release instruction and the
1537 // top down pointers that we are tracking.
1538 if (S.MatchWithRelease(MDKindCache, Inst)) {
1539 // If we succeed, copy S's RRInfo into the Release -> {Retain Set
1540 // Map}. Then we clear S.
1541 LLVM_DEBUG(dbgs() << " Matching with: " << *Inst << "\n");
1542 Releases[Inst] = S.GetRRInfo();
1544 }
1545 break;
1546 }
1547 case ARCInstKind::AutoreleasepoolPop:
1548 // Conservatively, clear MyStates for all known pointers.
1549 MyStates.clearTopDownPointers();
1550 return false;
1551 case ARCInstKind::AutoreleasepoolPush:
1552 case ARCInstKind::None:
1553 // These can not be uses of
1554 return false;
1555 default:
1556 break;
1557 }
1558
1559 // Consider any other possible effects of this instruction on each
1560 // pointer being tracked.
1561 for (auto MI = MyStates.top_down_ptr_begin(),
1562 ME = MyStates.top_down_ptr_end();
1563 MI != ME; ++MI) {
1564 const Value *Ptr = MI->first;
1565 if (Ptr == Arg)
1566 continue; // Handled above.
1567 TopDownPtrState &S = MI->second;
1568 if (S.HandlePotentialAlterRefCount(Inst, Ptr, PA, Class, *BundledInsts))
1569 continue;
1570
1571 S.HandlePotentialUse(Inst, Ptr, PA, Class);
1572 }
1573
1574 return NestingDetected;
1575}
1576
1577bool ObjCARCOpt::VisitTopDown(
1578 BasicBlock *BB, DenseMap<const BasicBlock *, BBState> &BBStates,
1579 DenseMap<Value *, RRInfo> &Releases,
1580 const DenseMap<const Instruction *, SmallPtrSet<const Value *, 2>>
1581 &ReleaseInsertPtToRCIdentityRoots) {
1582 LLVM_DEBUG(dbgs() << "\n== ObjCARCOpt::VisitTopDown ==\n");
1583 bool NestingDetected = false;
1584 BBState &MyStates = BBStates[BB];
1585
1586 // Merge the states from each predecessor to compute the initial state
1587 // for the current block.
1588 BBState::edge_iterator PI(MyStates.pred_begin()),
1589 PE(MyStates.pred_end());
1590 if (PI != PE) {
1591 const BasicBlock *Pred = *PI;
1592 DenseMap<const BasicBlock *, BBState>::iterator I = BBStates.find(Pred);
1593 assert(I != BBStates.end());
1594 MyStates.InitFromPred(I->second);
1595 ++PI;
1596 for (; PI != PE; ++PI) {
1597 Pred = *PI;
1598 I = BBStates.find(Pred);
1599 assert(I != BBStates.end());
1600 MyStates.MergePred(I->second);
1601 }
1602 }
1603
1604 // Check that BB and MyStates have the same number of predecessors. This
1605 // prevents retain calls that live outside a loop from being moved into the
1606 // loop.
1607 if (!BB->hasNPredecessors(MyStates.pred_end() - MyStates.pred_begin()))
1608 for (auto I = MyStates.top_down_ptr_begin(),
1609 E = MyStates.top_down_ptr_end();
1610 I != E; ++I)
1611 I->second.SetCFGHazardAfflicted(true);
1612
1613 LLVM_DEBUG(dbgs() << "Before:\n"
1614 << BBStates[BB] << "\n"
1615 << "Performing Dataflow:\n");
1616
1617 // Visit all the instructions, top-down.
1618 for (Instruction &Inst : *BB) {
1619 LLVM_DEBUG(dbgs() << " Visiting " << Inst << "\n");
1620
1621 NestingDetected |= VisitInstructionTopDown(
1622 &Inst, Releases, MyStates, ReleaseInsertPtToRCIdentityRoots);
1623
1624 // Bail out if the number of pointers being tracked becomes too large so
1625 // that this pass can complete in a reasonable amount of time.
1626 if (MyStates.top_down_ptr_list_size() > MaxPtrStates) {
1627 DisableRetainReleasePairing = true;
1628 return false;
1629 }
1630 }
1631
1632 LLVM_DEBUG(dbgs() << "\nState Before Checking for CFG Hazards:\n"
1633 << BBStates[BB] << "\n\n");
1634 CheckForCFGHazards(BB, BBStates, MyStates);
1635 LLVM_DEBUG(dbgs() << "Final State:\n" << BBStates[BB] << "\n");
1636 return NestingDetected;
1637}
1638
1639static void
1642 SmallVectorImpl<BasicBlock *> &ReverseCFGPostOrder,
1643 unsigned NoObjCARCExceptionsMDKind,
1645 /// The visited set, for doing DFS walks.
1647
1648 // Do DFS, computing the PostOrder.
1651
1652 // Functions always have exactly one entry block, and we don't have
1653 // any other block that we treat like an entry block.
1654 BasicBlock *EntryBB = &F.getEntryBlock();
1655 BBState &MyStates = BBStates[EntryBB];
1656 MyStates.SetAsEntry();
1657 SuccStack.push_back(std::make_pair(EntryBB, succ_begin(EntryBB)));
1658 Visited.insert(EntryBB);
1659 OnStack.insert(EntryBB);
1660 do {
1661 dfs_next_succ:
1662 BasicBlock *CurrBB = SuccStack.back().first;
1663 succ_iterator SE = succ_end(CurrBB->getTerminator());
1664
1665 while (SuccStack.back().second != SE) {
1666 BasicBlock *SuccBB = *SuccStack.back().second++;
1667 if (Visited.insert(SuccBB).second) {
1668 SuccStack.push_back(std::make_pair(SuccBB, succ_begin(SuccBB)));
1669 BBStates[CurrBB].addSucc(SuccBB);
1670 BBState &SuccStates = BBStates[SuccBB];
1671 SuccStates.addPred(CurrBB);
1672 OnStack.insert(SuccBB);
1673 goto dfs_next_succ;
1674 }
1675
1676 if (!OnStack.count(SuccBB)) {
1677 BBStates[CurrBB].addSucc(SuccBB);
1678 BBStates[SuccBB].addPred(CurrBB);
1679 }
1680 }
1681 OnStack.erase(CurrBB);
1682 PostOrder.push_back(CurrBB);
1683 SuccStack.pop_back();
1684 } while (!SuccStack.empty());
1685
1686 Visited.clear();
1687
1688 // Do reverse-CFG DFS, computing the reverse-CFG PostOrder.
1689 // Functions may have many exits, and there also blocks which we treat
1690 // as exits due to ignored edges.
1692 for (BasicBlock &ExitBB : F) {
1693 BBState &MyStates = BBStates[&ExitBB];
1694 if (!MyStates.isExit())
1695 continue;
1696
1697 MyStates.SetAsExit();
1698
1699 PredStack.push_back(std::make_pair(&ExitBB, MyStates.pred_begin()));
1700 Visited.insert(&ExitBB);
1701 while (!PredStack.empty()) {
1702 reverse_dfs_next_succ:
1703 BBState::edge_iterator PE = BBStates[PredStack.back().first].pred_end();
1704 while (PredStack.back().second != PE) {
1705 BasicBlock *BB = *PredStack.back().second++;
1706 if (Visited.insert(BB).second) {
1707 PredStack.push_back(std::make_pair(BB, BBStates[BB].pred_begin()));
1708 goto reverse_dfs_next_succ;
1709 }
1710 }
1711 ReverseCFGPostOrder.push_back(PredStack.pop_back_val().first);
1712 }
1713 }
1714}
1715
1716// Visit the function both top-down and bottom-up.
1717bool ObjCARCOpt::Visit(Function &F,
1718 DenseMap<const BasicBlock *, BBState> &BBStates,
1719 BlotMapVector<Value *, RRInfo> &Retains,
1720 DenseMap<Value *, RRInfo> &Releases) {
1721 // Use reverse-postorder traversals, because we magically know that loops
1722 // will be well behaved, i.e. they won't repeatedly call retain on a single
1723 // pointer without doing a release. We can't use the ReversePostOrderTraversal
1724 // class here because we want the reverse-CFG postorder to consider each
1725 // function exit point, and we want to ignore selected cycle edges.
1726 SmallVector<BasicBlock *, 16> PostOrder;
1727 SmallVector<BasicBlock *, 16> ReverseCFGPostOrder;
1728 ComputePostOrders(F, PostOrder, ReverseCFGPostOrder,
1729 MDKindCache.get(ARCMDKindID::NoObjCARCExceptions),
1730 BBStates);
1731
1732 // Use reverse-postorder on the reverse CFG for bottom-up.
1733 bool BottomUpNestingDetected = false;
1734 for (BasicBlock *BB : llvm::reverse(ReverseCFGPostOrder)) {
1735 BottomUpNestingDetected |= VisitBottomUp(BB, BBStates, Retains);
1736 if (DisableRetainReleasePairing)
1737 return false;
1738 }
1739
1740 DenseMap<const Instruction *, SmallPtrSet<const Value *, 2>>
1741 ReleaseInsertPtToRCIdentityRoots;
1742 collectReleaseInsertPts(Retains, ReleaseInsertPtToRCIdentityRoots);
1743
1744 // Use reverse-postorder for top-down.
1745 bool TopDownNestingDetected = false;
1746 for (BasicBlock *BB : llvm::reverse(PostOrder)) {
1747 TopDownNestingDetected |=
1748 VisitTopDown(BB, BBStates, Releases, ReleaseInsertPtToRCIdentityRoots);
1749 if (DisableRetainReleasePairing)
1750 return false;
1751 }
1752
1753 return TopDownNestingDetected && BottomUpNestingDetected;
1754}
1755
1756/// Move the calls in RetainsToMove and ReleasesToMove.
1757void ObjCARCOpt::MoveCalls(Value *Arg, RRInfo &RetainsToMove,
1758 RRInfo &ReleasesToMove,
1759 BlotMapVector<Value *, RRInfo> &Retains,
1760 DenseMap<Value *, RRInfo> &Releases,
1761 SmallVectorImpl<Instruction *> &DeadInsts,
1762 Module *M) {
1763 LLVM_DEBUG(dbgs() << "== ObjCARCOpt::MoveCalls ==\n");
1764
1765 // Insert the new retain and release calls.
1766 for (Instruction *InsertPt : ReleasesToMove.ReverseInsertPts) {
1767 Function *Decl = EP.get(ARCRuntimeEntryPointKind::Retain);
1769 addOpBundleForFunclet(InsertPt->getParent(), BundleList);
1770 CallInst *Call =
1771 CallInst::Create(Decl, Arg, BundleList, "", InsertPt->getIterator());
1773 Call->setTailCall();
1774
1775 LLVM_DEBUG(dbgs() << "Inserting new Retain: " << *Call
1776 << "\n"
1777 "At insertion point: "
1778 << *InsertPt << "\n");
1779 }
1780 for (Instruction *InsertPt : RetainsToMove.ReverseInsertPts) {
1781 Function *Decl = EP.get(ARCRuntimeEntryPointKind::Release);
1783 addOpBundleForFunclet(InsertPt->getParent(), BundleList);
1784 CallInst *Call =
1785 CallInst::Create(Decl, Arg, BundleList, "", InsertPt->getIterator());
1786 // Attach a clang.imprecise_release metadata tag, if appropriate.
1787 if (MDNode *M = ReleasesToMove.ReleaseMetadata)
1788 Call->setMetadata(MDKindCache.get(ARCMDKindID::ImpreciseRelease), M);
1790 if (ReleasesToMove.IsTailCallRelease)
1791 Call->setTailCall();
1792
1793 LLVM_DEBUG(dbgs() << "Inserting new Release: " << *Call
1794 << "\n"
1795 "At insertion point: "
1796 << *InsertPt << "\n");
1797 }
1798
1799 // Delete the original retain and release calls.
1800 for (Instruction *OrigRetain : RetainsToMove.Calls) {
1801 Retains.blot(OrigRetain);
1802 DeadInsts.push_back(OrigRetain);
1803 LLVM_DEBUG(dbgs() << "Deleting retain: " << *OrigRetain << "\n");
1804 }
1805 for (Instruction *OrigRelease : ReleasesToMove.Calls) {
1806 Releases.erase(OrigRelease);
1807 DeadInsts.push_back(OrigRelease);
1808 LLVM_DEBUG(dbgs() << "Deleting release: " << *OrigRelease << "\n");
1809 }
1810}
1811
1812bool ObjCARCOpt::PairUpRetainsAndReleases(
1813 DenseMap<const BasicBlock *, BBState> &BBStates,
1814 BlotMapVector<Value *, RRInfo> &Retains,
1815 DenseMap<Value *, RRInfo> &Releases, Module *M,
1816 Instruction *Retain,
1817 SmallVectorImpl<Instruction *> &DeadInsts, RRInfo &RetainsToMove,
1818 RRInfo &ReleasesToMove, Value *Arg, bool KnownSafe,
1819 bool &AnyPairsCompletelyEliminated) {
1820 // If a pair happens in a region where it is known that the reference count
1821 // is already incremented, we can similarly ignore possible decrements unless
1822 // we are dealing with a retainable object with multiple provenance sources.
1823 bool KnownSafeTD = true, KnownSafeBU = true;
1824 bool CFGHazardAfflicted = false;
1825
1826 // Connect the dots between the top-down-collected RetainsToMove and
1827 // bottom-up-collected ReleasesToMove to form sets of related calls.
1828 // This is an iterative process so that we connect multiple releases
1829 // to multiple retains if needed.
1830 unsigned OldDelta = 0;
1831 unsigned NewDelta = 0;
1832 unsigned OldCount = 0;
1833 unsigned NewCount = 0;
1834 bool FirstRelease = true;
1835 for (SmallVector<Instruction *, 4> NewRetains{Retain};;) {
1836 SmallVector<Instruction *, 4> NewReleases;
1837 for (Instruction *NewRetain : NewRetains) {
1838 auto It = Retains.find(NewRetain);
1839 assert(It != Retains.end());
1840 const RRInfo &NewRetainRRI = It->second;
1841 KnownSafeTD &= NewRetainRRI.KnownSafe;
1842 CFGHazardAfflicted |= NewRetainRRI.CFGHazardAfflicted;
1843 for (Instruction *NewRetainRelease : NewRetainRRI.Calls) {
1844 auto Jt = Releases.find(NewRetainRelease);
1845 if (Jt == Releases.end())
1846 return false;
1847 const RRInfo &NewRetainReleaseRRI = Jt->second;
1848
1849 // If the release does not have a reference to the retain as well,
1850 // something happened which is unaccounted for. Do not do anything.
1851 //
1852 // This can happen if we catch an additive overflow during path count
1853 // merging.
1854 if (!NewRetainReleaseRRI.Calls.count(NewRetain))
1855 return false;
1856
1857 if (ReleasesToMove.Calls.insert(NewRetainRelease).second) {
1858 // If we overflow when we compute the path count, don't remove/move
1859 // anything.
1860 const BBState &NRRBBState = BBStates[NewRetainRelease->getParent()];
1861 unsigned PathCount = BBState::OverflowOccurredValue;
1862 if (NRRBBState.GetAllPathCountWithOverflow(PathCount))
1863 return false;
1865 "PathCount at this point can not be "
1866 "OverflowOccurredValue.");
1867 OldDelta -= PathCount;
1868
1869 // Merge the ReleaseMetadata and IsTailCallRelease values.
1870 if (FirstRelease) {
1871 ReleasesToMove.ReleaseMetadata =
1872 NewRetainReleaseRRI.ReleaseMetadata;
1873 ReleasesToMove.IsTailCallRelease =
1874 NewRetainReleaseRRI.IsTailCallRelease;
1875 FirstRelease = false;
1876 } else {
1877 if (ReleasesToMove.ReleaseMetadata !=
1878 NewRetainReleaseRRI.ReleaseMetadata)
1879 ReleasesToMove.ReleaseMetadata = nullptr;
1880 if (ReleasesToMove.IsTailCallRelease !=
1881 NewRetainReleaseRRI.IsTailCallRelease)
1882 ReleasesToMove.IsTailCallRelease = false;
1883 }
1884
1885 // Collect the optimal insertion points.
1886 if (!KnownSafe)
1887 for (Instruction *RIP : NewRetainReleaseRRI.ReverseInsertPts) {
1888 if (ReleasesToMove.ReverseInsertPts.insert(RIP).second) {
1889 // If we overflow when we compute the path count, don't
1890 // remove/move anything.
1891 const BBState &RIPBBState = BBStates[RIP->getParent()];
1893 if (RIPBBState.GetAllPathCountWithOverflow(PathCount))
1894 return false;
1896 "PathCount at this point can not be "
1897 "OverflowOccurredValue.");
1898 NewDelta -= PathCount;
1899 }
1900 }
1901 NewReleases.push_back(NewRetainRelease);
1902 }
1903 }
1904 }
1905 NewRetains.clear();
1906 if (NewReleases.empty()) break;
1907
1908 // Back the other way.
1909 for (Instruction *NewRelease : NewReleases) {
1910 auto It = Releases.find(NewRelease);
1911 assert(It != Releases.end());
1912 const RRInfo &NewReleaseRRI = It->second;
1913 KnownSafeBU &= NewReleaseRRI.KnownSafe;
1914 CFGHazardAfflicted |= NewReleaseRRI.CFGHazardAfflicted;
1915 for (Instruction *NewReleaseRetain : NewReleaseRRI.Calls) {
1916 auto Jt = Retains.find(NewReleaseRetain);
1917 if (Jt == Retains.end())
1918 return false;
1919 const RRInfo &NewReleaseRetainRRI = Jt->second;
1920
1921 // If the retain does not have a reference to the release as well,
1922 // something happened which is unaccounted for. Do not do anything.
1923 //
1924 // This can happen if we catch an additive overflow during path count
1925 // merging.
1926 if (!NewReleaseRetainRRI.Calls.count(NewRelease))
1927 return false;
1928
1929 if (RetainsToMove.Calls.insert(NewReleaseRetain).second) {
1930 // If we overflow when we compute the path count, don't remove/move
1931 // anything.
1932 const BBState &NRRBBState = BBStates[NewReleaseRetain->getParent()];
1933 unsigned PathCount = BBState::OverflowOccurredValue;
1934 if (NRRBBState.GetAllPathCountWithOverflow(PathCount))
1935 return false;
1937 "PathCount at this point can not be "
1938 "OverflowOccurredValue.");
1939 OldDelta += PathCount;
1940 OldCount += PathCount;
1941
1942 // Collect the optimal insertion points.
1943 if (!KnownSafe)
1944 for (Instruction *RIP : NewReleaseRetainRRI.ReverseInsertPts) {
1945 if (RetainsToMove.ReverseInsertPts.insert(RIP).second) {
1946 // If we overflow when we compute the path count, don't
1947 // remove/move anything.
1948 const BBState &RIPBBState = BBStates[RIP->getParent()];
1949
1951 if (RIPBBState.GetAllPathCountWithOverflow(PathCount))
1952 return false;
1954 "PathCount at this point can not be "
1955 "OverflowOccurredValue.");
1956 NewDelta += PathCount;
1957 NewCount += PathCount;
1958 }
1959 }
1960 NewRetains.push_back(NewReleaseRetain);
1961 }
1962 }
1963 }
1964 if (NewRetains.empty()) break;
1965 }
1966
1967 // We can only remove pointers if we are known safe in both directions.
1968 bool UnconditionallySafe = KnownSafeTD && KnownSafeBU;
1969 if (UnconditionallySafe) {
1970 RetainsToMove.ReverseInsertPts.clear();
1971 ReleasesToMove.ReverseInsertPts.clear();
1972 NewCount = 0;
1973 } else {
1974 // Determine whether the new insertion points we computed preserve the
1975 // balance of retain and release calls through the program.
1976 // TODO: If the fully aggressive solution isn't valid, try to find a
1977 // less aggressive solution which is.
1978 if (NewDelta != 0)
1979 return false;
1980
1981 // At this point, we are not going to remove any RR pairs, but we still are
1982 // able to move RR pairs. If one of our pointers is afflicted with
1983 // CFGHazards, we cannot perform such code motion so exit early.
1984 const bool WillPerformCodeMotion =
1985 !RetainsToMove.ReverseInsertPts.empty() ||
1986 !ReleasesToMove.ReverseInsertPts.empty();
1987 if (CFGHazardAfflicted && WillPerformCodeMotion)
1988 return false;
1989 }
1990
1991 // Determine whether the original call points are balanced in the retain and
1992 // release calls through the program. If not, conservatively don't touch
1993 // them.
1994 // TODO: It's theoretically possible to do code motion in this case, as
1995 // long as the existing imbalances are maintained.
1996 if (OldDelta != 0)
1997 return false;
1998
1999 Changed = true;
2000 assert(OldCount != 0 && "Unreachable code?");
2001 NumRRs += OldCount - NewCount;
2002 // Set to true if we completely removed any RR pairs.
2003 AnyPairsCompletelyEliminated = NewCount == 0;
2004
2005 // We can move calls!
2006 return true;
2007}
2008
2009/// Identify pairings between the retains and releases, and delete and/or move
2010/// them.
2011bool ObjCARCOpt::PerformCodePlacement(
2012 DenseMap<const BasicBlock *, BBState> &BBStates,
2013 BlotMapVector<Value *, RRInfo> &Retains,
2014 DenseMap<Value *, RRInfo> &Releases, Module *M) {
2015 LLVM_DEBUG(dbgs() << "\n== ObjCARCOpt::PerformCodePlacement ==\n");
2016
2017 bool AnyPairsCompletelyEliminated = false;
2018 SmallVector<Instruction *, 8> DeadInsts;
2019
2020 // Visit each retain.
2022 E = Retains.end();
2023 I != E; ++I) {
2024 Value *V = I->first;
2025 if (!V) continue; // blotted
2026
2028
2029 LLVM_DEBUG(dbgs() << "Visiting: " << *Retain << "\n");
2030
2032
2033 // If the object being released is in static or stack storage, we know it's
2034 // not being managed by ObjC reference counting, so we can delete pairs
2035 // regardless of what possible decrements or uses lie between them.
2036 bool KnownSafe = isa<Constant>(Arg) || isa<AllocaInst>(Arg);
2037
2038 // A constant pointer can't be pointing to an object on the heap. It may
2039 // be reference-counted, but it won't be deleted.
2040 if (const LoadInst *LI = dyn_cast<LoadInst>(Arg))
2041 if (const GlobalVariable *GV =
2043 GetRCIdentityRoot(LI->getPointerOperand())))
2044 if (GV->isConstant())
2045 KnownSafe = true;
2046
2047 // Connect the dots between the top-down-collected RetainsToMove and
2048 // bottom-up-collected ReleasesToMove to form sets of related calls.
2049 RRInfo RetainsToMove, ReleasesToMove;
2050
2051 bool PerformMoveCalls = PairUpRetainsAndReleases(
2052 BBStates, Retains, Releases, M, Retain, DeadInsts,
2053 RetainsToMove, ReleasesToMove, Arg, KnownSafe,
2054 AnyPairsCompletelyEliminated);
2055
2056 if (PerformMoveCalls) {
2057 // Ok, everything checks out and we're all set. Let's move/delete some
2058 // code!
2059 MoveCalls(Arg, RetainsToMove, ReleasesToMove,
2060 Retains, Releases, DeadInsts, M);
2061 }
2062 }
2063
2064 // Now that we're done moving everything, we can delete the newly dead
2065 // instructions, as we no longer need them as insert points.
2066 while (!DeadInsts.empty())
2067 EraseInstruction(DeadInsts.pop_back_val());
2068
2069 return AnyPairsCompletelyEliminated;
2070}
2071
2072/// Weak pointer optimizations.
2073void ObjCARCOpt::OptimizeWeakCalls(Function &F) {
2074 LLVM_DEBUG(dbgs() << "\n== ObjCARCOpt::OptimizeWeakCalls ==\n");
2075
2076 // First, do memdep-style RLE and S2L optimizations. We can't use memdep
2077 // itself because it uses AliasAnalysis and we need to do provenance
2078 // queries instead.
2079 for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
2080 Instruction *Inst = &*I++;
2081
2082 LLVM_DEBUG(dbgs() << "Visiting: " << *Inst << "\n");
2083
2085 if (Class != ARCInstKind::LoadWeak &&
2086 Class != ARCInstKind::LoadWeakRetained)
2087 continue;
2088
2089 // Delete objc_loadWeak calls with no users.
2090 if (Class == ARCInstKind::LoadWeak && Inst->use_empty()) {
2091 Inst->eraseFromParent();
2092 Changed = true;
2093 continue;
2094 }
2095
2096 // TODO: For now, just look for an earlier available version of this value
2097 // within the same block. Theoretically, we could do memdep-style non-local
2098 // analysis too, but that would want caching. A better approach would be to
2099 // use the technique that EarlyCSE uses.
2100 inst_iterator Current = std::prev(I);
2101 BasicBlock *CurrentBB = &*Current.getBasicBlockIterator();
2102 for (BasicBlock::iterator B = CurrentBB->begin(),
2103 J = Current.getInstructionIterator();
2104 J != B; --J) {
2105 Instruction *EarlierInst = &*std::prev(J);
2106 ARCInstKind EarlierClass = GetARCInstKind(EarlierInst);
2107 switch (EarlierClass) {
2108 case ARCInstKind::LoadWeak:
2109 case ARCInstKind::LoadWeakRetained: {
2110 // If this is loading from the same pointer, replace this load's value
2111 // with that one.
2112 CallInst *Call = cast<CallInst>(Inst);
2113 CallInst *EarlierCall = cast<CallInst>(EarlierInst);
2114 Value *Arg = Call->getArgOperand(0);
2115 Value *EarlierArg = EarlierCall->getArgOperand(0);
2116 switch (PA.getAA()->alias(Arg, EarlierArg)) {
2118 Changed = true;
2119 // If the load has a builtin retain, insert a plain retain for it.
2120 if (Class == ARCInstKind::LoadWeakRetained) {
2121 Function *Decl = EP.get(ARCRuntimeEntryPointKind::Retain);
2122 CallInst *CI =
2123 CallInst::Create(Decl, EarlierCall, "", Call->getIterator());
2124 CI->setTailCall();
2125 }
2126 // Zap the fully redundant load.
2127 Call->replaceAllUsesWith(EarlierCall);
2129 goto clobbered;
2132 goto clobbered;
2134 break;
2135 }
2136 break;
2137 }
2138 case ARCInstKind::StoreWeak:
2139 case ARCInstKind::InitWeak: {
2140 // If this is storing to the same pointer and has the same size etc.
2141 // replace this load's value with the stored value.
2142 CallInst *Call = cast<CallInst>(Inst);
2143 CallInst *EarlierCall = cast<CallInst>(EarlierInst);
2144 Value *Arg = Call->getArgOperand(0);
2145 Value *EarlierArg = EarlierCall->getArgOperand(0);
2146 switch (PA.getAA()->alias(Arg, EarlierArg)) {
2148 Changed = true;
2149 // If the load has a builtin retain, insert a plain retain for it.
2150 if (Class == ARCInstKind::LoadWeakRetained) {
2151 Function *Decl = EP.get(ARCRuntimeEntryPointKind::Retain);
2152 CallInst *CI =
2153 CallInst::Create(Decl, EarlierCall, "", Call->getIterator());
2154 CI->setTailCall();
2155 }
2156 // Zap the fully redundant load.
2157 Call->replaceAllUsesWith(EarlierCall->getArgOperand(1));
2159 goto clobbered;
2162 goto clobbered;
2164 break;
2165 }
2166 break;
2167 }
2168 case ARCInstKind::MoveWeak:
2169 case ARCInstKind::CopyWeak:
2170 // TOOD: Grab the copied value.
2171 goto clobbered;
2172 case ARCInstKind::AutoreleasepoolPush:
2173 case ARCInstKind::None:
2174 case ARCInstKind::IntrinsicUser:
2175 case ARCInstKind::User:
2176 // Weak pointers are only modified through the weak entry points
2177 // (and arbitrary calls, which could call the weak entry points).
2178 break;
2179 default:
2180 // Anything else could modify the weak pointer.
2181 goto clobbered;
2182 }
2183 }
2184 clobbered:;
2185 }
2186
2187 // Then, for each destroyWeak with an alloca operand, check to see if
2188 // the alloca and all its users can be zapped.
2189 for (Instruction &Inst : llvm::make_early_inc_range(instructions(F))) {
2191 if (Class != ARCInstKind::DestroyWeak)
2192 continue;
2193
2194 CallInst *Call = cast<CallInst>(&Inst);
2195 Value *Arg = Call->getArgOperand(0);
2196 if (AllocaInst *Alloca = dyn_cast<AllocaInst>(Arg)) {
2197 for (User *U : Alloca->users()) {
2198 const Instruction *UserInst = cast<Instruction>(U);
2199 switch (GetBasicARCInstKind(UserInst)) {
2200 case ARCInstKind::InitWeak:
2201 case ARCInstKind::StoreWeak:
2202 case ARCInstKind::DestroyWeak:
2203 continue;
2204 default:
2205 goto done;
2206 }
2207 }
2208 Changed = true;
2209 for (User *U : llvm::make_early_inc_range(Alloca->users())) {
2210 CallInst *UserInst = cast<CallInst>(U);
2211 switch (GetBasicARCInstKind(UserInst)) {
2212 case ARCInstKind::InitWeak:
2213 case ARCInstKind::StoreWeak:
2214 // These functions return their second argument.
2215 UserInst->replaceAllUsesWith(UserInst->getArgOperand(1));
2216 break;
2217 case ARCInstKind::DestroyWeak:
2218 // No return value.
2219 break;
2220 default:
2221 llvm_unreachable("alloca really is used!");
2222 }
2223 UserInst->eraseFromParent();
2224 }
2225 Alloca->eraseFromParent();
2226 done:;
2227 }
2228 }
2229}
2230
2231/// Identify program paths which execute sequences of retains and releases which
2232/// can be eliminated.
2233bool ObjCARCOpt::OptimizeSequences(Function &F) {
2234 // Releases, Retains - These are used to store the results of the main flow
2235 // analysis. These use Value* as the key instead of Instruction* so that the
2236 // map stays valid when we get around to rewriting code and calls get
2237 // replaced by arguments.
2238 DenseMap<Value *, RRInfo> Releases;
2239 BlotMapVector<Value *, RRInfo> Retains;
2240
2241 // This is used during the traversal of the function to track the
2242 // states for each identified object at each block.
2243 DenseMap<const BasicBlock *, BBState> BBStates;
2244
2245 // Analyze the CFG of the function, and all instructions.
2246 bool NestingDetected = Visit(F, BBStates, Retains, Releases);
2247
2248 if (DisableRetainReleasePairing)
2249 return false;
2250
2251 // Transform.
2252 bool AnyPairsCompletelyEliminated = PerformCodePlacement(BBStates, Retains,
2253 Releases,
2254 F.getParent());
2255
2256 return AnyPairsCompletelyEliminated && NestingDetected;
2257}
2258
2259/// Check if there is a dependent call earlier that does not have anything in
2260/// between the Retain and the call that can affect the reference count of their
2261/// shared pointer argument. Note that Retain need not be in BB.
2264 ProvenanceAnalysis &PA) {
2266 CanChangeRetainCount, Arg, Retain->getParent(), Retain, PA));
2267
2268 // Check that the pointer is the return value of the call.
2269 if (!Call || Arg != Call)
2270 return nullptr;
2271
2272 // Check that the call is a regular call.
2274 return Class == ARCInstKind::CallOrUser || Class == ARCInstKind::Call
2275 ? Call
2276 : nullptr;
2277}
2278
2279/// Find a dependent retain that precedes the given autorelease for which there
2280/// is nothing in between the two instructions that can affect the ref count of
2281/// Arg.
2282static CallInst *
2285 ProvenanceAnalysis &PA) {
2288
2289 // Check that we found a retain with the same argument.
2291 GetArgRCIdentityRoot(Retain) != Arg) {
2292 return nullptr;
2293 }
2294
2295 return Retain;
2296}
2297
2298/// Look for an ``autorelease'' instruction dependent on Arg such that there are
2299/// no instructions dependent on Arg that need a positive ref count in between
2300/// the autorelease and the ret.
2301static CallInst *FindPredecessorAutoreleaseWithSafePath(
2302 const Value *Arg, BasicBlock *BB, ReturnInst *Ret, ProvenanceAnalysis &PA) {
2305
2306 if (!Autorelease)
2307 return nullptr;
2308 ARCInstKind AutoreleaseClass = GetBasicARCInstKind(Autorelease);
2309 if (!IsAutorelease(AutoreleaseClass))
2310 return nullptr;
2312 return nullptr;
2313
2314 return Autorelease;
2315}
2316
2317/// Look for this pattern:
2318/// \code
2319/// %call = call i8* @something(...)
2320/// %2 = call i8* @objc_retain(i8* %call)
2321/// %3 = call i8* @objc_autorelease(i8* %2)
2322/// ret i8* %3
2323/// \endcode
2324/// And delete the retain and autorelease.
2325void ObjCARCOpt::OptimizeReturns(Function &F) {
2326 if (!F.getReturnType()->isPointerTy())
2327 return;
2328
2329 LLVM_DEBUG(dbgs() << "\n== ObjCARCOpt::OptimizeReturns ==\n");
2330
2331 for (BasicBlock &BB: F) {
2332 ReturnInst *Ret = dyn_cast<ReturnInst>(&BB.back());
2333 if (!Ret)
2334 continue;
2335
2336 LLVM_DEBUG(dbgs() << "Visiting: " << *Ret << "\n");
2337
2338 const Value *Arg = GetRCIdentityRoot(Ret->getOperand(0));
2339
2340 // Look for an ``autorelease'' instruction that is a predecessor of Ret and
2341 // dependent on Arg such that there are no instructions dependent on Arg
2342 // that need a positive ref count in between the autorelease and Ret.
2344 FindPredecessorAutoreleaseWithSafePath(Arg, &BB, Ret, PA);
2345
2347 continue;
2348
2350 Arg, Autorelease->getParent(), Autorelease, PA);
2351
2352 if (!Retain)
2353 continue;
2354
2355 // Check that there is nothing that can affect the reference count
2356 // between the retain and the call. Note that Retain need not be in BB.
2358
2359 // Don't remove retainRV/autoreleaseRV pairs if the call isn't a tail call.
2360 if (!Call ||
2361 (!Call->isTailCall() &&
2364 continue;
2365
2366 // If so, we can zap the retain and autorelease.
2367 Changed = true;
2369 LLVM_DEBUG(dbgs() << "Erasing: " << *Retain << "\nErasing: " << *Autorelease
2370 << "\n");
2371 BundledInsts->eraseInst(Retain);
2373 }
2374}
2375
2376#ifndef NDEBUG
2377void
2378ObjCARCOpt::GatherStatistics(Function &F, bool AfterOptimization) {
2379 Statistic &NumRetains =
2380 AfterOptimization ? NumRetainsAfterOpt : NumRetainsBeforeOpt;
2381 Statistic &NumReleases =
2382 AfterOptimization ? NumReleasesAfterOpt : NumReleasesBeforeOpt;
2383
2384 for (inst_iterator I = inst_begin(&F), E = inst_end(&F); I != E; ) {
2385 Instruction *Inst = &*I++;
2386 switch (GetBasicARCInstKind(Inst)) {
2387 default:
2388 break;
2389 case ARCInstKind::Retain:
2390 ++NumRetains;
2391 break;
2392 case ARCInstKind::Release:
2393 ++NumReleases;
2394 break;
2395 }
2396 }
2397}
2398#endif
2399
2400void ObjCARCOpt::init(Function &F) {
2401 if (!EnableARCOpts)
2402 return;
2403
2404 // Intuitively, objc_retain and others are nocapture, however in practice
2405 // they are not, because they return their argument value. And objc_release
2406 // calls finalizers which can have arbitrary side effects.
2407 MDKindCache.init(F.getParent());
2408
2409 // Initialize our runtime entry point cache.
2410 EP.init(F.getParent());
2411
2412 // Compute which blocks are in which funclet.
2413 if (F.hasPersonalityFn() &&
2414 isScopedEHPersonality(classifyEHPersonality(F.getPersonalityFn())))
2415 BlockEHColors = colorEHFunclets(F);
2416}
2417
2418bool ObjCARCOpt::run(Function &F, AAResults &AA) {
2419 if (!EnableARCOpts)
2420 return false;
2421
2422 Changed = CFGChanged = false;
2423 BundledRetainClaimRVs BRV(EP, /*ContractPass=*/false, /*UseClaimRV=*/false);
2424 BundledInsts = &BRV;
2425
2426 LLVM_DEBUG(dbgs() << "<<< ObjCARCOpt: Visiting Function: " << F.getName()
2427 << " >>>"
2428 "\n");
2429
2430 std::pair<bool, bool> R = BundledInsts->insertAfterInvokes(F, nullptr);
2431 Changed |= R.first;
2432 CFGChanged |= R.second;
2433
2434 PA.setAA(&AA);
2435
2436#ifndef NDEBUG
2437 if (AreStatisticsEnabled()) {
2438 GatherStatistics(F, false);
2439 }
2440#endif
2441
2442 // This pass performs several distinct transformations. As a compile-time aid
2443 // when compiling code that isn't ObjC, skip these if the relevant ObjC
2444 // library functions aren't declared.
2445
2446 // Preliminary optimizations. This also computes UsedInThisFunction.
2447 OptimizeIndividualCalls(F);
2448
2449 // Optimizations for weak pointers.
2450 if (UsedInThisFunction & ((1 << unsigned(ARCInstKind::LoadWeak)) |
2451 (1 << unsigned(ARCInstKind::LoadWeakRetained)) |
2452 (1 << unsigned(ARCInstKind::StoreWeak)) |
2453 (1 << unsigned(ARCInstKind::InitWeak)) |
2454 (1 << unsigned(ARCInstKind::CopyWeak)) |
2455 (1 << unsigned(ARCInstKind::MoveWeak)) |
2456 (1 << unsigned(ARCInstKind::DestroyWeak))))
2457 OptimizeWeakCalls(F);
2458
2459 // Optimizations for retain+release pairs.
2460 if (UsedInThisFunction & ((1 << unsigned(ARCInstKind::Retain)) |
2461 (1 << unsigned(ARCInstKind::RetainRV)) |
2462 (1 << unsigned(ARCInstKind::RetainBlock))))
2463 if (UsedInThisFunction & (1 << unsigned(ARCInstKind::Release)))
2464 // Run OptimizeSequences until it either stops making changes or
2465 // no retain+release pair nesting is detected.
2466 while (OptimizeSequences(F)) {}
2467
2468 // Optimizations if objc_autorelease is used.
2469 if (UsedInThisFunction & ((1 << unsigned(ARCInstKind::Autorelease)) |
2470 (1 << unsigned(ARCInstKind::AutoreleaseRV))))
2471 OptimizeReturns(F);
2472
2473 // Optimizations for autorelease pools.
2474 if (UsedInThisFunction & ((1 << unsigned(ARCInstKind::AutoreleasepoolPush)) |
2475 (1 << unsigned(ARCInstKind::AutoreleasepoolPop))))
2476 OptimizeAutoreleasePools(F);
2477
2478 // Gather statistics after optimization.
2479#ifndef NDEBUG
2480 if (AreStatisticsEnabled()) {
2481 GatherStatistics(F, true);
2482 }
2483#endif
2484
2485 LLVM_DEBUG(dbgs() << "\n");
2486
2487 return Changed;
2488}
2489
2490/// Interprocedurally determine if calls made by the given call site can
2491/// possibly produce autoreleases.
2492bool MayAutorelease(const CallBase &CB, unsigned Depth = 0) {
2493 if (CB.onlyReadsMemory())
2494 return false;
2495
2496 // This recursion depth limit is arbitrary. It's just great
2497 // enough to cover known interesting testcases.
2498 if (Depth > 5)
2499 return true;
2500
2501 if (const Function *Callee = CB.getCalledFunction()) {
2502 if (!Callee->hasExactDefinition())
2503 return true;
2504 for (const BasicBlock &BB : *Callee) {
2505 for (const Instruction &I : BB) {
2506 // TODO: Ignore all instructions between autorelease pools
2507 ARCInstKind InstKind = GetBasicARCInstKind(&I);
2508 switch (InstKind) {
2514 // These may produce autoreleases
2515 return true;
2516
2532 // These ObjC runtime functions don't produce autoreleases
2533 break;
2534
2536 case ARCInstKind::Call:
2537 // For non-ObjC function calls, recursively analyze
2539 return true;
2540 break;
2541
2543 case ARCInstKind::User:
2544 case ARCInstKind::None:
2545 // These are not relevant for autorelease analysis
2546 break;
2547 }
2548 }
2549 }
2550 return false;
2551 }
2552
2553 return true;
2554}
2555
2556/// Optimize autorelease pools by eliminating empty push/pop pairs.
2557void ObjCARCOpt::OptimizeAutoreleasePools(Function &F) {
2558 LLVM_DEBUG(dbgs() << "\n== ObjCARCOpt::OptimizeAutoreleasePools ==\n");
2559
2560 OptimizationRemarkEmitter ORE(&F);
2561
2562 // Process each basic block independently.
2563 // TODO: Can we optimize inter-block autorelease pool pairs?
2564 // This would involve tracking autorelease pool state across blocks.
2565 for (BasicBlock &BB : F) {
2566 // Use a stack to track nested autorelease pools
2568 PoolStack; // {push_inst, has_autorelease_in_scope}
2569
2570 for (Instruction &Inst : llvm::make_early_inc_range(BB)) {
2572
2573 switch (Class) {
2574 case ARCInstKind::AutoreleasepoolPush: {
2575 // Start tracking a new autorelease pool scope
2576 auto *Push = cast<CallInst>(&Inst);
2577 PoolStack.push_back(
2578 {Push, false}); // {push_inst, has_autorelease_in_scope}
2579 LLVM_DEBUG(dbgs() << "Found autorelease pool push: " << *Push << "\n");
2580 break;
2581 }
2582
2583 case ARCInstKind::AutoreleasepoolPop: {
2584 auto *Pop = cast<CallInst>(&Inst);
2585
2586 if (PoolStack.empty())
2587 break;
2588
2589 auto &TopPool = PoolStack.back();
2590 CallInst *PendingPush = TopPool.first;
2591 bool HasAutoreleaseInScope = TopPool.second;
2592
2593 // Pop the stack - remove this pool scope
2594 PoolStack.pop_back();
2595
2596 // Bail if this pop doesn't match the pending push
2597 if (Pop->getArgOperand(0)->stripPointerCasts() != PendingPush)
2598 break;
2599
2600 // Bail if there were autoreleases in this scope
2601 if (HasAutoreleaseInScope)
2602 break;
2603
2604 // Optimize: eliminate this empty autorelease pool pair
2605 ORE.emit([&]() {
2606 return OptimizationRemark(DEBUG_TYPE, "AutoreleasePoolElimination",
2607 PendingPush)
2608 << "eliminated empty autorelease pool pair";
2609 });
2610
2611 // Replace all uses of push with poison before deletion
2612 PendingPush->replaceAllUsesWith(
2613 PoisonValue::get(PendingPush->getType()));
2614
2615 Pop->eraseFromParent();
2616 PendingPush->eraseFromParent();
2617
2618 Changed = true;
2619 ++NumNoops;
2620 break;
2621 }
2622 case ARCInstKind::CallOrUser:
2623 case ARCInstKind::Call:
2624 if (!MayAutorelease(cast<CallBase>(Inst)))
2625 break;
2626 [[fallthrough]];
2627 case ARCInstKind::Autorelease:
2628 case ARCInstKind::AutoreleaseRV:
2629 case ARCInstKind::FusedRetainAutorelease:
2630 case ARCInstKind::FusedRetainAutoreleaseRV:
2631 case ARCInstKind::LoadWeak: {
2632 // Track that we have autorelease calls in the current pool scope
2633 if (!PoolStack.empty()) {
2634 PoolStack.back().second = true; // Set has_autorelease_in_scope = true
2635 LLVM_DEBUG(
2636 dbgs()
2637 << "Found autorelease or potential autorelease in pool scope: "
2638 << Inst << "\n");
2639 }
2640 break;
2641 }
2642
2643 // Enumerate all remaining ARCInstKind cases explicitly
2644 case ARCInstKind::Retain:
2645 case ARCInstKind::RetainRV:
2646 case ARCInstKind::UnsafeClaimRV:
2647 case ARCInstKind::RetainBlock:
2648 case ARCInstKind::Release:
2649 case ARCInstKind::NoopCast:
2650 case ARCInstKind::LoadWeakRetained:
2651 case ARCInstKind::StoreWeak:
2652 case ARCInstKind::InitWeak:
2653 case ARCInstKind::MoveWeak:
2654 case ARCInstKind::CopyWeak:
2655 case ARCInstKind::DestroyWeak:
2656 case ARCInstKind::StoreStrong:
2657 case ARCInstKind::IntrinsicUser:
2658 case ARCInstKind::User:
2659 case ARCInstKind::None:
2660 // These instruction kinds don't affect autorelease pool optimization
2661 break;
2662 }
2663 }
2664 }
2665}
2666
2667/// @}
2668///
2669
2672 ObjCARCOpt OCAO;
2673 OCAO.init(F);
2674
2675 bool Changed = OCAO.run(F, AM.getResult<AAManager>(F));
2676 bool CFGChanged = OCAO.hasCFGChanged();
2677 if (Changed) {
2679 if (!CFGChanged)
2681 return PA;
2682 }
2683 return PreservedAnalyses::all();
2684}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file contains a class ARCRuntimeEntryPoints for use in creating/managing references to entry poi...
Expand Atomic instructions
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
This file defines the DenseMap class.
This file declares special dependency analysis routines used in Objective C ARC Optimizations.
#define DEBUG_TYPE
Hexagon Common GEP
IRTranslator LLVM IR MI
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
iv Induction Variable Users
Definition IVUsers.cpp:48
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
Machine Check Debug Module
This file contains the declarations for metadata subclasses.
uint64_t IntrinsicInst * II
This file defines common analysis utilities used by the ObjC ARC Optimizer.
static cl::opt< unsigned > MaxPtrStates("arc-opt-max-ptr-states", cl::Hidden, cl::desc("Maximum number of ptr states the optimizer keeps track of"), cl::init(4095))
This file defines ARC utility functions which are used by various parts of the compiler.
#define P(N)
This file declares a special form of Alias Analysis called Provenance / Analysis''.
This file contains some templates that are useful if you are working with the STL at all.
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
This file defines the 'Statistic' class, which is designed to be an easy way to expose various metric...
#define STATISTIC(VARNAME, DESC)
Definition Statistic.h:171
#define LLVM_DEBUG(...)
Definition Debug.h:114
void setAA(AAResults *aa)
AAResults * getAA() const
A manager for alias analyses.
LLVM_ABI AliasResult alias(const MemoryLocation &LocA, const MemoryLocation &LocB)
The main low level interface to the alias analysis implementation.
@ MayAlias
The two locations may or may not alias.
@ NoAlias
The two locations do not alias at all.
@ PartialAlias
The two locations alias, but only due to a partial overlap.
@ MustAlias
The two locations precisely alias each other.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
LLVM Basic Block Representation.
Definition BasicBlock.h:62
iterator end()
Definition BasicBlock.h:483
iterator begin()
Instruction iterator methods.
Definition BasicBlock.h:470
const Instruction & back() const
Definition BasicBlock.h:495
InstListType::const_iterator const_iterator
Definition BasicBlock.h:171
LLVM_ABI bool hasNPredecessors(unsigned N) const
Return true if this block has exactly N predecessors.
InstListType::iterator iterator
Instruction iterators...
Definition BasicBlock.h:170
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction if the block is well formed or null if the block is not well forme...
Definition BasicBlock.h:233
This class represents a no-op cast from one type to another.
An associative container with fast insertion-order (deterministic) iteration over its elements.
void blot(const KeyT &Key)
This is similar to erase, but instead of removing the element from the vector, it just zeros out the ...
iterator find(const KeyT &Key)
typename VectorTy::const_iterator const_iterator
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &InsertPair)
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
void setDoesNotThrow()
OperandBundleUse getOperandBundleAt(unsigned Index) const
Return the operand bundle at a specific index.
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
unsigned getNumOperandBundles() const
Return the number of operand bundles associated with this User.
bool onlyReadsMemory(unsigned OpNo) const
Value * getArgOperand(unsigned i) const
void setArgOperand(unsigned i, Value *v)
void setCalledFunction(Function *Fn)
Sets the function called, including updating the function type.
This class represents a function call, abstracting a target machine's calling convention.
static CallInst * Create(FunctionType *Ty, Value *F, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
void setTailCall(bool IsTc=true)
static LLVM_ABI ConstantInt * getTrue(LLVMContext &Context)
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:178
bool erase(const KeyT &Val)
Definition DenseMap.h:330
bool empty() const
Definition DenseMap.h:109
iterator end()
Definition DenseMap.h:81
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
BIty & getInstructionIterator()
BBIty & getBasicBlockIterator()
LLVM_ABI unsigned getNumSuccessors() const LLVM_READONLY
Return the number of successors that this instruction has.
LLVM_ABI void insertBefore(InstListType::iterator InsertPos)
Insert an unlinked instruction into a basic block immediately before the specified position.
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
MDNode * getMetadata(unsigned KindID) const
Get the metadata of given kind attached to this Instruction.
LLVM_ABI void setMetadata(unsigned KindID, MDNode *Node)
Set the metadata of the specified kind to the specified node.
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1572
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
op_range incoming_values()
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
unsigned getNumIncomingValues() const
Return the number of incoming edges.
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition Analysis.h:151
bool erase(PtrType Ptr)
Remove pointer from the set.
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
reference emplace_back(ArgTypes &&... Args)
typename SuperClass::const_iterator const_iterator
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
unsigned size() const
bool isVoidTy() const
Return true if this is 'void'.
Definition Type.h:139
Value * getOperand(unsigned i) const
Definition User.h:207
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:256
bool hasOneUse() const
Return true if there is exactly one use of this value.
Definition Value.h:440
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition Value.cpp:553
LLVMContext & getContext() const
All values hold a context through their type.
Definition Value.h:259
iterator_range< user_iterator > users()
Definition Value.h:427
bool use_empty() const
Definition Value.h:347
const ParentTy * getParent() const
Definition ilist_node.h:34
self_iterator getIterator()
Definition ilist_node.h:123
A cache of MDKinds used by various ARC optimizations.
Declarations for ObjC runtime functions and constants.
Function * get(ARCRuntimeEntryPointKind kind)
bool contains(const Instruction *I) const
See if an instruction is a bundled retainRV/claimRV call.
Definition ObjCARC.h:128
std::pair< bool, bool > insertAfterInvokes(Function &F, DominatorTree *DT)
Insert a retainRV/claimRV call to the normal destination blocks of invokes with operand bundle "clang...
Definition ObjCARC.cpp:44
CallInst * insertRVCall(BasicBlock::iterator InsertPt, CallBase *AnnotatedCall)
Insert a retainRV/claimRV call.
Definition ObjCARC.cpp:74
void eraseInst(CallInst *CI)
Remove a retainRV/claimRV call entirely.
Definition ObjCARC.h:135
This class summarizes several per-pointer runtime properties which are propagated through the flow gr...
Definition PtrState.h:100
void SetCFGHazardAfflicted(const bool NewValue)
Definition PtrState.h:138
Sequence GetSeq() const
Definition PtrState.h:149
const RRInfo & GetRRInfo() const
Definition PtrState.h:164
bool IsKnownSafe() const
Definition PtrState.h:118
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
static void CheckForUseCFGHazard(const Sequence SuccSSeq, const bool SuccSRRIKnownSafe, TopDownPtrState &S, bool &SomeSuccHasSame, bool &AllSuccsHaveSame, bool &NotAllSeqEqualButKnownSafe, bool &ShouldContinue)
If we have a top down pointer in the S_Use state, make sure that there are no CFG hazards by checking...
NumRets
static void CheckForCanReleaseCFGHazard(const Sequence SuccSSeq, const bool SuccSRRIKnownSafe, TopDownPtrState &S, bool &SomeSuccHasSame, bool &AllSuccsHaveSame, bool &NotAllSeqEqualButKnownSafe)
If we have a Top Down pointer in the S_CanRelease state, make sure that there are no CFG hazards by c...
static bool isInertARCValue(Value *V, SmallPtrSet< Value *, 1 > &VisitedPhis)
This function returns true if the value is inert.
CallInst * Retain
CallInst * Call
static void collectReleaseInsertPts(const BlotMapVector< Value *, RRInfo > &Retains, DenseMap< const Instruction *, SmallPtrSet< const Value *, 2 > > &ReleaseInsertPtToRCIdentityRoots)
Changed
CallInst * Autorelease
Look for an `‘autorelease’' instruction dependent on Arg such that there are / no instructions depend...
static void ComputePostOrders(Function &F, SmallVectorImpl< BasicBlock * > &PostOrder, SmallVectorImpl< BasicBlock * > &ReverseCFGPostOrder, unsigned NoObjCARCExceptionsMDKind, DenseMap< const BasicBlock *, BBState > &BBStates)
static CallInst * FindPredecessorRetainWithSafePath(const Value *Arg, BasicBlock *BB, Instruction *Autorelease, ProvenanceAnalysis &PA)
Find a dependent retain that precedes the given autorelease for which there is nothing in between the...
static const SmallPtrSet< const Value *, 2 > * getRCIdentityRootsFromReleaseInsertPt(const Instruction *InsertPt, const DenseMap< const Instruction *, SmallPtrSet< const Value *, 2 > > &ReleaseInsertPtToRCIdentityRoots)
bool MayAutorelease(const CallBase &CB, unsigned Depth=0)
Interprocedurally determine if calls made by the given call site can possibly produce autoreleases.
static const unsigned OverflowOccurredValue
static CallInst * HasSafePathToPredecessorCall(const Value *Arg, Instruction *Retain, ProvenanceAnalysis &PA)
Check if there is a dependent call earlier that does not have anything in between the Retain and the ...
static const Value * FindSingleUseIdentifiedObject(const Value *Arg)
This is similar to GetRCIdentityRoot but it stops as soon as it finds a value with multiple uses.
This file defines common definitions/declarations used by the ObjC ARC Optimizer.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:81
initializer< Ty > init(const Ty &Val)
PointerTypeMap run(const Module &M)
Compute the PointerTypeMap for the module M.
bool IsRetain(ARCInstKind Class)
Test if the given class is objc_retain or equivalent.
bool IsNeverTail(ARCInstKind Class)
Test if the given class represents instructions which are never safe to mark with the "tail" keyword.
bool IsAlwaysTail(ARCInstKind Class)
Test if the given class represents instructions which are always safe to mark with the "tail" keyword...
bool IsNullOrUndef(const Value *V)
bool IsAutorelease(ARCInstKind Class)
Test if the given class is objc_autorelease or equivalent.
ARCInstKind
Equivalence classes of instructions in the ARC Model.
@ DestroyWeak
objc_destroyWeak (derived)
@ FusedRetainAutorelease
objc_retainAutorelease
@ CallOrUser
could call objc_release and/or "use" pointers
@ StoreStrong
objc_storeStrong (derived)
@ LoadWeakRetained
objc_loadWeakRetained (primitive)
@ StoreWeak
objc_storeWeak (primitive)
@ AutoreleasepoolPop
objc_autoreleasePoolPop
@ AutoreleasepoolPush
objc_autoreleasePoolPush
@ InitWeak
objc_initWeak (derived)
@ Autorelease
objc_autorelease
@ LoadWeak
objc_loadWeak (derived)
@ None
anything that is inert from an ARC perspective.
@ MoveWeak
objc_moveWeak (derived)
@ User
could "use" a pointer
@ RetainRV
objc_retainAutoreleasedReturnValue
@ RetainBlock
objc_retainBlock
@ FusedRetainAutoreleaseRV
objc_retainAutoreleaseReturnValue
@ AutoreleaseRV
objc_autoreleaseReturnValue
@ Call
could call objc_release
@ CopyWeak
objc_copyWeak (derived)
@ NoopCast
objc_retainedObject, etc.
@ UnsafeClaimRV
objc_unsafeClaimAutoreleasedReturnValue
@ IntrinsicUser
llvm.objc.clang.arc.use
bool IsObjCIdentifiedObject(const Value *V)
Return true if this value refers to a distinct and identifiable object.
bool EnableARCOpts
A handy option to enable/disable all ARC Optimizations.
void getEquivalentPHIs(PHINodeTy &PN, VectorTy &PHIList)
Return the list of PHI nodes that are equivalent to PN.
Definition ObjCARC.h:75
bool IsForwarding(ARCInstKind Class)
Test if the given class represents instructions which return their argument verbatim.
bool IsNoopInstruction(const Instruction *I)
llvm::Instruction * findSingleDependency(DependenceKind Flavor, const Value *Arg, BasicBlock *StartBB, Instruction *StartInst, ProvenanceAnalysis &PA)
Find dependent instructions.
Sequence
A sequence of states that a pointer may go through in which an objc_retain and objc_release are actua...
Definition PtrState.h:41
@ S_CanRelease
foo(x) – x could possibly see a ref count decrement.
Definition PtrState.h:44
@ S_Use
any use of x.
Definition PtrState.h:45
@ S_Retain
objc_retain(x).
Definition PtrState.h:43
@ S_Stop
code motion is stopped.
Definition PtrState.h:46
@ S_MovableRelease
objc_release(x), !clang.imprecise_release.
Definition PtrState.h:47
ARCInstKind GetBasicARCInstKind(const Value *V)
Determine which objc runtime call instruction class V belongs to.
ARCInstKind GetARCInstKind(const Value *V)
Map V to its ARCInstKind equivalence class.
Value * GetArgRCIdentityRoot(Value *Inst)
Assuming the given instruction is one of the special calls such as objc_retain or objc_release,...
bool IsNoThrow(ARCInstKind Class)
Test if the given class represents instructions which are always safe to mark with the nounwind attri...
const Value * GetRCIdentityRoot(const Value *V)
The RCIdentity root of a value V is a dominating value U for which retaining or releasing U is equiva...
bool IsNoopOnGlobal(ARCInstKind Class)
Test if the given class represents instructions which do nothing if passed a global variable.
bool IsNoopOnNull(ARCInstKind Class)
Test if the given class represents instructions which do nothing if passed a null pointer.
bool hasAttachedCallOpBundle(const CallBase *CB)
Definition ObjCARCUtil.h:29
static void EraseInstruction(Instruction *CI)
Erase the given instruction.
Definition ObjCARC.h:40
friend class Instruction
Iterator for Instructions in a `BasicBlock.
Definition BasicBlock.h:73
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
InstIterator< SymbolTableList< BasicBlock >, Function::iterator, BasicBlock::iterator, Instruction > inst_iterator
auto pred_end(const MachineBasicBlock *BB)
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
auto successors(const MachineBasicBlock *BB)
LLVM_ABI DenseMap< BasicBlock *, ColorVector > colorEHFunclets(Function &F)
If an EH funclet personality is in use (see isFuncletEHPersonality), this will recompute which blocks...
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition STLExtras.h:634
inst_iterator inst_begin(Function *F)
bool isScopedEHPersonality(EHPersonality Pers)
Returns true if this personality uses scope-style EH IR instructions: catchswitch,...
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
NoopStatistic Statistic
Definition Statistic.h:162
auto reverse(ContainerTy &&C)
Definition STLExtras.h:408
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207
LLVM_ABI bool AreStatisticsEnabled()
Check if statistics are enabled.
LLVM_ABI EHPersonality classifyEHPersonality(const Value *Pers)
See if the given exception handling personality function is one that we understand.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
inst_iterator inst_end(Function *F)
RNSuccIterator< NodeRef, BlockT, RegionT > succ_begin(NodeRef Node)
@ Other
Any other memory.
Definition ModRef.h:68
RNSuccIterator< NodeRef, BlockT, RegionT > succ_end(NodeRef Node)
Instruction::succ_iterator succ_iterator
Definition CFG.h:138
DWARFExpression::Operation Op
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
TinyPtrVector< BasicBlock * > ColorVector
auto pred_begin(const MachineBasicBlock *BB)
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
bool is_contained(R &&Range, const E &Element)
Returns true if Element is found in Range.
Definition STLExtras.h:1947
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
LLVM_ABI PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)
A lightweight accessor for an operand bundle meant to be passed around by value.
bool HandlePotentialAlterRefCount(Instruction *Inst, const Value *Ptr, ProvenanceAnalysis &PA, ARCInstKind Class)
Definition PtrState.cpp:226
bool InitBottomUp(ARCMDKindCache &Cache, Instruction *I)
(Re-)Initialize this bottom up pointer returning true if we detected a pointer with nested releases.
Definition PtrState.cpp:174
bool MatchWithRetain()
Return true if this set of releases can be paired with a release.
Definition PtrState.cpp:203
void HandlePotentialUse(BasicBlock *BB, Instruction *Inst, const Value *Ptr, ProvenanceAnalysis &PA, ARCInstKind Class)
Definition PtrState.cpp:253
Unidirectional information about either a retain-decrement-use-release sequence or release-use-decrem...
Definition PtrState.h:55
bool KnownSafe
After an objc_retain, the reference count of the referenced object is known to be positive.
Definition PtrState.h:68
SmallPtrSet< Instruction *, 2 > Calls
For a top-down sequence, the set of objc_retains or objc_retainBlocks.
Definition PtrState.h:79
MDNode * ReleaseMetadata
If the Calls are objc_release calls and they all have a clang.imprecise_release tag,...
Definition PtrState.h:75
bool CFGHazardAfflicted
If this is true, we cannot perform code motion but can still remove retain/release pairs.
Definition PtrState.h:87
bool IsTailCallRelease
True of the objc_release calls are all marked with the "tail" keyword.
Definition PtrState.h:71
SmallPtrSet< Instruction *, 2 > ReverseInsertPts
The set of optimal insert positions for moving calls in the opposite sequence.
Definition PtrState.h:83
bool MatchWithRelease(ARCMDKindCache &Cache, Instruction *Release)
Return true if this set of retains can be paired with the given release.
Definition PtrState.cpp:349
bool InitTopDown(ARCInstKind Kind, Instruction *I)
(Re-)Initialize this bottom up pointer returning true if we detected a pointer with nested releases.
Definition PtrState.cpp:324
bool HandlePotentialAlterRefCount(Instruction *Inst, const Value *Ptr, ProvenanceAnalysis &PA, ARCInstKind Class, const BundledRetainClaimRVs &BundledRVs)
Definition PtrState.cpp:377
void HandlePotentialUse(Instruction *Inst, const Value *Ptr, ProvenanceAnalysis &PA, ARCInstKind Class)
Definition PtrState.cpp:416