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"
30 #include "basic-block.h"
33 #include "gimple-iterator.h"
34 #include "gimple-ssa.h"
36 #include "tree-ssanames.h"
37 #include "tree-into-ssa.h"
39 #include "tree-inline.h"
40 #include "diagnostic-core.h"
41 #include "hash-table.h"
43 #include "tree-pass.h"
44 #include "alloc-pool.h"
45 #include "splay-tree.h"
48 #include "pointer-set.h"
50 /* The idea behind this analyzer is to generate set constraints from the
51 program, then solve the resulting constraints in order to generate the
54 Set constraints are a way of modeling program analysis problems that
55 involve sets. They consist of an inclusion constraint language,
56 describing the variables (each variable is a set) and operations that
57 are involved on the variables, and a set of rules that derive facts
58 from these operations. To solve a system of set constraints, you derive
59 all possible facts under the rules, which gives you the correct sets
62 See "Efficient Field-sensitive pointer analysis for C" by "David
63 J. Pearce and Paul H. J. Kelly and Chris Hankin, at
64 http://citeseer.ist.psu.edu/pearce04efficient.html
66 Also see "Ultra-fast Aliasing Analysis using CLA: A Million Lines
67 of C Code in a Second" by ""Nevin Heintze and Olivier Tardieu" at
68 http://citeseer.ist.psu.edu/heintze01ultrafast.html
70 There are three types of real constraint expressions, DEREF,
71 ADDRESSOF, and SCALAR. Each constraint expression consists
72 of a constraint type, a variable, and an offset.
74 SCALAR is a constraint expression type used to represent x, whether
75 it appears on the LHS or the RHS of a statement.
76 DEREF is a constraint expression type used to represent *x, whether
77 it appears on the LHS or the RHS of a statement.
78 ADDRESSOF is a constraint expression used to represent &x, whether
79 it appears on the LHS or the RHS of a statement.
81 Each pointer variable in the program is assigned an integer id, and
82 each field of a structure variable is assigned an integer id as well.
84 Structure variables are linked to their list of fields through a "next
85 field" in each variable that points to the next field in offset
87 Each variable for a structure field has
89 1. "size", that tells the size in bits of that field.
90 2. "fullsize, that tells the size in bits of the entire structure.
91 3. "offset", that tells the offset in bits from the beginning of the
92 structure to this field.
104 foo.a -> id 1, size 32, offset 0, fullsize 64, next foo.b
105 foo.b -> id 2, size 32, offset 32, fullsize 64, next NULL
106 bar -> id 3, size 32, offset 0, fullsize 32, next NULL
109 In order to solve the system of set constraints, the following is
112 1. Each constraint variable x has a solution set associated with it,
115 2. Constraints are separated into direct, copy, and complex.
116 Direct constraints are ADDRESSOF constraints that require no extra
117 processing, such as P = &Q
118 Copy constraints are those of the form P = Q.
119 Complex constraints are all the constraints involving dereferences
120 and offsets (including offsetted copies).
122 3. All direct constraints of the form P = &Q are processed, such
123 that Q is added to Sol(P)
125 4. All complex constraints for a given constraint variable are stored in a
126 linked list attached to that variable's node.
128 5. A directed graph is built out of the copy constraints. Each
129 constraint variable is a node in the graph, and an edge from
130 Q to P is added for each copy constraint of the form P = Q
132 6. The graph is then walked, and solution sets are
133 propagated along the copy edges, such that an edge from Q to P
134 causes Sol(P) <- Sol(P) union Sol(Q).
136 7. As we visit each node, all complex constraints associated with
137 that node are processed by adding appropriate copy edges to the graph, or the
138 appropriate variables to the solution set.
140 8. The process of walking the graph is iterated until no solution
143 Prior to walking the graph in steps 6 and 7, We perform static
144 cycle elimination on the constraint graph, as well
145 as off-line variable substitution.
147 TODO: Adding offsets to pointer-to-structures can be handled (IE not punted
148 on and turned into anything), but isn't. You can just see what offset
149 inside the pointed-to struct it's going to access.
151 TODO: Constant bounded arrays can be handled as if they were structs of the
152 same number of elements.
154 TODO: Modeling heap and incoming pointers becomes much better if we
155 add fields to them as we discover them, which we could do.
157 TODO: We could handle unions, but to be honest, it's probably not
158 worth the pain or slowdown. */
160 /* IPA-PTA optimizations possible.
162 When the indirect function called is ANYTHING we can add disambiguation
163 based on the function signatures (or simply the parameter count which
164 is the varinfo size). We also do not need to consider functions that
165 do not have their address taken.
167 The is_global_var bit which marks escape points is overly conservative
168 in IPA mode. Split it to is_escape_point and is_global_var - only
169 externally visible globals are escape points in IPA mode. This is
170 also needed to fix the pt_solution_includes_global predicate
171 (and thus ptr_deref_may_alias_global_p).
173 The way we introduce DECL_PT_UID to avoid fixing up all points-to
174 sets in the translation unit when we copy a DECL during inlining
175 pessimizes precision. The advantage is that the DECL_PT_UID keeps
176 compile-time and memory usage overhead low - the points-to sets
177 do not grow or get unshared as they would during a fixup phase.
178 An alternative solution is to delay IPA PTA until after all
179 inlining transformations have been applied.
181 The way we propagate clobber/use information isn't optimized.
182 It should use a new complex constraint that properly filters
183 out local variables of the callee (though that would make
184 the sets invalid after inlining). OTOH we might as well
185 admit defeat to WHOPR and simply do all the clobber/use analysis
186 and propagation after PTA finished but before we threw away
187 points-to information for memory variables. WHOPR and PTA
188 do not play along well anyway - the whole constraint solving
189 would need to be done in WPA phase and it will be very interesting
190 to apply the results to local SSA names during LTRANS phase.
192 We probably should compute a per-function unit-ESCAPE solution
193 propagating it simply like the clobber / uses solutions. The
194 solution can go alongside the non-IPA espaced solution and be
195 used to query which vars escape the unit through a function.
197 We never put function decls in points-to sets so we do not
198 keep the set of called functions for indirect calls.
200 And probably more. */
202 static bool use_field_sensitive
= true;
203 static int in_ipa_mode
= 0;
205 /* Used for predecessor bitmaps. */
206 static bitmap_obstack predbitmap_obstack
;
208 /* Used for points-to sets. */
209 static bitmap_obstack pta_obstack
;
211 /* Used for oldsolution members of variables. */
212 static bitmap_obstack oldpta_obstack
;
214 /* Used for per-solver-iteration bitmaps. */
215 static bitmap_obstack iteration_obstack
;
217 static unsigned int create_variable_info_for (tree
, const char *);
218 typedef struct constraint_graph
*constraint_graph_t
;
219 static void unify_nodes (constraint_graph_t
, unsigned int, unsigned int, bool);
222 typedef struct constraint
*constraint_t
;
225 #define EXECUTE_IF_IN_NONNULL_BITMAP(a, b, c, d) \
227 EXECUTE_IF_SET_IN_BITMAP (a, b, c, d)
229 static struct constraint_stats
231 unsigned int total_vars
;
232 unsigned int nonpointer_vars
;
233 unsigned int unified_vars_static
;
234 unsigned int unified_vars_dynamic
;
235 unsigned int iterations
;
236 unsigned int num_edges
;
237 unsigned int num_implicit_edges
;
238 unsigned int points_to_sets_created
;
243 /* ID of this variable */
246 /* True if this is a variable created by the constraint analysis, such as
247 heap variables and constraints we had to break up. */
248 unsigned int is_artificial_var
: 1;
250 /* True if this is a special variable whose solution set should not be
252 unsigned int is_special_var
: 1;
254 /* True for variables whose size is not known or variable. */
255 unsigned int is_unknown_size_var
: 1;
257 /* True for (sub-)fields that represent a whole variable. */
258 unsigned int is_full_var
: 1;
260 /* True if this is a heap variable. */
261 unsigned int is_heap_var
: 1;
263 /* True if this field may contain pointers. */
264 unsigned int may_have_pointers
: 1;
266 /* True if this field has only restrict qualified pointers. */
267 unsigned int only_restrict_pointers
: 1;
269 /* True if this represents a global variable. */
270 unsigned int is_global_var
: 1;
272 /* True if this represents a IPA function info. */
273 unsigned int is_fn_info
: 1;
275 /* The ID of the variable for the next field in this structure
276 or zero for the last field in this structure. */
279 /* The ID of the variable for the first field in this structure. */
282 /* Offset of this variable, in bits, from the base variable */
283 unsigned HOST_WIDE_INT offset
;
285 /* Size of the variable, in bits. */
286 unsigned HOST_WIDE_INT size
;
288 /* Full size of the base variable, in bits. */
289 unsigned HOST_WIDE_INT fullsize
;
291 /* Name of this variable */
294 /* Tree that this variable is associated with. */
297 /* Points-to set for this variable. */
300 /* Old points-to set for this variable. */
303 typedef struct variable_info
*varinfo_t
;
305 static varinfo_t
first_vi_for_offset (varinfo_t
, unsigned HOST_WIDE_INT
);
306 static varinfo_t
first_or_preceding_vi_for_offset (varinfo_t
,
307 unsigned HOST_WIDE_INT
);
308 static varinfo_t
lookup_vi_for_tree (tree
);
309 static inline bool type_can_have_subvars (const_tree
);
311 /* Pool of variable info structures. */
312 static alloc_pool variable_info_pool
;
314 /* Map varinfo to final pt_solution. */
315 static pointer_map_t
*final_solutions
;
316 struct obstack final_solutions_obstack
;
318 /* Table of variable info structures for constraint variables.
319 Indexed directly by variable info id. */
320 static vec
<varinfo_t
> varmap
;
322 /* Return the varmap element N */
324 static inline varinfo_t
325 get_varinfo (unsigned int n
)
330 /* Return the next variable in the list of sub-variables of VI
331 or NULL if VI is the last sub-variable. */
333 static inline varinfo_t
334 vi_next (varinfo_t vi
)
336 return get_varinfo (vi
->next
);
339 /* Static IDs for the special variables. Variable ID zero is unused
340 and used as terminator for the sub-variable chain. */
341 enum { nothing_id
= 1, anything_id
= 2, readonly_id
= 3,
342 escaped_id
= 4, nonlocal_id
= 5,
343 storedanything_id
= 6, integer_id
= 7 };
345 /* Return a new variable info structure consisting for a variable
346 named NAME, and using constraint graph node NODE. Append it
347 to the vector of variable info structures. */
350 new_var_info (tree t
, const char *name
)
352 unsigned index
= varmap
.length ();
353 varinfo_t ret
= (varinfo_t
) pool_alloc (variable_info_pool
);
358 /* Vars without decl are artificial and do not have sub-variables. */
359 ret
->is_artificial_var
= (t
== NULL_TREE
);
360 ret
->is_special_var
= false;
361 ret
->is_unknown_size_var
= false;
362 ret
->is_full_var
= (t
== NULL_TREE
);
363 ret
->is_heap_var
= false;
364 ret
->may_have_pointers
= true;
365 ret
->only_restrict_pointers
= false;
366 ret
->is_global_var
= (t
== NULL_TREE
);
367 ret
->is_fn_info
= false;
369 ret
->is_global_var
= (is_global_var (t
)
370 /* We have to treat even local register variables
372 || (TREE_CODE (t
) == VAR_DECL
373 && DECL_HARD_REGISTER (t
)));
374 ret
->solution
= BITMAP_ALLOC (&pta_obstack
);
375 ret
->oldsolution
= NULL
;
381 varmap
.safe_push (ret
);
387 /* A map mapping call statements to per-stmt variables for uses
388 and clobbers specific to the call. */
389 static struct pointer_map_t
*call_stmt_vars
;
391 /* Lookup or create the variable for the call statement CALL. */
394 get_call_vi (gimple call
)
399 slot_p
= pointer_map_insert (call_stmt_vars
, call
);
401 return (varinfo_t
) *slot_p
;
403 vi
= new_var_info (NULL_TREE
, "CALLUSED");
407 vi
->is_full_var
= true;
409 vi2
= new_var_info (NULL_TREE
, "CALLCLOBBERED");
413 vi2
->is_full_var
= true;
417 *slot_p
= (void *) vi
;
421 /* Lookup the variable for the call statement CALL representing
422 the uses. Returns NULL if there is nothing special about this call. */
425 lookup_call_use_vi (gimple call
)
429 slot_p
= pointer_map_contains (call_stmt_vars
, call
);
431 return (varinfo_t
) *slot_p
;
436 /* Lookup the variable for the call statement CALL representing
437 the clobbers. Returns NULL if there is nothing special about this call. */
440 lookup_call_clobber_vi (gimple call
)
442 varinfo_t uses
= lookup_call_use_vi (call
);
446 return vi_next (uses
);
449 /* Lookup or create the variable for the call statement CALL representing
453 get_call_use_vi (gimple call
)
455 return get_call_vi (call
);
458 /* Lookup or create the variable for the call statement CALL representing
461 static varinfo_t ATTRIBUTE_UNUSED
462 get_call_clobber_vi (gimple call
)
464 return vi_next (get_call_vi (call
));
468 typedef enum {SCALAR
, DEREF
, ADDRESSOF
} constraint_expr_type
;
470 /* An expression that appears in a constraint. */
472 struct constraint_expr
474 /* Constraint type. */
475 constraint_expr_type type
;
477 /* Variable we are referring to in the constraint. */
480 /* Offset, in bits, of this constraint from the beginning of
481 variables it ends up referring to.
483 IOW, in a deref constraint, we would deref, get the result set,
484 then add OFFSET to each member. */
485 HOST_WIDE_INT offset
;
488 /* Use 0x8000... as special unknown offset. */
489 #define UNKNOWN_OFFSET HOST_WIDE_INT_MIN
491 typedef struct constraint_expr ce_s
;
492 static void get_constraint_for_1 (tree
, vec
<ce_s
> *, bool, bool);
493 static void get_constraint_for (tree
, vec
<ce_s
> *);
494 static void get_constraint_for_rhs (tree
, vec
<ce_s
> *);
495 static void do_deref (vec
<ce_s
> *);
497 /* Our set constraints are made up of two constraint expressions, one
500 As described in the introduction, our set constraints each represent an
501 operation between set valued variables.
505 struct constraint_expr lhs
;
506 struct constraint_expr rhs
;
509 /* List of constraints that we use to build the constraint graph from. */
511 static vec
<constraint_t
> constraints
;
512 static alloc_pool constraint_pool
;
514 /* The constraint graph is represented as an array of bitmaps
515 containing successor nodes. */
517 struct constraint_graph
519 /* Size of this graph, which may be different than the number of
520 nodes in the variable map. */
523 /* Explicit successors of each node. */
526 /* Implicit predecessors of each node (Used for variable
528 bitmap
*implicit_preds
;
530 /* Explicit predecessors of each node (Used for variable substitution). */
533 /* Indirect cycle representatives, or -1 if the node has no indirect
535 int *indirect_cycles
;
537 /* Representative node for a node. rep[a] == a unless the node has
541 /* Equivalence class representative for a label. This is used for
542 variable substitution. */
545 /* Pointer equivalence label for a node. All nodes with the same
546 pointer equivalence label can be unified together at some point
547 (either during constraint optimization or after the constraint
551 /* Pointer equivalence representative for a label. This is used to
552 handle nodes that are pointer equivalent but not location
553 equivalent. We can unite these once the addressof constraints
554 are transformed into initial points-to sets. */
557 /* Pointer equivalence label for each node, used during variable
559 unsigned int *pointer_label
;
561 /* Location equivalence label for each node, used during location
562 equivalence finding. */
563 unsigned int *loc_label
;
565 /* Pointed-by set for each node, used during location equivalence
566 finding. This is pointed-by rather than pointed-to, because it
567 is constructed using the predecessor graph. */
570 /* Points to sets for pointer equivalence. This is *not* the actual
571 points-to sets for nodes. */
574 /* Bitmap of nodes where the bit is set if the node is a direct
575 node. Used for variable substitution. */
576 sbitmap direct_nodes
;
578 /* Bitmap of nodes where the bit is set if the node is address
579 taken. Used for variable substitution. */
580 bitmap address_taken
;
582 /* Vector of complex constraints for each graph node. Complex
583 constraints are those involving dereferences or offsets that are
585 vec
<constraint_t
> *complex;
588 static constraint_graph_t graph
;
590 /* During variable substitution and the offline version of indirect
591 cycle finding, we create nodes to represent dereferences and
592 address taken constraints. These represent where these start and
594 #define FIRST_REF_NODE (varmap).length ()
595 #define LAST_REF_NODE (FIRST_REF_NODE + (FIRST_REF_NODE - 1))
597 /* Return the representative node for NODE, if NODE has been unioned
599 This function performs path compression along the way to finding
600 the representative. */
603 find (unsigned int node
)
605 gcc_checking_assert (node
< graph
->size
);
606 if (graph
->rep
[node
] != node
)
607 return graph
->rep
[node
] = find (graph
->rep
[node
]);
611 /* Union the TO and FROM nodes to the TO nodes.
612 Note that at some point in the future, we may want to do
613 union-by-rank, in which case we are going to have to return the
614 node we unified to. */
617 unite (unsigned int to
, unsigned int from
)
619 gcc_checking_assert (to
< graph
->size
&& from
< graph
->size
);
620 if (to
!= from
&& graph
->rep
[from
] != to
)
622 graph
->rep
[from
] = to
;
628 /* Create a new constraint consisting of LHS and RHS expressions. */
631 new_constraint (const struct constraint_expr lhs
,
632 const struct constraint_expr rhs
)
634 constraint_t ret
= (constraint_t
) pool_alloc (constraint_pool
);
640 /* Print out constraint C to FILE. */
643 dump_constraint (FILE *file
, constraint_t c
)
645 if (c
->lhs
.type
== ADDRESSOF
)
647 else if (c
->lhs
.type
== DEREF
)
649 fprintf (file
, "%s", get_varinfo (c
->lhs
.var
)->name
);
650 if (c
->lhs
.offset
== UNKNOWN_OFFSET
)
651 fprintf (file
, " + UNKNOWN");
652 else if (c
->lhs
.offset
!= 0)
653 fprintf (file
, " + " HOST_WIDE_INT_PRINT_DEC
, c
->lhs
.offset
);
654 fprintf (file
, " = ");
655 if (c
->rhs
.type
== ADDRESSOF
)
657 else if (c
->rhs
.type
== DEREF
)
659 fprintf (file
, "%s", get_varinfo (c
->rhs
.var
)->name
);
660 if (c
->rhs
.offset
== UNKNOWN_OFFSET
)
661 fprintf (file
, " + UNKNOWN");
662 else if (c
->rhs
.offset
!= 0)
663 fprintf (file
, " + " HOST_WIDE_INT_PRINT_DEC
, c
->rhs
.offset
);
667 void debug_constraint (constraint_t
);
668 void debug_constraints (void);
669 void debug_constraint_graph (void);
670 void debug_solution_for_var (unsigned int);
671 void debug_sa_points_to_info (void);
673 /* Print out constraint C to stderr. */
676 debug_constraint (constraint_t c
)
678 dump_constraint (stderr
, c
);
679 fprintf (stderr
, "\n");
682 /* Print out all constraints to FILE */
685 dump_constraints (FILE *file
, int from
)
689 for (i
= from
; constraints
.iterate (i
, &c
); i
++)
692 dump_constraint (file
, c
);
693 fprintf (file
, "\n");
697 /* Print out all constraints to stderr. */
700 debug_constraints (void)
702 dump_constraints (stderr
, 0);
705 /* Print the constraint graph in dot format. */
708 dump_constraint_graph (FILE *file
)
712 /* Only print the graph if it has already been initialized: */
716 /* Prints the header of the dot file: */
717 fprintf (file
, "strict digraph {\n");
718 fprintf (file
, " node [\n shape = box\n ]\n");
719 fprintf (file
, " edge [\n fontsize = \"12\"\n ]\n");
720 fprintf (file
, "\n // List of nodes and complex constraints in "
721 "the constraint graph:\n");
723 /* The next lines print the nodes in the graph together with the
724 complex constraints attached to them. */
725 for (i
= 1; i
< graph
->size
; i
++)
727 if (i
== FIRST_REF_NODE
)
731 if (i
< FIRST_REF_NODE
)
732 fprintf (file
, "\"%s\"", get_varinfo (i
)->name
);
734 fprintf (file
, "\"*%s\"", get_varinfo (i
- FIRST_REF_NODE
)->name
);
735 if (graph
->complex[i
].exists ())
739 fprintf (file
, " [label=\"\\N\\n");
740 for (j
= 0; graph
->complex[i
].iterate (j
, &c
); ++j
)
742 dump_constraint (file
, c
);
743 fprintf (file
, "\\l");
745 fprintf (file
, "\"]");
747 fprintf (file
, ";\n");
750 /* Go over the edges. */
751 fprintf (file
, "\n // Edges in the constraint graph:\n");
752 for (i
= 1; i
< graph
->size
; i
++)
758 EXECUTE_IF_IN_NONNULL_BITMAP (graph
->succs
[i
], 0, j
, bi
)
760 unsigned to
= find (j
);
763 if (i
< FIRST_REF_NODE
)
764 fprintf (file
, "\"%s\"", get_varinfo (i
)->name
);
766 fprintf (file
, "\"*%s\"", get_varinfo (i
- FIRST_REF_NODE
)->name
);
767 fprintf (file
, " -> ");
768 if (to
< FIRST_REF_NODE
)
769 fprintf (file
, "\"%s\"", get_varinfo (to
)->name
);
771 fprintf (file
, "\"*%s\"", get_varinfo (to
- FIRST_REF_NODE
)->name
);
772 fprintf (file
, ";\n");
776 /* Prints the tail of the dot file. */
777 fprintf (file
, "}\n");
780 /* Print out the constraint graph to stderr. */
783 debug_constraint_graph (void)
785 dump_constraint_graph (stderr
);
790 The solver is a simple worklist solver, that works on the following
793 sbitmap changed_nodes = all zeroes;
795 For each node that is not already collapsed:
797 set bit in changed nodes
799 while (changed_count > 0)
801 compute topological ordering for constraint graph
803 find and collapse cycles in the constraint graph (updating
804 changed if necessary)
806 for each node (n) in the graph in topological order:
809 Process each complex constraint associated with the node,
810 updating changed if necessary.
812 For each outgoing edge from n, propagate the solution from n to
813 the destination of the edge, updating changed as necessary.
817 /* Return true if two constraint expressions A and B are equal. */
820 constraint_expr_equal (struct constraint_expr a
, struct constraint_expr b
)
822 return a
.type
== b
.type
&& a
.var
== b
.var
&& a
.offset
== b
.offset
;
825 /* Return true if constraint expression A is less than constraint expression
826 B. This is just arbitrary, but consistent, in order to give them an
830 constraint_expr_less (struct constraint_expr a
, struct constraint_expr b
)
832 if (a
.type
== b
.type
)
835 return a
.offset
< b
.offset
;
837 return a
.var
< b
.var
;
840 return a
.type
< b
.type
;
843 /* Return true if constraint A is less than constraint B. This is just
844 arbitrary, but consistent, in order to give them an ordering. */
847 constraint_less (const constraint_t
&a
, const constraint_t
&b
)
849 if (constraint_expr_less (a
->lhs
, b
->lhs
))
851 else if (constraint_expr_less (b
->lhs
, a
->lhs
))
854 return constraint_expr_less (a
->rhs
, b
->rhs
);
857 /* Return true if two constraints A and B are equal. */
860 constraint_equal (struct constraint a
, struct constraint b
)
862 return constraint_expr_equal (a
.lhs
, b
.lhs
)
863 && constraint_expr_equal (a
.rhs
, b
.rhs
);
867 /* Find a constraint LOOKFOR in the sorted constraint vector VEC */
870 constraint_vec_find (vec
<constraint_t
> vec
,
871 struct constraint lookfor
)
879 place
= vec
.lower_bound (&lookfor
, constraint_less
);
880 if (place
>= vec
.length ())
883 if (!constraint_equal (*found
, lookfor
))
888 /* Union two constraint vectors, TO and FROM. Put the result in TO. */
891 constraint_set_union (vec
<constraint_t
> *to
,
892 vec
<constraint_t
> *from
)
897 FOR_EACH_VEC_ELT (*from
, i
, c
)
899 if (constraint_vec_find (*to
, *c
) == NULL
)
901 unsigned int place
= to
->lower_bound (c
, constraint_less
);
902 to
->safe_insert (place
, c
);
907 /* Expands the solution in SET to all sub-fields of variables included. */
910 solution_set_expand (bitmap set
)
915 /* In a first pass expand to the head of the variables we need to
916 add all sub-fields off. This avoids quadratic behavior. */
917 EXECUTE_IF_SET_IN_BITMAP (set
, 0, j
, bi
)
919 varinfo_t v
= get_varinfo (j
);
920 if (v
->is_artificial_var
923 bitmap_set_bit (set
, v
->head
);
926 /* In the second pass now expand all head variables with subfields. */
927 EXECUTE_IF_SET_IN_BITMAP (set
, 0, j
, bi
)
929 varinfo_t v
= get_varinfo (j
);
930 if (v
->is_artificial_var
934 for (v
= vi_next (v
); v
!= NULL
; v
= vi_next (v
))
935 bitmap_set_bit (set
, v
->id
);
939 /* Union solution sets TO and FROM, and add INC to each member of FROM in the
943 set_union_with_increment (bitmap to
, bitmap from
, HOST_WIDE_INT inc
)
945 bool changed
= false;
949 /* If the solution of FROM contains anything it is good enough to transfer
951 if (bitmap_bit_p (from
, anything_id
))
952 return bitmap_set_bit (to
, anything_id
);
954 /* For zero offset simply union the solution into the destination. */
956 return bitmap_ior_into (to
, from
);
958 /* If the offset is unknown we have to expand the solution to
960 if (inc
== UNKNOWN_OFFSET
)
962 bitmap tmp
= BITMAP_ALLOC (&iteration_obstack
);
963 bitmap_copy (tmp
, from
);
964 solution_set_expand (tmp
);
965 changed
|= bitmap_ior_into (to
, tmp
);
970 /* For non-zero offset union the offsetted solution into the destination. */
971 EXECUTE_IF_SET_IN_BITMAP (from
, 0, i
, bi
)
973 varinfo_t vi
= get_varinfo (i
);
975 /* If this is a variable with just one field just set its bit
977 if (vi
->is_artificial_var
978 || vi
->is_unknown_size_var
980 changed
|= bitmap_set_bit (to
, i
);
983 unsigned HOST_WIDE_INT fieldoffset
= vi
->offset
+ inc
;
985 /* If the offset makes the pointer point to before the
986 variable use offset zero for the field lookup. */
988 && fieldoffset
> vi
->offset
)
991 vi
= first_or_preceding_vi_for_offset (vi
, fieldoffset
);
993 changed
|= bitmap_set_bit (to
, vi
->id
);
994 /* If the result is not exactly at fieldoffset include the next
995 field as well. See get_constraint_for_ptr_offset for more
997 if (vi
->offset
!= fieldoffset
999 changed
|= bitmap_set_bit (to
, vi
->next
);
1006 /* Insert constraint C into the list of complex constraints for graph
1010 insert_into_complex (constraint_graph_t graph
,
1011 unsigned int var
, constraint_t c
)
1013 vec
<constraint_t
> complex = graph
->complex[var
];
1014 unsigned int place
= complex.lower_bound (c
, constraint_less
);
1016 /* Only insert constraints that do not already exist. */
1017 if (place
>= complex.length ()
1018 || !constraint_equal (*c
, *complex[place
]))
1019 graph
->complex[var
].safe_insert (place
, c
);
1023 /* Condense two variable nodes into a single variable node, by moving
1024 all associated info from SRC to TO. */
1027 merge_node_constraints (constraint_graph_t graph
, unsigned int to
,
1033 gcc_checking_assert (find (from
) == to
);
1035 /* Move all complex constraints from src node into to node */
1036 FOR_EACH_VEC_ELT (graph
->complex[from
], i
, c
)
1038 /* In complex constraints for node src, we may have either
1039 a = *src, and *src = a, or an offseted constraint which are
1040 always added to the rhs node's constraints. */
1042 if (c
->rhs
.type
== DEREF
)
1044 else if (c
->lhs
.type
== DEREF
)
1049 constraint_set_union (&graph
->complex[to
], &graph
->complex[from
]);
1050 graph
->complex[from
].release ();
1054 /* Remove edges involving NODE from GRAPH. */
1057 clear_edges_for_node (constraint_graph_t graph
, unsigned int node
)
1059 if (graph
->succs
[node
])
1060 BITMAP_FREE (graph
->succs
[node
]);
1063 /* Merge GRAPH nodes FROM and TO into node TO. */
1066 merge_graph_nodes (constraint_graph_t graph
, unsigned int to
,
1069 if (graph
->indirect_cycles
[from
] != -1)
1071 /* If we have indirect cycles with the from node, and we have
1072 none on the to node, the to node has indirect cycles from the
1073 from node now that they are unified.
1074 If indirect cycles exist on both, unify the nodes that they
1075 are in a cycle with, since we know they are in a cycle with
1077 if (graph
->indirect_cycles
[to
] == -1)
1078 graph
->indirect_cycles
[to
] = graph
->indirect_cycles
[from
];
1081 /* Merge all the successor edges. */
1082 if (graph
->succs
[from
])
1084 if (!graph
->succs
[to
])
1085 graph
->succs
[to
] = BITMAP_ALLOC (&pta_obstack
);
1086 bitmap_ior_into (graph
->succs
[to
],
1087 graph
->succs
[from
]);
1090 clear_edges_for_node (graph
, from
);
1094 /* Add an indirect graph edge to GRAPH, going from TO to FROM if
1095 it doesn't exist in the graph already. */
1098 add_implicit_graph_edge (constraint_graph_t graph
, unsigned int to
,
1104 if (!graph
->implicit_preds
[to
])
1105 graph
->implicit_preds
[to
] = BITMAP_ALLOC (&predbitmap_obstack
);
1107 if (bitmap_set_bit (graph
->implicit_preds
[to
], from
))
1108 stats
.num_implicit_edges
++;
1111 /* Add a predecessor graph edge to GRAPH, going from TO to FROM if
1112 it doesn't exist in the graph already.
1113 Return false if the edge already existed, true otherwise. */
1116 add_pred_graph_edge (constraint_graph_t graph
, unsigned int to
,
1119 if (!graph
->preds
[to
])
1120 graph
->preds
[to
] = BITMAP_ALLOC (&predbitmap_obstack
);
1121 bitmap_set_bit (graph
->preds
[to
], from
);
1124 /* Add a graph edge to GRAPH, going from FROM to TO if
1125 it doesn't exist in the graph already.
1126 Return false if the edge already existed, true otherwise. */
1129 add_graph_edge (constraint_graph_t graph
, unsigned int to
,
1140 if (!graph
->succs
[from
])
1141 graph
->succs
[from
] = BITMAP_ALLOC (&pta_obstack
);
1142 if (bitmap_set_bit (graph
->succs
[from
], to
))
1145 if (to
< FIRST_REF_NODE
&& from
< FIRST_REF_NODE
)
1153 /* Initialize the constraint graph structure to contain SIZE nodes. */
1156 init_graph (unsigned int size
)
1160 graph
= XCNEW (struct constraint_graph
);
1162 graph
->succs
= XCNEWVEC (bitmap
, graph
->size
);
1163 graph
->indirect_cycles
= XNEWVEC (int, graph
->size
);
1164 graph
->rep
= XNEWVEC (unsigned int, graph
->size
);
1165 /* ??? Macros do not support template types with multiple arguments,
1166 so we use a typedef to work around it. */
1167 typedef vec
<constraint_t
> vec_constraint_t_heap
;
1168 graph
->complex = XCNEWVEC (vec_constraint_t_heap
, size
);
1169 graph
->pe
= XCNEWVEC (unsigned int, graph
->size
);
1170 graph
->pe_rep
= XNEWVEC (int, graph
->size
);
1172 for (j
= 0; j
< graph
->size
; j
++)
1175 graph
->pe_rep
[j
] = -1;
1176 graph
->indirect_cycles
[j
] = -1;
1180 /* Build the constraint graph, adding only predecessor edges right now. */
1183 build_pred_graph (void)
1189 graph
->implicit_preds
= XCNEWVEC (bitmap
, graph
->size
);
1190 graph
->preds
= XCNEWVEC (bitmap
, graph
->size
);
1191 graph
->pointer_label
= XCNEWVEC (unsigned int, graph
->size
);
1192 graph
->loc_label
= XCNEWVEC (unsigned int, graph
->size
);
1193 graph
->pointed_by
= XCNEWVEC (bitmap
, graph
->size
);
1194 graph
->points_to
= XCNEWVEC (bitmap
, graph
->size
);
1195 graph
->eq_rep
= XNEWVEC (int, graph
->size
);
1196 graph
->direct_nodes
= sbitmap_alloc (graph
->size
);
1197 graph
->address_taken
= BITMAP_ALLOC (&predbitmap_obstack
);
1198 bitmap_clear (graph
->direct_nodes
);
1200 for (j
= 1; j
< FIRST_REF_NODE
; j
++)
1202 if (!get_varinfo (j
)->is_special_var
)
1203 bitmap_set_bit (graph
->direct_nodes
, j
);
1206 for (j
= 0; j
< graph
->size
; j
++)
1207 graph
->eq_rep
[j
] = -1;
1209 for (j
= 0; j
< varmap
.length (); j
++)
1210 graph
->indirect_cycles
[j
] = -1;
1212 FOR_EACH_VEC_ELT (constraints
, i
, c
)
1214 struct constraint_expr lhs
= c
->lhs
;
1215 struct constraint_expr rhs
= c
->rhs
;
1216 unsigned int lhsvar
= lhs
.var
;
1217 unsigned int rhsvar
= rhs
.var
;
1219 if (lhs
.type
== DEREF
)
1222 if (rhs
.offset
== 0 && lhs
.offset
== 0 && rhs
.type
== SCALAR
)
1223 add_pred_graph_edge (graph
, FIRST_REF_NODE
+ lhsvar
, rhsvar
);
1225 else if (rhs
.type
== DEREF
)
1228 if (rhs
.offset
== 0 && lhs
.offset
== 0 && lhs
.type
== SCALAR
)
1229 add_pred_graph_edge (graph
, lhsvar
, FIRST_REF_NODE
+ rhsvar
);
1231 bitmap_clear_bit (graph
->direct_nodes
, lhsvar
);
1233 else if (rhs
.type
== ADDRESSOF
)
1238 if (graph
->points_to
[lhsvar
] == NULL
)
1239 graph
->points_to
[lhsvar
] = BITMAP_ALLOC (&predbitmap_obstack
);
1240 bitmap_set_bit (graph
->points_to
[lhsvar
], rhsvar
);
1242 if (graph
->pointed_by
[rhsvar
] == NULL
)
1243 graph
->pointed_by
[rhsvar
] = BITMAP_ALLOC (&predbitmap_obstack
);
1244 bitmap_set_bit (graph
->pointed_by
[rhsvar
], lhsvar
);
1246 /* Implicitly, *x = y */
1247 add_implicit_graph_edge (graph
, FIRST_REF_NODE
+ lhsvar
, rhsvar
);
1249 /* All related variables are no longer direct nodes. */
1250 bitmap_clear_bit (graph
->direct_nodes
, rhsvar
);
1251 v
= get_varinfo (rhsvar
);
1252 if (!v
->is_full_var
)
1254 v
= get_varinfo (v
->head
);
1257 bitmap_clear_bit (graph
->direct_nodes
, v
->id
);
1262 bitmap_set_bit (graph
->address_taken
, rhsvar
);
1264 else if (lhsvar
> anything_id
1265 && lhsvar
!= rhsvar
&& lhs
.offset
== 0 && rhs
.offset
== 0)
1268 add_pred_graph_edge (graph
, lhsvar
, rhsvar
);
1269 /* Implicitly, *x = *y */
1270 add_implicit_graph_edge (graph
, FIRST_REF_NODE
+ lhsvar
,
1271 FIRST_REF_NODE
+ rhsvar
);
1273 else if (lhs
.offset
!= 0 || rhs
.offset
!= 0)
1275 if (rhs
.offset
!= 0)
1276 bitmap_clear_bit (graph
->direct_nodes
, lhs
.var
);
1277 else if (lhs
.offset
!= 0)
1278 bitmap_clear_bit (graph
->direct_nodes
, rhs
.var
);
1283 /* Build the constraint graph, adding successor edges. */
1286 build_succ_graph (void)
1291 FOR_EACH_VEC_ELT (constraints
, i
, c
)
1293 struct constraint_expr lhs
;
1294 struct constraint_expr rhs
;
1295 unsigned int lhsvar
;
1296 unsigned int rhsvar
;
1303 lhsvar
= find (lhs
.var
);
1304 rhsvar
= find (rhs
.var
);
1306 if (lhs
.type
== DEREF
)
1308 if (rhs
.offset
== 0 && lhs
.offset
== 0 && rhs
.type
== SCALAR
)
1309 add_graph_edge (graph
, FIRST_REF_NODE
+ lhsvar
, rhsvar
);
1311 else if (rhs
.type
== DEREF
)
1313 if (rhs
.offset
== 0 && lhs
.offset
== 0 && lhs
.type
== SCALAR
)
1314 add_graph_edge (graph
, lhsvar
, FIRST_REF_NODE
+ rhsvar
);
1316 else if (rhs
.type
== ADDRESSOF
)
1319 gcc_checking_assert (find (rhs
.var
) == rhs
.var
);
1320 bitmap_set_bit (get_varinfo (lhsvar
)->solution
, rhsvar
);
1322 else if (lhsvar
> anything_id
1323 && lhsvar
!= rhsvar
&& lhs
.offset
== 0 && rhs
.offset
== 0)
1325 add_graph_edge (graph
, lhsvar
, rhsvar
);
1329 /* Add edges from STOREDANYTHING to all non-direct nodes that can
1330 receive pointers. */
1331 t
= find (storedanything_id
);
1332 for (i
= integer_id
+ 1; i
< FIRST_REF_NODE
; ++i
)
1334 if (!bitmap_bit_p (graph
->direct_nodes
, i
)
1335 && get_varinfo (i
)->may_have_pointers
)
1336 add_graph_edge (graph
, find (i
), t
);
1339 /* Everything stored to ANYTHING also potentially escapes. */
1340 add_graph_edge (graph
, find (escaped_id
), t
);
1344 /* Changed variables on the last iteration. */
1345 static bitmap changed
;
1347 /* Strongly Connected Component visitation info. */
1354 unsigned int *node_mapping
;
1356 vec
<unsigned> scc_stack
;
1360 /* Recursive routine to find strongly connected components in GRAPH.
1361 SI is the SCC info to store the information in, and N is the id of current
1362 graph node we are processing.
1364 This is Tarjan's strongly connected component finding algorithm, as
1365 modified by Nuutila to keep only non-root nodes on the stack.
1366 The algorithm can be found in "On finding the strongly connected
1367 connected components in a directed graph" by Esko Nuutila and Eljas
1368 Soisalon-Soininen, in Information Processing Letters volume 49,
1369 number 1, pages 9-14. */
1372 scc_visit (constraint_graph_t graph
, struct scc_info
*si
, unsigned int n
)
1376 unsigned int my_dfs
;
1378 bitmap_set_bit (si
->visited
, n
);
1379 si
->dfs
[n
] = si
->current_index
++;
1380 my_dfs
= si
->dfs
[n
];
1382 /* Visit all the successors. */
1383 EXECUTE_IF_IN_NONNULL_BITMAP (graph
->succs
[n
], 0, i
, bi
)
1387 if (i
> LAST_REF_NODE
)
1391 if (bitmap_bit_p (si
->deleted
, w
))
1394 if (!bitmap_bit_p (si
->visited
, w
))
1395 scc_visit (graph
, si
, w
);
1397 unsigned int t
= find (w
);
1398 gcc_checking_assert (find (n
) == n
);
1399 if (si
->dfs
[t
] < si
->dfs
[n
])
1400 si
->dfs
[n
] = si
->dfs
[t
];
1403 /* See if any components have been identified. */
1404 if (si
->dfs
[n
] == my_dfs
)
1406 if (si
->scc_stack
.length () > 0
1407 && si
->dfs
[si
->scc_stack
.last ()] >= my_dfs
)
1409 bitmap scc
= BITMAP_ALLOC (NULL
);
1410 unsigned int lowest_node
;
1413 bitmap_set_bit (scc
, n
);
1415 while (si
->scc_stack
.length () != 0
1416 && si
->dfs
[si
->scc_stack
.last ()] >= my_dfs
)
1418 unsigned int w
= si
->scc_stack
.pop ();
1420 bitmap_set_bit (scc
, w
);
1423 lowest_node
= bitmap_first_set_bit (scc
);
1424 gcc_assert (lowest_node
< FIRST_REF_NODE
);
1426 /* Collapse the SCC nodes into a single node, and mark the
1428 EXECUTE_IF_SET_IN_BITMAP (scc
, 0, i
, bi
)
1430 if (i
< FIRST_REF_NODE
)
1432 if (unite (lowest_node
, i
))
1433 unify_nodes (graph
, lowest_node
, i
, false);
1437 unite (lowest_node
, i
);
1438 graph
->indirect_cycles
[i
- FIRST_REF_NODE
] = lowest_node
;
1442 bitmap_set_bit (si
->deleted
, n
);
1445 si
->scc_stack
.safe_push (n
);
1448 /* Unify node FROM into node TO, updating the changed count if
1449 necessary when UPDATE_CHANGED is true. */
1452 unify_nodes (constraint_graph_t graph
, unsigned int to
, unsigned int from
,
1453 bool update_changed
)
1455 gcc_checking_assert (to
!= from
&& find (to
) == to
);
1457 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1458 fprintf (dump_file
, "Unifying %s to %s\n",
1459 get_varinfo (from
)->name
,
1460 get_varinfo (to
)->name
);
1463 stats
.unified_vars_dynamic
++;
1465 stats
.unified_vars_static
++;
1467 merge_graph_nodes (graph
, to
, from
);
1468 merge_node_constraints (graph
, to
, from
);
1470 /* Mark TO as changed if FROM was changed. If TO was already marked
1471 as changed, decrease the changed count. */
1474 && bitmap_clear_bit (changed
, from
))
1475 bitmap_set_bit (changed
, to
);
1476 varinfo_t fromvi
= get_varinfo (from
);
1477 if (fromvi
->solution
)
1479 /* If the solution changes because of the merging, we need to mark
1480 the variable as changed. */
1481 varinfo_t tovi
= get_varinfo (to
);
1482 if (bitmap_ior_into (tovi
->solution
, fromvi
->solution
))
1485 bitmap_set_bit (changed
, to
);
1488 BITMAP_FREE (fromvi
->solution
);
1489 if (fromvi
->oldsolution
)
1490 BITMAP_FREE (fromvi
->oldsolution
);
1492 if (stats
.iterations
> 0
1493 && tovi
->oldsolution
)
1494 BITMAP_FREE (tovi
->oldsolution
);
1496 if (graph
->succs
[to
])
1497 bitmap_clear_bit (graph
->succs
[to
], to
);
1500 /* Information needed to compute the topological ordering of a graph. */
1504 /* sbitmap of visited nodes. */
1506 /* Array that stores the topological order of the graph, *in
1508 vec
<unsigned> topo_order
;
1512 /* Initialize and return a topological info structure. */
1514 static struct topo_info
*
1515 init_topo_info (void)
1517 size_t size
= graph
->size
;
1518 struct topo_info
*ti
= XNEW (struct topo_info
);
1519 ti
->visited
= sbitmap_alloc (size
);
1520 bitmap_clear (ti
->visited
);
1521 ti
->topo_order
.create (1);
1526 /* Free the topological sort info pointed to by TI. */
1529 free_topo_info (struct topo_info
*ti
)
1531 sbitmap_free (ti
->visited
);
1532 ti
->topo_order
.release ();
1536 /* Visit the graph in topological order, and store the order in the
1537 topo_info structure. */
1540 topo_visit (constraint_graph_t graph
, struct topo_info
*ti
,
1546 bitmap_set_bit (ti
->visited
, n
);
1548 if (graph
->succs
[n
])
1549 EXECUTE_IF_SET_IN_BITMAP (graph
->succs
[n
], 0, j
, bi
)
1551 if (!bitmap_bit_p (ti
->visited
, j
))
1552 topo_visit (graph
, ti
, j
);
1555 ti
->topo_order
.safe_push (n
);
1558 /* Process a constraint C that represents x = *(y + off), using DELTA as the
1559 starting solution for y. */
1562 do_sd_constraint (constraint_graph_t graph
, constraint_t c
,
1565 unsigned int lhs
= c
->lhs
.var
;
1567 bitmap sol
= get_varinfo (lhs
)->solution
;
1570 HOST_WIDE_INT roffset
= c
->rhs
.offset
;
1572 /* Our IL does not allow this. */
1573 gcc_checking_assert (c
->lhs
.offset
== 0);
1575 /* If the solution of Y contains anything it is good enough to transfer
1577 if (bitmap_bit_p (delta
, anything_id
))
1579 flag
|= bitmap_set_bit (sol
, anything_id
);
1583 /* If we do not know at with offset the rhs is dereferenced compute
1584 the reachability set of DELTA, conservatively assuming it is
1585 dereferenced at all valid offsets. */
1586 if (roffset
== UNKNOWN_OFFSET
)
1588 solution_set_expand (delta
);
1589 /* No further offset processing is necessary. */
1593 /* For each variable j in delta (Sol(y)), add
1594 an edge in the graph from j to x, and union Sol(j) into Sol(x). */
1595 EXECUTE_IF_SET_IN_BITMAP (delta
, 0, j
, bi
)
1597 varinfo_t v
= get_varinfo (j
);
1598 HOST_WIDE_INT fieldoffset
= v
->offset
+ roffset
;
1602 fieldoffset
= v
->offset
;
1603 else if (roffset
!= 0)
1604 v
= first_vi_for_offset (v
, fieldoffset
);
1605 /* If the access is outside of the variable we can ignore it. */
1613 /* Adding edges from the special vars is pointless.
1614 They don't have sets that can change. */
1615 if (get_varinfo (t
)->is_special_var
)
1616 flag
|= bitmap_ior_into (sol
, get_varinfo (t
)->solution
);
1617 /* Merging the solution from ESCAPED needlessly increases
1618 the set. Use ESCAPED as representative instead. */
1619 else if (v
->id
== escaped_id
)
1620 flag
|= bitmap_set_bit (sol
, escaped_id
);
1621 else if (v
->may_have_pointers
1622 && add_graph_edge (graph
, lhs
, t
))
1623 flag
|= bitmap_ior_into (sol
, get_varinfo (t
)->solution
);
1625 /* If the variable is not exactly at the requested offset
1626 we have to include the next one. */
1627 if (v
->offset
== (unsigned HOST_WIDE_INT
)fieldoffset
1632 fieldoffset
= v
->offset
;
1638 /* If the LHS solution changed, mark the var as changed. */
1641 get_varinfo (lhs
)->solution
= sol
;
1642 bitmap_set_bit (changed
, lhs
);
1646 /* Process a constraint C that represents *(x + off) = y using DELTA
1647 as the starting solution for x. */
1650 do_ds_constraint (constraint_t c
, bitmap delta
)
1652 unsigned int rhs
= c
->rhs
.var
;
1653 bitmap sol
= get_varinfo (rhs
)->solution
;
1656 HOST_WIDE_INT loff
= c
->lhs
.offset
;
1657 bool escaped_p
= false;
1659 /* Our IL does not allow this. */
1660 gcc_checking_assert (c
->rhs
.offset
== 0);
1662 /* If the solution of y contains ANYTHING simply use the ANYTHING
1663 solution. This avoids needlessly increasing the points-to sets. */
1664 if (bitmap_bit_p (sol
, anything_id
))
1665 sol
= get_varinfo (find (anything_id
))->solution
;
1667 /* If the solution for x contains ANYTHING we have to merge the
1668 solution of y into all pointer variables which we do via
1670 if (bitmap_bit_p (delta
, anything_id
))
1672 unsigned t
= find (storedanything_id
);
1673 if (add_graph_edge (graph
, t
, rhs
))
1675 if (bitmap_ior_into (get_varinfo (t
)->solution
, sol
))
1676 bitmap_set_bit (changed
, t
);
1681 /* If we do not know at with offset the rhs is dereferenced compute
1682 the reachability set of DELTA, conservatively assuming it is
1683 dereferenced at all valid offsets. */
1684 if (loff
== UNKNOWN_OFFSET
)
1686 solution_set_expand (delta
);
1690 /* For each member j of delta (Sol(x)), add an edge from y to j and
1691 union Sol(y) into Sol(j) */
1692 EXECUTE_IF_SET_IN_BITMAP (delta
, 0, j
, bi
)
1694 varinfo_t v
= get_varinfo (j
);
1696 HOST_WIDE_INT fieldoffset
= v
->offset
+ loff
;
1699 fieldoffset
= v
->offset
;
1701 v
= first_vi_for_offset (v
, fieldoffset
);
1702 /* If the access is outside of the variable we can ignore it. */
1708 if (v
->may_have_pointers
)
1710 /* If v is a global variable then this is an escape point. */
1711 if (v
->is_global_var
1714 t
= find (escaped_id
);
1715 if (add_graph_edge (graph
, t
, rhs
)
1716 && bitmap_ior_into (get_varinfo (t
)->solution
, sol
))
1717 bitmap_set_bit (changed
, t
);
1718 /* Enough to let rhs escape once. */
1722 if (v
->is_special_var
)
1726 if (add_graph_edge (graph
, t
, rhs
)
1727 && bitmap_ior_into (get_varinfo (t
)->solution
, sol
))
1728 bitmap_set_bit (changed
, t
);
1731 /* If the variable is not exactly at the requested offset
1732 we have to include the next one. */
1733 if (v
->offset
== (unsigned HOST_WIDE_INT
)fieldoffset
1738 fieldoffset
= v
->offset
;
1744 /* Handle a non-simple (simple meaning requires no iteration),
1745 constraint (IE *x = &y, x = *y, *x = y, and x = y with offsets involved). */
1748 do_complex_constraint (constraint_graph_t graph
, constraint_t c
, bitmap delta
)
1750 if (c
->lhs
.type
== DEREF
)
1752 if (c
->rhs
.type
== ADDRESSOF
)
1759 do_ds_constraint (c
, delta
);
1762 else if (c
->rhs
.type
== DEREF
)
1765 if (!(get_varinfo (c
->lhs
.var
)->is_special_var
))
1766 do_sd_constraint (graph
, c
, delta
);
1774 gcc_checking_assert (c
->rhs
.type
== SCALAR
&& c
->lhs
.type
== SCALAR
);
1775 solution
= get_varinfo (c
->rhs
.var
)->solution
;
1776 tmp
= get_varinfo (c
->lhs
.var
)->solution
;
1778 flag
= set_union_with_increment (tmp
, solution
, c
->rhs
.offset
);
1781 bitmap_set_bit (changed
, c
->lhs
.var
);
1785 /* Initialize and return a new SCC info structure. */
1787 static struct scc_info
*
1788 init_scc_info (size_t size
)
1790 struct scc_info
*si
= XNEW (struct scc_info
);
1793 si
->current_index
= 0;
1794 si
->visited
= sbitmap_alloc (size
);
1795 bitmap_clear (si
->visited
);
1796 si
->deleted
= sbitmap_alloc (size
);
1797 bitmap_clear (si
->deleted
);
1798 si
->node_mapping
= XNEWVEC (unsigned int, size
);
1799 si
->dfs
= XCNEWVEC (unsigned int, size
);
1801 for (i
= 0; i
< size
; i
++)
1802 si
->node_mapping
[i
] = i
;
1804 si
->scc_stack
.create (1);
1808 /* Free an SCC info structure pointed to by SI */
1811 free_scc_info (struct scc_info
*si
)
1813 sbitmap_free (si
->visited
);
1814 sbitmap_free (si
->deleted
);
1815 free (si
->node_mapping
);
1817 si
->scc_stack
.release ();
1822 /* Find indirect cycles in GRAPH that occur, using strongly connected
1823 components, and note them in the indirect cycles map.
1825 This technique comes from Ben Hardekopf and Calvin Lin,
1826 "It Pays to be Lazy: Fast and Accurate Pointer Analysis for Millions of
1827 Lines of Code", submitted to PLDI 2007. */
1830 find_indirect_cycles (constraint_graph_t graph
)
1833 unsigned int size
= graph
->size
;
1834 struct scc_info
*si
= init_scc_info (size
);
1836 for (i
= 0; i
< MIN (LAST_REF_NODE
, size
); i
++ )
1837 if (!bitmap_bit_p (si
->visited
, i
) && find (i
) == i
)
1838 scc_visit (graph
, si
, i
);
1843 /* Compute a topological ordering for GRAPH, and store the result in the
1844 topo_info structure TI. */
1847 compute_topo_order (constraint_graph_t graph
,
1848 struct topo_info
*ti
)
1851 unsigned int size
= graph
->size
;
1853 for (i
= 0; i
!= size
; ++i
)
1854 if (!bitmap_bit_p (ti
->visited
, i
) && find (i
) == i
)
1855 topo_visit (graph
, ti
, i
);
1858 /* Structure used to for hash value numbering of pointer equivalence
1861 typedef struct equiv_class_label
1864 unsigned int equivalence_class
;
1866 } *equiv_class_label_t
;
1867 typedef const struct equiv_class_label
*const_equiv_class_label_t
;
1869 /* Equiv_class_label hashtable helpers. */
1871 struct equiv_class_hasher
: typed_free_remove
<equiv_class_label
>
1873 typedef equiv_class_label value_type
;
1874 typedef equiv_class_label compare_type
;
1875 static inline hashval_t
hash (const value_type
*);
1876 static inline bool equal (const value_type
*, const compare_type
*);
1879 /* Hash function for a equiv_class_label_t */
1882 equiv_class_hasher::hash (const value_type
*ecl
)
1884 return ecl
->hashcode
;
1887 /* Equality function for two equiv_class_label_t's. */
1890 equiv_class_hasher::equal (const value_type
*eql1
, const compare_type
*eql2
)
1892 return (eql1
->hashcode
== eql2
->hashcode
1893 && bitmap_equal_p (eql1
->labels
, eql2
->labels
));
1896 /* A hashtable for mapping a bitmap of labels->pointer equivalence
1898 static hash_table
<equiv_class_hasher
> pointer_equiv_class_table
;
1900 /* A hashtable for mapping a bitmap of labels->location equivalence
1902 static hash_table
<equiv_class_hasher
> location_equiv_class_table
;
1904 /* Lookup a equivalence class in TABLE by the bitmap of LABELS with
1905 hash HAS it contains. Sets *REF_LABELS to the bitmap LABELS
1906 is equivalent to. */
1908 static equiv_class_label
*
1909 equiv_class_lookup_or_add (hash_table
<equiv_class_hasher
> table
, bitmap labels
)
1911 equiv_class_label
**slot
;
1912 equiv_class_label ecl
;
1914 ecl
.labels
= labels
;
1915 ecl
.hashcode
= bitmap_hash (labels
);
1916 slot
= table
.find_slot_with_hash (&ecl
, ecl
.hashcode
, INSERT
);
1919 *slot
= XNEW (struct equiv_class_label
);
1920 (*slot
)->labels
= labels
;
1921 (*slot
)->hashcode
= ecl
.hashcode
;
1922 (*slot
)->equivalence_class
= 0;
1928 /* Perform offline variable substitution.
1930 This is a worst case quadratic time way of identifying variables
1931 that must have equivalent points-to sets, including those caused by
1932 static cycles, and single entry subgraphs, in the constraint graph.
1934 The technique is described in "Exploiting Pointer and Location
1935 Equivalence to Optimize Pointer Analysis. In the 14th International
1936 Static Analysis Symposium (SAS), August 2007." It is known as the
1937 "HU" algorithm, and is equivalent to value numbering the collapsed
1938 constraint graph including evaluating unions.
1940 The general method of finding equivalence classes is as follows:
1941 Add fake nodes (REF nodes) and edges for *a = b and a = *b constraints.
1942 Initialize all non-REF nodes to be direct nodes.
1943 For each constraint a = a U {b}, we set pts(a) = pts(a) u {fresh
1945 For each constraint containing the dereference, we also do the same
1948 We then compute SCC's in the graph and unify nodes in the same SCC,
1951 For each non-collapsed node x:
1952 Visit all unvisited explicit incoming edges.
1953 Ignoring all non-pointers, set pts(x) = Union of pts(a) for y
1955 Lookup the equivalence class for pts(x).
1956 If we found one, equivalence_class(x) = found class.
1957 Otherwise, equivalence_class(x) = new class, and new_class is
1958 added to the lookup table.
1960 All direct nodes with the same equivalence class can be replaced
1961 with a single representative node.
1962 All unlabeled nodes (label == 0) are not pointers and all edges
1963 involving them can be eliminated.
1964 We perform these optimizations during rewrite_constraints
1966 In addition to pointer equivalence class finding, we also perform
1967 location equivalence class finding. This is the set of variables
1968 that always appear together in points-to sets. We use this to
1969 compress the size of the points-to sets. */
1971 /* Current maximum pointer equivalence class id. */
1972 static int pointer_equiv_class
;
1974 /* Current maximum location equivalence class id. */
1975 static int location_equiv_class
;
1977 /* Recursive routine to find strongly connected components in GRAPH,
1978 and label it's nodes with DFS numbers. */
1981 condense_visit (constraint_graph_t graph
, struct scc_info
*si
, unsigned int n
)
1985 unsigned int my_dfs
;
1987 gcc_checking_assert (si
->node_mapping
[n
] == n
);
1988 bitmap_set_bit (si
->visited
, n
);
1989 si
->dfs
[n
] = si
->current_index
++;
1990 my_dfs
= si
->dfs
[n
];
1992 /* Visit all the successors. */
1993 EXECUTE_IF_IN_NONNULL_BITMAP (graph
->preds
[n
], 0, i
, bi
)
1995 unsigned int w
= si
->node_mapping
[i
];
1997 if (bitmap_bit_p (si
->deleted
, w
))
2000 if (!bitmap_bit_p (si
->visited
, w
))
2001 condense_visit (graph
, si
, w
);
2003 unsigned int t
= si
->node_mapping
[w
];
2004 gcc_checking_assert (si
->node_mapping
[n
] == n
);
2005 if (si
->dfs
[t
] < si
->dfs
[n
])
2006 si
->dfs
[n
] = si
->dfs
[t
];
2009 /* Visit all the implicit predecessors. */
2010 EXECUTE_IF_IN_NONNULL_BITMAP (graph
->implicit_preds
[n
], 0, i
, bi
)
2012 unsigned int w
= si
->node_mapping
[i
];
2014 if (bitmap_bit_p (si
->deleted
, w
))
2017 if (!bitmap_bit_p (si
->visited
, w
))
2018 condense_visit (graph
, si
, w
);
2020 unsigned int t
= si
->node_mapping
[w
];
2021 gcc_assert (si
->node_mapping
[n
] == n
);
2022 if (si
->dfs
[t
] < si
->dfs
[n
])
2023 si
->dfs
[n
] = si
->dfs
[t
];
2026 /* See if any components have been identified. */
2027 if (si
->dfs
[n
] == my_dfs
)
2029 while (si
->scc_stack
.length () != 0
2030 && si
->dfs
[si
->scc_stack
.last ()] >= my_dfs
)
2032 unsigned int w
= si
->scc_stack
.pop ();
2033 si
->node_mapping
[w
] = n
;
2035 if (!bitmap_bit_p (graph
->direct_nodes
, w
))
2036 bitmap_clear_bit (graph
->direct_nodes
, n
);
2038 /* Unify our nodes. */
2039 if (graph
->preds
[w
])
2041 if (!graph
->preds
[n
])
2042 graph
->preds
[n
] = BITMAP_ALLOC (&predbitmap_obstack
);
2043 bitmap_ior_into (graph
->preds
[n
], graph
->preds
[w
]);
2045 if (graph
->implicit_preds
[w
])
2047 if (!graph
->implicit_preds
[n
])
2048 graph
->implicit_preds
[n
] = BITMAP_ALLOC (&predbitmap_obstack
);
2049 bitmap_ior_into (graph
->implicit_preds
[n
],
2050 graph
->implicit_preds
[w
]);
2052 if (graph
->points_to
[w
])
2054 if (!graph
->points_to
[n
])
2055 graph
->points_to
[n
] = BITMAP_ALLOC (&predbitmap_obstack
);
2056 bitmap_ior_into (graph
->points_to
[n
],
2057 graph
->points_to
[w
]);
2060 bitmap_set_bit (si
->deleted
, n
);
2063 si
->scc_stack
.safe_push (n
);
2066 /* Label pointer equivalences. */
2069 label_visit (constraint_graph_t graph
, struct scc_info
*si
, unsigned int n
)
2071 unsigned int i
, first_pred
;
2074 bitmap_set_bit (si
->visited
, n
);
2076 /* Label and union our incoming edges's points to sets. */
2078 EXECUTE_IF_IN_NONNULL_BITMAP (graph
->preds
[n
], 0, i
, bi
)
2080 unsigned int w
= si
->node_mapping
[i
];
2081 if (!bitmap_bit_p (si
->visited
, w
))
2082 label_visit (graph
, si
, w
);
2084 /* Skip unused edges */
2085 if (w
== n
|| graph
->pointer_label
[w
] == 0)
2088 if (graph
->points_to
[w
])
2090 if (!graph
->points_to
[n
])
2092 if (first_pred
== -1U)
2096 graph
->points_to
[n
] = BITMAP_ALLOC (&predbitmap_obstack
);
2097 bitmap_ior (graph
->points_to
[n
],
2098 graph
->points_to
[first_pred
],
2099 graph
->points_to
[w
]);
2103 bitmap_ior_into (graph
->points_to
[n
], graph
->points_to
[w
]);
2107 /* Indirect nodes get fresh variables and a new pointer equiv class. */
2108 if (!bitmap_bit_p (graph
->direct_nodes
, n
))
2110 if (!graph
->points_to
[n
])
2112 graph
->points_to
[n
] = BITMAP_ALLOC (&predbitmap_obstack
);
2113 if (first_pred
!= -1U)
2114 bitmap_copy (graph
->points_to
[n
], graph
->points_to
[first_pred
]);
2116 bitmap_set_bit (graph
->points_to
[n
], FIRST_REF_NODE
+ n
);
2117 graph
->pointer_label
[n
] = pointer_equiv_class
++;
2118 equiv_class_label_t ecl
;
2119 ecl
= equiv_class_lookup_or_add (pointer_equiv_class_table
,
2120 graph
->points_to
[n
]);
2121 ecl
->equivalence_class
= graph
->pointer_label
[n
];
2125 /* If there was only a single non-empty predecessor the pointer equiv
2126 class is the same. */
2127 if (!graph
->points_to
[n
])
2129 if (first_pred
!= -1U)
2131 graph
->pointer_label
[n
] = graph
->pointer_label
[first_pred
];
2132 graph
->points_to
[n
] = graph
->points_to
[first_pred
];
2137 if (!bitmap_empty_p (graph
->points_to
[n
]))
2139 equiv_class_label_t ecl
;
2140 ecl
= equiv_class_lookup_or_add (pointer_equiv_class_table
,
2141 graph
->points_to
[n
]);
2142 if (ecl
->equivalence_class
== 0)
2143 ecl
->equivalence_class
= pointer_equiv_class
++;
2146 BITMAP_FREE (graph
->points_to
[n
]);
2147 graph
->points_to
[n
] = ecl
->labels
;
2149 graph
->pointer_label
[n
] = ecl
->equivalence_class
;
2153 /* Print the pred graph in dot format. */
2156 dump_pred_graph (struct scc_info
*si
, FILE *file
)
2160 /* Only print the graph if it has already been initialized: */
2164 /* Prints the header of the dot file: */
2165 fprintf (file
, "strict digraph {\n");
2166 fprintf (file
, " node [\n shape = box\n ]\n");
2167 fprintf (file
, " edge [\n fontsize = \"12\"\n ]\n");
2168 fprintf (file
, "\n // List of nodes and complex constraints in "
2169 "the constraint graph:\n");
2171 /* The next lines print the nodes in the graph together with the
2172 complex constraints attached to them. */
2173 for (i
= 1; i
< graph
->size
; i
++)
2175 if (i
== FIRST_REF_NODE
)
2177 if (si
->node_mapping
[i
] != i
)
2179 if (i
< FIRST_REF_NODE
)
2180 fprintf (file
, "\"%s\"", get_varinfo (i
)->name
);
2182 fprintf (file
, "\"*%s\"", get_varinfo (i
- FIRST_REF_NODE
)->name
);
2183 if (graph
->points_to
[i
]
2184 && !bitmap_empty_p (graph
->points_to
[i
]))
2186 fprintf (file
, "[label=\"%s = {", get_varinfo (i
)->name
);
2189 EXECUTE_IF_SET_IN_BITMAP (graph
->points_to
[i
], 0, j
, bi
)
2190 fprintf (file
, " %d", j
);
2191 fprintf (file
, " }\"]");
2193 fprintf (file
, ";\n");
2196 /* Go over the edges. */
2197 fprintf (file
, "\n // Edges in the constraint graph:\n");
2198 for (i
= 1; i
< graph
->size
; i
++)
2202 if (si
->node_mapping
[i
] != i
)
2204 EXECUTE_IF_IN_NONNULL_BITMAP (graph
->preds
[i
], 0, j
, bi
)
2206 unsigned from
= si
->node_mapping
[j
];
2207 if (from
< FIRST_REF_NODE
)
2208 fprintf (file
, "\"%s\"", get_varinfo (from
)->name
);
2210 fprintf (file
, "\"*%s\"", get_varinfo (from
- FIRST_REF_NODE
)->name
);
2211 fprintf (file
, " -> ");
2212 if (i
< FIRST_REF_NODE
)
2213 fprintf (file
, "\"%s\"", get_varinfo (i
)->name
);
2215 fprintf (file
, "\"*%s\"", get_varinfo (i
- FIRST_REF_NODE
)->name
);
2216 fprintf (file
, ";\n");
2220 /* Prints the tail of the dot file. */
2221 fprintf (file
, "}\n");
2224 /* Perform offline variable substitution, discovering equivalence
2225 classes, and eliminating non-pointer variables. */
2227 static struct scc_info
*
2228 perform_var_substitution (constraint_graph_t graph
)
2231 unsigned int size
= graph
->size
;
2232 struct scc_info
*si
= init_scc_info (size
);
2234 bitmap_obstack_initialize (&iteration_obstack
);
2235 pointer_equiv_class_table
.create (511);
2236 location_equiv_class_table
.create (511);
2237 pointer_equiv_class
= 1;
2238 location_equiv_class
= 1;
2240 /* Condense the nodes, which means to find SCC's, count incoming
2241 predecessors, and unite nodes in SCC's. */
2242 for (i
= 1; i
< FIRST_REF_NODE
; i
++)
2243 if (!bitmap_bit_p (si
->visited
, si
->node_mapping
[i
]))
2244 condense_visit (graph
, si
, si
->node_mapping
[i
]);
2246 if (dump_file
&& (dump_flags
& TDF_GRAPH
))
2248 fprintf (dump_file
, "\n\n// The constraint graph before var-substitution "
2249 "in dot format:\n");
2250 dump_pred_graph (si
, dump_file
);
2251 fprintf (dump_file
, "\n\n");
2254 bitmap_clear (si
->visited
);
2255 /* Actually the label the nodes for pointer equivalences */
2256 for (i
= 1; i
< FIRST_REF_NODE
; i
++)
2257 if (!bitmap_bit_p (si
->visited
, si
->node_mapping
[i
]))
2258 label_visit (graph
, si
, si
->node_mapping
[i
]);
2260 /* Calculate location equivalence labels. */
2261 for (i
= 1; i
< FIRST_REF_NODE
; i
++)
2267 if (!graph
->pointed_by
[i
])
2269 pointed_by
= BITMAP_ALLOC (&iteration_obstack
);
2271 /* Translate the pointed-by mapping for pointer equivalence
2273 EXECUTE_IF_SET_IN_BITMAP (graph
->pointed_by
[i
], 0, j
, bi
)
2275 bitmap_set_bit (pointed_by
,
2276 graph
->pointer_label
[si
->node_mapping
[j
]]);
2278 /* The original pointed_by is now dead. */
2279 BITMAP_FREE (graph
->pointed_by
[i
]);
2281 /* Look up the location equivalence label if one exists, or make
2283 equiv_class_label_t ecl
;
2284 ecl
= equiv_class_lookup_or_add (location_equiv_class_table
, pointed_by
);
2285 if (ecl
->equivalence_class
== 0)
2286 ecl
->equivalence_class
= location_equiv_class
++;
2289 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2290 fprintf (dump_file
, "Found location equivalence for node %s\n",
2291 get_varinfo (i
)->name
);
2292 BITMAP_FREE (pointed_by
);
2294 graph
->loc_label
[i
] = ecl
->equivalence_class
;
2298 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2299 for (i
= 1; i
< FIRST_REF_NODE
; i
++)
2301 unsigned j
= si
->node_mapping
[i
];
2304 fprintf (dump_file
, "%s node id %d ",
2305 bitmap_bit_p (graph
->direct_nodes
, i
)
2306 ? "Direct" : "Indirect", i
);
2307 if (i
< FIRST_REF_NODE
)
2308 fprintf (dump_file
, "\"%s\"", get_varinfo (i
)->name
);
2310 fprintf (dump_file
, "\"*%s\"",
2311 get_varinfo (i
- FIRST_REF_NODE
)->name
);
2312 fprintf (dump_file
, " mapped to SCC leader node id %d ", j
);
2313 if (j
< FIRST_REF_NODE
)
2314 fprintf (dump_file
, "\"%s\"\n", get_varinfo (j
)->name
);
2316 fprintf (dump_file
, "\"*%s\"\n",
2317 get_varinfo (j
- FIRST_REF_NODE
)->name
);
2322 "Equivalence classes for %s node id %d ",
2323 bitmap_bit_p (graph
->direct_nodes
, i
)
2324 ? "direct" : "indirect", i
);
2325 if (i
< FIRST_REF_NODE
)
2326 fprintf (dump_file
, "\"%s\"", get_varinfo (i
)->name
);
2328 fprintf (dump_file
, "\"*%s\"",
2329 get_varinfo (i
- FIRST_REF_NODE
)->name
);
2331 ": pointer %d, location %d\n",
2332 graph
->pointer_label
[i
], graph
->loc_label
[i
]);
2336 /* Quickly eliminate our non-pointer variables. */
2338 for (i
= 1; i
< FIRST_REF_NODE
; i
++)
2340 unsigned int node
= si
->node_mapping
[i
];
2342 if (graph
->pointer_label
[node
] == 0)
2344 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2346 "%s is a non-pointer variable, eliminating edges.\n",
2347 get_varinfo (node
)->name
);
2348 stats
.nonpointer_vars
++;
2349 clear_edges_for_node (graph
, node
);
2356 /* Free information that was only necessary for variable
2360 free_var_substitution_info (struct scc_info
*si
)
2363 free (graph
->pointer_label
);
2364 free (graph
->loc_label
);
2365 free (graph
->pointed_by
);
2366 free (graph
->points_to
);
2367 free (graph
->eq_rep
);
2368 sbitmap_free (graph
->direct_nodes
);
2369 pointer_equiv_class_table
.dispose ();
2370 location_equiv_class_table
.dispose ();
2371 bitmap_obstack_release (&iteration_obstack
);
2374 /* Return an existing node that is equivalent to NODE, which has
2375 equivalence class LABEL, if one exists. Return NODE otherwise. */
2378 find_equivalent_node (constraint_graph_t graph
,
2379 unsigned int node
, unsigned int label
)
2381 /* If the address version of this variable is unused, we can
2382 substitute it for anything else with the same label.
2383 Otherwise, we know the pointers are equivalent, but not the
2384 locations, and we can unite them later. */
2386 if (!bitmap_bit_p (graph
->address_taken
, node
))
2388 gcc_checking_assert (label
< graph
->size
);
2390 if (graph
->eq_rep
[label
] != -1)
2392 /* Unify the two variables since we know they are equivalent. */
2393 if (unite (graph
->eq_rep
[label
], node
))
2394 unify_nodes (graph
, graph
->eq_rep
[label
], node
, false);
2395 return graph
->eq_rep
[label
];
2399 graph
->eq_rep
[label
] = node
;
2400 graph
->pe_rep
[label
] = node
;
2405 gcc_checking_assert (label
< graph
->size
);
2406 graph
->pe
[node
] = label
;
2407 if (graph
->pe_rep
[label
] == -1)
2408 graph
->pe_rep
[label
] = node
;
2414 /* Unite pointer equivalent but not location equivalent nodes in
2415 GRAPH. This may only be performed once variable substitution is
2419 unite_pointer_equivalences (constraint_graph_t graph
)
2423 /* Go through the pointer equivalences and unite them to their
2424 representative, if they aren't already. */
2425 for (i
= 1; i
< FIRST_REF_NODE
; i
++)
2427 unsigned int label
= graph
->pe
[i
];
2430 int label_rep
= graph
->pe_rep
[label
];
2432 if (label_rep
== -1)
2435 label_rep
= find (label_rep
);
2436 if (label_rep
>= 0 && unite (label_rep
, find (i
)))
2437 unify_nodes (graph
, label_rep
, i
, false);
2442 /* Move complex constraints to the GRAPH nodes they belong to. */
2445 move_complex_constraints (constraint_graph_t graph
)
2450 FOR_EACH_VEC_ELT (constraints
, i
, c
)
2454 struct constraint_expr lhs
= c
->lhs
;
2455 struct constraint_expr rhs
= c
->rhs
;
2457 if (lhs
.type
== DEREF
)
2459 insert_into_complex (graph
, lhs
.var
, c
);
2461 else if (rhs
.type
== DEREF
)
2463 if (!(get_varinfo (lhs
.var
)->is_special_var
))
2464 insert_into_complex (graph
, rhs
.var
, c
);
2466 else if (rhs
.type
!= ADDRESSOF
&& lhs
.var
> anything_id
2467 && (lhs
.offset
!= 0 || rhs
.offset
!= 0))
2469 insert_into_complex (graph
, rhs
.var
, c
);
2476 /* Optimize and rewrite complex constraints while performing
2477 collapsing of equivalent nodes. SI is the SCC_INFO that is the
2478 result of perform_variable_substitution. */
2481 rewrite_constraints (constraint_graph_t graph
,
2482 struct scc_info
*si
)
2487 #ifdef ENABLE_CHECKING
2488 for (unsigned int j
= 0; j
< graph
->size
; j
++)
2489 gcc_assert (find (j
) == j
);
2492 FOR_EACH_VEC_ELT (constraints
, i
, c
)
2494 struct constraint_expr lhs
= c
->lhs
;
2495 struct constraint_expr rhs
= c
->rhs
;
2496 unsigned int lhsvar
= find (lhs
.var
);
2497 unsigned int rhsvar
= find (rhs
.var
);
2498 unsigned int lhsnode
, rhsnode
;
2499 unsigned int lhslabel
, rhslabel
;
2501 lhsnode
= si
->node_mapping
[lhsvar
];
2502 rhsnode
= si
->node_mapping
[rhsvar
];
2503 lhslabel
= graph
->pointer_label
[lhsnode
];
2504 rhslabel
= graph
->pointer_label
[rhsnode
];
2506 /* See if it is really a non-pointer variable, and if so, ignore
2510 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2513 fprintf (dump_file
, "%s is a non-pointer variable,"
2514 "ignoring constraint:",
2515 get_varinfo (lhs
.var
)->name
);
2516 dump_constraint (dump_file
, c
);
2517 fprintf (dump_file
, "\n");
2519 constraints
[i
] = NULL
;
2525 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2528 fprintf (dump_file
, "%s is a non-pointer variable,"
2529 "ignoring constraint:",
2530 get_varinfo (rhs
.var
)->name
);
2531 dump_constraint (dump_file
, c
);
2532 fprintf (dump_file
, "\n");
2534 constraints
[i
] = NULL
;
2538 lhsvar
= find_equivalent_node (graph
, lhsvar
, lhslabel
);
2539 rhsvar
= find_equivalent_node (graph
, rhsvar
, rhslabel
);
2540 c
->lhs
.var
= lhsvar
;
2541 c
->rhs
.var
= rhsvar
;
2545 /* Eliminate indirect cycles involving NODE. Return true if NODE was
2546 part of an SCC, false otherwise. */
2549 eliminate_indirect_cycles (unsigned int node
)
2551 if (graph
->indirect_cycles
[node
] != -1
2552 && !bitmap_empty_p (get_varinfo (node
)->solution
))
2555 vec
<unsigned> queue
= vNULL
;
2557 unsigned int to
= find (graph
->indirect_cycles
[node
]);
2560 /* We can't touch the solution set and call unify_nodes
2561 at the same time, because unify_nodes is going to do
2562 bitmap unions into it. */
2564 EXECUTE_IF_SET_IN_BITMAP (get_varinfo (node
)->solution
, 0, i
, bi
)
2566 if (find (i
) == i
&& i
!= to
)
2569 queue
.safe_push (i
);
2574 queue
.iterate (queuepos
, &i
);
2577 unify_nodes (graph
, to
, i
, true);
2585 /* Solve the constraint graph GRAPH using our worklist solver.
2586 This is based on the PW* family of solvers from the "Efficient Field
2587 Sensitive Pointer Analysis for C" paper.
2588 It works by iterating over all the graph nodes, processing the complex
2589 constraints and propagating the copy constraints, until everything stops
2590 changed. This corresponds to steps 6-8 in the solving list given above. */
2593 solve_graph (constraint_graph_t graph
)
2595 unsigned int size
= graph
->size
;
2599 changed
= BITMAP_ALLOC (NULL
);
2601 /* Mark all initial non-collapsed nodes as changed. */
2602 for (i
= 1; i
< size
; i
++)
2604 varinfo_t ivi
= get_varinfo (i
);
2605 if (find (i
) == i
&& !bitmap_empty_p (ivi
->solution
)
2606 && ((graph
->succs
[i
] && !bitmap_empty_p (graph
->succs
[i
]))
2607 || graph
->complex[i
].length () > 0))
2608 bitmap_set_bit (changed
, i
);
2611 /* Allocate a bitmap to be used to store the changed bits. */
2612 pts
= BITMAP_ALLOC (&pta_obstack
);
2614 while (!bitmap_empty_p (changed
))
2617 struct topo_info
*ti
= init_topo_info ();
2620 bitmap_obstack_initialize (&iteration_obstack
);
2622 compute_topo_order (graph
, ti
);
2624 while (ti
->topo_order
.length () != 0)
2627 i
= ti
->topo_order
.pop ();
2629 /* If this variable is not a representative, skip it. */
2633 /* In certain indirect cycle cases, we may merge this
2634 variable to another. */
2635 if (eliminate_indirect_cycles (i
) && find (i
) != i
)
2638 /* If the node has changed, we need to process the
2639 complex constraints and outgoing edges again. */
2640 if (bitmap_clear_bit (changed
, i
))
2645 vec
<constraint_t
> complex = graph
->complex[i
];
2646 varinfo_t vi
= get_varinfo (i
);
2647 bool solution_empty
;
2649 /* Compute the changed set of solution bits. If anything
2650 is in the solution just propagate that. */
2651 if (bitmap_bit_p (vi
->solution
, anything_id
))
2653 /* If anything is also in the old solution there is
2655 ??? But we shouldn't ended up with "changed" set ... */
2657 && bitmap_bit_p (vi
->oldsolution
, anything_id
))
2659 bitmap_copy (pts
, get_varinfo (find (anything_id
))->solution
);
2661 else if (vi
->oldsolution
)
2662 bitmap_and_compl (pts
, vi
->solution
, vi
->oldsolution
);
2664 bitmap_copy (pts
, vi
->solution
);
2666 if (bitmap_empty_p (pts
))
2669 if (vi
->oldsolution
)
2670 bitmap_ior_into (vi
->oldsolution
, pts
);
2673 vi
->oldsolution
= BITMAP_ALLOC (&oldpta_obstack
);
2674 bitmap_copy (vi
->oldsolution
, pts
);
2677 solution
= vi
->solution
;
2678 solution_empty
= bitmap_empty_p (solution
);
2680 /* Process the complex constraints */
2681 FOR_EACH_VEC_ELT (complex, j
, c
)
2683 /* XXX: This is going to unsort the constraints in
2684 some cases, which will occasionally add duplicate
2685 constraints during unification. This does not
2686 affect correctness. */
2687 c
->lhs
.var
= find (c
->lhs
.var
);
2688 c
->rhs
.var
= find (c
->rhs
.var
);
2690 /* The only complex constraint that can change our
2691 solution to non-empty, given an empty solution,
2692 is a constraint where the lhs side is receiving
2693 some set from elsewhere. */
2694 if (!solution_empty
|| c
->lhs
.type
!= DEREF
)
2695 do_complex_constraint (graph
, c
, pts
);
2698 solution_empty
= bitmap_empty_p (solution
);
2700 if (!solution_empty
)
2703 unsigned eff_escaped_id
= find (escaped_id
);
2705 /* Propagate solution to all successors. */
2706 EXECUTE_IF_IN_NONNULL_BITMAP (graph
->succs
[i
],
2712 unsigned int to
= find (j
);
2713 tmp
= get_varinfo (to
)->solution
;
2716 /* Don't try to propagate to ourselves. */
2720 /* If we propagate from ESCAPED use ESCAPED as
2722 if (i
== eff_escaped_id
)
2723 flag
= bitmap_set_bit (tmp
, escaped_id
);
2725 flag
= bitmap_ior_into (tmp
, pts
);
2728 bitmap_set_bit (changed
, to
);
2733 free_topo_info (ti
);
2734 bitmap_obstack_release (&iteration_obstack
);
2738 BITMAP_FREE (changed
);
2739 bitmap_obstack_release (&oldpta_obstack
);
2742 /* Map from trees to variable infos. */
2743 static struct pointer_map_t
*vi_for_tree
;
2746 /* Insert ID as the variable id for tree T in the vi_for_tree map. */
2749 insert_vi_for_tree (tree t
, varinfo_t vi
)
2751 void **slot
= pointer_map_insert (vi_for_tree
, t
);
2753 gcc_assert (*slot
== NULL
);
2757 /* Find the variable info for tree T in VI_FOR_TREE. If T does not
2758 exist in the map, return NULL, otherwise, return the varinfo we found. */
2761 lookup_vi_for_tree (tree t
)
2763 void **slot
= pointer_map_contains (vi_for_tree
, t
);
2767 return (varinfo_t
) *slot
;
2770 /* Return a printable name for DECL */
2773 alias_get_name (tree decl
)
2775 const char *res
= NULL
;
2777 int num_printed
= 0;
2782 if (TREE_CODE (decl
) == SSA_NAME
)
2784 res
= get_name (decl
);
2786 num_printed
= asprintf (&temp
, "%s_%u", res
, SSA_NAME_VERSION (decl
));
2788 num_printed
= asprintf (&temp
, "_%u", SSA_NAME_VERSION (decl
));
2789 if (num_printed
> 0)
2791 res
= ggc_strdup (temp
);
2795 else if (DECL_P (decl
))
2797 if (DECL_ASSEMBLER_NAME_SET_P (decl
))
2798 res
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
2801 res
= get_name (decl
);
2804 num_printed
= asprintf (&temp
, "D.%u", DECL_UID (decl
));
2805 if (num_printed
> 0)
2807 res
= ggc_strdup (temp
);
2819 /* Find the variable id for tree T in the map.
2820 If T doesn't exist in the map, create an entry for it and return it. */
2823 get_vi_for_tree (tree t
)
2825 void **slot
= pointer_map_contains (vi_for_tree
, t
);
2827 return get_varinfo (create_variable_info_for (t
, alias_get_name (t
)));
2829 return (varinfo_t
) *slot
;
2832 /* Get a scalar constraint expression for a new temporary variable. */
2834 static struct constraint_expr
2835 new_scalar_tmp_constraint_exp (const char *name
)
2837 struct constraint_expr tmp
;
2840 vi
= new_var_info (NULL_TREE
, name
);
2844 vi
->is_full_var
= 1;
2853 /* Get a constraint expression vector from an SSA_VAR_P node.
2854 If address_p is true, the result will be taken its address of. */
2857 get_constraint_for_ssa_var (tree t
, vec
<ce_s
> *results
, bool address_p
)
2859 struct constraint_expr cexpr
;
2862 /* We allow FUNCTION_DECLs here even though it doesn't make much sense. */
2863 gcc_assert (TREE_CODE (t
) == SSA_NAME
|| DECL_P (t
));
2865 /* For parameters, get at the points-to set for the actual parm
2867 if (TREE_CODE (t
) == SSA_NAME
2868 && SSA_NAME_IS_DEFAULT_DEF (t
)
2869 && (TREE_CODE (SSA_NAME_VAR (t
)) == PARM_DECL
2870 || TREE_CODE (SSA_NAME_VAR (t
)) == RESULT_DECL
))
2872 get_constraint_for_ssa_var (SSA_NAME_VAR (t
), results
, address_p
);
2876 /* For global variables resort to the alias target. */
2877 if (TREE_CODE (t
) == VAR_DECL
2878 && (TREE_STATIC (t
) || DECL_EXTERNAL (t
)))
2880 struct varpool_node
*node
= varpool_get_node (t
);
2881 if (node
&& node
->alias
&& node
->analyzed
)
2883 node
= varpool_variable_node (node
, NULL
);
2888 vi
= get_vi_for_tree (t
);
2890 cexpr
.type
= SCALAR
;
2892 /* If we determine the result is "anything", and we know this is readonly,
2893 say it points to readonly memory instead. */
2894 if (cexpr
.var
== anything_id
&& TREE_READONLY (t
))
2897 cexpr
.type
= ADDRESSOF
;
2898 cexpr
.var
= readonly_id
;
2901 /* If we are not taking the address of the constraint expr, add all
2902 sub-fiels of the variable as well. */
2904 && !vi
->is_full_var
)
2906 for (; vi
; vi
= vi_next (vi
))
2909 results
->safe_push (cexpr
);
2914 results
->safe_push (cexpr
);
2917 /* Process constraint T, performing various simplifications and then
2918 adding it to our list of overall constraints. */
2921 process_constraint (constraint_t t
)
2923 struct constraint_expr rhs
= t
->rhs
;
2924 struct constraint_expr lhs
= t
->lhs
;
2926 gcc_assert (rhs
.var
< varmap
.length ());
2927 gcc_assert (lhs
.var
< varmap
.length ());
2929 /* If we didn't get any useful constraint from the lhs we get
2930 &ANYTHING as fallback from get_constraint_for. Deal with
2931 it here by turning it into *ANYTHING. */
2932 if (lhs
.type
== ADDRESSOF
2933 && lhs
.var
== anything_id
)
2936 /* ADDRESSOF on the lhs is invalid. */
2937 gcc_assert (lhs
.type
!= ADDRESSOF
);
2939 /* We shouldn't add constraints from things that cannot have pointers.
2940 It's not completely trivial to avoid in the callers, so do it here. */
2941 if (rhs
.type
!= ADDRESSOF
2942 && !get_varinfo (rhs
.var
)->may_have_pointers
)
2945 /* Likewise adding to the solution of a non-pointer var isn't useful. */
2946 if (!get_varinfo (lhs
.var
)->may_have_pointers
)
2949 /* This can happen in our IR with things like n->a = *p */
2950 if (rhs
.type
== DEREF
&& lhs
.type
== DEREF
&& rhs
.var
!= anything_id
)
2952 /* Split into tmp = *rhs, *lhs = tmp */
2953 struct constraint_expr tmplhs
;
2954 tmplhs
= new_scalar_tmp_constraint_exp ("doubledereftmp");
2955 process_constraint (new_constraint (tmplhs
, rhs
));
2956 process_constraint (new_constraint (lhs
, tmplhs
));
2958 else if (rhs
.type
== ADDRESSOF
&& lhs
.type
== DEREF
)
2960 /* Split into tmp = &rhs, *lhs = tmp */
2961 struct constraint_expr tmplhs
;
2962 tmplhs
= new_scalar_tmp_constraint_exp ("derefaddrtmp");
2963 process_constraint (new_constraint (tmplhs
, rhs
));
2964 process_constraint (new_constraint (lhs
, tmplhs
));
2968 gcc_assert (rhs
.type
!= ADDRESSOF
|| rhs
.offset
== 0);
2969 constraints
.safe_push (t
);
2974 /* Return the position, in bits, of FIELD_DECL from the beginning of its
2977 static HOST_WIDE_INT
2978 bitpos_of_field (const tree fdecl
)
2980 if (!host_integerp (DECL_FIELD_OFFSET (fdecl
), 0)
2981 || !host_integerp (DECL_FIELD_BIT_OFFSET (fdecl
), 0))
2984 return (TREE_INT_CST_LOW (DECL_FIELD_OFFSET (fdecl
)) * BITS_PER_UNIT
2985 + TREE_INT_CST_LOW (DECL_FIELD_BIT_OFFSET (fdecl
)));
2989 /* Get constraint expressions for offsetting PTR by OFFSET. Stores the
2990 resulting constraint expressions in *RESULTS. */
2993 get_constraint_for_ptr_offset (tree ptr
, tree offset
,
2996 struct constraint_expr c
;
2998 HOST_WIDE_INT rhsoffset
;
3000 /* If we do not do field-sensitive PTA adding offsets to pointers
3001 does not change the points-to solution. */
3002 if (!use_field_sensitive
)
3004 get_constraint_for_rhs (ptr
, results
);
3008 /* If the offset is not a non-negative integer constant that fits
3009 in a HOST_WIDE_INT, we have to fall back to a conservative
3010 solution which includes all sub-fields of all pointed-to
3011 variables of ptr. */
3012 if (offset
== NULL_TREE
3013 || TREE_CODE (offset
) != INTEGER_CST
)
3014 rhsoffset
= UNKNOWN_OFFSET
;
3017 /* Sign-extend the offset. */
3018 double_int soffset
= tree_to_double_int (offset
)
3019 .sext (TYPE_PRECISION (TREE_TYPE (offset
)));
3020 if (!soffset
.fits_shwi ())
3021 rhsoffset
= UNKNOWN_OFFSET
;
3024 /* Make sure the bit-offset also fits. */
3025 HOST_WIDE_INT rhsunitoffset
= soffset
.low
;
3026 rhsoffset
= rhsunitoffset
* BITS_PER_UNIT
;
3027 if (rhsunitoffset
!= rhsoffset
/ BITS_PER_UNIT
)
3028 rhsoffset
= UNKNOWN_OFFSET
;
3032 get_constraint_for_rhs (ptr
, results
);
3036 /* As we are eventually appending to the solution do not use
3037 vec::iterate here. */
3038 n
= results
->length ();
3039 for (j
= 0; j
< n
; j
++)
3043 curr
= get_varinfo (c
.var
);
3045 if (c
.type
== ADDRESSOF
3046 /* If this varinfo represents a full variable just use it. */
3047 && curr
->is_full_var
)
3049 else if (c
.type
== ADDRESSOF
3050 /* If we do not know the offset add all subfields. */
3051 && rhsoffset
== UNKNOWN_OFFSET
)
3053 varinfo_t temp
= get_varinfo (curr
->head
);
3056 struct constraint_expr c2
;
3058 c2
.type
= ADDRESSOF
;
3060 if (c2
.var
!= c
.var
)
3061 results
->safe_push (c2
);
3062 temp
= vi_next (temp
);
3066 else if (c
.type
== ADDRESSOF
)
3069 unsigned HOST_WIDE_INT offset
= curr
->offset
+ rhsoffset
;
3071 /* Search the sub-field which overlaps with the
3072 pointed-to offset. If the result is outside of the variable
3073 we have to provide a conservative result, as the variable is
3074 still reachable from the resulting pointer (even though it
3075 technically cannot point to anything). The last and first
3076 sub-fields are such conservative results.
3077 ??? If we always had a sub-field for &object + 1 then
3078 we could represent this in a more precise way. */
3080 && curr
->offset
< offset
)
3082 temp
= first_or_preceding_vi_for_offset (curr
, offset
);
3084 /* If the found variable is not exactly at the pointed to
3085 result, we have to include the next variable in the
3086 solution as well. Otherwise two increments by offset / 2
3087 do not result in the same or a conservative superset
3089 if (temp
->offset
!= offset
3092 struct constraint_expr c2
;
3093 c2
.var
= temp
->next
;
3094 c2
.type
= ADDRESSOF
;
3096 results
->safe_push (c2
);
3102 c
.offset
= rhsoffset
;
3109 /* Given a COMPONENT_REF T, return the constraint_expr vector for it.
3110 If address_p is true the result will be taken its address of.
3111 If lhs_p is true then the constraint expression is assumed to be used
3115 get_constraint_for_component_ref (tree t
, vec
<ce_s
> *results
,
3116 bool address_p
, bool lhs_p
)
3119 HOST_WIDE_INT bitsize
= -1;
3120 HOST_WIDE_INT bitmaxsize
= -1;
3121 HOST_WIDE_INT bitpos
;
3124 /* Some people like to do cute things like take the address of
3127 while (handled_component_p (forzero
)
3128 || INDIRECT_REF_P (forzero
)
3129 || TREE_CODE (forzero
) == MEM_REF
)
3130 forzero
= TREE_OPERAND (forzero
, 0);
3132 if (CONSTANT_CLASS_P (forzero
) && integer_zerop (forzero
))
3134 struct constraint_expr temp
;
3137 temp
.var
= integer_id
;
3139 results
->safe_push (temp
);
3143 /* Handle type-punning through unions. If we are extracting a pointer
3144 from a union via a possibly type-punning access that pointer
3145 points to anything, similar to a conversion of an integer to
3151 TREE_CODE (u
) == COMPONENT_REF
|| TREE_CODE (u
) == ARRAY_REF
;
3152 u
= TREE_OPERAND (u
, 0))
3153 if (TREE_CODE (u
) == COMPONENT_REF
3154 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u
, 0))) == UNION_TYPE
)
3156 struct constraint_expr temp
;
3159 temp
.var
= anything_id
;
3160 temp
.type
= ADDRESSOF
;
3161 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 (host_integerp (TYPE_SIZE (TREE_TYPE (t
)), 1))
3416 size
= TREE_INT_CST_LOW (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
);
3459 vec
<ce_s
> tmp
= vNULL
;
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
);
3470 /* We do not know whether the constructor was complete,
3471 so technically we have to add &NOTHING or &ANYTHING
3472 like we do for an empty constructor as well. */
3479 case tcc_declaration
:
3481 get_constraint_for_ssa_var (t
, results
, address_p
);
3486 /* We cannot refer to automatic variables through constants. */
3487 temp
.type
= ADDRESSOF
;
3488 temp
.var
= nonlocal_id
;
3490 results
->safe_push (temp
);
3496 /* The default fallback is a constraint from anything. */
3497 temp
.type
= ADDRESSOF
;
3498 temp
.var
= anything_id
;
3500 results
->safe_push (temp
);
3503 /* Given a gimple tree T, return the constraint expression vector for it. */
3506 get_constraint_for (tree t
, vec
<ce_s
> *results
)
3508 gcc_assert (results
->length () == 0);
3510 get_constraint_for_1 (t
, results
, false, true);
3513 /* Given a gimple tree T, return the constraint expression vector for it
3514 to be used as the rhs of a constraint. */
3517 get_constraint_for_rhs (tree t
, vec
<ce_s
> *results
)
3519 gcc_assert (results
->length () == 0);
3521 get_constraint_for_1 (t
, results
, false, false);
3525 /* Efficiently generates constraints from all entries in *RHSC to all
3526 entries in *LHSC. */
3529 process_all_all_constraints (vec
<ce_s
> lhsc
,
3532 struct constraint_expr
*lhsp
, *rhsp
;
3535 if (lhsc
.length () <= 1 || rhsc
.length () <= 1)
3537 FOR_EACH_VEC_ELT (lhsc
, i
, lhsp
)
3538 FOR_EACH_VEC_ELT (rhsc
, j
, rhsp
)
3539 process_constraint (new_constraint (*lhsp
, *rhsp
));
3543 struct constraint_expr tmp
;
3544 tmp
= new_scalar_tmp_constraint_exp ("allalltmp");
3545 FOR_EACH_VEC_ELT (rhsc
, i
, rhsp
)
3546 process_constraint (new_constraint (tmp
, *rhsp
));
3547 FOR_EACH_VEC_ELT (lhsc
, i
, lhsp
)
3548 process_constraint (new_constraint (*lhsp
, tmp
));
3552 /* Handle aggregate copies by expanding into copies of the respective
3553 fields of the structures. */
3556 do_structure_copy (tree lhsop
, tree rhsop
)
3558 struct constraint_expr
*lhsp
, *rhsp
;
3559 vec
<ce_s
> lhsc
= vNULL
;
3560 vec
<ce_s
> rhsc
= vNULL
;
3563 get_constraint_for (lhsop
, &lhsc
);
3564 get_constraint_for_rhs (rhsop
, &rhsc
);
3567 if (lhsp
->type
== DEREF
3568 || (lhsp
->type
== ADDRESSOF
&& lhsp
->var
== anything_id
)
3569 || rhsp
->type
== DEREF
)
3571 if (lhsp
->type
== DEREF
)
3573 gcc_assert (lhsc
.length () == 1);
3574 lhsp
->offset
= UNKNOWN_OFFSET
;
3576 if (rhsp
->type
== DEREF
)
3578 gcc_assert (rhsc
.length () == 1);
3579 rhsp
->offset
= UNKNOWN_OFFSET
;
3581 process_all_all_constraints (lhsc
, rhsc
);
3583 else if (lhsp
->type
== SCALAR
3584 && (rhsp
->type
== SCALAR
3585 || rhsp
->type
== ADDRESSOF
))
3587 HOST_WIDE_INT lhssize
, lhsmaxsize
, lhsoffset
;
3588 HOST_WIDE_INT rhssize
, rhsmaxsize
, rhsoffset
;
3590 get_ref_base_and_extent (lhsop
, &lhsoffset
, &lhssize
, &lhsmaxsize
);
3591 get_ref_base_and_extent (rhsop
, &rhsoffset
, &rhssize
, &rhsmaxsize
);
3592 for (j
= 0; lhsc
.iterate (j
, &lhsp
);)
3594 varinfo_t lhsv
, rhsv
;
3596 lhsv
= get_varinfo (lhsp
->var
);
3597 rhsv
= get_varinfo (rhsp
->var
);
3598 if (lhsv
->may_have_pointers
3599 && (lhsv
->is_full_var
3600 || rhsv
->is_full_var
3601 || ranges_overlap_p (lhsv
->offset
+ rhsoffset
, lhsv
->size
,
3602 rhsv
->offset
+ lhsoffset
, rhsv
->size
)))
3603 process_constraint (new_constraint (*lhsp
, *rhsp
));
3604 if (!rhsv
->is_full_var
3605 && (lhsv
->is_full_var
3606 || (lhsv
->offset
+ rhsoffset
+ lhsv
->size
3607 > rhsv
->offset
+ lhsoffset
+ rhsv
->size
)))
3610 if (k
>= rhsc
.length ())
3624 /* Create constraints ID = { rhsc }. */
3627 make_constraints_to (unsigned id
, vec
<ce_s
> rhsc
)
3629 struct constraint_expr
*c
;
3630 struct constraint_expr includes
;
3634 includes
.offset
= 0;
3635 includes
.type
= SCALAR
;
3637 FOR_EACH_VEC_ELT (rhsc
, j
, c
)
3638 process_constraint (new_constraint (includes
, *c
));
3641 /* Create a constraint ID = OP. */
3644 make_constraint_to (unsigned id
, tree op
)
3646 vec
<ce_s
> rhsc
= vNULL
;
3647 get_constraint_for_rhs (op
, &rhsc
);
3648 make_constraints_to (id
, rhsc
);
3652 /* Create a constraint ID = &FROM. */
3655 make_constraint_from (varinfo_t vi
, int from
)
3657 struct constraint_expr lhs
, rhs
;
3665 rhs
.type
= ADDRESSOF
;
3666 process_constraint (new_constraint (lhs
, rhs
));
3669 /* Create a constraint ID = FROM. */
3672 make_copy_constraint (varinfo_t vi
, int from
)
3674 struct constraint_expr lhs
, rhs
;
3683 process_constraint (new_constraint (lhs
, rhs
));
3686 /* Make constraints necessary to make OP escape. */
3689 make_escape_constraint (tree op
)
3691 make_constraint_to (escaped_id
, op
);
3694 /* Add constraints to that the solution of VI is transitively closed. */
3697 make_transitive_closure_constraints (varinfo_t vi
)
3699 struct constraint_expr lhs
, rhs
;
3708 process_constraint (new_constraint (lhs
, rhs
));
3710 /* VAR = VAR + UNKNOWN; */
3716 rhs
.offset
= UNKNOWN_OFFSET
;
3717 process_constraint (new_constraint (lhs
, rhs
));
3720 /* Temporary storage for fake var decls. */
3721 struct obstack fake_var_decl_obstack
;
3723 /* Build a fake VAR_DECL acting as referrer to a DECL_UID. */
3726 build_fake_var_decl (tree type
)
3728 tree decl
= (tree
) XOBNEW (&fake_var_decl_obstack
, struct tree_var_decl
);
3729 memset (decl
, 0, sizeof (struct tree_var_decl
));
3730 TREE_SET_CODE (decl
, VAR_DECL
);
3731 TREE_TYPE (decl
) = type
;
3732 DECL_UID (decl
) = allocate_decl_uid ();
3733 SET_DECL_PT_UID (decl
, -1);
3734 layout_decl (decl
, 0);
3738 /* Create a new artificial heap variable with NAME.
3739 Return the created variable. */
3742 make_heapvar (const char *name
)
3747 heapvar
= build_fake_var_decl (ptr_type_node
);
3748 DECL_EXTERNAL (heapvar
) = 1;
3750 vi
= new_var_info (heapvar
, name
);
3751 vi
->is_artificial_var
= true;
3752 vi
->is_heap_var
= true;
3753 vi
->is_unknown_size_var
= true;
3757 vi
->is_full_var
= true;
3758 insert_vi_for_tree (heapvar
, vi
);
3763 /* Create a new artificial heap variable with NAME and make a
3764 constraint from it to LHS. Set flags according to a tag used
3765 for tracking restrict pointers. */
3768 make_constraint_from_restrict (varinfo_t lhs
, const char *name
)
3770 varinfo_t vi
= make_heapvar (name
);
3771 vi
->is_global_var
= 1;
3772 vi
->may_have_pointers
= 1;
3773 make_constraint_from (lhs
, vi
->id
);
3777 /* Create a new artificial heap variable with NAME and make a
3778 constraint from it to LHS. Set flags according to a tag used
3779 for tracking restrict pointers and make the artificial heap
3780 point to global memory. */
3783 make_constraint_from_global_restrict (varinfo_t lhs
, const char *name
)
3785 varinfo_t vi
= make_constraint_from_restrict (lhs
, name
);
3786 make_copy_constraint (vi
, nonlocal_id
);
3790 /* In IPA mode there are varinfos for different aspects of reach
3791 function designator. One for the points-to set of the return
3792 value, one for the variables that are clobbered by the function,
3793 one for its uses and one for each parameter (including a single
3794 glob for remaining variadic arguments). */
3796 enum { fi_clobbers
= 1, fi_uses
= 2,
3797 fi_static_chain
= 3, fi_result
= 4, fi_parm_base
= 5 };
3799 /* Get a constraint for the requested part of a function designator FI
3800 when operating in IPA mode. */
3802 static struct constraint_expr
3803 get_function_part_constraint (varinfo_t fi
, unsigned part
)
3805 struct constraint_expr c
;
3807 gcc_assert (in_ipa_mode
);
3809 if (fi
->id
== anything_id
)
3811 /* ??? We probably should have a ANYFN special variable. */
3812 c
.var
= anything_id
;
3816 else if (TREE_CODE (fi
->decl
) == FUNCTION_DECL
)
3818 varinfo_t ai
= first_vi_for_offset (fi
, part
);
3822 c
.var
= anything_id
;
3836 /* For non-IPA mode, generate constraints necessary for a call on the
3840 handle_rhs_call (gimple stmt
, vec
<ce_s
> *results
)
3842 struct constraint_expr rhsc
;
3844 bool returns_uses
= false;
3846 for (i
= 0; i
< gimple_call_num_args (stmt
); ++i
)
3848 tree arg
= gimple_call_arg (stmt
, i
);
3849 int flags
= gimple_call_arg_flags (stmt
, i
);
3851 /* If the argument is not used we can ignore it. */
3852 if (flags
& EAF_UNUSED
)
3855 /* As we compute ESCAPED context-insensitive we do not gain
3856 any precision with just EAF_NOCLOBBER but not EAF_NOESCAPE
3857 set. The argument would still get clobbered through the
3859 if ((flags
& EAF_NOCLOBBER
)
3860 && (flags
& EAF_NOESCAPE
))
3862 varinfo_t uses
= get_call_use_vi (stmt
);
3863 if (!(flags
& EAF_DIRECT
))
3865 varinfo_t tem
= new_var_info (NULL_TREE
, "callarg");
3866 make_constraint_to (tem
->id
, arg
);
3867 make_transitive_closure_constraints (tem
);
3868 make_copy_constraint (uses
, tem
->id
);
3871 make_constraint_to (uses
->id
, arg
);
3872 returns_uses
= true;
3874 else if (flags
& EAF_NOESCAPE
)
3876 struct constraint_expr lhs
, rhs
;
3877 varinfo_t uses
= get_call_use_vi (stmt
);
3878 varinfo_t clobbers
= get_call_clobber_vi (stmt
);
3879 varinfo_t tem
= new_var_info (NULL_TREE
, "callarg");
3880 make_constraint_to (tem
->id
, arg
);
3881 if (!(flags
& EAF_DIRECT
))
3882 make_transitive_closure_constraints (tem
);
3883 make_copy_constraint (uses
, tem
->id
);
3884 make_copy_constraint (clobbers
, tem
->id
);
3885 /* Add *tem = nonlocal, do not add *tem = callused as
3886 EAF_NOESCAPE parameters do not escape to other parameters
3887 and all other uses appear in NONLOCAL as well. */
3892 rhs
.var
= nonlocal_id
;
3894 process_constraint (new_constraint (lhs
, rhs
));
3895 returns_uses
= true;
3898 make_escape_constraint (arg
);
3901 /* If we added to the calls uses solution make sure we account for
3902 pointers to it to be returned. */
3905 rhsc
.var
= get_call_use_vi (stmt
)->id
;
3908 results
->safe_push (rhsc
);
3911 /* The static chain escapes as well. */
3912 if (gimple_call_chain (stmt
))
3913 make_escape_constraint (gimple_call_chain (stmt
));
3915 /* And if we applied NRV the address of the return slot escapes as well. */
3916 if (gimple_call_return_slot_opt_p (stmt
)
3917 && gimple_call_lhs (stmt
) != NULL_TREE
3918 && TREE_ADDRESSABLE (TREE_TYPE (gimple_call_lhs (stmt
))))
3920 vec
<ce_s
> tmpc
= vNULL
;
3921 struct constraint_expr lhsc
, *c
;
3922 get_constraint_for_address_of (gimple_call_lhs (stmt
), &tmpc
);
3923 lhsc
.var
= escaped_id
;
3926 FOR_EACH_VEC_ELT (tmpc
, i
, c
)
3927 process_constraint (new_constraint (lhsc
, *c
));
3931 /* Regular functions return nonlocal memory. */
3932 rhsc
.var
= nonlocal_id
;
3935 results
->safe_push (rhsc
);
3938 /* For non-IPA mode, generate constraints necessary for a call
3939 that returns a pointer and assigns it to LHS. This simply makes
3940 the LHS point to global and escaped variables. */
3943 handle_lhs_call (gimple stmt
, tree lhs
, int flags
, vec
<ce_s
> rhsc
,
3946 vec
<ce_s
> lhsc
= vNULL
;
3948 get_constraint_for (lhs
, &lhsc
);
3949 /* If the store is to a global decl make sure to
3950 add proper escape constraints. */
3951 lhs
= get_base_address (lhs
);
3954 && is_global_var (lhs
))
3956 struct constraint_expr tmpc
;
3957 tmpc
.var
= escaped_id
;
3960 lhsc
.safe_push (tmpc
);
3963 /* If the call returns an argument unmodified override the rhs
3965 flags
= gimple_call_return_flags (stmt
);
3966 if (flags
& ERF_RETURNS_ARG
3967 && (flags
& ERF_RETURN_ARG_MASK
) < gimple_call_num_args (stmt
))
3971 arg
= gimple_call_arg (stmt
, flags
& ERF_RETURN_ARG_MASK
);
3972 get_constraint_for (arg
, &rhsc
);
3973 process_all_all_constraints (lhsc
, rhsc
);
3976 else if (flags
& ERF_NOALIAS
)
3979 struct constraint_expr tmpc
;
3981 vi
= make_heapvar ("HEAP");
3982 /* We delay marking allocated storage global until we know if
3984 DECL_EXTERNAL (vi
->decl
) = 0;
3985 vi
->is_global_var
= 0;
3986 /* If this is not a real malloc call assume the memory was
3987 initialized and thus may point to global memory. All
3988 builtin functions with the malloc attribute behave in a sane way. */
3990 || DECL_BUILT_IN_CLASS (fndecl
) != BUILT_IN_NORMAL
)
3991 make_constraint_from (vi
, nonlocal_id
);
3994 tmpc
.type
= ADDRESSOF
;
3995 rhsc
.safe_push (tmpc
);
3996 process_all_all_constraints (lhsc
, rhsc
);
4000 process_all_all_constraints (lhsc
, rhsc
);
4005 /* For non-IPA mode, generate constraints necessary for a call of a
4006 const function that returns a pointer in the statement STMT. */
4009 handle_const_call (gimple stmt
, vec
<ce_s
> *results
)
4011 struct constraint_expr rhsc
;
4014 /* Treat nested const functions the same as pure functions as far
4015 as the static chain is concerned. */
4016 if (gimple_call_chain (stmt
))
4018 varinfo_t uses
= get_call_use_vi (stmt
);
4019 make_transitive_closure_constraints (uses
);
4020 make_constraint_to (uses
->id
, gimple_call_chain (stmt
));
4021 rhsc
.var
= uses
->id
;
4024 results
->safe_push (rhsc
);
4027 /* May return arguments. */
4028 for (k
= 0; k
< gimple_call_num_args (stmt
); ++k
)
4030 tree arg
= gimple_call_arg (stmt
, k
);
4031 vec
<ce_s
> argc
= vNULL
;
4033 struct constraint_expr
*argp
;
4034 get_constraint_for_rhs (arg
, &argc
);
4035 FOR_EACH_VEC_ELT (argc
, i
, argp
)
4036 results
->safe_push (*argp
);
4040 /* May return addresses of globals. */
4041 rhsc
.var
= nonlocal_id
;
4043 rhsc
.type
= ADDRESSOF
;
4044 results
->safe_push (rhsc
);
4047 /* For non-IPA mode, generate constraints necessary for a call to a
4048 pure function in statement STMT. */
4051 handle_pure_call (gimple stmt
, vec
<ce_s
> *results
)
4053 struct constraint_expr rhsc
;
4055 varinfo_t uses
= NULL
;
4057 /* Memory reached from pointer arguments is call-used. */
4058 for (i
= 0; i
< gimple_call_num_args (stmt
); ++i
)
4060 tree arg
= gimple_call_arg (stmt
, i
);
4063 uses
= get_call_use_vi (stmt
);
4064 make_transitive_closure_constraints (uses
);
4066 make_constraint_to (uses
->id
, arg
);
4069 /* The static chain is used as well. */
4070 if (gimple_call_chain (stmt
))
4074 uses
= get_call_use_vi (stmt
);
4075 make_transitive_closure_constraints (uses
);
4077 make_constraint_to (uses
->id
, gimple_call_chain (stmt
));
4080 /* Pure functions may return call-used and nonlocal memory. */
4083 rhsc
.var
= uses
->id
;
4086 results
->safe_push (rhsc
);
4088 rhsc
.var
= nonlocal_id
;
4091 results
->safe_push (rhsc
);
4095 /* Return the varinfo for the callee of CALL. */
4098 get_fi_for_callee (gimple call
)
4100 tree decl
, fn
= gimple_call_fn (call
);
4102 if (fn
&& TREE_CODE (fn
) == OBJ_TYPE_REF
)
4103 fn
= OBJ_TYPE_REF_EXPR (fn
);
4105 /* If we can directly resolve the function being called, do so.
4106 Otherwise, it must be some sort of indirect expression that
4107 we should still be able to handle. */
4108 decl
= gimple_call_addr_fndecl (fn
);
4110 return get_vi_for_tree (decl
);
4112 /* If the function is anything other than a SSA name pointer we have no
4113 clue and should be getting ANYFN (well, ANYTHING for now). */
4114 if (!fn
|| TREE_CODE (fn
) != SSA_NAME
)
4115 return get_varinfo (anything_id
);
4117 if (SSA_NAME_IS_DEFAULT_DEF (fn
)
4118 && (TREE_CODE (SSA_NAME_VAR (fn
)) == PARM_DECL
4119 || TREE_CODE (SSA_NAME_VAR (fn
)) == RESULT_DECL
))
4120 fn
= SSA_NAME_VAR (fn
);
4122 return get_vi_for_tree (fn
);
4125 /* Create constraints for the builtin call T. Return true if the call
4126 was handled, otherwise false. */
4129 find_func_aliases_for_builtin_call (gimple t
)
4131 tree fndecl
= gimple_call_fndecl (t
);
4132 vec
<ce_s
> lhsc
= vNULL
;
4133 vec
<ce_s
> rhsc
= vNULL
;
4136 if (gimple_call_builtin_p (t
, BUILT_IN_NORMAL
))
4137 /* ??? All builtins that are handled here need to be handled
4138 in the alias-oracle query functions explicitly! */
4139 switch (DECL_FUNCTION_CODE (fndecl
))
4141 /* All the following functions return a pointer to the same object
4142 as their first argument points to. The functions do not add
4143 to the ESCAPED solution. The functions make the first argument
4144 pointed to memory point to what the second argument pointed to
4145 memory points to. */
4146 case BUILT_IN_STRCPY
:
4147 case BUILT_IN_STRNCPY
:
4148 case BUILT_IN_BCOPY
:
4149 case BUILT_IN_MEMCPY
:
4150 case BUILT_IN_MEMMOVE
:
4151 case BUILT_IN_MEMPCPY
:
4152 case BUILT_IN_STPCPY
:
4153 case BUILT_IN_STPNCPY
:
4154 case BUILT_IN_STRCAT
:
4155 case BUILT_IN_STRNCAT
:
4156 case BUILT_IN_STRCPY_CHK
:
4157 case BUILT_IN_STRNCPY_CHK
:
4158 case BUILT_IN_MEMCPY_CHK
:
4159 case BUILT_IN_MEMMOVE_CHK
:
4160 case BUILT_IN_MEMPCPY_CHK
:
4161 case BUILT_IN_STPCPY_CHK
:
4162 case BUILT_IN_STPNCPY_CHK
:
4163 case BUILT_IN_STRCAT_CHK
:
4164 case BUILT_IN_STRNCAT_CHK
:
4165 case BUILT_IN_TM_MEMCPY
:
4166 case BUILT_IN_TM_MEMMOVE
:
4168 tree res
= gimple_call_lhs (t
);
4169 tree dest
= gimple_call_arg (t
, (DECL_FUNCTION_CODE (fndecl
)
4170 == BUILT_IN_BCOPY
? 1 : 0));
4171 tree src
= gimple_call_arg (t
, (DECL_FUNCTION_CODE (fndecl
)
4172 == BUILT_IN_BCOPY
? 0 : 1));
4173 if (res
!= NULL_TREE
)
4175 get_constraint_for (res
, &lhsc
);
4176 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_MEMPCPY
4177 || DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_STPCPY
4178 || DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_STPNCPY
4179 || DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_MEMPCPY_CHK
4180 || DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_STPCPY_CHK
4181 || DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_STPNCPY_CHK
)
4182 get_constraint_for_ptr_offset (dest
, NULL_TREE
, &rhsc
);
4184 get_constraint_for (dest
, &rhsc
);
4185 process_all_all_constraints (lhsc
, rhsc
);
4189 get_constraint_for_ptr_offset (dest
, NULL_TREE
, &lhsc
);
4190 get_constraint_for_ptr_offset (src
, NULL_TREE
, &rhsc
);
4193 process_all_all_constraints (lhsc
, rhsc
);
4198 case BUILT_IN_MEMSET
:
4199 case BUILT_IN_MEMSET_CHK
:
4200 case BUILT_IN_TM_MEMSET
:
4202 tree res
= gimple_call_lhs (t
);
4203 tree dest
= gimple_call_arg (t
, 0);
4206 struct constraint_expr ac
;
4207 if (res
!= NULL_TREE
)
4209 get_constraint_for (res
, &lhsc
);
4210 get_constraint_for (dest
, &rhsc
);
4211 process_all_all_constraints (lhsc
, rhsc
);
4215 get_constraint_for_ptr_offset (dest
, NULL_TREE
, &lhsc
);
4217 if (flag_delete_null_pointer_checks
4218 && integer_zerop (gimple_call_arg (t
, 1)))
4220 ac
.type
= ADDRESSOF
;
4221 ac
.var
= nothing_id
;
4226 ac
.var
= integer_id
;
4229 FOR_EACH_VEC_ELT (lhsc
, i
, lhsp
)
4230 process_constraint (new_constraint (*lhsp
, ac
));
4234 case BUILT_IN_ASSUME_ALIGNED
:
4236 tree res
= gimple_call_lhs (t
);
4237 tree dest
= gimple_call_arg (t
, 0);
4238 if (res
!= NULL_TREE
)
4240 get_constraint_for (res
, &lhsc
);
4241 get_constraint_for (dest
, &rhsc
);
4242 process_all_all_constraints (lhsc
, rhsc
);
4248 /* All the following functions do not return pointers, do not
4249 modify the points-to sets of memory reachable from their
4250 arguments and do not add to the ESCAPED solution. */
4251 case BUILT_IN_SINCOS
:
4252 case BUILT_IN_SINCOSF
:
4253 case BUILT_IN_SINCOSL
:
4254 case BUILT_IN_FREXP
:
4255 case BUILT_IN_FREXPF
:
4256 case BUILT_IN_FREXPL
:
4257 case BUILT_IN_GAMMA_R
:
4258 case BUILT_IN_GAMMAF_R
:
4259 case BUILT_IN_GAMMAL_R
:
4260 case BUILT_IN_LGAMMA_R
:
4261 case BUILT_IN_LGAMMAF_R
:
4262 case BUILT_IN_LGAMMAL_R
:
4264 case BUILT_IN_MODFF
:
4265 case BUILT_IN_MODFL
:
4266 case BUILT_IN_REMQUO
:
4267 case BUILT_IN_REMQUOF
:
4268 case BUILT_IN_REMQUOL
:
4271 case BUILT_IN_STRDUP
:
4272 case BUILT_IN_STRNDUP
:
4273 if (gimple_call_lhs (t
))
4275 handle_lhs_call (t
, gimple_call_lhs (t
), gimple_call_flags (t
),
4277 get_constraint_for_ptr_offset (gimple_call_lhs (t
),
4279 get_constraint_for_ptr_offset (gimple_call_arg (t
, 0),
4283 process_all_all_constraints (lhsc
, rhsc
);
4289 /* String / character search functions return a pointer into the
4290 source string or NULL. */
4291 case BUILT_IN_INDEX
:
4292 case BUILT_IN_STRCHR
:
4293 case BUILT_IN_STRRCHR
:
4294 case BUILT_IN_MEMCHR
:
4295 case BUILT_IN_STRSTR
:
4296 case BUILT_IN_STRPBRK
:
4297 if (gimple_call_lhs (t
))
4299 tree src
= gimple_call_arg (t
, 0);
4300 get_constraint_for_ptr_offset (src
, NULL_TREE
, &rhsc
);
4301 constraint_expr nul
;
4302 nul
.var
= nothing_id
;
4304 nul
.type
= ADDRESSOF
;
4305 rhsc
.safe_push (nul
);
4306 get_constraint_for (gimple_call_lhs (t
), &lhsc
);
4307 process_all_all_constraints (lhsc
, rhsc
);
4312 /* Trampolines are special - they set up passing the static
4314 case BUILT_IN_INIT_TRAMPOLINE
:
4316 tree tramp
= gimple_call_arg (t
, 0);
4317 tree nfunc
= gimple_call_arg (t
, 1);
4318 tree frame
= gimple_call_arg (t
, 2);
4320 struct constraint_expr lhs
, *rhsp
;
4323 varinfo_t nfi
= NULL
;
4324 gcc_assert (TREE_CODE (nfunc
) == ADDR_EXPR
);
4325 nfi
= lookup_vi_for_tree (TREE_OPERAND (nfunc
, 0));
4328 lhs
= get_function_part_constraint (nfi
, fi_static_chain
);
4329 get_constraint_for (frame
, &rhsc
);
4330 FOR_EACH_VEC_ELT (rhsc
, i
, rhsp
)
4331 process_constraint (new_constraint (lhs
, *rhsp
));
4334 /* Make the frame point to the function for
4335 the trampoline adjustment call. */
4336 get_constraint_for (tramp
, &lhsc
);
4338 get_constraint_for (nfunc
, &rhsc
);
4339 process_all_all_constraints (lhsc
, rhsc
);
4346 /* Else fallthru to generic handling which will let
4347 the frame escape. */
4350 case BUILT_IN_ADJUST_TRAMPOLINE
:
4352 tree tramp
= gimple_call_arg (t
, 0);
4353 tree res
= gimple_call_lhs (t
);
4354 if (in_ipa_mode
&& res
)
4356 get_constraint_for (res
, &lhsc
);
4357 get_constraint_for (tramp
, &rhsc
);
4359 process_all_all_constraints (lhsc
, rhsc
);
4365 CASE_BUILT_IN_TM_STORE (1):
4366 CASE_BUILT_IN_TM_STORE (2):
4367 CASE_BUILT_IN_TM_STORE (4):
4368 CASE_BUILT_IN_TM_STORE (8):
4369 CASE_BUILT_IN_TM_STORE (FLOAT
):
4370 CASE_BUILT_IN_TM_STORE (DOUBLE
):
4371 CASE_BUILT_IN_TM_STORE (LDOUBLE
):
4372 CASE_BUILT_IN_TM_STORE (M64
):
4373 CASE_BUILT_IN_TM_STORE (M128
):
4374 CASE_BUILT_IN_TM_STORE (M256
):
4376 tree addr
= gimple_call_arg (t
, 0);
4377 tree src
= gimple_call_arg (t
, 1);
4379 get_constraint_for (addr
, &lhsc
);
4381 get_constraint_for (src
, &rhsc
);
4382 process_all_all_constraints (lhsc
, rhsc
);
4387 CASE_BUILT_IN_TM_LOAD (1):
4388 CASE_BUILT_IN_TM_LOAD (2):
4389 CASE_BUILT_IN_TM_LOAD (4):
4390 CASE_BUILT_IN_TM_LOAD (8):
4391 CASE_BUILT_IN_TM_LOAD (FLOAT
):
4392 CASE_BUILT_IN_TM_LOAD (DOUBLE
):
4393 CASE_BUILT_IN_TM_LOAD (LDOUBLE
):
4394 CASE_BUILT_IN_TM_LOAD (M64
):
4395 CASE_BUILT_IN_TM_LOAD (M128
):
4396 CASE_BUILT_IN_TM_LOAD (M256
):
4398 tree dest
= gimple_call_lhs (t
);
4399 tree addr
= gimple_call_arg (t
, 0);
4401 get_constraint_for (dest
, &lhsc
);
4402 get_constraint_for (addr
, &rhsc
);
4404 process_all_all_constraints (lhsc
, rhsc
);
4409 /* Variadic argument handling needs to be handled in IPA
4411 case BUILT_IN_VA_START
:
4413 tree valist
= gimple_call_arg (t
, 0);
4414 struct constraint_expr rhs
, *lhsp
;
4416 get_constraint_for (valist
, &lhsc
);
4418 /* The va_list gets access to pointers in variadic
4419 arguments. Which we know in the case of IPA analysis
4420 and otherwise are just all nonlocal variables. */
4423 fi
= lookup_vi_for_tree (cfun
->decl
);
4424 rhs
= get_function_part_constraint (fi
, ~0);
4425 rhs
.type
= ADDRESSOF
;
4429 rhs
.var
= nonlocal_id
;
4430 rhs
.type
= ADDRESSOF
;
4433 FOR_EACH_VEC_ELT (lhsc
, i
, lhsp
)
4434 process_constraint (new_constraint (*lhsp
, rhs
));
4436 /* va_list is clobbered. */
4437 make_constraint_to (get_call_clobber_vi (t
)->id
, valist
);
4440 /* va_end doesn't have any effect that matters. */
4441 case BUILT_IN_VA_END
:
4443 /* Alternate return. Simply give up for now. */
4444 case BUILT_IN_RETURN
:
4448 || !(fi
= get_vi_for_tree (cfun
->decl
)))
4449 make_constraint_from (get_varinfo (escaped_id
), anything_id
);
4450 else if (in_ipa_mode
4453 struct constraint_expr lhs
, rhs
;
4454 lhs
= get_function_part_constraint (fi
, fi_result
);
4455 rhs
.var
= anything_id
;
4458 process_constraint (new_constraint (lhs
, rhs
));
4462 /* printf-style functions may have hooks to set pointers to
4463 point to somewhere into the generated string. Leave them
4464 for a later exercise... */
4466 /* Fallthru to general call handling. */;
4472 /* Create constraints for the call T. */
4475 find_func_aliases_for_call (gimple t
)
4477 tree fndecl
= gimple_call_fndecl (t
);
4478 vec
<ce_s
> lhsc
= vNULL
;
4479 vec
<ce_s
> rhsc
= vNULL
;
4482 if (fndecl
!= NULL_TREE
4483 && DECL_BUILT_IN (fndecl
)
4484 && find_func_aliases_for_builtin_call (t
))
4487 fi
= get_fi_for_callee (t
);
4489 || (fndecl
&& !fi
->is_fn_info
))
4491 vec
<ce_s
> rhsc
= vNULL
;
4492 int flags
= gimple_call_flags (t
);
4494 /* Const functions can return their arguments and addresses
4495 of global memory but not of escaped memory. */
4496 if (flags
& (ECF_CONST
|ECF_NOVOPS
))
4498 if (gimple_call_lhs (t
))
4499 handle_const_call (t
, &rhsc
);
4501 /* Pure functions can return addresses in and of memory
4502 reachable from their arguments, but they are not an escape
4503 point for reachable memory of their arguments. */
4504 else if (flags
& (ECF_PURE
|ECF_LOOPING_CONST_OR_PURE
))
4505 handle_pure_call (t
, &rhsc
);
4507 handle_rhs_call (t
, &rhsc
);
4508 if (gimple_call_lhs (t
))
4509 handle_lhs_call (t
, gimple_call_lhs (t
), flags
, rhsc
, fndecl
);
4517 /* Assign all the passed arguments to the appropriate incoming
4518 parameters of the function. */
4519 for (j
= 0; j
< gimple_call_num_args (t
); j
++)
4521 struct constraint_expr lhs
;
4522 struct constraint_expr
*rhsp
;
4523 tree arg
= gimple_call_arg (t
, j
);
4525 get_constraint_for_rhs (arg
, &rhsc
);
4526 lhs
= get_function_part_constraint (fi
, fi_parm_base
+ j
);
4527 while (rhsc
.length () != 0)
4529 rhsp
= &rhsc
.last ();
4530 process_constraint (new_constraint (lhs
, *rhsp
));
4535 /* If we are returning a value, assign it to the result. */
4536 lhsop
= gimple_call_lhs (t
);
4539 struct constraint_expr rhs
;
4540 struct constraint_expr
*lhsp
;
4542 get_constraint_for (lhsop
, &lhsc
);
4543 rhs
= get_function_part_constraint (fi
, fi_result
);
4545 && DECL_RESULT (fndecl
)
4546 && DECL_BY_REFERENCE (DECL_RESULT (fndecl
)))
4548 vec
<ce_s
> tem
= vNULL
;
4549 tem
.safe_push (rhs
);
4554 FOR_EACH_VEC_ELT (lhsc
, j
, lhsp
)
4555 process_constraint (new_constraint (*lhsp
, rhs
));
4558 /* If we pass the result decl by reference, honor that. */
4561 && DECL_RESULT (fndecl
)
4562 && DECL_BY_REFERENCE (DECL_RESULT (fndecl
)))
4564 struct constraint_expr lhs
;
4565 struct constraint_expr
*rhsp
;
4567 get_constraint_for_address_of (lhsop
, &rhsc
);
4568 lhs
= get_function_part_constraint (fi
, fi_result
);
4569 FOR_EACH_VEC_ELT (rhsc
, j
, rhsp
)
4570 process_constraint (new_constraint (lhs
, *rhsp
));
4574 /* If we use a static chain, pass it along. */
4575 if (gimple_call_chain (t
))
4577 struct constraint_expr lhs
;
4578 struct constraint_expr
*rhsp
;
4580 get_constraint_for (gimple_call_chain (t
), &rhsc
);
4581 lhs
= get_function_part_constraint (fi
, fi_static_chain
);
4582 FOR_EACH_VEC_ELT (rhsc
, j
, rhsp
)
4583 process_constraint (new_constraint (lhs
, *rhsp
));
4588 /* Walk statement T setting up aliasing constraints according to the
4589 references found in T. This function is the main part of the
4590 constraint builder. AI points to auxiliary alias information used
4591 when building alias sets and computing alias grouping heuristics. */
4594 find_func_aliases (gimple origt
)
4597 vec
<ce_s
> lhsc
= vNULL
;
4598 vec
<ce_s
> rhsc
= vNULL
;
4599 struct constraint_expr
*c
;
4602 /* Now build constraints expressions. */
4603 if (gimple_code (t
) == GIMPLE_PHI
)
4608 /* For a phi node, assign all the arguments to
4610 get_constraint_for (gimple_phi_result (t
), &lhsc
);
4611 for (i
= 0; i
< gimple_phi_num_args (t
); i
++)
4613 tree strippedrhs
= PHI_ARG_DEF (t
, i
);
4615 STRIP_NOPS (strippedrhs
);
4616 get_constraint_for_rhs (gimple_phi_arg_def (t
, i
), &rhsc
);
4618 FOR_EACH_VEC_ELT (lhsc
, j
, c
)
4620 struct constraint_expr
*c2
;
4621 while (rhsc
.length () > 0)
4624 process_constraint (new_constraint (*c
, *c2
));
4630 /* In IPA mode, we need to generate constraints to pass call
4631 arguments through their calls. There are two cases,
4632 either a GIMPLE_CALL returning a value, or just a plain
4633 GIMPLE_CALL when we are not.
4635 In non-ipa mode, we need to generate constraints for each
4636 pointer passed by address. */
4637 else if (is_gimple_call (t
))
4638 find_func_aliases_for_call (t
);
4640 /* Otherwise, just a regular assignment statement. Only care about
4641 operations with pointer result, others are dealt with as escape
4642 points if they have pointer operands. */
4643 else if (is_gimple_assign (t
))
4645 /* Otherwise, just a regular assignment statement. */
4646 tree lhsop
= gimple_assign_lhs (t
);
4647 tree rhsop
= (gimple_num_ops (t
) == 2) ? gimple_assign_rhs1 (t
) : NULL
;
4649 if (rhsop
&& TREE_CLOBBER_P (rhsop
))
4650 /* Ignore clobbers, they don't actually store anything into
4653 else if (rhsop
&& AGGREGATE_TYPE_P (TREE_TYPE (lhsop
)))
4654 do_structure_copy (lhsop
, rhsop
);
4657 enum tree_code code
= gimple_assign_rhs_code (t
);
4659 get_constraint_for (lhsop
, &lhsc
);
4661 if (FLOAT_TYPE_P (TREE_TYPE (lhsop
)))
4662 /* If the operation produces a floating point result then
4663 assume the value is not produced to transfer a pointer. */
4665 else if (code
== POINTER_PLUS_EXPR
)
4666 get_constraint_for_ptr_offset (gimple_assign_rhs1 (t
),
4667 gimple_assign_rhs2 (t
), &rhsc
);
4668 else if (code
== BIT_AND_EXPR
4669 && TREE_CODE (gimple_assign_rhs2 (t
)) == INTEGER_CST
)
4671 /* Aligning a pointer via a BIT_AND_EXPR is offsetting
4672 the pointer. Handle it by offsetting it by UNKNOWN. */
4673 get_constraint_for_ptr_offset (gimple_assign_rhs1 (t
),
4676 else if ((CONVERT_EXPR_CODE_P (code
)
4677 && !(POINTER_TYPE_P (gimple_expr_type (t
))
4678 && !POINTER_TYPE_P (TREE_TYPE (rhsop
))))
4679 || gimple_assign_single_p (t
))
4680 get_constraint_for_rhs (rhsop
, &rhsc
);
4681 else if (code
== COND_EXPR
)
4683 /* The result is a merge of both COND_EXPR arms. */
4684 vec
<ce_s
> tmp
= vNULL
;
4685 struct constraint_expr
*rhsp
;
4687 get_constraint_for_rhs (gimple_assign_rhs2 (t
), &rhsc
);
4688 get_constraint_for_rhs (gimple_assign_rhs3 (t
), &tmp
);
4689 FOR_EACH_VEC_ELT (tmp
, i
, rhsp
)
4690 rhsc
.safe_push (*rhsp
);
4693 else if (truth_value_p (code
))
4694 /* Truth value results are not pointer (parts). Or at least
4695 very very unreasonable obfuscation of a part. */
4699 /* All other operations are merges. */
4700 vec
<ce_s
> tmp
= vNULL
;
4701 struct constraint_expr
*rhsp
;
4703 get_constraint_for_rhs (gimple_assign_rhs1 (t
), &rhsc
);
4704 for (i
= 2; i
< gimple_num_ops (t
); ++i
)
4706 get_constraint_for_rhs (gimple_op (t
, i
), &tmp
);
4707 FOR_EACH_VEC_ELT (tmp
, j
, rhsp
)
4708 rhsc
.safe_push (*rhsp
);
4713 process_all_all_constraints (lhsc
, rhsc
);
4715 /* If there is a store to a global variable the rhs escapes. */
4716 if ((lhsop
= get_base_address (lhsop
)) != NULL_TREE
4718 && is_global_var (lhsop
)
4720 || DECL_EXTERNAL (lhsop
) || TREE_PUBLIC (lhsop
)))
4721 make_escape_constraint (rhsop
);
4723 /* Handle escapes through return. */
4724 else if (gimple_code (t
) == GIMPLE_RETURN
4725 && gimple_return_retval (t
) != NULL_TREE
)
4729 || !(fi
= get_vi_for_tree (cfun
->decl
)))
4730 make_escape_constraint (gimple_return_retval (t
));
4731 else if (in_ipa_mode
4734 struct constraint_expr lhs
;
4735 struct constraint_expr
*rhsp
;
4738 lhs
= get_function_part_constraint (fi
, fi_result
);
4739 get_constraint_for_rhs (gimple_return_retval (t
), &rhsc
);
4740 FOR_EACH_VEC_ELT (rhsc
, i
, rhsp
)
4741 process_constraint (new_constraint (lhs
, *rhsp
));
4744 /* Handle asms conservatively by adding escape constraints to everything. */
4745 else if (gimple_code (t
) == GIMPLE_ASM
)
4747 unsigned i
, noutputs
;
4748 const char **oconstraints
;
4749 const char *constraint
;
4750 bool allows_mem
, allows_reg
, is_inout
;
4752 noutputs
= gimple_asm_noutputs (t
);
4753 oconstraints
= XALLOCAVEC (const char *, noutputs
);
4755 for (i
= 0; i
< noutputs
; ++i
)
4757 tree link
= gimple_asm_output_op (t
, i
);
4758 tree op
= TREE_VALUE (link
);
4760 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link
)));
4761 oconstraints
[i
] = constraint
;
4762 parse_output_constraint (&constraint
, i
, 0, 0, &allows_mem
,
4763 &allows_reg
, &is_inout
);
4765 /* A memory constraint makes the address of the operand escape. */
4766 if (!allows_reg
&& allows_mem
)
4767 make_escape_constraint (build_fold_addr_expr (op
));
4769 /* The asm may read global memory, so outputs may point to
4770 any global memory. */
4773 vec
<ce_s
> lhsc
= vNULL
;
4774 struct constraint_expr rhsc
, *lhsp
;
4776 get_constraint_for (op
, &lhsc
);
4777 rhsc
.var
= nonlocal_id
;
4780 FOR_EACH_VEC_ELT (lhsc
, j
, lhsp
)
4781 process_constraint (new_constraint (*lhsp
, rhsc
));
4785 for (i
= 0; i
< gimple_asm_ninputs (t
); ++i
)
4787 tree link
= gimple_asm_input_op (t
, i
);
4788 tree op
= TREE_VALUE (link
);
4790 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link
)));
4792 parse_input_constraint (&constraint
, 0, 0, noutputs
, 0, oconstraints
,
4793 &allows_mem
, &allows_reg
);
4795 /* A memory constraint makes the address of the operand escape. */
4796 if (!allows_reg
&& allows_mem
)
4797 make_escape_constraint (build_fold_addr_expr (op
));
4798 /* Strictly we'd only need the constraint to ESCAPED if
4799 the asm clobbers memory, otherwise using something
4800 along the lines of per-call clobbers/uses would be enough. */
4802 make_escape_constraint (op
);
4811 /* Create a constraint adding to the clobber set of FI the memory
4812 pointed to by PTR. */
4815 process_ipa_clobber (varinfo_t fi
, tree ptr
)
4817 vec
<ce_s
> ptrc
= vNULL
;
4818 struct constraint_expr
*c
, lhs
;
4820 get_constraint_for_rhs (ptr
, &ptrc
);
4821 lhs
= get_function_part_constraint (fi
, fi_clobbers
);
4822 FOR_EACH_VEC_ELT (ptrc
, i
, c
)
4823 process_constraint (new_constraint (lhs
, *c
));
4827 /* Walk statement T setting up clobber and use constraints according to the
4828 references found in T. This function is a main part of the
4829 IPA constraint builder. */
4832 find_func_clobbers (gimple origt
)
4835 vec
<ce_s
> lhsc
= vNULL
;
4836 vec
<ce_s
> rhsc
= vNULL
;
4839 /* Add constraints for clobbered/used in IPA mode.
4840 We are not interested in what automatic variables are clobbered
4841 or used as we only use the information in the caller to which
4842 they do not escape. */
4843 gcc_assert (in_ipa_mode
);
4845 /* If the stmt refers to memory in any way it better had a VUSE. */
4846 if (gimple_vuse (t
) == NULL_TREE
)
4849 /* We'd better have function information for the current function. */
4850 fi
= lookup_vi_for_tree (cfun
->decl
);
4851 gcc_assert (fi
!= NULL
);
4853 /* Account for stores in assignments and calls. */
4854 if (gimple_vdef (t
) != NULL_TREE
4855 && gimple_has_lhs (t
))
4857 tree lhs
= gimple_get_lhs (t
);
4859 while (handled_component_p (tem
))
4860 tem
= TREE_OPERAND (tem
, 0);
4862 && !auto_var_in_fn_p (tem
, cfun
->decl
))
4863 || INDIRECT_REF_P (tem
)
4864 || (TREE_CODE (tem
) == MEM_REF
4865 && !(TREE_CODE (TREE_OPERAND (tem
, 0)) == ADDR_EXPR
4867 (TREE_OPERAND (TREE_OPERAND (tem
, 0), 0), cfun
->decl
))))
4869 struct constraint_expr lhsc
, *rhsp
;
4871 lhsc
= get_function_part_constraint (fi
, fi_clobbers
);
4872 get_constraint_for_address_of (lhs
, &rhsc
);
4873 FOR_EACH_VEC_ELT (rhsc
, i
, rhsp
)
4874 process_constraint (new_constraint (lhsc
, *rhsp
));
4879 /* Account for uses in assigments and returns. */
4880 if (gimple_assign_single_p (t
)
4881 || (gimple_code (t
) == GIMPLE_RETURN
4882 && gimple_return_retval (t
) != NULL_TREE
))
4884 tree rhs
= (gimple_assign_single_p (t
)
4885 ? gimple_assign_rhs1 (t
) : gimple_return_retval (t
));
4887 while (handled_component_p (tem
))
4888 tem
= TREE_OPERAND (tem
, 0);
4890 && !auto_var_in_fn_p (tem
, cfun
->decl
))
4891 || INDIRECT_REF_P (tem
)
4892 || (TREE_CODE (tem
) == MEM_REF
4893 && !(TREE_CODE (TREE_OPERAND (tem
, 0)) == ADDR_EXPR
4895 (TREE_OPERAND (TREE_OPERAND (tem
, 0), 0), cfun
->decl
))))
4897 struct constraint_expr lhs
, *rhsp
;
4899 lhs
= get_function_part_constraint (fi
, fi_uses
);
4900 get_constraint_for_address_of (rhs
, &rhsc
);
4901 FOR_EACH_VEC_ELT (rhsc
, i
, rhsp
)
4902 process_constraint (new_constraint (lhs
, *rhsp
));
4907 if (is_gimple_call (t
))
4909 varinfo_t cfi
= NULL
;
4910 tree decl
= gimple_call_fndecl (t
);
4911 struct constraint_expr lhs
, rhs
;
4914 /* For builtins we do not have separate function info. For those
4915 we do not generate escapes for we have to generate clobbers/uses. */
4916 if (gimple_call_builtin_p (t
, BUILT_IN_NORMAL
))
4917 switch (DECL_FUNCTION_CODE (decl
))
4919 /* The following functions use and clobber memory pointed to
4920 by their arguments. */
4921 case BUILT_IN_STRCPY
:
4922 case BUILT_IN_STRNCPY
:
4923 case BUILT_IN_BCOPY
:
4924 case BUILT_IN_MEMCPY
:
4925 case BUILT_IN_MEMMOVE
:
4926 case BUILT_IN_MEMPCPY
:
4927 case BUILT_IN_STPCPY
:
4928 case BUILT_IN_STPNCPY
:
4929 case BUILT_IN_STRCAT
:
4930 case BUILT_IN_STRNCAT
:
4931 case BUILT_IN_STRCPY_CHK
:
4932 case BUILT_IN_STRNCPY_CHK
:
4933 case BUILT_IN_MEMCPY_CHK
:
4934 case BUILT_IN_MEMMOVE_CHK
:
4935 case BUILT_IN_MEMPCPY_CHK
:
4936 case BUILT_IN_STPCPY_CHK
:
4937 case BUILT_IN_STPNCPY_CHK
:
4938 case BUILT_IN_STRCAT_CHK
:
4939 case BUILT_IN_STRNCAT_CHK
:
4941 tree dest
= gimple_call_arg (t
, (DECL_FUNCTION_CODE (decl
)
4942 == BUILT_IN_BCOPY
? 1 : 0));
4943 tree src
= gimple_call_arg (t
, (DECL_FUNCTION_CODE (decl
)
4944 == BUILT_IN_BCOPY
? 0 : 1));
4946 struct constraint_expr
*rhsp
, *lhsp
;
4947 get_constraint_for_ptr_offset (dest
, NULL_TREE
, &lhsc
);
4948 lhs
= get_function_part_constraint (fi
, fi_clobbers
);
4949 FOR_EACH_VEC_ELT (lhsc
, i
, lhsp
)
4950 process_constraint (new_constraint (lhs
, *lhsp
));
4952 get_constraint_for_ptr_offset (src
, NULL_TREE
, &rhsc
);
4953 lhs
= get_function_part_constraint (fi
, fi_uses
);
4954 FOR_EACH_VEC_ELT (rhsc
, i
, rhsp
)
4955 process_constraint (new_constraint (lhs
, *rhsp
));
4959 /* The following function clobbers memory pointed to by
4961 case BUILT_IN_MEMSET
:
4962 case BUILT_IN_MEMSET_CHK
:
4964 tree dest
= gimple_call_arg (t
, 0);
4967 get_constraint_for_ptr_offset (dest
, NULL_TREE
, &lhsc
);
4968 lhs
= get_function_part_constraint (fi
, fi_clobbers
);
4969 FOR_EACH_VEC_ELT (lhsc
, i
, lhsp
)
4970 process_constraint (new_constraint (lhs
, *lhsp
));
4974 /* The following functions clobber their second and third
4976 case BUILT_IN_SINCOS
:
4977 case BUILT_IN_SINCOSF
:
4978 case BUILT_IN_SINCOSL
:
4980 process_ipa_clobber (fi
, gimple_call_arg (t
, 1));
4981 process_ipa_clobber (fi
, gimple_call_arg (t
, 2));
4984 /* The following functions clobber their second argument. */
4985 case BUILT_IN_FREXP
:
4986 case BUILT_IN_FREXPF
:
4987 case BUILT_IN_FREXPL
:
4988 case BUILT_IN_LGAMMA_R
:
4989 case BUILT_IN_LGAMMAF_R
:
4990 case BUILT_IN_LGAMMAL_R
:
4991 case BUILT_IN_GAMMA_R
:
4992 case BUILT_IN_GAMMAF_R
:
4993 case BUILT_IN_GAMMAL_R
:
4995 case BUILT_IN_MODFF
:
4996 case BUILT_IN_MODFL
:
4998 process_ipa_clobber (fi
, gimple_call_arg (t
, 1));
5001 /* The following functions clobber their third argument. */
5002 case BUILT_IN_REMQUO
:
5003 case BUILT_IN_REMQUOF
:
5004 case BUILT_IN_REMQUOL
:
5006 process_ipa_clobber (fi
, gimple_call_arg (t
, 2));
5009 /* The following functions neither read nor clobber memory. */
5010 case BUILT_IN_ASSUME_ALIGNED
:
5013 /* Trampolines are of no interest to us. */
5014 case BUILT_IN_INIT_TRAMPOLINE
:
5015 case BUILT_IN_ADJUST_TRAMPOLINE
:
5017 case BUILT_IN_VA_START
:
5018 case BUILT_IN_VA_END
:
5020 /* printf-style functions may have hooks to set pointers to
5021 point to somewhere into the generated string. Leave them
5022 for a later exercise... */
5024 /* Fallthru to general call handling. */;
5027 /* Parameters passed by value are used. */
5028 lhs
= get_function_part_constraint (fi
, fi_uses
);
5029 for (i
= 0; i
< gimple_call_num_args (t
); i
++)
5031 struct constraint_expr
*rhsp
;
5032 tree arg
= gimple_call_arg (t
, i
);
5034 if (TREE_CODE (arg
) == SSA_NAME
5035 || is_gimple_min_invariant (arg
))
5038 get_constraint_for_address_of (arg
, &rhsc
);
5039 FOR_EACH_VEC_ELT (rhsc
, j
, rhsp
)
5040 process_constraint (new_constraint (lhs
, *rhsp
));
5044 /* Build constraints for propagating clobbers/uses along the
5046 cfi
= get_fi_for_callee (t
);
5047 if (cfi
->id
== anything_id
)
5049 if (gimple_vdef (t
))
5050 make_constraint_from (first_vi_for_offset (fi
, fi_clobbers
),
5052 make_constraint_from (first_vi_for_offset (fi
, fi_uses
),
5057 /* For callees without function info (that's external functions),
5058 ESCAPED is clobbered and used. */
5059 if (gimple_call_fndecl (t
)
5060 && !cfi
->is_fn_info
)
5064 if (gimple_vdef (t
))
5065 make_copy_constraint (first_vi_for_offset (fi
, fi_clobbers
),
5067 make_copy_constraint (first_vi_for_offset (fi
, fi_uses
), escaped_id
);
5069 /* Also honor the call statement use/clobber info. */
5070 if ((vi
= lookup_call_clobber_vi (t
)) != NULL
)
5071 make_copy_constraint (first_vi_for_offset (fi
, fi_clobbers
),
5073 if ((vi
= lookup_call_use_vi (t
)) != NULL
)
5074 make_copy_constraint (first_vi_for_offset (fi
, fi_uses
),
5079 /* Otherwise the caller clobbers and uses what the callee does.
5080 ??? This should use a new complex constraint that filters
5081 local variables of the callee. */
5082 if (gimple_vdef (t
))
5084 lhs
= get_function_part_constraint (fi
, fi_clobbers
);
5085 rhs
= get_function_part_constraint (cfi
, fi_clobbers
);
5086 process_constraint (new_constraint (lhs
, rhs
));
5088 lhs
= get_function_part_constraint (fi
, fi_uses
);
5089 rhs
= get_function_part_constraint (cfi
, fi_uses
);
5090 process_constraint (new_constraint (lhs
, rhs
));
5092 else if (gimple_code (t
) == GIMPLE_ASM
)
5094 /* ??? Ick. We can do better. */
5095 if (gimple_vdef (t
))
5096 make_constraint_from (first_vi_for_offset (fi
, fi_clobbers
),
5098 make_constraint_from (first_vi_for_offset (fi
, fi_uses
),
5106 /* Find the first varinfo in the same variable as START that overlaps with
5107 OFFSET. Return NULL if we can't find one. */
5110 first_vi_for_offset (varinfo_t start
, unsigned HOST_WIDE_INT offset
)
5112 /* If the offset is outside of the variable, bail out. */
5113 if (offset
>= start
->fullsize
)
5116 /* If we cannot reach offset from start, lookup the first field
5117 and start from there. */
5118 if (start
->offset
> offset
)
5119 start
= get_varinfo (start
->head
);
5123 /* We may not find a variable in the field list with the actual
5124 offset when when we have glommed a structure to a variable.
5125 In that case, however, offset should still be within the size
5127 if (offset
>= start
->offset
5128 && (offset
- start
->offset
) < start
->size
)
5131 start
= vi_next (start
);
5137 /* Find the first varinfo in the same variable as START that overlaps with
5138 OFFSET. If there is no such varinfo the varinfo directly preceding
5139 OFFSET is returned. */
5142 first_or_preceding_vi_for_offset (varinfo_t start
,
5143 unsigned HOST_WIDE_INT offset
)
5145 /* If we cannot reach offset from start, lookup the first field
5146 and start from there. */
5147 if (start
->offset
> offset
)
5148 start
= get_varinfo (start
->head
);
5150 /* We may not find a variable in the field list with the actual
5151 offset when when we have glommed a structure to a variable.
5152 In that case, however, offset should still be within the size
5154 If we got beyond the offset we look for return the field
5155 directly preceding offset which may be the last field. */
5157 && offset
>= start
->offset
5158 && !((offset
- start
->offset
) < start
->size
))
5159 start
= vi_next (start
);
5165 /* This structure is used during pushing fields onto the fieldstack
5166 to track the offset of the field, since bitpos_of_field gives it
5167 relative to its immediate containing type, and we want it relative
5168 to the ultimate containing object. */
5172 /* Offset from the base of the base containing object to this field. */
5173 HOST_WIDE_INT offset
;
5175 /* Size, in bits, of the field. */
5176 unsigned HOST_WIDE_INT size
;
5178 unsigned has_unknown_size
: 1;
5180 unsigned must_have_pointers
: 1;
5182 unsigned may_have_pointers
: 1;
5184 unsigned only_restrict_pointers
: 1;
5186 typedef struct fieldoff fieldoff_s
;
5189 /* qsort comparison function for two fieldoff's PA and PB */
5192 fieldoff_compare (const void *pa
, const void *pb
)
5194 const fieldoff_s
*foa
= (const fieldoff_s
*)pa
;
5195 const fieldoff_s
*fob
= (const fieldoff_s
*)pb
;
5196 unsigned HOST_WIDE_INT foasize
, fobsize
;
5198 if (foa
->offset
< fob
->offset
)
5200 else if (foa
->offset
> fob
->offset
)
5203 foasize
= foa
->size
;
5204 fobsize
= fob
->size
;
5205 if (foasize
< fobsize
)
5207 else if (foasize
> fobsize
)
5212 /* Sort a fieldstack according to the field offset and sizes. */
5214 sort_fieldstack (vec
<fieldoff_s
> fieldstack
)
5216 fieldstack
.qsort (fieldoff_compare
);
5219 /* Return true if T is a type that can have subvars. */
5222 type_can_have_subvars (const_tree t
)
5224 /* Aggregates without overlapping fields can have subvars. */
5225 return TREE_CODE (t
) == RECORD_TYPE
;
5228 /* Return true if V is a tree that we can have subvars for.
5229 Normally, this is any aggregate type. Also complex
5230 types which are not gimple registers can have subvars. */
5233 var_can_have_subvars (const_tree v
)
5235 /* Volatile variables should never have subvars. */
5236 if (TREE_THIS_VOLATILE (v
))
5239 /* Non decls or memory tags can never have subvars. */
5243 return type_can_have_subvars (TREE_TYPE (v
));
5246 /* Return true if T is a type that does contain pointers. */
5249 type_must_have_pointers (tree type
)
5251 if (POINTER_TYPE_P (type
))
5254 if (TREE_CODE (type
) == ARRAY_TYPE
)
5255 return type_must_have_pointers (TREE_TYPE (type
));
5257 /* A function or method can have pointers as arguments, so track
5258 those separately. */
5259 if (TREE_CODE (type
) == FUNCTION_TYPE
5260 || TREE_CODE (type
) == METHOD_TYPE
)
5267 field_must_have_pointers (tree t
)
5269 return type_must_have_pointers (TREE_TYPE (t
));
5272 /* Given a TYPE, and a vector of field offsets FIELDSTACK, push all
5273 the fields of TYPE onto fieldstack, recording their offsets along
5276 OFFSET is used to keep track of the offset in this entire
5277 structure, rather than just the immediately containing structure.
5278 Returns false if the caller is supposed to handle the field we
5282 push_fields_onto_fieldstack (tree type
, vec
<fieldoff_s
> *fieldstack
,
5283 HOST_WIDE_INT offset
)
5286 bool empty_p
= true;
5288 if (TREE_CODE (type
) != RECORD_TYPE
)
5291 /* If the vector of fields is growing too big, bail out early.
5292 Callers check for vec::length <= MAX_FIELDS_FOR_FIELD_SENSITIVE, make
5294 if (fieldstack
->length () > MAX_FIELDS_FOR_FIELD_SENSITIVE
)
5297 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
5298 if (TREE_CODE (field
) == FIELD_DECL
)
5301 HOST_WIDE_INT foff
= bitpos_of_field (field
);
5303 if (!var_can_have_subvars (field
)
5304 || TREE_CODE (TREE_TYPE (field
)) == QUAL_UNION_TYPE
5305 || TREE_CODE (TREE_TYPE (field
)) == UNION_TYPE
)
5307 else if (!push_fields_onto_fieldstack
5308 (TREE_TYPE (field
), fieldstack
, offset
+ foff
)
5309 && (DECL_SIZE (field
)
5310 && !integer_zerop (DECL_SIZE (field
))))
5311 /* Empty structures may have actual size, like in C++. So
5312 see if we didn't push any subfields and the size is
5313 nonzero, push the field onto the stack. */
5318 fieldoff_s
*pair
= NULL
;
5319 bool has_unknown_size
= false;
5320 bool must_have_pointers_p
;
5322 if (!fieldstack
->is_empty ())
5323 pair
= &fieldstack
->last ();
5325 /* If there isn't anything at offset zero, create sth. */
5327 && offset
+ foff
!= 0)
5329 fieldoff_s e
= {0, offset
+ foff
, false, false, false, false};
5330 pair
= fieldstack
->safe_push (e
);
5333 if (!DECL_SIZE (field
)
5334 || !host_integerp (DECL_SIZE (field
), 1))
5335 has_unknown_size
= true;
5337 /* If adjacent fields do not contain pointers merge them. */
5338 must_have_pointers_p
= field_must_have_pointers (field
);
5340 && !has_unknown_size
5341 && !must_have_pointers_p
5342 && !pair
->must_have_pointers
5343 && !pair
->has_unknown_size
5344 && pair
->offset
+ (HOST_WIDE_INT
)pair
->size
== offset
+ foff
)
5346 pair
->size
+= TREE_INT_CST_LOW (DECL_SIZE (field
));
5351 e
.offset
= offset
+ foff
;
5352 e
.has_unknown_size
= has_unknown_size
;
5353 if (!has_unknown_size
)
5354 e
.size
= TREE_INT_CST_LOW (DECL_SIZE (field
));
5357 e
.must_have_pointers
= must_have_pointers_p
;
5358 e
.may_have_pointers
= true;
5359 e
.only_restrict_pointers
5360 = (!has_unknown_size
5361 && POINTER_TYPE_P (TREE_TYPE (field
))
5362 && TYPE_RESTRICT (TREE_TYPE (field
)));
5363 fieldstack
->safe_push (e
);
5373 /* Count the number of arguments DECL has, and set IS_VARARGS to true
5374 if it is a varargs function. */
5377 count_num_arguments (tree decl
, bool *is_varargs
)
5379 unsigned int num
= 0;
5382 /* Capture named arguments for K&R functions. They do not
5383 have a prototype and thus no TYPE_ARG_TYPES. */
5384 for (t
= DECL_ARGUMENTS (decl
); t
; t
= DECL_CHAIN (t
))
5387 /* Check if the function has variadic arguments. */
5388 for (t
= TYPE_ARG_TYPES (TREE_TYPE (decl
)); t
; t
= TREE_CHAIN (t
))
5389 if (TREE_VALUE (t
) == void_type_node
)
5397 /* Creation function node for DECL, using NAME, and return the index
5398 of the variable we've created for the function. */
5401 create_function_info_for (tree decl
, const char *name
)
5403 struct function
*fn
= DECL_STRUCT_FUNCTION (decl
);
5404 varinfo_t vi
, prev_vi
;
5407 bool is_varargs
= false;
5408 unsigned int num_args
= count_num_arguments (decl
, &is_varargs
);
5410 /* Create the variable info. */
5412 vi
= new_var_info (decl
, name
);
5415 vi
->fullsize
= fi_parm_base
+ num_args
;
5417 vi
->may_have_pointers
= false;
5420 insert_vi_for_tree (vi
->decl
, vi
);
5424 /* Create a variable for things the function clobbers and one for
5425 things the function uses. */
5427 varinfo_t clobbervi
, usevi
;
5428 const char *newname
;
5431 asprintf (&tempname
, "%s.clobber", name
);
5432 newname
= ggc_strdup (tempname
);
5435 clobbervi
= new_var_info (NULL
, newname
);
5436 clobbervi
->offset
= fi_clobbers
;
5437 clobbervi
->size
= 1;
5438 clobbervi
->fullsize
= vi
->fullsize
;
5439 clobbervi
->is_full_var
= true;
5440 clobbervi
->is_global_var
= false;
5441 gcc_assert (prev_vi
->offset
< clobbervi
->offset
);
5442 prev_vi
->next
= clobbervi
->id
;
5443 prev_vi
= clobbervi
;
5445 asprintf (&tempname
, "%s.use", name
);
5446 newname
= ggc_strdup (tempname
);
5449 usevi
= new_var_info (NULL
, newname
);
5450 usevi
->offset
= fi_uses
;
5452 usevi
->fullsize
= vi
->fullsize
;
5453 usevi
->is_full_var
= true;
5454 usevi
->is_global_var
= false;
5455 gcc_assert (prev_vi
->offset
< usevi
->offset
);
5456 prev_vi
->next
= usevi
->id
;
5460 /* And one for the static chain. */
5461 if (fn
->static_chain_decl
!= NULL_TREE
)
5464 const char *newname
;
5467 asprintf (&tempname
, "%s.chain", name
);
5468 newname
= ggc_strdup (tempname
);
5471 chainvi
= new_var_info (fn
->static_chain_decl
, newname
);
5472 chainvi
->offset
= fi_static_chain
;
5474 chainvi
->fullsize
= vi
->fullsize
;
5475 chainvi
->is_full_var
= true;
5476 chainvi
->is_global_var
= false;
5477 gcc_assert (prev_vi
->offset
< chainvi
->offset
);
5478 prev_vi
->next
= chainvi
->id
;
5480 insert_vi_for_tree (fn
->static_chain_decl
, chainvi
);
5483 /* Create a variable for the return var. */
5484 if (DECL_RESULT (decl
) != NULL
5485 || !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl
))))
5488 const char *newname
;
5490 tree resultdecl
= decl
;
5492 if (DECL_RESULT (decl
))
5493 resultdecl
= DECL_RESULT (decl
);
5495 asprintf (&tempname
, "%s.result", name
);
5496 newname
= ggc_strdup (tempname
);
5499 resultvi
= new_var_info (resultdecl
, newname
);
5500 resultvi
->offset
= fi_result
;
5502 resultvi
->fullsize
= vi
->fullsize
;
5503 resultvi
->is_full_var
= true;
5504 if (DECL_RESULT (decl
))
5505 resultvi
->may_have_pointers
= true;
5506 gcc_assert (prev_vi
->offset
< resultvi
->offset
);
5507 prev_vi
->next
= resultvi
->id
;
5509 if (DECL_RESULT (decl
))
5510 insert_vi_for_tree (DECL_RESULT (decl
), resultvi
);
5513 /* Set up variables for each argument. */
5514 arg
= DECL_ARGUMENTS (decl
);
5515 for (i
= 0; i
< num_args
; i
++)
5518 const char *newname
;
5520 tree argdecl
= decl
;
5525 asprintf (&tempname
, "%s.arg%d", name
, i
);
5526 newname
= ggc_strdup (tempname
);
5529 argvi
= new_var_info (argdecl
, newname
);
5530 argvi
->offset
= fi_parm_base
+ i
;
5532 argvi
->is_full_var
= true;
5533 argvi
->fullsize
= vi
->fullsize
;
5535 argvi
->may_have_pointers
= true;
5536 gcc_assert (prev_vi
->offset
< argvi
->offset
);
5537 prev_vi
->next
= argvi
->id
;
5541 insert_vi_for_tree (arg
, argvi
);
5542 arg
= DECL_CHAIN (arg
);
5546 /* Add one representative for all further args. */
5550 const char *newname
;
5554 asprintf (&tempname
, "%s.varargs", name
);
5555 newname
= ggc_strdup (tempname
);
5558 /* We need sth that can be pointed to for va_start. */
5559 decl
= build_fake_var_decl (ptr_type_node
);
5561 argvi
= new_var_info (decl
, newname
);
5562 argvi
->offset
= fi_parm_base
+ num_args
;
5564 argvi
->is_full_var
= true;
5565 argvi
->is_heap_var
= true;
5566 argvi
->fullsize
= vi
->fullsize
;
5567 gcc_assert (prev_vi
->offset
< argvi
->offset
);
5568 prev_vi
->next
= argvi
->id
;
5576 /* Return true if FIELDSTACK contains fields that overlap.
5577 FIELDSTACK is assumed to be sorted by offset. */
5580 check_for_overlaps (vec
<fieldoff_s
> fieldstack
)
5582 fieldoff_s
*fo
= NULL
;
5584 HOST_WIDE_INT lastoffset
= -1;
5586 FOR_EACH_VEC_ELT (fieldstack
, i
, fo
)
5588 if (fo
->offset
== lastoffset
)
5590 lastoffset
= fo
->offset
;
5595 /* Create a varinfo structure for NAME and DECL, and add it to VARMAP.
5596 This will also create any varinfo structures necessary for fields
5600 create_variable_info_for_1 (tree decl
, const char *name
)
5602 varinfo_t vi
, newvi
;
5603 tree decl_type
= TREE_TYPE (decl
);
5604 tree declsize
= DECL_P (decl
) ? DECL_SIZE (decl
) : TYPE_SIZE (decl_type
);
5605 vec
<fieldoff_s
> fieldstack
= vNULL
;
5610 || !host_integerp (declsize
, 1))
5612 vi
= new_var_info (decl
, name
);
5616 vi
->is_unknown_size_var
= true;
5617 vi
->is_full_var
= true;
5618 vi
->may_have_pointers
= true;
5622 /* Collect field information. */
5623 if (use_field_sensitive
5624 && var_can_have_subvars (decl
)
5625 /* ??? Force us to not use subfields for global initializers
5626 in IPA mode. Else we'd have to parse arbitrary initializers. */
5628 && is_global_var (decl
)
5629 && DECL_INITIAL (decl
)))
5631 fieldoff_s
*fo
= NULL
;
5632 bool notokay
= false;
5635 push_fields_onto_fieldstack (decl_type
, &fieldstack
, 0);
5637 for (i
= 0; !notokay
&& fieldstack
.iterate (i
, &fo
); i
++)
5638 if (fo
->has_unknown_size
5645 /* We can't sort them if we have a field with a variable sized type,
5646 which will make notokay = true. In that case, we are going to return
5647 without creating varinfos for the fields anyway, so sorting them is a
5651 sort_fieldstack (fieldstack
);
5652 /* Due to some C++ FE issues, like PR 22488, we might end up
5653 what appear to be overlapping fields even though they,
5654 in reality, do not overlap. Until the C++ FE is fixed,
5655 we will simply disable field-sensitivity for these cases. */
5656 notokay
= check_for_overlaps (fieldstack
);
5660 fieldstack
.release ();
5663 /* If we didn't end up collecting sub-variables create a full
5664 variable for the decl. */
5665 if (fieldstack
.length () <= 1
5666 || fieldstack
.length () > MAX_FIELDS_FOR_FIELD_SENSITIVE
)
5668 vi
= new_var_info (decl
, name
);
5670 vi
->may_have_pointers
= true;
5671 vi
->fullsize
= TREE_INT_CST_LOW (declsize
);
5672 vi
->size
= vi
->fullsize
;
5673 vi
->is_full_var
= true;
5674 fieldstack
.release ();
5678 vi
= new_var_info (decl
, name
);
5679 vi
->fullsize
= TREE_INT_CST_LOW (declsize
);
5680 for (i
= 0, newvi
= vi
;
5681 fieldstack
.iterate (i
, &fo
);
5682 ++i
, newvi
= vi_next (newvi
))
5684 const char *newname
= "NULL";
5689 asprintf (&tempname
, "%s." HOST_WIDE_INT_PRINT_DEC
5690 "+" HOST_WIDE_INT_PRINT_DEC
, name
, fo
->offset
, fo
->size
);
5691 newname
= ggc_strdup (tempname
);
5694 newvi
->name
= newname
;
5695 newvi
->offset
= fo
->offset
;
5696 newvi
->size
= fo
->size
;
5697 newvi
->fullsize
= vi
->fullsize
;
5698 newvi
->may_have_pointers
= fo
->may_have_pointers
;
5699 newvi
->only_restrict_pointers
= fo
->only_restrict_pointers
;
5700 if (i
+ 1 < fieldstack
.length ())
5702 varinfo_t tem
= new_var_info (decl
, name
);
5703 newvi
->next
= tem
->id
;
5708 fieldstack
.release ();
5714 create_variable_info_for (tree decl
, const char *name
)
5716 varinfo_t vi
= create_variable_info_for_1 (decl
, name
);
5717 unsigned int id
= vi
->id
;
5719 insert_vi_for_tree (decl
, vi
);
5721 if (TREE_CODE (decl
) != VAR_DECL
)
5724 /* Create initial constraints for globals. */
5725 for (; vi
; vi
= vi_next (vi
))
5727 if (!vi
->may_have_pointers
5728 || !vi
->is_global_var
)
5731 /* Mark global restrict qualified pointers. */
5732 if ((POINTER_TYPE_P (TREE_TYPE (decl
))
5733 && TYPE_RESTRICT (TREE_TYPE (decl
)))
5734 || vi
->only_restrict_pointers
)
5736 make_constraint_from_global_restrict (vi
, "GLOBAL_RESTRICT");
5740 /* In non-IPA mode the initializer from nonlocal is all we need. */
5742 || DECL_HARD_REGISTER (decl
))
5743 make_copy_constraint (vi
, nonlocal_id
);
5745 /* In IPA mode parse the initializer and generate proper constraints
5749 struct varpool_node
*vnode
= varpool_get_node (decl
);
5751 /* For escaped variables initialize them from nonlocal. */
5752 if (!varpool_all_refs_explicit_p (vnode
))
5753 make_copy_constraint (vi
, nonlocal_id
);
5755 /* If this is a global variable with an initializer and we are in
5756 IPA mode generate constraints for it. */
5757 if (DECL_INITIAL (decl
)
5758 && vnode
->definition
)
5760 vec
<ce_s
> rhsc
= vNULL
;
5761 struct constraint_expr lhs
, *rhsp
;
5763 get_constraint_for_rhs (DECL_INITIAL (decl
), &rhsc
);
5767 FOR_EACH_VEC_ELT (rhsc
, i
, rhsp
)
5768 process_constraint (new_constraint (lhs
, *rhsp
));
5769 /* If this is a variable that escapes from the unit
5770 the initializer escapes as well. */
5771 if (!varpool_all_refs_explicit_p (vnode
))
5773 lhs
.var
= escaped_id
;
5776 FOR_EACH_VEC_ELT (rhsc
, i
, rhsp
)
5777 process_constraint (new_constraint (lhs
, *rhsp
));
5787 /* Print out the points-to solution for VAR to FILE. */
5790 dump_solution_for_var (FILE *file
, unsigned int var
)
5792 varinfo_t vi
= get_varinfo (var
);
5796 /* Dump the solution for unified vars anyway, this avoids difficulties
5797 in scanning dumps in the testsuite. */
5798 fprintf (file
, "%s = { ", vi
->name
);
5799 vi
= get_varinfo (find (var
));
5800 EXECUTE_IF_SET_IN_BITMAP (vi
->solution
, 0, i
, bi
)
5801 fprintf (file
, "%s ", get_varinfo (i
)->name
);
5802 fprintf (file
, "}");
5804 /* But note when the variable was unified. */
5806 fprintf (file
, " same as %s", vi
->name
);
5808 fprintf (file
, "\n");
5811 /* Print the points-to solution for VAR to stdout. */
5814 debug_solution_for_var (unsigned int var
)
5816 dump_solution_for_var (stdout
, var
);
5819 /* Create varinfo structures for all of the variables in the
5820 function for intraprocedural mode. */
5823 intra_create_variable_infos (void)
5827 /* For each incoming pointer argument arg, create the constraint ARG
5828 = NONLOCAL or a dummy variable if it is a restrict qualified
5829 passed-by-reference argument. */
5830 for (t
= DECL_ARGUMENTS (current_function_decl
); t
; t
= DECL_CHAIN (t
))
5832 varinfo_t p
= get_vi_for_tree (t
);
5834 /* For restrict qualified pointers to objects passed by
5835 reference build a real representative for the pointed-to object.
5836 Treat restrict qualified references the same. */
5837 if (TYPE_RESTRICT (TREE_TYPE (t
))
5838 && ((DECL_BY_REFERENCE (t
) && POINTER_TYPE_P (TREE_TYPE (t
)))
5839 || TREE_CODE (TREE_TYPE (t
)) == REFERENCE_TYPE
)
5840 && !type_contains_placeholder_p (TREE_TYPE (TREE_TYPE (t
))))
5842 struct constraint_expr lhsc
, rhsc
;
5844 tree heapvar
= build_fake_var_decl (TREE_TYPE (TREE_TYPE (t
)));
5845 DECL_EXTERNAL (heapvar
) = 1;
5846 vi
= create_variable_info_for_1 (heapvar
, "PARM_NOALIAS");
5847 insert_vi_for_tree (heapvar
, vi
);
5852 rhsc
.type
= ADDRESSOF
;
5854 process_constraint (new_constraint (lhsc
, rhsc
));
5855 for (; vi
; vi
= vi_next (vi
))
5856 if (vi
->may_have_pointers
)
5858 if (vi
->only_restrict_pointers
)
5859 make_constraint_from_global_restrict (vi
, "GLOBAL_RESTRICT");
5861 make_copy_constraint (vi
, nonlocal_id
);
5866 if (POINTER_TYPE_P (TREE_TYPE (t
))
5867 && TYPE_RESTRICT (TREE_TYPE (t
)))
5868 make_constraint_from_global_restrict (p
, "PARM_RESTRICT");
5871 for (; p
; p
= vi_next (p
))
5873 if (p
->only_restrict_pointers
)
5874 make_constraint_from_global_restrict (p
, "PARM_RESTRICT");
5875 else if (p
->may_have_pointers
)
5876 make_constraint_from (p
, nonlocal_id
);
5881 /* Add a constraint for a result decl that is passed by reference. */
5882 if (DECL_RESULT (cfun
->decl
)
5883 && DECL_BY_REFERENCE (DECL_RESULT (cfun
->decl
)))
5885 varinfo_t p
, result_vi
= get_vi_for_tree (DECL_RESULT (cfun
->decl
));
5887 for (p
= result_vi
; p
; p
= vi_next (p
))
5888 make_constraint_from (p
, nonlocal_id
);
5891 /* Add a constraint for the incoming static chain parameter. */
5892 if (cfun
->static_chain_decl
!= NULL_TREE
)
5894 varinfo_t p
, chain_vi
= get_vi_for_tree (cfun
->static_chain_decl
);
5896 for (p
= chain_vi
; p
; p
= vi_next (p
))
5897 make_constraint_from (p
, nonlocal_id
);
5901 /* Structure used to put solution bitmaps in a hashtable so they can
5902 be shared among variables with the same points-to set. */
5904 typedef struct shared_bitmap_info
5908 } *shared_bitmap_info_t
;
5909 typedef const struct shared_bitmap_info
*const_shared_bitmap_info_t
;
5911 /* Shared_bitmap hashtable helpers. */
5913 struct shared_bitmap_hasher
: typed_free_remove
<shared_bitmap_info
>
5915 typedef shared_bitmap_info value_type
;
5916 typedef shared_bitmap_info compare_type
;
5917 static inline hashval_t
hash (const value_type
*);
5918 static inline bool equal (const value_type
*, const compare_type
*);
5921 /* Hash function for a shared_bitmap_info_t */
5924 shared_bitmap_hasher::hash (const value_type
*bi
)
5926 return bi
->hashcode
;
5929 /* Equality function for two shared_bitmap_info_t's. */
5932 shared_bitmap_hasher::equal (const value_type
*sbi1
, const compare_type
*sbi2
)
5934 return bitmap_equal_p (sbi1
->pt_vars
, sbi2
->pt_vars
);
5937 /* Shared_bitmap hashtable. */
5939 static hash_table
<shared_bitmap_hasher
> shared_bitmap_table
;
5941 /* Lookup a bitmap in the shared bitmap hashtable, and return an already
5942 existing instance if there is one, NULL otherwise. */
5945 shared_bitmap_lookup (bitmap pt_vars
)
5947 shared_bitmap_info
**slot
;
5948 struct shared_bitmap_info sbi
;
5950 sbi
.pt_vars
= pt_vars
;
5951 sbi
.hashcode
= bitmap_hash (pt_vars
);
5953 slot
= shared_bitmap_table
.find_slot_with_hash (&sbi
, sbi
.hashcode
,
5958 return (*slot
)->pt_vars
;
5962 /* Add a bitmap to the shared bitmap hashtable. */
5965 shared_bitmap_add (bitmap pt_vars
)
5967 shared_bitmap_info
**slot
;
5968 shared_bitmap_info_t sbi
= XNEW (struct shared_bitmap_info
);
5970 sbi
->pt_vars
= pt_vars
;
5971 sbi
->hashcode
= bitmap_hash (pt_vars
);
5973 slot
= shared_bitmap_table
.find_slot_with_hash (sbi
, sbi
->hashcode
, INSERT
);
5974 gcc_assert (!*slot
);
5979 /* Set bits in INTO corresponding to the variable uids in solution set FROM. */
5982 set_uids_in_ptset (bitmap into
, bitmap from
, struct pt_solution
*pt
)
5987 EXECUTE_IF_SET_IN_BITMAP (from
, 0, i
, bi
)
5989 varinfo_t vi
= get_varinfo (i
);
5991 /* The only artificial variables that are allowed in a may-alias
5992 set are heap variables. */
5993 if (vi
->is_artificial_var
&& !vi
->is_heap_var
)
5996 if (TREE_CODE (vi
->decl
) == VAR_DECL
5997 || TREE_CODE (vi
->decl
) == PARM_DECL
5998 || TREE_CODE (vi
->decl
) == RESULT_DECL
)
6000 /* If we are in IPA mode we will not recompute points-to
6001 sets after inlining so make sure they stay valid. */
6003 && !DECL_PT_UID_SET_P (vi
->decl
))
6004 SET_DECL_PT_UID (vi
->decl
, DECL_UID (vi
->decl
));
6006 /* Add the decl to the points-to set. Note that the points-to
6007 set contains global variables. */
6008 bitmap_set_bit (into
, DECL_PT_UID (vi
->decl
));
6009 if (vi
->is_global_var
)
6010 pt
->vars_contains_global
= true;
6016 /* Compute the points-to solution *PT for the variable VI. */
6018 static struct pt_solution
6019 find_what_var_points_to (varinfo_t orig_vi
)
6023 bitmap finished_solution
;
6027 struct pt_solution
*pt
;
6029 /* This variable may have been collapsed, let's get the real
6031 vi
= get_varinfo (find (orig_vi
->id
));
6033 /* See if we have already computed the solution and return it. */
6034 slot
= pointer_map_insert (final_solutions
, vi
);
6036 return *(struct pt_solution
*)*slot
;
6038 *slot
= pt
= XOBNEW (&final_solutions_obstack
, struct pt_solution
);
6039 memset (pt
, 0, sizeof (struct pt_solution
));
6041 /* Translate artificial variables into SSA_NAME_PTR_INFO
6043 EXECUTE_IF_SET_IN_BITMAP (vi
->solution
, 0, i
, bi
)
6045 varinfo_t vi
= get_varinfo (i
);
6047 if (vi
->is_artificial_var
)
6049 if (vi
->id
== nothing_id
)
6051 else if (vi
->id
== escaped_id
)
6054 pt
->ipa_escaped
= 1;
6058 else if (vi
->id
== nonlocal_id
)
6060 else if (vi
->is_heap_var
)
6061 /* We represent heapvars in the points-to set properly. */
6063 else if (vi
->id
== readonly_id
)
6066 else if (vi
->id
== anything_id
6067 || vi
->id
== integer_id
)
6072 /* Instead of doing extra work, simply do not create
6073 elaborate points-to information for pt_anything pointers. */
6077 /* Share the final set of variables when possible. */
6078 finished_solution
= BITMAP_GGC_ALLOC ();
6079 stats
.points_to_sets_created
++;
6081 set_uids_in_ptset (finished_solution
, vi
->solution
, pt
);
6082 result
= shared_bitmap_lookup (finished_solution
);
6085 shared_bitmap_add (finished_solution
);
6086 pt
->vars
= finished_solution
;
6091 bitmap_clear (finished_solution
);
6097 /* Given a pointer variable P, fill in its points-to set. */
6100 find_what_p_points_to (tree p
)
6102 struct ptr_info_def
*pi
;
6106 /* For parameters, get at the points-to set for the actual parm
6108 if (TREE_CODE (p
) == SSA_NAME
6109 && SSA_NAME_IS_DEFAULT_DEF (p
)
6110 && (TREE_CODE (SSA_NAME_VAR (p
)) == PARM_DECL
6111 || TREE_CODE (SSA_NAME_VAR (p
)) == RESULT_DECL
))
6112 lookup_p
= SSA_NAME_VAR (p
);
6114 vi
= lookup_vi_for_tree (lookup_p
);
6118 pi
= get_ptr_info (p
);
6119 pi
->pt
= find_what_var_points_to (vi
);
6123 /* Query statistics for points-to solutions. */
6126 unsigned HOST_WIDE_INT pt_solution_includes_may_alias
;
6127 unsigned HOST_WIDE_INT pt_solution_includes_no_alias
;
6128 unsigned HOST_WIDE_INT pt_solutions_intersect_may_alias
;
6129 unsigned HOST_WIDE_INT pt_solutions_intersect_no_alias
;
6133 dump_pta_stats (FILE *s
)
6135 fprintf (s
, "\nPTA query stats:\n");
6136 fprintf (s
, " pt_solution_includes: "
6137 HOST_WIDE_INT_PRINT_DEC
" disambiguations, "
6138 HOST_WIDE_INT_PRINT_DEC
" queries\n",
6139 pta_stats
.pt_solution_includes_no_alias
,
6140 pta_stats
.pt_solution_includes_no_alias
6141 + pta_stats
.pt_solution_includes_may_alias
);
6142 fprintf (s
, " pt_solutions_intersect: "
6143 HOST_WIDE_INT_PRINT_DEC
" disambiguations, "
6144 HOST_WIDE_INT_PRINT_DEC
" queries\n",
6145 pta_stats
.pt_solutions_intersect_no_alias
,
6146 pta_stats
.pt_solutions_intersect_no_alias
6147 + pta_stats
.pt_solutions_intersect_may_alias
);
6151 /* Reset the points-to solution *PT to a conservative default
6152 (point to anything). */
6155 pt_solution_reset (struct pt_solution
*pt
)
6157 memset (pt
, 0, sizeof (struct pt_solution
));
6158 pt
->anything
= true;
6161 /* Set the points-to solution *PT to point only to the variables
6162 in VARS. VARS_CONTAINS_GLOBAL specifies whether that contains
6163 global variables and VARS_CONTAINS_RESTRICT specifies whether
6164 it contains restrict tag variables. */
6167 pt_solution_set (struct pt_solution
*pt
, bitmap vars
, bool vars_contains_global
)
6169 memset (pt
, 0, sizeof (struct pt_solution
));
6171 pt
->vars_contains_global
= vars_contains_global
;
6174 /* Set the points-to solution *PT to point only to the variable VAR. */
6177 pt_solution_set_var (struct pt_solution
*pt
, tree var
)
6179 memset (pt
, 0, sizeof (struct pt_solution
));
6180 pt
->vars
= BITMAP_GGC_ALLOC ();
6181 bitmap_set_bit (pt
->vars
, DECL_PT_UID (var
));
6182 pt
->vars_contains_global
= is_global_var (var
);
6185 /* Computes the union of the points-to solutions *DEST and *SRC and
6186 stores the result in *DEST. This changes the points-to bitmap
6187 of *DEST and thus may not be used if that might be shared.
6188 The points-to bitmap of *SRC and *DEST will not be shared after
6189 this function if they were not before. */
6192 pt_solution_ior_into (struct pt_solution
*dest
, struct pt_solution
*src
)
6194 dest
->anything
|= src
->anything
;
6197 pt_solution_reset (dest
);
6201 dest
->nonlocal
|= src
->nonlocal
;
6202 dest
->escaped
|= src
->escaped
;
6203 dest
->ipa_escaped
|= src
->ipa_escaped
;
6204 dest
->null
|= src
->null
;
6205 dest
->vars_contains_global
|= src
->vars_contains_global
;
6210 dest
->vars
= BITMAP_GGC_ALLOC ();
6211 bitmap_ior_into (dest
->vars
, src
->vars
);
6214 /* Return true if the points-to solution *PT is empty. */
6217 pt_solution_empty_p (struct pt_solution
*pt
)
6224 && !bitmap_empty_p (pt
->vars
))
6227 /* If the solution includes ESCAPED, check if that is empty. */
6229 && !pt_solution_empty_p (&cfun
->gimple_df
->escaped
))
6232 /* If the solution includes ESCAPED, check if that is empty. */
6234 && !pt_solution_empty_p (&ipa_escaped_pt
))
6240 /* Return true if the points-to solution *PT only point to a single var, and
6241 return the var uid in *UID. */
6244 pt_solution_singleton_p (struct pt_solution
*pt
, unsigned *uid
)
6246 if (pt
->anything
|| pt
->nonlocal
|| pt
->escaped
|| pt
->ipa_escaped
6247 || pt
->null
|| pt
->vars
== NULL
6248 || !bitmap_single_bit_set_p (pt
->vars
))
6251 *uid
= bitmap_first_set_bit (pt
->vars
);
6255 /* Return true if the points-to solution *PT includes global memory. */
6258 pt_solution_includes_global (struct pt_solution
*pt
)
6262 || pt
->vars_contains_global
)
6266 return pt_solution_includes_global (&cfun
->gimple_df
->escaped
);
6268 if (pt
->ipa_escaped
)
6269 return pt_solution_includes_global (&ipa_escaped_pt
);
6271 /* ??? This predicate is not correct for the IPA-PTA solution
6272 as we do not properly distinguish between unit escape points
6273 and global variables. */
6274 if (cfun
->gimple_df
->ipa_pta
)
6280 /* Return true if the points-to solution *PT includes the variable
6281 declaration DECL. */
6284 pt_solution_includes_1 (struct pt_solution
*pt
, const_tree decl
)
6290 && is_global_var (decl
))
6294 && bitmap_bit_p (pt
->vars
, DECL_PT_UID (decl
)))
6297 /* If the solution includes ESCAPED, check it. */
6299 && pt_solution_includes_1 (&cfun
->gimple_df
->escaped
, decl
))
6302 /* If the solution includes ESCAPED, check it. */
6304 && pt_solution_includes_1 (&ipa_escaped_pt
, decl
))
6311 pt_solution_includes (struct pt_solution
*pt
, const_tree decl
)
6313 bool res
= pt_solution_includes_1 (pt
, decl
);
6315 ++pta_stats
.pt_solution_includes_may_alias
;
6317 ++pta_stats
.pt_solution_includes_no_alias
;
6321 /* Return true if both points-to solutions PT1 and PT2 have a non-empty
6325 pt_solutions_intersect_1 (struct pt_solution
*pt1
, struct pt_solution
*pt2
)
6327 if (pt1
->anything
|| pt2
->anything
)
6330 /* If either points to unknown global memory and the other points to
6331 any global memory they alias. */
6334 || pt2
->vars_contains_global
))
6336 && pt1
->vars_contains_global
))
6339 /* Check the escaped solution if required. */
6340 if ((pt1
->escaped
|| pt2
->escaped
)
6341 && !pt_solution_empty_p (&cfun
->gimple_df
->escaped
))
6343 /* If both point to escaped memory and that solution
6344 is not empty they alias. */
6345 if (pt1
->escaped
&& pt2
->escaped
)
6348 /* If either points to escaped memory see if the escaped solution
6349 intersects with the other. */
6351 && pt_solutions_intersect_1 (&cfun
->gimple_df
->escaped
, pt2
))
6353 && pt_solutions_intersect_1 (&cfun
->gimple_df
->escaped
, pt1
)))
6357 /* Check the escaped solution if required.
6358 ??? Do we need to check the local against the IPA escaped sets? */
6359 if ((pt1
->ipa_escaped
|| pt2
->ipa_escaped
)
6360 && !pt_solution_empty_p (&ipa_escaped_pt
))
6362 /* If both point to escaped memory and that solution
6363 is not empty they alias. */
6364 if (pt1
->ipa_escaped
&& pt2
->ipa_escaped
)
6367 /* If either points to escaped memory see if the escaped solution
6368 intersects with the other. */
6369 if ((pt1
->ipa_escaped
6370 && pt_solutions_intersect_1 (&ipa_escaped_pt
, pt2
))
6371 || (pt2
->ipa_escaped
6372 && pt_solutions_intersect_1 (&ipa_escaped_pt
, pt1
)))
6376 /* Now both pointers alias if their points-to solution intersects. */
6379 && bitmap_intersect_p (pt1
->vars
, pt2
->vars
));
6383 pt_solutions_intersect (struct pt_solution
*pt1
, struct pt_solution
*pt2
)
6385 bool res
= pt_solutions_intersect_1 (pt1
, pt2
);
6387 ++pta_stats
.pt_solutions_intersect_may_alias
;
6389 ++pta_stats
.pt_solutions_intersect_no_alias
;
6394 /* Dump points-to information to OUTFILE. */
6397 dump_sa_points_to_info (FILE *outfile
)
6401 fprintf (outfile
, "\nPoints-to sets\n\n");
6403 if (dump_flags
& TDF_STATS
)
6405 fprintf (outfile
, "Stats:\n");
6406 fprintf (outfile
, "Total vars: %d\n", stats
.total_vars
);
6407 fprintf (outfile
, "Non-pointer vars: %d\n",
6408 stats
.nonpointer_vars
);
6409 fprintf (outfile
, "Statically unified vars: %d\n",
6410 stats
.unified_vars_static
);
6411 fprintf (outfile
, "Dynamically unified vars: %d\n",
6412 stats
.unified_vars_dynamic
);
6413 fprintf (outfile
, "Iterations: %d\n", stats
.iterations
);
6414 fprintf (outfile
, "Number of edges: %d\n", stats
.num_edges
);
6415 fprintf (outfile
, "Number of implicit edges: %d\n",
6416 stats
.num_implicit_edges
);
6419 for (i
= 1; i
< varmap
.length (); i
++)
6421 varinfo_t vi
= get_varinfo (i
);
6422 if (!vi
->may_have_pointers
)
6424 dump_solution_for_var (outfile
, i
);
6429 /* Debug points-to information to stderr. */
6432 debug_sa_points_to_info (void)
6434 dump_sa_points_to_info (stderr
);
6438 /* Initialize the always-existing constraint variables for NULL
6439 ANYTHING, READONLY, and INTEGER */
6442 init_base_vars (void)
6444 struct constraint_expr lhs
, rhs
;
6445 varinfo_t var_anything
;
6446 varinfo_t var_nothing
;
6447 varinfo_t var_readonly
;
6448 varinfo_t var_escaped
;
6449 varinfo_t var_nonlocal
;
6450 varinfo_t var_storedanything
;
6451 varinfo_t var_integer
;
6453 /* Variable ID zero is reserved and should be NULL. */
6454 varmap
.safe_push (NULL
);
6456 /* Create the NULL variable, used to represent that a variable points
6458 var_nothing
= new_var_info (NULL_TREE
, "NULL");
6459 gcc_assert (var_nothing
->id
== nothing_id
);
6460 var_nothing
->is_artificial_var
= 1;
6461 var_nothing
->offset
= 0;
6462 var_nothing
->size
= ~0;
6463 var_nothing
->fullsize
= ~0;
6464 var_nothing
->is_special_var
= 1;
6465 var_nothing
->may_have_pointers
= 0;
6466 var_nothing
->is_global_var
= 0;
6468 /* Create the ANYTHING variable, used to represent that a variable
6469 points to some unknown piece of memory. */
6470 var_anything
= new_var_info (NULL_TREE
, "ANYTHING");
6471 gcc_assert (var_anything
->id
== anything_id
);
6472 var_anything
->is_artificial_var
= 1;
6473 var_anything
->size
= ~0;
6474 var_anything
->offset
= 0;
6475 var_anything
->fullsize
= ~0;
6476 var_anything
->is_special_var
= 1;
6478 /* Anything points to anything. This makes deref constraints just
6479 work in the presence of linked list and other p = *p type loops,
6480 by saying that *ANYTHING = ANYTHING. */
6482 lhs
.var
= anything_id
;
6484 rhs
.type
= ADDRESSOF
;
6485 rhs
.var
= anything_id
;
6488 /* This specifically does not use process_constraint because
6489 process_constraint ignores all anything = anything constraints, since all
6490 but this one are redundant. */
6491 constraints
.safe_push (new_constraint (lhs
, rhs
));
6493 /* Create the READONLY variable, used to represent that a variable
6494 points to readonly memory. */
6495 var_readonly
= new_var_info (NULL_TREE
, "READONLY");
6496 gcc_assert (var_readonly
->id
== readonly_id
);
6497 var_readonly
->is_artificial_var
= 1;
6498 var_readonly
->offset
= 0;
6499 var_readonly
->size
= ~0;
6500 var_readonly
->fullsize
= ~0;
6501 var_readonly
->is_special_var
= 1;
6503 /* readonly memory points to anything, in order to make deref
6504 easier. In reality, it points to anything the particular
6505 readonly variable can point to, but we don't track this
6508 lhs
.var
= readonly_id
;
6510 rhs
.type
= ADDRESSOF
;
6511 rhs
.var
= readonly_id
; /* FIXME */
6513 process_constraint (new_constraint (lhs
, rhs
));
6515 /* Create the ESCAPED variable, used to represent the set of escaped
6517 var_escaped
= new_var_info (NULL_TREE
, "ESCAPED");
6518 gcc_assert (var_escaped
->id
== escaped_id
);
6519 var_escaped
->is_artificial_var
= 1;
6520 var_escaped
->offset
= 0;
6521 var_escaped
->size
= ~0;
6522 var_escaped
->fullsize
= ~0;
6523 var_escaped
->is_special_var
= 0;
6525 /* Create the NONLOCAL variable, used to represent the set of nonlocal
6527 var_nonlocal
= new_var_info (NULL_TREE
, "NONLOCAL");
6528 gcc_assert (var_nonlocal
->id
== nonlocal_id
);
6529 var_nonlocal
->is_artificial_var
= 1;
6530 var_nonlocal
->offset
= 0;
6531 var_nonlocal
->size
= ~0;
6532 var_nonlocal
->fullsize
= ~0;
6533 var_nonlocal
->is_special_var
= 1;
6535 /* ESCAPED = *ESCAPED, because escaped is may-deref'd at calls, etc. */
6537 lhs
.var
= escaped_id
;
6540 rhs
.var
= escaped_id
;
6542 process_constraint (new_constraint (lhs
, rhs
));
6544 /* ESCAPED = ESCAPED + UNKNOWN_OFFSET, because if a sub-field escapes the
6545 whole variable escapes. */
6547 lhs
.var
= escaped_id
;
6550 rhs
.var
= escaped_id
;
6551 rhs
.offset
= UNKNOWN_OFFSET
;
6552 process_constraint (new_constraint (lhs
, rhs
));
6554 /* *ESCAPED = NONLOCAL. This is true because we have to assume
6555 everything pointed to by escaped points to what global memory can
6558 lhs
.var
= escaped_id
;
6561 rhs
.var
= nonlocal_id
;
6563 process_constraint (new_constraint (lhs
, rhs
));
6565 /* NONLOCAL = &NONLOCAL, NONLOCAL = &ESCAPED. This is true because
6566 global memory may point to global memory and escaped memory. */
6568 lhs
.var
= nonlocal_id
;
6570 rhs
.type
= ADDRESSOF
;
6571 rhs
.var
= nonlocal_id
;
6573 process_constraint (new_constraint (lhs
, rhs
));
6574 rhs
.type
= ADDRESSOF
;
6575 rhs
.var
= escaped_id
;
6577 process_constraint (new_constraint (lhs
, rhs
));
6579 /* Create the STOREDANYTHING variable, used to represent the set of
6580 variables stored to *ANYTHING. */
6581 var_storedanything
= new_var_info (NULL_TREE
, "STOREDANYTHING");
6582 gcc_assert (var_storedanything
->id
== storedanything_id
);
6583 var_storedanything
->is_artificial_var
= 1;
6584 var_storedanything
->offset
= 0;
6585 var_storedanything
->size
= ~0;
6586 var_storedanything
->fullsize
= ~0;
6587 var_storedanything
->is_special_var
= 0;
6589 /* Create the INTEGER variable, used to represent that a variable points
6590 to what an INTEGER "points to". */
6591 var_integer
= new_var_info (NULL_TREE
, "INTEGER");
6592 gcc_assert (var_integer
->id
== integer_id
);
6593 var_integer
->is_artificial_var
= 1;
6594 var_integer
->size
= ~0;
6595 var_integer
->fullsize
= ~0;
6596 var_integer
->offset
= 0;
6597 var_integer
->is_special_var
= 1;
6599 /* INTEGER = ANYTHING, because we don't know where a dereference of
6600 a random integer will point to. */
6602 lhs
.var
= integer_id
;
6604 rhs
.type
= ADDRESSOF
;
6605 rhs
.var
= anything_id
;
6607 process_constraint (new_constraint (lhs
, rhs
));
6610 /* Initialize things necessary to perform PTA */
6613 init_alias_vars (void)
6615 use_field_sensitive
= (MAX_FIELDS_FOR_FIELD_SENSITIVE
> 1);
6617 bitmap_obstack_initialize (&pta_obstack
);
6618 bitmap_obstack_initialize (&oldpta_obstack
);
6619 bitmap_obstack_initialize (&predbitmap_obstack
);
6621 constraint_pool
= create_alloc_pool ("Constraint pool",
6622 sizeof (struct constraint
), 30);
6623 variable_info_pool
= create_alloc_pool ("Variable info pool",
6624 sizeof (struct variable_info
), 30);
6625 constraints
.create (8);
6627 vi_for_tree
= pointer_map_create ();
6628 call_stmt_vars
= pointer_map_create ();
6630 memset (&stats
, 0, sizeof (stats
));
6631 shared_bitmap_table
.create (511);
6634 gcc_obstack_init (&fake_var_decl_obstack
);
6636 final_solutions
= pointer_map_create ();
6637 gcc_obstack_init (&final_solutions_obstack
);
6640 /* Remove the REF and ADDRESS edges from GRAPH, as well as all the
6641 predecessor edges. */
6644 remove_preds_and_fake_succs (constraint_graph_t graph
)
6648 /* Clear the implicit ref and address nodes from the successor
6650 for (i
= 1; i
< FIRST_REF_NODE
; i
++)
6652 if (graph
->succs
[i
])
6653 bitmap_clear_range (graph
->succs
[i
], FIRST_REF_NODE
,
6654 FIRST_REF_NODE
* 2);
6657 /* Free the successor list for the non-ref nodes. */
6658 for (i
= FIRST_REF_NODE
+ 1; i
< graph
->size
; i
++)
6660 if (graph
->succs
[i
])
6661 BITMAP_FREE (graph
->succs
[i
]);
6664 /* Now reallocate the size of the successor list as, and blow away
6665 the predecessor bitmaps. */
6666 graph
->size
= varmap
.length ();
6667 graph
->succs
= XRESIZEVEC (bitmap
, graph
->succs
, graph
->size
);
6669 free (graph
->implicit_preds
);
6670 graph
->implicit_preds
= NULL
;
6671 free (graph
->preds
);
6672 graph
->preds
= NULL
;
6673 bitmap_obstack_release (&predbitmap_obstack
);
6676 /* Solve the constraint set. */
6679 solve_constraints (void)
6681 struct scc_info
*si
;
6685 "\nCollapsing static cycles and doing variable "
6688 init_graph (varmap
.length () * 2);
6691 fprintf (dump_file
, "Building predecessor graph\n");
6692 build_pred_graph ();
6695 fprintf (dump_file
, "Detecting pointer and location "
6697 si
= perform_var_substitution (graph
);
6700 fprintf (dump_file
, "Rewriting constraints and unifying "
6702 rewrite_constraints (graph
, si
);
6704 build_succ_graph ();
6706 free_var_substitution_info (si
);
6708 /* Attach complex constraints to graph nodes. */
6709 move_complex_constraints (graph
);
6712 fprintf (dump_file
, "Uniting pointer but not location equivalent "
6714 unite_pointer_equivalences (graph
);
6717 fprintf (dump_file
, "Finding indirect cycles\n");
6718 find_indirect_cycles (graph
);
6720 /* Implicit nodes and predecessors are no longer necessary at this
6722 remove_preds_and_fake_succs (graph
);
6724 if (dump_file
&& (dump_flags
& TDF_GRAPH
))
6726 fprintf (dump_file
, "\n\n// The constraint graph before solve-graph "
6727 "in dot format:\n");
6728 dump_constraint_graph (dump_file
);
6729 fprintf (dump_file
, "\n\n");
6733 fprintf (dump_file
, "Solving graph\n");
6735 solve_graph (graph
);
6737 if (dump_file
&& (dump_flags
& TDF_GRAPH
))
6739 fprintf (dump_file
, "\n\n// The constraint graph after solve-graph "
6740 "in dot format:\n");
6741 dump_constraint_graph (dump_file
);
6742 fprintf (dump_file
, "\n\n");
6746 dump_sa_points_to_info (dump_file
);
6749 /* Create points-to sets for the current function. See the comments
6750 at the start of the file for an algorithmic overview. */
6753 compute_points_to_sets (void)
6759 timevar_push (TV_TREE_PTA
);
6763 intra_create_variable_infos ();
6765 /* Now walk all statements and build the constraint set. */
6768 gimple_stmt_iterator gsi
;
6770 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
6772 gimple phi
= gsi_stmt (gsi
);
6774 if (! virtual_operand_p (gimple_phi_result (phi
)))
6775 find_func_aliases (phi
);
6778 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
6780 gimple stmt
= gsi_stmt (gsi
);
6782 find_func_aliases (stmt
);
6788 fprintf (dump_file
, "Points-to analysis\n\nConstraints:\n\n");
6789 dump_constraints (dump_file
, 0);
6792 /* From the constraints compute the points-to sets. */
6793 solve_constraints ();
6795 /* Compute the points-to set for ESCAPED used for call-clobber analysis. */
6796 cfun
->gimple_df
->escaped
= find_what_var_points_to (get_varinfo (escaped_id
));
6798 /* Make sure the ESCAPED solution (which is used as placeholder in
6799 other solutions) does not reference itself. This simplifies
6800 points-to solution queries. */
6801 cfun
->gimple_df
->escaped
.escaped
= 0;
6803 /* Mark escaped HEAP variables as global. */
6804 FOR_EACH_VEC_ELT (varmap
, i
, vi
)
6807 && !vi
->is_global_var
)
6808 DECL_EXTERNAL (vi
->decl
) = vi
->is_global_var
6809 = pt_solution_includes (&cfun
->gimple_df
->escaped
, vi
->decl
);
6811 /* Compute the points-to sets for pointer SSA_NAMEs. */
6812 for (i
= 0; i
< num_ssa_names
; ++i
)
6814 tree ptr
= ssa_name (i
);
6816 && POINTER_TYPE_P (TREE_TYPE (ptr
)))
6817 find_what_p_points_to (ptr
);
6820 /* Compute the call-used/clobbered sets. */
6823 gimple_stmt_iterator gsi
;
6825 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
6827 gimple stmt
= gsi_stmt (gsi
);
6828 struct pt_solution
*pt
;
6829 if (!is_gimple_call (stmt
))
6832 pt
= gimple_call_use_set (stmt
);
6833 if (gimple_call_flags (stmt
) & ECF_CONST
)
6834 memset (pt
, 0, sizeof (struct pt_solution
));
6835 else if ((vi
= lookup_call_use_vi (stmt
)) != NULL
)
6837 *pt
= find_what_var_points_to (vi
);
6838 /* Escaped (and thus nonlocal) variables are always
6839 implicitly used by calls. */
6840 /* ??? ESCAPED can be empty even though NONLOCAL
6847 /* If there is nothing special about this call then
6848 we have made everything that is used also escape. */
6849 *pt
= cfun
->gimple_df
->escaped
;
6853 pt
= gimple_call_clobber_set (stmt
);
6854 if (gimple_call_flags (stmt
) & (ECF_CONST
|ECF_PURE
|ECF_NOVOPS
))
6855 memset (pt
, 0, sizeof (struct pt_solution
));
6856 else if ((vi
= lookup_call_clobber_vi (stmt
)) != NULL
)
6858 *pt
= find_what_var_points_to (vi
);
6859 /* Escaped (and thus nonlocal) variables are always
6860 implicitly clobbered by calls. */
6861 /* ??? ESCAPED can be empty even though NONLOCAL
6868 /* If there is nothing special about this call then
6869 we have made everything that is used also escape. */
6870 *pt
= cfun
->gimple_df
->escaped
;
6876 timevar_pop (TV_TREE_PTA
);
6880 /* Delete created points-to sets. */
6883 delete_points_to_sets (void)
6887 shared_bitmap_table
.dispose ();
6888 if (dump_file
&& (dump_flags
& TDF_STATS
))
6889 fprintf (dump_file
, "Points to sets created:%d\n",
6890 stats
.points_to_sets_created
);
6892 pointer_map_destroy (vi_for_tree
);
6893 pointer_map_destroy (call_stmt_vars
);
6894 bitmap_obstack_release (&pta_obstack
);
6895 constraints
.release ();
6897 for (i
= 0; i
< graph
->size
; i
++)
6898 graph
->complex[i
].release ();
6899 free (graph
->complex);
6902 free (graph
->succs
);
6904 free (graph
->pe_rep
);
6905 free (graph
->indirect_cycles
);
6909 free_alloc_pool (variable_info_pool
);
6910 free_alloc_pool (constraint_pool
);
6912 obstack_free (&fake_var_decl_obstack
, NULL
);
6914 pointer_map_destroy (final_solutions
);
6915 obstack_free (&final_solutions_obstack
, NULL
);
6919 /* Compute points-to information for every SSA_NAME pointer in the
6920 current function and compute the transitive closure of escaped
6921 variables to re-initialize the call-clobber states of local variables. */
6924 compute_may_aliases (void)
6926 if (cfun
->gimple_df
->ipa_pta
)
6930 fprintf (dump_file
, "\nNot re-computing points-to information "
6931 "because IPA points-to information is available.\n\n");
6933 /* But still dump what we have remaining it. */
6934 dump_alias_info (dump_file
);
6940 /* For each pointer P_i, determine the sets of variables that P_i may
6941 point-to. Compute the reachability set of escaped and call-used
6943 compute_points_to_sets ();
6945 /* Debugging dumps. */
6947 dump_alias_info (dump_file
);
6949 /* Deallocate memory used by aliasing data structures and the internal
6950 points-to solution. */
6951 delete_points_to_sets ();
6953 gcc_assert (!need_ssa_update_p (cfun
));
6959 gate_tree_pta (void)
6961 return flag_tree_pta
;
6964 /* A dummy pass to cause points-to information to be computed via
6965 TODO_rebuild_alias. */
6969 const pass_data pass_data_build_alias
=
6971 GIMPLE_PASS
, /* type */
6973 OPTGROUP_NONE
, /* optinfo_flags */
6974 true, /* has_gate */
6975 false, /* has_execute */
6976 TV_NONE
, /* tv_id */
6977 ( PROP_cfg
| PROP_ssa
), /* properties_required */
6978 0, /* properties_provided */
6979 0, /* properties_destroyed */
6980 0, /* todo_flags_start */
6981 TODO_rebuild_alias
, /* todo_flags_finish */
6984 class pass_build_alias
: public gimple_opt_pass
6987 pass_build_alias (gcc::context
*ctxt
)
6988 : gimple_opt_pass (pass_data_build_alias
, ctxt
)
6991 /* opt_pass methods: */
6992 bool gate () { return gate_tree_pta (); }
6994 }; // class pass_build_alias
6999 make_pass_build_alias (gcc::context
*ctxt
)
7001 return new pass_build_alias (ctxt
);
7004 /* A dummy pass to cause points-to information to be computed via
7005 TODO_rebuild_alias. */
7009 const pass_data pass_data_build_ealias
=
7011 GIMPLE_PASS
, /* type */
7012 "ealias", /* name */
7013 OPTGROUP_NONE
, /* optinfo_flags */
7014 true, /* has_gate */
7015 false, /* has_execute */
7016 TV_NONE
, /* tv_id */
7017 ( PROP_cfg
| PROP_ssa
), /* properties_required */
7018 0, /* properties_provided */
7019 0, /* properties_destroyed */
7020 0, /* todo_flags_start */
7021 TODO_rebuild_alias
, /* todo_flags_finish */
7024 class pass_build_ealias
: public gimple_opt_pass
7027 pass_build_ealias (gcc::context
*ctxt
)
7028 : gimple_opt_pass (pass_data_build_ealias
, ctxt
)
7031 /* opt_pass methods: */
7032 bool gate () { return gate_tree_pta (); }
7034 }; // class pass_build_ealias
7039 make_pass_build_ealias (gcc::context
*ctxt
)
7041 return new pass_build_ealias (ctxt
);
7045 /* Return true if we should execute IPA PTA. */
7051 /* Don't bother doing anything if the program has errors. */
7055 /* IPA PTA solutions for ESCAPED. */
7056 struct pt_solution ipa_escaped_pt
7057 = { true, false, false, false, false, false, NULL
};
7059 /* Associate node with varinfo DATA. Worker for
7060 cgraph_for_node_and_aliases. */
7062 associate_varinfo_to_alias (struct cgraph_node
*node
, void *data
)
7064 if ((node
->alias
|| node
->thunk
.thunk_p
)
7066 insert_vi_for_tree (node
->decl
, (varinfo_t
)data
);
7070 /* Execute the driver for IPA PTA. */
7072 ipa_pta_execute (void)
7074 struct cgraph_node
*node
;
7075 struct varpool_node
*var
;
7082 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7084 dump_symtab (dump_file
);
7085 fprintf (dump_file
, "\n");
7088 /* Build the constraints. */
7089 FOR_EACH_DEFINED_FUNCTION (node
)
7092 /* Nodes without a body are not interesting. Especially do not
7093 visit clones at this point for now - we get duplicate decls
7094 there for inline clones at least. */
7095 if (!cgraph_function_with_gimple_body_p (node
) || node
->clone_of
)
7097 cgraph_get_body (node
);
7099 gcc_assert (!node
->clone_of
);
7101 vi
= create_function_info_for (node
->decl
,
7102 alias_get_name (node
->decl
));
7103 cgraph_for_node_and_aliases (node
, associate_varinfo_to_alias
, vi
, true);
7106 /* Create constraints for global variables and their initializers. */
7107 FOR_EACH_VARIABLE (var
)
7109 if (var
->alias
&& var
->analyzed
)
7112 get_vi_for_tree (var
->decl
);
7118 "Generating constraints for global initializers\n\n");
7119 dump_constraints (dump_file
, 0);
7120 fprintf (dump_file
, "\n");
7122 from
= constraints
.length ();
7124 FOR_EACH_DEFINED_FUNCTION (node
)
7126 struct function
*func
;
7129 /* Nodes without a body are not interesting. */
7130 if (!cgraph_function_with_gimple_body_p (node
) || node
->clone_of
)
7136 "Generating constraints for %s", cgraph_node_name (node
));
7137 if (DECL_ASSEMBLER_NAME_SET_P (node
->decl
))
7138 fprintf (dump_file
, " (%s)",
7140 (DECL_ASSEMBLER_NAME (node
->decl
)));
7141 fprintf (dump_file
, "\n");
7144 func
= DECL_STRUCT_FUNCTION (node
->decl
);
7147 /* For externally visible or attribute used annotated functions use
7148 local constraints for their arguments.
7149 For local functions we see all callers and thus do not need initial
7150 constraints for parameters. */
7151 if (node
->used_from_other_partition
7152 || node
->externally_visible
7153 || node
->force_output
)
7155 intra_create_variable_infos ();
7157 /* We also need to make function return values escape. Nothing
7158 escapes by returning from main though. */
7159 if (!MAIN_NAME_P (DECL_NAME (node
->decl
)))
7162 fi
= lookup_vi_for_tree (node
->decl
);
7163 rvi
= first_vi_for_offset (fi
, fi_result
);
7164 if (rvi
&& rvi
->offset
== fi_result
)
7166 struct constraint_expr includes
;
7167 struct constraint_expr var
;
7168 includes
.var
= escaped_id
;
7169 includes
.offset
= 0;
7170 includes
.type
= SCALAR
;
7174 process_constraint (new_constraint (includes
, var
));
7179 /* Build constriants for the function body. */
7180 FOR_EACH_BB_FN (bb
, func
)
7182 gimple_stmt_iterator gsi
;
7184 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
);
7187 gimple phi
= gsi_stmt (gsi
);
7189 if (! virtual_operand_p (gimple_phi_result (phi
)))
7190 find_func_aliases (phi
);
7193 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
7195 gimple stmt
= gsi_stmt (gsi
);
7197 find_func_aliases (stmt
);
7198 find_func_clobbers (stmt
);
7206 fprintf (dump_file
, "\n");
7207 dump_constraints (dump_file
, from
);
7208 fprintf (dump_file
, "\n");
7210 from
= constraints
.length ();
7213 /* From the constraints compute the points-to sets. */
7214 solve_constraints ();
7216 /* Compute the global points-to sets for ESCAPED.
7217 ??? Note that the computed escape set is not correct
7218 for the whole unit as we fail to consider graph edges to
7219 externally visible functions. */
7220 ipa_escaped_pt
= find_what_var_points_to (get_varinfo (escaped_id
));
7222 /* Make sure the ESCAPED solution (which is used as placeholder in
7223 other solutions) does not reference itself. This simplifies
7224 points-to solution queries. */
7225 ipa_escaped_pt
.ipa_escaped
= 0;
7227 /* Assign the points-to sets to the SSA names in the unit. */
7228 FOR_EACH_DEFINED_FUNCTION (node
)
7231 struct function
*fn
;
7235 struct pt_solution uses
, clobbers
;
7236 struct cgraph_edge
*e
;
7238 /* Nodes without a body are not interesting. */
7239 if (!cgraph_function_with_gimple_body_p (node
) || node
->clone_of
)
7242 fn
= DECL_STRUCT_FUNCTION (node
->decl
);
7244 /* Compute the points-to sets for pointer SSA_NAMEs. */
7245 FOR_EACH_VEC_ELT (*fn
->gimple_df
->ssa_names
, i
, ptr
)
7248 && POINTER_TYPE_P (TREE_TYPE (ptr
)))
7249 find_what_p_points_to (ptr
);
7252 /* Compute the call-use and call-clobber sets for all direct calls. */
7253 fi
= lookup_vi_for_tree (node
->decl
);
7254 gcc_assert (fi
->is_fn_info
);
7256 = find_what_var_points_to (first_vi_for_offset (fi
, fi_clobbers
));
7257 uses
= find_what_var_points_to (first_vi_for_offset (fi
, fi_uses
));
7258 for (e
= node
->callers
; e
; e
= e
->next_caller
)
7263 *gimple_call_clobber_set (e
->call_stmt
) = clobbers
;
7264 *gimple_call_use_set (e
->call_stmt
) = uses
;
7267 /* Compute the call-use and call-clobber sets for indirect calls
7268 and calls to external functions. */
7269 FOR_EACH_BB_FN (bb
, fn
)
7271 gimple_stmt_iterator gsi
;
7273 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
7275 gimple stmt
= gsi_stmt (gsi
);
7276 struct pt_solution
*pt
;
7280 if (!is_gimple_call (stmt
))
7283 /* Handle direct calls to external functions. */
7284 decl
= gimple_call_fndecl (stmt
);
7286 && (!(fi
= lookup_vi_for_tree (decl
))
7287 || !fi
->is_fn_info
))
7289 pt
= gimple_call_use_set (stmt
);
7290 if (gimple_call_flags (stmt
) & ECF_CONST
)
7291 memset (pt
, 0, sizeof (struct pt_solution
));
7292 else if ((vi
= lookup_call_use_vi (stmt
)) != NULL
)
7294 *pt
= find_what_var_points_to (vi
);
7295 /* Escaped (and thus nonlocal) variables are always
7296 implicitly used by calls. */
7297 /* ??? ESCAPED can be empty even though NONLOCAL
7300 pt
->ipa_escaped
= 1;
7304 /* If there is nothing special about this call then
7305 we have made everything that is used also escape. */
7306 *pt
= ipa_escaped_pt
;
7310 pt
= gimple_call_clobber_set (stmt
);
7311 if (gimple_call_flags (stmt
) & (ECF_CONST
|ECF_PURE
|ECF_NOVOPS
))
7312 memset (pt
, 0, sizeof (struct pt_solution
));
7313 else if ((vi
= lookup_call_clobber_vi (stmt
)) != NULL
)
7315 *pt
= find_what_var_points_to (vi
);
7316 /* Escaped (and thus nonlocal) variables are always
7317 implicitly clobbered by calls. */
7318 /* ??? ESCAPED can be empty even though NONLOCAL
7321 pt
->ipa_escaped
= 1;
7325 /* If there is nothing special about this call then
7326 we have made everything that is used also escape. */
7327 *pt
= ipa_escaped_pt
;
7332 /* Handle indirect calls. */
7334 && (fi
= get_fi_for_callee (stmt
)))
7336 /* We need to accumulate all clobbers/uses of all possible
7338 fi
= get_varinfo (find (fi
->id
));
7339 /* If we cannot constrain the set of functions we'll end up
7340 calling we end up using/clobbering everything. */
7341 if (bitmap_bit_p (fi
->solution
, anything_id
)
7342 || bitmap_bit_p (fi
->solution
, nonlocal_id
)
7343 || bitmap_bit_p (fi
->solution
, escaped_id
))
7345 pt_solution_reset (gimple_call_clobber_set (stmt
));
7346 pt_solution_reset (gimple_call_use_set (stmt
));
7352 struct pt_solution
*uses
, *clobbers
;
7354 uses
= gimple_call_use_set (stmt
);
7355 clobbers
= gimple_call_clobber_set (stmt
);
7356 memset (uses
, 0, sizeof (struct pt_solution
));
7357 memset (clobbers
, 0, sizeof (struct pt_solution
));
7358 EXECUTE_IF_SET_IN_BITMAP (fi
->solution
, 0, i
, bi
)
7360 struct pt_solution sol
;
7362 vi
= get_varinfo (i
);
7363 if (!vi
->is_fn_info
)
7365 /* ??? We could be more precise here? */
7367 uses
->ipa_escaped
= 1;
7368 clobbers
->nonlocal
= 1;
7369 clobbers
->ipa_escaped
= 1;
7373 if (!uses
->anything
)
7375 sol
= find_what_var_points_to
7376 (first_vi_for_offset (vi
, fi_uses
));
7377 pt_solution_ior_into (uses
, &sol
);
7379 if (!clobbers
->anything
)
7381 sol
= find_what_var_points_to
7382 (first_vi_for_offset (vi
, fi_clobbers
));
7383 pt_solution_ior_into (clobbers
, &sol
);
7391 fn
->gimple_df
->ipa_pta
= true;
7394 delete_points_to_sets ();
7403 const pass_data pass_data_ipa_pta
=
7405 SIMPLE_IPA_PASS
, /* type */
7407 OPTGROUP_NONE
, /* optinfo_flags */
7408 true, /* has_gate */
7409 true, /* has_execute */
7410 TV_IPA_PTA
, /* tv_id */
7411 0, /* properties_required */
7412 0, /* properties_provided */
7413 0, /* properties_destroyed */
7414 0, /* todo_flags_start */
7415 TODO_update_ssa
, /* todo_flags_finish */
7418 class pass_ipa_pta
: public simple_ipa_opt_pass
7421 pass_ipa_pta (gcc::context
*ctxt
)
7422 : simple_ipa_opt_pass (pass_data_ipa_pta
, ctxt
)
7425 /* opt_pass methods: */
7426 bool gate () { return gate_ipa_pta (); }
7427 unsigned int execute () { return ipa_pta_execute (); }
7429 }; // class pass_ipa_pta
7433 simple_ipa_opt_pass
*
7434 make_pass_ipa_pta (gcc::context
*ctxt
)
7436 return new pass_ipa_pta (ctxt
);