Blame SOURCES/gcc48-rh1469697-11.patch

85359c
commit 27d2a2d27f3e0060ade9a1a82ce2292aad6c6931
85359c
Author: law <law@138bc75d-0d04-0410-961f-82ee72b054a4>
85359c
Date:   Mon Sep 25 23:13:55 2017 +0000
85359c
85359c
            * config/rs6000/rs6000-protos.h (output_probe_stack_range): Update
85359c
            prototype for new argument.
85359c
            * config/rs6000/rs6000.c (rs6000_emit_allocate_stack_1): New function,
85359c
            mostly extracted from rs6000_emit_allocate_stack.
85359c
            (rs6000_emit_probe_stack_range_stack_clash): New function.
85359c
            (rs6000_emit_allocate_stack): Call
85359c
            rs6000_emit_probe_stack_range_stack_clash as needed.
85359c
            (rs6000_emit_probe_stack_range): Add additional argument
85359c
            to call to gen_probe_stack_range{si,di}.
85359c
            (output_probe_stack_range): New.
85359c
            (output_probe_stack_range_1): Renamed from output_probe_stack_range.
85359c
            (output_probe_stack_range_stack_clash): New.
85359c
            (rs6000_emit_prologue): Emit notes into dump file as requested.
85359c
            * rs6000.md (allocate_stack): Handle -fstack-clash-protection.
85359c
            (probe_stack_range<P:mode>): Operand 0 is now early-clobbered.
85359c
            Add additional operand and pass it to output_probe_stack_range.
85359c
    
85359c
            * lib/target-supports.exp
85359c
            (check_effective_target_supports_stack_clash_protection): Enable for
85359c
            rs6000 and powerpc targets.
85359c
    
85359c
    git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@253179 138bc75d-0d04-0410-961f-82ee72b054a4
85359c
85359c
diff --git a/gcc/config/rs6000/rs6000-protos.h b/gcc/config/rs6000/rs6000-protos.h
85359c
index d4b93d9970d..cfb23ab80cc 100644
85359c
--- a/gcc/config/rs6000/rs6000-protos.h
85359c
+++ b/gcc/config/rs6000/rs6000-protos.h
85359c
@@ -114,7 +114,7 @@ extern void rs6000_emit_sCOND (enum machine_mode, rtx[]);
85359c
 extern void rs6000_emit_cbranch (enum machine_mode, rtx[]);
85359c
 extern char * output_cbranch (rtx, const char *, int, rtx);
85359c
 extern char * output_e500_flip_gt_bit (rtx, rtx);
85359c
-extern const char * output_probe_stack_range (rtx, rtx);
85359c
+extern const char * output_probe_stack_range (rtx, rtx, rtx);
85359c
 extern rtx rs6000_emit_set_const (rtx, enum machine_mode, rtx, int);
85359c
 extern int rs6000_emit_cmove (rtx, rtx, rtx, rtx);
85359c
 extern int rs6000_emit_vector_cond_expr (rtx, rtx, rtx, rtx, rtx, rtx);
85359c
diff --git a/gcc/config/rs6000/rs6000.c b/gcc/config/rs6000/rs6000.c
85359c
index a9052c6becf..c5d9988c1d9 100644
85359c
--- a/gcc/config/rs6000/rs6000.c
85359c
+++ b/gcc/config/rs6000/rs6000.c
85359c
@@ -22320,6 +22320,220 @@ rs6000_emit_stack_tie (rtx fp, bool hard_frame_needed)
85359c
   emit_insn (gen_stack_tie (gen_rtx_PARALLEL (VOIDmode, p)));
85359c
 }
85359c
 
