@@ -208,7 +208,8 @@ cleanupDeadTrivialPhiArgs(SILValue initialValue,
208
208
// / that case where we have to consider that destroy_value, we have a simpler
209
209
// / time here.
210
210
static void extendLifetimeToEndOfFunction (SILFunction &fn,
211
- ConvertEscapeToNoEscapeInst *cvt) {
211
+ ConvertEscapeToNoEscapeInst *cvt,
212
+ SILSSAUpdater &updater) {
212
213
auto escapingClosure = cvt->getOperand ();
213
214
auto escapingClosureTy = escapingClosure->getType ();
214
215
auto optionalEscapingClosureTy = SILType::getOptionalType (escapingClosureTy);
@@ -239,7 +240,7 @@ static void extendLifetimeToEndOfFunction(SILFunction &fn,
239
240
// use SILSSAUpdater::GetValueInMiddleOfBlock() to extend the object's
240
241
// lifetime respecting loops.
241
242
SmallVector<SILPhiArgument *, 8 > insertedPhis;
242
- SILSSAUpdater updater (&insertedPhis);
243
+ updater. setInsertedPhis (&insertedPhis);
243
244
updater.initialize (optionalEscapingClosureTy, fn.hasOwnership ()
244
245
? OwnershipKind::Owned
245
246
: OwnershipKind::None);
@@ -443,7 +444,7 @@ static SILValue skipConvert(SILValue v) {
443
444
// / nesting.
444
445
static SILValue tryRewriteToPartialApplyStack (
445
446
ConvertEscapeToNoEscapeInst *cvt,
446
- SILInstruction *closureUser, SILBasicBlock::iterator &advanceIfDelete ,
447
+ SILInstruction *closureUser, InstructionDeleter &deleter ,
447
448
llvm::DenseMap<SILInstruction *, SILInstruction *> &memoized) {
448
449
449
450
auto *origPA = dyn_cast<PartialApplyInst>(skipConvert (cvt->getOperand ()));
@@ -457,10 +458,8 @@ static SILValue tryRewriteToPartialApplyStack(
457
458
// Whenever we delete an instruction advance the iterator and remove the
458
459
// instruction from the memoized map.
459
460
auto saveDeleteInst = [&](SILInstruction *i) {
460
- if (&*advanceIfDelete == i)
461
- ++advanceIfDelete;
462
461
memoized.erase (i);
463
- i-> eraseFromParent ( );
462
+ deleter. forceDelete (i );
464
463
};
465
464
466
465
// Look for a single non ref count user of the partial_apply.
@@ -534,7 +533,7 @@ static SILValue tryRewriteToPartialApplyStack(
534
533
SmallVector<Operand*, 16 > Uses (arg.get ()->getUses ());
535
534
for (auto use : Uses)
536
535
if (auto *deallocInst = dyn_cast<DeallocStackInst>(use->getUser ()))
537
- deallocInst-> eraseFromParent ( );
536
+ deleter. forceDelete (deallocInst );
538
537
}
539
538
}
540
539
@@ -552,7 +551,7 @@ static SILValue tryRewriteToPartialApplyStack(
552
551
static bool tryExtendLifetimeToLastUse (
553
552
ConvertEscapeToNoEscapeInst *cvt,
554
553
llvm::DenseMap<SILInstruction *, SILInstruction *> &memoized,
555
- SILBasicBlock::iterator &advanceIfDelete ) {
554
+ InstructionDeleter &deleter ) {
556
555
// If there is a single user that is an apply this is simple: extend the
557
556
// lifetime of the operand until after the apply.
558
557
auto *singleUser = lookThroughRebastractionUsers (cvt, memoized);
@@ -574,7 +573,7 @@ static bool tryExtendLifetimeToLastUse(
574
573
}
575
574
576
575
if (SILValue closure = tryRewriteToPartialApplyStack (cvt, singleUser,
577
- advanceIfDelete , memoized)) {
576
+ deleter , memoized)) {
578
577
if (auto *cfi = dyn_cast<ConvertFunctionInst>(closure))
579
578
closure = cfi->getOperand ();
580
579
if (endAsyncLet && isa<MarkDependenceInst>(closure)) {
@@ -587,7 +586,7 @@ static bool tryExtendLifetimeToLastUse(
587
586
builder.createBuiltin (endAsyncLet->getLoc (), endAsyncLet->getName (),
588
587
endAsyncLet->getType (), endAsyncLet->getSubstitutions (),
589
588
{endAsyncLet->getOperand (0 ), closure});
590
- endAsyncLet-> eraseFromParent ( );
589
+ deleter. forceDelete (endAsyncLet );
591
590
}
592
591
return true ;
593
592
}
@@ -797,6 +796,7 @@ static SILInstruction *getOnlyDestroy(CopyBlockWithoutEscapingInst *cb) {
797
796
// / cond_fail %e
798
797
// / destroy_value %closure
799
798
static bool fixupCopyBlockWithoutEscaping (CopyBlockWithoutEscapingInst *cb,
799
+ InstructionDeleter &deleter,
800
800
bool &modifiedCFG) {
801
801
// Find the end of the lifetime of the copy_block_without_escaping
802
802
// instruction.
@@ -837,7 +837,7 @@ static bool fixupCopyBlockWithoutEscaping(CopyBlockWithoutEscapingInst *cb,
837
837
SILBuilderWithScope b (cb);
838
838
auto *newCB = b.createCopyBlock (loc, cb->getBlock ());
839
839
cb->replaceAllUsesWith (newCB);
840
- cb-> eraseFromParent ( );
840
+ deleter. forceDelete (cb );
841
841
842
842
auto autoGenLoc = RegularLocation::getAutoGeneratedLocation ();
843
843
@@ -977,14 +977,12 @@ static bool fixupClosureLifetimes(SILFunction &fn, bool &checkStackNesting,
977
977
llvm::DenseMap<SILInstruction *, SILInstruction *> memoizedQueries;
978
978
979
979
for (auto &block : fn) {
980
- auto i = block.begin ();
981
- while (i != block.end ()) {
982
- SILInstruction *inst = &*i;
983
- ++i;
980
+ SILSSAUpdater updater;
981
+ for (SILInstruction *inst : updater.getDeleter ().updatingRange (&block)) {
984
982
985
983
// Handle, copy_block_without_escaping instructions.
986
984
if (auto *cb = dyn_cast<CopyBlockWithoutEscapingInst>(inst)) {
987
- if (fixupCopyBlockWithoutEscaping (cb, modifiedCFG)) {
985
+ if (fixupCopyBlockWithoutEscaping (cb, updater. getDeleter (), modifiedCFG)) {
988
986
changed = true ;
989
987
}
990
988
continue ;
@@ -1004,15 +1002,15 @@ static bool fixupClosureLifetimes(SILFunction &fn, bool &checkStackNesting,
1004
1002
}
1005
1003
}
1006
1004
1007
- if (tryExtendLifetimeToLastUse (cvt, memoizedQueries, i )) {
1005
+ if (tryExtendLifetimeToLastUse (cvt, memoizedQueries, updater. getDeleter () )) {
1008
1006
changed = true ;
1009
1007
checkStackNesting = true ;
1010
1008
continue ;
1011
1009
}
1012
1010
1013
1011
// Otherwise, extend the lifetime of the operand to the end of the
1014
1012
// function.
1015
- extendLifetimeToEndOfFunction (fn, cvt);
1013
+ extendLifetimeToEndOfFunction (fn, cvt, updater );
1016
1014
changed = true ;
1017
1015
}
1018
1016
}
0 commit comments