OSDN Git Service

2006-06-15 Richard Guenther <rguenther@suse.de>
[pf3gnuchains/gcc-fork.git] / gcc / tree-ssa-structalias.c
1 /* Tree based points-to analysis
2    Copyright (C) 2005 Free Software Foundation, Inc.
3    Contributed by Daniel Berlin <dberlin@dberlin.org>
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20 */
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 "errors.h"
36 #include "diagnostic.h"
37 #include "tree.h"
38 #include "c-common.h"
39 #include "tree-flow.h"
40 #include "tree-inline.h"
41 #include "varray.h"
42 #include "c-tree.h"
43 #include "tree-gimple.h"
44 #include "hashtab.h"
45 #include "function.h"
46 #include "cgraph.h"
47 #include "tree-pass.h"
48 #include "timevar.h"
49 #include "alloc-pool.h"
50 #include "splay-tree.h"
51 #include "params.h"
52 #include "tree-ssa-structalias.h"
53 #include "cgraph.h"
54
55 /* The idea behind this analyzer is to generate set constraints from the
56    program, then solve the resulting constraints in order to generate the
57    points-to sets. 
58
59    Set constraints are a way of modeling program analysis problems that
60    involve sets.  They consist of an inclusion constraint language,
61    describing the variables (each variable is a set) and operations that
62    are involved on the variables, and a set of rules that derive facts
63    from these operations.  To solve a system of set constraints, you derive
64    all possible facts under the rules, which gives you the correct sets
65    as a consequence.
66
67    See  "Efficient Field-sensitive pointer analysis for C" by "David
68    J. Pearce and Paul H. J. Kelly and Chris Hankin, at
69    http://citeseer.ist.psu.edu/pearce04efficient.html
70
71    Also see "Ultra-fast Aliasing Analysis using CLA: A Million Lines
72    of C Code in a Second" by ""Nevin Heintze and Olivier Tardieu" at
73    http://citeseer.ist.psu.edu/heintze01ultrafast.html 
74
75    There are three types of constraint expressions, DEREF, ADDRESSOF, and
76    SCALAR.  Each constraint expression consists of a constraint type,
77    a variable, and an offset.  
78    
79    SCALAR is a constraint expression type used to represent x, whether
80    it appears on the LHS or the RHS of a statement.
81    DEREF is a constraint expression type used to represent *x, whether
82    it appears on the LHS or the RHS of a statement. 
83    ADDRESSOF is a constraint expression used to represent &x, whether
84    it appears on the LHS or the RHS of a statement.
85    
86    Each pointer variable in the program is assigned an integer id, and
87    each field of a structure variable is assigned an integer id as well.
88    
89    Structure variables are linked to their list of fields through a "next
90    field" in each variable that points to the next field in offset
91    order.  
92    Each variable for a structure field has 
93
94    1. "size", that tells the size in bits of that field.
95    2. "fullsize, that tells the size in bits of the entire structure.
96    3. "offset", that tells the offset in bits from the beginning of the
97    structure to this field.
98
99    Thus, 
100    struct f
101    {
102      int a;
103      int b;
104    } foo;
105    int *bar;
106
107    looks like
108
109    foo.a -> id 1, size 32, offset 0, fullsize 64, next foo.b
110    foo.b -> id 2, size 32, offset 32, fullsize 64, next NULL
111    bar -> id 3, size 32, offset 0, fullsize 32, next NULL
112
113    
114   In order to solve the system of set constraints, the following is
115   done:
116
117   1. Each constraint variable x has a solution set associated with it,
118   Sol(x).
119   
120   2. Constraints are separated into direct, copy, and complex.
121   Direct constraints are ADDRESSOF constraints that require no extra
122   processing, such as P = &Q
123   Copy constraints are those of the form P = Q.
124   Complex constraints are all the constraints involving dereferences.
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 static GTY ((if_marked ("tree_map_marked_p"), param_is (struct tree_map))) 
165   htab_t heapvar_for_stmt;
166 static bool use_field_sensitive = true;
167 static int in_ipa_mode = 0;
168 static bitmap_obstack predbitmap_obstack;
169 static bitmap_obstack ptabitmap_obstack;
170 static bitmap_obstack iteration_obstack;
171
172 static unsigned int create_variable_info_for (tree, const char *);
173 static void build_constraint_graph (void);
174
175 DEF_VEC_P(constraint_t);
176 DEF_VEC_ALLOC_P(constraint_t,heap);
177
178 #define EXECUTE_IF_IN_NONNULL_BITMAP(a, b, c, d)        \
179   if (a)                                                \
180     EXECUTE_IF_SET_IN_BITMAP (a, b, c, d)
181
182 static struct constraint_stats
183 {
184   unsigned int total_vars;
185   unsigned int collapsed_vars;
186   unsigned int unified_vars_static;
187   unsigned int unified_vars_dynamic;
188   unsigned int iterations;
189   unsigned int num_edges;
190 } stats;
191
192 struct variable_info
193 {
194   /* ID of this variable  */
195   unsigned int id;
196
197   /* Name of this variable */
198   const char *name;
199
200   /* Tree that this variable is associated with.  */
201   tree decl;
202
203   /* Offset of this variable, in bits, from the base variable  */
204   unsigned HOST_WIDE_INT offset;  
205
206   /* Size of the variable, in bits.  */
207   unsigned HOST_WIDE_INT size;
208
209   /* Full size of the base variable, in bits.  */
210   unsigned HOST_WIDE_INT fullsize;
211
212   /* A link to the variable for the next field in this structure.  */
213   struct variable_info *next;
214
215   /* Node in the graph that represents the constraints and points-to
216      solution for the variable.  */
217   unsigned int node;
218
219   /* True if the address of this variable is taken.  Needed for
220      variable substitution.  */
221   unsigned int address_taken:1;
222
223   /* True if this variable is the target of a dereference.  Needed for
224      variable substitution.  */
225   unsigned int indirect_target:1;
226
227   /* True if this is a variable created by the constraint analysis, such as
228      heap variables and constraints we had to break up.  */
229   unsigned int is_artificial_var:1;
230   
231   /* True if this is a special variable whose solution set should not be
232      changed.  */
233   unsigned int is_special_var:1;
234
235   /* True for variables whose size is not known or variable.  */
236   unsigned int is_unknown_size_var:1;  
237
238   /* True for variables that have unions somewhere in them.  */
239   unsigned int has_union:1;
240
241   /* True if this is a heap variable.  */
242   unsigned int is_heap_var:1;
243
244   /* Points-to set for this variable.  */
245   bitmap solution;
246
247   /* Variable ids represented by this node.  */
248   bitmap variables;
249
250   /* Vector of complex constraints for this node.  Complex
251      constraints are those involving dereferences.  */
252   VEC(constraint_t,heap) *complex;
253   
254   /* Variable id this was collapsed to due to type unsafety.
255      This should be unused completely after build_constraint_graph, or
256      something is broken.  */
257   struct variable_info *collapsed_to;
258 };
259 typedef struct variable_info *varinfo_t;
260
261 static varinfo_t first_vi_for_offset (varinfo_t, unsigned HOST_WIDE_INT);
262
263 /* Pool of variable info structures.  */
264 static alloc_pool variable_info_pool;
265
266 DEF_VEC_P(varinfo_t);
267
268 DEF_VEC_ALLOC_P(varinfo_t, heap);
269
270 /* Table of variable info structures for constraint variables.  Indexed directly
271    by variable info id.  */
272 static VEC(varinfo_t,heap) *varmap;
273
274 /* Return the varmap element N */
275
276 static inline varinfo_t
277 get_varinfo (unsigned int n)
278 {
279   return VEC_index(varinfo_t, varmap, n);
280 }
281
282 /* Return the varmap element N, following the collapsed_to link.  */
283
284 static inline varinfo_t
285 get_varinfo_fc (unsigned int n)
286 {
287   varinfo_t v = VEC_index(varinfo_t, varmap, n);
288
289   if (v->collapsed_to)
290     return v->collapsed_to;
291   return v;
292 }
293
294 /* Variable that represents the unknown pointer.  */
295 static varinfo_t var_anything;
296 static tree anything_tree;
297 static unsigned int anything_id;
298
299 /* Variable that represents the NULL pointer.  */
300 static varinfo_t var_nothing;
301 static tree nothing_tree;
302 static unsigned int nothing_id;
303
304 /* Variable that represents read only memory.  */
305 static varinfo_t var_readonly;
306 static tree readonly_tree;
307 static unsigned int readonly_id;
308
309 /* Variable that represents integers.  This is used for when people do things
310    like &0->a.b.  */
311 static varinfo_t var_integer;
312 static tree integer_tree;
313 static unsigned int integer_id;
314
315
316 /* Lookup a heap var for FROM, and return it if we find one.  */
317
318 static tree 
319 heapvar_lookup (tree from)
320 {
321   struct tree_map *h, in;
322   in.from = from;
323
324   h = htab_find_with_hash (heapvar_for_stmt, &in, htab_hash_pointer (from));
325   if (h)
326     return h->to;
327   return NULL_TREE;
328 }
329
330 /* Insert a mapping FROM->TO in the heap var for statement
331    hashtable.  */
332
333 static void
334 heapvar_insert (tree from, tree to)
335 {
336   struct tree_map *h;
337   void **loc;
338
339   h = ggc_alloc (sizeof (struct tree_map));
340   h->hash = htab_hash_pointer (from);
341   h->from = from;
342   h->to = to;
343   loc = htab_find_slot_with_hash (heapvar_for_stmt, h, h->hash, INSERT);
344   *(struct tree_map **) loc = h;
345 }  
346
347 /* Return a new variable info structure consisting for a variable
348    named NAME, and using constraint graph node NODE.  */
349
350 static varinfo_t
351 new_var_info (tree t, unsigned int id, const char *name, unsigned int node)
352 {
353   varinfo_t ret = pool_alloc (variable_info_pool);
354
355   ret->id = id;
356   ret->name = name;
357   ret->decl = t;
358   ret->node = node;
359   ret->address_taken = false;
360   ret->indirect_target = false;
361   ret->is_artificial_var = false;
362   ret->is_heap_var = false;
363   ret->is_special_var = false;
364   ret->is_unknown_size_var = false;
365   ret->has_union = false;
366   ret->solution = BITMAP_ALLOC (&ptabitmap_obstack);
367   ret->variables = BITMAP_ALLOC (&ptabitmap_obstack);
368   ret->complex = NULL;
369   ret->next = NULL;
370   ret->collapsed_to = NULL;
371   return ret;
372 }
373
374 typedef enum {SCALAR, DEREF, ADDRESSOF} constraint_expr_type;
375
376 /* An expression that appears in a constraint.  */
377
378 struct constraint_expr 
379 {
380   /* Constraint type.  */
381   constraint_expr_type type;
382
383   /* Variable we are referring to in the constraint.  */
384   unsigned int var;
385
386   /* Offset, in bits, of this constraint from the beginning of
387      variables it ends up referring to.
388
389      IOW, in a deref constraint, we would deref, get the result set,
390      then add OFFSET to each member.   */
391   unsigned HOST_WIDE_INT offset;
392 };
393
394 typedef struct constraint_expr ce_s;
395 DEF_VEC_O(ce_s);
396 DEF_VEC_ALLOC_O(ce_s, heap);
397 static void get_constraint_for (tree, VEC(ce_s, heap) **);
398 static void do_deref (VEC (ce_s, heap) **);
399
400 /* Our set constraints are made up of two constraint expressions, one
401    LHS, and one RHS.  
402
403    As described in the introduction, our set constraints each represent an
404    operation between set valued variables.
405 */
406 struct constraint
407 {
408   struct constraint_expr lhs;
409   struct constraint_expr rhs;
410 };
411
412 /* List of constraints that we use to build the constraint graph from.  */
413
414 static VEC(constraint_t,heap) *constraints;
415 static alloc_pool constraint_pool;
416
417 /* An edge in the weighted constraint graph.   The edges are weighted,
418    with a bit set in weights meaning their is an edge with that
419    weight. 
420    We don't keep the src in the edge, because we always know what it
421    is. */
422
423 struct constraint_edge
424 {
425   unsigned int dest;
426   bitmap weights;
427 };
428
429 typedef struct constraint_edge *constraint_edge_t;
430 static alloc_pool constraint_edge_pool;
431
432 /* Return a new constraint edge from SRC to DEST.  */
433
434 static constraint_edge_t
435 new_constraint_edge (unsigned int dest)
436 {
437   constraint_edge_t ret = pool_alloc (constraint_edge_pool);
438   ret->dest = dest;
439   ret->weights = NULL;
440   return ret;
441 }
442
443 DEF_VEC_P(constraint_edge_t);
444 DEF_VEC_ALLOC_P(constraint_edge_t,heap);
445
446
447 /* The constraint graph is represented internally in two different
448    ways.  The overwhelming majority of edges in the constraint graph
449    are zero weigh edges, and thus, using a vector of contrainst_edge_t
450    is a waste of time and memory, since they have no weights.  We
451    simply use a bitmap to store the preds and succs for each node.
452    The weighted edges are stored as a set of adjacency vectors, one
453    per variable. succs[x] is the vector of successors for variable x,
454    and preds[x] is the vector of predecessors for variable x.  IOW,
455    all edges are "forward" edges, which is not like our CFG.  So
456    remember that preds[x]->src == x, and succs[x]->src == x.  */
457
458 struct constraint_graph
459 {
460   bitmap *zero_weight_succs;
461   bitmap *zero_weight_preds;
462   VEC(constraint_edge_t,heap) **succs;
463   VEC(constraint_edge_t,heap) **preds;
464 };
465
466 typedef struct constraint_graph *constraint_graph_t;
467
468 static constraint_graph_t graph;
469
470 /* Create a new constraint consisting of LHS and RHS expressions.  */
471
472 static constraint_t 
473 new_constraint (const struct constraint_expr lhs,
474                 const struct constraint_expr rhs)
475 {
476   constraint_t ret = pool_alloc (constraint_pool);
477   ret->lhs = lhs;
478   ret->rhs = rhs;
479   return ret;
480 }
481
482 /* Print out constraint C to FILE.  */
483
484 void
485 dump_constraint (FILE *file, constraint_t c)
486 {
487   if (c->lhs.type == ADDRESSOF)
488     fprintf (file, "&");
489   else if (c->lhs.type == DEREF)
490     fprintf (file, "*");  
491   fprintf (file, "%s", get_varinfo_fc (c->lhs.var)->name);
492   if (c->lhs.offset != 0)
493     fprintf (file, " + " HOST_WIDE_INT_PRINT_DEC, c->lhs.offset);
494   fprintf (file, " = ");
495   if (c->rhs.type == ADDRESSOF)
496     fprintf (file, "&");
497   else if (c->rhs.type == DEREF)
498     fprintf (file, "*");
499   fprintf (file, "%s", get_varinfo_fc (c->rhs.var)->name);
500   if (c->rhs.offset != 0)
501     fprintf (file, " + " HOST_WIDE_INT_PRINT_DEC, c->rhs.offset);
502   fprintf (file, "\n");
503 }
504
505 /* Print out constraint C to stderr.  */
506
507 void
508 debug_constraint (constraint_t c)
509 {
510   dump_constraint (stderr, c);
511 }
512
513 /* Print out all constraints to FILE */
514
515 void
516 dump_constraints (FILE *file)
517 {
518   int i;
519   constraint_t c;
520   for (i = 0; VEC_iterate (constraint_t, constraints, i, c); i++)
521     dump_constraint (file, c);
522 }
523
524 /* Print out all constraints to stderr.  */
525
526 void
527 debug_constraints (void)
528 {
529   dump_constraints (stderr);
530 }
531
532 /* SOLVER FUNCTIONS 
533
534    The solver is a simple worklist solver, that works on the following
535    algorithm:
536    
537    sbitmap changed_nodes = all ones;
538    changed_count = number of nodes;
539    For each node that was already collapsed:
540        changed_count--;
541
542    while (changed_count > 0)
543    {
544      compute topological ordering for constraint graph
545   
546      find and collapse cycles in the constraint graph (updating
547      changed if necessary)
548      
549      for each node (n) in the graph in topological order:
550        changed_count--;
551
552        Process each complex constraint associated with the node,
553        updating changed if necessary.
554
555        For each outgoing edge from n, propagate the solution from n to
556        the destination of the edge, updating changed as necessary.
557
558    }  */
559
560 /* Return true if two constraint expressions A and B are equal.  */
561
562 static bool
563 constraint_expr_equal (struct constraint_expr a, struct constraint_expr b)
564 {
565   return a.type == b.type && a.var == b.var && a.offset == b.offset;
566 }
567
568 /* Return true if constraint expression A is less than constraint expression
569    B.  This is just arbitrary, but consistent, in order to give them an
570    ordering.  */
571
572 static bool
573 constraint_expr_less (struct constraint_expr a, struct constraint_expr b)
574 {
575   if (a.type == b.type)
576     {
577       if (a.var == b.var)
578         return a.offset < b.offset;
579       else
580         return a.var < b.var;
581     }
582   else
583     return a.type < b.type;
584 }
585
586 /* Return true if constraint A is less than constraint B.  This is just
587    arbitrary, but consistent, in order to give them an ordering.  */
588
589 static bool
590 constraint_less (const constraint_t a, const constraint_t b)
591 {
592   if (constraint_expr_less (a->lhs, b->lhs))
593     return true;
594   else if (constraint_expr_less (b->lhs, a->lhs))
595     return false;
596   else
597     return constraint_expr_less (a->rhs, b->rhs);
598 }
599
600 /* Return true if two constraints A and B are equal.  */
601   
602 static bool
603 constraint_equal (struct constraint a, struct constraint b)
604 {
605   return constraint_expr_equal (a.lhs, b.lhs) 
606     && constraint_expr_equal (a.rhs, b.rhs);
607 }
608
609
610 /* Find a constraint LOOKFOR in the sorted constraint vector VEC */
611
612 static constraint_t
613 constraint_vec_find (VEC(constraint_t,heap) *vec,
614                      struct constraint lookfor)
615 {
616   unsigned int place;  
617   constraint_t found;
618
619   if (vec == NULL)
620     return NULL;
621
622   place = VEC_lower_bound (constraint_t, vec, &lookfor, constraint_less);
623   if (place >= VEC_length (constraint_t, vec))
624     return NULL;
625   found = VEC_index (constraint_t, vec, place);
626   if (!constraint_equal (*found, lookfor))
627     return NULL;
628   return found;
629 }
630
631 /* Union two constraint vectors, TO and FROM.  Put the result in TO.  */
632
633 static void
634 constraint_set_union (VEC(constraint_t,heap) **to,
635                       VEC(constraint_t,heap) **from)
636 {
637   int i;
638   constraint_t c;
639
640   for (i = 0; VEC_iterate (constraint_t, *from, i, c); i++)
641     {
642       if (constraint_vec_find (*to, *c) == NULL)
643         {
644           unsigned int place = VEC_lower_bound (constraint_t, *to, c,
645                                                 constraint_less);
646           VEC_safe_insert (constraint_t, heap, *to, place, c);
647         }
648     }
649 }
650
651 /* Take a solution set SET, add OFFSET to each member of the set, and
652    overwrite SET with the result when done.  */
653
654 static void
655 solution_set_add (bitmap set, unsigned HOST_WIDE_INT offset)
656 {
657   bitmap result = BITMAP_ALLOC (&iteration_obstack);
658   unsigned int i;
659   bitmap_iterator bi;
660
661   EXECUTE_IF_SET_IN_BITMAP (set, 0, i, bi)
662     {
663       /* If this is a properly sized variable, only add offset if it's
664          less than end.  Otherwise, it is globbed to a single
665          variable.  */
666       
667       if ((get_varinfo (i)->offset + offset) < get_varinfo (i)->fullsize)
668         {
669           unsigned HOST_WIDE_INT fieldoffset = get_varinfo (i)->offset + offset;
670           varinfo_t v = first_vi_for_offset (get_varinfo (i), fieldoffset);
671           if (!v)
672             continue;
673           bitmap_set_bit (result, v->id);
674         }
675       else if (get_varinfo (i)->is_artificial_var 
676                || get_varinfo (i)->has_union
677                || get_varinfo (i)->is_unknown_size_var)
678         {
679           bitmap_set_bit (result, i);
680         }
681     }
682   
683   bitmap_copy (set, result);  
684   BITMAP_FREE (result);
685 }
686
687 /* Union solution sets TO and FROM, and add INC to each member of FROM in the
688    process.  */
689
690 static bool
691 set_union_with_increment  (bitmap to, bitmap from, unsigned HOST_WIDE_INT inc)
692 {
693   if (inc == 0)
694     return bitmap_ior_into (to, from);
695   else
696     {
697       bitmap tmp;
698       bool res;
699
700       tmp = BITMAP_ALLOC (&iteration_obstack);
701       bitmap_copy (tmp, from);
702       solution_set_add (tmp, inc);
703       res = bitmap_ior_into (to, tmp);
704       BITMAP_FREE (tmp);
705       return res;
706     }
707 }
708
709 /* Insert constraint C into the list of complex constraints for VAR.  */
710
711 static void
712 insert_into_complex (unsigned int var, constraint_t c)
713 {
714   varinfo_t vi = get_varinfo (var);
715   unsigned int place = VEC_lower_bound (constraint_t, vi->complex, c,
716                                         constraint_less);
717   VEC_safe_insert (constraint_t, heap, vi->complex, place, c);
718 }
719
720
721 /* Compare two constraint edges A and B, return true if they are equal.  */
722
723 static bool
724 constraint_edge_equal (struct constraint_edge a, struct constraint_edge b)
725 {
726   return a.dest == b.dest;
727 }
728
729 /* Compare two constraint edges, return true if A is less than B */
730
731 static bool
732 constraint_edge_less (const constraint_edge_t a, const constraint_edge_t b)
733 {
734   if (a->dest < b->dest)
735     return true;
736   return false;
737 }
738
739 /* Find the constraint edge that matches LOOKFOR, in VEC.
740    Return the edge, if found, NULL otherwise.  */
741
742 static constraint_edge_t 
743 constraint_edge_vec_find (VEC(constraint_edge_t,heap) *vec, 
744                           struct constraint_edge lookfor)
745 {
746   unsigned int place;  
747   constraint_edge_t edge = NULL;
748
749   place = VEC_lower_bound (constraint_edge_t, vec, &lookfor, 
750                            constraint_edge_less);
751   if (place >= VEC_length (constraint_edge_t, vec))
752     return NULL;
753   edge = VEC_index (constraint_edge_t, vec, place);
754   if (!constraint_edge_equal (*edge, lookfor))
755     return NULL;
756   return edge;
757 }
758
759 /* Condense two variable nodes into a single variable node, by moving
760    all associated info from SRC to TO.  */
761
762 static void 
763 condense_varmap_nodes (unsigned int to, unsigned int src)
764 {
765   varinfo_t tovi = get_varinfo (to);
766   varinfo_t srcvi = get_varinfo (src);
767   unsigned int i;
768   constraint_t c;
769   bitmap_iterator bi;
770   
771   /* the src node, and all its variables, are now the to node.  */
772   srcvi->node = to;
773   EXECUTE_IF_SET_IN_BITMAP (srcvi->variables, 0, i, bi)
774     get_varinfo (i)->node = to;
775   
776   /* Merge the src node variables and the to node variables.  */
777   bitmap_set_bit (tovi->variables, src);
778   bitmap_ior_into (tovi->variables, srcvi->variables);
779   bitmap_clear (srcvi->variables);
780   
781   /* Move all complex constraints from src node into to node  */
782   for (i = 0; VEC_iterate (constraint_t, srcvi->complex, i, c); i++)
783     {
784       /* In complex constraints for node src, we may have either
785          a = *src, and *src = a.  */
786       
787       if (c->rhs.type == DEREF)
788         c->rhs.var = to;
789       else
790         c->lhs.var = to;
791     }
792   constraint_set_union (&tovi->complex, &srcvi->complex);
793   VEC_free (constraint_t, heap, srcvi->complex);
794   srcvi->complex = NULL;
795 }
796
797 /* Erase an edge from SRC to SRC from GRAPH.  This routine only
798    handles self-edges (e.g. an edge from a to a).  */
799
800 static void
801 erase_graph_self_edge (constraint_graph_t graph, unsigned int src)
802 {
803   VEC(constraint_edge_t,heap) *predvec = graph->preds[src];
804   VEC(constraint_edge_t,heap) *succvec = graph->succs[src];
805   struct constraint_edge edge;
806   unsigned int place;
807
808   edge.dest = src;
809
810   /* Remove from the successors.  */
811   place = VEC_lower_bound (constraint_edge_t, succvec, &edge, 
812                            constraint_edge_less);
813   
814   /* Make sure we found the edge.  */
815 #ifdef ENABLE_CHECKING
816   {
817     constraint_edge_t tmp = VEC_index (constraint_edge_t, succvec, place);
818     gcc_assert (constraint_edge_equal (*tmp, edge));
819   }
820 #endif
821   VEC_ordered_remove (constraint_edge_t, succvec, place);
822
823   /* Remove from the predecessors.  */
824   place = VEC_lower_bound (constraint_edge_t, predvec, &edge,
825                            constraint_edge_less);
826
827   /* Make sure we found the edge.  */
828 #ifdef ENABLE_CHECKING
829   {
830     constraint_edge_t tmp = VEC_index (constraint_edge_t, predvec, place);
831     gcc_assert (constraint_edge_equal (*tmp, edge));
832   }
833 #endif
834   VEC_ordered_remove (constraint_edge_t, predvec, place);
835 }
836
837 /* Remove edges involving NODE from GRAPH.  */
838
839 static void
840 clear_edges_for_node (constraint_graph_t graph, unsigned int node)
841 {
842   VEC(constraint_edge_t,heap) *succvec = graph->succs[node];
843   VEC(constraint_edge_t,heap) *predvec = graph->preds[node];
844   bitmap_iterator bi;
845   unsigned int j;
846   constraint_edge_t c = NULL;
847   int i;
848
849   /* Walk the successors, erase the associated preds.  */
850   
851   EXECUTE_IF_IN_NONNULL_BITMAP (graph->zero_weight_succs[node], 0, j, bi)
852     if (j != node)
853       bitmap_clear_bit (graph->zero_weight_preds[j], node);
854   
855   for (i = 0; VEC_iterate (constraint_edge_t, succvec, i, c); i++)
856     if (c->dest != node)
857       {
858         unsigned int place;
859         struct constraint_edge lookfor;
860         constraint_edge_t result;
861
862         lookfor.dest = node;
863         place = VEC_lower_bound (constraint_edge_t, graph->preds[c->dest], 
864                                  &lookfor, constraint_edge_less);
865         result = VEC_ordered_remove (constraint_edge_t, 
866                                      graph->preds[c->dest], place);
867         pool_free (constraint_edge_pool, result);
868       }
869
870   /* Walk the preds, erase the associated succs.  */
871
872   EXECUTE_IF_IN_NONNULL_BITMAP (graph->zero_weight_preds[node], 0, j, bi)
873     if (j != node)
874       bitmap_clear_bit (graph->zero_weight_succs[j], node);
875   
876   for (i =0; VEC_iterate (constraint_edge_t, predvec, i, c); i++)
877     if (c->dest != node)
878       {
879         unsigned int place;
880         struct constraint_edge lookfor;
881         constraint_edge_t result;
882
883         lookfor.dest = node;
884         place = VEC_lower_bound (constraint_edge_t, graph->succs[c->dest],
885                                  &lookfor, constraint_edge_less);
886         result = VEC_ordered_remove (constraint_edge_t, 
887                                      graph->succs[c->dest], place);
888         pool_free (constraint_edge_pool, result);
889
890       }    
891
892   if (graph->zero_weight_preds[node])
893     {
894       BITMAP_FREE (graph->zero_weight_preds[node]);
895       graph->zero_weight_preds[node] = NULL;
896     } 
897
898   if (graph->zero_weight_succs[node])
899     {
900       BITMAP_FREE (graph->zero_weight_succs[node]);
901       graph->zero_weight_succs[node] = NULL;
902     } 
903
904   VEC_free (constraint_edge_t, heap, graph->preds[node]);
905   VEC_free (constraint_edge_t, heap, graph->succs[node]);
906   graph->preds[node] = NULL;
907   graph->succs[node] = NULL;
908 }
909
910 static bool edge_added = false;
911   
912 /* Add edge (src, dest) to the graph.  */
913
914 static bool
915 add_graph_edge (constraint_graph_t graph, unsigned int src, unsigned int dest)
916 {
917   unsigned int place;
918   VEC(constraint_edge_t,heap) *vec;
919   struct constraint_edge newe;
920   newe.dest = dest;
921
922   vec = graph->preds[src];
923   place = VEC_lower_bound (constraint_edge_t, vec, &newe, 
924                            constraint_edge_less);
925   if (place == VEC_length (constraint_edge_t, vec)
926       || VEC_index (constraint_edge_t, vec, place)->dest != dest)
927     {
928       constraint_edge_t edge = new_constraint_edge (dest);
929
930       VEC_safe_insert (constraint_edge_t, heap, graph->preds[src], 
931                        place, edge);
932       edge = new_constraint_edge (src);
933
934       place = VEC_lower_bound (constraint_edge_t, graph->succs[dest],
935                                edge, constraint_edge_less);
936       VEC_safe_insert (constraint_edge_t, heap, graph->succs[dest], 
937                        place, edge);
938       edge_added = true;
939       stats.num_edges++;
940       return true;
941     }
942   else
943     return false;
944 }
945
946
947 /* Return the bitmap representing the weights of edge (SRC, DEST).  */
948
949 static bitmap *
950 get_graph_weights (constraint_graph_t graph, unsigned int src,
951                    unsigned int dest)
952 {
953   constraint_edge_t edge;
954   VEC(constraint_edge_t,heap) *vec;
955   struct constraint_edge lookfor;
956
957   lookfor.dest = dest;
958
959   vec = graph->preds[src];
960   edge = constraint_edge_vec_find (vec, lookfor);
961   gcc_assert (edge != NULL);
962   return &edge->weights;
963 }
964
965 /* Allocate graph weight bitmap for the edges associated with SRC and
966    DEST in GRAPH.  Both the pred and the succ edges share a single
967    bitmap, so we need to set both edges to that bitmap.  */
968
969 static bitmap
970 allocate_graph_weights (constraint_graph_t graph, unsigned int src, 
971                         unsigned int dest)
972 {
973   bitmap result;
974   constraint_edge_t edge;
975   VEC(constraint_edge_t,heap) *vec;
976   struct constraint_edge lookfor;
977   
978   result = BITMAP_ALLOC (&ptabitmap_obstack);
979
980   /* Set the pred weight.  */
981   lookfor.dest = dest;
982   vec = graph->preds[src];
983   edge = constraint_edge_vec_find (vec, lookfor);
984   gcc_assert (edge != NULL);
985   edge->weights = result;
986
987   /* Set the succ weight.  */  
988   lookfor.dest = src;
989   vec = graph->succs[dest];
990   edge = constraint_edge_vec_find (vec, lookfor);
991   gcc_assert (edge != NULL);
992   edge->weights = result;
993   
994   return result;  
995 }
996
997
998 /* Merge GRAPH nodes FROM and TO into node TO.  */
999
1000 static void
1001 merge_graph_nodes (constraint_graph_t graph, unsigned int to, 
1002                    unsigned int from)
1003 {
1004   VEC(constraint_edge_t,heap) *succvec = graph->succs[from];
1005   VEC(constraint_edge_t,heap) *predvec = graph->preds[from];
1006   int i;
1007   constraint_edge_t c;
1008   unsigned int j;
1009   bitmap_iterator bi;
1010
1011   /* Merge all the zero weighted predecessor edges.  */
1012   if (graph->zero_weight_preds[from])
1013     {
1014       if (!graph->zero_weight_preds[to])
1015         graph->zero_weight_preds[to] = BITMAP_ALLOC (&predbitmap_obstack);
1016       
1017       EXECUTE_IF_SET_IN_BITMAP (graph->zero_weight_preds[from], 0, j, bi)
1018         {
1019           if (j != to)
1020             {
1021               bitmap_clear_bit (graph->zero_weight_succs[j], from);
1022               bitmap_set_bit (graph->zero_weight_succs[j], to);
1023             }
1024         }
1025       bitmap_ior_into (graph->zero_weight_preds[to], 
1026                        graph->zero_weight_preds[from]);
1027     }
1028
1029   /* Merge all the zero weighted successor edges.  */
1030   if (graph->zero_weight_succs[from])
1031     {
1032       if (!graph->zero_weight_succs[to])
1033         graph->zero_weight_succs[to] = BITMAP_ALLOC (&ptabitmap_obstack);
1034       EXECUTE_IF_SET_IN_BITMAP (graph->zero_weight_succs[from], 0, j, bi)
1035         {
1036           bitmap_clear_bit (graph->zero_weight_preds[j], from);
1037           bitmap_set_bit (graph->zero_weight_preds[j], to);
1038         }
1039       bitmap_ior_into (graph->zero_weight_succs[to], 
1040                        graph->zero_weight_succs[from]);
1041     }
1042
1043   /* Merge all the nonzero weighted predecessor edges.  */
1044   for (i = 0; VEC_iterate (constraint_edge_t, predvec, i, c); i++)
1045     {
1046       unsigned int d = c->dest;
1047       bitmap temp;
1048       bitmap *weights;
1049
1050       if (c->dest == from)
1051         d = to;
1052
1053       add_graph_edge (graph, to, d);
1054
1055       temp = *(get_graph_weights (graph, from, c->dest));      
1056       if (temp)
1057         {
1058           weights = get_graph_weights (graph, to, d);
1059           if (!*weights)
1060             *weights = allocate_graph_weights (graph, to, d);
1061           
1062           bitmap_ior_into (*weights, temp);
1063         }
1064       
1065     }
1066   
1067   /* Merge all the nonzero weighted successor edges.  */
1068   for (i = 0; VEC_iterate (constraint_edge_t, succvec, i, c); i++)
1069     {
1070       unsigned int d = c->dest;
1071       bitmap temp;
1072       bitmap *weights;
1073
1074       if (c->dest == from)
1075         d = to;
1076
1077       add_graph_edge (graph, d, to);
1078
1079       temp = *(get_graph_weights (graph, c->dest, from));
1080       if (temp)
1081         {
1082           weights = get_graph_weights (graph, d, to);
1083           if (!*weights)
1084             *weights = allocate_graph_weights (graph, d, to);
1085           bitmap_ior_into (*weights, temp);
1086         }
1087     }
1088   clear_edges_for_node (graph, from);
1089 }
1090
1091 /* Add a graph edge to GRAPH, going from TO to FROM, with WEIGHT, if
1092    it doesn't exist in the graph already.
1093    Return false if the edge already existed, true otherwise.  */
1094
1095 static bool
1096 int_add_graph_edge (constraint_graph_t graph, unsigned int to, 
1097                     unsigned int from, unsigned HOST_WIDE_INT weight)
1098 {
1099   if (to == from && weight == 0)
1100     {
1101       return false;
1102     }
1103   else
1104     {
1105       bool r = false;
1106
1107       if (weight == 0)
1108         {
1109           if (!graph->zero_weight_preds[to])
1110             graph->zero_weight_preds[to] = BITMAP_ALLOC (&predbitmap_obstack);
1111           if (!graph->zero_weight_succs[from])
1112             graph->zero_weight_succs[from] = BITMAP_ALLOC (&ptabitmap_obstack);
1113           if (!bitmap_bit_p (graph->zero_weight_succs[from], to))
1114             {
1115               edge_added = true;
1116               r = true;
1117               stats.num_edges++;
1118               bitmap_set_bit (graph->zero_weight_preds[to], from);
1119               bitmap_set_bit (graph->zero_weight_succs[from], to);
1120             }
1121         }
1122       else
1123         {
1124           bitmap *weights;
1125
1126           r = add_graph_edge (graph, to, from);
1127           weights = get_graph_weights (graph, to, from);
1128
1129           if (!*weights)
1130             {
1131               r = true;
1132               *weights = allocate_graph_weights (graph, to, from);
1133               bitmap_set_bit (*weights, weight);
1134             }
1135           else
1136             {
1137               r |= !bitmap_bit_p (*weights, weight);
1138               bitmap_set_bit (*weights, weight);
1139             }
1140         }
1141       
1142       return r;
1143     }
1144 }
1145
1146
1147 /* Return true if {DEST.SRC} is an existing graph edge in GRAPH.  */
1148
1149 static bool
1150 valid_graph_edge (constraint_graph_t graph, unsigned int src, 
1151                   unsigned int dest)
1152 {
1153   struct constraint_edge lookfor;
1154   lookfor.dest = src;
1155   
1156   return (graph->zero_weight_succs[dest] 
1157       && bitmap_bit_p (graph->zero_weight_succs[dest], src)) 
1158     || constraint_edge_vec_find (graph->succs[dest], lookfor) != NULL;
1159 }
1160
1161 /* Return true if {DEST, SRC} is an existing weighted graph edge (IE has
1162    a weight other than 0) in GRAPH.  */
1163 static bool
1164 valid_weighted_graph_edge (constraint_graph_t graph, unsigned int src, 
1165                            unsigned int dest)
1166 {
1167   struct constraint_edge lookfor;
1168   lookfor.dest = src;
1169   
1170   return graph->preds[src] 
1171     && constraint_edge_vec_find (graph->succs[dest], lookfor) != NULL;
1172 }
1173
1174
1175 /* Build the constraint graph.  */
1176
1177 static void
1178 build_constraint_graph (void)
1179 {
1180   int i = 0;
1181   constraint_t c;
1182
1183   graph = XNEW (struct constraint_graph);
1184   graph->succs = XCNEWVEC (VEC(constraint_edge_t,heap) *, VEC_length (varinfo_t, varmap) + 1);
1185   graph->preds = XCNEWVEC (VEC(constraint_edge_t,heap) *, VEC_length (varinfo_t, varmap) + 1);
1186   graph->zero_weight_succs = XCNEWVEC (bitmap, VEC_length (varinfo_t, varmap) + 1);
1187   graph->zero_weight_preds = XCNEWVEC (bitmap, VEC_length (varinfo_t, varmap) + 1);
1188
1189   for (i = 0; VEC_iterate (constraint_t, constraints, i, c); i++)
1190     {
1191       struct constraint_expr lhs = c->lhs;
1192       struct constraint_expr rhs = c->rhs;
1193       unsigned int lhsvar = get_varinfo_fc (lhs.var)->id;
1194       unsigned int rhsvar = get_varinfo_fc (rhs.var)->id;
1195
1196       if (lhs.type == DEREF)
1197         {
1198           /* *x = y or *x = &y (complex) */
1199           if (rhs.type == ADDRESSOF || rhsvar > anything_id)
1200             insert_into_complex (lhsvar, c);
1201         }
1202       else if (rhs.type == DEREF)
1203         {
1204           /* !special var= *y */
1205           if (!(get_varinfo (lhsvar)->is_special_var))
1206             insert_into_complex (rhsvar, c);
1207         }
1208       else if (rhs.type == ADDRESSOF)
1209         {
1210           /* x = &y */
1211           bitmap_set_bit (get_varinfo (lhsvar)->solution, rhsvar);
1212         }
1213       else if (lhsvar > anything_id)
1214         {
1215           /* Ignore 0 weighted self edges, as they can't possibly contribute
1216              anything */
1217           if (lhsvar != rhsvar || rhs.offset != 0 || lhs.offset != 0)
1218             {
1219               /* x = y (simple) */
1220               int_add_graph_edge (graph, lhs.var, rhs.var, rhs.offset);
1221             }
1222           
1223         }
1224     }
1225 }
1226
1227
1228 /* Changed variables on the last iteration.  */
1229 static unsigned int changed_count;
1230 static sbitmap changed;
1231
1232 DEF_VEC_I(unsigned);
1233 DEF_VEC_ALLOC_I(unsigned,heap);
1234
1235
1236 /* Strongly Connected Component visitation info.  */
1237
1238 struct scc_info
1239 {
1240   sbitmap visited;
1241   sbitmap in_component;
1242   int current_index;
1243   unsigned int *visited_index;
1244   VEC(unsigned,heap) *scc_stack;
1245   VEC(unsigned,heap) *unification_queue;
1246 };
1247
1248
1249 /* Recursive routine to find strongly connected components in GRAPH.
1250    SI is the SCC info to store the information in, and N is the id of current
1251    graph node we are processing.
1252    
1253    This is Tarjan's strongly connected component finding algorithm, as
1254    modified by Nuutila to keep only non-root nodes on the stack.  
1255    The algorithm can be found in "On finding the strongly connected
1256    connected components in a directed graph" by Esko Nuutila and Eljas
1257    Soisalon-Soininen, in Information Processing Letters volume 49,
1258    number 1, pages 9-14.  */
1259
1260 static void
1261 scc_visit (constraint_graph_t graph, struct scc_info *si, unsigned int n)
1262 {
1263   unsigned int i;
1264   bitmap_iterator bi;
1265
1266   gcc_assert (get_varinfo (n)->node == n);
1267   SET_BIT (si->visited, n);
1268   RESET_BIT (si->in_component, n);
1269   si->visited_index[n] = si->current_index ++;
1270   
1271   /* Visit all the successors.  */
1272   EXECUTE_IF_IN_NONNULL_BITMAP (graph->zero_weight_succs[n], 0, i, bi)
1273     {
1274       unsigned int w = i;
1275       if (!TEST_BIT (si->visited, w))
1276         scc_visit (graph, si, w);
1277       if (!TEST_BIT (si->in_component, w))
1278         {
1279           unsigned int t = get_varinfo (w)->node;
1280           unsigned int nnode = get_varinfo (n)->node;
1281           if (si->visited_index[t] < si->visited_index[nnode])
1282             get_varinfo (n)->node = t;
1283         }
1284     }
1285   
1286   /* See if any components have been identified.  */
1287   if (get_varinfo (n)->node == n)
1288     {
1289       unsigned int t = si->visited_index[n];
1290       SET_BIT (si->in_component, n);
1291       while (VEC_length (unsigned, si->scc_stack) != 0 
1292              && t < si->visited_index[VEC_last (unsigned, si->scc_stack)])
1293         {
1294           unsigned int w = VEC_pop (unsigned, si->scc_stack);
1295           get_varinfo (w)->node = n;
1296           SET_BIT (si->in_component, w);
1297           /* Mark this node for collapsing.  */
1298           VEC_safe_push (unsigned, heap, si->unification_queue, w);
1299         } 
1300     }
1301   else
1302     VEC_safe_push (unsigned, heap, si->scc_stack, n);
1303 }
1304
1305
1306 /* Collapse two variables into one variable.  */
1307
1308 static void
1309 collapse_nodes (constraint_graph_t graph, unsigned int to, unsigned int from)
1310 {
1311   bitmap tosol, fromsol;
1312
1313   condense_varmap_nodes (to, from);
1314   tosol = get_varinfo (to)->solution;
1315   fromsol = get_varinfo (from)->solution;
1316   bitmap_ior_into (tosol, fromsol);
1317   merge_graph_nodes (graph, to, from);
1318
1319   if (valid_graph_edge (graph, to, to))
1320     {
1321       if (graph->zero_weight_preds[to])
1322         {
1323           bitmap_clear_bit (graph->zero_weight_preds[to], to);
1324           bitmap_clear_bit (graph->zero_weight_succs[to], to);
1325         }
1326       if (valid_weighted_graph_edge (graph, to, to))
1327         {
1328           bitmap weights = *(get_graph_weights (graph, to, to));
1329           if (!weights || bitmap_empty_p (weights))
1330             erase_graph_self_edge (graph, to);
1331         }
1332     }
1333   BITMAP_FREE (fromsol);
1334   get_varinfo (to)->address_taken |= get_varinfo (from)->address_taken;
1335   get_varinfo (to)->indirect_target |= get_varinfo (from)->indirect_target;
1336 }
1337
1338
1339 /* Unify nodes in GRAPH that we have found to be part of a cycle.
1340    SI is the Strongly Connected Components information structure that tells us
1341    what components to unify.
1342    UPDATE_CHANGED should be set to true if the changed sbitmap and changed
1343    count should be updated to reflect the unification.  */
1344
1345 static void
1346 process_unification_queue (constraint_graph_t graph, struct scc_info *si,
1347                            bool update_changed)
1348 {
1349   size_t i = 0;
1350   bitmap tmp = BITMAP_ALLOC (update_changed ? &iteration_obstack : NULL);
1351   bitmap_clear (tmp);
1352
1353   /* We proceed as follows:
1354
1355      For each component in the queue (components are delineated by
1356      when current_queue_element->node != next_queue_element->node):
1357
1358         rep = representative node for component
1359
1360         For each node (tounify) to be unified in the component,
1361            merge the solution for tounify into tmp bitmap
1362
1363            clear solution for tounify
1364
1365            merge edges from tounify into rep
1366
1367            merge complex constraints from tounify into rep
1368
1369            update changed count to note that tounify will never change
1370            again
1371
1372         Merge tmp into solution for rep, marking rep changed if this
1373         changed rep's solution.
1374         
1375         Delete any 0 weighted self-edges we now have for rep.  */
1376   while (i != VEC_length (unsigned, si->unification_queue))
1377     {
1378       unsigned int tounify = VEC_index (unsigned, si->unification_queue, i);
1379       unsigned int n = get_varinfo (tounify)->node;
1380
1381       if (dump_file && (dump_flags & TDF_DETAILS))
1382         fprintf (dump_file, "Unifying %s to %s\n", 
1383                  get_varinfo (tounify)->name,
1384                  get_varinfo (n)->name);
1385       if (update_changed)
1386         stats.unified_vars_dynamic++;
1387       else
1388         stats.unified_vars_static++;
1389       bitmap_ior_into (tmp, get_varinfo (tounify)->solution);
1390       merge_graph_nodes (graph, n, tounify);
1391       condense_varmap_nodes (n, tounify);
1392       
1393       if (update_changed && TEST_BIT (changed, tounify))
1394         {
1395           RESET_BIT (changed, tounify);
1396           if (!TEST_BIT (changed, n))
1397             SET_BIT (changed, n);
1398           else
1399             {
1400               gcc_assert (changed_count > 0);
1401               changed_count--;
1402             }
1403         }
1404
1405       bitmap_clear (get_varinfo (tounify)->solution);
1406       ++i;
1407
1408       /* If we've either finished processing the entire queue, or
1409          finished processing all nodes for component n, update the solution for
1410          n.  */
1411       if (i == VEC_length (unsigned, si->unification_queue)
1412           || get_varinfo (VEC_index (unsigned, si->unification_queue, i))->node != n)
1413         {
1414           /* If the solution changes because of the merging, we need to mark
1415              the variable as changed.  */
1416           if (bitmap_ior_into (get_varinfo (n)->solution, tmp))
1417             {
1418               if (update_changed && !TEST_BIT (changed, n))
1419                 {
1420                   SET_BIT (changed, n);
1421                   changed_count++;
1422                 }
1423             }
1424           bitmap_clear (tmp);
1425
1426           if (valid_graph_edge (graph, n, n))
1427             {
1428               if (graph->zero_weight_succs[n])
1429                 {
1430                   if (graph->zero_weight_preds[n])
1431                     bitmap_clear_bit (graph->zero_weight_preds[n], n);
1432                   bitmap_clear_bit (graph->zero_weight_succs[n], n);
1433                 }
1434               if (valid_weighted_graph_edge (graph, n, n))
1435                 {
1436                   bitmap weights = *(get_graph_weights (graph, n, n));
1437                   if (!weights || bitmap_empty_p (weights))
1438                     erase_graph_self_edge (graph, n);
1439                 }
1440             }
1441         }
1442     }
1443   BITMAP_FREE (tmp);
1444 }
1445
1446
1447 /* Information needed to compute the topological ordering of a graph.  */
1448
1449 struct topo_info
1450 {
1451   /* sbitmap of visited nodes.  */
1452   sbitmap visited;
1453   /* Array that stores the topological order of the graph, *in
1454      reverse*.  */
1455   VEC(unsigned,heap) *topo_order;
1456 };
1457
1458
1459 /* Initialize and return a topological info structure.  */
1460
1461 static struct topo_info *
1462 init_topo_info (void)
1463 {
1464   size_t size = VEC_length (varinfo_t, varmap);
1465   struct topo_info *ti = XNEW (struct topo_info);
1466   ti->visited = sbitmap_alloc (size);
1467   sbitmap_zero (ti->visited);
1468   ti->topo_order = VEC_alloc (unsigned, heap, 1);
1469   return ti;
1470 }
1471
1472
1473 /* Free the topological sort info pointed to by TI.  */
1474
1475 static void
1476 free_topo_info (struct topo_info *ti)
1477 {
1478   sbitmap_free (ti->visited);
1479   VEC_free (unsigned, heap, ti->topo_order);
1480   free (ti);
1481 }
1482
1483 /* Visit the graph in topological order, and store the order in the
1484    topo_info structure.  */
1485
1486 static void
1487 topo_visit (constraint_graph_t graph, struct topo_info *ti,
1488             unsigned int n)
1489 {
1490   VEC(constraint_edge_t,heap) *succs = graph->succs[n];
1491   bitmap temp;
1492   bitmap_iterator bi;
1493   constraint_edge_t c;
1494   int i;
1495   unsigned int j;
1496
1497   SET_BIT (ti->visited, n);
1498   if (VEC_length (constraint_edge_t, succs) != 0)
1499     {
1500       temp = BITMAP_ALLOC (&iteration_obstack);
1501       if (graph->zero_weight_succs[n])
1502         bitmap_ior_into (temp, graph->zero_weight_succs[n]);
1503       for (i = 0; VEC_iterate (constraint_edge_t, succs, i, c); i++)
1504         bitmap_set_bit (temp, c->dest);
1505     }
1506   else 
1507     temp = graph->zero_weight_succs[n];
1508
1509   if (temp) 
1510     EXECUTE_IF_SET_IN_BITMAP (temp, 0, j, bi)
1511       {
1512         if (!TEST_BIT (ti->visited, j))
1513           topo_visit (graph, ti, j);
1514       }
1515   VEC_safe_push (unsigned, heap, ti->topo_order, n);
1516 }
1517
1518 /* Return true if variable N + OFFSET is a legal field of N.  */
1519
1520 static bool 
1521 type_safe (unsigned int n, unsigned HOST_WIDE_INT *offset)
1522 {
1523   varinfo_t ninfo = get_varinfo (n);
1524
1525   /* For things we've globbed to single variables, any offset into the
1526      variable acts like the entire variable, so that it becomes offset
1527      0.  */
1528   if (ninfo->is_special_var
1529       || ninfo->is_artificial_var
1530       || ninfo->is_unknown_size_var)
1531     {
1532       *offset = 0;
1533       return true;
1534     }
1535   return (get_varinfo (n)->offset + *offset) < get_varinfo (n)->fullsize;
1536 }
1537
1538 /* Process a constraint C that represents *x = &y.  */
1539
1540 static void
1541 do_da_constraint (constraint_graph_t graph ATTRIBUTE_UNUSED,
1542                   constraint_t c, bitmap delta)
1543 {
1544   unsigned int rhs = c->rhs.var;
1545   unsigned int j;
1546   bitmap_iterator bi;
1547
1548   /* For each member j of Delta (Sol(x)), add x to Sol(j)  */
1549   EXECUTE_IF_SET_IN_BITMAP (delta, 0, j, bi)
1550     {
1551       unsigned HOST_WIDE_INT offset = c->lhs.offset;
1552       if (type_safe (j, &offset) && !(get_varinfo (j)->is_special_var))
1553         {
1554         /* *x != NULL && *x != ANYTHING*/
1555           varinfo_t v;
1556           unsigned int t;
1557           bitmap sol;
1558           unsigned HOST_WIDE_INT fieldoffset = get_varinfo (j)->offset + offset;
1559
1560           v = first_vi_for_offset (get_varinfo (j), fieldoffset);
1561           if (!v)
1562             continue;
1563           t = v->node;
1564           sol = get_varinfo (t)->solution;
1565           if (!bitmap_bit_p (sol, rhs))
1566             {             
1567               bitmap_set_bit (sol, rhs);
1568               if (!TEST_BIT (changed, t))
1569                 {
1570                   SET_BIT (changed, t);
1571                   changed_count++;
1572                 }
1573             }
1574         }
1575       else if (0 && dump_file && !(get_varinfo (j)->is_special_var))
1576         fprintf (dump_file, "Untypesafe usage in do_da_constraint.\n");
1577       
1578     }
1579 }
1580
1581 /* Process a constraint C that represents x = *y, using DELTA as the
1582    starting solution.  */
1583
1584 static void
1585 do_sd_constraint (constraint_graph_t graph, constraint_t c,
1586                   bitmap delta)
1587 {
1588   unsigned int lhs = get_varinfo (c->lhs.var)->node;
1589   bool flag = false;
1590   bitmap sol = get_varinfo (lhs)->solution;
1591   unsigned int j;
1592   bitmap_iterator bi;
1593
1594  if (bitmap_bit_p (delta, anything_id))
1595    {
1596      flag = !bitmap_bit_p (sol, anything_id);
1597      if (flag)
1598        bitmap_set_bit (sol, anything_id);
1599      goto done;
1600    }
1601   /* For each variable j in delta (Sol(y)), add    
1602      an edge in the graph from j to x, and union Sol(j) into Sol(x).  */
1603   EXECUTE_IF_SET_IN_BITMAP (delta, 0, j, bi)
1604     {
1605       unsigned HOST_WIDE_INT roffset = c->rhs.offset;
1606       if (type_safe (j, &roffset))
1607         {
1608           varinfo_t v;
1609           unsigned HOST_WIDE_INT fieldoffset = get_varinfo (j)->offset + roffset;
1610           unsigned int t;
1611
1612           v = first_vi_for_offset (get_varinfo (j), fieldoffset);
1613           if (!v)
1614             continue;
1615           t = v->node;
1616
1617           /* Adding edges from the special vars is pointless.
1618              They don't have sets that can change.  */
1619           if (get_varinfo (t) ->is_special_var)
1620             flag |= bitmap_ior_into (sol, get_varinfo (t)->solution);
1621           else if (int_add_graph_edge (graph, lhs, t, 0))
1622             flag |= bitmap_ior_into (sol, get_varinfo (t)->solution);
1623         }
1624       else if (0 && dump_file && !(get_varinfo (j)->is_special_var))
1625         fprintf (dump_file, "Untypesafe usage in do_sd_constraint\n");
1626       
1627     }
1628
1629 done:
1630   /* If the LHS solution changed, mark the var as changed.  */
1631   if (flag)
1632     {
1633       get_varinfo (lhs)->solution = sol;
1634       if (!TEST_BIT (changed, lhs))
1635         {
1636           SET_BIT (changed, lhs);
1637           changed_count++;
1638         }
1639     }    
1640 }
1641
1642 /* Process a constraint C that represents *x = y.  */
1643
1644 static void
1645 do_ds_constraint (constraint_graph_t graph, constraint_t c, bitmap delta)
1646 {
1647   unsigned int rhs = get_varinfo (c->rhs.var)->node;
1648   unsigned HOST_WIDE_INT roff = c->rhs.offset;
1649   bitmap sol = get_varinfo (rhs)->solution;
1650   unsigned int j;
1651   bitmap_iterator bi;
1652
1653  if (bitmap_bit_p (sol, anything_id))
1654    {
1655      EXECUTE_IF_SET_IN_BITMAP (delta, 0, j, bi)
1656        {
1657          varinfo_t jvi = get_varinfo (j);
1658          unsigned int t;
1659          unsigned int loff = c->lhs.offset;
1660          unsigned HOST_WIDE_INT fieldoffset = jvi->offset + loff;
1661          varinfo_t v;
1662
1663          v = first_vi_for_offset (get_varinfo (j), fieldoffset);
1664          if (!v)
1665            continue;
1666          t = v->node;
1667          
1668          if (!bitmap_bit_p (get_varinfo (t)->solution, anything_id))
1669            {
1670              bitmap_set_bit (get_varinfo (t)->solution, anything_id);
1671              if (!TEST_BIT (changed, t))
1672                {
1673                  SET_BIT (changed, t);
1674                  changed_count++;
1675                }
1676            }
1677        }
1678      return;
1679    }
1680
1681   /* For each member j of delta (Sol(x)), add an edge from y to j and
1682      union Sol(y) into Sol(j) */
1683   EXECUTE_IF_SET_IN_BITMAP (delta, 0, j, bi)
1684     {
1685       unsigned HOST_WIDE_INT loff = c->lhs.offset;
1686       if (type_safe (j, &loff) && !(get_varinfo(j)->is_special_var))
1687         {
1688           varinfo_t v;
1689           unsigned int t;
1690           unsigned HOST_WIDE_INT fieldoffset = get_varinfo (j)->offset + loff;
1691
1692           v = first_vi_for_offset (get_varinfo (j), fieldoffset);
1693           if (!v)
1694             continue;
1695           t = v->node;
1696           if (int_add_graph_edge (graph, t, rhs, roff))
1697             {
1698               bitmap tmp = get_varinfo (t)->solution;
1699               if (set_union_with_increment (tmp, sol, roff))
1700                 {
1701                   get_varinfo (t)->solution = tmp;
1702                   if (t == rhs)
1703                     sol = get_varinfo (rhs)->solution;
1704                   if (!TEST_BIT (changed, t))
1705                     {
1706                       SET_BIT (changed, t);
1707                       changed_count++;
1708                     }
1709                 }
1710             }
1711         }    
1712       else if (0 && dump_file && !(get_varinfo (j)->is_special_var))
1713         fprintf (dump_file, "Untypesafe usage in do_ds_constraint\n");
1714     }
1715 }
1716
1717 /* Handle a non-simple (simple meaning requires no iteration), non-copy
1718    constraint (IE *x = &y, x = *y, and *x = y).  */
1719    
1720 static void
1721 do_complex_constraint (constraint_graph_t graph, constraint_t c, bitmap delta)
1722 {
1723   if (c->lhs.type == DEREF)
1724     {
1725       if (c->rhs.type == ADDRESSOF)
1726         {
1727           /* *x = &y */
1728           do_da_constraint (graph, c, delta);
1729         }
1730       else
1731         {
1732           /* *x = y */
1733           do_ds_constraint (graph, c, delta);
1734         }
1735     }
1736   else
1737     {
1738       /* x = *y */
1739       if (!(get_varinfo (c->lhs.var)->is_special_var))
1740         do_sd_constraint (graph, c, delta);
1741     }
1742 }
1743
1744 /* Initialize and return a new SCC info structure.  */
1745
1746 static struct scc_info *
1747 init_scc_info (void)
1748 {
1749   struct scc_info *si = XNEW (struct scc_info);
1750   size_t size = VEC_length (varinfo_t, varmap);
1751
1752   si->current_index = 0;
1753   si->visited = sbitmap_alloc (size);
1754   sbitmap_zero (si->visited);
1755   si->in_component = sbitmap_alloc (size);
1756   sbitmap_ones (si->in_component);
1757   si->visited_index = XCNEWVEC (unsigned int, size + 1);
1758   si->scc_stack = VEC_alloc (unsigned, heap, 1);
1759   si->unification_queue = VEC_alloc (unsigned, heap, 1);
1760   return si;
1761 }
1762
1763 /* Free an SCC info structure pointed to by SI */
1764
1765 static void
1766 free_scc_info (struct scc_info *si)
1767 {  
1768   sbitmap_free (si->visited);
1769   sbitmap_free (si->in_component);
1770   free (si->visited_index);
1771   VEC_free (unsigned, heap, si->scc_stack);
1772   VEC_free (unsigned, heap, si->unification_queue);
1773   free(si); 
1774 }
1775
1776
1777 /* Find cycles in GRAPH that occur, using strongly connected components, and
1778    collapse the cycles into a single representative node.  if UPDATE_CHANGED
1779    is true, then update the changed sbitmap to note those nodes whose
1780    solutions have changed as a result of collapsing.  */
1781
1782 static void
1783 find_and_collapse_graph_cycles (constraint_graph_t graph, bool update_changed)
1784 {
1785   unsigned int i;
1786   unsigned int size = VEC_length (varinfo_t, varmap);
1787   struct scc_info *si = init_scc_info ();
1788
1789   for (i = 0; i != size; ++i)
1790     if (!TEST_BIT (si->visited, i) && get_varinfo (i)->node == i)
1791       scc_visit (graph, si, i);
1792   
1793   process_unification_queue (graph, si, update_changed);
1794   free_scc_info (si);
1795 }
1796
1797 /* Compute a topological ordering for GRAPH, and store the result in the
1798    topo_info structure TI.  */
1799
1800 static void 
1801 compute_topo_order (constraint_graph_t graph,
1802                     struct topo_info *ti)
1803 {
1804   unsigned int i;
1805   unsigned int size = VEC_length (varinfo_t, varmap);
1806   
1807   for (i = 0; i != size; ++i)
1808     if (!TEST_BIT (ti->visited, i) && get_varinfo (i)->node == i)
1809       topo_visit (graph, ti, i);
1810 }
1811
1812 /* Return true if bitmap B is empty, or a bitmap other than bit 0 is set. */
1813
1814 static bool
1815 bitmap_other_than_zero_bit_set (bitmap b)
1816 {
1817   unsigned int i;
1818   bitmap_iterator bi;
1819
1820   if (bitmap_empty_p (b))
1821     return false;
1822   EXECUTE_IF_SET_IN_BITMAP (b, 1, i, bi)
1823     return true;
1824   return false;
1825 }
1826
1827 /* Perform offline variable substitution.
1828    
1829    This is a linear time way of identifying variables that must have
1830    equivalent points-to sets, including those caused by static cycles,
1831    and single entry subgraphs, in the constraint graph.
1832
1833    The technique is described in "Off-line variable substitution for
1834    scaling points-to analysis" by Atanas Rountev and Satish Chandra,
1835    in "ACM SIGPLAN Notices" volume 35, number 5, pages 47-56.  */
1836
1837 static void
1838 perform_var_substitution (constraint_graph_t graph)
1839 {
1840   struct topo_info *ti = init_topo_info ();
1841  
1842   bitmap_obstack_initialize (&iteration_obstack);
1843   /* Compute the topological ordering of the graph, then visit each
1844      node in topological order.  */
1845   compute_topo_order (graph, ti);
1846  
1847   while (VEC_length (unsigned, ti->topo_order) != 0)
1848     {
1849       unsigned int i = VEC_pop (unsigned, ti->topo_order);
1850       unsigned int pred;
1851       varinfo_t vi = get_varinfo (i);
1852       bool okay_to_elim = false;
1853       unsigned int root = VEC_length (varinfo_t, varmap);
1854       VEC(constraint_edge_t,heap) *predvec = graph->preds[i];
1855       constraint_edge_t ce = NULL;
1856       bitmap tmp;
1857       unsigned int k;
1858       bitmap_iterator bi;
1859
1860       /* We can't eliminate things whose address is taken, or which is
1861          the target of a dereference.  */
1862       if (vi->address_taken || vi->indirect_target)
1863         continue;
1864
1865       /* See if all predecessors of I are ripe for elimination */
1866       EXECUTE_IF_IN_NONNULL_BITMAP (graph->zero_weight_preds[i], 0, k, bi)
1867           {
1868             unsigned int w;
1869             w = get_varinfo (k)->node;
1870
1871             /* We can't eliminate the node if one of the predecessors is
1872                part of a different strongly connected component.  */
1873             if (!okay_to_elim)
1874               {
1875                 root = w;
1876                 okay_to_elim = true;
1877               }
1878             else if (w != root)
1879               {
1880                 okay_to_elim = false;
1881                 break;
1882               }
1883
1884             /* Theorem 4 in Rountev and Chandra: If i is a direct node,
1885                then Solution(i) is a subset of Solution (w), where w is a
1886                predecessor in the graph.  
1887                Corollary: If all predecessors of i have the same
1888                points-to set, then i has that same points-to set as
1889                those predecessors.  */
1890             tmp = BITMAP_ALLOC (NULL);
1891             bitmap_and_compl (tmp, get_varinfo (i)->solution,
1892                               get_varinfo (w)->solution);
1893             if (!bitmap_empty_p (tmp))
1894               {
1895                 okay_to_elim = false;
1896                 BITMAP_FREE (tmp);
1897                 break;
1898               }
1899             BITMAP_FREE (tmp);
1900           }
1901
1902       if (okay_to_elim)
1903         for (pred = 0; 
1904              VEC_iterate (constraint_edge_t, predvec, pred, ce); 
1905              pred++)
1906           {
1907             bitmap weight;
1908             unsigned int w;
1909             weight = *(get_graph_weights (graph, i, ce->dest));
1910
1911             /* We can't eliminate variables that have nonzero weighted
1912                edges between them.  */
1913             if (weight && bitmap_other_than_zero_bit_set (weight))
1914               {
1915                 okay_to_elim = false;
1916                 break;
1917               }
1918             w = get_varinfo (ce->dest)->node;
1919
1920             /* We can't eliminate the node if one of the predecessors is
1921                part of a different strongly connected component.  */
1922             if (!okay_to_elim)
1923               {
1924                 root = w;
1925                 okay_to_elim = true;
1926               }
1927             else if (w != root)
1928               {
1929                 okay_to_elim = false;
1930                 break;
1931               }
1932
1933             /* Theorem 4 in Rountev and Chandra: If i is a direct node,
1934                then Solution(i) is a subset of Solution (w), where w is a
1935                predecessor in the graph.  
1936                Corollary: If all predecessors of i have the same
1937                points-to set, then i has that same points-to set as
1938                those predecessors.  */
1939             tmp = BITMAP_ALLOC (NULL);
1940             bitmap_and_compl (tmp, get_varinfo (i)->solution,
1941                               get_varinfo (w)->solution);
1942             if (!bitmap_empty_p (tmp))
1943               {
1944                 okay_to_elim = false;
1945                 BITMAP_FREE (tmp);
1946                 break;
1947               }
1948             BITMAP_FREE (tmp);
1949           }
1950
1951       /* See if the root is different than the original node. 
1952          If so, we've found an equivalence.  */
1953       if (root != get_varinfo (i)->node && okay_to_elim)
1954         {
1955           /* Found an equivalence */
1956           get_varinfo (i)->node = root;
1957           collapse_nodes (graph, root, i);
1958           if (dump_file && (dump_flags & TDF_DETAILS))
1959             fprintf (dump_file, "Collapsing %s into %s\n",
1960                      get_varinfo (i)->name,
1961                      get_varinfo (root)->name);
1962           stats.collapsed_vars++;
1963         }
1964     }
1965
1966   bitmap_obstack_release (&iteration_obstack);
1967   free_topo_info (ti);
1968 }
1969
1970 /* Solve the constraint graph GRAPH using our worklist solver.
1971    This is based on the PW* family of solvers from the "Efficient Field
1972    Sensitive Pointer Analysis for C" paper.
1973    It works by iterating over all the graph nodes, processing the complex
1974    constraints and propagating the copy constraints, until everything stops
1975    changed.  This corresponds to steps 6-8 in the solving list given above.  */
1976
1977 static void
1978 solve_graph (constraint_graph_t graph)
1979 {
1980   unsigned int size = VEC_length (varinfo_t, varmap);
1981   unsigned int i;
1982
1983   changed_count = size;
1984   changed = sbitmap_alloc (size);
1985   sbitmap_ones (changed);
1986   
1987   /* The already collapsed/unreachable nodes will never change, so we
1988      need to  account for them in changed_count.  */
1989   for (i = 0; i < size; i++)
1990     if (get_varinfo (i)->node != i)
1991       changed_count--;
1992   
1993   while (changed_count > 0)
1994     {
1995       unsigned int i;
1996       struct topo_info *ti = init_topo_info ();
1997       stats.iterations++;
1998
1999       bitmap_obstack_initialize (&iteration_obstack);
2000       
2001       if (edge_added)
2002         {
2003           /* We already did cycle elimination once, when we did
2004              variable substitution, so we don't need it again for the
2005              first iteration.  */
2006           if (stats.iterations > 1)
2007             find_and_collapse_graph_cycles (graph, true);
2008
2009           edge_added = false;
2010         }
2011
2012       compute_topo_order (graph, ti);
2013
2014       while (VEC_length (unsigned, ti->topo_order) != 0)
2015         {
2016           i = VEC_pop (unsigned, ti->topo_order);
2017           gcc_assert (get_varinfo (i)->node == i);
2018
2019           /* If the node has changed, we need to process the
2020              complex constraints and outgoing edges again.  */
2021           if (TEST_BIT (changed, i))
2022             {
2023               unsigned int j;
2024               constraint_t c;
2025               constraint_edge_t e = NULL;
2026               bitmap solution;
2027               bitmap_iterator bi;
2028               VEC(constraint_t,heap) *complex = get_varinfo (i)->complex;
2029               VEC(constraint_edge_t,heap) *succs;
2030
2031               RESET_BIT (changed, i);
2032               changed_count--;
2033
2034               /* Process the complex constraints */
2035               solution = get_varinfo (i)->solution;
2036               for (j = 0; VEC_iterate (constraint_t, complex, j, c); j++)
2037                 do_complex_constraint (graph, c, solution);
2038
2039               /* Propagate solution to all successors.  */
2040               succs = graph->succs[i];
2041               
2042               EXECUTE_IF_IN_NONNULL_BITMAP (graph->zero_weight_succs[i], 0, j, bi)
2043                 {
2044                   bitmap tmp = get_varinfo (j)->solution;
2045                   bool flag = false;
2046                   
2047                   flag = set_union_with_increment (tmp, solution, 0);
2048                   
2049                   if (flag)
2050                     {
2051                       get_varinfo (j)->solution = tmp;
2052                       if (!TEST_BIT (changed, j))
2053                         {
2054                           SET_BIT (changed, j);
2055                           changed_count++;
2056                         }
2057                     }
2058                 }
2059               for (j = 0; VEC_iterate (constraint_edge_t, succs, j, e); j++)
2060                 {
2061                   bitmap tmp = get_varinfo (e->dest)->solution;
2062                   bool flag = false;
2063                   unsigned int k;
2064                   bitmap weights = e->weights;
2065                   bitmap_iterator bi;
2066
2067                   gcc_assert (weights && !bitmap_empty_p (weights));
2068                   EXECUTE_IF_SET_IN_BITMAP (weights, 0, k, bi)
2069                     flag |= set_union_with_increment (tmp, solution, k);
2070
2071                   if (flag)
2072                     {
2073                       get_varinfo (e->dest)->solution = tmp;
2074                       if (!TEST_BIT (changed, e->dest))
2075                         {
2076                           SET_BIT (changed, e->dest);
2077                           changed_count++;
2078                         }
2079                     }
2080                 }
2081             }
2082         }
2083       free_topo_info (ti);
2084       bitmap_obstack_release (&iteration_obstack);
2085     }
2086
2087   sbitmap_free (changed);
2088 }
2089
2090
2091 /* CONSTRAINT AND VARIABLE GENERATION FUNCTIONS */
2092
2093 /* Map from trees to variable ids.  */    
2094 static htab_t id_for_tree;
2095
2096 typedef struct tree_id
2097 {
2098   tree t;
2099   unsigned int id;
2100 } *tree_id_t;
2101
2102 /* Hash a tree id structure.  */
2103
2104 static hashval_t 
2105 tree_id_hash (const void *p)
2106 {
2107   const tree_id_t ta = (tree_id_t) p;
2108   return htab_hash_pointer (ta->t);
2109 }
2110
2111 /* Return true if the tree in P1 and the tree in P2 are the same.  */
2112
2113 static int
2114 tree_id_eq (const void *p1, const void *p2)
2115 {
2116   const tree_id_t ta1 = (tree_id_t) p1;
2117   const tree_id_t ta2 = (tree_id_t) p2;
2118   return ta1->t == ta2->t;
2119 }
2120
2121 /* Insert ID as the variable id for tree T in the hashtable.  */
2122
2123 static void 
2124 insert_id_for_tree (tree t, int id)
2125 {
2126   void **slot;
2127   struct tree_id finder;
2128   tree_id_t new_pair;
2129   
2130   finder.t = t;
2131   slot = htab_find_slot (id_for_tree, &finder, INSERT);
2132   gcc_assert (*slot == NULL);
2133   new_pair = XNEW (struct tree_id);
2134   new_pair->t = t;
2135   new_pair->id = id;
2136   *slot = (void *)new_pair;
2137 }
2138
2139 /* Find the variable id for tree T in ID_FOR_TREE.  If T does not
2140    exist in the hash table, return false, otherwise, return true and
2141    set *ID to the id we found.  */
2142
2143 static bool
2144 lookup_id_for_tree (tree t, unsigned int *id)
2145 {
2146   tree_id_t pair;
2147   struct tree_id finder;
2148
2149   finder.t = t;
2150   pair = htab_find (id_for_tree,  &finder);
2151   if (pair == NULL)
2152     return false;
2153   *id = pair->id;
2154   return true;
2155 }
2156
2157 /* Return a printable name for DECL  */
2158
2159 static const char *
2160 alias_get_name (tree decl)
2161 {
2162   const char *res = get_name (decl);
2163   char *temp;
2164   int num_printed = 0;
2165
2166   if (res != NULL)
2167     return res;
2168
2169   res = "NULL";
2170   if (!dump_file)
2171     return res;
2172
2173   if (TREE_CODE (decl) == SSA_NAME)
2174     {
2175       num_printed = asprintf (&temp, "%s_%u", 
2176                               alias_get_name (SSA_NAME_VAR (decl)),
2177                               SSA_NAME_VERSION (decl));
2178     }
2179   else if (DECL_P (decl))
2180     {
2181       num_printed = asprintf (&temp, "D.%u", DECL_UID (decl));
2182     }
2183   if (num_printed > 0)
2184     {
2185       res = ggc_strdup (temp);
2186       free (temp);
2187     }
2188   return res;
2189 }
2190
2191 /* Find the variable id for tree T in the hashtable.
2192    If T doesn't exist in the hash table, create an entry for it.  */
2193
2194 static unsigned int
2195 get_id_for_tree (tree t)
2196 {
2197   tree_id_t pair;
2198   struct tree_id finder;
2199
2200   finder.t = t;
2201   pair = htab_find (id_for_tree,  &finder);
2202   if (pair == NULL)
2203     return create_variable_info_for (t, alias_get_name (t));
2204   
2205   return pair->id;
2206 }
2207
2208 /* Get a constraint expression from an SSA_VAR_P node.  */
2209
2210 static struct constraint_expr
2211 get_constraint_exp_from_ssa_var (tree t)
2212 {
2213   struct constraint_expr cexpr;
2214
2215   gcc_assert (SSA_VAR_P (t) || DECL_P (t));
2216
2217   /* For parameters, get at the points-to set for the actual parm
2218      decl.  */
2219   if (TREE_CODE (t) == SSA_NAME 
2220       && TREE_CODE (SSA_NAME_VAR (t)) == PARM_DECL 
2221       && default_def (SSA_NAME_VAR (t)) == t)
2222     return get_constraint_exp_from_ssa_var (SSA_NAME_VAR (t));
2223
2224   cexpr.type = SCALAR;
2225   
2226   cexpr.var = get_id_for_tree (t);
2227   /* If we determine the result is "anything", and we know this is readonly,
2228      say it points to readonly memory instead.  */
2229   if (cexpr.var == anything_id && TREE_READONLY (t))
2230     {
2231       cexpr.type = ADDRESSOF;
2232       cexpr.var = readonly_id;
2233     }
2234     
2235   cexpr.offset = 0;
2236   return cexpr;
2237 }
2238
2239 /* Process a completed constraint T, and add it to the constraint
2240    list.  */
2241
2242 static void
2243 process_constraint (constraint_t t)
2244 {
2245   struct constraint_expr rhs = t->rhs;
2246   struct constraint_expr lhs = t->lhs;
2247   
2248   gcc_assert (rhs.var < VEC_length (varinfo_t, varmap));
2249   gcc_assert (lhs.var < VEC_length (varinfo_t, varmap));
2250
2251   /* ANYTHING == ANYTHING is pointless.  */
2252   if (lhs.var == anything_id && rhs.var == anything_id)
2253     return;
2254
2255   /* If we have &ANYTHING = something, convert to SOMETHING = &ANYTHING) */
2256   else if (lhs.var == anything_id && lhs.type == ADDRESSOF)
2257     {
2258       rhs = t->lhs;
2259       t->lhs = t->rhs;
2260       t->rhs = rhs;
2261       process_constraint (t);
2262     }   
2263   /* This can happen in our IR with things like n->a = *p */
2264   else if (rhs.type == DEREF && lhs.type == DEREF && rhs.var != anything_id)
2265     {
2266       /* Split into tmp = *rhs, *lhs = tmp */
2267       tree rhsdecl = get_varinfo (rhs.var)->decl;
2268       tree pointertype = TREE_TYPE (rhsdecl);
2269       tree pointedtotype = TREE_TYPE (pointertype);
2270       tree tmpvar = create_tmp_var_raw (pointedtotype, "doubledereftmp");
2271       struct constraint_expr tmplhs = get_constraint_exp_from_ssa_var (tmpvar);
2272       
2273       /* If this is an aggregate of known size, we should have passed
2274          this off to do_structure_copy, and it should have broken it
2275          up.  */
2276       gcc_assert (!AGGREGATE_TYPE_P (pointedtotype) 
2277                   || get_varinfo (rhs.var)->is_unknown_size_var);
2278       
2279       process_constraint (new_constraint (tmplhs, rhs));
2280       process_constraint (new_constraint (lhs, tmplhs));
2281     }
2282   else if (rhs.type == ADDRESSOF)
2283     {
2284       varinfo_t vi;
2285       gcc_assert (rhs.offset == 0);
2286       
2287       for (vi = get_varinfo (rhs.var); vi != NULL; vi = vi->next)
2288         vi->address_taken = true;
2289
2290       VEC_safe_push (constraint_t, heap, constraints, t);
2291     }
2292   else
2293     {
2294       if (lhs.type != DEREF && rhs.type == DEREF)
2295         get_varinfo (lhs.var)->indirect_target = true;
2296       VEC_safe_push (constraint_t, heap, constraints, t);
2297     }
2298 }
2299
2300
2301 /* Return the position, in bits, of FIELD_DECL from the beginning of its
2302    structure.  */
2303
2304 static unsigned HOST_WIDE_INT
2305 bitpos_of_field (const tree fdecl)
2306 {
2307
2308   if (TREE_CODE (DECL_FIELD_OFFSET (fdecl)) != INTEGER_CST
2309       || TREE_CODE (DECL_FIELD_BIT_OFFSET (fdecl)) != INTEGER_CST)
2310     return -1;
2311   
2312   return (tree_low_cst (DECL_FIELD_OFFSET (fdecl), 1) * 8) 
2313          + tree_low_cst (DECL_FIELD_BIT_OFFSET (fdecl), 1);
2314 }
2315
2316
2317 /* Return true if an access to [ACCESSPOS, ACCESSSIZE]
2318    overlaps with a field at [FIELDPOS, FIELDSIZE] */
2319
2320 static bool
2321 offset_overlaps_with_access (const unsigned HOST_WIDE_INT fieldpos,
2322                              const unsigned HOST_WIDE_INT fieldsize,
2323                              const unsigned HOST_WIDE_INT accesspos,
2324                              const unsigned HOST_WIDE_INT accesssize)
2325 {
2326   if (fieldpos == accesspos && fieldsize == accesssize)
2327     return true;
2328   if (accesspos >= fieldpos && accesspos < (fieldpos + fieldsize))
2329     return true;
2330   if (accesspos < fieldpos && (accesspos + accesssize > fieldpos))
2331     return true;
2332   
2333   return false;
2334 }
2335
2336 /* Given a COMPONENT_REF T, return the constraint_expr for it.  */
2337
2338 static void
2339 get_constraint_for_component_ref (tree t, VEC(ce_s, heap) **results)
2340 {
2341   tree orig_t = t;
2342   HOST_WIDE_INT bitsize = -1;
2343   HOST_WIDE_INT bitmaxsize = -1;
2344   HOST_WIDE_INT bitpos;
2345   tree forzero;
2346   struct constraint_expr *result;
2347   unsigned int beforelength = VEC_length (ce_s, *results);
2348
2349   /* Some people like to do cute things like take the address of
2350      &0->a.b */
2351   forzero = t;
2352   while (!SSA_VAR_P (forzero) && !CONSTANT_CLASS_P (forzero))
2353     forzero = TREE_OPERAND (forzero, 0);
2354
2355   if (CONSTANT_CLASS_P (forzero) && integer_zerop (forzero)) 
2356     {
2357       struct constraint_expr temp;
2358       
2359       temp.offset = 0;
2360       temp.var = integer_id;
2361       temp.type = SCALAR;
2362       VEC_safe_push (ce_s, heap, *results, &temp);
2363       return;
2364     }
2365  
2366   t = get_ref_base_and_extent (t, &bitpos, &bitsize, &bitmaxsize);
2367   get_constraint_for (t, results);
2368   result = VEC_last (ce_s, *results);
2369   result->offset = bitpos;
2370
2371   gcc_assert (beforelength + 1 == VEC_length (ce_s, *results));
2372
2373   /* This can also happen due to weird offsetof type macros.  */
2374   if (TREE_CODE (t) != ADDR_EXPR && result->type == ADDRESSOF)
2375     result->type = SCALAR;
2376  
2377   if (result->type == SCALAR)
2378     {
2379       /* In languages like C, you can access one past the end of an
2380          array.  You aren't allowed to dereference it, so we can
2381          ignore this constraint. When we handle pointer subtraction,
2382          we may have to do something cute here.  */
2383       
2384       if (result->offset < get_varinfo (result->var)->fullsize
2385           && bitmaxsize != 0)
2386         {
2387           /* It's also not true that the constraint will actually start at the
2388              right offset, it may start in some padding.  We only care about
2389              setting the constraint to the first actual field it touches, so
2390              walk to find it.  */ 
2391           varinfo_t curr;
2392           for (curr = get_varinfo (result->var); curr; curr = curr->next)
2393             {
2394               if (offset_overlaps_with_access (curr->offset, curr->size,
2395                                                result->offset, bitmaxsize))
2396                 {
2397                   result->var = curr->id;
2398                   break;
2399                 }
2400             }
2401           /* assert that we found *some* field there. The user couldn't be
2402              accessing *only* padding.  */
2403           /* Still the user could access one past the end of an array
2404              embedded in a struct resulting in accessing *only* padding.  */
2405           gcc_assert (curr || ref_contains_array_ref (orig_t));
2406         }
2407       else if (bitmaxsize == 0)
2408         {
2409           if (dump_file && (dump_flags & TDF_DETAILS))
2410             fprintf (dump_file, "Access to zero-sized part of variable,"
2411                      "ignoring\n");
2412         }
2413       else
2414         if (dump_file && (dump_flags & TDF_DETAILS))
2415           fprintf (dump_file, "Access to past the end of variable, ignoring\n");
2416
2417       result->offset = 0;
2418     }
2419 }
2420
2421
2422 /* Dereference the constraint expression CONS, and return the result.
2423    DEREF (ADDRESSOF) = SCALAR
2424    DEREF (SCALAR) = DEREF
2425    DEREF (DEREF) = (temp = DEREF1; result = DEREF(temp))
2426    This is needed so that we can handle dereferencing DEREF constraints.  */
2427
2428 static void
2429 do_deref (VEC (ce_s, heap) **constraints)
2430 {
2431   struct constraint_expr *c;
2432   unsigned int i = 0;
2433   for (i = 0; VEC_iterate (ce_s, *constraints, i, c); i++)
2434     {
2435       if (c->type == SCALAR)
2436         c->type = DEREF;
2437       else if (c->type == ADDRESSOF)
2438         c->type = SCALAR;
2439       else if (c->type == DEREF)
2440         {
2441           tree tmpvar = create_tmp_var_raw (ptr_type_node, "dereftmp");
2442           struct constraint_expr tmplhs = get_constraint_exp_from_ssa_var (tmpvar);
2443           process_constraint (new_constraint (tmplhs, *c));
2444           c->var = tmplhs.var;
2445         }
2446       else
2447         gcc_unreachable ();
2448     }
2449 }
2450
2451
2452 /* Given a tree T, return the constraint expression for it.  */
2453
2454 static void
2455 get_constraint_for (tree t, VEC (ce_s, heap) **results)
2456 {
2457   struct constraint_expr temp;
2458
2459   /* x = integer is all glommed to a single variable, which doesn't
2460      point to anything by itself.  That is, of course, unless it is an
2461      integer constant being treated as a pointer, in which case, we
2462      will return that this is really the addressof anything.  This
2463      happens below, since it will fall into the default case. The only
2464      case we know something about an integer treated like a pointer is
2465      when it is the NULL pointer, and then we just say it points to
2466      NULL.  */
2467   if (TREE_CODE (t) == INTEGER_CST
2468       && !POINTER_TYPE_P (TREE_TYPE (t)))
2469     {
2470       temp.var = integer_id;
2471       temp.type = SCALAR;
2472       temp.offset = 0;
2473       VEC_safe_push (ce_s, heap, *results, &temp);
2474       return;
2475     }
2476   else if (TREE_CODE (t) == INTEGER_CST
2477            && integer_zerop (t))
2478     {
2479       temp.var = nothing_id;
2480       temp.type = ADDRESSOF;
2481       temp.offset = 0;
2482       VEC_safe_push (ce_s, heap, *results, &temp);
2483       return;
2484     }
2485
2486   switch (TREE_CODE_CLASS (TREE_CODE (t)))
2487     {
2488     case tcc_expression:
2489       {
2490         switch (TREE_CODE (t))
2491           {
2492           case ADDR_EXPR:
2493             {
2494               struct constraint_expr *c;
2495               unsigned int i;
2496               tree exp = TREE_OPERAND (t, 0);
2497               tree pttype = TREE_TYPE (TREE_TYPE (t));
2498
2499               get_constraint_for (exp, results);
2500               /* Make sure we capture constraints to all elements
2501                  of an array.  */
2502               if ((handled_component_p (exp)
2503                    && ref_contains_array_ref (exp))
2504                   || TREE_CODE (TREE_TYPE (exp)) == ARRAY_TYPE)
2505                 {
2506                   struct constraint_expr *origrhs;
2507                   varinfo_t origvar;
2508                   struct constraint_expr tmp;
2509
2510                   gcc_assert (VEC_length (ce_s, *results) == 1);
2511                   origrhs = VEC_last (ce_s, *results);
2512                   tmp = *origrhs;
2513                   VEC_pop (ce_s, *results);
2514                   origvar = get_varinfo (origrhs->var);
2515                   for (; origvar; origvar = origvar->next)
2516                     {
2517                       tmp.var = origvar->id;
2518                       VEC_safe_push (ce_s, heap, *results, &tmp);
2519                     }
2520                 }
2521               else if (VEC_length (ce_s, *results) == 1
2522                        && (AGGREGATE_TYPE_P (pttype)
2523                            || TREE_CODE (pttype) == COMPLEX_TYPE))
2524                 {
2525                   struct constraint_expr *origrhs;
2526                   varinfo_t origvar;
2527                   struct constraint_expr tmp;
2528
2529                   gcc_assert (VEC_length (ce_s, *results) == 1);
2530                   origrhs = VEC_last (ce_s, *results);
2531                   tmp = *origrhs;
2532                   VEC_pop (ce_s, *results);
2533                   origvar = get_varinfo (origrhs->var);
2534                   for (; origvar; origvar = origvar->next)
2535                     {
2536                       tmp.var = origvar->id;
2537                       VEC_safe_push (ce_s, heap, *results, &tmp);
2538                     }
2539                 }
2540               
2541               for (i = 0; VEC_iterate (ce_s, *results, i, c); i++)
2542                 {
2543                   if (c->type == DEREF)
2544                     c->type = SCALAR;
2545                   else 
2546                     c->type = ADDRESSOF;
2547                 }
2548               return;
2549             }
2550             break;
2551           case CALL_EXPR:
2552             
2553             /* XXX: In interprocedural mode, if we didn't have the
2554                body, we would need to do *each pointer argument =
2555                &ANYTHING added.  */
2556             if (call_expr_flags (t) & (ECF_MALLOC | ECF_MAY_BE_ALLOCA))
2557               {
2558                 varinfo_t vi;
2559                 tree heapvar = heapvar_lookup (t);
2560                 
2561                 if (heapvar == NULL)
2562                   {                 
2563                     heapvar = create_tmp_var_raw (ptr_type_node, "HEAP");
2564                     DECL_EXTERNAL (heapvar) = 1;
2565                     if (referenced_vars)
2566                       add_referenced_var (heapvar);
2567                     heapvar_insert (t, heapvar);
2568                   }
2569
2570                 temp.var = create_variable_info_for (heapvar,
2571                                                      alias_get_name (heapvar));
2572                 
2573                 vi = get_varinfo (temp.var);
2574                 vi->is_artificial_var = 1;
2575                 vi->is_heap_var = 1;
2576                 temp.type = ADDRESSOF;
2577                 temp.offset = 0;
2578                 VEC_safe_push (ce_s, heap, *results, &temp);
2579                 return;
2580               }
2581             /* FALLTHRU */
2582           default:
2583             {
2584               temp.type = ADDRESSOF;
2585               temp.var = anything_id;
2586               temp.offset = 0;
2587               VEC_safe_push (ce_s, heap, *results, &temp);
2588               return;
2589             }
2590           }
2591       }
2592     case tcc_reference:
2593       {
2594         switch (TREE_CODE (t))
2595           {
2596           case INDIRECT_REF:
2597             {
2598               get_constraint_for (TREE_OPERAND (t, 0), results);
2599               do_deref (results);
2600               return;
2601             }
2602           case ARRAY_REF:
2603           case ARRAY_RANGE_REF:
2604           case COMPONENT_REF:
2605             get_constraint_for_component_ref (t, results);
2606             return;
2607           default:
2608             {
2609               temp.type = ADDRESSOF;
2610               temp.var = anything_id;
2611               temp.offset = 0;
2612               VEC_safe_push (ce_s, heap, *results, &temp);
2613               return;
2614             }
2615           }
2616       }
2617     case tcc_unary:
2618       {
2619         switch (TREE_CODE (t))
2620           {
2621           case NOP_EXPR:
2622           case CONVERT_EXPR:
2623           case NON_LVALUE_EXPR:
2624             {
2625               tree op = TREE_OPERAND (t, 0);
2626               
2627               /* Cast from non-pointer to pointers are bad news for us.
2628                  Anything else, we see through */
2629               if (!(POINTER_TYPE_P (TREE_TYPE (t))
2630                     && ! POINTER_TYPE_P (TREE_TYPE (op))))
2631                 {
2632                   get_constraint_for (op, results);
2633                   return;
2634                 }
2635
2636               /* FALLTHRU  */
2637             }
2638           default:
2639             {
2640               temp.type = ADDRESSOF;
2641               temp.var = anything_id;
2642               temp.offset = 0;
2643               VEC_safe_push (ce_s, heap, *results, &temp);
2644               return;
2645             }
2646           }
2647       }
2648     case tcc_exceptional:
2649       {
2650         switch (TREE_CODE (t))
2651           {
2652           case PHI_NODE:           
2653             {
2654               get_constraint_for (PHI_RESULT (t), results);
2655               return;
2656             }
2657             break;
2658           case SSA_NAME:
2659             {
2660               struct constraint_expr temp;
2661               temp = get_constraint_exp_from_ssa_var (t);
2662               VEC_safe_push (ce_s, heap, *results, &temp);
2663               return;
2664             }
2665             break;
2666           default:
2667             {
2668               temp.type = ADDRESSOF;
2669               temp.var = anything_id;
2670               temp.offset = 0;
2671               VEC_safe_push (ce_s, heap, *results, &temp);
2672               return;
2673             }
2674           }
2675       }
2676     case tcc_declaration:
2677       {
2678         struct constraint_expr temp;
2679         temp = get_constraint_exp_from_ssa_var (t);
2680         VEC_safe_push (ce_s, heap, *results, &temp);
2681         return;
2682       }
2683     default:
2684       {
2685         temp.type = ADDRESSOF;
2686         temp.var = anything_id;
2687         temp.offset = 0;
2688         VEC_safe_push (ce_s, heap, *results, &temp);
2689         return;
2690       }
2691     }
2692 }
2693
2694
2695 /* Handle the structure copy case where we have a simple structure copy
2696    between LHS and RHS that is of SIZE (in bits) 
2697   
2698    For each field of the lhs variable (lhsfield)
2699      For each field of the rhs variable at lhsfield.offset (rhsfield)
2700        add the constraint lhsfield = rhsfield
2701
2702    If we fail due to some kind of type unsafety or other thing we
2703    can't handle, return false.  We expect the caller to collapse the
2704    variable in that case.  */
2705
2706 static bool
2707 do_simple_structure_copy (const struct constraint_expr lhs,
2708                           const struct constraint_expr rhs,
2709                           const unsigned HOST_WIDE_INT size)
2710 {
2711   varinfo_t p = get_varinfo (lhs.var);
2712   unsigned HOST_WIDE_INT pstart, last;
2713   pstart = p->offset;
2714   last = p->offset + size;
2715   for (; p && p->offset < last; p = p->next)
2716     {
2717       varinfo_t q;
2718       struct constraint_expr templhs = lhs;
2719       struct constraint_expr temprhs = rhs;
2720       unsigned HOST_WIDE_INT fieldoffset;
2721
2722       templhs.var = p->id;            
2723       q = get_varinfo (temprhs.var);
2724       fieldoffset = p->offset - pstart;
2725       q = first_vi_for_offset (q, q->offset + fieldoffset);
2726       if (!q)
2727         return false;
2728       temprhs.var = q->id;
2729       process_constraint (new_constraint (templhs, temprhs));
2730     }
2731   return true;
2732 }
2733
2734
2735 /* Handle the structure copy case where we have a  structure copy between a
2736    aggregate on the LHS and a dereference of a pointer on the RHS
2737    that is of SIZE (in bits) 
2738   
2739    For each field of the lhs variable (lhsfield)
2740        rhs.offset = lhsfield->offset
2741        add the constraint lhsfield = rhs
2742 */
2743
2744 static void
2745 do_rhs_deref_structure_copy (const struct constraint_expr lhs,
2746                              const struct constraint_expr rhs,
2747                              const unsigned HOST_WIDE_INT size)
2748 {
2749   varinfo_t p = get_varinfo (lhs.var);
2750   unsigned HOST_WIDE_INT pstart,last;
2751   pstart = p->offset;
2752   last = p->offset + size;
2753
2754   for (; p && p->offset < last; p = p->next)
2755     {
2756       varinfo_t q;
2757       struct constraint_expr templhs = lhs;
2758       struct constraint_expr temprhs = rhs;
2759       unsigned HOST_WIDE_INT fieldoffset;
2760
2761
2762       if (templhs.type == SCALAR)
2763         templhs.var = p->id;      
2764       else
2765         templhs.offset = p->offset;
2766       
2767       q = get_varinfo (temprhs.var);
2768       fieldoffset = p->offset - pstart;      
2769       temprhs.offset += fieldoffset;
2770       process_constraint (new_constraint (templhs, temprhs));
2771     }
2772 }
2773
2774 /* Handle the structure copy case where we have a structure copy
2775    between a aggregate on the RHS and a dereference of a pointer on
2776    the LHS that is of SIZE (in bits) 
2777
2778    For each field of the rhs variable (rhsfield)
2779        lhs.offset = rhsfield->offset
2780        add the constraint lhs = rhsfield
2781 */
2782
2783 static void
2784 do_lhs_deref_structure_copy (const struct constraint_expr lhs,
2785                              const struct constraint_expr rhs,
2786                              const unsigned HOST_WIDE_INT size)
2787 {
2788   varinfo_t p = get_varinfo (rhs.var);
2789   unsigned HOST_WIDE_INT pstart,last;
2790   pstart = p->offset;
2791   last = p->offset + size;
2792
2793   for (; p && p->offset < last; p = p->next)
2794     {
2795       varinfo_t q;
2796       struct constraint_expr templhs = lhs;
2797       struct constraint_expr temprhs = rhs;
2798       unsigned HOST_WIDE_INT fieldoffset;
2799
2800
2801       if (temprhs.type == SCALAR)
2802         temprhs.var = p->id;      
2803       else
2804         temprhs.offset = p->offset;
2805       
2806       q = get_varinfo (templhs.var);
2807       fieldoffset = p->offset - pstart;      
2808       templhs.offset += fieldoffset;
2809       process_constraint (new_constraint (templhs, temprhs));
2810     }
2811 }
2812
2813 /* Sometimes, frontends like to give us bad type information.  This
2814    function will collapse all the fields from VAR to the end of VAR,
2815    into VAR, so that we treat those fields as a single variable. 
2816    We return the variable they were collapsed into.  */
2817
2818 static unsigned int
2819 collapse_rest_of_var (unsigned int var)
2820 {
2821   varinfo_t currvar = get_varinfo (var);
2822   varinfo_t field;
2823
2824   for (field = currvar->next; field; field = field->next)
2825     {
2826       if (dump_file)
2827         fprintf (dump_file, "Type safety: Collapsing var %s into %s\n", 
2828                  field->name, currvar->name);
2829       
2830       gcc_assert (!field->collapsed_to);
2831       field->collapsed_to = currvar;
2832     }
2833
2834   currvar->next = NULL;
2835   currvar->size = currvar->fullsize - currvar->offset;
2836   
2837   return currvar->id;
2838 }
2839
2840 /* Handle aggregate copies by expanding into copies of the respective
2841    fields of the structures.  */
2842
2843 static void
2844 do_structure_copy (tree lhsop, tree rhsop)
2845 {
2846   struct constraint_expr lhs, rhs, tmp;
2847   VEC (ce_s, heap) *lhsc = NULL, *rhsc = NULL;
2848   varinfo_t p;
2849   unsigned HOST_WIDE_INT lhssize;
2850   unsigned HOST_WIDE_INT rhssize;
2851
2852   get_constraint_for (lhsop, &lhsc);
2853   get_constraint_for (rhsop, &rhsc);
2854   gcc_assert (VEC_length (ce_s, lhsc) == 1);
2855   gcc_assert (VEC_length (ce_s, rhsc) == 1);
2856   lhs = *(VEC_last (ce_s, lhsc));
2857   rhs = *(VEC_last (ce_s, rhsc));
2858   
2859   VEC_free (ce_s, heap, lhsc);
2860   VEC_free (ce_s, heap, rhsc);
2861
2862   /* If we have special var = x, swap it around.  */
2863   if (lhs.var <= integer_id && !(get_varinfo (rhs.var)->is_special_var))
2864     {
2865       tmp = lhs;
2866       lhs = rhs;
2867       rhs = tmp;
2868     }
2869   
2870   /*  This is fairly conservative for the RHS == ADDRESSOF case, in that it's
2871       possible it's something we could handle.  However, most cases falling
2872       into this are dealing with transparent unions, which are slightly
2873       weird. */
2874   if (rhs.type == ADDRESSOF && !(get_varinfo (rhs.var)->is_special_var))
2875     {
2876       rhs.type = ADDRESSOF;
2877       rhs.var = anything_id;
2878     }
2879
2880   /* If the RHS is a special var, or an addressof, set all the LHS fields to
2881      that special var.  */
2882   if (rhs.var <= integer_id)
2883     {
2884       for (p = get_varinfo (lhs.var); p; p = p->next)
2885         {
2886           struct constraint_expr templhs = lhs;
2887           struct constraint_expr temprhs = rhs;
2888
2889           if (templhs.type == SCALAR )
2890             templhs.var = p->id;
2891           else
2892             templhs.offset += p->offset;
2893           process_constraint (new_constraint (templhs, temprhs));
2894         }
2895     }
2896   else
2897     {
2898       tree rhstype = TREE_TYPE (rhsop);
2899       tree lhstype = TREE_TYPE (lhsop);
2900       tree rhstypesize;
2901       tree lhstypesize;
2902
2903       lhstypesize = DECL_P (lhsop) ? DECL_SIZE (lhsop) : TYPE_SIZE (lhstype);
2904       rhstypesize = DECL_P (rhsop) ? DECL_SIZE (rhsop) : TYPE_SIZE (rhstype);
2905
2906       /* If we have a variably sized types on the rhs or lhs, and a deref
2907          constraint, add the constraint, lhsconstraint = &ANYTHING.
2908          This is conservatively correct because either the lhs is an unknown
2909          sized var (if the constraint is SCALAR), or the lhs is a DEREF
2910          constraint, and every variable it can point to must be unknown sized
2911          anyway, so we don't need to worry about fields at all.  */
2912       if ((rhs.type == DEREF && TREE_CODE (rhstypesize) != INTEGER_CST)
2913           || (lhs.type == DEREF && TREE_CODE (lhstypesize) != INTEGER_CST))
2914         {
2915           rhs.var = anything_id;
2916           rhs.type = ADDRESSOF;
2917           rhs.offset = 0;
2918           process_constraint (new_constraint (lhs, rhs));
2919           return;
2920         }
2921
2922       /* The size only really matters insofar as we don't set more or less of
2923          the variable.  If we hit an unknown size var, the size should be the
2924          whole darn thing.  */
2925       if (get_varinfo (rhs.var)->is_unknown_size_var)
2926         rhssize = ~0;
2927       else
2928         rhssize = TREE_INT_CST_LOW (rhstypesize);
2929
2930       if (get_varinfo (lhs.var)->is_unknown_size_var)
2931         lhssize = ~0;
2932       else
2933         lhssize = TREE_INT_CST_LOW (lhstypesize);
2934
2935   
2936       if (rhs.type == SCALAR && lhs.type == SCALAR)  
2937         {
2938           if (!do_simple_structure_copy (lhs, rhs, MIN (lhssize, rhssize)))
2939             {         
2940               lhs.var = collapse_rest_of_var (lhs.var);
2941               rhs.var = collapse_rest_of_var (rhs.var);
2942               lhs.offset = 0;
2943               rhs.offset = 0;
2944               lhs.type = SCALAR;
2945               rhs.type = SCALAR;
2946               process_constraint (new_constraint (lhs, rhs));
2947             }
2948         }
2949       else if (lhs.type != DEREF && rhs.type == DEREF)
2950         do_rhs_deref_structure_copy (lhs, rhs, MIN (lhssize, rhssize));
2951       else if (lhs.type == DEREF && rhs.type != DEREF)
2952         do_lhs_deref_structure_copy (lhs, rhs, MIN (lhssize, rhssize));
2953       else
2954         {
2955           tree pointedtotype = lhstype;
2956           tree tmpvar;  
2957
2958           gcc_assert (rhs.type == DEREF && lhs.type == DEREF);
2959           tmpvar = create_tmp_var_raw (pointedtotype, "structcopydereftmp");
2960           do_structure_copy (tmpvar, rhsop);
2961           do_structure_copy (lhsop, tmpvar);
2962         }
2963     }
2964 }
2965
2966 /* Update related alias information kept in AI.  This is used when
2967    building name tags, alias sets and deciding grouping heuristics.
2968    STMT is the statement to process.  This function also updates
2969    ADDRESSABLE_VARS.  */
2970
2971 static void
2972 update_alias_info (tree stmt, struct alias_info *ai)
2973 {
2974   bitmap addr_taken;
2975   use_operand_p use_p;
2976   ssa_op_iter iter;
2977   enum escape_type stmt_escape_type = is_escape_site (stmt, ai);
2978   tree op;
2979
2980   /* Mark all the variables whose address are taken by the statement.  */
2981   addr_taken = addresses_taken (stmt);
2982   if (addr_taken)
2983     {
2984       bitmap_ior_into (addressable_vars, addr_taken);
2985
2986       /* If STMT is an escape point, all the addresses taken by it are
2987          call-clobbered.  */
2988       if (stmt_escape_type != NO_ESCAPE)
2989         {
2990           bitmap_iterator bi;
2991           unsigned i;
2992
2993           EXECUTE_IF_SET_IN_BITMAP (addr_taken, 0, i, bi)
2994             {
2995               tree rvar = referenced_var (i);
2996               if (!unmodifiable_var_p (rvar))
2997                 mark_call_clobbered (rvar, stmt_escape_type);
2998             }
2999         }
3000     }
3001
3002   /* Process each operand use.  If an operand may be aliased, keep
3003      track of how many times it's being used.  For pointers, determine
3004      whether they are dereferenced by the statement, or whether their
3005      value escapes, etc.  */
3006   FOR_EACH_PHI_OR_STMT_USE (use_p, stmt, iter, SSA_OP_USE)
3007     {
3008       tree op, var;
3009       var_ann_t v_ann;
3010       struct ptr_info_def *pi;
3011       bool is_store, is_potential_deref;
3012       unsigned num_uses, num_derefs;
3013
3014       op = USE_FROM_PTR (use_p);
3015
3016       /* If STMT is a PHI node, OP may be an ADDR_EXPR.  If so, add it
3017          to the set of addressable variables.  */
3018       if (TREE_CODE (op) == ADDR_EXPR)
3019         {
3020           gcc_assert (TREE_CODE (stmt) == PHI_NODE);
3021
3022           /* PHI nodes don't have annotations for pinning the set
3023              of addresses taken, so we collect them here.
3024
3025              FIXME, should we allow PHI nodes to have annotations
3026              so that they can be treated like regular statements?
3027              Currently, they are treated as second-class
3028              statements.  */
3029           add_to_addressable_set (TREE_OPERAND (op, 0), &addressable_vars);
3030           continue;
3031         }
3032
3033       /* Ignore constants.  */
3034       if (TREE_CODE (op) != SSA_NAME)
3035         continue;
3036
3037       var = SSA_NAME_VAR (op);
3038       v_ann = var_ann (var);
3039
3040       /* The base variable of an ssa name must be a GIMPLE register, and thus
3041          it cannot be aliased.  */
3042       gcc_assert (!may_be_aliased (var));
3043
3044       /* We are only interested in pointers.  */
3045       if (!POINTER_TYPE_P (TREE_TYPE (op)))
3046         continue;
3047
3048       pi = get_ptr_info (op);
3049
3050       /* Add OP to AI->PROCESSED_PTRS, if it's not there already.  */
3051       if (!TEST_BIT (ai->ssa_names_visited, SSA_NAME_VERSION (op)))
3052         {
3053           SET_BIT (ai->ssa_names_visited, SSA_NAME_VERSION (op));
3054           VEC_safe_push (tree, heap, ai->processed_ptrs, op);
3055         }
3056
3057       /* If STMT is a PHI node, then it will not have pointer
3058          dereferences and it will not be an escape point.  */
3059       if (TREE_CODE (stmt) == PHI_NODE)
3060         continue;
3061
3062       /* Determine whether OP is a dereferenced pointer, and if STMT
3063          is an escape point, whether OP escapes.  */
3064       count_uses_and_derefs (op, stmt, &num_uses, &num_derefs, &is_store);
3065
3066       /* Handle a corner case involving address expressions of the
3067          form '&PTR->FLD'.  The problem with these expressions is that
3068          they do not represent a dereference of PTR.  However, if some
3069          other transformation propagates them into an INDIRECT_REF
3070          expression, we end up with '*(&PTR->FLD)' which is folded
3071          into 'PTR->FLD'.
3072
3073          So, if the original code had no other dereferences of PTR,
3074          the aliaser will not create memory tags for it, and when
3075          &PTR->FLD gets propagated to INDIRECT_REF expressions, the
3076          memory operations will receive no V_MAY_DEF/VUSE operands.
3077
3078          One solution would be to have count_uses_and_derefs consider
3079          &PTR->FLD a dereference of PTR.  But that is wrong, since it
3080          is not really a dereference but an offset calculation.
3081
3082          What we do here is to recognize these special ADDR_EXPR
3083          nodes.  Since these expressions are never GIMPLE values (they
3084          are not GIMPLE invariants), they can only appear on the RHS
3085          of an assignment and their base address is always an
3086          INDIRECT_REF expression.  */
3087       is_potential_deref = false;
3088       if (TREE_CODE (stmt) == MODIFY_EXPR
3089           && TREE_CODE (TREE_OPERAND (stmt, 1)) == ADDR_EXPR
3090           && !is_gimple_val (TREE_OPERAND (stmt, 1)))
3091         {
3092           /* If the RHS if of the form &PTR->FLD and PTR == OP, then
3093              this represents a potential dereference of PTR.  */
3094           tree rhs = TREE_OPERAND (stmt, 1);
3095           tree base = get_base_address (TREE_OPERAND (rhs, 0));
3096           if (TREE_CODE (base) == INDIRECT_REF
3097               && TREE_OPERAND (base, 0) == op)
3098             is_potential_deref = true;
3099         }
3100
3101       if (num_derefs > 0 || is_potential_deref)
3102         {
3103           /* Mark OP as dereferenced.  In a subsequent pass,
3104              dereferenced pointers that point to a set of
3105              variables will be assigned a name tag to alias
3106              all the variables OP points to.  */
3107           pi->is_dereferenced = 1;
3108
3109           /* Keep track of how many time we've dereferenced each
3110              pointer.  */
3111           NUM_REFERENCES_INC (v_ann);
3112
3113           /* If this is a store operation, mark OP as being
3114              dereferenced to store, otherwise mark it as being
3115              dereferenced to load.  */
3116           if (is_store)
3117             bitmap_set_bit (ai->dereferenced_ptrs_store, DECL_UID (var));
3118           else
3119             bitmap_set_bit (ai->dereferenced_ptrs_load, DECL_UID (var));
3120         }
3121
3122       if (stmt_escape_type != NO_ESCAPE && num_derefs < num_uses)
3123         {
3124           /* If STMT is an escape point and STMT contains at
3125              least one direct use of OP, then the value of OP
3126              escapes and so the pointed-to variables need to
3127              be marked call-clobbered.  */
3128           pi->value_escapes_p = 1;
3129           pi->escape_mask |= stmt_escape_type;
3130
3131           /* If the statement makes a function call, assume
3132              that pointer OP will be dereferenced in a store
3133              operation inside the called function.  */
3134           if (get_call_expr_in (stmt))
3135             {
3136               bitmap_set_bit (ai->dereferenced_ptrs_store, DECL_UID (var));
3137               pi->is_dereferenced = 1;
3138             }
3139         }
3140     }
3141
3142   if (TREE_CODE (stmt) == PHI_NODE)
3143     return;
3144
3145   /* Update reference counter for definitions to any
3146      potentially aliased variable.  This is used in the alias
3147      grouping heuristics.  */
3148   FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
3149     {
3150       tree var = SSA_NAME_VAR (op);
3151       var_ann_t ann = var_ann (var);
3152       bitmap_set_bit (ai->written_vars, DECL_UID (var));
3153       if (may_be_aliased (var))
3154         NUM_REFERENCES_INC (ann);
3155       
3156     }
3157   
3158   /* Mark variables in V_MAY_DEF operands as being written to.  */
3159   FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_VIRTUAL_DEFS)
3160     {
3161       tree var = DECL_P (op) ? op : SSA_NAME_VAR (op);
3162       bitmap_set_bit (ai->written_vars, DECL_UID (var));
3163     }
3164 }
3165
3166
3167 /* Handle pointer arithmetic EXPR when creating aliasing constraints.
3168    Expressions of the type PTR + CST can be handled in two ways:
3169
3170    1- If the constraint for PTR is ADDRESSOF for a non-structure
3171       variable, then we can use it directly because adding or
3172       subtracting a constant may not alter the original ADDRESSOF
3173       constraint (i.e., pointer arithmetic may not legally go outside
3174       an object's boundaries).
3175
3176    2- If the constraint for PTR is ADDRESSOF for a structure variable,
3177       then if CST is a compile-time constant that can be used as an
3178       offset, we can determine which sub-variable will be pointed-to
3179       by the expression.
3180
3181    Return true if the expression is handled.  For any other kind of
3182    expression, return false so that each operand can be added as a
3183    separate constraint by the caller.  */
3184
3185 static bool
3186 handle_ptr_arith (VEC (ce_s, heap) *lhsc, tree expr)
3187 {
3188   tree op0, op1;
3189   struct constraint_expr *c, *c2;
3190   unsigned int i = 0;
3191   unsigned int j = 0;
3192   VEC (ce_s, heap) *temp = NULL;
3193   unsigned int rhsoffset = 0;
3194
3195   if (TREE_CODE (expr) != PLUS_EXPR
3196       && TREE_CODE (expr) != MINUS_EXPR)
3197     return false;
3198
3199   op0 = TREE_OPERAND (expr, 0);
3200   op1 = TREE_OPERAND (expr, 1);
3201
3202   get_constraint_for (op0, &temp);
3203   if (POINTER_TYPE_P (TREE_TYPE (op0))
3204       && TREE_CODE (op1) == INTEGER_CST
3205       && TREE_CODE (expr) == PLUS_EXPR)
3206     {
3207       rhsoffset = TREE_INT_CST_LOW (op1) * BITS_PER_UNIT;
3208     }
3209   
3210
3211   for (i = 0; VEC_iterate (ce_s, lhsc, i, c); i++)
3212     for (j = 0; VEC_iterate (ce_s, temp, j, c2); j++)
3213       {
3214         if (c2->type == ADDRESSOF && rhsoffset != 0)
3215           {
3216             varinfo_t temp = get_varinfo (c2->var);
3217
3218             /* An access one after the end of an array is valid,
3219                so simply punt on accesses we cannot resolve.  */
3220             temp = first_vi_for_offset (temp, rhsoffset);
3221             if (temp == NULL)
3222               continue;
3223             c2->var = temp->id;
3224             c2->offset = 0;
3225           }
3226         else
3227           c2->offset = rhsoffset;
3228         process_constraint (new_constraint (*c, *c2));
3229       }
3230
3231   VEC_free (ce_s, heap, temp);
3232
3233   return true;
3234 }
3235
3236
3237 /* Walk statement T setting up aliasing constraints according to the
3238    references found in T.  This function is the main part of the
3239    constraint builder.  AI points to auxiliary alias information used
3240    when building alias sets and computing alias grouping heuristics.  */
3241
3242 static void
3243 find_func_aliases (tree origt)
3244 {
3245   tree t = origt;
3246   VEC(ce_s, heap) *lhsc = NULL;
3247   VEC(ce_s, heap) *rhsc = NULL;
3248   struct constraint_expr *c;
3249
3250   if (TREE_CODE (t) == RETURN_EXPR && TREE_OPERAND (t, 0))
3251     t = TREE_OPERAND (t, 0);
3252
3253   /* Now build constraints expressions.  */
3254   if (TREE_CODE (t) == PHI_NODE)
3255     {
3256       gcc_assert (!AGGREGATE_TYPE_P (TREE_TYPE (PHI_RESULT (t))));
3257
3258       /* Only care about pointers and structures containing
3259          pointers.  */
3260       if (POINTER_TYPE_P (TREE_TYPE (PHI_RESULT (t)))
3261           || TREE_CODE (TREE_TYPE (PHI_RESULT (t))) == COMPLEX_TYPE)
3262         {
3263           int i;
3264           unsigned int j;
3265           
3266           /* For a phi node, assign all the arguments to
3267              the result.  */
3268           get_constraint_for (PHI_RESULT (t), &lhsc);
3269           for (i = 0; i < PHI_NUM_ARGS (t); i++)
3270             { 
3271               tree rhstype;
3272               tree strippedrhs = PHI_ARG_DEF (t, i);
3273
3274               STRIP_NOPS (strippedrhs);
3275               rhstype = TREE_TYPE (strippedrhs);
3276               get_constraint_for (PHI_ARG_DEF (t, i), &rhsc);
3277
3278               for (j = 0; VEC_iterate (ce_s, lhsc, j, c); j++)
3279                 {
3280                   struct constraint_expr *c2;
3281                   while (VEC_length (ce_s, rhsc) > 0)
3282                     {
3283                       c2 = VEC_last (ce_s, rhsc);
3284                       process_constraint (new_constraint (*c, *c2));
3285                       VEC_pop (ce_s, rhsc);
3286                     }
3287                 }
3288             } 
3289         }
3290     }
3291   /* In IPA mode, we need to generate constraints to pass call
3292      arguments through their calls.   There are two case, either a
3293      modify_expr when we are returning a value, or just a plain
3294      call_expr when we are not.   */
3295   else if (in_ipa_mode
3296            && ((TREE_CODE (t) == MODIFY_EXPR 
3297                 && TREE_CODE (TREE_OPERAND (t, 1)) == CALL_EXPR
3298                && !(call_expr_flags (TREE_OPERAND (t, 1)) 
3299                     & (ECF_MALLOC | ECF_MAY_BE_ALLOCA)))
3300                || (TREE_CODE (t) == CALL_EXPR 
3301                    && !(call_expr_flags (t) 
3302                         & (ECF_MALLOC | ECF_MAY_BE_ALLOCA)))))
3303     {
3304       tree lhsop;
3305       tree rhsop;
3306       unsigned int varid;
3307       tree arglist;
3308       varinfo_t fi;
3309       int i = 1;
3310       tree decl;
3311       if (TREE_CODE (t) == MODIFY_EXPR)
3312         {
3313           lhsop = TREE_OPERAND (t, 0);
3314           rhsop = TREE_OPERAND (t, 1);
3315         }
3316       else
3317         {
3318           lhsop = NULL;
3319           rhsop = t;
3320         }
3321       decl = get_callee_fndecl (rhsop);
3322
3323       /* If we can directly resolve the function being called, do so.
3324          Otherwise, it must be some sort of indirect expression that
3325          we should still be able to handle.  */
3326       if (decl)
3327         {
3328           varid = get_id_for_tree (decl);
3329         }
3330       else
3331         {
3332           decl = TREE_OPERAND (rhsop, 0);
3333           varid = get_id_for_tree (decl);
3334         }
3335
3336       /* Assign all the passed arguments to the appropriate incoming
3337          parameters of the function.  */
3338       fi = get_varinfo (varid);
3339       arglist = TREE_OPERAND (rhsop, 1);
3340         
3341       for (;arglist; arglist = TREE_CHAIN (arglist))
3342         {
3343           tree arg = TREE_VALUE (arglist);
3344           struct constraint_expr lhs ;
3345           struct constraint_expr *rhsp;
3346
3347           get_constraint_for (arg, &rhsc);
3348           if (TREE_CODE (decl) != FUNCTION_DECL)
3349             {
3350               lhs.type = DEREF;
3351               lhs.var = fi->id;
3352               lhs.offset = i;
3353             }
3354           else
3355             {
3356               lhs.type = SCALAR;
3357               lhs.var = first_vi_for_offset (fi, i)->id;
3358               lhs.offset = 0;
3359             }
3360           while (VEC_length (ce_s, rhsc) != 0)
3361             {
3362               rhsp = VEC_last (ce_s, rhsc);
3363               process_constraint (new_constraint (lhs, *rhsp));
3364               VEC_pop (ce_s, rhsc);
3365             }
3366           i++;
3367         }
3368       /* If we are returning a value, assign it to the result.  */
3369       if (lhsop)
3370         {
3371           struct constraint_expr rhs;
3372           struct constraint_expr *lhsp;
3373           unsigned int j = 0;
3374           
3375           get_constraint_for (lhsop, &lhsc);
3376           if (TREE_CODE (decl) != FUNCTION_DECL)
3377             {
3378               rhs.type = DEREF;
3379               rhs.var = fi->id;
3380               rhs.offset = i;
3381             }
3382           else
3383             {
3384               rhs.type = SCALAR;
3385               rhs.var = first_vi_for_offset (fi, i)->id;
3386               rhs.offset = 0;
3387             }
3388           for (j = 0; VEC_iterate (ce_s, lhsc, j, lhsp); j++)
3389             process_constraint (new_constraint (*lhsp, rhs));
3390         }      
3391     }
3392   /* Otherwise, just a regular assignment statement.  */
3393   else if (TREE_CODE (t) == MODIFY_EXPR)
3394     {
3395       tree lhsop = TREE_OPERAND (t, 0);
3396       tree rhsop = TREE_OPERAND (t, 1);
3397       int i;
3398
3399       if ((AGGREGATE_TYPE_P (TREE_TYPE (lhsop)) 
3400            || TREE_CODE (TREE_TYPE (lhsop)) == COMPLEX_TYPE)
3401           && (AGGREGATE_TYPE_P (TREE_TYPE (rhsop))
3402               || TREE_CODE (TREE_TYPE (lhsop)) == COMPLEX_TYPE))
3403         {
3404           do_structure_copy (lhsop, rhsop);
3405         }
3406       else
3407         {
3408           /* Only care about operations with pointers, structures
3409              containing pointers, dereferences, and call expressions.  */
3410           if (POINTER_TYPE_P (TREE_TYPE (lhsop))
3411               || AGGREGATE_TYPE_P (TREE_TYPE (lhsop))
3412               || TREE_CODE (TREE_TYPE (lhsop)) == COMPLEX_TYPE
3413               || TREE_CODE (rhsop) == CALL_EXPR)
3414             {
3415               get_constraint_for (lhsop, &lhsc);
3416               switch (TREE_CODE_CLASS (TREE_CODE (rhsop)))
3417                 {
3418                   /* RHS that consist of unary operations,
3419                      exceptional types, or bare decls/constants, get
3420                      handled directly by get_constraint_for.  */ 
3421                   case tcc_reference:
3422                   case tcc_declaration:
3423                   case tcc_constant:
3424                   case tcc_exceptional:
3425                   case tcc_expression:
3426                   case tcc_unary:
3427                       {
3428                         unsigned int j;
3429
3430                         get_constraint_for (rhsop, &rhsc);
3431                         for (j = 0; VEC_iterate (ce_s, lhsc, j, c); j++)
3432                           {
3433                             struct constraint_expr *c2;
3434                             unsigned int k;
3435                             
3436                             for (k = 0; VEC_iterate (ce_s, rhsc, k, c2); k++)
3437                               process_constraint (new_constraint (*c, *c2));
3438                           }
3439
3440                       }
3441                     break;
3442
3443                   case tcc_binary:
3444                       {
3445                         /* For pointer arithmetic of the form
3446                            PTR + CST, we can simply use PTR's
3447                            constraint because pointer arithmetic is
3448                            not allowed to go out of bounds.  */
3449                         if (handle_ptr_arith (lhsc, rhsop))
3450                           break;
3451                       }
3452                     /* FALLTHRU  */
3453
3454                   /* Otherwise, walk each operand.  Notice that we
3455                      can't use the operand interface because we need
3456                      to process expressions other than simple operands
3457                      (e.g. INDIRECT_REF, ADDR_EXPR, CALL_EXPR).  */
3458                   default:
3459                     for (i = 0; i < TREE_CODE_LENGTH (TREE_CODE (rhsop)); i++)
3460                       {
3461                         tree op = TREE_OPERAND (rhsop, i);
3462                         unsigned int j;
3463
3464                         gcc_assert (VEC_length (ce_s, rhsc) == 0);
3465                         get_constraint_for (op, &rhsc);
3466                         for (j = 0; VEC_iterate (ce_s, lhsc, j, c); j++)
3467                           {
3468                             struct constraint_expr *c2;
3469                             while (VEC_length (ce_s, rhsc) > 0)
3470                               {
3471                                 c2 = VEC_last (ce_s, rhsc);
3472                                 process_constraint (new_constraint (*c, *c2));
3473                                 VEC_pop (ce_s, rhsc);
3474                               }
3475                           }
3476                       }
3477                 }      
3478             }
3479         }
3480     }
3481
3482   /* After promoting variables and computing aliasing we will
3483      need to re-scan most statements.  FIXME: Try to minimize the
3484      number of statements re-scanned.  It's not really necessary to
3485      re-scan *all* statements.  */  
3486   mark_stmt_modified (origt);
3487   VEC_free (ce_s, heap, rhsc);
3488   VEC_free (ce_s, heap, lhsc);
3489 }
3490
3491
3492 /* Find the first varinfo in the same variable as START that overlaps with
3493    OFFSET.
3494    Effectively, walk the chain of fields for the variable START to find the
3495    first field that overlaps with OFFSET.
3496    Return NULL if we can't find one.  */
3497
3498 static varinfo_t 
3499 first_vi_for_offset (varinfo_t start, unsigned HOST_WIDE_INT offset)
3500 {
3501   varinfo_t curr = start;
3502   while (curr)
3503     {
3504       /* We may not find a variable in the field list with the actual
3505          offset when when we have glommed a structure to a variable.
3506          In that case, however, offset should still be within the size
3507          of the variable. */
3508       if (offset >= curr->offset && offset < (curr->offset +  curr->size))
3509         return curr;
3510       curr = curr->next;
3511     }
3512   return NULL;
3513 }
3514
3515
3516 /* Insert the varinfo FIELD into the field list for BASE, at the front
3517    of the list.  */
3518
3519 static void
3520 insert_into_field_list (varinfo_t base, varinfo_t field)
3521 {
3522   varinfo_t prev = base;
3523   varinfo_t curr = base->next;
3524   
3525   field->next = curr;
3526   prev->next = field;
3527 }
3528
3529 /* Insert the varinfo FIELD into the field list for BASE, ordered by
3530    offset.  */
3531
3532 static void
3533 insert_into_field_list_sorted (varinfo_t base, varinfo_t field)
3534 {
3535   varinfo_t prev = base;
3536   varinfo_t curr = base->next;
3537   
3538   if (curr == NULL)
3539     {
3540       prev->next = field;
3541       field->next = NULL;
3542     }
3543   else
3544     {
3545       while (curr)
3546         {
3547           if (field->offset <= curr->offset)
3548             break;
3549           prev = curr;
3550           curr = curr->next;
3551         }
3552       field->next = prev->next;
3553       prev->next = field;
3554     }
3555 }
3556
3557 /* qsort comparison function for two fieldoff's PA and PB */
3558
3559 static int 
3560 fieldoff_compare (const void *pa, const void *pb)
3561 {
3562   const fieldoff_s *foa = (const fieldoff_s *)pa;
3563   const fieldoff_s *fob = (const fieldoff_s *)pb;
3564   HOST_WIDE_INT foasize, fobsize;
3565   
3566   if (foa->offset != fob->offset)
3567     return foa->offset - fob->offset;
3568
3569   foasize = TREE_INT_CST_LOW (foa->size);
3570   fobsize = TREE_INT_CST_LOW (fob->size);
3571   return foasize - fobsize;
3572 }
3573
3574 /* Sort a fieldstack according to the field offset and sizes.  */
3575 void sort_fieldstack (VEC(fieldoff_s,heap) *fieldstack)
3576 {
3577   qsort (VEC_address (fieldoff_s, fieldstack), 
3578          VEC_length (fieldoff_s, fieldstack), 
3579          sizeof (fieldoff_s),
3580          fieldoff_compare);
3581 }
3582
3583 /* Given a TYPE, and a vector of field offsets FIELDSTACK, push all the fields
3584    of TYPE onto fieldstack, recording their offsets along the way.
3585    OFFSET is used to keep track of the offset in this entire structure, rather
3586    than just the immediately containing structure.  Returns the number
3587    of fields pushed.
3588    HAS_UNION is set to true if we find a union type as a field of
3589    TYPE.  */
3590
3591 int
3592 push_fields_onto_fieldstack (tree type, VEC(fieldoff_s,heap) **fieldstack, 
3593                              HOST_WIDE_INT offset, bool *has_union)
3594 {
3595   tree field;
3596   int count = 0;
3597   
3598   if (TREE_CODE (type) == COMPLEX_TYPE)
3599     {
3600       fieldoff_s *real_part, *img_part;
3601       real_part = VEC_safe_push (fieldoff_s, heap, *fieldstack, NULL);
3602       real_part->type = TREE_TYPE (type);
3603       real_part->size = TYPE_SIZE (TREE_TYPE (type));
3604       real_part->offset = offset;
3605       real_part->decl = NULL_TREE;
3606       
3607       img_part = VEC_safe_push (fieldoff_s, heap, *fieldstack, NULL);
3608       img_part->type = TREE_TYPE (type);
3609       img_part->size = TYPE_SIZE (TREE_TYPE (type));
3610       img_part->offset = offset + TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (type)));
3611       img_part->decl = NULL_TREE;
3612       
3613       return 2;
3614     }
3615
3616   if (TREE_CODE (type) == ARRAY_TYPE)
3617     {
3618       tree sz = TYPE_SIZE (type);
3619       tree elsz = TYPE_SIZE (TREE_TYPE (type));
3620       HOST_WIDE_INT nr;
3621       int i;
3622
3623       if (! sz
3624           || ! host_integerp (sz, 1)
3625           || TREE_INT_CST_LOW (sz) == 0
3626           || ! elsz
3627           || ! host_integerp (elsz, 1)
3628           || TREE_INT_CST_LOW (elsz) == 0)
3629         return 0;
3630
3631       nr = TREE_INT_CST_LOW (sz) / TREE_INT_CST_LOW (elsz);
3632       if (nr > SALIAS_MAX_ARRAY_ELEMENTS)
3633         return 0;
3634
3635       for (i = 0; i < nr; ++i)
3636         {
3637           bool push = false;
3638           int pushed = 0;
3639         
3640           if (has_union 
3641               && (TREE_CODE (TREE_TYPE (type)) == QUAL_UNION_TYPE
3642                   || TREE_CODE (TREE_TYPE (type)) == UNION_TYPE))
3643             *has_union = true;
3644         
3645           if (!AGGREGATE_TYPE_P (TREE_TYPE (type))) /* var_can_have_subvars */
3646             push = true;
3647           else if (!(pushed = push_fields_onto_fieldstack
3648                      (TREE_TYPE (type), fieldstack,
3649                       offset + i * TREE_INT_CST_LOW (elsz), has_union)))
3650             /* Empty structures may have actual size, like in C++. So
3651                see if we didn't push any subfields and the size is
3652                nonzero, push the field onto the stack */
3653             push = true;
3654
3655           if (push)
3656             {
3657               fieldoff_s *pair;
3658
3659               pair = VEC_safe_push (fieldoff_s, heap, *fieldstack, NULL);
3660               pair->type = TREE_TYPE (type);
3661               pair->size = elsz;
3662               pair->decl = NULL_TREE;
3663               pair->offset = offset + i * TREE_INT_CST_LOW (elsz);
3664               count++;
3665             }
3666           else
3667             count += pushed;
3668         }
3669
3670       return count;
3671     }
3672
3673   for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3674     if (TREE_CODE (field) == FIELD_DECL)
3675       {
3676         bool push = false;
3677         int pushed = 0;
3678         
3679         if (has_union 
3680             && (TREE_CODE (TREE_TYPE (field)) == QUAL_UNION_TYPE
3681                 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
3682           *has_union = true;
3683         
3684         if (!var_can_have_subvars (field))
3685           push = true;
3686         else if (!(pushed = push_fields_onto_fieldstack
3687                    (TREE_TYPE (field), fieldstack,
3688                     offset + bitpos_of_field (field), has_union))
3689                  && DECL_SIZE (field)
3690                  && !integer_zerop (DECL_SIZE (field)))
3691           /* Empty structures may have actual size, like in C++. So
3692              see if we didn't push any subfields and the size is
3693              nonzero, push the field onto the stack */
3694           push = true;
3695         
3696         if (push)
3697           {
3698             fieldoff_s *pair;
3699
3700             pair = VEC_safe_push (fieldoff_s, heap, *fieldstack, NULL);
3701             pair->type = TREE_TYPE (field);
3702             pair->size = DECL_SIZE (field);
3703             pair->decl = field;
3704             pair->offset = offset + bitpos_of_field (field);
3705             count++;
3706           }
3707         else
3708           count += pushed;
3709       }
3710
3711   return count;
3712 }
3713
3714 static void
3715 make_constraint_to_anything (varinfo_t vi)
3716 {
3717   struct constraint_expr lhs, rhs;
3718   
3719   lhs.var = vi->id;
3720   lhs.offset = 0;
3721   lhs.type = SCALAR;
3722   
3723   rhs.var = anything_id;
3724   rhs.offset =0 ;
3725   rhs.type = ADDRESSOF;
3726   process_constraint (new_constraint (lhs, rhs));
3727 }
3728
3729 /* Count the number of arguments DECL has, and set IS_VARARGS to true
3730    if it is a varargs function.  */
3731
3732 static unsigned int
3733 count_num_arguments (tree decl, bool *is_varargs)
3734 {
3735   unsigned int i = 0;
3736   tree t;
3737
3738   for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); 
3739        t;
3740        t = TREE_CHAIN (t))
3741     {   
3742       if (TREE_VALUE (t) == void_type_node)
3743         break;
3744       i++;
3745     }
3746   
3747   if (!t)
3748     *is_varargs = true;
3749   return i;
3750 }
3751
3752 /* Creation function node for DECL, using NAME, and return the index
3753    of the variable we've created for the function.  */
3754
3755 static unsigned int
3756 create_function_info_for (tree decl, const char *name)
3757 {
3758   unsigned int index = VEC_length (varinfo_t, varmap);
3759   varinfo_t vi;
3760   tree arg; 
3761   unsigned int i;
3762   bool is_varargs = false;
3763
3764   /* Create the variable info.  */
3765
3766   vi = new_var_info (decl, index, name, index);
3767   vi->decl = decl;
3768   vi->offset = 0;
3769   vi->has_union = 0;
3770   vi->size = 1;
3771   vi->fullsize = count_num_arguments (decl, &is_varargs) + 1;
3772   insert_id_for_tree (vi->decl, index);  
3773   VEC_safe_push (varinfo_t, heap, varmap, vi);
3774
3775   stats.total_vars++;
3776
3777   /* If it's varargs, we don't know how many arguments it has, so we
3778      can't do much.
3779   */
3780   if (is_varargs)
3781     {
3782       vi->fullsize = ~0;
3783       vi->size = ~0;
3784       vi->is_unknown_size_var = true;
3785       return index;
3786     }
3787
3788   
3789   arg = DECL_ARGUMENTS (decl);
3790
3791   /* Set up variables for each argument.  */
3792   for (i = 1; i < vi->fullsize; i++)
3793     {      
3794       varinfo_t argvi;
3795       const char *newname;
3796       char *tempname;
3797       unsigned int newindex;
3798       tree argdecl = decl;
3799
3800       if (arg)
3801         argdecl = arg;
3802       
3803       newindex = VEC_length (varinfo_t, varmap);
3804       asprintf (&tempname, "%s.arg%d", name, i-1);
3805       newname = ggc_strdup (tempname);
3806       free (tempname);
3807
3808       argvi = new_var_info (argdecl, newindex,newname, newindex);
3809       argvi->decl = argdecl;
3810       VEC_safe_push (varinfo_t, heap, varmap, argvi);
3811       argvi->offset = i;
3812       argvi->size = 1;
3813       argvi->fullsize = vi->fullsize;
3814       argvi->has_union = false;
3815       insert_into_field_list_sorted (vi, argvi);
3816       stats.total_vars ++;
3817       if (arg)
3818         {
3819           insert_id_for_tree (arg, newindex);
3820           arg = TREE_CHAIN (arg);
3821         }
3822     }
3823
3824   /* Create a variable for the return var.  */
3825   if (DECL_RESULT (decl) != NULL
3826       || !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
3827     {
3828       varinfo_t resultvi;
3829       const char *newname;
3830       char *tempname;
3831       unsigned int newindex;
3832       tree resultdecl = decl;
3833
3834       vi->fullsize ++;
3835
3836       if (DECL_RESULT (decl))
3837         resultdecl = DECL_RESULT (decl);
3838       
3839       newindex = VEC_length (varinfo_t, varmap);
3840       asprintf (&tempname, "%s.result", name);
3841       newname = ggc_strdup (tempname);
3842       free (tempname);
3843
3844       resultvi = new_var_info (resultdecl, newindex, newname, newindex);
3845       resultvi->decl = resultdecl;
3846       VEC_safe_push (varinfo_t, heap, varmap, resultvi);
3847       resultvi->offset = i;
3848       resultvi->size = 1;
3849       resultvi->fullsize = vi->fullsize;
3850       resultvi->has_union = false;
3851       insert_into_field_list_sorted (vi, resultvi);
3852       stats.total_vars ++;
3853       if (DECL_RESULT (decl))
3854         insert_id_for_tree (DECL_RESULT (decl), newindex);
3855     }
3856   return index;
3857 }  
3858
3859
3860 /* Return true if FIELDSTACK contains fields that overlap. 
3861    FIELDSTACK is assumed to be sorted by offset.  */
3862
3863 static bool
3864 check_for_overlaps (VEC (fieldoff_s,heap) *fieldstack)
3865 {
3866   fieldoff_s *fo = NULL;
3867   unsigned int i;
3868   HOST_WIDE_INT lastoffset = -1;
3869
3870   for (i = 0; VEC_iterate (fieldoff_s, fieldstack, i, fo); i++)
3871     {
3872       if (fo->offset == lastoffset)
3873         return true;
3874       lastoffset = fo->offset;
3875     }
3876   return false;
3877 }
3878 /* Create a varinfo structure for NAME and DECL, and add it to VARMAP.
3879    This will also create any varinfo structures necessary for fields
3880    of DECL.  */
3881
3882 static unsigned int
3883 create_variable_info_for (tree decl, const char *name)
3884 {
3885   unsigned int index = VEC_length (varinfo_t, varmap);
3886   varinfo_t vi;
3887   tree decltype = TREE_TYPE (decl);
3888   tree declsize = DECL_P (decl) ? DECL_SIZE (decl) : TYPE_SIZE (decltype);
3889   bool notokay = false;
3890   bool hasunion;
3891   bool is_global = DECL_P (decl) ? is_global_var (decl) : false;
3892   VEC (fieldoff_s,heap) *fieldstack = NULL;
3893   
3894   if (TREE_CODE (decl) == FUNCTION_DECL && in_ipa_mode)
3895     return create_function_info_for (decl, name);
3896
3897   hasunion = TREE_CODE (decltype) == UNION_TYPE
3898              || TREE_CODE (decltype) == QUAL_UNION_TYPE;
3899   if (var_can_have_subvars (decl) && use_field_sensitive && !hasunion)
3900     {
3901       push_fields_onto_fieldstack (decltype, &fieldstack, 0, &hasunion);
3902       if (hasunion)
3903         {
3904           VEC_free (fieldoff_s, heap, fieldstack);
3905           notokay = true;
3906         }
3907     }
3908   
3909
3910   /* If the variable doesn't have subvars, we may end up needing to
3911      sort the field list and create fake variables for all the
3912      fields.  */
3913   vi = new_var_info (decl, index, name, index);
3914   vi->decl = decl;
3915   vi->offset = 0;
3916   vi->has_union = hasunion;
3917   if (!declsize
3918       || TREE_CODE (declsize) != INTEGER_CST
3919       || TREE_CODE (decltype) == UNION_TYPE
3920       || TREE_CODE (decltype) == QUAL_UNION_TYPE)
3921     {
3922       vi->is_unknown_size_var = true;
3923       vi->fullsize = ~0;
3924       vi->size = ~0;
3925     }
3926   else
3927     {
3928       vi->fullsize = TREE_INT_CST_LOW (declsize);
3929       vi->size = vi->fullsize;
3930     }
3931   
3932   insert_id_for_tree (vi->decl, index);  
3933   VEC_safe_push (varinfo_t, heap, varmap, vi);
3934   if (is_global && (!flag_whole_program || !in_ipa_mode))
3935     make_constraint_to_anything (vi);
3936
3937   stats.total_vars++;
3938   if (use_field_sensitive 
3939       && !notokay 
3940       && !vi->is_unknown_size_var 
3941       && var_can_have_subvars (decl)
3942       && VEC_length (fieldoff_s, fieldstack) <= MAX_FIELDS_FOR_FIELD_SENSITIVE)
3943     {
3944       unsigned int newindex = VEC_length (varinfo_t, varmap);
3945       fieldoff_s *fo = NULL;
3946       unsigned int i;
3947
3948       for (i = 0; !notokay && VEC_iterate (fieldoff_s, fieldstack, i, fo); i++)
3949         {
3950           if (! fo->size
3951               || TREE_CODE (fo->size) != INTEGER_CST
3952               || fo->offset < 0)
3953             {
3954               notokay = true;
3955               break;
3956             }
3957         }
3958
3959       /* We can't sort them if we have a field with a variable sized type,
3960          which will make notokay = true.  In that case, we are going to return
3961          without creating varinfos for the fields anyway, so sorting them is a
3962          waste to boot.  */
3963       if (!notokay)
3964         {       
3965           sort_fieldstack (fieldstack);
3966           /* Due to some C++ FE issues, like PR 22488, we might end up
3967              what appear to be overlapping fields even though they,
3968              in reality, do not overlap.  Until the C++ FE is fixed,
3969              we will simply disable field-sensitivity for these cases.  */
3970           notokay = check_for_overlaps (fieldstack);
3971         }
3972       
3973       
3974       if (VEC_length (fieldoff_s, fieldstack) != 0)
3975         fo = VEC_index (fieldoff_s, fieldstack, 0);
3976
3977       if (fo == NULL || notokay)
3978         {
3979           vi->is_unknown_size_var = 1;
3980           vi->fullsize = ~0;
3981           vi->size = ~0;
3982           VEC_free (fieldoff_s, heap, fieldstack);
3983           return index;
3984         }
3985       
3986       vi->size = TREE_INT_CST_LOW (fo->size);
3987       vi->offset = fo->offset;
3988       for (i = VEC_length (fieldoff_s, fieldstack) - 1; 
3989            i >= 1 && VEC_iterate (fieldoff_s, fieldstack, i, fo); 
3990            i--)
3991         {
3992           varinfo_t newvi;
3993           const char *newname = "NULL";
3994           char *tempname;
3995
3996           newindex = VEC_length (varinfo_t, varmap);
3997           if (dump_file)
3998             {
3999               if (fo->decl)
4000                 asprintf (&tempname, "%s.%s",
4001                           vi->name, alias_get_name (fo->decl));
4002               else
4003                 asprintf (&tempname, "%s." HOST_WIDE_INT_PRINT_DEC,
4004                           vi->name, fo->offset);
4005               newname = ggc_strdup (tempname);
4006               free (tempname);
4007             }
4008           newvi = new_var_info (decl, newindex, newname, newindex);
4009           newvi->offset = fo->offset;
4010           newvi->size = TREE_INT_CST_LOW (fo->size);
4011           newvi->fullsize = vi->fullsize;
4012           insert_into_field_list (vi, newvi);
4013           VEC_safe_push (varinfo_t, heap, varmap, newvi);
4014           if (is_global && (!flag_whole_program || !in_ipa_mode))
4015             make_constraint_to_anything (newvi);
4016
4017           stats.total_vars++;
4018         }
4019       VEC_free (fieldoff_s, heap, fieldstack);
4020     }
4021   return index;
4022 }
4023
4024 /* Print out the points-to solution for VAR to FILE.  */
4025
4026 void
4027 dump_solution_for_var (FILE *file, unsigned int var)
4028 {
4029   varinfo_t vi = get_varinfo (var);
4030   unsigned int i;
4031   bitmap_iterator bi; 
4032   
4033   fprintf (file, "%s = { ", vi->name);
4034   EXECUTE_IF_SET_IN_BITMAP (get_varinfo (vi->node)->solution, 0, i, bi)
4035     {
4036       fprintf (file, "%s ", get_varinfo (i)->name);
4037     }
4038   fprintf (file, "}\n");
4039 }
4040
4041 /* Print the points-to solution for VAR to stdout.  */
4042
4043 void
4044 debug_solution_for_var (unsigned int var)
4045 {
4046   dump_solution_for_var (stdout, var);
4047 }
4048
4049
4050 /* Create varinfo structures for all of the variables in the
4051    function for intraprocedural mode.  */
4052
4053 static void
4054 intra_create_variable_infos (void)
4055 {
4056   tree t;
4057
4058   /* For each incoming argument arg, ARG = &ANYTHING or a dummy variable if
4059      flag_argument_noalias > 2. */
4060   for (t = DECL_ARGUMENTS (current_function_decl); t; t = TREE_CHAIN (t))
4061     {
4062       struct constraint_expr lhs;
4063       varinfo_t p;
4064       
4065       lhs.offset = 0;
4066       lhs.type = SCALAR;
4067       lhs.var  = create_variable_info_for (t, alias_get_name (t));
4068
4069       /* With flag_argument_noalias greater than two means that the incoming
4070          argument cannot alias anything except for itself so create a HEAP
4071          variable.  */
4072       if (POINTER_TYPE_P (TREE_TYPE (t))
4073           && flag_argument_noalias > 2)
4074         {
4075           varinfo_t vi;
4076           struct constraint_expr rhs;
4077           tree heapvar = heapvar_lookup (t);
4078           unsigned int id;
4079           if (heapvar == NULL_TREE)
4080             {
4081               heapvar = create_tmp_var_raw (TREE_TYPE (TREE_TYPE (t)), 
4082                                             "PARM_NOALIAS");
4083               DECL_EXTERNAL (heapvar) = 1;
4084               if (referenced_vars)
4085                 add_referenced_var (heapvar);
4086               heapvar_insert (t, heapvar);
4087             }
4088           id = create_variable_info_for (heapvar,
4089                                          alias_get_name (heapvar));
4090           vi = get_varinfo (id);
4091           vi->is_artificial_var = 1;
4092           vi->is_heap_var = 1;
4093           rhs.var = id;
4094           rhs.type = ADDRESSOF;
4095           rhs.offset = 0;
4096           for (p = get_varinfo (lhs.var); p; p = p->next)
4097             {
4098               struct constraint_expr temp = lhs;
4099               temp.var = p->id;
4100               process_constraint (new_constraint (temp, rhs));
4101             }
4102         }
4103       else      
4104         for (p = get_varinfo (lhs.var); p; p = p->next)
4105           make_constraint_to_anything (p);
4106     }   
4107 }
4108
4109 /* Set bits in INTO corresponding to the variable uids in solution set
4110    FROM  */
4111
4112 static void
4113 set_uids_in_ptset (bitmap into, bitmap from)
4114 {
4115   unsigned int i;
4116   bitmap_iterator bi;
4117   subvar_t sv;
4118
4119   EXECUTE_IF_SET_IN_BITMAP (from, 0, i, bi)
4120     {
4121       varinfo_t vi = get_varinfo (i);
4122
4123       /* The only artificial variables that are allowed in a may-alias
4124          set are heap variables.  */
4125       if (vi->is_artificial_var && !vi->is_heap_var)
4126         continue;
4127       
4128       if (vi->has_union && get_subvars_for_var (vi->decl) != NULL)
4129         {
4130           /* Variables containing unions may need to be converted to
4131              their SFT's, because SFT's can have unions and we cannot.  */
4132           for (sv = get_subvars_for_var (vi->decl); sv; sv = sv->next)
4133             bitmap_set_bit (into, DECL_UID (sv->var));
4134         }
4135       else if (TREE_CODE (vi->decl) == VAR_DECL 
4136                || TREE_CODE (vi->decl) == PARM_DECL)
4137         {
4138           if (var_can_have_subvars (vi->decl)
4139                    && get_subvars_for_var (vi->decl))
4140             {
4141               /* If VI->DECL is an aggregate for which we created
4142                  SFTs, add the SFT corresponding to VI->OFFSET.  */
4143               tree sft = get_subvar_at (vi->decl, vi->offset);
4144               if (sft)
4145                 bitmap_set_bit (into, DECL_UID (sft));
4146             }
4147           else
4148             {
4149               /* Otherwise, just add VI->DECL to the alias set.  */
4150               bitmap_set_bit (into, DECL_UID (vi->decl));
4151             }
4152         }
4153     }
4154 }
4155
4156
4157 static bool have_alias_info = false;
4158
4159 /* Given a pointer variable P, fill in its points-to set, or return
4160    false if we can't.  */
4161
4162 bool
4163 find_what_p_points_to (tree p)
4164 {
4165   unsigned int id = 0;
4166   tree lookup_p = p;
4167
4168   if (!have_alias_info)
4169     return false;
4170
4171   /* For parameters, get at the points-to set for the actual parm
4172      decl.  */
4173   if (TREE_CODE (p) == SSA_NAME 
4174       && TREE_CODE (SSA_NAME_VAR (p)) == PARM_DECL 
4175       && default_def (SSA_NAME_VAR (p)) == p)
4176     lookup_p = SSA_NAME_VAR (p);
4177
4178   if (lookup_id_for_tree (lookup_p, &id))
4179     {
4180       varinfo_t vi = get_varinfo (id);
4181       
4182       if (vi->is_artificial_var)
4183         return false;
4184
4185       /* See if this is a field or a structure.  */
4186       if (vi->size != vi->fullsize)
4187         {
4188           /* Nothing currently asks about structure fields directly,
4189              but when they do, we need code here to hand back the
4190              points-to set.  */
4191           if (!var_can_have_subvars (vi->decl)
4192               || get_subvars_for_var (vi->decl) == NULL)
4193             return false;
4194         } 
4195       else
4196         {
4197           struct ptr_info_def *pi = get_ptr_info (p);
4198           unsigned int i;
4199           bitmap_iterator bi;
4200
4201           /* This variable may have been collapsed, let's get the real
4202              variable.  */
4203           vi = get_varinfo (vi->node);
4204           
4205           /* Translate artificial variables into SSA_NAME_PTR_INFO
4206              attributes.  */
4207           EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, i, bi)
4208             {
4209               varinfo_t vi = get_varinfo (i);
4210
4211               if (vi->is_artificial_var)
4212                 {
4213                   /* FIXME.  READONLY should be handled better so that
4214                      flow insensitive aliasing can disregard writable
4215                      aliases.  */
4216                   if (vi->id == nothing_id)
4217                     pi->pt_null = 1;
4218                   else if (vi->id == anything_id)
4219                     pi->pt_anything = 1;
4220                   else if (vi->id == readonly_id)
4221                     pi->pt_anything = 1;
4222                   else if (vi->id == integer_id)
4223                     pi->pt_anything = 1;
4224                   else if (vi->is_heap_var)
4225                     pi->pt_global_mem = 1;
4226                 }
4227             }
4228
4229           if (pi->pt_anything)
4230             return false;
4231
4232           if (!pi->pt_vars)
4233             pi->pt_vars = BITMAP_GGC_ALLOC ();
4234
4235           set_uids_in_ptset (pi->pt_vars, vi->solution);
4236
4237           if (bitmap_empty_p (pi->pt_vars))
4238             pi->pt_vars = NULL;
4239
4240           return true;
4241         }
4242     }
4243
4244   return false;
4245 }
4246
4247
4248
4249 /* Dump points-to information to OUTFILE.  */
4250
4251 void
4252 dump_sa_points_to_info (FILE *outfile)
4253 {
4254   unsigned int i;
4255
4256   fprintf (outfile, "\nPoints-to sets\n\n");
4257
4258   if (dump_flags & TDF_STATS)
4259     {
4260       fprintf (outfile, "Stats:\n");
4261       fprintf (outfile, "Total vars:               %d\n", stats.total_vars);
4262       fprintf (outfile, "Statically unified vars:  %d\n",
4263                stats.unified_vars_static);
4264       fprintf (outfile, "Collapsed vars:           %d\n", stats.collapsed_vars);
4265       fprintf (outfile, "Dynamically unified vars: %d\n",
4266                stats.unified_vars_dynamic);
4267       fprintf (outfile, "Iterations:               %d\n", stats.iterations);
4268       fprintf (outfile, "Number of edges:          %d\n", stats.num_edges);
4269     }
4270
4271   for (i = 0; i < VEC_length (varinfo_t, varmap); i++)
4272     dump_solution_for_var (outfile, i);
4273 }
4274
4275
4276 /* Debug points-to information to stderr.  */
4277
4278 void
4279 debug_sa_points_to_info (void)
4280 {
4281   dump_sa_points_to_info (stderr);
4282 }
4283
4284
4285 /* Initialize the always-existing constraint variables for NULL
4286    ANYTHING, READONLY, and INTEGER */
4287
4288 static void
4289 init_base_vars (void)
4290 {
4291   struct constraint_expr lhs, rhs;
4292
4293   /* Create the NULL variable, used to represent that a variable points
4294      to NULL.  */
4295   nothing_tree = create_tmp_var_raw (void_type_node, "NULL");
4296   var_nothing = new_var_info (nothing_tree, 0, "NULL", 0);
4297   insert_id_for_tree (nothing_tree, 0);
4298   var_nothing->is_artificial_var = 1;
4299   var_nothing->offset = 0;
4300   var_nothing->size = ~0;
4301   var_nothing->fullsize = ~0;
4302   var_nothing->is_special_var = 1;
4303   nothing_id = 0;
4304   VEC_safe_push (varinfo_t, heap, varmap, var_nothing);
4305
4306   /* Create the ANYTHING variable, used to represent that a variable
4307      points to some unknown piece of memory.  */
4308   anything_tree = create_tmp_var_raw (void_type_node, "ANYTHING");
4309   var_anything = new_var_info (anything_tree, 1, "ANYTHING", 1); 
4310   insert_id_for_tree (anything_tree, 1);
4311   var_anything->is_artificial_var = 1;
4312   var_anything->size = ~0;
4313   var_anything->offset = 0;
4314   var_anything->next = NULL;
4315   var_anything->fullsize = ~0;
4316   var_anything->is_special_var = 1;
4317   anything_id = 1;
4318
4319   /* Anything points to anything.  This makes deref constraints just
4320      work in the presence of linked list and other p = *p type loops, 
4321      by saying that *ANYTHING = ANYTHING. */
4322   VEC_safe_push (varinfo_t, heap, varmap, var_anything);
4323   lhs.type = SCALAR;
4324   lhs.var = anything_id;
4325   lhs.offset = 0;
4326   rhs.type = ADDRESSOF;
4327   rhs.var = anything_id;
4328   rhs.offset = 0;
4329   var_anything->address_taken = true;
4330
4331   /* This specifically does not use process_constraint because
4332      process_constraint ignores all anything = anything constraints, since all
4333      but this one are redundant.  */
4334   VEC_safe_push (constraint_t, heap, constraints, new_constraint (lhs, rhs));
4335   
4336   /* Create the READONLY variable, used to represent that a variable
4337      points to readonly memory.  */
4338   readonly_tree = create_tmp_var_raw (void_type_node, "READONLY");
4339   var_readonly = new_var_info (readonly_tree, 2, "READONLY", 2);
4340   var_readonly->is_artificial_var = 1;
4341   var_readonly->offset = 0;
4342   var_readonly->size = ~0;
4343   var_readonly->fullsize = ~0;
4344   var_readonly->next = NULL;
4345   var_readonly->is_special_var = 1;
4346   insert_id_for_tree (readonly_tree, 2);
4347   readonly_id = 2;
4348   VEC_safe_push (varinfo_t, heap, varmap, var_readonly);
4349
4350   /* readonly memory points to anything, in order to make deref
4351      easier.  In reality, it points to anything the particular
4352      readonly variable can point to, but we don't track this
4353      separately. */
4354   lhs.type = SCALAR;
4355   lhs.var = readonly_id;
4356   lhs.offset = 0;
4357   rhs.type = ADDRESSOF;
4358   rhs.var = anything_id;
4359   rhs.offset = 0;
4360   
4361   process_constraint (new_constraint (lhs, rhs));
4362   
4363   /* Create the INTEGER variable, used to represent that a variable points
4364      to an INTEGER.  */
4365   integer_tree = create_tmp_var_raw (void_type_node, "INTEGER");
4366   var_integer = new_var_info (integer_tree, 3, "INTEGER", 3);
4367   insert_id_for_tree (integer_tree, 3);
4368   var_integer->is_artificial_var = 1;
4369   var_integer->size = ~0;
4370   var_integer->fullsize = ~0;
4371   var_integer->offset = 0;
4372   var_integer->next = NULL;
4373   var_integer->is_special_var = 1;
4374   integer_id = 3;
4375   VEC_safe_push (varinfo_t, heap, varmap, var_integer);
4376
4377   /* *INTEGER = ANYTHING, because we don't know where a dereference of a random
4378      integer will point to.  */
4379   lhs.type = SCALAR;
4380   lhs.var = integer_id;
4381   lhs.offset = 0;
4382   rhs.type = ADDRESSOF;
4383   rhs.var = anything_id;
4384   rhs.offset = 0;
4385   process_constraint (new_constraint (lhs, rhs));
4386 }  
4387
4388 /* Return true if we actually need to solve the constraint graph in order to
4389    get our points-to sets.  This is false when, for example, no addresses are
4390    taken other than special vars, or all points-to sets with members already
4391    contain the anything variable and there are no predecessors for other
4392    sets.  */
4393
4394 static bool
4395 need_to_solve (void)
4396 {
4397   int i;
4398   varinfo_t v;
4399   bool found_address_taken = false;
4400   bool found_non_anything = false;
4401
4402   for (i = 0; VEC_iterate (varinfo_t, varmap, i, v); i++)
4403     {
4404       if (v->is_special_var)
4405         continue;
4406
4407       if (v->address_taken)
4408         found_address_taken = true;
4409
4410       if (v->solution 
4411           && !bitmap_empty_p (v->solution) 
4412           && !bitmap_bit_p (v->solution, anything_id))
4413         found_non_anything = true;
4414       else if (bitmap_empty_p (v->solution)
4415                && (VEC_length (constraint_edge_t, graph->preds[v->id]) != 0
4416                  || (graph->zero_weight_preds[v->id] && !bitmap_empty_p (graph->zero_weight_preds[v->id]))))
4417         found_non_anything = true;
4418
4419       if (found_address_taken && found_non_anything)
4420         return true;
4421     }
4422
4423   return false;
4424 }
4425
4426 /* Initialize things necessary to perform PTA */
4427
4428 static void
4429 init_alias_vars (void)
4430 {
4431   bitmap_obstack_initialize (&ptabitmap_obstack);
4432   bitmap_obstack_initialize (&predbitmap_obstack);
4433
4434   constraint_pool = create_alloc_pool ("Constraint pool", 
4435                                        sizeof (struct constraint), 30);
4436   variable_info_pool = create_alloc_pool ("Variable info pool",
4437                                           sizeof (struct variable_info), 30);
4438   constraint_edge_pool = create_alloc_pool ("Constraint edges",
4439                                             sizeof (struct constraint_edge), 30);
4440   
4441   constraints = VEC_alloc (constraint_t, heap, 8);
4442   varmap = VEC_alloc (varinfo_t, heap, 8);
4443   id_for_tree = htab_create (10, tree_id_hash, tree_id_eq, free);
4444   memset (&stats, 0, sizeof (stats));
4445
4446   init_base_vars ();
4447 }
4448
4449
4450 /* Create points-to sets for the current function.  See the comments
4451    at the start of the file for an algorithmic overview.  */
4452
4453 void
4454 compute_points_to_sets (struct alias_info *ai)
4455 {
4456   basic_block bb;
4457
4458   timevar_push (TV_TREE_PTA);
4459
4460   init_alias_vars ();
4461
4462   intra_create_variable_infos ();
4463
4464   /* Now walk all statements and derive aliases.  */
4465   FOR_EACH_BB (bb)
4466     {
4467       block_stmt_iterator bsi; 
4468       tree phi;
4469
4470       for (phi = phi_nodes (bb); phi; phi = TREE_CHAIN (phi))
4471         {
4472           if (is_gimple_reg (PHI_RESULT (phi)))
4473             {
4474               find_func_aliases (phi);
4475               /* Update various related attributes like escaped
4476                  addresses, pointer dereferences for loads and stores.
4477                  This is used when creating name tags and alias
4478                  sets.  */
4479               update_alias_info (phi, ai);
4480             }
4481         }
4482
4483       for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
4484         {
4485           tree stmt = bsi_stmt (bsi);
4486           find_func_aliases (stmt);
4487               /* Update various related attributes like escaped
4488                  addresses, pointer dereferences for loads and stores.
4489                  This is used when creating name tags and alias
4490                  sets.  */
4491           update_alias_info (stmt, ai);
4492         }
4493     }
4494
4495   build_constraint_graph ();
4496
4497   if (dump_file)
4498     {
4499       fprintf (dump_file, "Points-to analysis\n\nConstraints:\n\n");
4500       dump_constraints (dump_file);
4501     }
4502   
4503   if (1 || need_to_solve ())
4504     {
4505       if (dump_file)
4506         fprintf (dump_file,
4507                  "\nCollapsing static cycles and doing variable "
4508                  "substitution:\n");
4509       
4510       find_and_collapse_graph_cycles (graph, false);
4511       perform_var_substitution (graph);
4512       
4513       if (dump_file)
4514         fprintf (dump_file, "\nSolving graph:\n");
4515       
4516       solve_graph (graph);
4517     }
4518   
4519   if (dump_file)
4520     dump_sa_points_to_info (dump_file);
4521   
4522   have_alias_info = true;
4523
4524   timevar_pop (TV_TREE_PTA);
4525 }
4526
4527
4528 /* Delete created points-to sets.  */
4529
4530 void
4531 delete_points_to_sets (void)
4532 {
4533   varinfo_t v;
4534   int i;
4535
4536   htab_delete (id_for_tree);
4537   bitmap_obstack_release (&ptabitmap_obstack);
4538   bitmap_obstack_release (&predbitmap_obstack);
4539   VEC_free (constraint_t, heap, constraints);
4540   
4541   for (i = 0; VEC_iterate (varinfo_t, varmap, i, v); i++)
4542     {
4543       VEC_free (constraint_edge_t, heap, graph->succs[i]);
4544       VEC_free (constraint_edge_t, heap, graph->preds[i]);
4545       VEC_free (constraint_t, heap, v->complex);
4546     }
4547   free (graph->zero_weight_preds);
4548   free (graph->zero_weight_succs);
4549   free (graph->succs);
4550   free (graph->preds);
4551   free (graph);
4552
4553   VEC_free (varinfo_t, heap, varmap);
4554   free_alloc_pool (variable_info_pool);
4555   free_alloc_pool (constraint_pool); 
4556   free_alloc_pool (constraint_edge_pool);
4557
4558   have_alias_info = false;
4559 }
4560
4561 /* Return true if we should execute IPA PTA.  */
4562 static bool
4563 gate_ipa_pta (void)
4564 {
4565   return (flag_unit_at_a_time != 0
4566           && flag_ipa_pta
4567           /* Don't bother doing anything if the program has errors.  */
4568           && !(errorcount || sorrycount));
4569 }
4570
4571 /* Execute the driver for IPA PTA.  */
4572 static unsigned int
4573 ipa_pta_execute (void)
4574 {
4575   struct cgraph_node *node;
4576   in_ipa_mode = 1;
4577   init_alias_heapvars ();
4578   init_alias_vars ();
4579    
4580   for (node = cgraph_nodes; node; node = node->next)
4581     {
4582       if (!node->analyzed || cgraph_is_master_clone (node))
4583         {
4584           unsigned int varid;
4585           
4586           varid = create_function_info_for (node->decl, 
4587                                             cgraph_node_name (node));
4588           if (node->local.externally_visible)
4589             {
4590               varinfo_t fi = get_varinfo (varid);
4591               for (; fi; fi = fi->next)
4592                 make_constraint_to_anything (fi);
4593             }
4594         }
4595     }
4596   for (node = cgraph_nodes; node; node = node->next)
4597     {
4598       if (node->analyzed && cgraph_is_master_clone (node))
4599         {
4600           struct function *cfun = DECL_STRUCT_FUNCTION (node->decl);
4601           basic_block bb;
4602           tree old_func_decl = current_function_decl;
4603           if (dump_file)
4604             fprintf (dump_file, 
4605                      "Generating constraints for %s\n", 
4606                      cgraph_node_name (node)); 
4607           push_cfun (cfun);
4608           current_function_decl = node->decl;
4609
4610           FOR_EACH_BB_FN (bb, cfun)
4611             {
4612               block_stmt_iterator bsi; 
4613               tree phi;
4614               
4615               for (phi = phi_nodes (bb); phi; phi = TREE_CHAIN (phi))
4616                 {
4617                   if (is_gimple_reg (PHI_RESULT (phi)))
4618                     {
4619                       find_func_aliases (phi);
4620                     }
4621                 }
4622               
4623               for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
4624                 {
4625                   tree stmt = bsi_stmt (bsi);
4626                   find_func_aliases (stmt);
4627                 }
4628             }   
4629           current_function_decl = old_func_decl;
4630           pop_cfun ();    
4631         }
4632       else
4633         {
4634           /* Make point to anything.  */
4635         }
4636     }
4637
4638   build_constraint_graph ();
4639
4640   if (dump_file)
4641     {
4642       fprintf (dump_file, "Points-to analysis\n\nConstraints:\n\n");
4643       dump_constraints (dump_file);
4644     }
4645   
4646   if (need_to_solve ())
4647     {
4648       if (dump_file)
4649         fprintf (dump_file, 
4650                  "\nCollapsing static cycles and doing variable "
4651                  "substitution:\n");
4652       
4653       find_and_collapse_graph_cycles (graph, false);
4654       perform_var_substitution (graph);
4655       
4656       if (dump_file)
4657         fprintf (dump_file, "\nSolving graph:\n");
4658       
4659       solve_graph (graph);
4660     }
4661   
4662   if (dump_file)
4663     dump_sa_points_to_info (dump_file);
4664   in_ipa_mode = 0;
4665   delete_alias_heapvars ();
4666   delete_points_to_sets ();
4667   return 0;
4668 }
4669   
4670 struct tree_opt_pass pass_ipa_pta =
4671 {
4672   "pta",                                /* name */
4673   gate_ipa_pta,                 /* gate */
4674   ipa_pta_execute,                      /* execute */
4675   NULL,                                 /* sub */
4676   NULL,                                 /* next */
4677   0,                                    /* static_pass_number */
4678   TV_IPA_PTA,                   /* tv_id */
4679   0,                                    /* properties_required */
4680   0,                                    /* properties_provided */
4681   0,                                    /* properties_destroyed */
4682   0,                                    /* todo_flags_start */
4683   0,                                    /* todo_flags_finish */
4684   0                                     /* letter */
4685 };
4686
4687 /* Initialize the heapvar for statement mapping.  */
4688 void 
4689 init_alias_heapvars (void)
4690 {
4691   heapvar_for_stmt = htab_create_ggc (11, tree_map_hash, tree_map_eq, NULL);
4692 }
4693
4694 void
4695 delete_alias_heapvars (void)
4696 {
4697   htab_delete (heapvar_for_stmt);  
4698 }
4699
4700   
4701 #include "gt-tree-ssa-structalias.h"