[llvm] f71e4e9 - [InstSimplify] Handle nsz when simplifying X * 0.0 (#142181)

via llvm-commits llvm-commits at lists.llvm.org
Fri May 30 22:50:25 PDT 2025


Author: Nadharm
Date: 2025-05-31T13:50:22+08:00
New Revision: f71e4e9bc2b6cd1444ab2a395a1ce20d486362de

URL: https://github.com/llvm/llvm-project/commit/f71e4e9bc2b6cd1444ab2a395a1ce20d486362de
DIFF: https://github.com/llvm/llvm-project/commit/f71e4e9bc2b6cd1444ab2a395a1ce20d486362de.diff

LOG: [InstSimplify] Handle nsz when simplifying X * 0.0 (#142181)

If ValueTracking can guarantee non-NaN and non-INF and the `nsz`
fast-math flag is set, we can simplify X * 0.0 ==> 0.0.

https://alive2.llvm.org/ce/z/XacRQZ

Added: 
    

Modified: 
    llvm/lib/Analysis/InstructionSimplify.cpp
    llvm/test/Transforms/InstSimplify/floating-point-arithmetic.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index 23e147ba8c6a1..12c84c718d4fd 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -5844,6 +5844,9 @@ static Value *simplifyFMAFMul(Value *Op0, Value *Op1, FastMathFlags FMF,
     KnownFPClass Known =
         computeKnownFPClass(Op0, FMF, fcInf | fcNan, /*Depth=*/0, Q);
     if (Known.isKnownNever(fcInf | fcNan)) {
+      // if nsz is set, return 0.0
+      if (FMF.noSignedZeros())
+        return ConstantFP::getZero(Op0->getType());
       // +normal number * (-)0.0 --> (-)0.0
       if (Known.SignBit == false)
         return Op1;

diff  --git a/llvm/test/Transforms/InstSimplify/floating-point-arithmetic.ll b/llvm/test/Transforms/InstSimplify/floating-point-arithmetic.ll
index d3178a103d42c..ab4448b460bfc 100644
--- a/llvm/test/Transforms/InstSimplify/floating-point-arithmetic.ll
+++ b/llvm/test/Transforms/InstSimplify/floating-point-arithmetic.ll
@@ -211,6 +211,22 @@ define double @fmul_nnan_ninf_nneg_n0.0_commute(i127 %x) {
   ret double %r
 }
 
+define float @fmul_ninf_nnan_mul_zero_nsz(float nofpclass(inf nan) %f) {
+; CHECK-LABEL: @fmul_ninf_nnan_mul_zero_nsz(
+; CHECK-NEXT:     ret float 0.000000e+00
+;
+  %r = fmul nsz float %f, 0.0
+  ret float %r
+}
+
+define float @fmul_ninf_nnan_mul_nzero_nsz(float nofpclass(inf nan) %f) {
+; CHECK-LABEL: @fmul_ninf_nnan_mul_nzero_nsz(
+; CHECK-NEXT:     ret float 0.000000e+00
+;
+  %r = fmul nsz float %f, -0.0
+  ret float %r
+}
+
 define float @src_mul_nzero_neg(float nofpclass(inf nan pzero psub pnorm) %f) {
 ; CHECK-LABEL: @src_mul_nzero_neg(
 ; CHECK-NEXT:    ret float 0.000000e+00


        


More information about the llvm-commits mailing list