Lines Matching +full:cancel +full:- +full:in +full:- +full:progress

1 //===- ObjCARCOpts.cpp - ObjC ARC Optimization ----------------------------===//
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
12 /// in Objective C.
21 /// WARNING: This file knows about how certain Objective-C library functions are
23 /// behavior-preserving may break these assumptions.
25 //===----------------------------------------------------------------------===//
73 #define DEBUG_TYPE "objc-arc-opts"
75 static cl::opt<unsigned> MaxPtrStates("arc-opt-max-ptr-states",
87 // modules. It's never a single-use value.
91 if (Arg->hasOneUse()) {
93 return FindSingleUseIdentifiedObject(BC->getOperand(0));
95 if (GEP->hasAllZeroIndices())
96 return FindSingleUseIdentifiedObject(GEP->getPointerOperand());
99 cast<CallInst>(Arg)->getArgOperand(0));
106 // trivial uses, we can still consider this to be a single-use value.
108 for (const User *U : Arg->users())
109 if (!U->use_empty() || GetRCIdentityRoot(U) != Arg)
138 // calls followed by objc_autoreleasePoolPop calls (perhaps in ObjC++ code
141 // TODO: Critical-edge splitting. If the optimial insertion point is
144 // think in terms of edges, rather than blocks, and then split critical
150 // non-escaping arguments and non-releasing arguments, and may be
151 // non-autoreleasing.
161 STATISTIC(NumNoops, "Number of no-op objc calls eliminated");
162 STATISTIC(NumPartialNoops, "Number of partially no-op objc calls eliminated");
167 STATISTIC(NumPeeps, "Number of calls peephole-optimized");
181 /// Per-BasicBlock state.
190 /// The top-down traversal uses this to record information known about a
194 /// The bottom-up traversal uses this to record information known about a
295 /// top-down and bottom-up traversals are complete.
346 /// The top-down traversal uses this to merge information about predecessors to
352 // Other.TopDownPathCount can be 0, in which case it is either dead or a
356 // In order to be consistent, we clear the top down pointers when by adding
372 // For each entry in the other set, if our set has an entry with the same key,
378 Pair.first->second.Merge(Pair.second ? TopDownPtrState() : MI->second,
382 // For each entry in our set, if the other set doesn't have an entry with the
385 if (Other.PerPtrTopDown.find(MI->first) == Other.PerPtrTopDown.end())
386 MI->second.Merge(TopDownPtrState(), /*TopDown=*/true);
389 /// The bottom-up traversal uses this to merge information about successors to
395 // Other.BottomUpPathCount can be 0, in which case it is either dead or a
399 // In order to be consistent, we clear the top down pointers when by adding
415 // For each entry in the other set, if our set has an entry with the
421 Pair.first->second.Merge(Pair.second ? BottomUpPtrState() : MI->second,
425 // For each entry in our set, if the other set doesn't have an entry
429 if (Other.PerPtrBottomUp.find(MI->first) == Other.PerPtrBottomUp.end())
430 MI->second.Merge(BottomUpPtrState(), /*TopDown=*/false);
441 const PtrState &P = I->second;
442 OS << " Ptr: " << *I->first
461 const PtrState &P = I->second;
462 OS << " Ptr: " << *I->first
500 /// is in fact used in the current function.
573 for (unsigned I = 0, E = CI->getNumOperandBundles(); I != E; ++I) {
574 OperandBundleUse B = CI->getOperandBundleAt(I);
583 const ColorVector &CV = BlockEHColors.find(BB)->second;
587 dyn_cast<FuncletPadInst>(EHPadBB->getFirstNonPHIIt())) {
612 if (Call->getParent() == RetainRV->getParent()) {
620 BasicBlock *RetainRVParent = RetainRV->getParent();
621 if (II->getNormalDest() == RetainRVParent) {
622 BasicBlock::const_iterator I = RetainRVParent->begin();
631 assert(!BundledInsts->contains(RetainRV) &&
644 cast<CallInst>(RetainRV)->setCalledFunction(NewDecl);
654 if (BundledInsts->contains(Inst))
657 // Must be in the same basic block.
658 assert(Inst->getParent() == AutoreleaseRV->getParent());
681 AutoreleaseRV->replaceAllUsesWith(
682 cast<CallInst>(AutoreleaseRV)->getArgOperand(0));
686 // AutoreleaseRV and RetainRV cancel out. Delete the RetainRV.
687 Inst->replaceAllUsesWith(cast<CallInst>(Inst)->getArgOperand(0));
693 // AutoreleaseRV and RetainRV cancel out, replace UnsafeClaimRV with Release.
695 Value *CallArg = cast<CallInst>(Inst)->getArgOperand(0);
698 Inst->getIterator());
701 Release->setTailCall();
702 Inst->replaceAllUsesWith(CallArg);
732 for (const User *U : Ptr->users()) {
752 AutoreleaseRVCI->setCalledFunction(NewDecl);
753 AutoreleaseRVCI->setTailCall(false); // Never tail call objc_autorelease.
763 // Reset all the flags in preparation for recomputing them.
788 // If we hit the end of the basic block we're not going to find an RV-pair.
790 if (NonARCInst->isTerminator())
803 return CB->getIntrinsicID() != Intrinsic::not_intrinsic;
806 // Visit all objc_* calls in F.
812 BundledInsts->insertRVCall(I->getIterator(), CI);
827 // This is a non-ARC instruction. If we're delaying an AutoreleaseRV,
840 // We have a potential RV pair. Check if they cancel out.
862 V = V->stripPointerCasts();
869 if (GV->hasAttribute("objc_arc_inert"))
877 for (Value *Opnd : PN->incoming_values())
894 if (BundledInsts->contains(Inst)) {
900 if (isInertARCValue(Inst->getOperand(0), VisitedPhis)) {
901 if (!Inst->getType()->isVoidTy())
902 Inst->replaceAllUsesWith(Inst->getOperand(0));
903 Inst->eraseFromParent();
912 // Delete no-op casts. These function calls have special semantics, but
913 // the semantics are entirely implemented via lowering in the front-end,
914 // so by the time they reach the optimizer, they are just no-op calls
917 // There are gray areas here, as the ability to cast reference-counted
923 LLVM_DEBUG(dbgs() << "Erasing no-op cast: " << *Inst << "\n");
927 // If the pointer-to-weak-pointer is null, it's undefined behavior.
934 if (IsNullOrUndef(CI->getArgOperand(0))) {
936 new StoreInst(ConstantInt::getTrue(CI->getContext()),
937 PoisonValue::get(PointerType::getUnqual(CI->getContext())),
938 CI->getIterator());
939 Value *NewValue = PoisonValue::get(CI->getType());
941 dbgs() << "A null pointer-to-weak-pointer is undefined behavior."
944 CI->replaceAllUsesWith(NewValue);
945 CI->eraseFromParent();
953 if (IsNullOrUndef(CI->getArgOperand(0)) ||
954 IsNullOrUndef(CI->getArgOperand(1))) {
956 new StoreInst(ConstantInt::getTrue(CI->getContext()),
957 PoisonValue::get(PointerType::getUnqual(CI->getContext())),
958 CI->getIterator());
960 Value *NewValue = PoisonValue::get(CI->getType());
962 dbgs() << "A null pointer-to-weak-pointer is undefined behavior."
966 CI->replaceAllUsesWith(NewValue);
967 CI->eraseFromParent();
981 // objc_autorelease(x) -> objc_release(x) if x is otherwise unused.
982 if (IsAutorelease(Class) && Inst->use_empty()) {
984 const Value *Arg = Call->getArgOperand(0);
991 LLVMContext &C = Inst->getContext();
994 CallInst *NewCall = CallInst::Create(Decl, Call->getArgOperand(0), "",
995 Call->getIterator());
996 NewCall->setMetadata(MDKindCache.get(ARCMDKindID::ImpreciseRelease),
1011 if (IsAlwaysTail(Class) && !cast<CallInst>(Inst)->isNoTailCall()) {
1017 cast<CallInst>(Inst)->setTailCall();
1026 cast<CallInst>(Inst)->setTailCall(false);
1034 cast<CallInst>(Inst)->setDoesNotThrow();
1047 // ARC calls with null are no-ops. Delete them.
1051 LLVM_DEBUG(dbgs() << "ARC calls with null are no-ops. Erasing: " << *Inst
1058 // are actually present in this function.
1062 // PHI are null, and the call is control-equivalent to the PHI, and there
1065 // call could be pushed up to just those paths with non-null incoming
1068 !Inst->getMetadata(MDKindCache.get(ARCMDKindID::ImpreciseRelease)))
1086 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
1087 Value *Incoming = GetRCIdentityRoot(PN->getIncomingValue(i));
1090 else if (PN->getIncomingBlock(i)->getTerminator()->getNumSuccessors() !=
1115 Inst->getParent(), Inst, PA);
1120 Inst->getParent(), Inst, PA);
1139 // Clone the call into each predecessor that has a non-null value.
1141 Type *ParamTy = CInst->getArgOperand(0)->getType();
1142 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
1143 Value *Incoming = GetRCIdentityRoot(PN->getIncomingValue(i));
1146 Value *Op = PN->getIncomingValue(i);
1148 PN->getIncomingBlock(i)->back().getIterator();
1153 addOpBundleForFunclet(InsertPos->getParent(), OpBundles);
1155 if (Op->getType() != ParamTy)
1157 Clone->setArgOperand(0, Op);
1158 Clone->insertBefore(*InsertPos->getParent(), InsertPos);
1171 /// If we have a top down pointer in the S_Use state, make sure that there are
1201 llvm_unreachable("bottom-up pointer in retain state!");
1207 /// If we have a Top Down pointer in the S_CanRelease state, make sure that
1229 llvm_unreachable("bottom-up pointer in retain state!");
1236 /// other CFG structures where moving code across the edge would result in it
1242 // If any top-down local-use or possible-dec has a succ which is earlier in
1246 TopDownPtrState &S = I->second;
1247 const Sequence Seq = I->second.GetSeq();
1253 // Make sure that if extra top down states are added in the future that this
1258 const Value *Arg = I->first;
1269 const BottomUpPtrState &SuccS = BBI->second.getPtrBottomUpState(Arg);
1272 // If bottom up, the pointer is in an S_None state, clear the sequence
1273 // progress since the sequence in the bottom up state finished
1274 // suggesting a mismatch in between retains/releases. This is true for
1313 // guards against loops in the middle of a sequence.
1344 // In OptimizeIndividualCalls, we have strength reduced all optimizable
1381 const Value *Ptr = MI->first;
1384 BottomUpPtrState &S = MI->second;
1411 MyStates.InitFromSucc(I->second);
1417 MyStates.MergeSucc(I->second);
1425 // Visit all the instructions, bottom-up.
1426 for (BasicBlock::iterator I = BB->end(), E = BB->begin(); I != E; --I) {
1438 // that this pass can complete in a reasonable amount of time.
1446 // part of this block, since we can't insert code after an invoke in its own
1451 if (InvokeInst *II = dyn_cast<InvokeInst>(&Pred->back()))
1471 Value *Root = GetRCIdentityRoot(Retain->getOperand(0));
1489 return &I->second;
1520 // In OptimizeIndividualCalls, we have strength reduced all optimizable
1537 // Try to form a tentative pair in between this release instruction and the
1540 // If we succeed, copy S's RRInfo into the Release -> {Retain Set
1565 const Value *Ptr = MI->first;
1568 TopDownPtrState &S = MI->second;
1595 MyStates.InitFromPred(I->second);
1601 MyStates.MergePred(I->second);
1608 if (!BB->hasNPredecessors(MyStates.pred_end() - MyStates.pred_begin()))
1612 I->second.SetCFGHazardAfflicted(true);
1618 // Visit all the instructions, top-down.
1626 // that this pass can complete in a reasonable amount of time.
1658 Instruction *EntryTI = EntryBB->getTerminator();
1665 succ_iterator SE(CurrBB->getTerminator(), false);
1671 std::make_pair(SuccBB, succ_iterator(SuccBB->getTerminator())));
1691 // Do reverse-CFG DFS, computing the reverse-CFG PostOrder.
1719 // Visit the function both top-down and bottom-up.
1724 // Use reverse-postorder traversals, because we magically know that loops
1727 // class here because we want the reverse-CFG postorder to consider each
1735 // Use reverse-postorder on the reverse CFG for bottom-up.
1747 // Use reverse-postorder for top-down.
1759 /// Move the calls in RetainsToMove and ReleasesToMove.
1772 addOpBundleForFunclet(InsertPt->getParent(), BundleList);
1774 CallInst::Create(Decl, Arg, BundleList, "", InsertPt->getIterator());
1775 Call->setDoesNotThrow();
1776 Call->setTailCall();
1786 addOpBundleForFunclet(InsertPt->getParent(), BundleList);
1788 CallInst::Create(Decl, Arg, BundleList, "", InsertPt->getIterator());
1791 Call->setMetadata(MDKindCache.get(ARCMDKindID::ImpreciseRelease), M);
1792 Call->setDoesNotThrow();
1794 Call->setTailCall();
1823 // If a pair happens in a region where it is known that the reference count
1829 // Connect the dots between the top-down-collected RetainsToMove and
1830 // bottom-up-collected ReleasesToMove to form sets of related calls.
1843 const RRInfo &NewRetainRRI = It->second;
1850 const RRInfo &NewRetainReleaseRRI = Jt->second;
1863 const BBState &NRRBBState = BBStates[NewRetainRelease->getParent()];
1870 OldDelta -= PathCount;
1894 const BBState &RIPBBState = BBStates[RIP->getParent()];
1901 NewDelta -= PathCount;
1915 const RRInfo &NewReleaseRRI = It->second;
1922 const RRInfo &NewReleaseRetainRRI = Jt->second;
1935 const BBState &NRRBBState = BBStates[NewReleaseRetain->getParent()];
1951 const BBState &RIPBBState = BBStates[RIP->getParent()];
1970 // We can only remove pointers if we are known safe in both directions.
1994 // Determine whether the original call points are balanced in the retain and
1997 // TODO: It's theoretically possible to do code motion in this case, as
2004 NumRRs += OldCount - NewCount;
2027 Value *V = I->first;
2036 // If the object being released is in static or stack storage, we know it's
2042 // be reference-counted, but it won't be deleted.
2046 GetRCIdentityRoot(LI->getPointerOperand())))
2047 if (GV->isConstant())
2050 // Connect the dots between the top-down-collected RetainsToMove and
2051 // bottom-up-collected ReleasesToMove to form sets of related calls.
2079 // First, do memdep-style RLE and S2L optimizations. We can't use memdep
2093 if (Class == ARCInstKind::LoadWeak && Inst->use_empty()) {
2094 Inst->eraseFromParent();
2100 // within the same block. Theoretically, we could do memdep-style non-local
2105 for (BasicBlock::iterator B = CurrentBB->begin(),
2107 J != B; --J) {
2117 Value *Arg = Call->getArgOperand(0);
2118 Value *EarlierArg = EarlierCall->getArgOperand(0);
2119 switch (PA.getAA()->alias(Arg, EarlierArg)) {
2126 CallInst::Create(Decl, EarlierCall, "", Call->getIterator());
2127 CI->setTailCall();
2130 Call->replaceAllUsesWith(EarlierCall);
2131 Call->eraseFromParent();
2147 Value *Arg = Call->getArgOperand(0);
2148 Value *EarlierArg = EarlierCall->getArgOperand(0);
2149 switch (PA.getAA()->alias(Arg, EarlierArg)) {
2156 CallInst::Create(Decl, EarlierCall, "", Call->getIterator());
2157 CI->setTailCall();
2160 Call->replaceAllUsesWith(EarlierCall->getArgOperand(1));
2161 Call->eraseFromParent();
2198 Value *Arg = Call->getArgOperand(0);
2200 for (User *U : Alloca->users()) {
2212 for (User *U : llvm::make_early_inc_range(Alloca->users())) {
2218 UserInst->replaceAllUsesWith(UserInst->getArgOperand(1));
2226 UserInst->eraseFromParent();
2228 Alloca->eraseFromParent();
2237 // Releases, Retains - These are used to store the results of the main flow
2262 /// Check if there is a dependent call earlier that does not have anything in
2264 /// shared pointer argument. Note that Retain need not be in BB.
2269 CanChangeRetainCount, Arg, Retain->getParent(), Retain, PA));
2283 /// is nothing in between the two instructions that can affect the ref count of
2302 /// no instructions dependent on Arg that need a positive ref count in between
2332 if (!F.getReturnType()->isPointerTy())
2344 const Value *Arg = GetRCIdentityRoot(Ret->getOperand(0));
2348 // that need a positive ref count in between the autorelease and Ret.
2356 Arg, Autorelease->getParent(), Autorelease, PA);
2362 // between the retain and the call. Note that Retain need not be in BB.
2367 (!Call->isTailCall() &&
2377 BundledInsts->eraseInst(Retain);
2410 // Intuitively, objc_retain and others are nocapture, however in practice
2418 // Compute which blocks are in which funclet.
2436 std::pair<bool, bool> R = BundledInsts->insertAfterInvokes(F, nullptr);
2448 // This pass performs several distinct transformations. As a compile-time aid