i386: Avoid xchg reg,reg for register swaps on Intel targets [PR128445]
Optimizes register swaps on modern Intel CPUs by using three moves instead of xchg.
This commit introduces a new tuning option for Intel targets to improve performance on CPUs since Sandy Bridge. It avoids using the xchg reg,reg instruction for register swaps, as newer Intel microarchitectures process this instruction inefficiently. Instead, the compiler now uses a three-move sequence through a scratch register, which prevents unnecessary delays caused by false dependencies.
In Details
This patch introduces X86_TUNE_AVOID_FALSE_DEP_FOR_XCHG to address a performance bottleneck on Intel CPUs (Sandy Bridge and later) where xchg reg,reg micro-ops cause both source registers to wait for each other, creating a false dependency. The new tuning option replaces xchg reg,reg with a peephole2 pattern that swaps registers using three mov instructions via a temporary register. This optimization is specifically enabled for Core and hybrid tunings.
- xchg reg,reg
- An assembly instruction that swaps the contents of two registers. On some CPU architectures, this operation can introduce performance penalties due to how the CPU decodes and executes micro-operations.
- micro-ops
- Small, elementary operations that complex CPU instructions are broken down into for execution. Efficiently decoding and executing these micro-ops is crucial for CPU performance.
- false dependency
- A situation in instruction-level parallelism where the processor incorrectly assumes an instruction depends on the result of a previous one, even though it doesn't. This can stall the pipeline.
- peephole2
- A compiler optimization phase that examines a small window (a 'peephole') of subsequent instructions to find opportunities for replacing them with more efficient sequences.