binutils Newspaper
JULY 29, 2026
x86 Proposed

x86: Optimize XCHG to MOV for same-register forms

Suggests using pseudo-prefix or directives to prevent XCHG to MOV optimization for same-register forms.

This discussion centers on preventing the x86 assembler from optimizing XCHG instructions with identical source and destination registers into MOV instructions. Such optimizations can interfere with certain toolchain analyses, like those performed by Valgrind, especially when inline assembly is used in headers. The proposer suggests using .nooptimize, .insn, or .byte directives to disable this specific optimization.

In the Thread 1 participant
  1. Jan Beulich proposer

    Suggests using the {nooptimize} pseudo-prefix, .insn, or .byte directives to prevent the XCHG to MOV optimization for same-register forms.

    “Use the {nooptimize} pseudo-prefix, or .insn, or (as suggested by Michael) .byte? (Since {nooptimize} has been around for quite a bit longer than .insn, .insn likely is the poorest of the possible options.)”

In Details

This RFC addresses an optimization in the x86 assembler (gas) where XCHG reg, reg is converted to MOV reg, reg. This optimization, while generally beneficial, can cause issues for tools like Valgrind which rely on the precise semantics of XCHG for register tracking. The proposed solution involves using assembler directives to explicitly disable this transformation for specific code sections.

Part of a Series

Filed Under: x86assembleroptimizationValgrind