b89248
commit 14041afe24556efd5845564aa183b6451fd9d6cc
b89248
Author: law <law@138bc75d-0d04-0410-961f-82ee72b054a4>
b89248
Date:   Thu Feb 1 16:22:56 2018 +0000
b89248
b89248
            PR target/84128
b89248
            * config/i386/i386.c (release_scratch_register_on_entry): Add new
b89248
            OFFSET and RELEASE_VIA_POP arguments.  Use SP+OFFSET to restore
b89248
            the scratch if RELEASE_VIA_POP is false.
b89248
            (ix86_adjust_stack_and_probe_stack_clash): Un-constify SIZE.
b89248
            If we have to save a temporary register, decrement SIZE appropriately.
b89248
            Pass new arguments to release_scratch_register_on_entry.
b89248
            (ix86_adjust_stack_and_probe): Likewise.
b89248
            (ix86_emit_probe_stack_range): Pass new arguments to
b89248
            release_scratch_register_on_entry.
b89248
b89248
            PR target/84128
b89248
            * gcc.target/i386/pr84128.c: New test.
b89248
b89248
diff --git a/gcc/config/i386/i386.c b/gcc/config/i386/i386.c
b89248
index 2fe2a0c..c25d26c 100644
b89248
--- a/gcc/config/i386/i386.c
b89248
+++ b/gcc/config/i386/i386.c
b89248
@@ -10182,22 +10182,39 @@ get_scratch_register_on_entry (struct scratch_reg *sr)
b89248
     }
b89248
 }
b89248
 
b89248
-/* Release a scratch register obtained from the preceding function.  */
b89248
+/* Release a scratch register obtained from the preceding function.
b89248
+
b89248
+   If RELEASE_VIA_POP is true, we just pop the register off the stack
b89248
+   to release it.  This is what non-Linux systems use with -fstack-check.
b89248
+
b89248
+   Otherwise we use OFFSET to locate the saved register and the
b89248
+   allocated stack space becomes part of the local frame and is
b89248
+   deallcated by the epilogue.  */
b89248
 
b89248
 static void
b89248
-release_scratch_register_on_entry (struct scratch_reg *sr)
b89248
+release_scratch_register_on_entry (struct scratch_reg *sr, HOST_WIDE_INT offset,
b89248
+				   bool release_via_pop)
b89248
 {
b89248
   if (sr->saved)
b89248
     {
b89248
-      struct machine_function *m = cfun->machine;
b89248
-      rtx x, insn = emit_insn (gen_pop (sr->reg));
b89248
+      if (release_via_pop)
b89248
+	{
b89248
+	  struct machine_function *m = cfun->machine;
b89248
+	  rtx x, insn = emit_insn (gen_pop (sr->reg));
b89248
 
b89248
-      /* The RTX_FRAME_RELATED_P mechanism doesn't know about pop.  */
b89248
-      RTX_FRAME_RELATED_P (insn) = 1;
b89248
-      x = gen_rtx_PLUS (Pmode, stack_pointer_rtx, GEN_INT (UNITS_PER_WORD));
b89248
-      x = gen_rtx_SET (VOIDmode, stack_pointer_rtx, x);
b89248
-      add_reg_note (insn, REG_FRAME_RELATED_EXPR, x);
b89248
-      m->fs.sp_offset -= UNITS_PER_WORD;
b89248
+	  /* The RTX FRAME_RELATED_P mechanism doesn't know about pop.  */
b89248
+	  RTX_FRAME_RELATED_P (insn) = 1;
b89248
+	  x = gen_rtx_PLUS (Pmode, stack_pointer_rtx, GEN_INT (UNITS_PER_WORD));
b89248
+	  x = gen_rtx_SET (VOIDmode, stack_pointer_rtx, x);
b89248
+	  add_reg_note (insn, REG_FRAME_RELATED_EXPR, x);
b89248
+	  m->fs.sp_offset -= UNITS_PER_WORD;
b89248
+	}
b89248
+      else
b89248
+	{
b89248
+	  rtx x = gen_rtx_PLUS (Pmode, stack_pointer_rtx, GEN_INT (offset));
b89248
+	  x = gen_rtx_SET (VOIDmode, sr->reg, gen_rtx_MEM (word_mode, x));
b89248
+	  emit_insn (x);
b89248
+	}
b89248
     }
b89248
 }
b89248
 
b89248
@@ -10212,7 +10229,7 @@ release_scratch_register_on_entry (struct scratch_reg *sr)
b89248
    pushed on the stack.  */
b89248
 
b89248
 static void
