From 7a1bdd662bc670ee14646b371699c724df3ba643 Mon Sep 17 00:00:00 2001 From: Jeff Law Date: Mon, 6 Nov 2017 08:51:16 -0700 Subject: [PATCH] re PR target/82788 (wrong code with -fstack-clash-protection --param=stack-clash-protection-probe-interval=10 on simple code) PR target/82788 * config/i386/i386.c (PROBE_INTERVAL): Remove. (get_probe_interval): New functions. (ix86_adjust_stack_and_probe_stack_clash): Use get_probe_interval. (ix86_adjust_stack_and_probe): Likewise. (output_adjust_stack_and_probe): Likewise. (ix86_emit_probe_stack_range): Likewise. (ix86_expand_prologue): Likewise. PR target/82788 * gcc.dg/pr82788.c: New test. From-SVN: r254456 --- gcc/ChangeLog | 11 +++++++ gcc/config/i386/i386.c | 57 ++++++++++++++++++++-------------- gcc/testsuite/ChangeLog | 5 +++ gcc/testsuite/gcc.dg/pr82788.c | 4 +++ 4 files changed, 54 insertions(+), 23 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/pr82788.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 4ff37f4fad5..1cda8a32a7a 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,14 @@ +2017-11-06 Jeff Law + + PR target/82788 + * config/i386/i386.c (PROBE_INTERVAL): Remove. + (get_probe_interval): New functions. + (ix86_adjust_stack_and_probe_stack_clash): Use get_probe_interval. + (ix86_adjust_stack_and_probe): Likewise. + (output_adjust_stack_and_probe): Likewise. + (ix86_emit_probe_stack_range): Likewise. + (ix86_expand_prologue): Likewise. + 2017-11-06 Richard Sandiford Alan Hayward David Sherwood diff --git a/gcc/config/i386/i386.c b/gcc/config/i386/i386.c index 4123edf982f..0d5e2726a33 100644 --- a/gcc/config/i386/i386.c +++ b/gcc/config/i386/i386.c @@ -12099,7 +12099,17 @@ release_scratch_register_on_entry (struct scratch_reg *sr) } } -#define PROBE_INTERVAL (1 << STACK_CHECK_PROBE_INTERVAL_EXP) +/* Return the probing interval for -fstack-clash-protection. */ + +static HOST_WIDE_INT +get_probe_interval (void) +{ + if (flag_stack_clash_protection) + return (HOST_WIDE_INT_1U + << PARAM_VALUE (PARAM_STACK_CLASH_PROTECTION_PROBE_INTERVAL)); + else + return (HOST_WIDE_INT_1U << STACK_CHECK_PROBE_INTERVAL_EXP); +} /* Emit code to adjust the stack pointer by SIZE bytes while probing it. @@ -12168,8 +12178,7 @@ ix86_adjust_stack_and_probe_stack_clash (const HOST_WIDE_INT size) /* We're allocating a large enough stack frame that we need to emit probes. Either emit them inline or in a loop depending on the size. */ - HOST_WIDE_INT probe_interval - = 1 << PARAM_VALUE (PARAM_STACK_CLASH_PROTECTION_PROBE_INTERVAL); + HOST_WIDE_INT probe_interval = get_probe_interval (); if (size <= 4 * probe_interval) { HOST_WIDE_INT i; @@ -12178,7 +12187,7 @@ ix86_adjust_stack_and_probe_stack_clash (const HOST_WIDE_INT size) /* Allocate PROBE_INTERVAL bytes. */ rtx insn = pro_epilogue_adjust_stack (stack_pointer_rtx, stack_pointer_rtx, - GEN_INT (-PROBE_INTERVAL), -1, + GEN_INT (-probe_interval), -1, m->fs.cfa_reg == stack_pointer_rtx); add_reg_note (insn, REG_STACK_CHECK, const0_rtx); @@ -12271,7 +12280,7 @@ ix86_adjust_stack_and_probe (const HOST_WIDE_INT size) that's the easy case. The run-time loop is made up of 9 insns in the generic case while the compile-time loop is made up of 3+2*(n-1) insns for n # of intervals. */ - if (size <= 4 * PROBE_INTERVAL) + if (size <= 4 * get_probe_interval ()) { HOST_WIDE_INT i, adjust; bool first_probe = true; @@ -12280,15 +12289,15 @@ ix86_adjust_stack_and_probe (const HOST_WIDE_INT size) 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 < size; i += PROBE_INTERVAL) + for (i = get_probe_interval (); i < size; i += get_probe_interval ()) { if (first_probe) { - adjust = 2 * PROBE_INTERVAL + dope; + adjust = 2 * get_probe_interval () + dope; first_probe = false; } else - adjust = PROBE_INTERVAL; + adjust = get_probe_interval (); emit_insn (gen_rtx_SET (stack_pointer_rtx, plus_constant (Pmode, stack_pointer_rtx, @@ -12297,9 +12306,9 @@ ix86_adjust_stack_and_probe (const HOST_WIDE_INT size) } if (first_probe) - adjust = size + PROBE_INTERVAL + dope; + adjust = size + get_probe_interval () + dope; else - adjust = size + PROBE_INTERVAL - i; + adjust = size + get_probe_interval () - i; emit_insn (gen_rtx_SET (stack_pointer_rtx, plus_constant (Pmode, stack_pointer_rtx, @@ -12309,7 +12318,8 @@ ix86_adjust_stack_and_probe (const HOST_WIDE_INT size) /* Adjust back to account for the additional first interval. */ last = emit_insn (gen_rtx_SET (stack_pointer_rtx, plus_constant (Pmode, stack_pointer_rtx, - PROBE_INTERVAL + dope))); + (get_probe_interval () + + dope)))); } /* Otherwise, do the same as above, but in a loop. Note that we must be @@ -12327,7 +12337,7 @@ ix86_adjust_stack_and_probe (const HOST_WIDE_INT size) /* Step 1: round SIZE to the previous multiple of the interval. */ - rounded_size = ROUND_DOWN (size, PROBE_INTERVAL); + rounded_size = ROUND_DOWN (size, get_probe_interval ()); /* Step 2: compute initial and final value of the loop counter. */ @@ -12335,7 +12345,7 @@ ix86_adjust_stack_and_probe (const HOST_WIDE_INT size) /* SP = SP_0 + PROBE_INTERVAL. */ emit_insn (gen_rtx_SET (stack_pointer_rtx, plus_constant (Pmode, stack_pointer_rtx, - - (PROBE_INTERVAL + dope)))); + - (get_probe_interval () + dope)))); /* LAST_ADDR = SP_0 + PROBE_INTERVAL + ROUNDED_SIZE. */ if (rounded_size <= (HOST_WIDE_INT_1 << 31)) @@ -12380,7 +12390,8 @@ ix86_adjust_stack_and_probe (const HOST_WIDE_INT size) /* Adjust back to account for the additional first interval. */ last = emit_insn (gen_rtx_SET (stack_pointer_rtx, plus_constant (Pmode, stack_pointer_rtx, - PROBE_INTERVAL + dope))); + (get_probe_interval () + + dope)))); release_scratch_register_on_entry (&sr); } @@ -12397,7 +12408,7 @@ ix86_adjust_stack_and_probe (const HOST_WIDE_INT size) XVECEXP (expr, 0, 1) = gen_rtx_SET (stack_pointer_rtx, plus_constant (Pmode, stack_pointer_rtx, - PROBE_INTERVAL + dope + size)); + get_probe_interval () + dope + size)); add_reg_note (last, REG_FRAME_RELATED_EXPR, expr); RTX_FRAME_RELATED_P (last) = 1; @@ -12424,7 +12435,7 @@ output_adjust_stack_and_probe (rtx reg) /* SP = SP + PROBE_INTERVAL. */ xops[0] = stack_pointer_rtx; - xops[1] = GEN_INT (PROBE_INTERVAL); + xops[1] = GEN_INT (get_probe_interval ()); output_asm_insn ("sub%z0\t{%1, %0|%0, %1}", xops); /* Probe at SP. */ @@ -12454,14 +12465,14 @@ ix86_emit_probe_stack_range (HOST_WIDE_INT first, HOST_WIDE_INT size) that's the easy case. The run-time loop is made up of 6 insns in the generic case while the compile-time loop is made up of n insns for n # of intervals. */ - if (size <= 6 * PROBE_INTERVAL) + if (size <= 6 * get_probe_interval ()) { HOST_WIDE_INT i; /* Probe at FIRST + 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 probe at FIRST + SIZE. */ - for (i = PROBE_INTERVAL; i < size; i += PROBE_INTERVAL) + for (i = get_probe_interval (); i < size; i += get_probe_interval ()) emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx, -(first + i))); @@ -12484,7 +12495,7 @@ ix86_emit_probe_stack_range (HOST_WIDE_INT first, HOST_WIDE_INT size) /* Step 1: round SIZE to the previous multiple of the interval. */ - rounded_size = ROUND_DOWN (size, PROBE_INTERVAL); + rounded_size = ROUND_DOWN (size, get_probe_interval ()); /* Step 2: compute initial and final value of the loop counter. */ @@ -12545,7 +12556,7 @@ output_probe_stack_range (rtx reg, rtx end) /* TEST_ADDR = TEST_ADDR + PROBE_INTERVAL. */ xops[0] = reg; - xops[1] = GEN_INT (PROBE_INTERVAL); + xops[1] = GEN_INT (get_probe_interval ()); output_asm_insn ("sub%z0\t{%1, %0|%0, %1}", xops); /* Probe at TEST_ADDR. */ @@ -13223,7 +13234,7 @@ ix86_expand_prologue (void) else if (STACK_CHECK_MOVING_SP) { if (!(crtl->is_leaf && !cfun->calls_alloca - && allocate <= PROBE_INTERVAL)) + && allocate <= get_probe_interval ())) { ix86_adjust_stack_and_probe (allocate); allocate = 0; @@ -13240,7 +13251,7 @@ ix86_expand_prologue (void) { if (crtl->is_leaf && !cfun->calls_alloca) { - if (size > PROBE_INTERVAL) + if (size > get_probe_interval ()) ix86_emit_probe_stack_range (0, size); } else @@ -13251,7 +13262,7 @@ ix86_expand_prologue (void) { if (crtl->is_leaf && !cfun->calls_alloca) { - if (size > PROBE_INTERVAL + if (size > get_probe_interval () && size > get_stack_check_protect ()) ix86_emit_probe_stack_range (get_stack_check_protect (), size - get_stack_check_protect ()); diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index d098cfc27ca..de0e0a8ebee 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2017-11-06 Jeff Law + + PR target/82788 + * gcc.dg/pr82788.c: New test. + 2017-11-06 Richard Sandiford Alan Hayward David Sherwood diff --git a/gcc/testsuite/gcc.dg/pr82788.c b/gcc/testsuite/gcc.dg/pr82788.c new file mode 100644 index 00000000000..a8f628fd7f6 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr82788.c @@ -0,0 +1,4 @@ +/* { dg-do run } */ +/* { dg-options "-O2 -fstack-clash-protection --param stack-clash-protection-probe-interval=10 --param stack-clash-protection-guard-size=12" } */ +/* { dg-require-effective-target supports_stack_clash_protection } */ +int main() { int a[1442]; return 0;} -- 2.30.2