85359c
+/* Allocate SIZE_INT bytes on the stack using a store with update style insn
85359c
+   and set the appropriate attributes for the generated insn.  Return the
85359c
+   first insn which adjusts the stack pointer or the last insn before
85359c
+   the stack adjustment loop. 
85359c
+
85359c
+   SIZE_INT is used to create the CFI note for the allocation.
85359c
+
85359c
+   SIZE_RTX is an rtx containing the size of the adjustment.  Note that
85359c
+   since stacks grow to lower addresses its runtime value is -SIZE_INT.
85359c
+
85359c
+   ORIG_SP contains the backchain value that must be stored at *sp.  */
85359c
+
85359c
+static rtx
85359c
+rs6000_emit_allocate_stack_1 (HOST_WIDE_INT size_int, rtx orig_sp)
85359c
+{
85359c
+  rtx insn;
85359c
+
85359c
+  rtx size_rtx = GEN_INT (-size_int);
85359c
+  if (size_int > 32767)
85359c
+    {
85359c
+      rtx tmp_reg = gen_rtx_REG (Pmode, 0);
85359c
+      /* Need a note here so that try_split doesn't get confused.  */
85359c
+      if (get_last_insn () == NULL_RTX)
85359c
+	emit_note (NOTE_INSN_DELETED);
85359c
+      insn = emit_move_insn (tmp_reg, size_rtx);
85359c
+      try_split (PATTERN (insn), insn, 0);
85359c
+      size_rtx = tmp_reg;
85359c
+    }
85359c
+  
85359c
+  if (Pmode == SImode)
85359c
+    insn = emit_insn (gen_movsi_update_stack (stack_pointer_rtx,
85359c
+					      stack_pointer_rtx,
85359c
+					      size_rtx,
85359c
+					      orig_sp));
85359c
+  else
85359c
+    insn = emit_insn (gen_movdi_di_update_stack (stack_pointer_rtx,
85359c
+						 stack_pointer_rtx,
85359c
+						 size_rtx,
85359c
+						 orig_sp));
85359c
+  rtx par = PATTERN (insn);
85359c
+  gcc_assert (GET_CODE (par) == PARALLEL);
85359c
+  rtx set = XVECEXP (par, 0, 0);
85359c
+  gcc_assert (GET_CODE (set) == SET);
85359c
+  rtx mem = SET_DEST (set);
85359c
+  gcc_assert (MEM_P (mem));
85359c
+  MEM_NOTRAP_P (mem) = 1;
85359c
+  set_mem_alias_set (mem, get_frame_alias_set ());
85359c
+
85359c
+  RTX_FRAME_RELATED_P (insn) = 1;
85359c
+  add_reg_note (insn, REG_FRAME_RELATED_EXPR,
85359c
+		gen_rtx_SET (VOIDmode, stack_pointer_rtx,
85359c
+			     gen_rtx_PLUS (Pmode,
85359c
+					   stack_pointer_rtx,
85359c
+					   GEN_INT (-size_int))));
85359c
+
85359c
+  /* Emit a blockage to ensure the allocation/probing insns are
85359c
+     not optimized, combined, removed, etc.  Add REG_STACK_CHECK
85359c
+     note for similar reasons.  */
85359c
+  if (flag_stack_clash_protection)
85359c
+    {
85359c
+      add_reg_note (insn, REG_STACK_CHECK, const0_rtx);
85359c
+      emit_insn (gen_blockage ());
85359c
+    }
85359c
+
85359c
+  return insn;
85359c
+}
85359c
+
85359c
+static HOST_WIDE_INT
85359c
+get_stack_clash_protection_probe_interval (void)
85359c
+{
85359c
+  return (HOST_WIDE_INT_1U
85359c
+	  << PARAM_VALUE (PARAM_STACK_CLASH_PROTECTION_PROBE_INTERVAL));
85359c
+}
85359c
+
85359c
+static HOST_WIDE_INT
85359c
+get_stack_clash_protection_guard_size (void)
85359c
+{
85359c
+  return (HOST_WIDE_INT_1U
85359c
+	  << PARAM_VALUE (PARAM_STACK_CLASH_PROTECTION_GUARD_SIZE));
85359c
+}
85359c
+
85359c
+/* Allocate ORIG_SIZE bytes on the stack and probe the newly
85359c
+   allocated space every STACK_CLASH_PROTECTION_PROBE_INTERVAL bytes.
85359c
+
85359c
+   COPY_REG, if non-null, should contain a copy of the original
85359c
+   stack pointer at exit from this function.
85359c
+
85359c
+   This is subtly different than the Ada probing in that it tries hard to
85359c
+   prevent attacks that jump the stack guard.  Thus it is never allowed to
85359c
+   allocate more than STACK_CLASH_PROTECTION_PROBE_INTERVAL bytes of stack
85359c
+   space without a suitable probe.  */
85359c
+static rtx
85359c
+rs6000_emit_probe_stack_range_stack_clash (HOST_WIDE_INT orig_size,
85359c
+					   rtx copy_reg)
85359c
+{
85359c
+  rtx orig_sp = copy_reg;
85359c
+
85359c
+  HOST_WIDE_INT probe_interval = get_stack_clash_protection_probe_interval ();
85359c
+
85359c
+  /* Round the size down to a multiple of PROBE_INTERVAL.  */
85359c
+  HOST_WIDE_INT rounded_size = ROUND_DOWN (orig_size, probe_interval);
85359c
+
85359c
+  /* If explicitly requested,
85359c
+       or the rounded size is not the same as the original size
85359c
+       or the the rounded size is greater than a page,
85359c
+     then we will need a copy of the original stack pointer.  */
85359c
+  if (rounded_size != orig_size
85359c
+      || rounded_size > probe_interval
85359c
+      || copy_reg)
85359c
+    {
85359c
+      /* If the caller did not request a copy of the incoming stack
85359c
+	 pointer, then we use r0 to hold the copy.  */
85359c
+      if (!copy_reg)
85359c
+	orig_sp = gen_rtx_REG (Pmode, 0);
85359c
+      emit_move_insn (orig_sp, stack_pointer_rtx);
85359c
+    }
85359c
+
85359c
+  /* There's three cases here.
85359c
+
85359c
+     One is a single probe which is the most common and most efficiently
85359c
+     implemented as it does not have to have a copy of the original
85359c
+     stack pointer if there are no residuals.
85359c
+
85359c
+     Second is unrolled allocation/probes which we use if there's just
85359c
+     a few of them.  It needs to save the original stack pointer into a
85359c
+     temporary for use as a source register in the allocation/probe.
85359c
+
85359c
+     Last is a loop.  This is the most uncommon case and least efficient.  */
85359c
+  rtx retval = NULL;
85359c
+  if (rounded_size == probe_interval)
85359c
+    {
85359c
+      retval = rs6000_emit_allocate_stack_1 (probe_interval, stack_pointer_rtx);
85359c
+
85359c
+      dump_stack_clash_frame_info (PROBE_INLINE, rounded_size != orig_size);
85359c
+    }
85359c
+  else if (rounded_size <= 8 * probe_interval)
85359c
+    {
85359c
+      /* The ABI requires using the store with update insns to allocate
85359c
+	 space and store the backchain into the stack
85359c
+
85359c
+	 So we save the current stack pointer into a temporary, then
85359c
+	 emit the store-with-update insns to store the saved stack pointer
85359c
+	 into the right location in each new page.  */
85359c
+      for (int i = 0; i < rounded_size; i += probe_interval)
85359c
+	{
85359c
+	  rtx insn = rs6000_emit_allocate_stack_1 (probe_interval, orig_sp);
85359c
+
85359c
+	  /* Save the first stack adjustment in RETVAL.  */
85359c
+	  if (i == 0)
85359c
+	    retval = insn;
85359c
+	}
85359c
+
85359c
+      dump_stack_clash_frame_info (PROBE_INLINE, rounded_size != orig_size);
85359c
+    }
85359c
+  else
85359c
+    {
85359c
+      /* Compute the ending address.  */
85359c
+      rtx end_addr
85359c
+	= copy_reg ? gen_rtx_REG (Pmode, 0) : gen_rtx_REG (Pmode, 12);
85359c
+      rtx rs = GEN_INT (-rounded_size);
85359c
+      rtx insn;
85359c
+      if (add_operand (rs, Pmode))
85359c
+	insn = emit_insn (gen_add3_insn (end_addr, stack_pointer_rtx, rs));
85359c
+      else
85359c
+	{
85359c
+	  emit_move_insn (end_addr, GEN_INT (-rounded_size));
85359c
+	  insn = emit_insn (gen_add3_insn (end_addr, end_addr,
85359c
+					   stack_pointer_rtx));
85359c
+	  /* Describe the effect of INSN to the CFI engine.  */
85359c
+	  add_reg_note (insn, REG_FRAME_RELATED_EXPR,
85359c
+			gen_rtx_SET (VOIDmode, end_addr,
85359c
+				     gen_rtx_PLUS (Pmode, stack_pointer_rtx,
85359c
+						   rs)));
85359c
+	}
85359c
+      RTX_FRAME_RELATED_P (insn) = 1;
85359c
+
85359c
+      /* Emit the loop.  */
85359c
+      if (TARGET_64BIT)
85359c
+	retval = emit_insn (gen_probe_stack_rangedi (stack_pointer_rtx,
85359c
+						     stack_pointer_rtx, orig_sp,
85359c
+						     end_addr));
85359c
+      else
85359c
+	retval = emit_insn (gen_probe_stack_rangesi (stack_pointer_rtx,
85359c
+						     stack_pointer_rtx, orig_sp,
85359c
+						     end_addr));
85359c
+      RTX_FRAME_RELATED_P (retval) = 1;
85359c
+      /* Describe the effect of INSN to the CFI engine.  */
85359c
+      add_reg_note (retval, REG_FRAME_RELATED_EXPR,
85359c
+		    gen_rtx_SET (VOIDmode, stack_pointer_rtx, end_addr));
85359c
+
85359c
+      /* Emit a blockage to ensure the allocation/probing insns are
85359c
+	 not optimized, combined, removed, etc.  Other cases handle this
85359c
+	 within their call to rs6000_emit_allocate_stack_1.  */
85359c
+      emit_insn (gen_blockage ());
85359c
+
85359c
+      dump_stack_clash_frame_info (PROBE_LOOP, rounded_size != orig_size);
85359c
+    }
85359c
+
85359c
+  if (orig_size != rounded_size)
85359c
+    {
85359c
+      /* Allocate (and implicitly probe) any residual space.   */
85359c
+      HOST_WIDE_INT residual = orig_size - rounded_size;
85359c
+
85359c
+      rtx insn = rs6000_emit_allocate_stack_1 (residual, orig_sp);
85359c
+
85359c
+      /* If the residual was the only allocation, then we can return the
85359c
+	 allocating insn.  */
85359c
+      if (!retval)
85359c
+	retval = insn;
85359c
+    }
85359c
+
85359c
+  return retval;
85359c
+}
85359c
+
85359c
 /* Emit the correct code for allocating stack space, as insns.
85359c
    If COPY_REG, make sure a copy of the old frame is left there.
85359c
    The generated code may use hard register 0 as a temporary.  */
