re PR c++/69826 (problem with cilkplus pragma and preprocessor variable)
[platform/upstream/gcc.git] / gcc / tree-ssa-uncprop.c
index 63a2e10..307bb1f 100644 (file)
@@ -1,5 +1,5 @@
 /* Routines for discovering and unpropagating edge equivalences.
-   Copyright (C) 2005-2013 Free Software Foundation, Inc.
+   Copyright (C) 2005-2016 Free Software Foundation, Inc.
 
 This file is part of GCC.
 
@@ -20,27 +20,19 @@ along with GCC; see the file COPYING3.  If not see
 #include "config.h"
 #include "system.h"
 #include "coretypes.h"
-#include "tm.h"
+#include "backend.h"
 #include "tree.h"
-#include "stor-layout.h"
-#include "flags.h"
-#include "tm_p.h"
-#include "basic-block.h"
-#include "function.h"
-#include "hash-table.h"
-#include "tree-ssa-alias.h"
-#include "internal-fn.h"
-#include "gimple-expr.h"
-#include "is-a.h"
 #include "gimple.h"
+#include "tree-pass.h"
+#include "ssa.h"
+#include "fold-const.h"
+#include "cfganal.h"
 #include "gimple-iterator.h"
-#include "gimple-ssa.h"
 #include "tree-cfg.h"
-#include "tree-phinodes.h"
-#include "ssa-iterators.h"
 #include "domwalk.h"
-#include "tree-pass.h"
-#include "tree-ssa-propagate.h"
+#include "tree-hash-traits.h"
+#include "tree-ssa-live.h"
+#include "tree-ssa-coalesce.h"
 
 /* The basic structure describing an equivalency created by traversing
    an edge.  Traversing the edge effectively means that we can assume
@@ -68,7 +60,7 @@ associate_equivalences_with_edges (void)
   FOR_EACH_BB_FN (bb, cfun)
     {
       gimple_stmt_iterator gsi = gsi_last_bb (bb);
-      gimple stmt;
+      gimple *stmt;
 
       /* If the block does not end with a COND_EXPR or SWITCH_EXPR
         then there is nothing to do.  */
@@ -102,23 +94,26 @@ associate_equivalences_with_edges (void)
                 can record an equivalence for OP0 rather than COND.  */
              if (TREE_CODE (op0) == SSA_NAME
                  && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op0)
-                 && TREE_CODE (TREE_TYPE (op0)) == BOOLEAN_TYPE
+                 && ssa_name_has_boolean_range (op0)
                  && is_gimple_min_invariant (op1))
                {
+                 tree true_val = constant_boolean_node (true, TREE_TYPE (op0));
+                 tree false_val = constant_boolean_node (false,
+                                                         TREE_TYPE (op0));
                  if (code == EQ_EXPR)
                    {
                      equivalency = XNEW (struct edge_equivalency);
                      equivalency->lhs = op0;
                      equivalency->rhs = (integer_zerop (op1)
-                                         ? boolean_false_node
-                                         : boolean_true_node);
+                                         ? false_val
+                                         : true_val);
                      true_edge->aux = equivalency;
 
                      equivalency = XNEW (struct edge_equivalency);
                      equivalency->lhs = op0;
                      equivalency->rhs = (integer_zerop (op1)
-                                         ? boolean_true_node
-                                         : boolean_false_node);
+                                         ? true_val
+                                         : false_val);
                      false_edge->aux = equivalency;
                    }
                  else
@@ -126,15 +121,15 @@ associate_equivalences_with_edges (void)
                      equivalency = XNEW (struct edge_equivalency);
                      equivalency->lhs = op0;
                      equivalency->rhs = (integer_zerop (op1)
-                                         ? boolean_true_node
-                                         : boolean_false_node);
+                                         ? true_val
+                                         : false_val);
                      true_edge->aux = equivalency;
 
                      equivalency = XNEW (struct edge_equivalency);
                      equivalency->lhs = op0;
                      equivalency->rhs = (integer_zerop (op1)
-                                         ? boolean_false_node
-                                         : boolean_true_node);
+                                         ? false_val
+                                         : true_val);
                      false_edge->aux = equivalency;
                    }
                }
