https://gcc.gnu.org/g:b2c1ee0989bb010143e8eb5d931ffc28bb1aca7c
commit r17-5802-gb2c1ee0989bb010143e8eb5d931ffc28bb1aca7c Author: Kyrylo Tkachov <[email protected]> Date: Thu Oct 15 19:57:35 2026 +0200 bb-reorder: Respect max-goto-duplication-insns without insn lengths [PR middle-end/126236] pass_duplicate_computed_gotos measures blocks in units of the minimum length of an unconditional jump. On a target without a length attribute, such as c6x, ft32, ia64, m68k, mmix, mn10300, rl78 or vax, get_attr_min_length returns 0. The budget is then 0 and every block costs 0, so the pass copies blocks of any size and ignores --param=max-goto-duplication-insns, even when it is 0. Count one unit per active insn on such targets, and assert that the unit is not 0. Debug insns cost nothing, as before. Targets with a length attribute are unchanged. On the others the pass no longer emits a scratch jump to measure its length, so the numbers of later labels change. copy_bb_p and shrink-wrapping use the same lengths and are not changed here. Bootstrapped and tested on aarch64-linux-gnu and x86_64-linux-gnu with the rest of the series. With cross compilers for c6x, ft32, ia64, m68k, mmix, mn10300, rl78 and vax, duplicate-computed-goto-1.c now gets no copies (21 before), and duplicate-computed-goto-2.c gets the same 21. gcc/ChangeLog: PR middle-end/126236 * bb-reorder.cc: Include insn-attr.h. (duplication_unit): New function. (duplication_size): Likewise. (maybe_duplicate_computed_goto): Use duplication_size. (duplicate_computed_gotos): Use duplication_unit. gcc/testsuite/ChangeLog: PR middle-end/126236 * gcc.dg/duplicate-computed-goto-1.c: New test. * gcc.dg/duplicate-computed-goto-2.c: New test. Signed-off-by: Kyrylo Tkachov <[email protected]> Diff: --- gcc/bb-reorder.cc | 45 +++++++++++++++++++----- gcc/testsuite/gcc.dg/duplicate-computed-goto-1.c | 12 +++++++ gcc/testsuite/gcc.dg/duplicate-computed-goto-2.c | 12 +++++++ 3 files changed, 60 insertions(+), 9 deletions(-) diff --git a/gcc/bb-reorder.cc b/gcc/bb-reorder.cc index 4e863bdac998..3fbf6fd9fe98 100644 --- a/gcc/bb-reorder.cc +++ b/gcc/bb-reorder.cc @@ -104,6 +104,7 @@ #include "regs.h" #include "emit-rtl.h" #include "output.h" +#include "insn-attr.h" #include "expr.h" #include "tree-pass.h" #include "cfgrtl.h" @@ -2706,6 +2707,39 @@ make_pass_reorder_blocks (gcc::context *ctxt) return new pass_reorder_blocks (ctxt); } +/* Return the unit of --param=max-goto-duplication-insns: the length of an + unconditional jump, or 1 if the target has no length attribute. */ + +static int +duplication_unit (void) +{ + if (!HAVE_ATTR_length) + return 1; + + /* We are estimating the length of uncond jump insn only once + since the code for getting the insn length always returns + the minimal length now. */ + if (uncond_jump_length == 0) + uncond_jump_length = get_uncond_jump_length (); + + /* A target with insn lengths gives a jump a nonzero length. A zero unit + would make every budget 0, whatever the parameter says. */ + gcc_checking_assert (uncond_jump_length > 0); + return uncond_jump_length; +} + +/* Return the size of INSN in the units of duplication_unit: its minimum + length, or 1 for an active insn if the target has no length attribute. + Debug insns never count, so they cannot change the decision. */ + +static int +duplication_size (rtx_insn *insn) +{ + if (HAVE_ATTR_length) + return get_attr_min_length (insn); + return active_insn_p (insn); +} + /* Duplicate a block (that we already know ends in a computed jump) into its predecessors, where possible. Return whether anything is changed. */ static bool @@ -2716,7 +2750,7 @@ maybe_duplicate_computed_goto (basic_block bb, int max_size) FOR_BB_INSNS (bb, insn) if (INSN_P (insn)) { - max_size -= get_attr_min_length (insn); + max_size -= duplication_size (insn); if (max_size < 0) return false; } @@ -2772,15 +2806,8 @@ maybe_duplicate_computed_goto (basic_block bb, int max_size) static void duplicate_computed_gotos (function *fun) { - /* We are estimating the length of uncond jump insn only once - since the code for getting the insn length always returns - the minimal length now. */ - if (uncond_jump_length == 0) - uncond_jump_length = get_uncond_jump_length (); - /* Never copy a block larger than this. */ - int max_size - = uncond_jump_length * param_max_goto_duplication_insns; + int max_size = duplication_unit () * param_max_goto_duplication_insns; bool changed = false; diff --git a/gcc/testsuite/gcc.dg/duplicate-computed-goto-1.c b/gcc/testsuite/gcc.dg/duplicate-computed-goto-1.c new file mode 100644 index 000000000000..01a1fe2476b5 --- /dev/null +++ b/gcc/testsuite/gcc.dg/duplicate-computed-goto-1.c @@ -0,0 +1,12 @@ +/* PR middle-end/126236 */ +/* { dg-do compile } */ +/* { dg-require-effective-target label_values } */ +/* { dg-require-effective-target indirect_jumps } */ +/* { dg-options "-O2 -fno-tree-slp-vectorize --param=max-goto-duplication-insns=0 -fdump-rtl-compgotos" } */ + +/* A zero budget disables computed goto duplication, also on targets + without an insn length attribute. */ + +#include "pr127246-1.c" + +/* { dg-final { scan-rtl-dump-not "Duplicating" "compgotos" } } */ diff --git a/gcc/testsuite/gcc.dg/duplicate-computed-goto-2.c b/gcc/testsuite/gcc.dg/duplicate-computed-goto-2.c new file mode 100644 index 000000000000..56e722afe80f --- /dev/null +++ b/gcc/testsuite/gcc.dg/duplicate-computed-goto-2.c @@ -0,0 +1,12 @@ +/* PR middle-end/126236 */ +/* { dg-do compile } */ +/* { dg-require-effective-target label_values } */ +/* { dg-require-effective-target indirect_jumps } */ +/* { dg-options "-O2 -fno-tree-slp-vectorize --param=max-goto-duplication-insns=64 -fdump-rtl-compgotos" } */ + +/* The computed goto of duplicate-computed-goto-1.c is duplicated with a + sufficient budget. */ + +#include "pr127246-1.c" + +/* { dg-final { scan-rtl-dump "Duplicating" "compgotos" } } */
