[llvm] [ConstantFolding] Add constant folding support for nextafter/nexttoward (PR #167324)
Sayan Sivakumaran via llvm-commits
llvm-commits at lists.llvm.org
Mon Nov 10 07:01:39 PST 2025
================
@@ -3221,6 +3223,26 @@ static Constant *ConstantFoldLibCall2(StringRef Name, Type *Ty,
if (TLI->has(Func))
return ConstantFoldBinaryFP(atan2, Op1V, Op2V, Ty);
break;
+ case LibFunc_nextafter:
+ case LibFunc_nextafterf:
+ case LibFunc_nexttoward:
+ case LibFunc_nexttowardf:
+ if (TLI->has(Func)) {
+ if (Op1V.isNaN() || Op2V.isNaN()) {
+ return ConstantFP::get(Ty->getContext(),
+ APFloat::getNaN(Ty->getFltSemantics()));
+ }
+
+ APFloat PromotedOp1V = Op1V.getPromoted(APFloat::IEEEquad());
+ APFloat PromotedOp2V = Op2V.getPromoted(APFloat::IEEEquad());
+ if (PromotedOp1V == PromotedOp2V) {
+ return ConstantFP::get(Ty->getContext(), Op1V);
+ }
+
+ APFloat Next(Op1V);
+ Next.next(/*nextDown=*/PromotedOp1V > PromotedOp2V);
+ return ConstantFP::get(Ty->getContext(), Next);
----------------
sivakusayan wrote:
We need to promote here before doing any of these checks, as otherwise the operands have different types in the case of `nexttoward`. I believe that converting to `IEEEQuad` is strictly widening no matter what the argument types are, and the assertions seem to agree with that.
https://github.com/llvm/llvm-project/pull/167324
More information about the llvm-commits
mailing list