@@ -54,7 +54,7 @@ STATISTIC(NumAllocStackFound, "Number of AllocStack found");
54
54
STATISTIC (NumAllocStackCaptured, " Number of AllocStack captured" );
55
55
STATISTIC (NumInstRemoved, " Number of Instructions removed" );
56
56
57
- static bool shouldAddLexicalLifetime (AllocStackInst *asi);
57
+ static bool lexicalLifetimeEnsured (AllocStackInst *asi);
58
58
static bool isGuaranteedLexicalValue (SILValue src);
59
59
60
60
namespace {
@@ -83,15 +83,15 @@ struct LiveValues {
83
83
// / usages will be replaced with usages of the stored field. The
84
84
// / implementation constructs an instance to match those requirements.
85
85
static LiveValues toReplace (AllocStackInst *asi, SILValue replacement) {
86
- if (shouldAddLexicalLifetime (asi))
86
+ if (lexicalLifetimeEnsured (asi))
87
87
return {SILValue (), SILValue (), replacement};
88
88
return {replacement, SILValue (), SILValue ()};
89
89
};
90
90
91
91
// / The value with which usages of the provided AllocStackInst should be
92
92
// / replaced.
93
93
SILValue replacement (AllocStackInst *asi, SILInstruction *toReplace) {
94
- if (!shouldAddLexicalLifetime (asi)) {
94
+ if (!lexicalLifetimeEnsured (asi)) {
95
95
return stored;
96
96
}
97
97
// For lexical AllocStackInsts, which are store_borrow locations, we may
@@ -301,7 +301,7 @@ replaceLoad(SILInstruction *inst, SILValue newValue, AllocStackInst *asi,
301
301
op = inst->getOperand (0 );
302
302
303
303
if (auto *lbi = dyn_cast<LoadBorrowInst>(inst)) {
304
- if (shouldAddLexicalLifetime (asi)) {
304
+ if (lexicalLifetimeEnsured (asi)) {
305
305
assert (newValue->getOwnershipKind () == OwnershipKind::Guaranteed);
306
306
SmallVector<SILInstruction *, 4 > endBorrows;
307
307
for (auto *ebi : lbi->getUsersOfType <EndBorrowInst>()) {
@@ -311,8 +311,7 @@ replaceLoad(SILInstruction *inst, SILValue newValue, AllocStackInst *asi,
311
311
prepareForDeletion (ebi, instructionsToDelete);
312
312
}
313
313
lbi->replaceAllUsesWith (newValue);
314
- }
315
- else {
314
+ } else {
316
315
auto *borrow = SILBuilderWithScope (lbi, ctx).createBeginBorrow (
317
316
lbi->getLoc (), newValue, asi->isLexical ());
318
317
lbi->replaceAllUsesWith (borrow);
@@ -367,7 +366,7 @@ static SILValue createValueForEmptyTuple(SILType ty,
367
366
368
367
// / Whether lexical lifetimes should be added for the values stored into the
369
368
// / alloc_stack.
370
- static bool shouldAddLexicalLifetime (AllocStackInst *asi) {
369
+ static bool lexicalLifetimeEnsured (AllocStackInst *asi) {
371
370
return asi->getFunction ()->hasOwnership () &&
372
371
asi->getFunction ()
373
372
->getModule ()
@@ -412,7 +411,7 @@ static bool canEndLexicalLifetime(LiveValues values) { return values.borrow; }
412
411
static StorageStateTracking<LiveValues>
413
412
beginLexicalLifetimeAfterStore (AllocStackInst *asi, SILInstruction *inst) {
414
413
assert (isa<StoreInst>(inst) || isa<StoreBorrowInst>(inst));
415
- assert (shouldAddLexicalLifetime (asi));
414
+ assert (lexicalLifetimeEnsured (asi));
416
415
SILValue stored = inst->getOperand (CopyLikeInstruction::Src);
417
416
SILLocation loc = RegularLocation::getAutoGeneratedLocation (inst->getLoc ());
418
417
@@ -452,7 +451,7 @@ static void endOwnedLexicalLifetimeBeforeInst(AllocStackInst *asi,
452
451
SILInstruction *beforeInstruction,
453
452
SILBuilderContext &ctx,
454
453
LiveValues values) {
455
- assert (shouldAddLexicalLifetime (asi));
454
+ assert (lexicalLifetimeEnsured (asi));
456
455
assert (beforeInstruction);
457
456
458
457
auto builder = SILBuilderWithScope (beforeInstruction, ctx);
@@ -467,7 +466,7 @@ static void endOwnedLexicalLifetimeBeforeInst(AllocStackInst *asi,
467
466
static void endGuaranteedLexicalLifetimeBeforeInst (
468
467
AllocStackInst *asi, SILInstruction *beforeInstruction,
469
468
SILBuilderContext &ctx, LiveValues values) {
470
- assert (shouldAddLexicalLifetime (asi));
469
+ assert (lexicalLifetimeEnsured (asi));
471
470
assert (beforeInstruction);
472
471
assert (values.borrow );
473
472
@@ -667,7 +666,7 @@ SILInstruction *StackAllocationPromoter::promoteAllocationInBlock(
667
666
assert (!runningVals || runningVals->isStorageValid );
668
667
auto *li = dyn_cast<LoadInst>(inst);
669
668
if (li && li->getOwnershipQualifier () == LoadOwnershipQualifier::Take) {
670
- if (shouldAddLexicalLifetime (asi)) {
669
+ if (lexicalLifetimeEnsured (asi)) {
671
670
// End the lexical lifetime at a load [take]. The storage is no
672
671
// longer keeping the value alive.
673
672
if (runningVals && canEndLexicalLifetime (runningVals->value )) {
@@ -723,7 +722,7 @@ SILInstruction *StackAllocationPromoter::promoteAllocationInBlock(
723
722
auto *newLoad = localBuilder.createLoad (si->getLoc (), asi,
724
723
LoadOwnershipQualifier::Take);
725
724
localBuilder.createDestroyValue (si->getLoc (), newLoad);
726
- if (shouldAddLexicalLifetime (asi)) {
725
+ if (lexicalLifetimeEnsured (asi)) {
727
726
assert (!deinitializationPoints[blockPromotingWithin]);
728
727
deinitializationPoints[blockPromotingWithin] = si;
729
728
}
@@ -750,7 +749,7 @@ SILInstruction *StackAllocationPromoter::promoteAllocationInBlock(
750
749
// The current store is now the lastStoreInst (until we see
751
750
// another).
752
751
lastStoreInst = inst;
753
- if (shouldAddLexicalLifetime (asi)) {
752
+ if (lexicalLifetimeEnsured (asi)) {
754
753
if (oldRunningVals && oldRunningVals->isStorageValid &&
755
754
canEndLexicalLifetime (oldRunningVals->value )) {
756
755
endOwnedLexicalLifetimeBeforeInst (asi, /* beforeInstruction=*/ inst,
@@ -778,15 +777,15 @@ SILInstruction *StackAllocationPromoter::promoteAllocationInBlock(
778
777
/* isStorageValid=*/ true };
779
778
// The current store is now the lastStoreInst.
780
779
lastStoreInst = inst;
781
- if (shouldAddLexicalLifetime (asi)) {
780
+ if (lexicalLifetimeEnsured (asi)) {
782
781
runningVals = beginLexicalLifetimeAfterStore (asi, inst);
783
782
}
784
783
continue ;
785
784
}
786
785
787
786
// End the lexical lifetime of the store_borrow source.
788
787
if (auto *ebi = dyn_cast<EndBorrowInst>(inst)) {
789
- if (!shouldAddLexicalLifetime (asi)) {
788
+ if (!lexicalLifetimeEnsured (asi)) {
790
789
continue ;
791
790
}
792
791
auto *sbi = dyn_cast<StoreBorrowInst>(ebi->getOperand ());
@@ -833,7 +832,7 @@ SILInstruction *StackAllocationPromoter::promoteAllocationInBlock(
833
832
if (runningVals) {
834
833
replaceDestroy (dai, runningVals->value .replacement (asi, dai), ctx,
835
834
deleter, instructionsToDelete);
836
- if (shouldAddLexicalLifetime (asi)) {
835
+ if (lexicalLifetimeEnsured (asi)) {
837
836
endOwnedLexicalLifetimeBeforeInst (asi, /* beforeInstruction=*/ dai, ctx,
838
837
runningVals->value );
839
838
}
@@ -873,7 +872,7 @@ void StackAllocationPromoter::addBlockArguments(BlockSetVector &phiBlocks) {
873
872
for (auto *block : phiBlocks) {
874
873
// The stored value.
875
874
block->createPhiArgument (asi->getElementType (), OwnershipKind::Owned);
876
- if (shouldAddLexicalLifetime (asi)) {
875
+ if (lexicalLifetimeEnsured (asi)) {
877
876
// The borrow scope.
878
877
block->createPhiArgument (asi->getElementType (),
879
878
OwnershipKind::Guaranteed);
@@ -901,7 +900,7 @@ StackAllocationPromoter::getLiveOutValues(BlockSetVector &phiBlocks,
901
900
SILValue stored = inst->getOperand (CopyLikeInstruction::Src);
902
901
LLVM_DEBUG (llvm::dbgs () << " *** Found Store def " << stored);
903
902
904
- if (!shouldAddLexicalLifetime (asi)) {
903
+ if (!lexicalLifetimeEnsured (asi)) {
905
904
LiveValues values = {stored, SILValue (), SILValue ()};
906
905
return values;
907
906
}
@@ -927,7 +926,7 @@ StackAllocationPromoter::getLiveOutValues(BlockSetVector &phiBlocks,
927
926
SILValue original;
928
927
SILValue borrow;
929
928
SILValue copy;
930
- if (shouldAddLexicalLifetime (asi)) {
929
+ if (lexicalLifetimeEnsured (asi)) {
931
930
original = domBlock->getArgument (domBlock->getNumArguments () - 3 );
932
931
borrow = domBlock->getArgument (domBlock->getNumArguments () - 2 );
933
932
copy = domBlock->getArgument (domBlock->getNumArguments () - 1 );
@@ -970,7 +969,7 @@ StackAllocationPromoter::getLiveInValues(BlockSetVector &phiBlocks,
970
969
SILValue original;
971
970
SILValue borrow;
972
971
SILValue copy;
973
- if (shouldAddLexicalLifetime (asi)) {
972
+ if (lexicalLifetimeEnsured (asi)) {
974
973
original = block->getArgument (block->getNumArguments () - 3 );
975
974
borrow = block->getArgument (block->getNumArguments () - 2 );
976
975
copy = block->getArgument (block->getNumArguments () - 1 );
@@ -1017,7 +1016,7 @@ void StackAllocationPromoter::fixPhiPredBlock(BlockSetVector &phiBlocks,
1017
1016
1018
1017
SmallVector<SILValue> vals;
1019
1018
vals.push_back (def.stored );
1020
- if (shouldAddLexicalLifetime (asi)) {
1019
+ if (lexicalLifetimeEnsured (asi)) {
1021
1020
vals.push_back (def.borrow );
1022
1021
vals.push_back (def.copy );
1023
1022
}
@@ -1185,7 +1184,7 @@ void StackAllocationPromoter::fixBranchesAndUses(BlockSetVector &phiBlocks,
1185
1184
block->getArgument (block->getNumArguments () - 1 ));
1186
1185
if (!livePhis.contains (proactivePhi)) {
1187
1186
eraseLastPhiFromBlock (block);
1188
- if (shouldAddLexicalLifetime (asi)) {
1187
+ if (lexicalLifetimeEnsured (asi)) {
1189
1188
eraseLastPhiFromBlock (block);
1190
1189
eraseLastPhiFromBlock (block);
1191
1190
}
@@ -1231,7 +1230,7 @@ void StackAllocationPromoter::fixBranchesAndUses(BlockSetVector &phiBlocks,
1231
1230
// / This can only happen if the successor is a CFG merge and all paths
1232
1231
// / from here lead to unreachable.
1233
1232
void StackAllocationPromoter::endLexicalLifetime (BlockSetVector &phiBlocks) {
1234
- if (!shouldAddLexicalLifetime (asi))
1233
+ if (!lexicalLifetimeEnsured (asi))
1235
1234
return ;
1236
1235
1237
1236
// We need to separately consider and visit incoming unopened borrow scopes
@@ -1350,9 +1349,9 @@ void StackAllocationPromoter::pruneAllocStackUsage() {
1350
1349
1351
1350
for (auto block : functionBlocks)
1352
1351
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.
1356
1355
initializationPoints[block] = si;
1357
1356
}
1358
1357
@@ -1768,7 +1767,7 @@ void MemoryToRegisters::removeSingleBlockAllocation(AllocStackInst *asi) {
1768
1767
auto *loadInst = dyn_cast<LoadInst>(inst);
1769
1768
if (loadInst &&
1770
1769
loadInst->getOwnershipQualifier () == LoadOwnershipQualifier::Take) {
1771
- if (shouldAddLexicalLifetime (asi)) {
1770
+ if (lexicalLifetimeEnsured (asi)) {
1772
1771
// End the lexical lifetime at a load [take]. The storage is no
1773
1772
// longer keeping the value alive.
1774
1773
endOwnedLexicalLifetimeBeforeInst (asi, /* beforeInstruction=*/ inst,
@@ -1796,7 +1795,7 @@ void MemoryToRegisters::removeSingleBlockAllocation(AllocStackInst *asi) {
1796
1795
auto oldRunningVals = runningVals;
1797
1796
runningVals = {LiveValues::toReplace (asi, /* replacement=*/ si->getSrc ()),
1798
1797
/* isStorageValid=*/ true };
1799
- if (shouldAddLexicalLifetime (asi)) {
1798
+ if (lexicalLifetimeEnsured (asi)) {
1800
1799
if (oldRunningVals && oldRunningVals->isStorageValid ) {
1801
1800
endOwnedLexicalLifetimeBeforeInst (asi, /* beforeInstruction=*/ si, ctx,
1802
1801
oldRunningVals->value );
@@ -1814,7 +1813,7 @@ void MemoryToRegisters::removeSingleBlockAllocation(AllocStackInst *asi) {
1814
1813
}
1815
1814
runningVals = {LiveValues::toReplace (asi, /* replacement=*/ sbi->getSrc ()),
1816
1815
/* isStorageValid=*/ true };
1817
- if (shouldAddLexicalLifetime (asi)) {
1816
+ if (lexicalLifetimeEnsured (asi)) {
1818
1817
runningVals = beginLexicalLifetimeAfterStore (asi, inst);
1819
1818
}
1820
1819
continue ;
@@ -1866,7 +1865,7 @@ void MemoryToRegisters::removeSingleBlockAllocation(AllocStackInst *asi) {
1866
1865
assert (runningVals && runningVals->isStorageValid );
1867
1866
replaceDestroy (dai, runningVals->value .replacement (asi, dai), ctx,
1868
1867
deleter, instructionsToDelete);
1869
- if (shouldAddLexicalLifetime (asi)) {
1868
+ if (lexicalLifetimeEnsured (asi)) {
1870
1869
endOwnedLexicalLifetimeBeforeInst (asi, /* beforeInstruction=*/ dai, ctx,
1871
1870
runningVals->value );
1872
1871
}
@@ -1898,7 +1897,7 @@ void MemoryToRegisters::removeSingleBlockAllocation(AllocStackInst *asi) {
1898
1897
}
1899
1898
}
1900
1899
1901
- if (shouldAddLexicalLifetime (asi) && runningVals &&
1900
+ if (lexicalLifetimeEnsured (asi) && runningVals &&
1902
1901
runningVals->isStorageValid &&
1903
1902
runningVals->value .stored ->getOwnershipKind ().isCompatibleWith (
1904
1903
OwnershipKind::Owned)) {
@@ -1941,7 +1940,7 @@ bool MemoryToRegisters::promoteSingleAllocation(AllocStackInst *alloc) {
1941
1940
}
1942
1941
1943
1942
// Remove write-only AllocStacks.
1944
- if (isWriteOnlyAllocation (alloc) && !shouldAddLexicalLifetime (alloc)) {
1943
+ if (isWriteOnlyAllocation (alloc) && !lexicalLifetimeEnsured (alloc)) {
1945
1944
LLVM_DEBUG (llvm::dbgs () << " *** Deleting store-only AllocStack: " << *alloc);
1946
1945
deleter.forceDeleteWithUsers (alloc);
1947
1946
return true ;
0 commit comments