OSDN Git Service

2010-09-24 Tobias Burnus <burnus@net-b.de>
[pf3gnuchains/gcc-fork.git] / gcc / tree-ssa-alias.c
index 6cbec5d..e00c50a 100644 (file)
@@ -1,5 +1,5 @@
 /* Alias analysis for trees.
-   Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
+   Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
    Free Software Foundation, Inc.
    Contributed by Diego Novillo <dnovillo@redhat.com>
 
@@ -24,17 +24,15 @@ along with GCC; see the file COPYING3.  If not see
 #include "coretypes.h"
 #include "tm.h"
 #include "tree.h"
-#include "rtl.h"
 #include "tm_p.h"
-#include "hard-reg-set.h"
 #include "basic-block.h"
 #include "timevar.h"
-#include "expr.h"
 #include "ggc.h"
 #include "langhooks.h"
 #include "flags.h"
+#include "toplev.h"
 #include "function.h"
-#include "diagnostic.h"
+#include "tree-pretty-print.h"
 #include "tree-dump.h"
 #include "gimple.h"
 #include "tree-flow.h"
@@ -168,14 +166,9 @@ ptr_deref_may_alias_decl_p (tree ptr, tree decl)
 {
   struct ptr_info_def *pi;
 
-  /* ???  During SCCVN/PRE we can end up with *&x during valueizing
-     operands.  Likewise we can end up with dereferencing constant
-     pointers.  Just bail out in these cases for now.  */
-  if (TREE_CODE (ptr) == ADDR_EXPR
-      || TREE_CODE (ptr) == INTEGER_CST)
-    return true;
-
-  gcc_assert (TREE_CODE (ptr) == SSA_NAME
+  gcc_assert ((TREE_CODE (ptr) == SSA_NAME
+              || TREE_CODE (ptr) == ADDR_EXPR
+              || TREE_CODE (ptr) == INTEGER_CST)
              && (TREE_CODE (decl) == VAR_DECL
                  || TREE_CODE (decl) == PARM_DECL
                  || TREE_CODE (decl) == RESULT_DECL));
@@ -184,12 +177,44 @@ ptr_deref_may_alias_decl_p (tree ptr, tree decl)
   if (!may_be_aliased (decl))
     return false;
 
+  /* ADDR_EXPR pointers either just offset another pointer or directly
+     specify the pointed-to set.  */
+  if (TREE_CODE (ptr) == ADDR_EXPR)
+    {
+      tree base = get_base_address (TREE_OPERAND (ptr, 0));
+      if (base
+         && (INDIRECT_REF_P (base)
+             || TREE_CODE (base) == MEM_REF))
+       ptr = TREE_OPERAND (base, 0);
+      else if (base
+              && SSA_VAR_P (base))
+       return base == decl;
+      else if (base
+              && CONSTANT_CLASS_P (base))
+       return false;
+      else
+       return true;
+    }
+
+  /* We can end up with dereferencing constant pointers.
+     Just bail out in this case.  */
+  if (TREE_CODE (ptr) == INTEGER_CST)
+    return true;
+
   /* If we do not have useful points-to information for this pointer
      we cannot disambiguate anything else.  */
   pi = SSA_NAME_PTR_INFO (ptr);
   if (!pi)
     return true;
 
+  /* If the decl can be used as a restrict tag and we have a restrict
+     pointer and that pointers points-to set doesn't contain this decl
+     then they can't alias.  */
+  if (DECL_RESTRICTED_P (decl)
+      && TYPE_RESTRICT (TREE_TYPE (ptr))
+      && pi->pt.vars_contains_restrict)
+    return bitmap_bit_p (pi->pt.vars, DECL_PT_UID (decl));
+
   return pt_solution_includes (&pi->pt, decl);
 }
 
