Skip to main content

Module optimize

Module optimize 

Source
Expand description

Peephole optimizations for instruction encoding. Peephole optimizations for x86/x86-64 instructions.

These optimizations transform instructions into shorter or more efficient encodings without changing observable behavior. They are applied when OptLevel::Size is active (the default).

§Optimizations

  • Zero-idiom: mov reg, 0xor reg, reg (saves 3–5 bytes, faster on modern CPUs)
  • REX elimination: op r64, imm where imm fits i32 and result is zero-extended from 32-bit
  • NOP elimination: mov reg, reg with same register → nop or removed
  • Test conversion: and reg, regtest reg, reg (identical flags, does not write reg)
  • Short AL/AX/EAX/RAX forms: Uses accumulator-specific short encodings
  • Sign-extended immediate: mov r64, imm where imm fits i32 → mov r32, imm32

Functions§

optimize_instruction
Apply peephole optimizations to an instruction (mutates in place).