@@ -1643,33 +1643,46 @@ 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, const APFloat &F,
1653+ bool PreferBFloat) {
16561654 // 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 () );
1655+ if (PreferBFloat && fitsInFPType (F , APFloat::BFloat ()))
1656+ return Type::getBFloatTy (Ctx );
16591657 // 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 () );
1658+ if (!PreferBFloat && fitsInFPType (F , APFloat::IEEEhalf ()))
1659+ return Type::getHalfTy (Ctx );
16621660 // 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 ());
1661+ if (fitsInFPType (F, APFloat::IEEEsingle ()))
1662+ return Type::getFloatTy (Ctx);
1663+ if (&F.getSemantics () == &APFloat::IEEEdouble ())
1664+ return nullptr ; // Won't shrink.
1665+ // See if the value can be truncated to double and then reextended.
1666+ if (fitsInFPType (F, APFloat::IEEEdouble ()))
1667+ return Type::getDoubleTy (Ctx);
16691668 // Don't try to shrink to various long double types.
16701669 return nullptr ;
16711670}
16721671
1672+ static Type *shrinkFPConstant (ConstantFP *CFP, bool PreferBFloat) {
1673+ Type *Ty = CFP->getType ();
1674+ if (Ty->getScalarType ()->isPPC_FP128Ty ())
1675+ return nullptr ; // No constant folding of this.
1676+
1677+ Type *ShrinkTy =
1678+ shrinkFPConstant (CFP->getContext (), CFP->getValueAPF (), PreferBFloat);
1679+ if (ShrinkTy)
1680+ if (auto *VecTy = dyn_cast<VectorType>(Ty))
1681+ ShrinkTy = VectorType::get (ShrinkTy, VecTy);
1682+
1683+ return ShrinkTy;
1684+ }
1685+
16731686// Determine if this is a vector of ConstantFPs and if so, return the minimal
16741687// type we can safely truncate all elements to.
16751688static Type *shrinkFPConstantVector (Value *V, bool PreferBFloat) {
@@ -1720,10 +1733,10 @@ static Type *getMinimumFPType(Value *V, bool PreferBFloat) {
17201733
17211734 // Try to shrink scalable and fixed splat vectors.
17221735 if (auto *FPC = dyn_cast<Constant>(V))
1723- if (isa <VectorType>(V->getType ()))
1736+ if (auto *VTy = dyn_cast <VectorType>(V->getType ()))
17241737 if (auto *Splat = dyn_cast_or_null<ConstantFP>(FPC->getSplatValue ()))
17251738 if (Type *T = shrinkFPConstant (Splat, PreferBFloat))
1726- return T ;
1739+ return VectorType::get (T, VTy) ;
17271740
17281741 // Try to shrink a vector of FP constants. This returns nullptr on scalable
17291742 // vectors
@@ -1796,10 +1809,9 @@ Instruction *InstCombinerImpl::visitFPTrunc(FPTruncInst &FPT) {
17961809 Type *Ty = FPT.getType ();
17971810 auto *BO = dyn_cast<BinaryOperator>(FPT.getOperand (0 ));
17981811 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 ());
1812+ bool PreferBFloat = Ty->getScalarType ()->isBFloatTy ();
1813+ Type *LHSMinType = getMinimumFPType (BO->getOperand (0 ), PreferBFloat);
1814+ Type *RHSMinType = getMinimumFPType (BO->getOperand (1 ), PreferBFloat);
18031815 unsigned OpWidth = BO->getType ()->getFPMantissaWidth ();
18041816 unsigned LHSWidth = LHSMinType->getFPMantissaWidth ();
18051817 unsigned RHSWidth = RHSMinType->getFPMantissaWidth ();
0 commit comments