Hello, while checking why a loop snippet like
for (i = 0; i <= 5000; i++) if (b) a = 2; else a = x; was not optimized in -O2 (unless loop unrolling or loop switching), I found out that the case was already partially handled by Richard in PR43934. So this patch just adds a cost to the phi defs constants to allow the whole test to be hoisted out of the loop. Richard, does this seem reasonable and OK for 4.10 ? bootstrapped/regtested for x86 many thanks Christian
2014-06-03 Christian Bruel <christian.br...@st.com> PR tree-optimization/43934 * tree-ssa-loop-im.c (determine_max_movement): Add PHI def constant cost. 2014-06-03 Christian Bruel <christian.br...@st.com> PR tree-optimization/43934 * gcc.dg/tree-ssa/ssa-lim-8.c: New testcase. Index: gcc/testsuite/gcc.dg/tree-ssa/ssa-lim-8.c =================================================================== --- gcc/testsuite/gcc.dg/tree-ssa/ssa-lim-8.c (revision 0) +++ gcc/testsuite/gcc.dg/tree-ssa/ssa-lim-8.c (working copy) @@ -0,0 +1,20 @@ +/* { dg-do compile } */ +/* { dg-options "-O -fdump-tree-lim1-details" } */ + +void bar (int); +void foo (int n, int m) +{ + unsigned i; + for (i = 0; i < n; ++i) + { + int x; + if (m < 0) + x = 1; + else + x = m; + bar (x); + } +} + +/* { dg-final { scan-tree-dump-times "Moving PHI node" 1 "lim1" } } */ +/* { dg-final { cleanup-tree-dump "lim1" } } */ Index: gcc/tree-ssa-loop-im.c =================================================================== --- gcc/tree-ssa-loop-im.c (revision 211255) +++ gcc/tree-ssa-loop-im.c (working copy) @@ -719,8 +719,21 @@ determine_max_movement (gimple stmt, bool must_pre FOR_EACH_PHI_ARG (use_p, stmt, iter, SSA_OP_USE) { val = USE_FROM_PTR (use_p); + if (TREE_CODE (val) != SSA_NAME) - continue; + { + unsigned cst_cost = 1; + + gcc_assert (TREE_CODE (val) == INTEGER_CST + || TREE_CODE (val) == REAL_CST + || TREE_CODE (val) == VECTOR_CST + || TREE_CODE (val) == COMPLEX_CST + || TREE_CODE (val) == ADDR_EXPR); + + min_cost = MIN (min_cost, cst_cost); + total_cost += cst_cost; + continue; + } if (!add_dependency (val, lim_data, loop, false)) return false; def_data = get_lim_data (SSA_NAME_DEF_STMT (val));