OSDN Git Service

gcc/fortran:
[pf3gnuchains/gcc-fork.git] / gcc / tree-ssa-structalias.c
index 576ae1b..ddd7de3 100644 (file)
@@ -250,16 +250,16 @@ struct variable_info
   /* True if this is a heap variable.  */
   unsigned int is_heap_var:1;
 
+  /* True if we may not use TBAA to prune references to this
+     variable.  This is used for C++ placement new.  */
+  unsigned int no_tbaa_pruning : 1;
+
   /* Points-to set for this variable.  */
   bitmap solution;
 
   /* Old points-to set for this variable.  */
   bitmap oldsolution;
 
-  /* Finished points-to set for this variable (IE what is returned
-     from find_what_p_points_to.  */
-  bitmap finished_solution;
-
   /* Variable ids represented by this node.  */
   bitmap variables;
 
@@ -330,9 +330,10 @@ static tree
 heapvar_lookup (tree from)
 {
   struct tree_map *h, in;
-  in.from = from;
+  in.base.from = from;
 
-  h = htab_find_with_hash (heapvar_for_stmt, &in, htab_hash_pointer (from));
+  h = (struct tree_map *) htab_find_with_hash (heapvar_for_stmt, &in,
+                                              htab_hash_pointer (from));
   if (h)
     return h->to;
   return NULL_TREE;
@@ -347,9 +348,9 @@ heapvar_insert (tree from, tree to)
   struct tree_map *h;
   void **loc;
 
-  h = ggc_alloc (sizeof (struct tree_map));
+  h = GGC_NEW (struct tree_map);
   h->hash = htab_hash_pointer (from);
-  h->from = from;
+  h->base.from = from;
   h->to = to;
   loc = htab_find_slot_with_hash (heapvar_for_stmt, h, h->hash, INSERT);
   *(struct tree_map **) loc = h;
@@ -361,7 +362,8 @@ heapvar_insert (tree from, tree to)
 static varinfo_t
 new_var_info (tree t, unsigned int id, const char *name)
 {
-  varinfo_t ret = pool_alloc (variable_info_pool);
+  varinfo_t ret = (varinfo_t) pool_alloc (variable_info_pool);
+  tree var;
 
   ret->id = id;
   ret->name = name;
@@ -372,9 +374,14 @@ new_var_info (tree t, unsigned int id, const char *name)
   ret->is_special_var = false;
   ret->is_unknown_size_var = false;
   ret->has_union = false;
+  var = t;
+  if (TREE_CODE (var) == SSA_NAME)
+    var = SSA_NAME_VAR (var);
+  ret->no_tbaa_pruning = (DECL_P (var)
+                         && POINTER_TYPE_P (TREE_TYPE (var))
+                         && DECL_NO_TBAA_P (var));
   ret->solution = BITMAP_ALLOC (&pta_obstack);
   ret->oldsolution = BITMAP_ALLOC (&oldpta_obstack);
-  ret->finished_solution = NULL;
   ret->next = NULL;
   ret->collapsed_to = NULL;
   return ret;
@@ -518,7 +525,7 @@ static constraint_t
 new_constraint (const struct constraint_expr lhs,
                const struct constraint_expr rhs)
 {
-  constraint_t ret = pool_alloc (constraint_pool);
+  constraint_t ret = (constraint_t) pool_alloc (constraint_pool);
   ret->lhs = lhs;
   ret->rhs = rhs;
   return ret;
@@ -1199,6 +1206,9 @@ unify_nodes (constraint_graph_t graph, unsigned int to, unsigned int from,
   merge_graph_nodes (graph, to, from);
   merge_node_constraints (graph, to, from);
 
+  if (get_varinfo (from)->no_tbaa_pruning)
+    get_varinfo (to)->no_tbaa_pruning = true;
+
   if (update_changed && TEST_BIT (changed, from))
     {
       RESET_BIT (changed, from);
@@ -2138,67 +2148,31 @@ solve_graph (constraint_graph_t graph)
 }
 
 /* Map from trees to variable infos.  */
-static htab_t vi_for_tree;
-
-typedef struct tree_vi
-{
-  tree t;
-  varinfo_t vi;
-} *tree_vi_t;
-
-/* Hash a tree id structure.  */
+static struct pointer_map_t *vi_for_tree;
 
-static hashval_t
-tree_vi_hash (const void *p)
-{
-  const tree_vi_t ta = (tree_vi_t) p;
-  return htab_hash_pointer (ta->t);
-}
 
-/* Return true if the tree in P1 and the tree in P2 are the same.  */
-
-static int
-tree_vi_eq (const void *p1, const void *p2)
-{
-  const tree_vi_t ta1 = (tree_vi_t) p1;
-  const tree_vi_t ta2 = (tree_vi_t) p2;
-  return ta1->t == ta2->t;
-}
-
-/* Insert ID as the variable id for tree T in the hashtable.  */
+/* Insert ID as the variable id for tree T in the vi_for_tree map.  */
 
 static void
 insert_vi_for_tree (tree t, varinfo_t vi)
 {
-  void **slot;
-  struct tree_vi finder;
-  tree_vi_t new_pair;
-
-  finder.t = t;
-  slot = htab_find_slot (vi_for_tree, &finder, INSERT);
+  void **slot = pointer_map_insert (vi_for_tree, t);
+  gcc_assert (vi);
   gcc_assert (*slot == NULL);
-  new_pair = XNEW (struct tree_vi);
-  new_pair->t = t;
-  new_pair->vi = vi;
-  *slot = (void *)new_pair;
+  *slot = vi;
 }
 
 /* Find the variable info for tree T in VI_FOR_TREE.  If T does not
-   exist in the hash table, return false, otherwise, return true and
-   set *VI to the varinfo we found.  */
+   exist in the map, return NULL, otherwise, return the varinfo we found.  */
 
-static bool
-lookup_vi_for_tree (tree t, varinfo_t *vi)
+static varinfo_t
+lookup_vi_for_tree (tree t)
 {
-  tree_vi_t pair;
-  struct tree_vi finder;
+  void **slot = pointer_map_contains (vi_for_tree, t);
+  if (slot == NULL)
+    return NULL;
 
-  finder.t = t;
-  pair = htab_find (vi_for_tree,  &finder);
-  if (pair == NULL)
-    return false;
-  *vi = pair->vi;
-  return true;
+  return (varinfo_t) *slot;
 }
 
 /* Return a printable name for DECL  */
@@ -2235,21 +2209,17 @@ alias_get_name (tree decl)
   return res;
 }
 
-/* Find the variable id for tree T in the hashtable.
-   If T doesn't exist in the hash table, create an entry for it.  */
+/* Find the variable id for tree T in the map.
+   If T doesn't exist in the map, create an entry for it and return it.  */
 
 static varinfo_t
 get_vi_for_tree (tree t)
 {
-  tree_vi_t pair;
-  struct tree_vi finder;
-
-  finder.t = t;
-  pair = htab_find (vi_for_tree,  &finder);
-  if (pair == NULL)
+  void **slot = pointer_map_contains (vi_for_tree, t);
+  if (slot == NULL)
     return get_varinfo (create_variable_info_for (t, alias_get_name (t)));
 
-  return pair->vi;
+  return (varinfo_t) *slot;
 }
 
 /* Get a constraint expression from an SSA_VAR_P node.  */
@@ -2352,9 +2322,11 @@ could_have_pointers (tree t)
 {
   tree type = TREE_TYPE (t);
 
-  if (POINTER_TYPE_P (type) || AGGREGATE_TYPE_P (type)
+  if (POINTER_TYPE_P (type)
+      || AGGREGATE_TYPE_P (type)
       || TREE_CODE (type) == COMPLEX_TYPE)
     return true;
+
   return false;
 }
 
@@ -2552,6 +2524,7 @@ get_constraint_for (tree t, VEC (ce_s, heap) **results)
   switch (TREE_CODE_CLASS (TREE_CODE (t)))
     {
     case tcc_expression:
+    case tcc_vl_exp:
       {
        switch (TREE_CODE (t))
          {
@@ -2563,6 +2536,7 @@ get_constraint_for (tree t, VEC (ce_s, heap) **results)
              tree pttype = TREE_TYPE (TREE_TYPE (t));
 
              get_constraint_for (exp, results);
+
              /* Make sure we capture constraints to all elements
                 of an array.  */
              if ((handled_component_p (exp)
@@ -2849,7 +2823,7 @@ do_rhs_deref_structure_copy (const struct constraint_expr lhs,
 }
 
 /* Handle the structure copy case where we have a structure copy
-   between a aggregate on the RHS and a dereference of a pointer on
+   between an aggregate on the RHS and a dereference of a pointer on
    the LHS that is of SIZE (in bits)
 
    For each field of the rhs variable (rhsfield)
@@ -3040,6 +3014,7 @@ do_structure_copy (tree lhsop, tree rhsop)
     }
 }
 
+
 /* Update related alias information kept in AI.  This is used when
    building name tags, alias sets and deciding grouping heuristics.
    STMT is the statement to process.  This function also updates
@@ -3051,15 +3026,21 @@ update_alias_info (tree stmt, struct alias_info *ai)
   bitmap addr_taken;
   use_operand_p use_p;
   ssa_op_iter iter;
+  bool stmt_dereferences_ptr_p;
   enum escape_type stmt_escape_type = is_escape_site (stmt);
+  struct mem_ref_stats_d *mem_ref_stats = gimple_mem_ref_stats (cfun);
+
+  stmt_dereferences_ptr_p = false;
 
   if (stmt_escape_type == ESCAPE_TO_CALL
       || stmt_escape_type == ESCAPE_TO_PURE_CONST)
     {
-      ai->num_calls_found++;
+      mem_ref_stats->num_call_sites++;
       if (stmt_escape_type == ESCAPE_TO_PURE_CONST)
-       ai->num_pure_const_calls_found++;
+       mem_ref_stats->num_pure_const_call_sites++;
     }
+  else if (stmt_escape_type == ESCAPE_TO_ASM)
+    mem_ref_stats->num_asm_sites++;
 
   /* Mark all the variables whose address are taken by the statement.  */
   addr_taken = addresses_taken (stmt);
@@ -3083,17 +3064,15 @@ update_alias_info (tree stmt, struct alias_info *ai)
        }
     }
 
-  /* Process each operand use.  If an operand may be aliased, keep
-     track of how many times it's being used.  For pointers, determine
-     whether they are dereferenced by the statement, or whether their
-     value escapes, etc.  */
+  /* Process each operand use.  For pointers, determine whether they
+     are dereferenced by the statement, or whether their value
+     escapes, etc.  */
   FOR_EACH_PHI_OR_STMT_USE (use_p, stmt, iter, SSA_OP_USE)
     {
       tree op, var;
       var_ann_t v_ann;
       struct ptr_info_def *pi;
-      bool is_store, is_potential_deref;
-      unsigned num_uses, num_derefs;
+      unsigned num_uses, num_loads, num_stores;
 
       op = USE_FROM_PTR (use_p);
 
@@ -3113,12 +3092,11 @@ update_alias_info (tree stmt, struct alias_info *ai)
             so that they can be treated like regular statements?
             Currently, they are treated as second-class
             statements.  */
-         add_to_addressable_set (TREE_OPERAND (op, 0),
-                                 &addressable_vars);
+         add_to_addressable_set (TREE_OPERAND (op, 0), &addressable_vars);
          continue;
        }
 
-      /* Ignore constants.  */
+      /* Ignore constants (they may occur in PHI node arguments).  */
       if (TREE_CODE (op) != SSA_NAME)
        continue;
 
@@ -3149,7 +3127,7 @@ update_alias_info (tree stmt, struct alias_info *ai)
 
       /* Determine whether OP is a dereferenced pointer, and if STMT
         is an escape point, whether OP escapes.  */
-      count_uses_and_derefs (op, stmt, &num_uses, &num_derefs, &is_store);
+      count_uses_and_derefs (op, stmt, &num_uses, &num_loads, &num_stores);
 
       /* Handle a corner case involving address expressions of the
         form '&PTR->FLD'.  The problem with these expressions is that
@@ -3172,7 +3150,6 @@ update_alias_info (tree stmt, struct alias_info *ai)
         are not GIMPLE invariants), they can only appear on the RHS
         of an assignment and their base address is always an
         INDIRECT_REF expression.  */
-      is_potential_deref = false;
       if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT
          && TREE_CODE (GIMPLE_STMT_OPERAND (stmt, 1)) == ADDR_EXPR
          && !is_gimple_val (GIMPLE_STMT_OPERAND (stmt, 1)))
@@ -3183,10 +3160,10 @@ update_alias_info (tree stmt, struct alias_info *ai)
          tree base = get_base_address (TREE_OPERAND (rhs, 0));
          if (TREE_CODE (base) == INDIRECT_REF
              && TREE_OPERAND (base, 0) == op)
-           is_potential_deref = true;
+           num_loads++;
        }
 
-      if (num_derefs > 0 || is_potential_deref)
+      if (num_loads + num_stores > 0)
        {
          /* Mark OP as dereferenced.  In a subsequent pass,
             dereferenced pointers that point to a set of
@@ -3197,13 +3174,20 @@ update_alias_info (tree stmt, struct alias_info *ai)
          /* If this is a store operation, mark OP as being
             dereferenced to store, otherwise mark it as being
             dereferenced to load.  */
-         if (is_store)
+         if (num_stores > 0)
            pointer_set_insert (ai->dereferenced_ptrs_store, var);
          else
            pointer_set_insert (ai->dereferenced_ptrs_load, var);
+
+         /* Update the frequency estimate for all the dereferences of
+            pointer OP.  */
+         update_mem_sym_stats_from_stmt (op, stmt, num_loads, num_stores);
+         
+         /* Indicate that STMT contains pointer dereferences.  */
+         stmt_dereferences_ptr_p = true;
        }
 
-      if (stmt_escape_type != NO_ESCAPE && num_derefs < num_uses)
+      if (stmt_escape_type != NO_ESCAPE && num_loads + num_stores < num_uses)
        {
          /* If STMT is an escape point and STMT contains at
             least one direct use of OP, then the value of OP
@@ -3228,13 +3212,55 @@ update_alias_info (tree stmt, struct alias_info *ai)
     return;
 
   /* Mark stored variables in STMT as being written to and update the
-     reference counter for potentially aliased symbols in STMT.  */
-  if (stmt_references_memory_p (stmt) && STORED_SYMS (stmt))
+     memory reference stats for all memory symbols referenced by STMT.  */
+  if (stmt_references_memory_p (stmt))
     {
       unsigned i;
       bitmap_iterator bi;
-      EXECUTE_IF_SET_IN_BITMAP (STORED_SYMS (stmt), 0, i, bi)
-       pointer_set_insert (ai->written_vars, referenced_var (i));
+
+      mem_ref_stats->num_mem_stmts++;
+
+      /* Notice that we only update memory reference stats for symbols
+        loaded and stored by the statement if the statement does not
+        contain pointer dereferences and it is not a call/asm site.
+        This is to avoid double accounting problems when creating
+        memory partitions.  After computing points-to information,
+        pointer dereference statistics are used to update the
+        reference stats of the pointed-to variables, so here we
+        should only update direct references to symbols.
+
+        Indirect references are not updated here for two reasons: (1)
+        The first time we compute alias information, the sets
+        LOADED/STORED are empty for pointer dereferences, (2) After
+        partitioning, LOADED/STORED may have references to
+        partitions, not the original pointed-to variables.  So, if we
+        always counted LOADED/STORED here and during partitioning, we
+        would count many symbols more than once.
+
+        This does cause some imprecision when a statement has a
+        combination of direct symbol references and pointer
+        dereferences (e.g., MEMORY_VAR = *PTR) or if a call site has
+        memory symbols in its argument list, but these cases do not
+        occur so frequently as to constitute a serious problem.  */
+      if (STORED_SYMS (stmt))
+       EXECUTE_IF_SET_IN_BITMAP (STORED_SYMS (stmt), 0, i, bi)
+         {
+           tree sym = referenced_var (i);
+           pointer_set_insert (ai->written_vars, sym);
+           if (!stmt_dereferences_ptr_p
+               && stmt_escape_type != ESCAPE_TO_CALL
+               && stmt_escape_type != ESCAPE_TO_PURE_CONST
+               && stmt_escape_type != ESCAPE_TO_ASM)
+             update_mem_sym_stats_from_stmt (sym, stmt, 0, 1);
+         }
+
+      if (!stmt_dereferences_ptr_p
+         && LOADED_SYMS (stmt)
+         && stmt_escape_type != ESCAPE_TO_CALL
+         && stmt_escape_type != ESCAPE_TO_PURE_CONST
+         && stmt_escape_type != ESCAPE_TO_ASM)
+       EXECUTE_IF_SET_IN_BITMAP (LOADED_SYMS (stmt), 0, i, bi)
+         update_mem_sym_stats_from_stmt (referenced_var (i), stmt, 1, 0);
     }
 }
 
@@ -3265,25 +3291,26 @@ handle_ptr_arith (VEC (ce_s, heap) *lhsc, tree expr)
   unsigned int i = 0;
   unsigned int j = 0;
   VEC (ce_s, heap) *temp = NULL;
-  unsigned int rhsoffset = 0;
+  unsigned HOST_WIDE_INT rhsoffset = 0;
 
-  if (TREE_CODE (expr) != PLUS_EXPR
-      && TREE_CODE (expr) != MINUS_EXPR)
+  if (TREE_CODE (expr) != POINTER_PLUS_EXPR)
     return false;
 
   op0 = TREE_OPERAND (expr, 0);
   op1 = TREE_OPERAND (expr, 1);
+  gcc_assert (POINTER_TYPE_P (TREE_TYPE (op0)));
 
   get_constraint_for (op0, &temp);
-  if (POINTER_TYPE_P (TREE_TYPE (op0))
-      && TREE_CODE (op1) == INTEGER_CST
-      && TREE_CODE (expr) == PLUS_EXPR)
+
+  /* We can only handle positive offsets that do not overflow
+     if we multiply it by BITS_PER_UNIT.  */
+  if (host_integerp (op1, 1))
     {
       rhsoffset = TREE_INT_CST_LOW (op1) * BITS_PER_UNIT;
-    }
-  else
-    return false;
 
+      if (rhsoffset / BITS_PER_UNIT != TREE_INT_CST_LOW (op1))
+       return false;
+    }
 
   for (i = 0; VEC_iterate (ce_s, lhsc, i, c); i++)
     for (j = 0; VEC_iterate (ce_s, temp, j, c2); j++)
@@ -3365,9 +3392,9 @@ find_func_aliases (tree origt)
        }
     }
   /* In IPA mode, we need to generate constraints to pass call
-     arguments through their calls.   There are two case, either a
-     modify_expr when we are returning a value, or just a plain
-     call_expr when we are not.   */
+     arguments through their calls.   There are two cases, either a
+     GIMPLE_MODIFY_STMT when we are returning a value, or just a plain
+     CALL_EXPR when we are not.   */
   else if (in_ipa_mode
           && ((TREE_CODE (t) == GIMPLE_MODIFY_STMT
                && TREE_CODE (GIMPLE_STMT_OPERAND (t, 1)) == CALL_EXPR
@@ -3379,7 +3406,8 @@ find_func_aliases (tree origt)
     {
       tree lhsop;
       tree rhsop;
-      tree arglist;
+      tree arg;
+      call_expr_arg_iterator iter;
       varinfo_t fi;
       int i = 1;
       tree decl;
@@ -3404,17 +3432,15 @@ find_func_aliases (tree origt)
        }
       else
        {
-         decl = TREE_OPERAND (rhsop, 0);
+         decl = CALL_EXPR_FN (rhsop);
          fi = get_vi_for_tree (decl);
        }
 
       /* Assign all the passed arguments to the appropriate incoming
         parameters of the function.  */
-      arglist = TREE_OPERAND (rhsop, 1);
 
-      for (;arglist; arglist = TREE_CHAIN (arglist))
-       {
-         tree arg = TREE_VALUE (arglist);
+      FOR_EACH_CALL_EXPR_ARG (arg, iter, rhsop)
+       {
          struct constraint_expr lhs ;
          struct constraint_expr *rhsp;
 
@@ -3439,6 +3465,7 @@ find_func_aliases (tree origt)
            }
          i++;
        }
+
       /* If we are returning a value, assign it to the result.  */
       if (lhsop)
        {
@@ -3495,6 +3522,7 @@ find_func_aliases (tree origt)
                  case tcc_constant:
                  case tcc_exceptional:
                  case tcc_expression:
+                 case tcc_vl_exp:
                  case tcc_unary:
                      {
                        unsigned int j;
@@ -3528,7 +3556,7 @@ find_func_aliases (tree origt)
                     to process expressions other than simple operands
                     (e.g. INDIRECT_REF, ADDR_EXPR, CALL_EXPR).  */
                  default:
-                   for (i = 0; i < TREE_CODE_LENGTH (TREE_CODE (rhsop)); i++)
+                   for (i = 0; i < TREE_OPERAND_LENGTH (rhsop); i++)
                      {
                        tree op = TREE_OPERAND (rhsop, i);
                        unsigned int j;
@@ -3550,6 +3578,14 @@ find_func_aliases (tree origt)
            }
        }
     }
+  else if (TREE_CODE (t) == CHANGE_DYNAMIC_TYPE_EXPR)
+    {
+      unsigned int j;
+
+      get_constraint_for (CHANGE_DYNAMIC_TYPE_LOCATION (t), &lhsc);
+      for (j = 0; VEC_iterate (ce_s, lhsc, j, c); ++j)
+       get_varinfo (c->var)->no_tbaa_pruning = true;
+    }
 
   /* After promoting variables and computing aliasing we will
      need to re-scan most statements.  FIXME: Try to minimize the
@@ -3659,11 +3695,13 @@ sort_fieldstack (VEC(fieldoff_s,heap) *fieldstack)
    than just the immediately containing structure.  Returns the number
    of fields pushed.
    HAS_UNION is set to true if we find a union type as a field of
-   TYPE.  */
+   TYPE.  ADDRESSABLE_TYPE is the type of the outermost object that could have
+   its address taken.  */
 
 int
 push_fields_onto_fieldstack (tree type, VEC(fieldoff_s,heap) **fieldstack,
-                            HOST_WIDE_INT offset, bool *has_union)
+                            HOST_WIDE_INT offset, bool *has_union,
+                            tree addressable_type)
 {
   tree field;
   int count = 0;
@@ -3676,12 +3714,14 @@ push_fields_onto_fieldstack (tree type, VEC(fieldoff_s,heap) **fieldstack,
       real_part->size = TYPE_SIZE (TREE_TYPE (type));
       real_part->offset = offset;
       real_part->decl = NULL_TREE;
+      real_part->alias_set = -1;
 
       img_part = VEC_safe_push (fieldoff_s, heap, *fieldstack, NULL);
       img_part->type = TREE_TYPE (type);
       img_part->size = TYPE_SIZE (TREE_TYPE (type));
       img_part->offset = offset + TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (type)));
       img_part->decl = NULL_TREE;
+      img_part->alias_set = -1;
 
       return 2;
     }
@@ -3719,7 +3759,8 @@ push_fields_onto_fieldstack (tree type, VEC(fieldoff_s,heap) **fieldstack,
            push = true;
          else if (!(pushed = push_fields_onto_fieldstack
                     (TREE_TYPE (type), fieldstack,
-                     offset + i * TREE_INT_CST_LOW (elsz), has_union)))
+                     offset + i * TREE_INT_CST_LOW (elsz), has_union,
+                     TREE_TYPE (type))))
            /* Empty structures may have actual size, like in C++. So
               see if we didn't push any subfields and the size is
               nonzero, push the field onto the stack */
@@ -3734,6 +3775,7 @@ push_fields_onto_fieldstack (tree type, VEC(fieldoff_s,heap) **fieldstack,
              pair->size = elsz;
              pair->decl = NULL_TREE;
              pair->offset = offset + i * TREE_INT_CST_LOW (elsz);
+             pair->alias_set = -1;
              count++;
            }
          else
@@ -3758,7 +3800,10 @@ push_fields_onto_fieldstack (tree type, VEC(fieldoff_s,heap) **fieldstack,
          push = true;
        else if (!(pushed = push_fields_onto_fieldstack
                   (TREE_TYPE (field), fieldstack,
-                   offset + bitpos_of_field (field), has_union))
+                   offset + bitpos_of_field (field), has_union,
+                   (DECL_NONADDRESSABLE_P (field)
+                    ? addressable_type
+                    : TREE_TYPE (field))))
                 && DECL_SIZE (field)
                 && !integer_zerop (DECL_SIZE (field)))
          /* Empty structures may have actual size, like in C++. So
@@ -3775,6 +3820,10 @@ push_fields_onto_fieldstack (tree type, VEC(fieldoff_s,heap) **fieldstack,
            pair->size = DECL_SIZE (field);
            pair->decl = field;
            pair->offset = offset + bitpos_of_field (field);
+           if (DECL_NONADDRESSABLE_P (field))
+             pair->alias_set = get_alias_set (addressable_type);
+           else
+             pair->alias_set = -1;
            count++;
          }
        else
@@ -3973,7 +4022,8 @@ create_variable_info_for (tree decl, const char *name)
             || TREE_CODE (decltype) == QUAL_UNION_TYPE;
   if (var_can_have_subvars (decl) && use_field_sensitive && !hasunion)
     {
-      push_fields_onto_fieldstack (decltype, &fieldstack, 0, &hasunion);
+      push_fields_onto_fieldstack (decltype, &fieldstack, 0, &hasunion,
+                                  decltype);
       if (hasunion)
        {
          VEC_free (fieldoff_s, heap, fieldstack);
@@ -4117,7 +4167,10 @@ dump_solution_for_var (FILE *file, unsigned int var)
        {
          fprintf (file, "%s ", get_varinfo (i)->name);
        }
-      fprintf (file, "}\n");
+      fprintf (file, "}");
+      if (vi->no_tbaa_pruning)
+       fprintf (file, " no-tbaa-pruning");
+      fprintf (file, "\n");
     }
 }
 
@@ -4138,8 +4191,8 @@ intra_create_variable_infos (void)
   tree t;
   struct constraint_expr lhs, rhs;
 
-  /* For each incoming pointer argument arg, ARG = ANYTHING or a
-     dummy variable if flag_argument_noalias > 2. */
+  /* For each incoming pointer argument arg, create the constraint ARG
+     = ANYTHING or a dummy variable if flag_argument_noalias is set.  */
   for (t = DECL_ARGUMENTS (current_function_decl); t; t = TREE_CHAIN (t))
     {
       varinfo_t p;
@@ -4147,11 +4200,11 @@ intra_create_variable_infos (void)
       if (!could_have_pointers (t))
        continue;
 
-      /* With flag_argument_noalias greater than two means that the incoming
-        argument cannot alias anything except for itself so create a HEAP
-        variable.  */
-      if (POINTER_TYPE_P (TREE_TYPE (t))
-         && flag_argument_noalias > 2)
+      /* If flag_argument_noalias is set, then function pointer
+        arguments are guaranteed not to point to each other.  In that
+        case, create an artificial variable PARM_NOALIAS and the
+        constraint ARG = &PARM_NOALIAS.  */
+      if (POINTER_TYPE_P (TREE_TYPE (t)) && flag_argument_noalias > 0)
        {
          varinfo_t vi;
          tree heapvar = heapvar_lookup (t);
@@ -4162,14 +4215,26 @@ intra_create_variable_infos (void)
 
          if (heapvar == NULL_TREE)
            {
+             var_ann_t ann;
              heapvar = create_tmp_var_raw (TREE_TYPE (TREE_TYPE (t)),
                                            "PARM_NOALIAS");
-             get_var_ann (heapvar)->is_heapvar = 1;
              DECL_EXTERNAL (heapvar) = 1;
              if (gimple_referenced_vars (cfun))
                add_referenced_var (heapvar);
+
              heapvar_insert (t, heapvar);
+
+             ann = get_var_ann (heapvar);
+             if (flag_argument_noalias == 1)
+               ann->noalias_state = NO_ALIAS;
+             else if (flag_argument_noalias == 2)
+               ann->noalias_state = NO_ALIAS_GLOBAL;
+             else if (flag_argument_noalias == 3)
+               ann->noalias_state = NO_ALIAS_ANYTHING;
+             else
+               gcc_unreachable ();
            }
+
          vi = get_vi_for_tree (heapvar);
          vi->is_artificial_var = 1;
          vi->is_heap_var = 1;
@@ -4193,13 +4258,87 @@ intra_create_variable_infos (void)
     }
 }
 
