diff mbox series

ivopts: Fix IP_END handling for asm goto [PR107997]

Message ID Y5RT+ZOKX3pHseJu@tucnak
State New
Headers show
Series ivopts: Fix IP_END handling for asm goto [PR107997] | expand

Commit Message

Jakub Jelinek Dec. 10, 2022, 9:40 a.m. UTC
Hi!

The following testcase ICEs, because the latch bb ends with
asm goto which has both fallthrough to the header and one or more labels
in the header too.  In that case there is just a single edge out of the
latch block, but still the asm goto is stmt_ends_bb_p statement, yet
ivopts decides to emit an IV bump at the IP_END position and inserts
it into the same bb as the asm goto after it, which then fails verification
(control flow in the middle of bb).

The following patch fixes it by splitting the latch -> header edge in that
case and inserting into the newly created bb, where split_edge ->
redirect_edge_and_branch is able to deal with this case correctly.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

2022-12-10  Jakub Jelinek  <jakub@redhat.com>

	PR tree-optimization/107997
	* tree-ssa-loop-ivopts.cc: Include cfganal.h.
	(create_new_iv) <case IP_END>: If ip_end_pos bb is non-empty and ends
	with a stmt which ends bb, instead of adding iv update after it split
	the latch edge and insert iterator into the new latch bb.

	* gcc.c-torture/compile/pr107997.c: New test.


	Jakub

Comments

Richard Biener Dec. 10, 2022, 11:56 a.m. UTC | #1
> Am 10.12.2022 um 10:40 schrieb Jakub Jelinek via Gcc-patches <gcc-patches@gcc.gnu.org>:
> 
> Hi!
> 
> The following testcase ICEs, because the latch bb ends with
> asm goto which has both fallthrough to the header and one or more labels
> in the header too.  In that case there is just a single edge out of the
> latch block, but still the asm goto is stmt_ends_bb_p statement, yet
> ivopts decides to emit an IV bump at the IP_END position and inserts
> it into the same bb as the asm goto after it, which then fails verification
> (control flow in the middle of bb).
> 
> The following patch fixes it by splitting the latch -> header edge in that
> case and inserting into the newly created bb, where split_edge ->
> redirect_edge_and_branch is able to deal with this case correctly.
> 
> Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

Ok,

Richard 


> 2022-12-10  Jakub Jelinek  <jakub@redhat.com>
> 
>    PR tree-optimization/107997
>    * tree-ssa-loop-ivopts.cc: Include cfganal.h.
>    (create_new_iv) <case IP_END>: If ip_end_pos bb is non-empty and ends
>    with a stmt which ends bb, instead of adding iv update after it split
>    the latch edge and insert iterator into the new latch bb.
> 
>    * gcc.c-torture/compile/pr107997.c: New test.
> 
> --- gcc/tree-ssa-loop-ivopts.cc.jj    2022-12-05 11:10:37.786670489 +0100
> +++ gcc/tree-ssa-loop-ivopts.cc    2022-12-09 14:42:24.722896379 +0100
> @@ -131,6 +131,7 @@ along with GCC; see the file COPYING3.
> #include "builtins.h"
> #include "tree-vectorizer.h"
> #include "dbgcnt.h"
> +#include "cfganal.h"
> 
> /* For lang_hooks.types.type_for_mode.  */
> #include "langhooks.h"
> @@ -7235,6 +7236,12 @@ create_new_iv (struct ivopts_data *data,
>     case IP_END:
>       incr_pos = gsi_last_bb (ip_end_pos (data->current_loop));
>       after = true;
> +      if (!gsi_end_p (incr_pos) && stmt_ends_bb_p (gsi_stmt (incr_pos)))
> +    {
> +      edge e = find_edge (gsi_bb (incr_pos), data->current_loop->header);
> +      incr_pos = gsi_after_labels (split_edge (e));
> +      after = false;
> +    }
>       break;
> 
>     case IP_AFTER_USE:
> --- gcc/testsuite/gcc.c-torture/compile/pr107997.c.jj    2022-12-09 14:45:29.458160841 +0100
> +++ gcc/testsuite/gcc.c-torture/compile/pr107997.c    2022-12-09 14:45:12.290415073 +0100
> @@ -0,0 +1,23 @@
> +/* PR tree-optimization/107997 */
> +
> +int a, b;
> +void bar (int);
> +int baz (void);
> +
> +void *
> +foo (int x, void *y)
> +{
> +  asm goto ("" : : "r" (x || !a) : : l);
> +l:
> +  if (y)
> +    return 0;
> +  bar (b ? b : x);
> +  while (x--)
> +    {
> +      if (!baz ())
> +    baz ();
> +      asm goto ("" : : : : l2);
> +    l2:;
> +    }
> +  return y;
> +}
> 
>    Jakub
>
diff mbox series

Patch

--- gcc/tree-ssa-loop-ivopts.cc.jj	2022-12-05 11:10:37.786670489 +0100
+++ gcc/tree-ssa-loop-ivopts.cc	2022-12-09 14:42:24.722896379 +0100
@@ -131,6 +131,7 @@  along with GCC; see the file COPYING3.
 #include "builtins.h"
 #include "tree-vectorizer.h"
 #include "dbgcnt.h"
+#include "cfganal.h"
 
 /* For lang_hooks.types.type_for_mode.  */
 #include "langhooks.h"
@@ -7235,6 +7236,12 @@  create_new_iv (struct ivopts_data *data,
     case IP_END:
       incr_pos = gsi_last_bb (ip_end_pos (data->current_loop));
       after = true;
+      if (!gsi_end_p (incr_pos) && stmt_ends_bb_p (gsi_stmt (incr_pos)))
+	{
+	  edge e = find_edge (gsi_bb (incr_pos), data->current_loop->header);
+	  incr_pos = gsi_after_labels (split_edge (e));
+	  after = false;
+	}
       break;
 
     case IP_AFTER_USE:
--- gcc/testsuite/gcc.c-torture/compile/pr107997.c.jj	2022-12-09 14:45:29.458160841 +0100
+++ gcc/testsuite/gcc.c-torture/compile/pr107997.c	2022-12-09 14:45:12.290415073 +0100
@@ -0,0 +1,23 @@ 
+/* PR tree-optimization/107997 */
+
+int a, b;
+void bar (int);
+int baz (void);
+
+void *
+foo (int x, void *y)
+{
+  asm goto ("" : : "r" (x || !a) : : l);
+l:
+  if (y)
+    return 0;
+  bar (b ? b : x);
+  while (x--)
+    {
+      if (!baz ())
+	baz ();
+      asm goto ("" : : : : l2);
+    l2:;
+    }
+  return y;
+}