Patchwork [MIPS,committed] Fix PR 57260 (sibling calls vs. lazy binding)

login
register
mail settings
Submitter Richard Sandiford
Date May 15, 2013, 7:17 p.m.
Message ID <87vc6kqchf.fsf@talisman.default>
Download mbox | patch
Permalink /patch/244152/
State New
Headers show

Comments

Richard Sandiford - May 15, 2013, 7:17 p.m.
The sibling call optimisation shouldn't prevent functions from being
lazily bound.  This was a problem for n32 and n64 PIC, because MIPS
lazy binding stubs require $gp to be valid on entry, and because $gp
is a call-saved register.  (o32 is OK because $gp is call-clobbered.)

I was going to remove the MIPS_CALL_SIBCALL check in:

  /* If we're generating PIC, and this call is to a global function,
     try to allow its address to be resolved lazily.  This isn't
     possible for sibcalls when $gp is call-saved because the value
     of $gp on entry to the stub would be our caller's gp, not ours.  */
  if (TARGET_EXPLICIT_RELOCS
      && !(type == MIPS_CALL_SIBCALL && TARGET_CALL_SAVED_GP)
      && mips_ok_for_lazy_binding_p (addr))
    {
      addr = mips_got_load (dest, addr, SYMBOL_GOTOFF_CALL);
      emit_insn (gen_rtx_SET (VOIDmode, dest, addr));
      return true;
    }

but it is also used for sibling-style calls in C++ thunks.  I think
it's valid to override lazy binding in that case, since vtables
prevent lazy binding anyway.

Tested on mips64-linux-gnu and applied.

Richard


gcc/
	PR target/57260
	* config/mips/mips.c (mips_function_ok_for_sibcall): Don't allow
	sibling calls to functions that would normally be lazily bound,
	unless $gp is call-clobbered.

gcc/testsuite/
	PR target/57260
	* gcc.target/mips/call-1.c: Restrict to o32.
	* gcc.target/mips/call-5.c, gcc.target/mips/call-6.c: New test.

Patch

Index: gcc/config/mips/mips.c
===================================================================
--- gcc/config/mips/mips.c	2013-05-14 19:25:06.061932211 +0100
+++ gcc/config/mips/mips.c	2013-05-14 20:21:56.647243683 +0100
@@ -6995,6 +6995,15 @@  mips_function_ok_for_sibcall (tree decl,
       && mips_call_may_need_jalx_p (decl))
     return false;
 
+  /* Sibling calls should not prevent lazy binding.  Lazy-binding stubs
+     require $gp to be valid on entry, so sibcalls can only use stubs
+     if $gp is call-clobbered.  */
+  if (decl
+      && TARGET_CALL_SAVED_GP
+      && !TARGET_ABICALLS_PIC0
+      && !targetm.binds_local_p (decl))
+    return false;
+
   /* Otherwise OK.  */
   return true;
 }
Index: gcc/testsuite/gcc.target/mips/call-1.c
===================================================================
--- gcc/testsuite/gcc.target/mips/call-1.c	2012-08-27 17:27:13.000000000 +0100
+++ gcc/testsuite/gcc.target/mips/call-1.c	2013-05-14 19:51:06.188573399 +0100
@@ -1,10 +1,12 @@ 
-/* { dg-options "-mrelax-pic-calls -mshared -foptimize-sibling-calls" } */
+/* { dg-options "-mrelax-pic-calls -mshared -foptimize-sibling-calls -mabi=32" } */
 /* { dg-skip-if "requires -foptimize-sibling-calls" { *-*-* } { "-O0" } { "" } } */
 /* { dg-final { scan-assembler "\\.reloc\t1f,R_MIPS_JALR,normal\n1:\tjalr\t" } } */
 /* { dg-final { scan-assembler "\\.reloc\t1f,R_MIPS_JALR,normal2\n1:\tjalr\t" } } */
 /* { dg-final { scan-assembler "\\.reloc\t1f,R_MIPS_JALR,staticfunc\n1:\tjalr\t" } } */
 /* { dg-final { scan-assembler "\\.reloc\t1f,R_MIPS_JALR,tail\n1:\tjr\t" } } */
 /* { dg-final { scan-assembler "\\.reloc\t1f,R_MIPS_JALR,tail2\n1:\tjr\t" } } */
+/* { dg-final { scan-assembler "\\.reloc\t1f,R_MIPS_JALR,tail3\n1:\tjr\t" } } */
+/* { dg-final { scan-assembler "\\.reloc\t1f,R_MIPS_JALR,tail4\n1:\tjr\t" } } */
 
 __attribute__ ((noinline)) static void staticfunc () { asm (""); }
 int normal ();
@@ -31,3 +33,17 @@  NOMIPS16 void g ()
 {
   tail2 ();
 }
