re PR target/65697 (__atomic memory barriers not strong enough for __sync builtins)
[gcc.git] / gcc / tree-ssa-ccp.c
index 1d7cafeb80790a6eac17ac5e58c144aa00e5797d..496d84006fd7460245c739b0ba8be35fdb9cb47d 100644 (file)
@@ -122,16 +122,8 @@ along with GCC; see the file COPYING3.  If not see
 #include "system.h"
 #include "coretypes.h"
 #include "tm.h"
-#include "hash-set.h"
-#include "machmode.h"
-#include "vec.h"
-#include "double-int.h"
-#include "input.h"
 #include "alias.h"
 #include "symtab.h"
-#include "wide-int.h"
-#include "inchash.h"
-#include "real.h"
 #include "tree.h"
 #include "fold-const.h"
 #include "stor-layout.h"
@@ -139,19 +131,16 @@ along with GCC; see the file COPYING3.  If not see
 #include "tm_p.h"
 #include "predict.h"
 #include "hard-reg-set.h"
-#include "input.h"
 #include "function.h"
 #include "dominance.h"
 #include "cfg.h"
 #include "basic-block.h"
 #include "gimple-pretty-print.h"
-#include "hash-table.h"
 #include "tree-ssa-alias.h"
 #include "internal-fn.h"
 #include "gimple-fold.h"
 #include "tree-eh.h"
 #include "gimple-expr.h"
-#include "is-a.h"
 #include "gimple.h"
 #include "gimplify.h"
 #include "gimple-iterator.h"
@@ -208,6 +197,7 @@ static unsigned n_const_val;
 
 static void canonicalize_value (ccp_prop_value_t *);
 static bool ccp_fold_stmt (gimple_stmt_iterator *);
+static void ccp_lattice_meet (ccp_prop_value_t *, ccp_prop_value_t *);
 
 /* Dump constant propagation value VAL to file OUTF prefixed by PREFIX.  */
 
@@ -512,49 +502,51 @@ valid_lattice_transition (ccp_prop_value_t old_val, ccp_prop_value_t new_val)
    value is different from VAR's previous value.  */
 
 static bool
-set_lattice_value (tree var, ccp_prop_value_t new_val)
+set_lattice_value (tree var, ccp_prop_value_t *new_val)
 {
   /* We can deal with old UNINITIALIZED values just fine here.  */
   ccp_prop_value_t *old_val = &const_val[SSA_NAME_VERSION (var)];
 
-  canonicalize_value (&new_val);
+  canonicalize_value (new_val);
 
   /* We have to be careful to not go up the bitwise lattice
-     represented by the mask.
-     ???  This doesn't seem to be the best place to enforce this.  */
-  if (new_val.lattice_val == CONSTANT
+     represented by the mask.  Instead of dropping to VARYING
+     use the meet operator to retain a conservative value.
+     Missed optimizations like PR65851 makes this necessary.
+     It also ensures we converge to a stable lattice solution.  */
+  if (new_val->lattice_val == CONSTANT
       && old_val->lattice_val == CONSTANT
-      && TREE_CODE (new_val.value) == INTEGER_CST
-      && TREE_CODE (old_val->value) == INTEGER_CST)
-    {
-      widest_int diff = (wi::to_widest (new_val.value)
-                        ^ wi::to_widest (old_val->value));
-      new_val.mask = new_val.mask | old_val->mask | diff;
-    }
+      && TREE_CODE (new_val->value) != SSA_NAME)
+    ccp_lattice_meet (new_val, old_val);
 
-  gcc_checking_assert (valid_lattice_transition (*old_val, new_val));
+  gcc_checking_assert (valid_lattice_transition (*old_val, *new_val));
 
   /* If *OLD_VAL and NEW_VAL are the same, return false to inform the
      caller that this was a non-transition.  */
-  if (old_val->lattice_val != new_val.lattice_val
-      || (new_val.lattice_val == CONSTANT
-         && (TREE_CODE (new_val.value) != TREE_CODE (old_val->value)
-             || simple_cst_equal (new_val.value, old_val->value) != 1
-             || (TREE_CODE (new_val.value) == INTEGER_CST
-                 && new_val.mask != old_val->mask))))
+  if (old_val->lattice_val != new_val->lattice_val
+      || (new_val->lattice_val == CONSTANT
+         && (TREE_CODE (new_val->value) != TREE_CODE (old_val->value)
+             || (TREE_CODE (new_val->value) == INTEGER_CST
+                 && (new_val->mask != old_val->mask
+                     || (wi::bit_and_not (wi::to_widest (old_val->value),
+                                          new_val->mask)
+                         != wi::bit_and_not (wi::to_widest (new_val->value),
+                                             new_val->mask))))
+             || (TREE_CODE (new_val->value) != INTEGER_CST
+                 && !operand_equal_p (new_val->value, old_val->value, 0)))))
     {
       /* ???  We would like to delay creation of INTEGER_CSTs from
         partially constants here.  */
 
       if (dump_file && (dump_flags & TDF_DETAILS))
        {
-         dump_lattice_value (dump_file, "Lattice value changed to ", new_val);
+         dump_lattice_value (dump_file, "Lattice value changed to ", *new_val);
          fprintf (dump_file, ".  Adding SSA edges to worklist.\n");
        }
 
-      *old_val = new_val;
+      *old_val = *new_val;
 
-      gcc_assert (new_val.lattice_val != UNINITIALIZED);
+      gcc_assert (new_val->lattice_val != UNINITIALIZED);
       return true;
     }
 
@@ -623,6 +615,15 @@ get_value_for_expr (tree expr, bool for_bits_p)
          && val.lattice_val == CONSTANT
          && TREE_CODE (val.value) == ADDR_EXPR)
        val = get_value_from_alignment (val.value);
