Message ID | 20240429080931.32560-1-chenglulu@loongson.cn |
---|---|
State | New |
Headers | show |
Series | [gcc-12] LoongArch: Fix eh_return epilogue for normal returns. | expand |
Pushed to r12-10403. 在 2024/4/29 下午4:09, Lulu Cheng 写道: > From: Yang Yujie <yangyujie@loongson.cn> > > On LoongArch, the regitsters $r4 - $r7 (EH_RETURN_DATA_REGNO) will be saved > and restored in the function prologue and epilogue if the given function calls > __builtin_eh_return. This causes the return value to be overwritten on normal > return paths and breaks a rare case of libgcc's _Unwind_RaiseException. > > gcc/ChangeLog: > > PR target/114848 > * config/loongarch/loongarch.cc: Do not restore the saved eh_return > data registers ($r4-$r7) for a normal return of a function that calls > __builtin_eh_return elsewhere. > * config/loongarch/loongarch-protos.h: Same. > * config/loongarch/loongarch.md: Same. > > gcc/testsuite/ChangeLog: > > * gcc.target/loongarch/eh_return-normal-return.c: New test. > > (cherry picked from commit 4b421728289e6f1caa0dfaa953a11698ab95d37d) > --- > gcc/config/loongarch/loongarch-protos.h | 2 +- > gcc/config/loongarch/loongarch.cc | 35 ++++++++++++----- > gcc/config/loongarch/loongarch.md | 23 ++++++++++- > .../loongarch/eh_return-normal-return.c | 38 +++++++++++++++++++ > 4 files changed, 85 insertions(+), 13 deletions(-) > create mode 100644 gcc/testsuite/gcc.target/loongarch/eh_return-normal-return.c > > diff --git a/gcc/config/loongarch/loongarch-protos.h b/gcc/config/loongarch/loongarch-protos.h > index 2144c2421ed..8af82ffaa20 100644 > --- a/gcc/config/loongarch/loongarch-protos.h > +++ b/gcc/config/loongarch/loongarch-protos.h > @@ -47,7 +47,7 @@ enum loongarch_symbol_type { > extern rtx loongarch_emit_move (rtx, rtx); > extern HOST_WIDE_INT loongarch_initial_elimination_offset (int, int); > extern void loongarch_expand_prologue (void); > -extern void loongarch_expand_epilogue (bool); > +extern void loongarch_expand_epilogue (int); > extern bool loongarch_can_use_return_insn (void); > > extern bool loongarch_symbolic_constant_p (rtx, enum loongarch_symbol_type *); > diff --git a/gcc/config/loongarch/loongarch.cc b/gcc/config/loongarch/loongarch.cc > index 33b1919e7a3..a0e11f2fc66 100644 > --- a/gcc/config/loongarch/loongarch.cc > +++ b/gcc/config/loongarch/loongarch.cc > @@ -1008,7 +1008,8 @@ loongarch_save_restore_reg (machine_mode mode, int regno, HOST_WIDE_INT offset, > > static void > loongarch_for_each_saved_reg (HOST_WIDE_INT sp_offset, > - loongarch_save_restore_fn fn) > + loongarch_save_restore_fn fn, > + bool skip_eh_data_regs_p) > { > HOST_WIDE_INT offset; > > @@ -1017,7 +1018,15 @@ loongarch_for_each_saved_reg (HOST_WIDE_INT sp_offset, > for (int regno = GP_REG_FIRST; regno <= GP_REG_LAST; regno++) > if (BITSET_P (cfun->machine->frame.mask, regno - GP_REG_FIRST)) > { > - loongarch_save_restore_reg (word_mode, regno, offset, fn); > + /* Special care needs to be taken for $r4-$r7 (EH_RETURN_DATA_REGNO) > + when returning normally from a function that calls > + __builtin_eh_return. In this case, these registers are saved but > + should not be restored, or the return value may be clobbered. */ > + > + if (!(skip_eh_data_regs_p > + && GP_ARG_FIRST <= regno && regno < GP_ARG_FIRST + 4)) > + loongarch_save_restore_reg (word_mode, regno, offset, fn); > + > offset -= UNITS_PER_WORD; > } > > @@ -1289,7 +1298,7 @@ loongarch_expand_prologue (void) > GEN_INT (-step1)); > RTX_FRAME_RELATED_P (emit_insn (insn)) = 1; > size -= step1; > - loongarch_for_each_saved_reg (size, loongarch_save_reg); > + loongarch_for_each_saved_reg (size, loongarch_save_reg, false); > } > > > @@ -1336,11 +1345,13 @@ loongarch_can_use_return_insn (void) > return reload_completed && cfun->machine->frame.total_size == 0; > } > > -/* Expand an "epilogue" or "sibcall_epilogue" pattern; SIBCALL_P > - says which. */ > +/* Expand function epilogue using the following insn patterns: > + "epilogue" (style == NORMAL_RETURN) > + "sibcall_epilogue" (style == SIBCALL_RETURN) > + "eh_return" (style == EXCEPTION_RETURN) */ > > void > -loongarch_expand_epilogue (bool sibcall_p) > +loongarch_expand_epilogue (int style) > { > /* Split the frame into two. STEP1 is the amount of stack we should > deallocate before restoring the registers. STEP2 is the amount we > @@ -1357,7 +1368,8 @@ loongarch_expand_epilogue (bool sibcall_p) > bool need_barrier_p > = (get_frame_size () + cfun->machine->frame.arg_pointer_offset) != 0; > > - if (!sibcall_p && loongarch_can_use_return_insn ()) > + /* Handle simple returns. */ > + if (style == NORMAL_RETURN && loongarch_can_use_return_insn ()) > { > emit_jump_insn (gen_return ()); > return; > @@ -1433,7 +1445,9 @@ loongarch_expand_epilogue (bool sibcall_p) > > /* Restore the registers. */ > loongarch_for_each_saved_reg (frame->total_size - step2, > - loongarch_restore_reg); > + loongarch_restore_reg, > + crtl->calls_eh_return > + && style != EXCEPTION_RETURN); > > if (need_barrier_p) > loongarch_emit_stack_tie (); > @@ -1454,11 +1468,12 @@ loongarch_expand_epilogue (bool sibcall_p) > } > > /* Add in the __builtin_eh_return stack adjustment. */ > - if (crtl->calls_eh_return) > + if (crtl->calls_eh_return && style == EXCEPTION_RETURN) > emit_insn (gen_add3_insn (stack_pointer_rtx, stack_pointer_rtx, > EH_RETURN_STACKADJ_RTX)); > > - if (!sibcall_p) > + /* Emit return unless doing sibcall. */ > + if (style != SIBCALL_RETURN) > emit_jump_insn (gen_simple_return_internal (ra)); > } > > diff --git a/gcc/config/loongarch/loongarch.md b/gcc/config/loongarch/loongarch.md > index b49e058407c..1361e7ba7bf 100644 > --- a/gcc/config/loongarch/loongarch.md > +++ b/gcc/config/loongarch/loongarch.md > @@ -98,6 +98,11 @@ (define_constants > (T1_REGNUM 13) > (S0_REGNUM 23) > > + ;; Return path styles > + (NORMAL_RETURN 0) > + (SIBCALL_RETURN 1) > + (EXCEPTION_RETURN 2) > + > ;; PIC long branch sequences are never longer than 100 bytes. > (MAX_PIC_BRANCH_LENGTH 100) > ]) > @@ -2715,7 +2720,7 @@ (define_expand "epilogue" > [(const_int 2)] > "" > { > - loongarch_expand_epilogue (false); > + loongarch_expand_epilogue (NORMAL_RETURN); > DONE; > }) > > @@ -2723,7 +2728,7 @@ (define_expand "sibcall_epilogue" > [(const_int 2)] > "" > { > - loongarch_expand_epilogue (true); > + loongarch_expand_epilogue (SIBCALL_RETURN); > DONE; > }) > > @@ -2780,6 +2785,20 @@ (define_expand "eh_return" > emit_insn (gen_eh_set_ra_di (operands[0])); > else > emit_insn (gen_eh_set_ra_si (operands[0])); > + > + emit_jump_insn (gen_eh_return_internal ()); > + emit_barrier (); > + DONE; > +}) > + > +(define_insn_and_split "eh_return_internal" > + [(eh_return)] > + "" > + "#" > + "epilogue_completed" > + [(const_int 0)] > +{ > + loongarch_expand_epilogue (EXCEPTION_RETURN); > DONE; > }) > > diff --git a/gcc/testsuite/gcc.target/loongarch/eh_return-normal-return.c b/gcc/testsuite/gcc.target/loongarch/eh_return-normal-return.c > new file mode 100644 > index 00000000000..f8f3965f894 > --- /dev/null > +++ b/gcc/testsuite/gcc.target/loongarch/eh_return-normal-return.c > @@ -0,0 +1,38 @@ > +/* { dg-do run } */ > +/* { dg-options "-O2" } */ > + > +#include <stdlib.h> > + > +int foo () __attribute__((noinline)); > +int main (); > + > +int > +foo () { > + > + int t; > + > + /* prevent optimization using asm */ > + asm ("" : "=r" (t) : "0" (-1)); > + asm ("" : "=r" (t) : "0" (t ? 1 : 0)); > + > + if (t == 0) > + /* never reached */ > + __builtin_eh_return (0, __builtin_return_address (0)); > + > + else if (t == 1) > + /* return here */ > + return 202312; > + > + else > + /* never reached: prevent vrp optimization in main */ > + return 0; > +} > + > +int > +main () > +{ > + if (foo() == 202312) > + return 0; > + else > + abort (); > +}
diff --git a/gcc/config/loongarch/loongarch-protos.h b/gcc/config/loongarch/loongarch-protos.h index 2144c2421ed..8af82ffaa20 100644 --- a/gcc/config/loongarch/loongarch-protos.h +++ b/gcc/config/loongarch/loongarch-protos.h @@ -47,7 +47,7 @@ enum loongarch_symbol_type { extern rtx loongarch_emit_move (rtx, rtx); extern HOST_WIDE_INT loongarch_initial_elimination_offset (int, int); extern void loongarch_expand_prologue (void); -extern void loongarch_expand_epilogue (bool); +extern void loongarch_expand_epilogue (int); extern bool loongarch_can_use_return_insn (void); extern bool loongarch_symbolic_constant_p (rtx, enum loongarch_symbol_type *); diff --git a/gcc/config/loongarch/loongarch.cc b/gcc/config/loongarch/loongarch.cc index 33b1919e7a3..a0e11f2fc66 100644 --- a/gcc/config/loongarch/loongarch.cc +++ b/gcc/config/loongarch/loongarch.cc @@ -1008,7 +1008,8 @@ loongarch_save_restore_reg (machine_mode mode, int regno, HOST_WIDE_INT offset, static void loongarch_for_each_saved_reg (HOST_WIDE_INT sp_offset, - loongarch_save_restore_fn fn) + loongarch_save_restore_fn fn, + bool skip_eh_data_regs_p) { HOST_WIDE_INT offset; @@ -1017,7 +1018,15 @@ loongarch_for_each_saved_reg (HOST_WIDE_INT sp_offset, for (int regno = GP_REG_FIRST; regno <= GP_REG_LAST; regno++) if (BITSET_P (cfun->machine->frame.mask, regno - GP_REG_FIRST)) { - loongarch_save_restore_reg (word_mode, regno, offset, fn); + /* Special care needs to be taken for $r4-$r7 (EH_RETURN_DATA_REGNO) + when returning normally from a function that calls + __builtin_eh_return. In this case, these registers are saved but + should not be restored, or the return value may be clobbered. */ + + if (!(skip_eh_data_regs_p + && GP_ARG_FIRST <= regno && regno < GP_ARG_FIRST + 4)) + loongarch_save_restore_reg (word_mode, regno, offset, fn); + offset -= UNITS_PER_WORD; } @@ -1289,7 +1298,7 @@ loongarch_expand_prologue (void) GEN_INT (-step1)); RTX_FRAME_RELATED_P (emit_insn (insn)) = 1; size -= step1; - loongarch_for_each_saved_reg (size, loongarch_save_reg); + loongarch_for_each_saved_reg (size, loongarch_save_reg, false); } @@ -1336,11 +1345,13 @@ loongarch_can_use_return_insn (void) return reload_completed && cfun->machine->frame.total_size == 0; } -/* Expand an "epilogue" or "sibcall_epilogue" pattern; SIBCALL_P - says which. */ +/* Expand function epilogue using the following insn patterns: + "epilogue" (style == NORMAL_RETURN) + "sibcall_epilogue" (style == SIBCALL_RETURN) + "eh_return" (style == EXCEPTION_RETURN) */ void -loongarch_expand_epilogue (bool sibcall_p) +loongarch_expand_epilogue (int style) { /* Split the frame into two. STEP1 is the amount of stack we should deallocate before restoring the registers. STEP2 is the amount we @@ -1357,7 +1368,8 @@ loongarch_expand_epilogue (bool sibcall_p) bool need_barrier_p = (get_frame_size () + cfun->machine->frame.arg_pointer_offset) != 0; - if (!sibcall_p && loongarch_can_use_return_insn ()) + /* Handle simple returns. */ + if (style == NORMAL_RETURN && loongarch_can_use_return_insn ()) { emit_jump_insn (gen_return ()); return; @@ -1433,7 +1445,9 @@ loongarch_expand_epilogue (bool sibcall_p) /* Restore the registers. */ loongarch_for_each_saved_reg (frame->total_size - step2, - loongarch_restore_reg); + loongarch_restore_reg, + crtl->calls_eh_return + && style != EXCEPTION_RETURN); if (need_barrier_p) loongarch_emit_stack_tie (); @@ -1454,11 +1468,12 @@ loongarch_expand_epilogue (bool sibcall_p) } /* Add in the __builtin_eh_return stack adjustment. */ - if (crtl->calls_eh_return) + if (crtl->calls_eh_return && style == EXCEPTION_RETURN) emit_insn (gen_add3_insn (stack_pointer_rtx, stack_pointer_rtx, EH_RETURN_STACKADJ_RTX)); - if (!sibcall_p) + /* Emit return unless doing sibcall. */ + if (style != SIBCALL_RETURN) emit_jump_insn (gen_simple_return_internal (ra)); } diff --git a/gcc/config/loongarch/loongarch.md b/gcc/config/loongarch/loongarch.md index b49e058407c..1361e7ba7bf 100644 --- a/gcc/config/loongarch/loongarch.md +++ b/gcc/config/loongarch/loongarch.md @@ -98,6 +98,11 @@ (define_constants (T1_REGNUM 13) (S0_REGNUM 23) + ;; Return path styles + (NORMAL_RETURN 0) + (SIBCALL_RETURN 1) + (EXCEPTION_RETURN 2) + ;; PIC long branch sequences are never longer than 100 bytes. (MAX_PIC_BRANCH_LENGTH 100) ]) @@ -2715,7 +2720,7 @@ (define_expand "epilogue" [(const_int 2)] "" { - loongarch_expand_epilogue (false); + loongarch_expand_epilogue (NORMAL_RETURN); DONE; }) @@ -2723,7 +2728,7 @@ (define_expand "sibcall_epilogue" [(const_int 2)] "" { - loongarch_expand_epilogue (true); + loongarch_expand_epilogue (SIBCALL_RETURN); DONE; }) @@ -2780,6 +2785,20 @@ (define_expand "eh_return" emit_insn (gen_eh_set_ra_di (operands[0])); else emit_insn (gen_eh_set_ra_si (operands[0])); + + emit_jump_insn (gen_eh_return_internal ()); + emit_barrier (); + DONE; +}) + +(define_insn_and_split "eh_return_internal" + [(eh_return)] + "" + "#" + "epilogue_completed" + [(const_int 0)] +{ + loongarch_expand_epilogue (EXCEPTION_RETURN); DONE; }) diff --git a/gcc/testsuite/gcc.target/loongarch/eh_return-normal-return.c b/gcc/testsuite/gcc.target/loongarch/eh_return-normal-return.c new file mode 100644 index 00000000000..f8f3965f894 --- /dev/null +++ b/gcc/testsuite/gcc.target/loongarch/eh_return-normal-return.c @@ -0,0 +1,38 @@ +/* { dg-do run } */ +/* { dg-options "-O2" } */ + +#include <stdlib.h> + +int foo () __attribute__((noinline)); +int main (); + +int +foo () { + + int t; + + /* prevent optimization using asm */ + asm ("" : "=r" (t) : "0" (-1)); + asm ("" : "=r" (t) : "0" (t ? 1 : 0)); + + if (t == 0) + /* never reached */ + __builtin_eh_return (0, __builtin_return_address (0)); + + else if (t == 1) + /* return here */ + return 202312; + + else + /* never reached: prevent vrp optimization in main */ + return 0; +} + +int +main () +{ + if (foo() == 202312) + return 0; + else + abort (); +}
From: Yang Yujie <yangyujie@loongson.cn> On LoongArch, the regitsters $r4 - $r7 (EH_RETURN_DATA_REGNO) will be saved and restored in the function prologue and epilogue if the given function calls __builtin_eh_return. This causes the return value to be overwritten on normal return paths and breaks a rare case of libgcc's _Unwind_RaiseException. gcc/ChangeLog: PR target/114848 * config/loongarch/loongarch.cc: Do not restore the saved eh_return data registers ($r4-$r7) for a normal return of a function that calls __builtin_eh_return elsewhere. * config/loongarch/loongarch-protos.h: Same. * config/loongarch/loongarch.md: Same. gcc/testsuite/ChangeLog: * gcc.target/loongarch/eh_return-normal-return.c: New test. (cherry picked from commit 4b421728289e6f1caa0dfaa953a11698ab95d37d) --- gcc/config/loongarch/loongarch-protos.h | 2 +- gcc/config/loongarch/loongarch.cc | 35 ++++++++++++----- gcc/config/loongarch/loongarch.md | 23 ++++++++++- .../loongarch/eh_return-normal-return.c | 38 +++++++++++++++++++ 4 files changed, 85 insertions(+), 13 deletions(-) create mode 100644 gcc/testsuite/gcc.target/loongarch/eh_return-normal-return.c