[PATCH] D61916: Teach InstSimplify transform -X + X --> 0.0 about unary FNeg
Cameron McInally via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Tue May 14 13:33:39 PDT 2019
cameron.mcinally created this revision.
cameron.mcinally added reviewers: spatel, arsenm, kpn, craig.topper, andrew.w.kaylor.
Herald added subscribers: llvm-commits, hiraditya, wdng.
Herald added a project: LLVM.
Repository:
rL LLVM
https://reviews.llvm.org/D61916
Files:
llvm/lib/Analysis/InstructionSimplify.cpp
llvm/test/Transforms/InstSimplify/fast-math.ll
Index: llvm/test/Transforms/InstSimplify/fast-math.ll
===================================================================
--- llvm/test/Transforms/InstSimplify/fast-math.ll
+++ llvm/test/Transforms/InstSimplify/fast-math.ll
@@ -56,8 +56,8 @@
; -X + X --> 0.0 (with nnan on the fadd)
-define float @fadd_fnegx(float %x) {
-; CHECK-LABEL: @fadd_fnegx(
+define float @fadd_binary_fnegx(float %x) {
+; CHECK-LABEL: @fadd_binary_fnegx(
; CHECK-NEXT: ret float 0.000000e+00
;
%negx = fsub float -0.0, %x
@@ -65,10 +65,19 @@
ret float %r
}
+define float @fadd_unary_fnegx(float %x) {
+; CHECK-LABEL: @fadd_unary_fnegx(
+; CHECK-NEXT: ret float 0.000000e+00
+;
+ %negx = fneg float %x
+ %r = fadd nnan float %negx, %x
+ ret float %r
+}
+
; X + -X --> 0.0 (with nnan on the fadd)
-define <2 x float> @fadd_fnegx_commute_vec(<2 x float> %x) {
-; CHECK-LABEL: @fadd_fnegx_commute_vec(
+define <2 x float> @fadd_binary_fnegx_commute_vec(<2 x float> %x) {
+; CHECK-LABEL: @fadd_binary_fnegx_commute_vec(
; CHECK-NEXT: ret <2 x float> zeroinitializer
;
%negx = fsub <2 x float> <float -0.0, float -0.0>, %x
@@ -76,6 +85,15 @@
ret <2 x float> %r
}
+define <2 x float> @fadd_unary_fnegx_commute_vec(<2 x float> %x) {
+; CHECK-LABEL: @fadd_unary_fnegx_commute_vec(
+; CHECK-NEXT: ret <2 x float> zeroinitializer
+;
+ %negx = fneg <2 x float> %x
+ %r = fadd nnan <2 x float> %x, %negx
+ ret <2 x float> %r
+}
+
define <2 x float> @fadd_fnegx_commute_vec_undef(<2 x float> %x) {
; CHECK-LABEL: @fadd_fnegx_commute_vec_undef(
; CHECK-NEXT: ret <2 x float> zeroinitializer
Index: llvm/lib/Analysis/InstructionSimplify.cpp
===================================================================
--- llvm/lib/Analysis/InstructionSimplify.cpp
+++ llvm/lib/Analysis/InstructionSimplify.cpp
@@ -4316,16 +4316,22 @@
(FMF.noSignedZeros() || CannotBeNegativeZero(Op0, Q.TLI)))
return Op0;
- // With nnan: (+/-0.0 - X) + X --> 0.0 (and commuted variant)
+ // 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:
// X = -0.0: (-0.0 - (-0.0)) + (-0.0) == ( 0.0) + (-0.0) == 0.0
// X = -0.0: ( 0.0 - (-0.0)) + (-0.0) == ( 0.0) + (-0.0) == 0.0
// X = 0.0: (-0.0 - ( 0.0)) + ( 0.0) == (-0.0) + ( 0.0) == 0.0
// X = 0.0: ( 0.0 - ( 0.0)) + ( 0.0) == ( 0.0) + ( 0.0) == 0.0
- if (FMF.noNaNs() && (match(Op0, m_FSub(m_AnyZeroFP(), m_Specific(Op1))) ||
- match(Op1, m_FSub(m_AnyZeroFP(), m_Specific(Op0)))))
- return ConstantFP::getNullValue(Op0->getType());
+ if (FMF.noNaNs()) {
+ if (match(Op0, m_FSub(m_AnyZeroFP(), m_Specific(Op1))) ||
+ match(Op1, m_FSub(m_AnyZeroFP(), m_Specific(Op0))))
+ return ConstantFP::getNullValue(Op0->getType());
+
+ if (match(Op0, m_FNeg(m_Specific(Op1))) ||
+ match(Op1, m_FNeg(m_Specific(Op0))))
+ return ConstantFP::getNullValue(Op0->getType());
+ }
// (X - Y) + Y --> X
// Y + (X - Y) --> X
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D61916.199507.patch
Type: text/x-patch
Size: 3089 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190514/e6f5d9d7/attachment.bin>
More information about the llvm-commits
mailing list