+/* Structure used to put solution bitmaps in a hashtable so they can
+   be shared among variables with the same points-to set.  */
+
+typedef struct shared_bitmap_info
+{
+  bitmap pt_vars;
+  hashval_t hashcode;
+} *shared_bitmap_info_t;
+
+static htab_t shared_bitmap_table;
+
+/* Hash function for a shared_bitmap_info_t */
+
+static hashval_t
+shared_bitmap_hash (const void *p)
+{
+  const shared_bitmap_info_t bi = (shared_bitmap_info_t) p;
+  return bi->hashcode;
+}
+
+/* Equality function for two shared_bitmap_info_t's. */
+
+static int
+shared_bitmap_eq (const void *p1, const void *p2)
+{
+  const shared_bitmap_info_t sbi1 = (shared_bitmap_info_t) p1;
+  const shared_bitmap_info_t sbi2 = (shared_bitmap_info_t) p2;
+  return bitmap_equal_p (sbi1->pt_vars, sbi2->pt_vars);
+}
+
+/* Lookup a bitmap in the shared bitmap hashtable, and return an already
+   existing instance if there is one, NULL otherwise.  */
+
+static bitmap
+shared_bitmap_lookup (bitmap pt_vars)
+{
+  void **slot;
+  struct shared_bitmap_info sbi;
+
+  sbi.pt_vars = pt_vars;
+  sbi.hashcode = bitmap_hash (pt_vars);
+  
+  slot = htab_find_slot_with_hash (shared_bitmap_table, &sbi,
+                                  sbi.hashcode, NO_INSERT);
+  if (!slot)
+    return NULL;
+  else
+    return ((shared_bitmap_info_t) *slot)->pt_vars;
+}
+
+
+/* Add a bitmap to the shared bitmap hashtable.  */
+
+static void
+shared_bitmap_add (bitmap pt_vars)
+{
+  void **slot;
+  shared_bitmap_info_t sbi = XNEW (struct shared_bitmap_info);
+  
+  sbi->pt_vars = pt_vars;
+  sbi->hashcode = bitmap_hash (pt_vars);
+  
+  slot = htab_find_slot_with_hash (shared_bitmap_table, sbi,
+                                  sbi->hashcode, INSERT);
+  gcc_assert (!*slot);
+  *slot = (void *) sbi;
+}
+
+
 /* Set bits in INTO corresponding to the variable uids in solution set
    FROM, which came from variable PTR.
    For variables that are actually dereferenced, we also use type
-   based alias analysis to prune the points-to sets.  */
+   based alias analysis to prune the points-to sets.
+   IS_DEREFED is true if PTR was directly dereferenced, which we use to
+   help determine whether we are we are allowed to prune using TBAA.
+   If NO_TBAA_PRUNING is true, we do not perform any TBAA pruning of
+   the from set.  */
 
 static void
