@@ -544,6 +544,12 @@ struct AddressLoweringState {
544
544
return getBuilder (term->getParent ()->end (), term);
545
545
}
546
546
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
+
547
553
PhiRewriter &getPhiRewriter ();
548
554
549
555
SILValue getMaterializedAddress (SILValue origValue) const {
@@ -1347,6 +1353,32 @@ OpaqueStorageAllocation::getLeastCommonAncestorOfUses(SILValue value) {
1347
1353
return lca;
1348
1354
}
1349
1355
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
+
1350
1382
// Create alloc_stack that dominates an owned value \p value. Create
1351
1383
// jointly-postdominating dealloc_stack instructions. Nesting will be fixed
1352
1384
// later.
@@ -1370,28 +1402,7 @@ AllocStackInst *OpaqueStorageAllocation::createStackAllocation(SILValue value) {
1370
1402
// definition. Allocating as early as possible provides more opportunity for
1371
1403
// creating use projections into value. But allocation must be no earlier
1372
1404
// 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);
1395
1406
1396
1407
auto allocPt = latestOpeningInst
1397
1408
? latestOpeningInst->getNextInstruction ()->getIterator ()
0 commit comments