Hi Jeff, Thanks for looking into it.
On 6 July 2018 at 08:03, Jeff Law <l...@redhat.com> wrote: > On 06/24/2018 08:41 PM, Kugan Vivekanandarajah wrote: >> Hi Jeff, >> >> Thanks for the comments. >> >> On 23 June 2018 at 02:06, Jeff Law <l...@redhat.com> wrote: >>> On 06/22/2018 03:11 AM, Kugan Vivekanandarajah wrote: >>>> When we set niter with maybe_zero, currently final_value_relacement >>>> will not happen due to expression_expensive_p not handling. Patch 1 >>>> adds this. >>>> >>>> With that we have the following optimized gimple. >>>> >>>> <bb 2> [local count: 118111601]: >>>> if (b_4(D) != 0) >>>> goto <bb 3>; [89.00%] >>>> else >>>> goto <bb 4>; [11.00%] >>>> >>>> <bb 3> [local count: 105119324]: >>>> _2 = (unsigned long) b_4(D); >>>> _9 = __builtin_popcountl (_2); >>>> c_3 = b_4(D) != 0 ? _9 : 1; >>>> >>>> <bb 4> [local count: 118111601]: >>>> # c_12 = PHI <c_3(3), 0(2)> >>>> >>>> I assume that 1 in b_4(D) != 0 ? _9 : 1; is OK (?) because when the >>>> latch execute zero times for b_4 == 0 means that the body will execute >>>> ones. >>> ISTM that DOM ought to have simplified the conditional, unless there's >>> some other way to get to bb3. We know that b_4 is nonzero and thus c_3 >>> must have the value _9. >> As of now, dom is not optimizing it. With the attached hack, it can be made >> to. > What's strange is I'm not getting the c_3 = (b_4 != 0) ... in any of the > dumps I'm looking at. Instead it's c_3 = _9, which is what I would > expect since we know that b_4 != 0 > > > My tests have been on x86_64 and aarch64 linux targets. I've tried with > patch#1 installed as well as with patch #1 and patch #2 together. > > What target, what flags and what patches do I need to see this? You need the patch 1 (attaching) to get that. With Patch 2 in this series, it will be optimized. I haven't committed the patches yet as I am testing all the three patches. I will commit after testing on current trunk. Thanks, Kugan > > Jeff
From 12263df77931aa55d205b9db470436848d762684 Mon Sep 17 00:00:00 2001 From: Kugan Vivekanandarajah <kugan.vivekanandara...@linaro.org> Date: Fri, 22 Jun 2018 14:10:26 +1000 Subject: [PATCH 1/3] generate popcount when checked for zero Change-Id: I951e6d487268b757cbdaa8dcf671ab1377490db6 --- gcc/gimplify.c | 2 +- gcc/gimplify.h | 1 + gcc/testsuite/gcc.dg/tree-ssa/pr64183.c | 2 +- gcc/testsuite/gcc.target/i386/pr85073.c | 2 +- gcc/tree-scalar-evolution.c | 12 ++++++++++++ 5 files changed, 16 insertions(+), 3 deletions(-) diff --git a/gcc/gimplify.c b/gcc/gimplify.c index 48ac92e..c86ad1a 100644 --- a/gcc/gimplify.c +++ b/gcc/gimplify.c @@ -3878,7 +3878,7 @@ gimplify_pure_cond_expr (tree *expr_p, gimple_seq *pre_p) EXPR is GENERIC, while tree_could_trap_p can be called only on GIMPLE. */ -static bool +bool generic_expr_could_trap_p (tree expr) { unsigned i, n; diff --git a/gcc/gimplify.h b/gcc/gimplify.h index dd0e4c0..62ca869 100644 --- a/gcc/gimplify.h +++ b/gcc/gimplify.h @@ -83,6 +83,7 @@ extern enum gimplify_status gimplify_arg (tree *, gimple_seq *, location_t, extern void gimplify_function_tree (tree); extern enum gimplify_status gimplify_va_arg_expr (tree *, gimple_seq *, gimple_seq *); +extern bool generic_expr_could_trap_p (tree expr); gimple *gimplify_assign (tree, tree, gimple_seq *); #endif /* GCC_GIMPLIFY_H */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr64183.c b/gcc/testsuite/gcc.dg/tree-ssa/pr64183.c index 7a854fc..50d0c5a 100644 --- a/gcc/testsuite/gcc.dg/tree-ssa/pr64183.c +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr64183.c @@ -1,5 +1,5 @@ /* { dg-do compile } */ -/* { dg-options "-O3 -fno-tree-vectorize -fdump-tree-cunroll-details" } */ +/* { dg-options "-O3 -fno-tree-vectorize -fdisable-tree-sccp -fdump-tree-cunroll-details" } */ int bits; unsigned int size; diff --git a/gcc/testsuite/gcc.target/i386/pr85073.c b/gcc/testsuite/gcc.target/i386/pr85073.c index 187102d..71a5d23 100644 --- a/gcc/testsuite/gcc.target/i386/pr85073.c +++ b/gcc/testsuite/gcc.target/i386/pr85073.c @@ -1,6 +1,6 @@ /* PR target/85073 */ /* { dg-do compile } */ -/* { dg-options "-O2 -mbmi" } */ +/* { dg-options "-O2 -mbmi -fdisable-tree-sccp" } */ int foo (unsigned x) diff --git a/gcc/tree-scalar-evolution.c b/gcc/tree-scalar-evolution.c index 4b0ec02..8e29005 100644 --- a/gcc/tree-scalar-evolution.c +++ b/gcc/tree-scalar-evolution.c @@ -3508,6 +3508,18 @@ expression_expensive_p (tree expr) return false; } + if (code == COND_EXPR) + return (expression_expensive_p (TREE_OPERAND (expr, 0)) + || (EXPR_P (TREE_OPERAND (expr, 1)) + && EXPR_P (TREE_OPERAND (expr, 2))) + /* If either branch has side effects or could trap. */ + || TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1)) + || generic_expr_could_trap_p (TREE_OPERAND (expr, 1)) + || TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 0)) + || generic_expr_could_trap_p (TREE_OPERAND (expr, 0)) + || expression_expensive_p (TREE_OPERAND (expr, 1)) + || expression_expensive_p (TREE_OPERAND (expr, 2))); + switch (TREE_CODE_CLASS (code)) { case tcc_binary: -- 2.7.4