85359c
@@ -22331,7 +22545,6 @@ rs6000_emit_allocate_stack (HOST_WIDE_INT size, rtx copy_reg, int copy_off)
85359c
   rtx stack_reg = gen_rtx_REG (Pmode, STACK_POINTER_REGNUM);
85359c
   rtx tmp_reg = gen_rtx_REG (Pmode, 0);
85359c
   rtx todec = gen_int_mode (-size, Pmode);
85359c
-  rtx par, set, mem;
85359c
 
85359c
   if (INTVAL (todec) != -size)
85359c
     {
85359c
@@ -22368,6 +22581,22 @@ rs6000_emit_allocate_stack (HOST_WIDE_INT size, rtx copy_reg, int copy_off)
85359c
 	warning (0, "stack limit expression is not supported");
85359c
     }
85359c
 
85359c
+  if (flag_stack_clash_protection)
85359c
+    {
85359c
+      if (size < get_stack_clash_protection_guard_size ())
85359c
+	dump_stack_clash_frame_info (NO_PROBE_SMALL_FRAME, true);
85359c
+      else
85359c
+	{
85359c
+	  rtx insn = rs6000_emit_probe_stack_range_stack_clash (size, copy_reg);
85359c
+
85359c
+	  /* If we asked for a copy with an offset, then we still need add in
85359c
+	     the offset.  */
85359c
+	  if (copy_reg && copy_off)
85359c
+	    emit_insn (gen_add3_insn (copy_reg, copy_reg, GEN_INT (copy_off)));
85359c
+	  return;
85359c
+	}
85359c
+    }
85359c
+
85359c
   if (copy_reg)
