1 /* Tree based points-to analysis
2 Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011
3 Free Software Foundation, Inc.
4 Contributed by Daniel Berlin <dberlin@dberlin.org>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
30 #include "basic-block.h"
32 #include "tree-flow.h"
33 #include "tree-inline.h"
34 #include "diagnostic-core.h"
39 #include "tree-pass.h"
40 #include "alloc-pool.h"
41 #include "splay-tree.h"
45 #include "pointer-set.h"
47 /* The idea behind this analyzer is to generate set constraints from the
48 program, then solve the resulting constraints in order to generate the
51 Set constraints are a way of modeling program analysis problems that
52 involve sets. They consist of an inclusion constraint language,
53 describing the variables (each variable is a set) and operations that
54 are involved on the variables, and a set of rules that derive facts
55 from these operations. To solve a system of set constraints, you derive
56 all possible facts under the rules, which gives you the correct sets
59 See "Efficient Field-sensitive pointer analysis for C" by "David
60 J. Pearce and Paul H. J. Kelly and Chris Hankin, at
61 http://citeseer.ist.psu.edu/pearce04efficient.html
63 Also see "Ultra-fast Aliasing Analysis using CLA: A Million Lines
64 of C Code in a Second" by ""Nevin Heintze and Olivier Tardieu" at
65 http://citeseer.ist.psu.edu/heintze01ultrafast.html
67 There are three types of real constraint expressions, DEREF,
68 ADDRESSOF, and SCALAR. Each constraint expression consists
69 of a constraint type, a variable, and an offset.
71 SCALAR is a constraint expression type used to represent x, whether
72 it appears on the LHS or the RHS of a statement.
73 DEREF is a constraint expression type used to represent *x, whether
74 it appears on the LHS or the RHS of a statement.
75 ADDRESSOF is a constraint expression used to represent &x, whether
76 it appears on the LHS or the RHS of a statement.
78 Each pointer variable in the program is assigned an integer id, and
79 each field of a structure variable is assigned an integer id as well.
81 Structure variables are linked to their list of fields through a "next
82 field" in each variable that points to the next field in offset
84 Each variable for a structure field has
86 1. "size", that tells the size in bits of that field.
87 2. "fullsize, that tells the size in bits of the entire structure.
88 3. "offset", that tells the offset in bits from the beginning of the
89 structure to this field.
101 foo.a -> id 1, size 32, offset 0, fullsize 64, next foo.b
102 foo.b -> id 2, size 32, offset 32, fullsize 64, next NULL
103 bar -> id 3, size 32, offset 0, fullsize 32, next NULL
106 In order to solve the system of set constraints, the following is
109 1. Each constraint variable x has a solution set associated with it,
112 2. Constraints are separated into direct, copy, and complex.
113 Direct constraints are ADDRESSOF constraints that require no extra
114 processing, such as P = &Q
115 Copy constraints are those of the form P = Q.
116 Complex constraints are all the constraints involving dereferences
117 and offsets (including offsetted copies).
119 3. All direct constraints of the form P = &Q are processed, such
120 that Q is added to Sol(P)
122 4. All complex constraints for a given constraint variable are stored in a
123 linked list attached to that variable's node.
125 5. A directed graph is built out of the copy constraints. Each
126 constraint variable is a node in the graph, and an edge from
127 Q to P is added for each copy constraint of the form P = Q
129 6. The graph is then walked, and solution sets are
130 propagated along the copy edges, such that an edge from Q to P
131 causes Sol(P) <- Sol(P) union Sol(Q).
133 7. As we visit each node, all complex constraints associated with
134 that node are processed by adding appropriate copy edges to the graph, or the
135 appropriate variables to the solution set.
137 8. The process of walking the graph is iterated until no solution
140 Prior to walking the graph in steps 6 and 7, We perform static
141 cycle elimination on the constraint graph, as well
142 as off-line variable substitution.
144 TODO: Adding offsets to pointer-to-structures can be handled (IE not punted
145 on and turned into anything), but isn't. You can just see what offset
146 inside the pointed-to struct it's going to access.
148 TODO: Constant bounded arrays can be handled as if they were structs of the
149 same number of elements.
151 TODO: Modeling heap and incoming pointers becomes much better if we
152 add fields to them as we discover them, which we could do.
154 TODO: We could handle unions, but to be honest, it's probably not
155 worth the pain or slowdown. */
157 /* IPA-PTA optimizations possible.
159 When the indirect function called is ANYTHING we can add disambiguation
160 based on the function signatures (or simply the parameter count which
161 is the varinfo size). We also do not need to consider functions that
162 do not have their address taken.
164 The is_global_var bit which marks escape points is overly conservative
165 in IPA mode. Split it to is_escape_point and is_global_var - only
166 externally visible globals are escape points in IPA mode. This is
167 also needed to fix the pt_solution_includes_global predicate
168 (and thus ptr_deref_may_alias_global_p).
170 The way we introduce DECL_PT_UID to avoid fixing up all points-to
171 sets in the translation unit when we copy a DECL during inlining
172 pessimizes precision. The advantage is that the DECL_PT_UID keeps
173 compile-time and memory usage overhead low - the points-to sets
174 do not grow or get unshared as they would during a fixup phase.
175 An alternative solution is to delay IPA PTA until after all
176 inlining transformations have been applied.
178 The way we propagate clobber/use information isn't optimized.
179 It should use a new complex constraint that properly filters
180 out local variables of the callee (though that would make
181 the sets invalid after inlining). OTOH we might as well
182 admit defeat to WHOPR and simply do all the clobber/use analysis
183 and propagation after PTA finished but before we threw away
184 points-to information for memory variables. WHOPR and PTA
185 do not play along well anyway - the whole constraint solving
186 would need to be done in WPA phase and it will be very interesting
187 to apply the results to local SSA names during LTRANS phase.
189 We probably should compute a per-function unit-ESCAPE solution
190 propagating it simply like the clobber / uses solutions. The
191 solution can go alongside the non-IPA espaced solution and be
192 used to query which vars escape the unit through a function.
194 We never put function decls in points-to sets so we do not
195 keep the set of called functions for indirect calls.
197 And probably more. */
199 static bool use_field_sensitive
= true;
200 static int in_ipa_mode
= 0;
202 /* Used for predecessor bitmaps. */
203 static bitmap_obstack predbitmap_obstack
;
205 /* Used for points-to sets. */
206 static bitmap_obstack pta_obstack
;
208 /* Used for oldsolution members of variables. */
209 static bitmap_obstack oldpta_obstack
;
211 /* Used for per-solver-iteration bitmaps. */
212 static bitmap_obstack iteration_obstack
;
214 static unsigned int create_variable_info_for (tree
, const char *);
215 typedef struct constraint_graph
*constraint_graph_t
;
216 static void unify_nodes (constraint_graph_t
, unsigned int, unsigned int, bool);
219 typedef struct constraint
*constraint_t
;
221 DEF_VEC_P(constraint_t
);
222 DEF_VEC_ALLOC_P(constraint_t
,heap
);
224 #define EXECUTE_IF_IN_NONNULL_BITMAP(a, b, c, d) \
226 EXECUTE_IF_SET_IN_BITMAP (a, b, c, d)
228 static struct constraint_stats
230 unsigned int total_vars
;
231 unsigned int nonpointer_vars
;
232 unsigned int unified_vars_static
;
233 unsigned int unified_vars_dynamic
;
234 unsigned int iterations
;
235 unsigned int num_edges
;
236 unsigned int num_implicit_edges
;
237 unsigned int points_to_sets_created
;
242 /* ID of this variable */
245 /* True if this is a variable created by the constraint analysis, such as
246 heap variables and constraints we had to break up. */
247 unsigned int is_artificial_var
: 1;
249 /* True if this is a special variable whose solution set should not be
251 unsigned int is_special_var
: 1;
253 /* True for variables whose size is not known or variable. */
254 unsigned int is_unknown_size_var
: 1;
256 /* True for (sub-)fields that represent a whole variable. */
257 unsigned int is_full_var
: 1;
259 /* True if this is a heap variable. */
260 unsigned int is_heap_var
: 1;
262 /* True if this field may contain pointers. */
263 unsigned int may_have_pointers
: 1;
265 /* True if this field has only restrict qualified pointers. */
266 unsigned int only_restrict_pointers
: 1;
268 /* True if this represents a global variable. */
269 unsigned int is_global_var
: 1;
271 /* True if this represents a IPA function info. */
272 unsigned int is_fn_info
: 1;
274 /* A link to the variable for the next field in this structure. */
275 struct variable_info
*next
;
277 /* Offset of this variable, in bits, from the base variable */
278 unsigned HOST_WIDE_INT offset
;
280 /* Size of the variable, in bits. */
281 unsigned HOST_WIDE_INT size
;
283 /* Full size of the base variable, in bits. */
284 unsigned HOST_WIDE_INT fullsize
;
286 /* Name of this variable */
289 /* Tree that this variable is associated with. */
292 /* Points-to set for this variable. */
295 /* Old points-to set for this variable. */
298 typedef struct variable_info
*varinfo_t
;
300 static varinfo_t
first_vi_for_offset (varinfo_t
, unsigned HOST_WIDE_INT
);
301 static varinfo_t
first_or_preceding_vi_for_offset (varinfo_t
,
302 unsigned HOST_WIDE_INT
);
303 static varinfo_t
lookup_vi_for_tree (tree
);
304 static inline bool type_can_have_subvars (const_tree
);
306 /* Pool of variable info structures. */
307 static alloc_pool variable_info_pool
;
309 DEF_VEC_P(varinfo_t
);
311 DEF_VEC_ALLOC_P(varinfo_t
, heap
);
313 /* Table of variable info structures for constraint variables.
314 Indexed directly by variable info id. */
315 static VEC(varinfo_t
,heap
) *varmap
;
317 /* Return the varmap element N */
319 static inline varinfo_t
320 get_varinfo (unsigned int n
)
322 return VEC_index (varinfo_t
, varmap
, n
);
325 /* Static IDs for the special variables. */
326 enum { nothing_id
= 0, anything_id
= 1, readonly_id
= 2,
327 escaped_id
= 3, nonlocal_id
= 4,
328 storedanything_id
= 5, integer_id
= 6 };
330 /* Return a new variable info structure consisting for a variable
331 named NAME, and using constraint graph node NODE. Append it
332 to the vector of variable info structures. */
335 new_var_info (tree t
, const char *name
)
337 unsigned index
= VEC_length (varinfo_t
, varmap
);
338 varinfo_t ret
= (varinfo_t
) pool_alloc (variable_info_pool
);
343 /* Vars without decl are artificial and do not have sub-variables. */
344 ret
->is_artificial_var
= (t
== NULL_TREE
);
345 ret
->is_special_var
= false;
346 ret
->is_unknown_size_var
= false;
347 ret
->is_full_var
= (t
== NULL_TREE
);
348 ret
->is_heap_var
= false;
349 ret
->may_have_pointers
= true;
350 ret
->only_restrict_pointers
= false;
351 ret
->is_global_var
= (t
== NULL_TREE
);
352 ret
->is_fn_info
= false;
354 ret
->is_global_var
= (is_global_var (t
)
355 /* We have to treat even local register variables
357 || (TREE_CODE (t
) == VAR_DECL
358 && DECL_HARD_REGISTER (t
)));
359 ret
->solution
= BITMAP_ALLOC (&pta_obstack
);
360 ret
->oldsolution
= NULL
;
365 VEC_safe_push (varinfo_t
, heap
, varmap
, ret
);
371 /* A map mapping call statements to per-stmt variables for uses
372 and clobbers specific to the call. */
373 struct pointer_map_t
*call_stmt_vars
;
375 /* Lookup or create the variable for the call statement CALL. */
378 get_call_vi (gimple call
)
383 slot_p
= pointer_map_insert (call_stmt_vars
, call
);
385 return (varinfo_t
) *slot_p
;
387 vi
= new_var_info (NULL_TREE
, "CALLUSED");
391 vi
->is_full_var
= true;
393 vi
->next
= vi2
= new_var_info (NULL_TREE
, "CALLCLOBBERED");
397 vi2
->is_full_var
= true;
399 *slot_p
= (void *) vi
;
403 /* Lookup the variable for the call statement CALL representing
404 the uses. Returns NULL if there is nothing special about this call. */
407 lookup_call_use_vi (gimple call
)
411 slot_p
= pointer_map_contains (call_stmt_vars
, call
);
413 return (varinfo_t
) *slot_p
;
418 /* Lookup the variable for the call statement CALL representing
419 the clobbers. Returns NULL if there is nothing special about this call. */
422 lookup_call_clobber_vi (gimple call
)
424 varinfo_t uses
= lookup_call_use_vi (call
);
431 /* Lookup or create the variable for the call statement CALL representing
435 get_call_use_vi (gimple call
)
437 return get_call_vi (call
);
440 /* Lookup or create the variable for the call statement CALL representing
443 static varinfo_t ATTRIBUTE_UNUSED
444 get_call_clobber_vi (gimple call
)
446 return get_call_vi (call
)->next
;
450 typedef enum {SCALAR
, DEREF
, ADDRESSOF
} constraint_expr_type
;
452 /* An expression that appears in a constraint. */
454 struct constraint_expr
456 /* Constraint type. */
457 constraint_expr_type type
;
459 /* Variable we are referring to in the constraint. */
462 /* Offset, in bits, of this constraint from the beginning of
463 variables it ends up referring to.
465 IOW, in a deref constraint, we would deref, get the result set,
466 then add OFFSET to each member. */
467 HOST_WIDE_INT offset
;
470 /* Use 0x8000... as special unknown offset. */
471 #define UNKNOWN_OFFSET ((HOST_WIDE_INT)-1 << (HOST_BITS_PER_WIDE_INT-1))
473 typedef struct constraint_expr ce_s
;
475 DEF_VEC_ALLOC_O(ce_s
, heap
);
476 static void get_constraint_for_1 (tree
, VEC(ce_s
, heap
) **, bool, bool);
477 static void get_constraint_for (tree
, VEC(ce_s
, heap
) **);
478 static void get_constraint_for_rhs (tree
, VEC(ce_s
, heap
) **);
479 static void do_deref (VEC (ce_s
, heap
) **);
481 /* Our set constraints are made up of two constraint expressions, one
484 As described in the introduction, our set constraints each represent an
485 operation between set valued variables.
489 struct constraint_expr lhs
;
490 struct constraint_expr rhs
;
493 /* List of constraints that we use to build the constraint graph from. */
495 static VEC(constraint_t
,heap
) *constraints
;
496 static alloc_pool constraint_pool
;
498 /* The constraint graph is represented as an array of bitmaps
499 containing successor nodes. */
501 struct constraint_graph
503 /* Size of this graph, which may be different than the number of
504 nodes in the variable map. */
507 /* Explicit successors of each node. */
510 /* Implicit predecessors of each node (Used for variable
512 bitmap
*implicit_preds
;
514 /* Explicit predecessors of each node (Used for variable substitution). */
517 /* Indirect cycle representatives, or -1 if the node has no indirect
519 int *indirect_cycles
;
521 /* Representative node for a node. rep[a] == a unless the node has
525 /* Equivalence class representative for a label. This is used for
526 variable substitution. */
529 /* Pointer equivalence label for a node. All nodes with the same
530 pointer equivalence label can be unified together at some point
531 (either during constraint optimization or after the constraint
535 /* Pointer equivalence representative for a label. This is used to
536 handle nodes that are pointer equivalent but not location
537 equivalent. We can unite these once the addressof constraints
538 are transformed into initial points-to sets. */
541 /* Pointer equivalence label for each node, used during variable
543 unsigned int *pointer_label
;
545 /* Location equivalence label for each node, used during location
546 equivalence finding. */
547 unsigned int *loc_label
;
549 /* Pointed-by set for each node, used during location equivalence
550 finding. This is pointed-by rather than pointed-to, because it
551 is constructed using the predecessor graph. */
554 /* Points to sets for pointer equivalence. This is *not* the actual
555 points-to sets for nodes. */
558 /* Bitmap of nodes where the bit is set if the node is a direct
559 node. Used for variable substitution. */
560 sbitmap direct_nodes
;
562 /* Bitmap of nodes where the bit is set if the node is address
563 taken. Used for variable substitution. */
564 bitmap address_taken
;
566 /* Vector of complex constraints for each graph node. Complex
567 constraints are those involving dereferences or offsets that are
569 VEC(constraint_t
,heap
) **complex;
572 static constraint_graph_t graph
;
574 /* During variable substitution and the offline version of indirect
575 cycle finding, we create nodes to represent dereferences and
576 address taken constraints. These represent where these start and
578 #define FIRST_REF_NODE (VEC_length (varinfo_t, varmap))
579 #define LAST_REF_NODE (FIRST_REF_NODE + (FIRST_REF_NODE - 1))
581 /* Return the representative node for NODE, if NODE has been unioned
583 This function performs path compression along the way to finding
584 the representative. */
587 find (unsigned int node
)
589 gcc_assert (node
< graph
->size
);
590 if (graph
->rep
[node
] != node
)
591 return graph
->rep
[node
] = find (graph
->rep
[node
]);
595 /* Union the TO and FROM nodes to the TO nodes.
596 Note that at some point in the future, we may want to do
597 union-by-rank, in which case we are going to have to return the
598 node we unified to. */
601 unite (unsigned int to
, unsigned int from
)
603 gcc_assert (to
< graph
->size
&& from
< graph
->size
);
604 if (to
!= from
&& graph
->rep
[from
] != to
)
606 graph
->rep
[from
] = to
;
612 /* Create a new constraint consisting of LHS and RHS expressions. */
615 new_constraint (const struct constraint_expr lhs
,
616 const struct constraint_expr rhs
)
618 constraint_t ret
= (constraint_t
) pool_alloc (constraint_pool
);
624 /* Print out constraint C to FILE. */
627 dump_constraint (FILE *file
, constraint_t c
)
629 if (c
->lhs
.type
== ADDRESSOF
)
631 else if (c
->lhs
.type
== DEREF
)
633 fprintf (file
, "%s", get_varinfo (c
->lhs
.var
)->name
);
634 if (c
->lhs
.offset
== UNKNOWN_OFFSET
)
635 fprintf (file
, " + UNKNOWN");
636 else if (c
->lhs
.offset
!= 0)
637 fprintf (file
, " + " HOST_WIDE_INT_PRINT_DEC
, c
->lhs
.offset
);
638 fprintf (file
, " = ");
639 if (c
->rhs
.type
== ADDRESSOF
)
641 else if (c
->rhs
.type
== DEREF
)
643 fprintf (file
, "%s", get_varinfo (c
->rhs
.var
)->name
);
644 if (c
->rhs
.offset
== UNKNOWN_OFFSET
)
645 fprintf (file
, " + UNKNOWN");
646 else if (c
->rhs
.offset
!= 0)
647 fprintf (file
, " + " HOST_WIDE_INT_PRINT_DEC
, c
->rhs
.offset
);
651 void debug_constraint (constraint_t
);
652 void debug_constraints (void);
653 void debug_constraint_graph (void);
654 void debug_solution_for_var (unsigned int);
655 void debug_sa_points_to_info (void);
657 /* Print out constraint C to stderr. */
660 debug_constraint (constraint_t c
)
662 dump_constraint (stderr
, c
);
663 fprintf (stderr
, "\n");
666 /* Print out all constraints to FILE */
669 dump_constraints (FILE *file
, int from
)
673 for (i
= from
; VEC_iterate (constraint_t
, constraints
, i
, c
); i
++)
676 dump_constraint (file
, c
);
677 fprintf (file
, "\n");
681 /* Print out all constraints to stderr. */
684 debug_constraints (void)
686 dump_constraints (stderr
, 0);
689 /* Print the constraint graph in dot format. */
692 dump_constraint_graph (FILE *file
)
696 /* Only print the graph if it has already been initialized: */
700 /* Prints the header of the dot file: */
701 fprintf (file
, "strict digraph {\n");
702 fprintf (file
, " node [\n shape = box\n ]\n");
703 fprintf (file
, " edge [\n fontsize = \"12\"\n ]\n");
704 fprintf (file
, "\n // List of nodes and complex constraints in "
705 "the constraint graph:\n");
707 /* The next lines print the nodes in the graph together with the
708 complex constraints attached to them. */
709 for (i
= 0; i
< graph
->size
; i
++)
713 if (i
< FIRST_REF_NODE
)
714 fprintf (file
, "\"%s\"", get_varinfo (i
)->name
);
716 fprintf (file
, "\"*%s\"", get_varinfo (i
- FIRST_REF_NODE
)->name
);
717 if (graph
->complex[i
])
721 fprintf (file
, " [label=\"\\N\\n");
722 for (j
= 0; VEC_iterate (constraint_t
, graph
->complex[i
], j
, c
); ++j
)
724 dump_constraint (file
, c
);
725 fprintf (file
, "\\l");
727 fprintf (file
, "\"]");
729 fprintf (file
, ";\n");
732 /* Go over the edges. */
733 fprintf (file
, "\n // Edges in the constraint graph:\n");
734 for (i
= 0; i
< graph
->size
; i
++)
740 EXECUTE_IF_IN_NONNULL_BITMAP (graph
->succs
[i
], 0, j
, bi
)
742 unsigned to
= find (j
);
745 if (i
< FIRST_REF_NODE
)
746 fprintf (file
, "\"%s\"", get_varinfo (i
)->name
);
748 fprintf (file
, "\"*%s\"", get_varinfo (i
- FIRST_REF_NODE
)->name
);
749 fprintf (file
, " -> ");
750 if (to
< FIRST_REF_NODE
)
751 fprintf (file
, "\"%s\"", get_varinfo (to
)->name
);
753 fprintf (file
, "\"*%s\"", get_varinfo (to
- FIRST_REF_NODE
)->name
);
754 fprintf (file
, ";\n");
758 /* Prints the tail of the dot file. */
759 fprintf (file
, "}\n");
762 /* Print out the constraint graph to stderr. */
765 debug_constraint_graph (void)
767 dump_constraint_graph (stderr
);
772 The solver is a simple worklist solver, that works on the following
775 sbitmap changed_nodes = all zeroes;
777 For each node that is not already collapsed:
779 set bit in changed nodes
781 while (changed_count > 0)
783 compute topological ordering for constraint graph
785 find and collapse cycles in the constraint graph (updating
786 changed if necessary)
788 for each node (n) in the graph in topological order:
791 Process each complex constraint associated with the node,
792 updating changed if necessary.
794 For each outgoing edge from n, propagate the solution from n to
795 the destination of the edge, updating changed as necessary.
799 /* Return true if two constraint expressions A and B are equal. */
802 constraint_expr_equal (struct constraint_expr a
, struct constraint_expr b
)
804 return a
.type
== b
.type
&& a
.var
== b
.var
&& a
.offset
== b
.offset
;
807 /* Return true if constraint expression A is less than constraint expression
808 B. This is just arbitrary, but consistent, in order to give them an
812 constraint_expr_less (struct constraint_expr a
, struct constraint_expr b
)
814 if (a
.type
== b
.type
)
817 return a
.offset
< b
.offset
;
819 return a
.var
< b
.var
;
822 return a
.type
< b
.type
;
825 /* Return true if constraint A is less than constraint B. This is just
826 arbitrary, but consistent, in order to give them an ordering. */
829 constraint_less (const constraint_t a
, const constraint_t b
)
831 if (constraint_expr_less (a
->lhs
, b
->lhs
))
833 else if (constraint_expr_less (b
->lhs
, a
->lhs
))
836 return constraint_expr_less (a
->rhs
, b
->rhs
);
839 /* Return true if two constraints A and B are equal. */
842 constraint_equal (struct constraint a
, struct constraint b
)
844 return constraint_expr_equal (a
.lhs
, b
.lhs
)
845 && constraint_expr_equal (a
.rhs
, b
.rhs
);
849 /* Find a constraint LOOKFOR in the sorted constraint vector VEC */
852 constraint_vec_find (VEC(constraint_t
,heap
) *vec
,
853 struct constraint lookfor
)
861 place
= VEC_lower_bound (constraint_t
, vec
, &lookfor
, constraint_less
);
862 if (place
>= VEC_length (constraint_t
, vec
))
864 found
= VEC_index (constraint_t
, vec
, place
);
865 if (!constraint_equal (*found
, lookfor
))
870 /* Union two constraint vectors, TO and FROM. Put the result in TO. */
873 constraint_set_union (VEC(constraint_t
,heap
) **to
,
874 VEC(constraint_t
,heap
) **from
)
879 FOR_EACH_VEC_ELT (constraint_t
, *from
, i
, c
)
881 if (constraint_vec_find (*to
, *c
) == NULL
)
883 unsigned int place
= VEC_lower_bound (constraint_t
, *to
, c
,
885 VEC_safe_insert (constraint_t
, heap
, *to
, place
, c
);
890 /* Expands the solution in SET to all sub-fields of variables included.
891 Union the expanded result into RESULT. */
894 solution_set_expand (bitmap result
, bitmap set
)
900 /* In a first pass record all variables we need to add all
901 sub-fields off. This avoids quadratic behavior. */
902 EXECUTE_IF_SET_IN_BITMAP (set
, 0, j
, bi
)
904 varinfo_t v
= get_varinfo (j
);
905 if (v
->is_artificial_var
908 v
= lookup_vi_for_tree (v
->decl
);
910 vars
= BITMAP_ALLOC (NULL
);
911 bitmap_set_bit (vars
, v
->id
);
914 /* In the second pass now do the addition to the solution and
915 to speed up solving add it to the delta as well. */
918 EXECUTE_IF_SET_IN_BITMAP (vars
, 0, j
, bi
)
920 varinfo_t v
= get_varinfo (j
);
921 for (; v
!= NULL
; v
= v
->next
)
922 bitmap_set_bit (result
, v
->id
);
928 /* Take a solution set SET, add OFFSET to each member of the set, and
929 overwrite SET with the result when done. */
932 solution_set_add (bitmap set
, HOST_WIDE_INT offset
)
934 bitmap result
= BITMAP_ALLOC (&iteration_obstack
);
938 /* If the offset is unknown we have to expand the solution to
940 if (offset
== UNKNOWN_OFFSET
)
942 solution_set_expand (set
, set
);
946 EXECUTE_IF_SET_IN_BITMAP (set
, 0, i
, bi
)
948 varinfo_t vi
= get_varinfo (i
);
950 /* If this is a variable with just one field just set its bit
952 if (vi
->is_artificial_var
953 || vi
->is_unknown_size_var
955 bitmap_set_bit (result
, i
);
958 unsigned HOST_WIDE_INT fieldoffset
= vi
->offset
+ offset
;
960 /* If the offset makes the pointer point to before the
961 variable use offset zero for the field lookup. */
963 && fieldoffset
> vi
->offset
)
967 vi
= first_or_preceding_vi_for_offset (vi
, fieldoffset
);
969 bitmap_set_bit (result
, vi
->id
);
970 /* If the result is not exactly at fieldoffset include the next
971 field as well. See get_constraint_for_ptr_offset for more
973 if (vi
->offset
!= fieldoffset
975 bitmap_set_bit (result
, vi
->next
->id
);
979 bitmap_copy (set
, result
);
980 BITMAP_FREE (result
);
983 /* Union solution sets TO and FROM, and add INC to each member of FROM in the
987 set_union_with_increment (bitmap to
, bitmap from
, HOST_WIDE_INT inc
)
990 return bitmap_ior_into (to
, from
);
996 tmp
= BITMAP_ALLOC (&iteration_obstack
);
997 bitmap_copy (tmp
, from
);
998 solution_set_add (tmp
, inc
);
999 res
= bitmap_ior_into (to
, tmp
);
1005 /* Insert constraint C into the list of complex constraints for graph
1009 insert_into_complex (constraint_graph_t graph
,
1010 unsigned int var
, constraint_t c
)
1012 VEC (constraint_t
, heap
) *complex = graph
->complex[var
];
1013 unsigned int place
= VEC_lower_bound (constraint_t
, complex, c
,
1016 /* Only insert constraints that do not already exist. */
1017 if (place
>= VEC_length (constraint_t
, complex)
1018 || !constraint_equal (*c
, *VEC_index (constraint_t
, complex, place
)))
1019 VEC_safe_insert (constraint_t
, heap
, graph
->complex[var
], place
, c
);
1023 /* Condense two variable nodes into a single variable node, by moving
1024 all associated info from SRC to TO. */
1027 merge_node_constraints (constraint_graph_t graph
, unsigned int to
,
1033 gcc_assert (find (from
) == to
);
1035 /* Move all complex constraints from src node into to node */
1036 FOR_EACH_VEC_ELT (constraint_t
, graph
->complex[from
], i
, c
)
1038 /* In complex constraints for node src, we may have either
1039 a = *src, and *src = a, or an offseted constraint which are
1040 always added to the rhs node's constraints. */
1042 if (c
->rhs
.type
== DEREF
)
1044 else if (c
->lhs
.type
== DEREF
)
1049 constraint_set_union (&graph
->complex[to
], &graph
->complex[from
]);
1050 VEC_free (constraint_t
, heap
, graph
->complex[from
]);
1051 graph
->complex[from
] = NULL
;
1055 /* Remove edges involving NODE from GRAPH. */
1058 clear_edges_for_node (constraint_graph_t graph
, unsigned int node
)
1060 if (graph
->succs
[node
])
1061 BITMAP_FREE (graph
->succs
[node
]);
1064 /* Merge GRAPH nodes FROM and TO into node TO. */
1067 merge_graph_nodes (constraint_graph_t graph
, unsigned int to
,
1070 if (graph
->indirect_cycles
[from
] != -1)
1072 /* If we have indirect cycles with the from node, and we have
1073 none on the to node, the to node has indirect cycles from the
1074 from node now that they are unified.
1075 If indirect cycles exist on both, unify the nodes that they
1076 are in a cycle with, since we know they are in a cycle with
1078 if (graph
->indirect_cycles
[to
] == -1)
1079 graph
->indirect_cycles
[to
] = graph
->indirect_cycles
[from
];
1082 /* Merge all the successor edges. */
1083 if (graph
->succs
[from
])
1085 if (!graph
->succs
[to
])
1086 graph
->succs
[to
] = BITMAP_ALLOC (&pta_obstack
);
1087 bitmap_ior_into (graph
->succs
[to
],
1088 graph
->succs
[from
]);
1091 clear_edges_for_node (graph
, from
);
1095 /* Add an indirect graph edge to GRAPH, going from TO to FROM if
1096 it doesn't exist in the graph already. */
1099 add_implicit_graph_edge (constraint_graph_t graph
, unsigned int to
,
1105 if (!graph
->implicit_preds
[to
])
1106 graph
->implicit_preds
[to
] = BITMAP_ALLOC (&predbitmap_obstack
);
1108 if (bitmap_set_bit (graph
->implicit_preds
[to
], from
))
1109 stats
.num_implicit_edges
++;
1112 /* Add a predecessor graph edge to GRAPH, going from TO to FROM if
1113 it doesn't exist in the graph already.
1114 Return false if the edge already existed, true otherwise. */
1117 add_pred_graph_edge (constraint_graph_t graph
, unsigned int to
,
1120 if (!graph
->preds
[to
])
1121 graph
->preds
[to
] = BITMAP_ALLOC (&predbitmap_obstack
);
1122 bitmap_set_bit (graph
->preds
[to
], from
);
1125 /* Add a graph edge to GRAPH, going from FROM to TO if
1126 it doesn't exist in the graph already.
1127 Return false if the edge already existed, true otherwise. */
1130 add_graph_edge (constraint_graph_t graph
, unsigned int to
,
1141 if (!graph
->succs
[from
])
1142 graph
->succs
[from
] = BITMAP_ALLOC (&pta_obstack
);
1143 if (bitmap_set_bit (graph
->succs
[from
], to
))
1146 if (to
< FIRST_REF_NODE
&& from
< FIRST_REF_NODE
)
1154 /* Return true if {DEST.SRC} is an existing graph edge in GRAPH. */
1157 valid_graph_edge (constraint_graph_t graph
, unsigned int src
,
1160 return (graph
->succs
[dest
]
1161 && bitmap_bit_p (graph
->succs
[dest
], src
));
1164 /* Initialize the constraint graph structure to contain SIZE nodes. */
1167 init_graph (unsigned int size
)
1171 graph
= XCNEW (struct constraint_graph
);
1173 graph
->succs
= XCNEWVEC (bitmap
, graph
->size
);
1174 graph
->indirect_cycles
= XNEWVEC (int, graph
->size
);
1175 graph
->rep
= XNEWVEC (unsigned int, graph
->size
);
1176 graph
->complex = XCNEWVEC (VEC(constraint_t
, heap
) *, size
);
1177 graph
->pe
= XCNEWVEC (unsigned int, graph
->size
);
1178 graph
->pe_rep
= XNEWVEC (int, graph
->size
);
1180 for (j
= 0; j
< graph
->size
; j
++)
1183 graph
->pe_rep
[j
] = -1;
1184 graph
->indirect_cycles
[j
] = -1;
1188 /* Build the constraint graph, adding only predecessor edges right now. */
1191 build_pred_graph (void)
1197 graph
->implicit_preds
= XCNEWVEC (bitmap
, graph
->size
);
1198 graph
->preds
= XCNEWVEC (bitmap
, graph
->size
);
1199 graph
->pointer_label
= XCNEWVEC (unsigned int, graph
->size
);
1200 graph
->loc_label
= XCNEWVEC (unsigned int, graph
->size
);
1201 graph
->pointed_by
= XCNEWVEC (bitmap
, graph
->size
);
1202 graph
->points_to
= XCNEWVEC (bitmap
, graph
->size
);
1203 graph
->eq_rep
= XNEWVEC (int, graph
->size
);
1204 graph
->direct_nodes
= sbitmap_alloc (graph
->size
);
1205 graph
->address_taken
= BITMAP_ALLOC (&predbitmap_obstack
);
1206 sbitmap_zero (graph
->direct_nodes
);
1208 for (j
= 0; j
< FIRST_REF_NODE
; j
++)
1210 if (!get_varinfo (j
)->is_special_var
)
1211 SET_BIT (graph
->direct_nodes
, j
);
1214 for (j
= 0; j
< graph
->size
; j
++)
1215 graph
->eq_rep
[j
] = -1;
1217 for (j
= 0; j
< VEC_length (varinfo_t
, varmap
); j
++)
1218 graph
->indirect_cycles
[j
] = -1;
1220 FOR_EACH_VEC_ELT (constraint_t
, constraints
, i
, c
)
1222 struct constraint_expr lhs
= c
->lhs
;
1223 struct constraint_expr rhs
= c
->rhs
;
1224 unsigned int lhsvar
= lhs
.var
;
1225 unsigned int rhsvar
= rhs
.var
;
1227 if (lhs
.type
== DEREF
)
1230 if (rhs
.offset
== 0 && lhs
.offset
== 0 && rhs
.type
== SCALAR
)
1231 add_pred_graph_edge (graph
, FIRST_REF_NODE
+ lhsvar
, rhsvar
);
1233 else if (rhs
.type
== DEREF
)
1236 if (rhs
.offset
== 0 && lhs
.offset
== 0 && lhs
.type
== SCALAR
)
1237 add_pred_graph_edge (graph
, lhsvar
, FIRST_REF_NODE
+ rhsvar
);
1239 RESET_BIT (graph
->direct_nodes
, lhsvar
);
1241 else if (rhs
.type
== ADDRESSOF
)
1246 if (graph
->points_to
[lhsvar
] == NULL
)
1247 graph
->points_to
[lhsvar
] = BITMAP_ALLOC (&predbitmap_obstack
);
1248 bitmap_set_bit (graph
->points_to
[lhsvar
], rhsvar
);
1250 if (graph
->pointed_by
[rhsvar
] == NULL
)
1251 graph
->pointed_by
[rhsvar
] = BITMAP_ALLOC (&predbitmap_obstack
);
1252 bitmap_set_bit (graph
->pointed_by
[rhsvar
], lhsvar
);
1254 /* Implicitly, *x = y */
1255 add_implicit_graph_edge (graph
, FIRST_REF_NODE
+ lhsvar
, rhsvar
);
1257 /* All related variables are no longer direct nodes. */
1258 RESET_BIT (graph
->direct_nodes
, rhsvar
);
1259 v
= get_varinfo (rhsvar
);
1260 if (!v
->is_full_var
)
1262 v
= lookup_vi_for_tree (v
->decl
);
1265 RESET_BIT (graph
->direct_nodes
, v
->id
);
1270 bitmap_set_bit (graph
->address_taken
, rhsvar
);
1272 else if (lhsvar
> anything_id
1273 && lhsvar
!= rhsvar
&& lhs
.offset
== 0 && rhs
.offset
== 0)
1276 add_pred_graph_edge (graph
, lhsvar
, rhsvar
);
1277 /* Implicitly, *x = *y */
1278 add_implicit_graph_edge (graph
, FIRST_REF_NODE
+ lhsvar
,
1279 FIRST_REF_NODE
+ rhsvar
);
1281 else if (lhs
.offset
!= 0 || rhs
.offset
!= 0)
1283 if (rhs
.offset
!= 0)
1284 RESET_BIT (graph
->direct_nodes
, lhs
.var
);
1285 else if (lhs
.offset
!= 0)
1286 RESET_BIT (graph
->direct_nodes
, rhs
.var
);
1291 /* Build the constraint graph, adding successor edges. */
1294 build_succ_graph (void)
1299 FOR_EACH_VEC_ELT (constraint_t
, constraints
, i
, c
)
1301 struct constraint_expr lhs
;
1302 struct constraint_expr rhs
;
1303 unsigned int lhsvar
;
1304 unsigned int rhsvar
;
1311 lhsvar
= find (lhs
.var
);
1312 rhsvar
= find (rhs
.var
);
1314 if (lhs
.type
== DEREF
)
1316 if (rhs
.offset
== 0 && lhs
.offset
== 0 && rhs
.type
== SCALAR
)
1317 add_graph_edge (graph
, FIRST_REF_NODE
+ lhsvar
, rhsvar
);
1319 else if (rhs
.type
== DEREF
)
1321 if (rhs
.offset
== 0 && lhs
.offset
== 0 && lhs
.type
== SCALAR
)
1322 add_graph_edge (graph
, lhsvar
, FIRST_REF_NODE
+ rhsvar
);
1324 else if (rhs
.type
== ADDRESSOF
)
1327 gcc_assert (find (rhs
.var
) == rhs
.var
);
1328 bitmap_set_bit (get_varinfo (lhsvar
)->solution
, rhsvar
);
1330 else if (lhsvar
> anything_id
1331 && lhsvar
!= rhsvar
&& lhs
.offset
== 0 && rhs
.offset
== 0)
1333 add_graph_edge (graph
, lhsvar
, rhsvar
);
1337 /* Add edges from STOREDANYTHING to all non-direct nodes that can
1338 receive pointers. */
1339 t
= find (storedanything_id
);
1340 for (i
= integer_id
+ 1; i
< FIRST_REF_NODE
; ++i
)
1342 if (!TEST_BIT (graph
->direct_nodes
, i
)
1343 && get_varinfo (i
)->may_have_pointers
)
1344 add_graph_edge (graph
, find (i
), t
);
1347 /* Everything stored to ANYTHING also potentially escapes. */
1348 add_graph_edge (graph
, find (escaped_id
), t
);
1352 /* Changed variables on the last iteration. */
1353 static bitmap changed
;
1355 /* Strongly Connected Component visitation info. */
1362 unsigned int *node_mapping
;
1364 VEC(unsigned,heap
) *scc_stack
;
1368 /* Recursive routine to find strongly connected components in GRAPH.
1369 SI is the SCC info to store the information in, and N is the id of current
1370 graph node we are processing.
1372 This is Tarjan's strongly connected component finding algorithm, as
1373 modified by Nuutila to keep only non-root nodes on the stack.
1374 The algorithm can be found in "On finding the strongly connected
1375 connected components in a directed graph" by Esko Nuutila and Eljas
1376 Soisalon-Soininen, in Information Processing Letters volume 49,
1377 number 1, pages 9-14. */
1380 scc_visit (constraint_graph_t graph
, struct scc_info
*si
, unsigned int n
)
1384 unsigned int my_dfs
;
1386 SET_BIT (si
->visited
, n
);
1387 si
->dfs
[n
] = si
->current_index
++;
1388 my_dfs
= si
->dfs
[n
];
1390 /* Visit all the successors. */
1391 EXECUTE_IF_IN_NONNULL_BITMAP (graph
->succs
[n
], 0, i
, bi
)
1395 if (i
> LAST_REF_NODE
)
1399 if (TEST_BIT (si
->deleted
, w
))
1402 if (!TEST_BIT (si
->visited
, w
))
1403 scc_visit (graph
, si
, w
);
1405 unsigned int t
= find (w
);
1406 unsigned int nnode
= find (n
);
1407 gcc_assert (nnode
== n
);
1409 if (si
->dfs
[t
] < si
->dfs
[nnode
])
1410 si
->dfs
[n
] = si
->dfs
[t
];
1414 /* See if any components have been identified. */
1415 if (si
->dfs
[n
] == my_dfs
)
1417 if (VEC_length (unsigned, si
->scc_stack
) > 0
1418 && si
->dfs
[VEC_last (unsigned, si
->scc_stack
)] >= my_dfs
)
1420 bitmap scc
= BITMAP_ALLOC (NULL
);
1421 unsigned int lowest_node
;
1424 bitmap_set_bit (scc
, n
);
1426 while (VEC_length (unsigned, si
->scc_stack
) != 0
1427 && si
->dfs
[VEC_last (unsigned, si
->scc_stack
)] >= my_dfs
)
1429 unsigned int w
= VEC_pop (unsigned, si
->scc_stack
);
1431 bitmap_set_bit (scc
, w
);
1434 lowest_node
= bitmap_first_set_bit (scc
);
1435 gcc_assert (lowest_node
< FIRST_REF_NODE
);
1437 /* Collapse the SCC nodes into a single node, and mark the
1439 EXECUTE_IF_SET_IN_BITMAP (scc
, 0, i
, bi
)
1441 if (i
< FIRST_REF_NODE
)
1443 if (unite (lowest_node
, i
))
1444 unify_nodes (graph
, lowest_node
, i
, false);
1448 unite (lowest_node
, i
);
1449 graph
->indirect_cycles
[i
- FIRST_REF_NODE
] = lowest_node
;
1453 SET_BIT (si
->deleted
, n
);
1456 VEC_safe_push (unsigned, heap
, si
->scc_stack
, n
);
1459 /* Unify node FROM into node TO, updating the changed count if
1460 necessary when UPDATE_CHANGED is true. */
1463 unify_nodes (constraint_graph_t graph
, unsigned int to
, unsigned int from
,
1464 bool update_changed
)
1467 gcc_assert (to
!= from
&& find (to
) == to
);
1468 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1469 fprintf (dump_file
, "Unifying %s to %s\n",
1470 get_varinfo (from
)->name
,
1471 get_varinfo (to
)->name
);
1474 stats
.unified_vars_dynamic
++;
1476 stats
.unified_vars_static
++;
1478 merge_graph_nodes (graph
, to
, from
);
1479 merge_node_constraints (graph
, to
, from
);
1481 /* Mark TO as changed if FROM was changed. If TO was already marked
1482 as changed, decrease the changed count. */
1485 && bitmap_bit_p (changed
, from
))
1487 bitmap_clear_bit (changed
, from
);
1488 bitmap_set_bit (changed
, to
);
1490 if (get_varinfo (from
)->solution
)
1492 /* If the solution changes because of the merging, we need to mark
1493 the variable as changed. */
1494 if (bitmap_ior_into (get_varinfo (to
)->solution
,
1495 get_varinfo (from
)->solution
))
1498 bitmap_set_bit (changed
, to
);
1501 BITMAP_FREE (get_varinfo (from
)->solution
);
1502 if (get_varinfo (from
)->oldsolution
)
1503 BITMAP_FREE (get_varinfo (from
)->oldsolution
);
1505 if (stats
.iterations
> 0
1506 && get_varinfo (to
)->oldsolution
)
1507 BITMAP_FREE (get_varinfo (to
)->oldsolution
);
1509 if (valid_graph_edge (graph
, to
, to
))
1511 if (graph
->succs
[to
])
1512 bitmap_clear_bit (graph
->succs
[to
], to
);
1516 /* Information needed to compute the topological ordering of a graph. */
1520 /* sbitmap of visited nodes. */
1522 /* Array that stores the topological order of the graph, *in
1524 VEC(unsigned,heap
) *topo_order
;
1528 /* Initialize and return a topological info structure. */
1530 static struct topo_info
*
1531 init_topo_info (void)
1533 size_t size
= graph
->size
;
1534 struct topo_info
*ti
= XNEW (struct topo_info
);
1535 ti
->visited
= sbitmap_alloc (size
);
1536 sbitmap_zero (ti
->visited
);
1537 ti
->topo_order
= VEC_alloc (unsigned, heap
, 1);
1542 /* Free the topological sort info pointed to by TI. */
1545 free_topo_info (struct topo_info
*ti
)
1547 sbitmap_free (ti
->visited
);
1548 VEC_free (unsigned, heap
, ti
->topo_order
);
1552 /* Visit the graph in topological order, and store the order in the
1553 topo_info structure. */
1556 topo_visit (constraint_graph_t graph
, struct topo_info
*ti
,
1562 SET_BIT (ti
->visited
, n
);
1564 if (graph
->succs
[n
])
1565 EXECUTE_IF_SET_IN_BITMAP (graph
->succs
[n
], 0, j
, bi
)
1567 if (!TEST_BIT (ti
->visited
, j
))
1568 topo_visit (graph
, ti
, j
);
1571 VEC_safe_push (unsigned, heap
, ti
->topo_order
, n
);
1574 /* Process a constraint C that represents x = *(y + off), using DELTA as the
1575 starting solution for y. */
1578 do_sd_constraint (constraint_graph_t graph
, constraint_t c
,
1581 unsigned int lhs
= c
->lhs
.var
;
1583 bitmap sol
= get_varinfo (lhs
)->solution
;
1586 HOST_WIDE_INT roffset
= c
->rhs
.offset
;
1588 /* Our IL does not allow this. */
1589 gcc_assert (c
->lhs
.offset
== 0);
1591 /* If the solution of Y contains anything it is good enough to transfer
1593 if (bitmap_bit_p (delta
, anything_id
))
1595 flag
|= bitmap_set_bit (sol
, anything_id
);
1599 /* If we do not know at with offset the rhs is dereferenced compute
1600 the reachability set of DELTA, conservatively assuming it is
1601 dereferenced at all valid offsets. */
1602 if (roffset
== UNKNOWN_OFFSET
)
1604 solution_set_expand (delta
, delta
);
1605 /* No further offset processing is necessary. */
1609 /* For each variable j in delta (Sol(y)), add
1610 an edge in the graph from j to x, and union Sol(j) into Sol(x). */
1611 EXECUTE_IF_SET_IN_BITMAP (delta
, 0, j
, bi
)
1613 varinfo_t v
= get_varinfo (j
);
1614 HOST_WIDE_INT fieldoffset
= v
->offset
+ roffset
;
1618 fieldoffset
= v
->offset
;
1619 else if (roffset
!= 0)
1620 v
= first_vi_for_offset (v
, fieldoffset
);
1621 /* If the access is outside of the variable we can ignore it. */
1629 /* Adding edges from the special vars is pointless.
1630 They don't have sets that can change. */
1631 if (get_varinfo (t
)->is_special_var
)
1632 flag
|= bitmap_ior_into (sol
, get_varinfo (t
)->solution
);
1633 /* Merging the solution from ESCAPED needlessly increases
1634 the set. Use ESCAPED as representative instead. */
1635 else if (v
->id
== escaped_id
)
1636 flag
|= bitmap_set_bit (sol
, escaped_id
);
1637 else if (v
->may_have_pointers
1638 && add_graph_edge (graph
, lhs
, t
))
1639 flag
|= bitmap_ior_into (sol
, get_varinfo (t
)->solution
);
1641 /* If the variable is not exactly at the requested offset
1642 we have to include the next one. */
1643 if (v
->offset
== (unsigned HOST_WIDE_INT
)fieldoffset
1648 fieldoffset
= v
->offset
;
1654 /* If the LHS solution changed, mark the var as changed. */
1657 get_varinfo (lhs
)->solution
= sol
;
1658 bitmap_set_bit (changed
, lhs
);
1662 /* Process a constraint C that represents *(x + off) = y using DELTA
1663 as the starting solution for x. */
1666 do_ds_constraint (constraint_t c
, bitmap delta
)
1668 unsigned int rhs
= c
->rhs
.var
;
1669 bitmap sol
= get_varinfo (rhs
)->solution
;
1672 HOST_WIDE_INT loff
= c
->lhs
.offset
;
1673 bool escaped_p
= false;
1675 /* Our IL does not allow this. */
1676 gcc_assert (c
->rhs
.offset
== 0);
1678 /* If the solution of y contains ANYTHING simply use the ANYTHING
1679 solution. This avoids needlessly increasing the points-to sets. */
1680 if (bitmap_bit_p (sol
, anything_id
))
1681 sol
= get_varinfo (find (anything_id
))->solution
;
1683 /* If the solution for x contains ANYTHING we have to merge the
1684 solution of y into all pointer variables which we do via
1686 if (bitmap_bit_p (delta
, anything_id
))
1688 unsigned t
= find (storedanything_id
);
1689 if (add_graph_edge (graph
, t
, rhs
))
1691 if (bitmap_ior_into (get_varinfo (t
)->solution
, sol
))
1692 bitmap_set_bit (changed
, t
);
1697 /* If we do not know at with offset the rhs is dereferenced compute
1698 the reachability set of DELTA, conservatively assuming it is
1699 dereferenced at all valid offsets. */
1700 if (loff
== UNKNOWN_OFFSET
)
1702 solution_set_expand (delta
, delta
);
1706 /* For each member j of delta (Sol(x)), add an edge from y to j and
1707 union Sol(y) into Sol(j) */
1708 EXECUTE_IF_SET_IN_BITMAP (delta
, 0, j
, bi
)
1710 varinfo_t v
= get_varinfo (j
);
1712 HOST_WIDE_INT fieldoffset
= v
->offset
+ loff
;
1715 fieldoffset
= v
->offset
;
1717 v
= first_vi_for_offset (v
, fieldoffset
);
1718 /* If the access is outside of the variable we can ignore it. */
1724 if (v
->may_have_pointers
)
1726 /* If v is a global variable then this is an escape point. */
1727 if (v
->is_global_var
1730 t
= find (escaped_id
);
1731 if (add_graph_edge (graph
, t
, rhs
)
1732 && bitmap_ior_into (get_varinfo (t
)->solution
, sol
))
1733 bitmap_set_bit (changed
, t
);
1734 /* Enough to let rhs escape once. */
1738 if (v
->is_special_var
)
1742 if (add_graph_edge (graph
, t
, rhs
)
1743 && bitmap_ior_into (get_varinfo (t
)->solution
, sol
))
1744 bitmap_set_bit (changed
, t
);
1747 /* If the variable is not exactly at the requested offset
1748 we have to include the next one. */
1749 if (v
->offset
== (unsigned HOST_WIDE_INT
)fieldoffset
1754 fieldoffset
= v
->offset
;
1760 /* Handle a non-simple (simple meaning requires no iteration),
1761 constraint (IE *x = &y, x = *y, *x = y, and x = y with offsets involved). */
1764 do_complex_constraint (constraint_graph_t graph
, constraint_t c
, bitmap delta
)
1766 if (c
->lhs
.type
== DEREF
)
1768 if (c
->rhs
.type
== ADDRESSOF
)
1775 do_ds_constraint (c
, delta
);
1778 else if (c
->rhs
.type
== DEREF
)
1781 if (!(get_varinfo (c
->lhs
.var
)->is_special_var
))
1782 do_sd_constraint (graph
, c
, delta
);
1790 gcc_assert (c
->rhs
.type
== SCALAR
&& c
->lhs
.type
== SCALAR
);
1791 solution
= get_varinfo (c
->rhs
.var
)->solution
;
1792 tmp
= get_varinfo (c
->lhs
.var
)->solution
;
1794 flag
= set_union_with_increment (tmp
, solution
, c
->rhs
.offset
);
1798 get_varinfo (c
->lhs
.var
)->solution
= tmp
;
1799 bitmap_set_bit (changed
, c
->lhs
.var
);
1804 /* Initialize and return a new SCC info structure. */
1806 static struct scc_info
*
1807 init_scc_info (size_t size
)
1809 struct scc_info
*si
= XNEW (struct scc_info
);
1812 si
->current_index
= 0;
1813 si
->visited
= sbitmap_alloc (size
);
1814 sbitmap_zero (si
->visited
);
1815 si
->deleted
= sbitmap_alloc (size
);
1816 sbitmap_zero (si
->deleted
);
1817 si
->node_mapping
= XNEWVEC (unsigned int, size
);
1818 si
->dfs
= XCNEWVEC (unsigned int, size
);
1820 for (i
= 0; i
< size
; i
++)
1821 si
->node_mapping
[i
] = i
;
1823 si
->scc_stack
= VEC_alloc (unsigned, heap
, 1);
1827 /* Free an SCC info structure pointed to by SI */
1830 free_scc_info (struct scc_info
*si
)
1832 sbitmap_free (si
->visited
);
1833 sbitmap_free (si
->deleted
);
1834 free (si
->node_mapping
);
1836 VEC_free (unsigned, heap
, si
->scc_stack
);
1841 /* Find indirect cycles in GRAPH that occur, using strongly connected
1842 components, and note them in the indirect cycles map.
1844 This technique comes from Ben Hardekopf and Calvin Lin,
1845 "It Pays to be Lazy: Fast and Accurate Pointer Analysis for Millions of
1846 Lines of Code", submitted to PLDI 2007. */
1849 find_indirect_cycles (constraint_graph_t graph
)
1852 unsigned int size
= graph
->size
;
1853 struct scc_info
*si
= init_scc_info (size
);
1855 for (i
= 0; i
< MIN (LAST_REF_NODE
, size
); i
++ )
1856 if (!TEST_BIT (si
->visited
, i
) && find (i
) == i
)
1857 scc_visit (graph
, si
, i
);
1862 /* Compute a topological ordering for GRAPH, and store the result in the
1863 topo_info structure TI. */
1866 compute_topo_order (constraint_graph_t graph
,
1867 struct topo_info
*ti
)
1870 unsigned int size
= graph
->size
;
1872 for (i
= 0; i
!= size
; ++i
)
1873 if (!TEST_BIT (ti
->visited
, i
) && find (i
) == i
)
1874 topo_visit (graph
, ti
, i
);
1877 /* Structure used to for hash value numbering of pointer equivalence
1880 typedef struct equiv_class_label
1883 unsigned int equivalence_class
;
1885 } *equiv_class_label_t
;
1886 typedef const struct equiv_class_label
*const_equiv_class_label_t
;
1888 /* A hashtable for mapping a bitmap of labels->pointer equivalence
1890 static htab_t pointer_equiv_class_table
;
1892 /* A hashtable for mapping a bitmap of labels->location equivalence
1894 static htab_t location_equiv_class_table
;
1896 /* Hash function for a equiv_class_label_t */
1899 equiv_class_label_hash (const void *p
)
1901 const_equiv_class_label_t
const ecl
= (const_equiv_class_label_t
) p
;
1902 return ecl
->hashcode
;
1905 /* Equality function for two equiv_class_label_t's. */
1908 equiv_class_label_eq (const void *p1
, const void *p2
)
1910 const_equiv_class_label_t
const eql1
= (const_equiv_class_label_t
) p1
;
1911 const_equiv_class_label_t
const eql2
= (const_equiv_class_label_t
) p2
;
1912 return (eql1
->hashcode
== eql2
->hashcode
1913 && bitmap_equal_p (eql1
->labels
, eql2
->labels
));
1916 /* Lookup a equivalence class in TABLE by the bitmap of LABELS it
1920 equiv_class_lookup (htab_t table
, bitmap labels
)
1923 struct equiv_class_label ecl
;
1925 ecl
.labels
= labels
;
1926 ecl
.hashcode
= bitmap_hash (labels
);
1928 slot
= htab_find_slot_with_hash (table
, &ecl
,
1929 ecl
.hashcode
, NO_INSERT
);
1933 return ((equiv_class_label_t
) *slot
)->equivalence_class
;
1937 /* Add an equivalence class named EQUIVALENCE_CLASS with labels LABELS
1941 equiv_class_add (htab_t table
, unsigned int equivalence_class
,
1945 equiv_class_label_t ecl
= XNEW (struct equiv_class_label
);
1947 ecl
->labels
= labels
;
1948 ecl
->equivalence_class
= equivalence_class
;
1949 ecl
->hashcode
= bitmap_hash (labels
);
1951 slot
= htab_find_slot_with_hash (table
, ecl
,
1952 ecl
->hashcode
, INSERT
);
1953 gcc_assert (!*slot
);
1954 *slot
= (void *) ecl
;
1957 /* Perform offline variable substitution.
1959 This is a worst case quadratic time way of identifying variables
1960 that must have equivalent points-to sets, including those caused by
1961 static cycles, and single entry subgraphs, in the constraint graph.
1963 The technique is described in "Exploiting Pointer and Location
1964 Equivalence to Optimize Pointer Analysis. In the 14th International
1965 Static Analysis Symposium (SAS), August 2007." It is known as the
1966 "HU" algorithm, and is equivalent to value numbering the collapsed
1967 constraint graph including evaluating unions.
1969 The general method of finding equivalence classes is as follows:
1970 Add fake nodes (REF nodes) and edges for *a = b and a = *b constraints.
1971 Initialize all non-REF nodes to be direct nodes.
1972 For each constraint a = a U {b}, we set pts(a) = pts(a) u {fresh
1974 For each constraint containing the dereference, we also do the same
1977 We then compute SCC's in the graph and unify nodes in the same SCC,
1980 For each non-collapsed node x:
1981 Visit all unvisited explicit incoming edges.
1982 Ignoring all non-pointers, set pts(x) = Union of pts(a) for y
1984 Lookup the equivalence class for pts(x).
1985 If we found one, equivalence_class(x) = found class.
1986 Otherwise, equivalence_class(x) = new class, and new_class is
1987 added to the lookup table.
1989 All direct nodes with the same equivalence class can be replaced
1990 with a single representative node.
1991 All unlabeled nodes (label == 0) are not pointers and all edges
1992 involving them can be eliminated.
1993 We perform these optimizations during rewrite_constraints
1995 In addition to pointer equivalence class finding, we also perform
1996 location equivalence class finding. This is the set of variables
1997 that always appear together in points-to sets. We use this to
1998 compress the size of the points-to sets. */
2000 /* Current maximum pointer equivalence class id. */
2001 static int pointer_equiv_class
;
2003 /* Current maximum location equivalence class id. */
2004 static int location_equiv_class
;
2006 /* Recursive routine to find strongly connected components in GRAPH,
2007 and label it's nodes with DFS numbers. */
2010 condense_visit (constraint_graph_t graph
, struct scc_info
*si
, unsigned int n
)
2014 unsigned int my_dfs
;
2016 gcc_assert (si
->node_mapping
[n
] == n
);
2017 SET_BIT (si
->visited
, n
);
2018 si
->dfs
[n
] = si
->current_index
++;
2019 my_dfs
= si
->dfs
[n
];
2021 /* Visit all the successors. */
2022 EXECUTE_IF_IN_NONNULL_BITMAP (graph
->preds
[n
], 0, i
, bi
)
2024 unsigned int w
= si
->node_mapping
[i
];
2026 if (TEST_BIT (si
->deleted
, w
))
2029 if (!TEST_BIT (si
->visited
, w
))
2030 condense_visit (graph
, si
, w
);
2032 unsigned int t
= si
->node_mapping
[w
];
2033 unsigned int nnode
= si
->node_mapping
[n
];
2034 gcc_assert (nnode
== n
);
2036 if (si
->dfs
[t
] < si
->dfs
[nnode
])
2037 si
->dfs
[n
] = si
->dfs
[t
];
2041 /* Visit all the implicit predecessors. */
2042 EXECUTE_IF_IN_NONNULL_BITMAP (graph
->implicit_preds
[n
], 0, i
, bi
)
2044 unsigned int w
= si
->node_mapping
[i
];
2046 if (TEST_BIT (si
->deleted
, w
))
2049 if (!TEST_BIT (si
->visited
, w
))
2050 condense_visit (graph
, si
, w
);
2052 unsigned int t
= si
->node_mapping
[w
];
2053 unsigned int nnode
= si
->node_mapping
[n
];
2054 gcc_assert (nnode
== n
);
2056 if (si
->dfs
[t
] < si
->dfs
[nnode
])
2057 si
->dfs
[n
] = si
->dfs
[t
];
2061 /* See if any components have been identified. */
2062 if (si
->dfs
[n
] == my_dfs
)
2064 while (VEC_length (unsigned, si
->scc_stack
) != 0
2065 && si
->dfs
[VEC_last (unsigned, si
->scc_stack
)] >= my_dfs
)
2067 unsigned int w
= VEC_pop (unsigned, si
->scc_stack
);
2068 si
->node_mapping
[w
] = n
;
2070 if (!TEST_BIT (graph
->direct_nodes
, w
))
2071 RESET_BIT (graph
->direct_nodes
, n
);
2073 /* Unify our nodes. */
2074 if (graph
->preds
[w
])
2076 if (!graph
->preds
[n
])
2077 graph
->preds
[n
] = BITMAP_ALLOC (&predbitmap_obstack
);
2078 bitmap_ior_into (graph
->preds
[n
], graph
->preds
[w
]);
2080 if (graph
->implicit_preds
[w
])
2082 if (!graph
->implicit_preds
[n
])
2083 graph
->implicit_preds
[n
] = BITMAP_ALLOC (&predbitmap_obstack
);
2084 bitmap_ior_into (graph
->implicit_preds
[n
],
2085 graph
->implicit_preds
[w
]);
2087 if (graph
->points_to
[w
])
2089 if (!graph
->points_to
[n
])
2090 graph
->points_to
[n
] = BITMAP_ALLOC (&predbitmap_obstack
);
2091 bitmap_ior_into (graph
->points_to
[n
],
2092 graph
->points_to
[w
]);
2095 SET_BIT (si
->deleted
, n
);
2098 VEC_safe_push (unsigned, heap
, si
->scc_stack
, n
);
2101 /* Label pointer equivalences. */
2104 label_visit (constraint_graph_t graph
, struct scc_info
*si
, unsigned int n
)
2108 SET_BIT (si
->visited
, n
);
2110 if (!graph
->points_to
[n
])
2111 graph
->points_to
[n
] = BITMAP_ALLOC (&predbitmap_obstack
);
2113 /* Label and union our incoming edges's points to sets. */
2114 EXECUTE_IF_IN_NONNULL_BITMAP (graph
->preds
[n
], 0, i
, bi
)
2116 unsigned int w
= si
->node_mapping
[i
];
2117 if (!TEST_BIT (si
->visited
, w
))
2118 label_visit (graph
, si
, w
);
2120 /* Skip unused edges */
2121 if (w
== n
|| graph
->pointer_label
[w
] == 0)
2124 if (graph
->points_to
[w
])
2125 bitmap_ior_into(graph
->points_to
[n
], graph
->points_to
[w
]);
2127 /* Indirect nodes get fresh variables. */
2128 if (!TEST_BIT (graph
->direct_nodes
, n
))
2129 bitmap_set_bit (graph
->points_to
[n
], FIRST_REF_NODE
+ n
);
2131 if (!bitmap_empty_p (graph
->points_to
[n
]))
2133 unsigned int label
= equiv_class_lookup (pointer_equiv_class_table
,
2134 graph
->points_to
[n
]);
2137 label
= pointer_equiv_class
++;
2138 equiv_class_add (pointer_equiv_class_table
,
2139 label
, graph
->points_to
[n
]);
2141 graph
->pointer_label
[n
] = label
;
2145 /* Perform offline variable substitution, discovering equivalence
2146 classes, and eliminating non-pointer variables. */
2148 static struct scc_info
*
2149 perform_var_substitution (constraint_graph_t graph
)
2152 unsigned int size
= graph
->size
;
2153 struct scc_info
*si
= init_scc_info (size
);
2155 bitmap_obstack_initialize (&iteration_obstack
);
2156 pointer_equiv_class_table
= htab_create (511, equiv_class_label_hash
,
2157 equiv_class_label_eq
, free
);
2158 location_equiv_class_table
= htab_create (511, equiv_class_label_hash
,
2159 equiv_class_label_eq
, free
);
2160 pointer_equiv_class
= 1;
2161 location_equiv_class
= 1;
2163 /* Condense the nodes, which means to find SCC's, count incoming
2164 predecessors, and unite nodes in SCC's. */
2165 for (i
= 0; i
< FIRST_REF_NODE
; i
++)
2166 if (!TEST_BIT (si
->visited
, si
->node_mapping
[i
]))
2167 condense_visit (graph
, si
, si
->node_mapping
[i
]);
2169 sbitmap_zero (si
->visited
);
2170 /* Actually the label the nodes for pointer equivalences */
2171 for (i
= 0; i
< FIRST_REF_NODE
; i
++)
2172 if (!TEST_BIT (si
->visited
, si
->node_mapping
[i
]))
2173 label_visit (graph
, si
, si
->node_mapping
[i
]);
2175 /* Calculate location equivalence labels. */
2176 for (i
= 0; i
< FIRST_REF_NODE
; i
++)
2183 if (!graph
->pointed_by
[i
])
2185 pointed_by
= BITMAP_ALLOC (&iteration_obstack
);
2187 /* Translate the pointed-by mapping for pointer equivalence
2189 EXECUTE_IF_SET_IN_BITMAP (graph
->pointed_by
[i
], 0, j
, bi
)
2191 bitmap_set_bit (pointed_by
,
2192 graph
->pointer_label
[si
->node_mapping
[j
]]);
2194 /* The original pointed_by is now dead. */
2195 BITMAP_FREE (graph
->pointed_by
[i
]);
2197 /* Look up the location equivalence label if one exists, or make
2199 label
= equiv_class_lookup (location_equiv_class_table
,
2203 label
= location_equiv_class
++;
2204 equiv_class_add (location_equiv_class_table
,
2209 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2210 fprintf (dump_file
, "Found location equivalence for node %s\n",
2211 get_varinfo (i
)->name
);
2212 BITMAP_FREE (pointed_by
);
2214 graph
->loc_label
[i
] = label
;
2218 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2219 for (i
= 0; i
< FIRST_REF_NODE
; i
++)
2221 bool direct_node
= TEST_BIT (graph
->direct_nodes
, i
);
2223 "Equivalence classes for %s node id %d:%s are pointer: %d"
2225 direct_node
? "Direct node" : "Indirect node", i
,
2226 get_varinfo (i
)->name
,
2227 graph
->pointer_label
[si
->node_mapping
[i
]],
2228 graph
->loc_label
[si
->node_mapping
[i
]]);
2231 /* Quickly eliminate our non-pointer variables. */
2233 for (i
= 0; i
< FIRST_REF_NODE
; i
++)
2235 unsigned int node
= si
->node_mapping
[i
];
2237 if (graph
->pointer_label
[node
] == 0)
2239 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2241 "%s is a non-pointer variable, eliminating edges.\n",
2242 get_varinfo (node
)->name
);
2243 stats
.nonpointer_vars
++;
2244 clear_edges_for_node (graph
, node
);
2251 /* Free information that was only necessary for variable
2255 free_var_substitution_info (struct scc_info
*si
)
2258 free (graph
->pointer_label
);
2259 free (graph
->loc_label
);
2260 free (graph
->pointed_by
);
2261 free (graph
->points_to
);
2262 free (graph
->eq_rep
);
2263 sbitmap_free (graph
->direct_nodes
);
2264 htab_delete (pointer_equiv_class_table
);
2265 htab_delete (location_equiv_class_table
);
2266 bitmap_obstack_release (&iteration_obstack
);
2269 /* Return an existing node that is equivalent to NODE, which has
2270 equivalence class LABEL, if one exists. Return NODE otherwise. */
2273 find_equivalent_node (constraint_graph_t graph
,
2274 unsigned int node
, unsigned int label
)
2276 /* If the address version of this variable is unused, we can
2277 substitute it for anything else with the same label.
2278 Otherwise, we know the pointers are equivalent, but not the
2279 locations, and we can unite them later. */
2281 if (!bitmap_bit_p (graph
->address_taken
, node
))
2283 gcc_assert (label
< graph
->size
);
2285 if (graph
->eq_rep
[label
] != -1)
2287 /* Unify the two variables since we know they are equivalent. */
2288 if (unite (graph
->eq_rep
[label
], node
))
2289 unify_nodes (graph
, graph
->eq_rep
[label
], node
, false);
2290 return graph
->eq_rep
[label
];
2294 graph
->eq_rep
[label
] = node
;
2295 graph
->pe_rep
[label
] = node
;
2300 gcc_assert (label
< graph
->size
);
2301 graph
->pe
[node
] = label
;
2302 if (graph
->pe_rep
[label
] == -1)
2303 graph
->pe_rep
[label
] = node
;
2309 /* Unite pointer equivalent but not location equivalent nodes in
2310 GRAPH. This may only be performed once variable substitution is
2314 unite_pointer_equivalences (constraint_graph_t graph
)
2318 /* Go through the pointer equivalences and unite them to their
2319 representative, if they aren't already. */
2320 for (i
= 0; i
< FIRST_REF_NODE
; i
++)
2322 unsigned int label
= graph
->pe
[i
];
2325 int label_rep
= graph
->pe_rep
[label
];
2327 if (label_rep
== -1)
2330 label_rep
= find (label_rep
);
2331 if (label_rep
>= 0 && unite (label_rep
, find (i
)))
2332 unify_nodes (graph
, label_rep
, i
, false);
2337 /* Move complex constraints to the GRAPH nodes they belong to. */
2340 move_complex_constraints (constraint_graph_t graph
)
2345 FOR_EACH_VEC_ELT (constraint_t
, constraints
, i
, c
)
2349 struct constraint_expr lhs
= c
->lhs
;
2350 struct constraint_expr rhs
= c
->rhs
;
2352 if (lhs
.type
== DEREF
)
2354 insert_into_complex (graph
, lhs
.var
, c
);
2356 else if (rhs
.type
== DEREF
)
2358 if (!(get_varinfo (lhs
.var
)->is_special_var
))
2359 insert_into_complex (graph
, rhs
.var
, c
);
2361 else if (rhs
.type
!= ADDRESSOF
&& lhs
.var
> anything_id
2362 && (lhs
.offset
!= 0 || rhs
.offset
!= 0))
2364 insert_into_complex (graph
, rhs
.var
, c
);
2371 /* Optimize and rewrite complex constraints while performing
2372 collapsing of equivalent nodes. SI is the SCC_INFO that is the
2373 result of perform_variable_substitution. */
2376 rewrite_constraints (constraint_graph_t graph
,
2377 struct scc_info
*si
)
2383 for (j
= 0; j
< graph
->size
; j
++)
2384 gcc_assert (find (j
) == j
);
2386 FOR_EACH_VEC_ELT (constraint_t
, constraints
, i
, c
)
2388 struct constraint_expr lhs
= c
->lhs
;
2389 struct constraint_expr rhs
= c
->rhs
;
2390 unsigned int lhsvar
= find (lhs
.var
);
2391 unsigned int rhsvar
= find (rhs
.var
);
2392 unsigned int lhsnode
, rhsnode
;
2393 unsigned int lhslabel
, rhslabel
;
2395 lhsnode
= si
->node_mapping
[lhsvar
];
2396 rhsnode
= si
->node_mapping
[rhsvar
];
2397 lhslabel
= graph
->pointer_label
[lhsnode
];
2398 rhslabel
= graph
->pointer_label
[rhsnode
];
2400 /* See if it is really a non-pointer variable, and if so, ignore
2404 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2407 fprintf (dump_file
, "%s is a non-pointer variable,"
2408 "ignoring constraint:",
2409 get_varinfo (lhs
.var
)->name
);
2410 dump_constraint (dump_file
, c
);
2411 fprintf (dump_file
, "\n");
2413 VEC_replace (constraint_t
, constraints
, i
, NULL
);
2419 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2422 fprintf (dump_file
, "%s is a non-pointer variable,"
2423 "ignoring constraint:",
2424 get_varinfo (rhs
.var
)->name
);
2425 dump_constraint (dump_file
, c
);
2426 fprintf (dump_file
, "\n");
2428 VEC_replace (constraint_t
, constraints
, i
, NULL
);
2432 lhsvar
= find_equivalent_node (graph
, lhsvar
, lhslabel
);
2433 rhsvar
= find_equivalent_node (graph
, rhsvar
, rhslabel
);
2434 c
->lhs
.var
= lhsvar
;
2435 c
->rhs
.var
= rhsvar
;
2440 /* Eliminate indirect cycles involving NODE. Return true if NODE was
2441 part of an SCC, false otherwise. */
2444 eliminate_indirect_cycles (unsigned int node
)
2446 if (graph
->indirect_cycles
[node
] != -1
2447 && !bitmap_empty_p (get_varinfo (node
)->solution
))
2450 VEC(unsigned,heap
) *queue
= NULL
;
2452 unsigned int to
= find (graph
->indirect_cycles
[node
]);
2455 /* We can't touch the solution set and call unify_nodes
2456 at the same time, because unify_nodes is going to do
2457 bitmap unions into it. */
2459 EXECUTE_IF_SET_IN_BITMAP (get_varinfo (node
)->solution
, 0, i
, bi
)
2461 if (find (i
) == i
&& i
!= to
)
2464 VEC_safe_push (unsigned, heap
, queue
, i
);
2469 VEC_iterate (unsigned, queue
, queuepos
, i
);
2472 unify_nodes (graph
, to
, i
, true);
2474 VEC_free (unsigned, heap
, queue
);
2480 /* Solve the constraint graph GRAPH using our worklist solver.
2481 This is based on the PW* family of solvers from the "Efficient Field
2482 Sensitive Pointer Analysis for C" paper.
2483 It works by iterating over all the graph nodes, processing the complex
2484 constraints and propagating the copy constraints, until everything stops
2485 changed. This corresponds to steps 6-8 in the solving list given above. */
2488 solve_graph (constraint_graph_t graph
)
2490 unsigned int size
= graph
->size
;
2494 changed
= BITMAP_ALLOC (NULL
);
2496 /* Mark all initial non-collapsed nodes as changed. */
2497 for (i
= 0; i
< size
; i
++)
2499 varinfo_t ivi
= get_varinfo (i
);
2500 if (find (i
) == i
&& !bitmap_empty_p (ivi
->solution
)
2501 && ((graph
->succs
[i
] && !bitmap_empty_p (graph
->succs
[i
]))
2502 || VEC_length (constraint_t
, graph
->complex[i
]) > 0))
2503 bitmap_set_bit (changed
, i
);
2506 /* Allocate a bitmap to be used to store the changed bits. */
2507 pts
= BITMAP_ALLOC (&pta_obstack
);
2509 while (!bitmap_empty_p (changed
))
2512 struct topo_info
*ti
= init_topo_info ();
2515 bitmap_obstack_initialize (&iteration_obstack
);
2517 compute_topo_order (graph
, ti
);
2519 while (VEC_length (unsigned, ti
->topo_order
) != 0)
2522 i
= VEC_pop (unsigned, ti
->topo_order
);
2524 /* If this variable is not a representative, skip it. */
2528 /* In certain indirect cycle cases, we may merge this
2529 variable to another. */
2530 if (eliminate_indirect_cycles (i
) && find (i
) != i
)
2533 /* If the node has changed, we need to process the
2534 complex constraints and outgoing edges again. */
2535 if (bitmap_clear_bit (changed
, i
))
2540 VEC(constraint_t
,heap
) *complex = graph
->complex[i
];
2541 varinfo_t vi
= get_varinfo (i
);
2542 bool solution_empty
;
2544 /* Compute the changed set of solution bits. */
2545 if (vi
->oldsolution
)
2546 bitmap_and_compl (pts
, vi
->solution
, vi
->oldsolution
);
2548 bitmap_copy (pts
, vi
->solution
);
2550 if (bitmap_empty_p (pts
))
2553 if (vi
->oldsolution
)
2554 bitmap_ior_into (vi
->oldsolution
, pts
);
2557 vi
->oldsolution
= BITMAP_ALLOC (&oldpta_obstack
);
2558 bitmap_copy (vi
->oldsolution
, pts
);
2561 solution
= vi
->solution
;
2562 solution_empty
= bitmap_empty_p (solution
);
2564 /* Process the complex constraints */
2565 FOR_EACH_VEC_ELT (constraint_t
, complex, j
, c
)
2567 /* XXX: This is going to unsort the constraints in
2568 some cases, which will occasionally add duplicate
2569 constraints during unification. This does not
2570 affect correctness. */
2571 c
->lhs
.var
= find (c
->lhs
.var
);
2572 c
->rhs
.var
= find (c
->rhs
.var
);
2574 /* The only complex constraint that can change our
2575 solution to non-empty, given an empty solution,
2576 is a constraint where the lhs side is receiving
2577 some set from elsewhere. */
2578 if (!solution_empty
|| c
->lhs
.type
!= DEREF
)
2579 do_complex_constraint (graph
, c
, pts
);
2582 solution_empty
= bitmap_empty_p (solution
);
2584 if (!solution_empty
)
2587 unsigned eff_escaped_id
= find (escaped_id
);
2589 /* Propagate solution to all successors. */
2590 EXECUTE_IF_IN_NONNULL_BITMAP (graph
->succs
[i
],
2596 unsigned int to
= find (j
);
2597 tmp
= get_varinfo (to
)->solution
;
2600 /* Don't try to propagate to ourselves. */
2604 /* If we propagate from ESCAPED use ESCAPED as
2606 if (i
== eff_escaped_id
)
2607 flag
= bitmap_set_bit (tmp
, escaped_id
);
2609 flag
= set_union_with_increment (tmp
, pts
, 0);
2613 get_varinfo (to
)->solution
= tmp
;
2614 bitmap_set_bit (changed
, to
);
2620 free_topo_info (ti
);
2621 bitmap_obstack_release (&iteration_obstack
);
2625 BITMAP_FREE (changed
);
2626 bitmap_obstack_release (&oldpta_obstack
);
2629 /* Map from trees to variable infos. */
2630 static struct pointer_map_t
*vi_for_tree
;
2633 /* Insert ID as the variable id for tree T in the vi_for_tree map. */
2636 insert_vi_for_tree (tree t
, varinfo_t vi
)
2638 void **slot
= pointer_map_insert (vi_for_tree
, t
);
2640 gcc_assert (*slot
== NULL
);
2644 /* Find the variable info for tree T in VI_FOR_TREE. If T does not
2645 exist in the map, return NULL, otherwise, return the varinfo we found. */
2648 lookup_vi_for_tree (tree t
)
2650 void **slot
= pointer_map_contains (vi_for_tree
, t
);
2654 return (varinfo_t
) *slot
;
2657 /* Return a printable name for DECL */
2660 alias_get_name (tree decl
)
2664 int num_printed
= 0;
2666 if (DECL_ASSEMBLER_NAME_SET_P (decl
))
2667 res
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
2669 res
= get_name (decl
);
2677 if (TREE_CODE (decl
) == SSA_NAME
)
2679 num_printed
= asprintf (&temp
, "%s_%u",
2680 alias_get_name (SSA_NAME_VAR (decl
)),
2681 SSA_NAME_VERSION (decl
));
2683 else if (DECL_P (decl
))
2685 num_printed
= asprintf (&temp
, "D.%u", DECL_UID (decl
));
2687 if (num_printed
> 0)
2689 res
= ggc_strdup (temp
);
2695 /* Find the variable id for tree T in the map.
2696 If T doesn't exist in the map, create an entry for it and return it. */
2699 get_vi_for_tree (tree t
)
2701 void **slot
= pointer_map_contains (vi_for_tree
, t
);
2703 return get_varinfo (create_variable_info_for (t
, alias_get_name (t
)));
2705 return (varinfo_t
) *slot
;
2708 /* Get a scalar constraint expression for a new temporary variable. */
2710 static struct constraint_expr
2711 new_scalar_tmp_constraint_exp (const char *name
)
2713 struct constraint_expr tmp
;
2716 vi
= new_var_info (NULL_TREE
, name
);
2720 vi
->is_full_var
= 1;
2729 /* Get a constraint expression vector from an SSA_VAR_P node.
2730 If address_p is true, the result will be taken its address of. */
2733 get_constraint_for_ssa_var (tree t
, VEC(ce_s
, heap
) **results
, bool address_p
)
2735 struct constraint_expr cexpr
;
2738 /* We allow FUNCTION_DECLs here even though it doesn't make much sense. */
2739 gcc_assert (SSA_VAR_P (t
) || DECL_P (t
));
2741 /* For parameters, get at the points-to set for the actual parm
2743 if (TREE_CODE (t
) == SSA_NAME
2744 && (TREE_CODE (SSA_NAME_VAR (t
)) == PARM_DECL
2745 || TREE_CODE (SSA_NAME_VAR (t
)) == RESULT_DECL
)
2746 && SSA_NAME_IS_DEFAULT_DEF (t
))
2748 get_constraint_for_ssa_var (SSA_NAME_VAR (t
), results
, address_p
);
2752 /* For global variables resort to the alias target. */
2753 if (TREE_CODE (t
) == VAR_DECL
2754 && (TREE_STATIC (t
) || DECL_EXTERNAL (t
)))
2756 struct varpool_node
*node
= varpool_get_node (t
);
2757 if (node
&& node
->alias
)
2759 node
= varpool_variable_node (node
, NULL
);
2760 t
= node
->symbol
.decl
;
2764 vi
= get_vi_for_tree (t
);
2766 cexpr
.type
= SCALAR
;
2768 /* If we determine the result is "anything", and we know this is readonly,
2769 say it points to readonly memory instead. */
2770 if (cexpr
.var
== anything_id
&& TREE_READONLY (t
))
2773 cexpr
.type
= ADDRESSOF
;
2774 cexpr
.var
= readonly_id
;
2777 /* If we are not taking the address of the constraint expr, add all
2778 sub-fiels of the variable as well. */
2780 && !vi
->is_full_var
)
2782 for (; vi
; vi
= vi
->next
)
2785 VEC_safe_push (ce_s
, heap
, *results
, &cexpr
);
2790 VEC_safe_push (ce_s
, heap
, *results
, &cexpr
);
2793 /* Process constraint T, performing various simplifications and then
2794 adding it to our list of overall constraints. */
2797 process_constraint (constraint_t t
)
2799 struct constraint_expr rhs
= t
->rhs
;
2800 struct constraint_expr lhs
= t
->lhs
;
2802 gcc_assert (rhs
.var
< VEC_length (varinfo_t
, varmap
));
2803 gcc_assert (lhs
.var
< VEC_length (varinfo_t
, varmap
));
2805 /* If we didn't get any useful constraint from the lhs we get
2806 &ANYTHING as fallback from get_constraint_for. Deal with
2807 it here by turning it into *ANYTHING. */
2808 if (lhs
.type
== ADDRESSOF
2809 && lhs
.var
== anything_id
)
2812 /* ADDRESSOF on the lhs is invalid. */
2813 gcc_assert (lhs
.type
!= ADDRESSOF
);
2815 /* We shouldn't add constraints from things that cannot have pointers.
2816 It's not completely trivial to avoid in the callers, so do it here. */
2817 if (rhs
.type
!= ADDRESSOF
2818 && !get_varinfo (rhs
.var
)->may_have_pointers
)
2821 /* Likewise adding to the solution of a non-pointer var isn't useful. */
2822 if (!get_varinfo (lhs
.var
)->may_have_pointers
)
2825 /* This can happen in our IR with things like n->a = *p */
2826 if (rhs
.type
== DEREF
&& lhs
.type
== DEREF
&& rhs
.var
!= anything_id
)
2828 /* Split into tmp = *rhs, *lhs = tmp */
2829 struct constraint_expr tmplhs
;
2830 tmplhs
= new_scalar_tmp_constraint_exp ("doubledereftmp");
2831 process_constraint (new_constraint (tmplhs
, rhs
));
2832 process_constraint (new_constraint (lhs
, tmplhs
));
2834 else if (rhs
.type
== ADDRESSOF
&& lhs
.type
== DEREF
)
2836 /* Split into tmp = &rhs, *lhs = tmp */
2837 struct constraint_expr tmplhs
;
2838 tmplhs
= new_scalar_tmp_constraint_exp ("derefaddrtmp");
2839 process_constraint (new_constraint (tmplhs
, rhs
));
2840 process_constraint (new_constraint (lhs
, tmplhs
));
2844 gcc_assert (rhs
.type
!= ADDRESSOF
|| rhs
.offset
== 0);
2845 VEC_safe_push (constraint_t
, heap
, constraints
, t
);
2850 /* Return the position, in bits, of FIELD_DECL from the beginning of its
2853 static HOST_WIDE_INT
2854 bitpos_of_field (const tree fdecl
)
2856 if (!host_integerp (DECL_FIELD_OFFSET (fdecl
), 0)
2857 || !host_integerp (DECL_FIELD_BIT_OFFSET (fdecl
), 0))
2860 return (TREE_INT_CST_LOW (DECL_FIELD_OFFSET (fdecl
)) * BITS_PER_UNIT
2861 + TREE_INT_CST_LOW (DECL_FIELD_BIT_OFFSET (fdecl
)));
2865 /* Get constraint expressions for offsetting PTR by OFFSET. Stores the
2866 resulting constraint expressions in *RESULTS. */
2869 get_constraint_for_ptr_offset (tree ptr
, tree offset
,
2870 VEC (ce_s
, heap
) **results
)
2872 struct constraint_expr c
;
2874 HOST_WIDE_INT rhsoffset
;
2876 /* If we do not do field-sensitive PTA adding offsets to pointers
2877 does not change the points-to solution. */
2878 if (!use_field_sensitive
)
2880 get_constraint_for_rhs (ptr
, results
);
2884 /* If the offset is not a non-negative integer constant that fits
2885 in a HOST_WIDE_INT, we have to fall back to a conservative
2886 solution which includes all sub-fields of all pointed-to
2887 variables of ptr. */
2888 if (offset
== NULL_TREE
2889 || TREE_CODE (offset
) != INTEGER_CST
)
2890 rhsoffset
= UNKNOWN_OFFSET
;
2893 /* Sign-extend the offset. */
2895 = double_int_sext (tree_to_double_int (offset
),
2896 TYPE_PRECISION (TREE_TYPE (offset
)));
2897 if (!double_int_fits_in_shwi_p (soffset
))
2898 rhsoffset
= UNKNOWN_OFFSET
;
2901 /* Make sure the bit-offset also fits. */
2902 HOST_WIDE_INT rhsunitoffset
= soffset
.low
;
2903 rhsoffset
= rhsunitoffset
* BITS_PER_UNIT
;
2904 if (rhsunitoffset
!= rhsoffset
/ BITS_PER_UNIT
)
2905 rhsoffset
= UNKNOWN_OFFSET
;
2909 get_constraint_for_rhs (ptr
, results
);
2913 /* As we are eventually appending to the solution do not use
2914 VEC_iterate here. */
2915 n
= VEC_length (ce_s
, *results
);
2916 for (j
= 0; j
< n
; j
++)
2919 c
= *VEC_index (ce_s
, *results
, j
);
2920 curr
= get_varinfo (c
.var
);
2922 if (c
.type
== ADDRESSOF
2923 /* If this varinfo represents a full variable just use it. */
2924 && curr
->is_full_var
)
2926 else if (c
.type
== ADDRESSOF
2927 /* If we do not know the offset add all subfields. */
2928 && rhsoffset
== UNKNOWN_OFFSET
)
2930 varinfo_t temp
= lookup_vi_for_tree (curr
->decl
);
2933 struct constraint_expr c2
;
2935 c2
.type
= ADDRESSOF
;
2937 if (c2
.var
!= c
.var
)
2938 VEC_safe_push (ce_s
, heap
, *results
, &c2
);
2943 else if (c
.type
== ADDRESSOF
)
2946 unsigned HOST_WIDE_INT offset
= curr
->offset
+ rhsoffset
;
2948 /* Search the sub-field which overlaps with the
2949 pointed-to offset. If the result is outside of the variable
2950 we have to provide a conservative result, as the variable is
2951 still reachable from the resulting pointer (even though it
2952 technically cannot point to anything). The last and first
2953 sub-fields are such conservative results.
2954 ??? If we always had a sub-field for &object + 1 then
2955 we could represent this in a more precise way. */
2957 && curr
->offset
< offset
)
2959 temp
= first_or_preceding_vi_for_offset (curr
, offset
);
2961 /* If the found variable is not exactly at the pointed to
2962 result, we have to include the next variable in the
2963 solution as well. Otherwise two increments by offset / 2
2964 do not result in the same or a conservative superset
2966 if (temp
->offset
!= offset
2967 && temp
->next
!= NULL
)
2969 struct constraint_expr c2
;
2970 c2
.var
= temp
->next
->id
;
2971 c2
.type
= ADDRESSOF
;
2973 VEC_safe_push (ce_s
, heap
, *results
, &c2
);
2979 c
.offset
= rhsoffset
;
2981 VEC_replace (ce_s
, *results
, j
, &c
);
2986 /* Given a COMPONENT_REF T, return the constraint_expr vector for it.
2987 If address_p is true the result will be taken its address of.
2988 If lhs_p is true then the constraint expression is assumed to be used
2992 get_constraint_for_component_ref (tree t
, VEC(ce_s
, heap
) **results
,
2993 bool address_p
, bool lhs_p
)
2996 HOST_WIDE_INT bitsize
= -1;
2997 HOST_WIDE_INT bitmaxsize
= -1;
2998 HOST_WIDE_INT bitpos
;
3000 struct constraint_expr
*result
;
3002 /* Some people like to do cute things like take the address of
3005 while (handled_component_p (forzero
)
3006 || INDIRECT_REF_P (forzero
)
3007 || TREE_CODE (forzero
) == MEM_REF
)
3008 forzero
= TREE_OPERAND (forzero
, 0);
3010 if (CONSTANT_CLASS_P (forzero
) && integer_zerop (forzero
))
3012 struct constraint_expr temp
;
3015 temp
.var
= integer_id
;
3017 VEC_safe_push (ce_s
, heap
, *results
, &temp
);
3021 /* Handle type-punning through unions. If we are extracting a pointer
3022 from a union via a possibly type-punning access that pointer
3023 points to anything, similar to a conversion of an integer to
3029 TREE_CODE (u
) == COMPONENT_REF
|| TREE_CODE (u
) == ARRAY_REF
;
3030 u
= TREE_OPERAND (u
, 0))
3031 if (TREE_CODE (u
) == COMPONENT_REF
3032 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u
, 0))) == UNION_TYPE
)
3034 struct constraint_expr temp
;
3037 temp
.var
= anything_id
;
3038 temp
.type
= ADDRESSOF
;
3039 VEC_safe_push (ce_s
, heap
, *results
, &temp
);
3044 t
= get_ref_base_and_extent (t
, &bitpos
, &bitsize
, &bitmaxsize
);
3046 /* Pretend to take the address of the base, we'll take care of
3047 adding the required subset of sub-fields below. */
3048 get_constraint_for_1 (t
, results
, true, lhs_p
);
3049 gcc_assert (VEC_length (ce_s
, *results
) == 1);
3050 result
= VEC_last (ce_s
, *results
);
3052 if (result
->type
== SCALAR
3053 && get_varinfo (result
->var
)->is_full_var
)
3054 /* For single-field vars do not bother about the offset. */
3056 else if (result
->type
== SCALAR
)
3058 /* In languages like C, you can access one past the end of an
3059 array. You aren't allowed to dereference it, so we can
3060 ignore this constraint. When we handle pointer subtraction,
3061 we may have to do something cute here. */
3063 if ((unsigned HOST_WIDE_INT
)bitpos
< get_varinfo (result
->var
)->fullsize
3066 /* It's also not true that the constraint will actually start at the
3067 right offset, it may start in some padding. We only care about
3068 setting the constraint to the first actual field it touches, so
3070 struct constraint_expr cexpr
= *result
;
3072 VEC_pop (ce_s
, *results
);
3074 for (curr
= get_varinfo (cexpr
.var
); curr
; curr
= curr
->next
)
3076 if (ranges_overlap_p (curr
->offset
, curr
->size
,
3077 bitpos
, bitmaxsize
))
3079 cexpr
.var
= curr
->id
;
3080 VEC_safe_push (ce_s
, heap
, *results
, &cexpr
);
3085 /* If we are going to take the address of this field then
3086 to be able to compute reachability correctly add at least
3087 the last field of the variable. */
3089 && VEC_length (ce_s
, *results
) == 0)
3091 curr
= get_varinfo (cexpr
.var
);
3092 while (curr
->next
!= NULL
)
3094 cexpr
.var
= curr
->id
;
3095 VEC_safe_push (ce_s
, heap
, *results
, &cexpr
);
3097 else if (VEC_length (ce_s
, *results
) == 0)
3098 /* Assert that we found *some* field there. The user couldn't be
3099 accessing *only* padding. */
3100 /* Still the user could access one past the end of an array
3101 embedded in a struct resulting in accessing *only* padding. */
3102 /* Or accessing only padding via type-punning to a type
3103 that has a filed just in padding space. */
3105 cexpr
.type
= SCALAR
;
3106 cexpr
.var
= anything_id
;
3108 VEC_safe_push (ce_s
, heap
, *results
, &cexpr
);
3111 else if (bitmaxsize
== 0)
3113 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3114 fprintf (dump_file
, "Access to zero-sized part of variable,"
3118 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3119 fprintf (dump_file
, "Access to past the end of variable, ignoring\n");
3121 else if (result
->type
== DEREF
)
3123 /* If we do not know exactly where the access goes say so. Note
3124 that only for non-structure accesses we know that we access
3125 at most one subfiled of any variable. */
3127 || bitsize
!= bitmaxsize
3128 || AGGREGATE_TYPE_P (TREE_TYPE (orig_t
))
3129 || result
->offset
== UNKNOWN_OFFSET
)
3130 result
->offset
= UNKNOWN_OFFSET
;
3132 result
->offset
+= bitpos
;
3134 else if (result
->type
== ADDRESSOF
)
3136 /* We can end up here for component references on a
3137 VIEW_CONVERT_EXPR <>(&foobar). */
3138 result
->type
= SCALAR
;
3139 result
->var
= anything_id
;
3147 /* Dereference the constraint expression CONS, and return the result.
3148 DEREF (ADDRESSOF) = SCALAR
3149 DEREF (SCALAR) = DEREF
3150 DEREF (DEREF) = (temp = DEREF1; result = DEREF(temp))
3151 This is needed so that we can handle dereferencing DEREF constraints. */
3154 do_deref (VEC (ce_s
, heap
) **constraints
)
3156 struct constraint_expr
*c
;
3159 FOR_EACH_VEC_ELT (ce_s
, *constraints
, i
, c
)
3161 if (c
->type
== SCALAR
)
3163 else if (c
->type
== ADDRESSOF
)
3165 else if (c
->type
== DEREF
)
3167 struct constraint_expr tmplhs
;
3168 tmplhs
= new_scalar_tmp_constraint_exp ("dereftmp");
3169 process_constraint (new_constraint (tmplhs
, *c
));
3170 c
->var
= tmplhs
.var
;
3177 /* Given a tree T, return the constraint expression for taking the
3181 get_constraint_for_address_of (tree t
, VEC (ce_s
, heap
) **results
)
3183 struct constraint_expr
*c
;
3186 get_constraint_for_1 (t
, results
, true, true);
3188 FOR_EACH_VEC_ELT (ce_s
, *results
, i
, c
)
3190 if (c
->type
== DEREF
)
3193 c
->type
= ADDRESSOF
;
3197 /* Given a tree T, return the constraint expression for it. */
3200 get_constraint_for_1 (tree t
, VEC (ce_s
, heap
) **results
, bool address_p
,
3203 struct constraint_expr temp
;
3205 /* x = integer is all glommed to a single variable, which doesn't
3206 point to anything by itself. That is, of course, unless it is an
3207 integer constant being treated as a pointer, in which case, we
3208 will return that this is really the addressof anything. This
3209 happens below, since it will fall into the default case. The only
3210 case we know something about an integer treated like a pointer is
3211 when it is the NULL pointer, and then we just say it points to
3214 Do not do that if -fno-delete-null-pointer-checks though, because
3215 in that case *NULL does not fail, so it _should_ alias *anything.
3216 It is not worth adding a new option or renaming the existing one,
3217 since this case is relatively obscure. */
3218 if ((TREE_CODE (t
) == INTEGER_CST
3219 && integer_zerop (t
))
3220 /* The only valid CONSTRUCTORs in gimple with pointer typed
3221 elements are zero-initializer. But in IPA mode we also
3222 process global initializers, so verify at least. */
3223 || (TREE_CODE (t
) == CONSTRUCTOR
3224 && CONSTRUCTOR_NELTS (t
) == 0))
3226 if (flag_delete_null_pointer_checks
)
3227 temp
.var
= nothing_id
;
3229 temp
.var
= nonlocal_id
;
3230 temp
.type
= ADDRESSOF
;
3232 VEC_safe_push (ce_s
, heap
, *results
, &temp
);
3236 /* String constants are read-only. */
3237 if (TREE_CODE (t
) == STRING_CST
)
3239 temp
.var
= readonly_id
;
3242 VEC_safe_push (ce_s
, heap
, *results
, &temp
);
3246 switch (TREE_CODE_CLASS (TREE_CODE (t
)))
3248 case tcc_expression
:
3250 switch (TREE_CODE (t
))
3253 get_constraint_for_address_of (TREE_OPERAND (t
, 0), results
);
3261 switch (TREE_CODE (t
))
3265 struct constraint_expr cs
;
3267 get_constraint_for_ptr_offset (TREE_OPERAND (t
, 0),
3268 TREE_OPERAND (t
, 1), results
);
3271 /* If we are not taking the address then make sure to process
3272 all subvariables we might access. */
3276 cs
= *VEC_last (ce_s
, *results
);
3277 if (cs
.type
== DEREF
3278 && type_can_have_subvars (TREE_TYPE (t
)))
3280 /* For dereferences this means we have to defer it
3282 VEC_last (ce_s
, *results
)->offset
= UNKNOWN_OFFSET
;
3285 if (cs
.type
!= SCALAR
)
3288 vi
= get_varinfo (cs
.var
);
3290 if (!vi
->is_full_var
3293 unsigned HOST_WIDE_INT size
;
3294 if (host_integerp (TYPE_SIZE (TREE_TYPE (t
)), 1))
3295 size
= TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (t
)));
3298 for (; curr
; curr
= curr
->next
)
3300 if (curr
->offset
- vi
->offset
< size
)
3303 VEC_safe_push (ce_s
, heap
, *results
, &cs
);
3312 case ARRAY_RANGE_REF
:
3314 get_constraint_for_component_ref (t
, results
, address_p
, lhs_p
);
3316 case VIEW_CONVERT_EXPR
:
3317 get_constraint_for_1 (TREE_OPERAND (t
, 0), results
, address_p
,
3320 /* We are missing handling for TARGET_MEM_REF here. */
3325 case tcc_exceptional
:
3327 switch (TREE_CODE (t
))
3331 get_constraint_for_ssa_var (t
, results
, address_p
);
3338 VEC (ce_s
, heap
) *tmp
= NULL
;
3339 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t
), i
, val
)
3341 struct constraint_expr
*rhsp
;
3343 get_constraint_for_1 (val
, &tmp
, address_p
, lhs_p
);
3344 FOR_EACH_VEC_ELT (ce_s
, tmp
, j
, rhsp
)
3345 VEC_safe_push (ce_s
, heap
, *results
, rhsp
);
3346 VEC_truncate (ce_s
, tmp
, 0);
3348 VEC_free (ce_s
, heap
, tmp
);
3349 /* We do not know whether the constructor was complete,
3350 so technically we have to add &NOTHING or &ANYTHING
3351 like we do for an empty constructor as well. */
3358 case tcc_declaration
:
3360 get_constraint_for_ssa_var (t
, results
, address_p
);
3365 /* We cannot refer to automatic variables through constants. */
3366 temp
.type
= ADDRESSOF
;
3367 temp
.var
= nonlocal_id
;
3369 VEC_safe_push (ce_s
, heap
, *results
, &temp
);
3375 /* The default fallback is a constraint from anything. */
3376 temp
.type
= ADDRESSOF
;
3377 temp
.var
= anything_id
;
3379 VEC_safe_push (ce_s
, heap
, *results
, &temp
);
3382 /* Given a gimple tree T, return the constraint expression vector for it. */
3385 get_constraint_for (tree t
, VEC (ce_s
, heap
) **results
)
3387 gcc_assert (VEC_length (ce_s
, *results
) == 0);
3389 get_constraint_for_1 (t
, results
, false, true);
3392 /* Given a gimple tree T, return the constraint expression vector for it
3393 to be used as the rhs of a constraint. */
3396 get_constraint_for_rhs (tree t
, VEC (ce_s
, heap
) **results
)
3398 gcc_assert (VEC_length (ce_s
, *results
) == 0);
3400 get_constraint_for_1 (t
, results
, false, false);
3404 /* Efficiently generates constraints from all entries in *RHSC to all
3405 entries in *LHSC. */
3408 process_all_all_constraints (VEC (ce_s
, heap
) *lhsc
, VEC (ce_s
, heap
) *rhsc
)
3410 struct constraint_expr
*lhsp
, *rhsp
;
3413 if (VEC_length (ce_s
, lhsc
) <= 1
3414 || VEC_length (ce_s
, rhsc
) <= 1)
3416 FOR_EACH_VEC_ELT (ce_s
, lhsc
, i
, lhsp
)
3417 FOR_EACH_VEC_ELT (ce_s
, rhsc
, j
, rhsp
)
3418 process_constraint (new_constraint (*lhsp
, *rhsp
));
3422 struct constraint_expr tmp
;
3423 tmp
= new_scalar_tmp_constraint_exp ("allalltmp");
3424 FOR_EACH_VEC_ELT (ce_s
, rhsc
, i
, rhsp
)
3425 process_constraint (new_constraint (tmp
, *rhsp
));
3426 FOR_EACH_VEC_ELT (ce_s
, lhsc
, i
, lhsp
)
3427 process_constraint (new_constraint (*lhsp
, tmp
));
3431 /* Handle aggregate copies by expanding into copies of the respective
3432 fields of the structures. */
3435 do_structure_copy (tree lhsop
, tree rhsop
)
3437 struct constraint_expr
*lhsp
, *rhsp
;
3438 VEC (ce_s
, heap
) *lhsc
= NULL
, *rhsc
= NULL
;
3441 get_constraint_for (lhsop
, &lhsc
);
3442 get_constraint_for_rhs (rhsop
, &rhsc
);
3443 lhsp
= VEC_index (ce_s
, lhsc
, 0);
3444 rhsp
= VEC_index (ce_s
, rhsc
, 0);
3445 if (lhsp
->type
== DEREF
3446 || (lhsp
->type
== ADDRESSOF
&& lhsp
->var
== anything_id
)
3447 || rhsp
->type
== DEREF
)
3449 if (lhsp
->type
== DEREF
)
3451 gcc_assert (VEC_length (ce_s
, lhsc
) == 1);
3452 lhsp
->offset
= UNKNOWN_OFFSET
;
3454 if (rhsp
->type
== DEREF
)
3456 gcc_assert (VEC_length (ce_s
, rhsc
) == 1);
3457 rhsp
->offset
= UNKNOWN_OFFSET
;
3459 process_all_all_constraints (lhsc
, rhsc
);
3461 else if (lhsp
->type
== SCALAR
3462 && (rhsp
->type
== SCALAR
3463 || rhsp
->type
== ADDRESSOF
))
3465 HOST_WIDE_INT lhssize
, lhsmaxsize
, lhsoffset
;
3466 HOST_WIDE_INT rhssize
, rhsmaxsize
, rhsoffset
;
3468 get_ref_base_and_extent (lhsop
, &lhsoffset
, &lhssize
, &lhsmaxsize
);
3469 get_ref_base_and_extent (rhsop
, &rhsoffset
, &rhssize
, &rhsmaxsize
);
3470 for (j
= 0; VEC_iterate (ce_s
, lhsc
, j
, lhsp
);)
3472 varinfo_t lhsv
, rhsv
;
3473 rhsp
= VEC_index (ce_s
, rhsc
, k
);
3474 lhsv
= get_varinfo (lhsp
->var
);
3475 rhsv
= get_varinfo (rhsp
->var
);
3476 if (lhsv
->may_have_pointers
3477 && (lhsv
->is_full_var
3478 || rhsv
->is_full_var
3479 || ranges_overlap_p (lhsv
->offset
+ rhsoffset
, lhsv
->size
,
3480 rhsv
->offset
+ lhsoffset
, rhsv
->size
)))
3481 process_constraint (new_constraint (*lhsp
, *rhsp
));
3482 if (!rhsv
->is_full_var
3483 && (lhsv
->is_full_var
3484 || (lhsv
->offset
+ rhsoffset
+ lhsv
->size
3485 > rhsv
->offset
+ lhsoffset
+ rhsv
->size
)))
3488 if (k
>= VEC_length (ce_s
, rhsc
))
3498 VEC_free (ce_s
, heap
, lhsc
);
3499 VEC_free (ce_s
, heap
, rhsc
);
3502 /* Create constraints ID = { rhsc }. */
3505 make_constraints_to (unsigned id
, VEC(ce_s
, heap
) *rhsc
)
3507 struct constraint_expr
*c
;
3508 struct constraint_expr includes
;
3512 includes
.offset
= 0;
3513 includes
.type
= SCALAR
;
3515 FOR_EACH_VEC_ELT (ce_s
, rhsc
, j
, c
)
3516 process_constraint (new_constraint (includes
, *c
));
3519 /* Create a constraint ID = OP. */
3522 make_constraint_to (unsigned id
, tree op
)
3524 VEC(ce_s
, heap
) *rhsc
= NULL
;
3525 get_constraint_for_rhs (op
, &rhsc
);
3526 make_constraints_to (id
, rhsc
);
3527 VEC_free (ce_s
, heap
, rhsc
);
3530 /* Create a constraint ID = &FROM. */
3533 make_constraint_from (varinfo_t vi
, int from
)
3535 struct constraint_expr lhs
, rhs
;
3543 rhs
.type
= ADDRESSOF
;
3544 process_constraint (new_constraint (lhs
, rhs
));
3547 /* Create a constraint ID = FROM. */
3550 make_copy_constraint (varinfo_t vi
, int from
)
3552 struct constraint_expr lhs
, rhs
;
3561 process_constraint (new_constraint (lhs
, rhs
));
3564 /* Make constraints necessary to make OP escape. */
3567 make_escape_constraint (tree op
)
3569 make_constraint_to (escaped_id
, op
);
3572 /* Add constraints to that the solution of VI is transitively closed. */
3575 make_transitive_closure_constraints (varinfo_t vi
)
3577 struct constraint_expr lhs
, rhs
;
3586 process_constraint (new_constraint (lhs
, rhs
));
3588 /* VAR = VAR + UNKNOWN; */
3594 rhs
.offset
= UNKNOWN_OFFSET
;
3595 process_constraint (new_constraint (lhs
, rhs
));
3598 /* Temporary storage for fake var decls. */
3599 struct obstack fake_var_decl_obstack
;
3601 /* Build a fake VAR_DECL acting as referrer to a DECL_UID. */
3604 build_fake_var_decl (tree type
)
3606 tree decl
= (tree
) XOBNEW (&fake_var_decl_obstack
, struct tree_var_decl
);
3607 memset (decl
, 0, sizeof (struct tree_var_decl
));
3608 TREE_SET_CODE (decl
, VAR_DECL
);
3609 TREE_TYPE (decl
) = type
;
3610 DECL_UID (decl
) = allocate_decl_uid ();
3611 SET_DECL_PT_UID (decl
, -1);
3612 layout_decl (decl
, 0);
3616 /* Create a new artificial heap variable with NAME.
3617 Return the created variable. */
3620 make_heapvar (const char *name
)
3625 heapvar
= build_fake_var_decl (ptr_type_node
);
3626 DECL_EXTERNAL (heapvar
) = 1;
3628 vi
= new_var_info (heapvar
, name
);
3629 vi
->is_artificial_var
= true;
3630 vi
->is_heap_var
= true;
3631 vi
->is_unknown_size_var
= true;
3635 vi
->is_full_var
= true;
3636 insert_vi_for_tree (heapvar
, vi
);
3641 /* Create a new artificial heap variable with NAME and make a
3642 constraint from it to LHS. Set flags according to a tag used
3643 for tracking restrict pointers. */
3646 make_constraint_from_restrict (varinfo_t lhs
, const char *name
)
3648 varinfo_t vi
= make_heapvar (name
);
3649 vi
->is_global_var
= 1;
3650 vi
->may_have_pointers
= 1;
3651 make_constraint_from (lhs
, vi
->id
);
3655 /* Create a new artificial heap variable with NAME and make a
3656 constraint from it to LHS. Set flags according to a tag used
3657 for tracking restrict pointers and make the artificial heap
3658 point to global memory. */
3661 make_constraint_from_global_restrict (varinfo_t lhs
, const char *name
)
3663 varinfo_t vi
= make_constraint_from_restrict (lhs
, name
);
3664 make_copy_constraint (vi
, nonlocal_id
);
3668 /* In IPA mode there are varinfos for different aspects of reach
3669 function designator. One for the points-to set of the return
3670 value, one for the variables that are clobbered by the function,
3671 one for its uses and one for each parameter (including a single
3672 glob for remaining variadic arguments). */
3674 enum { fi_clobbers
= 1, fi_uses
= 2,
3675 fi_static_chain
= 3, fi_result
= 4, fi_parm_base
= 5 };
3677 /* Get a constraint for the requested part of a function designator FI
3678 when operating in IPA mode. */
3680 static struct constraint_expr
3681 get_function_part_constraint (varinfo_t fi
, unsigned part
)
3683 struct constraint_expr c
;
3685 gcc_assert (in_ipa_mode
);
3687 if (fi
->id
== anything_id
)
3689 /* ??? We probably should have a ANYFN special variable. */
3690 c
.var
= anything_id
;
3694 else if (TREE_CODE (fi
->decl
) == FUNCTION_DECL
)
3696 varinfo_t ai
= first_vi_for_offset (fi
, part
);
3700 c
.var
= anything_id
;
3714 /* For non-IPA mode, generate constraints necessary for a call on the
3718 handle_rhs_call (gimple stmt
, VEC(ce_s
, heap
) **results
)
3720 struct constraint_expr rhsc
;
3722 bool returns_uses
= false;
3724 for (i
= 0; i
< gimple_call_num_args (stmt
); ++i
)
3726 tree arg
= gimple_call_arg (stmt
, i
);
3727 int flags
= gimple_call_arg_flags (stmt
, i
);
3729 /* If the argument is not used we can ignore it. */
3730 if (flags
& EAF_UNUSED
)
3733 /* As we compute ESCAPED context-insensitive we do not gain
3734 any precision with just EAF_NOCLOBBER but not EAF_NOESCAPE
3735 set. The argument would still get clobbered through the
3737 ??? We might get away with less (and more precise) constraints
3738 if using a temporary for transitively closing things. */
3739 if ((flags
& EAF_NOCLOBBER
)
3740 && (flags
& EAF_NOESCAPE
))
3742 varinfo_t uses
= get_call_use_vi (stmt
);
3743 if (!(flags
& EAF_DIRECT
))
3744 make_transitive_closure_constraints (uses
);
3745 make_constraint_to (uses
->id
, arg
);
3746 returns_uses
= true;
3748 else if (flags
& EAF_NOESCAPE
)
3750 varinfo_t uses
= get_call_use_vi (stmt
);
3751 varinfo_t clobbers
= get_call_clobber_vi (stmt
);
3752 if (!(flags
& EAF_DIRECT
))
3754 make_transitive_closure_constraints (uses
);
3755 make_transitive_closure_constraints (clobbers
);
3757 make_constraint_to (uses
->id
, arg
);
3758 make_constraint_to (clobbers
->id
, arg
);
3759 returns_uses
= true;
3762 make_escape_constraint (arg
);
3765 /* If we added to the calls uses solution make sure we account for
3766 pointers to it to be returned. */
3769 rhsc
.var
= get_call_use_vi (stmt
)->id
;
3772 VEC_safe_push (ce_s
, heap
, *results
, &rhsc
);
3775 /* The static chain escapes as well. */
3776 if (gimple_call_chain (stmt
))
3777 make_escape_constraint (gimple_call_chain (stmt
));
3779 /* And if we applied NRV the address of the return slot escapes as well. */
3780 if (gimple_call_return_slot_opt_p (stmt
)
3781 && gimple_call_lhs (stmt
) != NULL_TREE
3782 && TREE_ADDRESSABLE (TREE_TYPE (gimple_call_lhs (stmt
))))
3784 VEC(ce_s
, heap
) *tmpc
= NULL
;
3785 struct constraint_expr lhsc
, *c
;
3786 get_constraint_for_address_of (gimple_call_lhs (stmt
), &tmpc
);
3787 lhsc
.var
= escaped_id
;
3790 FOR_EACH_VEC_ELT (ce_s
, tmpc
, i
, c
)
3791 process_constraint (new_constraint (lhsc
, *c
));
3792 VEC_free(ce_s
, heap
, tmpc
);
3795 /* Regular functions return nonlocal memory. */
3796 rhsc
.var
= nonlocal_id
;
3799 VEC_safe_push (ce_s
, heap
, *results
, &rhsc
);
3802 /* For non-IPA mode, generate constraints necessary for a call
3803 that returns a pointer and assigns it to LHS. This simply makes
3804 the LHS point to global and escaped variables. */
3807 handle_lhs_call (gimple stmt
, tree lhs
, int flags
, VEC(ce_s
, heap
) *rhsc
,
3810 VEC(ce_s
, heap
) *lhsc
= NULL
;
3812 get_constraint_for (lhs
, &lhsc
);
3813 /* If the store is to a global decl make sure to
3814 add proper escape constraints. */
3815 lhs
= get_base_address (lhs
);
3818 && is_global_var (lhs
))
3820 struct constraint_expr tmpc
;
3821 tmpc
.var
= escaped_id
;
3824 VEC_safe_push (ce_s
, heap
, lhsc
, &tmpc
);
3827 /* If the call returns an argument unmodified override the rhs
3829 flags
= gimple_call_return_flags (stmt
);
3830 if (flags
& ERF_RETURNS_ARG
3831 && (flags
& ERF_RETURN_ARG_MASK
) < gimple_call_num_args (stmt
))
3835 arg
= gimple_call_arg (stmt
, flags
& ERF_RETURN_ARG_MASK
);
3836 get_constraint_for (arg
, &rhsc
);
3837 process_all_all_constraints (lhsc
, rhsc
);
3838 VEC_free (ce_s
, heap
, rhsc
);
3840 else if (flags
& ERF_NOALIAS
)
3843 struct constraint_expr tmpc
;
3845 vi
= make_heapvar ("HEAP");
3846 /* We delay marking allocated storage global until we know if
3848 DECL_EXTERNAL (vi
->decl
) = 0;
3849 vi
->is_global_var
= 0;
3850 /* If this is not a real malloc call assume the memory was
3851 initialized and thus may point to global memory. All
3852 builtin functions with the malloc attribute behave in a sane way. */
3854 || DECL_BUILT_IN_CLASS (fndecl
) != BUILT_IN_NORMAL
)
3855 make_constraint_from (vi
, nonlocal_id
);
3858 tmpc
.type
= ADDRESSOF
;
3859 VEC_safe_push (ce_s
, heap
, rhsc
, &tmpc
);
3862 process_all_all_constraints (lhsc
, rhsc
);
3864 VEC_free (ce_s
, heap
, lhsc
);
3867 /* For non-IPA mode, generate constraints necessary for a call of a
3868 const function that returns a pointer in the statement STMT. */
3871 handle_const_call (gimple stmt
, VEC(ce_s
, heap
) **results
)
3873 struct constraint_expr rhsc
;
3876 /* Treat nested const functions the same as pure functions as far
3877 as the static chain is concerned. */
3878 if (gimple_call_chain (stmt
))
3880 varinfo_t uses
= get_call_use_vi (stmt
);
3881 make_transitive_closure_constraints (uses
);
3882 make_constraint_to (uses
->id
, gimple_call_chain (stmt
));
3883 rhsc
.var
= uses
->id
;
3886 VEC_safe_push (ce_s
, heap
, *results
, &rhsc
);
3889 /* May return arguments. */
3890 for (k
= 0; k
< gimple_call_num_args (stmt
); ++k
)
3892 tree arg
= gimple_call_arg (stmt
, k
);
3893 VEC(ce_s
, heap
) *argc
= NULL
;
3895 struct constraint_expr
*argp
;
3896 get_constraint_for_rhs (arg
, &argc
);
3897 FOR_EACH_VEC_ELT (ce_s
, argc
, i
, argp
)
3898 VEC_safe_push (ce_s
, heap
, *results
, argp
);
3899 VEC_free(ce_s
, heap
, argc
);
3902 /* May return addresses of globals. */
3903 rhsc
.var
= nonlocal_id
;
3905 rhsc
.type
= ADDRESSOF
;
3906 VEC_safe_push (ce_s
, heap
, *results
, &rhsc
);
3909 /* For non-IPA mode, generate constraints necessary for a call to a
3910 pure function in statement STMT. */
3913 handle_pure_call (gimple stmt
, VEC(ce_s
, heap
) **results
)
3915 struct constraint_expr rhsc
;
3917 varinfo_t uses
= NULL
;
3919 /* Memory reached from pointer arguments is call-used. */
3920 for (i
= 0; i
< gimple_call_num_args (stmt
); ++i
)
3922 tree arg
= gimple_call_arg (stmt
, i
);
3925 uses
= get_call_use_vi (stmt
);
3926 make_transitive_closure_constraints (uses
);
3928 make_constraint_to (uses
->id
, arg
);
3931 /* The static chain is used as well. */
3932 if (gimple_call_chain (stmt
))
3936 uses
= get_call_use_vi (stmt
);
3937 make_transitive_closure_constraints (uses
);
3939 make_constraint_to (uses
->id
, gimple_call_chain (stmt
));
3942 /* Pure functions may return call-used and nonlocal memory. */
3945 rhsc
.var
= uses
->id
;
3948 VEC_safe_push (ce_s
, heap
, *results
, &rhsc
);
3950 rhsc
.var
= nonlocal_id
;
3953 VEC_safe_push (ce_s
, heap
, *results
, &rhsc
);
3957 /* Return the varinfo for the callee of CALL. */
3960 get_fi_for_callee (gimple call
)
3962 tree decl
, fn
= gimple_call_fn (call
);
3964 if (fn
&& TREE_CODE (fn
) == OBJ_TYPE_REF
)
3965 fn
= OBJ_TYPE_REF_EXPR (fn
);
3967 /* If we can directly resolve the function being called, do so.
3968 Otherwise, it must be some sort of indirect expression that
3969 we should still be able to handle. */
3970 decl
= gimple_call_addr_fndecl (fn
);
3972 return get_vi_for_tree (decl
);
3974 /* If the function is anything other than a SSA name pointer we have no
3975 clue and should be getting ANYFN (well, ANYTHING for now). */
3976 if (!fn
|| TREE_CODE (fn
) != SSA_NAME
)
3977 return get_varinfo (anything_id
);
3979 if ((TREE_CODE (SSA_NAME_VAR (fn
)) == PARM_DECL
3980 || TREE_CODE (SSA_NAME_VAR (fn
)) == RESULT_DECL
)
3981 && SSA_NAME_IS_DEFAULT_DEF (fn
))
3982 fn
= SSA_NAME_VAR (fn
);
3984 return get_vi_for_tree (fn
);
3987 /* Create constraints for the builtin call T. Return true if the call
3988 was handled, otherwise false. */
3991 find_func_aliases_for_builtin_call (gimple t
)
3993 tree fndecl
= gimple_call_fndecl (t
);
3994 VEC(ce_s
, heap
) *lhsc
= NULL
;
3995 VEC(ce_s
, heap
) *rhsc
= NULL
;
3998 if (fndecl
!= NULL_TREE
3999 && DECL_BUILT_IN_CLASS (fndecl
) == BUILT_IN_NORMAL
)
4000 /* ??? All builtins that are handled here need to be handled
4001 in the alias-oracle query functions explicitly! */
4002 switch (DECL_FUNCTION_CODE (fndecl
))
4004 /* All the following functions return a pointer to the same object
4005 as their first argument points to. The functions do not add
4006 to the ESCAPED solution. The functions make the first argument
4007 pointed to memory point to what the second argument pointed to
4008 memory points to. */
4009 case BUILT_IN_STRCPY
:
4010 case BUILT_IN_STRNCPY
:
4011 case BUILT_IN_BCOPY
:
4012 case BUILT_IN_MEMCPY
:
4013 case BUILT_IN_MEMMOVE
:
4014 case BUILT_IN_MEMPCPY
:
4015 case BUILT_IN_STPCPY
:
4016 case BUILT_IN_STPNCPY
:
4017 case BUILT_IN_STRCAT
:
4018 case BUILT_IN_STRNCAT
:
4019 case BUILT_IN_STRCPY_CHK
:
4020 case BUILT_IN_STRNCPY_CHK
:
4021 case BUILT_IN_MEMCPY_CHK
:
4022 case BUILT_IN_MEMMOVE_CHK
:
4023 case BUILT_IN_MEMPCPY_CHK
:
4024 case BUILT_IN_STPCPY_CHK
:
4025 case BUILT_IN_STPNCPY_CHK
:
4026 case BUILT_IN_STRCAT_CHK
:
4027 case BUILT_IN_STRNCAT_CHK
:
4028 case BUILT_IN_TM_MEMCPY
:
4029 case BUILT_IN_TM_MEMMOVE
:
4031 tree res
= gimple_call_lhs (t
);
4032 tree dest
= gimple_call_arg (t
, (DECL_FUNCTION_CODE (fndecl
)
4033 == BUILT_IN_BCOPY
? 1 : 0));
4034 tree src
= gimple_call_arg (t
, (DECL_FUNCTION_CODE (fndecl
)
4035 == BUILT_IN_BCOPY
? 0 : 1));
4036 if (res
!= NULL_TREE
)
4038 get_constraint_for (res
, &lhsc
);
4039 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_MEMPCPY
4040 || DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_STPCPY
4041 || DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_STPNCPY
4042 || DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_MEMPCPY_CHK
4043 || DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_STPCPY_CHK
4044 || DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_STPNCPY_CHK
)
4045 get_constraint_for_ptr_offset (dest
, NULL_TREE
, &rhsc
);
4047 get_constraint_for (dest
, &rhsc
);
4048 process_all_all_constraints (lhsc
, rhsc
);
4049 VEC_free (ce_s
, heap
, lhsc
);
4050 VEC_free (ce_s
, heap
, rhsc
);
4052 get_constraint_for_ptr_offset (dest
, NULL_TREE
, &lhsc
);
4053 get_constraint_for_ptr_offset (src
, NULL_TREE
, &rhsc
);
4056 process_all_all_constraints (lhsc
, rhsc
);
4057 VEC_free (ce_s
, heap
, lhsc
);
4058 VEC_free (ce_s
, heap
, rhsc
);
4061 case BUILT_IN_MEMSET
:
4062 case BUILT_IN_MEMSET_CHK
:
4063 case BUILT_IN_TM_MEMSET
:
4065 tree res
= gimple_call_lhs (t
);
4066 tree dest
= gimple_call_arg (t
, 0);
4069 struct constraint_expr ac
;
4070 if (res
!= NULL_TREE
)
4072 get_constraint_for (res
, &lhsc
);
4073 get_constraint_for (dest
, &rhsc
);
4074 process_all_all_constraints (lhsc
, rhsc
);
4075 VEC_free (ce_s
, heap
, lhsc
);
4076 VEC_free (ce_s
, heap
, rhsc
);
4078 get_constraint_for_ptr_offset (dest
, NULL_TREE
, &lhsc
);
4080 if (flag_delete_null_pointer_checks
4081 && integer_zerop (gimple_call_arg (t
, 1)))
4083 ac
.type
= ADDRESSOF
;
4084 ac
.var
= nothing_id
;
4089 ac
.var
= integer_id
;
4092 FOR_EACH_VEC_ELT (ce_s
, lhsc
, i
, lhsp
)
4093 process_constraint (new_constraint (*lhsp
, ac
));
4094 VEC_free (ce_s
, heap
, lhsc
);
4097 case BUILT_IN_ASSUME_ALIGNED
:
4099 tree res
= gimple_call_lhs (t
);
4100 tree dest
= gimple_call_arg (t
, 0);
4101 if (res
!= NULL_TREE
)
4103 get_constraint_for (res
, &lhsc
);
4104 get_constraint_for (dest
, &rhsc
);
4105 process_all_all_constraints (lhsc
, rhsc
);
4106 VEC_free (ce_s
, heap
, lhsc
);
4107 VEC_free (ce_s
, heap
, rhsc
);
4111 /* All the following functions do not return pointers, do not
4112 modify the points-to sets of memory reachable from their
4113 arguments and do not add to the ESCAPED solution. */
4114 case BUILT_IN_SINCOS
:
4115 case BUILT_IN_SINCOSF
:
4116 case BUILT_IN_SINCOSL
:
4117 case BUILT_IN_FREXP
:
4118 case BUILT_IN_FREXPF
:
4119 case BUILT_IN_FREXPL
:
4120 case BUILT_IN_GAMMA_R
:
4121 case BUILT_IN_GAMMAF_R
:
4122 case BUILT_IN_GAMMAL_R
:
4123 case BUILT_IN_LGAMMA_R
:
4124 case BUILT_IN_LGAMMAF_R
:
4125 case BUILT_IN_LGAMMAL_R
:
4127 case BUILT_IN_MODFF
:
4128 case BUILT_IN_MODFL
:
4129 case BUILT_IN_REMQUO
:
4130 case BUILT_IN_REMQUOF
:
4131 case BUILT_IN_REMQUOL
:
4134 case BUILT_IN_STRDUP
:
4135 case BUILT_IN_STRNDUP
:
4136 if (gimple_call_lhs (t
))
4138 handle_lhs_call (t
, gimple_call_lhs (t
), gimple_call_flags (t
),
4140 get_constraint_for_ptr_offset (gimple_call_lhs (t
),
4142 get_constraint_for_ptr_offset (gimple_call_arg (t
, 0),
4146 process_all_all_constraints (lhsc
, rhsc
);
4147 VEC_free (ce_s
, heap
, lhsc
);
4148 VEC_free (ce_s
, heap
, rhsc
);
4152 /* Trampolines are special - they set up passing the static
4154 case BUILT_IN_INIT_TRAMPOLINE
:
4156 tree tramp
= gimple_call_arg (t
, 0);
4157 tree nfunc
= gimple_call_arg (t
, 1);
4158 tree frame
= gimple_call_arg (t
, 2);
4160 struct constraint_expr lhs
, *rhsp
;
4163 varinfo_t nfi
= NULL
;
4164 gcc_assert (TREE_CODE (nfunc
) == ADDR_EXPR
);
4165 nfi
= lookup_vi_for_tree (TREE_OPERAND (nfunc
, 0));
4168 lhs
= get_function_part_constraint (nfi
, fi_static_chain
);
4169 get_constraint_for (frame
, &rhsc
);
4170 FOR_EACH_VEC_ELT (ce_s
, rhsc
, i
, rhsp
)
4171 process_constraint (new_constraint (lhs
, *rhsp
));
4172 VEC_free (ce_s
, heap
, rhsc
);
4174 /* Make the frame point to the function for
4175 the trampoline adjustment call. */
4176 get_constraint_for (tramp
, &lhsc
);
4178 get_constraint_for (nfunc
, &rhsc
);
4179 process_all_all_constraints (lhsc
, rhsc
);
4180 VEC_free (ce_s
, heap
, rhsc
);
4181 VEC_free (ce_s
, heap
, lhsc
);
4186 /* Else fallthru to generic handling which will let
4187 the frame escape. */
4190 case BUILT_IN_ADJUST_TRAMPOLINE
:
4192 tree tramp
= gimple_call_arg (t
, 0);
4193 tree res
= gimple_call_lhs (t
);
4194 if (in_ipa_mode
&& res
)
4196 get_constraint_for (res
, &lhsc
);
4197 get_constraint_for (tramp
, &rhsc
);
4199 process_all_all_constraints (lhsc
, rhsc
);
4200 VEC_free (ce_s
, heap
, rhsc
);
4201 VEC_free (ce_s
, heap
, lhsc
);
4205 CASE_BUILT_IN_TM_STORE (1):
4206 CASE_BUILT_IN_TM_STORE (2):
4207 CASE_BUILT_IN_TM_STORE (4):
4208 CASE_BUILT_IN_TM_STORE (8):
4209 CASE_BUILT_IN_TM_STORE (FLOAT
):
4210 CASE_BUILT_IN_TM_STORE (DOUBLE
):
4211 CASE_BUILT_IN_TM_STORE (LDOUBLE
):
4212 CASE_BUILT_IN_TM_STORE (M64
):
4213 CASE_BUILT_IN_TM_STORE (M128
):
4214 CASE_BUILT_IN_TM_STORE (M256
):
4216 tree addr
= gimple_call_arg (t
, 0);
4217 tree src
= gimple_call_arg (t
, 1);
4219 get_constraint_for (addr
, &lhsc
);
4221 get_constraint_for (src
, &rhsc
);
4222 process_all_all_constraints (lhsc
, rhsc
);
4223 VEC_free (ce_s
, heap
, lhsc
);
4224 VEC_free (ce_s
, heap
, rhsc
);
4227 CASE_BUILT_IN_TM_LOAD (1):
4228 CASE_BUILT_IN_TM_LOAD (2):
4229 CASE_BUILT_IN_TM_LOAD (4):
4230 CASE_BUILT_IN_TM_LOAD (8):
4231 CASE_BUILT_IN_TM_LOAD (FLOAT
):
4232 CASE_BUILT_IN_TM_LOAD (DOUBLE
):
4233 CASE_BUILT_IN_TM_LOAD (LDOUBLE
):
4234 CASE_BUILT_IN_TM_LOAD (M64
):
4235 CASE_BUILT_IN_TM_LOAD (M128
):
4236 CASE_BUILT_IN_TM_LOAD (M256
):
4238 tree dest
= gimple_call_lhs (t
);
4239 tree addr
= gimple_call_arg (t
, 0);
4241 get_constraint_for (dest
, &lhsc
);
4242 get_constraint_for (addr
, &rhsc
);
4244 process_all_all_constraints (lhsc
, rhsc
);
4245 VEC_free (ce_s
, heap
, lhsc
);
4246 VEC_free (ce_s
, heap
, rhsc
);
4249 /* Variadic argument handling needs to be handled in IPA
4251 case BUILT_IN_VA_START
:
4253 tree valist
= gimple_call_arg (t
, 0);
4254 struct constraint_expr rhs
, *lhsp
;
4256 get_constraint_for (valist
, &lhsc
);
4258 /* The va_list gets access to pointers in variadic
4259 arguments. Which we know in the case of IPA analysis
4260 and otherwise are just all nonlocal variables. */
4263 fi
= lookup_vi_for_tree (cfun
->decl
);
4264 rhs
= get_function_part_constraint (fi
, ~0);
4265 rhs
.type
= ADDRESSOF
;
4269 rhs
.var
= nonlocal_id
;
4270 rhs
.type
= ADDRESSOF
;
4273 FOR_EACH_VEC_ELT (ce_s
, lhsc
, i
, lhsp
)
4274 process_constraint (new_constraint (*lhsp
, rhs
));
4275 VEC_free (ce_s
, heap
, lhsc
);
4276 /* va_list is clobbered. */
4277 make_constraint_to (get_call_clobber_vi (t
)->id
, valist
);
4280 /* va_end doesn't have any effect that matters. */
4281 case BUILT_IN_VA_END
:
4283 /* Alternate return. Simply give up for now. */
4284 case BUILT_IN_RETURN
:
4288 || !(fi
= get_vi_for_tree (cfun
->decl
)))
4289 make_constraint_from (get_varinfo (escaped_id
), anything_id
);
4290 else if (in_ipa_mode
4293 struct constraint_expr lhs
, rhs
;
4294 lhs
= get_function_part_constraint (fi
, fi_result
);
4295 rhs
.var
= anything_id
;
4298 process_constraint (new_constraint (lhs
, rhs
));
4302 /* printf-style functions may have hooks to set pointers to
4303 point to somewhere into the generated string. Leave them
4304 for a later excercise... */
4306 /* Fallthru to general call handling. */;
4312 /* Create constraints for the call T. */
4315 find_func_aliases_for_call (gimple t
)
4317 tree fndecl
= gimple_call_fndecl (t
);
4318 VEC(ce_s
, heap
) *lhsc
= NULL
;
4319 VEC(ce_s
, heap
) *rhsc
= NULL
;
4322 if (fndecl
!= NULL_TREE
4323 && DECL_BUILT_IN (fndecl
)
4324 && find_func_aliases_for_builtin_call (t
))
4327 fi
= get_fi_for_callee (t
);
4329 || (fndecl
&& !fi
->is_fn_info
))
4331 VEC(ce_s
, heap
) *rhsc
= NULL
;
4332 int flags
= gimple_call_flags (t
);
4334 /* Const functions can return their arguments and addresses
4335 of global memory but not of escaped memory. */
4336 if (flags
& (ECF_CONST
|ECF_NOVOPS
))
4338 if (gimple_call_lhs (t
))
4339 handle_const_call (t
, &rhsc
);
4341 /* Pure functions can return addresses in and of memory
4342 reachable from their arguments, but they are not an escape
4343 point for reachable memory of their arguments. */
4344 else if (flags
& (ECF_PURE
|ECF_LOOPING_CONST_OR_PURE
))
4345 handle_pure_call (t
, &rhsc
);
4347 handle_rhs_call (t
, &rhsc
);
4348 if (gimple_call_lhs (t
))
4349 handle_lhs_call (t
, gimple_call_lhs (t
), flags
, rhsc
, fndecl
);
4350 VEC_free (ce_s
, heap
, rhsc
);
4357 /* Assign all the passed arguments to the appropriate incoming
4358 parameters of the function. */
4359 for (j
= 0; j
< gimple_call_num_args (t
); j
++)
4361 struct constraint_expr lhs
;
4362 struct constraint_expr
*rhsp
;
4363 tree arg
= gimple_call_arg (t
, j
);
4365 get_constraint_for_rhs (arg
, &rhsc
);
4366 lhs
= get_function_part_constraint (fi
, fi_parm_base
+ j
);
4367 while (VEC_length (ce_s
, rhsc
) != 0)
4369 rhsp
= VEC_last (ce_s
, rhsc
);
4370 process_constraint (new_constraint (lhs
, *rhsp
));
4371 VEC_pop (ce_s
, rhsc
);
4375 /* If we are returning a value, assign it to the result. */
4376 lhsop
= gimple_call_lhs (t
);
4379 struct constraint_expr rhs
;
4380 struct constraint_expr
*lhsp
;
4382 get_constraint_for (lhsop
, &lhsc
);
4383 rhs
= get_function_part_constraint (fi
, fi_result
);
4385 && DECL_RESULT (fndecl
)
4386 && DECL_BY_REFERENCE (DECL_RESULT (fndecl
)))
4388 VEC(ce_s
, heap
) *tem
= NULL
;
4389 VEC_safe_push (ce_s
, heap
, tem
, &rhs
);
4391 rhs
= *VEC_index (ce_s
, tem
, 0);
4392 VEC_free(ce_s
, heap
, tem
);
4394 FOR_EACH_VEC_ELT (ce_s
, lhsc
, j
, lhsp
)
4395 process_constraint (new_constraint (*lhsp
, rhs
));
4398 /* If we pass the result decl by reference, honor that. */
4401 && DECL_RESULT (fndecl
)
4402 && DECL_BY_REFERENCE (DECL_RESULT (fndecl
)))
4404 struct constraint_expr lhs
;
4405 struct constraint_expr
*rhsp
;
4407 get_constraint_for_address_of (lhsop
, &rhsc
);
4408 lhs
= get_function_part_constraint (fi
, fi_result
);
4409 FOR_EACH_VEC_ELT (ce_s
, rhsc
, j
, rhsp
)
4410 process_constraint (new_constraint (lhs
, *rhsp
));
4411 VEC_free (ce_s
, heap
, rhsc
);
4414 /* If we use a static chain, pass it along. */
4415 if (gimple_call_chain (t
))
4417 struct constraint_expr lhs
;
4418 struct constraint_expr
*rhsp
;
4420 get_constraint_for (gimple_call_chain (t
), &rhsc
);
4421 lhs
= get_function_part_constraint (fi
, fi_static_chain
);
4422 FOR_EACH_VEC_ELT (ce_s
, rhsc
, j
, rhsp
)
4423 process_constraint (new_constraint (lhs
, *rhsp
));
4428 /* Walk statement T setting up aliasing constraints according to the
4429 references found in T. This function is the main part of the
4430 constraint builder. AI points to auxiliary alias information used
4431 when building alias sets and computing alias grouping heuristics. */
4434 find_func_aliases (gimple origt
)
4437 VEC(ce_s
, heap
) *lhsc
= NULL
;
4438 VEC(ce_s
, heap
) *rhsc
= NULL
;
4439 struct constraint_expr
*c
;
4442 /* Now build constraints expressions. */
4443 if (gimple_code (t
) == GIMPLE_PHI
)
4448 /* For a phi node, assign all the arguments to
4450 get_constraint_for (gimple_phi_result (t
), &lhsc
);
4451 for (i
= 0; i
< gimple_phi_num_args (t
); i
++)
4453 tree strippedrhs
= PHI_ARG_DEF (t
, i
);
4455 STRIP_NOPS (strippedrhs
);
4456 get_constraint_for_rhs (gimple_phi_arg_def (t
, i
), &rhsc
);
4458 FOR_EACH_VEC_ELT (ce_s
, lhsc
, j
, c
)
4460 struct constraint_expr
*c2
;
4461 while (VEC_length (ce_s
, rhsc
) > 0)
4463 c2
= VEC_last (ce_s
, rhsc
);
4464 process_constraint (new_constraint (*c
, *c2
));
4465 VEC_pop (ce_s
, rhsc
);
4470 /* In IPA mode, we need to generate constraints to pass call
4471 arguments through their calls. There are two cases,
4472 either a GIMPLE_CALL returning a value, or just a plain
4473 GIMPLE_CALL when we are not.
4475 In non-ipa mode, we need to generate constraints for each
4476 pointer passed by address. */
4477 else if (is_gimple_call (t
))
4478 find_func_aliases_for_call (t
);
4480 /* Otherwise, just a regular assignment statement. Only care about
4481 operations with pointer result, others are dealt with as escape
4482 points if they have pointer operands. */
4483 else if (is_gimple_assign (t
))
4485 /* Otherwise, just a regular assignment statement. */
4486 tree lhsop
= gimple_assign_lhs (t
);
4487 tree rhsop
= (gimple_num_ops (t
) == 2) ? gimple_assign_rhs1 (t
) : NULL
;
4489 if (rhsop
&& TREE_CLOBBER_P (rhsop
))
4490 /* Ignore clobbers, they don't actually store anything into
4493 else if (rhsop
&& AGGREGATE_TYPE_P (TREE_TYPE (lhsop
)))
4494 do_structure_copy (lhsop
, rhsop
);
4497 enum tree_code code
= gimple_assign_rhs_code (t
);
4499 get_constraint_for (lhsop
, &lhsc
);
4501 if (code
== POINTER_PLUS_EXPR
)
4502 get_constraint_for_ptr_offset (gimple_assign_rhs1 (t
),
4503 gimple_assign_rhs2 (t
), &rhsc
);
4504 else if (code
== BIT_AND_EXPR
4505 && TREE_CODE (gimple_assign_rhs2 (t
)) == INTEGER_CST
)
4507 /* Aligning a pointer via a BIT_AND_EXPR is offsetting
4508 the pointer. Handle it by offsetting it by UNKNOWN. */
4509 get_constraint_for_ptr_offset (gimple_assign_rhs1 (t
),
4512 else if ((CONVERT_EXPR_CODE_P (code
)
4513 && !(POINTER_TYPE_P (gimple_expr_type (t
))
4514 && !POINTER_TYPE_P (TREE_TYPE (rhsop
))))
4515 || gimple_assign_single_p (t
))
4516 get_constraint_for_rhs (rhsop
, &rhsc
);
4517 else if (truth_value_p (code
))
4518 /* Truth value results are not pointer (parts). Or at least
4519 very very unreasonable obfuscation of a part. */
4523 /* All other operations are merges. */
4524 VEC (ce_s
, heap
) *tmp
= NULL
;
4525 struct constraint_expr
*rhsp
;
4527 get_constraint_for_rhs (gimple_assign_rhs1 (t
), &rhsc
);
4528 for (i
= 2; i
< gimple_num_ops (t
); ++i
)
4530 get_constraint_for_rhs (gimple_op (t
, i
), &tmp
);
4531 FOR_EACH_VEC_ELT (ce_s
, tmp
, j
, rhsp
)
4532 VEC_safe_push (ce_s
, heap
, rhsc
, rhsp
);
4533 VEC_truncate (ce_s
, tmp
, 0);
4535 VEC_free (ce_s
, heap
, tmp
);
4537 process_all_all_constraints (lhsc
, rhsc
);
4539 /* If there is a store to a global variable the rhs escapes. */
4540 if ((lhsop
= get_base_address (lhsop
)) != NULL_TREE
4542 && is_global_var (lhsop
)
4544 || DECL_EXTERNAL (lhsop
) || TREE_PUBLIC (lhsop
)))
4545 make_escape_constraint (rhsop
);
4547 /* Handle escapes through return. */
4548 else if (gimple_code (t
) == GIMPLE_RETURN
4549 && gimple_return_retval (t
) != NULL_TREE
)
4553 || !(fi
= get_vi_for_tree (cfun
->decl
)))
4554 make_escape_constraint (gimple_return_retval (t
));
4555 else if (in_ipa_mode
4558 struct constraint_expr lhs
;
4559 struct constraint_expr
*rhsp
;
4562 lhs
= get_function_part_constraint (fi
, fi_result
);
4563 get_constraint_for_rhs (gimple_return_retval (t
), &rhsc
);
4564 FOR_EACH_VEC_ELT (ce_s
, rhsc
, i
, rhsp
)
4565 process_constraint (new_constraint (lhs
, *rhsp
));
4568 /* Handle asms conservatively by adding escape constraints to everything. */
4569 else if (gimple_code (t
) == GIMPLE_ASM
)
4571 unsigned i
, noutputs
;
4572 const char **oconstraints
;
4573 const char *constraint
;
4574 bool allows_mem
, allows_reg
, is_inout
;
4576 noutputs
= gimple_asm_noutputs (t
);
4577 oconstraints
= XALLOCAVEC (const char *, noutputs
);
4579 for (i
= 0; i
< noutputs
; ++i
)
4581 tree link
= gimple_asm_output_op (t
, i
);
4582 tree op
= TREE_VALUE (link
);
4584 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link
)));
4585 oconstraints
[i
] = constraint
;
4586 parse_output_constraint (&constraint
, i
, 0, 0, &allows_mem
,
4587 &allows_reg
, &is_inout
);
4589 /* A memory constraint makes the address of the operand escape. */
4590 if (!allows_reg
&& allows_mem
)
4591 make_escape_constraint (build_fold_addr_expr (op
));
4593 /* The asm may read global memory, so outputs may point to
4594 any global memory. */
4597 VEC(ce_s
, heap
) *lhsc
= NULL
;
4598 struct constraint_expr rhsc
, *lhsp
;
4600 get_constraint_for (op
, &lhsc
);
4601 rhsc
.var
= nonlocal_id
;
4604 FOR_EACH_VEC_ELT (ce_s
, lhsc
, j
, lhsp
)
4605 process_constraint (new_constraint (*lhsp
, rhsc
));
4606 VEC_free (ce_s
, heap
, lhsc
);
4609 for (i
= 0; i
< gimple_asm_ninputs (t
); ++i
)
4611 tree link
= gimple_asm_input_op (t
, i
);
4612 tree op
= TREE_VALUE (link
);
4614 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link
)));
4616 parse_input_constraint (&constraint
, 0, 0, noutputs
, 0, oconstraints
,
4617 &allows_mem
, &allows_reg
);
4619 /* A memory constraint makes the address of the operand escape. */
4620 if (!allows_reg
&& allows_mem
)
4621 make_escape_constraint (build_fold_addr_expr (op
));
4622 /* Strictly we'd only need the constraint to ESCAPED if
4623 the asm clobbers memory, otherwise using something
4624 along the lines of per-call clobbers/uses would be enough. */
4626 make_escape_constraint (op
);
4630 VEC_free (ce_s
, heap
, rhsc
);
4631 VEC_free (ce_s
, heap
, lhsc
);
4635 /* Create a constraint adding to the clobber set of FI the memory
4636 pointed to by PTR. */
4639 process_ipa_clobber (varinfo_t fi
, tree ptr
)
4641 VEC(ce_s
, heap
) *ptrc
= NULL
;
4642 struct constraint_expr
*c
, lhs
;
4644 get_constraint_for_rhs (ptr
, &ptrc
);
4645 lhs
= get_function_part_constraint (fi
, fi_clobbers
);
4646 FOR_EACH_VEC_ELT (ce_s
, ptrc
, i
, c
)
4647 process_constraint (new_constraint (lhs
, *c
));
4648 VEC_free (ce_s
, heap
, ptrc
);
4651 /* Walk statement T setting up clobber and use constraints according to the
4652 references found in T. This function is a main part of the
4653 IPA constraint builder. */
4656 find_func_clobbers (gimple origt
)
4659 VEC(ce_s
, heap
) *lhsc
= NULL
;
4660 VEC(ce_s
, heap
) *rhsc
= NULL
;
4663 /* Add constraints for clobbered/used in IPA mode.
4664 We are not interested in what automatic variables are clobbered
4665 or used as we only use the information in the caller to which
4666 they do not escape. */
4667 gcc_assert (in_ipa_mode
);
4669 /* If the stmt refers to memory in any way it better had a VUSE. */
4670 if (gimple_vuse (t
) == NULL_TREE
)
4673 /* We'd better have function information for the current function. */
4674 fi
= lookup_vi_for_tree (cfun
->decl
);
4675 gcc_assert (fi
!= NULL
);
4677 /* Account for stores in assignments and calls. */
4678 if (gimple_vdef (t
) != NULL_TREE
4679 && gimple_has_lhs (t
))
4681 tree lhs
= gimple_get_lhs (t
);
4683 while (handled_component_p (tem
))
4684 tem
= TREE_OPERAND (tem
, 0);
4686 && !auto_var_in_fn_p (tem
, cfun
->decl
))
4687 || INDIRECT_REF_P (tem
)
4688 || (TREE_CODE (tem
) == MEM_REF
4689 && !(TREE_CODE (TREE_OPERAND (tem
, 0)) == ADDR_EXPR
4691 (TREE_OPERAND (TREE_OPERAND (tem
, 0), 0), cfun
->decl
))))
4693 struct constraint_expr lhsc
, *rhsp
;
4695 lhsc
= get_function_part_constraint (fi
, fi_clobbers
);
4696 get_constraint_for_address_of (lhs
, &rhsc
);
4697 FOR_EACH_VEC_ELT (ce_s
, rhsc
, i
, rhsp
)
4698 process_constraint (new_constraint (lhsc
, *rhsp
));
4699 VEC_free (ce_s
, heap
, rhsc
);
4703 /* Account for uses in assigments and returns. */
4704 if (gimple_assign_single_p (t
)
4705 || (gimple_code (t
) == GIMPLE_RETURN
4706 && gimple_return_retval (t
) != NULL_TREE
))
4708 tree rhs
= (gimple_assign_single_p (t
)
4709 ? gimple_assign_rhs1 (t
) : gimple_return_retval (t
));
4711 while (handled_component_p (tem
))
4712 tem
= TREE_OPERAND (tem
, 0);
4714 && !auto_var_in_fn_p (tem
, cfun
->decl
))
4715 || INDIRECT_REF_P (tem
)
4716 || (TREE_CODE (tem
) == MEM_REF
4717 && !(TREE_CODE (TREE_OPERAND (tem
, 0)) == ADDR_EXPR
4719 (TREE_OPERAND (TREE_OPERAND (tem
, 0), 0), cfun
->decl
))))
4721 struct constraint_expr lhs
, *rhsp
;
4723 lhs
= get_function_part_constraint (fi
, fi_uses
);
4724 get_constraint_for_address_of (rhs
, &rhsc
);
4725 FOR_EACH_VEC_ELT (ce_s
, rhsc
, i
, rhsp
)
4726 process_constraint (new_constraint (lhs
, *rhsp
));
4727 VEC_free (ce_s
, heap
, rhsc
);
4731 if (is_gimple_call (t
))
4733 varinfo_t cfi
= NULL
;
4734 tree decl
= gimple_call_fndecl (t
);
4735 struct constraint_expr lhs
, rhs
;
4738 /* For builtins we do not have separate function info. For those
4739 we do not generate escapes for we have to generate clobbers/uses. */
4741 && DECL_BUILT_IN_CLASS (decl
) == BUILT_IN_NORMAL
)
4742 switch (DECL_FUNCTION_CODE (decl
))
4744 /* The following functions use and clobber memory pointed to
4745 by their arguments. */
4746 case BUILT_IN_STRCPY
:
4747 case BUILT_IN_STRNCPY
:
4748 case BUILT_IN_BCOPY
:
4749 case BUILT_IN_MEMCPY
:
4750 case BUILT_IN_MEMMOVE
:
4751 case BUILT_IN_MEMPCPY
:
4752 case BUILT_IN_STPCPY
:
4753 case BUILT_IN_STPNCPY
:
4754 case BUILT_IN_STRCAT
:
4755 case BUILT_IN_STRNCAT
:
4756 case BUILT_IN_STRCPY_CHK
:
4757 case BUILT_IN_STRNCPY_CHK
:
4758 case BUILT_IN_MEMCPY_CHK
:
4759 case BUILT_IN_MEMMOVE_CHK
:
4760 case BUILT_IN_MEMPCPY_CHK
:
4761 case BUILT_IN_STPCPY_CHK
:
4762 case BUILT_IN_STPNCPY_CHK
:
4763 case BUILT_IN_STRCAT_CHK
:
4764 case BUILT_IN_STRNCAT_CHK
:
4766 tree dest
= gimple_call_arg (t
, (DECL_FUNCTION_CODE (decl
)
4767 == BUILT_IN_BCOPY
? 1 : 0));
4768 tree src
= gimple_call_arg (t
, (DECL_FUNCTION_CODE (decl
)
4769 == BUILT_IN_BCOPY
? 0 : 1));
4771 struct constraint_expr
*rhsp
, *lhsp
;
4772 get_constraint_for_ptr_offset (dest
, NULL_TREE
, &lhsc
);
4773 lhs
= get_function_part_constraint (fi
, fi_clobbers
);
4774 FOR_EACH_VEC_ELT (ce_s
, lhsc
, i
, lhsp
)
4775 process_constraint (new_constraint (lhs
, *lhsp
));
4776 VEC_free (ce_s
, heap
, lhsc
);
4777 get_constraint_for_ptr_offset (src
, NULL_TREE
, &rhsc
);
4778 lhs
= get_function_part_constraint (fi
, fi_uses
);
4779 FOR_EACH_VEC_ELT (ce_s
, rhsc
, i
, rhsp
)
4780 process_constraint (new_constraint (lhs
, *rhsp
));
4781 VEC_free (ce_s
, heap
, rhsc
);
4784 /* The following function clobbers memory pointed to by
4786 case BUILT_IN_MEMSET
:
4787 case BUILT_IN_MEMSET_CHK
:
4789 tree dest
= gimple_call_arg (t
, 0);
4792 get_constraint_for_ptr_offset (dest
, NULL_TREE
, &lhsc
);
4793 lhs
= get_function_part_constraint (fi
, fi_clobbers
);
4794 FOR_EACH_VEC_ELT (ce_s
, lhsc
, i
, lhsp
)
4795 process_constraint (new_constraint (lhs
, *lhsp
));
4796 VEC_free (ce_s
, heap
, lhsc
);
4799 /* The following functions clobber their second and third
4801 case BUILT_IN_SINCOS
:
4802 case BUILT_IN_SINCOSF
:
4803 case BUILT_IN_SINCOSL
:
4805 process_ipa_clobber (fi
, gimple_call_arg (t
, 1));
4806 process_ipa_clobber (fi
, gimple_call_arg (t
, 2));
4809 /* The following functions clobber their second argument. */
4810 case BUILT_IN_FREXP
:
4811 case BUILT_IN_FREXPF
:
4812 case BUILT_IN_FREXPL
:
4813 case BUILT_IN_LGAMMA_R
:
4814 case BUILT_IN_LGAMMAF_R
:
4815 case BUILT_IN_LGAMMAL_R
:
4816 case BUILT_IN_GAMMA_R
:
4817 case BUILT_IN_GAMMAF_R
:
4818 case BUILT_IN_GAMMAL_R
:
4820 case BUILT_IN_MODFF
:
4821 case BUILT_IN_MODFL
:
4823 process_ipa_clobber (fi
, gimple_call_arg (t
, 1));
4826 /* The following functions clobber their third argument. */
4827 case BUILT_IN_REMQUO
:
4828 case BUILT_IN_REMQUOF
:
4829 case BUILT_IN_REMQUOL
:
4831 process_ipa_clobber (fi
, gimple_call_arg (t
, 2));
4834 /* The following functions neither read nor clobber memory. */
4835 case BUILT_IN_ASSUME_ALIGNED
:
4838 /* Trampolines are of no interest to us. */
4839 case BUILT_IN_INIT_TRAMPOLINE
:
4840 case BUILT_IN_ADJUST_TRAMPOLINE
:
4842 case BUILT_IN_VA_START
:
4843 case BUILT_IN_VA_END
:
4845 /* printf-style functions may have hooks to set pointers to
4846 point to somewhere into the generated string. Leave them
4847 for a later excercise... */
4849 /* Fallthru to general call handling. */;
4852 /* Parameters passed by value are used. */
4853 lhs
= get_function_part_constraint (fi
, fi_uses
);
4854 for (i
= 0; i
< gimple_call_num_args (t
); i
++)
4856 struct constraint_expr
*rhsp
;
4857 tree arg
= gimple_call_arg (t
, i
);
4859 if (TREE_CODE (arg
) == SSA_NAME
4860 || is_gimple_min_invariant (arg
))
4863 get_constraint_for_address_of (arg
, &rhsc
);
4864 FOR_EACH_VEC_ELT (ce_s
, rhsc
, j
, rhsp
)
4865 process_constraint (new_constraint (lhs
, *rhsp
));
4866 VEC_free (ce_s
, heap
, rhsc
);
4869 /* Build constraints for propagating clobbers/uses along the
4871 cfi
= get_fi_for_callee (t
);
4872 if (cfi
->id
== anything_id
)
4874 if (gimple_vdef (t
))
4875 make_constraint_from (first_vi_for_offset (fi
, fi_clobbers
),
4877 make_constraint_from (first_vi_for_offset (fi
, fi_uses
),
4882 /* For callees without function info (that's external functions),
4883 ESCAPED is clobbered and used. */
4884 if (gimple_call_fndecl (t
)
4885 && !cfi
->is_fn_info
)
4889 if (gimple_vdef (t
))
4890 make_copy_constraint (first_vi_for_offset (fi
, fi_clobbers
),
4892 make_copy_constraint (first_vi_for_offset (fi
, fi_uses
), escaped_id
);
4894 /* Also honor the call statement use/clobber info. */
4895 if ((vi
= lookup_call_clobber_vi (t
)) != NULL
)
4896 make_copy_constraint (first_vi_for_offset (fi
, fi_clobbers
),
4898 if ((vi
= lookup_call_use_vi (t
)) != NULL
)
4899 make_copy_constraint (first_vi_for_offset (fi
, fi_uses
),
4904 /* Otherwise the caller clobbers and uses what the callee does.
4905 ??? This should use a new complex constraint that filters
4906 local variables of the callee. */
4907 if (gimple_vdef (t
))
4909 lhs
= get_function_part_constraint (fi
, fi_clobbers
);
4910 rhs
= get_function_part_constraint (cfi
, fi_clobbers
);
4911 process_constraint (new_constraint (lhs
, rhs
));
4913 lhs
= get_function_part_constraint (fi
, fi_uses
);
4914 rhs
= get_function_part_constraint (cfi
, fi_uses
);
4915 process_constraint (new_constraint (lhs
, rhs
));
4917 else if (gimple_code (t
) == GIMPLE_ASM
)
4919 /* ??? Ick. We can do better. */
4920 if (gimple_vdef (t
))
4921 make_constraint_from (first_vi_for_offset (fi
, fi_clobbers
),
4923 make_constraint_from (first_vi_for_offset (fi
, fi_uses
),
4927 VEC_free (ce_s
, heap
, rhsc
);
4931 /* Find the first varinfo in the same variable as START that overlaps with
4932 OFFSET. Return NULL if we can't find one. */
4935 first_vi_for_offset (varinfo_t start
, unsigned HOST_WIDE_INT offset
)
4937 /* If the offset is outside of the variable, bail out. */
4938 if (offset
>= start
->fullsize
)
4941 /* If we cannot reach offset from start, lookup the first field
4942 and start from there. */
4943 if (start
->offset
> offset
)
4944 start
= lookup_vi_for_tree (start
->decl
);
4948 /* We may not find a variable in the field list with the actual
4949 offset when when we have glommed a structure to a variable.
4950 In that case, however, offset should still be within the size
4952 if (offset
>= start
->offset
4953 && (offset
- start
->offset
) < start
->size
)
4962 /* Find the first varinfo in the same variable as START that overlaps with
4963 OFFSET. If there is no such varinfo the varinfo directly preceding
4964 OFFSET is returned. */
4967 first_or_preceding_vi_for_offset (varinfo_t start
,
4968 unsigned HOST_WIDE_INT offset
)
4970 /* If we cannot reach offset from start, lookup the first field
4971 and start from there. */
4972 if (start
->offset
> offset
)
4973 start
= lookup_vi_for_tree (start
->decl
);
4975 /* We may not find a variable in the field list with the actual
4976 offset when when we have glommed a structure to a variable.
4977 In that case, however, offset should still be within the size
4979 If we got beyond the offset we look for return the field
4980 directly preceding offset which may be the last field. */
4982 && offset
>= start
->offset
4983 && !((offset
- start
->offset
) < start
->size
))
4984 start
= start
->next
;
4990 /* This structure is used during pushing fields onto the fieldstack
4991 to track the offset of the field, since bitpos_of_field gives it
4992 relative to its immediate containing type, and we want it relative
4993 to the ultimate containing object. */
4997 /* Offset from the base of the base containing object to this field. */
4998 HOST_WIDE_INT offset
;
5000 /* Size, in bits, of the field. */
5001 unsigned HOST_WIDE_INT size
;
5003 unsigned has_unknown_size
: 1;
5005 unsigned must_have_pointers
: 1;
5007 unsigned may_have_pointers
: 1;
5009 unsigned only_restrict_pointers
: 1;
5011 typedef struct fieldoff fieldoff_s
;
5013 DEF_VEC_O(fieldoff_s
);
5014 DEF_VEC_ALLOC_O(fieldoff_s
,heap
);
5016 /* qsort comparison function for two fieldoff's PA and PB */
5019 fieldoff_compare (const void *pa
, const void *pb
)
5021 const fieldoff_s
*foa
= (const fieldoff_s
*)pa
;
5022 const fieldoff_s
*fob
= (const fieldoff_s
*)pb
;
5023 unsigned HOST_WIDE_INT foasize
, fobsize
;
5025 if (foa
->offset
< fob
->offset
)
5027 else if (foa
->offset
> fob
->offset
)
5030 foasize
= foa
->size
;
5031 fobsize
= fob
->size
;
5032 if (foasize
< fobsize
)
5034 else if (foasize
> fobsize
)
5039 /* Sort a fieldstack according to the field offset and sizes. */
5041 sort_fieldstack (VEC(fieldoff_s
,heap
) *fieldstack
)
5043 VEC_qsort (fieldoff_s
, fieldstack
, fieldoff_compare
);
5046 /* Return true if T is a type that can have subvars. */
5049 type_can_have_subvars (const_tree t
)
5051 /* Aggregates without overlapping fields can have subvars. */
5052 return TREE_CODE (t
) == RECORD_TYPE
;
5055 /* Return true if V is a tree that we can have subvars for.
5056 Normally, this is any aggregate type. Also complex
5057 types which are not gimple registers can have subvars. */
5060 var_can_have_subvars (const_tree v
)
5062 /* Volatile variables should never have subvars. */
5063 if (TREE_THIS_VOLATILE (v
))
5066 /* Non decls or memory tags can never have subvars. */
5070 return type_can_have_subvars (TREE_TYPE (v
));
5073 /* Return true if T is a type that does contain pointers. */
5076 type_must_have_pointers (tree type
)
5078 if (POINTER_TYPE_P (type
))
5081 if (TREE_CODE (type
) == ARRAY_TYPE
)
5082 return type_must_have_pointers (TREE_TYPE (type
));
5084 /* A function or method can have pointers as arguments, so track
5085 those separately. */
5086 if (TREE_CODE (type
) == FUNCTION_TYPE
5087 || TREE_CODE (type
) == METHOD_TYPE
)
5094 field_must_have_pointers (tree t
)
5096 return type_must_have_pointers (TREE_TYPE (t
));
5099 /* Given a TYPE, and a vector of field offsets FIELDSTACK, push all
5100 the fields of TYPE onto fieldstack, recording their offsets along
5103 OFFSET is used to keep track of the offset in this entire
5104 structure, rather than just the immediately containing structure.
5105 Returns false if the caller is supposed to handle the field we
5109 push_fields_onto_fieldstack (tree type
, VEC(fieldoff_s
,heap
) **fieldstack
,
5110 HOST_WIDE_INT offset
)
5113 bool empty_p
= true;
5115 if (TREE_CODE (type
) != RECORD_TYPE
)
5118 /* If the vector of fields is growing too big, bail out early.
5119 Callers check for VEC_length <= MAX_FIELDS_FOR_FIELD_SENSITIVE, make
5121 if (VEC_length (fieldoff_s
, *fieldstack
) > MAX_FIELDS_FOR_FIELD_SENSITIVE
)
5124 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
5125 if (TREE_CODE (field
) == FIELD_DECL
)
5128 HOST_WIDE_INT foff
= bitpos_of_field (field
);
5130 if (!var_can_have_subvars (field
)
5131 || TREE_CODE (TREE_TYPE (field
)) == QUAL_UNION_TYPE
5132 || TREE_CODE (TREE_TYPE (field
)) == UNION_TYPE
)
5134 else if (!push_fields_onto_fieldstack
5135 (TREE_TYPE (field
), fieldstack
, offset
+ foff
)
5136 && (DECL_SIZE (field
)
5137 && !integer_zerop (DECL_SIZE (field
))))
5138 /* Empty structures may have actual size, like in C++. So
5139 see if we didn't push any subfields and the size is
5140 nonzero, push the field onto the stack. */
5145 fieldoff_s
*pair
= NULL
;
5146 bool has_unknown_size
= false;
5147 bool must_have_pointers_p
;
5149 if (!VEC_empty (fieldoff_s
, *fieldstack
))
5150 pair
= VEC_last (fieldoff_s
, *fieldstack
);
5152 /* If there isn't anything at offset zero, create sth. */
5154 && offset
+ foff
!= 0)
5156 pair
= VEC_safe_push (fieldoff_s
, heap
, *fieldstack
, NULL
);
5158 pair
->size
= offset
+ foff
;
5159 pair
->has_unknown_size
= false;
5160 pair
->must_have_pointers
= false;
5161 pair
->may_have_pointers
= false;
5162 pair
->only_restrict_pointers
= false;
5165 if (!DECL_SIZE (field
)
5166 || !host_integerp (DECL_SIZE (field
), 1))
5167 has_unknown_size
= true;
5169 /* If adjacent fields do not contain pointers merge them. */
5170 must_have_pointers_p
= field_must_have_pointers (field
);
5172 && !has_unknown_size
5173 && !must_have_pointers_p
5174 && !pair
->must_have_pointers
5175 && !pair
->has_unknown_size
5176 && pair
->offset
+ (HOST_WIDE_INT
)pair
->size
== offset
+ foff
)
5178 pair
->size
+= TREE_INT_CST_LOW (DECL_SIZE (field
));
5182 pair
= VEC_safe_push (fieldoff_s
, heap
, *fieldstack
, NULL
);
5183 pair
->offset
= offset
+ foff
;
5184 pair
->has_unknown_size
= has_unknown_size
;
5185 if (!has_unknown_size
)
5186 pair
->size
= TREE_INT_CST_LOW (DECL_SIZE (field
));
5189 pair
->must_have_pointers
= must_have_pointers_p
;
5190 pair
->may_have_pointers
= true;
5191 pair
->only_restrict_pointers
5192 = (!has_unknown_size
5193 && POINTER_TYPE_P (TREE_TYPE (field
))
5194 && TYPE_RESTRICT (TREE_TYPE (field
)));
5204 /* Count the number of arguments DECL has, and set IS_VARARGS to true
5205 if it is a varargs function. */
5208 count_num_arguments (tree decl
, bool *is_varargs
)
5210 unsigned int num
= 0;
5213 /* Capture named arguments for K&R functions. They do not
5214 have a prototype and thus no TYPE_ARG_TYPES. */
5215 for (t
= DECL_ARGUMENTS (decl
); t
; t
= DECL_CHAIN (t
))
5218 /* Check if the function has variadic arguments. */
5219 for (t
= TYPE_ARG_TYPES (TREE_TYPE (decl
)); t
; t
= TREE_CHAIN (t
))
5220 if (TREE_VALUE (t
) == void_type_node
)
5228 /* Creation function node for DECL, using NAME, and return the index
5229 of the variable we've created for the function. */
5232 create_function_info_for (tree decl
, const char *name
)
5234 struct function
*fn
= DECL_STRUCT_FUNCTION (decl
);
5235 varinfo_t vi
, prev_vi
;
5238 bool is_varargs
= false;
5239 unsigned int num_args
= count_num_arguments (decl
, &is_varargs
);
5241 /* Create the variable info. */
5243 vi
= new_var_info (decl
, name
);
5246 vi
->fullsize
= fi_parm_base
+ num_args
;
5248 vi
->may_have_pointers
= false;
5251 insert_vi_for_tree (vi
->decl
, vi
);
5255 /* Create a variable for things the function clobbers and one for
5256 things the function uses. */
5258 varinfo_t clobbervi
, usevi
;
5259 const char *newname
;
5262 asprintf (&tempname
, "%s.clobber", name
);
5263 newname
= ggc_strdup (tempname
);
5266 clobbervi
= new_var_info (NULL
, newname
);
5267 clobbervi
->offset
= fi_clobbers
;
5268 clobbervi
->size
= 1;
5269 clobbervi
->fullsize
= vi
->fullsize
;
5270 clobbervi
->is_full_var
= true;
5271 clobbervi
->is_global_var
= false;
5272 gcc_assert (prev_vi
->offset
< clobbervi
->offset
);
5273 prev_vi
->next
= clobbervi
;
5274 prev_vi
= clobbervi
;
5276 asprintf (&tempname
, "%s.use", name
);
5277 newname
= ggc_strdup (tempname
);
5280 usevi
= new_var_info (NULL
, newname
);
5281 usevi
->offset
= fi_uses
;
5283 usevi
->fullsize
= vi
->fullsize
;
5284 usevi
->is_full_var
= true;
5285 usevi
->is_global_var
= false;
5286 gcc_assert (prev_vi
->offset
< usevi
->offset
);
5287 prev_vi
->next
= usevi
;
5291 /* And one for the static chain. */
5292 if (fn
->static_chain_decl
!= NULL_TREE
)
5295 const char *newname
;
5298 asprintf (&tempname
, "%s.chain", name
);
5299 newname
= ggc_strdup (tempname
);
5302 chainvi
= new_var_info (fn
->static_chain_decl
, newname
);
5303 chainvi
->offset
= fi_static_chain
;
5305 chainvi
->fullsize
= vi
->fullsize
;
5306 chainvi
->is_full_var
= true;
5307 chainvi
->is_global_var
= false;
5308 gcc_assert (prev_vi
->offset
< chainvi
->offset
);
5309 prev_vi
->next
= chainvi
;
5311 insert_vi_for_tree (fn
->static_chain_decl
, chainvi
);
5314 /* Create a variable for the return var. */
5315 if (DECL_RESULT (decl
) != NULL
5316 || !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl
))))
5319 const char *newname
;
5321 tree resultdecl
= decl
;
5323 if (DECL_RESULT (decl
))
5324 resultdecl
= DECL_RESULT (decl
);
5326 asprintf (&tempname
, "%s.result", name
);
5327 newname
= ggc_strdup (tempname
);
5330 resultvi
= new_var_info (resultdecl
, newname
);
5331 resultvi
->offset
= fi_result
;
5333 resultvi
->fullsize
= vi
->fullsize
;
5334 resultvi
->is_full_var
= true;
5335 if (DECL_RESULT (decl
))
5336 resultvi
->may_have_pointers
= true;
5337 gcc_assert (prev_vi
->offset
< resultvi
->offset
);
5338 prev_vi
->next
= resultvi
;
5340 if (DECL_RESULT (decl
))
5341 insert_vi_for_tree (DECL_RESULT (decl
), resultvi
);
5344 /* Set up variables for each argument. */
5345 arg
= DECL_ARGUMENTS (decl
);
5346 for (i
= 0; i
< num_args
; i
++)
5349 const char *newname
;
5351 tree argdecl
= decl
;
5356 asprintf (&tempname
, "%s.arg%d", name
, i
);
5357 newname
= ggc_strdup (tempname
);
5360 argvi
= new_var_info (argdecl
, newname
);
5361 argvi
->offset
= fi_parm_base
+ i
;
5363 argvi
->is_full_var
= true;
5364 argvi
->fullsize
= vi
->fullsize
;
5366 argvi
->may_have_pointers
= true;
5367 gcc_assert (prev_vi
->offset
< argvi
->offset
);
5368 prev_vi
->next
= argvi
;
5372 insert_vi_for_tree (arg
, argvi
);
5373 arg
= DECL_CHAIN (arg
);
5377 /* Add one representative for all further args. */
5381 const char *newname
;
5385 asprintf (&tempname
, "%s.varargs", name
);
5386 newname
= ggc_strdup (tempname
);
5389 /* We need sth that can be pointed to for va_start. */
5390 decl
= build_fake_var_decl (ptr_type_node
);
5392 argvi
= new_var_info (decl
, newname
);
5393 argvi
->offset
= fi_parm_base
+ num_args
;
5395 argvi
->is_full_var
= true;
5396 argvi
->is_heap_var
= true;
5397 argvi
->fullsize
= vi
->fullsize
;
5398 gcc_assert (prev_vi
->offset
< argvi
->offset
);
5399 prev_vi
->next
= argvi
;
5407 /* Return true if FIELDSTACK contains fields that overlap.
5408 FIELDSTACK is assumed to be sorted by offset. */
5411 check_for_overlaps (VEC (fieldoff_s
,heap
) *fieldstack
)
5413 fieldoff_s
*fo
= NULL
;
5415 HOST_WIDE_INT lastoffset
= -1;
5417 FOR_EACH_VEC_ELT (fieldoff_s
, fieldstack
, i
, fo
)
5419 if (fo
->offset
== lastoffset
)
5421 lastoffset
= fo
->offset
;
5426 /* Create a varinfo structure for NAME and DECL, and add it to VARMAP.
5427 This will also create any varinfo structures necessary for fields
5431 create_variable_info_for_1 (tree decl
, const char *name
)
5433 varinfo_t vi
, newvi
;
5434 tree decl_type
= TREE_TYPE (decl
);
5435 tree declsize
= DECL_P (decl
) ? DECL_SIZE (decl
) : TYPE_SIZE (decl_type
);
5436 VEC (fieldoff_s
,heap
) *fieldstack
= NULL
;
5441 || !host_integerp (declsize
, 1))
5443 vi
= new_var_info (decl
, name
);
5447 vi
->is_unknown_size_var
= true;
5448 vi
->is_full_var
= true;
5449 vi
->may_have_pointers
= true;
5453 /* Collect field information. */
5454 if (use_field_sensitive
5455 && var_can_have_subvars (decl
)
5456 /* ??? Force us to not use subfields for global initializers
5457 in IPA mode. Else we'd have to parse arbitrary initializers. */
5459 && is_global_var (decl
)
5460 && DECL_INITIAL (decl
)))
5462 fieldoff_s
*fo
= NULL
;
5463 bool notokay
= false;
5466 push_fields_onto_fieldstack (decl_type
, &fieldstack
, 0);
5468 for (i
= 0; !notokay
&& VEC_iterate (fieldoff_s
, fieldstack
, i
, fo
); i
++)
5469 if (fo
->has_unknown_size
5476 /* We can't sort them if we have a field with a variable sized type,
5477 which will make notokay = true. In that case, we are going to return
5478 without creating varinfos for the fields anyway, so sorting them is a
5482 sort_fieldstack (fieldstack
);
5483 /* Due to some C++ FE issues, like PR 22488, we might end up
5484 what appear to be overlapping fields even though they,
5485 in reality, do not overlap. Until the C++ FE is fixed,
5486 we will simply disable field-sensitivity for these cases. */
5487 notokay
= check_for_overlaps (fieldstack
);
5491 VEC_free (fieldoff_s
, heap
, fieldstack
);
5494 /* If we didn't end up collecting sub-variables create a full
5495 variable for the decl. */
5496 if (VEC_length (fieldoff_s
, fieldstack
) <= 1
5497 || VEC_length (fieldoff_s
, fieldstack
) > MAX_FIELDS_FOR_FIELD_SENSITIVE
)
5499 vi
= new_var_info (decl
, name
);
5501 vi
->may_have_pointers
= true;
5502 vi
->fullsize
= TREE_INT_CST_LOW (declsize
);
5503 vi
->size
= vi
->fullsize
;
5504 vi
->is_full_var
= true;
5505 VEC_free (fieldoff_s
, heap
, fieldstack
);
5509 vi
= new_var_info (decl
, name
);
5510 vi
->fullsize
= TREE_INT_CST_LOW (declsize
);
5511 for (i
= 0, newvi
= vi
;
5512 VEC_iterate (fieldoff_s
, fieldstack
, i
, fo
);
5513 ++i
, newvi
= newvi
->next
)
5515 const char *newname
= "NULL";
5520 asprintf (&tempname
, "%s." HOST_WIDE_INT_PRINT_DEC
5521 "+" HOST_WIDE_INT_PRINT_DEC
, name
, fo
->offset
, fo
->size
);
5522 newname
= ggc_strdup (tempname
);
5525 newvi
->name
= newname
;
5526 newvi
->offset
= fo
->offset
;
5527 newvi
->size
= fo
->size
;
5528 newvi
->fullsize
= vi
->fullsize
;
5529 newvi
->may_have_pointers
= fo
->may_have_pointers
;
5530 newvi
->only_restrict_pointers
= fo
->only_restrict_pointers
;
5531 if (i
+ 1 < VEC_length (fieldoff_s
, fieldstack
))
5532 newvi
->next
= new_var_info (decl
, name
);
5535 VEC_free (fieldoff_s
, heap
, fieldstack
);
5541 create_variable_info_for (tree decl
, const char *name
)
5543 varinfo_t vi
= create_variable_info_for_1 (decl
, name
);
5544 unsigned int id
= vi
->id
;
5546 insert_vi_for_tree (decl
, vi
);
5548 if (TREE_CODE (decl
) != VAR_DECL
)
5551 /* Create initial constraints for globals. */
5552 for (; vi
; vi
= vi
->next
)
5554 if (!vi
->may_have_pointers
5555 || !vi
->is_global_var
)
5558 /* Mark global restrict qualified pointers. */
5559 if ((POINTER_TYPE_P (TREE_TYPE (decl
))
5560 && TYPE_RESTRICT (TREE_TYPE (decl
)))
5561 || vi
->only_restrict_pointers
)
5563 make_constraint_from_global_restrict (vi
, "GLOBAL_RESTRICT");
5567 /* In non-IPA mode the initializer from nonlocal is all we need. */
5569 || DECL_HARD_REGISTER (decl
))
5570 make_copy_constraint (vi
, nonlocal_id
);
5572 /* In IPA mode parse the initializer and generate proper constraints
5576 struct varpool_node
*vnode
= varpool_get_node (decl
);
5578 /* For escaped variables initialize them from nonlocal. */
5579 if (!varpool_all_refs_explicit_p (vnode
))
5580 make_copy_constraint (vi
, nonlocal_id
);
5582 /* If this is a global variable with an initializer and we are in
5583 IPA mode generate constraints for it. */
5584 if (DECL_INITIAL (decl
)
5587 VEC (ce_s
, heap
) *rhsc
= NULL
;
5588 struct constraint_expr lhs
, *rhsp
;
5590 get_constraint_for_rhs (DECL_INITIAL (decl
), &rhsc
);
5594 FOR_EACH_VEC_ELT (ce_s
, rhsc
, i
, rhsp
)
5595 process_constraint (new_constraint (lhs
, *rhsp
));
5596 /* If this is a variable that escapes from the unit
5597 the initializer escapes as well. */
5598 if (!varpool_all_refs_explicit_p (vnode
))
5600 lhs
.var
= escaped_id
;
5603 FOR_EACH_VEC_ELT (ce_s
, rhsc
, i
, rhsp
)
5604 process_constraint (new_constraint (lhs
, *rhsp
));
5606 VEC_free (ce_s
, heap
, rhsc
);
5614 /* Print out the points-to solution for VAR to FILE. */
5617 dump_solution_for_var (FILE *file
, unsigned int var
)
5619 varinfo_t vi
= get_varinfo (var
);
5623 /* Dump the solution for unified vars anyway, this avoids difficulties
5624 in scanning dumps in the testsuite. */
5625 fprintf (file
, "%s = { ", vi
->name
);
5626 vi
= get_varinfo (find (var
));
5627 EXECUTE_IF_SET_IN_BITMAP (vi
->solution
, 0, i
, bi
)
5628 fprintf (file
, "%s ", get_varinfo (i
)->name
);
5629 fprintf (file
, "}");
5631 /* But note when the variable was unified. */
5633 fprintf (file
, " same as %s", vi
->name
);
5635 fprintf (file
, "\n");
5638 /* Print the points-to solution for VAR to stdout. */
5641 debug_solution_for_var (unsigned int var
)
5643 dump_solution_for_var (stdout
, var
);
5646 /* Create varinfo structures for all of the variables in the
5647 function for intraprocedural mode. */
5650 intra_create_variable_infos (void)
5654 /* For each incoming pointer argument arg, create the constraint ARG
5655 = NONLOCAL or a dummy variable if it is a restrict qualified
5656 passed-by-reference argument. */
5657 for (t
= DECL_ARGUMENTS (current_function_decl
); t
; t
= DECL_CHAIN (t
))
5659 varinfo_t p
= get_vi_for_tree (t
);
5661 /* For restrict qualified pointers to objects passed by
5662 reference build a real representative for the pointed-to object.
5663 Treat restrict qualified references the same. */
5664 if (TYPE_RESTRICT (TREE_TYPE (t
))
5665 && ((DECL_BY_REFERENCE (t
) && POINTER_TYPE_P (TREE_TYPE (t
)))
5666 || TREE_CODE (TREE_TYPE (t
)) == REFERENCE_TYPE
)
5667 && !type_contains_placeholder_p (TREE_TYPE (TREE_TYPE (t
))))
5669 struct constraint_expr lhsc
, rhsc
;
5671 tree heapvar
= build_fake_var_decl (TREE_TYPE (TREE_TYPE (t
)));
5672 DECL_EXTERNAL (heapvar
) = 1;
5673 vi
= create_variable_info_for_1 (heapvar
, "PARM_NOALIAS");
5674 insert_vi_for_tree (heapvar
, vi
);
5679 rhsc
.type
= ADDRESSOF
;
5681 process_constraint (new_constraint (lhsc
, rhsc
));
5682 for (; vi
; vi
= vi
->next
)
5683 if (vi
->may_have_pointers
)
5685 if (vi
->only_restrict_pointers
)
5686 make_constraint_from_global_restrict (vi
, "GLOBAL_RESTRICT");
5688 make_copy_constraint (vi
, nonlocal_id
);
5693 if (POINTER_TYPE_P (TREE_TYPE (t
))
5694 && TYPE_RESTRICT (TREE_TYPE (t
)))
5695 make_constraint_from_global_restrict (p
, "PARM_RESTRICT");
5698 for (; p
; p
= p
->next
)
5700 if (p
->only_restrict_pointers
)
5701 make_constraint_from_global_restrict (p
, "PARM_RESTRICT");
5702 else if (p
->may_have_pointers
)
5703 make_constraint_from (p
, nonlocal_id
);
5708 /* Add a constraint for a result decl that is passed by reference. */
5709 if (DECL_RESULT (cfun
->decl
)
5710 && DECL_BY_REFERENCE (DECL_RESULT (cfun
->decl
)))
5712 varinfo_t p
, result_vi
= get_vi_for_tree (DECL_RESULT (cfun
->decl
));
5714 for (p
= result_vi
; p
; p
= p
->next
)
5715 make_constraint_from (p
, nonlocal_id
);
5718 /* Add a constraint for the incoming static chain parameter. */
5719 if (cfun
->static_chain_decl
!= NULL_TREE
)
5721 varinfo_t p
, chain_vi
= get_vi_for_tree (cfun
->static_chain_decl
);
5723 for (p
= chain_vi
; p
; p
= p
->next
)
5724 make_constraint_from (p
, nonlocal_id
);
5728 /* Structure used to put solution bitmaps in a hashtable so they can
5729 be shared among variables with the same points-to set. */
5731 typedef struct shared_bitmap_info
5735 } *shared_bitmap_info_t
;
5736 typedef const struct shared_bitmap_info
*const_shared_bitmap_info_t
;
5738 static htab_t shared_bitmap_table
;
5740 /* Hash function for a shared_bitmap_info_t */
5743 shared_bitmap_hash (const void *p
)
5745 const_shared_bitmap_info_t
const bi
= (const_shared_bitmap_info_t
) p
;
5746 return bi
->hashcode
;
5749 /* Equality function for two shared_bitmap_info_t's. */
5752 shared_bitmap_eq (const void *p1
, const void *p2
)
5754 const_shared_bitmap_info_t
const sbi1
= (const_shared_bitmap_info_t
) p1
;
5755 const_shared_bitmap_info_t
const sbi2
= (const_shared_bitmap_info_t
) p2
;
5756 return bitmap_equal_p (sbi1
->pt_vars
, sbi2
->pt_vars
);
5759 /* Lookup a bitmap in the shared bitmap hashtable, and return an already
5760 existing instance if there is one, NULL otherwise. */
5763 shared_bitmap_lookup (bitmap pt_vars
)
5766 struct shared_bitmap_info sbi
;
5768 sbi
.pt_vars
= pt_vars
;
5769 sbi
.hashcode
= bitmap_hash (pt_vars
);
5771 slot
= htab_find_slot_with_hash (shared_bitmap_table
, &sbi
,
5772 sbi
.hashcode
, NO_INSERT
);
5776 return ((shared_bitmap_info_t
) *slot
)->pt_vars
;
5780 /* Add a bitmap to the shared bitmap hashtable. */
5783 shared_bitmap_add (bitmap pt_vars
)
5786 shared_bitmap_info_t sbi
= XNEW (struct shared_bitmap_info
);
5788 sbi
->pt_vars
= pt_vars
;
5789 sbi
->hashcode
= bitmap_hash (pt_vars
);
5791 slot
= htab_find_slot_with_hash (shared_bitmap_table
, sbi
,
5792 sbi
->hashcode
, INSERT
);
5793 gcc_assert (!*slot
);
5794 *slot
= (void *) sbi
;
5798 /* Set bits in INTO corresponding to the variable uids in solution set FROM. */
5801 set_uids_in_ptset (bitmap into
, bitmap from
, struct pt_solution
*pt
)
5806 EXECUTE_IF_SET_IN_BITMAP (from
, 0, i
, bi
)
5808 varinfo_t vi
= get_varinfo (i
);
5810 /* The only artificial variables that are allowed in a may-alias
5811 set are heap variables. */
5812 if (vi
->is_artificial_var
&& !vi
->is_heap_var
)
5815 if (TREE_CODE (vi
->decl
) == VAR_DECL
5816 || TREE_CODE (vi
->decl
) == PARM_DECL
5817 || TREE_CODE (vi
->decl
) == RESULT_DECL
)
5819 /* If we are in IPA mode we will not recompute points-to
5820 sets after inlining so make sure they stay valid. */
5822 && !DECL_PT_UID_SET_P (vi
->decl
))
5823 SET_DECL_PT_UID (vi
->decl
, DECL_UID (vi
->decl
));
5825 /* Add the decl to the points-to set. Note that the points-to
5826 set contains global variables. */
5827 bitmap_set_bit (into
, DECL_PT_UID (vi
->decl
));
5828 if (vi
->is_global_var
)
5829 pt
->vars_contains_global
= true;
5835 /* Compute the points-to solution *PT for the variable VI. */
5838 find_what_var_points_to (varinfo_t orig_vi
, struct pt_solution
*pt
)
5842 bitmap finished_solution
;
5846 memset (pt
, 0, sizeof (struct pt_solution
));
5848 /* This variable may have been collapsed, let's get the real
5850 vi
= get_varinfo (find (orig_vi
->id
));
5852 /* Translate artificial variables into SSA_NAME_PTR_INFO
5854 EXECUTE_IF_SET_IN_BITMAP (vi
->solution
, 0, i
, bi
)
5856 varinfo_t vi
= get_varinfo (i
);
5858 if (vi
->is_artificial_var
)
5860 if (vi
->id
== nothing_id
)
5862 else if (vi
->id
== escaped_id
)
5865 pt
->ipa_escaped
= 1;
5869 else if (vi
->id
== nonlocal_id
)
5871 else if (vi
->is_heap_var
)
5872 /* We represent heapvars in the points-to set properly. */
5874 else if (vi
->id
== readonly_id
)
5877 else if (vi
->id
== anything_id
5878 || vi
->id
== integer_id
)
5883 /* Instead of doing extra work, simply do not create
5884 elaborate points-to information for pt_anything pointers. */
5888 /* Share the final set of variables when possible. */
5889 finished_solution
= BITMAP_GGC_ALLOC ();
5890 stats
.points_to_sets_created
++;
5892 set_uids_in_ptset (finished_solution
, vi
->solution
, pt
);
5893 result
= shared_bitmap_lookup (finished_solution
);
5896 shared_bitmap_add (finished_solution
);
5897 pt
->vars
= finished_solution
;
5902 bitmap_clear (finished_solution
);
5906 /* Given a pointer variable P, fill in its points-to set. */
5909 find_what_p_points_to (tree p
)
5911 struct ptr_info_def
*pi
;
5915 /* For parameters, get at the points-to set for the actual parm
5917 if (TREE_CODE (p
) == SSA_NAME
5918 && (TREE_CODE (SSA_NAME_VAR (p
)) == PARM_DECL
5919 || TREE_CODE (SSA_NAME_VAR (p
)) == RESULT_DECL
)
5920 && SSA_NAME_IS_DEFAULT_DEF (p
))
5921 lookup_p
= SSA_NAME_VAR (p
);
5923 vi
= lookup_vi_for_tree (lookup_p
);
5927 pi
= get_ptr_info (p
);
5928 find_what_var_points_to (vi
, &pi
->pt
);
5932 /* Query statistics for points-to solutions. */
5935 unsigned HOST_WIDE_INT pt_solution_includes_may_alias
;
5936 unsigned HOST_WIDE_INT pt_solution_includes_no_alias
;
5937 unsigned HOST_WIDE_INT pt_solutions_intersect_may_alias
;
5938 unsigned HOST_WIDE_INT pt_solutions_intersect_no_alias
;
5942 dump_pta_stats (FILE *s
)
5944 fprintf (s
, "\nPTA query stats:\n");
5945 fprintf (s
, " pt_solution_includes: "
5946 HOST_WIDE_INT_PRINT_DEC
" disambiguations, "
5947 HOST_WIDE_INT_PRINT_DEC
" queries\n",
5948 pta_stats
.pt_solution_includes_no_alias
,
5949 pta_stats
.pt_solution_includes_no_alias
5950 + pta_stats
.pt_solution_includes_may_alias
);
5951 fprintf (s
, " pt_solutions_intersect: "
5952 HOST_WIDE_INT_PRINT_DEC
" disambiguations, "
5953 HOST_WIDE_INT_PRINT_DEC
" queries\n",
5954 pta_stats
.pt_solutions_intersect_no_alias
,
5955 pta_stats
.pt_solutions_intersect_no_alias
5956 + pta_stats
.pt_solutions_intersect_may_alias
);
5960 /* Reset the points-to solution *PT to a conservative default
5961 (point to anything). */
5964 pt_solution_reset (struct pt_solution
*pt
)
5966 memset (pt
, 0, sizeof (struct pt_solution
));
5967 pt
->anything
= true;
5970 /* Set the points-to solution *PT to point only to the variables
5971 in VARS. VARS_CONTAINS_GLOBAL specifies whether that contains
5972 global variables and VARS_CONTAINS_RESTRICT specifies whether
5973 it contains restrict tag variables. */
5976 pt_solution_set (struct pt_solution
*pt
, bitmap vars
, bool vars_contains_global
)
5978 memset (pt
, 0, sizeof (struct pt_solution
));
5980 pt
->vars_contains_global
= vars_contains_global
;
5983 /* Set the points-to solution *PT to point only to the variable VAR. */
5986 pt_solution_set_var (struct pt_solution
*pt
, tree var
)
5988 memset (pt
, 0, sizeof (struct pt_solution
));
5989 pt
->vars
= BITMAP_GGC_ALLOC ();
5990 bitmap_set_bit (pt
->vars
, DECL_PT_UID (var
));
5991 pt
->vars_contains_global
= is_global_var (var
);
5994 /* Computes the union of the points-to solutions *DEST and *SRC and
5995 stores the result in *DEST. This changes the points-to bitmap
5996 of *DEST and thus may not be used if that might be shared.
5997 The points-to bitmap of *SRC and *DEST will not be shared after
5998 this function if they were not before. */
6001 pt_solution_ior_into (struct pt_solution
*dest
, struct pt_solution
*src
)
6003 dest
->anything
|= src
->anything
;
6006 pt_solution_reset (dest
);
6010 dest
->nonlocal
|= src
->nonlocal
;
6011 dest
->escaped
|= src
->escaped
;
6012 dest
->ipa_escaped
|= src
->ipa_escaped
;
6013 dest
->null
|= src
->null
;
6014 dest
->vars_contains_global
|= src
->vars_contains_global
;
6019 dest
->vars
= BITMAP_GGC_ALLOC ();
6020 bitmap_ior_into (dest
->vars
, src
->vars
);
6023 /* Return true if the points-to solution *PT is empty. */
6026 pt_solution_empty_p (struct pt_solution
*pt
)
6033 && !bitmap_empty_p (pt
->vars
))
6036 /* If the solution includes ESCAPED, check if that is empty. */
6038 && !pt_solution_empty_p (&cfun
->gimple_df
->escaped
))
6041 /* If the solution includes ESCAPED, check if that is empty. */
6043 && !pt_solution_empty_p (&ipa_escaped_pt
))
6049 /* Return true if the points-to solution *PT only point to a single var, and
6050 return the var uid in *UID. */
6053 pt_solution_singleton_p (struct pt_solution
*pt
, unsigned *uid
)
6055 if (pt
->anything
|| pt
->nonlocal
|| pt
->escaped
|| pt
->ipa_escaped
6056 || pt
->null
|| pt
->vars
== NULL
6057 || !bitmap_single_bit_set_p (pt
->vars
))
6060 *uid
= bitmap_first_set_bit (pt
->vars
);
6064 /* Return true if the points-to solution *PT includes global memory. */
6067 pt_solution_includes_global (struct pt_solution
*pt
)
6071 || pt
->vars_contains_global
)
6075 return pt_solution_includes_global (&cfun
->gimple_df
->escaped
);
6077 if (pt
->ipa_escaped
)
6078 return pt_solution_includes_global (&ipa_escaped_pt
);
6080 /* ??? This predicate is not correct for the IPA-PTA solution
6081 as we do not properly distinguish between unit escape points
6082 and global variables. */
6083 if (cfun
->gimple_df
->ipa_pta
)
6089 /* Return true if the points-to solution *PT includes the variable
6090 declaration DECL. */
6093 pt_solution_includes_1 (struct pt_solution
*pt
, const_tree decl
)
6099 && is_global_var (decl
))
6103 && bitmap_bit_p (pt
->vars
, DECL_PT_UID (decl
)))
6106 /* If the solution includes ESCAPED, check it. */
6108 && pt_solution_includes_1 (&cfun
->gimple_df
->escaped
, decl
))
6111 /* If the solution includes ESCAPED, check it. */
6113 && pt_solution_includes_1 (&ipa_escaped_pt
, decl
))
6120 pt_solution_includes (struct pt_solution
*pt
, const_tree decl
)
6122 bool res
= pt_solution_includes_1 (pt
, decl
);
6124 ++pta_stats
.pt_solution_includes_may_alias
;
6126 ++pta_stats
.pt_solution_includes_no_alias
;
6130 /* Return true if both points-to solutions PT1 and PT2 have a non-empty
6134 pt_solutions_intersect_1 (struct pt_solution
*pt1
, struct pt_solution
*pt2
)
6136 if (pt1
->anything
|| pt2
->anything
)
6139 /* If either points to unknown global memory and the other points to
6140 any global memory they alias. */
6143 || pt2
->vars_contains_global
))
6145 && pt1
->vars_contains_global
))
6148 /* Check the escaped solution if required. */
6149 if ((pt1
->escaped
|| pt2
->escaped
)
6150 && !pt_solution_empty_p (&cfun
->gimple_df
->escaped
))
6152 /* If both point to escaped memory and that solution
6153 is not empty they alias. */
6154 if (pt1
->escaped
&& pt2
->escaped
)
6157 /* If either points to escaped memory see if the escaped solution
6158 intersects with the other. */
6160 && pt_solutions_intersect_1 (&cfun
->gimple_df
->escaped
, pt2
))
6162 && pt_solutions_intersect_1 (&cfun
->gimple_df
->escaped
, pt1
)))
6166 /* Check the escaped solution if required.
6167 ??? Do we need to check the local against the IPA escaped sets? */
6168 if ((pt1
->ipa_escaped
|| pt2
->ipa_escaped
)
6169 && !pt_solution_empty_p (&ipa_escaped_pt
))
6171 /* If both point to escaped memory and that solution
6172 is not empty they alias. */
6173 if (pt1
->ipa_escaped
&& pt2
->ipa_escaped
)
6176 /* If either points to escaped memory see if the escaped solution
6177 intersects with the other. */
6178 if ((pt1
->ipa_escaped
6179 && pt_solutions_intersect_1 (&ipa_escaped_pt
, pt2
))
6180 || (pt2
->ipa_escaped
6181 && pt_solutions_intersect_1 (&ipa_escaped_pt
, pt1
)))
6185 /* Now both pointers alias if their points-to solution intersects. */
6188 && bitmap_intersect_p (pt1
->vars
, pt2
->vars
));
6192 pt_solutions_intersect (struct pt_solution
*pt1
, struct pt_solution
*pt2
)
6194 bool res
= pt_solutions_intersect_1 (pt1
, pt2
);
6196 ++pta_stats
.pt_solutions_intersect_may_alias
;
6198 ++pta_stats
.pt_solutions_intersect_no_alias
;
6203 /* Dump points-to information to OUTFILE. */
6206 dump_sa_points_to_info (FILE *outfile
)
6210 fprintf (outfile
, "\nPoints-to sets\n\n");
6212 if (dump_flags
& TDF_STATS
)
6214 fprintf (outfile
, "Stats:\n");
6215 fprintf (outfile
, "Total vars: %d\n", stats
.total_vars
);
6216 fprintf (outfile
, "Non-pointer vars: %d\n",
6217 stats
.nonpointer_vars
);
6218 fprintf (outfile
, "Statically unified vars: %d\n",
6219 stats
.unified_vars_static
);
6220 fprintf (outfile
, "Dynamically unified vars: %d\n",
6221 stats
.unified_vars_dynamic
);
6222 fprintf (outfile
, "Iterations: %d\n", stats
.iterations
);
6223 fprintf (outfile
, "Number of edges: %d\n", stats
.num_edges
);
6224 fprintf (outfile
, "Number of implicit edges: %d\n",
6225 stats
.num_implicit_edges
);
6228 for (i
= 0; i
< VEC_length (varinfo_t
, varmap
); i
++)
6230 varinfo_t vi
= get_varinfo (i
);
6231 if (!vi
->may_have_pointers
)
6233 dump_solution_for_var (outfile
, i
);
6238 /* Debug points-to information to stderr. */
6241 debug_sa_points_to_info (void)
6243 dump_sa_points_to_info (stderr
);
6247 /* Initialize the always-existing constraint variables for NULL
6248 ANYTHING, READONLY, and INTEGER */
6251 init_base_vars (void)
6253 struct constraint_expr lhs
, rhs
;
6254 varinfo_t var_anything
;
6255 varinfo_t var_nothing
;
6256 varinfo_t var_readonly
;
6257 varinfo_t var_escaped
;
6258 varinfo_t var_nonlocal
;
6259 varinfo_t var_storedanything
;
6260 varinfo_t var_integer
;
6262 /* Create the NULL variable, used to represent that a variable points
6264 var_nothing
= new_var_info (NULL_TREE
, "NULL");
6265 gcc_assert (var_nothing
->id
== nothing_id
);
6266 var_nothing
->is_artificial_var
= 1;
6267 var_nothing
->offset
= 0;
6268 var_nothing
->size
= ~0;
6269 var_nothing
->fullsize
= ~0;
6270 var_nothing
->is_special_var
= 1;
6271 var_nothing
->may_have_pointers
= 0;
6272 var_nothing
->is_global_var
= 0;
6274 /* Create the ANYTHING variable, used to represent that a variable
6275 points to some unknown piece of memory. */
6276 var_anything
= new_var_info (NULL_TREE
, "ANYTHING");
6277 gcc_assert (var_anything
->id
== anything_id
);
6278 var_anything
->is_artificial_var
= 1;
6279 var_anything
->size
= ~0;
6280 var_anything
->offset
= 0;
6281 var_anything
->next
= NULL
;
6282 var_anything
->fullsize
= ~0;
6283 var_anything
->is_special_var
= 1;
6285 /* Anything points to anything. This makes deref constraints just
6286 work in the presence of linked list and other p = *p type loops,
6287 by saying that *ANYTHING = ANYTHING. */
6289 lhs
.var
= anything_id
;
6291 rhs
.type
= ADDRESSOF
;
6292 rhs
.var
= anything_id
;
6295 /* This specifically does not use process_constraint because
6296 process_constraint ignores all anything = anything constraints, since all
6297 but this one are redundant. */
6298 VEC_safe_push (constraint_t
, heap
, constraints
, new_constraint (lhs
, rhs
));
6300 /* Create the READONLY variable, used to represent that a variable
6301 points to readonly memory. */
6302 var_readonly
= new_var_info (NULL_TREE
, "READONLY");
6303 gcc_assert (var_readonly
->id
== readonly_id
);
6304 var_readonly
->is_artificial_var
= 1;
6305 var_readonly
->offset
= 0;
6306 var_readonly
->size
= ~0;
6307 var_readonly
->fullsize
= ~0;
6308 var_readonly
->next
= NULL
;
6309 var_readonly
->is_special_var
= 1;
6311 /* readonly memory points to anything, in order to make deref
6312 easier. In reality, it points to anything the particular
6313 readonly variable can point to, but we don't track this
6316 lhs
.var
= readonly_id
;
6318 rhs
.type
= ADDRESSOF
;
6319 rhs
.var
= readonly_id
; /* FIXME */
6321 process_constraint (new_constraint (lhs
, rhs
));
6323 /* Create the ESCAPED variable, used to represent the set of escaped
6325 var_escaped
= new_var_info (NULL_TREE
, "ESCAPED");
6326 gcc_assert (var_escaped
->id
== escaped_id
);
6327 var_escaped
->is_artificial_var
= 1;
6328 var_escaped
->offset
= 0;
6329 var_escaped
->size
= ~0;
6330 var_escaped
->fullsize
= ~0;
6331 var_escaped
->is_special_var
= 0;
6333 /* Create the NONLOCAL variable, used to represent the set of nonlocal
6335 var_nonlocal
= new_var_info (NULL_TREE
, "NONLOCAL");
6336 gcc_assert (var_nonlocal
->id
== nonlocal_id
);
6337 var_nonlocal
->is_artificial_var
= 1;
6338 var_nonlocal
->offset
= 0;
6339 var_nonlocal
->size
= ~0;
6340 var_nonlocal
->fullsize
= ~0;
6341 var_nonlocal
->is_special_var
= 1;
6343 /* ESCAPED = *ESCAPED, because escaped is may-deref'd at calls, etc. */
6345 lhs
.var
= escaped_id
;
6348 rhs
.var
= escaped_id
;
6350 process_constraint (new_constraint (lhs
, rhs
));
6352 /* ESCAPED = ESCAPED + UNKNOWN_OFFSET, because if a sub-field escapes the
6353 whole variable escapes. */
6355 lhs
.var
= escaped_id
;
6358 rhs
.var
= escaped_id
;
6359 rhs
.offset
= UNKNOWN_OFFSET
;
6360 process_constraint (new_constraint (lhs
, rhs
));
6362 /* *ESCAPED = NONLOCAL. This is true because we have to assume
6363 everything pointed to by escaped points to what global memory can
6366 lhs
.var
= escaped_id
;
6369 rhs
.var
= nonlocal_id
;
6371 process_constraint (new_constraint (lhs
, rhs
));
6373 /* NONLOCAL = &NONLOCAL, NONLOCAL = &ESCAPED. This is true because
6374 global memory may point to global memory and escaped memory. */
6376 lhs
.var
= nonlocal_id
;
6378 rhs
.type
= ADDRESSOF
;
6379 rhs
.var
= nonlocal_id
;
6381 process_constraint (new_constraint (lhs
, rhs
));
6382 rhs
.type
= ADDRESSOF
;
6383 rhs
.var
= escaped_id
;
6385 process_constraint (new_constraint (lhs
, rhs
));
6387 /* Create the STOREDANYTHING variable, used to represent the set of
6388 variables stored to *ANYTHING. */
6389 var_storedanything
= new_var_info (NULL_TREE
, "STOREDANYTHING");
6390 gcc_assert (var_storedanything
->id
== storedanything_id
);
6391 var_storedanything
->is_artificial_var
= 1;
6392 var_storedanything
->offset
= 0;
6393 var_storedanything
->size
= ~0;
6394 var_storedanything
->fullsize
= ~0;
6395 var_storedanything
->is_special_var
= 0;
6397 /* Create the INTEGER variable, used to represent that a variable points
6398 to what an INTEGER "points to". */
6399 var_integer
= new_var_info (NULL_TREE
, "INTEGER");
6400 gcc_assert (var_integer
->id
== integer_id
);
6401 var_integer
->is_artificial_var
= 1;
6402 var_integer
->size
= ~0;
6403 var_integer
->fullsize
= ~0;
6404 var_integer
->offset
= 0;
6405 var_integer
->next
= NULL
;
6406 var_integer
->is_special_var
= 1;
6408 /* INTEGER = ANYTHING, because we don't know where a dereference of
6409 a random integer will point to. */
6411 lhs
.var
= integer_id
;
6413 rhs
.type
= ADDRESSOF
;
6414 rhs
.var
= anything_id
;
6416 process_constraint (new_constraint (lhs
, rhs
));
6419 /* Initialize things necessary to perform PTA */
6422 init_alias_vars (void)
6424 use_field_sensitive
= (MAX_FIELDS_FOR_FIELD_SENSITIVE
> 1);
6426 bitmap_obstack_initialize (&pta_obstack
);
6427 bitmap_obstack_initialize (&oldpta_obstack
);
6428 bitmap_obstack_initialize (&predbitmap_obstack
);
6430 constraint_pool
= create_alloc_pool ("Constraint pool",
6431 sizeof (struct constraint
), 30);
6432 variable_info_pool
= create_alloc_pool ("Variable info pool",
6433 sizeof (struct variable_info
), 30);
6434 constraints
= VEC_alloc (constraint_t
, heap
, 8);
6435 varmap
= VEC_alloc (varinfo_t
, heap
, 8);
6436 vi_for_tree
= pointer_map_create ();
6437 call_stmt_vars
= pointer_map_create ();
6439 memset (&stats
, 0, sizeof (stats
));
6440 shared_bitmap_table
= htab_create (511, shared_bitmap_hash
,
6441 shared_bitmap_eq
, free
);
6444 gcc_obstack_init (&fake_var_decl_obstack
);
6447 /* Remove the REF and ADDRESS edges from GRAPH, as well as all the
6448 predecessor edges. */
6451 remove_preds_and_fake_succs (constraint_graph_t graph
)
6455 /* Clear the implicit ref and address nodes from the successor
6457 for (i
= 0; i
< FIRST_REF_NODE
; i
++)
6459 if (graph
->succs
[i
])
6460 bitmap_clear_range (graph
->succs
[i
], FIRST_REF_NODE
,
6461 FIRST_REF_NODE
* 2);
6464 /* Free the successor list for the non-ref nodes. */
6465 for (i
= FIRST_REF_NODE
; i
< graph
->size
; i
++)
6467 if (graph
->succs
[i
])
6468 BITMAP_FREE (graph
->succs
[i
]);
6471 /* Now reallocate the size of the successor list as, and blow away
6472 the predecessor bitmaps. */
6473 graph
->size
= VEC_length (varinfo_t
, varmap
);
6474 graph
->succs
= XRESIZEVEC (bitmap
, graph
->succs
, graph
->size
);
6476 free (graph
->implicit_preds
);
6477 graph
->implicit_preds
= NULL
;
6478 free (graph
->preds
);
6479 graph
->preds
= NULL
;
6480 bitmap_obstack_release (&predbitmap_obstack
);
6483 /* Solve the constraint set. */
6486 solve_constraints (void)
6488 struct scc_info
*si
;
6492 "\nCollapsing static cycles and doing variable "
6495 init_graph (VEC_length (varinfo_t
, varmap
) * 2);
6498 fprintf (dump_file
, "Building predecessor graph\n");
6499 build_pred_graph ();
6502 fprintf (dump_file
, "Detecting pointer and location "
6504 si
= perform_var_substitution (graph
);
6507 fprintf (dump_file
, "Rewriting constraints and unifying "
6509 rewrite_constraints (graph
, si
);
6511 build_succ_graph ();
6513 free_var_substitution_info (si
);
6515 /* Attach complex constraints to graph nodes. */
6516 move_complex_constraints (graph
);
6519 fprintf (dump_file
, "Uniting pointer but not location equivalent "
6521 unite_pointer_equivalences (graph
);
6524 fprintf (dump_file
, "Finding indirect cycles\n");
6525 find_indirect_cycles (graph
);
6527 /* Implicit nodes and predecessors are no longer necessary at this
6529 remove_preds_and_fake_succs (graph
);
6531 if (dump_file
&& (dump_flags
& TDF_GRAPH
))
6533 fprintf (dump_file
, "\n\n// The constraint graph before solve-graph "
6534 "in dot format:\n");
6535 dump_constraint_graph (dump_file
);
6536 fprintf (dump_file
, "\n\n");
6540 fprintf (dump_file
, "Solving graph\n");
6542 solve_graph (graph
);
6544 if (dump_file
&& (dump_flags
& TDF_GRAPH
))
6546 fprintf (dump_file
, "\n\n// The constraint graph after solve-graph "
6547 "in dot format:\n");
6548 dump_constraint_graph (dump_file
);
6549 fprintf (dump_file
, "\n\n");
6553 dump_sa_points_to_info (dump_file
);
6556 /* Create points-to sets for the current function. See the comments
6557 at the start of the file for an algorithmic overview. */
6560 compute_points_to_sets (void)
6566 timevar_push (TV_TREE_PTA
);
6570 intra_create_variable_infos ();
6572 /* Now walk all statements and build the constraint set. */
6575 gimple_stmt_iterator gsi
;
6577 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
6579 gimple phi
= gsi_stmt (gsi
);
6581 if (is_gimple_reg (gimple_phi_result (phi
)))
6582 find_func_aliases (phi
);
6585 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
6587 gimple stmt
= gsi_stmt (gsi
);
6589 find_func_aliases (stmt
);
6595 fprintf (dump_file
, "Points-to analysis\n\nConstraints:\n\n");
6596 dump_constraints (dump_file
, 0);
6599 /* From the constraints compute the points-to sets. */
6600 solve_constraints ();
6602 /* Compute the points-to set for ESCAPED used for call-clobber analysis. */
6603 find_what_var_points_to (get_varinfo (escaped_id
),
6604 &cfun
->gimple_df
->escaped
);
6606 /* Make sure the ESCAPED solution (which is used as placeholder in
6607 other solutions) does not reference itself. This simplifies
6608 points-to solution queries. */
6609 cfun
->gimple_df
->escaped
.escaped
= 0;
6611 /* Mark escaped HEAP variables as global. */
6612 FOR_EACH_VEC_ELT (varinfo_t
, varmap
, i
, vi
)
6614 && !vi
->is_global_var
)
6615 DECL_EXTERNAL (vi
->decl
) = vi
->is_global_var
6616 = pt_solution_includes (&cfun
->gimple_df
->escaped
, vi
->decl
);
6618 /* Compute the points-to sets for pointer SSA_NAMEs. */
6619 for (i
= 0; i
< num_ssa_names
; ++i
)
6621 tree ptr
= ssa_name (i
);
6623 && POINTER_TYPE_P (TREE_TYPE (ptr
)))
6624 find_what_p_points_to (ptr
);
6627 /* Compute the call-used/clobbered sets. */
6630 gimple_stmt_iterator gsi
;
6632 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
6634 gimple stmt
= gsi_stmt (gsi
);
6635 struct pt_solution
*pt
;
6636 if (!is_gimple_call (stmt
))
6639 pt
= gimple_call_use_set (stmt
);
6640 if (gimple_call_flags (stmt
) & ECF_CONST
)
6641 memset (pt
, 0, sizeof (struct pt_solution
));
6642 else if ((vi
= lookup_call_use_vi (stmt
)) != NULL
)
6644 find_what_var_points_to (vi
, pt
);
6645 /* Escaped (and thus nonlocal) variables are always
6646 implicitly used by calls. */
6647 /* ??? ESCAPED can be empty even though NONLOCAL
6654 /* If there is nothing special about this call then
6655 we have made everything that is used also escape. */
6656 *pt
= cfun
->gimple_df
->escaped
;
6660 pt
= gimple_call_clobber_set (stmt
);
6661 if (gimple_call_flags (stmt
) & (ECF_CONST
|ECF_PURE
|ECF_NOVOPS
))
6662 memset (pt
, 0, sizeof (struct pt_solution
));
6663 else if ((vi
= lookup_call_clobber_vi (stmt
)) != NULL
)
6665 find_what_var_points_to (vi
, pt
);
6666 /* Escaped (and thus nonlocal) variables are always
6667 implicitly clobbered by calls. */
6668 /* ??? ESCAPED can be empty even though NONLOCAL
6675 /* If there is nothing special about this call then
6676 we have made everything that is used also escape. */
6677 *pt
= cfun
->gimple_df
->escaped
;
6683 timevar_pop (TV_TREE_PTA
);
6687 /* Delete created points-to sets. */
6690 delete_points_to_sets (void)
6694 htab_delete (shared_bitmap_table
);
6695 if (dump_file
&& (dump_flags
& TDF_STATS
))
6696 fprintf (dump_file
, "Points to sets created:%d\n",
6697 stats
.points_to_sets_created
);
6699 pointer_map_destroy (vi_for_tree
);
6700 pointer_map_destroy (call_stmt_vars
);
6701 bitmap_obstack_release (&pta_obstack
);
6702 VEC_free (constraint_t
, heap
, constraints
);
6704 for (i
= 0; i
< graph
->size
; i
++)
6705 VEC_free (constraint_t
, heap
, graph
->complex[i
]);
6706 free (graph
->complex);
6709 free (graph
->succs
);
6711 free (graph
->pe_rep
);
6712 free (graph
->indirect_cycles
);
6715 VEC_free (varinfo_t
, heap
, varmap
);
6716 free_alloc_pool (variable_info_pool
);
6717 free_alloc_pool (constraint_pool
);
6719 obstack_free (&fake_var_decl_obstack
, NULL
);
6723 /* Compute points-to information for every SSA_NAME pointer in the
6724 current function and compute the transitive closure of escaped
6725 variables to re-initialize the call-clobber states of local variables. */
6728 compute_may_aliases (void)
6730 if (cfun
->gimple_df
->ipa_pta
)
6734 fprintf (dump_file
, "\nNot re-computing points-to information "
6735 "because IPA points-to information is available.\n\n");
6737 /* But still dump what we have remaining it. */
6738 dump_alias_info (dump_file
);
6740 if (dump_flags
& TDF_DETAILS
)
6741 dump_referenced_vars (dump_file
);
6747 /* For each pointer P_i, determine the sets of variables that P_i may
6748 point-to. Compute the reachability set of escaped and call-used
6750 compute_points_to_sets ();
6752 /* Debugging dumps. */
6755 dump_alias_info (dump_file
);
6757 if (dump_flags
& TDF_DETAILS
)
6758 dump_referenced_vars (dump_file
);
6761 /* Deallocate memory used by aliasing data structures and the internal
6762 points-to solution. */
6763 delete_points_to_sets ();
6765 gcc_assert (!need_ssa_update_p (cfun
));
6771 gate_tree_pta (void)
6773 return flag_tree_pta
;
6776 /* A dummy pass to cause points-to information to be computed via
6777 TODO_rebuild_alias. */
6779 struct gimple_opt_pass pass_build_alias
=
6784 gate_tree_pta
, /* gate */
6788 0, /* static_pass_number */
6789 TV_NONE
, /* tv_id */
6790 PROP_cfg
| PROP_ssa
, /* properties_required */
6791 0, /* properties_provided */
6792 0, /* properties_destroyed */
6793 0, /* todo_flags_start */
6794 TODO_rebuild_alias
/* todo_flags_finish */
6798 /* A dummy pass to cause points-to information to be computed via
6799 TODO_rebuild_alias. */
6801 struct gimple_opt_pass pass_build_ealias
=
6805 "ealias", /* name */
6806 gate_tree_pta
, /* gate */
6810 0, /* static_pass_number */
6811 TV_NONE
, /* tv_id */
6812 PROP_cfg
| PROP_ssa
, /* properties_required */
6813 0, /* properties_provided */
6814 0, /* properties_destroyed */
6815 0, /* todo_flags_start */
6816 TODO_rebuild_alias
/* todo_flags_finish */
6821 /* Return true if we should execute IPA PTA. */
6827 /* Don't bother doing anything if the program has errors. */
6831 /* IPA PTA solutions for ESCAPED. */
6832 struct pt_solution ipa_escaped_pt
6833 = { true, false, false, false, false, false, NULL
};
6835 /* Associate node with varinfo DATA. Worker for
6836 cgraph_for_node_and_aliases. */
6838 associate_varinfo_to_alias (struct cgraph_node
*node
, void *data
)
6840 if (node
->alias
|| node
->thunk
.thunk_p
)
6841 insert_vi_for_tree (node
->symbol
.decl
, (varinfo_t
)data
);
6845 /* Execute the driver for IPA PTA. */
6847 ipa_pta_execute (void)
6849 struct cgraph_node
*node
;
6850 struct varpool_node
*var
;
6857 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
6859 dump_symtab (dump_file
);
6860 fprintf (dump_file
, "\n");
6863 /* Build the constraints. */
6864 FOR_EACH_DEFINED_FUNCTION (node
)
6867 /* Nodes without a body are not interesting. Especially do not
6868 visit clones at this point for now - we get duplicate decls
6869 there for inline clones at least. */
6870 if (!cgraph_function_with_gimple_body_p (node
))
6873 gcc_assert (!node
->clone_of
);
6875 vi
= create_function_info_for (node
->symbol
.decl
,
6876 alias_get_name (node
->symbol
.decl
));
6877 cgraph_for_node_and_aliases (node
, associate_varinfo_to_alias
, vi
, true);
6880 /* Create constraints for global variables and their initializers. */
6881 FOR_EACH_VARIABLE (var
)
6886 get_vi_for_tree (var
->symbol
.decl
);
6892 "Generating constraints for global initializers\n\n");
6893 dump_constraints (dump_file
, 0);
6894 fprintf (dump_file
, "\n");
6896 from
= VEC_length (constraint_t
, constraints
);
6898 FOR_EACH_DEFINED_FUNCTION (node
)
6900 struct function
*func
;
6904 /* Nodes without a body are not interesting. */
6905 if (!cgraph_function_with_gimple_body_p (node
))
6911 "Generating constraints for %s", cgraph_node_name (node
));
6912 if (DECL_ASSEMBLER_NAME_SET_P (node
->symbol
.decl
))
6913 fprintf (dump_file
, " (%s)",
6915 (DECL_ASSEMBLER_NAME (node
->symbol
.decl
)));
6916 fprintf (dump_file
, "\n");
6919 func
= DECL_STRUCT_FUNCTION (node
->symbol
.decl
);
6920 old_func_decl
= current_function_decl
;
6922 current_function_decl
= node
->symbol
.decl
;
6924 /* For externally visible or attribute used annotated functions use
6925 local constraints for their arguments.
6926 For local functions we see all callers and thus do not need initial
6927 constraints for parameters. */
6928 if (node
->symbol
.used_from_other_partition
6929 || node
->symbol
.externally_visible
6930 || node
->symbol
.force_output
)
6932 intra_create_variable_infos ();
6934 /* We also need to make function return values escape. Nothing
6935 escapes by returning from main though. */
6936 if (!MAIN_NAME_P (DECL_NAME (node
->symbol
.decl
)))
6939 fi
= lookup_vi_for_tree (node
->symbol
.decl
);
6940 rvi
= first_vi_for_offset (fi
, fi_result
);
6941 if (rvi
&& rvi
->offset
== fi_result
)
6943 struct constraint_expr includes
;
6944 struct constraint_expr var
;
6945 includes
.var
= escaped_id
;
6946 includes
.offset
= 0;
6947 includes
.type
= SCALAR
;
6951 process_constraint (new_constraint (includes
, var
));
6956 /* Build constriants for the function body. */
6957 FOR_EACH_BB_FN (bb
, func
)
6959 gimple_stmt_iterator gsi
;
6961 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
);
6964 gimple phi
= gsi_stmt (gsi
);
6966 if (is_gimple_reg (gimple_phi_result (phi
)))
6967 find_func_aliases (phi
);
6970 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
6972 gimple stmt
= gsi_stmt (gsi
);
6974 find_func_aliases (stmt
);
6975 find_func_clobbers (stmt
);
6979 current_function_decl
= old_func_decl
;
6984 fprintf (dump_file
, "\n");
6985 dump_constraints (dump_file
, from
);
6986 fprintf (dump_file
, "\n");
6988 from
= VEC_length (constraint_t
, constraints
);
6991 /* From the constraints compute the points-to sets. */
6992 solve_constraints ();
6994 /* Compute the global points-to sets for ESCAPED.
6995 ??? Note that the computed escape set is not correct
6996 for the whole unit as we fail to consider graph edges to
6997 externally visible functions. */
6998 find_what_var_points_to (get_varinfo (escaped_id
), &ipa_escaped_pt
);
7000 /* Make sure the ESCAPED solution (which is used as placeholder in
7001 other solutions) does not reference itself. This simplifies
7002 points-to solution queries. */
7003 ipa_escaped_pt
.ipa_escaped
= 0;
7005 /* Assign the points-to sets to the SSA names in the unit. */
7006 FOR_EACH_DEFINED_FUNCTION (node
)
7009 struct function
*fn
;
7013 struct pt_solution uses
, clobbers
;
7014 struct cgraph_edge
*e
;
7016 /* Nodes without a body are not interesting. */
7017 if (!cgraph_function_with_gimple_body_p (node
))
7020 fn
= DECL_STRUCT_FUNCTION (node
->symbol
.decl
);
7022 /* Compute the points-to sets for pointer SSA_NAMEs. */
7023 FOR_EACH_VEC_ELT (tree
, fn
->gimple_df
->ssa_names
, i
, ptr
)
7026 && POINTER_TYPE_P (TREE_TYPE (ptr
)))
7027 find_what_p_points_to (ptr
);
7030 /* Compute the call-use and call-clobber sets for all direct calls. */
7031 fi
= lookup_vi_for_tree (node
->symbol
.decl
);
7032 gcc_assert (fi
->is_fn_info
);
7033 find_what_var_points_to (first_vi_for_offset (fi
, fi_clobbers
),
7035 find_what_var_points_to (first_vi_for_offset (fi
, fi_uses
), &uses
);
7036 for (e
= node
->callers
; e
; e
= e
->next_caller
)
7041 *gimple_call_clobber_set (e
->call_stmt
) = clobbers
;
7042 *gimple_call_use_set (e
->call_stmt
) = uses
;
7045 /* Compute the call-use and call-clobber sets for indirect calls
7046 and calls to external functions. */
7047 FOR_EACH_BB_FN (bb
, fn
)
7049 gimple_stmt_iterator gsi
;
7051 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
7053 gimple stmt
= gsi_stmt (gsi
);
7054 struct pt_solution
*pt
;
7058 if (!is_gimple_call (stmt
))
7061 /* Handle direct calls to external functions. */
7062 decl
= gimple_call_fndecl (stmt
);
7064 && (!(fi
= lookup_vi_for_tree (decl
))
7065 || !fi
->is_fn_info
))
7067 pt
= gimple_call_use_set (stmt
);
7068 if (gimple_call_flags (stmt
) & ECF_CONST
)
7069 memset (pt
, 0, sizeof (struct pt_solution
));
7070 else if ((vi
= lookup_call_use_vi (stmt
)) != NULL
)
7072 find_what_var_points_to (vi
, pt
);
7073 /* Escaped (and thus nonlocal) variables are always
7074 implicitly used by calls. */
7075 /* ??? ESCAPED can be empty even though NONLOCAL
7078 pt
->ipa_escaped
= 1;
7082 /* If there is nothing special about this call then
7083 we have made everything that is used also escape. */
7084 *pt
= ipa_escaped_pt
;
7088 pt
= gimple_call_clobber_set (stmt
);
7089 if (gimple_call_flags (stmt
) & (ECF_CONST
|ECF_PURE
|ECF_NOVOPS
))
7090 memset (pt
, 0, sizeof (struct pt_solution
));
7091 else if ((vi
= lookup_call_clobber_vi (stmt
)) != NULL
)
7093 find_what_var_points_to (vi
, pt
);
7094 /* Escaped (and thus nonlocal) variables are always
7095 implicitly clobbered by calls. */
7096 /* ??? ESCAPED can be empty even though NONLOCAL
7099 pt
->ipa_escaped
= 1;
7103 /* If there is nothing special about this call then
7104 we have made everything that is used also escape. */
7105 *pt
= ipa_escaped_pt
;
7110 /* Handle indirect calls. */
7112 && (fi
= get_fi_for_callee (stmt
)))
7114 /* We need to accumulate all clobbers/uses of all possible
7116 fi
= get_varinfo (find (fi
->id
));
7117 /* If we cannot constrain the set of functions we'll end up
7118 calling we end up using/clobbering everything. */
7119 if (bitmap_bit_p (fi
->solution
, anything_id
)
7120 || bitmap_bit_p (fi
->solution
, nonlocal_id
)
7121 || bitmap_bit_p (fi
->solution
, escaped_id
))
7123 pt_solution_reset (gimple_call_clobber_set (stmt
));
7124 pt_solution_reset (gimple_call_use_set (stmt
));
7130 struct pt_solution
*uses
, *clobbers
;
7132 uses
= gimple_call_use_set (stmt
);
7133 clobbers
= gimple_call_clobber_set (stmt
);
7134 memset (uses
, 0, sizeof (struct pt_solution
));
7135 memset (clobbers
, 0, sizeof (struct pt_solution
));
7136 EXECUTE_IF_SET_IN_BITMAP (fi
->solution
, 0, i
, bi
)
7138 struct pt_solution sol
;
7140 vi
= get_varinfo (i
);
7141 if (!vi
->is_fn_info
)
7143 /* ??? We could be more precise here? */
7145 uses
->ipa_escaped
= 1;
7146 clobbers
->nonlocal
= 1;
7147 clobbers
->ipa_escaped
= 1;
7151 if (!uses
->anything
)
7153 find_what_var_points_to
7154 (first_vi_for_offset (vi
, fi_uses
), &sol
);
7155 pt_solution_ior_into (uses
, &sol
);
7157 if (!clobbers
->anything
)
7159 find_what_var_points_to
7160 (first_vi_for_offset (vi
, fi_clobbers
), &sol
);
7161 pt_solution_ior_into (clobbers
, &sol
);
7169 fn
->gimple_df
->ipa_pta
= true;
7172 delete_points_to_sets ();
7179 struct simple_ipa_opt_pass pass_ipa_pta
=
7184 gate_ipa_pta
, /* gate */
7185 ipa_pta_execute
, /* execute */
7188 0, /* static_pass_number */
7189 TV_IPA_PTA
, /* tv_id */
7190 0, /* properties_required */
7191 0, /* properties_provided */
7192 0, /* properties_destroyed */
7193 0, /* todo_flags_start */
7194 TODO_update_ssa
/* todo_flags_finish */