56d343
commit 4361c221ff4b53f585a2e8c0ba38956c8132609f
56d343
Author: hjl <hjl@138bc75d-0d04-0410-961f-82ee72b054a4>
56d343
Date:   Mon Feb 26 15:29:30 2018 +0000
56d343
56d343
    i386: Update -mfunction-return= for return with pop
56d343
    
56d343
    When -mfunction-return= is used, simple_return_pop_internal should pop
56d343
    return address into ECX register, adjust stack by bytes to pop from stack
56d343
    and jump to the return thunk via ECX register.
56d343
    
56d343
    Tested on i686 and x86-64.
56d343
    
56d343
            PR target/84530
56d343
            * config/i386/i386-protos.h (ix86_output_indirect_jmp): Remove
56d343
            the bool argument.
56d343
            (ix86_output_indirect_function_return): New prototype.
56d343
            (ix86_split_simple_return_pop_internal): Likewise.
56d343
            * config/i386/i386.c (indirect_return_via_cx): New.
56d343
            (indirect_return_via_cx_bnd): Likewise.
56d343
            (indirect_thunk_name): Handle return va CX_REG.
56d343
            (output_indirect_thunk_function): Create alias for
56d343
            __x86_return_thunk_[re]cx and __x86_return_thunk_[re]cx_bnd.
56d343
            (ix86_output_indirect_jmp): Remove the bool argument.
56d343
            (ix86_output_indirect_function_return): New function.
56d343
            (ix86_split_simple_return_pop_internal): Likewise.
56d343
            * config/i386/i386.md (*indirect_jump): Don't pass false
56d343
            to ix86_output_indirect_jmp.
56d343
            (*tablejump_1): Likewise.
56d343
            (simple_return_pop_internal): Change it to define_insn_and_split.
56d343
            Call ix86_split_simple_return_pop_internal to split it for
56d343
            -mfunction-return=.
56d343
            (simple_return_indirect_internal): Call
56d343
            ix86_output_indirect_function_return instead of
56d343
            ix86_output_indirect_jmp.
56d343
    
56d343
    gcc/testsuite/
56d343
    
56d343
            PR target/84530
56d343
            * gcc.target/i386/ret-thunk-22.c: New test.
56d343
            * gcc.target/i386/ret-thunk-23.c: Likewise.
56d343
            * gcc.target/i386/ret-thunk-24.c: Likewise.
56d343
            * gcc.target/i386/ret-thunk-25.c: Likewise.
56d343
            * gcc.target/i386/ret-thunk-26.c: Likewise.
56d343
    
56d343
    git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@257992 138bc75d-0d04-0410-961f-82ee72b054a4
56d343
56d343
diff --git a/gcc/config/i386/i386-protos.h b/gcc/config/i386/i386-protos.h
56d343
index 4e4b2100f79..394d4aebf96 100644
56d343
--- a/gcc/config/i386/i386-protos.h
56d343
+++ b/gcc/config/i386/i386-protos.h
56d343
@@ -306,8 +306,10 @@ extern enum attr_cpu ix86_schedule;
56d343
 #endif
56d343
 
56d343
 extern const char * ix86_output_call_insn (rtx insn, rtx call_op);
56d343
-extern const char * ix86_output_indirect_jmp (rtx call_op, bool ret_p);
56d343
+extern const char * ix86_output_indirect_jmp (rtx call_op);
56d343
 extern const char * ix86_output_function_return (bool long_p);
56d343
+extern const char * ix86_output_indirect_function_return (rtx ret_op);
56d343
+extern void ix86_split_simple_return_pop_internal (rtx);
56d343
 
56d343
 #ifdef RTX_CODE
56d343
 /* Target data for multipass lookahead scheduling.
56d343
diff --git a/gcc/config/i386/i386.c b/gcc/config/i386/i386.c
56d343
index c25d26ca826..a8238a001ee 100644
56d343
--- a/gcc/config/i386/i386.c
56d343
+++ b/gcc/config/i386/i386.c
56d343
@@ -8777,6 +8777,9 @@ static bool indirect_thunk_needed = false;
56d343
    by call and return thunks functions.  */
56d343
 static int indirect_thunks_used;
56d343
 
56d343
+/* True if return thunk function via CX is needed.  */
56d343
+static bool indirect_return_via_cx;
56d343
+
56d343
 #ifndef INDIRECT_LABEL
56d343
 # define INDIRECT_LABEL "LIND"
56d343
 #endif
56d343
@@ -8786,26 +8789,29 @@ static int indirect_thunks_used;
56d343
 static void
56d343
 indirect_thunk_name (char name[32], int regno, bool ret_p)