@@ -202,18 +227,48 @@ ptr_derefs_may_alias_p (tree ptr1, tree ptr2)
 {
   struct ptr_info_def *pi1, *pi2;
 
-  /* ???  During SCCVN/PRE we can end up with *&x during valueizing
-     operands.  Likewise we can end up with dereferencing constant
-     pointers.  Just bail out in these cases for now.  */
-  if (TREE_CODE (ptr1) == ADDR_EXPR
-      || TREE_CODE (ptr1) == INTEGER_CST
-      || TREE_CODE (ptr2) == ADDR_EXPR
+  gcc_assert ((TREE_CODE (ptr1) == SSA_NAME
+              || TREE_CODE (ptr1) == ADDR_EXPR
+              || TREE_CODE (ptr1) == INTEGER_CST)
+             && (TREE_CODE (ptr2) == SSA_NAME
+                 || TREE_CODE (ptr2) == ADDR_EXPR
+                 || TREE_CODE (ptr2) == INTEGER_CST));
+
+  /* ADDR_EXPR pointers either just offset another pointer or directly
+     specify the pointed-to set.  */
+  if (TREE_CODE (ptr1) == ADDR_EXPR)
+    {
+      tree base = get_base_address (TREE_OPERAND (ptr1, 0));
+      if (base
+         && (INDIRECT_REF_P (base)
+             || TREE_CODE (base) == MEM_REF))
+       ptr1 = TREE_OPERAND (base, 0);
+      else if (base
+              && SSA_VAR_P (base))
+       return ptr_deref_may_alias_decl_p (ptr2, base);
+      else
+       return true;
+    }
+  if (TREE_CODE (ptr2) == ADDR_EXPR)
+    {
+      tree base = get_base_address (TREE_OPERAND (ptr2, 0));
+      if (base
+         && (INDIRECT_REF_P (base)
+             || TREE_CODE (base) == MEM_REF))
+       ptr2 = TREE_OPERAND (base, 0);
+      else if (base
+              && SSA_VAR_P (base))
+       return ptr_deref_may_alias_decl_p (ptr1, base);
+      else
+       return true;
+    }
+
+  /* We can end up with dereferencing constant pointers.
+     Just bail out in this case.  */
+  if (TREE_CODE (ptr1) == INTEGER_CST
       || TREE_CODE (ptr2) == INTEGER_CST)
     return true;
 
-  gcc_assert (TREE_CODE (ptr1) == SSA_NAME
-             && TREE_CODE (ptr2) == SSA_NAME);
-
   /* We may end up with two empty points-to solutions for two same pointers.
      In this case we still want to say both pointers alias, so shortcut
      that here.  */
@@ -227,11 +282,36 @@ ptr_derefs_may_alias_p (tree ptr1, tree ptr2)
   if (!pi1 || !pi2)
     return true;
 
+  /* If both pointers are restrict-qualified try to disambiguate
+     with restrict information.  */
+  if (TYPE_RESTRICT (TREE_TYPE (ptr1))
+      && TYPE_RESTRICT (TREE_TYPE (ptr2))
+      && !pt_solutions_same_restrict_base (&pi1->pt, &pi2->pt))
+    return false;
+
   /* ???  This does not use TBAA to prune decls from the intersection
      that not both pointers may access.  */
   return pt_solutions_intersect (&pi1->pt, &pi2->pt);
 }
 
+/* Return true if dereferencing PTR may alias *REF.
+   The caller is responsible for applying TBAA to see if PTR
+   may access *REF at all.  */
+
+static bool
+ptr_deref_may_alias_ref_p_1 (tree ptr, ao_ref *ref)
+{
+  tree base = ao_ref_base (ref);
+
+  if (INDIRECT_REF_P (base)
+      || TREE_CODE (base) == MEM_REF)
+    return ptr_derefs_may_alias_p (ptr, TREE_OPERAND (base, 0));
+  else if (SSA_VAR_P (base))
+    return ptr_deref_may_alias_decl_p (ptr, base);
+
+  return true;
+}
+
 
 /* Dump alias information on FILE.  */
 
@@ -247,7 +327,7 @@ dump_alias_info (FILE *file)
   fprintf (file, "\n\nAlias information for %s\n\n", funcname);
 
   fprintf (file, "Aliased symbols\n\n");
-  
+
   FOR_EACH_REFERENCED_VAR (var, rvi)
     {
       if (may_be_aliased (var))
@@ -258,8 +338,6 @@ dump_alias_info (FILE *file)
 
   fprintf (file, "\nESCAPED");
   dump_points_to_solution (file, &cfun->gimple_df->escaped);
-  fprintf (file, "\nCALLUSED");
-  dump_points_to_solution (file, &cfun->gimple_df->callused);
 
   fprintf (file, "\n\nFlow-insensitive points-to information\n\n");
 
@@ -267,7 +345,7 @@ dump_alias_info (FILE *file)
     {
       tree ptr = ssa_name (i);
       struct ptr_info_def *pi;
-      
+
       if (ptr == NULL_TREE
          || SSA_NAME_IN_FREE_LIST (ptr))
        continue;
@@ -283,34 +361,13 @@ dump_alias_info (FILE *file)
 
 /* Dump alias information on stderr.  */
 
-void
+DEBUG_FUNCTION void
 debug_alias_info (void)
 {
   dump_alias_info (stderr);
 }
 
 
-/* Return the alias information associated with pointer T.  It creates a
-   new instance if none existed.  */
-
-struct ptr_info_def *
-get_ptr_info (tree t)
-{
-  struct ptr_info_def *pi;
-
-  gcc_assert (POINTER_TYPE_P (TREE_TYPE (t)));
-
-  pi = SSA_NAME_PTR_INFO (t);
-  if (pi == NULL)
-    {
-      pi = GGC_CNEW (struct ptr_info_def);
-      pt_solution_reset (&pi->pt);
-      SSA_NAME_PTR_INFO (t) = pi;
-    }
-
-  return pi;
-}
-
 /* Dump the points-to set *PT into FILE.  */
 
 void
@@ -325,6 +382,9 @@ dump_points_to_solution (FILE *file, struct pt_solution *pt)
   if (pt->escaped)
     fprintf (file, ", points-to escaped");
 
+  if (pt->ipa_escaped)
+    fprintf (file, ", points-to unit escaped");
+
   if (pt->null)
     fprintf (file, ", points-to NULL");
 
@@ -334,6 +394,8 @@ dump_points_to_solution (FILE *file, struct pt_solution *pt)
       dump_decl_set (file, pt->vars);
       if (pt->vars_contains_global)
        fprintf (file, " (includes global vars)");
+      if (pt->vars_contains_restrict)
+       fprintf (file, " (includes restrict tags)");
     }
 }
 
@@ -357,7 +419,7 @@ dump_points_to_info_for (FILE *file, tree ptr)
 
 /* Dump points-to information for VAR into stderr.  */
 
-void
+DEBUG_FUNCTION void
 debug_points_to_info_for (tree var)
 {
   dump_points_to_info_for (stderr, var);
@@ -392,12 +454,18 @@ ao_ref_base (ao_ref *ref)
 
 /* Returns the base object alias set of the memory reference *REF.  */
 
-static alias_set_type ATTRIBUTE_UNUSED
+static alias_set_type
 ao_ref_base_alias_set (ao_ref *ref)
 {
+  tree base_ref;
   if (ref->base_alias_set != -1)
     return ref->base_alias_set;
-  ref->base_alias_set = get_alias_set (ao_ref_base (ref));
+  if (!ref->ref)
+    return 0;
+  base_ref = ref->ref;
+  while (handled_component_p (base_ref))
+    base_ref = TREE_OPERAND (base_ref, 0);
+  ref->base_alias_set = get_alias_set (base_ref);
   return ref->base_alias_set;
 }
 
@@ -412,6 +480,35 @@ ao_ref_alias_set (ao_ref *ref)
   return ref->ref_alias_set;
 }
 
+/* Init an alias-oracle reference representation from a gimple pointer
+   PTR and a gimple size SIZE in bytes.  If SIZE is NULL_TREE the the
+   size is assumed to be unknown.  The access is assumed to be only
+   to or after of the pointer target, not before it.  */
+
+void
+ao_ref_init_from_ptr_and_size (ao_ref *ref, tree ptr, tree size)
+{
+  HOST_WIDE_INT t1, t2;
+  ref->ref = NULL_TREE;
+  if (TREE_CODE (ptr) == ADDR_EXPR)
+    ref->base = get_ref_base_and_extent (TREE_OPERAND (ptr, 0),
+                                        &ref->offset, &t1, &t2);
+  else
+    {
+      ref->base = build2 (MEM_REF, char_type_node,
+                         ptr, null_pointer_node);
+      ref->offset = 0;
+    }
+  if (size
+      && host_integerp (size, 0)
+      && TREE_INT_CST_LOW (size) * 8 / 8 == TREE_INT_CST_LOW (size))
+    ref->max_size = ref->size = TREE_INT_CST_LOW (size) * 8;
+  else
+    ref->max_size = ref->size = -1;
+  ref->ref_alias_set = 0;
+  ref->base_alias_set = 0;
+}
+
 /* Return 1 if TYPE1 and TYPE2 are to be considered equivalent for the
    purpose of TBAA.  Return 0 if they are distinct and -1 if we cannot
    decide.  */
@@ -431,26 +528,45 @@ same_type_for_tbaa (tree type1, tree type2)
   if (TYPE_CANONICAL (type1) == TYPE_CANONICAL (type2))
     return 1;
 
-  /* ???  Array types are not properly unified in all cases as we have
+  /* ??? Array types are not properly unified in all cases as we have
      spurious changes in the index types for example.  Removing this
      causes all sorts of problems with the Fortran frontend.  */
   if (TREE_CODE (type1) == ARRAY_TYPE
       && TREE_CODE (type2) == ARRAY_TYPE)
     return -1;
 
+  /* ??? In Ada, an lvalue of an unconstrained type can be used to access an
+     object of one of its constrained subtypes, e.g. when a function with an
+     unconstrained parameter passed by reference is called on an object and
+     inlined.  But, even in the case of a fixed size, type and subtypes are
+     not equivalent enough as to share the same TYPE_CANONICAL, since this
+     would mean that conversions between them are useless, whereas they are
+     not (e.g. type and subtypes can have different modes).  So, in the end,
+     they are only guaranteed to have the same alias set.  */
+  if (get_alias_set (type1) == get_alias_set (type2))
+    return -1;
+
   /* The types are known to be not equal.  */
   return 0;
 }
 
 /* Determine if the two component references REF1 and REF2 which are
    based on access types TYPE1 and TYPE2 and of which at least one is based
-   on an indirect reference may alias.  */
+   on an indirect reference may alias.  REF2 is the only one that can
+   be a decl in which case REF2_IS_DECL is true.
+   REF1_ALIAS_SET, BASE1_ALIAS_SET, REF2_ALIAS_SET and BASE2_ALIAS_SET
+   are the respective alias sets.  */
 
 static bool