85359c
     {
85359c
       if (copy_off != 0)
85359c
@@ -22376,39 +22605,12 @@ rs6000_emit_allocate_stack (HOST_WIDE_INT size, rtx copy_reg, int copy_off)
85359c
 	emit_move_insn (copy_reg, stack_reg);
85359c
     }
85359c
 
85359c
-  if (size > 32767)
85359c
-    {
85359c
-      /* Need a note here so that try_split doesn't get confused.  */
85359c
-      if (get_last_insn () == NULL_RTX)
85359c
-	emit_note (NOTE_INSN_DELETED);
85359c
-      insn = emit_move_insn (tmp_reg, todec);
85359c
-      try_split (PATTERN (insn), insn, 0);
85359c
-      todec = tmp_reg;
85359c
-    }
85359c
-  
85359c
-  insn = emit_insn (TARGET_32BIT
85359c
-		    ? gen_movsi_update_stack (stack_reg, stack_reg,
85359c
-					todec, stack_reg)
85359c
-		    : gen_movdi_di_update_stack (stack_reg, stack_reg,
85359c
-					   todec, stack_reg));
85359c
   /* Since we didn't use gen_frame_mem to generate the MEM, grab
85359c
      it now and set the alias set/attributes. The above gen_*_update
85359c
      calls will generate a PARALLEL with the MEM set being the first
85359c
      operation. */
