Skip to content

Commit

Permalink
[ConstantFold] Fold fptoi.sat intrinsics
Browse files Browse the repository at this point in the history
The APFloat::convertToInteger() API already implements the desired
saturation semantics.
  • Loading branch information
nikic committed Jan 10, 2021
1 parent bdb748a commit 1ecae1e
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 145 deletions.
17 changes: 16 additions & 1 deletion llvm/lib/Analysis/ConstantFolding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1515,6 +1515,8 @@ bool llvm::canConstantFoldCallTo(const CallBase *Call, const Function *F) {
case Intrinsic::powi:
case Intrinsic::fma:
case Intrinsic::fmuladd:
case Intrinsic::fptoui_sat:
case Intrinsic::fptosi_sat:
case Intrinsic::convert_from_fp16:
case Intrinsic::convert_to_fp16:
case Intrinsic::amdgcn_cos:
Expand Down Expand Up @@ -1850,8 +1852,11 @@ static Constant *ConstantFoldScalarCall1(StringRef Name,
if (isa<UndefValue>(Operands[0])) {
// cosine(arg) is between -1 and 1. cosine(invalid arg) is NaN.
// ctpop() is between 0 and bitwidth, pick 0 for undef.
// fptoui.sat and fptosi.sat can always fold to zero (for a zero input).
if (IntrinsicID == Intrinsic::cos ||
IntrinsicID == Intrinsic::ctpop)
IntrinsicID == Intrinsic::ctpop ||
IntrinsicID == Intrinsic::fptoui_sat ||
IntrinsicID == Intrinsic::fptosi_sat)
return Constant::getNullValue(Ty);
if (IntrinsicID == Intrinsic::bswap ||
IntrinsicID == Intrinsic::bitreverse ||
Expand Down Expand Up @@ -1923,6 +1928,16 @@ static Constant *ConstantFoldScalarCall1(StringRef Name,
: ConstantInt::get(Ty, APInt::getMaxValue(Width));
}

if (IntrinsicID == Intrinsic::fptoui_sat ||
IntrinsicID == Intrinsic::fptosi_sat) {
// convertToInteger() already has the desired saturation semantics.
APSInt Int(Ty->getIntegerBitWidth(),
IntrinsicID == Intrinsic::fptoui_sat);
bool IsExact;
U.convertToInteger(Int, APFloat::rmTowardZero, &IsExact);
return ConstantInt::get(Ty, Int);
}

if (!Ty->isHalfTy() && !Ty->isFloatTy() && !Ty->isDoubleTy())
return nullptr;

Expand Down
Loading

0 comments on commit 1ecae1e

Please sign in to comment.