56d343
56d343
            * config/i386/i386.c (ix86_emit_restore_reg_using_pop): Prototype.
56d343
            (ix86_adjust_stack_and_probe_stack_clash): Use a push/pop sequence
56d343
            to probe at the start of a noreturn function.
56d343
    
56d343
            * gcc.target/i386/stack-check-12.c: New test
56d343
56d343
diff -Nrup a/gcc/config/i386/i386.c b/gcc/config/i386/i386.c
56d343
--- a/gcc/config/i386/i386.c	2017-11-03 13:35:17.641528205 -0600
56d343
+++ b/gcc/config/i386/i386.c	2017-11-03 13:37:39.489361692 -0600
56d343
@@ -64,6 +64,7 @@ along with GCC; see the file COPYING3.
56d343
 #include "tree-flow.h"
56d343
 
56d343
 static rtx legitimize_dllimport_symbol (rtx, bool);
56d343
+static void ix86_emit_restore_reg_using_pop (rtx);
56d343
 
56d343
 #ifndef CHECK_STACK_LIMIT
56d343
 #define CHECK_STACK_LIMIT (-1)
56d343
@@ -9884,8 +9885,14 @@ ix86_adjust_stack_and_probe_stack_clash
56d343
      we just probe when we cross PROBE_INTERVAL.  */
56d343
   if (TREE_THIS_VOLATILE (cfun->decl))
56d343
     {
56d343
-      emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
56d343
-				       -GET_MODE_SIZE (word_mode)));
56d343
+      /* We can safely use any register here since we're just going to push
56d343
+	 its value and immediately pop it back.  But we do try and avoid
56d343
+	 argument passing registers so as not to introduce dependencies in
56d343
+	 the pipeline.  For 32 bit we use %esi and for 64 bit we use %rax.  */
56d343
+      rtx dummy_reg = gen_rtx_REG (word_mode, TARGET_64BIT ? AX_REG : SI_REG);
56d343
+      rtx insn = emit_insn (gen_push (dummy_reg));
56d343
+      RTX_FRAME_RELATED_P (insn) = 1;
56d343
+      ix86_emit_restore_reg_using_pop (dummy_reg);
56d343
       emit_insn (gen_blockage ());
56d343
     }
56d343
 
56d343
diff -Nrup a/gcc/testsuite/gcc.target/i386/stack-check-12.c b/gcc/testsuite/gcc.target/i386/stack-check-12.c
56d343
--- a/gcc/testsuite/gcc.target/i386/stack-check-12.c	1969-12-31 17:00:00.000000000 -0700
56d343
+++ b/gcc/testsuite/gcc.target/i386/stack-check-12.c	2017-11-03 13:36:15.104055651 -0600
56d343
@@ -0,0 +1,19 @@
56d343
+/* { dg-do compile } */
56d343
+/* { dg-options "-O2 -fstack-clash-protection -mtune=generic -fomit-frame-pointer" } */
56d343
+/* { dg-require-effective-target supports_stack_clash_protection } */
56d343
+
56d343
+__attribute__ ((noreturn)) void exit (int);
56d343
+
56d343
+__attribute__ ((noreturn)) void
56d343
+f (void)
56d343
+{
56d343
+  asm volatile ("nop" ::: "edi");
56d343
+  exit (1);
56d343
+}
56d343
+
56d343
+/* { dg-final { scan-assembler-not "or\[ql\]" } } */
56d343
+/* { dg-final { scan-assembler "pushl	%esi" { target ia32 } } } */
56d343
+/* { dg-final { scan-assembler "popl	%esi" { target ia32 } } }*/
56d343
+/* { dg-final { scan-assembler "pushq	%rax" { target { ! ia32 } } } } */
56d343
+/* { dg-final { scan-assembler "popq	%rax" { target { ! ia32 } } } }*/
56d343
+