https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103435

--- Comment #1 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Untested fix:

2021-11-26  Jakub Jelinek  <ja...@redhat.com>

        PR tree-optimization/103435
        * gimple-ssa-store-merging.c (find_bswap_or_nop_finalize): Avoid UB if
        n->range - rsize == 8, just clear both *cmpnop and *cmpxchg in that
        case.

--- gcc/gimple-ssa-store-merging.c.jj   2021-11-25 10:47:07.000000000 +0100
+++ gcc/gimple-ssa-store-merging.c      2021-11-26 10:54:11.959800560 +0100
@@ -871,12 +871,18 @@ find_bswap_or_nop_finalize (struct symbo
        {
          mask = ((uint64_t) 1 << (rsize * BITS_PER_MARKER)) - 1;
          *cmpxchg &= mask;
-         *cmpnop >>= (n->range - rsize) * BITS_PER_MARKER;
+         if (n->range - rsize == sizeof (int64_t))
+           *cmpnop = 0;
+         else
+           *cmpnop >>= (n->range - rsize) * BITS_PER_MARKER;
        }
       else
        {
          mask = ((uint64_t) 1 << (rsize * BITS_PER_MARKER)) - 1;
-         *cmpxchg >>= (n->range - rsize) * BITS_PER_MARKER;
+         if (n->range - rsize == sizeof (int64_t))
+           *cmpxchg = 0;
+         else
+           *cmpxchg >>= (n->range - rsize) * BITS_PER_MARKER;
          *cmpnop &= mask;
        }
       n->range = rsize;

Reply via email to