public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [PATCH] match.pd: Implement missed optimization (x << c) >> c -> -(x & 1) [PR101955]
@ 2023-07-20 14:47 Drew Ross
2023-07-20 14:52 ` Drew Ross
2023-07-20 15:17 ` Andrew Pinski
0 siblings, 2 replies; 4+ messages in thread
From: Drew Ross @ 2023-07-20 14:47 UTC (permalink / raw)
To: gcc-bugs; +Cc: Drew Ross
PR middle-end/101955
gcc/ChangeLog:
* match.pd (x << c) >> c -> -(x & 1): New simplification.
gcc/testsuite/ChangeLog:
* gcc.dg/pr101955.c: New test.
---
gcc/match.pd | 9 +++++
gcc/testsuite/gcc.dg/pr101955.c | 62 +++++++++++++++++++++++++++++++++
2 files changed, 71 insertions(+)
create mode 100644 gcc/testsuite/gcc.dg/pr101955.c
diff --git a/gcc/match.pd b/gcc/match.pd
index 8543f777a28..bf63652e80f 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -3766,6 +3766,15 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
&& (wi::ltu_p (wi::to_wide (@1), element_precision (type))))
(bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
+/* Optimize (X << C) >> C where C = precision(type) - 1 and X is signed
+ into -(X & 1). */
+(simplify
+ (rshift (lshift @0 INTEGER_CST@1) @@1)
+ (if (ANY_INTEGRAL_TYPE_P (type)
+ && !TYPE_UNSIGNED (type)
+ && wi::eq_p (wi::to_wide (@1), element_precision (type) - 1))
+ (negate (bit_and @0 { build_one_cst (type); }))))
+
/* Optimize x >> x into 0 */
(simplify
(rshift @0 @0)
diff --git a/gcc/testsuite/gcc.dg/pr101955.c b/gcc/testsuite/gcc.dg/pr101955.c
new file mode 100644
index 00000000000..0e233269e21
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr101955.c
@@ -0,0 +1,62 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-dse1 -Wno-psabi" } */
+
+typedef int v4si __attribute__((vector_size(16)));
+
+__attribute__((noipa)) int
+t1 (int x)
+{
+ return (x << 31) >> 31;
+}
+
+__attribute__((noipa)) int
+t2 (int x)
+{
+ int y = x << 31;
+ int z = y >> 31;
+ return z;
+}
+
+__attribute__((noipa)) int
+t3 (int x)
+{
+ int w = 31;
+ int y = x << w;
+ int z = y >> w;
+ return z;
+}
+
+__attribute__((noipa)) long long
+t4 (long long x)
+{
+ return (x << 63) >> 63;
+}
+
+__attribute__((noipa)) long long
+t5 (long long x)
+{
+ long long y = x << 63;
+ long long z = y >> 63;
+ return z;
+}
+
+__attribute__((noipa)) long long
+t6 (long long x)
+{
+ int w = 63;
+ long long y = x << w;
+ long long z = y >> w;
+ return z;
+}
+
+__attribute__((noipa)) v4si
+t7 (v4si x)
+{
+ return (x << 31) >> 31;
+}
+
+/* { dg-final { scan-tree-dump-not " >> " "dse1" } } */
+/* { dg-final { scan-tree-dump-not " << " "dse1" } } */
+/* { dg-final { scan-tree-dump-times " -" 7 "dse1" } } */
+/* { dg-final { scan-tree-dump-times " & " 7 "dse1" } } */
+
--
2.39.3
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] match.pd: Implement missed optimization (x << c) >> c -> -(x & 1) [PR101955]
2023-07-20 14:47 [PATCH] match.pd: Implement missed optimization (x << c) >> c -> -(x & 1) [PR101955] Drew Ross
@ 2023-07-20 14:52 ` Drew Ross
2023-07-20 15:17 ` Andrew Pinski
1 sibling, 0 replies; 4+ messages in thread
From: Drew Ross @ 2023-07-20 14:52 UTC (permalink / raw)
To: gcc-bugs
[-- Attachment #1: Type: text/plain, Size: 3197 bytes --]
My mailer clipped off the top of the commit message:
Simplifies (x << c) >> c where x is a signed integral type of
width >= int and c = precision(type) - 1 into -(x & 1). Tested successfully
on x86_64 and x86 targets.
PR middle-end/101955
gcc/ChangeLog:
* match.pd (x << c) >> c -> -(x & 1): New simplification.
gcc/testsuite/ChangeLog:
* gcc.dg/pr101955.c: New test.
On Thu, Jul 20, 2023 at 10:47 AM Drew Ross <drross@redhat.com> wrote:
> PR middle-end/101955
>
> gcc/ChangeLog:
>
> * match.pd (x << c) >> c -> -(x & 1): New simplification.
>
> gcc/testsuite/ChangeLog:
>
> * gcc.dg/pr101955.c: New test.
> ---
> gcc/match.pd | 9 +++++
> gcc/testsuite/gcc.dg/pr101955.c | 62 +++++++++++++++++++++++++++++++++
> 2 files changed, 71 insertions(+)
> create mode 100644 gcc/testsuite/gcc.dg/pr101955.c
>
> diff --git a/gcc/match.pd b/gcc/match.pd
> index 8543f777a28..bf63652e80f 100644
> --- a/gcc/match.pd
> +++ b/gcc/match.pd
> @@ -3766,6 +3766,15 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
> && (wi::ltu_p (wi::to_wide (@1), element_precision (type))))
> (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
>
> +/* Optimize (X << C) >> C where C = precision(type) - 1 and X is signed
> + into -(X & 1). */
> +(simplify
> + (rshift (lshift @0 INTEGER_CST@1) @@1)
> + (if (ANY_INTEGRAL_TYPE_P (type)
> + && !TYPE_UNSIGNED (type)
> + && wi::eq_p (wi::to_wide (@1), element_precision (type) - 1))
> + (negate (bit_and @0 { build_one_cst (type); }))))
> +
> /* Optimize x >> x into 0 */
> (simplify
> (rshift @0 @0)
> diff --git a/gcc/testsuite/gcc.dg/pr101955.c
> b/gcc/testsuite/gcc.dg/pr101955.c
> new file mode 100644
> index 00000000000..0e233269e21
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/pr101955.c
> @@ -0,0 +1,62 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O2 -fdump-tree-dse1 -Wno-psabi" } */
> +
> +typedef int v4si __attribute__((vector_size(16)));
> +
> +__attribute__((noipa)) int
> +t1 (int x)
> +{
> + return (x << 31) >> 31;
> +}
> +
> +__attribute__((noipa)) int
> +t2 (int x)
> +{
> + int y = x << 31;
> + int z = y >> 31;
> + return z;
> +}
> +
> +__attribute__((noipa)) int
> +t3 (int x)
> +{
> + int w = 31;
> + int y = x << w;
> + int z = y >> w;
> + return z;
> +}
> +
> +__attribute__((noipa)) long long
> +t4 (long long x)
> +{
> + return (x << 63) >> 63;
> +}
> +
> +__attribute__((noipa)) long long
> +t5 (long long x)
> +{
> + long long y = x << 63;
> + long long z = y >> 63;
> + return z;
> +}
> +
> +__attribute__((noipa)) long long
> +t6 (long long x)
> +{
> + int w = 63;
> + long long y = x << w;
> + long long z = y >> w;
> + return z;
> +}
> +
> +__attribute__((noipa)) v4si
> +t7 (v4si x)
> +{
> + return (x << 31) >> 31;
> +}
> +
> +/* { dg-final { scan-tree-dump-not " >> " "dse1" } } */
> +/* { dg-final { scan-tree-dump-not " << " "dse1" } } */
> +/* { dg-final { scan-tree-dump-times " -" 7 "dse1" } } */
> +/* { dg-final { scan-tree-dump-times " & " 7 "dse1" } } */
> +
> --
> 2.39.3
>
>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] match.pd: Implement missed optimization (x << c) >> c -> -(x & 1) [PR101955]
2023-07-20 14:47 [PATCH] match.pd: Implement missed optimization (x << c) >> c -> -(x & 1) [PR101955] Drew Ross
2023-07-20 14:52 ` Drew Ross
@ 2023-07-20 15:17 ` Andrew Pinski
2023-07-20 18:08 ` Andrew Pinski
1 sibling, 1 reply; 4+ messages in thread
From: Andrew Pinski @ 2023-07-20 15:17 UTC (permalink / raw)
To: Drew Ross; +Cc: gcc-bugs
On Thu, Jul 20, 2023 at 7:47 AM Drew Ross via Gcc-bugs
<gcc-bugs@gcc.gnu.org> wrote:
>
> PR middle-end/101955
>
> gcc/ChangeLog:
>
> * match.pd (x << c) >> c -> -(x & 1): New simplification.
>
> gcc/testsuite/ChangeLog:
>
> * gcc.dg/pr101955.c: New test.
> ---
> gcc/match.pd | 9 +++++
> gcc/testsuite/gcc.dg/pr101955.c | 62 +++++++++++++++++++++++++++++++++
> 2 files changed, 71 insertions(+)
> create mode 100644 gcc/testsuite/gcc.dg/pr101955.c
>
> diff --git a/gcc/match.pd b/gcc/match.pd
> index 8543f777a28..bf63652e80f 100644
> --- a/gcc/match.pd
> +++ b/gcc/match.pd
> @@ -3766,6 +3766,15 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
> && (wi::ltu_p (wi::to_wide (@1), element_precision (type))))
> (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
>
> +/* Optimize (X << C) >> C where C = precision(type) - 1 and X is signed
> + into -(X & 1). */
> +(simplify
> + (rshift (lshift @0 INTEGER_CST@1) @@1)
> + (if (ANY_INTEGRAL_TYPE_P (type)
> + && !TYPE_UNSIGNED (type)
> + && wi::eq_p (wi::to_wide (@1), element_precision (type) - 1))
> + (negate (bit_and @0 { build_one_cst (type); }))))
I think the lshift does not need to be in the same type but could be
in the unsigned type so you should add a nop_convert? operand there
That is:
(simplify
(rshift (nop_convert? (lshift @0 INTEGER_CST@1)) @@1)
...
(negate (bit_and (convert @0) { build_one_cst (type); }))))
Note I see you use INTEGER_CST and then ANY_INTEGRAL_TYPE_P. If you
are going to support vectors, you should use uniform_integer_cst_p
instead of INTEGER_CST and then use uniform_integer_cst_p to get the
constant.
Thanks,
Andrew
> +
> /* Optimize x >> x into 0 */
> (simplify
> (rshift @0 @0)
> diff --git a/gcc/testsuite/gcc.dg/pr101955.c b/gcc/testsuite/gcc.dg/pr101955.c
> new file mode 100644
> index 00000000000..0e233269e21
> --- /dev/null
> +++ b/gcc/testsuite/gcc.dg/pr101955.c
> @@ -0,0 +1,62 @@
> +/* { dg-do compile } */
> +/* { dg-options "-O2 -fdump-tree-dse1 -Wno-psabi" } */
> +
> +typedef int v4si __attribute__((vector_size(16)));
> +
> +__attribute__((noipa)) int
> +t1 (int x)
> +{
> + return (x << 31) >> 31;
> +}
> +
> +__attribute__((noipa)) int
> +t2 (int x)
> +{
> + int y = x << 31;
> + int z = y >> 31;
> + return z;
> +}
> +
> +__attribute__((noipa)) int
> +t3 (int x)
> +{
> + int w = 31;
> + int y = x << w;
> + int z = y >> w;
> + return z;
> +}
> +
> +__attribute__((noipa)) long long
> +t4 (long long x)
> +{
> + return (x << 63) >> 63;
> +}
> +
> +__attribute__((noipa)) long long
> +t5 (long long x)
> +{
> + long long y = x << 63;
> + long long z = y >> 63;
> + return z;
> +}
> +
> +__attribute__((noipa)) long long
> +t6 (long long x)
> +{
> + int w = 63;
> + long long y = x << w;
> + long long z = y >> w;
> + return z;
> +}
> +
> +__attribute__((noipa)) v4si
> +t7 (v4si x)
> +{
> + return (x << 31) >> 31;
> +}
> +
> +/* { dg-final { scan-tree-dump-not " >> " "dse1" } } */
> +/* { dg-final { scan-tree-dump-not " << " "dse1" } } */
> +/* { dg-final { scan-tree-dump-times " -" 7 "dse1" } } */
> +/* { dg-final { scan-tree-dump-times " & " 7 "dse1" } } */
> +
> --
> 2.39.3
>
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] match.pd: Implement missed optimization (x << c) >> c -> -(x & 1) [PR101955]
2023-07-20 15:17 ` Andrew Pinski
@ 2023-07-20 18:08 ` Andrew Pinski
0 siblings, 0 replies; 4+ messages in thread
From: Andrew Pinski @ 2023-07-20 18:08 UTC (permalink / raw)
To: Drew Ross; +Cc: gcc-bugs
On Thu, Jul 20, 2023 at 8:17 AM Andrew Pinski <pinskia@gmail.com> wrote:
>
> On Thu, Jul 20, 2023 at 7:47 AM Drew Ross via Gcc-bugs
> <gcc-bugs@gcc.gnu.org> wrote:
> >
> > PR middle-end/101955
> >
> > gcc/ChangeLog:
> >
> > * match.pd (x << c) >> c -> -(x & 1): New simplification.
> >
> > gcc/testsuite/ChangeLog:
> >
> > * gcc.dg/pr101955.c: New test.
> > ---
> > gcc/match.pd | 9 +++++
> > gcc/testsuite/gcc.dg/pr101955.c | 62 +++++++++++++++++++++++++++++++++
> > 2 files changed, 71 insertions(+)
> > create mode 100644 gcc/testsuite/gcc.dg/pr101955.c
> >
> > diff --git a/gcc/match.pd b/gcc/match.pd
> > index 8543f777a28..bf63652e80f 100644
> > --- a/gcc/match.pd
> > +++ b/gcc/match.pd
> > @@ -3766,6 +3766,15 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
> > && (wi::ltu_p (wi::to_wide (@1), element_precision (type))))
> > (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
> >
> > +/* Optimize (X << C) >> C where C = precision(type) - 1 and X is signed
> > + into -(X & 1). */
> > +(simplify
> > + (rshift (lshift @0 INTEGER_CST@1) @@1)
> > + (if (ANY_INTEGRAL_TYPE_P (type)
> > + && !TYPE_UNSIGNED (type)
> > + && wi::eq_p (wi::to_wide (@1), element_precision (type) - 1))
> > + (negate (bit_and @0 { build_one_cst (type); }))))
>
> I think the lshift does not need to be in the same type but could be
> in the unsigned type so you should add a nop_convert? operand there
> That is:
> (simplify
> (rshift (nop_convert? (lshift @0 INTEGER_CST@1)) @@1)
> ...
> (negate (bit_and (convert @0) { build_one_cst (type); }))))
>
> Note I see you use INTEGER_CST and then ANY_INTEGRAL_TYPE_P. If you
> are going to support vectors, you should use uniform_integer_cst_p
> instead of INTEGER_CST and then use uniform_integer_cst_p to get the
> constant.
Note I just noticed you sent the patch to gcc-bugs@, it really should
go to gcc-patches@ .
Thanks,
Andrew
>
> Thanks,
> Andrew
>
>
> > +
> > /* Optimize x >> x into 0 */
> > (simplify
> > (rshift @0 @0)
> > diff --git a/gcc/testsuite/gcc.dg/pr101955.c b/gcc/testsuite/gcc.dg/pr101955.c
> > new file mode 100644
> > index 00000000000..0e233269e21
> > --- /dev/null
> > +++ b/gcc/testsuite/gcc.dg/pr101955.c
> > @@ -0,0 +1,62 @@
> > +/* { dg-do compile } */
> > +/* { dg-options "-O2 -fdump-tree-dse1 -Wno-psabi" } */
> > +
> > +typedef int v4si __attribute__((vector_size(16)));
> > +
> > +__attribute__((noipa)) int
> > +t1 (int x)
> > +{
> > + return (x << 31) >> 31;
> > +}
> > +
> > +__attribute__((noipa)) int
> > +t2 (int x)
> > +{
> > + int y = x << 31;
> > + int z = y >> 31;
> > + return z;
> > +}
> > +
> > +__attribute__((noipa)) int
> > +t3 (int x)
> > +{
> > + int w = 31;
> > + int y = x << w;
> > + int z = y >> w;
> > + return z;
> > +}
> > +
> > +__attribute__((noipa)) long long
> > +t4 (long long x)
> > +{
> > + return (x << 63) >> 63;
> > +}
> > +
> > +__attribute__((noipa)) long long
> > +t5 (long long x)
> > +{
> > + long long y = x << 63;
> > + long long z = y >> 63;
> > + return z;
> > +}
> > +
> > +__attribute__((noipa)) long long
> > +t6 (long long x)
> > +{
> > + int w = 63;
> > + long long y = x << w;
> > + long long z = y >> w;
> > + return z;
> > +}
> > +
> > +__attribute__((noipa)) v4si
> > +t7 (v4si x)
> > +{
> > + return (x << 31) >> 31;
> > +}
> > +
> > +/* { dg-final { scan-tree-dump-not " >> " "dse1" } } */
> > +/* { dg-final { scan-tree-dump-not " << " "dse1" } } */
> > +/* { dg-final { scan-tree-dump-times " -" 7 "dse1" } } */
> > +/* { dg-final { scan-tree-dump-times " & " 7 "dse1" } } */
> > +
> > --
> > 2.39.3
> >
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2023-07-20 18:08 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-20 14:47 [PATCH] match.pd: Implement missed optimization (x << c) >> c -> -(x & 1) [PR101955] Drew Ross
2023-07-20 14:52 ` Drew Ross
2023-07-20 15:17 ` Andrew Pinski
2023-07-20 18:08 ` Andrew Pinski
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).