https://gcc.gnu.org/g:495c38ed4c31c3c1e41c651850408fb4f5ca5f9c
commit r17-5943-g495c38ed4c31c3c1e41c651850408fb4f5ca5f9c Author: Jeff Law <[email protected]> Date: Wed Nov 4 06:47:44 2026 -0700 [RISC-V] Reorder sequence for (plus (reg) (const_int)) for nontrivial constants Another LLM finding -- more fallout from trying to fix the dhrystone regression a couple weeks ago. We very much want to steer PLUS operations where one operand is a constant through synthesize_add. The dhrystone fix sent another class of those into synthesize_add and exposed that the code it generates is less than ideal. Basically we generate lui addi add When adding a large constant to a register. The lui+add will usually fuse, so it's often just 2c. When the result is used in a memory reference it's another cycle. lui+addi add memref It can be difficult for fold-mem-offsets to see through this in some cases. We can rewrite into lui add addi memref The addi and memref will trivially fold and the lui has no incoming dependencies. So practically it's going to be 2-3 cycles instead of 3 cycles. This saves about 4b instructions in x264 input #1, so north of 2%. Not bad. Of course to some degree it's just recovering a recent regression. So it's not clear if it's really a net new win or just recovering what we lost. Tested on riscv32-elf and riscv64-elf. Bootstrapped on the K3 and C920. K1 is in progress. Pushing to the trunk. gcc/ * config/riscv/riscv.cc (synthesize_add): Reorder instructions when we do not find a good synthesis. Diff: --- gcc/config/riscv/riscv.cc | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc index cd4b2bb76c2d..a2a6f3c223c5 100644 --- a/gcc/config/riscv/riscv.cc +++ b/gcc/config/riscv/riscv.cc @@ -16940,11 +16940,29 @@ synthesize_add (rtx operands[3]) } /* No add synthesis was found. Synthesize the constant into - a temporary and use that. */ + a temporary and use that. Reassociate so that the addi can + trivially merge into a subsequent memory reference. This will + spoil fusion, but the merge seems more valuable. */ if (can_create_pseudo_p ()) { - rtx x = force_reg (word_mode, operands[2]); - x = gen_rtx_PLUS (word_mode, operands[1], x); + /* The full offset. */ + HOST_WIDE_INT offset = INTVAL (operands[2]); + + /* Get the upper bits into a temporary register and add that + to operands[1], storing the result in another temporary. */ + rtx temp1 = gen_reg_rtx (word_mode); + rtx high = gen_int_mode (CONST_HIGH_PART (offset), word_mode); + high = riscv_force_temporary (temp1, high); + + /* Now add the temporary to operands[1], storing the result in + a new temporary. */ + rtx temp2 = gen_reg_rtx (word_mode); + rtx x = gen_rtx_PLUS (word_mode, operands[1], high); + emit_insn (gen_rtx_SET (temp2, x)); + + /* And finally add the low bits as the last step. This combines + trivially with memory references. */ + x = plus_constant (word_mode, temp2, CONST_LOW_PART (offset)); emit_insn (gen_rtx_SET (operands[0], x)); return true; }