-set_uids_in_ptset (tree ptr, bitmap into, bitmap from)
+set_uids_in_ptset (tree ptr, bitmap into, bitmap from, bool is_derefed,
+                  bool no_tbaa_pruning)
 {
   unsigned int i;
   bitmap_iterator bi;
@@ -4224,7 +4363,8 @@ set_uids_in_ptset (tree ptr, bitmap into, bitmap from)
            bitmap_set_bit (into, DECL_UID (sv->var));
        }
       else if (TREE_CODE (vi->decl) == VAR_DECL
-              || TREE_CODE (vi->decl) == PARM_DECL)
+              || TREE_CODE (vi->decl) == PARM_DECL
+              || TREE_CODE (vi->decl) == RESULT_DECL)
        {
          if (var_can_have_subvars (vi->decl)
              && get_subvars_for_var (vi->decl))
@@ -4235,7 +4375,8 @@ set_uids_in_ptset (tree ptr, bitmap into, bitmap from)
              if (sft)
                {
                  var_alias_set = get_alias_set (sft);
-                 if (!vi->directly_dereferenced
+                 if (no_tbaa_pruning
+                     || (!is_derefed && !vi->directly_dereferenced)
                      || alias_sets_conflict_p (ptr_alias_set, var_alias_set))
                    bitmap_set_bit (into, DECL_UID (sft));
                }
@@ -4249,7 +4390,8 @@ set_uids_in_ptset (tree ptr, bitmap into, bitmap from)
              else
                {
                  var_alias_set = get_alias_set (vi->decl);
-                 if (!vi->directly_dereferenced
+                 if (no_tbaa_pruning
+                     || (!is_derefed && !vi->directly_dereferenced)
                      || alias_sets_conflict_p (ptr_alias_set, var_alias_set))
                    bitmap_set_bit (into, DECL_UID (vi->decl));
                }
@@ -4316,12 +4458,12 @@ set_used_smts (void)
     }
 }
 
-/* Merge the necessary SMT's into the solution set for VI, which is
+/* Merge the necessary SMT's into the bitmap INTO, which is
    P's varinfo.  This involves merging all SMT's that are a subset of
    the SMT necessary for P. */
 
 static void
-merge_smts_into (tree p, varinfo_t vi)
+merge_smts_into (tree p, bitmap solution)
 {
   unsigned int i;
   bitmap_iterator bi;
@@ -4339,7 +4481,7 @@ merge_smts_into (tree p, varinfo_t vi)
 
       /* Need to set the SMT subsets first before this
         will work properly.  */
-      bitmap_set_bit (vi->finished_solution, DECL_UID (smt));
+      bitmap_set_bit (solution, DECL_UID (smt));
       EXECUTE_IF_SET_IN_BITMAP (used_smts, 0, i, bi)
        {
          tree newsmt = referenced_var (i);
@@ -4347,12 +4489,12 @@ merge_smts_into (tree p, varinfo_t vi)
 
          if (alias_set_subset_of (get_alias_set (newsmttype),
                                   smtset))
-           bitmap_set_bit (vi->finished_solution, i);
+           bitmap_set_bit (solution, i);
        }
 
       aliases = MTAG_ALIASES (smt);
       if (aliases)
-        bitmap_ior_into (vi->finished_solution, aliases);
+        bitmap_ior_into (solution, aliases);
     }
 }
 
