Blame SOURCES/gcc48-rh1469697-21.patch

56d343
	PR middle-end/83654
56d343
	* explow.c (anti_adjust_stack_and_probe_stack_clash): Test a
56d343
	non-constant residual for zero at runtime and avoid probing in
56d343
	that case.  Reorganize code for trailing problem to mirror handling
56d343
	of the residual.
56d343
56d343
	PR middle-end/83654
56d343
	* gcc.target/i386/stack-check-18.c: New test.
56d343
	* gcc.target/i386/stack-check-19.c: New test.
56d343
56d343
diff --git a/gcc/explow.c b/gcc/explow.c
56d343
index b6c56602152..042e71904ec 100644
56d343
--- a/gcc/explow.c
56d343
+++ b/gcc/explow.c
56d343
@@ -1997,11 +1997,27 @@ anti_adjust_stack_and_probe_stack_clash (rtx size)
56d343
 
56d343
   if (residual != CONST0_RTX (Pmode))
56d343
     {
56d343
+      rtx label = NULL_RTX;
56d343
+      /* RESIDUAL could be zero at runtime and in that case *sp could
56d343
+	 hold live data.  Furthermore, we do not want to probe into the
56d343
+	 red zone.
56d343
+
56d343
+	 Go ahead and just guard the probe at *sp on RESIDUAL != 0 at
56d343
+	 runtime if RESIDUAL is not a compile time constant.  */
56d343
+      if (!CONST_INT_P (residual))
56d343
+	{
56d343
+	  label = gen_label_rtx ();
56d343
+	  emit_cmp_and_jump_insns (residual, CONST0_RTX (GET_MODE (residual)),
56d343
+				   EQ, NULL_RTX, Pmode, 1, label);
56d343
+	}
56d343
+
56d343
       rtx x = force_reg (Pmode, plus_constant (Pmode, residual,
56d343
 					       -GET_MODE_SIZE (word_mode)));
56d343
       anti_adjust_stack (residual);
56d343
       emit_stack_probe (gen_rtx_PLUS (Pmode, stack_pointer_rtx, x));
56d343
       emit_insn (gen_blockage ());
56d343
+      if (!CONST_INT_P (residual))
56d343
+	emit_label (label);
56d343
     }
56d343
 
56d343
   /* Some targets make optimistic assumptions in their prologues about
56d343
@@ -2014,28 +2030,20 @@ anti_adjust_stack_and_probe_stack_clash (rtx size)
56d343
 	 live data.  Furthermore, we don't want to probe into the red
56d343
 	 zone.
56d343
 
56d343
-	 Go ahead and just guard a probe at *sp on SIZE != 0 at runtime
56d343
+	 Go ahead and just guard the probe at *sp on SIZE != 0 at runtime
56d343
 	 if SIZE is not a compile time constant.  */
56d343
-
56d343
-      /* Ideally we would just probe at *sp.  However, if SIZE is not
56d343
-	 a compile-time constant, but is zero at runtime, then *sp
56d343
-	 might hold live data.  So probe at *sp if we know that
56d343
-	 an allocation was made, otherwise probe into the red zone
56d343
-	 which is obviously undesirable.  */
56d343
-      if (CONST_INT_P (size))
56d343
-	{
56d343
-	  emit_stack_probe (stack_pointer_rtx);
56d343
-	  emit_insn (gen_blockage ());
56d343
-	}
56d343
-      else
56d343
+      rtx label = NULL_RTX;
56d343
+      if (!CONST_INT_P (size))
56d343
 	{
56d343
-	  rtx label = gen_label_rtx ();
56d343
+	  label = gen_label_rtx ();
56d343
 	  emit_cmp_and_jump_insns (size, CONST0_RTX (GET_MODE (size)),
56d343
 				   EQ, NULL_RTX, Pmode, 1, label);
56d343
-	  emit_stack_probe (stack_pointer_rtx);
56d343
-	  emit_insn (gen_blockage ());
56d343
-	  emit_label (label);
56d343
 	}
56d343
+
56d343
+      emit_stack_probe (stack_pointer_rtx);
56d343
+      emit_insn (gen_blockage ());
56d343
+      if (!CONST_INT_P (size))
56d343
+	emit_label (label);
56d343
     }
56d343
 }
56d343
 
56d343
diff --git a/gcc/testsuite/gcc.target/i386/stack-check-18.c b/gcc/testsuite/gcc.target/i386/stack-check-18.c
56d343
new file mode 100644
56d343
index 00000000000..6dbff4402da
56d343
--- /dev/null
56d343
+++ b/gcc/testsuite/gcc.target/i386/stack-check-18.c
56d343
@@ -0,0 +1,23 @@
56d343
+/* { dg-do compile } */
56d343
+/* { dg-options "-O2 -fstack-clash-protection -mtune=generic -fdump-rtl-expand" } */
56d343
+/* { dg-require-effective-target supports_stack_clash_protection } */
56d343
+
56d343
+int f1 (char *);
56d343
+
56d343
+int
56d343
+f2 (void)
56d343
+{
56d343
+  const int size = 4096;
56d343
+  char buffer[size];
56d343
+  return f1 (buffer);
56d343
+}
56d343
+
56d343
+/* So we want to verify that at expand time that we probed the main
56d343
+   VLA allocation as well as the residuals.  Then we want to verify
56d343
+   there was only one probe in the final assembly (implying the
56d343
+   residual probe was optimized away).  */
56d343
+/* { dg-final { scan-rtl-dump-times "allocation and probing in loop" 1 "expand" } } */
56d343
+/* { dg-final { scan-rtl-dump-times "allocation and probing residuals" 1 "expand" } } */
56d343
+
56d343
+/* { dg-final { scan-assembler-times "or\[ql\]" 1 } } */
56d343
+
56d343
diff --git a/gcc/testsuite/gcc.target/i386/stack-check-19.c b/gcc/testsuite/gcc.target/i386/stack-check-19.c
56d343
new file mode 100644
56d343
index 00000000000..b92c126d57f
56d343
--- /dev/null
56d343
+++ b/gcc/testsuite/gcc.target/i386/stack-check-19.c
56d343
@@ -0,0 +1,29 @@
56d343
+/* { dg-do compile } */
56d343
+/* { dg-options "-O2 -fstack-clash-protection -mtune=generic -fdump-rtl-expand" } */
56d343
+/* { dg-require-effective-target supports_stack_clash_protection } */
56d343
+
56d343
+int f1 (char *);
56d343
+
56d343
+int
56d343
+f2 (const int size)
56d343
+{
56d343
+  char buffer[size];
56d343
+  return f1 (buffer);
56d343
+}
56d343
+
56d343
+/* So we want to verify that at expand time that we probed the main
56d343
+   VLA allocation as well as the residuals.  Then we want to verify
56d343
+   there are two probes in the final assembly code.  */
56d343
+/* { dg-final { scan-rtl-dump-times "allocation and probing in loop" 1 "expand" } } */
56d343
+/* { dg-final { scan-rtl-dump-times "allocation and probing residuals" 1 "expand" } } */
56d343
+/* { dg-final { scan-assembler-times "or\[ql\]" 2 } } */
56d343
+
56d343
+/* We also want to verify (indirectly) that the residual probe is
56d343
+   guarded.  We do that by checking the number of conditional
56d343
+   branches.  There should be 3.  One that bypasses the probe loop, one
56d343
+   in the probe loop and one that bypasses the residual probe.
56d343
+
56d343
+   These will all be equality tests.  */
56d343
+/* { dg-final { scan-assembler-times "(\?:je|jne)" 3 } } */
56d343
+
56d343
+