Blame SOURCES/gcc7-pr84128.patch

ed1ed2
ed1ed2
	PR target/84128
ed1ed2
	* config/i386/i386.c (release_scratch_register_on_entry): Add new
ed1ed2
	OFFSET and RELEASE_VIA_POP arguments.  Use SP+OFFSET to restore
ed1ed2
	the scratch if RELEASE_VIA_POP is false.
ed1ed2
	(ix86_adjust_stack_and_probe_stack_clash): Un-constify SIZE.
ed1ed2
	If we have to save a temporary register, decrement SIZE appropriately.
ed1ed2
	Pass new arguments to release_scratch_register_on_entry.
ed1ed2
	(ix86_adjust_stack_and_probe): Likewise.
ed1ed2
	(ix86_emit_probe_stack_range): Pass new arguments to
ed1ed2
	release_scratch_register_on_entry.
ed1ed2
ed1ed2
	PR target/84128
ed1ed2
	* gcc.target/i386/pr84128.c: New test.
ed1ed2
ed1ed2
diff --git a/gcc/config/i386/i386.c b/gcc/config/i386/i386.c
ed1ed2
index fef34a1..3196ac4 100644
ed1ed2
--- gcc/config/i386/i386.c
ed1ed2
+++ gcc/config/i386/i386.c
ed1ed2
@@ -12567,22 +12567,39 @@ get_scratch_register_on_entry (struct scratch_reg *sr)
ed1ed2
     }
ed1ed2
 }
ed1ed2
 
ed1ed2
-/* Release a scratch register obtained from the preceding function.  */
ed1ed2
+/* Release a scratch register obtained from the preceding function.
ed1ed2
+
ed1ed2
+   If RELEASE_VIA_POP is true, we just pop the register off the stack
ed1ed2
+   to release it.  This is what non-Linux systems use with -fstack-check.
ed1ed2
+
ed1ed2
+   Otherwise we use OFFSET to locate the saved register and the
ed1ed2
+   allocated stack space becomes part of the local frame and is
ed1ed2
+   deallocated by the epilogue.  */
ed1ed2
 
ed1ed2
 static void
ed1ed2
-release_scratch_register_on_entry (struct scratch_reg *sr)
ed1ed2
+release_scratch_register_on_entry (struct scratch_reg *sr, HOST_WIDE_INT offset,
ed1ed2
+				   bool release_via_pop)
ed1ed2
 {
ed1ed2
   if (sr->saved)
ed1ed2
     {
ed1ed2
-      struct machine_function *m = cfun->machine;
ed1ed2
-      rtx x, insn = emit_insn (gen_pop (sr->reg));
ed1ed2
+      if (release_via_pop)
ed1ed2
+	{
ed1ed2
+	  struct machine_function *m = cfun->machine;
ed1ed2
+	  rtx x, insn = emit_insn (gen_pop (sr->reg));
ed1ed2
 
ed1ed2
-      /* The RTX_FRAME_RELATED_P mechanism doesn't know about pop.  */
ed1ed2
-      RTX_FRAME_RELATED_P (insn) = 1;
ed1ed2
-      x = gen_rtx_PLUS (Pmode, stack_pointer_rtx, GEN_INT (UNITS_PER_WORD));
ed1ed2
-      x = gen_rtx_SET (stack_pointer_rtx, x);
ed1ed2
-      add_reg_note (insn, REG_FRAME_RELATED_EXPR, x);
ed1ed2
-      m->fs.sp_offset -= UNITS_PER_WORD;
ed1ed2
+	  /* The RX FRAME_RELATED_P mechanism doesn't know about pop.  */
ed1ed2
+	  RTX_FRAME_RELATED_P (insn) = 1;
ed1ed2
+	  x = gen_rtx_PLUS (Pmode, stack_pointer_rtx, GEN_INT (UNITS_PER_WORD));
ed1ed2
+	  x = gen_rtx_SET (stack_pointer_rtx, x);
ed1ed2
+	  add_reg_note (insn, REG_FRAME_RELATED_EXPR, x);
ed1ed2
+	  m->fs.sp_offset -= UNITS_PER_WORD;
ed1ed2
+	}
ed1ed2
+      else
ed1ed2
+	{
ed1ed2
+	  rtx x = gen_rtx_PLUS (Pmode, stack_pointer_rtx, GEN_INT (offset));
ed1ed2
+	  x = gen_rtx_SET (sr->reg, gen_rtx_MEM (word_mode, x));
ed1ed2
+	  emit_insn (x);
ed1ed2
+	}
ed1ed2
     }
ed1ed2
 }
ed1ed2
 
ed1ed2
@@ -12597,7 +12614,7 @@ release_scratch_register_on_entry (struct scratch_reg *sr)
ed1ed2
    pushed on the stack.  */
ed1ed2
 
ed1ed2
 static void
