On Wed, 2023-11-15 at 04:42 +0800, Xi Ruoyao wrote: > > There seems a better solution as suggested by the GCC internal doc. > > Section 18.9.16 mentions -fipa-ra: > > > > -- Target Hook: bool TARGET_CALL_FUSAGE_CONTAINS_NON_CALLEE_CLOBBERS > > Set to true if each call that binds to a local definition > > explicitly clobbers or sets all non-fixed registers modified by > > performing the call. That is, by the call pattern itself, or by > > code that might be inserted by the linker (e.g. stubs, veneers, > > branch islands), but not including those modifiable by the callee. > > The affected registers may be mentioned explicitly in the call > > pattern, or included as clobbers in CALL_INSN_FUNCTION_USAGE. The > > default version of this hook is set to false. The purpose of this > > hook is to enable the fipa-ra optimization. > > > > So we can add t0 into CALL_INSN_FUNCTION_USAGE of the call insn, like: > > > > diff --git a/gcc/config/loongarch/loongarch.md > > b/gcc/config/loongarch/loongarch.md > > index ea36542b1c3..96e7e98e6b3 100644 > > --- a/gcc/config/loongarch/loongarch.md > > +++ b/gcc/config/loongarch/loongarch.md > > @@ -3274,7 +3274,14 @@ (define_expand "sibcall" > > XEXP (target, 1), > > operands[1])); > > else > > - emit_call_insn (gen_sibcall_internal (target, operands[1])); > > + { > > + rtx call = gen_sibcall_internal (target, operands[1]); > > + > > + if (TARGET_CMODEL_MEDIUM && !REG_P (target)) > > + clobber_reg (&CALL_INSN_FUNCTION_USAGE (call), > > + gen_rtx_REG (Pmode, T0_REGNUM)); > > + emit_call_insn (call); > > + } > > DONE; > > }) > > > > Likewise for sibcall_value. > > Nope. This does not work for > > __attribute__((noinline)) void q() { asm ("":::"memory"); } > __attribute__((noinline)) static void t() { q(); } > > int > main () > { > int x0 = 114514; > > asm("":"+r"(x0)); > t(); > if (x0 != 114514) > __builtin_trap (); > } > > Not sure why...
Alright, it should be diff --git a/gcc/config/loongarch/loongarch.md b/gcc/config/loongarch/loongarch.md index ea36542b1c3..75c595abad7 100644 --- a/gcc/config/loongarch/loongarch.md +++ b/gcc/config/loongarch/loongarch.md @@ -3274,7 +3274,13 @@ (define_expand "sibcall" XEXP (target, 1), operands[1])); else - emit_call_insn (gen_sibcall_internal (target, operands[1])); + { + rtx call = emit_call_insn (gen_sibcall_internal (target, operands[1])); + + if (TARGET_CMODEL_MEDIUM && !REG_P (target)) + clobber_reg (&CALL_INSN_FUNCTION_USAGE (call), + gen_rtx_REG (Pmode, T0_REGNUM)); + } DONE; }) instead. -- Xi Ruoyao <xry...@xry111.site> School of Aerospace Science and Technology, Xidian University