OSDN Git Service

2008-06-12 Tobias Burnus <burnus@net-b.de>
[pf3gnuchains/gcc-fork.git] / gcc / tree-ssa-structalias.c
1 /* Tree based points-to analysis
2    Copyright (C) 2005, 2006, 2007 Free Software Foundation, Inc.
3    Contributed by Daniel Berlin <dberlin@dberlin.org>
4
5    This file is part of GCC.
6
7    GCC is free software; you can redistribute it and/or modify
8    under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11
12    GCC is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GCC; see the file COPYING3.  If not see
19    <http://www.gnu.org/licenses/>.  */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "ggc.h"
26 #include "obstack.h"
27 #include "bitmap.h"
28 #include "flags.h"
29 #include "rtl.h"
30 #include "tm_p.h"
31 #include "hard-reg-set.h"
32 #include "basic-block.h"
33 #include "output.h"
34 #include "errors.h"
35 #include "diagnostic.h"
36 #include "tree.h"
37 #include "c-common.h"
38 #include "tree-flow.h"
39 #include "tree-inline.h"
40 #include "varray.h"
41 #include "c-tree.h"
42 #include "tree-gimple.h"
43 #include "hashtab.h"
44 #include "function.h"
45 #include "cgraph.h"
46 #include "tree-pass.h"
47 #include "timevar.h"
48 #include "alloc-pool.h"
49 #include "splay-tree.h"
50 #include "params.h"
51 #include "tree-ssa-structalias.h"
52 #include "cgraph.h"
53 #include "alias.h"
54 #include "pointer-set.h"
55
56 /* The idea behind this analyzer is to generate set constraints from the
57    program, then solve the resulting constraints in order to generate the
58    points-to sets.
59
60    Set constraints are a way of modeling program analysis problems that
61    involve sets.  They consist of an inclusion constraint language,
62    describing the variables (each variable is a set) and operations that
63    are involved on the variables, and a set of rules that derive facts
64    from these operations.  To solve a system of set constraints, you derive
65    all possible facts under the rules, which gives you the correct sets
66    as a consequence.
67
68    See  "Efficient Field-sensitive pointer analysis for C" by "David
69    J. Pearce and Paul H. J. Kelly and Chris Hankin, at
70    http://citeseer.ist.psu.edu/pearce04efficient.html
71
72    Also see "Ultra-fast Aliasing Analysis using CLA: A Million Lines
73    of C Code in a Second" by ""Nevin Heintze and Olivier Tardieu" at
74    http://citeseer.ist.psu.edu/heintze01ultrafast.html
75
76    There are three types of real constraint expressions, DEREF,
77    ADDRESSOF, and SCALAR.  Each constraint expression consists
78    of a constraint type, a variable, and an offset.
79
80    SCALAR is a constraint expression type used to represent x, whether
81    it appears on the LHS or the RHS of a statement.
82    DEREF is a constraint expression type used to represent *x, whether
83    it appears on the LHS or the RHS of a statement.
84    ADDRESSOF is a constraint expression used to represent &x, whether
85    it appears on the LHS or the RHS of a statement.
86
87    Each pointer variable in the program is assigned an integer id, and
88    each field of a structure variable is assigned an integer id as well.
89
90    Structure variables are linked to their list of fields through a "next
91    field" in each variable that points to the next field in offset
92    order.
93    Each variable for a structure field has
94
95    1. "size", that tells the size in bits of that field.
96    2. "fullsize, that tells the size in bits of the entire structure.
97    3. "offset", that tells the offset in bits from the beginning of the
98    structure to this field.
99
100    Thus,
101    struct f
102    {
103      int a;
104      int b;
105    } foo;
106    int *bar;
107
108    looks like
109
110    foo.a -> id 1, size 32, offset 0, fullsize 64, next foo.b
111    foo.b -> id 2, size 32, offset 32, fullsize 64, next NULL
112    bar -> id 3, size 32, offset 0, fullsize 32, next NULL
113
114
115   In order to solve the system of set constraints, the following is
116   done:
117
118   1. Each constraint variable x has a solution set associated with it,
119   Sol(x).
120
121   2. Constraints are separated into direct, copy, and complex.
122   Direct constraints are ADDRESSOF constraints that require no extra
123   processing, such as P = &Q
124   Copy constraints are those of the form P = Q.
125   Complex constraints are all the constraints involving dereferences
126   and offsets (including offsetted copies).
127
128   3. All direct constraints of the form P = &Q are processed, such
129   that Q is added to Sol(P)
130
131   4. All complex constraints for a given constraint variable are stored in a
132   linked list attached to that variable's node.
133
134   5. A directed graph is built out of the copy constraints. Each
135   constraint variable is a node in the graph, and an edge from
136   Q to P is added for each copy constraint of the form P = Q
137
138   6. The graph is then walked, and solution sets are
139   propagated along the copy edges, such that an edge from Q to P
140   causes Sol(P) <- Sol(P) union Sol(Q).
141
142   7.  As we visit each node, all complex constraints associated with
143   that node are processed by adding appropriate copy edges to the graph, or the
144   appropriate variables to the solution set.
145
146   8. The process of walking the graph is iterated until no solution
147   sets change.
148
149   Prior to walking the graph in steps 6 and 7, We perform static
150   cycle elimination on the constraint graph, as well
151   as off-line variable substitution.
152
153   TODO: Adding offsets to pointer-to-structures can be handled (IE not punted
154   on and turned into anything), but isn't.  You can just see what offset
155   inside the pointed-to struct it's going to access.
156
157   TODO: Constant bounded arrays can be handled as if they were structs of the
158   same number of elements.
159
160   TODO: Modeling heap and incoming pointers becomes much better if we
161   add fields to them as we discover them, which we could do.
162
163   TODO: We could handle unions, but to be honest, it's probably not
164   worth the pain or slowdown.  */
165
166 static GTY ((if_marked ("tree_map_marked_p"), param_is (struct tree_map)))
167 htab_t heapvar_for_stmt;
168
169 static bool use_field_sensitive = true;
170 static int in_ipa_mode = 0;
171
172 /* Used for predecessor bitmaps. */
173 static bitmap_obstack predbitmap_obstack;
174
175 /* Used for points-to sets.  */
176 static bitmap_obstack pta_obstack;
177
178 /* Used for oldsolution members of variables. */
179 static bitmap_obstack oldpta_obstack;
180
181 /* Used for per-solver-iteration bitmaps.  */
182 static bitmap_obstack iteration_obstack;
183
184 static unsigned int create_variable_info_for (tree, const char *);
185 typedef struct constraint_graph *constraint_graph_t;
186 static void unify_nodes (constraint_graph_t, unsigned int, unsigned int, bool);
187
188 DEF_VEC_P(constraint_t);
189 DEF_VEC_ALLOC_P(constraint_t,heap);
190
191 #define EXECUTE_IF_IN_NONNULL_BITMAP(a, b, c, d)        \
192   if (a)                                                \
193     EXECUTE_IF_SET_IN_BITMAP (a, b, c, d)
194
195 static struct constraint_stats
196 {
197   unsigned int total_vars;
198   unsigned int nonpointer_vars;
199   unsigned int unified_vars_static;
200   unsigned int unified_vars_dynamic;
201   unsigned int iterations;
202   unsigned int num_edges;
203   unsigned int num_implicit_edges;
204   unsigned int points_to_sets_created;
205 } stats;
206
207 struct variable_info
208 {
209   /* ID of this variable  */
210   unsigned int id;
211
212   /* Name of this variable */
213   const char *name;
214
215   /* Tree that this variable is associated with.  */
216   tree decl;
217
218   /* Offset of this variable, in bits, from the base variable  */
219   unsigned HOST_WIDE_INT offset;
220
221   /* Size of the variable, in bits.  */
222   unsigned HOST_WIDE_INT size;
223
224   /* Full size of the base variable, in bits.  */
225   unsigned HOST_WIDE_INT fullsize;
226
227   /* A link to the variable for the next field in this structure.  */
228   struct variable_info *next;
229
230   /* True if this is a variable created by the constraint analysis, such as
231      heap variables and constraints we had to break up.  */
232   unsigned int is_artificial_var:1;
233
234   /* True if this is a special variable whose solution set should not be
235      changed.  */
236   unsigned int is_special_var:1;
237
238   /* True for variables whose size is not known or variable.  */
239   unsigned int is_unknown_size_var:1;
240
241   /* True for variables that have unions somewhere in them.  */
242   unsigned int has_union:1;
243
244   /* True if this is a heap variable.  */
245   unsigned int is_heap_var:1;
246
247   /* True if we may not use TBAA to prune references to this
248      variable.  This is used for C++ placement new.  */
249   unsigned int no_tbaa_pruning : 1;
250
251   /* Points-to set for this variable.  */
252   bitmap solution;
253
254   /* Old points-to set for this variable.  */
255   bitmap oldsolution;
256
257   /* Variable id this was collapsed to due to type unsafety.  This
258      should be unused completely after build_succ_graph, or something
259      is broken.  */
260   struct variable_info *collapsed_to;
261 };
262 typedef struct variable_info *varinfo_t;
263
264 static varinfo_t first_vi_for_offset (varinfo_t, unsigned HOST_WIDE_INT);
265
266 /* Pool of variable info structures.  */
267 static alloc_pool variable_info_pool;
268
269 DEF_VEC_P(varinfo_t);
270
271 DEF_VEC_ALLOC_P(varinfo_t, heap);
272
273 /* Table of variable info structures for constraint variables.
274    Indexed directly by variable info id.  */
275 static VEC(varinfo_t,heap) *varmap;
276
277 /* Return the varmap element N */
278
279 static inline varinfo_t
280 get_varinfo (unsigned int n)
281 {
282   return VEC_index (varinfo_t, varmap, n);
283 }
284
285 /* Return the varmap element N, following the collapsed_to link.  */
286
287 static inline varinfo_t
288 get_varinfo_fc (unsigned int n)
289 {
290   varinfo_t v = VEC_index (varinfo_t, varmap, n);
291
292   if (v->collapsed_to)
293     return v->collapsed_to;
294   return v;
295 }
296
297 /* Variable that represents the unknown pointer.  */
298 static varinfo_t var_anything;
299 static tree anything_tree;
300 static unsigned int anything_id;
301
302 /* Variable that represents the NULL pointer.  */
303 static varinfo_t var_nothing;
304 static tree nothing_tree;
305 static unsigned int nothing_id;
306
307 /* Variable that represents read only memory.  */
308 static varinfo_t var_readonly;
309 static tree readonly_tree;
310 static unsigned int readonly_id;
311
312 /* Variable that represents integers.  This is used for when people do things
313    like &0->a.b.  */
314 static varinfo_t var_integer;
315 static tree integer_tree;
316 static unsigned int integer_id;
317
318 /* Lookup a heap var for FROM, and return it if we find one.  */
319
320 static tree
321 heapvar_lookup (tree from)
322 {
323   struct tree_map *h, in;
324   in.base.from = from;
325
326   h = (struct tree_map *) htab_find_with_hash (heapvar_for_stmt, &in,
327                                                htab_hash_pointer (from));
328   if (h)
329     return h->to;
330   return NULL_TREE;
331 }
332
333 /* Insert a mapping FROM->TO in the heap var for statement
334    hashtable.  */
335
336 static void
337 heapvar_insert (tree from, tree to)
338 {
339   struct tree_map *h;
340   void **loc;
341
342   h = GGC_NEW (struct tree_map);
343   h->hash = htab_hash_pointer (from);
344   h->base.from = from;
345   h->to = to;
346   loc = htab_find_slot_with_hash (heapvar_for_stmt, h, h->hash, INSERT);
347   *(struct tree_map **) loc = h;
348 }
349
350 /* Return a new variable info structure consisting for a variable
351    named NAME, and using constraint graph node NODE.  */
352
353 static varinfo_t
354 new_var_info (tree t, unsigned int id, const char *name)
355 {
356   varinfo_t ret = (varinfo_t) pool_alloc (variable_info_pool);
357   tree var;
358
359   ret->id = id;
360   ret->name = name;
361   ret->decl = t;
362   ret->is_artificial_var = false;
363   ret->is_heap_var = false;
364   ret->is_special_var = false;
365   ret->is_unknown_size_var = false;
366   ret->has_union = false;
367   var = t;
368   if (TREE_CODE (var) == SSA_NAME)
369     var = SSA_NAME_VAR (var);
370   ret->no_tbaa_pruning = (DECL_P (var)
371                           && POINTER_TYPE_P (TREE_TYPE (var))
372                           && DECL_NO_TBAA_P (var));
373   ret->solution = BITMAP_ALLOC (&pta_obstack);
374   ret->oldsolution = BITMAP_ALLOC (&oldpta_obstack);
375   ret->next = NULL;
376   ret->collapsed_to = NULL;
377   return ret;
378 }
379
380 typedef enum {SCALAR, DEREF, ADDRESSOF} constraint_expr_type;
381
382 /* An expression that appears in a constraint.  */
383
384 struct constraint_expr
385 {
386   /* Constraint type.  */
387   constraint_expr_type type;
388
389   /* Variable we are referring to in the constraint.  */
390   unsigned int var;
391
392   /* Offset, in bits, of this constraint from the beginning of
393      variables it ends up referring to.
394
395      IOW, in a deref constraint, we would deref, get the result set,
396      then add OFFSET to each member.   */
397   unsigned HOST_WIDE_INT offset;
398 };
399
400 typedef struct constraint_expr ce_s;
401 DEF_VEC_O(ce_s);
402 DEF_VEC_ALLOC_O(ce_s, heap);
403 static void get_constraint_for (tree, VEC(ce_s, heap) **);
404 static void do_deref (VEC (ce_s, heap) **);
405
406 /* Our set constraints are made up of two constraint expressions, one
407    LHS, and one RHS.
408
409    As described in the introduction, our set constraints each represent an
410    operation between set valued variables.
411 */
412 struct constraint
413 {
414   struct constraint_expr lhs;
415   struct constraint_expr rhs;
416 };
417
418 /* List of constraints that we use to build the constraint graph from.  */
419
420 static VEC(constraint_t,heap) *constraints;
421 static alloc_pool constraint_pool;
422
423
424 DEF_VEC_I(int);
425 DEF_VEC_ALLOC_I(int, heap);
426
427 /* The constraint graph is represented as an array of bitmaps
428    containing successor nodes.  */
429
430 struct constraint_graph
431 {
432   /* Size of this graph, which may be different than the number of
433      nodes in the variable map.  */
434   unsigned int size;
435
436   /* Explicit successors of each node. */
437   bitmap *succs;
438
439   /* Implicit predecessors of each node (Used for variable
440      substitution). */
441   bitmap *implicit_preds;
442
443   /* Explicit predecessors of each node (Used for variable substitution).  */
444   bitmap *preds;
445
446   /* Indirect cycle representatives, or -1 if the node has no indirect
447      cycles.  */
448   int *indirect_cycles;
449
450   /* Representative node for a node.  rep[a] == a unless the node has
451      been unified. */
452   unsigned int *rep;
453
454   /* Equivalence class representative for a label.  This is used for
455      variable substitution.  */
456   int *eq_rep;
457
458   /* Pointer equivalence label for a node.  All nodes with the same
459      pointer equivalence label can be unified together at some point
460      (either during constraint optimization or after the constraint
461      graph is built).  */
462   unsigned int *pe;
463
464   /* Pointer equivalence representative for a label.  This is used to
465      handle nodes that are pointer equivalent but not location
466      equivalent.  We can unite these once the addressof constraints
467      are transformed into initial points-to sets.  */
468   int *pe_rep;
469
470   /* Pointer equivalence label for each node, used during variable
471      substitution.  */
472   unsigned int *pointer_label;
473
474   /* Location equivalence label for each node, used during location
475      equivalence finding.  */
476   unsigned int *loc_label;
477
478   /* Pointed-by set for each node, used during location equivalence
479      finding.  This is pointed-by rather than pointed-to, because it
480      is constructed using the predecessor graph.  */
481   bitmap *pointed_by;
482
483   /* Points to sets for pointer equivalence.  This is *not* the actual
484      points-to sets for nodes.  */
485   bitmap *points_to;
486
487   /* Bitmap of nodes where the bit is set if the node is a direct
488      node.  Used for variable substitution.  */
489   sbitmap direct_nodes;
490
491   /* Bitmap of nodes where the bit is set if the node is address
492      taken.  Used for variable substitution.  */
493   bitmap address_taken;
494
495   /* True if points_to bitmap for this node is stored in the hash
496      table.  */
497   sbitmap pt_used;
498
499   /* Number of incoming edges remaining to be processed by pointer
500      equivalence.
501      Used for variable substitution.  */
502   unsigned int *number_incoming;
503
504
505   /* Vector of complex constraints for each graph node.  Complex
506      constraints are those involving dereferences or offsets that are
507      not 0.  */
508   VEC(constraint_t,heap) **complex;
509 };
510
511 static constraint_graph_t graph;
512
513 /* During variable substitution and the offline version of indirect
514    cycle finding, we create nodes to represent dereferences and
515    address taken constraints.  These represent where these start and
516    end.  */
517 #define FIRST_REF_NODE (VEC_length (varinfo_t, varmap))
518 #define LAST_REF_NODE (FIRST_REF_NODE + (FIRST_REF_NODE - 1))
519
520 /* Return the representative node for NODE, if NODE has been unioned
521    with another NODE.
522    This function performs path compression along the way to finding
523    the representative.  */
524
525 static unsigned int
526 find (unsigned int node)
527 {
528   gcc_assert (node < graph->size);
529   if (graph->rep[node] != node)
530     return graph->rep[node] = find (graph->rep[node]);
531   return node;
532 }
533
534 /* Union the TO and FROM nodes to the TO nodes.
535    Note that at some point in the future, we may want to do
536    union-by-rank, in which case we are going to have to return the
537    node we unified to.  */
538
539 static bool
540 unite (unsigned int to, unsigned int from)
541 {
542   gcc_assert (to < graph->size && from < graph->size);
543   if (to != from && graph->rep[from] != to)
544     {
545       graph->rep[from] = to;
546       return true;
547     }
548   return false;
549 }
550
551 /* Create a new constraint consisting of LHS and RHS expressions.  */
552
553 static constraint_t
554 new_constraint (const struct constraint_expr lhs,
555                 const struct constraint_expr rhs)
556 {
557   constraint_t ret = (constraint_t) pool_alloc (constraint_pool);
558   ret->lhs = lhs;
559   ret->rhs = rhs;
560   return ret;
561 }
562
563 /* Print out constraint C to FILE.  */
564
565 void
566 dump_constraint (FILE *file, constraint_t c)
567 {
568   if (c->lhs.type == ADDRESSOF)
569     fprintf (file, "&");
570   else if (c->lhs.type == DEREF)
571     fprintf (file, "*");
572   fprintf (file, "%s", get_varinfo_fc (c->lhs.var)->name);
573   if (c->lhs.offset != 0)
574     fprintf (file, " + " HOST_WIDE_INT_PRINT_DEC, c->lhs.offset);
575   fprintf (file, " = ");
576   if (c->rhs.type == ADDRESSOF)
577     fprintf (file, "&");
578   else if (c->rhs.type == DEREF)
579     fprintf (file, "*");
580   fprintf (file, "%s", get_varinfo_fc (c->rhs.var)->name);
581   if (c->rhs.offset != 0)
582     fprintf (file, " + " HOST_WIDE_INT_PRINT_DEC, c->rhs.offset);
583   fprintf (file, "\n");
584 }
585
586 /* Print out constraint C to stderr.  */
587
588 void
589 debug_constraint (constraint_t c)
590 {
591   dump_constraint (stderr, c);
592 }
593
594 /* Print out all constraints to FILE */
595
596 void
597 dump_constraints (FILE *file)
598 {
599   int i;
600   constraint_t c;
601   for (i = 0; VEC_iterate (constraint_t, constraints, i, c); i++)
602     dump_constraint (file, c);
603 }
604
605 /* Print out all constraints to stderr.  */
606
607 void
608 debug_constraints (void)
609 {
610   dump_constraints (stderr);
611 }
612
613 /* SOLVER FUNCTIONS
614
615    The solver is a simple worklist solver, that works on the following
616    algorithm:
617
618    sbitmap changed_nodes = all zeroes;
619    changed_count = 0;
620    For each node that is not already collapsed:
621        changed_count++;
622        set bit in changed nodes
623
624    while (changed_count > 0)
625    {
626      compute topological ordering for constraint graph
627
628      find and collapse cycles in the constraint graph (updating
629      changed if necessary)
630
631      for each node (n) in the graph in topological order:
632        changed_count--;
633
634        Process each complex constraint associated with the node,
635        updating changed if necessary.
636
637        For each outgoing edge from n, propagate the solution from n to
638        the destination of the edge, updating changed as necessary.
639
640    }  */
641
642 /* Return true if two constraint expressions A and B are equal.  */
643
644 static bool
645 constraint_expr_equal (struct constraint_expr a, struct constraint_expr b)
646 {
647   return a.type == b.type && a.var == b.var && a.offset == b.offset;
648 }
649
650 /* Return true if constraint expression A is less than constraint expression
651    B.  This is just arbitrary, but consistent, in order to give them an
652    ordering.  */
653
654 static bool
655 constraint_expr_less (struct constraint_expr a, struct constraint_expr b)
656 {
657   if (a.type == b.type)
658     {
659       if (a.var == b.var)
660         return a.offset < b.offset;
661       else
662         return a.var < b.var;
663     }
664   else
665     return a.type < b.type;
666 }
667
668 /* Return true if constraint A is less than constraint B.  This is just
669    arbitrary, but consistent, in order to give them an ordering.  */
670
671 static bool
672 constraint_less (const constraint_t a, const constraint_t b)
673 {
674   if (constraint_expr_less (a->lhs, b->lhs))
675     return true;
676   else if (constraint_expr_less (b->lhs, a->lhs))
677     return false;
678   else
679     return constraint_expr_less (a->rhs, b->rhs);
680 }
681
682 /* Return true if two constraints A and B are equal.  */
683
684 static bool
685 constraint_equal (struct constraint a, struct constraint b)
686 {
687   return constraint_expr_equal (a.lhs, b.lhs)
688     && constraint_expr_equal (a.rhs, b.rhs);
689 }
690
691
692 /* Find a constraint LOOKFOR in the sorted constraint vector VEC */
693
694 static constraint_t
695 constraint_vec_find (VEC(constraint_t,heap) *vec,
696                      struct constraint lookfor)
697 {
698   unsigned int place;
699   constraint_t found;
700
701   if (vec == NULL)
702     return NULL;
703
704   place = VEC_lower_bound (constraint_t, vec, &lookfor, constraint_less);
705   if (place >= VEC_length (constraint_t, vec))
706     return NULL;
707   found = VEC_index (constraint_t, vec, place);
708   if (!constraint_equal (*found, lookfor))
709     return NULL;
710   return found;
711 }
712
713 /* Union two constraint vectors, TO and FROM.  Put the result in TO.  */
714
715 static void
716 constraint_set_union (VEC(constraint_t,heap) **to,
717                       VEC(constraint_t,heap) **from)
718 {
719   int i;
720   constraint_t c;
721
722   for (i = 0; VEC_iterate (constraint_t, *from, i, c); i++)
723     {
724       if (constraint_vec_find (*to, *c) == NULL)
725         {
726           unsigned int place = VEC_lower_bound (constraint_t, *to, c,
727                                                 constraint_less);
728           VEC_safe_insert (constraint_t, heap, *to, place, c);
729         }
730     }
731 }
732
733 /* Take a solution set SET, add OFFSET to each member of the set, and
734    overwrite SET with the result when done.  */
735
736 static void
737 solution_set_add (bitmap set, unsigned HOST_WIDE_INT offset)
738 {
739   bitmap result = BITMAP_ALLOC (&iteration_obstack);
740   unsigned int i;
741   bitmap_iterator bi;
742
743   EXECUTE_IF_SET_IN_BITMAP (set, 0, i, bi)
744     {
745       /* If this is a properly sized variable, only add offset if it's
746          less than end.  Otherwise, it is globbed to a single
747          variable.  */
748
749       if ((get_varinfo (i)->offset + offset) < get_varinfo (i)->fullsize)
750         {
751           unsigned HOST_WIDE_INT fieldoffset = get_varinfo (i)->offset + offset;
752           varinfo_t v = first_vi_for_offset (get_varinfo (i), fieldoffset);
753           if (!v)
754             continue;
755           bitmap_set_bit (result, v->id);
756         }
757       else if (get_varinfo (i)->is_artificial_var
758                || get_varinfo (i)->has_union
759                || get_varinfo (i)->is_unknown_size_var)
760         {
761           bitmap_set_bit (result, i);
762         }
763     }
764
765   bitmap_copy (set, result);
766   BITMAP_FREE (result);
767 }
768
769 /* Union solution sets TO and FROM, and add INC to each member of FROM in the
770    process.  */
771
772 static bool
773 set_union_with_increment  (bitmap to, bitmap from, unsigned HOST_WIDE_INT inc)
774 {
775   if (inc == 0)
776     return bitmap_ior_into (to, from);
777   else
778     {
779       bitmap tmp;
780       bool res;
781
782       tmp = BITMAP_ALLOC (&iteration_obstack);
783       bitmap_copy (tmp, from);
784       solution_set_add (tmp, inc);
785       res = bitmap_ior_into (to, tmp);
786       BITMAP_FREE (tmp);
787       return res;
788     }
789 }
790
791 /* Insert constraint C into the list of complex constraints for graph
792    node VAR.  */
793
794 static void
795 insert_into_complex (constraint_graph_t graph,
796                      unsigned int var, constraint_t c)
797 {
798   VEC (constraint_t, heap) *complex = graph->complex[var];
799   unsigned int place = VEC_lower_bound (constraint_t, complex, c,
800                                         constraint_less);
801
802   /* Only insert constraints that do not already exist.  */
803   if (place >= VEC_length (constraint_t, complex)
804       || !constraint_equal (*c, *VEC_index (constraint_t, complex, place)))
805     VEC_safe_insert (constraint_t, heap, graph->complex[var], place, c);
806 }
807
808
809 /* Condense two variable nodes into a single variable node, by moving
810    all associated info from SRC to TO.  */
811
812 static void
813 merge_node_constraints (constraint_graph_t graph, unsigned int to,
814                         unsigned int from)
815 {
816   unsigned int i;
817   constraint_t c;
818
819   gcc_assert (find (from) == to);
820
821   /* Move all complex constraints from src node into to node  */
822   for (i = 0; VEC_iterate (constraint_t, graph->complex[from], i, c); i++)
823     {
824       /* In complex constraints for node src, we may have either
825          a = *src, and *src = a, or an offseted constraint which are
826          always added to the rhs node's constraints.  */
827
828       if (c->rhs.type == DEREF)
829         c->rhs.var = to;
830       else if (c->lhs.type == DEREF)
831         c->lhs.var = to;
832       else
833         c->rhs.var = to;
834     }
835   constraint_set_union (&graph->complex[to], &graph->complex[from]);
836   VEC_free (constraint_t, heap, graph->complex[from]);
837   graph->complex[from] = NULL;
838 }
839
840
841 /* Remove edges involving NODE from GRAPH.  */
842
843 static void
844 clear_edges_for_node (constraint_graph_t graph, unsigned int node)
845 {
846   if (graph->succs[node])
847     BITMAP_FREE (graph->succs[node]);
848 }
849
850 /* Merge GRAPH nodes FROM and TO into node TO.  */
851
852 static void
853 merge_graph_nodes (constraint_graph_t graph, unsigned int to,
854                    unsigned int from)
855 {
856   if (graph->indirect_cycles[from] != -1)
857     {
858       /* If we have indirect cycles with the from node, and we have
859          none on the to node, the to node has indirect cycles from the
860          from node now that they are unified.
861          If indirect cycles exist on both, unify the nodes that they
862          are in a cycle with, since we know they are in a cycle with
863          each other.  */
864       if (graph->indirect_cycles[to] == -1)
865         graph->indirect_cycles[to] = graph->indirect_cycles[from];
866     }
867
868   /* Merge all the successor edges.  */
869   if (graph->succs[from])
870     {
871       if (!graph->succs[to])
872         graph->succs[to] = BITMAP_ALLOC (&pta_obstack);
873       bitmap_ior_into (graph->succs[to],
874                        graph->succs[from]);
875     }
876
877   clear_edges_for_node (graph, from);
878 }
879
880
881 /* Add an indirect graph edge to GRAPH, going from TO to FROM if
882    it doesn't exist in the graph already.  */
883
884 static void
885 add_implicit_graph_edge (constraint_graph_t graph, unsigned int to,
886                          unsigned int from)
887 {
888   if (to == from)
889     return;
890
891   if (!graph->implicit_preds[to])
892     graph->implicit_preds[to] = BITMAP_ALLOC (&predbitmap_obstack);
893
894   if (!bitmap_bit_p (graph->implicit_preds[to], from))
895     {
896       stats.num_implicit_edges++;
897       bitmap_set_bit (graph->implicit_preds[to], from);
898     }
899 }
900
901 /* Add a predecessor graph edge to GRAPH, going from TO to FROM if
902    it doesn't exist in the graph already.
903    Return false if the edge already existed, true otherwise.  */
904
905 static void
906 add_pred_graph_edge (constraint_graph_t graph, unsigned int to,
907                      unsigned int from)
908 {
909   if (!graph->preds[to])
910     graph->preds[to] = BITMAP_ALLOC (&predbitmap_obstack);
911   if (!bitmap_bit_p (graph->preds[to], from))
912     bitmap_set_bit (graph->preds[to], from);
913 }
914
915 /* Add a graph edge to GRAPH, going from FROM to TO if
916    it doesn't exist in the graph already.
917    Return false if the edge already existed, true otherwise.  */
918
919 static bool
920 add_graph_edge (constraint_graph_t graph, unsigned int to,
921                 unsigned int from)
922 {
923   if (to == from)
924     {
925       return false;
926     }
927   else
928     {
929       bool r = false;
930
931       if (!graph->succs[from])
932         graph->succs[from] = BITMAP_ALLOC (&pta_obstack);
933       if (!bitmap_bit_p (graph->succs[from], to))
934         {
935           r = true;
936           if (to < FIRST_REF_NODE && from < FIRST_REF_NODE)
937             stats.num_edges++;
938           bitmap_set_bit (graph->succs[from], to);
939         }
940       return r;
941     }
942 }
943
944
945 /* Return true if {DEST.SRC} is an existing graph edge in GRAPH.  */
946
947 static bool
948 valid_graph_edge (constraint_graph_t graph, unsigned int src,
949                   unsigned int dest)
950 {
951   return (graph->succs[dest]
952           && bitmap_bit_p (graph->succs[dest], src));
953 }
954
955 /* Initialize the constraint graph structure to contain SIZE nodes.  */
956
957 static void
958 init_graph (unsigned int size)
959 {
960   unsigned int j;
961
962   graph = XCNEW (struct constraint_graph);
963   graph->size = size;
964   graph->succs = XCNEWVEC (bitmap, graph->size);
965   graph->indirect_cycles = XNEWVEC (int, graph->size);
966   graph->rep = XNEWVEC (unsigned int, graph->size);
967   graph->complex = XCNEWVEC (VEC(constraint_t, heap) *, size);
968   graph->pe = XCNEWVEC (unsigned int, graph->size);
969   graph->pe_rep = XNEWVEC (int, graph->size);
970
971   for (j = 0; j < graph->size; j++)
972     {
973       graph->rep[j] = j;
974       graph->pe_rep[j] = -1;
975       graph->indirect_cycles[j] = -1;
976     }
977 }
978
979 /* Build the constraint graph, adding only predecessor edges right now.  */
980
981 static void
982 build_pred_graph (void)
983 {
984   int i;
985   constraint_t c;
986   unsigned int j;
987
988   graph->implicit_preds = XCNEWVEC (bitmap, graph->size);
989   graph->preds = XCNEWVEC (bitmap, graph->size);
990   graph->pointer_label = XCNEWVEC (unsigned int, graph->size);
991   graph->loc_label = XCNEWVEC (unsigned int, graph->size);
992   graph->pointed_by = XCNEWVEC (bitmap, graph->size);
993   graph->points_to = XCNEWVEC (bitmap, graph->size);
994   graph->eq_rep = XNEWVEC (int, graph->size);
995   graph->direct_nodes = sbitmap_alloc (graph->size);
996   graph->pt_used = sbitmap_alloc (graph->size);
997   graph->address_taken = BITMAP_ALLOC (&predbitmap_obstack);
998   graph->number_incoming = XCNEWVEC (unsigned int, graph->size);
999   sbitmap_zero (graph->direct_nodes);
1000   sbitmap_zero (graph->pt_used);
1001
1002   for (j = 0; j < FIRST_REF_NODE; j++)
1003     {
1004       if (!get_varinfo (j)->is_special_var)
1005         SET_BIT (graph->direct_nodes, j);
1006     }
1007
1008   for (j = 0; j < graph->size; j++)
1009     graph->eq_rep[j] = -1;
1010
1011   for (j = 0; j < VEC_length (varinfo_t, varmap); j++)
1012     graph->indirect_cycles[j] = -1;
1013
1014   for (i = 0; VEC_iterate (constraint_t, constraints, i, c); i++)
1015     {
1016       struct constraint_expr lhs = c->lhs;
1017       struct constraint_expr rhs = c->rhs;
1018       unsigned int lhsvar = get_varinfo_fc (lhs.var)->id;
1019       unsigned int rhsvar = get_varinfo_fc (rhs.var)->id;
1020
1021       if (lhs.type == DEREF)
1022         {
1023           /* *x = y.  */
1024           if (rhs.offset == 0 && lhs.offset == 0 && rhs.type == SCALAR)
1025             add_pred_graph_edge (graph, FIRST_REF_NODE + lhsvar, rhsvar);
1026         }
1027       else if (rhs.type == DEREF)
1028         {
1029           /* x = *y */
1030           if (rhs.offset == 0 && lhs.offset == 0 && lhs.type == SCALAR)
1031             add_pred_graph_edge (graph, lhsvar, FIRST_REF_NODE + rhsvar);
1032           else
1033             RESET_BIT (graph->direct_nodes, lhsvar);
1034         }
1035       else if (rhs.type == ADDRESSOF)
1036         {
1037           /* x = &y */
1038           if (graph->points_to[lhsvar] == NULL)
1039             graph->points_to[lhsvar] = BITMAP_ALLOC (&predbitmap_obstack);
1040           bitmap_set_bit (graph->points_to[lhsvar], rhsvar);
1041
1042           if (graph->pointed_by[rhsvar] == NULL)
1043             graph->pointed_by[rhsvar] = BITMAP_ALLOC (&predbitmap_obstack);
1044           bitmap_set_bit (graph->pointed_by[rhsvar], lhsvar);
1045
1046           /* Implicitly, *x = y */
1047           add_implicit_graph_edge (graph, FIRST_REF_NODE + lhsvar, rhsvar);
1048
1049           RESET_BIT (graph->direct_nodes, rhsvar);
1050           bitmap_set_bit (graph->address_taken, rhsvar);
1051         }
1052       else if (lhsvar > anything_id
1053                && lhsvar != rhsvar && lhs.offset == 0 && rhs.offset == 0)
1054         {
1055           /* x = y */
1056           add_pred_graph_edge (graph, lhsvar, rhsvar);
1057           /* Implicitly, *x = *y */
1058           add_implicit_graph_edge (graph, FIRST_REF_NODE + lhsvar,
1059                                    FIRST_REF_NODE + rhsvar);
1060         }
1061       else if (lhs.offset != 0 || rhs.offset != 0)
1062         {
1063           if (rhs.offset != 0)
1064             RESET_BIT (graph->direct_nodes, lhs.var);
1065           else if (lhs.offset != 0)
1066             RESET_BIT (graph->direct_nodes, rhs.var);
1067         }
1068     }
1069 }
1070
1071 /* Build the constraint graph, adding successor edges.  */
1072
1073 static void
1074 build_succ_graph (void)
1075 {
1076   int i;
1077   constraint_t c;
1078
1079   for (i = 0; VEC_iterate (constraint_t, constraints, i, c); i++)
1080     {
1081       struct constraint_expr lhs;
1082       struct constraint_expr rhs;
1083       unsigned int lhsvar;
1084       unsigned int rhsvar;
1085
1086       if (!c)
1087         continue;
1088
1089       lhs = c->lhs;
1090       rhs = c->rhs;
1091       lhsvar = find (get_varinfo_fc (lhs.var)->id);
1092       rhsvar = find (get_varinfo_fc (rhs.var)->id);
1093
1094       if (lhs.type == DEREF)
1095         {
1096           if (rhs.offset == 0 && lhs.offset == 0 && rhs.type == SCALAR)
1097             add_graph_edge (graph, FIRST_REF_NODE + lhsvar, rhsvar);
1098         }
1099       else if (rhs.type == DEREF)
1100         {
1101           if (rhs.offset == 0 && lhs.offset == 0 && lhs.type == SCALAR)
1102             add_graph_edge (graph, lhsvar, FIRST_REF_NODE + rhsvar);
1103         }
1104       else if (rhs.type == ADDRESSOF)
1105         {
1106           /* x = &y */
1107           gcc_assert (find (get_varinfo_fc (rhs.var)->id)
1108                       == get_varinfo_fc (rhs.var)->id);
1109           bitmap_set_bit (get_varinfo (lhsvar)->solution, rhsvar);
1110         }
1111       else if (lhsvar > anything_id
1112                && lhsvar != rhsvar && lhs.offset == 0 && rhs.offset == 0)
1113         {
1114           add_graph_edge (graph, lhsvar, rhsvar);
1115         }
1116     }
1117 }
1118
1119
1120 /* Changed variables on the last iteration.  */
1121 static unsigned int changed_count;
1122 static sbitmap changed;
1123
1124 DEF_VEC_I(unsigned);
1125 DEF_VEC_ALLOC_I(unsigned,heap);
1126
1127
1128 /* Strongly Connected Component visitation info.  */
1129
1130 struct scc_info
1131 {
1132   sbitmap visited;
1133   sbitmap deleted;
1134   unsigned int *dfs;
1135   unsigned int *node_mapping;
1136   int current_index;
1137   VEC(unsigned,heap) *scc_stack;
1138 };
1139
1140
1141 /* Recursive routine to find strongly connected components in GRAPH.
1142    SI is the SCC info to store the information in, and N is the id of current
1143    graph node we are processing.
1144
1145    This is Tarjan's strongly connected component finding algorithm, as
1146    modified by Nuutila to keep only non-root nodes on the stack.
1147    The algorithm can be found in "On finding the strongly connected
1148    connected components in a directed graph" by Esko Nuutila and Eljas
1149    Soisalon-Soininen, in Information Processing Letters volume 49,
1150    number 1, pages 9-14.  */
1151
1152 static void
1153 scc_visit (constraint_graph_t graph, struct scc_info *si, unsigned int n)
1154 {
1155   unsigned int i;
1156   bitmap_iterator bi;
1157   unsigned int my_dfs;
1158
1159   SET_BIT (si->visited, n);
1160   si->dfs[n] = si->current_index ++;
1161   my_dfs = si->dfs[n];
1162
1163   /* Visit all the successors.  */
1164   EXECUTE_IF_IN_NONNULL_BITMAP (graph->succs[n], 0, i, bi)
1165     {
1166       unsigned int w;
1167
1168       if (i > LAST_REF_NODE)
1169         break;
1170
1171       w = find (i);
1172       if (TEST_BIT (si->deleted, w))
1173         continue;
1174
1175       if (!TEST_BIT (si->visited, w))
1176         scc_visit (graph, si, w);
1177       {
1178         unsigned int t = find (w);
1179         unsigned int nnode = find (n);
1180         gcc_assert (nnode == n);
1181
1182         if (si->dfs[t] < si->dfs[nnode])
1183           si->dfs[n] = si->dfs[t];
1184       }
1185     }
1186
1187   /* See if any components have been identified.  */
1188   if (si->dfs[n] == my_dfs)
1189     {
1190       if (VEC_length (unsigned, si->scc_stack) > 0
1191           && si->dfs[VEC_last (unsigned, si->scc_stack)] >= my_dfs)
1192         {
1193           bitmap scc = BITMAP_ALLOC (NULL);
1194           bool have_ref_node = n >= FIRST_REF_NODE;
1195           unsigned int lowest_node;
1196           bitmap_iterator bi;
1197
1198           bitmap_set_bit (scc, n);
1199
1200           while (VEC_length (unsigned, si->scc_stack) != 0
1201                  && si->dfs[VEC_last (unsigned, si->scc_stack)] >= my_dfs)
1202             {
1203               unsigned int w = VEC_pop (unsigned, si->scc_stack);
1204
1205               bitmap_set_bit (scc, w);
1206               if (w >= FIRST_REF_NODE)
1207                 have_ref_node = true;
1208             }
1209
1210           lowest_node = bitmap_first_set_bit (scc);
1211           gcc_assert (lowest_node < FIRST_REF_NODE);
1212
1213           /* Collapse the SCC nodes into a single node, and mark the
1214              indirect cycles.  */
1215           EXECUTE_IF_SET_IN_BITMAP (scc, 0, i, bi)
1216             {
1217               if (i < FIRST_REF_NODE)
1218                 {
1219                   if (unite (lowest_node, i))
1220                     unify_nodes (graph, lowest_node, i, false);
1221                 }
1222               else
1223                 {
1224                   unite (lowest_node, i);
1225                   graph->indirect_cycles[i - FIRST_REF_NODE] = lowest_node;
1226                 }
1227             }
1228         }
1229       SET_BIT (si->deleted, n);
1230     }
1231   else
1232     VEC_safe_push (unsigned, heap, si->scc_stack, n);
1233 }
1234
1235 /* Unify node FROM into node TO, updating the changed count if
1236    necessary when UPDATE_CHANGED is true.  */
1237
1238 static void
1239 unify_nodes (constraint_graph_t graph, unsigned int to, unsigned int from,
1240              bool update_changed)
1241 {
1242
1243   gcc_assert (to != from && find (to) == to);
1244   if (dump_file && (dump_flags & TDF_DETAILS))
1245     fprintf (dump_file, "Unifying %s to %s\n",
1246              get_varinfo (from)->name,
1247              get_varinfo (to)->name);
1248
1249   if (update_changed)
1250     stats.unified_vars_dynamic++;
1251   else
1252     stats.unified_vars_static++;
1253
1254   merge_graph_nodes (graph, to, from);
1255   merge_node_constraints (graph, to, from);
1256
1257   if (get_varinfo (from)->no_tbaa_pruning)
1258     get_varinfo (to)->no_tbaa_pruning = true;
1259
1260   /* Mark TO as changed if FROM was changed. If TO was already marked
1261      as changed, decrease the changed count.  */
1262
1263   if (update_changed && TEST_BIT (changed, from))
1264     {
1265       RESET_BIT (changed, from);
1266       if (!TEST_BIT (changed, to))
1267         SET_BIT (changed, to);
1268       else
1269         {
1270           gcc_assert (changed_count > 0);
1271           changed_count--;
1272         }
1273     }
1274   if (get_varinfo (from)->solution)
1275     {
1276       /* If the solution changes because of the merging, we need to mark
1277          the variable as changed.  */
1278       if (bitmap_ior_into (get_varinfo (to)->solution,
1279                            get_varinfo (from)->solution))
1280         {
1281           if (update_changed && !TEST_BIT (changed, to))
1282             {
1283               SET_BIT (changed, to);
1284               changed_count++;
1285             }
1286         }
1287       
1288       BITMAP_FREE (get_varinfo (from)->solution);
1289       BITMAP_FREE (get_varinfo (from)->oldsolution);
1290       
1291       if (stats.iterations > 0)
1292         {
1293           BITMAP_FREE (get_varinfo (to)->oldsolution);
1294           get_varinfo (to)->oldsolution = BITMAP_ALLOC (&oldpta_obstack);
1295         }
1296     }
1297   if (valid_graph_edge (graph, to, to))
1298     {
1299       if (graph->succs[to])
1300         bitmap_clear_bit (graph->succs[to], to);
1301     }
1302 }
1303
1304 /* Information needed to compute the topological ordering of a graph.  */
1305
1306 struct topo_info
1307 {
1308   /* sbitmap of visited nodes.  */
1309   sbitmap visited;
1310   /* Array that stores the topological order of the graph, *in
1311      reverse*.  */
1312   VEC(unsigned,heap) *topo_order;
1313 };
1314
1315
1316 /* Initialize and return a topological info structure.  */
1317
1318 static struct topo_info *
1319 init_topo_info (void)
1320 {
1321   size_t size = graph->size;
1322   struct topo_info *ti = XNEW (struct topo_info);
1323   ti->visited = sbitmap_alloc (size);
1324   sbitmap_zero (ti->visited);
1325   ti->topo_order = VEC_alloc (unsigned, heap, 1);
1326   return ti;
1327 }
1328
1329
1330 /* Free the topological sort info pointed to by TI.  */
1331
1332 static void
1333 free_topo_info (struct topo_info *ti)
1334 {
1335   sbitmap_free (ti->visited);
1336   VEC_free (unsigned, heap, ti->topo_order);
1337   free (ti);
1338 }
1339
1340 /* Visit the graph in topological order, and store the order in the
1341    topo_info structure.  */
1342
1343 static void
1344 topo_visit (constraint_graph_t graph, struct topo_info *ti,
1345             unsigned int n)
1346 {
1347   bitmap_iterator bi;
1348   unsigned int j;
1349
1350   SET_BIT (ti->visited, n);
1351
1352   if (graph->succs[n])
1353     EXECUTE_IF_SET_IN_BITMAP (graph->succs[n], 0, j, bi)
1354       {
1355         if (!TEST_BIT (ti->visited, j))
1356           topo_visit (graph, ti, j);
1357       }
1358
1359   VEC_safe_push (unsigned, heap, ti->topo_order, n);
1360 }
1361
1362 /* Return true if variable N + OFFSET is a legal field of N.  */
1363
1364 static bool
1365 type_safe (unsigned int n, unsigned HOST_WIDE_INT *offset)
1366 {
1367   varinfo_t ninfo = get_varinfo (n);
1368
1369   /* For things we've globbed to single variables, any offset into the
1370      variable acts like the entire variable, so that it becomes offset
1371      0.  */
1372   if (ninfo->is_special_var
1373       || ninfo->is_artificial_var
1374       || ninfo->is_unknown_size_var)
1375     {
1376       *offset = 0;
1377       return true;
1378     }
1379   return (get_varinfo (n)->offset + *offset) < get_varinfo (n)->fullsize;
1380 }
1381
1382 /* Process a constraint C that represents x = *y, using DELTA as the
1383    starting solution.  */
1384
1385 static void
1386 do_sd_constraint (constraint_graph_t graph, constraint_t c,
1387                   bitmap delta)
1388 {
1389   unsigned int lhs = c->lhs.var;
1390   bool flag = false;
1391   bitmap sol = get_varinfo (lhs)->solution;
1392   unsigned int j;
1393   bitmap_iterator bi;
1394
1395  if (bitmap_bit_p (delta, anything_id))
1396    {
1397      flag = !bitmap_bit_p (sol, anything_id);
1398      if (flag)
1399        bitmap_set_bit (sol, anything_id);
1400      goto done;
1401    }
1402   /* For each variable j in delta (Sol(y)), add
1403      an edge in the graph from j to x, and union Sol(j) into Sol(x).  */
1404   EXECUTE_IF_SET_IN_BITMAP (delta, 0, j, bi)
1405     {
1406       unsigned HOST_WIDE_INT roffset = c->rhs.offset;
1407       if (type_safe (j, &roffset))
1408         {
1409           varinfo_t v;
1410           unsigned HOST_WIDE_INT fieldoffset = get_varinfo (j)->offset + roffset;
1411           unsigned int t;
1412
1413           v = first_vi_for_offset (get_varinfo (j), fieldoffset);
1414           if (!v)
1415             continue;
1416           t = find (v->id);
1417
1418           /* Adding edges from the special vars is pointless.
1419              They don't have sets that can change.  */
1420           if (get_varinfo (t) ->is_special_var)
1421             flag |= bitmap_ior_into (sol, get_varinfo (t)->solution);
1422           else if (add_graph_edge (graph, lhs, t))
1423             flag |= bitmap_ior_into (sol, get_varinfo (t)->solution);
1424         }
1425     }
1426
1427 done:
1428   /* If the LHS solution changed, mark the var as changed.  */
1429   if (flag)
1430     {
1431       get_varinfo (lhs)->solution = sol;
1432       if (!TEST_BIT (changed, lhs))
1433         {
1434           SET_BIT (changed, lhs);
1435           changed_count++;
1436         }
1437     }
1438 }
1439
1440 /* Process a constraint C that represents *x = y.  */
1441
1442 static void
1443 do_ds_constraint (constraint_t c, bitmap delta)
1444 {
1445   unsigned int rhs = c->rhs.var;
1446   bitmap sol = get_varinfo (rhs)->solution;
1447   unsigned int j;
1448   bitmap_iterator bi;
1449
1450  if (bitmap_bit_p (sol, anything_id))
1451    {
1452      EXECUTE_IF_SET_IN_BITMAP (delta, 0, j, bi)
1453        {
1454          varinfo_t jvi = get_varinfo (j);
1455          unsigned int t;
1456          unsigned int loff = c->lhs.offset;
1457          unsigned HOST_WIDE_INT fieldoffset = jvi->offset + loff;
1458          varinfo_t v;
1459
1460          v = first_vi_for_offset (get_varinfo (j), fieldoffset);
1461          if (!v)
1462            continue;
1463          t = find (v->id);
1464
1465          if (!bitmap_bit_p (get_varinfo (t)->solution, anything_id))
1466            {
1467              bitmap_set_bit (get_varinfo (t)->solution, anything_id);
1468              if (!TEST_BIT (changed, t))
1469                {
1470                  SET_BIT (changed, t);
1471                  changed_count++;
1472                }
1473            }
1474        }
1475      return;
1476    }
1477
1478   /* For each member j of delta (Sol(x)), add an edge from y to j and
1479      union Sol(y) into Sol(j) */
1480   EXECUTE_IF_SET_IN_BITMAP (delta, 0, j, bi)
1481     {
1482       unsigned HOST_WIDE_INT loff = c->lhs.offset;
1483       if (type_safe (j, &loff) && !(get_varinfo (j)->is_special_var))
1484         {
1485           varinfo_t v;
1486           unsigned int t;
1487           unsigned HOST_WIDE_INT fieldoffset = get_varinfo (j)->offset + loff;
1488           bitmap tmp;
1489
1490           v = first_vi_for_offset (get_varinfo (j), fieldoffset);
1491           if (!v)
1492             continue;
1493           t = find (v->id);
1494           tmp = get_varinfo (t)->solution;
1495
1496           if (set_union_with_increment (tmp, sol, 0))
1497             {
1498               get_varinfo (t)->solution = tmp;
1499               if (t == rhs)
1500                 sol = get_varinfo (rhs)->solution;
1501               if (!TEST_BIT (changed, t))
1502                 {
1503                   SET_BIT (changed, t);
1504                   changed_count++;
1505                 }
1506             }
1507         }
1508     }
1509 }
1510
1511 /* Handle a non-simple (simple meaning requires no iteration),
1512    constraint (IE *x = &y, x = *y, *x = y, and x = y with offsets involved).  */
1513
1514 static void
1515 do_complex_constraint (constraint_graph_t graph, constraint_t c, bitmap delta)
1516 {
1517   if (c->lhs.type == DEREF)
1518     {
1519       if (c->rhs.type == ADDRESSOF)
1520         {
1521           gcc_unreachable();
1522         }
1523       else
1524         {
1525           /* *x = y */
1526           do_ds_constraint (c, delta);
1527         }
1528     }
1529   else if (c->rhs.type == DEREF)
1530     {
1531       /* x = *y */
1532       if (!(get_varinfo (c->lhs.var)->is_special_var))
1533         do_sd_constraint (graph, c, delta);
1534     }
1535   else
1536     {
1537       bitmap tmp;
1538       bitmap solution;
1539       bool flag = false;
1540
1541       gcc_assert (c->rhs.type == SCALAR && c->lhs.type == SCALAR);
1542       solution = get_varinfo (c->rhs.var)->solution;
1543       tmp = get_varinfo (c->lhs.var)->solution;
1544
1545       flag = set_union_with_increment (tmp, solution, c->rhs.offset);
1546
1547       if (flag)
1548         {
1549           get_varinfo (c->lhs.var)->solution = tmp;
1550           if (!TEST_BIT (changed, c->lhs.var))
1551             {
1552               SET_BIT (changed, c->lhs.var);
1553               changed_count++;
1554             }
1555         }
1556     }
1557 }
1558
1559 /* Initialize and return a new SCC info structure.  */
1560
1561 static struct scc_info *
1562 init_scc_info (size_t size)
1563 {
1564   struct scc_info *si = XNEW (struct scc_info);
1565   size_t i;
1566
1567   si->current_index = 0;
1568   si->visited = sbitmap_alloc (size);
1569   sbitmap_zero (si->visited);
1570   si->deleted = sbitmap_alloc (size);
1571   sbitmap_zero (si->deleted);
1572   si->node_mapping = XNEWVEC (unsigned int, size);
1573   si->dfs = XCNEWVEC (unsigned int, size);
1574
1575   for (i = 0; i < size; i++)
1576     si->node_mapping[i] = i;
1577
1578   si->scc_stack = VEC_alloc (unsigned, heap, 1);
1579   return si;
1580 }
1581
1582 /* Free an SCC info structure pointed to by SI */
1583
1584 static void
1585 free_scc_info (struct scc_info *si)
1586 {
1587   sbitmap_free (si->visited);
1588   sbitmap_free (si->deleted);
1589   free (si->node_mapping);
1590   free (si->dfs);
1591   VEC_free (unsigned, heap, si->scc_stack);
1592   free (si);
1593 }
1594
1595
1596 /* Find indirect cycles in GRAPH that occur, using strongly connected
1597    components, and note them in the indirect cycles map.
1598
1599    This technique comes from Ben Hardekopf and Calvin Lin,
1600    "It Pays to be Lazy: Fast and Accurate Pointer Analysis for Millions of
1601    Lines of Code", submitted to PLDI 2007.  */
1602
1603 static void
1604 find_indirect_cycles (constraint_graph_t graph)
1605 {
1606   unsigned int i;
1607   unsigned int size = graph->size;
1608   struct scc_info *si = init_scc_info (size);
1609
1610   for (i = 0; i < MIN (LAST_REF_NODE, size); i ++ )
1611     if (!TEST_BIT (si->visited, i) && find (i) == i)
1612       scc_visit (graph, si, i);
1613
1614   free_scc_info (si);
1615 }
1616
1617 /* Compute a topological ordering for GRAPH, and store the result in the
1618    topo_info structure TI.  */
1619
1620 static void
1621 compute_topo_order (constraint_graph_t graph,
1622                     struct topo_info *ti)
1623 {
1624   unsigned int i;
1625   unsigned int size = graph->size;
1626
1627   for (i = 0; i != size; ++i)
1628     if (!TEST_BIT (ti->visited, i) && find (i) == i)
1629       topo_visit (graph, ti, i);
1630 }
1631
1632 /* Structure used to for hash value numbering of pointer equivalence
1633    classes.  */
1634
1635 typedef struct equiv_class_label
1636 {
1637   unsigned int equivalence_class;
1638   bitmap labels;
1639   hashval_t hashcode;
1640 } *equiv_class_label_t;
1641 typedef const struct equiv_class_label *const_equiv_class_label_t;
1642
1643 /* A hashtable for mapping a bitmap of labels->pointer equivalence
1644    classes.  */
1645 static htab_t pointer_equiv_class_table;
1646
1647 /* A hashtable for mapping a bitmap of labels->location equivalence
1648    classes.  */
1649 static htab_t location_equiv_class_table;
1650
1651 /* Hash function for a equiv_class_label_t */
1652
1653 static hashval_t
1654 equiv_class_label_hash (const void *p)
1655 {
1656   const_equiv_class_label_t const ecl = (const_equiv_class_label_t) p;
1657   return ecl->hashcode;
1658 }
1659
1660 /* Equality function for two equiv_class_label_t's.  */
1661
1662 static int
1663 equiv_class_label_eq (const void *p1, const void *p2)
1664 {
1665   const_equiv_class_label_t const eql1 = (const_equiv_class_label_t) p1;
1666   const_equiv_class_label_t const eql2 = (const_equiv_class_label_t) p2;
1667   return bitmap_equal_p (eql1->labels, eql2->labels);
1668 }
1669
1670 /* Lookup a equivalence class in TABLE by the bitmap of LABELS it
1671    contains.  */
1672
1673 static unsigned int
1674 equiv_class_lookup (htab_t table, bitmap labels)
1675 {
1676   void **slot;
1677   struct equiv_class_label ecl;
1678
1679   ecl.labels = labels;
1680   ecl.hashcode = bitmap_hash (labels);
1681
1682   slot = htab_find_slot_with_hash (table, &ecl,
1683                                    ecl.hashcode, NO_INSERT);
1684   if (!slot)
1685     return 0;
1686   else
1687     return ((equiv_class_label_t) *slot)->equivalence_class;
1688 }
1689
1690
1691 /* Add an equivalence class named EQUIVALENCE_CLASS with labels LABELS
1692    to TABLE.  */
1693
1694 static void
1695 equiv_class_add (htab_t table, unsigned int equivalence_class,
1696                  bitmap labels)
1697 {
1698   void **slot;
1699   equiv_class_label_t ecl = XNEW (struct equiv_class_label);
1700
1701   ecl->labels = labels;
1702   ecl->equivalence_class = equivalence_class;
1703   ecl->hashcode = bitmap_hash (labels);
1704
1705   slot = htab_find_slot_with_hash (table, ecl,
1706                                    ecl->hashcode, INSERT);
1707   gcc_assert (!*slot);
1708   *slot = (void *) ecl;
1709 }
1710
1711 /* Perform offline variable substitution.
1712
1713    This is a worst case quadratic time way of identifying variables
1714    that must have equivalent points-to sets, including those caused by
1715    static cycles, and single entry subgraphs, in the constraint graph.
1716
1717    The technique is described in "Exploiting Pointer and Location
1718    Equivalence to Optimize Pointer Analysis. In the 14th International
1719    Static Analysis Symposium (SAS), August 2007."  It is known as the
1720    "HU" algorithm, and is equivalent to value numbering the collapsed
1721    constraint graph including evaluating unions.
1722
1723    The general method of finding equivalence classes is as follows:
1724    Add fake nodes (REF nodes) and edges for *a = b and a = *b constraints.
1725    Initialize all non-REF nodes to be direct nodes.
1726    For each constraint a = a U {b}, we set pts(a) = pts(a) u {fresh
1727    variable}
1728    For each constraint containing the dereference, we also do the same
1729    thing.
1730
1731    We then compute SCC's in the graph and unify nodes in the same SCC,
1732    including pts sets.
1733
1734    For each non-collapsed node x:
1735     Visit all unvisited explicit incoming edges.
1736     Ignoring all non-pointers, set pts(x) = Union of pts(a) for y
1737     where y->x.
1738     Lookup the equivalence class for pts(x).
1739      If we found one, equivalence_class(x) = found class.
1740      Otherwise, equivalence_class(x) = new class, and new_class is
1741     added to the lookup table.
1742
1743    All direct nodes with the same equivalence class can be replaced
1744    with a single representative node.
1745    All unlabeled nodes (label == 0) are not pointers and all edges
1746    involving them can be eliminated.
1747    We perform these optimizations during rewrite_constraints
1748
1749    In addition to pointer equivalence class finding, we also perform
1750    location equivalence class finding.  This is the set of variables
1751    that always appear together in points-to sets.  We use this to
1752    compress the size of the points-to sets.  */
1753
1754 /* Current maximum pointer equivalence class id.  */
1755 static int pointer_equiv_class;
1756
1757 /* Current maximum location equivalence class id.  */
1758 static int location_equiv_class;
1759
1760 /* Recursive routine to find strongly connected components in GRAPH,
1761    and label it's nodes with DFS numbers.  */
1762
1763 static void
1764 condense_visit (constraint_graph_t graph, struct scc_info *si, unsigned int n)
1765 {
1766   unsigned int i;
1767   bitmap_iterator bi;
1768   unsigned int my_dfs;
1769
1770   gcc_assert (si->node_mapping[n] == n);
1771   SET_BIT (si->visited, n);
1772   si->dfs[n] = si->current_index ++;
1773   my_dfs = si->dfs[n];
1774
1775   /* Visit all the successors.  */
1776   EXECUTE_IF_IN_NONNULL_BITMAP (graph->preds[n], 0, i, bi)
1777     {
1778       unsigned int w = si->node_mapping[i];
1779
1780       if (TEST_BIT (si->deleted, w))
1781         continue;
1782
1783       if (!TEST_BIT (si->visited, w))
1784         condense_visit (graph, si, w);
1785       {
1786         unsigned int t = si->node_mapping[w];
1787         unsigned int nnode = si->node_mapping[n];
1788         gcc_assert (nnode == n);
1789
1790         if (si->dfs[t] < si->dfs[nnode])
1791           si->dfs[n] = si->dfs[t];
1792       }
1793     }
1794
1795   /* Visit all the implicit predecessors.  */
1796   EXECUTE_IF_IN_NONNULL_BITMAP (graph->implicit_preds[n], 0, i, bi)
1797     {
1798       unsigned int w = si->node_mapping[i];
1799
1800       if (TEST_BIT (si->deleted, w))
1801         continue;
1802
1803       if (!TEST_BIT (si->visited, w))
1804         condense_visit (graph, si, w);
1805       {
1806         unsigned int t = si->node_mapping[w];
1807         unsigned int nnode = si->node_mapping[n];
1808         gcc_assert (nnode == n);
1809
1810         if (si->dfs[t] < si->dfs[nnode])
1811           si->dfs[n] = si->dfs[t];
1812       }
1813     }
1814
1815   /* See if any components have been identified.  */
1816   if (si->dfs[n] == my_dfs)
1817     {
1818       while (VEC_length (unsigned, si->scc_stack) != 0
1819              && si->dfs[VEC_last (unsigned, si->scc_stack)] >= my_dfs)
1820         {
1821           unsigned int w = VEC_pop (unsigned, si->scc_stack);
1822           si->node_mapping[w] = n;
1823
1824           if (!TEST_BIT (graph->direct_nodes, w))
1825             RESET_BIT (graph->direct_nodes, n);
1826
1827           /* Unify our nodes.  */
1828           if (graph->preds[w])
1829             {
1830               if (!graph->preds[n])
1831                 graph->preds[n] = BITMAP_ALLOC (&predbitmap_obstack);
1832               bitmap_ior_into (graph->preds[n], graph->preds[w]);
1833             }
1834           if (graph->implicit_preds[w])
1835             {
1836               if (!graph->implicit_preds[n])
1837                 graph->implicit_preds[n] = BITMAP_ALLOC (&predbitmap_obstack);
1838               bitmap_ior_into (graph->implicit_preds[n],
1839                                graph->implicit_preds[w]);
1840             }
1841           if (graph->points_to[w])
1842             {
1843               if (!graph->points_to[n])
1844                 graph->points_to[n] = BITMAP_ALLOC (&predbitmap_obstack);
1845               bitmap_ior_into (graph->points_to[n],
1846                                graph->points_to[w]);
1847             }
1848           EXECUTE_IF_IN_NONNULL_BITMAP (graph->preds[n], 0, i, bi)
1849             {
1850               unsigned int rep = si->node_mapping[i];
1851               graph->number_incoming[rep]++;
1852             }
1853         }
1854       SET_BIT (si->deleted, n);
1855     }
1856   else
1857     VEC_safe_push (unsigned, heap, si->scc_stack, n);
1858 }
1859
1860 /* Label pointer equivalences.  */
1861
1862 static void
1863 label_visit (constraint_graph_t graph, struct scc_info *si, unsigned int n)
1864 {
1865   unsigned int i;
1866   bitmap_iterator bi;
1867   SET_BIT (si->visited, n);
1868
1869   if (!graph->points_to[n])
1870     graph->points_to[n] = BITMAP_ALLOC (&predbitmap_obstack);
1871
1872   /* Label and union our incoming edges's points to sets.  */
1873   EXECUTE_IF_IN_NONNULL_BITMAP (graph->preds[n], 0, i, bi)
1874     {
1875       unsigned int w = si->node_mapping[i];
1876       if (!TEST_BIT (si->visited, w))
1877         label_visit (graph, si, w);
1878
1879       /* Skip unused edges  */
1880       if (w == n || graph->pointer_label[w] == 0)
1881         {
1882           graph->number_incoming[w]--;
1883           continue;
1884         }
1885       if (graph->points_to[w])
1886         bitmap_ior_into(graph->points_to[n], graph->points_to[w]);
1887
1888       /* If all incoming edges to w have been processed and
1889          graph->points_to[w] was not stored in the hash table, we can
1890          free it.  */
1891       graph->number_incoming[w]--;
1892       if (!graph->number_incoming[w] && !TEST_BIT (graph->pt_used, w))
1893         {
1894           BITMAP_FREE (graph->points_to[w]);
1895         }
1896     }
1897   /* Indirect nodes get fresh variables.  */
1898   if (!TEST_BIT (graph->direct_nodes, n))
1899     bitmap_set_bit (graph->points_to[n], FIRST_REF_NODE + n);
1900
1901   if (!bitmap_empty_p (graph->points_to[n]))
1902     {
1903       unsigned int label = equiv_class_lookup (pointer_equiv_class_table,
1904                                                graph->points_to[n]);
1905       if (!label)
1906         {
1907           SET_BIT (graph->pt_used, n);
1908           label = pointer_equiv_class++;
1909           equiv_class_add (pointer_equiv_class_table,
1910                            label, graph->points_to[n]);
1911         }
1912       graph->pointer_label[n] = label;
1913     }
1914 }
1915
1916 /* Perform offline variable substitution, discovering equivalence
1917    classes, and eliminating non-pointer variables.  */
1918
1919 static struct scc_info *
1920 perform_var_substitution (constraint_graph_t graph)
1921 {
1922   unsigned int i;
1923   unsigned int size = graph->size;
1924   struct scc_info *si = init_scc_info (size);
1925
1926   bitmap_obstack_initialize (&iteration_obstack);
1927   pointer_equiv_class_table = htab_create (511, equiv_class_label_hash,
1928                                            equiv_class_label_eq, free);
1929   location_equiv_class_table = htab_create (511, equiv_class_label_hash,
1930                                             equiv_class_label_eq, free);
1931   pointer_equiv_class = 1;
1932   location_equiv_class = 1;
1933
1934   /* Condense the nodes, which means to find SCC's, count incoming
1935      predecessors, and unite nodes in SCC's.  */
1936   for (i = 0; i < FIRST_REF_NODE; i++)
1937     if (!TEST_BIT (si->visited, si->node_mapping[i]))
1938       condense_visit (graph, si, si->node_mapping[i]);
1939
1940   sbitmap_zero (si->visited);
1941   /* Actually the label the nodes for pointer equivalences  */
1942   for (i = 0; i < FIRST_REF_NODE; i++)
1943     if (!TEST_BIT (si->visited, si->node_mapping[i]))
1944       label_visit (graph, si, si->node_mapping[i]);
1945
1946   /* Calculate location equivalence labels.  */
1947   for (i = 0; i < FIRST_REF_NODE; i++)
1948     {
1949       bitmap pointed_by;
1950       bitmap_iterator bi;
1951       unsigned int j;
1952       unsigned int label;
1953
1954       if (!graph->pointed_by[i])
1955         continue;
1956       pointed_by = BITMAP_ALLOC (&iteration_obstack);
1957
1958       /* Translate the pointed-by mapping for pointer equivalence
1959          labels.  */
1960       EXECUTE_IF_SET_IN_BITMAP (graph->pointed_by[i], 0, j, bi)
1961         {
1962           bitmap_set_bit (pointed_by,
1963                           graph->pointer_label[si->node_mapping[j]]);
1964         }
1965       /* The original pointed_by is now dead.  */
1966       BITMAP_FREE (graph->pointed_by[i]);
1967
1968       /* Look up the location equivalence label if one exists, or make
1969          one otherwise.  */
1970       label = equiv_class_lookup (location_equiv_class_table,
1971                                   pointed_by);
1972       if (label == 0)
1973         {
1974           label = location_equiv_class++;
1975           equiv_class_add (location_equiv_class_table,
1976                            label, pointed_by);
1977         }
1978       else
1979         {
1980           if (dump_file && (dump_flags & TDF_DETAILS))
1981             fprintf (dump_file, "Found location equivalence for node %s\n",
1982                      get_varinfo (i)->name);
1983           BITMAP_FREE (pointed_by);
1984         }
1985       graph->loc_label[i] = label;
1986
1987     }
1988
1989   if (dump_file && (dump_flags & TDF_DETAILS))
1990     for (i = 0; i < FIRST_REF_NODE; i++)
1991       {
1992         bool direct_node = TEST_BIT (graph->direct_nodes, i);
1993         fprintf (dump_file,
1994                  "Equivalence classes for %s node id %d:%s are pointer: %d"
1995                  ", location:%d\n",
1996                  direct_node ? "Direct node" : "Indirect node", i,
1997                  get_varinfo (i)->name,
1998                  graph->pointer_label[si->node_mapping[i]],
1999                  graph->loc_label[si->node_mapping[i]]);
2000       }
2001
2002   /* Quickly eliminate our non-pointer variables.  */
2003
2004   for (i = 0; i < FIRST_REF_NODE; i++)
2005     {
2006       unsigned int node = si->node_mapping[i];
2007
2008       if (graph->pointer_label[node] == 0)
2009         {
2010           if (dump_file && (dump_flags & TDF_DETAILS))
2011             fprintf (dump_file,
2012                      "%s is a non-pointer variable, eliminating edges.\n",
2013                      get_varinfo (node)->name);
2014           stats.nonpointer_vars++;
2015           clear_edges_for_node (graph, node);
2016         }
2017     }
2018
2019   return si;
2020 }
2021
2022 /* Free information that was only necessary for variable
2023    substitution.  */
2024
2025 static void
2026 free_var_substitution_info (struct scc_info *si)
2027 {
2028   free_scc_info (si);
2029   free (graph->pointer_label);
2030   free (graph->loc_label);
2031   free (graph->pointed_by);
2032   free (graph->points_to);
2033   free (graph->number_incoming);
2034   free (graph->eq_rep);
2035   sbitmap_free (graph->direct_nodes);
2036   sbitmap_free (graph->pt_used);
2037   htab_delete (pointer_equiv_class_table);
2038   htab_delete (location_equiv_class_table);
2039   bitmap_obstack_release (&iteration_obstack);
2040 }
2041
2042 /* Return an existing node that is equivalent to NODE, which has
2043    equivalence class LABEL, if one exists.  Return NODE otherwise.  */
2044
2045 static unsigned int
2046 find_equivalent_node (constraint_graph_t graph,
2047                       unsigned int node, unsigned int label)
2048 {
2049   /* If the address version of this variable is unused, we can
2050      substitute it for anything else with the same label.
2051      Otherwise, we know the pointers are equivalent, but not the
2052      locations, and we can unite them later.  */
2053
2054   if (!bitmap_bit_p (graph->address_taken, node))
2055     {
2056       gcc_assert (label < graph->size);
2057
2058       if (graph->eq_rep[label] != -1)
2059         {
2060           /* Unify the two variables since we know they are equivalent.  */
2061           if (unite (graph->eq_rep[label], node))
2062             unify_nodes (graph, graph->eq_rep[label], node, false);
2063           return graph->eq_rep[label];
2064         }
2065       else
2066         {
2067           graph->eq_rep[label] = node;
2068           graph->pe_rep[label] = node;
2069         }
2070     }
2071   else
2072     {
2073       gcc_assert (label < graph->size);
2074       graph->pe[node] = label;
2075       if (graph->pe_rep[label] == -1)
2076         graph->pe_rep[label] = node;
2077     }
2078
2079   return node;
2080 }
2081
2082 /* Unite pointer equivalent but not location equivalent nodes in
2083    GRAPH.  This may only be performed once variable substitution is
2084    finished.  */
2085
2086 static void
2087 unite_pointer_equivalences (constraint_graph_t graph)
2088 {
2089   unsigned int i;
2090
2091   /* Go through the pointer equivalences and unite them to their
2092      representative, if they aren't already.  */
2093   for (i = 0; i < FIRST_REF_NODE; i++)
2094     {
2095       unsigned int label = graph->pe[i];
2096       if (label)
2097         {
2098           int label_rep = graph->pe_rep[label];
2099           
2100           if (label_rep == -1)
2101             continue;
2102           
2103           label_rep = find (label_rep);
2104           if (label_rep >= 0 && unite (label_rep, find (i)))
2105             unify_nodes (graph, label_rep, i, false);
2106         }
2107     }
2108 }
2109
2110 /* Move complex constraints to the GRAPH nodes they belong to.  */
2111
2112 static void
2113 move_complex_constraints (constraint_graph_t graph)
2114 {
2115   int i;
2116   constraint_t c;
2117
2118   for (i = 0; VEC_iterate (constraint_t, constraints, i, c); i++)
2119     {
2120       if (c)
2121         {
2122           struct constraint_expr lhs = c->lhs;
2123           struct constraint_expr rhs = c->rhs;
2124
2125           if (lhs.type == DEREF)
2126             {
2127               insert_into_complex (graph, lhs.var, c);
2128             }
2129           else if (rhs.type == DEREF)
2130             {
2131               if (!(get_varinfo (lhs.var)->is_special_var))
2132                 insert_into_complex (graph, rhs.var, c);
2133             }
2134           else if (rhs.type != ADDRESSOF && lhs.var > anything_id
2135                    && (lhs.offset != 0 || rhs.offset != 0))
2136             {
2137               insert_into_complex (graph, rhs.var, c);
2138             }
2139         }
2140     }
2141 }
2142
2143
2144 /* Optimize and rewrite complex constraints while performing
2145    collapsing of equivalent nodes.  SI is the SCC_INFO that is the
2146    result of perform_variable_substitution.  */
2147
2148 static void
2149 rewrite_constraints (constraint_graph_t graph,
2150                      struct scc_info *si)
2151 {
2152   int i;
2153   unsigned int j;
2154   constraint_t c;
2155
2156   for (j = 0; j < graph->size; j++)
2157     gcc_assert (find (j) == j);
2158
2159   for (i = 0; VEC_iterate (constraint_t, constraints, i, c); i++)
2160     {
2161       struct constraint_expr lhs = c->lhs;
2162       struct constraint_expr rhs = c->rhs;
2163       unsigned int lhsvar = find (get_varinfo_fc (lhs.var)->id);
2164       unsigned int rhsvar = find (get_varinfo_fc (rhs.var)->id);
2165       unsigned int lhsnode, rhsnode;
2166       unsigned int lhslabel, rhslabel;
2167
2168       lhsnode = si->node_mapping[lhsvar];
2169       rhsnode = si->node_mapping[rhsvar];
2170       lhslabel = graph->pointer_label[lhsnode];
2171       rhslabel = graph->pointer_label[rhsnode];
2172
2173       /* See if it is really a non-pointer variable, and if so, ignore
2174          the constraint.  */
2175       if (lhslabel == 0)
2176         {
2177           if (dump_file && (dump_flags & TDF_DETAILS))
2178             {
2179               
2180               fprintf (dump_file, "%s is a non-pointer variable,"
2181                        "ignoring constraint:",
2182                        get_varinfo (lhs.var)->name);
2183               dump_constraint (dump_file, c);
2184             }
2185           VEC_replace (constraint_t, constraints, i, NULL);
2186           continue;
2187         }
2188
2189       if (rhslabel == 0)
2190         {
2191           if (dump_file && (dump_flags & TDF_DETAILS))
2192             {
2193               
2194               fprintf (dump_file, "%s is a non-pointer variable,"
2195                        "ignoring constraint:",
2196                        get_varinfo (rhs.var)->name);
2197               dump_constraint (dump_file, c);
2198             }
2199           VEC_replace (constraint_t, constraints, i, NULL);
2200           continue;
2201         }
2202
2203       lhsvar = find_equivalent_node (graph, lhsvar, lhslabel);
2204       rhsvar = find_equivalent_node (graph, rhsvar, rhslabel);
2205       c->lhs.var = lhsvar;
2206       c->rhs.var = rhsvar;
2207
2208     }
2209 }
2210
2211 /* Eliminate indirect cycles involving NODE.  Return true if NODE was
2212    part of an SCC, false otherwise.  */
2213
2214 static bool
2215 eliminate_indirect_cycles (unsigned int node)
2216 {
2217   if (graph->indirect_cycles[node] != -1
2218       && !bitmap_empty_p (get_varinfo (node)->solution))
2219     {
2220       unsigned int i;
2221       VEC(unsigned,heap) *queue = NULL;
2222       int queuepos;
2223       unsigned int to = find (graph->indirect_cycles[node]);
2224       bitmap_iterator bi;
2225
2226       /* We can't touch the solution set and call unify_nodes
2227          at the same time, because unify_nodes is going to do
2228          bitmap unions into it. */
2229
2230       EXECUTE_IF_SET_IN_BITMAP (get_varinfo (node)->solution, 0, i, bi)
2231         {
2232           if (find (i) == i && i != to)
2233             {
2234               if (unite (to, i))
2235                 VEC_safe_push (unsigned, heap, queue, i);
2236             }
2237         }
2238
2239       for (queuepos = 0;
2240            VEC_iterate (unsigned, queue, queuepos, i);
2241            queuepos++)
2242         {
2243           unify_nodes (graph, to, i, true);
2244         }
2245       VEC_free (unsigned, heap, queue);
2246       return true;
2247     }
2248   return false;
2249 }
2250
2251 /* Solve the constraint graph GRAPH using our worklist solver.
2252    This is based on the PW* family of solvers from the "Efficient Field
2253    Sensitive Pointer Analysis for C" paper.
2254    It works by iterating over all the graph nodes, processing the complex
2255    constraints and propagating the copy constraints, until everything stops
2256    changed.  This corresponds to steps 6-8 in the solving list given above.  */
2257
2258 static void
2259 solve_graph (constraint_graph_t graph)
2260 {
2261   unsigned int size = graph->size;
2262   unsigned int i;
2263   bitmap pts;
2264
2265   changed_count = 0;
2266   changed = sbitmap_alloc (size);
2267   sbitmap_zero (changed);
2268
2269   /* Mark all initial non-collapsed nodes as changed.  */
2270   for (i = 0; i < size; i++)
2271     {
2272       varinfo_t ivi = get_varinfo (i);
2273       if (find (i) == i && !bitmap_empty_p (ivi->solution)
2274           && ((graph->succs[i] && !bitmap_empty_p (graph->succs[i]))
2275               || VEC_length (constraint_t, graph->complex[i]) > 0))
2276         {
2277           SET_BIT (changed, i);
2278           changed_count++;
2279         }
2280     }
2281
2282   /* Allocate a bitmap to be used to store the changed bits.  */
2283   pts = BITMAP_ALLOC (&pta_obstack);
2284
2285   while (changed_count > 0)
2286     {
2287       unsigned int i;
2288       struct topo_info *ti = init_topo_info ();
2289       stats.iterations++;
2290
2291       bitmap_obstack_initialize (&iteration_obstack);
2292
2293       compute_topo_order (graph, ti);
2294
2295       while (VEC_length (unsigned, ti->topo_order) != 0)
2296         {
2297
2298           i = VEC_pop (unsigned, ti->topo_order);
2299
2300           /* If this variable is not a representative, skip it.  */
2301           if (find (i) != i)
2302             continue;
2303
2304           /* In certain indirect cycle cases, we may merge this
2305              variable to another.  */
2306           if (eliminate_indirect_cycles (i) && find (i) != i)
2307             continue;
2308
2309           /* If the node has changed, we need to process the
2310              complex constraints and outgoing edges again.  */
2311           if (TEST_BIT (changed, i))
2312             {
2313               unsigned int j;
2314               constraint_t c;
2315               bitmap solution;
2316               VEC(constraint_t,heap) *complex = graph->complex[i];
2317               bool solution_empty;
2318
2319               RESET_BIT (changed, i);
2320               changed_count--;
2321
2322               /* Compute the changed set of solution bits.  */
2323               bitmap_and_compl (pts, get_varinfo (i)->solution,
2324                                 get_varinfo (i)->oldsolution);
2325
2326               if (bitmap_empty_p (pts))
2327                 continue;
2328
2329               bitmap_ior_into (get_varinfo (i)->oldsolution, pts);
2330
2331               solution = get_varinfo (i)->solution;
2332               solution_empty = bitmap_empty_p (solution);
2333
2334               /* Process the complex constraints */
2335               for (j = 0; VEC_iterate (constraint_t, complex, j, c); j++)
2336                 {
2337                   /* XXX: This is going to unsort the constraints in
2338                      some cases, which will occasionally add duplicate
2339                      constraints during unification.  This does not
2340                      affect correctness.  */
2341                   c->lhs.var = find (c->lhs.var);
2342                   c->rhs.var = find (c->rhs.var);
2343
2344                   /* The only complex constraint that can change our
2345                      solution to non-empty, given an empty solution,
2346                      is a constraint where the lhs side is receiving
2347                      some set from elsewhere.  */
2348                   if (!solution_empty || c->lhs.type != DEREF)
2349                     do_complex_constraint (graph, c, pts);
2350                 }
2351
2352               solution_empty = bitmap_empty_p (solution);
2353
2354               if (!solution_empty)
2355                 {
2356                   bitmap_iterator bi;
2357
2358                   /* Propagate solution to all successors.  */
2359                   EXECUTE_IF_IN_NONNULL_BITMAP (graph->succs[i],
2360                                                 0, j, bi)
2361                     {
2362                       bitmap tmp;
2363                       bool flag;
2364
2365                       unsigned int to = find (j);
2366                       tmp = get_varinfo (to)->solution;
2367                       flag = false;
2368
2369                       /* Don't try to propagate to ourselves.  */
2370                       if (to == i)
2371                         continue;
2372
2373                       flag = set_union_with_increment (tmp, pts, 0);
2374
2375                       if (flag)
2376                         {
2377                           get_varinfo (to)->solution = tmp;
2378                           if (!TEST_BIT (changed, to))
2379                             {
2380                               SET_BIT (changed, to);
2381                               changed_count++;
2382                             }
2383                         }
2384                     }
2385                 }
2386             }
2387         }
2388       free_topo_info (ti);
2389       bitmap_obstack_release (&iteration_obstack);
2390     }
2391
2392   BITMAP_FREE (pts);
2393   sbitmap_free (changed);
2394   bitmap_obstack_release (&oldpta_obstack);
2395 }
2396
2397 /* Map from trees to variable infos.  */
2398 static struct pointer_map_t *vi_for_tree;
2399
2400
2401 /* Insert ID as the variable id for tree T in the vi_for_tree map.  */
2402
2403 static void
2404 insert_vi_for_tree (tree t, varinfo_t vi)
2405 {
2406   void **slot = pointer_map_insert (vi_for_tree, t);
2407   gcc_assert (vi);
2408   gcc_assert (*slot == NULL);
2409   *slot = vi;
2410 }
2411
2412 /* Find the variable info for tree T in VI_FOR_TREE.  If T does not
2413    exist in the map, return NULL, otherwise, return the varinfo we found.  */
2414
2415 static varinfo_t
2416 lookup_vi_for_tree (tree t)
2417 {
2418   void **slot = pointer_map_contains (vi_for_tree, t);
2419   if (slot == NULL)
2420     return NULL;
2421
2422   return (varinfo_t) *slot;
2423 }
2424
2425 /* Return a printable name for DECL  */
2426
2427 static const char *
2428 alias_get_name (tree decl)
2429 {
2430   const char *res = get_name (decl);
2431   char *temp;
2432   int num_printed = 0;
2433
2434   if (res != NULL)
2435     return res;
2436
2437   res = "NULL";
2438   if (!dump_file)
2439     return res;
2440
2441   if (TREE_CODE (decl) == SSA_NAME)
2442     {
2443       num_printed = asprintf (&temp, "%s_%u",
2444                               alias_get_name (SSA_NAME_VAR (decl)),
2445                               SSA_NAME_VERSION (decl));
2446     }
2447   else if (DECL_P (decl))
2448     {
2449       num_printed = asprintf (&temp, "D.%u", DECL_UID (decl));
2450     }
2451   if (num_printed > 0)
2452     {
2453       res = ggc_strdup (temp);
2454       free (temp);
2455     }
2456   return res;
2457 }
2458
2459 /* Find the variable id for tree T in the map.
2460    If T doesn't exist in the map, create an entry for it and return it.  */
2461
2462 static varinfo_t
2463 get_vi_for_tree (tree t)
2464 {
2465   void **slot = pointer_map_contains (vi_for_tree, t);
2466   if (slot == NULL)
2467     return get_varinfo (create_variable_info_for (t, alias_get_name (t)));
2468
2469   return (varinfo_t) *slot;
2470 }
2471
2472 /* Get a constraint expression from an SSA_VAR_P node.  */
2473
2474 static struct constraint_expr
2475 get_constraint_exp_from_ssa_var (tree t)
2476 {
2477   struct constraint_expr cexpr;
2478
2479   gcc_assert (SSA_VAR_P (t) || DECL_P (t));
2480
2481   /* For parameters, get at the points-to set for the actual parm
2482      decl.  */
2483   if (TREE_CODE (t) == SSA_NAME
2484       && TREE_CODE (SSA_NAME_VAR (t)) == PARM_DECL
2485       && SSA_NAME_IS_DEFAULT_DEF (t))
2486     return get_constraint_exp_from_ssa_var (SSA_NAME_VAR (t));
2487
2488   cexpr.type = SCALAR;
2489
2490   cexpr.var = get_vi_for_tree (t)->id;
2491   /* If we determine the result is "anything", and we know this is readonly,
2492      say it points to readonly memory instead.  */
2493   if (cexpr.var == anything_id && TREE_READONLY (t))
2494     {
2495       cexpr.type = ADDRESSOF;
2496       cexpr.var = readonly_id;
2497     }
2498
2499   cexpr.offset = 0;
2500   return cexpr;
2501 }
2502
2503 /* Process a completed constraint T, and add it to the constraint
2504    list.  FROM_CALL is true if this is a constraint coming from a
2505    call, which means any DEREFs we see are "may-deref's", not
2506    "must-deref"'s.  */
2507
2508 static void
2509 process_constraint_1 (constraint_t t, bool from_call)
2510 {
2511   struct constraint_expr rhs = t->rhs;
2512   struct constraint_expr lhs = t->lhs;
2513
2514   gcc_assert (rhs.var < VEC_length (varinfo_t, varmap));
2515   gcc_assert (lhs.var < VEC_length (varinfo_t, varmap));
2516
2517   if (!use_field_sensitive)
2518     {
2519       t->rhs.offset = 0;
2520       t->lhs.offset = 0;
2521     }
2522
2523   /* ANYTHING == ANYTHING is pointless.  */
2524   if (lhs.var == anything_id && rhs.var == anything_id)
2525     return;
2526
2527   /* If we have &ANYTHING = something, convert to SOMETHING = &ANYTHING) */
2528   else if (lhs.var == anything_id && lhs.type == ADDRESSOF)
2529     {
2530       rhs = t->lhs;
2531       t->lhs = t->rhs;
2532       t->rhs = rhs;
2533       process_constraint_1 (t, from_call);
2534     }
2535   /* This can happen in our IR with things like n->a = *p */
2536   else if (rhs.type == DEREF && lhs.type == DEREF && rhs.var != anything_id)
2537     {
2538       /* Split into tmp = *rhs, *lhs = tmp */
2539       tree rhsdecl = get_varinfo (rhs.var)->decl;
2540       tree pointertype = TREE_TYPE (rhsdecl);
2541       tree pointedtotype = TREE_TYPE (pointertype);
2542       tree tmpvar = create_tmp_var_raw (pointedtotype, "doubledereftmp");
2543       struct constraint_expr tmplhs = get_constraint_exp_from_ssa_var (tmpvar);
2544
2545       /* If this is an aggregate of known size, we should have passed
2546          this off to do_structure_copy, and it should have broken it
2547          up.  */
2548       gcc_assert (!AGGREGATE_TYPE_P (pointedtotype)
2549                   || get_varinfo (rhs.var)->is_unknown_size_var);
2550
2551       process_constraint_1 (new_constraint (tmplhs, rhs), from_call);
2552       process_constraint_1 (new_constraint (lhs, tmplhs), from_call);
2553     }
2554   else if (rhs.type == ADDRESSOF && lhs.type == DEREF)
2555     {
2556       /* Split into tmp = &rhs, *lhs = tmp */
2557       tree rhsdecl = get_varinfo (rhs.var)->decl;
2558       tree pointertype = TREE_TYPE (rhsdecl);
2559       tree tmpvar = create_tmp_var_raw (pointertype, "derefaddrtmp");
2560       struct constraint_expr tmplhs = get_constraint_exp_from_ssa_var (tmpvar);
2561
2562       process_constraint_1 (new_constraint (tmplhs, rhs), from_call);
2563       process_constraint_1 (new_constraint (lhs, tmplhs), from_call);
2564     }
2565   else
2566     {
2567       gcc_assert (rhs.type != ADDRESSOF || rhs.offset == 0);
2568       VEC_safe_push (constraint_t, heap, constraints, t);
2569     }
2570 }
2571
2572
2573 /* Process constraint T, performing various simplifications and then
2574    adding it to our list of overall constraints.  */
2575
2576 static void
2577 process_constraint (constraint_t t)
2578 {
2579   process_constraint_1 (t, false);
2580 }
2581
2582 /* Return true if T is a variable of a type that could contain
2583    pointers.  */
2584
2585 static bool
2586 could_have_pointers (tree t)
2587 {
2588   tree type = TREE_TYPE (t);
2589
2590   if (POINTER_TYPE_P (type)
2591       || AGGREGATE_TYPE_P (type)
2592       || TREE_CODE (type) == COMPLEX_TYPE)
2593     return true;
2594
2595   return false;
2596 }
2597
2598 /* Return the position, in bits, of FIELD_DECL from the beginning of its
2599    structure.  */
2600
2601 static unsigned HOST_WIDE_INT
2602 bitpos_of_field (const tree fdecl)
2603 {
2604
2605   if (TREE_CODE (DECL_FIELD_OFFSET (fdecl)) != INTEGER_CST
2606       || TREE_CODE (DECL_FIELD_BIT_OFFSET (fdecl)) != INTEGER_CST)
2607     return -1;
2608
2609   return (tree_low_cst (DECL_FIELD_OFFSET (fdecl), 1) * 8)
2610          + tree_low_cst (DECL_FIELD_BIT_OFFSET (fdecl), 1);
2611 }
2612
2613
2614 /* Given a COMPONENT_REF T, return the constraint_expr for it.  */
2615
2616 static void
2617 get_constraint_for_component_ref (tree t, VEC(ce_s, heap) **results)
2618 {
2619   tree orig_t = t;
2620   HOST_WIDE_INT bitsize = -1;
2621   HOST_WIDE_INT bitmaxsize = -1;
2622   HOST_WIDE_INT bitpos;
2623   tree forzero;
2624   struct constraint_expr *result;
2625   unsigned int beforelength = VEC_length (ce_s, *results);
2626
2627   /* Some people like to do cute things like take the address of
2628      &0->a.b */
2629   forzero = t;
2630   while (!SSA_VAR_P (forzero) && !CONSTANT_CLASS_P (forzero))
2631     forzero = TREE_OPERAND (forzero, 0);
2632
2633   if (CONSTANT_CLASS_P (forzero) && integer_zerop (forzero))
2634     {
2635       struct constraint_expr temp;
2636
2637       temp.offset = 0;
2638       temp.var = integer_id;
2639       temp.type = SCALAR;
2640       VEC_safe_push (ce_s, heap, *results, &temp);
2641       return;
2642     }
2643
2644   t = get_ref_base_and_extent (t, &bitpos, &bitsize, &bitmaxsize);
2645
2646   get_constraint_for (t, results);
2647   result = VEC_last (ce_s, *results);
2648   result->offset = bitpos;
2649
2650   gcc_assert (beforelength + 1 == VEC_length (ce_s, *results));
2651
2652   /* This can also happen due to weird offsetof type macros.  */
2653   if (TREE_CODE (t) != ADDR_EXPR && result->type == ADDRESSOF)
2654     result->type = SCALAR;
2655
2656   if (result->type == SCALAR)
2657     {
2658       /* In languages like C, you can access one past the end of an
2659          array.  You aren't allowed to dereference it, so we can
2660          ignore this constraint. When we handle pointer subtraction,
2661          we may have to do something cute here.  */
2662
2663       if (result->offset < get_varinfo (result->var)->fullsize
2664           && bitmaxsize != 0)
2665         {
2666           /* It's also not true that the constraint will actually start at the
2667              right offset, it may start in some padding.  We only care about
2668              setting the constraint to the first actual field it touches, so
2669              walk to find it.  */
2670           varinfo_t curr;
2671           for (curr = get_varinfo (result->var); curr; curr = curr->next)
2672             {
2673               if (ranges_overlap_p (curr->offset, curr->size,
2674                                     result->offset, bitmaxsize))
2675                 {
2676                   result->var = curr->id;
2677                   break;
2678                 }
2679             }
2680           /* assert that we found *some* field there. The user couldn't be
2681              accessing *only* padding.  */
2682           /* Still the user could access one past the end of an array
2683              embedded in a struct resulting in accessing *only* padding.  */
2684           gcc_assert (curr || ref_contains_array_ref (orig_t));
2685         }
2686       else if (bitmaxsize == 0)
2687         {
2688           if (dump_file && (dump_flags & TDF_DETAILS))
2689             fprintf (dump_file, "Access to zero-sized part of variable,"
2690                      "ignoring\n");
2691         }
2692       else
2693         if (dump_file && (dump_flags & TDF_DETAILS))
2694           fprintf (dump_file, "Access to past the end of variable, ignoring\n");
2695
2696       result->offset = 0;
2697     }
2698   else if (bitmaxsize == -1)
2699     {
2700       /* We can't handle DEREF constraints with unknown size, we'll
2701          get the wrong answer.  Punt and return anything.  */
2702       result->var = anything_id;
2703       result->offset = 0;
2704     }
2705 }
2706
2707
2708 /* Dereference the constraint expression CONS, and return the result.
2709    DEREF (ADDRESSOF) = SCALAR
2710    DEREF (SCALAR) = DEREF
2711    DEREF (DEREF) = (temp = DEREF1; result = DEREF(temp))
2712    This is needed so that we can handle dereferencing DEREF constraints.  */
2713
2714 static void
2715 do_deref (VEC (ce_s, heap) **constraints)
2716 {
2717   struct constraint_expr *c;
2718   unsigned int i = 0;
2719
2720   for (i = 0; VEC_iterate (ce_s, *constraints, i, c); i++)
2721     {
2722       if (c->type == SCALAR)
2723         c->type = DEREF;
2724       else if (c->type == ADDRESSOF)
2725         c->type = SCALAR;
2726       else if (c->type == DEREF)
2727         {
2728           tree tmpvar = create_tmp_var_raw (ptr_type_node, "dereftmp");
2729           struct constraint_expr tmplhs = get_constraint_exp_from_ssa_var (tmpvar);
2730           process_constraint (new_constraint (tmplhs, *c));
2731           c->var = tmplhs.var;
2732         }
2733       else
2734         gcc_unreachable ();
2735     }
2736 }
2737
2738 /* Given a tree T, return the constraint expression for it.  */
2739
2740 static void
2741 get_constraint_for (tree t, VEC (ce_s, heap) **results)
2742 {
2743   struct constraint_expr temp;
2744
2745   /* x = integer is all glommed to a single variable, which doesn't
2746      point to anything by itself.  That is, of course, unless it is an
2747      integer constant being treated as a pointer, in which case, we
2748      will return that this is really the addressof anything.  This
2749      happens below, since it will fall into the default case. The only
2750      case we know something about an integer treated like a pointer is
2751      when it is the NULL pointer, and then we just say it points to
2752      NULL.  */
2753   if (TREE_CODE (t) == INTEGER_CST
2754       && integer_zerop (t))
2755     {
2756       temp.var = nothing_id;
2757       temp.type = ADDRESSOF;
2758       temp.offset = 0;
2759       VEC_safe_push (ce_s, heap, *results, &temp);
2760       return;
2761     }
2762
2763   /* String constants are read-only.  */
2764   if (TREE_CODE (t) == STRING_CST)
2765     {
2766       temp.var = readonly_id;
2767       temp.type = SCALAR;
2768       temp.offset = 0;
2769       VEC_safe_push (ce_s, heap, *results, &temp);
2770       return;
2771     }
2772
2773   switch (TREE_CODE_CLASS (TREE_CODE (t)))
2774     {
2775     case tcc_expression:
2776     case tcc_vl_exp:
2777       {
2778         switch (TREE_CODE (t))
2779           {
2780           case ADDR_EXPR:
2781             {
2782               struct constraint_expr *c;
2783               unsigned int i;
2784               tree exp = TREE_OPERAND (t, 0);
2785               tree pttype = TREE_TYPE (TREE_TYPE (t));
2786
2787               get_constraint_for (exp, results);
2788
2789
2790               /* Complex types are special. Taking the address of one
2791                  allows you to access either part of it through that
2792                  pointer.  */
2793               if (VEC_length (ce_s, *results) == 1 &&
2794                   TREE_CODE (pttype) == COMPLEX_TYPE)
2795                 {
2796                   struct constraint_expr *origrhs;
2797                   varinfo_t origvar;
2798                   struct constraint_expr tmp;
2799
2800                   gcc_assert (VEC_length (ce_s, *results) == 1);
2801                   origrhs = VEC_last (ce_s, *results);
2802                   tmp = *origrhs;
2803                   VEC_pop (ce_s, *results);
2804                   origvar = get_varinfo (origrhs->var);
2805                   for (; origvar; origvar = origvar->next)
2806                     {
2807                       tmp.var = origvar->id;
2808                       VEC_safe_push (ce_s, heap, *results, &tmp);
2809                     }
2810                 }
2811
2812               for (i = 0; VEC_iterate (ce_s, *results, i, c); i++)
2813                 {
2814                   if (c->type == DEREF)
2815                     c->type = SCALAR;
2816                   else
2817                     c->type = ADDRESSOF;
2818                 }
2819               return;
2820             }
2821             break;
2822           case CALL_EXPR:
2823             /* XXX: In interprocedural mode, if we didn't have the
2824                body, we would need to do *each pointer argument =
2825                &ANYTHING added.  */
2826             if (call_expr_flags (t) & (ECF_MALLOC | ECF_MAY_BE_ALLOCA))
2827               {
2828                 varinfo_t vi;
2829                 tree heapvar = heapvar_lookup (t);
2830
2831                 if (heapvar == NULL)
2832                   {
2833                     heapvar = create_tmp_var_raw (ptr_type_node, "HEAP");
2834                     DECL_EXTERNAL (heapvar) = 1;
2835                     get_var_ann (heapvar)->is_heapvar = 1;
2836                     if (gimple_referenced_vars (cfun))
2837                       add_referenced_var (heapvar);
2838                     heapvar_insert (t, heapvar);
2839                   }
2840
2841                 temp.var = create_variable_info_for (heapvar,
2842                                                      alias_get_name (heapvar));
2843
2844                 vi = get_varinfo (temp.var);
2845                 vi->is_artificial_var = 1;
2846                 vi->is_heap_var = 1;
2847                 temp.type = ADDRESSOF;
2848                 temp.offset = 0;
2849                 VEC_safe_push (ce_s, heap, *results, &temp);
2850                 return;
2851               }
2852             else
2853               {
2854                 temp.var = anything_id;
2855                 temp.type = SCALAR;
2856                 temp.offset = 0;
2857                 VEC_safe_push (ce_s, heap, *results, &temp);
2858                 return;
2859               }
2860             break;
2861           default:
2862             {
2863               temp.type = ADDRESSOF;
2864               temp.var = anything_id;
2865               temp.offset = 0;
2866               VEC_safe_push (ce_s, heap, *results, &temp);
2867               return;
2868             }
2869           }
2870       }
2871     case tcc_reference:
2872       {
2873         switch (TREE_CODE (t))
2874           {
2875           case INDIRECT_REF:
2876             {
2877               get_constraint_for (TREE_OPERAND (t, 0), results);
2878               do_deref (results);
2879               return;
2880             }
2881           case ARRAY_REF:
2882           case ARRAY_RANGE_REF:
2883           case COMPONENT_REF:
2884             get_constraint_for_component_ref (t, results);
2885             return;
2886           default:
2887             {
2888               temp.type = ADDRESSOF;
2889               temp.var = anything_id;
2890               temp.offset = 0;
2891               VEC_safe_push (ce_s, heap, *results, &temp);
2892               return;
2893             }
2894           }
2895       }
2896     case tcc_unary:
2897       {
2898         switch (TREE_CODE (t))
2899           {
2900           CASE_CONVERT:
2901             {
2902               tree op = TREE_OPERAND (t, 0);
2903
2904               /* Cast from non-pointer to pointers are bad news for us.
2905                  Anything else, we see through */
2906               if (!(POINTER_TYPE_P (TREE_TYPE (t))
2907                     && ! POINTER_TYPE_P (TREE_TYPE (op))))
2908                 {
2909                   get_constraint_for (op, results);
2910                   return;
2911                 }
2912
2913               /* FALLTHRU  */
2914             }
2915           default:
2916             {
2917               temp.type = ADDRESSOF;
2918               temp.var = anything_id;
2919               temp.offset = 0;
2920               VEC_safe_push (ce_s, heap, *results, &temp);
2921               return;
2922             }
2923           }
2924       }
2925     case tcc_exceptional:
2926       {
2927         switch (TREE_CODE (t))
2928           {
2929           case PHI_NODE:
2930             {
2931               get_constraint_for (PHI_RESULT (t), results);
2932               return;
2933             }
2934             break;
2935           case SSA_NAME:
2936             {
2937               struct constraint_expr temp;
2938               temp = get_constraint_exp_from_ssa_var (t);
2939               VEC_safe_push (ce_s, heap, *results, &temp);
2940               return;
2941             }
2942             break;
2943           default:
2944             {
2945               temp.type = ADDRESSOF;
2946               temp.var = anything_id;
2947               temp.offset = 0;
2948               VEC_safe_push (ce_s, heap, *results, &temp);
2949               return;
2950             }
2951           }
2952       }
2953     case tcc_declaration:
2954       {
2955         struct constraint_expr temp;
2956         temp = get_constraint_exp_from_ssa_var (t);
2957         VEC_safe_push (ce_s, heap, *results, &temp);
2958         return;
2959       }
2960     default:
2961       {
2962         temp.type = ADDRESSOF;
2963         temp.var = anything_id;
2964         temp.offset = 0;
2965         VEC_safe_push (ce_s, heap, *results, &temp);
2966         return;
2967       }
2968     }
2969 }
2970
2971
2972 /* Handle the structure copy case where we have a simple structure copy
2973    between LHS and RHS that is of SIZE (in bits)
2974
2975    For each field of the lhs variable (lhsfield)
2976      For each field of the rhs variable at lhsfield.offset (rhsfield)
2977        add the constraint lhsfield = rhsfield
2978
2979    If we fail due to some kind of type unsafety or other thing we
2980    can't handle, return false.  We expect the caller to collapse the
2981    variable in that case.  */
2982
2983 static bool
2984 do_simple_structure_copy (const struct constraint_expr lhs,
2985                           const struct constraint_expr rhs,
2986                           const unsigned HOST_WIDE_INT size)
2987 {
2988   varinfo_t p = get_varinfo (lhs.var);
2989   unsigned HOST_WIDE_INT pstart, last;
2990   pstart = p->offset;
2991   last = p->offset + size;
2992   for (; p && p->offset < last; p = p->next)
2993     {
2994       varinfo_t q;
2995       struct constraint_expr templhs = lhs;
2996       struct constraint_expr temprhs = rhs;
2997       unsigned HOST_WIDE_INT fieldoffset;
2998
2999       templhs.var = p->id;
3000       q = get_varinfo (temprhs.var);
3001       fieldoffset = p->offset - pstart;
3002       q = first_vi_for_offset (q, q->offset + fieldoffset);
3003       if (!q)
3004         return false;
3005       temprhs.var = q->id;
3006       process_constraint (new_constraint (templhs, temprhs));
3007     }
3008   return true;
3009 }
3010
3011
3012 /* Handle the structure copy case where we have a  structure copy between a
3013    aggregate on the LHS and a dereference of a pointer on the RHS
3014    that is of SIZE (in bits)
3015
3016    For each field of the lhs variable (lhsfield)
3017        rhs.offset = lhsfield->offset
3018        add the constraint lhsfield = rhs
3019 */
3020
3021 static void
3022 do_rhs_deref_structure_copy (const struct constraint_expr lhs,
3023                              const struct constraint_expr rhs,
3024                              const unsigned HOST_WIDE_INT size)
3025 {
3026   varinfo_t p = get_varinfo (lhs.var);
3027   unsigned HOST_WIDE_INT pstart,last;
3028   pstart = p->offset;
3029   last = p->offset + size;
3030
3031   for (; p && p->offset < last; p = p->next)
3032     {
3033       varinfo_t q;
3034       struct constraint_expr templhs = lhs;
3035       struct constraint_expr temprhs = rhs;
3036       unsigned HOST_WIDE_INT fieldoffset;
3037
3038
3039       if (templhs.type == SCALAR)
3040         templhs.var = p->id;
3041       else
3042         templhs.offset = p->offset;
3043
3044       q = get_varinfo (temprhs.var);
3045       fieldoffset = p->offset - pstart;
3046       temprhs.offset += fieldoffset;
3047       process_constraint (new_constraint (templhs, temprhs));
3048     }
3049 }
3050
3051 /* Handle the structure copy case where we have a structure copy
3052    between an aggregate on the RHS and a dereference of a pointer on
3053    the LHS that is of SIZE (in bits)
3054
3055    For each field of the rhs variable (rhsfield)
3056        lhs.offset = rhsfield->offset
3057        add the constraint lhs = rhsfield
3058 */
3059
3060 static void
3061 do_lhs_deref_structure_copy (const struct constraint_expr lhs,
3062                              const struct constraint_expr rhs,
3063                              const unsigned HOST_WIDE_INT size)
3064 {
3065   varinfo_t p = get_varinfo (rhs.var);
3066   unsigned HOST_WIDE_INT pstart,last;
3067   pstart = p->offset;
3068   last = p->offset + size;
3069
3070   for (; p && p->offset < last; p = p->next)
3071     {
3072       varinfo_t q;
3073       struct constraint_expr templhs = lhs;
3074       struct constraint_expr temprhs = rhs;
3075       unsigned HOST_WIDE_INT fieldoffset;
3076
3077
3078       if (temprhs.type == SCALAR)
3079         temprhs.var = p->id;
3080       else
3081         temprhs.offset = p->offset;
3082
3083       q = get_varinfo (templhs.var);
3084       fieldoffset = p->offset - pstart;
3085       templhs.offset += fieldoffset;
3086       process_constraint (new_constraint (templhs, temprhs));
3087     }
3088 }
3089
3090 /* Sometimes, frontends like to give us bad type information.  This
3091    function will collapse all the fields from VAR to the end of VAR,
3092    into VAR, so that we treat those fields as a single variable.
3093    We return the variable they were collapsed into.  */
3094
3095 static unsigned int
3096 collapse_rest_of_var (unsigned int var)
3097 {
3098   varinfo_t currvar = get_varinfo (var);
3099   varinfo_t field;
3100
3101   for (field = currvar->next; field; field = field->next)
3102     {
3103       if (dump_file)
3104         fprintf (dump_file, "Type safety: Collapsing var %s into %s\n",
3105                  field->name, currvar->name);
3106
3107       gcc_assert (!field->collapsed_to);
3108       field->collapsed_to = currvar;
3109     }
3110
3111   currvar->next = NULL;
3112   currvar->size = currvar->fullsize - currvar->offset;
3113
3114   return currvar->id;
3115 }
3116
3117 /* Handle aggregate copies by expanding into copies of the respective
3118    fields of the structures.  */
3119
3120 static void
3121 do_structure_copy (tree lhsop, tree rhsop)
3122 {
3123   struct constraint_expr lhs, rhs, tmp;
3124   VEC (ce_s, heap) *lhsc = NULL, *rhsc = NULL;
3125   varinfo_t p;
3126   unsigned HOST_WIDE_INT lhssize;
3127   unsigned HOST_WIDE_INT rhssize;
3128
3129   get_constraint_for (lhsop, &lhsc);
3130   get_constraint_for (rhsop, &rhsc);
3131   gcc_assert (VEC_length (ce_s, lhsc) == 1);
3132   gcc_assert (VEC_length (ce_s, rhsc) == 1);
3133   lhs = *(VEC_last (ce_s, lhsc));
3134   rhs = *(VEC_last (ce_s, rhsc));
3135
3136   VEC_free (ce_s, heap, lhsc);
3137   VEC_free (ce_s, heap, rhsc);
3138
3139   /* If we have special var = x, swap it around.  */
3140   if (lhs.var <= integer_id && !(get_varinfo (rhs.var)->is_special_var))
3141     {
3142       tmp = lhs;
3143       lhs = rhs;
3144       rhs = tmp;
3145     }
3146
3147   /*  This is fairly conservative for the RHS == ADDRESSOF case, in that it's
3148       possible it's something we could handle.  However, most cases falling
3149       into this are dealing with transparent unions, which are slightly
3150       weird. */
3151   if (rhs.type == ADDRESSOF && !(get_varinfo (rhs.var)->is_special_var))
3152     {
3153       rhs.type = ADDRESSOF;
3154       rhs.var = anything_id;
3155     }
3156
3157   /* If the RHS is a special var, or an addressof, set all the LHS fields to
3158      that special var.  */
3159   if (rhs.var <= integer_id)
3160     {
3161       for (p = get_varinfo (lhs.var); p; p = p->next)
3162         {
3163           struct constraint_expr templhs = lhs;
3164           struct constraint_expr temprhs = rhs;
3165
3166           if (templhs.type == SCALAR )
3167             templhs.var = p->id;
3168           else
3169             templhs.offset += p->offset;
3170           process_constraint (new_constraint (templhs, temprhs));
3171         }
3172     }
3173   else
3174     {
3175       tree rhstype = TREE_TYPE (rhsop);
3176       tree lhstype = TREE_TYPE (lhsop);
3177       tree rhstypesize;
3178       tree lhstypesize;
3179
3180       lhstypesize = DECL_P (lhsop) ? DECL_SIZE (lhsop) : TYPE_SIZE (lhstype);
3181       rhstypesize = DECL_P (rhsop) ? DECL_SIZE (rhsop) : TYPE_SIZE (rhstype);
3182
3183       /* If we have a variably sized types on the rhs or lhs, and a deref
3184          constraint, add the constraint, lhsconstraint = &ANYTHING.
3185          This is conservatively correct because either the lhs is an unknown
3186          sized var (if the constraint is SCALAR), or the lhs is a DEREF
3187          constraint, and every variable it can point to must be unknown sized
3188          anyway, so we don't need to worry about fields at all.  */
3189       if ((rhs.type == DEREF && TREE_CODE (rhstypesize) != INTEGER_CST)
3190           || (lhs.type == DEREF && TREE_CODE (lhstypesize) != INTEGER_CST))
3191         {
3192           rhs.var = anything_id;
3193           rhs.type = ADDRESSOF;
3194           rhs.offset = 0;
3195           process_constraint (new_constraint (lhs, rhs));
3196           return;
3197         }
3198
3199       /* The size only really matters insofar as we don't set more or less of
3200          the variable.  If we hit an unknown size var, the size should be the
3201          whole darn thing.  */
3202       if (get_varinfo (rhs.var)->is_unknown_size_var)
3203         rhssize = ~0;
3204       else
3205         rhssize = TREE_INT_CST_LOW (rhstypesize);
3206
3207       if (get_varinfo (lhs.var)->is_unknown_size_var)
3208         lhssize = ~0;
3209       else
3210         lhssize = TREE_INT_CST_LOW (lhstypesize);
3211
3212
3213       if (rhs.type == SCALAR && lhs.type == SCALAR)
3214         {
3215           if (!do_simple_structure_copy (lhs, rhs, MIN (lhssize, rhssize)))
3216             {
3217               lhs.var = collapse_rest_of_var (lhs.var);
3218               rhs.var = collapse_rest_of_var (rhs.var);
3219               lhs.offset = 0;
3220               rhs.offset = 0;
3221               lhs.type = SCALAR;
3222               rhs.type = SCALAR;
3223               process_constraint (new_constraint (lhs, rhs));
3224             }
3225         }
3226       else if (lhs.type != DEREF && rhs.type == DEREF)
3227         do_rhs_deref_structure_copy (lhs, rhs, MIN (lhssize, rhssize));
3228       else if (lhs.type == DEREF && rhs.type != DEREF)
3229         do_lhs_deref_structure_copy (lhs, rhs, MIN (lhssize, rhssize));
3230       else
3231         {
3232           tree pointedtotype = lhstype;
3233           tree tmpvar;
3234
3235           gcc_assert (rhs.type == DEREF && lhs.type == DEREF);
3236           tmpvar = create_tmp_var_raw (pointedtotype, "structcopydereftmp");
3237           do_structure_copy (tmpvar, rhsop);
3238           do_structure_copy (lhsop, tmpvar);
3239         }
3240     }
3241 }
3242
3243
3244 /* Update related alias information kept in AI.  This is used when
3245    building name tags, alias sets and deciding grouping heuristics.
3246    STMT is the statement to process.  This function also updates
3247    ADDRESSABLE_VARS.  */
3248
3249 static void
3250 update_alias_info (tree stmt, struct alias_info *ai)
3251 {
3252   bitmap addr_taken;
3253   use_operand_p use_p;
3254   ssa_op_iter iter;
3255   bool stmt_dereferences_ptr_p;
3256   enum escape_type stmt_escape_type = is_escape_site (stmt);
3257   struct mem_ref_stats_d *mem_ref_stats = gimple_mem_ref_stats (cfun);
3258
3259   stmt_dereferences_ptr_p = false;
3260
3261   if (stmt_escape_type == ESCAPE_TO_CALL
3262       || stmt_escape_type == ESCAPE_TO_PURE_CONST)
3263     {
3264       mem_ref_stats->num_call_sites++;
3265       if (stmt_escape_type == ESCAPE_TO_PURE_CONST)
3266         mem_ref_stats->num_pure_const_call_sites++;
3267     }
3268   else if (stmt_escape_type == ESCAPE_TO_ASM)
3269     mem_ref_stats->num_asm_sites++;
3270
3271   /* Mark all the variables whose address are taken by the statement.  */
3272   addr_taken = addresses_taken (stmt);
3273   if (addr_taken)
3274     {
3275       bitmap_ior_into (gimple_addressable_vars (cfun), addr_taken);
3276
3277       /* If STMT is an escape point, all the addresses taken by it are
3278          call-clobbered.  */
3279       if (stmt_escape_type != NO_ESCAPE)
3280         {
3281           bitmap_iterator bi;
3282           unsigned i;
3283
3284           EXECUTE_IF_SET_IN_BITMAP (addr_taken, 0, i, bi)
3285             {
3286               tree rvar = referenced_var (i);
3287               if (!unmodifiable_var_p (rvar))
3288                 mark_call_clobbered (rvar, stmt_escape_type);
3289             }
3290         }
3291     }
3292
3293   /* Process each operand use.  For pointers, determine whether they
3294      are dereferenced by the statement, or whether their value
3295      escapes, etc.  */
3296   FOR_EACH_PHI_OR_STMT_USE (use_p, stmt, iter, SSA_OP_USE)
3297     {
3298       tree op, var;
3299       var_ann_t v_ann;
3300       struct ptr_info_def *pi;
3301       unsigned num_uses, num_loads, num_stores;
3302
3303       op = USE_FROM_PTR (use_p);
3304
3305       /* If STMT is a PHI node, OP may be an ADDR_EXPR.  If so, add it
3306          to the set of addressable variables.  */
3307       if (TREE_CODE (op) == ADDR_EXPR)
3308         {
3309           bitmap addressable_vars = gimple_addressable_vars (cfun);
3310
3311           gcc_assert (TREE_CODE (stmt) == PHI_NODE);
3312           gcc_assert (addressable_vars);
3313
3314           /* PHI nodes don't have annotations for pinning the set
3315              of addresses taken, so we collect them here.
3316
3317              FIXME, should we allow PHI nodes to have annotations
3318              so that they can be treated like regular statements?
3319              Currently, they are treated as second-class
3320              statements.  */
3321           add_to_addressable_set (TREE_OPERAND (op, 0), &addressable_vars);
3322           continue;
3323         }
3324
3325       /* Ignore constants (they may occur in PHI node arguments).  */
3326       if (TREE_CODE (op) != SSA_NAME)
3327         continue;
3328
3329       var = SSA_NAME_VAR (op);
3330       v_ann = var_ann (var);
3331
3332       /* The base variable of an SSA name must be a GIMPLE register, and thus
3333          it cannot be aliased.  */
3334       gcc_assert (!may_be_aliased (var));
3335
3336       /* We are only interested in pointers.  */
3337       if (!POINTER_TYPE_P (TREE_TYPE (op)))
3338         continue;
3339
3340       pi = get_ptr_info (op);
3341
3342       /* Add OP to AI->PROCESSED_PTRS, if it's not there already.  */
3343       if (!TEST_BIT (ai->ssa_names_visited, SSA_NAME_VERSION (op)))
3344         {
3345           SET_BIT (ai->ssa_names_visited, SSA_NAME_VERSION (op));
3346           VEC_safe_push (tree, heap, ai->processed_ptrs, op);
3347         }
3348
3349       /* If STMT is a PHI node, then it will not have pointer
3350          dereferences and it will not be an escape point.  */
3351       if (TREE_CODE (stmt) == PHI_NODE)
3352         continue;
3353
3354       /* Determine whether OP is a dereferenced pointer, and if STMT
3355          is an escape point, whether OP escapes.  */
3356       count_uses_and_derefs (op, stmt, &num_uses, &num_loads, &num_stores);
3357
3358       /* For directly dereferenced pointers we can apply
3359          TBAA-pruning to their points-to set.  We may not count the
3360          implicit dereferences &PTR->FLD here.  */
3361       if (num_loads + num_stores > 0)
3362         pi->is_dereferenced = 1;
3363
3364       /* Handle a corner case involving address expressions of the
3365          form '&PTR->FLD'.  The problem with these expressions is that
3366          they do not represent a dereference of PTR.  However, if some
3367          other transformation propagates them into an INDIRECT_REF
3368          expression, we end up with '*(&PTR->FLD)' which is folded
3369          into 'PTR->FLD'.
3370
3371          So, if the original code had no other dereferences of PTR,
3372          the aliaser will not create memory tags for it, and when
3373          &PTR->FLD gets propagated to INDIRECT_REF expressions, the
3374          memory operations will receive no VDEF/VUSE operands.
3375
3376          One solution would be to have count_uses_and_derefs consider
3377          &PTR->FLD a dereference of PTR.  But that is wrong, since it
3378          is not really a dereference but an offset calculation.
3379
3380          What we do here is to recognize these special ADDR_EXPR
3381          nodes.  Since these expressions are never GIMPLE values (they
3382          are not GIMPLE invariants), they can only appear on the RHS
3383          of an assignment and their base address is always an
3384          INDIRECT_REF expression.  */
3385       if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT
3386           && TREE_CODE (GIMPLE_STMT_OPERAND (stmt, 1)) == ADDR_EXPR
3387           && !is_gimple_val (GIMPLE_STMT_OPERAND (stmt, 1)))
3388         {
3389           /* If the RHS if of the form &PTR->FLD and PTR == OP, then
3390              this represents a potential dereference of PTR.  */
3391           tree rhs = GIMPLE_STMT_OPERAND (stmt, 1);
3392           tree base = get_base_address (TREE_OPERAND (rhs, 0));
3393           if (TREE_CODE (base) == INDIRECT_REF
3394               && TREE_OPERAND (base, 0) == op)
3395             num_loads++;
3396         }
3397
3398       if (num_loads + num_stores > 0)
3399         {
3400           /* Mark OP as dereferenced.  In a subsequent pass,
3401              dereferenced pointers that point to a set of
3402              variables will be assigned a name tag to alias
3403              all the variables OP points to.  */
3404           pi->memory_tag_needed = 1;
3405
3406           /* ???  For always executed direct dereferences we can
3407              apply TBAA-pruning to their escape set.  */
3408
3409           /* If this is a store operation, mark OP as being
3410              dereferenced to store, otherwise mark it as being
3411              dereferenced to load.  */
3412           if (num_stores > 0)
3413             pointer_set_insert (ai->dereferenced_ptrs_store, var);
3414           else
3415             pointer_set_insert (ai->dereferenced_ptrs_load, var);
3416
3417           /* Update the frequency estimate for all the dereferences of
3418              pointer OP.  */
3419           update_mem_sym_stats_from_stmt (op, stmt, num_loads, num_stores);
3420
3421           /* Indicate that STMT contains pointer dereferences.  */
3422           stmt_dereferences_ptr_p = true;
3423         }
3424
3425       if (stmt_escape_type != NO_ESCAPE && num_loads + num_stores < num_uses)
3426         {
3427           /* If STMT is an escape point and STMT contains at
3428              least one direct use of OP, then the value of OP
3429              escapes and so the pointed-to variables need to
3430              be marked call-clobbered.  */
3431           pi->value_escapes_p = 1;
3432           pi->escape_mask |= stmt_escape_type;
3433
3434           /* If the statement makes a function call, assume
3435              that pointer OP will be dereferenced in a store
3436              operation inside the called function.  */
3437           if (get_call_expr_in (stmt)
3438               || stmt_escape_type == ESCAPE_STORED_IN_GLOBAL)
3439             {
3440               pointer_set_insert (ai->dereferenced_ptrs_store, var);
3441               pi->memory_tag_needed = 1;
3442             }
3443         }
3444     }
3445
3446   if (TREE_CODE (stmt) == PHI_NODE)
3447     return;
3448
3449   /* Mark stored variables in STMT as being written to and update the
3450      memory reference stats for all memory symbols referenced by STMT.  */
3451   if (stmt_references_memory_p (stmt))
3452     {
3453       unsigned i;
3454       bitmap_iterator bi;
3455
3456       mem_ref_stats->num_mem_stmts++;
3457
3458       /* Notice that we only update memory reference stats for symbols
3459          loaded and stored by the statement if the statement does not
3460          contain pointer dereferences and it is not a call/asm site.
3461          This is to avoid double accounting problems when creating
3462          memory partitions.  After computing points-to information,
3463          pointer dereference statistics are used to update the
3464          reference stats of the pointed-to variables, so here we
3465          should only update direct references to symbols.
3466
3467          Indirect references are not updated here for two reasons: (1)
3468          The first time we compute alias information, the sets
3469          LOADED/STORED are empty for pointer dereferences, (2) After
3470          partitioning, LOADED/STORED may have references to
3471          partitions, not the original pointed-to variables.  So, if we
3472          always counted LOADED/STORED here and during partitioning, we
3473          would count many symbols more than once.
3474
3475          This does cause some imprecision when a statement has a
3476          combination of direct symbol references and pointer
3477          dereferences (e.g., MEMORY_VAR = *PTR) or if a call site has
3478          memory symbols in its argument list, but these cases do not
3479          occur so frequently as to constitute a serious problem.  */
3480       if (STORED_SYMS (stmt))
3481         EXECUTE_IF_SET_IN_BITMAP (STORED_SYMS (stmt), 0, i, bi)
3482           {
3483             tree sym = referenced_var (i);
3484             pointer_set_insert (ai->written_vars, sym);
3485             if (!stmt_dereferences_ptr_p
3486                 && stmt_escape_type != ESCAPE_TO_CALL
3487                 && stmt_escape_type != ESCAPE_TO_PURE_CONST
3488                 && stmt_escape_type != ESCAPE_TO_ASM)
3489               update_mem_sym_stats_from_stmt (sym, stmt, 0, 1);
3490           }
3491
3492       if (!stmt_dereferences_ptr_p
3493           && LOADED_SYMS (stmt)
3494           && stmt_escape_type != ESCAPE_TO_CALL
3495           && stmt_escape_type != ESCAPE_TO_PURE_CONST
3496           && stmt_escape_type != ESCAPE_TO_ASM)
3497         EXECUTE_IF_SET_IN_BITMAP (LOADED_SYMS (stmt), 0, i, bi)
3498           update_mem_sym_stats_from_stmt (referenced_var (i), stmt, 1, 0);
3499     }
3500 }
3501
3502
3503 /* Handle pointer arithmetic EXPR when creating aliasing constraints.
3504    Expressions of the type PTR + CST can be handled in two ways:
3505
3506    1- If the constraint for PTR is ADDRESSOF for a non-structure
3507       variable, then we can use it directly because adding or
3508       subtracting a constant may not alter the original ADDRESSOF
3509       constraint (i.e., pointer arithmetic may not legally go outside
3510       an object's boundaries).
3511
3512    2- If the constraint for PTR is ADDRESSOF for a structure variable,
3513       then if CST is a compile-time constant that can be used as an
3514       offset, we can determine which sub-variable will be pointed-to
3515       by the expression.
3516
3517    Return true if the expression is handled.  For any other kind of
3518    expression, return false so that each operand can be added as a
3519    separate constraint by the caller.  */
3520
3521 static bool
3522 handle_ptr_arith (VEC (ce_s, heap) *lhsc, tree expr)
3523 {
3524   tree op0, op1;
3525   struct constraint_expr *c, *c2;
3526   unsigned int i = 0;
3527   unsigned int j = 0;
3528   VEC (ce_s, heap) *temp = NULL;
3529   unsigned HOST_WIDE_INT rhsunitoffset, rhsoffset;
3530
3531   if (TREE_CODE (expr) != POINTER_PLUS_EXPR)
3532     return false;
3533
3534   op0 = TREE_OPERAND (expr, 0);
3535   op1 = TREE_OPERAND (expr, 1);
3536   gcc_assert (POINTER_TYPE_P (TREE_TYPE (op0)));
3537
3538   /* If the offset is not a non-negative integer constant that fits
3539      in a HOST_WIDE_INT, we cannot handle it here.  */
3540   if (!host_integerp (op1, 1))
3541     return false;
3542
3543   /* Make sure the bit-offset also fits.  */
3544   rhsunitoffset = TREE_INT_CST_LOW (op1);
3545   rhsoffset = rhsunitoffset * BITS_PER_UNIT;
3546   if (rhsunitoffset != rhsoffset / BITS_PER_UNIT)
3547     return false;
3548
3549   get_constraint_for (op0, &temp);
3550
3551   for (i = 0; VEC_iterate (ce_s, lhsc, i, c); i++)
3552     for (j = 0; VEC_iterate (ce_s, temp, j, c2); j++)
3553       {
3554         if (c2->type == ADDRESSOF && rhsoffset != 0)
3555           {
3556             varinfo_t temp = get_varinfo (c2->var);
3557
3558             /* An access one after the end of an array is valid,
3559                so simply punt on accesses we cannot resolve.  */
3560             temp = first_vi_for_offset (temp, rhsoffset);
3561             if (temp == NULL)
3562               continue;
3563             c2->var = temp->id;
3564             c2->offset = 0;
3565           }
3566         else
3567           c2->offset = rhsoffset;
3568         process_constraint (new_constraint (*c, *c2));
3569       }
3570
3571   VEC_free (ce_s, heap, temp);
3572
3573   return true;
3574 }
3575
3576 /* For non-IPA mode, generate constraints necessary for a call on the
3577    RHS.  */
3578
3579 static void
3580 handle_rhs_call  (tree rhs)
3581 {
3582   tree arg;
3583   call_expr_arg_iterator iter;
3584   struct constraint_expr rhsc;
3585
3586   rhsc.var = anything_id;
3587   rhsc.offset = 0;
3588   rhsc.type = ADDRESSOF;
3589
3590   FOR_EACH_CALL_EXPR_ARG (arg, iter, rhs)
3591     {
3592       VEC(ce_s, heap) *lhsc = NULL;
3593
3594       /* Find those pointers being passed, and make sure they end up
3595          pointing to anything.  */
3596       if (POINTER_TYPE_P (TREE_TYPE (arg)))
3597         {
3598           unsigned int j;
3599           struct constraint_expr *lhsp;
3600
3601           get_constraint_for (arg, &lhsc);
3602           do_deref (&lhsc);
3603           for (j = 0; VEC_iterate (ce_s, lhsc, j, lhsp); j++)
3604             process_constraint_1 (new_constraint (*lhsp, rhsc), true);
3605           VEC_free (ce_s, heap, lhsc);
3606         }
3607     }
3608 }
3609
3610 /* For non-IPA mode, generate constraints necessary for a call
3611    that returns a pointer and assigns it to LHS.  This simply makes
3612    the LHS point to anything.  */
3613
3614 static void
3615 handle_lhs_call (tree lhs)
3616 {
3617   VEC(ce_s, heap) *lhsc = NULL;
3618   struct constraint_expr rhsc;
3619   unsigned int j;
3620   struct constraint_expr *lhsp;
3621
3622   rhsc.var = anything_id;
3623   rhsc.offset = 0;
3624   rhsc.type = ADDRESSOF;
3625   get_constraint_for (lhs, &lhsc);
3626   for (j = 0; VEC_iterate (ce_s, lhsc, j, lhsp); j++)
3627     process_constraint_1 (new_constraint (*lhsp, rhsc), true);
3628   VEC_free (ce_s, heap, lhsc);
3629 }
3630
3631 /* Walk statement T setting up aliasing constraints according to the
3632    references found in T.  This function is the main part of the
3633    constraint builder.  AI points to auxiliary alias information used
3634    when building alias sets and computing alias grouping heuristics.  */
3635
3636 static void
3637 find_func_aliases (tree origt)
3638 {
3639   tree t = origt;
3640   VEC(ce_s, heap) *lhsc = NULL;
3641   VEC(ce_s, heap) *rhsc = NULL;
3642   struct constraint_expr *c;
3643
3644   if (TREE_CODE (t) == RETURN_EXPR && TREE_OPERAND (t, 0))
3645     t = TREE_OPERAND (t, 0);
3646
3647   /* Now build constraints expressions.  */
3648   if (TREE_CODE (t) == PHI_NODE)
3649     {
3650       gcc_assert (!AGGREGATE_TYPE_P (TREE_TYPE (PHI_RESULT (t))));
3651
3652       /* Only care about pointers and structures containing
3653          pointers.  */
3654       if (could_have_pointers (PHI_RESULT (t)))
3655         {
3656           int i;
3657           unsigned int j;
3658
3659           /* For a phi node, assign all the arguments to
3660              the result.  */
3661           get_constraint_for (PHI_RESULT (t), &lhsc);
3662           for (i = 0; i < PHI_NUM_ARGS (t); i++)
3663             {
3664               tree rhstype;
3665               tree strippedrhs = PHI_ARG_DEF (t, i);
3666
3667               STRIP_NOPS (strippedrhs);
3668               rhstype = TREE_TYPE (strippedrhs);
3669               get_constraint_for (PHI_ARG_DEF (t, i), &rhsc);
3670
3671               for (j = 0; VEC_iterate (ce_s, lhsc, j, c); j++)
3672                 {
3673                   struct constraint_expr *c2;
3674                   while (VEC_length (ce_s, rhsc) > 0)
3675                     {
3676                       c2 = VEC_last (ce_s, rhsc);
3677                       process_constraint (new_constraint (*c, *c2));
3678                       VEC_pop (ce_s, rhsc);
3679                     }
3680                 }
3681             }
3682         }
3683     }
3684   /* In IPA mode, we need to generate constraints to pass call
3685      arguments through their calls.   There are two cases, either a
3686      GIMPLE_MODIFY_STMT when we are returning a value, or just a plain
3687      CALL_EXPR when we are not.
3688
3689      In non-ipa mode, we need to generate constraints for each
3690      pointer passed by address.  */
3691   else if (((TREE_CODE (t) == GIMPLE_MODIFY_STMT
3692              && TREE_CODE (GIMPLE_STMT_OPERAND (t, 1)) == CALL_EXPR
3693              && !(call_expr_flags (GIMPLE_STMT_OPERAND (t, 1))
3694                   & (ECF_MALLOC | ECF_MAY_BE_ALLOCA)))
3695             || (TREE_CODE (t) == CALL_EXPR
3696                 && !(call_expr_flags (t)
3697                      & (ECF_MALLOC | ECF_MAY_BE_ALLOCA)))))
3698     {
3699       if (!in_ipa_mode)
3700         {
3701           if (TREE_CODE (t) == GIMPLE_MODIFY_STMT)
3702             {
3703               handle_rhs_call (GIMPLE_STMT_OPERAND (t, 1));
3704               if (could_have_pointers (GIMPLE_STMT_OPERAND (t, 1)))
3705                 handle_lhs_call (GIMPLE_STMT_OPERAND (t, 0));
3706             }
3707           else
3708             handle_rhs_call (t);
3709         }
3710       else
3711         {
3712           tree lhsop;
3713           tree rhsop;
3714           tree arg;
3715           call_expr_arg_iterator iter;
3716           varinfo_t fi;
3717           int i = 1;
3718           tree decl;
3719           if (TREE_CODE (t) == GIMPLE_MODIFY_STMT)
3720             {
3721               lhsop = GIMPLE_STMT_OPERAND (t, 0);
3722               rhsop = GIMPLE_STMT_OPERAND (t, 1);
3723             }
3724           else
3725             {
3726               lhsop = NULL;
3727               rhsop = t;
3728             }
3729           decl = get_callee_fndecl (rhsop);
3730
3731           /* If we can directly resolve the function being called, do so.
3732              Otherwise, it must be some sort of indirect expression that
3733              we should still be able to handle.  */
3734           if (decl)
3735             {
3736               fi = get_vi_for_tree (decl);
3737             }
3738           else
3739             {
3740               decl = CALL_EXPR_FN (rhsop);
3741               fi = get_vi_for_tree (decl);
3742             }
3743
3744           /* Assign all the passed arguments to the appropriate incoming
3745              parameters of the function.  */
3746
3747           FOR_EACH_CALL_EXPR_ARG (arg, iter, rhsop)
3748             {
3749               struct constraint_expr lhs ;
3750               struct constraint_expr *rhsp;
3751
3752               get_constraint_for (arg, &rhsc);
3753               if (TREE_CODE (decl) != FUNCTION_DECL)
3754                 {
3755                   lhs.type = DEREF;
3756                   lhs.var = fi->id;
3757                   lhs.offset = i;
3758                 }
3759               else
3760                 {
3761                   lhs.type = SCALAR;
3762                   lhs.var = first_vi_for_offset (fi, i)->id;
3763                   lhs.offset = 0;
3764                 }
3765               while (VEC_length (ce_s, rhsc) != 0)
3766                 {
3767                   rhsp = VEC_last (ce_s, rhsc);
3768                   process_constraint (new_constraint (lhs, *rhsp));
3769                   VEC_pop (ce_s, rhsc);
3770                 }
3771               i++;
3772             }
3773
3774           /* If we are returning a value, assign it to the result.  */
3775           if (lhsop)
3776             {
3777               struct constraint_expr rhs;
3778               struct constraint_expr *lhsp;
3779               unsigned int j = 0;
3780
3781               get_constraint_for (lhsop, &lhsc);
3782               if (TREE_CODE (decl) != FUNCTION_DECL)
3783                 {
3784                   rhs.type = DEREF;
3785                   rhs.var = fi->id;
3786                   rhs.offset = i;
3787                 }
3788               else
3789                 {
3790                   rhs.type = SCALAR;
3791                   rhs.var = first_vi_for_offset (fi, i)->id;
3792                   rhs.offset = 0;
3793                 }
3794               for (j = 0; VEC_iterate (ce_s, lhsc, j, lhsp); j++)
3795                 process_constraint (new_constraint (*lhsp, rhs));
3796             }
3797         }
3798     }
3799   /* Otherwise, just a regular assignment statement.  */
3800   else if (TREE_CODE (t) == GIMPLE_MODIFY_STMT)
3801     {
3802       tree lhsop = GIMPLE_STMT_OPERAND (t, 0);
3803       tree rhsop = GIMPLE_STMT_OPERAND (t, 1);
3804       int i;
3805
3806       if ((AGGREGATE_TYPE_P (TREE_TYPE (lhsop))
3807            || TREE_CODE (TREE_TYPE (lhsop)) == COMPLEX_TYPE)
3808           && (AGGREGATE_TYPE_P (TREE_TYPE (rhsop))
3809               || TREE_CODE (TREE_TYPE (lhsop)) == COMPLEX_TYPE))
3810         {
3811           do_structure_copy (lhsop, rhsop);
3812         }
3813       else
3814         {
3815           /* Only care about operations with pointers, structures
3816              containing pointers, dereferences, and call expressions.  */
3817           if (could_have_pointers (lhsop)
3818               || TREE_CODE (rhsop) == CALL_EXPR)
3819             {
3820               get_constraint_for (lhsop, &lhsc);
3821               switch (TREE_CODE_CLASS (TREE_CODE (rhsop)))
3822                 {
3823                   /* RHS that consist of unary operations,
3824                      exceptional types, or bare decls/constants, get
3825                      handled directly by get_constraint_for.  */
3826                   case tcc_reference:
3827                   case tcc_declaration:
3828                   case tcc_constant:
3829                   case tcc_exceptional:
3830                   case tcc_expression:
3831                   case tcc_vl_exp:
3832                   case tcc_unary:
3833                       {
3834                         unsigned int j;
3835
3836                         get_constraint_for (rhsop, &rhsc);
3837                         for (j = 0; VEC_iterate (ce_s, lhsc, j, c); j++)
3838                           {
3839                             struct constraint_expr *c2;
3840                             unsigned int k;
3841
3842                             for (k = 0; VEC_iterate (ce_s, rhsc, k, c2); k++)
3843                               process_constraint (new_constraint (*c, *c2));
3844                           }
3845
3846                       }
3847                     break;
3848
3849                   case tcc_binary:
3850                       {
3851                         /* For pointer arithmetic of the form
3852                            PTR + CST, we can simply use PTR's
3853                            constraint because pointer arithmetic is
3854                            not allowed to go out of bounds.  */
3855                         if (handle_ptr_arith (lhsc, rhsop))
3856                           break;
3857                       }
3858                     /* FALLTHRU  */
3859
3860                   /* Otherwise, walk each operand.  Notice that we
3861                      can't use the operand interface because we need
3862                      to process expressions other than simple operands
3863                      (e.g. INDIRECT_REF, ADDR_EXPR, CALL_EXPR).  */
3864                   default:
3865                     for (i = 0; i < TREE_OPERAND_LENGTH (rhsop); i++)
3866                       {
3867                         tree op = TREE_OPERAND (rhsop, i);
3868                         unsigned int j;
3869
3870                         gcc_assert (VEC_length (ce_s, rhsc) == 0);
3871                         get_constraint_for (op, &rhsc);
3872                         for (j = 0; VEC_iterate (ce_s, lhsc, j, c); j++)
3873                           {
3874                             struct constraint_expr *c2;
3875                             while (VEC_length (ce_s, rhsc) > 0)
3876                               {
3877                                 c2 = VEC_last (ce_s, rhsc);
3878                                 process_constraint (new_constraint (*c, *c2));
3879                                 VEC_pop (ce_s, rhsc);
3880                               }
3881                           }
3882                       }
3883                 }
3884             }
3885         }
3886     }
3887   else if (TREE_CODE (t) == CHANGE_DYNAMIC_TYPE_EXPR)
3888     {
3889       unsigned int j;
3890
3891       get_constraint_for (CHANGE_DYNAMIC_TYPE_LOCATION (t), &lhsc);
3892       for (j = 0; VEC_iterate (ce_s, lhsc, j, c); ++j)
3893         get_varinfo (c->var)->no_tbaa_pruning = true;
3894     }
3895
3896   /* After promoting variables and computing aliasing we will
3897      need to re-scan most statements.  FIXME: Try to minimize the
3898      number of statements re-scanned.  It's not really necessary to
3899      re-scan *all* statements.  */
3900   mark_stmt_modified (origt);
3901   VEC_free (ce_s, heap, rhsc);
3902   VEC_free (ce_s, heap, lhsc);
3903 }
3904
3905
3906 /* Find the first varinfo in the same variable as START that overlaps with
3907    OFFSET.
3908    Effectively, walk the chain of fields for the variable START to find the
3909    first field that overlaps with OFFSET.
3910    Return NULL if we can't find one.  */
3911
3912 static varinfo_t
3913 first_vi_for_offset (varinfo_t start, unsigned HOST_WIDE_INT offset)
3914 {
3915   varinfo_t curr = start;
3916   while (curr)
3917     {
3918       /* We may not find a variable in the field list with the actual
3919          offset when when we have glommed a structure to a variable.
3920          In that case, however, offset should still be within the size
3921          of the variable. */
3922       if (offset >= curr->offset && offset < (curr->offset +  curr->size))
3923         return curr;
3924       curr = curr->next;
3925     }
3926   return NULL;
3927 }
3928
3929
3930 /* Insert the varinfo FIELD into the field list for BASE, at the front
3931    of the list.  */
3932
3933 static void
3934 insert_into_field_list (varinfo_t base, varinfo_t field)
3935 {
3936   varinfo_t prev = base;
3937   varinfo_t curr = base->next;
3938
3939   field->next = curr;
3940   prev->next = field;
3941 }
3942
3943 /* Insert the varinfo FIELD into the field list for BASE, ordered by
3944    offset.  */
3945
3946 static void
3947 insert_into_field_list_sorted (varinfo_t base, varinfo_t field)
3948 {
3949   varinfo_t prev = base;
3950   varinfo_t curr = base->next;
3951
3952   if (curr == NULL)
3953     {
3954       prev->next = field;
3955       field->next = NULL;
3956     }
3957   else
3958     {
3959       while (curr)
3960         {
3961           if (field->offset <= curr->offset)
3962             break;
3963           prev = curr;
3964           curr = curr->next;
3965         }
3966       field->next = prev->next;
3967       prev->next = field;
3968     }
3969 }
3970
3971 /* This structure is used during pushing fields onto the fieldstack
3972    to track the offset of the field, since bitpos_of_field gives it
3973    relative to its immediate containing type, and we want it relative
3974    to the ultimate containing object.  */
3975
3976 struct fieldoff
3977 {
3978   /* Type of the field.  */
3979   tree type;
3980
3981   /* Size, in bits, of the field.  */
3982   tree size;
3983
3984   /* Field.  */
3985   tree decl;
3986
3987   /* Offset from the base of the base containing object to this field.  */
3988   HOST_WIDE_INT offset;  
3989 };
3990 typedef struct fieldoff fieldoff_s;
3991
3992 DEF_VEC_O(fieldoff_s);
3993 DEF_VEC_ALLOC_O(fieldoff_s,heap);
3994
3995 /* qsort comparison function for two fieldoff's PA and PB */
3996
3997 static int
3998 fieldoff_compare (const void *pa, const void *pb)
3999 {
4000   const fieldoff_s *foa = (const fieldoff_s *)pa;
4001   const fieldoff_s *fob = (const fieldoff_s *)pb;
4002   HOST_WIDE_INT foasize, fobsize;
4003
4004   if (foa->offset != fob->offset)
4005     return foa->offset - fob->offset;
4006
4007   foasize = TREE_INT_CST_LOW (foa->size);
4008   fobsize = TREE_INT_CST_LOW (fob->size);
4009   return foasize - fobsize;
4010 }
4011
4012 /* Sort a fieldstack according to the field offset and sizes.  */
4013 static void
4014 sort_fieldstack (VEC(fieldoff_s,heap) *fieldstack)
4015 {
4016   qsort (VEC_address (fieldoff_s, fieldstack),
4017          VEC_length (fieldoff_s, fieldstack),
4018          sizeof (fieldoff_s),
4019          fieldoff_compare);
4020 }
4021
4022 /* Return true if V is a tree that we can have subvars for.
4023    Normally, this is any aggregate type.  Also complex
4024    types which are not gimple registers can have subvars.  */
4025
4026 static inline bool
4027 var_can_have_subvars (const_tree v)
4028 {
4029   /* Volatile variables should never have subvars.  */
4030   if (TREE_THIS_VOLATILE (v))
4031     return false;
4032
4033   /* Non decls or memory tags can never have subvars.  */
4034   if (!DECL_P (v) || MTAG_P (v))
4035     return false;
4036
4037   /* Aggregates without overlapping fields can have subvars.  */
4038   if (TREE_CODE (TREE_TYPE (v)) == RECORD_TYPE)
4039     return true;
4040
4041   return false;
4042 }
4043
4044 /* Given a TYPE, and a vector of field offsets FIELDSTACK, push all
4045    the fields of TYPE onto fieldstack, recording their offsets along
4046    the way.
4047
4048    OFFSET is used to keep track of the offset in this entire
4049    structure, rather than just the immediately containing structure.
4050    Returns the number of fields pushed.
4051
4052    HAS_UNION is set to true if we find a union type as a field of
4053    TYPE.  */
4054
4055 static int
4056 push_fields_onto_fieldstack (tree type, VEC(fieldoff_s,heap) **fieldstack,
4057                              HOST_WIDE_INT offset, bool *has_union)
4058 {
4059   tree field;
4060   int count = 0;
4061
4062   if (TREE_CODE (type) != RECORD_TYPE)
4063     return 0;
4064
4065   /* If the vector of fields is growing too big, bail out early.
4066      Callers check for VEC_length <= MAX_FIELDS_FOR_FIELD_SENSITIVE, make
4067      sure this fails.  */
4068   if (VEC_length (fieldoff_s, *fieldstack) > MAX_FIELDS_FOR_FIELD_SENSITIVE)
4069     return 0;
4070
4071   for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
4072     if (TREE_CODE (field) == FIELD_DECL)
4073       {
4074         bool push = false;
4075         int pushed = 0;
4076
4077         if (has_union
4078             && (TREE_CODE (TREE_TYPE (field)) == QUAL_UNION_TYPE
4079                 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
4080           *has_union = true;
4081
4082         if (!var_can_have_subvars (field))
4083           push = true;
4084         else if (!(pushed = push_fields_onto_fieldstack
4085                    (TREE_TYPE (field),
4086                     fieldstack,
4087                     offset + bitpos_of_field (field),
4088                     has_union))
4089                  && (DECL_SIZE (field)
4090                      && !integer_zerop (DECL_SIZE (field))))
4091           /* Empty structures may have actual size, like in C++.  So
4092              see if we didn't push any subfields and the size is
4093              nonzero, push the field onto the stack.  */
4094           push = true;
4095
4096         if (push)
4097           {
4098             fieldoff_s *pair;
4099
4100             pair = VEC_safe_push (fieldoff_s, heap, *fieldstack, NULL);
4101             pair->type = TREE_TYPE (field);
4102             pair->size = DECL_SIZE (field);
4103             pair->decl = field;
4104             pair->offset = offset + bitpos_of_field (field);
4105             count++;
4106           }
4107         else
4108           count += pushed;
4109       }
4110
4111   return count;
4112 }
4113
4114 /* Create a constraint from ANYTHING variable to VI.  */
4115 static void
4116 make_constraint_from_anything (varinfo_t vi)
4117 {
4118   struct constraint_expr lhs, rhs;
4119
4120   lhs.var = vi->id;
4121   lhs.offset = 0;
4122   lhs.type = SCALAR;
4123
4124   rhs.var = anything_id;
4125   rhs.offset = 0;
4126   rhs.type = ADDRESSOF;
4127   process_constraint (new_constraint (lhs, rhs));
4128 }
4129
4130 /* Count the number of arguments DECL has, and set IS_VARARGS to true
4131    if it is a varargs function.  */
4132
4133 static unsigned int
4134 count_num_arguments (tree decl, bool *is_varargs)
4135 {
4136   unsigned int i = 0;
4137   tree t;
4138
4139   for (t = TYPE_ARG_TYPES (TREE_TYPE (decl));
4140        t;
4141        t = TREE_CHAIN (t))
4142     {
4143       if (TREE_VALUE (t) == void_type_node)
4144         break;
4145       i++;
4146     }
4147
4148   if (!t)
4149     *is_varargs = true;
4150   return i;
4151 }
4152
4153 /* Creation function node for DECL, using NAME, and return the index
4154    of the variable we've created for the function.  */
4155
4156 static unsigned int
4157 create_function_info_for (tree decl, const char *name)
4158 {
4159   unsigned int index = VEC_length (varinfo_t, varmap);
4160   varinfo_t vi;
4161   tree arg;
4162   unsigned int i;
4163   bool is_varargs = false;
4164
4165   /* Create the variable info.  */
4166
4167   vi = new_var_info (decl, index, name);
4168   vi->decl = decl;
4169   vi->offset = 0;
4170   vi->has_union = 0;
4171   vi->size = 1;
4172   vi->fullsize = count_num_arguments (decl, &is_varargs) + 1;
4173   insert_vi_for_tree (vi->decl, vi);
4174   VEC_safe_push (varinfo_t, heap, varmap, vi);
4175
4176   stats.total_vars++;
4177
4178   /* If it's varargs, we don't know how many arguments it has, so we
4179      can't do much.
4180   */
4181   if (is_varargs)
4182     {
4183       vi->fullsize = ~0;
4184       vi->size = ~0;
4185       vi->is_unknown_size_var = true;
4186       return index;
4187     }
4188
4189
4190   arg = DECL_ARGUMENTS (decl);
4191
4192   /* Set up variables for each argument.  */
4193   for (i = 1; i < vi->fullsize; i++)
4194     {
4195       varinfo_t argvi;
4196       const char *newname;
4197       char *tempname;
4198       unsigned int newindex;
4199       tree argdecl = decl;
4200
4201       if (arg)
4202         argdecl = arg;
4203
4204       newindex = VEC_length (varinfo_t, varmap);
4205       asprintf (&tempname, "%s.arg%d", name, i-1);
4206       newname = ggc_strdup (tempname);
4207       free (tempname);
4208
4209       argvi = new_var_info (argdecl, newindex, newname);
4210       argvi->decl = argdecl;
4211       VEC_safe_push (varinfo_t, heap, varmap, argvi);
4212       argvi->offset = i;
4213       argvi->size = 1;
4214       argvi->fullsize = vi->fullsize;
4215       argvi->has_union = false;
4216       insert_into_field_list_sorted (vi, argvi);
4217       stats.total_vars ++;
4218       if (arg)
4219         {
4220           insert_vi_for_tree (arg, argvi);
4221           arg = TREE_CHAIN (arg);
4222         }
4223     }
4224
4225   /* Create a variable for the return var.  */
4226   if (DECL_RESULT (decl) != NULL
4227       || !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4228     {
4229       varinfo_t resultvi;
4230       const char *newname;
4231       char *tempname;
4232       unsigned int newindex;
4233       tree resultdecl = decl;
4234
4235       vi->fullsize ++;
4236
4237       if (DECL_RESULT (decl))
4238         resultdecl = DECL_RESULT (decl);
4239
4240       newindex = VEC_length (varinfo_t, varmap);
4241       asprintf (&tempname, "%s.result", name);
4242       newname = ggc_strdup (tempname);
4243       free (tempname);
4244
4245       resultvi = new_var_info (resultdecl, newindex, newname);
4246       resultvi->decl = resultdecl;
4247       VEC_safe_push (varinfo_t, heap, varmap, resultvi);
4248       resultvi->offset = i;
4249       resultvi->size = 1;
4250       resultvi->fullsize = vi->fullsize;
4251       resultvi->has_union = false;
4252       insert_into_field_list_sorted (vi, resultvi);
4253       stats.total_vars ++;
4254       if (DECL_RESULT (decl))
4255         insert_vi_for_tree (DECL_RESULT (decl), resultvi);
4256     }
4257   return index;
4258 }
4259
4260
4261 /* Return true if FIELDSTACK contains fields that overlap.
4262    FIELDSTACK is assumed to be sorted by offset.  */
4263
4264 static bool
4265 check_for_overlaps (VEC (fieldoff_s,heap) *fieldstack)
4266 {
4267   fieldoff_s *fo = NULL;
4268   unsigned int i;
4269   HOST_WIDE_INT lastoffset = -1;
4270
4271   for (i = 0; VEC_iterate (fieldoff_s, fieldstack, i, fo); i++)
4272     {
4273       if (fo->offset == lastoffset)
4274         return true;
4275       lastoffset = fo->offset;
4276     }
4277   return false;
4278 }
4279
4280 /* Create a varinfo structure for NAME and DECL, and add it to VARMAP.
4281    This will also create any varinfo structures necessary for fields
4282    of DECL.  */
4283
4284 static unsigned int
4285 create_variable_info_for (tree decl, const char *name)
4286 {
4287   unsigned int index = VEC_length (varinfo_t, varmap);
4288   varinfo_t vi;
4289   tree decltype = TREE_TYPE (decl);
4290   tree declsize = DECL_P (decl) ? DECL_SIZE (decl) : TYPE_SIZE (decltype);
4291   bool notokay = false;
4292   bool hasunion;
4293   bool is_global = DECL_P (decl) ? is_global_var (decl) : false;
4294   VEC (fieldoff_s,heap) *fieldstack = NULL;
4295
4296   if (TREE_CODE (decl) == FUNCTION_DECL && in_ipa_mode)
4297     return create_function_info_for (decl, name);
4298
4299   hasunion = TREE_CODE (decltype) == UNION_TYPE
4300              || TREE_CODE (decltype) == QUAL_UNION_TYPE;
4301   if (var_can_have_subvars (decl) && use_field_sensitive && !hasunion)
4302     {
4303       push_fields_onto_fieldstack (decltype, &fieldstack, 0, &hasunion);
4304       if (hasunion)
4305         {
4306           VEC_free (fieldoff_s, heap, fieldstack);
4307           notokay = true;
4308         }
4309     }
4310
4311   /* If the variable doesn't have subvars, we may end up needing to
4312      sort the field list and create fake variables for all the
4313      fields.  */
4314   vi = new_var_info (decl, index, name);
4315   vi->decl = decl;
4316   vi->offset = 0;
4317   vi->has_union = hasunion;
4318   if (!declsize
4319       || TREE_CODE (declsize) != INTEGER_CST
4320       || TREE_CODE (decltype) == UNION_TYPE
4321       || TREE_CODE (decltype) == QUAL_UNION_TYPE)
4322     {
4323       vi->is_unknown_size_var = true;
4324       vi->fullsize = ~0;
4325       vi->size = ~0;
4326     }
4327   else
4328     {
4329       vi->fullsize = TREE_INT_CST_LOW (declsize);
4330       vi->size = vi->fullsize;
4331     }
4332
4333   insert_vi_for_tree (vi->decl, vi);
4334   VEC_safe_push (varinfo_t, heap, varmap, vi);
4335   if (is_global && (!flag_whole_program || !in_ipa_mode))
4336     make_constraint_from_anything (vi);
4337
4338   stats.total_vars++;
4339   if (use_field_sensitive
4340       && !notokay
4341       && !vi->is_unknown_size_var
4342       && var_can_have_subvars (decl)
4343       && VEC_length (fieldoff_s, fieldstack) > 1
4344       && VEC_length (fieldoff_s, fieldstack) <= MAX_FIELDS_FOR_FIELD_SENSITIVE)
4345     {
4346       unsigned int newindex = VEC_length (varinfo_t, varmap);
4347       fieldoff_s *fo = NULL;
4348       unsigned int i;
4349
4350       for (i = 0; !notokay && VEC_iterate (fieldoff_s, fieldstack, i, fo); i++)
4351         {
4352           if (! fo->size
4353               || TREE_CODE (fo->size) != INTEGER_CST
4354               || fo->offset < 0)
4355             {
4356               notokay = true;
4357               break;
4358             }
4359         }
4360
4361       /* We can't sort them if we have a field with a variable sized type,
4362          which will make notokay = true.  In that case, we are going to return
4363          without creating varinfos for the fields anyway, so sorting them is a
4364          waste to boot.  */
4365       if (!notokay)
4366         {
4367           sort_fieldstack (fieldstack);
4368           /* Due to some C++ FE issues, like PR 22488, we might end up
4369              what appear to be overlapping fields even though they,
4370              in reality, do not overlap.  Until the C++ FE is fixed,
4371              we will simply disable field-sensitivity for these cases.  */
4372           notokay = check_for_overlaps (fieldstack);
4373         }
4374
4375
4376       if (VEC_length (fieldoff_s, fieldstack) != 0)
4377         fo = VEC_index (fieldoff_s, fieldstack, 0);
4378
4379       if (fo == NULL || notokay)
4380         {
4381           vi->is_unknown_size_var = 1;
4382           vi->fullsize = ~0;
4383           vi->size = ~0;
4384           VEC_free (fieldoff_s, heap, fieldstack);
4385           return index;
4386         }
4387
4388       vi->size = TREE_INT_CST_LOW (fo->size);
4389       vi->offset = fo->offset;
4390       for (i = VEC_length (fieldoff_s, fieldstack) - 1;
4391            i >= 1 && VEC_iterate (fieldoff_s, fieldstack, i, fo);
4392            i--)
4393         {
4394           varinfo_t newvi;
4395           const char *newname = "NULL";
4396           char *tempname;
4397
4398           newindex = VEC_length (varinfo_t, varmap);
4399           if (dump_file)
4400             {
4401               if (fo->decl)
4402                 asprintf (&tempname, "%s.%s",
4403                           vi->name, alias_get_name (fo->decl));
4404               else
4405                 asprintf (&tempname, "%s." HOST_WIDE_INT_PRINT_DEC,
4406                           vi->name, fo->offset);
4407               newname = ggc_strdup (tempname);
4408               free (tempname);
4409             }
4410           newvi = new_var_info (decl, newindex, newname);
4411           newvi->offset = fo->offset;
4412           newvi->size = TREE_INT_CST_LOW (fo->size);
4413           newvi->fullsize = vi->fullsize;
4414           insert_into_field_list (vi, newvi);
4415           VEC_safe_push (varinfo_t, heap, varmap, newvi);
4416           if (is_global && (!flag_whole_program || !in_ipa_mode))
4417               make_constraint_from_anything (newvi);
4418
4419           stats.total_vars++;
4420         }
4421     }
4422
4423   VEC_free (fieldoff_s, heap, fieldstack);
4424
4425   return index;
4426 }
4427
4428 /* Print out the points-to solution for VAR to FILE.  */
4429
4430 void
4431 dump_solution_for_var (FILE *file, unsigned int var)
4432 {
4433   varinfo_t vi = get_varinfo (var);
4434   unsigned int i;
4435   bitmap_iterator bi;
4436
4437   if (find (var) != var)
4438     {
4439       varinfo_t vipt = get_varinfo (find (var));
4440       fprintf (file, "%s = same as %s\n", vi->name, vipt->name);
4441     }
4442   else
4443     {
4444       fprintf (file, "%s = { ", vi->name);
4445       EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, i, bi)
4446         {
4447           fprintf (file, "%s ", get_varinfo (i)->name);
4448         }
4449       fprintf (file, "}");
4450       if (vi->no_tbaa_pruning)
4451         fprintf (file, " no-tbaa-pruning");
4452       fprintf (file, "\n");
4453     }
4454 }
4455
4456 /* Print the points-to solution for VAR to stdout.  */
4457
4458 void
4459 debug_solution_for_var (unsigned int var)
4460 {
4461   dump_solution_for_var (stdout, var);
4462 }
4463
4464 /* Create varinfo structures for all of the variables in the
4465    function for intraprocedural mode.  */
4466
4467 static void
4468 intra_create_variable_infos (void)
4469 {
4470   tree t;
4471   struct constraint_expr lhs, rhs;
4472
4473   /* For each incoming pointer argument arg, create the constraint ARG
4474      = ANYTHING or a dummy variable if flag_argument_noalias is set.  */
4475   for (t = DECL_ARGUMENTS (current_function_decl); t; t = TREE_CHAIN (t))
4476     {
4477       varinfo_t p;
4478
4479       if (!could_have_pointers (t))
4480         continue;
4481
4482       /* If flag_argument_noalias is set, then function pointer
4483          arguments are guaranteed not to point to each other.  In that
4484          case, create an artificial variable PARM_NOALIAS and the
4485          constraint ARG = &PARM_NOALIAS.  */
4486       if (POINTER_TYPE_P (TREE_TYPE (t)) && flag_argument_noalias > 0)
4487         {
4488           varinfo_t vi;
4489           tree heapvar = heapvar_lookup (t);
4490
4491           lhs.offset = 0;
4492           lhs.type = SCALAR;
4493           lhs.var  = get_vi_for_tree (t)->id;
4494
4495           if (heapvar == NULL_TREE)
4496             {
4497               var_ann_t ann;
4498               heapvar = create_tmp_var_raw (TREE_TYPE (TREE_TYPE (t)),
4499                                             "PARM_NOALIAS");
4500               DECL_EXTERNAL (heapvar) = 1;
4501               if (gimple_referenced_vars (cfun))
4502                 add_referenced_var (heapvar);
4503
4504               heapvar_insert (t, heapvar);
4505
4506               ann = get_var_ann (heapvar);
4507               if (flag_argument_noalias == 1)
4508                 ann->noalias_state = NO_ALIAS;
4509               else if (flag_argument_noalias == 2)
4510                 ann->noalias_state = NO_ALIAS_GLOBAL;
4511               else if (flag_argument_noalias == 3)
4512                 ann->noalias_state = NO_ALIAS_ANYTHING;
4513               else
4514                 gcc_unreachable ();
4515             }
4516
4517           vi = get_vi_for_tree (heapvar);
4518           vi->is_artificial_var = 1;
4519           vi->is_heap_var = 1;
4520           rhs.var = vi->id;
4521           rhs.type = ADDRESSOF;
4522           rhs.offset = 0;
4523           for (p = get_varinfo (lhs.var); p; p = p->next)
4524             {
4525               struct constraint_expr temp = lhs;
4526               temp.var = p->id;
4527               process_constraint (new_constraint (temp, rhs));
4528             }
4529         }
4530       else
4531         {
4532           varinfo_t arg_vi = get_vi_for_tree (t);
4533
4534           for (p = arg_vi; p; p = p->next)
4535             make_constraint_from_anything (p);
4536         }
4537     }
4538 }
4539
4540 /* Structure used to put solution bitmaps in a hashtable so they can
4541    be shared among variables with the same points-to set.  */
4542
4543 typedef struct shared_bitmap_info
4544 {
4545   bitmap pt_vars;
4546   hashval_t hashcode;
4547 } *shared_bitmap_info_t;
4548 typedef const struct shared_bitmap_info *const_shared_bitmap_info_t;
4549
4550 static htab_t shared_bitmap_table;
4551
4552 /* Hash function for a shared_bitmap_info_t */
4553
4554 static hashval_t
4555 shared_bitmap_hash (const void *p)
4556 {
4557   const_shared_bitmap_info_t const bi = (const_shared_bitmap_info_t) p;
4558   return bi->hashcode;
4559 }
4560
4561 /* Equality function for two shared_bitmap_info_t's. */
4562
4563 static int
4564 shared_bitmap_eq (const void *p1, const void *p2)
4565 {
4566   const_shared_bitmap_info_t const sbi1 = (const_shared_bitmap_info_t) p1;
4567   const_shared_bitmap_info_t const sbi2 = (const_shared_bitmap_info_t) p2;
4568   return bitmap_equal_p (sbi1->pt_vars, sbi2->pt_vars);
4569 }
4570
4571 /* Lookup a bitmap in the shared bitmap hashtable, and return an already
4572    existing instance if there is one, NULL otherwise.  */
4573
4574 static bitmap
4575 shared_bitmap_lookup (bitmap pt_vars)
4576 {
4577   void **slot;
4578   struct shared_bitmap_info sbi;
4579
4580   sbi.pt_vars = pt_vars;
4581   sbi.hashcode = bitmap_hash (pt_vars);
4582
4583   slot = htab_find_slot_with_hash (shared_bitmap_table, &sbi,
4584                                    sbi.hashcode, NO_INSERT);
4585   if (!slot)
4586     return NULL;
4587   else
4588     return ((shared_bitmap_info_t) *slot)->pt_vars;
4589 }
4590
4591
4592 /* Add a bitmap to the shared bitmap hashtable.  */
4593
4594 static void
4595 shared_bitmap_add (bitmap pt_vars)
4596 {
4597   void **slot;
4598   shared_bitmap_info_t sbi = XNEW (struct shared_bitmap_info);
4599
4600   sbi->pt_vars = pt_vars;
4601   sbi->hashcode = bitmap_hash (pt_vars);
4602
4603   slot = htab_find_slot_with_hash (shared_bitmap_table, sbi,
4604                                    sbi->hashcode, INSERT);
4605   gcc_assert (!*slot);
4606   *slot = (void *) sbi;
4607 }
4608
4609
4610 /* Set bits in INTO corresponding to the variable uids in solution set
4611    FROM, which came from variable PTR.
4612    For variables that are actually dereferenced, we also use type
4613    based alias analysis to prune the points-to sets.
4614    IS_DEREFED is true if PTR was directly dereferenced, which we use to
4615    help determine whether we are we are allowed to prune using TBAA.
4616    If NO_TBAA_PRUNING is true, we do not perform any TBAA pruning of
4617    the from set.  */
4618
4619 static void
4620 set_uids_in_ptset (tree ptr, bitmap into, bitmap from, bool is_derefed,
4621                    bool no_tbaa_pruning)
4622 {
4623   unsigned int i;
4624   bitmap_iterator bi;
4625
4626   gcc_assert (POINTER_TYPE_P (TREE_TYPE (ptr)));
4627
4628   EXECUTE_IF_SET_IN_BITMAP (from, 0, i, bi)
4629     {
4630       varinfo_t vi = get_varinfo (i);
4631
4632       /* The only artificial variables that are allowed in a may-alias
4633          set are heap variables.  */
4634       if (vi->is_artificial_var && !vi->is_heap_var)
4635         continue;
4636
4637       if (TREE_CODE (vi->decl) == VAR_DECL
4638           || TREE_CODE (vi->decl) == PARM_DECL
4639           || TREE_CODE (vi->decl) == RESULT_DECL)
4640         {
4641           /* Just add VI->DECL to the alias set.
4642              Don't type prune artificial vars or points-to sets
4643              for pointers that have not been dereferenced or with
4644              type-based pruning disabled.  */
4645           if (vi->is_artificial_var
4646               || !is_derefed
4647               || no_tbaa_pruning)
4648             bitmap_set_bit (into, DECL_UID (vi->decl));
4649           else
4650             {
4651               alias_set_type var_alias_set, mem_alias_set;
4652               var_alias_set = get_alias_set (vi->decl);
4653               mem_alias_set = get_alias_set (TREE_TYPE (TREE_TYPE (ptr)));
4654               if (may_alias_p (SSA_NAME_VAR (ptr), mem_alias_set,
4655                                vi->decl, var_alias_set, true))
4656                 bitmap_set_bit (into, DECL_UID (vi->decl));
4657             }
4658         }
4659     }
4660 }
4661
4662
4663 static bool have_alias_info = false;
4664
4665 /* The list of SMT's that are in use by our pointer variables.  This
4666    is the set of SMT's for all pointers that can point to anything.   */
4667 static bitmap used_smts;
4668
4669 /* Due to the ordering of points-to set calculation and SMT
4670    calculation being a bit co-dependent, we can't just calculate SMT
4671    used info whenever we want, we have to calculate it around the time
4672    that find_what_p_points_to is called.  */
4673
4674 /* Mark which SMT's are in use by points-to anything variables.  */
4675
4676 void
4677 set_used_smts (void)
4678 {
4679   int i;
4680   varinfo_t vi;
4681   used_smts = BITMAP_ALLOC (&pta_obstack);
4682
4683   for (i = 0; VEC_iterate (varinfo_t, varmap, i, vi); i++)
4684     {
4685       tree var = vi->decl;
4686       varinfo_t withsolution = get_varinfo (find (i));
4687       tree smt;
4688       var_ann_t va;
4689       struct ptr_info_def *pi = NULL;
4690
4691       /* For parm decls, the pointer info may be under the default
4692          def.  */
4693       if (TREE_CODE (vi->decl) == PARM_DECL
4694           && gimple_default_def (cfun, var))
4695         pi = SSA_NAME_PTR_INFO (gimple_default_def (cfun, var));
4696       else if (TREE_CODE (var) == SSA_NAME)
4697         pi = SSA_NAME_PTR_INFO (var);
4698
4699       /* Skip the special variables and those that can't be aliased.  */
4700       if (vi->is_special_var
4701           || !SSA_VAR_P (var)
4702           || (pi && !pi->memory_tag_needed)
4703           || (TREE_CODE (var) == VAR_DECL && !may_be_aliased (var))
4704           || !POINTER_TYPE_P (TREE_TYPE (var)))
4705         continue;
4706
4707       if (TREE_CODE (var) == SSA_NAME)
4708         var = SSA_NAME_VAR (var);
4709
4710       va = var_ann (var);
4711       if (!va)
4712         continue;
4713
4714       smt = va->symbol_mem_tag;
4715       if (smt && bitmap_bit_p (withsolution->solution, anything_id))
4716         bitmap_set_bit (used_smts, DECL_UID (smt));
4717     }
4718 }
4719
4720 /* Given a pointer variable P, fill in its points-to set, or return
4721    false if we can't.
4722    Rather than return false for variables that point-to anything, we
4723    instead find the corresponding SMT, and merge in its aliases.  In
4724    addition to these aliases, we also set the bits for the SMT's
4725    themselves and their subsets, as SMT's are still in use by
4726    non-SSA_NAME's, and pruning may eliminate every one of their
4727    aliases.  In such a case, if we did not include the right set of
4728    SMT's in the points-to set of the variable, we'd end up with
4729    statements that do not conflict but should.  */
4730
4731 bool
4732 find_what_p_points_to (tree p)
4733 {
4734   tree lookup_p = p;
4735   varinfo_t vi;
4736
4737   if (!have_alias_info)
4738     return false;
4739
4740   /* For parameters, get at the points-to set for the actual parm
4741      decl.  */
4742   if (TREE_CODE (p) == SSA_NAME
4743       && TREE_CODE (SSA_NAME_VAR (p)) == PARM_DECL
4744       && SSA_NAME_IS_DEFAULT_DEF (p))
4745     lookup_p = SSA_NAME_VAR (p);
4746
4747   vi = lookup_vi_for_tree (lookup_p);
4748   if (vi)
4749     {
4750       if (vi->is_artificial_var)
4751         return false;
4752
4753       /* See if this is a field or a structure.  */
4754       if (vi->size != vi->fullsize)
4755         {
4756           /* Nothing currently asks about structure fields directly,
4757              but when they do, we need code here to hand back the
4758              points-to set.  */
4759           return false;
4760         }
4761       else
4762         {
4763           struct ptr_info_def *pi = get_ptr_info (p);
4764           unsigned int i;
4765           bitmap_iterator bi;
4766           bool was_pt_anything = false;
4767           bitmap finished_solution;
4768           bitmap result;
4769
4770           if (!pi->memory_tag_needed)
4771             return false;
4772
4773           /* This variable may have been collapsed, let's get the real
4774              variable.  */
4775           vi = get_varinfo (find (vi->id));
4776
4777           /* Translate artificial variables into SSA_NAME_PTR_INFO
4778              attributes.  */
4779           EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, i, bi)
4780             {
4781               varinfo_t vi = get_varinfo (i);
4782
4783               if (vi->is_artificial_var)
4784                 {
4785                   /* FIXME.  READONLY should be handled better so that
4786                      flow insensitive aliasing can disregard writable
4787                      aliases.  */
4788                   if (vi->id == nothing_id)
4789                     pi->pt_null = 1;
4790                   else if (vi->id == anything_id)
4791                     was_pt_anything = 1;
4792                   else if (vi->id == readonly_id)
4793                     was_pt_anything = 1;
4794                   else if (vi->id == integer_id)
4795                     was_pt_anything = 1;
4796                   else if (vi->is_heap_var)
4797                     pi->pt_global_mem = 1;
4798                 }
4799             }
4800
4801           /* Instead of doing extra work, simply do not create
4802              points-to information for pt_anything pointers.  This
4803              will cause the operand scanner to fall back to the
4804              type-based SMT and its aliases.  Which is the best
4805              we could do here for the points-to set as well.  */
4806           if (was_pt_anything)
4807             return false;
4808
4809           /* Share the final set of variables when possible.  */
4810           finished_solution = BITMAP_GGC_ALLOC ();
4811           stats.points_to_sets_created++;
4812
4813           set_uids_in_ptset (p, finished_solution, vi->solution,
4814                              pi->is_dereferenced,
4815                              vi->no_tbaa_pruning);
4816           result = shared_bitmap_lookup (finished_solution);
4817
4818           if (!result)
4819             {
4820               shared_bitmap_add (finished_solution);
4821               pi->pt_vars = finished_solution;
4822             }
4823           else
4824             {
4825               pi->pt_vars = result;
4826               bitmap_clear (finished_solution);
4827             }
4828
4829           if (bitmap_empty_p (pi->pt_vars))
4830             pi->pt_vars = NULL;
4831
4832           return true;
4833         }
4834     }
4835
4836   return false;
4837 }
4838
4839 /* Mark everything that p points to as call clobbered.  Returns true
4840    if everything is done and false if all addressable variables need to
4841    be clobbered because p points to anything.  */
4842
4843 bool
4844 clobber_what_p_points_to (tree p)
4845 {
4846   tree lookup_p = p;
4847   varinfo_t vi;
4848   struct ptr_info_def *pi;
4849   unsigned int i;
4850   bitmap_iterator bi;
4851
4852   if (!have_alias_info)
4853     return false;
4854
4855   /* For parameters, get at the points-to set for the actual parm
4856      decl.  */
4857   if (TREE_CODE (p) == SSA_NAME
4858       && TREE_CODE (SSA_NAME_VAR (p)) == PARM_DECL
4859       && SSA_NAME_IS_DEFAULT_DEF (p))
4860     lookup_p = SSA_NAME_VAR (p);
4861
4862   vi = lookup_vi_for_tree (lookup_p);
4863   if (!vi)
4864     return false;
4865
4866   /* We are asking for the points-to solution of pointers.  */
4867   gcc_assert (!vi->is_artificial_var
4868               && vi->size == vi->fullsize);
4869
4870   pi = get_ptr_info (p);
4871
4872   /* This variable may have been collapsed, let's get the real
4873      variable.  */
4874   vi = get_varinfo (find (vi->id));
4875
4876   /* Mark variables in the solution call-clobbered.  */
4877   EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, i, bi)
4878     {
4879       varinfo_t vi = get_varinfo (i);
4880
4881       if (vi->is_artificial_var)
4882         {
4883           /* nothing_id and readonly_id do not cause any
4884              call clobber ops.  For anything_id and integer_id
4885              we need to clobber all addressable vars.  */
4886           if (vi->id == anything_id
4887               || vi->id == integer_id)
4888             return false;
4889         }
4890
4891       /* Only artificial heap-vars are further interesting.  */
4892       if (vi->is_artificial_var && !vi->is_heap_var)
4893         continue;
4894
4895       if ((TREE_CODE (vi->decl) == VAR_DECL
4896            || TREE_CODE (vi->decl) == PARM_DECL
4897            || TREE_CODE (vi->decl) == RESULT_DECL)
4898           && !unmodifiable_var_p (vi->decl))
4899         mark_call_clobbered (vi->decl, pi->escape_mask);
4900     }
4901
4902   return true;
4903 }
4904
4905 /* Dump points-to information to OUTFILE.  */
4906
4907 void
4908 dump_sa_points_to_info (FILE *outfile)
4909 {
4910   unsigned int i;
4911
4912   fprintf (outfile, "\nPoints-to sets\n\n");
4913
4914   if (dump_flags & TDF_STATS)
4915     {
4916       fprintf (outfile, "Stats:\n");
4917       fprintf (outfile, "Total vars:               %d\n", stats.total_vars);
4918       fprintf (outfile, "Non-pointer vars:          %d\n",
4919                stats.nonpointer_vars);
4920       fprintf (outfile, "Statically unified vars:  %d\n",
4921                stats.unified_vars_static);
4922       fprintf (outfile, "Dynamically unified vars: %d\n",
4923                stats.unified_vars_dynamic);
4924       fprintf (outfile, "Iterations:               %d\n", stats.iterations);
4925       fprintf (outfile, "Number of edges:          %d\n", stats.num_edges);
4926       fprintf (outfile, "Number of implicit edges: %d\n",
4927                stats.num_implicit_edges);
4928     }
4929
4930   for (i = 0; i < VEC_length (varinfo_t, varmap); i++)
4931     dump_solution_for_var (outfile, i);
4932 }
4933
4934
4935 /* Debug points-to information to stderr.  */
4936
4937 void
4938 debug_sa_points_to_info (void)
4939 {
4940   dump_sa_points_to_info (stderr);
4941 }
4942
4943
4944 /* Initialize the always-existing constraint variables for NULL
4945    ANYTHING, READONLY, and INTEGER */
4946
4947 static void
4948 init_base_vars (void)
4949 {
4950   struct constraint_expr lhs, rhs;
4951
4952   /* Create the NULL variable, used to represent that a variable points
4953      to NULL.  */
4954   nothing_tree = create_tmp_var_raw (void_type_node, "NULL");
4955   var_nothing = new_var_info (nothing_tree, 0, "NULL");
4956   insert_vi_for_tree (nothing_tree, var_nothing);
4957   var_nothing->is_artificial_var = 1;
4958   var_nothing->offset = 0;
4959   var_nothing->size = ~0;
4960   var_nothing->fullsize = ~0;
4961   var_nothing->is_special_var = 1;
4962   nothing_id = 0;
4963   VEC_safe_push (varinfo_t, heap, varmap, var_nothing);
4964
4965   /* Create the ANYTHING variable, used to represent that a variable
4966      points to some unknown piece of memory.  */
4967   anything_tree = create_tmp_var_raw (void_type_node, "ANYTHING");
4968   var_anything = new_var_info (anything_tree, 1, "ANYTHING");
4969   insert_vi_for_tree (anything_tree, var_anything);
4970   var_anything->is_artificial_var = 1;
4971   var_anything->size = ~0;
4972   var_anything->offset = 0;
4973   var_anything->next = NULL;
4974   var_anything->fullsize = ~0;
4975   var_anything->is_special_var = 1;
4976   anything_id = 1;
4977
4978   /* Anything points to anything.  This makes deref constraints just
4979      work in the presence of linked list and other p = *p type loops,
4980      by saying that *ANYTHING = ANYTHING. */
4981   VEC_safe_push (varinfo_t, heap, varmap, var_anything);
4982   lhs.type = SCALAR;
4983   lhs.var = anything_id;
4984   lhs.offset = 0;
4985   rhs.type = ADDRESSOF;
4986   rhs.var = anything_id;
4987   rhs.offset = 0;
4988
4989   /* This specifically does not use process_constraint because
4990      process_constraint ignores all anything = anything constraints, since all
4991      but this one are redundant.  */
4992   VEC_safe_push (constraint_t, heap, constraints, new_constraint (lhs, rhs));
4993
4994   /* Create the READONLY variable, used to represent that a variable
4995      points to readonly memory.  */
4996   readonly_tree = create_tmp_var_raw (void_type_node, "READONLY");
4997   var_readonly = new_var_info (readonly_tree, 2, "READONLY");
4998   var_readonly->is_artificial_var = 1;
4999   var_readonly->offset = 0;
5000   var_readonly->size = ~0;
5001   var_readonly->fullsize = ~0;
5002   var_readonly->next = NULL;
5003   var_readonly->is_special_var = 1;
5004   insert_vi_for_tree (readonly_tree, var_readonly);
5005   readonly_id = 2;
5006   VEC_safe_push (varinfo_t, heap, varmap, var_readonly);
5007
5008   /* readonly memory points to anything, in order to make deref
5009      easier.  In reality, it points to anything the particular
5010      readonly variable can point to, but we don't track this
5011      separately. */
5012   lhs.type = SCALAR;
5013   lhs.var = readonly_id;
5014   lhs.offset = 0;
5015   rhs.type = ADDRESSOF;
5016   rhs.var = anything_id;
5017   rhs.offset = 0;
5018
5019   process_constraint (new_constraint (lhs, rhs));
5020
5021   /* Create the INTEGER variable, used to represent that a variable points
5022      to an INTEGER.  */
5023   integer_tree = create_tmp_var_raw (void_type_node, "INTEGER");
5024   var_integer = new_var_info (integer_tree, 3, "INTEGER");
5025   insert_vi_for_tree (integer_tree, var_integer);
5026   var_integer->is_artificial_var = 1;
5027   var_integer->size = ~0;
5028   var_integer->fullsize = ~0;
5029   var_integer->offset = 0;
5030   var_integer->next = NULL;
5031   var_integer->is_special_var = 1;
5032   integer_id = 3;
5033   VEC_safe_push (varinfo_t, heap, varmap, var_integer);
5034
5035   /* INTEGER = ANYTHING, because we don't know where a dereference of
5036      a random integer will point to.  */
5037   lhs.type = SCALAR;
5038   lhs.var = integer_id;
5039   lhs.offset = 0;
5040   rhs.type = ADDRESSOF;
5041   rhs.var = anything_id;
5042   rhs.offset = 0;
5043   process_constraint (new_constraint (lhs, rhs));
5044 }
5045
5046 /* Initialize things necessary to perform PTA */
5047
5048 static void
5049 init_alias_vars (void)
5050 {
5051   bitmap_obstack_initialize (&pta_obstack);
5052   bitmap_obstack_initialize (&oldpta_obstack);
5053   bitmap_obstack_initialize (&predbitmap_obstack);
5054
5055   constraint_pool = create_alloc_pool ("Constraint pool",
5056                                        sizeof (struct constraint), 30);
5057   variable_info_pool = create_alloc_pool ("Variable info pool",
5058                                           sizeof (struct variable_info), 30);
5059   constraints = VEC_alloc (constraint_t, heap, 8);
5060   varmap = VEC_alloc (varinfo_t, heap, 8);
5061   vi_for_tree = pointer_map_create ();
5062
5063   memset (&stats, 0, sizeof (stats));
5064   shared_bitmap_table = htab_create (511, shared_bitmap_hash,
5065                                      shared_bitmap_eq, free);
5066   init_base_vars ();
5067 }
5068
5069 /* Remove the REF and ADDRESS edges from GRAPH, as well as all the
5070    predecessor edges.  */
5071
5072 static void
5073 remove_preds_and_fake_succs (constraint_graph_t graph)
5074 {
5075   unsigned int i;
5076
5077   /* Clear the implicit ref and address nodes from the successor
5078      lists.  */
5079   for (i = 0; i < FIRST_REF_NODE; i++)
5080     {
5081       if (graph->succs[i])
5082         bitmap_clear_range (graph->succs[i], FIRST_REF_NODE,
5083                             FIRST_REF_NODE * 2);
5084     }
5085
5086   /* Free the successor list for the non-ref nodes.  */
5087   for (i = FIRST_REF_NODE; i < graph->size; i++)
5088     {
5089       if (graph->succs[i])
5090         BITMAP_FREE (graph->succs[i]);
5091     }
5092
5093   /* Now reallocate the size of the successor list as, and blow away
5094      the predecessor bitmaps.  */
5095   graph->size = VEC_length (varinfo_t, varmap);
5096   graph->succs = XRESIZEVEC (bitmap, graph->succs, graph->size);
5097
5098   free (graph->implicit_preds);
5099   graph->implicit_preds = NULL;
5100   free (graph->preds);
5101   graph->preds = NULL;
5102   bitmap_obstack_release (&predbitmap_obstack);
5103 }
5104
5105 /* Compute the set of variables we can't TBAA prune.  */
5106
5107 static void
5108 compute_tbaa_pruning (void)
5109 {
5110   unsigned int size = VEC_length (varinfo_t, varmap);
5111   unsigned int i;
5112   bool any;
5113
5114   changed_count = 0;
5115   changed = sbitmap_alloc (size);
5116   sbitmap_zero (changed);
5117
5118   /* Mark all initial no_tbaa_pruning nodes as changed.  */
5119   any = false;
5120   for (i = 0; i < size; ++i)
5121     {
5122       varinfo_t ivi = get_varinfo (i);
5123
5124       if (find (i) == i && ivi->no_tbaa_pruning)
5125         {
5126           any = true;
5127           if ((graph->succs[i] && !bitmap_empty_p (graph->succs[i]))
5128               || VEC_length (constraint_t, graph->complex[i]) > 0)
5129             {
5130               SET_BIT (changed, i);
5131               ++changed_count;
5132             }
5133         }
5134     }
5135
5136   while (changed_count > 0)
5137     {
5138       struct topo_info *ti = init_topo_info ();
5139       ++stats.iterations;
5140
5141       compute_topo_order (graph, ti);
5142
5143       while (VEC_length (unsigned, ti->topo_order) != 0)
5144         {
5145           bitmap_iterator bi;
5146
5147           i = VEC_pop (unsigned, ti->topo_order);
5148
5149           /* If this variable is not a representative, skip it.  */
5150           if (find (i) != i)
5151             continue;
5152
5153           /* If the node has changed, we need to process the complex
5154              constraints and outgoing edges again.  */
5155           if (TEST_BIT (changed, i))
5156             {
5157               unsigned int j;
5158               constraint_t c;
5159               VEC(constraint_t,heap) *complex = graph->complex[i];
5160
5161               RESET_BIT (changed, i);
5162               --changed_count;
5163
5164               /* Process the complex copy constraints.  */
5165               for (j = 0; VEC_iterate (constraint_t, complex, j, c); ++j)
5166                 {
5167                   if (c->lhs.type == SCALAR && c->rhs.type == SCALAR)
5168                     {
5169                       varinfo_t lhsvi = get_varinfo (find (c->lhs.var));
5170
5171                       if (!lhsvi->no_tbaa_pruning)
5172                         {
5173                           lhsvi->no_tbaa_pruning = true;
5174                           if (!TEST_BIT (changed, lhsvi->id))
5175                             {
5176                               SET_BIT (changed, lhsvi->id);
5177                               ++changed_count;
5178                             }
5179                         }
5180                     }
5181                 }
5182
5183               /* Propagate to all successors.  */
5184               EXECUTE_IF_IN_NONNULL_BITMAP (graph->succs[i], 0, j, bi)
5185                 {
5186                   unsigned int to = find (j);
5187                   varinfo_t tovi = get_varinfo (to);
5188
5189                   /* Don't propagate to ourselves.  */
5190                   if (to == i)
5191                     continue;
5192
5193                   if (!tovi->no_tbaa_pruning)
5194                     {
5195                       tovi->no_tbaa_pruning = true;
5196                       if (!TEST_BIT (changed, to))
5197                         {
5198                           SET_BIT (changed, to);
5199                           ++changed_count;
5200                         }
5201                     }
5202                 }
5203             }
5204         }
5205
5206       free_topo_info (ti);
5207     }
5208
5209   sbitmap_free (changed);
5210
5211   if (any)
5212     {
5213       for (i = 0; i < size; ++i)
5214         {
5215           varinfo_t ivi = get_varinfo (i);
5216           varinfo_t ivip = get_varinfo (find (i));
5217
5218           if (ivip->no_tbaa_pruning)
5219             {
5220               tree var = ivi->decl;
5221
5222               if (TREE_CODE (var) == SSA_NAME)
5223                 var = SSA_NAME_VAR (var);
5224
5225               if (POINTER_TYPE_P (TREE_TYPE (var)))
5226                 {
5227                   DECL_NO_TBAA_P (var) = 1;
5228
5229                   /* Tell the RTL layer that this pointer can alias
5230                      anything.  */
5231                   DECL_POINTER_ALIAS_SET (var) = 0;
5232                 }
5233             }
5234         }
5235     }
5236 }
5237
5238 /* Create points-to sets for the current function.  See the comments
5239    at the start of the file for an algorithmic overview.  */
5240
5241 void
5242 compute_points_to_sets (struct alias_info *ai)
5243 {
5244   struct scc_info *si;
5245   basic_block bb;
5246
5247   timevar_push (TV_TREE_PTA);
5248
5249   init_alias_vars ();
5250   init_alias_heapvars ();
5251
5252   intra_create_variable_infos ();
5253
5254   /* Now walk all statements and derive aliases.  */
5255   FOR_EACH_BB (bb)
5256     {
5257       block_stmt_iterator bsi;
5258       tree phi;
5259
5260       for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
5261         {
5262           if (is_gimple_reg (PHI_RESULT (phi)))
5263             {
5264               find_func_aliases (phi);
5265
5266               /* Update various related attributes like escaped
5267                  addresses, pointer dereferences for loads and stores.
5268                  This is used when creating name tags and alias
5269                  sets.  */
5270               update_alias_info (phi, ai);
5271             }
5272         }
5273
5274       for (bsi = bsi_start (bb); !bsi_end_p (bsi); )
5275         {
5276           tree stmt = bsi_stmt (bsi);
5277
5278           find_func_aliases (stmt);
5279
5280           /* Update various related attributes like escaped
5281              addresses, pointer dereferences for loads and stores.
5282              This is used when creating name tags and alias
5283              sets.  */
5284           update_alias_info (stmt, ai);
5285
5286           /* The information in CHANGE_DYNAMIC_TYPE_EXPR nodes has now
5287              been captured, and we can remove them.  */
5288           if (TREE_CODE (stmt) == CHANGE_DYNAMIC_TYPE_EXPR)
5289             bsi_remove (&bsi, true);
5290           else
5291             bsi_next (&bsi);
5292         }
5293     }
5294
5295
5296   if (dump_file)
5297     {
5298       fprintf (dump_file, "Points-to analysis\n\nConstraints:\n\n");
5299       dump_constraints (dump_file);
5300     }
5301
5302   if (dump_file)
5303     fprintf (dump_file,
5304              "\nCollapsing static cycles and doing variable "
5305              "substitution\n");
5306
5307   init_graph (VEC_length (varinfo_t, varmap) * 2);
5308   
5309   if (dump_file)
5310     fprintf (dump_file, "Building predecessor graph\n");
5311   build_pred_graph ();
5312   
5313   if (dump_file)
5314     fprintf (dump_file, "Detecting pointer and location "
5315              "equivalences\n");
5316   si = perform_var_substitution (graph);
5317   
5318   if (dump_file)
5319     fprintf (dump_file, "Rewriting constraints and unifying "
5320              "variables\n");
5321   rewrite_constraints (graph, si);
5322   free_var_substitution_info (si);
5323
5324   build_succ_graph ();
5325   move_complex_constraints (graph);
5326
5327   if (dump_file)
5328     fprintf (dump_file, "Uniting pointer but not location equivalent "
5329              "variables\n");
5330   unite_pointer_equivalences (graph);
5331
5332   if (dump_file)
5333     fprintf (dump_file, "Finding indirect cycles\n");
5334   find_indirect_cycles (graph);
5335
5336   /* Implicit nodes and predecessors are no longer necessary at this
5337      point. */
5338   remove_preds_and_fake_succs (graph);
5339
5340   if (dump_file)
5341     fprintf (dump_file, "Solving graph\n");
5342
5343   solve_graph (graph);
5344
5345   compute_tbaa_pruning ();
5346
5347   if (dump_file)
5348     dump_sa_points_to_info (dump_file);
5349
5350   have_alias_info = true;
5351
5352   timevar_pop (TV_TREE_PTA);
5353 }
5354
5355
5356 /* Delete created points-to sets.  */
5357
5358 void
5359 delete_points_to_sets (void)
5360 {
5361   unsigned int i;
5362
5363   htab_delete (shared_bitmap_table);
5364   if (dump_file && (dump_flags & TDF_STATS))
5365     fprintf (dump_file, "Points to sets created:%d\n",
5366              stats.points_to_sets_created);
5367
5368   pointer_map_destroy (vi_for_tree);
5369   bitmap_obstack_release (&pta_obstack);
5370   VEC_free (constraint_t, heap, constraints);
5371
5372   for (i = 0; i < graph->size; i++)
5373     VEC_free (constraint_t, heap, graph->complex[i]);
5374   free (graph->complex);
5375
5376   free (graph->rep);
5377   free (graph->succs);
5378   free (graph->pe);
5379   free (graph->pe_rep);
5380   free (graph->indirect_cycles);
5381   free (graph);
5382
5383   VEC_free (varinfo_t, heap, varmap);
5384   free_alloc_pool (variable_info_pool);
5385   free_alloc_pool (constraint_pool);
5386   have_alias_info = false;
5387 }
5388
5389 /* Return true if we should execute IPA PTA.  */
5390 static bool
5391 gate_ipa_pta (void)
5392 {
5393   return (flag_unit_at_a_time != 0
5394           && flag_ipa_pta
5395           /* Don't bother doing anything if the program has errors.  */
5396           && !(errorcount || sorrycount));
5397 }
5398
5399 /* Execute the driver for IPA PTA.  */
5400 static unsigned int
5401 ipa_pta_execute (void)
5402 {
5403   struct cgraph_node *node;
5404   struct scc_info *si;
5405
5406   in_ipa_mode = 1;
5407   init_alias_heapvars ();
5408   init_alias_vars ();
5409
5410   for (node = cgraph_nodes; node; node = node->next)
5411     {
5412       if (!node->analyzed || cgraph_is_master_clone (node))
5413         {
5414           unsigned int varid;
5415
5416           varid = create_function_info_for (node->decl,
5417                                             cgraph_node_name (node));
5418           if (node->local.externally_visible)
5419             {
5420               varinfo_t fi = get_varinfo (varid);
5421               for (; fi; fi = fi->next)
5422                 make_constraint_from_anything (fi);
5423             }
5424         }
5425     }
5426   for (node = cgraph_nodes; node; node = node->next)
5427     {
5428       if (node->analyzed && cgraph_is_master_clone (node))
5429         {
5430           struct function *func = DECL_STRUCT_FUNCTION (node->decl);
5431           basic_block bb;
5432           tree old_func_decl = current_function_decl;
5433           if (dump_file)
5434             fprintf (dump_file,
5435                      "Generating constraints for %s\n",
5436                      cgraph_node_name (node));
5437           push_cfun (func);
5438           current_function_decl = node->decl;
5439
5440           FOR_EACH_BB_FN (bb, func)
5441             {
5442               block_stmt_iterator bsi;
5443               tree phi;
5444
5445               for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
5446                 {
5447                   if (is_gimple_reg (PHI_RESULT (phi)))
5448                     {
5449                       find_func_aliases (phi);
5450                     }
5451                 }
5452
5453               for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
5454                 {
5455                   tree stmt = bsi_stmt (bsi);
5456                   find_func_aliases (stmt);
5457                 }
5458             }
5459           current_function_decl = old_func_decl;
5460           pop_cfun ();
5461         }
5462       else
5463         {
5464           /* Make point to anything.  */
5465         }
5466     }
5467
5468   if (dump_file)
5469     {
5470       fprintf (dump_file, "Points-to analysis\n\nConstraints:\n\n");
5471       dump_constraints (dump_file);
5472     }
5473
5474   if (dump_file)
5475     fprintf (dump_file,
5476              "\nCollapsing static cycles and doing variable "
5477              "substitution:\n");
5478
5479   init_graph (VEC_length (varinfo_t, varmap) * 2);
5480   build_pred_graph ();
5481   si = perform_var_substitution (graph);
5482   rewrite_constraints (graph, si);
5483   free_var_substitution_info (si);
5484
5485   build_succ_graph ();
5486   move_complex_constraints (graph);
5487   unite_pointer_equivalences (graph);
5488   find_indirect_cycles (graph);
5489
5490   /* Implicit nodes and predecessors are no longer necessary at this
5491      point. */
5492   remove_preds_and_fake_succs (graph);
5493
5494   if (dump_file)
5495     fprintf (dump_file, "\nSolving graph\n");
5496
5497   solve_graph (graph);
5498
5499   if (dump_file)
5500     dump_sa_points_to_info (dump_file);
5501
5502   in_ipa_mode = 0;
5503   delete_alias_heapvars ();
5504   delete_points_to_sets ();
5505   return 0;
5506 }
5507
5508 struct simple_ipa_opt_pass pass_ipa_pta =
5509 {
5510  {
5511   SIMPLE_IPA_PASS,
5512   "pta",                                /* name */
5513   gate_ipa_pta,                 /* gate */
5514   ipa_pta_execute,                      /* execute */
5515   NULL,                                 /* sub */
5516   NULL,                                 /* next */
5517   0,                                    /* static_pass_number */
5518   TV_IPA_PTA,                   /* tv_id */
5519   0,                                    /* properties_required */
5520   0,                                    /* properties_provided */
5521   0,                                    /* properties_destroyed */
5522   0,                                    /* todo_flags_start */
5523   TODO_update_ssa                       /* todo_flags_finish */
5524  }
5525 };
5526
5527 /* Initialize the heapvar for statement mapping.  */
5528 void
5529 init_alias_heapvars (void)
5530 {
5531   if (!heapvar_for_stmt)
5532     heapvar_for_stmt = htab_create_ggc (11, tree_map_hash, tree_map_eq,
5533                                         NULL);
5534 }
5535
5536 void
5537 delete_alias_heapvars (void)
5538 {
5539   htab_delete (heapvar_for_stmt);
5540   heapvar_for_stmt = NULL;
5541 }
5542
5543
5544 #include "gt-tree-ssa-structalias.h"