* [PATCH] Optimize x < 0 ? ~y : y to (x >> 31) ^ y in match.pd
@ 2021-05-23 9:52 apinski
2021-05-23 12:18 ` Martin Liška
2021-05-23 12:38 ` Marc Glisse
0 siblings, 2 replies; 6+ messages in thread
From: apinski @ 2021-05-23 9:52 UTC (permalink / raw)
To: gcc-patches; +Cc: Andrew Pinski
From: Andrew Pinski <apinski@marvell.com>
This copies the optimization that is done in phiopt for
"x < 0 ? ~y : y to (x >> 31) ^ y" into match.pd. The code
for phiopt is kept around until phiopt uses match.pd (which
I am working towards).
Note the original testcase is now optimized early on and I added a
new testcase to optimize during phiopt.
OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.
Thanks,
Andrew Pinski
gcc:
* match.pd (x < 0 ? ~y : y): New patterns.
gcc/testsuite:
* gcc.dg/tree-ssa/pr96928.c: Update test for slightly different IR.
* gcc.dg/tree-ssa/pr96928-1.c: New testcase.
---
gcc/match.pd | 30 +++++++++++++++++++
gcc/testsuite/gcc.dg/tree-ssa/pr96928-1.c | 48 +++++++++++++++++++++++++++++++
gcc/testsuite/gcc.dg/tree-ssa/pr96928.c | 7 +++--
3 files changed, 83 insertions(+), 2 deletions(-)
create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr96928-1.c
diff --git a/gcc/match.pd b/gcc/match.pd
index ad6b057..6710aa9 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -4875,6 +4875,36 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
(cmp (bit_and@2 @0 integer_pow2p@1) @1)
(icmp @2 { build_zero_cst (TREE_TYPE (@0)); })))
+(for cmp (ge lt)
+/* x < 0 ? ~y : y into (x >> (prec-1)) ^ y. */
+/* x >= 0 ? ~y : y into ~((x >> (prec-1)) ^ y). */
+ (simplify
+ (cond (cmp @0 integer_zerop) (bit_not @1) @1)
+ (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
+ && !TYPE_UNSIGNED (TREE_TYPE (@0))
+ && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
+ (with
+ {
+ tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
+ }
+ (if (cmp == LT_EXPR)
+ (bit_xor (convert (rshift @0 {shifter;})) @1)
+ (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1))))))
+/* x < 0 ? y : ~y into ~((x >> (prec-1)) ^ y). */
+/* x >= 0 ? y : ~y into (x >> (prec-1)) ^ y. */
+ (simplify
+ (cond (cmp @0 integer_zerop) @1 (bit_not @1))
+ (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
+ && !TYPE_UNSIGNED (TREE_TYPE (@0))
+ && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
+ (with
+ {
+ tree shifter = build_int_cst (integer_type_node, TYPE_PRECISION (type) - 1);
+ }
+ (if (cmp == GE_EXPR)
+ (bit_xor (convert (rshift @0 {shifter;})) @1)
+ (bit_not (bit_xor (convert (rshift @0 {shifter;})) @1)))))))
+
/* If we have (A & C) != 0 ? D : 0 where C and D are powers of 2,
convert this into a shift followed by ANDing with D. */
(simplify
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr96928-1.c b/gcc/testsuite/gcc.dg/tree-ssa/pr96928-1.c
new file mode 100644
index 0000000..a2770e5
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr96928-1.c
@@ -0,0 +1,48 @@
+/* PR tree-optimization/96928 */
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-phiopt2" } */
+/* { dg-final { scan-tree-dump-times " = a_\[0-9]*\\\(D\\\) >> " 5 "phiopt2" } } */
+/* { dg-final { scan-tree-dump-times " = ~c_\[0-9]*\\\(D\\\);" 1 "phiopt2" } } */
+/* { dg-final { scan-tree-dump-times " = ~" 1 "phiopt2" } } */
+/* { dg-final { scan-tree-dump-times " = \[abc_0-9\\\(\\\)D]* \\\^ " 5 "phiopt2" } } */
+/* { dg-final { scan-tree-dump-not "a < 0" "phiopt2" } } */
+
+int
+foo (int a)
+{
+ if (a < 0)
+ return ~a;
+ return a;
+}
+
+int
+bar (int a, int b)
+{
+ if (a < 0)
+ return ~b;
+ return b;
+}
+
+unsigned
+baz (int a, unsigned int b)
+{
+ if (a < 0)
+ return ~b;
+ return b;
+}
+
+unsigned
+qux (int a, unsigned int c)
+{
+ if (a >= 0)
+ return ~c;
+ return c;
+}
+
+int
+corge (int a, int b)
+{
+ if (a >= 0)
+ return b;
+ return ~b;
+}
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr96928.c b/gcc/testsuite/gcc.dg/tree-ssa/pr96928.c
index 2091357..e8fd82f 100644
--- a/gcc/testsuite/gcc.dg/tree-ssa/pr96928.c
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr96928.c
@@ -1,8 +1,11 @@
/* PR tree-optimization/96928 */
/* { dg-do compile } */
-/* { dg-options "-O2 -fdump-tree-phiopt2" } */
+/* { dg-options "-O2 -fdump-tree-phiopt2 -fdump-tree-optimized" } */
/* { dg-final { scan-tree-dump-times " = a_\[0-9]*\\\(D\\\) >> " 5 "phiopt2" } } */
-/* { dg-final { scan-tree-dump-times " = ~c_\[0-9]*\\\(D\\\);" 1 "phiopt2" } } */
+/* The following check is done at optimized because a ^ (~b) is rewritten as ~(a^b)
+ and in the case of match.pd optimizing these ?:, the ~ is moved out already
+ by the time we get to phiopt2. */
+/* { dg-final { scan-tree-dump-times "\\\^ c_\[0-9]*\\\(D\\\);" 1 "optimized" } } */
/* { dg-final { scan-tree-dump-times " = ~" 1 "phiopt2" } } */
/* { dg-final { scan-tree-dump-times " = \[abc_0-9\\\(\\\)D]* \\\^ " 5 "phiopt2" } } */
/* { dg-final { scan-tree-dump-not "a < 0" "phiopt2" } } */
--
1.8.3.1
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] Optimize x < 0 ? ~y : y to (x >> 31) ^ y in match.pd
2021-05-23 9:52 [PATCH] Optimize x < 0 ? ~y : y to (x >> 31) ^ y in match.pd apinski
@ 2021-05-23 12:18 ` Martin Liška
2021-05-23 16:24 ` Andrew Pinski
2021-05-23 12:38 ` Marc Glisse
1 sibling, 1 reply; 6+ messages in thread
From: Martin Liška @ 2021-05-23 12:18 UTC (permalink / raw)
To: apinski, gcc-patches
Hello.
Similarly to:
https://gcc.gnu.org/pipermail/gcc-patches/2021-May/571039.html
I've just reverted the ChangeLog commit change. I'm planning update
of the documentation.
Cheers,
Martin
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] Optimize x < 0 ? ~y : y to (x >> 31) ^ y in match.pd
2021-05-23 9:52 [PATCH] Optimize x < 0 ? ~y : y to (x >> 31) ^ y in match.pd apinski
2021-05-23 12:18 ` Martin Liška
@ 2021-05-23 12:38 ` Marc Glisse
2021-05-23 16:21 ` Andrew Pinski
1 sibling, 1 reply; 6+ messages in thread
From: Marc Glisse @ 2021-05-23 12:38 UTC (permalink / raw)
To: Andrew Pinski; +Cc: gcc-patches
On Sun, 23 May 2021, apinski--- via Gcc-patches wrote:
> +(for cmp (ge lt)
> +/* x < 0 ? ~y : y into (x >> (prec-1)) ^ y. */
> +/* x >= 0 ? ~y : y into ~((x >> (prec-1)) ^ y). */
> + (simplify
> + (cond (cmp @0 integer_zerop) (bit_not @1) @1)
> + (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
> + && !TYPE_UNSIGNED (TREE_TYPE (@0))
> + && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
Is there a risk that x is signed char (precision 8) and y is a vector with
8 elements?
--
Marc Glisse
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] Optimize x < 0 ? ~y : y to (x >> 31) ^ y in match.pd
2021-05-23 12:38 ` Marc Glisse
@ 2021-05-23 16:21 ` Andrew Pinski
2021-05-30 18:24 ` Jeff Law
0 siblings, 1 reply; 6+ messages in thread
From: Andrew Pinski @ 2021-05-23 16:21 UTC (permalink / raw)
To: GCC Patches; +Cc: Andrew Pinski
On Sun, May 23, 2021 at 6:14 AM Marc Glisse <marc.glisse@inria.fr> wrote:
>
> On Sun, 23 May 2021, apinski--- via Gcc-patches wrote:
>
> > +(for cmp (ge lt)
> > +/* x < 0 ? ~y : y into (x >> (prec-1)) ^ y. */
> > +/* x >= 0 ? ~y : y into ~((x >> (prec-1)) ^ y). */
> > + (simplify
> > + (cond (cmp @0 integer_zerop) (bit_not @1) @1)
> > + (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
> > + && !TYPE_UNSIGNED (TREE_TYPE (@0))
> > + && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
>
> Is there a risk that x is signed char (precision 8) and y is a vector with
> 8 elements?
Yes there should be a check for "INTEGRAL_TYPE_P (type)" there too. I
had missed it when I was doing the conversion. I will add it and
resubmit the patch.
Thanks,
Andrew
>
> --
> Marc Glisse
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] Optimize x < 0 ? ~y : y to (x >> 31) ^ y in match.pd
2021-05-23 12:18 ` Martin Liška
@ 2021-05-23 16:24 ` Andrew Pinski
0 siblings, 0 replies; 6+ messages in thread
From: Andrew Pinski @ 2021-05-23 16:24 UTC (permalink / raw)
To: Martin Liška; +Cc: apinski, GCC Patches
On Sun, May 23, 2021 at 5:49 AM Martin Liška <mliska@suse.cz> wrote:
>
> Hello.
>
> Similarly to:
> https://gcc.gnu.org/pipermail/gcc-patches/2021-May/571039.html
>
> I've just reverted the ChangeLog commit change. I'm planning update
> of the documentation.
Obviously is for the other patch which I pushed and not for this one.
I was trying to figure out why I could not push my changes with the
changelog included, the error message was not so obvious either.
Thanks,
Andrew
>
> Cheers,
> Martin
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH] Optimize x < 0 ? ~y : y to (x >> 31) ^ y in match.pd
2021-05-23 16:21 ` Andrew Pinski
@ 2021-05-30 18:24 ` Jeff Law
0 siblings, 0 replies; 6+ messages in thread
From: Jeff Law @ 2021-05-30 18:24 UTC (permalink / raw)
To: Andrew Pinski, GCC Patches; +Cc: Andrew Pinski
On 5/23/2021 10:21 AM, Andrew Pinski via Gcc-patches wrote:
> On Sun, May 23, 2021 at 6:14 AM Marc Glisse <marc.glisse@inria.fr> wrote:
>> On Sun, 23 May 2021, apinski--- via Gcc-patches wrote:
>>
>>> +(for cmp (ge lt)
>>> +/* x < 0 ? ~y : y into (x >> (prec-1)) ^ y. */
>>> +/* x >= 0 ? ~y : y into ~((x >> (prec-1)) ^ y). */
>>> + (simplify
>>> + (cond (cmp @0 integer_zerop) (bit_not @1) @1)
>>> + (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
>>> + && !TYPE_UNSIGNED (TREE_TYPE (@0))
>>> + && TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type))
>> Is there a risk that x is signed char (precision 8) and y is a vector with
>> 8 elements?
> Yes there should be a check for "INTEGRAL_TYPE_P (type)" there too. I
> had missed it when I was doing the conversion. I will add it and
> resubmit the patch.
Consider it approved with that fix.
Thanks,
Jeff
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2021-05-30 18:24 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-23 9:52 [PATCH] Optimize x < 0 ? ~y : y to (x >> 31) ^ y in match.pd apinski
2021-05-23 12:18 ` Martin Liška
2021-05-23 16:24 ` Andrew Pinski
2021-05-23 12:38 ` Marc Glisse
2021-05-23 16:21 ` Andrew Pinski
2021-05-30 18:24 ` Jeff Law
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).