Bootstrapped and tested on x86_64-unknown-linux-gnu.
Richard. 2020-01-09 Richard Biener <rguent...@suse.de> PR tree-optimization/93040 * gimple-ssa-store-merging.c (find_bswap_or_nop): Raise search limit. * gcc.dg/optimize-bswaphi-1.c: Amend. * gcc.dg/optimize-bswapsi-2.c: Likewise. Index: gcc/gimple-ssa-store-merging.c =================================================================== --- gcc/gimple-ssa-store-merging.c (revision 280006) +++ gcc/gimple-ssa-store-merging.c (working copy) @@ -848,11 +848,11 @@ find_bswap_or_nop (gimple *stmt, struct { /* The last parameter determines the depth search limit. It usually correlates directly to the number n of bytes to be touched. We - increase that number by log2(n) + 1 here in order to also + increase that number by 2 * (log2(n) + 1) here in order to also cover signed -> unsigned conversions of the src operand as can be seen in libgcc, and for initial shift/and operation of the src operand. */ int limit = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (gimple_expr_type (stmt))); - limit += 1 + (int) ceil_log2 ((unsigned HOST_WIDE_INT) limit); + limit += 2 * (1 + (int) ceil_log2 ((unsigned HOST_WIDE_INT) limit)); gimple *ins_stmt = find_bswap_or_nop_1 (stmt, n, limit); if (!ins_stmt) Index: gcc/testsuite/gcc.dg/optimize-bswaphi-1.c =================================================================== --- gcc/testsuite/gcc.dg/optimize-bswaphi-1.c (revision 280006) +++ gcc/testsuite/gcc.dg/optimize-bswaphi-1.c (working copy) @@ -54,5 +54,11 @@ swap16 (HItype in) | (((in >> 8) & 0xFF) << 0); } -/* { dg-final { scan-tree-dump-times "16 bit load in target endianness found at" 3 "bswap" } } */ +unsigned short +get_unaligned_16 (unsigned char *p) +{ + return p[0] | (p[1] << 8); +} + +/* { dg-final { scan-tree-dump-times "16 bit load in target endianness found at" 4 "bswap" } } */ /* { dg-final { scan-tree-dump-times "16 bit bswap implementation found at" 4 "bswap" } } */ Index: gcc/testsuite/gcc.dg/optimize-bswapsi-2.c =================================================================== --- gcc/testsuite/gcc.dg/optimize-bswapsi-2.c (revision 280006) +++ gcc/testsuite/gcc.dg/optimize-bswapsi-2.c (working copy) @@ -44,5 +44,16 @@ uint32_t read_be32_3 (unsigned char *dat | (*data << 24); } -/* { dg-final { scan-tree-dump-times "32 bit load in target endianness found at" 3 "bswap" } } */ +static inline unsigned short +get_unaligned_16 (unsigned char *p) +{ + return p[0] | (p[1] << 8); +} +unsigned int +get_unaligned_32 (unsigned char *p) +{ + return get_unaligned_16 (p) | (get_unaligned_16 (p + 2) << 16); +} + +/* { dg-final { scan-tree-dump-times "32 bit load in target endianness found at" 4 "bswap" } } */ /* { dg-final { scan-tree-dump-times "32 bit bswap implementation found at" 3 "bswap" } } */