Blame SOURCES/gcc48-rh1469697-11.patch

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