From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1851) id 1E9CB3858423; Tue, 7 Dec 2021 15:22:05 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1E9CB3858423 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Martin Liska To: gcc-cvs@gcc.gnu.org Subject: [gcc(refs/users/marxin/heads/loop-unswitch-improvement-v6)] Add switch tests. X-Act-Checkin: gcc X-Git-Author: Martin Liska X-Git-Refname: refs/users/marxin/heads/loop-unswitch-improvement-v6 X-Git-Oldrev: 09c6cc5da43aff690920ea75500c1185a654ec5e X-Git-Newrev: 038ea3223553d8b397bbc490e39c9f964ceae330 Message-Id: <20211207152205.1E9CB3858423@sourceware.org> Date: Tue, 7 Dec 2021 15:22:05 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 07 Dec 2021 15:22:05 -0000 https://gcc.gnu.org/g:038ea3223553d8b397bbc490e39c9f964ceae330 commit 038ea3223553d8b397bbc490e39c9f964ceae330 Author: Martin Liska Date: Mon Dec 6 15:33:55 2021 +0100 Add switch tests. Diff: --- gcc/testsuite/gcc.dg/loop-unswitch-10.c | 56 ++++++++++++++++++++++++++++++ gcc/testsuite/gcc.dg/loop-unswitch-11.c | 45 +++++++++++++++++++++++++ gcc/testsuite/gcc.dg/loop-unswitch-12.c | 28 +++++++++++++++ gcc/testsuite/gcc.dg/loop-unswitch-13.c | 34 +++++++++++++++++++ gcc/testsuite/gcc.dg/loop-unswitch-14.c | 28 +++++++++++++++ gcc/testsuite/gcc.dg/loop-unswitch-15.c | 60 +++++++++++++++++++++++++++++++++ gcc/tree-cfg.c | 7 +++- gcc/tree-ssa-loop-unswitch.c | 10 ++++-- 8 files changed, 264 insertions(+), 4 deletions(-) diff --git a/gcc/testsuite/gcc.dg/loop-unswitch-10.c b/gcc/testsuite/gcc.dg/loop-unswitch-10.c new file mode 100644 index 00000000000..c3c940a1f80 --- /dev/null +++ b/gcc/testsuite/gcc.dg/loop-unswitch-10.c @@ -0,0 +1,56 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -funswitch-loops -fdump-tree-unswitch-details" } */ + +int +__attribute__((noipa)) +foo(double *a, double *b, double *c, double *d, double *r, int size, int order) +{ + for (int i = 0; i < size; i++) + { + double tmp, tmp2; + + switch(order) + { + case 0: + tmp = -8 * a[i]; + tmp2 = 2 * b[i]; + break; + case 1: + tmp = 3 * a[i] - 2 * b[i]; + tmp2 = 5 * b[i] - 2 * c[i]; + break; + case 2: + tmp = 9 * a[i] + 2 * b[i] + c[i]; + tmp2 = 4 * b[i] + 2 * c[i] + 8 * d[i]; + break; + case 3: + tmp = 3 * a[i] + 2 * b[i] - c[i]; + tmp2 = b[i] - 2 * c[i] + 8 * d[i]; + break; + defaut: + __builtin_unreachable (); + } + + double x = 3 * tmp + d[i] + tmp; + double y = 3.4f * tmp + d[i] + tmp2; + r[i] = x + y; + } + + return 0; +} + +#define N 16 * 1024 +double aa[N], bb[N], cc[N], dd[N], rr[N]; + +int main() +{ + for (int i = 0; i < 100 * 1000; i++) + foo (aa, bb, cc, dd, rr, N, i % 4); +} + + +/* Test that we actually unswitched something. */ +/* { dg-final { scan-tree-dump-times ";; Unswitching loop on condition: order.* == 0" 1 "unswitch" } } */ +/* { dg-final { scan-tree-dump-times ";; Unswitching loop on condition: order.* == 1" 1 "unswitch" } } */ +/* { dg-final { scan-tree-dump-times ";; Unswitching loop on condition: order.* == 2" 1 "unswitch" } } */ +/* { dg-final { scan-tree-dump-times ";; Unswitching loop on condition: order.* == 3" 1 "unswitch" } } */ diff --git a/gcc/testsuite/gcc.dg/loop-unswitch-11.c b/gcc/testsuite/gcc.dg/loop-unswitch-11.c new file mode 100644 index 00000000000..85173bd6b64 --- /dev/null +++ b/gcc/testsuite/gcc.dg/loop-unswitch-11.c @@ -0,0 +1,45 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -funswitch-loops -fdump-tree-unswitch-details" } */ + +int +foo(double *a, double *b, double *c, double *d, double *r, int size, int order) +{ + for (int i = 0; i < size; i++) + { + double tmp, tmp2; + + switch(order) + { + case 5 ... 6: + case 9: + tmp = -8 * a[i]; + tmp2 = 2 * b[i]; + break; + case 11: + tmp = 3 * a[i] - 2 * b[i]; + tmp2 = 5 * b[i] - 2 * c[i]; + break; + case 22: + tmp = 9 * a[i] + 2 * b[i] + c[i]; + tmp2 = 4 * b[i] + 2 * c[i] + 8 * d[i]; + break; + case 33: + tmp = 3 * a[i] + 2 * b[i] - c[i]; + tmp2 = b[i] - 2 * c[i] + 8 * d[i]; + break; + defaut: + __builtin_unreachable (); + } + + double x = 3 * tmp + d[i] + tmp; + double y = 3.4f * tmp + d[i] + tmp2; + r[i] = x + y; + } + + return 0; +} + +/* { dg-final { scan-tree-dump-times ";; Unswitching loop on condition: order_.* >= 5 & order_.* <= 6 \\| order_.* == 9" 1 "unswitch" } } */ +/* { dg-final { scan-tree-dump-times ";; Unswitching loop on condition: order.* == 1" 1 "unswitch" } } */ +/* { dg-final { scan-tree-dump-times ";; Unswitching loop on condition: order.* == 2" 1 "unswitch" } } */ +/* { dg-final { scan-tree-dump-times ";; Unswitching loop on condition: order.* == 3" 1 "unswitch" } } */ diff --git a/gcc/testsuite/gcc.dg/loop-unswitch-12.c b/gcc/testsuite/gcc.dg/loop-unswitch-12.c new file mode 100644 index 00000000000..c99afe0e61c --- /dev/null +++ b/gcc/testsuite/gcc.dg/loop-unswitch-12.c @@ -0,0 +1,28 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -funswitch-loops -fdump-tree-unswitch-details" } */ + +int +foo(double *a, double *b, double *c, double *d, double *r, int size, int order) +{ + for (int i = 0; i < size; i++) + { + double tmp; + + if (order == 1) + tmp = -8 * a[i]; + else + tmp = -4 * b[i]; + + double x = 3 * tmp + d[i] + tmp; + + if (order == 1) + x += 2; + + double y = 3.4f * tmp + d[i]; + r[i] = x + y; + } + + return 0; +} + +/* { dg-final { scan-tree-dump-times ";; Unswitching loop on condition: order.* == 1" 1 "unswitch" } } */ diff --git a/gcc/testsuite/gcc.dg/loop-unswitch-13.c b/gcc/testsuite/gcc.dg/loop-unswitch-13.c new file mode 100644 index 00000000000..df092b668d6 --- /dev/null +++ b/gcc/testsuite/gcc.dg/loop-unswitch-13.c @@ -0,0 +1,34 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -funswitch-loops -fdump-tree-unswitch-details" } */ + +int +foo(double *a, double *b, double *c, double *d, double *r, int size, unsigned order) +{ + for (int i = 0; i < size; i++) + { + double tmp; + + switch (order) + { + case 0 ... 4: + tmp = -8 * a[i]; + break; + default: + tmp = -4 * b[i]; + break; + } + + double x = 3 * tmp + d[i] + tmp; + + /* This should not be unswitched as it's mutually excluded with case 0 ... 4. */ + if (order >= 5) + x += 2; + + double y = 3.4f * tmp + d[i]; + r[i] = x + y; + } + + return 0; +} + +/* { dg-final { scan-tree-dump-times ";; Unswitching loop on condition: order.* <= 4" 1 "unswitch" } } */ diff --git a/gcc/testsuite/gcc.dg/loop-unswitch-14.c b/gcc/testsuite/gcc.dg/loop-unswitch-14.c new file mode 100644 index 00000000000..92e5cef5916 --- /dev/null +++ b/gcc/testsuite/gcc.dg/loop-unswitch-14.c @@ -0,0 +1,28 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -funswitch-loops -fdump-tree-unswitch-details" } */ + +int +foo(double *a, double *b, double *c, double *d, double *r, int size, float order) +{ + for (int i = 0; i < size; i++) + { + double tmp; + + if (order == 1.f) + tmp = -8 * a[i]; + else + tmp = -4 * b[i]; + + double x = 3 * tmp + d[i] + tmp; + + if (order == 1.f) + x += 2; + + double y = 3.4f * tmp + d[i]; + r[i] = x + y; + } + + return 0; +} + +/* { dg-final { scan-tree-dump-times ";; Unswitching loop on condition: order.* == 1.0e" 1 "unswitch" } } */ diff --git a/gcc/testsuite/gcc.dg/loop-unswitch-15.c b/gcc/testsuite/gcc.dg/loop-unswitch-15.c new file mode 100644 index 00000000000..406611cd28a --- /dev/null +++ b/gcc/testsuite/gcc.dg/loop-unswitch-15.c @@ -0,0 +1,60 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -funswitch-loops -fdump-tree-unswitch-details --param=max-unswitch-insns=1000" } */ + +int +__attribute__((noipa)) +foo(double *a, double *b, double *c, double *d, double *r, int size, int order) +{ + for (int i = 0; i < size; i++) + { + double tmp, tmp2; + + if (order <= 0) + tmp = 123; + + switch(order) + { + case 0: + tmp += -8 * a[i]; + tmp2 = 2 * b[i]; + break; + case 1: + tmp = 3 * a[i] - 2 * b[i]; + tmp2 = 5 * b[i] - 2 * c[i]; + break; + case 2: + tmp = 9 * a[i] + 2 * b[i] + c[i]; + tmp2 = 4 * b[i] + 2 * c[i] + 8 * d[i]; + break; + case 3: + tmp = 3 * a[i] + 2 * b[i] - c[i]; + tmp2 = b[i] - 2 * c[i] + 8 * d[i]; + break; + defaut: + __builtin_unreachable (); + } + + double x = 3 * tmp + d[i] + tmp; + double y = 3.4f * tmp + d[i] + tmp2; + r[i] = x + y; + } + + return 0; +} + +#define N 16 * 1024 +double aa[N], bb[N], cc[N], dd[N], rr[N]; + +int main() +{ + for (int i = 0; i < 100 * 1000; i++) + foo (aa, bb, cc, dd, rr, N, i % 4); +} + + +/* Test that we actually unswitched something. */ +/* { dg-final { scan-tree-dump-times ";; Unswitching loop on condition: order.* <= 0" 1 "unswitch" } } */ +/* { dg-final { scan-tree-dump-times ";; Unswitching loop on condition: order.* == 0" 1 "unswitch" } } */ +/* { dg-final { scan-tree-dump-times ";; Unswitching loop on condition: order.* == 1" 1 "unswitch" } } */ +/* { dg-final { scan-tree-dump-times ";; Unswitching loop on condition: order.* == 2" 1 "unswitch" } } */ +/* { dg-final { scan-tree-dump-times ";; Unswitching loop on condition: order.* == 3" 1 "unswitch" } } */ diff --git a/gcc/tree-cfg.c b/gcc/tree-cfg.c index ebbd894ae03..4eaa5cf6c97 100644 --- a/gcc/tree-cfg.c +++ b/gcc/tree-cfg.c @@ -9063,11 +9063,16 @@ gimple_lv_add_condition_to_bb (basic_block first_head ATTRIBUTE_UNUSED, edge e0; /* Build new conditional expr */ + gsi = gsi_last_bb (cond_bb); + + cond_expr = force_gimple_operand_gsi_1 (&gsi, cond_expr, + is_gimple_condexpr_for_cond, + NULL_TREE, false, + GSI_CONTINUE_LINKING); new_cond_expr = gimple_build_cond_from_tree (cond_expr, NULL_TREE, NULL_TREE); /* Add new cond in cond_bb. */ - gsi = gsi_last_bb (cond_bb); gsi_insert_after (&gsi, new_cond_expr, GSI_NEW_STMT); /* Adjust edges appropriately to connect new head with first head diff --git a/gcc/tree-ssa-loop-unswitch.c b/gcc/tree-ssa-loop-unswitch.c index 2f9821751f0..50b7a607a79 100644 --- a/gcc/tree-ssa-loop-unswitch.c +++ b/gcc/tree-ssa-loop-unswitch.c @@ -365,9 +365,13 @@ find_unswitching_predicates_for_bb (basic_block bb, class loop *loop, extract_true_false_edges_from_block (bb, &edge_true, &edge_false); unswitch_predicate *predicate = new unswitch_predicate (cond, lhs); - ranger->gori().outgoing_edge_range_p (predicate->true_range, edge_true, - lhs, *get_global_range_query ()); - predicate->init_false_edge (); + + if (irange::supports_type_p (TREE_TYPE (lhs))) + { + ranger->gori().outgoing_edge_range_p (predicate->true_range, edge_true, + lhs, *get_global_range_query ()); + predicate->init_false_edge (); + } candidates.safe_push (predicate); }