ed1ed2
-ix86_adjust_stack_and_probe_stack_clash (const HOST_WIDE_INT size,
ed1ed2
+ix86_adjust_stack_and_probe_stack_clash (HOST_WIDE_INT size,
ed1ed2
 					 const bool int_registers_saved)
ed1ed2
 {
ed1ed2
   struct machine_function *m = cfun->machine;
ed1ed2
@@ -12713,6 +12730,12 @@ ix86_adjust_stack_and_probe_stack_clash (const HOST_WIDE_INT size,
ed1ed2
       struct scratch_reg sr;
ed1ed2
       get_scratch_register_on_entry (&sr);
ed1ed2
 
ed1ed2
+      /* If we needed to save a register, then account for any space
ed1ed2
+	 that was pushed (we are not going to pop the register when
ed1ed2
+	 we do the restore).  */
ed1ed2
+      if (sr.saved)
ed1ed2
+	size -= UNITS_PER_WORD;
ed1ed2
+
ed1ed2
       /* Step 1: round SIZE down to a multiple of the interval.  */
ed1ed2
       HOST_WIDE_INT rounded_size = size & -probe_interval;
ed1ed2
 
ed1ed2
@@ -12761,7 +12784,9 @@ ix86_adjust_stack_and_probe_stack_clash (const HOST_WIDE_INT size,
ed1ed2
 				   m->fs.cfa_reg == stack_pointer_rtx);
ed1ed2
       dump_stack_clash_frame_info (PROBE_LOOP, size != rounded_size);
ed1ed2
 
ed1ed2
-      release_scratch_register_on_entry (&sr);
ed1ed2
+      /* This does not deallocate the space reserved for the scratch
ed1ed2
+	 register.  That will be deallocated in the epilogue.  */
ed1ed2
+      release_scratch_register_on_entry (&sr, size, false);
ed1ed2
     }
ed1ed2
 
ed1ed2
   /* Make sure nothing is scheduled before we are done.  */
ed1ed2
@@ -12774,7 +12799,7 @@ ix86_adjust_stack_and_probe_stack_clash (const HOST_WIDE_INT size,
ed1ed2
    pushed on the stack.  */
ed1ed2
 
ed1ed2
 static void
ed1ed2
-ix86_adjust_stack_and_probe (const HOST_WIDE_INT size,
ed1ed2
+ix86_adjust_stack_and_probe (HOST_WIDE_INT size,
ed1ed2
 			     const bool int_registers_saved)
ed1ed2
 {
ed1ed2
   /* We skip the probe for the first interval + a small dope of 4 words and
ed1ed2
@@ -12847,6 +12872,11 @@ ix86_adjust_stack_and_probe (const HOST_WIDE_INT size,
ed1ed2
 
ed1ed2
       get_scratch_register_on_entry (&sr);
ed1ed2
 
ed1ed2
+      /* If we needed to save a register, then account for any space
ed1ed2
+	 that was pushed (we are not going to pop the register when
ed1ed2
+	 we do the restore).  */
ed1ed2
+      if (sr.saved)
ed1ed2
+	size -= UNITS_PER_WORD;
ed1ed2
 
ed1ed2
       /* Step 1: round SIZE to the previous multiple of the interval.  */
ed1ed2
 
ed1ed2
@@ -12906,7 +12936,9 @@ ix86_adjust_stack_and_probe (const HOST_WIDE_INT size,
ed1ed2
 						    (get_probe_interval ()
ed1ed2
 						     + dope))));
ed1ed2
 
ed1ed2
-      release_scratch_register_on_entry (&sr);
ed1ed2
+      /* This does not deallocate the space reserved for the scratch
ed1ed2
+	 register.  That will be deallocated in the epilogue.  */
ed1ed2
+      release_scratch_register_on_entry (&sr, size, false);
ed1ed2
     }
ed1ed2
 
ed1ed2
   /* Even if the stack pointer isn't the CFA register, we need to correctly
ed1ed2
@@ -13055,7 +13087,7 @@ ix86_emit_probe_stack_range (HOST_WIDE_INT first, HOST_WIDE_INT size,
ed1ed2
 						       sr.reg),
ed1ed2
 					 rounded_size - size));
ed1ed2
 
ed1ed2
-      release_scratch_register_on_entry (&sr);
ed1ed2
+      release_scratch_register_on_entry (&sr, size, true);
ed1ed2
     }
ed1ed2
 
ed1ed2
   /* Make sure nothing is scheduled before we are done.  */
ed1ed2
ed1ed2
diff --git a/gcc/testsuite/gcc.target/i386/pr84128.c b/gcc/testsuite/gcc.target/i386/pr84128.c
ed1ed2
new file mode 100644
ed1ed2
index 0000000..a8323fd6
ed1ed2
--- /dev/null
ed1ed2
+++ gcc/testsuite/gcc.target/i386/pr84128.c
ed1ed2
@@ -0,0 +1,30 @@
ed1ed2
+/* { dg-do run } */
ed1ed2
+/* { dg-options "-O2 -march=i686 -mtune=generic -fstack-clash-protection" } */
ed1ed2
+/* { dg-require-effective-target ia32 } */
ed1ed2
+
ed1ed2
+__attribute__ ((noinline, noclone, weak, regparm (3)))
ed1ed2
+int
ed1ed2
+f1 (long arg0, int (*pf) (long, void *))
ed1ed2
+{
ed1ed2
+  unsigned char buf[32768];
ed1ed2
+  return pf (arg0, buf);
ed1ed2
+}
ed1ed2
+
ed1ed2
+__attribute__ ((noinline, noclone, weak))
ed1ed2
+int
ed1ed2
+f2 (long arg0, void *ignored)
ed1ed2
+{
ed1ed2
+  if (arg0 != 17)
ed1ed2
+    __builtin_abort ();
ed1ed2
+  return 19;
ed1ed2
+}
ed1ed2
+
ed1ed2
+int
ed1ed2
+main (void)
ed1ed2
+{
ed1ed2
+  if (f1 (17, f2) != 19)
ed1ed2
+    __builtin_abort ();
ed1ed2
+  return 0;
ed1ed2
+}
ed1ed2
+
ed1ed2
+