-nonaliasing_component_refs_p (tree ref1, tree type1,
-                             HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
-                             tree ref2, tree type2,
-                             HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2)
+aliasing_component_refs_p (tree ref1, tree type1,
+                          alias_set_type ref1_alias_set,
+                          alias_set_type base1_alias_set,
+                          HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
+                          tree ref2, tree type2,
+                          alias_set_type ref2_alias_set,
+                          alias_set_type base2_alias_set,
+                          HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
+                          bool ref2_is_decl)
 {
   /* If one reference is a component references through pointers try to find a
      common base and apply offset based disambiguation.  This handles
@@ -494,14 +610,26 @@ nonaliasing_component_refs_p (tree ref1, tree type1,
       offset1 -= offadj;
       return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
     }
+
   /* If we have two type access paths B1.path1 and B2.path2 they may
-     only alias if either B1 is in B2.path2 or B2 is in B1.path1.  */
+     only alias if either B1 is in B2.path2 or B2 is in B1.path1.
+     But we can still have a path that goes B1.path1...B2.path2 with
+     a part that we do not see.  So we can only disambiguate now
+     if there is no B2 in the tail of path1 and no B1 on the
+     tail of path2.  */
+  if (base1_alias_set == ref2_alias_set
+      || alias_set_subset_of (base1_alias_set, ref2_alias_set))
+    return true;
+  /* If this is ptr vs. decl then we know there is no ptr ... decl path.  */
+  if (!ref2_is_decl)
+    return (base2_alias_set == ref1_alias_set
+           || alias_set_subset_of (base2_alias_set, ref1_alias_set));
   return false;
 }
 
 /* Return true if two memory references based on the variables BASE1
-   and BASE2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1[ and
-   [OFFSET2, OFFSET2 + MAX_SIZE2[ may alias.  */
+   and BASE2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
+   [OFFSET2, OFFSET2 + MAX_SIZE2) may alias.  */
 
 static bool
 decl_refs_may_alias_p (tree base1,
@@ -512,7 +640,7 @@ decl_refs_may_alias_p (tree base1,
   gcc_assert (SSA_VAR_P (base1) && SSA_VAR_P (base2));
 
   /* If both references are based on different variables, they cannot alias.  */
-  if (!operand_equal_p (base1, base2, 0))
+  if (base1 != base2)
     return false;
 
   /* If both references are based on the same variable, they cannot alias if
@@ -521,38 +649,71 @@ decl_refs_may_alias_p (tree base1,
 }
 
 /* Return true if an indirect reference based on *PTR1 constrained
-   to [OFFSET1, OFFSET1 + MAX_SIZE1[ may alias a variable based on BASE2
-   constrained to [OFFSET2, OFFSET2 + MAX_SIZE2[.  *PTR1 and BASE2 have
+   to [OFFSET1, OFFSET1 + MAX_SIZE1) may alias a variable based on BASE2
+   constrained to [OFFSET2, OFFSET2 + MAX_SIZE2).  *PTR1 and BASE2 have
    the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
    in which case they are computed on-demand.  REF1 and REF2
    if non-NULL are the complete memory reference trees.  */
 
 static bool
-indirect_ref_may_alias_decl_p (tree ref1, tree ptr1,
-                              HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
+indirect_ref_may_alias_decl_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
+                              HOST_WIDE_INT offset1,
+                              HOST_WIDE_INT max_size1 ATTRIBUTE_UNUSED,
+                              alias_set_type ref1_alias_set,
                               alias_set_type base1_alias_set,
-                              tree ref2, tree base2,
+                              tree ref2 ATTRIBUTE_UNUSED, tree base2,
                               HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
-                              alias_set_type base2_alias_set)
+                              alias_set_type ref2_alias_set,
+                              alias_set_type base2_alias_set, bool tbaa_p)
 {
+  tree ptr1;
+  tree ptrtype1;
+  HOST_WIDE_INT offset1p = offset1, offset2p = offset2;
+
+  ptr1 = TREE_OPERAND (base1, 0);
+
+  /* The offset embedded in MEM_REFs can be negative.  Bias them
+     so that the resulting offset adjustment is positive.  */
+  if (TREE_CODE (base1) == MEM_REF
+      || TREE_CODE (base1) == TARGET_MEM_REF)
+    {
+      double_int moff = mem_ref_offset (base1);
+      moff = double_int_lshift (moff,
+                               BITS_PER_UNIT == 8
+                               ? 3 : exact_log2 (BITS_PER_UNIT),
+                               HOST_BITS_PER_DOUBLE_INT, true);
+      if (double_int_negative_p (moff))
+       offset2p += double_int_neg (moff).low;
+      else
+       offset1p += moff.low;
+    }
+
   /* If only one reference is based on a variable, they cannot alias if
      the pointer access is beyond the extent of the variable access.
      (the pointer base cannot validly point to an offset less than zero
      of the variable).
      They also cannot alias if the pointer may not point to the decl.  */
-  if (max_size2 != -1
-      && !ranges_overlap_p (offset1, max_size1, 0, offset2 + max_size2))
+  if ((TREE_CODE (base1) != TARGET_MEM_REF
+       || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
+      && !ranges_overlap_p (MAX (0, offset1p), -1, offset2p, max_size2))
     return false;
   if (!ptr_deref_may_alias_decl_p (ptr1, base2))
     return false;
 
   /* Disambiguations that rely on strict aliasing rules follow.  */
-  if (!flag_strict_aliasing)
+  if (!flag_strict_aliasing || !tbaa_p)
     return true;
 
+  if (TREE_CODE (base1) == MEM_REF)
+    ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
+  else if (TREE_CODE (base1) == TARGET_MEM_REF)
+    ptrtype1 = TREE_TYPE (TMR_OFFSET (base1));
+  else
+    ptrtype1 = TREE_TYPE (ptr1);
+
   /* If the alias set for a pointer access is zero all bets are off.  */
   if (base1_alias_set == -1)
-    base1_alias_set = get_deref_alias_set (ptr1);
+    base1_alias_set = get_deref_alias_set (ptrtype1);
   if (base1_alias_set == 0)
     return true;
   if (base2_alias_set == -1)
@@ -560,71 +721,183 @@ indirect_ref_may_alias_decl_p (tree ref1, tree ptr1,
 
   /* If both references are through the same type, they do not alias
      if the accesses do not overlap.  This does extra disambiguation
-     for mixed/pointer accesses but requires strict aliasing.  */
-  if (same_type_for_tbaa (TREE_TYPE (TREE_TYPE (ptr1)),
-                         TREE_TYPE (base2)) == 1)
+     for mixed/pointer accesses but requires strict aliasing.
+     For MEM_REFs we require that the component-ref offset we computed
+     is relative to the start of the type which we ensure by
+     comparing rvalue and access type and disregarding the constant
+     pointer offset.  */
+  if ((TREE_CODE (base1) != TARGET_MEM_REF
+       || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
+      && (TREE_CODE (base1) != MEM_REF
+         || same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1)
+      && same_type_for_tbaa (TREE_TYPE (ptrtype1), TREE_TYPE (base2)) == 1)
     return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
 
-  /* The only way to access a variable is through a pointer dereference
-     of the same alias set or a subset of it.  */
+  /* When we are trying to disambiguate an access with a pointer dereference
+     as base versus one with a decl as base we can use both the size
+     of the decl and its dynamic type for extra disambiguation.
+     ???  We do not know anything about the dynamic type of the decl
+     other than that its alias-set contains base2_alias_set as a subset
+     which does not help us here.  */
+  /* As we know nothing useful about the dynamic type of the decl just
+     use the usual conflict check rather than a subset test.
+     ???  We could introduce -fvery-strict-aliasing when the language
+     does not allow decls to have a dynamic type that differs from their
+     static type.  Then we can check 
+     !alias_set_subset_of (base1_alias_set, base2_alias_set) instead.  */
   if (base1_alias_set != base2_alias_set
-      && !alias_set_subset_of (base1_alias_set, base2_alias_set))
+      && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
+    return false;
+  /* If the size of the access relevant for TBAA through the pointer
+     is bigger than the size of the decl we can't possibly access the
+     decl via that pointer.  */
+  if (DECL_SIZE (base2) && COMPLETE_TYPE_P (TREE_TYPE (ptrtype1))
+      && TREE_CODE (DECL_SIZE (base2)) == INTEGER_CST
+      && TREE_CODE (TYPE_SIZE (TREE_TYPE (ptrtype1))) == INTEGER_CST
+      /* ???  This in turn may run afoul when a decl of type T which is
+        a member of union type U is accessed through a pointer to
+        type U and sizeof T is smaller than sizeof U.  */
+      && TREE_CODE (TREE_TYPE (ptrtype1)) != UNION_TYPE
+      && TREE_CODE (TREE_TYPE (ptrtype1)) != QUAL_UNION_TYPE
+      && tree_int_cst_lt (DECL_SIZE (base2), TYPE_SIZE (TREE_TYPE (ptrtype1))))
     return false;
 
   /* Do access-path based disambiguation.  */
   if (ref1 && ref2
       && handled_component_p (ref1)
-      && handled_component_p (ref2))
-    return nonaliasing_component_refs_p (ref1, TREE_TYPE (TREE_TYPE (ptr1)),
-                                        offset1, max_size1,
-                                        ref2, TREE_TYPE (base2),
-                                        offset2, max_size2);
+      && handled_component_p (ref2)
+      && TREE_CODE (base1) != TARGET_MEM_REF
+      && (TREE_CODE (base1) != MEM_REF
+         || same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1))
+    return aliasing_component_refs_p (ref1, TREE_TYPE (ptrtype1),
+                                     ref1_alias_set, base1_alias_set,
+                                     offset1, max_size1,
+                                     ref2, TREE_TYPE (base2),
+                                     ref2_alias_set, base2_alias_set,
+                                     offset2, max_size2, true);
 
   return true;
 }
 
 /* Return true if two indirect references based on *PTR1
-   and *PTR2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1[ and
-   [OFFSET2, OFFSET2 + MAX_SIZE2[ may alias.  *PTR1 and *PTR2 have
+   and *PTR2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
+   [OFFSET2, OFFSET2 + MAX_SIZE2) may alias.  *PTR1 and *PTR2 have
    the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
    in which case they are computed on-demand.  REF1 and REF2
    if non-NULL are the complete memory reference trees. */
 
 static bool
-indirect_refs_may_alias_p (tree ref1, tree ptr1,
+indirect_refs_may_alias_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
                           HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
+                          alias_set_type ref1_alias_set,
                           alias_set_type base1_alias_set,
-                          tree ref2, tree ptr2,
+                          tree ref2 ATTRIBUTE_UNUSED, tree base2,
                           HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
-                          alias_set_type base2_alias_set)
+                          alias_set_type ref2_alias_set,
+                          alias_set_type base2_alias_set, bool tbaa_p)
 {
+  tree ptr1;
+  tree ptr2;
+  tree ptrtype1, ptrtype2;
+
+  ptr1 = TREE_OPERAND (base1, 0);
+  ptr2 = TREE_OPERAND (base2, 0);
+
   /* If both bases are based on pointers they cannot alias if they may not
      point to the same memory object or if they point to the same object
      and the accesses do not overlap.  */
-  if (operand_equal_p (ptr1, ptr2, 0))
-    return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
+  if ((!cfun || gimple_in_ssa_p (cfun))
+      && operand_equal_p (ptr1, ptr2, 0)
+      && (((TREE_CODE (base1) != TARGET_MEM_REF
+           || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
+          && (TREE_CODE (base2) != TARGET_MEM_REF
+              || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2))))
+         || (TREE_CODE (base1) == TARGET_MEM_REF
+             && TREE_CODE (base2) == TARGET_MEM_REF
+             && (TMR_STEP (base1) == TMR_STEP (base2)
+                 || (TMR_STEP (base1) && TMR_STEP (base2)
+                     && operand_equal_p (TMR_STEP (base1),
+                                         TMR_STEP (base2), 0)))
+             && (TMR_INDEX (base1) == TMR_INDEX (base2)
+                 || (TMR_INDEX (base1) && TMR_INDEX (base2)
+                     && operand_equal_p (TMR_INDEX (base1),
+                                         TMR_INDEX (base2), 0)))
+             && (TMR_INDEX2 (base1) == TMR_INDEX2 (base2)
+                 || (TMR_INDEX2 (base1) && TMR_INDEX2 (base2)
+                     && operand_equal_p (TMR_INDEX2 (base1),
+                                         TMR_INDEX2 (base2), 0))))))
+    {
+      /* The offset embedded in MEM_REFs can be negative.  Bias them
+        so that the resulting offset adjustment is positive.  */
+      if (TREE_CODE (base1) == MEM_REF
+         || TREE_CODE (base1) == TARGET_MEM_REF)
+       {
+         double_int moff = mem_ref_offset (base1);
+         moff = double_int_lshift (moff,
+                                   BITS_PER_UNIT == 8
+                                   ? 3 : exact_log2 (BITS_PER_UNIT),
+                                   HOST_BITS_PER_DOUBLE_INT, true);
+         if (double_int_negative_p (moff))
+           offset2 += double_int_neg (moff).low;
+         else
+           offset1 += moff.low;
+       }
+      if (TREE_CODE (base2) == MEM_REF
+         || TREE_CODE (base2) == TARGET_MEM_REF)
+       {
+         double_int moff = mem_ref_offset (base2);
+         moff = double_int_lshift (moff,
+                                   BITS_PER_UNIT == 8
+                                   ? 3 : exact_log2 (BITS_PER_UNIT),
+                                   HOST_BITS_PER_DOUBLE_INT, true);
+         if (double_int_negative_p (moff))
+           offset1 += double_int_neg (moff).low;
+         else
+           offset2 += moff.low;
+       }
+      return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
+    }
   if (!ptr_derefs_may_alias_p (ptr1, ptr2))
     return false;
 
   /* Disambiguations that rely on strict aliasing rules follow.  */
-  if (!flag_strict_aliasing)
+  if (!flag_strict_aliasing || !tbaa_p)
     return true;
 
+  if (TREE_CODE (base1) == MEM_REF)
+    ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
+  else if (TREE_CODE (base1) == TARGET_MEM_REF)
+    ptrtype1 = TREE_TYPE (TMR_OFFSET (base1));
+  else
+    ptrtype1 = TREE_TYPE (ptr1);
+  if (TREE_CODE (base2) == MEM_REF)
+    ptrtype2 = TREE_TYPE (TREE_OPERAND (base2, 1));
+  else if (TREE_CODE (base2) == TARGET_MEM_REF)
+    ptrtype2 = TREE_TYPE (TMR_OFFSET (base2));
+  else
+    ptrtype2 = TREE_TYPE (ptr2);
+
   /* If the alias set for a pointer access is zero all bets are off.  */
   if (base1_alias_set == -1)
-    base1_alias_set = get_deref_alias_set (ptr1);
+    base1_alias_set = get_deref_alias_set (ptrtype1);
   if (base1_alias_set == 0)
     return true;
   if (base2_alias_set == -1)
-    base2_alias_set = get_deref_alias_set (ptr2);
+    base2_alias_set = get_deref_alias_set (ptrtype2);
   if (base2_alias_set == 0)
     return true;
 
   /* If both references are through the same type, they do not alias
      if the accesses do not overlap.  This does extra disambiguation
      for mixed/pointer accesses but requires strict aliasing.  */
-  if (same_type_for_tbaa (TREE_TYPE (TREE_TYPE (ptr1)),
-                         TREE_TYPE (TREE_TYPE (ptr2))) == 1)
+  if ((TREE_CODE (base1) != TARGET_MEM_REF || !TMR_INDEX (base1))
+      && (TREE_CODE (base2) != TARGET_MEM_REF || !TMR_INDEX (base2))
+      && (TREE_CODE (base1) != MEM_REF
+         || same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1)
+      && (TREE_CODE (base2) != MEM_REF
+         || same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) == 1)
+      && same_type_for_tbaa (TREE_TYPE (ptrtype1),
+                            TREE_TYPE (ptrtype2)) == 1)
     return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
 
   /* Do type-based disambiguation.  */
@@ -635,46 +908,54 @@ indirect_refs_may_alias_p (tree ref1, tree ptr1,
   /* Do access-path based disambiguation.  */
   if (ref1 && ref2
       && handled_component_p (ref1)
-      && handled_component_p (ref2))
-    return nonaliasing_component_refs_p (ref1, TREE_TYPE (TREE_TYPE (ptr1)),
-                                        offset1, max_size1,
-                                        ref2, TREE_TYPE (TREE_TYPE (ptr2)),
-                                        offset2, max_size2);
+      && handled_component_p (ref2)
+      && TREE_CODE (base1) != TARGET_MEM_REF
+      && TREE_CODE (base2) != TARGET_MEM_REF
+      && (TREE_CODE (base1) != MEM_REF
+         || same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1)
+      && (TREE_CODE (base2) != MEM_REF
+         || same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) == 1))
+    return aliasing_component_refs_p (ref1, TREE_TYPE (ptrtype1),
+                                     ref1_alias_set, base1_alias_set,
+                                     offset1, max_size1,
+                                     ref2, TREE_TYPE (ptrtype2),
+                                     ref2_alias_set, base2_alias_set,
+                                     offset2, max_size2, false);
 
   return true;
 }
 
 /* Return true, if the two memory references REF1 and REF2 may alias.  */
 
