1 PR tree-optimization/32044
4 Date: 2008-12-12 21:32:47 +0100
6 * tree-scalar-evolution.h (expression_expensive_p): Declare.
7 * tree-scalar-evolution.c (expression_expensive_p): New function.
8 (scev_const_prop): Avoid introducing expensive expressions.
9 * tree-ssa-loop-ivopts.c (may_eliminate_iv): Ditto.
11 * gcc.dg/pr34027-1.c: Change outcome.
12 * gcc.dg/tree-ssa/pr32044.c: New test.
14 cherry picked from svn://gcc.gnu.org/svn/gcc/trunk, rev 142719 and adapted to
17 ------------------------------------------------------------------------
18 Index: gcc-4.3.2/gcc/tree-scalar-evolution.c
19 ===================================================================
20 --- gcc-4.3.2.orig/gcc/tree-scalar-evolution.c 2009-01-28 10:14:37.000000000 +0100
21 +++ gcc-4.3.2/gcc/tree-scalar-evolution.c 2009-01-28 10:17:50.000000000 +0100
22 @@ -2716,6 +2716,50 @@
23 scalar_evolution_info = NULL;
26 +/* Returns true if the expression EXPR is considered to be too expensive
27 + for scev_const_prop. */
30 +expression_expensive_p (tree expr)
32 + enum tree_code code;
34 + if (is_gimple_val (expr))
37 + code = TREE_CODE (expr);
38 + if (code == TRUNC_DIV_EXPR
39 + || code == CEIL_DIV_EXPR
40 + || code == FLOOR_DIV_EXPR
41 + || code == ROUND_DIV_EXPR
42 + || code == TRUNC_MOD_EXPR
43 + || code == CEIL_MOD_EXPR
44 + || code == FLOOR_MOD_EXPR
45 + || code == ROUND_MOD_EXPR
46 + || code == EXACT_DIV_EXPR)
48 + /* Division by power of two is usually cheap, so we allow it.
49 + Forbid anything else. */
50 + if (!integer_pow2p (TREE_OPERAND (expr, 1)))
54 + switch (TREE_CODE_CLASS (code))
57 + case tcc_comparison:
58 + if (expression_expensive_p (TREE_OPERAND (expr, 1)))
63 + return expression_expensive_p (TREE_OPERAND (expr, 0));
70 /* Replace ssa names for that scev can prove they are constant by the
71 appropriate constants. Also perform final value replacement in loops,
72 in case the replacement expressions are cheap.
73 @@ -2802,12 +2846,6 @@
76 niter = number_of_latch_executions (loop);
77 - /* We used to check here whether the computation of NITER is expensive,
78 - and avoided final value elimination if that is the case. The problem
79 - is that it is hard to evaluate whether the expression is too
80 - expensive, as we do not know what optimization opportunities the
81 - the elimination of the final value may reveal. Therefore, we now
82 - eliminate the final values of induction variables unconditionally. */
83 if (niter == chrec_dont_know)
86 @@ -2838,7 +2876,15 @@
87 /* Moving the computation from the loop may prolong life range
88 of some ssa names, which may cause problems if they appear
90 - || contains_abnormal_ssa_name_p (def))
91 + || contains_abnormal_ssa_name_p (def)
92 + /* Do not emit expensive expressions. The rationale is that
93 + when someone writes a code like
95 + while (n > 45) n -= 45;
97 + he probably knows that n is not large, and does not want it
98 + to be turned into n %= 45. */
99 + || expression_expensive_p (def))
102 /* Eliminate the PHI node and replace it by a computation outside
103 Index: gcc-4.3.2/gcc/tree-scalar-evolution.h
104 ===================================================================
105 --- gcc-4.3.2.orig/gcc/tree-scalar-evolution.h 2009-01-28 10:22:47.000000000 +0100
106 +++ gcc-4.3.2/gcc/tree-scalar-evolution.h 2009-01-28 10:23:10.000000000 +0100
108 extern void scev_analysis (void);
109 unsigned int scev_const_prop (void);
111 +bool expression_expensive_p (tree);
112 extern bool simple_iv (struct loop *, tree, tree, affine_iv *, bool);
114 /* Returns the loop of the polynomial chrec CHREC. */
115 Index: gcc-4.3.2/gcc/testsuite/gcc.dg/pr34027-1.c
116 ===================================================================
117 --- gcc-4.3.2.orig/gcc/testsuite/gcc.dg/pr34027-1.c 2009-01-28 10:24:09.000000000 +0100
118 +++ gcc-4.3.2/gcc/testsuite/gcc.dg/pr34027-1.c 2009-01-28 10:24:43.000000000 +0100
123 -/* { dg-final { scan-tree-dump "ns % 10000" "optimized" } } */
124 +/* This test was originally introduced to test that we transform
125 + to ns % 10000. See the discussion of PR 32044 why we do not do
127 +/* { dg-final { scan-tree-dump-times "%" 0 "optimized" } } */
128 +/* { dg-final { scan-tree-dump-times "/" 0 "optimized" } } */
129 /* { dg-final { cleanup-tree-dump "optimized" } } */
130 Index: gcc-4.3.2/gcc/testsuite/gcc.dg/tree-ssa/pr32044.c
131 ===================================================================
132 --- /dev/null 1970-01-01 00:00:00.000000000 +0000
133 +++ gcc-4.3.2/gcc/testsuite/gcc.dg/tree-ssa/pr32044.c 2009-01-28 10:25:50.000000000 +0100
135 +/* { dg-do compile } */
136 +/* { dg-options "-O2 -fdump-tree-empty -fdump-tree-final_cleanup" } */
180 +/* The loops computing division/modulo by 64 should be eliminated. */
181 +/* { dg-final { scan-tree-dump-times "Removing empty loop" 2 "empty" } } */
183 +/* There should be no division/modulo in the final dump (division and modulo
184 + by 64 are done using bit operations). */
185 +/* { dg-final { scan-tree-dump-times "/" 0 "final_cleanup" } } */
186 +/* { dg-final { scan-tree-dump-times "%" 0 "final_cleanup" } } */
188 +/* { dg-final { cleanup-tree-dump "empty" } } */
189 +/* { dg-final { cleanup-tree-dump "final_cleanup" } } */
190 Index: gcc-4.3.2/gcc/tree-ssa-loop-ivopts.c
191 ===================================================================
192 --- gcc-4.3.2.orig/gcc/tree-ssa-loop-ivopts.c 2009-01-28 10:26:04.000000000 +0100
193 +++ gcc-4.3.2/gcc/tree-ssa-loop-ivopts.c 2009-01-28 10:27:09.000000000 +0100
194 @@ -3778,7 +3778,12 @@
197 cand_value_at (loop, cand, use->stmt, nit, &bnd);
199 *bound = aff_combination_to_tree (&bnd);
200 + /* It is unlikely that computing the number of iterations using division
201 + would be more profitable than keeping the original induction variable. */
202 + if (expression_expensive_p (*bound))