Blame SOURCES/gcc48-rh1469697-11.patch

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