public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] PHIOPT: Fix minmax_replacement for three way
@ 2023-09-21  8:09 Andrew Pinski
  2023-09-26 11:38 ` Richard Biener
  0 siblings, 1 reply; 4+ messages in thread
From: Andrew Pinski @ 2023-09-21  8:09 UTC (permalink / raw)
  To: gcc-patches; +Cc: Andrew Pinski

So when diamond bb support was added to minmax_replacement in r13-1950-g9bb19e143cfe,
the code was not expecting the alt_middle_bb not to exist if it was empty (for threeway_p).
So when factor_out_conditional_conversion was used to factor out conversions, it turns out
the assumption for alt_middle_bb to be wrong and we ended up with threeway_p being true but
having middle_bb being empty but alt_middle_bb not being empty which causes wrong code in
many cases.

This patch fixes the issue by adding a test for the 2 cases where the assumption on
threeway_p case having the other bb being empty.

Note my plan for GCC 15 is remove minmax_replacement as match.pd will catch all cases
at that point.

OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.

	PR tree-optimization/111469

gcc/ChangeLog:

	* tree-ssa-phiopt.cc (minmax_replacement): Fix
	the assumption for the `non-diamond` handling cases
	of diamond code.

gcc/testsuite/ChangeLog:

	* gcc.c-torture/execute/pr111469-1.c: New test.
---
 .../gcc.c-torture/execute/pr111469-1.c        | 38 +++++++++++++++++++
 gcc/tree-ssa-phiopt.cc                        | 10 ++++-
 2 files changed, 46 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/gcc.c-torture/execute/pr111469-1.c

diff --git a/gcc/testsuite/gcc.c-torture/execute/pr111469-1.c b/gcc/testsuite/gcc.c-torture/execute/pr111469-1.c
new file mode 100644
index 00000000000..b68d5989eac
--- /dev/null
+++ b/gcc/testsuite/gcc.c-torture/execute/pr111469-1.c
@@ -0,0 +1,38 @@
+/* PR tree-optimization/111469 */
+
+long f;
+char *g;
+__attribute__((noinline))
+char o() {
+  char l;
+  while (f)
+    ;
+  l = *g;
+  return l;
+}
+
+/* factor_out_conditional_conversion is able to remove the casts
+   from the 2 bbs (correctly)
+   but then minmax_replacement should not optimize this to a MIN_EXPR
+   as o has side effects. */
+
+__attribute__((noinline))
+unsigned short gg(unsigned short a, unsigned short b)
+{
+  short d;
+  if (a > b)
+  {
+    d= b;
+  }
+  else
+  {
+    o();
+    d = a;
+  }
+  return d;
+}
+
+int main(void)
+{
+  gg(3, 2);
+}
diff --git a/gcc/tree-ssa-phiopt.cc b/gcc/tree-ssa-phiopt.cc
index 3835d25d08c..96901a40444 100644
--- a/gcc/tree-ssa-phiopt.cc
+++ b/gcc/tree-ssa-phiopt.cc
@@ -1823,7 +1823,9 @@ minmax_replacement (basic_block cond_bb, basic_block middle_bb, basic_block alt_
       arg_false = arg0;
     }
 
-  if (empty_block_p (middle_bb))
+  if (empty_block_p (middle_bb)
+      && (!threeway_p
+	  || empty_block_p (alt_middle_bb)))
     {
       if ((operand_equal_for_phi_arg_p (arg_true, smaller)
 	   || (alt_smaller
@@ -2006,7 +2008,9 @@ minmax_replacement (basic_block cond_bb, basic_block middle_bb, basic_block alt_
 
       return true;
     }
-  else
+  else if (middle_bb == alt_middle_bb
+           && (!threeway_p
+	       || empty_block_p (alt_middle_bb)))
     {
       /* Recognize the following case, assuming d <= u:
 
@@ -2182,6 +2186,8 @@ minmax_replacement (basic_block cond_bb, basic_block middle_bb, basic_block alt_
 							  SSA_OP_DEF));
       gsi_move_before (&gsi_from, &gsi);
     }
+  else
+    return false;
 
   /* Emit the statement to compute min/max.  */
   gimple_seq stmts = NULL;
-- 
2.31.1


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

* Re: [PATCH] PHIOPT: Fix minmax_replacement for three way
  2023-09-21  8:09 [PATCH] PHIOPT: Fix minmax_replacement for three way Andrew Pinski
@ 2023-09-26 11:38 ` Richard Biener
  0 siblings, 0 replies; 4+ messages in thread