85359c
-  par = PATTERN (insn);
85359c
-  gcc_assert (GET_CODE (par) == PARALLEL);
85359c
-  set = XVECEXP (par, 0, 0);
85359c
-  gcc_assert (GET_CODE (set) == SET);
85359c
-  mem = SET_DEST (set);
85359c
-  gcc_assert (MEM_P (mem));
85359c
-  MEM_NOTRAP_P (mem) = 1;
85359c
-  set_mem_alias_set (mem, get_frame_alias_set ());
85359c
-
85359c
-  RTX_FRAME_RELATED_P (insn) = 1;
85359c
-  add_reg_note (insn, REG_FRAME_RELATED_EXPR,
85359c
-		gen_rtx_SET (VOIDmode, stack_reg,
85359c
-			     gen_rtx_PLUS (Pmode, stack_reg,
85359c
-					   GEN_INT (-size))));
85359c
+  insn = rs6000_emit_allocate_stack_1 (size, stack_reg);
85359c
+  return;
85359c
 }
85359c
 
85359c
 #define PROBE_INTERVAL (1 << STACK_CHECK_PROBE_INTERVAL_EXP)
85359c
@@ -22490,9 +22692,9 @@ rs6000_emit_probe_stack_range (HOST_WIDE_INT first, HOST_WIDE_INT size)
85359c
 	 until it is equal to ROUNDED_SIZE.  */
85359c
 
85359c
       if (TARGET_64BIT)
85359c
-	emit_insn (gen_probe_stack_rangedi (r12, r12, r0));
85359c
+	emit_insn (gen_probe_stack_rangedi (r12, r12, stack_pointer_rtx, r0));
85359c
       else
85359c
-	emit_insn (gen_probe_stack_rangesi (r12, r12, r0));
85359c
+	emit_insn (gen_probe_stack_rangesi (r12, r12, stack_pointer_rtx, r0));
85359c
 
85359c
 
