@@ -100,8 +100,8 @@ insertOwnedBaseValueAlongBranchEdge(BranchInst *bi, SILValue innerCopy,
100
100
}
101
101
102
102
static 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) {
105
105
assert (value.getOwnershipKind () == OwnershipKind::Guaranteed);
106
106
107
107
unsigned firstOffset = usePoints.size ();
@@ -165,7 +165,9 @@ static bool findTransitiveBorrowedUses(
165
165
[&](Operand *scopeEndingUse) {
166
166
if (auto scopeEndingBorrowingOp = BorrowingOperand (scopeEndingUse)) {
167
167
if (scopeEndingBorrowingOp.isReborrow ()) {
168
- reborrowPoints.push_back (scopeEndingUse);
168
+ auto *branch = scopeEndingUse->getUser ();
169
+ reborrowPoints.push_back (
170
+ {branch->getParent (), scopeEndingUse->getOperandNumber ()});
169
171
return true ;
170
172
}
171
173
}
@@ -239,8 +241,11 @@ static bool canFixUpOwnershipForRAUW(SILValue oldValue, SILValue newValue,
239
241
if (oldValue.getOwnershipKind () == OwnershipKind::Guaranteed) {
240
242
// Check that the old lifetime can be extended and record the necessary
241
243
// 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
+ }
244
249
}
245
250
return true ;
246
251
}
@@ -430,18 +435,20 @@ OwnershipLifetimeExtender::createPlusZeroBorrow(SILValue newValue,
430
435
// ===----------------------------------------------------------------------===//
431
436
432
437
static void eliminateReborrowsOfRecursiveBorrows (
433
- ArrayRef<BorrowingOperand > transitiveReborrows,
438
+ ArrayRef<std::pair<SILBasicBlock *, unsigned > > transitiveReborrows,
434
439
SmallVectorImpl<Operand *> &usePoints, InstModCallbacks &callbacks) {
435
440
SmallVector<std::pair<SILPhiArgument *, SILPhiArgument *>, 8 >
436
441
baseBorrowedValuePair;
437
442
// Ok, we have transitive reborrows.
438
- for (auto borrowingOperand : transitiveReborrows) {
443
+ for (auto it : transitiveReborrows) {
439
444
// We eliminate the reborrow by creating a new copy+borrow at the reborrow
440
445
// edge from the base value and using that for the reborrow instead of the
441
446
// actual value. We of course insert an end_borrow for our original incoming
442
447
// value.
448
+ auto *bi = cast<BranchInst>(it.first ->getTerminator ());
449
+ auto &op = bi->getOperandRef (it.second );
450
+ BorrowingOperand borrowingOperand (&op);
443
451
SILValue value = borrowingOperand->get ();
444
- auto *bi = cast<BranchInst>(borrowingOperand->getUser ());
445
452
SILBuilderWithScope reborrowBuilder (bi);
446
453
// Use an auto-generated location here, because the branch may have an
447
454
// incompatible LocationKind
@@ -501,15 +508,19 @@ static void eliminateReborrowsOfRecursiveBorrows(
501
508
}
502
509
}
503
510
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) {
507
515
// Each initial reborrow that we have is a use of oldValue, so we know
508
516
// that copy should be valid at the reborrow.
509
517
SmallVector<std::pair<SILPhiArgument *, SILPhiArgument *>, 8 >
510
518
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
+
513
524
SILBuilderWithScope reborrowBuilder (bi);
514
525
// Use an auto-generated location here, because the branch may have an
515
526
// incompatible LocationKind
@@ -713,7 +724,7 @@ SILBasicBlock::iterator OwnershipRAUWUtility::handleGuaranteed() {
713
724
// non-dominating copy value, allowing us to force our borrowing value to
714
725
// need a base phi argument (the one of our choosing).
715
726
if (auto oldValueBorrowedVal = BorrowedValue::get (oldValue)) {
716
- SmallVector<BorrowingOperand , 8 > foundReborrows;
727
+ SmallVector<std::pair<SILBasicBlock *, unsigned > , 8 > foundReborrows;
717
728
if (oldValueBorrowedVal.gatherReborrows (foundReborrows)) {
718
729
rewriteReborrows (newBorrowedValue, foundReborrows, ctx.callbacks );
719
730
}
0 commit comments