forwprop: Use tree_fits_shwi_p in check_ctz_array

When touching the function yesterday, I was surprised to see just
TREE_CODE (something) != INTEGER_CST checks followed by tree_to_shwi.
That would ICE if the INTEGER_CST doesn't fit.

I have actually not been able to reproduce an ICE for the elt case
as the caller gives up if the precision of the array type is larger than 32
bits (but I think it is still cleaner to use it), the idx case can actually
ICE e.g. on
static const unsigned long long magic = 0x03f08c5392f756cdULL;
static const char table[] = {
     0, [0x8000000000000000ULL] = 1, 12,  2, 13, 22, 17,  3, 14, 33, 23, 36, 18, 58, 28,  4,
    62, 15, 34, 26, 24, 48, 50, 37, 19, 55, 59, 52, 29, 44, 39,  5,
    63, 11, 21, 16, 32, 35, 57, 27, 61, 25, 47, 49, 54, 51, 43, 38,
    10, 20, 31, 56, 60, 46, 53, 42, 9, 30, 45, 41,  8, 40,  7,  6,
};

int ctz (unsigned long x)
{
  unsigned long lsb = x & -x;
  return table[(lsb * magic) >> 58];
}
~/src/gcc/obj20/gcc/cc1 -quiet /tmp/1.c -O2 -mbmi
/tmp/1.c:2:19: warning: size of ‘table’ 9223372036854775871 bytes exceeds maximum object size 9223372036854775807 [-Wlarger-than=]
    2 | static const char table[] = {
      |                   ^~~~~
during GIMPLE pass: forwprop
/tmp/1.c: In function ‘ctz’:
/tmp/1.c:13:1: internal compiler error: in tree_to_shwi, at tree.cc:6518
   13 | }
      | ^
0x2efa51f internal_error(char const*, ...)
        ../../gcc/diagnostic-global-context.cc:517
0xf3c617 fancy_abort(char const*, int, char const*)
        ../../gcc/diagnostic.cc:1722
0x949a36 tree_to_shwi(tree_node const*)
        ../../gcc/tree.cc:6518
0x949a36 tree_to_shwi(tree_node const*)
        ../../gcc/tree.cc:6516
0x18472d5 check_ctz_array
        ../../gcc/tree-ssa-forwprop.cc:2286
but given the 9223372036854775871 bytes long array
I'm not sure it is appropriate for the testsuite.

2025-01-03  Jakub Jelinek  <jakub@redhat.com>

	* tree-ssa-forwprop.cc (check_ctz_array): Use tree_fits_shwi_p instead
	of just TREE_CODE tests for INTEGER_CST.
This commit is contained in:
Jakub Jelinek 2025-01-03 17:55:04 +01:00 committed by Jakub Jelinek
parent 26ca00eeed
commit 514577c66b

View file

@ -2278,9 +2278,9 @@ check_ctz_array (tree ctor, unsigned HOST_WIDE_INT mulc,
FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), i, idx, elt)
{
if (TREE_CODE (idx) != INTEGER_CST)
if (!tree_fits_shwi_p (idx))
return false;
if (TREE_CODE (elt) != INTEGER_CST && TREE_CODE (elt) != RAW_DATA_CST)
if (!tree_fits_shwi_p (elt) && TREE_CODE (elt) != RAW_DATA_CST)
return false;
unsigned HOST_WIDE_INT index = tree_to_shwi (idx);