GCC Middle and Back End API Reference
tree-if-conv.c File Reference

Data Structures

struct  bb_predicate_s
struct  ifc_dr

Typedefs

typedef struct bb_predicate_sbb_predicate_p

Functions

static bool bb_has_predicate ()
static tree bb_predicate ()
static void set_bb_predicate ()
static gimple_seq bb_predicate_gimplified_stmts ()
static void set_bb_predicate_gimplified_stmts ()
static void add_bb_predicate_gimplified_stmts ()
static void init_bb_predicate ()
static void free_bb_predicate ()
static void reset_bb_predicate ()
static tree ifc_temp_var ()
static bool is_true_predicate ()
static bool is_predicated ()
static enum tree_code parse_predicate ()
static tree fold_or_predicates ()
static bool constant_or_ssa_name ()
static tree fold_build_cond_expr ()
static void add_to_predicate_list ()
static void add_to_dst_predicate_list (struct loop *loop, edge e, tree prev_cond, tree cond)
static bool bb_with_exit_edge_p ()
static bool if_convertible_phi_p ()
static bool memrefs_read_or_written_unconditionally (gimple stmt, vec< data_reference_p > drs)
static bool write_memrefs_written_at_least_once (gimple stmt, vec< data_reference_p > drs)
static bool ifcvt_memrefs_wont_trap ()
static bool ifcvt_could_trap_p ()
static bool if_convertible_gimple_assign_stmt_p (gimple stmt, vec< data_reference_p > refs)
static bool if_convertible_stmt_p ()
static bool if_convertible_bb_p ()
static bool pred_blocks_visited_p ()
static basic_blockget_loop_body_in_if_conv_order ()
static bool predicate_bbs ()
static bool if_convertible_loop_p_1 (struct loop *loop, vec< loop_p > *loop_nest, vec< data_reference_p > *refs, vec< ddr_p > *ddrs)
static bool if_convertible_loop_p ()
static basic_block find_phi_replacement_condition (basic_block bb, tree *cond, gimple_stmt_iterator *gsi)
static void predicate_scalar_phi (gimple phi, tree cond, basic_block true_bb, gimple_stmt_iterator *gsi)
static void predicate_all_scalar_phis ()
static void insert_gimplified_predicates ()
static void predicate_mem_writes ()
static void remove_conditions_and_labels ()
static void combine_blocks ()
static bool tree_if_conversion ()
static unsigned int main_tree_if_conversion ()
static bool gate_tree_if_conversion ()
gimple_opt_passmake_pass_if_conversion ()

Variables

static basic_blockifc_bbs

Typedef Documentation

typedef struct bb_predicate_s * bb_predicate_p
   Structure used to predicate basic blocks.  This is attached to the
   ->aux field of the BBs in the loop to be if-converted.  

Function Documentation

static void add_bb_predicate_gimplified_stmts ( )
inlinestatic
   Adds the sequence of statements STMTS to the sequence of statements
   of the predicate for basic block BB.  

Referenced by add_to_predicate_list().

static void add_to_dst_predicate_list ( struct loop loop,
edge  e,
tree  prev_cond,
tree  cond 
)
static
   Add the condition COND to the previous condition PREV_COND, and add
   this to the predicate list of the destination of edge E.  LOOP is
   the loop to be if-converted.  

References dump_file, and dump_flags.

static void add_to_predicate_list ( )
inlinestatic
   Add condition NC to the predicate list of basic block BB.  
     Allow a TRUTH_NOT_EXPR around the main predicate.  

References add_bb_predicate_gimplified_stmts(), force_gimple_operand_1(), and is_gimple_condexpr().

static bool bb_has_predicate ( )
inlinestatic
   Returns true when the basic block BB has a predicate.  

References basic_block_def::aux.

static tree bb_predicate ( )
inlinestatic
   Returns the gimplified predicate for basic block BB.  

References basic_block_def::aux, and is_gimple_condexpr().

Referenced by if_convertible_loop_p(), ifc_temp_var(), and insert_gimplified_predicates().

static gimple_seq bb_predicate_gimplified_stmts ( )
inlinestatic
   Returns the sequence of statements of the gimplification of the
   predicate for basic block BB.  

References basic_block_def::aux.

static bool bb_with_exit_edge_p ( )
static
   Return true if one of the successor edges of BB exits LOOP.  

References dump_file, and print_gimple_stmt().

