1 /* Tree based points-to analysis
2 Copyright (C) 2005, 2006, 2007 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/>. */
23 #include "coretypes.h"
31 #include "hard-reg-set.h"
32 #include "basic-block.h"
35 #include "diagnostic.h"
38 #include "tree-flow.h"
39 #include "tree-inline.h"
42 #include "tree-gimple.h"
46 #include "tree-pass.h"
48 #include "alloc-pool.h"
49 #include "splay-tree.h"
51 #include "tree-ssa-structalias.h"
54 #include "pointer-set.h"
56 /* The idea behind this analyzer is to generate set constraints from the
57 program, then solve the resulting constraints in order to generate the
60 Set constraints are a way of modeling program analysis problems that
61 involve sets. They consist of an inclusion constraint language,
62 describing the variables (each variable is a set) and operations that
63 are involved on the variables, and a set of rules that derive facts
64 from these operations. To solve a system of set constraints, you derive
65 all possible facts under the rules, which gives you the correct sets
68 See "Efficient Field-sensitive pointer analysis for C" by "David
69 J. Pearce and Paul H. J. Kelly and Chris Hankin, at
70 http://citeseer.ist.psu.edu/pearce04efficient.html
72 Also see "Ultra-fast Aliasing Analysis using CLA: A Million Lines
73 of C Code in a Second" by ""Nevin Heintze and Olivier Tardieu" at
74 http://citeseer.ist.psu.edu/heintze01ultrafast.html
76 There are three types of real constraint expressions, DEREF,
77 ADDRESSOF, and SCALAR. Each constraint expression consists
78 of a constraint type, a variable, and an offset.
80 SCALAR is a constraint expression type used to represent x, whether
81 it appears on the LHS or the RHS of a statement.
82 DEREF is a constraint expression type used to represent *x, whether
83 it appears on the LHS or the RHS of a statement.
84 ADDRESSOF is a constraint expression used to represent &x, whether
85 it appears on the LHS or the RHS of a statement.
87 Each pointer variable in the program is assigned an integer id, and
88 each field of a structure variable is assigned an integer id as well.
90 Structure variables are linked to their list of fields through a "next
91 field" in each variable that points to the next field in offset
93 Each variable for a structure field has
95 1. "size", that tells the size in bits of that field.
96 2. "fullsize, that tells the size in bits of the entire structure.
97 3. "offset", that tells the offset in bits from the beginning of the
98 structure to this field.
110 foo.a -> id 1, size 32, offset 0, fullsize 64, next foo.b
111 foo.b -> id 2, size 32, offset 32, fullsize 64, next NULL
112 bar -> id 3, size 32, offset 0, fullsize 32, next NULL
115 In order to solve the system of set constraints, the following is
118 1. Each constraint variable x has a solution set associated with it,
121 2. Constraints are separated into direct, copy, and complex.
122 Direct constraints are ADDRESSOF constraints that require no extra
123 processing, such as P = &Q
124 Copy constraints are those of the form P = Q.
125 Complex constraints are all the constraints involving dereferences
126 and offsets (including offsetted copies).
128 3. All direct constraints of the form P = &Q are processed, such
129 that Q is added to Sol(P)
131 4. All complex constraints for a given constraint variable are stored in a
132 linked list attached to that variable's node.
134 5. A directed graph is built out of the copy constraints. Each
135 constraint variable is a node in the graph, and an edge from
136 Q to P is added for each copy constraint of the form P = Q
138 6. The graph is then walked, and solution sets are
139 propagated along the copy edges, such that an edge from Q to P
140 causes Sol(P) <- Sol(P) union Sol(Q).
142 7. As we visit each node, all complex constraints associated with
143 that node are processed by adding appropriate copy edges to the graph, or the
144 appropriate variables to the solution set.
146 8. The process of walking the graph is iterated until no solution
149 Prior to walking the graph in steps 6 and 7, We perform static
150 cycle elimination on the constraint graph, as well
151 as off-line variable substitution.
153 TODO: Adding offsets to pointer-to-structures can be handled (IE not punted
154 on and turned into anything), but isn't. You can just see what offset
155 inside the pointed-to struct it's going to access.
157 TODO: Constant bounded arrays can be handled as if they were structs of the
158 same number of elements.
160 TODO: Modeling heap and incoming pointers becomes much better if we
161 add fields to them as we discover them, which we could do.
163 TODO: We could handle unions, but to be honest, it's probably not
164 worth the pain or slowdown. */
166 static GTY ((if_marked ("tree_map_marked_p"), param_is (struct tree_map)))
167 htab_t heapvar_for_stmt;
169 static bool use_field_sensitive = true;
170 static int in_ipa_mode = 0;
172 /* Used for predecessor bitmaps. */
173 static bitmap_obstack predbitmap_obstack;
175 /* Used for points-to sets. */
176 static bitmap_obstack pta_obstack;
178 /* Used for oldsolution members of variables. */
179 static bitmap_obstack oldpta_obstack;
181 /* Used for per-solver-iteration bitmaps. */
182 static bitmap_obstack iteration_obstack;
184 static unsigned int create_variable_info_for (tree, const char *);
185 typedef struct constraint_graph *constraint_graph_t;
186 static void unify_nodes (constraint_graph_t, unsigned int, unsigned int, bool);
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) \
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;
209 /* ID of this variable */
212 /* Name of this variable */
215 /* Tree that this variable is associated with. */
218 /* Offset of this variable, in bits, from the base variable */
219 unsigned HOST_WIDE_INT offset;
221 /* Size of the variable, in bits. */
222 unsigned HOST_WIDE_INT size;
224 /* Full size of the base variable, in bits. */
225 unsigned HOST_WIDE_INT fullsize;
227 /* A link to the variable for the next field in this structure. */
228 struct variable_info *next;
230 /* True if the variable is directly the target of a dereference.
231 This is used to track which variables are *actually* dereferenced
232 so we can prune their points to listed. */
233 unsigned int directly_dereferenced:1;
235 /* True if this is a variable created by the constraint analysis, such as
236 heap variables and constraints we had to break up. */
237 unsigned int is_artificial_var:1;
239 /* True if this is a special variable whose solution set should not be
241 unsigned int is_special_var:1;
243 /* True for variables whose size is not known or variable. */
244 unsigned int is_unknown_size_var:1;
246 /* True for variables that have unions somewhere in them. */
247 unsigned int has_union:1;
249 /* True if this is a heap variable. */
250 unsigned int is_heap_var:1;
252 /* True if we may not use TBAA to prune references to this
253 variable. This is used for C++ placement new. */
254 unsigned int no_tbaa_pruning : 1;
256 /* Points-to set for this variable. */
259 /* Old points-to set for this variable. */
262 /* Variable id this was collapsed to due to type unsafety. This
263 should be unused completely after build_succ_graph, or something
265 struct variable_info *collapsed_to;
267 typedef struct variable_info *varinfo_t;
269 static varinfo_t first_vi_for_offset (varinfo_t, unsigned HOST_WIDE_INT);
271 /* Pool of variable info structures. */
272 static alloc_pool variable_info_pool;
274 DEF_VEC_P(varinfo_t);
276 DEF_VEC_ALLOC_P(varinfo_t, heap);
278 /* Table of variable info structures for constraint variables.
279 Indexed directly by variable info id. */
280 static VEC(varinfo_t,heap) *varmap;
282 /* Return the varmap element N */
284 static inline varinfo_t
285 get_varinfo (unsigned int n)
287 return VEC_index (varinfo_t, varmap, n);
290 /* Return the varmap element N, following the collapsed_to link. */
292 static inline varinfo_t
293 get_varinfo_fc (unsigned int n)
295 varinfo_t v = VEC_index (varinfo_t, varmap, n);
298 return v->collapsed_to;
302 /* Variable that represents the unknown pointer. */
303 static varinfo_t var_anything;
304 static tree anything_tree;
305 static unsigned int anything_id;
307 /* Variable that represents the NULL pointer. */
308 static varinfo_t var_nothing;
309 static tree nothing_tree;
310 static unsigned int nothing_id;
312 /* Variable that represents read only memory. */
313 static varinfo_t var_readonly;
314 static tree readonly_tree;
315 static unsigned int readonly_id;
317 /* Variable that represents integers. This is used for when people do things
319 static varinfo_t var_integer;
320 static tree integer_tree;
321 static unsigned int integer_id;
323 /* Lookup a heap var for FROM, and return it if we find one. */
326 heapvar_lookup (tree from)
328 struct tree_map *h, in;
331 h = (struct tree_map *) htab_find_with_hash (heapvar_for_stmt, &in,
332 htab_hash_pointer (from));
338 /* Insert a mapping FROM->TO in the heap var for statement
342 heapvar_insert (tree from, tree to)
347 h = GGC_NEW (struct tree_map);
348 h->hash = htab_hash_pointer (from);
351 loc = htab_find_slot_with_hash (heapvar_for_stmt, h, h->hash, INSERT);
352 *(struct tree_map **) loc = h;
355 /* Return a new variable info structure consisting for a variable
356 named NAME, and using constraint graph node NODE. */
359 new_var_info (tree t, unsigned int id, const char *name)
361 varinfo_t ret = (varinfo_t) pool_alloc (variable_info_pool);
367 ret->directly_dereferenced = false;
368 ret->is_artificial_var = false;
369 ret->is_heap_var = false;
370 ret->is_special_var = false;
371 ret->is_unknown_size_var = false;
372 ret->has_union = false;
374 if (TREE_CODE (var) == SSA_NAME)
375 var = SSA_NAME_VAR (var);
376 ret->no_tbaa_pruning = (DECL_P (var)
377 && POINTER_TYPE_P (TREE_TYPE (var))
378 && DECL_NO_TBAA_P (var));
379 ret->solution = BITMAP_ALLOC (&pta_obstack);
380 ret->oldsolution = BITMAP_ALLOC (&oldpta_obstack);
382 ret->collapsed_to = NULL;
386 typedef enum {SCALAR, DEREF, ADDRESSOF} constraint_expr_type;
388 /* An expression that appears in a constraint. */
390 struct constraint_expr
392 /* Constraint type. */
393 constraint_expr_type type;
395 /* Variable we are referring to in the constraint. */
398 /* Offset, in bits, of this constraint from the beginning of
399 variables it ends up referring to.
401 IOW, in a deref constraint, we would deref, get the result set,
402 then add OFFSET to each member. */
403 unsigned HOST_WIDE_INT offset;
406 typedef struct constraint_expr ce_s;
408 DEF_VEC_ALLOC_O(ce_s, heap);
409 static void get_constraint_for (tree, VEC(ce_s, heap) **);
410 static void do_deref (VEC (ce_s, heap) **);
412 /* Our set constraints are made up of two constraint expressions, one
415 As described in the introduction, our set constraints each represent an
416 operation between set valued variables.
420 struct constraint_expr lhs;
421 struct constraint_expr rhs;
424 /* List of constraints that we use to build the constraint graph from. */
426 static VEC(constraint_t,heap) *constraints;
427 static alloc_pool constraint_pool;
431 DEF_VEC_ALLOC_I(int, heap);
433 /* The constraint graph is represented as an array of bitmaps
434 containing successor nodes. */
436 struct constraint_graph
438 /* Size of this graph, which may be different than the number of
439 nodes in the variable map. */
442 /* Explicit successors of each node. */
445 /* Implicit predecessors of each node (Used for variable
447 bitmap *implicit_preds;
449 /* Explicit predecessors of each node (Used for variable substitution). */
452 /* Indirect cycle representatives, or -1 if the node has no indirect
454 int *indirect_cycles;
456 /* Representative node for a node. rep[a] == a unless the node has
460 /* Equivalence class representative for a label. This is used for
461 variable substitution. */
464 /* Pointer equivalence label for a node. All nodes with the same
465 pointer equivalence label can be unified together at some point
466 (either during constraint optimization or after the constraint
470 /* Pointer equivalence representative for a label. This is used to
471 handle nodes that are pointer equivalent but not location
472 equivalent. We can unite these once the addressof constraints
473 are transformed into initial points-to sets. */
476 /* Pointer equivalence label for each node, used during variable
478 unsigned int *pointer_label;
480 /* Location equivalence label for each node, used during location
481 equivalence finding. */
482 unsigned int *loc_label;
484 /* Pointed-by set for each node, used during location equivalence
485 finding. This is pointed-by rather than pointed-to, because it
486 is constructed using the predecessor graph. */
489 /* Points to sets for pointer equivalence. This is *not* the actual
490 points-to sets for nodes. */
493 /* Bitmap of nodes where the bit is set if the node is a direct
494 node. Used for variable substitution. */
495 sbitmap direct_nodes;
497 /* Bitmap of nodes where the bit is set if the node is address
498 taken. Used for variable substitution. */
499 bitmap address_taken;
501 /* True if points_to bitmap for this node is stored in the hash
505 /* Number of incoming edges remaining to be processed by pointer
507 Used for variable substitution. */
508 unsigned int *number_incoming;
511 /* Vector of complex constraints for each graph node. Complex
512 constraints are those involving dereferences or offsets that are
514 VEC(constraint_t,heap) **complex;
517 static constraint_graph_t graph;
519 /* During variable substitution and the offline version of indirect
520 cycle finding, we create nodes to represent dereferences and
521 address taken constraints. These represent where these start and
523 #define FIRST_REF_NODE (VEC_length (varinfo_t, varmap))
524 #define LAST_REF_NODE (FIRST_REF_NODE + (FIRST_REF_NODE - 1))
526 /* Return the representative node for NODE, if NODE has been unioned
528 This function performs path compression along the way to finding
529 the representative. */
532 find (unsigned int node)
534 gcc_assert (node < graph->size);
535 if (graph->rep[node] != node)
536 return graph->rep[node] = find (graph->rep[node]);
540 /* Union the TO and FROM nodes to the TO nodes.
541 Note that at some point in the future, we may want to do
542 union-by-rank, in which case we are going to have to return the
543 node we unified to. */
546 unite (unsigned int to, unsigned int from)
548 gcc_assert (to < graph->size && from < graph->size);
549 if (to != from && graph->rep[from] != to)
551 graph->rep[from] = to;
557 /* Create a new constraint consisting of LHS and RHS expressions. */
560 new_constraint (const struct constraint_expr lhs,
561 const struct constraint_expr rhs)
563 constraint_t ret = (constraint_t) pool_alloc (constraint_pool);
569 /* Print out constraint C to FILE. */
572 dump_constraint (FILE *file, constraint_t c)
574 if (c->lhs.type == ADDRESSOF)
576 else if (c->lhs.type == DEREF)
578 fprintf (file, "%s", get_varinfo_fc (c->lhs.var)->name);
579 if (c->lhs.offset != 0)
580 fprintf (file, " + " HOST_WIDE_INT_PRINT_DEC, c->lhs.offset);
581 fprintf (file, " = ");
582 if (c->rhs.type == ADDRESSOF)
584 else if (c->rhs.type == DEREF)
586 fprintf (file, "%s", get_varinfo_fc (c->rhs.var)->name);
587 if (c->rhs.offset != 0)
588 fprintf (file, " + " HOST_WIDE_INT_PRINT_DEC, c->rhs.offset);
589 fprintf (file, "\n");
592 /* Print out constraint C to stderr. */
595 debug_constraint (constraint_t c)
597 dump_constraint (stderr, c);
600 /* Print out all constraints to FILE */
603 dump_constraints (FILE *file)
607 for (i = 0; VEC_iterate (constraint_t, constraints, i, c); i++)
608 dump_constraint (file, c);
611 /* Print out all constraints to stderr. */
614 debug_constraints (void)
616 dump_constraints (stderr);
621 The solver is a simple worklist solver, that works on the following
624 sbitmap changed_nodes = all zeroes;
626 For each node that is not already collapsed:
628 set bit in changed nodes
630 while (changed_count > 0)
632 compute topological ordering for constraint graph
634 find and collapse cycles in the constraint graph (updating
635 changed if necessary)
637 for each node (n) in the graph in topological order:
640 Process each complex constraint associated with the node,
641 updating changed if necessary.
643 For each outgoing edge from n, propagate the solution from n to
644 the destination of the edge, updating changed as necessary.
648 /* Return true if two constraint expressions A and B are equal. */
651 constraint_expr_equal (struct constraint_expr a, struct constraint_expr b)
653 return a.type == b.type && a.var == b.var && a.offset == b.offset;
656 /* Return true if constraint expression A is less than constraint expression
657 B. This is just arbitrary, but consistent, in order to give them an
661 constraint_expr_less (struct constraint_expr a, struct constraint_expr b)
663 if (a.type == b.type)
666 return a.offset < b.offset;
668 return a.var < b.var;
671 return a.type < b.type;
674 /* Return true if constraint A is less than constraint B. This is just
675 arbitrary, but consistent, in order to give them an ordering. */
678 constraint_less (const constraint_t a, const constraint_t b)
680 if (constraint_expr_less (a->lhs, b->lhs))
682 else if (constraint_expr_less (b->lhs, a->lhs))
685 return constraint_expr_less (a->rhs, b->rhs);
688 /* Return true if two constraints A and B are equal. */
691 constraint_equal (struct constraint a, struct constraint b)
693 return constraint_expr_equal (a.lhs, b.lhs)
694 && constraint_expr_equal (a.rhs, b.rhs);
698 /* Find a constraint LOOKFOR in the sorted constraint vector VEC */
701 constraint_vec_find (VEC(constraint_t,heap) *vec,
702 struct constraint lookfor)
710 place = VEC_lower_bound (constraint_t, vec, &lookfor, constraint_less);
711 if (place >= VEC_length (constraint_t, vec))
713 found = VEC_index (constraint_t, vec, place);
714 if (!constraint_equal (*found, lookfor))
719 /* Union two constraint vectors, TO and FROM. Put the result in TO. */
722 constraint_set_union (VEC(constraint_t,heap) **to,
723 VEC(constraint_t,heap) **from)
728 for (i = 0; VEC_iterate (constraint_t, *from, i, c); i++)
730 if (constraint_vec_find (*to, *c) == NULL)
732 unsigned int place = VEC_lower_bound (constraint_t, *to, c,
734 VEC_safe_insert (constraint_t, heap, *to, place, c);
739 /* Take a solution set SET, add OFFSET to each member of the set, and
740 overwrite SET with the result when done. */
743 solution_set_add (bitmap set, unsigned HOST_WIDE_INT offset)
745 bitmap result = BITMAP_ALLOC (&iteration_obstack);
749 EXECUTE_IF_SET_IN_BITMAP (set, 0, i, bi)
751 /* If this is a properly sized variable, only add offset if it's
752 less than end. Otherwise, it is globbed to a single
755 if ((get_varinfo (i)->offset + offset) < get_varinfo (i)->fullsize)
757 unsigned HOST_WIDE_INT fieldoffset = get_varinfo (i)->offset + offset;
758 varinfo_t v = first_vi_for_offset (get_varinfo (i), fieldoffset);
761 bitmap_set_bit (result, v->id);
763 else if (get_varinfo (i)->is_artificial_var
764 || get_varinfo (i)->has_union
765 || get_varinfo (i)->is_unknown_size_var)
767 bitmap_set_bit (result, i);
771 bitmap_copy (set, result);
772 BITMAP_FREE (result);
775 /* Union solution sets TO and FROM, and add INC to each member of FROM in the
779 set_union_with_increment (bitmap to, bitmap from, unsigned HOST_WIDE_INT inc)
782 return bitmap_ior_into (to, from);
788 tmp = BITMAP_ALLOC (&iteration_obstack);
789 bitmap_copy (tmp, from);
790 solution_set_add (tmp, inc);
791 res = bitmap_ior_into (to, tmp);
797 /* Insert constraint C into the list of complex constraints for graph
801 insert_into_complex (constraint_graph_t graph,
802 unsigned int var, constraint_t c)
804 VEC (constraint_t, heap) *complex = graph->complex[var];
805 unsigned int place = VEC_lower_bound (constraint_t, complex, c,
808 /* Only insert constraints that do not already exist. */
809 if (place >= VEC_length (constraint_t, complex)
810 || !constraint_equal (*c, *VEC_index (constraint_t, complex, place)))
811 VEC_safe_insert (constraint_t, heap, graph->complex[var], place, c);
815 /* Condense two variable nodes into a single variable node, by moving
816 all associated info from SRC to TO. */
819 merge_node_constraints (constraint_graph_t graph, unsigned int to,
825 gcc_assert (find (from) == to);
827 /* Move all complex constraints from src node into to node */
828 for (i = 0; VEC_iterate (constraint_t, graph->complex[from], i, c); i++)
830 /* In complex constraints for node src, we may have either
831 a = *src, and *src = a, or an offseted constraint which are
832 always added to the rhs node's constraints. */
834 if (c->rhs.type == DEREF)
836 else if (c->lhs.type == DEREF)
841 constraint_set_union (&graph->complex[to], &graph->complex[from]);
842 VEC_free (constraint_t, heap, graph->complex[from]);
843 graph->complex[from] = NULL;
847 /* Remove edges involving NODE from GRAPH. */
850 clear_edges_for_node (constraint_graph_t graph, unsigned int node)
852 if (graph->succs[node])
853 BITMAP_FREE (graph->succs[node]);
856 /* Merge GRAPH nodes FROM and TO into node TO. */
859 merge_graph_nodes (constraint_graph_t graph, unsigned int to,
862 if (graph->indirect_cycles[from] != -1)
864 /* If we have indirect cycles with the from node, and we have
865 none on the to node, the to node has indirect cycles from the
866 from node now that they are unified.
867 If indirect cycles exist on both, unify the nodes that they
868 are in a cycle with, since we know they are in a cycle with
870 if (graph->indirect_cycles[to] == -1)
871 graph->indirect_cycles[to] = graph->indirect_cycles[from];
874 /* Merge all the successor edges. */
875 if (graph->succs[from])
877 if (!graph->succs[to])
878 graph->succs[to] = BITMAP_ALLOC (&pta_obstack);
879 bitmap_ior_into (graph->succs[to],
883 clear_edges_for_node (graph, from);
887 /* Add an indirect graph edge to GRAPH, going from TO to FROM if
888 it doesn't exist in the graph already. */
891 add_implicit_graph_edge (constraint_graph_t graph, unsigned int to,
897 if (!graph->implicit_preds[to])
898 graph->implicit_preds[to] = BITMAP_ALLOC (&predbitmap_obstack);
900 if (!bitmap_bit_p (graph->implicit_preds[to], from))
902 stats.num_implicit_edges++;
903 bitmap_set_bit (graph->implicit_preds[to], from);
907 /* Add a predecessor graph edge to GRAPH, going from TO to FROM if
908 it doesn't exist in the graph already.
909 Return false if the edge already existed, true otherwise. */
912 add_pred_graph_edge (constraint_graph_t graph, unsigned int to,
915 if (!graph->preds[to])
916 graph->preds[to] = BITMAP_ALLOC (&predbitmap_obstack);
917 if (!bitmap_bit_p (graph->preds[to], from))
918 bitmap_set_bit (graph->preds[to], from);
921 /* Add a graph edge to GRAPH, going from FROM to TO if
922 it doesn't exist in the graph already.
923 Return false if the edge already existed, true otherwise. */
926 add_graph_edge (constraint_graph_t graph, unsigned int to,
937 if (!graph->succs[from])
938 graph->succs[from] = BITMAP_ALLOC (&pta_obstack);
939 if (!bitmap_bit_p (graph->succs[from], to))
942 if (to < FIRST_REF_NODE && from < FIRST_REF_NODE)
944 bitmap_set_bit (graph->succs[from], to);
951 /* Return true if {DEST.SRC} is an existing graph edge in GRAPH. */
954 valid_graph_edge (constraint_graph_t graph, unsigned int src,
957 return (graph->succs[dest]
958 && bitmap_bit_p (graph->succs[dest], src));
961 /* Initialize the constraint graph structure to contain SIZE nodes. */
964 init_graph (unsigned int size)
968 graph = XCNEW (struct constraint_graph);
970 graph->succs = XCNEWVEC (bitmap, graph->size);
971 graph->indirect_cycles = XNEWVEC (int, graph->size);
972 graph->rep = XNEWVEC (unsigned int, graph->size);
973 graph->complex = XCNEWVEC (VEC(constraint_t, heap) *, size);
974 graph->pe = XCNEWVEC (unsigned int, graph->size);
975 graph->pe_rep = XNEWVEC (int, graph->size);
977 for (j = 0; j < graph->size; j++)
980 graph->pe_rep[j] = -1;
981 graph->indirect_cycles[j] = -1;
985 /* Build the constraint graph, adding only predecessor edges right now. */
988 build_pred_graph (void)
994 graph->implicit_preds = XCNEWVEC (bitmap, graph->size);
995 graph->preds = XCNEWVEC (bitmap, graph->size);
996 graph->pointer_label = XCNEWVEC (unsigned int, graph->size);
997 graph->loc_label = XCNEWVEC (unsigned int, graph->size);
998 graph->pointed_by = XCNEWVEC (bitmap, graph->size);
999 graph->points_to = XCNEWVEC (bitmap, graph->size);
1000 graph->eq_rep = XNEWVEC (int, graph->size);
1001 graph->direct_nodes = sbitmap_alloc (graph->size);
1002 graph->pt_used = sbitmap_alloc (graph->size);
1003 graph->address_taken = BITMAP_ALLOC (&predbitmap_obstack);
1004 graph->number_incoming = XCNEWVEC (unsigned int, graph->size);
1005 sbitmap_zero (graph->direct_nodes);
1006 sbitmap_zero (graph->pt_used);
1008 for (j = 0; j < FIRST_REF_NODE; j++)
1010 if (!get_varinfo (j)->is_special_var)
1011 SET_BIT (graph->direct_nodes, j);
1014 for (j = 0; j < graph->size; j++)
1015 graph->eq_rep[j] = -1;
1017 for (j = 0; j < VEC_length (varinfo_t, varmap); j++)
1018 graph->indirect_cycles[j] = -1;
1020 for (i = 0; VEC_iterate (constraint_t, constraints, i, c); i++)
1022 struct constraint_expr lhs = c->lhs;
1023 struct constraint_expr rhs = c->rhs;
1024 unsigned int lhsvar = get_varinfo_fc (lhs.var)->id;
1025 unsigned int rhsvar = get_varinfo_fc (rhs.var)->id;
1027 if (lhs.type == DEREF)
1030 if (rhs.offset == 0 && lhs.offset == 0 && rhs.type == SCALAR)
1031 add_pred_graph_edge (graph, FIRST_REF_NODE + lhsvar, rhsvar);
1033 else if (rhs.type == DEREF)
1036 if (rhs.offset == 0 && lhs.offset == 0 && lhs.type == SCALAR)
1037 add_pred_graph_edge (graph, lhsvar, FIRST_REF_NODE + rhsvar);
1039 RESET_BIT (graph->direct_nodes, lhsvar);
1041 else if (rhs.type == ADDRESSOF)
1044 if (graph->points_to[lhsvar] == NULL)
1045 graph->points_to[lhsvar] = BITMAP_ALLOC (&predbitmap_obstack);
1046 bitmap_set_bit (graph->points_to[lhsvar], rhsvar);
1048 if (graph->pointed_by[rhsvar] == NULL)
1049 graph->pointed_by[rhsvar] = BITMAP_ALLOC (&predbitmap_obstack);
1050 bitmap_set_bit (graph->pointed_by[rhsvar], lhsvar);
1052 /* Implicitly, *x = y */
1053 add_implicit_graph_edge (graph, FIRST_REF_NODE + lhsvar, rhsvar);
1055 RESET_BIT (graph->direct_nodes, rhsvar);
1056 bitmap_set_bit (graph->address_taken, rhsvar);
1058 else if (lhsvar > anything_id
1059 && lhsvar != rhsvar && lhs.offset == 0 && rhs.offset == 0)
1062 add_pred_graph_edge (graph, lhsvar, rhsvar);
1063 /* Implicitly, *x = *y */
1064 add_implicit_graph_edge (graph, FIRST_REF_NODE + lhsvar,
1065 FIRST_REF_NODE + rhsvar);
1067 else if (lhs.offset != 0 || rhs.offset != 0)
1069 if (rhs.offset != 0)
1070 RESET_BIT (graph->direct_nodes, lhs.var);
1071 else if (lhs.offset != 0)
1072 RESET_BIT (graph->direct_nodes, rhs.var);
1077 /* Build the constraint graph, adding successor edges. */
1080 build_succ_graph (void)
1085 for (i = 0; VEC_iterate (constraint_t, constraints, i, c); i++)
1087 struct constraint_expr lhs;
1088 struct constraint_expr rhs;
1089 unsigned int lhsvar;
1090 unsigned int rhsvar;
1097 lhsvar = find (get_varinfo_fc (lhs.var)->id);
1098 rhsvar = find (get_varinfo_fc (rhs.var)->id);
1100 if (lhs.type == DEREF)
1102 if (rhs.offset == 0 && lhs.offset == 0 && rhs.type == SCALAR)
1103 add_graph_edge (graph, FIRST_REF_NODE + lhsvar, rhsvar);
1105 else if (rhs.type == DEREF)
1107 if (rhs.offset == 0 && lhs.offset == 0 && lhs.type == SCALAR)
1108 add_graph_edge (graph, lhsvar, FIRST_REF_NODE + rhsvar);
1110 else if (rhs.type == ADDRESSOF)
1113 gcc_assert (find (get_varinfo_fc (rhs.var)->id)
1114 == get_varinfo_fc (rhs.var)->id);
1115 bitmap_set_bit (get_varinfo (lhsvar)->solution, rhsvar);
1117 else if (lhsvar > anything_id
1118 && lhsvar != rhsvar && lhs.offset == 0 && rhs.offset == 0)
1120 add_graph_edge (graph, lhsvar, rhsvar);
1126 /* Changed variables on the last iteration. */
1127 static unsigned int changed_count;
1128 static sbitmap changed;
1130 DEF_VEC_I(unsigned);
1131 DEF_VEC_ALLOC_I(unsigned,heap);
1134 /* Strongly Connected Component visitation info. */
1141 unsigned int *node_mapping;
1143 VEC(unsigned,heap) *scc_stack;
1147 /* Recursive routine to find strongly connected components in GRAPH.
1148 SI is the SCC info to store the information in, and N is the id of current
1149 graph node we are processing.
1151 This is Tarjan's strongly connected component finding algorithm, as
1152 modified by Nuutila to keep only non-root nodes on the stack.
1153 The algorithm can be found in "On finding the strongly connected
1154 connected components in a directed graph" by Esko Nuutila and Eljas
1155 Soisalon-Soininen, in Information Processing Letters volume 49,
1156 number 1, pages 9-14. */
1159 scc_visit (constraint_graph_t graph, struct scc_info *si, unsigned int n)
1163 unsigned int my_dfs;
1165 SET_BIT (si->visited, n);
1166 si->dfs[n] = si->current_index ++;
1167 my_dfs = si->dfs[n];
1169 /* Visit all the successors. */
1170 EXECUTE_IF_IN_NONNULL_BITMAP (graph->succs[n], 0, i, bi)
1174 if (i > LAST_REF_NODE)
1178 if (TEST_BIT (si->deleted, w))
1181 if (!TEST_BIT (si->visited, w))
1182 scc_visit (graph, si, w);
1184 unsigned int t = find (w);
1185 unsigned int nnode = find (n);
1186 gcc_assert (nnode == n);
1188 if (si->dfs[t] < si->dfs[nnode])
1189 si->dfs[n] = si->dfs[t];
1193 /* See if any components have been identified. */
1194 if (si->dfs[n] == my_dfs)
1196 if (VEC_length (unsigned, si->scc_stack) > 0
1197 && si->dfs[VEC_last (unsigned, si->scc_stack)] >= my_dfs)
1199 bitmap scc = BITMAP_ALLOC (NULL);
1200 bool have_ref_node = n >= FIRST_REF_NODE;
1201 unsigned int lowest_node;
1204 bitmap_set_bit (scc, n);
1206 while (VEC_length (unsigned, si->scc_stack) != 0
1207 && si->dfs[VEC_last (unsigned, si->scc_stack)] >= my_dfs)
1209 unsigned int w = VEC_pop (unsigned, si->scc_stack);
1211 bitmap_set_bit (scc, w);
1212 if (w >= FIRST_REF_NODE)
1213 have_ref_node = true;
1216 lowest_node = bitmap_first_set_bit (scc);
1217 gcc_assert (lowest_node < FIRST_REF_NODE);
1219 /* Collapse the SCC nodes into a single node, and mark the
1221 EXECUTE_IF_SET_IN_BITMAP (scc, 0, i, bi)
1223 if (i < FIRST_REF_NODE)
1225 if (unite (lowest_node, i))
1226 unify_nodes (graph, lowest_node, i, false);
1230 unite (lowest_node, i);
1231 graph->indirect_cycles[i - FIRST_REF_NODE] = lowest_node;
1235 SET_BIT (si->deleted, n);
1238 VEC_safe_push (unsigned, heap, si->scc_stack, n);
1241 /* Unify node FROM into node TO, updating the changed count if
1242 necessary when UPDATE_CHANGED is true. */
1245 unify_nodes (constraint_graph_t graph, unsigned int to, unsigned int from,
1246 bool update_changed)
1249 gcc_assert (to != from && find (to) == to);
1250 if (dump_file && (dump_flags & TDF_DETAILS))
1251 fprintf (dump_file, "Unifying %s to %s\n",
1252 get_varinfo (from)->name,
1253 get_varinfo (to)->name);
1256 stats.unified_vars_dynamic++;
1258 stats.unified_vars_static++;
1260 merge_graph_nodes (graph, to, from);
1261 merge_node_constraints (graph, to, from);
1263 if (get_varinfo (from)->no_tbaa_pruning)
1264 get_varinfo (to)->no_tbaa_pruning = true;
1266 /* Mark TO as changed if FROM was changed. If TO was already marked
1267 as changed, decrease the changed count. */
1269 if (update_changed && TEST_BIT (changed, from))
1271 RESET_BIT (changed, from);
1272 if (!TEST_BIT (changed, to))
1273 SET_BIT (changed, to);
1276 gcc_assert (changed_count > 0);
1280 if (get_varinfo (from)->solution)
1282 /* If the solution changes because of the merging, we need to mark
1283 the variable as changed. */
1284 if (bitmap_ior_into (get_varinfo (to)->solution,
1285 get_varinfo (from)->solution))
1287 if (update_changed && !TEST_BIT (changed, to))
1289 SET_BIT (changed, to);
1294 BITMAP_FREE (get_varinfo (from)->solution);
1295 BITMAP_FREE (get_varinfo (from)->oldsolution);
1297 if (stats.iterations > 0)
1299 BITMAP_FREE (get_varinfo (to)->oldsolution);
1300 get_varinfo (to)->oldsolution = BITMAP_ALLOC (&oldpta_obstack);
1303 if (valid_graph_edge (graph, to, to))
1305 if (graph->succs[to])
1306 bitmap_clear_bit (graph->succs[to], to);
1310 /* Information needed to compute the topological ordering of a graph. */
1314 /* sbitmap of visited nodes. */
1316 /* Array that stores the topological order of the graph, *in
1318 VEC(unsigned,heap) *topo_order;
1322 /* Initialize and return a topological info structure. */
1324 static struct topo_info *
1325 init_topo_info (void)
1327 size_t size = graph->size;
1328 struct topo_info *ti = XNEW (struct topo_info);
1329 ti->visited = sbitmap_alloc (size);
1330 sbitmap_zero (ti->visited);
1331 ti->topo_order = VEC_alloc (unsigned, heap, 1);
1336 /* Free the topological sort info pointed to by TI. */
1339 free_topo_info (struct topo_info *ti)
1341 sbitmap_free (ti->visited);
1342 VEC_free (unsigned, heap, ti->topo_order);
1346 /* Visit the graph in topological order, and store the order in the
1347 topo_info structure. */
1350 topo_visit (constraint_graph_t graph, struct topo_info *ti,
1356 SET_BIT (ti->visited, n);
1358 if (graph->succs[n])
1359 EXECUTE_IF_SET_IN_BITMAP (graph->succs[n], 0, j, bi)
1361 if (!TEST_BIT (ti->visited, j))
1362 topo_visit (graph, ti, j);
1365 VEC_safe_push (unsigned, heap, ti->topo_order, n);
1368 /* Return true if variable N + OFFSET is a legal field of N. */
1371 type_safe (unsigned int n, unsigned HOST_WIDE_INT *offset)
1373 varinfo_t ninfo = get_varinfo (n);
1375 /* For things we've globbed to single variables, any offset into the
1376 variable acts like the entire variable, so that it becomes offset
1378 if (ninfo->is_special_var
1379 || ninfo->is_artificial_var
1380 || ninfo->is_unknown_size_var)
1385 return (get_varinfo (n)->offset + *offset) < get_varinfo (n)->fullsize;
1388 /* Process a constraint C that represents x = *y, using DELTA as the
1389 starting solution. */
1392 do_sd_constraint (constraint_graph_t graph, constraint_t c,
1395 unsigned int lhs = c->lhs.var;
1397 bitmap sol = get_varinfo (lhs)->solution;
1401 if (bitmap_bit_p (delta, anything_id))
1403 flag = !bitmap_bit_p (sol, anything_id);
1405 bitmap_set_bit (sol, anything_id);
1408 /* For each variable j in delta (Sol(y)), add
1409 an edge in the graph from j to x, and union Sol(j) into Sol(x). */
1410 EXECUTE_IF_SET_IN_BITMAP (delta, 0, j, bi)
1412 unsigned HOST_WIDE_INT roffset = c->rhs.offset;
1413 if (type_safe (j, &roffset))
1416 unsigned HOST_WIDE_INT fieldoffset = get_varinfo (j)->offset + roffset;
1419 v = first_vi_for_offset (get_varinfo (j), fieldoffset);
1424 /* Adding edges from the special vars is pointless.
1425 They don't have sets that can change. */
1426 if (get_varinfo (t) ->is_special_var)
1427 flag |= bitmap_ior_into (sol, get_varinfo (t)->solution);
1428 else if (add_graph_edge (graph, lhs, t))
1429 flag |= bitmap_ior_into (sol, get_varinfo (t)->solution);
1431 else if (0 && dump_file && !(get_varinfo (j)->is_special_var))
1432 fprintf (dump_file, "Untypesafe usage in do_sd_constraint\n");
1437 /* If the LHS solution changed, mark the var as changed. */
1440 get_varinfo (lhs)->solution = sol;
1441 if (!TEST_BIT (changed, lhs))
1443 SET_BIT (changed, lhs);
1449 /* Process a constraint C that represents *x = y. */
1452 do_ds_constraint (constraint_t c, bitmap delta)
1454 unsigned int rhs = c->rhs.var;
1455 bitmap sol = get_varinfo (rhs)->solution;
1459 if (bitmap_bit_p (sol, anything_id))
1461 EXECUTE_IF_SET_IN_BITMAP (delta, 0, j, bi)
1463 varinfo_t jvi = get_varinfo (j);
1465 unsigned int loff = c->lhs.offset;
1466 unsigned HOST_WIDE_INT fieldoffset = jvi->offset + loff;
1469 v = first_vi_for_offset (get_varinfo (j), fieldoffset);
1474 if (!bitmap_bit_p (get_varinfo (t)->solution, anything_id))
1476 bitmap_set_bit (get_varinfo (t)->solution, anything_id);
1477 if (!TEST_BIT (changed, t))
1479 SET_BIT (changed, t);
1487 /* For each member j of delta (Sol(x)), add an edge from y to j and
1488 union Sol(y) into Sol(j) */
1489 EXECUTE_IF_SET_IN_BITMAP (delta, 0, j, bi)
1491 unsigned HOST_WIDE_INT loff = c->lhs.offset;
1492 if (type_safe (j, &loff) && !(get_varinfo (j)->is_special_var))
1496 unsigned HOST_WIDE_INT fieldoffset = get_varinfo (j)->offset + loff;
1499 v = first_vi_for_offset (get_varinfo (j), fieldoffset);
1503 tmp = get_varinfo (t)->solution;
1505 if (set_union_with_increment (tmp, sol, 0))
1507 get_varinfo (t)->solution = tmp;
1509 sol = get_varinfo (rhs)->solution;
1510 if (!TEST_BIT (changed, t))
1512 SET_BIT (changed, t);
1517 else if (0 && dump_file && !(get_varinfo (j)->is_special_var))
1518 fprintf (dump_file, "Untypesafe usage in do_ds_constraint\n");
1522 /* Handle a non-simple (simple meaning requires no iteration),
1523 constraint (IE *x = &y, x = *y, *x = y, and x = y with offsets involved). */
1526 do_complex_constraint (constraint_graph_t graph, constraint_t c, bitmap delta)
1528 if (c->lhs.type == DEREF)
1530 if (c->rhs.type == ADDRESSOF)
1537 do_ds_constraint (c, delta);
1540 else if (c->rhs.type == DEREF)
1543 if (!(get_varinfo (c->lhs.var)->is_special_var))
1544 do_sd_constraint (graph, c, delta);
1552 gcc_assert (c->rhs.type == SCALAR && c->lhs.type == SCALAR);
1553 solution = get_varinfo (c->rhs.var)->solution;
1554 tmp = get_varinfo (c->lhs.var)->solution;
1556 flag = set_union_with_increment (tmp, solution, c->rhs.offset);
1560 get_varinfo (c->lhs.var)->solution = tmp;
1561 if (!TEST_BIT (changed, c->lhs.var))
1563 SET_BIT (changed, c->lhs.var);
1570 /* Initialize and return a new SCC info structure. */
1572 static struct scc_info *
1573 init_scc_info (size_t size)
1575 struct scc_info *si = XNEW (struct scc_info);
1578 si->current_index = 0;
1579 si->visited = sbitmap_alloc (size);
1580 sbitmap_zero (si->visited);
1581 si->deleted = sbitmap_alloc (size);
1582 sbitmap_zero (si->deleted);
1583 si->node_mapping = XNEWVEC (unsigned int, size);
1584 si->dfs = XCNEWVEC (unsigned int, size);
1586 for (i = 0; i < size; i++)
1587 si->node_mapping[i] = i;
1589 si->scc_stack = VEC_alloc (unsigned, heap, 1);
1593 /* Free an SCC info structure pointed to by SI */
1596 free_scc_info (struct scc_info *si)
1598 sbitmap_free (si->visited);
1599 sbitmap_free (si->deleted);
1600 free (si->node_mapping);
1602 VEC_free (unsigned, heap, si->scc_stack);
1607 /* Find indirect cycles in GRAPH that occur, using strongly connected
1608 components, and note them in the indirect cycles map.
1610 This technique comes from Ben Hardekopf and Calvin Lin,
1611 "It Pays to be Lazy: Fast and Accurate Pointer Analysis for Millions of
1612 Lines of Code", submitted to PLDI 2007. */
1615 find_indirect_cycles (constraint_graph_t graph)
1618 unsigned int size = graph->size;
1619 struct scc_info *si = init_scc_info (size);
1621 for (i = 0; i < MIN (LAST_REF_NODE, size); i ++ )
1622 if (!TEST_BIT (si->visited, i) && find (i) == i)
1623 scc_visit (graph, si, i);
1628 /* Compute a topological ordering for GRAPH, and store the result in the
1629 topo_info structure TI. */
1632 compute_topo_order (constraint_graph_t graph,
1633 struct topo_info *ti)
1636 unsigned int size = graph->size;
1638 for (i = 0; i != size; ++i)
1639 if (!TEST_BIT (ti->visited, i) && find (i) == i)
1640 topo_visit (graph, ti, i);
1643 /* Structure used to for hash value numbering of pointer equivalence
1646 typedef struct equiv_class_label
1648 unsigned int equivalence_class;
1651 } *equiv_class_label_t;
1652 typedef const struct equiv_class_label *const_equiv_class_label_t;
1654 /* A hashtable for mapping a bitmap of labels->pointer equivalence
1656 static htab_t pointer_equiv_class_table;
1658 /* A hashtable for mapping a bitmap of labels->location equivalence
1660 static htab_t location_equiv_class_table;
1662 /* Hash function for a equiv_class_label_t */
1665 equiv_class_label_hash (const void *p)
1667 const_equiv_class_label_t const ecl = (const_equiv_class_label_t) p;
1668 return ecl->hashcode;
1671 /* Equality function for two equiv_class_label_t's. */
1674 equiv_class_label_eq (const void *p1, const void *p2)
1676 const_equiv_class_label_t const eql1 = (const_equiv_class_label_t) p1;
1677 const_equiv_class_label_t const eql2 = (const_equiv_class_label_t) p2;
1678 return bitmap_equal_p (eql1->labels, eql2->labels);
1681 /* Lookup a equivalence class in TABLE by the bitmap of LABELS it
1685 equiv_class_lookup (htab_t table, bitmap labels)
1688 struct equiv_class_label ecl;
1690 ecl.labels = labels;
1691 ecl.hashcode = bitmap_hash (labels);
1693 slot = htab_find_slot_with_hash (table, &ecl,
1694 ecl.hashcode, NO_INSERT);
1698 return ((equiv_class_label_t) *slot)->equivalence_class;
1702 /* Add an equivalence class named EQUIVALENCE_CLASS with labels LABELS
1706 equiv_class_add (htab_t table, unsigned int equivalence_class,
1710 equiv_class_label_t ecl = XNEW (struct equiv_class_label);
1712 ecl->labels = labels;
1713 ecl->equivalence_class = equivalence_class;
1714 ecl->hashcode = bitmap_hash (labels);
1716 slot = htab_find_slot_with_hash (table, ecl,
1717 ecl->hashcode, INSERT);
1718 gcc_assert (!*slot);
1719 *slot = (void *) ecl;
1722 /* Perform offline variable substitution.
1724 This is a worst case quadratic time way of identifying variables
1725 that must have equivalent points-to sets, including those caused by
1726 static cycles, and single entry subgraphs, in the constraint graph.
1728 The technique is described in "Exploiting Pointer and Location
1729 Equivalence to Optimize Pointer Analysis. In the 14th International
1730 Static Analysis Symposium (SAS), August 2007." It is known as the
1731 "HU" algorithm, and is equivalent to value numbering the collapsed
1732 constraint graph including evaluating unions.
1734 The general method of finding equivalence classes is as follows:
1735 Add fake nodes (REF nodes) and edges for *a = b and a = *b constraints.
1736 Initialize all non-REF nodes to be direct nodes.
1737 For each constraint a = a U {b}, we set pts(a) = pts(a) u {fresh
1739 For each constraint containing the dereference, we also do the same
1742 We then compute SCC's in the graph and unify nodes in the same SCC,
1745 For each non-collapsed node x:
1746 Visit all unvisited explicit incoming edges.
1747 Ignoring all non-pointers, set pts(x) = Union of pts(a) for y
1749 Lookup the equivalence class for pts(x).
1750 If we found one, equivalence_class(x) = found class.
1751 Otherwise, equivalence_class(x) = new class, and new_class is
1752 added to the lookup table.
1754 All direct nodes with the same equivalence class can be replaced
1755 with a single representative node.
1756 All unlabeled nodes (label == 0) are not pointers and all edges
1757 involving them can be eliminated.
1758 We perform these optimizations during rewrite_constraints
1760 In addition to pointer equivalence class finding, we also perform
1761 location equivalence class finding. This is the set of variables
1762 that always appear together in points-to sets. We use this to
1763 compress the size of the points-to sets. */
1765 /* Current maximum pointer equivalence class id. */
1766 static int pointer_equiv_class;
1768 /* Current maximum location equivalence class id. */
1769 static int location_equiv_class;
1771 /* Recursive routine to find strongly connected components in GRAPH,
1772 and label it's nodes with DFS numbers. */
1775 condense_visit (constraint_graph_t graph, struct scc_info *si, unsigned int n)
1779 unsigned int my_dfs;
1781 gcc_assert (si->node_mapping[n] == n);
1782 SET_BIT (si->visited, n);
1783 si->dfs[n] = si->current_index ++;
1784 my_dfs = si->dfs[n];
1786 /* Visit all the successors. */
1787 EXECUTE_IF_IN_NONNULL_BITMAP (graph->preds[n], 0, i, bi)
1789 unsigned int w = si->node_mapping[i];
1791 if (TEST_BIT (si->deleted, w))
1794 if (!TEST_BIT (si->visited, w))
1795 condense_visit (graph, si, w);
1797 unsigned int t = si->node_mapping[w];
1798 unsigned int nnode = si->node_mapping[n];
1799 gcc_assert (nnode == n);
1801 if (si->dfs[t] < si->dfs[nnode])
1802 si->dfs[n] = si->dfs[t];
1806 /* Visit all the implicit predecessors. */
1807 EXECUTE_IF_IN_NONNULL_BITMAP (graph->implicit_preds[n], 0, i, bi)
1809 unsigned int w = si->node_mapping[i];
1811 if (TEST_BIT (si->deleted, w))
1814 if (!TEST_BIT (si->visited, w))
1815 condense_visit (graph, si, w);
1817 unsigned int t = si->node_mapping[w];
1818 unsigned int nnode = si->node_mapping[n];
1819 gcc_assert (nnode == n);
1821 if (si->dfs[t] < si->dfs[nnode])
1822 si->dfs[n] = si->dfs[t];
1826 /* See if any components have been identified. */
1827 if (si->dfs[n] == my_dfs)
1829 while (VEC_length (unsigned, si->scc_stack) != 0
1830 && si->dfs[VEC_last (unsigned, si->scc_stack)] >= my_dfs)
1832 unsigned int w = VEC_pop (unsigned, si->scc_stack);
1833 si->node_mapping[w] = n;
1835 if (!TEST_BIT (graph->direct_nodes, w))
1836 RESET_BIT (graph->direct_nodes, n);
1838 /* Unify our nodes. */
1839 if (graph->preds[w])
1841 if (!graph->preds[n])
1842 graph->preds[n] = BITMAP_ALLOC (&predbitmap_obstack);
1843 bitmap_ior_into (graph->preds[n], graph->preds[w]);
1845 if (graph->implicit_preds[w])
1847 if (!graph->implicit_preds[n])
1848 graph->implicit_preds[n] = BITMAP_ALLOC (&predbitmap_obstack);
1849 bitmap_ior_into (graph->implicit_preds[n],
1850 graph->implicit_preds[w]);
1852 if (graph->points_to[w])
1854 if (!graph->points_to[n])
1855 graph->points_to[n] = BITMAP_ALLOC (&predbitmap_obstack);
1856 bitmap_ior_into (graph->points_to[n],
1857 graph->points_to[w]);
1859 EXECUTE_IF_IN_NONNULL_BITMAP (graph->preds[n], 0, i, bi)
1861 unsigned int rep = si->node_mapping[i];
1862 graph->number_incoming[rep]++;
1865 SET_BIT (si->deleted, n);
1868 VEC_safe_push (unsigned, heap, si->scc_stack, n);
1871 /* Label pointer equivalences. */
1874 label_visit (constraint_graph_t graph, struct scc_info *si, unsigned int n)
1878 SET_BIT (si->visited, n);
1880 if (!graph->points_to[n])
1881 graph->points_to[n] = BITMAP_ALLOC (&predbitmap_obstack);
1883 /* Label and union our incoming edges's points to sets. */
1884 EXECUTE_IF_IN_NONNULL_BITMAP (graph->preds[n], 0, i, bi)
1886 unsigned int w = si->node_mapping[i];
1887 if (!TEST_BIT (si->visited, w))
1888 label_visit (graph, si, w);
1890 /* Skip unused edges */
1891 if (w == n || graph->pointer_label[w] == 0)
1893 graph->number_incoming[w]--;
1896 if (graph->points_to[w])
1897 bitmap_ior_into(graph->points_to[n], graph->points_to[w]);
1899 /* If all incoming edges to w have been processed and
1900 graph->points_to[w] was not stored in the hash table, we can
1902 graph->number_incoming[w]--;
1903 if (!graph->number_incoming[w] && !TEST_BIT (graph->pt_used, w))
1905 BITMAP_FREE (graph->points_to[w]);
1908 /* Indirect nodes get fresh variables. */
1909 if (!TEST_BIT (graph->direct_nodes, n))
1910 bitmap_set_bit (graph->points_to[n], FIRST_REF_NODE + n);
1912 if (!bitmap_empty_p (graph->points_to[n]))
1914 unsigned int label = equiv_class_lookup (pointer_equiv_class_table,
1915 graph->points_to[n]);
1918 SET_BIT (graph->pt_used, n);
1919 label = pointer_equiv_class++;
1920 equiv_class_add (pointer_equiv_class_table,
1921 label, graph->points_to[n]);
1923 graph->pointer_label[n] = label;
1927 /* Perform offline variable substitution, discovering equivalence
1928 classes, and eliminating non-pointer variables. */
1930 static struct scc_info *
1931 perform_var_substitution (constraint_graph_t graph)
1934 unsigned int size = graph->size;
1935 struct scc_info *si = init_scc_info (size);
1937 bitmap_obstack_initialize (&iteration_obstack);
1938 pointer_equiv_class_table = htab_create (511, equiv_class_label_hash,
1939 equiv_class_label_eq, free);
1940 location_equiv_class_table = htab_create (511, equiv_class_label_hash,
1941 equiv_class_label_eq, free);
1942 pointer_equiv_class = 1;
1943 location_equiv_class = 1;
1945 /* Condense the nodes, which means to find SCC's, count incoming
1946 predecessors, and unite nodes in SCC's. */
1947 for (i = 0; i < FIRST_REF_NODE; i++)
1948 if (!TEST_BIT (si->visited, si->node_mapping[i]))
1949 condense_visit (graph, si, si->node_mapping[i]);
1951 sbitmap_zero (si->visited);
1952 /* Actually the label the nodes for pointer equivalences */
1953 for (i = 0; i < FIRST_REF_NODE; i++)
1954 if (!TEST_BIT (si->visited, si->node_mapping[i]))
1955 label_visit (graph, si, si->node_mapping[i]);
1957 /* Calculate location equivalence labels. */
1958 for (i = 0; i < FIRST_REF_NODE; i++)
1965 if (!graph->pointed_by[i])
1967 pointed_by = BITMAP_ALLOC (&iteration_obstack);
1969 /* Translate the pointed-by mapping for pointer equivalence
1971 EXECUTE_IF_SET_IN_BITMAP (graph->pointed_by[i], 0, j, bi)
1973 bitmap_set_bit (pointed_by,
1974 graph->pointer_label[si->node_mapping[j]]);
1976 /* The original pointed_by is now dead. */
1977 BITMAP_FREE (graph->pointed_by[i]);
1979 /* Look up the location equivalence label if one exists, or make
1981 label = equiv_class_lookup (location_equiv_class_table,
1985 label = location_equiv_class++;
1986 equiv_class_add (location_equiv_class_table,
1991 if (dump_file && (dump_flags & TDF_DETAILS))
1992 fprintf (dump_file, "Found location equivalence for node %s\n",
1993 get_varinfo (i)->name);
1994 BITMAP_FREE (pointed_by);
1996 graph->loc_label[i] = label;
2000 if (dump_file && (dump_flags & TDF_DETAILS))
2001 for (i = 0; i < FIRST_REF_NODE; i++)
2003 bool direct_node = TEST_BIT (graph->direct_nodes, i);
2005 "Equivalence classes for %s node id %d:%s are pointer: %d"
2007 direct_node ? "Direct node" : "Indirect node", i,
2008 get_varinfo (i)->name,
2009 graph->pointer_label[si->node_mapping[i]],
2010 graph->loc_label[si->node_mapping[i]]);
2013 /* Quickly eliminate our non-pointer variables. */
2015 for (i = 0; i < FIRST_REF_NODE; i++)
2017 unsigned int node = si->node_mapping[i];
2019 if (graph->pointer_label[node] == 0)
2021 if (dump_file && (dump_flags & TDF_DETAILS))
2023 "%s is a non-pointer variable, eliminating edges.\n",
2024 get_varinfo (node)->name);
2025 stats.nonpointer_vars++;
2026 clear_edges_for_node (graph, node);
2033 /* Free information that was only necessary for variable
2037 free_var_substitution_info (struct scc_info *si)
2040 free (graph->pointer_label);
2041 free (graph->loc_label);
2042 free (graph->pointed_by);
2043 free (graph->points_to);
2044 free (graph->number_incoming);
2045 free (graph->eq_rep);
2046 sbitmap_free (graph->direct_nodes);
2047 sbitmap_free (graph->pt_used);
2048 htab_delete (pointer_equiv_class_table);
2049 htab_delete (location_equiv_class_table);
2050 bitmap_obstack_release (&iteration_obstack);
2053 /* Return an existing node that is equivalent to NODE, which has
2054 equivalence class LABEL, if one exists. Return NODE otherwise. */
2057 find_equivalent_node (constraint_graph_t graph,
2058 unsigned int node, unsigned int label)
2060 /* If the address version of this variable is unused, we can
2061 substitute it for anything else with the same label.
2062 Otherwise, we know the pointers are equivalent, but not the
2063 locations, and we can unite them later. */
2065 if (!bitmap_bit_p (graph->address_taken, node))
2067 gcc_assert (label < graph->size);
2069 if (graph->eq_rep[label] != -1)
2071 /* Unify the two variables since we know they are equivalent. */
2072 if (unite (graph->eq_rep[label], node))
2073 unify_nodes (graph, graph->eq_rep[label], node, false);
2074 return graph->eq_rep[label];
2078 graph->eq_rep[label] = node;
2079 graph->pe_rep[label] = node;
2084 gcc_assert (label < graph->size);
2085 graph->pe[node] = label;
2086 if (graph->pe_rep[label] == -1)
2087 graph->pe_rep[label] = node;
2093 /* Unite pointer equivalent but not location equivalent nodes in
2094 GRAPH. This may only be performed once variable substitution is
2098 unite_pointer_equivalences (constraint_graph_t graph)
2102 /* Go through the pointer equivalences and unite them to their
2103 representative, if they aren't already. */
2104 for (i = 0; i < FIRST_REF_NODE; i++)
2106 unsigned int label = graph->pe[i];
2109 int label_rep = graph->pe_rep[label];
2111 if (label_rep == -1)
2114 label_rep = find (label_rep);
2115 if (label_rep >= 0 && unite (label_rep, find (i)))
2116 unify_nodes (graph, label_rep, i, false);
2121 /* Move complex constraints to the GRAPH nodes they belong to. */
2124 move_complex_constraints (constraint_graph_t graph)
2129 for (i = 0; VEC_iterate (constraint_t, constraints, i, c); i++)
2133 struct constraint_expr lhs = c->lhs;
2134 struct constraint_expr rhs = c->rhs;
2136 if (lhs.type == DEREF)
2138 insert_into_complex (graph, lhs.var, c);
2140 else if (rhs.type == DEREF)
2142 if (!(get_varinfo (lhs.var)->is_special_var))
2143 insert_into_complex (graph, rhs.var, c);
2145 else if (rhs.type != ADDRESSOF && lhs.var > anything_id
2146 && (lhs.offset != 0 || rhs.offset != 0))
2148 insert_into_complex (graph, rhs.var, c);
2155 /* Optimize and rewrite complex constraints while performing
2156 collapsing of equivalent nodes. SI is the SCC_INFO that is the
2157 result of perform_variable_substitution. */
2160 rewrite_constraints (constraint_graph_t graph,
2161 struct scc_info *si)
2167 for (j = 0; j < graph->size; j++)
2168 gcc_assert (find (j) == j);
2170 for (i = 0; VEC_iterate (constraint_t, constraints, i, c); i++)
2172 struct constraint_expr lhs = c->lhs;
2173 struct constraint_expr rhs = c->rhs;
2174 unsigned int lhsvar = find (get_varinfo_fc (lhs.var)->id);
2175 unsigned int rhsvar = find (get_varinfo_fc (rhs.var)->id);
2176 unsigned int lhsnode, rhsnode;
2177 unsigned int lhslabel, rhslabel;
2179 lhsnode = si->node_mapping[lhsvar];
2180 rhsnode = si->node_mapping[rhsvar];
2181 lhslabel = graph->pointer_label[lhsnode];
2182 rhslabel = graph->pointer_label[rhsnode];
2184 /* See if it is really a non-pointer variable, and if so, ignore
2188 if (dump_file && (dump_flags & TDF_DETAILS))
2191 fprintf (dump_file, "%s is a non-pointer variable,"
2192 "ignoring constraint:",
2193 get_varinfo (lhs.var)->name);
2194 dump_constraint (dump_file, c);
2196 VEC_replace (constraint_t, constraints, i, NULL);
2202 if (dump_file && (dump_flags & TDF_DETAILS))
2205 fprintf (dump_file, "%s is a non-pointer variable,"
2206 "ignoring constraint:",
2207 get_varinfo (rhs.var)->name);
2208 dump_constraint (dump_file, c);
2210 VEC_replace (constraint_t, constraints, i, NULL);
2214 lhsvar = find_equivalent_node (graph, lhsvar, lhslabel);
2215 rhsvar = find_equivalent_node (graph, rhsvar, rhslabel);
2216 c->lhs.var = lhsvar;
2217 c->rhs.var = rhsvar;
2222 /* Eliminate indirect cycles involving NODE. Return true if NODE was
2223 part of an SCC, false otherwise. */
2226 eliminate_indirect_cycles (unsigned int node)
2228 if (graph->indirect_cycles[node] != -1
2229 && !bitmap_empty_p (get_varinfo (node)->solution))
2232 VEC(unsigned,heap) *queue = NULL;
2234 unsigned int to = find (graph->indirect_cycles[node]);
2237 /* We can't touch the solution set and call unify_nodes
2238 at the same time, because unify_nodes is going to do
2239 bitmap unions into it. */
2241 EXECUTE_IF_SET_IN_BITMAP (get_varinfo (node)->solution, 0, i, bi)
2243 if (find (i) == i && i != to)
2246 VEC_safe_push (unsigned, heap, queue, i);
2251 VEC_iterate (unsigned, queue, queuepos, i);
2254 unify_nodes (graph, to, i, true);
2256 VEC_free (unsigned, heap, queue);
2262 /* Solve the constraint graph GRAPH using our worklist solver.
2263 This is based on the PW* family of solvers from the "Efficient Field
2264 Sensitive Pointer Analysis for C" paper.
2265 It works by iterating over all the graph nodes, processing the complex
2266 constraints and propagating the copy constraints, until everything stops
2267 changed. This corresponds to steps 6-8 in the solving list given above. */
2270 solve_graph (constraint_graph_t graph)
2272 unsigned int size = graph->size;
2277 changed = sbitmap_alloc (size);
2278 sbitmap_zero (changed);
2280 /* Mark all initial non-collapsed nodes as changed. */
2281 for (i = 0; i < size; i++)
2283 varinfo_t ivi = get_varinfo (i);
2284 if (find (i) == i && !bitmap_empty_p (ivi->solution)
2285 && ((graph->succs[i] && !bitmap_empty_p (graph->succs[i]))
2286 || VEC_length (constraint_t, graph->complex[i]) > 0))
2288 SET_BIT (changed, i);
2293 /* Allocate a bitmap to be used to store the changed bits. */
2294 pts = BITMAP_ALLOC (&pta_obstack);
2296 while (changed_count > 0)
2299 struct topo_info *ti = init_topo_info ();
2302 bitmap_obstack_initialize (&iteration_obstack);
2304 compute_topo_order (graph, ti);
2306 while (VEC_length (unsigned, ti->topo_order) != 0)
2309 i = VEC_pop (unsigned, ti->topo_order);
2311 /* If this variable is not a representative, skip it. */
2315 /* In certain indirect cycle cases, we may merge this
2316 variable to another. */
2317 if (eliminate_indirect_cycles (i) && find (i) != i)
2320 /* If the node has changed, we need to process the
2321 complex constraints and outgoing edges again. */
2322 if (TEST_BIT (changed, i))
2327 VEC(constraint_t,heap) *complex = graph->complex[i];
2328 bool solution_empty;
2330 RESET_BIT (changed, i);
2333 /* Compute the changed set of solution bits. */
2334 bitmap_and_compl (pts, get_varinfo (i)->solution,
2335 get_varinfo (i)->oldsolution);
2337 if (bitmap_empty_p (pts))
2340 bitmap_ior_into (get_varinfo (i)->oldsolution, pts);
2342 solution = get_varinfo (i)->solution;
2343 solution_empty = bitmap_empty_p (solution);
2345 /* Process the complex constraints */
2346 for (j = 0; VEC_iterate (constraint_t, complex, j, c); j++)
2348 /* XXX: This is going to unsort the constraints in
2349 some cases, which will occasionally add duplicate
2350 constraints during unification. This does not
2351 affect correctness. */
2352 c->lhs.var = find (c->lhs.var);
2353 c->rhs.var = find (c->rhs.var);
2355 /* The only complex constraint that can change our
2356 solution to non-empty, given an empty solution,
2357 is a constraint where the lhs side is receiving
2358 some set from elsewhere. */
2359 if (!solution_empty || c->lhs.type != DEREF)
2360 do_complex_constraint (graph, c, pts);
2363 solution_empty = bitmap_empty_p (solution);
2365 if (!solution_empty)
2369 /* Propagate solution to all successors. */
2370 EXECUTE_IF_IN_NONNULL_BITMAP (graph->succs[i],
2376 unsigned int to = find (j);
2377 tmp = get_varinfo (to)->solution;
2380 /* Don't try to propagate to ourselves. */
2384 flag = set_union_with_increment (tmp, pts, 0);
2388 get_varinfo (to)->solution = tmp;
2389 if (!TEST_BIT (changed, to))
2391 SET_BIT (changed, to);
2399 free_topo_info (ti);
2400 bitmap_obstack_release (&iteration_obstack);
2404 sbitmap_free (changed);
2405 bitmap_obstack_release (&oldpta_obstack);
2408 /* Map from trees to variable infos. */
2409 static struct pointer_map_t *vi_for_tree;
2412 /* Insert ID as the variable id for tree T in the vi_for_tree map. */
2415 insert_vi_for_tree (tree t, varinfo_t vi)
2417 void **slot = pointer_map_insert (vi_for_tree, t);
2419 gcc_assert (*slot == NULL);
2423 /* Find the variable info for tree T in VI_FOR_TREE. If T does not
2424 exist in the map, return NULL, otherwise, return the varinfo we found. */
2427 lookup_vi_for_tree (tree t)
2429 void **slot = pointer_map_contains (vi_for_tree, t);
2433 return (varinfo_t) *slot;
2436 /* Return a printable name for DECL */
2439 alias_get_name (tree decl)
2441 const char *res = get_name (decl);
2443 int num_printed = 0;
2452 if (TREE_CODE (decl) == SSA_NAME)
2454 num_printed = asprintf (&temp, "%s_%u",
2455 alias_get_name (SSA_NAME_VAR (decl)),
2456 SSA_NAME_VERSION (decl));
2458 else if (DECL_P (decl))
2460 num_printed = asprintf (&temp, "D.%u", DECL_UID (decl));
2462 if (num_printed > 0)
2464 res = ggc_strdup (temp);
2470 /* Find the variable id for tree T in the map.
2471 If T doesn't exist in the map, create an entry for it and return it. */
2474 get_vi_for_tree (tree t)
2476 void **slot = pointer_map_contains (vi_for_tree, t);
2478 return get_varinfo (create_variable_info_for (t, alias_get_name (t)));
2480 return (varinfo_t) *slot;
2483 /* Get a constraint expression from an SSA_VAR_P node. */
2485 static struct constraint_expr
2486 get_constraint_exp_from_ssa_var (tree t)
2488 struct constraint_expr cexpr;
2490 gcc_assert (SSA_VAR_P (t) || DECL_P (t));
2492 /* For parameters, get at the points-to set for the actual parm
2494 if (TREE_CODE (t) == SSA_NAME
2495 && TREE_CODE (SSA_NAME_VAR (t)) == PARM_DECL
2496 && SSA_NAME_IS_DEFAULT_DEF (t))
2497 return get_constraint_exp_from_ssa_var (SSA_NAME_VAR (t));
2499 cexpr.type = SCALAR;
2501 cexpr.var = get_vi_for_tree (t)->id;
2502 /* If we determine the result is "anything", and we know this is readonly,
2503 say it points to readonly memory instead. */
2504 if (cexpr.var == anything_id && TREE_READONLY (t))
2506 cexpr.type = ADDRESSOF;
2507 cexpr.var = readonly_id;
2514 /* Process a completed constraint T, and add it to the constraint
2515 list. FROM_CALL is true if this is a constraint coming from a
2516 call, which means any DEREFs we see are "may-deref's", not
2520 process_constraint_1 (constraint_t t, bool from_call)
2522 struct constraint_expr rhs = t->rhs;
2523 struct constraint_expr lhs = t->lhs;
2525 gcc_assert (rhs.var < VEC_length (varinfo_t, varmap));
2526 gcc_assert (lhs.var < VEC_length (varinfo_t, varmap));
2530 if (lhs.type == DEREF)
2531 get_varinfo (lhs.var)->directly_dereferenced = true;
2532 if (rhs.type == DEREF)
2533 get_varinfo (rhs.var)->directly_dereferenced = true;
2536 if (!use_field_sensitive)
2542 /* ANYTHING == ANYTHING is pointless. */
2543 if (lhs.var == anything_id && rhs.var == anything_id)
2546 /* If we have &ANYTHING = something, convert to SOMETHING = &ANYTHING) */
2547 else if (lhs.var == anything_id && lhs.type == ADDRESSOF)
2552 process_constraint_1 (t, from_call);
2554 /* This can happen in our IR with things like n->a = *p */
2555 else if (rhs.type == DEREF && lhs.type == DEREF && rhs.var != anything_id)
2557 /* Split into tmp = *rhs, *lhs = tmp */
2558 tree rhsdecl = get_varinfo (rhs.var)->decl;
2559 tree pointertype = TREE_TYPE (rhsdecl);
2560 tree pointedtotype = TREE_TYPE (pointertype);
2561 tree tmpvar = create_tmp_var_raw (pointedtotype, "doubledereftmp");
2562 struct constraint_expr tmplhs = get_constraint_exp_from_ssa_var (tmpvar);
2564 /* If this is an aggregate of known size, we should have passed
2565 this off to do_structure_copy, and it should have broken it
2567 gcc_assert (!AGGREGATE_TYPE_P (pointedtotype)
2568 || get_varinfo (rhs.var)->is_unknown_size_var);
2570 process_constraint_1 (new_constraint (tmplhs, rhs), from_call);
2571 process_constraint_1 (new_constraint (lhs, tmplhs), from_call);
2573 else if (rhs.type == ADDRESSOF && lhs.type == DEREF)
2575 /* Split into tmp = &rhs, *lhs = tmp */
2576 tree rhsdecl = get_varinfo (rhs.var)->decl;
2577 tree pointertype = TREE_TYPE (rhsdecl);
2578 tree tmpvar = create_tmp_var_raw (pointertype, "derefaddrtmp");
2579 struct constraint_expr tmplhs = get_constraint_exp_from_ssa_var (tmpvar);
2581 process_constraint_1 (new_constraint (tmplhs, rhs), from_call);
2582 process_constraint_1 (new_constraint (lhs, tmplhs), from_call);
2586 gcc_assert (rhs.type != ADDRESSOF || rhs.offset == 0);
2587 VEC_safe_push (constraint_t, heap, constraints, t);
2592 /* Process constraint T, performing various simplifications and then
2593 adding it to our list of overall constraints. */
2596 process_constraint (constraint_t t)
2598 process_constraint_1 (t, false);
2601 /* Return true if T is a variable of a type that could contain
2605 could_have_pointers (tree t)
2607 tree type = TREE_TYPE (t);
2609 if (POINTER_TYPE_P (type)
2610 || AGGREGATE_TYPE_P (type)
2611 || TREE_CODE (type) == COMPLEX_TYPE)
2617 /* Return the position, in bits, of FIELD_DECL from the beginning of its
2620 static unsigned HOST_WIDE_INT
2621 bitpos_of_field (const tree fdecl)
2624 if (TREE_CODE (DECL_FIELD_OFFSET (fdecl)) != INTEGER_CST
2625 || TREE_CODE (DECL_FIELD_BIT_OFFSET (fdecl)) != INTEGER_CST)
2628 return (tree_low_cst (DECL_FIELD_OFFSET (fdecl), 1) * 8)
2629 + tree_low_cst (DECL_FIELD_BIT_OFFSET (fdecl), 1);
2633 /* Given a COMPONENT_REF T, return the constraint_expr for it. */
2636 get_constraint_for_component_ref (tree t, VEC(ce_s, heap) **results)
2639 HOST_WIDE_INT bitsize = -1;
2640 HOST_WIDE_INT bitmaxsize = -1;
2641 HOST_WIDE_INT bitpos;
2643 struct constraint_expr *result;
2644 unsigned int beforelength = VEC_length (ce_s, *results);
2646 /* Some people like to do cute things like take the address of
2649 while (!SSA_VAR_P (forzero) && !CONSTANT_CLASS_P (forzero))
2650 forzero = TREE_OPERAND (forzero, 0);
2652 if (CONSTANT_CLASS_P (forzero) && integer_zerop (forzero))
2654 struct constraint_expr temp;
2657 temp.var = integer_id;
2659 VEC_safe_push (ce_s, heap, *results, &temp);
2663 t = get_ref_base_and_extent (t, &bitpos, &bitsize, &bitmaxsize);
2665 /* String constants are readonly, so there is nothing to really do
2667 if (TREE_CODE (t) == STRING_CST)
2670 get_constraint_for (t, results);
2671 result = VEC_last (ce_s, *results);
2672 result->offset = bitpos;
2674 gcc_assert (beforelength + 1 == VEC_length (ce_s, *results));
2676 /* This can also happen due to weird offsetof type macros. */
2677 if (TREE_CODE (t) != ADDR_EXPR && result->type == ADDRESSOF)
2678 result->type = SCALAR;
2680 if (result->type == SCALAR)
2682 /* In languages like C, you can access one past the end of an
2683 array. You aren't allowed to dereference it, so we can
2684 ignore this constraint. When we handle pointer subtraction,
2685 we may have to do something cute here. */
2687 if (result->offset < get_varinfo (result->var)->fullsize
2690 /* It's also not true that the constraint will actually start at the
2691 right offset, it may start in some padding. We only care about
2692 setting the constraint to the first actual field it touches, so
2695 for (curr = get_varinfo (result->var); curr; curr = curr->next)
2697 if (ranges_overlap_p (curr->offset, curr->size,
2698 result->offset, bitmaxsize))
2700 result->var = curr->id;
2704 /* assert that we found *some* field there. The user couldn't be
2705 accessing *only* padding. */
2706 /* Still the user could access one past the end of an array
2707 embedded in a struct resulting in accessing *only* padding. */
2708 gcc_assert (curr || ref_contains_array_ref (orig_t));
2710 else if (bitmaxsize == 0)
2712 if (dump_file && (dump_flags & TDF_DETAILS))
2713 fprintf (dump_file, "Access to zero-sized part of variable,"
2717 if (dump_file && (dump_flags & TDF_DETAILS))
2718 fprintf (dump_file, "Access to past the end of variable, ignoring\n");
2722 else if (bitmaxsize == -1)
2724 /* We can't handle DEREF constraints with unknown size, we'll
2725 get the wrong answer. Punt and return anything. */
2726 result->var = anything_id;
2732 /* Dereference the constraint expression CONS, and return the result.
2733 DEREF (ADDRESSOF) = SCALAR
2734 DEREF (SCALAR) = DEREF
2735 DEREF (DEREF) = (temp = DEREF1; result = DEREF(temp))
2736 This is needed so that we can handle dereferencing DEREF constraints. */
2739 do_deref (VEC (ce_s, heap) **constraints)
2741 struct constraint_expr *c;
2744 for (i = 0; VEC_iterate (ce_s, *constraints, i, c); i++)
2746 if (c->type == SCALAR)
2748 else if (c->type == ADDRESSOF)
2750 else if (c->type == DEREF)
2752 tree tmpvar = create_tmp_var_raw (ptr_type_node, "dereftmp");
2753 struct constraint_expr tmplhs = get_constraint_exp_from_ssa_var (tmpvar);
2754 process_constraint (new_constraint (tmplhs, *c));
2755 c->var = tmplhs.var;
2762 /* Given a tree T, return the constraint expression for it. */
2765 get_constraint_for (tree t, VEC (ce_s, heap) **results)
2767 struct constraint_expr temp;
2769 /* x = integer is all glommed to a single variable, which doesn't
2770 point to anything by itself. That is, of course, unless it is an
2771 integer constant being treated as a pointer, in which case, we
2772 will return that this is really the addressof anything. This
2773 happens below, since it will fall into the default case. The only
2774 case we know something about an integer treated like a pointer is
2775 when it is the NULL pointer, and then we just say it points to
2777 if (TREE_CODE (t) == INTEGER_CST
2778 && integer_zerop (t))
2780 temp.var = nothing_id;
2781 temp.type = ADDRESSOF;
2783 VEC_safe_push (ce_s, heap, *results, &temp);
2787 switch (TREE_CODE_CLASS (TREE_CODE (t)))
2789 case tcc_expression:
2792 switch (TREE_CODE (t))
2796 struct constraint_expr *c;
2798 tree exp = TREE_OPERAND (t, 0);
2799 tree pttype = TREE_TYPE (TREE_TYPE (t));
2801 get_constraint_for (exp, results);
2804 /* Complex types are special. Taking the address of one
2805 allows you to access either part of it through that
2807 if (VEC_length (ce_s, *results) == 1 &&
2808 TREE_CODE (pttype) == COMPLEX_TYPE)
2810 struct constraint_expr *origrhs;
2812 struct constraint_expr tmp;
2814 gcc_assert (VEC_length (ce_s, *results) == 1);
2815 origrhs = VEC_last (ce_s, *results);
2817 VEC_pop (ce_s, *results);
2818 origvar = get_varinfo (origrhs->var);
2819 for (; origvar; origvar = origvar->next)
2821 tmp.var = origvar->id;
2822 VEC_safe_push (ce_s, heap, *results, &tmp);
2826 for (i = 0; VEC_iterate (ce_s, *results, i, c); i++)
2828 if (c->type == DEREF)
2831 c->type = ADDRESSOF;
2837 /* XXX: In interprocedural mode, if we didn't have the
2838 body, we would need to do *each pointer argument =
2840 if (call_expr_flags (t) & (ECF_MALLOC | ECF_MAY_BE_ALLOCA))
2843 tree heapvar = heapvar_lookup (t);
2845 if (heapvar == NULL)
2847 heapvar = create_tmp_var_raw (ptr_type_node, "HEAP");
2848 DECL_EXTERNAL (heapvar) = 1;
2849 get_var_ann (heapvar)->is_heapvar = 1;
2850 if (gimple_referenced_vars (cfun))
2851 add_referenced_var (heapvar);
2852 heapvar_insert (t, heapvar);
2855 temp.var = create_variable_info_for (heapvar,
2856 alias_get_name (heapvar));
2858 vi = get_varinfo (temp.var);
2859 vi->is_artificial_var = 1;
2860 vi->is_heap_var = 1;
2861 temp.type = ADDRESSOF;
2863 VEC_safe_push (ce_s, heap, *results, &temp);
2868 temp.var = anything_id;
2871 VEC_safe_push (ce_s, heap, *results, &temp);
2877 temp.type = ADDRESSOF;
2878 temp.var = anything_id;
2880 VEC_safe_push (ce_s, heap, *results, &temp);
2887 switch (TREE_CODE (t))
2891 get_constraint_for (TREE_OPERAND (t, 0), results);
2896 case ARRAY_RANGE_REF:
2898 get_constraint_for_component_ref (t, results);
2902 temp.type = ADDRESSOF;
2903 temp.var = anything_id;
2905 VEC_safe_push (ce_s, heap, *results, &temp);
2912 switch (TREE_CODE (t))
2916 case NON_LVALUE_EXPR:
2918 tree op = TREE_OPERAND (t, 0);
2920 /* Cast from non-pointer to pointers are bad news for us.
2921 Anything else, we see through */
2922 if (!(POINTER_TYPE_P (TREE_TYPE (t))
2923 && ! POINTER_TYPE_P (TREE_TYPE (op))))
2925 get_constraint_for (op, results);
2933 temp.type = ADDRESSOF;
2934 temp.var = anything_id;
2936 VEC_safe_push (ce_s, heap, *results, &temp);
2941 case tcc_exceptional:
2943 switch (TREE_CODE (t))
2947 get_constraint_for (PHI_RESULT (t), results);
2953 struct constraint_expr temp;
2954 temp = get_constraint_exp_from_ssa_var (t);
2955 VEC_safe_push (ce_s, heap, *results, &temp);
2961 temp.type = ADDRESSOF;
2962 temp.var = anything_id;
2964 VEC_safe_push (ce_s, heap, *results, &temp);
2969 case tcc_declaration:
2971 struct constraint_expr temp;
2972 temp = get_constraint_exp_from_ssa_var (t);
2973 VEC_safe_push (ce_s, heap, *results, &temp);
2978 temp.type = ADDRESSOF;
2979 temp.var = anything_id;
2981 VEC_safe_push (ce_s, heap, *results, &temp);
2988 /* Handle the structure copy case where we have a simple structure copy
2989 between LHS and RHS that is of SIZE (in bits)
2991 For each field of the lhs variable (lhsfield)
2992 For each field of the rhs variable at lhsfield.offset (rhsfield)
2993 add the constraint lhsfield = rhsfield
2995 If we fail due to some kind of type unsafety or other thing we
2996 can't handle, return false. We expect the caller to collapse the
2997 variable in that case. */
3000 do_simple_structure_copy (const struct constraint_expr lhs,
3001 const struct constraint_expr rhs,
3002 const unsigned HOST_WIDE_INT size)
3004 varinfo_t p = get_varinfo (lhs.var);
3005 unsigned HOST_WIDE_INT pstart, last;
3007 last = p->offset + size;
3008 for (; p && p->offset < last; p = p->next)
3011 struct constraint_expr templhs = lhs;
3012 struct constraint_expr temprhs = rhs;
3013 unsigned HOST_WIDE_INT fieldoffset;
3015 templhs.var = p->id;
3016 q = get_varinfo (temprhs.var);
3017 fieldoffset = p->offset - pstart;
3018 q = first_vi_for_offset (q, q->offset + fieldoffset);
3021 temprhs.var = q->id;
3022 process_constraint (new_constraint (templhs, temprhs));
3028 /* Handle the structure copy case where we have a structure copy between a
3029 aggregate on the LHS and a dereference of a pointer on the RHS
3030 that is of SIZE (in bits)
3032 For each field of the lhs variable (lhsfield)
3033 rhs.offset = lhsfield->offset
3034 add the constraint lhsfield = rhs
3038 do_rhs_deref_structure_copy (const struct constraint_expr lhs,
3039 const struct constraint_expr rhs,
3040 const unsigned HOST_WIDE_INT size)
3042 varinfo_t p = get_varinfo (lhs.var);
3043 unsigned HOST_WIDE_INT pstart,last;
3045 last = p->offset + size;
3047 for (; p && p->offset < last; p = p->next)
3050 struct constraint_expr templhs = lhs;
3051 struct constraint_expr temprhs = rhs;
3052 unsigned HOST_WIDE_INT fieldoffset;
3055 if (templhs.type == SCALAR)
3056 templhs.var = p->id;
3058 templhs.offset = p->offset;
3060 q = get_varinfo (temprhs.var);
3061 fieldoffset = p->offset - pstart;
3062 temprhs.offset += fieldoffset;
3063 process_constraint (new_constraint (templhs, temprhs));
3067 /* Handle the structure copy case where we have a structure copy
3068 between an aggregate on the RHS and a dereference of a pointer on
3069 the LHS that is of SIZE (in bits)
3071 For each field of the rhs variable (rhsfield)
3072 lhs.offset = rhsfield->offset
3073 add the constraint lhs = rhsfield
3077 do_lhs_deref_structure_copy (const struct constraint_expr lhs,
3078 const struct constraint_expr rhs,
3079 const unsigned HOST_WIDE_INT size)
3081 varinfo_t p = get_varinfo (rhs.var);
3082 unsigned HOST_WIDE_INT pstart,last;
3084 last = p->offset + size;
3086 for (; p && p->offset < last; p = p->next)
3089 struct constraint_expr templhs = lhs;
3090 struct constraint_expr temprhs = rhs;
3091 unsigned HOST_WIDE_INT fieldoffset;
3094 if (temprhs.type == SCALAR)
3095 temprhs.var = p->id;
3097 temprhs.offset = p->offset;
3099 q = get_varinfo (templhs.var);
3100 fieldoffset = p->offset - pstart;
3101 templhs.offset += fieldoffset;
3102 process_constraint (new_constraint (templhs, temprhs));
3106 /* Sometimes, frontends like to give us bad type information. This
3107 function will collapse all the fields from VAR to the end of VAR,
3108 into VAR, so that we treat those fields as a single variable.
3109 We return the variable they were collapsed into. */
3112 collapse_rest_of_var (unsigned int var)
3114 varinfo_t currvar = get_varinfo (var);
3117 for (field = currvar->next; field; field = field->next)
3120 fprintf (dump_file, "Type safety: Collapsing var %s into %s\n",
3121 field->name, currvar->name);
3123 gcc_assert (!field->collapsed_to);
3124 field->collapsed_to = currvar;
3127 currvar->next = NULL;
3128 currvar->size = currvar->fullsize - currvar->offset;
3133 /* Handle aggregate copies by expanding into copies of the respective
3134 fields of the structures. */
3137 do_structure_copy (tree lhsop, tree rhsop)
3139 struct constraint_expr lhs, rhs, tmp;
3140 VEC (ce_s, heap) *lhsc = NULL, *rhsc = NULL;
3142 unsigned HOST_WIDE_INT lhssize;
3143 unsigned HOST_WIDE_INT rhssize;
3145 get_constraint_for (lhsop, &lhsc);
3146 get_constraint_for (rhsop, &rhsc);
3147 gcc_assert (VEC_length (ce_s, lhsc) == 1);
3148 gcc_assert (VEC_length (ce_s, rhsc) == 1);
3149 lhs = *(VEC_last (ce_s, lhsc));
3150 rhs = *(VEC_last (ce_s, rhsc));
3152 VEC_free (ce_s, heap, lhsc);
3153 VEC_free (ce_s, heap, rhsc);
3155 /* If we have special var = x, swap it around. */
3156 if (lhs.var <= integer_id && !(get_varinfo (rhs.var)->is_special_var))
3163 /* This is fairly conservative for the RHS == ADDRESSOF case, in that it's
3164 possible it's something we could handle. However, most cases falling
3165 into this are dealing with transparent unions, which are slightly
3167 if (rhs.type == ADDRESSOF && !(get_varinfo (rhs.var)->is_special_var))
3169 rhs.type = ADDRESSOF;
3170 rhs.var = anything_id;
3173 /* If the RHS is a special var, or an addressof, set all the LHS fields to
3174 that special var. */
3175 if (rhs.var <= integer_id)
3177 for (p = get_varinfo (lhs.var); p; p = p->next)
3179 struct constraint_expr templhs = lhs;
3180 struct constraint_expr temprhs = rhs;
3182 if (templhs.type == SCALAR )
3183 templhs.var = p->id;
3185 templhs.offset += p->offset;
3186 process_constraint (new_constraint (templhs, temprhs));
3191 tree rhstype = TREE_TYPE (rhsop);
3192 tree lhstype = TREE_TYPE (lhsop);
3196 lhstypesize = DECL_P (lhsop) ? DECL_SIZE (lhsop) : TYPE_SIZE (lhstype);
3197 rhstypesize = DECL_P (rhsop) ? DECL_SIZE (rhsop) : TYPE_SIZE (rhstype);
3199 /* If we have a variably sized types on the rhs or lhs, and a deref
3200 constraint, add the constraint, lhsconstraint = &ANYTHING.
3201 This is conservatively correct because either the lhs is an unknown
3202 sized var (if the constraint is SCALAR), or the lhs is a DEREF
3203 constraint, and every variable it can point to must be unknown sized
3204 anyway, so we don't need to worry about fields at all. */
3205 if ((rhs.type == DEREF && TREE_CODE (rhstypesize) != INTEGER_CST)
3206 || (lhs.type == DEREF && TREE_CODE (lhstypesize) != INTEGER_CST))
3208 rhs.var = anything_id;
3209 rhs.type = ADDRESSOF;
3211 process_constraint (new_constraint (lhs, rhs));
3215 /* The size only really matters insofar as we don't set more or less of
3216 the variable. If we hit an unknown size var, the size should be the
3217 whole darn thing. */
3218 if (get_varinfo (rhs.var)->is_unknown_size_var)
3221 rhssize = TREE_INT_CST_LOW (rhstypesize);
3223 if (get_varinfo (lhs.var)->is_unknown_size_var)
3226 lhssize = TREE_INT_CST_LOW (lhstypesize);
3229 if (rhs.type == SCALAR && lhs.type == SCALAR)
3231 if (!do_simple_structure_copy (lhs, rhs, MIN (lhssize, rhssize)))
3233 lhs.var = collapse_rest_of_var (lhs.var);
3234 rhs.var = collapse_rest_of_var (rhs.var);
3239 process_constraint (new_constraint (lhs, rhs));
3242 else if (lhs.type != DEREF && rhs.type == DEREF)
3243 do_rhs_deref_structure_copy (lhs, rhs, MIN (lhssize, rhssize));
3244 else if (lhs.type == DEREF && rhs.type != DEREF)
3245 do_lhs_deref_structure_copy (lhs, rhs, MIN (lhssize, rhssize));
3248 tree pointedtotype = lhstype;
3251 gcc_assert (rhs.type == DEREF && lhs.type == DEREF);
3252 tmpvar = create_tmp_var_raw (pointedtotype, "structcopydereftmp");
3253 do_structure_copy (tmpvar, rhsop);
3254 do_structure_copy (lhsop, tmpvar);
3260 /* Update related alias information kept in AI. This is used when
3261 building name tags, alias sets and deciding grouping heuristics.
3262 STMT is the statement to process. This function also updates
3263 ADDRESSABLE_VARS. */
3266 update_alias_info (tree stmt, struct alias_info *ai)
3269 use_operand_p use_p;
3271 bool stmt_dereferences_ptr_p;
3272 enum escape_type stmt_escape_type = is_escape_site (stmt);
3273 struct mem_ref_stats_d *mem_ref_stats = gimple_mem_ref_stats (cfun);
3275 stmt_dereferences_ptr_p = false;
3277 if (stmt_escape_type == ESCAPE_TO_CALL
3278 || stmt_escape_type == ESCAPE_TO_PURE_CONST)
3280 mem_ref_stats->num_call_sites++;
3281 if (stmt_escape_type == ESCAPE_TO_PURE_CONST)
3282 mem_ref_stats->num_pure_const_call_sites++;
3284 else if (stmt_escape_type == ESCAPE_TO_ASM)
3285 mem_ref_stats->num_asm_sites++;
3287 /* Mark all the variables whose address are taken by the statement. */
3288 addr_taken = addresses_taken (stmt);
3291 bitmap_ior_into (gimple_addressable_vars (cfun), addr_taken);
3293 /* If STMT is an escape point, all the addresses taken by it are
3295 if (stmt_escape_type != NO_ESCAPE)
3300 EXECUTE_IF_SET_IN_BITMAP (addr_taken, 0, i, bi)
3302 tree rvar = referenced_var (i);
3303 if (!unmodifiable_var_p (rvar))
3304 mark_call_clobbered (rvar, stmt_escape_type);
3309 /* Process each operand use. For pointers, determine whether they
3310 are dereferenced by the statement, or whether their value
3312 FOR_EACH_PHI_OR_STMT_USE (use_p, stmt, iter, SSA_OP_USE)
3316 struct ptr_info_def *pi;
3317 unsigned num_uses, num_loads, num_stores;
3319 op = USE_FROM_PTR (use_p);
3321 /* If STMT is a PHI node, OP may be an ADDR_EXPR. If so, add it
3322 to the set of addressable variables. */
3323 if (TREE_CODE (op) == ADDR_EXPR)
3325 bitmap addressable_vars = gimple_addressable_vars (cfun);
3327 gcc_assert (TREE_CODE (stmt) == PHI_NODE);
3328 gcc_assert (addressable_vars);
3330 /* PHI nodes don't have annotations for pinning the set
3331 of addresses taken, so we collect them here.
3333 FIXME, should we allow PHI nodes to have annotations
3334 so that they can be treated like regular statements?
3335 Currently, they are treated as second-class
3337 add_to_addressable_set (TREE_OPERAND (op, 0), &addressable_vars);
3341 /* Ignore constants (they may occur in PHI node arguments). */
3342 if (TREE_CODE (op) != SSA_NAME)
3345 var = SSA_NAME_VAR (op);
3346 v_ann = var_ann (var);
3348 /* The base variable of an SSA name must be a GIMPLE register, and thus
3349 it cannot be aliased. */
3350 gcc_assert (!may_be_aliased (var));
3352 /* We are only interested in pointers. */
3353 if (!POINTER_TYPE_P (TREE_TYPE (op)))
3356 pi = get_ptr_info (op);
3358 /* Add OP to AI->PROCESSED_PTRS, if it's not there already. */
3359 if (!TEST_BIT (ai->ssa_names_visited, SSA_NAME_VERSION (op)))
3361 SET_BIT (ai->ssa_names_visited, SSA_NAME_VERSION (op));
3362 VEC_safe_push (tree, heap, ai->processed_ptrs, op);
3365 /* If STMT is a PHI node, then it will not have pointer
3366 dereferences and it will not be an escape point. */
3367 if (TREE_CODE (stmt) == PHI_NODE)
3370 /* Determine whether OP is a dereferenced pointer, and if STMT
3371 is an escape point, whether OP escapes. */
3372 count_uses_and_derefs (op, stmt, &num_uses, &num_loads, &num_stores);
3374 /* Handle a corner case involving address expressions of the
3375 form '&PTR->FLD'. The problem with these expressions is that
3376 they do not represent a dereference of PTR. However, if some
3377 other transformation propagates them into an INDIRECT_REF
3378 expression, we end up with '*(&PTR->FLD)' which is folded
3381 So, if the original code had no other dereferences of PTR,
3382 the aliaser will not create memory tags for it, and when
3383 &PTR->FLD gets propagated to INDIRECT_REF expressions, the
3384 memory operations will receive no VDEF/VUSE operands.
3386 One solution would be to have count_uses_and_derefs consider
3387 &PTR->FLD a dereference of PTR. But that is wrong, since it
3388 is not really a dereference but an offset calculation.
3390 What we do here is to recognize these special ADDR_EXPR
3391 nodes. Since these expressions are never GIMPLE values (they
3392 are not GIMPLE invariants), they can only appear on the RHS
3393 of an assignment and their base address is always an
3394 INDIRECT_REF expression. */
3395 if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT
3396 && TREE_CODE (GIMPLE_STMT_OPERAND (stmt, 1)) == ADDR_EXPR
3397 && !is_gimple_val (GIMPLE_STMT_OPERAND (stmt, 1)))
3399 /* If the RHS if of the form &PTR->FLD and PTR == OP, then
3400 this represents a potential dereference of PTR. */
3401 tree rhs = GIMPLE_STMT_OPERAND (stmt, 1);
3402 tree base = get_base_address (TREE_OPERAND (rhs, 0));
3403 if (TREE_CODE (base) == INDIRECT_REF
3404 && TREE_OPERAND (base, 0) == op)
3408 if (num_loads + num_stores > 0)
3410 /* Mark OP as dereferenced. In a subsequent pass,
3411 dereferenced pointers that point to a set of
3412 variables will be assigned a name tag to alias
3413 all the variables OP points to. */
3414 pi->is_dereferenced = 1;
3416 /* If this is a store operation, mark OP as being
3417 dereferenced to store, otherwise mark it as being
3418 dereferenced to load. */
3420 pointer_set_insert (ai->dereferenced_ptrs_store, var);
3422 pointer_set_insert (ai->dereferenced_ptrs_load, var);
3424 /* Update the frequency estimate for all the dereferences of
3426 update_mem_sym_stats_from_stmt (op, stmt, num_loads, num_stores);
3428 /* Indicate that STMT contains pointer dereferences. */
3429 stmt_dereferences_ptr_p = true;
3432 if (stmt_escape_type != NO_ESCAPE && num_loads + num_stores < num_uses)
3434 /* If STMT is an escape point and STMT contains at
3435 least one direct use of OP, then the value of OP
3436 escapes and so the pointed-to variables need to
3437 be marked call-clobbered. */
3438 pi->value_escapes_p = 1;
3439 pi->escape_mask |= stmt_escape_type;
3441 /* If the statement makes a function call, assume
3442 that pointer OP will be dereferenced in a store
3443 operation inside the called function. */
3444 if (get_call_expr_in (stmt)
3445 || stmt_escape_type == ESCAPE_STORED_IN_GLOBAL)
3447 pointer_set_insert (ai->dereferenced_ptrs_store, var);
3448 pi->is_dereferenced = 1;
3453 if (TREE_CODE (stmt) == PHI_NODE)
3456 /* Mark stored variables in STMT as being written to and update the
3457 memory reference stats for all memory symbols referenced by STMT. */
3458 if (stmt_references_memory_p (stmt))
3463 mem_ref_stats->num_mem_stmts++;
3465 /* Notice that we only update memory reference stats for symbols
3466 loaded and stored by the statement if the statement does not
3467 contain pointer dereferences and it is not a call/asm site.
3468 This is to avoid double accounting problems when creating
3469 memory partitions. After computing points-to information,
3470 pointer dereference statistics are used to update the
3471 reference stats of the pointed-to variables, so here we
3472 should only update direct references to symbols.
3474 Indirect references are not updated here for two reasons: (1)
3475 The first time we compute alias information, the sets
3476 LOADED/STORED are empty for pointer dereferences, (2) After
3477 partitioning, LOADED/STORED may have references to
3478 partitions, not the original pointed-to variables. So, if we
3479 always counted LOADED/STORED here and during partitioning, we
3480 would count many symbols more than once.
3482 This does cause some imprecision when a statement has a
3483 combination of direct symbol references and pointer
3484 dereferences (e.g., MEMORY_VAR = *PTR) or if a call site has
3485 memory symbols in its argument list, but these cases do not
3486 occur so frequently as to constitute a serious problem. */
3487 if (STORED_SYMS (stmt))
3488 EXECUTE_IF_SET_IN_BITMAP (STORED_SYMS (stmt), 0, i, bi)
3490 tree sym = referenced_var (i);
3491 pointer_set_insert (ai->written_vars, sym);
3492 if (!stmt_dereferences_ptr_p
3493 && stmt_escape_type != ESCAPE_TO_CALL
3494 && stmt_escape_type != ESCAPE_TO_PURE_CONST
3495 && stmt_escape_type != ESCAPE_TO_ASM)
3496 update_mem_sym_stats_from_stmt (sym, stmt, 0, 1);
3499 if (!stmt_dereferences_ptr_p
3500 && LOADED_SYMS (stmt)
3501 && stmt_escape_type != ESCAPE_TO_CALL
3502 && stmt_escape_type != ESCAPE_TO_PURE_CONST
3503 && stmt_escape_type != ESCAPE_TO_ASM)
3504 EXECUTE_IF_SET_IN_BITMAP (LOADED_SYMS (stmt), 0, i, bi)
3505 update_mem_sym_stats_from_stmt (referenced_var (i), stmt, 1, 0);
3510 /* Handle pointer arithmetic EXPR when creating aliasing constraints.
3511 Expressions of the type PTR + CST can be handled in two ways:
3513 1- If the constraint for PTR is ADDRESSOF for a non-structure
3514 variable, then we can use it directly because adding or
3515 subtracting a constant may not alter the original ADDRESSOF
3516 constraint (i.e., pointer arithmetic may not legally go outside
3517 an object's boundaries).
3519 2- If the constraint for PTR is ADDRESSOF for a structure variable,
3520 then if CST is a compile-time constant that can be used as an
3521 offset, we can determine which sub-variable will be pointed-to
3524 Return true if the expression is handled. For any other kind of
3525 expression, return false so that each operand can be added as a
3526 separate constraint by the caller. */
3529 handle_ptr_arith (VEC (ce_s, heap) *lhsc, tree expr)
3532 struct constraint_expr *c, *c2;
3535 VEC (ce_s, heap) *temp = NULL;
3536 unsigned int rhsoffset = 0;
3537 bool unknown_addend = false;
3539 if (TREE_CODE (expr) != POINTER_PLUS_EXPR)
3542 op0 = TREE_OPERAND (expr, 0);
3543 op1 = TREE_OPERAND (expr, 1);
3544 gcc_assert (POINTER_TYPE_P (TREE_TYPE (op0)));
3546 get_constraint_for (op0, &temp);
3548 /* Handle non-constants by making constraints from integer. */
3549 if (TREE_CODE (op1) == INTEGER_CST)
3550 rhsoffset = TREE_INT_CST_LOW (op1) * BITS_PER_UNIT;
3552 unknown_addend = true;
3554 for (i = 0; VEC_iterate (ce_s, lhsc, i, c); i++)
3555 for (j = 0; VEC_iterate (ce_s, temp, j, c2); j++)
3557 if (c2->type == ADDRESSOF && rhsoffset != 0)
3559 varinfo_t temp = get_varinfo (c2->var);
3561 /* An access one after the end of an array is valid,
3562 so simply punt on accesses we cannot resolve. */
3563 temp = first_vi_for_offset (temp, rhsoffset);
3569 else if (unknown_addend)
3571 /* Can't handle *a + integer where integer is unknown. */
3572 if (c2->type != SCALAR)
3574 struct constraint_expr intc;
3575 intc.var = integer_id;
3578 process_constraint (new_constraint (*c, intc));
3582 /* We known it lives somewhere within c2->var. */
3583 varinfo_t tmp = get_varinfo (c2->var);
3584 for (; tmp; tmp = tmp->next)
3586 struct constraint_expr tmpc = *c2;
3589 process_constraint (new_constraint (*c, tmpc));
3594 c2->offset = rhsoffset;
3595 process_constraint (new_constraint (*c, *c2));
3598 VEC_free (ce_s, heap, temp);
3603 /* For non-IPA mode, generate constraints necessary for a call on the
3607 handle_rhs_call (tree rhs)
3610 call_expr_arg_iterator iter;
3611 struct constraint_expr rhsc;
3613 rhsc.var = anything_id;
3615 rhsc.type = ADDRESSOF;
3617 FOR_EACH_CALL_EXPR_ARG (arg, iter, rhs)
3619 VEC(ce_s, heap) *lhsc = NULL;
3621 /* Find those pointers being passed, and make sure they end up
3622 pointing to anything. */
3623 if (POINTER_TYPE_P (TREE_TYPE (arg)))
3626 struct constraint_expr *lhsp;
3628 get_constraint_for (arg, &lhsc);
3630 for (j = 0; VEC_iterate (ce_s, lhsc, j, lhsp); j++)
3631 process_constraint_1 (new_constraint (*lhsp, rhsc), true);
3632 VEC_free (ce_s, heap, lhsc);
3637 /* For non-IPA mode, generate constraints necessary for a call
3638 that returns a pointer and assigns it to LHS. This simply makes
3639 the LHS point to anything. */
3642 handle_lhs_call (tree lhs)
3644 VEC(ce_s, heap) *lhsc = NULL;
3645 struct constraint_expr rhsc;
3647 struct constraint_expr *lhsp;
3649 rhsc.var = anything_id;
3651 rhsc.type = ADDRESSOF;
3652 get_constraint_for (lhs, &lhsc);
3653 for (j = 0; VEC_iterate (ce_s, lhsc, j, lhsp); j++)
3654 process_constraint_1 (new_constraint (*lhsp, rhsc), true);
3655 VEC_free (ce_s, heap, lhsc);
3658 /* Walk statement T setting up aliasing constraints according to the
3659 references found in T. This function is the main part of the
3660 constraint builder. AI points to auxiliary alias information used
3661 when building alias sets and computing alias grouping heuristics. */
3664 find_func_aliases (tree origt)
3667 VEC(ce_s, heap) *lhsc = NULL;
3668 VEC(ce_s, heap) *rhsc = NULL;
3669 struct constraint_expr *c;
3671 if (TREE_CODE (t) == RETURN_EXPR && TREE_OPERAND (t, 0))
3672 t = TREE_OPERAND (t, 0);
3674 /* Now build constraints expressions. */
3675 if (TREE_CODE (t) == PHI_NODE)
3677 gcc_assert (!AGGREGATE_TYPE_P (TREE_TYPE (PHI_RESULT (t))));
3679 /* Only care about pointers and structures containing
3681 if (could_have_pointers (PHI_RESULT (t)))
3686 /* For a phi node, assign all the arguments to
3688 get_constraint_for (PHI_RESULT (t), &lhsc);
3689 for (i = 0; i < PHI_NUM_ARGS (t); i++)
3692 tree strippedrhs = PHI_ARG_DEF (t, i);
3694 STRIP_NOPS (strippedrhs);
3695 rhstype = TREE_TYPE (strippedrhs);
3696 get_constraint_for (PHI_ARG_DEF (t, i), &rhsc);
3698 for (j = 0; VEC_iterate (ce_s, lhsc, j, c); j++)
3700 struct constraint_expr *c2;
3701 while (VEC_length (ce_s, rhsc) > 0)
3703 c2 = VEC_last (ce_s, rhsc);
3704 process_constraint (new_constraint (*c, *c2));
3705 VEC_pop (ce_s, rhsc);
3711 /* In IPA mode, we need to generate constraints to pass call
3712 arguments through their calls. There are two cases, either a
3713 GIMPLE_MODIFY_STMT when we are returning a value, or just a plain
3714 CALL_EXPR when we are not.
3716 In non-ipa mode, we need to generate constraints for each
3717 pointer passed by address. */
3718 else if (((TREE_CODE (t) == GIMPLE_MODIFY_STMT
3719 && TREE_CODE (GIMPLE_STMT_OPERAND (t, 1)) == CALL_EXPR
3720 && !(call_expr_flags (GIMPLE_STMT_OPERAND (t, 1))
3721 & (ECF_MALLOC | ECF_MAY_BE_ALLOCA)))
3722 || (TREE_CODE (t) == CALL_EXPR
3723 && !(call_expr_flags (t)
3724 & (ECF_MALLOC | ECF_MAY_BE_ALLOCA)))))
3728 if (TREE_CODE (t) == GIMPLE_MODIFY_STMT)
3730 handle_rhs_call (GIMPLE_STMT_OPERAND (t, 1));
3731 if (POINTER_TYPE_P (TREE_TYPE (GIMPLE_STMT_OPERAND (t, 1))))
3732 handle_lhs_call (GIMPLE_STMT_OPERAND (t, 0));
3735 handle_rhs_call (t);
3742 call_expr_arg_iterator iter;
3746 if (TREE_CODE (t) == GIMPLE_MODIFY_STMT)
3748 lhsop = GIMPLE_STMT_OPERAND (t, 0);
3749 rhsop = GIMPLE_STMT_OPERAND (t, 1);
3756 decl = get_callee_fndecl (rhsop);
3758 /* If we can directly resolve the function being called, do so.
3759 Otherwise, it must be some sort of indirect expression that
3760 we should still be able to handle. */
3763 fi = get_vi_for_tree (decl);
3767 decl = CALL_EXPR_FN (rhsop);
3768 fi = get_vi_for_tree (decl);
3771 /* Assign all the passed arguments to the appropriate incoming
3772 parameters of the function. */
3774 FOR_EACH_CALL_EXPR_ARG (arg, iter, rhsop)
3776 struct constraint_expr lhs ;
3777 struct constraint_expr *rhsp;
3779 get_constraint_for (arg, &rhsc);
3780 if (TREE_CODE (decl) != FUNCTION_DECL)
3789 lhs.var = first_vi_for_offset (fi, i)->id;
3792 while (VEC_length (ce_s, rhsc) != 0)
3794 rhsp = VEC_last (ce_s, rhsc);
3795 process_constraint (new_constraint (lhs, *rhsp));
3796 VEC_pop (ce_s, rhsc);
3801 /* If we are returning a value, assign it to the result. */
3804 struct constraint_expr rhs;
3805 struct constraint_expr *lhsp;
3808 get_constraint_for (lhsop, &lhsc);
3809 if (TREE_CODE (decl) != FUNCTION_DECL)
3818 rhs.var = first_vi_for_offset (fi, i)->id;
3821 for (j = 0; VEC_iterate (ce_s, lhsc, j, lhsp); j++)
3822 process_constraint (new_constraint (*lhsp, rhs));
3826 /* Otherwise, just a regular assignment statement. */
3827 else if (TREE_CODE (t) == GIMPLE_MODIFY_STMT)
3829 tree lhsop = GIMPLE_STMT_OPERAND (t, 0);
3830 tree rhsop = GIMPLE_STMT_OPERAND (t, 1);
3833 if ((AGGREGATE_TYPE_P (TREE_TYPE (lhsop))
3834 || TREE_CODE (TREE_TYPE (lhsop)) == COMPLEX_TYPE)
3835 && (AGGREGATE_TYPE_P (TREE_TYPE (rhsop))
3836 || TREE_CODE (TREE_TYPE (lhsop)) == COMPLEX_TYPE))
3838 do_structure_copy (lhsop, rhsop);
3842 /* Only care about operations with pointers, structures
3843 containing pointers, dereferences, and call expressions. */
3844 if (could_have_pointers (lhsop)
3845 || TREE_CODE (rhsop) == CALL_EXPR)
3847 get_constraint_for (lhsop, &lhsc);
3848 switch (TREE_CODE_CLASS (TREE_CODE (rhsop)))
3850 /* RHS that consist of unary operations,
3851 exceptional types, or bare decls/constants, get
3852 handled directly by get_constraint_for. */
3854 case tcc_declaration:
3856 case tcc_exceptional:
3857 case tcc_expression:
3863 get_constraint_for (rhsop, &rhsc);
3864 for (j = 0; VEC_iterate (ce_s, lhsc, j, c); j++)
3866 struct constraint_expr *c2;
3869 for (k = 0; VEC_iterate (ce_s, rhsc, k, c2); k++)
3870 process_constraint (new_constraint (*c, *c2));
3878 /* For pointer arithmetic of the form
3879 PTR + CST, we can simply use PTR's
3880 constraint because pointer arithmetic is
3881 not allowed to go out of bounds. */
3882 if (handle_ptr_arith (lhsc, rhsop))
3887 /* Otherwise, walk each operand. Notice that we
3888 can't use the operand interface because we need
3889 to process expressions other than simple operands
3890 (e.g. INDIRECT_REF, ADDR_EXPR, CALL_EXPR). */
3892 for (i = 0; i < TREE_OPERAND_LENGTH (rhsop); i++)
3894 tree op = TREE_OPERAND (rhsop, i);
3897 gcc_assert (VEC_length (ce_s, rhsc) == 0);
3898 get_constraint_for (op, &rhsc);
3899 for (j = 0; VEC_iterate (ce_s, lhsc, j, c); j++)
3901 struct constraint_expr *c2;
3902 while (VEC_length (ce_s, rhsc) > 0)
3904 c2 = VEC_last (ce_s, rhsc);
3905 process_constraint (new_constraint (*c, *c2));
3906 VEC_pop (ce_s, rhsc);
3914 else if (TREE_CODE (t) == CHANGE_DYNAMIC_TYPE_EXPR)
3918 get_constraint_for (CHANGE_DYNAMIC_TYPE_LOCATION (t), &lhsc);
3919 for (j = 0; VEC_iterate (ce_s, lhsc, j, c); ++j)
3920 get_varinfo (c->var)->no_tbaa_pruning = true;
3923 /* After promoting variables and computing aliasing we will
3924 need to re-scan most statements. FIXME: Try to minimize the
3925 number of statements re-scanned. It's not really necessary to
3926 re-scan *all* statements. */
3927 mark_stmt_modified (origt);
3928 VEC_free (ce_s, heap, rhsc);
3929 VEC_free (ce_s, heap, lhsc);
3933 /* Find the first varinfo in the same variable as START that overlaps with
3935 Effectively, walk the chain of fields for the variable START to find the
3936 first field that overlaps with OFFSET.
3937 Return NULL if we can't find one. */
3940 first_vi_for_offset (varinfo_t start, unsigned HOST_WIDE_INT offset)
3942 varinfo_t curr = start;
3945 /* We may not find a variable in the field list with the actual
3946 offset when when we have glommed a structure to a variable.
3947 In that case, however, offset should still be within the size
3949 if (offset >= curr->offset && offset < (curr->offset + curr->size))
3957 /* Insert the varinfo FIELD into the field list for BASE, at the front
3961 insert_into_field_list (varinfo_t base, varinfo_t field)
3963 varinfo_t prev = base;
3964 varinfo_t curr = base->next;
3970 /* Insert the varinfo FIELD into the field list for BASE, ordered by
3974 insert_into_field_list_sorted (varinfo_t base, varinfo_t field)
3976 varinfo_t prev = base;
3977 varinfo_t curr = base->next;
3988 if (field->offset <= curr->offset)
3993 field->next = prev->next;
3998 /* qsort comparison function for two fieldoff's PA and PB */
4001 fieldoff_compare (const void *pa, const void *pb)
4003 const fieldoff_s *foa = (const fieldoff_s *)pa;
4004 const fieldoff_s *fob = (const fieldoff_s *)pb;
4005 HOST_WIDE_INT foasize, fobsize;
4007 if (foa->offset != fob->offset)
4008 return foa->offset - fob->offset;
4010 foasize = TREE_INT_CST_LOW (foa->size);
4011 fobsize = TREE_INT_CST_LOW (fob->size);
4012 return foasize - fobsize;
4015 /* Sort a fieldstack according to the field offset and sizes. */
4017 sort_fieldstack (VEC(fieldoff_s,heap) *fieldstack)
4019 qsort (VEC_address (fieldoff_s, fieldstack),
4020 VEC_length (fieldoff_s, fieldstack),
4021 sizeof (fieldoff_s),
4025 /* Given a TYPE, and a vector of field offsets FIELDSTACK, push all
4026 the fields of TYPE onto fieldstack, recording their offsets along
4029 OFFSET is used to keep track of the offset in this entire
4030 structure, rather than just the immediately containing structure.
4031 Returns the number of fields pushed.
4033 HAS_UNION is set to true if we find a union type as a field of
4036 ADDRESSABLE_TYPE is the type of the outermost object that could
4037 have its address taken. */
4040 push_fields_onto_fieldstack (tree type, VEC(fieldoff_s,heap) **fieldstack,
4041 HOST_WIDE_INT offset, bool *has_union,
4042 tree addressable_type)
4046 unsigned int first_element = VEC_length (fieldoff_s, *fieldstack);
4048 /* If the vector of fields is growing too big, bail out early.
4049 Callers check for VEC_length <= MAX_FIELDS_FOR_FIELD_SENSITIVE, make
4051 if (first_element > MAX_FIELDS_FOR_FIELD_SENSITIVE)
4054 if (TREE_CODE (type) == COMPLEX_TYPE)
4056 fieldoff_s *real_part, *img_part;
4057 real_part = VEC_safe_push (fieldoff_s, heap, *fieldstack, NULL);
4058 real_part->type = TREE_TYPE (type);
4059 real_part->size = TYPE_SIZE (TREE_TYPE (type));
4060 real_part->offset = offset;
4061 real_part->decl = NULL_TREE;
4062 real_part->alias_set = -1;
4063 real_part->base_for_components = false;
4065 img_part = VEC_safe_push (fieldoff_s, heap, *fieldstack, NULL);
4066 img_part->type = TREE_TYPE (type);
4067 img_part->size = TYPE_SIZE (TREE_TYPE (type));
4068 img_part->offset = offset + TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (type)));
4069 img_part->decl = NULL_TREE;
4070 img_part->alias_set = -1;
4071 img_part->base_for_components = false;
4076 else if (TREE_CODE (type) == ARRAY_TYPE)
4078 tree sz = TYPE_SIZE (type);
4079 tree elsz = TYPE_SIZE (TREE_TYPE (type));
4084 || ! host_integerp (sz, 1)
4085 || TREE_INT_CST_LOW (sz) == 0
4087 || ! host_integerp (elsz, 1)
4088 || TREE_INT_CST_LOW (elsz) == 0)
4091 nr = TREE_INT_CST_LOW (sz) / TREE_INT_CST_LOW (elsz);
4092 if (nr > SALIAS_MAX_ARRAY_ELEMENTS)
4095 for (i = 0; i < nr; ++i)
4101 && (TREE_CODE (TREE_TYPE (type)) == QUAL_UNION_TYPE
4102 || TREE_CODE (TREE_TYPE (type)) == UNION_TYPE))
4105 if (!AGGREGATE_TYPE_P (TREE_TYPE (type))) /* var_can_have_subvars */
4107 else if (!(pushed = push_fields_onto_fieldstack
4110 offset + i * TREE_INT_CST_LOW (elsz),
4112 (TYPE_NONALIASED_COMPONENT (type)
4114 : TREE_TYPE (type)))))
4115 /* Empty structures may have actual size, like in C++. So
4116 see if we didn't push any subfields and the size is
4117 nonzero, push the field onto the stack */
4124 pair = VEC_safe_push (fieldoff_s, heap, *fieldstack, NULL);
4125 pair->type = TREE_TYPE (type);
4127 pair->decl = NULL_TREE;
4128 pair->offset = offset + i * TREE_INT_CST_LOW (elsz);
4129 if (TYPE_NONALIASED_COMPONENT (type))
4130 pair->alias_set = get_alias_set (addressable_type);
4132 pair->alias_set = -1;
4133 pair->base_for_components = false;
4143 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
4144 if (TREE_CODE (field) == FIELD_DECL)
4150 && (TREE_CODE (TREE_TYPE (field)) == QUAL_UNION_TYPE
4151 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE))
4154 if (!var_can_have_subvars (field))
4156 else if (!(pushed = push_fields_onto_fieldstack
4159 offset + bitpos_of_field (field),
4161 (DECL_NONADDRESSABLE_P (field)
4163 : TREE_TYPE (field))))
4164 && DECL_SIZE (field)
4165 && !integer_zerop (DECL_SIZE (field)))
4166 /* Empty structures may have actual size, like in C++. So
4167 see if we didn't push any subfields and the size is
4168 nonzero, push the field onto the stack */
4175 pair = VEC_safe_push (fieldoff_s, heap, *fieldstack, NULL);
4176 pair->type = TREE_TYPE (field);
4177 pair->size = DECL_SIZE (field);
4179 pair->offset = offset + bitpos_of_field (field);
4180 if (DECL_NONADDRESSABLE_P (field))
4181 pair->alias_set = get_alias_set (addressable_type);
4183 pair->alias_set = -1;
4184 pair->base_for_components = false;
4192 /* Make sure the first pushed field is marked as eligible for
4193 being a base for component references. */
4195 VEC_index (fieldoff_s, *fieldstack, first_element)->base_for_components = true;
4200 /* Create a constraint from ANYTHING variable to VI. */
4202 make_constraint_from_anything (varinfo_t vi)
4204 struct constraint_expr lhs, rhs;
4210 rhs.var = anything_id;
4212 rhs.type = ADDRESSOF;
4213 process_constraint (new_constraint (lhs, rhs));
4216 /* Count the number of arguments DECL has, and set IS_VARARGS to true
4217 if it is a varargs function. */
4220 count_num_arguments (tree decl, bool *is_varargs)
4225 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl));
4229 if (TREE_VALUE (t) == void_type_node)
4239 /* Creation function node for DECL, using NAME, and return the index
4240 of the variable we've created for the function. */
4243 create_function_info_for (tree decl, const char *name)
4245 unsigned int index = VEC_length (varinfo_t, varmap);
4249 bool is_varargs = false;
4251 /* Create the variable info. */
4253 vi = new_var_info (decl, index, name);
4258 vi->fullsize = count_num_arguments (decl, &is_varargs) + 1;
4259 insert_vi_for_tree (vi->decl, vi);
4260 VEC_safe_push (varinfo_t, heap, varmap, vi);
4264 /* If it's varargs, we don't know how many arguments it has, so we
4271 vi->is_unknown_size_var = true;
4276 arg = DECL_ARGUMENTS (decl);
4278 /* Set up variables for each argument. */
4279 for (i = 1; i < vi->fullsize; i++)
4282 const char *newname;
4284 unsigned int newindex;
4285 tree argdecl = decl;
4290 newindex = VEC_length (varinfo_t, varmap);
4291 asprintf (&tempname, "%s.arg%d", name, i-1);
4292 newname = ggc_strdup (tempname);
4295 argvi = new_var_info (argdecl, newindex, newname);
4296 argvi->decl = argdecl;
4297 VEC_safe_push (varinfo_t, heap, varmap, argvi);
4300 argvi->fullsize = vi->fullsize;
4301 argvi->has_union = false;
4302 insert_into_field_list_sorted (vi, argvi);
4303 stats.total_vars ++;
4306 insert_vi_for_tree (arg, argvi);
4307 arg = TREE_CHAIN (arg);
4311 /* Create a variable for the return var. */
4312 if (DECL_RESULT (decl) != NULL
4313 || !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
4316 const char *newname;
4318 unsigned int newindex;
4319 tree resultdecl = decl;
4323 if (DECL_RESULT (decl))
4324 resultdecl = DECL_RESULT (decl);
4326 newindex = VEC_length (varinfo_t, varmap);
4327 asprintf (&tempname, "%s.result", name);
4328 newname = ggc_strdup (tempname);
4331 resultvi = new_var_info (resultdecl, newindex, newname);
4332 resultvi->decl = resultdecl;
4333 VEC_safe_push (varinfo_t, heap, varmap, resultvi);
4334 resultvi->offset = i;
4336 resultvi->fullsize = vi->fullsize;
4337 resultvi->has_union = false;
4338 insert_into_field_list_sorted (vi, resultvi);
4339 stats.total_vars ++;
4340 if (DECL_RESULT (decl))
4341 insert_vi_for_tree (DECL_RESULT (decl), resultvi);
4347 /* Return true if FIELDSTACK contains fields that overlap.
4348 FIELDSTACK is assumed to be sorted by offset. */
4351 check_for_overlaps (VEC (fieldoff_s,heap) *fieldstack)
4353 fieldoff_s *fo = NULL;
4355 HOST_WIDE_INT lastoffset = -1;
4357 for (i = 0; VEC_iterate (fieldoff_s, fieldstack, i, fo); i++)
4359 if (fo->offset == lastoffset)
4361 lastoffset = fo->offset;
4366 /* Create a varinfo structure for NAME and DECL, and add it to VARMAP.
4367 This will also create any varinfo structures necessary for fields
4371 create_variable_info_for (tree decl, const char *name)
4373 unsigned int index = VEC_length (varinfo_t, varmap);
4375 tree decltype = TREE_TYPE (decl);
4376 tree declsize = DECL_P (decl) ? DECL_SIZE (decl) : TYPE_SIZE (decltype);
4377 bool notokay = false;
4379 bool is_global = DECL_P (decl) ? is_global_var (decl) : false;
4380 VEC (fieldoff_s,heap) *fieldstack = NULL;
4382 if (TREE_CODE (decl) == FUNCTION_DECL && in_ipa_mode)
4383 return create_function_info_for (decl, name);
4385 hasunion = TREE_CODE (decltype) == UNION_TYPE
4386 || TREE_CODE (decltype) == QUAL_UNION_TYPE;
4387 if (var_can_have_subvars (decl) && use_field_sensitive && !hasunion)
4389 push_fields_onto_fieldstack (decltype, &fieldstack, 0, &hasunion,
4393 VEC_free (fieldoff_s, heap, fieldstack);
4399 /* If the variable doesn't have subvars, we may end up needing to
4400 sort the field list and create fake variables for all the
4402 vi = new_var_info (decl, index, name);
4405 vi->has_union = hasunion;
4407 || TREE_CODE (declsize) != INTEGER_CST
4408 || TREE_CODE (decltype) == UNION_TYPE
4409 || TREE_CODE (decltype) == QUAL_UNION_TYPE)
4411 vi->is_unknown_size_var = true;
4417 vi->fullsize = TREE_INT_CST_LOW (declsize);
4418 vi->size = vi->fullsize;
4421 insert_vi_for_tree (vi->decl, vi);
4422 VEC_safe_push (varinfo_t, heap, varmap, vi);
4423 if (is_global && (!flag_whole_program || !in_ipa_mode))
4424 make_constraint_from_anything (vi);
4427 if (use_field_sensitive
4429 && !vi->is_unknown_size_var
4430 && var_can_have_subvars (decl)
4431 && VEC_length (fieldoff_s, fieldstack) > 1
4432 && VEC_length (fieldoff_s, fieldstack) <= MAX_FIELDS_FOR_FIELD_SENSITIVE)
4434 unsigned int newindex = VEC_length (varinfo_t, varmap);
4435 fieldoff_s *fo = NULL;
4438 for (i = 0; !notokay && VEC_iterate (fieldoff_s, fieldstack, i, fo); i++)
4441 || TREE_CODE (fo->size) != INTEGER_CST
4449 /* We can't sort them if we have a field with a variable sized type,
4450 which will make notokay = true. In that case, we are going to return
4451 without creating varinfos for the fields anyway, so sorting them is a
4455 sort_fieldstack (fieldstack);
4456 /* Due to some C++ FE issues, like PR 22488, we might end up
4457 what appear to be overlapping fields even though they,
4458 in reality, do not overlap. Until the C++ FE is fixed,
4459 we will simply disable field-sensitivity for these cases. */
4460 notokay = check_for_overlaps (fieldstack);
4464 if (VEC_length (fieldoff_s, fieldstack) != 0)
4465 fo = VEC_index (fieldoff_s, fieldstack, 0);
4467 if (fo == NULL || notokay)
4469 vi->is_unknown_size_var = 1;
4472 VEC_free (fieldoff_s, heap, fieldstack);
4476 vi->size = TREE_INT_CST_LOW (fo->size);
4477 vi->offset = fo->offset;
4478 for (i = VEC_length (fieldoff_s, fieldstack) - 1;
4479 i >= 1 && VEC_iterate (fieldoff_s, fieldstack, i, fo);
4483 const char *newname = "NULL";
4486 newindex = VEC_length (varinfo_t, varmap);
4490 asprintf (&tempname, "%s.%s",
4491 vi->name, alias_get_name (fo->decl));
4493 asprintf (&tempname, "%s." HOST_WIDE_INT_PRINT_DEC,
4494 vi->name, fo->offset);
4495 newname = ggc_strdup (tempname);
4498 newvi = new_var_info (decl, newindex, newname);
4499 newvi->offset = fo->offset;
4500 newvi->size = TREE_INT_CST_LOW (fo->size);
4501 newvi->fullsize = vi->fullsize;
4502 insert_into_field_list (vi, newvi);
4503 VEC_safe_push (varinfo_t, heap, varmap, newvi);
4504 if (is_global && (!flag_whole_program || !in_ipa_mode))
4505 make_constraint_from_anything (newvi);
4511 VEC_free (fieldoff_s, heap, fieldstack);
4516 /* Print out the points-to solution for VAR to FILE. */
4519 dump_solution_for_var (FILE *file, unsigned int var)
4521 varinfo_t vi = get_varinfo (var);
4525 if (find (var) != var)
4527 varinfo_t vipt = get_varinfo (find (var));
4528 fprintf (file, "%s = same as %s\n", vi->name, vipt->name);
4532 fprintf (file, "%s = { ", vi->name);
4533 EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, i, bi)
4535 fprintf (file, "%s ", get_varinfo (i)->name);
4537 fprintf (file, "}");
4538 if (vi->no_tbaa_pruning)
4539 fprintf (file, " no-tbaa-pruning");
4540 fprintf (file, "\n");
4544 /* Print the points-to solution for VAR to stdout. */
4547 debug_solution_for_var (unsigned int var)
4549 dump_solution_for_var (stdout, var);
4552 /* Create varinfo structures for all of the variables in the
4553 function for intraprocedural mode. */
4556 intra_create_variable_infos (void)
4559 struct constraint_expr lhs, rhs;
4561 /* For each incoming pointer argument arg, create the constraint ARG
4562 = ANYTHING or a dummy variable if flag_argument_noalias is set. */
4563 for (t = DECL_ARGUMENTS (current_function_decl); t; t = TREE_CHAIN (t))
4567 if (!could_have_pointers (t))
4570 /* If flag_argument_noalias is set, then function pointer
4571 arguments are guaranteed not to point to each other. In that
4572 case, create an artificial variable PARM_NOALIAS and the
4573 constraint ARG = &PARM_NOALIAS. */
4574 if (POINTER_TYPE_P (TREE_TYPE (t)) && flag_argument_noalias > 0)
4577 tree heapvar = heapvar_lookup (t);
4581 lhs.var = get_vi_for_tree (t)->id;
4583 if (heapvar == NULL_TREE)
4586 heapvar = create_tmp_var_raw (TREE_TYPE (TREE_TYPE (t)),
4588 DECL_EXTERNAL (heapvar) = 1;
4589 if (gimple_referenced_vars (cfun))
4590 add_referenced_var (heapvar);
4592 heapvar_insert (t, heapvar);
4594 ann = get_var_ann (heapvar);
4595 if (flag_argument_noalias == 1)
4596 ann->noalias_state = NO_ALIAS;
4597 else if (flag_argument_noalias == 2)
4598 ann->noalias_state = NO_ALIAS_GLOBAL;
4599 else if (flag_argument_noalias == 3)
4600 ann->noalias_state = NO_ALIAS_ANYTHING;
4605 vi = get_vi_for_tree (heapvar);
4606 vi->is_artificial_var = 1;
4607 vi->is_heap_var = 1;
4609 rhs.type = ADDRESSOF;
4611 for (p = get_varinfo (lhs.var); p; p = p->next)
4613 struct constraint_expr temp = lhs;
4615 process_constraint (new_constraint (temp, rhs));
4620 varinfo_t arg_vi = get_vi_for_tree (t);
4622 for (p = arg_vi; p; p = p->next)
4623 make_constraint_from_anything (p);
4628 /* Structure used to put solution bitmaps in a hashtable so they can
4629 be shared among variables with the same points-to set. */
4631 typedef struct shared_bitmap_info
4635 } *shared_bitmap_info_t;
4636 typedef const struct shared_bitmap_info *const_shared_bitmap_info_t;
4638 static htab_t shared_bitmap_table;
4640 /* Hash function for a shared_bitmap_info_t */
4643 shared_bitmap_hash (const void *p)
4645 const_shared_bitmap_info_t const bi = (const_shared_bitmap_info_t) p;
4646 return bi->hashcode;
4649 /* Equality function for two shared_bitmap_info_t's. */
4652 shared_bitmap_eq (const void *p1, const void *p2)
4654 const_shared_bitmap_info_t const sbi1 = (const_shared_bitmap_info_t) p1;
4655 const_shared_bitmap_info_t const sbi2 = (const_shared_bitmap_info_t) p2;
4656 return bitmap_equal_p (sbi1->pt_vars, sbi2->pt_vars);
4659 /* Lookup a bitmap in the shared bitmap hashtable, and return an already
4660 existing instance if there is one, NULL otherwise. */
4663 shared_bitmap_lookup (bitmap pt_vars)
4666 struct shared_bitmap_info sbi;
4668 sbi.pt_vars = pt_vars;
4669 sbi.hashcode = bitmap_hash (pt_vars);
4671 slot = htab_find_slot_with_hash (shared_bitmap_table, &sbi,
4672 sbi.hashcode, NO_INSERT);
4676 return ((shared_bitmap_info_t) *slot)->pt_vars;
4680 /* Add a bitmap to the shared bitmap hashtable. */
4683 shared_bitmap_add (bitmap pt_vars)
4686 shared_bitmap_info_t sbi = XNEW (struct shared_bitmap_info);
4688 sbi->pt_vars = pt_vars;
4689 sbi->hashcode = bitmap_hash (pt_vars);
4691 slot = htab_find_slot_with_hash (shared_bitmap_table, sbi,
4692 sbi->hashcode, INSERT);
4693 gcc_assert (!*slot);
4694 *slot = (void *) sbi;
4698 /* Set bits in INTO corresponding to the variable uids in solution set
4699 FROM, which came from variable PTR.
4700 For variables that are actually dereferenced, we also use type
4701 based alias analysis to prune the points-to sets.
4702 IS_DEREFED is true if PTR was directly dereferenced, which we use to
4703 help determine whether we are we are allowed to prune using TBAA.
4704 If NO_TBAA_PRUNING is true, we do not perform any TBAA pruning of
4708 set_uids_in_ptset (tree ptr, bitmap into, bitmap from, bool is_derefed,
4709 bool no_tbaa_pruning)
4713 alias_set_type ptr_alias_set;
4715 gcc_assert (POINTER_TYPE_P (TREE_TYPE (ptr)));
4716 ptr_alias_set = get_alias_set (TREE_TYPE (TREE_TYPE (ptr)));
4718 EXECUTE_IF_SET_IN_BITMAP (from, 0, i, bi)
4720 varinfo_t vi = get_varinfo (i);
4721 alias_set_type var_alias_set;
4723 /* The only artificial variables that are allowed in a may-alias
4724 set are heap variables. */
4725 if (vi->is_artificial_var && !vi->is_heap_var)
4728 if (vi->has_union && get_subvars_for_var (vi->decl) != NULL)
4732 subvar_t sv = get_subvars_for_var (vi->decl);
4734 /* Variables containing unions may need to be converted to
4735 their SFT's, because SFT's can have unions and we cannot. */
4736 for (i = 0; VEC_iterate (tree, sv, i, subvar); ++i)
4737 bitmap_set_bit (into, DECL_UID (subvar));
4739 else if (TREE_CODE (vi->decl) == VAR_DECL
4740 || TREE_CODE (vi->decl) == PARM_DECL
4741 || TREE_CODE (vi->decl) == RESULT_DECL)
4744 if (var_can_have_subvars (vi->decl)
4745 && (sv = get_subvars_for_var (vi->decl)))
4747 /* If VI->DECL is an aggregate for which we created
4748 SFTs, add the SFT corresponding to VI->OFFSET.
4749 If we didn't do field-sensitive PTA we need to to
4750 add all overlapping SFTs. */
4752 tree sft = get_first_overlapping_subvar (sv, vi->offset,
4755 for (; VEC_iterate (tree, sv, j, sft); ++j)
4757 if (SFT_OFFSET (sft) > vi->offset
4758 && vi->size <= SFT_OFFSET (sft) - vi->offset)
4761 var_alias_set = get_alias_set (sft);
4763 || (!is_derefed && !vi->directly_dereferenced)
4764 || alias_sets_conflict_p (ptr_alias_set, var_alias_set))
4766 bitmap_set_bit (into, DECL_UID (sft));
4768 /* Pointed-to SFTs are needed by the operand scanner
4769 to adjust offsets when adding operands to memory
4770 expressions that dereference PTR. This means
4771 that memory partitioning may not partition
4772 this SFT because the operand scanner will not
4773 be able to find the other SFTs next to this
4774 one. But we only need to do this if the pointed
4775 to type is aggregate. */
4776 if (SFT_BASE_FOR_COMPONENTS_P (sft))
4777 SFT_UNPARTITIONABLE_P (sft) = true;
4783 /* Otherwise, just add VI->DECL to the alias set.
4784 Don't type prune artificial vars. */
4785 if (vi->is_artificial_var)
4786 bitmap_set_bit (into, DECL_UID (vi->decl));
4789 var_alias_set = get_alias_set (vi->decl);
4791 || (!is_derefed && !vi->directly_dereferenced)
4792 || alias_sets_conflict_p (ptr_alias_set, var_alias_set))
4793 bitmap_set_bit (into, DECL_UID (vi->decl));
4801 static bool have_alias_info = false;
4803 /* The list of SMT's that are in use by our pointer variables. This
4804 is the set of SMT's for all pointers that can point to anything. */
4805 static bitmap used_smts;
4807 /* Due to the ordering of points-to set calculation and SMT
4808 calculation being a bit co-dependent, we can't just calculate SMT
4809 used info whenever we want, we have to calculate it around the time
4810 that find_what_p_points_to is called. */
4812 /* Mark which SMT's are in use by points-to anything variables. */
4815 set_used_smts (void)
4819 used_smts = BITMAP_ALLOC (&pta_obstack);
4821 for (i = 0; VEC_iterate (varinfo_t, varmap, i, vi); i++)
4823 tree var = vi->decl;
4824 varinfo_t withsolution = get_varinfo (find (i));
4827 struct ptr_info_def *pi = NULL;
4829 /* For parm decls, the pointer info may be under the default
4831 if (TREE_CODE (vi->decl) == PARM_DECL
4832 && gimple_default_def (cfun, var))
4833 pi = SSA_NAME_PTR_INFO (gimple_default_def (cfun, var));
4834 else if (TREE_CODE (var) == SSA_NAME)
4835 pi = SSA_NAME_PTR_INFO (var);
4837 /* Skip the special variables and those that can't be aliased. */
4838 if (vi->is_special_var
4840 || (pi && !pi->is_dereferenced)
4841 || (TREE_CODE (var) == VAR_DECL && !may_be_aliased (var))
4842 || !POINTER_TYPE_P (TREE_TYPE (var)))
4845 if (TREE_CODE (var) == SSA_NAME)
4846 var = SSA_NAME_VAR (var);
4852 smt = va->symbol_mem_tag;
4853 if (smt && bitmap_bit_p (withsolution->solution, anything_id))
4854 bitmap_set_bit (used_smts, DECL_UID (smt));
4858 /* Merge the necessary SMT's into the bitmap INTO, which is
4859 P's varinfo. This involves merging all SMT's that are a subset of
4860 the SMT necessary for P. */
4863 merge_smts_into (tree p, bitmap solution)
4871 if (TREE_CODE (p) == SSA_NAME)
4872 var = SSA_NAME_VAR (p);
4874 smt = var_ann (var)->symbol_mem_tag;
4877 alias_set_type smtset = get_alias_set (TREE_TYPE (smt));
4879 /* Need to set the SMT subsets first before this
4880 will work properly. */
4881 bitmap_set_bit (solution, DECL_UID (smt));
4882 EXECUTE_IF_SET_IN_BITMAP (used_smts, 0, i, bi)
4884 tree newsmt = referenced_var (i);
4885 tree newsmttype = TREE_TYPE (newsmt);
4887 if (alias_set_subset_of (get_alias_set (newsmttype),
4889 bitmap_set_bit (solution, i);
4892 aliases = MTAG_ALIASES (smt);
4894 bitmap_ior_into (solution, aliases);
4898 /* Given a pointer variable P, fill in its points-to set, or return
4900 Rather than return false for variables that point-to anything, we
4901 instead find the corresponding SMT, and merge in its aliases. In
4902 addition to these aliases, we also set the bits for the SMT's
4903 themselves and their subsets, as SMT's are still in use by
4904 non-SSA_NAME's, and pruning may eliminate every one of their
4905 aliases. In such a case, if we did not include the right set of
4906 SMT's in the points-to set of the variable, we'd end up with
4907 statements that do not conflict but should. */
4910 find_what_p_points_to (tree p)
4915 if (!have_alias_info)
4918 /* For parameters, get at the points-to set for the actual parm
4920 if (TREE_CODE (p) == SSA_NAME
4921 && TREE_CODE (SSA_NAME_VAR (p)) == PARM_DECL
4922 && SSA_NAME_IS_DEFAULT_DEF (p))
4923 lookup_p = SSA_NAME_VAR (p);
4925 vi = lookup_vi_for_tree (lookup_p);
4928 if (vi->is_artificial_var)
4931 /* See if this is a field or a structure. */
4932 if (vi->size != vi->fullsize)
4934 /* Nothing currently asks about structure fields directly,
4935 but when they do, we need code here to hand back the
4937 if (!var_can_have_subvars (vi->decl)
4938 || get_subvars_for_var (vi->decl) == NULL)
4943 struct ptr_info_def *pi = get_ptr_info (p);
4946 bool was_pt_anything = false;
4947 bitmap finished_solution;
4950 if (!pi->is_dereferenced)
4953 /* This variable may have been collapsed, let's get the real
4955 vi = get_varinfo (find (vi->id));
4957 /* Translate artificial variables into SSA_NAME_PTR_INFO
4959 EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, i, bi)
4961 varinfo_t vi = get_varinfo (i);
4963 if (vi->is_artificial_var)
4965 /* FIXME. READONLY should be handled better so that
4966 flow insensitive aliasing can disregard writable
4968 if (vi->id == nothing_id)
4970 else if (vi->id == anything_id)
4971 was_pt_anything = 1;
4972 else if (vi->id == readonly_id)
4973 was_pt_anything = 1;
4974 else if (vi->id == integer_id)
4975 was_pt_anything = 1;
4976 else if (vi->is_heap_var)
4977 pi->pt_global_mem = 1;
4981 /* Share the final set of variables when possible. */
4982 finished_solution = BITMAP_GGC_ALLOC ();
4983 stats.points_to_sets_created++;
4985 /* Instead of using pt_anything, we merge in the SMT aliases
4986 for the underlying SMT. In addition, if they could have
4987 pointed to anything, they could point to global memory.
4988 But we cannot do that for ref-all pointers because these
4989 aliases have not been computed yet. */
4990 if (was_pt_anything)
4992 if (PTR_IS_REF_ALL (p))
4994 pi->pt_anything = 1;
4998 merge_smts_into (p, finished_solution);
4999 pi->pt_global_mem = 1;
5002 set_uids_in_ptset (p, finished_solution, vi->solution,
5003 vi->directly_dereferenced,
5004 vi->no_tbaa_pruning);
5005 result = shared_bitmap_lookup (finished_solution);
5009 shared_bitmap_add (finished_solution);
5010 pi->pt_vars = finished_solution;
5014 pi->pt_vars = result;
5015 bitmap_clear (finished_solution);
5018 if (bitmap_empty_p (pi->pt_vars))
5030 /* Dump points-to information to OUTFILE. */
5033 dump_sa_points_to_info (FILE *outfile)
5037 fprintf (outfile, "\nPoints-to sets\n\n");
5039 if (dump_flags & TDF_STATS)
5041 fprintf (outfile, "Stats:\n");
5042 fprintf (outfile, "Total vars: %d\n", stats.total_vars);
5043 fprintf (outfile, "Non-pointer vars: %d\n",
5044 stats.nonpointer_vars);
5045 fprintf (outfile, "Statically unified vars: %d\n",
5046 stats.unified_vars_static);
5047 fprintf (outfile, "Dynamically unified vars: %d\n",
5048 stats.unified_vars_dynamic);
5049 fprintf (outfile, "Iterations: %d\n", stats.iterations);
5050 fprintf (outfile, "Number of edges: %d\n", stats.num_edges);
5051 fprintf (outfile, "Number of implicit edges: %d\n",
5052 stats.num_implicit_edges);
5055 for (i = 0; i < VEC_length (varinfo_t, varmap); i++)
5056 dump_solution_for_var (outfile, i);
5060 /* Debug points-to information to stderr. */
5063 debug_sa_points_to_info (void)
5065 dump_sa_points_to_info (stderr);
5069 /* Initialize the always-existing constraint variables for NULL
5070 ANYTHING, READONLY, and INTEGER */
5073 init_base_vars (void)
5075 struct constraint_expr lhs, rhs;
5077 /* Create the NULL variable, used to represent that a variable points
5079 nothing_tree = create_tmp_var_raw (void_type_node, "NULL");
5080 var_nothing = new_var_info (nothing_tree, 0, "NULL");
5081 insert_vi_for_tree (nothing_tree, var_nothing);
5082 var_nothing->is_artificial_var = 1;
5083 var_nothing->offset = 0;
5084 var_nothing->size = ~0;
5085 var_nothing->fullsize = ~0;
5086 var_nothing->is_special_var = 1;
5088 VEC_safe_push (varinfo_t, heap, varmap, var_nothing);
5090 /* Create the ANYTHING variable, used to represent that a variable
5091 points to some unknown piece of memory. */
5092 anything_tree = create_tmp_var_raw (void_type_node, "ANYTHING");
5093 var_anything = new_var_info (anything_tree, 1, "ANYTHING");
5094 insert_vi_for_tree (anything_tree, var_anything);
5095 var_anything->is_artificial_var = 1;
5096 var_anything->size = ~0;
5097 var_anything->offset = 0;
5098 var_anything->next = NULL;
5099 var_anything->fullsize = ~0;
5100 var_anything->is_special_var = 1;
5103 /* Anything points to anything. This makes deref constraints just
5104 work in the presence of linked list and other p = *p type loops,
5105 by saying that *ANYTHING = ANYTHING. */
5106 VEC_safe_push (varinfo_t, heap, varmap, var_anything);
5108 lhs.var = anything_id;
5110 rhs.type = ADDRESSOF;
5111 rhs.var = anything_id;
5114 /* This specifically does not use process_constraint because
5115 process_constraint ignores all anything = anything constraints, since all
5116 but this one are redundant. */
5117 VEC_safe_push (constraint_t, heap, constraints, new_constraint (lhs, rhs));
5119 /* Create the READONLY variable, used to represent that a variable
5120 points to readonly memory. */
5121 readonly_tree = create_tmp_var_raw (void_type_node, "READONLY");
5122 var_readonly = new_var_info (readonly_tree, 2, "READONLY");
5123 var_readonly->is_artificial_var = 1;
5124 var_readonly->offset = 0;
5125 var_readonly->size = ~0;
5126 var_readonly->fullsize = ~0;
5127 var_readonly->next = NULL;
5128 var_readonly->is_special_var = 1;
5129 insert_vi_for_tree (readonly_tree, var_readonly);
5131 VEC_safe_push (varinfo_t, heap, varmap, var_readonly);
5133 /* readonly memory points to anything, in order to make deref
5134 easier. In reality, it points to anything the particular
5135 readonly variable can point to, but we don't track this
5138 lhs.var = readonly_id;
5140 rhs.type = ADDRESSOF;
5141 rhs.var = anything_id;
5144 process_constraint (new_constraint (lhs, rhs));
5146 /* Create the INTEGER variable, used to represent that a variable points
5148 integer_tree = create_tmp_var_raw (void_type_node, "INTEGER");
5149 var_integer = new_var_info (integer_tree, 3, "INTEGER");
5150 insert_vi_for_tree (integer_tree, var_integer);
5151 var_integer->is_artificial_var = 1;
5152 var_integer->size = ~0;
5153 var_integer->fullsize = ~0;
5154 var_integer->offset = 0;
5155 var_integer->next = NULL;
5156 var_integer->is_special_var = 1;
5158 VEC_safe_push (varinfo_t, heap, varmap, var_integer);
5160 /* INTEGER = ANYTHING, because we don't know where a dereference of
5161 a random integer will point to. */
5163 lhs.var = integer_id;
5165 rhs.type = ADDRESSOF;
5166 rhs.var = anything_id;
5168 process_constraint (new_constraint (lhs, rhs));
5171 /* Initialize things necessary to perform PTA */
5174 init_alias_vars (void)
5176 bitmap_obstack_initialize (&pta_obstack);
5177 bitmap_obstack_initialize (&oldpta_obstack);
5178 bitmap_obstack_initialize (&predbitmap_obstack);
5180 constraint_pool = create_alloc_pool ("Constraint pool",
5181 sizeof (struct constraint), 30);
5182 variable_info_pool = create_alloc_pool ("Variable info pool",
5183 sizeof (struct variable_info), 30);
5184 constraints = VEC_alloc (constraint_t, heap, 8);
5185 varmap = VEC_alloc (varinfo_t, heap, 8);
5186 vi_for_tree = pointer_map_create ();
5188 memset (&stats, 0, sizeof (stats));
5189 shared_bitmap_table = htab_create (511, shared_bitmap_hash,
5190 shared_bitmap_eq, free);
5194 /* Remove the REF and ADDRESS edges from GRAPH, as well as all the
5195 predecessor edges. */
5198 remove_preds_and_fake_succs (constraint_graph_t graph)
5202 /* Clear the implicit ref and address nodes from the successor
5204 for (i = 0; i < FIRST_REF_NODE; i++)
5206 if (graph->succs[i])
5207 bitmap_clear_range (graph->succs[i], FIRST_REF_NODE,
5208 FIRST_REF_NODE * 2);
5211 /* Free the successor list for the non-ref nodes. */
5212 for (i = FIRST_REF_NODE; i < graph->size; i++)
5214 if (graph->succs[i])
5215 BITMAP_FREE (graph->succs[i]);
5218 /* Now reallocate the size of the successor list as, and blow away
5219 the predecessor bitmaps. */
5220 graph->size = VEC_length (varinfo_t, varmap);
5221 graph->succs = XRESIZEVEC (bitmap, graph->succs, graph->size);
5223 free (graph->implicit_preds);
5224 graph->implicit_preds = NULL;
5225 free (graph->preds);
5226 graph->preds = NULL;
5227 bitmap_obstack_release (&predbitmap_obstack);
5230 /* Compute the set of variables we can't TBAA prune. */
5233 compute_tbaa_pruning (void)
5235 unsigned int size = VEC_length (varinfo_t, varmap);
5240 changed = sbitmap_alloc (size);
5241 sbitmap_zero (changed);
5243 /* Mark all initial no_tbaa_pruning nodes as changed. */
5245 for (i = 0; i < size; ++i)
5247 varinfo_t ivi = get_varinfo (i);
5249 if (find (i) == i && ivi->no_tbaa_pruning)
5252 if ((graph->succs[i] && !bitmap_empty_p (graph->succs[i]))
5253 || VEC_length (constraint_t, graph->complex[i]) > 0)
5255 SET_BIT (changed, i);
5261 while (changed_count > 0)
5263 struct topo_info *ti = init_topo_info ();
5266 compute_topo_order (graph, ti);
5268 while (VEC_length (unsigned, ti->topo_order) != 0)
5272 i = VEC_pop (unsigned, ti->topo_order);
5274 /* If this variable is not a representative, skip it. */
5278 /* If the node has changed, we need to process the complex
5279 constraints and outgoing edges again. */
5280 if (TEST_BIT (changed, i))
5284 VEC(constraint_t,heap) *complex = graph->complex[i];
5286 RESET_BIT (changed, i);
5289 /* Process the complex copy constraints. */
5290 for (j = 0; VEC_iterate (constraint_t, complex, j, c); ++j)
5292 if (c->lhs.type == SCALAR && c->rhs.type == SCALAR)
5294 varinfo_t lhsvi = get_varinfo (find (c->lhs.var));
5296 if (!lhsvi->no_tbaa_pruning)
5298 lhsvi->no_tbaa_pruning = true;
5299 if (!TEST_BIT (changed, lhsvi->id))
5301 SET_BIT (changed, lhsvi->id);
5308 /* Propagate to all successors. */
5309 EXECUTE_IF_IN_NONNULL_BITMAP (graph->succs[i], 0, j, bi)
5311 unsigned int to = find (j);
5312 varinfo_t tovi = get_varinfo (to);
5314 /* Don't propagate to ourselves. */
5318 if (!tovi->no_tbaa_pruning)
5320 tovi->no_tbaa_pruning = true;
5321 if (!TEST_BIT (changed, to))
5323 SET_BIT (changed, to);
5331 free_topo_info (ti);
5334 sbitmap_free (changed);
5338 for (i = 0; i < size; ++i)
5340 varinfo_t ivi = get_varinfo (i);
5341 varinfo_t ivip = get_varinfo (find (i));
5343 if (ivip->no_tbaa_pruning)
5345 tree var = ivi->decl;
5347 if (TREE_CODE (var) == SSA_NAME)
5348 var = SSA_NAME_VAR (var);
5350 if (POINTER_TYPE_P (TREE_TYPE (var)))
5352 DECL_NO_TBAA_P (var) = 1;
5354 /* Tell the RTL layer that this pointer can alias
5356 DECL_POINTER_ALIAS_SET (var) = 0;
5363 /* Create points-to sets for the current function. See the comments
5364 at the start of the file for an algorithmic overview. */
5367 compute_points_to_sets (struct alias_info *ai)
5369 struct scc_info *si;
5372 timevar_push (TV_TREE_PTA);
5375 init_alias_heapvars ();
5377 intra_create_variable_infos ();
5379 /* Now walk all statements and derive aliases. */
5382 block_stmt_iterator bsi;
5385 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
5387 if (is_gimple_reg (PHI_RESULT (phi)))
5389 find_func_aliases (phi);
5391 /* Update various related attributes like escaped
5392 addresses, pointer dereferences for loads and stores.
5393 This is used when creating name tags and alias
5395 update_alias_info (phi, ai);
5399 for (bsi = bsi_start (bb); !bsi_end_p (bsi); )
5401 tree stmt = bsi_stmt (bsi);
5403 find_func_aliases (stmt);
5405 /* Update various related attributes like escaped
5406 addresses, pointer dereferences for loads and stores.
5407 This is used when creating name tags and alias
5409 update_alias_info (stmt, ai);
5411 /* The information in CHANGE_DYNAMIC_TYPE_EXPR nodes has now
5412 been captured, and we can remove them. */
5413 if (TREE_CODE (stmt) == CHANGE_DYNAMIC_TYPE_EXPR)
5414 bsi_remove (&bsi, true);
5423 fprintf (dump_file, "Points-to analysis\n\nConstraints:\n\n");
5424 dump_constraints (dump_file);
5429 "\nCollapsing static cycles and doing variable "
5432 init_graph (VEC_length (varinfo_t, varmap) * 2);
5435 fprintf (dump_file, "Building predecessor graph\n");
5436 build_pred_graph ();
5439 fprintf (dump_file, "Detecting pointer and location "
5441 si = perform_var_substitution (graph);
5444 fprintf (dump_file, "Rewriting constraints and unifying "
5446 rewrite_constraints (graph, si);
5447 free_var_substitution_info (si);
5449 build_succ_graph ();
5450 move_complex_constraints (graph);
5453 fprintf (dump_file, "Uniting pointer but not location equivalent "
5455 unite_pointer_equivalences (graph);
5458 fprintf (dump_file, "Finding indirect cycles\n");
5459 find_indirect_cycles (graph);
5461 /* Implicit nodes and predecessors are no longer necessary at this
5463 remove_preds_and_fake_succs (graph);
5466 fprintf (dump_file, "Solving graph\n");
5468 solve_graph (graph);
5470 compute_tbaa_pruning ();
5473 dump_sa_points_to_info (dump_file);
5475 have_alias_info = true;
5477 timevar_pop (TV_TREE_PTA);
5481 /* Delete created points-to sets. */
5484 delete_points_to_sets (void)
5488 htab_delete (shared_bitmap_table);
5489 if (dump_file && (dump_flags & TDF_STATS))
5490 fprintf (dump_file, "Points to sets created:%d\n",
5491 stats.points_to_sets_created);
5493 pointer_map_destroy (vi_for_tree);
5494 bitmap_obstack_release (&pta_obstack);
5495 VEC_free (constraint_t, heap, constraints);
5497 for (i = 0; i < graph->size; i++)
5498 VEC_free (constraint_t, heap, graph->complex[i]);
5499 free (graph->complex);
5502 free (graph->succs);
5504 free (graph->pe_rep);
5505 free (graph->indirect_cycles);
5508 VEC_free (varinfo_t, heap, varmap);
5509 free_alloc_pool (variable_info_pool);
5510 free_alloc_pool (constraint_pool);
5511 have_alias_info = false;
5514 /* Return true if we should execute IPA PTA. */
5518 return (flag_unit_at_a_time != 0
5520 /* Don't bother doing anything if the program has errors. */
5521 && !(errorcount || sorrycount));
5524 /* Execute the driver for IPA PTA. */
5526 ipa_pta_execute (void)
5528 struct cgraph_node *node;
5529 struct scc_info *si;
5532 init_alias_heapvars ();
5535 for (node = cgraph_nodes; node; node = node->next)
5537 if (!node->analyzed || cgraph_is_master_clone (node))
5541 varid = create_function_info_for (node->decl,
5542 cgraph_node_name (node));
5543 if (node->local.externally_visible)
5545 varinfo_t fi = get_varinfo (varid);
5546 for (; fi; fi = fi->next)
5547 make_constraint_from_anything (fi);
5551 for (node = cgraph_nodes; node; node = node->next)
5553 if (node->analyzed && cgraph_is_master_clone (node))
5555 struct function *func = DECL_STRUCT_FUNCTION (node->decl);
5557 tree old_func_decl = current_function_decl;
5560 "Generating constraints for %s\n",
5561 cgraph_node_name (node));
5563 current_function_decl = node->decl;
5565 FOR_EACH_BB_FN (bb, func)
5567 block_stmt_iterator bsi;
5570 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
5572 if (is_gimple_reg (PHI_RESULT (phi)))
5574 find_func_aliases (phi);
5578 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
5580 tree stmt = bsi_stmt (bsi);
5581 find_func_aliases (stmt);
5584 current_function_decl = old_func_decl;
5589 /* Make point to anything. */
5595 fprintf (dump_file, "Points-to analysis\n\nConstraints:\n\n");
5596 dump_constraints (dump_file);
5601 "\nCollapsing static cycles and doing variable "
5604 init_graph (VEC_length (varinfo_t, varmap) * 2);
5605 build_pred_graph ();
5606 si = perform_var_substitution (graph);
5607 rewrite_constraints (graph, si);
5608 free_var_substitution_info (si);
5610 build_succ_graph ();
5611 move_complex_constraints (graph);
5612 unite_pointer_equivalences (graph);
5613 find_indirect_cycles (graph);
5615 /* Implicit nodes and predecessors are no longer necessary at this
5617 remove_preds_and_fake_succs (graph);
5620 fprintf (dump_file, "\nSolving graph\n");
5622 solve_graph (graph);
5625 dump_sa_points_to_info (dump_file);
5628 delete_alias_heapvars ();
5629 delete_points_to_sets ();
5633 struct simple_ipa_opt_pass pass_ipa_pta =
5638 gate_ipa_pta, /* gate */
5639 ipa_pta_execute, /* execute */
5642 0, /* static_pass_number */
5643 TV_IPA_PTA, /* tv_id */
5644 0, /* properties_required */
5645 0, /* properties_provided */
5646 0, /* properties_destroyed */
5647 0, /* todo_flags_start */
5648 TODO_update_ssa /* todo_flags_finish */
5652 /* Initialize the heapvar for statement mapping. */
5654 init_alias_heapvars (void)
5656 if (!heapvar_for_stmt)
5657 heapvar_for_stmt = htab_create_ggc (11, tree_map_hash, tree_map_eq,
5662 delete_alias_heapvars (void)
5664 htab_delete (heapvar_for_stmt);
5665 heapvar_for_stmt = NULL;
5669 #include "gt-tree-ssa-structalias.h"