85359c
       /* Step 4: probe at FIRST + SIZE if we cannot assert at compile-time
85359c
@@ -22504,10 +22706,10 @@ rs6000_emit_probe_stack_range (HOST_WIDE_INT first, HOST_WIDE_INT size)
85359c
 }
85359c
 
85359c
 /* Probe a range of stack addresses from REG1 to REG2 inclusive.  These are
85359c
-   absolute addresses.  */
85359c
+   addresses, not offsets.  */
85359c
 
85359c
-const char *
85359c
-output_probe_stack_range (rtx reg1, rtx reg2)
85359c
+static const char *
85359c
+output_probe_stack_range_1 (rtx reg1, rtx reg2)
85359c
 {
85359c
   static int labelno = 0;
85359c
   char loop_lab[32], end_lab[32];
85359c
@@ -22546,6 +22748,63 @@ output_probe_stack_range (rtx reg1, rtx reg2)
85359c
   return "";
85359c
 }
85359c
 
85359c
+/* Probe a range of stack addresses from REG1 to REG3 inclusive.  These are
85359c
+   addresses, not offsets.
85359c
+
85359c
+   REG2 contains the backchain that must be stored into *sp at each allocation.
85359c
+
85359c
+   This is subtly different than the Ada probing above in that it tries hard
85359c
+   to prevent attacks that jump the stack guard.  Thus, it is never allowed
85359c
+   to allocate more than PROBE_INTERVAL bytes of stack space without a
85359c
+   suitable probe.  */
85359c
+
85359c
+static const char *
85359c
+output_probe_stack_range_stack_clash (rtx reg1, rtx reg2, rtx reg3)
85359c
+{
85359c
+  static int labelno = 0;
85359c
+  char loop_lab[32];
85359c
+  rtx xops[3];
85359c
+
85359c
+  HOST_WIDE_INT probe_interval = get_stack_clash_protection_probe_interval ();
85359c
+
85359c
+  ASM_GENERATE_INTERNAL_LABEL (loop_lab, "LPSRL", labelno++);
85359c
+
85359c
+  ASM_OUTPUT_INTERNAL_LABEL (asm_out_file, loop_lab);
85359c
+
85359c
+  /* This allocates and probes.  */
85359c
+  xops[0] = reg1;
85359c
+  xops[1] = reg2;
85359c
+  xops[2] = GEN_INT (-probe_interval);
85359c
+  if (TARGET_64BIT)
85359c
+    output_asm_insn ("stdu %1,%2(%0)", xops);
85359c
+  else
85359c
+    output_asm_insn ("stwu %1,%2(%0)", xops);
85359c
+
85359c
+  /* Jump to LOOP_LAB if TEST_ADDR != LAST_ADDR.  */
85359c
+  xops[0] = reg1;
85359c
+  xops[1] = reg3;
85359c
+  if (TARGET_64BIT)
85359c
+    output_asm_insn ("cmpd 0,%0,%1", xops);
85359c
+  else
85359c
+    output_asm_insn ("cmpw 0,%0,%1", xops);
85359c
+
85359c
+  fputs ("\tbne 0,", asm_out_file);
85359c
+  assemble_name_raw (asm_out_file, loop_lab);
85359c
+  fputc ('\n', asm_out_file);
85359c
+
85359c
+  return "";
85359c
+}
85359c
+
85359c
+/* Wrapper around the output_probe_stack_range routines.  */
85359c
+const char *
85359c
+output_probe_stack_range (rtx reg1, rtx reg2, rtx reg3)
85359c
+{
85359c
+  if (flag_stack_clash_protection)
85359c
+    return output_probe_stack_range_stack_clash (reg1, reg2, reg3);
85359c
+  else
85359c
+    return output_probe_stack_range_1 (reg1, reg3);
85359c
+}
85359c
+
85359c
 /* Add to 'insn' a note which is PATTERN (INSN) but with REG replaced
85359c
    with (plus:P (reg 1) VAL), and with REG2 replaced with RREG if REG2
85359c
    is not NULL.  It would be nice if dwarf2out_frame_debug_expr could
85359c
@@ -23857,6 +24116,13 @@ rs6000_emit_prologue (void)
85359c
 	  }
85359c
     }
85359c
 
85359c
+  /* If we are emitting stack probes, but allocate no stack, then
85359c
+     just note that in the dump file.  */
85359c
+  if (flag_stack_clash_protection
85359c
+      && dump_file
85359c
+      && !info->push_p)
85359c
+    dump_stack_clash_frame_info (NO_PROBE_NO_FRAME, false);
85359c
+
85359c
   /* Update stack and set back pointer unless this is V.4,
85359c
      for which it was done previously.  */
