Skip to content

Commit 0429373

Browse files
committed
Add new apis to OwnershipLifetimeExtender
1 parent aa54818 commit 0429373

File tree

1 file changed

+35
-6
lines changed

1 file changed

+35
-6
lines changed

lib/SILOptimizer/Utils/OwnershipOptUtils.cpp

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,12 @@ struct OwnershipLifetimeExtender {
643643
SILBasicBlock::iterator borrowPoint,
644644
RangeTy guaranteedUsePoints);
645645

646+
template <typename RangeTy>
647+
BeginBorrowInst *
648+
borrowCopyOverGuaranteedUsers(SILValue newValue,
649+
SILBasicBlock::iterator borrowPoint,
650+
RangeTy guaranteedUsers);
651+
646652
/// Borrow \p newValue over the lifetime of \p guaranteedValue. Return the
647653
/// new guaranteed value.
648654
SILValue borrowOverValue(SILValue newValue, SILValue guaranteedValue);
@@ -654,6 +660,10 @@ struct OwnershipLifetimeExtender {
654660
/// the BorrowedValue that begins the scope.
655661
SILValue borrowOverSingleUse(SILValue newValue,
656662
Operand *singleGuaranteedUse);
663+
664+
SILValue
665+
borrowOverSingleNonLifetimeEndingUser(SILValue newValue,
666+
SILInstruction *nonLifetimeEndingUser);
657667
};
658668

659669
} // end anonymous namespace
@@ -780,9 +790,9 @@ OwnershipLifetimeExtender::borrowCopyOverScope(SILValue newValue,
780790
///
781791
/// Precondition: None of \p guaranteedUses are lifetime ending.
782792
template <typename RangeTy>
783-
BeginBorrowInst *OwnershipLifetimeExtender::borrowCopyOverGuaranteedUses(
793+
BeginBorrowInst *OwnershipLifetimeExtender::borrowCopyOverGuaranteedUsers(
784794
SILValue newValue, SILBasicBlock::iterator borrowPoint,
785-
RangeTy guaranteedUsePoints) {
795+
RangeTy guaranteedUsers) {
786796

787797
auto loc = RegularLocation::getAutoGeneratedLocation(borrowPoint->getLoc());
788798

@@ -794,12 +804,11 @@ BeginBorrowInst *OwnershipLifetimeExtender::borrowCopyOverGuaranteedUses(
794804

795805
// Create end_borrows at the end of the borrow's lifetime.
796806
{
797-
// We don't expect an empty guaranteedUsePoints. If it happens, then the
807+
// We don't expect an empty guaranteedUsers. If it happens, then the
798808
// newly created copy will never be destroyed.
799-
assert(!guaranteedUsePoints.empty());
809+
assert(!guaranteedUsers.empty());
800810

801-
auto opRange = makeUserRange(guaranteedUsePoints);
802-
ValueLifetimeAnalysis lifetimeAnalysis(borrow, opRange);
811+
ValueLifetimeAnalysis lifetimeAnalysis(borrow, guaranteedUsers);
803812
ValueLifetimeBoundary borrowBoundary;
804813
lifetimeAnalysis.computeLifetimeBoundary(borrowBoundary);
805814

@@ -834,6 +843,14 @@ BeginBorrowInst *OwnershipLifetimeExtender::borrowCopyOverGuaranteedUses(
834843
return borrow;
835844
}
836845

846+
template <typename RangeTy>
847+
BeginBorrowInst *OwnershipLifetimeExtender::borrowCopyOverGuaranteedUses(
848+
SILValue newValue, SILBasicBlock::iterator borrowPoint,
849+
RangeTy guaranteedUsePoints) {
850+
return borrowCopyOverGuaranteedUsers(newValue, borrowPoint,
851+
makeUserRange(guaranteedUsePoints));
852+
}
853+
837854
// Return the borrow position when replacing guaranteedValue with newValue.
838855
//
839856
// Precondition: newValue's block dominates and reaches guaranteedValue's block.
@@ -944,6 +961,18 @@ OwnershipLifetimeExtender::borrowOverSingleUse(SILValue newValue,
944961
return newBeginBorrow;
945962
}
946963

964+
SILValue OwnershipLifetimeExtender::borrowOverSingleNonLifetimeEndingUser(
965+
SILValue newValue, SILInstruction *nonLifetimeEndingUser) {
966+
// Avoid borrowing guaranteed function arguments.
967+
if (isa<SILFunctionArgument>(newValue) &&
968+
newValue.getOwnershipKind() == OwnershipKind::Guaranteed) {
969+
return newValue;
970+
}
971+
auto borrowPt = newValue->getNextInstruction()->getIterator();
972+
return borrowCopyOverGuaranteedUsers(
973+
newValue, borrowPt, ArrayRef<SILInstruction *>(nonLifetimeEndingUser));
974+
}
975+
947976
//===----------------------------------------------------------------------===//
948977
// OwnershipRAUWUtility - RAUW + fix ownership
949978
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)