Blame SOURCES/gcc48-rh1537828-7.patch

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