Hi Richard, On 05/25/2011 03:44 PM, Richard Sandiford wrote: > Sorry for being so late. I was just curious... > > Tom de Vries writes: >> The init cost of an iv will in general not be zero. It will be >> exceptional that the iv register happens to be initialized with the >> proper value at no cost. In general, there will at the very least be a >> regcopy or a const set. >> >> 2011-05-05 Tom de Vries >> >> PR target/45098 >> * tree-ssa-loop-ivopts.c (determine_iv_cost): Prevent >> cost_base.cost == 0. >> Index: gcc/tree-ssa-loop-ivopts.c >> =================================================================== >> --- gcc/tree-ssa-loop-ivopts.c (revision 173380) >> +++ gcc/tree-ssa-loop-ivopts.c (working copy) >> @@ -4688,6 +4688,8 @@ determine_iv_cost (struct ivopts_data *d >> >> base = cand->iv->base; >> cost_base = force_var_cost (data, base, NULL); >> + if (cost_base.cost == 0) >> + cost_base.cost = COSTS_N_INSNS (1); >> cost_step = add_cost (TYPE_MODE (TREE_TYPE (base)), data->speed); >> >> cost = cost_step + adjust_setup_cost (data, cost_base.cost); > > ...why does this reasoning apply only to this call to force_var_cost? > > Richard force_var_cost is described as estimating the cost of forcing expression expr into a variable. If expr is already a var, this definition is ambiguous. If we can use the var directly, the cost is zero, but if we need a regcopy, it should be the cost of a regcopy. What is special for an iv, is that we know that it is not only used but also modified. If a var is used in or after the loop, we need a regcopy to init the iv with that var. If that var is not used in or after the loop, we can use that var as iv. The patch above is a heuristic that estimates that the latter situation is the less frequent one. In general, we don't have such specific information, and the the cost of zero is a good choice then. We could add a parameter to force_var_cost that indicates this choice, that would perhaps be a better fix. As for the reasoning related to the const set, that is something that indeed holds more general, and could be implemented in force_var_cost, which is what you're suggesting if I understand you correctly. The tentative patch below explores these last 2 ideas. Thanks, -Tom