static void combine_blocks ( )
static
   Combine all the basic blocks from LOOP into one or two super basic
   blocks.  Replace PHI nodes with conditional modify expressions.  
     Merge basic blocks: first remove all the edges in the loop,
     except for those from the exit block.  
             Connect this node to loop header.  
         Redirect non-exit edges to loop->latch.  
         If the loop does not have an exit, reconnect header and latch.  
         Make stmts member of loop->header.  
         Update stmt list.  
     If possible, merge loop header to the block with the exit edge.
     This reduces the number of basic blocks to two, to please the
     vectorizer that handles only loops with two nodes.  
static bool constant_or_ssa_name ( )
static
   Returns true if N is either a constant or a SSA_NAME.  

Referenced by fold_build_cond_expr().

static basic_block find_phi_replacement_condition ( basic_block  bb,
tree cond,
gimple_stmt_iterator gsi 
)
static
   Basic block BB has two predecessors.  Using predecessor's bb
   predicate, set an appropriate condition COND for the PHI node
   replacement.  Return the true block whose phi arguments are
   selected when cond is true.  LOOP is the loop containing the
   if-converted region, GSI is the place to insert the code for the
   if-conversion.  
     Prefer an edge with a not negated predicate.
     ???  That's a very weak cost model.  
     Check if the edge we take the condition from is not critical.
     We know that at least one non-critical edge exists.  
           Select non loop header bb.  
     Gimplify the condition to a valid cond-expr conditonal operand.  
static tree fold_build_cond_expr ( )
static
   Returns either a COND_EXPR or the folded expression if the folded
   expression is a MIN_EXPR, a MAX_EXPR, an ABS_EXPR,
   a constant or a SSA_NAME. 

References constant_or_ssa_name().

Referenced by insert_gimplified_predicates().

static tree fold_or_predicates ( )
static
   Returns the fold of predicate C1 OR C2 at location LOC.  
static void free_bb_predicate ( )
inlinestatic
   Free the predicate of basic block BB.  
     Release the SSA_NAMEs created for the gimplification of the
     predicate.  
static bool gate_tree_if_conversion ( )
static
   Returns true when the if-conversion pass is enabled.  
static basic_block* get_loop_body_in_if_conv_order ( )
static
   Get body of a LOOP in suitable order for if-conversion.  It is
   caller's responsibility to deallocate basic block list.
   If-conversion suitable order is, breadth first sort (BFS) order
   with an additional constraint: select a block only if all its
   predecessors are already selected.  
                 This block is now visited.  
           Not done yet.  
static bool if_convertible_bb_p ( )
static
   Return true when BB is if-convertible.  This routine does not check
   basic block's statements and phis.

   A basic block is not if-convertible if:
   - it is non-empty and it is after the exit block (in BFS order),
   - it is after the exit block but before the latch,
   - its edges are not normal.

   EXIT_BB is the basic block containing the exit of the LOOP.  BB is
   inside LOOP.  
     Be less adventurous and handle only normal edges.  
     At least one incoming edge has to be non-critical as otherwise edge
     predicates are not equal to basic-block predicates of the edge
     source.  
static bool if_convertible_gimple_assign_stmt_p ( gimple  stmt,
vec< data_reference_p refs 
)
static
   Return true when STMT is if-convertible.

   GIMPLE_ASSIGN statement is not if-convertible if,
   - it is not movable,
   - it could trap,
   - LHS is not var decl.  
     Some of these constrains might be too conservative.  

References dump_file, and dump_flags.

static bool if_convertible_loop_p ( )
static
   Return true when LOOP is if-convertible.
   LOOP is if-convertible if:
   - it is innermost,
   - it has two or more basic blocks,
   - it has only one exit,
   - loop header is not the exit edge,
   - if its basic blocks and phi nodes are if convertible.  
     Handle only innermost loop.  
     If only one block, no need for if-conversion.  
     More than one loop exit is too much to handle.  
     If one of the loop header's edge is an exit edge then do not
     apply if-conversion.  

References bb_predicate(), force_gimple_operand_gsi_1(), GSI_SAME_STMT, is_gimple_condexpr(), basic_block_def::preds, edge_def::src, basic_block_def::succs, and unshare_expr().

