OSDN Git Service

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