OSDN Git Service

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