1 /* Tree based points-to analysis
2 Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011
3 Free Software Foundation, Inc.
4 Contributed by Daniel Berlin <dberlin@dberlin.org>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
30 #include "basic-block.h"
33 #include "tree-flow.h"
34 #include "tree-inline.h"
35 #include "diagnostic-core.h"
40 #include "tree-pass.h"
42 #include "alloc-pool.h"
43 #include "splay-tree.h"
47 #include "pointer-set.h"
49 /* The idea behind this analyzer is to generate set constraints from the
50 program, then solve the resulting constraints in order to generate the
53 Set constraints are a way of modeling program analysis problems that
54 involve sets. They consist of an inclusion constraint language,
55 describing the variables (each variable is a set) and operations that
56 are involved on the variables, and a set of rules that derive facts
57 from these operations. To solve a system of set constraints, you derive
58 all possible facts under the rules, which gives you the correct sets
61 See "Efficient Field-sensitive pointer analysis for C" by "David
62 J. Pearce and Paul H. J. Kelly and Chris Hankin, at
63 http://citeseer.ist.psu.edu/pearce04efficient.html
65 Also see "Ultra-fast Aliasing Analysis using CLA: A Million Lines
66 of C Code in a Second" by ""Nevin Heintze and Olivier Tardieu" at
67 http://citeseer.ist.psu.edu/heintze01ultrafast.html
69 There are three types of real constraint expressions, DEREF,
70 ADDRESSOF, and SCALAR. Each constraint expression consists
71 of a constraint type, a variable, and an offset.
73 SCALAR is a constraint expression type used to represent x, whether
74 it appears on the LHS or the RHS of a statement.
75 DEREF is a constraint expression type used to represent *x, whether
76 it appears on the LHS or the RHS of a statement.
77 ADDRESSOF is a constraint expression used to represent &x, whether
78 it appears on the LHS or the RHS of a statement.
80 Each pointer variable in the program is assigned an integer id, and
81 each field of a structure variable is assigned an integer id as well.
83 Structure variables are linked to their list of fields through a "next
84 field" in each variable that points to the next field in offset
86 Each variable for a structure field has
88 1. "size", that tells the size in bits of that field.
89 2. "fullsize, that tells the size in bits of the entire structure.
90 3. "offset", that tells the offset in bits from the beginning of the
91 structure to this field.
103 foo.a -> id 1, size 32, offset 0, fullsize 64, next foo.b
104 foo.b -> id 2, size 32, offset 32, fullsize 64, next NULL
105 bar -> id 3, size 32, offset 0, fullsize 32, next NULL
108 In order to solve the system of set constraints, the following is
111 1. Each constraint variable x has a solution set associated with it,
114 2. Constraints are separated into direct, copy, and complex.
115 Direct constraints are ADDRESSOF constraints that require no extra
116 processing, such as P = &Q
117 Copy constraints are those of the form P = Q.
118 Complex constraints are all the constraints involving dereferences
119 and offsets (including offsetted copies).
121 3. All direct constraints of the form P = &Q are processed, such
122 that Q is added to Sol(P)
124 4. All complex constraints for a given constraint variable are stored in a
125 linked list attached to that variable's node.
127 5. A directed graph is built out of the copy constraints. Each
128 constraint variable is a node in the graph, and an edge from
129 Q to P is added for each copy constraint of the form P = Q
131 6. The graph is then walked, and solution sets are
132 propagated along the copy edges, such that an edge from Q to P
133 causes Sol(P) <- Sol(P) union Sol(Q).
135 7. As we visit each node, all complex constraints associated with
136 that node are processed by adding appropriate copy edges to the graph, or the
137 appropriate variables to the solution set.
139 8. The process of walking the graph is iterated until no solution
142 Prior to walking the graph in steps 6 and 7, We perform static
143 cycle elimination on the constraint graph, as well
144 as off-line variable substitution.
146 TODO: Adding offsets to pointer-to-structures can be handled (IE not punted
147 on and turned into anything), but isn't. You can just see what offset
148 inside the pointed-to struct it's going to access.
150 TODO: Constant bounded arrays can be handled as if they were structs of the
151 same number of elements.
153 TODO: Modeling heap and incoming pointers becomes much better if we
154 add fields to them as we discover them, which we could do.
156 TODO: We could handle unions, but to be honest, it's probably not
157 worth the pain or slowdown. */
159 /* IPA-PTA optimizations possible.
161 When the indirect function called is ANYTHING we can add disambiguation
162 based on the function signatures (or simply the parameter count which
163 is the varinfo size). We also do not need to consider functions that
164 do not have their address taken.
166 The is_global_var bit which marks escape points is overly conservative
167 in IPA mode. Split it to is_escape_point and is_global_var - only
168 externally visible globals are escape points in IPA mode. This is
169 also needed to fix the pt_solution_includes_global predicate
170 (and thus ptr_deref_may_alias_global_p).
172 The way we introduce DECL_PT_UID to avoid fixing up all points-to
173 sets in the translation unit when we copy a DECL during inlining
174 pessimizes precision. The advantage is that the DECL_PT_UID keeps
175 compile-time and memory usage overhead low - the points-to sets
176 do not grow or get unshared as they would during a fixup phase.
177 An alternative solution is to delay IPA PTA until after all
178 inlining transformations have been applied.
180 The way we propagate clobber/use information isn't optimized.
181 It should use a new complex constraint that properly filters
182 out local variables of the callee (though that would make
183 the sets invalid after inlining). OTOH we might as well
184 admit defeat to WHOPR and simply do all the clobber/use analysis
185 and propagation after PTA finished but before we threw away
186 points-to information for memory variables. WHOPR and PTA
187 do not play along well anyway - the whole constraint solving
188 would need to be done in WPA phase and it will be very interesting
189 to apply the results to local SSA names during LTRANS phase.
191 We probably should compute a per-function unit-ESCAPE solution
192 propagating it simply like the clobber / uses solutions. The
193 solution can go alongside the non-IPA espaced solution and be
194 used to query which vars escape the unit through a function.
196 We never put function decls in points-to sets so we do not
197 keep the set of called functions for indirect calls.
199 And probably more. */
201 static bool use_field_sensitive = true;
202 static int in_ipa_mode = 0;
204 /* Used for predecessor bitmaps. */
205 static bitmap_obstack predbitmap_obstack;
207 /* Used for points-to sets. */
208 static bitmap_obstack pta_obstack;
210 /* Used for oldsolution members of variables. */
211 static bitmap_obstack oldpta_obstack;
213 /* Used for per-solver-iteration bitmaps. */
214 static bitmap_obstack iteration_obstack;
216 static unsigned int create_variable_info_for (tree, const char *);
217 typedef struct constraint_graph *constraint_graph_t;
218 static void unify_nodes (constraint_graph_t, unsigned int, unsigned int, bool);
221 typedef struct constraint *constraint_t;
223 DEF_VEC_P(constraint_t);
224 DEF_VEC_ALLOC_P(constraint_t,heap);
226 #define EXECUTE_IF_IN_NONNULL_BITMAP(a, b, c, d) \
228 EXECUTE_IF_SET_IN_BITMAP (a, b, c, d)
230 static struct constraint_stats
232 unsigned int total_vars;
233 unsigned int nonpointer_vars;
234 unsigned int unified_vars_static;
235 unsigned int unified_vars_dynamic;
236 unsigned int iterations;
237 unsigned int num_edges;
238 unsigned int num_implicit_edges;
239 unsigned int points_to_sets_created;
244 /* ID of this variable */
247 /* True if this is a variable created by the constraint analysis, such as
248 heap variables and constraints we had to break up. */
249 unsigned int is_artificial_var : 1;
251 /* True if this is a special variable whose solution set should not be
253 unsigned int is_special_var : 1;
255 /* True for variables whose size is not known or variable. */
256 unsigned int is_unknown_size_var : 1;
258 /* True for (sub-)fields that represent a whole variable. */
259 unsigned int is_full_var : 1;
261 /* True if this is a heap variable. */
262 unsigned int is_heap_var : 1;
264 /* True if this field may contain pointers. */
265 unsigned int may_have_pointers : 1;
267 /* True if this field has only restrict qualified pointers. */
268 unsigned int only_restrict_pointers : 1;
270 /* True if this represents a global variable. */
271 unsigned int is_global_var : 1;
273 /* True if this represents a IPA function info. */
274 unsigned int is_fn_info : 1;
276 /* A link to the variable for the next field in this structure. */
277 struct variable_info *next;
279 /* Offset of this variable, in bits, from the base variable */
280 unsigned HOST_WIDE_INT offset;
282 /* Size of the variable, in bits. */
283 unsigned HOST_WIDE_INT size;
285 /* Full size of the base variable, in bits. */
286 unsigned HOST_WIDE_INT fullsize;
288 /* Name of this variable */
291 /* Tree that this variable is associated with. */
294 /* Points-to set for this variable. */
297 /* Old points-to set for this variable. */
300 typedef struct variable_info *varinfo_t;
302 static varinfo_t first_vi_for_offset (varinfo_t, unsigned HOST_WIDE_INT);
303 static varinfo_t first_or_preceding_vi_for_offset (varinfo_t,
304 unsigned HOST_WIDE_INT);
305 static varinfo_t lookup_vi_for_tree (tree);
306 static inline bool type_can_have_subvars (const_tree);
308 /* Pool of variable info structures. */
309 static alloc_pool variable_info_pool;
311 DEF_VEC_P(varinfo_t);
313 DEF_VEC_ALLOC_P(varinfo_t, heap);
315 /* Table of variable info structures for constraint variables.
316 Indexed directly by variable info id. */
317 static VEC(varinfo_t,heap) *varmap;
319 /* Return the varmap element N */
321 static inline varinfo_t
322 get_varinfo (unsigned int n)
324 return VEC_index (varinfo_t, varmap, n);
327 /* Static IDs for the special variables. */
328 enum { nothing_id = 0, anything_id = 1, readonly_id = 2,
329 escaped_id = 3, nonlocal_id = 4,
330 storedanything_id = 5, integer_id = 6 };
332 /* Return a new variable info structure consisting for a variable
333 named NAME, and using constraint graph node NODE. Append it
334 to the vector of variable info structures. */
337 new_var_info (tree t, const char *name)
339 unsigned index = VEC_length (varinfo_t, varmap);
340 varinfo_t ret = (varinfo_t) pool_alloc (variable_info_pool);
345 /* Vars without decl are artificial and do not have sub-variables. */
346 ret->is_artificial_var = (t == NULL_TREE);
347 ret->is_special_var = false;
348 ret->is_unknown_size_var = false;
349 ret->is_full_var = (t == NULL_TREE);
350 ret->is_heap_var = false;
351 ret->may_have_pointers = true;
352 ret->only_restrict_pointers = false;
353 ret->is_global_var = (t == NULL_TREE);
354 ret->is_fn_info = false;
356 ret->is_global_var = (is_global_var (t)
357 /* We have to treat even local register variables
359 || (TREE_CODE (t) == VAR_DECL
360 && DECL_HARD_REGISTER (t)));
361 ret->solution = BITMAP_ALLOC (&pta_obstack);
362 ret->oldsolution = NULL;
367 VEC_safe_push (varinfo_t, heap, varmap, ret);
373 /* A map mapping call statements to per-stmt variables for uses
374 and clobbers specific to the call. */
375 struct pointer_map_t *call_stmt_vars;
377 /* Lookup or create the variable for the call statement CALL. */
380 get_call_vi (gimple call)
385 slot_p = pointer_map_insert (call_stmt_vars, call);
387 return (varinfo_t) *slot_p;
389 vi = new_var_info (NULL_TREE, "CALLUSED");
393 vi->is_full_var = true;
395 vi->next = vi2 = new_var_info (NULL_TREE, "CALLCLOBBERED");
399 vi2->is_full_var = true;
401 *slot_p = (void *) vi;
405 /* Lookup the variable for the call statement CALL representing
406 the uses. Returns NULL if there is nothing special about this call. */
409 lookup_call_use_vi (gimple call)
413 slot_p = pointer_map_contains (call_stmt_vars, call);
415 return (varinfo_t) *slot_p;
420 /* Lookup the variable for the call statement CALL representing
421 the clobbers. Returns NULL if there is nothing special about this call. */
424 lookup_call_clobber_vi (gimple call)
426 varinfo_t uses = lookup_call_use_vi (call);
433 /* Lookup or create the variable for the call statement CALL representing
437 get_call_use_vi (gimple call)
439 return get_call_vi (call);
442 /* Lookup or create the variable for the call statement CALL representing
445 static varinfo_t ATTRIBUTE_UNUSED
446 get_call_clobber_vi (gimple call)
448 return get_call_vi (call)->next;
452 typedef enum {SCALAR, DEREF, ADDRESSOF} constraint_expr_type;
454 /* An expression that appears in a constraint. */
456 struct constraint_expr
458 /* Constraint type. */
459 constraint_expr_type type;
461 /* Variable we are referring to in the constraint. */
464 /* Offset, in bits, of this constraint from the beginning of
465 variables it ends up referring to.
467 IOW, in a deref constraint, we would deref, get the result set,
468 then add OFFSET to each member. */
469 HOST_WIDE_INT offset;
472 /* Use 0x8000... as special unknown offset. */
473 #define UNKNOWN_OFFSET ((HOST_WIDE_INT)-1 << (HOST_BITS_PER_WIDE_INT-1))
475 typedef struct constraint_expr ce_s;
477 DEF_VEC_ALLOC_O(ce_s, heap);
478 static void get_constraint_for_1 (tree, VEC(ce_s, heap) **, bool, bool);
479 static void get_constraint_for (tree, VEC(ce_s, heap) **);
480 static void get_constraint_for_rhs (tree, VEC(ce_s, heap) **);
481 static void do_deref (VEC (ce_s, heap) **);
483 /* Our set constraints are made up of two constraint expressions, one
486 As described in the introduction, our set constraints each represent an
487 operation between set valued variables.
491 struct constraint_expr lhs;
492 struct constraint_expr rhs;
495 /* List of constraints that we use to build the constraint graph from. */
497 static VEC(constraint_t,heap) *constraints;
498 static alloc_pool constraint_pool;
500 /* The constraint graph is represented as an array of bitmaps
501 containing successor nodes. */
503 struct constraint_graph
505 /* Size of this graph, which may be different than the number of
506 nodes in the variable map. */
509 /* Explicit successors of each node. */
512 /* Implicit predecessors of each node (Used for variable
514 bitmap *implicit_preds;
516 /* Explicit predecessors of each node (Used for variable substitution). */
519 /* Indirect cycle representatives, or -1 if the node has no indirect
521 int *indirect_cycles;
523 /* Representative node for a node. rep[a] == a unless the node has
527 /* Equivalence class representative for a label. This is used for
528 variable substitution. */
531 /* Pointer equivalence label for a node. All nodes with the same
532 pointer equivalence label can be unified together at some point
533 (either during constraint optimization or after the constraint
537 /* Pointer equivalence representative for a label. This is used to
538 handle nodes that are pointer equivalent but not location
539 equivalent. We can unite these once the addressof constraints
540 are transformed into initial points-to sets. */
543 /* Pointer equivalence label for each node, used during variable
545 unsigned int *pointer_label;
547 /* Location equivalence label for each node, used during location
548 equivalence finding. */
549 unsigned int *loc_label;
551 /* Pointed-by set for each node, used during location equivalence
552 finding. This is pointed-by rather than pointed-to, because it
553 is constructed using the predecessor graph. */
556 /* Points to sets for pointer equivalence. This is *not* the actual
557 points-to sets for nodes. */
560 /* Bitmap of nodes where the bit is set if the node is a direct
561 node. Used for variable substitution. */
562 sbitmap direct_nodes;
564 /* Bitmap of nodes where the bit is set if the node is address
565 taken. Used for variable substitution. */
566 bitmap address_taken;
568 /* Vector of complex constraints for each graph node. Complex
569 constraints are those involving dereferences or offsets that are
571 VEC(constraint_t,heap) **complex;
574 static constraint_graph_t graph;
576 /* During variable substitution and the offline version of indirect
577 cycle finding, we create nodes to represent dereferences and
578 address taken constraints. These represent where these start and
580 #define FIRST_REF_NODE (VEC_length (varinfo_t, varmap))
581 #define LAST_REF_NODE (FIRST_REF_NODE + (FIRST_REF_NODE - 1))
583 /* Return the representative node for NODE, if NODE has been unioned
585 This function performs path compression along the way to finding
586 the representative. */
589 find (unsigned int node)
591 gcc_assert (node < graph->size);
592 if (graph->rep[node] != node)
593 return graph->rep[node] = find (graph->rep[node]);
597 /* Union the TO and FROM nodes to the TO nodes.
598 Note that at some point in the future, we may want to do
599 union-by-rank, in which case we are going to have to return the
600 node we unified to. */
603 unite (unsigned int to, unsigned int from)
605 gcc_assert (to < graph->size && from < graph->size);
606 if (to != from && graph->rep[from] != to)
608 graph->rep[from] = to;
614 /* Create a new constraint consisting of LHS and RHS expressions. */
617 new_constraint (const struct constraint_expr lhs,
618 const struct constraint_expr rhs)
620 constraint_t ret = (constraint_t) pool_alloc (constraint_pool);
626 /* Print out constraint C to FILE. */
629 dump_constraint (FILE *file, constraint_t c)
631 if (c->lhs.type == ADDRESSOF)
633 else if (c->lhs.type == DEREF)
635 fprintf (file, "%s", get_varinfo (c->lhs.var)->name);
636 if (c->lhs.offset == UNKNOWN_OFFSET)
637 fprintf (file, " + UNKNOWN");
638 else if (c->lhs.offset != 0)
639 fprintf (file, " + " HOST_WIDE_INT_PRINT_DEC, c->lhs.offset);
640 fprintf (file, " = ");
641 if (c->rhs.type == ADDRESSOF)
643 else if (c->rhs.type == DEREF)
645 fprintf (file, "%s", get_varinfo (c->rhs.var)->name);
646 if (c->rhs.offset == UNKNOWN_OFFSET)
647 fprintf (file, " + UNKNOWN");
648 else if (c->rhs.offset != 0)
649 fprintf (file, " + " HOST_WIDE_INT_PRINT_DEC, c->rhs.offset);
653 void debug_constraint (constraint_t);
654 void debug_constraints (void);
655 void debug_constraint_graph (void);
656 void debug_solution_for_var (unsigned int);
657 void debug_sa_points_to_info (void);
659 /* Print out constraint C to stderr. */
662 debug_constraint (constraint_t c)
664 dump_constraint (stderr, c);
665 fprintf (stderr, "\n");
668 /* Print out all constraints to FILE */
671 dump_constraints (FILE *file, int from)
675 for (i = from; VEC_iterate (constraint_t, constraints, i, c); i++)
678 dump_constraint (file, c);
679 fprintf (file, "\n");
683 /* Print out all constraints to stderr. */
686 debug_constraints (void)
688 dump_constraints (stderr, 0);
691 /* Print the constraint graph in dot format. */
694 dump_constraint_graph (FILE *file)
698 /* Only print the graph if it has already been initialized: */
702 /* Prints the header of the dot file: */
703 fprintf (file, "strict digraph {\n");
704 fprintf (file, " node [\n shape = box\n ]\n");
705 fprintf (file, " edge [\n fontsize = \"12\"\n ]\n");
706 fprintf (file, "\n // List of nodes and complex constraints in "
707 "the constraint graph:\n");
709 /* The next lines print the nodes in the graph together with the
710 complex constraints attached to them. */
711 for (i = 0; i < graph->size; i++)
715 if (i < FIRST_REF_NODE)
716 fprintf (file, "\"%s\"", get_varinfo (i)->name);
718 fprintf (file, "\"*%s\"", get_varinfo (i - FIRST_REF_NODE)->name);
719 if (graph->complex[i])
723 fprintf (file, " [label=\"\\N\\n");
724 for (j = 0; VEC_iterate (constraint_t, graph->complex[i], j, c); ++j)
726 dump_constraint (file, c);
727 fprintf (file, "\\l");
729 fprintf (file, "\"]");
731 fprintf (file, ";\n");
734 /* Go over the edges. */
735 fprintf (file, "\n // Edges in the constraint graph:\n");
736 for (i = 0; i < graph->size; i++)
742 EXECUTE_IF_IN_NONNULL_BITMAP (graph->succs[i], 0, j, bi)
744 unsigned to = find (j);
747 if (i < FIRST_REF_NODE)
748 fprintf (file, "\"%s\"", get_varinfo (i)->name);
750 fprintf (file, "\"*%s\"", get_varinfo (i - FIRST_REF_NODE)->name);
751 fprintf (file, " -> ");
752 if (to < FIRST_REF_NODE)
753 fprintf (file, "\"%s\"", get_varinfo (to)->name);
755 fprintf (file, "\"*%s\"", get_varinfo (to - FIRST_REF_NODE)->name);
756 fprintf (file, ";\n");
760 /* Prints the tail of the dot file. */
761 fprintf (file, "}\n");
764 /* Print out the constraint graph to stderr. */
767 debug_constraint_graph (void)
769 dump_constraint_graph (stderr);
774 The solver is a simple worklist solver, that works on the following
777 sbitmap changed_nodes = all zeroes;
779 For each node that is not already collapsed:
781 set bit in changed nodes
783 while (changed_count > 0)
785 compute topological ordering for constraint graph
787 find and collapse cycles in the constraint graph (updating
788 changed if necessary)
790 for each node (n) in the graph in topological order:
793 Process each complex constraint associated with the node,
794 updating changed if necessary.
796 For each outgoing edge from n, propagate the solution from n to
797 the destination of the edge, updating changed as necessary.
801 /* Return true if two constraint expressions A and B are equal. */
804 constraint_expr_equal (struct constraint_expr a, struct constraint_expr b)
806 return a.type == b.type && a.var == b.var && a.offset == b.offset;
809 /* Return true if constraint expression A is less than constraint expression
810 B. This is just arbitrary, but consistent, in order to give them an
814 constraint_expr_less (struct constraint_expr a, struct constraint_expr b)
816 if (a.type == b.type)
819 return a.offset < b.offset;
821 return a.var < b.var;
824 return a.type < b.type;
827 /* Return true if constraint A is less than constraint B. This is just
828 arbitrary, but consistent, in order to give them an ordering. */
831 constraint_less (const constraint_t a, const constraint_t b)
833 if (constraint_expr_less (a->lhs, b->lhs))
835 else if (constraint_expr_less (b->lhs, a->lhs))
838 return constraint_expr_less (a->rhs, b->rhs);
841 /* Return true if two constraints A and B are equal. */
844 constraint_equal (struct constraint a, struct constraint b)
846 return constraint_expr_equal (a.lhs, b.lhs)
847 && constraint_expr_equal (a.rhs, b.rhs);
851 /* Find a constraint LOOKFOR in the sorted constraint vector VEC */
854 constraint_vec_find (VEC(constraint_t,heap) *vec,
855 struct constraint lookfor)
863 place = VEC_lower_bound (constraint_t, vec, &lookfor, constraint_less);
864 if (place >= VEC_length (constraint_t, vec))
866 found = VEC_index (constraint_t, vec, place);
867 if (!constraint_equal (*found, lookfor))
872 /* Union two constraint vectors, TO and FROM. Put the result in TO. */
875 constraint_set_union (VEC(constraint_t,heap) **to,
876 VEC(constraint_t,heap) **from)
881 FOR_EACH_VEC_ELT (constraint_t, *from, i, c)
883 if (constraint_vec_find (*to, *c) == NULL)
885 unsigned int place = VEC_lower_bound (constraint_t, *to, c,
887 VEC_safe_insert (constraint_t, heap, *to, place, c);
892 /* Expands the solution in SET to all sub-fields of variables included.
893 Union the expanded result into RESULT. */
896 solution_set_expand (bitmap result, bitmap set)
902 /* In a first pass record all variables we need to add all
903 sub-fields off. This avoids quadratic behavior. */
904 EXECUTE_IF_SET_IN_BITMAP (set, 0, j, bi)
906 varinfo_t v = get_varinfo (j);
907 if (v->is_artificial_var
910 v = lookup_vi_for_tree (v->decl);
912 vars = BITMAP_ALLOC (NULL);
913 bitmap_set_bit (vars, v->id);
916 /* In the second pass now do the addition to the solution and
917 to speed up solving add it to the delta as well. */
920 EXECUTE_IF_SET_IN_BITMAP (vars, 0, j, bi)
922 varinfo_t v = get_varinfo (j);
923 for (; v != NULL; v = v->next)
924 bitmap_set_bit (result, v->id);
930 /* Take a solution set SET, add OFFSET to each member of the set, and
931 overwrite SET with the result when done. */
934 solution_set_add (bitmap set, HOST_WIDE_INT offset)
936 bitmap result = BITMAP_ALLOC (&iteration_obstack);
940 /* If the offset is unknown we have to expand the solution to
942 if (offset == UNKNOWN_OFFSET)
944 solution_set_expand (set, set);
948 EXECUTE_IF_SET_IN_BITMAP (set, 0, i, bi)
950 varinfo_t vi = get_varinfo (i);
952 /* If this is a variable with just one field just set its bit
954 if (vi->is_artificial_var
955 || vi->is_unknown_size_var
957 bitmap_set_bit (result, i);
960 unsigned HOST_WIDE_INT fieldoffset = vi->offset + offset;
962 /* If the offset makes the pointer point to before the
963 variable use offset zero for the field lookup. */
965 && fieldoffset > vi->offset)
969 vi = first_or_preceding_vi_for_offset (vi, fieldoffset);
971 bitmap_set_bit (result, vi->id);
972 /* If the result is not exactly at fieldoffset include the next
973 field as well. See get_constraint_for_ptr_offset for more
975 if (vi->offset != fieldoffset
977 bitmap_set_bit (result, vi->next->id);
981 bitmap_copy (set, result);
982 BITMAP_FREE (result);
985 /* Union solution sets TO and FROM, and add INC to each member of FROM in the
989 set_union_with_increment (bitmap to, bitmap from, HOST_WIDE_INT inc)
992 return bitmap_ior_into (to, from);
998 tmp = BITMAP_ALLOC (&iteration_obstack);
999 bitmap_copy (tmp, from);
1000 solution_set_add (tmp, inc);
1001 res = bitmap_ior_into (to, tmp);
1007 /* Insert constraint C into the list of complex constraints for graph
1011 insert_into_complex (constraint_graph_t graph,
1012 unsigned int var, constraint_t c)
1014 VEC (constraint_t, heap) *complex = graph->complex[var];
1015 unsigned int place = VEC_lower_bound (constraint_t, complex, c,
1018 /* Only insert constraints that do not already exist. */
1019 if (place >= VEC_length (constraint_t, complex)
1020 || !constraint_equal (*c, *VEC_index (constraint_t, complex, place)))
1021 VEC_safe_insert (constraint_t, heap, graph->complex[var], place, c);
1025 /* Condense two variable nodes into a single variable node, by moving
1026 all associated info from SRC to TO. */
1029 merge_node_constraints (constraint_graph_t graph, unsigned int to,
1035 gcc_assert (find (from) == to);
1037 /* Move all complex constraints from src node into to node */
1038 FOR_EACH_VEC_ELT (constraint_t, graph->complex[from], i, c)
1040 /* In complex constraints for node src, we may have either
1041 a = *src, and *src = a, or an offseted constraint which are
1042 always added to the rhs node's constraints. */
1044 if (c->rhs.type == DEREF)
1046 else if (c->lhs.type == DEREF)
1051 constraint_set_union (&graph->complex[to], &graph->complex[from]);
1052 VEC_free (constraint_t, heap, graph->complex[from]);
1053 graph->complex[from] = NULL;
1057 /* Remove edges involving NODE from GRAPH. */
1060 clear_edges_for_node (constraint_graph_t graph, unsigned int node)
1062 if (graph->succs[node])
1063 BITMAP_FREE (graph->succs[node]);
1066 /* Merge GRAPH nodes FROM and TO into node TO. */
1069 merge_graph_nodes (constraint_graph_t graph, unsigned int to,
1072 if (graph->indirect_cycles[from] != -1)
1074 /* If we have indirect cycles with the from node, and we have
1075 none on the to node, the to node has indirect cycles from the
1076 from node now that they are unified.
1077 If indirect cycles exist on both, unify the nodes that they
1078 are in a cycle with, since we know they are in a cycle with
1080 if (graph->indirect_cycles[to] == -1)
1081 graph->indirect_cycles[to] = graph->indirect_cycles[from];
1084 /* Merge all the successor edges. */
1085 if (graph->succs[from])
1087 if (!graph->succs[to])
1088 graph->succs[to] = BITMAP_ALLOC (&pta_obstack);
1089 bitmap_ior_into (graph->succs[to],
1090 graph->succs[from]);
1093 clear_edges_for_node (graph, from);
1097 /* Add an indirect graph edge to GRAPH, going from TO to FROM if
1098 it doesn't exist in the graph already. */
1101 add_implicit_graph_edge (constraint_graph_t graph, unsigned int to,
1107 if (!graph->implicit_preds[to])
1108 graph->implicit_preds[to] = BITMAP_ALLOC (&predbitmap_obstack);
1110 if (bitmap_set_bit (graph->implicit_preds[to], from))
1111 stats.num_implicit_edges++;
1114 /* Add a predecessor graph edge to GRAPH, going from TO to FROM if
1115 it doesn't exist in the graph already.
1116 Return false if the edge already existed, true otherwise. */
1119 add_pred_graph_edge (constraint_graph_t graph, unsigned int to,
1122 if (!graph->preds[to])
1123 graph->preds[to] = BITMAP_ALLOC (&predbitmap_obstack);
1124 bitmap_set_bit (graph->preds[to], from);
1127 /* Add a graph edge to GRAPH, going from FROM to TO if
1128 it doesn't exist in the graph already.
1129 Return false if the edge already existed, true otherwise. */
1132 add_graph_edge (constraint_graph_t graph, unsigned int to,
1143 if (!graph->succs[from])
1144 graph->succs[from] = BITMAP_ALLOC (&pta_obstack);
1145 if (bitmap_set_bit (graph->succs[from], to))
1148 if (to < FIRST_REF_NODE && from < FIRST_REF_NODE)
1156 /* Return true if {DEST.SRC} is an existing graph edge in GRAPH. */
1159 valid_graph_edge (constraint_graph_t graph, unsigned int src,
1162 return (graph->succs[dest]
1163 && bitmap_bit_p (graph->succs[dest], src));
1166 /* Initialize the constraint graph structure to contain SIZE nodes. */
1169 init_graph (unsigned int size)
1173 graph = XCNEW (struct constraint_graph);
1175 graph->succs = XCNEWVEC (bitmap, graph->size);
1176 graph->indirect_cycles = XNEWVEC (int, graph->size);
1177 graph->rep = XNEWVEC (unsigned int, graph->size);
1178 graph->complex = XCNEWVEC (VEC(constraint_t, heap) *, size);
1179 graph->pe = XCNEWVEC (unsigned int, graph->size);
1180 graph->pe_rep = XNEWVEC (int, graph->size);
1182 for (j = 0; j < graph->size; j++)
1185 graph->pe_rep[j] = -1;
1186 graph->indirect_cycles[j] = -1;
1190 /* Build the constraint graph, adding only predecessor edges right now. */
1193 build_pred_graph (void)
1199 graph->implicit_preds = XCNEWVEC (bitmap, graph->size);
1200 graph->preds = XCNEWVEC (bitmap, graph->size);
1201 graph->pointer_label = XCNEWVEC (unsigned int, graph->size);
1202 graph->loc_label = XCNEWVEC (unsigned int, graph->size);
1203 graph->pointed_by = XCNEWVEC (bitmap, graph->size);
1204 graph->points_to = XCNEWVEC (bitmap, graph->size);
1205 graph->eq_rep = XNEWVEC (int, graph->size);
1206 graph->direct_nodes = sbitmap_alloc (graph->size);
1207 graph->address_taken = BITMAP_ALLOC (&predbitmap_obstack);
1208 sbitmap_zero (graph->direct_nodes);
1210 for (j = 0; j < FIRST_REF_NODE; j++)
1212 if (!get_varinfo (j)->is_special_var)
1213 SET_BIT (graph->direct_nodes, j);
1216 for (j = 0; j < graph->size; j++)
1217 graph->eq_rep[j] = -1;
1219 for (j = 0; j < VEC_length (varinfo_t, varmap); j++)
1220 graph->indirect_cycles[j] = -1;
1222 FOR_EACH_VEC_ELT (constraint_t, constraints, i, c)
1224 struct constraint_expr lhs = c->lhs;
1225 struct constraint_expr rhs = c->rhs;
1226 unsigned int lhsvar = lhs.var;
1227 unsigned int rhsvar = rhs.var;
1229 if (lhs.type == DEREF)
1232 if (rhs.offset == 0 && lhs.offset == 0 && rhs.type == SCALAR)
1233 add_pred_graph_edge (graph, FIRST_REF_NODE + lhsvar, rhsvar);
1235 else if (rhs.type == DEREF)
1238 if (rhs.offset == 0 && lhs.offset == 0 && lhs.type == SCALAR)
1239 add_pred_graph_edge (graph, lhsvar, FIRST_REF_NODE + rhsvar);
1241 RESET_BIT (graph->direct_nodes, lhsvar);
1243 else if (rhs.type == ADDRESSOF)
1248 if (graph->points_to[lhsvar] == NULL)
1249 graph->points_to[lhsvar] = BITMAP_ALLOC (&predbitmap_obstack);
1250 bitmap_set_bit (graph->points_to[lhsvar], rhsvar);
1252 if (graph->pointed_by[rhsvar] == NULL)
1253 graph->pointed_by[rhsvar] = BITMAP_ALLOC (&predbitmap_obstack);
1254 bitmap_set_bit (graph->pointed_by[rhsvar], lhsvar);
1256 /* Implicitly, *x = y */
1257 add_implicit_graph_edge (graph, FIRST_REF_NODE + lhsvar, rhsvar);
1259 /* All related variables are no longer direct nodes. */
1260 RESET_BIT (graph->direct_nodes, rhsvar);
1261 v = get_varinfo (rhsvar);
1262 if (!v->is_full_var)
1264 v = lookup_vi_for_tree (v->decl);
1267 RESET_BIT (graph->direct_nodes, v->id);
1272 bitmap_set_bit (graph->address_taken, rhsvar);
1274 else if (lhsvar > anything_id
1275 && lhsvar != rhsvar && lhs.offset == 0 && rhs.offset == 0)
1278 add_pred_graph_edge (graph, lhsvar, rhsvar);
1279 /* Implicitly, *x = *y */
1280 add_implicit_graph_edge (graph, FIRST_REF_NODE + lhsvar,
1281 FIRST_REF_NODE + rhsvar);
1283 else if (lhs.offset != 0 || rhs.offset != 0)
1285 if (rhs.offset != 0)
1286 RESET_BIT (graph->direct_nodes, lhs.var);
1287 else if (lhs.offset != 0)
1288 RESET_BIT (graph->direct_nodes, rhs.var);
1293 /* Build the constraint graph, adding successor edges. */
1296 build_succ_graph (void)
1301 FOR_EACH_VEC_ELT (constraint_t, constraints, i, c)
1303 struct constraint_expr lhs;
1304 struct constraint_expr rhs;
1305 unsigned int lhsvar;
1306 unsigned int rhsvar;
1313 lhsvar = find (lhs.var);
1314 rhsvar = find (rhs.var);
1316 if (lhs.type == DEREF)
1318 if (rhs.offset == 0 && lhs.offset == 0 && rhs.type == SCALAR)
1319 add_graph_edge (graph, FIRST_REF_NODE + lhsvar, rhsvar);
1321 else if (rhs.type == DEREF)
1323 if (rhs.offset == 0 && lhs.offset == 0 && lhs.type == SCALAR)
1324 add_graph_edge (graph, lhsvar, FIRST_REF_NODE + rhsvar);
1326 else if (rhs.type == ADDRESSOF)
1329 gcc_assert (find (rhs.var) == rhs.var);
1330 bitmap_set_bit (get_varinfo (lhsvar)->solution, rhsvar);
1332 else if (lhsvar > anything_id
1333 && lhsvar != rhsvar && lhs.offset == 0 && rhs.offset == 0)
1335 add_graph_edge (graph, lhsvar, rhsvar);
1339 /* Add edges from STOREDANYTHING to all non-direct nodes that can
1340 receive pointers. */
1341 t = find (storedanything_id);
1342 for (i = integer_id + 1; i < FIRST_REF_NODE; ++i)
1344 if (!TEST_BIT (graph->direct_nodes, i)
1345 && get_varinfo (i)->may_have_pointers)
1346 add_graph_edge (graph, find (i), t);
1349 /* Everything stored to ANYTHING also potentially escapes. */
1350 add_graph_edge (graph, find (escaped_id), t);
1354 /* Changed variables on the last iteration. */
1355 static bitmap changed;
1357 /* Strongly Connected Component visitation info. */
1364 unsigned int *node_mapping;
1366 VEC(unsigned,heap) *scc_stack;
1370 /* Recursive routine to find strongly connected components in GRAPH.
1371 SI is the SCC info to store the information in, and N is the id of current
1372 graph node we are processing.
1374 This is Tarjan's strongly connected component finding algorithm, as
1375 modified by Nuutila to keep only non-root nodes on the stack.
1376 The algorithm can be found in "On finding the strongly connected
1377 connected components in a directed graph" by Esko Nuutila and Eljas
1378 Soisalon-Soininen, in Information Processing Letters volume 49,
1379 number 1, pages 9-14. */
1382 scc_visit (constraint_graph_t graph, struct scc_info *si, unsigned int n)
1386 unsigned int my_dfs;
1388 SET_BIT (si->visited, n);
1389 si->dfs[n] = si->current_index ++;
1390 my_dfs = si->dfs[n];
1392 /* Visit all the successors. */
1393 EXECUTE_IF_IN_NONNULL_BITMAP (graph->succs[n], 0, i, bi)
1397 if (i > LAST_REF_NODE)
1401 if (TEST_BIT (si->deleted, w))
1404 if (!TEST_BIT (si->visited, w))
1405 scc_visit (graph, si, w);
1407 unsigned int t = find (w);
1408 unsigned int nnode = find (n);
1409 gcc_assert (nnode == n);
1411 if (si->dfs[t] < si->dfs[nnode])
1412 si->dfs[n] = si->dfs[t];
1416 /* See if any components have been identified. */
1417 if (si->dfs[n] == my_dfs)
1419 if (VEC_length (unsigned, si->scc_stack) > 0
1420 && si->dfs[VEC_last (unsigned, si->scc_stack)] >= my_dfs)
1422 bitmap scc = BITMAP_ALLOC (NULL);
1423 unsigned int lowest_node;
1426 bitmap_set_bit (scc, n);
1428 while (VEC_length (unsigned, si->scc_stack) != 0
1429 && si->dfs[VEC_last (unsigned, si->scc_stack)] >= my_dfs)
1431 unsigned int w = VEC_pop (unsigned, si->scc_stack);
1433 bitmap_set_bit (scc, w);
1436 lowest_node = bitmap_first_set_bit (scc);
1437 gcc_assert (lowest_node < FIRST_REF_NODE);
1439 /* Collapse the SCC nodes into a single node, and mark the
1441 EXECUTE_IF_SET_IN_BITMAP (scc, 0, i, bi)
1443 if (i < FIRST_REF_NODE)
1445 if (unite (lowest_node, i))
1446 unify_nodes (graph, lowest_node, i, false);
1450 unite (lowest_node, i);
1451 graph->indirect_cycles[i - FIRST_REF_NODE] = lowest_node;
1455 SET_BIT (si->deleted, n);
1458 VEC_safe_push (unsigned, heap, si->scc_stack, n);
1461 /* Unify node FROM into node TO, updating the changed count if
1462 necessary when UPDATE_CHANGED is true. */
1465 unify_nodes (constraint_graph_t graph, unsigned int to, unsigned int from,
1466 bool update_changed)
1469 gcc_assert (to != from && find (to) == to);
1470 if (dump_file && (dump_flags & TDF_DETAILS))
1471 fprintf (dump_file, "Unifying %s to %s\n",
1472 get_varinfo (from)->name,
1473 get_varinfo (to)->name);
1476 stats.unified_vars_dynamic++;
1478 stats.unified_vars_static++;
1480 merge_graph_nodes (graph, to, from);
1481 merge_node_constraints (graph, to, from);
1483 /* Mark TO as changed if FROM was changed. If TO was already marked
1484 as changed, decrease the changed count. */
1487 && bitmap_bit_p (changed, from))
1489 bitmap_clear_bit (changed, from);
1490 bitmap_set_bit (changed, to);
1492 if (get_varinfo (from)->solution)
1494 /* If the solution changes because of the merging, we need to mark
1495 the variable as changed. */
1496 if (bitmap_ior_into (get_varinfo (to)->solution,
1497 get_varinfo (from)->solution))
1500 bitmap_set_bit (changed, to);
1503 BITMAP_FREE (get_varinfo (from)->solution);
1504 if (get_varinfo (from)->oldsolution)
1505 BITMAP_FREE (get_varinfo (from)->oldsolution);
1507 if (stats.iterations > 0
1508 && get_varinfo (to)->oldsolution)
1509 BITMAP_FREE (get_varinfo (to)->oldsolution);
1511 if (valid_graph_edge (graph, to, to))
1513 if (graph->succs[to])
1514 bitmap_clear_bit (graph->succs[to], to);
1518 /* Information needed to compute the topological ordering of a graph. */
1522 /* sbitmap of visited nodes. */
1524 /* Array that stores the topological order of the graph, *in
1526 VEC(unsigned,heap) *topo_order;
1530 /* Initialize and return a topological info structure. */
1532 static struct topo_info *
1533 init_topo_info (void)
1535 size_t size = graph->size;
1536 struct topo_info *ti = XNEW (struct topo_info);
1537 ti->visited = sbitmap_alloc (size);
1538 sbitmap_zero (ti->visited);
1539 ti->topo_order = VEC_alloc (unsigned, heap, 1);
1544 /* Free the topological sort info pointed to by TI. */
1547 free_topo_info (struct topo_info *ti)
1549 sbitmap_free (ti->visited);
1550 VEC_free (unsigned, heap, ti->topo_order);
1554 /* Visit the graph in topological order, and store the order in the
1555 topo_info structure. */
1558 topo_visit (constraint_graph_t graph, struct topo_info *ti,
1564 SET_BIT (ti->visited, n);
1566 if (graph->succs[n])
1567 EXECUTE_IF_SET_IN_BITMAP (graph->succs[n], 0, j, bi)
1569 if (!TEST_BIT (ti->visited, j))
1570 topo_visit (graph, ti, j);
1573 VEC_safe_push (unsigned, heap, ti->topo_order, n);
1576 /* Process a constraint C that represents x = *(y + off), using DELTA as the
1577 starting solution for y. */
1580 do_sd_constraint (constraint_graph_t graph, constraint_t c,
1583 unsigned int lhs = c->lhs.var;
1585 bitmap sol = get_varinfo (lhs)->solution;
1588 HOST_WIDE_INT roffset = c->rhs.offset;
1590 /* Our IL does not allow this. */
1591 gcc_assert (c->lhs.offset == 0);
1593 /* If the solution of Y contains anything it is good enough to transfer
1595 if (bitmap_bit_p (delta, anything_id))
1597 flag |= bitmap_set_bit (sol, anything_id);
1601 /* If we do not know at with offset the rhs is dereferenced compute
1602 the reachability set of DELTA, conservatively assuming it is
1603 dereferenced at all valid offsets. */
1604 if (roffset == UNKNOWN_OFFSET)
1606 solution_set_expand (delta, delta);
1607 /* No further offset processing is necessary. */
1611 /* For each variable j in delta (Sol(y)), add
1612 an edge in the graph from j to x, and union Sol(j) into Sol(x). */
1613 EXECUTE_IF_SET_IN_BITMAP (delta, 0, j, bi)
1615 varinfo_t v = get_varinfo (j);
1616 HOST_WIDE_INT fieldoffset = v->offset + roffset;
1620 fieldoffset = v->offset;
1621 else if (roffset != 0)
1622 v = first_vi_for_offset (v, fieldoffset);
1623 /* If the access is outside of the variable we can ignore it. */
1631 /* Adding edges from the special vars is pointless.
1632 They don't have sets that can change. */
1633 if (get_varinfo (t)->is_special_var)
1634 flag |= bitmap_ior_into (sol, get_varinfo (t)->solution);
1635 /* Merging the solution from ESCAPED needlessly increases
1636 the set. Use ESCAPED as representative instead. */
1637 else if (v->id == escaped_id)
1638 flag |= bitmap_set_bit (sol, escaped_id);
1639 else if (v->may_have_pointers
1640 && add_graph_edge (graph, lhs, t))
1641 flag |= bitmap_ior_into (sol, get_varinfo (t)->solution);
1643 /* If the variable is not exactly at the requested offset
1644 we have to include the next one. */
1645 if (v->offset == (unsigned HOST_WIDE_INT)fieldoffset
1650 fieldoffset = v->offset;
1656 /* If the LHS solution changed, mark the var as changed. */
1659 get_varinfo (lhs)->solution = sol;
1660 bitmap_set_bit (changed, lhs);
1664 /* Process a constraint C that represents *(x + off) = y using DELTA
1665 as the starting solution for x. */
1668 do_ds_constraint (constraint_t c, bitmap delta)
1670 unsigned int rhs = c->rhs.var;
1671 bitmap sol = get_varinfo (rhs)->solution;
1674 HOST_WIDE_INT loff = c->lhs.offset;
1675 bool escaped_p = false;
1677 /* Our IL does not allow this. */
1678 gcc_assert (c->rhs.offset == 0);
1680 /* If the solution of y contains ANYTHING simply use the ANYTHING
1681 solution. This avoids needlessly increasing the points-to sets. */
1682 if (bitmap_bit_p (sol, anything_id))
1683 sol = get_varinfo (find (anything_id))->solution;
1685 /* If the solution for x contains ANYTHING we have to merge the
1686 solution of y into all pointer variables which we do via
1688 if (bitmap_bit_p (delta, anything_id))
1690 unsigned t = find (storedanything_id);
1691 if (add_graph_edge (graph, t, rhs))
1693 if (bitmap_ior_into (get_varinfo (t)->solution, sol))
1694 bitmap_set_bit (changed, t);
1699 /* If we do not know at with offset the rhs is dereferenced compute
1700 the reachability set of DELTA, conservatively assuming it is
1701 dereferenced at all valid offsets. */
1702 if (loff == UNKNOWN_OFFSET)
1704 solution_set_expand (delta, delta);
1708 /* For each member j of delta (Sol(x)), add an edge from y to j and
1709 union Sol(y) into Sol(j) */
1710 EXECUTE_IF_SET_IN_BITMAP (delta, 0, j, bi)
1712 varinfo_t v = get_varinfo (j);
1714 HOST_WIDE_INT fieldoffset = v->offset + loff;
1717 fieldoffset = v->offset;
1719 v = first_vi_for_offset (v, fieldoffset);
1720 /* If the access is outside of the variable we can ignore it. */
1726 if (v->may_have_pointers)
1728 /* If v is a global variable then this is an escape point. */
1729 if (v->is_global_var
1732 t = find (escaped_id);
1733 if (add_graph_edge (graph, t, rhs)
1734 && bitmap_ior_into (get_varinfo (t)->solution, sol))
1735 bitmap_set_bit (changed, t);
1736 /* Enough to let rhs escape once. */
1740 if (v->is_special_var)
1744 if (add_graph_edge (graph, t, rhs)
1745 && bitmap_ior_into (get_varinfo (t)->solution, sol))
1746 bitmap_set_bit (changed, t);
1749 /* If the variable is not exactly at the requested offset
1750 we have to include the next one. */
1751 if (v->offset == (unsigned HOST_WIDE_INT)fieldoffset
1756 fieldoffset = v->offset;
1762 /* Handle a non-simple (simple meaning requires no iteration),
1763 constraint (IE *x = &y, x = *y, *x = y, and x = y with offsets involved). */
1766 do_complex_constraint (constraint_graph_t graph, constraint_t c, bitmap delta)
1768 if (c->lhs.type == DEREF)
1770 if (c->rhs.type == ADDRESSOF)
1777 do_ds_constraint (c, delta);
1780 else if (c->rhs.type == DEREF)
1783 if (!(get_varinfo (c->lhs.var)->is_special_var))
1784 do_sd_constraint (graph, c, delta);
1792 gcc_assert (c->rhs.type == SCALAR && c->lhs.type == SCALAR);
1793 solution = get_varinfo (c->rhs.var)->solution;
1794 tmp = get_varinfo (c->lhs.var)->solution;
1796 flag = set_union_with_increment (tmp, solution, c->rhs.offset);
1800 get_varinfo (c->lhs.var)->solution = tmp;
1801 bitmap_set_bit (changed, c->lhs.var);
1806 /* Initialize and return a new SCC info structure. */
1808 static struct scc_info *
1809 init_scc_info (size_t size)
1811 struct scc_info *si = XNEW (struct scc_info);
1814 si->current_index = 0;
1815 si->visited = sbitmap_alloc (size);
1816 sbitmap_zero (si->visited);
1817 si->deleted = sbitmap_alloc (size);
1818 sbitmap_zero (si->deleted);
1819 si->node_mapping = XNEWVEC (unsigned int, size);
1820 si->dfs = XCNEWVEC (unsigned int, size);
1822 for (i = 0; i < size; i++)
1823 si->node_mapping[i] = i;
1825 si->scc_stack = VEC_alloc (unsigned, heap, 1);
1829 /* Free an SCC info structure pointed to by SI */
1832 free_scc_info (struct scc_info *si)
1834 sbitmap_free (si->visited);
1835 sbitmap_free (si->deleted);
1836 free (si->node_mapping);
1838 VEC_free (unsigned, heap, si->scc_stack);
1843 /* Find indirect cycles in GRAPH that occur, using strongly connected
1844 components, and note them in the indirect cycles map.
1846 This technique comes from Ben Hardekopf and Calvin Lin,
1847 "It Pays to be Lazy: Fast and Accurate Pointer Analysis for Millions of
1848 Lines of Code", submitted to PLDI 2007. */
1851 find_indirect_cycles (constraint_graph_t graph)
1854 unsigned int size = graph->size;
1855 struct scc_info *si = init_scc_info (size);
1857 for (i = 0; i < MIN (LAST_REF_NODE, size); i ++ )
1858 if (!TEST_BIT (si->visited, i) && find (i) == i)
1859 scc_visit (graph, si, i);
1864 /* Compute a topological ordering for GRAPH, and store the result in the
1865 topo_info structure TI. */
1868 compute_topo_order (constraint_graph_t graph,
1869 struct topo_info *ti)
1872 unsigned int size = graph->size;
1874 for (i = 0; i != size; ++i)
1875 if (!TEST_BIT (ti->visited, i) && find (i) == i)
1876 topo_visit (graph, ti, i);
1879 /* Structure used to for hash value numbering of pointer equivalence
1882 typedef struct equiv_class_label
1885 unsigned int equivalence_class;
1887 } *equiv_class_label_t;
1888 typedef const struct equiv_class_label *const_equiv_class_label_t;
1890 /* A hashtable for mapping a bitmap of labels->pointer equivalence
1892 static htab_t pointer_equiv_class_table;
1894 /* A hashtable for mapping a bitmap of labels->location equivalence
1896 static htab_t location_equiv_class_table;
1898 /* Hash function for a equiv_class_label_t */
1901 equiv_class_label_hash (const void *p)
1903 const_equiv_class_label_t const ecl = (const_equiv_class_label_t) p;
1904 return ecl->hashcode;
1907 /* Equality function for two equiv_class_label_t's. */
1910 equiv_class_label_eq (const void *p1, const void *p2)
1912 const_equiv_class_label_t const eql1 = (const_equiv_class_label_t) p1;
1913 const_equiv_class_label_t const eql2 = (const_equiv_class_label_t) p2;
1914 return (eql1->hashcode == eql2->hashcode
1915 && bitmap_equal_p (eql1->labels, eql2->labels));
1918 /* Lookup a equivalence class in TABLE by the bitmap of LABELS it
1922 equiv_class_lookup (htab_t table, bitmap labels)
1925 struct equiv_class_label ecl;
1927 ecl.labels = labels;
1928 ecl.hashcode = bitmap_hash (labels);
1930 slot = htab_find_slot_with_hash (table, &ecl,
1931 ecl.hashcode, NO_INSERT);
1935 return ((equiv_class_label_t) *slot)->equivalence_class;
1939 /* Add an equivalence class named EQUIVALENCE_CLASS with labels LABELS
1943 equiv_class_add (htab_t table, unsigned int equivalence_class,
1947 equiv_class_label_t ecl = XNEW (struct equiv_class_label);
1949 ecl->labels = labels;
1950 ecl->equivalence_class = equivalence_class;
1951 ecl->hashcode = bitmap_hash (labels);
1953 slot = htab_find_slot_with_hash (table, ecl,
1954 ecl->hashcode, INSERT);
1955 gcc_assert (!*slot);
1956 *slot = (void *) ecl;
1959 /* Perform offline variable substitution.
1961 This is a worst case quadratic time way of identifying variables
1962 that must have equivalent points-to sets, including those caused by
1963 static cycles, and single entry subgraphs, in the constraint graph.
1965 The technique is described in "Exploiting Pointer and Location
1966 Equivalence to Optimize Pointer Analysis. In the 14th International
1967 Static Analysis Symposium (SAS), August 2007." It is known as the
1968 "HU" algorithm, and is equivalent to value numbering the collapsed
1969 constraint graph including evaluating unions.
1971 The general method of finding equivalence classes is as follows:
1972 Add fake nodes (REF nodes) and edges for *a = b and a = *b constraints.
1973 Initialize all non-REF nodes to be direct nodes.
1974 For each constraint a = a U {b}, we set pts(a) = pts(a) u {fresh
1976 For each constraint containing the dereference, we also do the same
1979 We then compute SCC's in the graph and unify nodes in the same SCC,
1982 For each non-collapsed node x:
1983 Visit all unvisited explicit incoming edges.
1984 Ignoring all non-pointers, set pts(x) = Union of pts(a) for y
1986 Lookup the equivalence class for pts(x).
1987 If we found one, equivalence_class(x) = found class.
1988 Otherwise, equivalence_class(x) = new class, and new_class is
1989 added to the lookup table.
1991 All direct nodes with the same equivalence class can be replaced
1992 with a single representative node.
1993 All unlabeled nodes (label == 0) are not pointers and all edges
1994 involving them can be eliminated.
1995 We perform these optimizations during rewrite_constraints
1997 In addition to pointer equivalence class finding, we also perform
1998 location equivalence class finding. This is the set of variables
1999 that always appear together in points-to sets. We use this to
2000 compress the size of the points-to sets. */
2002 /* Current maximum pointer equivalence class id. */
2003 static int pointer_equiv_class;
2005 /* Current maximum location equivalence class id. */
2006 static int location_equiv_class;
2008 /* Recursive routine to find strongly connected components in GRAPH,
2009 and label it's nodes with DFS numbers. */
2012 condense_visit (constraint_graph_t graph, struct scc_info *si, unsigned int n)
2016 unsigned int my_dfs;
2018 gcc_assert (si->node_mapping[n] == n);
2019 SET_BIT (si->visited, n);
2020 si->dfs[n] = si->current_index ++;
2021 my_dfs = si->dfs[n];
2023 /* Visit all the successors. */
2024 EXECUTE_IF_IN_NONNULL_BITMAP (graph->preds[n], 0, i, bi)
2026 unsigned int w = si->node_mapping[i];
2028 if (TEST_BIT (si->deleted, w))
2031 if (!TEST_BIT (si->visited, w))
2032 condense_visit (graph, si, w);
2034 unsigned int t = si->node_mapping[w];
2035 unsigned int nnode = si->node_mapping[n];
2036 gcc_assert (nnode == n);
2038 if (si->dfs[t] < si->dfs[nnode])
2039 si->dfs[n] = si->dfs[t];
2043 /* Visit all the implicit predecessors. */
2044 EXECUTE_IF_IN_NONNULL_BITMAP (graph->implicit_preds[n], 0, i, bi)
2046 unsigned int w = si->node_mapping[i];
2048 if (TEST_BIT (si->deleted, w))
2051 if (!TEST_BIT (si->visited, w))
2052 condense_visit (graph, si, w);
2054 unsigned int t = si->node_mapping[w];
2055 unsigned int nnode = si->node_mapping[n];
2056 gcc_assert (nnode == n);
2058 if (si->dfs[t] < si->dfs[nnode])
2059 si->dfs[n] = si->dfs[t];
2063 /* See if any components have been identified. */
2064 if (si->dfs[n] == my_dfs)
2066 while (VEC_length (unsigned, si->scc_stack) != 0
2067 && si->dfs[VEC_last (unsigned, si->scc_stack)] >= my_dfs)
2069 unsigned int w = VEC_pop (unsigned, si->scc_stack);
2070 si->node_mapping[w] = n;
2072 if (!TEST_BIT (graph->direct_nodes, w))
2073 RESET_BIT (graph->direct_nodes, n);
2075 /* Unify our nodes. */
2076 if (graph->preds[w])
2078 if (!graph->preds[n])
2079 graph->preds[n] = BITMAP_ALLOC (&predbitmap_obstack);
2080 bitmap_ior_into (graph->preds[n], graph->preds[w]);
2082 if (graph->implicit_preds[w])
2084 if (!graph->implicit_preds[n])
2085 graph->implicit_preds[n] = BITMAP_ALLOC (&predbitmap_obstack);
2086 bitmap_ior_into (graph->implicit_preds[n],
2087 graph->implicit_preds[w]);
2089 if (graph->points_to[w])
2091 if (!graph->points_to[n])
2092 graph->points_to[n] = BITMAP_ALLOC (&predbitmap_obstack);
2093 bitmap_ior_into (graph->points_to[n],
2094 graph->points_to[w]);
2097 SET_BIT (si->deleted, n);
2100 VEC_safe_push (unsigned, heap, si->scc_stack, n);
2103 /* Label pointer equivalences. */
2106 label_visit (constraint_graph_t graph, struct scc_info *si, unsigned int n)
2110 SET_BIT (si->visited, n);
2112 if (!graph->points_to[n])
2113 graph->points_to[n] = BITMAP_ALLOC (&predbitmap_obstack);
2115 /* Label and union our incoming edges's points to sets. */
2116 EXECUTE_IF_IN_NONNULL_BITMAP (graph->preds[n], 0, i, bi)
2118 unsigned int w = si->node_mapping[i];
2119 if (!TEST_BIT (si->visited, w))
2120 label_visit (graph, si, w);
2122 /* Skip unused edges */
2123 if (w == n || graph->pointer_label[w] == 0)
2126 if (graph->points_to[w])
2127 bitmap_ior_into(graph->points_to[n], graph->points_to[w]);
2129 /* Indirect nodes get fresh variables. */
2130 if (!TEST_BIT (graph->direct_nodes, n))
2131 bitmap_set_bit (graph->points_to[n], FIRST_REF_NODE + n);
2133 if (!bitmap_empty_p (graph->points_to[n]))
2135 unsigned int label = equiv_class_lookup (pointer_equiv_class_table,
2136 graph->points_to[n]);
2139 label = pointer_equiv_class++;
2140 equiv_class_add (pointer_equiv_class_table,
2141 label, graph->points_to[n]);
2143 graph->pointer_label[n] = label;
2147 /* Perform offline variable substitution, discovering equivalence
2148 classes, and eliminating non-pointer variables. */
2150 static struct scc_info *
2151 perform_var_substitution (constraint_graph_t graph)
2154 unsigned int size = graph->size;
2155 struct scc_info *si = init_scc_info (size);
2157 bitmap_obstack_initialize (&iteration_obstack);
2158 pointer_equiv_class_table = htab_create (511, equiv_class_label_hash,
2159 equiv_class_label_eq, free);
2160 location_equiv_class_table = htab_create (511, equiv_class_label_hash,
2161 equiv_class_label_eq, free);
2162 pointer_equiv_class = 1;
2163 location_equiv_class = 1;
2165 /* Condense the nodes, which means to find SCC's, count incoming
2166 predecessors, and unite nodes in SCC's. */
2167 for (i = 0; i < FIRST_REF_NODE; i++)
2168 if (!TEST_BIT (si->visited, si->node_mapping[i]))
2169 condense_visit (graph, si, si->node_mapping[i]);
2171 sbitmap_zero (si->visited);
2172 /* Actually the label the nodes for pointer equivalences */
2173 for (i = 0; i < FIRST_REF_NODE; i++)
2174 if (!TEST_BIT (si->visited, si->node_mapping[i]))
2175 label_visit (graph, si, si->node_mapping[i]);
2177 /* Calculate location equivalence labels. */
2178 for (i = 0; i < FIRST_REF_NODE; i++)
2185 if (!graph->pointed_by[i])
2187 pointed_by = BITMAP_ALLOC (&iteration_obstack);
2189 /* Translate the pointed-by mapping for pointer equivalence
2191 EXECUTE_IF_SET_IN_BITMAP (graph->pointed_by[i], 0, j, bi)
2193 bitmap_set_bit (pointed_by,
2194 graph->pointer_label[si->node_mapping[j]]);
2196 /* The original pointed_by is now dead. */
2197 BITMAP_FREE (graph->pointed_by[i]);
2199 /* Look up the location equivalence label if one exists, or make
2201 label = equiv_class_lookup (location_equiv_class_table,
2205 label = location_equiv_class++;
2206 equiv_class_add (location_equiv_class_table,
2211 if (dump_file && (dump_flags & TDF_DETAILS))
2212 fprintf (dump_file, "Found location equivalence for node %s\n",
2213 get_varinfo (i)->name);
2214 BITMAP_FREE (pointed_by);
2216 graph->loc_label[i] = label;
2220 if (dump_file && (dump_flags & TDF_DETAILS))
2221 for (i = 0; i < FIRST_REF_NODE; i++)
2223 bool direct_node = TEST_BIT (graph->direct_nodes, i);
2225 "Equivalence classes for %s node id %d:%s are pointer: %d"
2227 direct_node ? "Direct node" : "Indirect node", i,
2228 get_varinfo (i)->name,
2229 graph->pointer_label[si->node_mapping[i]],
2230 graph->loc_label[si->node_mapping[i]]);
2233 /* Quickly eliminate our non-pointer variables. */
2235 for (i = 0; i < FIRST_REF_NODE; i++)
2237 unsigned int node = si->node_mapping[i];
2239 if (graph->pointer_label[node] == 0)
2241 if (dump_file && (dump_flags & TDF_DETAILS))
2243 "%s is a non-pointer variable, eliminating edges.\n",
2244 get_varinfo (node)->name);
2245 stats.nonpointer_vars++;
2246 clear_edges_for_node (graph, node);
2253 /* Free information that was only necessary for variable
2257 free_var_substitution_info (struct scc_info *si)
2260 free (graph->pointer_label);
2261 free (graph->loc_label);
2262 free (graph->pointed_by);
2263 free (graph->points_to);
2264 free (graph->eq_rep);
2265 sbitmap_free (graph->direct_nodes);
2266 htab_delete (pointer_equiv_class_table);
2267 htab_delete (location_equiv_class_table);
2268 bitmap_obstack_release (&iteration_obstack);
2271 /* Return an existing node that is equivalent to NODE, which has
2272 equivalence class LABEL, if one exists. Return NODE otherwise. */
2275 find_equivalent_node (constraint_graph_t graph,
2276 unsigned int node, unsigned int label)
2278 /* If the address version of this variable is unused, we can
2279 substitute it for anything else with the same label.
2280 Otherwise, we know the pointers are equivalent, but not the
2281 locations, and we can unite them later. */
2283 if (!bitmap_bit_p (graph->address_taken, node))
2285 gcc_assert (label < graph->size);
2287 if (graph->eq_rep[label] != -1)
2289 /* Unify the two variables since we know they are equivalent. */
2290 if (unite (graph->eq_rep[label], node))
2291 unify_nodes (graph, graph->eq_rep[label], node, false);
2292 return graph->eq_rep[label];
2296 graph->eq_rep[label] = node;
2297 graph->pe_rep[label] = node;
2302 gcc_assert (label < graph->size);
2303 graph->pe[node] = label;
2304 if (graph->pe_rep[label] == -1)
2305 graph->pe_rep[label] = node;
2311 /* Unite pointer equivalent but not location equivalent nodes in
2312 GRAPH. This may only be performed once variable substitution is
2316 unite_pointer_equivalences (constraint_graph_t graph)
2320 /* Go through the pointer equivalences and unite them to their
2321 representative, if they aren't already. */
2322 for (i = 0; i < FIRST_REF_NODE; i++)
2324 unsigned int label = graph->pe[i];
2327 int label_rep = graph->pe_rep[label];
2329 if (label_rep == -1)
2332 label_rep = find (label_rep);
2333 if (label_rep >= 0 && unite (label_rep, find (i)))
2334 unify_nodes (graph, label_rep, i, false);
2339 /* Move complex constraints to the GRAPH nodes they belong to. */
2342 move_complex_constraints (constraint_graph_t graph)
2347 FOR_EACH_VEC_ELT (constraint_t, constraints, i, c)
2351 struct constraint_expr lhs = c->lhs;
2352 struct constraint_expr rhs = c->rhs;
2354 if (lhs.type == DEREF)
2356 insert_into_complex (graph, lhs.var, c);
2358 else if (rhs.type == DEREF)
2360 if (!(get_varinfo (lhs.var)->is_special_var))
2361 insert_into_complex (graph, rhs.var, c);
2363 else if (rhs.type != ADDRESSOF && lhs.var > anything_id
2364 && (lhs.offset != 0 || rhs.offset != 0))
2366 insert_into_complex (graph, rhs.var, c);
2373 /* Optimize and rewrite complex constraints while performing
2374 collapsing of equivalent nodes. SI is the SCC_INFO that is the
2375 result of perform_variable_substitution. */
2378 rewrite_constraints (constraint_graph_t graph,
2379 struct scc_info *si)
2385 for (j = 0; j < graph->size; j++)
2386 gcc_assert (find (j) == j);
2388 FOR_EACH_VEC_ELT (constraint_t, constraints, i, c)
2390 struct constraint_expr lhs = c->lhs;
2391 struct constraint_expr rhs = c->rhs;
2392 unsigned int lhsvar = find (lhs.var);
2393 unsigned int rhsvar = find (rhs.var);
2394 unsigned int lhsnode, rhsnode;
2395 unsigned int lhslabel, rhslabel;
2397 lhsnode = si->node_mapping[lhsvar];
2398 rhsnode = si->node_mapping[rhsvar];
2399 lhslabel = graph->pointer_label[lhsnode];
2400 rhslabel = graph->pointer_label[rhsnode];
2402 /* See if it is really a non-pointer variable, and if so, ignore
2406 if (dump_file && (dump_flags & TDF_DETAILS))
2409 fprintf (dump_file, "%s is a non-pointer variable,"
2410 "ignoring constraint:",
2411 get_varinfo (lhs.var)->name);
2412 dump_constraint (dump_file, c);
2413 fprintf (dump_file, "\n");
2415 VEC_replace (constraint_t, constraints, i, NULL);
2421 if (dump_file && (dump_flags & TDF_DETAILS))
2424 fprintf (dump_file, "%s is a non-pointer variable,"
2425 "ignoring constraint:",
2426 get_varinfo (rhs.var)->name);
2427 dump_constraint (dump_file, c);
2428 fprintf (dump_file, "\n");
2430 VEC_replace (constraint_t, constraints, i, NULL);
2434 lhsvar = find_equivalent_node (graph, lhsvar, lhslabel);
2435 rhsvar = find_equivalent_node (graph, rhsvar, rhslabel);
2436 c->lhs.var = lhsvar;
2437 c->rhs.var = rhsvar;
2442 /* Eliminate indirect cycles involving NODE. Return true if NODE was
2443 part of an SCC, false otherwise. */
2446 eliminate_indirect_cycles (unsigned int node)
2448 if (graph->indirect_cycles[node] != -1
2449 && !bitmap_empty_p (get_varinfo (node)->solution))
2452 VEC(unsigned,heap) *queue = NULL;
2454 unsigned int to = find (graph->indirect_cycles[node]);
2457 /* We can't touch the solution set and call unify_nodes
2458 at the same time, because unify_nodes is going to do
2459 bitmap unions into it. */
2461 EXECUTE_IF_SET_IN_BITMAP (get_varinfo (node)->solution, 0, i, bi)
2463 if (find (i) == i && i != to)
2466 VEC_safe_push (unsigned, heap, queue, i);
2471 VEC_iterate (unsigned, queue, queuepos, i);
2474 unify_nodes (graph, to, i, true);
2476 VEC_free (unsigned, heap, queue);
2482 /* Solve the constraint graph GRAPH using our worklist solver.
2483 This is based on the PW* family of solvers from the "Efficient Field
2484 Sensitive Pointer Analysis for C" paper.
2485 It works by iterating over all the graph nodes, processing the complex
2486 constraints and propagating the copy constraints, until everything stops
2487 changed. This corresponds to steps 6-8 in the solving list given above. */
2490 solve_graph (constraint_graph_t graph)
2492 unsigned int size = graph->size;
2496 changed = BITMAP_ALLOC (NULL);
2498 /* Mark all initial non-collapsed nodes as changed. */
2499 for (i = 0; i < size; i++)
2501 varinfo_t ivi = get_varinfo (i);
2502 if (find (i) == i && !bitmap_empty_p (ivi->solution)
2503 && ((graph->succs[i] && !bitmap_empty_p (graph->succs[i]))
2504 || VEC_length (constraint_t, graph->complex[i]) > 0))
2505 bitmap_set_bit (changed, i);
2508 /* Allocate a bitmap to be used to store the changed bits. */
2509 pts = BITMAP_ALLOC (&pta_obstack);
2511 while (!bitmap_empty_p (changed))
2514 struct topo_info *ti = init_topo_info ();
2517 bitmap_obstack_initialize (&iteration_obstack);
2519 compute_topo_order (graph, ti);
2521 while (VEC_length (unsigned, ti->topo_order) != 0)
2524 i = VEC_pop (unsigned, ti->topo_order);
2526 /* If this variable is not a representative, skip it. */
2530 /* In certain indirect cycle cases, we may merge this
2531 variable to another. */
2532 if (eliminate_indirect_cycles (i) && find (i) != i)
2535 /* If the node has changed, we need to process the
2536 complex constraints and outgoing edges again. */
2537 if (bitmap_clear_bit (changed, i))
2542 VEC(constraint_t,heap) *complex = graph->complex[i];
2543 varinfo_t vi = get_varinfo (i);
2544 bool solution_empty;
2546 /* Compute the changed set of solution bits. */
2547 if (vi->oldsolution)
2548 bitmap_and_compl (pts, vi->solution, vi->oldsolution);
2550 bitmap_copy (pts, vi->solution);
2552 if (bitmap_empty_p (pts))
2555 if (vi->oldsolution)
2556 bitmap_ior_into (vi->oldsolution, pts);
2559 vi->oldsolution = BITMAP_ALLOC (&oldpta_obstack);
2560 bitmap_copy (vi->oldsolution, pts);
2563 solution = vi->solution;
2564 solution_empty = bitmap_empty_p (solution);
2566 /* Process the complex constraints */
2567 FOR_EACH_VEC_ELT (constraint_t, complex, j, c)
2569 /* XXX: This is going to unsort the constraints in
2570 some cases, which will occasionally add duplicate
2571 constraints during unification. This does not
2572 affect correctness. */
2573 c->lhs.var = find (c->lhs.var);
2574 c->rhs.var = find (c->rhs.var);
2576 /* The only complex constraint that can change our
2577 solution to non-empty, given an empty solution,
2578 is a constraint where the lhs side is receiving
2579 some set from elsewhere. */
2580 if (!solution_empty || c->lhs.type != DEREF)
2581 do_complex_constraint (graph, c, pts);
2584 solution_empty = bitmap_empty_p (solution);
2586 if (!solution_empty)
2589 unsigned eff_escaped_id = find (escaped_id);
2591 /* Propagate solution to all successors. */
2592 EXECUTE_IF_IN_NONNULL_BITMAP (graph->succs[i],
2598 unsigned int to = find (j);
2599 tmp = get_varinfo (to)->solution;
2602 /* Don't try to propagate to ourselves. */
2606 /* If we propagate from ESCAPED use ESCAPED as
2608 if (i == eff_escaped_id)
2609 flag = bitmap_set_bit (tmp, escaped_id);
2611 flag = set_union_with_increment (tmp, pts, 0);
2615 get_varinfo (to)->solution = tmp;
2616 bitmap_set_bit (changed, to);
2622 free_topo_info (ti);
2623 bitmap_obstack_release (&iteration_obstack);
2627 BITMAP_FREE (changed);
2628 bitmap_obstack_release (&oldpta_obstack);
2631 /* Map from trees to variable infos. */
2632 static struct pointer_map_t *vi_for_tree;
2635 /* Insert ID as the variable id for tree T in the vi_for_tree map. */
2638 insert_vi_for_tree (tree t, varinfo_t vi)
2640 void **slot = pointer_map_insert (vi_for_tree, t);
2642 gcc_assert (*slot == NULL);
2646 /* Find the variable info for tree T in VI_FOR_TREE. If T does not
2647 exist in the map, return NULL, otherwise, return the varinfo we found. */
2650 lookup_vi_for_tree (tree t)
2652 void **slot = pointer_map_contains (vi_for_tree, t);
2656 return (varinfo_t) *slot;
2659 /* Return a printable name for DECL */
2662 alias_get_name (tree decl)
2666 int num_printed = 0;
2668 if (DECL_ASSEMBLER_NAME_SET_P (decl))
2669 res = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2671 res= get_name (decl);
2679 if (TREE_CODE (decl) == SSA_NAME)
2681 num_printed = asprintf (&temp, "%s_%u",
2682 alias_get_name (SSA_NAME_VAR (decl)),
2683 SSA_NAME_VERSION (decl));
2685 else if (DECL_P (decl))
2687 num_printed = asprintf (&temp, "D.%u", DECL_UID (decl));
2689 if (num_printed > 0)
2691 res = ggc_strdup (temp);
2697 /* Find the variable id for tree T in the map.
2698 If T doesn't exist in the map, create an entry for it and return it. */
2701 get_vi_for_tree (tree t)
2703 void **slot = pointer_map_contains (vi_for_tree, t);
2705 return get_varinfo (create_variable_info_for (t, alias_get_name (t)));
2707 return (varinfo_t) *slot;
2710 /* Get a scalar constraint expression for a new temporary variable. */
2712 static struct constraint_expr
2713 new_scalar_tmp_constraint_exp (const char *name)
2715 struct constraint_expr tmp;
2718 vi = new_var_info (NULL_TREE, name);
2722 vi->is_full_var = 1;
2731 /* Get a constraint expression vector from an SSA_VAR_P node.
2732 If address_p is true, the result will be taken its address of. */
2735 get_constraint_for_ssa_var (tree t, VEC(ce_s, heap) **results, bool address_p)
2737 struct constraint_expr cexpr;
2740 /* We allow FUNCTION_DECLs here even though it doesn't make much sense. */
2741 gcc_assert (SSA_VAR_P (t) || DECL_P (t));
2743 /* For parameters, get at the points-to set for the actual parm
2745 if (TREE_CODE (t) == SSA_NAME
2746 && (TREE_CODE (SSA_NAME_VAR (t)) == PARM_DECL
2747 || TREE_CODE (SSA_NAME_VAR (t)) == RESULT_DECL)
2748 && SSA_NAME_IS_DEFAULT_DEF (t))
2750 get_constraint_for_ssa_var (SSA_NAME_VAR (t), results, address_p);
2754 /* For global variables resort to the alias target. */
2755 if (TREE_CODE (t) == VAR_DECL
2756 && (TREE_STATIC (t) || DECL_EXTERNAL (t)))
2758 struct varpool_node *node = varpool_get_node (t);
2759 if (node && node->alias)
2761 node = varpool_variable_node (node, NULL);
2766 vi = get_vi_for_tree (t);
2768 cexpr.type = SCALAR;
2770 /* If we determine the result is "anything", and we know this is readonly,
2771 say it points to readonly memory instead. */
2772 if (cexpr.var == anything_id && TREE_READONLY (t))
2775 cexpr.type = ADDRESSOF;
2776 cexpr.var = readonly_id;
2779 /* If we are not taking the address of the constraint expr, add all
2780 sub-fiels of the variable as well. */
2782 && !vi->is_full_var)
2784 for (; vi; vi = vi->next)
2787 VEC_safe_push (ce_s, heap, *results, &cexpr);
2792 VEC_safe_push (ce_s, heap, *results, &cexpr);
2795 /* Process constraint T, performing various simplifications and then
2796 adding it to our list of overall constraints. */
2799 process_constraint (constraint_t t)
2801 struct constraint_expr rhs = t->rhs;
2802 struct constraint_expr lhs = t->lhs;
2804 gcc_assert (rhs.var < VEC_length (varinfo_t, varmap));
2805 gcc_assert (lhs.var < VEC_length (varinfo_t, varmap));
2807 /* If we didn't get any useful constraint from the lhs we get
2808 &ANYTHING as fallback from get_constraint_for. Deal with
2809 it here by turning it into *ANYTHING. */
2810 if (lhs.type == ADDRESSOF
2811 && lhs.var == anything_id)
2814 /* ADDRESSOF on the lhs is invalid. */
2815 gcc_assert (lhs.type != ADDRESSOF);
2817 /* We shouldn't add constraints from things that cannot have pointers.
2818 It's not completely trivial to avoid in the callers, so do it here. */
2819 if (rhs.type != ADDRESSOF
2820 && !get_varinfo (rhs.var)->may_have_pointers)
2823 /* Likewise adding to the solution of a non-pointer var isn't useful. */
2824 if (!get_varinfo (lhs.var)->may_have_pointers)
2827 /* This can happen in our IR with things like n->a = *p */
2828 if (rhs.type == DEREF && lhs.type == DEREF && rhs.var != anything_id)
2830 /* Split into tmp = *rhs, *lhs = tmp */
2831 struct constraint_expr tmplhs;
2832 tmplhs = new_scalar_tmp_constraint_exp ("doubledereftmp");
2833 process_constraint (new_constraint (tmplhs, rhs));
2834 process_constraint (new_constraint (lhs, tmplhs));
2836 else if (rhs.type == ADDRESSOF && lhs.type == DEREF)
2838 /* Split into tmp = &rhs, *lhs = tmp */
2839 struct constraint_expr tmplhs;
2840 tmplhs = new_scalar_tmp_constraint_exp ("derefaddrtmp");
2841 process_constraint (new_constraint (tmplhs, rhs));
2842 process_constraint (new_constraint (lhs, tmplhs));
2846 gcc_assert (rhs.type != ADDRESSOF || rhs.offset == 0);
2847 VEC_safe_push (constraint_t, heap, constraints, t);
2852 /* Return the position, in bits, of FIELD_DECL from the beginning of its
2855 static HOST_WIDE_INT
2856 bitpos_of_field (const tree fdecl)
2858 if (!host_integerp (DECL_FIELD_OFFSET (fdecl), 0)
2859 || !host_integerp (DECL_FIELD_BIT_OFFSET (fdecl), 0))
2862 return (TREE_INT_CST_LOW (DECL_FIELD_OFFSET (fdecl)) * BITS_PER_UNIT
2863 + TREE_INT_CST_LOW (DECL_FIELD_BIT_OFFSET (fdecl)));
2867 /* Get constraint expressions for offsetting PTR by OFFSET. Stores the
2868 resulting constraint expressions in *RESULTS. */
2871 get_constraint_for_ptr_offset (tree ptr, tree offset,
2872 VEC (ce_s, heap) **results)
2874 struct constraint_expr c;
2876 HOST_WIDE_INT rhsoffset;
2878 /* If we do not do field-sensitive PTA adding offsets to pointers
2879 does not change the points-to solution. */
2880 if (!use_field_sensitive)
2882 get_constraint_for_rhs (ptr, results);
2886 /* If the offset is not a non-negative integer constant that fits
2887 in a HOST_WIDE_INT, we have to fall back to a conservative
2888 solution which includes all sub-fields of all pointed-to
2889 variables of ptr. */
2890 if (offset == NULL_TREE
2891 || TREE_CODE (offset) != INTEGER_CST)
2892 rhsoffset = UNKNOWN_OFFSET;
2895 /* Sign-extend the offset. */
2897 = double_int_sext (tree_to_double_int (offset),
2898 TYPE_PRECISION (TREE_TYPE (offset)));
2899 if (!double_int_fits_in_shwi_p (soffset))
2900 rhsoffset = UNKNOWN_OFFSET;
2903 /* Make sure the bit-offset also fits. */
2904 HOST_WIDE_INT rhsunitoffset = soffset.low;
2905 rhsoffset = rhsunitoffset * BITS_PER_UNIT;
2906 if (rhsunitoffset != rhsoffset / BITS_PER_UNIT)
2907 rhsoffset = UNKNOWN_OFFSET;
2911 get_constraint_for_rhs (ptr, results);
2915 /* As we are eventually appending to the solution do not use
2916 VEC_iterate here. */
2917 n = VEC_length (ce_s, *results);
2918 for (j = 0; j < n; j++)
2921 c = *VEC_index (ce_s, *results, j);
2922 curr = get_varinfo (c.var);
2924 if (c.type == ADDRESSOF
2925 /* If this varinfo represents a full variable just use it. */
2926 && curr->is_full_var)
2928 else if (c.type == ADDRESSOF
2929 /* If we do not know the offset add all subfields. */
2930 && rhsoffset == UNKNOWN_OFFSET)
2932 varinfo_t temp = lookup_vi_for_tree (curr->decl);
2935 struct constraint_expr c2;
2937 c2.type = ADDRESSOF;
2939 if (c2.var != c.var)
2940 VEC_safe_push (ce_s, heap, *results, &c2);
2945 else if (c.type == ADDRESSOF)
2948 unsigned HOST_WIDE_INT offset = curr->offset + rhsoffset;
2950 /* Search the sub-field which overlaps with the
2951 pointed-to offset. If the result is outside of the variable
2952 we have to provide a conservative result, as the variable is
2953 still reachable from the resulting pointer (even though it
2954 technically cannot point to anything). The last and first
2955 sub-fields are such conservative results.
2956 ??? If we always had a sub-field for &object + 1 then
2957 we could represent this in a more precise way. */
2959 && curr->offset < offset)
2961 temp = first_or_preceding_vi_for_offset (curr, offset);
2963 /* If the found variable is not exactly at the pointed to
2964 result, we have to include the next variable in the
2965 solution as well. Otherwise two increments by offset / 2
2966 do not result in the same or a conservative superset
2968 if (temp->offset != offset
2969 && temp->next != NULL)
2971 struct constraint_expr c2;
2972 c2.var = temp->next->id;
2973 c2.type = ADDRESSOF;
2975 VEC_safe_push (ce_s, heap, *results, &c2);
2981 c.offset = rhsoffset;
2983 VEC_replace (ce_s, *results, j, &c);
2988 /* Given a COMPONENT_REF T, return the constraint_expr vector for it.
2989 If address_p is true the result will be taken its address of.
2990 If lhs_p is true then the constraint expression is assumed to be used
2994 get_constraint_for_component_ref (tree t, VEC(ce_s, heap) **results,
2995 bool address_p, bool lhs_p)
2998 HOST_WIDE_INT bitsize = -1;
2999 HOST_WIDE_INT bitmaxsize = -1;
3000 HOST_WIDE_INT bitpos;
3002 struct constraint_expr *result;
3004 /* Some people like to do cute things like take the address of
3007 while (handled_component_p (forzero)
3008 || INDIRECT_REF_P (forzero)
3009 || TREE_CODE (forzero) == MEM_REF)
3010 forzero = TREE_OPERAND (forzero, 0);
3012 if (CONSTANT_CLASS_P (forzero) && integer_zerop (forzero))
3014 struct constraint_expr temp;
3017 temp.var = integer_id;
3019 VEC_safe_push (ce_s, heap, *results, &temp);
3023 /* Handle type-punning through unions. If we are extracting a pointer
3024 from a union via a possibly type-punning access that pointer
3025 points to anything, similar to a conversion of an integer to
3031 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
3032 u = TREE_OPERAND (u, 0))
3033 if (TREE_CODE (u) == COMPONENT_REF
3034 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
3036 struct constraint_expr temp;
3039 temp.var = anything_id;
3040 temp.type = ADDRESSOF;
3041 VEC_safe_push (ce_s, heap, *results, &temp);
3046 t = get_ref_base_and_extent (t, &bitpos, &bitsize, &bitmaxsize);
3048 /* Pretend to take the address of the base, we'll take care of
3049 adding the required subset of sub-fields below. */
3050 get_constraint_for_1 (t, results, true, lhs_p);
3051 gcc_assert (VEC_length (ce_s, *results) == 1);
3052 result = VEC_last (ce_s, *results);
3054 if (result->type == SCALAR
3055 && get_varinfo (result->var)->is_full_var)
3056 /* For single-field vars do not bother about the offset. */
3058 else if (result->type == SCALAR)
3060 /* In languages like C, you can access one past the end of an
3061 array. You aren't allowed to dereference it, so we can
3062 ignore this constraint. When we handle pointer subtraction,
3063 we may have to do something cute here. */
3065 if ((unsigned HOST_WIDE_INT)bitpos < get_varinfo (result->var)->fullsize
3068 /* It's also not true that the constraint will actually start at the
3069 right offset, it may start in some padding. We only care about
3070 setting the constraint to the first actual field it touches, so
3072 struct constraint_expr cexpr = *result;
3074 VEC_pop (ce_s, *results);
3076 for (curr = get_varinfo (cexpr.var); curr; curr = curr->next)
3078 if (ranges_overlap_p (curr->offset, curr->size,
3079 bitpos, bitmaxsize))
3081 cexpr.var = curr->id;
3082 VEC_safe_push (ce_s, heap, *results, &cexpr);
3087 /* If we are going to take the address of this field then
3088 to be able to compute reachability correctly add at least
3089 the last field of the variable. */
3091 && VEC_length (ce_s, *results) == 0)
3093 curr = get_varinfo (cexpr.var);
3094 while (curr->next != NULL)
3096 cexpr.var = curr->id;
3097 VEC_safe_push (ce_s, heap, *results, &cexpr);
3099 else if (VEC_length (ce_s, *results) == 0)
3100 /* Assert that we found *some* field there. The user couldn't be
3101 accessing *only* padding. */
3102 /* Still the user could access one past the end of an array
3103 embedded in a struct resulting in accessing *only* padding. */
3104 /* Or accessing only padding via type-punning to a type
3105 that has a filed just in padding space. */
3107 cexpr.type = SCALAR;
3108 cexpr.var = anything_id;
3110 VEC_safe_push (ce_s, heap, *results, &cexpr);
3113 else if (bitmaxsize == 0)
3115 if (dump_file && (dump_flags & TDF_DETAILS))
3116 fprintf (dump_file, "Access to zero-sized part of variable,"
3120 if (dump_file && (dump_flags & TDF_DETAILS))
3121 fprintf (dump_file, "Access to past the end of variable, ignoring\n");
3123 else if (result->type == DEREF)
3125 /* If we do not know exactly where the access goes say so. Note
3126 that only for non-structure accesses we know that we access
3127 at most one subfiled of any variable. */
3129 || bitsize != bitmaxsize
3130 || AGGREGATE_TYPE_P (TREE_TYPE (orig_t))
3131 || result->offset == UNKNOWN_OFFSET)
3132 result->offset = UNKNOWN_OFFSET;
3134 result->offset += bitpos;
3136 else if (result->type == ADDRESSOF)
3138 /* We can end up here for component references on a
3139 VIEW_CONVERT_EXPR <>(&foobar). */
3140 result->type = SCALAR;
3141 result->var = anything_id;
3149 /* Dereference the constraint expression CONS, and return the result.
3150 DEREF (ADDRESSOF) = SCALAR
3151 DEREF (SCALAR) = DEREF
3152 DEREF (DEREF) = (temp = DEREF1; result = DEREF(temp))
3153 This is needed so that we can handle dereferencing DEREF constraints. */
3156 do_deref (VEC (ce_s, heap) **constraints)
3158 struct constraint_expr *c;
3161 FOR_EACH_VEC_ELT (ce_s, *constraints, i, c)
3163 if (c->type == SCALAR)
3165 else if (c->type == ADDRESSOF)
3167 else if (c->type == DEREF)
3169 struct constraint_expr tmplhs;
3170 tmplhs = new_scalar_tmp_constraint_exp ("dereftmp");
3171 process_constraint (new_constraint (tmplhs, *c));
3172 c->var = tmplhs.var;
3179 /* Given a tree T, return the constraint expression for taking the
3183 get_constraint_for_address_of (tree t, VEC (ce_s, heap) **results)
3185 struct constraint_expr *c;
3188 get_constraint_for_1 (t, results, true, true);
3190 FOR_EACH_VEC_ELT (ce_s, *results, i, c)
3192 if (c->type == DEREF)
3195 c->type = ADDRESSOF;
3199 /* Given a tree T, return the constraint expression for it. */
3202 get_constraint_for_1 (tree t, VEC (ce_s, heap) **results, bool address_p,
3205 struct constraint_expr temp;
3207 /* x = integer is all glommed to a single variable, which doesn't
3208 point to anything by itself. That is, of course, unless it is an
3209 integer constant being treated as a pointer, in which case, we
3210 will return that this is really the addressof anything. This
3211 happens below, since it will fall into the default case. The only
3212 case we know something about an integer treated like a pointer is
3213 when it is the NULL pointer, and then we just say it points to
3216 Do not do that if -fno-delete-null-pointer-checks though, because
3217 in that case *NULL does not fail, so it _should_ alias *anything.
3218 It is not worth adding a new option or renaming the existing one,
3219 since this case is relatively obscure. */
3220 if ((TREE_CODE (t) == INTEGER_CST
3221 && integer_zerop (t))
3222 /* The only valid CONSTRUCTORs in gimple with pointer typed
3223 elements are zero-initializer. But in IPA mode we also
3224 process global initializers, so verify at least. */
3225 || (TREE_CODE (t) == CONSTRUCTOR
3226 && CONSTRUCTOR_NELTS (t) == 0))
3228 if (flag_delete_null_pointer_checks)
3229 temp.var = nothing_id;
3231 temp.var = nonlocal_id;
3232 temp.type = ADDRESSOF;
3234 VEC_safe_push (ce_s, heap, *results, &temp);
3238 /* String constants are read-only. */
3239 if (TREE_CODE (t) == STRING_CST)
3241 temp.var = readonly_id;
3244 VEC_safe_push (ce_s, heap, *results, &temp);
3248 switch (TREE_CODE_CLASS (TREE_CODE (t)))
3250 case tcc_expression:
3252 switch (TREE_CODE (t))
3255 get_constraint_for_address_of (TREE_OPERAND (t, 0), results);
3263 switch (TREE_CODE (t))
3267 struct constraint_expr cs;
3269 get_constraint_for_ptr_offset (TREE_OPERAND (t, 0),
3270 TREE_OPERAND (t, 1), results);
3273 /* If we are not taking the address then make sure to process
3274 all subvariables we might access. */
3278 cs = *VEC_last (ce_s, *results);
3279 if (cs.type == DEREF
3280 && type_can_have_subvars (TREE_TYPE (t)))
3282 /* For dereferences this means we have to defer it
3284 VEC_last (ce_s, *results)->offset = UNKNOWN_OFFSET;
3287 if (cs.type != SCALAR)
3290 vi = get_varinfo (cs.var);
3292 if (!vi->is_full_var
3295 unsigned HOST_WIDE_INT size;
3296 if (host_integerp (TYPE_SIZE (TREE_TYPE (t)), 1))
3297 size = TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (t)));
3300 for (; curr; curr = curr->next)
3302 if (curr->offset - vi->offset < size)
3305 VEC_safe_push (ce_s, heap, *results, &cs);
3314 case ARRAY_RANGE_REF:
3316 get_constraint_for_component_ref (t, results, address_p, lhs_p);
3318 case VIEW_CONVERT_EXPR:
3319 get_constraint_for_1 (TREE_OPERAND (t, 0), results, address_p,
3322 /* We are missing handling for TARGET_MEM_REF here. */
3327 case tcc_exceptional:
3329 switch (TREE_CODE (t))
3333 get_constraint_for_ssa_var (t, results, address_p);
3340 VEC (ce_s, heap) *tmp = NULL;
3341 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
3343 struct constraint_expr *rhsp;
3345 get_constraint_for_1 (val, &tmp, address_p, lhs_p);
3346 FOR_EACH_VEC_ELT (ce_s, tmp, j, rhsp)
3347 VEC_safe_push (ce_s, heap, *results, rhsp);
3348 VEC_truncate (ce_s, tmp, 0);
3350 VEC_free (ce_s, heap, tmp);
3351 /* We do not know whether the constructor was complete,
3352 so technically we have to add &NOTHING or &ANYTHING
3353 like we do for an empty constructor as well. */
3360 case tcc_declaration:
3362 get_constraint_for_ssa_var (t, results, address_p);
3367 /* We cannot refer to automatic variables through constants. */
3368 temp.type = ADDRESSOF;
3369 temp.var = nonlocal_id;
3371 VEC_safe_push (ce_s, heap, *results, &temp);
3377 /* The default fallback is a constraint from anything. */
3378 temp.type = ADDRESSOF;
3379 temp.var = anything_id;
3381 VEC_safe_push (ce_s, heap, *results, &temp);
3384 /* Given a gimple tree T, return the constraint expression vector for it. */
3387 get_constraint_for (tree t, VEC (ce_s, heap) **results)
3389 gcc_assert (VEC_length (ce_s, *results) == 0);
3391 get_constraint_for_1 (t, results, false, true);
3394 /* Given a gimple tree T, return the constraint expression vector for it
3395 to be used as the rhs of a constraint. */
3398 get_constraint_for_rhs (tree t, VEC (ce_s, heap) **results)
3400 gcc_assert (VEC_length (ce_s, *results) == 0);
3402 get_constraint_for_1 (t, results, false, false);
3406 /* Efficiently generates constraints from all entries in *RHSC to all
3407 entries in *LHSC. */
3410 process_all_all_constraints (VEC (ce_s, heap) *lhsc, VEC (ce_s, heap) *rhsc)
3412 struct constraint_expr *lhsp, *rhsp;
3415 if (VEC_length (ce_s, lhsc) <= 1
3416 || VEC_length (ce_s, rhsc) <= 1)
3418 FOR_EACH_VEC_ELT (ce_s, lhsc, i, lhsp)
3419 FOR_EACH_VEC_ELT (ce_s, rhsc, j, rhsp)
3420 process_constraint (new_constraint (*lhsp, *rhsp));
3424 struct constraint_expr tmp;
3425 tmp = new_scalar_tmp_constraint_exp ("allalltmp");
3426 FOR_EACH_VEC_ELT (ce_s, rhsc, i, rhsp)
3427 process_constraint (new_constraint (tmp, *rhsp));
3428 FOR_EACH_VEC_ELT (ce_s, lhsc, i, lhsp)
3429 process_constraint (new_constraint (*lhsp, tmp));
3433 /* Handle aggregate copies by expanding into copies of the respective
3434 fields of the structures. */
3437 do_structure_copy (tree lhsop, tree rhsop)
3439 struct constraint_expr *lhsp, *rhsp;
3440 VEC (ce_s, heap) *lhsc = NULL, *rhsc = NULL;
3443 get_constraint_for (lhsop, &lhsc);
3444 get_constraint_for_rhs (rhsop, &rhsc);
3445 lhsp = VEC_index (ce_s, lhsc, 0);
3446 rhsp = VEC_index (ce_s, rhsc, 0);
3447 if (lhsp->type == DEREF
3448 || (lhsp->type == ADDRESSOF && lhsp->var == anything_id)
3449 || rhsp->type == DEREF)
3451 if (lhsp->type == DEREF)
3453 gcc_assert (VEC_length (ce_s, lhsc) == 1);
3454 lhsp->offset = UNKNOWN_OFFSET;
3456 if (rhsp->type == DEREF)
3458 gcc_assert (VEC_length (ce_s, rhsc) == 1);
3459 rhsp->offset = UNKNOWN_OFFSET;
3461 process_all_all_constraints (lhsc, rhsc);
3463 else if (lhsp->type == SCALAR
3464 && (rhsp->type == SCALAR
3465 || rhsp->type == ADDRESSOF))
3467 HOST_WIDE_INT lhssize, lhsmaxsize, lhsoffset;
3468 HOST_WIDE_INT rhssize, rhsmaxsize, rhsoffset;
3470 get_ref_base_and_extent (lhsop, &lhsoffset, &lhssize, &lhsmaxsize);
3471 get_ref_base_and_extent (rhsop, &rhsoffset, &rhssize, &rhsmaxsize);
3472 for (j = 0; VEC_iterate (ce_s, lhsc, j, lhsp);)
3474 varinfo_t lhsv, rhsv;
3475 rhsp = VEC_index (ce_s, rhsc, k);
3476 lhsv = get_varinfo (lhsp->var);
3477 rhsv = get_varinfo (rhsp->var);
3478 if (lhsv->may_have_pointers
3479 && (lhsv->is_full_var
3480 || rhsv->is_full_var
3481 || ranges_overlap_p (lhsv->offset + rhsoffset, lhsv->size,
3482 rhsv->offset + lhsoffset, rhsv->size)))
3483 process_constraint (new_constraint (*lhsp, *rhsp));
3484 if (!rhsv->is_full_var
3485 && (lhsv->is_full_var
3486 || (lhsv->offset + rhsoffset + lhsv->size
3487 > rhsv->offset + lhsoffset + rhsv->size)))
3490 if (k >= VEC_length (ce_s, rhsc))
3500 VEC_free (ce_s, heap, lhsc);
3501 VEC_free (ce_s, heap, rhsc);
3504 /* Create constraints ID = { rhsc }. */
3507 make_constraints_to (unsigned id, VEC(ce_s, heap) *rhsc)
3509 struct constraint_expr *c;
3510 struct constraint_expr includes;
3514 includes.offset = 0;
3515 includes.type = SCALAR;
3517 FOR_EACH_VEC_ELT (ce_s, rhsc, j, c)
3518 process_constraint (new_constraint (includes, *c));
3521 /* Create a constraint ID = OP. */
3524 make_constraint_to (unsigned id, tree op)
3526 VEC(ce_s, heap) *rhsc = NULL;
3527 get_constraint_for_rhs (op, &rhsc);
3528 make_constraints_to (id, rhsc);
3529 VEC_free (ce_s, heap, rhsc);
3532 /* Create a constraint ID = &FROM. */
3535 make_constraint_from (varinfo_t vi, int from)
3537 struct constraint_expr lhs, rhs;
3545 rhs.type = ADDRESSOF;
3546 process_constraint (new_constraint (lhs, rhs));
3549 /* Create a constraint ID = FROM. */
3552 make_copy_constraint (varinfo_t vi, int from)
3554 struct constraint_expr lhs, rhs;
3563 process_constraint (new_constraint (lhs, rhs));
3566 /* Make constraints necessary to make OP escape. */
3569 make_escape_constraint (tree op)
3571 make_constraint_to (escaped_id, op);
3574 /* Add constraints to that the solution of VI is transitively closed. */
3577 make_transitive_closure_constraints (varinfo_t vi)
3579 struct constraint_expr lhs, rhs;
3588 process_constraint (new_constraint (lhs, rhs));
3590 /* VAR = VAR + UNKNOWN; */
3596 rhs.offset = UNKNOWN_OFFSET;
3597 process_constraint (new_constraint (lhs, rhs));
3600 /* Temporary storage for fake var decls. */
3601 struct obstack fake_var_decl_obstack;
3603 /* Build a fake VAR_DECL acting as referrer to a DECL_UID. */
3606 build_fake_var_decl (tree type)
3608 tree decl = (tree) XOBNEW (&fake_var_decl_obstack, struct tree_var_decl);
3609 memset (decl, 0, sizeof (struct tree_var_decl));
3610 TREE_SET_CODE (decl, VAR_DECL);
3611 TREE_TYPE (decl) = type;
3612 DECL_UID (decl) = allocate_decl_uid ();
3613 SET_DECL_PT_UID (decl, -1);
3614 layout_decl (decl, 0);
3618 /* Create a new artificial heap variable with NAME.
3619 Return the created variable. */
3622 make_heapvar (const char *name)
3627 heapvar = build_fake_var_decl (ptr_type_node);
3628 DECL_EXTERNAL (heapvar) = 1;
3630 vi = new_var_info (heapvar, name);
3631 vi->is_artificial_var = true;
3632 vi->is_heap_var = true;
3633 vi->is_unknown_size_var = true;
3637 vi->is_full_var = true;
3638 insert_vi_for_tree (heapvar, vi);
3643 /* Create a new artificial heap variable with NAME and make a
3644 constraint from it to LHS. Set flags according to a tag used
3645 for tracking restrict pointers. */
3648 make_constraint_from_restrict (varinfo_t lhs, const char *name)
3650 varinfo_t vi = make_heapvar (name);
3651 vi->is_global_var = 1;
3652 vi->may_have_pointers = 1;
3653 make_constraint_from (lhs, vi->id);
3657 /* Create a new artificial heap variable with NAME and make a
3658 constraint from it to LHS. Set flags according to a tag used
3659 for tracking restrict pointers and make the artificial heap
3660 point to global memory. */
3663 make_constraint_from_global_restrict (varinfo_t lhs, const char *name)
3665 varinfo_t vi = make_constraint_from_restrict (lhs, name);
3666 make_copy_constraint (vi, nonlocal_id);
3670 /* In IPA mode there are varinfos for different aspects of reach
3671 function designator. One for the points-to set of the return
3672 value, one for the variables that are clobbered by the function,
3673 one for its uses and one for each parameter (including a single
3674 glob for remaining variadic arguments). */
3676 enum { fi_clobbers = 1, fi_uses = 2,
3677 fi_static_chain = 3, fi_result = 4, fi_parm_base = 5 };
3679 /* Get a constraint for the requested part of a function designator FI
3680 when operating in IPA mode. */
3682 static struct constraint_expr
3683 get_function_part_constraint (varinfo_t fi, unsigned part)
3685 struct constraint_expr c;
3687 gcc_assert (in_ipa_mode);
3689 if (fi->id == anything_id)
3691 /* ??? We probably should have a ANYFN special variable. */
3692 c.var = anything_id;
3696 else if (TREE_CODE (fi->decl) == FUNCTION_DECL)
3698 varinfo_t ai = first_vi_for_offset (fi, part);
3702 c.var = anything_id;
3716 /* For non-IPA mode, generate constraints necessary for a call on the
3720 handle_rhs_call (gimple stmt, VEC(ce_s, heap) **results)
3722 struct constraint_expr rhsc;
3724 bool returns_uses = false;
3726 for (i = 0; i < gimple_call_num_args (stmt); ++i)
3728 tree arg = gimple_call_arg (stmt, i);
3729 int flags = gimple_call_arg_flags (stmt, i);
3731 /* If the argument is not used we can ignore it. */
3732 if (flags & EAF_UNUSED)
3735 /* As we compute ESCAPED context-insensitive we do not gain
3736 any precision with just EAF_NOCLOBBER but not EAF_NOESCAPE
3737 set. The argument would still get clobbered through the
3739 if ((flags & EAF_NOCLOBBER)
3740 && (flags & EAF_NOESCAPE))
3742 varinfo_t uses = get_call_use_vi (stmt);
3743 if (!(flags & EAF_DIRECT))
3745 varinfo_t tem = new_var_info (NULL_TREE, "callarg");
3746 make_constraint_to (tem->id, arg);
3747 make_transitive_closure_constraints (tem);
3748 make_copy_constraint (uses, tem->id);
3751 make_constraint_to (uses->id, arg);
3752 returns_uses = true;
3754 else if (flags & EAF_NOESCAPE)
3756 struct constraint_expr lhs, rhs;
3757 varinfo_t uses = get_call_use_vi (stmt);
3758 varinfo_t clobbers = get_call_clobber_vi (stmt);
3759 varinfo_t tem = new_var_info (NULL_TREE, "callarg");
3760 make_constraint_to (tem->id, arg);
3761 if (!(flags & EAF_DIRECT))
3762 make_transitive_closure_constraints (tem);
3763 make_copy_constraint (uses, tem->id);
3764 make_copy_constraint (clobbers, tem->id);
3765 /* Add *tem = nonlocal, do not add *tem = callused as
3766 EAF_NOESCAPE parameters do not escape to other parameters
3767 and all other uses appear in NONLOCAL as well. */
3772 rhs.var = nonlocal_id;
3774 process_constraint (new_constraint (lhs, rhs));
3775 returns_uses = true;
3778 make_escape_constraint (arg);
3781 /* If we added to the calls uses solution make sure we account for
3782 pointers to it to be returned. */
3785 rhsc.var = get_call_use_vi (stmt)->id;
3788 VEC_safe_push (ce_s, heap, *results, &rhsc);
3791 /* The static chain escapes as well. */
3792 if (gimple_call_chain (stmt))
3793 make_escape_constraint (gimple_call_chain (stmt));
3795 /* And if we applied NRV the address of the return slot escapes as well. */
3796 if (gimple_call_return_slot_opt_p (stmt)
3797 && gimple_call_lhs (stmt) != NULL_TREE
3798 && TREE_ADDRESSABLE (TREE_TYPE (gimple_call_lhs (stmt))))
3800 VEC(ce_s, heap) *tmpc = NULL;
3801 struct constraint_expr lhsc, *c;
3802 get_constraint_for_address_of (gimple_call_lhs (stmt), &tmpc);
3803 lhsc.var = escaped_id;
3806 FOR_EACH_VEC_ELT (ce_s, tmpc, i, c)
3807 process_constraint (new_constraint (lhsc, *c));
3808 VEC_free(ce_s, heap, tmpc);
3811 /* Regular functions return nonlocal memory. */
3812 rhsc.var = nonlocal_id;
3815 VEC_safe_push (ce_s, heap, *results, &rhsc);
3818 /* For non-IPA mode, generate constraints necessary for a call
3819 that returns a pointer and assigns it to LHS. This simply makes
3820 the LHS point to global and escaped variables. */
3823 handle_lhs_call (gimple stmt, tree lhs, int flags, VEC(ce_s, heap) *rhsc,
3826 VEC(ce_s, heap) *lhsc = NULL;
3828 get_constraint_for (lhs, &lhsc);
3829 /* If the store is to a global decl make sure to
3830 add proper escape constraints. */
3831 lhs = get_base_address (lhs);
3834 && is_global_var (lhs))
3836 struct constraint_expr tmpc;
3837 tmpc.var = escaped_id;
3840 VEC_safe_push (ce_s, heap, lhsc, &tmpc);
3843 /* If the call returns an argument unmodified override the rhs
3845 flags = gimple_call_return_flags (stmt);
3846 if (flags & ERF_RETURNS_ARG
3847 && (flags & ERF_RETURN_ARG_MASK) < gimple_call_num_args (stmt))
3851 arg = gimple_call_arg (stmt, flags & ERF_RETURN_ARG_MASK);
3852 get_constraint_for (arg, &rhsc);
3853 process_all_all_constraints (lhsc, rhsc);
3854 VEC_free (ce_s, heap, rhsc);
3856 else if (flags & ERF_NOALIAS)
3859 struct constraint_expr tmpc;
3861 vi = make_heapvar ("HEAP");
3862 /* We delay marking allocated storage global until we know if
3864 DECL_EXTERNAL (vi->decl) = 0;
3865 vi->is_global_var = 0;
3866 /* If this is not a real malloc call assume the memory was
3867 initialized and thus may point to global memory. All
3868 builtin functions with the malloc attribute behave in a sane way. */
3870 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
3871 make_constraint_from (vi, nonlocal_id);
3874 tmpc.type = ADDRESSOF;
3875 VEC_safe_push (ce_s, heap, rhsc, &tmpc);
3876 process_all_all_constraints (lhsc, rhsc);
3877 VEC_free (ce_s, heap, rhsc);
3880 process_all_all_constraints (lhsc, rhsc);
3882 VEC_free (ce_s, heap, lhsc);
3885 /* For non-IPA mode, generate constraints necessary for a call of a
3886 const function that returns a pointer in the statement STMT. */
3889 handle_const_call (gimple stmt, VEC(ce_s, heap) **results)
3891 struct constraint_expr rhsc;
3894 /* Treat nested const functions the same as pure functions as far
3895 as the static chain is concerned. */
3896 if (gimple_call_chain (stmt))
3898 varinfo_t uses = get_call_use_vi (stmt);
3899 make_transitive_closure_constraints (uses);
3900 make_constraint_to (uses->id, gimple_call_chain (stmt));
3901 rhsc.var = uses->id;
3904 VEC_safe_push (ce_s, heap, *results, &rhsc);
3907 /* May return arguments. */
3908 for (k = 0; k < gimple_call_num_args (stmt); ++k)
3910 tree arg = gimple_call_arg (stmt, k);
3911 VEC(ce_s, heap) *argc = NULL;
3913 struct constraint_expr *argp;
3914 get_constraint_for_rhs (arg, &argc);
3915 FOR_EACH_VEC_ELT (ce_s, argc, i, argp)
3916 VEC_safe_push (ce_s, heap, *results, argp);
3917 VEC_free(ce_s, heap, argc);
3920 /* May return addresses of globals. */
3921 rhsc.var = nonlocal_id;
3923 rhsc.type = ADDRESSOF;
3924 VEC_safe_push (ce_s, heap, *results, &rhsc);
3927 /* For non-IPA mode, generate constraints necessary for a call to a
3928 pure function in statement STMT. */
3931 handle_pure_call (gimple stmt, VEC(ce_s, heap) **results)
3933 struct constraint_expr rhsc;
3935 varinfo_t uses = NULL;
3937 /* Memory reached from pointer arguments is call-used. */
3938 for (i = 0; i < gimple_call_num_args (stmt); ++i)
3940 tree arg = gimple_call_arg (stmt, i);
3943 uses = get_call_use_vi (stmt);
3944 make_transitive_closure_constraints (uses);
3946 make_constraint_to (uses->id, arg);
3949 /* The static chain is used as well. */
3950 if (gimple_call_chain (stmt))
3954 uses = get_call_use_vi (stmt);
3955 make_transitive_closure_constraints (uses);
3957 make_constraint_to (uses->id, gimple_call_chain (stmt));
3960 /* Pure functions may return call-used and nonlocal memory. */
3963 rhsc.var = uses->id;
3966 VEC_safe_push (ce_s, heap, *results, &rhsc);
3968 rhsc.var = nonlocal_id;
3971 VEC_safe_push (ce_s, heap, *results, &rhsc);
3975 /* Return the varinfo for the callee of CALL. */
3978 get_fi_for_callee (gimple call)
3980 tree decl, fn = gimple_call_fn (call);
3982 if (fn && TREE_CODE (fn) == OBJ_TYPE_REF)
3983 fn = OBJ_TYPE_REF_EXPR (fn);
3985 /* If we can directly resolve the function being called, do so.
3986 Otherwise, it must be some sort of indirect expression that
3987 we should still be able to handle. */
3988 decl = gimple_call_addr_fndecl (fn);
3990 return get_vi_for_tree (decl);
3992 /* If the function is anything other than a SSA name pointer we have no
3993 clue and should be getting ANYFN (well, ANYTHING for now). */
3994 if (!fn || TREE_CODE (fn) != SSA_NAME)
3995 return get_varinfo (anything_id);
3997 if ((TREE_CODE (SSA_NAME_VAR (fn)) == PARM_DECL
3998 || TREE_CODE (SSA_NAME_VAR (fn)) == RESULT_DECL)
3999 && SSA_NAME_IS_DEFAULT_DEF (fn))
4000 fn = SSA_NAME_VAR (fn);
4002 return get_vi_for_tree (fn);
4005 /* Create constraints for the builtin call T. Return true if the call
4006 was handled, otherwise false. */
4009 find_func_aliases_for_builtin_call (gimple t)
4011 tree fndecl = gimple_call_fndecl (t);
4012 VEC(ce_s, heap) *lhsc = NULL;
4013 VEC(ce_s, heap) *rhsc = NULL;
4016 if (fndecl != NULL_TREE
4017 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
4018 /* ??? All builtins that are handled here need to be handled
4019 in the alias-oracle query functions explicitly! */
4020 switch (DECL_FUNCTION_CODE (fndecl))
4022 /* All the following functions return a pointer to the same object
4023 as their first argument points to. The functions do not add
4024 to the ESCAPED solution. The functions make the first argument
4025 pointed to memory point to what the second argument pointed to
4026 memory points to. */
4027 case BUILT_IN_STRCPY:
4028 case BUILT_IN_STRNCPY:
4029 case BUILT_IN_BCOPY:
4030 case BUILT_IN_MEMCPY:
4031 case BUILT_IN_MEMMOVE:
4032 case BUILT_IN_MEMPCPY:
4033 case BUILT_IN_STPCPY:
4034 case BUILT_IN_STPNCPY:
4035 case BUILT_IN_STRCAT:
4036 case BUILT_IN_STRNCAT:
4037 case BUILT_IN_STRCPY_CHK:
4038 case BUILT_IN_STRNCPY_CHK:
4039 case BUILT_IN_MEMCPY_CHK:
4040 case BUILT_IN_MEMMOVE_CHK:
4041 case BUILT_IN_MEMPCPY_CHK:
4042 case BUILT_IN_STPCPY_CHK:
4043 case BUILT_IN_STPNCPY_CHK:
4044 case BUILT_IN_STRCAT_CHK:
4045 case BUILT_IN_STRNCAT_CHK:
4046 case BUILT_IN_TM_MEMCPY:
4047 case BUILT_IN_TM_MEMMOVE:
4049 tree res = gimple_call_lhs (t);
4050 tree dest = gimple_call_arg (t, (DECL_FUNCTION_CODE (fndecl)
4051 == BUILT_IN_BCOPY ? 1 : 0));
4052 tree src = gimple_call_arg (t, (DECL_FUNCTION_CODE (fndecl)
4053 == BUILT_IN_BCOPY ? 0 : 1));
4054 if (res != NULL_TREE)
4056 get_constraint_for (res, &lhsc);
4057 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_MEMPCPY
4058 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPCPY
4059 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPNCPY
4060 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_MEMPCPY_CHK
4061 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPCPY_CHK
4062 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPNCPY_CHK)
4063 get_constraint_for_ptr_offset (dest, NULL_TREE, &rhsc);
4065 get_constraint_for (dest, &rhsc);
4066 process_all_all_constraints (lhsc, rhsc);
4067 VEC_free (ce_s, heap, lhsc);
4068 VEC_free (ce_s, heap, rhsc);
4070 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
4071 get_constraint_for_ptr_offset (src, NULL_TREE, &rhsc);
4074 process_all_all_constraints (lhsc, rhsc);
4075 VEC_free (ce_s, heap, lhsc);
4076 VEC_free (ce_s, heap, rhsc);
4079 case BUILT_IN_MEMSET:
4080 case BUILT_IN_MEMSET_CHK:
4081 case BUILT_IN_TM_MEMSET:
4083 tree res = gimple_call_lhs (t);
4084 tree dest = gimple_call_arg (t, 0);
4087 struct constraint_expr ac;
4088 if (res != NULL_TREE)
4090 get_constraint_for (res, &lhsc);
4091 get_constraint_for (dest, &rhsc);
4092 process_all_all_constraints (lhsc, rhsc);
4093 VEC_free (ce_s, heap, lhsc);
4094 VEC_free (ce_s, heap, rhsc);
4096 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
4098 if (flag_delete_null_pointer_checks
4099 && integer_zerop (gimple_call_arg (t, 1)))
4101 ac.type = ADDRESSOF;
4102 ac.var = nothing_id;
4107 ac.var = integer_id;
4110 FOR_EACH_VEC_ELT (ce_s, lhsc, i, lhsp)
4111 process_constraint (new_constraint (*lhsp, ac));
4112 VEC_free (ce_s, heap, lhsc);
4115 case BUILT_IN_ASSUME_ALIGNED:
4117 tree res = gimple_call_lhs (t);
4118 tree dest = gimple_call_arg (t, 0);
4119 if (res != NULL_TREE)
4121 get_constraint_for (res, &lhsc);
4122 get_constraint_for (dest, &rhsc);
4123 process_all_all_constraints (lhsc, rhsc);
4124 VEC_free (ce_s, heap, lhsc);
4125 VEC_free (ce_s, heap, rhsc);
4129 /* All the following functions do not return pointers, do not
4130 modify the points-to sets of memory reachable from their
4131 arguments and do not add to the ESCAPED solution. */
4132 case BUILT_IN_SINCOS:
4133 case BUILT_IN_SINCOSF:
4134 case BUILT_IN_SINCOSL:
4135 case BUILT_IN_FREXP:
4136 case BUILT_IN_FREXPF:
4137 case BUILT_IN_FREXPL:
4138 case BUILT_IN_GAMMA_R:
4139 case BUILT_IN_GAMMAF_R:
4140 case BUILT_IN_GAMMAL_R:
4141 case BUILT_IN_LGAMMA_R:
4142 case BUILT_IN_LGAMMAF_R:
4143 case BUILT_IN_LGAMMAL_R:
4145 case BUILT_IN_MODFF:
4146 case BUILT_IN_MODFL:
4147 case BUILT_IN_REMQUO:
4148 case BUILT_IN_REMQUOF:
4149 case BUILT_IN_REMQUOL:
4152 case BUILT_IN_STRDUP:
4153 case BUILT_IN_STRNDUP:
4154 if (gimple_call_lhs (t))
4156 handle_lhs_call (t, gimple_call_lhs (t), gimple_call_flags (t),
4158 get_constraint_for_ptr_offset (gimple_call_lhs (t),
4160 get_constraint_for_ptr_offset (gimple_call_arg (t, 0),
4164 process_all_all_constraints (lhsc, rhsc);
4165 VEC_free (ce_s, heap, lhsc);
4166 VEC_free (ce_s, heap, rhsc);
4170 /* Trampolines are special - they set up passing the static
4172 case BUILT_IN_INIT_TRAMPOLINE:
4174 tree tramp = gimple_call_arg (t, 0);
4175 tree nfunc = gimple_call_arg (t, 1);
4176 tree frame = gimple_call_arg (t, 2);
4178 struct constraint_expr lhs, *rhsp;
4181 varinfo_t nfi = NULL;
4182 gcc_assert (TREE_CODE (nfunc) == ADDR_EXPR);
4183 nfi = lookup_vi_for_tree (TREE_OPERAND (nfunc, 0));
4186 lhs = get_function_part_constraint (nfi, fi_static_chain);
4187 get_constraint_for (frame, &rhsc);
4188 FOR_EACH_VEC_ELT (ce_s, rhsc, i, rhsp)
4189 process_constraint (new_constraint (lhs, *rhsp));
4190 VEC_free (ce_s, heap, rhsc);
4192 /* Make the frame point to the function for
4193 the trampoline adjustment call. */
4194 get_constraint_for (tramp, &lhsc);
4196 get_constraint_for (nfunc, &rhsc);
4197 process_all_all_constraints (lhsc, rhsc);
4198 VEC_free (ce_s, heap, rhsc);
4199 VEC_free (ce_s, heap, lhsc);
4204 /* Else fallthru to generic handling which will let
4205 the frame escape. */
4208 case BUILT_IN_ADJUST_TRAMPOLINE:
4210 tree tramp = gimple_call_arg (t, 0);
4211 tree res = gimple_call_lhs (t);
4212 if (in_ipa_mode && res)
4214 get_constraint_for (res, &lhsc);
4215 get_constraint_for (tramp, &rhsc);
4217 process_all_all_constraints (lhsc, rhsc);
4218 VEC_free (ce_s, heap, rhsc);
4219 VEC_free (ce_s, heap, lhsc);
4223 CASE_BUILT_IN_TM_STORE (1):
4224 CASE_BUILT_IN_TM_STORE (2):
4225 CASE_BUILT_IN_TM_STORE (4):
4226 CASE_BUILT_IN_TM_STORE (8):
4227 CASE_BUILT_IN_TM_STORE (FLOAT):
4228 CASE_BUILT_IN_TM_STORE (DOUBLE):
4229 CASE_BUILT_IN_TM_STORE (LDOUBLE):
4230 CASE_BUILT_IN_TM_STORE (M64):
4231 CASE_BUILT_IN_TM_STORE (M128):
4232 CASE_BUILT_IN_TM_STORE (M256):
4234 tree addr = gimple_call_arg (t, 0);
4235 tree src = gimple_call_arg (t, 1);
4237 get_constraint_for (addr, &lhsc);
4239 get_constraint_for (src, &rhsc);
4240 process_all_all_constraints (lhsc, rhsc);
4241 VEC_free (ce_s, heap, lhsc);
4242 VEC_free (ce_s, heap, rhsc);
4245 CASE_BUILT_IN_TM_LOAD (1):
4246 CASE_BUILT_IN_TM_LOAD (2):
4247 CASE_BUILT_IN_TM_LOAD (4):
4248 CASE_BUILT_IN_TM_LOAD (8):
4249 CASE_BUILT_IN_TM_LOAD (FLOAT):
4250 CASE_BUILT_IN_TM_LOAD (DOUBLE):
4251 CASE_BUILT_IN_TM_LOAD (LDOUBLE):
4252 CASE_BUILT_IN_TM_LOAD (M64):
4253 CASE_BUILT_IN_TM_LOAD (M128):
4254 CASE_BUILT_IN_TM_LOAD (M256):
4256 tree dest = gimple_call_lhs (t);
4257 tree addr = gimple_call_arg (t, 0);
4259 get_constraint_for (dest, &lhsc);
4260 get_constraint_for (addr, &rhsc);
4262 process_all_all_constraints (lhsc, rhsc);
4263 VEC_free (ce_s, heap, lhsc);
4264 VEC_free (ce_s, heap, rhsc);
4267 /* Variadic argument handling needs to be handled in IPA
4269 case BUILT_IN_VA_START:
4271 tree valist = gimple_call_arg (t, 0);
4272 struct constraint_expr rhs, *lhsp;
4274 get_constraint_for (valist, &lhsc);
4276 /* The va_list gets access to pointers in variadic
4277 arguments. Which we know in the case of IPA analysis
4278 and otherwise are just all nonlocal variables. */
4281 fi = lookup_vi_for_tree (cfun->decl);
4282 rhs = get_function_part_constraint (fi, ~0);
4283 rhs.type = ADDRESSOF;
4287 rhs.var = nonlocal_id;
4288 rhs.type = ADDRESSOF;
4291 FOR_EACH_VEC_ELT (ce_s, lhsc, i, lhsp)
4292 process_constraint (new_constraint (*lhsp, rhs));
4293 VEC_free (ce_s, heap, lhsc);
4294 /* va_list is clobbered. */
4295 make_constraint_to (get_call_clobber_vi (t)->id, valist);
4298 /* va_end doesn't have any effect that matters. */
4299 case BUILT_IN_VA_END:
4301 /* Alternate return. Simply give up for now. */
4302 case BUILT_IN_RETURN:
4306 || !(fi = get_vi_for_tree (cfun->decl)))
4307 make_constraint_from (get_varinfo (escaped_id), anything_id);
4308 else if (in_ipa_mode
4311 struct constraint_expr lhs, rhs;
4312 lhs = get_function_part_constraint (fi, fi_result);
4313 rhs.var = anything_id;
4316 process_constraint (new_constraint (lhs, rhs));
4320 /* printf-style functions may have hooks to set pointers to
4321 point to somewhere into the generated string. Leave them
4322 for a later excercise... */
4324 /* Fallthru to general call handling. */;
4330 /* Create constraints for the call T. */
4333 find_func_aliases_for_call (gimple t)
4335 tree fndecl = gimple_call_fndecl (t);
4336 VEC(ce_s, heap) *lhsc = NULL;
4337 VEC(ce_s, heap) *rhsc = NULL;
4340 if (fndecl != NULL_TREE
4341 && DECL_BUILT_IN (fndecl)
4342 && find_func_aliases_for_builtin_call (t))
4345 fi = get_fi_for_callee (t);
4347 || (fndecl && !fi->is_fn_info))
4349 VEC(ce_s, heap) *rhsc = NULL;
4350 int flags = gimple_call_flags (t);
4352 /* Const functions can return their arguments and addresses
4353 of global memory but not of escaped memory. */
4354 if (flags & (ECF_CONST|ECF_NOVOPS))
4356 if (gimple_call_lhs (t))
4357 handle_const_call (t, &rhsc);
4359 /* Pure functions can return addresses in and of memory
4360 reachable from their arguments, but they are not an escape
4361 point for reachable memory of their arguments. */
4362 else if (flags & (ECF_PURE|ECF_LOOPING_CONST_OR_PURE))
4363 handle_pure_call (t, &rhsc);
4365 handle_rhs_call (t, &rhsc);
4366 if (gimple_call_lhs (t))
4367 handle_lhs_call (t, gimple_call_lhs (t), flags, rhsc, fndecl);
4368 VEC_free (ce_s, heap, rhsc);
4375 /* Assign all the passed arguments to the appropriate incoming
4376 parameters of the function. */
4377 for (j = 0; j < gimple_call_num_args (t); j++)
4379 struct constraint_expr lhs ;
4380 struct constraint_expr *rhsp;
4381 tree arg = gimple_call_arg (t, j);
4383 get_constraint_for_rhs (arg, &rhsc);
4384 lhs = get_function_part_constraint (fi, fi_parm_base + j);
4385 while (VEC_length (ce_s, rhsc) != 0)
4387 rhsp = VEC_last (ce_s, rhsc);
4388 process_constraint (new_constraint (lhs, *rhsp));
4389 VEC_pop (ce_s, rhsc);
4393 /* If we are returning a value, assign it to the result. */
4394 lhsop = gimple_call_lhs (t);
4397 struct constraint_expr rhs;
4398 struct constraint_expr *lhsp;
4400 get_constraint_for (lhsop, &lhsc);
4401 rhs = get_function_part_constraint (fi, fi_result);
4403 && DECL_RESULT (fndecl)
4404 && DECL_BY_REFERENCE (DECL_RESULT (fndecl)))
4406 VEC(ce_s, heap) *tem = NULL;
4407 VEC_safe_push (ce_s, heap, tem, &rhs);
4409 rhs = *VEC_index (ce_s, tem, 0);
4410 VEC_free(ce_s, heap, tem);
4412 FOR_EACH_VEC_ELT (ce_s, lhsc, j, lhsp)
4413 process_constraint (new_constraint (*lhsp, rhs));
4416 /* If we pass the result decl by reference, honor that. */
4419 && DECL_RESULT (fndecl)
4420 && DECL_BY_REFERENCE (DECL_RESULT (fndecl)))
4422 struct constraint_expr lhs;
4423 struct constraint_expr *rhsp;
4425 get_constraint_for_address_of (lhsop, &rhsc);
4426 lhs = get_function_part_constraint (fi, fi_result);
4427 FOR_EACH_VEC_ELT (ce_s, rhsc, j, rhsp)
4428 process_constraint (new_constraint (lhs, *rhsp));
4429 VEC_free (ce_s, heap, rhsc);
4432 /* If we use a static chain, pass it along. */
4433 if (gimple_call_chain (t))
4435 struct constraint_expr lhs;
4436 struct constraint_expr *rhsp;
4438 get_constraint_for (gimple_call_chain (t), &rhsc);
4439 lhs = get_function_part_constraint (fi, fi_static_chain);
4440 FOR_EACH_VEC_ELT (ce_s, rhsc, j, rhsp)
4441 process_constraint (new_constraint (lhs, *rhsp));
4446 /* Walk statement T setting up aliasing constraints according to the
4447 references found in T. This function is the main part of the
4448 constraint builder. AI points to auxiliary alias information used
4449 when building alias sets and computing alias grouping heuristics. */
4452 find_func_aliases (gimple origt)
4455 VEC(ce_s, heap) *lhsc = NULL;
4456 VEC(ce_s, heap) *rhsc = NULL;
4457 struct constraint_expr *c;
4460 /* Now build constraints expressions. */
4461 if (gimple_code (t) == GIMPLE_PHI)
4466 /* For a phi node, assign all the arguments to
4468 get_constraint_for (gimple_phi_result (t), &lhsc);
4469 for (i = 0; i < gimple_phi_num_args (t); i++)
4471 tree strippedrhs = PHI_ARG_DEF (t, i);
4473 STRIP_NOPS (strippedrhs);
4474 get_constraint_for_rhs (gimple_phi_arg_def (t, i), &rhsc);
4476 FOR_EACH_VEC_ELT (ce_s, lhsc, j, c)
4478 struct constraint_expr *c2;
4479 while (VEC_length (ce_s, rhsc) > 0)
4481 c2 = VEC_last (ce_s, rhsc);
4482 process_constraint (new_constraint (*c, *c2));
4483 VEC_pop (ce_s, rhsc);
4488 /* In IPA mode, we need to generate constraints to pass call
4489 arguments through their calls. There are two cases,
4490 either a GIMPLE_CALL returning a value, or just a plain
4491 GIMPLE_CALL when we are not.
4493 In non-ipa mode, we need to generate constraints for each
4494 pointer passed by address. */
4495 else if (is_gimple_call (t))
4496 find_func_aliases_for_call (t);
4498 /* Otherwise, just a regular assignment statement. Only care about
4499 operations with pointer result, others are dealt with as escape
4500 points if they have pointer operands. */
4501 else if (is_gimple_assign (t))
4503 /* Otherwise, just a regular assignment statement. */
4504 tree lhsop = gimple_assign_lhs (t);
4505 tree rhsop = (gimple_num_ops (t) == 2) ? gimple_assign_rhs1 (t) : NULL;
4507 if (rhsop && TREE_CLOBBER_P (rhsop))
4508 /* Ignore clobbers, they don't actually store anything into
4511 else if (rhsop && AGGREGATE_TYPE_P (TREE_TYPE (lhsop)))
4512 do_structure_copy (lhsop, rhsop);
4515 enum tree_code code = gimple_assign_rhs_code (t);
4517 get_constraint_for (lhsop, &lhsc);
4519 if (code == POINTER_PLUS_EXPR)
4520 get_constraint_for_ptr_offset (gimple_assign_rhs1 (t),
4521 gimple_assign_rhs2 (t), &rhsc);
4522 else if (code == BIT_AND_EXPR
4523 && TREE_CODE (gimple_assign_rhs2 (t)) == INTEGER_CST)
4525 /* Aligning a pointer via a BIT_AND_EXPR is offsetting
4526 the pointer. Handle it by offsetting it by UNKNOWN. */
4527 get_constraint_for_ptr_offset (gimple_assign_rhs1 (t),
4530 else if ((CONVERT_EXPR_CODE_P (code)
4531 && !(POINTER_TYPE_P (gimple_expr_type (t))
4532 && !POINTER_TYPE_P (TREE_TYPE (rhsop))))
4533 || gimple_assign_single_p (t))
4534 get_constraint_for_rhs (rhsop, &rhsc);
4535 else if (truth_value_p (code))
4536 /* Truth value results are not pointer (parts). Or at least
4537 very very unreasonable obfuscation of a part. */
4541 /* All other operations are merges. */
4542 VEC (ce_s, heap) *tmp = NULL;
4543 struct constraint_expr *rhsp;
4545 get_constraint_for_rhs (gimple_assign_rhs1 (t), &rhsc);
4546 for (i = 2; i < gimple_num_ops (t); ++i)
4548 get_constraint_for_rhs (gimple_op (t, i), &tmp);
4549 FOR_EACH_VEC_ELT (ce_s, tmp, j, rhsp)
4550 VEC_safe_push (ce_s, heap, rhsc, rhsp);
4551 VEC_truncate (ce_s, tmp, 0);
4553 VEC_free (ce_s, heap, tmp);
4555 process_all_all_constraints (lhsc, rhsc);
4557 /* If there is a store to a global variable the rhs escapes. */
4558 if ((lhsop = get_base_address (lhsop)) != NULL_TREE
4560 && is_global_var (lhsop)
4562 || DECL_EXTERNAL (lhsop) || TREE_PUBLIC (lhsop)))
4563 make_escape_constraint (rhsop);
4565 /* Handle escapes through return. */
4566 else if (gimple_code (t) == GIMPLE_RETURN
4567 && gimple_return_retval (t) != NULL_TREE)
4571 || !(fi = get_vi_for_tree (cfun->decl)))
4572 make_escape_constraint (gimple_return_retval (t));
4573 else if (in_ipa_mode
4576 struct constraint_expr lhs ;
4577 struct constraint_expr *rhsp;
4580 lhs = get_function_part_constraint (fi, fi_result);
4581 get_constraint_for_rhs (gimple_return_retval (t), &rhsc);
4582 FOR_EACH_VEC_ELT (ce_s, rhsc, i, rhsp)
4583 process_constraint (new_constraint (lhs, *rhsp));
4586 /* Handle asms conservatively by adding escape constraints to everything. */
4587 else if (gimple_code (t) == GIMPLE_ASM)
4589 unsigned i, noutputs;
4590 const char **oconstraints;
4591 const char *constraint;
4592 bool allows_mem, allows_reg, is_inout;
4594 noutputs = gimple_asm_noutputs (t);
4595 oconstraints = XALLOCAVEC (const char *, noutputs);
4597 for (i = 0; i < noutputs; ++i)
4599 tree link = gimple_asm_output_op (t, i);
4600 tree op = TREE_VALUE (link);
4602 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
4603 oconstraints[i] = constraint;
4604 parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
4605 &allows_reg, &is_inout);
4607 /* A memory constraint makes the address of the operand escape. */
4608 if (!allows_reg && allows_mem)
4609 make_escape_constraint (build_fold_addr_expr (op));
4611 /* The asm may read global memory, so outputs may point to
4612 any global memory. */
4615 VEC(ce_s, heap) *lhsc = NULL;
4616 struct constraint_expr rhsc, *lhsp;
4618 get_constraint_for (op, &lhsc);
4619 rhsc.var = nonlocal_id;
4622 FOR_EACH_VEC_ELT (ce_s, lhsc, j, lhsp)
4623 process_constraint (new_constraint (*lhsp, rhsc));
4624 VEC_free (ce_s, heap, lhsc);
4627 for (i = 0; i < gimple_asm_ninputs (t); ++i)
4629 tree link = gimple_asm_input_op (t, i);
4630 tree op = TREE_VALUE (link);
4632 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
4634 parse_input_constraint (&constraint, 0, 0, noutputs, 0, oconstraints,
4635 &allows_mem, &allows_reg);
4637 /* A memory constraint makes the address of the operand escape. */
4638 if (!allows_reg && allows_mem)
4639 make_escape_constraint (build_fold_addr_expr (op));
4640 /* Strictly we'd only need the constraint to ESCAPED if
4641 the asm clobbers memory, otherwise using something
4642 along the lines of per-call clobbers/uses would be enough. */
4644 make_escape_constraint (op);
4648 VEC_free (ce_s, heap, rhsc);
4649 VEC_free (ce_s, heap, lhsc);
4653 /* Create a constraint adding to the clobber set of FI the memory
4654 pointed to by PTR. */
4657 process_ipa_clobber (varinfo_t fi, tree ptr)
4659 VEC(ce_s, heap) *ptrc = NULL;
4660 struct constraint_expr *c, lhs;
4662 get_constraint_for_rhs (ptr, &ptrc);
4663 lhs = get_function_part_constraint (fi, fi_clobbers);
4664 FOR_EACH_VEC_ELT (ce_s, ptrc, i, c)
4665 process_constraint (new_constraint (lhs, *c));
4666 VEC_free (ce_s, heap, ptrc);
4669 /* Walk statement T setting up clobber and use constraints according to the
4670 references found in T. This function is a main part of the
4671 IPA constraint builder. */
4674 find_func_clobbers (gimple origt)
4677 VEC(ce_s, heap) *lhsc = NULL;
4678 VEC(ce_s, heap) *rhsc = NULL;
4681 /* Add constraints for clobbered/used in IPA mode.
4682 We are not interested in what automatic variables are clobbered
4683 or used as we only use the information in the caller to which
4684 they do not escape. */
4685 gcc_assert (in_ipa_mode);
4687 /* If the stmt refers to memory in any way it better had a VUSE. */
4688 if (gimple_vuse (t) == NULL_TREE)
4691 /* We'd better have function information for the current function. */
4692 fi = lookup_vi_for_tree (cfun->decl);
4693 gcc_assert (fi != NULL);
4695 /* Account for stores in assignments and calls. */
4696 if (gimple_vdef (t) != NULL_TREE
4697 && gimple_has_lhs (t))
4699 tree lhs = gimple_get_lhs (t);
4701 while (handled_component_p (tem))
4702 tem = TREE_OPERAND (tem, 0);
4704 && !auto_var_in_fn_p (tem, cfun->decl))
4705 || INDIRECT_REF_P (tem)
4706 || (TREE_CODE (tem) == MEM_REF
4707 && !(TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR
4709 (TREE_OPERAND (TREE_OPERAND (tem, 0), 0), cfun->decl))))
4711 struct constraint_expr lhsc, *rhsp;
4713 lhsc = get_function_part_constraint (fi, fi_clobbers);
4714 get_constraint_for_address_of (lhs, &rhsc);
4715 FOR_EACH_VEC_ELT (ce_s, rhsc, i, rhsp)
4716 process_constraint (new_constraint (lhsc, *rhsp));
4717 VEC_free (ce_s, heap, rhsc);
4721 /* Account for uses in assigments and returns. */
4722 if (gimple_assign_single_p (t)
4723 || (gimple_code (t) == GIMPLE_RETURN
4724 && gimple_return_retval (t) != NULL_TREE))
4726 tree rhs = (gimple_assign_single_p (t)
4727 ? gimple_assign_rhs1 (t) : gimple_return_retval (t));
4729 while (handled_component_p (tem))
4730 tem = TREE_OPERAND (tem, 0);
4732 && !auto_var_in_fn_p (tem, cfun->decl))
4733 || INDIRECT_REF_P (tem)
4734 || (TREE_CODE (tem) == MEM_REF
4735 && !(TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR
4737 (TREE_OPERAND (TREE_OPERAND (tem, 0), 0), cfun->decl))))
4739 struct constraint_expr lhs, *rhsp;
4741 lhs = get_function_part_constraint (fi, fi_uses);
4742 get_constraint_for_address_of (rhs, &rhsc);
4743 FOR_EACH_VEC_ELT (ce_s, rhsc, i, rhsp)
4744 process_constraint (new_constraint (lhs, *rhsp));
4745 VEC_free (ce_s, heap, rhsc);
4749 if (is_gimple_call (t))
4751 varinfo_t cfi = NULL;
4752 tree decl = gimple_call_fndecl (t);
4753 struct constraint_expr lhs, rhs;
4756 /* For builtins we do not have separate function info. For those
4757 we do not generate escapes for we have to generate clobbers/uses. */
4759 && DECL_BUILT_IN_CLASS (decl) == BUILT_IN_NORMAL)
4760 switch (DECL_FUNCTION_CODE (decl))
4762 /* The following functions use and clobber memory pointed to
4763 by their arguments. */
4764 case BUILT_IN_STRCPY:
4765 case BUILT_IN_STRNCPY:
4766 case BUILT_IN_BCOPY:
4767 case BUILT_IN_MEMCPY:
4768 case BUILT_IN_MEMMOVE:
4769 case BUILT_IN_MEMPCPY:
4770 case BUILT_IN_STPCPY:
4771 case BUILT_IN_STPNCPY:
4772 case BUILT_IN_STRCAT:
4773 case BUILT_IN_STRNCAT:
4774 case BUILT_IN_STRCPY_CHK:
4775 case BUILT_IN_STRNCPY_CHK:
4776 case BUILT_IN_MEMCPY_CHK:
4777 case BUILT_IN_MEMMOVE_CHK:
4778 case BUILT_IN_MEMPCPY_CHK:
4779 case BUILT_IN_STPCPY_CHK:
4780 case BUILT_IN_STPNCPY_CHK:
4781 case BUILT_IN_STRCAT_CHK:
4782 case BUILT_IN_STRNCAT_CHK:
4784 tree dest = gimple_call_arg (t, (DECL_FUNCTION_CODE (decl)
4785 == BUILT_IN_BCOPY ? 1 : 0));
4786 tree src = gimple_call_arg (t, (DECL_FUNCTION_CODE (decl)
4787 == BUILT_IN_BCOPY ? 0 : 1));
4789 struct constraint_expr *rhsp, *lhsp;
4790 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
4791 lhs = get_function_part_constraint (fi, fi_clobbers);
4792 FOR_EACH_VEC_ELT (ce_s, lhsc, i, lhsp)
4793 process_constraint (new_constraint (lhs, *lhsp));
4794 VEC_free (ce_s, heap, lhsc);
4795 get_constraint_for_ptr_offset (src, NULL_TREE, &rhsc);
4796 lhs = get_function_part_constraint (fi, fi_uses);
4797 FOR_EACH_VEC_ELT (ce_s, rhsc, i, rhsp)
4798 process_constraint (new_constraint (lhs, *rhsp));
4799 VEC_free (ce_s, heap, rhsc);
4802 /* The following function clobbers memory pointed to by
4804 case BUILT_IN_MEMSET:
4805 case BUILT_IN_MEMSET_CHK:
4807 tree dest = gimple_call_arg (t, 0);
4810 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
4811 lhs = get_function_part_constraint (fi, fi_clobbers);
4812 FOR_EACH_VEC_ELT (ce_s, lhsc, i, lhsp)
4813 process_constraint (new_constraint (lhs, *lhsp));
4814 VEC_free (ce_s, heap, lhsc);
4817 /* The following functions clobber their second and third
4819 case BUILT_IN_SINCOS:
4820 case BUILT_IN_SINCOSF:
4821 case BUILT_IN_SINCOSL:
4823 process_ipa_clobber (fi, gimple_call_arg (t, 1));
4824 process_ipa_clobber (fi, gimple_call_arg (t, 2));
4827 /* The following functions clobber their second argument. */
4828 case BUILT_IN_FREXP:
4829 case BUILT_IN_FREXPF:
4830 case BUILT_IN_FREXPL:
4831 case BUILT_IN_LGAMMA_R:
4832 case BUILT_IN_LGAMMAF_R:
4833 case BUILT_IN_LGAMMAL_R:
4834 case BUILT_IN_GAMMA_R:
4835 case BUILT_IN_GAMMAF_R:
4836 case BUILT_IN_GAMMAL_R:
4838 case BUILT_IN_MODFF:
4839 case BUILT_IN_MODFL:
4841 process_ipa_clobber (fi, gimple_call_arg (t, 1));
4844 /* The following functions clobber their third argument. */
4845 case BUILT_IN_REMQUO:
4846 case BUILT_IN_REMQUOF:
4847 case BUILT_IN_REMQUOL:
4849 process_ipa_clobber (fi, gimple_call_arg (t, 2));
4852 /* The following functions neither read nor clobber memory. */
4853 case BUILT_IN_ASSUME_ALIGNED:
4856 /* Trampolines are of no interest to us. */
4857 case BUILT_IN_INIT_TRAMPOLINE:
4858 case BUILT_IN_ADJUST_TRAMPOLINE:
4860 case BUILT_IN_VA_START:
4861 case BUILT_IN_VA_END:
4863 /* printf-style functions may have hooks to set pointers to
4864 point to somewhere into the generated string. Leave them
4865 for a later excercise... */
4867 /* Fallthru to general call handling. */;
4870 /* Parameters passed by value are used. */
4871 lhs = get_function_part_constraint (fi, fi_uses);
4872 for (i = 0; i < gimple_call_num_args (t); i++)
4874 struct constraint_expr *rhsp;
4875 tree arg = gimple_call_arg (t, i);
4877 if (TREE_CODE (arg) == SSA_NAME
4878 || is_gimple_min_invariant (arg))
4881 get_constraint_for_address_of (arg, &rhsc);
4882 FOR_EACH_VEC_ELT (ce_s, rhsc, j, rhsp)
4883 process_constraint (new_constraint (lhs, *rhsp));
4884 VEC_free (ce_s, heap, rhsc);
4887 /* Build constraints for propagating clobbers/uses along the
4889 cfi = get_fi_for_callee (t);
4890 if (cfi->id == anything_id)
4892 if (gimple_vdef (t))
4893 make_constraint_from (first_vi_for_offset (fi, fi_clobbers),
4895 make_constraint_from (first_vi_for_offset (fi, fi_uses),
4900 /* For callees without function info (that's external functions),
4901 ESCAPED is clobbered and used. */
4902 if (gimple_call_fndecl (t)
4903 && !cfi->is_fn_info)
4907 if (gimple_vdef (t))
4908 make_copy_constraint (first_vi_for_offset (fi, fi_clobbers),
4910 make_copy_constraint (first_vi_for_offset (fi, fi_uses), escaped_id);
4912 /* Also honor the call statement use/clobber info. */
4913 if ((vi = lookup_call_clobber_vi (t)) != NULL)
4914 make_copy_constraint (first_vi_for_offset (fi, fi_clobbers),
4916 if ((vi = lookup_call_use_vi (t)) != NULL)
4917 make_copy_constraint (first_vi_for_offset (fi, fi_uses),
4922 /* Otherwise the caller clobbers and uses what the callee does.
4923 ??? This should use a new complex constraint that filters
4924 local variables of the callee. */
4925 if (gimple_vdef (t))
4927 lhs = get_function_part_constraint (fi, fi_clobbers);
4928 rhs = get_function_part_constraint (cfi, fi_clobbers);
4929 process_constraint (new_constraint (lhs, rhs));
4931 lhs = get_function_part_constraint (fi, fi_uses);
4932 rhs = get_function_part_constraint (cfi, fi_uses);
4933 process_constraint (new_constraint (lhs, rhs));
4935 else if (gimple_code (t) == GIMPLE_ASM)
4937 /* ??? Ick. We can do better. */
4938 if (gimple_vdef (t))
4939 make_constraint_from (first_vi_for_offset (fi, fi_clobbers),
4941 make_constraint_from (first_vi_for_offset (fi, fi_uses),
4945 VEC_free (ce_s, heap, rhsc);
4949 /* Find the first varinfo in the same variable as START that overlaps with
4950 OFFSET. Return NULL if we can't find one. */
4953 first_vi_for_offset (varinfo_t start, unsigned HOST_WIDE_INT offset)
4955 /* If the offset is outside of the variable, bail out. */
4956 if (offset >= start->fullsize)
4959 /* If we cannot reach offset from start, lookup the first field
4960 and start from there. */
4961 if (start->offset > offset)
4962 start = lookup_vi_for_tree (start->decl);
4966 /* We may not find a variable in the field list with the actual
4967 offset when when we have glommed a structure to a variable.
4968 In that case, however, offset should still be within the size
4970 if (offset >= start->offset
4971 && (offset - start->offset) < start->size)
4980 /* Find the first varinfo in the same variable as START that overlaps with
4981 OFFSET. If there is no such varinfo the varinfo directly preceding
4982 OFFSET is returned. */
4985 first_or_preceding_vi_for_offset (varinfo_t start,
4986 unsigned HOST_WIDE_INT offset)
4988 /* If we cannot reach offset from start, lookup the first field
4989 and start from there. */
4990 if (start->offset > offset)
4991 start = lookup_vi_for_tree (start->decl);
4993 /* We may not find a variable in the field list with the actual
4994 offset when when we have glommed a structure to a variable.
4995 In that case, however, offset should still be within the size
4997 If we got beyond the offset we look for return the field
4998 directly preceding offset which may be the last field. */
5000 && offset >= start->offset
5001 && !((offset - start->offset) < start->size))
5002 start = start->next;
5008 /* This structure is used during pushing fields onto the fieldstack
5009 to track the offset of the field, since bitpos_of_field gives it
5010 relative to its immediate containing type, and we want it relative
5011 to the ultimate containing object. */
5015 /* Offset from the base of the base containing object to this field. */
5016 HOST_WIDE_INT offset;
5018 /* Size, in bits, of the field. */
5019 unsigned HOST_WIDE_INT size;
5021 unsigned has_unknown_size : 1;
5023 unsigned must_have_pointers : 1;
5025 unsigned may_have_pointers : 1;
5027 unsigned only_restrict_pointers : 1;
5029 typedef struct fieldoff fieldoff_s;
5031 DEF_VEC_O(fieldoff_s);
5032 DEF_VEC_ALLOC_O(fieldoff_s,heap);
5034 /* qsort comparison function for two fieldoff's PA and PB */
5037 fieldoff_compare (const void *pa, const void *pb)
5039 const fieldoff_s *foa = (const fieldoff_s *)pa;
5040 const fieldoff_s *fob = (const fieldoff_s *)pb;
5041 unsigned HOST_WIDE_INT foasize, fobsize;
5043 if (foa->offset < fob->offset)
5045 else if (foa->offset > fob->offset)
5048 foasize = foa->size;
5049 fobsize = fob->size;
5050 if (foasize < fobsize)
5052 else if (foasize > fobsize)
5057 /* Sort a fieldstack according to the field offset and sizes. */
5059 sort_fieldstack (VEC(fieldoff_s,heap) *fieldstack)
5061 VEC_qsort (fieldoff_s, fieldstack, fieldoff_compare);
5064 /* Return true if T is a type that can have subvars. */
5067 type_can_have_subvars (const_tree t)
5069 /* Aggregates without overlapping fields can have subvars. */
5070 return TREE_CODE (t) == RECORD_TYPE;
5073 /* Return true if V is a tree that we can have subvars for.
5074 Normally, this is any aggregate type. Also complex
5075 types which are not gimple registers can have subvars. */
5078 var_can_have_subvars (const_tree v)
5080 /* Volatile variables should never have subvars. */
5081 if (TREE_THIS_VOLATILE (v))
5084 /* Non decls or memory tags can never have subvars. */
5088 return type_can_have_subvars (TREE_TYPE (v));
5091 /* Return true if T is a type that does contain pointers. */
5094 type_must_have_pointers (tree type)
5096 if (POINTER_TYPE_P (type))
5099 if (TREE_CODE (type) == ARRAY_TYPE)
5100 return type_must_have_pointers (TREE_TYPE (type));
5102 /* A function or method can have pointers as arguments, so track
5103 those separately. */
5104 if (TREE_CODE (type) == FUNCTION_TYPE
5105 || TREE_CODE (type) == METHOD_TYPE)
5112 field_must_have_pointers (tree t)
5114 return type_must_have_pointers (TREE_TYPE (t));
5117 /* Given a TYPE, and a vector of field offsets FIELDSTACK, push all
5118 the fields of TYPE onto fieldstack, recording their offsets along
5121 OFFSET is used to keep track of the offset in this entire
5122 structure, rather than just the immediately containing structure.
5123 Returns false if the caller is supposed to handle the field we
5127 push_fields_onto_fieldstack (tree type, VEC(fieldoff_s,heap) **fieldstack,
5128 HOST_WIDE_INT offset)
5131 bool empty_p = true;
5133 if (TREE_CODE (type) != RECORD_TYPE)
5136 /* If the vector of fields is growing too big, bail out early.
5137 Callers check for VEC_length <= MAX_FIELDS_FOR_FIELD_SENSITIVE, make
5139 if (VEC_length (fieldoff_s, *fieldstack) > MAX_FIELDS_FOR_FIELD_SENSITIVE)
5142 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5143 if (TREE_CODE (field) == FIELD_DECL)
5146 HOST_WIDE_INT foff = bitpos_of_field (field);
5148 if (!var_can_have_subvars (field)
5149 || TREE_CODE (TREE_TYPE (field)) == QUAL_UNION_TYPE
5150 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5152 else if (!push_fields_onto_fieldstack
5153 (TREE_TYPE (field), fieldstack, offset + foff)
5154 && (DECL_SIZE (field)
5155 && !integer_zerop (DECL_SIZE (field))))
5156 /* Empty structures may have actual size, like in C++. So
5157 see if we didn't push any subfields and the size is
5158 nonzero, push the field onto the stack. */
5163 fieldoff_s *pair = NULL;
5164 bool has_unknown_size = false;
5165 bool must_have_pointers_p;
5167 if (!VEC_empty (fieldoff_s, *fieldstack))
5168 pair = VEC_last (fieldoff_s, *fieldstack);
5170 /* If there isn't anything at offset zero, create sth. */
5172 && offset + foff != 0)
5174 pair = VEC_safe_push (fieldoff_s, heap, *fieldstack, NULL);
5176 pair->size = offset + foff;
5177 pair->has_unknown_size = false;
5178 pair->must_have_pointers = false;
5179 pair->may_have_pointers = false;
5180 pair->only_restrict_pointers = false;
5183 if (!DECL_SIZE (field)
5184 || !host_integerp (DECL_SIZE (field), 1))
5185 has_unknown_size = true;
5187 /* If adjacent fields do not contain pointers merge them. */
5188 must_have_pointers_p = field_must_have_pointers (field);
5190 && !has_unknown_size
5191 && !must_have_pointers_p
5192 && !pair->must_have_pointers
5193 && !pair->has_unknown_size
5194 && pair->offset + (HOST_WIDE_INT)pair->size == offset + foff)
5196 pair->size += TREE_INT_CST_LOW (DECL_SIZE (field));
5200 pair = VEC_safe_push (fieldoff_s, heap, *fieldstack, NULL);
5201 pair->offset = offset + foff;
5202 pair->has_unknown_size = has_unknown_size;
5203 if (!has_unknown_size)
5204 pair->size = TREE_INT_CST_LOW (DECL_SIZE (field));
5207 pair->must_have_pointers = must_have_pointers_p;
5208 pair->may_have_pointers = true;
5209 pair->only_restrict_pointers
5210 = (!has_unknown_size
5211 && POINTER_TYPE_P (TREE_TYPE (field))
5212 && TYPE_RESTRICT (TREE_TYPE (field)));
5222 /* Count the number of arguments DECL has, and set IS_VARARGS to true
5223 if it is a varargs function. */
5226 count_num_arguments (tree decl, bool *is_varargs)
5228 unsigned int num = 0;
5231 /* Capture named arguments for K&R functions. They do not
5232 have a prototype and thus no TYPE_ARG_TYPES. */
5233 for (t = DECL_ARGUMENTS (decl); t; t = DECL_CHAIN (t))
5236 /* Check if the function has variadic arguments. */
5237 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
5238 if (TREE_VALUE (t) == void_type_node)
5246 /* Creation function node for DECL, using NAME, and return the index
5247 of the variable we've created for the function. */
5250 create_function_info_for (tree decl, const char *name)
5252 struct function *fn = DECL_STRUCT_FUNCTION (decl);
5253 varinfo_t vi, prev_vi;
5256 bool is_varargs = false;
5257 unsigned int num_args = count_num_arguments (decl, &is_varargs);
5259 /* Create the variable info. */
5261 vi = new_var_info (decl, name);
5264 vi->fullsize = fi_parm_base + num_args;
5266 vi->may_have_pointers = false;
5269 insert_vi_for_tree (vi->decl, vi);
5273 /* Create a variable for things the function clobbers and one for
5274 things the function uses. */
5276 varinfo_t clobbervi, usevi;
5277 const char *newname;
5280 asprintf (&tempname, "%s.clobber", name);
5281 newname = ggc_strdup (tempname);
5284 clobbervi = new_var_info (NULL, newname);
5285 clobbervi->offset = fi_clobbers;
5286 clobbervi->size = 1;
5287 clobbervi->fullsize = vi->fullsize;
5288 clobbervi->is_full_var = true;
5289 clobbervi->is_global_var = false;
5290 gcc_assert (prev_vi->offset < clobbervi->offset);
5291 prev_vi->next = clobbervi;
5292 prev_vi = clobbervi;
5294 asprintf (&tempname, "%s.use", name);
5295 newname = ggc_strdup (tempname);
5298 usevi = new_var_info (NULL, newname);
5299 usevi->offset = fi_uses;
5301 usevi->fullsize = vi->fullsize;
5302 usevi->is_full_var = true;
5303 usevi->is_global_var = false;
5304 gcc_assert (prev_vi->offset < usevi->offset);
5305 prev_vi->next = usevi;
5309 /* And one for the static chain. */
5310 if (fn->static_chain_decl != NULL_TREE)
5313 const char *newname;
5316 asprintf (&tempname, "%s.chain", name);
5317 newname = ggc_strdup (tempname);
5320 chainvi = new_var_info (fn->static_chain_decl, newname);
5321 chainvi->offset = fi_static_chain;
5323 chainvi->fullsize = vi->fullsize;
5324 chainvi->is_full_var = true;
5325 chainvi->is_global_var = false;
5326 gcc_assert (prev_vi->offset < chainvi->offset);
5327 prev_vi->next = chainvi;
5329 insert_vi_for_tree (fn->static_chain_decl, chainvi);
5332 /* Create a variable for the return var. */
5333 if (DECL_RESULT (decl) != NULL
5334 || !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
5337 const char *newname;
5339 tree resultdecl = decl;
5341 if (DECL_RESULT (decl))
5342 resultdecl = DECL_RESULT (decl);
5344 asprintf (&tempname, "%s.result", name);
5345 newname = ggc_strdup (tempname);
5348 resultvi = new_var_info (resultdecl, newname);
5349 resultvi->offset = fi_result;
5351 resultvi->fullsize = vi->fullsize;
5352 resultvi->is_full_var = true;
5353 if (DECL_RESULT (decl))
5354 resultvi->may_have_pointers = true;
5355 gcc_assert (prev_vi->offset < resultvi->offset);
5356 prev_vi->next = resultvi;
5358 if (DECL_RESULT (decl))
5359 insert_vi_for_tree (DECL_RESULT (decl), resultvi);
5362 /* Set up variables for each argument. */
5363 arg = DECL_ARGUMENTS (decl);
5364 for (i = 0; i < num_args; i++)
5367 const char *newname;
5369 tree argdecl = decl;
5374 asprintf (&tempname, "%s.arg%d", name, i);
5375 newname = ggc_strdup (tempname);
5378 argvi = new_var_info (argdecl, newname);
5379 argvi->offset = fi_parm_base + i;
5381 argvi->is_full_var = true;
5382 argvi->fullsize = vi->fullsize;
5384 argvi->may_have_pointers = true;
5385 gcc_assert (prev_vi->offset < argvi->offset);
5386 prev_vi->next = argvi;
5390 insert_vi_for_tree (arg, argvi);
5391 arg = DECL_CHAIN (arg);
5395 /* Add one representative for all further args. */
5399 const char *newname;
5403 asprintf (&tempname, "%s.varargs", name);
5404 newname = ggc_strdup (tempname);
5407 /* We need sth that can be pointed to for va_start. */
5408 decl = build_fake_var_decl (ptr_type_node);
5410 argvi = new_var_info (decl, newname);
5411 argvi->offset = fi_parm_base + num_args;
5413 argvi->is_full_var = true;
5414 argvi->is_heap_var = true;
5415 argvi->fullsize = vi->fullsize;
5416 gcc_assert (prev_vi->offset < argvi->offset);
5417 prev_vi->next = argvi;
5425 /* Return true if FIELDSTACK contains fields that overlap.
5426 FIELDSTACK is assumed to be sorted by offset. */
5429 check_for_overlaps (VEC (fieldoff_s,heap) *fieldstack)
5431 fieldoff_s *fo = NULL;
5433 HOST_WIDE_INT lastoffset = -1;
5435 FOR_EACH_VEC_ELT (fieldoff_s, fieldstack, i, fo)
5437 if (fo->offset == lastoffset)
5439 lastoffset = fo->offset;
5444 /* Create a varinfo structure for NAME and DECL, and add it to VARMAP.
5445 This will also create any varinfo structures necessary for fields
5449 create_variable_info_for_1 (tree decl, const char *name)
5451 varinfo_t vi, newvi;
5452 tree decl_type = TREE_TYPE (decl);
5453 tree declsize = DECL_P (decl) ? DECL_SIZE (decl) : TYPE_SIZE (decl_type);
5454 VEC (fieldoff_s,heap) *fieldstack = NULL;
5459 || !host_integerp (declsize, 1))
5461 vi = new_var_info (decl, name);
5465 vi->is_unknown_size_var = true;
5466 vi->is_full_var = true;
5467 vi->may_have_pointers = true;
5471 /* Collect field information. */
5472 if (use_field_sensitive
5473 && var_can_have_subvars (decl)
5474 /* ??? Force us to not use subfields for global initializers
5475 in IPA mode. Else we'd have to parse arbitrary initializers. */
5477 && is_global_var (decl)
5478 && DECL_INITIAL (decl)))
5480 fieldoff_s *fo = NULL;
5481 bool notokay = false;
5484 push_fields_onto_fieldstack (decl_type, &fieldstack, 0);
5486 for (i = 0; !notokay && VEC_iterate (fieldoff_s, fieldstack, i, fo); i++)
5487 if (fo->has_unknown_size
5494 /* We can't sort them if we have a field with a variable sized type,
5495 which will make notokay = true. In that case, we are going to return
5496 without creating varinfos for the fields anyway, so sorting them is a
5500 sort_fieldstack (fieldstack);
5501 /* Due to some C++ FE issues, like PR 22488, we might end up
5502 what appear to be overlapping fields even though they,
5503 in reality, do not overlap. Until the C++ FE is fixed,
5504 we will simply disable field-sensitivity for these cases. */
5505 notokay = check_for_overlaps (fieldstack);
5509 VEC_free (fieldoff_s, heap, fieldstack);
5512 /* If we didn't end up collecting sub-variables create a full
5513 variable for the decl. */
5514 if (VEC_length (fieldoff_s, fieldstack) <= 1
5515 || VEC_length (fieldoff_s, fieldstack) > MAX_FIELDS_FOR_FIELD_SENSITIVE)
5517 vi = new_var_info (decl, name);
5519 vi->may_have_pointers = true;
5520 vi->fullsize = TREE_INT_CST_LOW (declsize);
5521 vi->size = vi->fullsize;
5522 vi->is_full_var = true;
5523 VEC_free (fieldoff_s, heap, fieldstack);
5527 vi = new_var_info (decl, name);
5528 vi->fullsize = TREE_INT_CST_LOW (declsize);
5529 for (i = 0, newvi = vi;
5530 VEC_iterate (fieldoff_s, fieldstack, i, fo);
5531 ++i, newvi = newvi->next)
5533 const char *newname = "NULL";
5538 asprintf (&tempname, "%s." HOST_WIDE_INT_PRINT_DEC
5539 "+" HOST_WIDE_INT_PRINT_DEC, name, fo->offset, fo->size);
5540 newname = ggc_strdup (tempname);
5543 newvi->name = newname;
5544 newvi->offset = fo->offset;
5545 newvi->size = fo->size;
5546 newvi->fullsize = vi->fullsize;
5547 newvi->may_have_pointers = fo->may_have_pointers;
5548 newvi->only_restrict_pointers = fo->only_restrict_pointers;
5549 if (i + 1 < VEC_length (fieldoff_s, fieldstack))
5550 newvi->next = new_var_info (decl, name);
5553 VEC_free (fieldoff_s, heap, fieldstack);
5559 create_variable_info_for (tree decl, const char *name)
5561 varinfo_t vi = create_variable_info_for_1 (decl, name);
5562 unsigned int id = vi->id;
5564 insert_vi_for_tree (decl, vi);
5566 if (TREE_CODE (decl) != VAR_DECL)
5569 /* Create initial constraints for globals. */
5570 for (; vi; vi = vi->next)
5572 if (!vi->may_have_pointers
5573 || !vi->is_global_var)
5576 /* Mark global restrict qualified pointers. */
5577 if ((POINTER_TYPE_P (TREE_TYPE (decl))
5578 && TYPE_RESTRICT (TREE_TYPE (decl)))
5579 || vi->only_restrict_pointers)
5581 make_constraint_from_global_restrict (vi, "GLOBAL_RESTRICT");
5585 /* In non-IPA mode the initializer from nonlocal is all we need. */
5587 || DECL_HARD_REGISTER (decl))
5588 make_copy_constraint (vi, nonlocal_id);
5590 /* In IPA mode parse the initializer and generate proper constraints
5594 struct varpool_node *vnode = varpool_get_node (decl);
5596 /* For escaped variables initialize them from nonlocal. */
5597 if (!varpool_all_refs_explicit_p (vnode))
5598 make_copy_constraint (vi, nonlocal_id);
5600 /* If this is a global variable with an initializer and we are in
5601 IPA mode generate constraints for it. */
5602 if (DECL_INITIAL (decl))
5604 VEC (ce_s, heap) *rhsc = NULL;
5605 struct constraint_expr lhs, *rhsp;
5607 get_constraint_for_rhs (DECL_INITIAL (decl), &rhsc);
5611 FOR_EACH_VEC_ELT (ce_s, rhsc, i, rhsp)
5612 process_constraint (new_constraint (lhs, *rhsp));
5613 /* If this is a variable that escapes from the unit
5614 the initializer escapes as well. */
5615 if (!varpool_all_refs_explicit_p (vnode))
5617 lhs.var = escaped_id;
5620 FOR_EACH_VEC_ELT (ce_s, rhsc, i, rhsp)
5621 process_constraint (new_constraint (lhs, *rhsp));
5623 VEC_free (ce_s, heap, rhsc);
5631 /* Print out the points-to solution for VAR to FILE. */
5634 dump_solution_for_var (FILE *file, unsigned int var)
5636 varinfo_t vi = get_varinfo (var);
5640 /* Dump the solution for unified vars anyway, this avoids difficulties
5641 in scanning dumps in the testsuite. */
5642 fprintf (file, "%s = { ", vi->name);
5643 vi = get_varinfo (find (var));
5644 EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, i, bi)
5645 fprintf (file, "%s ", get_varinfo (i)->name);
5646 fprintf (file, "}");
5648 /* But note when the variable was unified. */
5650 fprintf (file, " same as %s", vi->name);
5652 fprintf (file, "\n");
5655 /* Print the points-to solution for VAR to stdout. */
5658 debug_solution_for_var (unsigned int var)
5660 dump_solution_for_var (stdout, var);
5663 /* Create varinfo structures for all of the variables in the
5664 function for intraprocedural mode. */
5667 intra_create_variable_infos (void)
5671 /* For each incoming pointer argument arg, create the constraint ARG
5672 = NONLOCAL or a dummy variable if it is a restrict qualified
5673 passed-by-reference argument. */
5674 for (t = DECL_ARGUMENTS (current_function_decl); t; t = DECL_CHAIN (t))
5676 varinfo_t p = get_vi_for_tree (t);
5678 /* For restrict qualified pointers to objects passed by
5679 reference build a real representative for the pointed-to object.
5680 Treat restrict qualified references the same. */
5681 if (TYPE_RESTRICT (TREE_TYPE (t))
5682 && ((DECL_BY_REFERENCE (t) && POINTER_TYPE_P (TREE_TYPE (t)))
5683 || TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
5684 && !type_contains_placeholder_p (TREE_TYPE (TREE_TYPE (t))))
5686 struct constraint_expr lhsc, rhsc;
5688 tree heapvar = build_fake_var_decl (TREE_TYPE (TREE_TYPE (t)));
5689 DECL_EXTERNAL (heapvar) = 1;
5690 vi = create_variable_info_for_1 (heapvar, "PARM_NOALIAS");
5691 insert_vi_for_tree (heapvar, vi);
5696 rhsc.type = ADDRESSOF;
5698 process_constraint (new_constraint (lhsc, rhsc));
5699 for (; vi; vi = vi->next)
5700 if (vi->may_have_pointers)
5702 if (vi->only_restrict_pointers)
5703 make_constraint_from_global_restrict (vi, "GLOBAL_RESTRICT");
5705 make_copy_constraint (vi, nonlocal_id);
5710 if (POINTER_TYPE_P (TREE_TYPE (t))
5711 && TYPE_RESTRICT (TREE_TYPE (t)))
5712 make_constraint_from_global_restrict (p, "PARM_RESTRICT");
5715 for (; p; p = p->next)
5717 if (p->only_restrict_pointers)
5718 make_constraint_from_global_restrict (p, "PARM_RESTRICT");
5719 else if (p->may_have_pointers)
5720 make_constraint_from (p, nonlocal_id);
5725 /* Add a constraint for a result decl that is passed by reference. */
5726 if (DECL_RESULT (cfun->decl)
5727 && DECL_BY_REFERENCE (DECL_RESULT (cfun->decl)))
5729 varinfo_t p, result_vi = get_vi_for_tree (DECL_RESULT (cfun->decl));
5731 for (p = result_vi; p; p = p->next)
5732 make_constraint_from (p, nonlocal_id);
5735 /* Add a constraint for the incoming static chain parameter. */
5736 if (cfun->static_chain_decl != NULL_TREE)
5738 varinfo_t p, chain_vi = get_vi_for_tree (cfun->static_chain_decl);
5740 for (p = chain_vi; p; p = p->next)
5741 make_constraint_from (p, nonlocal_id);
5745 /* Structure used to put solution bitmaps in a hashtable so they can
5746 be shared among variables with the same points-to set. */
5748 typedef struct shared_bitmap_info
5752 } *shared_bitmap_info_t;
5753 typedef const struct shared_bitmap_info *const_shared_bitmap_info_t;
5755 static htab_t shared_bitmap_table;
5757 /* Hash function for a shared_bitmap_info_t */
5760 shared_bitmap_hash (const void *p)
5762 const_shared_bitmap_info_t const bi = (const_shared_bitmap_info_t) p;
5763 return bi->hashcode;
5766 /* Equality function for two shared_bitmap_info_t's. */
5769 shared_bitmap_eq (const void *p1, const void *p2)
5771 const_shared_bitmap_info_t const sbi1 = (const_shared_bitmap_info_t) p1;
5772 const_shared_bitmap_info_t const sbi2 = (const_shared_bitmap_info_t) p2;
5773 return bitmap_equal_p (sbi1->pt_vars, sbi2->pt_vars);
5776 /* Lookup a bitmap in the shared bitmap hashtable, and return an already
5777 existing instance if there is one, NULL otherwise. */
5780 shared_bitmap_lookup (bitmap pt_vars)
5783 struct shared_bitmap_info sbi;
5785 sbi.pt_vars = pt_vars;
5786 sbi.hashcode = bitmap_hash (pt_vars);
5788 slot = htab_find_slot_with_hash (shared_bitmap_table, &sbi,
5789 sbi.hashcode, NO_INSERT);
5793 return ((shared_bitmap_info_t) *slot)->pt_vars;
5797 /* Add a bitmap to the shared bitmap hashtable. */
5800 shared_bitmap_add (bitmap pt_vars)
5803 shared_bitmap_info_t sbi = XNEW (struct shared_bitmap_info);
5805 sbi->pt_vars = pt_vars;
5806 sbi->hashcode = bitmap_hash (pt_vars);
5808 slot = htab_find_slot_with_hash (shared_bitmap_table, sbi,
5809 sbi->hashcode, INSERT);
5810 gcc_assert (!*slot);
5811 *slot = (void *) sbi;
5815 /* Set bits in INTO corresponding to the variable uids in solution set FROM. */
5818 set_uids_in_ptset (bitmap into, bitmap from, struct pt_solution *pt)
5823 EXECUTE_IF_SET_IN_BITMAP (from, 0, i, bi)
5825 varinfo_t vi = get_varinfo (i);
5827 /* The only artificial variables that are allowed in a may-alias
5828 set are heap variables. */
5829 if (vi->is_artificial_var && !vi->is_heap_var)
5832 if (TREE_CODE (vi->decl) == VAR_DECL
5833 || TREE_CODE (vi->decl) == PARM_DECL
5834 || TREE_CODE (vi->decl) == RESULT_DECL)
5836 /* If we are in IPA mode we will not recompute points-to
5837 sets after inlining so make sure they stay valid. */
5839 && !DECL_PT_UID_SET_P (vi->decl))
5840 SET_DECL_PT_UID (vi->decl, DECL_UID (vi->decl));
5842 /* Add the decl to the points-to set. Note that the points-to
5843 set contains global variables. */
5844 bitmap_set_bit (into, DECL_PT_UID (vi->decl));
5845 if (vi->is_global_var)
5846 pt->vars_contains_global = true;
5852 /* Compute the points-to solution *PT for the variable VI. */
5855 find_what_var_points_to (varinfo_t orig_vi, struct pt_solution *pt)
5859 bitmap finished_solution;
5863 memset (pt, 0, sizeof (struct pt_solution));
5865 /* This variable may have been collapsed, let's get the real
5867 vi = get_varinfo (find (orig_vi->id));
5869 /* Translate artificial variables into SSA_NAME_PTR_INFO
5871 EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, i, bi)
5873 varinfo_t vi = get_varinfo (i);
5875 if (vi->is_artificial_var)
5877 if (vi->id == nothing_id)
5879 else if (vi->id == escaped_id)
5882 pt->ipa_escaped = 1;
5886 else if (vi->id == nonlocal_id)
5888 else if (vi->is_heap_var)
5889 /* We represent heapvars in the points-to set properly. */
5891 else if (vi->id == readonly_id)
5894 else if (vi->id == anything_id
5895 || vi->id == integer_id)
5900 /* Instead of doing extra work, simply do not create
5901 elaborate points-to information for pt_anything pointers. */
5905 /* Share the final set of variables when possible. */
5906 finished_solution = BITMAP_GGC_ALLOC ();
5907 stats.points_to_sets_created++;
5909 set_uids_in_ptset (finished_solution, vi->solution, pt);
5910 result = shared_bitmap_lookup (finished_solution);
5913 shared_bitmap_add (finished_solution);
5914 pt->vars = finished_solution;
5919 bitmap_clear (finished_solution);
5923 /* Given a pointer variable P, fill in its points-to set. */
5926 find_what_p_points_to (tree p)
5928 struct ptr_info_def *pi;
5932 /* For parameters, get at the points-to set for the actual parm
5934 if (TREE_CODE (p) == SSA_NAME
5935 && (TREE_CODE (SSA_NAME_VAR (p)) == PARM_DECL
5936 || TREE_CODE (SSA_NAME_VAR (p)) == RESULT_DECL)
5937 && SSA_NAME_IS_DEFAULT_DEF (p))
5938 lookup_p = SSA_NAME_VAR (p);
5940 vi = lookup_vi_for_tree (lookup_p);
5944 pi = get_ptr_info (p);
5945 find_what_var_points_to (vi, &pi->pt);
5949 /* Query statistics for points-to solutions. */
5952 unsigned HOST_WIDE_INT pt_solution_includes_may_alias;
5953 unsigned HOST_WIDE_INT pt_solution_includes_no_alias;
5954 unsigned HOST_WIDE_INT pt_solutions_intersect_may_alias;
5955 unsigned HOST_WIDE_INT pt_solutions_intersect_no_alias;
5959 dump_pta_stats (FILE *s)
5961 fprintf (s, "\nPTA query stats:\n");
5962 fprintf (s, " pt_solution_includes: "
5963 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
5964 HOST_WIDE_INT_PRINT_DEC" queries\n",
5965 pta_stats.pt_solution_includes_no_alias,
5966 pta_stats.pt_solution_includes_no_alias
5967 + pta_stats.pt_solution_includes_may_alias);
5968 fprintf (s, " pt_solutions_intersect: "
5969 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
5970 HOST_WIDE_INT_PRINT_DEC" queries\n",
5971 pta_stats.pt_solutions_intersect_no_alias,
5972 pta_stats.pt_solutions_intersect_no_alias
5973 + pta_stats.pt_solutions_intersect_may_alias);
5977 /* Reset the points-to solution *PT to a conservative default
5978 (point to anything). */
5981 pt_solution_reset (struct pt_solution *pt)
5983 memset (pt, 0, sizeof (struct pt_solution));
5984 pt->anything = true;
5987 /* Set the points-to solution *PT to point only to the variables
5988 in VARS. VARS_CONTAINS_GLOBAL specifies whether that contains
5989 global variables and VARS_CONTAINS_RESTRICT specifies whether
5990 it contains restrict tag variables. */
5993 pt_solution_set (struct pt_solution *pt, bitmap vars, bool vars_contains_global)
5995 memset (pt, 0, sizeof (struct pt_solution));
5997 pt->vars_contains_global = vars_contains_global;
6000 /* Set the points-to solution *PT to point only to the variable VAR. */
6003 pt_solution_set_var (struct pt_solution *pt, tree var)
6005 memset (pt, 0, sizeof (struct pt_solution));
6006 pt->vars = BITMAP_GGC_ALLOC ();
6007 bitmap_set_bit (pt->vars, DECL_PT_UID (var));
6008 pt->vars_contains_global = is_global_var (var);
6011 /* Computes the union of the points-to solutions *DEST and *SRC and
6012 stores the result in *DEST. This changes the points-to bitmap
6013 of *DEST and thus may not be used if that might be shared.
6014 The points-to bitmap of *SRC and *DEST will not be shared after
6015 this function if they were not before. */
6018 pt_solution_ior_into (struct pt_solution *dest, struct pt_solution *src)
6020 dest->anything |= src->anything;
6023 pt_solution_reset (dest);
6027 dest->nonlocal |= src->nonlocal;
6028 dest->escaped |= src->escaped;
6029 dest->ipa_escaped |= src->ipa_escaped;
6030 dest->null |= src->null;
6031 dest->vars_contains_global |= src->vars_contains_global;
6036 dest->vars = BITMAP_GGC_ALLOC ();
6037 bitmap_ior_into (dest->vars, src->vars);
6040 /* Return true if the points-to solution *PT is empty. */
6043 pt_solution_empty_p (struct pt_solution *pt)
6050 && !bitmap_empty_p (pt->vars))
6053 /* If the solution includes ESCAPED, check if that is empty. */
6055 && !pt_solution_empty_p (&cfun->gimple_df->escaped))
6058 /* If the solution includes ESCAPED, check if that is empty. */
6060 && !pt_solution_empty_p (&ipa_escaped_pt))
6066 /* Return true if the points-to solution *PT only point to a single var, and
6067 return the var uid in *UID. */
6070 pt_solution_singleton_p (struct pt_solution *pt, unsigned *uid)
6072 if (pt->anything || pt->nonlocal || pt->escaped || pt->ipa_escaped
6073 || pt->null || pt->vars == NULL
6074 || !bitmap_single_bit_set_p (pt->vars))
6077 *uid = bitmap_first_set_bit (pt->vars);
6081 /* Return true if the points-to solution *PT includes global memory. */
6084 pt_solution_includes_global (struct pt_solution *pt)
6088 || pt->vars_contains_global)
6092 return pt_solution_includes_global (&cfun->gimple_df->escaped);
6094 if (pt->ipa_escaped)
6095 return pt_solution_includes_global (&ipa_escaped_pt);
6097 /* ??? This predicate is not correct for the IPA-PTA solution
6098 as we do not properly distinguish between unit escape points
6099 and global variables. */
6100 if (cfun->gimple_df->ipa_pta)
6106 /* Return true if the points-to solution *PT includes the variable
6107 declaration DECL. */
6110 pt_solution_includes_1 (struct pt_solution *pt, const_tree decl)
6116 && is_global_var (decl))
6120 && bitmap_bit_p (pt->vars, DECL_PT_UID (decl)))
6123 /* If the solution includes ESCAPED, check it. */
6125 && pt_solution_includes_1 (&cfun->gimple_df->escaped, decl))
6128 /* If the solution includes ESCAPED, check it. */
6130 && pt_solution_includes_1 (&ipa_escaped_pt, decl))
6137 pt_solution_includes (struct pt_solution *pt, const_tree decl)
6139 bool res = pt_solution_includes_1 (pt, decl);
6141 ++pta_stats.pt_solution_includes_may_alias;
6143 ++pta_stats.pt_solution_includes_no_alias;
6147 /* Return true if both points-to solutions PT1 and PT2 have a non-empty
6151 pt_solutions_intersect_1 (struct pt_solution *pt1, struct pt_solution *pt2)
6153 if (pt1->anything || pt2->anything)
6156 /* If either points to unknown global memory and the other points to
6157 any global memory they alias. */
6160 || pt2->vars_contains_global))
6162 && pt1->vars_contains_global))
6165 /* Check the escaped solution if required. */
6166 if ((pt1->escaped || pt2->escaped)
6167 && !pt_solution_empty_p (&cfun->gimple_df->escaped))
6169 /* If both point to escaped memory and that solution
6170 is not empty they alias. */
6171 if (pt1->escaped && pt2->escaped)
6174 /* If either points to escaped memory see if the escaped solution
6175 intersects with the other. */
6177 && pt_solutions_intersect_1 (&cfun->gimple_df->escaped, pt2))
6179 && pt_solutions_intersect_1 (&cfun->gimple_df->escaped, pt1)))
6183 /* Check the escaped solution if required.
6184 ??? Do we need to check the local against the IPA escaped sets? */
6185 if ((pt1->ipa_escaped || pt2->ipa_escaped)
6186 && !pt_solution_empty_p (&ipa_escaped_pt))
6188 /* If both point to escaped memory and that solution
6189 is not empty they alias. */
6190 if (pt1->ipa_escaped && pt2->ipa_escaped)
6193 /* If either points to escaped memory see if the escaped solution
6194 intersects with the other. */
6195 if ((pt1->ipa_escaped
6196 && pt_solutions_intersect_1 (&ipa_escaped_pt, pt2))
6197 || (pt2->ipa_escaped
6198 && pt_solutions_intersect_1 (&ipa_escaped_pt, pt1)))
6202 /* Now both pointers alias if their points-to solution intersects. */
6205 && bitmap_intersect_p (pt1->vars, pt2->vars));
6209 pt_solutions_intersect (struct pt_solution *pt1, struct pt_solution *pt2)
6211 bool res = pt_solutions_intersect_1 (pt1, pt2);
6213 ++pta_stats.pt_solutions_intersect_may_alias;
6215 ++pta_stats.pt_solutions_intersect_no_alias;
6220 /* Dump points-to information to OUTFILE. */
6223 dump_sa_points_to_info (FILE *outfile)
6227 fprintf (outfile, "\nPoints-to sets\n\n");
6229 if (dump_flags & TDF_STATS)
6231 fprintf (outfile, "Stats:\n");
6232 fprintf (outfile, "Total vars: %d\n", stats.total_vars);
6233 fprintf (outfile, "Non-pointer vars: %d\n",
6234 stats.nonpointer_vars);
6235 fprintf (outfile, "Statically unified vars: %d\n",
6236 stats.unified_vars_static);
6237 fprintf (outfile, "Dynamically unified vars: %d\n",
6238 stats.unified_vars_dynamic);
6239 fprintf (outfile, "Iterations: %d\n", stats.iterations);
6240 fprintf (outfile, "Number of edges: %d\n", stats.num_edges);
6241 fprintf (outfile, "Number of implicit edges: %d\n",
6242 stats.num_implicit_edges);
6245 for (i = 0; i < VEC_length (varinfo_t, varmap); i++)
6247 varinfo_t vi = get_varinfo (i);
6248 if (!vi->may_have_pointers)
6250 dump_solution_for_var (outfile, i);
6255 /* Debug points-to information to stderr. */
6258 debug_sa_points_to_info (void)
6260 dump_sa_points_to_info (stderr);
6264 /* Initialize the always-existing constraint variables for NULL
6265 ANYTHING, READONLY, and INTEGER */
6268 init_base_vars (void)
6270 struct constraint_expr lhs, rhs;
6271 varinfo_t var_anything;
6272 varinfo_t var_nothing;
6273 varinfo_t var_readonly;
6274 varinfo_t var_escaped;
6275 varinfo_t var_nonlocal;
6276 varinfo_t var_storedanything;
6277 varinfo_t var_integer;
6279 /* Create the NULL variable, used to represent that a variable points
6281 var_nothing = new_var_info (NULL_TREE, "NULL");
6282 gcc_assert (var_nothing->id == nothing_id);
6283 var_nothing->is_artificial_var = 1;
6284 var_nothing->offset = 0;
6285 var_nothing->size = ~0;
6286 var_nothing->fullsize = ~0;
6287 var_nothing->is_special_var = 1;
6288 var_nothing->may_have_pointers = 0;
6289 var_nothing->is_global_var = 0;
6291 /* Create the ANYTHING variable, used to represent that a variable
6292 points to some unknown piece of memory. */
6293 var_anything = new_var_info (NULL_TREE, "ANYTHING");
6294 gcc_assert (var_anything->id == anything_id);
6295 var_anything->is_artificial_var = 1;
6296 var_anything->size = ~0;
6297 var_anything->offset = 0;
6298 var_anything->next = NULL;
6299 var_anything->fullsize = ~0;
6300 var_anything->is_special_var = 1;
6302 /* Anything points to anything. This makes deref constraints just
6303 work in the presence of linked list and other p = *p type loops,
6304 by saying that *ANYTHING = ANYTHING. */
6306 lhs.var = anything_id;
6308 rhs.type = ADDRESSOF;
6309 rhs.var = anything_id;
6312 /* This specifically does not use process_constraint because
6313 process_constraint ignores all anything = anything constraints, since all
6314 but this one are redundant. */
6315 VEC_safe_push (constraint_t, heap, constraints, new_constraint (lhs, rhs));
6317 /* Create the READONLY variable, used to represent that a variable
6318 points to readonly memory. */
6319 var_readonly = new_var_info (NULL_TREE, "READONLY");
6320 gcc_assert (var_readonly->id == readonly_id);
6321 var_readonly->is_artificial_var = 1;
6322 var_readonly->offset = 0;
6323 var_readonly->size = ~0;
6324 var_readonly->fullsize = ~0;
6325 var_readonly->next = NULL;
6326 var_readonly->is_special_var = 1;
6328 /* readonly memory points to anything, in order to make deref
6329 easier. In reality, it points to anything the particular
6330 readonly variable can point to, but we don't track this
6333 lhs.var = readonly_id;
6335 rhs.type = ADDRESSOF;
6336 rhs.var = readonly_id; /* FIXME */
6338 process_constraint (new_constraint (lhs, rhs));
6340 /* Create the ESCAPED variable, used to represent the set of escaped
6342 var_escaped = new_var_info (NULL_TREE, "ESCAPED");
6343 gcc_assert (var_escaped->id == escaped_id);
6344 var_escaped->is_artificial_var = 1;
6345 var_escaped->offset = 0;
6346 var_escaped->size = ~0;
6347 var_escaped->fullsize = ~0;
6348 var_escaped->is_special_var = 0;
6350 /* Create the NONLOCAL variable, used to represent the set of nonlocal
6352 var_nonlocal = new_var_info (NULL_TREE, "NONLOCAL");
6353 gcc_assert (var_nonlocal->id == nonlocal_id);
6354 var_nonlocal->is_artificial_var = 1;
6355 var_nonlocal->offset = 0;
6356 var_nonlocal->size = ~0;
6357 var_nonlocal->fullsize = ~0;
6358 var_nonlocal->is_special_var = 1;
6360 /* ESCAPED = *ESCAPED, because escaped is may-deref'd at calls, etc. */
6362 lhs.var = escaped_id;
6365 rhs.var = escaped_id;
6367 process_constraint (new_constraint (lhs, rhs));
6369 /* ESCAPED = ESCAPED + UNKNOWN_OFFSET, because if a sub-field escapes the
6370 whole variable escapes. */
6372 lhs.var = escaped_id;
6375 rhs.var = escaped_id;
6376 rhs.offset = UNKNOWN_OFFSET;
6377 process_constraint (new_constraint (lhs, rhs));
6379 /* *ESCAPED = NONLOCAL. This is true because we have to assume
6380 everything pointed to by escaped points to what global memory can
6383 lhs.var = escaped_id;
6386 rhs.var = nonlocal_id;
6388 process_constraint (new_constraint (lhs, rhs));
6390 /* NONLOCAL = &NONLOCAL, NONLOCAL = &ESCAPED. This is true because
6391 global memory may point to global memory and escaped memory. */
6393 lhs.var = nonlocal_id;
6395 rhs.type = ADDRESSOF;
6396 rhs.var = nonlocal_id;
6398 process_constraint (new_constraint (lhs, rhs));
6399 rhs.type = ADDRESSOF;
6400 rhs.var = escaped_id;
6402 process_constraint (new_constraint (lhs, rhs));
6404 /* Create the STOREDANYTHING variable, used to represent the set of
6405 variables stored to *ANYTHING. */
6406 var_storedanything = new_var_info (NULL_TREE, "STOREDANYTHING");
6407 gcc_assert (var_storedanything->id == storedanything_id);
6408 var_storedanything->is_artificial_var = 1;
6409 var_storedanything->offset = 0;
6410 var_storedanything->size = ~0;
6411 var_storedanything->fullsize = ~0;
6412 var_storedanything->is_special_var = 0;
6414 /* Create the INTEGER variable, used to represent that a variable points
6415 to what an INTEGER "points to". */
6416 var_integer = new_var_info (NULL_TREE, "INTEGER");
6417 gcc_assert (var_integer->id == integer_id);
6418 var_integer->is_artificial_var = 1;
6419 var_integer->size = ~0;
6420 var_integer->fullsize = ~0;
6421 var_integer->offset = 0;
6422 var_integer->next = NULL;
6423 var_integer->is_special_var = 1;
6425 /* INTEGER = ANYTHING, because we don't know where a dereference of
6426 a random integer will point to. */
6428 lhs.var = integer_id;
6430 rhs.type = ADDRESSOF;
6431 rhs.var = anything_id;
6433 process_constraint (new_constraint (lhs, rhs));
6436 /* Initialize things necessary to perform PTA */
6439 init_alias_vars (void)
6441 use_field_sensitive = (MAX_FIELDS_FOR_FIELD_SENSITIVE > 1);
6443 bitmap_obstack_initialize (&pta_obstack);
6444 bitmap_obstack_initialize (&oldpta_obstack);
6445 bitmap_obstack_initialize (&predbitmap_obstack);
6447 constraint_pool = create_alloc_pool ("Constraint pool",
6448 sizeof (struct constraint), 30);
6449 variable_info_pool = create_alloc_pool ("Variable info pool",
6450 sizeof (struct variable_info), 30);
6451 constraints = VEC_alloc (constraint_t, heap, 8);
6452 varmap = VEC_alloc (varinfo_t, heap, 8);
6453 vi_for_tree = pointer_map_create ();
6454 call_stmt_vars = pointer_map_create ();
6456 memset (&stats, 0, sizeof (stats));
6457 shared_bitmap_table = htab_create (511, shared_bitmap_hash,
6458 shared_bitmap_eq, free);
6461 gcc_obstack_init (&fake_var_decl_obstack);
6464 /* Remove the REF and ADDRESS edges from GRAPH, as well as all the
6465 predecessor edges. */
6468 remove_preds_and_fake_succs (constraint_graph_t graph)
6472 /* Clear the implicit ref and address nodes from the successor
6474 for (i = 0; i < FIRST_REF_NODE; i++)
6476 if (graph->succs[i])
6477 bitmap_clear_range (graph->succs[i], FIRST_REF_NODE,
6478 FIRST_REF_NODE * 2);
6481 /* Free the successor list for the non-ref nodes. */
6482 for (i = FIRST_REF_NODE; i < graph->size; i++)
6484 if (graph->succs[i])
6485 BITMAP_FREE (graph->succs[i]);
6488 /* Now reallocate the size of the successor list as, and blow away
6489 the predecessor bitmaps. */
6490 graph->size = VEC_length (varinfo_t, varmap);
6491 graph->succs = XRESIZEVEC (bitmap, graph->succs, graph->size);
6493 free (graph->implicit_preds);
6494 graph->implicit_preds = NULL;
6495 free (graph->preds);
6496 graph->preds = NULL;
6497 bitmap_obstack_release (&predbitmap_obstack);
6500 /* Solve the constraint set. */
6503 solve_constraints (void)
6505 struct scc_info *si;
6509 "\nCollapsing static cycles and doing variable "
6512 init_graph (VEC_length (varinfo_t, varmap) * 2);
6515 fprintf (dump_file, "Building predecessor graph\n");
6516 build_pred_graph ();
6519 fprintf (dump_file, "Detecting pointer and location "
6521 si = perform_var_substitution (graph);
6524 fprintf (dump_file, "Rewriting constraints and unifying "
6526 rewrite_constraints (graph, si);
6528 build_succ_graph ();
6530 free_var_substitution_info (si);
6532 /* Attach complex constraints to graph nodes. */
6533 move_complex_constraints (graph);
6536 fprintf (dump_file, "Uniting pointer but not location equivalent "
6538 unite_pointer_equivalences (graph);
6541 fprintf (dump_file, "Finding indirect cycles\n");
6542 find_indirect_cycles (graph);
6544 /* Implicit nodes and predecessors are no longer necessary at this
6546 remove_preds_and_fake_succs (graph);
6548 if (dump_file && (dump_flags & TDF_GRAPH))
6550 fprintf (dump_file, "\n\n// The constraint graph before solve-graph "
6551 "in dot format:\n");
6552 dump_constraint_graph (dump_file);
6553 fprintf (dump_file, "\n\n");
6557 fprintf (dump_file, "Solving graph\n");
6559 solve_graph (graph);
6561 if (dump_file && (dump_flags & TDF_GRAPH))
6563 fprintf (dump_file, "\n\n// The constraint graph after solve-graph "
6564 "in dot format:\n");
6565 dump_constraint_graph (dump_file);
6566 fprintf (dump_file, "\n\n");
6570 dump_sa_points_to_info (dump_file);
6573 /* Create points-to sets for the current function. See the comments
6574 at the start of the file for an algorithmic overview. */
6577 compute_points_to_sets (void)
6583 timevar_push (TV_TREE_PTA);
6587 intra_create_variable_infos ();
6589 /* Now walk all statements and build the constraint set. */
6592 gimple_stmt_iterator gsi;
6594 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
6596 gimple phi = gsi_stmt (gsi);
6598 if (is_gimple_reg (gimple_phi_result (phi)))
6599 find_func_aliases (phi);
6602 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
6604 gimple stmt = gsi_stmt (gsi);
6606 find_func_aliases (stmt);
6612 fprintf (dump_file, "Points-to analysis\n\nConstraints:\n\n");
6613 dump_constraints (dump_file, 0);
6616 /* From the constraints compute the points-to sets. */
6617 solve_constraints ();
6619 /* Compute the points-to set for ESCAPED used for call-clobber analysis. */
6620 find_what_var_points_to (get_varinfo (escaped_id),
6621 &cfun->gimple_df->escaped);
6623 /* Make sure the ESCAPED solution (which is used as placeholder in
6624 other solutions) does not reference itself. This simplifies
6625 points-to solution queries. */
6626 cfun->gimple_df->escaped.escaped = 0;
6628 /* Mark escaped HEAP variables as global. */
6629 FOR_EACH_VEC_ELT (varinfo_t, varmap, i, vi)
6631 && !vi->is_global_var)
6632 DECL_EXTERNAL (vi->decl) = vi->is_global_var
6633 = pt_solution_includes (&cfun->gimple_df->escaped, vi->decl);
6635 /* Compute the points-to sets for pointer SSA_NAMEs. */
6636 for (i = 0; i < num_ssa_names; ++i)
6638 tree ptr = ssa_name (i);
6640 && POINTER_TYPE_P (TREE_TYPE (ptr)))
6641 find_what_p_points_to (ptr);
6644 /* Compute the call-used/clobbered sets. */
6647 gimple_stmt_iterator gsi;
6649 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
6651 gimple stmt = gsi_stmt (gsi);
6652 struct pt_solution *pt;
6653 if (!is_gimple_call (stmt))
6656 pt = gimple_call_use_set (stmt);
6657 if (gimple_call_flags (stmt) & ECF_CONST)
6658 memset (pt, 0, sizeof (struct pt_solution));
6659 else if ((vi = lookup_call_use_vi (stmt)) != NULL)
6661 find_what_var_points_to (vi, pt);
6662 /* Escaped (and thus nonlocal) variables are always
6663 implicitly used by calls. */
6664 /* ??? ESCAPED can be empty even though NONLOCAL
6671 /* If there is nothing special about this call then
6672 we have made everything that is used also escape. */
6673 *pt = cfun->gimple_df->escaped;
6677 pt = gimple_call_clobber_set (stmt);
6678 if (gimple_call_flags (stmt) & (ECF_CONST|ECF_PURE|ECF_NOVOPS))
6679 memset (pt, 0, sizeof (struct pt_solution));
6680 else if ((vi = lookup_call_clobber_vi (stmt)) != NULL)
6682 find_what_var_points_to (vi, pt);
6683 /* Escaped (and thus nonlocal) variables are always
6684 implicitly clobbered by calls. */
6685 /* ??? ESCAPED can be empty even though NONLOCAL
6692 /* If there is nothing special about this call then
6693 we have made everything that is used also escape. */
6694 *pt = cfun->gimple_df->escaped;
6700 timevar_pop (TV_TREE_PTA);
6704 /* Delete created points-to sets. */
6707 delete_points_to_sets (void)
6711 htab_delete (shared_bitmap_table);
6712 if (dump_file && (dump_flags & TDF_STATS))
6713 fprintf (dump_file, "Points to sets created:%d\n",
6714 stats.points_to_sets_created);
6716 pointer_map_destroy (vi_for_tree);
6717 pointer_map_destroy (call_stmt_vars);
6718 bitmap_obstack_release (&pta_obstack);
6719 VEC_free (constraint_t, heap, constraints);
6721 for (i = 0; i < graph->size; i++)
6722 VEC_free (constraint_t, heap, graph->complex[i]);
6723 free (graph->complex);
6726 free (graph->succs);
6728 free (graph->pe_rep);
6729 free (graph->indirect_cycles);
6732 VEC_free (varinfo_t, heap, varmap);
6733 free_alloc_pool (variable_info_pool);
6734 free_alloc_pool (constraint_pool);
6736 obstack_free (&fake_var_decl_obstack, NULL);
6740 /* Compute points-to information for every SSA_NAME pointer in the
6741 current function and compute the transitive closure of escaped
6742 variables to re-initialize the call-clobber states of local variables. */
6745 compute_may_aliases (void)
6747 if (cfun->gimple_df->ipa_pta)
6751 fprintf (dump_file, "\nNot re-computing points-to information "
6752 "because IPA points-to information is available.\n\n");
6754 /* But still dump what we have remaining it. */
6755 dump_alias_info (dump_file);
6757 if (dump_flags & TDF_DETAILS)
6758 dump_referenced_vars (dump_file);
6764 /* For each pointer P_i, determine the sets of variables that P_i may
6765 point-to. Compute the reachability set of escaped and call-used
6767 compute_points_to_sets ();
6769 /* Debugging dumps. */
6772 dump_alias_info (dump_file);
6774 if (dump_flags & TDF_DETAILS)
6775 dump_referenced_vars (dump_file);
6778 /* Deallocate memory used by aliasing data structures and the internal
6779 points-to solution. */
6780 delete_points_to_sets ();
6782 gcc_assert (!need_ssa_update_p (cfun));
6788 gate_tree_pta (void)
6790 return flag_tree_pta;
6793 /* A dummy pass to cause points-to information to be computed via
6794 TODO_rebuild_alias. */
6796 struct gimple_opt_pass pass_build_alias =
6801 gate_tree_pta, /* gate */
6805 0, /* static_pass_number */
6806 TV_NONE, /* tv_id */
6807 PROP_cfg | PROP_ssa, /* properties_required */
6808 0, /* properties_provided */
6809 0, /* properties_destroyed */
6810 0, /* todo_flags_start */
6811 TODO_rebuild_alias /* todo_flags_finish */
6815 /* A dummy pass to cause points-to information to be computed via
6816 TODO_rebuild_alias. */
6818 struct gimple_opt_pass pass_build_ealias =
6822 "ealias", /* name */
6823 gate_tree_pta, /* gate */
6827 0, /* static_pass_number */
6828 TV_NONE, /* tv_id */
6829 PROP_cfg | PROP_ssa, /* properties_required */
6830 0, /* properties_provided */
6831 0, /* properties_destroyed */
6832 0, /* todo_flags_start */
6833 TODO_rebuild_alias /* todo_flags_finish */
6838 /* Return true if we should execute IPA PTA. */
6844 /* Don't bother doing anything if the program has errors. */
6848 /* IPA PTA solutions for ESCAPED. */
6849 struct pt_solution ipa_escaped_pt
6850 = { true, false, false, false, false, false, NULL };
6852 /* Associate node with varinfo DATA. Worker for
6853 cgraph_for_node_and_aliases. */
6855 associate_varinfo_to_alias (struct cgraph_node *node, void *data)
6857 if (node->alias || node->thunk.thunk_p)
6858 insert_vi_for_tree (node->decl, (varinfo_t)data);
6862 /* Execute the driver for IPA PTA. */
6864 ipa_pta_execute (void)
6866 struct cgraph_node *node;
6867 struct varpool_node *var;
6874 if (dump_file && (dump_flags & TDF_DETAILS))
6876 dump_cgraph (dump_file);
6877 fprintf (dump_file, "\n");
6880 /* Build the constraints. */
6881 for (node = cgraph_nodes; node; node = node->next)
6884 /* Nodes without a body are not interesting. Especially do not
6885 visit clones at this point for now - we get duplicate decls
6886 there for inline clones at least. */
6887 if (!cgraph_function_with_gimple_body_p (node))
6890 gcc_assert (!node->clone_of);
6892 vi = create_function_info_for (node->decl,
6893 alias_get_name (node->decl));
6894 cgraph_for_node_and_aliases (node, associate_varinfo_to_alias, vi, true);
6897 /* Create constraints for global variables and their initializers. */
6898 for (var = varpool_nodes; var; var = var->next)
6903 get_vi_for_tree (var->decl);
6909 "Generating constraints for global initializers\n\n");
6910 dump_constraints (dump_file, 0);
6911 fprintf (dump_file, "\n");
6913 from = VEC_length (constraint_t, constraints);
6915 for (node = cgraph_nodes; node; node = node->next)
6917 struct function *func;
6921 /* Nodes without a body are not interesting. */
6922 if (!cgraph_function_with_gimple_body_p (node))
6928 "Generating constraints for %s", cgraph_node_name (node));
6929 if (DECL_ASSEMBLER_NAME_SET_P (node->decl))
6930 fprintf (dump_file, " (%s)",
6931 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node->decl)));
6932 fprintf (dump_file, "\n");
6935 func = DECL_STRUCT_FUNCTION (node->decl);
6936 old_func_decl = current_function_decl;
6938 current_function_decl = node->decl;
6940 /* For externally visible or attribute used annotated functions use
6941 local constraints for their arguments.
6942 For local functions we see all callers and thus do not need initial
6943 constraints for parameters. */
6944 if (node->reachable_from_other_partition
6945 || node->local.externally_visible
6948 intra_create_variable_infos ();
6950 /* We also need to make function return values escape. Nothing
6951 escapes by returning from main though. */
6952 if (!MAIN_NAME_P (DECL_NAME (node->decl)))
6955 fi = lookup_vi_for_tree (node->decl);
6956 rvi = first_vi_for_offset (fi, fi_result);
6957 if (rvi && rvi->offset == fi_result)
6959 struct constraint_expr includes;
6960 struct constraint_expr var;
6961 includes.var = escaped_id;
6962 includes.offset = 0;
6963 includes.type = SCALAR;
6967 process_constraint (new_constraint (includes, var));
6972 /* Build constriants for the function body. */
6973 FOR_EACH_BB_FN (bb, func)
6975 gimple_stmt_iterator gsi;
6977 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
6980 gimple phi = gsi_stmt (gsi);
6982 if (is_gimple_reg (gimple_phi_result (phi)))
6983 find_func_aliases (phi);
6986 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
6988 gimple stmt = gsi_stmt (gsi);
6990 find_func_aliases (stmt);
6991 find_func_clobbers (stmt);
6995 current_function_decl = old_func_decl;
7000 fprintf (dump_file, "\n");
7001 dump_constraints (dump_file, from);
7002 fprintf (dump_file, "\n");
7004 from = VEC_length (constraint_t, constraints);
7007 /* From the constraints compute the points-to sets. */
7008 solve_constraints ();
7010 /* Compute the global points-to sets for ESCAPED.
7011 ??? Note that the computed escape set is not correct
7012 for the whole unit as we fail to consider graph edges to
7013 externally visible functions. */
7014 find_what_var_points_to (get_varinfo (escaped_id), &ipa_escaped_pt);
7016 /* Make sure the ESCAPED solution (which is used as placeholder in
7017 other solutions) does not reference itself. This simplifies
7018 points-to solution queries. */
7019 ipa_escaped_pt.ipa_escaped = 0;
7021 /* Assign the points-to sets to the SSA names in the unit. */
7022 for (node = cgraph_nodes; node; node = node->next)
7025 struct function *fn;
7029 struct pt_solution uses, clobbers;
7030 struct cgraph_edge *e;
7032 /* Nodes without a body are not interesting. */
7033 if (!cgraph_function_with_gimple_body_p (node))
7036 fn = DECL_STRUCT_FUNCTION (node->decl);
7038 /* Compute the points-to sets for pointer SSA_NAMEs. */
7039 FOR_EACH_VEC_ELT (tree, fn->gimple_df->ssa_names, i, ptr)
7042 && POINTER_TYPE_P (TREE_TYPE (ptr)))
7043 find_what_p_points_to (ptr);
7046 /* Compute the call-use and call-clobber sets for all direct calls. */
7047 fi = lookup_vi_for_tree (node->decl);
7048 gcc_assert (fi->is_fn_info);
7049 find_what_var_points_to (first_vi_for_offset (fi, fi_clobbers),
7051 find_what_var_points_to (first_vi_for_offset (fi, fi_uses), &uses);
7052 for (e = node->callers; e; e = e->next_caller)
7057 *gimple_call_clobber_set (e->call_stmt) = clobbers;
7058 *gimple_call_use_set (e->call_stmt) = uses;
7061 /* Compute the call-use and call-clobber sets for indirect calls
7062 and calls to external functions. */
7063 FOR_EACH_BB_FN (bb, fn)
7065 gimple_stmt_iterator gsi;
7067 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
7069 gimple stmt = gsi_stmt (gsi);
7070 struct pt_solution *pt;
7074 if (!is_gimple_call (stmt))
7077 /* Handle direct calls to external functions. */
7078 decl = gimple_call_fndecl (stmt);
7080 && (!(fi = lookup_vi_for_tree (decl))
7081 || !fi->is_fn_info))
7083 pt = gimple_call_use_set (stmt);
7084 if (gimple_call_flags (stmt) & ECF_CONST)
7085 memset (pt, 0, sizeof (struct pt_solution));
7086 else if ((vi = lookup_call_use_vi (stmt)) != NULL)
7088 find_what_var_points_to (vi, pt);
7089 /* Escaped (and thus nonlocal) variables are always
7090 implicitly used by calls. */
7091 /* ??? ESCAPED can be empty even though NONLOCAL
7094 pt->ipa_escaped = 1;
7098 /* If there is nothing special about this call then
7099 we have made everything that is used also escape. */
7100 *pt = ipa_escaped_pt;
7104 pt = gimple_call_clobber_set (stmt);
7105 if (gimple_call_flags (stmt) & (ECF_CONST|ECF_PURE|ECF_NOVOPS))
7106 memset (pt, 0, sizeof (struct pt_solution));
7107 else if ((vi = lookup_call_clobber_vi (stmt)) != NULL)
7109 find_what_var_points_to (vi, pt);
7110 /* Escaped (and thus nonlocal) variables are always
7111 implicitly clobbered by calls. */
7112 /* ??? ESCAPED can be empty even though NONLOCAL
7115 pt->ipa_escaped = 1;
7119 /* If there is nothing special about this call then
7120 we have made everything that is used also escape. */
7121 *pt = ipa_escaped_pt;
7126 /* Handle indirect calls. */
7128 && (fi = get_fi_for_callee (stmt)))
7130 /* We need to accumulate all clobbers/uses of all possible
7132 fi = get_varinfo (find (fi->id));
7133 /* If we cannot constrain the set of functions we'll end up
7134 calling we end up using/clobbering everything. */
7135 if (bitmap_bit_p (fi->solution, anything_id)
7136 || bitmap_bit_p (fi->solution, nonlocal_id)
7137 || bitmap_bit_p (fi->solution, escaped_id))
7139 pt_solution_reset (gimple_call_clobber_set (stmt));
7140 pt_solution_reset (gimple_call_use_set (stmt));
7146 struct pt_solution *uses, *clobbers;
7148 uses = gimple_call_use_set (stmt);
7149 clobbers = gimple_call_clobber_set (stmt);
7150 memset (uses, 0, sizeof (struct pt_solution));
7151 memset (clobbers, 0, sizeof (struct pt_solution));
7152 EXECUTE_IF_SET_IN_BITMAP (fi->solution, 0, i, bi)
7154 struct pt_solution sol;
7156 vi = get_varinfo (i);
7157 if (!vi->is_fn_info)
7159 /* ??? We could be more precise here? */
7161 uses->ipa_escaped = 1;
7162 clobbers->nonlocal = 1;
7163 clobbers->ipa_escaped = 1;
7167 if (!uses->anything)
7169 find_what_var_points_to
7170 (first_vi_for_offset (vi, fi_uses), &sol);
7171 pt_solution_ior_into (uses, &sol);
7173 if (!clobbers->anything)
7175 find_what_var_points_to
7176 (first_vi_for_offset (vi, fi_clobbers), &sol);
7177 pt_solution_ior_into (clobbers, &sol);
7185 fn->gimple_df->ipa_pta = true;
7188 delete_points_to_sets ();
7195 struct simple_ipa_opt_pass pass_ipa_pta =
7200 gate_ipa_pta, /* gate */
7201 ipa_pta_execute, /* execute */
7204 0, /* static_pass_number */
7205 TV_IPA_PTA, /* tv_id */
7206 0, /* properties_required */
7207 0, /* properties_provided */
7208 0, /* properties_destroyed */
7209 0, /* todo_flags_start */
7210 TODO_update_ssa /* todo_flags_finish */