https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97417
--- Comment #40 from Jim Wilson <wilson at gcc dot gnu.org> --- If you look at riscv.opt you will see that the -mshorten-memrefs option sets the variable riscv_mshorten_memrefs. If you grep for that, you will see that it is used in riscv_address_cost in riscv.c. I believe it is this change to the address cost that is supposed to prevent the recombination back into addresses that don't fit in compressed instructions. So you need to look at why this works in the current code, but not with your zero/sign extend load patch. Maybe there is something about the rtx costs for a regular load versus a zero/sign extend load that causes the problem. In the combine dump where it says "original costs" and "replacement costs" that is where it is using rtx_cost and riscv_address_cost. The replacement cost should be more than the original cost to prevent the recombination. You should see that if you look at the combine dump for the unpatched compiler.