Simplify (and (xor A 1) B) to (and not A B) for RISC-V
GCC simplifies a bitwise operation sequence to improve instruction generation for RISC-V.
This commit optimizes a specific bitwise operation pattern in GCC’s RTL (Register Transfer Language) to generate more efficient code for RISC-V processors. It transforms (and (xor A 1) B) into (and not A B) when B is a 0 or 1 value. This change is particularly beneficial for RISC-V’s limited instruction set, reducing instruction count by approximately 160 million in benchmarks like x264 by utilizing the andn instruction.
In Details
This change targets the simplify-rtx.cc file within GCC's RTL simplification passes. It addresses a specific pattern (and (xor A 1) B) where B is a constant 0 or 1. For targets like RISC-V with restricted instruction sets, this pattern can be inefficiently expanded. The patch rewrites it to (and not A B), which can be directly mapped to the ANDN instruction on RISC-V, avoiding a SEQ or XOR inversion. This optimization improves code density and performance for certain bit manipulation sequences.
- RTL
- Register Transfer Language. An intermediate representation used within GCC that describes operations on registers and memory, closer to machine code than GIMPLE but still abstract.
- RISC-V
- An open-standard instruction set architecture (ISA) based on Reduced Instruction Set Computer principles. It is designed to be simple, modular, and extensible, making it popular for embedded systems and custom hardware.
- andn
- The bitwise AND NOT instruction. It performs a bitwise AND operation between the first operand and the bitwise NOT of the second operand. This is a specialized instruction that can sometimes replace sequences of AND and XOR operations.
- GIMPLE
- GCC's internal three-address code representation. It's a higher-level intermediate representation than RTL, used for many optimization passes before being converted to RTL.