@@ -4383,9 +4525,9 @@ find_what_p_points_to (tree p)
       && SSA_NAME_IS_DEFAULT_DEF (p))
     lookup_p = SSA_NAME_VAR (p);
 
-  if (lookup_vi_for_tree (lookup_p, &vi))
+  vi = lookup_vi_for_tree (lookup_p);
+  if (vi)
     {
-
       if (vi->is_artificial_var)
        return false;
 
@@ -4405,7 +4547,9 @@ find_what_p_points_to (tree p)
          unsigned int i;
          bitmap_iterator bi;
          bool was_pt_anything = false;
-
+         bitmap finished_solution;
+         bitmap result;
+         
          if (!pi->is_dereferenced)
            return false;
 
@@ -4437,32 +4581,42 @@ find_what_p_points_to (tree p)
                }
            }
 
-         /* Share the final set of variables between the SSA_NAME
-            pointer infos for collapsed nodes that are collapsed to
-            non-special variables.  This is because special vars have
-            no real types associated with them, so while we know the
-            pointers are equivalent to them, we need to generate the
-            solution separately since it will include SMT's from the
-            original non-collapsed variable.  */
-         if (!vi->is_special_var && vi->finished_solution)
-           {
-             pi->pt_vars = vi->finished_solution;
-           }
-         else
+         /* Share the final set of variables when possible.  */
+         
+         finished_solution = BITMAP_GGC_ALLOC ();
+         stats.points_to_sets_created++;
+         
+         /* Instead of using pt_anything, we merge in the SMT aliases
+            for the underlying SMT.  In addition, if they could have
+            pointed to anything, they could point to global memory.
+            But we cannot do that for ref-all pointers because these
+            aliases have not been computed yet.  */
+         if (was_pt_anything)
            {
-             vi->finished_solution = BITMAP_GGC_ALLOC ();
-             stats.points_to_sets_created++;
-
-             /* Instead of using pt_anything, we instead merge in the SMT
-                aliases for the underlying SMT.  */
-             if (was_pt_anything)
+             if (PTR_IS_REF_ALL (p))
                {
-                 merge_smts_into (p, vi);
-                 pi->pt_global_mem = 1;
+                 pi->pt_anything = 1;
+                 return false;
                }
 
-             set_uids_in_ptset (vi->decl, vi->finished_solution, vi->solution);
-             pi->pt_vars = vi->finished_solution;
+             merge_smts_into (p, finished_solution);
+             pi->pt_global_mem = 1;
+           }
+         
+         set_uids_in_ptset (vi->decl, finished_solution, vi->solution,
+                            vi->directly_dereferenced,
+                            vi->no_tbaa_pruning);
+         result = shared_bitmap_lookup (finished_solution);
+
+         if (!result)
+           {
+             shared_bitmap_add (finished_solution);
+             pi->pt_vars = finished_solution;
+           }
+         else
+           {
+             pi->pt_vars = result;
+             bitmap_clear (finished_solution);
            }
 
          if (bitmap_empty_p (pi->pt_vars))