static bool if_convertible_loop_p_1 ( struct loop loop,
vec< loop_p > *  loop_nest,
vec< data_reference_p > *  refs,
vec< ddr_p > *  ddrs 
)
static
   Return true when LOOP is if-convertible.  This is a helper function
   for if_convertible_loop_p.  REFS and DDRS are initialized and freed
   in if_convertible_loop_p.  
     Don't if-convert the loop when the data dependences cannot be
     computed: the loop won't be vectorized in that case.  
     Allow statements that can be handled during if-conversion.  
         Check the if-convertibility of statements in predicated BBs.  
static bool if_convertible_phi_p ( )
static
   Return true when PHI is if-convertible.  PHI is part of loop LOOP
   and it belongs to basic block BB.

   PHI is not if-convertible if:
   - it has more than 2 arguments.

   When the flag_tree_loop_if_convert_stores is not set, PHI is not
   if-convertible if:
   - a virtual PHI is immediately used in another PHI node,
   - there is a virtual PHI in a BB other than the loop->header.  
     When the flag_tree_loop_if_convert_stores is not set, check
     that there are no memory writes in the branches of the loop to be
     if-converted.  
static bool if_convertible_stmt_p ( )
static
   Return true when STMT is if-convertible.

   A statement is if-convertible if:
   - it is an if-convertible GIMPLE_ASSIGN,
   - it is a GIMPLE_LABEL or a GIMPLE_COND.  
                   We can only vectorize some builtins at the moment,
                   so restrict if-conversion to those.  
         Don't know what to do with 'em so don't do anything.  
static tree ifc_temp_var ( )
static
   Returns a new SSA_NAME of type TYPE that is assigned the value of
   the expression EXPR.  Inserts the statement created for this
   computation before GSI and leaves the iterator GSI at the same
   statement.  

References bb_predicate(), and is_true_predicate().

Referenced by insert_gimplified_predicates().

static bool ifcvt_could_trap_p ( )
static
   Wrapper around gimple_could_trap_p refined for the needs of the
   if-conversion.  Try to prove that the memory accesses of STMT could
   not trap in the innermost loop containing STMT.  

References dump_file, and dump_flags.

static bool ifcvt_memrefs_wont_trap ( )
static
   Return true when the memory references of STMT won't trap in the
   if-converted code.  There are two things that we have to check for:

   - writes to memory occur to writable memory: if-conversion of
   memory writes transforms the conditional memory writes into
   unconditional writes, i.e. "if (cond) A[i] = foo" is transformed
   into "A[i] = cond ? foo : A[i]", and as the write to memory may not
   be executed at all in the original code, it may be a readonly
   memory.  To check that A is not const-qualified, we check that
   there exists at least an unconditional write to A in the current
   function.

   - reads or writes to memory are valid memory accesses for every
   iteration.  To check that the memory accesses are correctly formed
   and that we are allowed to read and write in these locations, we
   check that the memory accesses to be if-converted occur at every
   iteration unconditionally.  

References dump_file, and print_gimple_stmt().

static void init_bb_predicate ( )
inlinestatic
   Initializes to TRUE the predicate of basic block BB.  
static void insert_gimplified_predicates ( )
static
   Insert in each basic block of LOOP the statements produced by the
   gimplification of the predicates.  
             Do not insert statements for a basic block that is not
             predicated.  Also make sure that the predicate of the
             basic block is set to true.  
                 Insert the predicate of the BB just after the label,
                 as the if-conversion of memory writes will use this
                 predicate.  
                 Insert the predicate of the BB at the end of the BB
                 as this would reduce the register pressure: the only
                 use of this predicate will be in successor BBs.  
             Once the sequence is code generated, set it to NULL.  

References bb_predicate(), fold_build_cond_expr(), force_gimple_operand_gsi_1(), gimple_assign_lhs(), gimple_assign_rhs1(), gimple_assign_set_rhs1(), gimple_assign_single_p(), gimple_vdef(), gsi_end_p(), gsi_next(), GSI_SAME_STMT, gsi_start_bb(), gsi_stmt(), ifc_temp_var(), is_gimple_condexpr(), is_true_predicate(), loop::num_nodes, swap(), unshare_expr(), and update_stmt().

static bool is_predicated ( )
inlinestatic
   Returns true when BB has a predicate that is not trivial: true or
   NULL_TREE.  
static bool is_true_predicate ( )
inlinestatic
   Return true when COND is a true predicate.  

References gimple_assign_rhs1(), gimple_assign_rhs_code(), is_gimple_assign(), and tcc_comparison.

Referenced by ifc_temp_var(), and insert_gimplified_predicates().