-static bool
+bool
 refs_may_alias_p_1 (ao_ref *ref1, ao_ref *ref2, bool tbaa_p)
 {
   tree base1, base2;
   HOST_WIDE_INT offset1 = 0, offset2 = 0;
-  HOST_WIDE_INT size1 = -1, size2 = -1;
   HOST_WIDE_INT max_size1 = -1, max_size2 = -1;
   bool var1_p, var2_p, ind1_p, ind2_p;
-  alias_set_type set;
-
-  gcc_assert ((!ref1->ref
-              || SSA_VAR_P (ref1->ref)
-              || handled_component_p (ref1->ref)
-              || INDIRECT_REF_P (ref1->ref)
-              || TREE_CODE (ref1->ref) == TARGET_MEM_REF)
-             && (!ref2->ref
-                 || SSA_VAR_P (ref2->ref)
-                 || handled_component_p (ref2->ref)
-                 || INDIRECT_REF_P (ref2->ref)
-                 || TREE_CODE (ref2->ref) == TARGET_MEM_REF));
+
+  gcc_checking_assert ((!ref1->ref
+                       || TREE_CODE (ref1->ref) == SSA_NAME
+                       || DECL_P (ref1->ref)
+                       || handled_component_p (ref1->ref)
+                       || INDIRECT_REF_P (ref1->ref)
+                       || TREE_CODE (ref1->ref) == MEM_REF
+                       || TREE_CODE (ref1->ref) == TARGET_MEM_REF)
+                      && (!ref2->ref
+                          || TREE_CODE (ref2->ref) == SSA_NAME
+                          || DECL_P (ref2->ref)
+                          || handled_component_p (ref2->ref)
+                          || INDIRECT_REF_P (ref2->ref)
+                          || TREE_CODE (ref2->ref) == MEM_REF
+                          || TREE_CODE (ref2->ref) == TARGET_MEM_REF));
 
   /* Decompose the references into their base objects and the access.  */
   base1 = ao_ref_base (ref1);
   offset1 = ref1->offset;
-  size1 = ref1->size;
   max_size1 = ref1->max_size;
   base2 = ao_ref_base (ref2);
   offset2 = ref2->offset;
-  size2 = ref2->size;
   max_size2 = ref2->max_size;
 
   /* We can end up with registers or constants as bases for example from
@@ -682,10 +963,21 @@ refs_may_alias_p_1 (ao_ref *ref1, ao_ref *ref2, bool tbaa_p)
      which is seen as a struct copy.  */
   if (TREE_CODE (base1) == SSA_NAME
       || TREE_CODE (base2) == SSA_NAME
+      || TREE_CODE (base1) == CONST_DECL
+      || TREE_CODE (base2) == CONST_DECL
       || is_gimple_min_invariant (base1)
       || is_gimple_min_invariant (base2))
     return false;
 
+  /* We can end up refering to code via function and label decls.
+     As we likely do not properly track code aliases conservatively
+     bail out.  */
+  if (TREE_CODE (base1) == FUNCTION_DECL
+      || TREE_CODE (base2) == FUNCTION_DECL
+      || TREE_CODE (base1) == LABEL_DECL
+      || TREE_CODE (base2) == LABEL_DECL)
+    return true;
+
   /* Defer to simple offset based disambiguation if we have
      references based on two decls.  Do this before defering to
      TBAA to handle must-alias cases in conformance with the
@@ -696,6 +988,29 @@ refs_may_alias_p_1 (ao_ref *ref1, ao_ref *ref2, bool tbaa_p)
     return decl_refs_may_alias_p (base1, offset1, max_size1,
                                  base2, offset2, max_size2);
 
+  ind1_p = (INDIRECT_REF_P (base1)
+           || (TREE_CODE (base1) == MEM_REF)
+           || (TREE_CODE (base1) == TARGET_MEM_REF));
+  ind2_p = (INDIRECT_REF_P (base2)
+           || (TREE_CODE (base2) == MEM_REF)
+           || (TREE_CODE (base2) == TARGET_MEM_REF));
+
+  /* Canonicalize the pointer-vs-decl case.  */
+  if (ind1_p && var2_p)
+    {
+      HOST_WIDE_INT tmp1;
+      tree tmp2;
+      ao_ref *tmp3;
+      tmp1 = offset1; offset1 = offset2; offset2 = tmp1;
+      tmp1 = max_size1; max_size1 = max_size2; max_size2 = tmp1;
+      tmp2 = base1; base1 = base2; base2 = tmp2;
+      tmp3 = ref1; ref1 = ref2; ref2 = tmp3;
+      var1_p = true;
+      ind1_p = false;
+      var2_p = false;
+      ind2_p = true;
+    }
+
   /* First defer to TBAA if possible.  */
   if (tbaa_p
       && flag_strict_aliasing
@@ -703,32 +1018,24 @@ refs_may_alias_p_1 (ao_ref *ref1, ao_ref *ref2, bool tbaa_p)
                                 ao_ref_alias_set (ref2)))
     return false;
 
