[llvm] [ConstantFolding] Constant fold nextafter and nexttoward (PR #168794)
Sayan Sivakumaran via llvm-commits
llvm-commits at lists.llvm.org
Sun Feb 8 18:26:00 PST 2026
================
@@ -3174,6 +3176,57 @@ static Constant *evaluateCompare(const APFloat &Op1, const APFloat &Op2,
return nullptr;
}
+/// Returns the first NaN in the operand list if it exists, preserving the NaN
+/// payload if possible. Returns nullptr if no NaNs are in the list.
+static Constant *TryConstantFoldNaN(ArrayRef<APFloat> Operands,
+ const Type *RetTy) {
+ assert(RetTy != nullptr);
+ for (const APFloat &Op : Operands) {
+ if (Op.isNaN()) {
+ bool Unused;
+ APFloat Ret(Op);
+ Ret.convert(RetTy->getFltSemantics(), detail::rmNearestTiesToEven,
+ &Unused);
+ return ConstantFP::get(RetTy->getContext(), Ret);
+ }
+ }
+ return nullptr;
+}
+
+static Constant *ConstantFoldNextToward(const APFloat &Op0, const APFloat &Op1,
+ const Type *RetTy) {
+ assert(RetTy != nullptr);
+ if (Constant *RetNaN = TryConstantFoldNaN({Op0, Op1}, RetTy))
----------------
sivakusayan wrote:
Thanks - I removed the special case for checking NaN on the first operand, since it can be propagated as you point out.
I think we still need the special case for checking NaN on the second operand. Let me know if you had something else in mind, I might have misunderstood something. Without checking the second operand the test `@nextafter_nan_with_payload_2` fails locally.
https://github.com/llvm/llvm-project/pull/168794
More information about the llvm-commits
mailing list