56d343
 {
56d343
-  if (regno >= 0 && ret_p)
56d343
+  if (regno != INVALID_REGNUM && regno != CX_REG && ret_p)
56d343
     gcc_unreachable ();
56d343
 
56d343
   if (USE_HIDDEN_LINKONCE)
56d343
     {
56d343
-      if (regno >= 0)
56d343
+      const char *prefix;
56d343
+
56d343
+      prefix = "";
56d343
+
56d343
+      const char *ret = ret_p ? "return" : "indirect";
56d343
+
56d343
+      if (regno != INVALID_REGNUM)
56d343
 	{
56d343
 	  const char *reg_prefix;
56d343
 	  if (LEGACY_INT_REGNO_P (regno))
56d343
 	    reg_prefix = TARGET_64BIT ? "r" : "e";
56d343
 	  else
56d343
 	    reg_prefix = "";
56d343
-	  sprintf (name, "__x86_indirect_thunk_%s%s",
56d343
-		   reg_prefix, reg_names[regno]);
56d343
+	  sprintf (name, "__x86_%s_thunk%s_%s%s",
56d343
+		   ret, prefix, reg_prefix, reg_names[regno]);
56d343
 	}
56d343
       else
56d343
-	{
56d343
-	  const char *ret = ret_p ? "return" : "indirect";
56d343
-	  sprintf (name, "__x86_%s_thunk", ret);
56d343
-	}
56d343
+	sprintf (name, "__x86_%s_thunk%s", ret, prefix);
56d343
     }
56d343
   else
56d343
     {
56d343
@@ -8947,9 +8953,18 @@ output_indirect_thunk_function (int regno)
56d343
 	ASM_OUTPUT_LABEL (asm_out_file, name);
56d343
       }
56d343
 
56d343
-  if (regno < 0)
56d343
+  /* Create alias for __x86_return_thunk or
56d343
+     __x86_return_thunk_ecx.  */
56d343
+  bool need_alias;
56d343
+  if (regno == INVALID_REGNUM)
56d343
+    need_alias = true;
56d343
+  else if (regno == CX_REG)
56d343
+    need_alias = indirect_return_via_cx;
56d343
+  else
56d343
+    need_alias = false;
56d343
+
56d343
+  if (need_alias)
56d343
     {
56d343
-      /* Create alias for __x86.return_thunk/__x86.return_thunk_bnd.  */
56d343
       char alias[32];
56d343
 
56d343
       indirect_thunk_name (alias, regno, true);
56d343
@@ -24704,21 +24719,21 @@ ix86_output_indirect_branch (rtx call_op, const char *xasm,
56d343
   else
56d343
     ix86_output_indirect_branch_via_push (call_op, xasm, sibcall_p);
56d343
 }
56d343
+
56d343
 /* Output indirect jump.  CALL_OP is the jump target.  Jump is a
56d343
    function return if RET_P is true.  */
56d343
 
56d343
 const char *
56d343
-ix86_output_indirect_jmp (rtx call_op, bool ret_p)
56d343
+ix86_output_indirect_jmp (rtx call_op)
56d343
 {
56d343
   if (cfun->machine->indirect_branch_type != indirect_branch_keep)
56d343
     {
56d343
       struct ix86_frame frame;
56d343
       ix86_compute_frame_layout (&frame);
56d343
 
56d343
-      /* We can't have red-zone if this isn't a function return since
56d343
-	 "call" in the indirect thunk pushes the return address onto
56d343
-	 stack, destroying red-zone.  */
56d343
-      if (!ret_p && frame.red_zone_size != 0)
56d343
+      /* We can't have red-zone since "call" in the indirect thunk
56d343
+	 pushes the return address onto the stack, destroying the red-zone.  */
56d343
+      if (frame.red_zone_size != 0)
56d343
 	gcc_unreachable ();
56d343
 
56d343
       ix86_output_indirect_branch (call_op, "%0", true);
56d343
@@ -24759,6 +24774,75 @@ ix86_output_function_return (bool long_p)
56d343
   return "rep%; ret";
56d343
 }
56d343
 
56d343
+/* Output indirect function return.  RET_OP is the function return
56d343
+   target.  */
56d343
+
56d343
+const char *
56d343
+ix86_output_indirect_function_return (rtx ret_op)
56d343
+{
56d343
+  if (cfun->machine->function_return_type != indirect_branch_keep)
56d343
+    {
56d343
+      char thunk_name[32];
56d343
+      enum indirect_thunk_prefix need_prefix
56d343
+	= indirect_thunk_need_prefix (current_output_insn);
56d343
+      unsigned int regno = REGNO (ret_op);
56d343
+      gcc_assert (regno == CX_REG);
56d343
+
56d343
+      if (cfun->machine->function_return_type
56d343
+	  != indirect_branch_thunk_inline)
56d343
+	{
56d343
+	  bool need_thunk = (cfun->machine->function_return_type
56d343
+			     == indirect_branch_thunk);
56d343
+	  indirect_thunk_name (thunk_name, regno, need_prefix, true);
56d343
+	  if (need_thunk)
56d343
+	    {
56d343
+	      indirect_return_via_cx = true;
56d343
+	      indirect_thunks_used |= 1 << CX_REG;
56d343
+	    }
56d343
+	  fprintf (asm_out_file, "\tjmp\t%s\n", thunk_name);
56d343
+	}
56d343
+      else
56d343
+	output_indirect_thunk (need_prefix, regno);
56d343
+
56d343
+      return "";
56d343
+    }
56d343
+  else
56d343
+    return "jmp\t%A0";
56d343
+}
56d343
+
56d343
+/* Split simple return with popping POPC bytes from stack to indirect
56d343
+   branch with stack adjustment .  */
56d343
+
56d343
+void
56d343
+ix86_split_simple_return_pop_internal (rtx popc)
56d343
+{
56d343
+  struct machine_function *m = cfun->machine;
56d343
+  rtx ecx = gen_rtx_REG (SImode, CX_REG);
56d343
+  rtx insn;
56d343
+
56d343
+  /* There is no "pascal" calling convention in any 64bit ABI.  */
56d343
+  gcc_assert (!TARGET_64BIT);
56d343
+
56d343
+  insn = emit_insn (gen_pop (ecx));
56d343
+  m->fs.cfa_offset -= UNITS_PER_WORD;
56d343
+  m->fs.sp_offset -= UNITS_PER_WORD;
56d343
+
56d343
+  rtx x = plus_constant (Pmode, stack_pointer_rtx, UNITS_PER_WORD);
56d343
+  x = gen_rtx_SET (VOIDmode, stack_pointer_rtx, x);
56d343
+  add_reg_note (insn, REG_CFA_ADJUST_CFA, x);
56d343
+  add_reg_note (insn, REG_CFA_REGISTER, gen_rtx_SET (VOIDmode, ecx, pc_rtx));
56d343
+  RTX_FRAME_RELATED_P (insn) = 1;
56d343
+
56d343
+  x = gen_rtx_PLUS (Pmode, stack_pointer_rtx, popc);
56d343
+  x = gen_rtx_SET (VOIDmode, stack_pointer_rtx, x);
56d343
+  insn = emit_insn (x);
56d343
+  add_reg_note (insn, REG_CFA_ADJUST_CFA, x);
56d343
+  RTX_FRAME_RELATED_P (insn) = 1;
56d343
+
56d343
+  /* Now return address is in ECX.  */
56d343
+  emit_jump_insn (gen_simple_return_indirect_internal (ecx));
56d343
+}
56d343
+
56d343
 /* Output the assembly for a call instruction.  */
56d343
 
56d343
 const char *
56d343
diff --git a/gcc/config/i386/i386.md b/gcc/config/i386/i386.md
56d343
index 228f8f6d77a..3320ec233d2 100644
56d343
--- a/gcc/config/i386/i386.md
56d343
+++ b/gcc/config/i386/i386.md
56d343
@@ -11282,7 +11282,7 @@
56d343
 (define_insn "*indirect_jump"
56d343
   [(set (pc) (match_operand:W 0 "indirect_branch_operand" "rw"))]
56d343
   ""
56d343
-  "* return ix86_output_indirect_jmp (operands[0], false);"
56d343
+  "* return ix86_output_indirect_jmp (operands[0]);"
56d343
   [(set (attr "type")
56d343
      (if_then_else (match_test "(cfun->machine->indirect_branch_type
56d343
 				 != indirect_branch_keep)")
56d343
@@ -11336,7 +11336,7 @@
56d343
   [(set (pc) (match_operand:W 0 "indirect_branch_operand" "rw"))
56d343
    (use (label_ref (match_operand 1)))]
56d343
   ""
56d343
-  "* return ix86_output_indirect_jmp (operands[0], false);"
56d343
+  "* return ix86_output_indirect_jmp (operands[0]);"
56d343
   [(set (attr "type")
56d343
      (if_then_else (match_test "(cfun->machine->indirect_branch_type
56d343
 				 != indirect_branch_keep)")
56d343
@@ -11769,11 +11769,14 @@
56d343
    (set_attr "prefix_rep" "1")
56d343
    (set_attr "modrm" "0")])
56d343
 
56d343
-(define_insn "simple_return_pop_internal"
56d343
+(define_insn_and_split "simple_return_pop_internal"
56d343
   [(simple_return)
56d343
    (use (match_operand:SI 0 "const_int_operand"))]
56d343
   "reload_completed"
56d343
   "ret\t%0"
56d343
+  "&& cfun->machine->function_return_type != indirect_branch_keep"
56d343
+  [(const_int 0)]
56d343
+  "ix86_split_simple_return_pop_internal (operands[0]); DONE;"
56d343
   [(set_attr "length" "3")
56d343
    (set_attr "atom_unit" "jeu")
56d343
    (set_attr "length_immediate" "2")
56d343
@@ -11783,7 +11786,7 @@
56d343
   [(simple_return)
56d343
    (use (match_operand:SI 0 "register_operand" "r"))]
56d343
   "reload_completed"
56d343
-  "* return ix86_output_indirect_jmp (operands[0], true);"
56d343
+  "* return ix86_output_indirect_function_return (operands[0]);"
56d343
   [(set (attr "type")
56d343
      (if_then_else (match_test "(cfun->machine->indirect_branch_type
56d343
 				 != indirect_branch_keep)")
56d343
diff --git a/gcc/testsuite/gcc.target/i386/ret-thunk-22.c b/gcc/testsuite/gcc.target/i386/ret-thunk-22.c
56d343
new file mode 100644
56d343
index 00000000000..89e086de97b
56d343
--- /dev/null
56d343
+++ b/gcc/testsuite/gcc.target/i386/ret-thunk-22.c
56d343
@@ -0,0 +1,15 @@
56d343
+/* PR target/r84530 */
56d343
+/* { dg-do compile { target ia32 } } */
56d343
+/* { dg-options "-O2 -mfunction-return=thunk" } */
56d343
+
56d343
+struct s { _Complex unsigned short x; };
56d343
+struct s gs = { 100 + 200i };
56d343
+struct s __attribute__((noinline)) foo (void) { return gs; }
56d343
+
56d343
+/* { dg-final { scan-assembler-times "popl\[\\t \]*%ecx" 1 } } */
56d343
+/* { dg-final { scan-assembler "lea\[l\]?\[\\t \]*4\\(%esp\\), %esp" } } */
56d343
+/* { dg-final { scan-assembler "jmp\[ \t\]*__x86_return_thunk_ecx" } } */
56d343
+/* { dg-final { scan-assembler "jmp\[ \t\]*\.LIND" } } */
56d343
+/* { dg-final { scan-assembler "call\[ \t\]*\.LIND" } } */
56d343
+/* { dg-final { scan-assembler {\tpause} } } */
56d343
+/* { dg-final { scan-assembler {\tlfence} } } */
56d343
diff --git a/gcc/testsuite/gcc.target/i386/ret-thunk-23.c b/gcc/testsuite/gcc.target/i386/ret-thunk-23.c
56d343
new file mode 100644
56d343
index 00000000000..43f0ccaa854
56d343
--- /dev/null
56d343
+++ b/gcc/testsuite/gcc.target/i386/ret-thunk-23.c
56d343
@@ -0,0 +1,15 @@
56d343
+/* PR target/r84530 */
56d343
+/* { dg-do compile { target ia32 } } */
56d343
+/* { dg-options "-O2 -mfunction-return=thunk-extern" } */
56d343
+
56d343
+struct s { _Complex unsigned short x; };
56d343
+struct s gs = { 100 + 200i };
56d343
+struct s __attribute__((noinline)) foo (void) { return gs; }
56d343
+
56d343
+/* { dg-final { scan-assembler-times "popl\[\\t \]*%ecx" 1 } } */
56d343
+/* { dg-final { scan-assembler "lea\[l\]?\[\\t \]*4\\(%esp\\), %esp" } } */
56d343
+/* { dg-final { scan-assembler "jmp\[ \t\]*__x86_return_thunk_ecx" } } */
56d343
+/* { dg-final { scan-assembler-not "jmp\[ \t\]*\.LIND" } } */
56d343
+/* { dg-final { scan-assembler-not "call\[ \t\]*\.LIND" } } */
56d343
+/* { dg-final { scan-assembler-not {\tpause} } } */
56d343
+/* { dg-final { scan-assembler-not {\tlfence} } } */
56d343
diff --git a/gcc/testsuite/gcc.target/i386/ret-thunk-24.c b/gcc/testsuite/gcc.target/i386/ret-thunk-24.c
56d343
new file mode 100644
56d343
index 00000000000..8729e35147e
56d343
--- /dev/null
56d343
+++ b/gcc/testsuite/gcc.target/i386/ret-thunk-24.c
56d343
@@ -0,0 +1,15 @@
56d343
+/* PR target/r84530 */
56d343
+/* { dg-do compile { target ia32 } } */
56d343
+/* { dg-options "-O2 -mfunction-return=thunk-inline" } */
56d343
+
56d343
+struct s { _Complex unsigned short x; };
56d343
+struct s gs = { 100 + 200i };
56d343
+struct s __attribute__((noinline)) foo (void) { return gs; }
56d343
+
56d343
+/* { dg-final { scan-assembler-times "popl\[\\t \]*%ecx" 1 } } */
56d343
+/* { dg-final { scan-assembler "lea\[l\]?\[\\t \]*4\\(%esp\\), %esp" } } */
56d343
+/* { dg-final { scan-assembler-not "jmp\[ \t\]*__x86_return_thunk_ecx" } } */
56d343
+/* { dg-final { scan-assembler "jmp\[ \t\]*\.LIND" } } */
56d343
+/* { dg-final { scan-assembler "call\[ \t\]*\.LIND" } } */
56d343
+/* { dg-final { scan-assembler {\tpause} } } */
56d343
+/* { dg-final { scan-assembler {\tlfence} } } */
56d343
diff --git a/gcc/testsuite/gcc.target/i386/ret-thunk-25.c b/gcc/testsuite/gcc.target/i386/ret-thunk-25.c
56d343
new file mode 100644
56d343
index 00000000000..f73553c9a9f
56d343
--- /dev/null
56d343
+++ b/gcc/testsuite/gcc.target/i386/ret-thunk-25.c
56d343
@@ -0,0 +1,14 @@
56d343
+/* PR target/r84530 */
56d343
+/* { dg-do compile { target ia32 } } */
56d343
+/* { dg-options "-O2 -mfunction-return=thunk -fno-pic" } */
56d343
+
56d343
+struct s { _Complex unsigned short x; };
56d343
+struct s gs = { 100 + 200i };
56d343
+struct s __attribute__((noinline)) foo (void) { return gs; }
56d343
+
56d343
+/* { dg-final { scan-assembler-times "popl\[\\t \]*%ecx" 1 } } */
56d343
+/* { dg-final { scan-assembler "lea\[l\]?\[\\t \]*4\\(%esp\\), %esp" } } */
56d343
+/* { dg-final { scan-assembler "jmp\[ \t\]*\.LIND" } } */
56d343
+/* { dg-final { scan-assembler "call\[ \t\]*\.LIND" } } */
56d343
+/* { dg-final { scan-assembler {\tpause} } } */
56d343
+/* { dg-final { scan-assembler {\tlfence} } } */
56d343
diff --git a/gcc/testsuite/gcc.target/i386/ret-thunk-26.c b/gcc/testsuite/gcc.target/i386/ret-thunk-26.c
56d343
new file mode 100644
56d343
index 00000000000..9144e988735
56d343
--- /dev/null
56d343
+++ b/gcc/testsuite/gcc.target/i386/ret-thunk-26.c
56d343
@@ -0,0 +1,40 @@
56d343
+/* PR target/r84530 */
56d343
+/* { dg-do run } */
56d343
+/* { dg-options "-Os -mfunction-return=thunk" } */
56d343
+
56d343
+struct S { int i; };
56d343
+__attribute__((const, noinline, noclone))
56d343
+struct S foo (int x)
56d343
+{
56d343
+  struct S s;
56d343
+  s.i = x;
56d343
+  return s;
56d343
+}
56d343
+
56d343
+int a[2048], b[2048], c[2048], d[2048];
56d343
+struct S e[2048];
56d343
+
56d343
+__attribute__((noinline, noclone)) void
56d343
+bar (void)
56d343
+{
56d343
+  int i;
56d343
+  for (i = 0; i < 1024; i++)
56d343
+    {
56d343
+      e[i] = foo (i);
56d343
+      a[i+2] = a[i] + a[i+1];
56d343
+      b[10] = b[10] + i;
56d343
+      c[i] = c[2047 - i];
56d343
+      d[i] = d[i + 1];
56d343
+    }
56d343
+}
56d343
+
56d343
+int
56d343
+main ()
56d343
+{
56d343
+  int i;
56d343
+  bar ();
56d343
+  for (i = 0; i < 1024; i++)
56d343
+    if (e[i].i != i)
56d343
+      __builtin_abort ();
56d343
+  return 0;
56d343
+}