diff --git a/gcc/testsuite/gcc.dg/pr90838-2.c b/gcc/testsuite/gcc.dg/pr90838-2.c new file mode 100644 index 00000000000..7ecbf1e4418 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr90838-2.c @@ -0,0 +1,39 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-forwprop2-details" } */ +/* { dg-additional-options "-mbmi" { target { { i?86-*-* x86_64-*-* } && { ! { ia32 } } } } } */ +/* { dg-additional-options "-march=rv64gc_zbb" { target { rv64 } } } */ +/* { dg-additional-options "-march=rv32gc_zbb" { target { rv32 } } } */ +/* { dg-require-effective-target int32plus } */ + +static const unsigned long long magic = 0x03f08c5392f756cdULL; + +static const char table[128] = { + 0, 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, + 1, 2, 3, 4, 5, 6, 7, 8, + 9, 10, 11, 12, 13, 14, 15, 16, + 17, 18, 19, 20, 21, 22, 23, 24, + 25, 26, 27, 28, 29, 30, 31, 32, + 33, 34, 35, 36, 37, 38, 39, 40, + 41, 42, 43, 44, 45, 46, 47, 48, + 49, 50, 51, 52, 53, 54, 55, 56, + 57, 58, 59, 60, 61, 62, 63, 64 +}; + +int ctz4 (unsigned long x) +{ + unsigned long lsb = x & -x; + return table[(lsb * magic) >> 58]; +} + +/* { dg-final { scan-tree-dump {= \.CTZ} "forwprop2" { target { { i?86-*-* x86_64-*-* } && { ! { ia32 } } } } } } */ +/* { dg-final { scan-tree-dump {= \.CTZ} "forwprop2" { target aarch64*-*-* } } } */ +/* { dg-final { scan-tree-dump {= \.CTZ} "forwprop2" { target { rv64 } } } } */ +/* { dg-final { scan-tree-dump {= \.CTZ} "forwprop2" { target { rv32 } } } } */ +/* { dg-final { scan-tree-dump {= \.CTZ} "forwprop2" { target { loongarch64*-*-* } } } } */ diff --git a/gcc/tree-ssa-forwprop.cc b/gcc/tree-ssa-forwprop.cc index 7f5872dae96..b35f845a42a 100644 --- a/gcc/tree-ssa-forwprop.cc +++ b/gcc/tree-ssa-forwprop.cc @@ -2269,7 +2269,7 @@ check_ctz_array (tree ctor, unsigned HOST_WIDE_INT mulc, HOST_WIDE_INT &zero_val, unsigned shift, unsigned bits) { tree elt, idx; - unsigned HOST_WIDE_INT i, mask; + unsigned HOST_WIDE_INT i, mask, raw_idx = 0; unsigned matched = 0; mask = ((HOST_WIDE_INT_1U << (bits - shift)) - 1) << shift; @@ -2278,13 +2278,34 @@ 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 || TREE_CODE (elt) != INTEGER_CST) + if (TREE_CODE (idx) != INTEGER_CST) return false; - if (i > bits * 2) + if (TREE_CODE (elt) != INTEGER_CST && TREE_CODE (elt) != RAW_DATA_CST) return false; unsigned HOST_WIDE_INT index = tree_to_shwi (idx); - HOST_WIDE_INT val = tree_to_shwi (elt); + HOST_WIDE_INT val; + + if (TREE_CODE (elt) == INTEGER_CST) + val = tree_to_shwi (elt); + else + { + if (raw_idx == (unsigned) RAW_DATA_LENGTH (elt)) + { + raw_idx = 0; + continue; + } + if (TYPE_UNSIGNED (TREE_TYPE (elt))) + val = RAW_DATA_UCHAR_ELT (elt, raw_idx); + else + val = RAW_DATA_SCHAR_ELT (elt, raw_idx); + index += raw_idx; + raw_idx++; + i--; + } + + if (index > bits * 2) + return false; if (index == 0) {