/* Scalar evolution detector.
- Copyright (C) 2003, 2004 Free Software Foundation, Inc.
+ Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
Contributed by Sebastian Pop <s.pop@laposte.net>
This file is part of GCC.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING. If not, write to the Free
-Software Foundation, 59 Temple Place - Suite 330, Boston, MA
-02111-1307, USA. */
+Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
+02110-1301, USA. */
/*
Description:
#include "system.h"
#include "coretypes.h"
#include "tm.h"
-#include "errors.h"
#include "ggc.h"
#include "tree.h"
+#include "real.h"
/* These RTL headers are needed for basic-block.h. */
#include "rtl.h"
/* Get the index corresponding to VAR in the current LOOP. If
it's the first time we ask for this VAR, then we return
- chrec_not_analysed_yet for this VAR and return its index. */
+ chrec_not_analyzed_yet for this VAR and return its index. */
static tree *
find_var_scev_info (tree var)
return &res->chrec;
}
-/* Tries to express CHREC in wider type TYPE. */
-
-tree
-count_ev_in_wider_type (tree type, tree chrec)
-{
- tree base, step;
- struct loop *loop;
-
- if (!evolution_function_is_affine_p (chrec))
- return fold_convert (type, chrec);
-
- base = CHREC_LEFT (chrec);
- step = CHREC_RIGHT (chrec);
- loop = current_loops->parray[CHREC_VARIABLE (chrec)];
-
- /* TODO -- if we knew the statement at that the conversion occurs,
- we could pass it to can_count_iv_in_wider_type and get a better
- result. */
- step = can_count_iv_in_wider_type (loop, type, base, step, NULL_TREE);
- if (!step)
- return fold_convert (type, chrec);
- base = chrec_convert (type, base);
-
- return build_polynomial_chrec (CHREC_VARIABLE (chrec),
- base, step);
-}
-
/* Return true when CHREC contains symbolic names defined in
LOOP_NB. */
/* Number of iterations is off by one (the ssa name we
analyze must be defined before the exit). */
nb_iter = chrec_fold_minus (chrec_type (nb_iter),
- nb_iter,
- fold_convert (chrec_type (nb_iter),
- integer_one_node));
+ nb_iter,
+ build_int_cst_type (chrec_type (nb_iter), 1));
/* evolution_fn is the evolution function in LOOP. Get
its value in the nb_iter-th iteration. */
nb_iter = chrec_fold_minus
(chrec_type (nb_iter), nb_iter,
- fold_convert (chrec_type (nb_iter), integer_one_node));
+ build_int_cst (chrec_type (nb_iter), 1));
#if 0
/* TODO -- If the test is after the exit, we may decrease the number of
if (after_exit)
nb_iter = chrec_fold_minus
(chrec_type (nb_iter), nb_iter,
- fold_convert (chrec_type (nb_iter), integer_one_node));
+ build_int_cst (chrec_type (nb_iter), 1));
#endif
end_value = chrec_apply (CHREC_VARIABLE (chrec), chrec, nb_iter);
{
var = loop_nb;
left = chrec_before;
- right = fold_convert (type, integer_zero_node);
+ right = build_int_cst (type, 0);
}
else
{
}
if (code == MINUS_EXPR)
- to_add = chrec_fold_multiply (type, to_add,
- fold_convert (type, integer_minus_one_node));
+ to_add = chrec_fold_multiply (type, to_add, SCALAR_FLOAT_TYPE_P (type)
+ ? build_real (type, dconstm1)
+ : build_int_cst_type (type, -1));
res = add_to_evolution_1 (loop_nb, chrec_before, to_add);
set_nb_iterations_in_loop (struct loop *loop,
tree res)
{
- res = chrec_fold_plus (chrec_type (res), res, integer_one_node);
+ res = chrec_fold_plus (chrec_type (res), res,
+ build_int_cst_type (chrec_type (res), 1));
+
/* FIXME HWI: However we want to store one iteration less than the
count of the loop in order to be compatible with the other
nb_iter computations in loop-iv. This also allows the
representation of nb_iters that are equal to MAX_INT. */
- if ((TREE_CODE (res) == INTEGER_CST && TREE_INT_CST_LOW (res) == 0)
- || TREE_OVERFLOW (res))
+ if (TREE_CODE (res) == INTEGER_CST
+ && (TREE_INT_CST_LOW (res) == 0
+ || TREE_OVERFLOW (res)))
res = chrec_dont_know;
if (dump_file && (dump_flags & TDF_DETAILS))
switch (TREE_CODE (condition))
{
case SSA_NAME:
- /* Volatile expressions are not analyzable. */
- if (TREE_THIS_VOLATILE (SSA_NAME_VAR (condition)))
- return false;
return true;
case LT_EXPR:
case GE_EXPR:
case EQ_EXPR:
case NE_EXPR:
- {
- tree opnd0, opnd1;
-
- opnd0 = TREE_OPERAND (condition, 0);
- opnd1 = TREE_OPERAND (condition, 1);
-
- if (TREE_CODE (opnd0) == SSA_NAME
- && TREE_THIS_VOLATILE (SSA_NAME_VAR (opnd0)))
- return false;
-
- if (TREE_CODE (opnd1) == SSA_NAME
- && TREE_THIS_VOLATILE (SSA_NAME_VAR (opnd1)))
- return false;
-
- return true;
- }
+ return true;
default:
return false;
static void
get_exit_conditions_rec (struct loop *loop,
- varray_type *exit_conditions)
+ VEC(tree,heap) **exit_conditions)
{
if (!loop)
return;
tree loop_condition = get_loop_exit_condition (loop);
if (loop_condition)
- VARRAY_PUSH_TREE (*exit_conditions, loop_condition);
+ VEC_safe_push (tree, heap, *exit_conditions, loop_condition);
}
}
/* Select the candidate loop nests for the analysis. This function
- initializes the EXIT_CONDITIONS array. */
+ initializes the EXIT_CONDITIONS array. */
static void
select_loops_exit_conditions (struct loops *loops,
- varray_type *exit_conditions)
+ VEC(tree,heap) **exit_conditions)
{
struct loop *function_body = loops->parray[0];
static bool
follow_ssa_edge_in_rhs (struct loop *loop,
+ tree at_stmt,
tree rhs,
tree halting_phi,
tree *evolution_of_loop)
- an INTEGER_CST,
- a PLUS_EXPR,
- a MINUS_EXPR,
- - other cases are not yet handled.
- */
+ - an ASSERT_EXPR,
+ - other cases are not yet handled. */
switch (TREE_CODE (rhs))
{
case NOP_EXPR:
/* This assignment is under the form "a_1 = (cast) rhs. */
- res = follow_ssa_edge_in_rhs (loop, TREE_OPERAND (rhs, 0), halting_phi,
- evolution_of_loop);
- *evolution_of_loop = chrec_convert (TREE_TYPE (rhs), *evolution_of_loop);
+ res = follow_ssa_edge_in_rhs (loop, at_stmt, TREE_OPERAND (rhs, 0),
+ halting_phi, evolution_of_loop);
+ *evolution_of_loop = chrec_convert (TREE_TYPE (rhs),
+ *evolution_of_loop, at_stmt);
break;
case INTEGER_CST:
if (res)
*evolution_of_loop = add_to_evolution
(loop->num,
- chrec_convert (type_rhs, *evolution_of_loop),
+ chrec_convert (type_rhs, *evolution_of_loop, at_stmt),
PLUS_EXPR, rhs1);
else
if (res)
*evolution_of_loop = add_to_evolution
(loop->num,
- chrec_convert (type_rhs, *evolution_of_loop),
+ chrec_convert (type_rhs, *evolution_of_loop, at_stmt),
PLUS_EXPR, rhs0);
}
}
evolution_of_loop);
if (res)
*evolution_of_loop = add_to_evolution
- (loop->num, chrec_convert (type_rhs, *evolution_of_loop),
+ (loop->num, chrec_convert (type_rhs, *evolution_of_loop,
+ at_stmt),
PLUS_EXPR, rhs1);
}
}
evolution_of_loop);
if (res)
*evolution_of_loop = add_to_evolution
- (loop->num, chrec_convert (type_rhs, *evolution_of_loop),
+ (loop->num, chrec_convert (type_rhs, *evolution_of_loop,
+ at_stmt),
PLUS_EXPR, rhs0);
}
if (TREE_CODE (rhs0) == SSA_NAME)
{
- if (TREE_CODE (rhs1) == SSA_NAME)
- {
- /* Match an assignment under the form:
- "a = b - c". */
- res = follow_ssa_edge
- (loop, SSA_NAME_DEF_STMT (rhs0), halting_phi,
- evolution_of_loop);
-
- if (res)
- *evolution_of_loop = add_to_evolution
- (loop->num, chrec_convert (type_rhs, *evolution_of_loop),
- MINUS_EXPR, rhs1);
-
- else
- {
- res = follow_ssa_edge
- (loop, SSA_NAME_DEF_STMT (rhs1), halting_phi,
- evolution_of_loop);
-
- if (res)
- *evolution_of_loop = add_to_evolution
- (loop->num,
- chrec_fold_multiply (type_rhs,
- *evolution_of_loop,
- fold_convert (type_rhs,
- integer_minus_one_node)),
- PLUS_EXPR, rhs0);
- }
- }
-
- else
- {
- /* Match an assignment under the form:
- "a = b - ...". */
- res = follow_ssa_edge
- (loop, SSA_NAME_DEF_STMT (rhs0), halting_phi,
- evolution_of_loop);
- if (res)
- *evolution_of_loop = add_to_evolution
- (loop->num, chrec_convert (type_rhs, *evolution_of_loop),
- MINUS_EXPR, rhs1);
- }
- }
-
- else if (TREE_CODE (rhs1) == SSA_NAME)
- {
/* Match an assignment under the form:
- "a = ... - c". */
- res = follow_ssa_edge
- (loop, SSA_NAME_DEF_STMT (rhs1), halting_phi,
- evolution_of_loop);
+ "a = b - ...". */
+ res = follow_ssa_edge (loop, SSA_NAME_DEF_STMT (rhs0), halting_phi,
+ evolution_of_loop);
if (res)
*evolution_of_loop = add_to_evolution
- (loop->num,
- chrec_fold_multiply (type_rhs,
- *evolution_of_loop,
- fold_convert (type_rhs, integer_minus_one_node)),
- PLUS_EXPR, rhs0);
+ (loop->num, chrec_convert (type_rhs, *evolution_of_loop,
+ at_stmt),
+ MINUS_EXPR, rhs1);
}
-
else
/* Otherwise, match an assignment under the form:
"a = ... - ...". */
break;
+ case ASSERT_EXPR:
+ {
+ /* This assignment is of the form: "a_1 = ASSERT_EXPR <a_2, ...>"
+ It must be handled as a copy assignment of the form a_1 = a_2. */
+ tree op0 = ASSERT_EXPR_VAR (rhs);
+ if (TREE_CODE (op0) == SSA_NAME)
+ res = follow_ssa_edge (loop, SSA_NAME_DEF_STMT (op0),
+ halting_phi, evolution_of_loop);
+ else
+ res = false;
+ break;
+ }
+
+
default:
res = false;
break;
}
/* This case occurs when one of the condition branches sets
- the variable to a constant: ie. a phi-node like
+ the variable to a constant: i.e. a phi-node like
"a_2 = PHI <a_7(5), 2(6)>;".
FIXME: This case have to be refined correctly:
for (i = 1; i < PHI_NUM_ARGS (condition_phi); i++)
{
+ /* Quickly give up when the evolution of one of the branches is
+ not known. */
+ if (*evolution_of_loop == chrec_dont_know)
+ return true;
+
if (!follow_ssa_edge_in_condition_phi_branch (i, loop, condition_phi,
halting_phi,
&evolution_of_branch,
/* Follow the edges that exit the inner loop. */
bb = PHI_ARG_EDGE (loop_phi_node, i)->src;
if (!flow_bb_inside_loop_p (loop, bb))
- res = res || follow_ssa_edge_in_rhs (outer_loop, arg, halting_phi,
+ res = res || follow_ssa_edge_in_rhs (outer_loop, loop_phi_node,
+ arg, halting_phi,
evolution_of_loop);
}
/* Otherwise, compute the overall effect of the inner loop. */
ev = compute_overall_effect_of_inner_loop (loop, ev);
- return follow_ssa_edge_in_rhs (outer_loop, ev, halting_phi,
+ return follow_ssa_edge_in_rhs (outer_loop, loop_phi_node, ev, halting_phi,
evolution_of_loop);
}
return true;
/* Otherwise, the evolution of the HALTING_PHI depends
- on the evolution of another loop-phi-node, ie. the
+ on the evolution of another loop-phi-node, i.e. the
evolution function is a higher degree polynomial. */
if (def_loop == loop)
return false;
return false;
case MODIFY_EXPR:
- return follow_ssa_edge_in_rhs (loop,
+ return follow_ssa_edge_in_rhs (loop, def,
TREE_OPERAND (def, 1),
halting_phi,
evolution_of_loop);
/* When it is impossible to go back on the same
loop_phi_node by following the ssa edges, the
- evolution is represented by a peeled chrec, ie. the
+ evolution is represented by a peeled chrec, i.e. the
first iteration, EV_FN has the value INIT_COND, then
all the other iterations it has the value of ARG.
For the moment, PEELED_CHREC nodes are not built. */
}
/* Interpret the right hand side of a modify_expr OPND1. If we didn't
- analyzed this node before, follow the definitions until ending
+ analyze this node before, follow the definitions until ending
either on an analyzed modify_expr, or on a loop-phi-node. On the
return path, this function propagates evolutions (ala constant copy
propagation). OPND1 is not a GIMPLE expression because we could
analyze the effect of an inner loop: see interpret_loop_phi. */
static tree
-interpret_rhs_modify_expr (struct loop *loop,
+interpret_rhs_modify_expr (struct loop *loop, tree at_stmt,
tree opnd1, tree type)
{
tree res, opnd10, opnd11, chrec10, chrec11;
-
+
if (is_gimple_min_invariant (opnd1))
- return chrec_convert (type, opnd1);
-
+ return chrec_convert (type, opnd1, at_stmt);
+
switch (TREE_CODE (opnd1))
{
case PLUS_EXPR:
opnd11 = TREE_OPERAND (opnd1, 1);
chrec10 = analyze_scalar_evolution (loop, opnd10);
chrec11 = analyze_scalar_evolution (loop, opnd11);
- chrec10 = chrec_convert (type, chrec10);
- chrec11 = chrec_convert (type, chrec11);
+ chrec10 = chrec_convert (type, chrec10, at_stmt);
+ chrec11 = chrec_convert (type, chrec11, at_stmt);
res = chrec_fold_plus (type, chrec10, chrec11);
break;
opnd11 = TREE_OPERAND (opnd1, 1);
chrec10 = analyze_scalar_evolution (loop, opnd10);
chrec11 = analyze_scalar_evolution (loop, opnd11);
- chrec10 = chrec_convert (type, chrec10);
- chrec11 = chrec_convert (type, chrec11);
+ chrec10 = chrec_convert (type, chrec10, at_stmt);
+ chrec11 = chrec_convert (type, chrec11, at_stmt);
res = chrec_fold_minus (type, chrec10, chrec11);
break;
case NEGATE_EXPR:
opnd10 = TREE_OPERAND (opnd1, 0);
chrec10 = analyze_scalar_evolution (loop, opnd10);
- chrec10 = chrec_convert (type, chrec10);
- res = chrec_fold_minus (type, fold_convert (type, integer_zero_node),
- chrec10);
+ chrec10 = chrec_convert (type, chrec10, at_stmt);
+ res = chrec_fold_minus (type, build_int_cst (type, 0), chrec10);
break;
case MULT_EXPR:
opnd11 = TREE_OPERAND (opnd1, 1);
chrec10 = analyze_scalar_evolution (loop, opnd10);
chrec11 = analyze_scalar_evolution (loop, opnd11);
- chrec10 = chrec_convert (type, chrec10);
- chrec11 = chrec_convert (type, chrec11);
+ chrec10 = chrec_convert (type, chrec10, at_stmt);
+ chrec11 = chrec_convert (type, chrec11, at_stmt);
res = chrec_fold_multiply (type, chrec10, chrec11);
break;
case SSA_NAME:
- res = chrec_convert (type, analyze_scalar_evolution (loop, opnd1));
+ res = chrec_convert (type, analyze_scalar_evolution (loop, opnd1),
+ at_stmt);
+ break;
+
+ case ASSERT_EXPR:
+ opnd10 = ASSERT_EXPR_VAR (opnd1);
+ res = chrec_convert (type, analyze_scalar_evolution (loop, opnd10),
+ at_stmt);
break;
case NOP_EXPR:
case CONVERT_EXPR:
opnd10 = TREE_OPERAND (opnd1, 0);
chrec10 = analyze_scalar_evolution (loop, opnd10);
- res = chrec_convert (type, chrec10);
+ res = chrec_convert (type, chrec10, at_stmt);
break;
default:
return chrec_dont_know;
if (TREE_CODE (var) != SSA_NAME)
- return interpret_rhs_modify_expr (loop, var, type);
+ return interpret_rhs_modify_expr (loop, NULL_TREE, var, type);
def = SSA_NAME_DEF_STMT (var);
bb = bb_for_stmt (def);
switch (TREE_CODE (def))
{
case MODIFY_EXPR:
- res = interpret_rhs_modify_expr (loop, TREE_OPERAND (def, 1), type);
+ res = interpret_rhs_modify_expr (loop, def, TREE_OPERAND (def, 1), type);
break;
case PHI_NODE:
}
}
+/* Returns instantiated value for VERSION in CACHE. */
+
+static tree
+get_instantiated_value (htab_t cache, tree version)
+{
+ struct scev_info_str *info, pattern;
+
+ pattern.var = version;
+ info = htab_find (cache, &pattern);
+
+ if (info)
+ return info->chrec;
+ else
+ return NULL_TREE;
+}
+
+/* Sets instantiated value for VERSION to VAL in CACHE. */
+
+static void
+set_instantiated_value (htab_t cache, tree version, tree val)
+{
+ struct scev_info_str *info, pattern;
+ PTR *slot;
+
+ pattern.var = version;
+ slot = htab_find_slot (cache, &pattern, INSERT);
+
+ if (*slot)
+ info = *slot;
+ else
+ info = *slot = new_scev_info_str (version);
+ info->chrec = val;
+}
+
/* Analyze all the parameters of the chrec that were left under a symbolic form,
with respect to LOOP. CHREC is the chrec to instantiate. If
ALLOW_SUPERLOOP_CHRECS is true, replacing loop invariants with
- outer loop chrecs is done. */
+ outer loop chrecs is done. CACHE is the cache of already instantiated
+ values. */
static tree
instantiate_parameters_1 (struct loop *loop, tree chrec,
- bool allow_superloop_chrecs)
+ bool allow_superloop_chrecs,
+ htab_t cache)
{
tree res, op0, op1, op2;
basic_block def_bb;
struct loop *def_loop;
-
+
if (chrec == NULL_TREE
|| automatically_generated_chrec_p (chrec))
return chrec;
&& !flow_bb_inside_loop_p (loop, def_bb)))
return chrec;
- /* Don't instantiate the SSA_NAME if it is in a mixer
+ /* We cache the value of instantiated variable to avoid exponential
+ time complexity due to reevaluations. We also store the convenient
+ value in the cache in order to prevent infinite recursion -- we do
+ not want to instantiate the SSA_NAME if it is in a mixer
structure. This is used for avoiding the instantiation of
recursively defined functions, such as:
| a_2 -> {0, +, 1, +, a_2}_1 */
-
+
+ res = get_instantiated_value (cache, chrec);
+ if (res)
+ return res;
+
+ /* Store the convenient value for chrec in the structure. If it
+ is defined outside of the loop, we may just leave it in symbolic
+ form, otherwise we need to admit that we do not know its behavior
+ inside the loop. */
+ res = !flow_bb_inside_loop_p (loop, def_bb) ? chrec : chrec_dont_know;
+ set_instantiated_value (cache, chrec, res);
+
+ /* To make things even more complicated, instantiate_parameters_1
+ calls analyze_scalar_evolution that may call # of iterations
+ analysis that may in turn call instantiate_parameters_1 again.
+ To prevent the infinite recursion, keep also the bitmap of
+ ssa names that are being instantiated globally. */
if (bitmap_bit_p (already_instantiated, SSA_NAME_VERSION (chrec)))
- {
- if (!flow_bb_inside_loop_p (loop, def_bb))
- {
- /* We may keep the loop invariant in symbolic form. */
- return chrec;
- }
- else
- {
- /* Something with unknown behavior in LOOP. */
- return chrec_dont_know;
- }
- }
+ return res;
def_loop = find_common_loop (loop, def_bb->loop_father);
/* If the analysis yields a parametric chrec, instantiate the
- result again. Avoid the cyclic instantiation in mixers. */
+ result again. */
bitmap_set_bit (already_instantiated, SSA_NAME_VERSION (chrec));
res = analyze_scalar_evolution (def_loop, chrec);
- res = instantiate_parameters_1 (loop, res, allow_superloop_chrecs);
+ if (res != chrec_dont_know)
+ res = instantiate_parameters_1 (loop, res, allow_superloop_chrecs,
+ cache);
bitmap_clear_bit (already_instantiated, SSA_NAME_VERSION (chrec));
+
+ /* Store the correct value to the cache. */
+ set_instantiated_value (cache, chrec, res);
return res;
case POLYNOMIAL_CHREC:
op0 = instantiate_parameters_1 (loop, CHREC_LEFT (chrec),
- allow_superloop_chrecs);
+ allow_superloop_chrecs, cache);
+ if (op0 == chrec_dont_know)
+ return chrec_dont_know;
+
op1 = instantiate_parameters_1 (loop, CHREC_RIGHT (chrec),
- allow_superloop_chrecs);
- return build_polynomial_chrec (CHREC_VARIABLE (chrec), op0, op1);
+ allow_superloop_chrecs, cache);
+ if (op1 == chrec_dont_know)
+ return chrec_dont_know;
+
+ if (CHREC_LEFT (chrec) != op0
+ || CHREC_RIGHT (chrec) != op1)
+ chrec = build_polynomial_chrec (CHREC_VARIABLE (chrec), op0, op1);
+ return chrec;
case PLUS_EXPR:
op0 = instantiate_parameters_1 (loop, TREE_OPERAND (chrec, 0),
- allow_superloop_chrecs);
+ allow_superloop_chrecs, cache);
+ if (op0 == chrec_dont_know)
+ return chrec_dont_know;
+
op1 = instantiate_parameters_1 (loop, TREE_OPERAND (chrec, 1),
- allow_superloop_chrecs);
- return chrec_fold_plus (TREE_TYPE (chrec), op0, op1);
+ allow_superloop_chrecs, cache);
+ if (op1 == chrec_dont_know)
+ return chrec_dont_know;
+
+ if (TREE_OPERAND (chrec, 0) != op0
+ || TREE_OPERAND (chrec, 1) != op1)
+ chrec = chrec_fold_plus (TREE_TYPE (chrec), op0, op1);
+ return chrec;
case MINUS_EXPR:
op0 = instantiate_parameters_1 (loop, TREE_OPERAND (chrec, 0),
- allow_superloop_chrecs);
+ allow_superloop_chrecs, cache);
+ if (op0 == chrec_dont_know)
+ return chrec_dont_know;
+
op1 = instantiate_parameters_1 (loop, TREE_OPERAND (chrec, 1),
- allow_superloop_chrecs);
- return chrec_fold_minus (TREE_TYPE (chrec), op0, op1);
+ allow_superloop_chrecs, cache);
+ if (op1 == chrec_dont_know)
+ return chrec_dont_know;
+
+ if (TREE_OPERAND (chrec, 0) != op0
+ || TREE_OPERAND (chrec, 1) != op1)
+ chrec = chrec_fold_minus (TREE_TYPE (chrec), op0, op1);
+ return chrec;
case MULT_EXPR:
op0 = instantiate_parameters_1 (loop, TREE_OPERAND (chrec, 0),
- allow_superloop_chrecs);
+ allow_superloop_chrecs, cache);
+ if (op0 == chrec_dont_know)
+ return chrec_dont_know;
+
op1 = instantiate_parameters_1 (loop, TREE_OPERAND (chrec, 1),
- allow_superloop_chrecs);
- return chrec_fold_multiply (TREE_TYPE (chrec), op0, op1);
+ allow_superloop_chrecs, cache);
+ if (op1 == chrec_dont_know)
+ return chrec_dont_know;
+
+ if (TREE_OPERAND (chrec, 0) != op0
+ || TREE_OPERAND (chrec, 1) != op1)
+ chrec = chrec_fold_multiply (TREE_TYPE (chrec), op0, op1);
+ return chrec;
case NOP_EXPR:
case CONVERT_EXPR:
case NON_LVALUE_EXPR:
op0 = instantiate_parameters_1 (loop, TREE_OPERAND (chrec, 0),
- allow_superloop_chrecs);
+ allow_superloop_chrecs, cache);
if (op0 == chrec_dont_know)
return chrec_dont_know;
- return chrec_convert (TREE_TYPE (chrec), op0);
+ if (op0 == TREE_OPERAND (chrec, 0))
+ return chrec;
+
+ return chrec_convert (TREE_TYPE (chrec), op0, NULL_TREE);
case SCEV_NOT_KNOWN:
return chrec_dont_know;
{
case 3:
op0 = instantiate_parameters_1 (loop, TREE_OPERAND (chrec, 0),
- allow_superloop_chrecs);
+ allow_superloop_chrecs, cache);
+ if (op0 == chrec_dont_know)
+ return chrec_dont_know;
+
op1 = instantiate_parameters_1 (loop, TREE_OPERAND (chrec, 1),
- allow_superloop_chrecs);
+ allow_superloop_chrecs, cache);
+ if (op1 == chrec_dont_know)
+ return chrec_dont_know;
+
op2 = instantiate_parameters_1 (loop, TREE_OPERAND (chrec, 2),
- allow_superloop_chrecs);
- if (op0 == chrec_dont_know
- || op1 == chrec_dont_know
- || op2 == chrec_dont_know)
+ allow_superloop_chrecs, cache);
+ if (op2 == chrec_dont_know)
return chrec_dont_know;
- return fold (build (TREE_CODE (chrec),
- TREE_TYPE (chrec), op0, op1, op2));
+
+ if (op0 == TREE_OPERAND (chrec, 0)
+ && op1 == TREE_OPERAND (chrec, 1)
+ && op2 == TREE_OPERAND (chrec, 2))
+ return chrec;
+
+ return fold_build3 (TREE_CODE (chrec),
+ TREE_TYPE (chrec), op0, op1, op2);
case 2:
op0 = instantiate_parameters_1 (loop, TREE_OPERAND (chrec, 0),
- allow_superloop_chrecs);
+ allow_superloop_chrecs, cache);
+ if (op0 == chrec_dont_know)
+ return chrec_dont_know;
+
op1 = instantiate_parameters_1 (loop, TREE_OPERAND (chrec, 1),
- allow_superloop_chrecs);
- if (op0 == chrec_dont_know
- || op1 == chrec_dont_know)
+ allow_superloop_chrecs, cache);
+ if (op1 == chrec_dont_know)
return chrec_dont_know;
- return fold (build (TREE_CODE (chrec), TREE_TYPE (chrec), op0, op1));
+
+ if (op0 == TREE_OPERAND (chrec, 0)
+ && op1 == TREE_OPERAND (chrec, 1))
+ return chrec;
+ return fold_build2 (TREE_CODE (chrec), TREE_TYPE (chrec), op0, op1);
case 1:
op0 = instantiate_parameters_1 (loop, TREE_OPERAND (chrec, 0),
- allow_superloop_chrecs);
+ allow_superloop_chrecs, cache);
if (op0 == chrec_dont_know)
return chrec_dont_know;
- return fold (build1 (TREE_CODE (chrec), TREE_TYPE (chrec), op0));
+ if (op0 == TREE_OPERAND (chrec, 0))
+ return chrec;
+ return fold_build1 (TREE_CODE (chrec), TREE_TYPE (chrec), op0);
case 0:
return chrec;
tree chrec)
{
tree res;
+ htab_t cache = htab_create (10, hash_scev_info, eq_scev_info, del_scev_info);
if (dump_file && (dump_flags & TDF_DETAILS))
{
fprintf (dump_file, ")\n");
}
- res = instantiate_parameters_1 (loop, chrec, true);
+ res = instantiate_parameters_1 (loop, chrec, true, cache);
if (dump_file && (dump_flags & TDF_DETAILS))
{
print_generic_expr (dump_file, res, 0);
fprintf (dump_file, "))\n");
}
+
+ htab_delete (cache);
return res;
}
static tree
resolve_mixers (struct loop *loop, tree chrec)
{
- return instantiate_parameters_1 (loop, chrec, false);
+ htab_t cache = htab_create (10, hash_scev_info, eq_scev_info, del_scev_info);
+ tree ret = instantiate_parameters_1 (loop, chrec, false, cache);
+ htab_delete (cache);
+ return ret;
}
/* Entry point for the analysis of the number of iterations pass.
if (!exit)
goto end;
- if (!number_of_iterations_exit (loop, exit, &niter_desc))
+ if (!number_of_iterations_exit (loop, exit, &niter_desc, false))
goto end;
type = TREE_TYPE (niter_desc.niter);
if (integer_nonzerop (niter_desc.may_be_zero))
- res = fold_convert (type, integer_zero_node);
+ res = build_int_cst (type, 0);
else if (integer_zerop (niter_desc.may_be_zero))
res = niter_desc.niter;
else
from the EXIT_CONDITIONS array. */
static void
-number_of_iterations_for_all_loops (varray_type exit_conditions)
+number_of_iterations_for_all_loops (VEC(tree,heap) **exit_conditions)
{
unsigned int i;
unsigned nb_chrec_dont_know_loops = 0;
unsigned nb_static_loops = 0;
+ tree cond;
- for (i = 0; i < VARRAY_ACTIVE_SIZE (exit_conditions); i++)
+ for (i = 0; VEC_iterate (tree, *exit_conditions, i, cond); i++)
{
- tree res = number_of_iterations_in_loop
- (loop_containing_stmt (VARRAY_TREE (exit_conditions, i)));
+ tree res = number_of_iterations_in_loop (loop_containing_stmt (cond));
if (chrec_contains_undetermined (res))
nb_chrec_dont_know_loops++;
else
index. This allows the parallelization of the loop. */
static void
-analyze_scalar_evolution_for_all_loop_phi_nodes (varray_type exit_conditions)
+analyze_scalar_evolution_for_all_loop_phi_nodes (VEC(tree,heap) **exit_conditions)
{
unsigned int i;
struct chrec_stats stats;
+ tree cond;
reset_chrecs_counters (&stats);
- for (i = 0; i < VARRAY_ACTIVE_SIZE (exit_conditions); i++)
+ for (i = 0; VEC_iterate (tree, *exit_conditions, i, cond); i++)
{
struct loop *loop;
basic_block bb;
tree phi, chrec;
- loop = loop_containing_stmt (VARRAY_TREE (exit_conditions, i));
+ loop = loop_containing_stmt (cond);
bb = loop->header;
- for (phi = phi_nodes (bb); phi; phi = TREE_CHAIN (phi))
+ for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
if (is_gimple_reg (PHI_RESULT (phi)))
{
chrec = instantiate_parameters
chrec_not_analyzed_yet = NULL_TREE;
chrec_dont_know = make_node (SCEV_NOT_KNOWN);
chrec_known = make_node (SCEV_KNOWN);
- TREE_TYPE (chrec_dont_know) = NULL_TREE;
- TREE_TYPE (chrec_known) = NULL_TREE;
+ TREE_TYPE (chrec_dont_know) = void_type_node;
+ TREE_TYPE (chrec_known) = void_type_node;
}
}
scalar_evolution_info = htab_create (100, hash_scev_info,
eq_scev_info, del_scev_info);
- already_instantiated = BITMAP_XMALLOC ();
+ already_instantiated = BITMAP_ALLOC (NULL);
initialize_scalar_evolutions_analyzer ();
}
/* Checks whether OP behaves as a simple affine iv of LOOP in STMT and returns
- its BASE and STEP if possible. */
+ its BASE and STEP if possible. If ALLOW_NONCONSTANT_STEP is true, we
+ want STEP to be invariant in LOOP. Otherwise we require it to be an
+ integer constant. */
bool
-simple_iv (struct loop *loop, tree stmt, tree op, tree *base, tree *step)
+simple_iv (struct loop *loop, tree stmt, tree op, tree *base, tree *step,
+ bool allow_nonconstant_step)
{
basic_block bb = bb_for_stmt (stmt);
tree type, ev;
return false;
*step = CHREC_RIGHT (ev);
- if (TREE_CODE (*step) != INTEGER_CST)
+ if (allow_nonconstant_step)
+ {
+ if (tree_contains_chrecs (*step, NULL)
+ || chrec_contains_symbols_defined_in_loop (*step, loop->num))
+ return false;
+ }
+ else if (TREE_CODE (*step) != INTEGER_CST)
return false;
+
*base = CHREC_LEFT (ev);
- if (tree_contains_chrecs (*base)
+ if (tree_contains_chrecs (*base, NULL)
|| chrec_contains_symbols_defined_in_loop (*base, loop->num))
return false;
void
scev_analysis (void)
{
- varray_type exit_conditions;
+ VEC(tree,heap) *exit_conditions;
- VARRAY_GENERIC_PTR_INIT (exit_conditions, 37, "exit_conditions");
+ exit_conditions = VEC_alloc (tree, heap, 37);
select_loops_exit_conditions (current_loops, &exit_conditions);
if (dump_file && (dump_flags & TDF_STATS))
- analyze_scalar_evolution_for_all_loop_phi_nodes (exit_conditions);
+ analyze_scalar_evolution_for_all_loop_phi_nodes (&exit_conditions);
- number_of_iterations_for_all_loops (exit_conditions);
- VARRAY_CLEAR (exit_conditions);
+ number_of_iterations_for_all_loops (&exit_conditions);
+ VEC_free (tree, heap, exit_conditions);
}
/* Finalize the scalar evolution analysis. */
scev_finalize (void)
{
htab_delete (scalar_evolution_info);
- BITMAP_XFREE (already_instantiated);
+ BITMAP_FREE (already_instantiated);
}
+/* Replace ssa names for that scev can prove they are constant by the
+ appropriate constants. Also perform final value replacement in loops,
+ in case the replacement expressions are cheap.
+
+ We only consider SSA names defined by phi nodes; rest is left to the
+ ordinary constant propagation pass. */
+
+void
+scev_const_prop (void)
+{
+ basic_block bb;
+ tree name, phi, next_phi, type, ev;
+ struct loop *loop, *ex_loop;
+ bitmap ssa_names_to_remove = NULL;
+ unsigned i;
+
+ if (!current_loops)
+ return;
+
+ FOR_EACH_BB (bb)
+ {
+ loop = bb->loop_father;
+
+ for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
+ {
+ name = PHI_RESULT (phi);
+
+ if (!is_gimple_reg (name))
+ continue;
+
+ type = TREE_TYPE (name);
+
+ if (!POINTER_TYPE_P (type)
+ && !INTEGRAL_TYPE_P (type))
+ continue;
+
+ ev = resolve_mixers (loop, analyze_scalar_evolution (loop, name));
+ if (!is_gimple_min_invariant (ev)
+ || !may_propagate_copy (name, ev))
+ continue;
+
+ /* Replace the uses of the name. */
+ replace_uses_by (name, ev);
+
+ if (!ssa_names_to_remove)
+ ssa_names_to_remove = BITMAP_ALLOC (NULL);
+ bitmap_set_bit (ssa_names_to_remove, SSA_NAME_VERSION (name));
+ }
+ }
+
+ /* Remove the ssa names that were replaced by constants. We do not remove them
+ directly in the previous cycle, since this invalidates scev cache. */
+ if (ssa_names_to_remove)
+ {
+ bitmap_iterator bi;
+ unsigned i;
+
+ EXECUTE_IF_SET_IN_BITMAP (ssa_names_to_remove, 0, i, bi)
+ {
+ name = ssa_name (i);
+ phi = SSA_NAME_DEF_STMT (name);
+
+ gcc_assert (TREE_CODE (phi) == PHI_NODE);
+ remove_phi_node (phi, NULL);
+ }
+
+ BITMAP_FREE (ssa_names_to_remove);
+ scev_reset ();
+ }
+
+ /* Now the regular final value replacement. */
+ for (i = current_loops->num - 1; i > 0; i--)
+ {
+ edge exit;
+ tree def, stmts;
+
+ loop = current_loops->parray[i];
+ if (!loop)
+ continue;
+
+ /* If we do not know exact number of iterations of the loop, we cannot
+ replace the final value. */
+ exit = loop->single_exit;
+ if (!exit
+ || number_of_iterations_in_loop (loop) == chrec_dont_know)
+ continue;
+ ex_loop = exit->dest->loop_father;
+
+ for (phi = phi_nodes (exit->dest); phi; phi = next_phi)
+ {
+ next_phi = PHI_CHAIN (phi);
+ def = PHI_ARG_DEF_FROM_EDGE (phi, exit);
+ if (!is_gimple_reg (def)
+ || expr_invariant_in_loop_p (loop, def))
+ continue;
+
+ if (!POINTER_TYPE_P (TREE_TYPE (def))
+ && !INTEGRAL_TYPE_P (TREE_TYPE (def)))
+ continue;
+
+ def = analyze_scalar_evolution_in_loop (ex_loop, ex_loop, def);
+ if (!tree_does_not_contain_chrecs (def)
+ || chrec_contains_symbols_defined_in_loop (def, loop->num))
+ continue;
+
+ /* If computing the expression is expensive, let it remain in
+ loop. TODO -- we should take the cost of computing the expression
+ in loop into account. */
+ if (force_expr_to_var_cost (def) >= target_spill_cost)
+ continue;
+ def = unshare_expr (def);
+
+ if (is_gimple_val (def))
+ stmts = NULL_TREE;
+ else
+ def = force_gimple_operand (def, &stmts, true,
+ SSA_NAME_VAR (PHI_RESULT (phi)));
+ SET_USE (PHI_ARG_DEF_PTR_FROM_EDGE (phi, exit), def);
+ if (stmts)
+ compute_phi_arg_on_exit (exit, stmts, def);
+ }
+ }
+}