{
rtx call[2];
rtx insn;
+ rtx r12 = NULL_RTX;
+ rtx func_addr = func_desc;
gcc_assert (INTVAL (cookie) == 0);
if (global_tlsarg)
tlsarg = global_tlsarg;
+ /* For ELFv2, r12 and CTR need to hold the function address
+ for an indirect call. */
+ if (GET_CODE (func_desc) != SYMBOL_REF && DEFAULT_ABI == ABI_ELFv2)
+ {
+ r12 = gen_rtx_REG (Pmode, 12);
+ if (!rtx_equal_p (r12, func_desc))
+ emit_move_insn (r12, func_desc);
+ func_addr = gen_rtx_REG (Pmode, CTR_REGNO);
+ emit_move_insn (func_addr, r12);
+ }
+
/* Create the call. */
- call[0] = gen_rtx_CALL (VOIDmode, gen_rtx_MEM (SImode, func_desc), tlsarg);
+ call[0] = gen_rtx_CALL (VOIDmode, gen_rtx_MEM (SImode, func_addr), tlsarg);
if (value != NULL_RTX)
call[0] = gen_rtx_SET (value, call[0]);
if (!rs6000_pcrel_p (cfun))
use_reg (&CALL_INSN_FUNCTION_USAGE (insn),
gen_rtx_REG (Pmode, TOC_REGNUM));
+
+ /* Note use of r12. */
+ if (r12)
+ use_reg (&CALL_INSN_FUNCTION_USAGE (insn), r12);
}
/* Expand code to perform a call under the SYSV4 ABI. */
--- /dev/null
+/* { dg-do compile } */
+/* { dg-require-effective-target powerpc_elfv2 } */
+/* { dg-require-effective-target power10_ok } */
+/* { dg-options "-O2 -mdejagnu-cpu=power10" } */
+
+/* Verify that we generate an indirect sibcall for ELFv2 on P10 and
+ later, with r12 and CTR containing the function address. PR96787. */
+
+extern int f (int);
+
+int main ()
+{
+ if (f (3) != 6)
+ return 1;
+ return 0;
+}
+
+
+int g (int a)
+{
+ return a * 2;
+}
+
+
+int h (int a)
+{
+ return a + 2;
+}
+
+int __attribute__((__noinline__)) f (int a)
+{
+ int (*x) (int) = a % 2 ? &g : &h;
+ (*x) (a);
+}
+
+/* { dg-final { scan-assembler {\mmtctr 12\M} } } */
+/* { dg-final { scan-assembler {\mbctr\M} } } */
+/* { dg-final { scan-assembler-not {\mbctrl\M} } } */
--- /dev/null
+/* { dg-do run } */
+/* { dg-require-effective-target power10_hw } */
+/* { dg-options "-O2 -mdejagnu-cpu=power10" } */
+
+/* Verify that we generate an indirect sibcall for ELFv2 on P10 and
+ later, with r12 and CTR containing the function address. PR96787. */
+
+extern void abort (void);
+extern int f (int);
+
+int main ()
+{
+ if (f (3) != 6)
+ abort ();
+ return 0;
+}
+
+
+int g (int a)
+{
+ return a * 2;
+}
+
+
+int h (int a)
+{
+ return a + 2;
+}
+
+int __attribute__((__noinline__)) f (int a)
+{
+ int (*x) (int) = a % 2 ? &g : &h;
+ (*x) (a);
+}
+