RISC-V: Enable SUB followed by SEQZ/SNEZ fusion
GCC can now fuse SUB instructions with SEQZ/SNEZ on RISC-V, potentially improving performance.
GCC has added support for macro-fusion of SUB/SUBW instructions followed by SEQZ (set if equal to zero) or SNEZ (set if not equal to zero) on RISC-V architectures. This optimization occurs when the SUB/SUBW instruction has the necessary producer-consumer relationship with the SEQZ/SNEZ instruction and their destination registers align correctly. This new fusion is disabled by default and is validated with a new test case.
In Details
This commit introduces the RISCV_FUSE_SUB_SEQZ macro-fusion pattern to GCC's RISC-V backend. It targets the specific instruction sequence of SUB or SUBW followed by SEQZ or SNEZ. The fusion is enabled if the SUB/SUBW instruction's result is a source for the SEQZ/SNEZ instruction, and if the destination registers meet the required criteria. This pattern allows for more efficient generation of code that involves subtraction and subsequent conditional checks for zero or non-zero results. The fusion is disabled by default.
- macro-fusion
- An optimization technique where the compiler combines two or more adjacent instructions into a single, more efficient instruction or sequence, provided specific conditions (like register dependencies) are met.
- SUB
- A RISC-V instruction that performs subtraction between two source registers, storing the result in a destination register.
- SUBW
- A RISC-V instruction that performs word-sized subtraction between two source registers.
- SEQZ
- Set if Equal to Zero, a RISC-V instruction that sets a destination register to 1 if the source register is zero, and 0 otherwise.
- SNEZ
- Set if Not Equal to Zero, a RISC-V instruction that sets a destination register to 1 if the source register is non-zero, and 0 otherwise.
- producer-consumer relationship
- A dependency where one instruction (producer) generates a result that is used as an input by another instruction (consumer).
- RISC-V
- An open-standard instruction set architecture (ISA) based on modularity, extensibility, and simplicity, gaining traction in embedded systems and high-performance computing.