+
+__attribute__ ((visibility ("hidden"))) void tail3 ();
+
+NOMIPS16 void j ()
+{
+  tail3 ();
+}
+
+__attribute__ ((noinline)) static void tail4 () { asm (""); }
+
+NOMIPS16 void k ()
+{
+  tail4 ();
+}
Index: gcc/testsuite/gcc.target/mips/call-5.c
===================================================================
--- /dev/null	2013-05-10 09:05:06.836922288 +0100
+++ gcc/testsuite/gcc.target/mips/call-5.c	2013-05-14 19:51:01.492525330 +0100
@@ -0,0 +1,51 @@ 
+/* Like call-1.c, but for n32.  We cannot use sibling calls for tail and tail2
+   in this case (PR target/57260).  */
+/* { dg-options "-mrelax-pic-calls -mshared -foptimize-sibling-calls -mabi=n32" } */
+/* { dg-skip-if "requires -foptimize-sibling-calls" { *-*-* } { "-O0" } { "" } } */
+/* { dg-final { scan-assembler "\\.reloc\t1f,R_MIPS_JALR,normal\n1:\tjalr\t" } } */
+/* { dg-final { scan-assembler "\\.reloc\t1f,R_MIPS_JALR,normal2\n1:\tjalr\t" } } */
+/* { dg-final { scan-assembler "\\.reloc\t1f,R_MIPS_JALR,staticfunc\n1:\tjalr\t" } } */
+/* { dg-final { scan-assembler "\\.reloc\t1f,R_MIPS_JALR,tail\n1:\tjalr\t" } } */
+/* { dg-final { scan-assembler "\\.reloc\t1f,R_MIPS_JALR,tail2\n1:\tjalr\t" } } */
+/* { dg-final { scan-assembler "\\.reloc\t1f,R_MIPS_JALR,tail3\n1:\tjr\t" } } */
+/* { dg-final { scan-assembler "\\.reloc\t1f,R_MIPS_JALR,tail4\n1:\tjr\t" } } */
+
+__attribute__ ((noinline)) static void staticfunc () { asm (""); }
+int normal ();
+void normal2 ();
+
+NOMIPS16 f (int *p)
+{
+  *p = normal ();
+  normal2 ();
+  staticfunc ();
+  return 1;
+}
+
+int tail ();
+
+NOMIPS16 h ()
+{
+  return tail ();
+}
+
+void tail2 ();
+
+NOMIPS16 void g ()
+{
+  tail2 ();
+}
+
+__attribute__ ((visibility ("hidden"))) void tail3 ();
+
+NOMIPS16 void j ()
+{
+  tail3 ();
+}
+
+__attribute__ ((noinline)) static void tail4 () { asm (""); }
+
+NOMIPS16 void k ()
+{
+  tail4 ();
+}
Index: gcc/testsuite/gcc.target/mips/call-6.c
===================================================================
--- /dev/null	2013-05-10 09:05:06.836922288 +0100
+++ gcc/testsuite/gcc.target/mips/call-6.c	2013-05-14 20:15:08.281988889 +0100
@@ -0,0 +1,50 @@ 
+/* Like call-5.c, but for n64.  */
+/* { dg-options "-mrelax-pic-calls -mshared -foptimize-sibling-calls -mabi=64" } */
+/* { dg-skip-if "requires -foptimize-sibling-calls" { *-*-* } { "-O0" } { "" } } */
+/* { dg-final { scan-assembler "\\.reloc\t1f,R_MIPS_JALR,normal\n1:\tjalr\t" } } */
+/* { dg-final { scan-assembler "\\.reloc\t1f,R_MIPS_JALR,normal2\n1:\tjalr\t" } } */
+/* { dg-final { scan-assembler "\\.reloc\t1f,R_MIPS_JALR,staticfunc\n1:\tjalr\t" } } */
+/* { dg-final { scan-assembler "\\.reloc\t1f,R_MIPS_JALR,tail\n1:\tjalr\t" } } */
+/* { dg-final { scan-assembler "\\.reloc\t1f,R_MIPS_JALR,tail2\n1:\tjalr\t" } } */
+/* { dg-final { scan-assembler "\\.reloc\t1f,R_MIPS_JALR,tail3\n1:\tjr\t" } } */
+/* { dg-final { scan-assembler "\\.reloc\t1f,R_MIPS_JALR,tail4\n1:\tjr\t" } } */
+
+__attribute__ ((noinline)) static void staticfunc () { asm (""); }
+int normal ();
+void normal2 ();
+
+NOMIPS16 f (int *p)
+{
+  *p = normal ();
+  normal2 ();
+  staticfunc ();
+  return 1;
+}
+
+int tail ();
+
+NOMIPS16 h ()
+{
+  return tail ();
+}
+
+void tail2 ();
+
+NOMIPS16 void g ()
+{
+  tail2 ();
+}
+
+__attribute__ ((visibility ("hidden"))) void tail3 ();
+
+NOMIPS16 void j ()
+{
+  tail3 ();
+}
+
+__attribute__ ((noinline)) static void tail4 () { asm (""); }
+
+NOMIPS16 void k ()
+{
+  tail4 ();
+}