1 /* Tree based points-to analysis
2 Copyright (C) 2005-2013 Free Software Foundation, Inc.
3 Contributed by Daniel Berlin <dberlin@dberlin.org>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
29 #include "basic-block.h"
31 #include "stor-layout.h"
33 #include "pointer-set.h"
34 #include "hash-table.h"
35 #include "tree-ssa-alias.h"
36 #include "internal-fn.h"
37 #include "gimple-expr.h"
40 #include "gimple-iterator.h"
41 #include "gimple-ssa.h"
43 #include "stringpool.h"
44 #include "tree-ssanames.h"
45 #include "tree-into-ssa.h"
48 #include "tree-inline.h"
49 #include "diagnostic-core.h"
51 #include "tree-pass.h"
52 #include "alloc-pool.h"
53 #include "splay-tree.h"
57 /* The idea behind this analyzer is to generate set constraints from the
58 program, then solve the resulting constraints in order to generate the
61 Set constraints are a way of modeling program analysis problems that
62 involve sets. They consist of an inclusion constraint language,
63 describing the variables (each variable is a set) and operations that
64 are involved on the variables, and a set of rules that derive facts
65 from these operations. To solve a system of set constraints, you derive
66 all possible facts under the rules, which gives you the correct sets
69 See "Efficient Field-sensitive pointer analysis for C" by "David
70 J. Pearce and Paul H. J. Kelly and Chris Hankin, at
71 http://citeseer.ist.psu.edu/pearce04efficient.html
73 Also see "Ultra-fast Aliasing Analysis using CLA: A Million Lines
74 of C Code in a Second" by ""Nevin Heintze and Olivier Tardieu" at
75 http://citeseer.ist.psu.edu/heintze01ultrafast.html
77 There are three types of real constraint expressions, DEREF,
78 ADDRESSOF, and SCALAR. Each constraint expression consists
79 of a constraint type, a variable, and an offset.
81 SCALAR is a constraint expression type used to represent x, whether
82 it appears on the LHS or the RHS of a statement.
83 DEREF is a constraint expression type used to represent *x, whether
84 it appears on the LHS or the RHS of a statement.
85 ADDRESSOF is a constraint expression used to represent &x, whether
86 it appears on the LHS or the RHS of a statement.
88 Each pointer variable in the program is assigned an integer id, and
89 each field of a structure variable is assigned an integer id as well.
91 Structure variables are linked to their list of fields through a "next
92 field" in each variable that points to the next field in offset
94 Each variable for a structure field has
96 1. "size", that tells the size in bits of that field.
97 2. "fullsize, that tells the size in bits of the entire structure.
98 3. "offset", that tells the offset in bits from the beginning of the
99 structure to this field.
111 foo.a -> id 1, size 32, offset 0, fullsize 64, next foo.b
112 foo.b -> id 2, size 32, offset 32, fullsize 64, next NULL
113 bar -> id 3, size 32, offset 0, fullsize 32, next NULL
116 In order to solve the system of set constraints, the following is
119 1. Each constraint variable x has a solution set associated with it,
122 2. Constraints are separated into direct, copy, and complex.
123 Direct constraints are ADDRESSOF constraints that require no extra
124 processing, such as P = &Q
125 Copy constraints are those of the form P = Q.
126 Complex constraints are all the constraints involving dereferences
127 and offsets (including offsetted copies).
129 3. All direct constraints of the form P = &Q are processed, such
130 that Q is added to Sol(P)
132 4. All complex constraints for a given constraint variable are stored in a
133 linked list attached to that variable's node.
135 5. A directed graph is built out of the copy constraints. Each
136 constraint variable is a node in the graph, and an edge from
137 Q to P is added for each copy constraint of the form P = Q
139 6. The graph is then walked, and solution sets are
140 propagated along the copy edges, such that an edge from Q to P
141 causes Sol(P) <- Sol(P) union Sol(Q).
143 7. As we visit each node, all complex constraints associated with
144 that node are processed by adding appropriate copy edges to the graph, or the
145 appropriate variables to the solution set.
147 8. The process of walking the graph is iterated until no solution
150 Prior to walking the graph in steps 6 and 7, We perform static
151 cycle elimination on the constraint graph, as well
152 as off-line variable substitution.
154 TODO: Adding offsets to pointer-to-structures can be handled (IE not punted
155 on and turned into anything), but isn't. You can just see what offset
156 inside the pointed-to struct it's going to access.
158 TODO: Constant bounded arrays can be handled as if they were structs of the
159 same number of elements.
161 TODO: Modeling heap and incoming pointers becomes much better if we
162 add fields to them as we discover them, which we could do.
164 TODO: We could handle unions, but to be honest, it's probably not
165 worth the pain or slowdown. */
167 /* IPA-PTA optimizations possible.
169 When the indirect function called is ANYTHING we can add disambiguation
170 based on the function signatures (or simply the parameter count which
171 is the varinfo size). We also do not need to consider functions that
172 do not have their address taken.
174 The is_global_var bit which marks escape points is overly conservative
175 in IPA mode. Split it to is_escape_point and is_global_var - only
176 externally visible globals are escape points in IPA mode. This is
177 also needed to fix the pt_solution_includes_global predicate
178 (and thus ptr_deref_may_alias_global_p).
180 The way we introduce DECL_PT_UID to avoid fixing up all points-to
181 sets in the translation unit when we copy a DECL during inlining
182 pessimizes precision. The advantage is that the DECL_PT_UID keeps
183 compile-time and memory usage overhead low - the points-to sets
184 do not grow or get unshared as they would during a fixup phase.
185 An alternative solution is to delay IPA PTA until after all
186 inlining transformations have been applied.
188 The way we propagate clobber/use information isn't optimized.
189 It should use a new complex constraint that properly filters
190 out local variables of the callee (though that would make
191 the sets invalid after inlining). OTOH we might as well
192 admit defeat to WHOPR and simply do all the clobber/use analysis
193 and propagation after PTA finished but before we threw away
194 points-to information for memory variables. WHOPR and PTA
195 do not play along well anyway - the whole constraint solving
196 would need to be done in WPA phase and it will be very interesting
197 to apply the results to local SSA names during LTRANS phase.
199 We probably should compute a per-function unit-ESCAPE solution
200 propagating it simply like the clobber / uses solutions. The
201 solution can go alongside the non-IPA espaced solution and be
202 used to query which vars escape the unit through a function.
204 We never put function decls in points-to sets so we do not
205 keep the set of called functions for indirect calls.
207 And probably more. */
209 static bool use_field_sensitive
= true;
210 static int in_ipa_mode
= 0;
212 /* Used for predecessor bitmaps. */
213 static bitmap_obstack predbitmap_obstack
;
215 /* Used for points-to sets. */
216 static bitmap_obstack pta_obstack
;
218 /* Used for oldsolution members of variables. */
219 static bitmap_obstack oldpta_obstack
;
221 /* Used for per-solver-iteration bitmaps. */
222 static bitmap_obstack iteration_obstack
;
224 static unsigned int create_variable_info_for (tree
, const char *);
225 typedef struct constraint_graph
*constraint_graph_t
;
226 static void unify_nodes (constraint_graph_t
, unsigned int, unsigned int, bool);
229 typedef struct constraint
*constraint_t
;
232 #define EXECUTE_IF_IN_NONNULL_BITMAP(a, b, c, d) \
234 EXECUTE_IF_SET_IN_BITMAP (a, b, c, d)
236 static struct constraint_stats
238 unsigned int total_vars
;
239 unsigned int nonpointer_vars
;
240 unsigned int unified_vars_static
;
241 unsigned int unified_vars_dynamic
;
242 unsigned int iterations
;
243 unsigned int num_edges
;
244 unsigned int num_implicit_edges
;
245 unsigned int points_to_sets_created
;
250 /* ID of this variable */
253 /* True if this is a variable created by the constraint analysis, such as
254 heap variables and constraints we had to break up. */
255 unsigned int is_artificial_var
: 1;
257 /* True if this is a special variable whose solution set should not be
259 unsigned int is_special_var
: 1;
261 /* True for variables whose size is not known or variable. */
262 unsigned int is_unknown_size_var
: 1;
264 /* True for (sub-)fields that represent a whole variable. */
265 unsigned int is_full_var
: 1;
267 /* True if this is a heap variable. */
268 unsigned int is_heap_var
: 1;
270 /* True if this field may contain pointers. */
271 unsigned int may_have_pointers
: 1;
273 /* True if this field has only restrict qualified pointers. */
274 unsigned int only_restrict_pointers
: 1;
276 /* True if this represents a global variable. */
277 unsigned int is_global_var
: 1;
279 /* True if this represents a IPA function info. */
280 unsigned int is_fn_info
: 1;
282 /* The ID of the variable for the next field in this structure
283 or zero for the last field in this structure. */
286 /* The ID of the variable for the first field in this structure. */
289 /* Offset of this variable, in bits, from the base variable */
290 unsigned HOST_WIDE_INT offset
;
292 /* Size of the variable, in bits. */
293 unsigned HOST_WIDE_INT size
;
295 /* Full size of the base variable, in bits. */
296 unsigned HOST_WIDE_INT fullsize
;
298 /* Name of this variable */
301 /* Tree that this variable is associated with. */
304 /* Points-to set for this variable. */
307 /* Old points-to set for this variable. */
310 typedef struct variable_info
*varinfo_t
;
312 static varinfo_t
first_vi_for_offset (varinfo_t
, unsigned HOST_WIDE_INT
);
313 static varinfo_t
first_or_preceding_vi_for_offset (varinfo_t
,
314 unsigned HOST_WIDE_INT
);
315 static varinfo_t
lookup_vi_for_tree (tree
);
316 static inline bool type_can_have_subvars (const_tree
);
318 /* Pool of variable info structures. */
319 static alloc_pool variable_info_pool
;
321 /* Map varinfo to final pt_solution. */
322 static pointer_map_t
*final_solutions
;
323 struct obstack final_solutions_obstack
;
325 /* Table of variable info structures for constraint variables.
326 Indexed directly by variable info id. */
327 static vec
<varinfo_t
> varmap
;
329 /* Return the varmap element N */
331 static inline varinfo_t
332 get_varinfo (unsigned int n
)
337 /* Return the next variable in the list of sub-variables of VI
338 or NULL if VI is the last sub-variable. */
340 static inline varinfo_t
341 vi_next (varinfo_t vi
)
343 return get_varinfo (vi
->next
);
346 /* Static IDs for the special variables. Variable ID zero is unused
347 and used as terminator for the sub-variable chain. */
348 enum { nothing_id
= 1, anything_id
= 2, readonly_id
= 3,
349 escaped_id
= 4, nonlocal_id
= 5,
350 storedanything_id
= 6, integer_id
= 7 };
352 /* Return a new variable info structure consisting for a variable
353 named NAME, and using constraint graph node NODE. Append it
354 to the vector of variable info structures. */
357 new_var_info (tree t
, const char *name
)
359 unsigned index
= varmap
.length ();
360 varinfo_t ret
= (varinfo_t
) pool_alloc (variable_info_pool
);
365 /* Vars without decl are artificial and do not have sub-variables. */
366 ret
->is_artificial_var
= (t
== NULL_TREE
);
367 ret
->is_special_var
= false;
368 ret
->is_unknown_size_var
= false;
369 ret
->is_full_var
= (t
== NULL_TREE
);
370 ret
->is_heap_var
= false;
371 ret
->may_have_pointers
= true;
372 ret
->only_restrict_pointers
= false;
373 ret
->is_global_var
= (t
== NULL_TREE
);
374 ret
->is_fn_info
= false;
376 ret
->is_global_var
= (is_global_var (t
)
377 /* We have to treat even local register variables
379 || (TREE_CODE (t
) == VAR_DECL
380 && DECL_HARD_REGISTER (t
)));
381 ret
->solution
= BITMAP_ALLOC (&pta_obstack
);
382 ret
->oldsolution
= NULL
;
388 varmap
.safe_push (ret
);
394 /* A map mapping call statements to per-stmt variables for uses
395 and clobbers specific to the call. */
396 static struct pointer_map_t
*call_stmt_vars
;
398 /* Lookup or create the variable for the call statement CALL. */
401 get_call_vi (gimple call
)
406 slot_p
= pointer_map_insert (call_stmt_vars
, call
);
408 return (varinfo_t
) *slot_p
;
410 vi
= new_var_info (NULL_TREE
, "CALLUSED");
414 vi
->is_full_var
= true;
416 vi2
= new_var_info (NULL_TREE
, "CALLCLOBBERED");
420 vi2
->is_full_var
= true;
424 *slot_p
= (void *) vi
;
428 /* Lookup the variable for the call statement CALL representing
429 the uses. Returns NULL if there is nothing special about this call. */
432 lookup_call_use_vi (gimple call
)
436 slot_p
= pointer_map_contains (call_stmt_vars
, call
);
438 return (varinfo_t
) *slot_p
;
443 /* Lookup the variable for the call statement CALL representing
444 the clobbers. Returns NULL if there is nothing special about this call. */
447 lookup_call_clobber_vi (gimple call
)
449 varinfo_t uses
= lookup_call_use_vi (call
);
453 return vi_next (uses
);
456 /* Lookup or create the variable for the call statement CALL representing
460 get_call_use_vi (gimple call
)
462 return get_call_vi (call
);
465 /* Lookup or create the variable for the call statement CALL representing
468 static varinfo_t ATTRIBUTE_UNUSED
469 get_call_clobber_vi (gimple call
)
471 return vi_next (get_call_vi (call
));
475 typedef enum {SCALAR
, DEREF
, ADDRESSOF
} constraint_expr_type
;
477 /* An expression that appears in a constraint. */
479 struct constraint_expr
481 /* Constraint type. */
482 constraint_expr_type type
;
484 /* Variable we are referring to in the constraint. */
487 /* Offset, in bits, of this constraint from the beginning of
488 variables it ends up referring to.
490 IOW, in a deref constraint, we would deref, get the result set,
491 then add OFFSET to each member. */
492 HOST_WIDE_INT offset
;
495 /* Use 0x8000... as special unknown offset. */
496 #define UNKNOWN_OFFSET HOST_WIDE_INT_MIN
498 typedef struct constraint_expr ce_s
;
499 static void get_constraint_for_1 (tree
, vec
<ce_s
> *, bool, bool);
500 static void get_constraint_for (tree
, vec
<ce_s
> *);
501 static void get_constraint_for_rhs (tree
, vec
<ce_s
> *);
502 static void do_deref (vec
<ce_s
> *);
504 /* Our set constraints are made up of two constraint expressions, one
507 As described in the introduction, our set constraints each represent an
508 operation between set valued variables.
512 struct constraint_expr lhs
;
513 struct constraint_expr rhs
;
516 /* List of constraints that we use to build the constraint graph from. */
518 static vec
<constraint_t
> constraints
;
519 static alloc_pool constraint_pool
;
521 /* The constraint graph is represented as an array of bitmaps
522 containing successor nodes. */
524 struct constraint_graph
526 /* Size of this graph, which may be different than the number of
527 nodes in the variable map. */
530 /* Explicit successors of each node. */
533 /* Implicit predecessors of each node (Used for variable
535 bitmap
*implicit_preds
;
537 /* Explicit predecessors of each node (Used for variable substitution). */
540 /* Indirect cycle representatives, or -1 if the node has no indirect
542 int *indirect_cycles
;
544 /* Representative node for a node. rep[a] == a unless the node has
548 /* Equivalence class representative for a label. This is used for
549 variable substitution. */
552 /* Pointer equivalence label for a node. All nodes with the same
553 pointer equivalence label can be unified together at some point
554 (either during constraint optimization or after the constraint
558 /* Pointer equivalence representative for a label. This is used to
559 handle nodes that are pointer equivalent but not location
560 equivalent. We can unite these once the addressof constraints
561 are transformed into initial points-to sets. */
564 /* Pointer equivalence label for each node, used during variable
566 unsigned int *pointer_label
;
568 /* Location equivalence label for each node, used during location
569 equivalence finding. */
570 unsigned int *loc_label
;
572 /* Pointed-by set for each node, used during location equivalence
573 finding. This is pointed-by rather than pointed-to, because it
574 is constructed using the predecessor graph. */
577 /* Points to sets for pointer equivalence. This is *not* the actual
578 points-to sets for nodes. */
581 /* Bitmap of nodes where the bit is set if the node is a direct
582 node. Used for variable substitution. */
583 sbitmap direct_nodes
;
585 /* Bitmap of nodes where the bit is set if the node is address
586 taken. Used for variable substitution. */
587 bitmap address_taken
;
589 /* Vector of complex constraints for each graph node. Complex
590 constraints are those involving dereferences or offsets that are
592 vec
<constraint_t
> *complex;
595 static constraint_graph_t graph
;
597 /* During variable substitution and the offline version of indirect
598 cycle finding, we create nodes to represent dereferences and
599 address taken constraints. These represent where these start and
601 #define FIRST_REF_NODE (varmap).length ()
602 #define LAST_REF_NODE (FIRST_REF_NODE + (FIRST_REF_NODE - 1))
604 /* Return the representative node for NODE, if NODE has been unioned
606 This function performs path compression along the way to finding
607 the representative. */
610 find (unsigned int node
)
612 gcc_checking_assert (node
< graph
->size
);
613 if (graph
->rep
[node
] != node
)
614 return graph
->rep
[node
] = find (graph
->rep
[node
]);
618 /* Union the TO and FROM nodes to the TO nodes.
619 Note that at some point in the future, we may want to do
620 union-by-rank, in which case we are going to have to return the
621 node we unified to. */
624 unite (unsigned int to
, unsigned int from
)
626 gcc_checking_assert (to
< graph
->size
&& from
< graph
->size
);
627 if (to
!= from
&& graph
->rep
[from
] != to
)
629 graph
->rep
[from
] = to
;
635 /* Create a new constraint consisting of LHS and RHS expressions. */
638 new_constraint (const struct constraint_expr lhs
,
639 const struct constraint_expr rhs
)
641 constraint_t ret
= (constraint_t
) pool_alloc (constraint_pool
);
647 /* Print out constraint C to FILE. */
650 dump_constraint (FILE *file
, constraint_t c
)
652 if (c
->lhs
.type
== ADDRESSOF
)
654 else if (c
->lhs
.type
== DEREF
)
656 fprintf (file
, "%s", get_varinfo (c
->lhs
.var
)->name
);
657 if (c
->lhs
.offset
== UNKNOWN_OFFSET
)
658 fprintf (file
, " + UNKNOWN");
659 else if (c
->lhs
.offset
!= 0)
660 fprintf (file
, " + " HOST_WIDE_INT_PRINT_DEC
, c
->lhs
.offset
);
661 fprintf (file
, " = ");
662 if (c
->rhs
.type
== ADDRESSOF
)
664 else if (c
->rhs
.type
== DEREF
)
666 fprintf (file
, "%s", get_varinfo (c
->rhs
.var
)->name
);
667 if (c
->rhs
.offset
== UNKNOWN_OFFSET
)
668 fprintf (file
, " + UNKNOWN");
669 else if (c
->rhs
.offset
!= 0)
670 fprintf (file
, " + " HOST_WIDE_INT_PRINT_DEC
, c
->rhs
.offset
);
674 void debug_constraint (constraint_t
);
675 void debug_constraints (void);
676 void debug_constraint_graph (void);
677 void debug_solution_for_var (unsigned int);
678 void debug_sa_points_to_info (void);
680 /* Print out constraint C to stderr. */
683 debug_constraint (constraint_t c
)
685 dump_constraint (stderr
, c
);
686 fprintf (stderr
, "\n");
689 /* Print out all constraints to FILE */
692 dump_constraints (FILE *file
, int from
)
696 for (i
= from
; constraints
.iterate (i
, &c
); i
++)
699 dump_constraint (file
, c
);
700 fprintf (file
, "\n");
704 /* Print out all constraints to stderr. */
707 debug_constraints (void)
709 dump_constraints (stderr
, 0);
712 /* Print the constraint graph in dot format. */
715 dump_constraint_graph (FILE *file
)
719 /* Only print the graph if it has already been initialized: */
723 /* Prints the header of the dot file: */
724 fprintf (file
, "strict digraph {\n");
725 fprintf (file
, " node [\n shape = box\n ]\n");
726 fprintf (file
, " edge [\n fontsize = \"12\"\n ]\n");
727 fprintf (file
, "\n // List of nodes and complex constraints in "
728 "the constraint graph:\n");
730 /* The next lines print the nodes in the graph together with the
731 complex constraints attached to them. */
732 for (i
= 1; i
< graph
->size
; i
++)
734 if (i
== FIRST_REF_NODE
)
738 if (i
< FIRST_REF_NODE
)
739 fprintf (file
, "\"%s\"", get_varinfo (i
)->name
);
741 fprintf (file
, "\"*%s\"", get_varinfo (i
- FIRST_REF_NODE
)->name
);
742 if (graph
->complex[i
].exists ())
746 fprintf (file
, " [label=\"\\N\\n");
747 for (j
= 0; graph
->complex[i
].iterate (j
, &c
); ++j
)
749 dump_constraint (file
, c
);
750 fprintf (file
, "\\l");
752 fprintf (file
, "\"]");
754 fprintf (file
, ";\n");
757 /* Go over the edges. */
758 fprintf (file
, "\n // Edges in the constraint graph:\n");
759 for (i
= 1; i
< graph
->size
; i
++)
765 EXECUTE_IF_IN_NONNULL_BITMAP (graph
->succs
[i
], 0, j
, bi
)
767 unsigned to
= find (j
);
770 if (i
< FIRST_REF_NODE
)
771 fprintf (file
, "\"%s\"", get_varinfo (i
)->name
);
773 fprintf (file
, "\"*%s\"", get_varinfo (i
- FIRST_REF_NODE
)->name
);
774 fprintf (file
, " -> ");
775 if (to
< FIRST_REF_NODE
)
776 fprintf (file
, "\"%s\"", get_varinfo (to
)->name
);
778 fprintf (file
, "\"*%s\"", get_varinfo (to
- FIRST_REF_NODE
)->name
);
779 fprintf (file
, ";\n");
783 /* Prints the tail of the dot file. */
784 fprintf (file
, "}\n");
787 /* Print out the constraint graph to stderr. */
790 debug_constraint_graph (void)
792 dump_constraint_graph (stderr
);
797 The solver is a simple worklist solver, that works on the following
800 sbitmap changed_nodes = all zeroes;
802 For each node that is not already collapsed:
804 set bit in changed nodes
806 while (changed_count > 0)
808 compute topological ordering for constraint graph
810 find and collapse cycles in the constraint graph (updating
811 changed if necessary)
813 for each node (n) in the graph in topological order:
816 Process each complex constraint associated with the node,
817 updating changed if necessary.
819 For each outgoing edge from n, propagate the solution from n to
820 the destination of the edge, updating changed as necessary.
824 /* Return true if two constraint expressions A and B are equal. */
827 constraint_expr_equal (struct constraint_expr a
, struct constraint_expr b
)
829 return a
.type
== b
.type
&& a
.var
== b
.var
&& a
.offset
== b
.offset
;
832 /* Return true if constraint expression A is less than constraint expression
833 B. This is just arbitrary, but consistent, in order to give them an
837 constraint_expr_less (struct constraint_expr a
, struct constraint_expr b
)
839 if (a
.type
== b
.type
)
842 return a
.offset
< b
.offset
;
844 return a
.var
< b
.var
;
847 return a
.type
< b
.type
;
850 /* Return true if constraint A is less than constraint B. This is just
851 arbitrary, but consistent, in order to give them an ordering. */
854 constraint_less (const constraint_t
&a
, const constraint_t
&b
)
856 if (constraint_expr_less (a
->lhs
, b
->lhs
))
858 else if (constraint_expr_less (b
->lhs
, a
->lhs
))
861 return constraint_expr_less (a
->rhs
, b
->rhs
);
864 /* Return true if two constraints A and B are equal. */
867 constraint_equal (struct constraint a
, struct constraint b
)
869 return constraint_expr_equal (a
.lhs
, b
.lhs
)
870 && constraint_expr_equal (a
.rhs
, b
.rhs
);
874 /* Find a constraint LOOKFOR in the sorted constraint vector VEC */
877 constraint_vec_find (vec
<constraint_t
> vec
,
878 struct constraint lookfor
)
886 place
= vec
.lower_bound (&lookfor
, constraint_less
);
887 if (place
>= vec
.length ())
890 if (!constraint_equal (*found
, lookfor
))
895 /* Union two constraint vectors, TO and FROM. Put the result in TO. */
898 constraint_set_union (vec
<constraint_t
> *to
,
899 vec
<constraint_t
> *from
)
904 FOR_EACH_VEC_ELT (*from
, i
, c
)
906 if (constraint_vec_find (*to
, *c
) == NULL
)
908 unsigned int place
= to
->lower_bound (c
, constraint_less
);
909 to
->safe_insert (place
, c
);
914 /* Expands the solution in SET to all sub-fields of variables included. */
917 solution_set_expand (bitmap set
)
922 /* In a first pass expand to the head of the variables we need to
923 add all sub-fields off. This avoids quadratic behavior. */
924 EXECUTE_IF_SET_IN_BITMAP (set
, 0, j
, bi
)
926 varinfo_t v
= get_varinfo (j
);
927 if (v
->is_artificial_var
930 bitmap_set_bit (set
, v
->head
);
933 /* In the second pass now expand all head variables with subfields. */
934 EXECUTE_IF_SET_IN_BITMAP (set
, 0, j
, bi
)
936 varinfo_t v
= get_varinfo (j
);
937 if (v
->is_artificial_var
941 for (v
= vi_next (v
); v
!= NULL
; v
= vi_next (v
))
942 bitmap_set_bit (set
, v
->id
);
946 /* Union solution sets TO and FROM, and add INC to each member of FROM in the
950 set_union_with_increment (bitmap to
, bitmap from
, HOST_WIDE_INT inc
)
952 bool changed
= false;
956 /* If the solution of FROM contains anything it is good enough to transfer
958 if (bitmap_bit_p (from
, anything_id
))
959 return bitmap_set_bit (to
, anything_id
);
961 /* For zero offset simply union the solution into the destination. */
963 return bitmap_ior_into (to
, from
);
965 /* If the offset is unknown we have to expand the solution to
967 if (inc
== UNKNOWN_OFFSET
)
969 bitmap tmp
= BITMAP_ALLOC (&iteration_obstack
);
970 bitmap_copy (tmp
, from
);
971 solution_set_expand (tmp
);
972 changed
|= bitmap_ior_into (to
, tmp
);
977 /* For non-zero offset union the offsetted solution into the destination. */
978 EXECUTE_IF_SET_IN_BITMAP (from
, 0, i
, bi
)
980 varinfo_t vi
= get_varinfo (i
);
982 /* If this is a variable with just one field just set its bit
984 if (vi
->is_artificial_var
985 || vi
->is_unknown_size_var
987 changed
|= bitmap_set_bit (to
, i
);
990 unsigned HOST_WIDE_INT fieldoffset
= vi
->offset
+ inc
;
992 /* If the offset makes the pointer point to before the
993 variable use offset zero for the field lookup. */
995 && fieldoffset
> vi
->offset
)
998 vi
= first_or_preceding_vi_for_offset (vi
, fieldoffset
);
1000 changed
|= bitmap_set_bit (to
, vi
->id
);
1001 /* If the result is not exactly at fieldoffset include the next
1002 field as well. See get_constraint_for_ptr_offset for more
1004 if (vi
->offset
!= fieldoffset
1006 changed
|= bitmap_set_bit (to
, vi
->next
);
1013 /* Insert constraint C into the list of complex constraints for graph
1017 insert_into_complex (constraint_graph_t graph
,
1018 unsigned int var
, constraint_t c
)
1020 vec
<constraint_t
> complex = graph
->complex[var
];
1021 unsigned int place
= complex.lower_bound (c
, constraint_less
);
1023 /* Only insert constraints that do not already exist. */
1024 if (place
>= complex.length ()
1025 || !constraint_equal (*c
, *complex[place
]))
1026 graph
->complex[var
].safe_insert (place
, c
);
1030 /* Condense two variable nodes into a single variable node, by moving
1031 all associated info from SRC to TO. */
1034 merge_node_constraints (constraint_graph_t graph
, unsigned int to
,
1040 gcc_checking_assert (find (from
) == to
);
1042 /* Move all complex constraints from src node into to node */
1043 FOR_EACH_VEC_ELT (graph
->complex[from
], i
, c
)
1045 /* In complex constraints for node src, we may have either
1046 a = *src, and *src = a, or an offseted constraint which are
1047 always added to the rhs node's constraints. */
1049 if (c
->rhs
.type
== DEREF
)
1051 else if (c
->lhs
.type
== DEREF
)
1056 constraint_set_union (&graph
->complex[to
], &graph
->complex[from
]);
1057 graph
->complex[from
].release ();
1061 /* Remove edges involving NODE from GRAPH. */
1064 clear_edges_for_node (constraint_graph_t graph
, unsigned int node
)
1066 if (graph
->succs
[node
])
1067 BITMAP_FREE (graph
->succs
[node
]);
1070 /* Merge GRAPH nodes FROM and TO into node TO. */
1073 merge_graph_nodes (constraint_graph_t graph
, unsigned int to
,
1076 if (graph
->indirect_cycles
[from
] != -1)
1078 /* If we have indirect cycles with the from node, and we have
1079 none on the to node, the to node has indirect cycles from the
1080 from node now that they are unified.
1081 If indirect cycles exist on both, unify the nodes that they
1082 are in a cycle with, since we know they are in a cycle with
1084 if (graph
->indirect_cycles
[to
] == -1)
1085 graph
->indirect_cycles
[to
] = graph
->indirect_cycles
[from
];
1088 /* Merge all the successor edges. */
1089 if (graph
->succs
[from
])
1091 if (!graph
->succs
[to
])
1092 graph
->succs
[to
] = BITMAP_ALLOC (&pta_obstack
);
1093 bitmap_ior_into (graph
->succs
[to
],
1094 graph
->succs
[from
]);
1097 clear_edges_for_node (graph
, from
);
1101 /* Add an indirect graph edge to GRAPH, going from TO to FROM if
1102 it doesn't exist in the graph already. */
1105 add_implicit_graph_edge (constraint_graph_t graph
, unsigned int to
,
1111 if (!graph
->implicit_preds
[to
])
1112 graph
->implicit_preds
[to
] = BITMAP_ALLOC (&predbitmap_obstack
);
1114 if (bitmap_set_bit (graph
->implicit_preds
[to
], from
))
1115 stats
.num_implicit_edges
++;
1118 /* Add a predecessor graph edge to GRAPH, going from TO to FROM if
1119 it doesn't exist in the graph already.
1120 Return false if the edge already existed, true otherwise. */
1123 add_pred_graph_edge (constraint_graph_t graph
, unsigned int to
,
1126 if (!graph
->preds
[to
])
1127 graph
->preds
[to
] = BITMAP_ALLOC (&predbitmap_obstack
);
1128 bitmap_set_bit (graph
->preds
[to
], from
);
1131 /* Add a graph edge to GRAPH, going from FROM to TO if
1132 it doesn't exist in the graph already.
1133 Return false if the edge already existed, true otherwise. */
1136 add_graph_edge (constraint_graph_t graph
, unsigned int to
,
1147 if (!graph
->succs
[from
])
1148 graph
->succs
[from
] = BITMAP_ALLOC (&pta_obstack
);
1149 if (bitmap_set_bit (graph
->succs
[from
], to
))
1152 if (to
< FIRST_REF_NODE
&& from
< FIRST_REF_NODE
)
1160 /* Initialize the constraint graph structure to contain SIZE nodes. */
1163 init_graph (unsigned int size
)
1167 graph
= XCNEW (struct constraint_graph
);
1169 graph
->succs
= XCNEWVEC (bitmap
, graph
->size
);
1170 graph
->indirect_cycles
= XNEWVEC (int, graph
->size
);
1171 graph
->rep
= XNEWVEC (unsigned int, graph
->size
);
1172 /* ??? Macros do not support template types with multiple arguments,
1173 so we use a typedef to work around it. */
1174 typedef vec
<constraint_t
> vec_constraint_t_heap
;
1175 graph
->complex = XCNEWVEC (vec_constraint_t_heap
, size
);
1176 graph
->pe
= XCNEWVEC (unsigned int, graph
->size
);
1177 graph
->pe_rep
= XNEWVEC (int, graph
->size
);
1179 for (j
= 0; j
< graph
->size
; j
++)
1182 graph
->pe_rep
[j
] = -1;
1183 graph
->indirect_cycles
[j
] = -1;
1187 /* Build the constraint graph, adding only predecessor edges right now. */
1190 build_pred_graph (void)
1196 graph
->implicit_preds
= XCNEWVEC (bitmap
, graph
->size
);
1197 graph
->preds
= XCNEWVEC (bitmap
, graph
->size
);
1198 graph
->pointer_label
= XCNEWVEC (unsigned int, graph
->size
);
1199 graph
->loc_label
= XCNEWVEC (unsigned int, graph
->size
);
1200 graph
->pointed_by
= XCNEWVEC (bitmap
, graph
->size
);
1201 graph
->points_to
= XCNEWVEC (bitmap
, graph
->size
);
1202 graph
->eq_rep
= XNEWVEC (int, graph
->size
);
1203 graph
->direct_nodes
= sbitmap_alloc (graph
->size
);
1204 graph
->address_taken
= BITMAP_ALLOC (&predbitmap_obstack
);
1205 bitmap_clear (graph
->direct_nodes
);
1207 for (j
= 1; j
< FIRST_REF_NODE
; j
++)
1209 if (!get_varinfo (j
)->is_special_var
)
1210 bitmap_set_bit (graph
->direct_nodes
, j
);
1213 for (j
= 0; j
< graph
->size
; j
++)
1214 graph
->eq_rep
[j
] = -1;
1216 for (j
= 0; j
< varmap
.length (); j
++)
1217 graph
->indirect_cycles
[j
] = -1;
1219 FOR_EACH_VEC_ELT (constraints
, i
, c
)
1221 struct constraint_expr lhs
= c
->lhs
;
1222 struct constraint_expr rhs
= c
->rhs
;
1223 unsigned int lhsvar
= lhs
.var
;
1224 unsigned int rhsvar
= rhs
.var
;
1226 if (lhs
.type
== DEREF
)
1229 if (rhs
.offset
== 0 && lhs
.offset
== 0 && rhs
.type
== SCALAR
)
1230 add_pred_graph_edge (graph
, FIRST_REF_NODE
+ lhsvar
, rhsvar
);
1232 else if (rhs
.type
== DEREF
)
1235 if (rhs
.offset
== 0 && lhs
.offset
== 0 && lhs
.type
== SCALAR
)
1236 add_pred_graph_edge (graph
, lhsvar
, FIRST_REF_NODE
+ rhsvar
);
1238 bitmap_clear_bit (graph
->direct_nodes
, lhsvar
);
1240 else if (rhs
.type
== ADDRESSOF
)
1245 if (graph
->points_to
[lhsvar
] == NULL
)
1246 graph
->points_to
[lhsvar
] = BITMAP_ALLOC (&predbitmap_obstack
);
1247 bitmap_set_bit (graph
->points_to
[lhsvar
], rhsvar
);
1249 if (graph
->pointed_by
[rhsvar
] == NULL
)
1250 graph
->pointed_by
[rhsvar
] = BITMAP_ALLOC (&predbitmap_obstack
);
1251 bitmap_set_bit (graph
->pointed_by
[rhsvar
], lhsvar
);
1253 /* Implicitly, *x = y */
1254 add_implicit_graph_edge (graph
, FIRST_REF_NODE
+ lhsvar
, rhsvar
);
1256 /* All related variables are no longer direct nodes. */
1257 bitmap_clear_bit (graph
->direct_nodes
, rhsvar
);
1258 v
= get_varinfo (rhsvar
);
1259 if (!v
->is_full_var
)
1261 v
= get_varinfo (v
->head
);
1264 bitmap_clear_bit (graph
->direct_nodes
, v
->id
);
1269 bitmap_set_bit (graph
->address_taken
, rhsvar
);
1271 else if (lhsvar
> anything_id
1272 && lhsvar
!= rhsvar
&& lhs
.offset
== 0 && rhs
.offset
== 0)
1275 add_pred_graph_edge (graph
, lhsvar
, rhsvar
);
1276 /* Implicitly, *x = *y */
1277 add_implicit_graph_edge (graph
, FIRST_REF_NODE
+ lhsvar
,
1278 FIRST_REF_NODE
+ rhsvar
);
1280 else if (lhs
.offset
!= 0 || rhs
.offset
!= 0)
1282 if (rhs
.offset
!= 0)
1283 bitmap_clear_bit (graph
->direct_nodes
, lhs
.var
);
1284 else if (lhs
.offset
!= 0)
1285 bitmap_clear_bit (graph
->direct_nodes
, rhs
.var
);
1290 /* Build the constraint graph, adding successor edges. */
1293 build_succ_graph (void)
1298 FOR_EACH_VEC_ELT (constraints
, i
, c
)
1300 struct constraint_expr lhs
;
1301 struct constraint_expr rhs
;
1302 unsigned int lhsvar
;
1303 unsigned int rhsvar
;
1310 lhsvar
= find (lhs
.var
);
1311 rhsvar
= find (rhs
.var
);
1313 if (lhs
.type
== DEREF
)
1315 if (rhs
.offset
== 0 && lhs
.offset
== 0 && rhs
.type
== SCALAR
)
1316 add_graph_edge (graph
, FIRST_REF_NODE
+ lhsvar
, rhsvar
);
1318 else if (rhs
.type
== DEREF
)
1320 if (rhs
.offset
== 0 && lhs
.offset
== 0 && lhs
.type
== SCALAR
)
1321 add_graph_edge (graph
, lhsvar
, FIRST_REF_NODE
+ rhsvar
);
1323 else if (rhs
.type
== ADDRESSOF
)
1326 gcc_checking_assert (find (rhs
.var
) == rhs
.var
);
1327 bitmap_set_bit (get_varinfo (lhsvar
)->solution
, rhsvar
);
1329 else if (lhsvar
> anything_id
1330 && lhsvar
!= rhsvar
&& lhs
.offset
== 0 && rhs
.offset
== 0)
1332 add_graph_edge (graph
, lhsvar
, rhsvar
);
1336 /* Add edges from STOREDANYTHING to all non-direct nodes that can
1337 receive pointers. */
1338 t
= find (storedanything_id
);
1339 for (i
= integer_id
+ 1; i
< FIRST_REF_NODE
; ++i
)
1341 if (!bitmap_bit_p (graph
->direct_nodes
, i
)
1342 && get_varinfo (i
)->may_have_pointers
)
1343 add_graph_edge (graph
, find (i
), t
);
1346 /* Everything stored to ANYTHING also potentially escapes. */
1347 add_graph_edge (graph
, find (escaped_id
), t
);
1351 /* Changed variables on the last iteration. */
1352 static bitmap changed
;
1354 /* Strongly Connected Component visitation info. */
1361 unsigned int *node_mapping
;
1363 vec
<unsigned> scc_stack
;
1367 /* Recursive routine to find strongly connected components in GRAPH.
1368 SI is the SCC info to store the information in, and N is the id of current
1369 graph node we are processing.
1371 This is Tarjan's strongly connected component finding algorithm, as
1372 modified by Nuutila to keep only non-root nodes on the stack.
1373 The algorithm can be found in "On finding the strongly connected
1374 connected components in a directed graph" by Esko Nuutila and Eljas
1375 Soisalon-Soininen, in Information Processing Letters volume 49,
1376 number 1, pages 9-14. */
1379 scc_visit (constraint_graph_t graph
, struct scc_info
*si
, unsigned int n
)
1383 unsigned int my_dfs
;
1385 bitmap_set_bit (si
->visited
, n
);
1386 si
->dfs
[n
] = si
->current_index
++;
1387 my_dfs
= si
->dfs
[n
];
1389 /* Visit all the successors. */
1390 EXECUTE_IF_IN_NONNULL_BITMAP (graph
->succs
[n
], 0, i
, bi
)
1394 if (i
> LAST_REF_NODE
)
1398 if (bitmap_bit_p (si
->deleted
, w
))
1401 if (!bitmap_bit_p (si
->visited
, w
))
1402 scc_visit (graph
, si
, w
);
1404 unsigned int t
= find (w
);
1405 gcc_checking_assert (find (n
) == n
);
1406 if (si
->dfs
[t
] < si
->dfs
[n
])
1407 si
->dfs
[n
] = si
->dfs
[t
];
1410 /* See if any components have been identified. */
1411 if (si
->dfs
[n
] == my_dfs
)
1413 if (si
->scc_stack
.length () > 0
1414 && si
->dfs
[si
->scc_stack
.last ()] >= my_dfs
)
1416 bitmap scc
= BITMAP_ALLOC (NULL
);
1417 unsigned int lowest_node
;
1420 bitmap_set_bit (scc
, n
);
1422 while (si
->scc_stack
.length () != 0
1423 && si
->dfs
[si
->scc_stack
.last ()] >= my_dfs
)
1425 unsigned int w
= si
->scc_stack
.pop ();
1427 bitmap_set_bit (scc
, w
);
1430 lowest_node
= bitmap_first_set_bit (scc
);
1431 gcc_assert (lowest_node
< FIRST_REF_NODE
);
1433 /* Collapse the SCC nodes into a single node, and mark the
1435 EXECUTE_IF_SET_IN_BITMAP (scc
, 0, i
, bi
)
1437 if (i
< FIRST_REF_NODE
)
1439 if (unite (lowest_node
, i
))
1440 unify_nodes (graph
, lowest_node
, i
, false);
1444 unite (lowest_node
, i
);
1445 graph
->indirect_cycles
[i
- FIRST_REF_NODE
] = lowest_node
;
1449 bitmap_set_bit (si
->deleted
, n
);
1452 si
->scc_stack
.safe_push (n
);
1455 /* Unify node FROM into node TO, updating the changed count if
1456 necessary when UPDATE_CHANGED is true. */
1459 unify_nodes (constraint_graph_t graph
, unsigned int to
, unsigned int from
,
1460 bool update_changed
)
1462 gcc_checking_assert (to
!= from
&& find (to
) == to
);
1464 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1465 fprintf (dump_file
, "Unifying %s to %s\n",
1466 get_varinfo (from
)->name
,
1467 get_varinfo (to
)->name
);
1470 stats
.unified_vars_dynamic
++;
1472 stats
.unified_vars_static
++;
1474 merge_graph_nodes (graph
, to
, from
);
1475 merge_node_constraints (graph
, to
, from
);
1477 /* Mark TO as changed if FROM was changed. If TO was already marked
1478 as changed, decrease the changed count. */
1481 && bitmap_clear_bit (changed
, from
))
1482 bitmap_set_bit (changed
, to
);
1483 varinfo_t fromvi
= get_varinfo (from
);
1484 if (fromvi
->solution
)
1486 /* If the solution changes because of the merging, we need to mark
1487 the variable as changed. */
1488 varinfo_t tovi
= get_varinfo (to
);
1489 if (bitmap_ior_into (tovi
->solution
, fromvi
->solution
))
1492 bitmap_set_bit (changed
, to
);
1495 BITMAP_FREE (fromvi
->solution
);
1496 if (fromvi
->oldsolution
)
1497 BITMAP_FREE (fromvi
->oldsolution
);
1499 if (stats
.iterations
> 0
1500 && tovi
->oldsolution
)
1501 BITMAP_FREE (tovi
->oldsolution
);
1503 if (graph
->succs
[to
])
1504 bitmap_clear_bit (graph
->succs
[to
], to
);
1507 /* Information needed to compute the topological ordering of a graph. */
1511 /* sbitmap of visited nodes. */
1513 /* Array that stores the topological order of the graph, *in
1515 vec
<unsigned> topo_order
;
1519 /* Initialize and return a topological info structure. */
1521 static struct topo_info
*
1522 init_topo_info (void)
1524 size_t size
= graph
->size
;
1525 struct topo_info
*ti
= XNEW (struct topo_info
);
1526 ti
->visited
= sbitmap_alloc (size
);
1527 bitmap_clear (ti
->visited
);
1528 ti
->topo_order
.create (1);
1533 /* Free the topological sort info pointed to by TI. */
1536 free_topo_info (struct topo_info
*ti
)
1538 sbitmap_free (ti
->visited
);
1539 ti
->topo_order
.release ();
1543 /* Visit the graph in topological order, and store the order in the
1544 topo_info structure. */
1547 topo_visit (constraint_graph_t graph
, struct topo_info
*ti
,
1553 bitmap_set_bit (ti
->visited
, n
);
1555 if (graph
->succs
[n
])
1556 EXECUTE_IF_SET_IN_BITMAP (graph
->succs
[n
], 0, j
, bi
)
1558 if (!bitmap_bit_p (ti
->visited
, j
))
1559 topo_visit (graph
, ti
, j
);
1562 ti
->topo_order
.safe_push (n
);
1565 /* Process a constraint C that represents x = *(y + off), using DELTA as the
1566 starting solution for y. */
1569 do_sd_constraint (constraint_graph_t graph
, constraint_t c
,
1572 unsigned int lhs
= c
->lhs
.var
;
1574 bitmap sol
= get_varinfo (lhs
)->solution
;
1577 HOST_WIDE_INT roffset
= c
->rhs
.offset
;
1579 /* Our IL does not allow this. */
1580 gcc_checking_assert (c
->lhs
.offset
== 0);
1582 /* If the solution of Y contains anything it is good enough to transfer
1584 if (bitmap_bit_p (delta
, anything_id
))
1586 flag
|= bitmap_set_bit (sol
, anything_id
);
1590 /* If we do not know at with offset the rhs is dereferenced compute
1591 the reachability set of DELTA, conservatively assuming it is
1592 dereferenced at all valid offsets. */
1593 if (roffset
== UNKNOWN_OFFSET
)
1595 solution_set_expand (delta
);
1596 /* No further offset processing is necessary. */
1600 /* For each variable j in delta (Sol(y)), add
1601 an edge in the graph from j to x, and union Sol(j) into Sol(x). */
1602 EXECUTE_IF_SET_IN_BITMAP (delta
, 0, j
, bi
)
1604 varinfo_t v
= get_varinfo (j
);
1605 HOST_WIDE_INT fieldoffset
= v
->offset
+ roffset
;
1609 fieldoffset
= v
->offset
;
1610 else if (roffset
!= 0)
1611 v
= first_vi_for_offset (v
, fieldoffset
);
1612 /* If the access is outside of the variable we can ignore it. */
1620 /* Adding edges from the special vars is pointless.
1621 They don't have sets that can change. */
1622 if (get_varinfo (t
)->is_special_var
)
1623 flag
|= bitmap_ior_into (sol
, get_varinfo (t
)->solution
);
1624 /* Merging the solution from ESCAPED needlessly increases
1625 the set. Use ESCAPED as representative instead. */
1626 else if (v
->id
== escaped_id
)
1627 flag
|= bitmap_set_bit (sol
, escaped_id
);
1628 else if (v
->may_have_pointers
1629 && add_graph_edge (graph
, lhs
, t
))
1630 flag
|= bitmap_ior_into (sol
, get_varinfo (t
)->solution
);
1632 /* If the variable is not exactly at the requested offset
1633 we have to include the next one. */
1634 if (v
->offset
== (unsigned HOST_WIDE_INT
)fieldoffset
1639 fieldoffset
= v
->offset
;
1645 /* If the LHS solution changed, mark the var as changed. */
1648 get_varinfo (lhs
)->solution
= sol
;
1649 bitmap_set_bit (changed
, lhs
);
1653 /* Process a constraint C that represents *(x + off) = y using DELTA
1654 as the starting solution for x. */
1657 do_ds_constraint (constraint_t c
, bitmap delta
)
1659 unsigned int rhs
= c
->rhs
.var
;
1660 bitmap sol
= get_varinfo (rhs
)->solution
;
1663 HOST_WIDE_INT loff
= c
->lhs
.offset
;
1664 bool escaped_p
= false;
1666 /* Our IL does not allow this. */
1667 gcc_checking_assert (c
->rhs
.offset
== 0);
1669 /* If the solution of y contains ANYTHING simply use the ANYTHING
1670 solution. This avoids needlessly increasing the points-to sets. */
1671 if (bitmap_bit_p (sol
, anything_id
))
1672 sol
= get_varinfo (find (anything_id
))->solution
;
1674 /* If the solution for x contains ANYTHING we have to merge the
1675 solution of y into all pointer variables which we do via
1677 if (bitmap_bit_p (delta
, anything_id
))
1679 unsigned t
= find (storedanything_id
);
1680 if (add_graph_edge (graph
, t
, rhs
))
1682 if (bitmap_ior_into (get_varinfo (t
)->solution
, sol
))
1683 bitmap_set_bit (changed
, t
);
1688 /* If we do not know at with offset the rhs is dereferenced compute
1689 the reachability set of DELTA, conservatively assuming it is
1690 dereferenced at all valid offsets. */
1691 if (loff
== UNKNOWN_OFFSET
)
1693 solution_set_expand (delta
);
1697 /* For each member j of delta (Sol(x)), add an edge from y to j and
1698 union Sol(y) into Sol(j) */
1699 EXECUTE_IF_SET_IN_BITMAP (delta
, 0, j
, bi
)
1701 varinfo_t v
= get_varinfo (j
);
1703 HOST_WIDE_INT fieldoffset
= v
->offset
+ loff
;
1706 fieldoffset
= v
->offset
;
1708 v
= first_vi_for_offset (v
, fieldoffset
);
1709 /* If the access is outside of the variable we can ignore it. */
1715 if (v
->may_have_pointers
)
1717 /* If v is a global variable then this is an escape point. */
1718 if (v
->is_global_var
1721 t
= find (escaped_id
);
1722 if (add_graph_edge (graph
, t
, rhs
)
1723 && bitmap_ior_into (get_varinfo (t
)->solution
, sol
))
1724 bitmap_set_bit (changed
, t
);
1725 /* Enough to let rhs escape once. */
1729 if (v
->is_special_var
)
1733 if (add_graph_edge (graph
, t
, rhs
)
1734 && bitmap_ior_into (get_varinfo (t
)->solution
, sol
))
1735 bitmap_set_bit (changed
, t
);
1738 /* If the variable is not exactly at the requested offset
1739 we have to include the next one. */
1740 if (v
->offset
== (unsigned HOST_WIDE_INT
)fieldoffset
1745 fieldoffset
= v
->offset
;
1751 /* Handle a non-simple (simple meaning requires no iteration),
1752 constraint (IE *x = &y, x = *y, *x = y, and x = y with offsets involved). */
1755 do_complex_constraint (constraint_graph_t graph
, constraint_t c
, bitmap delta
)
1757 if (c
->lhs
.type
== DEREF
)
1759 if (c
->rhs
.type
== ADDRESSOF
)
1766 do_ds_constraint (c
, delta
);
1769 else if (c
->rhs
.type
== DEREF
)
1772 if (!(get_varinfo (c
->lhs
.var
)->is_special_var
))
1773 do_sd_constraint (graph
, c
, delta
);
1781 gcc_checking_assert (c
->rhs
.type
== SCALAR
&& c
->lhs
.type
== SCALAR
);
1782 solution
= get_varinfo (c
->rhs
.var
)->solution
;
1783 tmp
= get_varinfo (c
->lhs
.var
)->solution
;
1785 flag
= set_union_with_increment (tmp
, solution
, c
->rhs
.offset
);
1788 bitmap_set_bit (changed
, c
->lhs
.var
);
1792 /* Initialize and return a new SCC info structure. */
1794 static struct scc_info
*
1795 init_scc_info (size_t size
)
1797 struct scc_info
*si
= XNEW (struct scc_info
);
1800 si
->current_index
= 0;
1801 si
->visited
= sbitmap_alloc (size
);
1802 bitmap_clear (si
->visited
);
1803 si
->deleted
= sbitmap_alloc (size
);
1804 bitmap_clear (si
->deleted
);
1805 si
->node_mapping
= XNEWVEC (unsigned int, size
);
1806 si
->dfs
= XCNEWVEC (unsigned int, size
);
1808 for (i
= 0; i
< size
; i
++)
1809 si
->node_mapping
[i
] = i
;
1811 si
->scc_stack
.create (1);
1815 /* Free an SCC info structure pointed to by SI */
1818 free_scc_info (struct scc_info
*si
)
1820 sbitmap_free (si
->visited
);
1821 sbitmap_free (si
->deleted
);
1822 free (si
->node_mapping
);
1824 si
->scc_stack
.release ();
1829 /* Find indirect cycles in GRAPH that occur, using strongly connected
1830 components, and note them in the indirect cycles map.
1832 This technique comes from Ben Hardekopf and Calvin Lin,
1833 "It Pays to be Lazy: Fast and Accurate Pointer Analysis for Millions of
1834 Lines of Code", submitted to PLDI 2007. */
1837 find_indirect_cycles (constraint_graph_t graph
)
1840 unsigned int size
= graph
->size
;
1841 struct scc_info
*si
= init_scc_info (size
);
1843 for (i
= 0; i
< MIN (LAST_REF_NODE
, size
); i
++ )
1844 if (!bitmap_bit_p (si
->visited
, i
) && find (i
) == i
)
1845 scc_visit (graph
, si
, i
);
1850 /* Compute a topological ordering for GRAPH, and store the result in the
1851 topo_info structure TI. */
1854 compute_topo_order (constraint_graph_t graph
,
1855 struct topo_info
*ti
)
1858 unsigned int size
= graph
->size
;
1860 for (i
= 0; i
!= size
; ++i
)
1861 if (!bitmap_bit_p (ti
->visited
, i
) && find (i
) == i
)
1862 topo_visit (graph
, ti
, i
);
1865 /* Structure used to for hash value numbering of pointer equivalence
1868 typedef struct equiv_class_label
1871 unsigned int equivalence_class
;
1873 } *equiv_class_label_t
;
1874 typedef const struct equiv_class_label
*const_equiv_class_label_t
;
1876 /* Equiv_class_label hashtable helpers. */
1878 struct equiv_class_hasher
: typed_free_remove
<equiv_class_label
>
1880 typedef equiv_class_label value_type
;
1881 typedef equiv_class_label compare_type
;
1882 static inline hashval_t
hash (const value_type
*);
1883 static inline bool equal (const value_type
*, const compare_type
*);
1886 /* Hash function for a equiv_class_label_t */
1889 equiv_class_hasher::hash (const value_type
*ecl
)
1891 return ecl
->hashcode
;
1894 /* Equality function for two equiv_class_label_t's. */
1897 equiv_class_hasher::equal (const value_type
*eql1
, const compare_type
*eql2
)
1899 return (eql1
->hashcode
== eql2
->hashcode
1900 && bitmap_equal_p (eql1
->labels
, eql2
->labels
));
1903 /* A hashtable for mapping a bitmap of labels->pointer equivalence
1905 static hash_table
<equiv_class_hasher
> pointer_equiv_class_table
;
1907 /* A hashtable for mapping a bitmap of labels->location equivalence
1909 static hash_table
<equiv_class_hasher
> location_equiv_class_table
;
1911 /* Lookup a equivalence class in TABLE by the bitmap of LABELS with
1912 hash HAS it contains. Sets *REF_LABELS to the bitmap LABELS
1913 is equivalent to. */
1915 static equiv_class_label
*
1916 equiv_class_lookup_or_add (hash_table
<equiv_class_hasher
> table
, bitmap labels
)
1918 equiv_class_label
**slot
;
1919 equiv_class_label ecl
;
1921 ecl
.labels
= labels
;
1922 ecl
.hashcode
= bitmap_hash (labels
);
1923 slot
= table
.find_slot_with_hash (&ecl
, ecl
.hashcode
, INSERT
);
1926 *slot
= XNEW (struct equiv_class_label
);
1927 (*slot
)->labels
= labels
;
1928 (*slot
)->hashcode
= ecl
.hashcode
;
1929 (*slot
)->equivalence_class
= 0;
1935 /* Perform offline variable substitution.
1937 This is a worst case quadratic time way of identifying variables
1938 that must have equivalent points-to sets, including those caused by
1939 static cycles, and single entry subgraphs, in the constraint graph.
1941 The technique is described in "Exploiting Pointer and Location
1942 Equivalence to Optimize Pointer Analysis. In the 14th International
1943 Static Analysis Symposium (SAS), August 2007." It is known as the
1944 "HU" algorithm, and is equivalent to value numbering the collapsed
1945 constraint graph including evaluating unions.
1947 The general method of finding equivalence classes is as follows:
1948 Add fake nodes (REF nodes) and edges for *a = b and a = *b constraints.
1949 Initialize all non-REF nodes to be direct nodes.
1950 For each constraint a = a U {b}, we set pts(a) = pts(a) u {fresh
1952 For each constraint containing the dereference, we also do the same
1955 We then compute SCC's in the graph and unify nodes in the same SCC,
1958 For each non-collapsed node x:
1959 Visit all unvisited explicit incoming edges.
1960 Ignoring all non-pointers, set pts(x) = Union of pts(a) for y
1962 Lookup the equivalence class for pts(x).
1963 If we found one, equivalence_class(x) = found class.
1964 Otherwise, equivalence_class(x) = new class, and new_class is
1965 added to the lookup table.
1967 All direct nodes with the same equivalence class can be replaced
1968 with a single representative node.
1969 All unlabeled nodes (label == 0) are not pointers and all edges
1970 involving them can be eliminated.
1971 We perform these optimizations during rewrite_constraints
1973 In addition to pointer equivalence class finding, we also perform
1974 location equivalence class finding. This is the set of variables
1975 that always appear together in points-to sets. We use this to
1976 compress the size of the points-to sets. */
1978 /* Current maximum pointer equivalence class id. */
1979 static int pointer_equiv_class
;
1981 /* Current maximum location equivalence class id. */
1982 static int location_equiv_class
;
1984 /* Recursive routine to find strongly connected components in GRAPH,
1985 and label it's nodes with DFS numbers. */
1988 condense_visit (constraint_graph_t graph
, struct scc_info
*si
, unsigned int n
)
1992 unsigned int my_dfs
;
1994 gcc_checking_assert (si
->node_mapping
[n
] == n
);
1995 bitmap_set_bit (si
->visited
, n
);
1996 si
->dfs
[n
] = si
->current_index
++;
1997 my_dfs
= si
->dfs
[n
];
1999 /* Visit all the successors. */
2000 EXECUTE_IF_IN_NONNULL_BITMAP (graph
->preds
[n
], 0, i
, bi
)
2002 unsigned int w
= si
->node_mapping
[i
];
2004 if (bitmap_bit_p (si
->deleted
, w
))
2007 if (!bitmap_bit_p (si
->visited
, w
))
2008 condense_visit (graph
, si
, w
);
2010 unsigned int t
= si
->node_mapping
[w
];
2011 gcc_checking_assert (si
->node_mapping
[n
] == n
);
2012 if (si
->dfs
[t
] < si
->dfs
[n
])
2013 si
->dfs
[n
] = si
->dfs
[t
];
2016 /* Visit all the implicit predecessors. */
2017 EXECUTE_IF_IN_NONNULL_BITMAP (graph
->implicit_preds
[n
], 0, i
, bi
)
2019 unsigned int w
= si
->node_mapping
[i
];
2021 if (bitmap_bit_p (si
->deleted
, w
))
2024 if (!bitmap_bit_p (si
->visited
, w
))
2025 condense_visit (graph
, si
, w
);
2027 unsigned int t
= si
->node_mapping
[w
];
2028 gcc_assert (si
->node_mapping
[n
] == n
);
2029 if (si
->dfs
[t
] < si
->dfs
[n
])
2030 si
->dfs
[n
] = si
->dfs
[t
];
2033 /* See if any components have been identified. */
2034 if (si
->dfs
[n
] == my_dfs
)
2036 while (si
->scc_stack
.length () != 0
2037 && si
->dfs
[si
->scc_stack
.last ()] >= my_dfs
)
2039 unsigned int w
= si
->scc_stack
.pop ();
2040 si
->node_mapping
[w
] = n
;
2042 if (!bitmap_bit_p (graph
->direct_nodes
, w
))
2043 bitmap_clear_bit (graph
->direct_nodes
, n
);
2045 /* Unify our nodes. */
2046 if (graph
->preds
[w
])
2048 if (!graph
->preds
[n
])
2049 graph
->preds
[n
] = BITMAP_ALLOC (&predbitmap_obstack
);
2050 bitmap_ior_into (graph
->preds
[n
], graph
->preds
[w
]);
2052 if (graph
->implicit_preds
[w
])
2054 if (!graph
->implicit_preds
[n
])
2055 graph
->implicit_preds
[n
] = BITMAP_ALLOC (&predbitmap_obstack
);
2056 bitmap_ior_into (graph
->implicit_preds
[n
],
2057 graph
->implicit_preds
[w
]);
2059 if (graph
->points_to
[w
])
2061 if (!graph
->points_to
[n
])
2062 graph
->points_to
[n
] = BITMAP_ALLOC (&predbitmap_obstack
);
2063 bitmap_ior_into (graph
->points_to
[n
],
2064 graph
->points_to
[w
]);
2067 bitmap_set_bit (si
->deleted
, n
);
2070 si
->scc_stack
.safe_push (n
);
2073 /* Label pointer equivalences.
2075 This performs a value numbering of the constraint graph to
2076 discover which variables will always have the same points-to sets
2077 under the current set of constraints.
2079 The way it value numbers is to store the set of points-to bits
2080 generated by the constraints and graph edges. This is just used as a
2081 hash and equality comparison. The *actual set of points-to bits* is
2082 completely irrelevant, in that we don't care about being able to
2085 The equality values (currently bitmaps) just have to satisfy a few
2086 constraints, the main ones being:
2087 1. The combining operation must be order independent.
2088 2. The end result of a given set of operations must be unique iff the
2089 combination of input values is unique
2093 label_visit (constraint_graph_t graph
, struct scc_info
*si
, unsigned int n
)
2095 unsigned int i
, first_pred
;
2098 bitmap_set_bit (si
->visited
, n
);
2100 /* Label and union our incoming edges's points to sets. */
2102 EXECUTE_IF_IN_NONNULL_BITMAP (graph
->preds
[n
], 0, i
, bi
)
2104 unsigned int w
= si
->node_mapping
[i
];
2105 if (!bitmap_bit_p (si
->visited
, w
))
2106 label_visit (graph
, si
, w
);
2108 /* Skip unused edges */
2109 if (w
== n
|| graph
->pointer_label
[w
] == 0)
2112 if (graph
->points_to
[w
])
2114 if (!graph
->points_to
[n
])
2116 if (first_pred
== -1U)
2120 graph
->points_to
[n
] = BITMAP_ALLOC (&predbitmap_obstack
);
2121 bitmap_ior (graph
->points_to
[n
],
2122 graph
->points_to
[first_pred
],
2123 graph
->points_to
[w
]);
2127 bitmap_ior_into (graph
->points_to
[n
], graph
->points_to
[w
]);
2131 /* Indirect nodes get fresh variables and a new pointer equiv class. */
2132 if (!bitmap_bit_p (graph
->direct_nodes
, n
))
2134 if (!graph
->points_to
[n
])
2136 graph
->points_to
[n
] = BITMAP_ALLOC (&predbitmap_obstack
);
2137 if (first_pred
!= -1U)
2138 bitmap_copy (graph
->points_to
[n
], graph
->points_to
[first_pred
]);
2140 bitmap_set_bit (graph
->points_to
[n
], FIRST_REF_NODE
+ n
);
2141 graph
->pointer_label
[n
] = pointer_equiv_class
++;
2142 equiv_class_label_t ecl
;
2143 ecl
= equiv_class_lookup_or_add (pointer_equiv_class_table
,
2144 graph
->points_to
[n
]);
2145 ecl
->equivalence_class
= graph
->pointer_label
[n
];
2149 /* If there was only a single non-empty predecessor the pointer equiv
2150 class is the same. */
2151 if (!graph
->points_to
[n
])
2153 if (first_pred
!= -1U)
2155 graph
->pointer_label
[n
] = graph
->pointer_label
[first_pred
];
2156 graph
->points_to
[n
] = graph
->points_to
[first_pred
];
2161 if (!bitmap_empty_p (graph
->points_to
[n
]))
2163 equiv_class_label_t ecl
;
2164 ecl
= equiv_class_lookup_or_add (pointer_equiv_class_table
,
2165 graph
->points_to
[n
]);
2166 if (ecl
->equivalence_class
== 0)
2167 ecl
->equivalence_class
= pointer_equiv_class
++;
2170 BITMAP_FREE (graph
->points_to
[n
]);
2171 graph
->points_to
[n
] = ecl
->labels
;
2173 graph
->pointer_label
[n
] = ecl
->equivalence_class
;
2177 /* Print the pred graph in dot format. */
2180 dump_pred_graph (struct scc_info
*si
, FILE *file
)
2184 /* Only print the graph if it has already been initialized: */
2188 /* Prints the header of the dot file: */
2189 fprintf (file
, "strict digraph {\n");
2190 fprintf (file
, " node [\n shape = box\n ]\n");
2191 fprintf (file
, " edge [\n fontsize = \"12\"\n ]\n");
2192 fprintf (file
, "\n // List of nodes and complex constraints in "
2193 "the constraint graph:\n");
2195 /* The next lines print the nodes in the graph together with the
2196 complex constraints attached to them. */
2197 for (i
= 1; i
< graph
->size
; i
++)
2199 if (i
== FIRST_REF_NODE
)
2201 if (si
->node_mapping
[i
] != i
)
2203 if (i
< FIRST_REF_NODE
)
2204 fprintf (file
, "\"%s\"", get_varinfo (i
)->name
);
2206 fprintf (file
, "\"*%s\"", get_varinfo (i
- FIRST_REF_NODE
)->name
);
2207 if (graph
->points_to
[i
]
2208 && !bitmap_empty_p (graph
->points_to
[i
]))
2210 fprintf (file
, "[label=\"%s = {", get_varinfo (i
)->name
);
2213 EXECUTE_IF_SET_IN_BITMAP (graph
->points_to
[i
], 0, j
, bi
)
2214 fprintf (file
, " %d", j
);
2215 fprintf (file
, " }\"]");
2217 fprintf (file
, ";\n");
2220 /* Go over the edges. */
2221 fprintf (file
, "\n // Edges in the constraint graph:\n");
2222 for (i
= 1; i
< graph
->size
; i
++)
2226 if (si
->node_mapping
[i
] != i
)
2228 EXECUTE_IF_IN_NONNULL_BITMAP (graph
->preds
[i
], 0, j
, bi
)
2230 unsigned from
= si
->node_mapping
[j
];
2231 if (from
< FIRST_REF_NODE
)
2232 fprintf (file
, "\"%s\"", get_varinfo (from
)->name
);
2234 fprintf (file
, "\"*%s\"", get_varinfo (from
- FIRST_REF_NODE
)->name
);
2235 fprintf (file
, " -> ");
2236 if (i
< FIRST_REF_NODE
)
2237 fprintf (file
, "\"%s\"", get_varinfo (i
)->name
);
2239 fprintf (file
, "\"*%s\"", get_varinfo (i
- FIRST_REF_NODE
)->name
);
2240 fprintf (file
, ";\n");
2244 /* Prints the tail of the dot file. */
2245 fprintf (file
, "}\n");
2248 /* Perform offline variable substitution, discovering equivalence
2249 classes, and eliminating non-pointer variables. */
2251 static struct scc_info
*
2252 perform_var_substitution (constraint_graph_t graph
)
2255 unsigned int size
= graph
->size
;
2256 struct scc_info
*si
= init_scc_info (size
);
2258 bitmap_obstack_initialize (&iteration_obstack
);
2259 pointer_equiv_class_table
.create (511);
2260 location_equiv_class_table
.create (511);
2261 pointer_equiv_class
= 1;
2262 location_equiv_class
= 1;
2264 /* Condense the nodes, which means to find SCC's, count incoming
2265 predecessors, and unite nodes in SCC's. */
2266 for (i
= 1; i
< FIRST_REF_NODE
; i
++)
2267 if (!bitmap_bit_p (si
->visited
, si
->node_mapping
[i
]))
2268 condense_visit (graph
, si
, si
->node_mapping
[i
]);
2270 if (dump_file
&& (dump_flags
& TDF_GRAPH
))
2272 fprintf (dump_file
, "\n\n// The constraint graph before var-substitution "
2273 "in dot format:\n");
2274 dump_pred_graph (si
, dump_file
);
2275 fprintf (dump_file
, "\n\n");
2278 bitmap_clear (si
->visited
);
2279 /* Actually the label the nodes for pointer equivalences */
2280 for (i
= 1; i
< FIRST_REF_NODE
; i
++)
2281 if (!bitmap_bit_p (si
->visited
, si
->node_mapping
[i
]))
2282 label_visit (graph
, si
, si
->node_mapping
[i
]);
2284 /* Calculate location equivalence labels. */
2285 for (i
= 1; i
< FIRST_REF_NODE
; i
++)
2291 if (!graph
->pointed_by
[i
])
2293 pointed_by
= BITMAP_ALLOC (&iteration_obstack
);
2295 /* Translate the pointed-by mapping for pointer equivalence
2297 EXECUTE_IF_SET_IN_BITMAP (graph
->pointed_by
[i
], 0, j
, bi
)
2299 bitmap_set_bit (pointed_by
,
2300 graph
->pointer_label
[si
->node_mapping
[j
]]);
2302 /* The original pointed_by is now dead. */
2303 BITMAP_FREE (graph
->pointed_by
[i
]);
2305 /* Look up the location equivalence label if one exists, or make
2307 equiv_class_label_t ecl
;
2308 ecl
= equiv_class_lookup_or_add (location_equiv_class_table
, pointed_by
);
2309 if (ecl
->equivalence_class
== 0)
2310 ecl
->equivalence_class
= location_equiv_class
++;
2313 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2314 fprintf (dump_file
, "Found location equivalence for node %s\n",
2315 get_varinfo (i
)->name
);
2316 BITMAP_FREE (pointed_by
);
2318 graph
->loc_label
[i
] = ecl
->equivalence_class
;
2322 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2323 for (i
= 1; i
< FIRST_REF_NODE
; i
++)
2325 unsigned j
= si
->node_mapping
[i
];
2328 fprintf (dump_file
, "%s node id %d ",
2329 bitmap_bit_p (graph
->direct_nodes
, i
)
2330 ? "Direct" : "Indirect", i
);
2331 if (i
< FIRST_REF_NODE
)
2332 fprintf (dump_file
, "\"%s\"", get_varinfo (i
)->name
);
2334 fprintf (dump_file
, "\"*%s\"",
2335 get_varinfo (i
- FIRST_REF_NODE
)->name
);
2336 fprintf (dump_file
, " mapped to SCC leader node id %d ", j
);
2337 if (j
< FIRST_REF_NODE
)
2338 fprintf (dump_file
, "\"%s\"\n", get_varinfo (j
)->name
);
2340 fprintf (dump_file
, "\"*%s\"\n",
2341 get_varinfo (j
- FIRST_REF_NODE
)->name
);
2346 "Equivalence classes for %s node id %d ",
2347 bitmap_bit_p (graph
->direct_nodes
, i
)
2348 ? "direct" : "indirect", i
);
2349 if (i
< FIRST_REF_NODE
)
2350 fprintf (dump_file
, "\"%s\"", get_varinfo (i
)->name
);
2352 fprintf (dump_file
, "\"*%s\"",
2353 get_varinfo (i
- FIRST_REF_NODE
)->name
);
2355 ": pointer %d, location %d\n",
2356 graph
->pointer_label
[i
], graph
->loc_label
[i
]);
2360 /* Quickly eliminate our non-pointer variables. */
2362 for (i
= 1; i
< FIRST_REF_NODE
; i
++)
2364 unsigned int node
= si
->node_mapping
[i
];
2366 if (graph
->pointer_label
[node
] == 0)
2368 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2370 "%s is a non-pointer variable, eliminating edges.\n",
2371 get_varinfo (node
)->name
);
2372 stats
.nonpointer_vars
++;
2373 clear_edges_for_node (graph
, node
);
2380 /* Free information that was only necessary for variable
2384 free_var_substitution_info (struct scc_info
*si
)
2387 free (graph
->pointer_label
);
2388 free (graph
->loc_label
);
2389 free (graph
->pointed_by
);
2390 free (graph
->points_to
);
2391 free (graph
->eq_rep
);
2392 sbitmap_free (graph
->direct_nodes
);
2393 pointer_equiv_class_table
.dispose ();
2394 location_equiv_class_table
.dispose ();
2395 bitmap_obstack_release (&iteration_obstack
);
2398 /* Return an existing node that is equivalent to NODE, which has
2399 equivalence class LABEL, if one exists. Return NODE otherwise. */
2402 find_equivalent_node (constraint_graph_t graph
,
2403 unsigned int node
, unsigned int label
)
2405 /* If the address version of this variable is unused, we can
2406 substitute it for anything else with the same label.
2407 Otherwise, we know the pointers are equivalent, but not the
2408 locations, and we can unite them later. */
2410 if (!bitmap_bit_p (graph
->address_taken
, node
))
2412 gcc_checking_assert (label
< graph
->size
);
2414 if (graph
->eq_rep
[label
] != -1)
2416 /* Unify the two variables since we know they are equivalent. */
2417 if (unite (graph
->eq_rep
[label
], node
))
2418 unify_nodes (graph
, graph
->eq_rep
[label
], node
, false);
2419 return graph
->eq_rep
[label
];
2423 graph
->eq_rep
[label
] = node
;
2424 graph
->pe_rep
[label
] = node
;
2429 gcc_checking_assert (label
< graph
->size
);
2430 graph
->pe
[node
] = label
;
2431 if (graph
->pe_rep
[label
] == -1)
2432 graph
->pe_rep
[label
] = node
;
2438 /* Unite pointer equivalent but not location equivalent nodes in
2439 GRAPH. This may only be performed once variable substitution is
2443 unite_pointer_equivalences (constraint_graph_t graph
)
2447 /* Go through the pointer equivalences and unite them to their
2448 representative, if they aren't already. */
2449 for (i
= 1; i
< FIRST_REF_NODE
; i
++)
2451 unsigned int label
= graph
->pe
[i
];
2454 int label_rep
= graph
->pe_rep
[label
];
2456 if (label_rep
== -1)
2459 label_rep
= find (label_rep
);
2460 if (label_rep
>= 0 && unite (label_rep
, find (i
)))
2461 unify_nodes (graph
, label_rep
, i
, false);
2466 /* Move complex constraints to the GRAPH nodes they belong to. */
2469 move_complex_constraints (constraint_graph_t graph
)
2474 FOR_EACH_VEC_ELT (constraints
, i
, c
)
2478 struct constraint_expr lhs
= c
->lhs
;
2479 struct constraint_expr rhs
= c
->rhs
;
2481 if (lhs
.type
== DEREF
)
2483 insert_into_complex (graph
, lhs
.var
, c
);
2485 else if (rhs
.type
== DEREF
)
2487 if (!(get_varinfo (lhs
.var
)->is_special_var
))
2488 insert_into_complex (graph
, rhs
.var
, c
);
2490 else if (rhs
.type
!= ADDRESSOF
&& lhs
.var
> anything_id
2491 && (lhs
.offset
!= 0 || rhs
.offset
!= 0))
2493 insert_into_complex (graph
, rhs
.var
, c
);
2500 /* Optimize and rewrite complex constraints while performing
2501 collapsing of equivalent nodes. SI is the SCC_INFO that is the
2502 result of perform_variable_substitution. */
2505 rewrite_constraints (constraint_graph_t graph
,
2506 struct scc_info
*si
)
2511 #ifdef ENABLE_CHECKING
2512 for (unsigned int j
= 0; j
< graph
->size
; j
++)
2513 gcc_assert (find (j
) == j
);
2516 FOR_EACH_VEC_ELT (constraints
, i
, c
)
2518 struct constraint_expr lhs
= c
->lhs
;
2519 struct constraint_expr rhs
= c
->rhs
;
2520 unsigned int lhsvar
= find (lhs
.var
);
2521 unsigned int rhsvar
= find (rhs
.var
);
2522 unsigned int lhsnode
, rhsnode
;
2523 unsigned int lhslabel
, rhslabel
;
2525 lhsnode
= si
->node_mapping
[lhsvar
];
2526 rhsnode
= si
->node_mapping
[rhsvar
];
2527 lhslabel
= graph
->pointer_label
[lhsnode
];
2528 rhslabel
= graph
->pointer_label
[rhsnode
];
2530 /* See if it is really a non-pointer variable, and if so, ignore
2534 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2537 fprintf (dump_file
, "%s is a non-pointer variable,"
2538 "ignoring constraint:",
2539 get_varinfo (lhs
.var
)->name
);
2540 dump_constraint (dump_file
, c
);
2541 fprintf (dump_file
, "\n");
2543 constraints
[i
] = NULL
;
2549 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2552 fprintf (dump_file
, "%s is a non-pointer variable,"
2553 "ignoring constraint:",
2554 get_varinfo (rhs
.var
)->name
);
2555 dump_constraint (dump_file
, c
);
2556 fprintf (dump_file
, "\n");
2558 constraints
[i
] = NULL
;
2562 lhsvar
= find_equivalent_node (graph
, lhsvar
, lhslabel
);
2563 rhsvar
= find_equivalent_node (graph
, rhsvar
, rhslabel
);
2564 c
->lhs
.var
= lhsvar
;
2565 c
->rhs
.var
= rhsvar
;
2569 /* Eliminate indirect cycles involving NODE. Return true if NODE was
2570 part of an SCC, false otherwise. */
2573 eliminate_indirect_cycles (unsigned int node
)
2575 if (graph
->indirect_cycles
[node
] != -1
2576 && !bitmap_empty_p (get_varinfo (node
)->solution
))
2579 auto_vec
<unsigned> queue
;
2581 unsigned int to
= find (graph
->indirect_cycles
[node
]);
2584 /* We can't touch the solution set and call unify_nodes
2585 at the same time, because unify_nodes is going to do
2586 bitmap unions into it. */
2588 EXECUTE_IF_SET_IN_BITMAP (get_varinfo (node
)->solution
, 0, i
, bi
)
2590 if (find (i
) == i
&& i
!= to
)
2593 queue
.safe_push (i
);
2598 queue
.iterate (queuepos
, &i
);
2601 unify_nodes (graph
, to
, i
, true);
2608 /* Solve the constraint graph GRAPH using our worklist solver.
2609 This is based on the PW* family of solvers from the "Efficient Field
2610 Sensitive Pointer Analysis for C" paper.
2611 It works by iterating over all the graph nodes, processing the complex
2612 constraints and propagating the copy constraints, until everything stops
2613 changed. This corresponds to steps 6-8 in the solving list given above. */
2616 solve_graph (constraint_graph_t graph
)
2618 unsigned int size
= graph
->size
;
2622 changed
= BITMAP_ALLOC (NULL
);
2624 /* Mark all initial non-collapsed nodes as changed. */
2625 for (i
= 1; i
< size
; i
++)
2627 varinfo_t ivi
= get_varinfo (i
);
2628 if (find (i
) == i
&& !bitmap_empty_p (ivi
->solution
)
2629 && ((graph
->succs
[i
] && !bitmap_empty_p (graph
->succs
[i
]))
2630 || graph
->complex[i
].length () > 0))
2631 bitmap_set_bit (changed
, i
);
2634 /* Allocate a bitmap to be used to store the changed bits. */
2635 pts
= BITMAP_ALLOC (&pta_obstack
);
2637 while (!bitmap_empty_p (changed
))
2640 struct topo_info
*ti
= init_topo_info ();
2643 bitmap_obstack_initialize (&iteration_obstack
);
2645 compute_topo_order (graph
, ti
);
2647 while (ti
->topo_order
.length () != 0)
2650 i
= ti
->topo_order
.pop ();
2652 /* If this variable is not a representative, skip it. */
2656 /* In certain indirect cycle cases, we may merge this
2657 variable to another. */
2658 if (eliminate_indirect_cycles (i
) && find (i
) != i
)
2661 /* If the node has changed, we need to process the
2662 complex constraints and outgoing edges again. */
2663 if (bitmap_clear_bit (changed
, i
))
2668 vec
<constraint_t
> complex = graph
->complex[i
];
2669 varinfo_t vi
= get_varinfo (i
);
2670 bool solution_empty
;
2672 /* Compute the changed set of solution bits. If anything
2673 is in the solution just propagate that. */
2674 if (bitmap_bit_p (vi
->solution
, anything_id
))
2676 /* If anything is also in the old solution there is
2678 ??? But we shouldn't ended up with "changed" set ... */
2680 && bitmap_bit_p (vi
->oldsolution
, anything_id
))
2682 bitmap_copy (pts
, get_varinfo (find (anything_id
))->solution
);
2684 else if (vi
->oldsolution
)
2685 bitmap_and_compl (pts
, vi
->solution
, vi
->oldsolution
);
2687 bitmap_copy (pts
, vi
->solution
);
2689 if (bitmap_empty_p (pts
))
2692 if (vi
->oldsolution
)
2693 bitmap_ior_into (vi
->oldsolution
, pts
);
2696 vi
->oldsolution
= BITMAP_ALLOC (&oldpta_obstack
);
2697 bitmap_copy (vi
->oldsolution
, pts
);
2700 solution
= vi
->solution
;
2701 solution_empty
= bitmap_empty_p (solution
);
2703 /* Process the complex constraints */
2704 FOR_EACH_VEC_ELT (complex, j
, c
)
2706 /* XXX: This is going to unsort the constraints in
2707 some cases, which will occasionally add duplicate
2708 constraints during unification. This does not
2709 affect correctness. */
2710 c
->lhs
.var
= find (c
->lhs
.var
);
2711 c
->rhs
.var
= find (c
->rhs
.var
);
2713 /* The only complex constraint that can change our
2714 solution to non-empty, given an empty solution,
2715 is a constraint where the lhs side is receiving
2716 some set from elsewhere. */
2717 if (!solution_empty
|| c
->lhs
.type
!= DEREF
)
2718 do_complex_constraint (graph
, c
, pts
);
2721 solution_empty
= bitmap_empty_p (solution
);
2723 if (!solution_empty
)
2726 unsigned eff_escaped_id
= find (escaped_id
);
2728 /* Propagate solution to all successors. */
2729 EXECUTE_IF_IN_NONNULL_BITMAP (graph
->succs
[i
],
2735 unsigned int to
= find (j
);
2736 tmp
= get_varinfo (to
)->solution
;
2739 /* Don't try to propagate to ourselves. */
2743 /* If we propagate from ESCAPED use ESCAPED as
2745 if (i
== eff_escaped_id
)
2746 flag
= bitmap_set_bit (tmp
, escaped_id
);
2748 flag
= bitmap_ior_into (tmp
, pts
);
2751 bitmap_set_bit (changed
, to
);
2756 free_topo_info (ti
);
2757 bitmap_obstack_release (&iteration_obstack
);
2761 BITMAP_FREE (changed
);
2762 bitmap_obstack_release (&oldpta_obstack
);
2765 /* Map from trees to variable infos. */
2766 static struct pointer_map_t
*vi_for_tree
;
2769 /* Insert ID as the variable id for tree T in the vi_for_tree map. */
2772 insert_vi_for_tree (tree t
, varinfo_t vi
)
2774 void **slot
= pointer_map_insert (vi_for_tree
, t
);
2776 gcc_assert (*slot
== NULL
);
2780 /* Find the variable info for tree T in VI_FOR_TREE. If T does not
2781 exist in the map, return NULL, otherwise, return the varinfo we found. */
2784 lookup_vi_for_tree (tree t
)
2786 void **slot
= pointer_map_contains (vi_for_tree
, t
);
2790 return (varinfo_t
) *slot
;
2793 /* Return a printable name for DECL */
2796 alias_get_name (tree decl
)
2798 const char *res
= NULL
;
2800 int num_printed
= 0;
2805 if (TREE_CODE (decl
) == SSA_NAME
)
2807 res
= get_name (decl
);
2809 num_printed
= asprintf (&temp
, "%s_%u", res
, SSA_NAME_VERSION (decl
));
2811 num_printed
= asprintf (&temp
, "_%u", SSA_NAME_VERSION (decl
));
2812 if (num_printed
> 0)
2814 res
= ggc_strdup (temp
);
2818 else if (DECL_P (decl
))
2820 if (DECL_ASSEMBLER_NAME_SET_P (decl
))
2821 res
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
2824 res
= get_name (decl
);
2827 num_printed
= asprintf (&temp
, "D.%u", DECL_UID (decl
));
2828 if (num_printed
> 0)
2830 res
= ggc_strdup (temp
);
2842 /* Find the variable id for tree T in the map.
2843 If T doesn't exist in the map, create an entry for it and return it. */
2846 get_vi_for_tree (tree t
)
2848 void **slot
= pointer_map_contains (vi_for_tree
, t
);
2850 return get_varinfo (create_variable_info_for (t
, alias_get_name (t
)));
2852 return (varinfo_t
) *slot
;
2855 /* Get a scalar constraint expression for a new temporary variable. */
2857 static struct constraint_expr
2858 new_scalar_tmp_constraint_exp (const char *name
)
2860 struct constraint_expr tmp
;
2863 vi
= new_var_info (NULL_TREE
, name
);
2867 vi
->is_full_var
= 1;
2876 /* Get a constraint expression vector from an SSA_VAR_P node.
2877 If address_p is true, the result will be taken its address of. */
2880 get_constraint_for_ssa_var (tree t
, vec
<ce_s
> *results
, bool address_p
)
2882 struct constraint_expr cexpr
;
2885 /* We allow FUNCTION_DECLs here even though it doesn't make much sense. */
2886 gcc_assert (TREE_CODE (t
) == SSA_NAME
|| DECL_P (t
));
2888 /* For parameters, get at the points-to set for the actual parm
2890 if (TREE_CODE (t
) == SSA_NAME
2891 && SSA_NAME_IS_DEFAULT_DEF (t
)
2892 && (TREE_CODE (SSA_NAME_VAR (t
)) == PARM_DECL
2893 || TREE_CODE (SSA_NAME_VAR (t
)) == RESULT_DECL
))
2895 get_constraint_for_ssa_var (SSA_NAME_VAR (t
), results
, address_p
);
2899 /* For global variables resort to the alias target. */
2900 if (TREE_CODE (t
) == VAR_DECL
2901 && (TREE_STATIC (t
) || DECL_EXTERNAL (t
)))
2903 struct varpool_node
*node
= varpool_get_node (t
);
2904 if (node
&& node
->alias
&& node
->analyzed
)
2906 node
= varpool_variable_node (node
, NULL
);
2911 vi
= get_vi_for_tree (t
);
2913 cexpr
.type
= SCALAR
;
2915 /* If we determine the result is "anything", and we know this is readonly,
2916 say it points to readonly memory instead. */
2917 if (cexpr
.var
== anything_id
&& TREE_READONLY (t
))
2920 cexpr
.type
= ADDRESSOF
;
2921 cexpr
.var
= readonly_id
;
2924 /* If we are not taking the address of the constraint expr, add all
2925 sub-fiels of the variable as well. */
2927 && !vi
->is_full_var
)
2929 for (; vi
; vi
= vi_next (vi
))
2932 results
->safe_push (cexpr
);
2937 results
->safe_push (cexpr
);
2940 /* Process constraint T, performing various simplifications and then
2941 adding it to our list of overall constraints. */
2944 process_constraint (constraint_t t
)
2946 struct constraint_expr rhs
= t
->rhs
;
2947 struct constraint_expr lhs
= t
->lhs
;
2949 gcc_assert (rhs
.var
< varmap
.length ());
2950 gcc_assert (lhs
.var
< varmap
.length ());
2952 /* If we didn't get any useful constraint from the lhs we get
2953 &ANYTHING as fallback from get_constraint_for. Deal with
2954 it here by turning it into *ANYTHING. */
2955 if (lhs
.type
== ADDRESSOF
2956 && lhs
.var
== anything_id
)
2959 /* ADDRESSOF on the lhs is invalid. */
2960 gcc_assert (lhs
.type
!= ADDRESSOF
);
2962 /* We shouldn't add constraints from things that cannot have pointers.
2963 It's not completely trivial to avoid in the callers, so do it here. */
2964 if (rhs
.type
!= ADDRESSOF
2965 && !get_varinfo (rhs
.var
)->may_have_pointers
)
2968 /* Likewise adding to the solution of a non-pointer var isn't useful. */
2969 if (!get_varinfo (lhs
.var
)->may_have_pointers
)
2972 /* This can happen in our IR with things like n->a = *p */
2973 if (rhs
.type
== DEREF
&& lhs
.type
== DEREF
&& rhs
.var
!= anything_id
)
2975 /* Split into tmp = *rhs, *lhs = tmp */
2976 struct constraint_expr tmplhs
;
2977 tmplhs
= new_scalar_tmp_constraint_exp ("doubledereftmp");
2978 process_constraint (new_constraint (tmplhs
, rhs
));
2979 process_constraint (new_constraint (lhs
, tmplhs
));
2981 else if (rhs
.type
== ADDRESSOF
&& lhs
.type
== DEREF
)
2983 /* Split into tmp = &rhs, *lhs = tmp */
2984 struct constraint_expr tmplhs
;
2985 tmplhs
= new_scalar_tmp_constraint_exp ("derefaddrtmp");
2986 process_constraint (new_constraint (tmplhs
, rhs
));
2987 process_constraint (new_constraint (lhs
, tmplhs
));
2991 gcc_assert (rhs
.type
!= ADDRESSOF
|| rhs
.offset
== 0);
2992 constraints
.safe_push (t
);
2997 /* Return the position, in bits, of FIELD_DECL from the beginning of its
3000 static HOST_WIDE_INT
3001 bitpos_of_field (const tree fdecl
)
3003 if (!tree_fits_shwi_p (DECL_FIELD_OFFSET (fdecl
))
3004 || !tree_fits_shwi_p (DECL_FIELD_BIT_OFFSET (fdecl
)))
3007 return (tree_to_shwi (DECL_FIELD_OFFSET (fdecl
)) * BITS_PER_UNIT
3008 + tree_to_shwi (DECL_FIELD_BIT_OFFSET (fdecl
)));
3012 /* Get constraint expressions for offsetting PTR by OFFSET. Stores the
3013 resulting constraint expressions in *RESULTS. */
3016 get_constraint_for_ptr_offset (tree ptr
, tree offset
,
3019 struct constraint_expr c
;
3021 HOST_WIDE_INT rhsoffset
;
3023 /* If we do not do field-sensitive PTA adding offsets to pointers
3024 does not change the points-to solution. */
3025 if (!use_field_sensitive
)
3027 get_constraint_for_rhs (ptr
, results
);
3031 /* If the offset is not a non-negative integer constant that fits
3032 in a HOST_WIDE_INT, we have to fall back to a conservative
3033 solution which includes all sub-fields of all pointed-to
3034 variables of ptr. */
3035 if (offset
== NULL_TREE
3036 || TREE_CODE (offset
) != INTEGER_CST
)
3037 rhsoffset
= UNKNOWN_OFFSET
;
3040 /* Sign-extend the offset. */
3041 double_int soffset
= tree_to_double_int (offset
)
3042 .sext (TYPE_PRECISION (TREE_TYPE (offset
)));
3043 if (!soffset
.fits_shwi ())
3044 rhsoffset
= UNKNOWN_OFFSET
;
3047 /* Make sure the bit-offset also fits. */
3048 HOST_WIDE_INT rhsunitoffset
= soffset
.low
;
3049 rhsoffset
= rhsunitoffset
* BITS_PER_UNIT
;
3050 if (rhsunitoffset
!= rhsoffset
/ BITS_PER_UNIT
)
3051 rhsoffset
= UNKNOWN_OFFSET
;
3055 get_constraint_for_rhs (ptr
, results
);
3059 /* As we are eventually appending to the solution do not use
3060 vec::iterate here. */
3061 n
= results
->length ();
3062 for (j
= 0; j
< n
; j
++)
3066 curr
= get_varinfo (c
.var
);
3068 if (c
.type
== ADDRESSOF
3069 /* If this varinfo represents a full variable just use it. */
3070 && curr
->is_full_var
)
3072 else if (c
.type
== ADDRESSOF
3073 /* If we do not know the offset add all subfields. */
3074 && rhsoffset
== UNKNOWN_OFFSET
)
3076 varinfo_t temp
= get_varinfo (curr
->head
);
3079 struct constraint_expr c2
;
3081 c2
.type
= ADDRESSOF
;
3083 if (c2
.var
!= c
.var
)
3084 results
->safe_push (c2
);
3085 temp
= vi_next (temp
);
3089 else if (c
.type
== ADDRESSOF
)
3092 unsigned HOST_WIDE_INT offset
= curr
->offset
+ rhsoffset
;
3094 /* Search the sub-field which overlaps with the
3095 pointed-to offset. If the result is outside of the variable
3096 we have to provide a conservative result, as the variable is
3097 still reachable from the resulting pointer (even though it
3098 technically cannot point to anything). The last and first
3099 sub-fields are such conservative results.
3100 ??? If we always had a sub-field for &object + 1 then
3101 we could represent this in a more precise way. */
3103 && curr
->offset
< offset
)
3105 temp
= first_or_preceding_vi_for_offset (curr
, offset
);
3107 /* If the found variable is not exactly at the pointed to
3108 result, we have to include the next variable in the
3109 solution as well. Otherwise two increments by offset / 2
3110 do not result in the same or a conservative superset
3112 if (temp
->offset
!= offset
3115 struct constraint_expr c2
;
3116 c2
.var
= temp
->next
;
3117 c2
.type
= ADDRESSOF
;
3119 results
->safe_push (c2
);
3125 c
.offset
= rhsoffset
;
3132 /* Given a COMPONENT_REF T, return the constraint_expr vector for it.
3133 If address_p is true the result will be taken its address of.
3134 If lhs_p is true then the constraint expression is assumed to be used
3138 get_constraint_for_component_ref (tree t
, vec
<ce_s
> *results
,
3139 bool address_p
, bool lhs_p
)
3142 HOST_WIDE_INT bitsize
= -1;
3143 HOST_WIDE_INT bitmaxsize
= -1;
3144 HOST_WIDE_INT bitpos
;
3147 /* Some people like to do cute things like take the address of
3150 while (handled_component_p (forzero
)
3151 || INDIRECT_REF_P (forzero
)
3152 || TREE_CODE (forzero
) == MEM_REF
)
3153 forzero
= TREE_OPERAND (forzero
, 0);
3155 if (CONSTANT_CLASS_P (forzero
) && integer_zerop (forzero
))
3157 struct constraint_expr temp
;
3160 temp
.var
= integer_id
;
3162 results
->safe_push (temp
);
3166 t
= get_ref_base_and_extent (t
, &bitpos
, &bitsize
, &bitmaxsize
);
3168 /* Pretend to take the address of the base, we'll take care of
3169 adding the required subset of sub-fields below. */
3170 get_constraint_for_1 (t
, results
, true, lhs_p
);
3171 gcc_assert (results
->length () == 1);
3172 struct constraint_expr
&result
= results
->last ();
3174 if (result
.type
== SCALAR
3175 && get_varinfo (result
.var
)->is_full_var
)
3176 /* For single-field vars do not bother about the offset. */
3178 else if (result
.type
== SCALAR
)
3180 /* In languages like C, you can access one past the end of an
3181 array. You aren't allowed to dereference it, so we can
3182 ignore this constraint. When we handle pointer subtraction,
3183 we may have to do something cute here. */
3185 if ((unsigned HOST_WIDE_INT
)bitpos
< get_varinfo (result
.var
)->fullsize
3188 /* It's also not true that the constraint will actually start at the
3189 right offset, it may start in some padding. We only care about
3190 setting the constraint to the first actual field it touches, so
3192 struct constraint_expr cexpr
= result
;
3196 for (curr
= get_varinfo (cexpr
.var
); curr
; curr
= vi_next (curr
))
3198 if (ranges_overlap_p (curr
->offset
, curr
->size
,
3199 bitpos
, bitmaxsize
))
3201 cexpr
.var
= curr
->id
;
3202 results
->safe_push (cexpr
);
3207 /* If we are going to take the address of this field then
3208 to be able to compute reachability correctly add at least
3209 the last field of the variable. */
3210 if (address_p
&& results
->length () == 0)
3212 curr
= get_varinfo (cexpr
.var
);
3213 while (curr
->next
!= 0)
3214 curr
= vi_next (curr
);
3215 cexpr
.var
= curr
->id
;
3216 results
->safe_push (cexpr
);
3218 else if (results
->length () == 0)
3219 /* Assert that we found *some* field there. The user couldn't be
3220 accessing *only* padding. */
3221 /* Still the user could access one past the end of an array
3222 embedded in a struct resulting in accessing *only* padding. */
3223 /* Or accessing only padding via type-punning to a type
3224 that has a filed just in padding space. */
3226 cexpr
.type
= SCALAR
;
3227 cexpr
.var
= anything_id
;
3229 results
->safe_push (cexpr
);
3232 else if (bitmaxsize
== 0)
3234 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3235 fprintf (dump_file
, "Access to zero-sized part of variable,"
3239 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3240 fprintf (dump_file
, "Access to past the end of variable, ignoring\n");
3242 else if (result
.type
== DEREF
)
3244 /* If we do not know exactly where the access goes say so. Note
3245 that only for non-structure accesses we know that we access
3246 at most one subfiled of any variable. */
3248 || bitsize
!= bitmaxsize
3249 || AGGREGATE_TYPE_P (TREE_TYPE (orig_t
))
3250 || result
.offset
== UNKNOWN_OFFSET
)
3251 result
.offset
= UNKNOWN_OFFSET
;
3253 result
.offset
+= bitpos
;
3255 else if (result
.type
== ADDRESSOF
)
3257 /* We can end up here for component references on a
3258 VIEW_CONVERT_EXPR <>(&foobar). */
3259 result
.type
= SCALAR
;
3260 result
.var
= anything_id
;
3268 /* Dereference the constraint expression CONS, and return the result.
3269 DEREF (ADDRESSOF) = SCALAR
3270 DEREF (SCALAR) = DEREF
3271 DEREF (DEREF) = (temp = DEREF1; result = DEREF(temp))
3272 This is needed so that we can handle dereferencing DEREF constraints. */
3275 do_deref (vec
<ce_s
> *constraints
)
3277 struct constraint_expr
*c
;
3280 FOR_EACH_VEC_ELT (*constraints
, i
, c
)
3282 if (c
->type
== SCALAR
)
3284 else if (c
->type
== ADDRESSOF
)
3286 else if (c
->type
== DEREF
)
3288 struct constraint_expr tmplhs
;
3289 tmplhs
= new_scalar_tmp_constraint_exp ("dereftmp");
3290 process_constraint (new_constraint (tmplhs
, *c
));
3291 c
->var
= tmplhs
.var
;
3298 /* Given a tree T, return the constraint expression for taking the
3302 get_constraint_for_address_of (tree t
, vec
<ce_s
> *results
)
3304 struct constraint_expr
*c
;
3307 get_constraint_for_1 (t
, results
, true, true);
3309 FOR_EACH_VEC_ELT (*results
, i
, c
)
3311 if (c
->type
== DEREF
)
3314 c
->type
= ADDRESSOF
;
3318 /* Given a tree T, return the constraint expression for it. */
3321 get_constraint_for_1 (tree t
, vec
<ce_s
> *results
, bool address_p
,
3324 struct constraint_expr temp
;
3326 /* x = integer is all glommed to a single variable, which doesn't
3327 point to anything by itself. That is, of course, unless it is an
3328 integer constant being treated as a pointer, in which case, we
3329 will return that this is really the addressof anything. This
3330 happens below, since it will fall into the default case. The only
3331 case we know something about an integer treated like a pointer is
3332 when it is the NULL pointer, and then we just say it points to
3335 Do not do that if -fno-delete-null-pointer-checks though, because
3336 in that case *NULL does not fail, so it _should_ alias *anything.
3337 It is not worth adding a new option or renaming the existing one,
3338 since this case is relatively obscure. */
3339 if ((TREE_CODE (t
) == INTEGER_CST
3340 && integer_zerop (t
))
3341 /* The only valid CONSTRUCTORs in gimple with pointer typed
3342 elements are zero-initializer. But in IPA mode we also
3343 process global initializers, so verify at least. */
3344 || (TREE_CODE (t
) == CONSTRUCTOR
3345 && CONSTRUCTOR_NELTS (t
) == 0))
3347 if (flag_delete_null_pointer_checks
)
3348 temp
.var
= nothing_id
;
3350 temp
.var
= nonlocal_id
;
3351 temp
.type
= ADDRESSOF
;
3353 results
->safe_push (temp
);
3357 /* String constants are read-only. */
3358 if (TREE_CODE (t
) == STRING_CST
)
3360 temp
.var
= readonly_id
;
3363 results
->safe_push (temp
);
3367 switch (TREE_CODE_CLASS (TREE_CODE (t
)))
3369 case tcc_expression
:
3371 switch (TREE_CODE (t
))
3374 get_constraint_for_address_of (TREE_OPERAND (t
, 0), results
);
3382 switch (TREE_CODE (t
))
3386 struct constraint_expr cs
;
3388 get_constraint_for_ptr_offset (TREE_OPERAND (t
, 0),
3389 TREE_OPERAND (t
, 1), results
);
3392 /* If we are not taking the address then make sure to process
3393 all subvariables we might access. */
3397 cs
= results
->last ();
3398 if (cs
.type
== DEREF
3399 && type_can_have_subvars (TREE_TYPE (t
)))
3401 /* For dereferences this means we have to defer it
3403 results
->last ().offset
= UNKNOWN_OFFSET
;
3406 if (cs
.type
!= SCALAR
)
3409 vi
= get_varinfo (cs
.var
);
3410 curr
= vi_next (vi
);
3411 if (!vi
->is_full_var
3414 unsigned HOST_WIDE_INT size
;
3415 if (tree_fits_uhwi_p (TYPE_SIZE (TREE_TYPE (t
))))
3416 size
= tree_to_uhwi (TYPE_SIZE (TREE_TYPE (t
)));
3419 for (; curr
; curr
= vi_next (curr
))
3421 if (curr
->offset
- vi
->offset
< size
)
3424 results
->safe_push (cs
);
3433 case ARRAY_RANGE_REF
:
3435 get_constraint_for_component_ref (t
, results
, address_p
, lhs_p
);
3437 case VIEW_CONVERT_EXPR
:
3438 get_constraint_for_1 (TREE_OPERAND (t
, 0), results
, address_p
,
3441 /* We are missing handling for TARGET_MEM_REF here. */
3446 case tcc_exceptional
:
3448 switch (TREE_CODE (t
))
3452 get_constraint_for_ssa_var (t
, results
, address_p
);
3460 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t
), i
, val
)
3462 struct constraint_expr
*rhsp
;
3464 get_constraint_for_1 (val
, &tmp
, address_p
, lhs_p
);
3465 FOR_EACH_VEC_ELT (tmp
, j
, rhsp
)
3466 results
->safe_push (*rhsp
);
3469 /* We do not know whether the constructor was complete,
3470 so technically we have to add &NOTHING or &ANYTHING
3471 like we do for an empty constructor as well. */
3478 case tcc_declaration
:
3480 get_constraint_for_ssa_var (t
, results
, address_p
);
3485 /* We cannot refer to automatic variables through constants. */
3486 temp
.type
= ADDRESSOF
;
3487 temp
.var
= nonlocal_id
;
3489 results
->safe_push (temp
);
3495 /* The default fallback is a constraint from anything. */
3496 temp
.type
= ADDRESSOF
;
3497 temp
.var
= anything_id
;
3499 results
->safe_push (temp
);
3502 /* Given a gimple tree T, return the constraint expression vector for it. */
3505 get_constraint_for (tree t
, vec
<ce_s
> *results
)
3507 gcc_assert (results
->length () == 0);
3509 get_constraint_for_1 (t
, results
, false, true);
3512 /* Given a gimple tree T, return the constraint expression vector for it
3513 to be used as the rhs of a constraint. */
3516 get_constraint_for_rhs (tree t
, vec
<ce_s
> *results
)
3518 gcc_assert (results
->length () == 0);
3520 get_constraint_for_1 (t
, results
, false, false);
3524 /* Efficiently generates constraints from all entries in *RHSC to all
3525 entries in *LHSC. */
3528 process_all_all_constraints (vec
<ce_s
> lhsc
,
3531 struct constraint_expr
*lhsp
, *rhsp
;
3534 if (lhsc
.length () <= 1 || rhsc
.length () <= 1)
3536 FOR_EACH_VEC_ELT (lhsc
, i
, lhsp
)
3537 FOR_EACH_VEC_ELT (rhsc
, j
, rhsp
)
3538 process_constraint (new_constraint (*lhsp
, *rhsp
));
3542 struct constraint_expr tmp
;
3543 tmp
= new_scalar_tmp_constraint_exp ("allalltmp");
3544 FOR_EACH_VEC_ELT (rhsc
, i
, rhsp
)
3545 process_constraint (new_constraint (tmp
, *rhsp
));
3546 FOR_EACH_VEC_ELT (lhsc
, i
, lhsp
)
3547 process_constraint (new_constraint (*lhsp
, tmp
));
3551 /* Handle aggregate copies by expanding into copies of the respective
3552 fields of the structures. */
3555 do_structure_copy (tree lhsop
, tree rhsop
)
3557 struct constraint_expr
*lhsp
, *rhsp
;
3558 auto_vec
<ce_s
> lhsc
;
3559 auto_vec
<ce_s
> rhsc
;
3562 get_constraint_for (lhsop
, &lhsc
);
3563 get_constraint_for_rhs (rhsop
, &rhsc
);
3566 if (lhsp
->type
== DEREF
3567 || (lhsp
->type
== ADDRESSOF
&& lhsp
->var
== anything_id
)
3568 || rhsp
->type
== DEREF
)
3570 if (lhsp
->type
== DEREF
)
3572 gcc_assert (lhsc
.length () == 1);
3573 lhsp
->offset
= UNKNOWN_OFFSET
;
3575 if (rhsp
->type
== DEREF
)
3577 gcc_assert (rhsc
.length () == 1);
3578 rhsp
->offset
= UNKNOWN_OFFSET
;
3580 process_all_all_constraints (lhsc
, rhsc
);
3582 else if (lhsp
->type
== SCALAR
3583 && (rhsp
->type
== SCALAR
3584 || rhsp
->type
== ADDRESSOF
))
3586 HOST_WIDE_INT lhssize
, lhsmaxsize
, lhsoffset
;
3587 HOST_WIDE_INT rhssize
, rhsmaxsize
, rhsoffset
;
3589 get_ref_base_and_extent (lhsop
, &lhsoffset
, &lhssize
, &lhsmaxsize
);
3590 get_ref_base_and_extent (rhsop
, &rhsoffset
, &rhssize
, &rhsmaxsize
);
3591 for (j
= 0; lhsc
.iterate (j
, &lhsp
);)
3593 varinfo_t lhsv
, rhsv
;
3595 lhsv
= get_varinfo (lhsp
->var
);
3596 rhsv
= get_varinfo (rhsp
->var
);
3597 if (lhsv
->may_have_pointers
3598 && (lhsv
->is_full_var
3599 || rhsv
->is_full_var
3600 || ranges_overlap_p (lhsv
->offset
+ rhsoffset
, lhsv
->size
,
3601 rhsv
->offset
+ lhsoffset
, rhsv
->size
)))
3602 process_constraint (new_constraint (*lhsp
, *rhsp
));
3603 if (!rhsv
->is_full_var
3604 && (lhsv
->is_full_var
3605 || (lhsv
->offset
+ rhsoffset
+ lhsv
->size
3606 > rhsv
->offset
+ lhsoffset
+ rhsv
->size
)))
3609 if (k
>= rhsc
.length ())
3620 /* Create constraints ID = { rhsc }. */
3623 make_constraints_to (unsigned id
, vec
<ce_s
> rhsc
)
3625 struct constraint_expr
*c
;
3626 struct constraint_expr includes
;
3630 includes
.offset
= 0;
3631 includes
.type
= SCALAR
;
3633 FOR_EACH_VEC_ELT (rhsc
, j
, c
)
3634 process_constraint (new_constraint (includes
, *c
));
3637 /* Create a constraint ID = OP. */
3640 make_constraint_to (unsigned id
, tree op
)
3642 auto_vec
<ce_s
> rhsc
;
3643 get_constraint_for_rhs (op
, &rhsc
);
3644 make_constraints_to (id
, rhsc
);
3647 /* Create a constraint ID = &FROM. */
3650 make_constraint_from (varinfo_t vi
, int from
)
3652 struct constraint_expr lhs
, rhs
;
3660 rhs
.type
= ADDRESSOF
;
3661 process_constraint (new_constraint (lhs
, rhs
));
3664 /* Create a constraint ID = FROM. */
3667 make_copy_constraint (varinfo_t vi
, int from
)
3669 struct constraint_expr lhs
, rhs
;
3678 process_constraint (new_constraint (lhs
, rhs
));
3681 /* Make constraints necessary to make OP escape. */
3684 make_escape_constraint (tree op
)
3686 make_constraint_to (escaped_id
, op
);
3689 /* Add constraints to that the solution of VI is transitively closed. */
3692 make_transitive_closure_constraints (varinfo_t vi
)
3694 struct constraint_expr lhs
, rhs
;
3703 process_constraint (new_constraint (lhs
, rhs
));
3705 /* VAR = VAR + UNKNOWN; */
3711 rhs
.offset
= UNKNOWN_OFFSET
;
3712 process_constraint (new_constraint (lhs
, rhs
));
3715 /* Temporary storage for fake var decls. */
3716 struct obstack fake_var_decl_obstack
;
3718 /* Build a fake VAR_DECL acting as referrer to a DECL_UID. */
3721 build_fake_var_decl (tree type
)
3723 tree decl
= (tree
) XOBNEW (&fake_var_decl_obstack
, struct tree_var_decl
);
3724 memset (decl
, 0, sizeof (struct tree_var_decl
));
3725 TREE_SET_CODE (decl
, VAR_DECL
);
3726 TREE_TYPE (decl
) = type
;
3727 DECL_UID (decl
) = allocate_decl_uid ();
3728 SET_DECL_PT_UID (decl
, -1);
3729 layout_decl (decl
, 0);
3733 /* Create a new artificial heap variable with NAME.
3734 Return the created variable. */
3737 make_heapvar (const char *name
)
3742 heapvar
= build_fake_var_decl (ptr_type_node
);
3743 DECL_EXTERNAL (heapvar
) = 1;
3745 vi
= new_var_info (heapvar
, name
);
3746 vi
->is_artificial_var
= true;
3747 vi
->is_heap_var
= true;
3748 vi
->is_unknown_size_var
= true;
3752 vi
->is_full_var
= true;
3753 insert_vi_for_tree (heapvar
, vi
);
3758 /* Create a new artificial heap variable with NAME and make a
3759 constraint from it to LHS. Set flags according to a tag used
3760 for tracking restrict pointers. */
3763 make_constraint_from_restrict (varinfo_t lhs
, const char *name
)
3765 varinfo_t vi
= make_heapvar (name
);
3766 vi
->is_global_var
= 1;
3767 vi
->may_have_pointers
= 1;
3768 make_constraint_from (lhs
, vi
->id
);
3772 /* Create a new artificial heap variable with NAME and make a
3773 constraint from it to LHS. Set flags according to a tag used
3774 for tracking restrict pointers and make the artificial heap
3775 point to global memory. */
3778 make_constraint_from_global_restrict (varinfo_t lhs
, const char *name
)
3780 varinfo_t vi
= make_constraint_from_restrict (lhs
, name
);
3781 make_copy_constraint (vi
, nonlocal_id
);
3785 /* In IPA mode there are varinfos for different aspects of reach
3786 function designator. One for the points-to set of the return
3787 value, one for the variables that are clobbered by the function,
3788 one for its uses and one for each parameter (including a single
3789 glob for remaining variadic arguments). */
3791 enum { fi_clobbers
= 1, fi_uses
= 2,
3792 fi_static_chain
= 3, fi_result
= 4, fi_parm_base
= 5 };
3794 /* Get a constraint for the requested part of a function designator FI
3795 when operating in IPA mode. */
3797 static struct constraint_expr
3798 get_function_part_constraint (varinfo_t fi
, unsigned part
)
3800 struct constraint_expr c
;
3802 gcc_assert (in_ipa_mode
);
3804 if (fi
->id
== anything_id
)
3806 /* ??? We probably should have a ANYFN special variable. */
3807 c
.var
= anything_id
;
3811 else if (TREE_CODE (fi
->decl
) == FUNCTION_DECL
)
3813 varinfo_t ai
= first_vi_for_offset (fi
, part
);
3817 c
.var
= anything_id
;
3831 /* For non-IPA mode, generate constraints necessary for a call on the
3835 handle_rhs_call (gimple stmt
, vec
<ce_s
> *results
)
3837 struct constraint_expr rhsc
;
3839 bool returns_uses
= false;
3841 for (i
= 0; i
< gimple_call_num_args (stmt
); ++i
)
3843 tree arg
= gimple_call_arg (stmt
, i
);
3844 int flags
= gimple_call_arg_flags (stmt
, i
);
3846 /* If the argument is not used we can ignore it. */
3847 if (flags
& EAF_UNUSED
)
3850 /* As we compute ESCAPED context-insensitive we do not gain
3851 any precision with just EAF_NOCLOBBER but not EAF_NOESCAPE
3852 set. The argument would still get clobbered through the
3854 if ((flags
& EAF_NOCLOBBER
)
3855 && (flags
& EAF_NOESCAPE
))
3857 varinfo_t uses
= get_call_use_vi (stmt
);
3858 if (!(flags
& EAF_DIRECT
))
3860 varinfo_t tem
= new_var_info (NULL_TREE
, "callarg");
3861 make_constraint_to (tem
->id
, arg
);
3862 make_transitive_closure_constraints (tem
);
3863 make_copy_constraint (uses
, tem
->id
);
3866 make_constraint_to (uses
->id
, arg
);
3867 returns_uses
= true;
3869 else if (flags
& EAF_NOESCAPE
)
3871 struct constraint_expr lhs
, rhs
;
3872 varinfo_t uses
= get_call_use_vi (stmt
);
3873 varinfo_t clobbers
= get_call_clobber_vi (stmt
);
3874 varinfo_t tem
= new_var_info (NULL_TREE
, "callarg");
3875 make_constraint_to (tem
->id
, arg
);
3876 if (!(flags
& EAF_DIRECT
))
3877 make_transitive_closure_constraints (tem
);
3878 make_copy_constraint (uses
, tem
->id
);
3879 make_copy_constraint (clobbers
, tem
->id
);
3880 /* Add *tem = nonlocal, do not add *tem = callused as
3881 EAF_NOESCAPE parameters do not escape to other parameters
3882 and all other uses appear in NONLOCAL as well. */
3887 rhs
.var
= nonlocal_id
;
3889 process_constraint (new_constraint (lhs
, rhs
));
3890 returns_uses
= true;
3893 make_escape_constraint (arg
);
3896 /* If we added to the calls uses solution make sure we account for
3897 pointers to it to be returned. */
3900 rhsc
.var
= get_call_use_vi (stmt
)->id
;
3903 results
->safe_push (rhsc
);
3906 /* The static chain escapes as well. */
3907 if (gimple_call_chain (stmt
))
3908 make_escape_constraint (gimple_call_chain (stmt
));
3910 /* And if we applied NRV the address of the return slot escapes as well. */
3911 if (gimple_call_return_slot_opt_p (stmt
)
3912 && gimple_call_lhs (stmt
) != NULL_TREE
3913 && TREE_ADDRESSABLE (TREE_TYPE (gimple_call_lhs (stmt
))))
3915 auto_vec
<ce_s
> tmpc
;
3916 struct constraint_expr lhsc
, *c
;
3917 get_constraint_for_address_of (gimple_call_lhs (stmt
), &tmpc
);
3918 lhsc
.var
= escaped_id
;
3921 FOR_EACH_VEC_ELT (tmpc
, i
, c
)
3922 process_constraint (new_constraint (lhsc
, *c
));
3925 /* Regular functions return nonlocal memory. */
3926 rhsc
.var
= nonlocal_id
;
3929 results
->safe_push (rhsc
);
3932 /* For non-IPA mode, generate constraints necessary for a call
3933 that returns a pointer and assigns it to LHS. This simply makes
3934 the LHS point to global and escaped variables. */
3937 handle_lhs_call (gimple stmt
, tree lhs
, int flags
, vec
<ce_s
> rhsc
,
3940 auto_vec
<ce_s
> lhsc
;
3942 get_constraint_for (lhs
, &lhsc
);
3943 /* If the store is to a global decl make sure to
3944 add proper escape constraints. */
3945 lhs
= get_base_address (lhs
);
3948 && is_global_var (lhs
))
3950 struct constraint_expr tmpc
;
3951 tmpc
.var
= escaped_id
;
3954 lhsc
.safe_push (tmpc
);
3957 /* If the call returns an argument unmodified override the rhs
3959 flags
= gimple_call_return_flags (stmt
);
3960 if (flags
& ERF_RETURNS_ARG
3961 && (flags
& ERF_RETURN_ARG_MASK
) < gimple_call_num_args (stmt
))
3965 arg
= gimple_call_arg (stmt
, flags
& ERF_RETURN_ARG_MASK
);
3966 get_constraint_for (arg
, &rhsc
);
3967 process_all_all_constraints (lhsc
, rhsc
);
3970 else if (flags
& ERF_NOALIAS
)
3973 struct constraint_expr tmpc
;
3975 vi
= make_heapvar ("HEAP");
3976 /* We marking allocated storage local, we deal with it becoming
3977 global by escaping and setting of vars_contains_escaped_heap. */
3978 DECL_EXTERNAL (vi
->decl
) = 0;
3979 vi
->is_global_var
= 0;
3980 /* If this is not a real malloc call assume the memory was
3981 initialized and thus may point to global memory. All
3982 builtin functions with the malloc attribute behave in a sane way. */
3984 || DECL_BUILT_IN_CLASS (fndecl
) != BUILT_IN_NORMAL
)
3985 make_constraint_from (vi
, nonlocal_id
);
3988 tmpc
.type
= ADDRESSOF
;
3989 rhsc
.safe_push (tmpc
);
3990 process_all_all_constraints (lhsc
, rhsc
);
3994 process_all_all_constraints (lhsc
, rhsc
);
3997 /* For non-IPA mode, generate constraints necessary for a call of a
3998 const function that returns a pointer in the statement STMT. */
4001 handle_const_call (gimple stmt
, vec
<ce_s
> *results
)
4003 struct constraint_expr rhsc
;
4006 /* Treat nested const functions the same as pure functions as far
4007 as the static chain is concerned. */
4008 if (gimple_call_chain (stmt
))
4010 varinfo_t uses
= get_call_use_vi (stmt
);
4011 make_transitive_closure_constraints (uses
);
4012 make_constraint_to (uses
->id
, gimple_call_chain (stmt
));
4013 rhsc
.var
= uses
->id
;
4016 results
->safe_push (rhsc
);
4019 /* May return arguments. */
4020 for (k
= 0; k
< gimple_call_num_args (stmt
); ++k
)
4022 tree arg
= gimple_call_arg (stmt
, k
);
4023 auto_vec
<ce_s
> argc
;
4025 struct constraint_expr
*argp
;
4026 get_constraint_for_rhs (arg
, &argc
);
4027 FOR_EACH_VEC_ELT (argc
, i
, argp
)
4028 results
->safe_push (*argp
);
4031 /* May return addresses of globals. */
4032 rhsc
.var
= nonlocal_id
;
4034 rhsc
.type
= ADDRESSOF
;
4035 results
->safe_push (rhsc
);
4038 /* For non-IPA mode, generate constraints necessary for a call to a
4039 pure function in statement STMT. */
4042 handle_pure_call (gimple stmt
, vec
<ce_s
> *results
)
4044 struct constraint_expr rhsc
;
4046 varinfo_t uses
= NULL
;
4048 /* Memory reached from pointer arguments is call-used. */
4049 for (i
= 0; i
< gimple_call_num_args (stmt
); ++i
)
4051 tree arg
= gimple_call_arg (stmt
, i
);
4054 uses
= get_call_use_vi (stmt
);
4055 make_transitive_closure_constraints (uses
);
4057 make_constraint_to (uses
->id
, arg
);
4060 /* The static chain is used as well. */
4061 if (gimple_call_chain (stmt
))
4065 uses
= get_call_use_vi (stmt
);
4066 make_transitive_closure_constraints (uses
);
4068 make_constraint_to (uses
->id
, gimple_call_chain (stmt
));
4071 /* Pure functions may return call-used and nonlocal memory. */
4074 rhsc
.var
= uses
->id
;
4077 results
->safe_push (rhsc
);
4079 rhsc
.var
= nonlocal_id
;
4082 results
->safe_push (rhsc
);
4086 /* Return the varinfo for the callee of CALL. */
4089 get_fi_for_callee (gimple call
)
4091 tree decl
, fn
= gimple_call_fn (call
);
4093 if (fn
&& TREE_CODE (fn
) == OBJ_TYPE_REF
)
4094 fn
= OBJ_TYPE_REF_EXPR (fn
);
4096 /* If we can directly resolve the function being called, do so.
4097 Otherwise, it must be some sort of indirect expression that
4098 we should still be able to handle. */
4099 decl
= gimple_call_addr_fndecl (fn
);
4101 return get_vi_for_tree (decl
);
4103 /* If the function is anything other than a SSA name pointer we have no
4104 clue and should be getting ANYFN (well, ANYTHING for now). */
4105 if (!fn
|| TREE_CODE (fn
) != SSA_NAME
)
4106 return get_varinfo (anything_id
);
4108 if (SSA_NAME_IS_DEFAULT_DEF (fn
)
4109 && (TREE_CODE (SSA_NAME_VAR (fn
)) == PARM_DECL
4110 || TREE_CODE (SSA_NAME_VAR (fn
)) == RESULT_DECL
))
4111 fn
= SSA_NAME_VAR (fn
);
4113 return get_vi_for_tree (fn
);
4116 /* Create constraints for the builtin call T. Return true if the call
4117 was handled, otherwise false. */
4120 find_func_aliases_for_builtin_call (gimple t
)
4122 tree fndecl
= gimple_call_fndecl (t
);
4123 vec
<ce_s
> lhsc
= vNULL
;
4124 vec
<ce_s
> rhsc
= vNULL
;
4127 if (gimple_call_builtin_p (t
, BUILT_IN_NORMAL
))
4128 /* ??? All builtins that are handled here need to be handled
4129 in the alias-oracle query functions explicitly! */
4130 switch (DECL_FUNCTION_CODE (fndecl
))
4132 /* All the following functions return a pointer to the same object
4133 as their first argument points to. The functions do not add
4134 to the ESCAPED solution. The functions make the first argument
4135 pointed to memory point to what the second argument pointed to
4136 memory points to. */
4137 case BUILT_IN_STRCPY
:
4138 case BUILT_IN_STRNCPY
:
4139 case BUILT_IN_BCOPY
:
4140 case BUILT_IN_MEMCPY
:
4141 case BUILT_IN_MEMMOVE
:
4142 case BUILT_IN_MEMPCPY
:
4143 case BUILT_IN_STPCPY
:
4144 case BUILT_IN_STPNCPY
:
4145 case BUILT_IN_STRCAT
:
4146 case BUILT_IN_STRNCAT
:
4147 case BUILT_IN_STRCPY_CHK
:
4148 case BUILT_IN_STRNCPY_CHK
:
4149 case BUILT_IN_MEMCPY_CHK
:
4150 case BUILT_IN_MEMMOVE_CHK
:
4151 case BUILT_IN_MEMPCPY_CHK
:
4152 case BUILT_IN_STPCPY_CHK
:
4153 case BUILT_IN_STPNCPY_CHK
:
4154 case BUILT_IN_STRCAT_CHK
:
4155 case BUILT_IN_STRNCAT_CHK
:
4156 case BUILT_IN_TM_MEMCPY
:
4157 case BUILT_IN_TM_MEMMOVE
:
4159 tree res
= gimple_call_lhs (t
);
4160 tree dest
= gimple_call_arg (t
, (DECL_FUNCTION_CODE (fndecl
)
4161 == BUILT_IN_BCOPY
? 1 : 0));
4162 tree src
= gimple_call_arg (t
, (DECL_FUNCTION_CODE (fndecl
)
4163 == BUILT_IN_BCOPY
? 0 : 1));
4164 if (res
!= NULL_TREE
)
4166 get_constraint_for (res
, &lhsc
);
4167 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_MEMPCPY
4168 || DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_STPCPY
4169 || DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_STPNCPY
4170 || DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_MEMPCPY_CHK
4171 || DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_STPCPY_CHK
4172 || DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_STPNCPY_CHK
)
4173 get_constraint_for_ptr_offset (dest
, NULL_TREE
, &rhsc
);
4175 get_constraint_for (dest
, &rhsc
);
4176 process_all_all_constraints (lhsc
, rhsc
);
4180 get_constraint_for_ptr_offset (dest
, NULL_TREE
, &lhsc
);
4181 get_constraint_for_ptr_offset (src
, NULL_TREE
, &rhsc
);
4184 process_all_all_constraints (lhsc
, rhsc
);
4189 case BUILT_IN_MEMSET
:
4190 case BUILT_IN_MEMSET_CHK
:
4191 case BUILT_IN_TM_MEMSET
:
4193 tree res
= gimple_call_lhs (t
);
4194 tree dest
= gimple_call_arg (t
, 0);
4197 struct constraint_expr ac
;
4198 if (res
!= NULL_TREE
)
4200 get_constraint_for (res
, &lhsc
);
4201 get_constraint_for (dest
, &rhsc
);
4202 process_all_all_constraints (lhsc
, rhsc
);
4206 get_constraint_for_ptr_offset (dest
, NULL_TREE
, &lhsc
);
4208 if (flag_delete_null_pointer_checks
4209 && integer_zerop (gimple_call_arg (t
, 1)))
4211 ac
.type
= ADDRESSOF
;
4212 ac
.var
= nothing_id
;
4217 ac
.var
= integer_id
;
4220 FOR_EACH_VEC_ELT (lhsc
, i
, lhsp
)
4221 process_constraint (new_constraint (*lhsp
, ac
));
4225 case BUILT_IN_ASSUME_ALIGNED
:
4227 tree res
= gimple_call_lhs (t
);
4228 tree dest
= gimple_call_arg (t
, 0);
4229 if (res
!= NULL_TREE
)
4231 get_constraint_for (res
, &lhsc
);
4232 get_constraint_for (dest
, &rhsc
);
4233 process_all_all_constraints (lhsc
, rhsc
);
4239 /* All the following functions do not return pointers, do not
4240 modify the points-to sets of memory reachable from their
4241 arguments and do not add to the ESCAPED solution. */
4242 case BUILT_IN_SINCOS
:
4243 case BUILT_IN_SINCOSF
:
4244 case BUILT_IN_SINCOSL
:
4245 case BUILT_IN_FREXP
:
4246 case BUILT_IN_FREXPF
:
4247 case BUILT_IN_FREXPL
:
4248 case BUILT_IN_GAMMA_R
:
4249 case BUILT_IN_GAMMAF_R
:
4250 case BUILT_IN_GAMMAL_R
:
4251 case BUILT_IN_LGAMMA_R
:
4252 case BUILT_IN_LGAMMAF_R
:
4253 case BUILT_IN_LGAMMAL_R
:
4255 case BUILT_IN_MODFF
:
4256 case BUILT_IN_MODFL
:
4257 case BUILT_IN_REMQUO
:
4258 case BUILT_IN_REMQUOF
:
4259 case BUILT_IN_REMQUOL
:
4262 case BUILT_IN_STRDUP
:
4263 case BUILT_IN_STRNDUP
:
4264 if (gimple_call_lhs (t
))
4266 handle_lhs_call (t
, gimple_call_lhs (t
), gimple_call_flags (t
),
4268 get_constraint_for_ptr_offset (gimple_call_lhs (t
),
4270 get_constraint_for_ptr_offset (gimple_call_arg (t
, 0),
4274 process_all_all_constraints (lhsc
, rhsc
);
4280 /* String / character search functions return a pointer into the
4281 source string or NULL. */
4282 case BUILT_IN_INDEX
:
4283 case BUILT_IN_STRCHR
:
4284 case BUILT_IN_STRRCHR
:
4285 case BUILT_IN_MEMCHR
:
4286 case BUILT_IN_STRSTR
:
4287 case BUILT_IN_STRPBRK
:
4288 if (gimple_call_lhs (t
))
4290 tree src
= gimple_call_arg (t
, 0);
4291 get_constraint_for_ptr_offset (src
, NULL_TREE
, &rhsc
);
4292 constraint_expr nul
;
4293 nul
.var
= nothing_id
;
4295 nul
.type
= ADDRESSOF
;
4296 rhsc
.safe_push (nul
);
4297 get_constraint_for (gimple_call_lhs (t
), &lhsc
);
4298 process_all_all_constraints (lhsc
, rhsc
);
4303 /* Trampolines are special - they set up passing the static
4305 case BUILT_IN_INIT_TRAMPOLINE
:
4307 tree tramp
= gimple_call_arg (t
, 0);
4308 tree nfunc
= gimple_call_arg (t
, 1);
4309 tree frame
= gimple_call_arg (t
, 2);
4311 struct constraint_expr lhs
, *rhsp
;
4314 varinfo_t nfi
= NULL
;
4315 gcc_assert (TREE_CODE (nfunc
) == ADDR_EXPR
);
4316 nfi
= lookup_vi_for_tree (TREE_OPERAND (nfunc
, 0));
4319 lhs
= get_function_part_constraint (nfi
, fi_static_chain
);
4320 get_constraint_for (frame
, &rhsc
);
4321 FOR_EACH_VEC_ELT (rhsc
, i
, rhsp
)
4322 process_constraint (new_constraint (lhs
, *rhsp
));
4325 /* Make the frame point to the function for
4326 the trampoline adjustment call. */
4327 get_constraint_for (tramp
, &lhsc
);
4329 get_constraint_for (nfunc
, &rhsc
);
4330 process_all_all_constraints (lhsc
, rhsc
);
4337 /* Else fallthru to generic handling which will let
4338 the frame escape. */
4341 case BUILT_IN_ADJUST_TRAMPOLINE
:
4343 tree tramp
= gimple_call_arg (t
, 0);
4344 tree res
= gimple_call_lhs (t
);
4345 if (in_ipa_mode
&& res
)
4347 get_constraint_for (res
, &lhsc
);
4348 get_constraint_for (tramp
, &rhsc
);
4350 process_all_all_constraints (lhsc
, rhsc
);
4356 CASE_BUILT_IN_TM_STORE (1):
4357 CASE_BUILT_IN_TM_STORE (2):
4358 CASE_BUILT_IN_TM_STORE (4):
4359 CASE_BUILT_IN_TM_STORE (8):
4360 CASE_BUILT_IN_TM_STORE (FLOAT
):
4361 CASE_BUILT_IN_TM_STORE (DOUBLE
):
4362 CASE_BUILT_IN_TM_STORE (LDOUBLE
):
4363 CASE_BUILT_IN_TM_STORE (M64
):
4364 CASE_BUILT_IN_TM_STORE (M128
):
4365 CASE_BUILT_IN_TM_STORE (M256
):
4367 tree addr
= gimple_call_arg (t
, 0);
4368 tree src
= gimple_call_arg (t
, 1);
4370 get_constraint_for (addr
, &lhsc
);
4372 get_constraint_for (src
, &rhsc
);
4373 process_all_all_constraints (lhsc
, rhsc
);
4378 CASE_BUILT_IN_TM_LOAD (1):
4379 CASE_BUILT_IN_TM_LOAD (2):
4380 CASE_BUILT_IN_TM_LOAD (4):
4381 CASE_BUILT_IN_TM_LOAD (8):
4382 CASE_BUILT_IN_TM_LOAD (FLOAT
):
4383 CASE_BUILT_IN_TM_LOAD (DOUBLE
):
4384 CASE_BUILT_IN_TM_LOAD (LDOUBLE
):
4385 CASE_BUILT_IN_TM_LOAD (M64
):
4386 CASE_BUILT_IN_TM_LOAD (M128
):
4387 CASE_BUILT_IN_TM_LOAD (M256
):
4389 tree dest
= gimple_call_lhs (t
);
4390 tree addr
= gimple_call_arg (t
, 0);
4392 get_constraint_for (dest
, &lhsc
);
4393 get_constraint_for (addr
, &rhsc
);
4395 process_all_all_constraints (lhsc
, rhsc
);
4400 /* Variadic argument handling needs to be handled in IPA
4402 case BUILT_IN_VA_START
:
4404 tree valist
= gimple_call_arg (t
, 0);
4405 struct constraint_expr rhs
, *lhsp
;
4407 get_constraint_for (valist
, &lhsc
);
4409 /* The va_list gets access to pointers in variadic
4410 arguments. Which we know in the case of IPA analysis
4411 and otherwise are just all nonlocal variables. */
4414 fi
= lookup_vi_for_tree (cfun
->decl
);
4415 rhs
= get_function_part_constraint (fi
, ~0);
4416 rhs
.type
= ADDRESSOF
;
4420 rhs
.var
= nonlocal_id
;
4421 rhs
.type
= ADDRESSOF
;
4424 FOR_EACH_VEC_ELT (lhsc
, i
, lhsp
)
4425 process_constraint (new_constraint (*lhsp
, rhs
));
4427 /* va_list is clobbered. */
4428 make_constraint_to (get_call_clobber_vi (t
)->id
, valist
);
4431 /* va_end doesn't have any effect that matters. */
4432 case BUILT_IN_VA_END
:
4434 /* Alternate return. Simply give up for now. */
4435 case BUILT_IN_RETURN
:
4439 || !(fi
= get_vi_for_tree (cfun
->decl
)))
4440 make_constraint_from (get_varinfo (escaped_id
), anything_id
);
4441 else if (in_ipa_mode
4444 struct constraint_expr lhs
, rhs
;
4445 lhs
= get_function_part_constraint (fi
, fi_result
);
4446 rhs
.var
= anything_id
;
4449 process_constraint (new_constraint (lhs
, rhs
));
4453 /* printf-style functions may have hooks to set pointers to
4454 point to somewhere into the generated string. Leave them
4455 for a later exercise... */
4457 /* Fallthru to general call handling. */;
4463 /* Create constraints for the call T. */
4466 find_func_aliases_for_call (gimple t
)
4468 tree fndecl
= gimple_call_fndecl (t
);
4469 vec
<ce_s
> lhsc
= vNULL
;
4470 vec
<ce_s
> rhsc
= vNULL
;
4473 if (fndecl
!= NULL_TREE
4474 && DECL_BUILT_IN (fndecl
)
4475 && find_func_aliases_for_builtin_call (t
))
4478 fi
= get_fi_for_callee (t
);
4480 || (fndecl
&& !fi
->is_fn_info
))
4482 vec
<ce_s
> rhsc
= vNULL
;
4483 int flags
= gimple_call_flags (t
);
4485 /* Const functions can return their arguments and addresses
4486 of global memory but not of escaped memory. */
4487 if (flags
& (ECF_CONST
|ECF_NOVOPS
))
4489 if (gimple_call_lhs (t
))
4490 handle_const_call (t
, &rhsc
);
4492 /* Pure functions can return addresses in and of memory
4493 reachable from their arguments, but they are not an escape
4494 point for reachable memory of their arguments. */
4495 else if (flags
& (ECF_PURE
|ECF_LOOPING_CONST_OR_PURE
))
4496 handle_pure_call (t
, &rhsc
);
4498 handle_rhs_call (t
, &rhsc
);
4499 if (gimple_call_lhs (t
))
4500 handle_lhs_call (t
, gimple_call_lhs (t
), flags
, rhsc
, fndecl
);
4508 /* Assign all the passed arguments to the appropriate incoming
4509 parameters of the function. */
4510 for (j
= 0; j
< gimple_call_num_args (t
); j
++)
4512 struct constraint_expr lhs
;
4513 struct constraint_expr
*rhsp
;
4514 tree arg
= gimple_call_arg (t
, j
);
4516 get_constraint_for_rhs (arg
, &rhsc
);
4517 lhs
= get_function_part_constraint (fi
, fi_parm_base
+ j
);
4518 while (rhsc
.length () != 0)
4520 rhsp
= &rhsc
.last ();
4521 process_constraint (new_constraint (lhs
, *rhsp
));
4526 /* If we are returning a value, assign it to the result. */
4527 lhsop
= gimple_call_lhs (t
);
4530 struct constraint_expr rhs
;
4531 struct constraint_expr
*lhsp
;
4533 get_constraint_for (lhsop
, &lhsc
);
4534 rhs
= get_function_part_constraint (fi
, fi_result
);
4536 && DECL_RESULT (fndecl
)
4537 && DECL_BY_REFERENCE (DECL_RESULT (fndecl
)))
4539 vec
<ce_s
> tem
= vNULL
;
4540 tem
.safe_push (rhs
);
4545 FOR_EACH_VEC_ELT (lhsc
, j
, lhsp
)
4546 process_constraint (new_constraint (*lhsp
, rhs
));
4549 /* If we pass the result decl by reference, honor that. */
4552 && DECL_RESULT (fndecl
)
4553 && DECL_BY_REFERENCE (DECL_RESULT (fndecl
)))
4555 struct constraint_expr lhs
;
4556 struct constraint_expr
*rhsp
;
4558 get_constraint_for_address_of (lhsop
, &rhsc
);
4559 lhs
= get_function_part_constraint (fi
, fi_result
);
4560 FOR_EACH_VEC_ELT (rhsc
, j
, rhsp
)
4561 process_constraint (new_constraint (lhs
, *rhsp
));
4565 /* If we use a static chain, pass it along. */
4566 if (gimple_call_chain (t
))
4568 struct constraint_expr lhs
;
4569 struct constraint_expr
*rhsp
;
4571 get_constraint_for (gimple_call_chain (t
), &rhsc
);
4572 lhs
= get_function_part_constraint (fi
, fi_static_chain
);
4573 FOR_EACH_VEC_ELT (rhsc
, j
, rhsp
)
4574 process_constraint (new_constraint (lhs
, *rhsp
));
4579 /* Walk statement T setting up aliasing constraints according to the
4580 references found in T. This function is the main part of the
4581 constraint builder. AI points to auxiliary alias information used
4582 when building alias sets and computing alias grouping heuristics. */
4585 find_func_aliases (gimple origt
)
4588 vec
<ce_s
> lhsc
= vNULL
;
4589 vec
<ce_s
> rhsc
= vNULL
;
4590 struct constraint_expr
*c
;
4593 /* Now build constraints expressions. */
4594 if (gimple_code (t
) == GIMPLE_PHI
)
4599 /* For a phi node, assign all the arguments to
4601 get_constraint_for (gimple_phi_result (t
), &lhsc
);
4602 for (i
= 0; i
< gimple_phi_num_args (t
); i
++)
4604 tree strippedrhs
= PHI_ARG_DEF (t
, i
);
4606 STRIP_NOPS (strippedrhs
);
4607 get_constraint_for_rhs (gimple_phi_arg_def (t
, i
), &rhsc
);
4609 FOR_EACH_VEC_ELT (lhsc
, j
, c
)
4611 struct constraint_expr
*c2
;
4612 while (rhsc
.length () > 0)
4615 process_constraint (new_constraint (*c
, *c2
));
4621 /* In IPA mode, we need to generate constraints to pass call
4622 arguments through their calls. There are two cases,
4623 either a GIMPLE_CALL returning a value, or just a plain
4624 GIMPLE_CALL when we are not.
4626 In non-ipa mode, we need to generate constraints for each
4627 pointer passed by address. */
4628 else if (is_gimple_call (t
))
4629 find_func_aliases_for_call (t
);
4631 /* Otherwise, just a regular assignment statement. Only care about
4632 operations with pointer result, others are dealt with as escape
4633 points if they have pointer operands. */
4634 else if (is_gimple_assign (t
))
4636 /* Otherwise, just a regular assignment statement. */
4637 tree lhsop
= gimple_assign_lhs (t
);
4638 tree rhsop
= (gimple_num_ops (t
) == 2) ? gimple_assign_rhs1 (t
) : NULL
;
4640 if (rhsop
&& TREE_CLOBBER_P (rhsop
))
4641 /* Ignore clobbers, they don't actually store anything into
4644 else if (rhsop
&& AGGREGATE_TYPE_P (TREE_TYPE (lhsop
)))
4645 do_structure_copy (lhsop
, rhsop
);
4648 enum tree_code code
= gimple_assign_rhs_code (t
);
4650 get_constraint_for (lhsop
, &lhsc
);
4652 if (FLOAT_TYPE_P (TREE_TYPE (lhsop
)))
4653 /* If the operation produces a floating point result then
4654 assume the value is not produced to transfer a pointer. */
4656 else if (code
== POINTER_PLUS_EXPR
)
4657 get_constraint_for_ptr_offset (gimple_assign_rhs1 (t
),
4658 gimple_assign_rhs2 (t
), &rhsc
);
4659 else if (code
== BIT_AND_EXPR
4660 && TREE_CODE (gimple_assign_rhs2 (t
)) == INTEGER_CST
)
4662 /* Aligning a pointer via a BIT_AND_EXPR is offsetting
4663 the pointer. Handle it by offsetting it by UNKNOWN. */
4664 get_constraint_for_ptr_offset (gimple_assign_rhs1 (t
),
4667 else if ((CONVERT_EXPR_CODE_P (code
)
4668 && !(POINTER_TYPE_P (gimple_expr_type (t
))
4669 && !POINTER_TYPE_P (TREE_TYPE (rhsop
))))
4670 || gimple_assign_single_p (t
))
4671 get_constraint_for_rhs (rhsop
, &rhsc
);
4672 else if (code
== COND_EXPR
)
4674 /* The result is a merge of both COND_EXPR arms. */
4675 vec
<ce_s
> tmp
= vNULL
;
4676 struct constraint_expr
*rhsp
;
4678 get_constraint_for_rhs (gimple_assign_rhs2 (t
), &rhsc
);
4679 get_constraint_for_rhs (gimple_assign_rhs3 (t
), &tmp
);
4680 FOR_EACH_VEC_ELT (tmp
, i
, rhsp
)
4681 rhsc
.safe_push (*rhsp
);
4684 else if (truth_value_p (code
))
4685 /* Truth value results are not pointer (parts). Or at least
4686 very very unreasonable obfuscation of a part. */
4690 /* All other operations are merges. */
4691 vec
<ce_s
> tmp
= vNULL
;
4692 struct constraint_expr
*rhsp
;
4694 get_constraint_for_rhs (gimple_assign_rhs1 (t
), &rhsc
);
4695 for (i
= 2; i
< gimple_num_ops (t
); ++i
)
4697 get_constraint_for_rhs (gimple_op (t
, i
), &tmp
);
4698 FOR_EACH_VEC_ELT (tmp
, j
, rhsp
)
4699 rhsc
.safe_push (*rhsp
);
4704 process_all_all_constraints (lhsc
, rhsc
);
4706 /* If there is a store to a global variable the rhs escapes. */
4707 if ((lhsop
= get_base_address (lhsop
)) != NULL_TREE
4709 && is_global_var (lhsop
)
4711 || DECL_EXTERNAL (lhsop
) || TREE_PUBLIC (lhsop
)))
4712 make_escape_constraint (rhsop
);
4714 /* Handle escapes through return. */
4715 else if (gimple_code (t
) == GIMPLE_RETURN
4716 && gimple_return_retval (t
) != NULL_TREE
)
4720 || !(fi
= get_vi_for_tree (cfun
->decl
)))
4721 make_escape_constraint (gimple_return_retval (t
));
4722 else if (in_ipa_mode
4725 struct constraint_expr lhs
;
4726 struct constraint_expr
*rhsp
;
4729 lhs
= get_function_part_constraint (fi
, fi_result
);
4730 get_constraint_for_rhs (gimple_return_retval (t
), &rhsc
);
4731 FOR_EACH_VEC_ELT (rhsc
, i
, rhsp
)
4732 process_constraint (new_constraint (lhs
, *rhsp
));
4735 /* Handle asms conservatively by adding escape constraints to everything. */
4736 else if (gimple_code (t
) == GIMPLE_ASM
)
4738 unsigned i
, noutputs
;
4739 const char **oconstraints
;
4740 const char *constraint
;
4741 bool allows_mem
, allows_reg
, is_inout
;
4743 noutputs
= gimple_asm_noutputs (t
);
4744 oconstraints
= XALLOCAVEC (const char *, noutputs
);
4746 for (i
= 0; i
< noutputs
; ++i
)
4748 tree link
= gimple_asm_output_op (t
, i
);
4749 tree op
= TREE_VALUE (link
);
4751 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link
)));
4752 oconstraints
[i
] = constraint
;
4753 parse_output_constraint (&constraint
, i
, 0, 0, &allows_mem
,
4754 &allows_reg
, &is_inout
);
4756 /* A memory constraint makes the address of the operand escape. */
4757 if (!allows_reg
&& allows_mem
)
4758 make_escape_constraint (build_fold_addr_expr (op
));
4760 /* The asm may read global memory, so outputs may point to
4761 any global memory. */
4764 vec
<ce_s
> lhsc
= vNULL
;
4765 struct constraint_expr rhsc
, *lhsp
;
4767 get_constraint_for (op
, &lhsc
);
4768 rhsc
.var
= nonlocal_id
;
4771 FOR_EACH_VEC_ELT (lhsc
, j
, lhsp
)
4772 process_constraint (new_constraint (*lhsp
, rhsc
));
4776 for (i
= 0; i
< gimple_asm_ninputs (t
); ++i
)
4778 tree link
= gimple_asm_input_op (t
, i
);
4779 tree op
= TREE_VALUE (link
);
4781 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link
)));
4783 parse_input_constraint (&constraint
, 0, 0, noutputs
, 0, oconstraints
,
4784 &allows_mem
, &allows_reg
);
4786 /* A memory constraint makes the address of the operand escape. */
4787 if (!allows_reg
&& allows_mem
)
4788 make_escape_constraint (build_fold_addr_expr (op
));
4789 /* Strictly we'd only need the constraint to ESCAPED if
4790 the asm clobbers memory, otherwise using something
4791 along the lines of per-call clobbers/uses would be enough. */
4793 make_escape_constraint (op
);
4802 /* Create a constraint adding to the clobber set of FI the memory
4803 pointed to by PTR. */
4806 process_ipa_clobber (varinfo_t fi
, tree ptr
)
4808 vec
<ce_s
> ptrc
= vNULL
;
4809 struct constraint_expr
*c
, lhs
;
4811 get_constraint_for_rhs (ptr
, &ptrc
);
4812 lhs
= get_function_part_constraint (fi
, fi_clobbers
);
4813 FOR_EACH_VEC_ELT (ptrc
, i
, c
)
4814 process_constraint (new_constraint (lhs
, *c
));
4818 /* Walk statement T setting up clobber and use constraints according to the
4819 references found in T. This function is a main part of the
4820 IPA constraint builder. */
4823 find_func_clobbers (gimple origt
)
4826 vec
<ce_s
> lhsc
= vNULL
;
4827 auto_vec
<ce_s
> rhsc
;
4830 /* Add constraints for clobbered/used in IPA mode.
4831 We are not interested in what automatic variables are clobbered
4832 or used as we only use the information in the caller to which
4833 they do not escape. */
4834 gcc_assert (in_ipa_mode
);
4836 /* If the stmt refers to memory in any way it better had a VUSE. */
4837 if (gimple_vuse (t
) == NULL_TREE
)
4840 /* We'd better have function information for the current function. */
4841 fi
= lookup_vi_for_tree (cfun
->decl
);
4842 gcc_assert (fi
!= NULL
);
4844 /* Account for stores in assignments and calls. */
4845 if (gimple_vdef (t
) != NULL_TREE
4846 && gimple_has_lhs (t
))
4848 tree lhs
= gimple_get_lhs (t
);
4850 while (handled_component_p (tem
))
4851 tem
= TREE_OPERAND (tem
, 0);
4853 && !auto_var_in_fn_p (tem
, cfun
->decl
))
4854 || INDIRECT_REF_P (tem
)
4855 || (TREE_CODE (tem
) == MEM_REF
4856 && !(TREE_CODE (TREE_OPERAND (tem
, 0)) == ADDR_EXPR
4858 (TREE_OPERAND (TREE_OPERAND (tem
, 0), 0), cfun
->decl
))))
4860 struct constraint_expr lhsc
, *rhsp
;
4862 lhsc
= get_function_part_constraint (fi
, fi_clobbers
);
4863 get_constraint_for_address_of (lhs
, &rhsc
);
4864 FOR_EACH_VEC_ELT (rhsc
, i
, rhsp
)
4865 process_constraint (new_constraint (lhsc
, *rhsp
));
4870 /* Account for uses in assigments and returns. */
4871 if (gimple_assign_single_p (t
)
4872 || (gimple_code (t
) == GIMPLE_RETURN
4873 && gimple_return_retval (t
) != NULL_TREE
))
4875 tree rhs
= (gimple_assign_single_p (t
)
4876 ? gimple_assign_rhs1 (t
) : gimple_return_retval (t
));
4878 while (handled_component_p (tem
))
4879 tem
= TREE_OPERAND (tem
, 0);
4881 && !auto_var_in_fn_p (tem
, cfun
->decl
))
4882 || INDIRECT_REF_P (tem
)
4883 || (TREE_CODE (tem
) == MEM_REF
4884 && !(TREE_CODE (TREE_OPERAND (tem
, 0)) == ADDR_EXPR
4886 (TREE_OPERAND (TREE_OPERAND (tem
, 0), 0), cfun
->decl
))))
4888 struct constraint_expr lhs
, *rhsp
;
4890 lhs
= get_function_part_constraint (fi
, fi_uses
);
4891 get_constraint_for_address_of (rhs
, &rhsc
);
4892 FOR_EACH_VEC_ELT (rhsc
, i
, rhsp
)
4893 process_constraint (new_constraint (lhs
, *rhsp
));
4898 if (is_gimple_call (t
))
4900 varinfo_t cfi
= NULL
;
4901 tree decl
= gimple_call_fndecl (t
);
4902 struct constraint_expr lhs
, rhs
;
4905 /* For builtins we do not have separate function info. For those
4906 we do not generate escapes for we have to generate clobbers/uses. */
4907 if (gimple_call_builtin_p (t
, BUILT_IN_NORMAL
))
4908 switch (DECL_FUNCTION_CODE (decl
))
4910 /* The following functions use and clobber memory pointed to
4911 by their arguments. */
4912 case BUILT_IN_STRCPY
:
4913 case BUILT_IN_STRNCPY
:
4914 case BUILT_IN_BCOPY
:
4915 case BUILT_IN_MEMCPY
:
4916 case BUILT_IN_MEMMOVE
:
4917 case BUILT_IN_MEMPCPY
:
4918 case BUILT_IN_STPCPY
:
4919 case BUILT_IN_STPNCPY
:
4920 case BUILT_IN_STRCAT
:
4921 case BUILT_IN_STRNCAT
:
4922 case BUILT_IN_STRCPY_CHK
:
4923 case BUILT_IN_STRNCPY_CHK
:
4924 case BUILT_IN_MEMCPY_CHK
:
4925 case BUILT_IN_MEMMOVE_CHK
:
4926 case BUILT_IN_MEMPCPY_CHK
:
4927 case BUILT_IN_STPCPY_CHK
:
4928 case BUILT_IN_STPNCPY_CHK
:
4929 case BUILT_IN_STRCAT_CHK
:
4930 case BUILT_IN_STRNCAT_CHK
:
4932 tree dest
= gimple_call_arg (t
, (DECL_FUNCTION_CODE (decl
)
4933 == BUILT_IN_BCOPY
? 1 : 0));
4934 tree src
= gimple_call_arg (t
, (DECL_FUNCTION_CODE (decl
)
4935 == BUILT_IN_BCOPY
? 0 : 1));
4937 struct constraint_expr
*rhsp
, *lhsp
;
4938 get_constraint_for_ptr_offset (dest
, NULL_TREE
, &lhsc
);
4939 lhs
= get_function_part_constraint (fi
, fi_clobbers
);
4940 FOR_EACH_VEC_ELT (lhsc
, i
, lhsp
)
4941 process_constraint (new_constraint (lhs
, *lhsp
));
4943 get_constraint_for_ptr_offset (src
, NULL_TREE
, &rhsc
);
4944 lhs
= get_function_part_constraint (fi
, fi_uses
);
4945 FOR_EACH_VEC_ELT (rhsc
, i
, rhsp
)
4946 process_constraint (new_constraint (lhs
, *rhsp
));
4950 /* The following function clobbers memory pointed to by
4952 case BUILT_IN_MEMSET
:
4953 case BUILT_IN_MEMSET_CHK
:
4955 tree dest
= gimple_call_arg (t
, 0);
4958 get_constraint_for_ptr_offset (dest
, NULL_TREE
, &lhsc
);
4959 lhs
= get_function_part_constraint (fi
, fi_clobbers
);
4960 FOR_EACH_VEC_ELT (lhsc
, i
, lhsp
)
4961 process_constraint (new_constraint (lhs
, *lhsp
));
4965 /* The following functions clobber their second and third
4967 case BUILT_IN_SINCOS
:
4968 case BUILT_IN_SINCOSF
:
4969 case BUILT_IN_SINCOSL
:
4971 process_ipa_clobber (fi
, gimple_call_arg (t
, 1));
4972 process_ipa_clobber (fi
, gimple_call_arg (t
, 2));
4975 /* The following functions clobber their second argument. */
4976 case BUILT_IN_FREXP
:
4977 case BUILT_IN_FREXPF
:
4978 case BUILT_IN_FREXPL
:
4979 case BUILT_IN_LGAMMA_R
:
4980 case BUILT_IN_LGAMMAF_R
:
4981 case BUILT_IN_LGAMMAL_R
:
4982 case BUILT_IN_GAMMA_R
:
4983 case BUILT_IN_GAMMAF_R
:
4984 case BUILT_IN_GAMMAL_R
:
4986 case BUILT_IN_MODFF
:
4987 case BUILT_IN_MODFL
:
4989 process_ipa_clobber (fi
, gimple_call_arg (t
, 1));
4992 /* The following functions clobber their third argument. */
4993 case BUILT_IN_REMQUO
:
4994 case BUILT_IN_REMQUOF
:
4995 case BUILT_IN_REMQUOL
:
4997 process_ipa_clobber (fi
, gimple_call_arg (t
, 2));
5000 /* The following functions neither read nor clobber memory. */
5001 case BUILT_IN_ASSUME_ALIGNED
:
5004 /* Trampolines are of no interest to us. */
5005 case BUILT_IN_INIT_TRAMPOLINE
:
5006 case BUILT_IN_ADJUST_TRAMPOLINE
:
5008 case BUILT_IN_VA_START
:
5009 case BUILT_IN_VA_END
:
5011 /* printf-style functions may have hooks to set pointers to
5012 point to somewhere into the generated string. Leave them
5013 for a later exercise... */
5015 /* Fallthru to general call handling. */;
5018 /* Parameters passed by value are used. */
5019 lhs
= get_function_part_constraint (fi
, fi_uses
);
5020 for (i
= 0; i
< gimple_call_num_args (t
); i
++)
5022 struct constraint_expr
*rhsp
;
5023 tree arg
= gimple_call_arg (t
, i
);
5025 if (TREE_CODE (arg
) == SSA_NAME
5026 || is_gimple_min_invariant (arg
))
5029 get_constraint_for_address_of (arg
, &rhsc
);
5030 FOR_EACH_VEC_ELT (rhsc
, j
, rhsp
)
5031 process_constraint (new_constraint (lhs
, *rhsp
));
5035 /* Build constraints for propagating clobbers/uses along the
5037 cfi
= get_fi_for_callee (t
);
5038 if (cfi
->id
== anything_id
)
5040 if (gimple_vdef (t
))
5041 make_constraint_from (first_vi_for_offset (fi
, fi_clobbers
),
5043 make_constraint_from (first_vi_for_offset (fi
, fi_uses
),
5048 /* For callees without function info (that's external functions),
5049 ESCAPED is clobbered and used. */
5050 if (gimple_call_fndecl (t
)
5051 && !cfi
->is_fn_info
)
5055 if (gimple_vdef (t
))
5056 make_copy_constraint (first_vi_for_offset (fi
, fi_clobbers
),
5058 make_copy_constraint (first_vi_for_offset (fi
, fi_uses
), escaped_id
);
5060 /* Also honor the call statement use/clobber info. */
5061 if ((vi
= lookup_call_clobber_vi (t
)) != NULL
)
5062 make_copy_constraint (first_vi_for_offset (fi
, fi_clobbers
),
5064 if ((vi
= lookup_call_use_vi (t
)) != NULL
)
5065 make_copy_constraint (first_vi_for_offset (fi
, fi_uses
),
5070 /* Otherwise the caller clobbers and uses what the callee does.
5071 ??? This should use a new complex constraint that filters
5072 local variables of the callee. */
5073 if (gimple_vdef (t
))
5075 lhs
= get_function_part_constraint (fi
, fi_clobbers
);
5076 rhs
= get_function_part_constraint (cfi
, fi_clobbers
);
5077 process_constraint (new_constraint (lhs
, rhs
));
5079 lhs
= get_function_part_constraint (fi
, fi_uses
);
5080 rhs
= get_function_part_constraint (cfi
, fi_uses
);
5081 process_constraint (new_constraint (lhs
, rhs
));
5083 else if (gimple_code (t
) == GIMPLE_ASM
)
5085 /* ??? Ick. We can do better. */
5086 if (gimple_vdef (t
))
5087 make_constraint_from (first_vi_for_offset (fi
, fi_clobbers
),
5089 make_constraint_from (first_vi_for_offset (fi
, fi_uses
),
5095 /* Find the first varinfo in the same variable as START that overlaps with
5096 OFFSET. Return NULL if we can't find one. */
5099 first_vi_for_offset (varinfo_t start
, unsigned HOST_WIDE_INT offset
)
5101 /* If the offset is outside of the variable, bail out. */
5102 if (offset
>= start
->fullsize
)
5105 /* If we cannot reach offset from start, lookup the first field
5106 and start from there. */
5107 if (start
->offset
> offset
)
5108 start
= get_varinfo (start
->head
);
5112 /* We may not find a variable in the field list with the actual
5113 offset when when we have glommed a structure to a variable.
5114 In that case, however, offset should still be within the size
5116 if (offset
>= start
->offset
5117 && (offset
- start
->offset
) < start
->size
)
5120 start
= vi_next (start
);
5126 /* Find the first varinfo in the same variable as START that overlaps with
5127 OFFSET. If there is no such varinfo the varinfo directly preceding
5128 OFFSET is returned. */
5131 first_or_preceding_vi_for_offset (varinfo_t start
,
5132 unsigned HOST_WIDE_INT offset
)
5134 /* If we cannot reach offset from start, lookup the first field
5135 and start from there. */
5136 if (start
->offset
> offset
)
5137 start
= get_varinfo (start
->head
);
5139 /* We may not find a variable in the field list with the actual
5140 offset when when we have glommed a structure to a variable.
5141 In that case, however, offset should still be within the size
5143 If we got beyond the offset we look for return the field
5144 directly preceding offset which may be the last field. */
5146 && offset
>= start
->offset
5147 && !((offset
- start
->offset
) < start
->size
))
5148 start
= vi_next (start
);
5154 /* This structure is used during pushing fields onto the fieldstack
5155 to track the offset of the field, since bitpos_of_field gives it
5156 relative to its immediate containing type, and we want it relative
5157 to the ultimate containing object. */
5161 /* Offset from the base of the base containing object to this field. */
5162 HOST_WIDE_INT offset
;
5164 /* Size, in bits, of the field. */
5165 unsigned HOST_WIDE_INT size
;
5167 unsigned has_unknown_size
: 1;
5169 unsigned must_have_pointers
: 1;
5171 unsigned may_have_pointers
: 1;
5173 unsigned only_restrict_pointers
: 1;
5175 typedef struct fieldoff fieldoff_s
;
5178 /* qsort comparison function for two fieldoff's PA and PB */
5181 fieldoff_compare (const void *pa
, const void *pb
)
5183 const fieldoff_s
*foa
= (const fieldoff_s
*)pa
;
5184 const fieldoff_s
*fob
= (const fieldoff_s
*)pb
;
5185 unsigned HOST_WIDE_INT foasize
, fobsize
;
5187 if (foa
->offset
< fob
->offset
)
5189 else if (foa
->offset
> fob
->offset
)
5192 foasize
= foa
->size
;
5193 fobsize
= fob
->size
;
5194 if (foasize
< fobsize
)
5196 else if (foasize
> fobsize
)
5201 /* Sort a fieldstack according to the field offset and sizes. */
5203 sort_fieldstack (vec
<fieldoff_s
> fieldstack
)
5205 fieldstack
.qsort (fieldoff_compare
);
5208 /* Return true if T is a type that can have subvars. */
5211 type_can_have_subvars (const_tree t
)
5213 /* Aggregates without overlapping fields can have subvars. */
5214 return TREE_CODE (t
) == RECORD_TYPE
;
5217 /* Return true if V is a tree that we can have subvars for.
5218 Normally, this is any aggregate type. Also complex
5219 types which are not gimple registers can have subvars. */
5222 var_can_have_subvars (const_tree v
)
5224 /* Volatile variables should never have subvars. */
5225 if (TREE_THIS_VOLATILE (v
))
5228 /* Non decls or memory tags can never have subvars. */
5232 return type_can_have_subvars (TREE_TYPE (v
));
5235 /* Return true if T is a type that does contain pointers. */
5238 type_must_have_pointers (tree type
)
5240 if (POINTER_TYPE_P (type
))
5243 if (TREE_CODE (type
) == ARRAY_TYPE
)
5244 return type_must_have_pointers (TREE_TYPE (type
));
5246 /* A function or method can have pointers as arguments, so track
5247 those separately. */
5248 if (TREE_CODE (type
) == FUNCTION_TYPE
5249 || TREE_CODE (type
) == METHOD_TYPE
)
5256 field_must_have_pointers (tree t
)
5258 return type_must_have_pointers (TREE_TYPE (t
));
5261 /* Given a TYPE, and a vector of field offsets FIELDSTACK, push all
5262 the fields of TYPE onto fieldstack, recording their offsets along
5265 OFFSET is used to keep track of the offset in this entire
5266 structure, rather than just the immediately containing structure.
5267 Returns false if the caller is supposed to handle the field we
5271 push_fields_onto_fieldstack (tree type
, vec
<fieldoff_s
> *fieldstack
,
5272 HOST_WIDE_INT offset
)
5275 bool empty_p
= true;
5277 if (TREE_CODE (type
) != RECORD_TYPE
)
5280 /* If the vector of fields is growing too big, bail out early.
5281 Callers check for vec::length <= MAX_FIELDS_FOR_FIELD_SENSITIVE, make
5283 if (fieldstack
->length () > MAX_FIELDS_FOR_FIELD_SENSITIVE
)
5286 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
5287 if (TREE_CODE (field
) == FIELD_DECL
)
5290 HOST_WIDE_INT foff
= bitpos_of_field (field
);
5292 if (!var_can_have_subvars (field
)
5293 || TREE_CODE (TREE_TYPE (field
)) == QUAL_UNION_TYPE
5294 || TREE_CODE (TREE_TYPE (field
)) == UNION_TYPE
)
5296 else if (!push_fields_onto_fieldstack
5297 (TREE_TYPE (field
), fieldstack
, offset
+ foff
)
5298 && (DECL_SIZE (field
)
5299 && !integer_zerop (DECL_SIZE (field
))))
5300 /* Empty structures may have actual size, like in C++. So
5301 see if we didn't push any subfields and the size is
5302 nonzero, push the field onto the stack. */
5307 fieldoff_s
*pair
= NULL
;
5308 bool has_unknown_size
= false;
5309 bool must_have_pointers_p
;
5311 if (!fieldstack
->is_empty ())
5312 pair
= &fieldstack
->last ();
5314 /* If there isn't anything at offset zero, create sth. */
5316 && offset
+ foff
!= 0)
5318 fieldoff_s e
= {0, offset
+ foff
, false, false, false, false};
5319 pair
= fieldstack
->safe_push (e
);
5322 if (!DECL_SIZE (field
)
5323 || !tree_fits_uhwi_p (DECL_SIZE (field
)))
5324 has_unknown_size
= true;
5326 /* If adjacent fields do not contain pointers merge them. */
5327 must_have_pointers_p
= field_must_have_pointers (field
);
5329 && !has_unknown_size
5330 && !must_have_pointers_p
5331 && !pair
->must_have_pointers
5332 && !pair
->has_unknown_size
5333 && pair
->offset
+ (HOST_WIDE_INT
)pair
->size
== offset
+ foff
)
5335 pair
->size
+= tree_to_uhwi (DECL_SIZE (field
));
5340 e
.offset
= offset
+ foff
;
5341 e
.has_unknown_size
= has_unknown_size
;
5342 if (!has_unknown_size
)
5343 e
.size
= tree_to_uhwi (DECL_SIZE (field
));
5346 e
.must_have_pointers
= must_have_pointers_p
;
5347 e
.may_have_pointers
= true;
5348 e
.only_restrict_pointers
5349 = (!has_unknown_size
5350 && POINTER_TYPE_P (TREE_TYPE (field
))
5351 && TYPE_RESTRICT (TREE_TYPE (field
)));
5352 fieldstack
->safe_push (e
);
5362 /* Count the number of arguments DECL has, and set IS_VARARGS to true
5363 if it is a varargs function. */
5366 count_num_arguments (tree decl
, bool *is_varargs
)
5368 unsigned int num
= 0;
5371 /* Capture named arguments for K&R functions. They do not
5372 have a prototype and thus no TYPE_ARG_TYPES. */
5373 for (t
= DECL_ARGUMENTS (decl
); t
; t
= DECL_CHAIN (t
))
5376 /* Check if the function has variadic arguments. */
5377 for (t
= TYPE_ARG_TYPES (TREE_TYPE (decl
)); t
; t
= TREE_CHAIN (t
))
5378 if (TREE_VALUE (t
) == void_type_node
)
5386 /* Creation function node for DECL, using NAME, and return the index
5387 of the variable we've created for the function. */
5390 create_function_info_for (tree decl
, const char *name
)
5392 struct function
*fn
= DECL_STRUCT_FUNCTION (decl
);
5393 varinfo_t vi
, prev_vi
;
5396 bool is_varargs
= false;
5397 unsigned int num_args
= count_num_arguments (decl
, &is_varargs
);
5399 /* Create the variable info. */
5401 vi
= new_var_info (decl
, name
);
5404 vi
->fullsize
= fi_parm_base
+ num_args
;
5406 vi
->may_have_pointers
= false;
5409 insert_vi_for_tree (vi
->decl
, vi
);
5413 /* Create a variable for things the function clobbers and one for
5414 things the function uses. */
5416 varinfo_t clobbervi
, usevi
;
5417 const char *newname
;
5420 asprintf (&tempname
, "%s.clobber", name
);
5421 newname
= ggc_strdup (tempname
);
5424 clobbervi
= new_var_info (NULL
, newname
);
5425 clobbervi
->offset
= fi_clobbers
;
5426 clobbervi
->size
= 1;
5427 clobbervi
->fullsize
= vi
->fullsize
;
5428 clobbervi
->is_full_var
= true;
5429 clobbervi
->is_global_var
= false;
5430 gcc_assert (prev_vi
->offset
< clobbervi
->offset
);
5431 prev_vi
->next
= clobbervi
->id
;
5432 prev_vi
= clobbervi
;
5434 asprintf (&tempname
, "%s.use", name
);
5435 newname
= ggc_strdup (tempname
);
5438 usevi
= new_var_info (NULL
, newname
);
5439 usevi
->offset
= fi_uses
;
5441 usevi
->fullsize
= vi
->fullsize
;
5442 usevi
->is_full_var
= true;
5443 usevi
->is_global_var
= false;
5444 gcc_assert (prev_vi
->offset
< usevi
->offset
);
5445 prev_vi
->next
= usevi
->id
;
5449 /* And one for the static chain. */
5450 if (fn
->static_chain_decl
!= NULL_TREE
)
5453 const char *newname
;
5456 asprintf (&tempname
, "%s.chain", name
);
5457 newname
= ggc_strdup (tempname
);
5460 chainvi
= new_var_info (fn
->static_chain_decl
, newname
);
5461 chainvi
->offset
= fi_static_chain
;
5463 chainvi
->fullsize
= vi
->fullsize
;
5464 chainvi
->is_full_var
= true;
5465 chainvi
->is_global_var
= false;
5466 gcc_assert (prev_vi
->offset
< chainvi
->offset
);
5467 prev_vi
->next
= chainvi
->id
;
5469 insert_vi_for_tree (fn
->static_chain_decl
, chainvi
);
5472 /* Create a variable for the return var. */
5473 if (DECL_RESULT (decl
) != NULL
5474 || !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl
))))
5477 const char *newname
;
5479 tree resultdecl
= decl
;
5481 if (DECL_RESULT (decl
))
5482 resultdecl
= DECL_RESULT (decl
);
5484 asprintf (&tempname
, "%s.result", name
);
5485 newname
= ggc_strdup (tempname
);
5488 resultvi
= new_var_info (resultdecl
, newname
);
5489 resultvi
->offset
= fi_result
;
5491 resultvi
->fullsize
= vi
->fullsize
;
5492 resultvi
->is_full_var
= true;
5493 if (DECL_RESULT (decl
))
5494 resultvi
->may_have_pointers
= true;
5495 gcc_assert (prev_vi
->offset
< resultvi
->offset
);
5496 prev_vi
->next
= resultvi
->id
;
5498 if (DECL_RESULT (decl
))
5499 insert_vi_for_tree (DECL_RESULT (decl
), resultvi
);
5502 /* Set up variables for each argument. */
5503 arg
= DECL_ARGUMENTS (decl
);
5504 for (i
= 0; i
< num_args
; i
++)
5507 const char *newname
;
5509 tree argdecl
= decl
;
5514 asprintf (&tempname
, "%s.arg%d", name
, i
);
5515 newname
= ggc_strdup (tempname
);
5518 argvi
= new_var_info (argdecl
, newname
);
5519 argvi
->offset
= fi_parm_base
+ i
;
5521 argvi
->is_full_var
= true;
5522 argvi
->fullsize
= vi
->fullsize
;
5524 argvi
->may_have_pointers
= true;
5525 gcc_assert (prev_vi
->offset
< argvi
->offset
);
5526 prev_vi
->next
= argvi
->id
;
5530 insert_vi_for_tree (arg
, argvi
);
5531 arg
= DECL_CHAIN (arg
);
5535 /* Add one representative for all further args. */
5539 const char *newname
;
5543 asprintf (&tempname
, "%s.varargs", name
);
5544 newname
= ggc_strdup (tempname
);
5547 /* We need sth that can be pointed to for va_start. */
5548 decl
= build_fake_var_decl (ptr_type_node
);
5550 argvi
= new_var_info (decl
, newname
);
5551 argvi
->offset
= fi_parm_base
+ num_args
;
5553 argvi
->is_full_var
= true;
5554 argvi
->is_heap_var
= true;
5555 argvi
->fullsize
= vi
->fullsize
;
5556 gcc_assert (prev_vi
->offset
< argvi
->offset
);
5557 prev_vi
->next
= argvi
->id
;
5565 /* Return true if FIELDSTACK contains fields that overlap.
5566 FIELDSTACK is assumed to be sorted by offset. */
5569 check_for_overlaps (vec
<fieldoff_s
> fieldstack
)
5571 fieldoff_s
*fo
= NULL
;
5573 HOST_WIDE_INT lastoffset
= -1;
5575 FOR_EACH_VEC_ELT (fieldstack
, i
, fo
)
5577 if (fo
->offset
== lastoffset
)
5579 lastoffset
= fo
->offset
;
5584 /* Create a varinfo structure for NAME and DECL, and add it to VARMAP.
5585 This will also create any varinfo structures necessary for fields
5589 create_variable_info_for_1 (tree decl
, const char *name
)
5591 varinfo_t vi
, newvi
;
5592 tree decl_type
= TREE_TYPE (decl
);
5593 tree declsize
= DECL_P (decl
) ? DECL_SIZE (decl
) : TYPE_SIZE (decl_type
);
5594 auto_vec
<fieldoff_s
> fieldstack
;
5599 || !tree_fits_uhwi_p (declsize
))
5601 vi
= new_var_info (decl
, name
);
5605 vi
->is_unknown_size_var
= true;
5606 vi
->is_full_var
= true;
5607 vi
->may_have_pointers
= true;
5611 /* Collect field information. */
5612 if (use_field_sensitive
5613 && var_can_have_subvars (decl
)
5614 /* ??? Force us to not use subfields for global initializers
5615 in IPA mode. Else we'd have to parse arbitrary initializers. */
5617 && is_global_var (decl
)
5618 && DECL_INITIAL (decl
)))
5620 fieldoff_s
*fo
= NULL
;
5621 bool notokay
= false;
5624 push_fields_onto_fieldstack (decl_type
, &fieldstack
, 0);
5626 for (i
= 0; !notokay
&& fieldstack
.iterate (i
, &fo
); i
++)
5627 if (fo
->has_unknown_size
5634 /* We can't sort them if we have a field with a variable sized type,
5635 which will make notokay = true. In that case, we are going to return
5636 without creating varinfos for the fields anyway, so sorting them is a
5640 sort_fieldstack (fieldstack
);
5641 /* Due to some C++ FE issues, like PR 22488, we might end up
5642 what appear to be overlapping fields even though they,
5643 in reality, do not overlap. Until the C++ FE is fixed,
5644 we will simply disable field-sensitivity for these cases. */
5645 notokay
= check_for_overlaps (fieldstack
);
5649 fieldstack
.release ();
5652 /* If we didn't end up collecting sub-variables create a full
5653 variable for the decl. */
5654 if (fieldstack
.length () <= 1
5655 || fieldstack
.length () > MAX_FIELDS_FOR_FIELD_SENSITIVE
)
5657 vi
= new_var_info (decl
, name
);
5659 vi
->may_have_pointers
= true;
5660 vi
->fullsize
= tree_to_uhwi (declsize
);
5661 vi
->size
= vi
->fullsize
;
5662 vi
->is_full_var
= true;
5663 fieldstack
.release ();
5667 vi
= new_var_info (decl
, name
);
5668 vi
->fullsize
= tree_to_uhwi (declsize
);
5669 for (i
= 0, newvi
= vi
;
5670 fieldstack
.iterate (i
, &fo
);
5671 ++i
, newvi
= vi_next (newvi
))
5673 const char *newname
= "NULL";
5678 asprintf (&tempname
, "%s." HOST_WIDE_INT_PRINT_DEC
5679 "+" HOST_WIDE_INT_PRINT_DEC
, name
, fo
->offset
, fo
->size
);
5680 newname
= ggc_strdup (tempname
);
5683 newvi
->name
= newname
;
5684 newvi
->offset
= fo
->offset
;
5685 newvi
->size
= fo
->size
;
5686 newvi
->fullsize
= vi
->fullsize
;
5687 newvi
->may_have_pointers
= fo
->may_have_pointers
;
5688 newvi
->only_restrict_pointers
= fo
->only_restrict_pointers
;
5689 if (i
+ 1 < fieldstack
.length ())
5691 varinfo_t tem
= new_var_info (decl
, name
);
5692 newvi
->next
= tem
->id
;
5701 create_variable_info_for (tree decl
, const char *name
)
5703 varinfo_t vi
= create_variable_info_for_1 (decl
, name
);
5704 unsigned int id
= vi
->id
;
5706 insert_vi_for_tree (decl
, vi
);
5708 if (TREE_CODE (decl
) != VAR_DECL
)
5711 /* Create initial constraints for globals. */
5712 for (; vi
; vi
= vi_next (vi
))
5714 if (!vi
->may_have_pointers
5715 || !vi
->is_global_var
)
5718 /* Mark global restrict qualified pointers. */
5719 if ((POINTER_TYPE_P (TREE_TYPE (decl
))
5720 && TYPE_RESTRICT (TREE_TYPE (decl
)))
5721 || vi
->only_restrict_pointers
)
5723 make_constraint_from_global_restrict (vi
, "GLOBAL_RESTRICT");
5727 /* In non-IPA mode the initializer from nonlocal is all we need. */
5729 || DECL_HARD_REGISTER (decl
))
5730 make_copy_constraint (vi
, nonlocal_id
);
5732 /* In IPA mode parse the initializer and generate proper constraints
5736 struct varpool_node
*vnode
= varpool_get_node (decl
);
5738 /* For escaped variables initialize them from nonlocal. */
5739 if (!varpool_all_refs_explicit_p (vnode
))
5740 make_copy_constraint (vi
, nonlocal_id
);
5742 /* If this is a global variable with an initializer and we are in
5743 IPA mode generate constraints for it. */
5744 if (DECL_INITIAL (decl
)
5745 && vnode
->definition
)
5747 auto_vec
<ce_s
> rhsc
;
5748 struct constraint_expr lhs
, *rhsp
;
5750 get_constraint_for_rhs (DECL_INITIAL (decl
), &rhsc
);
5754 FOR_EACH_VEC_ELT (rhsc
, i
, rhsp
)
5755 process_constraint (new_constraint (lhs
, *rhsp
));
5756 /* If this is a variable that escapes from the unit
5757 the initializer escapes as well. */
5758 if (!varpool_all_refs_explicit_p (vnode
))
5760 lhs
.var
= escaped_id
;
5763 FOR_EACH_VEC_ELT (rhsc
, i
, rhsp
)
5764 process_constraint (new_constraint (lhs
, *rhsp
));
5773 /* Print out the points-to solution for VAR to FILE. */
5776 dump_solution_for_var (FILE *file
, unsigned int var
)
5778 varinfo_t vi
= get_varinfo (var
);
5782 /* Dump the solution for unified vars anyway, this avoids difficulties
5783 in scanning dumps in the testsuite. */
5784 fprintf (file
, "%s = { ", vi
->name
);
5785 vi
= get_varinfo (find (var
));
5786 EXECUTE_IF_SET_IN_BITMAP (vi
->solution
, 0, i
, bi
)
5787 fprintf (file
, "%s ", get_varinfo (i
)->name
);
5788 fprintf (file
, "}");
5790 /* But note when the variable was unified. */
5792 fprintf (file
, " same as %s", vi
->name
);
5794 fprintf (file
, "\n");
5797 /* Print the points-to solution for VAR to stdout. */
5800 debug_solution_for_var (unsigned int var
)
5802 dump_solution_for_var (stdout
, var
);
5805 /* Create varinfo structures for all of the variables in the
5806 function for intraprocedural mode. */
5809 intra_create_variable_infos (void)
5813 /* For each incoming pointer argument arg, create the constraint ARG
5814 = NONLOCAL or a dummy variable if it is a restrict qualified
5815 passed-by-reference argument. */
5816 for (t
= DECL_ARGUMENTS (current_function_decl
); t
; t
= DECL_CHAIN (t
))
5818 varinfo_t p
= get_vi_for_tree (t
);
5820 /* For restrict qualified pointers to objects passed by
5821 reference build a real representative for the pointed-to object.
5822 Treat restrict qualified references the same. */
5823 if (TYPE_RESTRICT (TREE_TYPE (t
))
5824 && ((DECL_BY_REFERENCE (t
) && POINTER_TYPE_P (TREE_TYPE (t
)))
5825 || TREE_CODE (TREE_TYPE (t
)) == REFERENCE_TYPE
)
5826 && !type_contains_placeholder_p (TREE_TYPE (TREE_TYPE (t
))))
5828 struct constraint_expr lhsc
, rhsc
;
5830 tree heapvar
= build_fake_var_decl (TREE_TYPE (TREE_TYPE (t
)));
5831 DECL_EXTERNAL (heapvar
) = 1;
5832 vi
= create_variable_info_for_1 (heapvar
, "PARM_NOALIAS");
5833 insert_vi_for_tree (heapvar
, vi
);
5838 rhsc
.type
= ADDRESSOF
;
5840 process_constraint (new_constraint (lhsc
, rhsc
));
5841 for (; vi
; vi
= vi_next (vi
))
5842 if (vi
->may_have_pointers
)
5844 if (vi
->only_restrict_pointers
)
5845 make_constraint_from_global_restrict (vi
, "GLOBAL_RESTRICT");
5847 make_copy_constraint (vi
, nonlocal_id
);
5852 if (POINTER_TYPE_P (TREE_TYPE (t
))
5853 && TYPE_RESTRICT (TREE_TYPE (t
)))
5854 make_constraint_from_global_restrict (p
, "PARM_RESTRICT");
5857 for (; p
; p
= vi_next (p
))
5859 if (p
->only_restrict_pointers
)
5860 make_constraint_from_global_restrict (p
, "PARM_RESTRICT");
5861 else if (p
->may_have_pointers
)
5862 make_constraint_from (p
, nonlocal_id
);
5867 /* Add a constraint for a result decl that is passed by reference. */
5868 if (DECL_RESULT (cfun
->decl
)
5869 && DECL_BY_REFERENCE (DECL_RESULT (cfun
->decl
)))
5871 varinfo_t p
, result_vi
= get_vi_for_tree (DECL_RESULT (cfun
->decl
));
5873 for (p
= result_vi
; p
; p
= vi_next (p
))
5874 make_constraint_from (p
, nonlocal_id
);
5877 /* Add a constraint for the incoming static chain parameter. */
5878 if (cfun
->static_chain_decl
!= NULL_TREE
)
5880 varinfo_t p
, chain_vi
= get_vi_for_tree (cfun
->static_chain_decl
);
5882 for (p
= chain_vi
; p
; p
= vi_next (p
))
5883 make_constraint_from (p
, nonlocal_id
);
5887 /* Structure used to put solution bitmaps in a hashtable so they can
5888 be shared among variables with the same points-to set. */
5890 typedef struct shared_bitmap_info
5894 } *shared_bitmap_info_t
;
5895 typedef const struct shared_bitmap_info
*const_shared_bitmap_info_t
;
5897 /* Shared_bitmap hashtable helpers. */
5899 struct shared_bitmap_hasher
: typed_free_remove
<shared_bitmap_info
>
5901 typedef shared_bitmap_info value_type
;
5902 typedef shared_bitmap_info compare_type
;
5903 static inline hashval_t
hash (const value_type
*);
5904 static inline bool equal (const value_type
*, const compare_type
*);
5907 /* Hash function for a shared_bitmap_info_t */
5910 shared_bitmap_hasher::hash (const value_type
*bi
)
5912 return bi
->hashcode
;
5915 /* Equality function for two shared_bitmap_info_t's. */
5918 shared_bitmap_hasher::equal (const value_type
*sbi1
, const compare_type
*sbi2
)
5920 return bitmap_equal_p (sbi1
->pt_vars
, sbi2
->pt_vars
);
5923 /* Shared_bitmap hashtable. */
5925 static hash_table
<shared_bitmap_hasher
> shared_bitmap_table
;
5927 /* Lookup a bitmap in the shared bitmap hashtable, and return an already
5928 existing instance if there is one, NULL otherwise. */
5931 shared_bitmap_lookup (bitmap pt_vars
)
5933 shared_bitmap_info
**slot
;
5934 struct shared_bitmap_info sbi
;
5936 sbi
.pt_vars
= pt_vars
;
5937 sbi
.hashcode
= bitmap_hash (pt_vars
);
5939 slot
= shared_bitmap_table
.find_slot_with_hash (&sbi
, sbi
.hashcode
,
5944 return (*slot
)->pt_vars
;
5948 /* Add a bitmap to the shared bitmap hashtable. */
5951 shared_bitmap_add (bitmap pt_vars
)
5953 shared_bitmap_info
**slot
;
5954 shared_bitmap_info_t sbi
= XNEW (struct shared_bitmap_info
);
5956 sbi
->pt_vars
= pt_vars
;
5957 sbi
->hashcode
= bitmap_hash (pt_vars
);
5959 slot
= shared_bitmap_table
.find_slot_with_hash (sbi
, sbi
->hashcode
, INSERT
);
5960 gcc_assert (!*slot
);
5965 /* Set bits in INTO corresponding to the variable uids in solution set FROM. */
5968 set_uids_in_ptset (bitmap into
, bitmap from
, struct pt_solution
*pt
)
5972 varinfo_t escaped_vi
= get_varinfo (find (escaped_id
));
5973 bool everything_escaped
5974 = escaped_vi
->solution
&& bitmap_bit_p (escaped_vi
->solution
, anything_id
);
5976 EXECUTE_IF_SET_IN_BITMAP (from
, 0, i
, bi
)
5978 varinfo_t vi
= get_varinfo (i
);
5980 /* The only artificial variables that are allowed in a may-alias
5981 set are heap variables. */
5982 if (vi
->is_artificial_var
&& !vi
->is_heap_var
)
5985 if (everything_escaped
5986 || (escaped_vi
->solution
5987 && bitmap_bit_p (escaped_vi
->solution
, i
)))
5989 pt
->vars_contains_escaped
= true;
5990 pt
->vars_contains_escaped_heap
= vi
->is_heap_var
;
5993 if (TREE_CODE (vi
->decl
) == VAR_DECL
5994 || TREE_CODE (vi
->decl
) == PARM_DECL
5995 || TREE_CODE (vi
->decl
) == RESULT_DECL
)
5997 /* If we are in IPA mode we will not recompute points-to
5998 sets after inlining so make sure they stay valid. */
6000 && !DECL_PT_UID_SET_P (vi
->decl
))
6001 SET_DECL_PT_UID (vi
->decl
, DECL_UID (vi
->decl
));
6003 /* Add the decl to the points-to set. Note that the points-to
6004 set contains global variables. */
6005 bitmap_set_bit (into
, DECL_PT_UID (vi
->decl
));
6006 if (vi
->is_global_var
)
6007 pt
->vars_contains_nonlocal
= true;
6013 /* Compute the points-to solution *PT for the variable VI. */
6015 static struct pt_solution
6016 find_what_var_points_to (varinfo_t orig_vi
)
6020 bitmap finished_solution
;
6024 struct pt_solution
*pt
;
6026 /* This variable may have been collapsed, let's get the real
6028 vi
= get_varinfo (find (orig_vi
->id
));
6030 /* See if we have already computed the solution and return it. */
6031 slot
= pointer_map_insert (final_solutions
, vi
);
6033 return *(struct pt_solution
*)*slot
;
6035 *slot
= pt
= XOBNEW (&final_solutions_obstack
, struct pt_solution
);
6036 memset (pt
, 0, sizeof (struct pt_solution
));
6038 /* Translate artificial variables into SSA_NAME_PTR_INFO
6040 EXECUTE_IF_SET_IN_BITMAP (vi
->solution
, 0, i
, bi
)
6042 varinfo_t vi
= get_varinfo (i
);
6044 if (vi
->is_artificial_var
)
6046 if (vi
->id
== nothing_id
)
6048 else if (vi
->id
== escaped_id
)
6051 pt
->ipa_escaped
= 1;
6055 else if (vi
->id
== nonlocal_id
)
6057 else if (vi
->is_heap_var
)
6058 /* We represent heapvars in the points-to set properly. */
6060 else if (vi
->id
== readonly_id
)
6063 else if (vi
->id
== anything_id
6064 || vi
->id
== integer_id
)
6069 /* Instead of doing extra work, simply do not create
6070 elaborate points-to information for pt_anything pointers. */
6074 /* Share the final set of variables when possible. */
6075 finished_solution
= BITMAP_GGC_ALLOC ();
6076 stats
.points_to_sets_created
++;
6078 set_uids_in_ptset (finished_solution
, vi
->solution
, pt
);
6079 result
= shared_bitmap_lookup (finished_solution
);
6082 shared_bitmap_add (finished_solution
);
6083 pt
->vars
= finished_solution
;
6088 bitmap_clear (finished_solution
);
6094 /* Given a pointer variable P, fill in its points-to set. */
6097 find_what_p_points_to (tree p
)
6099 struct ptr_info_def
*pi
;
6103 /* For parameters, get at the points-to set for the actual parm
6105 if (TREE_CODE (p
) == SSA_NAME
6106 && SSA_NAME_IS_DEFAULT_DEF (p
)
6107 && (TREE_CODE (SSA_NAME_VAR (p
)) == PARM_DECL
6108 || TREE_CODE (SSA_NAME_VAR (p
)) == RESULT_DECL
))
6109 lookup_p
= SSA_NAME_VAR (p
);
6111 vi
= lookup_vi_for_tree (lookup_p
);
6115 pi
= get_ptr_info (p
);
6116 pi
->pt
= find_what_var_points_to (vi
);
6120 /* Query statistics for points-to solutions. */
6123 unsigned HOST_WIDE_INT pt_solution_includes_may_alias
;
6124 unsigned HOST_WIDE_INT pt_solution_includes_no_alias
;
6125 unsigned HOST_WIDE_INT pt_solutions_intersect_may_alias
;
6126 unsigned HOST_WIDE_INT pt_solutions_intersect_no_alias
;
6130 dump_pta_stats (FILE *s
)
6132 fprintf (s
, "\nPTA query stats:\n");
6133 fprintf (s
, " pt_solution_includes: "
6134 HOST_WIDE_INT_PRINT_DEC
" disambiguations, "
6135 HOST_WIDE_INT_PRINT_DEC
" queries\n",
6136 pta_stats
.pt_solution_includes_no_alias
,
6137 pta_stats
.pt_solution_includes_no_alias
6138 + pta_stats
.pt_solution_includes_may_alias
);
6139 fprintf (s
, " pt_solutions_intersect: "
6140 HOST_WIDE_INT_PRINT_DEC
" disambiguations, "
6141 HOST_WIDE_INT_PRINT_DEC
" queries\n",
6142 pta_stats
.pt_solutions_intersect_no_alias
,
6143 pta_stats
.pt_solutions_intersect_no_alias
6144 + pta_stats
.pt_solutions_intersect_may_alias
);
6148 /* Reset the points-to solution *PT to a conservative default
6149 (point to anything). */
6152 pt_solution_reset (struct pt_solution
*pt
)
6154 memset (pt
, 0, sizeof (struct pt_solution
));
6155 pt
->anything
= true;
6158 /* Set the points-to solution *PT to point only to the variables
6159 in VARS. VARS_CONTAINS_GLOBAL specifies whether that contains
6160 global variables and VARS_CONTAINS_RESTRICT specifies whether
6161 it contains restrict tag variables. */
6164 pt_solution_set (struct pt_solution
*pt
, bitmap vars
,
6165 bool vars_contains_nonlocal
)
6167 memset (pt
, 0, sizeof (struct pt_solution
));
6169 pt
->vars_contains_nonlocal
= vars_contains_nonlocal
;
6170 pt
->vars_contains_escaped
6171 = (cfun
->gimple_df
->escaped
.anything
6172 || bitmap_intersect_p (cfun
->gimple_df
->escaped
.vars
, vars
));
6175 /* Set the points-to solution *PT to point only to the variable VAR. */
6178 pt_solution_set_var (struct pt_solution
*pt
, tree var
)
6180 memset (pt
, 0, sizeof (struct pt_solution
));
6181 pt
->vars
= BITMAP_GGC_ALLOC ();
6182 bitmap_set_bit (pt
->vars
, DECL_PT_UID (var
));
6183 pt
->vars_contains_nonlocal
= is_global_var (var
);
6184 pt
->vars_contains_escaped
6185 = (cfun
->gimple_df
->escaped
.anything
6186 || bitmap_bit_p (cfun
->gimple_df
->escaped
.vars
, DECL_PT_UID (var
)));
6189 /* Computes the union of the points-to solutions *DEST and *SRC and
6190 stores the result in *DEST. This changes the points-to bitmap
6191 of *DEST and thus may not be used if that might be shared.
6192 The points-to bitmap of *SRC and *DEST will not be shared after
6193 this function if they were not before. */
6196 pt_solution_ior_into (struct pt_solution
*dest
, struct pt_solution
*src
)
6198 dest
->anything
|= src
->anything
;
6201 pt_solution_reset (dest
);
6205 dest
->nonlocal
|= src
->nonlocal
;
6206 dest
->escaped
|= src
->escaped
;
6207 dest
->ipa_escaped
|= src
->ipa_escaped
;
6208 dest
->null
|= src
->null
;
6209 dest
->vars_contains_nonlocal
|= src
->vars_contains_nonlocal
;
6210 dest
->vars_contains_escaped
|= src
->vars_contains_escaped
;
6211 dest
->vars_contains_escaped_heap
|= src
->vars_contains_escaped_heap
;
6216 dest
->vars
= BITMAP_GGC_ALLOC ();
6217 bitmap_ior_into (dest
->vars
, src
->vars
);
6220 /* Return true if the points-to solution *PT is empty. */
6223 pt_solution_empty_p (struct pt_solution
*pt
)
6230 && !bitmap_empty_p (pt
->vars
))
6233 /* If the solution includes ESCAPED, check if that is empty. */
6235 && !pt_solution_empty_p (&cfun
->gimple_df
->escaped
))
6238 /* If the solution includes ESCAPED, check if that is empty. */
6240 && !pt_solution_empty_p (&ipa_escaped_pt
))
6246 /* Return true if the points-to solution *PT only point to a single var, and
6247 return the var uid in *UID. */
6250 pt_solution_singleton_p (struct pt_solution
*pt
, unsigned *uid
)
6252 if (pt
->anything
|| pt
->nonlocal
|| pt
->escaped
|| pt
->ipa_escaped
6253 || pt
->null
|| pt
->vars
== NULL
6254 || !bitmap_single_bit_set_p (pt
->vars
))
6257 *uid
= bitmap_first_set_bit (pt
->vars
);
6261 /* Return true if the points-to solution *PT includes global memory. */
6264 pt_solution_includes_global (struct pt_solution
*pt
)
6268 || pt
->vars_contains_nonlocal
6269 /* The following is a hack to make the malloc escape hack work.
6270 In reality we'd need different sets for escaped-through-return
6271 and escaped-to-callees and passes would need to be updated. */
6272 || pt
->vars_contains_escaped_heap
)
6275 /* 'escaped' is also a placeholder so we have to look into it. */
6277 return pt_solution_includes_global (&cfun
->gimple_df
->escaped
);
6279 if (pt
->ipa_escaped
)
6280 return pt_solution_includes_global (&ipa_escaped_pt
);
6282 /* ??? This predicate is not correct for the IPA-PTA solution
6283 as we do not properly distinguish between unit escape points
6284 and global variables. */
6285 if (cfun
->gimple_df
->ipa_pta
)
6291 /* Return true if the points-to solution *PT includes the variable
6292 declaration DECL. */
6295 pt_solution_includes_1 (struct pt_solution
*pt
, const_tree decl
)
6301 && is_global_var (decl
))
6305 && bitmap_bit_p (pt
->vars
, DECL_PT_UID (decl
)))
6308 /* If the solution includes ESCAPED, check it. */
6310 && pt_solution_includes_1 (&cfun
->gimple_df
->escaped
, decl
))
6313 /* If the solution includes ESCAPED, check it. */
6315 && pt_solution_includes_1 (&ipa_escaped_pt
, decl
))
6322 pt_solution_includes (struct pt_solution
*pt
, const_tree decl
)
6324 bool res
= pt_solution_includes_1 (pt
, decl
);
6326 ++pta_stats
.pt_solution_includes_may_alias
;
6328 ++pta_stats
.pt_solution_includes_no_alias
;
6332 /* Return true if both points-to solutions PT1 and PT2 have a non-empty
6336 pt_solutions_intersect_1 (struct pt_solution
*pt1
, struct pt_solution
*pt2
)
6338 if (pt1
->anything
|| pt2
->anything
)
6341 /* If either points to unknown global memory and the other points to
6342 any global memory they alias. */
6345 || pt2
->vars_contains_nonlocal
))
6347 && pt1
->vars_contains_nonlocal
))
6350 /* If either points to all escaped memory and the other points to
6351 any escaped memory they alias. */
6354 || pt2
->vars_contains_escaped
))
6356 && pt1
->vars_contains_escaped
))
6359 /* Check the escaped solution if required.
6360 ??? Do we need to check the local against the IPA escaped sets? */
6361 if ((pt1
->ipa_escaped
|| pt2
->ipa_escaped
)
6362 && !pt_solution_empty_p (&ipa_escaped_pt
))
6364 /* If both point to escaped memory and that solution
6365 is not empty they alias. */
6366 if (pt1
->ipa_escaped
&& pt2
->ipa_escaped
)
6369 /* If either points to escaped memory see if the escaped solution
6370 intersects with the other. */
6371 if ((pt1
->ipa_escaped
6372 && pt_solutions_intersect_1 (&ipa_escaped_pt
, pt2
))
6373 || (pt2
->ipa_escaped
6374 && pt_solutions_intersect_1 (&ipa_escaped_pt
, pt1
)))
6378 /* Now both pointers alias if their points-to solution intersects. */
6381 && bitmap_intersect_p (pt1
->vars
, pt2
->vars
));
6385 pt_solutions_intersect (struct pt_solution
*pt1
, struct pt_solution
*pt2
)
6387 bool res
= pt_solutions_intersect_1 (pt1
, pt2
);
6389 ++pta_stats
.pt_solutions_intersect_may_alias
;
6391 ++pta_stats
.pt_solutions_intersect_no_alias
;
6396 /* Dump points-to information to OUTFILE. */
6399 dump_sa_points_to_info (FILE *outfile
)
6403 fprintf (outfile
, "\nPoints-to sets\n\n");
6405 if (dump_flags
& TDF_STATS
)
6407 fprintf (outfile
, "Stats:\n");
6408 fprintf (outfile
, "Total vars: %d\n", stats
.total_vars
);
6409 fprintf (outfile
, "Non-pointer vars: %d\n",
6410 stats
.nonpointer_vars
);
6411 fprintf (outfile
, "Statically unified vars: %d\n",
6412 stats
.unified_vars_static
);
6413 fprintf (outfile
, "Dynamically unified vars: %d\n",
6414 stats
.unified_vars_dynamic
);
6415 fprintf (outfile
, "Iterations: %d\n", stats
.iterations
);
6416 fprintf (outfile
, "Number of edges: %d\n", stats
.num_edges
);
6417 fprintf (outfile
, "Number of implicit edges: %d\n",
6418 stats
.num_implicit_edges
);
6421 for (i
= 1; i
< varmap
.length (); i
++)
6423 varinfo_t vi
= get_varinfo (i
);
6424 if (!vi
->may_have_pointers
)
6426 dump_solution_for_var (outfile
, i
);
6431 /* Debug points-to information to stderr. */
6434 debug_sa_points_to_info (void)
6436 dump_sa_points_to_info (stderr
);
6440 /* Initialize the always-existing constraint variables for NULL
6441 ANYTHING, READONLY, and INTEGER */
6444 init_base_vars (void)
6446 struct constraint_expr lhs
, rhs
;
6447 varinfo_t var_anything
;
6448 varinfo_t var_nothing
;
6449 varinfo_t var_readonly
;
6450 varinfo_t var_escaped
;
6451 varinfo_t var_nonlocal
;
6452 varinfo_t var_storedanything
;
6453 varinfo_t var_integer
;
6455 /* Variable ID zero is reserved and should be NULL. */
6456 varmap
.safe_push (NULL
);
6458 /* Create the NULL variable, used to represent that a variable points
6460 var_nothing
= new_var_info (NULL_TREE
, "NULL");
6461 gcc_assert (var_nothing
->id
== nothing_id
);
6462 var_nothing
->is_artificial_var
= 1;
6463 var_nothing
->offset
= 0;
6464 var_nothing
->size
= ~0;
6465 var_nothing
->fullsize
= ~0;
6466 var_nothing
->is_special_var
= 1;
6467 var_nothing
->may_have_pointers
= 0;
6468 var_nothing
->is_global_var
= 0;
6470 /* Create the ANYTHING variable, used to represent that a variable
6471 points to some unknown piece of memory. */
6472 var_anything
= new_var_info (NULL_TREE
, "ANYTHING");
6473 gcc_assert (var_anything
->id
== anything_id
);
6474 var_anything
->is_artificial_var
= 1;
6475 var_anything
->size
= ~0;
6476 var_anything
->offset
= 0;
6477 var_anything
->fullsize
= ~0;
6478 var_anything
->is_special_var
= 1;
6480 /* Anything points to anything. This makes deref constraints just
6481 work in the presence of linked list and other p = *p type loops,
6482 by saying that *ANYTHING = ANYTHING. */
6484 lhs
.var
= anything_id
;
6486 rhs
.type
= ADDRESSOF
;
6487 rhs
.var
= anything_id
;
6490 /* This specifically does not use process_constraint because
6491 process_constraint ignores all anything = anything constraints, since all
6492 but this one are redundant. */
6493 constraints
.safe_push (new_constraint (lhs
, rhs
));
6495 /* Create the READONLY variable, used to represent that a variable
6496 points to readonly memory. */
6497 var_readonly
= new_var_info (NULL_TREE
, "READONLY");
6498 gcc_assert (var_readonly
->id
== readonly_id
);
6499 var_readonly
->is_artificial_var
= 1;
6500 var_readonly
->offset
= 0;
6501 var_readonly
->size
= ~0;
6502 var_readonly
->fullsize
= ~0;
6503 var_readonly
->is_special_var
= 1;
6505 /* readonly memory points to anything, in order to make deref
6506 easier. In reality, it points to anything the particular
6507 readonly variable can point to, but we don't track this
6510 lhs
.var
= readonly_id
;
6512 rhs
.type
= ADDRESSOF
;
6513 rhs
.var
= readonly_id
; /* FIXME */
6515 process_constraint (new_constraint (lhs
, rhs
));
6517 /* Create the ESCAPED variable, used to represent the set of escaped
6519 var_escaped
= new_var_info (NULL_TREE
, "ESCAPED");
6520 gcc_assert (var_escaped
->id
== escaped_id
);
6521 var_escaped
->is_artificial_var
= 1;
6522 var_escaped
->offset
= 0;
6523 var_escaped
->size
= ~0;
6524 var_escaped
->fullsize
= ~0;
6525 var_escaped
->is_special_var
= 0;
6527 /* Create the NONLOCAL variable, used to represent the set of nonlocal
6529 var_nonlocal
= new_var_info (NULL_TREE
, "NONLOCAL");
6530 gcc_assert (var_nonlocal
->id
== nonlocal_id
);
6531 var_nonlocal
->is_artificial_var
= 1;
6532 var_nonlocal
->offset
= 0;
6533 var_nonlocal
->size
= ~0;
6534 var_nonlocal
->fullsize
= ~0;
6535 var_nonlocal
->is_special_var
= 1;
6537 /* ESCAPED = *ESCAPED, because escaped is may-deref'd at calls, etc. */
6539 lhs
.var
= escaped_id
;
6542 rhs
.var
= escaped_id
;
6544 process_constraint (new_constraint (lhs
, rhs
));
6546 /* ESCAPED = ESCAPED + UNKNOWN_OFFSET, because if a sub-field escapes the
6547 whole variable escapes. */
6549 lhs
.var
= escaped_id
;
6552 rhs
.var
= escaped_id
;
6553 rhs
.offset
= UNKNOWN_OFFSET
;
6554 process_constraint (new_constraint (lhs
, rhs
));
6556 /* *ESCAPED = NONLOCAL. This is true because we have to assume
6557 everything pointed to by escaped points to what global memory can
6560 lhs
.var
= escaped_id
;
6563 rhs
.var
= nonlocal_id
;
6565 process_constraint (new_constraint (lhs
, rhs
));
6567 /* NONLOCAL = &NONLOCAL, NONLOCAL = &ESCAPED. This is true because
6568 global memory may point to global memory and escaped memory. */
6570 lhs
.var
= nonlocal_id
;
6572 rhs
.type
= ADDRESSOF
;
6573 rhs
.var
= nonlocal_id
;
6575 process_constraint (new_constraint (lhs
, rhs
));
6576 rhs
.type
= ADDRESSOF
;
6577 rhs
.var
= escaped_id
;
6579 process_constraint (new_constraint (lhs
, rhs
));
6581 /* Create the STOREDANYTHING variable, used to represent the set of
6582 variables stored to *ANYTHING. */
6583 var_storedanything
= new_var_info (NULL_TREE
, "STOREDANYTHING");
6584 gcc_assert (var_storedanything
->id
== storedanything_id
);
6585 var_storedanything
->is_artificial_var
= 1;
6586 var_storedanything
->offset
= 0;
6587 var_storedanything
->size
= ~0;
6588 var_storedanything
->fullsize
= ~0;
6589 var_storedanything
->is_special_var
= 0;
6591 /* Create the INTEGER variable, used to represent that a variable points
6592 to what an INTEGER "points to". */
6593 var_integer
= new_var_info (NULL_TREE
, "INTEGER");
6594 gcc_assert (var_integer
->id
== integer_id
);
6595 var_integer
->is_artificial_var
= 1;
6596 var_integer
->size
= ~0;
6597 var_integer
->fullsize
= ~0;
6598 var_integer
->offset
= 0;
6599 var_integer
->is_special_var
= 1;
6601 /* INTEGER = ANYTHING, because we don't know where a dereference of
6602 a random integer will point to. */
6604 lhs
.var
= integer_id
;
6606 rhs
.type
= ADDRESSOF
;
6607 rhs
.var
= anything_id
;
6609 process_constraint (new_constraint (lhs
, rhs
));
6612 /* Initialize things necessary to perform PTA */
6615 init_alias_vars (void)
6617 use_field_sensitive
= (MAX_FIELDS_FOR_FIELD_SENSITIVE
> 1);
6619 bitmap_obstack_initialize (&pta_obstack
);
6620 bitmap_obstack_initialize (&oldpta_obstack
);
6621 bitmap_obstack_initialize (&predbitmap_obstack
);
6623 constraint_pool
= create_alloc_pool ("Constraint pool",
6624 sizeof (struct constraint
), 30);
6625 variable_info_pool
= create_alloc_pool ("Variable info pool",
6626 sizeof (struct variable_info
), 30);
6627 constraints
.create (8);
6629 vi_for_tree
= pointer_map_create ();
6630 call_stmt_vars
= pointer_map_create ();
6632 memset (&stats
, 0, sizeof (stats
));
6633 shared_bitmap_table
.create (511);
6636 gcc_obstack_init (&fake_var_decl_obstack
);
6638 final_solutions
= pointer_map_create ();
6639 gcc_obstack_init (&final_solutions_obstack
);
6642 /* Remove the REF and ADDRESS edges from GRAPH, as well as all the
6643 predecessor edges. */
6646 remove_preds_and_fake_succs (constraint_graph_t graph
)
6650 /* Clear the implicit ref and address nodes from the successor
6652 for (i
= 1; i
< FIRST_REF_NODE
; i
++)
6654 if (graph
->succs
[i
])
6655 bitmap_clear_range (graph
->succs
[i
], FIRST_REF_NODE
,
6656 FIRST_REF_NODE
* 2);
6659 /* Free the successor list for the non-ref nodes. */
6660 for (i
= FIRST_REF_NODE
+ 1; i
< graph
->size
; i
++)
6662 if (graph
->succs
[i
])
6663 BITMAP_FREE (graph
->succs
[i
]);
6666 /* Now reallocate the size of the successor list as, and blow away
6667 the predecessor bitmaps. */
6668 graph
->size
= varmap
.length ();
6669 graph
->succs
= XRESIZEVEC (bitmap
, graph
->succs
, graph
->size
);
6671 free (graph
->implicit_preds
);
6672 graph
->implicit_preds
= NULL
;
6673 free (graph
->preds
);
6674 graph
->preds
= NULL
;
6675 bitmap_obstack_release (&predbitmap_obstack
);
6678 /* Solve the constraint set. */
6681 solve_constraints (void)
6683 struct scc_info
*si
;
6687 "\nCollapsing static cycles and doing variable "
6690 init_graph (varmap
.length () * 2);
6693 fprintf (dump_file
, "Building predecessor graph\n");
6694 build_pred_graph ();
6697 fprintf (dump_file
, "Detecting pointer and location "
6699 si
= perform_var_substitution (graph
);
6702 fprintf (dump_file
, "Rewriting constraints and unifying "
6704 rewrite_constraints (graph
, si
);
6706 build_succ_graph ();
6708 free_var_substitution_info (si
);
6710 /* Attach complex constraints to graph nodes. */
6711 move_complex_constraints (graph
);
6714 fprintf (dump_file
, "Uniting pointer but not location equivalent "
6716 unite_pointer_equivalences (graph
);
6719 fprintf (dump_file
, "Finding indirect cycles\n");
6720 find_indirect_cycles (graph
);
6722 /* Implicit nodes and predecessors are no longer necessary at this
6724 remove_preds_and_fake_succs (graph
);
6726 if (dump_file
&& (dump_flags
& TDF_GRAPH
))
6728 fprintf (dump_file
, "\n\n// The constraint graph before solve-graph "
6729 "in dot format:\n");
6730 dump_constraint_graph (dump_file
);
6731 fprintf (dump_file
, "\n\n");
6735 fprintf (dump_file
, "Solving graph\n");
6737 solve_graph (graph
);
6739 if (dump_file
&& (dump_flags
& TDF_GRAPH
))
6741 fprintf (dump_file
, "\n\n// The constraint graph after solve-graph "
6742 "in dot format:\n");
6743 dump_constraint_graph (dump_file
);
6744 fprintf (dump_file
, "\n\n");
6748 dump_sa_points_to_info (dump_file
);
6751 /* Create points-to sets for the current function. See the comments
6752 at the start of the file for an algorithmic overview. */
6755 compute_points_to_sets (void)
6761 timevar_push (TV_TREE_PTA
);
6765 intra_create_variable_infos ();
6767 /* Now walk all statements and build the constraint set. */
6770 gimple_stmt_iterator gsi
;
6772 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
6774 gimple phi
= gsi_stmt (gsi
);
6776 if (! virtual_operand_p (gimple_phi_result (phi
)))
6777 find_func_aliases (phi
);
6780 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
6782 gimple stmt
= gsi_stmt (gsi
);
6784 find_func_aliases (stmt
);
6790 fprintf (dump_file
, "Points-to analysis\n\nConstraints:\n\n");
6791 dump_constraints (dump_file
, 0);
6794 /* From the constraints compute the points-to sets. */
6795 solve_constraints ();
6797 /* Compute the points-to set for ESCAPED used for call-clobber analysis. */
6798 cfun
->gimple_df
->escaped
= find_what_var_points_to (get_varinfo (escaped_id
));
6800 /* Make sure the ESCAPED solution (which is used as placeholder in
6801 other solutions) does not reference itself. This simplifies
6802 points-to solution queries. */
6803 cfun
->gimple_df
->escaped
.escaped
= 0;
6805 /* Compute the points-to sets for pointer SSA_NAMEs. */
6806 for (i
= 0; i
< num_ssa_names
; ++i
)
6808 tree ptr
= ssa_name (i
);
6810 && POINTER_TYPE_P (TREE_TYPE (ptr
)))
6811 find_what_p_points_to (ptr
);
6814 /* Compute the call-used/clobbered sets. */
6817 gimple_stmt_iterator gsi
;
6819 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
6821 gimple stmt
= gsi_stmt (gsi
);
6822 struct pt_solution
*pt
;
6823 if (!is_gimple_call (stmt
))
6826 pt
= gimple_call_use_set (stmt
);
6827 if (gimple_call_flags (stmt
) & ECF_CONST
)
6828 memset (pt
, 0, sizeof (struct pt_solution
));
6829 else if ((vi
= lookup_call_use_vi (stmt
)) != NULL
)
6831 *pt
= find_what_var_points_to (vi
);
6832 /* Escaped (and thus nonlocal) variables are always
6833 implicitly used by calls. */
6834 /* ??? ESCAPED can be empty even though NONLOCAL
6841 /* If there is nothing special about this call then
6842 we have made everything that is used also escape. */
6843 *pt
= cfun
->gimple_df
->escaped
;
6847 pt
= gimple_call_clobber_set (stmt
);
6848 if (gimple_call_flags (stmt
) & (ECF_CONST
|ECF_PURE
|ECF_NOVOPS
))
6849 memset (pt
, 0, sizeof (struct pt_solution
));
6850 else if ((vi
= lookup_call_clobber_vi (stmt
)) != NULL
)
6852 *pt
= find_what_var_points_to (vi
);
6853 /* Escaped (and thus nonlocal) variables are always
6854 implicitly clobbered by calls. */
6855 /* ??? ESCAPED can be empty even though NONLOCAL
6862 /* If there is nothing special about this call then
6863 we have made everything that is used also escape. */
6864 *pt
= cfun
->gimple_df
->escaped
;
6870 timevar_pop (TV_TREE_PTA
);
6874 /* Delete created points-to sets. */
6877 delete_points_to_sets (void)
6881 shared_bitmap_table
.dispose ();
6882 if (dump_file
&& (dump_flags
& TDF_STATS
))
6883 fprintf (dump_file
, "Points to sets created:%d\n",
6884 stats
.points_to_sets_created
);
6886 pointer_map_destroy (vi_for_tree
);
6887 pointer_map_destroy (call_stmt_vars
);
6888 bitmap_obstack_release (&pta_obstack
);
6889 constraints
.release ();
6891 for (i
= 0; i
< graph
->size
; i
++)
6892 graph
->complex[i
].release ();
6893 free (graph
->complex);
6896 free (graph
->succs
);
6898 free (graph
->pe_rep
);
6899 free (graph
->indirect_cycles
);
6903 free_alloc_pool (variable_info_pool
);
6904 free_alloc_pool (constraint_pool
);
6906 obstack_free (&fake_var_decl_obstack
, NULL
);
6908 pointer_map_destroy (final_solutions
);
6909 obstack_free (&final_solutions_obstack
, NULL
);
6913 /* Compute points-to information for every SSA_NAME pointer in the
6914 current function and compute the transitive closure of escaped
6915 variables to re-initialize the call-clobber states of local variables. */
6918 compute_may_aliases (void)
6920 if (cfun
->gimple_df
->ipa_pta
)
6924 fprintf (dump_file
, "\nNot re-computing points-to information "
6925 "because IPA points-to information is available.\n\n");
6927 /* But still dump what we have remaining it. */
6928 dump_alias_info (dump_file
);
6934 /* For each pointer P_i, determine the sets of variables that P_i may
6935 point-to. Compute the reachability set of escaped and call-used
6937 compute_points_to_sets ();
6939 /* Debugging dumps. */
6941 dump_alias_info (dump_file
);
6943 /* Deallocate memory used by aliasing data structures and the internal
6944 points-to solution. */
6945 delete_points_to_sets ();
6947 gcc_assert (!need_ssa_update_p (cfun
));
6953 gate_tree_pta (void)
6955 return flag_tree_pta
;
6958 /* A dummy pass to cause points-to information to be computed via
6959 TODO_rebuild_alias. */
6963 const pass_data pass_data_build_alias
=
6965 GIMPLE_PASS
, /* type */
6967 OPTGROUP_NONE
, /* optinfo_flags */
6968 true, /* has_gate */
6969 false, /* has_execute */
6970 TV_NONE
, /* tv_id */
6971 ( PROP_cfg
| PROP_ssa
), /* properties_required */
6972 0, /* properties_provided */
6973 0, /* properties_destroyed */
6974 0, /* todo_flags_start */
6975 TODO_rebuild_alias
, /* todo_flags_finish */
6978 class pass_build_alias
: public gimple_opt_pass
6981 pass_build_alias (gcc::context
*ctxt
)
6982 : gimple_opt_pass (pass_data_build_alias
, ctxt
)
6985 /* opt_pass methods: */
6986 bool gate () { return gate_tree_pta (); }
6988 }; // class pass_build_alias
6993 make_pass_build_alias (gcc::context
*ctxt
)
6995 return new pass_build_alias (ctxt
);
6998 /* A dummy pass to cause points-to information to be computed via
6999 TODO_rebuild_alias. */
7003 const pass_data pass_data_build_ealias
=
7005 GIMPLE_PASS
, /* type */
7006 "ealias", /* name */
7007 OPTGROUP_NONE
, /* optinfo_flags */
7008 true, /* has_gate */
7009 false, /* has_execute */
7010 TV_NONE
, /* tv_id */
7011 ( PROP_cfg
| PROP_ssa
), /* properties_required */
7012 0, /* properties_provided */
7013 0, /* properties_destroyed */
7014 0, /* todo_flags_start */
7015 TODO_rebuild_alias
, /* todo_flags_finish */
7018 class pass_build_ealias
: public gimple_opt_pass
7021 pass_build_ealias (gcc::context
*ctxt
)
7022 : gimple_opt_pass (pass_data_build_ealias
, ctxt
)
7025 /* opt_pass methods: */
7026 bool gate () { return gate_tree_pta (); }
7028 }; // class pass_build_ealias
7033 make_pass_build_ealias (gcc::context
*ctxt
)
7035 return new pass_build_ealias (ctxt
);
7039 /* Return true if we should execute IPA PTA. */
7045 /* Don't bother doing anything if the program has errors. */
7049 /* IPA PTA solutions for ESCAPED. */
7050 struct pt_solution ipa_escaped_pt
7051 = { true, false, false, false, false, false, false, false, NULL
};
7053 /* Associate node with varinfo DATA. Worker for
7054 cgraph_for_node_and_aliases. */
7056 associate_varinfo_to_alias (struct cgraph_node
*node
, void *data
)
7058 if ((node
->alias
|| node
->thunk
.thunk_p
)
7060 insert_vi_for_tree (node
->decl
, (varinfo_t
)data
);
7064 /* Execute the driver for IPA PTA. */
7066 ipa_pta_execute (void)
7068 struct cgraph_node
*node
;
7069 struct varpool_node
*var
;
7076 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7078 dump_symtab (dump_file
);
7079 fprintf (dump_file
, "\n");
7082 /* Build the constraints. */
7083 FOR_EACH_DEFINED_FUNCTION (node
)
7086 /* Nodes without a body are not interesting. Especially do not
7087 visit clones at this point for now - we get duplicate decls
7088 there for inline clones at least. */
7089 if (!cgraph_function_with_gimple_body_p (node
) || node
->clone_of
)
7091 cgraph_get_body (node
);
7093 gcc_assert (!node
->clone_of
);
7095 vi
= create_function_info_for (node
->decl
,
7096 alias_get_name (node
->decl
));
7097 cgraph_for_node_and_aliases (node
, associate_varinfo_to_alias
, vi
, true);
7100 /* Create constraints for global variables and their initializers. */
7101 FOR_EACH_VARIABLE (var
)
7103 if (var
->alias
&& var
->analyzed
)
7106 get_vi_for_tree (var
->decl
);
7112 "Generating constraints for global initializers\n\n");
7113 dump_constraints (dump_file
, 0);
7114 fprintf (dump_file
, "\n");
7116 from
= constraints
.length ();
7118 FOR_EACH_DEFINED_FUNCTION (node
)
7120 struct function
*func
;
7123 /* Nodes without a body are not interesting. */
7124 if (!cgraph_function_with_gimple_body_p (node
) || node
->clone_of
)
7130 "Generating constraints for %s", node
->name ());
7131 if (DECL_ASSEMBLER_NAME_SET_P (node
->decl
))
7132 fprintf (dump_file
, " (%s)",
7134 (DECL_ASSEMBLER_NAME (node
->decl
)));
7135 fprintf (dump_file
, "\n");
7138 func
= DECL_STRUCT_FUNCTION (node
->decl
);
7141 /* For externally visible or attribute used annotated functions use
7142 local constraints for their arguments.
7143 For local functions we see all callers and thus do not need initial
7144 constraints for parameters. */
7145 if (node
->used_from_other_partition
7146 || node
->externally_visible
7147 || node
->force_output
)
7149 intra_create_variable_infos ();
7151 /* We also need to make function return values escape. Nothing
7152 escapes by returning from main though. */
7153 if (!MAIN_NAME_P (DECL_NAME (node
->decl
)))
7156 fi
= lookup_vi_for_tree (node
->decl
);
7157 rvi
= first_vi_for_offset (fi
, fi_result
);
7158 if (rvi
&& rvi
->offset
== fi_result
)
7160 struct constraint_expr includes
;
7161 struct constraint_expr var
;
7162 includes
.var
= escaped_id
;
7163 includes
.offset
= 0;
7164 includes
.type
= SCALAR
;
7168 process_constraint (new_constraint (includes
, var
));
7173 /* Build constriants for the function body. */
7174 FOR_EACH_BB_FN (bb
, func
)
7176 gimple_stmt_iterator gsi
;
7178 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
);
7181 gimple phi
= gsi_stmt (gsi
);
7183 if (! virtual_operand_p (gimple_phi_result (phi
)))
7184 find_func_aliases (phi
);
7187 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
7189 gimple stmt
= gsi_stmt (gsi
);
7191 find_func_aliases (stmt
);
7192 find_func_clobbers (stmt
);
7200 fprintf (dump_file
, "\n");
7201 dump_constraints (dump_file
, from
);
7202 fprintf (dump_file
, "\n");
7204 from
= constraints
.length ();
7207 /* From the constraints compute the points-to sets. */
7208 solve_constraints ();
7210 /* Compute the global points-to sets for ESCAPED.
7211 ??? Note that the computed escape set is not correct
7212 for the whole unit as we fail to consider graph edges to
7213 externally visible functions. */
7214 ipa_escaped_pt
= find_what_var_points_to (get_varinfo (escaped_id
));
7216 /* Make sure the ESCAPED solution (which is used as placeholder in
7217 other solutions) does not reference itself. This simplifies
7218 points-to solution queries. */
7219 ipa_escaped_pt
.ipa_escaped
= 0;
7221 /* Assign the points-to sets to the SSA names in the unit. */
7222 FOR_EACH_DEFINED_FUNCTION (node
)
7225 struct function
*fn
;
7229 struct pt_solution uses
, clobbers
;
7230 struct cgraph_edge
*e
;
7232 /* Nodes without a body are not interesting. */
7233 if (!cgraph_function_with_gimple_body_p (node
) || node
->clone_of
)
7236 fn
= DECL_STRUCT_FUNCTION (node
->decl
);
7238 /* Compute the points-to sets for pointer SSA_NAMEs. */
7239 FOR_EACH_VEC_ELT (*fn
->gimple_df
->ssa_names
, i
, ptr
)
7242 && POINTER_TYPE_P (TREE_TYPE (ptr
)))
7243 find_what_p_points_to (ptr
);
7246 /* Compute the call-use and call-clobber sets for all direct calls. */
7247 fi
= lookup_vi_for_tree (node
->decl
);
7248 gcc_assert (fi
->is_fn_info
);
7250 = find_what_var_points_to (first_vi_for_offset (fi
, fi_clobbers
));
7251 uses
= find_what_var_points_to (first_vi_for_offset (fi
, fi_uses
));
7252 for (e
= node
->callers
; e
; e
= e
->next_caller
)
7257 *gimple_call_clobber_set (e
->call_stmt
) = clobbers
;
7258 *gimple_call_use_set (e
->call_stmt
) = uses
;
7261 /* Compute the call-use and call-clobber sets for indirect calls
7262 and calls to external functions. */
7263 FOR_EACH_BB_FN (bb
, fn
)
7265 gimple_stmt_iterator gsi
;
7267 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
7269 gimple stmt
= gsi_stmt (gsi
);
7270 struct pt_solution
*pt
;
7274 if (!is_gimple_call (stmt
))
7277 /* Handle direct calls to external functions. */
7278 decl
= gimple_call_fndecl (stmt
);
7280 && (!(fi
= lookup_vi_for_tree (decl
))
7281 || !fi
->is_fn_info
))
7283 pt
= gimple_call_use_set (stmt
);
7284 if (gimple_call_flags (stmt
) & ECF_CONST
)
7285 memset (pt
, 0, sizeof (struct pt_solution
));
7286 else if ((vi
= lookup_call_use_vi (stmt
)) != NULL
)
7288 *pt
= find_what_var_points_to (vi
);
7289 /* Escaped (and thus nonlocal) variables are always
7290 implicitly used by calls. */
7291 /* ??? ESCAPED can be empty even though NONLOCAL
7294 pt
->ipa_escaped
= 1;
7298 /* If there is nothing special about this call then
7299 we have made everything that is used also escape. */
7300 *pt
= ipa_escaped_pt
;
7304 pt
= gimple_call_clobber_set (stmt
);
7305 if (gimple_call_flags (stmt
) & (ECF_CONST
|ECF_PURE
|ECF_NOVOPS
))
7306 memset (pt
, 0, sizeof (struct pt_solution
));
7307 else if ((vi
= lookup_call_clobber_vi (stmt
)) != NULL
)
7309 *pt
= find_what_var_points_to (vi
);
7310 /* Escaped (and thus nonlocal) variables are always
7311 implicitly clobbered by calls. */
7312 /* ??? ESCAPED can be empty even though NONLOCAL
7315 pt
->ipa_escaped
= 1;
7319 /* If there is nothing special about this call then
7320 we have made everything that is used also escape. */
7321 *pt
= ipa_escaped_pt
;
7326 /* Handle indirect calls. */
7328 && (fi
= get_fi_for_callee (stmt
)))
7330 /* We need to accumulate all clobbers/uses of all possible
7332 fi
= get_varinfo (find (fi
->id
));
7333 /* If we cannot constrain the set of functions we'll end up
7334 calling we end up using/clobbering everything. */
7335 if (bitmap_bit_p (fi
->solution
, anything_id
)
7336 || bitmap_bit_p (fi
->solution
, nonlocal_id
)
7337 || bitmap_bit_p (fi
->solution
, escaped_id
))
7339 pt_solution_reset (gimple_call_clobber_set (stmt
));
7340 pt_solution_reset (gimple_call_use_set (stmt
));
7346 struct pt_solution
*uses
, *clobbers
;
7348 uses
= gimple_call_use_set (stmt
);
7349 clobbers
= gimple_call_clobber_set (stmt
);
7350 memset (uses
, 0, sizeof (struct pt_solution
));
7351 memset (clobbers
, 0, sizeof (struct pt_solution
));
7352 EXECUTE_IF_SET_IN_BITMAP (fi
->solution
, 0, i
, bi
)
7354 struct pt_solution sol
;
7356 vi
= get_varinfo (i
);
7357 if (!vi
->is_fn_info
)
7359 /* ??? We could be more precise here? */
7361 uses
->ipa_escaped
= 1;
7362 clobbers
->nonlocal
= 1;
7363 clobbers
->ipa_escaped
= 1;
7367 if (!uses
->anything
)
7369 sol
= find_what_var_points_to
7370 (first_vi_for_offset (vi
, fi_uses
));
7371 pt_solution_ior_into (uses
, &sol
);
7373 if (!clobbers
->anything
)
7375 sol
= find_what_var_points_to
7376 (first_vi_for_offset (vi
, fi_clobbers
));
7377 pt_solution_ior_into (clobbers
, &sol
);
7385 fn
->gimple_df
->ipa_pta
= true;
7388 delete_points_to_sets ();
7397 const pass_data pass_data_ipa_pta
=
7399 SIMPLE_IPA_PASS
, /* type */
7401 OPTGROUP_NONE
, /* optinfo_flags */
7402 true, /* has_gate */
7403 true, /* has_execute */
7404 TV_IPA_PTA
, /* tv_id */
7405 0, /* properties_required */
7406 0, /* properties_provided */
7407 0, /* properties_destroyed */
7408 0, /* todo_flags_start */
7409 0, /* todo_flags_finish */
7412 class pass_ipa_pta
: public simple_ipa_opt_pass
7415 pass_ipa_pta (gcc::context
*ctxt
)
7416 : simple_ipa_opt_pass (pass_data_ipa_pta
, ctxt
)
7419 /* opt_pass methods: */
7420 bool gate () { return gate_ipa_pta (); }
7421 unsigned int execute () { return ipa_pta_execute (); }
7423 }; // class pass_ipa_pta
7427 simple_ipa_opt_pass
*
7428 make_pass_ipa_pta (gcc::context
*ctxt
)
7430 return new pass_ipa_pta (ctxt
);