https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128885
--- Comment #3 from GCC Commits <cvs-commit at gcc dot gnu.org> --- The master branch has been updated by Kyrylo Tkachov <[email protected]>: https://gcc.gnu.org/g:dea5f4b6aec73809732e5ef957692ec90dcc2e3b commit r17-6712-gdea5f4b6aec73809732e5ef957692ec90dcc2e3b Author: Kyrylo Tkachov <[email protected]> Date: Tue Nov 24 03:08:30 2026 -0800 simplify-rtx: Fix truncation of AND of LSHIFTRT for wide modes [PR128885] simplify_truncation turns (truncate:M (and:OM (lshiftrt:OM x C) C2)) into (and:M (lshiftrt:M (truncate:M x) C) C2) when ((GET_MODE_MASK (M) >> C) & C2) == ((GET_MODE_MASK (OM) >> C) & C2). GET_MODE_MASK is capped at HOST_BITS_PER_WIDE_INT, so for OM wider than a HOST_WIDE_INT (TImode on 64-bit hosts) both sides are always equal. With M = DImode, C = 8 and C2 = (1 << 57) - 1, bit 56 of the result comes from bit 64 of x, which the narrowed DImode shift fills with zero. On x86-64 the AND is dropped and only "shrq $8" is emitted for the high part of the operand of the multiplication: (subreg:DI (and:TI (lshiftrt:TI (reg:TI b) (const_int 8)) (const_int 0x1ffffffffffffff)) 0) -> (lshiftrt:DI (subreg:DI (reg:TI b) 0) (const_int 8)) Check directly that no bit of C2 lies outside the bits of M that the narrowed shift defines, and require M to be HWI computable so that GET_MODE_MASK (M) is exact. The mask is first restricted to OM, so for OM no wider than a HOST_WIDE_INT the sign extension of a CONST_INT mask is ignored as before. For wider OM a sign-extended negative mask keeps its high bits and is rejected. Bootstrapped and tested on aarch64-none-linux-gnu and x86_64-linux. gcc/ChangeLog: PR rtl-optimization/128885 * simplify-rtx.cc (simplify_context::simplify_truncation): Check the AND mask against the narrowed shift directly. gcc/testsuite/ChangeLog: PR rtl-optimization/128885 * gcc.dg/torture/pr128885.c: New test. Signed-off-by: Kyrylo Tkachov <[email protected]>
