1 /* Tree based points-to analysis
2 Copyright (C) 2005-2015 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 "hard-reg-set.h"
32 #include "dominance.h"
34 #include "basic-block.h"
38 #include "fold-const.h"
39 #include "stor-layout.h"
41 #include "tree-ssa-alias.h"
42 #include "internal-fn.h"
43 #include "gimple-expr.h"
45 #include "gimple-iterator.h"
46 #include "gimple-ssa.h"
47 #include "plugin-api.h"
50 #include "stringpool.h"
51 #include "tree-ssanames.h"
52 #include "tree-into-ssa.h"
54 #include "insn-config.h"
63 #include "tree-inline.h"
64 #include "diagnostic-core.h"
65 #include "tree-pass.h"
66 #include "alloc-pool.h"
67 #include "splay-tree.h"
69 #include "tree-phinodes.h"
70 #include "ssa-iterators.h"
71 #include "tree-pretty-print.h"
72 #include "gimple-walk.h"
74 /* The idea behind this analyzer is to generate set constraints from the
75 program, then solve the resulting constraints in order to generate the
78 Set constraints are a way of modeling program analysis problems that
79 involve sets. They consist of an inclusion constraint language,
80 describing the variables (each variable is a set) and operations that
81 are involved on the variables, and a set of rules that derive facts
82 from these operations. To solve a system of set constraints, you derive
83 all possible facts under the rules, which gives you the correct sets
86 See "Efficient Field-sensitive pointer analysis for C" by "David
87 J. Pearce and Paul H. J. Kelly and Chris Hankin, at
88 http://citeseer.ist.psu.edu/pearce04efficient.html
90 Also see "Ultra-fast Aliasing Analysis using CLA: A Million Lines
91 of C Code in a Second" by ""Nevin Heintze and Olivier Tardieu" at
92 http://citeseer.ist.psu.edu/heintze01ultrafast.html
94 There are three types of real constraint expressions, DEREF,
95 ADDRESSOF, and SCALAR. Each constraint expression consists
96 of a constraint type, a variable, and an offset.
98 SCALAR is a constraint expression type used to represent x, whether
99 it appears on the LHS or the RHS of a statement.
100 DEREF is a constraint expression type used to represent *x, whether
101 it appears on the LHS or the RHS of a statement.
102 ADDRESSOF is a constraint expression used to represent &x, whether
103 it appears on the LHS or the RHS of a statement.
105 Each pointer variable in the program is assigned an integer id, and
106 each field of a structure variable is assigned an integer id as well.
108 Structure variables are linked to their list of fields through a "next
109 field" in each variable that points to the next field in offset
111 Each variable for a structure field has
113 1. "size", that tells the size in bits of that field.
114 2. "fullsize, that tells the size in bits of the entire structure.
115 3. "offset", that tells the offset in bits from the beginning of the
116 structure to this field.
128 foo.a -> id 1, size 32, offset 0, fullsize 64, next foo.b
129 foo.b -> id 2, size 32, offset 32, fullsize 64, next NULL
130 bar -> id 3, size 32, offset 0, fullsize 32, next NULL
133 In order to solve the system of set constraints, the following is
136 1. Each constraint variable x has a solution set associated with it,
139 2. Constraints are separated into direct, copy, and complex.
140 Direct constraints are ADDRESSOF constraints that require no extra
141 processing, such as P = &Q
142 Copy constraints are those of the form P = Q.
143 Complex constraints are all the constraints involving dereferences
144 and offsets (including offsetted copies).
146 3. All direct constraints of the form P = &Q are processed, such
147 that Q is added to Sol(P)
149 4. All complex constraints for a given constraint variable are stored in a
150 linked list attached to that variable's node.
152 5. A directed graph is built out of the copy constraints. Each
153 constraint variable is a node in the graph, and an edge from
154 Q to P is added for each copy constraint of the form P = Q
156 6. The graph is then walked, and solution sets are
157 propagated along the copy edges, such that an edge from Q to P
158 causes Sol(P) <- Sol(P) union Sol(Q).
160 7. As we visit each node, all complex constraints associated with
161 that node are processed by adding appropriate copy edges to the graph, or the
162 appropriate variables to the solution set.
164 8. The process of walking the graph is iterated until no solution
167 Prior to walking the graph in steps 6 and 7, We perform static
168 cycle elimination on the constraint graph, as well
169 as off-line variable substitution.
171 TODO: Adding offsets to pointer-to-structures can be handled (IE not punted
172 on and turned into anything), but isn't. You can just see what offset
173 inside the pointed-to struct it's going to access.
175 TODO: Constant bounded arrays can be handled as if they were structs of the
176 same number of elements.
178 TODO: Modeling heap and incoming pointers becomes much better if we
179 add fields to them as we discover them, which we could do.
181 TODO: We could handle unions, but to be honest, it's probably not
182 worth the pain or slowdown. */
184 /* IPA-PTA optimizations possible.
186 When the indirect function called is ANYTHING we can add disambiguation
187 based on the function signatures (or simply the parameter count which
188 is the varinfo size). We also do not need to consider functions that
189 do not have their address taken.
191 The is_global_var bit which marks escape points is overly conservative
192 in IPA mode. Split it to is_escape_point and is_global_var - only
193 externally visible globals are escape points in IPA mode. This is
194 also needed to fix the pt_solution_includes_global predicate
195 (and thus ptr_deref_may_alias_global_p).
197 The way we introduce DECL_PT_UID to avoid fixing up all points-to
198 sets in the translation unit when we copy a DECL during inlining
199 pessimizes precision. The advantage is that the DECL_PT_UID keeps
200 compile-time and memory usage overhead low - the points-to sets
201 do not grow or get unshared as they would during a fixup phase.
202 An alternative solution is to delay IPA PTA until after all
203 inlining transformations have been applied.
205 The way we propagate clobber/use information isn't optimized.
206 It should use a new complex constraint that properly filters
207 out local variables of the callee (though that would make
208 the sets invalid after inlining). OTOH we might as well
209 admit defeat to WHOPR and simply do all the clobber/use analysis
210 and propagation after PTA finished but before we threw away
211 points-to information for memory variables. WHOPR and PTA
212 do not play along well anyway - the whole constraint solving
213 would need to be done in WPA phase and it will be very interesting
214 to apply the results to local SSA names during LTRANS phase.
216 We probably should compute a per-function unit-ESCAPE solution
217 propagating it simply like the clobber / uses solutions. The
218 solution can go alongside the non-IPA espaced solution and be
219 used to query which vars escape the unit through a function.
221 We never put function decls in points-to sets so we do not
222 keep the set of called functions for indirect calls.
224 And probably more. */
226 static bool use_field_sensitive
= true;
227 static int in_ipa_mode
= 0;
229 /* Used for predecessor bitmaps. */
230 static bitmap_obstack predbitmap_obstack
;
232 /* Used for points-to sets. */
233 static bitmap_obstack pta_obstack
;
235 /* Used for oldsolution members of variables. */
236 static bitmap_obstack oldpta_obstack
;
238 /* Used for per-solver-iteration bitmaps. */
239 static bitmap_obstack iteration_obstack
;
241 static unsigned int create_variable_info_for (tree
, const char *);
242 typedef struct constraint_graph
*constraint_graph_t
;
243 static void unify_nodes (constraint_graph_t
, unsigned int, unsigned int, bool);
246 typedef struct constraint
*constraint_t
;
249 #define EXECUTE_IF_IN_NONNULL_BITMAP(a, b, c, d) \
251 EXECUTE_IF_SET_IN_BITMAP (a, b, c, d)
253 static struct constraint_stats
255 unsigned int total_vars
;
256 unsigned int nonpointer_vars
;
257 unsigned int unified_vars_static
;
258 unsigned int unified_vars_dynamic
;
259 unsigned int iterations
;
260 unsigned int num_edges
;
261 unsigned int num_implicit_edges
;
262 unsigned int points_to_sets_created
;
267 /* ID of this variable */
270 /* True if this is a variable created by the constraint analysis, such as
271 heap variables and constraints we had to break up. */
272 unsigned int is_artificial_var
: 1;
274 /* True if this is a special variable whose solution set should not be
276 unsigned int is_special_var
: 1;
278 /* True for variables whose size is not known or variable. */
279 unsigned int is_unknown_size_var
: 1;
281 /* True for (sub-)fields that represent a whole variable. */
282 unsigned int is_full_var
: 1;
284 /* True if this is a heap variable. */
285 unsigned int is_heap_var
: 1;
287 /* True if this field may contain pointers. */
288 unsigned int may_have_pointers
: 1;
290 /* True if this field has only restrict qualified pointers. */
291 unsigned int only_restrict_pointers
: 1;
293 /* True if this represents a heap var created for a restrict qualified
295 unsigned int is_restrict_var
: 1;
297 /* True if this represents a global variable. */
298 unsigned int is_global_var
: 1;
300 /* True if this represents a IPA function info. */
301 unsigned int is_fn_info
: 1;
303 /* ??? Store somewhere better. */
306 /* The ID of the variable for the next field in this structure
307 or zero for the last field in this structure. */
310 /* The ID of the variable for the first field in this structure. */
313 /* Offset of this variable, in bits, from the base variable */
314 unsigned HOST_WIDE_INT offset
;
316 /* Size of the variable, in bits. */
317 unsigned HOST_WIDE_INT size
;
319 /* Full size of the base variable, in bits. */
320 unsigned HOST_WIDE_INT fullsize
;
322 /* Name of this variable */
325 /* Tree that this variable is associated with. */
328 /* Points-to set for this variable. */
331 /* Old points-to set for this variable. */
334 typedef struct variable_info
*varinfo_t
;
336 static varinfo_t
first_vi_for_offset (varinfo_t
, unsigned HOST_WIDE_INT
);
337 static varinfo_t
first_or_preceding_vi_for_offset (varinfo_t
,
338 unsigned HOST_WIDE_INT
);
339 static varinfo_t
lookup_vi_for_tree (tree
);
340 static inline bool type_can_have_subvars (const_tree
);
342 /* Pool of variable info structures. */
343 static pool_allocator
<variable_info
> variable_info_pool
344 ("Variable info pool", 30);
346 /* Map varinfo to final pt_solution. */
347 static hash_map
<varinfo_t
, pt_solution
*> *final_solutions
;
348 struct obstack final_solutions_obstack
;
350 /* Table of variable info structures for constraint variables.
351 Indexed directly by variable info id. */
352 static vec
<varinfo_t
> varmap
;
354 /* Return the varmap element N */
356 static inline varinfo_t
357 get_varinfo (unsigned int n
)
362 /* Return the next variable in the list of sub-variables of VI
363 or NULL if VI is the last sub-variable. */
365 static inline varinfo_t
366 vi_next (varinfo_t vi
)
368 return get_varinfo (vi
->next
);
371 /* Static IDs for the special variables. Variable ID zero is unused
372 and used as terminator for the sub-variable chain. */
373 enum { nothing_id
= 1, anything_id
= 2, string_id
= 3,
374 escaped_id
= 4, nonlocal_id
= 5,
375 storedanything_id
= 6, integer_id
= 7 };
377 /* Return a new variable info structure consisting for a variable
378 named NAME, and using constraint graph node NODE. Append it
379 to the vector of variable info structures. */
382 new_var_info (tree t
, const char *name
)
384 unsigned index
= varmap
.length ();
385 varinfo_t ret
= variable_info_pool
.allocate ();
390 /* Vars without decl are artificial and do not have sub-variables. */
391 ret
->is_artificial_var
= (t
== NULL_TREE
);
392 ret
->is_special_var
= false;
393 ret
->is_unknown_size_var
= false;
394 ret
->is_full_var
= (t
== NULL_TREE
);
395 ret
->is_heap_var
= false;
396 ret
->may_have_pointers
= true;
397 ret
->only_restrict_pointers
= false;
398 ret
->is_restrict_var
= false;
400 ret
->is_global_var
= (t
== NULL_TREE
);
401 ret
->is_fn_info
= false;
403 ret
->is_global_var
= (is_global_var (t
)
404 /* We have to treat even local register variables
406 || (TREE_CODE (t
) == VAR_DECL
407 && DECL_HARD_REGISTER (t
)));
408 ret
->solution
= BITMAP_ALLOC (&pta_obstack
);
409 ret
->oldsolution
= NULL
;
415 varmap
.safe_push (ret
);
421 /* A map mapping call statements to per-stmt variables for uses
422 and clobbers specific to the call. */
423 static hash_map
<gimple
, varinfo_t
> *call_stmt_vars
;
425 /* Lookup or create the variable for the call statement CALL. */
428 get_call_vi (gcall
*call
)
433 varinfo_t
*slot_p
= &call_stmt_vars
->get_or_insert (call
, &existed
);
437 vi
= new_var_info (NULL_TREE
, "CALLUSED");
441 vi
->is_full_var
= true;
443 vi2
= new_var_info (NULL_TREE
, "CALLCLOBBERED");
447 vi2
->is_full_var
= true;
455 /* Lookup the variable for the call statement CALL representing
456 the uses. Returns NULL if there is nothing special about this call. */
459 lookup_call_use_vi (gcall
*call
)
461 varinfo_t
*slot_p
= call_stmt_vars
->get (call
);
468 /* Lookup the variable for the call statement CALL representing
469 the clobbers. Returns NULL if there is nothing special about this call. */
472 lookup_call_clobber_vi (gcall
*call
)
474 varinfo_t uses
= lookup_call_use_vi (call
);
478 return vi_next (uses
);
481 /* Lookup or create the variable for the call statement CALL representing
485 get_call_use_vi (gcall
*call
)
487 return get_call_vi (call
);
490 /* Lookup or create the variable for the call statement CALL representing
493 static varinfo_t ATTRIBUTE_UNUSED
494 get_call_clobber_vi (gcall
*call
)
496 return vi_next (get_call_vi (call
));
500 typedef enum {SCALAR
, DEREF
, ADDRESSOF
} constraint_expr_type
;
502 /* An expression that appears in a constraint. */
504 struct constraint_expr
506 /* Constraint type. */
507 constraint_expr_type type
;
509 /* Variable we are referring to in the constraint. */
512 /* Offset, in bits, of this constraint from the beginning of
513 variables it ends up referring to.
515 IOW, in a deref constraint, we would deref, get the result set,
516 then add OFFSET to each member. */
517 HOST_WIDE_INT offset
;
520 /* Use 0x8000... as special unknown offset. */
521 #define UNKNOWN_OFFSET HOST_WIDE_INT_MIN
523 typedef struct constraint_expr ce_s
;
524 static void get_constraint_for_1 (tree
, vec
<ce_s
> *, bool, bool);
525 static void get_constraint_for (tree
, vec
<ce_s
> *);
526 static void get_constraint_for_rhs (tree
, vec
<ce_s
> *);
527 static void do_deref (vec
<ce_s
> *);
529 /* Our set constraints are made up of two constraint expressions, one
532 As described in the introduction, our set constraints each represent an
533 operation between set valued variables.
537 struct constraint_expr lhs
;
538 struct constraint_expr rhs
;
541 /* List of constraints that we use to build the constraint graph from. */
543 static vec
<constraint_t
> constraints
;
544 static pool_allocator
<constraint
> constraint_pool ("Constraint pool", 30);
546 /* The constraint graph is represented as an array of bitmaps
547 containing successor nodes. */
549 struct constraint_graph
551 /* Size of this graph, which may be different than the number of
552 nodes in the variable map. */
555 /* Explicit successors of each node. */
558 /* Implicit predecessors of each node (Used for variable
560 bitmap
*implicit_preds
;
562 /* Explicit predecessors of each node (Used for variable substitution). */
565 /* Indirect cycle representatives, or -1 if the node has no indirect
567 int *indirect_cycles
;
569 /* Representative node for a node. rep[a] == a unless the node has
573 /* Equivalence class representative for a label. This is used for
574 variable substitution. */
577 /* Pointer equivalence label for a node. All nodes with the same
578 pointer equivalence label can be unified together at some point
579 (either during constraint optimization or after the constraint
583 /* Pointer equivalence representative for a label. This is used to
584 handle nodes that are pointer equivalent but not location
585 equivalent. We can unite these once the addressof constraints
586 are transformed into initial points-to sets. */
589 /* Pointer equivalence label for each node, used during variable
591 unsigned int *pointer_label
;
593 /* Location equivalence label for each node, used during location
594 equivalence finding. */
595 unsigned int *loc_label
;
597 /* Pointed-by set for each node, used during location equivalence
598 finding. This is pointed-by rather than pointed-to, because it
599 is constructed using the predecessor graph. */
602 /* Points to sets for pointer equivalence. This is *not* the actual
603 points-to sets for nodes. */
606 /* Bitmap of nodes where the bit is set if the node is a direct
607 node. Used for variable substitution. */
608 sbitmap direct_nodes
;
610 /* Bitmap of nodes where the bit is set if the node is address
611 taken. Used for variable substitution. */
612 bitmap address_taken
;
614 /* Vector of complex constraints for each graph node. Complex
615 constraints are those involving dereferences or offsets that are
617 vec
<constraint_t
> *complex;
620 static constraint_graph_t graph
;
622 /* During variable substitution and the offline version of indirect
623 cycle finding, we create nodes to represent dereferences and
624 address taken constraints. These represent where these start and
626 #define FIRST_REF_NODE (varmap).length ()
627 #define LAST_REF_NODE (FIRST_REF_NODE + (FIRST_REF_NODE - 1))
629 /* Return the representative node for NODE, if NODE has been unioned
631 This function performs path compression along the way to finding
632 the representative. */
635 find (unsigned int node
)
637 gcc_checking_assert (node
< graph
->size
);
638 if (graph
->rep
[node
] != node
)
639 return graph
->rep
[node
] = find (graph
->rep
[node
]);
643 /* Union the TO and FROM nodes to the TO nodes.
644 Note that at some point in the future, we may want to do
645 union-by-rank, in which case we are going to have to return the
646 node we unified to. */
649 unite (unsigned int to
, unsigned int from
)
651 gcc_checking_assert (to
< graph
->size
&& from
< graph
->size
);
652 if (to
!= from
&& graph
->rep
[from
] != to
)
654 graph
->rep
[from
] = to
;
660 /* Create a new constraint consisting of LHS and RHS expressions. */
663 new_constraint (const struct constraint_expr lhs
,
664 const struct constraint_expr rhs
)
666 constraint_t ret
= constraint_pool
.allocate ();
672 /* Print out constraint C to FILE. */
675 dump_constraint (FILE *file
, constraint_t c
)
677 if (c
->lhs
.type
== ADDRESSOF
)
679 else if (c
->lhs
.type
== DEREF
)
681 fprintf (file
, "%s", get_varinfo (c
->lhs
.var
)->name
);
682 if (c
->lhs
.offset
== UNKNOWN_OFFSET
)
683 fprintf (file
, " + UNKNOWN");
684 else if (c
->lhs
.offset
!= 0)
685 fprintf (file
, " + " HOST_WIDE_INT_PRINT_DEC
, c
->lhs
.offset
);
686 fprintf (file
, " = ");
687 if (c
->rhs
.type
== ADDRESSOF
)
689 else if (c
->rhs
.type
== DEREF
)
691 fprintf (file
, "%s", get_varinfo (c
->rhs
.var
)->name
);
692 if (c
->rhs
.offset
== UNKNOWN_OFFSET
)
693 fprintf (file
, " + UNKNOWN");
694 else if (c
->rhs
.offset
!= 0)
695 fprintf (file
, " + " HOST_WIDE_INT_PRINT_DEC
, c
->rhs
.offset
);
699 void debug_constraint (constraint_t
);
700 void debug_constraints (void);
701 void debug_constraint_graph (void);
702 void debug_solution_for_var (unsigned int);
703 void debug_sa_points_to_info (void);
705 /* Print out constraint C to stderr. */
708 debug_constraint (constraint_t c
)
710 dump_constraint (stderr
, c
);
711 fprintf (stderr
, "\n");
714 /* Print out all constraints to FILE */
717 dump_constraints (FILE *file
, int from
)
721 for (i
= from
; constraints
.iterate (i
, &c
); i
++)
724 dump_constraint (file
, c
);
725 fprintf (file
, "\n");
729 /* Print out all constraints to stderr. */
732 debug_constraints (void)
734 dump_constraints (stderr
, 0);
737 /* Print the constraint graph in dot format. */
740 dump_constraint_graph (FILE *file
)
744 /* Only print the graph if it has already been initialized: */
748 /* Prints the header of the dot file: */
749 fprintf (file
, "strict digraph {\n");
750 fprintf (file
, " node [\n shape = box\n ]\n");
751 fprintf (file
, " edge [\n fontsize = \"12\"\n ]\n");
752 fprintf (file
, "\n // List of nodes and complex constraints in "
753 "the constraint graph:\n");
755 /* The next lines print the nodes in the graph together with the
756 complex constraints attached to them. */
757 for (i
= 1; i
< graph
->size
; i
++)
759 if (i
== FIRST_REF_NODE
)
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 if (graph
->complex[i
].exists ())
771 fprintf (file
, " [label=\"\\N\\n");
772 for (j
= 0; graph
->complex[i
].iterate (j
, &c
); ++j
)
774 dump_constraint (file
, c
);
775 fprintf (file
, "\\l");
777 fprintf (file
, "\"]");
779 fprintf (file
, ";\n");
782 /* Go over the edges. */
783 fprintf (file
, "\n // Edges in the constraint graph:\n");
784 for (i
= 1; i
< graph
->size
; i
++)
790 EXECUTE_IF_IN_NONNULL_BITMAP (graph
->succs
[i
], 0, j
, bi
)
792 unsigned to
= find (j
);
795 if (i
< FIRST_REF_NODE
)
796 fprintf (file
, "\"%s\"", get_varinfo (i
)->name
);
798 fprintf (file
, "\"*%s\"", get_varinfo (i
- FIRST_REF_NODE
)->name
);
799 fprintf (file
, " -> ");
800 if (to
< FIRST_REF_NODE
)
801 fprintf (file
, "\"%s\"", get_varinfo (to
)->name
);
803 fprintf (file
, "\"*%s\"", get_varinfo (to
- FIRST_REF_NODE
)->name
);
804 fprintf (file
, ";\n");
808 /* Prints the tail of the dot file. */
809 fprintf (file
, "}\n");
812 /* Print out the constraint graph to stderr. */
815 debug_constraint_graph (void)
817 dump_constraint_graph (stderr
);
822 The solver is a simple worklist solver, that works on the following
825 sbitmap changed_nodes = all zeroes;
827 For each node that is not already collapsed:
829 set bit in changed nodes
831 while (changed_count > 0)
833 compute topological ordering for constraint graph
835 find and collapse cycles in the constraint graph (updating
836 changed if necessary)
838 for each node (n) in the graph in topological order:
841 Process each complex constraint associated with the node,
842 updating changed if necessary.
844 For each outgoing edge from n, propagate the solution from n to
845 the destination of the edge, updating changed as necessary.
849 /* Return true if two constraint expressions A and B are equal. */
852 constraint_expr_equal (struct constraint_expr a
, struct constraint_expr b
)
854 return a
.type
== b
.type
&& a
.var
== b
.var
&& a
.offset
== b
.offset
;
857 /* Return true if constraint expression A is less than constraint expression
858 B. This is just arbitrary, but consistent, in order to give them an
862 constraint_expr_less (struct constraint_expr a
, struct constraint_expr b
)
864 if (a
.type
== b
.type
)
867 return a
.offset
< b
.offset
;
869 return a
.var
< b
.var
;
872 return a
.type
< b
.type
;
875 /* Return true if constraint A is less than constraint B. This is just
876 arbitrary, but consistent, in order to give them an ordering. */
879 constraint_less (const constraint_t
&a
, const constraint_t
&b
)
881 if (constraint_expr_less (a
->lhs
, b
->lhs
))
883 else if (constraint_expr_less (b
->lhs
, a
->lhs
))
886 return constraint_expr_less (a
->rhs
, b
->rhs
);
889 /* Return true if two constraints A and B are equal. */
892 constraint_equal (struct constraint a
, struct constraint b
)
894 return constraint_expr_equal (a
.lhs
, b
.lhs
)
895 && constraint_expr_equal (a
.rhs
, b
.rhs
);
899 /* Find a constraint LOOKFOR in the sorted constraint vector VEC */
902 constraint_vec_find (vec
<constraint_t
> vec
,
903 struct constraint lookfor
)
911 place
= vec
.lower_bound (&lookfor
, constraint_less
);
912 if (place
>= vec
.length ())
915 if (!constraint_equal (*found
, lookfor
))
920 /* Union two constraint vectors, TO and FROM. Put the result in TO.
921 Returns true of TO set is changed. */
924 constraint_set_union (vec
<constraint_t
> *to
,
925 vec
<constraint_t
> *from
)
929 bool any_change
= false;
931 FOR_EACH_VEC_ELT (*from
, i
, c
)
933 if (constraint_vec_find (*to
, *c
) == NULL
)
935 unsigned int place
= to
->lower_bound (c
, constraint_less
);
936 to
->safe_insert (place
, c
);
943 /* Expands the solution in SET to all sub-fields of variables included. */
946 solution_set_expand (bitmap set
, bitmap
*expanded
)
954 *expanded
= BITMAP_ALLOC (&iteration_obstack
);
956 /* In a first pass expand to the head of the variables we need to
957 add all sub-fields off. This avoids quadratic behavior. */
958 EXECUTE_IF_SET_IN_BITMAP (set
, 0, j
, bi
)
960 varinfo_t v
= get_varinfo (j
);
961 if (v
->is_artificial_var
964 bitmap_set_bit (*expanded
, v
->head
);
967 /* In the second pass now expand all head variables with subfields. */
968 EXECUTE_IF_SET_IN_BITMAP (*expanded
, 0, j
, bi
)
970 varinfo_t v
= get_varinfo (j
);
973 for (v
= vi_next (v
); v
!= NULL
; v
= vi_next (v
))
974 bitmap_set_bit (*expanded
, v
->id
);
977 /* And finally set the rest of the bits from SET. */
978 bitmap_ior_into (*expanded
, set
);
983 /* Union solution sets TO and DELTA, and add INC to each member of DELTA in the
987 set_union_with_increment (bitmap to
, bitmap delta
, HOST_WIDE_INT inc
,
988 bitmap
*expanded_delta
)
990 bool changed
= false;
994 /* If the solution of DELTA contains anything it is good enough to transfer
996 if (bitmap_bit_p (delta
, anything_id
))
997 return bitmap_set_bit (to
, anything_id
);
999 /* If the offset is unknown we have to expand the solution to
1001 if (inc
== UNKNOWN_OFFSET
)
1003 delta
= solution_set_expand (delta
, expanded_delta
);
1004 changed
|= bitmap_ior_into (to
, delta
);
1008 /* For non-zero offset union the offsetted solution into the destination. */
1009 EXECUTE_IF_SET_IN_BITMAP (delta
, 0, i
, bi
)
1011 varinfo_t vi
= get_varinfo (i
);
1013 /* If this is a variable with just one field just set its bit
1015 if (vi
->is_artificial_var
1016 || vi
->is_unknown_size_var
1018 changed
|= bitmap_set_bit (to
, i
);
1021 HOST_WIDE_INT fieldoffset
= vi
->offset
+ inc
;
1022 unsigned HOST_WIDE_INT size
= vi
->size
;
1024 /* If the offset makes the pointer point to before the
1025 variable use offset zero for the field lookup. */
1026 if (fieldoffset
< 0)
1027 vi
= get_varinfo (vi
->head
);
1029 vi
= first_or_preceding_vi_for_offset (vi
, fieldoffset
);
1033 changed
|= bitmap_set_bit (to
, vi
->id
);
1038 /* We have to include all fields that overlap the current field
1042 while (vi
->offset
< fieldoffset
+ size
);
1049 /* Insert constraint C into the list of complex constraints for graph
1053 insert_into_complex (constraint_graph_t graph
,
1054 unsigned int var
, constraint_t c
)
1056 vec
<constraint_t
> complex = graph
->complex[var
];
1057 unsigned int place
= complex.lower_bound (c
, constraint_less
);
1059 /* Only insert constraints that do not already exist. */
1060 if (place
>= complex.length ()
1061 || !constraint_equal (*c
, *complex[place
]))
1062 graph
->complex[var
].safe_insert (place
, c
);
1066 /* Condense two variable nodes into a single variable node, by moving
1067 all associated info from FROM to TO. Returns true if TO node's
1068 constraint set changes after the merge. */
1071 merge_node_constraints (constraint_graph_t graph
, unsigned int to
,
1076 bool any_change
= false;
1078 gcc_checking_assert (find (from
) == to
);
1080 /* Move all complex constraints from src node into to node */
1081 FOR_EACH_VEC_ELT (graph
->complex[from
], i
, c
)
1083 /* In complex constraints for node FROM, we may have either
1084 a = *FROM, and *FROM = a, or an offseted constraint which are
1085 always added to the rhs node's constraints. */
1087 if (c
->rhs
.type
== DEREF
)
1089 else if (c
->lhs
.type
== DEREF
)
1095 any_change
= constraint_set_union (&graph
->complex[to
],
1096 &graph
->complex[from
]);
1097 graph
->complex[from
].release ();
1102 /* Remove edges involving NODE from GRAPH. */
1105 clear_edges_for_node (constraint_graph_t graph
, unsigned int node
)
1107 if (graph
->succs
[node
])
1108 BITMAP_FREE (graph
->succs
[node
]);
1111 /* Merge GRAPH nodes FROM and TO into node TO. */
1114 merge_graph_nodes (constraint_graph_t graph
, unsigned int to
,
1117 if (graph
->indirect_cycles
[from
] != -1)
1119 /* If we have indirect cycles with the from node, and we have
1120 none on the to node, the to node has indirect cycles from the
1121 from node now that they are unified.
1122 If indirect cycles exist on both, unify the nodes that they
1123 are in a cycle with, since we know they are in a cycle with
1125 if (graph
->indirect_cycles
[to
] == -1)
1126 graph
->indirect_cycles
[to
] = graph
->indirect_cycles
[from
];
1129 /* Merge all the successor edges. */
1130 if (graph
->succs
[from
])
1132 if (!graph
->succs
[to
])
1133 graph
->succs
[to
] = BITMAP_ALLOC (&pta_obstack
);
1134 bitmap_ior_into (graph
->succs
[to
],
1135 graph
->succs
[from
]);
1138 clear_edges_for_node (graph
, from
);
1142 /* Add an indirect graph edge to GRAPH, going from TO to FROM if
1143 it doesn't exist in the graph already. */
1146 add_implicit_graph_edge (constraint_graph_t graph
, unsigned int to
,
1152 if (!graph
->implicit_preds
[to
])
1153 graph
->implicit_preds
[to
] = BITMAP_ALLOC (&predbitmap_obstack
);
1155 if (bitmap_set_bit (graph
->implicit_preds
[to
], from
))
1156 stats
.num_implicit_edges
++;
1159 /* Add a predecessor graph edge to GRAPH, going from TO to FROM if
1160 it doesn't exist in the graph already.
1161 Return false if the edge already existed, true otherwise. */
1164 add_pred_graph_edge (constraint_graph_t graph
, unsigned int to
,
1167 if (!graph
->preds
[to
])
1168 graph
->preds
[to
] = BITMAP_ALLOC (&predbitmap_obstack
);
1169 bitmap_set_bit (graph
->preds
[to
], from
);
1172 /* Add a graph edge to GRAPH, going from FROM to TO if
1173 it doesn't exist in the graph already.
1174 Return false if the edge already existed, true otherwise. */
1177 add_graph_edge (constraint_graph_t graph
, unsigned int to
,
1188 if (!graph
->succs
[from
])
1189 graph
->succs
[from
] = BITMAP_ALLOC (&pta_obstack
);
1190 if (bitmap_set_bit (graph
->succs
[from
], to
))
1193 if (to
< FIRST_REF_NODE
&& from
< FIRST_REF_NODE
)
1201 /* Initialize the constraint graph structure to contain SIZE nodes. */
1204 init_graph (unsigned int size
)
1208 graph
= XCNEW (struct constraint_graph
);
1210 graph
->succs
= XCNEWVEC (bitmap
, graph
->size
);
1211 graph
->indirect_cycles
= XNEWVEC (int, graph
->size
);
1212 graph
->rep
= XNEWVEC (unsigned int, graph
->size
);
1213 /* ??? Macros do not support template types with multiple arguments,
1214 so we use a typedef to work around it. */
1215 typedef vec
<constraint_t
> vec_constraint_t_heap
;
1216 graph
->complex = XCNEWVEC (vec_constraint_t_heap
, size
);
1217 graph
->pe
= XCNEWVEC (unsigned int, graph
->size
);
1218 graph
->pe_rep
= XNEWVEC (int, graph
->size
);
1220 for (j
= 0; j
< graph
->size
; j
++)
1223 graph
->pe_rep
[j
] = -1;
1224 graph
->indirect_cycles
[j
] = -1;
1228 /* Build the constraint graph, adding only predecessor edges right now. */
1231 build_pred_graph (void)
1237 graph
->implicit_preds
= XCNEWVEC (bitmap
, graph
->size
);
1238 graph
->preds
= XCNEWVEC (bitmap
, graph
->size
);
1239 graph
->pointer_label
= XCNEWVEC (unsigned int, graph
->size
);
1240 graph
->loc_label
= XCNEWVEC (unsigned int, graph
->size
);
1241 graph
->pointed_by
= XCNEWVEC (bitmap
, graph
->size
);
1242 graph
->points_to
= XCNEWVEC (bitmap
, graph
->size
);
1243 graph
->eq_rep
= XNEWVEC (int, graph
->size
);
1244 graph
->direct_nodes
= sbitmap_alloc (graph
->size
);
1245 graph
->address_taken
= BITMAP_ALLOC (&predbitmap_obstack
);
1246 bitmap_clear (graph
->direct_nodes
);
1248 for (j
= 1; j
< FIRST_REF_NODE
; j
++)
1250 if (!get_varinfo (j
)->is_special_var
)
1251 bitmap_set_bit (graph
->direct_nodes
, j
);
1254 for (j
= 0; j
< graph
->size
; j
++)
1255 graph
->eq_rep
[j
] = -1;
1257 for (j
= 0; j
< varmap
.length (); j
++)
1258 graph
->indirect_cycles
[j
] = -1;
1260 FOR_EACH_VEC_ELT (constraints
, i
, c
)
1262 struct constraint_expr lhs
= c
->lhs
;
1263 struct constraint_expr rhs
= c
->rhs
;
1264 unsigned int lhsvar
= lhs
.var
;
1265 unsigned int rhsvar
= rhs
.var
;
1267 if (lhs
.type
== DEREF
)
1270 if (rhs
.offset
== 0 && lhs
.offset
== 0 && rhs
.type
== SCALAR
)
1271 add_pred_graph_edge (graph
, FIRST_REF_NODE
+ lhsvar
, rhsvar
);
1273 else if (rhs
.type
== DEREF
)
1276 if (rhs
.offset
== 0 && lhs
.offset
== 0 && lhs
.type
== SCALAR
)
1277 add_pred_graph_edge (graph
, lhsvar
, FIRST_REF_NODE
+ rhsvar
);
1279 bitmap_clear_bit (graph
->direct_nodes
, lhsvar
);
1281 else if (rhs
.type
== ADDRESSOF
)
1286 if (graph
->points_to
[lhsvar
] == NULL
)
1287 graph
->points_to
[lhsvar
] = BITMAP_ALLOC (&predbitmap_obstack
);
1288 bitmap_set_bit (graph
->points_to
[lhsvar
], rhsvar
);
1290 if (graph
->pointed_by
[rhsvar
] == NULL
)
1291 graph
->pointed_by
[rhsvar
] = BITMAP_ALLOC (&predbitmap_obstack
);
1292 bitmap_set_bit (graph
->pointed_by
[rhsvar
], lhsvar
);
1294 /* Implicitly, *x = y */
1295 add_implicit_graph_edge (graph
, FIRST_REF_NODE
+ lhsvar
, rhsvar
);
1297 /* All related variables are no longer direct nodes. */
1298 bitmap_clear_bit (graph
->direct_nodes
, rhsvar
);
1299 v
= get_varinfo (rhsvar
);
1300 if (!v
->is_full_var
)
1302 v
= get_varinfo (v
->head
);
1305 bitmap_clear_bit (graph
->direct_nodes
, v
->id
);
1310 bitmap_set_bit (graph
->address_taken
, rhsvar
);
1312 else if (lhsvar
> anything_id
1313 && lhsvar
!= rhsvar
&& lhs
.offset
== 0 && rhs
.offset
== 0)
1316 add_pred_graph_edge (graph
, lhsvar
, rhsvar
);
1317 /* Implicitly, *x = *y */
1318 add_implicit_graph_edge (graph
, FIRST_REF_NODE
+ lhsvar
,
1319 FIRST_REF_NODE
+ rhsvar
);
1321 else if (lhs
.offset
!= 0 || rhs
.offset
!= 0)
1323 if (rhs
.offset
!= 0)
1324 bitmap_clear_bit (graph
->direct_nodes
, lhs
.var
);
1325 else if (lhs
.offset
!= 0)
1326 bitmap_clear_bit (graph
->direct_nodes
, rhs
.var
);
1331 /* Build the constraint graph, adding successor edges. */
1334 build_succ_graph (void)
1339 FOR_EACH_VEC_ELT (constraints
, i
, c
)
1341 struct constraint_expr lhs
;
1342 struct constraint_expr rhs
;
1343 unsigned int lhsvar
;
1344 unsigned int rhsvar
;
1351 lhsvar
= find (lhs
.var
);
1352 rhsvar
= find (rhs
.var
);
1354 if (lhs
.type
== DEREF
)
1356 if (rhs
.offset
== 0 && lhs
.offset
== 0 && rhs
.type
== SCALAR
)
1357 add_graph_edge (graph
, FIRST_REF_NODE
+ lhsvar
, rhsvar
);
1359 else if (rhs
.type
== DEREF
)
1361 if (rhs
.offset
== 0 && lhs
.offset
== 0 && lhs
.type
== SCALAR
)
1362 add_graph_edge (graph
, lhsvar
, FIRST_REF_NODE
+ rhsvar
);
1364 else if (rhs
.type
== ADDRESSOF
)
1367 gcc_checking_assert (find (rhs
.var
) == rhs
.var
);
1368 bitmap_set_bit (get_varinfo (lhsvar
)->solution
, rhsvar
);
1370 else if (lhsvar
> anything_id
1371 && lhsvar
!= rhsvar
&& lhs
.offset
== 0 && rhs
.offset
== 0)
1373 add_graph_edge (graph
, lhsvar
, rhsvar
);
1377 /* Add edges from STOREDANYTHING to all non-direct nodes that can
1378 receive pointers. */
1379 t
= find (storedanything_id
);
1380 for (i
= integer_id
+ 1; i
< FIRST_REF_NODE
; ++i
)
1382 if (!bitmap_bit_p (graph
->direct_nodes
, i
)
1383 && get_varinfo (i
)->may_have_pointers
)
1384 add_graph_edge (graph
, find (i
), t
);
1387 /* Everything stored to ANYTHING also potentially escapes. */
1388 add_graph_edge (graph
, find (escaped_id
), t
);
1392 /* Changed variables on the last iteration. */
1393 static bitmap changed
;
1395 /* Strongly Connected Component visitation info. */
1402 unsigned int *node_mapping
;
1404 vec
<unsigned> scc_stack
;
1408 /* Recursive routine to find strongly connected components in GRAPH.
1409 SI is the SCC info to store the information in, and N is the id of current
1410 graph node we are processing.
1412 This is Tarjan's strongly connected component finding algorithm, as
1413 modified by Nuutila to keep only non-root nodes on the stack.
1414 The algorithm can be found in "On finding the strongly connected
1415 connected components in a directed graph" by Esko Nuutila and Eljas
1416 Soisalon-Soininen, in Information Processing Letters volume 49,
1417 number 1, pages 9-14. */
1420 scc_visit (constraint_graph_t graph
, struct scc_info
*si
, unsigned int n
)
1424 unsigned int my_dfs
;
1426 bitmap_set_bit (si
->visited
, n
);
1427 si
->dfs
[n
] = si
->current_index
++;
1428 my_dfs
= si
->dfs
[n
];
1430 /* Visit all the successors. */
1431 EXECUTE_IF_IN_NONNULL_BITMAP (graph
->succs
[n
], 0, i
, bi
)
1435 if (i
> LAST_REF_NODE
)
1439 if (bitmap_bit_p (si
->deleted
, w
))
1442 if (!bitmap_bit_p (si
->visited
, w
))
1443 scc_visit (graph
, si
, w
);
1445 unsigned int t
= find (w
);
1446 gcc_checking_assert (find (n
) == n
);
1447 if (si
->dfs
[t
] < si
->dfs
[n
])
1448 si
->dfs
[n
] = si
->dfs
[t
];
1451 /* See if any components have been identified. */
1452 if (si
->dfs
[n
] == my_dfs
)
1454 if (si
->scc_stack
.length () > 0
1455 && si
->dfs
[si
->scc_stack
.last ()] >= my_dfs
)
1457 bitmap scc
= BITMAP_ALLOC (NULL
);
1458 unsigned int lowest_node
;
1461 bitmap_set_bit (scc
, n
);
1463 while (si
->scc_stack
.length () != 0
1464 && si
->dfs
[si
->scc_stack
.last ()] >= my_dfs
)
1466 unsigned int w
= si
->scc_stack
.pop ();
1468 bitmap_set_bit (scc
, w
);
1471 lowest_node
= bitmap_first_set_bit (scc
);
1472 gcc_assert (lowest_node
< FIRST_REF_NODE
);
1474 /* Collapse the SCC nodes into a single node, and mark the
1476 EXECUTE_IF_SET_IN_BITMAP (scc
, 0, i
, bi
)
1478 if (i
< FIRST_REF_NODE
)
1480 if (unite (lowest_node
, i
))
1481 unify_nodes (graph
, lowest_node
, i
, false);
1485 unite (lowest_node
, i
);
1486 graph
->indirect_cycles
[i
- FIRST_REF_NODE
] = lowest_node
;
1490 bitmap_set_bit (si
->deleted
, n
);
1493 si
->scc_stack
.safe_push (n
);
1496 /* Unify node FROM into node TO, updating the changed count if
1497 necessary when UPDATE_CHANGED is true. */
1500 unify_nodes (constraint_graph_t graph
, unsigned int to
, unsigned int from
,
1501 bool update_changed
)
1503 gcc_checking_assert (to
!= from
&& find (to
) == to
);
1505 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1506 fprintf (dump_file
, "Unifying %s to %s\n",
1507 get_varinfo (from
)->name
,
1508 get_varinfo (to
)->name
);
1511 stats
.unified_vars_dynamic
++;
1513 stats
.unified_vars_static
++;
1515 merge_graph_nodes (graph
, to
, from
);
1516 if (merge_node_constraints (graph
, to
, from
))
1519 bitmap_set_bit (changed
, to
);
1522 /* Mark TO as changed if FROM was changed. If TO was already marked
1523 as changed, decrease the changed count. */
1526 && bitmap_clear_bit (changed
, from
))
1527 bitmap_set_bit (changed
, to
);
1528 varinfo_t fromvi
= get_varinfo (from
);
1529 if (fromvi
->solution
)
1531 /* If the solution changes because of the merging, we need to mark
1532 the variable as changed. */
1533 varinfo_t tovi
= get_varinfo (to
);
1534 if (bitmap_ior_into (tovi
->solution
, fromvi
->solution
))
1537 bitmap_set_bit (changed
, to
);
1540 BITMAP_FREE (fromvi
->solution
);
1541 if (fromvi
->oldsolution
)
1542 BITMAP_FREE (fromvi
->oldsolution
);
1544 if (stats
.iterations
> 0
1545 && tovi
->oldsolution
)
1546 BITMAP_FREE (tovi
->oldsolution
);
1548 if (graph
->succs
[to
])
1549 bitmap_clear_bit (graph
->succs
[to
], to
);
1552 /* Information needed to compute the topological ordering of a graph. */
1556 /* sbitmap of visited nodes. */
1558 /* Array that stores the topological order of the graph, *in
1560 vec
<unsigned> topo_order
;
1564 /* Initialize and return a topological info structure. */
1566 static struct topo_info
*
1567 init_topo_info (void)
1569 size_t size
= graph
->size
;
1570 struct topo_info
*ti
= XNEW (struct topo_info
);
1571 ti
->visited
= sbitmap_alloc (size
);
1572 bitmap_clear (ti
->visited
);
1573 ti
->topo_order
.create (1);
1578 /* Free the topological sort info pointed to by TI. */
1581 free_topo_info (struct topo_info
*ti
)
1583 sbitmap_free (ti
->visited
);
1584 ti
->topo_order
.release ();
1588 /* Visit the graph in topological order, and store the order in the
1589 topo_info structure. */
1592 topo_visit (constraint_graph_t graph
, struct topo_info
*ti
,
1598 bitmap_set_bit (ti
->visited
, n
);
1600 if (graph
->succs
[n
])
1601 EXECUTE_IF_SET_IN_BITMAP (graph
->succs
[n
], 0, j
, bi
)
1603 if (!bitmap_bit_p (ti
->visited
, j
))
1604 topo_visit (graph
, ti
, j
);
1607 ti
->topo_order
.safe_push (n
);
1610 /* Process a constraint C that represents x = *(y + off), using DELTA as the
1611 starting solution for y. */
1614 do_sd_constraint (constraint_graph_t graph
, constraint_t c
,
1615 bitmap delta
, bitmap
*expanded_delta
)
1617 unsigned int lhs
= c
->lhs
.var
;
1619 bitmap sol
= get_varinfo (lhs
)->solution
;
1622 HOST_WIDE_INT roffset
= c
->rhs
.offset
;
1624 /* Our IL does not allow this. */
1625 gcc_checking_assert (c
->lhs
.offset
== 0);
1627 /* If the solution of Y contains anything it is good enough to transfer
1629 if (bitmap_bit_p (delta
, anything_id
))
1631 flag
|= bitmap_set_bit (sol
, anything_id
);
1635 /* If we do not know at with offset the rhs is dereferenced compute
1636 the reachability set of DELTA, conservatively assuming it is
1637 dereferenced at all valid offsets. */
1638 if (roffset
== UNKNOWN_OFFSET
)
1640 delta
= solution_set_expand (delta
, expanded_delta
);
1641 /* No further offset processing is necessary. */
1645 /* For each variable j in delta (Sol(y)), add
1646 an edge in the graph from j to x, and union Sol(j) into Sol(x). */
1647 EXECUTE_IF_SET_IN_BITMAP (delta
, 0, j
, bi
)
1649 varinfo_t v
= get_varinfo (j
);
1650 HOST_WIDE_INT fieldoffset
= v
->offset
+ roffset
;
1651 unsigned HOST_WIDE_INT size
= v
->size
;
1656 else if (roffset
!= 0)
1658 if (fieldoffset
< 0)
1659 v
= get_varinfo (v
->head
);
1661 v
= first_or_preceding_vi_for_offset (v
, fieldoffset
);
1664 /* We have to include all fields that overlap the current field
1665 shifted by roffset. */
1670 /* Adding edges from the special vars is pointless.
1671 They don't have sets that can change. */
1672 if (get_varinfo (t
)->is_special_var
)
1673 flag
|= bitmap_ior_into (sol
, get_varinfo (t
)->solution
);
1674 /* Merging the solution from ESCAPED needlessly increases
1675 the set. Use ESCAPED as representative instead. */
1676 else if (v
->id
== escaped_id
)
1677 flag
|= bitmap_set_bit (sol
, escaped_id
);
1678 else if (v
->may_have_pointers
1679 && add_graph_edge (graph
, lhs
, t
))
1680 flag
|= bitmap_ior_into (sol
, get_varinfo (t
)->solution
);
1688 while (v
->offset
< fieldoffset
+ size
);
1692 /* If the LHS solution changed, mark the var as changed. */
1695 get_varinfo (lhs
)->solution
= sol
;
1696 bitmap_set_bit (changed
, lhs
);
1700 /* Process a constraint C that represents *(x + off) = y using DELTA
1701 as the starting solution for x. */
1704 do_ds_constraint (constraint_t c
, bitmap delta
, bitmap
*expanded_delta
)
1706 unsigned int rhs
= c
->rhs
.var
;
1707 bitmap sol
= get_varinfo (rhs
)->solution
;
1710 HOST_WIDE_INT loff
= c
->lhs
.offset
;
1711 bool escaped_p
= false;
1713 /* Our IL does not allow this. */
1714 gcc_checking_assert (c
->rhs
.offset
== 0);
1716 /* If the solution of y contains ANYTHING simply use the ANYTHING
1717 solution. This avoids needlessly increasing the points-to sets. */
1718 if (bitmap_bit_p (sol
, anything_id
))
1719 sol
= get_varinfo (find (anything_id
))->solution
;
1721 /* If the solution for x contains ANYTHING we have to merge the
1722 solution of y into all pointer variables which we do via
1724 if (bitmap_bit_p (delta
, anything_id
))
1726 unsigned t
= find (storedanything_id
);
1727 if (add_graph_edge (graph
, t
, rhs
))
1729 if (bitmap_ior_into (get_varinfo (t
)->solution
, sol
))
1730 bitmap_set_bit (changed
, t
);
1735 /* If we do not know at with offset the rhs is dereferenced compute
1736 the reachability set of DELTA, conservatively assuming it is
1737 dereferenced at all valid offsets. */
1738 if (loff
== UNKNOWN_OFFSET
)
1740 delta
= solution_set_expand (delta
, expanded_delta
);
1744 /* For each member j of delta (Sol(x)), add an edge from y to j and
1745 union Sol(y) into Sol(j) */
1746 EXECUTE_IF_SET_IN_BITMAP (delta
, 0, j
, bi
)
1748 varinfo_t v
= get_varinfo (j
);
1750 HOST_WIDE_INT fieldoffset
= v
->offset
+ loff
;
1751 unsigned HOST_WIDE_INT size
= v
->size
;
1757 if (fieldoffset
< 0)
1758 v
= get_varinfo (v
->head
);
1760 v
= first_or_preceding_vi_for_offset (v
, fieldoffset
);
1763 /* We have to include all fields that overlap the current field
1767 if (v
->may_have_pointers
)
1769 /* If v is a global variable then this is an escape point. */
1770 if (v
->is_global_var
1773 t
= find (escaped_id
);
1774 if (add_graph_edge (graph
, t
, rhs
)
1775 && bitmap_ior_into (get_varinfo (t
)->solution
, sol
))
1776 bitmap_set_bit (changed
, t
);
1777 /* Enough to let rhs escape once. */
1781 if (v
->is_special_var
)
1785 if (add_graph_edge (graph
, t
, rhs
)
1786 && bitmap_ior_into (get_varinfo (t
)->solution
, sol
))
1787 bitmap_set_bit (changed
, t
);
1796 while (v
->offset
< fieldoffset
+ size
);
1800 /* Handle a non-simple (simple meaning requires no iteration),
1801 constraint (IE *x = &y, x = *y, *x = y, and x = y with offsets involved). */
1804 do_complex_constraint (constraint_graph_t graph
, constraint_t c
, bitmap delta
,
1805 bitmap
*expanded_delta
)
1807 if (c
->lhs
.type
== DEREF
)
1809 if (c
->rhs
.type
== ADDRESSOF
)
1816 do_ds_constraint (c
, delta
, expanded_delta
);
1819 else if (c
->rhs
.type
== DEREF
)
1822 if (!(get_varinfo (c
->lhs
.var
)->is_special_var
))
1823 do_sd_constraint (graph
, c
, delta
, expanded_delta
);
1830 gcc_checking_assert (c
->rhs
.type
== SCALAR
&& c
->lhs
.type
== SCALAR
1831 && c
->rhs
.offset
!= 0 && c
->lhs
.offset
== 0);
1832 tmp
= get_varinfo (c
->lhs
.var
)->solution
;
1834 flag
= set_union_with_increment (tmp
, delta
, c
->rhs
.offset
,
1838 bitmap_set_bit (changed
, c
->lhs
.var
);
1842 /* Initialize and return a new SCC info structure. */
1844 static struct scc_info
*
1845 init_scc_info (size_t size
)
1847 struct scc_info
*si
= XNEW (struct scc_info
);
1850 si
->current_index
= 0;
1851 si
->visited
= sbitmap_alloc (size
);
1852 bitmap_clear (si
->visited
);
1853 si
->deleted
= sbitmap_alloc (size
);
1854 bitmap_clear (si
->deleted
);
1855 si
->node_mapping
= XNEWVEC (unsigned int, size
);
1856 si
->dfs
= XCNEWVEC (unsigned int, size
);
1858 for (i
= 0; i
< size
; i
++)
1859 si
->node_mapping
[i
] = i
;
1861 si
->scc_stack
.create (1);
1865 /* Free an SCC info structure pointed to by SI */
1868 free_scc_info (struct scc_info
*si
)
1870 sbitmap_free (si
->visited
);
1871 sbitmap_free (si
->deleted
);
1872 free (si
->node_mapping
);
1874 si
->scc_stack
.release ();
1879 /* Find indirect cycles in GRAPH that occur, using strongly connected
1880 components, and note them in the indirect cycles map.
1882 This technique comes from Ben Hardekopf and Calvin Lin,
1883 "It Pays to be Lazy: Fast and Accurate Pointer Analysis for Millions of
1884 Lines of Code", submitted to PLDI 2007. */
1887 find_indirect_cycles (constraint_graph_t graph
)
1890 unsigned int size
= graph
->size
;
1891 struct scc_info
*si
= init_scc_info (size
);
1893 for (i
= 0; i
< MIN (LAST_REF_NODE
, size
); i
++ )
1894 if (!bitmap_bit_p (si
->visited
, i
) && find (i
) == i
)
1895 scc_visit (graph
, si
, i
);
1900 /* Compute a topological ordering for GRAPH, and store the result in the
1901 topo_info structure TI. */
1904 compute_topo_order (constraint_graph_t graph
,
1905 struct topo_info
*ti
)
1908 unsigned int size
= graph
->size
;
1910 for (i
= 0; i
!= size
; ++i
)
1911 if (!bitmap_bit_p (ti
->visited
, i
) && find (i
) == i
)
1912 topo_visit (graph
, ti
, i
);
1915 /* Structure used to for hash value numbering of pointer equivalence
1918 typedef struct equiv_class_label
1921 unsigned int equivalence_class
;
1923 } *equiv_class_label_t
;
1924 typedef const struct equiv_class_label
*const_equiv_class_label_t
;
1926 /* Equiv_class_label hashtable helpers. */
1928 struct equiv_class_hasher
: typed_free_remove
<equiv_class_label
>
1930 typedef equiv_class_label
*value_type
;
1931 typedef equiv_class_label
*compare_type
;
1932 static inline hashval_t
hash (const equiv_class_label
*);
1933 static inline bool equal (const equiv_class_label
*,
1934 const equiv_class_label
*);
1937 /* Hash function for a equiv_class_label_t */
1940 equiv_class_hasher::hash (const equiv_class_label
*ecl
)
1942 return ecl
->hashcode
;
1945 /* Equality function for two equiv_class_label_t's. */
1948 equiv_class_hasher::equal (const equiv_class_label
*eql1
,
1949 const equiv_class_label
*eql2
)
1951 return (eql1
->hashcode
== eql2
->hashcode
1952 && bitmap_equal_p (eql1
->labels
, eql2
->labels
));
1955 /* A hashtable for mapping a bitmap of labels->pointer equivalence
1957 static hash_table
<equiv_class_hasher
> *pointer_equiv_class_table
;
1959 /* A hashtable for mapping a bitmap of labels->location equivalence
1961 static hash_table
<equiv_class_hasher
> *location_equiv_class_table
;
1963 /* Lookup a equivalence class in TABLE by the bitmap of LABELS with
1964 hash HAS it contains. Sets *REF_LABELS to the bitmap LABELS
1965 is equivalent to. */
1967 static equiv_class_label
*
1968 equiv_class_lookup_or_add (hash_table
<equiv_class_hasher
> *table
,
1971 equiv_class_label
**slot
;
1972 equiv_class_label ecl
;
1974 ecl
.labels
= labels
;
1975 ecl
.hashcode
= bitmap_hash (labels
);
1976 slot
= table
->find_slot (&ecl
, INSERT
);
1979 *slot
= XNEW (struct equiv_class_label
);
1980 (*slot
)->labels
= labels
;
1981 (*slot
)->hashcode
= ecl
.hashcode
;
1982 (*slot
)->equivalence_class
= 0;
1988 /* Perform offline variable substitution.
1990 This is a worst case quadratic time way of identifying variables
1991 that must have equivalent points-to sets, including those caused by
1992 static cycles, and single entry subgraphs, in the constraint graph.
1994 The technique is described in "Exploiting Pointer and Location
1995 Equivalence to Optimize Pointer Analysis. In the 14th International
1996 Static Analysis Symposium (SAS), August 2007." It is known as the
1997 "HU" algorithm, and is equivalent to value numbering the collapsed
1998 constraint graph including evaluating unions.
2000 The general method of finding equivalence classes is as follows:
2001 Add fake nodes (REF nodes) and edges for *a = b and a = *b constraints.
2002 Initialize all non-REF nodes to be direct nodes.
2003 For each constraint a = a U {b}, we set pts(a) = pts(a) u {fresh
2005 For each constraint containing the dereference, we also do the same
2008 We then compute SCC's in the graph and unify nodes in the same SCC,
2011 For each non-collapsed node x:
2012 Visit all unvisited explicit incoming edges.
2013 Ignoring all non-pointers, set pts(x) = Union of pts(a) for y
2015 Lookup the equivalence class for pts(x).
2016 If we found one, equivalence_class(x) = found class.
2017 Otherwise, equivalence_class(x) = new class, and new_class is
2018 added to the lookup table.
2020 All direct nodes with the same equivalence class can be replaced
2021 with a single representative node.
2022 All unlabeled nodes (label == 0) are not pointers and all edges
2023 involving them can be eliminated.
2024 We perform these optimizations during rewrite_constraints
2026 In addition to pointer equivalence class finding, we also perform
2027 location equivalence class finding. This is the set of variables
2028 that always appear together in points-to sets. We use this to
2029 compress the size of the points-to sets. */
2031 /* Current maximum pointer equivalence class id. */
2032 static int pointer_equiv_class
;
2034 /* Current maximum location equivalence class id. */
2035 static int location_equiv_class
;
2037 /* Recursive routine to find strongly connected components in GRAPH,
2038 and label it's nodes with DFS numbers. */
2041 condense_visit (constraint_graph_t graph
, struct scc_info
*si
, unsigned int n
)
2045 unsigned int my_dfs
;
2047 gcc_checking_assert (si
->node_mapping
[n
] == n
);
2048 bitmap_set_bit (si
->visited
, n
);
2049 si
->dfs
[n
] = si
->current_index
++;
2050 my_dfs
= si
->dfs
[n
];
2052 /* Visit all the successors. */
2053 EXECUTE_IF_IN_NONNULL_BITMAP (graph
->preds
[n
], 0, i
, bi
)
2055 unsigned int w
= si
->node_mapping
[i
];
2057 if (bitmap_bit_p (si
->deleted
, w
))
2060 if (!bitmap_bit_p (si
->visited
, w
))
2061 condense_visit (graph
, si
, w
);
2063 unsigned int t
= si
->node_mapping
[w
];
2064 gcc_checking_assert (si
->node_mapping
[n
] == n
);
2065 if (si
->dfs
[t
] < si
->dfs
[n
])
2066 si
->dfs
[n
] = si
->dfs
[t
];
2069 /* Visit all the implicit predecessors. */
2070 EXECUTE_IF_IN_NONNULL_BITMAP (graph
->implicit_preds
[n
], 0, i
, bi
)
2072 unsigned int w
= si
->node_mapping
[i
];
2074 if (bitmap_bit_p (si
->deleted
, w
))
2077 if (!bitmap_bit_p (si
->visited
, w
))
2078 condense_visit (graph
, si
, w
);
2080 unsigned int t
= si
->node_mapping
[w
];
2081 gcc_assert (si
->node_mapping
[n
] == n
);
2082 if (si
->dfs
[t
] < si
->dfs
[n
])
2083 si
->dfs
[n
] = si
->dfs
[t
];
2086 /* See if any components have been identified. */
2087 if (si
->dfs
[n
] == my_dfs
)
2089 while (si
->scc_stack
.length () != 0
2090 && si
->dfs
[si
->scc_stack
.last ()] >= my_dfs
)
2092 unsigned int w
= si
->scc_stack
.pop ();
2093 si
->node_mapping
[w
] = n
;
2095 if (!bitmap_bit_p (graph
->direct_nodes
, w
))
2096 bitmap_clear_bit (graph
->direct_nodes
, n
);
2098 /* Unify our nodes. */
2099 if (graph
->preds
[w
])
2101 if (!graph
->preds
[n
])
2102 graph
->preds
[n
] = BITMAP_ALLOC (&predbitmap_obstack
);
2103 bitmap_ior_into (graph
->preds
[n
], graph
->preds
[w
]);
2105 if (graph
->implicit_preds
[w
])
2107 if (!graph
->implicit_preds
[n
])
2108 graph
->implicit_preds
[n
] = BITMAP_ALLOC (&predbitmap_obstack
);
2109 bitmap_ior_into (graph
->implicit_preds
[n
],
2110 graph
->implicit_preds
[w
]);
2112 if (graph
->points_to
[w
])
2114 if (!graph
->points_to
[n
])
2115 graph
->points_to
[n
] = BITMAP_ALLOC (&predbitmap_obstack
);
2116 bitmap_ior_into (graph
->points_to
[n
],
2117 graph
->points_to
[w
]);
2120 bitmap_set_bit (si
->deleted
, n
);
2123 si
->scc_stack
.safe_push (n
);
2126 /* Label pointer equivalences.
2128 This performs a value numbering of the constraint graph to
2129 discover which variables will always have the same points-to sets
2130 under the current set of constraints.
2132 The way it value numbers is to store the set of points-to bits
2133 generated by the constraints and graph edges. This is just used as a
2134 hash and equality comparison. The *actual set of points-to bits* is
2135 completely irrelevant, in that we don't care about being able to
2138 The equality values (currently bitmaps) just have to satisfy a few
2139 constraints, the main ones being:
2140 1. The combining operation must be order independent.
2141 2. The end result of a given set of operations must be unique iff the
2142 combination of input values is unique
2146 label_visit (constraint_graph_t graph
, struct scc_info
*si
, unsigned int n
)
2148 unsigned int i
, first_pred
;
2151 bitmap_set_bit (si
->visited
, n
);
2153 /* Label and union our incoming edges's points to sets. */
2155 EXECUTE_IF_IN_NONNULL_BITMAP (graph
->preds
[n
], 0, i
, bi
)
2157 unsigned int w
= si
->node_mapping
[i
];
2158 if (!bitmap_bit_p (si
->visited
, w
))
2159 label_visit (graph
, si
, w
);
2161 /* Skip unused edges */
2162 if (w
== n
|| graph
->pointer_label
[w
] == 0)
2165 if (graph
->points_to
[w
])
2167 if (!graph
->points_to
[n
])
2169 if (first_pred
== -1U)
2173 graph
->points_to
[n
] = BITMAP_ALLOC (&predbitmap_obstack
);
2174 bitmap_ior (graph
->points_to
[n
],
2175 graph
->points_to
[first_pred
],
2176 graph
->points_to
[w
]);
2180 bitmap_ior_into (graph
->points_to
[n
], graph
->points_to
[w
]);
2184 /* Indirect nodes get fresh variables and a new pointer equiv class. */
2185 if (!bitmap_bit_p (graph
->direct_nodes
, n
))
2187 if (!graph
->points_to
[n
])
2189 graph
->points_to
[n
] = BITMAP_ALLOC (&predbitmap_obstack
);
2190 if (first_pred
!= -1U)
2191 bitmap_copy (graph
->points_to
[n
], graph
->points_to
[first_pred
]);
2193 bitmap_set_bit (graph
->points_to
[n
], FIRST_REF_NODE
+ n
);
2194 graph
->pointer_label
[n
] = pointer_equiv_class
++;
2195 equiv_class_label_t ecl
;
2196 ecl
= equiv_class_lookup_or_add (pointer_equiv_class_table
,
2197 graph
->points_to
[n
]);
2198 ecl
->equivalence_class
= graph
->pointer_label
[n
];
2202 /* If there was only a single non-empty predecessor the pointer equiv
2203 class is the same. */
2204 if (!graph
->points_to
[n
])
2206 if (first_pred
!= -1U)
2208 graph
->pointer_label
[n
] = graph
->pointer_label
[first_pred
];
2209 graph
->points_to
[n
] = graph
->points_to
[first_pred
];
2214 if (!bitmap_empty_p (graph
->points_to
[n
]))
2216 equiv_class_label_t ecl
;
2217 ecl
= equiv_class_lookup_or_add (pointer_equiv_class_table
,
2218 graph
->points_to
[n
]);
2219 if (ecl
->equivalence_class
== 0)
2220 ecl
->equivalence_class
= pointer_equiv_class
++;
2223 BITMAP_FREE (graph
->points_to
[n
]);
2224 graph
->points_to
[n
] = ecl
->labels
;
2226 graph
->pointer_label
[n
] = ecl
->equivalence_class
;
2230 /* Print the pred graph in dot format. */
2233 dump_pred_graph (struct scc_info
*si
, FILE *file
)
2237 /* Only print the graph if it has already been initialized: */
2241 /* Prints the header of the dot file: */
2242 fprintf (file
, "strict digraph {\n");
2243 fprintf (file
, " node [\n shape = box\n ]\n");
2244 fprintf (file
, " edge [\n fontsize = \"12\"\n ]\n");
2245 fprintf (file
, "\n // List of nodes and complex constraints in "
2246 "the constraint graph:\n");
2248 /* The next lines print the nodes in the graph together with the
2249 complex constraints attached to them. */
2250 for (i
= 1; i
< graph
->size
; i
++)
2252 if (i
== FIRST_REF_NODE
)
2254 if (si
->node_mapping
[i
] != i
)
2256 if (i
< FIRST_REF_NODE
)
2257 fprintf (file
, "\"%s\"", get_varinfo (i
)->name
);
2259 fprintf (file
, "\"*%s\"", get_varinfo (i
- FIRST_REF_NODE
)->name
);
2260 if (graph
->points_to
[i
]
2261 && !bitmap_empty_p (graph
->points_to
[i
]))
2263 fprintf (file
, "[label=\"%s = {", get_varinfo (i
)->name
);
2266 EXECUTE_IF_SET_IN_BITMAP (graph
->points_to
[i
], 0, j
, bi
)
2267 fprintf (file
, " %d", j
);
2268 fprintf (file
, " }\"]");
2270 fprintf (file
, ";\n");
2273 /* Go over the edges. */
2274 fprintf (file
, "\n // Edges in the constraint graph:\n");
2275 for (i
= 1; i
< graph
->size
; i
++)
2279 if (si
->node_mapping
[i
] != i
)
2281 EXECUTE_IF_IN_NONNULL_BITMAP (graph
->preds
[i
], 0, j
, bi
)
2283 unsigned from
= si
->node_mapping
[j
];
2284 if (from
< FIRST_REF_NODE
)
2285 fprintf (file
, "\"%s\"", get_varinfo (from
)->name
);
2287 fprintf (file
, "\"*%s\"", get_varinfo (from
- FIRST_REF_NODE
)->name
);
2288 fprintf (file
, " -> ");
2289 if (i
< FIRST_REF_NODE
)
2290 fprintf (file
, "\"%s\"", get_varinfo (i
)->name
);
2292 fprintf (file
, "\"*%s\"", get_varinfo (i
- FIRST_REF_NODE
)->name
);
2293 fprintf (file
, ";\n");
2297 /* Prints the tail of the dot file. */
2298 fprintf (file
, "}\n");
2301 /* Perform offline variable substitution, discovering equivalence
2302 classes, and eliminating non-pointer variables. */
2304 static struct scc_info
*
2305 perform_var_substitution (constraint_graph_t graph
)
2308 unsigned int size
= graph
->size
;
2309 struct scc_info
*si
= init_scc_info (size
);
2311 bitmap_obstack_initialize (&iteration_obstack
);
2312 pointer_equiv_class_table
= new hash_table
<equiv_class_hasher
> (511);
2313 location_equiv_class_table
2314 = new hash_table
<equiv_class_hasher
> (511);
2315 pointer_equiv_class
= 1;
2316 location_equiv_class
= 1;
2318 /* Condense the nodes, which means to find SCC's, count incoming
2319 predecessors, and unite nodes in SCC's. */
2320 for (i
= 1; i
< FIRST_REF_NODE
; i
++)
2321 if (!bitmap_bit_p (si
->visited
, si
->node_mapping
[i
]))
2322 condense_visit (graph
, si
, si
->node_mapping
[i
]);
2324 if (dump_file
&& (dump_flags
& TDF_GRAPH
))
2326 fprintf (dump_file
, "\n\n// The constraint graph before var-substitution "
2327 "in dot format:\n");
2328 dump_pred_graph (si
, dump_file
);
2329 fprintf (dump_file
, "\n\n");
2332 bitmap_clear (si
->visited
);
2333 /* Actually the label the nodes for pointer equivalences */
2334 for (i
= 1; i
< FIRST_REF_NODE
; i
++)
2335 if (!bitmap_bit_p (si
->visited
, si
->node_mapping
[i
]))
2336 label_visit (graph
, si
, si
->node_mapping
[i
]);
2338 /* Calculate location equivalence labels. */
2339 for (i
= 1; i
< FIRST_REF_NODE
; i
++)
2345 if (!graph
->pointed_by
[i
])
2347 pointed_by
= BITMAP_ALLOC (&iteration_obstack
);
2349 /* Translate the pointed-by mapping for pointer equivalence
2351 EXECUTE_IF_SET_IN_BITMAP (graph
->pointed_by
[i
], 0, j
, bi
)
2353 bitmap_set_bit (pointed_by
,
2354 graph
->pointer_label
[si
->node_mapping
[j
]]);
2356 /* The original pointed_by is now dead. */
2357 BITMAP_FREE (graph
->pointed_by
[i
]);
2359 /* Look up the location equivalence label if one exists, or make
2361 equiv_class_label_t ecl
;
2362 ecl
= equiv_class_lookup_or_add (location_equiv_class_table
, pointed_by
);
2363 if (ecl
->equivalence_class
== 0)
2364 ecl
->equivalence_class
= location_equiv_class
++;
2367 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2368 fprintf (dump_file
, "Found location equivalence for node %s\n",
2369 get_varinfo (i
)->name
);
2370 BITMAP_FREE (pointed_by
);
2372 graph
->loc_label
[i
] = ecl
->equivalence_class
;
2376 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2377 for (i
= 1; i
< FIRST_REF_NODE
; i
++)
2379 unsigned j
= si
->node_mapping
[i
];
2382 fprintf (dump_file
, "%s node id %d ",
2383 bitmap_bit_p (graph
->direct_nodes
, i
)
2384 ? "Direct" : "Indirect", i
);
2385 if (i
< FIRST_REF_NODE
)
2386 fprintf (dump_file
, "\"%s\"", get_varinfo (i
)->name
);
2388 fprintf (dump_file
, "\"*%s\"",
2389 get_varinfo (i
- FIRST_REF_NODE
)->name
);
2390 fprintf (dump_file
, " mapped to SCC leader node id %d ", j
);
2391 if (j
< FIRST_REF_NODE
)
2392 fprintf (dump_file
, "\"%s\"\n", get_varinfo (j
)->name
);
2394 fprintf (dump_file
, "\"*%s\"\n",
2395 get_varinfo (j
- FIRST_REF_NODE
)->name
);
2400 "Equivalence classes for %s node id %d ",
2401 bitmap_bit_p (graph
->direct_nodes
, i
)
2402 ? "direct" : "indirect", i
);
2403 if (i
< FIRST_REF_NODE
)
2404 fprintf (dump_file
, "\"%s\"", get_varinfo (i
)->name
);
2406 fprintf (dump_file
, "\"*%s\"",
2407 get_varinfo (i
- FIRST_REF_NODE
)->name
);
2409 ": pointer %d, location %d\n",
2410 graph
->pointer_label
[i
], graph
->loc_label
[i
]);
2414 /* Quickly eliminate our non-pointer variables. */
2416 for (i
= 1; i
< FIRST_REF_NODE
; i
++)
2418 unsigned int node
= si
->node_mapping
[i
];
2420 if (graph
->pointer_label
[node
] == 0)
2422 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2424 "%s is a non-pointer variable, eliminating edges.\n",
2425 get_varinfo (node
)->name
);
2426 stats
.nonpointer_vars
++;
2427 clear_edges_for_node (graph
, node
);
2434 /* Free information that was only necessary for variable
2438 free_var_substitution_info (struct scc_info
*si
)
2441 free (graph
->pointer_label
);
2442 free (graph
->loc_label
);
2443 free (graph
->pointed_by
);
2444 free (graph
->points_to
);
2445 free (graph
->eq_rep
);
2446 sbitmap_free (graph
->direct_nodes
);
2447 delete pointer_equiv_class_table
;
2448 pointer_equiv_class_table
= NULL
;
2449 delete location_equiv_class_table
;
2450 location_equiv_class_table
= NULL
;
2451 bitmap_obstack_release (&iteration_obstack
);
2454 /* Return an existing node that is equivalent to NODE, which has
2455 equivalence class LABEL, if one exists. Return NODE otherwise. */
2458 find_equivalent_node (constraint_graph_t graph
,
2459 unsigned int node
, unsigned int label
)
2461 /* If the address version of this variable is unused, we can
2462 substitute it for anything else with the same label.
2463 Otherwise, we know the pointers are equivalent, but not the
2464 locations, and we can unite them later. */
2466 if (!bitmap_bit_p (graph
->address_taken
, node
))
2468 gcc_checking_assert (label
< graph
->size
);
2470 if (graph
->eq_rep
[label
] != -1)
2472 /* Unify the two variables since we know they are equivalent. */
2473 if (unite (graph
->eq_rep
[label
], node
))
2474 unify_nodes (graph
, graph
->eq_rep
[label
], node
, false);
2475 return graph
->eq_rep
[label
];
2479 graph
->eq_rep
[label
] = node
;
2480 graph
->pe_rep
[label
] = node
;
2485 gcc_checking_assert (label
< graph
->size
);
2486 graph
->pe
[node
] = label
;
2487 if (graph
->pe_rep
[label
] == -1)
2488 graph
->pe_rep
[label
] = node
;
2494 /* Unite pointer equivalent but not location equivalent nodes in
2495 GRAPH. This may only be performed once variable substitution is
2499 unite_pointer_equivalences (constraint_graph_t graph
)
2503 /* Go through the pointer equivalences and unite them to their
2504 representative, if they aren't already. */
2505 for (i
= 1; i
< FIRST_REF_NODE
; i
++)
2507 unsigned int label
= graph
->pe
[i
];
2510 int label_rep
= graph
->pe_rep
[label
];
2512 if (label_rep
== -1)
2515 label_rep
= find (label_rep
);
2516 if (label_rep
>= 0 && unite (label_rep
, find (i
)))
2517 unify_nodes (graph
, label_rep
, i
, false);
2522 /* Move complex constraints to the GRAPH nodes they belong to. */
2525 move_complex_constraints (constraint_graph_t graph
)
2530 FOR_EACH_VEC_ELT (constraints
, i
, c
)
2534 struct constraint_expr lhs
= c
->lhs
;
2535 struct constraint_expr rhs
= c
->rhs
;
2537 if (lhs
.type
== DEREF
)
2539 insert_into_complex (graph
, lhs
.var
, c
);
2541 else if (rhs
.type
== DEREF
)
2543 if (!(get_varinfo (lhs
.var
)->is_special_var
))
2544 insert_into_complex (graph
, rhs
.var
, c
);
2546 else if (rhs
.type
!= ADDRESSOF
&& lhs
.var
> anything_id
2547 && (lhs
.offset
!= 0 || rhs
.offset
!= 0))
2549 insert_into_complex (graph
, rhs
.var
, c
);
2556 /* Optimize and rewrite complex constraints while performing
2557 collapsing of equivalent nodes. SI is the SCC_INFO that is the
2558 result of perform_variable_substitution. */
2561 rewrite_constraints (constraint_graph_t graph
,
2562 struct scc_info
*si
)
2567 #ifdef ENABLE_CHECKING
2568 for (unsigned int j
= 0; j
< graph
->size
; j
++)
2569 gcc_assert (find (j
) == j
);
2572 FOR_EACH_VEC_ELT (constraints
, i
, c
)
2574 struct constraint_expr lhs
= c
->lhs
;
2575 struct constraint_expr rhs
= c
->rhs
;
2576 unsigned int lhsvar
= find (lhs
.var
);
2577 unsigned int rhsvar
= find (rhs
.var
);
2578 unsigned int lhsnode
, rhsnode
;
2579 unsigned int lhslabel
, rhslabel
;
2581 lhsnode
= si
->node_mapping
[lhsvar
];
2582 rhsnode
= si
->node_mapping
[rhsvar
];
2583 lhslabel
= graph
->pointer_label
[lhsnode
];
2584 rhslabel
= graph
->pointer_label
[rhsnode
];
2586 /* See if it is really a non-pointer variable, and if so, ignore
2590 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2593 fprintf (dump_file
, "%s is a non-pointer variable,"
2594 "ignoring constraint:",
2595 get_varinfo (lhs
.var
)->name
);
2596 dump_constraint (dump_file
, c
);
2597 fprintf (dump_file
, "\n");
2599 constraints
[i
] = NULL
;
2605 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2608 fprintf (dump_file
, "%s is a non-pointer variable,"
2609 "ignoring constraint:",
2610 get_varinfo (rhs
.var
)->name
);
2611 dump_constraint (dump_file
, c
);
2612 fprintf (dump_file
, "\n");
2614 constraints
[i
] = NULL
;
2618 lhsvar
= find_equivalent_node (graph
, lhsvar
, lhslabel
);
2619 rhsvar
= find_equivalent_node (graph
, rhsvar
, rhslabel
);
2620 c
->lhs
.var
= lhsvar
;
2621 c
->rhs
.var
= rhsvar
;
2625 /* Eliminate indirect cycles involving NODE. Return true if NODE was
2626 part of an SCC, false otherwise. */
2629 eliminate_indirect_cycles (unsigned int node
)
2631 if (graph
->indirect_cycles
[node
] != -1
2632 && !bitmap_empty_p (get_varinfo (node
)->solution
))
2635 auto_vec
<unsigned> queue
;
2637 unsigned int to
= find (graph
->indirect_cycles
[node
]);
2640 /* We can't touch the solution set and call unify_nodes
2641 at the same time, because unify_nodes is going to do
2642 bitmap unions into it. */
2644 EXECUTE_IF_SET_IN_BITMAP (get_varinfo (node
)->solution
, 0, i
, bi
)
2646 if (find (i
) == i
&& i
!= to
)
2649 queue
.safe_push (i
);
2654 queue
.iterate (queuepos
, &i
);
2657 unify_nodes (graph
, to
, i
, true);
2664 /* Solve the constraint graph GRAPH using our worklist solver.
2665 This is based on the PW* family of solvers from the "Efficient Field
2666 Sensitive Pointer Analysis for C" paper.
2667 It works by iterating over all the graph nodes, processing the complex
2668 constraints and propagating the copy constraints, until everything stops
2669 changed. This corresponds to steps 6-8 in the solving list given above. */
2672 solve_graph (constraint_graph_t graph
)
2674 unsigned int size
= graph
->size
;
2678 changed
= BITMAP_ALLOC (NULL
);
2680 /* Mark all initial non-collapsed nodes as changed. */
2681 for (i
= 1; i
< size
; i
++)
2683 varinfo_t ivi
= get_varinfo (i
);
2684 if (find (i
) == i
&& !bitmap_empty_p (ivi
->solution
)
2685 && ((graph
->succs
[i
] && !bitmap_empty_p (graph
->succs
[i
]))
2686 || graph
->complex[i
].length () > 0))
2687 bitmap_set_bit (changed
, i
);
2690 /* Allocate a bitmap to be used to store the changed bits. */
2691 pts
= BITMAP_ALLOC (&pta_obstack
);
2693 while (!bitmap_empty_p (changed
))
2696 struct topo_info
*ti
= init_topo_info ();
2699 bitmap_obstack_initialize (&iteration_obstack
);
2701 compute_topo_order (graph
, ti
);
2703 while (ti
->topo_order
.length () != 0)
2706 i
= ti
->topo_order
.pop ();
2708 /* If this variable is not a representative, skip it. */
2712 /* In certain indirect cycle cases, we may merge this
2713 variable to another. */
2714 if (eliminate_indirect_cycles (i
) && find (i
) != i
)
2717 /* If the node has changed, we need to process the
2718 complex constraints and outgoing edges again. */
2719 if (bitmap_clear_bit (changed
, i
))
2724 vec
<constraint_t
> complex = graph
->complex[i
];
2725 varinfo_t vi
= get_varinfo (i
);
2726 bool solution_empty
;
2728 /* Compute the changed set of solution bits. If anything
2729 is in the solution just propagate that. */
2730 if (bitmap_bit_p (vi
->solution
, anything_id
))
2732 /* If anything is also in the old solution there is
2734 ??? But we shouldn't ended up with "changed" set ... */
2736 && bitmap_bit_p (vi
->oldsolution
, anything_id
))
2738 bitmap_copy (pts
, get_varinfo (find (anything_id
))->solution
);
2740 else if (vi
->oldsolution
)
2741 bitmap_and_compl (pts
, vi
->solution
, vi
->oldsolution
);
2743 bitmap_copy (pts
, vi
->solution
);
2745 if (bitmap_empty_p (pts
))
2748 if (vi
->oldsolution
)
2749 bitmap_ior_into (vi
->oldsolution
, pts
);
2752 vi
->oldsolution
= BITMAP_ALLOC (&oldpta_obstack
);
2753 bitmap_copy (vi
->oldsolution
, pts
);
2756 solution
= vi
->solution
;
2757 solution_empty
= bitmap_empty_p (solution
);
2759 /* Process the complex constraints */
2760 bitmap expanded_pts
= NULL
;
2761 FOR_EACH_VEC_ELT (complex, j
, c
)
2763 /* XXX: This is going to unsort the constraints in
2764 some cases, which will occasionally add duplicate
2765 constraints during unification. This does not
2766 affect correctness. */
2767 c
->lhs
.var
= find (c
->lhs
.var
);
2768 c
->rhs
.var
= find (c
->rhs
.var
);
2770 /* The only complex constraint that can change our
2771 solution to non-empty, given an empty solution,
2772 is a constraint where the lhs side is receiving
2773 some set from elsewhere. */
2774 if (!solution_empty
|| c
->lhs
.type
!= DEREF
)
2775 do_complex_constraint (graph
, c
, pts
, &expanded_pts
);
2777 BITMAP_FREE (expanded_pts
);
2779 solution_empty
= bitmap_empty_p (solution
);
2781 if (!solution_empty
)
2784 unsigned eff_escaped_id
= find (escaped_id
);
2786 /* Propagate solution to all successors. */
2787 EXECUTE_IF_IN_NONNULL_BITMAP (graph
->succs
[i
],
2793 unsigned int to
= find (j
);
2794 tmp
= get_varinfo (to
)->solution
;
2797 /* Don't try to propagate to ourselves. */
2801 /* If we propagate from ESCAPED use ESCAPED as
2803 if (i
== eff_escaped_id
)
2804 flag
= bitmap_set_bit (tmp
, escaped_id
);
2806 flag
= bitmap_ior_into (tmp
, pts
);
2809 bitmap_set_bit (changed
, to
);
2814 free_topo_info (ti
);
2815 bitmap_obstack_release (&iteration_obstack
);
2819 BITMAP_FREE (changed
);
2820 bitmap_obstack_release (&oldpta_obstack
);
2823 /* Map from trees to variable infos. */
2824 static hash_map
<tree
, varinfo_t
> *vi_for_tree
;
2827 /* Insert ID as the variable id for tree T in the vi_for_tree map. */
2830 insert_vi_for_tree (tree t
, varinfo_t vi
)
2833 gcc_assert (!vi_for_tree
->put (t
, vi
));
2836 /* Find the variable info for tree T in VI_FOR_TREE. If T does not
2837 exist in the map, return NULL, otherwise, return the varinfo we found. */
2840 lookup_vi_for_tree (tree t
)
2842 varinfo_t
*slot
= vi_for_tree
->get (t
);
2849 /* Return a printable name for DECL */
2852 alias_get_name (tree decl
)
2854 const char *res
= NULL
;
2856 int num_printed
= 0;
2861 if (TREE_CODE (decl
) == SSA_NAME
)
2863 res
= get_name (decl
);
2865 num_printed
= asprintf (&temp
, "%s_%u", res
, SSA_NAME_VERSION (decl
));
2867 num_printed
= asprintf (&temp
, "_%u", SSA_NAME_VERSION (decl
));
2868 if (num_printed
> 0)
2870 res
= ggc_strdup (temp
);
2874 else if (DECL_P (decl
))
2876 if (DECL_ASSEMBLER_NAME_SET_P (decl
))
2877 res
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
2880 res
= get_name (decl
);
2883 num_printed
= asprintf (&temp
, "D.%u", DECL_UID (decl
));
2884 if (num_printed
> 0)
2886 res
= ggc_strdup (temp
);
2898 /* Find the variable id for tree T in the map.
2899 If T doesn't exist in the map, create an entry for it and return it. */
2902 get_vi_for_tree (tree t
)
2904 varinfo_t
*slot
= vi_for_tree
->get (t
);
2906 return get_varinfo (create_variable_info_for (t
, alias_get_name (t
)));
2911 /* Get a scalar constraint expression for a new temporary variable. */
2913 static struct constraint_expr
2914 new_scalar_tmp_constraint_exp (const char *name
)
2916 struct constraint_expr tmp
;
2919 vi
= new_var_info (NULL_TREE
, name
);
2923 vi
->is_full_var
= 1;
2932 /* Get a constraint expression vector from an SSA_VAR_P node.
2933 If address_p is true, the result will be taken its address of. */
2936 get_constraint_for_ssa_var (tree t
, vec
<ce_s
> *results
, bool address_p
)
2938 struct constraint_expr cexpr
;
2941 /* We allow FUNCTION_DECLs here even though it doesn't make much sense. */
2942 gcc_assert (TREE_CODE (t
) == SSA_NAME
|| DECL_P (t
));
2944 /* For parameters, get at the points-to set for the actual parm
2946 if (TREE_CODE (t
) == SSA_NAME
2947 && SSA_NAME_IS_DEFAULT_DEF (t
)
2948 && (TREE_CODE (SSA_NAME_VAR (t
)) == PARM_DECL
2949 || TREE_CODE (SSA_NAME_VAR (t
)) == RESULT_DECL
))
2951 get_constraint_for_ssa_var (SSA_NAME_VAR (t
), results
, address_p
);
2955 /* For global variables resort to the alias target. */
2956 if (TREE_CODE (t
) == VAR_DECL
2957 && (TREE_STATIC (t
) || DECL_EXTERNAL (t
)))
2959 varpool_node
*node
= varpool_node::get (t
);
2960 if (node
&& node
->alias
&& node
->analyzed
)
2962 node
= node
->ultimate_alias_target ();
2967 vi
= get_vi_for_tree (t
);
2969 cexpr
.type
= SCALAR
;
2972 /* If we are not taking the address of the constraint expr, add all
2973 sub-fiels of the variable as well. */
2975 && !vi
->is_full_var
)
2977 for (; vi
; vi
= vi_next (vi
))
2980 results
->safe_push (cexpr
);
2985 results
->safe_push (cexpr
);
2988 /* Process constraint T, performing various simplifications and then
2989 adding it to our list of overall constraints. */
2992 process_constraint (constraint_t t
)
2994 struct constraint_expr rhs
= t
->rhs
;
2995 struct constraint_expr lhs
= t
->lhs
;
2997 gcc_assert (rhs
.var
< varmap
.length ());
2998 gcc_assert (lhs
.var
< varmap
.length ());
3000 /* If we didn't get any useful constraint from the lhs we get
3001 &ANYTHING as fallback from get_constraint_for. Deal with
3002 it here by turning it into *ANYTHING. */
3003 if (lhs
.type
== ADDRESSOF
3004 && lhs
.var
== anything_id
)
3007 /* ADDRESSOF on the lhs is invalid. */
3008 gcc_assert (lhs
.type
!= ADDRESSOF
);
3010 /* We shouldn't add constraints from things that cannot have pointers.
3011 It's not completely trivial to avoid in the callers, so do it here. */
3012 if (rhs
.type
!= ADDRESSOF
3013 && !get_varinfo (rhs
.var
)->may_have_pointers
)
3016 /* Likewise adding to the solution of a non-pointer var isn't useful. */
3017 if (!get_varinfo (lhs
.var
)->may_have_pointers
)
3020 /* This can happen in our IR with things like n->a = *p */
3021 if (rhs
.type
== DEREF
&& lhs
.type
== DEREF
&& rhs
.var
!= anything_id
)
3023 /* Split into tmp = *rhs, *lhs = tmp */
3024 struct constraint_expr tmplhs
;
3025 tmplhs
= new_scalar_tmp_constraint_exp ("doubledereftmp");
3026 process_constraint (new_constraint (tmplhs
, rhs
));
3027 process_constraint (new_constraint (lhs
, tmplhs
));
3029 else if (rhs
.type
== ADDRESSOF
&& lhs
.type
== DEREF
)
3031 /* Split into tmp = &rhs, *lhs = tmp */
3032 struct constraint_expr tmplhs
;
3033 tmplhs
= new_scalar_tmp_constraint_exp ("derefaddrtmp");
3034 process_constraint (new_constraint (tmplhs
, rhs
));
3035 process_constraint (new_constraint (lhs
, tmplhs
));
3039 gcc_assert (rhs
.type
!= ADDRESSOF
|| rhs
.offset
== 0);
3040 constraints
.safe_push (t
);
3045 /* Return the position, in bits, of FIELD_DECL from the beginning of its
3048 static HOST_WIDE_INT
3049 bitpos_of_field (const tree fdecl
)
3051 if (!tree_fits_shwi_p (DECL_FIELD_OFFSET (fdecl
))
3052 || !tree_fits_shwi_p (DECL_FIELD_BIT_OFFSET (fdecl
)))
3055 return (tree_to_shwi (DECL_FIELD_OFFSET (fdecl
)) * BITS_PER_UNIT
3056 + tree_to_shwi (DECL_FIELD_BIT_OFFSET (fdecl
)));
3060 /* Get constraint expressions for offsetting PTR by OFFSET. Stores the
3061 resulting constraint expressions in *RESULTS. */
3064 get_constraint_for_ptr_offset (tree ptr
, tree offset
,
3067 struct constraint_expr c
;
3069 HOST_WIDE_INT rhsoffset
;
3071 /* If we do not do field-sensitive PTA adding offsets to pointers
3072 does not change the points-to solution. */
3073 if (!use_field_sensitive
)
3075 get_constraint_for_rhs (ptr
, results
);
3079 /* If the offset is not a non-negative integer constant that fits
3080 in a HOST_WIDE_INT, we have to fall back to a conservative
3081 solution which includes all sub-fields of all pointed-to
3082 variables of ptr. */
3083 if (offset
== NULL_TREE
3084 || TREE_CODE (offset
) != INTEGER_CST
)
3085 rhsoffset
= UNKNOWN_OFFSET
;
3088 /* Sign-extend the offset. */
3089 offset_int soffset
= offset_int::from (offset
, SIGNED
);
3090 if (!wi::fits_shwi_p (soffset
))
3091 rhsoffset
= UNKNOWN_OFFSET
;
3094 /* Make sure the bit-offset also fits. */
3095 HOST_WIDE_INT rhsunitoffset
= soffset
.to_shwi ();
3096 rhsoffset
= rhsunitoffset
* BITS_PER_UNIT
;
3097 if (rhsunitoffset
!= rhsoffset
/ BITS_PER_UNIT
)
3098 rhsoffset
= UNKNOWN_OFFSET
;
3102 get_constraint_for_rhs (ptr
, results
);
3106 /* As we are eventually appending to the solution do not use
3107 vec::iterate here. */
3108 n
= results
->length ();
3109 for (j
= 0; j
< n
; j
++)
3113 curr
= get_varinfo (c
.var
);
3115 if (c
.type
== ADDRESSOF
3116 /* If this varinfo represents a full variable just use it. */
3117 && curr
->is_full_var
)
3119 else if (c
.type
== ADDRESSOF
3120 /* If we do not know the offset add all subfields. */
3121 && rhsoffset
== UNKNOWN_OFFSET
)
3123 varinfo_t temp
= get_varinfo (curr
->head
);
3126 struct constraint_expr c2
;
3128 c2
.type
= ADDRESSOF
;
3130 if (c2
.var
!= c
.var
)
3131 results
->safe_push (c2
);
3132 temp
= vi_next (temp
);
3136 else if (c
.type
== ADDRESSOF
)
3139 unsigned HOST_WIDE_INT offset
= curr
->offset
+ rhsoffset
;
3141 /* If curr->offset + rhsoffset is less than zero adjust it. */
3143 && curr
->offset
< offset
)
3146 /* We have to include all fields that overlap the current
3147 field shifted by rhsoffset. And we include at least
3148 the last or the first field of the variable to represent
3149 reachability of off-bound addresses, in particular &object + 1,
3150 conservatively correct. */
3151 temp
= first_or_preceding_vi_for_offset (curr
, offset
);
3154 temp
= vi_next (temp
);
3156 && temp
->offset
< offset
+ curr
->size
)
3158 struct constraint_expr c2
;
3160 c2
.type
= ADDRESSOF
;
3162 results
->safe_push (c2
);
3163 temp
= vi_next (temp
);
3166 else if (c
.type
== SCALAR
)
3168 gcc_assert (c
.offset
== 0);
3169 c
.offset
= rhsoffset
;
3172 /* We shouldn't get any DEREFs here. */
3180 /* Given a COMPONENT_REF T, return the constraint_expr vector for it.
3181 If address_p is true the result will be taken its address of.
3182 If lhs_p is true then the constraint expression is assumed to be used
3186 get_constraint_for_component_ref (tree t
, vec
<ce_s
> *results
,
3187 bool address_p
, bool lhs_p
)
3190 HOST_WIDE_INT bitsize
= -1;
3191 HOST_WIDE_INT bitmaxsize
= -1;
3192 HOST_WIDE_INT bitpos
;
3195 /* Some people like to do cute things like take the address of
3198 while (handled_component_p (forzero
)
3199 || INDIRECT_REF_P (forzero
)
3200 || TREE_CODE (forzero
) == MEM_REF
)
3201 forzero
= TREE_OPERAND (forzero
, 0);
3203 if (CONSTANT_CLASS_P (forzero
) && integer_zerop (forzero
))
3205 struct constraint_expr temp
;
3208 temp
.var
= integer_id
;
3210 results
->safe_push (temp
);
3214 t
= get_ref_base_and_extent (t
, &bitpos
, &bitsize
, &bitmaxsize
);
3216 /* Pretend to take the address of the base, we'll take care of
3217 adding the required subset of sub-fields below. */
3218 get_constraint_for_1 (t
, results
, true, lhs_p
);
3219 gcc_assert (results
->length () == 1);
3220 struct constraint_expr
&result
= results
->last ();
3222 if (result
.type
== SCALAR
3223 && get_varinfo (result
.var
)->is_full_var
)
3224 /* For single-field vars do not bother about the offset. */
3226 else if (result
.type
== SCALAR
)
3228 /* In languages like C, you can access one past the end of an
3229 array. You aren't allowed to dereference it, so we can
3230 ignore this constraint. When we handle pointer subtraction,
3231 we may have to do something cute here. */
3233 if ((unsigned HOST_WIDE_INT
)bitpos
< get_varinfo (result
.var
)->fullsize
3236 /* It's also not true that the constraint will actually start at the
3237 right offset, it may start in some padding. We only care about
3238 setting the constraint to the first actual field it touches, so
3240 struct constraint_expr cexpr
= result
;
3244 for (curr
= get_varinfo (cexpr
.var
); curr
; curr
= vi_next (curr
))
3246 if (ranges_overlap_p (curr
->offset
, curr
->size
,
3247 bitpos
, bitmaxsize
))
3249 cexpr
.var
= curr
->id
;
3250 results
->safe_push (cexpr
);
3255 /* If we are going to take the address of this field then
3256 to be able to compute reachability correctly add at least
3257 the last field of the variable. */
3258 if (address_p
&& results
->length () == 0)
3260 curr
= get_varinfo (cexpr
.var
);
3261 while (curr
->next
!= 0)
3262 curr
= vi_next (curr
);
3263 cexpr
.var
= curr
->id
;
3264 results
->safe_push (cexpr
);
3266 else if (results
->length () == 0)
3267 /* Assert that we found *some* field there. The user couldn't be
3268 accessing *only* padding. */
3269 /* Still the user could access one past the end of an array
3270 embedded in a struct resulting in accessing *only* padding. */
3271 /* Or accessing only padding via type-punning to a type
3272 that has a filed just in padding space. */
3274 cexpr
.type
= SCALAR
;
3275 cexpr
.var
= anything_id
;
3277 results
->safe_push (cexpr
);
3280 else if (bitmaxsize
== 0)
3282 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3283 fprintf (dump_file
, "Access to zero-sized part of variable,"
3287 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3288 fprintf (dump_file
, "Access to past the end of variable, ignoring\n");
3290 else if (result
.type
== DEREF
)
3292 /* If we do not know exactly where the access goes say so. Note
3293 that only for non-structure accesses we know that we access
3294 at most one subfiled of any variable. */
3296 || bitsize
!= bitmaxsize
3297 || AGGREGATE_TYPE_P (TREE_TYPE (orig_t
))
3298 || result
.offset
== UNKNOWN_OFFSET
)
3299 result
.offset
= UNKNOWN_OFFSET
;
3301 result
.offset
+= bitpos
;
3303 else if (result
.type
== ADDRESSOF
)
3305 /* We can end up here for component references on a
3306 VIEW_CONVERT_EXPR <>(&foobar). */
3307 result
.type
= SCALAR
;
3308 result
.var
= anything_id
;
3316 /* Dereference the constraint expression CONS, and return the result.
3317 DEREF (ADDRESSOF) = SCALAR
3318 DEREF (SCALAR) = DEREF
3319 DEREF (DEREF) = (temp = DEREF1; result = DEREF(temp))
3320 This is needed so that we can handle dereferencing DEREF constraints. */
3323 do_deref (vec
<ce_s
> *constraints
)
3325 struct constraint_expr
*c
;
3328 FOR_EACH_VEC_ELT (*constraints
, i
, c
)
3330 if (c
->type
== SCALAR
)
3332 else if (c
->type
== ADDRESSOF
)
3334 else if (c
->type
== DEREF
)
3336 struct constraint_expr tmplhs
;
3337 tmplhs
= new_scalar_tmp_constraint_exp ("dereftmp");
3338 process_constraint (new_constraint (tmplhs
, *c
));
3339 c
->var
= tmplhs
.var
;
3346 /* Given a tree T, return the constraint expression for taking the
3350 get_constraint_for_address_of (tree t
, vec
<ce_s
> *results
)
3352 struct constraint_expr
*c
;
3355 get_constraint_for_1 (t
, results
, true, true);
3357 FOR_EACH_VEC_ELT (*results
, i
, c
)
3359 if (c
->type
== DEREF
)
3362 c
->type
= ADDRESSOF
;
3366 /* Given a tree T, return the constraint expression for it. */
3369 get_constraint_for_1 (tree t
, vec
<ce_s
> *results
, bool address_p
,
3372 struct constraint_expr temp
;
3374 /* x = integer is all glommed to a single variable, which doesn't
3375 point to anything by itself. That is, of course, unless it is an
3376 integer constant being treated as a pointer, in which case, we
3377 will return that this is really the addressof anything. This
3378 happens below, since it will fall into the default case. The only
3379 case we know something about an integer treated like a pointer is
3380 when it is the NULL pointer, and then we just say it points to
3383 Do not do that if -fno-delete-null-pointer-checks though, because
3384 in that case *NULL does not fail, so it _should_ alias *anything.
3385 It is not worth adding a new option or renaming the existing one,
3386 since this case is relatively obscure. */
3387 if ((TREE_CODE (t
) == INTEGER_CST
3388 && integer_zerop (t
))
3389 /* The only valid CONSTRUCTORs in gimple with pointer typed
3390 elements are zero-initializer. But in IPA mode we also
3391 process global initializers, so verify at least. */
3392 || (TREE_CODE (t
) == CONSTRUCTOR
3393 && CONSTRUCTOR_NELTS (t
) == 0))
3395 if (flag_delete_null_pointer_checks
)
3396 temp
.var
= nothing_id
;
3398 temp
.var
= nonlocal_id
;
3399 temp
.type
= ADDRESSOF
;
3401 results
->safe_push (temp
);
3405 /* String constants are read-only, ideally we'd have a CONST_DECL
3407 if (TREE_CODE (t
) == STRING_CST
)
3409 temp
.var
= string_id
;
3412 results
->safe_push (temp
);
3416 switch (TREE_CODE_CLASS (TREE_CODE (t
)))
3418 case tcc_expression
:
3420 switch (TREE_CODE (t
))
3423 get_constraint_for_address_of (TREE_OPERAND (t
, 0), results
);
3431 switch (TREE_CODE (t
))
3435 struct constraint_expr cs
;
3437 get_constraint_for_ptr_offset (TREE_OPERAND (t
, 0),
3438 TREE_OPERAND (t
, 1), results
);
3441 /* If we are not taking the address then make sure to process
3442 all subvariables we might access. */
3446 cs
= results
->last ();
3447 if (cs
.type
== DEREF
3448 && type_can_have_subvars (TREE_TYPE (t
)))
3450 /* For dereferences this means we have to defer it
3452 results
->last ().offset
= UNKNOWN_OFFSET
;
3455 if (cs
.type
!= SCALAR
)
3458 vi
= get_varinfo (cs
.var
);
3459 curr
= vi_next (vi
);
3460 if (!vi
->is_full_var
3463 unsigned HOST_WIDE_INT size
;
3464 if (tree_fits_uhwi_p (TYPE_SIZE (TREE_TYPE (t
))))
3465 size
= tree_to_uhwi (TYPE_SIZE (TREE_TYPE (t
)));
3468 for (; curr
; curr
= vi_next (curr
))
3470 if (curr
->offset
- vi
->offset
< size
)
3473 results
->safe_push (cs
);
3482 case ARRAY_RANGE_REF
:
3487 get_constraint_for_component_ref (t
, results
, address_p
, lhs_p
);
3489 case VIEW_CONVERT_EXPR
:
3490 get_constraint_for_1 (TREE_OPERAND (t
, 0), results
, address_p
,
3493 /* We are missing handling for TARGET_MEM_REF here. */
3498 case tcc_exceptional
:
3500 switch (TREE_CODE (t
))
3504 get_constraint_for_ssa_var (t
, results
, address_p
);
3512 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t
), i
, val
)
3514 struct constraint_expr
*rhsp
;
3516 get_constraint_for_1 (val
, &tmp
, address_p
, lhs_p
);
3517 FOR_EACH_VEC_ELT (tmp
, j
, rhsp
)
3518 results
->safe_push (*rhsp
);
3521 /* We do not know whether the constructor was complete,
3522 so technically we have to add &NOTHING or &ANYTHING
3523 like we do for an empty constructor as well. */
3530 case tcc_declaration
:
3532 get_constraint_for_ssa_var (t
, results
, address_p
);
3537 /* We cannot refer to automatic variables through constants. */
3538 temp
.type
= ADDRESSOF
;
3539 temp
.var
= nonlocal_id
;
3541 results
->safe_push (temp
);
3547 /* The default fallback is a constraint from anything. */
3548 temp
.type
= ADDRESSOF
;
3549 temp
.var
= anything_id
;
3551 results
->safe_push (temp
);
3554 /* Given a gimple tree T, return the constraint expression vector for it. */
3557 get_constraint_for (tree t
, vec
<ce_s
> *results
)
3559 gcc_assert (results
->length () == 0);
3561 get_constraint_for_1 (t
, results
, false, true);
3564 /* Given a gimple tree T, return the constraint expression vector for it
3565 to be used as the rhs of a constraint. */
3568 get_constraint_for_rhs (tree t
, vec
<ce_s
> *results
)
3570 gcc_assert (results
->length () == 0);
3572 get_constraint_for_1 (t
, results
, false, false);
3576 /* Efficiently generates constraints from all entries in *RHSC to all
3577 entries in *LHSC. */
3580 process_all_all_constraints (vec
<ce_s
> lhsc
,
3583 struct constraint_expr
*lhsp
, *rhsp
;
3586 if (lhsc
.length () <= 1 || rhsc
.length () <= 1)
3588 FOR_EACH_VEC_ELT (lhsc
, i
, lhsp
)
3589 FOR_EACH_VEC_ELT (rhsc
, j
, rhsp
)
3590 process_constraint (new_constraint (*lhsp
, *rhsp
));
3594 struct constraint_expr tmp
;
3595 tmp
= new_scalar_tmp_constraint_exp ("allalltmp");
3596 FOR_EACH_VEC_ELT (rhsc
, i
, rhsp
)
3597 process_constraint (new_constraint (tmp
, *rhsp
));
3598 FOR_EACH_VEC_ELT (lhsc
, i
, lhsp
)
3599 process_constraint (new_constraint (*lhsp
, tmp
));
3603 /* Handle aggregate copies by expanding into copies of the respective
3604 fields of the structures. */
3607 do_structure_copy (tree lhsop
, tree rhsop
)
3609 struct constraint_expr
*lhsp
, *rhsp
;
3610 auto_vec
<ce_s
> lhsc
;
3611 auto_vec
<ce_s
> rhsc
;
3614 get_constraint_for (lhsop
, &lhsc
);
3615 get_constraint_for_rhs (rhsop
, &rhsc
);
3618 if (lhsp
->type
== DEREF
3619 || (lhsp
->type
== ADDRESSOF
&& lhsp
->var
== anything_id
)
3620 || rhsp
->type
== DEREF
)
3622 if (lhsp
->type
== DEREF
)
3624 gcc_assert (lhsc
.length () == 1);
3625 lhsp
->offset
= UNKNOWN_OFFSET
;
3627 if (rhsp
->type
== DEREF
)
3629 gcc_assert (rhsc
.length () == 1);
3630 rhsp
->offset
= UNKNOWN_OFFSET
;
3632 process_all_all_constraints (lhsc
, rhsc
);
3634 else if (lhsp
->type
== SCALAR
3635 && (rhsp
->type
== SCALAR
3636 || rhsp
->type
== ADDRESSOF
))
3638 HOST_WIDE_INT lhssize
, lhsmaxsize
, lhsoffset
;
3639 HOST_WIDE_INT rhssize
, rhsmaxsize
, rhsoffset
;
3641 get_ref_base_and_extent (lhsop
, &lhsoffset
, &lhssize
, &lhsmaxsize
);
3642 get_ref_base_and_extent (rhsop
, &rhsoffset
, &rhssize
, &rhsmaxsize
);
3643 for (j
= 0; lhsc
.iterate (j
, &lhsp
);)
3645 varinfo_t lhsv
, rhsv
;
3647 lhsv
= get_varinfo (lhsp
->var
);
3648 rhsv
= get_varinfo (rhsp
->var
);
3649 if (lhsv
->may_have_pointers
3650 && (lhsv
->is_full_var
3651 || rhsv
->is_full_var
3652 || ranges_overlap_p (lhsv
->offset
+ rhsoffset
, lhsv
->size
,
3653 rhsv
->offset
+ lhsoffset
, rhsv
->size
)))
3654 process_constraint (new_constraint (*lhsp
, *rhsp
));
3655 if (!rhsv
->is_full_var
3656 && (lhsv
->is_full_var
3657 || (lhsv
->offset
+ rhsoffset
+ lhsv
->size
3658 > rhsv
->offset
+ lhsoffset
+ rhsv
->size
)))
3661 if (k
>= rhsc
.length ())
3672 /* Create constraints ID = { rhsc }. */
3675 make_constraints_to (unsigned id
, vec
<ce_s
> rhsc
)
3677 struct constraint_expr
*c
;
3678 struct constraint_expr includes
;
3682 includes
.offset
= 0;
3683 includes
.type
= SCALAR
;
3685 FOR_EACH_VEC_ELT (rhsc
, j
, c
)
3686 process_constraint (new_constraint (includes
, *c
));
3689 /* Create a constraint ID = OP. */
3692 make_constraint_to (unsigned id
, tree op
)
3694 auto_vec
<ce_s
> rhsc
;
3695 get_constraint_for_rhs (op
, &rhsc
);
3696 make_constraints_to (id
, rhsc
);
3699 /* Create a constraint ID = &FROM. */
3702 make_constraint_from (varinfo_t vi
, int from
)
3704 struct constraint_expr lhs
, rhs
;
3712 rhs
.type
= ADDRESSOF
;
3713 process_constraint (new_constraint (lhs
, rhs
));
3716 /* Create a constraint ID = FROM. */
3719 make_copy_constraint (varinfo_t vi
, int from
)
3721 struct constraint_expr lhs
, rhs
;
3730 process_constraint (new_constraint (lhs
, rhs
));
3733 /* Make constraints necessary to make OP escape. */
3736 make_escape_constraint (tree op
)
3738 make_constraint_to (escaped_id
, op
);
3741 /* Add constraints to that the solution of VI is transitively closed. */
3744 make_transitive_closure_constraints (varinfo_t vi
)
3746 struct constraint_expr lhs
, rhs
;
3754 rhs
.offset
= UNKNOWN_OFFSET
;
3755 process_constraint (new_constraint (lhs
, rhs
));
3758 /* Temporary storage for fake var decls. */
3759 struct obstack fake_var_decl_obstack
;
3761 /* Build a fake VAR_DECL acting as referrer to a DECL_UID. */
3764 build_fake_var_decl (tree type
)
3766 tree decl
= (tree
) XOBNEW (&fake_var_decl_obstack
, struct tree_var_decl
);
3767 memset (decl
, 0, sizeof (struct tree_var_decl
));
3768 TREE_SET_CODE (decl
, VAR_DECL
);
3769 TREE_TYPE (decl
) = type
;
3770 DECL_UID (decl
) = allocate_decl_uid ();
3771 SET_DECL_PT_UID (decl
, -1);
3772 layout_decl (decl
, 0);
3776 /* Create a new artificial heap variable with NAME.
3777 Return the created variable. */
3780 make_heapvar (const char *name
)
3785 heapvar
= build_fake_var_decl (ptr_type_node
);
3786 DECL_EXTERNAL (heapvar
) = 1;
3788 vi
= new_var_info (heapvar
, name
);
3789 vi
->is_artificial_var
= true;
3790 vi
->is_heap_var
= true;
3791 vi
->is_unknown_size_var
= true;
3795 vi
->is_full_var
= true;
3796 insert_vi_for_tree (heapvar
, vi
);
3801 /* Create a new artificial heap variable with NAME and make a
3802 constraint from it to LHS. Set flags according to a tag used
3803 for tracking restrict pointers. */
3806 make_constraint_from_restrict (varinfo_t lhs
, const char *name
)
3808 varinfo_t vi
= make_heapvar (name
);
3809 vi
->is_restrict_var
= 1;
3810 vi
->is_global_var
= 1;
3811 vi
->may_have_pointers
= 1;
3812 make_constraint_from (lhs
, vi
->id
);
3816 /* Create a new artificial heap variable with NAME and make a
3817 constraint from it to LHS. Set flags according to a tag used
3818 for tracking restrict pointers and make the artificial heap
3819 point to global memory. */
3822 make_constraint_from_global_restrict (varinfo_t lhs
, const char *name
)
3824 varinfo_t vi
= make_constraint_from_restrict (lhs
, name
);
3825 make_copy_constraint (vi
, nonlocal_id
);
3829 /* In IPA mode there are varinfos for different aspects of reach
3830 function designator. One for the points-to set of the return
3831 value, one for the variables that are clobbered by the function,
3832 one for its uses and one for each parameter (including a single
3833 glob for remaining variadic arguments). */
3835 enum { fi_clobbers
= 1, fi_uses
= 2,
3836 fi_static_chain
= 3, fi_result
= 4, fi_parm_base
= 5 };
3838 /* Get a constraint for the requested part of a function designator FI
3839 when operating in IPA mode. */
3841 static struct constraint_expr
3842 get_function_part_constraint (varinfo_t fi
, unsigned part
)
3844 struct constraint_expr c
;
3846 gcc_assert (in_ipa_mode
);
3848 if (fi
->id
== anything_id
)
3850 /* ??? We probably should have a ANYFN special variable. */
3851 c
.var
= anything_id
;
3855 else if (TREE_CODE (fi
->decl
) == FUNCTION_DECL
)
3857 varinfo_t ai
= first_vi_for_offset (fi
, part
);
3861 c
.var
= anything_id
;
3875 /* For non-IPA mode, generate constraints necessary for a call on the
3879 handle_rhs_call (gcall
*stmt
, vec
<ce_s
> *results
)
3881 struct constraint_expr rhsc
;
3883 bool returns_uses
= false;
3885 for (i
= 0; i
< gimple_call_num_args (stmt
); ++i
)
3887 tree arg
= gimple_call_arg (stmt
, i
);
3888 int flags
= gimple_call_arg_flags (stmt
, i
);
3890 /* If the argument is not used we can ignore it. */
3891 if (flags
& EAF_UNUSED
)
3894 /* As we compute ESCAPED context-insensitive we do not gain
3895 any precision with just EAF_NOCLOBBER but not EAF_NOESCAPE
3896 set. The argument would still get clobbered through the
3898 if ((flags
& EAF_NOCLOBBER
)
3899 && (flags
& EAF_NOESCAPE
))
3901 varinfo_t uses
= get_call_use_vi (stmt
);
3902 if (!(flags
& EAF_DIRECT
))
3904 varinfo_t tem
= new_var_info (NULL_TREE
, "callarg");
3905 make_constraint_to (tem
->id
, arg
);
3906 make_transitive_closure_constraints (tem
);
3907 make_copy_constraint (uses
, tem
->id
);
3910 make_constraint_to (uses
->id
, arg
);
3911 returns_uses
= true;
3913 else if (flags
& EAF_NOESCAPE
)
3915 struct constraint_expr lhs
, rhs
;
3916 varinfo_t uses
= get_call_use_vi (stmt
);
3917 varinfo_t clobbers
= get_call_clobber_vi (stmt
);
3918 varinfo_t tem
= new_var_info (NULL_TREE
, "callarg");
3919 make_constraint_to (tem
->id
, arg
);
3920 if (!(flags
& EAF_DIRECT
))
3921 make_transitive_closure_constraints (tem
);
3922 make_copy_constraint (uses
, tem
->id
);
3923 make_copy_constraint (clobbers
, tem
->id
);
3924 /* Add *tem = nonlocal, do not add *tem = callused as
3925 EAF_NOESCAPE parameters do not escape to other parameters
3926 and all other uses appear in NONLOCAL as well. */
3931 rhs
.var
= nonlocal_id
;
3933 process_constraint (new_constraint (lhs
, rhs
));
3934 returns_uses
= true;
3937 make_escape_constraint (arg
);
3940 /* If we added to the calls uses solution make sure we account for
3941 pointers to it to be returned. */
3944 rhsc
.var
= get_call_use_vi (stmt
)->id
;
3947 results
->safe_push (rhsc
);
3950 /* The static chain escapes as well. */
3951 if (gimple_call_chain (stmt
))
3952 make_escape_constraint (gimple_call_chain (stmt
));
3954 /* And if we applied NRV the address of the return slot escapes as well. */
3955 if (gimple_call_return_slot_opt_p (stmt
)
3956 && gimple_call_lhs (stmt
) != NULL_TREE
3957 && TREE_ADDRESSABLE (TREE_TYPE (gimple_call_lhs (stmt
))))
3959 auto_vec
<ce_s
> tmpc
;
3960 struct constraint_expr lhsc
, *c
;
3961 get_constraint_for_address_of (gimple_call_lhs (stmt
), &tmpc
);
3962 lhsc
.var
= escaped_id
;
3965 FOR_EACH_VEC_ELT (tmpc
, i
, c
)
3966 process_constraint (new_constraint (lhsc
, *c
));
3969 /* Regular functions return nonlocal memory. */
3970 rhsc
.var
= nonlocal_id
;
3973 results
->safe_push (rhsc
);
3976 /* For non-IPA mode, generate constraints necessary for a call
3977 that returns a pointer and assigns it to LHS. This simply makes
3978 the LHS point to global and escaped variables. */
3981 handle_lhs_call (gcall
*stmt
, tree lhs
, int flags
, vec
<ce_s
> rhsc
,
3984 auto_vec
<ce_s
> lhsc
;
3986 get_constraint_for (lhs
, &lhsc
);
3987 /* If the store is to a global decl make sure to
3988 add proper escape constraints. */
3989 lhs
= get_base_address (lhs
);
3992 && is_global_var (lhs
))
3994 struct constraint_expr tmpc
;
3995 tmpc
.var
= escaped_id
;
3998 lhsc
.safe_push (tmpc
);
4001 /* If the call returns an argument unmodified override the rhs
4003 if (flags
& ERF_RETURNS_ARG
4004 && (flags
& ERF_RETURN_ARG_MASK
) < gimple_call_num_args (stmt
))
4008 arg
= gimple_call_arg (stmt
, flags
& ERF_RETURN_ARG_MASK
);
4009 get_constraint_for (arg
, &rhsc
);
4010 process_all_all_constraints (lhsc
, rhsc
);
4013 else if (flags
& ERF_NOALIAS
)
4016 struct constraint_expr tmpc
;
4018 vi
= make_heapvar ("HEAP");
4019 /* We are marking allocated storage local, we deal with it becoming
4020 global by escaping and setting of vars_contains_escaped_heap. */
4021 DECL_EXTERNAL (vi
->decl
) = 0;
4022 vi
->is_global_var
= 0;
4023 /* If this is not a real malloc call assume the memory was
4024 initialized and thus may point to global memory. All
4025 builtin functions with the malloc attribute behave in a sane way. */
4027 || DECL_BUILT_IN_CLASS (fndecl
) != BUILT_IN_NORMAL
)
4028 make_constraint_from (vi
, nonlocal_id
);
4031 tmpc
.type
= ADDRESSOF
;
4032 rhsc
.safe_push (tmpc
);
4033 process_all_all_constraints (lhsc
, rhsc
);
4037 process_all_all_constraints (lhsc
, rhsc
);
4040 /* For non-IPA mode, generate constraints necessary for a call of a
4041 const function that returns a pointer in the statement STMT. */
4044 handle_const_call (gcall
*stmt
, vec
<ce_s
> *results
)
4046 struct constraint_expr rhsc
;
4049 /* Treat nested const functions the same as pure functions as far
4050 as the static chain is concerned. */
4051 if (gimple_call_chain (stmt
))
4053 varinfo_t uses
= get_call_use_vi (stmt
);
4054 make_transitive_closure_constraints (uses
);
4055 make_constraint_to (uses
->id
, gimple_call_chain (stmt
));
4056 rhsc
.var
= uses
->id
;
4059 results
->safe_push (rhsc
);
4062 /* May return arguments. */
4063 for (k
= 0; k
< gimple_call_num_args (stmt
); ++k
)
4065 tree arg
= gimple_call_arg (stmt
, k
);
4066 auto_vec
<ce_s
> argc
;
4068 struct constraint_expr
*argp
;
4069 get_constraint_for_rhs (arg
, &argc
);
4070 FOR_EACH_VEC_ELT (argc
, i
, argp
)
4071 results
->safe_push (*argp
);
4074 /* May return addresses of globals. */
4075 rhsc
.var
= nonlocal_id
;
4077 rhsc
.type
= ADDRESSOF
;
4078 results
->safe_push (rhsc
);
4081 /* For non-IPA mode, generate constraints necessary for a call to a
4082 pure function in statement STMT. */
4085 handle_pure_call (gcall
*stmt
, vec
<ce_s
> *results
)
4087 struct constraint_expr rhsc
;
4089 varinfo_t uses
= NULL
;
4091 /* Memory reached from pointer arguments is call-used. */
4092 for (i
= 0; i
< gimple_call_num_args (stmt
); ++i
)
4094 tree arg
= gimple_call_arg (stmt
, i
);
4097 uses
= get_call_use_vi (stmt
);
4098 make_transitive_closure_constraints (uses
);
4100 make_constraint_to (uses
->id
, arg
);
4103 /* The static chain is used as well. */
4104 if (gimple_call_chain (stmt
))
4108 uses
= get_call_use_vi (stmt
);
4109 make_transitive_closure_constraints (uses
);
4111 make_constraint_to (uses
->id
, gimple_call_chain (stmt
));
4114 /* Pure functions may return call-used and nonlocal memory. */
4117 rhsc
.var
= uses
->id
;
4120 results
->safe_push (rhsc
);
4122 rhsc
.var
= nonlocal_id
;
4125 results
->safe_push (rhsc
);
4129 /* Return the varinfo for the callee of CALL. */
4132 get_fi_for_callee (gcall
*call
)
4134 tree decl
, fn
= gimple_call_fn (call
);
4136 if (fn
&& TREE_CODE (fn
) == OBJ_TYPE_REF
)
4137 fn
= OBJ_TYPE_REF_EXPR (fn
);
4139 /* If we can directly resolve the function being called, do so.
4140 Otherwise, it must be some sort of indirect expression that
4141 we should still be able to handle. */
4142 decl
= gimple_call_addr_fndecl (fn
);
4144 return get_vi_for_tree (decl
);
4146 /* If the function is anything other than a SSA name pointer we have no
4147 clue and should be getting ANYFN (well, ANYTHING for now). */
4148 if (!fn
|| TREE_CODE (fn
) != SSA_NAME
)
4149 return get_varinfo (anything_id
);
4151 if (SSA_NAME_IS_DEFAULT_DEF (fn
)
4152 && (TREE_CODE (SSA_NAME_VAR (fn
)) == PARM_DECL
4153 || TREE_CODE (SSA_NAME_VAR (fn
)) == RESULT_DECL
))
4154 fn
= SSA_NAME_VAR (fn
);
4156 return get_vi_for_tree (fn
);
4159 /* Create constraints for the builtin call T. Return true if the call
4160 was handled, otherwise false. */
4163 find_func_aliases_for_builtin_call (struct function
*fn
, gcall
*t
)
4165 tree fndecl
= gimple_call_fndecl (t
);
4166 auto_vec
<ce_s
, 2> lhsc
;
4167 auto_vec
<ce_s
, 4> rhsc
;
4170 if (gimple_call_builtin_p (t
, BUILT_IN_NORMAL
))
4171 /* ??? All builtins that are handled here need to be handled
4172 in the alias-oracle query functions explicitly! */
4173 switch (DECL_FUNCTION_CODE (fndecl
))
4175 /* All the following functions return a pointer to the same object
4176 as their first argument points to. The functions do not add
4177 to the ESCAPED solution. The functions make the first argument
4178 pointed to memory point to what the second argument pointed to
4179 memory points to. */
4180 case BUILT_IN_STRCPY
:
4181 case BUILT_IN_STRNCPY
:
4182 case BUILT_IN_BCOPY
:
4183 case BUILT_IN_MEMCPY
:
4184 case BUILT_IN_MEMMOVE
:
4185 case BUILT_IN_MEMPCPY
:
4186 case BUILT_IN_STPCPY
:
4187 case BUILT_IN_STPNCPY
:
4188 case BUILT_IN_STRCAT
:
4189 case BUILT_IN_STRNCAT
:
4190 case BUILT_IN_STRCPY_CHK
:
4191 case BUILT_IN_STRNCPY_CHK
:
4192 case BUILT_IN_MEMCPY_CHK
:
4193 case BUILT_IN_MEMMOVE_CHK
:
4194 case BUILT_IN_MEMPCPY_CHK
:
4195 case BUILT_IN_STPCPY_CHK
:
4196 case BUILT_IN_STPNCPY_CHK
:
4197 case BUILT_IN_STRCAT_CHK
:
4198 case BUILT_IN_STRNCAT_CHK
:
4199 case BUILT_IN_TM_MEMCPY
:
4200 case BUILT_IN_TM_MEMMOVE
:
4202 tree res
= gimple_call_lhs (t
);
4203 tree dest
= gimple_call_arg (t
, (DECL_FUNCTION_CODE (fndecl
)
4204 == BUILT_IN_BCOPY
? 1 : 0));
4205 tree src
= gimple_call_arg (t
, (DECL_FUNCTION_CODE (fndecl
)
4206 == BUILT_IN_BCOPY
? 0 : 1));
4207 if (res
!= NULL_TREE
)
4209 get_constraint_for (res
, &lhsc
);
4210 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_MEMPCPY
4211 || DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_STPCPY
4212 || DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_STPNCPY
4213 || DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_MEMPCPY_CHK
4214 || DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_STPCPY_CHK
4215 || DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_STPNCPY_CHK
)
4216 get_constraint_for_ptr_offset (dest
, NULL_TREE
, &rhsc
);
4218 get_constraint_for (dest
, &rhsc
);
4219 process_all_all_constraints (lhsc
, rhsc
);
4223 get_constraint_for_ptr_offset (dest
, NULL_TREE
, &lhsc
);
4224 get_constraint_for_ptr_offset (src
, NULL_TREE
, &rhsc
);
4227 process_all_all_constraints (lhsc
, rhsc
);
4230 case BUILT_IN_MEMSET
:
4231 case BUILT_IN_MEMSET_CHK
:
4232 case BUILT_IN_TM_MEMSET
:
4234 tree res
= gimple_call_lhs (t
);
4235 tree dest
= gimple_call_arg (t
, 0);
4238 struct constraint_expr ac
;
4239 if (res
!= NULL_TREE
)
4241 get_constraint_for (res
, &lhsc
);
4242 get_constraint_for (dest
, &rhsc
);
4243 process_all_all_constraints (lhsc
, rhsc
);
4246 get_constraint_for_ptr_offset (dest
, NULL_TREE
, &lhsc
);
4248 if (flag_delete_null_pointer_checks
4249 && integer_zerop (gimple_call_arg (t
, 1)))
4251 ac
.type
= ADDRESSOF
;
4252 ac
.var
= nothing_id
;
4257 ac
.var
= integer_id
;
4260 FOR_EACH_VEC_ELT (lhsc
, i
, lhsp
)
4261 process_constraint (new_constraint (*lhsp
, ac
));
4264 case BUILT_IN_POSIX_MEMALIGN
:
4266 tree ptrptr
= gimple_call_arg (t
, 0);
4267 get_constraint_for (ptrptr
, &lhsc
);
4269 varinfo_t vi
= make_heapvar ("HEAP");
4270 /* We are marking allocated storage local, we deal with it becoming
4271 global by escaping and setting of vars_contains_escaped_heap. */
4272 DECL_EXTERNAL (vi
->decl
) = 0;
4273 vi
->is_global_var
= 0;
4274 struct constraint_expr tmpc
;
4277 tmpc
.type
= ADDRESSOF
;
4278 rhsc
.safe_push (tmpc
);
4279 process_all_all_constraints (lhsc
, rhsc
);
4282 case BUILT_IN_ASSUME_ALIGNED
:
4284 tree res
= gimple_call_lhs (t
);
4285 tree dest
= gimple_call_arg (t
, 0);
4286 if (res
!= NULL_TREE
)
4288 get_constraint_for (res
, &lhsc
);
4289 get_constraint_for (dest
, &rhsc
);
4290 process_all_all_constraints (lhsc
, rhsc
);
4294 /* All the following functions do not return pointers, do not
4295 modify the points-to sets of memory reachable from their
4296 arguments and do not add to the ESCAPED solution. */
4297 case BUILT_IN_SINCOS
:
4298 case BUILT_IN_SINCOSF
:
4299 case BUILT_IN_SINCOSL
:
4300 case BUILT_IN_FREXP
:
4301 case BUILT_IN_FREXPF
:
4302 case BUILT_IN_FREXPL
:
4303 case BUILT_IN_GAMMA_R
:
4304 case BUILT_IN_GAMMAF_R
:
4305 case BUILT_IN_GAMMAL_R
:
4306 case BUILT_IN_LGAMMA_R
:
4307 case BUILT_IN_LGAMMAF_R
:
4308 case BUILT_IN_LGAMMAL_R
:
4310 case BUILT_IN_MODFF
:
4311 case BUILT_IN_MODFL
:
4312 case BUILT_IN_REMQUO
:
4313 case BUILT_IN_REMQUOF
:
4314 case BUILT_IN_REMQUOL
:
4317 case BUILT_IN_STRDUP
:
4318 case BUILT_IN_STRNDUP
:
4319 case BUILT_IN_REALLOC
:
4320 if (gimple_call_lhs (t
))
4322 handle_lhs_call (t
, gimple_call_lhs (t
),
4323 gimple_call_return_flags (t
) | ERF_NOALIAS
,
4325 get_constraint_for_ptr_offset (gimple_call_lhs (t
),
4327 get_constraint_for_ptr_offset (gimple_call_arg (t
, 0),
4331 process_all_all_constraints (lhsc
, rhsc
);
4334 /* For realloc the resulting pointer can be equal to the
4335 argument as well. But only doing this wouldn't be
4336 correct because with ptr == 0 realloc behaves like malloc. */
4337 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_REALLOC
)
4339 get_constraint_for (gimple_call_lhs (t
), &lhsc
);
4340 get_constraint_for (gimple_call_arg (t
, 0), &rhsc
);
4341 process_all_all_constraints (lhsc
, rhsc
);
4346 /* String / character search functions return a pointer into the
4347 source string or NULL. */
4348 case BUILT_IN_INDEX
:
4349 case BUILT_IN_STRCHR
:
4350 case BUILT_IN_STRRCHR
:
4351 case BUILT_IN_MEMCHR
:
4352 case BUILT_IN_STRSTR
:
4353 case BUILT_IN_STRPBRK
:
4354 if (gimple_call_lhs (t
))
4356 tree src
= gimple_call_arg (t
, 0);
4357 get_constraint_for_ptr_offset (src
, NULL_TREE
, &rhsc
);
4358 constraint_expr nul
;
4359 nul
.var
= nothing_id
;
4361 nul
.type
= ADDRESSOF
;
4362 rhsc
.safe_push (nul
);
4363 get_constraint_for (gimple_call_lhs (t
), &lhsc
);
4364 process_all_all_constraints (lhsc
, rhsc
);
4367 /* Trampolines are special - they set up passing the static
4369 case BUILT_IN_INIT_TRAMPOLINE
:
4371 tree tramp
= gimple_call_arg (t
, 0);
4372 tree nfunc
= gimple_call_arg (t
, 1);
4373 tree frame
= gimple_call_arg (t
, 2);
4375 struct constraint_expr lhs
, *rhsp
;
4378 varinfo_t nfi
= NULL
;
4379 gcc_assert (TREE_CODE (nfunc
) == ADDR_EXPR
);
4380 nfi
= lookup_vi_for_tree (TREE_OPERAND (nfunc
, 0));
4383 lhs
= get_function_part_constraint (nfi
, fi_static_chain
);
4384 get_constraint_for (frame
, &rhsc
);
4385 FOR_EACH_VEC_ELT (rhsc
, i
, rhsp
)
4386 process_constraint (new_constraint (lhs
, *rhsp
));
4389 /* Make the frame point to the function for
4390 the trampoline adjustment call. */
4391 get_constraint_for (tramp
, &lhsc
);
4393 get_constraint_for (nfunc
, &rhsc
);
4394 process_all_all_constraints (lhsc
, rhsc
);
4399 /* Else fallthru to generic handling which will let
4400 the frame escape. */
4403 case BUILT_IN_ADJUST_TRAMPOLINE
:
4405 tree tramp
= gimple_call_arg (t
, 0);
4406 tree res
= gimple_call_lhs (t
);
4407 if (in_ipa_mode
&& res
)
4409 get_constraint_for (res
, &lhsc
);
4410 get_constraint_for (tramp
, &rhsc
);
4412 process_all_all_constraints (lhsc
, rhsc
);
4416 CASE_BUILT_IN_TM_STORE (1):
4417 CASE_BUILT_IN_TM_STORE (2):
4418 CASE_BUILT_IN_TM_STORE (4):
4419 CASE_BUILT_IN_TM_STORE (8):
4420 CASE_BUILT_IN_TM_STORE (FLOAT
):
4421 CASE_BUILT_IN_TM_STORE (DOUBLE
):
4422 CASE_BUILT_IN_TM_STORE (LDOUBLE
):
4423 CASE_BUILT_IN_TM_STORE (M64
):
4424 CASE_BUILT_IN_TM_STORE (M128
):
4425 CASE_BUILT_IN_TM_STORE (M256
):
4427 tree addr
= gimple_call_arg (t
, 0);
4428 tree src
= gimple_call_arg (t
, 1);
4430 get_constraint_for (addr
, &lhsc
);
4432 get_constraint_for (src
, &rhsc
);
4433 process_all_all_constraints (lhsc
, rhsc
);
4436 CASE_BUILT_IN_TM_LOAD (1):
4437 CASE_BUILT_IN_TM_LOAD (2):
4438 CASE_BUILT_IN_TM_LOAD (4):
4439 CASE_BUILT_IN_TM_LOAD (8):
4440 CASE_BUILT_IN_TM_LOAD (FLOAT
):
4441 CASE_BUILT_IN_TM_LOAD (DOUBLE
):
4442 CASE_BUILT_IN_TM_LOAD (LDOUBLE
):
4443 CASE_BUILT_IN_TM_LOAD (M64
):
4444 CASE_BUILT_IN_TM_LOAD (M128
):
4445 CASE_BUILT_IN_TM_LOAD (M256
):
4447 tree dest
= gimple_call_lhs (t
);
4448 tree addr
= gimple_call_arg (t
, 0);
4450 get_constraint_for (dest
, &lhsc
);
4451 get_constraint_for (addr
, &rhsc
);
4453 process_all_all_constraints (lhsc
, rhsc
);
4456 /* Variadic argument handling needs to be handled in IPA
4458 case BUILT_IN_VA_START
:
4460 tree valist
= gimple_call_arg (t
, 0);
4461 struct constraint_expr rhs
, *lhsp
;
4463 get_constraint_for (valist
, &lhsc
);
4465 /* The va_list gets access to pointers in variadic
4466 arguments. Which we know in the case of IPA analysis
4467 and otherwise are just all nonlocal variables. */
4470 fi
= lookup_vi_for_tree (fn
->decl
);
4471 rhs
= get_function_part_constraint (fi
, ~0);
4472 rhs
.type
= ADDRESSOF
;
4476 rhs
.var
= nonlocal_id
;
4477 rhs
.type
= ADDRESSOF
;
4480 FOR_EACH_VEC_ELT (lhsc
, i
, lhsp
)
4481 process_constraint (new_constraint (*lhsp
, rhs
));
4482 /* va_list is clobbered. */
4483 make_constraint_to (get_call_clobber_vi (t
)->id
, valist
);
4486 /* va_end doesn't have any effect that matters. */
4487 case BUILT_IN_VA_END
:
4489 /* Alternate return. Simply give up for now. */
4490 case BUILT_IN_RETURN
:
4494 || !(fi
= get_vi_for_tree (fn
->decl
)))
4495 make_constraint_from (get_varinfo (escaped_id
), anything_id
);
4496 else if (in_ipa_mode
4499 struct constraint_expr lhs
, rhs
;
4500 lhs
= get_function_part_constraint (fi
, fi_result
);
4501 rhs
.var
= anything_id
;
4504 process_constraint (new_constraint (lhs
, rhs
));
4508 /* printf-style functions may have hooks to set pointers to
4509 point to somewhere into the generated string. Leave them
4510 for a later exercise... */
4512 /* Fallthru to general call handling. */;
4518 /* Create constraints for the call T. */
4521 find_func_aliases_for_call (struct function
*fn
, gcall
*t
)
4523 tree fndecl
= gimple_call_fndecl (t
);
4526 if (fndecl
!= NULL_TREE
4527 && DECL_BUILT_IN (fndecl
)
4528 && find_func_aliases_for_builtin_call (fn
, t
))
4531 fi
= get_fi_for_callee (t
);
4533 || (fndecl
&& !fi
->is_fn_info
))
4535 auto_vec
<ce_s
, 16> rhsc
;
4536 int flags
= gimple_call_flags (t
);
4538 /* Const functions can return their arguments and addresses
4539 of global memory but not of escaped memory. */
4540 if (flags
& (ECF_CONST
|ECF_NOVOPS
))
4542 if (gimple_call_lhs (t
))
4543 handle_const_call (t
, &rhsc
);
4545 /* Pure functions can return addresses in and of memory
4546 reachable from their arguments, but they are not an escape
4547 point for reachable memory of their arguments. */
4548 else if (flags
& (ECF_PURE
|ECF_LOOPING_CONST_OR_PURE
))
4549 handle_pure_call (t
, &rhsc
);
4551 handle_rhs_call (t
, &rhsc
);
4552 if (gimple_call_lhs (t
))
4553 handle_lhs_call (t
, gimple_call_lhs (t
),
4554 gimple_call_return_flags (t
), rhsc
, fndecl
);
4558 auto_vec
<ce_s
, 2> rhsc
;
4562 /* Assign all the passed arguments to the appropriate incoming
4563 parameters of the function. */
4564 for (j
= 0; j
< gimple_call_num_args (t
); j
++)
4566 struct constraint_expr lhs
;
4567 struct constraint_expr
*rhsp
;
4568 tree arg
= gimple_call_arg (t
, j
);
4570 get_constraint_for_rhs (arg
, &rhsc
);
4571 lhs
= get_function_part_constraint (fi
, fi_parm_base
+ j
);
4572 while (rhsc
.length () != 0)
4574 rhsp
= &rhsc
.last ();
4575 process_constraint (new_constraint (lhs
, *rhsp
));
4580 /* If we are returning a value, assign it to the result. */
4581 lhsop
= gimple_call_lhs (t
);
4584 auto_vec
<ce_s
, 2> lhsc
;
4585 struct constraint_expr rhs
;
4586 struct constraint_expr
*lhsp
;
4588 get_constraint_for (lhsop
, &lhsc
);
4589 rhs
= get_function_part_constraint (fi
, fi_result
);
4591 && DECL_RESULT (fndecl
)
4592 && DECL_BY_REFERENCE (DECL_RESULT (fndecl
)))
4594 auto_vec
<ce_s
, 2> tem
;
4595 tem
.quick_push (rhs
);
4597 gcc_checking_assert (tem
.length () == 1);
4600 FOR_EACH_VEC_ELT (lhsc
, j
, lhsp
)
4601 process_constraint (new_constraint (*lhsp
, rhs
));
4604 /* If we pass the result decl by reference, honor that. */
4607 && DECL_RESULT (fndecl
)
4608 && DECL_BY_REFERENCE (DECL_RESULT (fndecl
)))
4610 struct constraint_expr lhs
;
4611 struct constraint_expr
*rhsp
;
4613 get_constraint_for_address_of (lhsop
, &rhsc
);
4614 lhs
= get_function_part_constraint (fi
, fi_result
);
4615 FOR_EACH_VEC_ELT (rhsc
, j
, rhsp
)
4616 process_constraint (new_constraint (lhs
, *rhsp
));
4620 /* If we use a static chain, pass it along. */
4621 if (gimple_call_chain (t
))
4623 struct constraint_expr lhs
;
4624 struct constraint_expr
*rhsp
;
4626 get_constraint_for (gimple_call_chain (t
), &rhsc
);
4627 lhs
= get_function_part_constraint (fi
, fi_static_chain
);
4628 FOR_EACH_VEC_ELT (rhsc
, j
, rhsp
)
4629 process_constraint (new_constraint (lhs
, *rhsp
));
4634 /* Walk statement T setting up aliasing constraints according to the
4635 references found in T. This function is the main part of the
4636 constraint builder. AI points to auxiliary alias information used
4637 when building alias sets and computing alias grouping heuristics. */
4640 find_func_aliases (struct function
*fn
, gimple origt
)
4643 auto_vec
<ce_s
, 16> lhsc
;
4644 auto_vec
<ce_s
, 16> rhsc
;
4645 struct constraint_expr
*c
;
4648 /* Now build constraints expressions. */
4649 if (gimple_code (t
) == GIMPLE_PHI
)
4654 /* For a phi node, assign all the arguments to
4656 get_constraint_for (gimple_phi_result (t
), &lhsc
);
4657 for (i
= 0; i
< gimple_phi_num_args (t
); i
++)
4659 tree strippedrhs
= PHI_ARG_DEF (t
, i
);
4661 STRIP_NOPS (strippedrhs
);
4662 get_constraint_for_rhs (gimple_phi_arg_def (t
, i
), &rhsc
);
4664 FOR_EACH_VEC_ELT (lhsc
, j
, c
)
4666 struct constraint_expr
*c2
;
4667 while (rhsc
.length () > 0)
4670 process_constraint (new_constraint (*c
, *c2
));
4676 /* In IPA mode, we need to generate constraints to pass call
4677 arguments through their calls. There are two cases,
4678 either a GIMPLE_CALL returning a value, or just a plain
4679 GIMPLE_CALL when we are not.
4681 In non-ipa mode, we need to generate constraints for each
4682 pointer passed by address. */
4683 else if (is_gimple_call (t
))
4684 find_func_aliases_for_call (fn
, as_a
<gcall
*> (t
));
4686 /* Otherwise, just a regular assignment statement. Only care about
4687 operations with pointer result, others are dealt with as escape
4688 points if they have pointer operands. */
4689 else if (is_gimple_assign (t
))
4691 /* Otherwise, just a regular assignment statement. */
4692 tree lhsop
= gimple_assign_lhs (t
);
4693 tree rhsop
= (gimple_num_ops (t
) == 2) ? gimple_assign_rhs1 (t
) : NULL
;
4695 if (rhsop
&& TREE_CLOBBER_P (rhsop
))
4696 /* Ignore clobbers, they don't actually store anything into
4699 else if (rhsop
&& AGGREGATE_TYPE_P (TREE_TYPE (lhsop
)))
4700 do_structure_copy (lhsop
, rhsop
);
4703 enum tree_code code
= gimple_assign_rhs_code (t
);
4705 get_constraint_for (lhsop
, &lhsc
);
4707 if (code
== POINTER_PLUS_EXPR
)
4708 get_constraint_for_ptr_offset (gimple_assign_rhs1 (t
),
4709 gimple_assign_rhs2 (t
), &rhsc
);
4710 else if (code
== BIT_AND_EXPR
4711 && TREE_CODE (gimple_assign_rhs2 (t
)) == INTEGER_CST
)
4713 /* Aligning a pointer via a BIT_AND_EXPR is offsetting
4714 the pointer. Handle it by offsetting it by UNKNOWN. */
4715 get_constraint_for_ptr_offset (gimple_assign_rhs1 (t
),
4718 else if ((CONVERT_EXPR_CODE_P (code
)
4719 && !(POINTER_TYPE_P (gimple_expr_type (t
))
4720 && !POINTER_TYPE_P (TREE_TYPE (rhsop
))))
4721 || gimple_assign_single_p (t
))
4722 get_constraint_for_rhs (rhsop
, &rhsc
);
4723 else if (code
== COND_EXPR
)
4725 /* The result is a merge of both COND_EXPR arms. */
4726 auto_vec
<ce_s
, 2> tmp
;
4727 struct constraint_expr
*rhsp
;
4729 get_constraint_for_rhs (gimple_assign_rhs2 (t
), &rhsc
);
4730 get_constraint_for_rhs (gimple_assign_rhs3 (t
), &tmp
);
4731 FOR_EACH_VEC_ELT (tmp
, i
, rhsp
)
4732 rhsc
.safe_push (*rhsp
);
4734 else if (truth_value_p (code
))
4735 /* Truth value results are not pointer (parts). Or at least
4736 very very unreasonable obfuscation of a part. */
4740 /* All other operations are merges. */
4741 auto_vec
<ce_s
, 4> tmp
;
4742 struct constraint_expr
*rhsp
;
4744 get_constraint_for_rhs (gimple_assign_rhs1 (t
), &rhsc
);
4745 for (i
= 2; i
< gimple_num_ops (t
); ++i
)
4747 get_constraint_for_rhs (gimple_op (t
, i
), &tmp
);
4748 FOR_EACH_VEC_ELT (tmp
, j
, rhsp
)
4749 rhsc
.safe_push (*rhsp
);
4753 process_all_all_constraints (lhsc
, rhsc
);
4755 /* If there is a store to a global variable the rhs escapes. */
4756 if ((lhsop
= get_base_address (lhsop
)) != NULL_TREE
4758 && is_global_var (lhsop
)
4760 || DECL_EXTERNAL (lhsop
) || TREE_PUBLIC (lhsop
)))
4761 make_escape_constraint (rhsop
);
4763 /* Handle escapes through return. */
4764 else if (gimple_code (t
) == GIMPLE_RETURN
4765 && gimple_return_retval (as_a
<greturn
*> (t
)) != NULL_TREE
)
4767 greturn
*return_stmt
= as_a
<greturn
*> (t
);
4770 || !(fi
= get_vi_for_tree (fn
->decl
)))
4771 make_escape_constraint (gimple_return_retval (return_stmt
));
4772 else if (in_ipa_mode
4775 struct constraint_expr lhs
;
4776 struct constraint_expr
*rhsp
;
4779 lhs
= get_function_part_constraint (fi
, fi_result
);
4780 get_constraint_for_rhs (gimple_return_retval (return_stmt
), &rhsc
);
4781 FOR_EACH_VEC_ELT (rhsc
, i
, rhsp
)
4782 process_constraint (new_constraint (lhs
, *rhsp
));
4785 /* Handle asms conservatively by adding escape constraints to everything. */
4786 else if (gasm
*asm_stmt
= dyn_cast
<gasm
*> (t
))
4788 unsigned i
, noutputs
;
4789 const char **oconstraints
;
4790 const char *constraint
;
4791 bool allows_mem
, allows_reg
, is_inout
;
4793 noutputs
= gimple_asm_noutputs (asm_stmt
);
4794 oconstraints
= XALLOCAVEC (const char *, noutputs
);
4796 for (i
= 0; i
< noutputs
; ++i
)
4798 tree link
= gimple_asm_output_op (asm_stmt
, i
);
4799 tree op
= TREE_VALUE (link
);
4801 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link
)));
4802 oconstraints
[i
] = constraint
;
4803 parse_output_constraint (&constraint
, i
, 0, 0, &allows_mem
,
4804 &allows_reg
, &is_inout
);
4806 /* A memory constraint makes the address of the operand escape. */
4807 if (!allows_reg
&& allows_mem
)
4808 make_escape_constraint (build_fold_addr_expr (op
));
4810 /* The asm may read global memory, so outputs may point to
4811 any global memory. */
4814 auto_vec
<ce_s
, 2> lhsc
;
4815 struct constraint_expr rhsc
, *lhsp
;
4817 get_constraint_for (op
, &lhsc
);
4818 rhsc
.var
= nonlocal_id
;
4821 FOR_EACH_VEC_ELT (lhsc
, j
, lhsp
)
4822 process_constraint (new_constraint (*lhsp
, rhsc
));
4825 for (i
= 0; i
< gimple_asm_ninputs (asm_stmt
); ++i
)
4827 tree link
= gimple_asm_input_op (asm_stmt
, i
);
4828 tree op
= TREE_VALUE (link
);
4830 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link
)));
4832 parse_input_constraint (&constraint
, 0, 0, noutputs
, 0, oconstraints
,
4833 &allows_mem
, &allows_reg
);
4835 /* A memory constraint makes the address of the operand escape. */
4836 if (!allows_reg
&& allows_mem
)
4837 make_escape_constraint (build_fold_addr_expr (op
));
4838 /* Strictly we'd only need the constraint to ESCAPED if
4839 the asm clobbers memory, otherwise using something
4840 along the lines of per-call clobbers/uses would be enough. */
4842 make_escape_constraint (op
);
4848 /* Create a constraint adding to the clobber set of FI the memory
4849 pointed to by PTR. */
4852 process_ipa_clobber (varinfo_t fi
, tree ptr
)
4854 vec
<ce_s
> ptrc
= vNULL
;
4855 struct constraint_expr
*c
, lhs
;
4857 get_constraint_for_rhs (ptr
, &ptrc
);
4858 lhs
= get_function_part_constraint (fi
, fi_clobbers
);
4859 FOR_EACH_VEC_ELT (ptrc
, i
, c
)
4860 process_constraint (new_constraint (lhs
, *c
));
4864 /* Walk statement T setting up clobber and use constraints according to the
4865 references found in T. This function is a main part of the
4866 IPA constraint builder. */
4869 find_func_clobbers (struct function
*fn
, gimple origt
)
4872 auto_vec
<ce_s
, 16> lhsc
;
4873 auto_vec
<ce_s
, 16> rhsc
;
4876 /* Add constraints for clobbered/used in IPA mode.
4877 We are not interested in what automatic variables are clobbered
4878 or used as we only use the information in the caller to which
4879 they do not escape. */
4880 gcc_assert (in_ipa_mode
);
4882 /* If the stmt refers to memory in any way it better had a VUSE. */
4883 if (gimple_vuse (t
) == NULL_TREE
)
4886 /* We'd better have function information for the current function. */
4887 fi
= lookup_vi_for_tree (fn
->decl
);
4888 gcc_assert (fi
!= NULL
);
4890 /* Account for stores in assignments and calls. */
4891 if (gimple_vdef (t
) != NULL_TREE
4892 && gimple_has_lhs (t
))
4894 tree lhs
= gimple_get_lhs (t
);
4896 while (handled_component_p (tem
))
4897 tem
= TREE_OPERAND (tem
, 0);
4899 && !auto_var_in_fn_p (tem
, fn
->decl
))
4900 || INDIRECT_REF_P (tem
)
4901 || (TREE_CODE (tem
) == MEM_REF
4902 && !(TREE_CODE (TREE_OPERAND (tem
, 0)) == ADDR_EXPR
4904 (TREE_OPERAND (TREE_OPERAND (tem
, 0), 0), fn
->decl
))))
4906 struct constraint_expr lhsc
, *rhsp
;
4908 lhsc
= get_function_part_constraint (fi
, fi_clobbers
);
4909 get_constraint_for_address_of (lhs
, &rhsc
);
4910 FOR_EACH_VEC_ELT (rhsc
, i
, rhsp
)
4911 process_constraint (new_constraint (lhsc
, *rhsp
));
4916 /* Account for uses in assigments and returns. */
4917 if (gimple_assign_single_p (t
)
4918 || (gimple_code (t
) == GIMPLE_RETURN
4919 && gimple_return_retval (as_a
<greturn
*> (t
)) != NULL_TREE
))
4921 tree rhs
= (gimple_assign_single_p (t
)
4922 ? gimple_assign_rhs1 (t
)
4923 : gimple_return_retval (as_a
<greturn
*> (t
)));
4925 while (handled_component_p (tem
))
4926 tem
= TREE_OPERAND (tem
, 0);
4928 && !auto_var_in_fn_p (tem
, fn
->decl
))
4929 || INDIRECT_REF_P (tem
)
4930 || (TREE_CODE (tem
) == MEM_REF
4931 && !(TREE_CODE (TREE_OPERAND (tem
, 0)) == ADDR_EXPR
4933 (TREE_OPERAND (TREE_OPERAND (tem
, 0), 0), fn
->decl
))))
4935 struct constraint_expr lhs
, *rhsp
;
4937 lhs
= get_function_part_constraint (fi
, fi_uses
);
4938 get_constraint_for_address_of (rhs
, &rhsc
);
4939 FOR_EACH_VEC_ELT (rhsc
, i
, rhsp
)
4940 process_constraint (new_constraint (lhs
, *rhsp
));
4945 if (gcall
*call_stmt
= dyn_cast
<gcall
*> (t
))
4947 varinfo_t cfi
= NULL
;
4948 tree decl
= gimple_call_fndecl (t
);
4949 struct constraint_expr lhs
, rhs
;
4952 /* For builtins we do not have separate function info. For those
4953 we do not generate escapes for we have to generate clobbers/uses. */
4954 if (gimple_call_builtin_p (t
, BUILT_IN_NORMAL
))
4955 switch (DECL_FUNCTION_CODE (decl
))
4957 /* The following functions use and clobber memory pointed to
4958 by their arguments. */
4959 case BUILT_IN_STRCPY
:
4960 case BUILT_IN_STRNCPY
:
4961 case BUILT_IN_BCOPY
:
4962 case BUILT_IN_MEMCPY
:
4963 case BUILT_IN_MEMMOVE
:
4964 case BUILT_IN_MEMPCPY
:
4965 case BUILT_IN_STPCPY
:
4966 case BUILT_IN_STPNCPY
:
4967 case BUILT_IN_STRCAT
:
4968 case BUILT_IN_STRNCAT
:
4969 case BUILT_IN_STRCPY_CHK
:
4970 case BUILT_IN_STRNCPY_CHK
:
4971 case BUILT_IN_MEMCPY_CHK
:
4972 case BUILT_IN_MEMMOVE_CHK
:
4973 case BUILT_IN_MEMPCPY_CHK
:
4974 case BUILT_IN_STPCPY_CHK
:
4975 case BUILT_IN_STPNCPY_CHK
:
4976 case BUILT_IN_STRCAT_CHK
:
4977 case BUILT_IN_STRNCAT_CHK
:
4979 tree dest
= gimple_call_arg (t
, (DECL_FUNCTION_CODE (decl
)
4980 == BUILT_IN_BCOPY
? 1 : 0));
4981 tree src
= gimple_call_arg (t
, (DECL_FUNCTION_CODE (decl
)
4982 == BUILT_IN_BCOPY
? 0 : 1));
4984 struct constraint_expr
*rhsp
, *lhsp
;
4985 get_constraint_for_ptr_offset (dest
, NULL_TREE
, &lhsc
);
4986 lhs
= get_function_part_constraint (fi
, fi_clobbers
);
4987 FOR_EACH_VEC_ELT (lhsc
, i
, lhsp
)
4988 process_constraint (new_constraint (lhs
, *lhsp
));
4989 get_constraint_for_ptr_offset (src
, NULL_TREE
, &rhsc
);
4990 lhs
= get_function_part_constraint (fi
, fi_uses
);
4991 FOR_EACH_VEC_ELT (rhsc
, i
, rhsp
)
4992 process_constraint (new_constraint (lhs
, *rhsp
));
4995 /* The following function clobbers memory pointed to by
4997 case BUILT_IN_MEMSET
:
4998 case BUILT_IN_MEMSET_CHK
:
4999 case BUILT_IN_POSIX_MEMALIGN
:
5001 tree dest
= gimple_call_arg (t
, 0);
5004 get_constraint_for_ptr_offset (dest
, NULL_TREE
, &lhsc
);
5005 lhs
= get_function_part_constraint (fi
, fi_clobbers
);
5006 FOR_EACH_VEC_ELT (lhsc
, i
, lhsp
)
5007 process_constraint (new_constraint (lhs
, *lhsp
));
5010 /* The following functions clobber their second and third
5012 case BUILT_IN_SINCOS
:
5013 case BUILT_IN_SINCOSF
:
5014 case BUILT_IN_SINCOSL
:
5016 process_ipa_clobber (fi
, gimple_call_arg (t
, 1));
5017 process_ipa_clobber (fi
, gimple_call_arg (t
, 2));
5020 /* The following functions clobber their second argument. */
5021 case BUILT_IN_FREXP
:
5022 case BUILT_IN_FREXPF
:
5023 case BUILT_IN_FREXPL
:
5024 case BUILT_IN_LGAMMA_R
:
5025 case BUILT_IN_LGAMMAF_R
:
5026 case BUILT_IN_LGAMMAL_R
:
5027 case BUILT_IN_GAMMA_R
:
5028 case BUILT_IN_GAMMAF_R
:
5029 case BUILT_IN_GAMMAL_R
:
5031 case BUILT_IN_MODFF
:
5032 case BUILT_IN_MODFL
:
5034 process_ipa_clobber (fi
, gimple_call_arg (t
, 1));
5037 /* The following functions clobber their third argument. */
5038 case BUILT_IN_REMQUO
:
5039 case BUILT_IN_REMQUOF
:
5040 case BUILT_IN_REMQUOL
:
5042 process_ipa_clobber (fi
, gimple_call_arg (t
, 2));
5045 /* The following functions neither read nor clobber memory. */
5046 case BUILT_IN_ASSUME_ALIGNED
:
5049 /* Trampolines are of no interest to us. */
5050 case BUILT_IN_INIT_TRAMPOLINE
:
5051 case BUILT_IN_ADJUST_TRAMPOLINE
:
5053 case BUILT_IN_VA_START
:
5054 case BUILT_IN_VA_END
:
5056 /* printf-style functions may have hooks to set pointers to
5057 point to somewhere into the generated string. Leave them
5058 for a later exercise... */
5060 /* Fallthru to general call handling. */;
5063 /* Parameters passed by value are used. */
5064 lhs
= get_function_part_constraint (fi
, fi_uses
);
5065 for (i
= 0; i
< gimple_call_num_args (t
); i
++)
5067 struct constraint_expr
*rhsp
;
5068 tree arg
= gimple_call_arg (t
, i
);
5070 if (TREE_CODE (arg
) == SSA_NAME
5071 || is_gimple_min_invariant (arg
))
5074 get_constraint_for_address_of (arg
, &rhsc
);
5075 FOR_EACH_VEC_ELT (rhsc
, j
, rhsp
)
5076 process_constraint (new_constraint (lhs
, *rhsp
));
5080 /* Build constraints for propagating clobbers/uses along the
5082 cfi
= get_fi_for_callee (call_stmt
);
5083 if (cfi
->id
== anything_id
)
5085 if (gimple_vdef (t
))
5086 make_constraint_from (first_vi_for_offset (fi
, fi_clobbers
),
5088 make_constraint_from (first_vi_for_offset (fi
, fi_uses
),
5093 /* For callees without function info (that's external functions),
5094 ESCAPED is clobbered and used. */
5095 if (gimple_call_fndecl (t
)
5096 && !cfi
->is_fn_info
)
5100 if (gimple_vdef (t
))
5101 make_copy_constraint (first_vi_for_offset (fi
, fi_clobbers
),
5103 make_copy_constraint (first_vi_for_offset (fi
, fi_uses
), escaped_id
);
5105 /* Also honor the call statement use/clobber info. */
5106 if ((vi
= lookup_call_clobber_vi (call_stmt
)) != NULL
)
5107 make_copy_constraint (first_vi_for_offset (fi
, fi_clobbers
),
5109 if ((vi
= lookup_call_use_vi (call_stmt
)) != NULL
)
5110 make_copy_constraint (first_vi_for_offset (fi
, fi_uses
),
5115 /* Otherwise the caller clobbers and uses what the callee does.
5116 ??? This should use a new complex constraint that filters
5117 local variables of the callee. */
5118 if (gimple_vdef (t
))
5120 lhs
= get_function_part_constraint (fi
, fi_clobbers
);
5121 rhs
= get_function_part_constraint (cfi
, fi_clobbers
);
5122 process_constraint (new_constraint (lhs
, rhs
));
5124 lhs
= get_function_part_constraint (fi
, fi_uses
);
5125 rhs
= get_function_part_constraint (cfi
, fi_uses
);
5126 process_constraint (new_constraint (lhs
, rhs
));
5128 else if (gimple_code (t
) == GIMPLE_ASM
)
5130 /* ??? Ick. We can do better. */
5131 if (gimple_vdef (t
))
5132 make_constraint_from (first_vi_for_offset (fi
, fi_clobbers
),
5134 make_constraint_from (first_vi_for_offset (fi
, fi_uses
),
5140 /* Find the first varinfo in the same variable as START that overlaps with
5141 OFFSET. Return NULL if we can't find one. */
5144 first_vi_for_offset (varinfo_t start
, unsigned HOST_WIDE_INT offset
)
5146 /* If the offset is outside of the variable, bail out. */
5147 if (offset
>= start
->fullsize
)
5150 /* If we cannot reach offset from start, lookup the first field
5151 and start from there. */
5152 if (start
->offset
> offset
)
5153 start
= get_varinfo (start
->head
);
5157 /* We may not find a variable in the field list with the actual
5158 offset when when we have glommed a structure to a variable.
5159 In that case, however, offset should still be within the size
5161 if (offset
>= start
->offset
5162 && (offset
- start
->offset
) < start
->size
)
5165 start
= vi_next (start
);
5171 /* Find the first varinfo in the same variable as START that overlaps with
5172 OFFSET. If there is no such varinfo the varinfo directly preceding
5173 OFFSET is returned. */
5176 first_or_preceding_vi_for_offset (varinfo_t start
,
5177 unsigned HOST_WIDE_INT offset
)
5179 /* If we cannot reach offset from start, lookup the first field
5180 and start from there. */
5181 if (start
->offset
> offset
)
5182 start
= get_varinfo (start
->head
);
5184 /* We may not find a variable in the field list with the actual
5185 offset when when we have glommed a structure to a variable.
5186 In that case, however, offset should still be within the size
5188 If we got beyond the offset we look for return the field
5189 directly preceding offset which may be the last field. */
5191 && offset
>= start
->offset
5192 && !((offset
- start
->offset
) < start
->size
))
5193 start
= vi_next (start
);
5199 /* This structure is used during pushing fields onto the fieldstack
5200 to track the offset of the field, since bitpos_of_field gives it
5201 relative to its immediate containing type, and we want it relative
5202 to the ultimate containing object. */
5206 /* Offset from the base of the base containing object to this field. */
5207 HOST_WIDE_INT offset
;
5209 /* Size, in bits, of the field. */
5210 unsigned HOST_WIDE_INT size
;
5212 unsigned has_unknown_size
: 1;
5214 unsigned must_have_pointers
: 1;
5216 unsigned may_have_pointers
: 1;
5218 unsigned only_restrict_pointers
: 1;
5220 typedef struct fieldoff fieldoff_s
;
5223 /* qsort comparison function for two fieldoff's PA and PB */
5226 fieldoff_compare (const void *pa
, const void *pb
)
5228 const fieldoff_s
*foa
= (const fieldoff_s
*)pa
;
5229 const fieldoff_s
*fob
= (const fieldoff_s
*)pb
;
5230 unsigned HOST_WIDE_INT foasize
, fobsize
;
5232 if (foa
->offset
< fob
->offset
)
5234 else if (foa
->offset
> fob
->offset
)
5237 foasize
= foa
->size
;
5238 fobsize
= fob
->size
;
5239 if (foasize
< fobsize
)
5241 else if (foasize
> fobsize
)
5246 /* Sort a fieldstack according to the field offset and sizes. */
5248 sort_fieldstack (vec
<fieldoff_s
> fieldstack
)
5250 fieldstack
.qsort (fieldoff_compare
);
5253 /* Return true if T is a type that can have subvars. */
5256 type_can_have_subvars (const_tree t
)
5258 /* Aggregates without overlapping fields can have subvars. */
5259 return TREE_CODE (t
) == RECORD_TYPE
;
5262 /* Return true if V is a tree that we can have subvars for.
5263 Normally, this is any aggregate type. Also complex
5264 types which are not gimple registers can have subvars. */
5267 var_can_have_subvars (const_tree v
)
5269 /* Volatile variables should never have subvars. */
5270 if (TREE_THIS_VOLATILE (v
))
5273 /* Non decls or memory tags can never have subvars. */
5277 return type_can_have_subvars (TREE_TYPE (v
));
5280 /* Return true if T is a type that does contain pointers. */
5283 type_must_have_pointers (tree type
)
5285 if (POINTER_TYPE_P (type
))
5288 if (TREE_CODE (type
) == ARRAY_TYPE
)
5289 return type_must_have_pointers (TREE_TYPE (type
));
5291 /* A function or method can have pointers as arguments, so track
5292 those separately. */
5293 if (TREE_CODE (type
) == FUNCTION_TYPE
5294 || TREE_CODE (type
) == METHOD_TYPE
)
5301 field_must_have_pointers (tree t
)
5303 return type_must_have_pointers (TREE_TYPE (t
));
5306 /* Given a TYPE, and a vector of field offsets FIELDSTACK, push all
5307 the fields of TYPE onto fieldstack, recording their offsets along
5310 OFFSET is used to keep track of the offset in this entire
5311 structure, rather than just the immediately containing structure.
5312 Returns false if the caller is supposed to handle the field we
5316 push_fields_onto_fieldstack (tree type
, vec
<fieldoff_s
> *fieldstack
,
5317 HOST_WIDE_INT offset
)
5320 bool empty_p
= true;
5322 if (TREE_CODE (type
) != RECORD_TYPE
)
5325 /* If the vector of fields is growing too big, bail out early.
5326 Callers check for vec::length <= MAX_FIELDS_FOR_FIELD_SENSITIVE, make
5328 if (fieldstack
->length () > MAX_FIELDS_FOR_FIELD_SENSITIVE
)
5331 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
5332 if (TREE_CODE (field
) == FIELD_DECL
)
5335 HOST_WIDE_INT foff
= bitpos_of_field (field
);
5337 if (!var_can_have_subvars (field
)
5338 || TREE_CODE (TREE_TYPE (field
)) == QUAL_UNION_TYPE
5339 || TREE_CODE (TREE_TYPE (field
)) == UNION_TYPE
)
5341 else if (!push_fields_onto_fieldstack
5342 (TREE_TYPE (field
), fieldstack
, offset
+ foff
)
5343 && (DECL_SIZE (field
)
5344 && !integer_zerop (DECL_SIZE (field
))))
5345 /* Empty structures may have actual size, like in C++. So
5346 see if we didn't push any subfields and the size is
5347 nonzero, push the field onto the stack. */
5352 fieldoff_s
*pair
= NULL
;
5353 bool has_unknown_size
= false;
5354 bool must_have_pointers_p
;
5356 if (!fieldstack
->is_empty ())
5357 pair
= &fieldstack
->last ();
5359 /* If there isn't anything at offset zero, create sth. */
5361 && offset
+ foff
!= 0)
5363 fieldoff_s e
= {0, offset
+ foff
, false, false, false, false};
5364 pair
= fieldstack
->safe_push (e
);
5367 if (!DECL_SIZE (field
)
5368 || !tree_fits_uhwi_p (DECL_SIZE (field
)))
5369 has_unknown_size
= true;
5371 /* If adjacent fields do not contain pointers merge them. */
5372 must_have_pointers_p
= field_must_have_pointers (field
);
5374 && !has_unknown_size
5375 && !must_have_pointers_p
5376 && !pair
->must_have_pointers
5377 && !pair
->has_unknown_size
5378 && pair
->offset
+ (HOST_WIDE_INT
)pair
->size
== offset
+ foff
)
5380 pair
->size
+= tree_to_uhwi (DECL_SIZE (field
));
5385 e
.offset
= offset
+ foff
;
5386 e
.has_unknown_size
= has_unknown_size
;
5387 if (!has_unknown_size
)
5388 e
.size
= tree_to_uhwi (DECL_SIZE (field
));
5391 e
.must_have_pointers
= must_have_pointers_p
;
5392 e
.may_have_pointers
= true;
5393 e
.only_restrict_pointers
5394 = (!has_unknown_size
5395 && POINTER_TYPE_P (TREE_TYPE (field
))
5396 && TYPE_RESTRICT (TREE_TYPE (field
)));
5397 fieldstack
->safe_push (e
);
5407 /* Count the number of arguments DECL has, and set IS_VARARGS to true
5408 if it is a varargs function. */
5411 count_num_arguments (tree decl
, bool *is_varargs
)
5413 unsigned int num
= 0;
5416 /* Capture named arguments for K&R functions. They do not
5417 have a prototype and thus no TYPE_ARG_TYPES. */
5418 for (t
= DECL_ARGUMENTS (decl
); t
; t
= DECL_CHAIN (t
))
5421 /* Check if the function has variadic arguments. */
5422 for (t
= TYPE_ARG_TYPES (TREE_TYPE (decl
)); t
; t
= TREE_CHAIN (t
))
5423 if (TREE_VALUE (t
) == void_type_node
)
5431 /* Creation function node for DECL, using NAME, and return the index
5432 of the variable we've created for the function. */
5435 create_function_info_for (tree decl
, const char *name
)
5437 struct function
*fn
= DECL_STRUCT_FUNCTION (decl
);
5438 varinfo_t vi
, prev_vi
;
5441 bool is_varargs
= false;
5442 unsigned int num_args
= count_num_arguments (decl
, &is_varargs
);
5444 /* Create the variable info. */
5446 vi
= new_var_info (decl
, name
);
5449 vi
->fullsize
= fi_parm_base
+ num_args
;
5451 vi
->may_have_pointers
= false;
5454 insert_vi_for_tree (vi
->decl
, vi
);
5458 /* Create a variable for things the function clobbers and one for
5459 things the function uses. */
5461 varinfo_t clobbervi
, usevi
;
5462 const char *newname
;
5465 tempname
= xasprintf ("%s.clobber", name
);
5466 newname
= ggc_strdup (tempname
);
5469 clobbervi
= new_var_info (NULL
, newname
);
5470 clobbervi
->offset
= fi_clobbers
;
5471 clobbervi
->size
= 1;
5472 clobbervi
->fullsize
= vi
->fullsize
;
5473 clobbervi
->is_full_var
= true;
5474 clobbervi
->is_global_var
= false;
5475 gcc_assert (prev_vi
->offset
< clobbervi
->offset
);
5476 prev_vi
->next
= clobbervi
->id
;
5477 prev_vi
= clobbervi
;
5479 tempname
= xasprintf ("%s.use", name
);
5480 newname
= ggc_strdup (tempname
);
5483 usevi
= new_var_info (NULL
, newname
);
5484 usevi
->offset
= fi_uses
;
5486 usevi
->fullsize
= vi
->fullsize
;
5487 usevi
->is_full_var
= true;
5488 usevi
->is_global_var
= false;
5489 gcc_assert (prev_vi
->offset
< usevi
->offset
);
5490 prev_vi
->next
= usevi
->id
;
5494 /* And one for the static chain. */
5495 if (fn
->static_chain_decl
!= NULL_TREE
)
5498 const char *newname
;
5501 tempname
= xasprintf ("%s.chain", name
);
5502 newname
= ggc_strdup (tempname
);
5505 chainvi
= new_var_info (fn
->static_chain_decl
, newname
);
5506 chainvi
->offset
= fi_static_chain
;
5508 chainvi
->fullsize
= vi
->fullsize
;
5509 chainvi
->is_full_var
= true;
5510 chainvi
->is_global_var
= false;
5511 gcc_assert (prev_vi
->offset
< chainvi
->offset
);
5512 prev_vi
->next
= chainvi
->id
;
5514 insert_vi_for_tree (fn
->static_chain_decl
, chainvi
);
5517 /* Create a variable for the return var. */
5518 if (DECL_RESULT (decl
) != NULL
5519 || !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl
))))
5522 const char *newname
;
5524 tree resultdecl
= decl
;
5526 if (DECL_RESULT (decl
))
5527 resultdecl
= DECL_RESULT (decl
);
5529 tempname
= xasprintf ("%s.result", name
);
5530 newname
= ggc_strdup (tempname
);
5533 resultvi
= new_var_info (resultdecl
, newname
);
5534 resultvi
->offset
= fi_result
;
5536 resultvi
->fullsize
= vi
->fullsize
;
5537 resultvi
->is_full_var
= true;
5538 if (DECL_RESULT (decl
))
5539 resultvi
->may_have_pointers
= true;
5540 gcc_assert (prev_vi
->offset
< resultvi
->offset
);
5541 prev_vi
->next
= resultvi
->id
;
5543 if (DECL_RESULT (decl
))
5544 insert_vi_for_tree (DECL_RESULT (decl
), resultvi
);
5547 /* Set up variables for each argument. */
5548 arg
= DECL_ARGUMENTS (decl
);
5549 for (i
= 0; i
< num_args
; i
++)
5552 const char *newname
;
5554 tree argdecl
= decl
;
5559 tempname
= xasprintf ("%s.arg%d", name
, i
);
5560 newname
= ggc_strdup (tempname
);
5563 argvi
= new_var_info (argdecl
, newname
);
5564 argvi
->offset
= fi_parm_base
+ i
;
5566 argvi
->is_full_var
= true;
5567 argvi
->fullsize
= vi
->fullsize
;
5569 argvi
->may_have_pointers
= true;
5570 gcc_assert (prev_vi
->offset
< argvi
->offset
);
5571 prev_vi
->next
= argvi
->id
;
5575 insert_vi_for_tree (arg
, argvi
);
5576 arg
= DECL_CHAIN (arg
);
5580 /* Add one representative for all further args. */
5584 const char *newname
;
5588 tempname
= xasprintf ("%s.varargs", name
);
5589 newname
= ggc_strdup (tempname
);
5592 /* We need sth that can be pointed to for va_start. */
5593 decl
= build_fake_var_decl (ptr_type_node
);
5595 argvi
= new_var_info (decl
, newname
);
5596 argvi
->offset
= fi_parm_base
+ num_args
;
5598 argvi
->is_full_var
= true;
5599 argvi
->is_heap_var
= true;
5600 argvi
->fullsize
= vi
->fullsize
;
5601 gcc_assert (prev_vi
->offset
< argvi
->offset
);
5602 prev_vi
->next
= argvi
->id
;
5610 /* Return true if FIELDSTACK contains fields that overlap.
5611 FIELDSTACK is assumed to be sorted by offset. */
5614 check_for_overlaps (vec
<fieldoff_s
> fieldstack
)
5616 fieldoff_s
*fo
= NULL
;
5618 HOST_WIDE_INT lastoffset
= -1;
5620 FOR_EACH_VEC_ELT (fieldstack
, i
, fo
)
5622 if (fo
->offset
== lastoffset
)
5624 lastoffset
= fo
->offset
;
5629 /* Create a varinfo structure for NAME and DECL, and add it to VARMAP.
5630 This will also create any varinfo structures necessary for fields
5634 create_variable_info_for_1 (tree decl
, const char *name
)
5636 varinfo_t vi
, newvi
;
5637 tree decl_type
= TREE_TYPE (decl
);
5638 tree declsize
= DECL_P (decl
) ? DECL_SIZE (decl
) : TYPE_SIZE (decl_type
);
5639 auto_vec
<fieldoff_s
> fieldstack
;
5642 varpool_node
*vnode
;
5645 || !tree_fits_uhwi_p (declsize
))
5647 vi
= new_var_info (decl
, name
);
5651 vi
->is_unknown_size_var
= true;
5652 vi
->is_full_var
= true;
5653 vi
->may_have_pointers
= true;
5657 /* Collect field information. */
5658 if (use_field_sensitive
5659 && var_can_have_subvars (decl
)
5660 /* ??? Force us to not use subfields for global initializers
5661 in IPA mode. Else we'd have to parse arbitrary initializers. */
5663 && is_global_var (decl
)
5664 && (vnode
= varpool_node::get (decl
))
5665 && vnode
->get_constructor ()))
5667 fieldoff_s
*fo
= NULL
;
5668 bool notokay
= false;
5671 push_fields_onto_fieldstack (decl_type
, &fieldstack
, 0);
5673 for (i
= 0; !notokay
&& fieldstack
.iterate (i
, &fo
); i
++)
5674 if (fo
->has_unknown_size
5681 /* We can't sort them if we have a field with a variable sized type,
5682 which will make notokay = true. In that case, we are going to return
5683 without creating varinfos for the fields anyway, so sorting them is a
5687 sort_fieldstack (fieldstack
);
5688 /* Due to some C++ FE issues, like PR 22488, we might end up
5689 what appear to be overlapping fields even though they,
5690 in reality, do not overlap. Until the C++ FE is fixed,
5691 we will simply disable field-sensitivity for these cases. */
5692 notokay
= check_for_overlaps (fieldstack
);
5696 fieldstack
.release ();
5699 /* If we didn't end up collecting sub-variables create a full
5700 variable for the decl. */
5701 if (fieldstack
.length () <= 1
5702 || fieldstack
.length () > MAX_FIELDS_FOR_FIELD_SENSITIVE
)
5704 vi
= new_var_info (decl
, name
);
5706 vi
->may_have_pointers
= true;
5707 vi
->fullsize
= tree_to_uhwi (declsize
);
5708 vi
->size
= vi
->fullsize
;
5709 vi
->is_full_var
= true;
5710 fieldstack
.release ();
5714 vi
= new_var_info (decl
, name
);
5715 vi
->fullsize
= tree_to_uhwi (declsize
);
5716 for (i
= 0, newvi
= vi
;
5717 fieldstack
.iterate (i
, &fo
);
5718 ++i
, newvi
= vi_next (newvi
))
5720 const char *newname
= "NULL";
5726 = xasprintf ("%s." HOST_WIDE_INT_PRINT_DEC
5727 "+" HOST_WIDE_INT_PRINT_DEC
, name
,
5728 fo
->offset
, fo
->size
);
5729 newname
= ggc_strdup (tempname
);
5732 newvi
->name
= newname
;
5733 newvi
->offset
= fo
->offset
;
5734 newvi
->size
= fo
->size
;
5735 newvi
->fullsize
= vi
->fullsize
;
5736 newvi
->may_have_pointers
= fo
->may_have_pointers
;
5737 newvi
->only_restrict_pointers
= fo
->only_restrict_pointers
;
5738 if (i
+ 1 < fieldstack
.length ())
5740 varinfo_t tem
= new_var_info (decl
, name
);
5741 newvi
->next
= tem
->id
;
5750 create_variable_info_for (tree decl
, const char *name
)
5752 varinfo_t vi
= create_variable_info_for_1 (decl
, name
);
5753 unsigned int id
= vi
->id
;
5755 insert_vi_for_tree (decl
, vi
);
5757 if (TREE_CODE (decl
) != VAR_DECL
)
5760 /* Create initial constraints for globals. */
5761 for (; vi
; vi
= vi_next (vi
))
5763 if (!vi
->may_have_pointers
5764 || !vi
->is_global_var
)
5767 /* Mark global restrict qualified pointers. */
5768 if ((POINTER_TYPE_P (TREE_TYPE (decl
))
5769 && TYPE_RESTRICT (TREE_TYPE (decl
)))
5770 || vi
->only_restrict_pointers
)
5773 = make_constraint_from_global_restrict (vi
, "GLOBAL_RESTRICT");
5774 /* ??? For now exclude reads from globals as restrict sources
5775 if those are not (indirectly) from incoming parameters. */
5776 rvi
->is_restrict_var
= false;
5780 /* In non-IPA mode the initializer from nonlocal is all we need. */
5782 || DECL_HARD_REGISTER (decl
))
5783 make_copy_constraint (vi
, nonlocal_id
);
5785 /* In IPA mode parse the initializer and generate proper constraints
5789 varpool_node
*vnode
= varpool_node::get (decl
);
5791 /* For escaped variables initialize them from nonlocal. */
5792 if (!vnode
->all_refs_explicit_p ())
5793 make_copy_constraint (vi
, nonlocal_id
);
5795 /* If this is a global variable with an initializer and we are in
5796 IPA mode generate constraints for it. */
5797 if (vnode
->get_constructor ()
5798 && vnode
->definition
)
5800 auto_vec
<ce_s
> rhsc
;
5801 struct constraint_expr lhs
, *rhsp
;
5803 get_constraint_for_rhs (vnode
->get_constructor (), &rhsc
);
5807 FOR_EACH_VEC_ELT (rhsc
, i
, rhsp
)
5808 process_constraint (new_constraint (lhs
, *rhsp
));
5809 /* If this is a variable that escapes from the unit
5810 the initializer escapes as well. */
5811 if (!vnode
->all_refs_explicit_p ())
5813 lhs
.var
= escaped_id
;
5816 FOR_EACH_VEC_ELT (rhsc
, i
, rhsp
)
5817 process_constraint (new_constraint (lhs
, *rhsp
));
5826 /* Print out the points-to solution for VAR to FILE. */
5829 dump_solution_for_var (FILE *file
, unsigned int var
)
5831 varinfo_t vi
= get_varinfo (var
);
5835 /* Dump the solution for unified vars anyway, this avoids difficulties
5836 in scanning dumps in the testsuite. */
5837 fprintf (file
, "%s = { ", vi
->name
);
5838 vi
= get_varinfo (find (var
));
5839 EXECUTE_IF_SET_IN_BITMAP (vi
->solution
, 0, i
, bi
)
5840 fprintf (file
, "%s ", get_varinfo (i
)->name
);
5841 fprintf (file
, "}");
5843 /* But note when the variable was unified. */
5845 fprintf (file
, " same as %s", vi
->name
);
5847 fprintf (file
, "\n");
5850 /* Print the points-to solution for VAR to stderr. */
5853 debug_solution_for_var (unsigned int var
)
5855 dump_solution_for_var (stderr
, var
);
5858 /* Create varinfo structures for all of the variables in the
5859 function for intraprocedural mode. */
5862 intra_create_variable_infos (struct function
*fn
)
5866 /* For each incoming pointer argument arg, create the constraint ARG
5867 = NONLOCAL or a dummy variable if it is a restrict qualified
5868 passed-by-reference argument. */
5869 for (t
= DECL_ARGUMENTS (fn
->decl
); t
; t
= DECL_CHAIN (t
))
5871 varinfo_t p
= get_vi_for_tree (t
);
5873 /* For restrict qualified pointers to objects passed by
5874 reference build a real representative for the pointed-to object.
5875 Treat restrict qualified references the same. */
5876 if (TYPE_RESTRICT (TREE_TYPE (t
))
5877 && ((DECL_BY_REFERENCE (t
) && POINTER_TYPE_P (TREE_TYPE (t
)))
5878 || TREE_CODE (TREE_TYPE (t
)) == REFERENCE_TYPE
)
5879 && !type_contains_placeholder_p (TREE_TYPE (TREE_TYPE (t
))))
5881 struct constraint_expr lhsc
, rhsc
;
5883 tree heapvar
= build_fake_var_decl (TREE_TYPE (TREE_TYPE (t
)));
5884 DECL_EXTERNAL (heapvar
) = 1;
5885 vi
= create_variable_info_for_1 (heapvar
, "PARM_NOALIAS");
5886 vi
->is_restrict_var
= 1;
5887 insert_vi_for_tree (heapvar
, vi
);
5892 rhsc
.type
= ADDRESSOF
;
5894 process_constraint (new_constraint (lhsc
, rhsc
));
5895 for (; vi
; vi
= vi_next (vi
))
5896 if (vi
->may_have_pointers
)
5898 if (vi
->only_restrict_pointers
)
5899 make_constraint_from_global_restrict (vi
, "GLOBAL_RESTRICT");
5901 make_copy_constraint (vi
, nonlocal_id
);
5906 if (POINTER_TYPE_P (TREE_TYPE (t
))
5907 && TYPE_RESTRICT (TREE_TYPE (t
)))
5908 make_constraint_from_global_restrict (p
, "PARM_RESTRICT");
5911 for (; p
; p
= vi_next (p
))
5913 if (p
->only_restrict_pointers
)
5914 make_constraint_from_global_restrict (p
, "PARM_RESTRICT");
5915 else if (p
->may_have_pointers
)
5916 make_constraint_from (p
, nonlocal_id
);
5921 /* Add a constraint for a result decl that is passed by reference. */
5922 if (DECL_RESULT (fn
->decl
)
5923 && DECL_BY_REFERENCE (DECL_RESULT (fn
->decl
)))
5925 varinfo_t p
, result_vi
= get_vi_for_tree (DECL_RESULT (fn
->decl
));
5927 for (p
= result_vi
; p
; p
= vi_next (p
))
5928 make_constraint_from (p
, nonlocal_id
);
5931 /* Add a constraint for the incoming static chain parameter. */
5932 if (fn
->static_chain_decl
!= NULL_TREE
)
5934 varinfo_t p
, chain_vi
= get_vi_for_tree (fn
->static_chain_decl
);
5936 for (p
= chain_vi
; p
; p
= vi_next (p
))
5937 make_constraint_from (p
, nonlocal_id
);
5941 /* Structure used to put solution bitmaps in a hashtable so they can
5942 be shared among variables with the same points-to set. */
5944 typedef struct shared_bitmap_info
5948 } *shared_bitmap_info_t
;
5949 typedef const struct shared_bitmap_info
*const_shared_bitmap_info_t
;
5951 /* Shared_bitmap hashtable helpers. */
5953 struct shared_bitmap_hasher
: typed_free_remove
<shared_bitmap_info
>
5955 typedef shared_bitmap_info
*value_type
;
5956 typedef shared_bitmap_info
*compare_type
;
5957 static inline hashval_t
hash (const shared_bitmap_info
*);
5958 static inline bool equal (const shared_bitmap_info
*,
5959 const shared_bitmap_info
*);
5962 /* Hash function for a shared_bitmap_info_t */
5965 shared_bitmap_hasher::hash (const shared_bitmap_info
*bi
)
5967 return bi
->hashcode
;
5970 /* Equality function for two shared_bitmap_info_t's. */
5973 shared_bitmap_hasher::equal (const shared_bitmap_info
*sbi1
,
5974 const shared_bitmap_info
*sbi2
)
5976 return bitmap_equal_p (sbi1
->pt_vars
, sbi2
->pt_vars
);
5979 /* Shared_bitmap hashtable. */
5981 static hash_table
<shared_bitmap_hasher
> *shared_bitmap_table
;
5983 /* Lookup a bitmap in the shared bitmap hashtable, and return an already
5984 existing instance if there is one, NULL otherwise. */
5987 shared_bitmap_lookup (bitmap pt_vars
)
5989 shared_bitmap_info
**slot
;
5990 struct shared_bitmap_info sbi
;
5992 sbi
.pt_vars
= pt_vars
;
5993 sbi
.hashcode
= bitmap_hash (pt_vars
);
5995 slot
= shared_bitmap_table
->find_slot (&sbi
, NO_INSERT
);
5999 return (*slot
)->pt_vars
;
6003 /* Add a bitmap to the shared bitmap hashtable. */
6006 shared_bitmap_add (bitmap pt_vars
)
6008 shared_bitmap_info
**slot
;
6009 shared_bitmap_info_t sbi
= XNEW (struct shared_bitmap_info
);
6011 sbi
->pt_vars
= pt_vars
;
6012 sbi
->hashcode
= bitmap_hash (pt_vars
);
6014 slot
= shared_bitmap_table
->find_slot (sbi
, INSERT
);
6015 gcc_assert (!*slot
);
6020 /* Set bits in INTO corresponding to the variable uids in solution set FROM. */
6023 set_uids_in_ptset (bitmap into
, bitmap from
, struct pt_solution
*pt
)
6027 varinfo_t escaped_vi
= get_varinfo (find (escaped_id
));
6028 bool everything_escaped
6029 = escaped_vi
->solution
&& bitmap_bit_p (escaped_vi
->solution
, anything_id
);
6031 EXECUTE_IF_SET_IN_BITMAP (from
, 0, i
, bi
)
6033 varinfo_t vi
= get_varinfo (i
);
6035 /* The only artificial variables that are allowed in a may-alias
6036 set are heap variables. */
6037 if (vi
->is_artificial_var
&& !vi
->is_heap_var
)
6040 if (everything_escaped
6041 || (escaped_vi
->solution
6042 && bitmap_bit_p (escaped_vi
->solution
, i
)))
6044 pt
->vars_contains_escaped
= true;
6045 pt
->vars_contains_escaped_heap
= vi
->is_heap_var
;
6048 if (TREE_CODE (vi
->decl
) == VAR_DECL
6049 || TREE_CODE (vi
->decl
) == PARM_DECL
6050 || TREE_CODE (vi
->decl
) == RESULT_DECL
)
6052 /* If we are in IPA mode we will not recompute points-to
6053 sets after inlining so make sure they stay valid. */
6055 && !DECL_PT_UID_SET_P (vi
->decl
))
6056 SET_DECL_PT_UID (vi
->decl
, DECL_UID (vi
->decl
));
6058 /* Add the decl to the points-to set. Note that the points-to
6059 set contains global variables. */
6060 bitmap_set_bit (into
, DECL_PT_UID (vi
->decl
));
6061 if (vi
->is_global_var
)
6062 pt
->vars_contains_nonlocal
= true;
6068 /* Compute the points-to solution *PT for the variable VI. */
6070 static struct pt_solution
6071 find_what_var_points_to (varinfo_t orig_vi
)
6075 bitmap finished_solution
;
6078 struct pt_solution
*pt
;
6080 /* This variable may have been collapsed, let's get the real
6082 vi
= get_varinfo (find (orig_vi
->id
));
6084 /* See if we have already computed the solution and return it. */
6085 pt_solution
**slot
= &final_solutions
->get_or_insert (vi
);
6089 *slot
= pt
= XOBNEW (&final_solutions_obstack
, struct pt_solution
);
6090 memset (pt
, 0, sizeof (struct pt_solution
));
6092 /* Translate artificial variables into SSA_NAME_PTR_INFO
6094 EXECUTE_IF_SET_IN_BITMAP (vi
->solution
, 0, i
, bi
)
6096 varinfo_t vi
= get_varinfo (i
);
6098 if (vi
->is_artificial_var
)
6100 if (vi
->id
== nothing_id
)
6102 else if (vi
->id
== escaped_id
)
6105 pt
->ipa_escaped
= 1;
6108 /* Expand some special vars of ESCAPED in-place here. */
6109 varinfo_t evi
= get_varinfo (find (escaped_id
));
6110 if (bitmap_bit_p (evi
->solution
, nonlocal_id
))
6113 else if (vi
->id
== nonlocal_id
)
6115 else if (vi
->is_heap_var
)
6116 /* We represent heapvars in the points-to set properly. */
6118 else if (vi
->id
== string_id
)
6119 /* Nobody cares - STRING_CSTs are read-only entities. */
6121 else if (vi
->id
== anything_id
6122 || vi
->id
== integer_id
)
6127 /* Instead of doing extra work, simply do not create
6128 elaborate points-to information for pt_anything pointers. */
6132 /* Share the final set of variables when possible. */
6133 finished_solution
= BITMAP_GGC_ALLOC ();
6134 stats
.points_to_sets_created
++;
6136 set_uids_in_ptset (finished_solution
, vi
->solution
, pt
);
6137 result
= shared_bitmap_lookup (finished_solution
);
6140 shared_bitmap_add (finished_solution
);
6141 pt
->vars
= finished_solution
;
6146 bitmap_clear (finished_solution
);
6152 /* Given a pointer variable P, fill in its points-to set. */
6155 find_what_p_points_to (tree p
)
6157 struct ptr_info_def
*pi
;
6161 /* For parameters, get at the points-to set for the actual parm
6163 if (TREE_CODE (p
) == SSA_NAME
6164 && SSA_NAME_IS_DEFAULT_DEF (p
)
6165 && (TREE_CODE (SSA_NAME_VAR (p
)) == PARM_DECL
6166 || TREE_CODE (SSA_NAME_VAR (p
)) == RESULT_DECL
))
6167 lookup_p
= SSA_NAME_VAR (p
);
6169 vi
= lookup_vi_for_tree (lookup_p
);
6173 pi
= get_ptr_info (p
);
6174 pi
->pt
= find_what_var_points_to (vi
);
6178 /* Query statistics for points-to solutions. */
6181 unsigned HOST_WIDE_INT pt_solution_includes_may_alias
;
6182 unsigned HOST_WIDE_INT pt_solution_includes_no_alias
;
6183 unsigned HOST_WIDE_INT pt_solutions_intersect_may_alias
;
6184 unsigned HOST_WIDE_INT pt_solutions_intersect_no_alias
;
6188 dump_pta_stats (FILE *s
)
6190 fprintf (s
, "\nPTA query stats:\n");
6191 fprintf (s
, " pt_solution_includes: "
6192 HOST_WIDE_INT_PRINT_DEC
" disambiguations, "
6193 HOST_WIDE_INT_PRINT_DEC
" queries\n",
6194 pta_stats
.pt_solution_includes_no_alias
,
6195 pta_stats
.pt_solution_includes_no_alias
6196 + pta_stats
.pt_solution_includes_may_alias
);
6197 fprintf (s
, " pt_solutions_intersect: "
6198 HOST_WIDE_INT_PRINT_DEC
" disambiguations, "
6199 HOST_WIDE_INT_PRINT_DEC
" queries\n",
6200 pta_stats
.pt_solutions_intersect_no_alias
,
6201 pta_stats
.pt_solutions_intersect_no_alias
6202 + pta_stats
.pt_solutions_intersect_may_alias
);
6206 /* Reset the points-to solution *PT to a conservative default
6207 (point to anything). */
6210 pt_solution_reset (struct pt_solution
*pt
)
6212 memset (pt
, 0, sizeof (struct pt_solution
));
6213 pt
->anything
= true;
6216 /* Set the points-to solution *PT to point only to the variables
6217 in VARS. VARS_CONTAINS_GLOBAL specifies whether that contains
6218 global variables and VARS_CONTAINS_RESTRICT specifies whether
6219 it contains restrict tag variables. */
6222 pt_solution_set (struct pt_solution
*pt
, bitmap vars
,
6223 bool vars_contains_nonlocal
)
6225 memset (pt
, 0, sizeof (struct pt_solution
));
6227 pt
->vars_contains_nonlocal
= vars_contains_nonlocal
;
6228 pt
->vars_contains_escaped
6229 = (cfun
->gimple_df
->escaped
.anything
6230 || bitmap_intersect_p (cfun
->gimple_df
->escaped
.vars
, vars
));
6233 /* Set the points-to solution *PT to point only to the variable VAR. */
6236 pt_solution_set_var (struct pt_solution
*pt
, tree var
)
6238 memset (pt
, 0, sizeof (struct pt_solution
));
6239 pt
->vars
= BITMAP_GGC_ALLOC ();
6240 bitmap_set_bit (pt
->vars
, DECL_PT_UID (var
));
6241 pt
->vars_contains_nonlocal
= is_global_var (var
);
6242 pt
->vars_contains_escaped
6243 = (cfun
->gimple_df
->escaped
.anything
6244 || bitmap_bit_p (cfun
->gimple_df
->escaped
.vars
, DECL_PT_UID (var
)));
6247 /* Computes the union of the points-to solutions *DEST and *SRC and
6248 stores the result in *DEST. This changes the points-to bitmap
6249 of *DEST and thus may not be used if that might be shared.
6250 The points-to bitmap of *SRC and *DEST will not be shared after
6251 this function if they were not before. */
6254 pt_solution_ior_into (struct pt_solution
*dest
, struct pt_solution
*src
)
6256 dest
->anything
|= src
->anything
;
6259 pt_solution_reset (dest
);
6263 dest
->nonlocal
|= src
->nonlocal
;
6264 dest
->escaped
|= src
->escaped
;
6265 dest
->ipa_escaped
|= src
->ipa_escaped
;
6266 dest
->null
|= src
->null
;
6267 dest
->vars_contains_nonlocal
|= src
->vars_contains_nonlocal
;
6268 dest
->vars_contains_escaped
|= src
->vars_contains_escaped
;
6269 dest
->vars_contains_escaped_heap
|= src
->vars_contains_escaped_heap
;
6274 dest
->vars
= BITMAP_GGC_ALLOC ();
6275 bitmap_ior_into (dest
->vars
, src
->vars
);
6278 /* Return true if the points-to solution *PT is empty. */
6281 pt_solution_empty_p (struct pt_solution
*pt
)
6288 && !bitmap_empty_p (pt
->vars
))
6291 /* If the solution includes ESCAPED, check if that is empty. */
6293 && !pt_solution_empty_p (&cfun
->gimple_df
->escaped
))
6296 /* If the solution includes ESCAPED, check if that is empty. */
6298 && !pt_solution_empty_p (&ipa_escaped_pt
))
6304 /* Return true if the points-to solution *PT only point to a single var, and
6305 return the var uid in *UID. */
6308 pt_solution_singleton_p (struct pt_solution
*pt
, unsigned *uid
)
6310 if (pt
->anything
|| pt
->nonlocal
|| pt
->escaped
|| pt
->ipa_escaped
6311 || pt
->null
|| pt
->vars
== NULL
6312 || !bitmap_single_bit_set_p (pt
->vars
))
6315 *uid
= bitmap_first_set_bit (pt
->vars
);
6319 /* Return true if the points-to solution *PT includes global memory. */
6322 pt_solution_includes_global (struct pt_solution
*pt
)
6326 || pt
->vars_contains_nonlocal
6327 /* The following is a hack to make the malloc escape hack work.
6328 In reality we'd need different sets for escaped-through-return
6329 and escaped-to-callees and passes would need to be updated. */
6330 || pt
->vars_contains_escaped_heap
)
6333 /* 'escaped' is also a placeholder so we have to look into it. */
6335 return pt_solution_includes_global (&cfun
->gimple_df
->escaped
);
6337 if (pt
->ipa_escaped
)
6338 return pt_solution_includes_global (&ipa_escaped_pt
);
6340 /* ??? This predicate is not correct for the IPA-PTA solution
6341 as we do not properly distinguish between unit escape points
6342 and global variables. */
6343 if (cfun
->gimple_df
->ipa_pta
)
6349 /* Return true if the points-to solution *PT includes the variable
6350 declaration DECL. */
6353 pt_solution_includes_1 (struct pt_solution
*pt
, const_tree decl
)
6359 && is_global_var (decl
))
6363 && bitmap_bit_p (pt
->vars
, DECL_PT_UID (decl
)))
6366 /* If the solution includes ESCAPED, check it. */
6368 && pt_solution_includes_1 (&cfun
->gimple_df
->escaped
, decl
))
6371 /* If the solution includes ESCAPED, check it. */
6373 && pt_solution_includes_1 (&ipa_escaped_pt
, decl
))
6380 pt_solution_includes (struct pt_solution
*pt
, const_tree decl
)
6382 bool res
= pt_solution_includes_1 (pt
, decl
);
6384 ++pta_stats
.pt_solution_includes_may_alias
;
6386 ++pta_stats
.pt_solution_includes_no_alias
;
6390 /* Return true if both points-to solutions PT1 and PT2 have a non-empty
6394 pt_solutions_intersect_1 (struct pt_solution
*pt1
, struct pt_solution
*pt2
)
6396 if (pt1
->anything
|| pt2
->anything
)
6399 /* If either points to unknown global memory and the other points to
6400 any global memory they alias. */
6403 || pt2
->vars_contains_nonlocal
))
6405 && pt1
->vars_contains_nonlocal
))
6408 /* If either points to all escaped memory and the other points to
6409 any escaped memory they alias. */
6412 || pt2
->vars_contains_escaped
))
6414 && pt1
->vars_contains_escaped
))
6417 /* Check the escaped solution if required.
6418 ??? Do we need to check the local against the IPA escaped sets? */
6419 if ((pt1
->ipa_escaped
|| pt2
->ipa_escaped
)
6420 && !pt_solution_empty_p (&ipa_escaped_pt
))
6422 /* If both point to escaped memory and that solution
6423 is not empty they alias. */
6424 if (pt1
->ipa_escaped
&& pt2
->ipa_escaped
)
6427 /* If either points to escaped memory see if the escaped solution
6428 intersects with the other. */
6429 if ((pt1
->ipa_escaped
6430 && pt_solutions_intersect_1 (&ipa_escaped_pt
, pt2
))
6431 || (pt2
->ipa_escaped
6432 && pt_solutions_intersect_1 (&ipa_escaped_pt
, pt1
)))
6436 /* Now both pointers alias if their points-to solution intersects. */
6439 && bitmap_intersect_p (pt1
->vars
, pt2
->vars
));
6443 pt_solutions_intersect (struct pt_solution
*pt1
, struct pt_solution
*pt2
)
6445 bool res
= pt_solutions_intersect_1 (pt1
, pt2
);
6447 ++pta_stats
.pt_solutions_intersect_may_alias
;
6449 ++pta_stats
.pt_solutions_intersect_no_alias
;
6454 /* Dump points-to information to OUTFILE. */
6457 dump_sa_points_to_info (FILE *outfile
)
6461 fprintf (outfile
, "\nPoints-to sets\n\n");
6463 if (dump_flags
& TDF_STATS
)
6465 fprintf (outfile
, "Stats:\n");
6466 fprintf (outfile
, "Total vars: %d\n", stats
.total_vars
);
6467 fprintf (outfile
, "Non-pointer vars: %d\n",
6468 stats
.nonpointer_vars
);
6469 fprintf (outfile
, "Statically unified vars: %d\n",
6470 stats
.unified_vars_static
);
6471 fprintf (outfile
, "Dynamically unified vars: %d\n",
6472 stats
.unified_vars_dynamic
);
6473 fprintf (outfile
, "Iterations: %d\n", stats
.iterations
);
6474 fprintf (outfile
, "Number of edges: %d\n", stats
.num_edges
);
6475 fprintf (outfile
, "Number of implicit edges: %d\n",
6476 stats
.num_implicit_edges
);
6479 for (i
= 1; i
< varmap
.length (); i
++)
6481 varinfo_t vi
= get_varinfo (i
);
6482 if (!vi
->may_have_pointers
)
6484 dump_solution_for_var (outfile
, i
);
6489 /* Debug points-to information to stderr. */
6492 debug_sa_points_to_info (void)
6494 dump_sa_points_to_info (stderr
);
6498 /* Initialize the always-existing constraint variables for NULL
6499 ANYTHING, READONLY, and INTEGER */
6502 init_base_vars (void)
6504 struct constraint_expr lhs
, rhs
;
6505 varinfo_t var_anything
;
6506 varinfo_t var_nothing
;
6507 varinfo_t var_string
;
6508 varinfo_t var_escaped
;
6509 varinfo_t var_nonlocal
;
6510 varinfo_t var_storedanything
;
6511 varinfo_t var_integer
;
6513 /* Variable ID zero is reserved and should be NULL. */
6514 varmap
.safe_push (NULL
);
6516 /* Create the NULL variable, used to represent that a variable points
6518 var_nothing
= new_var_info (NULL_TREE
, "NULL");
6519 gcc_assert (var_nothing
->id
== nothing_id
);
6520 var_nothing
->is_artificial_var
= 1;
6521 var_nothing
->offset
= 0;
6522 var_nothing
->size
= ~0;
6523 var_nothing
->fullsize
= ~0;
6524 var_nothing
->is_special_var
= 1;
6525 var_nothing
->may_have_pointers
= 0;
6526 var_nothing
->is_global_var
= 0;
6528 /* Create the ANYTHING variable, used to represent that a variable
6529 points to some unknown piece of memory. */
6530 var_anything
= new_var_info (NULL_TREE
, "ANYTHING");
6531 gcc_assert (var_anything
->id
== anything_id
);
6532 var_anything
->is_artificial_var
= 1;
6533 var_anything
->size
= ~0;
6534 var_anything
->offset
= 0;
6535 var_anything
->fullsize
= ~0;
6536 var_anything
->is_special_var
= 1;
6538 /* Anything points to anything. This makes deref constraints just
6539 work in the presence of linked list and other p = *p type loops,
6540 by saying that *ANYTHING = ANYTHING. */
6542 lhs
.var
= anything_id
;
6544 rhs
.type
= ADDRESSOF
;
6545 rhs
.var
= anything_id
;
6548 /* This specifically does not use process_constraint because
6549 process_constraint ignores all anything = anything constraints, since all
6550 but this one are redundant. */
6551 constraints
.safe_push (new_constraint (lhs
, rhs
));
6553 /* Create the STRING variable, used to represent that a variable
6554 points to a string literal. String literals don't contain
6555 pointers so STRING doesn't point to anything. */
6556 var_string
= new_var_info (NULL_TREE
, "STRING");
6557 gcc_assert (var_string
->id
== string_id
);
6558 var_string
->is_artificial_var
= 1;
6559 var_string
->offset
= 0;
6560 var_string
->size
= ~0;
6561 var_string
->fullsize
= ~0;
6562 var_string
->is_special_var
= 1;
6563 var_string
->may_have_pointers
= 0;
6565 /* Create the ESCAPED variable, used to represent the set of escaped
6567 var_escaped
= new_var_info (NULL_TREE
, "ESCAPED");
6568 gcc_assert (var_escaped
->id
== escaped_id
);
6569 var_escaped
->is_artificial_var
= 1;
6570 var_escaped
->offset
= 0;
6571 var_escaped
->size
= ~0;
6572 var_escaped
->fullsize
= ~0;
6573 var_escaped
->is_special_var
= 0;
6575 /* Create the NONLOCAL variable, used to represent the set of nonlocal
6577 var_nonlocal
= new_var_info (NULL_TREE
, "NONLOCAL");
6578 gcc_assert (var_nonlocal
->id
== nonlocal_id
);
6579 var_nonlocal
->is_artificial_var
= 1;
6580 var_nonlocal
->offset
= 0;
6581 var_nonlocal
->size
= ~0;
6582 var_nonlocal
->fullsize
= ~0;
6583 var_nonlocal
->is_special_var
= 1;
6585 /* ESCAPED = *ESCAPED, because escaped is may-deref'd at calls, etc. */
6587 lhs
.var
= escaped_id
;
6590 rhs
.var
= escaped_id
;
6592 process_constraint (new_constraint (lhs
, rhs
));
6594 /* ESCAPED = ESCAPED + UNKNOWN_OFFSET, because if a sub-field escapes the
6595 whole variable escapes. */
6597 lhs
.var
= escaped_id
;
6600 rhs
.var
= escaped_id
;
6601 rhs
.offset
= UNKNOWN_OFFSET
;
6602 process_constraint (new_constraint (lhs
, rhs
));
6604 /* *ESCAPED = NONLOCAL. This is true because we have to assume
6605 everything pointed to by escaped points to what global memory can
6608 lhs
.var
= escaped_id
;
6611 rhs
.var
= nonlocal_id
;
6613 process_constraint (new_constraint (lhs
, rhs
));
6615 /* NONLOCAL = &NONLOCAL, NONLOCAL = &ESCAPED. This is true because
6616 global memory may point to global memory and escaped memory. */
6618 lhs
.var
= nonlocal_id
;
6620 rhs
.type
= ADDRESSOF
;
6621 rhs
.var
= nonlocal_id
;
6623 process_constraint (new_constraint (lhs
, rhs
));
6624 rhs
.type
= ADDRESSOF
;
6625 rhs
.var
= escaped_id
;
6627 process_constraint (new_constraint (lhs
, rhs
));
6629 /* Create the STOREDANYTHING variable, used to represent the set of
6630 variables stored to *ANYTHING. */
6631 var_storedanything
= new_var_info (NULL_TREE
, "STOREDANYTHING");
6632 gcc_assert (var_storedanything
->id
== storedanything_id
);
6633 var_storedanything
->is_artificial_var
= 1;
6634 var_storedanything
->offset
= 0;
6635 var_storedanything
->size
= ~0;
6636 var_storedanything
->fullsize
= ~0;
6637 var_storedanything
->is_special_var
= 0;
6639 /* Create the INTEGER variable, used to represent that a variable points
6640 to what an INTEGER "points to". */
6641 var_integer
= new_var_info (NULL_TREE
, "INTEGER");
6642 gcc_assert (var_integer
->id
== integer_id
);
6643 var_integer
->is_artificial_var
= 1;
6644 var_integer
->size
= ~0;
6645 var_integer
->fullsize
= ~0;
6646 var_integer
->offset
= 0;
6647 var_integer
->is_special_var
= 1;
6649 /* INTEGER = ANYTHING, because we don't know where a dereference of
6650 a random integer will point to. */
6652 lhs
.var
= integer_id
;
6654 rhs
.type
= ADDRESSOF
;
6655 rhs
.var
= anything_id
;
6657 process_constraint (new_constraint (lhs
, rhs
));
6660 /* Initialize things necessary to perform PTA */
6663 init_alias_vars (void)
6665 use_field_sensitive
= (MAX_FIELDS_FOR_FIELD_SENSITIVE
> 1);
6667 bitmap_obstack_initialize (&pta_obstack
);
6668 bitmap_obstack_initialize (&oldpta_obstack
);
6669 bitmap_obstack_initialize (&predbitmap_obstack
);
6671 constraints
.create (8);
6673 vi_for_tree
= new hash_map
<tree
, varinfo_t
>;
6674 call_stmt_vars
= new hash_map
<gimple
, varinfo_t
>;
6676 memset (&stats
, 0, sizeof (stats
));
6677 shared_bitmap_table
= new hash_table
<shared_bitmap_hasher
> (511);
6680 gcc_obstack_init (&fake_var_decl_obstack
);
6682 final_solutions
= new hash_map
<varinfo_t
, pt_solution
*>;
6683 gcc_obstack_init (&final_solutions_obstack
);
6686 /* Remove the REF and ADDRESS edges from GRAPH, as well as all the
6687 predecessor edges. */
6690 remove_preds_and_fake_succs (constraint_graph_t graph
)
6694 /* Clear the implicit ref and address nodes from the successor
6696 for (i
= 1; i
< FIRST_REF_NODE
; i
++)
6698 if (graph
->succs
[i
])
6699 bitmap_clear_range (graph
->succs
[i
], FIRST_REF_NODE
,
6700 FIRST_REF_NODE
* 2);
6703 /* Free the successor list for the non-ref nodes. */
6704 for (i
= FIRST_REF_NODE
+ 1; i
< graph
->size
; i
++)
6706 if (graph
->succs
[i
])
6707 BITMAP_FREE (graph
->succs
[i
]);
6710 /* Now reallocate the size of the successor list as, and blow away
6711 the predecessor bitmaps. */
6712 graph
->size
= varmap
.length ();
6713 graph
->succs
= XRESIZEVEC (bitmap
, graph
->succs
, graph
->size
);
6715 free (graph
->implicit_preds
);
6716 graph
->implicit_preds
= NULL
;
6717 free (graph
->preds
);
6718 graph
->preds
= NULL
;
6719 bitmap_obstack_release (&predbitmap_obstack
);
6722 /* Solve the constraint set. */
6725 solve_constraints (void)
6727 struct scc_info
*si
;
6731 "\nCollapsing static cycles and doing variable "
6734 init_graph (varmap
.length () * 2);
6737 fprintf (dump_file
, "Building predecessor graph\n");
6738 build_pred_graph ();
6741 fprintf (dump_file
, "Detecting pointer and location "
6743 si
= perform_var_substitution (graph
);
6746 fprintf (dump_file
, "Rewriting constraints and unifying "
6748 rewrite_constraints (graph
, si
);
6750 build_succ_graph ();
6752 free_var_substitution_info (si
);
6754 /* Attach complex constraints to graph nodes. */
6755 move_complex_constraints (graph
);
6758 fprintf (dump_file
, "Uniting pointer but not location equivalent "
6760 unite_pointer_equivalences (graph
);
6763 fprintf (dump_file
, "Finding indirect cycles\n");
6764 find_indirect_cycles (graph
);
6766 /* Implicit nodes and predecessors are no longer necessary at this
6768 remove_preds_and_fake_succs (graph
);
6770 if (dump_file
&& (dump_flags
& TDF_GRAPH
))
6772 fprintf (dump_file
, "\n\n// The constraint graph before solve-graph "
6773 "in dot format:\n");
6774 dump_constraint_graph (dump_file
);
6775 fprintf (dump_file
, "\n\n");
6779 fprintf (dump_file
, "Solving graph\n");
6781 solve_graph (graph
);
6783 if (dump_file
&& (dump_flags
& TDF_GRAPH
))
6785 fprintf (dump_file
, "\n\n// The constraint graph after solve-graph "
6786 "in dot format:\n");
6787 dump_constraint_graph (dump_file
);
6788 fprintf (dump_file
, "\n\n");
6792 dump_sa_points_to_info (dump_file
);
6795 /* Create points-to sets for the current function. See the comments
6796 at the start of the file for an algorithmic overview. */
6799 compute_points_to_sets (void)
6805 timevar_push (TV_TREE_PTA
);
6809 intra_create_variable_infos (cfun
);
6811 /* Now walk all statements and build the constraint set. */
6812 FOR_EACH_BB_FN (bb
, cfun
)
6814 for (gphi_iterator gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
);
6817 gphi
*phi
= gsi
.phi ();
6819 if (! virtual_operand_p (gimple_phi_result (phi
)))
6820 find_func_aliases (cfun
, phi
);
6823 for (gimple_stmt_iterator gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
);
6826 gimple stmt
= gsi_stmt (gsi
);
6828 find_func_aliases (cfun
, stmt
);
6834 fprintf (dump_file
, "Points-to analysis\n\nConstraints:\n\n");
6835 dump_constraints (dump_file
, 0);
6838 /* From the constraints compute the points-to sets. */
6839 solve_constraints ();
6841 /* Compute the points-to set for ESCAPED used for call-clobber analysis. */
6842 cfun
->gimple_df
->escaped
= find_what_var_points_to (get_varinfo (escaped_id
));
6844 /* Make sure the ESCAPED solution (which is used as placeholder in
6845 other solutions) does not reference itself. This simplifies
6846 points-to solution queries. */
6847 cfun
->gimple_df
->escaped
.escaped
= 0;
6849 /* Compute the points-to sets for pointer SSA_NAMEs. */
6850 for (i
= 0; i
< num_ssa_names
; ++i
)
6852 tree ptr
= ssa_name (i
);
6854 && POINTER_TYPE_P (TREE_TYPE (ptr
)))
6855 find_what_p_points_to (ptr
);
6858 /* Compute the call-used/clobbered sets. */
6859 FOR_EACH_BB_FN (bb
, cfun
)
6861 gimple_stmt_iterator gsi
;
6863 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
6866 struct pt_solution
*pt
;
6868 stmt
= dyn_cast
<gcall
*> (gsi_stmt (gsi
));
6872 pt
= gimple_call_use_set (stmt
);
6873 if (gimple_call_flags (stmt
) & ECF_CONST
)
6874 memset (pt
, 0, sizeof (struct pt_solution
));
6875 else if ((vi
= lookup_call_use_vi (stmt
)) != NULL
)
6877 *pt
= find_what_var_points_to (vi
);
6878 /* Escaped (and thus nonlocal) variables are always
6879 implicitly used by calls. */
6880 /* ??? ESCAPED can be empty even though NONLOCAL
6887 /* If there is nothing special about this call then
6888 we have made everything that is used also escape. */
6889 *pt
= cfun
->gimple_df
->escaped
;
6893 pt
= gimple_call_clobber_set (stmt
);
6894 if (gimple_call_flags (stmt
) & (ECF_CONST
|ECF_PURE
|ECF_NOVOPS
))
6895 memset (pt
, 0, sizeof (struct pt_solution
));
6896 else if ((vi
= lookup_call_clobber_vi (stmt
)) != NULL
)
6898 *pt
= find_what_var_points_to (vi
);
6899 /* Escaped (and thus nonlocal) variables are always
6900 implicitly clobbered by calls. */
6901 /* ??? ESCAPED can be empty even though NONLOCAL
6908 /* If there is nothing special about this call then
6909 we have made everything that is used also escape. */
6910 *pt
= cfun
->gimple_df
->escaped
;
6916 timevar_pop (TV_TREE_PTA
);
6920 /* Delete created points-to sets. */
6923 delete_points_to_sets (void)
6927 delete shared_bitmap_table
;
6928 shared_bitmap_table
= NULL
;
6929 if (dump_file
&& (dump_flags
& TDF_STATS
))
6930 fprintf (dump_file
, "Points to sets created:%d\n",
6931 stats
.points_to_sets_created
);
6934 delete call_stmt_vars
;
6935 bitmap_obstack_release (&pta_obstack
);
6936 constraints
.release ();
6938 for (i
= 0; i
< graph
->size
; i
++)
6939 graph
->complex[i
].release ();
6940 free (graph
->complex);
6943 free (graph
->succs
);
6945 free (graph
->pe_rep
);
6946 free (graph
->indirect_cycles
);
6950 variable_info_pool
.release ();
6951 constraint_pool
.release ();
6953 obstack_free (&fake_var_decl_obstack
, NULL
);
6955 delete final_solutions
;
6956 obstack_free (&final_solutions_obstack
, NULL
);
6959 /* Mark "other" loads and stores as belonging to CLIQUE and with
6963 visit_loadstore (gimple
, tree base
, tree ref
, void *clique_
)
6965 unsigned short clique
= (uintptr_t)clique_
;
6966 if (TREE_CODE (base
) == MEM_REF
6967 || TREE_CODE (base
) == TARGET_MEM_REF
)
6969 tree ptr
= TREE_OPERAND (base
, 0);
6970 if (TREE_CODE (ptr
) == SSA_NAME
)
6972 /* ??? We need to make sure 'ptr' doesn't include any of
6973 the restrict tags in its points-to set. */
6977 /* For now let decls through. */
6979 /* Do not overwrite existing cliques (that includes clique, base
6980 pairs we just set). */
6981 if (MR_DEPENDENCE_CLIQUE (base
) == 0)
6983 MR_DEPENDENCE_CLIQUE (base
) = clique
;
6984 MR_DEPENDENCE_BASE (base
) = 0;
6988 /* For plain decl accesses see whether they are accesses to globals
6989 and rewrite them to MEM_REFs with { clique, 0 }. */
6990 if (TREE_CODE (base
) == VAR_DECL
6991 && is_global_var (base
)
6992 /* ??? We can't rewrite a plain decl with the walk_stmt_load_store
6997 while (handled_component_p (*basep
))
6998 basep
= &TREE_OPERAND (*basep
, 0);
6999 gcc_assert (TREE_CODE (*basep
) == VAR_DECL
);
7000 tree ptr
= build_fold_addr_expr (*basep
);
7001 tree zero
= build_int_cst (TREE_TYPE (ptr
), 0);
7002 *basep
= build2 (MEM_REF
, TREE_TYPE (*basep
), ptr
, zero
);
7003 MR_DEPENDENCE_CLIQUE (*basep
) = clique
;
7004 MR_DEPENDENCE_BASE (*basep
) = 0;
7010 /* If REF is a MEM_REF then assign a clique, base pair to it, updating
7011 CLIQUE, *RESTRICT_VAR and LAST_RUID. Return whether dependence info
7012 was assigned to REF. */
7015 maybe_set_dependence_info (tree ref
, tree ptr
,
7016 unsigned short &clique
, varinfo_t restrict_var
,
7017 unsigned short &last_ruid
)
7019 while (handled_component_p (ref
))
7020 ref
= TREE_OPERAND (ref
, 0);
7021 if ((TREE_CODE (ref
) == MEM_REF
7022 || TREE_CODE (ref
) == TARGET_MEM_REF
)
7023 && TREE_OPERAND (ref
, 0) == ptr
)
7025 /* Do not overwrite existing cliques. This avoids overwriting dependence
7026 info inlined from a function with restrict parameters inlined
7027 into a function with restrict parameters. This usually means we
7028 prefer to be precise in innermost loops. */
7029 if (MR_DEPENDENCE_CLIQUE (ref
) == 0)
7032 clique
= ++cfun
->last_clique
;
7033 if (restrict_var
->ruid
== 0)
7034 restrict_var
->ruid
= ++last_ruid
;
7035 MR_DEPENDENCE_CLIQUE (ref
) = clique
;
7036 MR_DEPENDENCE_BASE (ref
) = restrict_var
->ruid
;
7043 /* Compute the set of independend memory references based on restrict
7044 tags and their conservative propagation to the points-to sets. */
7047 compute_dependence_clique (void)
7049 unsigned short clique
= 0;
7050 unsigned short last_ruid
= 0;
7051 for (unsigned i
= 0; i
< num_ssa_names
; ++i
)
7053 tree ptr
= ssa_name (i
);
7054 if (!ptr
|| !POINTER_TYPE_P (TREE_TYPE (ptr
)))
7057 /* Avoid all this when ptr is not dereferenced? */
7059 if (SSA_NAME_IS_DEFAULT_DEF (ptr
)
7060 && (TREE_CODE (SSA_NAME_VAR (ptr
)) == PARM_DECL
7061 || TREE_CODE (SSA_NAME_VAR (ptr
)) == RESULT_DECL
))
7062 p
= SSA_NAME_VAR (ptr
);
7063 varinfo_t vi
= lookup_vi_for_tree (p
);
7066 vi
= get_varinfo (find (vi
->id
));
7069 varinfo_t restrict_var
= NULL
;
7070 EXECUTE_IF_SET_IN_BITMAP (vi
->solution
, 0, j
, bi
)
7072 varinfo_t oi
= get_varinfo (j
);
7073 if (oi
->is_restrict_var
)
7077 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7079 fprintf (dump_file
, "found restrict pointed-to "
7081 print_generic_expr (dump_file
, ptr
, 0);
7082 fprintf (dump_file
, " but not exclusively\n");
7084 restrict_var
= NULL
;
7089 /* NULL is the only other valid points-to entry. */
7090 else if (oi
->id
!= nothing_id
)
7092 restrict_var
= NULL
;
7096 /* Ok, found that ptr must(!) point to a single(!) restrict
7098 /* ??? PTA isn't really a proper propagation engine to compute
7100 ??? We could handle merging of two restricts by unifying them. */
7103 /* Now look at possible dereferences of ptr. */
7104 imm_use_iterator ui
;
7106 FOR_EACH_IMM_USE_STMT (use_stmt
, ui
, ptr
)
7108 /* ??? Calls and asms. */
7109 if (!gimple_assign_single_p (use_stmt
))
7111 maybe_set_dependence_info (gimple_assign_lhs (use_stmt
), ptr
,
7112 clique
, restrict_var
, last_ruid
);
7113 maybe_set_dependence_info (gimple_assign_rhs1 (use_stmt
), ptr
,
7114 clique
, restrict_var
, last_ruid
);
7122 /* Assign the BASE id zero to all accesses not based on a restrict
7123 pointer. That way they get disabiguated against restrict
7124 accesses but not against each other. */
7125 /* ??? For restricts derived from globals (thus not incoming
7126 parameters) we can't restrict scoping properly thus the following
7127 is too aggressive there. For now we have excluded those globals from
7128 getting into the MR_DEPENDENCE machinery. */
7130 FOR_EACH_BB_FN (bb
, cfun
)
7131 for (gimple_stmt_iterator gsi
= gsi_start_bb (bb
);
7132 !gsi_end_p (gsi
); gsi_next (&gsi
))
7134 gimple stmt
= gsi_stmt (gsi
);
7135 walk_stmt_load_store_ops (stmt
, (void *)(uintptr_t)clique
,
7136 visit_loadstore
, visit_loadstore
);
7140 /* Compute points-to information for every SSA_NAME pointer in the
7141 current function and compute the transitive closure of escaped
7142 variables to re-initialize the call-clobber states of local variables. */
7145 compute_may_aliases (void)
7147 if (cfun
->gimple_df
->ipa_pta
)
7151 fprintf (dump_file
, "\nNot re-computing points-to information "
7152 "because IPA points-to information is available.\n\n");
7154 /* But still dump what we have remaining it. */
7155 dump_alias_info (dump_file
);
7161 /* For each pointer P_i, determine the sets of variables that P_i may
7162 point-to. Compute the reachability set of escaped and call-used
7164 compute_points_to_sets ();
7166 /* Debugging dumps. */
7168 dump_alias_info (dump_file
);
7170 /* Compute restrict-based memory disambiguations. */
7171 compute_dependence_clique ();
7173 /* Deallocate memory used by aliasing data structures and the internal
7174 points-to solution. */
7175 delete_points_to_sets ();
7177 gcc_assert (!need_ssa_update_p (cfun
));
7182 /* A dummy pass to cause points-to information to be computed via
7183 TODO_rebuild_alias. */
7187 const pass_data pass_data_build_alias
=
7189 GIMPLE_PASS
, /* type */
7191 OPTGROUP_NONE
, /* optinfo_flags */
7192 TV_NONE
, /* tv_id */
7193 ( PROP_cfg
| PROP_ssa
), /* properties_required */
7194 0, /* properties_provided */
7195 0, /* properties_destroyed */
7196 0, /* todo_flags_start */
7197 TODO_rebuild_alias
, /* todo_flags_finish */
7200 class pass_build_alias
: public gimple_opt_pass
7203 pass_build_alias (gcc::context
*ctxt
)
7204 : gimple_opt_pass (pass_data_build_alias
, ctxt
)
7207 /* opt_pass methods: */
7208 virtual bool gate (function
*) { return flag_tree_pta
; }
7210 }; // class pass_build_alias
7215 make_pass_build_alias (gcc::context
*ctxt
)
7217 return new pass_build_alias (ctxt
);
7220 /* A dummy pass to cause points-to information to be computed via
7221 TODO_rebuild_alias. */
7225 const pass_data pass_data_build_ealias
=
7227 GIMPLE_PASS
, /* type */
7228 "ealias", /* name */
7229 OPTGROUP_NONE
, /* optinfo_flags */
7230 TV_NONE
, /* tv_id */
7231 ( PROP_cfg
| PROP_ssa
), /* properties_required */
7232 0, /* properties_provided */
7233 0, /* properties_destroyed */
7234 0, /* todo_flags_start */
7235 TODO_rebuild_alias
, /* todo_flags_finish */
7238 class pass_build_ealias
: public gimple_opt_pass
7241 pass_build_ealias (gcc::context
*ctxt
)
7242 : gimple_opt_pass (pass_data_build_ealias
, ctxt
)
7245 /* opt_pass methods: */
7246 virtual bool gate (function
*) { return flag_tree_pta
; }
7248 }; // class pass_build_ealias
7253 make_pass_build_ealias (gcc::context
*ctxt
)
7255 return new pass_build_ealias (ctxt
);
7259 /* IPA PTA solutions for ESCAPED. */
7260 struct pt_solution ipa_escaped_pt
7261 = { true, false, false, false, false, false, false, false, NULL
};
7263 /* Associate node with varinfo DATA. Worker for
7264 cgraph_for_node_and_aliases. */
7266 associate_varinfo_to_alias (struct cgraph_node
*node
, void *data
)
7268 if ((node
->alias
|| node
->thunk
.thunk_p
)
7270 insert_vi_for_tree (node
->decl
, (varinfo_t
)data
);
7274 /* Execute the driver for IPA PTA. */
7276 ipa_pta_execute (void)
7278 struct cgraph_node
*node
;
7286 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7288 symtab_node::dump_table (dump_file
);
7289 fprintf (dump_file
, "\n");
7292 /* Build the constraints. */
7293 FOR_EACH_DEFINED_FUNCTION (node
)
7296 /* Nodes without a body are not interesting. Especially do not
7297 visit clones at this point for now - we get duplicate decls
7298 there for inline clones at least. */
7299 if (!node
->has_gimple_body_p () || node
->global
.inlined_to
)
7303 gcc_assert (!node
->clone_of
);
7305 vi
= create_function_info_for (node
->decl
,
7306 alias_get_name (node
->decl
));
7307 node
->call_for_symbol_thunks_and_aliases
7308 (associate_varinfo_to_alias
, vi
, true);
7311 /* Create constraints for global variables and their initializers. */
7312 FOR_EACH_VARIABLE (var
)
7314 if (var
->alias
&& var
->analyzed
)
7317 get_vi_for_tree (var
->decl
);
7323 "Generating constraints for global initializers\n\n");
7324 dump_constraints (dump_file
, 0);
7325 fprintf (dump_file
, "\n");
7327 from
= constraints
.length ();
7329 FOR_EACH_DEFINED_FUNCTION (node
)
7331 struct function
*func
;
7334 /* Nodes without a body are not interesting. */
7335 if (!node
->has_gimple_body_p () || node
->clone_of
)
7341 "Generating constraints for %s", node
->name ());
7342 if (DECL_ASSEMBLER_NAME_SET_P (node
->decl
))
7343 fprintf (dump_file
, " (%s)",
7345 (DECL_ASSEMBLER_NAME (node
->decl
)));
7346 fprintf (dump_file
, "\n");
7349 func
= DECL_STRUCT_FUNCTION (node
->decl
);
7350 gcc_assert (cfun
== NULL
);
7352 /* For externally visible or attribute used annotated functions use
7353 local constraints for their arguments.
7354 For local functions we see all callers and thus do not need initial
7355 constraints for parameters. */
7356 if (node
->used_from_other_partition
7357 || node
->externally_visible
7358 || node
->force_output
7359 || node
->address_taken
)
7361 intra_create_variable_infos (func
);
7363 /* We also need to make function return values escape. Nothing
7364 escapes by returning from main though. */
7365 if (!MAIN_NAME_P (DECL_NAME (node
->decl
)))
7368 fi
= lookup_vi_for_tree (node
->decl
);
7369 rvi
= first_vi_for_offset (fi
, fi_result
);
7370 if (rvi
&& rvi
->offset
== fi_result
)
7372 struct constraint_expr includes
;
7373 struct constraint_expr var
;
7374 includes
.var
= escaped_id
;
7375 includes
.offset
= 0;
7376 includes
.type
= SCALAR
;
7380 process_constraint (new_constraint (includes
, var
));
7385 /* Build constriants for the function body. */
7386 FOR_EACH_BB_FN (bb
, func
)
7388 for (gphi_iterator gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
);
7391 gphi
*phi
= gsi
.phi ();
7393 if (! virtual_operand_p (gimple_phi_result (phi
)))
7394 find_func_aliases (func
, phi
);
7397 for (gimple_stmt_iterator gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
);
7400 gimple stmt
= gsi_stmt (gsi
);
7402 find_func_aliases (func
, stmt
);
7403 find_func_clobbers (func
, stmt
);
7409 fprintf (dump_file
, "\n");
7410 dump_constraints (dump_file
, from
);
7411 fprintf (dump_file
, "\n");
7413 from
= constraints
.length ();
7416 /* From the constraints compute the points-to sets. */
7417 solve_constraints ();
7419 /* Compute the global points-to sets for ESCAPED.
7420 ??? Note that the computed escape set is not correct
7421 for the whole unit as we fail to consider graph edges to
7422 externally visible functions. */
7423 ipa_escaped_pt
= find_what_var_points_to (get_varinfo (escaped_id
));
7425 /* Make sure the ESCAPED solution (which is used as placeholder in
7426 other solutions) does not reference itself. This simplifies
7427 points-to solution queries. */
7428 ipa_escaped_pt
.ipa_escaped
= 0;
7430 /* Assign the points-to sets to the SSA names in the unit. */
7431 FOR_EACH_DEFINED_FUNCTION (node
)
7434 struct function
*fn
;
7438 /* Nodes without a body are not interesting. */
7439 if (!node
->has_gimple_body_p () || node
->clone_of
)
7442 fn
= DECL_STRUCT_FUNCTION (node
->decl
);
7444 /* Compute the points-to sets for pointer SSA_NAMEs. */
7445 FOR_EACH_VEC_ELT (*fn
->gimple_df
->ssa_names
, i
, ptr
)
7448 && POINTER_TYPE_P (TREE_TYPE (ptr
)))
7449 find_what_p_points_to (ptr
);
7452 /* Compute the call-use and call-clobber sets for indirect calls
7453 and calls to external functions. */
7454 FOR_EACH_BB_FN (bb
, fn
)
7456 gimple_stmt_iterator gsi
;
7458 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
7461 struct pt_solution
*pt
;
7465 stmt
= dyn_cast
<gcall
*> (gsi_stmt (gsi
));
7469 /* Handle direct calls to functions with body. */
7470 decl
= gimple_call_fndecl (stmt
);
7472 && (fi
= lookup_vi_for_tree (decl
))
7475 *gimple_call_clobber_set (stmt
)
7476 = find_what_var_points_to
7477 (first_vi_for_offset (fi
, fi_clobbers
));
7478 *gimple_call_use_set (stmt
)
7479 = find_what_var_points_to
7480 (first_vi_for_offset (fi
, fi_uses
));
7482 /* Handle direct calls to external functions. */
7485 pt
= gimple_call_use_set (stmt
);
7486 if (gimple_call_flags (stmt
) & ECF_CONST
)
7487 memset (pt
, 0, sizeof (struct pt_solution
));
7488 else if ((vi
= lookup_call_use_vi (stmt
)) != NULL
)
7490 *pt
= find_what_var_points_to (vi
);
7491 /* Escaped (and thus nonlocal) variables are always
7492 implicitly used by calls. */
7493 /* ??? ESCAPED can be empty even though NONLOCAL
7496 pt
->ipa_escaped
= 1;
7500 /* If there is nothing special about this call then
7501 we have made everything that is used also escape. */
7502 *pt
= ipa_escaped_pt
;
7506 pt
= gimple_call_clobber_set (stmt
);
7507 if (gimple_call_flags (stmt
) & (ECF_CONST
|ECF_PURE
|ECF_NOVOPS
))
7508 memset (pt
, 0, sizeof (struct pt_solution
));
7509 else if ((vi
= lookup_call_clobber_vi (stmt
)) != NULL
)
7511 *pt
= find_what_var_points_to (vi
);
7512 /* Escaped (and thus nonlocal) variables are always
7513 implicitly clobbered by calls. */
7514 /* ??? ESCAPED can be empty even though NONLOCAL
7517 pt
->ipa_escaped
= 1;
7521 /* If there is nothing special about this call then
7522 we have made everything that is used also escape. */
7523 *pt
= ipa_escaped_pt
;
7527 /* Handle indirect calls. */
7529 && (fi
= get_fi_for_callee (stmt
)))
7531 /* We need to accumulate all clobbers/uses of all possible
7533 fi
= get_varinfo (find (fi
->id
));
7534 /* If we cannot constrain the set of functions we'll end up
7535 calling we end up using/clobbering everything. */
7536 if (bitmap_bit_p (fi
->solution
, anything_id
)
7537 || bitmap_bit_p (fi
->solution
, nonlocal_id
)
7538 || bitmap_bit_p (fi
->solution
, escaped_id
))
7540 pt_solution_reset (gimple_call_clobber_set (stmt
));
7541 pt_solution_reset (gimple_call_use_set (stmt
));
7547 struct pt_solution
*uses
, *clobbers
;
7549 uses
= gimple_call_use_set (stmt
);
7550 clobbers
= gimple_call_clobber_set (stmt
);
7551 memset (uses
, 0, sizeof (struct pt_solution
));
7552 memset (clobbers
, 0, sizeof (struct pt_solution
));
7553 EXECUTE_IF_SET_IN_BITMAP (fi
->solution
, 0, i
, bi
)
7555 struct pt_solution sol
;
7557 vi
= get_varinfo (i
);
7558 if (!vi
->is_fn_info
)
7560 /* ??? We could be more precise here? */
7562 uses
->ipa_escaped
= 1;
7563 clobbers
->nonlocal
= 1;
7564 clobbers
->ipa_escaped
= 1;
7568 if (!uses
->anything
)
7570 sol
= find_what_var_points_to
7571 (first_vi_for_offset (vi
, fi_uses
));
7572 pt_solution_ior_into (uses
, &sol
);
7574 if (!clobbers
->anything
)
7576 sol
= find_what_var_points_to
7577 (first_vi_for_offset (vi
, fi_clobbers
));
7578 pt_solution_ior_into (clobbers
, &sol
);
7586 fn
->gimple_df
->ipa_pta
= true;
7589 delete_points_to_sets ();
7598 const pass_data pass_data_ipa_pta
=
7600 SIMPLE_IPA_PASS
, /* type */
7602 OPTGROUP_NONE
, /* optinfo_flags */
7603 TV_IPA_PTA
, /* tv_id */
7604 0, /* properties_required */
7605 0, /* properties_provided */
7606 0, /* properties_destroyed */
7607 0, /* todo_flags_start */
7608 0, /* todo_flags_finish */
7611 class pass_ipa_pta
: public simple_ipa_opt_pass
7614 pass_ipa_pta (gcc::context
*ctxt
)
7615 : simple_ipa_opt_pass (pass_data_ipa_pta
, ctxt
)
7618 /* opt_pass methods: */
7619 virtual bool gate (function
*)
7623 /* Don't bother doing anything if the program has errors. */
7627 virtual unsigned int execute (function
*) { return ipa_pta_execute (); }
7629 }; // class pass_ipa_pta
7633 simple_ipa_opt_pass
*
7634 make_pass_ipa_pta (gcc::context
*ctxt
)
7636 return new pass_ipa_pta (ctxt
);