https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97417
--- Comment #42 from Jim Wilson <wilson at gcc dot gnu.org> --- riscv_address_cost is a hook, so it is targetm.address_cost which is only called from address_cost which is only called in a few places one of which is in postreload.c so that is the one I would look at first. This is in try_replace_in_use which is called from reload_combine_recognize_const_pattern which is trying to put offsets back into mems which is exactly what we don't want here. This suggests that containing_mem isn't getting set when we have a sign/zero extend. It should get set in reload_combine_note_use.