@@ -1643,33 +1643,43 @@ Instruction *InstCombinerImpl::visitSExt(SExtInst &Sext) {
16431643
16441644// / Return a Constant* for the specified floating-point constant if it fits
16451645// / in the specified FP type without changing its value.
1646- static bool fitsInFPType (ConstantFP *CFP , const fltSemantics &Sem) {
1646+ static bool fitsInFPType (APFloat F , const fltSemantics &Sem) {
16471647 bool losesInfo;
1648- APFloat F = CFP->getValueAPF ();
16491648 (void )F.convert (Sem, APFloat::rmNearestTiesToEven, &losesInfo);
16501649 return !losesInfo;
16511650}
16521651
1653- static Type *shrinkFPConstant (ConstantFP *CFP, bool PreferBFloat) {
1654- if (CFP->getType () == Type::getPPC_FP128Ty (CFP->getContext ()))
1655- return nullptr ; // No constant folding of this.
1652+ static Type *shrinkFPConstant (LLVMContext &Ctx, APFloat F, bool PreferBFloat) {
16561653 // See if the value can be truncated to bfloat and then reextended.
1657- if (PreferBFloat && fitsInFPType (CFP , APFloat::BFloat ()))
1658- return Type::getBFloatTy (CFP-> getContext () );
1654+ if (PreferBFloat && fitsInFPType (F , APFloat::BFloat ()))
1655+ return Type::getBFloatTy (Ctx );
16591656 // See if the value can be truncated to half and then reextended.
1660- if (!PreferBFloat && fitsInFPType (CFP , APFloat::IEEEhalf ()))
1661- return Type::getHalfTy (CFP-> getContext () );
1657+ if (!PreferBFloat && fitsInFPType (F , APFloat::IEEEhalf ()))
1658+ return Type::getHalfTy (Ctx );
16621659 // See if the value can be truncated to float and then reextended.
1663- if (fitsInFPType (CFP, APFloat::IEEEsingle ()))
1664- return Type::getFloatTy (CFP->getContext ());
1665- if (CFP->getType ()->isDoubleTy ())
1666- return nullptr ; // Won't shrink.
1667- if (fitsInFPType (CFP, APFloat::IEEEdouble ()))
1668- return Type::getDoubleTy (CFP->getContext ());
1669- // Don't try to shrink to various long double types.
1660+ if (fitsInFPType (F, APFloat::IEEEsingle ()))
1661+ return Type::getFloatTy (Ctx);
1662+ // See if the value can be truncated to double and then reextended.
1663+ if (fitsInFPType (F, APFloat::IEEEdouble ()))
1664+ return Type::getDoubleTy (Ctx);
1665+ // Does not shrink.
16701666 return nullptr ;
16711667}
16721668
1669+ static Type *shrinkFPConstant (ConstantFP *CFP, bool PreferBFloat) {
1670+ Type *Ty = CFP->getType ();
1671+ if (Ty->getScalarType () == Type::getPPC_FP128Ty (CFP->getContext ()))
1672+ return nullptr ; // No constant folding of this.
1673+
1674+ Type *ShrinkTy =
1675+ shrinkFPConstant (CFP->getContext (), CFP->getValueAPF (), PreferBFloat);
1676+ if (auto *VecTy = dyn_cast<VectorType>(Ty))
1677+ ShrinkTy = VectorType::get (ShrinkTy, VecTy);
1678+
1679+ // Does it shrink?
1680+ return ShrinkTy != Ty ? ShrinkTy : nullptr ;
1681+ }
1682+
16731683// Determine if this is a vector of ConstantFPs and if so, return the minimal
16741684// type we can safely truncate all elements to.
16751685static Type *shrinkFPConstantVector (Value *V, bool PreferBFloat) {
@@ -1720,10 +1730,10 @@ static Type *getMinimumFPType(Value *V, bool PreferBFloat) {
17201730
17211731 // Try to shrink scalable and fixed splat vectors.
17221732 if (auto *FPC = dyn_cast<Constant>(V))
1723- if (isa <VectorType>(V->getType ()))
1733+ if (auto *VTy = dyn_cast <VectorType>(V->getType ()))
17241734 if (auto *Splat = dyn_cast_or_null<ConstantFP>(FPC->getSplatValue ()))
17251735 if (Type *T = shrinkFPConstant (Splat, PreferBFloat))
1726- return T ;
1736+ return VectorType::get (T, VTy) ;
17271737
17281738 // Try to shrink a vector of FP constants. This returns nullptr on scalable
17291739 // vectors
@@ -1796,10 +1806,9 @@ Instruction *InstCombinerImpl::visitFPTrunc(FPTruncInst &FPT) {
17961806 Type *Ty = FPT.getType ();
17971807 auto *BO = dyn_cast<BinaryOperator>(FPT.getOperand (0 ));
17981808 if (BO && BO->hasOneUse ()) {
1799- Type *LHSMinType =
1800- getMinimumFPType (BO->getOperand (0 ), /* PreferBFloat=*/ Ty->isBFloatTy ());
1801- Type *RHSMinType =
1802- getMinimumFPType (BO->getOperand (1 ), /* PreferBFloat=*/ Ty->isBFloatTy ());
1809+ bool PreferBFloat = Ty->getScalarType ()->isBFloatTy ();
1810+ Type *LHSMinType = getMinimumFPType (BO->getOperand (0 ), PreferBFloat);
1811+ Type *RHSMinType = getMinimumFPType (BO->getOperand (1 ), PreferBFloat);
18031812 unsigned OpWidth = BO->getType ()->getFPMantissaWidth ();
18041813 unsigned LHSWidth = LHSMinType->getFPMantissaWidth ();
18051814 unsigned RHSWidth = RHSMinType->getFPMantissaWidth ();
0 commit comments