85359c
   if (!WORLD_SAVE_P (info) && info->push_p
85359c
diff --git a/gcc/config/rs6000/rs6000.md b/gcc/config/rs6000/rs6000.md
85359c
index cd197213480..3cd70e592c1 100644
85359c
--- a/gcc/config/rs6000/rs6000.md
85359c
+++ b/gcc/config/rs6000/rs6000.md
85359c
@@ -11822,10 +11822,20 @@
85359c
 ;;
85359c
 ;; First, an insn to allocate new stack space for dynamic use (e.g., alloca).
85359c
 ;; We move the back-chain and decrement the stack pointer.
85359c
-
85359c
+;;
85359c
+;; Operand1 is more naturally reg_or_short_operand.  However, for a large
85359c
+;; constant alloca, using that predicate will force the generic code to put
85359c
+;; the constant size into a register before calling the expander.
85359c
+;;
85359c
+;; As a result the expander would not have the constant size information
85359c
+;; in those cases and would have to generate less efficient code.
85359c
+;;
85359c
+;; Thus we allow reg_or_cint_operand instead so that the expander can see
85359c
+;; the constant size.  The value is forced into a register if necessary.
85359c
+;;
85359c
 (define_expand "allocate_stack"
85359c
   [(set (match_operand 0 "gpc_reg_operand" "")
85359c
-	(minus (reg 1) (match_operand 1 "reg_or_short_operand" "")))
85359c
+	(minus (reg 1) (match_operand 1 "reg_or_cint_operand" "")))
85359c
    (set (reg 1)
85359c
 	(minus (reg 1) (match_dup 1)))]
85359c
   ""
85359c
@@ -11835,6 +11845,15 @@
85359c
   rtx neg_op0;
85359c
   rtx insn, par, set, mem;
85359c
 
85359c
+  /* By allowing reg_or_cint_operand as the predicate we can get
85359c
+     better code for stack-clash-protection because we do not lose
85359c
+     size information.  But the rest of the code expects the operand
85359c
+     to be reg_or_short_operand.  If it isn't, then force it into
85359c
+     a register.  */
85359c
+  rtx orig_op1 = operands[1];
85359c
+  if (!reg_or_short_operand (operands[1], Pmode))
85359c
+    operands[1] = force_reg (Pmode, operands[1]);
85359c
+
85359c
   emit_move_insn (chain, stack_bot);
85359c
 
85359c
   /* Check stack bounds if necessary.  */
85359c
@@ -11847,6 +11866,51 @@
85359c
       emit_insn (gen_cond_trap (LTU, available, operands[1], const0_rtx));
85359c
     }
85359c
 
