Commit 9b92d12b by Bill Schmidt Committed by William Schmidt

gimple-ssa-strength-reduction.c (cand_kind): Add CAND_PHI.

gcc:

2013-05-03  Bill Schmidt  <wschmidt@linux.vnet.ibm.com>

	* gimple-ssa-strength-reduction.c (cand_kind): Add CAND_PHI.
	(slsr_cand_d): Redefine def_phi.
	(stride_status, phi_adjust_status, count_phis_status): New enums.
	(find_phi_def): New.
	(find_basis_for_base_expr): New.
	(find_basis_for_candidate): Handle hidden bases.
	(alloc_cand_and_find_basis): Handle phi candidates.
	(slsr_process_phi): New.
	(create_mul_ssa_cand): Exclude phi base candidates; use integer_onep.
	(create_mul_imm_cand): Likewise.
	(create_add_ssa_cand): Exclude phi base candidates.
	(create_add_imm_cand): Likewise.
	(slsr_process_cast): Likewise.
	(slsr_process_copy): Likewise.
	(find_candidates_in_block): Handle phi candidates.
	(dump_candidate): Likewise.
	(unconditional_cands): Delete.
	(unconditional_cands_with_known_stride_p): Delete.
	(phi_dependent_cand_p): New.
	(cand_increment): Handle phi-dependent candidates.
	(replace_dependent): Delete.
	(replace_mult_candidate): New.
	(replace_unconditional_candidate): New.
	(incr_vec_index): Move to avoid forward reference.
	(create_add_on_incoming_edge): New.
	(create_phi_basis): New.
	(replace_dependents): Delete.
	(replace_conditional_candidate): New.
	(phi_add_costs): New.
	(replace_uncond_cands_and_profitable_phis): New.
	(record_increment): Handle phi adjustments.
	(record_phi_increments): New.
	(record_increments): Handle phi adjustments.
	(phi_incr_cost): New.
	(lowest_cost_path): Handle phis.
	(total_savings): Likewise.
	(analyze_increments): Likewise.
	(ncd_with_phi): New.
	(ncd_of_cand_and_phis): New.
	(nearest_common_dominator_for_cands): Handle phi increments.
	(all_phi_incrs_profitable): New.
	(replace_profitable_candidates): Handle phi-dependent candidates.
	(analyze_candidates_and_replace): Likewise.

gcc/testsuite:

2013-05-03  Bill Schmidt  <wschmidt@linux.vnet.ibm.com>

	* gcc.dg/tree-ssa/slsr-32.c: New.
	* gcc.dg/tree-ssa/slsr-33.c: New.
	* gcc.dg/tree-ssa/slsr-34.c: New.
	* gcc.dg/tree-ssa/slsr-35.c: New.
	* gcc.dg/tree-ssa/slsr-36.c: New.
	* gcc.dg/tree-ssa/slsr-37.c: New.
	* gcc.dg/tree-ssa/slsr-38.c: New.

