diff mbox series

bitint: Handle m_bitfld_load cast in outer m_cast_conditional [PR114555]

Message ID Zg5W4t/VkV0F5Hvq@tucnak
State New
Headers show
Series bitint: Handle m_bitfld_load cast in outer m_cast_conditional [PR114555] | expand

Commit Message

Jakub Jelinek April 4, 2024, 7:29 a.m. UTC
Hi!

We ICE on the following testcase, because we use result of a PHI node
which is only conditional because of a m_cast_conditional on the outermost
loops PHI node argument and so is invalid SSA form.

The following patch fixes it like similar cases elsewhere by adding
needed intervening PHI(s).

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

2024-04-04  Jakub Jelinek  <jakub@redhat.com>

	PR tree-optimization/114555
	* gimple-lower-bitint.cc (bitint_large_huge::handle_cast): For
	m_bitfld_load and save_cast_conditional add any needed PHIs
	and adjust t4 accordingly.

	* gcc.dg/bitint-103.c: New test.
	* gcc.dg/bitint-104.c: New test.


	Jakub

Comments

Richard Biener April 4, 2024, 8:06 a.m. UTC | #1
On Thu, 4 Apr 2024, Jakub Jelinek wrote:

> Hi!
> 
> We ICE on the following testcase, because we use result of a PHI node
> which is only conditional because of a m_cast_conditional on the outermost
> loops PHI node argument and so is invalid SSA form.
> 
> The following patch fixes it like similar cases elsewhere by adding
> needed intervening PHI(s).
> 
> Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

OK.

Richard.

