GCC Middle and Back End API Reference
gimple-ssa-strength-reduction.c File Reference

Data Structures

struct  slsr_cand_d
struct  cand_chain_d
struct  incr_info_d
struct  cand_chain_hasher

Typedefs

typedef unsigned cand_idx
typedef struct slsr_cand_d slsr_cand
typedef struct slsr_cand_dslsr_cand_t
typedef struct slsr_cand_dconst_slsr_cand_t
typedef struct cand_chain_d cand_chain
typedef struct cand_chain_dcand_chain_t
typedef struct cand_chain_dconst_cand_chain_t
typedef struct incr_info_d incr_info
typedef struct incr_info_dincr_info_t

Enumerations

enum  cand_kind { CAND_MULT, CAND_ADD, CAND_REF, CAND_PHI }
enum  cost_consts { COST_NEUTRAL = 0, 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 }

Functions

static slsr_cand_t base_cand_from_table (tree)
static tree introduce_cast_before_cand (slsr_cand_t, tree, tree)
static slsr_cand_t lookup_cand ()
static cand_idx find_phi_def ()
static slsr_cand_t find_basis_for_base_expr ()
static int find_basis_for_candidate ()
static void record_potential_basis ()
static slsr_cand_t alloc_cand_and_find_basis (enum cand_kind kind, gimple gs, tree base, double_int index, tree stride, tree ctype, unsigned savings)
static int stmt_cost ()
static slsr_cand_t base_cand_from_table ()
static void add_cand_for_stmt ()
static void slsr_process_phi ()
static bool restructure_reference (tree *pbase, tree *poffset, double_int *pindex, tree *ptype)
static void slsr_process_ref ()
static slsr_cand_t create_mul_ssa_cand ()
static slsr_cand_t create_mul_imm_cand ()
static void slsr_process_mul ()
static slsr_cand_t create_add_ssa_cand (gimple gs, tree base_in, tree addend_in, bool subtract_p, bool speed)
static slsr_cand_t create_add_imm_cand ()
static void slsr_process_add ()
static void slsr_process_neg ()
static bool legal_cast_p_1 ()
static bool legal_cast_p ()
static void slsr_process_cast ()
static void slsr_process_copy ()
static void find_candidates_in_block (struct dom_walk_data *walk_data, basic_block bb)
static void dump_candidate ()
static void dump_cand_vec ()
int ssa_base_cand_dump_callback ()
static void dump_cand_chains ()
static void dump_incr_vec ()
static void replace_ref ()
static void replace_refs ()
static bool phi_dependent_cand_p ()
static double_int cand_increment ()
static double_int cand_abs_increment ()
static bool cand_already_replaced ()
static void replace_mult_candidate ()
static void replace_unconditional_candidate ()
static int incr_vec_index ()
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)
static tree create_phi_basis (slsr_cand_t c, gimple from_phi, tree basis_name, location_t loc, bool known_stride)
static void replace_conditional_candidate ()
static int phi_add_costs ()
static void replace_uncond_cands_and_profitable_phis ()
static int count_candidates ()
static void record_increment ()
static void record_phi_increments ()
static void record_increments ()
static int phi_incr_cost ()
static slsr_cand_t unreplaced_cand_in_tree ()
static bool optimize_cands_for_speed_p ()
static int lowest_cost_path (int cost_in, int repl_savings, slsr_cand_t c, double_int incr, bool count_phis)
static int total_savings (int repl_savings, slsr_cand_t c, double_int incr, bool count_phis)
static void analyze_increments ()
static basic_block ncd_for_two_cands (basic_block bb1, basic_block bb2, slsr_cand_t c1, slsr_cand_t c2, slsr_cand_t *where)
static basic_block ncd_with_phi (slsr_cand_t c, double_int incr, gimple phi, basic_block ncd, slsr_cand_t *where)
static basic_block ncd_of_cand_and_phis ()
static basic_block nearest_common_dominator_for_cands (slsr_cand_t c, double_int incr, slsr_cand_t *where)
static bool profitable_increment_p ()
static void insert_initializers ()
static bool all_phi_incrs_profitable ()
static tree introduce_cast_before_cand ()
static gimple replace_rhs_if_not_dup (enum tree_code new_code, tree new_rhs1, tree new_rhs2, enum tree_code old_code, tree old_rhs1, tree old_rhs2, slsr_cand_t c)
static void replace_one_candidate ()
static void replace_profitable_candidates ()
static void analyze_candidates_and_replace ()
static unsigned execute_strength_reduction ()
static bool gate_strength_reduction ()
gimple_opt_passmake_pass_strength_reduction ()

Variables

static vec< slsr_cand_tcand_vec
static struct pointer_map_tstmt_cand_map
static struct obstack cand_obstack
static struct obstack chain_obstack
static incr_info_t incr_vec
static unsigned incr_vec_len
const int MAX_INCR_VEC_LEN = 16
static bool address_arithmetic_p
static hash_table
< cand_chain_hasher
base_cand_map

Typedef Documentation

typedef struct cand_chain_d cand_chain
typedef struct cand_chain_d * cand_chain_t
typedef unsigned cand_idx
@verbatim Straight-line strength reduction.

