@@ -552,6 +552,12 @@ struct AddressLoweringState {
552
552
return getBuilder (term->getParent ()->end (), term);
553
553
}
554
554
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
+
555
561
PhiRewriter &getPhiRewriter ();
556
562
557
563
SILValue getMaterializedAddress (SILValue origValue) const {
@@ -1343,6 +1349,32 @@ void OpaqueStorageAllocation::removeAllocation(SILValue value) {
1343
1349
pass.deleter .forceDelete (allocInst);
1344
1350
}
1345
1351
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
+
1346
1378
// Create alloc_stack that dominates an owned value \p value. Create
1347
1379
// jointly-postdominating dealloc_stack instructions. Nesting will be fixed
1348
1380
// later.
@@ -1366,28 +1398,7 @@ AllocStackInst *OpaqueStorageAllocation::createStackAllocation(SILValue value) {
1366
1398
// definition. Allocating as early as possible provides more opportunity for
1367
1399
// creating use projections into value. But allocation must be no earlier
1368
1400
// 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);
1391
1402
1392
1403
auto allocPt = latestOpeningInst
1393
1404
? latestOpeningInst->getNextInstruction ()->getIterator ()
0 commit comments