Skip to content

Commit 66b8b2f

Browse files
committed
[Mem2Reg] NFC: Split up lexical lifetime starts.
Split the beginning of lexical lifetimes up according to whether the lifetimes (and alloc_stacks) are owned or guaranteed.
1 parent 0564279 commit 66b8b2f

File tree

1 file changed

+28
-22
lines changed

1 file changed

+28
-22
lines changed

lib/SILOptimizer/Transforms/SILMem2Reg.cpp

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -517,25 +517,14 @@ static bool canEndLexicalLifetime(LiveValues values) {
517517
///
518518
/// The beginning of the scope looks like
519519
///
520-
/// %lifetime = begin_borrow %original
520+
/// %lifetime = begin_borrow [lexical] %original
521521
/// %copy = copy_value %lifetime
522-
/// For a StoreBorrowInst, begin a lexical borrow scope only if the stored value
523-
/// is non-lexical.
524522
static StorageStateTracking<LiveValues>
525-
beginLexicalLifetimeAfterStore(AllocStackInst *asi, SILInstruction *inst) {
526-
assert(isa<StoreInst>(inst) || isa<StoreBorrowInst>(inst));
523+
beginOwnedLexicalLifetimeAfterStore(AllocStackInst *asi, StoreInst *inst) {
527524
assert(lexicalLifetimeEnsured(asi));
528525
SILValue stored = inst->getOperand(CopyLikeInstruction::Src);
529526
SILLocation loc = RegularLocation::getAutoGeneratedLocation(inst->getLoc());
530527

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-
}
539528
BeginBorrowInst *bbi = nullptr;
540529
CopyValueInst *cvi = nullptr;
541530
SILBuilderWithScope::insertAfter(inst, [&](SILBuilder &builder) {
@@ -548,6 +537,24 @@ beginLexicalLifetimeAfterStore(AllocStackInst *asi, SILInstruction *inst) {
548537
return vals;
549538
}
550539

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+
551558
/// End the lexical borrow scope for an @owned stored value described by the
552559
/// provided LiveValues struct before the specified instruction.
553560
///
@@ -869,15 +876,14 @@ SILInstruction *StackAllocationPromoter::promoteAllocationInBlock(
869876
/*isStorageValid=*/true};
870877
// The current store is now the lastStoreInst (until we see
871878
// another).
872-
lastStoreInst = inst;
879+
lastStoreInst = si;
873880
if (lexicalLifetimeEnsured(asi)) {
874881
if (oldRunningVals && oldRunningVals->isStorageValid &&
875882
canEndLexicalLifetime(oldRunningVals->value)) {
876-
endOwnedLexicalLifetimeBeforeInst(asi, /*beforeInstruction=*/inst,
877-
ctx,
883+
endOwnedLexicalLifetimeBeforeInst(asi, /*beforeInstruction=*/si, ctx,
878884
oldRunningVals->value.getOwned());
879885
}
880-
runningVals = beginLexicalLifetimeAfterStore(asi, inst);
886+
runningVals = beginOwnedLexicalLifetimeAfterStore(asi, si);
881887
}
882888
continue;
883889
}
@@ -898,9 +904,9 @@ SILInstruction *StackAllocationPromoter::promoteAllocationInBlock(
898904
runningVals = {LiveValues::toReplace(asi, sbi->getSrc()),
899905
/*isStorageValid=*/true};
900906
// The current store is now the lastStoreInst.
901-
lastStoreInst = inst;
907+
lastStoreInst = sbi;
902908
if (lexicalLifetimeEnsured(asi)) {
903-
runningVals = beginLexicalLifetimeAfterStore(asi, inst);
909+
runningVals = beginGuaranteedLexicalLifetimeAfterStore(asi, sbi);
904910
}
905911
continue;
906912
}
@@ -1930,9 +1936,9 @@ void MemoryToRegisters::removeSingleBlockAllocation(AllocStackInst *asi) {
19301936
endOwnedLexicalLifetimeBeforeInst(asi, /*beforeInstruction=*/si, ctx,
19311937
oldRunningVals->value.getOwned());
19321938
}
1933-
runningVals = beginLexicalLifetimeAfterStore(asi, si);
1939+
runningVals = beginOwnedLexicalLifetimeAfterStore(asi, si);
19341940
}
1935-
deleter.forceDelete(inst);
1941+
deleter.forceDelete(si);
19361942
++NumInstRemoved;
19371943
continue;
19381944
}
@@ -1944,7 +1950,7 @@ void MemoryToRegisters::removeSingleBlockAllocation(AllocStackInst *asi) {
19441950
runningVals = {LiveValues::toReplace(asi, /*replacement=*/sbi->getSrc()),
19451951
/*isStorageValid=*/true};
19461952
if (lexicalLifetimeEnsured(asi)) {
1947-
runningVals = beginLexicalLifetimeAfterStore(asi, inst);
1953+
runningVals = beginGuaranteedLexicalLifetimeAfterStore(asi, sbi);
19481954
}
19491955
continue;
19501956
}

0 commit comments

Comments
 (0)