aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Analysis/ConstantFolding.cpp
diff options
context:
space:
mode:
authorNikita Popov <nikita.ppv@gmail.com>2021-01-10 17:07:14 +0100
committerNikita Popov <nikita.ppv@gmail.com>2021-01-10 17:37:27 +0100
commit1ecae1e62ad016f0c12c204ce312fdfd653ca8cf (patch)
tree88e3f21d061fc201cf272a753468e65f68e54b19 /llvm/lib/Analysis/ConstantFolding.cpp
parentbdb748a0ab24b9d87f98d3cdbecdbbf504aed930 (diff)
downloadllvm-1ecae1e62ad016f0c12c204ce312fdfd653ca8cf.zip
llvm-1ecae1e62ad016f0c12c204ce312fdfd653ca8cf.tar.gz
llvm-1ecae1e62ad016f0c12c204ce312fdfd653ca8cf.tar.bz2
[ConstantFold] Fold fptoi.sat intrinsics
The APFloat::convertToInteger() API already implements the desired saturation semantics.
Diffstat (limited to 'llvm/lib/Analysis/ConstantFolding.cpp')
-rw-r--r--llvm/lib/Analysis/ConstantFolding.cpp17
1 files changed, 16 insertions, 1 deletions
diff --git a/llvm/lib/Analysis/ConstantFolding.cpp b/llvm/lib/Analysis/ConstantFolding.cpp
index 22b9acb..f73890d 100644
--- a/llvm/lib/Analysis/ConstantFolding.cpp
+++ b/llvm/lib/Analysis/ConstantFolding.cpp
@@ -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:
@@ -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 ||
@@ -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;