85359c
+  /* Allocate and probe if requested.
85359c
+     This may look similar to the loop we use for prologue allocations,
85359c
+     but it is critically different.  For the former we know the loop
85359c
+     will iterate, but do not know that generally here.  The former
85359c
+     uses that knowledge to rotate the loop.  Combining them would be
85359c
+     possible with some performance cost.  */
85359c
+  if (flag_stack_clash_protection)
85359c
+    {
85359c
+      rtx rounded_size, last_addr, residual;
85359c
+      HOST_WIDE_INT probe_interval;
85359c
+      compute_stack_clash_protection_loop_data (&rounded_size, &last_addr,
85359c
+						&residual, &probe_interval,
85359c
+						orig_op1);
85359c
+      
85359c
+      /* We do occasionally get in here with constant sizes, we might
85359c
+	 as well do a reasonable job when we obviously can.  */
85359c
+      if (rounded_size != const0_rtx)
85359c
+	{
85359c
+	  rtx loop_lab, end_loop;
85359c
+	  bool rotated = CONST_INT_P (rounded_size);
85359c
+
85359c
+	  emit_stack_clash_protection_probe_loop_start (&loop_lab, &end_loop,
85359c
+							last_addr, rotated);
85359c
+
85359c
+	  if (Pmode == SImode)
85359c
+	    emit_insn (gen_movsi_update_stack (stack_pointer_rtx,
85359c
+					       stack_pointer_rtx,
85359c
+					       GEN_INT (-probe_interval),
85359c
+					       chain));
85359c
+	  else
85359c
+	    emit_insn (gen_movdi_di_update_stack (stack_pointer_rtx,
85359c
+					          stack_pointer_rtx,
85359c
+					          GEN_INT (-probe_interval),
85359c
+					          chain));
85359c
+	  emit_stack_clash_protection_probe_loop_end (loop_lab, end_loop,
85359c
+						      last_addr, rotated);
85359c
+	}
85359c
+
85359c
+      /* Now handle residuals.  We just have to set operands[1] correctly
85359c
+	 and let the rest of the expander run.  */
85359c
+      operands[1] = residual;
85359c
+      if (!CONST_INT_P (residual))
85359c
+	operands[1] = force_reg (Pmode, operands[1]);
85359c
+    }
85359c
+
85359c
   if (GET_CODE (operands[1]) != CONST_INT
85359c
       || INTVAL (operands[1]) < -32767
85359c
       || INTVAL (operands[1]) > 32768)
85359c
@@ -12994,12 +13058,13 @@
85359c
    (set_attr "length" "4")])
85359c
 
85359c
 (define_insn "probe_stack_range<P:mode>"
85359c
-  [(set (match_operand:P 0 "register_operand" "=r")
85359c
+  [(set (match_operand:P 0 "register_operand" "=&r")
85359c
 	(unspec_volatile:P [(match_operand:P 1 "register_operand" "0")
85359c
-			    (match_operand:P 2 "register_operand" "r")]
85359c
+			    (match_operand:P 2 "register_operand" "r")
85359c
+			    (match_operand:P 3 "register_operand" "r")]
85359c
 			   UNSPECV_PROBE_STACK_RANGE))]
85359c
   ""
85359c
-  "* return output_probe_stack_range (operands[0], operands[2]);"
85359c
+  "* return output_probe_stack_range (operands[0], operands[2], operands[3]);"
85359c
   [(set_attr "type" "three")])
85359c
 
85359c
 ;; Compare insns are next.  Note that the RS/6000 has two types of compares,
85359c
diff --git a/gcc/testsuite/lib/target-supports.exp b/gcc/testsuite/lib/target-supports.exp
85359c
index 7c126e4122b..aba99513ed0 100644
85359c
--- a/gcc/testsuite/lib/target-supports.exp
85359c
+++ b/gcc/testsuite/lib/target-supports.exp
85359c
@@ -5421,12 +5421,12 @@ proc check_effective_target_autoincdec { } {
85359c
 proc check_effective_target_supports_stack_clash_protection { } {
85359c
 
85359c
    # Temporary until the target bits are fully ACK'd.
85359c
-#  if { [istarget aarch*-*-*]
85359c
-#       || [istarget powerpc*-*-*] || [istarget rs6000*-*-*] } {
85359c
+#  if { [istarget aarch*-*-*] } {
85359c
 #	return 1
85359c
 #  }
85359c
 
85359c
     if { [istarget x86_64-*-*] || [istarget i?86-*-*] 
85359c
+	  || [istarget powerpc*-*-*] || [istarget rs6000*-*-*]
85359c
 	  || [istarget s390*-*-*] } {
85359c
 	return 1
85359c
     }