@@ -57,6 +57,7 @@ STATISTIC(NumInstRemoved, "Number of Instructions removed");
57
57
58
58
static bool lexicalLifetimeEnsured (AllocStackInst *asi);
59
59
static bool isGuaranteedLexicalValue (SILValue src);
60
+ static bool isOwnedLexicalValue (SILValue src);
60
61
61
62
namespace {
62
63
@@ -96,9 +97,11 @@ class LiveValues {
96
97
if (!lexicalLifetimeEnsured (asi)) {
97
98
return stored;
98
99
}
99
- // We should have created a move of the @owned stored value.
100
- assert (move);
101
- return move;
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;
102
105
}
103
106
104
107
bool canEndLexicalLifetime () {
@@ -107,7 +110,8 @@ class LiveValues {
107
110
// to end a lexical lifetime. In that case, the lifetime end will be
108
111
// added later, when we have enough information, namely the live in
109
112
// values, to end it.
110
- return move;
113
+ auto storedIsLexical = stored && isOwnedLexicalValue (stored);
114
+ return storedIsLexical ? stored : move;
111
115
}
112
116
};
113
117
struct Guaranteed {
@@ -522,6 +526,10 @@ static bool lexicalLifetimeEnsured(AllocStackInst *asi) {
522
526
!asi->getElementType ().isTrivial (*asi->getFunction ());
523
527
}
524
528
529
+ static bool isOwnedLexicalValue (SILValue src) {
530
+ return src->getOwnershipKind () == OwnershipKind::Owned && src->isLexical ();
531
+ }
532
+
525
533
static bool isGuaranteedLexicalValue (SILValue src) {
526
534
return src->getOwnershipKind () == OwnershipKind::Guaranteed &&
527
535
src->isLexical ();
@@ -542,6 +550,9 @@ beginOwnedLexicalLifetimeAfterStore(AllocStackInst *asi, StoreInst *inst) {
542
550
SILValue stored = inst->getOperand (CopyLikeInstruction::Src);
543
551
SILLocation loc = RegularLocation::getAutoGeneratedLocation (inst->getLoc ());
544
552
553
+ if (isOwnedLexicalValue (stored)) {
554
+ return {LiveValues::forOwned (stored, {}), /* isStorageValid*/ true };
555
+ }
545
556
MoveValueInst *mvi = nullptr ;
546
557
SILBuilderWithScope::insertAfter (inst, [&](SILBuilder &builder) {
547
558
mvi = builder.createMoveValue (loc, stored, /* isLexical*/ true );
@@ -1063,6 +1074,10 @@ StackAllocationPromoter::getLiveOutValues(BlockSetVector &phiBlocks,
1063
1074
auto values = LiveValues::forGuaranteed (stored, borrow);
1064
1075
return values;
1065
1076
}
1077
+ if (isOwnedLexicalValue (stored)) {
1078
+ auto values = LiveValues::forOwned (stored, {});
1079
+ return values;
1080
+ }
1066
1081
auto move = cast<MoveValueInst>(inst->getNextInstruction ());
1067
1082
auto values = LiveValues::forOwned (stored, move);
1068
1083
return values;
0 commit comments