-  /* If one reference is a TARGET_MEM_REF weird things are allowed.  Still
-     TBAA disambiguation based on the access type is possible, so bail
-     out only after that check.  */
-  if ((ref1->ref && TREE_CODE (ref1->ref) == TARGET_MEM_REF)
-      || (ref2->ref && TREE_CODE (ref2->ref) == TARGET_MEM_REF))
-    return true;
-
   /* Dispatch to the pointer-vs-decl or pointer-vs-pointer disambiguators.  */
-  ind1_p = INDIRECT_REF_P (base1);
-  ind2_p = INDIRECT_REF_P (base2);
-  set = tbaa_p ? -1 : 0;
   if (var1_p && ind2_p)
-    return indirect_ref_may_alias_decl_p (ref2->ref, TREE_OPERAND (base2, 0),
-                                         offset2, max_size2, set,
+    return indirect_ref_may_alias_decl_p (ref2->ref, base2,
+                                         offset2, max_size2,
+                                         ao_ref_alias_set (ref2), -1,
                                          ref1->ref, base1,
-                                         offset1, max_size1, set);
-  else if (ind1_p && var2_p)
-    return indirect_ref_may_alias_decl_p (ref1->ref, TREE_OPERAND (base1, 0),
-                                         offset1, max_size1, set,
-                                         ref2->ref, base2,
-                                         offset2, max_size2, set);
+                                         offset1, max_size1,
+                                         ao_ref_alias_set (ref1),
+                                         ao_ref_base_alias_set (ref1),
+                                         tbaa_p);
   else if (ind1_p && ind2_p)