@@ -4633,10 +4787,11 @@ init_alias_vars (void)
                                          sizeof (struct variable_info), 30);
   constraints = VEC_alloc (constraint_t, heap, 8);
   varmap = VEC_alloc (varinfo_t, heap, 8);
-  vi_for_tree = htab_create (10, tree_vi_hash, tree_vi_eq, free);
+  vi_for_tree = pointer_map_create ();
 
   memset (&stats, 0, sizeof (stats));
-
+  shared_bitmap_table = htab_create (511, shared_bitmap_hash,
+                                    shared_bitmap_eq, free);
   init_base_vars ();
 }
 
@@ -4667,7 +4822,7 @@ remove_preds_and_fake_succs (constraint_graph_t graph)
   /* Now reallocate the size of the successor list as, and blow away
      the predecessor bitmaps.  */
   graph->size = VEC_length (varinfo_t, varmap);
-  graph->succs = xrealloc (graph->succs, graph->size * sizeof (bitmap));
+  graph->succs = XRESIZEVEC (bitmap, graph->succs, graph->size);
 
   free (graph->implicit_preds);
   graph->implicit_preds = NULL;
@@ -4676,6 +4831,142 @@ remove_preds_and_fake_succs (constraint_graph_t graph)
   bitmap_obstack_release (&predbitmap_obstack);
 }
 