From-SVN: r198586
parent 72c88644
2013-05-03 Bill Schmidt <wschmidt@linux.vnet.ibm.com>
* gimple-ssa-strength-reduction.c (cand_kind): Add CAND_PHI.
(slsr_cand_d): Redefine def_phi.
(stride_status, phi_adjust_status, count_phis_status): New enums.
(find_phi_def): New.
(find_basis_for_base_expr): New.
(find_basis_for_candidate): Handle hidden bases.
(alloc_cand_and_find_basis): Handle phi candidates.
(slsr_process_phi): New.
(create_mul_ssa_cand): Exclude phi base candidates; use integer_onep.
(create_mul_imm_cand): Likewise.
(create_add_ssa_cand): Exclude phi base candidates.
(create_add_imm_cand): Likewise.
(slsr_process_cast): Likewise.
(slsr_process_copy): Likewise.
(find_candidates_in_block): Handle phi candidates.
(dump_candidate): Likewise.
(unconditional_cands): Delete.
(unconditional_cands_with_known_stride_p): Delete.
(phi_dependent_cand_p): New.
(cand_increment): Handle phi-dependent candidates.
(replace_dependent): Delete.
(replace_mult_candidate): New.
(replace_unconditional_candidate): New.
(incr_vec_index): Move to avoid forward reference.
(create_add_on_incoming_edge): New.
(create_phi_basis): New.
(replace_dependents): Delete.
(replace_conditional_candidate): New.
(phi_add_costs): New.
(replace_uncond_cands_and_profitable_phis): New.
(record_increment): Handle phi adjustments.
(record_phi_increments): New.
(record_increments): Handle phi adjustments.
(phi_incr_cost): New.
(lowest_cost_path): Handle phis.
(total_savings): Likewise.
(analyze_increments): Likewise.
(ncd_with_phi): New.
(ncd_of_cand_and_phis): New.
(nearest_common_dominator_for_cands): Handle phi increments.
(all_phi_incrs_profitable): New.
(replace_profitable_candidates): Handle phi-dependent candidates.
(analyze_candidates_and_replace): Likewise.
2013-05-03 Teresa Johnson <tejohnson@google.com>
PR bootstrap/57154
......
......@@ -24,18 +24,10 @@ along with GCC; see the file COPYING3. If not see
up the crumbs it leaves behind, by considering opportunities for
strength reduction along dominator paths.
Strength reduction will be implemented in four stages, gradually
adding more complex candidates:
1) Explicit multiplies, known constant multipliers, no
conditional increments. (complete)
2) Explicit multiplies, unknown constant multipliers,
no conditional increments. (complete)
3) Implicit multiplies in addressing expressions. (complete)
4) Explicit multiplies, conditional increments. (pending)
It would also be possible to apply strength reduction to divisions
and modulos, but such opportunities are relatively uncommon.
Strength reduction addresses explicit multiplies, and certain
multiplies implicit in addressing expressions. It would also be
possible to apply strength reduction to divisions and modulos,
but such opportunities are relatively uncommon.
Strength reduction is also currently restricted to integer operations.
If desired, it could be extended to floating-point operations under
......@@ -147,7 +139,69 @@ along with GCC; see the file COPYING3. If not see
is thus another CAND_REF with the same B and S values. When at
least two CAND_REFs are chained together using the basis relation,
each of them is replaced as above, resulting in improved code
generation for addressing. */
generation for addressing.
Conditional candidates
======================
Conditional candidates are best illustrated with an example.
Consider the code sequence:
(1) x_0 = ...;
(2) a_0 = x_0 * 5; MULT (B: x_0; i: 0; S: 5)
if (...)
(3) x_1 = x_0 + 1; ADD (B: x_0, i: 1; S: 1)
(4) x_2 = PHI <x_0, x_1>; PHI (B: x_0, i: 0, S: 1)
(5) x_3 = x_2 + 1; ADD (B: x_2, i: 1, S: 1)
(6) a_1 = x_3 * 5; MULT (B: x_2, i: 1; S: 5)
Here strength reduction is complicated by the uncertain value of x_2.
A legitimate transformation is:
(1) x_0 = ...;
(2) a_0 = x_0 * 5;
if (...)
{
(3) [x_1 = x_0 + 1;]
(3a) t_1 = a_0 + 5;
}
(4) [x_2 = PHI <x_0, x_1>;]
(4a) t_2 = PHI <a_0, t_1>;
(5) [x_3 = x_2 + 1;]
(6r) a_1 = t_2 + 5;
where the bracketed instructions may go dead.
To recognize this opportunity, we have to observe that statement (6)
has a "hidden basis" (2). The hidden basis is unlike a normal basis
in that the statement and the hidden basis have different base SSA
names (x_2 and x_0, respectively). The relationship is established
when a statement's base name (x_2) is defined by a phi statement (4),
each argument of which (x_0, x_1) has an identical "derived base name."
If the argument is defined by a candidate (as x_1 is by (3)) that is a
CAND_ADD having a stride of 1, the derived base name of the argument is
the base name of the candidate (x_0). Otherwise, the argument itself
is its derived base name (as is the case with argument x_0).
The hidden basis for statement (6) is the nearest dominating candidate
whose base name is the derived base name (x_0) of the feeding phi (4),
and whose stride is identical to that of the statement. We can then
create the new "phi basis" (4a) and feeding adds along incoming arcs (3a),
allowing the final replacement of (6) by the strength-reduced (6r).
To facilitate this, a new kind of candidate (CAND_PHI) is introduced.
A CAND_PHI is not a candidate for replacement, but is maintained in the
candidate table to ease discovery of hidden bases. Any phi statement
whose arguments share a common derived base name is entered into the
table with the derived base name, an (arbitrary) index of zero, and a
stride of 1. A statement with a hidden basis can then be detected by
simply looking up its feeding phi definition in the candidate table,
extracting the derived base name, and searching for a basis in the
usual manner after substituting the derived base name.
Note that the transformation is only valid when the original phi and
the statements that define the phi's arguments are all at the same
position in the loop hierarchy. */
/* Index into the candidate vector, offset by 1. VECs are zero-based,
......@@ -159,7 +213,8 @@ enum cand_kind
{
CAND_MULT,
CAND_ADD,
CAND_REF
CAND_REF,
CAND_PHI
};
struct slsr_cand_d
......@@ -204,9 +259,9 @@ struct slsr_cand_d
/* Next candidate having the same basis as this one. */
cand_idx sibling;
/* If this is a conditional candidate, the defining PHI statement
for the base SSA name B. For future use; always NULL for now. */
gimple def_phi;
/* If this is a conditional candidate, the CAND_PHI candidate
that defines the base SSA name B. */
cand_idx def_phi;
/* Savings that can be expected from eliminating dead code if this
candidate is replaced. */
......@@ -282,6 +337,24 @@ enum cost_consts
COST_INFINITE = 1000
};
enum stride_status
{
UNKNOWN_STRIDE = 0,
KNOWN_STRIDE = 1
};
enum phi_adjust_status
{
NOT_PHI_ADJUST = 0,
PHI_ADJUST = 1
};
enum count_phis_status
{
DONT_COUNT_PHIS = 0,
COUNT_PHIS = 1
};
/* Pointer map embodying a mapping from statements to candidates. */
static struct pointer_map_t *stmt_cand_map;
......@@ -300,6 +373,10 @@ static unsigned incr_vec_len;
/* For a chain of candidates with unknown stride, indicates whether or not
we must generate pointer arithmetic when replacing statements. */
static bool address_arithmetic_p;
/* Forward function declarations. */
static slsr_cand_t base_cand_from_table (tree);
static tree introduce_cast_before_cand (slsr_cand_t, tree, tree, tree*);
/* Produce a pointer to the IDX'th candidate in the candidate vector. */
......@@ -335,15 +412,31 @@ cand_chain_hasher::equal (const value_type *chain1, const compare_type *chain2)
/* Hash table embodying a mapping from base exprs to chains of candidates. */
static hash_table <cand_chain_hasher> base_cand_map;
/* Use the base expr from candidate C to look for possible candidates
that can serve as a basis for C. Each potential basis must also
appear in a block that dominates the candidate statement and have
the same stride and type. If more than one possible basis exists,
the one with highest index in the vector is chosen; this will be
the most immediately dominating basis. */
/* Look in the candidate table for a CAND_PHI that defines BASE and
return it if found; otherwise return NULL. */
static int
find_basis_for_candidate (slsr_cand_t c)
static cand_idx
find_phi_def (tree base)
{
slsr_cand_t c;
if (TREE_CODE (base) != SSA_NAME)
return 0;
c = base_cand_from_table (base);
if (!c || c->kind != CAND_PHI)
return 0;
return c->cand_num;
}
/* Helper routine for find_basis_for_candidate. May be called twice:
once for the candidate's base expr, and optionally again for the
candidate's phi definition. */
static slsr_cand_t
find_basis_for_base_expr (slsr_cand_t c, tree base_expr)
{
cand_chain mapping_key;
cand_chain_t chain;
......@@ -353,7 +446,7 @@ find_basis_for_candidate (slsr_cand_t c)
int iters = 0;
int max_iters = PARAM_VALUE (PARAM_MAX_SLSR_CANDIDATE_SCAN);
mapping_key.base_expr = c->base_expr;
mapping_key.base_expr = base_expr;
chain = base_cand_map.find (&mapping_key);
for (; chain && iters < max_iters; chain = chain->next, ++iters)
......@@ -373,6 +466,50 @@ find_basis_for_candidate (slsr_cand_t c)
basis = one_basis;
}
return basis;
}
/* Use the base expr from candidate C to look for possible candidates
that can serve as a basis for C. Each potential basis must also
appear in a block that dominates the candidate statement and have
the same stride and type. If more than one possible basis exists,
the one with highest index in the vector is chosen; this will be
the most immediately dominating basis. */
static int
find_basis_for_candidate (slsr_cand_t c)
{
slsr_cand_t basis = find_basis_for_base_expr (c, c->base_expr);
/* If a candidate doesn't have a basis using its base expression,
it may have a basis hidden by one or more intervening phis. */
if (!basis && c->def_phi)
{
basic_block basis_bb, phi_bb;
slsr_cand_t phi_cand = lookup_cand (c->def_phi);
basis = find_basis_for_base_expr (c, phi_cand->base_expr);
if (basis)
{
/* A hidden basis must dominate the phi-definition of the
candidate's base name. */
phi_bb = gimple_bb (phi_cand->cand_stmt);
basis_bb = gimple_bb (basis->cand_stmt);
if (phi_bb == basis_bb
|| !dominated_by_p (CDI_DOMINATORS, phi_bb, basis_bb))
{
basis = NULL;
c->basis = 0;
}
/* If we found a hidden basis, estimate additional dead-code
savings if the phi and its feeding statements can be removed. */
if (basis && has_single_use (gimple_phi_result (phi_cand->cand_stmt)))
c->dead_savings += phi_cand->dead_savings;
}
}
if (basis)
{
c->sibling = basis->dependent;
......@@ -428,11 +565,16 @@ alloc_cand_and_find_basis (enum cand_kind kind, gimple gs, tree base,
c->next_interp = 0;
c->dependent = 0;
c->sibling = 0;
c->def_phi = NULL;
c->def_phi = find_phi_def (base);
c->dead_savings = savings;
cand_vec.safe_push (c);
c->basis = find_basis_for_candidate (c);
if (kind == CAND_PHI)
c->basis = 0;
else
c->basis = find_basis_for_candidate (c);
record_potential_basis (c);
return c;
......@@ -515,6 +657,97 @@ add_cand_for_stmt (gimple gs, slsr_cand_t c)
*slot = c;
}
/* Given PHI which contains a phi statement, determine whether it
satisfies all the requirements of a phi candidate. If so, create
a candidate. Note that a CAND_PHI never has a basis itself, but
is used to help find a basis for subsequent candidates. */
static void
slsr_process_phi (gimple phi, bool speed)
{
unsigned i;
tree arg0_base = NULL_TREE, base_type;
slsr_cand_t c;
struct loop *cand_loop = gimple_bb (phi)->loop_father;
unsigned savings = 0;
/* A CAND_PHI requires each of its arguments to have the same
derived base name. (See the module header commentary for a
definition of derived base names.) Furthermore, all feeding
definitions must be in the same position in the loop hierarchy
as PHI. */
for (i = 0; i < gimple_phi_num_args (phi); i++)
{
slsr_cand_t arg_cand;
tree arg = gimple_phi_arg_def (phi, i);
tree derived_base_name = NULL_TREE;
gimple arg_stmt = NULL;
basic_block arg_bb = NULL;
if (TREE_CODE (arg) != SSA_NAME)
return;
arg_cand = base_cand_from_table (arg);
if (arg_cand)
{
while (arg_cand->kind != CAND_ADD && arg_cand->kind != CAND_PHI)
{
if (!arg_cand->next_interp)
return;
arg_cand = lookup_cand (arg_cand->next_interp);
}
if (!integer_onep (arg_cand->stride))
return;
derived_base_name = arg_cand->base_expr;
arg_stmt = arg_cand->cand_stmt;
arg_bb = gimple_bb (arg_stmt);
/* Gather potential dead code savings if the phi statement
can be removed later on. */
if (has_single_use (arg))
{
if (gimple_code (arg_stmt) == GIMPLE_PHI)
savings += arg_cand->dead_savings;
else
savings += stmt_cost (arg_stmt, speed);
}
}
else
{
derived_base_name = arg;
if (SSA_NAME_IS_DEFAULT_DEF (arg))
arg_bb = single_succ (ENTRY_BLOCK_PTR);
else
gimple_bb (SSA_NAME_DEF_STMT (arg));
}
if (!arg_bb || arg_bb->loop_father != cand_loop)
return;
if (i == 0)
arg0_base = derived_base_name;
else if (!operand_equal_p (derived_base_name, arg0_base, 0))
return;
}
/* Create the candidate. "alloc_cand_and_find_basis" is named
misleadingly for this case, as no basis will be sought for a
CAND_PHI. */
base_type = TREE_TYPE (arg0_base);
c = alloc_cand_and_find_basis (CAND_PHI, phi, arg0_base, double_int_zero,
integer_one_node, base_type, savings);
/* Add the candidate to the statement-candidate mapping. */
add_cand_for_stmt (phi, c);
}
/* Look for the following pattern:
*PBASE: MEM_REF (T1, C1)
......@@ -652,11 +885,10 @@ create_mul_ssa_cand (gimple gs, tree base_in, tree stride_in, bool speed)
/* Look at all interpretations of the base candidate, if necessary,
to find information to propagate into this candidate. */
while (base_cand && !base)
while (base_cand && !base && base_cand->kind != CAND_PHI)
{
if (base_cand->kind == CAND_MULT
&& operand_equal_p (base_cand->stride, integer_one_node, 0))
if (base_cand->kind == CAND_MULT && integer_onep (base_cand->stride))
{
/* Y = (B + i') * 1
X = Y * Z
......@@ -723,7 +955,7 @@ create_mul_imm_cand (gimple gs, tree base_in, tree stride_in, bool speed)
/* Look at all interpretations of the base candidate, if necessary,
to find information to propagate into this candidate. */
while (base_cand && !base)
while (base_cand && !base && base_cand->kind != CAND_PHI)
{
if (base_cand->kind == CAND_MULT
&& TREE_CODE (base_cand->stride) == INTEGER_CST)
......@@ -742,8 +974,7 @@ create_mul_imm_cand (gimple gs, tree base_in, tree stride_in, bool speed)
savings = (base_cand->dead_savings
+ stmt_cost (base_cand->cand_stmt, speed));
}
else if (base_cand->kind == CAND_ADD
&& operand_equal_p (base_cand->stride, integer_one_node, 0))
else if (base_cand->kind == CAND_ADD && integer_onep (base_cand->stride))
{
/* Y = B + (i' * 1)
X = Y * c
......@@ -856,7 +1087,7 @@ create_add_ssa_cand (gimple gs, tree base_in, tree addend_in,
/* The most useful transformation is a multiply-immediate feeding
an add or subtract. Look for that first. */
while (addend_cand && !base)
while (addend_cand && !base && addend_cand->kind != CAND_PHI)
{
if (addend_cand->kind == CAND_MULT
&& addend_cand->index.is_zero ()
......@@ -883,7 +1114,7 @@ create_add_ssa_cand (gimple gs, tree base_in, tree addend_in,
addend_cand = NULL;
}
while (base_cand && !base)
while (base_cand && !base && base_cand->kind != CAND_PHI)
{
if (base_cand->kind == CAND_ADD
&& (base_cand->index.is_zero ()
......@@ -906,7 +1137,7 @@ create_add_ssa_cand (gimple gs, tree base_in, tree addend_in,
{
slsr_cand_t subtrahend_cand = base_cand_from_table (addend_in);
while (subtrahend_cand && !base)
while (subtrahend_cand && !base && subtrahend_cand->kind != CAND_PHI)
{
if (subtrahend_cand->kind == CAND_MULT
&& subtrahend_cand->index.is_zero ()
......@@ -968,7 +1199,7 @@ create_add_imm_cand (gimple gs, tree base_in, double_int index_in, bool speed)
slsr_cand_t c;
slsr_cand_t base_cand = base_cand_from_table (base_in);
while (base_cand && !base)
while (base_cand && !base && base_cand->kind != CAND_PHI)
{
bool unsigned_p = TYPE_UNSIGNED (TREE_TYPE (base_cand->stride));
......@@ -1178,7 +1409,7 @@ slsr_process_cast (gimple gs, tree rhs1, bool speed)
base_cand = base_cand_from_table (rhs1);
ctype = TREE_TYPE (lhs);
if (base_cand)
if (base_cand && base_cand->kind != CAND_PHI)
{
while (base_cand)
{
......@@ -1237,7 +1468,7 @@ slsr_process_copy (gimple gs, tree rhs1, bool speed)
base_cand = base_cand_from_table (rhs1);
if (base_cand)
if (base_cand && base_cand->kind != CAND_PHI)
{
while (base_cand)
{
......@@ -1288,6 +1519,9 @@ find_candidates_in_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
bool speed = optimize_bb_for_speed_p (bb);
gimple_stmt_iterator gsi;
for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
slsr_process_phi (gsi_stmt (gsi), speed);
for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
{
gimple gs = gsi_stmt (gsi);
......@@ -1400,6 +1634,13 @@ dump_candidate (slsr_cand_t c)
dump_double_int (dump_file, c->index, false);
fputs (" : ", dump_file);
break;
case CAND_PHI:
fputs (" PHI : ", dump_file);
print_generic_expr (dump_file, c->base_expr, 0);
fputs (" + (unknown * ", dump_file);
print_generic_expr (dump_file, c->stride, 0);
fputs (") : ", dump_file);
break;
default:
gcc_unreachable ();
}
......@@ -1409,10 +1650,7 @@ dump_candidate (slsr_cand_t c)
fprintf (dump_file, " next-interp: %d dead-savings: %d\n",
c->next_interp, c->dead_savings);
if (c->def_phi)
{
fputs (" phi: ", dump_file);
print_gimple_stmt (dump_file, c->def_phi, 0, 0);
}
fprintf (dump_file, " phi: %d\n", c->def_phi);
fputs ("\n", dump_file);
}
......@@ -1482,40 +1720,6 @@ dump_incr_vec (void)
}
}
/* Recursive helper for unconditional_cands_with_known_stride_p.
Returns TRUE iff C, its siblings, and its dependents are all
unconditional candidates. */
static bool
unconditional_cands (slsr_cand_t c)
{
if (c->def_phi)
return false;
if (c->sibling && !unconditional_cands (lookup_cand (c->sibling)))
return false;
if (c->dependent && !unconditional_cands (lookup_cand (c->dependent)))
return false;
return true;
}
/* Determine whether or not the tree of candidates rooted at
ROOT consists entirely of unconditional increments with
an INTEGER_CST stride. */
static bool
unconditional_cands_with_known_stride_p (slsr_cand_t root)
{
/* The stride is identical for all related candidates, so
check it once. */
if (TREE_CODE (root->stride) != INTEGER_CST)
return false;
return unconditional_cands (lookup_cand (root->dependent));
}
/* Replace *EXPR in candidate C with an equivalent strength-reduced
data reference. */
......@@ -1563,6 +1767,20 @@ replace_refs (slsr_cand_t c)
replace_refs (lookup_cand (c->dependent));
}
/* Return TRUE if candidate C is dependent upon a PHI. */
static bool
phi_dependent_cand_p (slsr_cand_t c)
{
/* A candidate is not necessarily dependent upon a PHI just because
it has a phi definition for its base name. It may have a basis
that relies upon the same phi definition, in which case the PHI
is irrelevant to this candidate. */
return (c->def_phi
&& c->basis
&& lookup_cand (c->basis)->def_phi != c->def_phi);
}
/* Calculate the increment required for candidate C relative to
its basis. */
......@@ -1573,8 +1791,10 @@ cand_increment (slsr_cand_t c)
/* If the candidate doesn't have a basis, just return its own
index. This is useful in record_increments to help us find
an existing initializer. */
if (!c->basis)
an existing initializer. Also, if the candidate's basis is
hidden by a phi, then its own index will be the increment
from the newly introduced phi basis. */
if (!c->basis || phi_dependent_cand_p (c))
return c->index;
basis = lookup_cand (c->basis);
......@@ -1617,142 +1837,431 @@ cand_already_replaced (slsr_cand_t c)
return (gimple_bb (c->cand_stmt) == 0);
}
/* Helper routine for replace_dependents, doing the work for a
single candidate C. */
/* Common logic used by replace_unconditional_candidate and
replace_conditional_candidate. */
static void
replace_dependent (slsr_cand_t c, enum tree_code cand_code)
replace_mult_candidate (slsr_cand_t c, tree basis_name, double_int bump,
tree *var)
{
double_int stride = tree_to_double_int (c->stride);
double_int bump = cand_increment (c) * stride;
gimple stmt_to_print = NULL;
slsr_cand_t basis;
tree basis_name, incr_type, bump_tree;
enum tree_code code;
tree target_type = TREE_TYPE (gimple_assign_lhs (c->cand_stmt));
enum tree_code cand_code = gimple_assign_rhs_code (c->cand_stmt);
/* It is highly unlikely, but possible, that the resulting
bump doesn't fit in a HWI. Abandon the replacement
in this case. Restriction to signed HWI is conservative
for unsigned types but allows for safe negation without
twisted logic. */
if (!bump.fits_shwi ())
in this case. This does not affect siblings or dependents
of C. Restriction to signed HWI is conservative for unsigned
types but allows for safe negation without twisted logic. */
if (bump.fits_shwi ()
&& bump.to_shwi () != HOST_WIDE_INT_MIN
/* It is not useful to replace casts, copies, or adds of
an SSA name and a constant. */
&& cand_code != MODIFY_EXPR
&& cand_code != NOP_EXPR
&& cand_code != PLUS_EXPR
&& cand_code != POINTER_PLUS_EXPR
&& cand_code != MINUS_EXPR)
{
enum tree_code code = PLUS_EXPR;
tree bump_tree;
gimple stmt_to_print = NULL;
/* If the basis name and the candidate's LHS have incompatible
types, introduce a cast. */
if (!useless_type_conversion_p (target_type, TREE_TYPE (basis_name)))
basis_name = introduce_cast_before_cand (c, target_type,
basis_name, var);
if (bump.is_negative ())
{
code = MINUS_EXPR;
bump = -bump;
}
bump_tree = double_int_to_tree (target_type, bump);
if (dump_file && (dump_flags & TDF_DETAILS))
{
fputs ("Replacing: ", dump_file);
print_gimple_stmt (dump_file, c->cand_stmt, 0, 0);
}
if (bump.is_zero ())
{
tree lhs = gimple_assign_lhs (c->cand_stmt);
gimple copy_stmt = gimple_build_assign (lhs, basis_name);
gimple_stmt_iterator gsi = gsi_for_stmt (c->cand_stmt);
gimple_set_location (copy_stmt, gimple_location (c->cand_stmt));
gsi_replace (&gsi, copy_stmt, false);
if (dump_file && (dump_flags & TDF_DETAILS))
stmt_to_print = copy_stmt;
}
else
{
tree rhs1, rhs2;
if (cand_code != NEGATE_EXPR) {
rhs1 = gimple_assign_rhs1 (c->cand_stmt);
rhs2 = gimple_assign_rhs2 (c->cand_stmt);
}
if (cand_code != NEGATE_EXPR
&& ((operand_equal_p (rhs1, basis_name, 0)
&& operand_equal_p (rhs2, bump_tree, 0))
|| (operand_equal_p (rhs1, bump_tree, 0)
&& operand_equal_p (rhs2, basis_name, 0))))
{
if (dump_file && (dump_flags & TDF_DETAILS))
{
fputs ("(duplicate, not actually replacing)", dump_file);
stmt_to_print = c->cand_stmt;
}
}
else
{
gimple_stmt_iterator gsi = gsi_for_stmt (c->cand_stmt);
gimple_assign_set_rhs_with_ops (&gsi, code,
basis_name, bump_tree);
update_stmt (gsi_stmt (gsi));
if (dump_file && (dump_flags & TDF_DETAILS))
stmt_to_print = gsi_stmt (gsi);
}
}
if (dump_file && (dump_flags & TDF_DETAILS))
{
fputs ("With: ", dump_file);
print_gimple_stmt (dump_file, stmt_to_print, 0, 0);
fputs ("\n", dump_file);
}
}
}
/* Replace candidate C with an add or subtract. Note that we only
operate on CAND_MULTs with known strides, so we will never generate
a POINTER_PLUS_EXPR. Each candidate X = (B + i) * S is replaced by
X = Y + ((i - i') * S), as described in the module commentary. The
folded value ((i - i') * S) is referred to here as the "bump." */
static void
replace_unconditional_candidate (slsr_cand_t c)
{
slsr_cand_t basis;
double_int stride, bump;
tree var = NULL;
if (cand_already_replaced (c))
return;
basis = lookup_cand (c->basis);
basis_name = gimple_assign_lhs (basis->cand_stmt);
if (cand_code == POINTER_PLUS_EXPR)
stride = tree_to_double_int (c->stride);
bump = cand_increment (c) * stride;
replace_mult_candidate (c, gimple_assign_lhs (basis->cand_stmt), bump, &var);
}
/* Return the index in the increment vector of the given INCREMENT. */
static inline unsigned
incr_vec_index (double_int increment)
{
unsigned i;
for (i = 0; i < incr_vec_len && increment != incr_vec[i].incr; i++)
;
gcc_assert (i < incr_vec_len);
return i;
}
/* Create a new statement along edge E to add BASIS_NAME to the product
of INCREMENT and the stride of candidate C. Create and return a new
SSA name from *VAR to be used as the LHS of the new statement.
KNOWN_STRIDE is true iff C's stride is a constant. */
static tree
create_add_on_incoming_edge (slsr_cand_t c, tree basis_name,
double_int increment, edge e, location_t loc,
bool known_stride)
{
basic_block insert_bb;
gimple_stmt_iterator gsi;
tree lhs, basis_type;
gimple new_stmt;
/* If the add candidate along this incoming edge has the same
index as C's hidden basis, the hidden basis represents this
edge correctly. */
if (increment.is_zero ())
return basis_name;
basis_type = TREE_TYPE (basis_name);
lhs = make_temp_ssa_name (basis_type, NULL, "slsr");
if (known_stride)
{
incr_type = sizetype;
code = cand_code;
tree bump_tree;
enum tree_code code = PLUS_EXPR;
double_int bump = increment * tree_to_double_int (c->stride);
if (bump.is_negative ())
{
code = MINUS_EXPR;
bump = -bump;
}
bump_tree = double_int_to_tree (basis_type, bump);
new_stmt = gimple_build_assign_with_ops (code, lhs, basis_name,
bump_tree);
}
else
{
incr_type = TREE_TYPE (gimple_assign_rhs1 (c->cand_stmt));
code = PLUS_EXPR;
}
unsigned i;
bool negate_incr = (!address_arithmetic_p && increment.is_negative ());
i = incr_vec_index (negate_incr ? -increment : increment);
if (bump.is_negative ()
&& cand_code != POINTER_PLUS_EXPR)
{
code = MINUS_EXPR;
bump = -bump;
if (incr_vec[i].initializer)
{
enum tree_code code = negate_incr ? MINUS_EXPR : PLUS_EXPR;
new_stmt = gimple_build_assign_with_ops (code, lhs, basis_name,
incr_vec[i].initializer);
}
else if (increment.is_one ())
new_stmt = gimple_build_assign_with_ops (PLUS_EXPR, lhs, basis_name,
c->stride);
else if (increment.is_minus_one ())
new_stmt = gimple_build_assign_with_ops (MINUS_EXPR, lhs, basis_name,
c->stride);
else
gcc_unreachable ();
}
bump_tree = double_int_to_tree (incr_type, bump);
insert_bb = single_succ_p (e->src) ? e->src : split_edge (e);
gsi = gsi_last_bb (insert_bb);
if (!gsi_end_p (gsi) && is_ctrl_stmt (gsi_stmt (gsi)))
gsi_insert_before (&gsi, new_stmt, GSI_NEW_STMT);
else
gsi_insert_after (&gsi, new_stmt, GSI_NEW_STMT);
gimple_set_location (new_stmt, loc);
if (dump_file && (dump_flags & TDF_DETAILS))
{
fputs ("Replacing: ", dump_file);
print_gimple_stmt (dump_file, c->cand_stmt, 0, 0);
fprintf (dump_file, "Inserting in block %d: ", insert_bb->index);
print_gimple_stmt (dump_file, new_stmt, 0, 0);
}
if (bump.is_zero ())
{
tree lhs = gimple_assign_lhs (c->cand_stmt);
gimple copy_stmt = gimple_build_assign (lhs, basis_name);
gimple_stmt_iterator gsi = gsi_for_stmt (c->cand_stmt);
gimple_set_location (copy_stmt, gimple_location (c->cand_stmt));
gsi_replace (&gsi, copy_stmt, false);
if (dump_file && (dump_flags & TDF_DETAILS))
stmt_to_print = copy_stmt;
}
else
return lhs;
}
/* Given a candidate C with BASIS_NAME being the LHS of C's basis which
is hidden by the phi node FROM_PHI, create a new phi node in the same
block as FROM_PHI. The new phi is suitable for use as a basis by C,
with its phi arguments representing conditional adjustments to the
hidden basis along conditional incoming paths. Those adjustments are
made by creating add statements (and sometimes recursively creating
phis) along those incoming paths. LOC is the location to attach to
the introduced statements. KNOWN_STRIDE is true iff C's stride is a
constant. */
static tree
create_phi_basis (slsr_cand_t c, gimple from_phi, tree basis_name,
location_t loc, bool known_stride)
{
int i;
tree name, phi_arg;
gimple phi;
vec<tree> phi_args;
slsr_cand_t basis = lookup_cand (c->basis);
int nargs = gimple_phi_num_args (from_phi);
basic_block phi_bb = gimple_bb (from_phi);
slsr_cand_t phi_cand = base_cand_from_table (gimple_phi_result (from_phi));
phi_args.create (nargs);
/* Process each argument of the existing phi that represents
conditionally-executed add candidates. */
for (i = 0; i < nargs; i++)
{
tree rhs1 = gimple_assign_rhs1 (c->cand_stmt);
tree rhs2 = gimple_assign_rhs2 (c->cand_stmt);
if (cand_code != NEGATE_EXPR
&& ((operand_equal_p (rhs1, basis_name, 0)
&& operand_equal_p (rhs2, bump_tree, 0))
|| (operand_equal_p (rhs1, bump_tree, 0)
&& operand_equal_p (rhs2, basis_name, 0))))
edge e = (*phi_bb->preds)[i];
tree arg = gimple_phi_arg_def (from_phi, i);
tree feeding_def;
/* If the phi argument is the base name of the CAND_PHI, then
this incoming arc should use the hidden basis. */
if (operand_equal_p (arg, phi_cand->base_expr, 0))
if (basis->index.is_zero ())
feeding_def = gimple_assign_lhs (basis->cand_stmt);
else
{
double_int incr = c->index - basis->index;
feeding_def = create_add_on_incoming_edge (c, basis_name, incr,
e, loc, known_stride);
}
else
{
if (dump_file && (dump_flags & TDF_DETAILS))
gimple arg_def = SSA_NAME_DEF_STMT (arg);
/* If there is another phi along this incoming edge, we must
process it in the same fashion to ensure that all basis
adjustments are made along its incoming edges. */
if (gimple_code (arg_def) == GIMPLE_PHI)
feeding_def = create_phi_basis (c, arg_def, basis_name,
loc, known_stride);
else
{
fputs ("(duplicate, not actually replacing)", dump_file);
stmt_to_print = c->cand_stmt;
slsr_cand_t arg_cand = base_cand_from_table (arg);
double_int diff = arg_cand->index - basis->index;
feeding_def = create_add_on_incoming_edge (c, basis_name, diff,
e, loc, known_stride);
}
}
else
{
gimple_stmt_iterator gsi = gsi_for_stmt (c->cand_stmt);
gimple_assign_set_rhs_with_ops (&gsi, code, basis_name, bump_tree);
update_stmt (gsi_stmt (gsi));
if (dump_file && (dump_flags & TDF_DETAILS))
stmt_to_print = gsi_stmt (gsi);
}
/* Because of recursion, we need to save the arguments in a vector
so we can create the PHI statement all at once. Otherwise the
storage for the half-created PHI can be reclaimed. */
phi_args.safe_push (feeding_def);
}
/* Create the new phi basis. */
name = make_temp_ssa_name (TREE_TYPE (basis_name), NULL, "slsr");
phi = create_phi_node (name, phi_bb);
SSA_NAME_DEF_STMT (name) = phi;
FOR_EACH_VEC_ELT (phi_args, i, phi_arg)
{
edge e = (*phi_bb->preds)[i];
add_phi_arg (phi, phi_arg, e, loc);
}
update_stmt (phi);
if (dump_file && (dump_flags & TDF_DETAILS))
{
fputs ("With: ", dump_file);
print_gimple_stmt (dump_file, stmt_to_print, 0, 0);
fputs ("\n", dump_file);
fputs ("Introducing new phi basis: ", dump_file);
print_gimple_stmt (dump_file, phi, 0, 0);
}
return name;
}
/* Replace candidate C, each sibling of candidate C, and each
dependent of candidate C with an add or subtract. Note that we
only operate on CAND_MULTs with known strides, so we will never
generate a POINTER_PLUS_EXPR. Each candidate X = (B + i) * S is
replaced by X = Y + ((i - i') * S), as described in the module
commentary. The folded value ((i - i') * S) is referred to here
as the "bump." */
/* Given a candidate C whose basis is hidden by at least one intervening
phi, introduce a matching number of new phis to represent its basis
adjusted by conditional increments along possible incoming paths. Then
replace C as though it were an unconditional candidate, using the new
basis. */
static void
replace_dependents (slsr_cand_t c)
replace_conditional_candidate (slsr_cand_t c)
{
enum tree_code cand_code = gimple_assign_rhs_code (c->cand_stmt);
tree basis_name, name, var = NULL;
slsr_cand_t basis;
location_t loc;
double_int stride, bump;
/* It is not useful to replace casts, copies, or adds of an SSA name
and a constant. Also skip candidates that have already been
replaced under another interpretation. */
if (cand_code != MODIFY_EXPR
&& cand_code != NOP_EXPR
&& c->kind == CAND_MULT
&& !cand_already_replaced (c))
replace_dependent (c, cand_code);
/* Look up the LHS SSA name from C's basis. This will be the
RHS1 of the adds we will introduce to create new phi arguments. */
basis = lookup_cand (c->basis);
basis_name = gimple_assign_lhs (basis->cand_stmt);
if (c->sibling)
replace_dependents (lookup_cand (c->sibling));
/* Create a new phi statement which will represent C's true basis
after the transformation is complete. */
loc = gimple_location (c->cand_stmt);
name = create_phi_basis (c, lookup_cand (c->def_phi)->cand_stmt,
basis_name, loc, KNOWN_STRIDE);
/* Replace C with an add of the new basis phi and a constant. */
stride = tree_to_double_int (c->stride);
bump = c->index * stride;
if (c->dependent)
replace_dependents (lookup_cand (c->dependent));
replace_mult_candidate (c, name, bump, &var);
}
/* Return the index in the increment vector of the given INCREMENT. */
static inline unsigned
incr_vec_index (double_int increment)
/* Compute the expected costs of inserting basis adjustments for
candidate C with phi-definition PHI. The cost of inserting
one adjustment is given by ONE_ADD_COST. If PHI has arguments
which are themselves phi results, recursively calculate costs
for those phis as well. */
static int
phi_add_costs (gimple phi, slsr_cand_t c, int one_add_cost)
{
unsigned i;
for (i = 0; i < incr_vec_len && increment != incr_vec[i].incr; i++)
;
int cost = 0;
slsr_cand_t phi_cand = base_cand_from_table (gimple_phi_result (phi));
gcc_assert (i < incr_vec_len);
return i;
for (i = 0; i < gimple_phi_num_args (phi); i++)
{
tree arg = gimple_phi_arg_def (phi, i);
if (arg != phi_cand->base_expr)
{
gimple arg_def = SSA_NAME_DEF_STMT (arg);
if (gimple_code (arg_def) == GIMPLE_PHI)
cost += phi_add_costs (arg_def, c, one_add_cost);
else
{
slsr_cand_t arg_cand = base_cand_from_table (arg);
if (arg_cand->index != c->index)
cost += one_add_cost;
}
}
}
return cost;
}
/* For candidate C, each sibling of candidate C, and each dependent of
candidate C, determine whether the candidate is dependent upon a
phi that hides its basis. If not, replace the candidate unconditionally.
Otherwise, determine whether the cost of introducing compensation code
for the candidate is offset by the gains from strength reduction. If
so, replace the candidate and introduce the compensation code. */
static void
replace_uncond_cands_and_profitable_phis (slsr_cand_t c)
{
if (phi_dependent_cand_p (c))
{
if (c->kind == CAND_MULT)
{
/* A candidate dependent upon a phi will replace a multiply by
a constant with an add, and will insert at most one add for
each phi argument. Add these costs with the potential dead-code
savings to determine profitability. */
bool speed = optimize_bb_for_speed_p (gimple_bb (c->cand_stmt));
int mult_savings = stmt_cost (c->cand_stmt, speed);
gimple phi = lookup_cand (c->def_phi)->cand_stmt;
tree phi_result = gimple_phi_result (phi);
int one_add_cost = add_cost (speed,
TYPE_MODE (TREE_TYPE (phi_result)));
int add_costs = one_add_cost + phi_add_costs (phi, c, one_add_cost);
int cost = add_costs - mult_savings - c->dead_savings;
if (dump_file && (dump_flags & TDF_DETAILS))
{
fprintf (dump_file, " Conditional candidate %d:\n", c->cand_num);
fprintf (dump_file, " add_costs = %d\n", add_costs);
fprintf (dump_file, " mult_savings = %d\n", mult_savings);
fprintf (dump_file, " dead_savings = %d\n", c->dead_savings);
fprintf (dump_file, " cost = %d\n", cost);
if (cost <= COST_NEUTRAL)
fputs (" Replacing...\n", dump_file);
else
fputs (" Not replaced.\n", dump_file);
}
if (cost <= COST_NEUTRAL)
replace_conditional_candidate (c);
}
}
else
replace_unconditional_candidate (c);
if (c->sibling)
replace_uncond_cands_and_profitable_phis (lookup_cand (c->sibling));
if (c->dependent)
replace_uncond_cands_and_profitable_phis (lookup_cand (c->dependent));
}
/* Count the number of candidates in the tree rooted at C that have
not already been replaced under other interpretations. */
......@@ -1771,12 +2280,14 @@ count_candidates (slsr_cand_t c)
}
/* Increase the count of INCREMENT by one in the increment vector.
INCREMENT is associated with candidate C. If an initializer
INCREMENT is associated with candidate C. If INCREMENT is to be
conditionally executed as part of a conditional candidate replacement,
IS_PHI_ADJUST is true, otherwise false. If an initializer
T_0 = stride * I is provided by a candidate that dominates all
candidates with the same increment, also record T_0 for subsequent use. */
static void
record_increment (slsr_cand_t c, double_int increment)
record_increment (slsr_cand_t c, double_int increment, bool is_phi_adjust)
{
bool found = false;
unsigned i;
......@@ -1816,14 +2327,16 @@ record_increment (slsr_cand_t c, double_int increment)
the count to zero. We're only processing it so it can possibly
provide an initializer for other candidates. */
incr_vec[incr_vec_len].incr = increment;
incr_vec[incr_vec_len].count = c->basis ? 1 : 0;
incr_vec[incr_vec_len].count = c->basis || is_phi_adjust ? 1 : 0;
incr_vec[incr_vec_len].cost = COST_INFINITE;
/* Optimistically record the first occurrence of this increment
as providing an initializer (if it does); we will revise this
opinion later if it doesn't dominate all other occurrences.
Exception: increments of -1, 0, 1 never need initializers. */
Exception: increments of -1, 0, 1 never need initializers;
and phi adjustments don't ever provide initializers. */
if (c->kind == CAND_ADD
&& !is_phi_adjust
&& c->index == increment
&& (increment.sgt (double_int_one)
|| increment.slt (double_int_minus_one))
......@@ -1859,6 +2372,38 @@ record_increment (slsr_cand_t c, double_int increment)
}
}
/* Given phi statement PHI that hides a candidate from its BASIS, find
the increments along each incoming arc (recursively handling additional
phis that may be present) and record them. These increments are the
difference in index between the index-adjusting statements and the
index of the basis. */
static void
record_phi_increments (slsr_cand_t basis, gimple phi)
{
unsigned i;
slsr_cand_t phi_cand = base_cand_from_table (gimple_phi_result (phi));
for (i = 0; i < gimple_phi_num_args (phi); i++)
{
tree arg = gimple_phi_arg_def (phi, i);
if (!operand_equal_p (arg, phi_cand->base_expr, 0))
{
gimple arg_def = SSA_NAME_DEF_STMT (arg);
if (gimple_code (arg_def) == GIMPLE_PHI)
record_phi_increments (basis, arg_def);
else
{
slsr_cand_t arg_cand = base_cand_from_table (arg);
double_int diff = arg_cand->index - basis->index;
record_increment (arg_cand, diff, PHI_ADJUST);
}
}
}
}
/* Determine how many times each unique increment occurs in the set
of candidates rooted at C's parent, recording the data in the
increment vector. For each unique increment I, if an initializer
......@@ -1870,7 +2415,24 @@ static void
record_increments (slsr_cand_t c)
{
if (!cand_already_replaced (c))
record_increment (c, cand_increment (c));
{
if (!phi_dependent_cand_p (c))
record_increment (c, cand_increment (c), NOT_PHI_ADJUST);
else
{
/* A candidate with a basis hidden by a phi will have one
increment for its relationship to the index represented by
the phi, and potentially additional increments along each
incoming edge. For the root of the dependency tree (which
has no basis), process just the initial index in case it has
an initializer that can be used by subsequent candidates. */
record_increment (c, c->index, NOT_PHI_ADJUST);
if (c->basis)
record_phi_increments (lookup_cand (c->basis),
lookup_cand (c->def_phi)->cand_stmt);
}
}
if (c->sibling)
record_increments (lookup_cand (c->sibling));
......@@ -1879,6 +2441,55 @@ record_increments (slsr_cand_t c)
record_increments (lookup_cand (c->dependent));
}
/* Add up and return the costs of introducing add statements that
require the increment INCR on behalf of candidate C and phi
statement PHI. Accumulate into *SAVINGS the potential savings
from removing existing statements that feed PHI and have no other
uses. */
static int
phi_incr_cost (slsr_cand_t c, double_int incr, gimple phi, int *savings)
{
unsigned i;
int cost = 0;
slsr_cand_t basis = lookup_cand (c->basis);
slsr_cand_t phi_cand = base_cand_from_table (gimple_phi_result (phi));
for (i = 0; i < gimple_phi_num_args (phi); i++)
{
tree arg = gimple_phi_arg_def (phi, i);
if (!operand_equal_p (arg, phi_cand->base_expr, 0))
{
gimple arg_def = SSA_NAME_DEF_STMT (arg);
if (gimple_code (arg_def) == GIMPLE_PHI)
{
int feeding_savings = 0;
cost += phi_incr_cost (c, incr, arg_def, &feeding_savings);
if (has_single_use (gimple_phi_result (arg_def)))
*savings += feeding_savings;
}
else
{
slsr_cand_t arg_cand = base_cand_from_table (arg);
double_int diff = arg_cand->index - basis->index;
if (incr == diff)
{
tree basis_lhs = gimple_assign_lhs (basis->cand_stmt);
tree lhs = gimple_assign_lhs (arg_cand->cand_stmt);
cost += add_cost (true, TYPE_MODE (TREE_TYPE (basis_lhs)));
if (has_single_use (lhs))
*savings += stmt_cost (arg_cand->cand_stmt, true);
}
}
}
}
return cost;
}
/* Return the first candidate in the tree rooted at C that has not
already been replaced, favoring siblings over dependents. */
......@@ -1922,12 +2533,14 @@ optimize_cands_for_speed_p (slsr_cand_t c)
candidate C or any of its siblings, counting only candidates along
such paths with increment INCR. Assume that replacing a candidate
reduces cost by REPL_SAVINGS. Also account for savings from any
statements that would go dead. */
statements that would go dead. If COUNT_PHIS is true, include
costs of introducing feeding statements for conditional candidates. */
static int
lowest_cost_path (int cost_in, int repl_savings, slsr_cand_t c, double_int incr)
lowest_cost_path (int cost_in, int repl_savings, slsr_cand_t c,
double_int incr, bool count_phis)
{
int local_cost, sib_cost;
int local_cost, sib_cost, savings = 0;
double_int cand_incr = cand_abs_increment (c);
if (cand_already_replaced (c))
......@@ -1937,14 +2550,27 @@ lowest_cost_path (int cost_in, int repl_savings, slsr_cand_t c, double_int incr)
else
local_cost = cost_in - c->dead_savings;
if (count_phis
&& phi_dependent_cand_p (c)
&& !cand_already_replaced (c))
{
gimple phi = lookup_cand (c->def_phi)->cand_stmt;
local_cost += phi_incr_cost (c, incr, phi, &savings);
if (has_single_use (gimple_phi_result (phi)))
local_cost -= savings;
}
if (c->dependent)
local_cost = lowest_cost_path (local_cost, repl_savings,
lookup_cand (c->dependent), incr);
lookup_cand (c->dependent), incr,
count_phis);
if (c->sibling)
{
sib_cost = lowest_cost_path (cost_in, repl_savings,
lookup_cand (c->sibling), incr);
lookup_cand (c->sibling), incr,
count_phis);
local_cost = MIN (local_cost, sib_cost);
}
......@@ -1958,7 +2584,8 @@ lowest_cost_path (int cost_in, int repl_savings, slsr_cand_t c, double_int incr)
would go dead. */
static int
total_savings (int repl_savings, slsr_cand_t c, double_int incr)
total_savings (int repl_savings, slsr_cand_t c, double_int incr,
bool count_phis)
{
int savings = 0;
double_int cand_incr = cand_abs_increment (c);
......@@ -1966,11 +2593,25 @@ total_savings (int repl_savings, slsr_cand_t c, double_int incr)
if (incr == cand_incr && !cand_already_replaced (c))
savings += repl_savings + c->dead_savings;
if (count_phis
&& phi_dependent_cand_p (c)
&& !cand_already_replaced (c))
{
int phi_savings = 0;
gimple phi = lookup_cand (c->def_phi)->cand_stmt;
savings -= phi_incr_cost (c, incr, phi, &phi_savings);
if (has_single_use (gimple_phi_result (phi)))
savings += phi_savings;
}
if (c->dependent)
savings += total_savings (repl_savings, lookup_cand (c->dependent), incr);
savings += total_savings (repl_savings, lookup_cand (c->dependent), incr,
count_phis);
if (c->sibling)
savings += total_savings (repl_savings, lookup_cand (c->sibling), incr);
savings += total_savings (repl_savings, lookup_cand (c->sibling), incr,
count_phis);
return savings;
}
......@@ -2062,9 +2703,10 @@ analyze_increments (slsr_cand_t first_dep, enum machine_mode mode, bool speed)
int repl_savings = mul_cost (speed, mode) - add_cost (speed, mode);
if (speed)
cost = lowest_cost_path (cost, repl_savings, first_dep,
incr_vec[i].incr);
incr_vec[i].incr, COUNT_PHIS);
else
cost -= total_savings (repl_savings, first_dep, incr_vec[i].incr);
cost -= total_savings (repl_savings, first_dep, incr_vec[i].incr,
COUNT_PHIS);
incr_vec[i].cost = cost;
}
......@@ -2081,9 +2723,11 @@ analyze_increments (slsr_cand_t first_dep, enum machine_mode mode, bool speed)
cost = mult_by_coeff_cost (incr, mode, speed);
if (speed)
cost = lowest_cost_path (cost, 0, first_dep, incr_vec[i].incr);
cost = lowest_cost_path (cost, 0, first_dep, incr_vec[i].incr,
DONT_COUNT_PHIS);
else
cost -= total_savings (0, first_dep, incr_vec[i].incr);
cost -= total_savings (0, first_dep, incr_vec[i].incr,
DONT_COUNT_PHIS);
incr_vec[i].cost = cost;
}
......@@ -2141,6 +2785,69 @@ ncd_for_two_cands (basic_block bb1, basic_block bb2,
return ncd;
}
/* Consider all candidates that feed PHI. Find the nearest common
dominator of those candidates requiring the given increment INCR.
Further find and return the nearest common dominator of this result
with block NCD. If the returned block contains one or more of the
candidates, return the earliest candidate in the block in *WHERE. */
static basic_block
ncd_with_phi (slsr_cand_t c, double_int incr, gimple phi,
basic_block ncd, slsr_cand_t *where)
{
unsigned i;
slsr_cand_t basis = lookup_cand (c->basis);
slsr_cand_t phi_cand = base_cand_from_table (gimple_phi_result (phi));
for (i = 0; i < gimple_phi_num_args (phi); i++)
{
tree arg = gimple_phi_arg_def (phi, i);
if (!operand_equal_p (arg, phi_cand->base_expr, 0))
{
gimple arg_def = SSA_NAME_DEF_STMT (arg);
if (gimple_code (arg_def) == GIMPLE_PHI)
ncd = ncd_with_phi (c, incr, arg_def, ncd, where);
else
{
slsr_cand_t arg_cand = base_cand_from_table (arg);
double_int diff = arg_cand->index - basis->index;
if ((incr == diff) || (!address_arithmetic_p && incr == -diff))
ncd = ncd_for_two_cands (ncd, gimple_bb (arg_cand->cand_stmt),
*where, arg_cand, where);
}
}
}
return ncd;
}
/* Consider the candidate C together with any candidates that feed
C's phi dependence (if any). Find and return the nearest common
dominator of those candidates requiring the given increment INCR.
If the returned block contains one or more of the candidates,
return the earliest candidate in the block in *WHERE. */
static basic_block
ncd_of_cand_and_phis (slsr_cand_t c, double_int incr, slsr_cand_t *where)
{
basic_block ncd = NULL;
if (cand_abs_increment (c) == incr)
{
ncd = gimple_bb (c->cand_stmt);
*where = c;
}
if (phi_dependent_cand_p (c))
ncd = ncd_with_phi (c, incr, lookup_cand (c->def_phi)->cand_stmt,
ncd, where);
return ncd;
}
/* Consider all candidates in the tree rooted at C for which INCR
represents the required increment of C relative to its basis.
Find and return the basic block that most nearly dominates all
......@@ -2154,7 +2861,6 @@ nearest_common_dominator_for_cands (slsr_cand_t c, double_int incr,
{
basic_block sib_ncd = NULL, dep_ncd = NULL, this_ncd = NULL, ncd;
slsr_cand_t sib_where = NULL, dep_where = NULL, this_where = NULL, new_where;
double_int cand_incr;
/* First find the NCD of all siblings and dependents. */
if (c->sibling)
......@@ -2183,10 +2889,11 @@ nearest_common_dominator_for_cands (slsr_cand_t c, double_int incr,
dep_where, &new_where);
/* If the candidate's increment doesn't match the one we're interested
in, then the result depends only on siblings and dependents. */
cand_incr = cand_abs_increment (c);
in (and nor do any increments for feeding defs of a phi-dependence),
then the result depends only on siblings and dependents. */
this_ncd = ncd_of_cand_and_phis (c, incr, &this_where);
if (cand_incr != incr || cand_already_replaced (c))
if (!this_ncd || cand_already_replaced (c))
{
*where = new_where;
return ncd;
......@@ -2194,8 +2901,6 @@ nearest_common_dominator_for_cands (slsr_cand_t c, double_int incr,
/* Otherwise, compare this candidate with the result from all siblings
and dependents. */
this_where = c;
this_ncd = gimple_bb (c->cand_stmt);
ncd = ncd_for_two_cands (ncd, this_ncd, new_where, this_where, where);
return ncd;
......@@ -2294,6 +2999,63 @@ insert_initializers (slsr_cand_t c)
}
}
/* Return TRUE iff all required increments for candidates feeding PHI
are profitable to replace on behalf of candidate C. */
static bool
all_phi_incrs_profitable (slsr_cand_t c, gimple phi)
{
unsigned i;
slsr_cand_t basis = lookup_cand (c->basis);
slsr_cand_t phi_cand = base_cand_from_table (gimple_phi_result (phi));
for (i = 0; i < gimple_phi_num_args (phi); i++)
{
tree arg = gimple_phi_arg_def (phi, i);
if (!operand_equal_p (arg, phi_cand->base_expr, 0))
{
gimple arg_def = SSA_NAME_DEF_STMT (arg);
if (gimple_code (arg_def) == GIMPLE_PHI)
{
if (!all_phi_incrs_profitable (c, arg_def))
return false;
}
else
{
unsigned j;
slsr_cand_t arg_cand = base_cand_from_table (arg);
double_int increment = arg_cand->index - basis->index;
if (!address_arithmetic_p && increment.is_negative ())
increment = -increment;
j = incr_vec_index (increment);
if (dump_file && (dump_flags & TDF_DETAILS))
{
fprintf (dump_file, " Conditional candidate %d, phi: ",
c->cand_num);
print_gimple_stmt (dump_file, phi, 0, 0);
fputs (" increment: ", dump_file);
dump_double_int (dump_file, increment, false);
fprintf (dump_file, "\n cost: %d\n", incr_vec[j].cost);
if (profitable_increment_p (j))
fputs (" Replacing...\n", dump_file);
else
fputs (" Not replaced.\n", dump_file);
}
if (!profitable_increment_p (j))
return false;
}
}
}
return true;
}
/* Create a NOP_EXPR that copies FROM_EXPR into a new SSA name of
type TO_TYPE, and insert it in front of the statement represented
by candidate C. Use *NEW_VAR to create the new SSA name. Return
......@@ -2520,9 +3282,35 @@ replace_profitable_candidates (slsr_cand_t c)
&& orig_code != MODIFY_EXPR
&& orig_code != NOP_EXPR)
{
slsr_cand_t basis = lookup_cand (c->basis);
tree basis_name = gimple_assign_lhs (basis->cand_stmt);
replace_one_candidate (c, i, &new_var, basis_name);
if (phi_dependent_cand_p (c))
{
gimple phi = lookup_cand (c->def_phi)->cand_stmt;
if (all_phi_incrs_profitable (c, phi))
{
/* Look up the LHS SSA name from C's basis. This will be
the RHS1 of the adds we will introduce to create new
phi arguments. */
slsr_cand_t basis = lookup_cand (c->basis);
tree basis_name = gimple_assign_lhs (basis->cand_stmt);
/* Create a new phi statement that will represent C's true
basis after the transformation is complete. */
location_t loc = gimple_location (c->cand_stmt);
tree name = create_phi_basis (c, phi, basis_name,
loc, UNKNOWN_STRIDE);
/* Replace C with an add of the new basis phi and the
increment. */
replace_one_candidate (c, i, &new_var, name);
}
}
else
{
slsr_cand_t basis = lookup_cand (c->basis);
tree basis_name = gimple_assign_lhs (basis->cand_stmt);
replace_one_candidate (c, i, &new_var, basis_name);
}
}
}
......@@ -2564,14 +3352,15 @@ analyze_candidates_and_replace (void)
if (c->kind == CAND_REF)
replace_refs (c);
/* If the common stride of all related candidates is a
known constant, and none of these has a phi-dependence,
then all replacements are considered profitable.
Each replaces a multiply by a single add, with the
possibility that a feeding add also goes dead as a
result. */
else if (unconditional_cands_with_known_stride_p (c))
replace_dependents (first_dep);
/* If the common stride of all related candidates is a known
constant, each candidate without a phi-dependence can be
profitably replaced. Each replaces a multiply by a single
add, with the possibility that a feeding add also goes dead.
A candidate with a phi-dependence is replaced only if the
compensation code it requires is offset by the strength
reduction savings. */
else if (TREE_CODE (c->stride) == INTEGER_CST)
replace_uncond_cands_and_profitable_phis (first_dep);
/* When the stride is an SSA name, it may still be profitable
to replace some or all of the dependent candidates, depending
......@@ -2613,10 +3402,6 @@ analyze_candidates_and_replace (void)
replace_profitable_candidates (first_dep);
free (incr_vec);
}
/* TODO: When conditional increments occur so that a
candidate is dependent upon a phi-basis, the cost of
introducing a temporary must be accounted for. */
}
}
......
2013-05-03 Bill Schmidt <wschmidt@linux.vnet.ibm.com>
* gcc.dg/tree-ssa/slsr-32.c: New.
* gcc.dg/tree-ssa/slsr-33.c: New.
* gcc.dg/tree-ssa/slsr-34.c: New.
* gcc.dg/tree-ssa/slsr-35.c: New.
* gcc.dg/tree-ssa/slsr-36.c: New.
* gcc.dg/tree-ssa/slsr-37.c: New.
* gcc.dg/tree-ssa/slsr-38.c: New.
2013-05-03 Ian Bolton <ian.bolton@arm.com>
* gcc.target/aarch64/tst_1.c: New test.
......
/* Verify straight-line strength reduction for a candidate with a basis
hidden by a phi dependence and having an unknown stride. */
/* { dg-do compile } */
/* { dg-options "-O3 -fdump-tree-optimized" } */
int
f (int s, int c, int i)
{
int a1, a2, a3, x1, x2, x3, x;
a1 = i * s;
x1 = c + a1;
i = i + 2;
a2 = i * s;
x2 = c + a2;
if (x2 > 6)
i = i + 2;
i = i + 2;
a3 = i * s;
x3 = c + a3;
x = x1 + x2 + x3;
return x;
}
/* { dg-final { scan-tree-dump-times " \\* s" 1 "optimized" } } */
/* { dg-final { scan-tree-dump-times " \\* 2" 1 "optimized" } } */
/* { dg-final { cleanup-tree-dump "optimized" } } */
/* Verify straight-line strength reduction for a candidate with a basis
hidden by a phi dependence and having a known stride. */
/* { dg-do compile } */
/* { dg-options "-O3 -fdump-tree-optimized" } */
int
f (int c, int i)
{
int a1, a2, a3, x1, x2, x3, x;
a1 = i * 16;
x1 = c + a1;
i = i + 2;
a2 = i * 16;
x2 = c + a2;
if (x2 > 6)
i = i + 2;
i = i + 2;
a3 = i * 16;
x3 = c + a3;
x = x1 + x2 + x3;
return x;
}
/* { dg-final { scan-tree-dump-times " \\* " 1 "optimized" } } */
/* { dg-final { cleanup-tree-dump "optimized" } } */
/* Verify straight-line strength reduction for a candidate with a basis
hidden by two phi dependences, and having a known stride. */
/* { dg-do compile } */
/* { dg-options "-O3 -fdump-tree-optimized" } */
extern void
g (void);
int
f (int c, int i)
{
int a1, a2, a3, x1, x2, x3, x;
a1 = i * 16;
x1 = c + a1;
i = i + 2;
a2 = i * 16;
x2 = c + a2;
if (x2 > 6)
{
if (c < 200)
i = i + 2;
else
i = i + 4;
g ();
}
else
i = i + 6;
i = i + 2;
a3 = i * 16;
x3 = c + a3;
x = x1 + x2 + x3;
return x;
}
/* { dg-final { scan-tree-dump-times " \\* " 1 "optimized" } } */
/* { dg-final { scan-tree-dump-times "PHI" 2 "optimized" } } */
/* { dg-final { cleanup-tree-dump "optimized" } } */
/* Verify straight-line strength reduction for a candidate with a basis
hidden by a phi dependences, having a known stride, and where the
phi has an argument which is a parameter. */
/* { dg-do compile } */
/* { dg-options "-O3 -fdump-tree-optimized" } */
int
f (int c, int i)
{
int a1, a3, x1, x3, x;
a1 = i * 16;
x1 = c + a1;
if (x1 > 6)
i = i + 2;
i = i + 2;
a3 = i * 16;
x3 = c + a3;
x = x1 + x3;
return x;
}
/* { dg-final { scan-tree-dump-times " \\* " 1 "optimized" } } */
/* { dg-final { cleanup-tree-dump "optimized" } } */
/* Verify straight-line strength reduction for a candidate with a basis
hidden by a phi dependences, having an unknown stride, and where the
phi has an argument which is a parameter. */
/* { dg-do compile } */
/* { dg-options "-O3 -fdump-tree-optimized" } */
int
f (int s, int c, int i)
{
int a1, a3, x1, x3, x;
a1 = i * s;
x1 = c + a1;
if (x1 > 6)
i = i + 2;
i = i + 2;
a3 = i * s;
x3 = c + a3;
x = x1 + x3;
return x;
}
/* { dg-final { scan-tree-dump-times " \\* s" 1 "optimized" } } */
/* { dg-final { scan-tree-dump-times " \\* 2" 1 "optimized" } } */
/* { dg-final { cleanup-tree-dump "optimized" } } */
/* Verify straight-line strength reduction for a candidate with a basis
hidden by a phi dependence and having an unknown stride. Variation
using negative increments. */
/* { dg-do compile } */
/* { dg-options "-O3 -fdump-tree-optimized" } */
int
f (int s, int c, int i)
{
int a1, a2, a3, x1, x2, x3, x;
a1 = i * s;
x1 = c + a1;
i = i - 2;
a2 = i * s;
x2 = c + a2;
if (x2 > 6)
i = i - 2;
i = i - 2;
a3 = i * s;
x3 = c + a3;
x = x1 + x2 + x3;
return x;
}
/* { dg-final { scan-tree-dump-times " \\* s" 1 "optimized" } } */
/* { dg-final { scan-tree-dump-times " \\* 2" 1 "optimized" } } */
/* { dg-final { cleanup-tree-dump "optimized" } } */
/* Verify straight-line strength reduction for a candidate with a basis
hidden by a phi dependence and having a known stride. Variation using
negative increments. */
/* { dg-do compile } */
/* { dg-options "-O3 -fdump-tree-optimized" } */
int
f (int c, int i)
{
int a1, a2, a3, x1, x2, x3, x;
a1 = i * 16;
x1 = c + a1;
i = i - 2;
a2 = i * 16;
x2 = c + a2;
if (x2 > 6)
i = i - 2;
i = i - 2;
a3 = i * 16;
x3 = c + a3;
x = x1 + x2 + x3;
return x;
}
/* { dg-final { scan-tree-dump-times " \\* " 1 "optimized" } } */
/* { dg-final { cleanup-tree-dump "optimized" } } */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment