@@ -308,9 +308,6 @@ class FilterCandidates final {
308308
309309 // / Determines whether each candidate is viable for folding.
310310 // /
311- // / Requiring findUsers to have been called, this uses the more expensive
312- // / isViableMatch.
313- // /
314311 // / returns true if any candidates were viable
315312 // / false otherwise
316313 bool run (Candidates &candidates);
@@ -334,8 +331,7 @@ class FilterCandidates final {
334331// /
335332// / If there are, we can't fold the apply. Specifically, we can't introduce
336333// / a move_value [lexical] %borrowee because that value still needs to be used
337- // / in those locations. While updateSSA would happily rewrite those uses to
338- // / be uses of the move, that is not a semantically valid transformation.
334+ // / in those locations.
339335// /
340336// / For example, given the following SIL
341337// / %borrowee : @owned
@@ -412,10 +408,6 @@ class Rewriter final {
412408 // / (3) delete the destroy_value
413409 void fold (Match, SmallVectorImpl<int > const &rewritableArgumentIndices);
414410
415- // Update SSA to reflect that fact that %move and %borrowee are two
416- // defs for the "same value".
417- void updateSSA ();
418-
419411 // The move_value [lexical] instruction that was added during the run.
420412 //
421413 // Defined during createMove.
@@ -470,25 +462,31 @@ bool run(Context &context) {
470462void Rewriter::run () {
471463 bool foldedAny = false ;
472464 (void )foldedAny;
473- for ( unsigned index = 0 , count = candidates.vector .size (); index < count ;
474- ++index) {
465+ auto size = candidates.vector .size ();
466+ for ( unsigned index = 0 ; index < size; ++index) {
475467 auto candidate = candidates.vector [index];
476- if (!candidate.viable )
477- continue ;
478468 createMove ();
469+ if (!candidate.viable ) {
470+ // Nonviable candidates still end with the pattern
471+ //
472+ // end_borrow %lifetime
473+ // ...
474+ // destroy_value %borrowee
475+ //
476+ // Now that the new move_value [lexical] dominates all candidates, the
477+ // every candidate's destroy_value %borrowee is dominated by it, so every
478+ // one is dominated by another consuming use which is illegal. Rewrite
479+ // each such destroy_value to be a destroy_value of the move.
480+ candidate.match .dvi ->setOperand (mvi);
481+ continue ;
482+ }
479483
480484 fold (candidate.match , candidate.argumentIndices );
481485#ifndef NDEBUG
482486 foldedAny = true ;
483487#endif
484488 }
485489 assert (foldedAny && " rewriting without anything to rewrite!?" );
486-
487- // There are now "two defs for %borrowee":
488- // - %borrowee
489- // - %move
490- // We need to update SSA.
491- updateSSA ();
492490}
493491
494492bool Rewriter::createMove () {
@@ -551,26 +549,6 @@ void Rewriter::fold(Match candidate,
551549 context.deleter .forceDelete (candidate.dvi );
552550}
553551
554- void Rewriter::updateSSA () {
555- SILSSAUpdater updater;
556- updater.initialize (context.borrowee ->getType (),
557- context.borrowee .getOwnershipKind ());
558- updater.addAvailableValue (context.borrowee ->getParentBlock (),
559- context.borrowee );
560- updater.addAvailableValue (mvi->getParentBlock (), mvi);
561-
562- SmallVector<Operand *, 16 > uses;
563- for (auto use : context.borrowee ->getUses ()) {
564- if (use->getUser () == mvi)
565- continue ;
566- uses.push_back (use);
567- }
568-
569- for (auto use : uses) {
570- updater.rewriteUse (*use);
571- }
572- }
573-
574552// ===----------------------------------------------------------------------===//
575553// MARK: Lookups
576554// ===----------------------------------------------------------------------===//
@@ -640,6 +618,8 @@ bool FilterCandidates::run(Candidates &candidates) {
640618
641619bool findBorroweeUsage (Context &context, BorroweeUsage &usage) {
642620 auto recordUse = [&](Operand *use) {
621+ // Ignore uses that aren't dominated by the introducer. PrunedLiveness
622+ // relies on us doing this check.
643623 if (!context.dominanceTree .dominates (context.introducer , use->getUser ()))
644624 return ;
645625 usage.uses .push_back (use);
0 commit comments