Add GIMPLE switch support to loop unswitching
authorMartin Liska <mliska@suse.cz>
Mon, 22 Nov 2021 12:54:20 +0000 (13:54 +0100)
committerRichard Biener <rguenther@suse.de>
Wed, 25 May 2022 08:37:13 +0000 (10:37 +0200)
commita1c9f779f75283427316b5c670c1e01ff8ce9ced
tree3b23452070d3fa5c671287b3796721451c71356e
parent0d344b557604e966dc7f91739881f03e1f221efd
Add GIMPLE switch support to loop unswitching

This patch adds support to unswitch loops with switch statements
based on invariant index.  It furthermore reworks the cost model
to allow an overall budget of statements to be created per original
loop by all unswitching opportunities in the loop.  Compared to
the original all unswitching opportunities in a loop are
pre-evaluated before the first transform which will allow future
changes to select the most profitable candidates first.

To efficiently support switch statements the pass now uses
ranger to simplify switch statements and conditions in loop
copies based on ranges extracted from the recorded set of
predicates unswitched.

gcc/ChangeLog:

* dbgcnt.def (DEBUG_COUNTER): Add loop_unswitch counter.
* params.opt (max-unswitch-level): Remove.
* doc/invoke.texi (max-unswitch-level): Likewise.
* tree-cfg.cc (gimple_lv_add_condition_to_bb): Support not
gimplified expressions.
* tree-ssa-loop-unswitch.cc (struct unswitch_predicate): New.
(tree_may_unswitch_on): Rename to ...
(find_unswitching_predicates_for_bb): ... this and handle
switch statements.
(get_predicates_for_bb): Likewise.
(set_predicates_for_bb): Likewise.
(init_loop_unswitch_info): Likewise.
(tree_ssa_unswitch_loops): Prepare stuff before calling
tree_unswitch_single_loop.
(tree_unswitch_single_loop): Rework the function using
pre-computed predicates and with a per original loop cost model.
(merge_last): New.
(add_predicate_to_path): Likewise.
(find_range_for_lhs): Likewise.
(simplify_using_entry_checks): Rename to ...
(evaluate_control_stmt_using_entry_checks): ... this, handle
switch statements and improve simplifications using ranger.
(simplify_loop_version): Rework using
evaluate_control_stmt_using_entry_checks.
(evaluate_bbs): New.
(evaluate_loop_insns_for_predicate): Likewise.
(tree_unswitch_loop): Adjust to allow switch statements and
pass in the edge to unswitch.
(clean_up_after_unswitching): New.
(pass_tree_unswitch::execute): Pass down fun.

gcc/testsuite/ChangeLog:

* gcc.dg/loop-unswitch-7.c: New test.
* gcc.dg/loop-unswitch-8.c: New test.
* gcc.dg/loop-unswitch-9.c: New test.
* gcc.dg/loop-unswitch-10.c: New test.
* gcc.dg/loop-unswitch-11.c: New test.
* gcc.dg/loop-unswitch-12.c: New test.
* gcc.dg/loop-unswitch-13.c: New test.
* gcc.dg/loop-unswitch-14.c: New test.
* gcc.dg/loop-unswitch-15.c: New test.
* gcc.dg/loop-unswitch-16.c: New test.
* gcc.dg/loop-unswitch-17.c: New test.
* gcc.dg/torture/20220518-1.c: New test.
* gcc.dg/torture/20220518-2.c: New test.
* gcc.dg/torture/20220525-1.c: New test.
* gcc.dg/alias-10.c: Adjust.
* gcc.dg/tree-ssa/loop-6.c: Likewise.
* gcc.dg/loop-unswitch-1.c: Likewise.

Co-authored-by: Richard Biener <rguenther@suse.de>
22 files changed:
gcc/dbgcnt.def
gcc/doc/invoke.texi
gcc/params.opt
gcc/testsuite/gcc.dg/alias-10.c
gcc/testsuite/gcc.dg/loop-unswitch-1.c
gcc/testsuite/gcc.dg/loop-unswitch-10.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/loop-unswitch-11.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/loop-unswitch-12.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/loop-unswitch-13.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/loop-unswitch-14.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/loop-unswitch-15.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/loop-unswitch-16.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/loop-unswitch-17.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/loop-unswitch-7.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/loop-unswitch-8.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/loop-unswitch-9.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/torture/20220518-1.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/torture/20220518-2.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/torture/20220525-1.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/tree-ssa/loop-6.c
gcc/tree-cfg.cc
gcc/tree-ssa-loop-unswitch.cc