Skip to content

Commit b8c2269

Browse files
committed
[NFC] Changed function name.
Because lexical borrows are already avoided for store_borrows of lexical values, the function is already misnamed: it's not that Mem2Reg should necessarily _add_ a lexical lifetime, but rather that it should ensure that there is one. Considering that we should do the same for owned lexical values, the renaming will remain appropriate later. Finally, name it so that it can switch from being a boolean to returning a tristate (none, guaranteed, owned) when that becomes necessary (as it will when we need to distinguish among the states to determine what phis look like).
1 parent 06ed5d7 commit b8c2269

File tree

1 file changed

+31
-32
lines changed

1 file changed

+31
-32
lines changed

lib/SILOptimizer/Transforms/SILMem2Reg.cpp

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ STATISTIC(NumAllocStackFound, "Number of AllocStack found");
5454
STATISTIC(NumAllocStackCaptured, "Number of AllocStack captured");
5555
STATISTIC(NumInstRemoved, "Number of Instructions removed");
5656

57-
static bool shouldAddLexicalLifetime(AllocStackInst *asi);
57+
static bool lexicalLifetimeEnsured(AllocStackInst *asi);
5858
static bool isGuaranteedLexicalValue(SILValue src);
5959

6060
namespace {
@@ -83,15 +83,15 @@ struct LiveValues {
8383
/// usages will be replaced with usages of the stored field. The
8484
/// implementation constructs an instance to match those requirements.
8585
static LiveValues toReplace(AllocStackInst *asi, SILValue replacement) {
86-
if (shouldAddLexicalLifetime(asi))
86+
if (lexicalLifetimeEnsured(asi))
8787
return {SILValue(), SILValue(), replacement};
8888
return {replacement, SILValue(), SILValue()};
8989
};
9090

9191
/// The value with which usages of the provided AllocStackInst should be
9292
/// replaced.
9393
SILValue replacement(AllocStackInst *asi, SILInstruction *toReplace) {
94-
if (!shouldAddLexicalLifetime(asi)) {
94+
if (!lexicalLifetimeEnsured(asi)) {
9595
return stored;
9696
}
9797
// For lexical AllocStackInsts, which are store_borrow locations, we may
@@ -301,7 +301,7 @@ replaceLoad(SILInstruction *inst, SILValue newValue, AllocStackInst *asi,
301301
op = inst->getOperand(0);
302302

303303
if (auto *lbi = dyn_cast<LoadBorrowInst>(inst)) {
304-
if (shouldAddLexicalLifetime(asi)) {
304+
if (lexicalLifetimeEnsured(asi)) {
305305
assert(newValue->getOwnershipKind() == OwnershipKind::Guaranteed);
306306
SmallVector<SILInstruction *, 4> endBorrows;
307307
for (auto *ebi : lbi->getUsersOfType<EndBorrowInst>()) {
@@ -311,8 +311,7 @@ replaceLoad(SILInstruction *inst, SILValue newValue, AllocStackInst *asi,
311311
prepareForDeletion(ebi, instructionsToDelete);
312312
}
313313
lbi->replaceAllUsesWith(newValue);
314-
}
315-
else {
314+
} else {
316315
auto *borrow = SILBuilderWithScope(lbi, ctx).createBeginBorrow(
317316
lbi->getLoc(), newValue, asi->isLexical());
318317
lbi->replaceAllUsesWith(borrow);
@@ -367,7 +366,7 @@ static SILValue createValueForEmptyTuple(SILType ty,
367366

368367
/// Whether lexical lifetimes should be added for the values stored into the
369368
/// alloc_stack.
370-
static bool shouldAddLexicalLifetime(AllocStackInst *asi) {
369+
static bool lexicalLifetimeEnsured(AllocStackInst *asi) {
371370
return asi->getFunction()->hasOwnership() &&
372371
asi->getFunction()
373372
->getModule()
@@ -412,7 +411,7 @@ static bool canEndLexicalLifetime(LiveValues values) { return values.borrow; }
412411
static StorageStateTracking<LiveValues>
413412
beginLexicalLifetimeAfterStore(AllocStackInst *asi, SILInstruction *inst) {
414413
assert(isa<StoreInst>(inst) || isa<StoreBorrowInst>(inst));
415-
assert(shouldAddLexicalLifetime(asi));
414+
assert(lexicalLifetimeEnsured(asi));
416415
SILValue stored = inst->getOperand(CopyLikeInstruction::Src);
417416
SILLocation loc = RegularLocation::getAutoGeneratedLocation(inst->getLoc());
418417

@@ -452,7 +451,7 @@ static void endOwnedLexicalLifetimeBeforeInst(AllocStackInst *asi,
452451
SILInstruction *beforeInstruction,
453452
SILBuilderContext &ctx,
454453
LiveValues values) {
455-
assert(shouldAddLexicalLifetime(asi));
454+
assert(lexicalLifetimeEnsured(asi));
456455
assert(beforeInstruction);
457456

458457
auto builder = SILBuilderWithScope(beforeInstruction, ctx);
@@ -467,7 +466,7 @@ static void endOwnedLexicalLifetimeBeforeInst(AllocStackInst *asi,
467466
static void endGuaranteedLexicalLifetimeBeforeInst(
468467
AllocStackInst *asi, SILInstruction *beforeInstruction,
469468
SILBuilderContext &ctx, LiveValues values) {
470-
assert(shouldAddLexicalLifetime(asi));
469+
assert(lexicalLifetimeEnsured(asi));
471470
assert(beforeInstruction);
472471
assert(values.borrow);
473472

@@ -667,7 +666,7 @@ SILInstruction *StackAllocationPromoter::promoteAllocationInBlock(
667666
assert(!runningVals || runningVals->isStorageValid);
668667
auto *li = dyn_cast<LoadInst>(inst);
669668
if (li && li->getOwnershipQualifier() == LoadOwnershipQualifier::Take) {
670-
if (shouldAddLexicalLifetime(asi)) {
669+
if (lexicalLifetimeEnsured(asi)) {
671670
// End the lexical lifetime at a load [take]. The storage is no
672671
// longer keeping the value alive.
673672
if (runningVals && canEndLexicalLifetime(runningVals->value)) {
@@ -723,7 +722,7 @@ SILInstruction *StackAllocationPromoter::promoteAllocationInBlock(
723722
auto *newLoad = localBuilder.createLoad(si->getLoc(), asi,
724723
LoadOwnershipQualifier::Take);
725724
localBuilder.createDestroyValue(si->getLoc(), newLoad);
726-
if (shouldAddLexicalLifetime(asi)) {
725+
if (lexicalLifetimeEnsured(asi)) {
727726
assert(!deinitializationPoints[blockPromotingWithin]);
728727
deinitializationPoints[blockPromotingWithin] = si;
729728
}
@@ -750,7 +749,7 @@ SILInstruction *StackAllocationPromoter::promoteAllocationInBlock(
750749
// The current store is now the lastStoreInst (until we see
751750
// another).
752751
lastStoreInst = inst;
753-
if (shouldAddLexicalLifetime(asi)) {
752+
if (lexicalLifetimeEnsured(asi)) {
754753
if (oldRunningVals && oldRunningVals->isStorageValid &&
755754
canEndLexicalLifetime(oldRunningVals->value)) {
756755
endOwnedLexicalLifetimeBeforeInst(asi, /*beforeInstruction=*/inst,
@@ -778,15 +777,15 @@ SILInstruction *StackAllocationPromoter::promoteAllocationInBlock(
778777
/*isStorageValid=*/true};
779778
// The current store is now the lastStoreInst.
780779
lastStoreInst = inst;
781-
if (shouldAddLexicalLifetime(asi)) {
780+
if (lexicalLifetimeEnsured(asi)) {
782781
runningVals = beginLexicalLifetimeAfterStore(asi, inst);
783782
}
784783
continue;
785784
}
786785

787786
// End the lexical lifetime of the store_borrow source.
788787
if (auto *ebi = dyn_cast<EndBorrowInst>(inst)) {
789-
if (!shouldAddLexicalLifetime(asi)) {
788+
if (!lexicalLifetimeEnsured(asi)) {
790789
continue;
791790
}
792791
auto *sbi = dyn_cast<StoreBorrowInst>(ebi->getOperand());
@@ -833,7 +832,7 @@ SILInstruction *StackAllocationPromoter::promoteAllocationInBlock(
833832
if (runningVals) {
834833
replaceDestroy(dai, runningVals->value.replacement(asi, dai), ctx,
835834
deleter, instructionsToDelete);
836-
if (shouldAddLexicalLifetime(asi)) {
835+
if (lexicalLifetimeEnsured(asi)) {
837836
endOwnedLexicalLifetimeBeforeInst(asi, /*beforeInstruction=*/dai, ctx,
838837
runningVals->value);
839838
}
@@ -873,7 +872,7 @@ void StackAllocationPromoter::addBlockArguments(BlockSetVector &phiBlocks) {
873872
for (auto *block : phiBlocks) {
874873
// The stored value.
875874
block->createPhiArgument(asi->getElementType(), OwnershipKind::Owned);
876-
if (shouldAddLexicalLifetime(asi)) {
875+
if (lexicalLifetimeEnsured(asi)) {
877876
// The borrow scope.
878877
block->createPhiArgument(asi->getElementType(),
879878
OwnershipKind::Guaranteed);
@@ -901,7 +900,7 @@ StackAllocationPromoter::getLiveOutValues(BlockSetVector &phiBlocks,
901900
SILValue stored = inst->getOperand(CopyLikeInstruction::Src);
902901
LLVM_DEBUG(llvm::dbgs() << "*** Found Store def " << stored);
903902

904-
if (!shouldAddLexicalLifetime(asi)) {
903+
if (!lexicalLifetimeEnsured(asi)) {
905904
LiveValues values = {stored, SILValue(), SILValue()};
906905
return values;
907906
}
@@ -927,7 +926,7 @@ StackAllocationPromoter::getLiveOutValues(BlockSetVector &phiBlocks,
927926
SILValue original;
928927
SILValue borrow;
929928
SILValue copy;
930-
if (shouldAddLexicalLifetime(asi)) {
929+
if (lexicalLifetimeEnsured(asi)) {
931930
original = domBlock->getArgument(domBlock->getNumArguments() - 3);
932931
borrow = domBlock->getArgument(domBlock->getNumArguments() - 2);
933932
copy = domBlock->getArgument(domBlock->getNumArguments() - 1);
@@ -970,7 +969,7 @@ StackAllocationPromoter::getLiveInValues(BlockSetVector &phiBlocks,
970969
SILValue original;
971970
SILValue borrow;
972971
SILValue copy;
973-
if (shouldAddLexicalLifetime(asi)) {
972+
if (lexicalLifetimeEnsured(asi)) {
974973
original = block->getArgument(block->getNumArguments() - 3);
975974
borrow = block->getArgument(block->getNumArguments() - 2);
976975
copy = block->getArgument(block->getNumArguments() - 1);
@@ -1017,7 +1016,7 @@ void StackAllocationPromoter::fixPhiPredBlock(BlockSetVector &phiBlocks,
10171016

10181017
SmallVector<SILValue> vals;
10191018
vals.push_back(def.stored);
1020-
if (shouldAddLexicalLifetime(asi)) {
1019+
if (lexicalLifetimeEnsured(asi)) {
10211020
vals.push_back(def.borrow);
10221021
vals.push_back(def.copy);
10231022
}
@@ -1185,7 +1184,7 @@ void StackAllocationPromoter::fixBranchesAndUses(BlockSetVector &phiBlocks,
11851184
block->getArgument(block->getNumArguments() - 1));
11861185
if (!livePhis.contains(proactivePhi)) {
11871186
eraseLastPhiFromBlock(block);
1188-
if (shouldAddLexicalLifetime(asi)) {
1187+
if (lexicalLifetimeEnsured(asi)) {
11891188
eraseLastPhiFromBlock(block);
11901189
eraseLastPhiFromBlock(block);
11911190
}
@@ -1231,7 +1230,7 @@ void StackAllocationPromoter::fixBranchesAndUses(BlockSetVector &phiBlocks,
12311230
/// This can only happen if the successor is a CFG merge and all paths
12321231
/// from here lead to unreachable.
12331232
void StackAllocationPromoter::endLexicalLifetime(BlockSetVector &phiBlocks) {
1234-
if (!shouldAddLexicalLifetime(asi))
1233+
if (!lexicalLifetimeEnsured(asi))
12351234
return;
12361235

12371236
// We need to separately consider and visit incoming unopened borrow scopes
@@ -1350,9 +1349,9 @@ void StackAllocationPromoter::pruneAllocStackUsage() {
13501349

13511350
for (auto block : functionBlocks)
13521351
if (auto si = promoteAllocationInBlock(block)) {
1353-
// There was a final storee instruction which was not followed by an
1354-
// instruction that deinitializes the memory. Record it as a cross-block
1355-
// initialization point.
1352+
// There was a final store/store_borrow instruction which was not
1353+
// followed by an instruction that deinitializes the memory. Record it
1354+
// as a cross-block initialization point.
13561355
initializationPoints[block] = si;
13571356
}
13581357

@@ -1768,7 +1767,7 @@ void MemoryToRegisters::removeSingleBlockAllocation(AllocStackInst *asi) {
17681767
auto *loadInst = dyn_cast<LoadInst>(inst);
17691768
if (loadInst &&
17701769
loadInst->getOwnershipQualifier() == LoadOwnershipQualifier::Take) {
1771-
if (shouldAddLexicalLifetime(asi)) {
1770+
if (lexicalLifetimeEnsured(asi)) {
17721771
// End the lexical lifetime at a load [take]. The storage is no
17731772
// longer keeping the value alive.
17741773
endOwnedLexicalLifetimeBeforeInst(asi, /*beforeInstruction=*/inst,
@@ -1796,7 +1795,7 @@ void MemoryToRegisters::removeSingleBlockAllocation(AllocStackInst *asi) {
17961795
auto oldRunningVals = runningVals;
17971796
runningVals = {LiveValues::toReplace(asi, /*replacement=*/si->getSrc()),
17981797
/*isStorageValid=*/true};
1799-
if (shouldAddLexicalLifetime(asi)) {
1798+
if (lexicalLifetimeEnsured(asi)) {
18001799
if (oldRunningVals && oldRunningVals->isStorageValid) {
18011800
endOwnedLexicalLifetimeBeforeInst(asi, /*beforeInstruction=*/si, ctx,
18021801
oldRunningVals->value);
@@ -1814,7 +1813,7 @@ void MemoryToRegisters::removeSingleBlockAllocation(AllocStackInst *asi) {
18141813
}
18151814
runningVals = {LiveValues::toReplace(asi, /*replacement=*/sbi->getSrc()),
18161815
/*isStorageValid=*/true};
1817-
if (shouldAddLexicalLifetime(asi)) {
1816+
if (lexicalLifetimeEnsured(asi)) {
18181817
runningVals = beginLexicalLifetimeAfterStore(asi, inst);
18191818
}
18201819
continue;
@@ -1866,7 +1865,7 @@ void MemoryToRegisters::removeSingleBlockAllocation(AllocStackInst *asi) {
18661865
assert(runningVals && runningVals->isStorageValid);
18671866
replaceDestroy(dai, runningVals->value.replacement(asi, dai), ctx,
18681867
deleter, instructionsToDelete);
1869-
if (shouldAddLexicalLifetime(asi)) {
1868+
if (lexicalLifetimeEnsured(asi)) {
18701869
endOwnedLexicalLifetimeBeforeInst(asi, /*beforeInstruction=*/dai, ctx,
18711870
runningVals->value);
18721871
}
@@ -1898,7 +1897,7 @@ void MemoryToRegisters::removeSingleBlockAllocation(AllocStackInst *asi) {
18981897
}
18991898
}
19001899

1901-
if (shouldAddLexicalLifetime(asi) && runningVals &&
1900+
if (lexicalLifetimeEnsured(asi) && runningVals &&
19021901
runningVals->isStorageValid &&
19031902
runningVals->value.stored->getOwnershipKind().isCompatibleWith(
19041903
OwnershipKind::Owned)) {
@@ -1941,7 +1940,7 @@ bool MemoryToRegisters::promoteSingleAllocation(AllocStackInst *alloc) {
19411940
}
19421941

19431942
// Remove write-only AllocStacks.
1944-
if (isWriteOnlyAllocation(alloc) && !shouldAddLexicalLifetime(alloc)) {
1943+
if (isWriteOnlyAllocation(alloc) && !lexicalLifetimeEnsured(alloc)) {
19451944
LLVM_DEBUG(llvm::dbgs() << "*** Deleting store-only AllocStack: "<< *alloc);
19461945
deleter.forceDeleteWithUsers(alloc);
19471946
return true;

0 commit comments

Comments
 (0)