-    return indirect_refs_may_alias_p (ref1->ref, TREE_OPERAND (base1, 0),
-                                     offset1, max_size1, set,
-                                     ref2->ref, TREE_OPERAND (base2, 0),
-                                     offset2, max_size2, set);
+    return indirect_refs_may_alias_p (ref1->ref, base1,
+                                     offset1, max_size1,
+                                     ao_ref_alias_set (ref1), -1,
+                                     ref2->ref, base2,
+                                     offset2, max_size2,
+                                     ao_ref_alias_set (ref2), -1,
+                                     tbaa_p);
 
   gcc_unreachable ();
 }
@@ -776,9 +1083,9 @@ refs_output_dependent_p (tree store1, tree store2)
    otherwise return false.  */
 
 static bool
-ref_maybe_used_by_call_p_1 (gimple call, tree ref)
+ref_maybe_used_by_call_p_1 (gimple call, ao_ref *ref)
 {
-  tree base;
+  tree base, callee;
   unsigned i;
   int flags = gimple_call_flags (call);
 
@@ -787,12 +1094,8 @@ ref_maybe_used_by_call_p_1 (gimple call, tree ref)
       && (flags & (ECF_CONST|ECF_NOVOPS)))
     goto process_args;
 
-  /* If the reference is not based on a decl give up.
-     ???  Handle indirect references by intersecting the call-used
-         solution with that of the pointer.  */
-  base = get_base_address (ref);
-  if (!base
-      || !DECL_P (base))
+  base = ao_ref_base (ref);
+  if (!base)
     return true;
 
   /* If the reference is based on a decl that is not aliased the call
@@ -803,13 +1106,79 @@ ref_maybe_used_by_call_p_1 (gimple call, tree ref)
       && !is_global_var (base))
     goto process_args;
 
+  callee = gimple_call_fndecl (call);
+
+  /* Handle those builtin functions explicitly that do not act as
+     escape points.  See tree-ssa-structalias.c:find_func_aliases
+     for the list of builtins we might need to handle here.  */
+  if (callee != NULL_TREE
+      && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
+    switch (DECL_FUNCTION_CODE (callee))
+      {
+       /* All the following functions clobber memory pointed to by
+          their first argument.  */
+       case BUILT_IN_STRCPY:
+       case BUILT_IN_STRNCPY:
+       case BUILT_IN_MEMCPY:
+       case BUILT_IN_MEMMOVE:
+       case BUILT_IN_MEMPCPY:
+       case BUILT_IN_STPCPY:
+       case BUILT_IN_STPNCPY:
+       case BUILT_IN_STRCAT:
+       case BUILT_IN_STRNCAT:
+         {
+           ao_ref dref;
+           tree size = NULL_TREE;
+           if (gimple_call_num_args (call) == 3)
+             size = gimple_call_arg (call, 2);
+           ao_ref_init_from_ptr_and_size (&dref,
+                                          gimple_call_arg (call, 1),
+                                          size);
+           return refs_may_alias_p_1 (&dref, ref, false);
+         }
+       case BUILT_IN_BCOPY:
+         {
+           ao_ref dref;
+           tree size = gimple_call_arg (call, 2);
+           ao_ref_init_from_ptr_and_size (&dref,
+                                          gimple_call_arg (call, 0),
+                                          size);
+           return refs_may_alias_p_1 (&dref, ref, false);
+         }
+       /* The following builtins do not read from memory.  */
+       case BUILT_IN_FREE:
+       case BUILT_IN_MALLOC:
+       case BUILT_IN_CALLOC:
+       case BUILT_IN_MEMSET:
+       case BUILT_IN_FREXP:
+       case BUILT_IN_FREXPF:
+       case BUILT_IN_FREXPL:
+       case BUILT_IN_GAMMA_R:
+       case BUILT_IN_GAMMAF_R:
+       case BUILT_IN_GAMMAL_R:
+       case BUILT_IN_LGAMMA_R:
+       case BUILT_IN_LGAMMAF_R:
+       case BUILT_IN_LGAMMAL_R:
+       case BUILT_IN_MODF:
+       case BUILT_IN_MODFF:
+       case BUILT_IN_MODFL:
+       case BUILT_IN_REMQUO:
+       case BUILT_IN_REMQUOF:
+       case BUILT_IN_REMQUOL:
+       case BUILT_IN_SINCOS:
+       case BUILT_IN_SINCOSF:
+       case BUILT_IN_SINCOSL:
+         return false;
+
+       default:
+         /* Fallthru to general call handling.  */;
+      }
+
   /* Check if base is a global static variable that is not read
      by the function.  */
   if (TREE_CODE (base) == VAR_DECL
-      && TREE_STATIC (base)
-      && !TREE_PUBLIC (base))
+      && TREE_STATIC (base))
     {
-      tree callee = gimple_call_fndecl (call);
       bitmap not_read;
 
       if (callee != NULL_TREE
@@ -819,36 +1188,47 @@ ref_maybe_used_by_call_p_1 (gimple call, tree ref)
        goto process_args;
     }
 
-  /* If the base variable is call-used or call-clobbered then
-     it may be used.  */
-  if (flags & (ECF_PURE|ECF_CONST|ECF_LOOPING_CONST_OR_PURE|ECF_NOVOPS))
+  /* Check if the base variable is call-used.  */
+  if (DECL_P (base))
     {
-      if (is_call_used (base))
+      if (pt_solution_includes (gimple_call_use_set (call), base))
        return true;
     }
-  else
+  else if ((INDIRECT_REF_P (base)
+           || TREE_CODE (base) == MEM_REF)
+          && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
     {
-      if (is_call_clobbered (base))
+      struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
+      if (!pi)
+       return true;
+
+      if (pt_solutions_intersect (gimple_call_use_set (call), &pi->pt))
        return true;
     }
+  else
+    return true;
 
   /* Inspect call arguments for passed-by-value aliases.  */
 process_args:
   for (i = 0; i < gimple_call_num_args (call); ++i)
     {
       tree op = gimple_call_arg (call, i);
+      int flags = gimple_call_arg_flags (call, i);
 
-      if (TREE_CODE (op) == EXC_PTR_EXPR
-         || TREE_CODE (op) == FILTER_EXPR)
+      if (flags & EAF_UNUSED)
        continue;
 
       if (TREE_CODE (op) == WITH_SIZE_EXPR)
        op = TREE_OPERAND (op, 0);
 
       if (TREE_CODE (op) != SSA_NAME
-         && !is_gimple_min_invariant (op)
-         && refs_may_alias_p (op, ref))
-       return true;
+         && !is_gimple_min_invariant (op))
+       {
+         ao_ref r;
+         ao_ref_init (&r, op);
+         if (refs_may_alias_p_1 (&r, ref, true))
+           return true;
+       }
     }
 
   return false;
@@ -857,7 +1237,10 @@ process_args:
 static bool
 ref_maybe_used_by_call_p (gimple call, tree ref)
 {
-  bool res = ref_maybe_used_by_call_p_1 (call, ref);
+  ao_ref r;
+  bool res;
+  ao_ref_init (&r, ref);
+  res = ref_maybe_used_by_call_p_1 (call, &r);
   if (res)
     ++alias_stats.ref_maybe_used_by_call_p_may_alias;
   else
@@ -901,6 +1284,7 @@ static bool
 call_may_clobber_ref_p_1 (gimple call, ao_ref *ref)
 {
   tree base;
+  tree callee;
 
   /* If the call is pure or const it cannot clobber anything.  */
   if (gimple_call_flags (call)
@@ -926,29 +1310,160 @@ call_may_clobber_ref_p_1 (gimple call, ao_ref *ref)
          || !is_global_var (base)))
     return false;
 
+  callee = gimple_call_fndecl (call);
+
+  /* Handle those builtin functions explicitly that do not act as
+     escape points.  See tree-ssa-structalias.c:find_func_aliases
+     for the list of builtins we might need to handle here.  */
+  if (callee != NULL_TREE
+      && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
+    switch (DECL_FUNCTION_CODE (callee))
+      {
+       /* All the following functions clobber memory pointed to by
+          their first argument.  */
+       case BUILT_IN_STRCPY:
+       case BUILT_IN_STRNCPY:
+       case BUILT_IN_MEMCPY:
+       case BUILT_IN_MEMMOVE:
+       case BUILT_IN_MEMPCPY:
+       case BUILT_IN_STPCPY:
+       case BUILT_IN_STPNCPY:
+       case BUILT_IN_STRCAT:
+       case BUILT_IN_STRNCAT:
+       case BUILT_IN_MEMSET:
+         {
+           ao_ref dref;
+           tree size = NULL_TREE;
+           if (gimple_call_num_args (call) == 3)
+             size = gimple_call_arg (call, 2);
+           ao_ref_init_from_ptr_and_size (&dref,
+                                          gimple_call_arg (call, 0),
+                                          size);
+           return refs_may_alias_p_1 (&dref, ref, false);
+         }
+       case BUILT_IN_BCOPY:
+         {
+           ao_ref dref;
+           tree size = gimple_call_arg (call, 2);
+           ao_ref_init_from_ptr_and_size (&dref,
+                                          gimple_call_arg (call, 1),
+                                          size);
+           return refs_may_alias_p_1 (&dref, ref, false);
+         }
+       /* Allocating memory does not have any side-effects apart from
+          being the definition point for the pointer.  */
+       case BUILT_IN_MALLOC:
+       case BUILT_IN_CALLOC:
+         /* Unix98 specifies that errno is set on allocation failure.
+            Until we properly can track the errno location assume it
+            is not a local decl but external or anonymous storage in
+            a different translation unit.  Also assume it is of
+            type int as required by the standard.  */
+         if (flag_errno_math
+             && TREE_TYPE (base) == integer_type_node)
+           {
+             struct ptr_info_def *pi;
+             if (DECL_P (base)
+                 && !TREE_STATIC (base))
+               return true;
+             else if ((INDIRECT_REF_P (base)
+                       || TREE_CODE (base) == MEM_REF)
+                      && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME
+                      && (pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0))))
+               return pi->pt.anything || pi->pt.nonlocal;
+           }
+         return false;
+       /* Freeing memory kills the pointed-to memory.  More importantly
+          the call has to serve as a barrier for moving loads and stores
+          across it.  */
+       case BUILT_IN_FREE:
+         {
+           tree ptr = gimple_call_arg (call, 0);
+           return ptr_deref_may_alias_ref_p_1 (ptr, ref);
+         }
+       case BUILT_IN_GAMMA_R:
+       case BUILT_IN_GAMMAF_R:
+       case BUILT_IN_GAMMAL_R:
+       case BUILT_IN_LGAMMA_R:
+       case BUILT_IN_LGAMMAF_R:
+       case BUILT_IN_LGAMMAL_R:
+         {
+           tree out = gimple_call_arg (call, 1);
+           if (ptr_deref_may_alias_ref_p_1 (out, ref))
+             return true;
+           if (flag_errno_math)
+             break;
+           return false;
+         }
+       case BUILT_IN_FREXP:
+       case BUILT_IN_FREXPF:
+       case BUILT_IN_FREXPL:
+       case BUILT_IN_MODF:
+       case BUILT_IN_MODFF:
+       case BUILT_IN_MODFL:
+         {
+           tree out = gimple_call_arg (call, 1);
+           return ptr_deref_may_alias_ref_p_1 (out, ref);
+         }
+       case BUILT_IN_REMQUO:
+       case BUILT_IN_REMQUOF:
+       case BUILT_IN_REMQUOL:
+         {
+           tree out = gimple_call_arg (call, 2);
+           if (ptr_deref_may_alias_ref_p_1 (out, ref))
+             return true;
+           if (flag_errno_math)
+             break;
+           return false;
+         }
+       case BUILT_IN_SINCOS:
+       case BUILT_IN_SINCOSF:
+       case BUILT_IN_SINCOSL:
+         {
+           tree sin = gimple_call_arg (call, 1);
+           tree cos = gimple_call_arg (call, 2);
+           return (ptr_deref_may_alias_ref_p_1 (sin, ref)
+                   || ptr_deref_may_alias_ref_p_1 (cos, ref));
+         }
+       default:
+         /* Fallthru to general call handling.  */;
+      }
+
   /* Check if base is a global static variable that is not written
      by the function.  */
-  if (TREE_CODE (base) == VAR_DECL
-      && TREE_STATIC (base)
-      && !TREE_PUBLIC (base))
+  if (callee != NULL_TREE
+      && TREE_CODE (base) == VAR_DECL
+      && TREE_STATIC (base))
     {
-      tree callee = gimple_call_fndecl (call);
       bitmap not_written;
 
-      if (callee != NULL_TREE
-         && (not_written
-               = ipa_reference_get_not_written_global (cgraph_node (callee)))
+      if ((not_written
+            = ipa_reference_get_not_written_global (cgraph_node (callee)))
          && bitmap_bit_p (not_written, DECL_UID (base)))
        return false;
     }
 
+  /* Check if the base variable is call-clobbered.  */
   if (DECL_P (base))
-    return is_call_clobbered (base);
+    return pt_solution_includes (gimple_call_clobber_set (call), base);
+  else if ((INDIRECT_REF_P (base)
+           || TREE_CODE (base) == MEM_REF)
+          && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
+    {
+      struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
+      if (!pi)
+       return true;
+
+      return pt_solutions_intersect (gimple_call_clobber_set (call), &pi->pt);
+    }
 
   return true;
 }
 
-static bool ATTRIBUTE_UNUSED
+/* If the call in statement CALL may clobber the memory reference REF
+   return true, otherwise return false.  */
+
+bool
 call_may_clobber_ref_p (gimple call, tree ref)
 {
   bool res;
@@ -983,11 +1498,15 @@ stmt_may_clobber_ref_p_1 (gimple stmt, ao_ref *ref)
 
       return call_may_clobber_ref_p_1 (stmt, ref);
     }
-  else if (is_gimple_assign (stmt))
+  else if (gimple_assign_single_p (stmt))
     {
-      ao_ref r;
-      ao_ref_init (&r, gimple_assign_lhs (stmt));
-      return refs_may_alias_p_1 (ref, &r, true);
+      tree lhs = gimple_assign_lhs (stmt);
+      if (!is_gimple_reg (lhs))
+       {
+         ao_ref r;
+         ao_ref_init (&r, gimple_assign_lhs (stmt));
+         return refs_may_alias_p_1 (ref, &r, true);
+       }
     }
   else if (gimple_code (stmt) == GIMPLE_ASM)
     return true;
@@ -1004,8 +1523,6 @@ stmt_may_clobber_ref_p (gimple stmt, tree ref)
 }
 
 
-static tree get_continuation_for_phi (gimple, ao_ref *, bitmap *);
-
 /* Walk the virtual use-def chain of VUSE until hitting the virtual operand
    TARGET or a statement clobbering the memory reference REF in which
    case false is returned.  The walk starts with VUSE, one argument of PHI.  */
@@ -1049,7 +1566,7 @@ maybe_skip_until (gimple phi, tree target, ao_ref *ref,
    clobber REF.  Returns NULL_TREE if no suitable virtual operand can
    be found.  */
 
-static tree
+tree
 get_continuation_for_phi (gimple phi, ao_ref *ref, bitmap *visited)
 {
   unsigned nargs = gimple_phi_num_args (phi);
@@ -1066,6 +1583,7 @@ get_continuation_for_phi (gimple phi, ao_ref *ref, bitmap *visited)
       tree arg1 = PHI_ARG_DEF (phi, 1);
       gimple def0 = SSA_NAME_DEF_STMT (arg0);
       gimple def1 = SSA_NAME_DEF_STMT (arg1);
+      tree common_vuse;
 
       if (arg0 == arg1)
        return arg0;
@@ -1084,6 +1602,26 @@ get_continuation_for_phi (gimple phi, ao_ref *ref, bitmap *visited)
          if (maybe_skip_until (phi, arg1, ref, arg0, visited))
            return arg1;
        }
+      /* Special case of a diamond:
+          MEM_1 = ...
+          goto (cond) ? L1 : L2
+          L1: store1 = ...    #MEM_2 = vuse(MEM_1)
+              goto L3
+          L2: store2 = ...    #MEM_3 = vuse(MEM_1)
+          L3: MEM_4 = PHI<MEM_2, MEM_3>
+        We were called with the PHI at L3, MEM_2 and MEM_3 don't
+        dominate each other, but still we can easily skip this PHI node
+        if we recognize that the vuse MEM operand is the same for both,
+        and that we can skip both statements (they don't clobber us).
+        This is still linear.  Don't use maybe_skip_until, that might
+        potentially be slow.  */
+      else if ((common_vuse = gimple_vuse (def0))
+              && common_vuse == gimple_vuse (def1))
+       {
+         if (!stmt_may_clobber_ref_p_1 (def0, ref)
+             && !stmt_may_clobber_ref_p_1 (def1, ref))
+           return common_vuse;
+       }
     }
 
   return NULL_TREE;
@@ -1177,8 +1715,8 @@ walk_non_aliased_vuses (ao_ref *ref, tree vuse,
    The function returns the number of statements walked.  */
 
 static unsigned int
-walk_aliased_vdefs_1 (tree ref, tree vdef,
-                     bool (*walker)(tree, tree, void *), void *data,
+walk_aliased_vdefs_1 (ao_ref *ref, tree vdef,
+                     bool (*walker)(ao_ref *, tree, void *), void *data,
                      bitmap *visited, unsigned int cnt)
 {
   do
@@ -1205,7 +1743,7 @@ walk_aliased_vdefs_1 (tree ref, tree vdef,
       /* ???  Do we want to account this to TV_ALIAS_STMT_WALK?  */
       cnt++;
       if ((!ref
-          || stmt_may_clobber_ref_p (def_stmt, ref))
+          || stmt_may_clobber_ref_p_1 (def_stmt, ref))
          && (*walker) (ref, vdef, data))
        return cnt;
 
@@ -1215,8 +1753,8 @@ walk_aliased_vdefs_1 (tree ref, tree vdef,
 }
 
 unsigned int
-walk_aliased_vdefs (tree ref, tree vdef,
-                   bool (*walker)(tree, tree, void *), void *data,
+walk_aliased_vdefs (ao_ref *ref, tree vdef,
+                   bool (*walker)(ao_ref *, tree, void *), void *data,
                    bitmap *visited)
 {
   bitmap local_visited = NULL;