Blame SOURCES/gcc48-rh1537828-7.patch

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