Skip to content

Commit 9ae28a2

Browse files
committed
CastOptimizer: remove some unnecessary restrictions for existential metatype cast optimization
Those restrictions are copied from the `alloc_stack` case, but are not necessary for the `alloc_ref` case.
1 parent ad594f2 commit 9ae28a2

File tree

1 file changed

+19
-42
lines changed

1 file changed

+19
-42
lines changed

lib/SILOptimizer/Utils/CastOptimizer.cpp

Lines changed: 19 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,50 +1300,27 @@ CastOptimizer::optimizeCheckedCastBranchInst(CheckedCastBranchInst *Inst) {
13001300
// %1 = metatype $A.Type
13011301
// checked_cast_br %1, ....
13021302
if (auto *FoundIERI = dyn_cast<InitExistentialRefInst>(Op)) {
1303-
auto *ASRI = dyn_cast<AllocRefInst>(FoundIERI->getOperand());
1304-
if (!ASRI)
1303+
SILValue op = FoundIERI->getOperand();
1304+
if (!isa<AllocRefInst>(op))
13051305
return nullptr;
1306-
// Should be in the same BB.
1307-
if (ASRI->getParent() != EMI->getParent())
1308-
return nullptr;
1309-
// Check if this alloc_stack is only initialized once by means of
1310-
// a single init_existential_ref.
1311-
bool isLegal = true;
1312-
for (auto Use : getNonDebugUses(ASRI)) {
1313-
auto *User = Use->getUser();
1314-
if (isa<ExistentialMetatypeInst>(User) || isa<StrongReleaseInst>(User))
1315-
continue;
1316-
if (auto *IERI = dyn_cast<InitExistentialRefInst>(User)) {
1317-
if (IERI == FoundIERI) {
1318-
continue;
1319-
}
1320-
}
1321-
isLegal = false;
1322-
break;
1323-
}
13241306

1325-
if (isLegal && FoundIERI) {
1326-
// Should be in the same BB.
1327-
if (FoundIERI->getParent() != EMI->getParent())
1328-
return nullptr;
1329-
// Get the type used to initialize the existential.
1330-
auto ConcreteTy = FoundIERI->getFormalConcreteType();
1331-
// We don't know enough at compile time about existential
1332-
// and generic type parameters.
1333-
if (ConcreteTy.isAnyExistentialType() ||
1334-
ConcreteTy->is<ArchetypeType>())
1335-
return nullptr;
1336-
// Get the SIL metatype of this type.
1337-
auto EMT = EMI->getType().castTo<AnyMetatypeType>();
1338-
auto *MetaTy = MetatypeType::get(ConcreteTy, EMT->getRepresentation());
1339-
auto CanMetaTy = CanTypeWrapper<MetatypeType>(MetaTy);
1340-
auto SILMetaTy = SILType::getPrimitiveObjectType(CanMetaTy);
1341-
SILBuilderWithScope B(Inst, builderContext);
1342-
auto *MI = B.createMetatype(FoundIERI->getLoc(), SILMetaTy);
1343-
auto *NewI = replaceCastHelper(B, dynamicCast, MI);
1344-
eraseInstAction(Inst);
1345-
return NewI;
1346-
}
1307+
// Get the type used to initialize the existential.
1308+
auto ConcreteTy = FoundIERI->getFormalConcreteType();
1309+
// We don't know enough at compile time about existential
1310+
// and generic type parameters.
1311+
if (ConcreteTy.isAnyExistentialType() ||
1312+
ConcreteTy->is<ArchetypeType>())
1313+
return nullptr;
1314+
// Get the SIL metatype of this type.
1315+
auto EMT = EMI->getType().castTo<AnyMetatypeType>();
1316+
auto *MetaTy = MetatypeType::get(ConcreteTy, EMT->getRepresentation());
1317+
auto CanMetaTy = CanTypeWrapper<MetatypeType>(MetaTy);
1318+
auto SILMetaTy = SILType::getPrimitiveObjectType(CanMetaTy);
1319+
SILBuilderWithScope B(Inst, builderContext);
1320+
auto *MI = B.createMetatype(FoundIERI->getLoc(), SILMetaTy);
1321+
auto *NewI = replaceCastHelper(B, dynamicCast, MI);
1322+
eraseInstAction(Inst);
1323+
return NewI;
13471324
}
13481325
}
13491326

0 commit comments

Comments
 (0)