@@ -100,8 +100,8 @@ insertOwnedBaseValueAlongBranchEdge(BranchInst *bi, SILValue innerCopy,
100100}
101101
102102static bool findTransitiveBorrowedUses (
103- SILValue value, SmallVectorImpl<Operand *> &usePoints,
104- SmallVectorImpl<BorrowingOperand > &reborrowPoints) {
103+ SILValue value, SmallVectorImpl<Operand *> &usePoints,
104+ SmallVectorImpl<std::pair<SILBasicBlock *, unsigned > > &reborrowPoints) {
105105 assert (value.getOwnershipKind () == OwnershipKind::Guaranteed);
106106
107107 unsigned firstOffset = usePoints.size ();
@@ -165,7 +165,9 @@ static bool findTransitiveBorrowedUses(
165165 [&](Operand *scopeEndingUse) {
166166 if (auto scopeEndingBorrowingOp = BorrowingOperand (scopeEndingUse)) {
167167 if (scopeEndingBorrowingOp.isReborrow ()) {
168- reborrowPoints.push_back (scopeEndingUse);
168+ auto *branch = scopeEndingUse->getUser ();
169+ reborrowPoints.push_back (
170+ {branch->getParent (), scopeEndingUse->getOperandNumber ()});
169171 return true ;
170172 }
171173 }
@@ -239,8 +241,11 @@ static bool canFixUpOwnershipForRAUW(SILValue oldValue, SILValue newValue,
239241 if (oldValue.getOwnershipKind () == OwnershipKind::Guaranteed) {
240242 // Check that the old lifetime can be extended and record the necessary
241243 // book-keeping in the OwnershipFixupContext.
242- return findTransitiveBorrowedUses (oldValue, context.transitiveBorrowedUses ,
243- context.recursiveReborrows );
244+ if (!findTransitiveBorrowedUses (oldValue, context.transitiveBorrowedUses ,
245+ context.recursiveReborrows )) {
246+ context.clear ();
247+ return false ;
248+ }
244249 }
245250 return true ;
246251}
@@ -430,18 +435,20 @@ OwnershipLifetimeExtender::createPlusZeroBorrow(SILValue newValue,
430435// ===----------------------------------------------------------------------===//
431436
432437static void eliminateReborrowsOfRecursiveBorrows (
433- ArrayRef<BorrowingOperand > transitiveReborrows,
438+ ArrayRef<std::pair<SILBasicBlock *, unsigned > > transitiveReborrows,
434439 SmallVectorImpl<Operand *> &usePoints, InstModCallbacks &callbacks) {
435440 SmallVector<std::pair<SILPhiArgument *, SILPhiArgument *>, 8 >
436441 baseBorrowedValuePair;
437442 // Ok, we have transitive reborrows.
438- for (auto borrowingOperand : transitiveReborrows) {
443+ for (auto it : transitiveReborrows) {
439444 // We eliminate the reborrow by creating a new copy+borrow at the reborrow
440445 // edge from the base value and using that for the reborrow instead of the
441446 // actual value. We of course insert an end_borrow for our original incoming
442447 // value.
448+ auto *bi = cast<BranchInst>(it.first ->getTerminator ());
449+ auto &op = bi->getOperandRef (it.second );
450+ BorrowingOperand borrowingOperand (&op);
443451 SILValue value = borrowingOperand->get ();
444- auto *bi = cast<BranchInst>(borrowingOperand->getUser ());
445452 SILBuilderWithScope reborrowBuilder (bi);
446453 // Use an auto-generated location here, because the branch may have an
447454 // incompatible LocationKind
@@ -501,15 +508,19 @@ static void eliminateReborrowsOfRecursiveBorrows(
501508 }
502509}
503510
504- static void rewriteReborrows (SILValue newBorrowedValue,
505- ArrayRef<BorrowingOperand> foundReborrows,
506- InstModCallbacks &callbacks) {
511+ static void
512+ rewriteReborrows (SILValue newBorrowedValue,
513+ ArrayRef<std::pair<SILBasicBlock *, unsigned >> foundReborrows,
514+ InstModCallbacks &callbacks) {
507515 // Each initial reborrow that we have is a use of oldValue, so we know
508516 // that copy should be valid at the reborrow.
509517 SmallVector<std::pair<SILPhiArgument *, SILPhiArgument *>, 8 >
510518 baseBorrowedValuePair;
511- for (auto reborrow : foundReborrows) {
512- auto *bi = cast<BranchInst>(reborrow.op ->getUser ());
519+ for (auto it : foundReborrows) {
520+ auto *bi = cast<BranchInst>(it.first ->getTerminator ());
521+ auto &op = bi->getOperandRef (it.second );
522+ BorrowingOperand reborrow (&op);
523+
513524 SILBuilderWithScope reborrowBuilder (bi);
514525 // Use an auto-generated location here, because the branch may have an
515526 // incompatible LocationKind
@@ -713,7 +724,7 @@ SILBasicBlock::iterator OwnershipRAUWUtility::handleGuaranteed() {
713724 // non-dominating copy value, allowing us to force our borrowing value to
714725 // need a base phi argument (the one of our choosing).
715726 if (auto oldValueBorrowedVal = BorrowedValue::get (oldValue)) {
716- SmallVector<BorrowingOperand , 8 > foundReborrows;
727+ SmallVector<std::pair<SILBasicBlock *, unsigned > , 8 > foundReborrows;
717728 if (oldValueBorrowedVal.gatherReborrows (foundReborrows)) {
718729 rewriteReborrows (newBorrowedValue, foundReborrows, ctx.callbacks );
719730 }
0 commit comments