@@ -443,38 +443,26 @@ class CopyPropagation : public SILFunctionTransform {
443
443
// / The entry point to this function transformation.
444
444
void run () override ;
445
445
446
+ void propagateCopies (CanonicalDefWorklist &defWorklist, bool &changed,
447
+ NonLocalAccessBlockAnalysis *accessBlockAnalysis,
448
+ InstructionDeleter &deleter);
449
+
446
450
void verifyOwnership ();
447
451
};
448
452
449
453
} // end anonymous namespace
450
454
451
- // / Top-level pass driver.
452
- void CopyPropagation::run () {
455
+ void CopyPropagation::propagateCopies (
456
+ CanonicalDefWorklist &defWorklist, bool &changed,
457
+ NonLocalAccessBlockAnalysis *accessBlockAnalysis,
458
+ InstructionDeleter &deleter) {
453
459
auto *f = getFunction ();
454
460
auto *postOrderAnalysis = getAnalysis<PostOrderAnalysis>();
455
- auto *accessBlockAnalysis = getAnalysis<NonLocalAccessBlockAnalysis>();
456
461
auto *deadEndBlocksAnalysis = getAnalysis<DeadEndBlocksAnalysis>();
457
462
auto *dominanceAnalysis = getAnalysis<DominanceAnalysis>();
458
463
auto *calleeAnalysis = getAnalysis<BasicCalleeAnalysis>();
459
464
DominanceInfo *domTree = dominanceAnalysis->get (f);
460
465
461
- // Label for unit testing with debug output.
462
- LLVM_DEBUG (llvm::dbgs () << " *** CopyPropagation: " << f->getName () << " \n " );
463
-
464
- // This algorithm fundamentally assumes ownership.
465
- if (!f->hasOwnership ())
466
- return ;
467
-
468
- CanonicalDefWorklist defWorklist (canonicalizeBorrows);
469
- auto callbacks =
470
- InstModCallbacks ().onDelete ([&](SILInstruction *instToDelete) {
471
- defWorklist.erase (instToDelete);
472
- instToDelete->eraseFromParent ();
473
- });
474
-
475
- InstructionDeleter deleter (std::move (callbacks));
476
- bool changed = false ;
477
-
478
466
StackList<BeginBorrowInst *> beginBorrowsToShrink (f);
479
467
StackList<MoveValueInst *> moveValues (f);
480
468
@@ -514,6 +502,8 @@ void CopyPropagation::run() {
514
502
// at least once and then until each stops making changes.
515
503
while (true ) {
516
504
SmallVector<CopyValueInst *, 4 > modifiedCopyValueInsts;
505
+ if (!continueWithNextSubpassRun (bbi))
506
+ return ;
517
507
auto shrunk = shrinkBorrowScope (*bbi, deleter, calleeAnalysis,
518
508
modifiedCopyValueInsts);
519
509
for (auto *cvi : modifiedCopyValueInsts)
@@ -528,25 +518,35 @@ void CopyPropagation::run() {
528
518
if (borrowee->getOwnershipKind () != OwnershipKind::Owned)
529
519
break ;
530
520
521
+ if (!continueWithNextSubpassRun (borrowee))
522
+ return ;
531
523
auto canonicalized = canonicalizer.canonicalizeValueLifetime (borrowee);
532
524
if (!canonicalized && !firstRun)
533
525
break ;
534
526
527
+ if (!continueWithNextSubpassRun (bbi))
528
+ return ;
535
529
auto folded = foldDestroysOfCopiedLexicalBorrow (bbi, *domTree, deleter);
536
530
if (!folded)
537
531
break ;
538
532
auto hoisted = canonicalizer.canonicalizeValueLifetime (folded);
539
533
// Keep running even if the new move's destroys can't be hoisted.
540
534
(void )hoisted;
535
+ if (!continueWithNextSubpassRun (folded))
536
+ return ;
541
537
eliminateRedundantMove (folded, deleter, defWorklist);
542
538
firstRun = false ;
543
539
}
544
540
}
545
541
for (auto *mvi : moveValues) {
542
+ if (!continueWithNextSubpassRun (mvi))
543
+ return ;
546
544
eliminateRedundantMove (mvi, deleter, defWorklist);
547
545
}
548
546
for (auto *argument : f->getArguments ()) {
549
547
if (argument->getOwnershipKind () == OwnershipKind::Owned) {
548
+ if (!continueWithNextSubpassRun (argument))
549
+ return ;
550
550
canonicalizer.canonicalizeValueLifetime (argument);
551
551
}
552
552
}
@@ -584,8 +584,12 @@ void CopyPropagation::run() {
584
584
// they may be chained, and CanonicalizeBorrowScopes pushes them
585
585
// top-down.
586
586
for (auto result : ownedForward->getResults ()) {
587
+ if (!continueWithNextSubpassRun (result))
588
+ return ;
587
589
canonicalizer.canonicalizeValueLifetime (result);
588
590
}
591
+ if (!continueWithNextSubpassRun (ownedForward))
592
+ return ;
589
593
if (sinkOwnedForward (ownedForward, postOrderAnalysis, domTree)) {
590
594
changed = true ;
591
595
// Sinking 'ownedForward' may create an opportunity to sink its
@@ -607,6 +611,8 @@ void CopyPropagation::run() {
607
611
BorrowedValue borrow (defWorklist.borrowedValues .pop_back_val ());
608
612
assert (canonicalizeBorrows || !borrow.isLocalScope ());
609
613
614
+ if (!continueWithNextSubpassRun (borrow.value ))
615
+ return ;
610
616
borrowCanonicalizer.canonicalizeBorrowScope (borrow);
611
617
for (CopyValueInst *copy : borrowCanonicalizer.getUpdatedCopies ()) {
612
618
defWorklist.updateForCopy (copy);
@@ -623,13 +629,41 @@ void CopyPropagation::run() {
623
629
// Canonicalize all owned defs.
624
630
while (!defWorklist.ownedValues .empty ()) {
625
631
SILValue def = defWorklist.ownedValues .pop_back_val ();
632
+ if (!continueWithNextSubpassRun (def))
633
+ return ;
626
634
auto canonicalized = canonicalizer.canonicalizeValueLifetime (def);
627
635
if (!canonicalized)
628
636
continue ;
629
637
// Copies of borrowed values may be dead.
630
638
if (auto *inst = def->getDefiningInstruction ())
631
639
deleter.trackIfDead (inst);
632
640
}
641
+ }
642
+
643
+ // / Top-level pass driver.
644
+ void CopyPropagation::run () {
645
+ auto *f = getFunction ();
646
+ // This algorithm fundamentally assumes ownership.
647
+ if (!f->hasOwnership ())
648
+ return ;
649
+
650
+ // Label for unit testing with debug output.
651
+ LLVM_DEBUG (llvm::dbgs () << " *** CopyPropagation: " << f->getName () << " \n " );
652
+
653
+ auto *accessBlockAnalysis = getAnalysis<NonLocalAccessBlockAnalysis>();
654
+
655
+ CanonicalDefWorklist defWorklist (canonicalizeBorrows);
656
+
657
+ auto callbacks =
658
+ InstModCallbacks ().onDelete ([&](SILInstruction *instToDelete) {
659
+ defWorklist.erase (instToDelete);
660
+ instToDelete->eraseFromParent ();
661
+ });
662
+ InstructionDeleter deleter (std::move (callbacks));
663
+
664
+ bool changed = false ;
665
+ propagateCopies (defWorklist, changed, accessBlockAnalysis, deleter);
666
+
633
667
// Recursively cleanup dead defs after removing uses.
634
668
deleter.cleanupDeadInstructions ();
635
669
0 commit comments