+ /* Probe at TEST_ADDR. */
+ emit_stack_probe (test_addr);
+
+ emit_jump (loop_lab);
+
+ emit_label (end_lab);
+
+
+ /* Step 4: probe at FIRST + SIZE if we cannot assert at compile-time
+ that SIZE is equal to ROUNDED_SIZE. */
+
+ /* TEMP = SIZE - ROUNDED_SIZE. */
+ temp = simplify_gen_binary (MINUS, Pmode, size, rounded_size);
+ if (temp != const0_rtx)
+ {
+ rtx addr;
+
+ if (CONST_INT_P (temp))
+ {
+ /* Use [base + disp} addressing mode if supported. */
+ HOST_WIDE_INT offset = INTVAL (temp);
+ addr = memory_address (Pmode,
+ plus_constant (last_addr,
+ STACK_GROW_OFF (offset)));
+ }
+ else
+ {
+ /* Manual CSE if the difference is not known at compile-time. */
+ temp = gen_rtx_MINUS (Pmode, size, rounded_size_op);
+ addr = memory_address (Pmode,
+ gen_rtx_fmt_ee (STACK_GROW_OP, Pmode,
+ last_addr, temp));
+ }
+
+ emit_stack_probe (addr);
+ }
+ }
+}
+
+/* Adjust the stack pointer by minus SIZE (an rtx for a number of bytes)
+ while probing it. This pushes when SIZE is positive. SIZE need not
+ be constant. If ADJUST_BACK is true, adjust back the stack pointer
+ by plus SIZE at the end. */
+
+void
+anti_adjust_stack_and_probe (rtx size, bool adjust_back)
+{
+ /* We skip the probe for the first interval + a small dope of 4 words and
+ probe that many bytes past the specified size to maintain a protection
+ area at the botton of the stack. */
+ const int dope = 4 * UNITS_PER_WORD;
+
+ /* First ensure SIZE is Pmode. */
+ if (GET_MODE (size) != VOIDmode && GET_MODE (size) != Pmode)
+ size = convert_to_mode (Pmode, size, 1);
+
+ /* If we have a constant small number of probes to generate, that's the
+ easy case. */
+ if (CONST_INT_P (size) && INTVAL (size) < 7 * PROBE_INTERVAL)
+ {
+ HOST_WIDE_INT isize = INTVAL (size), i;
+ bool first_probe = true;
+
+ /* Adjust SP and probe at PROBE_INTERVAL + N * PROBE_INTERVAL for
+ values of N from 1 until it exceeds SIZE. If only one probe is
+ needed, this will not generate any code. Then adjust and probe
+ to PROBE_INTERVAL + SIZE. */
+ for (i = PROBE_INTERVAL; i < isize; i += PROBE_INTERVAL)
+ {
+ if (first_probe)
+ {
+ anti_adjust_stack (GEN_INT (2 * PROBE_INTERVAL + dope));
+ first_probe = false;
+ }
+ else
+ anti_adjust_stack (GEN_INT (PROBE_INTERVAL));
+ emit_stack_probe (stack_pointer_rtx);
+ }
+
+ if (first_probe)
+ anti_adjust_stack (plus_constant (size, PROBE_INTERVAL + dope));
+ else
+ anti_adjust_stack (plus_constant (size, PROBE_INTERVAL - i));
+ emit_stack_probe (stack_pointer_rtx);
+ }
+
+ /* In the variable case, do the same as above, but in a loop. Note that we
+ must be extra careful with variables wrapping around because we might be
+ at the very top (or the very bottom) of the address space and we have to
+ be able to handle this case properly; in particular, we use an equality
+ test for the loop condition. */
+ else
+ {
+ rtx rounded_size, rounded_size_op, last_addr, temp;
+ rtx loop_lab = gen_label_rtx ();
+ rtx end_lab = gen_label_rtx ();
+
+
+ /* Step 1: round SIZE to the previous multiple of the interval. */
+
+ /* ROUNDED_SIZE = SIZE & -PROBE_INTERVAL */
+ rounded_size
+ = simplify_gen_binary (AND, Pmode, size, GEN_INT (-PROBE_INTERVAL));
+ rounded_size_op = force_operand (rounded_size, NULL_RTX);
+
+
+ /* Step 2: compute initial and final value of the loop counter. */
+
+ /* SP = SP_0 + PROBE_INTERVAL. */
+ anti_adjust_stack (GEN_INT (PROBE_INTERVAL + dope));
+
+ /* LAST_ADDR = SP_0 + PROBE_INTERVAL + ROUNDED_SIZE. */
+ last_addr = force_operand (gen_rtx_fmt_ee (STACK_GROW_OP, Pmode,
+ stack_pointer_rtx,
+ rounded_size_op), NULL_RTX);
+
+
+ /* Step 3: the loop
+
+ while (SP != LAST_ADDR)
+ {
+ SP = SP + PROBE_INTERVAL
+ probe at SP
+ }
+
+ adjusts SP and probes at PROBE_INTERVAL + N * PROBE_INTERVAL for
+ values of N from 1 until it is equal to ROUNDED_SIZE. */
+
+ emit_label (loop_lab);
+
+ /* Jump to END_LAB if SP == LAST_ADDR. */
+ emit_cmp_and_jump_insns (stack_pointer_rtx, last_addr, EQ, NULL_RTX,
+ Pmode, 1, end_lab);
+
+ /* SP = SP + PROBE_INTERVAL and probe at SP. */
+ anti_adjust_stack (GEN_INT (PROBE_INTERVAL));
+ emit_stack_probe (stack_pointer_rtx);
+
+ emit_jump (loop_lab);
+