lower-bitint, v2: Make temporarily wrong IL less wrong [PR112843]

Message ID ZW7Zs6KdXyx1k+K1@tucnak
State Unresolved
Headers
Series lower-bitint, v2: Make temporarily wrong IL less wrong [PR112843] |

Checks

Context Check Description
snail/gcc-patch-check warning Git am fail log

Commit Message

Jakub Jelinek Dec. 5, 2023, 8:05 a.m. UTC
  On Tue, Dec 05, 2023 at 08:21:15AM +0100, Jakub Jelinek wrote:
> The reason I tweaked the lhs first is that it then just uses gimple_op and
> iterates over all ops, if that is done before lhs it would need to special
> case which op to skip because it is lhs (I'm using gimple_get_lhs for the
> lhs, but this isn't done for GIMPLE_CALL nor GIMPLE_PHI, so GIMPLE_ASSIGN
> or say GIMPLE_GOTO etc. are the only options, so I could just start with
> op 1 rather than 0 for is_gimple_assign).

Here is a variant which adjusts the inputs first before set_lhs, then
addition of cast after it and finally update_stmt.

So far tested with
make check-gcc check-g++ -j32 -k GCC_TEST_RUN_EXPENSIVE=1 RUNTESTFLAGS="GCC_TEST_RUN_EXPENSIVE=1 dg.exp='*bitint* pr112673.c builtin-stdc-bit-*.c pr112566-2.c pr112511.c' dg-torture.exp=*bitint* dfp.exp=*bitint*"

2023-12-05  Jakub Jelinek  <jakub@redhat.com>

	PR tree-optimization/112843
	* gimple-lower-bitint.cc (gimple_lower_bitint): Change lhs of stmt
	to lhs2 before building and inserting lhs = (cast) lhs2; assignment.
	Adjust stmt operands before adjusting lhs.

	* gcc.dg/bitint-47.c: New test.



	Jakub
  

Patch

--- gcc/gimple-lower-bitint.cc.jj	2023-12-03 17:53:55.604855552 +0100
+++ gcc/gimple-lower-bitint.cc	2023-12-05 08:53:13.424763429 +0100
@@ -6329,27 +6329,9 @@  gimple_lower_bitint (void)
 	      tree type = NULL_TREE;
 	      /* Middle _BitInt(N) is rewritten to casts to INTEGER_TYPEs
 		 with the same precision and back.  */
-	      if (tree lhs = gimple_get_lhs (stmt))
-		if (TREE_CODE (TREE_TYPE (lhs)) == BITINT_TYPE
-		    && (bitint_precision_kind (TREE_TYPE (lhs))
-			== bitint_prec_middle))
-		  {
-		    int prec = TYPE_PRECISION (TREE_TYPE (lhs));
-		    int uns = TYPE_UNSIGNED (TREE_TYPE (lhs));
-		    type = build_nonstandard_integer_type (prec, uns);
-		    tree lhs2 = make_ssa_name (type);
-		    gimple *g = gimple_build_assign (lhs, NOP_EXPR, lhs2);
-		    if (stmt_ends_bb_p (stmt))
-		      {
-			edge e = find_fallthru_edge (gsi_bb (gsi)->succs);
-			gsi_insert_on_edge_immediate (e, g);
-		      }
-		    else
-		      gsi_insert_after (&gsi, g, GSI_SAME_STMT);
-		    gimple_set_lhs (stmt, lhs2);
-		  }
 	      unsigned int nops = gimple_num_ops (stmt);
-	      for (unsigned int i = 0; i < nops; ++i)
+	      for (unsigned int i = is_gimple_assign (stmt) ? 1 : 0;
+		   i < nops; ++i)
 		if (tree op = gimple_op (stmt, i))
 		  {
 		    tree nop = maybe_cast_middle_bitint (&gsi, op, type);
@@ -6376,6 +6358,25 @@  gimple_lower_bitint (void)
 						      type);
 		      }
 		  }
+	      if (tree lhs = gimple_get_lhs (stmt))
+		if (TREE_CODE (TREE_TYPE (lhs)) == BITINT_TYPE
+		    && (bitint_precision_kind (TREE_TYPE (lhs))
+			== bitint_prec_middle))
+		  {
+		    int prec = TYPE_PRECISION (TREE_TYPE (lhs));
+		    int uns = TYPE_UNSIGNED (TREE_TYPE (lhs));
+		    type = build_nonstandard_integer_type (prec, uns);
+		    tree lhs2 = make_ssa_name (type);
+		    gimple_set_lhs (stmt, lhs2);
+		    gimple *g = gimple_build_assign (lhs, NOP_EXPR, lhs2);
+		    if (stmt_ends_bb_p (stmt))
+		      {
+			edge e = find_fallthru_edge (gsi_bb (gsi)->succs);
+			gsi_insert_on_edge_immediate (e, g);
+		      }
+		    else
+		      gsi_insert_after (&gsi, g, GSI_SAME_STMT);
+		  }
 	      update_stmt (stmt);
 	      continue;
 	    }
--- gcc/testsuite/gcc.dg/bitint-47.c.jj	2023-12-04 14:53:19.784200724 +0100
+++ gcc/testsuite/gcc.dg/bitint-47.c	2023-12-04 14:42:07.251699994 +0100
@@ -0,0 +1,13 @@ 
+/* PR tree-optimization/112843 */
+/* { dg-do compile { target bitint } } */
+/* { dg-options "-O2" } */
+
+#if __BITINT_MAXWIDTH__ >= 256
+_BitInt (256)
+foo (_BitInt (128) x, _BitInt (256) y)
+{
+  return x * 5 * y;
+}
+#else
+int x;
+#endif