#include "flags.h"
#include "timevar.h"
#include "basic-block.h"
-#include "diagnostic.h"
#include "tree-pretty-print.h"
#include "gimple-pretty-print.h"
#include "tree-flow.h"
#include "tree-data-ref.h"
#include "tree-scalar-evolution.h"
#include "tree-pass.h"
+#include "dbgcnt.h"
/* List of basic blocks in if-conversion-suitable order. */
static basic_block *ifc_bbs;
+/* Structure used to predicate basic blocks. This is attached to the
+ ->aux field of the BBs in the loop to be if-converted. */
+typedef struct bb_predicate_s {
+
+ /* The condition under which this basic block is executed. */
+ tree predicate;
+
+ /* PREDICATE is gimplified, and the sequence of statements is
+ recorded here, in order to avoid the duplication of computations
+ that occur in previous conditions. See PR44483. */
+ gimple_seq predicate_gimplified_stmts;
+} *bb_predicate_p;
+
+/* Returns true when the basic block BB has a predicate. */
+
+static inline bool
+bb_has_predicate (basic_block bb)
+{
+ return bb->aux != NULL;
+}
+
+/* Returns the gimplified predicate for basic block BB. */
+
+static inline tree
+bb_predicate (basic_block bb)
+{
+ return ((bb_predicate_p) bb->aux)->predicate;
+}
+
+/* Sets the gimplified predicate COND for basic block BB. */
+
+static inline void
+set_bb_predicate (basic_block bb, tree cond)
+{
+ ((bb_predicate_p) bb->aux)->predicate = cond;
+}
+
+/* Returns the sequence of statements of the gimplification of the
+ predicate for basic block BB. */
+
+static inline gimple_seq
+bb_predicate_gimplified_stmts (basic_block bb)
+{
+ return ((bb_predicate_p) bb->aux)->predicate_gimplified_stmts;
+}
+
+/* Sets the sequence of statements STMTS of the gimplification of the
+ predicate for basic block BB. */
+
+static inline void
+set_bb_predicate_gimplified_stmts (basic_block bb, gimple_seq stmts)
+{
+ ((bb_predicate_p) bb->aux)->predicate_gimplified_stmts = stmts;
+}
+
+/* Adds the sequence of statements STMTS to the sequence of statements
+ of the predicate for basic block BB. */
+
+static inline void
+add_bb_predicate_gimplified_stmts (basic_block bb, gimple_seq stmts)
+{
+ gimple_seq_add_seq
+ (&(((bb_predicate_p) bb->aux)->predicate_gimplified_stmts), stmts);
+}
+
+/* Initializes to TRUE the predicate of basic block BB. */
+
+static inline void
+init_bb_predicate (basic_block bb)
+{
+ bb->aux = XNEW (struct bb_predicate_s);
+ set_bb_predicate_gimplified_stmts (bb, NULL);
+ set_bb_predicate (bb, boolean_true_node);
+}
+
+/* Free the predicate of basic block BB. */
+
+static inline void
+free_bb_predicate (basic_block bb)
+{
+ gimple_seq stmts;
+
+ if (!bb_has_predicate (bb))
+ return;
+
+ /* Release the SSA_NAMEs created for the gimplification of the
+ predicate. */
+ stmts = bb_predicate_gimplified_stmts (bb);
+ if (stmts)
+ {
+ gimple_stmt_iterator i;
+
+ for (i = gsi_start (stmts); !gsi_end_p (i); gsi_next (&i))
+ free_stmt_operands (gsi_stmt (i));
+ }
+
+ free (bb->aux);
+ bb->aux = NULL;
+}
+
+/* Free the predicate of BB and reinitialize it with the true
+ predicate. */
+
+static inline void
+reset_bb_predicate (basic_block bb)
+{
+ free_bb_predicate (bb);
+ init_bb_predicate (bb);
+}
+
/* Create a new temp variable of type TYPE. Add GIMPLE_ASSIGN to assign EXP
to the new variable. */
return stmt;
}
-/* Add condition NEW_COND to the predicate list of basic block BB. */
+/* Return true when COND is a true predicate. */
-static void
-add_to_predicate_list (basic_block bb, tree new_cond)
+static inline bool
+is_true_predicate (tree cond)
+{
+ return (cond == NULL_TREE
+ || cond == boolean_true_node
+ || integer_onep (cond));
+}
+
+/* Returns true when BB has a predicate that is not trivial: true or
+ NULL_TREE. */
+
+static inline bool
+is_predicated (basic_block bb)
+{
+ return !is_true_predicate (bb_predicate (bb));
+}
+
+/* Parses the predicate COND and returns its comparison code and
+ operands OP0 and OP1. */
+
+static enum tree_code
+parse_predicate (tree cond, tree *op0, tree *op1)
+{
+ gimple s;
+
+ if (TREE_CODE (cond) == SSA_NAME
+ && is_gimple_assign (s = SSA_NAME_DEF_STMT (cond)))
+ {
+ if (TREE_CODE_CLASS (gimple_assign_rhs_code (s)) == tcc_comparison)
+ {
+ *op0 = gimple_assign_rhs1 (s);
+ *op1 = gimple_assign_rhs2 (s);
+ return gimple_assign_rhs_code (s);
+ }
+
+ else if (gimple_assign_rhs_code (s) == TRUTH_NOT_EXPR)
+ {
+ tree op = gimple_assign_rhs1 (s);
+ tree type = TREE_TYPE (op);
+ enum tree_code code = parse_predicate (op, op0, op1);
+
+ return code == ERROR_MARK ? ERROR_MARK
+ : invert_tree_comparison (code, HONOR_NANS (TYPE_MODE (type)));
+ }
+
+ return ERROR_MARK;
+ }
+
+ if (TREE_CODE_CLASS (TREE_CODE (cond)) == tcc_comparison)
+ {
+ *op0 = TREE_OPERAND (cond, 0);
+ *op1 = TREE_OPERAND (cond, 1);
+ return TREE_CODE (cond);
+ }
+
+ return ERROR_MARK;
+}
+
+/* Returns the fold of predicate C1 OR C2 at location LOC. */
+
+static tree
+fold_or_predicates (location_t loc, tree c1, tree c2)
{
- tree cond = (tree) bb->aux;
+ tree op1a, op1b, op2a, op2b;
+ enum tree_code code1 = parse_predicate (c1, &op1a, &op1b);
+ enum tree_code code2 = parse_predicate (c2, &op2a, &op2b);
+
+ if (code1 != ERROR_MARK && code2 != ERROR_MARK)
+ {
+ tree t = maybe_fold_or_comparisons (code1, op1a, op1b,
+ code2, op2a, op2b);
+ if (t)
+ return t;
+ }
+
+ return fold_build2_loc (loc, TRUTH_OR_EXPR, boolean_type_node, c1, c2);
+}
+
+/* Add condition NC to the predicate list of basic block BB. */
- if (cond)
- cond = fold_build2_loc (EXPR_LOCATION (cond),
- TRUTH_OR_EXPR, boolean_type_node,
- unshare_expr (cond), new_cond);
+static inline void
+add_to_predicate_list (basic_block bb, tree nc)
+{
+ tree bc;
+
+ if (is_true_predicate (nc))
+ return;
+
+ if (!is_predicated (bb))
+ bc = nc;
else
- cond = new_cond;
+ {
+ bc = bb_predicate (bb);
+ bc = fold_or_predicates (EXPR_LOCATION (bc), nc, bc);
+ }
- bb->aux = cond;
+ if (!is_gimple_condexpr (bc))
+ {
+ gimple_seq stmts;
+ bc = force_gimple_operand (bc, &stmts, true, NULL_TREE);
+ add_bb_predicate_gimplified_stmts (bb, stmts);
+ }
+
+ if (is_true_predicate (bc))
+ reset_bb_predicate (bb);
+ else
+ set_bb_predicate (bb, bc);
}
/* 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. */
-static tree
+static void
add_to_dst_predicate_list (struct loop *loop, edge e,
tree prev_cond, tree cond)
{
- tree new_cond = NULL_TREE;
-
if (!flow_bb_inside_loop_p (loop, e->dest))
- return NULL_TREE;
+ return;
- if (prev_cond == boolean_true_node || !prev_cond)
- new_cond = unshare_expr (cond);
- else
- {
- /* Add the condition COND to the e->aux field. In case the edge
- destination is a PHI node, this condition will be added to
- the block predicate to construct a complete condition. */
- e->aux = cond;
-
- new_cond = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
- unshare_expr (prev_cond), cond);
- }
+ if (!is_true_predicate (prev_cond))
+ cond = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
+ prev_cond, cond);
- add_to_predicate_list (e->dest, new_cond);
- return new_cond;
+ add_to_predicate_list (e->dest, cond);
}
/* Return true if one of the successor edges of BB exits LOOP. */
/* Returns true when the analysis of the predicates for all the basic
blocks in LOOP succeeded.
- predicate_bbs first clears the ->aux fields of the edges and 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
+ 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". */
+ S1 will be predicated with "x", and
+ S2 will be predicated with "!x". */
static bool
predicate_bbs (loop_p loop)
unsigned int i;
for (i = 0; i < loop->num_nodes; i++)
- {
- edge e;
- edge_iterator ei;
- basic_block bb = ifc_bbs [i];
- gimple_stmt_iterator itr = gsi_start_phis (bb);
-
- if (!gsi_end_p (itr))
- FOR_EACH_EDGE (e, ei, bb->preds)
- e->aux = NULL;
-
- bb->aux = NULL;
- }
+ init_bb_predicate (ifc_bbs[i]);
for (i = 0; i < loop->num_nodes; i++)
{
- basic_block bb = ifc_bbs [i];
- tree cond = (tree) bb->aux;
+ basic_block bb = ifc_bbs[i];
+ tree cond;
gimple_stmt_iterator itr;
+ /* The loop latch is always executed and has no extra conditions
+ to be processed: skip it. */
+ if (bb == loop->latch)
+ {
+ reset_bb_predicate (loop->latch);
+ continue;
+ }
+
+ cond = bb_predicate (bb);
+ if (cond
+ && bb != loop->header)
+ {
+ gimple_seq stmts;
+
+ cond = force_gimple_operand (cond, &stmts, true, NULL_TREE);
+ add_bb_predicate_gimplified_stmts (bb, stmts);
+ }
+
for (itr = gsi_start_bb (bb); !gsi_end_p (itr); gsi_next (&itr))
{
gimple stmt = gsi_stmt (itr);
case GIMPLE_LABEL:
case GIMPLE_ASSIGN:
case GIMPLE_CALL:
- break;
-
case GIMPLE_DEBUG:
- /* ??? Should there be conditional GIMPLE_DEBUG_BINDs? */
- if (gimple_debug_bind_p (gsi_stmt (itr)))
- {
- gimple_debug_bind_reset_value (gsi_stmt (itr));
- update_stmt (gsi_stmt (itr));
- }
break;
case GIMPLE_COND:
gimple_cond_lhs (stmt),
gimple_cond_rhs (stmt));
+ /* Add new condition into destination's predicate list. */
extract_true_false_edges_from_block (gimple_bb (stmt),
&true_edge, &false_edge);
- /* Add new condition into destination's predicate list. */
-
/* If C is true, then TRUE_EDGE is taken. */
add_to_dst_predicate_list (loop, true_edge, cond, c);
break;
}
- case GIMPLE_SWITCH:
+ default:
/* Not handled yet in if-conversion. */
return false;
-
- default:
- gcc_unreachable ();
}
}
}
/* The loop header is always executed. */
- loop->header->aux = boolean_true_node;
+ reset_bb_predicate (loop->header);
+ gcc_assert (bb_predicate_gimplified_stmts (loop->header) == NULL
+ && bb_predicate_gimplified_stmts (loop->latch) == NULL);
return true;
}
-/* Returns true when BB has a predicate that is not trivial: true or
- NULL_TREE. */
-
-static bool
-is_predicated (basic_block bb)
-{
- tree cond = (tree) bb->aux;
-
- return (cond != NULL_TREE
- && cond != boolean_true_node
- && !integer_onep (cond));
-}
-
/* Return true when LOOP is if-convertible.
LOOP is if-convertible if:
- it is innermost,
basic_block bb = ifc_bbs[i];
gimple_stmt_iterator itr;
+ for (itr = gsi_start_phis (bb); !gsi_end_p (itr); gsi_next (&itr))
+ if (!if_convertible_phi_p (loop, bb, gsi_stmt (itr)))
+ return false;
+
/* For non predicated BBs, don't check their statements. */
if (!is_predicated (bb))
continue;
for (itr = gsi_start_bb (bb); !gsi_end_p (itr); gsi_next (&itr))
if (!if_convertible_stmt_p (loop, bb, gsi_stmt (itr)))
return false;
-
- for (itr = gsi_start_phis (bb); !gsi_end_p (itr); gsi_next (&itr))
- if (!if_convertible_phi_p (loop, bb, gsi_stmt (itr)))
- return false;
}
if (dump_file)
return true;
}
-/* During if-conversion, the bb->aux field is used to hold a predicate
- list. This function cleans for all the basic blocks in the given
- LOOP their predicate list. It also cleans up the e->aux field of
- all the successor edges: e->aux is used to hold the true and false
- conditions for conditional expressions. */
-
-static void
-clean_predicate_lists (struct loop *loop)
-{
- basic_block *bb;
- unsigned int i;
- edge e;
- edge_iterator ei;
-
- bb = get_loop_body (loop);
- for (i = 0; i < loop->num_nodes; i++)
- {
- bb[i]->aux = NULL;
- FOR_EACH_EDGE (e, ei, bb[i]->succs)
- e->aux = NULL;
- }
- free (bb);
-}
-
-/* Basic block BB has two predecessors. Using predecessor's bb->aux
- field, set appropriate condition COND for the PHI node replacement.
- Return 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. */
+/* 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. */
static basic_block
find_phi_replacement_condition (struct loop *loop,
See PR23115. */
/* Select condition that is not TRUTH_NOT_EXPR. */
- tmp_cond = (tree) (first_edge->src)->aux;
+ tmp_cond = bb_predicate (first_edge->src);
gcc_assert (tmp_cond);
if (TREE_CODE (tmp_cond) == TRUTH_NOT_EXPR)
|| dominated_by_p (CDI_DOMINATORS,
second_edge->src, first_edge->src))
{
- *cond = (tree) (second_edge->src)->aux;
-
- /* If there is a condition on an incoming edge, add it to the
- incoming bb predicate. */
- if (second_edge->aux)
- *cond = build2 (TRUTH_AND_EXPR, boolean_type_node,
- *cond, (tree) second_edge->aux);
+ *cond = bb_predicate (second_edge->src);
if (TREE_CODE (*cond) == TRUTH_NOT_EXPR)
*cond = invert_truthvalue (*cond);
first_edge = second_edge;
}
else
- {
- *cond = (tree) (first_edge->src)->aux;
-
- /* If there is a condition on an incoming edge, add it to the
- incoming bb predicate. */
- if (first_edge->aux)
- *cond = build2 (TRUTH_AND_EXPR, boolean_type_node,
- *cond, (tree) first_edge->aux);
- }
+ *cond = bb_predicate (first_edge->src);
/* Gimplify the condition: the vectorizer prefers to have gimple
values as conditions. Various targets use different means to
gimple new_stmt;
basic_block bb;
tree rhs;
- tree arg_0, arg_1;
+ tree arg;
gcc_assert (gimple_code (phi) == GIMPLE_PHI
&& gimple_phi_num_args (phi) == 2);
bb = gimple_bb (phi);
- /* Use condition that is not TRUTH_NOT_EXPR in conditional modify expr. */
- if (EDGE_PRED (bb, 1)->src == true_bb)
- {
- arg_0 = gimple_phi_arg_def (phi, 1);
- arg_1 = gimple_phi_arg_def (phi, 0);
- }
+ arg = degenerate_phi_result (phi);
+ if (arg)
+ rhs = arg;
else
{
- arg_0 = gimple_phi_arg_def (phi, 0);
- arg_1 = gimple_phi_arg_def (phi, 1);
- }
+ tree arg_0, arg_1;
+ /* Use condition that is not TRUTH_NOT_EXPR in conditional modify expr. */
+ if (EDGE_PRED (bb, 1)->src == true_bb)
+ {
+ arg_0 = gimple_phi_arg_def (phi, 1);
+ arg_1 = gimple_phi_arg_def (phi, 0);
+ }
+ else
+ {
+ arg_0 = gimple_phi_arg_def (phi, 0);
+ arg_1 = gimple_phi_arg_def (phi, 1);
+ }
- /* Build new RHS using selected condition and arguments. */
- rhs = build3 (COND_EXPR, TREE_TYPE (PHI_RESULT (phi)),
- unshare_expr (cond), unshare_expr (arg_0),
- unshare_expr (arg_1));
+ /* Build new RHS using selected condition and arguments. */
+ rhs = build3 (COND_EXPR, TREE_TYPE (PHI_RESULT (phi)),
+ unshare_expr (cond), arg_0, arg_1);
+ }
- new_stmt = gimple_build_assign (unshare_expr (PHI_RESULT (phi)), rhs);
+ new_stmt = gimple_build_assign (PHI_RESULT (phi), rhs);
SSA_NAME_DEF_STMT (gimple_phi_result (phi)) = new_stmt;
gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
update_stmt (new_stmt);
}
}
-/* Process phi nodes for the given LOOP. Replace phi nodes with
- conditional modify expressions. */
+/* Replaces in LOOP all the phi nodes other than those in the
+ LOOP->header block with conditional modify expressions. */
static void
-process_phi_nodes (struct loop *loop)
+ifconvert_phi_nodes (struct loop *loop)
{
basic_block bb;
unsigned int orig_loop_num_nodes = loop->num_nodes;
continue;
phi_gsi = gsi_start_phis (bb);
- gsi = gsi_after_labels (bb);
+ if (gsi_end_p (phi_gsi))
+ continue;
/* BB has two predecessors. Using predecessor's aux field, set
appropriate condition for the PHI node replacement. */
- if (!gsi_end_p (phi_gsi))
- true_bb = find_phi_replacement_condition (loop, bb, &cond, &gsi);
+ gsi = gsi_after_labels (bb);
+ true_bb = find_phi_replacement_condition (loop, bb, &cond, &gsi);
while (!gsi_end_p (phi_gsi))
{
release_phi_node (phi);
gsi_next (&phi_gsi);
}
+
set_phi_nodes (bb, NULL);
}
}
+/* Insert in each basic block of LOOP the statements produced by the
+ gimplification of the predicates. */
+
+static void
+insert_gimplified_predicates (loop_p loop)
+{
+ unsigned int i;
+
+ for (i = 0; i < loop->num_nodes; i++)
+ {
+ basic_block bb = ifc_bbs[i];
+ gimple_seq stmts = bb_predicate_gimplified_stmts (bb);
+
+ if (!is_predicated (bb))
+ {
+ /* 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. */
+ reset_bb_predicate (bb);
+ continue;
+ }
+
+ if (stmts)
+ {
+ gimple_stmt_iterator gsi = gsi_last_bb (bb);
+
+ if (gsi_end_p (gsi)
+ || gimple_code (gsi_stmt (gsi)) == GIMPLE_COND)
+ gsi_insert_seq_before (&gsi, stmts, GSI_SAME_STMT);
+ else
+ gsi_insert_seq_after (&gsi, stmts, GSI_SAME_STMT);
+
+ /* Once the sequence is code generated, set it to NULL. */
+ set_bb_predicate_gimplified_stmts (bb, NULL);
+ }
+ }
+}
+
/* Remove all GIMPLE_CONDs and GIMPLE_LABELs of all the basic blocks
- other than the exit and latch of the LOOP. */
+ other than the exit and latch of the LOOP. Also resets the
+ GIMPLE_DEBUG information. */
static void
remove_conditions_and_labels (loop_p loop)
for (i = 0; i < loop->num_nodes; i++)
{
- basic_block bb = ifc_bbs [i];
+ basic_block bb = ifc_bbs[i];
if (bb_with_exit_edge_p (loop, bb)
|| bb == loop->latch)
continue;
for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
- if (gimple_code (gsi_stmt (gsi)) == GIMPLE_COND
- || gimple_code (gsi_stmt (gsi)) == GIMPLE_LABEL)
- gsi_remove (&gsi, true);
- else
- gsi_next (&gsi);
+ switch (gimple_code (gsi_stmt (gsi)))
+ {
+ case GIMPLE_COND:
+ case GIMPLE_LABEL:
+ gsi_remove (&gsi, true);
+ break;
+
+ case GIMPLE_DEBUG:
+ /* ??? Should there be conditional GIMPLE_DEBUG_BINDs? */
+ if (gimple_debug_bind_p (gsi_stmt (gsi)))
+ {
+ gimple_debug_bind_reset_value (gsi_stmt (gsi));
+ update_stmt (gsi_stmt (gsi));
+ }
+ gsi_next (&gsi);
+ break;
+
+ default:
+ gsi_next (&gsi);
+ }
}
}
edge_iterator ei;
remove_conditions_and_labels (loop);
-
- /* Process phi nodes to prepare blocks for merge. */
- process_phi_nodes (loop);
+ insert_gimplified_predicates (loop);
+ ifconvert_phi_nodes (loop);
/* Merge basic blocks: first remove all the edges in the loop,
except for those from the exit block. */
/* 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.
-
- FIXME: Call cleanup_tree_cfg. */
+ vectorizer that handles only loops with two nodes. */
if (exit_bb
&& exit_bb != loop->header
&& can_merge_blocks_p (loop->header, exit_bb))
}
/* If-convert LOOP when it is legal. For the moment this pass has no
- profitability analysis. */
+ profitability analysis. Returns true when something changed. */
-static void
+static bool
tree_if_conversion (struct loop *loop)
{
+ bool changed = false;
ifc_bbs = NULL;
- if (!if_convertible_loop_p (loop))
+ if (!if_convertible_loop_p (loop)
+ || !dbg_cnt (if_conversion_tree))
goto cleanup;
/* Now all statements are if-convertible. Combine all the basic
blocks into one huge basic block doing the if-conversion
on-the-fly. */
combine_blocks (loop);
+ changed = true;
cleanup:
- clean_predicate_lists (loop);
if (ifc_bbs)
{
+ unsigned int i;
+
+ for (i = 0; i < loop->num_nodes; i++)
+ free_bb_predicate (ifc_bbs[i]);
+
free (ifc_bbs);
ifc_bbs = NULL;
}
+
+ return changed;
}
/* Tree if-conversion pass management. */
{
loop_iterator li;
struct loop *loop;
+ bool changed = false;
if (number_of_loops () <= 1)
return 0;
FOR_EACH_LOOP (li, loop, 0)
- tree_if_conversion (loop);
+ changed |= tree_if_conversion (loop);
- return 0;
+ return changed ? TODO_cleanup_cfg : 0;
}
+/* Returns true when the if-conversion pass is enabled. */
+
static bool
gate_tree_if_conversion (void)
{
- return flag_tree_vectorize != 0;
+ return ((flag_tree_vectorize && flag_tree_loop_if_convert != 0)
+ || flag_tree_loop_if_convert == 1);
}
struct gimple_opt_pass pass_if_conversion =