@@ -149,9 +144,9 @@ associate_equivalences_with_edges (void)
                     the sign of a variable compared against zero.  If
                     we're honoring signed zeros, then we cannot record
                     this value unless we know that the value is nonzero.  */
-                 if (HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op0)))
+                 if (HONOR_SIGNED_ZEROS (op0)
                      && (TREE_CODE (op1) != REAL_CST
-                         || REAL_VALUES_EQUAL (dconst0, TREE_REAL_CST (op1))))
+                         || real_equal (&dconst0, &TREE_REAL_CST (op1))))
                    continue;
 
                  equivalency = XNEW (struct edge_equivalency);
@@ -173,12 +168,13 @@ associate_equivalences_with_edges (void)
         target block creates an equivalence.  */
       else if (gimple_code (stmt) == GIMPLE_SWITCH)
        {
-         tree cond = gimple_switch_index (stmt);
+         gswitch *switch_stmt = as_a <gswitch *> (stmt);
+         tree cond = gimple_switch_index (switch_stmt);
 
          if (TREE_CODE (cond) == SSA_NAME
              && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (cond))
            {
-             int i, n_labels = gimple_switch_num_labels (stmt);
+             int i, n_labels = gimple_switch_num_labels (switch_stmt);
              tree *info = XCNEWVEC (tree, last_basic_block_for_fn (cfun));
 
              /* Walk over the case label vector.  Record blocks
@@ -186,7 +182,7 @@ associate_equivalences_with_edges (void)
                 a single value.  */
              for (i = 0; i < n_labels; i++)
                {
-                 tree label = gimple_switch_label (stmt, i);
+                 tree label = gimple_switch_label (switch_stmt, i);
                  basic_block bb = label_to_block (CASE_LABEL (label));
 
                  if (CASE_HIGH (label)
@@ -282,46 +278,10 @@ struct equiv_hash_elt
   vec<tree> equivalences;
 };
 
-/* Value to ssa name equivalence hashtable helpers.  */
-
-struct val_ssa_equiv_hasher
-{
-  typedef equiv_hash_elt value_type;
-  typedef equiv_hash_elt compare_type;
-  static inline hashval_t hash (const value_type *);
-  static inline bool equal (const value_type *, const compare_type *);
-  static inline void remove (value_type *);
-};
-
-inline hashval_t
-val_ssa_equiv_hasher::hash (const value_type *p)
-{
-  tree const value = p->value;
-  return iterative_hash_expr (value, 0);
-}
-
-inline bool
-val_ssa_equiv_hasher::equal (const value_type *p1, const compare_type *p2)
-{
-  tree value1 = p1->value;
-  tree value2 = p2->value;
-
-  return operand_equal_p (value1, value2, 0);
-}
-
-/* Free an instance of equiv_hash_elt.  */
-
-inline void
-val_ssa_equiv_hasher::remove (value_type *elt)
-{
-  elt->equivalences.release ();
-  free (elt);
-}
-
 /* Global hash table implementing a mapping from invariant values
    to a list of SSA_NAMEs which have the same value.  We might be
    able to reuse tree-vn for this code.  */
-static hash_table <val_ssa_equiv_hasher> val_ssa_equiv;
+static hash_map<tree, auto_vec<tree> > *val_ssa_equiv;
 
 static void uncprop_into_successor_phis (basic_block);
 
@@ -330,16 +290,7 @@ static void uncprop_into_successor_phis (basic_block);
 static void
 remove_equivalence (tree value)
 {
-  struct equiv_hash_elt an_equiv_elt, *an_equiv_elt_p;
-  equiv_hash_elt **slot;
-
-  an_equiv_elt.value = value;
-  an_equiv_elt.equivalences.create (0);
-
-  slot = val_ssa_equiv.find_slot (&an_equiv_elt, NO_INSERT);
-
-  an_equiv_elt_p = *slot;
-  an_equiv_elt_p->equivalences.pop ();
+    val_ssa_equiv->get (value)->pop ();
 }
 
 /* Record EQUIVALENCE = VALUE into our hash table.  */
@@ -347,23 +298,7 @@ remove_equivalence (tree value)
 static void
 record_equiv (tree value, tree equivalence)
 {
-  equiv_hash_elt *an_equiv_elt_p;
-  equiv_hash_elt **slot;
-
-  an_equiv_elt_p = XNEW (struct equiv_hash_elt);
-  an_equiv_elt_p->value = value;
-  an_equiv_elt_p->equivalences.create (0);
-
-  slot = val_ssa_equiv.find_slot (an_equiv_elt_p, INSERT);
-
-  if (*slot == NULL)
-    *slot = an_equiv_elt_p;
-  else
-     free (an_equiv_elt_p);
-
-  an_equiv_elt_p = *slot;
-
-  an_equiv_elt_p->equivalences.safe_push (equivalence);
+  val_ssa_equiv->get_or_insert (value).safe_push (equivalence);
 }
 
 class uncprop_dom_walker : public dom_walker
@@ -371,7 +306,7 @@ class uncprop_dom_walker : public dom_walker
 public:
   uncprop_dom_walker (cdi_direction direction) : dom_walker (direction) {}
 
-  virtual void before_dom_children (basic_block);
+  virtual edge before_dom_children (basic_block);
   virtual void after_dom_children (basic_block);
 
 private:
@@ -380,50 +315,9 @@ private:
      leading to this block.  If no such edge equivalency exists, then we
      record NULL.  These equivalences are live until we leave the dominator
      subtree rooted at the block where we record the equivalency.  */
-  stack_vec<tree, 2> m_equiv_stack;
+  auto_vec<tree, 2> m_equiv_stack;
 };
 
-/* Main driver for un-cprop.  */
-
-static unsigned int
-tree_ssa_uncprop (void)
-{
-  basic_block bb;
-
-  associate_equivalences_with_edges ();
-
-  /* Create our global data structures.  */
-  val_ssa_equiv.create (1024);
-
-  /* We're going to do a dominator walk, so ensure that we have
-     dominance information.  */
-  calculate_dominance_info (CDI_DOMINATORS);
-
-  /* Recursively walk the dominator tree undoing unprofitable
-     constant/copy propagations.  */
-  uncprop_dom_walker (CDI_DOMINATORS).walk (cfun->cfg->x_entry_block_ptr);
-
-  /* we just need to empty elements out of the hash table, and cleanup the
-    AUX field on the edges.  */
-  val_ssa_equiv.dispose ();
-  FOR_EACH_BB_FN (bb, cfun)
-    {
-      edge e;
-      edge_iterator ei;
-
-      FOR_EACH_EDGE (e, ei, bb->succs)
-       {
-         if (e->aux)
-           {
-             free (e->aux);
-             e->aux = NULL;
-           }
-       }
-    }
-  return 0;
-}
-
-
 /* We have finished processing the dominator children of BB, perform
    any finalization actions in preparation for leaving this node in
    the dominator tree.  */
@@ -471,11 +365,9 @@ uncprop_into_successor_phis (basic_block bb)
       /* Walk over the PHI nodes, unpropagating values.  */
       for (gsi = gsi_start (phis) ; !gsi_end_p (gsi); gsi_next (&gsi))
        {
-         gimple phi = gsi_stmt (gsi);
+         gimple *phi = gsi_stmt (gsi);
          tree arg = PHI_ARG_DEF (phi, e->dest_idx);
          tree res = PHI_RESULT (phi);
-         equiv_hash_elt an_equiv_elt;
-         equiv_hash_elt **slot;
 
          /* If the argument is not an invariant and can be potentially
             coalesced with the result, then there's no point in
@@ -485,23 +377,17 @@ uncprop_into_successor_phis (basic_block bb)
            continue;
 
          /* Lookup this argument's value in the hash table.  */
-         an_equiv_elt.value = arg;
-         an_equiv_elt.equivalences.create (0);
-         slot = val_ssa_equiv.find_slot (&an_equiv_elt, NO_INSERT);
-
-         if (slot)
+         vec<tree> *equivalences = val_ssa_equiv->get (arg);
+         if (equivalences)
            {
-             struct equiv_hash_elt *elt = *slot;
-             int j;
-
              /* Walk every equivalence with the same value.  If we find
                 one that can potentially coalesce with the PHI rsult,
                 then replace the value in the argument with its equivalent
                 SSA_NAME.  Use the most recent equivalence as hopefully
                 that results in shortest lifetimes.  */
-             for (j = elt->equivalences.length () - 1; j >= 0; j--)
+             for (int j = equivalences->length () - 1; j >= 0; j--)
                {
-                 tree equiv = elt->equivalences[j];
+                 tree equiv = (*equivalences)[j];
 
                  if (gimple_can_coalesce_p (equiv, res))
                    {
@@ -550,7 +436,7 @@ single_incoming_edge_ignoring_loop_edges (basic_block bb)
   return retval;
 }
 
-void
+edge
 uncprop_dom_walker::before_dom_children (basic_block bb)
 {
   basic_block parent;
@@ -579,12 +465,7 @@ uncprop_dom_walker::before_dom_children (basic_block bb)
     m_equiv_stack.safe_push (NULL_TREE);
 
   uncprop_into_successor_phis (bb);
-}
-
-static bool
-gate_uncprop (void)
-{
-  return flag_tree_dom != 0;
+  return NULL;
 }
 
 namespace {
@@ -594,14 +475,12 @@ const pass_data pass_data_uncprop =
   GIMPLE_PASS, /* type */
   "uncprop", /* name */
   OPTGROUP_NONE, /* optinfo_flags */
-  true, /* has_gate */
-  true, /* has_execute */
   TV_TREE_SSA_UNCPROP, /* tv_id */
   ( PROP_cfg | PROP_ssa ), /* properties_required */
   0, /* properties_provided */
   0, /* properties_destroyed */
   0, /* todo_flags_start */
-  TODO_verify_ssa, /* todo_flags_finish */
+  0, /* todo_flags_finish */
 };
 
 class pass_uncprop : public gimple_opt_pass
@@ -613,11 +492,50 @@ public:
 
   /* opt_pass methods: */
   opt_pass * clone () { return new pass_uncprop (m_ctxt); }
-  bool gate () { return gate_uncprop (); }
-  unsigned int execute () { return tree_ssa_uncprop (); }
+  virtual bool gate (function *) { return flag_tree_dom != 0; }
+  virtual unsigned int execute (function *);
 
 }; // class pass_uncprop
 
+unsigned int
+pass_uncprop::execute (function *fun)
+{
+  basic_block bb;
+
+  associate_equivalences_with_edges ();
+
+  /* Create our global data structures.  */
+  val_ssa_equiv = new hash_map<tree, auto_vec<tree> > (1024);
+
+  /* We're going to do a dominator walk, so ensure that we have
+     dominance information.  */
+  calculate_dominance_info (CDI_DOMINATORS);
+
+  /* Recursively walk the dominator tree undoing unprofitable
+     constant/copy propagations.  */
+  uncprop_dom_walker (CDI_DOMINATORS).walk (fun->cfg->x_entry_block_ptr);
+
+  /* we just need to empty elements out of the hash table, and cleanup the
+    AUX field on the edges.  */
+  delete val_ssa_equiv;
+  val_ssa_equiv = NULL;
+  FOR_EACH_BB_FN (bb, fun)
+    {
+      edge e;
+      edge_iterator ei;
+
+      FOR_EACH_EDGE (e, ei, bb->succs)
+       {
+         if (e->aux)
+           {
+             free (e->aux);
+             e->aux = NULL;
+           }
+       }
+    }
+  return 0;
+}
+
 } // anon namespace
 
 gimple_opt_pass *