@@ -1577,40 +1577,45 @@ SILInstruction *CastOptimizer::optimizeUnconditionalCheckedCastAddrInst(
1577
1577
}
1578
1578
1579
1579
// / Simplify conversions between thick and objc metatypes.
1580
- SILInstruction * CastOptimizer::optimizeMetatypeConversion (
1581
- ConversionInst *MCI , MetatypeRepresentation Representation ) {
1582
- SILValue Op = MCI ->getOperand (0 );
1580
+ SILValue CastOptimizer::optimizeMetatypeConversion (
1581
+ ConversionInst *mci , MetatypeRepresentation representation ) {
1582
+ SILValue op = mci ->getOperand (0 );
1583
1583
// Instruction has a proper target type already.
1584
- SILType Ty = MCI ->getType ();
1585
- auto MetatypeTy = Op ->getType ().getAs <AnyMetatypeType>();
1584
+ SILType ty = mci ->getType ();
1585
+ auto metatypeTy = op ->getType ().getAs <AnyMetatypeType>();
1586
1586
1587
- if (MetatypeTy->getRepresentation () != Representation)
1588
- return nullptr ;
1587
+ if (metatypeTy->getRepresentation () != representation)
1588
+ return SILValue ();
1589
+
1590
+ auto loc = mci->getLoc ();
1589
1591
1590
1592
// Rematerialize the incoming metatype instruction with the outgoing type.
1591
- auto replaceCast = [&](SingleValueInstruction *NewCast) {
1592
- assert (Ty .getAs <AnyMetatypeType>()->getRepresentation ()
1593
- == NewCast ->getType ().getAs <AnyMetatypeType>()->getRepresentation ());
1594
- MCI-> replaceAllUsesWith (NewCast );
1595
- EraseInstAction (MCI );
1596
- return NewCast ;
1593
+ auto replaceCast = [&](SILValue newValue) -> SILValue {
1594
+ assert (ty .getAs <AnyMetatypeType>()->getRepresentation () ==
1595
+ newValue ->getType ().getAs <AnyMetatypeType>()->getRepresentation ());
1596
+ ReplaceValueUsesAction (mci, newValue );
1597
+ EraseInstAction (mci );
1598
+ return newValue ;
1597
1599
};
1598
- if (auto *MI = dyn_cast<MetatypeInst>(Op)) {
1599
- return replaceCast (SILBuilderWithScope (MCI, BuilderContext)
1600
- .createMetatype (MCI->getLoc (), Ty));
1600
+
1601
+ if (auto *mi = dyn_cast<MetatypeInst>(op)) {
1602
+ return replaceCast (
1603
+ SILBuilderWithScope (mci, BuilderContext).createMetatype (loc, ty));
1601
1604
}
1605
+
1602
1606
// For metatype instructions that require an operand, generate the new
1603
1607
// metatype at the same position as the original to avoid extending the
1604
- // lifetime of `Op` past its destroy.
1605
- if (auto *VMI = dyn_cast<ValueMetatypeInst>(Op)) {
1606
- return replaceCast (
1607
- SILBuilderWithScope (VMI, BuilderContext)
1608
- .createValueMetatype (MCI->getLoc (), Ty, VMI->getOperand ()));
1608
+ // lifetime of `op` past its destroy.
1609
+ if (auto *vmi = dyn_cast<ValueMetatypeInst>(op)) {
1610
+ return replaceCast (SILBuilderWithScope (vmi, BuilderContext)
1611
+ .createValueMetatype (loc, ty, vmi->getOperand ()));
1609
1612
}
1610
- if (auto *EMI = dyn_cast<ExistentialMetatypeInst>(Op)) {
1613
+
1614
+ if (auto *emi = dyn_cast<ExistentialMetatypeInst>(op)) {
1611
1615
return replaceCast (
1612
- SILBuilderWithScope (EMI , BuilderContext)
1613
- .createExistentialMetatype (MCI-> getLoc (), Ty, EMI ->getOperand ()));
1616
+ SILBuilderWithScope (emi , BuilderContext)
1617
+ .createExistentialMetatype (loc, ty, emi ->getOperand ()));
1614
1618
}
1615
- return nullptr ;
1619
+
1620
+ return SILValue ();
1616
1621
}
0 commit comments