OSDN Git Service

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