[llvm] [InstSimplify] Handle nsz when simplifying X * 0.0 (PR #142181)
via llvm-commits
llvm-commits at lists.llvm.org
Fri May 30 09:15:40 PDT 2025
https://github.com/Nadharm created https://github.com/llvm/llvm-project/pull/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
>From 6a1a5ba46bd6475bff4bb5e5844e5e2b600ad02e Mon Sep 17 00:00:00 2001
From: Nadharm Dhiantravan <ndhiantravan at nvidia.com>
Date: Fri, 30 May 2025 08:26:50 -0700
Subject: [PATCH] [InstSimplify] Handle nsz when simplifying X * 0.0
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
---
llvm/lib/Analysis/InstructionSimplify.cpp | 3 +++
.../InstSimplify/floating-point-arithmetic.ll | 16 ++++++++++++++++
2 files changed, 19 insertions(+)
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