@@ -1433,15 +1433,49 @@ class ExtendUnconsumedLiveness {
1433
1433
1434
1434
namespace {
1435
1435
1436
+ struct CopiedLoadBorrowEliminationState {
1437
+ SILFunction *fn;
1438
+ StackList<LoadBorrowInst *> targets;
1439
+
1440
+ CopiedLoadBorrowEliminationState (SILFunction *fn) : fn(fn), targets(fn) {}
1441
+
1442
+ void process () {
1443
+ if (targets.empty ())
1444
+ return ;
1445
+
1446
+ while (!targets.empty ()) {
1447
+ auto *lbi = targets.pop_back_val ();
1448
+ SILBuilderWithScope builder (lbi);
1449
+ SILValue li = builder.emitLoadValueOperation (
1450
+ lbi->getLoc (), lbi->getOperand (), LoadOwnershipQualifier::Copy);
1451
+ SILValue borrow = builder.createBeginBorrow (lbi->getLoc (), li);
1452
+
1453
+ for (auto *ebi : lbi->getEndBorrows ()) {
1454
+ auto *next = ebi->getNextInstruction ();
1455
+ SILBuilderWithScope builder (next);
1456
+ auto loc = RegularLocation::getAutoGeneratedLocation ();
1457
+ builder.emitDestroyValueOperation (loc, li);
1458
+ }
1459
+
1460
+ lbi->replaceAllUsesWith (borrow);
1461
+ lbi->eraseFromParent ();
1462
+ }
1463
+
1464
+ LLVM_DEBUG (llvm::dbgs () << " After Load Borrow Elim. Func Dump Start! " ;
1465
+ fn->print (llvm::dbgs ()));
1466
+ LLVM_DEBUG (llvm::dbgs () << " After Load Borrow Elim. Func Dump End!\n " );
1467
+ }
1468
+ };
1469
+
1436
1470
// / An early transform that we run to convert any load_borrow that are copied
1437
1471
// / directly or that have any subelement that is copied to a load [copy]. This
1438
1472
// / lets the rest of the optimization handle these as appropriate.
1439
1473
struct CopiedLoadBorrowEliminationVisitor final
1440
1474
: public TransitiveAddressWalker {
1441
- SILFunction *fn;
1442
- StackList<LoadBorrowInst *> targets;
1475
+ CopiedLoadBorrowEliminationState &state;
1443
1476
1444
- CopiedLoadBorrowEliminationVisitor (SILFunction *fn) : fn(fn), targets(fn) {}
1477
+ CopiedLoadBorrowEliminationVisitor (CopiedLoadBorrowEliminationState &state)
1478
+ : state(state) {}
1445
1479
1446
1480
bool visitUse (Operand *op) override {
1447
1481
LLVM_DEBUG (llvm::dbgs () << " CopiedLBElim visiting " ;
@@ -1527,36 +1561,9 @@ struct CopiedLoadBorrowEliminationVisitor final
1527
1561
if (!shouldConvertToLoadCopy)
1528
1562
return true ;
1529
1563
1530
- targets.push_back (lbi);
1564
+ state. targets .push_back (lbi);
1531
1565
return true ;
1532
1566
}
1533
-
1534
- void process () {
1535
- if (targets.empty ())
1536
- return ;
1537
-
1538
- while (!targets.empty ()) {
1539
- auto *lbi = targets.pop_back_val ();
1540
- SILBuilderWithScope builder (lbi);
1541
- SILValue li = builder.emitLoadValueOperation (
1542
- lbi->getLoc (), lbi->getOperand (), LoadOwnershipQualifier::Copy);
1543
- SILValue borrow = builder.createBeginBorrow (lbi->getLoc (), li);
1544
-
1545
- for (auto *ebi : lbi->getEndBorrows ()) {
1546
- auto *next = ebi->getNextInstruction ();
1547
- SILBuilderWithScope builder (next);
1548
- auto loc = RegularLocation::getAutoGeneratedLocation ();
1549
- builder.emitDestroyValueOperation (loc, li);
1550
- }
1551
-
1552
- lbi->replaceAllUsesWith (borrow);
1553
- lbi->eraseFromParent ();
1554
- }
1555
-
1556
- LLVM_DEBUG (llvm::dbgs () << " After Load Borrow Elim. Func Dump Start! " ;
1557
- fn->print (llvm::dbgs ()));
1558
- LLVM_DEBUG (llvm::dbgs () << " After Load Borrow Elim. Func Dump End!\n " );
1559
- }
1560
1567
};
1561
1568
1562
1569
} // namespace
@@ -3130,14 +3137,15 @@ bool MoveOnlyAddressCheckerPImpl::performSingleCheck(
3130
3137
// [copy] + begin_borrow for further processing. This just eliminates a case
3131
3138
// that the checker doesn't need to know about.
3132
3139
{
3133
- CopiedLoadBorrowEliminationVisitor copiedLoadBorrowEliminator (fn);
3140
+ CopiedLoadBorrowEliminationState state (markedAddress->getFunction ());
3141
+ CopiedLoadBorrowEliminationVisitor copiedLoadBorrowEliminator (state);
3134
3142
if (AddressUseKind::Unknown ==
3135
3143
std::move (copiedLoadBorrowEliminator).walk (markedAddress)) {
3136
3144
LLVM_DEBUG (llvm::dbgs () << " Failed copied load borrow eliminator visit: "
3137
3145
<< *markedAddress);
3138
3146
return false ;
3139
3147
}
3140
- copiedLoadBorrowEliminator .process ();
3148
+ state .process ();
3141
3149
}
3142
3150
3143
3151
// Then gather all uses of our address by walking from def->uses. We use this
0 commit comments