+      /* Fall back to a copy value.  */
+      if (!for_bits_p
+         && val.lattice_val == VARYING
+         && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (expr))
+       {
+         val.lattice_val = CONSTANT;
+         val.value = expr;
+         val.mask = -1;
+       }
     }
   else if (is_gimple_min_invariant (expr)
           && (!for_bits_p || TREE_CODE (expr) != ADDR_EXPR))
@@ -977,8 +978,7 @@ ccp_finalize (void)
    */
 
 static void
-ccp_lattice_meet (basic_block where,
-                 ccp_prop_value_t *val1, ccp_prop_value_t *val2)
+ccp_lattice_meet (ccp_prop_value_t *val1, ccp_prop_value_t *val2)
 {
   if (val1->lattice_val == UNDEFINED
       /* For UNDEFINED M SSA we can't always SSA because its definition
@@ -1028,7 +1028,7 @@ ccp_lattice_meet (basic_block where,
     }
   else if (val1->lattice_val == CONSTANT
           && val2->lattice_val == CONSTANT
-          && simple_cst_equal (val1->value, val2->value) == 1)
+          && operand_equal_p (val1->value, val2->value, 0))
     {
       /* Ci M Cj = Ci          if (i == j)
         Ci M Cj = VARYING      if (i != j)
@@ -1047,7 +1047,7 @@ ccp_lattice_meet (basic_block where,
        *val1 = get_value_for_expr (val1->value, true);
       if (TREE_CODE (val2->value) == ADDR_EXPR)
        tem = get_value_for_expr (val2->value, true);
-      ccp_lattice_meet (where, val1, &tem);
+      ccp_lattice_meet (val1, &tem);
     }
   else
     {
@@ -1068,7 +1068,7 @@ static enum ssa_prop_result
 ccp_visit_phi_node (gphi *phi)
 {
   unsigned i;
-  ccp_prop_value_t *old_val, new_val;
+  ccp_prop_value_t new_val;
 
   if (dump_file && (dump_flags & TDF_DETAILS))
     {
@@ -1076,25 +1076,11 @@ ccp_visit_phi_node (gphi *phi)
       print_gimple_stmt (dump_file, phi, 0, dump_flags);
     }
 
-  old_val = get_value (gimple_phi_result (phi));
-  switch (old_val->lattice_val)
-    {
-    case VARYING:
-      return SSA_PROP_VARYING;
-
-    case CONSTANT:
-      new_val = *old_val;
-      break;
-
-    case UNDEFINED:
-      new_val.lattice_val = UNDEFINED;
-      new_val.value = NULL_TREE;
-      break;
-
-    default:
-      gcc_unreachable ();
-    }
+  new_val.lattice_val = UNDEFINED;
+  new_val.value = NULL_TREE;
+  new_val.mask = 0;
 
+  bool first = true;
   for (i = 0; i < gimple_phi_num_args (phi); i++)
     {
       /* Compute the meet operator over all the PHI arguments flowing
@@ -1116,7 +1102,13 @@ ccp_visit_phi_node (gphi *phi)
          tree arg = gimple_phi_arg (phi, i)->def;
          ccp_prop_value_t arg_val = get_value_for_expr (arg, false);
 
-         ccp_lattice_meet (gimple_bb (phi), &new_val, &arg_val);
+         if (first)
+           {
+             new_val = arg_val;
+             first = false;
+           }
+         else
+           ccp_lattice_meet (&new_val, &arg_val);
 
          if (dump_file && (dump_flags & TDF_DETAILS))
            {
@@ -1138,7 +1130,7 @@ ccp_visit_phi_node (gphi *phi)
     }
 
   /* Make the transition to the new value.  */
-  if (set_lattice_value (gimple_phi_result (phi), new_val))
+  if (set_lattice_value (gimple_phi_result (phi), &new_val))
     {
       if (new_val.lattice_val == VARYING)
        return SSA_PROP_VARYING;
@@ -1739,6 +1731,15 @@ evaluate_stmt (gimple stmt)
     {
       fold_defer_overflow_warnings ();
       simplified = ccp_fold (stmt);
+      if (simplified && TREE_CODE (simplified) == SSA_NAME)
+       {
+         val = *get_value (simplified);
+         if (val.lattice_val != VARYING)
+           {
+             fold_undefer_overflow_warnings (true, stmt, 0);
+             return val;
+           }
+       }
       is_constant = simplified && is_gimple_min_invariant (simplified);
       fold_undefer_overflow_warnings (is_constant, stmt, 0);
       if (is_constant)
@@ -1747,6 +1748,7 @@ evaluate_stmt (gimple stmt)
          val.lattice_val = CONSTANT;
          val.value = simplified;
          val.mask = 0;
+         return val;
        }
     }
   /* If the statement is likely to have a VARYING result, then do not
@@ -1970,7 +1972,7 @@ evaluate_stmt (gimple stmt)
   return val;
 }
 
-typedef hash_table<pointer_hash<gimple_statement_base> > gimple_htab;
+typedef hash_table<nofree_ptr_hash<gimple_statement_base> > gimple_htab;
 
 /* Given a BUILT_IN_STACK_SAVE value SAVED_VAL, insert a clobber of VAR before
    each matching BUILT_IN_STACK_RESTORE.  Mark visited phis in VISITED.  */
@@ -2287,7 +2289,7 @@ visit_assignment (gimple stmt, tree *output_p)
 
       /* If STMT is an assignment to an SSA_NAME, we only have one
         value to set.  */
-      if (set_lattice_value (lhs, val))
+      if (set_lattice_value (lhs, &val))
        {
          *output_p = lhs;
          if (val.lattice_val == VARYING)
@@ -2385,10 +2387,7 @@ ccp_visit_stmt (gimple stmt, edge *taken_edge_p, tree *output_p)
      SSA_NAMEs represent unknown modifications to their outputs.
      Mark them VARYING.  */
   FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
-    {
-      ccp_prop_value_t v = { VARYING, NULL_TREE, -1 };
-      set_lattice_value (def, v);
-    }
+    set_value_varying (def);
 
   return SSA_PROP_VARYING;
 }