crc: Fix up ICE from optimize_crc_loop [PR120677]

The following testcase ICEs, because optimize_crc_loop inserts a call
statement before labels instead of after labels.

Fixed thusly (plus fixed other issues noticed around it).

2025-06-17  Jakub Jelinek  <jakub@redhat.com>

	PR tree-optimization/120677
	* gimple-crc-optimization.cc (crc_optimization::optimize_crc_loop):
	Insert before gsi_after_labels instead of gsi_start_bb.  Use
	gimple_bb (output_crc) instead of output_crc->bb.  Formatting fix.

	* gcc.c-torture/execute/pr120677.c: New test.
This commit is contained in:
Jakub Jelinek
2025-06-17 13:20:11 +02:00
committed by Jakub Jelinek
parent a63b97e918
commit 75ffef5c6f
2 changed files with 34 additions and 6 deletions

View File

@@ -1261,15 +1261,12 @@ crc_optimization::optimize_crc_loop (gphi *output_crc)
loc = EXPR_LOCATION (phi_result);
/* Add IFN call and write the return value in the phi_result. */
gcall *call
= gimple_build_call_internal (ifn, 3,
m_crc_arg,
m_data_arg,
polynomial_arg);
gcall *call = gimple_build_call_internal (ifn, 3, m_crc_arg, m_data_arg,
polynomial_arg);
gimple_call_set_lhs (call, phi_result);
gimple_set_location (call, loc);
gimple_stmt_iterator si = gsi_start_bb (output_crc->bb);
gimple_stmt_iterator si = gsi_after_labels (gimple_bb (output_crc));
gsi_insert_before (&si, call, GSI_SAME_STMT);
/* Remove phi statement, which was holding CRC result. */

View File

@@ -0,0 +1,31 @@
/* PR tree-optimization/120677 */
/* { dg-do run { target int32plus } } */
unsigned a;
int b, e;
int
foo (int d)
{
switch (d)
{
case 0:
case 2:
return 0;
default:
return 1;
}
}
int
main ()
{
for (b = 8; b; b--)
if (a & 1)
a = a >> 1 ^ 20000000;
else
a >>= 1;
e = foo (0);
if (e || a)
__builtin_abort ();
}