@@ -517,25 +517,14 @@ static bool canEndLexicalLifetime(LiveValues values) {
517
517
// /
518
518
// / The beginning of the scope looks like
519
519
// /
520
- // / %lifetime = begin_borrow %original
520
+ // / %lifetime = begin_borrow [lexical] %original
521
521
// / %copy = copy_value %lifetime
522
- // / For a StoreBorrowInst, begin a lexical borrow scope only if the stored value
523
- // / is non-lexical.
524
522
static StorageStateTracking<LiveValues>
525
- beginLexicalLifetimeAfterStore (AllocStackInst *asi, SILInstruction *inst) {
526
- assert (isa<StoreInst>(inst) || isa<StoreBorrowInst>(inst));
523
+ beginOwnedLexicalLifetimeAfterStore (AllocStackInst *asi, StoreInst *inst) {
527
524
assert (lexicalLifetimeEnsured (asi));
528
525
SILValue stored = inst->getOperand (CopyLikeInstruction::Src);
529
526
SILLocation loc = RegularLocation::getAutoGeneratedLocation (inst->getLoc ());
530
527
531
- if (isa<StoreBorrowInst>(inst)) {
532
- if (isGuaranteedLexicalValue (stored)) {
533
- return {LiveValues::forGuaranteed (stored, {}), /* isStorageValid*/ true };
534
- }
535
- auto *borrow = SILBuilderWithScope (inst->getNextInstruction ())
536
- .createBeginBorrow (loc, stored, /* isLexical*/ true );
537
- return {LiveValues::forGuaranteed (stored, borrow), /* isStorageValid*/ true };
538
- }
539
528
BeginBorrowInst *bbi = nullptr ;
540
529
CopyValueInst *cvi = nullptr ;
541
530
SILBuilderWithScope::insertAfter (inst, [&](SILBuilder &builder) {
@@ -548,6 +537,24 @@ beginLexicalLifetimeAfterStore(AllocStackInst *asi, SILInstruction *inst) {
548
537
return vals;
549
538
}
550
539
540
+ // / Begin a lexical borrow scope for the value stored via the provided
541
+ // / StoreBorrowInst after that instruction. Only do so if the stored value is
542
+ // / non-lexical.
543
+ static StorageStateTracking<LiveValues>
544
+ beginGuaranteedLexicalLifetimeAfterStore (AllocStackInst *asi,
545
+ StoreBorrowInst *inst) {
546
+ assert (lexicalLifetimeEnsured (asi));
547
+ SILValue stored = inst->getOperand (CopyLikeInstruction::Src);
548
+ SILLocation loc = RegularLocation::getAutoGeneratedLocation (inst->getLoc ());
549
+
550
+ if (isGuaranteedLexicalValue (stored)) {
551
+ return {LiveValues::forGuaranteed (stored, {}), /* isStorageValid*/ true };
552
+ }
553
+ auto *borrow = SILBuilderWithScope (inst->getNextInstruction ())
554
+ .createBeginBorrow (loc, stored, /* isLexical*/ true );
555
+ return {LiveValues::forGuaranteed (stored, borrow), /* isStorageValid*/ true };
556
+ }
557
+
551
558
// / End the lexical borrow scope for an @owned stored value described by the
552
559
// / provided LiveValues struct before the specified instruction.
553
560
// /
@@ -869,15 +876,14 @@ SILInstruction *StackAllocationPromoter::promoteAllocationInBlock(
869
876
/* isStorageValid=*/ true };
870
877
// The current store is now the lastStoreInst (until we see
871
878
// another).
872
- lastStoreInst = inst ;
879
+ lastStoreInst = si ;
873
880
if (lexicalLifetimeEnsured (asi)) {
874
881
if (oldRunningVals && oldRunningVals->isStorageValid &&
875
882
canEndLexicalLifetime (oldRunningVals->value )) {
876
- endOwnedLexicalLifetimeBeforeInst (asi, /* beforeInstruction=*/ inst,
877
- ctx,
883
+ endOwnedLexicalLifetimeBeforeInst (asi, /* beforeInstruction=*/ si, ctx,
878
884
oldRunningVals->value .getOwned ());
879
885
}
880
- runningVals = beginLexicalLifetimeAfterStore (asi, inst );
886
+ runningVals = beginOwnedLexicalLifetimeAfterStore (asi, si );
881
887
}
882
888
continue ;
883
889
}
@@ -898,9 +904,9 @@ SILInstruction *StackAllocationPromoter::promoteAllocationInBlock(
898
904
runningVals = {LiveValues::toReplace (asi, sbi->getSrc ()),
899
905
/* isStorageValid=*/ true };
900
906
// The current store is now the lastStoreInst.
901
- lastStoreInst = inst ;
907
+ lastStoreInst = sbi ;
902
908
if (lexicalLifetimeEnsured (asi)) {
903
- runningVals = beginLexicalLifetimeAfterStore (asi, inst );
909
+ runningVals = beginGuaranteedLexicalLifetimeAfterStore (asi, sbi );
904
910
}
905
911
continue ;
906
912
}
@@ -1930,9 +1936,9 @@ void MemoryToRegisters::removeSingleBlockAllocation(AllocStackInst *asi) {
1930
1936
endOwnedLexicalLifetimeBeforeInst (asi, /* beforeInstruction=*/ si, ctx,
1931
1937
oldRunningVals->value .getOwned ());
1932
1938
}
1933
- runningVals = beginLexicalLifetimeAfterStore (asi, si);
1939
+ runningVals = beginOwnedLexicalLifetimeAfterStore (asi, si);
1934
1940
}
1935
- deleter.forceDelete (inst );
1941
+ deleter.forceDelete (si );
1936
1942
++NumInstRemoved;
1937
1943
continue ;
1938
1944
}
@@ -1944,7 +1950,7 @@ void MemoryToRegisters::removeSingleBlockAllocation(AllocStackInst *asi) {
1944
1950
runningVals = {LiveValues::toReplace (asi, /* replacement=*/ sbi->getSrc ()),
1945
1951
/* isStorageValid=*/ true };
1946
1952
if (lexicalLifetimeEnsured (asi)) {
1947
- runningVals = beginLexicalLifetimeAfterStore (asi, inst );
1953
+ runningVals = beginGuaranteedLexicalLifetimeAfterStore (asi, sbi );
1948
1954
}
1949
1955
continue ;
1950
1956
}
0 commit comments