[llvm] [ConstantFolding] Constant fold nextafter and nexttoward (PR #168794)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 24 07:53:31 PST 2025


================
@@ -0,0 +1,279 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: cat %S/floating-point-constants.ll %s | opt -passes=instcombine -S | FileCheck %s
+
+declare double @nexttoward(double, fp128) #0
+declare float @nexttowardf(float, fp128) #0
+
+attributes #0 = { willreturn memory(errnomem: write) }
+
+define double @nexttoward_up_direction() {
+; CHECK-LABEL: define double @nexttoward_up_direction() {
+; CHECK-NEXT:    ret double 0x3FF0000000000001
+;
+  %arg = fpext double 2.0 to fp128
+  %next = call double @nexttoward(double 1.0, fp128 %arg)
+  ret double %next
+}
+
+define float @nexttowardf_up_direction() {
+; CHECK-LABEL: define float @nexttowardf_up_direction() {
+; CHECK-NEXT:    ret float 0x3FF0000020000000
+;
+  %arg = fpext float 2.0 to fp128
+  %next = call float @nexttowardf(float 1.0, fp128 %arg)
+  ret float %next
+}
+
+define double @nexttoward_down_direction() {
+; CHECK-LABEL: define double @nexttoward_down_direction() {
+; CHECK-NEXT:    ret double 0x3FEFFFFFFFFFFFFF
+;
+  %arg = fpext double 0.0 to fp128
+  %next = call double @nexttoward(double 1.0, fp128 %arg)
+  ret double %next
+}
+
+define float @nexttowardf_down_direction() {
+; CHECK-LABEL: define float @nexttowardf_down_direction() {
+; CHECK-NEXT:    ret float 0x3FEFFFFFE0000000
+;
+  %arg = fpext float 0.0 to fp128
+  %next = call float @nexttowardf(float 1.0, fp128 %arg)
+  ret float %next
+}
+
+define double @nexttoward_equal_args() {
+; CHECK-LABEL: define double @nexttoward_equal_args() {
+; CHECK-NEXT:    ret double 1.000000e+00
+;
+  %arg = fpext double 1.0 to fp128
+  %next = call double @nexttoward(double 1.0, fp128 %arg)
+  ret double %next
+}
+
+define float @nexttowardf_equal_args() {
+; CHECK-LABEL: define float @nexttowardf_equal_args() {
+; CHECK-NEXT:    ret float 1.000000e+00
+;
+  %arg = fpext float 1.0 to fp128
+  %next = call float @nexttowardf(float 1.0, fp128 %arg)
+  ret float %next
+}
+
+define double @nexttoward_pos_zero_neg_zero() {
+; CHECK-LABEL: define double @nexttoward_pos_zero_neg_zero() {
+; CHECK-NEXT:    ret double -0.000000e+00
+;
+  %neg_zero_ext = fpext double -0.0 to fp128
+  %next = call double @nexttoward(double 0.0, fp128 %neg_zero_ext)
+  ret double %next
+}
+
+define float @nexttowardf_pos_zero_neg_zero() {
+; CHECK-LABEL: define float @nexttowardf_pos_zero_neg_zero() {
+; CHECK-NEXT:    ret float -0.000000e+00
+;
+  %neg_zero_ext = fpext float -0.0 to fp128
+  %next = call float @nexttowardf(float 0.0, fp128 %neg_zero_ext)
+  ret float %next
+}
+
+define double @nexttoward_neg_zero_pos_zero() {
+; CHECK-LABEL: define double @nexttoward_neg_zero_pos_zero() {
+; CHECK-NEXT:    ret double 0.000000e+00
+;
+  %pos_zero_ext = fpext double 0.0 to fp128
+  %next = call double @nexttoward(double -0.0, fp128 %pos_zero_ext)
+  ret double %next
+}
+
+define float @nexttowardf_neg_zero_pos_zero() {
+; CHECK-LABEL: define float @nexttowardf_neg_zero_pos_zero() {
+; CHECK-NEXT:    ret float 0.000000e+00
+;
+  %pos_zero_ext = fpext float 0.0 to fp128
+  %next = call float @nexttowardf(float -0.0, fp128 %pos_zero_ext)
+  ret float %next
+}
+
+define double @nexttoward_nan_with_payload() {
+; CHECK-LABEL: define double @nexttoward_nan_with_payload() {
+; CHECK-NEXT:    ret double 0x7FF8000000000001
+;
+  %nan = load double, double* @dbl_nan
----------------
dtcxzyw wrote:

```suggestion
  %nan = load double, ptr @dbl_nan
```
Use opaque pointer type.


https://github.com/llvm/llvm-project/pull/168794


More information about the llvm-commits mailing list