Blame SOURCES/gcc48-rh1537828-7.patch

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