Blame SOURCES/gcc7-pr84128.patch

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