static unsigned int main_tree_if_conversion ( )
static
   Tree if-conversion pass management.  
gimple_opt_pass* make_pass_if_conversion ( )
static bool memrefs_read_or_written_unconditionally ( gimple  stmt,
vec< data_reference_p drs 
)
static
   Returns true when the memory references of STMT are read or written
   unconditionally.  In other words, this function returns true when
   for every data reference A in STMT there exist other accesses to
   a data reference with the same base with predicates that add up (OR-up) to
   the true predicate: this ensures that the data reference A is touched
   (read or written) on every iteration of the if-converted loop.  
static enum tree_code parse_predicate ( )
static
   Parses the predicate COND and returns its comparison code and
   operands OP0 and OP1.  
static bool pred_blocks_visited_p ( )
static
   Return true when all predecessor blocks of BB are visited.  The
   VISITED bitmap keeps track of the visited blocks.  
static void predicate_all_scalar_phis ( )
static
   Replaces in LOOP all the scalar phi nodes other than those in the
   LOOP->header block with conditional modify expressions.  
         BB has two predecessors.  Using predecessor's aux field, set
         appropriate condition for the PHI node replacement.  
static bool predicate_bbs ( )
static
   Returns true when the analysis of the predicates for all the basic
   blocks in LOOP succeeded.

   predicate_bbs first allocates the predicates of the basic blocks.
   These fields are then initialized with the tree expressions
   representing the predicates under which a basic block is executed
   in the LOOP.  As the loop->header is executed at each iteration, it
   has the "true" predicate.  Other statements executed under a
   condition are predicated with that condition, for example

   | if (x)
   |   S1;
   | else
   |   S2;

   S1 will be predicated with "x", and
   S2 will be predicated with "!x".  
         The loop latch is always executed and has no extra conditions
         to be processed: skip it.  
                   Add new condition into destination's predicate list.  
                   If C is true, then TRUE_EDGE is taken.  
                   If C is false, then FALSE_EDGE is taken.  
                 Not handled yet in if-conversion.  
         If current bb has only one successor, then consider it as an
         unconditional goto.  
             The successor bb inherits the predicate of its
             predecessor.  If there is no predicate in the predecessor
             bb, then consider the successor bb as always executed.  
     The loop header is always executed.  
static void predicate_mem_writes ( )
static
@verbatim 

Predicate each write to memory in LOOP.

This function transforms control flow constructs containing memory writes of the form:

| for (i = 0; i < N; i++) | if (cond) | A[i] = expr;

into the following form that does not contain control flow:

| for (i = 0; i < N; i++) | A[i] = cond ? expr : A[i];

The original CFG looks like this:

| bb_0 | i = 0 | end_bb_0 | | bb_1 | if (i < N) goto bb_5 else goto bb_2 | end_bb_1 | | bb_2 | cond = some_computation; | if (cond) goto bb_3 else goto bb_4 | end_bb_2 | | bb_3 | A[i] = expr; | goto bb_4 | end_bb_3 | | bb_4 | goto bb_1 | end_bb_4

insert_gimplified_predicates inserts the computation of the COND expression at the beginning of the destination basic block:

| bb_0 | i = 0 | end_bb_0 | | bb_1 | if (i < N) goto bb_5 else goto bb_2 | end_bb_1 | | bb_2 | cond = some_computation; | if (cond) goto bb_3 else goto bb_4 | end_bb_2 | | bb_3 | cond = some_computation; | A[i] = expr; | goto bb_4 | end_bb_3 | | bb_4 | goto bb_1 | end_bb_4

predicate_mem_writes is then predicating the memory write as follows:

| bb_0 | i = 0 | end_bb_0 | | bb_1 | if (i < N) goto bb_5 else goto bb_2 | end_bb_1 | | bb_2 | if (cond) goto bb_3 else goto bb_4 | end_bb_2 | | bb_3 | cond = some_computation; | A[i] = cond ? expr : A[i]; | goto bb_4 | end_bb_3 | | bb_4 | goto bb_1 | end_bb_4

and finally combine_blocks removes the basic block boundaries making the loop vectorizable:

| bb_0 | i = 0 | if (i < N) goto bb_5 else goto bb_1 | end_bb_0 | | bb_1 | cond = some_computation; | A[i] = cond ? expr : A[i]; | if (i < N) goto bb_5 else goto bb_4 | end_bb_1 | | bb_4 | goto bb_1 | end_bb_4