From: Richard Biener @ 2023-09-26 11:38 UTC (permalink / raw)
  To: Andrew Pinski; +Cc: gcc-patches

On Thu, Sep 21, 2023 at 10:10 AM Andrew Pinski <apinski@marvell.com> wrote:
>
> So when diamond bb support was added to minmax_replacement in r13-1950-g9bb19e143cfe,
> the code was not expecting the alt_middle_bb not to exist if it was empty (for threeway_p).
> So when factor_out_conditional_conversion was used to factor out conversions, it turns out
> the assumption for alt_middle_bb to be wrong and we ended up with threeway_p being true but
> having middle_bb being empty but alt_middle_bb not being empty which causes wrong code in
> many cases.
>
> This patch fixes the issue by adding a test for the 2 cases where the assumption on
> threeway_p case having the other bb being empty.
>
> Note my plan for GCC 15 is remove minmax_replacement as match.pd will catch all cases
> at that point.
>
> OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.

OK.

>         PR tree-optimization/111469
>
> gcc/ChangeLog:
>
>         * tree-ssa-phiopt.cc (minmax_replacement): Fix
>         the assumption for the `non-diamond` handling cases
>         of diamond code.
>
> gcc/testsuite/ChangeLog:
>
>         * gcc.c-torture/execute/pr111469-1.c: New test.
> ---
>  .../gcc.c-torture/execute/pr111469-1.c        | 38 +++++++++++++++++++
>  gcc/tree-ssa-phiopt.cc                        | 10 ++++-
>  2 files changed, 46 insertions(+), 2 deletions(-)
>  create mode 100644 gcc/testsuite/gcc.c-torture/execute/pr111469-1.c
>
> diff --git a/gcc/testsuite/gcc.c-torture/execute/pr111469-1.c b/gcc/testsuite/gcc.c-torture/execute/pr111469-1.c
> new file mode 100644
> index 00000000000..b68d5989eac
> --- /dev/null
> +++ b/gcc/testsuite/gcc.c-torture/execute/pr111469-1.c
> @@ -0,0 +1,38 @@
> +/* PR tree-optimization/111469 */
> +
> +long f;
> +char *g;
> +__attribute__((noinline))
> +char o() {
> +  char l;
> +  while (f)
> +    ;
> +  l = *g;
> +  return l;
> +}
> +
> +/* factor_out_conditional_conversion is able to remove the casts
> +   from the 2 bbs (correctly)
> +   but then minmax_replacement should not optimize this to a MIN_EXPR
> +   as o has side effects. */
> +
> +__attribute__((noinline))
> +unsigned short gg(unsigned short a, unsigned short b)
> +{
> +  short d;
> +  if (a > b)
> +  {
> +    d= b;
> +  }
> +  else
> +  {
> +    o();
> +    d = a;
> +  }
> +  return d;
> +}
> +
> +int main(void)
> +{
> +  gg(3, 2);
> +}
> diff --git a/gcc/tree-ssa-phiopt.cc b/gcc/tree-ssa-phiopt.cc
> index 3835d25d08c..96901a40444 100644
> --- a/gcc/tree-ssa-phiopt.cc
> +++ b/gcc/tree-ssa-phiopt.cc
> @@ -1823,7 +1823,9 @@ minmax_replacement (basic_block cond_bb, basic_block middle_bb, basic_block alt_
>        arg_false = arg0;
>      }
>
> -  if (empty_block_p (middle_bb))
> +  if (empty_block_p (middle_bb)
> +      && (!threeway_p
> +         || empty_block_p (alt_middle_bb)))
>      {
>        if ((operand_equal_for_phi_arg_p (arg_true, smaller)
>            || (alt_smaller
> @@ -2006,7 +2008,9 @@ minmax_replacement (basic_block cond_bb, basic_block middle_bb, basic_block alt_
>
>        return true;
>      }
> -  else
> +  else if (middle_bb == alt_middle_bb
> +           && (!threeway_p
> +              || empty_block_p (alt_middle_bb)))
>      {
>        /* Recognize the following case, assuming d <= u:
>
> @@ -2182,6 +2186,8 @@ minmax_replacement (basic_block cond_bb, basic_block middle_bb, basic_block alt_
>                                                           SSA_OP_DEF));
>        gsi_move_before (&gsi_from, &gsi);
>      }
> +  else
> +    return false;
>
>    /* Emit the statement to compute min/max.  */
>    gimple_seq stmts = NULL;
> --
> 2.31.1
>

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

* Re: [PATCH] PHIOPT: Fix minmax_replacement for three way
  2023-09-23  0:54 Andrew Pinski
@ 2023-09-26 11:38 ` Richard Biener
  0 siblings, 0 replies; 4+ messages in thread
From: Richard Biener @ 2023-09-26 11:38 UTC (permalink / raw)
  To: Andrew Pinski; +Cc: gcc-patches

On Sat, Sep 23, 2023 at 2:55 AM Andrew Pinski <apinski@marvell.com> wrote:
>
> So when diamond bb support was added to minmax_replacement in r13-1950-g9bb19e143cfe,
> the code was not expecting the alt_middle_bb not to exist if it was empty (for threeway_p).
> So when factor_out_conditional_conversion was used to factor out conversions, it turns out
> the assumption for alt_middle_bb to be wrong and we ended up with threeway_p being true but
> having middle_bb being empty but alt_middle_bb not being empty which causes wrong code in
> many cases.
>
> This patch fixes the issue by adding a test for the 2 cases where the assumption on
> threeway_p case having the other bb being empty.
>
> Changes made:
> v2: Fix test for `(a <= u) b = MAX(a, d) else b = u`.
>
> Note my plan for GCC 15 is remove minmax_replacement as match.pd will catch all cases
> at that point.
>
> OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.

OK.

>         PR tree-optimization/111469
>
> gcc/ChangeLog:
>
>         * tree-ssa-phiopt.cc (minmax_replacement): Fix
>         the assumption for the `non-diamond` handling cases
>         of diamond code.
>
> gcc/testsuite/ChangeLog:
>
>         * gcc.c-torture/execute/pr111469-1.c: New test.
> ---
>  .../gcc.c-torture/execute/pr111469-1.c        | 38 +++++++++++++++++++
>  gcc/tree-ssa-phiopt.cc                        |  9 ++++-
>  2 files changed, 45 insertions(+), 2 deletions(-)
>  create mode 100644 gcc/testsuite/gcc.c-torture/execute/pr111469-1.c
>
> diff --git a/gcc/testsuite/gcc.c-torture/execute/pr111469-1.c b/gcc/testsuite/gcc.c-torture/execute/pr111469-1.c
> new file mode 100644
> index 00000000000..b68d5989eac
> --- /dev/null
> +++ b/gcc/testsuite/gcc.c-torture/execute/pr111469-1.c
> @@ -0,0 +1,38 @@
> +/* PR tree-optimization/111469 */
> +
> +long f;
> +char *g;
> +__attribute__((noinline))
> +char o() {
> +  char l;
> +  while (f)
> +    ;
> +  l = *g;
> +  return l;
> +}
> +
> +/* factor_out_conditional_conversion is able to remove the casts
> +   from the 2 bbs (correctly)
> +   but then minmax_replacement should not optimize this to a MIN_EXPR
> +   as o has side effects. */
> +
> +__attribute__((noinline))
> +unsigned short gg(unsigned short a, unsigned short b)
> +{
> +  short d;
> +  if (a > b)
> +  {
> +    d= b;
> +  }
> +  else
> +  {
> +    o();
> +    d = a;
> +  }
> +  return d;
> +}
> +
> +int main(void)
> +{
> +  gg(3, 2);
> +}
> diff --git a/gcc/tree-ssa-phiopt.cc b/gcc/tree-ssa-phiopt.cc
> index 3835d25d08c..312a6f9082b 100644
> --- a/gcc/tree-ssa-phiopt.cc
> +++ b/gcc/tree-ssa-phiopt.cc
> @@ -1823,7 +1823,9 @@ minmax_replacement (basic_block cond_bb, basic_block middle_bb, basic_block alt_
>        arg_false = arg0;
>      }
>
> -  if (empty_block_p (middle_bb))
> +  if (empty_block_p (middle_bb)
> +      && (!threeway_p
> +         || empty_block_p (alt_middle_bb)))
>      {
>        if ((operand_equal_for_phi_arg_p (arg_true, smaller)
>            || (alt_smaller
> @@ -2006,7 +2008,8 @@ minmax_replacement (basic_block cond_bb, basic_block middle_bb, basic_block alt_
>
>        return true;
>      }
> -  else
> +  else if (!threeway_p
> +          || empty_block_p (alt_middle_bb))
>      {
>        /* Recognize the following case, assuming d <= u:
>
> @@ -2182,6 +2185,8 @@ minmax_replacement (basic_block cond_bb, basic_block middle_bb, basic_block alt_
>                                                           SSA_OP_DEF));
>        gsi_move_before (&gsi_from, &gsi);
>      }
> +  else
> +    return false;
>
>    /* Emit the statement to compute min/max.  */
>    gimple_seq stmts = NULL;
> --
> 2.31.1
>

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

* [PATCH] PHIOPT: Fix minmax_replacement for three way
@ 2023-09-23  0:54 Andrew Pinski
  2023-09-26 11:38 ` Richard Biener
  0 siblings, 1 reply; 4+ messages in thread
From: Andrew Pinski @ 2023-09-23  0:54 UTC (permalink / raw)
  To: gcc-patches; +Cc: Andrew Pinski

So when diamond bb support was added to minmax_replacement in r13-1950-g9bb19e143cfe,
the code was not expecting the alt_middle_bb not to exist if it was empty (for threeway_p).
So when factor_out_conditional_conversion was used to factor out conversions, it turns out
the assumption for alt_middle_bb to be wrong and we ended up with threeway_p being true but
having middle_bb being empty but alt_middle_bb not being empty which causes wrong code in
many cases.

This patch fixes the issue by adding a test for the 2 cases where the assumption on
threeway_p case having the other bb being empty.

Changes made:
v2: Fix test for `(a <= u) b = MAX(a, d) else b = u`.

Note my plan for GCC 15 is remove minmax_replacement as match.pd will catch all cases
at that point.

OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.

	PR tree-optimization/111469

gcc/ChangeLog:

	* tree-ssa-phiopt.cc (minmax_replacement): Fix
	the assumption for the `non-diamond` handling cases
	of diamond code.

gcc/testsuite/ChangeLog:

	* gcc.c-torture/execute/pr111469-1.c: New test.
---
 .../gcc.c-torture/execute/pr111469-1.c        | 38 +++++++++++++++++++
 gcc/tree-ssa-phiopt.cc                        |  9 ++++-
 2 files changed, 45 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/gcc.c-torture/execute/pr111469-1.c

diff --git a/gcc/testsuite/gcc.c-torture/execute/pr111469-1.c b/gcc/testsuite/gcc.c-torture/execute/pr111469-1.c
new file mode 100644
index 00000000000..b68d5989eac
--- /dev/null
+++ b/gcc/testsuite/gcc.c-torture/execute/pr111469-1.c
@@ -0,0 +1,38 @@
+/* PR tree-optimization/111469 */
+
+long f;
+char *g;
+__attribute__((noinline))
+char o() {
+  char l;
+  while (f)
+    ;
+  l = *g;
+  return l;
+}
+
+/* factor_out_conditional_conversion is able to remove the casts
+   from the 2 bbs (correctly)
+   but then minmax_replacement should not optimize this to a MIN_EXPR
+   as o has side effects. */
+
+__attribute__((noinline))
+unsigned short gg(unsigned short a, unsigned short b)
+{
+  short d;
+  if (a > b)
+  {
+    d= b;
+  }
+  else
+  {
+    o();
+    d = a;
+  }
+  return d;
+}
+
+int main(void)
+{
+  gg(3, 2);
+}
diff --git a/gcc/tree-ssa-phiopt.cc b/gcc/tree-ssa-phiopt.cc
index 3835d25d08c..312a6f9082b 100644
--- a/gcc/tree-ssa-phiopt.cc
+++ b/gcc/tree-ssa-phiopt.cc
@@ -1823,7 +1823,9 @@ minmax_replacement (basic_block cond_bb, basic_block middle_bb, basic_block alt_
       arg_false = arg0;
     }
 
-  if (empty_block_p (middle_bb))
+  if (empty_block_p (middle_bb)
+      && (!threeway_p
+	  || empty_block_p (alt_middle_bb)))
     {
       if ((operand_equal_for_phi_arg_p (arg_true, smaller)
 	   || (alt_smaller
@@ -2006,7 +2008,8 @@ minmax_replacement (basic_block cond_bb, basic_block middle_bb, basic_block alt_
 
       return true;
     }
-  else
+  else if (!threeway_p
+	   || empty_block_p (alt_middle_bb))
     {
       /* Recognize the following case, assuming d <= u:
 
@@ -2182,6 +2185,8 @@ minmax_replacement (basic_block cond_bb, basic_block middle_bb, basic_block alt_
 							  SSA_OP_DEF));
       gsi_move_before (&gsi_from, &gsi);
     }
+  else
+    return false;
 
   /* Emit the statement to compute min/max.  */
   gimple_seq stmts = NULL;
-- 
2.31.1


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

end of thread, other threads:[~2023-09-26 11:41 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-21  8:09 [PATCH] PHIOPT: Fix minmax_replacement for three way Andrew Pinski
2023-09-26 11:38 ` Richard Biener
2023-09-23  0:54 Andrew Pinski
2023-09-26 11:38 ` 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).