Copyright (C) 2012-2013 Free Software Foundation, Inc. Contributed by Bill Schmidt, IBM wschm.nosp@m.idt@.nosp@m.linux.nosp@m..ibm.nosp@m..com

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3, or (at your option) any later version.

GCC is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with GCC; see the file COPYING3. If not see http://www.gnu.org/licenses/.

There are many algorithms for performing strength reduction on
   loops.  This is not one of them.  IVOPTS handles strength reduction
   of induction variables just fine.  This pass is intended to pick
   up the crumbs it leaves behind, by considering opportunities for
   strength reduction along dominator paths.

   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
   control of something like -funsafe-math-optimizations.   
Information about a strength reduction candidate.  Each statement
   in the candidate table represents an expression of one of the
   following forms (the special case of CAND_REF will be described
   later):

   (CAND_MULT)  S1:  X = (B + i) * S
   (CAND_ADD)   S1:  X = B + (i * S)

   Here X and B are SSA names, i is an integer constant, and S is
   either an SSA name or a constant.  We call B the "base," i the
   "index", and S the "stride."

   Any statement S0 that dominates S1 and is of the form:

   (CAND_MULT)  S0:  Y = (B + i') * S
   (CAND_ADD)   S0:  Y = B + (i' * S)

   is called a "basis" for S1.  In both cases, S1 may be replaced by
   
                S1':  X = Y + (i - i') * S,

   where (i - i') * S is folded to the extent possible.

   All gimple statements are visited in dominator order, and each
   statement that may contribute to one of the forms of S1 above is
   given at least one entry in the candidate table.  Such statements
   include addition, pointer addition, subtraction, multiplication,
   negation, copies, and nontrivial type casts.  If a statement may
   represent more than one expression of the forms of S1 above, 
   multiple "interpretations" are stored in the table and chained
   together.  Examples:

   * An add of two SSA names may treat either operand as the base.
   * A multiply of two SSA names, likewise.
   * A copy or cast may be thought of as either a CAND_MULT with
     i = 0 and S = 1, or as a CAND_ADD with i = 0 or S = 0.

   Candidate records are allocated from an obstack.  They are addressed
   both from a hash table keyed on S1, and from a vector of candidate
   pointers arranged in predominator order.

   Opportunity note
   ----------------
   Currently we don't recognize:

     S0: Y = (S * i') - B
     S1: X = (S * i) - B

   as a strength reduction opportunity, even though this S1 would
   also be replaceable by the S1' above.  This can be added if it
   comes up in practice.

   Strength reduction in addressing
   --------------------------------
   There is another kind of candidate known as CAND_REF.  A CAND_REF
   describes a statement containing a memory reference having 
   complex addressing that might benefit from strength reduction.
   Specifically, we are interested in references for which 
   get_inner_reference returns a base address, offset, and bitpos as
   follows:

     base:    MEM_REF (T1, C1)
     offset:  MULT_EXPR (PLUS_EXPR (T2, C2), C3)
     bitpos:  C4 * BITS_PER_UNIT

   Here T1 and T2 are arbitrary trees, and C1, C2, C3, C4 are 
   arbitrary integer constants.  Note that C2 may be zero, in which
   case the offset will be MULT_EXPR (T2, C3).

   When this pattern is recognized, the original memory reference
   can be replaced with:

     MEM_REF (POINTER_PLUS_EXPR (T1, MULT_EXPR (T2, C3)),
              C1 + (C2 * C3) + C4)

   which distributes the multiply to allow constant folding.  When
   two or more addressing expressions can be represented by MEM_REFs
   of this form, differing only in the constants C1, C2, and C4,
   making this substitution produces more efficient addressing during
   the RTL phases.  When there are not at least two expressions with
   the same values of T1, T2, and C3, there is nothing to be gained
   by the replacement.

   Strength reduction of CAND_REFs uses the same infrastructure as
   that used by CAND_MULTs and CAND_ADDs.  We record T1 in the base (B)
   field, MULT_EXPR (T2, C3) in the stride (S) field, and 
   C1 + (C2 * C3) + C4 in the index (i) field.  A basis for a CAND_REF
   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.

   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,
   while cand_idx's are one-based, with zero indicating null.   
typedef struct slsr_cand_d* const_slsr_cand_t
typedef struct incr_info_d incr_info
typedef struct incr_info_d * incr_info_t
typedef struct slsr_cand_d slsr_cand
typedef struct slsr_cand_d * slsr_cand_t

Enumeration Type Documentation

enum cand_kind
The kind of candidate.   
Enumerator:
CAND_MULT 
CAND_ADD 
CAND_REF 
CAND_PHI 
Enumerator:
COST_NEUTRAL 
COST_INFINITE 
Enumerator:
DONT_COUNT_PHIS 
COUNT_PHIS 
Enumerator:
NOT_PHI_ADJUST 
PHI_ADJUST 
Enumerator:
UNKNOWN_STRIDE 
KNOWN_STRIDE 

Function Documentation

static void add_cand_for_stmt ( )
static
static void analyze_increments ( )
static
Use target-specific costs to determine and record which increments
   in the current candidate tree are profitable to replace, assuming
   MODE and SPEED.  FIRST_DEP is the first dependent of the root of
   the candidate tree.

   One slight limitation here is that we don't account for the possible
   introduction of casts in some cases.  See replace_one_candidate for
   the cases where these are introduced.  This should probably be cleaned
   up sometime.   

References add_cost(), CAND_MULT, slsr_cand_d::cand_stmt, incr_info_d::cost, COST_INFINITE, COST_NEUTRAL, incr_info_d::count, COUNT_PHIS, DONT_COUNT_PHIS, gimple_assign_lhs(), gimple_assign_rhs_code(), HOST_WIDE_INT, incr_info_d::incr, incr_vec_len, slsr_cand_d::kind, legal_cast_p_1(), lowest_cost_path(), mul_cost(), mult_by_coeff_cost(), slsr_cand_d::stride, double_int::to_shwi(), and total_savings().

Referenced by analyze_candidates_and_replace().

static slsr_cand_t base_cand_from_table ( )
static
Look up the defining statement for BASE_IN and return a pointer
   to its candidate in the candidate table, if any; otherwise NULL.
   Only CAND_ADD and CAND_MULT candidates are returned.   

References CAND_REF, slsr_cand_d::kind, and pointer_map_contains().

static double_int cand_abs_increment ( )
inlinestatic
Calculate the increment required for candidate C relative to
   its basis.  If we aren't going to generate pointer arithmetic
   for this candidate, return the absolute value of that increment
   instead.   

References address_arithmetic_p, cand_increment(), and double_int::is_negative().

Referenced by lowest_cost_path(), ncd_of_cand_and_phis(), replace_profitable_candidates(), and total_savings().

static bool cand_already_replaced ( )
inlinestatic
static double_int cand_increment ( )
static
static int count_candidates ( )
static
Count the number of candidates in the tree rooted at C that have
   not already been replaced under other interpretations.   

References cand_already_replaced(), count, slsr_cand_d::dependent, lookup_cand(), and slsr_cand_d::sibling.

Referenced by analyze_candidates_and_replace().

static slsr_cand_t create_add_imm_cand ( )
static
Create a candidate entry for a statement GS, where GS adds SSA
   name BASE_IN to constant INDEX_IN.  Propagate any known information
   about BASE_IN into the new candidate.  Return the new candidate.   

References alloc_cand_and_find_basis(), base_cand_from_table(), slsr_cand_d::base_expr, CAND_ADD, CAND_PHI, slsr_cand_d::cand_stmt, slsr_cand_d::cand_type, slsr_cand_d::dead_savings, has_single_use(), slsr_cand_d::index, slsr_cand_d::kind, lookup_cand(), double_int::multiple_of(), slsr_cand_d::next_interp, stmt_cost(), slsr_cand_d::stride, and tree_to_double_int().

Referenced by slsr_process_add().

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 
)
static
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.   

References address_arithmetic_p, double_int_to_tree(), dump_file, dump_flags, gimple_build_assign_with_ops(), gimple_set_location(), gsi_end_p(), gsi_insert_after(), gsi_insert_before(), gsi_last_bb(), GSI_NEW_STMT, gsi_stmt(), incr_vec_index(), basic_block_def::index, insert_bb(), is_ctrl_stmt(), double_int::is_negative(), double_int::is_zero(), make_temp_ssa_name(), print_gimple_stmt(), single_succ_p(), split_edge(), edge_def::src, slsr_cand_d::stride, and tree_to_double_int().

Referenced by create_phi_basis().

static slsr_cand_t create_add_ssa_cand ( gimple  gs,
tree  base_in,
tree  addend_in,
bool  subtract_p,
bool  speed 
)
static
Create a candidate entry for a statement GS, where GS adds two
   SSA names BASE_IN and ADDEND_IN if SUBTRACT_P is false, and
   subtracts ADDEND_IN from BASE_IN otherwise.  Propagate any known
   information about the two SSA names into the new candidate.
   Return the new candidate.   

References alloc_cand_and_find_basis(), base_cand_from_table(), slsr_cand_d::base_expr, CAND_ADD, CAND_MULT, CAND_PHI, slsr_cand_d::cand_stmt, slsr_cand_d::cand_type, slsr_cand_d::dead_savings, has_single_use(), slsr_cand_d::index, double_int::is_zero(), slsr_cand_d::kind, lookup_cand(), slsr_cand_d::next_interp, operand_equal_p(), stmt_cost(), slsr_cand_d::stride, and tree_to_double_int().

Referenced by slsr_process_add().

static slsr_cand_t create_mul_imm_cand ( )
static
Create a candidate entry for a statement GS, where GS multiplies
   SSA name BASE_IN by constant STRIDE_IN.  Propagate any known
   information about BASE_IN into the new candidate.  Return the new
   candidate.   

References alloc_cand_and_find_basis(), base_cand_from_table(), slsr_cand_d::base_expr, CAND_ADD, CAND_MULT, CAND_PHI, slsr_cand_d::cand_stmt, slsr_cand_d::cand_type, slsr_cand_d::dead_savings, double_int_to_tree(), has_single_use(), slsr_cand_d::index, integer_onep(), double_int::is_one(), slsr_cand_d::kind, lookup_cand(), slsr_cand_d::next_interp, stmt_cost(), slsr_cand_d::stride, and tree_to_double_int().

Referenced by slsr_process_mul(), and slsr_process_neg().

static slsr_cand_t create_mul_ssa_cand ( )
static
Create a candidate entry for a statement GS, where GS multiplies
   two SSA names BASE_IN and STRIDE_IN.  Propagate any known information
   about the two SSA names into the new candidate.  Return the new
   candidate.   

References alloc_cand_and_find_basis(), base_cand_from_table(), slsr_cand_d::base_expr, CAND_ADD, CAND_MULT, CAND_PHI, slsr_cand_d::cand_stmt, slsr_cand_d::cand_type, slsr_cand_d::dead_savings, has_single_use(), slsr_cand_d::index, integer_onep(), slsr_cand_d::kind, lookup_cand(), slsr_cand_d::next_interp, stmt_cost(), slsr_cand_d::stride, and tree_to_double_int().

Referenced by slsr_process_mul().

static tree create_phi_basis ( slsr_cand_t  c,
gimple  from_phi,
tree  basis_name,
location_t  loc,
bool  known_stride 
)
static
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.   

References add_phi_arg(), base_cand_from_table(), slsr_cand_d::base_expr, slsr_cand_d::basis, slsr_cand_d::cand_stmt, create_add_on_incoming_edge(), create_phi_node(), dump_file, dump_flags, gimple_assign_lhs(), gimple_bb(), gimple_phi_arg_def(), gimple_phi_num_args(), gimple_phi_result(), slsr_cand_d::index, double_int::is_zero(), lookup_cand(), make_temp_ssa_name(), operand_equal_p(), basic_block_def::preds, print_gimple_stmt(), and update_stmt().

Referenced by replace_conditional_candidate(), and replace_profitable_candidates().

static void dump_cand_chains ( )
static
static void dump_cand_vec ( )
static
Dump the candidate vector for debug.   

References dump_candidate(), and dump_file.

Referenced by execute_strength_reduction().

static void dump_incr_vec ( )
static
Dump the increment vector for debug.   

References count, dump_double_int(), dump_file, dump_flags, incr_vec_len, and print_generic_expr().

Referenced by analyze_candidates_and_replace().

static slsr_cand_t find_basis_for_base_expr ( )
static
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.   

References cand_chain_d::base_expr, cand_chain_d::cand, slsr_cand_d::cand_num, slsr_cand_d::cand_stmt, slsr_cand_d::cand_type, CDI_DOMINATORS, dominated_by_p(), hash_table< Descriptor, Allocator >::find(), slsr_cand_d::kind, cand_chain_d::next, operand_equal_p(), slsr_cand_d::stride, and types_compatible_p().

Referenced by find_basis_for_candidate().

static int find_basis_for_candidate ( )
static
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.   

References slsr_cand_d::base_expr, slsr_cand_d::basis, slsr_cand_d::cand_num, slsr_cand_d::cand_stmt, CDI_DOMINATORS, slsr_cand_d::dead_savings, slsr_cand_d::def_phi, slsr_cand_d::dependent, dominated_by_p(), find_basis_for_base_expr(), gimple_bb(), gimple_phi_result(), has_single_use(), lookup_cand(), and slsr_cand_d::sibling.

Referenced by alloc_cand_and_find_basis().

static cand_idx find_phi_def ( )
static
Look in the candidate table for a CAND_PHI that defines BASE and
   return it if found; otherwise return NULL.   

References base_cand_from_table(), slsr_cand_d::cand_num, CAND_PHI, and slsr_cand_d::kind.

Referenced by alloc_cand_and_find_basis().

static bool gate_strength_reduction ( )
static
static int incr_vec_index ( )
inlinestatic
Return the index in the increment vector of the given INCREMENT,
   or -1 if not found.  The latter can occur if more than
   MAX_INCR_VEC_LEN increments have been found.   

References incr_info_d::incr, and incr_vec_len.

Referenced by all_phi_incrs_profitable(), create_add_on_incoming_edge(), and replace_profitable_candidates().

static void insert_initializers ( )
static
For each profitable increment in the increment vector not equal to
   0 or 1 (or -1, for non-pointer arithmetic), find the nearest common
   dominator of all statements in the candidate chain rooted at C
   that require that increment, and insert an initializer
   T_0 = stride * increment at that location.  Record T_0 with the
   increment record.   

References slsr_cand_d::basis, slsr_cand_d::cand_stmt, double_int_to_tree(), dump_file, dump_flags, gimple_assign_rhs_code(), gimple_build_assign_with_ops(), gimple_location(), gimple_set_location(), gsi_end_p(), gsi_for_stmt(), gsi_insert_after(), gsi_insert_before(), gsi_last_bb(), GSI_SAME_STMT, gsi_stmt(), incr_info_d::incr, incr_vec_len, incr_info_d::initializer, is_ctrl_stmt(), double_int::is_minus_one(), double_int::is_one(), double_int::is_zero(), lookup_cand(), make_temp_ssa_name(), nearest_common_dominator_for_cands(), print_gimple_stmt(), profitable_increment_p(), and slsr_cand_d::stride.

Referenced by analyze_candidates_and_replace().

static tree introduce_cast_before_cand ( slsr_cand_t  ,
tree  ,
tree   
)
static
static tree introduce_cast_before_cand ( )
static
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
   the new SSA name.   

References slsr_cand_d::cand_stmt, dump_file, dump_flags, gimple_build_assign_with_ops(), gimple_location(), gimple_set_location(), gsi_for_stmt(), gsi_insert_before(), GSI_SAME_STMT, make_temp_ssa_name(), and print_gimple_stmt().

static bool legal_cast_p ( )
static
Return TRUE if GS is a statement that defines an SSA name from
   a conversion and is legal for us to combine with an add and multiply
   in the candidate table.  For example, suppose we have:

     A = B + i;
     C = (type) A;
     D = C * S;

   Without the type-cast, we would create a CAND_MULT for D with base B,
   index i, and stride S.  We want to record this candidate only if it
   is equivalent to apply the type cast following the multiply:

     A = B + i;
     E = A * S;
     D = (type) E;

   We will record the type with the candidate for D.  This allows us
   to use a similar previous candidate as a basis.  If we have earlier seen

     A' = B + i';
     C' = (type) A';
     D' = C' * S;

   we can replace D with

     D = D' + (i - i') * S;

   But if moving the type-cast would change semantics, we mustn't do this.

   This is legitimate for casts from a non-wrapping integral type to
   any integral type of the same or larger size.  It is not legitimate
   to convert a wrapping type to a non-wrapping type, or to a wrapping
   type of a different size.  I.e., with a wrapping type, we must
   assume that the addition B + i could wrap, in which case performing
   the multiply before or after one of the "illegal" type casts will
   have different semantics.   

References gimple_assign_lhs(), gimple_assign_rhs_code(), is_gimple_assign(), and legal_cast_p_1().

Referenced by slsr_process_cast().

static bool legal_cast_p_1 ( )
static
Help function for legal_cast_p, operating on two trees.  Checks
   whether it's allowable to cast from RHS to LHS.  See legal_cast_p
   for more details.   

Referenced by analyze_increments(), and legal_cast_p().

static int lowest_cost_path ( int  cost_in,
int  repl_savings,
slsr_cand_t  c,
double_int  incr,
bool  count_phis 
)
static
Add COST_IN to the lowest cost of any dependent path starting at
   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.  If COUNT_PHIS is true, include
   costs of introducing feeding statements for conditional candidates.   

References cand_abs_increment(), cand_already_replaced(), slsr_cand_d::cand_stmt, slsr_cand_d::dead_savings, slsr_cand_d::def_phi, slsr_cand_d::dependent, gimple_phi_result(), has_single_use(), lookup_cand(), phi_dependent_cand_p(), phi_incr_cost(), and slsr_cand_d::sibling.

Referenced by analyze_increments().

gimple_opt_pass* make_pass_strength_reduction ( )
static basic_block ncd_for_two_cands ( basic_block  bb1,
basic_block  bb2,
slsr_cand_t  c1,
slsr_cand_t  c2,
slsr_cand_t where 
)
static
Return the nearest common dominator of BB1 and BB2.  If the blocks
   are identical, return the earlier of C1 and C2 in *WHERE.  Otherwise,
   if the NCD matches BB1, return C1 in *WHERE; if the NCD matches BB2,
   return C2 in *WHERE; and if the NCD matches neither, return NULL in
   *WHERE.  Note: It is possible for one of C1 and C2 to be NULL.   

References slsr_cand_d::cand_num, CDI_DOMINATORS, and nearest_common_dominator().

Referenced by ncd_with_phi(), and nearest_common_dominator_for_cands().

static basic_block ncd_of_cand_and_phis ( )
static
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.   

References cand_abs_increment(), slsr_cand_d::cand_stmt, slsr_cand_d::def_phi, gimple_bb(), lookup_cand(), ncd_with_phi(), and phi_dependent_cand_p().

Referenced by nearest_common_dominator_for_cands().

static basic_block ncd_with_phi ( slsr_cand_t  c,
double_int  incr,
gimple  phi,
basic_block  ncd,
slsr_cand_t where 
)
static
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.   

References address_arithmetic_p, base_cand_from_table(), slsr_cand_d::base_expr, slsr_cand_d::basis, slsr_cand_d::cand_stmt, gimple_phi_arg_def(), gimple_phi_num_args(), gimple_phi_result(), slsr_cand_d::index, lookup_cand(), ncd_for_two_cands(), and operand_equal_p().

Referenced by ncd_of_cand_and_phis().

static basic_block nearest_common_dominator_for_cands ( slsr_cand_t  c,
double_int  incr,
slsr_cand_t where 
)
static
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
   such candidates.  If the returned block contains one or more of
   the candidates, return the earliest candidate in the block in
   *WHERE.   

References cand_already_replaced(), slsr_cand_d::dependent, lookup_cand(), ncd_for_two_cands(), ncd_of_cand_and_phis(), and slsr_cand_d::sibling.

Referenced by insert_initializers().

static bool optimize_cands_for_speed_p ( )
static
Return TRUE if the candidates in the tree rooted at C should be
   optimized for speed, else FALSE.  We estimate this based on the block
   containing the most dominant candidate in the tree that has not yet
   been replaced.   

References slsr_cand_d::cand_stmt, optimize_bb_for_speed_p(), and unreplaced_cand_in_tree().

Referenced by analyze_candidates_and_replace().

static int phi_add_costs ( )
static
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.   

References base_cand_from_table(), slsr_cand_d::base_expr, slsr_cand_d::basis, slsr_cand_d::cand_stmt, CDI_DOMINATORS, COST_INFINITE, dominated_by_p(), gimple_bb(), gimple_phi_arg_def(), gimple_phi_num_args(), gimple_phi_result(), slsr_cand_d::index, and lookup_cand().

Referenced by replace_uncond_cands_and_profitable_phis().

static bool phi_dependent_cand_p ( )
static
static int phi_incr_cost ( )
static
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.   

References add_cost(), base_cand_from_table(), slsr_cand_d::base_expr, slsr_cand_d::basis, slsr_cand_d::cand_stmt, gimple_assign_lhs(), gimple_phi_arg_def(), gimple_phi_num_args(), gimple_phi_result(), has_single_use(), slsr_cand_d::index, lookup_cand(), operand_equal_p(), and stmt_cost().

Referenced by lowest_cost_path(), and total_savings().

static bool profitable_increment_p ( )
inlinestatic
Return TRUE if the increment indexed by INDEX is profitable to replace.   

References COST_NEUTRAL.

Referenced by all_phi_incrs_profitable(), insert_initializers(), and replace_profitable_candidates().

static void record_increment ( )
static
Increase the count of INCREMENT by one in the increment vector.
   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.   

References address_arithmetic_p, slsr_cand_d::base_expr, slsr_cand_d::basis, CAND_ADD, slsr_cand_d::cand_stmt, CDI_DOMINATORS, incr_info_d::cost, COST_INFINITE, incr_info_d::count, dominated_by_p(), gimple_assign_rhs1(), gimple_assign_rhs2(), gimple_assign_rhs_code(), gimple_bb(), incr_info_d::incr, incr_vec_len, slsr_cand_d::index, incr_info_d::init_bb, incr_info_d::initializer, double_int::is_negative(), slsr_cand_d::kind, MAX_INCR_VEC_LEN, operand_equal_p(), double_int::sgt(), and double_int::slt().

Referenced by record_increments(), and record_phi_increments().

static void record_increments ( )
static
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
   T_0 = stride * I is provided by a candidate that dominates all
   candidates with the same increment, also record T_0 for subsequent
   use.   

References slsr_cand_d::basis, cand_already_replaced(), cand_increment(), slsr_cand_d::cand_stmt, slsr_cand_d::def_phi, slsr_cand_d::dependent, slsr_cand_d::index, lookup_cand(), NOT_PHI_ADJUST, phi_dependent_cand_p(), record_increment(), record_phi_increments(), and slsr_cand_d::sibling.

Referenced by analyze_candidates_and_replace().

static void record_phi_increments ( )
static
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.   

References base_cand_from_table(), slsr_cand_d::base_expr, gimple_phi_arg_def(), gimple_phi_num_args(), gimple_phi_result(), slsr_cand_d::index, operand_equal_p(), PHI_ADJUST, and record_increment().

Referenced by record_increments().

static void record_potential_basis ( )
static
Record a mapping from the base expression of C to C itself, indicating that
   C may potentially serve as a basis using that base expression.   

References slsr_cand_d::base_expr, cand_chain_d::base_expr, cand_chain_d::cand, chain_obstack, hash_table< Descriptor, Allocator >::find_slot(), and cand_chain_d::next.

Referenced by alloc_cand_and_find_basis().

static void replace_conditional_candidate ( )
static
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.   

References slsr_cand_d::basis, slsr_cand_d::cand_stmt, create_phi_basis(), slsr_cand_d::def_phi, gimple_assign_lhs(), gimple_location(), slsr_cand_d::index, KNOWN_STRIDE, lookup_cand(), replace_mult_candidate(), slsr_cand_d::stride, and tree_to_double_int().

Referenced by replace_uncond_cands_and_profitable_phis().

static void replace_one_candidate ( )
static
Strength-reduce the statement represented by candidate C by replacing
   it with an equivalent addition or subtraction.  I is the index into
   the increment vector identifying C's increment.  NEW_VAR is used to
   create a new SSA name if a cast needs to be introduced.  BASIS_NAME
   is the rhs1 to use in creating the add/subtract.   

References address_arithmetic_p, cand_increment(), slsr_cand_d::cand_stmt, dump_file, dump_flags, gimple_assign_lhs(), gimple_assign_rhs1(), gimple_assign_rhs2(), gimple_assign_rhs_code(), gimple_assign_set_rhs_with_ops(), gimple_build_assign_with_ops(), gimple_location(), gimple_set_location(), gsi_for_stmt(), gsi_replace(), gsi_stmt(), incr_info_d::initializer, introduce_cast_before_cand(), double_int::is_minus_one(), double_int::is_one(), double_int::is_zero(), operand_equal_p(), print_gimple_stmt(), replace_rhs_if_not_dup(), slsr_cand_d::stride, types_compatible_p(), and update_stmt().

Referenced by replace_profitable_candidates().

static void replace_refs ( )
static
Replace CAND_REF candidate C, each sibling of candidate C, and each
   dependent of candidate C with an equivalent strength-reduced data
   reference.   

References slsr_cand_d::cand_stmt, slsr_cand_d::dependent, gimple_assign_lhs_ptr(), gimple_assign_rhs1_ptr(), gimple_vdef(), lookup_cand(), replace_ref(), and slsr_cand_d::sibling.

Referenced by analyze_candidates_and_replace().

static gimple replace_rhs_if_not_dup ( enum tree_code  new_code,
tree  new_rhs1,
tree  new_rhs2,
enum tree_code  old_code,
tree  old_rhs1,
tree  old_rhs2,
slsr_cand_t  c 
)
static
Replace the RHS of the statement represented by candidate C with 
   NEW_CODE, NEW_RHS1, and NEW_RHS2, provided that to do so doesn't
   leave C unchanged or just interchange its operands.  The original
   operation and operands are in OLD_CODE, OLD_RHS1, and OLD_RHS2.
   If the replacement was made and we are doing a details dump,
   return the revised statement, else NULL.   

References slsr_cand_d::cand_stmt, dump_file, dump_flags, gimple_assign_set_rhs_with_ops(), gsi_for_stmt(), gsi_stmt(), operand_equal_p(), and update_stmt().

Referenced by replace_one_candidate().

static void replace_uncond_cands_and_profitable_phis ( )
static
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.   

References add_cost(), add_costs(), CAND_MULT, slsr_cand_d::cand_num, slsr_cand_d::cand_stmt, COST_NEUTRAL, slsr_cand_d::dead_savings, slsr_cand_d::def_phi, slsr_cand_d::dependent, dump_file, dump_flags, gimple_phi_result(), slsr_cand_d::kind, lookup_cand(), optimize_bb_for_speed_p(), phi_add_costs(), phi_dependent_cand_p(), replace_conditional_candidate(), replace_unconditional_candidate(), slsr_cand_d::sibling, and stmt_cost().

Referenced by analyze_candidates_and_replace().

static void replace_unconditional_candidate ( )
static
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."   

References slsr_cand_d::basis, cand_already_replaced(), cand_increment(), slsr_cand_d::cand_stmt, gimple_assign_lhs(), lookup_cand(), replace_mult_candidate(), slsr_cand_d::stride, and tree_to_double_int().

Referenced by replace_uncond_cands_and_profitable_phis().

static bool restructure_reference ( tree pbase,
tree poffset,
double_int pindex,
tree ptype 
)
static
Look for the following pattern:

    *PBASE:    MEM_REF (T1, C1)

    *POFFSET:  MULT_EXPR (T2, C3)        [C2 is zero]
                     or
               MULT_EXPR (PLUS_EXPR (T2, C2), C3)
                     or
               MULT_EXPR (MINUS_EXPR (T2, -C2), C3)

    *PINDEX:   C4 * BITS_PER_UNIT

   If not present, leave the input values unchanged and return FALSE.
   Otherwise, modify the input values as follows and return TRUE:

    *PBASE:    T1
    *POFFSET:  MULT_EXPR (T2, C3)
    *PINDEX:   C1 + (C2 * C3) + C4   

References double_int_to_tree(), double_int::from_uhwi(), double_int::is_zero(), mem_ref_offset(), offset, tree_to_double_int(), type(), double_int::udiv(), and double_int::umod().

Referenced by slsr_process_ref().

static void slsr_process_add ( )
static
Given GS which is an add or subtract of scalar integers or pointers,
   make at least one appropriate entry in the candidate table.   

References add_cand_for_stmt(), create_add_imm_cand(), create_add_ssa_cand(), gimple_assign_rhs_code(), slsr_cand_d::next_interp, operand_equal_p(), and tree_to_double_int().

Referenced by find_candidates_in_block().

static void slsr_process_cast ( )
static
Given GS which is a cast to a scalar integer type, determine whether
   the cast is legal for strength reduction.  If so, make at least one
   appropriate entry in the candidate table.   

References add_cand_for_stmt(), alloc_cand_and_find_basis(), base_cand_from_table(), slsr_cand_d::base_expr, CAND_ADD, CAND_MULT, slsr_cand_d::cand_num, CAND_PHI, slsr_cand_d::cand_stmt, slsr_cand_d::dead_savings, gimple_assign_lhs(), has_single_use(), slsr_cand_d::index, slsr_cand_d::kind, legal_cast_p(), lookup_cand(), slsr_cand_d::next_interp, stmt_cost(), and slsr_cand_d::stride.

Referenced by find_candidates_in_block().

static void slsr_process_copy ( )
static
Given GS which is a copy of a scalar integer type, make at least one
   appropriate entry in the candidate table.

   This interface is included for completeness, but is unnecessary
   if this pass immediately follows a pass that performs copy 
   propagation, such as DOM.   

References add_cand_for_stmt(), alloc_cand_and_find_basis(), base_cand_from_table(), slsr_cand_d::base_expr, CAND_ADD, CAND_MULT, slsr_cand_d::cand_num, CAND_PHI, slsr_cand_d::cand_stmt, slsr_cand_d::cand_type, slsr_cand_d::dead_savings, has_single_use(), slsr_cand_d::index, slsr_cand_d::kind, lookup_cand(), slsr_cand_d::next_interp, stmt_cost(), and slsr_cand_d::stride.

Referenced by find_candidates_in_block().

static void slsr_process_mul ( )
static
Given GS which is a multiply of scalar integers, make an appropriate
   entry in the candidate table.  If this is a multiply of two SSA names,
   create two CAND_MULT interpretations and attempt to find a basis for
   each of them.  Otherwise, create a single CAND_MULT and attempt to
   find a basis.   

References add_cand_for_stmt(), slsr_cand_d::cand_num, create_mul_imm_cand(), create_mul_ssa_cand(), and slsr_cand_d::next_interp.

Referenced by find_candidates_in_block().

static void slsr_process_neg ( )
static
Given GS which is a negate of a scalar integer, make an appropriate
   entry in the candidate table.  A negate is equivalent to a multiply
   by -1.   

References add_cand_for_stmt(), and create_mul_imm_cand().

Referenced by find_candidates_in_block().

static void slsr_process_phi ( )
static
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.   

References add_cand_for_stmt(), alloc_cand_and_find_basis(), base_cand_from_table(), slsr_cand_d::base_expr, CAND_ADD, CAND_PHI, slsr_cand_d::cand_stmt, slsr_cand_d::dead_savings, gimple_bb(), gimple_phi_arg_def(), gimple_phi_num_args(), has_single_use(), integer_onep(), slsr_cand_d::kind, lookup_cand(), basic_block_def::loop_father, slsr_cand_d::next_interp, operand_equal_p(), single_succ(), stmt_cost(), and slsr_cand_d::stride.

Referenced by find_candidates_in_block().

static void slsr_process_ref ( )
static
Given GS which contains a data reference, create a CAND_REF entry in
   the candidate table and attempt to find a basis.   

References add_cand_for_stmt(), alloc_cand_and_find_basis(), CAND_REF, double_int::from_uhwi(), get_inner_reference(), gimple_assign_lhs(), gimple_assign_rhs1(), gimple_vdef(), handled_component_p(), HOST_WIDE_INT, offset, restructure_reference(), and type().

Referenced by find_candidates_in_block().

int ssa_base_cand_dump_callback ( )
Callback used to dump the candidate chains hash table.   

References cand_chain_d::base_expr, cand_chain_d::cand, slsr_cand_d::cand_num, dump_file, cand_chain_d::next, and print_generic_expr().

Referenced by dump_cand_chains().

static int total_savings ( int  repl_savings,
slsr_cand_t  c,
double_int  incr,
bool  count_phis 
)
static
Compute the total savings that would accrue from all replacements
   in the candidate tree rooted at C, counting only candidates with
   increment INCR.  Assume that replacing a candidate reduces cost
   by REPL_SAVINGS.  Also account for savings from statements that
   would go dead.   

References cand_abs_increment(), cand_already_replaced(), slsr_cand_d::cand_stmt, slsr_cand_d::dead_savings, slsr_cand_d::def_phi, slsr_cand_d::dependent, gimple_phi_result(), has_single_use(), lookup_cand(), phi_dependent_cand_p(), phi_incr_cost(), and slsr_cand_d::sibling.

Referenced by analyze_increments().

static slsr_cand_t unreplaced_cand_in_tree ( )
static
Return the first candidate in the tree rooted at C that has not
   already been replaced, favoring siblings over dependents.   

References cand_already_replaced(), slsr_cand_d::dependent, lookup_cand(), and slsr_cand_d::sibling.

Referenced by optimize_cands_for_speed_p().


Variable Documentation

bool address_arithmetic_p
static
For a chain of candidates with unknown stride, indicates whether or not
   we must generate pointer arithmetic when replacing statements.   

Referenced by all_phi_incrs_profitable(), analyze_candidates_and_replace(), cand_abs_increment(), create_add_on_incoming_edge(), ncd_with_phi(), record_increment(), and replace_one_candidate().

hash_table<cand_chain_hasher> base_cand_map
static
Hash table embodying a mapping from base exprs to chains of candidates.   
struct obstack cand_obstack
static
Obstack for candidates.   

Referenced by alloc_cand_and_find_basis(), and execute_strength_reduction().

vec<slsr_cand_t> cand_vec
static
Candidates are maintained in a vector.  If candidate X dominates
   candidate Y, then X appears before Y in the vector; but the
   converse does not necessarily hold.   
struct obstack chain_obstack
static
Obstack for candidate chains.   

Referenced by execute_strength_reduction(), and record_potential_basis().

incr_info_t incr_vec
static
An array INCR_VEC of incr_infos is used during analysis of related
   candidates having an SSA name for a stride.  INCR_VEC_LEN describes
   its current length.  MAX_INCR_VEC_LEN is used to avoid costly
   pathological cases.  
const int MAX_INCR_VEC_LEN = 16
struct pointer_map_t* stmt_cand_map
static
Pointer map embodying a mapping from statements to candidates.