[llvm] 22bd65f - [FPEnv][InstSimplify] Fold fsub X, +0 ==> X
Kevin P. Neal via llvm-commits
llvm-commits at lists.llvm.org
Mon Feb 14 08:56:57 PST 2022
Author: Kevin P. Neal
Date: 2022-02-14T11:56:45-05:00
New Revision: 22bd65fbe7ecf06b9c0a6df212985943de04cd32
URL: https://github.com/llvm/llvm-project/commit/22bd65fbe7ecf06b9c0a6df212985943de04cd32
DIFF: https://github.com/llvm/llvm-project/commit/22bd65fbe7ecf06b9c0a6df212985943de04cd32.diff
LOG: [FPEnv][InstSimplify] Fold fsub X, +0 ==> X
Currently the fsub optimizations in InstSimplify don't know how to fold X
- +0.0 to X when using the constrained intrinsics. This adds the support.
This review is split out from D107285.
Differential Revision: https://reviews.llvm.org/D118928
Added:
Modified:
llvm/lib/Analysis/InstructionSimplify.cpp
llvm/test/Transforms/InstSimplify/strictfp-fsub.ll
Removed:
################################################################################
diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index 8679cd6881bf7..5d7efac9a2c7b 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -5136,13 +5136,16 @@ SimplifyFSubInst(Value *Op0, Value *Op1, FastMathFlags FMF,
if (Constant *C = simplifyFPOp({Op0, Op1}, FMF, Q, ExBehavior, Rounding))
return C;
+ // fsub X, +0 ==> X
+ if (canIgnoreSNaN(ExBehavior, FMF) &&
+ (!canRoundingModeBe(Rounding, RoundingMode::TowardNegative) ||
+ FMF.noSignedZeros()))
+ if (match(Op1, m_PosZeroFP()))
+ return Op0;
+
if (!isDefaultFPEnvironment(ExBehavior, Rounding))
return nullptr;
- // fsub X, +0 ==> X
- if (match(Op1, m_PosZeroFP()))
- return Op0;
-
// fsub X, -0 ==> X, when we know X is not -0
if (match(Op1, m_NegZeroFP()) &&
(FMF.noSignedZeros() || CannotBeNegativeZero(Op0, Q.TLI)))
diff --git a/llvm/test/Transforms/InstSimplify/strictfp-fsub.ll b/llvm/test/Transforms/InstSimplify/strictfp-fsub.ll
index 35ee46e7b8ff4..f72304166d56b 100644
--- a/llvm/test/Transforms/InstSimplify/strictfp-fsub.ll
+++ b/llvm/test/Transforms/InstSimplify/strictfp-fsub.ll
@@ -27,11 +27,9 @@ define float @fsub_x_p0_ebmaytrap(float %a) #0 {
ret float %ret
}
-; TODO: This will fold if we allow non-default floating point environments.
define float @fsub_nnan_x_p0_ebmaytrap(float %a) #0 {
; CHECK-LABEL: @fsub_nnan_x_p0_ebmaytrap(
-; CHECK-NEXT: [[RET:%.*]] = call nnan float @llvm.experimental.constrained.fsub.f32(float [[A:%.*]], float 0.000000e+00, metadata !"round.tonearest", metadata !"fpexcept.maytrap") #[[ATTR0]]
-; CHECK-NEXT: ret float [[RET]]
+; CHECK-NEXT: ret float [[A:%.*]]
;
%ret = call nnan float @llvm.experimental.constrained.fsub.f32(float %a, float 0.0, metadata !"round.tonearest", metadata !"fpexcept.maytrap") #0
ret float %ret
@@ -47,12 +45,11 @@ define float @fsub_x_p0_ebstrict(float %a) #0 {
ret float %ret
}
-; TODO: This will fold if we allow non-default floating point environments.
; TODO: The instruction is expected to remain, but the result isn't used.
define float @fsub_nnan_x_p0_ebstrict(float %a) #0 {
; CHECK-LABEL: @fsub_nnan_x_p0_ebstrict(
; CHECK-NEXT: [[RET:%.*]] = call nnan float @llvm.experimental.constrained.fsub.f32(float [[A:%.*]], float 0.000000e+00, metadata !"round.tonearest", metadata !"fpexcept.strict") #[[ATTR0]]
-; CHECK-NEXT: ret float [[RET]]
+; CHECK-NEXT: ret float [[A]]
;
%ret = call nnan float @llvm.experimental.constrained.fsub.f32(float %a, float 0.0, metadata !"round.tonearest", metadata !"fpexcept.strict") #0
ret float %ret
@@ -68,6 +65,45 @@ define float @fsub_ninf_x_p0_ebstrict(float %a) #0 {
ret float %ret
}
+; Round to -inf and if x is zero then the result is -0.0: must not fire
+define float @fsub_x_p0_neginf(float %a) #0 {
+; CHECK-LABEL: @fsub_x_p0_neginf(
+; CHECK-NEXT: [[RET:%.*]] = call float @llvm.experimental.constrained.fsub.f32(float [[A:%.*]], float 0.000000e+00, metadata !"round.downward", metadata !"fpexcept.ignore") #[[ATTR0]]
+; CHECK-NEXT: ret float [[RET]]
+;
+ %ret = call float @llvm.experimental.constrained.fsub.f32(float %a, float 0.0, metadata !"round.downward", metadata !"fpexcept.ignore") #0
+ ret float %ret
+}
+
+; Dynamic rounding means the rounding mode might be to -inf:
+; Round to -inf and if x is zero then the result is -0.0: must not fire
+define float @fsub_x_p0_dynamic(float %a) #0 {
+; CHECK-LABEL: @fsub_x_p0_dynamic(
+; CHECK-NEXT: [[RET:%.*]] = call float @llvm.experimental.constrained.fsub.f32(float [[A:%.*]], float 0.000000e+00, metadata !"round.dynamic", metadata !"fpexcept.ignore") #[[ATTR0]]
+; CHECK-NEXT: ret float [[RET]]
+;
+ %ret = call float @llvm.experimental.constrained.fsub.f32(float %a, float 0.0, metadata !"round.dynamic", metadata !"fpexcept.ignore") #0
+ ret float %ret
+}
+
+; With nsz we don't have to worry about -0.0 so the transform is valid.
+define float @fsub_nsz_x_p0_neginf(float %a) #0 {
+; CHECK-LABEL: @fsub_nsz_x_p0_neginf(
+; CHECK-NEXT: ret float [[A:%.*]]
+;
+ %ret = call nsz float @llvm.experimental.constrained.fsub.f32(float %a, float 0.0, metadata !"round.downward", metadata !"fpexcept.ignore") #0
+ ret float %ret
+}
+
+; With nsz we don't have to worry about -0.0 so the transform is valid.
+define float @fsub_nsz_x_p0_dynamic(float %a) #0 {
+; CHECK-LABEL: @fsub_nsz_x_p0_dynamic(
+; CHECK-NEXT: ret float [[A:%.*]]
+;
+ %ret = call nsz float @llvm.experimental.constrained.fsub.f32(float %a, float 0.0, metadata !"round.dynamic", metadata !"fpexcept.ignore") #0
+ ret float %ret
+}
+
;
; fsub X, -0 ==> X, when we know X is not -0
; (fast math flag: nsz)
More information about the llvm-commits
mailing list