+/* Compute the set of variables we can't TBAA prune.  */
+
+static void
+compute_tbaa_pruning (void)
+{
+  unsigned int size = VEC_length (varinfo_t, varmap);
+  unsigned int i;
+  bool any;
+
+  changed_count = 0;
+  changed = sbitmap_alloc (size);
+  sbitmap_zero (changed);
+
+  /* Mark all initial no_tbaa_pruning nodes as changed.  */
+  any = false;
+  for (i = 0; i < size; ++i)
+    {
+      varinfo_t ivi = get_varinfo (i);
+
+      if (find (i) == i && ivi->no_tbaa_pruning)
+       {
+         any = true;
+         if ((graph->succs[i] && !bitmap_empty_p (graph->succs[i]))
+             || VEC_length (constraint_t, graph->complex[i]) > 0)
+           {
+             SET_BIT (changed, i);
+             ++changed_count;
+           }
+       }
+    }
+
+  while (changed_count > 0)
+    {
+      struct topo_info *ti = init_topo_info ();
+      ++stats.iterations;
+
+      bitmap_obstack_initialize (&iteration_obstack);
+
+      compute_topo_order (graph, ti);
+
+      while (VEC_length (unsigned, ti->topo_order) != 0)
+       {
+         bitmap_iterator bi;
+
+         i = VEC_pop (unsigned, ti->topo_order);
+
+         /* If this variable is not a representative, skip it.  */
+         if (find (i) != i)
+           continue;
+
+         /* If the node has changed, we need to process the complex
+            constraints and outgoing edges again.  */
+         if (TEST_BIT (changed, i))
+           {
+             unsigned int j;
+             constraint_t c;
+             VEC(constraint_t,heap) *complex = graph->complex[i];
+
+             RESET_BIT (changed, i);
+             --changed_count;
+
+             /* Process the complex copy constraints.  */
+             for (j = 0; VEC_iterate (constraint_t, complex, j, c); ++j)
+               {
+                 if (c->lhs.type == SCALAR && c->rhs.type == SCALAR)
+                   {
+                     varinfo_t lhsvi = get_varinfo (find (c->lhs.var));
+
+                     if (!lhsvi->no_tbaa_pruning)
+                       {
+                         lhsvi->no_tbaa_pruning = true;
+                         if (!TEST_BIT (changed, lhsvi->id))
+                           {
+                             SET_BIT (changed, lhsvi->id);
+                             ++changed_count;
+                           }
+                       }
+                   }
+               }
+
+             /* Propagate to all successors.  */
+             EXECUTE_IF_IN_NONNULL_BITMAP (graph->succs[i], 0, j, bi)
+               {
+                 unsigned int to = find (j);
+                 varinfo_t tovi = get_varinfo (to);
+
+                 /* Don't propagate to ourselves.  */
+                 if (to == i)
+                   continue;
+
+                 if (!tovi->no_tbaa_pruning)
+                   {
+                     tovi->no_tbaa_pruning = true;
+                     if (!TEST_BIT (changed, to))
+                       {
+                         SET_BIT (changed, to);
+                         ++changed_count;
+                       }
+                   }
+               }
+           }
+       }
+
+      free_topo_info (ti);
+      bitmap_obstack_release (&iteration_obstack);
+    }
+
+  sbitmap_free (changed);
+
+  if (any)
+    {
+      for (i = 0; i < size; ++i)
+       {
+         varinfo_t ivi = get_varinfo (i);
+         varinfo_t ivip = get_varinfo (find (i));
+
+         if (ivip->no_tbaa_pruning)
+           {
+             tree var = ivi->decl;
+
+             if (TREE_CODE (var) == SSA_NAME)
+               var = SSA_NAME_VAR (var);
+
+             if (POINTER_TYPE_P (TREE_TYPE (var)))
+               {
+                 DECL_NO_TBAA_P (var) = 1;
+
+                 /* Tell the RTL layer that this pointer can alias
+                    anything.  */
+                 DECL_POINTER_ALIAS_SET (var) = 0;
+               }
+           }
+       }
+    }
+}
+
 /* Create points-to sets for the current function.  See the comments
    at the start of the file for an algorithmic overview.  */
 
