OSDN Git Service

* call.c (print_z_candidates): Do print viable deleted candidates.
[pf3gnuchains/gcc-fork.git] / gcc / tree-if-conv.c
index c338ecb..1686473 100644 (file)
@@ -89,7 +89,6 @@ along with GCC; see the file COPYING3.  If not see
 #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"
@@ -103,6 +102,106 @@ along with GCC; see the file COPYING3.  If not see
 /* 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, NULL_TREE);
+}
+
+/* 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;
+}
+
 /* Create a new temp variable of type TYPE.  Add GIMPLE_ASSIGN to assign EXP
    to the new variable.  */
 
@@ -130,51 +229,54 @@ ifc_temp_var (tree type, tree exp)
   return stmt;
 }
 
+/* Return true when COND is a true predicate.  */
+
+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));
+}
+
 /* Add condition NEW_COND to the predicate list of basic block BB.  */
 
-static void
+static inline void
 add_to_predicate_list (basic_block bb, tree new_cond)
 {
-  tree cond = (tree) bb->aux;
-
-  if (cond)
-    cond = fold_build2_loc (EXPR_LOCATION (cond),
-                           TRUTH_OR_EXPR, boolean_type_node,
-                           unshare_expr (cond), new_cond);
-  else
-    cond = new_cond;
+  tree cond = bb_predicate (bb);
 
-  bb->aux = cond;
+  set_bb_predicate (bb, is_true_predicate (cond) ? new_cond :
+                   fold_build2_loc (EXPR_LOCATION (cond),
+                                    TRUTH_OR_EXPR, boolean_type_node,
+                                    cond, new_cond));
 }
 
 /* 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;
+  if (!is_true_predicate (prev_cond))
+    cond = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
+                       prev_cond, cond);
 
-      new_cond = fold_build2 (TRUTH_AND_EXPR, boolean_type_node,
-                             unshare_expr (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.  */
@@ -469,20 +571,20 @@ get_loop_body_in_if_conv_order (const struct loop *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)
@@ -490,25 +592,33 @@ 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)
+       {
+         set_bb_predicate (loop->latch, boolean_true_node);
+         set_bb_predicate_gimplified_stmts (loop->latch, NULL);
+         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);
@@ -518,15 +628,7 @@ predicate_bbs (loop_p loop)
            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:
@@ -539,11 +641,10 @@ predicate_bbs (loop_p loop)
                                          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);
 
@@ -555,12 +656,9 @@ predicate_bbs (loop_p loop)
                break;
              }
 
-           case GIMPLE_SWITCH:
+           default:
              /* Not handled yet in if-conversion.  */
              return false;
-
-           default:
-             gcc_unreachable ();
            }
        }
 
@@ -581,24 +679,13 @@ predicate_bbs (loop_p loop)
     }
 
   /* The loop header is always executed.  */
-  loop->header->aux = boolean_true_node;
+  set_bb_predicate (loop->header, boolean_true_node);
+  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,
@@ -712,35 +799,12 @@ if_convertible_loop_p (struct loop *loop)
   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,
@@ -779,7 +843,7 @@ 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)
@@ -797,13 +861,7 @@ find_phi_replacement_condition (struct loop *loop,
       || 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);
@@ -812,15 +870,7 @@ find_phi_replacement_condition (struct loop *loop,
        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
@@ -864,31 +914,37 @@ replace_phi_with_cond_gimple_assign_stmt (gimple phi, tree cond,
   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);
@@ -900,11 +956,11 @@ replace_phi_with_cond_gimple_assign_stmt (gimple phi, tree cond,
     }
 }
 
-/* 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;
@@ -922,12 +978,13 @@ process_phi_nodes (struct loop *loop)
        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))
        {
@@ -936,12 +993,43 @@ process_phi_nodes (struct loop *loop)
          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 (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)
@@ -951,18 +1039,33 @@ 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);
+         }
     }
 }
 
@@ -979,9 +1082,8 @@ combine_blocks (struct loop *loop)
   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.  */
@@ -1085,9 +1187,13 @@ tree_if_conversion (struct loop *loop)
   combine_blocks (loop);
 
  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;
     }