* toplev.h (floor_log2): If GCC_VERSION >= 3004, declare as static
[official-gcc.git] / gcc / tree-ssa-structalias.c
blobf60b24c9c521dcf4a39d3346190d42075d0e73b0
1 /* Tree based points-to analysis
2 Copyright (C) 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
3 Contributed by Daniel Berlin <dberlin@dberlin.org>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "ggc.h"
26 #include "obstack.h"
27 #include "bitmap.h"
28 #include "flags.h"
29 #include "rtl.h"
30 #include "tm_p.h"
31 #include "hard-reg-set.h"
32 #include "basic-block.h"
33 #include "output.h"
34 #include "tree.h"
35 #include "tree-flow.h"
36 #include "tree-inline.h"
37 #include "varray.h"
38 #include "diagnostic.h"
39 #include "toplev.h"
40 #include "gimple.h"
41 #include "hashtab.h"
42 #include "function.h"
43 #include "cgraph.h"
44 #include "tree-pass.h"
45 #include "timevar.h"
46 #include "alloc-pool.h"
47 #include "splay-tree.h"
48 #include "params.h"
49 #include "cgraph.h"
50 #include "alias.h"
51 #include "pointer-set.h"
53 /* The idea behind this analyzer is to generate set constraints from the
54 program, then solve the resulting constraints in order to generate the
55 points-to sets.
57 Set constraints are a way of modeling program analysis problems that
58 involve sets. They consist of an inclusion constraint language,
59 describing the variables (each variable is a set) and operations that
60 are involved on the variables, and a set of rules that derive facts
61 from these operations. To solve a system of set constraints, you derive
62 all possible facts under the rules, which gives you the correct sets
63 as a consequence.
65 See "Efficient Field-sensitive pointer analysis for C" by "David
66 J. Pearce and Paul H. J. Kelly and Chris Hankin, at
67 http://citeseer.ist.psu.edu/pearce04efficient.html
69 Also see "Ultra-fast Aliasing Analysis using CLA: A Million Lines
70 of C Code in a Second" by ""Nevin Heintze and Olivier Tardieu" at
71 http://citeseer.ist.psu.edu/heintze01ultrafast.html
73 There are three types of real constraint expressions, DEREF,
74 ADDRESSOF, and SCALAR. Each constraint expression consists
75 of a constraint type, a variable, and an offset.
77 SCALAR is a constraint expression type used to represent x, whether
78 it appears on the LHS or the RHS of a statement.
79 DEREF is a constraint expression type used to represent *x, whether
80 it appears on the LHS or the RHS of a statement.
81 ADDRESSOF is a constraint expression used to represent &x, whether
82 it appears on the LHS or the RHS of a statement.
84 Each pointer variable in the program is assigned an integer id, and
85 each field of a structure variable is assigned an integer id as well.
87 Structure variables are linked to their list of fields through a "next
88 field" in each variable that points to the next field in offset
89 order.
90 Each variable for a structure field has
92 1. "size", that tells the size in bits of that field.
93 2. "fullsize, that tells the size in bits of the entire structure.
94 3. "offset", that tells the offset in bits from the beginning of the
95 structure to this field.
97 Thus,
98 struct f
100 int a;
101 int b;
102 } foo;
103 int *bar;
105 looks like
107 foo.a -> id 1, size 32, offset 0, fullsize 64, next foo.b
108 foo.b -> id 2, size 32, offset 32, fullsize 64, next NULL
109 bar -> id 3, size 32, offset 0, fullsize 32, next NULL
112 In order to solve the system of set constraints, the following is
113 done:
115 1. Each constraint variable x has a solution set associated with it,
116 Sol(x).
118 2. Constraints are separated into direct, copy, and complex.
119 Direct constraints are ADDRESSOF constraints that require no extra
120 processing, such as P = &Q
121 Copy constraints are those of the form P = Q.
122 Complex constraints are all the constraints involving dereferences
123 and offsets (including offsetted copies).
125 3. All direct constraints of the form P = &Q are processed, such
126 that Q is added to Sol(P)
128 4. All complex constraints for a given constraint variable are stored in a
129 linked list attached to that variable's node.
131 5. A directed graph is built out of the copy constraints. Each
132 constraint variable is a node in the graph, and an edge from
133 Q to P is added for each copy constraint of the form P = Q
135 6. The graph is then walked, and solution sets are
136 propagated along the copy edges, such that an edge from Q to P
137 causes Sol(P) <- Sol(P) union Sol(Q).
139 7. As we visit each node, all complex constraints associated with
140 that node are processed by adding appropriate copy edges to the graph, or the
141 appropriate variables to the solution set.
143 8. The process of walking the graph is iterated until no solution
144 sets change.
146 Prior to walking the graph in steps 6 and 7, We perform static
147 cycle elimination on the constraint graph, as well
148 as off-line variable substitution.
150 TODO: Adding offsets to pointer-to-structures can be handled (IE not punted
151 on and turned into anything), but isn't. You can just see what offset
152 inside the pointed-to struct it's going to access.
154 TODO: Constant bounded arrays can be handled as if they were structs of the
155 same number of elements.
157 TODO: Modeling heap and incoming pointers becomes much better if we
158 add fields to them as we discover them, which we could do.
160 TODO: We could handle unions, but to be honest, it's probably not
161 worth the pain or slowdown. */
163 static GTY ((if_marked ("tree_map_marked_p"), param_is (struct tree_map)))
164 htab_t heapvar_for_stmt;
166 static bool use_field_sensitive = true;
167 static int in_ipa_mode = 0;
169 /* Used for predecessor bitmaps. */
170 static bitmap_obstack predbitmap_obstack;
172 /* Used for points-to sets. */
173 static bitmap_obstack pta_obstack;
175 /* Used for oldsolution members of variables. */
176 static bitmap_obstack oldpta_obstack;
178 /* Used for per-solver-iteration bitmaps. */
179 static bitmap_obstack iteration_obstack;
181 static unsigned int create_variable_info_for (tree, const char *);
182 typedef struct constraint_graph *constraint_graph_t;
183 static void unify_nodes (constraint_graph_t, unsigned int, unsigned int, bool);
185 struct constraint;
186 typedef struct constraint *constraint_t;
188 DEF_VEC_P(constraint_t);
189 DEF_VEC_ALLOC_P(constraint_t,heap);
191 #define EXECUTE_IF_IN_NONNULL_BITMAP(a, b, c, d) \
192 if (a) \
193 EXECUTE_IF_SET_IN_BITMAP (a, b, c, d)
195 static struct constraint_stats
197 unsigned int total_vars;
198 unsigned int nonpointer_vars;
199 unsigned int unified_vars_static;
200 unsigned int unified_vars_dynamic;
201 unsigned int iterations;
202 unsigned int num_edges;
203 unsigned int num_implicit_edges;
204 unsigned int points_to_sets_created;
205 } stats;
207 struct variable_info
209 /* ID of this variable */
210 unsigned int id;
212 /* True if this is a variable created by the constraint analysis, such as
213 heap variables and constraints we had to break up. */
214 unsigned int is_artificial_var:1;
216 /* True if this is a special variable whose solution set should not be
217 changed. */
218 unsigned int is_special_var:1;
220 /* True for variables whose size is not known or variable. */
221 unsigned int is_unknown_size_var:1;
223 /* True for (sub-)fields that represent a whole variable. */
224 unsigned int is_full_var : 1;
226 /* True if this is a heap variable. */
227 unsigned int is_heap_var:1;
229 /* True if this field may contain pointers. */
230 unsigned int may_have_pointers : 1;
232 /* A link to the variable for the next field in this structure. */
233 struct variable_info *next;
235 /* Offset of this variable, in bits, from the base variable */
236 unsigned HOST_WIDE_INT offset;
238 /* Size of the variable, in bits. */
239 unsigned HOST_WIDE_INT size;
241 /* Full size of the base variable, in bits. */
242 unsigned HOST_WIDE_INT fullsize;
244 /* Name of this variable */
245 const char *name;
247 /* Tree that this variable is associated with. */
248 tree decl;
250 /* Points-to set for this variable. */
251 bitmap solution;
253 /* Old points-to set for this variable. */
254 bitmap oldsolution;
256 typedef struct variable_info *varinfo_t;
258 static varinfo_t first_vi_for_offset (varinfo_t, unsigned HOST_WIDE_INT);
259 static varinfo_t first_or_preceding_vi_for_offset (varinfo_t,
260 unsigned HOST_WIDE_INT);
261 static varinfo_t lookup_vi_for_tree (tree);
263 /* Pool of variable info structures. */
264 static alloc_pool variable_info_pool;
266 DEF_VEC_P(varinfo_t);
268 DEF_VEC_ALLOC_P(varinfo_t, heap);
270 /* Table of variable info structures for constraint variables.
271 Indexed directly by variable info id. */
272 static VEC(varinfo_t,heap) *varmap;
274 /* Return the varmap element N */
276 static inline varinfo_t
277 get_varinfo (unsigned int n)
279 return VEC_index (varinfo_t, varmap, n);
282 /* Static IDs for the special variables. */
283 enum { nothing_id = 0, anything_id = 1, readonly_id = 2,
284 escaped_id = 3, nonlocal_id = 4, callused_id = 5,
285 storedanything_id = 6, integer_id = 7 };
287 /* Variable that represents the unknown pointer. */
288 static varinfo_t var_anything;
289 static tree anything_tree;
291 /* Variable that represents the NULL pointer. */
292 static varinfo_t var_nothing;
293 static tree nothing_tree;
295 /* Variable that represents read only memory. */
296 static varinfo_t var_readonly;
297 static tree readonly_tree;
299 /* Variable that represents escaped memory. */
300 static varinfo_t var_escaped;
301 static tree escaped_tree;
303 /* Variable that represents nonlocal memory. */
304 static varinfo_t var_nonlocal;
305 static tree nonlocal_tree;
307 /* Variable that represents call-used memory. */
308 static varinfo_t var_callused;
309 static tree callused_tree;
311 /* Variable that represents variables that are stored to anything. */
312 static varinfo_t var_storedanything;
313 static tree storedanything_tree;
315 /* Variable that represents integers. This is used for when people do things
316 like &0->a.b. */
317 static varinfo_t var_integer;
318 static tree integer_tree;
320 /* Lookup a heap var for FROM, and return it if we find one. */
322 static tree
323 heapvar_lookup (tree from)
325 struct tree_map *h, in;
326 in.base.from = from;
328 h = (struct tree_map *) htab_find_with_hash (heapvar_for_stmt, &in,
329 htab_hash_pointer (from));
330 if (h)
331 return h->to;
332 return NULL_TREE;
335 /* Insert a mapping FROM->TO in the heap var for statement
336 hashtable. */
338 static void
339 heapvar_insert (tree from, tree to)
341 struct tree_map *h;
342 void **loc;
344 h = GGC_NEW (struct tree_map);
345 h->hash = htab_hash_pointer (from);
346 h->base.from = from;
347 h->to = to;
348 loc = htab_find_slot_with_hash (heapvar_for_stmt, h, h->hash, INSERT);
349 *(struct tree_map **) loc = h;
352 /* Return a new variable info structure consisting for a variable
353 named NAME, and using constraint graph node NODE. */
355 static varinfo_t
356 new_var_info (tree t, unsigned int id, const char *name)
358 varinfo_t ret = (varinfo_t) pool_alloc (variable_info_pool);
360 ret->id = id;
361 ret->name = name;
362 ret->decl = t;
363 ret->is_artificial_var = false;
364 ret->is_heap_var = false;
365 ret->is_special_var = false;
366 ret->is_unknown_size_var = false;
367 ret->is_full_var = false;
368 ret->may_have_pointers = true;
369 ret->solution = BITMAP_ALLOC (&pta_obstack);
370 ret->oldsolution = BITMAP_ALLOC (&oldpta_obstack);
371 ret->next = NULL;
372 return ret;
375 typedef enum {SCALAR, DEREF, ADDRESSOF} constraint_expr_type;
377 /* An expression that appears in a constraint. */
379 struct constraint_expr
381 /* Constraint type. */
382 constraint_expr_type type;
384 /* Variable we are referring to in the constraint. */
385 unsigned int var;
387 /* Offset, in bits, of this constraint from the beginning of
388 variables it ends up referring to.
390 IOW, in a deref constraint, we would deref, get the result set,
391 then add OFFSET to each member. */
392 HOST_WIDE_INT offset;
395 /* Use 0x8000... as special unknown offset. */
396 #define UNKNOWN_OFFSET ((HOST_WIDE_INT)-1 << (HOST_BITS_PER_WIDE_INT-1))
398 typedef struct constraint_expr ce_s;
399 DEF_VEC_O(ce_s);
400 DEF_VEC_ALLOC_O(ce_s, heap);
401 static void get_constraint_for_1 (tree, VEC(ce_s, heap) **, bool);
402 static void get_constraint_for (tree, VEC(ce_s, heap) **);
403 static void do_deref (VEC (ce_s, heap) **);
405 /* Our set constraints are made up of two constraint expressions, one
406 LHS, and one RHS.
408 As described in the introduction, our set constraints each represent an
409 operation between set valued variables.
411 struct constraint
413 struct constraint_expr lhs;
414 struct constraint_expr rhs;
417 /* List of constraints that we use to build the constraint graph from. */
419 static VEC(constraint_t,heap) *constraints;
420 static alloc_pool constraint_pool;
423 DEF_VEC_I(int);
424 DEF_VEC_ALLOC_I(int, heap);
426 /* The constraint graph is represented as an array of bitmaps
427 containing successor nodes. */
429 struct constraint_graph
431 /* Size of this graph, which may be different than the number of
432 nodes in the variable map. */
433 unsigned int size;
435 /* Explicit successors of each node. */
436 bitmap *succs;
438 /* Implicit predecessors of each node (Used for variable
439 substitution). */
440 bitmap *implicit_preds;
442 /* Explicit predecessors of each node (Used for variable substitution). */
443 bitmap *preds;
445 /* Indirect cycle representatives, or -1 if the node has no indirect
446 cycles. */
447 int *indirect_cycles;
449 /* Representative node for a node. rep[a] == a unless the node has
450 been unified. */
451 unsigned int *rep;
453 /* Equivalence class representative for a label. This is used for
454 variable substitution. */
455 int *eq_rep;
457 /* Pointer equivalence label for a node. All nodes with the same
458 pointer equivalence label can be unified together at some point
459 (either during constraint optimization or after the constraint
460 graph is built). */
461 unsigned int *pe;
463 /* Pointer equivalence representative for a label. This is used to
464 handle nodes that are pointer equivalent but not location
465 equivalent. We can unite these once the addressof constraints
466 are transformed into initial points-to sets. */
467 int *pe_rep;
469 /* Pointer equivalence label for each node, used during variable
470 substitution. */
471 unsigned int *pointer_label;
473 /* Location equivalence label for each node, used during location
474 equivalence finding. */
475 unsigned int *loc_label;
477 /* Pointed-by set for each node, used during location equivalence
478 finding. This is pointed-by rather than pointed-to, because it
479 is constructed using the predecessor graph. */
480 bitmap *pointed_by;
482 /* Points to sets for pointer equivalence. This is *not* the actual
483 points-to sets for nodes. */
484 bitmap *points_to;
486 /* Bitmap of nodes where the bit is set if the node is a direct
487 node. Used for variable substitution. */
488 sbitmap direct_nodes;
490 /* Bitmap of nodes where the bit is set if the node is address
491 taken. Used for variable substitution. */
492 bitmap address_taken;
494 /* Vector of complex constraints for each graph node. Complex
495 constraints are those involving dereferences or offsets that are
496 not 0. */
497 VEC(constraint_t,heap) **complex;
500 static constraint_graph_t graph;
502 /* During variable substitution and the offline version of indirect
503 cycle finding, we create nodes to represent dereferences and
504 address taken constraints. These represent where these start and
505 end. */
506 #define FIRST_REF_NODE (VEC_length (varinfo_t, varmap))
507 #define LAST_REF_NODE (FIRST_REF_NODE + (FIRST_REF_NODE - 1))
509 /* Return the representative node for NODE, if NODE has been unioned
510 with another NODE.
511 This function performs path compression along the way to finding
512 the representative. */
514 static unsigned int
515 find (unsigned int node)
517 gcc_assert (node < graph->size);
518 if (graph->rep[node] != node)
519 return graph->rep[node] = find (graph->rep[node]);
520 return node;
523 /* Union the TO and FROM nodes to the TO nodes.
524 Note that at some point in the future, we may want to do
525 union-by-rank, in which case we are going to have to return the
526 node we unified to. */
528 static bool
529 unite (unsigned int to, unsigned int from)
531 gcc_assert (to < graph->size && from < graph->size);
532 if (to != from && graph->rep[from] != to)
534 graph->rep[from] = to;
535 return true;
537 return false;
540 /* Create a new constraint consisting of LHS and RHS expressions. */
542 static constraint_t
543 new_constraint (const struct constraint_expr lhs,
544 const struct constraint_expr rhs)
546 constraint_t ret = (constraint_t) pool_alloc (constraint_pool);
547 ret->lhs = lhs;
548 ret->rhs = rhs;
549 return ret;
552 /* Print out constraint C to FILE. */
554 static void
555 dump_constraint (FILE *file, constraint_t c)
557 if (c->lhs.type == ADDRESSOF)
558 fprintf (file, "&");
559 else if (c->lhs.type == DEREF)
560 fprintf (file, "*");
561 fprintf (file, "%s", get_varinfo (c->lhs.var)->name);
562 if (c->lhs.offset == UNKNOWN_OFFSET)
563 fprintf (file, " + UNKNOWN");
564 else if (c->lhs.offset != 0)
565 fprintf (file, " + " HOST_WIDE_INT_PRINT_DEC, c->lhs.offset);
566 fprintf (file, " = ");
567 if (c->rhs.type == ADDRESSOF)
568 fprintf (file, "&");
569 else if (c->rhs.type == DEREF)
570 fprintf (file, "*");
571 fprintf (file, "%s", get_varinfo (c->rhs.var)->name);
572 if (c->rhs.offset == UNKNOWN_OFFSET)
573 fprintf (file, " + UNKNOWN");
574 else if (c->rhs.offset != 0)
575 fprintf (file, " + " HOST_WIDE_INT_PRINT_DEC, c->rhs.offset);
576 fprintf (file, "\n");
580 void debug_constraint (constraint_t);
581 void debug_constraints (void);
582 void debug_constraint_graph (void);
583 void debug_solution_for_var (unsigned int);
584 void debug_sa_points_to_info (void);
586 /* Print out constraint C to stderr. */
588 void
589 debug_constraint (constraint_t c)
591 dump_constraint (stderr, c);
594 /* Print out all constraints to FILE */
596 static void
597 dump_constraints (FILE *file)
599 int i;
600 constraint_t c;
601 for (i = 0; VEC_iterate (constraint_t, constraints, i, c); i++)
602 dump_constraint (file, c);
605 /* Print out all constraints to stderr. */
607 void
608 debug_constraints (void)
610 dump_constraints (stderr);
613 /* Print out to FILE the edge in the constraint graph that is created by
614 constraint c. The edge may have a label, depending on the type of
615 constraint that it represents. If complex1, e.g: a = *b, then the label
616 is "=*", if complex2, e.g: *a = b, then the label is "*=", if
617 complex with an offset, e.g: a = b + 8, then the label is "+".
618 Otherwise the edge has no label. */
620 static void
621 dump_constraint_edge (FILE *file, constraint_t c)
623 if (c->rhs.type != ADDRESSOF)
625 const char *src = get_varinfo (c->rhs.var)->name;
626 const char *dst = get_varinfo (c->lhs.var)->name;
627 fprintf (file, " \"%s\" -> \"%s\" ", src, dst);
628 /* Due to preprocessing of constraints, instructions like *a = *b are
629 illegal; thus, we do not have to handle such cases. */
630 if (c->lhs.type == DEREF)
631 fprintf (file, " [ label=\"*=\" ] ;\n");
632 else if (c->rhs.type == DEREF)
633 fprintf (file, " [ label=\"=*\" ] ;\n");
634 else
636 /* We must check the case where the constraint is an offset.
637 In this case, it is treated as a complex constraint. */
638 if (c->rhs.offset != c->lhs.offset)
639 fprintf (file, " [ label=\"+\" ] ;\n");
640 else
641 fprintf (file, " ;\n");
646 /* Print the constraint graph in dot format. */
648 static void
649 dump_constraint_graph (FILE *file)
651 unsigned int i=0, size;
652 constraint_t c;
654 /* Only print the graph if it has already been initialized: */
655 if (!graph)
656 return;
658 /* Print the constraints used to produce the constraint graph. The
659 constraints will be printed as comments in the dot file: */
660 fprintf (file, "\n\n/* Constraints used in the constraint graph:\n");
661 dump_constraints (file);
662 fprintf (file, "*/\n");
664 /* Prints the header of the dot file: */
665 fprintf (file, "\n\n// The constraint graph in dot format:\n");
666 fprintf (file, "strict digraph {\n");
667 fprintf (file, " node [\n shape = box\n ]\n");
668 fprintf (file, " edge [\n fontsize = \"12\"\n ]\n");
669 fprintf (file, "\n // List of nodes in the constraint graph:\n");
671 /* The next lines print the nodes in the graph. In order to get the
672 number of nodes in the graph, we must choose the minimum between the
673 vector VEC (varinfo_t, varmap) and graph->size. If the graph has not
674 yet been initialized, then graph->size == 0, otherwise we must only
675 read nodes that have an entry in VEC (varinfo_t, varmap). */
676 size = VEC_length (varinfo_t, varmap);
677 size = size < graph->size ? size : graph->size;
678 for (i = 0; i < size; i++)
680 const char *name = get_varinfo (graph->rep[i])->name;
681 fprintf (file, " \"%s\" ;\n", name);
684 /* Go over the list of constraints printing the edges in the constraint
685 graph. */
686 fprintf (file, "\n // The constraint edges:\n");
687 for (i = 0; VEC_iterate (constraint_t, constraints, i, c); i++)
688 if (c)
689 dump_constraint_edge (file, c);
691 /* Prints the tail of the dot file. By now, only the closing bracket. */
692 fprintf (file, "}\n\n\n");
695 /* Print out the constraint graph to stderr. */
697 void
698 debug_constraint_graph (void)
700 dump_constraint_graph (stderr);
703 /* SOLVER FUNCTIONS
705 The solver is a simple worklist solver, that works on the following
706 algorithm:
708 sbitmap changed_nodes = all zeroes;
709 changed_count = 0;
710 For each node that is not already collapsed:
711 changed_count++;
712 set bit in changed nodes
714 while (changed_count > 0)
716 compute topological ordering for constraint graph
718 find and collapse cycles in the constraint graph (updating
719 changed if necessary)
721 for each node (n) in the graph in topological order:
722 changed_count--;
724 Process each complex constraint associated with the node,
725 updating changed if necessary.
727 For each outgoing edge from n, propagate the solution from n to
728 the destination of the edge, updating changed as necessary.
730 } */
732 /* Return true if two constraint expressions A and B are equal. */
734 static bool
735 constraint_expr_equal (struct constraint_expr a, struct constraint_expr b)
737 return a.type == b.type && a.var == b.var && a.offset == b.offset;
740 /* Return true if constraint expression A is less than constraint expression
741 B. This is just arbitrary, but consistent, in order to give them an
742 ordering. */
744 static bool
745 constraint_expr_less (struct constraint_expr a, struct constraint_expr b)
747 if (a.type == b.type)
749 if (a.var == b.var)
750 return a.offset < b.offset;
751 else
752 return a.var < b.var;
754 else
755 return a.type < b.type;
758 /* Return true if constraint A is less than constraint B. This is just
759 arbitrary, but consistent, in order to give them an ordering. */
761 static bool
762 constraint_less (const constraint_t a, const constraint_t b)
764 if (constraint_expr_less (a->lhs, b->lhs))
765 return true;
766 else if (constraint_expr_less (b->lhs, a->lhs))
767 return false;
768 else
769 return constraint_expr_less (a->rhs, b->rhs);
772 /* Return true if two constraints A and B are equal. */
774 static bool
775 constraint_equal (struct constraint a, struct constraint b)
777 return constraint_expr_equal (a.lhs, b.lhs)
778 && constraint_expr_equal (a.rhs, b.rhs);
782 /* Find a constraint LOOKFOR in the sorted constraint vector VEC */
784 static constraint_t
785 constraint_vec_find (VEC(constraint_t,heap) *vec,
786 struct constraint lookfor)
788 unsigned int place;
789 constraint_t found;
791 if (vec == NULL)
792 return NULL;
794 place = VEC_lower_bound (constraint_t, vec, &lookfor, constraint_less);
795 if (place >= VEC_length (constraint_t, vec))
796 return NULL;
797 found = VEC_index (constraint_t, vec, place);
798 if (!constraint_equal (*found, lookfor))
799 return NULL;
800 return found;
803 /* Union two constraint vectors, TO and FROM. Put the result in TO. */
805 static void
806 constraint_set_union (VEC(constraint_t,heap) **to,
807 VEC(constraint_t,heap) **from)
809 int i;
810 constraint_t c;
812 for (i = 0; VEC_iterate (constraint_t, *from, i, c); i++)
814 if (constraint_vec_find (*to, *c) == NULL)
816 unsigned int place = VEC_lower_bound (constraint_t, *to, c,
817 constraint_less);
818 VEC_safe_insert (constraint_t, heap, *to, place, c);
823 /* Expands the solution in SET to all sub-fields of variables included.
824 Union the expanded result into RESULT. */
826 static void
827 solution_set_expand (bitmap result, bitmap set)
829 bitmap_iterator bi;
830 bitmap vars = NULL;
831 unsigned j;
833 /* In a first pass record all variables we need to add all
834 sub-fields off. This avoids quadratic behavior. */
835 EXECUTE_IF_SET_IN_BITMAP (set, 0, j, bi)
837 varinfo_t v = get_varinfo (j);
838 if (v->is_artificial_var
839 || v->is_full_var)
840 continue;
841 v = lookup_vi_for_tree (v->decl);
842 if (vars == NULL)
843 vars = BITMAP_ALLOC (NULL);
844 bitmap_set_bit (vars, v->id);
847 /* In the second pass now do the addition to the solution and
848 to speed up solving add it to the delta as well. */
849 if (vars != NULL)
851 EXECUTE_IF_SET_IN_BITMAP (vars, 0, j, bi)
853 varinfo_t v = get_varinfo (j);
854 for (; v != NULL; v = v->next)
855 bitmap_set_bit (result, v->id);
857 BITMAP_FREE (vars);
861 /* Take a solution set SET, add OFFSET to each member of the set, and
862 overwrite SET with the result when done. */
864 static void
865 solution_set_add (bitmap set, HOST_WIDE_INT offset)
867 bitmap result = BITMAP_ALLOC (&iteration_obstack);
868 unsigned int i;
869 bitmap_iterator bi;
871 /* If the offset is unknown we have to expand the solution to
872 all subfields. */
873 if (offset == UNKNOWN_OFFSET)
875 solution_set_expand (set, set);
876 return;
879 EXECUTE_IF_SET_IN_BITMAP (set, 0, i, bi)
881 varinfo_t vi = get_varinfo (i);
883 /* If this is a variable with just one field just set its bit
884 in the result. */
885 if (vi->is_artificial_var
886 || vi->is_unknown_size_var
887 || vi->is_full_var)
888 bitmap_set_bit (result, i);
889 else
891 unsigned HOST_WIDE_INT fieldoffset = vi->offset + offset;
893 /* If the offset makes the pointer point to before the
894 variable use offset zero for the field lookup. */
895 if (offset < 0
896 && fieldoffset > vi->offset)
897 fieldoffset = 0;
899 if (offset != 0)
900 vi = first_or_preceding_vi_for_offset (vi, fieldoffset);
902 bitmap_set_bit (result, vi->id);
903 /* If the result is not exactly at fieldoffset include the next
904 field as well. See get_constraint_for_ptr_offset for more
905 rationale. */
906 if (vi->offset != fieldoffset
907 && vi->next != NULL)
908 bitmap_set_bit (result, vi->next->id);
912 bitmap_copy (set, result);
913 BITMAP_FREE (result);
916 /* Union solution sets TO and FROM, and add INC to each member of FROM in the
917 process. */
919 static bool
920 set_union_with_increment (bitmap to, bitmap from, HOST_WIDE_INT inc)
922 if (inc == 0)
923 return bitmap_ior_into (to, from);
924 else
926 bitmap tmp;
927 bool res;
929 tmp = BITMAP_ALLOC (&iteration_obstack);
930 bitmap_copy (tmp, from);
931 solution_set_add (tmp, inc);
932 res = bitmap_ior_into (to, tmp);
933 BITMAP_FREE (tmp);
934 return res;
938 /* Insert constraint C into the list of complex constraints for graph
939 node VAR. */
941 static void
942 insert_into_complex (constraint_graph_t graph,
943 unsigned int var, constraint_t c)
945 VEC (constraint_t, heap) *complex = graph->complex[var];
946 unsigned int place = VEC_lower_bound (constraint_t, complex, c,
947 constraint_less);
949 /* Only insert constraints that do not already exist. */
950 if (place >= VEC_length (constraint_t, complex)
951 || !constraint_equal (*c, *VEC_index (constraint_t, complex, place)))
952 VEC_safe_insert (constraint_t, heap, graph->complex[var], place, c);
956 /* Condense two variable nodes into a single variable node, by moving
957 all associated info from SRC to TO. */
959 static void
960 merge_node_constraints (constraint_graph_t graph, unsigned int to,
961 unsigned int from)
963 unsigned int i;
964 constraint_t c;
966 gcc_assert (find (from) == to);
968 /* Move all complex constraints from src node into to node */
969 for (i = 0; VEC_iterate (constraint_t, graph->complex[from], i, c); i++)
971 /* In complex constraints for node src, we may have either
972 a = *src, and *src = a, or an offseted constraint which are
973 always added to the rhs node's constraints. */
975 if (c->rhs.type == DEREF)
976 c->rhs.var = to;
977 else if (c->lhs.type == DEREF)
978 c->lhs.var = to;
979 else
980 c->rhs.var = to;
982 constraint_set_union (&graph->complex[to], &graph->complex[from]);
983 VEC_free (constraint_t, heap, graph->complex[from]);
984 graph->complex[from] = NULL;
988 /* Remove edges involving NODE from GRAPH. */
990 static void
991 clear_edges_for_node (constraint_graph_t graph, unsigned int node)
993 if (graph->succs[node])
994 BITMAP_FREE (graph->succs[node]);
997 /* Merge GRAPH nodes FROM and TO into node TO. */
999 static void
1000 merge_graph_nodes (constraint_graph_t graph, unsigned int to,
1001 unsigned int from)
1003 if (graph->indirect_cycles[from] != -1)
1005 /* If we have indirect cycles with the from node, and we have
1006 none on the to node, the to node has indirect cycles from the
1007 from node now that they are unified.
1008 If indirect cycles exist on both, unify the nodes that they
1009 are in a cycle with, since we know they are in a cycle with
1010 each other. */
1011 if (graph->indirect_cycles[to] == -1)
1012 graph->indirect_cycles[to] = graph->indirect_cycles[from];
1015 /* Merge all the successor edges. */
1016 if (graph->succs[from])
1018 if (!graph->succs[to])
1019 graph->succs[to] = BITMAP_ALLOC (&pta_obstack);
1020 bitmap_ior_into (graph->succs[to],
1021 graph->succs[from]);
1024 clear_edges_for_node (graph, from);
1028 /* Add an indirect graph edge to GRAPH, going from TO to FROM if
1029 it doesn't exist in the graph already. */
1031 static void
1032 add_implicit_graph_edge (constraint_graph_t graph, unsigned int to,
1033 unsigned int from)
1035 if (to == from)
1036 return;
1038 if (!graph->implicit_preds[to])
1039 graph->implicit_preds[to] = BITMAP_ALLOC (&predbitmap_obstack);
1041 if (bitmap_set_bit (graph->implicit_preds[to], from))
1042 stats.num_implicit_edges++;
1045 /* Add a predecessor graph edge to GRAPH, going from TO to FROM if
1046 it doesn't exist in the graph already.
1047 Return false if the edge already existed, true otherwise. */
1049 static void
1050 add_pred_graph_edge (constraint_graph_t graph, unsigned int to,
1051 unsigned int from)
1053 if (!graph->preds[to])
1054 graph->preds[to] = BITMAP_ALLOC (&predbitmap_obstack);
1055 bitmap_set_bit (graph->preds[to], from);
1058 /* Add a graph edge to GRAPH, going from FROM to TO if
1059 it doesn't exist in the graph already.
1060 Return false if the edge already existed, true otherwise. */
1062 static bool
1063 add_graph_edge (constraint_graph_t graph, unsigned int to,
1064 unsigned int from)
1066 if (to == from)
1068 return false;
1070 else
1072 bool r = false;
1074 if (!graph->succs[from])
1075 graph->succs[from] = BITMAP_ALLOC (&pta_obstack);
1076 if (bitmap_set_bit (graph->succs[from], to))
1078 r = true;
1079 if (to < FIRST_REF_NODE && from < FIRST_REF_NODE)
1080 stats.num_edges++;
1082 return r;
1087 /* Return true if {DEST.SRC} is an existing graph edge in GRAPH. */
1089 static bool
1090 valid_graph_edge (constraint_graph_t graph, unsigned int src,
1091 unsigned int dest)
1093 return (graph->succs[dest]
1094 && bitmap_bit_p (graph->succs[dest], src));
1097 /* Initialize the constraint graph structure to contain SIZE nodes. */
1099 static void
1100 init_graph (unsigned int size)
1102 unsigned int j;
1104 graph = XCNEW (struct constraint_graph);
1105 graph->size = size;
1106 graph->succs = XCNEWVEC (bitmap, graph->size);
1107 graph->indirect_cycles = XNEWVEC (int, graph->size);
1108 graph->rep = XNEWVEC (unsigned int, graph->size);
1109 graph->complex = XCNEWVEC (VEC(constraint_t, heap) *, size);
1110 graph->pe = XCNEWVEC (unsigned int, graph->size);
1111 graph->pe_rep = XNEWVEC (int, graph->size);
1113 for (j = 0; j < graph->size; j++)
1115 graph->rep[j] = j;
1116 graph->pe_rep[j] = -1;
1117 graph->indirect_cycles[j] = -1;
1121 /* Build the constraint graph, adding only predecessor edges right now. */
1123 static void
1124 build_pred_graph (void)
1126 int i;
1127 constraint_t c;
1128 unsigned int j;
1130 graph->implicit_preds = XCNEWVEC (bitmap, graph->size);
1131 graph->preds = XCNEWVEC (bitmap, graph->size);
1132 graph->pointer_label = XCNEWVEC (unsigned int, graph->size);
1133 graph->loc_label = XCNEWVEC (unsigned int, graph->size);
1134 graph->pointed_by = XCNEWVEC (bitmap, graph->size);
1135 graph->points_to = XCNEWVEC (bitmap, graph->size);
1136 graph->eq_rep = XNEWVEC (int, graph->size);
1137 graph->direct_nodes = sbitmap_alloc (graph->size);
1138 graph->address_taken = BITMAP_ALLOC (&predbitmap_obstack);
1139 sbitmap_zero (graph->direct_nodes);
1141 for (j = 0; j < FIRST_REF_NODE; j++)
1143 if (!get_varinfo (j)->is_special_var)
1144 SET_BIT (graph->direct_nodes, j);
1147 for (j = 0; j < graph->size; j++)
1148 graph->eq_rep[j] = -1;
1150 for (j = 0; j < VEC_length (varinfo_t, varmap); j++)
1151 graph->indirect_cycles[j] = -1;
1153 for (i = 0; VEC_iterate (constraint_t, constraints, i, c); i++)
1155 struct constraint_expr lhs = c->lhs;
1156 struct constraint_expr rhs = c->rhs;
1157 unsigned int lhsvar = lhs.var;
1158 unsigned int rhsvar = rhs.var;
1160 if (lhs.type == DEREF)
1162 /* *x = y. */
1163 if (rhs.offset == 0 && lhs.offset == 0 && rhs.type == SCALAR)
1164 add_pred_graph_edge (graph, FIRST_REF_NODE + lhsvar, rhsvar);
1166 else if (rhs.type == DEREF)
1168 /* x = *y */
1169 if (rhs.offset == 0 && lhs.offset == 0 && lhs.type == SCALAR)
1170 add_pred_graph_edge (graph, lhsvar, FIRST_REF_NODE + rhsvar);
1171 else
1172 RESET_BIT (graph->direct_nodes, lhsvar);
1174 else if (rhs.type == ADDRESSOF)
1176 varinfo_t v;
1178 /* x = &y */
1179 if (graph->points_to[lhsvar] == NULL)
1180 graph->points_to[lhsvar] = BITMAP_ALLOC (&predbitmap_obstack);
1181 bitmap_set_bit (graph->points_to[lhsvar], rhsvar);
1183 if (graph->pointed_by[rhsvar] == NULL)
1184 graph->pointed_by[rhsvar] = BITMAP_ALLOC (&predbitmap_obstack);
1185 bitmap_set_bit (graph->pointed_by[rhsvar], lhsvar);
1187 /* Implicitly, *x = y */
1188 add_implicit_graph_edge (graph, FIRST_REF_NODE + lhsvar, rhsvar);
1190 /* All related variables are no longer direct nodes. */
1191 RESET_BIT (graph->direct_nodes, rhsvar);
1192 v = get_varinfo (rhsvar);
1193 if (!v->is_full_var)
1195 v = lookup_vi_for_tree (v->decl);
1198 RESET_BIT (graph->direct_nodes, v->id);
1199 v = v->next;
1201 while (v != NULL);
1203 bitmap_set_bit (graph->address_taken, rhsvar);
1205 else if (lhsvar > anything_id
1206 && lhsvar != rhsvar && lhs.offset == 0 && rhs.offset == 0)
1208 /* x = y */
1209 add_pred_graph_edge (graph, lhsvar, rhsvar);
1210 /* Implicitly, *x = *y */
1211 add_implicit_graph_edge (graph, FIRST_REF_NODE + lhsvar,
1212 FIRST_REF_NODE + rhsvar);
1214 else if (lhs.offset != 0 || rhs.offset != 0)
1216 if (rhs.offset != 0)
1217 RESET_BIT (graph->direct_nodes, lhs.var);
1218 else if (lhs.offset != 0)
1219 RESET_BIT (graph->direct_nodes, rhs.var);
1224 /* Build the constraint graph, adding successor edges. */
1226 static void
1227 build_succ_graph (void)
1229 unsigned i, t;
1230 constraint_t c;
1232 for (i = 0; VEC_iterate (constraint_t, constraints, i, c); i++)
1234 struct constraint_expr lhs;
1235 struct constraint_expr rhs;
1236 unsigned int lhsvar;
1237 unsigned int rhsvar;
1239 if (!c)
1240 continue;
1242 lhs = c->lhs;
1243 rhs = c->rhs;
1244 lhsvar = find (lhs.var);
1245 rhsvar = find (rhs.var);
1247 if (lhs.type == DEREF)
1249 if (rhs.offset == 0 && lhs.offset == 0 && rhs.type == SCALAR)
1250 add_graph_edge (graph, FIRST_REF_NODE + lhsvar, rhsvar);
1252 else if (rhs.type == DEREF)
1254 if (rhs.offset == 0 && lhs.offset == 0 && lhs.type == SCALAR)
1255 add_graph_edge (graph, lhsvar, FIRST_REF_NODE + rhsvar);
1257 else if (rhs.type == ADDRESSOF)
1259 /* x = &y */
1260 gcc_assert (find (rhs.var) == rhs.var);
1261 bitmap_set_bit (get_varinfo (lhsvar)->solution, rhsvar);
1263 else if (lhsvar > anything_id
1264 && lhsvar != rhsvar && lhs.offset == 0 && rhs.offset == 0)
1266 add_graph_edge (graph, lhsvar, rhsvar);
1270 /* Add edges from STOREDANYTHING to all non-direct nodes. */
1271 t = find (storedanything_id);
1272 for (i = integer_id + 1; i < FIRST_REF_NODE; ++i)
1274 if (!TEST_BIT (graph->direct_nodes, i))
1275 add_graph_edge (graph, find (i), t);
1280 /* Changed variables on the last iteration. */
1281 static unsigned int changed_count;
1282 static sbitmap changed;
1284 DEF_VEC_I(unsigned);
1285 DEF_VEC_ALLOC_I(unsigned,heap);
1288 /* Strongly Connected Component visitation info. */
1290 struct scc_info
1292 sbitmap visited;
1293 sbitmap deleted;
1294 unsigned int *dfs;
1295 unsigned int *node_mapping;
1296 int current_index;
1297 VEC(unsigned,heap) *scc_stack;
1301 /* Recursive routine to find strongly connected components in GRAPH.
1302 SI is the SCC info to store the information in, and N is the id of current
1303 graph node we are processing.
1305 This is Tarjan's strongly connected component finding algorithm, as
1306 modified by Nuutila to keep only non-root nodes on the stack.
1307 The algorithm can be found in "On finding the strongly connected
1308 connected components in a directed graph" by Esko Nuutila and Eljas
1309 Soisalon-Soininen, in Information Processing Letters volume 49,
1310 number 1, pages 9-14. */
1312 static void
1313 scc_visit (constraint_graph_t graph, struct scc_info *si, unsigned int n)
1315 unsigned int i;
1316 bitmap_iterator bi;
1317 unsigned int my_dfs;
1319 SET_BIT (si->visited, n);
1320 si->dfs[n] = si->current_index ++;
1321 my_dfs = si->dfs[n];
1323 /* Visit all the successors. */
1324 EXECUTE_IF_IN_NONNULL_BITMAP (graph->succs[n], 0, i, bi)
1326 unsigned int w;
1328 if (i > LAST_REF_NODE)
1329 break;
1331 w = find (i);
1332 if (TEST_BIT (si->deleted, w))
1333 continue;
1335 if (!TEST_BIT (si->visited, w))
1336 scc_visit (graph, si, w);
1338 unsigned int t = find (w);
1339 unsigned int nnode = find (n);
1340 gcc_assert (nnode == n);
1342 if (si->dfs[t] < si->dfs[nnode])
1343 si->dfs[n] = si->dfs[t];
1347 /* See if any components have been identified. */
1348 if (si->dfs[n] == my_dfs)
1350 if (VEC_length (unsigned, si->scc_stack) > 0
1351 && si->dfs[VEC_last (unsigned, si->scc_stack)] >= my_dfs)
1353 bitmap scc = BITMAP_ALLOC (NULL);
1354 bool have_ref_node = n >= FIRST_REF_NODE;
1355 unsigned int lowest_node;
1356 bitmap_iterator bi;
1358 bitmap_set_bit (scc, n);
1360 while (VEC_length (unsigned, si->scc_stack) != 0
1361 && si->dfs[VEC_last (unsigned, si->scc_stack)] >= my_dfs)
1363 unsigned int w = VEC_pop (unsigned, si->scc_stack);
1365 bitmap_set_bit (scc, w);
1366 if (w >= FIRST_REF_NODE)
1367 have_ref_node = true;
1370 lowest_node = bitmap_first_set_bit (scc);
1371 gcc_assert (lowest_node < FIRST_REF_NODE);
1373 /* Collapse the SCC nodes into a single node, and mark the
1374 indirect cycles. */
1375 EXECUTE_IF_SET_IN_BITMAP (scc, 0, i, bi)
1377 if (i < FIRST_REF_NODE)
1379 if (unite (lowest_node, i))
1380 unify_nodes (graph, lowest_node, i, false);
1382 else
1384 unite (lowest_node, i);
1385 graph->indirect_cycles[i - FIRST_REF_NODE] = lowest_node;
1389 SET_BIT (si->deleted, n);
1391 else
1392 VEC_safe_push (unsigned, heap, si->scc_stack, n);
1395 /* Unify node FROM into node TO, updating the changed count if
1396 necessary when UPDATE_CHANGED is true. */
1398 static void
1399 unify_nodes (constraint_graph_t graph, unsigned int to, unsigned int from,
1400 bool update_changed)
1403 gcc_assert (to != from && find (to) == to);
1404 if (dump_file && (dump_flags & TDF_DETAILS))
1405 fprintf (dump_file, "Unifying %s to %s\n",
1406 get_varinfo (from)->name,
1407 get_varinfo (to)->name);
1409 if (update_changed)
1410 stats.unified_vars_dynamic++;
1411 else
1412 stats.unified_vars_static++;
1414 merge_graph_nodes (graph, to, from);
1415 merge_node_constraints (graph, to, from);
1417 /* Mark TO as changed if FROM was changed. If TO was already marked
1418 as changed, decrease the changed count. */
1420 if (update_changed && TEST_BIT (changed, from))
1422 RESET_BIT (changed, from);
1423 if (!TEST_BIT (changed, to))
1424 SET_BIT (changed, to);
1425 else
1427 gcc_assert (changed_count > 0);
1428 changed_count--;
1431 if (get_varinfo (from)->solution)
1433 /* If the solution changes because of the merging, we need to mark
1434 the variable as changed. */
1435 if (bitmap_ior_into (get_varinfo (to)->solution,
1436 get_varinfo (from)->solution))
1438 if (update_changed && !TEST_BIT (changed, to))
1440 SET_BIT (changed, to);
1441 changed_count++;
1445 BITMAP_FREE (get_varinfo (from)->solution);
1446 BITMAP_FREE (get_varinfo (from)->oldsolution);
1448 if (stats.iterations > 0)
1450 BITMAP_FREE (get_varinfo (to)->oldsolution);
1451 get_varinfo (to)->oldsolution = BITMAP_ALLOC (&oldpta_obstack);
1454 if (valid_graph_edge (graph, to, to))
1456 if (graph->succs[to])
1457 bitmap_clear_bit (graph->succs[to], to);
1461 /* Information needed to compute the topological ordering of a graph. */
1463 struct topo_info
1465 /* sbitmap of visited nodes. */
1466 sbitmap visited;
1467 /* Array that stores the topological order of the graph, *in
1468 reverse*. */
1469 VEC(unsigned,heap) *topo_order;
1473 /* Initialize and return a topological info structure. */
1475 static struct topo_info *
1476 init_topo_info (void)
1478 size_t size = graph->size;
1479 struct topo_info *ti = XNEW (struct topo_info);
1480 ti->visited = sbitmap_alloc (size);
1481 sbitmap_zero (ti->visited);
1482 ti->topo_order = VEC_alloc (unsigned, heap, 1);
1483 return ti;
1487 /* Free the topological sort info pointed to by TI. */
1489 static void
1490 free_topo_info (struct topo_info *ti)
1492 sbitmap_free (ti->visited);
1493 VEC_free (unsigned, heap, ti->topo_order);
1494 free (ti);
1497 /* Visit the graph in topological order, and store the order in the
1498 topo_info structure. */
1500 static void
1501 topo_visit (constraint_graph_t graph, struct topo_info *ti,
1502 unsigned int n)
1504 bitmap_iterator bi;
1505 unsigned int j;
1507 SET_BIT (ti->visited, n);
1509 if (graph->succs[n])
1510 EXECUTE_IF_SET_IN_BITMAP (graph->succs[n], 0, j, bi)
1512 if (!TEST_BIT (ti->visited, j))
1513 topo_visit (graph, ti, j);
1516 VEC_safe_push (unsigned, heap, ti->topo_order, n);
1519 /* Process a constraint C that represents x = *(y + off), using DELTA as the
1520 starting solution for y. */
1522 static void
1523 do_sd_constraint (constraint_graph_t graph, constraint_t c,
1524 bitmap delta)
1526 unsigned int lhs = c->lhs.var;
1527 bool flag = false;
1528 bitmap sol = get_varinfo (lhs)->solution;
1529 unsigned int j;
1530 bitmap_iterator bi;
1531 HOST_WIDE_INT roffset = c->rhs.offset;
1533 /* Our IL does not allow this. */
1534 gcc_assert (c->lhs.offset == 0);
1536 /* If the solution of Y contains anything it is good enough to transfer
1537 this to the LHS. */
1538 if (bitmap_bit_p (delta, anything_id))
1540 flag |= bitmap_set_bit (sol, anything_id);
1541 goto done;
1544 /* If we do not know at with offset the rhs is dereferenced compute
1545 the reachability set of DELTA, conservatively assuming it is
1546 dereferenced at all valid offsets. */
1547 if (roffset == UNKNOWN_OFFSET)
1549 solution_set_expand (delta, delta);
1550 /* No further offset processing is necessary. */
1551 roffset = 0;
1554 /* For each variable j in delta (Sol(y)), add
1555 an edge in the graph from j to x, and union Sol(j) into Sol(x). */
1556 EXECUTE_IF_SET_IN_BITMAP (delta, 0, j, bi)
1558 varinfo_t v = get_varinfo (j);
1559 HOST_WIDE_INT fieldoffset = v->offset + roffset;
1560 unsigned int t;
1562 if (v->is_full_var)
1563 fieldoffset = v->offset;
1564 else if (roffset != 0)
1565 v = first_vi_for_offset (v, fieldoffset);
1566 /* If the access is outside of the variable we can ignore it. */
1567 if (!v)
1568 continue;
1572 t = find (v->id);
1574 /* Adding edges from the special vars is pointless.
1575 They don't have sets that can change. */
1576 if (get_varinfo (t)->is_special_var)
1577 flag |= bitmap_ior_into (sol, get_varinfo (t)->solution);
1578 /* Merging the solution from ESCAPED needlessly increases
1579 the set. Use ESCAPED as representative instead. */
1580 else if (v->id == escaped_id)
1581 flag |= bitmap_set_bit (sol, escaped_id);
1582 else if (add_graph_edge (graph, lhs, t))
1583 flag |= bitmap_ior_into (sol, get_varinfo (t)->solution);
1585 /* If the variable is not exactly at the requested offset
1586 we have to include the next one. */
1587 if (v->offset == (unsigned HOST_WIDE_INT)fieldoffset
1588 || v->next == NULL)
1589 break;
1591 v = v->next;
1592 fieldoffset = v->offset;
1594 while (1);
1597 done:
1598 /* If the LHS solution changed, mark the var as changed. */
1599 if (flag)
1601 get_varinfo (lhs)->solution = sol;
1602 if (!TEST_BIT (changed, lhs))
1604 SET_BIT (changed, lhs);
1605 changed_count++;
1610 /* Process a constraint C that represents *(x + off) = y using DELTA
1611 as the starting solution for x. */
1613 static void
1614 do_ds_constraint (constraint_t c, bitmap delta)
1616 unsigned int rhs = c->rhs.var;
1617 bitmap sol = get_varinfo (rhs)->solution;
1618 unsigned int j;
1619 bitmap_iterator bi;
1620 HOST_WIDE_INT loff = c->lhs.offset;
1622 /* Our IL does not allow this. */
1623 gcc_assert (c->rhs.offset == 0);
1625 /* If the solution of y contains ANYTHING simply use the ANYTHING
1626 solution. This avoids needlessly increasing the points-to sets. */
1627 if (bitmap_bit_p (sol, anything_id))
1628 sol = get_varinfo (find (anything_id))->solution;
1630 /* If the solution for x contains ANYTHING we have to merge the
1631 solution of y into all pointer variables which we do via
1632 STOREDANYTHING. */
1633 if (bitmap_bit_p (delta, anything_id))
1635 unsigned t = find (storedanything_id);
1636 if (add_graph_edge (graph, t, rhs))
1638 if (bitmap_ior_into (get_varinfo (t)->solution, sol))
1640 if (!TEST_BIT (changed, t))
1642 SET_BIT (changed, t);
1643 changed_count++;
1647 return;
1650 /* If we do not know at with offset the rhs is dereferenced compute
1651 the reachability set of DELTA, conservatively assuming it is
1652 dereferenced at all valid offsets. */
1653 if (loff == UNKNOWN_OFFSET)
1655 solution_set_expand (delta, delta);
1656 loff = 0;
1659 /* For each member j of delta (Sol(x)), add an edge from y to j and
1660 union Sol(y) into Sol(j) */
1661 EXECUTE_IF_SET_IN_BITMAP (delta, 0, j, bi)
1663 varinfo_t v = get_varinfo (j);
1664 unsigned int t;
1665 HOST_WIDE_INT fieldoffset = v->offset + loff;
1667 /* If v is a NONLOCAL then this is an escape point. */
1668 if (j == nonlocal_id)
1670 t = find (escaped_id);
1671 if (add_graph_edge (graph, t, rhs)
1672 && bitmap_ior_into (get_varinfo (t)->solution, sol)
1673 && !TEST_BIT (changed, t))
1675 SET_BIT (changed, t);
1676 changed_count++;
1680 if (v->is_special_var)
1681 continue;
1683 if (v->is_full_var)
1684 fieldoffset = v->offset;
1685 else if (loff != 0)
1686 v = first_vi_for_offset (v, fieldoffset);
1687 /* If the access is outside of the variable we can ignore it. */
1688 if (!v)
1689 continue;
1693 if (v->may_have_pointers)
1695 t = find (v->id);
1696 if (add_graph_edge (graph, t, rhs)
1697 && bitmap_ior_into (get_varinfo (t)->solution, sol)
1698 && !TEST_BIT (changed, t))
1700 SET_BIT (changed, t);
1701 changed_count++;
1704 /* If v is a global variable then this is an escape point. */
1705 if (is_global_var (v->decl))
1707 t = find (escaped_id);
1708 if (add_graph_edge (graph, t, rhs)
1709 && bitmap_ior_into (get_varinfo (t)->solution, sol)
1710 && !TEST_BIT (changed, t))
1712 SET_BIT (changed, t);
1713 changed_count++;
1717 /* If the variable is not exactly at the requested offset
1718 we have to include the next one. */
1719 if (v->offset == (unsigned HOST_WIDE_INT)fieldoffset
1720 || v->next == NULL)
1721 break;
1723 v = v->next;
1724 fieldoffset = v->offset;
1726 while (1);
1730 /* Handle a non-simple (simple meaning requires no iteration),
1731 constraint (IE *x = &y, x = *y, *x = y, and x = y with offsets involved). */
1733 static void
1734 do_complex_constraint (constraint_graph_t graph, constraint_t c, bitmap delta)
1736 if (c->lhs.type == DEREF)
1738 if (c->rhs.type == ADDRESSOF)
1740 gcc_unreachable();
1742 else
1744 /* *x = y */
1745 do_ds_constraint (c, delta);
1748 else if (c->rhs.type == DEREF)
1750 /* x = *y */
1751 if (!(get_varinfo (c->lhs.var)->is_special_var))
1752 do_sd_constraint (graph, c, delta);
1754 else
1756 bitmap tmp;
1757 bitmap solution;
1758 bool flag = false;
1760 gcc_assert (c->rhs.type == SCALAR && c->lhs.type == SCALAR);
1761 solution = get_varinfo (c->rhs.var)->solution;
1762 tmp = get_varinfo (c->lhs.var)->solution;
1764 flag = set_union_with_increment (tmp, solution, c->rhs.offset);
1766 if (flag)
1768 get_varinfo (c->lhs.var)->solution = tmp;
1769 if (!TEST_BIT (changed, c->lhs.var))
1771 SET_BIT (changed, c->lhs.var);
1772 changed_count++;
1778 /* Initialize and return a new SCC info structure. */
1780 static struct scc_info *
1781 init_scc_info (size_t size)
1783 struct scc_info *si = XNEW (struct scc_info);
1784 size_t i;
1786 si->current_index = 0;
1787 si->visited = sbitmap_alloc (size);
1788 sbitmap_zero (si->visited);
1789 si->deleted = sbitmap_alloc (size);
1790 sbitmap_zero (si->deleted);
1791 si->node_mapping = XNEWVEC (unsigned int, size);
1792 si->dfs = XCNEWVEC (unsigned int, size);
1794 for (i = 0; i < size; i++)
1795 si->node_mapping[i] = i;
1797 si->scc_stack = VEC_alloc (unsigned, heap, 1);
1798 return si;
1801 /* Free an SCC info structure pointed to by SI */
1803 static void
1804 free_scc_info (struct scc_info *si)
1806 sbitmap_free (si->visited);
1807 sbitmap_free (si->deleted);
1808 free (si->node_mapping);
1809 free (si->dfs);
1810 VEC_free (unsigned, heap, si->scc_stack);
1811 free (si);
1815 /* Find indirect cycles in GRAPH that occur, using strongly connected
1816 components, and note them in the indirect cycles map.
1818 This technique comes from Ben Hardekopf and Calvin Lin,
1819 "It Pays to be Lazy: Fast and Accurate Pointer Analysis for Millions of
1820 Lines of Code", submitted to PLDI 2007. */
1822 static void
1823 find_indirect_cycles (constraint_graph_t graph)
1825 unsigned int i;
1826 unsigned int size = graph->size;
1827 struct scc_info *si = init_scc_info (size);
1829 for (i = 0; i < MIN (LAST_REF_NODE, size); i ++ )
1830 if (!TEST_BIT (si->visited, i) && find (i) == i)
1831 scc_visit (graph, si, i);
1833 free_scc_info (si);
1836 /* Compute a topological ordering for GRAPH, and store the result in the
1837 topo_info structure TI. */
1839 static void
1840 compute_topo_order (constraint_graph_t graph,
1841 struct topo_info *ti)
1843 unsigned int i;
1844 unsigned int size = graph->size;
1846 for (i = 0; i != size; ++i)
1847 if (!TEST_BIT (ti->visited, i) && find (i) == i)
1848 topo_visit (graph, ti, i);
1851 /* Structure used to for hash value numbering of pointer equivalence
1852 classes. */
1854 typedef struct equiv_class_label
1856 hashval_t hashcode;
1857 unsigned int equivalence_class;
1858 bitmap labels;
1859 } *equiv_class_label_t;
1860 typedef const struct equiv_class_label *const_equiv_class_label_t;
1862 /* A hashtable for mapping a bitmap of labels->pointer equivalence
1863 classes. */
1864 static htab_t pointer_equiv_class_table;
1866 /* A hashtable for mapping a bitmap of labels->location equivalence
1867 classes. */
1868 static htab_t location_equiv_class_table;
1870 /* Hash function for a equiv_class_label_t */
1872 static hashval_t
1873 equiv_class_label_hash (const void *p)
1875 const_equiv_class_label_t const ecl = (const_equiv_class_label_t) p;
1876 return ecl->hashcode;
1879 /* Equality function for two equiv_class_label_t's. */
1881 static int
1882 equiv_class_label_eq (const void *p1, const void *p2)
1884 const_equiv_class_label_t const eql1 = (const_equiv_class_label_t) p1;
1885 const_equiv_class_label_t const eql2 = (const_equiv_class_label_t) p2;
1886 return (eql1->hashcode == eql2->hashcode
1887 && bitmap_equal_p (eql1->labels, eql2->labels));
1890 /* Lookup a equivalence class in TABLE by the bitmap of LABELS it
1891 contains. */
1893 static unsigned int
1894 equiv_class_lookup (htab_t table, bitmap labels)
1896 void **slot;
1897 struct equiv_class_label ecl;
1899 ecl.labels = labels;
1900 ecl.hashcode = bitmap_hash (labels);
1902 slot = htab_find_slot_with_hash (table, &ecl,
1903 ecl.hashcode, NO_INSERT);
1904 if (!slot)
1905 return 0;
1906 else
1907 return ((equiv_class_label_t) *slot)->equivalence_class;
1911 /* Add an equivalence class named EQUIVALENCE_CLASS with labels LABELS
1912 to TABLE. */
1914 static void
1915 equiv_class_add (htab_t table, unsigned int equivalence_class,
1916 bitmap labels)
1918 void **slot;
1919 equiv_class_label_t ecl = XNEW (struct equiv_class_label);
1921 ecl->labels = labels;
1922 ecl->equivalence_class = equivalence_class;
1923 ecl->hashcode = bitmap_hash (labels);
1925 slot = htab_find_slot_with_hash (table, ecl,
1926 ecl->hashcode, INSERT);
1927 gcc_assert (!*slot);
1928 *slot = (void *) ecl;
1931 /* Perform offline variable substitution.
1933 This is a worst case quadratic time way of identifying variables
1934 that must have equivalent points-to sets, including those caused by
1935 static cycles, and single entry subgraphs, in the constraint graph.
1937 The technique is described in "Exploiting Pointer and Location
1938 Equivalence to Optimize Pointer Analysis. In the 14th International
1939 Static Analysis Symposium (SAS), August 2007." It is known as the
1940 "HU" algorithm, and is equivalent to value numbering the collapsed
1941 constraint graph including evaluating unions.
1943 The general method of finding equivalence classes is as follows:
1944 Add fake nodes (REF nodes) and edges for *a = b and a = *b constraints.
1945 Initialize all non-REF nodes to be direct nodes.
1946 For each constraint a = a U {b}, we set pts(a) = pts(a) u {fresh
1947 variable}
1948 For each constraint containing the dereference, we also do the same
1949 thing.
1951 We then compute SCC's in the graph and unify nodes in the same SCC,
1952 including pts sets.
1954 For each non-collapsed node x:
1955 Visit all unvisited explicit incoming edges.
1956 Ignoring all non-pointers, set pts(x) = Union of pts(a) for y
1957 where y->x.
1958 Lookup the equivalence class for pts(x).
1959 If we found one, equivalence_class(x) = found class.
1960 Otherwise, equivalence_class(x) = new class, and new_class is
1961 added to the lookup table.
1963 All direct nodes with the same equivalence class can be replaced
1964 with a single representative node.
1965 All unlabeled nodes (label == 0) are not pointers and all edges
1966 involving them can be eliminated.
1967 We perform these optimizations during rewrite_constraints
1969 In addition to pointer equivalence class finding, we also perform
1970 location equivalence class finding. This is the set of variables
1971 that always appear together in points-to sets. We use this to
1972 compress the size of the points-to sets. */
1974 /* Current maximum pointer equivalence class id. */
1975 static int pointer_equiv_class;
1977 /* Current maximum location equivalence class id. */
1978 static int location_equiv_class;
1980 /* Recursive routine to find strongly connected components in GRAPH,
1981 and label it's nodes with DFS numbers. */
1983 static void
1984 condense_visit (constraint_graph_t graph, struct scc_info *si, unsigned int n)
1986 unsigned int i;
1987 bitmap_iterator bi;
1988 unsigned int my_dfs;
1990 gcc_assert (si->node_mapping[n] == n);
1991 SET_BIT (si->visited, n);
1992 si->dfs[n] = si->current_index ++;
1993 my_dfs = si->dfs[n];
1995 /* Visit all the successors. */
1996 EXECUTE_IF_IN_NONNULL_BITMAP (graph->preds[n], 0, i, bi)
1998 unsigned int w = si->node_mapping[i];
2000 if (TEST_BIT (si->deleted, w))
2001 continue;
2003 if (!TEST_BIT (si->visited, w))
2004 condense_visit (graph, si, w);
2006 unsigned int t = si->node_mapping[w];
2007 unsigned int nnode = si->node_mapping[n];
2008 gcc_assert (nnode == n);
2010 if (si->dfs[t] < si->dfs[nnode])
2011 si->dfs[n] = si->dfs[t];
2015 /* Visit all the implicit predecessors. */
2016 EXECUTE_IF_IN_NONNULL_BITMAP (graph->implicit_preds[n], 0, i, bi)
2018 unsigned int w = si->node_mapping[i];
2020 if (TEST_BIT (si->deleted, w))
2021 continue;
2023 if (!TEST_BIT (si->visited, w))
2024 condense_visit (graph, si, w);
2026 unsigned int t = si->node_mapping[w];
2027 unsigned int nnode = si->node_mapping[n];
2028 gcc_assert (nnode == n);
2030 if (si->dfs[t] < si->dfs[nnode])
2031 si->dfs[n] = si->dfs[t];
2035 /* See if any components have been identified. */
2036 if (si->dfs[n] == my_dfs)
2038 while (VEC_length (unsigned, si->scc_stack) != 0
2039 && si->dfs[VEC_last (unsigned, si->scc_stack)] >= my_dfs)
2041 unsigned int w = VEC_pop (unsigned, si->scc_stack);
2042 si->node_mapping[w] = n;
2044 if (!TEST_BIT (graph->direct_nodes, w))
2045 RESET_BIT (graph->direct_nodes, n);
2047 /* Unify our nodes. */
2048 if (graph->preds[w])
2050 if (!graph->preds[n])
2051 graph->preds[n] = BITMAP_ALLOC (&predbitmap_obstack);
2052 bitmap_ior_into (graph->preds[n], graph->preds[w]);
2054 if (graph->implicit_preds[w])
2056 if (!graph->implicit_preds[n])
2057 graph->implicit_preds[n] = BITMAP_ALLOC (&predbitmap_obstack);
2058 bitmap_ior_into (graph->implicit_preds[n],
2059 graph->implicit_preds[w]);
2061 if (graph->points_to[w])
2063 if (!graph->points_to[n])
2064 graph->points_to[n] = BITMAP_ALLOC (&predbitmap_obstack);
2065 bitmap_ior_into (graph->points_to[n],
2066 graph->points_to[w]);
2069 SET_BIT (si->deleted, n);
2071 else
2072 VEC_safe_push (unsigned, heap, si->scc_stack, n);
2075 /* Label pointer equivalences. */
2077 static void
2078 label_visit (constraint_graph_t graph, struct scc_info *si, unsigned int n)
2080 unsigned int i;
2081 bitmap_iterator bi;
2082 SET_BIT (si->visited, n);
2084 if (!graph->points_to[n])
2085 graph->points_to[n] = BITMAP_ALLOC (&predbitmap_obstack);
2087 /* Label and union our incoming edges's points to sets. */
2088 EXECUTE_IF_IN_NONNULL_BITMAP (graph->preds[n], 0, i, bi)
2090 unsigned int w = si->node_mapping[i];
2091 if (!TEST_BIT (si->visited, w))
2092 label_visit (graph, si, w);
2094 /* Skip unused edges */
2095 if (w == n || graph->pointer_label[w] == 0)
2096 continue;
2098 if (graph->points_to[w])
2099 bitmap_ior_into(graph->points_to[n], graph->points_to[w]);
2101 /* Indirect nodes get fresh variables. */
2102 if (!TEST_BIT (graph->direct_nodes, n))
2103 bitmap_set_bit (graph->points_to[n], FIRST_REF_NODE + n);
2105 if (!bitmap_empty_p (graph->points_to[n]))
2107 unsigned int label = equiv_class_lookup (pointer_equiv_class_table,
2108 graph->points_to[n]);
2109 if (!label)
2111 label = pointer_equiv_class++;
2112 equiv_class_add (pointer_equiv_class_table,
2113 label, graph->points_to[n]);
2115 graph->pointer_label[n] = label;
2119 /* Perform offline variable substitution, discovering equivalence
2120 classes, and eliminating non-pointer variables. */
2122 static struct scc_info *
2123 perform_var_substitution (constraint_graph_t graph)
2125 unsigned int i;
2126 unsigned int size = graph->size;
2127 struct scc_info *si = init_scc_info (size);
2129 bitmap_obstack_initialize (&iteration_obstack);
2130 pointer_equiv_class_table = htab_create (511, equiv_class_label_hash,
2131 equiv_class_label_eq, free);
2132 location_equiv_class_table = htab_create (511, equiv_class_label_hash,
2133 equiv_class_label_eq, free);
2134 pointer_equiv_class = 1;
2135 location_equiv_class = 1;
2137 /* Condense the nodes, which means to find SCC's, count incoming
2138 predecessors, and unite nodes in SCC's. */
2139 for (i = 0; i < FIRST_REF_NODE; i++)
2140 if (!TEST_BIT (si->visited, si->node_mapping[i]))
2141 condense_visit (graph, si, si->node_mapping[i]);
2143 sbitmap_zero (si->visited);
2144 /* Actually the label the nodes for pointer equivalences */
2145 for (i = 0; i < FIRST_REF_NODE; i++)
2146 if (!TEST_BIT (si->visited, si->node_mapping[i]))
2147 label_visit (graph, si, si->node_mapping[i]);
2149 /* Calculate location equivalence labels. */
2150 for (i = 0; i < FIRST_REF_NODE; i++)
2152 bitmap pointed_by;
2153 bitmap_iterator bi;
2154 unsigned int j;
2155 unsigned int label;
2157 if (!graph->pointed_by[i])
2158 continue;
2159 pointed_by = BITMAP_ALLOC (&iteration_obstack);
2161 /* Translate the pointed-by mapping for pointer equivalence
2162 labels. */
2163 EXECUTE_IF_SET_IN_BITMAP (graph->pointed_by[i], 0, j, bi)
2165 bitmap_set_bit (pointed_by,
2166 graph->pointer_label[si->node_mapping[j]]);
2168 /* The original pointed_by is now dead. */
2169 BITMAP_FREE (graph->pointed_by[i]);
2171 /* Look up the location equivalence label if one exists, or make
2172 one otherwise. */
2173 label = equiv_class_lookup (location_equiv_class_table,
2174 pointed_by);
2175 if (label == 0)
2177 label = location_equiv_class++;
2178 equiv_class_add (location_equiv_class_table,
2179 label, pointed_by);
2181 else
2183 if (dump_file && (dump_flags & TDF_DETAILS))
2184 fprintf (dump_file, "Found location equivalence for node %s\n",
2185 get_varinfo (i)->name);
2186 BITMAP_FREE (pointed_by);
2188 graph->loc_label[i] = label;
2192 if (dump_file && (dump_flags & TDF_DETAILS))
2193 for (i = 0; i < FIRST_REF_NODE; i++)
2195 bool direct_node = TEST_BIT (graph->direct_nodes, i);
2196 fprintf (dump_file,
2197 "Equivalence classes for %s node id %d:%s are pointer: %d"
2198 ", location:%d\n",
2199 direct_node ? "Direct node" : "Indirect node", i,
2200 get_varinfo (i)->name,
2201 graph->pointer_label[si->node_mapping[i]],
2202 graph->loc_label[si->node_mapping[i]]);
2205 /* Quickly eliminate our non-pointer variables. */
2207 for (i = 0; i < FIRST_REF_NODE; i++)
2209 unsigned int node = si->node_mapping[i];
2211 if (graph->pointer_label[node] == 0)
2213 if (dump_file && (dump_flags & TDF_DETAILS))
2214 fprintf (dump_file,
2215 "%s is a non-pointer variable, eliminating edges.\n",
2216 get_varinfo (node)->name);
2217 stats.nonpointer_vars++;
2218 clear_edges_for_node (graph, node);
2222 return si;
2225 /* Free information that was only necessary for variable
2226 substitution. */
2228 static void
2229 free_var_substitution_info (struct scc_info *si)
2231 free_scc_info (si);
2232 free (graph->pointer_label);
2233 free (graph->loc_label);
2234 free (graph->pointed_by);
2235 free (graph->points_to);
2236 free (graph->eq_rep);
2237 sbitmap_free (graph->direct_nodes);
2238 htab_delete (pointer_equiv_class_table);
2239 htab_delete (location_equiv_class_table);
2240 bitmap_obstack_release (&iteration_obstack);
2243 /* Return an existing node that is equivalent to NODE, which has
2244 equivalence class LABEL, if one exists. Return NODE otherwise. */
2246 static unsigned int
2247 find_equivalent_node (constraint_graph_t graph,
2248 unsigned int node, unsigned int label)
2250 /* If the address version of this variable is unused, we can
2251 substitute it for anything else with the same label.
2252 Otherwise, we know the pointers are equivalent, but not the
2253 locations, and we can unite them later. */
2255 if (!bitmap_bit_p (graph->address_taken, node))
2257 gcc_assert (label < graph->size);
2259 if (graph->eq_rep[label] != -1)
2261 /* Unify the two variables since we know they are equivalent. */
2262 if (unite (graph->eq_rep[label], node))
2263 unify_nodes (graph, graph->eq_rep[label], node, false);
2264 return graph->eq_rep[label];
2266 else
2268 graph->eq_rep[label] = node;
2269 graph->pe_rep[label] = node;
2272 else
2274 gcc_assert (label < graph->size);
2275 graph->pe[node] = label;
2276 if (graph->pe_rep[label] == -1)
2277 graph->pe_rep[label] = node;
2280 return node;
2283 /* Unite pointer equivalent but not location equivalent nodes in
2284 GRAPH. This may only be performed once variable substitution is
2285 finished. */
2287 static void
2288 unite_pointer_equivalences (constraint_graph_t graph)
2290 unsigned int i;
2292 /* Go through the pointer equivalences and unite them to their
2293 representative, if they aren't already. */
2294 for (i = 0; i < FIRST_REF_NODE; i++)
2296 unsigned int label = graph->pe[i];
2297 if (label)
2299 int label_rep = graph->pe_rep[label];
2301 if (label_rep == -1)
2302 continue;
2304 label_rep = find (label_rep);
2305 if (label_rep >= 0 && unite (label_rep, find (i)))
2306 unify_nodes (graph, label_rep, i, false);
2311 /* Move complex constraints to the GRAPH nodes they belong to. */
2313 static void
2314 move_complex_constraints (constraint_graph_t graph)
2316 int i;
2317 constraint_t c;
2319 for (i = 0; VEC_iterate (constraint_t, constraints, i, c); i++)
2321 if (c)
2323 struct constraint_expr lhs = c->lhs;
2324 struct constraint_expr rhs = c->rhs;
2326 if (lhs.type == DEREF)
2328 insert_into_complex (graph, lhs.var, c);
2330 else if (rhs.type == DEREF)
2332 if (!(get_varinfo (lhs.var)->is_special_var))
2333 insert_into_complex (graph, rhs.var, c);
2335 else if (rhs.type != ADDRESSOF && lhs.var > anything_id
2336 && (lhs.offset != 0 || rhs.offset != 0))
2338 insert_into_complex (graph, rhs.var, c);
2345 /* Optimize and rewrite complex constraints while performing
2346 collapsing of equivalent nodes. SI is the SCC_INFO that is the
2347 result of perform_variable_substitution. */
2349 static void
2350 rewrite_constraints (constraint_graph_t graph,
2351 struct scc_info *si)
2353 int i;
2354 unsigned int j;
2355 constraint_t c;
2357 for (j = 0; j < graph->size; j++)
2358 gcc_assert (find (j) == j);
2360 for (i = 0; VEC_iterate (constraint_t, constraints, i, c); i++)
2362 struct constraint_expr lhs = c->lhs;
2363 struct constraint_expr rhs = c->rhs;
2364 unsigned int lhsvar = find (lhs.var);
2365 unsigned int rhsvar = find (rhs.var);
2366 unsigned int lhsnode, rhsnode;
2367 unsigned int lhslabel, rhslabel;
2369 lhsnode = si->node_mapping[lhsvar];
2370 rhsnode = si->node_mapping[rhsvar];
2371 lhslabel = graph->pointer_label[lhsnode];
2372 rhslabel = graph->pointer_label[rhsnode];
2374 /* See if it is really a non-pointer variable, and if so, ignore
2375 the constraint. */
2376 if (lhslabel == 0)
2378 if (dump_file && (dump_flags & TDF_DETAILS))
2381 fprintf (dump_file, "%s is a non-pointer variable,"
2382 "ignoring constraint:",
2383 get_varinfo (lhs.var)->name);
2384 dump_constraint (dump_file, c);
2386 VEC_replace (constraint_t, constraints, i, NULL);
2387 continue;
2390 if (rhslabel == 0)
2392 if (dump_file && (dump_flags & TDF_DETAILS))
2395 fprintf (dump_file, "%s is a non-pointer variable,"
2396 "ignoring constraint:",
2397 get_varinfo (rhs.var)->name);
2398 dump_constraint (dump_file, c);
2400 VEC_replace (constraint_t, constraints, i, NULL);
2401 continue;
2404 lhsvar = find_equivalent_node (graph, lhsvar, lhslabel);
2405 rhsvar = find_equivalent_node (graph, rhsvar, rhslabel);
2406 c->lhs.var = lhsvar;
2407 c->rhs.var = rhsvar;
2412 /* Eliminate indirect cycles involving NODE. Return true if NODE was
2413 part of an SCC, false otherwise. */
2415 static bool
2416 eliminate_indirect_cycles (unsigned int node)
2418 if (graph->indirect_cycles[node] != -1
2419 && !bitmap_empty_p (get_varinfo (node)->solution))
2421 unsigned int i;
2422 VEC(unsigned,heap) *queue = NULL;
2423 int queuepos;
2424 unsigned int to = find (graph->indirect_cycles[node]);
2425 bitmap_iterator bi;
2427 /* We can't touch the solution set and call unify_nodes
2428 at the same time, because unify_nodes is going to do
2429 bitmap unions into it. */
2431 EXECUTE_IF_SET_IN_BITMAP (get_varinfo (node)->solution, 0, i, bi)
2433 if (find (i) == i && i != to)
2435 if (unite (to, i))
2436 VEC_safe_push (unsigned, heap, queue, i);
2440 for (queuepos = 0;
2441 VEC_iterate (unsigned, queue, queuepos, i);
2442 queuepos++)
2444 unify_nodes (graph, to, i, true);
2446 VEC_free (unsigned, heap, queue);
2447 return true;
2449 return false;
2452 /* Solve the constraint graph GRAPH using our worklist solver.
2453 This is based on the PW* family of solvers from the "Efficient Field
2454 Sensitive Pointer Analysis for C" paper.
2455 It works by iterating over all the graph nodes, processing the complex
2456 constraints and propagating the copy constraints, until everything stops
2457 changed. This corresponds to steps 6-8 in the solving list given above. */
2459 static void
2460 solve_graph (constraint_graph_t graph)
2462 unsigned int size = graph->size;
2463 unsigned int i;
2464 bitmap pts;
2466 changed_count = 0;
2467 changed = sbitmap_alloc (size);
2468 sbitmap_zero (changed);
2470 /* Mark all initial non-collapsed nodes as changed. */
2471 for (i = 0; i < size; i++)
2473 varinfo_t ivi = get_varinfo (i);
2474 if (find (i) == i && !bitmap_empty_p (ivi->solution)
2475 && ((graph->succs[i] && !bitmap_empty_p (graph->succs[i]))
2476 || VEC_length (constraint_t, graph->complex[i]) > 0))
2478 SET_BIT (changed, i);
2479 changed_count++;
2483 /* Allocate a bitmap to be used to store the changed bits. */
2484 pts = BITMAP_ALLOC (&pta_obstack);
2486 while (changed_count > 0)
2488 unsigned int i;
2489 struct topo_info *ti = init_topo_info ();
2490 stats.iterations++;
2492 bitmap_obstack_initialize (&iteration_obstack);
2494 compute_topo_order (graph, ti);
2496 while (VEC_length (unsigned, ti->topo_order) != 0)
2499 i = VEC_pop (unsigned, ti->topo_order);
2501 /* If this variable is not a representative, skip it. */
2502 if (find (i) != i)
2503 continue;
2505 /* In certain indirect cycle cases, we may merge this
2506 variable to another. */
2507 if (eliminate_indirect_cycles (i) && find (i) != i)
2508 continue;
2510 /* If the node has changed, we need to process the
2511 complex constraints and outgoing edges again. */
2512 if (TEST_BIT (changed, i))
2514 unsigned int j;
2515 constraint_t c;
2516 bitmap solution;
2517 VEC(constraint_t,heap) *complex = graph->complex[i];
2518 bool solution_empty;
2520 RESET_BIT (changed, i);
2521 changed_count--;
2523 /* Compute the changed set of solution bits. */
2524 bitmap_and_compl (pts, get_varinfo (i)->solution,
2525 get_varinfo (i)->oldsolution);
2527 if (bitmap_empty_p (pts))
2528 continue;
2530 bitmap_ior_into (get_varinfo (i)->oldsolution, pts);
2532 solution = get_varinfo (i)->solution;
2533 solution_empty = bitmap_empty_p (solution);
2535 /* Process the complex constraints */
2536 for (j = 0; VEC_iterate (constraint_t, complex, j, c); j++)
2538 /* XXX: This is going to unsort the constraints in
2539 some cases, which will occasionally add duplicate
2540 constraints during unification. This does not
2541 affect correctness. */
2542 c->lhs.var = find (c->lhs.var);
2543 c->rhs.var = find (c->rhs.var);
2545 /* The only complex constraint that can change our
2546 solution to non-empty, given an empty solution,
2547 is a constraint where the lhs side is receiving
2548 some set from elsewhere. */
2549 if (!solution_empty || c->lhs.type != DEREF)
2550 do_complex_constraint (graph, c, pts);
2553 solution_empty = bitmap_empty_p (solution);
2555 if (!solution_empty)
2557 bitmap_iterator bi;
2558 unsigned eff_escaped_id = find (escaped_id);
2560 /* Propagate solution to all successors. */
2561 EXECUTE_IF_IN_NONNULL_BITMAP (graph->succs[i],
2562 0, j, bi)
2564 bitmap tmp;
2565 bool flag;
2567 unsigned int to = find (j);
2568 tmp = get_varinfo (to)->solution;
2569 flag = false;
2571 /* Don't try to propagate to ourselves. */
2572 if (to == i)
2573 continue;
2575 /* If we propagate from ESCAPED use ESCAPED as
2576 placeholder. */
2577 if (i == eff_escaped_id)
2578 flag = bitmap_set_bit (tmp, escaped_id);
2579 else
2580 flag = set_union_with_increment (tmp, pts, 0);
2582 if (flag)
2584 get_varinfo (to)->solution = tmp;
2585 if (!TEST_BIT (changed, to))
2587 SET_BIT (changed, to);
2588 changed_count++;
2595 free_topo_info (ti);
2596 bitmap_obstack_release (&iteration_obstack);
2599 BITMAP_FREE (pts);
2600 sbitmap_free (changed);
2601 bitmap_obstack_release (&oldpta_obstack);
2604 /* Map from trees to variable infos. */
2605 static struct pointer_map_t *vi_for_tree;
2608 /* Insert ID as the variable id for tree T in the vi_for_tree map. */
2610 static void
2611 insert_vi_for_tree (tree t, varinfo_t vi)
2613 void **slot = pointer_map_insert (vi_for_tree, t);
2614 gcc_assert (vi);
2615 gcc_assert (*slot == NULL);
2616 *slot = vi;
2619 /* Find the variable info for tree T in VI_FOR_TREE. If T does not
2620 exist in the map, return NULL, otherwise, return the varinfo we found. */
2622 static varinfo_t
2623 lookup_vi_for_tree (tree t)
2625 void **slot = pointer_map_contains (vi_for_tree, t);
2626 if (slot == NULL)
2627 return NULL;
2629 return (varinfo_t) *slot;
2632 /* Return a printable name for DECL */
2634 static const char *
2635 alias_get_name (tree decl)
2637 const char *res = get_name (decl);
2638 char *temp;
2639 int num_printed = 0;
2641 if (res != NULL)
2642 return res;
2644 res = "NULL";
2645 if (!dump_file)
2646 return res;
2648 if (TREE_CODE (decl) == SSA_NAME)
2650 num_printed = asprintf (&temp, "%s_%u",
2651 alias_get_name (SSA_NAME_VAR (decl)),
2652 SSA_NAME_VERSION (decl));
2654 else if (DECL_P (decl))
2656 num_printed = asprintf (&temp, "D.%u", DECL_UID (decl));
2658 if (num_printed > 0)
2660 res = ggc_strdup (temp);
2661 free (temp);
2663 return res;
2666 /* Find the variable id for tree T in the map.
2667 If T doesn't exist in the map, create an entry for it and return it. */
2669 static varinfo_t
2670 get_vi_for_tree (tree t)
2672 void **slot = pointer_map_contains (vi_for_tree, t);
2673 if (slot == NULL)
2674 return get_varinfo (create_variable_info_for (t, alias_get_name (t)));
2676 return (varinfo_t) *slot;
2679 /* Get a constraint expression for a new temporary variable. */
2681 static struct constraint_expr
2682 get_constraint_exp_for_temp (tree t)
2684 struct constraint_expr cexpr;
2686 gcc_assert (SSA_VAR_P (t));
2688 cexpr.type = SCALAR;
2689 cexpr.var = get_vi_for_tree (t)->id;
2690 cexpr.offset = 0;
2692 return cexpr;
2695 /* Get a constraint expression vector from an SSA_VAR_P node.
2696 If address_p is true, the result will be taken its address of. */
2698 static void
2699 get_constraint_for_ssa_var (tree t, VEC(ce_s, heap) **results, bool address_p)
2701 struct constraint_expr cexpr;
2702 varinfo_t vi;
2704 /* We allow FUNCTION_DECLs here even though it doesn't make much sense. */
2705 gcc_assert (SSA_VAR_P (t) || DECL_P (t));
2707 /* For parameters, get at the points-to set for the actual parm
2708 decl. */
2709 if (TREE_CODE (t) == SSA_NAME
2710 && TREE_CODE (SSA_NAME_VAR (t)) == PARM_DECL
2711 && SSA_NAME_IS_DEFAULT_DEF (t))
2713 get_constraint_for_ssa_var (SSA_NAME_VAR (t), results, address_p);
2714 return;
2717 vi = get_vi_for_tree (t);
2718 cexpr.var = vi->id;
2719 cexpr.type = SCALAR;
2720 cexpr.offset = 0;
2721 /* If we determine the result is "anything", and we know this is readonly,
2722 say it points to readonly memory instead. */
2723 if (cexpr.var == anything_id && TREE_READONLY (t))
2725 gcc_unreachable ();
2726 cexpr.type = ADDRESSOF;
2727 cexpr.var = readonly_id;
2730 /* If we are not taking the address of the constraint expr, add all
2731 sub-fiels of the variable as well. */
2732 if (!address_p)
2734 for (; vi; vi = vi->next)
2736 cexpr.var = vi->id;
2737 VEC_safe_push (ce_s, heap, *results, &cexpr);
2739 return;
2742 VEC_safe_push (ce_s, heap, *results, &cexpr);
2745 /* Process constraint T, performing various simplifications and then
2746 adding it to our list of overall constraints. */
2748 static void
2749 process_constraint (constraint_t t)
2751 struct constraint_expr rhs = t->rhs;
2752 struct constraint_expr lhs = t->lhs;
2754 gcc_assert (rhs.var < VEC_length (varinfo_t, varmap));
2755 gcc_assert (lhs.var < VEC_length (varinfo_t, varmap));
2757 /* If we didn't get any useful constraint from the lhs we get
2758 &ANYTHING as fallback from get_constraint_for. Deal with
2759 it here by turning it into *ANYTHING. */
2760 if (lhs.type == ADDRESSOF
2761 && lhs.var == anything_id)
2762 lhs.type = DEREF;
2764 /* ADDRESSOF on the lhs is invalid. */
2765 gcc_assert (lhs.type != ADDRESSOF);
2767 /* This can happen in our IR with things like n->a = *p */
2768 if (rhs.type == DEREF && lhs.type == DEREF && rhs.var != anything_id)
2770 /* Split into tmp = *rhs, *lhs = tmp */
2771 tree rhsdecl = get_varinfo (rhs.var)->decl;
2772 tree pointertype = TREE_TYPE (rhsdecl);
2773 tree pointedtotype = TREE_TYPE (pointertype);
2774 tree tmpvar = create_tmp_var_raw (pointedtotype, "doubledereftmp");
2775 struct constraint_expr tmplhs = get_constraint_exp_for_temp (tmpvar);
2777 process_constraint (new_constraint (tmplhs, rhs));
2778 process_constraint (new_constraint (lhs, tmplhs));
2780 else if (rhs.type == ADDRESSOF && lhs.type == DEREF)
2782 /* Split into tmp = &rhs, *lhs = tmp */
2783 tree rhsdecl = get_varinfo (rhs.var)->decl;
2784 tree pointertype = TREE_TYPE (rhsdecl);
2785 tree tmpvar = create_tmp_var_raw (pointertype, "derefaddrtmp");
2786 struct constraint_expr tmplhs = get_constraint_exp_for_temp (tmpvar);
2788 process_constraint (new_constraint (tmplhs, rhs));
2789 process_constraint (new_constraint (lhs, tmplhs));
2791 else
2793 gcc_assert (rhs.type != ADDRESSOF || rhs.offset == 0);
2794 VEC_safe_push (constraint_t, heap, constraints, t);
2798 /* Return true if T is a type that could contain pointers. */
2800 static bool
2801 type_could_have_pointers (tree type)
2803 if (POINTER_TYPE_P (type))
2804 return true;
2806 if (TREE_CODE (type) == ARRAY_TYPE)
2807 return type_could_have_pointers (TREE_TYPE (type));
2809 return AGGREGATE_TYPE_P (type);
2812 /* Return true if T is a variable of a type that could contain
2813 pointers. */
2815 static bool
2816 could_have_pointers (tree t)
2818 return type_could_have_pointers (TREE_TYPE (t));
2821 /* Return the position, in bits, of FIELD_DECL from the beginning of its
2822 structure. */
2824 static HOST_WIDE_INT
2825 bitpos_of_field (const tree fdecl)
2828 if (!host_integerp (DECL_FIELD_OFFSET (fdecl), 0)
2829 || !host_integerp (DECL_FIELD_BIT_OFFSET (fdecl), 0))
2830 return -1;
2832 return (TREE_INT_CST_LOW (DECL_FIELD_OFFSET (fdecl)) * 8
2833 + TREE_INT_CST_LOW (DECL_FIELD_BIT_OFFSET (fdecl)));
2837 /* Get constraint expressions for offsetting PTR by OFFSET. Stores the
2838 resulting constraint expressions in *RESULTS. */
2840 static void
2841 get_constraint_for_ptr_offset (tree ptr, tree offset,
2842 VEC (ce_s, heap) **results)
2844 struct constraint_expr *c;
2845 unsigned int j, n;
2846 HOST_WIDE_INT rhsunitoffset, rhsoffset;
2848 /* If we do not do field-sensitive PTA adding offsets to pointers
2849 does not change the points-to solution. */
2850 if (!use_field_sensitive)
2852 get_constraint_for (ptr, results);
2853 return;
2856 /* If the offset is not a non-negative integer constant that fits
2857 in a HOST_WIDE_INT, we have to fall back to a conservative
2858 solution which includes all sub-fields of all pointed-to
2859 variables of ptr. */
2860 if (!host_integerp (offset, 0))
2861 rhsoffset = UNKNOWN_OFFSET;
2862 else
2864 /* Make sure the bit-offset also fits. */
2865 rhsunitoffset = TREE_INT_CST_LOW (offset);
2866 rhsoffset = rhsunitoffset * BITS_PER_UNIT;
2867 if (rhsunitoffset != rhsoffset / BITS_PER_UNIT)
2868 rhsoffset = UNKNOWN_OFFSET;
2871 get_constraint_for (ptr, results);
2872 if (rhsoffset == 0)
2873 return;
2875 /* As we are eventually appending to the solution do not use
2876 VEC_iterate here. */
2877 n = VEC_length (ce_s, *results);
2878 for (j = 0; j < n; j++)
2880 varinfo_t curr;
2881 c = VEC_index (ce_s, *results, j);
2882 curr = get_varinfo (c->var);
2884 if (c->type == ADDRESSOF
2885 /* If this varinfo represents a full variable just use it. */
2886 && curr->is_full_var)
2887 c->offset = 0;
2888 else if (c->type == ADDRESSOF
2889 /* If we do not know the offset add all subfields. */
2890 && rhsoffset == UNKNOWN_OFFSET)
2892 varinfo_t temp = lookup_vi_for_tree (curr->decl);
2895 struct constraint_expr c2;
2896 c2.var = temp->id;
2897 c2.type = ADDRESSOF;
2898 c2.offset = 0;
2899 VEC_safe_push (ce_s, heap, *results, &c2);
2900 temp = temp->next;
2902 while (temp);
2904 else if (c->type == ADDRESSOF)
2906 varinfo_t temp;
2907 unsigned HOST_WIDE_INT offset = curr->offset + rhsoffset;
2909 /* Search the sub-field which overlaps with the
2910 pointed-to offset. If the result is outside of the variable
2911 we have to provide a conservative result, as the variable is
2912 still reachable from the resulting pointer (even though it
2913 technically cannot point to anything). The last and first
2914 sub-fields are such conservative results.
2915 ??? If we always had a sub-field for &object + 1 then
2916 we could represent this in a more precise way. */
2917 if (rhsoffset < 0
2918 && curr->offset < offset)
2919 offset = 0;
2920 temp = first_or_preceding_vi_for_offset (curr, offset);
2922 /* If the found variable is not exactly at the pointed to
2923 result, we have to include the next variable in the
2924 solution as well. Otherwise two increments by offset / 2
2925 do not result in the same or a conservative superset
2926 solution. */
2927 if (temp->offset != offset
2928 && temp->next != NULL)
2930 struct constraint_expr c2;
2931 c2.var = temp->next->id;
2932 c2.type = ADDRESSOF;
2933 c2.offset = 0;
2934 VEC_safe_push (ce_s, heap, *results, &c2);
2936 c->var = temp->id;
2937 c->offset = 0;
2939 else
2940 c->offset = rhsoffset;
2945 /* Given a COMPONENT_REF T, return the constraint_expr vector for it.
2946 If address_p is true the result will be taken its address of. */
2948 static void
2949 get_constraint_for_component_ref (tree t, VEC(ce_s, heap) **results,
2950 bool address_p)
2952 tree orig_t = t;
2953 HOST_WIDE_INT bitsize = -1;
2954 HOST_WIDE_INT bitmaxsize = -1;
2955 HOST_WIDE_INT bitpos;
2956 tree forzero;
2957 struct constraint_expr *result;
2959 /* Some people like to do cute things like take the address of
2960 &0->a.b */
2961 forzero = t;
2962 while (!SSA_VAR_P (forzero) && !CONSTANT_CLASS_P (forzero))
2963 forzero = TREE_OPERAND (forzero, 0);
2965 if (CONSTANT_CLASS_P (forzero) && integer_zerop (forzero))
2967 struct constraint_expr temp;
2969 temp.offset = 0;
2970 temp.var = integer_id;
2971 temp.type = SCALAR;
2972 VEC_safe_push (ce_s, heap, *results, &temp);
2973 return;
2976 t = get_ref_base_and_extent (t, &bitpos, &bitsize, &bitmaxsize);
2978 /* Pretend to take the address of the base, we'll take care of
2979 adding the required subset of sub-fields below. */
2980 get_constraint_for_1 (t, results, true);
2981 gcc_assert (VEC_length (ce_s, *results) == 1);
2982 result = VEC_last (ce_s, *results);
2984 if (result->type == SCALAR
2985 && get_varinfo (result->var)->is_full_var)
2986 /* For single-field vars do not bother about the offset. */
2987 result->offset = 0;
2988 else if (result->type == SCALAR)
2990 /* In languages like C, you can access one past the end of an
2991 array. You aren't allowed to dereference it, so we can
2992 ignore this constraint. When we handle pointer subtraction,
2993 we may have to do something cute here. */
2995 if ((unsigned HOST_WIDE_INT)bitpos < get_varinfo (result->var)->fullsize
2996 && bitmaxsize != 0)
2998 /* It's also not true that the constraint will actually start at the
2999 right offset, it may start in some padding. We only care about
3000 setting the constraint to the first actual field it touches, so
3001 walk to find it. */
3002 struct constraint_expr cexpr = *result;
3003 varinfo_t curr;
3004 VEC_pop (ce_s, *results);
3005 cexpr.offset = 0;
3006 for (curr = get_varinfo (cexpr.var); curr; curr = curr->next)
3008 if (ranges_overlap_p (curr->offset, curr->size,
3009 bitpos, bitmaxsize))
3011 cexpr.var = curr->id;
3012 VEC_safe_push (ce_s, heap, *results, &cexpr);
3013 if (address_p)
3014 break;
3017 /* If we are going to take the address of this field then
3018 to be able to compute reachability correctly add at least
3019 the last field of the variable. */
3020 if (address_p
3021 && VEC_length (ce_s, *results) == 0)
3023 curr = get_varinfo (cexpr.var);
3024 while (curr->next != NULL)
3025 curr = curr->next;
3026 cexpr.var = curr->id;
3027 VEC_safe_push (ce_s, heap, *results, &cexpr);
3029 else
3030 /* Assert that we found *some* field there. The user couldn't be
3031 accessing *only* padding. */
3032 /* Still the user could access one past the end of an array
3033 embedded in a struct resulting in accessing *only* padding. */
3034 gcc_assert (VEC_length (ce_s, *results) >= 1
3035 || ref_contains_array_ref (orig_t));
3037 else if (bitmaxsize == 0)
3039 if (dump_file && (dump_flags & TDF_DETAILS))
3040 fprintf (dump_file, "Access to zero-sized part of variable,"
3041 "ignoring\n");
3043 else
3044 if (dump_file && (dump_flags & TDF_DETAILS))
3045 fprintf (dump_file, "Access to past the end of variable, ignoring\n");
3047 else if (result->type == DEREF)
3049 /* If we do not know exactly where the access goes say so. Note
3050 that only for non-structure accesses we know that we access
3051 at most one subfiled of any variable. */
3052 if (bitpos == -1
3053 || bitsize != bitmaxsize
3054 || AGGREGATE_TYPE_P (TREE_TYPE (orig_t)))
3055 result->offset = UNKNOWN_OFFSET;
3056 else
3057 result->offset = bitpos;
3059 else if (result->type == ADDRESSOF)
3061 /* We can end up here for component references on a
3062 VIEW_CONVERT_EXPR <>(&foobar). */
3063 result->type = SCALAR;
3064 result->var = anything_id;
3065 result->offset = 0;
3067 else
3068 gcc_unreachable ();
3072 /* Dereference the constraint expression CONS, and return the result.
3073 DEREF (ADDRESSOF) = SCALAR
3074 DEREF (SCALAR) = DEREF
3075 DEREF (DEREF) = (temp = DEREF1; result = DEREF(temp))
3076 This is needed so that we can handle dereferencing DEREF constraints. */
3078 static void
3079 do_deref (VEC (ce_s, heap) **constraints)
3081 struct constraint_expr *c;
3082 unsigned int i = 0;
3084 for (i = 0; VEC_iterate (ce_s, *constraints, i, c); i++)
3086 if (c->type == SCALAR)
3087 c->type = DEREF;
3088 else if (c->type == ADDRESSOF)
3089 c->type = SCALAR;
3090 else if (c->type == DEREF)
3092 tree tmpvar = create_tmp_var_raw (ptr_type_node, "dereftmp");
3093 struct constraint_expr tmplhs = get_constraint_exp_for_temp (tmpvar);
3094 process_constraint (new_constraint (tmplhs, *c));
3095 c->var = tmplhs.var;
3097 else
3098 gcc_unreachable ();
3102 static void get_constraint_for_1 (tree, VEC (ce_s, heap) **, bool);
3104 /* Given a tree T, return the constraint expression for taking the
3105 address of it. */
3107 static void
3108 get_constraint_for_address_of (tree t, VEC (ce_s, heap) **results)
3110 struct constraint_expr *c;
3111 unsigned int i;
3113 get_constraint_for_1 (t, results, true);
3115 for (i = 0; VEC_iterate (ce_s, *results, i, c); i++)
3117 if (c->type == DEREF)
3118 c->type = SCALAR;
3119 else
3120 c->type = ADDRESSOF;
3124 /* Given a tree T, return the constraint expression for it. */
3126 static void
3127 get_constraint_for_1 (tree t, VEC (ce_s, heap) **results, bool address_p)
3129 struct constraint_expr temp;
3131 /* x = integer is all glommed to a single variable, which doesn't
3132 point to anything by itself. That is, of course, unless it is an
3133 integer constant being treated as a pointer, in which case, we
3134 will return that this is really the addressof anything. This
3135 happens below, since it will fall into the default case. The only
3136 case we know something about an integer treated like a pointer is
3137 when it is the NULL pointer, and then we just say it points to
3138 NULL.
3140 Do not do that if -fno-delete-null-pointer-checks though, because
3141 in that case *NULL does not fail, so it _should_ alias *anything.
3142 It is not worth adding a new option or renaming the existing one,
3143 since this case is relatively obscure. */
3144 if (flag_delete_null_pointer_checks
3145 && ((TREE_CODE (t) == INTEGER_CST
3146 && integer_zerop (t))
3147 /* The only valid CONSTRUCTORs in gimple with pointer typed
3148 elements are zero-initializer. */
3149 || TREE_CODE (t) == CONSTRUCTOR))
3151 temp.var = nothing_id;
3152 temp.type = ADDRESSOF;
3153 temp.offset = 0;
3154 VEC_safe_push (ce_s, heap, *results, &temp);
3155 return;
3158 /* String constants are read-only. */
3159 if (TREE_CODE (t) == STRING_CST)
3161 temp.var = readonly_id;
3162 temp.type = SCALAR;
3163 temp.offset = 0;
3164 VEC_safe_push (ce_s, heap, *results, &temp);
3165 return;
3168 switch (TREE_CODE_CLASS (TREE_CODE (t)))
3170 case tcc_expression:
3172 switch (TREE_CODE (t))
3174 case ADDR_EXPR:
3175 get_constraint_for_address_of (TREE_OPERAND (t, 0), results);
3176 return;
3177 default:;
3179 break;
3181 case tcc_reference:
3183 switch (TREE_CODE (t))
3185 case INDIRECT_REF:
3187 get_constraint_for_1 (TREE_OPERAND (t, 0), results, address_p);
3188 do_deref (results);
3189 return;
3191 case ARRAY_REF:
3192 case ARRAY_RANGE_REF:
3193 case COMPONENT_REF:
3194 get_constraint_for_component_ref (t, results, address_p);
3195 return;
3196 case VIEW_CONVERT_EXPR:
3197 get_constraint_for_1 (TREE_OPERAND (t, 0), results, address_p);
3198 return;
3199 /* We are missing handling for TARGET_MEM_REF here. */
3200 default:;
3202 break;
3204 case tcc_exceptional:
3206 switch (TREE_CODE (t))
3208 case SSA_NAME:
3210 get_constraint_for_ssa_var (t, results, address_p);
3211 return;
3213 default:;
3215 break;
3217 case tcc_declaration:
3219 get_constraint_for_ssa_var (t, results, address_p);
3220 return;
3222 default:;
3225 /* The default fallback is a constraint from anything. */
3226 temp.type = ADDRESSOF;
3227 temp.var = anything_id;
3228 temp.offset = 0;
3229 VEC_safe_push (ce_s, heap, *results, &temp);
3232 /* Given a gimple tree T, return the constraint expression vector for it. */
3234 static void
3235 get_constraint_for (tree t, VEC (ce_s, heap) **results)
3237 gcc_assert (VEC_length (ce_s, *results) == 0);
3239 get_constraint_for_1 (t, results, false);
3242 /* Handle aggregate copies by expanding into copies of the respective
3243 fields of the structures. */
3245 static void
3246 do_structure_copy (tree lhsop, tree rhsop)
3248 struct constraint_expr *lhsp, *rhsp;
3249 VEC (ce_s, heap) *lhsc = NULL, *rhsc = NULL;
3250 unsigned j;
3252 get_constraint_for (lhsop, &lhsc);
3253 get_constraint_for (rhsop, &rhsc);
3254 lhsp = VEC_index (ce_s, lhsc, 0);
3255 rhsp = VEC_index (ce_s, rhsc, 0);
3256 if (lhsp->type == DEREF
3257 || (lhsp->type == ADDRESSOF && lhsp->var == anything_id)
3258 || rhsp->type == DEREF)
3260 struct constraint_expr tmp;
3261 tree tmpvar = create_tmp_var_raw (ptr_type_node,
3262 "structcopydereftmp");
3263 tmp.var = get_vi_for_tree (tmpvar)->id;
3264 tmp.type = SCALAR;
3265 tmp.offset = 0;
3266 for (j = 0; VEC_iterate (ce_s, rhsc, j, rhsp); ++j)
3267 process_constraint (new_constraint (tmp, *rhsp));
3268 for (j = 0; VEC_iterate (ce_s, lhsc, j, lhsp); ++j)
3269 process_constraint (new_constraint (*lhsp, tmp));
3271 else if (lhsp->type == SCALAR
3272 && (rhsp->type == SCALAR
3273 || rhsp->type == ADDRESSOF))
3275 tree lhsbase, rhsbase;
3276 HOST_WIDE_INT lhssize, lhsmaxsize, lhsoffset;
3277 HOST_WIDE_INT rhssize, rhsmaxsize, rhsoffset;
3278 unsigned k = 0;
3279 lhsbase = get_ref_base_and_extent (lhsop, &lhsoffset,
3280 &lhssize, &lhsmaxsize);
3281 rhsbase = get_ref_base_and_extent (rhsop, &rhsoffset,
3282 &rhssize, &rhsmaxsize);
3283 for (j = 0; VEC_iterate (ce_s, lhsc, j, lhsp);)
3285 varinfo_t lhsv, rhsv;
3286 rhsp = VEC_index (ce_s, rhsc, k);
3287 lhsv = get_varinfo (lhsp->var);
3288 rhsv = get_varinfo (rhsp->var);
3289 if (lhsv->may_have_pointers
3290 && ranges_overlap_p (lhsv->offset + rhsoffset, lhsv->size,
3291 rhsv->offset + lhsoffset, rhsv->size))
3292 process_constraint (new_constraint (*lhsp, *rhsp));
3293 if (lhsv->offset + rhsoffset + lhsv->size
3294 > rhsv->offset + lhsoffset + rhsv->size)
3296 ++k;
3297 if (k >= VEC_length (ce_s, rhsc))
3298 break;
3300 else
3301 ++j;
3304 else
3305 gcc_unreachable ();
3307 VEC_free (ce_s, heap, lhsc);
3308 VEC_free (ce_s, heap, rhsc);
3311 /* Create a constraint ID = OP. */
3313 static void
3314 make_constraint_to (unsigned id, tree op)
3316 VEC(ce_s, heap) *rhsc = NULL;
3317 struct constraint_expr *c;
3318 struct constraint_expr includes;
3319 unsigned int j;
3321 includes.var = id;
3322 includes.offset = 0;
3323 includes.type = SCALAR;
3325 get_constraint_for (op, &rhsc);
3326 for (j = 0; VEC_iterate (ce_s, rhsc, j, c); j++)
3327 process_constraint (new_constraint (includes, *c));
3328 VEC_free (ce_s, heap, rhsc);
3331 /* Make constraints necessary to make OP escape. */
3333 static void
3334 make_escape_constraint (tree op)
3336 make_constraint_to (escaped_id, op);
3339 /* For non-IPA mode, generate constraints necessary for a call on the
3340 RHS. */
3342 static void
3343 handle_rhs_call (gimple stmt, VEC(ce_s, heap) **results)
3345 struct constraint_expr rhsc;
3346 unsigned i;
3348 for (i = 0; i < gimple_call_num_args (stmt); ++i)
3350 tree arg = gimple_call_arg (stmt, i);
3352 /* Find those pointers being passed, and make sure they end up
3353 pointing to anything. */
3354 if (could_have_pointers (arg))
3355 make_escape_constraint (arg);
3358 /* The static chain escapes as well. */
3359 if (gimple_call_chain (stmt))
3360 make_escape_constraint (gimple_call_chain (stmt));
3362 /* And if we applied NRV the address of the return slot escapes as well. */
3363 if (gimple_call_return_slot_opt_p (stmt)
3364 && gimple_call_lhs (stmt) != NULL_TREE
3365 && TREE_ADDRESSABLE (gimple_call_lhs (stmt)))
3367 VEC(ce_s, heap) *tmpc = NULL;
3368 struct constraint_expr lhsc, *c;
3369 get_constraint_for_address_of (gimple_call_lhs (stmt), &tmpc);
3370 lhsc.var = escaped_id;
3371 lhsc.offset = 0;
3372 lhsc.type = SCALAR;
3373 for (i = 0; VEC_iterate (ce_s, tmpc, i, c); ++i)
3374 process_constraint (new_constraint (lhsc, *c));
3375 VEC_free(ce_s, heap, tmpc);
3378 /* Regular functions return nonlocal memory. */
3379 rhsc.var = nonlocal_id;
3380 rhsc.offset = 0;
3381 rhsc.type = SCALAR;
3382 VEC_safe_push (ce_s, heap, *results, &rhsc);
3385 /* For non-IPA mode, generate constraints necessary for a call
3386 that returns a pointer and assigns it to LHS. This simply makes
3387 the LHS point to global and escaped variables. */
3389 static void
3390 handle_lhs_call (tree lhs, int flags, VEC(ce_s, heap) *rhsc)
3392 VEC(ce_s, heap) *lhsc = NULL;
3393 unsigned int j;
3394 struct constraint_expr *lhsp;
3396 get_constraint_for (lhs, &lhsc);
3398 if (flags & ECF_MALLOC)
3400 struct constraint_expr rhsc;
3401 tree heapvar = heapvar_lookup (lhs);
3402 varinfo_t vi;
3404 if (heapvar == NULL)
3406 heapvar = create_tmp_var_raw (ptr_type_node, "HEAP");
3407 DECL_EXTERNAL (heapvar) = 1;
3408 get_var_ann (heapvar)->is_heapvar = 1;
3409 if (gimple_referenced_vars (cfun))
3410 add_referenced_var (heapvar);
3411 heapvar_insert (lhs, heapvar);
3414 rhsc.var = create_variable_info_for (heapvar,
3415 alias_get_name (heapvar));
3416 vi = get_varinfo (rhsc.var);
3417 vi->is_artificial_var = 1;
3418 vi->is_heap_var = 1;
3419 vi->is_unknown_size_var = true;
3420 vi->fullsize = ~0;
3421 vi->size = ~0;
3422 rhsc.type = ADDRESSOF;
3423 rhsc.offset = 0;
3424 for (j = 0; VEC_iterate (ce_s, lhsc, j, lhsp); j++)
3425 process_constraint (new_constraint (*lhsp, rhsc));
3427 else if (VEC_length (ce_s, rhsc) > 0)
3429 struct constraint_expr *lhsp, *rhsp;
3430 unsigned int i, j;
3431 /* If the store is to a global decl make sure to
3432 add proper escape constraints. */
3433 lhs = get_base_address (lhs);
3434 if (lhs
3435 && DECL_P (lhs)
3436 && is_global_var (lhs))
3438 struct constraint_expr tmpc;
3439 tmpc.var = escaped_id;
3440 tmpc.offset = 0;
3441 tmpc.type = SCALAR;
3442 VEC_safe_push (ce_s, heap, lhsc, &tmpc);
3444 for (i = 0; VEC_iterate (ce_s, lhsc, i, lhsp); ++i)
3445 for (j = 0; VEC_iterate (ce_s, rhsc, j, rhsp); ++j)
3446 process_constraint (new_constraint (*lhsp, *rhsp));
3448 VEC_free (ce_s, heap, lhsc);
3451 /* For non-IPA mode, generate constraints necessary for a call of a
3452 const function that returns a pointer in the statement STMT. */
3454 static void
3455 handle_const_call (gimple stmt, VEC(ce_s, heap) **results)
3457 struct constraint_expr rhsc, tmpc = {SCALAR, 0, 0};
3458 tree tmpvar = NULL_TREE;
3459 unsigned int k;
3461 /* Treat nested const functions the same as pure functions as far
3462 as the static chain is concerned. */
3463 if (gimple_call_chain (stmt))
3465 make_constraint_to (callused_id, gimple_call_chain (stmt));
3466 rhsc.var = callused_id;
3467 rhsc.offset = 0;
3468 rhsc.type = SCALAR;
3469 VEC_safe_push (ce_s, heap, *results, &rhsc);
3472 /* May return arguments. */
3473 for (k = 0; k < gimple_call_num_args (stmt); ++k)
3475 tree arg = gimple_call_arg (stmt, k);
3477 if (could_have_pointers (arg))
3479 VEC(ce_s, heap) *argc = NULL;
3480 struct constraint_expr *argp;
3481 int i;
3483 /* We always use a temporary here, otherwise we end up with
3484 a quadratic amount of constraints for
3485 large_struct = const_call (large_struct);
3486 with field-sensitive PTA. */
3487 if (tmpvar == NULL_TREE)
3489 tmpvar = create_tmp_var_raw (ptr_type_node, "consttmp");
3490 tmpc = get_constraint_exp_for_temp (tmpvar);
3493 get_constraint_for (arg, &argc);
3494 for (i = 0; VEC_iterate (ce_s, argc, i, argp); i++)
3495 process_constraint (new_constraint (tmpc, *argp));
3496 VEC_free (ce_s, heap, argc);
3499 if (tmpvar != NULL_TREE)
3500 VEC_safe_push (ce_s, heap, *results, &tmpc);
3502 /* May return addresses of globals. */
3503 rhsc.var = nonlocal_id;
3504 rhsc.offset = 0;
3505 rhsc.type = ADDRESSOF;
3506 VEC_safe_push (ce_s, heap, *results, &rhsc);
3509 /* For non-IPA mode, generate constraints necessary for a call to a
3510 pure function in statement STMT. */
3512 static void
3513 handle_pure_call (gimple stmt, VEC(ce_s, heap) **results)
3515 struct constraint_expr rhsc;
3516 unsigned i;
3517 bool need_callused = false;
3519 /* Memory reached from pointer arguments is call-used. */
3520 for (i = 0; i < gimple_call_num_args (stmt); ++i)
3522 tree arg = gimple_call_arg (stmt, i);
3524 if (could_have_pointers (arg))
3526 make_constraint_to (callused_id, arg);
3527 need_callused = true;
3531 /* The static chain is used as well. */
3532 if (gimple_call_chain (stmt))
3534 make_constraint_to (callused_id, gimple_call_chain (stmt));
3535 need_callused = true;
3538 /* Pure functions may return callused and nonlocal memory. */
3539 if (need_callused)
3541 rhsc.var = callused_id;
3542 rhsc.offset = 0;
3543 rhsc.type = SCALAR;
3544 VEC_safe_push (ce_s, heap, *results, &rhsc);
3546 rhsc.var = nonlocal_id;
3547 rhsc.offset = 0;
3548 rhsc.type = SCALAR;
3549 VEC_safe_push (ce_s, heap, *results, &rhsc);
3552 /* Walk statement T setting up aliasing constraints according to the
3553 references found in T. This function is the main part of the
3554 constraint builder. AI points to auxiliary alias information used
3555 when building alias sets and computing alias grouping heuristics. */
3557 static void
3558 find_func_aliases (gimple origt)
3560 gimple t = origt;
3561 VEC(ce_s, heap) *lhsc = NULL;
3562 VEC(ce_s, heap) *rhsc = NULL;
3563 struct constraint_expr *c;
3565 /* Now build constraints expressions. */
3566 if (gimple_code (t) == GIMPLE_PHI)
3568 gcc_assert (!AGGREGATE_TYPE_P (TREE_TYPE (gimple_phi_result (t))));
3570 /* Only care about pointers and structures containing
3571 pointers. */
3572 if (could_have_pointers (gimple_phi_result (t)))
3574 size_t i;
3575 unsigned int j;
3577 /* For a phi node, assign all the arguments to
3578 the result. */
3579 get_constraint_for (gimple_phi_result (t), &lhsc);
3580 for (i = 0; i < gimple_phi_num_args (t); i++)
3582 tree rhstype;
3583 tree strippedrhs = PHI_ARG_DEF (t, i);
3585 STRIP_NOPS (strippedrhs);
3586 rhstype = TREE_TYPE (strippedrhs);
3587 get_constraint_for (gimple_phi_arg_def (t, i), &rhsc);
3589 for (j = 0; VEC_iterate (ce_s, lhsc, j, c); j++)
3591 struct constraint_expr *c2;
3592 while (VEC_length (ce_s, rhsc) > 0)
3594 c2 = VEC_last (ce_s, rhsc);
3595 process_constraint (new_constraint (*c, *c2));
3596 VEC_pop (ce_s, rhsc);
3602 /* In IPA mode, we need to generate constraints to pass call
3603 arguments through their calls. There are two cases,
3604 either a GIMPLE_CALL returning a value, or just a plain
3605 GIMPLE_CALL when we are not.
3607 In non-ipa mode, we need to generate constraints for each
3608 pointer passed by address. */
3609 else if (is_gimple_call (t))
3611 if (!in_ipa_mode)
3613 VEC(ce_s, heap) *rhsc = NULL;
3614 int flags = gimple_call_flags (t);
3616 /* Const functions can return their arguments and addresses
3617 of global memory but not of escaped memory. */
3618 if (flags & (ECF_CONST|ECF_NOVOPS))
3620 if (gimple_call_lhs (t)
3621 && could_have_pointers (gimple_call_lhs (t)))
3622 handle_const_call (t, &rhsc);
3624 /* Pure functions can return addresses in and of memory
3625 reachable from their arguments, but they are not an escape
3626 point for reachable memory of their arguments. */
3627 else if (flags & (ECF_PURE|ECF_LOOPING_CONST_OR_PURE))
3628 handle_pure_call (t, &rhsc);
3629 else
3630 handle_rhs_call (t, &rhsc);
3631 if (gimple_call_lhs (t)
3632 && could_have_pointers (gimple_call_lhs (t)))
3633 handle_lhs_call (gimple_call_lhs (t), flags, rhsc);
3634 VEC_free (ce_s, heap, rhsc);
3636 else
3638 tree lhsop;
3639 varinfo_t fi;
3640 int i = 1;
3641 size_t j;
3642 tree decl;
3644 lhsop = gimple_call_lhs (t);
3645 decl = gimple_call_fndecl (t);
3647 /* If we can directly resolve the function being called, do so.
3648 Otherwise, it must be some sort of indirect expression that
3649 we should still be able to handle. */
3650 if (decl)
3651 fi = get_vi_for_tree (decl);
3652 else
3654 decl = gimple_call_fn (t);
3655 fi = get_vi_for_tree (decl);
3658 /* Assign all the passed arguments to the appropriate incoming
3659 parameters of the function. */
3660 for (j = 0; j < gimple_call_num_args (t); j++)
3662 struct constraint_expr lhs ;
3663 struct constraint_expr *rhsp;
3664 tree arg = gimple_call_arg (t, j);
3666 get_constraint_for (arg, &rhsc);
3667 if (TREE_CODE (decl) != FUNCTION_DECL)
3669 lhs.type = DEREF;
3670 lhs.var = fi->id;
3671 lhs.offset = i;
3673 else
3675 lhs.type = SCALAR;
3676 lhs.var = first_vi_for_offset (fi, i)->id;
3677 lhs.offset = 0;
3679 while (VEC_length (ce_s, rhsc) != 0)
3681 rhsp = VEC_last (ce_s, rhsc);
3682 process_constraint (new_constraint (lhs, *rhsp));
3683 VEC_pop (ce_s, rhsc);
3685 i++;
3688 /* If we are returning a value, assign it to the result. */
3689 if (lhsop)
3691 struct constraint_expr rhs;
3692 struct constraint_expr *lhsp;
3693 unsigned int j = 0;
3695 get_constraint_for (lhsop, &lhsc);
3696 if (TREE_CODE (decl) != FUNCTION_DECL)
3698 rhs.type = DEREF;
3699 rhs.var = fi->id;
3700 rhs.offset = i;
3702 else
3704 rhs.type = SCALAR;
3705 rhs.var = first_vi_for_offset (fi, i)->id;
3706 rhs.offset = 0;
3708 for (j = 0; VEC_iterate (ce_s, lhsc, j, lhsp); j++)
3709 process_constraint (new_constraint (*lhsp, rhs));
3713 /* Otherwise, just a regular assignment statement. Only care about
3714 operations with pointer result, others are dealt with as escape
3715 points if they have pointer operands. */
3716 else if (is_gimple_assign (t)
3717 && could_have_pointers (gimple_assign_lhs (t)))
3719 /* Otherwise, just a regular assignment statement. */
3720 tree lhsop = gimple_assign_lhs (t);
3721 tree rhsop = (gimple_num_ops (t) == 2) ? gimple_assign_rhs1 (t) : NULL;
3723 if (rhsop && AGGREGATE_TYPE_P (TREE_TYPE (lhsop)))
3724 do_structure_copy (lhsop, rhsop);
3725 else
3727 unsigned int j;
3728 struct constraint_expr temp;
3729 get_constraint_for (lhsop, &lhsc);
3731 if (gimple_assign_rhs_code (t) == POINTER_PLUS_EXPR)
3732 get_constraint_for_ptr_offset (gimple_assign_rhs1 (t),
3733 gimple_assign_rhs2 (t), &rhsc);
3734 else if ((CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (t))
3735 && !(POINTER_TYPE_P (gimple_expr_type (t))
3736 && !POINTER_TYPE_P (TREE_TYPE (rhsop))))
3737 || gimple_assign_single_p (t))
3738 get_constraint_for (rhsop, &rhsc);
3739 else
3741 temp.type = ADDRESSOF;
3742 temp.var = anything_id;
3743 temp.offset = 0;
3744 VEC_safe_push (ce_s, heap, rhsc, &temp);
3746 for (j = 0; VEC_iterate (ce_s, lhsc, j, c); j++)
3748 struct constraint_expr *c2;
3749 unsigned int k;
3751 for (k = 0; VEC_iterate (ce_s, rhsc, k, c2); k++)
3752 process_constraint (new_constraint (*c, *c2));
3755 /* If there is a store to a global variable the rhs escapes. */
3756 if ((lhsop = get_base_address (lhsop)) != NULL_TREE
3757 && DECL_P (lhsop)
3758 && is_global_var (lhsop))
3759 make_escape_constraint (rhsop);
3761 /* For conversions of pointers to non-pointers the pointer escapes. */
3762 else if (gimple_assign_cast_p (t)
3763 && POINTER_TYPE_P (TREE_TYPE (gimple_assign_rhs1 (t)))
3764 && !POINTER_TYPE_P (TREE_TYPE (gimple_assign_lhs (t))))
3766 make_escape_constraint (gimple_assign_rhs1 (t));
3768 /* Handle asms conservatively by adding escape constraints to everything. */
3769 else if (gimple_code (t) == GIMPLE_ASM)
3771 unsigned i, noutputs;
3772 const char **oconstraints;
3773 const char *constraint;
3774 bool allows_mem, allows_reg, is_inout;
3776 noutputs = gimple_asm_noutputs (t);
3777 oconstraints = XALLOCAVEC (const char *, noutputs);
3779 for (i = 0; i < noutputs; ++i)
3781 tree link = gimple_asm_output_op (t, i);
3782 tree op = TREE_VALUE (link);
3784 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
3785 oconstraints[i] = constraint;
3786 parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
3787 &allows_reg, &is_inout);
3789 /* A memory constraint makes the address of the operand escape. */
3790 if (!allows_reg && allows_mem)
3791 make_escape_constraint (build_fold_addr_expr (op));
3793 /* The asm may read global memory, so outputs may point to
3794 any global memory. */
3795 if (op && could_have_pointers (op))
3797 VEC(ce_s, heap) *lhsc = NULL;
3798 struct constraint_expr rhsc, *lhsp;
3799 unsigned j;
3800 get_constraint_for (op, &lhsc);
3801 rhsc.var = nonlocal_id;
3802 rhsc.offset = 0;
3803 rhsc.type = SCALAR;
3804 for (j = 0; VEC_iterate (ce_s, lhsc, j, lhsp); j++)
3805 process_constraint (new_constraint (*lhsp, rhsc));
3806 VEC_free (ce_s, heap, lhsc);
3809 for (i = 0; i < gimple_asm_ninputs (t); ++i)
3811 tree link = gimple_asm_input_op (t, i);
3812 tree op = TREE_VALUE (link);
3814 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
3816 parse_input_constraint (&constraint, 0, 0, noutputs, 0, oconstraints,
3817 &allows_mem, &allows_reg);
3819 /* A memory constraint makes the address of the operand escape. */
3820 if (!allows_reg && allows_mem)
3821 make_escape_constraint (build_fold_addr_expr (op));
3822 /* Strictly we'd only need the constraint to ESCAPED if
3823 the asm clobbers memory, otherwise using CALLUSED
3824 would be enough. */
3825 else if (op && could_have_pointers (op))
3826 make_escape_constraint (op);
3830 VEC_free (ce_s, heap, rhsc);
3831 VEC_free (ce_s, heap, lhsc);
3835 /* Find the first varinfo in the same variable as START that overlaps with
3836 OFFSET. Return NULL if we can't find one. */
3838 static varinfo_t
3839 first_vi_for_offset (varinfo_t start, unsigned HOST_WIDE_INT offset)
3841 /* If the offset is outside of the variable, bail out. */
3842 if (offset >= start->fullsize)
3843 return NULL;
3845 /* If we cannot reach offset from start, lookup the first field
3846 and start from there. */
3847 if (start->offset > offset)
3848 start = lookup_vi_for_tree (start->decl);
3850 while (start)
3852 /* We may not find a variable in the field list with the actual
3853 offset when when we have glommed a structure to a variable.
3854 In that case, however, offset should still be within the size
3855 of the variable. */
3856 if (offset >= start->offset
3857 && offset < (start->offset + start->size))
3858 return start;
3860 start= start->next;
3863 return NULL;
3866 /* Find the first varinfo in the same variable as START that overlaps with
3867 OFFSET. If there is no such varinfo the varinfo directly preceding
3868 OFFSET is returned. */
3870 static varinfo_t
3871 first_or_preceding_vi_for_offset (varinfo_t start,
3872 unsigned HOST_WIDE_INT offset)
3874 /* If we cannot reach offset from start, lookup the first field
3875 and start from there. */
3876 if (start->offset > offset)
3877 start = lookup_vi_for_tree (start->decl);
3879 /* We may not find a variable in the field list with the actual
3880 offset when when we have glommed a structure to a variable.
3881 In that case, however, offset should still be within the size
3882 of the variable.
3883 If we got beyond the offset we look for return the field
3884 directly preceding offset which may be the last field. */
3885 while (start->next
3886 && offset >= start->offset
3887 && !(offset < (start->offset + start->size)))
3888 start = start->next;
3890 return start;
3894 /* Insert the varinfo FIELD into the field list for BASE, at the front
3895 of the list. */
3897 static void
3898 insert_into_field_list (varinfo_t base, varinfo_t field)
3900 varinfo_t prev = base;
3901 varinfo_t curr = base->next;
3903 field->next = curr;
3904 prev->next = field;
3907 /* Insert the varinfo FIELD into the field list for BASE, ordered by
3908 offset. */
3910 static void
3911 insert_into_field_list_sorted (varinfo_t base, varinfo_t field)
3913 varinfo_t prev = base;
3914 varinfo_t curr = base->next;
3916 if (curr == NULL)
3918 prev->next = field;
3919 field->next = NULL;
3921 else
3923 while (curr)
3925 if (field->offset <= curr->offset)
3926 break;
3927 prev = curr;
3928 curr = curr->next;
3930 field->next = prev->next;
3931 prev->next = field;
3935 /* This structure is used during pushing fields onto the fieldstack
3936 to track the offset of the field, since bitpos_of_field gives it
3937 relative to its immediate containing type, and we want it relative
3938 to the ultimate containing object. */
3940 struct fieldoff
3942 /* Offset from the base of the base containing object to this field. */
3943 HOST_WIDE_INT offset;
3945 /* Size, in bits, of the field. */
3946 unsigned HOST_WIDE_INT size;
3948 unsigned has_unknown_size : 1;
3950 unsigned may_have_pointers : 1;
3952 typedef struct fieldoff fieldoff_s;
3954 DEF_VEC_O(fieldoff_s);
3955 DEF_VEC_ALLOC_O(fieldoff_s,heap);
3957 /* qsort comparison function for two fieldoff's PA and PB */
3959 static int
3960 fieldoff_compare (const void *pa, const void *pb)
3962 const fieldoff_s *foa = (const fieldoff_s *)pa;
3963 const fieldoff_s *fob = (const fieldoff_s *)pb;
3964 unsigned HOST_WIDE_INT foasize, fobsize;
3966 if (foa->offset < fob->offset)
3967 return -1;
3968 else if (foa->offset > fob->offset)
3969 return 1;
3971 foasize = foa->size;
3972 fobsize = fob->size;
3973 if (foasize < fobsize)
3974 return -1;
3975 else if (foasize > fobsize)
3976 return 1;
3977 return 0;
3980 /* Sort a fieldstack according to the field offset and sizes. */
3981 static void
3982 sort_fieldstack (VEC(fieldoff_s,heap) *fieldstack)
3984 qsort (VEC_address (fieldoff_s, fieldstack),
3985 VEC_length (fieldoff_s, fieldstack),
3986 sizeof (fieldoff_s),
3987 fieldoff_compare);
3990 /* Return true if V is a tree that we can have subvars for.
3991 Normally, this is any aggregate type. Also complex
3992 types which are not gimple registers can have subvars. */
3994 static inline bool
3995 var_can_have_subvars (const_tree v)
3997 /* Volatile variables should never have subvars. */
3998 if (TREE_THIS_VOLATILE (v))
3999 return false;
4001 /* Non decls or memory tags can never have subvars. */
4002 if (!DECL_P (v))
4003 return false;
4005 /* Aggregates without overlapping fields can have subvars. */
4006 if (TREE_CODE (TREE_TYPE (v)) == RECORD_TYPE)
4007 return true;
4009 return false;
4012 /* Given a TYPE, and a vector of field offsets FIELDSTACK, push all
4013 the fields of TYPE onto fieldstack, recording their offsets along
4014 the way.
4016 OFFSET is used to keep track of the offset in this entire
4017 structure, rather than just the immediately containing structure.
4018 Returns the number of fields pushed. */
4020 static int
4021 push_fields_onto_fieldstack (tree type, VEC(fieldoff_s,heap) **fieldstack,
4022 HOST_WIDE_INT offset)
4024 tree field;
4025 int count = 0;
4027 if (TREE_CODE (type) != RECORD_TYPE)
4028 return 0;
4030 /* If the vector of fields is growing too big, bail out early.
4031 Callers check for VEC_length <= MAX_FIELDS_FOR_FIELD_SENSITIVE, make
4032 sure this fails. */
4033 if (VEC_length (fieldoff_s, *fieldstack) > MAX_FIELDS_FOR_FIELD_SENSITIVE)
4034 return 0;
4036 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
4037 if (TREE_CODE (field) == FIELD_DECL)
4039 bool push = false;
4040 int pushed = 0;
4041 HOST_WIDE_INT foff = bitpos_of_field (field);
4043 if (!var_can_have_subvars (field)
4044 || TREE_CODE (TREE_TYPE (field)) == QUAL_UNION_TYPE
4045 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
4046 push = true;
4047 else if (!(pushed = push_fields_onto_fieldstack
4048 (TREE_TYPE (field), fieldstack, offset + foff))
4049 && (DECL_SIZE (field)
4050 && !integer_zerop (DECL_SIZE (field))))
4051 /* Empty structures may have actual size, like in C++. So
4052 see if we didn't push any subfields and the size is
4053 nonzero, push the field onto the stack. */
4054 push = true;
4056 if (push)
4058 fieldoff_s *pair = NULL;
4059 bool has_unknown_size = false;
4061 if (!VEC_empty (fieldoff_s, *fieldstack))
4062 pair = VEC_last (fieldoff_s, *fieldstack);
4064 if (!DECL_SIZE (field)
4065 || !host_integerp (DECL_SIZE (field), 1))
4066 has_unknown_size = true;
4068 /* If adjacent fields do not contain pointers merge them. */
4069 if (pair
4070 && !pair->may_have_pointers
4071 && !could_have_pointers (field)
4072 && !pair->has_unknown_size
4073 && !has_unknown_size
4074 && pair->offset + (HOST_WIDE_INT)pair->size == offset + foff)
4076 pair = VEC_last (fieldoff_s, *fieldstack);
4077 pair->size += TREE_INT_CST_LOW (DECL_SIZE (field));
4079 else
4081 pair = VEC_safe_push (fieldoff_s, heap, *fieldstack, NULL);
4082 pair->offset = offset + foff;
4083 pair->has_unknown_size = has_unknown_size;
4084 if (!has_unknown_size)
4085 pair->size = TREE_INT_CST_LOW (DECL_SIZE (field));
4086 else
4087 pair->size = -1;
4088 pair->may_have_pointers = could_have_pointers (field);
4089 count++;
4092 else
4093 count += pushed;
4096 return count;
4099 /* Create a constraint ID = &FROM. */
4101 static void
4102 make_constraint_from (varinfo_t vi, int from)
4104 struct constraint_expr lhs, rhs;
4106 lhs.var = vi->id;
4107 lhs.offset = 0;
4108 lhs.type = SCALAR;
4110 rhs.var = from;
4111 rhs.offset = 0;
4112 rhs.type = ADDRESSOF;
4113 process_constraint (new_constraint (lhs, rhs));
4116 /* Create a constraint ID = FROM. */
4118 static void
4119 make_copy_constraint (varinfo_t vi, int from)
4121 struct constraint_expr lhs, rhs;
4123 lhs.var = vi->id;
4124 lhs.offset = 0;
4125 lhs.type = SCALAR;
4127 rhs.var = from;
4128 rhs.offset = 0;
4129 rhs.type = SCALAR;
4130 process_constraint (new_constraint (lhs, rhs));
4133 /* Count the number of arguments DECL has, and set IS_VARARGS to true
4134 if it is a varargs function. */
4136 static unsigned int
4137 count_num_arguments (tree decl, bool *is_varargs)
4139 unsigned int i = 0;
4140 tree t;
4142 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl));
4144 t = TREE_CHAIN (t))
4146 if (TREE_VALUE (t) == void_type_node)
4147 break;
4148 i++;
4151 if (!t)
4152 *is_varargs = true;
4153 return i;
4156 /* Creation function node for DECL, using NAME, and return the index
4157 of the variable we've created for the function. */
4159 static unsigned int
4160 create_function_info_for (tree decl, const char *name)
4162 unsigned int index = VEC_length (varinfo_t, varmap);
4163 varinfo_t vi;
4164 tree arg;
4165 unsigned int i;
4166 bool is_varargs = false;
4168 /* Create the variable info. */
4170 vi = new_var_info (decl, index, name);
4171 vi->decl = decl;
4172 vi->offset = 0;
4173 vi->size = 1;
4174 vi->fullsize = count_num_arguments (decl, &is_varargs) + 1;
4175 insert_vi_for_tree (vi->decl, vi);
4176 VEC_safe_push (varinfo_t, heap, varmap, vi);
4178 stats.total_vars++;
4180 /* If it's varargs, we don't know how many arguments it has, so we
4181 can't do much. */
4182 if (is_varargs)
4184 vi->fullsize = ~0;
4185 vi->size = ~0;
4186 vi->is_unknown_size_var = true;
4187 return index;
4191 arg = DECL_ARGUMENTS (decl);
4193 /* Set up variables for each argument. */
4194 for (i = 1; i < vi->fullsize; i++)
4196 varinfo_t argvi;
4197 const char *newname;
4198 char *tempname;
4199 unsigned int newindex;
4200 tree argdecl = decl;
4202 if (arg)
4203 argdecl = arg;
4205 newindex = VEC_length (varinfo_t, varmap);
4206 asprintf (&tempname, "%s.arg%d", name, i-1);
4207 newname = ggc_strdup (tempname);
4208 free (tempname);
4210 argvi = new_var_info (argdecl, newindex, newname);
4211 argvi->decl = argdecl;
4212 VEC_safe_push (varinfo_t, heap, varmap, argvi);
4213 argvi->offset = i;
4214 argvi->size = 1;
4215 argvi->is_full_var = true;
4216 argvi->fullsize = vi->fullsize;
4217 insert_into_field_list_sorted (vi, argvi);
4218 stats.total_vars ++;
4219 if (arg)
4221 insert_vi_for_tree (arg, argvi);
4222 arg = TREE_CHAIN (arg);
4226 /* Create a variable for the return var. */
4227 if (DECL_RESULT (decl) != NULL
4228 || !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4230 varinfo_t resultvi;
4231 const char *newname;
4232 char *tempname;
4233 unsigned int newindex;
4234 tree resultdecl = decl;
4236 vi->fullsize ++;
4238 if (DECL_RESULT (decl))
4239 resultdecl = DECL_RESULT (decl);
4241 newindex = VEC_length (varinfo_t, varmap);
4242 asprintf (&tempname, "%s.result", name);
4243 newname = ggc_strdup (tempname);
4244 free (tempname);
4246 resultvi = new_var_info (resultdecl, newindex, newname);
4247 resultvi->decl = resultdecl;
4248 VEC_safe_push (varinfo_t, heap, varmap, resultvi);
4249 resultvi->offset = i;
4250 resultvi->size = 1;
4251 resultvi->fullsize = vi->fullsize;
4252 resultvi->is_full_var = true;
4253 insert_into_field_list_sorted (vi, resultvi);
4254 stats.total_vars ++;
4255 if (DECL_RESULT (decl))
4256 insert_vi_for_tree (DECL_RESULT (decl), resultvi);
4258 return index;
4262 /* Return true if FIELDSTACK contains fields that overlap.
4263 FIELDSTACK is assumed to be sorted by offset. */
4265 static bool
4266 check_for_overlaps (VEC (fieldoff_s,heap) *fieldstack)
4268 fieldoff_s *fo = NULL;
4269 unsigned int i;
4270 HOST_WIDE_INT lastoffset = -1;
4272 for (i = 0; VEC_iterate (fieldoff_s, fieldstack, i, fo); i++)
4274 if (fo->offset == lastoffset)
4275 return true;
4276 lastoffset = fo->offset;
4278 return false;
4281 /* Create a varinfo structure for NAME and DECL, and add it to VARMAP.
4282 This will also create any varinfo structures necessary for fields
4283 of DECL. */
4285 static unsigned int
4286 create_variable_info_for (tree decl, const char *name)
4288 unsigned int index = VEC_length (varinfo_t, varmap);
4289 varinfo_t vi;
4290 tree decl_type = TREE_TYPE (decl);
4291 tree declsize = DECL_P (decl) ? DECL_SIZE (decl) : TYPE_SIZE (decl_type);
4292 bool is_global = DECL_P (decl) ? is_global_var (decl) : false;
4293 VEC (fieldoff_s,heap) *fieldstack = NULL;
4295 if (TREE_CODE (decl) == FUNCTION_DECL && in_ipa_mode)
4296 return create_function_info_for (decl, name);
4298 if (var_can_have_subvars (decl) && use_field_sensitive
4299 && (!var_ann (decl)
4300 || var_ann (decl)->noalias_state == 0)
4301 && (!var_ann (decl)
4302 || !var_ann (decl)->is_heapvar))
4303 push_fields_onto_fieldstack (decl_type, &fieldstack, 0);
4305 /* If the variable doesn't have subvars, we may end up needing to
4306 sort the field list and create fake variables for all the
4307 fields. */
4308 vi = new_var_info (decl, index, name);
4309 vi->decl = decl;
4310 vi->offset = 0;
4311 vi->may_have_pointers = could_have_pointers (decl);
4312 if (!declsize
4313 || !host_integerp (declsize, 1))
4315 vi->is_unknown_size_var = true;
4316 vi->fullsize = ~0;
4317 vi->size = ~0;
4319 else
4321 vi->fullsize = TREE_INT_CST_LOW (declsize);
4322 vi->size = vi->fullsize;
4325 insert_vi_for_tree (vi->decl, vi);
4326 VEC_safe_push (varinfo_t, heap, varmap, vi);
4327 if (is_global && (!flag_whole_program || !in_ipa_mode)
4328 && vi->may_have_pointers)
4330 if (var_ann (decl)
4331 && var_ann (decl)->noalias_state == NO_ALIAS_ANYTHING)
4332 make_constraint_from (vi, vi->id);
4333 else
4334 make_copy_constraint (vi, nonlocal_id);
4337 stats.total_vars++;
4338 if (use_field_sensitive
4339 && !vi->is_unknown_size_var
4340 && var_can_have_subvars (decl)
4341 && VEC_length (fieldoff_s, fieldstack) > 1
4342 && VEC_length (fieldoff_s, fieldstack) <= MAX_FIELDS_FOR_FIELD_SENSITIVE)
4344 unsigned int newindex = VEC_length (varinfo_t, varmap);
4345 fieldoff_s *fo = NULL;
4346 bool notokay = false;
4347 unsigned int i;
4349 for (i = 0; !notokay && VEC_iterate (fieldoff_s, fieldstack, i, fo); i++)
4351 if (fo->has_unknown_size
4352 || fo->offset < 0)
4354 notokay = true;
4355 break;
4359 /* We can't sort them if we have a field with a variable sized type,
4360 which will make notokay = true. In that case, we are going to return
4361 without creating varinfos for the fields anyway, so sorting them is a
4362 waste to boot. */
4363 if (!notokay)
4365 sort_fieldstack (fieldstack);
4366 /* Due to some C++ FE issues, like PR 22488, we might end up
4367 what appear to be overlapping fields even though they,
4368 in reality, do not overlap. Until the C++ FE is fixed,
4369 we will simply disable field-sensitivity for these cases. */
4370 notokay = check_for_overlaps (fieldstack);
4374 if (VEC_length (fieldoff_s, fieldstack) != 0)
4375 fo = VEC_index (fieldoff_s, fieldstack, 0);
4377 if (fo == NULL || notokay)
4379 vi->is_unknown_size_var = 1;
4380 vi->fullsize = ~0;
4381 vi->size = ~0;
4382 vi->is_full_var = true;
4383 VEC_free (fieldoff_s, heap, fieldstack);
4384 return index;
4387 vi->size = fo->size;
4388 vi->offset = fo->offset;
4389 vi->may_have_pointers = fo->may_have_pointers;
4390 for (i = VEC_length (fieldoff_s, fieldstack) - 1;
4391 i >= 1 && VEC_iterate (fieldoff_s, fieldstack, i, fo);
4392 i--)
4394 varinfo_t newvi;
4395 const char *newname = "NULL";
4396 char *tempname;
4398 newindex = VEC_length (varinfo_t, varmap);
4399 if (dump_file)
4401 asprintf (&tempname, "%s." HOST_WIDE_INT_PRINT_DEC
4402 "+" HOST_WIDE_INT_PRINT_DEC,
4403 vi->name, fo->offset, fo->size);
4404 newname = ggc_strdup (tempname);
4405 free (tempname);
4407 newvi = new_var_info (decl, newindex, newname);
4408 newvi->offset = fo->offset;
4409 newvi->size = fo->size;
4410 newvi->fullsize = vi->fullsize;
4411 newvi->may_have_pointers = fo->may_have_pointers;
4412 insert_into_field_list (vi, newvi);
4413 VEC_safe_push (varinfo_t, heap, varmap, newvi);
4414 if (is_global && (!flag_whole_program || !in_ipa_mode)
4415 && newvi->may_have_pointers)
4416 make_copy_constraint (newvi, nonlocal_id);
4418 stats.total_vars++;
4421 else
4422 vi->is_full_var = true;
4424 VEC_free (fieldoff_s, heap, fieldstack);
4426 return index;
4429 /* Print out the points-to solution for VAR to FILE. */
4431 static void
4432 dump_solution_for_var (FILE *file, unsigned int var)
4434 varinfo_t vi = get_varinfo (var);
4435 unsigned int i;
4436 bitmap_iterator bi;
4438 if (find (var) != var)
4440 varinfo_t vipt = get_varinfo (find (var));
4441 fprintf (file, "%s = same as %s\n", vi->name, vipt->name);
4443 else
4445 fprintf (file, "%s = { ", vi->name);
4446 EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, i, bi)
4448 fprintf (file, "%s ", get_varinfo (i)->name);
4450 fprintf (file, "}\n");
4454 /* Print the points-to solution for VAR to stdout. */
4456 void
4457 debug_solution_for_var (unsigned int var)
4459 dump_solution_for_var (stdout, var);
4462 /* Create varinfo structures for all of the variables in the
4463 function for intraprocedural mode. */
4465 static void
4466 intra_create_variable_infos (void)
4468 tree t;
4469 struct constraint_expr lhs, rhs;
4471 /* For each incoming pointer argument arg, create the constraint ARG
4472 = NONLOCAL or a dummy variable if flag_argument_noalias is set. */
4473 for (t = DECL_ARGUMENTS (current_function_decl); t; t = TREE_CHAIN (t))
4475 varinfo_t p;
4477 if (!could_have_pointers (t))
4478 continue;
4480 /* If flag_argument_noalias is set, then function pointer
4481 arguments are guaranteed not to point to each other. In that
4482 case, create an artificial variable PARM_NOALIAS and the
4483 constraint ARG = &PARM_NOALIAS. */
4484 if (POINTER_TYPE_P (TREE_TYPE (t)) && flag_argument_noalias > 0)
4486 varinfo_t vi;
4487 tree heapvar = heapvar_lookup (t);
4489 lhs.offset = 0;
4490 lhs.type = SCALAR;
4491 lhs.var = get_vi_for_tree (t)->id;
4493 if (heapvar == NULL_TREE)
4495 var_ann_t ann;
4496 heapvar = create_tmp_var_raw (ptr_type_node,
4497 "PARM_NOALIAS");
4498 DECL_EXTERNAL (heapvar) = 1;
4499 if (gimple_referenced_vars (cfun))
4500 add_referenced_var (heapvar);
4502 heapvar_insert (t, heapvar);
4504 ann = get_var_ann (heapvar);
4505 ann->is_heapvar = 1;
4506 if (flag_argument_noalias == 1)
4507 ann->noalias_state = NO_ALIAS;
4508 else if (flag_argument_noalias == 2)
4509 ann->noalias_state = NO_ALIAS_GLOBAL;
4510 else if (flag_argument_noalias == 3)
4511 ann->noalias_state = NO_ALIAS_ANYTHING;
4512 else
4513 gcc_unreachable ();
4516 vi = get_vi_for_tree (heapvar);
4517 vi->is_artificial_var = 1;
4518 vi->is_heap_var = 1;
4519 vi->is_unknown_size_var = true;
4520 vi->fullsize = ~0;
4521 vi->size = ~0;
4522 rhs.var = vi->id;
4523 rhs.type = ADDRESSOF;
4524 rhs.offset = 0;
4525 for (p = get_varinfo (lhs.var); p; p = p->next)
4527 struct constraint_expr temp = lhs;
4528 temp.var = p->id;
4529 process_constraint (new_constraint (temp, rhs));
4532 else
4534 varinfo_t arg_vi = get_vi_for_tree (t);
4536 for (p = arg_vi; p; p = p->next)
4537 make_constraint_from (p, nonlocal_id);
4541 /* Add a constraint for a result decl that is passed by reference. */
4542 if (DECL_RESULT (cfun->decl)
4543 && DECL_BY_REFERENCE (DECL_RESULT (cfun->decl)))
4545 varinfo_t p, result_vi = get_vi_for_tree (DECL_RESULT (cfun->decl));
4547 for (p = result_vi; p; p = p->next)
4548 make_constraint_from (p, nonlocal_id);
4551 /* Add a constraint for the incoming static chain parameter. */
4552 if (cfun->static_chain_decl != NULL_TREE)
4554 varinfo_t p, chain_vi = get_vi_for_tree (cfun->static_chain_decl);
4556 for (p = chain_vi; p; p = p->next)
4557 make_constraint_from (p, nonlocal_id);
4561 /* Structure used to put solution bitmaps in a hashtable so they can
4562 be shared among variables with the same points-to set. */
4564 typedef struct shared_bitmap_info
4566 bitmap pt_vars;
4567 hashval_t hashcode;
4568 } *shared_bitmap_info_t;
4569 typedef const struct shared_bitmap_info *const_shared_bitmap_info_t;
4571 static htab_t shared_bitmap_table;
4573 /* Hash function for a shared_bitmap_info_t */
4575 static hashval_t
4576 shared_bitmap_hash (const void *p)
4578 const_shared_bitmap_info_t const bi = (const_shared_bitmap_info_t) p;
4579 return bi->hashcode;
4582 /* Equality function for two shared_bitmap_info_t's. */
4584 static int
4585 shared_bitmap_eq (const void *p1, const void *p2)
4587 const_shared_bitmap_info_t const sbi1 = (const_shared_bitmap_info_t) p1;
4588 const_shared_bitmap_info_t const sbi2 = (const_shared_bitmap_info_t) p2;
4589 return bitmap_equal_p (sbi1->pt_vars, sbi2->pt_vars);
4592 /* Lookup a bitmap in the shared bitmap hashtable, and return an already
4593 existing instance if there is one, NULL otherwise. */
4595 static bitmap
4596 shared_bitmap_lookup (bitmap pt_vars)
4598 void **slot;
4599 struct shared_bitmap_info sbi;
4601 sbi.pt_vars = pt_vars;
4602 sbi.hashcode = bitmap_hash (pt_vars);
4604 slot = htab_find_slot_with_hash (shared_bitmap_table, &sbi,
4605 sbi.hashcode, NO_INSERT);
4606 if (!slot)
4607 return NULL;
4608 else
4609 return ((shared_bitmap_info_t) *slot)->pt_vars;
4613 /* Add a bitmap to the shared bitmap hashtable. */
4615 static void
4616 shared_bitmap_add (bitmap pt_vars)
4618 void **slot;
4619 shared_bitmap_info_t sbi = XNEW (struct shared_bitmap_info);
4621 sbi->pt_vars = pt_vars;
4622 sbi->hashcode = bitmap_hash (pt_vars);
4624 slot = htab_find_slot_with_hash (shared_bitmap_table, sbi,
4625 sbi->hashcode, INSERT);
4626 gcc_assert (!*slot);
4627 *slot = (void *) sbi;
4631 /* Set bits in INTO corresponding to the variable uids in solution set FROM. */
4633 static void
4634 set_uids_in_ptset (bitmap into, bitmap from, struct pt_solution *pt)
4636 unsigned int i;
4637 bitmap_iterator bi;
4639 EXECUTE_IF_SET_IN_BITMAP (from, 0, i, bi)
4641 varinfo_t vi = get_varinfo (i);
4643 /* The only artificial variables that are allowed in a may-alias
4644 set are heap variables. */
4645 if (vi->is_artificial_var && !vi->is_heap_var)
4646 continue;
4648 if (TREE_CODE (vi->decl) == VAR_DECL
4649 || TREE_CODE (vi->decl) == PARM_DECL
4650 || TREE_CODE (vi->decl) == RESULT_DECL)
4652 /* Add the decl to the points-to set. Note that the points-to
4653 set contains global variables. */
4654 bitmap_set_bit (into, DECL_UID (vi->decl));
4655 if (is_global_var (vi->decl))
4656 pt->vars_contains_global = true;
4662 static bool have_alias_info = false;
4664 /* Compute the points-to solution *PT for the variable VI. */
4666 static void
4667 find_what_var_points_to (varinfo_t vi, struct pt_solution *pt)
4669 unsigned int i;
4670 bitmap_iterator bi;
4671 bitmap finished_solution;
4672 bitmap result;
4673 tree ptr = vi->decl;
4675 memset (pt, 0, sizeof (struct pt_solution));
4677 /* This variable may have been collapsed, let's get the real
4678 variable. */
4679 vi = get_varinfo (find (vi->id));
4681 /* Translate artificial variables into SSA_NAME_PTR_INFO
4682 attributes. */
4683 EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, i, bi)
4685 varinfo_t vi = get_varinfo (i);
4687 if (vi->is_artificial_var)
4689 if (vi->id == nothing_id)
4690 pt->null = 1;
4691 else if (vi->id == escaped_id)
4692 pt->escaped = 1;
4693 else if (vi->id == callused_id)
4694 gcc_unreachable ();
4695 else if (vi->id == nonlocal_id)
4696 pt->nonlocal = 1;
4697 else if (vi->is_heap_var)
4698 /* We represent heapvars in the points-to set properly. */
4700 else if (vi->id == anything_id
4701 || vi->id == readonly_id
4702 || vi->id == integer_id)
4703 pt->anything = 1;
4707 /* Instead of doing extra work, simply do not create
4708 elaborate points-to information for pt_anything pointers. */
4709 if (pt->anything)
4710 return;
4712 /* Share the final set of variables when possible. */
4713 finished_solution = BITMAP_GGC_ALLOC ();
4714 stats.points_to_sets_created++;
4716 if (TREE_CODE (ptr) == SSA_NAME)
4717 ptr = SSA_NAME_VAR (ptr);
4719 set_uids_in_ptset (finished_solution, vi->solution, pt);
4720 result = shared_bitmap_lookup (finished_solution);
4721 if (!result)
4723 shared_bitmap_add (finished_solution);
4724 pt->vars = finished_solution;
4726 else
4728 pt->vars = result;
4729 bitmap_clear (finished_solution);
4733 /* Given a pointer variable P, fill in its points-to set. */
4735 static void
4736 find_what_p_points_to (tree p)
4738 struct ptr_info_def *pi;
4739 tree lookup_p = p;
4740 varinfo_t vi;
4742 /* For parameters, get at the points-to set for the actual parm
4743 decl. */
4744 if (TREE_CODE (p) == SSA_NAME
4745 && TREE_CODE (SSA_NAME_VAR (p)) == PARM_DECL
4746 && SSA_NAME_IS_DEFAULT_DEF (p))
4747 lookup_p = SSA_NAME_VAR (p);
4749 vi = lookup_vi_for_tree (lookup_p);
4750 if (!vi)
4751 return;
4753 pi = get_ptr_info (p);
4754 find_what_var_points_to (vi, &pi->pt);
4758 /* Query statistics for points-to solutions. */
4760 static struct {
4761 unsigned HOST_WIDE_INT pt_solution_includes_may_alias;
4762 unsigned HOST_WIDE_INT pt_solution_includes_no_alias;
4763 unsigned HOST_WIDE_INT pt_solutions_intersect_may_alias;
4764 unsigned HOST_WIDE_INT pt_solutions_intersect_no_alias;
4765 } pta_stats;
4767 void
4768 dump_pta_stats (FILE *s)
4770 fprintf (s, "\nPTA query stats:\n");
4771 fprintf (s, " pt_solution_includes: "
4772 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
4773 HOST_WIDE_INT_PRINT_DEC" queries\n",
4774 pta_stats.pt_solution_includes_no_alias,
4775 pta_stats.pt_solution_includes_no_alias
4776 + pta_stats.pt_solution_includes_may_alias);
4777 fprintf (s, " pt_solutions_intersect: "
4778 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
4779 HOST_WIDE_INT_PRINT_DEC" queries\n",
4780 pta_stats.pt_solutions_intersect_no_alias,
4781 pta_stats.pt_solutions_intersect_no_alias
4782 + pta_stats.pt_solutions_intersect_may_alias);
4786 /* Reset the points-to solution *PT to a conservative default
4787 (point to anything). */
4789 void
4790 pt_solution_reset (struct pt_solution *pt)
4792 memset (pt, 0, sizeof (struct pt_solution));
4793 pt->anything = true;
4796 /* Return true if the points-to solution *PT is empty. */
4798 static bool
4799 pt_solution_empty_p (struct pt_solution *pt)
4801 if (pt->anything
4802 || pt->nonlocal)
4803 return false;
4805 if (pt->vars
4806 && !bitmap_empty_p (pt->vars))
4807 return false;
4809 /* If the solution includes ESCAPED, check if that is empty. */
4810 if (pt->escaped
4811 && !pt_solution_empty_p (&cfun->gimple_df->escaped))
4812 return false;
4814 return true;
4817 /* Return true if the points-to solution *PT includes global memory. */
4819 bool
4820 pt_solution_includes_global (struct pt_solution *pt)
4822 if (pt->anything
4823 || pt->nonlocal
4824 || pt->vars_contains_global)
4825 return true;
4827 if (pt->escaped)
4828 return pt_solution_includes_global (&cfun->gimple_df->escaped);
4830 return false;
4833 /* Return true if the points-to solution *PT includes the variable
4834 declaration DECL. */
4836 static bool
4837 pt_solution_includes_1 (struct pt_solution *pt, const_tree decl)
4839 if (pt->anything)
4840 return true;
4842 if (pt->nonlocal
4843 && is_global_var (decl))
4844 return true;
4846 if (pt->vars
4847 && bitmap_bit_p (pt->vars, DECL_UID (decl)))
4848 return true;
4850 /* If the solution includes ESCAPED, check it. */
4851 if (pt->escaped
4852 && pt_solution_includes_1 (&cfun->gimple_df->escaped, decl))
4853 return true;
4855 return false;
4858 bool
4859 pt_solution_includes (struct pt_solution *pt, const_tree decl)
4861 bool res = pt_solution_includes_1 (pt, decl);
4862 if (res)
4863 ++pta_stats.pt_solution_includes_may_alias;
4864 else
4865 ++pta_stats.pt_solution_includes_no_alias;
4866 return res;
4869 /* Return true if both points-to solutions PT1 and PT2 have a non-empty
4870 intersection. */
4872 static bool
4873 pt_solutions_intersect_1 (struct pt_solution *pt1, struct pt_solution *pt2)
4875 if (pt1->anything || pt2->anything)
4876 return true;
4878 /* If either points to unknown global memory and the other points to
4879 any global memory they alias. */
4880 if ((pt1->nonlocal
4881 && (pt2->nonlocal
4882 || pt2->vars_contains_global))
4883 || (pt2->nonlocal
4884 && pt1->vars_contains_global))
4885 return true;
4887 /* Check the escaped solution if required. */
4888 if ((pt1->escaped || pt2->escaped)
4889 && !pt_solution_empty_p (&cfun->gimple_df->escaped))
4891 /* If both point to escaped memory and that solution
4892 is not empty they alias. */
4893 if (pt1->escaped && pt2->escaped)
4894 return true;
4896 /* If either points to escaped memory see if the escaped solution
4897 intersects with the other. */
4898 if ((pt1->escaped
4899 && pt_solutions_intersect_1 (&cfun->gimple_df->escaped, pt2))
4900 || (pt2->escaped
4901 && pt_solutions_intersect_1 (&cfun->gimple_df->escaped, pt1)))
4902 return true;
4905 /* Now both pointers alias if their points-to solution intersects. */
4906 return (pt1->vars
4907 && pt2->vars
4908 && bitmap_intersect_p (pt1->vars, pt2->vars));
4911 bool
4912 pt_solutions_intersect (struct pt_solution *pt1, struct pt_solution *pt2)
4914 bool res = pt_solutions_intersect_1 (pt1, pt2);
4915 if (res)
4916 ++pta_stats.pt_solutions_intersect_may_alias;
4917 else
4918 ++pta_stats.pt_solutions_intersect_no_alias;
4919 return res;
4923 /* Dump points-to information to OUTFILE. */
4925 static void
4926 dump_sa_points_to_info (FILE *outfile)
4928 unsigned int i;
4930 fprintf (outfile, "\nPoints-to sets\n\n");
4932 if (dump_flags & TDF_STATS)
4934 fprintf (outfile, "Stats:\n");
4935 fprintf (outfile, "Total vars: %d\n", stats.total_vars);
4936 fprintf (outfile, "Non-pointer vars: %d\n",
4937 stats.nonpointer_vars);
4938 fprintf (outfile, "Statically unified vars: %d\n",
4939 stats.unified_vars_static);
4940 fprintf (outfile, "Dynamically unified vars: %d\n",
4941 stats.unified_vars_dynamic);
4942 fprintf (outfile, "Iterations: %d\n", stats.iterations);
4943 fprintf (outfile, "Number of edges: %d\n", stats.num_edges);
4944 fprintf (outfile, "Number of implicit edges: %d\n",
4945 stats.num_implicit_edges);
4948 for (i = 0; i < VEC_length (varinfo_t, varmap); i++)
4949 dump_solution_for_var (outfile, i);
4953 /* Debug points-to information to stderr. */
4955 void
4956 debug_sa_points_to_info (void)
4958 dump_sa_points_to_info (stderr);
4962 /* Initialize the always-existing constraint variables for NULL
4963 ANYTHING, READONLY, and INTEGER */
4965 static void
4966 init_base_vars (void)
4968 struct constraint_expr lhs, rhs;
4970 /* Create the NULL variable, used to represent that a variable points
4971 to NULL. */
4972 nothing_tree = create_tmp_var_raw (void_type_node, "NULL");
4973 var_nothing = new_var_info (nothing_tree, nothing_id, "NULL");
4974 insert_vi_for_tree (nothing_tree, var_nothing);
4975 var_nothing->is_artificial_var = 1;
4976 var_nothing->offset = 0;
4977 var_nothing->size = ~0;
4978 var_nothing->fullsize = ~0;
4979 var_nothing->is_special_var = 1;
4980 VEC_safe_push (varinfo_t, heap, varmap, var_nothing);
4982 /* Create the ANYTHING variable, used to represent that a variable
4983 points to some unknown piece of memory. */
4984 anything_tree = create_tmp_var_raw (ptr_type_node, "ANYTHING");
4985 var_anything = new_var_info (anything_tree, anything_id, "ANYTHING");
4986 insert_vi_for_tree (anything_tree, var_anything);
4987 var_anything->is_artificial_var = 1;
4988 var_anything->size = ~0;
4989 var_anything->offset = 0;
4990 var_anything->next = NULL;
4991 var_anything->fullsize = ~0;
4992 var_anything->is_special_var = 1;
4994 /* Anything points to anything. This makes deref constraints just
4995 work in the presence of linked list and other p = *p type loops,
4996 by saying that *ANYTHING = ANYTHING. */
4997 VEC_safe_push (varinfo_t, heap, varmap, var_anything);
4998 lhs.type = SCALAR;
4999 lhs.var = anything_id;
5000 lhs.offset = 0;
5001 rhs.type = ADDRESSOF;
5002 rhs.var = anything_id;
5003 rhs.offset = 0;
5005 /* This specifically does not use process_constraint because
5006 process_constraint ignores all anything = anything constraints, since all
5007 but this one are redundant. */
5008 VEC_safe_push (constraint_t, heap, constraints, new_constraint (lhs, rhs));
5010 /* Create the READONLY variable, used to represent that a variable
5011 points to readonly memory. */
5012 readonly_tree = create_tmp_var_raw (ptr_type_node, "READONLY");
5013 var_readonly = new_var_info (readonly_tree, readonly_id, "READONLY");
5014 var_readonly->is_artificial_var = 1;
5015 var_readonly->offset = 0;
5016 var_readonly->size = ~0;
5017 var_readonly->fullsize = ~0;
5018 var_readonly->next = NULL;
5019 var_readonly->is_special_var = 1;
5020 insert_vi_for_tree (readonly_tree, var_readonly);
5021 VEC_safe_push (varinfo_t, heap, varmap, var_readonly);
5023 /* readonly memory points to anything, in order to make deref
5024 easier. In reality, it points to anything the particular
5025 readonly variable can point to, but we don't track this
5026 separately. */
5027 lhs.type = SCALAR;
5028 lhs.var = readonly_id;
5029 lhs.offset = 0;
5030 rhs.type = ADDRESSOF;
5031 rhs.var = readonly_id; /* FIXME */
5032 rhs.offset = 0;
5033 process_constraint (new_constraint (lhs, rhs));
5035 /* Create the ESCAPED variable, used to represent the set of escaped
5036 memory. */
5037 escaped_tree = create_tmp_var_raw (ptr_type_node, "ESCAPED");
5038 var_escaped = new_var_info (escaped_tree, escaped_id, "ESCAPED");
5039 insert_vi_for_tree (escaped_tree, var_escaped);
5040 var_escaped->is_artificial_var = 1;
5041 var_escaped->offset = 0;
5042 var_escaped->size = ~0;
5043 var_escaped->fullsize = ~0;
5044 var_escaped->is_special_var = 0;
5045 VEC_safe_push (varinfo_t, heap, varmap, var_escaped);
5046 gcc_assert (VEC_index (varinfo_t, varmap, 3) == var_escaped);
5048 /* Create the NONLOCAL variable, used to represent the set of nonlocal
5049 memory. */
5050 nonlocal_tree = create_tmp_var_raw (ptr_type_node, "NONLOCAL");
5051 var_nonlocal = new_var_info (nonlocal_tree, nonlocal_id, "NONLOCAL");
5052 insert_vi_for_tree (nonlocal_tree, var_nonlocal);
5053 var_nonlocal->is_artificial_var = 1;
5054 var_nonlocal->offset = 0;
5055 var_nonlocal->size = ~0;
5056 var_nonlocal->fullsize = ~0;
5057 var_nonlocal->is_special_var = 1;
5058 VEC_safe_push (varinfo_t, heap, varmap, var_nonlocal);
5060 /* ESCAPED = *ESCAPED, because escaped is may-deref'd at calls, etc. */
5061 lhs.type = SCALAR;
5062 lhs.var = escaped_id;
5063 lhs.offset = 0;
5064 rhs.type = DEREF;
5065 rhs.var = escaped_id;
5066 rhs.offset = 0;
5067 process_constraint (new_constraint (lhs, rhs));
5069 /* ESCAPED = ESCAPED + UNKNOWN_OFFSET, because if a sub-field escapes the
5070 whole variable escapes. */
5071 lhs.type = SCALAR;
5072 lhs.var = escaped_id;
5073 lhs.offset = 0;
5074 rhs.type = SCALAR;
5075 rhs.var = escaped_id;
5076 rhs.offset = UNKNOWN_OFFSET;
5077 process_constraint (new_constraint (lhs, rhs));
5079 /* *ESCAPED = NONLOCAL. This is true because we have to assume
5080 everything pointed to by escaped points to what global memory can
5081 point to. */
5082 lhs.type = DEREF;
5083 lhs.var = escaped_id;
5084 lhs.offset = 0;
5085 rhs.type = SCALAR;
5086 rhs.var = nonlocal_id;
5087 rhs.offset = 0;
5088 process_constraint (new_constraint (lhs, rhs));
5090 /* NONLOCAL = &NONLOCAL, NONLOCAL = &ESCAPED. This is true because
5091 global memory may point to global memory and escaped memory. */
5092 lhs.type = SCALAR;
5093 lhs.var = nonlocal_id;
5094 lhs.offset = 0;
5095 rhs.type = ADDRESSOF;
5096 rhs.var = nonlocal_id;
5097 rhs.offset = 0;
5098 process_constraint (new_constraint (lhs, rhs));
5099 rhs.type = ADDRESSOF;
5100 rhs.var = escaped_id;
5101 rhs.offset = 0;
5102 process_constraint (new_constraint (lhs, rhs));
5104 /* Create the CALLUSED variable, used to represent the set of call-used
5105 memory. */
5106 callused_tree = create_tmp_var_raw (ptr_type_node, "CALLUSED");
5107 var_callused = new_var_info (callused_tree, callused_id, "CALLUSED");
5108 insert_vi_for_tree (callused_tree, var_callused);
5109 var_callused->is_artificial_var = 1;
5110 var_callused->offset = 0;
5111 var_callused->size = ~0;
5112 var_callused->fullsize = ~0;
5113 var_callused->is_special_var = 0;
5114 VEC_safe_push (varinfo_t, heap, varmap, var_callused);
5116 /* CALLUSED = *CALLUSED, because call-used is may-deref'd at calls, etc. */
5117 lhs.type = SCALAR;
5118 lhs.var = callused_id;
5119 lhs.offset = 0;
5120 rhs.type = DEREF;
5121 rhs.var = callused_id;
5122 rhs.offset = 0;
5123 process_constraint (new_constraint (lhs, rhs));
5125 /* CALLUSED = CALLUSED + UNKNOWN, because if a sub-field is call-used the
5126 whole variable is call-used. */
5127 lhs.type = SCALAR;
5128 lhs.var = callused_id;
5129 lhs.offset = 0;
5130 rhs.type = SCALAR;
5131 rhs.var = callused_id;
5132 rhs.offset = UNKNOWN_OFFSET;
5133 process_constraint (new_constraint (lhs, rhs));
5135 /* Create the STOREDANYTHING variable, used to represent the set of
5136 variables stored to *ANYTHING. */
5137 storedanything_tree = create_tmp_var_raw (ptr_type_node, "STOREDANYTHING");
5138 var_storedanything = new_var_info (storedanything_tree, storedanything_id,
5139 "STOREDANYTHING");
5140 insert_vi_for_tree (storedanything_tree, var_storedanything);
5141 var_storedanything->is_artificial_var = 1;
5142 var_storedanything->offset = 0;
5143 var_storedanything->size = ~0;
5144 var_storedanything->fullsize = ~0;
5145 var_storedanything->is_special_var = 0;
5146 VEC_safe_push (varinfo_t, heap, varmap, var_storedanything);
5148 /* Create the INTEGER variable, used to represent that a variable points
5149 to what an INTEGER "points to". */
5150 integer_tree = create_tmp_var_raw (ptr_type_node, "INTEGER");
5151 var_integer = new_var_info (integer_tree, integer_id, "INTEGER");
5152 insert_vi_for_tree (integer_tree, var_integer);
5153 var_integer->is_artificial_var = 1;
5154 var_integer->size = ~0;
5155 var_integer->fullsize = ~0;
5156 var_integer->offset = 0;
5157 var_integer->next = NULL;
5158 var_integer->is_special_var = 1;
5159 VEC_safe_push (varinfo_t, heap, varmap, var_integer);
5161 /* INTEGER = ANYTHING, because we don't know where a dereference of
5162 a random integer will point to. */
5163 lhs.type = SCALAR;
5164 lhs.var = integer_id;
5165 lhs.offset = 0;
5166 rhs.type = ADDRESSOF;
5167 rhs.var = anything_id;
5168 rhs.offset = 0;
5169 process_constraint (new_constraint (lhs, rhs));
5172 /* Initialize things necessary to perform PTA */
5174 static void
5175 init_alias_vars (void)
5177 use_field_sensitive = (MAX_FIELDS_FOR_FIELD_SENSITIVE > 1);
5179 bitmap_obstack_initialize (&pta_obstack);
5180 bitmap_obstack_initialize (&oldpta_obstack);
5181 bitmap_obstack_initialize (&predbitmap_obstack);
5183 constraint_pool = create_alloc_pool ("Constraint pool",
5184 sizeof (struct constraint), 30);
5185 variable_info_pool = create_alloc_pool ("Variable info pool",
5186 sizeof (struct variable_info), 30);
5187 constraints = VEC_alloc (constraint_t, heap, 8);
5188 varmap = VEC_alloc (varinfo_t, heap, 8);
5189 vi_for_tree = pointer_map_create ();
5191 memset (&stats, 0, sizeof (stats));
5192 shared_bitmap_table = htab_create (511, shared_bitmap_hash,
5193 shared_bitmap_eq, free);
5194 init_base_vars ();
5197 /* Remove the REF and ADDRESS edges from GRAPH, as well as all the
5198 predecessor edges. */
5200 static void
5201 remove_preds_and_fake_succs (constraint_graph_t graph)
5203 unsigned int i;
5205 /* Clear the implicit ref and address nodes from the successor
5206 lists. */
5207 for (i = 0; i < FIRST_REF_NODE; i++)
5209 if (graph->succs[i])
5210 bitmap_clear_range (graph->succs[i], FIRST_REF_NODE,
5211 FIRST_REF_NODE * 2);
5214 /* Free the successor list for the non-ref nodes. */
5215 for (i = FIRST_REF_NODE; i < graph->size; i++)
5217 if (graph->succs[i])
5218 BITMAP_FREE (graph->succs[i]);
5221 /* Now reallocate the size of the successor list as, and blow away
5222 the predecessor bitmaps. */
5223 graph->size = VEC_length (varinfo_t, varmap);
5224 graph->succs = XRESIZEVEC (bitmap, graph->succs, graph->size);
5226 free (graph->implicit_preds);
5227 graph->implicit_preds = NULL;
5228 free (graph->preds);
5229 graph->preds = NULL;
5230 bitmap_obstack_release (&predbitmap_obstack);
5233 /* Initialize the heapvar for statement mapping. */
5235 static void
5236 init_alias_heapvars (void)
5238 if (!heapvar_for_stmt)
5239 heapvar_for_stmt = htab_create_ggc (11, tree_map_hash, tree_map_eq,
5240 NULL);
5243 /* Delete the heapvar for statement mapping. */
5245 void
5246 delete_alias_heapvars (void)
5248 if (heapvar_for_stmt)
5249 htab_delete (heapvar_for_stmt);
5250 heapvar_for_stmt = NULL;
5253 /* Create points-to sets for the current function. See the comments
5254 at the start of the file for an algorithmic overview. */
5256 static void
5257 compute_points_to_sets (void)
5259 struct scc_info *si;
5260 basic_block bb;
5261 unsigned i;
5263 timevar_push (TV_TREE_PTA);
5265 init_alias_vars ();
5266 init_alias_heapvars ();
5268 intra_create_variable_infos ();
5270 /* Now walk all statements and derive aliases. */
5271 FOR_EACH_BB (bb)
5273 gimple_stmt_iterator gsi;
5275 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
5277 gimple phi = gsi_stmt (gsi);
5279 if (is_gimple_reg (gimple_phi_result (phi)))
5280 find_func_aliases (phi);
5283 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
5285 gimple stmt = gsi_stmt (gsi);
5287 find_func_aliases (stmt);
5291 if (dump_file)
5293 fprintf (dump_file, "Points-to analysis\n\nConstraints:\n\n");
5294 dump_constraints (dump_file);
5297 if (dump_file)
5298 fprintf (dump_file,
5299 "\nCollapsing static cycles and doing variable "
5300 "substitution\n");
5302 init_graph (VEC_length (varinfo_t, varmap) * 2);
5304 if (dump_file)
5305 fprintf (dump_file, "Building predecessor graph\n");
5306 build_pred_graph ();
5308 if (dump_file)
5309 fprintf (dump_file, "Detecting pointer and location "
5310 "equivalences\n");
5311 si = perform_var_substitution (graph);
5313 if (dump_file)
5314 fprintf (dump_file, "Rewriting constraints and unifying "
5315 "variables\n");
5316 rewrite_constraints (graph, si);
5318 build_succ_graph ();
5319 free_var_substitution_info (si);
5321 if (dump_file && (dump_flags & TDF_GRAPH))
5322 dump_constraint_graph (dump_file);
5324 move_complex_constraints (graph);
5326 if (dump_file)
5327 fprintf (dump_file, "Uniting pointer but not location equivalent "
5328 "variables\n");
5329 unite_pointer_equivalences (graph);
5331 if (dump_file)
5332 fprintf (dump_file, "Finding indirect cycles\n");
5333 find_indirect_cycles (graph);
5335 /* Implicit nodes and predecessors are no longer necessary at this
5336 point. */
5337 remove_preds_and_fake_succs (graph);
5339 if (dump_file)
5340 fprintf (dump_file, "Solving graph\n");
5342 solve_graph (graph);
5344 if (dump_file)
5345 dump_sa_points_to_info (dump_file);
5347 /* Compute the points-to sets for ESCAPED and CALLUSED used for
5348 call-clobber analysis. */
5349 find_what_var_points_to (var_escaped, &cfun->gimple_df->escaped);
5350 find_what_var_points_to (var_callused, &cfun->gimple_df->callused);
5352 /* Make sure the ESCAPED solution (which is used as placeholder in
5353 other solutions) does not reference itself. This simplifies
5354 points-to solution queries. */
5355 cfun->gimple_df->escaped.escaped = 0;
5357 /* Compute the points-to sets for pointer SSA_NAMEs. */
5358 for (i = 0; i < num_ssa_names; ++i)
5360 tree ptr = ssa_name (i);
5361 if (ptr
5362 && POINTER_TYPE_P (TREE_TYPE (ptr)))
5363 find_what_p_points_to (ptr);
5366 timevar_pop (TV_TREE_PTA);
5368 have_alias_info = true;
5372 /* Delete created points-to sets. */
5374 static void
5375 delete_points_to_sets (void)
5377 unsigned int i;
5379 htab_delete (shared_bitmap_table);
5380 if (dump_file && (dump_flags & TDF_STATS))
5381 fprintf (dump_file, "Points to sets created:%d\n",
5382 stats.points_to_sets_created);
5384 pointer_map_destroy (vi_for_tree);
5385 bitmap_obstack_release (&pta_obstack);
5386 VEC_free (constraint_t, heap, constraints);
5388 for (i = 0; i < graph->size; i++)
5389 VEC_free (constraint_t, heap, graph->complex[i]);
5390 free (graph->complex);
5392 free (graph->rep);
5393 free (graph->succs);
5394 free (graph->pe);
5395 free (graph->pe_rep);
5396 free (graph->indirect_cycles);
5397 free (graph);
5399 VEC_free (varinfo_t, heap, varmap);
5400 free_alloc_pool (variable_info_pool);
5401 free_alloc_pool (constraint_pool);
5402 have_alias_info = false;
5406 /* Compute points-to information for every SSA_NAME pointer in the
5407 current function and compute the transitive closure of escaped
5408 variables to re-initialize the call-clobber states of local variables. */
5410 unsigned int
5411 compute_may_aliases (void)
5413 /* For each pointer P_i, determine the sets of variables that P_i may
5414 point-to. Compute the reachability set of escaped and call-used
5415 variables. */
5416 compute_points_to_sets ();
5418 /* Debugging dumps. */
5419 if (dump_file)
5421 dump_alias_info (dump_file);
5423 if (dump_flags & TDF_DETAILS)
5424 dump_referenced_vars (dump_file);
5427 /* Deallocate memory used by aliasing data structures and the internal
5428 points-to solution. */
5429 delete_points_to_sets ();
5431 gcc_assert (!need_ssa_update_p (cfun));
5433 return 0;
5436 static bool
5437 gate_tree_pta (void)
5439 return flag_tree_pta;
5442 /* A dummy pass to cause points-to information to be computed via
5443 TODO_rebuild_alias. */
5445 struct gimple_opt_pass pass_build_alias =
5448 GIMPLE_PASS,
5449 "alias", /* name */
5450 gate_tree_pta, /* gate */
5451 NULL, /* execute */
5452 NULL, /* sub */
5453 NULL, /* next */
5454 0, /* static_pass_number */
5455 TV_NONE, /* tv_id */
5456 PROP_cfg | PROP_ssa, /* properties_required */
5457 0, /* properties_provided */
5458 0, /* properties_destroyed */
5459 0, /* todo_flags_start */
5460 TODO_rebuild_alias | TODO_dump_func /* todo_flags_finish */
5464 /* A dummy pass to cause points-to information to be computed via
5465 TODO_rebuild_alias. */
5467 struct gimple_opt_pass pass_build_ealias =
5470 GIMPLE_PASS,
5471 "ealias", /* name */
5472 gate_tree_pta, /* gate */
5473 NULL, /* execute */
5474 NULL, /* sub */
5475 NULL, /* next */
5476 0, /* static_pass_number */
5477 TV_NONE, /* tv_id */
5478 PROP_cfg | PROP_ssa, /* properties_required */
5479 0, /* properties_provided */
5480 0, /* properties_destroyed */
5481 0, /* todo_flags_start */
5482 TODO_rebuild_alias | TODO_dump_func /* todo_flags_finish */
5487 /* Return true if we should execute IPA PTA. */
5488 static bool
5489 gate_ipa_pta (void)
5491 return (flag_ipa_pta
5492 /* Don't bother doing anything if the program has errors. */
5493 && !(errorcount || sorrycount));
5496 /* Execute the driver for IPA PTA. */
5497 static unsigned int
5498 ipa_pta_execute (void)
5500 struct cgraph_node *node;
5501 struct scc_info *si;
5503 in_ipa_mode = 1;
5504 init_alias_heapvars ();
5505 init_alias_vars ();
5507 for (node = cgraph_nodes; node; node = node->next)
5509 unsigned int varid;
5511 varid = create_function_info_for (node->decl,
5512 cgraph_node_name (node));
5513 if (node->local.externally_visible)
5515 varinfo_t fi = get_varinfo (varid);
5516 for (; fi; fi = fi->next)
5517 make_constraint_from (fi, anything_id);
5520 for (node = cgraph_nodes; node; node = node->next)
5522 if (node->analyzed)
5524 struct function *func = DECL_STRUCT_FUNCTION (node->decl);
5525 basic_block bb;
5526 tree old_func_decl = current_function_decl;
5527 if (dump_file)
5528 fprintf (dump_file,
5529 "Generating constraints for %s\n",
5530 cgraph_node_name (node));
5531 push_cfun (func);
5532 current_function_decl = node->decl;
5534 FOR_EACH_BB_FN (bb, func)
5536 gimple_stmt_iterator gsi;
5538 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
5539 gsi_next (&gsi))
5541 gimple phi = gsi_stmt (gsi);
5543 if (is_gimple_reg (gimple_phi_result (phi)))
5544 find_func_aliases (phi);
5547 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
5548 find_func_aliases (gsi_stmt (gsi));
5550 current_function_decl = old_func_decl;
5551 pop_cfun ();
5553 else
5555 /* Make point to anything. */
5559 if (dump_file)
5561 fprintf (dump_file, "Points-to analysis\n\nConstraints:\n\n");
5562 dump_constraints (dump_file);
5565 if (dump_file)
5566 fprintf (dump_file,
5567 "\nCollapsing static cycles and doing variable "
5568 "substitution:\n");
5570 init_graph (VEC_length (varinfo_t, varmap) * 2);
5571 build_pred_graph ();
5572 si = perform_var_substitution (graph);
5573 rewrite_constraints (graph, si);
5575 build_succ_graph ();
5576 free_var_substitution_info (si);
5577 move_complex_constraints (graph);
5578 unite_pointer_equivalences (graph);
5579 find_indirect_cycles (graph);
5581 /* Implicit nodes and predecessors are no longer necessary at this
5582 point. */
5583 remove_preds_and_fake_succs (graph);
5585 if (dump_file)
5586 fprintf (dump_file, "\nSolving graph\n");
5588 solve_graph (graph);
5590 if (dump_file)
5591 dump_sa_points_to_info (dump_file);
5593 in_ipa_mode = 0;
5594 delete_alias_heapvars ();
5595 delete_points_to_sets ();
5596 return 0;
5599 struct simple_ipa_opt_pass pass_ipa_pta =
5602 SIMPLE_IPA_PASS,
5603 "pta", /* name */
5604 gate_ipa_pta, /* gate */
5605 ipa_pta_execute, /* execute */
5606 NULL, /* sub */
5607 NULL, /* next */
5608 0, /* static_pass_number */
5609 TV_IPA_PTA, /* tv_id */
5610 0, /* properties_required */
5611 0, /* properties_provided */
5612 0, /* properties_destroyed */
5613 0, /* todo_flags_start */
5614 TODO_update_ssa /* todo_flags_finish */
5619 #include "gt-tree-ssa-structalias.h"