@@ -4698,11 +4989,12 @@ compute_points_to_sets (struct alias_info *ai)
       block_stmt_iterator bsi;
       tree phi;
 
-      for (phi = phi_nodes (bb); phi; phi = TREE_CHAIN (phi))
+      for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
        {
          if (is_gimple_reg (PHI_RESULT (phi)))
            {
              find_func_aliases (phi);
+
              /* Update various related attributes like escaped
                 addresses, pointer dereferences for loads and stores.
                 This is used when creating name tags and alias
@@ -4711,7 +5003,7 @@ compute_points_to_sets (struct alias_info *ai)
            }
        }
 
-      for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
+      for (bsi = bsi_start (bb); !bsi_end_p (bsi); )
        {
          tree stmt = bsi_stmt (bsi);
 
@@ -4722,6 +5014,13 @@ compute_points_to_sets (struct alias_info *ai)
             This is used when creating name tags and alias
             sets.  */
          update_alias_info (stmt, ai);
+
+         /* The information in CHANGE_DYNAMIC_TYPE_EXPR nodes has now
+            been captured, and we can remove them.  */
+         if (TREE_CODE (stmt) == CHANGE_DYNAMIC_TYPE_EXPR)
+           bsi_remove (&bsi, true);
+         else
+           bsi_next (&bsi);
        }
     }
 
