From: Andrew Pinski <quic_apinski@quicinc.com>
To: <gcc-patches@gcc.gnu.org>
Subject: [PATCHv2 2/2] MATCH: (convert)(zero_one !=/== 0/1) for outer type and zero_one type are the same
Date: Sun, 10 Dec 2023 11:56:50 -0800 [thread overview]
Message-ID: <20231210195650.1772459-3-quic_apinski@quicinc.com> (raw)
In-Reply-To: <20231210195650.1772459-1-quic_apinski@quicinc.com>
When I moved two_value to match.pd, I removed the check for the {0,+-1}
as I had placed it after the {0,+-1} case for cond in match.pd.
In the case of {0,+-1} and non boolean, before we would optmize those
case to just `(convert)a` but after we would get `(convert)(a != 0)`
which was not handled anyways to just `(convert)a`.
So this adds a pattern to match `(convert)(zeroone != 0)` and simplify
to `(convert)zeroone`.
Also this optimizes (convert)(zeroone == 0) into (zeroone^1) if the
type match. Removing the opposite transformation from fold.
The opposite transformation was added with
https://gcc.gnu.org/pipermail/gcc-patches/2006-February/190514.html
It is no longer considered the canonicalization either, even VRP will
transform it back into `(~a) & 1` so removing it is a good idea.
Note the testcase pr69270.c needed a slight update due to not matching
exactly a scan pattern, this update makes it more robust and will match
before and afterwards and if there are other changes in this area too.
Note the testcase gcc.target/i386/pr110790-2.c needs a slight update
for better code generation in LP64 bit mode.
Bootstrapped and tested on x86_64-linux-gnu with no regressions.
gcc/ChangeLog:
PR tree-optimization/111972
PR tree-optimization/110637
* match.pd (`(convert)(zeroone !=/== CST)`): Match
and simplify to ((convert)zeroone){,^1}.
* fold-const.cc (fold_binary_loc): Remove
transformation of `(~a) & 1` and `(a ^ 1) & 1)`
into `(convert)(a == 0)`.
gcc/testsuite/ChangeLog:
* gcc.dg/tree-ssa/pr110637-1.c: New test.
* gcc.dg/tree-ssa/pr110637-2.c: New test.
* gcc.dg/tree-ssa/pr110637-3.c: New test.
* gcc.dg/tree-ssa/pr111972-1.c: New test.
* gcc.dg/tree-ssa/pr69270.c: Update testcase.
* gcc.target/i386/pr110790-2.c: Update testcase.
* gcc.dg/fold-even-1.c: Removed.
Signed-off-by: Andrew Pinski <quic_apinski@quicinc.com>
---
gcc/fold-const.cc | 27 -----------------
gcc/match.pd | 16 ++++++++++
gcc/testsuite/gcc.dg/fold-even-1.c | 32 --------------------
gcc/testsuite/gcc.dg/tree-ssa/pr110637-1.c | 10 +++++++
gcc/testsuite/gcc.dg/tree-ssa/pr110637-2.c | 13 +++++++++
gcc/testsuite/gcc.dg/tree-ssa/pr110637-3.c | 14 +++++++++
gcc/testsuite/gcc.dg/tree-ssa/pr111972-1.c | 34 ++++++++++++++++++++++
gcc/testsuite/gcc.dg/tree-ssa/pr69270.c | 4 +--
gcc/testsuite/gcc.target/i386/pr110790-2.c | 16 ++++++++--
9 files changed, 103 insertions(+), 63 deletions(-)
delete mode 100644 gcc/testsuite/gcc.dg/fold-even-1.c
create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr110637-1.c
create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr110637-2.c
create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr110637-3.c
create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr111972-1.c
diff --git a/gcc/fold-const.cc b/gcc/fold-const.cc
index 2692b98ceac..f5d68ac323a 100644
--- a/gcc/fold-const.cc
+++ b/gcc/fold-const.cc
@@ -12077,33 +12077,6 @@ fold_binary_loc (location_t loc, enum tree_code code, tree type,
goto bit_rotate;
case BIT_AND_EXPR:
- /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
- if (TREE_CODE (arg0) == BIT_XOR_EXPR
- && INTEGRAL_TYPE_P (type)
- && integer_onep (TREE_OPERAND (arg0, 1))
- && integer_onep (arg1))
- {
- tree tem2;
- tem = TREE_OPERAND (arg0, 0);
- tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
- tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
- tem, tem2);
- return fold_build2_loc (loc, EQ_EXPR, type, tem2,
- build_zero_cst (TREE_TYPE (tem)));
- }
- /* Fold ~X & 1 as (X & 1) == 0. */
- if (TREE_CODE (arg0) == BIT_NOT_EXPR
- && INTEGRAL_TYPE_P (type)
- && integer_onep (arg1))
- {
- tree tem2;
- tem = TREE_OPERAND (arg0, 0);
- tem2 = fold_convert_loc (loc, TREE_TYPE (tem), arg1);
- tem2 = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem),
- tem, tem2);
- return fold_build2_loc (loc, EQ_EXPR, type, tem2,
- build_zero_cst (TREE_TYPE (tem)));
- }
/* Fold !X & 1 as X == 0. */
if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
&& integer_onep (arg1))
diff --git a/gcc/match.pd b/gcc/match.pd
index 4d554ba4721..e242eac92f5 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -3332,6 +3332,22 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
(if (INTEGRAL_TYPE_P (TREE_TYPE (@0)) || POINTER_TYPE_P (TREE_TYPE (@0)))
(rcmp @0 @1))))
+/* (type)([0,1]@a != 0) -> (type)a
+ (type)([0,1]@a == 1) -> (type)a
+ (type)([0,1]@a == 0) -> a ^ 1
+ (type)([0,1]@a != 1) -> a ^ 1. */
+(for eqne (eq ne)
+ (simplify
+ (convert (eqne zero_one_valued_p@0 INTEGER_CST@1))
+ (if ((integer_zerop (@1) || integer_onep (@1)))
+ (if ((eqne == EQ_EXPR) ^ integer_zerop (@1))
+ (convert @0)
+ /* Only do this if the types match as (type)(a == 0) is
+ canonical form normally, while `a ^ 1` is canonical when
+ there is no type change. */
+ (if (types_match (type, TREE_TYPE (@0)))
+ (bit_xor @0 { build_one_cst (type); } ))))))
+
/* We can't reassociate at all for saturating types. */
(if (!TYPE_SATURATING (type))
diff --git a/gcc/testsuite/gcc.dg/fold-even-1.c b/gcc/testsuite/gcc.dg/fold-even-1.c
deleted file mode 100644
index 94711ab1499..00000000000
--- a/gcc/testsuite/gcc.dg/fold-even-1.c
+++ /dev/null
@@ -1,32 +0,0 @@
-/* { dg-do compile } */
-/* { dg-options "-O2 -fdump-tree-original" } */
-int test1(int a)
-{
- return !(a & 1);
-}
-
-int test2(int b)
-{
- return (b & 1) == 0;
-}
-
-int test3(int c)
-{
- return (c & 1) ^ 1;
-}
-
-int test4(int d)
-{
- return (d ^ 1) & 1;
-}
-
-int test5(int e)
-{
- return ~e & 1;
-}
-
-/* { dg-final { scan-tree-dump-times "\\(a \& 1\\) == 0" 1 "original" } } */
-/* { dg-final { scan-tree-dump-times "\\(b \& 1\\) == 0" 1 "original" } } */
-/* { dg-final { scan-tree-dump-times "\\(c \& 1\\) == 0" 1 "original" } } */
-/* { dg-final { scan-tree-dump-times "\\(d \& 1\\) == 0" 1 "original" } } */
-/* { dg-final { scan-tree-dump-times "\\(e \& 1\\) == 0" 1 "original" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr110637-1.c b/gcc/testsuite/gcc.dg/tree-ssa/pr110637-1.c
new file mode 100644
index 00000000000..3d03b0992a4
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr110637-1.c
@@ -0,0 +1,10 @@
+/* { dg-do compile } */
+/* { dg-options "-O1 -fdump-tree-optimized" } */
+int f(int a)
+{
+ int b = (a & 1)!=0;
+ return b;
+}
+
+/* This should be optimized to just return (a & 1); */
+/* { dg-final { scan-tree-dump-not " == " "optimized"} } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr110637-2.c b/gcc/testsuite/gcc.dg/tree-ssa/pr110637-2.c
new file mode 100644
index 00000000000..f1c5b90353a
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr110637-2.c
@@ -0,0 +1,13 @@
+/* { dg-do compile } */
+/* { dg-options "-O1 -fdump-tree-optimized" } */
+int f(int a)
+{
+ int b = a & 1;
+ int c = b == 0;
+ return c;
+}
+
+/* This should be optimized to just return `(a&1) ^ 1` or `(~a) & 1`. */
+/* { dg-final { scan-tree-dump-not " == " "optimized"} } */
+/* { dg-final { scan-tree-dump-times "~a" 1 "optimized"} } */
+/* { dg-final { scan-tree-dump-times " & 1" 1 "optimized"} } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr110637-3.c b/gcc/testsuite/gcc.dg/tree-ssa/pr110637-3.c
new file mode 100644
index 00000000000..ce80146d9df
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr110637-3.c
@@ -0,0 +1,14 @@
+/* { dg-do compile } */
+/* { dg-options "-O1 -fdump-tree-optimized" } */
+int f(int a)
+{
+ int b = a & 1;
+ int c = b == 0;
+ int d = ~a;
+ int e = d & 1;
+ return c == e;
+}
+
+/* This should be optimized to just `return 1` */
+/* { dg-final { scan-tree-dump-not " == " "optimized"} } */
+/* { dg-final { scan-tree-dump-times "return 1" 1 "optimized"} } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr111972-1.c b/gcc/testsuite/gcc.dg/tree-ssa/pr111972-1.c
new file mode 100644
index 00000000000..0611808ed50
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr111972-1.c
@@ -0,0 +1,34 @@
+/* { dg-do compile } */
+/* { dg-options "-O1 -fdump-tree-phiopt" } */
+double
+foo() {
+ long n3 = 3450000, xtra = 7270;
+ long i,ix;
+ long j;
+ double Check;
+
+ /* Section 3, Conditional jumps */
+ j = 0;
+ {
+ for (ix=0; ix<xtra; ix++)
+ {
+ for(i=0; i<n3; i++)
+ {
+ if(j==1) j = 2;
+ else j = 3;
+ if(j>2) j = 0;
+ else j = 1;
+ if(j<1) j = 1;
+ else j = 0;
+ }
+ }
+ }
+ Check = Check + (double)j;
+ return Check;
+}
+
+/* the above if statements in loop should be optimized to just `j ^ 1`
+ and should not be (type)(j != 1). */
+/* { dg-final { scan-tree-dump-not " != 1" "phiopt2"} } */
+/* { dg-final { scan-tree-dump-times " \\^ 1" 1 "phiopt2"} } */
+
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr69270.c b/gcc/testsuite/gcc.dg/tree-ssa/pr69270.c
index 0d66cc4383f..b08ec9d6ddb 100644
--- a/gcc/testsuite/gcc.dg/tree-ssa/pr69270.c
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr69270.c
@@ -7,8 +7,8 @@
/* { dg-final { scan-tree-dump-times "Replaced .bufferstep_\[0-9\]+. with constant .1." 1 "dom3"} } */
/* And some assignments ought to fold down to constants. */
-/* { dg-final { scan-tree-dump-times "Folded to: _\[0-9\]+ = 1;" 1 "dom3"} } */
-/* { dg-final { scan-tree-dump-times "Folded to: _\[0-9\]+ = 0;" 1 "dom3"} } */
+/* { dg-final { scan-tree-dump-times "Folded to: (?:bufferstep)?_\[0-9\]+ = 1;" 1 "dom3"} } */
+/* { dg-final { scan-tree-dump-times "Folded to: (?:bufferstep)?_\[0-9\]+ = 0;" 1 "dom3"} } */
/* The XOR operations should have been optimized to constants. */
/* { dg-final { scan-tree-dump-not "bit_xor" "dom3"} } */
diff --git a/gcc/testsuite/gcc.target/i386/pr110790-2.c b/gcc/testsuite/gcc.target/i386/pr110790-2.c
index 8b9d650c6e9..16c73cb7465 100644
--- a/gcc/testsuite/gcc.target/i386/pr110790-2.c
+++ b/gcc/testsuite/gcc.target/i386/pr110790-2.c
@@ -9,5 +9,17 @@ refmpn_tstbit_bad (mp_srcptr ptr, unsigned long bit)
return (((ptr)[(bit)/(32 - 0)] & (((mp_limb_t) 1L) << ((bit)%(32 - 0)))) != 0);
}
-/* { dg-final { scan-assembler "bt\[ql\]" } } */
-/* { dg-final { scan-assembler "setc" } } */
+/* 32bit produces:
+ btl %eax, %edx
+ setc %al
+ movzbl %al, %eax
+ */
+/* { dg-final { scan-assembler "bt\[ql\]" { target { ! lp64 } } } } */
+/* { dg-final { scan-assembler "setc" { target { ! lp64 } } } } */
+
+/* 64bit produces:
+ shrq %cl, %rax
+ andl $1, %eax
+ */
+/* { dg-final { scan-assembler-times "shrq" 2 { target { lp64 } } } } */
+/* { dg-final { scan-assembler-times "andl" 2 { target { lp64 } } } } */
--
2.39.3
next prev parent reply other threads:[~2023-12-10 19:57 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-12-10 19:56 [PATCHv2 0/2] Fix PR 111972: Missed vectorization due to phiopt changes Andrew Pinski
2023-12-10 19:56 ` [PATCH 1/2] analyzer: Remove check of unsigned_char in maybe_undo_optimize_bit_field_compare Andrew Pinski
2023-12-11 8:04 ` Richard Biener
2023-12-11 14:18 ` David Malcolm
2023-12-10 19:56 ` Andrew Pinski [this message]
2023-12-11 8:03 ` [PATCHv2 2/2] MATCH: (convert)(zero_one !=/== 0/1) for outer type and zero_one type are the same Richard Biener
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20231210195650.1772459-3-quic_apinski@quicinc.com \
--to=quic_apinski@quicinc.com \
--cc=gcc-patches@gcc.gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).