Skip to content

Commit c13d8f7

Browse files
authored
Merge pull request #65143 from apple/revert-65022-rdar99160718
Revert "[Mem2Reg] Omit lexical moves for lexical values."
2 parents aace459 + 6b19388 commit c13d8f7

File tree

4 files changed

+246
-372
lines changed

4 files changed

+246
-372
lines changed

lib/SILOptimizer/Transforms/SILMem2Reg.cpp

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ STATISTIC(NumInstRemoved, "Number of Instructions removed");
5757

5858
static bool lexicalLifetimeEnsured(AllocStackInst *asi);
5959
static bool isGuaranteedLexicalValue(SILValue src);
60-
static bool isOwnedLexicalValue(SILValue src);
6160

6261
namespace {
6362

@@ -97,11 +96,9 @@ class LiveValues {
9796
if (!lexicalLifetimeEnsured(asi)) {
9897
return stored;
9998
}
100-
auto storedIsLexical = stored && isOwnedLexicalValue(stored);
101-
// If the value was already lexical, we use it directly. Otherwise, a new
102-
// move_value [lexical] is used.
103-
assert(storedIsLexical || move);
104-
return storedIsLexical ? stored : move;
99+
// We should have created a move of the @owned stored value.
100+
assert(move);
101+
return move;
105102
}
106103

107104
bool canEndLexicalLifetime() {
@@ -110,8 +107,7 @@ class LiveValues {
110107
// to end a lexical lifetime. In that case, the lifetime end will be
111108
// added later, when we have enough information, namely the live in
112109
// values, to end it.
113-
auto storedIsLexical = stored && isOwnedLexicalValue(stored);
114-
return storedIsLexical ? stored : move;
110+
return move;
115111
}
116112
};
117113
struct Guaranteed {
@@ -225,7 +221,6 @@ class LiveValues {
225221
return guaranteed.stored;
226222
}
227223

228-
/// Whether it's possible and appropriate to end the lifetime.
229224
bool canEndLexicalLifetime() {
230225
if (auto *owned = storage.dyn_cast<Owned>()) {
231226
return owned->canEndLexicalLifetime();
@@ -526,15 +521,16 @@ static bool lexicalLifetimeEnsured(AllocStackInst *asi) {
526521
!asi->getElementType().isTrivial(*asi->getFunction());
527522
}
528523

529-
static bool isOwnedLexicalValue(SILValue src) {
530-
return src->getOwnershipKind() == OwnershipKind::Owned && src->isLexical();
531-
}
532-
533524
static bool isGuaranteedLexicalValue(SILValue src) {
534525
return src->getOwnershipKind() == OwnershipKind::Guaranteed &&
535526
src->isLexical();
536527
}
537528

529+
/// Returns true if we have enough information to end the lifetime.
530+
static bool canEndLexicalLifetime(LiveValues values) {
531+
return values.canEndLexicalLifetime();
532+
}
533+
538534
/// Begin a lexical borrow scope for the value stored into the provided
539535
/// StoreInst after that instruction.
540536
///
@@ -550,9 +546,6 @@ beginOwnedLexicalLifetimeAfterStore(AllocStackInst *asi, StoreInst *inst) {
550546
SILValue stored = inst->getOperand(CopyLikeInstruction::Src);
551547
SILLocation loc = RegularLocation::getAutoGeneratedLocation(inst->getLoc());
552548

553-
if (isOwnedLexicalValue(stored)) {
554-
return {LiveValues::forOwned(stored, {}), /*isStorageValid*/ true};
555-
}
556549
MoveValueInst *mvi = nullptr;
557550
SILBuilderWithScope::insertAfter(inst, [&](SILBuilder &builder) {
558551
mvi = builder.createMoveValue(loc, stored, /*isLexical*/ true);
@@ -832,7 +825,7 @@ SILInstruction *StackAllocationPromoter::promoteAllocationInBlock(
832825
if (lexicalLifetimeEnsured(asi)) {
833826
// End the lexical lifetime at a load [take]. The storage is no
834827
// longer keeping the value alive.
835-
if (runningVals && runningVals->value.canEndLexicalLifetime()) {
828+
if (runningVals && canEndLexicalLifetime(runningVals->value)) {
836829
// End it right now if we have enough information.
837830
endOwnedLexicalLifetimeBeforeInst(asi, /*beforeInstruction=*/li,
838831
ctx,
@@ -915,7 +908,7 @@ SILInstruction *StackAllocationPromoter::promoteAllocationInBlock(
915908
lastStoreInst = si;
916909
if (lexicalLifetimeEnsured(asi)) {
917910
if (oldRunningVals && oldRunningVals->isStorageValid &&
918-
oldRunningVals->value.canEndLexicalLifetime()) {
911+
canEndLexicalLifetime(oldRunningVals->value)) {
919912
endOwnedLexicalLifetimeBeforeInst(asi, /*beforeInstruction=*/si, ctx,
920913
oldRunningVals->value.getOwned());
921914
}
@@ -972,7 +965,7 @@ SILInstruction *StackAllocationPromoter::promoteAllocationInBlock(
972965
}
973966
// Mark storage as invalid and mark end_borrow as a deinit point.
974967
runningVals->isStorageValid = false;
975-
if (!runningVals->value.canEndLexicalLifetime()) {
968+
if (!canEndLexicalLifetime(runningVals->value)) {
976969
continue;
977970
}
978971
endGuaranteedLexicalLifetimeBeforeInst(
@@ -1074,10 +1067,6 @@ StackAllocationPromoter::getLiveOutValues(BlockSetVector &phiBlocks,
10741067
auto values = LiveValues::forGuaranteed(stored, borrow);
10751068
return values;
10761069
}
1077-
if (isOwnedLexicalValue(stored)) {
1078-
auto values = LiveValues::forOwned(stored, {});
1079-
return values;
1080-
}
10811070
auto move = cast<MoveValueInst>(inst->getNextInstruction());
10821071
auto values = LiveValues::forOwned(stored, move);
10831072
return values;
@@ -1433,7 +1422,7 @@ void StackAllocationPromoter::endLexicalLifetime(BlockSetVector &phiBlocks) {
14331422
if (isa<EndBorrowInst>(inst)) {
14341423
// Not all store_borrows will have a begin_borrow [lexical] that needs
14351424
// to be ended. If the source is already lexical, we don't create it.
1436-
if (!values->canEndLexicalLifetime()) {
1425+
if (!canEndLexicalLifetime(*values)) {
14371426
continue;
14381427
}
14391428
endGuaranteedLexicalLifetimeBeforeInst(
@@ -1456,7 +1445,7 @@ void StackAllocationPromoter::endLexicalLifetime(BlockSetVector &phiBlocks) {
14561445
if (terminatesInUnreachable || uniqueSuccessorLacksLiveInValues()) {
14571446
auto values = getLiveOutValues(phiBlocks, bb);
14581447
if (values->isGuaranteed()) {
1459-
if (!values->canEndLexicalLifetime()) {
1448+
if (!canEndLexicalLifetime(*values)) {
14601449
continue;
14611450
}
14621451
endGuaranteedLexicalLifetimeBeforeInst(
@@ -1982,7 +1971,7 @@ void MemoryToRegisters::removeSingleBlockAllocation(AllocStackInst *asi) {
19821971
continue;
19831972
}
19841973
runningVals->isStorageValid = false;
1985-
if (!runningVals->value.canEndLexicalLifetime()) {
1974+
if (!canEndLexicalLifetime(runningVals->value)) {
19861975
continue;
19871976
}
19881977
endGuaranteedLexicalLifetimeBeforeInst(

0 commit comments

Comments
 (0)