x86/APX: Optimize XOR with all-ones immediate
GAS replaces XOR instructions with an all-ones immediate operand with NOT on x86/APX.
The GNU Assembler (GAS) now optimizes XOR instructions on the x86/APX architecture when the immediate operand is all ones (equivalent to -1 or 0xFF for byte operations). This operation is identical to a bitwise NOT. The assembler replaces these XOR forms with the NOT instruction, which can offer better performance and encoding. The change handles various operand sizes and includes optimizations for legacy encodings where applicable.
In Details
This commit refactors GAS's optimization logic for x86/APX to identify and replace XOR operations with an all-ones immediate. Such an operation is semantically equivalent to a bitwise NOT on the destination operand. The assembler now translates patterns like XOR $~0, %reg or XOR $0xFF, %reg into the NOT instruction. This optimization is applied across different operand sizes (byte, word, long, quad) and considers both modern (e.g., VEX/EVEX) and legacy instruction encodings to maximize efficiency and code density.
- XOR
- A bitwise logical operation that outputs true (1) if and only if inputs differ. In programming, XORing a value with itself results in zero, and XORing with all ones inverts the bits.
- immediate operand
- A constant value that is part of an instruction.
- NOT
- A bitwise logical operation that inverts each bit of its operand. In two's complement arithmetic, NOT(x) is equivalent to -x - 1.
- x86/APX
- An extension to the x86 instruction set architecture, likely referring to Advanced Performance Extensions, which introduces new instructions and encodings for improved performance.
- GAS
- GNU Assembler, the assembler used by the GNU toolchain. It translates assembly language into machine code.