> 2024-04-04  Jakub Jelinek  <jakub@redhat.com>
> 
> 	PR tree-optimization/114555
> 	* gimple-lower-bitint.cc (bitint_large_huge::handle_cast): For
> 	m_bitfld_load and save_cast_conditional add any needed PHIs
> 	and adjust t4 accordingly.
> 
> 	* gcc.dg/bitint-103.c: New test.
> 	* gcc.dg/bitint-104.c: New test.
> 
> --- gcc/gimple-lower-bitint.cc.jj	2024-03-23 11:19:53.000000000 +0100
> +++ gcc/gimple-lower-bitint.cc	2024-04-03 15:31:19.686583203 +0200
> @@ -1506,7 +1506,7 @@ bitint_large_huge::handle_cast (tree lhs
>  	  if (m_bitfld_load)
>  	    {
>  	      tree t4;
> -	      if (!save_first)
> +	      if (!save_first && !save_cast_conditional)
>  		t4 = m_data[m_bitfld_load + 1];
>  	      else
>  		t4 = make_ssa_name (m_limb_type);
> @@ -1519,6 +1519,24 @@ bitint_large_huge::handle_cast (tree lhs
>  	      if (edge_true_true)
>  		add_phi_arg (phi, m_data[m_bitfld_load], edge_true_true,
>  			     UNKNOWN_LOCATION);
> +	      if (save_cast_conditional)
> +		for (basic_block bb = gsi_bb (m_gsi);;)
> +		  {
> +		    edge e1 = single_succ_edge (bb);
> +		    edge e2 = find_edge (e1->dest, m_bb), e3;
> +		    tree t5 = ((e2 && !save_first) ? m_data[m_bitfld_load + 1]
> +			       : make_ssa_name (m_limb_type));
> +		    phi = create_phi_node (t5, e1->dest);
> +		    edge_iterator ei;
> +		    FOR_EACH_EDGE (e3, ei, e1->dest->preds)
> +		      add_phi_arg (phi, (e3 == e1 ? t4
> +					 : build_zero_cst (m_limb_type)),
> +				   e3, UNKNOWN_LOCATION);
> +		    t4 = t5;
> +		    if (e2)
> +		      break;
> +		    bb = e1->dest;
> +		  }
>  	      m_data[m_bitfld_load] = t4;
>  	      m_data[m_bitfld_load + 2] = t4;
>  	      m_bitfld_load = 0;
> --- gcc/testsuite/gcc.dg/bitint-103.c.jj	2024-04-03 15:34:19.468113199 +0200
> +++ gcc/testsuite/gcc.dg/bitint-103.c	2024-04-03 15:34:05.805300917 +0200
> @@ -0,0 +1,16 @@
> +/* PR tree-optimization/114555 */
> +/* { dg-do compile { target bitint } } */
> +/* { dg-options "-std=c23 -O2" } */
> +
> +#if __BITINT_MAXWIDTH__ >= 1225
> +struct S { _BitInt(512) : 98; _BitInt(1225) b : 509; } s;
> +_BitInt(1225) a;
> +#endif
> +
> +void
> +foo (void)
> +{
> +#if __BITINT_MAXWIDTH__ >= 1225
> +  a ^= (unsigned _BitInt(1025)) s.b;
> +#endif
> +}
> --- gcc/testsuite/gcc.dg/bitint-104.c.jj	2024-04-03 15:36:59.385916107 +0200
> +++ gcc/testsuite/gcc.dg/bitint-104.c	2024-04-03 15:36:28.034346850 +0200
> @@ -0,0 +1,17 @@
> +/* PR tree-optimization/114555 */
> +/* { dg-do compile { target bitint } } */
> +/* { dg-options "-std=c23 -O -fno-tree-forwprop" } */
> +
> +#if __BITINT_MAXWIDTH__ >= 4139
> +struct S { _BitInt(31) : 6; _BitInt(513) b : 241; } s;
> +_BitInt(4139) a;
> +#endif
> +
> +void
> +foo (void)
> +{
> +#if __BITINT_MAXWIDTH__ >= 4139
> +  int i = 0;
> +  a -= s.b << i;
> +#endif
> +}
> 
> 	Jakub
> 
>
diff mbox series

Patch

--- gcc/gimple-lower-bitint.cc.jj	2024-03-23 11:19:53.000000000 +0100
+++ gcc/gimple-lower-bitint.cc	2024-04-03 15:31:19.686583203 +0200
@@ -1506,7 +1506,7 @@  bitint_large_huge::handle_cast (tree lhs
 	  if (m_bitfld_load)
 	    {
 	      tree t4;
-	      if (!save_first)
+	      if (!save_first && !save_cast_conditional)
 		t4 = m_data[m_bitfld_load + 1];
 	      else
 		t4 = make_ssa_name (m_limb_type);
@@ -1519,6 +1519,24 @@  bitint_large_huge::handle_cast (tree lhs
 	      if (edge_true_true)
 		add_phi_arg (phi, m_data[m_bitfld_load], edge_true_true,
 			     UNKNOWN_LOCATION);
+	      if (save_cast_conditional)
+		for (basic_block bb = gsi_bb (m_gsi);;)
+		  {
+		    edge e1 = single_succ_edge (bb);
+		    edge e2 = find_edge (e1->dest, m_bb), e3;
+		    tree t5 = ((e2 && !save_first) ? m_data[m_bitfld_load + 1]
+			       : make_ssa_name (m_limb_type));
+		    phi = create_phi_node (t5, e1->dest);
+		    edge_iterator ei;
+		    FOR_EACH_EDGE (e3, ei, e1->dest->preds)
+		      add_phi_arg (phi, (e3 == e1 ? t4
+					 : build_zero_cst (m_limb_type)),
+				   e3, UNKNOWN_LOCATION);
+		    t4 = t5;
+		    if (e2)
+		      break;
+		    bb = e1->dest;
+		  }
 	      m_data[m_bitfld_load] = t4;
 	      m_data[m_bitfld_load + 2] = t4;
 	      m_bitfld_load = 0;
--- gcc/testsuite/gcc.dg/bitint-103.c.jj	2024-04-03 15:34:19.468113199 +0200
+++ gcc/testsuite/gcc.dg/bitint-103.c	2024-04-03 15:34:05.805300917 +0200
@@ -0,0 +1,16 @@ 
+/* PR tree-optimization/114555 */
+/* { dg-do compile { target bitint } } */
+/* { dg-options "-std=c23 -O2" } */
+
+#if __BITINT_MAXWIDTH__ >= 1225
+struct S { _BitInt(512) : 98; _BitInt(1225) b : 509; } s;
+_BitInt(1225) a;
+#endif
+
+void
+foo (void)
+{
+#if __BITINT_MAXWIDTH__ >= 1225
+  a ^= (unsigned _BitInt(1025)) s.b;
+#endif
+}
--- gcc/testsuite/gcc.dg/bitint-104.c.jj	2024-04-03 15:36:59.385916107 +0200
+++ gcc/testsuite/gcc.dg/bitint-104.c	2024-04-03 15:36:28.034346850 +0200
@@ -0,0 +1,17 @@ 
+/* PR tree-optimization/114555 */
+/* { dg-do compile { target bitint } } */
+/* { dg-options "-std=c23 -O -fno-tree-forwprop" } */
+
+#if __BITINT_MAXWIDTH__ >= 4139
+struct S { _BitInt(31) : 6; _BitInt(513) b : 241; } s;
+_BitInt(4139) a;
+#endif
+
+void
+foo (void)
+{
+#if __BITINT_MAXWIDTH__ >= 4139
+  int i = 0;
+  a -= s.b << i;
+#endif
+}