OSDN Git Service

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