Skip to content

Commit 062d063

Browse files
committed
[NFC] MoveAddrChecker: Extracted function.
While the FIXME to derive whether an address begins initialized requires auditing all sites where the instruction is emitted to begin with, making this a predicate that depends only on the instruction can be done now.
1 parent 15db242 commit 062d063

File tree

1 file changed

+44
-39
lines changed

1 file changed

+44
-39
lines changed

lib/SILOptimizer/Mandatory/MoveOnlyAddressCheckerUtils.cpp

Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -987,35 +987,15 @@ static bool findNonEscapingPartialApplyUses(PartialApplyInst *pai,
987987
return true;
988988
}
989989

990-
void UseState::initializeLiveness(
991-
FieldSensitiveMultiDefPrunedLiveRange &liveness) {
992-
assert(liveness.getNumSubElements() == getNumSubelements());
993-
// We begin by initializing all of our init uses.
994-
for (auto initInstAndValue : initInsts) {
995-
LLVM_DEBUG(llvm::dbgs() << "Found def: " << *initInstAndValue.first);
996-
997-
liveness.initializeDef(initInstAndValue.first, initInstAndValue.second);
998-
}
999-
1000-
// If we have a reinitInstAndValue that we are going to be able to convert
1001-
// into a simple init, add it as an init. We are going to consider the rest of
1002-
// our reinit uses to be liveness uses.
1003-
for (auto reinitInstAndValue : reinitInsts) {
1004-
if (isReinitToInitConvertibleInst(reinitInstAndValue.first)) {
1005-
LLVM_DEBUG(llvm::dbgs() << "Found def: " << *reinitInstAndValue.first);
1006-
liveness.initializeDef(reinitInstAndValue.first,
1007-
reinitInstAndValue.second);
1008-
}
1009-
}
1010-
990+
static bool
991+
addressBeginsInitialized(MarkUnresolvedNonCopyableValueInst *address) {
1011992
// FIXME: Whether the initial use is an initialization ought to be entirely
1012993
// derivable from the CheckKind of the mark instruction.
1013994

1014-
// Then check if our markedValue is from an argument that is in,
1015-
// in_guaranteed, inout, or inout_aliasable, consider the marked address to be
1016-
// the initialization point.
1017-
bool beginsInitialized = false;
1018995
{
996+
// Then check if our markedValue is from an argument that is in,
997+
// in_guaranteed, inout, or inout_aliasable, consider the marked address to
998+
// be the initialization point.
1019999
SILValue operand = address->getOperand();
10201000
if (auto *c = dyn_cast<CopyableToMoveOnlyWrapperAddrInst>(operand))
10211001
operand = c->getOperand();
@@ -1034,7 +1014,7 @@ void UseState::initializeLiveness(
10341014
"an init... adding mark_unresolved_non_copyable_value as "
10351015
"init!\n");
10361016
// We cheat here slightly and use our address's operand.
1037-
beginsInitialized = true;
1017+
return true;
10381018
break;
10391019
case swift::SILArgumentConvention::Indirect_Out:
10401020
llvm_unreachable("Should never have out addresses here");
@@ -1059,7 +1039,7 @@ void UseState::initializeLiveness(
10591039
LLVM_DEBUG(llvm::dbgs()
10601040
<< "Found move only arg closure box use... "
10611041
"adding mark_unresolved_non_copyable_value as init!\n");
1062-
beginsInitialized = true;
1042+
return true;
10631043
break;
10641044
case SILAccessKind::Init:
10651045
break;
@@ -1081,14 +1061,14 @@ void UseState::initializeLiveness(
10811061
LLVM_DEBUG(llvm::dbgs()
10821062
<< "Found move only arg closure box use... "
10831063
"adding mark_unresolved_non_copyable_value as init!\n");
1084-
beginsInitialized = true;
1064+
return true;
10851065
}
10861066
} else if (auto *box = dyn_cast<AllocBoxInst>(
10871067
lookThroughOwnershipInsts(projectBox->getOperand()))) {
10881068
LLVM_DEBUG(llvm::dbgs()
10891069
<< "Found move only var allocbox use... "
10901070
"adding mark_unresolved_non_copyable_value as init!\n");
1091-
beginsInitialized = true;
1071+
return true;
10921072
}
10931073
}
10941074

@@ -1099,7 +1079,7 @@ void UseState::initializeLiveness(
10991079
LLVM_DEBUG(llvm::dbgs()
11001080
<< "Found ref_element_addr use... "
11011081
"adding mark_unresolved_non_copyable_value as init!\n");
1102-
beginsInitialized = true;
1082+
return true;
11031083
}
11041084

11051085
// Check if our address is from a global_addr. In such a case, we treat the
@@ -1109,7 +1089,7 @@ void UseState::initializeLiveness(
11091089
LLVM_DEBUG(llvm::dbgs()
11101090
<< "Found global_addr use... "
11111091
"adding mark_unresolved_non_copyable_value as init!\n");
1112-
beginsInitialized = true;
1092+
return true;
11131093
}
11141094

11151095
if (auto *ptai = dyn_cast<PointerToAddressInst>(
@@ -1118,21 +1098,21 @@ void UseState::initializeLiveness(
11181098
LLVM_DEBUG(llvm::dbgs()
11191099
<< "Found pointer to address use... "
11201100
"adding mark_unresolved_non_copyable_value as init!\n");
1121-
beginsInitialized = true;
1101+
return true;
11221102
}
11231103

11241104
if (auto *bai = dyn_cast_or_null<BeginApplyInst>(
11251105
stripAccessMarkers(address->getOperand())->getDefiningInstruction())) {
11261106
LLVM_DEBUG(llvm::dbgs()
11271107
<< "Adding accessor coroutine begin_apply as init!\n");
1128-
beginsInitialized = true;
1108+
return true;
11291109
}
11301110

11311111
if (auto *eai = dyn_cast<UncheckedTakeEnumDataAddrInst>(
11321112
stripAccessMarkers(address->getOperand()))) {
11331113
LLVM_DEBUG(llvm::dbgs()
11341114
<< "Adding enum projection as init!\n");
1135-
beginsInitialized = true;
1115+
return true;
11361116
}
11371117

11381118
// Assume a strict check of a temporary or formal access is initialized
@@ -1142,30 +1122,55 @@ void UseState::initializeLiveness(
11421122
asi && address->isStrict()) {
11431123
LLVM_DEBUG(llvm::dbgs()
11441124
<< "Adding strict-marked alloc_stack as init!\n");
1145-
beginsInitialized = true;
1125+
return true;
11461126
}
11471127

11481128
// Assume a strict-checked value initialized before the check.
11491129
if (address->isStrict()) {
11501130
LLVM_DEBUG(llvm::dbgs()
11511131
<< "Adding strict marker as init!\n");
1152-
beginsInitialized = true;
1132+
return true;
11531133
}
11541134

11551135
// Assume a value whose deinit has been dropped has been initialized.
11561136
if (auto *ddi = dyn_cast<DropDeinitInst>(address->getOperand())) {
11571137
LLVM_DEBUG(llvm::dbgs()
11581138
<< "Adding copyable_to_move_only_wrapper as init!\n");
1159-
beginsInitialized = true;
1139+
return true;
11601140
}
11611141

11621142
// Assume a value wrapped in a MoveOnlyWrapper is initialized.
11631143
if (auto *m2c = dyn_cast<CopyableToMoveOnlyWrapperAddrInst>(address->getOperand())) {
11641144
LLVM_DEBUG(llvm::dbgs()
11651145
<< "Adding copyable_to_move_only_wrapper as init!\n");
1166-
beginsInitialized = true;
1146+
return true;
11671147
}
1168-
1148+
return false;
1149+
}
1150+
1151+
void UseState::initializeLiveness(
1152+
FieldSensitiveMultiDefPrunedLiveRange &liveness) {
1153+
assert(liveness.getNumSubElements() == getNumSubelements());
1154+
// We begin by initializing all of our init uses.
1155+
for (auto initInstAndValue : initInsts) {
1156+
LLVM_DEBUG(llvm::dbgs() << "Found def: " << *initInstAndValue.first);
1157+
1158+
liveness.initializeDef(initInstAndValue.first, initInstAndValue.second);
1159+
}
1160+
1161+
// If we have a reinitInstAndValue that we are going to be able to convert
1162+
// into a simple init, add it as an init. We are going to consider the rest of
1163+
// our reinit uses to be liveness uses.
1164+
for (auto reinitInstAndValue : reinitInsts) {
1165+
if (isReinitToInitConvertibleInst(reinitInstAndValue.first)) {
1166+
LLVM_DEBUG(llvm::dbgs() << "Found def: " << *reinitInstAndValue.first);
1167+
liveness.initializeDef(reinitInstAndValue.first,
1168+
reinitInstAndValue.second);
1169+
}
1170+
}
1171+
1172+
bool beginsInitialized = addressBeginsInitialized(address);
1173+
11691174
if (beginsInitialized) {
11701175
recordInitUse(address, address, liveness.getTopLevelSpan());
11711176
liveness.initializeDef(SILValue(address), liveness.getTopLevelSpan());

0 commit comments

Comments
 (0)