AVR: Adding +/-1 to a lower reg doesn't need a scratch.
AVR target code generation for adding/subtracting 1 is optimized to avoid using a scratch register.
The GCC AVR backend has been optimized to eliminate the need for a scratch register when adding or subtracting 1 from lower-numbered registers. Previously, such operations required a temporary register, increasing register pressure. The new implementation uses sequences of sec (set carry flag) with adc (add with carry) or sbc (subtract with carry) instructions, which achieve the +/-1 operation without spilling to a scratch register, thus reducing register pressure while maintaining code size.
In Details
The avr_out_plus_1 function and associated match patterns (add<mode>3_clobber, add<mode>3, addpsi3, *addpsi3) are modified to avoid using a scratch register for adding/subtracting 1 to lower registers. This is achieved by using sec followed by adc __zero_reg__ or sbc __zero_reg__. A new constraint alternative Y01 Ym1 is added to the AVR instruction patterns, signifying these sequences that operate on lower registers without a scratch. This optimization reduces register pressure.
- AVR
- A family of microcontrollers manufactured by Atmel (now Microchip Technology), commonly used in embedded systems. GCC provides a backend for compiling code for AVR microcontrollers.
- scratch register
- A temporary register used by a program or compiler to hold intermediate values during computation. Using scratch registers efficiently is crucial for minimizing register pressure and optimizing performance.
- register pressure
- A metric indicating how many registers are actively used by the program at a given point. High register pressure can lead to more spills (storing register values to memory), negatively impacting performance.
- adc
- Add with Carry. An AVR assembly instruction that adds two operands along with the value of the carry flag.
- sbc
- Subtract with Carry. An AVR assembly instruction that subtracts one operand from another, taking into account the value of the carry flag.
- sec
- Set Carry Flag. An AVR assembly instruction that sets the carry flag to 1.