b89248
-ix86_adjust_stack_and_probe_stack_clash (const HOST_WIDE_INT size,
b89248
+ix86_adjust_stack_and_probe_stack_clash (HOST_WIDE_INT size,
b89248
 					 const bool int_registers_saved)
b89248
 {
b89248
   struct machine_function *m = cfun->machine;
b89248
@@ -10331,6 +10348,12 @@ ix86_adjust_stack_and_probe_stack_clash (const HOST_WIDE_INT size,
b89248
       struct scratch_reg sr;
b89248
       get_scratch_register_on_entry (&sr);
b89248
 
b89248
+      /* If we needed to save a register, then account for any space
b89248
+	 that was pushed (we are not going to pop the register when
b89248
+	 we do the restore).  */
b89248
+      if (sr.saved)
b89248
+	size -= UNITS_PER_WORD;
b89248
+
b89248
       /* Step 1: round SIZE down to a multiple of the interval.  */
b89248
       HOST_WIDE_INT rounded_size = size & -probe_interval;
b89248
 
b89248
@@ -10379,7 +10402,9 @@ ix86_adjust_stack_and_probe_stack_clash (const HOST_WIDE_INT size,
b89248
 				   m->fs.cfa_reg == stack_pointer_rtx);
b89248
       dump_stack_clash_frame_info (PROBE_LOOP, size != rounded_size);
b89248
 
b89248
-      release_scratch_register_on_entry (&sr);
b89248
+      /* This does not deallocate the space reserved for the scratch
b89248
+	 register.  That will be deallocated in the epilogue.  */
b89248
+      release_scratch_register_on_entry (&sr, size, false);
b89248
     }
b89248
 
b89248
   /* Make sure nothing is scheduled before we are done.  */
b89248
@@ -10392,7 +10417,7 @@ ix86_adjust_stack_and_probe_stack_clash (const HOST_WIDE_INT size,
b89248
    pushed on the stack.  */
b89248
 
b89248
 static void
b89248
-ix86_adjust_stack_and_probe (const HOST_WIDE_INT size,
b89248
+ix86_adjust_stack_and_probe (HOST_WIDE_INT size,
b89248
 			     const bool int_registers_saved)
b89248
 {
b89248
   /* We skip the probe for the first interval + a small dope of 4 words and
b89248
@@ -10465,6 +10490,11 @@ ix86_adjust_stack_and_probe (const HOST_WIDE_INT size,
b89248
 
b89248
       get_scratch_register_on_entry (&sr);
b89248
 
b89248
+      /* If we needed to save a register, then account for any space
b89248
+	 that was pushed (we are not going to pop the register when
b89248
+	 we do the restore).  */
b89248
+      if (sr.saved)
b89248
+	size -= UNITS_PER_WORD;
b89248
 
b89248
       /* Step 1: round SIZE to the previous multiple of the interval.  */
b89248
 
b89248
@@ -10516,7 +10546,9 @@ ix86_adjust_stack_and_probe (const HOST_WIDE_INT size,
b89248
 						    (get_probe_interval ()
b89248
 						     + dope))));
b89248
 
b89248
-      release_scratch_register_on_entry (&sr);
b89248
+      /* This does not deallocate the space reserved for the scratch
b89248
+	 register.  That will be deallocated in the epilogue.  */
b89248
+      release_scratch_register_on_entry (&sr, size, false);
b89248
     }
b89248
 
b89248
   gcc_assert (cfun->machine->fs.cfa_reg != stack_pointer_rtx);
b89248
@@ -10669,7 +10701,7 @@ ix86_emit_probe_stack_range (HOST_WIDE_INT first, HOST_WIDE_INT size,
b89248
 						       sr.reg),
b89248
 					 rounded_size - size));
b89248
 
b89248
-      release_scratch_register_on_entry (&sr);
b89248
+      release_scratch_register_on_entry (&sr, size, true);
b89248
     }
b89248
 
b89248
   /* Make sure nothing is scheduled before we are done.  */
b89248
diff --git a/gcc/testsuite/gcc.target/i386/pr84128.c b/gcc/testsuite/gcc.target/i386/pr84128.c
b89248
new file mode 100644
b89248
index 0000000..a8323fd
b89248
--- /dev/null
b89248
+++ b/gcc/testsuite/gcc.target/i386/pr84128.c
b89248
@@ -0,0 +1,30 @@
b89248
+/* { dg-do run } */
b89248
+/* { dg-options "-O2 -march=i686 -mtune=generic -fstack-clash-protection" } */
b89248
+/* { dg-require-effective-target ia32 } */
b89248
+
b89248
+__attribute__ ((noinline, noclone, weak, regparm (3)))
b89248
+int
b89248
+f1 (long arg0, int (*pf) (long, void *))
b89248
+{
b89248
+  unsigned char buf[32768];
b89248
+  return pf (arg0, buf);
b89248
+}
b89248
+
b89248
+__attribute__ ((noinline, noclone, weak))
b89248
+int
b89248
+f2 (long arg0, void *ignored)
b89248
+{
b89248
+  if (arg0 != 17)
b89248
+    __builtin_abort ();
b89248
+  return 19;
b89248
+}
b89248
+
b89248
+int
b89248
+main (void)
b89248
+{
b89248
+  if (f1 (17, f2) != 19)
b89248
+    __builtin_abort ();
b89248
+  return 0;
b89248
+}
b89248
+
b89248
+