@@ -4753,6 +5052,8 @@ compute_points_to_sets (struct alias_info *ai)
 
   solve_graph (graph);
 
+  compute_tbaa_pruning ();
+
   if (dump_file)
     dump_sa_points_to_info (dump_file);
 
@@ -4770,16 +5071,18 @@ delete_points_to_sets (void)
   varinfo_t v;
   int i;
 
+  htab_delete (shared_bitmap_table);
   if (dump_file && (dump_flags & TDF_STATS))
     fprintf (dump_file, "Points to sets created:%d\n",
             stats.points_to_sets_created);
 
-  htab_delete (vi_for_tree);
+  pointer_map_destroy (vi_for_tree);
   bitmap_obstack_release (&pta_obstack);
   VEC_free (constraint_t, heap, constraints);
 
   for (i = 0; VEC_iterate (varinfo_t, varmap, i, v); i++)
     VEC_free (constraint_t, heap, graph->complex[i]);
+  free (graph->complex);
 
   free (graph->rep);
   free (graph->succs);
@@ -4848,7 +5151,7 @@ ipa_pta_execute (void)
              block_stmt_iterator bsi;
              tree phi;
 
-             for (phi = phi_nodes (bb); phi; phi = TREE_CHAIN (phi))
+             for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
                {
                  if (is_gimple_reg (PHI_RESULT (phi)))
                    {