https://gcc.gnu.org/bugzilla/show_bug.cgi?id=94516
--- Comment #12 from Jakub Jelinek <jakub at gcc dot gnu.org> --- Wonder if we couldn't let postreload.c add REG_EQUAL notes when it replaces sp += CONST_INT with sp = reg, like: --- gcc/postreload.c.jj 2020-04-08 12:03:54.600398023 +0200 +++ gcc/postreload.c 2020-04-09 14:36:48.288632884 +0200 @@ -97,6 +97,16 @@ reload_cse_simplify (rtx_insn *insn, rtx if (NO_FUNCTION_CSE && CALL_P (insn)) return false; + /* Remember if this insn has been sp += const_int. */ + rtx sp_set = set_for_reg_notes (insn); + rtx sp_addend = NULL_RTX; + if (sp_set + && SET_DEST (sp_set) == stack_pointer_rtx + && GET_CODE (SET_SRC (sp_set)) == PLUS + && XEXP (SET_SRC (sp_set), 0) == stack_pointer_rtx + && CONST_INT_P (XEXP (SET_SRC (sp_set), 1))) + sp_addend = XEXP (SET_SRC (sp_set), 1); + if (GET_CODE (body) == SET) { int count = 0; @@ -180,6 +190,15 @@ reload_cse_simplify (rtx_insn *insn, rtx reload_cse_simplify_operands (insn, testreg); } + /* If sp += const_int insn is changed into sp = reg;, add REG_EQUAL + note so that the stack_adjustments pass can undo it if beneficial. */ + if (sp_addend + && SET_DEST (sp_set) == stack_pointer_rtx + && GET_CODE (SET_SRC (sp_set)) != PLUS) + set_dst_reg_note (insn, REG_EQUAL, + gen_rtx_PLUS (Pmode, stack_pointer_rtx, + sp_addend), stack_pointer_rtx); + done: return (EDGE_COUNT (insn_bb->succs) != insn_bb_succs); } and teach combine-stack-adj.c to deal with that. I guess the major problem is that e.g. on x86 sp += const_int has a CC clobber, but sp = reg doesn't. And post-RA not really sure how can one figure out what clobbers to readd and how to check whether it is safe (the reg is dead). Perhaps the clobbers could be taken from the other stack adjustment insn, but still we need to verify the register(s) aren't live there.