https://gcc.gnu.org/bugzilla/show_bug.cgi?id=129053
Bug ID: 129053
Summary: x86-64: Redundant instructions for masked 128-bit
shifts in loops
Product: gcc
Version: 16.2.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c
Assignee: unassigned at gcc dot gnu.org
Reporter: akr at fsij dot org
Target Milestone: ---
I would like to report a missed optimization on x86-64: 128-bit shifts in loops
retain redundant instructions for shift counts of 64 or greater, even though
the shift count is explicitly masked with 63. In the generated code, the mask
is outside the loop, but the redundant instructions remain inside it.
This issue is reproducible with x86-64 GCC 14.2.0 -O3 on Debian GNU/Linux 13
(trixie).
I also tested with Compiler Explorer for x86-64 GCC 16.2 at -O3 and x86-64 GCC
trunk at -O3 on 2026-11-26.
Compiler Explorer: https://godbolt.org/z/3x1r89daY
(The assembly code in this issue is copied from Compiler Explorer.)
Consider the following code:
typedef unsigned __int128 u128;
typedef unsigned long uint64_t; /* assume 64-bit */
/* OK: only two shift instructions are generated. */
u128 single(u128 x, unsigned n)
{
return x >> (n & 63);
}
/* Bad: redundant test and conditional moves inside the loop. */
void loop(u128 *a, const u128 *b,
unsigned long len, unsigned n)
{
for (unsigned long i = 0; i < len; i++)
a[i] = b[i] >> (n & 63);
}
The shift in single is efficiently implemented using two shift instructions,
without an explicit masking instruction:
shrd rax, rdi, cl
shr rdx, cl
However, in loop, the mask is hoisted out of the loop:
and ecx, 63
The following instruction sequence is generated inside the loop:
xor r10d, r10d ; redundant
shrd rsi, rdi, cl
shr rdi, cl
test cl, 64 ; redundant
cmovne rsi, rdi ; redundant
cmovne rdi, r10 ; redundant
Since the shift count has already been masked with 63 outside the loop, the
four instructions marked above are redundant.
Thus, explicitly masking the shift count works for an isolated 128-bit shift,
but the same source-level workaround becomes ineffective when the shift is
placed inside a loop with a loop-invariant shift count.
Practical example: multiple-precision integer right shift
I encountered this problem when implementing a right shift of a
multiple-precision integer represented as an array of 64-bit limbs, with the
least significant limb first.
A simplified example is:
/* Bad: redundant shr, test and cmovne inside the loop. */
void mp_rshift(uint64_t *a, const uint64_t *b,
unsigned long len, unsigned n)
{
for (unsigned long i = 0; i < len - 1; i++) {
u128 x = ((u128)b[i + 1] << 64) | b[i];
a[i] = (uint64_t)(x >> (n & 63));
}
a[len - 1] = b[len - 1] >> (n & 63);
}
Using a 128-bit integer is a convenient way to express the operation on two
adjacent limbs.
Ideally, the computation of each result limb inside the loop should require
only a single shrd instruction, apart from loads, stores, and loop control.
However, GCC generates the following instruction sequence inside the loop:
shrd r8, r9, cl
shr r9, cl ; redundant
test cl, 64 ; redundant
cmovne r8, r9 ; redundant
Again, the last three instructions are redundant because the shift count has
already been masked with 63 outside the loop.
Explicitly specifying the range of the shift count
Neither the assume attribute nor __builtin_unreachable eliminates the redundant
instructions in the following examples:
/* Bad: the assume attribute does not eliminate redundant instructions. */
void loop_assume(u128 *a, const u128 *b,
unsigned long len, unsigned n)
{
__attribute__((assume(n < 64)));
for (unsigned long i = 0; i < len; i++)
a[i] = b[i] >> n;
}
/* Bad: __builtin_unreachable does not eliminate redundant instructions. */
void loop_unreachable(u128 *a, const u128 *b,
unsigned long len, unsigned n)
{
if (n >= 64)
__builtin_unreachable();
for (unsigned long i = 0; i < len; i++)
a[i] = b[i] >> n;
}
Both functions generate the same instruction sequence for the shift operation
as loop:
xor r10d, r10d ; redundant
shrd rsi, rdi, cl
shr rdi, cl
test cl, 64 ; redundant
cmovne rsi, rdi ; redundant
cmovne rdi, r10 ; redundant
Why this matters
In multiple-precision arithmetic, the shift count is naturally loop-invariant,
so the redundant instructions are executed for every iteration. Neither
explicit masking nor range information supplied through the assume attribute or
__builtin_unreachable avoids them in these examples. Target-specific inline
assembly can avoid the redundant instructions, but requires replacing an
otherwise straightforward implementation with architecture-specific code.
Expected behavior
GCC should make use of the known range of the shift count even when the masking
operation has been hoisted out of the loop or when the range is specified
through the assume attribute or __builtin_unreachable.
The shift operation in loop, loop_assume, and loop_unreachable should require
only two instructions:
shrd rsi, rdi, cl
shr rdi, cl
The computation of each result limb in the loop of mp_rshift should require
only a single instruction:
shrd r8, r9, cl
For the masked examples, the explicit masking instruction outside the loop
could also be eliminated after the redundant shift-count handling has been
removed, since the 64-bit x86 shift instructions use only the low six bits of
the count.G