Blame SOURCES/gcc7-pr84128.patch

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