I was thinking does it possible to using peephole2 to optimize this case, but I realized their is several barrier, like stack tie and note...so it seems hard to just leverage peephole2.
And the patch is LGTM, only a few minor coding format issues, but you don't need to send new patch, I can fix those stuff when I push, and I would strongly suggest you setup git-format-patch, <gcc-src>/contrib has a clang format setting , that can release you from the boring coding format issues. # Copy to <gcc-src>/.clang-format, so that clang-format can found that automatically. $ cp contrib/clang-format .clang-format > @@ -5747,6 +5748,80 @@ riscv_adjust_libcall_cfi_epilogue () > return dwarf; > } > > +/* return true if popretz pattern can be matched. > + set (reg 10 a0) (const_int 0) > + use (reg 10 a0) > + NOTE_INSN_EPILOGUE_BEG */ > +static rtx_insn * > +riscv_zcmp_can_use_popretz(void) Need space between function name and (void) > +{ > + rtx_insn *insn = NULL, *use = NULL, *clear = NULL; > + > + /* sequence stack for NOTE_INSN_EPILOGUE_BEG*/ > + struct sequence_stack * outer_seq = get_current_sequence ()->next; > + if (!outer_seq) > + return NULL; > + insn = outer_seq->first; > + if(!insn || !NOTE_P (insn) || NOTE_KIND (insn) != NOTE_INSN_EPILOGUE_BEG) > + return NULL; > + > + /* sequence stack for the insn before NOTE_INSN_EPILOGUE_BEG*/ > + outer_seq = outer_seq->next; > + if (outer_seq) > + insn = outer_seq->last; > + > + /* skip notes */ > + while (insn && NOTE_P (insn)) > + { > + insn = PREV_INSN (insn); > + } > + use = insn; > + > + /* match use (reg 10 a0) */ > + if (use == NULL || !INSN_P (use) > + || GET_CODE (PATTERN (use)) != USE > + || !REG_P(XEXP(PATTERN (use), 0)) > + || REGNO(XEXP(PATTERN (use), 0)) != A0_REGNUM) > + return NULL; > + > + /* match set (reg 10 a0) (const_int 0 [0]) */ > + clear = PREV_INSN (use); > + if (clear != NULL && INSN_P (clear) > + && GET_CODE (PATTERN (clear)) == SET > + && REG_P (SET_DEST (PATTERN (clear))) > + && REGNO (SET_DEST (PATTERN (clear))) == A0_REGNUM > + && SET_SRC (PATTERN (clear)) == const0_rtx) > + return clear; > + > + return NULL; > +} > + > +static void > +riscv_gen_multi_pop_insn(bool use_multi_pop_normal, unsigned mask, > + unsigned multipop_size) Same issue here, need space between argument and function name.