Skip to content

Commit 903721c

Browse files
committed
SIL: Remove SILType::getMetatypeInstanceType()
1 parent 2970334 commit 903721c

File tree

7 files changed

+45
-43
lines changed

7 files changed

+45
-43
lines changed

include/swift/SIL/SILType.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -477,12 +477,6 @@ class SILType {
477477
/// representation. Class existentials do not always qualify.
478478
bool isHeapObjectReferenceType() const;
479479

480-
/// Return the SILType corresponding to the underlying type of the given
481-
/// metatype type.
482-
///
483-
/// *NOTE* Only call on SILTypes for metatype types.
484-
SILType getMetatypeInstanceType(SILModule& M) const;
485-
486480
/// Returns true if this SILType is an aggregate that contains \p Ty
487481
bool aggregateContainsRecord(SILType Ty, SILModule &SILMod) const;
488482

lib/SIL/SILType.cpp

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -216,17 +216,6 @@ bool SILType::isHeapObjectReferenceType() const {
216216
return false;
217217
}
218218

219-
SILType SILType::getMetatypeInstanceType(SILModule &M) const {
220-
CanType MetatypeType = getASTType();
221-
assert(MetatypeType->is<AnyMetatypeType>() &&
222-
"This method should only be called on SILTypes with an underlying "
223-
"metatype type.");
224-
Type instanceType =
225-
MetatypeType->castTo<AnyMetatypeType>()->getInstanceType();
226-
227-
return M.Types.getLoweredType(instanceType->getCanonicalType());
228-
}
229-
230219
bool SILType::aggregateContainsRecord(SILType Record, SILModule &Mod) const {
231220
assert(!hasArchetype() && "Agg should be proven to not be generic "
232221
"before passed to this function.");
@@ -551,8 +540,7 @@ bool SILType::hasAbstractionDifference(SILFunctionTypeRepresentation rep,
551540
bool SILType::isLoweringOf(SILModule &Mod, CanType formalType) {
552541
SILType loweredType = *this;
553542

554-
// Optional lowers its contained type. The difference between Optional
555-
// and IUO is lowered away.
543+
// Optional lowers its contained type.
556544
SILType loweredObjectType = loweredType.getOptionalObjectType();
557545
CanType formalObjectType = formalType.getOptionalObjectType();
558546

@@ -562,10 +550,9 @@ bool SILType::isLoweringOf(SILModule &Mod, CanType formalType) {
562550
}
563551

564552
// Metatypes preserve their instance type through lowering.
565-
if (loweredType.is<MetatypeType>()) {
553+
if (auto loweredMT = loweredType.getAs<MetatypeType>()) {
566554
if (auto formalMT = dyn_cast<MetatypeType>(formalType)) {
567-
return loweredType.getMetatypeInstanceType(Mod).isLoweringOf(
568-
Mod, formalMT.getInstanceType());
555+
return loweredMT.getInstanceType() == formalMT.getInstanceType();
569556
}
570557
}
571558

lib/SIL/SILVerifier.cpp

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3100,7 +3100,7 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
31003100
"alloc_existential_box must be used with a boxed existential "
31013101
"type");
31023102

3103-
checkExistentialProtocolConformances(exType,
3103+
checkExistentialProtocolConformances(exType.getASTType(),
31043104
AEBI->getFormalConcreteType(),
31053105
AEBI->getConformances());
31063106
verifyOpenedArchetype(AEBI, AEBI->getFormalConcreteType());
@@ -3132,7 +3132,7 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
31323132
"init_existential_addr payload must be a lowering of the formal "
31333133
"concrete type");
31343134

3135-
checkExistentialProtocolConformances(exType,
3135+
checkExistentialProtocolConformances(exType.getASTType(),
31363136
AEI->getFormalConcreteType(),
31373137
AEI->getConformances());
31383138
verifyOpenedArchetype(AEI, AEI->getFormalConcreteType());
@@ -3159,7 +3159,7 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
31593159
"init_existential_value operand must be a lowering of the formal "
31603160
"concrete type");
31613161

3162-
checkExistentialProtocolConformances(exType,
3162+
checkExistentialProtocolConformances(exType.getASTType(),
31633163
IEI->getFormalConcreteType(),
31643164
IEI->getConformances());
31653165
verifyOpenedArchetype(IEI, IEI->getFormalConcreteType());
@@ -3190,7 +3190,7 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
31903190
"init_existential_ref operand must be a lowering of the formal "
31913191
"concrete type");
31923192

3193-
checkExistentialProtocolConformances(exType,
3193+
checkExistentialProtocolConformances(exType.getASTType(),
31943194
IEI->getFormalConcreteType(),
31953195
IEI->getConformances());
31963196
verifyOpenedArchetype(IEI, IEI->getFormalConcreteType());
@@ -3247,21 +3247,25 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
32473247
"init_existential_metatype result must match representation of "
32483248
"operand");
32493249

3250-
while (resultType.is<ExistentialMetatypeType>()) {
3251-
resultType = resultType.getMetatypeInstanceType(F.getModule());
3252-
operandType = operandType.getMetatypeInstanceType(F.getModule());
3250+
auto resultInstanceType = resultType.getASTType();
3251+
auto operandInstanceType = operandType.getASTType();
3252+
while (isa<ExistentialMetatypeType>(resultInstanceType)) {
3253+
resultInstanceType =
3254+
cast<ExistentialMetatypeType>(resultInstanceType).getInstanceType();
3255+
operandInstanceType =
3256+
cast<MetatypeType>(operandInstanceType).getInstanceType();
32533257
}
32543258

3255-
checkExistentialProtocolConformances(resultType,
3256-
operandType.getASTType(),
3259+
checkExistentialProtocolConformances(resultInstanceType,
3260+
operandInstanceType,
32573261
I->getConformances());
32583262
verifyOpenedArchetype(I, MetaTy.getInstanceType());
32593263
}
32603264

3261-
void checkExistentialProtocolConformances(SILType resultType,
3265+
void checkExistentialProtocolConformances(CanType resultType,
32623266
CanType concreteType,
32633267
ArrayRef<ProtocolConformanceRef> conformances) {
3264-
auto layout = resultType.getASTType().getExistentialLayout();
3268+
auto layout = resultType.getExistentialLayout();
32653269
auto protocols = layout.getProtocols();
32663270

32673271
require(conformances.size() == protocols.size(),

lib/SILGen/SILGenBuiltin.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -963,8 +963,11 @@ static ManagedValue emitBuiltinAllocWithTailElems(SILGenFunction &SGF,
963963
}
964964
ManagedValue Metatype = args[0];
965965
if (isa<MetatypeInst>(Metatype)) {
966-
assert(Metatype.getType().getMetatypeInstanceType(SGF.SGM.M) == RefType &&
966+
auto InstanceType =
967+
Metatype.getType().castTo<MetatypeType>().getInstanceType();
968+
assert(InstanceType == RefType.getASTType() &&
967969
"substituted type does not match operand metatype");
970+
(void) InstanceType;
968971
return SGF.B.createAllocRef(loc, RefType, false, false,
969972
ElemTypes, Counts);
970973
} else {

lib/SILOptimizer/SILCombiner/SILCombinerBuiltinVisitors.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -306,12 +306,13 @@ static SILValue createIndexAddrFrom(IndexRawPointerInst *I,
306306
SILType RawPointerTy,
307307
SILBuilder &Builder) {
308308
Builder.setCurrentDebugScope(I->getDebugScope());
309-
SILType InstanceType =
310-
Metatype->getType().getMetatypeInstanceType(I->getModule());
309+
310+
CanType InstanceType =
311+
Metatype->getType().castTo<MetatypeType>().getInstanceType();
311312

312313
// index_raw_pointer's address type is currently always strict.
313314
auto *NewPTAI = Builder.createPointerToAddress(
314-
I->getLoc(), Ptr, InstanceType.getAddressType(),
315+
I->getLoc(), Ptr, SILType::getPrimitiveAddressType(InstanceType),
315316
/*isStrict*/ true, /*isInvariant*/ false);
316317

317318
auto *DistanceAsWord =

lib/SILOptimizer/SILCombiner/SILCombinerCastVisitors.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ SILInstruction *SILCombiner::visitUpcastInst(UpcastInst *UCI) {
7171
SILInstruction *
7272
SILCombiner::
7373
visitPointerToAddressInst(PointerToAddressInst *PTAI) {
74+
auto *F = PTAI->getFunction();
75+
7476
Builder.setCurrentDebugScope(PTAI->getDebugScope());
7577

7678
// If we reach this point, we know that the types must be different since
@@ -127,8 +129,11 @@ visitPointerToAddressInst(PointerToAddressInst *PTAI) {
127129
m_ApplyInst(BuiltinValueKind::Strideof,
128130
m_MetatypeInst(Metatype))),
129131
m_SILValue(Distance)))) {
132+
130133
SILType InstanceType =
131-
Metatype->getType().getMetatypeInstanceType(PTAI->getModule());
134+
F->getLoweredType(Metatype->getType()
135+
.castTo<MetatypeType>().getInstanceType());
136+
132137
auto *Trunc = cast<BuiltinInst>(TruncOrBitCast);
133138

134139
// Make sure that the type of the metatype matches the type that we are
@@ -168,8 +173,10 @@ visitPointerToAddressInst(PointerToAddressInst *PTAI) {
168173
m_ApplyInst(BuiltinValueKind::Strideof,
169174
m_MetatypeInst(Metatype)),
170175
m_ValueBase()))) {
176+
171177
SILType InstanceType =
172-
Metatype->getType().getMetatypeInstanceType(PTAI->getModule());
178+
F->getLoweredType(Metatype->getType()
179+
.castTo<MetatypeType>().getInstanceType());
173180

174181
// Make sure that the type of the metatype matches the type that we are
175182
// casting to so we stride by the correct amount.

lib/SILOptimizer/SILCombiner/SILCombinerMiscVisitors.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1616,8 +1616,11 @@ visitAllocRefDynamicInst(AllocRefDynamicInst *ARDI) {
16161616

16171617
SingleValueInstruction *NewInst = nullptr;
16181618
if (auto *MI = dyn_cast<MetatypeInst>(MDVal)) {
1619-
auto &Mod = ARDI->getModule();
1620-
auto SILInstanceTy = MI->getType().getMetatypeInstanceType(Mod);
1619+
auto MetaTy = MI->getType().castTo<MetatypeType>();
1620+
auto InstanceTy = MetaTy.getInstanceType();
1621+
if (auto SelfTy = dyn_cast<DynamicSelfType>(InstanceTy))
1622+
InstanceTy = SelfTy.getSelfType();
1623+
auto SILInstanceTy = SILType::getPrimitiveObjectType(InstanceTy);
16211624
if (!SILInstanceTy.getClassOrBoundGenericClass())
16221625
return nullptr;
16231626

@@ -1639,8 +1642,11 @@ visitAllocRefDynamicInst(AllocRefDynamicInst *ARDI) {
16391642
return nullptr;
16401643
auto *CCBI = dyn_cast<CheckedCastBranchInst>(PredBB->getTerminator());
16411644
if (CCBI && CCBI->isExact() && ARDI->getParent() == CCBI->getSuccessBB()) {
1642-
auto &Mod = ARDI->getModule();
1643-
auto SILInstanceTy = CCBI->getCastType().getMetatypeInstanceType(Mod);
1645+
auto MetaTy = CCBI->getCastType().castTo<MetatypeType>();
1646+
auto InstanceTy = MetaTy.getInstanceType();
1647+
if (auto SelfTy = dyn_cast<DynamicSelfType>(InstanceTy))
1648+
InstanceTy = SelfTy.getSelfType();
1649+
auto SILInstanceTy = SILType::getPrimitiveObjectType(InstanceTy);
16441650
if (!SILInstanceTy.getClassOrBoundGenericClass())
16451651
return nullptr;
16461652
NewInst = Builder.createAllocRef(ARDI->getLoc(), SILInstanceTy,

0 commit comments

Comments
 (0)