Skip to content

Commit e98df12

Browse files
committed
[AddressLowering] Extracted getLatestOpeningInst.
Promoted it from createStackAllocation to a member of AddressLoweringState so that it can be called elsewhere.
1 parent 37c1622 commit e98df12

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
@@ -544,6 +544,12 @@ struct AddressLoweringState {
544544
return getBuilder(term->getParent()->end(), term);
545545
}
546546

547+
/// The latest instruction which opens an archetype involved in the indicated
548+
/// type.
549+
///
550+
/// @returns nullable instruction
551+
SILInstruction *getLatestOpeningInst(SILType ty) const;
552+
547553
PhiRewriter &getPhiRewriter();
548554

549555
SILValue getMaterializedAddress(SILValue origValue) const {
@@ -1347,6 +1353,32 @@ OpaqueStorageAllocation::getLeastCommonAncestorOfUses(SILValue value) {
13471353
return lca;
13481354
}
13491355

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

13961407
auto allocPt = latestOpeningInst
13971408
? latestOpeningInst->getNextInstruction()->getIterator()

0 commit comments

Comments
 (0)