public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] Fix two testcases for ssa names which are more than 1 digit
@ 2021-05-24  1:12 apinski
  2021-05-24  1:12 ` [PATCHv2] Optimize x < 0 ? ~y : y to (x >> 31) ^ y in match.pd apinski
  0 siblings, 1 reply; 3+ messages in thread
From: apinski @ 2021-05-24  1:12 UTC (permalink / raw)
  To: gcc-patches; +Cc: Andrew Pinski

From: Andrew Pinski <apinski@marvell.com>

phi-opt-10.c and phi-opt-7.c both depend on currently that some ssa name
versions are one digit long which is not always correct. This fixes the
problem by detecting digits rather than just using '.'.

Committed as obvious after a bootstrap/test.

Thanks,
Andrew Pinski

gcc/testsuite/ChangeLog
	* gcc.dg/tree-ssa/phi-opt-10.c: Use "\[0-9\]*" instead of '.'
	when matching ssa name version.
	* gcc.dg/tree-ssa/phi-opt-7.c: Likewise.
---
 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-10.c | 2 +-
 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-7.c  | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-10.c b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-10.c
index 4c190e6af7d..3681fa78836 100644
--- a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-10.c
+++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-10.c
@@ -7,4 +7,4 @@ int eqm1_phi (unsigned long a) { return a ? 0 : -1; }
 int spaceship1 (long a) { return a > 0 ? 1 : a < 0 ? -1 : 0; }
 int spaceship2 (long a) { return a > 0 ? 1 : a == 0 ? 0 : -1; }
 
-/* { dg-final { scan-tree-dump-times " = -\[^\r\n\]*_.;" 4 "optimized"} } */
+/* { dg-final { scan-tree-dump-times " = -\[^\r\n\]*_\[0-9\]*;" 4 "optimized"} } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-7.c b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-7.c
index 18ecbd52aa2..51e1f6dfa75 100644
--- a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-7.c
+++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-7.c
@@ -18,5 +18,5 @@ int f(int t, int c)
 /* There should be one ifs as one of them should be changed into
    a conditional and the other should be there still.  */
 /* { dg-final { scan-tree-dump-times "if" 1 "optimized" }  }*/
-/* { dg-final { scan-tree-dump-times "\[^\r\n\]*_. = c_\[0-9\]*.D. != 0" 1 "optimized"  } } */
+/* { dg-final { scan-tree-dump-times "\[^\r\n\]*_\[0-9\]* = c_\[0-9\]*.D. != 0" 1 "optimized"  } } */
 
-- 
2.17.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCHv2] Optimize x < 0 ? ~y : y to (x >> 31) ^ y in match.pd
  2021-05-24  1:12 [PATCH] Fix two testcases for ssa names which are more than 1 digit apinski
@ 2021-05-24  1:12 ` apinski
  2021-05-25 14:19   ` Richard Biener
  0 siblings, 1 reply; 3+ messages in thread
From: apinski @ 2021-05-24  1:12 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

Differences from v1:
V2: Add check for integeral type to make sure vector types are not done.

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                              | 32 +++++++++++++++
 gcc/testsuite/gcc.dg/tree-ssa/pr96928-1.c | 48 +++++++++++++++++++++++
 gcc/testsuite/gcc.dg/tree-ssa/pr96928.c   |  7 +++-
 3 files changed, 85 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 ad6b057c56d..dd730814942 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -4875,6 +4875,38 @@ 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 (type)
+	&& 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 (type)
+	&& 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 00000000000..a2770e5e896
--- /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 20913572691..e8fd82fc26e 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" } } */
-- 
2.17.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCHv2] Optimize x < 0 ? ~y : y to (x >> 31) ^ y in match.pd
  2021-05-24  1:12 ` [PATCHv2] Optimize x < 0 ? ~y : y to (x >> 31) ^ y in match.pd apinski
@ 2021-05-25 14:19   ` Richard Biener
  0 siblings, 0 replies; 3+ messages in thread
From: Richard Biener @ 2021-05-25 14:19 UTC (permalink / raw)
  To: apinski; +Cc: GCC Patches

On Mon, May 24, 2021 at 3:27 AM apinski--- via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
> 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
>
> Differences from v1:
> V2: Add check for integeral type to make sure vector types are not done.
>
> 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                              | 32 +++++++++++++++
>  gcc/testsuite/gcc.dg/tree-ssa/pr96928-1.c | 48 +++++++++++++++++++++++
>  gcc/testsuite/gcc.dg/tree-ssa/pr96928.c   |  7 +++-
>  3 files changed, 85 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 ad6b057c56d..dd730814942 100644
> --- a/gcc/match.pd
> +++ b/gcc/match.pd
> @@ -4875,6 +4875,38 @@ 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 (type)
> +       && 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 (type)
> +       && 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)))))))

I wonder if it makes sense to support

 (for cmp (ge lt)
  (cond:c (cmp @0 integer_zerop) (bit_not @1) @1)
  ...

similar to how we support (cmp:c ...), so (cond:c (cmp ...) @0 @1) would
lower to

  (cond (cmp ..) @0 @1)

and

  (cond (invert_tree_comparison (cmp) ..) @1 @0)

with the caveat that invert_tree_comparison doesn't work for all compares
with HONOR_NANS (and thus compares ever matching FP codes).  Thus the
implementation might be a bit tricky.

Anyway, the patch looks OK to me, we can ponder over match.pd extensions
as followup.

Thanks,
Richard.

>  /* 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 00000000000..a2770e5e896
> --- /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 20913572691..e8fd82fc26e 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" } } */
> --
> 2.17.1
>

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-05-25 14:19 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-24  1:12 [PATCH] Fix two testcases for ssa names which are more than 1 digit apinski
2021-05-24  1:12 ` [PATCHv2] Optimize x < 0 ? ~y : y to (x >> 31) ^ y in match.pd apinski
2021-05-25 14:19   ` Richard Biener

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).