GCC Newspaper
JULY 29, 2026
Date
/
Architectures
Components
Topics
News & Policy
i386 Performance Win

i386: Add ROLW %r,8 alternative to bswaphi2 patterns

Improves 16-bit byte swap implementation on i386 by preferring ROLW over XCHGB.

The i386 backend now favors the ROLW %r,8 instruction for 16-bit byte swaps (bswaphi2), overing the XCHGB instruction. This change allows the compiler to use a wider range of registers and enables better instruction scheduling by leaving the choice to the preferred_for_* machinery. Additionally, it ensures that all 16-bit rotates by 8 bits are converted to the bswap pattern, which can utilize the MOVBE instruction when available.

In Details

The bswaphi2 (HImode) pattern in i386.md is updated to include ROLW reg,8 as a preferred alternative to XCHGB %rH,%rL. While XCHGB doesn't clobber flags, it's restricted to general-purpose registers %ax, %bx, %cx, %dx. ROLW can use any register but clobbers flags. Explicitly adding ROLW and associated attributes like preferred_for_size and preferred_for_speed allows the preferred_for_* machinery to make a better choice, making the peephole2 optimization for register allocation obsolete.

For Context
HImode
A GCC internal mode representing a 16-bit integer.
bswap
An instruction that reverses the byte order of a value. For a 16-bit value, it swaps the high and low bytes.
ROLW
Rotate Left Word: Rotates the bits of a 16-bit register to the left by a specified amount.
XCHGB
Exchange Bytes: An instruction that swaps the high and low bytes of a register (specifically used here for a 16-bit register).
preferred_for_*
Attributes in GCC's instruction description files that guide instruction selection based on preferences like speed or size.
Filed Under: i386optimizationinstruction selection