static void predicate_scalar_phi ( gimple  phi,
tree  cond,
basic_block  true_bb,
gimple_stmt_iterator gsi 
)
static
   Replace a scalar PHI node with a COND_EXPR using COND as condition.
   This routine does not handle PHI nodes with more than two
   arguments.

   For example,
     S1: A = PHI <x1(1), x2(5)>
   is converted into,
     S2: A = cond ? x1 : x2;

   The generated code is inserted at GSI that points to the top of
   basic block's statement list.  When COND is true, phi arg from
   TRUE_BB is selected.  
     Do not handle virtual phi nodes.  
         Use condition that is not TRUTH_NOT_EXPR in conditional modify expr.  
         Build new RHS using selected condition and arguments.  
static void remove_conditions_and_labels ( )
static
   Remove all GIMPLE_CONDs and GIMPLE_LABELs of all the basic blocks
   other than the exit and latch of the LOOP.  Also resets the
   GIMPLE_DEBUG information.  
               ??? Should there be conditional GIMPLE_DEBUG_BINDs?  

References CDI_DOMINATORS, loop::header, loop::latch, loop_exit_edge_p(), make_edge(), redirect_edge_and_branch(), set_immediate_dominator(), and basic_block_def::succs.

static void reset_bb_predicate ( )
inlinestatic
   Free the predicate of BB and reinitialize it with the true
   predicate.  
static void set_bb_predicate ( )
inlinestatic
   Sets the gimplified predicate COND for basic block BB.  

References basic_block_def::aux.

static void set_bb_predicate_gimplified_stmts ( )
inlinestatic
   Sets the sequence of statements STMTS of the gimplification of the
   predicate for basic block BB.  
static bool tree_if_conversion ( )
static
   If-convert LOOP when it is legal.  For the moment this pass has no
   profitability analysis.  Returns true when something changed.  
     Now all statements are if-convertible.  Combine all the basic
     blocks into one huge basic block doing the if-conversion
     on-the-fly.  
static bool write_memrefs_written_at_least_once ( gimple  stmt,
vec< data_reference_p drs 
)
static
   Returns true when the memory references of STMT are unconditionally
   written.  In other words, this function returns true when for every
   data reference A written in STMT, there exist other writes to the
   same data reference with predicates that add up (OR-up) to the true
   predicate: this ensures that the data reference A is written on
   every iteration of the if-converted loop.  

Variable Documentation

basic_block* ifc_bbs
static
@verbatim 

If-conversion for vectorizer. Copyright (C) 2004-2013 Free Software Foundation, Inc. Contributed by Devang Patel dpate.nosp@m.l@ap.nosp@m.ple.c.nosp@m.om

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/.

   This pass implements a tree level if-conversion of loops.  Its
   initial goal is to help the vectorizer to vectorize loops with
   conditions.

   A short description of if-conversion:

     o Decide if a loop is if-convertible or not.
     o Walk all loop basic blocks in breadth first order (BFS order).
       o Remove conditional statements (at the end of basic block)
         and propagate condition into destination basic blocks'
         predicate list.
       o Replace modify expression with conditional modify expression
         using current basic block's condition.
     o Merge all basic blocks
       o Replace phi nodes with conditional modify expr
       o Merge all basic blocks into header

     Sample transformation:

     INPUT
     -----

     # i_23 = PHI <0(0), i_18(10)>;
     <L0>:;
     j_15 = A[i_23];
     if (j_15 > 41) goto <L1>; else goto <L17>;

     <L17>:;
     goto <bb 3> (<L3>);

     <L1>:;

     # iftmp.2_4 = PHI <0(8), 42(2)>;
     <L3>:;
     A[i_23] = iftmp.2_4;
     i_18 = i_23 + 1;
     if (i_18 <= 15) goto <L19>; else goto <L18>;

     <L19>:;
     goto <bb 1> (<L0>);

     <L18>:;

     OUTPUT
     ------

     # i_23 = PHI <0(0), i_18(10)>;
     <L0>:;
     j_15 = A[i_23];

     <L3>:;
     iftmp.2_4 = j_15 > 41 ? 42 : 0;
     A[i_23] = iftmp.2_4;
     i_18 = i_23 + 1;
     if (i_18 <= 15) goto <L19>; else goto <L18>;

     <L19>:;
     goto <bb 1> (<L0>);

     <L18>:;
   List of basic blocks in if-conversion-suitable order.