[PATCH] D111450: [FPEnv][InstSimplify] Fold fadd X, 0 ==> X, when we know X is not -0
Kevin P. Neal via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Fri Oct 8 11:38:27 PDT 2021
kpn created this revision.
kpn added reviewers: sepavloff, spatel, lebedev.ri, nlopes.
kpn requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.
Currently the fadd optimizations in InstSimplify don't know how to do this NoSignedZeros "X + 0.0 ==> X" fold when using the constrained intrinsics. This adds the support.
This review is derived from D106362 <https://reviews.llvm.org/D106362> with some improvements from D107285 <https://reviews.llvm.org/D107285> and is a follow-on to D111085 <https://reviews.llvm.org/D111085>.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D111450
Files:
llvm/lib/Analysis/InstructionSimplify.cpp
llvm/test/Transforms/InstSimplify/strictfp-fadd.ll
Index: llvm/test/Transforms/InstSimplify/strictfp-fadd.ll
===================================================================
--- llvm/test/Transforms/InstSimplify/strictfp-fadd.ll
+++ llvm/test/Transforms/InstSimplify/strictfp-fadd.ll
@@ -247,8 +247,7 @@
define float @fold_fadd_nnan_nsz_x_0_ebmaytrap(float %a) #0 {
; CHECK-LABEL: @fold_fadd_nnan_nsz_x_0_ebmaytrap(
-; CHECK-NEXT: [[ADD:%.*]] = call nnan nsz float @llvm.experimental.constrained.fadd.f32(float [[A:%.*]], float 0.000000e+00, metadata !"round.tonearest", metadata !"fpexcept.maytrap") #[[ATTR0]]
-; CHECK-NEXT: ret float [[ADD]]
+; CHECK-NEXT: ret float [[A:%.*]]
;
%add = call nnan nsz float @llvm.experimental.constrained.fadd.f32(float %a, float 0.0, metadata !"round.tonearest", metadata !"fpexcept.maytrap") #0
ret float %add
@@ -256,8 +255,7 @@
define <2 x float> @fold_fadd_vec_nnan_nsz_x_0_ebmaytrap(<2 x float> %a) #0 {
; CHECK-LABEL: @fold_fadd_vec_nnan_nsz_x_0_ebmaytrap(
-; CHECK-NEXT: [[ADD:%.*]] = call nnan nsz <2 x float> @llvm.experimental.constrained.fadd.v2f32(<2 x float> [[A:%.*]], <2 x float> zeroinitializer, metadata !"round.tonearest", metadata !"fpexcept.maytrap") #[[ATTR0]]
-; CHECK-NEXT: ret <2 x float> [[ADD]]
+; CHECK-NEXT: ret <2 x float> [[A:%.*]]
;
%add = call nnan nsz <2 x float> @llvm.experimental.constrained.fadd.v2f32(<2 x float> %a, <2 x float> zeroinitializer, metadata !"round.tonearest", metadata !"fpexcept.maytrap") #0
ret <2 x float> %add
@@ -284,7 +282,7 @@
define float @fold_fadd_nsz_nnan_x_0_ebstrict(float %a) #0 {
; CHECK-LABEL: @fold_fadd_nsz_nnan_x_0_ebstrict(
; CHECK-NEXT: [[ADD:%.*]] = call nnan nsz float @llvm.experimental.constrained.fadd.f32(float [[A:%.*]], float 0.000000e+00, metadata !"round.tonearest", metadata !"fpexcept.strict") #[[ATTR0]]
-; CHECK-NEXT: ret float [[ADD]]
+; CHECK-NEXT: ret float [[A]]
;
%add = call nsz nnan float @llvm.experimental.constrained.fadd.f32(float %a, float 0.0, metadata !"round.tonearest", metadata !"fpexcept.strict") #0
ret float %add
@@ -293,7 +291,7 @@
define <2 x float> @fold_fadd_vec_nsz_nnan_x_0_ebstrict(<2 x float> %a) #0 {
; CHECK-LABEL: @fold_fadd_vec_nsz_nnan_x_0_ebstrict(
; CHECK-NEXT: [[ADD:%.*]] = call nnan nsz <2 x float> @llvm.experimental.constrained.fadd.v2f32(<2 x float> [[A:%.*]], <2 x float> zeroinitializer, metadata !"round.tonearest", metadata !"fpexcept.strict") #[[ATTR0]]
-; CHECK-NEXT: ret <2 x float> [[ADD]]
+; CHECK-NEXT: ret <2 x float> [[A]]
;
%add = call nsz nnan <2 x float> @llvm.experimental.constrained.fadd.v2f32(<2 x float> %a, <2 x float> zeroinitializer, metadata !"round.tonearest", metadata !"fpexcept.strict") #0
ret <2 x float> %add
Index: llvm/lib/Analysis/InstructionSimplify.cpp
===================================================================
--- llvm/lib/Analysis/InstructionSimplify.cpp
+++ llvm/lib/Analysis/InstructionSimplify.cpp
@@ -4978,14 +4978,15 @@
if (match(Op1, m_NegZeroFP()))
return Op0;
+ // fadd X, 0 ==> X, when we know X is not -0
+ if (canIgnoreSNaN(ExBehavior, FMF))
+ if (match(Op1, m_PosZeroFP()) &&
+ (FMF.noSignedZeros() || CannotBeNegativeZero(Op0, Q.TLI)))
+ return Op0;
+
if (!isDefaultFPEnvironment(ExBehavior, Rounding))
return nullptr;
- // fadd X, 0 ==> X, when we know X is not -0
- if (match(Op1, m_PosZeroFP()) &&
- (FMF.noSignedZeros() || CannotBeNegativeZero(Op0, Q.TLI)))
- return Op0;
-
// With nnan: -X + X --> 0.0 (and commuted variant)
// We don't have to explicitly exclude infinities (ninf): INF + -INF == NaN.
// Negative zeros are allowed because we always end up with positive zero:
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D111450.378317.patch
Type: text/x-patch
Size: 3697 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211008/3cc4b06d/attachment.bin>
More information about the llvm-commits
mailing list