Compress RISC-V jr and jalr with zero offset
Binutils adds compressed instruction forms for RISC-V 'jr' and 'jalr' with explicit zero offsets.
This commit enhances the RISC-V assembler in Binutils by adding support for compressed instruction forms (C-extension) for jr and jalr instructions when they involve a zero offset. Previously, the assembler compressed these instructions only in their most basic forms. Now, sequences like jalr x0, 0(a2) will correctly map to the compressed c.jalr instruction, matching the behavior of LLVM. This improves code density by enabling shorter instruction encodings for common jump operations.
In Details
This commit updates the RISC-V assembler in Binutils (gas) to recognize and generate compressed instruction forms for jr and jalr when an explicit zero offset is provided. It introduces new operand types ('r' for ra and 'z' for x0) and updates riscv_ip in tc-riscv.c to handle these. Specifically, it adds Zca aliases for jalr and jr to match LLVM's behavior, ensuring that forms like jalr x0, 0(a2) or jalr ra, 0(a2) are compressed to c.jalr and c.jr respectively. This improves code density for jump instructions by leveraging the RISC-V C-extension.
- 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.
- jr
- Jump Register. An instruction that transfers control to a target address stored in a register.
- jalr
- Jump and Link Register. An instruction that jumps to a target address in a register and saves the return address in another register. It's commonly used for function calls and returns.
- C-extension
- The compressed instruction extension for RISC-V, which provides 16-bit instruction encodings for common 32-bit instructions, improving code density.