Skip to content

Commit a23bcef

Browse files
committed
[AddressLowering] NFC: Extracted function.
Promoted the functionality from createStackAllocation to a named member AddressLoweringState::getLatestOpeningInst so that it can be employed elsewhere.
1 parent a4aef54 commit a23bcef

File tree

1 file changed

+33
-22
lines changed

1 file changed

+33
-22
lines changed

lib/SILOptimizer/Mandatory/AddressLowering.cpp

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -552,6 +552,12 @@ struct AddressLoweringState {
552552
return getBuilder(term->getParent()->end(), term);
553553
}
554554

555+
/// The latest instruction which opens an archetype involved in the indicated
556+
/// type.
557+
///
558+
/// @returns nullable instruction
559+
SILInstruction *getLatestOpeningInst(SILType ty) const;
560+
555561
PhiRewriter &getPhiRewriter();
556562

557563
SILValue getMaterializedAddress(SILValue origValue) const {
@@ -1343,6 +1349,32 @@ void OpaqueStorageAllocation::removeAllocation(SILValue value) {
13431349
pass.deleter.forceDelete(allocInst);
13441350
}
13451351

1352+
SILInstruction *AddressLoweringState::getLatestOpeningInst(SILType ty) const {
1353+
SILInstruction *latestOpeningInst = nullptr;
1354+
ty.getASTType().visit([&](CanType type) {
1355+
auto archetype = dyn_cast<ArchetypeType>(type);
1356+
if (!archetype)
1357+
return;
1358+
1359+
if (auto openedTy = getOpenedArchetypeOf(archetype)) {
1360+
auto openingVal =
1361+
getModule()->getRootLocalArchetypeDef(openedTy, function);
1362+
1363+
auto *openingInst = openingVal->getDefiningInstruction();
1364+
assert(openingVal && "all opened archetypes should be resolved");
1365+
if (latestOpeningInst) {
1366+
if (domInfo->dominates(openingInst, latestOpeningInst))
1367+
return;
1368+
1369+
assert(domInfo->dominates(latestOpeningInst, openingInst) &&
1370+
"opened archetypes must dominate their uses");
1371+
}
1372+
latestOpeningInst = openingInst;
1373+
}
1374+
});
1375+
return latestOpeningInst;
1376+
}
1377+
13461378
// Create alloc_stack that dominates an owned value \p value. Create
13471379
// jointly-postdominating dealloc_stack instructions. Nesting will be fixed
13481380
// later.
@@ -1366,28 +1398,7 @@ AllocStackInst *OpaqueStorageAllocation::createStackAllocation(SILValue value) {
13661398
// definition. Allocating as early as possible provides more opportunity for
13671399
// creating use projections into value. But allocation must be no earlier
13681400
// than the latest type definition.
1369-
SILInstruction *latestOpeningInst = nullptr;
1370-
allocTy.getASTType().visit([&](CanType type) {
1371-
auto archetype = dyn_cast<ArchetypeType>(type);
1372-
if (!archetype)
1373-
return;
1374-
1375-
if (auto openedTy = getOpenedArchetypeOf(archetype)) {
1376-
auto openingVal =
1377-
pass.getModule()->getRootLocalArchetypeDef(openedTy, pass.function);
1378-
1379-
auto *openingInst = openingVal->getDefiningInstruction();
1380-
assert(openingVal && "all opened archetypes should be resolved");
1381-
if (latestOpeningInst) {
1382-
if (pass.domInfo->dominates(openingInst, latestOpeningInst))
1383-
return;
1384-
1385-
assert(pass.domInfo->dominates(latestOpeningInst, openingInst) &&
1386-
"opened archetypes must dominate their uses");
1387-
}
1388-
latestOpeningInst = openingInst;
1389-
}
1390-
});
1401+
auto *latestOpeningInst = pass.getLatestOpeningInst(allocTy);
13911402

13921403
auto allocPt = latestOpeningInst
13931404
? latestOpeningInst->getNextInstruction()->getIterator()

0 commit comments

Comments
 (0)