1 /* Tree based points-to analysis
2 Copyright (C) 2005-2015 Free Software Foundation, Inc.
3 Contributed by Daniel Berlin <dberlin@dberlin.org>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
31 #include "fold-const.h"
32 #include "stor-layout.h"
34 #include "internal-fn.h"
35 #include "gimple-iterator.h"
37 #include "tree-into-ssa.h"
38 #include "insn-config.h"
47 #include "tree-inline.h"
48 #include "diagnostic-core.h"
49 #include "tree-pass.h"
50 #include "alloc-pool.h"
51 #include "splay-tree.h"
53 #include "tree-pretty-print.h"
54 #include "gimple-walk.h"
56 /* The idea behind this analyzer is to generate set constraints from the
57 program, then solve the resulting constraints in order to generate the
60 Set constraints are a way of modeling program analysis problems that
61 involve sets. They consist of an inclusion constraint language,
62 describing the variables (each variable is a set) and operations that
63 are involved on the variables, and a set of rules that derive facts
64 from these operations. To solve a system of set constraints, you derive
65 all possible facts under the rules, which gives you the correct sets
68 See "Efficient Field-sensitive pointer analysis for C" by "David
69 J. Pearce and Paul H. J. Kelly and Chris Hankin, at
70 http://citeseer.ist.psu.edu/pearce04efficient.html
72 Also see "Ultra-fast Aliasing Analysis using CLA: A Million Lines
73 of C Code in a Second" by ""Nevin Heintze and Olivier Tardieu" at
74 http://citeseer.ist.psu.edu/heintze01ultrafast.html
76 There are three types of real constraint expressions, DEREF,
77 ADDRESSOF, and SCALAR. Each constraint expression consists
78 of a constraint type, a variable, and an offset.
80 SCALAR is a constraint expression type used to represent x, whether
81 it appears on the LHS or the RHS of a statement.
82 DEREF is a constraint expression type used to represent *x, whether
83 it appears on the LHS or the RHS of a statement.
84 ADDRESSOF is a constraint expression used to represent &x, whether
85 it appears on the LHS or the RHS of a statement.
87 Each pointer variable in the program is assigned an integer id, and
88 each field of a structure variable is assigned an integer id as well.
90 Structure variables are linked to their list of fields through a "next
91 field" in each variable that points to the next field in offset
93 Each variable for a structure field has
95 1. "size", that tells the size in bits of that field.
96 2. "fullsize, that tells the size in bits of the entire structure.
97 3. "offset", that tells the offset in bits from the beginning of the
98 structure to this field.
110 foo.a -> id 1, size 32, offset 0, fullsize 64, next foo.b
111 foo.b -> id 2, size 32, offset 32, fullsize 64, next NULL
112 bar -> id 3, size 32, offset 0, fullsize 32, next NULL
115 In order to solve the system of set constraints, the following is
118 1. Each constraint variable x has a solution set associated with it,
121 2. Constraints are separated into direct, copy, and complex.
122 Direct constraints are ADDRESSOF constraints that require no extra
123 processing, such as P = &Q
124 Copy constraints are those of the form P = Q.
125 Complex constraints are all the constraints involving dereferences
126 and offsets (including offsetted copies).
128 3. All direct constraints of the form P = &Q are processed, such
129 that Q is added to Sol(P)
131 4. All complex constraints for a given constraint variable are stored in a
132 linked list attached to that variable's node.
134 5. A directed graph is built out of the copy constraints. Each
135 constraint variable is a node in the graph, and an edge from
136 Q to P is added for each copy constraint of the form P = Q
138 6. The graph is then walked, and solution sets are
139 propagated along the copy edges, such that an edge from Q to P
140 causes Sol(P) <- Sol(P) union Sol(Q).
142 7. As we visit each node, all complex constraints associated with
143 that node are processed by adding appropriate copy edges to the graph, or the
144 appropriate variables to the solution set.
146 8. The process of walking the graph is iterated until no solution
149 Prior to walking the graph in steps 6 and 7, We perform static
150 cycle elimination on the constraint graph, as well
151 as off-line variable substitution.
153 TODO: Adding offsets to pointer-to-structures can be handled (IE not punted
154 on and turned into anything), but isn't. You can just see what offset
155 inside the pointed-to struct it's going to access.
157 TODO: Constant bounded arrays can be handled as if they were structs of the
158 same number of elements.
160 TODO: Modeling heap and incoming pointers becomes much better if we
161 add fields to them as we discover them, which we could do.
163 TODO: We could handle unions, but to be honest, it's probably not
164 worth the pain or slowdown. */
166 /* IPA-PTA optimizations possible.
168 When the indirect function called is ANYTHING we can add disambiguation
169 based on the function signatures (or simply the parameter count which
170 is the varinfo size). We also do not need to consider functions that
171 do not have their address taken.
173 The is_global_var bit which marks escape points is overly conservative
174 in IPA mode. Split it to is_escape_point and is_global_var - only
175 externally visible globals are escape points in IPA mode. This is
176 also needed to fix the pt_solution_includes_global predicate
177 (and thus ptr_deref_may_alias_global_p).
179 The way we introduce DECL_PT_UID to avoid fixing up all points-to
180 sets in the translation unit when we copy a DECL during inlining
181 pessimizes precision. The advantage is that the DECL_PT_UID keeps
182 compile-time and memory usage overhead low - the points-to sets
183 do not grow or get unshared as they would during a fixup phase.
184 An alternative solution is to delay IPA PTA until after all
185 inlining transformations have been applied.
187 The way we propagate clobber/use information isn't optimized.
188 It should use a new complex constraint that properly filters
189 out local variables of the callee (though that would make
190 the sets invalid after inlining). OTOH we might as well
191 admit defeat to WHOPR and simply do all the clobber/use analysis
192 and propagation after PTA finished but before we threw away
193 points-to information for memory variables. WHOPR and PTA
194 do not play along well anyway - the whole constraint solving
195 would need to be done in WPA phase and it will be very interesting
196 to apply the results to local SSA names during LTRANS phase.
198 We probably should compute a per-function unit-ESCAPE solution
199 propagating it simply like the clobber / uses solutions. The
200 solution can go alongside the non-IPA espaced solution and be
201 used to query which vars escape the unit through a function.
203 We never put function decls in points-to sets so we do not
204 keep the set of called functions for indirect calls.
206 And probably more. */
208 static bool use_field_sensitive
= true;
209 static int in_ipa_mode
= 0;
211 /* Used for predecessor bitmaps. */
212 static bitmap_obstack predbitmap_obstack
;
214 /* Used for points-to sets. */
215 static bitmap_obstack pta_obstack
;
217 /* Used for oldsolution members of variables. */
218 static bitmap_obstack oldpta_obstack
;
220 /* Used for per-solver-iteration bitmaps. */
221 static bitmap_obstack iteration_obstack
;
223 static unsigned int create_variable_info_for (tree
, const char *);
224 typedef struct constraint_graph
*constraint_graph_t
;
225 static void unify_nodes (constraint_graph_t
, unsigned int, unsigned int, bool);
228 typedef struct constraint
*constraint_t
;
231 #define EXECUTE_IF_IN_NONNULL_BITMAP(a, b, c, d) \
233 EXECUTE_IF_SET_IN_BITMAP (a, b, c, d)
235 static struct constraint_stats
237 unsigned int total_vars
;
238 unsigned int nonpointer_vars
;
239 unsigned int unified_vars_static
;
240 unsigned int unified_vars_dynamic
;
241 unsigned int iterations
;
242 unsigned int num_edges
;
243 unsigned int num_implicit_edges
;
244 unsigned int points_to_sets_created
;
249 /* ID of this variable */
252 /* True if this is a variable created by the constraint analysis, such as
253 heap variables and constraints we had to break up. */
254 unsigned int is_artificial_var
: 1;
256 /* True if this is a special variable whose solution set should not be
258 unsigned int is_special_var
: 1;
260 /* True for variables whose size is not known or variable. */
261 unsigned int is_unknown_size_var
: 1;
263 /* True for (sub-)fields that represent a whole variable. */
264 unsigned int is_full_var
: 1;
266 /* True if this is a heap variable. */
267 unsigned int is_heap_var
: 1;
269 /* True if this field may contain pointers. */
270 unsigned int may_have_pointers
: 1;
272 /* True if this field has only restrict qualified pointers. */
273 unsigned int only_restrict_pointers
: 1;
275 /* True if this represents a heap var created for a restrict qualified
277 unsigned int is_restrict_var
: 1;
279 /* True if this represents a global variable. */
280 unsigned int is_global_var
: 1;
282 /* True if this represents a IPA function info. */
283 unsigned int is_fn_info
: 1;
285 /* ??? Store somewhere better. */
288 /* The ID of the variable for the next field in this structure
289 or zero for the last field in this structure. */
292 /* The ID of the variable for the first field in this structure. */
295 /* Offset of this variable, in bits, from the base variable */
296 unsigned HOST_WIDE_INT offset
;
298 /* Size of the variable, in bits. */
299 unsigned HOST_WIDE_INT size
;
301 /* Full size of the base variable, in bits. */
302 unsigned HOST_WIDE_INT fullsize
;
304 /* Name of this variable */
307 /* Tree that this variable is associated with. */
310 /* Points-to set for this variable. */
313 /* Old points-to set for this variable. */
316 typedef struct variable_info
*varinfo_t
;
318 static varinfo_t
first_vi_for_offset (varinfo_t
, unsigned HOST_WIDE_INT
);
319 static varinfo_t
first_or_preceding_vi_for_offset (varinfo_t
,
320 unsigned HOST_WIDE_INT
);
321 static varinfo_t
lookup_vi_for_tree (tree
);
322 static inline bool type_can_have_subvars (const_tree
);
324 /* Pool of variable info structures. */
325 static object_allocator
<variable_info
> variable_info_pool
326 ("Variable info pool", 30);
328 /* Map varinfo to final pt_solution. */
329 static hash_map
<varinfo_t
, pt_solution
*> *final_solutions
;
330 struct obstack final_solutions_obstack
;
332 /* Table of variable info structures for constraint variables.
333 Indexed directly by variable info id. */
334 static vec
<varinfo_t
> varmap
;
336 /* Return the varmap element N */
338 static inline varinfo_t
339 get_varinfo (unsigned int n
)
344 /* Return the next variable in the list of sub-variables of VI
345 or NULL if VI is the last sub-variable. */
347 static inline varinfo_t
348 vi_next (varinfo_t vi
)
350 return get_varinfo (vi
->next
);
353 /* Static IDs for the special variables. Variable ID zero is unused
354 and used as terminator for the sub-variable chain. */
355 enum { nothing_id
= 1, anything_id
= 2, string_id
= 3,
356 escaped_id
= 4, nonlocal_id
= 5,
357 storedanything_id
= 6, integer_id
= 7 };
359 /* Return a new variable info structure consisting for a variable
360 named NAME, and using constraint graph node NODE. Append it
361 to the vector of variable info structures. */
364 new_var_info (tree t
, const char *name
)
366 unsigned index
= varmap
.length ();
367 varinfo_t ret
= variable_info_pool
.allocate ();
372 /* Vars without decl are artificial and do not have sub-variables. */
373 ret
->is_artificial_var
= (t
== NULL_TREE
);
374 ret
->is_special_var
= false;
375 ret
->is_unknown_size_var
= false;
376 ret
->is_full_var
= (t
== NULL_TREE
);
377 ret
->is_heap_var
= false;
378 ret
->may_have_pointers
= true;
379 ret
->only_restrict_pointers
= false;
380 ret
->is_restrict_var
= false;
382 ret
->is_global_var
= (t
== NULL_TREE
);
383 ret
->is_fn_info
= false;
385 ret
->is_global_var
= (is_global_var (t
)
386 /* We have to treat even local register variables
388 || (TREE_CODE (t
) == VAR_DECL
389 && DECL_HARD_REGISTER (t
)));
390 ret
->solution
= BITMAP_ALLOC (&pta_obstack
);
391 ret
->oldsolution
= NULL
;
397 varmap
.safe_push (ret
);
403 /* A map mapping call statements to per-stmt variables for uses
404 and clobbers specific to the call. */
405 static hash_map
<gimple
, varinfo_t
> *call_stmt_vars
;
407 /* Lookup or create the variable for the call statement CALL. */
410 get_call_vi (gcall
*call
)
415 varinfo_t
*slot_p
= &call_stmt_vars
->get_or_insert (call
, &existed
);
419 vi
= new_var_info (NULL_TREE
, "CALLUSED");
423 vi
->is_full_var
= true;
425 vi2
= new_var_info (NULL_TREE
, "CALLCLOBBERED");
429 vi2
->is_full_var
= true;
437 /* Lookup the variable for the call statement CALL representing
438 the uses. Returns NULL if there is nothing special about this call. */
441 lookup_call_use_vi (gcall
*call
)
443 varinfo_t
*slot_p
= call_stmt_vars
->get (call
);
450 /* Lookup the variable for the call statement CALL representing
451 the clobbers. Returns NULL if there is nothing special about this call. */
454 lookup_call_clobber_vi (gcall
*call
)
456 varinfo_t uses
= lookup_call_use_vi (call
);
460 return vi_next (uses
);
463 /* Lookup or create the variable for the call statement CALL representing
467 get_call_use_vi (gcall
*call
)
469 return get_call_vi (call
);
472 /* Lookup or create the variable for the call statement CALL representing
475 static varinfo_t ATTRIBUTE_UNUSED
476 get_call_clobber_vi (gcall
*call
)
478 return vi_next (get_call_vi (call
));
482 enum constraint_expr_type
{SCALAR
, DEREF
, ADDRESSOF
};
484 /* An expression that appears in a constraint. */
486 struct constraint_expr
488 /* Constraint type. */
489 constraint_expr_type type
;
491 /* Variable we are referring to in the constraint. */
494 /* Offset, in bits, of this constraint from the beginning of
495 variables it ends up referring to.
497 IOW, in a deref constraint, we would deref, get the result set,
498 then add OFFSET to each member. */
499 HOST_WIDE_INT offset
;
502 /* Use 0x8000... as special unknown offset. */
503 #define UNKNOWN_OFFSET HOST_WIDE_INT_MIN
505 typedef struct constraint_expr ce_s
;
506 static void get_constraint_for_1 (tree
, vec
<ce_s
> *, bool, bool);
507 static void get_constraint_for (tree
, vec
<ce_s
> *);
508 static void get_constraint_for_rhs (tree
, vec
<ce_s
> *);
509 static void do_deref (vec
<ce_s
> *);
511 /* Our set constraints are made up of two constraint expressions, one
514 As described in the introduction, our set constraints each represent an
515 operation between set valued variables.
519 struct constraint_expr lhs
;
520 struct constraint_expr rhs
;
523 /* List of constraints that we use to build the constraint graph from. */
525 static vec
<constraint_t
> constraints
;
526 static object_allocator
<constraint
> constraint_pool ("Constraint pool", 30);
528 /* The constraint graph is represented as an array of bitmaps
529 containing successor nodes. */
531 struct constraint_graph
533 /* Size of this graph, which may be different than the number of
534 nodes in the variable map. */
537 /* Explicit successors of each node. */
540 /* Implicit predecessors of each node (Used for variable
542 bitmap
*implicit_preds
;
544 /* Explicit predecessors of each node (Used for variable substitution). */
547 /* Indirect cycle representatives, or -1 if the node has no indirect
549 int *indirect_cycles
;
551 /* Representative node for a node. rep[a] == a unless the node has
555 /* Equivalence class representative for a label. This is used for
556 variable substitution. */
559 /* Pointer equivalence label for a node. All nodes with the same
560 pointer equivalence label can be unified together at some point
561 (either during constraint optimization or after the constraint
565 /* Pointer equivalence representative for a label. This is used to
566 handle nodes that are pointer equivalent but not location
567 equivalent. We can unite these once the addressof constraints
568 are transformed into initial points-to sets. */
571 /* Pointer equivalence label for each node, used during variable
573 unsigned int *pointer_label
;
575 /* Location equivalence label for each node, used during location
576 equivalence finding. */
577 unsigned int *loc_label
;
579 /* Pointed-by set for each node, used during location equivalence
580 finding. This is pointed-by rather than pointed-to, because it
581 is constructed using the predecessor graph. */
584 /* Points to sets for pointer equivalence. This is *not* the actual
585 points-to sets for nodes. */
588 /* Bitmap of nodes where the bit is set if the node is a direct
589 node. Used for variable substitution. */
590 sbitmap direct_nodes
;
592 /* Bitmap of nodes where the bit is set if the node is address
593 taken. Used for variable substitution. */
594 bitmap address_taken
;
596 /* Vector of complex constraints for each graph node. Complex
597 constraints are those involving dereferences or offsets that are
599 vec
<constraint_t
> *complex;
602 static constraint_graph_t graph
;
604 /* During variable substitution and the offline version of indirect
605 cycle finding, we create nodes to represent dereferences and
606 address taken constraints. These represent where these start and
608 #define FIRST_REF_NODE (varmap).length ()
609 #define LAST_REF_NODE (FIRST_REF_NODE + (FIRST_REF_NODE - 1))
611 /* Return the representative node for NODE, if NODE has been unioned
613 This function performs path compression along the way to finding
614 the representative. */
617 find (unsigned int node
)
619 gcc_checking_assert (node
< graph
->size
);
620 if (graph
->rep
[node
] != node
)
621 return graph
->rep
[node
] = find (graph
->rep
[node
]);
625 /* Union the TO and FROM nodes to the TO nodes.
626 Note that at some point in the future, we may want to do
627 union-by-rank, in which case we are going to have to return the
628 node we unified to. */
631 unite (unsigned int to
, unsigned int from
)
633 gcc_checking_assert (to
< graph
->size
&& from
< graph
->size
);
634 if (to
!= from
&& graph
->rep
[from
] != to
)
636 graph
->rep
[from
] = to
;
642 /* Create a new constraint consisting of LHS and RHS expressions. */
645 new_constraint (const struct constraint_expr lhs
,
646 const struct constraint_expr rhs
)
648 constraint_t ret
= constraint_pool
.allocate ();
654 /* Print out constraint C to FILE. */
657 dump_constraint (FILE *file
, constraint_t c
)
659 if (c
->lhs
.type
== ADDRESSOF
)
661 else if (c
->lhs
.type
== DEREF
)
663 fprintf (file
, "%s", get_varinfo (c
->lhs
.var
)->name
);
664 if (c
->lhs
.offset
== UNKNOWN_OFFSET
)
665 fprintf (file
, " + UNKNOWN");
666 else if (c
->lhs
.offset
!= 0)
667 fprintf (file
, " + " HOST_WIDE_INT_PRINT_DEC
, c
->lhs
.offset
);
668 fprintf (file
, " = ");
669 if (c
->rhs
.type
== ADDRESSOF
)
671 else if (c
->rhs
.type
== DEREF
)
673 fprintf (file
, "%s", get_varinfo (c
->rhs
.var
)->name
);
674 if (c
->rhs
.offset
== UNKNOWN_OFFSET
)
675 fprintf (file
, " + UNKNOWN");
676 else if (c
->rhs
.offset
!= 0)
677 fprintf (file
, " + " HOST_WIDE_INT_PRINT_DEC
, c
->rhs
.offset
);
681 void debug_constraint (constraint_t
);
682 void debug_constraints (void);
683 void debug_constraint_graph (void);
684 void debug_solution_for_var (unsigned int);
685 void debug_sa_points_to_info (void);
687 /* Print out constraint C to stderr. */
690 debug_constraint (constraint_t c
)
692 dump_constraint (stderr
, c
);
693 fprintf (stderr
, "\n");
696 /* Print out all constraints to FILE */
699 dump_constraints (FILE *file
, int from
)
703 for (i
= from
; constraints
.iterate (i
, &c
); i
++)
706 dump_constraint (file
, c
);
707 fprintf (file
, "\n");
711 /* Print out all constraints to stderr. */
714 debug_constraints (void)
716 dump_constraints (stderr
, 0);
719 /* Print the constraint graph in dot format. */
722 dump_constraint_graph (FILE *file
)
726 /* Only print the graph if it has already been initialized: */
730 /* Prints the header of the dot file: */
731 fprintf (file
, "strict digraph {\n");
732 fprintf (file
, " node [\n shape = box\n ]\n");
733 fprintf (file
, " edge [\n fontsize = \"12\"\n ]\n");
734 fprintf (file
, "\n // List of nodes and complex constraints in "
735 "the constraint graph:\n");
737 /* The next lines print the nodes in the graph together with the
738 complex constraints attached to them. */
739 for (i
= 1; i
< graph
->size
; i
++)
741 if (i
== FIRST_REF_NODE
)
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 if (graph
->complex[i
].exists ())
753 fprintf (file
, " [label=\"\\N\\n");
754 for (j
= 0; graph
->complex[i
].iterate (j
, &c
); ++j
)
756 dump_constraint (file
, c
);
757 fprintf (file
, "\\l");
759 fprintf (file
, "\"]");
761 fprintf (file
, ";\n");
764 /* Go over the edges. */
765 fprintf (file
, "\n // Edges in the constraint graph:\n");
766 for (i
= 1; i
< graph
->size
; i
++)
772 EXECUTE_IF_IN_NONNULL_BITMAP (graph
->succs
[i
], 0, j
, bi
)
774 unsigned to
= find (j
);
777 if (i
< FIRST_REF_NODE
)
778 fprintf (file
, "\"%s\"", get_varinfo (i
)->name
);
780 fprintf (file
, "\"*%s\"", get_varinfo (i
- FIRST_REF_NODE
)->name
);
781 fprintf (file
, " -> ");
782 if (to
< FIRST_REF_NODE
)
783 fprintf (file
, "\"%s\"", get_varinfo (to
)->name
);
785 fprintf (file
, "\"*%s\"", get_varinfo (to
- FIRST_REF_NODE
)->name
);
786 fprintf (file
, ";\n");
790 /* Prints the tail of the dot file. */
791 fprintf (file
, "}\n");
794 /* Print out the constraint graph to stderr. */
797 debug_constraint_graph (void)
799 dump_constraint_graph (stderr
);
804 The solver is a simple worklist solver, that works on the following
807 sbitmap changed_nodes = all zeroes;
809 For each node that is not already collapsed:
811 set bit in changed nodes
813 while (changed_count > 0)
815 compute topological ordering for constraint graph
817 find and collapse cycles in the constraint graph (updating
818 changed if necessary)
820 for each node (n) in the graph in topological order:
823 Process each complex constraint associated with the node,
824 updating changed if necessary.
826 For each outgoing edge from n, propagate the solution from n to
827 the destination of the edge, updating changed as necessary.
831 /* Return true if two constraint expressions A and B are equal. */
834 constraint_expr_equal (struct constraint_expr a
, struct constraint_expr b
)
836 return a
.type
== b
.type
&& a
.var
== b
.var
&& a
.offset
== b
.offset
;
839 /* Return true if constraint expression A is less than constraint expression
840 B. This is just arbitrary, but consistent, in order to give them an
844 constraint_expr_less (struct constraint_expr a
, struct constraint_expr b
)
846 if (a
.type
== b
.type
)
849 return a
.offset
< b
.offset
;
851 return a
.var
< b
.var
;
854 return a
.type
< b
.type
;
857 /* Return true if constraint A is less than constraint B. This is just
858 arbitrary, but consistent, in order to give them an ordering. */
861 constraint_less (const constraint_t
&a
, const constraint_t
&b
)
863 if (constraint_expr_less (a
->lhs
, b
->lhs
))
865 else if (constraint_expr_less (b
->lhs
, a
->lhs
))
868 return constraint_expr_less (a
->rhs
, b
->rhs
);
871 /* Return true if two constraints A and B are equal. */
874 constraint_equal (struct constraint a
, struct constraint b
)
876 return constraint_expr_equal (a
.lhs
, b
.lhs
)
877 && constraint_expr_equal (a
.rhs
, b
.rhs
);
881 /* Find a constraint LOOKFOR in the sorted constraint vector VEC */
884 constraint_vec_find (vec
<constraint_t
> vec
,
885 struct constraint lookfor
)
893 place
= vec
.lower_bound (&lookfor
, constraint_less
);
894 if (place
>= vec
.length ())
897 if (!constraint_equal (*found
, lookfor
))
902 /* Union two constraint vectors, TO and FROM. Put the result in TO.
903 Returns true of TO set is changed. */
906 constraint_set_union (vec
<constraint_t
> *to
,
907 vec
<constraint_t
> *from
)
911 bool any_change
= false;
913 FOR_EACH_VEC_ELT (*from
, i
, c
)
915 if (constraint_vec_find (*to
, *c
) == NULL
)
917 unsigned int place
= to
->lower_bound (c
, constraint_less
);
918 to
->safe_insert (place
, c
);
925 /* Expands the solution in SET to all sub-fields of variables included. */
928 solution_set_expand (bitmap set
, bitmap
*expanded
)
936 *expanded
= BITMAP_ALLOC (&iteration_obstack
);
938 /* In a first pass expand to the head of the variables we need to
939 add all sub-fields off. This avoids quadratic behavior. */
940 EXECUTE_IF_SET_IN_BITMAP (set
, 0, j
, bi
)
942 varinfo_t v
= get_varinfo (j
);
943 if (v
->is_artificial_var
946 bitmap_set_bit (*expanded
, v
->head
);
949 /* In the second pass now expand all head variables with subfields. */
950 EXECUTE_IF_SET_IN_BITMAP (*expanded
, 0, j
, bi
)
952 varinfo_t v
= get_varinfo (j
);
955 for (v
= vi_next (v
); v
!= NULL
; v
= vi_next (v
))
956 bitmap_set_bit (*expanded
, v
->id
);
959 /* And finally set the rest of the bits from SET. */
960 bitmap_ior_into (*expanded
, set
);
965 /* Union solution sets TO and DELTA, and add INC to each member of DELTA in the
969 set_union_with_increment (bitmap to
, bitmap delta
, HOST_WIDE_INT inc
,
970 bitmap
*expanded_delta
)
972 bool changed
= false;
976 /* If the solution of DELTA contains anything it is good enough to transfer
978 if (bitmap_bit_p (delta
, anything_id
))
979 return bitmap_set_bit (to
, anything_id
);
981 /* If the offset is unknown we have to expand the solution to
983 if (inc
== UNKNOWN_OFFSET
)
985 delta
= solution_set_expand (delta
, expanded_delta
);
986 changed
|= bitmap_ior_into (to
, delta
);
990 /* For non-zero offset union the offsetted solution into the destination. */
991 EXECUTE_IF_SET_IN_BITMAP (delta
, 0, i
, bi
)
993 varinfo_t vi
= get_varinfo (i
);
995 /* If this is a variable with just one field just set its bit
997 if (vi
->is_artificial_var
998 || vi
->is_unknown_size_var
1000 changed
|= bitmap_set_bit (to
, i
);
1003 HOST_WIDE_INT fieldoffset
= vi
->offset
+ inc
;
1004 unsigned HOST_WIDE_INT size
= vi
->size
;
1006 /* If the offset makes the pointer point to before the
1007 variable use offset zero for the field lookup. */
1008 if (fieldoffset
< 0)
1009 vi
= get_varinfo (vi
->head
);
1011 vi
= first_or_preceding_vi_for_offset (vi
, fieldoffset
);
1015 changed
|= bitmap_set_bit (to
, vi
->id
);
1020 /* We have to include all fields that overlap the current field
1024 while (vi
->offset
< fieldoffset
+ size
);
1031 /* Insert constraint C into the list of complex constraints for graph
1035 insert_into_complex (constraint_graph_t graph
,
1036 unsigned int var
, constraint_t c
)
1038 vec
<constraint_t
> complex = graph
->complex[var
];
1039 unsigned int place
= complex.lower_bound (c
, constraint_less
);
1041 /* Only insert constraints that do not already exist. */
1042 if (place
>= complex.length ()
1043 || !constraint_equal (*c
, *complex[place
]))
1044 graph
->complex[var
].safe_insert (place
, c
);
1048 /* Condense two variable nodes into a single variable node, by moving
1049 all associated info from FROM to TO. Returns true if TO node's
1050 constraint set changes after the merge. */
1053 merge_node_constraints (constraint_graph_t graph
, unsigned int to
,
1058 bool any_change
= false;
1060 gcc_checking_assert (find (from
) == to
);
1062 /* Move all complex constraints from src node into to node */
1063 FOR_EACH_VEC_ELT (graph
->complex[from
], i
, c
)
1065 /* In complex constraints for node FROM, we may have either
1066 a = *FROM, and *FROM = a, or an offseted constraint which are
1067 always added to the rhs node's constraints. */
1069 if (c
->rhs
.type
== DEREF
)
1071 else if (c
->lhs
.type
== DEREF
)
1077 any_change
= constraint_set_union (&graph
->complex[to
],
1078 &graph
->complex[from
]);
1079 graph
->complex[from
].release ();
1084 /* Remove edges involving NODE from GRAPH. */
1087 clear_edges_for_node (constraint_graph_t graph
, unsigned int node
)
1089 if (graph
->succs
[node
])
1090 BITMAP_FREE (graph
->succs
[node
]);
1093 /* Merge GRAPH nodes FROM and TO into node TO. */
1096 merge_graph_nodes (constraint_graph_t graph
, unsigned int to
,
1099 if (graph
->indirect_cycles
[from
] != -1)
1101 /* If we have indirect cycles with the from node, and we have
1102 none on the to node, the to node has indirect cycles from the
1103 from node now that they are unified.
1104 If indirect cycles exist on both, unify the nodes that they
1105 are in a cycle with, since we know they are in a cycle with
1107 if (graph
->indirect_cycles
[to
] == -1)
1108 graph
->indirect_cycles
[to
] = graph
->indirect_cycles
[from
];
1111 /* Merge all the successor edges. */
1112 if (graph
->succs
[from
])
1114 if (!graph
->succs
[to
])
1115 graph
->succs
[to
] = BITMAP_ALLOC (&pta_obstack
);
1116 bitmap_ior_into (graph
->succs
[to
],
1117 graph
->succs
[from
]);
1120 clear_edges_for_node (graph
, from
);
1124 /* Add an indirect graph edge to GRAPH, going from TO to FROM if
1125 it doesn't exist in the graph already. */
1128 add_implicit_graph_edge (constraint_graph_t graph
, unsigned int to
,
1134 if (!graph
->implicit_preds
[to
])
1135 graph
->implicit_preds
[to
] = BITMAP_ALLOC (&predbitmap_obstack
);
1137 if (bitmap_set_bit (graph
->implicit_preds
[to
], from
))
1138 stats
.num_implicit_edges
++;
1141 /* Add a predecessor graph edge to GRAPH, going from TO to FROM if
1142 it doesn't exist in the graph already.
1143 Return false if the edge already existed, true otherwise. */
1146 add_pred_graph_edge (constraint_graph_t graph
, unsigned int to
,
1149 if (!graph
->preds
[to
])
1150 graph
->preds
[to
] = BITMAP_ALLOC (&predbitmap_obstack
);
1151 bitmap_set_bit (graph
->preds
[to
], from
);
1154 /* Add a graph edge to GRAPH, going from FROM to TO if
1155 it doesn't exist in the graph already.
1156 Return false if the edge already existed, true otherwise. */
1159 add_graph_edge (constraint_graph_t graph
, unsigned int to
,
1170 if (!graph
->succs
[from
])
1171 graph
->succs
[from
] = BITMAP_ALLOC (&pta_obstack
);
1172 if (bitmap_set_bit (graph
->succs
[from
], to
))
1175 if (to
< FIRST_REF_NODE
&& from
< FIRST_REF_NODE
)
1183 /* Initialize the constraint graph structure to contain SIZE nodes. */
1186 init_graph (unsigned int size
)
1190 graph
= XCNEW (struct constraint_graph
);
1192 graph
->succs
= XCNEWVEC (bitmap
, graph
->size
);
1193 graph
->indirect_cycles
= XNEWVEC (int, graph
->size
);
1194 graph
->rep
= XNEWVEC (unsigned int, graph
->size
);
1195 /* ??? Macros do not support template types with multiple arguments,
1196 so we use a typedef to work around it. */
1197 typedef vec
<constraint_t
> vec_constraint_t_heap
;
1198 graph
->complex = XCNEWVEC (vec_constraint_t_heap
, size
);
1199 graph
->pe
= XCNEWVEC (unsigned int, graph
->size
);
1200 graph
->pe_rep
= XNEWVEC (int, graph
->size
);
1202 for (j
= 0; j
< graph
->size
; j
++)
1205 graph
->pe_rep
[j
] = -1;
1206 graph
->indirect_cycles
[j
] = -1;
1210 /* Build the constraint graph, adding only predecessor edges right now. */
1213 build_pred_graph (void)
1219 graph
->implicit_preds
= XCNEWVEC (bitmap
, graph
->size
);
1220 graph
->preds
= XCNEWVEC (bitmap
, graph
->size
);
1221 graph
->pointer_label
= XCNEWVEC (unsigned int, graph
->size
);
1222 graph
->loc_label
= XCNEWVEC (unsigned int, graph
->size
);
1223 graph
->pointed_by
= XCNEWVEC (bitmap
, graph
->size
);
1224 graph
->points_to
= XCNEWVEC (bitmap
, graph
->size
);
1225 graph
->eq_rep
= XNEWVEC (int, graph
->size
);
1226 graph
->direct_nodes
= sbitmap_alloc (graph
->size
);
1227 graph
->address_taken
= BITMAP_ALLOC (&predbitmap_obstack
);
1228 bitmap_clear (graph
->direct_nodes
);
1230 for (j
= 1; j
< FIRST_REF_NODE
; j
++)
1232 if (!get_varinfo (j
)->is_special_var
)
1233 bitmap_set_bit (graph
->direct_nodes
, j
);
1236 for (j
= 0; j
< graph
->size
; j
++)
1237 graph
->eq_rep
[j
] = -1;
1239 for (j
= 0; j
< varmap
.length (); j
++)
1240 graph
->indirect_cycles
[j
] = -1;
1242 FOR_EACH_VEC_ELT (constraints
, i
, c
)
1244 struct constraint_expr lhs
= c
->lhs
;
1245 struct constraint_expr rhs
= c
->rhs
;
1246 unsigned int lhsvar
= lhs
.var
;
1247 unsigned int rhsvar
= rhs
.var
;
1249 if (lhs
.type
== DEREF
)
1252 if (rhs
.offset
== 0 && lhs
.offset
== 0 && rhs
.type
== SCALAR
)
1253 add_pred_graph_edge (graph
, FIRST_REF_NODE
+ lhsvar
, rhsvar
);
1255 else if (rhs
.type
== DEREF
)
1258 if (rhs
.offset
== 0 && lhs
.offset
== 0 && lhs
.type
== SCALAR
)
1259 add_pred_graph_edge (graph
, lhsvar
, FIRST_REF_NODE
+ rhsvar
);
1261 bitmap_clear_bit (graph
->direct_nodes
, lhsvar
);
1263 else if (rhs
.type
== ADDRESSOF
)
1268 if (graph
->points_to
[lhsvar
] == NULL
)
1269 graph
->points_to
[lhsvar
] = BITMAP_ALLOC (&predbitmap_obstack
);
1270 bitmap_set_bit (graph
->points_to
[lhsvar
], rhsvar
);
1272 if (graph
->pointed_by
[rhsvar
] == NULL
)
1273 graph
->pointed_by
[rhsvar
] = BITMAP_ALLOC (&predbitmap_obstack
);
1274 bitmap_set_bit (graph
->pointed_by
[rhsvar
], lhsvar
);
1276 /* Implicitly, *x = y */
1277 add_implicit_graph_edge (graph
, FIRST_REF_NODE
+ lhsvar
, rhsvar
);
1279 /* All related variables are no longer direct nodes. */
1280 bitmap_clear_bit (graph
->direct_nodes
, rhsvar
);
1281 v
= get_varinfo (rhsvar
);
1282 if (!v
->is_full_var
)
1284 v
= get_varinfo (v
->head
);
1287 bitmap_clear_bit (graph
->direct_nodes
, v
->id
);
1292 bitmap_set_bit (graph
->address_taken
, rhsvar
);
1294 else if (lhsvar
> anything_id
1295 && lhsvar
!= rhsvar
&& lhs
.offset
== 0 && rhs
.offset
== 0)
1298 add_pred_graph_edge (graph
, lhsvar
, rhsvar
);
1299 /* Implicitly, *x = *y */
1300 add_implicit_graph_edge (graph
, FIRST_REF_NODE
+ lhsvar
,
1301 FIRST_REF_NODE
+ rhsvar
);
1303 else if (lhs
.offset
!= 0 || rhs
.offset
!= 0)
1305 if (rhs
.offset
!= 0)
1306 bitmap_clear_bit (graph
->direct_nodes
, lhs
.var
);
1307 else if (lhs
.offset
!= 0)
1308 bitmap_clear_bit (graph
->direct_nodes
, rhs
.var
);
1313 /* Build the constraint graph, adding successor edges. */
1316 build_succ_graph (void)
1321 FOR_EACH_VEC_ELT (constraints
, i
, c
)
1323 struct constraint_expr lhs
;
1324 struct constraint_expr rhs
;
1325 unsigned int lhsvar
;
1326 unsigned int rhsvar
;
1333 lhsvar
= find (lhs
.var
);
1334 rhsvar
= find (rhs
.var
);
1336 if (lhs
.type
== DEREF
)
1338 if (rhs
.offset
== 0 && lhs
.offset
== 0 && rhs
.type
== SCALAR
)
1339 add_graph_edge (graph
, FIRST_REF_NODE
+ lhsvar
, rhsvar
);
1341 else if (rhs
.type
== DEREF
)
1343 if (rhs
.offset
== 0 && lhs
.offset
== 0 && lhs
.type
== SCALAR
)
1344 add_graph_edge (graph
, lhsvar
, FIRST_REF_NODE
+ rhsvar
);
1346 else if (rhs
.type
== ADDRESSOF
)
1349 gcc_checking_assert (find (rhs
.var
) == rhs
.var
);
1350 bitmap_set_bit (get_varinfo (lhsvar
)->solution
, rhsvar
);
1352 else if (lhsvar
> anything_id
1353 && lhsvar
!= rhsvar
&& lhs
.offset
== 0 && rhs
.offset
== 0)
1355 add_graph_edge (graph
, lhsvar
, rhsvar
);
1359 /* Add edges from STOREDANYTHING to all non-direct nodes that can
1360 receive pointers. */
1361 t
= find (storedanything_id
);
1362 for (i
= integer_id
+ 1; i
< FIRST_REF_NODE
; ++i
)
1364 if (!bitmap_bit_p (graph
->direct_nodes
, i
)
1365 && get_varinfo (i
)->may_have_pointers
)
1366 add_graph_edge (graph
, find (i
), t
);
1369 /* Everything stored to ANYTHING also potentially escapes. */
1370 add_graph_edge (graph
, find (escaped_id
), t
);
1374 /* Changed variables on the last iteration. */
1375 static bitmap changed
;
1377 /* Strongly Connected Component visitation info. */
1384 unsigned int *node_mapping
;
1386 vec
<unsigned> scc_stack
;
1390 /* Recursive routine to find strongly connected components in GRAPH.
1391 SI is the SCC info to store the information in, and N is the id of current
1392 graph node we are processing.
1394 This is Tarjan's strongly connected component finding algorithm, as
1395 modified by Nuutila to keep only non-root nodes on the stack.
1396 The algorithm can be found in "On finding the strongly connected
1397 connected components in a directed graph" by Esko Nuutila and Eljas
1398 Soisalon-Soininen, in Information Processing Letters volume 49,
1399 number 1, pages 9-14. */
1402 scc_visit (constraint_graph_t graph
, struct scc_info
*si
, unsigned int n
)
1406 unsigned int my_dfs
;
1408 bitmap_set_bit (si
->visited
, n
);
1409 si
->dfs
[n
] = si
->current_index
++;
1410 my_dfs
= si
->dfs
[n
];
1412 /* Visit all the successors. */
1413 EXECUTE_IF_IN_NONNULL_BITMAP (graph
->succs
[n
], 0, i
, bi
)
1417 if (i
> LAST_REF_NODE
)
1421 if (bitmap_bit_p (si
->deleted
, w
))
1424 if (!bitmap_bit_p (si
->visited
, w
))
1425 scc_visit (graph
, si
, w
);
1427 unsigned int t
= find (w
);
1428 gcc_checking_assert (find (n
) == n
);
1429 if (si
->dfs
[t
] < si
->dfs
[n
])
1430 si
->dfs
[n
] = si
->dfs
[t
];
1433 /* See if any components have been identified. */
1434 if (si
->dfs
[n
] == my_dfs
)
1436 if (si
->scc_stack
.length () > 0
1437 && si
->dfs
[si
->scc_stack
.last ()] >= my_dfs
)
1439 bitmap scc
= BITMAP_ALLOC (NULL
);
1440 unsigned int lowest_node
;
1443 bitmap_set_bit (scc
, n
);
1445 while (si
->scc_stack
.length () != 0
1446 && si
->dfs
[si
->scc_stack
.last ()] >= my_dfs
)
1448 unsigned int w
= si
->scc_stack
.pop ();
1450 bitmap_set_bit (scc
, w
);
1453 lowest_node
= bitmap_first_set_bit (scc
);
1454 gcc_assert (lowest_node
< FIRST_REF_NODE
);
1456 /* Collapse the SCC nodes into a single node, and mark the
1458 EXECUTE_IF_SET_IN_BITMAP (scc
, 0, i
, bi
)
1460 if (i
< FIRST_REF_NODE
)
1462 if (unite (lowest_node
, i
))
1463 unify_nodes (graph
, lowest_node
, i
, false);
1467 unite (lowest_node
, i
);
1468 graph
->indirect_cycles
[i
- FIRST_REF_NODE
] = lowest_node
;
1472 bitmap_set_bit (si
->deleted
, n
);
1475 si
->scc_stack
.safe_push (n
);
1478 /* Unify node FROM into node TO, updating the changed count if
1479 necessary when UPDATE_CHANGED is true. */
1482 unify_nodes (constraint_graph_t graph
, unsigned int to
, unsigned int from
,
1483 bool update_changed
)
1485 gcc_checking_assert (to
!= from
&& find (to
) == to
);
1487 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1488 fprintf (dump_file
, "Unifying %s to %s\n",
1489 get_varinfo (from
)->name
,
1490 get_varinfo (to
)->name
);
1493 stats
.unified_vars_dynamic
++;
1495 stats
.unified_vars_static
++;
1497 merge_graph_nodes (graph
, to
, from
);
1498 if (merge_node_constraints (graph
, to
, from
))
1501 bitmap_set_bit (changed
, to
);
1504 /* Mark TO as changed if FROM was changed. If TO was already marked
1505 as changed, decrease the changed count. */
1508 && bitmap_clear_bit (changed
, from
))
1509 bitmap_set_bit (changed
, to
);
1510 varinfo_t fromvi
= get_varinfo (from
);
1511 if (fromvi
->solution
)
1513 /* If the solution changes because of the merging, we need to mark
1514 the variable as changed. */
1515 varinfo_t tovi
= get_varinfo (to
);
1516 if (bitmap_ior_into (tovi
->solution
, fromvi
->solution
))
1519 bitmap_set_bit (changed
, to
);
1522 BITMAP_FREE (fromvi
->solution
);
1523 if (fromvi
->oldsolution
)
1524 BITMAP_FREE (fromvi
->oldsolution
);
1526 if (stats
.iterations
> 0
1527 && tovi
->oldsolution
)
1528 BITMAP_FREE (tovi
->oldsolution
);
1530 if (graph
->succs
[to
])
1531 bitmap_clear_bit (graph
->succs
[to
], to
);
1534 /* Information needed to compute the topological ordering of a graph. */
1538 /* sbitmap of visited nodes. */
1540 /* Array that stores the topological order of the graph, *in
1542 vec
<unsigned> topo_order
;
1546 /* Initialize and return a topological info structure. */
1548 static struct topo_info
*
1549 init_topo_info (void)
1551 size_t size
= graph
->size
;
1552 struct topo_info
*ti
= XNEW (struct topo_info
);
1553 ti
->visited
= sbitmap_alloc (size
);
1554 bitmap_clear (ti
->visited
);
1555 ti
->topo_order
.create (1);
1560 /* Free the topological sort info pointed to by TI. */
1563 free_topo_info (struct topo_info
*ti
)
1565 sbitmap_free (ti
->visited
);
1566 ti
->topo_order
.release ();
1570 /* Visit the graph in topological order, and store the order in the
1571 topo_info structure. */
1574 topo_visit (constraint_graph_t graph
, struct topo_info
*ti
,
1580 bitmap_set_bit (ti
->visited
, n
);
1582 if (graph
->succs
[n
])
1583 EXECUTE_IF_SET_IN_BITMAP (graph
->succs
[n
], 0, j
, bi
)
1585 if (!bitmap_bit_p (ti
->visited
, j
))
1586 topo_visit (graph
, ti
, j
);
1589 ti
->topo_order
.safe_push (n
);
1592 /* Process a constraint C that represents x = *(y + off), using DELTA as the
1593 starting solution for y. */
1596 do_sd_constraint (constraint_graph_t graph
, constraint_t c
,
1597 bitmap delta
, bitmap
*expanded_delta
)
1599 unsigned int lhs
= c
->lhs
.var
;
1601 bitmap sol
= get_varinfo (lhs
)->solution
;
1604 HOST_WIDE_INT roffset
= c
->rhs
.offset
;
1606 /* Our IL does not allow this. */
1607 gcc_checking_assert (c
->lhs
.offset
== 0);
1609 /* If the solution of Y contains anything it is good enough to transfer
1611 if (bitmap_bit_p (delta
, anything_id
))
1613 flag
|= bitmap_set_bit (sol
, anything_id
);
1617 /* If we do not know at with offset the rhs is dereferenced compute
1618 the reachability set of DELTA, conservatively assuming it is
1619 dereferenced at all valid offsets. */
1620 if (roffset
== UNKNOWN_OFFSET
)
1622 delta
= solution_set_expand (delta
, expanded_delta
);
1623 /* No further offset processing is necessary. */
1627 /* For each variable j in delta (Sol(y)), add
1628 an edge in the graph from j to x, and union Sol(j) into Sol(x). */
1629 EXECUTE_IF_SET_IN_BITMAP (delta
, 0, j
, bi
)
1631 varinfo_t v
= get_varinfo (j
);
1632 HOST_WIDE_INT fieldoffset
= v
->offset
+ roffset
;
1633 unsigned HOST_WIDE_INT size
= v
->size
;
1638 else if (roffset
!= 0)
1640 if (fieldoffset
< 0)
1641 v
= get_varinfo (v
->head
);
1643 v
= first_or_preceding_vi_for_offset (v
, fieldoffset
);
1646 /* We have to include all fields that overlap the current field
1647 shifted by roffset. */
1652 /* Adding edges from the special vars is pointless.
1653 They don't have sets that can change. */
1654 if (get_varinfo (t
)->is_special_var
)
1655 flag
|= bitmap_ior_into (sol
, get_varinfo (t
)->solution
);
1656 /* Merging the solution from ESCAPED needlessly increases
1657 the set. Use ESCAPED as representative instead. */
1658 else if (v
->id
== escaped_id
)
1659 flag
|= bitmap_set_bit (sol
, escaped_id
);
1660 else if (v
->may_have_pointers
1661 && add_graph_edge (graph
, lhs
, t
))
1662 flag
|= bitmap_ior_into (sol
, get_varinfo (t
)->solution
);
1670 while (v
->offset
< fieldoffset
+ size
);
1674 /* If the LHS solution changed, mark the var as changed. */
1677 get_varinfo (lhs
)->solution
= sol
;
1678 bitmap_set_bit (changed
, lhs
);
1682 /* Process a constraint C that represents *(x + off) = y using DELTA
1683 as the starting solution for x. */
1686 do_ds_constraint (constraint_t c
, bitmap delta
, bitmap
*expanded_delta
)
1688 unsigned int rhs
= c
->rhs
.var
;
1689 bitmap sol
= get_varinfo (rhs
)->solution
;
1692 HOST_WIDE_INT loff
= c
->lhs
.offset
;
1693 bool escaped_p
= false;
1695 /* Our IL does not allow this. */
1696 gcc_checking_assert (c
->rhs
.offset
== 0);
1698 /* If the solution of y contains ANYTHING simply use the ANYTHING
1699 solution. This avoids needlessly increasing the points-to sets. */
1700 if (bitmap_bit_p (sol
, anything_id
))
1701 sol
= get_varinfo (find (anything_id
))->solution
;
1703 /* If the solution for x contains ANYTHING we have to merge the
1704 solution of y into all pointer variables which we do via
1706 if (bitmap_bit_p (delta
, anything_id
))
1708 unsigned t
= find (storedanything_id
);
1709 if (add_graph_edge (graph
, t
, rhs
))
1711 if (bitmap_ior_into (get_varinfo (t
)->solution
, sol
))
1712 bitmap_set_bit (changed
, t
);
1717 /* If we do not know at with offset the rhs is dereferenced compute
1718 the reachability set of DELTA, conservatively assuming it is
1719 dereferenced at all valid offsets. */
1720 if (loff
== UNKNOWN_OFFSET
)
1722 delta
= solution_set_expand (delta
, expanded_delta
);
1726 /* For each member j of delta (Sol(x)), add an edge from y to j and
1727 union Sol(y) into Sol(j) */
1728 EXECUTE_IF_SET_IN_BITMAP (delta
, 0, j
, bi
)
1730 varinfo_t v
= get_varinfo (j
);
1732 HOST_WIDE_INT fieldoffset
= v
->offset
+ loff
;
1733 unsigned HOST_WIDE_INT size
= v
->size
;
1739 if (fieldoffset
< 0)
1740 v
= get_varinfo (v
->head
);
1742 v
= first_or_preceding_vi_for_offset (v
, fieldoffset
);
1745 /* We have to include all fields that overlap the current field
1749 if (v
->may_have_pointers
)
1751 /* If v is a global variable then this is an escape point. */
1752 if (v
->is_global_var
1755 t
= find (escaped_id
);
1756 if (add_graph_edge (graph
, t
, rhs
)
1757 && bitmap_ior_into (get_varinfo (t
)->solution
, sol
))
1758 bitmap_set_bit (changed
, t
);
1759 /* Enough to let rhs escape once. */
1763 if (v
->is_special_var
)
1767 if (add_graph_edge (graph
, t
, rhs
)
1768 && bitmap_ior_into (get_varinfo (t
)->solution
, sol
))
1769 bitmap_set_bit (changed
, t
);
1778 while (v
->offset
< fieldoffset
+ size
);
1782 /* Handle a non-simple (simple meaning requires no iteration),
1783 constraint (IE *x = &y, x = *y, *x = y, and x = y with offsets involved). */
1786 do_complex_constraint (constraint_graph_t graph
, constraint_t c
, bitmap delta
,
1787 bitmap
*expanded_delta
)
1789 if (c
->lhs
.type
== DEREF
)
1791 if (c
->rhs
.type
== ADDRESSOF
)
1798 do_ds_constraint (c
, delta
, expanded_delta
);
1801 else if (c
->rhs
.type
== DEREF
)
1804 if (!(get_varinfo (c
->lhs
.var
)->is_special_var
))
1805 do_sd_constraint (graph
, c
, delta
, expanded_delta
);
1812 gcc_checking_assert (c
->rhs
.type
== SCALAR
&& c
->lhs
.type
== SCALAR
1813 && c
->rhs
.offset
!= 0 && c
->lhs
.offset
== 0);
1814 tmp
= get_varinfo (c
->lhs
.var
)->solution
;
1816 flag
= set_union_with_increment (tmp
, delta
, c
->rhs
.offset
,
1820 bitmap_set_bit (changed
, c
->lhs
.var
);
1824 /* Initialize and return a new SCC info structure. */
1826 static struct scc_info
*
1827 init_scc_info (size_t size
)
1829 struct scc_info
*si
= XNEW (struct scc_info
);
1832 si
->current_index
= 0;
1833 si
->visited
= sbitmap_alloc (size
);
1834 bitmap_clear (si
->visited
);
1835 si
->deleted
= sbitmap_alloc (size
);
1836 bitmap_clear (si
->deleted
);
1837 si
->node_mapping
= XNEWVEC (unsigned int, size
);
1838 si
->dfs
= XCNEWVEC (unsigned int, size
);
1840 for (i
= 0; i
< size
; i
++)
1841 si
->node_mapping
[i
] = i
;
1843 si
->scc_stack
.create (1);
1847 /* Free an SCC info structure pointed to by SI */
1850 free_scc_info (struct scc_info
*si
)
1852 sbitmap_free (si
->visited
);
1853 sbitmap_free (si
->deleted
);
1854 free (si
->node_mapping
);
1856 si
->scc_stack
.release ();
1861 /* Find indirect cycles in GRAPH that occur, using strongly connected
1862 components, and note them in the indirect cycles map.
1864 This technique comes from Ben Hardekopf and Calvin Lin,
1865 "It Pays to be Lazy: Fast and Accurate Pointer Analysis for Millions of
1866 Lines of Code", submitted to PLDI 2007. */
1869 find_indirect_cycles (constraint_graph_t graph
)
1872 unsigned int size
= graph
->size
;
1873 struct scc_info
*si
= init_scc_info (size
);
1875 for (i
= 0; i
< MIN (LAST_REF_NODE
, size
); i
++ )
1876 if (!bitmap_bit_p (si
->visited
, i
) && find (i
) == i
)
1877 scc_visit (graph
, si
, i
);
1882 /* Compute a topological ordering for GRAPH, and store the result in the
1883 topo_info structure TI. */
1886 compute_topo_order (constraint_graph_t graph
,
1887 struct topo_info
*ti
)
1890 unsigned int size
= graph
->size
;
1892 for (i
= 0; i
!= size
; ++i
)
1893 if (!bitmap_bit_p (ti
->visited
, i
) && find (i
) == i
)
1894 topo_visit (graph
, ti
, i
);
1897 /* Structure used to for hash value numbering of pointer equivalence
1900 typedef struct equiv_class_label
1903 unsigned int equivalence_class
;
1905 } *equiv_class_label_t
;
1906 typedef const struct equiv_class_label
*const_equiv_class_label_t
;
1908 /* Equiv_class_label hashtable helpers. */
1910 struct equiv_class_hasher
: free_ptr_hash
<equiv_class_label
>
1912 static inline hashval_t
hash (const equiv_class_label
*);
1913 static inline bool equal (const equiv_class_label
*,
1914 const equiv_class_label
*);
1917 /* Hash function for a equiv_class_label_t */
1920 equiv_class_hasher::hash (const equiv_class_label
*ecl
)
1922 return ecl
->hashcode
;
1925 /* Equality function for two equiv_class_label_t's. */
1928 equiv_class_hasher::equal (const equiv_class_label
*eql1
,
1929 const equiv_class_label
*eql2
)
1931 return (eql1
->hashcode
== eql2
->hashcode
1932 && bitmap_equal_p (eql1
->labels
, eql2
->labels
));
1935 /* A hashtable for mapping a bitmap of labels->pointer equivalence
1937 static hash_table
<equiv_class_hasher
> *pointer_equiv_class_table
;
1939 /* A hashtable for mapping a bitmap of labels->location equivalence
1941 static hash_table
<equiv_class_hasher
> *location_equiv_class_table
;
1943 /* Lookup a equivalence class in TABLE by the bitmap of LABELS with
1944 hash HAS it contains. Sets *REF_LABELS to the bitmap LABELS
1945 is equivalent to. */
1947 static equiv_class_label
*
1948 equiv_class_lookup_or_add (hash_table
<equiv_class_hasher
> *table
,
1951 equiv_class_label
**slot
;
1952 equiv_class_label ecl
;
1954 ecl
.labels
= labels
;
1955 ecl
.hashcode
= bitmap_hash (labels
);
1956 slot
= table
->find_slot (&ecl
, INSERT
);
1959 *slot
= XNEW (struct equiv_class_label
);
1960 (*slot
)->labels
= labels
;
1961 (*slot
)->hashcode
= ecl
.hashcode
;
1962 (*slot
)->equivalence_class
= 0;
1968 /* Perform offline variable substitution.
1970 This is a worst case quadratic time way of identifying variables
1971 that must have equivalent points-to sets, including those caused by
1972 static cycles, and single entry subgraphs, in the constraint graph.
1974 The technique is described in "Exploiting Pointer and Location
1975 Equivalence to Optimize Pointer Analysis. In the 14th International
1976 Static Analysis Symposium (SAS), August 2007." It is known as the
1977 "HU" algorithm, and is equivalent to value numbering the collapsed
1978 constraint graph including evaluating unions.
1980 The general method of finding equivalence classes is as follows:
1981 Add fake nodes (REF nodes) and edges for *a = b and a = *b constraints.
1982 Initialize all non-REF nodes to be direct nodes.
1983 For each constraint a = a U {b}, we set pts(a) = pts(a) u {fresh
1985 For each constraint containing the dereference, we also do the same
1988 We then compute SCC's in the graph and unify nodes in the same SCC,
1991 For each non-collapsed node x:
1992 Visit all unvisited explicit incoming edges.
1993 Ignoring all non-pointers, set pts(x) = Union of pts(a) for y
1995 Lookup the equivalence class for pts(x).
1996 If we found one, equivalence_class(x) = found class.
1997 Otherwise, equivalence_class(x) = new class, and new_class is
1998 added to the lookup table.
2000 All direct nodes with the same equivalence class can be replaced
2001 with a single representative node.
2002 All unlabeled nodes (label == 0) are not pointers and all edges
2003 involving them can be eliminated.
2004 We perform these optimizations during rewrite_constraints
2006 In addition to pointer equivalence class finding, we also perform
2007 location equivalence class finding. This is the set of variables
2008 that always appear together in points-to sets. We use this to
2009 compress the size of the points-to sets. */
2011 /* Current maximum pointer equivalence class id. */
2012 static int pointer_equiv_class
;
2014 /* Current maximum location equivalence class id. */
2015 static int location_equiv_class
;
2017 /* Recursive routine to find strongly connected components in GRAPH,
2018 and label it's nodes with DFS numbers. */
2021 condense_visit (constraint_graph_t graph
, struct scc_info
*si
, unsigned int n
)
2025 unsigned int my_dfs
;
2027 gcc_checking_assert (si
->node_mapping
[n
] == n
);
2028 bitmap_set_bit (si
->visited
, n
);
2029 si
->dfs
[n
] = si
->current_index
++;
2030 my_dfs
= si
->dfs
[n
];
2032 /* Visit all the successors. */
2033 EXECUTE_IF_IN_NONNULL_BITMAP (graph
->preds
[n
], 0, i
, bi
)
2035 unsigned int w
= si
->node_mapping
[i
];
2037 if (bitmap_bit_p (si
->deleted
, w
))
2040 if (!bitmap_bit_p (si
->visited
, w
))
2041 condense_visit (graph
, si
, w
);
2043 unsigned int t
= si
->node_mapping
[w
];
2044 gcc_checking_assert (si
->node_mapping
[n
] == n
);
2045 if (si
->dfs
[t
] < si
->dfs
[n
])
2046 si
->dfs
[n
] = si
->dfs
[t
];
2049 /* Visit all the implicit predecessors. */
2050 EXECUTE_IF_IN_NONNULL_BITMAP (graph
->implicit_preds
[n
], 0, i
, bi
)
2052 unsigned int w
= si
->node_mapping
[i
];
2054 if (bitmap_bit_p (si
->deleted
, w
))
2057 if (!bitmap_bit_p (si
->visited
, w
))
2058 condense_visit (graph
, si
, w
);
2060 unsigned int t
= si
->node_mapping
[w
];
2061 gcc_assert (si
->node_mapping
[n
] == n
);
2062 if (si
->dfs
[t
] < si
->dfs
[n
])
2063 si
->dfs
[n
] = si
->dfs
[t
];
2066 /* See if any components have been identified. */
2067 if (si
->dfs
[n
] == my_dfs
)
2069 while (si
->scc_stack
.length () != 0
2070 && si
->dfs
[si
->scc_stack
.last ()] >= my_dfs
)
2072 unsigned int w
= si
->scc_stack
.pop ();
2073 si
->node_mapping
[w
] = n
;
2075 if (!bitmap_bit_p (graph
->direct_nodes
, w
))
2076 bitmap_clear_bit (graph
->direct_nodes
, n
);
2078 /* Unify our nodes. */
2079 if (graph
->preds
[w
])
2081 if (!graph
->preds
[n
])
2082 graph
->preds
[n
] = BITMAP_ALLOC (&predbitmap_obstack
);
2083 bitmap_ior_into (graph
->preds
[n
], graph
->preds
[w
]);
2085 if (graph
->implicit_preds
[w
])
2087 if (!graph
->implicit_preds
[n
])
2088 graph
->implicit_preds
[n
] = BITMAP_ALLOC (&predbitmap_obstack
);
2089 bitmap_ior_into (graph
->implicit_preds
[n
],
2090 graph
->implicit_preds
[w
]);
2092 if (graph
->points_to
[w
])
2094 if (!graph
->points_to
[n
])
2095 graph
->points_to
[n
] = BITMAP_ALLOC (&predbitmap_obstack
);
2096 bitmap_ior_into (graph
->points_to
[n
],
2097 graph
->points_to
[w
]);
2100 bitmap_set_bit (si
->deleted
, n
);
2103 si
->scc_stack
.safe_push (n
);
2106 /* Label pointer equivalences.
2108 This performs a value numbering of the constraint graph to
2109 discover which variables will always have the same points-to sets
2110 under the current set of constraints.
2112 The way it value numbers is to store the set of points-to bits
2113 generated by the constraints and graph edges. This is just used as a
2114 hash and equality comparison. The *actual set of points-to bits* is
2115 completely irrelevant, in that we don't care about being able to
2118 The equality values (currently bitmaps) just have to satisfy a few
2119 constraints, the main ones being:
2120 1. The combining operation must be order independent.
2121 2. The end result of a given set of operations must be unique iff the
2122 combination of input values is unique
2126 label_visit (constraint_graph_t graph
, struct scc_info
*si
, unsigned int n
)
2128 unsigned int i
, first_pred
;
2131 bitmap_set_bit (si
->visited
, n
);
2133 /* Label and union our incoming edges's points to sets. */
2135 EXECUTE_IF_IN_NONNULL_BITMAP (graph
->preds
[n
], 0, i
, bi
)
2137 unsigned int w
= si
->node_mapping
[i
];
2138 if (!bitmap_bit_p (si
->visited
, w
))
2139 label_visit (graph
, si
, w
);
2141 /* Skip unused edges */
2142 if (w
== n
|| graph
->pointer_label
[w
] == 0)
2145 if (graph
->points_to
[w
])
2147 if (!graph
->points_to
[n
])
2149 if (first_pred
== -1U)
2153 graph
->points_to
[n
] = BITMAP_ALLOC (&predbitmap_obstack
);
2154 bitmap_ior (graph
->points_to
[n
],
2155 graph
->points_to
[first_pred
],
2156 graph
->points_to
[w
]);
2160 bitmap_ior_into (graph
->points_to
[n
], graph
->points_to
[w
]);
2164 /* Indirect nodes get fresh variables and a new pointer equiv class. */
2165 if (!bitmap_bit_p (graph
->direct_nodes
, n
))
2167 if (!graph
->points_to
[n
])
2169 graph
->points_to
[n
] = BITMAP_ALLOC (&predbitmap_obstack
);
2170 if (first_pred
!= -1U)
2171 bitmap_copy (graph
->points_to
[n
], graph
->points_to
[first_pred
]);
2173 bitmap_set_bit (graph
->points_to
[n
], FIRST_REF_NODE
+ n
);
2174 graph
->pointer_label
[n
] = pointer_equiv_class
++;
2175 equiv_class_label_t ecl
;
2176 ecl
= equiv_class_lookup_or_add (pointer_equiv_class_table
,
2177 graph
->points_to
[n
]);
2178 ecl
->equivalence_class
= graph
->pointer_label
[n
];
2182 /* If there was only a single non-empty predecessor the pointer equiv
2183 class is the same. */
2184 if (!graph
->points_to
[n
])
2186 if (first_pred
!= -1U)
2188 graph
->pointer_label
[n
] = graph
->pointer_label
[first_pred
];
2189 graph
->points_to
[n
] = graph
->points_to
[first_pred
];
2194 if (!bitmap_empty_p (graph
->points_to
[n
]))
2196 equiv_class_label_t ecl
;
2197 ecl
= equiv_class_lookup_or_add (pointer_equiv_class_table
,
2198 graph
->points_to
[n
]);
2199 if (ecl
->equivalence_class
== 0)
2200 ecl
->equivalence_class
= pointer_equiv_class
++;
2203 BITMAP_FREE (graph
->points_to
[n
]);
2204 graph
->points_to
[n
] = ecl
->labels
;
2206 graph
->pointer_label
[n
] = ecl
->equivalence_class
;
2210 /* Print the pred graph in dot format. */
2213 dump_pred_graph (struct scc_info
*si
, FILE *file
)
2217 /* Only print the graph if it has already been initialized: */
2221 /* Prints the header of the dot file: */
2222 fprintf (file
, "strict digraph {\n");
2223 fprintf (file
, " node [\n shape = box\n ]\n");
2224 fprintf (file
, " edge [\n fontsize = \"12\"\n ]\n");
2225 fprintf (file
, "\n // List of nodes and complex constraints in "
2226 "the constraint graph:\n");
2228 /* The next lines print the nodes in the graph together with the
2229 complex constraints attached to them. */
2230 for (i
= 1; i
< graph
->size
; i
++)
2232 if (i
== FIRST_REF_NODE
)
2234 if (si
->node_mapping
[i
] != i
)
2236 if (i
< FIRST_REF_NODE
)
2237 fprintf (file
, "\"%s\"", get_varinfo (i
)->name
);
2239 fprintf (file
, "\"*%s\"", get_varinfo (i
- FIRST_REF_NODE
)->name
);
2240 if (graph
->points_to
[i
]
2241 && !bitmap_empty_p (graph
->points_to
[i
]))
2243 fprintf (file
, "[label=\"%s = {", get_varinfo (i
)->name
);
2246 EXECUTE_IF_SET_IN_BITMAP (graph
->points_to
[i
], 0, j
, bi
)
2247 fprintf (file
, " %d", j
);
2248 fprintf (file
, " }\"]");
2250 fprintf (file
, ";\n");
2253 /* Go over the edges. */
2254 fprintf (file
, "\n // Edges in the constraint graph:\n");
2255 for (i
= 1; i
< graph
->size
; i
++)
2259 if (si
->node_mapping
[i
] != i
)
2261 EXECUTE_IF_IN_NONNULL_BITMAP (graph
->preds
[i
], 0, j
, bi
)
2263 unsigned from
= si
->node_mapping
[j
];
2264 if (from
< FIRST_REF_NODE
)
2265 fprintf (file
, "\"%s\"", get_varinfo (from
)->name
);
2267 fprintf (file
, "\"*%s\"", get_varinfo (from
- FIRST_REF_NODE
)->name
);
2268 fprintf (file
, " -> ");
2269 if (i
< FIRST_REF_NODE
)
2270 fprintf (file
, "\"%s\"", get_varinfo (i
)->name
);
2272 fprintf (file
, "\"*%s\"", get_varinfo (i
- FIRST_REF_NODE
)->name
);
2273 fprintf (file
, ";\n");
2277 /* Prints the tail of the dot file. */
2278 fprintf (file
, "}\n");
2281 /* Perform offline variable substitution, discovering equivalence
2282 classes, and eliminating non-pointer variables. */
2284 static struct scc_info
*
2285 perform_var_substitution (constraint_graph_t graph
)
2288 unsigned int size
= graph
->size
;
2289 struct scc_info
*si
= init_scc_info (size
);
2291 bitmap_obstack_initialize (&iteration_obstack
);
2292 pointer_equiv_class_table
= new hash_table
<equiv_class_hasher
> (511);
2293 location_equiv_class_table
2294 = new hash_table
<equiv_class_hasher
> (511);
2295 pointer_equiv_class
= 1;
2296 location_equiv_class
= 1;
2298 /* Condense the nodes, which means to find SCC's, count incoming
2299 predecessors, and unite nodes in SCC's. */
2300 for (i
= 1; i
< FIRST_REF_NODE
; i
++)
2301 if (!bitmap_bit_p (si
->visited
, si
->node_mapping
[i
]))
2302 condense_visit (graph
, si
, si
->node_mapping
[i
]);
2304 if (dump_file
&& (dump_flags
& TDF_GRAPH
))
2306 fprintf (dump_file
, "\n\n// The constraint graph before var-substitution "
2307 "in dot format:\n");
2308 dump_pred_graph (si
, dump_file
);
2309 fprintf (dump_file
, "\n\n");
2312 bitmap_clear (si
->visited
);
2313 /* Actually the label the nodes for pointer equivalences */
2314 for (i
= 1; i
< FIRST_REF_NODE
; i
++)
2315 if (!bitmap_bit_p (si
->visited
, si
->node_mapping
[i
]))
2316 label_visit (graph
, si
, si
->node_mapping
[i
]);
2318 /* Calculate location equivalence labels. */
2319 for (i
= 1; i
< FIRST_REF_NODE
; i
++)
2325 if (!graph
->pointed_by
[i
])
2327 pointed_by
= BITMAP_ALLOC (&iteration_obstack
);
2329 /* Translate the pointed-by mapping for pointer equivalence
2331 EXECUTE_IF_SET_IN_BITMAP (graph
->pointed_by
[i
], 0, j
, bi
)
2333 bitmap_set_bit (pointed_by
,
2334 graph
->pointer_label
[si
->node_mapping
[j
]]);
2336 /* The original pointed_by is now dead. */
2337 BITMAP_FREE (graph
->pointed_by
[i
]);
2339 /* Look up the location equivalence label if one exists, or make
2341 equiv_class_label_t ecl
;
2342 ecl
= equiv_class_lookup_or_add (location_equiv_class_table
, pointed_by
);
2343 if (ecl
->equivalence_class
== 0)
2344 ecl
->equivalence_class
= location_equiv_class
++;
2347 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2348 fprintf (dump_file
, "Found location equivalence for node %s\n",
2349 get_varinfo (i
)->name
);
2350 BITMAP_FREE (pointed_by
);
2352 graph
->loc_label
[i
] = ecl
->equivalence_class
;
2356 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2357 for (i
= 1; i
< FIRST_REF_NODE
; i
++)
2359 unsigned j
= si
->node_mapping
[i
];
2362 fprintf (dump_file
, "%s node id %d ",
2363 bitmap_bit_p (graph
->direct_nodes
, i
)
2364 ? "Direct" : "Indirect", i
);
2365 if (i
< FIRST_REF_NODE
)
2366 fprintf (dump_file
, "\"%s\"", get_varinfo (i
)->name
);
2368 fprintf (dump_file
, "\"*%s\"",
2369 get_varinfo (i
- FIRST_REF_NODE
)->name
);
2370 fprintf (dump_file
, " mapped to SCC leader node id %d ", j
);
2371 if (j
< FIRST_REF_NODE
)
2372 fprintf (dump_file
, "\"%s\"\n", get_varinfo (j
)->name
);
2374 fprintf (dump_file
, "\"*%s\"\n",
2375 get_varinfo (j
- FIRST_REF_NODE
)->name
);
2380 "Equivalence classes for %s node id %d ",
2381 bitmap_bit_p (graph
->direct_nodes
, i
)
2382 ? "direct" : "indirect", i
);
2383 if (i
< FIRST_REF_NODE
)
2384 fprintf (dump_file
, "\"%s\"", get_varinfo (i
)->name
);
2386 fprintf (dump_file
, "\"*%s\"",
2387 get_varinfo (i
- FIRST_REF_NODE
)->name
);
2389 ": pointer %d, location %d\n",
2390 graph
->pointer_label
[i
], graph
->loc_label
[i
]);
2394 /* Quickly eliminate our non-pointer variables. */
2396 for (i
= 1; i
< FIRST_REF_NODE
; i
++)
2398 unsigned int node
= si
->node_mapping
[i
];
2400 if (graph
->pointer_label
[node
] == 0)
2402 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2404 "%s is a non-pointer variable, eliminating edges.\n",
2405 get_varinfo (node
)->name
);
2406 stats
.nonpointer_vars
++;
2407 clear_edges_for_node (graph
, node
);
2414 /* Free information that was only necessary for variable
2418 free_var_substitution_info (struct scc_info
*si
)
2421 free (graph
->pointer_label
);
2422 free (graph
->loc_label
);
2423 free (graph
->pointed_by
);
2424 free (graph
->points_to
);
2425 free (graph
->eq_rep
);
2426 sbitmap_free (graph
->direct_nodes
);
2427 delete pointer_equiv_class_table
;
2428 pointer_equiv_class_table
= NULL
;
2429 delete location_equiv_class_table
;
2430 location_equiv_class_table
= NULL
;
2431 bitmap_obstack_release (&iteration_obstack
);
2434 /* Return an existing node that is equivalent to NODE, which has
2435 equivalence class LABEL, if one exists. Return NODE otherwise. */
2438 find_equivalent_node (constraint_graph_t graph
,
2439 unsigned int node
, unsigned int label
)
2441 /* If the address version of this variable is unused, we can
2442 substitute it for anything else with the same label.
2443 Otherwise, we know the pointers are equivalent, but not the
2444 locations, and we can unite them later. */
2446 if (!bitmap_bit_p (graph
->address_taken
, node
))
2448 gcc_checking_assert (label
< graph
->size
);
2450 if (graph
->eq_rep
[label
] != -1)
2452 /* Unify the two variables since we know they are equivalent. */
2453 if (unite (graph
->eq_rep
[label
], node
))
2454 unify_nodes (graph
, graph
->eq_rep
[label
], node
, false);
2455 return graph
->eq_rep
[label
];
2459 graph
->eq_rep
[label
] = node
;
2460 graph
->pe_rep
[label
] = node
;
2465 gcc_checking_assert (label
< graph
->size
);
2466 graph
->pe
[node
] = label
;
2467 if (graph
->pe_rep
[label
] == -1)
2468 graph
->pe_rep
[label
] = node
;
2474 /* Unite pointer equivalent but not location equivalent nodes in
2475 GRAPH. This may only be performed once variable substitution is
2479 unite_pointer_equivalences (constraint_graph_t graph
)
2483 /* Go through the pointer equivalences and unite them to their
2484 representative, if they aren't already. */
2485 for (i
= 1; i
< FIRST_REF_NODE
; i
++)
2487 unsigned int label
= graph
->pe
[i
];
2490 int label_rep
= graph
->pe_rep
[label
];
2492 if (label_rep
== -1)
2495 label_rep
= find (label_rep
);
2496 if (label_rep
>= 0 && unite (label_rep
, find (i
)))
2497 unify_nodes (graph
, label_rep
, i
, false);
2502 /* Move complex constraints to the GRAPH nodes they belong to. */
2505 move_complex_constraints (constraint_graph_t graph
)
2510 FOR_EACH_VEC_ELT (constraints
, i
, c
)
2514 struct constraint_expr lhs
= c
->lhs
;
2515 struct constraint_expr rhs
= c
->rhs
;
2517 if (lhs
.type
== DEREF
)
2519 insert_into_complex (graph
, lhs
.var
, c
);
2521 else if (rhs
.type
== DEREF
)
2523 if (!(get_varinfo (lhs
.var
)->is_special_var
))
2524 insert_into_complex (graph
, rhs
.var
, c
);
2526 else if (rhs
.type
!= ADDRESSOF
&& lhs
.var
> anything_id
2527 && (lhs
.offset
!= 0 || rhs
.offset
!= 0))
2529 insert_into_complex (graph
, rhs
.var
, c
);
2536 /* Optimize and rewrite complex constraints while performing
2537 collapsing of equivalent nodes. SI is the SCC_INFO that is the
2538 result of perform_variable_substitution. */
2541 rewrite_constraints (constraint_graph_t graph
,
2542 struct scc_info
*si
)
2547 #ifdef ENABLE_CHECKING
2548 for (unsigned int j
= 0; j
< graph
->size
; j
++)
2549 gcc_assert (find (j
) == j
);
2552 FOR_EACH_VEC_ELT (constraints
, i
, c
)
2554 struct constraint_expr lhs
= c
->lhs
;
2555 struct constraint_expr rhs
= c
->rhs
;
2556 unsigned int lhsvar
= find (lhs
.var
);
2557 unsigned int rhsvar
= find (rhs
.var
);
2558 unsigned int lhsnode
, rhsnode
;
2559 unsigned int lhslabel
, rhslabel
;
2561 lhsnode
= si
->node_mapping
[lhsvar
];
2562 rhsnode
= si
->node_mapping
[rhsvar
];
2563 lhslabel
= graph
->pointer_label
[lhsnode
];
2564 rhslabel
= graph
->pointer_label
[rhsnode
];
2566 /* See if it is really a non-pointer variable, and if so, ignore
2570 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2573 fprintf (dump_file
, "%s is a non-pointer variable,"
2574 "ignoring constraint:",
2575 get_varinfo (lhs
.var
)->name
);
2576 dump_constraint (dump_file
, c
);
2577 fprintf (dump_file
, "\n");
2579 constraints
[i
] = NULL
;
2585 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2588 fprintf (dump_file
, "%s is a non-pointer variable,"
2589 "ignoring constraint:",
2590 get_varinfo (rhs
.var
)->name
);
2591 dump_constraint (dump_file
, c
);
2592 fprintf (dump_file
, "\n");
2594 constraints
[i
] = NULL
;
2598 lhsvar
= find_equivalent_node (graph
, lhsvar
, lhslabel
);
2599 rhsvar
= find_equivalent_node (graph
, rhsvar
, rhslabel
);
2600 c
->lhs
.var
= lhsvar
;
2601 c
->rhs
.var
= rhsvar
;
2605 /* Eliminate indirect cycles involving NODE. Return true if NODE was
2606 part of an SCC, false otherwise. */
2609 eliminate_indirect_cycles (unsigned int node
)
2611 if (graph
->indirect_cycles
[node
] != -1
2612 && !bitmap_empty_p (get_varinfo (node
)->solution
))
2615 auto_vec
<unsigned> queue
;
2617 unsigned int to
= find (graph
->indirect_cycles
[node
]);
2620 /* We can't touch the solution set and call unify_nodes
2621 at the same time, because unify_nodes is going to do
2622 bitmap unions into it. */
2624 EXECUTE_IF_SET_IN_BITMAP (get_varinfo (node
)->solution
, 0, i
, bi
)
2626 if (find (i
) == i
&& i
!= to
)
2629 queue
.safe_push (i
);
2634 queue
.iterate (queuepos
, &i
);
2637 unify_nodes (graph
, to
, i
, true);
2644 /* Solve the constraint graph GRAPH using our worklist solver.
2645 This is based on the PW* family of solvers from the "Efficient Field
2646 Sensitive Pointer Analysis for C" paper.
2647 It works by iterating over all the graph nodes, processing the complex
2648 constraints and propagating the copy constraints, until everything stops
2649 changed. This corresponds to steps 6-8 in the solving list given above. */
2652 solve_graph (constraint_graph_t graph
)
2654 unsigned int size
= graph
->size
;
2658 changed
= BITMAP_ALLOC (NULL
);
2660 /* Mark all initial non-collapsed nodes as changed. */
2661 for (i
= 1; i
< size
; i
++)
2663 varinfo_t ivi
= get_varinfo (i
);
2664 if (find (i
) == i
&& !bitmap_empty_p (ivi
->solution
)
2665 && ((graph
->succs
[i
] && !bitmap_empty_p (graph
->succs
[i
]))
2666 || graph
->complex[i
].length () > 0))
2667 bitmap_set_bit (changed
, i
);
2670 /* Allocate a bitmap to be used to store the changed bits. */
2671 pts
= BITMAP_ALLOC (&pta_obstack
);
2673 while (!bitmap_empty_p (changed
))
2676 struct topo_info
*ti
= init_topo_info ();
2679 bitmap_obstack_initialize (&iteration_obstack
);
2681 compute_topo_order (graph
, ti
);
2683 while (ti
->topo_order
.length () != 0)
2686 i
= ti
->topo_order
.pop ();
2688 /* If this variable is not a representative, skip it. */
2692 /* In certain indirect cycle cases, we may merge this
2693 variable to another. */
2694 if (eliminate_indirect_cycles (i
) && find (i
) != i
)
2697 /* If the node has changed, we need to process the
2698 complex constraints and outgoing edges again. */
2699 if (bitmap_clear_bit (changed
, i
))
2704 vec
<constraint_t
> complex = graph
->complex[i
];
2705 varinfo_t vi
= get_varinfo (i
);
2706 bool solution_empty
;
2708 /* Compute the changed set of solution bits. If anything
2709 is in the solution just propagate that. */
2710 if (bitmap_bit_p (vi
->solution
, anything_id
))
2712 /* If anything is also in the old solution there is
2714 ??? But we shouldn't ended up with "changed" set ... */
2716 && bitmap_bit_p (vi
->oldsolution
, anything_id
))
2718 bitmap_copy (pts
, get_varinfo (find (anything_id
))->solution
);
2720 else if (vi
->oldsolution
)
2721 bitmap_and_compl (pts
, vi
->solution
, vi
->oldsolution
);
2723 bitmap_copy (pts
, vi
->solution
);
2725 if (bitmap_empty_p (pts
))
2728 if (vi
->oldsolution
)
2729 bitmap_ior_into (vi
->oldsolution
, pts
);
2732 vi
->oldsolution
= BITMAP_ALLOC (&oldpta_obstack
);
2733 bitmap_copy (vi
->oldsolution
, pts
);
2736 solution
= vi
->solution
;
2737 solution_empty
= bitmap_empty_p (solution
);
2739 /* Process the complex constraints */
2740 bitmap expanded_pts
= NULL
;
2741 FOR_EACH_VEC_ELT (complex, j
, c
)
2743 /* XXX: This is going to unsort the constraints in
2744 some cases, which will occasionally add duplicate
2745 constraints during unification. This does not
2746 affect correctness. */
2747 c
->lhs
.var
= find (c
->lhs
.var
);
2748 c
->rhs
.var
= find (c
->rhs
.var
);
2750 /* The only complex constraint that can change our
2751 solution to non-empty, given an empty solution,
2752 is a constraint where the lhs side is receiving
2753 some set from elsewhere. */
2754 if (!solution_empty
|| c
->lhs
.type
!= DEREF
)
2755 do_complex_constraint (graph
, c
, pts
, &expanded_pts
);
2757 BITMAP_FREE (expanded_pts
);
2759 solution_empty
= bitmap_empty_p (solution
);
2761 if (!solution_empty
)
2764 unsigned eff_escaped_id
= find (escaped_id
);
2766 /* Propagate solution to all successors. */
2767 EXECUTE_IF_IN_NONNULL_BITMAP (graph
->succs
[i
],
2773 unsigned int to
= find (j
);
2774 tmp
= get_varinfo (to
)->solution
;
2777 /* Don't try to propagate to ourselves. */
2781 /* If we propagate from ESCAPED use ESCAPED as
2783 if (i
== eff_escaped_id
)
2784 flag
= bitmap_set_bit (tmp
, escaped_id
);
2786 flag
= bitmap_ior_into (tmp
, pts
);
2789 bitmap_set_bit (changed
, to
);
2794 free_topo_info (ti
);
2795 bitmap_obstack_release (&iteration_obstack
);
2799 BITMAP_FREE (changed
);
2800 bitmap_obstack_release (&oldpta_obstack
);
2803 /* Map from trees to variable infos. */
2804 static hash_map
<tree
, varinfo_t
> *vi_for_tree
;
2807 /* Insert ID as the variable id for tree T in the vi_for_tree map. */
2810 insert_vi_for_tree (tree t
, varinfo_t vi
)
2813 gcc_assert (!vi_for_tree
->put (t
, vi
));
2816 /* Find the variable info for tree T in VI_FOR_TREE. If T does not
2817 exist in the map, return NULL, otherwise, return the varinfo we found. */
2820 lookup_vi_for_tree (tree t
)
2822 varinfo_t
*slot
= vi_for_tree
->get (t
);
2829 /* Return a printable name for DECL */
2832 alias_get_name (tree decl
)
2834 const char *res
= NULL
;
2836 int num_printed
= 0;
2841 if (TREE_CODE (decl
) == SSA_NAME
)
2843 res
= get_name (decl
);
2845 num_printed
= asprintf (&temp
, "%s_%u", res
, SSA_NAME_VERSION (decl
));
2847 num_printed
= asprintf (&temp
, "_%u", SSA_NAME_VERSION (decl
));
2848 if (num_printed
> 0)
2850 res
= ggc_strdup (temp
);
2854 else if (DECL_P (decl
))
2856 if (DECL_ASSEMBLER_NAME_SET_P (decl
))
2857 res
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
2860 res
= get_name (decl
);
2863 num_printed
= asprintf (&temp
, "D.%u", DECL_UID (decl
));
2864 if (num_printed
> 0)
2866 res
= ggc_strdup (temp
);
2878 /* Find the variable id for tree T in the map.
2879 If T doesn't exist in the map, create an entry for it and return it. */
2882 get_vi_for_tree (tree t
)
2884 varinfo_t
*slot
= vi_for_tree
->get (t
);
2886 return get_varinfo (create_variable_info_for (t
, alias_get_name (t
)));
2891 /* Get a scalar constraint expression for a new temporary variable. */
2893 static struct constraint_expr
2894 new_scalar_tmp_constraint_exp (const char *name
)
2896 struct constraint_expr tmp
;
2899 vi
= new_var_info (NULL_TREE
, name
);
2903 vi
->is_full_var
= 1;
2912 /* Get a constraint expression vector from an SSA_VAR_P node.
2913 If address_p is true, the result will be taken its address of. */
2916 get_constraint_for_ssa_var (tree t
, vec
<ce_s
> *results
, bool address_p
)
2918 struct constraint_expr cexpr
;
2921 /* We allow FUNCTION_DECLs here even though it doesn't make much sense. */
2922 gcc_assert (TREE_CODE (t
) == SSA_NAME
|| DECL_P (t
));
2924 /* For parameters, get at the points-to set for the actual parm
2926 if (TREE_CODE (t
) == SSA_NAME
2927 && SSA_NAME_IS_DEFAULT_DEF (t
)
2928 && (TREE_CODE (SSA_NAME_VAR (t
)) == PARM_DECL
2929 || TREE_CODE (SSA_NAME_VAR (t
)) == RESULT_DECL
))
2931 get_constraint_for_ssa_var (SSA_NAME_VAR (t
), results
, address_p
);
2935 /* For global variables resort to the alias target. */
2936 if (TREE_CODE (t
) == VAR_DECL
2937 && (TREE_STATIC (t
) || DECL_EXTERNAL (t
)))
2939 varpool_node
*node
= varpool_node::get (t
);
2940 if (node
&& node
->alias
&& node
->analyzed
)
2942 node
= node
->ultimate_alias_target ();
2947 vi
= get_vi_for_tree (t
);
2949 cexpr
.type
= SCALAR
;
2952 /* If we are not taking the address of the constraint expr, add all
2953 sub-fiels of the variable as well. */
2955 && !vi
->is_full_var
)
2957 for (; vi
; vi
= vi_next (vi
))
2960 results
->safe_push (cexpr
);
2965 results
->safe_push (cexpr
);
2968 /* Process constraint T, performing various simplifications and then
2969 adding it to our list of overall constraints. */
2972 process_constraint (constraint_t t
)
2974 struct constraint_expr rhs
= t
->rhs
;
2975 struct constraint_expr lhs
= t
->lhs
;
2977 gcc_assert (rhs
.var
< varmap
.length ());
2978 gcc_assert (lhs
.var
< varmap
.length ());
2980 /* If we didn't get any useful constraint from the lhs we get
2981 &ANYTHING as fallback from get_constraint_for. Deal with
2982 it here by turning it into *ANYTHING. */
2983 if (lhs
.type
== ADDRESSOF
2984 && lhs
.var
== anything_id
)
2987 /* ADDRESSOF on the lhs is invalid. */
2988 gcc_assert (lhs
.type
!= ADDRESSOF
);
2990 /* We shouldn't add constraints from things that cannot have pointers.
2991 It's not completely trivial to avoid in the callers, so do it here. */
2992 if (rhs
.type
!= ADDRESSOF
2993 && !get_varinfo (rhs
.var
)->may_have_pointers
)
2996 /* Likewise adding to the solution of a non-pointer var isn't useful. */
2997 if (!get_varinfo (lhs
.var
)->may_have_pointers
)
3000 /* This can happen in our IR with things like n->a = *p */
3001 if (rhs
.type
== DEREF
&& lhs
.type
== DEREF
&& rhs
.var
!= anything_id
)
3003 /* Split into tmp = *rhs, *lhs = tmp */
3004 struct constraint_expr tmplhs
;
3005 tmplhs
= new_scalar_tmp_constraint_exp ("doubledereftmp");
3006 process_constraint (new_constraint (tmplhs
, rhs
));
3007 process_constraint (new_constraint (lhs
, tmplhs
));
3009 else if (rhs
.type
== ADDRESSOF
&& lhs
.type
== DEREF
)
3011 /* Split into tmp = &rhs, *lhs = tmp */
3012 struct constraint_expr tmplhs
;
3013 tmplhs
= new_scalar_tmp_constraint_exp ("derefaddrtmp");
3014 process_constraint (new_constraint (tmplhs
, rhs
));
3015 process_constraint (new_constraint (lhs
, tmplhs
));
3019 gcc_assert (rhs
.type
!= ADDRESSOF
|| rhs
.offset
== 0);
3020 constraints
.safe_push (t
);
3025 /* Return the position, in bits, of FIELD_DECL from the beginning of its
3028 static HOST_WIDE_INT
3029 bitpos_of_field (const tree fdecl
)
3031 if (!tree_fits_shwi_p (DECL_FIELD_OFFSET (fdecl
))
3032 || !tree_fits_shwi_p (DECL_FIELD_BIT_OFFSET (fdecl
)))
3035 return (tree_to_shwi (DECL_FIELD_OFFSET (fdecl
)) * BITS_PER_UNIT
3036 + tree_to_shwi (DECL_FIELD_BIT_OFFSET (fdecl
)));
3040 /* Get constraint expressions for offsetting PTR by OFFSET. Stores the
3041 resulting constraint expressions in *RESULTS. */
3044 get_constraint_for_ptr_offset (tree ptr
, tree offset
,
3047 struct constraint_expr c
;
3049 HOST_WIDE_INT rhsoffset
;
3051 /* If we do not do field-sensitive PTA adding offsets to pointers
3052 does not change the points-to solution. */
3053 if (!use_field_sensitive
)
3055 get_constraint_for_rhs (ptr
, results
);
3059 /* If the offset is not a non-negative integer constant that fits
3060 in a HOST_WIDE_INT, we have to fall back to a conservative
3061 solution which includes all sub-fields of all pointed-to
3062 variables of ptr. */
3063 if (offset
== NULL_TREE
3064 || TREE_CODE (offset
) != INTEGER_CST
)
3065 rhsoffset
= UNKNOWN_OFFSET
;
3068 /* Sign-extend the offset. */
3069 offset_int soffset
= offset_int::from (offset
, SIGNED
);
3070 if (!wi::fits_shwi_p (soffset
))
3071 rhsoffset
= UNKNOWN_OFFSET
;
3074 /* Make sure the bit-offset also fits. */
3075 HOST_WIDE_INT rhsunitoffset
= soffset
.to_shwi ();
3076 rhsoffset
= rhsunitoffset
* BITS_PER_UNIT
;
3077 if (rhsunitoffset
!= rhsoffset
/ BITS_PER_UNIT
)
3078 rhsoffset
= UNKNOWN_OFFSET
;
3082 get_constraint_for_rhs (ptr
, results
);
3086 /* As we are eventually appending to the solution do not use
3087 vec::iterate here. */
3088 n
= results
->length ();
3089 for (j
= 0; j
< n
; j
++)
3093 curr
= get_varinfo (c
.var
);
3095 if (c
.type
== ADDRESSOF
3096 /* If this varinfo represents a full variable just use it. */
3097 && curr
->is_full_var
)
3099 else if (c
.type
== ADDRESSOF
3100 /* If we do not know the offset add all subfields. */
3101 && rhsoffset
== UNKNOWN_OFFSET
)
3103 varinfo_t temp
= get_varinfo (curr
->head
);
3106 struct constraint_expr c2
;
3108 c2
.type
= ADDRESSOF
;
3110 if (c2
.var
!= c
.var
)
3111 results
->safe_push (c2
);
3112 temp
= vi_next (temp
);
3116 else if (c
.type
== ADDRESSOF
)
3119 unsigned HOST_WIDE_INT offset
= curr
->offset
+ rhsoffset
;
3121 /* If curr->offset + rhsoffset is less than zero adjust it. */
3123 && curr
->offset
< offset
)
3126 /* We have to include all fields that overlap the current
3127 field shifted by rhsoffset. And we include at least
3128 the last or the first field of the variable to represent
3129 reachability of off-bound addresses, in particular &object + 1,
3130 conservatively correct. */
3131 temp
= first_or_preceding_vi_for_offset (curr
, offset
);
3134 temp
= vi_next (temp
);
3136 && temp
->offset
< offset
+ curr
->size
)
3138 struct constraint_expr c2
;
3140 c2
.type
= ADDRESSOF
;
3142 results
->safe_push (c2
);
3143 temp
= vi_next (temp
);
3146 else if (c
.type
== SCALAR
)
3148 gcc_assert (c
.offset
== 0);
3149 c
.offset
= rhsoffset
;
3152 /* We shouldn't get any DEREFs here. */
3160 /* Given a COMPONENT_REF T, return the constraint_expr vector for it.
3161 If address_p is true the result will be taken its address of.
3162 If lhs_p is true then the constraint expression is assumed to be used
3166 get_constraint_for_component_ref (tree t
, vec
<ce_s
> *results
,
3167 bool address_p
, bool lhs_p
)
3170 HOST_WIDE_INT bitsize
= -1;
3171 HOST_WIDE_INT bitmaxsize
= -1;
3172 HOST_WIDE_INT bitpos
;
3175 /* Some people like to do cute things like take the address of
3178 while (handled_component_p (forzero
)
3179 || INDIRECT_REF_P (forzero
)
3180 || TREE_CODE (forzero
) == MEM_REF
)
3181 forzero
= TREE_OPERAND (forzero
, 0);
3183 if (CONSTANT_CLASS_P (forzero
) && integer_zerop (forzero
))
3185 struct constraint_expr temp
;
3188 temp
.var
= integer_id
;
3190 results
->safe_push (temp
);
3194 t
= get_ref_base_and_extent (t
, &bitpos
, &bitsize
, &bitmaxsize
);
3196 /* Pretend to take the address of the base, we'll take care of
3197 adding the required subset of sub-fields below. */
3198 get_constraint_for_1 (t
, results
, true, lhs_p
);
3199 gcc_assert (results
->length () == 1);
3200 struct constraint_expr
&result
= results
->last ();
3202 if (result
.type
== SCALAR
3203 && get_varinfo (result
.var
)->is_full_var
)
3204 /* For single-field vars do not bother about the offset. */
3206 else if (result
.type
== SCALAR
)
3208 /* In languages like C, you can access one past the end of an
3209 array. You aren't allowed to dereference it, so we can
3210 ignore this constraint. When we handle pointer subtraction,
3211 we may have to do something cute here. */
3213 if ((unsigned HOST_WIDE_INT
)bitpos
< get_varinfo (result
.var
)->fullsize
3216 /* It's also not true that the constraint will actually start at the
3217 right offset, it may start in some padding. We only care about
3218 setting the constraint to the first actual field it touches, so
3220 struct constraint_expr cexpr
= result
;
3224 for (curr
= get_varinfo (cexpr
.var
); curr
; curr
= vi_next (curr
))
3226 if (ranges_overlap_p (curr
->offset
, curr
->size
,
3227 bitpos
, bitmaxsize
))
3229 cexpr
.var
= curr
->id
;
3230 results
->safe_push (cexpr
);
3235 /* If we are going to take the address of this field then
3236 to be able to compute reachability correctly add at least
3237 the last field of the variable. */
3238 if (address_p
&& results
->length () == 0)
3240 curr
= get_varinfo (cexpr
.var
);
3241 while (curr
->next
!= 0)
3242 curr
= vi_next (curr
);
3243 cexpr
.var
= curr
->id
;
3244 results
->safe_push (cexpr
);
3246 else if (results
->length () == 0)
3247 /* Assert that we found *some* field there. The user couldn't be
3248 accessing *only* padding. */
3249 /* Still the user could access one past the end of an array
3250 embedded in a struct resulting in accessing *only* padding. */
3251 /* Or accessing only padding via type-punning to a type
3252 that has a filed just in padding space. */
3254 cexpr
.type
= SCALAR
;
3255 cexpr
.var
= anything_id
;
3257 results
->safe_push (cexpr
);
3260 else if (bitmaxsize
== 0)
3262 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3263 fprintf (dump_file
, "Access to zero-sized part of variable,"
3267 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3268 fprintf (dump_file
, "Access to past the end of variable, ignoring\n");
3270 else if (result
.type
== DEREF
)
3272 /* If we do not know exactly where the access goes say so. Note
3273 that only for non-structure accesses we know that we access
3274 at most one subfiled of any variable. */
3276 || bitsize
!= bitmaxsize
3277 || AGGREGATE_TYPE_P (TREE_TYPE (orig_t
))
3278 || result
.offset
== UNKNOWN_OFFSET
)
3279 result
.offset
= UNKNOWN_OFFSET
;
3281 result
.offset
+= bitpos
;
3283 else if (result
.type
== ADDRESSOF
)
3285 /* We can end up here for component references on a
3286 VIEW_CONVERT_EXPR <>(&foobar). */
3287 result
.type
= SCALAR
;
3288 result
.var
= anything_id
;
3296 /* Dereference the constraint expression CONS, and return the result.
3297 DEREF (ADDRESSOF) = SCALAR
3298 DEREF (SCALAR) = DEREF
3299 DEREF (DEREF) = (temp = DEREF1; result = DEREF(temp))
3300 This is needed so that we can handle dereferencing DEREF constraints. */
3303 do_deref (vec
<ce_s
> *constraints
)
3305 struct constraint_expr
*c
;
3308 FOR_EACH_VEC_ELT (*constraints
, i
, c
)
3310 if (c
->type
== SCALAR
)
3312 else if (c
->type
== ADDRESSOF
)
3314 else if (c
->type
== DEREF
)
3316 struct constraint_expr tmplhs
;
3317 tmplhs
= new_scalar_tmp_constraint_exp ("dereftmp");
3318 process_constraint (new_constraint (tmplhs
, *c
));
3319 c
->var
= tmplhs
.var
;
3326 /* Given a tree T, return the constraint expression for taking the
3330 get_constraint_for_address_of (tree t
, vec
<ce_s
> *results
)
3332 struct constraint_expr
*c
;
3335 get_constraint_for_1 (t
, results
, true, true);
3337 FOR_EACH_VEC_ELT (*results
, i
, c
)
3339 if (c
->type
== DEREF
)
3342 c
->type
= ADDRESSOF
;
3346 /* Given a tree T, return the constraint expression for it. */
3349 get_constraint_for_1 (tree t
, vec
<ce_s
> *results
, bool address_p
,
3352 struct constraint_expr temp
;
3354 /* x = integer is all glommed to a single variable, which doesn't
3355 point to anything by itself. That is, of course, unless it is an
3356 integer constant being treated as a pointer, in which case, we
3357 will return that this is really the addressof anything. This
3358 happens below, since it will fall into the default case. The only
3359 case we know something about an integer treated like a pointer is
3360 when it is the NULL pointer, and then we just say it points to
3363 Do not do that if -fno-delete-null-pointer-checks though, because
3364 in that case *NULL does not fail, so it _should_ alias *anything.
3365 It is not worth adding a new option or renaming the existing one,
3366 since this case is relatively obscure. */
3367 if ((TREE_CODE (t
) == INTEGER_CST
3368 && integer_zerop (t
))
3369 /* The only valid CONSTRUCTORs in gimple with pointer typed
3370 elements are zero-initializer. But in IPA mode we also
3371 process global initializers, so verify at least. */
3372 || (TREE_CODE (t
) == CONSTRUCTOR
3373 && CONSTRUCTOR_NELTS (t
) == 0))
3375 if (flag_delete_null_pointer_checks
)
3376 temp
.var
= nothing_id
;
3378 temp
.var
= nonlocal_id
;
3379 temp
.type
= ADDRESSOF
;
3381 results
->safe_push (temp
);
3385 /* String constants are read-only, ideally we'd have a CONST_DECL
3387 if (TREE_CODE (t
) == STRING_CST
)
3389 temp
.var
= string_id
;
3392 results
->safe_push (temp
);
3396 switch (TREE_CODE_CLASS (TREE_CODE (t
)))
3398 case tcc_expression
:
3400 switch (TREE_CODE (t
))
3403 get_constraint_for_address_of (TREE_OPERAND (t
, 0), results
);
3411 switch (TREE_CODE (t
))
3415 struct constraint_expr cs
;
3417 get_constraint_for_ptr_offset (TREE_OPERAND (t
, 0),
3418 TREE_OPERAND (t
, 1), results
);
3421 /* If we are not taking the address then make sure to process
3422 all subvariables we might access. */
3426 cs
= results
->last ();
3427 if (cs
.type
== DEREF
3428 && type_can_have_subvars (TREE_TYPE (t
)))
3430 /* For dereferences this means we have to defer it
3432 results
->last ().offset
= UNKNOWN_OFFSET
;
3435 if (cs
.type
!= SCALAR
)
3438 vi
= get_varinfo (cs
.var
);
3439 curr
= vi_next (vi
);
3440 if (!vi
->is_full_var
3443 unsigned HOST_WIDE_INT size
;
3444 if (tree_fits_uhwi_p (TYPE_SIZE (TREE_TYPE (t
))))
3445 size
= tree_to_uhwi (TYPE_SIZE (TREE_TYPE (t
)));
3448 for (; curr
; curr
= vi_next (curr
))
3450 if (curr
->offset
- vi
->offset
< size
)
3453 results
->safe_push (cs
);
3462 case ARRAY_RANGE_REF
:
3467 get_constraint_for_component_ref (t
, results
, address_p
, lhs_p
);
3469 case VIEW_CONVERT_EXPR
:
3470 get_constraint_for_1 (TREE_OPERAND (t
, 0), results
, address_p
,
3473 /* We are missing handling for TARGET_MEM_REF here. */
3478 case tcc_exceptional
:
3480 switch (TREE_CODE (t
))
3484 get_constraint_for_ssa_var (t
, results
, address_p
);
3492 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t
), i
, val
)
3494 struct constraint_expr
*rhsp
;
3496 get_constraint_for_1 (val
, &tmp
, address_p
, lhs_p
);
3497 FOR_EACH_VEC_ELT (tmp
, j
, rhsp
)
3498 results
->safe_push (*rhsp
);
3501 /* We do not know whether the constructor was complete,
3502 so technically we have to add &NOTHING or &ANYTHING
3503 like we do for an empty constructor as well. */
3510 case tcc_declaration
:
3512 get_constraint_for_ssa_var (t
, results
, address_p
);
3517 /* We cannot refer to automatic variables through constants. */
3518 temp
.type
= ADDRESSOF
;
3519 temp
.var
= nonlocal_id
;
3521 results
->safe_push (temp
);
3527 /* The default fallback is a constraint from anything. */
3528 temp
.type
= ADDRESSOF
;
3529 temp
.var
= anything_id
;
3531 results
->safe_push (temp
);
3534 /* Given a gimple tree T, return the constraint expression vector for it. */
3537 get_constraint_for (tree t
, vec
<ce_s
> *results
)
3539 gcc_assert (results
->length () == 0);
3541 get_constraint_for_1 (t
, results
, false, true);
3544 /* Given a gimple tree T, return the constraint expression vector for it
3545 to be used as the rhs of a constraint. */
3548 get_constraint_for_rhs (tree t
, vec
<ce_s
> *results
)
3550 gcc_assert (results
->length () == 0);
3552 get_constraint_for_1 (t
, results
, false, false);
3556 /* Efficiently generates constraints from all entries in *RHSC to all
3557 entries in *LHSC. */
3560 process_all_all_constraints (vec
<ce_s
> lhsc
,
3563 struct constraint_expr
*lhsp
, *rhsp
;
3566 if (lhsc
.length () <= 1 || rhsc
.length () <= 1)
3568 FOR_EACH_VEC_ELT (lhsc
, i
, lhsp
)
3569 FOR_EACH_VEC_ELT (rhsc
, j
, rhsp
)
3570 process_constraint (new_constraint (*lhsp
, *rhsp
));
3574 struct constraint_expr tmp
;
3575 tmp
= new_scalar_tmp_constraint_exp ("allalltmp");
3576 FOR_EACH_VEC_ELT (rhsc
, i
, rhsp
)
3577 process_constraint (new_constraint (tmp
, *rhsp
));
3578 FOR_EACH_VEC_ELT (lhsc
, i
, lhsp
)
3579 process_constraint (new_constraint (*lhsp
, tmp
));
3583 /* Handle aggregate copies by expanding into copies of the respective
3584 fields of the structures. */
3587 do_structure_copy (tree lhsop
, tree rhsop
)
3589 struct constraint_expr
*lhsp
, *rhsp
;
3590 auto_vec
<ce_s
> lhsc
;
3591 auto_vec
<ce_s
> rhsc
;
3594 get_constraint_for (lhsop
, &lhsc
);
3595 get_constraint_for_rhs (rhsop
, &rhsc
);
3598 if (lhsp
->type
== DEREF
3599 || (lhsp
->type
== ADDRESSOF
&& lhsp
->var
== anything_id
)
3600 || rhsp
->type
== DEREF
)
3602 if (lhsp
->type
== DEREF
)
3604 gcc_assert (lhsc
.length () == 1);
3605 lhsp
->offset
= UNKNOWN_OFFSET
;
3607 if (rhsp
->type
== DEREF
)
3609 gcc_assert (rhsc
.length () == 1);
3610 rhsp
->offset
= UNKNOWN_OFFSET
;
3612 process_all_all_constraints (lhsc
, rhsc
);
3614 else if (lhsp
->type
== SCALAR
3615 && (rhsp
->type
== SCALAR
3616 || rhsp
->type
== ADDRESSOF
))
3618 HOST_WIDE_INT lhssize
, lhsmaxsize
, lhsoffset
;
3619 HOST_WIDE_INT rhssize
, rhsmaxsize
, rhsoffset
;
3621 get_ref_base_and_extent (lhsop
, &lhsoffset
, &lhssize
, &lhsmaxsize
);
3622 get_ref_base_and_extent (rhsop
, &rhsoffset
, &rhssize
, &rhsmaxsize
);
3623 for (j
= 0; lhsc
.iterate (j
, &lhsp
);)
3625 varinfo_t lhsv
, rhsv
;
3627 lhsv
= get_varinfo (lhsp
->var
);
3628 rhsv
= get_varinfo (rhsp
->var
);
3629 if (lhsv
->may_have_pointers
3630 && (lhsv
->is_full_var
3631 || rhsv
->is_full_var
3632 || ranges_overlap_p (lhsv
->offset
+ rhsoffset
, lhsv
->size
,
3633 rhsv
->offset
+ lhsoffset
, rhsv
->size
)))
3634 process_constraint (new_constraint (*lhsp
, *rhsp
));
3635 if (!rhsv
->is_full_var
3636 && (lhsv
->is_full_var
3637 || (lhsv
->offset
+ rhsoffset
+ lhsv
->size
3638 > rhsv
->offset
+ lhsoffset
+ rhsv
->size
)))
3641 if (k
>= rhsc
.length ())
3652 /* Create constraints ID = { rhsc }. */
3655 make_constraints_to (unsigned id
, vec
<ce_s
> rhsc
)
3657 struct constraint_expr
*c
;
3658 struct constraint_expr includes
;
3662 includes
.offset
= 0;
3663 includes
.type
= SCALAR
;
3665 FOR_EACH_VEC_ELT (rhsc
, j
, c
)
3666 process_constraint (new_constraint (includes
, *c
));
3669 /* Create a constraint ID = OP. */
3672 make_constraint_to (unsigned id
, tree op
)
3674 auto_vec
<ce_s
> rhsc
;
3675 get_constraint_for_rhs (op
, &rhsc
);
3676 make_constraints_to (id
, rhsc
);
3679 /* Create a constraint ID = &FROM. */
3682 make_constraint_from (varinfo_t vi
, int from
)
3684 struct constraint_expr lhs
, rhs
;
3692 rhs
.type
= ADDRESSOF
;
3693 process_constraint (new_constraint (lhs
, rhs
));
3696 /* Create a constraint ID = FROM. */
3699 make_copy_constraint (varinfo_t vi
, int from
)
3701 struct constraint_expr lhs
, rhs
;
3710 process_constraint (new_constraint (lhs
, rhs
));
3713 /* Make constraints necessary to make OP escape. */
3716 make_escape_constraint (tree op
)
3718 make_constraint_to (escaped_id
, op
);
3721 /* Add constraints to that the solution of VI is transitively closed. */
3724 make_transitive_closure_constraints (varinfo_t vi
)
3726 struct constraint_expr lhs
, rhs
;
3734 rhs
.offset
= UNKNOWN_OFFSET
;
3735 process_constraint (new_constraint (lhs
, rhs
));
3738 /* Temporary storage for fake var decls. */
3739 struct obstack fake_var_decl_obstack
;
3741 /* Build a fake VAR_DECL acting as referrer to a DECL_UID. */
3744 build_fake_var_decl (tree type
)
3746 tree decl
= (tree
) XOBNEW (&fake_var_decl_obstack
, struct tree_var_decl
);
3747 memset (decl
, 0, sizeof (struct tree_var_decl
));
3748 TREE_SET_CODE (decl
, VAR_DECL
);
3749 TREE_TYPE (decl
) = type
;
3750 DECL_UID (decl
) = allocate_decl_uid ();
3751 SET_DECL_PT_UID (decl
, -1);
3752 layout_decl (decl
, 0);
3756 /* Create a new artificial heap variable with NAME.
3757 Return the created variable. */
3760 make_heapvar (const char *name
)
3765 heapvar
= build_fake_var_decl (ptr_type_node
);
3766 DECL_EXTERNAL (heapvar
) = 1;
3768 vi
= new_var_info (heapvar
, name
);
3769 vi
->is_artificial_var
= true;
3770 vi
->is_heap_var
= true;
3771 vi
->is_unknown_size_var
= true;
3775 vi
->is_full_var
= true;
3776 insert_vi_for_tree (heapvar
, vi
);
3781 /* Create a new artificial heap variable with NAME and make a
3782 constraint from it to LHS. Set flags according to a tag used
3783 for tracking restrict pointers. */
3786 make_constraint_from_restrict (varinfo_t lhs
, const char *name
)
3788 varinfo_t vi
= make_heapvar (name
);
3789 vi
->is_restrict_var
= 1;
3790 vi
->is_global_var
= 1;
3791 vi
->may_have_pointers
= 1;
3792 make_constraint_from (lhs
, vi
->id
);
3796 /* Create a new artificial heap variable with NAME and make a
3797 constraint from it to LHS. Set flags according to a tag used
3798 for tracking restrict pointers and make the artificial heap
3799 point to global memory. */
3802 make_constraint_from_global_restrict (varinfo_t lhs
, const char *name
)
3804 varinfo_t vi
= make_constraint_from_restrict (lhs
, name
);
3805 make_copy_constraint (vi
, nonlocal_id
);
3809 /* In IPA mode there are varinfos for different aspects of reach
3810 function designator. One for the points-to set of the return
3811 value, one for the variables that are clobbered by the function,
3812 one for its uses and one for each parameter (including a single
3813 glob for remaining variadic arguments). */
3815 enum { fi_clobbers
= 1, fi_uses
= 2,
3816 fi_static_chain
= 3, fi_result
= 4, fi_parm_base
= 5 };
3818 /* Get a constraint for the requested part of a function designator FI
3819 when operating in IPA mode. */
3821 static struct constraint_expr
3822 get_function_part_constraint (varinfo_t fi
, unsigned part
)
3824 struct constraint_expr c
;
3826 gcc_assert (in_ipa_mode
);
3828 if (fi
->id
== anything_id
)
3830 /* ??? We probably should have a ANYFN special variable. */
3831 c
.var
= anything_id
;
3835 else if (TREE_CODE (fi
->decl
) == FUNCTION_DECL
)
3837 varinfo_t ai
= first_vi_for_offset (fi
, part
);
3841 c
.var
= anything_id
;
3855 /* For non-IPA mode, generate constraints necessary for a call on the
3859 handle_rhs_call (gcall
*stmt
, vec
<ce_s
> *results
)
3861 struct constraint_expr rhsc
;
3863 bool returns_uses
= false;
3865 for (i
= 0; i
< gimple_call_num_args (stmt
); ++i
)
3867 tree arg
= gimple_call_arg (stmt
, i
);
3868 int flags
= gimple_call_arg_flags (stmt
, i
);
3870 /* If the argument is not used we can ignore it. */
3871 if (flags
& EAF_UNUSED
)
3874 /* As we compute ESCAPED context-insensitive we do not gain
3875 any precision with just EAF_NOCLOBBER but not EAF_NOESCAPE
3876 set. The argument would still get clobbered through the
3878 if ((flags
& EAF_NOCLOBBER
)
3879 && (flags
& EAF_NOESCAPE
))
3881 varinfo_t uses
= get_call_use_vi (stmt
);
3882 if (!(flags
& EAF_DIRECT
))
3884 varinfo_t tem
= new_var_info (NULL_TREE
, "callarg");
3885 make_constraint_to (tem
->id
, arg
);
3886 make_transitive_closure_constraints (tem
);
3887 make_copy_constraint (uses
, tem
->id
);
3890 make_constraint_to (uses
->id
, arg
);
3891 returns_uses
= true;
3893 else if (flags
& EAF_NOESCAPE
)
3895 struct constraint_expr lhs
, rhs
;
3896 varinfo_t uses
= get_call_use_vi (stmt
);
3897 varinfo_t clobbers
= get_call_clobber_vi (stmt
);
3898 varinfo_t tem
= new_var_info (NULL_TREE
, "callarg");
3899 make_constraint_to (tem
->id
, arg
);
3900 if (!(flags
& EAF_DIRECT
))
3901 make_transitive_closure_constraints (tem
);
3902 make_copy_constraint (uses
, tem
->id
);
3903 make_copy_constraint (clobbers
, tem
->id
);
3904 /* Add *tem = nonlocal, do not add *tem = callused as
3905 EAF_NOESCAPE parameters do not escape to other parameters
3906 and all other uses appear in NONLOCAL as well. */
3911 rhs
.var
= nonlocal_id
;
3913 process_constraint (new_constraint (lhs
, rhs
));
3914 returns_uses
= true;
3917 make_escape_constraint (arg
);
3920 /* If we added to the calls uses solution make sure we account for
3921 pointers to it to be returned. */
3924 rhsc
.var
= get_call_use_vi (stmt
)->id
;
3927 results
->safe_push (rhsc
);
3930 /* The static chain escapes as well. */
3931 if (gimple_call_chain (stmt
))
3932 make_escape_constraint (gimple_call_chain (stmt
));
3934 /* And if we applied NRV the address of the return slot escapes as well. */
3935 if (gimple_call_return_slot_opt_p (stmt
)
3936 && gimple_call_lhs (stmt
) != NULL_TREE
3937 && TREE_ADDRESSABLE (TREE_TYPE (gimple_call_lhs (stmt
))))
3939 auto_vec
<ce_s
> tmpc
;
3940 struct constraint_expr lhsc
, *c
;
3941 get_constraint_for_address_of (gimple_call_lhs (stmt
), &tmpc
);
3942 lhsc
.var
= escaped_id
;
3945 FOR_EACH_VEC_ELT (tmpc
, i
, c
)
3946 process_constraint (new_constraint (lhsc
, *c
));
3949 /* Regular functions return nonlocal memory. */
3950 rhsc
.var
= nonlocal_id
;
3953 results
->safe_push (rhsc
);
3956 /* For non-IPA mode, generate constraints necessary for a call
3957 that returns a pointer and assigns it to LHS. This simply makes
3958 the LHS point to global and escaped variables. */
3961 handle_lhs_call (gcall
*stmt
, tree lhs
, int flags
, vec
<ce_s
> rhsc
,
3964 auto_vec
<ce_s
> lhsc
;
3966 get_constraint_for (lhs
, &lhsc
);
3967 /* If the store is to a global decl make sure to
3968 add proper escape constraints. */
3969 lhs
= get_base_address (lhs
);
3972 && is_global_var (lhs
))
3974 struct constraint_expr tmpc
;
3975 tmpc
.var
= escaped_id
;
3978 lhsc
.safe_push (tmpc
);
3981 /* If the call returns an argument unmodified override the rhs
3983 if (flags
& ERF_RETURNS_ARG
3984 && (flags
& ERF_RETURN_ARG_MASK
) < gimple_call_num_args (stmt
))
3988 arg
= gimple_call_arg (stmt
, flags
& ERF_RETURN_ARG_MASK
);
3989 get_constraint_for (arg
, &rhsc
);
3990 process_all_all_constraints (lhsc
, rhsc
);
3993 else if (flags
& ERF_NOALIAS
)
3996 struct constraint_expr tmpc
;
3998 vi
= make_heapvar ("HEAP");
3999 /* We are marking allocated storage local, we deal with it becoming
4000 global by escaping and setting of vars_contains_escaped_heap. */
4001 DECL_EXTERNAL (vi
->decl
) = 0;
4002 vi
->is_global_var
= 0;
4003 /* If this is not a real malloc call assume the memory was
4004 initialized and thus may point to global memory. All
4005 builtin functions with the malloc attribute behave in a sane way. */
4007 || DECL_BUILT_IN_CLASS (fndecl
) != BUILT_IN_NORMAL
)
4008 make_constraint_from (vi
, nonlocal_id
);
4011 tmpc
.type
= ADDRESSOF
;
4012 rhsc
.safe_push (tmpc
);
4013 process_all_all_constraints (lhsc
, rhsc
);
4017 process_all_all_constraints (lhsc
, rhsc
);
4020 /* For non-IPA mode, generate constraints necessary for a call of a
4021 const function that returns a pointer in the statement STMT. */
4024 handle_const_call (gcall
*stmt
, vec
<ce_s
> *results
)
4026 struct constraint_expr rhsc
;
4029 /* Treat nested const functions the same as pure functions as far
4030 as the static chain is concerned. */
4031 if (gimple_call_chain (stmt
))
4033 varinfo_t uses
= get_call_use_vi (stmt
);
4034 make_transitive_closure_constraints (uses
);
4035 make_constraint_to (uses
->id
, gimple_call_chain (stmt
));
4036 rhsc
.var
= uses
->id
;
4039 results
->safe_push (rhsc
);
4042 /* May return arguments. */
4043 for (k
= 0; k
< gimple_call_num_args (stmt
); ++k
)
4045 tree arg
= gimple_call_arg (stmt
, k
);
4046 auto_vec
<ce_s
> argc
;
4048 struct constraint_expr
*argp
;
4049 get_constraint_for_rhs (arg
, &argc
);
4050 FOR_EACH_VEC_ELT (argc
, i
, argp
)
4051 results
->safe_push (*argp
);
4054 /* May return addresses of globals. */
4055 rhsc
.var
= nonlocal_id
;
4057 rhsc
.type
= ADDRESSOF
;
4058 results
->safe_push (rhsc
);
4061 /* For non-IPA mode, generate constraints necessary for a call to a
4062 pure function in statement STMT. */
4065 handle_pure_call (gcall
*stmt
, vec
<ce_s
> *results
)
4067 struct constraint_expr rhsc
;
4069 varinfo_t uses
= NULL
;
4071 /* Memory reached from pointer arguments is call-used. */
4072 for (i
= 0; i
< gimple_call_num_args (stmt
); ++i
)
4074 tree arg
= gimple_call_arg (stmt
, i
);
4077 uses
= get_call_use_vi (stmt
);
4078 make_transitive_closure_constraints (uses
);
4080 make_constraint_to (uses
->id
, arg
);
4083 /* The static chain is used as well. */
4084 if (gimple_call_chain (stmt
))
4088 uses
= get_call_use_vi (stmt
);
4089 make_transitive_closure_constraints (uses
);
4091 make_constraint_to (uses
->id
, gimple_call_chain (stmt
));
4094 /* Pure functions may return call-used and nonlocal memory. */
4097 rhsc
.var
= uses
->id
;
4100 results
->safe_push (rhsc
);
4102 rhsc
.var
= nonlocal_id
;
4105 results
->safe_push (rhsc
);
4109 /* Return the varinfo for the callee of CALL. */
4112 get_fi_for_callee (gcall
*call
)
4114 tree decl
, fn
= gimple_call_fn (call
);
4116 if (fn
&& TREE_CODE (fn
) == OBJ_TYPE_REF
)
4117 fn
= OBJ_TYPE_REF_EXPR (fn
);
4119 /* If we can directly resolve the function being called, do so.
4120 Otherwise, it must be some sort of indirect expression that
4121 we should still be able to handle. */
4122 decl
= gimple_call_addr_fndecl (fn
);
4124 return get_vi_for_tree (decl
);
4126 /* If the function is anything other than a SSA name pointer we have no
4127 clue and should be getting ANYFN (well, ANYTHING for now). */
4128 if (!fn
|| TREE_CODE (fn
) != SSA_NAME
)
4129 return get_varinfo (anything_id
);
4131 if (SSA_NAME_IS_DEFAULT_DEF (fn
)
4132 && (TREE_CODE (SSA_NAME_VAR (fn
)) == PARM_DECL
4133 || TREE_CODE (SSA_NAME_VAR (fn
)) == RESULT_DECL
))
4134 fn
= SSA_NAME_VAR (fn
);
4136 return get_vi_for_tree (fn
);
4139 /* Create constraints for the builtin call T. Return true if the call
4140 was handled, otherwise false. */
4143 find_func_aliases_for_builtin_call (struct function
*fn
, gcall
*t
)
4145 tree fndecl
= gimple_call_fndecl (t
);
4146 auto_vec
<ce_s
, 2> lhsc
;
4147 auto_vec
<ce_s
, 4> rhsc
;
4150 if (gimple_call_builtin_p (t
, BUILT_IN_NORMAL
))
4151 /* ??? All builtins that are handled here need to be handled
4152 in the alias-oracle query functions explicitly! */
4153 switch (DECL_FUNCTION_CODE (fndecl
))
4155 /* All the following functions return a pointer to the same object
4156 as their first argument points to. The functions do not add
4157 to the ESCAPED solution. The functions make the first argument
4158 pointed to memory point to what the second argument pointed to
4159 memory points to. */
4160 case BUILT_IN_STRCPY
:
4161 case BUILT_IN_STRNCPY
:
4162 case BUILT_IN_BCOPY
:
4163 case BUILT_IN_MEMCPY
:
4164 case BUILT_IN_MEMMOVE
:
4165 case BUILT_IN_MEMPCPY
:
4166 case BUILT_IN_STPCPY
:
4167 case BUILT_IN_STPNCPY
:
4168 case BUILT_IN_STRCAT
:
4169 case BUILT_IN_STRNCAT
:
4170 case BUILT_IN_STRCPY_CHK
:
4171 case BUILT_IN_STRNCPY_CHK
:
4172 case BUILT_IN_MEMCPY_CHK
:
4173 case BUILT_IN_MEMMOVE_CHK
:
4174 case BUILT_IN_MEMPCPY_CHK
:
4175 case BUILT_IN_STPCPY_CHK
:
4176 case BUILT_IN_STPNCPY_CHK
:
4177 case BUILT_IN_STRCAT_CHK
:
4178 case BUILT_IN_STRNCAT_CHK
:
4179 case BUILT_IN_TM_MEMCPY
:
4180 case BUILT_IN_TM_MEMMOVE
:
4182 tree res
= gimple_call_lhs (t
);
4183 tree dest
= gimple_call_arg (t
, (DECL_FUNCTION_CODE (fndecl
)
4184 == BUILT_IN_BCOPY
? 1 : 0));
4185 tree src
= gimple_call_arg (t
, (DECL_FUNCTION_CODE (fndecl
)
4186 == BUILT_IN_BCOPY
? 0 : 1));
4187 if (res
!= NULL_TREE
)
4189 get_constraint_for (res
, &lhsc
);
4190 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_MEMPCPY
4191 || DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_STPCPY
4192 || DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_STPNCPY
4193 || DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_MEMPCPY_CHK
4194 || DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_STPCPY_CHK
4195 || DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_STPNCPY_CHK
)
4196 get_constraint_for_ptr_offset (dest
, NULL_TREE
, &rhsc
);
4198 get_constraint_for (dest
, &rhsc
);
4199 process_all_all_constraints (lhsc
, rhsc
);
4203 get_constraint_for_ptr_offset (dest
, NULL_TREE
, &lhsc
);
4204 get_constraint_for_ptr_offset (src
, NULL_TREE
, &rhsc
);
4207 process_all_all_constraints (lhsc
, rhsc
);
4210 case BUILT_IN_MEMSET
:
4211 case BUILT_IN_MEMSET_CHK
:
4212 case BUILT_IN_TM_MEMSET
:
4214 tree res
= gimple_call_lhs (t
);
4215 tree dest
= gimple_call_arg (t
, 0);
4218 struct constraint_expr ac
;
4219 if (res
!= NULL_TREE
)
4221 get_constraint_for (res
, &lhsc
);
4222 get_constraint_for (dest
, &rhsc
);
4223 process_all_all_constraints (lhsc
, rhsc
);
4226 get_constraint_for_ptr_offset (dest
, NULL_TREE
, &lhsc
);
4228 if (flag_delete_null_pointer_checks
4229 && integer_zerop (gimple_call_arg (t
, 1)))
4231 ac
.type
= ADDRESSOF
;
4232 ac
.var
= nothing_id
;
4237 ac
.var
= integer_id
;
4240 FOR_EACH_VEC_ELT (lhsc
, i
, lhsp
)
4241 process_constraint (new_constraint (*lhsp
, ac
));
4244 case BUILT_IN_POSIX_MEMALIGN
:
4246 tree ptrptr
= gimple_call_arg (t
, 0);
4247 get_constraint_for (ptrptr
, &lhsc
);
4249 varinfo_t vi
= make_heapvar ("HEAP");
4250 /* We are marking allocated storage local, we deal with it becoming
4251 global by escaping and setting of vars_contains_escaped_heap. */
4252 DECL_EXTERNAL (vi
->decl
) = 0;
4253 vi
->is_global_var
= 0;
4254 struct constraint_expr tmpc
;
4257 tmpc
.type
= ADDRESSOF
;
4258 rhsc
.safe_push (tmpc
);
4259 process_all_all_constraints (lhsc
, rhsc
);
4262 case BUILT_IN_ASSUME_ALIGNED
:
4264 tree res
= gimple_call_lhs (t
);
4265 tree dest
= gimple_call_arg (t
, 0);
4266 if (res
!= NULL_TREE
)
4268 get_constraint_for (res
, &lhsc
);
4269 get_constraint_for (dest
, &rhsc
);
4270 process_all_all_constraints (lhsc
, rhsc
);
4274 /* All the following functions do not return pointers, do not
4275 modify the points-to sets of memory reachable from their
4276 arguments and do not add to the ESCAPED solution. */
4277 case BUILT_IN_SINCOS
:
4278 case BUILT_IN_SINCOSF
:
4279 case BUILT_IN_SINCOSL
:
4280 case BUILT_IN_FREXP
:
4281 case BUILT_IN_FREXPF
:
4282 case BUILT_IN_FREXPL
:
4283 case BUILT_IN_GAMMA_R
:
4284 case BUILT_IN_GAMMAF_R
:
4285 case BUILT_IN_GAMMAL_R
:
4286 case BUILT_IN_LGAMMA_R
:
4287 case BUILT_IN_LGAMMAF_R
:
4288 case BUILT_IN_LGAMMAL_R
:
4290 case BUILT_IN_MODFF
:
4291 case BUILT_IN_MODFL
:
4292 case BUILT_IN_REMQUO
:
4293 case BUILT_IN_REMQUOF
:
4294 case BUILT_IN_REMQUOL
:
4297 case BUILT_IN_STRDUP
:
4298 case BUILT_IN_STRNDUP
:
4299 case BUILT_IN_REALLOC
:
4300 if (gimple_call_lhs (t
))
4302 handle_lhs_call (t
, gimple_call_lhs (t
),
4303 gimple_call_return_flags (t
) | ERF_NOALIAS
,
4305 get_constraint_for_ptr_offset (gimple_call_lhs (t
),
4307 get_constraint_for_ptr_offset (gimple_call_arg (t
, 0),
4311 process_all_all_constraints (lhsc
, rhsc
);
4314 /* For realloc the resulting pointer can be equal to the
4315 argument as well. But only doing this wouldn't be
4316 correct because with ptr == 0 realloc behaves like malloc. */
4317 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_REALLOC
)
4319 get_constraint_for (gimple_call_lhs (t
), &lhsc
);
4320 get_constraint_for (gimple_call_arg (t
, 0), &rhsc
);
4321 process_all_all_constraints (lhsc
, rhsc
);
4326 /* String / character search functions return a pointer into the
4327 source string or NULL. */
4328 case BUILT_IN_INDEX
:
4329 case BUILT_IN_STRCHR
:
4330 case BUILT_IN_STRRCHR
:
4331 case BUILT_IN_MEMCHR
:
4332 case BUILT_IN_STRSTR
:
4333 case BUILT_IN_STRPBRK
:
4334 if (gimple_call_lhs (t
))
4336 tree src
= gimple_call_arg (t
, 0);
4337 get_constraint_for_ptr_offset (src
, NULL_TREE
, &rhsc
);
4338 constraint_expr nul
;
4339 nul
.var
= nothing_id
;
4341 nul
.type
= ADDRESSOF
;
4342 rhsc
.safe_push (nul
);
4343 get_constraint_for (gimple_call_lhs (t
), &lhsc
);
4344 process_all_all_constraints (lhsc
, rhsc
);
4347 /* Trampolines are special - they set up passing the static
4349 case BUILT_IN_INIT_TRAMPOLINE
:
4351 tree tramp
= gimple_call_arg (t
, 0);
4352 tree nfunc
= gimple_call_arg (t
, 1);
4353 tree frame
= gimple_call_arg (t
, 2);
4355 struct constraint_expr lhs
, *rhsp
;
4358 varinfo_t nfi
= NULL
;
4359 gcc_assert (TREE_CODE (nfunc
) == ADDR_EXPR
);
4360 nfi
= lookup_vi_for_tree (TREE_OPERAND (nfunc
, 0));
4363 lhs
= get_function_part_constraint (nfi
, fi_static_chain
);
4364 get_constraint_for (frame
, &rhsc
);
4365 FOR_EACH_VEC_ELT (rhsc
, i
, rhsp
)
4366 process_constraint (new_constraint (lhs
, *rhsp
));
4369 /* Make the frame point to the function for
4370 the trampoline adjustment call. */
4371 get_constraint_for (tramp
, &lhsc
);
4373 get_constraint_for (nfunc
, &rhsc
);
4374 process_all_all_constraints (lhsc
, rhsc
);
4379 /* Else fallthru to generic handling which will let
4380 the frame escape. */
4383 case BUILT_IN_ADJUST_TRAMPOLINE
:
4385 tree tramp
= gimple_call_arg (t
, 0);
4386 tree res
= gimple_call_lhs (t
);
4387 if (in_ipa_mode
&& res
)
4389 get_constraint_for (res
, &lhsc
);
4390 get_constraint_for (tramp
, &rhsc
);
4392 process_all_all_constraints (lhsc
, rhsc
);
4396 CASE_BUILT_IN_TM_STORE (1):
4397 CASE_BUILT_IN_TM_STORE (2):
4398 CASE_BUILT_IN_TM_STORE (4):
4399 CASE_BUILT_IN_TM_STORE (8):
4400 CASE_BUILT_IN_TM_STORE (FLOAT
):
4401 CASE_BUILT_IN_TM_STORE (DOUBLE
):
4402 CASE_BUILT_IN_TM_STORE (LDOUBLE
):
4403 CASE_BUILT_IN_TM_STORE (M64
):
4404 CASE_BUILT_IN_TM_STORE (M128
):
4405 CASE_BUILT_IN_TM_STORE (M256
):
4407 tree addr
= gimple_call_arg (t
, 0);
4408 tree src
= gimple_call_arg (t
, 1);
4410 get_constraint_for (addr
, &lhsc
);
4412 get_constraint_for (src
, &rhsc
);
4413 process_all_all_constraints (lhsc
, rhsc
);
4416 CASE_BUILT_IN_TM_LOAD (1):
4417 CASE_BUILT_IN_TM_LOAD (2):
4418 CASE_BUILT_IN_TM_LOAD (4):
4419 CASE_BUILT_IN_TM_LOAD (8):
4420 CASE_BUILT_IN_TM_LOAD (FLOAT
):
4421 CASE_BUILT_IN_TM_LOAD (DOUBLE
):
4422 CASE_BUILT_IN_TM_LOAD (LDOUBLE
):
4423 CASE_BUILT_IN_TM_LOAD (M64
):
4424 CASE_BUILT_IN_TM_LOAD (M128
):
4425 CASE_BUILT_IN_TM_LOAD (M256
):
4427 tree dest
= gimple_call_lhs (t
);
4428 tree addr
= gimple_call_arg (t
, 0);
4430 get_constraint_for (dest
, &lhsc
);
4431 get_constraint_for (addr
, &rhsc
);
4433 process_all_all_constraints (lhsc
, rhsc
);
4436 /* Variadic argument handling needs to be handled in IPA
4438 case BUILT_IN_VA_START
:
4440 tree valist
= gimple_call_arg (t
, 0);
4441 struct constraint_expr rhs
, *lhsp
;
4443 get_constraint_for (valist
, &lhsc
);
4445 /* The va_list gets access to pointers in variadic
4446 arguments. Which we know in the case of IPA analysis
4447 and otherwise are just all nonlocal variables. */
4450 fi
= lookup_vi_for_tree (fn
->decl
);
4451 rhs
= get_function_part_constraint (fi
, ~0);
4452 rhs
.type
= ADDRESSOF
;
4456 rhs
.var
= nonlocal_id
;
4457 rhs
.type
= ADDRESSOF
;
4460 FOR_EACH_VEC_ELT (lhsc
, i
, lhsp
)
4461 process_constraint (new_constraint (*lhsp
, rhs
));
4462 /* va_list is clobbered. */
4463 make_constraint_to (get_call_clobber_vi (t
)->id
, valist
);
4466 /* va_end doesn't have any effect that matters. */
4467 case BUILT_IN_VA_END
:
4469 /* Alternate return. Simply give up for now. */
4470 case BUILT_IN_RETURN
:
4474 || !(fi
= get_vi_for_tree (fn
->decl
)))
4475 make_constraint_from (get_varinfo (escaped_id
), anything_id
);
4476 else if (in_ipa_mode
4479 struct constraint_expr lhs
, rhs
;
4480 lhs
= get_function_part_constraint (fi
, fi_result
);
4481 rhs
.var
= anything_id
;
4484 process_constraint (new_constraint (lhs
, rhs
));
4488 /* printf-style functions may have hooks to set pointers to
4489 point to somewhere into the generated string. Leave them
4490 for a later exercise... */
4492 /* Fallthru to general call handling. */;
4498 /* Create constraints for the call T. */
4501 find_func_aliases_for_call (struct function
*fn
, gcall
*t
)
4503 tree fndecl
= gimple_call_fndecl (t
);
4506 if (fndecl
!= NULL_TREE
4507 && DECL_BUILT_IN (fndecl
)
4508 && find_func_aliases_for_builtin_call (fn
, t
))
4511 fi
= get_fi_for_callee (t
);
4513 || (fndecl
&& !fi
->is_fn_info
))
4515 auto_vec
<ce_s
, 16> rhsc
;
4516 int flags
= gimple_call_flags (t
);
4518 /* Const functions can return their arguments and addresses
4519 of global memory but not of escaped memory. */
4520 if (flags
& (ECF_CONST
|ECF_NOVOPS
))
4522 if (gimple_call_lhs (t
))
4523 handle_const_call (t
, &rhsc
);
4525 /* Pure functions can return addresses in and of memory
4526 reachable from their arguments, but they are not an escape
4527 point for reachable memory of their arguments. */
4528 else if (flags
& (ECF_PURE
|ECF_LOOPING_CONST_OR_PURE
))
4529 handle_pure_call (t
, &rhsc
);
4531 handle_rhs_call (t
, &rhsc
);
4532 if (gimple_call_lhs (t
))
4533 handle_lhs_call (t
, gimple_call_lhs (t
),
4534 gimple_call_return_flags (t
), rhsc
, fndecl
);
4538 auto_vec
<ce_s
, 2> rhsc
;
4542 /* Assign all the passed arguments to the appropriate incoming
4543 parameters of the function. */
4544 for (j
= 0; j
< gimple_call_num_args (t
); j
++)
4546 struct constraint_expr lhs
;
4547 struct constraint_expr
*rhsp
;
4548 tree arg
= gimple_call_arg (t
, j
);
4550 get_constraint_for_rhs (arg
, &rhsc
);
4551 lhs
= get_function_part_constraint (fi
, fi_parm_base
+ j
);
4552 while (rhsc
.length () != 0)
4554 rhsp
= &rhsc
.last ();
4555 process_constraint (new_constraint (lhs
, *rhsp
));
4560 /* If we are returning a value, assign it to the result. */
4561 lhsop
= gimple_call_lhs (t
);
4564 auto_vec
<ce_s
, 2> lhsc
;
4565 struct constraint_expr rhs
;
4566 struct constraint_expr
*lhsp
;
4568 get_constraint_for (lhsop
, &lhsc
);
4569 rhs
= get_function_part_constraint (fi
, fi_result
);
4571 && DECL_RESULT (fndecl
)
4572 && DECL_BY_REFERENCE (DECL_RESULT (fndecl
)))
4574 auto_vec
<ce_s
, 2> tem
;
4575 tem
.quick_push (rhs
);
4577 gcc_checking_assert (tem
.length () == 1);
4580 FOR_EACH_VEC_ELT (lhsc
, j
, lhsp
)
4581 process_constraint (new_constraint (*lhsp
, rhs
));
4584 /* If we pass the result decl by reference, honor that. */
4587 && DECL_RESULT (fndecl
)
4588 && DECL_BY_REFERENCE (DECL_RESULT (fndecl
)))
4590 struct constraint_expr lhs
;
4591 struct constraint_expr
*rhsp
;
4593 get_constraint_for_address_of (lhsop
, &rhsc
);
4594 lhs
= get_function_part_constraint (fi
, fi_result
);
4595 FOR_EACH_VEC_ELT (rhsc
, j
, rhsp
)
4596 process_constraint (new_constraint (lhs
, *rhsp
));
4600 /* If we use a static chain, pass it along. */
4601 if (gimple_call_chain (t
))
4603 struct constraint_expr lhs
;
4604 struct constraint_expr
*rhsp
;
4606 get_constraint_for (gimple_call_chain (t
), &rhsc
);
4607 lhs
= get_function_part_constraint (fi
, fi_static_chain
);
4608 FOR_EACH_VEC_ELT (rhsc
, j
, rhsp
)
4609 process_constraint (new_constraint (lhs
, *rhsp
));
4614 /* Walk statement T setting up aliasing constraints according to the
4615 references found in T. This function is the main part of the
4616 constraint builder. AI points to auxiliary alias information used
4617 when building alias sets and computing alias grouping heuristics. */
4620 find_func_aliases (struct function
*fn
, gimple origt
)
4623 auto_vec
<ce_s
, 16> lhsc
;
4624 auto_vec
<ce_s
, 16> rhsc
;
4625 struct constraint_expr
*c
;
4628 /* Now build constraints expressions. */
4629 if (gimple_code (t
) == GIMPLE_PHI
)
4634 /* For a phi node, assign all the arguments to
4636 get_constraint_for (gimple_phi_result (t
), &lhsc
);
4637 for (i
= 0; i
< gimple_phi_num_args (t
); i
++)
4639 tree strippedrhs
= PHI_ARG_DEF (t
, i
);
4641 STRIP_NOPS (strippedrhs
);
4642 get_constraint_for_rhs (gimple_phi_arg_def (t
, i
), &rhsc
);
4644 FOR_EACH_VEC_ELT (lhsc
, j
, c
)
4646 struct constraint_expr
*c2
;
4647 while (rhsc
.length () > 0)
4650 process_constraint (new_constraint (*c
, *c2
));
4656 /* In IPA mode, we need to generate constraints to pass call
4657 arguments through their calls. There are two cases,
4658 either a GIMPLE_CALL returning a value, or just a plain
4659 GIMPLE_CALL when we are not.
4661 In non-ipa mode, we need to generate constraints for each
4662 pointer passed by address. */
4663 else if (is_gimple_call (t
))
4664 find_func_aliases_for_call (fn
, as_a
<gcall
*> (t
));
4666 /* Otherwise, just a regular assignment statement. Only care about
4667 operations with pointer result, others are dealt with as escape
4668 points if they have pointer operands. */
4669 else if (is_gimple_assign (t
))
4671 /* Otherwise, just a regular assignment statement. */
4672 tree lhsop
= gimple_assign_lhs (t
);
4673 tree rhsop
= (gimple_num_ops (t
) == 2) ? gimple_assign_rhs1 (t
) : NULL
;
4675 if (rhsop
&& TREE_CLOBBER_P (rhsop
))
4676 /* Ignore clobbers, they don't actually store anything into
4679 else if (rhsop
&& AGGREGATE_TYPE_P (TREE_TYPE (lhsop
)))
4680 do_structure_copy (lhsop
, rhsop
);
4683 enum tree_code code
= gimple_assign_rhs_code (t
);
4685 get_constraint_for (lhsop
, &lhsc
);
4687 if (code
== POINTER_PLUS_EXPR
)
4688 get_constraint_for_ptr_offset (gimple_assign_rhs1 (t
),
4689 gimple_assign_rhs2 (t
), &rhsc
);
4690 else if (code
== BIT_AND_EXPR
4691 && TREE_CODE (gimple_assign_rhs2 (t
)) == INTEGER_CST
)
4693 /* Aligning a pointer via a BIT_AND_EXPR is offsetting
4694 the pointer. Handle it by offsetting it by UNKNOWN. */
4695 get_constraint_for_ptr_offset (gimple_assign_rhs1 (t
),
4698 else if ((CONVERT_EXPR_CODE_P (code
)
4699 && !(POINTER_TYPE_P (gimple_expr_type (t
))
4700 && !POINTER_TYPE_P (TREE_TYPE (rhsop
))))
4701 || gimple_assign_single_p (t
))
4702 get_constraint_for_rhs (rhsop
, &rhsc
);
4703 else if (code
== COND_EXPR
)
4705 /* The result is a merge of both COND_EXPR arms. */
4706 auto_vec
<ce_s
, 2> tmp
;
4707 struct constraint_expr
*rhsp
;
4709 get_constraint_for_rhs (gimple_assign_rhs2 (t
), &rhsc
);
4710 get_constraint_for_rhs (gimple_assign_rhs3 (t
), &tmp
);
4711 FOR_EACH_VEC_ELT (tmp
, i
, rhsp
)
4712 rhsc
.safe_push (*rhsp
);
4714 else if (truth_value_p (code
))
4715 /* Truth value results are not pointer (parts). Or at least
4716 very unreasonable obfuscation of a part. */
4720 /* All other operations are merges. */
4721 auto_vec
<ce_s
, 4> tmp
;
4722 struct constraint_expr
*rhsp
;
4724 get_constraint_for_rhs (gimple_assign_rhs1 (t
), &rhsc
);
4725 for (i
= 2; i
< gimple_num_ops (t
); ++i
)
4727 get_constraint_for_rhs (gimple_op (t
, i
), &tmp
);
4728 FOR_EACH_VEC_ELT (tmp
, j
, rhsp
)
4729 rhsc
.safe_push (*rhsp
);
4733 process_all_all_constraints (lhsc
, rhsc
);
4735 /* If there is a store to a global variable the rhs escapes. */
4736 if ((lhsop
= get_base_address (lhsop
)) != NULL_TREE
4738 && is_global_var (lhsop
)
4740 || DECL_EXTERNAL (lhsop
) || TREE_PUBLIC (lhsop
)))
4741 make_escape_constraint (rhsop
);
4743 /* Handle escapes through return. */
4744 else if (gimple_code (t
) == GIMPLE_RETURN
4745 && gimple_return_retval (as_a
<greturn
*> (t
)) != NULL_TREE
)
4747 greturn
*return_stmt
= as_a
<greturn
*> (t
);
4750 || !(fi
= get_vi_for_tree (fn
->decl
)))
4751 make_escape_constraint (gimple_return_retval (return_stmt
));
4752 else if (in_ipa_mode
4755 struct constraint_expr lhs
;
4756 struct constraint_expr
*rhsp
;
4759 lhs
= get_function_part_constraint (fi
, fi_result
);
4760 get_constraint_for_rhs (gimple_return_retval (return_stmt
), &rhsc
);
4761 FOR_EACH_VEC_ELT (rhsc
, i
, rhsp
)
4762 process_constraint (new_constraint (lhs
, *rhsp
));
4765 /* Handle asms conservatively by adding escape constraints to everything. */
4766 else if (gasm
*asm_stmt
= dyn_cast
<gasm
*> (t
))
4768 unsigned i
, noutputs
;
4769 const char **oconstraints
;
4770 const char *constraint
;
4771 bool allows_mem
, allows_reg
, is_inout
;
4773 noutputs
= gimple_asm_noutputs (asm_stmt
);
4774 oconstraints
= XALLOCAVEC (const char *, noutputs
);
4776 for (i
= 0; i
< noutputs
; ++i
)
4778 tree link
= gimple_asm_output_op (asm_stmt
, i
);
4779 tree op
= TREE_VALUE (link
);
4781 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link
)));
4782 oconstraints
[i
] = constraint
;
4783 parse_output_constraint (&constraint
, i
, 0, 0, &allows_mem
,
4784 &allows_reg
, &is_inout
);
4786 /* A memory constraint makes the address of the operand escape. */
4787 if (!allows_reg
&& allows_mem
)
4788 make_escape_constraint (build_fold_addr_expr (op
));
4790 /* The asm may read global memory, so outputs may point to
4791 any global memory. */
4794 auto_vec
<ce_s
, 2> lhsc
;
4795 struct constraint_expr rhsc
, *lhsp
;
4797 get_constraint_for (op
, &lhsc
);
4798 rhsc
.var
= nonlocal_id
;
4801 FOR_EACH_VEC_ELT (lhsc
, j
, lhsp
)
4802 process_constraint (new_constraint (*lhsp
, rhsc
));
4805 for (i
= 0; i
< gimple_asm_ninputs (asm_stmt
); ++i
)
4807 tree link
= gimple_asm_input_op (asm_stmt
, i
);
4808 tree op
= TREE_VALUE (link
);
4810 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link
)));
4812 parse_input_constraint (&constraint
, 0, 0, noutputs
, 0, oconstraints
,
4813 &allows_mem
, &allows_reg
);
4815 /* A memory constraint makes the address of the operand escape. */
4816 if (!allows_reg
&& allows_mem
)
4817 make_escape_constraint (build_fold_addr_expr (op
));
4818 /* Strictly we'd only need the constraint to ESCAPED if
4819 the asm clobbers memory, otherwise using something
4820 along the lines of per-call clobbers/uses would be enough. */
4822 make_escape_constraint (op
);
4828 /* Create a constraint adding to the clobber set of FI the memory
4829 pointed to by PTR. */
4832 process_ipa_clobber (varinfo_t fi
, tree ptr
)
4834 vec
<ce_s
> ptrc
= vNULL
;
4835 struct constraint_expr
*c
, lhs
;
4837 get_constraint_for_rhs (ptr
, &ptrc
);
4838 lhs
= get_function_part_constraint (fi
, fi_clobbers
);
4839 FOR_EACH_VEC_ELT (ptrc
, i
, c
)
4840 process_constraint (new_constraint (lhs
, *c
));
4844 /* Walk statement T setting up clobber and use constraints according to the
4845 references found in T. This function is a main part of the
4846 IPA constraint builder. */
4849 find_func_clobbers (struct function
*fn
, gimple origt
)
4852 auto_vec
<ce_s
, 16> lhsc
;
4853 auto_vec
<ce_s
, 16> rhsc
;
4856 /* Add constraints for clobbered/used in IPA mode.
4857 We are not interested in what automatic variables are clobbered
4858 or used as we only use the information in the caller to which
4859 they do not escape. */
4860 gcc_assert (in_ipa_mode
);
4862 /* If the stmt refers to memory in any way it better had a VUSE. */
4863 if (gimple_vuse (t
) == NULL_TREE
)
4866 /* We'd better have function information for the current function. */
4867 fi
= lookup_vi_for_tree (fn
->decl
);
4868 gcc_assert (fi
!= NULL
);
4870 /* Account for stores in assignments and calls. */
4871 if (gimple_vdef (t
) != NULL_TREE
4872 && gimple_has_lhs (t
))
4874 tree lhs
= gimple_get_lhs (t
);
4876 while (handled_component_p (tem
))
4877 tem
= TREE_OPERAND (tem
, 0);
4879 && !auto_var_in_fn_p (tem
, fn
->decl
))
4880 || INDIRECT_REF_P (tem
)
4881 || (TREE_CODE (tem
) == MEM_REF
4882 && !(TREE_CODE (TREE_OPERAND (tem
, 0)) == ADDR_EXPR
4884 (TREE_OPERAND (TREE_OPERAND (tem
, 0), 0), fn
->decl
))))
4886 struct constraint_expr lhsc
, *rhsp
;
4888 lhsc
= get_function_part_constraint (fi
, fi_clobbers
);
4889 get_constraint_for_address_of (lhs
, &rhsc
);
4890 FOR_EACH_VEC_ELT (rhsc
, i
, rhsp
)
4891 process_constraint (new_constraint (lhsc
, *rhsp
));
4896 /* Account for uses in assigments and returns. */
4897 if (gimple_assign_single_p (t
)
4898 || (gimple_code (t
) == GIMPLE_RETURN
4899 && gimple_return_retval (as_a
<greturn
*> (t
)) != NULL_TREE
))
4901 tree rhs
= (gimple_assign_single_p (t
)
4902 ? gimple_assign_rhs1 (t
)
4903 : gimple_return_retval (as_a
<greturn
*> (t
)));
4905 while (handled_component_p (tem
))
4906 tem
= TREE_OPERAND (tem
, 0);
4908 && !auto_var_in_fn_p (tem
, fn
->decl
))
4909 || INDIRECT_REF_P (tem
)
4910 || (TREE_CODE (tem
) == MEM_REF
4911 && !(TREE_CODE (TREE_OPERAND (tem
, 0)) == ADDR_EXPR
4913 (TREE_OPERAND (TREE_OPERAND (tem
, 0), 0), fn
->decl
))))
4915 struct constraint_expr lhs
, *rhsp
;
4917 lhs
= get_function_part_constraint (fi
, fi_uses
);
4918 get_constraint_for_address_of (rhs
, &rhsc
);
4919 FOR_EACH_VEC_ELT (rhsc
, i
, rhsp
)
4920 process_constraint (new_constraint (lhs
, *rhsp
));
4925 if (gcall
*call_stmt
= dyn_cast
<gcall
*> (t
))
4927 varinfo_t cfi
= NULL
;
4928 tree decl
= gimple_call_fndecl (t
);
4929 struct constraint_expr lhs
, rhs
;
4932 /* For builtins we do not have separate function info. For those
4933 we do not generate escapes for we have to generate clobbers/uses. */
4934 if (gimple_call_builtin_p (t
, BUILT_IN_NORMAL
))
4935 switch (DECL_FUNCTION_CODE (decl
))
4937 /* The following functions use and clobber memory pointed to
4938 by their arguments. */
4939 case BUILT_IN_STRCPY
:
4940 case BUILT_IN_STRNCPY
:
4941 case BUILT_IN_BCOPY
:
4942 case BUILT_IN_MEMCPY
:
4943 case BUILT_IN_MEMMOVE
:
4944 case BUILT_IN_MEMPCPY
:
4945 case BUILT_IN_STPCPY
:
4946 case BUILT_IN_STPNCPY
:
4947 case BUILT_IN_STRCAT
:
4948 case BUILT_IN_STRNCAT
:
4949 case BUILT_IN_STRCPY_CHK
:
4950 case BUILT_IN_STRNCPY_CHK
:
4951 case BUILT_IN_MEMCPY_CHK
:
4952 case BUILT_IN_MEMMOVE_CHK
:
4953 case BUILT_IN_MEMPCPY_CHK
:
4954 case BUILT_IN_STPCPY_CHK
:
4955 case BUILT_IN_STPNCPY_CHK
:
4956 case BUILT_IN_STRCAT_CHK
:
4957 case BUILT_IN_STRNCAT_CHK
:
4959 tree dest
= gimple_call_arg (t
, (DECL_FUNCTION_CODE (decl
)
4960 == BUILT_IN_BCOPY
? 1 : 0));
4961 tree src
= gimple_call_arg (t
, (DECL_FUNCTION_CODE (decl
)
4962 == BUILT_IN_BCOPY
? 0 : 1));
4964 struct constraint_expr
*rhsp
, *lhsp
;
4965 get_constraint_for_ptr_offset (dest
, NULL_TREE
, &lhsc
);
4966 lhs
= get_function_part_constraint (fi
, fi_clobbers
);
4967 FOR_EACH_VEC_ELT (lhsc
, i
, lhsp
)
4968 process_constraint (new_constraint (lhs
, *lhsp
));
4969 get_constraint_for_ptr_offset (src
, NULL_TREE
, &rhsc
);
4970 lhs
= get_function_part_constraint (fi
, fi_uses
);
4971 FOR_EACH_VEC_ELT (rhsc
, i
, rhsp
)
4972 process_constraint (new_constraint (lhs
, *rhsp
));
4975 /* The following function clobbers memory pointed to by
4977 case BUILT_IN_MEMSET
:
4978 case BUILT_IN_MEMSET_CHK
:
4979 case BUILT_IN_POSIX_MEMALIGN
:
4981 tree dest
= gimple_call_arg (t
, 0);
4984 get_constraint_for_ptr_offset (dest
, NULL_TREE
, &lhsc
);
4985 lhs
= get_function_part_constraint (fi
, fi_clobbers
);
4986 FOR_EACH_VEC_ELT (lhsc
, i
, lhsp
)
4987 process_constraint (new_constraint (lhs
, *lhsp
));
4990 /* The following functions clobber their second and third
4992 case BUILT_IN_SINCOS
:
4993 case BUILT_IN_SINCOSF
:
4994 case BUILT_IN_SINCOSL
:
4996 process_ipa_clobber (fi
, gimple_call_arg (t
, 1));
4997 process_ipa_clobber (fi
, gimple_call_arg (t
, 2));
5000 /* The following functions clobber their second argument. */
5001 case BUILT_IN_FREXP
:
5002 case BUILT_IN_FREXPF
:
5003 case BUILT_IN_FREXPL
:
5004 case BUILT_IN_LGAMMA_R
:
5005 case BUILT_IN_LGAMMAF_R
:
5006 case BUILT_IN_LGAMMAL_R
:
5007 case BUILT_IN_GAMMA_R
:
5008 case BUILT_IN_GAMMAF_R
:
5009 case BUILT_IN_GAMMAL_R
:
5011 case BUILT_IN_MODFF
:
5012 case BUILT_IN_MODFL
:
5014 process_ipa_clobber (fi
, gimple_call_arg (t
, 1));
5017 /* The following functions clobber their third argument. */
5018 case BUILT_IN_REMQUO
:
5019 case BUILT_IN_REMQUOF
:
5020 case BUILT_IN_REMQUOL
:
5022 process_ipa_clobber (fi
, gimple_call_arg (t
, 2));
5025 /* The following functions neither read nor clobber memory. */
5026 case BUILT_IN_ASSUME_ALIGNED
:
5029 /* Trampolines are of no interest to us. */
5030 case BUILT_IN_INIT_TRAMPOLINE
:
5031 case BUILT_IN_ADJUST_TRAMPOLINE
:
5033 case BUILT_IN_VA_START
:
5034 case BUILT_IN_VA_END
:
5036 /* printf-style functions may have hooks to set pointers to
5037 point to somewhere into the generated string. Leave them
5038 for a later exercise... */
5040 /* Fallthru to general call handling. */;
5043 /* Parameters passed by value are used. */
5044 lhs
= get_function_part_constraint (fi
, fi_uses
);
5045 for (i
= 0; i
< gimple_call_num_args (t
); i
++)
5047 struct constraint_expr
*rhsp
;
5048 tree arg
= gimple_call_arg (t
, i
);
5050 if (TREE_CODE (arg
) == SSA_NAME
5051 || is_gimple_min_invariant (arg
))
5054 get_constraint_for_address_of (arg
, &rhsc
);
5055 FOR_EACH_VEC_ELT (rhsc
, j
, rhsp
)
5056 process_constraint (new_constraint (lhs
, *rhsp
));
5060 /* Build constraints for propagating clobbers/uses along the
5062 cfi
= get_fi_for_callee (call_stmt
);
5063 if (cfi
->id
== anything_id
)
5065 if (gimple_vdef (t
))
5066 make_constraint_from (first_vi_for_offset (fi
, fi_clobbers
),
5068 make_constraint_from (first_vi_for_offset (fi
, fi_uses
),
5073 /* For callees without function info (that's external functions),
5074 ESCAPED is clobbered and used. */
5075 if (gimple_call_fndecl (t
)
5076 && !cfi
->is_fn_info
)
5080 if (gimple_vdef (t
))
5081 make_copy_constraint (first_vi_for_offset (fi
, fi_clobbers
),
5083 make_copy_constraint (first_vi_for_offset (fi
, fi_uses
), escaped_id
);
5085 /* Also honor the call statement use/clobber info. */
5086 if ((vi
= lookup_call_clobber_vi (call_stmt
)) != NULL
)
5087 make_copy_constraint (first_vi_for_offset (fi
, fi_clobbers
),
5089 if ((vi
= lookup_call_use_vi (call_stmt
)) != NULL
)
5090 make_copy_constraint (first_vi_for_offset (fi
, fi_uses
),
5095 /* Otherwise the caller clobbers and uses what the callee does.
5096 ??? This should use a new complex constraint that filters
5097 local variables of the callee. */
5098 if (gimple_vdef (t
))
5100 lhs
= get_function_part_constraint (fi
, fi_clobbers
);
5101 rhs
= get_function_part_constraint (cfi
, fi_clobbers
);
5102 process_constraint (new_constraint (lhs
, rhs
));
5104 lhs
= get_function_part_constraint (fi
, fi_uses
);
5105 rhs
= get_function_part_constraint (cfi
, fi_uses
);
5106 process_constraint (new_constraint (lhs
, rhs
));
5108 else if (gimple_code (t
) == GIMPLE_ASM
)
5110 /* ??? Ick. We can do better. */
5111 if (gimple_vdef (t
))
5112 make_constraint_from (first_vi_for_offset (fi
, fi_clobbers
),
5114 make_constraint_from (first_vi_for_offset (fi
, fi_uses
),
5120 /* Find the first varinfo in the same variable as START that overlaps with
5121 OFFSET. Return NULL if we can't find one. */
5124 first_vi_for_offset (varinfo_t start
, unsigned HOST_WIDE_INT offset
)
5126 /* If the offset is outside of the variable, bail out. */
5127 if (offset
>= start
->fullsize
)
5130 /* If we cannot reach offset from start, lookup the first field
5131 and start from there. */
5132 if (start
->offset
> offset
)
5133 start
= get_varinfo (start
->head
);
5137 /* We may not find a variable in the field list with the actual
5138 offset when we have glommed a structure to a variable.
5139 In that case, however, offset should still be within the size
5141 if (offset
>= start
->offset
5142 && (offset
- start
->offset
) < start
->size
)
5145 start
= vi_next (start
);
5151 /* Find the first varinfo in the same variable as START that overlaps with
5152 OFFSET. If there is no such varinfo the varinfo directly preceding
5153 OFFSET is returned. */
5156 first_or_preceding_vi_for_offset (varinfo_t start
,
5157 unsigned HOST_WIDE_INT offset
)
5159 /* If we cannot reach offset from start, lookup the first field
5160 and start from there. */
5161 if (start
->offset
> offset
)
5162 start
= get_varinfo (start
->head
);
5164 /* We may not find a variable in the field list with the actual
5165 offset when we have glommed a structure to a variable.
5166 In that case, however, offset should still be within the size
5168 If we got beyond the offset we look for return the field
5169 directly preceding offset which may be the last field. */
5171 && offset
>= start
->offset
5172 && !((offset
- start
->offset
) < start
->size
))
5173 start
= vi_next (start
);
5179 /* This structure is used during pushing fields onto the fieldstack
5180 to track the offset of the field, since bitpos_of_field gives it
5181 relative to its immediate containing type, and we want it relative
5182 to the ultimate containing object. */
5186 /* Offset from the base of the base containing object to this field. */
5187 HOST_WIDE_INT offset
;
5189 /* Size, in bits, of the field. */
5190 unsigned HOST_WIDE_INT size
;
5192 unsigned has_unknown_size
: 1;
5194 unsigned must_have_pointers
: 1;
5196 unsigned may_have_pointers
: 1;
5198 unsigned only_restrict_pointers
: 1;
5200 typedef struct fieldoff fieldoff_s
;
5203 /* qsort comparison function for two fieldoff's PA and PB */
5206 fieldoff_compare (const void *pa
, const void *pb
)
5208 const fieldoff_s
*foa
= (const fieldoff_s
*)pa
;
5209 const fieldoff_s
*fob
= (const fieldoff_s
*)pb
;
5210 unsigned HOST_WIDE_INT foasize
, fobsize
;
5212 if (foa
->offset
< fob
->offset
)
5214 else if (foa
->offset
> fob
->offset
)
5217 foasize
= foa
->size
;
5218 fobsize
= fob
->size
;
5219 if (foasize
< fobsize
)
5221 else if (foasize
> fobsize
)
5226 /* Sort a fieldstack according to the field offset and sizes. */
5228 sort_fieldstack (vec
<fieldoff_s
> fieldstack
)
5230 fieldstack
.qsort (fieldoff_compare
);
5233 /* Return true if T is a type that can have subvars. */
5236 type_can_have_subvars (const_tree t
)
5238 /* Aggregates without overlapping fields can have subvars. */
5239 return TREE_CODE (t
) == RECORD_TYPE
;
5242 /* Return true if V is a tree that we can have subvars for.
5243 Normally, this is any aggregate type. Also complex
5244 types which are not gimple registers can have subvars. */
5247 var_can_have_subvars (const_tree v
)
5249 /* Volatile variables should never have subvars. */
5250 if (TREE_THIS_VOLATILE (v
))
5253 /* Non decls or memory tags can never have subvars. */
5257 return type_can_have_subvars (TREE_TYPE (v
));
5260 /* Return true if T is a type that does contain pointers. */
5263 type_must_have_pointers (tree type
)
5265 if (POINTER_TYPE_P (type
))
5268 if (TREE_CODE (type
) == ARRAY_TYPE
)
5269 return type_must_have_pointers (TREE_TYPE (type
));
5271 /* A function or method can have pointers as arguments, so track
5272 those separately. */
5273 if (TREE_CODE (type
) == FUNCTION_TYPE
5274 || TREE_CODE (type
) == METHOD_TYPE
)
5281 field_must_have_pointers (tree t
)
5283 return type_must_have_pointers (TREE_TYPE (t
));
5286 /* Given a TYPE, and a vector of field offsets FIELDSTACK, push all
5287 the fields of TYPE onto fieldstack, recording their offsets along
5290 OFFSET is used to keep track of the offset in this entire
5291 structure, rather than just the immediately containing structure.
5292 Returns false if the caller is supposed to handle the field we
5296 push_fields_onto_fieldstack (tree type
, vec
<fieldoff_s
> *fieldstack
,
5297 HOST_WIDE_INT offset
)
5300 bool empty_p
= true;
5302 if (TREE_CODE (type
) != RECORD_TYPE
)
5305 /* If the vector of fields is growing too big, bail out early.
5306 Callers check for vec::length <= MAX_FIELDS_FOR_FIELD_SENSITIVE, make
5308 if (fieldstack
->length () > MAX_FIELDS_FOR_FIELD_SENSITIVE
)
5311 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
5312 if (TREE_CODE (field
) == FIELD_DECL
)
5315 HOST_WIDE_INT foff
= bitpos_of_field (field
);
5317 if (!var_can_have_subvars (field
)
5318 || TREE_CODE (TREE_TYPE (field
)) == QUAL_UNION_TYPE
5319 || TREE_CODE (TREE_TYPE (field
)) == UNION_TYPE
)
5321 else if (!push_fields_onto_fieldstack
5322 (TREE_TYPE (field
), fieldstack
, offset
+ foff
)
5323 && (DECL_SIZE (field
)
5324 && !integer_zerop (DECL_SIZE (field
))))
5325 /* Empty structures may have actual size, like in C++. So
5326 see if we didn't push any subfields and the size is
5327 nonzero, push the field onto the stack. */
5332 fieldoff_s
*pair
= NULL
;
5333 bool has_unknown_size
= false;
5334 bool must_have_pointers_p
;
5336 if (!fieldstack
->is_empty ())
5337 pair
= &fieldstack
->last ();
5339 /* If there isn't anything at offset zero, create sth. */
5341 && offset
+ foff
!= 0)
5343 fieldoff_s e
= {0, offset
+ foff
, false, false, false, false};
5344 pair
= fieldstack
->safe_push (e
);
5347 if (!DECL_SIZE (field
)
5348 || !tree_fits_uhwi_p (DECL_SIZE (field
)))
5349 has_unknown_size
= true;
5351 /* If adjacent fields do not contain pointers merge them. */
5352 must_have_pointers_p
= field_must_have_pointers (field
);
5354 && !has_unknown_size
5355 && !must_have_pointers_p
5356 && !pair
->must_have_pointers
5357 && !pair
->has_unknown_size
5358 && pair
->offset
+ (HOST_WIDE_INT
)pair
->size
== offset
+ foff
)
5360 pair
->size
+= tree_to_uhwi (DECL_SIZE (field
));
5365 e
.offset
= offset
+ foff
;
5366 e
.has_unknown_size
= has_unknown_size
;
5367 if (!has_unknown_size
)
5368 e
.size
= tree_to_uhwi (DECL_SIZE (field
));
5371 e
.must_have_pointers
= must_have_pointers_p
;
5372 e
.may_have_pointers
= true;
5373 e
.only_restrict_pointers
5374 = (!has_unknown_size
5375 && POINTER_TYPE_P (TREE_TYPE (field
))
5376 && TYPE_RESTRICT (TREE_TYPE (field
)));
5377 fieldstack
->safe_push (e
);
5387 /* Count the number of arguments DECL has, and set IS_VARARGS to true
5388 if it is a varargs function. */
5391 count_num_arguments (tree decl
, bool *is_varargs
)
5393 unsigned int num
= 0;
5396 /* Capture named arguments for K&R functions. They do not
5397 have a prototype and thus no TYPE_ARG_TYPES. */
5398 for (t
= DECL_ARGUMENTS (decl
); t
; t
= DECL_CHAIN (t
))
5401 /* Check if the function has variadic arguments. */
5402 for (t
= TYPE_ARG_TYPES (TREE_TYPE (decl
)); t
; t
= TREE_CHAIN (t
))
5403 if (TREE_VALUE (t
) == void_type_node
)
5411 /* Creation function node for DECL, using NAME, and return the index
5412 of the variable we've created for the function. */
5415 create_function_info_for (tree decl
, const char *name
)
5417 struct function
*fn
= DECL_STRUCT_FUNCTION (decl
);
5418 varinfo_t vi
, prev_vi
;
5421 bool is_varargs
= false;
5422 unsigned int num_args
= count_num_arguments (decl
, &is_varargs
);
5424 /* Create the variable info. */
5426 vi
= new_var_info (decl
, name
);
5429 vi
->fullsize
= fi_parm_base
+ num_args
;
5431 vi
->may_have_pointers
= false;
5434 insert_vi_for_tree (vi
->decl
, vi
);
5438 /* Create a variable for things the function clobbers and one for
5439 things the function uses. */
5441 varinfo_t clobbervi
, usevi
;
5442 const char *newname
;
5445 tempname
= xasprintf ("%s.clobber", name
);
5446 newname
= ggc_strdup (tempname
);
5449 clobbervi
= new_var_info (NULL
, newname
);
5450 clobbervi
->offset
= fi_clobbers
;
5451 clobbervi
->size
= 1;
5452 clobbervi
->fullsize
= vi
->fullsize
;
5453 clobbervi
->is_full_var
= true;
5454 clobbervi
->is_global_var
= false;
5455 gcc_assert (prev_vi
->offset
< clobbervi
->offset
);
5456 prev_vi
->next
= clobbervi
->id
;
5457 prev_vi
= clobbervi
;
5459 tempname
= xasprintf ("%s.use", name
);
5460 newname
= ggc_strdup (tempname
);
5463 usevi
= new_var_info (NULL
, newname
);
5464 usevi
->offset
= fi_uses
;
5466 usevi
->fullsize
= vi
->fullsize
;
5467 usevi
->is_full_var
= true;
5468 usevi
->is_global_var
= false;
5469 gcc_assert (prev_vi
->offset
< usevi
->offset
);
5470 prev_vi
->next
= usevi
->id
;
5474 /* And one for the static chain. */
5475 if (fn
->static_chain_decl
!= NULL_TREE
)
5478 const char *newname
;
5481 tempname
= xasprintf ("%s.chain", name
);
5482 newname
= ggc_strdup (tempname
);
5485 chainvi
= new_var_info (fn
->static_chain_decl
, newname
);
5486 chainvi
->offset
= fi_static_chain
;
5488 chainvi
->fullsize
= vi
->fullsize
;
5489 chainvi
->is_full_var
= true;
5490 chainvi
->is_global_var
= false;
5491 gcc_assert (prev_vi
->offset
< chainvi
->offset
);
5492 prev_vi
->next
= chainvi
->id
;
5494 insert_vi_for_tree (fn
->static_chain_decl
, chainvi
);
5497 /* Create a variable for the return var. */
5498 if (DECL_RESULT (decl
) != NULL
5499 || !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl
))))
5502 const char *newname
;
5504 tree resultdecl
= decl
;
5506 if (DECL_RESULT (decl
))
5507 resultdecl
= DECL_RESULT (decl
);
5509 tempname
= xasprintf ("%s.result", name
);
5510 newname
= ggc_strdup (tempname
);
5513 resultvi
= new_var_info (resultdecl
, newname
);
5514 resultvi
->offset
= fi_result
;
5516 resultvi
->fullsize
= vi
->fullsize
;
5517 resultvi
->is_full_var
= true;
5518 if (DECL_RESULT (decl
))
5519 resultvi
->may_have_pointers
= true;
5520 gcc_assert (prev_vi
->offset
< resultvi
->offset
);
5521 prev_vi
->next
= resultvi
->id
;
5523 if (DECL_RESULT (decl
))
5524 insert_vi_for_tree (DECL_RESULT (decl
), resultvi
);
5527 /* Set up variables for each argument. */
5528 arg
= DECL_ARGUMENTS (decl
);
5529 for (i
= 0; i
< num_args
; i
++)
5532 const char *newname
;
5534 tree argdecl
= decl
;
5539 tempname
= xasprintf ("%s.arg%d", name
, i
);
5540 newname
= ggc_strdup (tempname
);
5543 argvi
= new_var_info (argdecl
, newname
);
5544 argvi
->offset
= fi_parm_base
+ i
;
5546 argvi
->is_full_var
= true;
5547 argvi
->fullsize
= vi
->fullsize
;
5549 argvi
->may_have_pointers
= true;
5550 gcc_assert (prev_vi
->offset
< argvi
->offset
);
5551 prev_vi
->next
= argvi
->id
;
5555 insert_vi_for_tree (arg
, argvi
);
5556 arg
= DECL_CHAIN (arg
);
5560 /* Add one representative for all further args. */
5564 const char *newname
;
5568 tempname
= xasprintf ("%s.varargs", name
);
5569 newname
= ggc_strdup (tempname
);
5572 /* We need sth that can be pointed to for va_start. */
5573 decl
= build_fake_var_decl (ptr_type_node
);
5575 argvi
= new_var_info (decl
, newname
);
5576 argvi
->offset
= fi_parm_base
+ num_args
;
5578 argvi
->is_full_var
= true;
5579 argvi
->is_heap_var
= true;
5580 argvi
->fullsize
= vi
->fullsize
;
5581 gcc_assert (prev_vi
->offset
< argvi
->offset
);
5582 prev_vi
->next
= argvi
->id
;
5590 /* Return true if FIELDSTACK contains fields that overlap.
5591 FIELDSTACK is assumed to be sorted by offset. */
5594 check_for_overlaps (vec
<fieldoff_s
> fieldstack
)
5596 fieldoff_s
*fo
= NULL
;
5598 HOST_WIDE_INT lastoffset
= -1;
5600 FOR_EACH_VEC_ELT (fieldstack
, i
, fo
)
5602 if (fo
->offset
== lastoffset
)
5604 lastoffset
= fo
->offset
;
5609 /* Create a varinfo structure for NAME and DECL, and add it to VARMAP.
5610 This will also create any varinfo structures necessary for fields
5614 create_variable_info_for_1 (tree decl
, const char *name
)
5616 varinfo_t vi
, newvi
;
5617 tree decl_type
= TREE_TYPE (decl
);
5618 tree declsize
= DECL_P (decl
) ? DECL_SIZE (decl
) : TYPE_SIZE (decl_type
);
5619 auto_vec
<fieldoff_s
> fieldstack
;
5622 varpool_node
*vnode
;
5625 || !tree_fits_uhwi_p (declsize
))
5627 vi
= new_var_info (decl
, name
);
5631 vi
->is_unknown_size_var
= true;
5632 vi
->is_full_var
= true;
5633 vi
->may_have_pointers
= true;
5637 /* Collect field information. */
5638 if (use_field_sensitive
5639 && var_can_have_subvars (decl
)
5640 /* ??? Force us to not use subfields for global initializers
5641 in IPA mode. Else we'd have to parse arbitrary initializers. */
5643 && is_global_var (decl
)
5644 && (vnode
= varpool_node::get (decl
))
5645 && vnode
->get_constructor ()))
5647 fieldoff_s
*fo
= NULL
;
5648 bool notokay
= false;
5651 push_fields_onto_fieldstack (decl_type
, &fieldstack
, 0);
5653 for (i
= 0; !notokay
&& fieldstack
.iterate (i
, &fo
); i
++)
5654 if (fo
->has_unknown_size
5661 /* We can't sort them if we have a field with a variable sized type,
5662 which will make notokay = true. In that case, we are going to return
5663 without creating varinfos for the fields anyway, so sorting them is a
5667 sort_fieldstack (fieldstack
);
5668 /* Due to some C++ FE issues, like PR 22488, we might end up
5669 what appear to be overlapping fields even though they,
5670 in reality, do not overlap. Until the C++ FE is fixed,
5671 we will simply disable field-sensitivity for these cases. */
5672 notokay
= check_for_overlaps (fieldstack
);
5676 fieldstack
.release ();
5679 /* If we didn't end up collecting sub-variables create a full
5680 variable for the decl. */
5681 if (fieldstack
.length () <= 1
5682 || fieldstack
.length () > MAX_FIELDS_FOR_FIELD_SENSITIVE
)
5684 vi
= new_var_info (decl
, name
);
5686 vi
->may_have_pointers
= true;
5687 vi
->fullsize
= tree_to_uhwi (declsize
);
5688 vi
->size
= vi
->fullsize
;
5689 vi
->is_full_var
= true;
5690 fieldstack
.release ();
5694 vi
= new_var_info (decl
, name
);
5695 vi
->fullsize
= tree_to_uhwi (declsize
);
5696 for (i
= 0, newvi
= vi
;
5697 fieldstack
.iterate (i
, &fo
);
5698 ++i
, newvi
= vi_next (newvi
))
5700 const char *newname
= "NULL";
5706 = xasprintf ("%s." HOST_WIDE_INT_PRINT_DEC
5707 "+" HOST_WIDE_INT_PRINT_DEC
, name
,
5708 fo
->offset
, fo
->size
);
5709 newname
= ggc_strdup (tempname
);
5712 newvi
->name
= newname
;
5713 newvi
->offset
= fo
->offset
;
5714 newvi
->size
= fo
->size
;
5715 newvi
->fullsize
= vi
->fullsize
;
5716 newvi
->may_have_pointers
= fo
->may_have_pointers
;
5717 newvi
->only_restrict_pointers
= fo
->only_restrict_pointers
;
5718 if (i
+ 1 < fieldstack
.length ())
5720 varinfo_t tem
= new_var_info (decl
, name
);
5721 newvi
->next
= tem
->id
;
5730 create_variable_info_for (tree decl
, const char *name
)
5732 varinfo_t vi
= create_variable_info_for_1 (decl
, name
);
5733 unsigned int id
= vi
->id
;
5735 insert_vi_for_tree (decl
, vi
);
5737 if (TREE_CODE (decl
) != VAR_DECL
)
5740 /* Create initial constraints for globals. */
5741 for (; vi
; vi
= vi_next (vi
))
5743 if (!vi
->may_have_pointers
5744 || !vi
->is_global_var
)
5747 /* Mark global restrict qualified pointers. */
5748 if ((POINTER_TYPE_P (TREE_TYPE (decl
))
5749 && TYPE_RESTRICT (TREE_TYPE (decl
)))
5750 || vi
->only_restrict_pointers
)
5753 = make_constraint_from_global_restrict (vi
, "GLOBAL_RESTRICT");
5754 /* ??? For now exclude reads from globals as restrict sources
5755 if those are not (indirectly) from incoming parameters. */
5756 rvi
->is_restrict_var
= false;
5760 /* In non-IPA mode the initializer from nonlocal is all we need. */
5762 || DECL_HARD_REGISTER (decl
))
5763 make_copy_constraint (vi
, nonlocal_id
);
5765 /* In IPA mode parse the initializer and generate proper constraints
5769 varpool_node
*vnode
= varpool_node::get (decl
);
5771 /* For escaped variables initialize them from nonlocal. */
5772 if (!vnode
->all_refs_explicit_p ())
5773 make_copy_constraint (vi
, nonlocal_id
);
5775 /* If this is a global variable with an initializer and we are in
5776 IPA mode generate constraints for it. */
5777 if (vnode
->get_constructor ()
5778 && vnode
->definition
)
5780 auto_vec
<ce_s
> rhsc
;
5781 struct constraint_expr lhs
, *rhsp
;
5783 get_constraint_for_rhs (vnode
->get_constructor (), &rhsc
);
5787 FOR_EACH_VEC_ELT (rhsc
, i
, rhsp
)
5788 process_constraint (new_constraint (lhs
, *rhsp
));
5789 /* If this is a variable that escapes from the unit
5790 the initializer escapes as well. */
5791 if (!vnode
->all_refs_explicit_p ())
5793 lhs
.var
= escaped_id
;
5796 FOR_EACH_VEC_ELT (rhsc
, i
, rhsp
)
5797 process_constraint (new_constraint (lhs
, *rhsp
));
5806 /* Print out the points-to solution for VAR to FILE. */
5809 dump_solution_for_var (FILE *file
, unsigned int var
)
5811 varinfo_t vi
= get_varinfo (var
);
5815 /* Dump the solution for unified vars anyway, this avoids difficulties
5816 in scanning dumps in the testsuite. */
5817 fprintf (file
, "%s = { ", vi
->name
);
5818 vi
= get_varinfo (find (var
));
5819 EXECUTE_IF_SET_IN_BITMAP (vi
->solution
, 0, i
, bi
)
5820 fprintf (file
, "%s ", get_varinfo (i
)->name
);
5821 fprintf (file
, "}");
5823 /* But note when the variable was unified. */
5825 fprintf (file
, " same as %s", vi
->name
);
5827 fprintf (file
, "\n");
5830 /* Print the points-to solution for VAR to stderr. */
5833 debug_solution_for_var (unsigned int var
)
5835 dump_solution_for_var (stderr
, var
);
5838 /* Create varinfo structures for all of the variables in the
5839 function for intraprocedural mode. */
5842 intra_create_variable_infos (struct function
*fn
)
5846 /* For each incoming pointer argument arg, create the constraint ARG
5847 = NONLOCAL or a dummy variable if it is a restrict qualified
5848 passed-by-reference argument. */
5849 for (t
= DECL_ARGUMENTS (fn
->decl
); t
; t
= DECL_CHAIN (t
))
5851 varinfo_t p
= get_vi_for_tree (t
);
5853 /* For restrict qualified pointers to objects passed by
5854 reference build a real representative for the pointed-to object.
5855 Treat restrict qualified references the same. */
5856 if (TYPE_RESTRICT (TREE_TYPE (t
))
5857 && ((DECL_BY_REFERENCE (t
) && POINTER_TYPE_P (TREE_TYPE (t
)))
5858 || TREE_CODE (TREE_TYPE (t
)) == REFERENCE_TYPE
)
5859 && !type_contains_placeholder_p (TREE_TYPE (TREE_TYPE (t
))))
5861 struct constraint_expr lhsc
, rhsc
;
5863 tree heapvar
= build_fake_var_decl (TREE_TYPE (TREE_TYPE (t
)));
5864 DECL_EXTERNAL (heapvar
) = 1;
5865 vi
= create_variable_info_for_1 (heapvar
, "PARM_NOALIAS");
5866 vi
->is_restrict_var
= 1;
5867 insert_vi_for_tree (heapvar
, vi
);
5872 rhsc
.type
= ADDRESSOF
;
5874 process_constraint (new_constraint (lhsc
, rhsc
));
5875 for (; vi
; vi
= vi_next (vi
))
5876 if (vi
->may_have_pointers
)
5878 if (vi
->only_restrict_pointers
)
5879 make_constraint_from_global_restrict (vi
, "GLOBAL_RESTRICT");
5881 make_copy_constraint (vi
, nonlocal_id
);
5886 if (POINTER_TYPE_P (TREE_TYPE (t
))
5887 && TYPE_RESTRICT (TREE_TYPE (t
)))
5888 make_constraint_from_global_restrict (p
, "PARM_RESTRICT");
5891 for (; p
; p
= vi_next (p
))
5893 if (p
->only_restrict_pointers
)
5894 make_constraint_from_global_restrict (p
, "PARM_RESTRICT");
5895 else if (p
->may_have_pointers
)
5896 make_constraint_from (p
, nonlocal_id
);
5901 /* Add a constraint for a result decl that is passed by reference. */
5902 if (DECL_RESULT (fn
->decl
)
5903 && DECL_BY_REFERENCE (DECL_RESULT (fn
->decl
)))
5905 varinfo_t p
, result_vi
= get_vi_for_tree (DECL_RESULT (fn
->decl
));
5907 for (p
= result_vi
; p
; p
= vi_next (p
))
5908 make_constraint_from (p
, nonlocal_id
);
5911 /* Add a constraint for the incoming static chain parameter. */
5912 if (fn
->static_chain_decl
!= NULL_TREE
)
5914 varinfo_t p
, chain_vi
= get_vi_for_tree (fn
->static_chain_decl
);
5916 for (p
= chain_vi
; p
; p
= vi_next (p
))
5917 make_constraint_from (p
, nonlocal_id
);
5921 /* Structure used to put solution bitmaps in a hashtable so they can
5922 be shared among variables with the same points-to set. */
5924 typedef struct shared_bitmap_info
5928 } *shared_bitmap_info_t
;
5929 typedef const struct shared_bitmap_info
*const_shared_bitmap_info_t
;
5931 /* Shared_bitmap hashtable helpers. */
5933 struct shared_bitmap_hasher
: free_ptr_hash
<shared_bitmap_info
>
5935 static inline hashval_t
hash (const shared_bitmap_info
*);
5936 static inline bool equal (const shared_bitmap_info
*,
5937 const shared_bitmap_info
*);
5940 /* Hash function for a shared_bitmap_info_t */
5943 shared_bitmap_hasher::hash (const shared_bitmap_info
*bi
)
5945 return bi
->hashcode
;
5948 /* Equality function for two shared_bitmap_info_t's. */
5951 shared_bitmap_hasher::equal (const shared_bitmap_info
*sbi1
,
5952 const shared_bitmap_info
*sbi2
)
5954 return bitmap_equal_p (sbi1
->pt_vars
, sbi2
->pt_vars
);
5957 /* Shared_bitmap hashtable. */
5959 static hash_table
<shared_bitmap_hasher
> *shared_bitmap_table
;
5961 /* Lookup a bitmap in the shared bitmap hashtable, and return an already
5962 existing instance if there is one, NULL otherwise. */
5965 shared_bitmap_lookup (bitmap pt_vars
)
5967 shared_bitmap_info
**slot
;
5968 struct shared_bitmap_info sbi
;
5970 sbi
.pt_vars
= pt_vars
;
5971 sbi
.hashcode
= bitmap_hash (pt_vars
);
5973 slot
= shared_bitmap_table
->find_slot (&sbi
, NO_INSERT
);
5977 return (*slot
)->pt_vars
;
5981 /* Add a bitmap to the shared bitmap hashtable. */
5984 shared_bitmap_add (bitmap pt_vars
)
5986 shared_bitmap_info
**slot
;
5987 shared_bitmap_info_t sbi
= XNEW (struct shared_bitmap_info
);
5989 sbi
->pt_vars
= pt_vars
;
5990 sbi
->hashcode
= bitmap_hash (pt_vars
);
5992 slot
= shared_bitmap_table
->find_slot (sbi
, INSERT
);
5993 gcc_assert (!*slot
);
5998 /* Set bits in INTO corresponding to the variable uids in solution set FROM. */
6001 set_uids_in_ptset (bitmap into
, bitmap from
, struct pt_solution
*pt
)
6005 varinfo_t escaped_vi
= get_varinfo (find (escaped_id
));
6006 bool everything_escaped
6007 = escaped_vi
->solution
&& bitmap_bit_p (escaped_vi
->solution
, anything_id
);
6009 EXECUTE_IF_SET_IN_BITMAP (from
, 0, i
, bi
)
6011 varinfo_t vi
= get_varinfo (i
);
6013 /* The only artificial variables that are allowed in a may-alias
6014 set are heap variables. */
6015 if (vi
->is_artificial_var
&& !vi
->is_heap_var
)
6018 if (everything_escaped
6019 || (escaped_vi
->solution
6020 && bitmap_bit_p (escaped_vi
->solution
, i
)))
6022 pt
->vars_contains_escaped
= true;
6023 pt
->vars_contains_escaped_heap
= vi
->is_heap_var
;
6026 if (TREE_CODE (vi
->decl
) == VAR_DECL
6027 || TREE_CODE (vi
->decl
) == PARM_DECL
6028 || TREE_CODE (vi
->decl
) == RESULT_DECL
)
6030 /* If we are in IPA mode we will not recompute points-to
6031 sets after inlining so make sure they stay valid. */
6033 && !DECL_PT_UID_SET_P (vi
->decl
))
6034 SET_DECL_PT_UID (vi
->decl
, DECL_UID (vi
->decl
));
6036 /* Add the decl to the points-to set. Note that the points-to
6037 set contains global variables. */
6038 bitmap_set_bit (into
, DECL_PT_UID (vi
->decl
));
6039 if (vi
->is_global_var
)
6040 pt
->vars_contains_nonlocal
= true;
6046 /* Compute the points-to solution *PT for the variable VI. */
6048 static struct pt_solution
6049 find_what_var_points_to (varinfo_t orig_vi
)
6053 bitmap finished_solution
;
6056 struct pt_solution
*pt
;
6058 /* This variable may have been collapsed, let's get the real
6060 vi
= get_varinfo (find (orig_vi
->id
));
6062 /* See if we have already computed the solution and return it. */
6063 pt_solution
**slot
= &final_solutions
->get_or_insert (vi
);
6067 *slot
= pt
= XOBNEW (&final_solutions_obstack
, struct pt_solution
);
6068 memset (pt
, 0, sizeof (struct pt_solution
));
6070 /* Translate artificial variables into SSA_NAME_PTR_INFO
6072 EXECUTE_IF_SET_IN_BITMAP (vi
->solution
, 0, i
, bi
)
6074 varinfo_t vi
= get_varinfo (i
);
6076 if (vi
->is_artificial_var
)
6078 if (vi
->id
== nothing_id
)
6080 else if (vi
->id
== escaped_id
)
6083 pt
->ipa_escaped
= 1;
6086 /* Expand some special vars of ESCAPED in-place here. */
6087 varinfo_t evi
= get_varinfo (find (escaped_id
));
6088 if (bitmap_bit_p (evi
->solution
, nonlocal_id
))
6091 else if (vi
->id
== nonlocal_id
)
6093 else if (vi
->is_heap_var
)
6094 /* We represent heapvars in the points-to set properly. */
6096 else if (vi
->id
== string_id
)
6097 /* Nobody cares - STRING_CSTs are read-only entities. */
6099 else if (vi
->id
== anything_id
6100 || vi
->id
== integer_id
)
6105 /* Instead of doing extra work, simply do not create
6106 elaborate points-to information for pt_anything pointers. */
6110 /* Share the final set of variables when possible. */
6111 finished_solution
= BITMAP_GGC_ALLOC ();
6112 stats
.points_to_sets_created
++;
6114 set_uids_in_ptset (finished_solution
, vi
->solution
, pt
);
6115 result
= shared_bitmap_lookup (finished_solution
);
6118 shared_bitmap_add (finished_solution
);
6119 pt
->vars
= finished_solution
;
6124 bitmap_clear (finished_solution
);
6130 /* Given a pointer variable P, fill in its points-to set. */
6133 find_what_p_points_to (tree p
)
6135 struct ptr_info_def
*pi
;
6139 /* For parameters, get at the points-to set for the actual parm
6141 if (TREE_CODE (p
) == SSA_NAME
6142 && SSA_NAME_IS_DEFAULT_DEF (p
)
6143 && (TREE_CODE (SSA_NAME_VAR (p
)) == PARM_DECL
6144 || TREE_CODE (SSA_NAME_VAR (p
)) == RESULT_DECL
))
6145 lookup_p
= SSA_NAME_VAR (p
);
6147 vi
= lookup_vi_for_tree (lookup_p
);
6151 pi
= get_ptr_info (p
);
6152 pi
->pt
= find_what_var_points_to (vi
);
6156 /* Query statistics for points-to solutions. */
6159 unsigned HOST_WIDE_INT pt_solution_includes_may_alias
;
6160 unsigned HOST_WIDE_INT pt_solution_includes_no_alias
;
6161 unsigned HOST_WIDE_INT pt_solutions_intersect_may_alias
;
6162 unsigned HOST_WIDE_INT pt_solutions_intersect_no_alias
;
6166 dump_pta_stats (FILE *s
)
6168 fprintf (s
, "\nPTA query stats:\n");
6169 fprintf (s
, " pt_solution_includes: "
6170 HOST_WIDE_INT_PRINT_DEC
" disambiguations, "
6171 HOST_WIDE_INT_PRINT_DEC
" queries\n",
6172 pta_stats
.pt_solution_includes_no_alias
,
6173 pta_stats
.pt_solution_includes_no_alias
6174 + pta_stats
.pt_solution_includes_may_alias
);
6175 fprintf (s
, " pt_solutions_intersect: "
6176 HOST_WIDE_INT_PRINT_DEC
" disambiguations, "
6177 HOST_WIDE_INT_PRINT_DEC
" queries\n",
6178 pta_stats
.pt_solutions_intersect_no_alias
,
6179 pta_stats
.pt_solutions_intersect_no_alias
6180 + pta_stats
.pt_solutions_intersect_may_alias
);
6184 /* Reset the points-to solution *PT to a conservative default
6185 (point to anything). */
6188 pt_solution_reset (struct pt_solution
*pt
)
6190 memset (pt
, 0, sizeof (struct pt_solution
));
6191 pt
->anything
= true;
6194 /* Set the points-to solution *PT to point only to the variables
6195 in VARS. VARS_CONTAINS_GLOBAL specifies whether that contains
6196 global variables and VARS_CONTAINS_RESTRICT specifies whether
6197 it contains restrict tag variables. */
6200 pt_solution_set (struct pt_solution
*pt
, bitmap vars
,
6201 bool vars_contains_nonlocal
)
6203 memset (pt
, 0, sizeof (struct pt_solution
));
6205 pt
->vars_contains_nonlocal
= vars_contains_nonlocal
;
6206 pt
->vars_contains_escaped
6207 = (cfun
->gimple_df
->escaped
.anything
6208 || bitmap_intersect_p (cfun
->gimple_df
->escaped
.vars
, vars
));
6211 /* Set the points-to solution *PT to point only to the variable VAR. */
6214 pt_solution_set_var (struct pt_solution
*pt
, tree var
)
6216 memset (pt
, 0, sizeof (struct pt_solution
));
6217 pt
->vars
= BITMAP_GGC_ALLOC ();
6218 bitmap_set_bit (pt
->vars
, DECL_PT_UID (var
));
6219 pt
->vars_contains_nonlocal
= is_global_var (var
);
6220 pt
->vars_contains_escaped
6221 = (cfun
->gimple_df
->escaped
.anything
6222 || bitmap_bit_p (cfun
->gimple_df
->escaped
.vars
, DECL_PT_UID (var
)));
6225 /* Computes the union of the points-to solutions *DEST and *SRC and
6226 stores the result in *DEST. This changes the points-to bitmap
6227 of *DEST and thus may not be used if that might be shared.
6228 The points-to bitmap of *SRC and *DEST will not be shared after
6229 this function if they were not before. */
6232 pt_solution_ior_into (struct pt_solution
*dest
, struct pt_solution
*src
)
6234 dest
->anything
|= src
->anything
;
6237 pt_solution_reset (dest
);
6241 dest
->nonlocal
|= src
->nonlocal
;
6242 dest
->escaped
|= src
->escaped
;
6243 dest
->ipa_escaped
|= src
->ipa_escaped
;
6244 dest
->null
|= src
->null
;
6245 dest
->vars_contains_nonlocal
|= src
->vars_contains_nonlocal
;
6246 dest
->vars_contains_escaped
|= src
->vars_contains_escaped
;
6247 dest
->vars_contains_escaped_heap
|= src
->vars_contains_escaped_heap
;
6252 dest
->vars
= BITMAP_GGC_ALLOC ();
6253 bitmap_ior_into (dest
->vars
, src
->vars
);
6256 /* Return true if the points-to solution *PT is empty. */
6259 pt_solution_empty_p (struct pt_solution
*pt
)
6266 && !bitmap_empty_p (pt
->vars
))
6269 /* If the solution includes ESCAPED, check if that is empty. */
6271 && !pt_solution_empty_p (&cfun
->gimple_df
->escaped
))
6274 /* If the solution includes ESCAPED, check if that is empty. */
6276 && !pt_solution_empty_p (&ipa_escaped_pt
))
6282 /* Return true if the points-to solution *PT only point to a single var, and
6283 return the var uid in *UID. */
6286 pt_solution_singleton_p (struct pt_solution
*pt
, unsigned *uid
)
6288 if (pt
->anything
|| pt
->nonlocal
|| pt
->escaped
|| pt
->ipa_escaped
6289 || pt
->null
|| pt
->vars
== NULL
6290 || !bitmap_single_bit_set_p (pt
->vars
))
6293 *uid
= bitmap_first_set_bit (pt
->vars
);
6297 /* Return true if the points-to solution *PT includes global memory. */
6300 pt_solution_includes_global (struct pt_solution
*pt
)
6304 || pt
->vars_contains_nonlocal
6305 /* The following is a hack to make the malloc escape hack work.
6306 In reality we'd need different sets for escaped-through-return
6307 and escaped-to-callees and passes would need to be updated. */
6308 || pt
->vars_contains_escaped_heap
)
6311 /* 'escaped' is also a placeholder so we have to look into it. */
6313 return pt_solution_includes_global (&cfun
->gimple_df
->escaped
);
6315 if (pt
->ipa_escaped
)
6316 return pt_solution_includes_global (&ipa_escaped_pt
);
6318 /* ??? This predicate is not correct for the IPA-PTA solution
6319 as we do not properly distinguish between unit escape points
6320 and global variables. */
6321 if (cfun
->gimple_df
->ipa_pta
)
6327 /* Return true if the points-to solution *PT includes the variable
6328 declaration DECL. */
6331 pt_solution_includes_1 (struct pt_solution
*pt
, const_tree decl
)
6337 && is_global_var (decl
))
6341 && bitmap_bit_p (pt
->vars
, DECL_PT_UID (decl
)))
6344 /* If the solution includes ESCAPED, check it. */
6346 && pt_solution_includes_1 (&cfun
->gimple_df
->escaped
, decl
))
6349 /* If the solution includes ESCAPED, check it. */
6351 && pt_solution_includes_1 (&ipa_escaped_pt
, decl
))
6358 pt_solution_includes (struct pt_solution
*pt
, const_tree decl
)
6360 bool res
= pt_solution_includes_1 (pt
, decl
);
6362 ++pta_stats
.pt_solution_includes_may_alias
;
6364 ++pta_stats
.pt_solution_includes_no_alias
;
6368 /* Return true if both points-to solutions PT1 and PT2 have a non-empty
6372 pt_solutions_intersect_1 (struct pt_solution
*pt1
, struct pt_solution
*pt2
)
6374 if (pt1
->anything
|| pt2
->anything
)
6377 /* If either points to unknown global memory and the other points to
6378 any global memory they alias. */
6381 || pt2
->vars_contains_nonlocal
))
6383 && pt1
->vars_contains_nonlocal
))
6386 /* If either points to all escaped memory and the other points to
6387 any escaped memory they alias. */
6390 || pt2
->vars_contains_escaped
))
6392 && pt1
->vars_contains_escaped
))
6395 /* Check the escaped solution if required.
6396 ??? Do we need to check the local against the IPA escaped sets? */
6397 if ((pt1
->ipa_escaped
|| pt2
->ipa_escaped
)
6398 && !pt_solution_empty_p (&ipa_escaped_pt
))
6400 /* If both point to escaped memory and that solution
6401 is not empty they alias. */
6402 if (pt1
->ipa_escaped
&& pt2
->ipa_escaped
)
6405 /* If either points to escaped memory see if the escaped solution
6406 intersects with the other. */
6407 if ((pt1
->ipa_escaped
6408 && pt_solutions_intersect_1 (&ipa_escaped_pt
, pt2
))
6409 || (pt2
->ipa_escaped
6410 && pt_solutions_intersect_1 (&ipa_escaped_pt
, pt1
)))
6414 /* Now both pointers alias if their points-to solution intersects. */
6417 && bitmap_intersect_p (pt1
->vars
, pt2
->vars
));
6421 pt_solutions_intersect (struct pt_solution
*pt1
, struct pt_solution
*pt2
)
6423 bool res
= pt_solutions_intersect_1 (pt1
, pt2
);
6425 ++pta_stats
.pt_solutions_intersect_may_alias
;
6427 ++pta_stats
.pt_solutions_intersect_no_alias
;
6432 /* Dump points-to information to OUTFILE. */
6435 dump_sa_points_to_info (FILE *outfile
)
6439 fprintf (outfile
, "\nPoints-to sets\n\n");
6441 if (dump_flags
& TDF_STATS
)
6443 fprintf (outfile
, "Stats:\n");
6444 fprintf (outfile
, "Total vars: %d\n", stats
.total_vars
);
6445 fprintf (outfile
, "Non-pointer vars: %d\n",
6446 stats
.nonpointer_vars
);
6447 fprintf (outfile
, "Statically unified vars: %d\n",
6448 stats
.unified_vars_static
);
6449 fprintf (outfile
, "Dynamically unified vars: %d\n",
6450 stats
.unified_vars_dynamic
);
6451 fprintf (outfile
, "Iterations: %d\n", stats
.iterations
);
6452 fprintf (outfile
, "Number of edges: %d\n", stats
.num_edges
);
6453 fprintf (outfile
, "Number of implicit edges: %d\n",
6454 stats
.num_implicit_edges
);
6457 for (i
= 1; i
< varmap
.length (); i
++)
6459 varinfo_t vi
= get_varinfo (i
);
6460 if (!vi
->may_have_pointers
)
6462 dump_solution_for_var (outfile
, i
);
6467 /* Debug points-to information to stderr. */
6470 debug_sa_points_to_info (void)
6472 dump_sa_points_to_info (stderr
);
6476 /* Initialize the always-existing constraint variables for NULL
6477 ANYTHING, READONLY, and INTEGER */
6480 init_base_vars (void)
6482 struct constraint_expr lhs
, rhs
;
6483 varinfo_t var_anything
;
6484 varinfo_t var_nothing
;
6485 varinfo_t var_string
;
6486 varinfo_t var_escaped
;
6487 varinfo_t var_nonlocal
;
6488 varinfo_t var_storedanything
;
6489 varinfo_t var_integer
;
6491 /* Variable ID zero is reserved and should be NULL. */
6492 varmap
.safe_push (NULL
);
6494 /* Create the NULL variable, used to represent that a variable points
6496 var_nothing
= new_var_info (NULL_TREE
, "NULL");
6497 gcc_assert (var_nothing
->id
== nothing_id
);
6498 var_nothing
->is_artificial_var
= 1;
6499 var_nothing
->offset
= 0;
6500 var_nothing
->size
= ~0;
6501 var_nothing
->fullsize
= ~0;
6502 var_nothing
->is_special_var
= 1;
6503 var_nothing
->may_have_pointers
= 0;
6504 var_nothing
->is_global_var
= 0;
6506 /* Create the ANYTHING variable, used to represent that a variable
6507 points to some unknown piece of memory. */
6508 var_anything
= new_var_info (NULL_TREE
, "ANYTHING");
6509 gcc_assert (var_anything
->id
== anything_id
);
6510 var_anything
->is_artificial_var
= 1;
6511 var_anything
->size
= ~0;
6512 var_anything
->offset
= 0;
6513 var_anything
->fullsize
= ~0;
6514 var_anything
->is_special_var
= 1;
6516 /* Anything points to anything. This makes deref constraints just
6517 work in the presence of linked list and other p = *p type loops,
6518 by saying that *ANYTHING = ANYTHING. */
6520 lhs
.var
= anything_id
;
6522 rhs
.type
= ADDRESSOF
;
6523 rhs
.var
= anything_id
;
6526 /* This specifically does not use process_constraint because
6527 process_constraint ignores all anything = anything constraints, since all
6528 but this one are redundant. */
6529 constraints
.safe_push (new_constraint (lhs
, rhs
));
6531 /* Create the STRING variable, used to represent that a variable
6532 points to a string literal. String literals don't contain
6533 pointers so STRING doesn't point to anything. */
6534 var_string
= new_var_info (NULL_TREE
, "STRING");
6535 gcc_assert (var_string
->id
== string_id
);
6536 var_string
->is_artificial_var
= 1;
6537 var_string
->offset
= 0;
6538 var_string
->size
= ~0;
6539 var_string
->fullsize
= ~0;
6540 var_string
->is_special_var
= 1;
6541 var_string
->may_have_pointers
= 0;
6543 /* Create the ESCAPED variable, used to represent the set of escaped
6545 var_escaped
= new_var_info (NULL_TREE
, "ESCAPED");
6546 gcc_assert (var_escaped
->id
== escaped_id
);
6547 var_escaped
->is_artificial_var
= 1;
6548 var_escaped
->offset
= 0;
6549 var_escaped
->size
= ~0;
6550 var_escaped
->fullsize
= ~0;
6551 var_escaped
->is_special_var
= 0;
6553 /* Create the NONLOCAL variable, used to represent the set of nonlocal
6555 var_nonlocal
= new_var_info (NULL_TREE
, "NONLOCAL");
6556 gcc_assert (var_nonlocal
->id
== nonlocal_id
);
6557 var_nonlocal
->is_artificial_var
= 1;
6558 var_nonlocal
->offset
= 0;
6559 var_nonlocal
->size
= ~0;
6560 var_nonlocal
->fullsize
= ~0;
6561 var_nonlocal
->is_special_var
= 1;
6563 /* ESCAPED = *ESCAPED, because escaped is may-deref'd at calls, etc. */
6565 lhs
.var
= escaped_id
;
6568 rhs
.var
= escaped_id
;
6570 process_constraint (new_constraint (lhs
, rhs
));
6572 /* ESCAPED = ESCAPED + UNKNOWN_OFFSET, because if a sub-field escapes the
6573 whole variable escapes. */
6575 lhs
.var
= escaped_id
;
6578 rhs
.var
= escaped_id
;
6579 rhs
.offset
= UNKNOWN_OFFSET
;
6580 process_constraint (new_constraint (lhs
, rhs
));
6582 /* *ESCAPED = NONLOCAL. This is true because we have to assume
6583 everything pointed to by escaped points to what global memory can
6586 lhs
.var
= escaped_id
;
6589 rhs
.var
= nonlocal_id
;
6591 process_constraint (new_constraint (lhs
, rhs
));
6593 /* NONLOCAL = &NONLOCAL, NONLOCAL = &ESCAPED. This is true because
6594 global memory may point to global memory and escaped memory. */
6596 lhs
.var
= nonlocal_id
;
6598 rhs
.type
= ADDRESSOF
;
6599 rhs
.var
= nonlocal_id
;
6601 process_constraint (new_constraint (lhs
, rhs
));
6602 rhs
.type
= ADDRESSOF
;
6603 rhs
.var
= escaped_id
;
6605 process_constraint (new_constraint (lhs
, rhs
));
6607 /* Create the STOREDANYTHING variable, used to represent the set of
6608 variables stored to *ANYTHING. */
6609 var_storedanything
= new_var_info (NULL_TREE
, "STOREDANYTHING");
6610 gcc_assert (var_storedanything
->id
== storedanything_id
);
6611 var_storedanything
->is_artificial_var
= 1;
6612 var_storedanything
->offset
= 0;
6613 var_storedanything
->size
= ~0;
6614 var_storedanything
->fullsize
= ~0;
6615 var_storedanything
->is_special_var
= 0;
6617 /* Create the INTEGER variable, used to represent that a variable points
6618 to what an INTEGER "points to". */
6619 var_integer
= new_var_info (NULL_TREE
, "INTEGER");
6620 gcc_assert (var_integer
->id
== integer_id
);
6621 var_integer
->is_artificial_var
= 1;
6622 var_integer
->size
= ~0;
6623 var_integer
->fullsize
= ~0;
6624 var_integer
->offset
= 0;
6625 var_integer
->is_special_var
= 1;
6627 /* INTEGER = ANYTHING, because we don't know where a dereference of
6628 a random integer will point to. */
6630 lhs
.var
= integer_id
;
6632 rhs
.type
= ADDRESSOF
;
6633 rhs
.var
= anything_id
;
6635 process_constraint (new_constraint (lhs
, rhs
));
6638 /* Initialize things necessary to perform PTA */
6641 init_alias_vars (void)
6643 use_field_sensitive
= (MAX_FIELDS_FOR_FIELD_SENSITIVE
> 1);
6645 bitmap_obstack_initialize (&pta_obstack
);
6646 bitmap_obstack_initialize (&oldpta_obstack
);
6647 bitmap_obstack_initialize (&predbitmap_obstack
);
6649 constraints
.create (8);
6651 vi_for_tree
= new hash_map
<tree
, varinfo_t
>;
6652 call_stmt_vars
= new hash_map
<gimple
, varinfo_t
>;
6654 memset (&stats
, 0, sizeof (stats
));
6655 shared_bitmap_table
= new hash_table
<shared_bitmap_hasher
> (511);
6658 gcc_obstack_init (&fake_var_decl_obstack
);
6660 final_solutions
= new hash_map
<varinfo_t
, pt_solution
*>;
6661 gcc_obstack_init (&final_solutions_obstack
);
6664 /* Remove the REF and ADDRESS edges from GRAPH, as well as all the
6665 predecessor edges. */
6668 remove_preds_and_fake_succs (constraint_graph_t graph
)
6672 /* Clear the implicit ref and address nodes from the successor
6674 for (i
= 1; i
< FIRST_REF_NODE
; i
++)
6676 if (graph
->succs
[i
])
6677 bitmap_clear_range (graph
->succs
[i
], FIRST_REF_NODE
,
6678 FIRST_REF_NODE
* 2);
6681 /* Free the successor list for the non-ref nodes. */
6682 for (i
= FIRST_REF_NODE
+ 1; i
< graph
->size
; i
++)
6684 if (graph
->succs
[i
])
6685 BITMAP_FREE (graph
->succs
[i
]);
6688 /* Now reallocate the size of the successor list as, and blow away
6689 the predecessor bitmaps. */
6690 graph
->size
= varmap
.length ();
6691 graph
->succs
= XRESIZEVEC (bitmap
, graph
->succs
, graph
->size
);
6693 free (graph
->implicit_preds
);
6694 graph
->implicit_preds
= NULL
;
6695 free (graph
->preds
);
6696 graph
->preds
= NULL
;
6697 bitmap_obstack_release (&predbitmap_obstack
);
6700 /* Solve the constraint set. */
6703 solve_constraints (void)
6705 struct scc_info
*si
;
6709 "\nCollapsing static cycles and doing variable "
6712 init_graph (varmap
.length () * 2);
6715 fprintf (dump_file
, "Building predecessor graph\n");
6716 build_pred_graph ();
6719 fprintf (dump_file
, "Detecting pointer and location "
6721 si
= perform_var_substitution (graph
);
6724 fprintf (dump_file
, "Rewriting constraints and unifying "
6726 rewrite_constraints (graph
, si
);
6728 build_succ_graph ();
6730 free_var_substitution_info (si
);
6732 /* Attach complex constraints to graph nodes. */
6733 move_complex_constraints (graph
);
6736 fprintf (dump_file
, "Uniting pointer but not location equivalent "
6738 unite_pointer_equivalences (graph
);
6741 fprintf (dump_file
, "Finding indirect cycles\n");
6742 find_indirect_cycles (graph
);
6744 /* Implicit nodes and predecessors are no longer necessary at this
6746 remove_preds_and_fake_succs (graph
);
6748 if (dump_file
&& (dump_flags
& TDF_GRAPH
))
6750 fprintf (dump_file
, "\n\n// The constraint graph before solve-graph "
6751 "in dot format:\n");
6752 dump_constraint_graph (dump_file
);
6753 fprintf (dump_file
, "\n\n");
6757 fprintf (dump_file
, "Solving graph\n");
6759 solve_graph (graph
);
6761 if (dump_file
&& (dump_flags
& TDF_GRAPH
))
6763 fprintf (dump_file
, "\n\n// The constraint graph after solve-graph "
6764 "in dot format:\n");
6765 dump_constraint_graph (dump_file
);
6766 fprintf (dump_file
, "\n\n");
6770 dump_sa_points_to_info (dump_file
);
6773 /* Create points-to sets for the current function. See the comments
6774 at the start of the file for an algorithmic overview. */
6777 compute_points_to_sets (void)
6783 timevar_push (TV_TREE_PTA
);
6787 intra_create_variable_infos (cfun
);
6789 /* Now walk all statements and build the constraint set. */
6790 FOR_EACH_BB_FN (bb
, cfun
)
6792 for (gphi_iterator gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
);
6795 gphi
*phi
= gsi
.phi ();
6797 if (! virtual_operand_p (gimple_phi_result (phi
)))
6798 find_func_aliases (cfun
, phi
);
6801 for (gimple_stmt_iterator gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
);
6804 gimple stmt
= gsi_stmt (gsi
);
6806 find_func_aliases (cfun
, stmt
);
6812 fprintf (dump_file
, "Points-to analysis\n\nConstraints:\n\n");
6813 dump_constraints (dump_file
, 0);
6816 /* From the constraints compute the points-to sets. */
6817 solve_constraints ();
6819 /* Compute the points-to set for ESCAPED used for call-clobber analysis. */
6820 cfun
->gimple_df
->escaped
= find_what_var_points_to (get_varinfo (escaped_id
));
6822 /* Make sure the ESCAPED solution (which is used as placeholder in
6823 other solutions) does not reference itself. This simplifies
6824 points-to solution queries. */
6825 cfun
->gimple_df
->escaped
.escaped
= 0;
6827 /* Compute the points-to sets for pointer SSA_NAMEs. */
6828 for (i
= 0; i
< num_ssa_names
; ++i
)
6830 tree ptr
= ssa_name (i
);
6832 && POINTER_TYPE_P (TREE_TYPE (ptr
)))
6833 find_what_p_points_to (ptr
);
6836 /* Compute the call-used/clobbered sets. */
6837 FOR_EACH_BB_FN (bb
, cfun
)
6839 gimple_stmt_iterator gsi
;
6841 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
6844 struct pt_solution
*pt
;
6846 stmt
= dyn_cast
<gcall
*> (gsi_stmt (gsi
));
6850 pt
= gimple_call_use_set (stmt
);
6851 if (gimple_call_flags (stmt
) & ECF_CONST
)
6852 memset (pt
, 0, sizeof (struct pt_solution
));
6853 else if ((vi
= lookup_call_use_vi (stmt
)) != NULL
)
6855 *pt
= find_what_var_points_to (vi
);
6856 /* Escaped (and thus nonlocal) variables are always
6857 implicitly used by calls. */
6858 /* ??? ESCAPED can be empty even though NONLOCAL
6865 /* If there is nothing special about this call then
6866 we have made everything that is used also escape. */
6867 *pt
= cfun
->gimple_df
->escaped
;
6871 pt
= gimple_call_clobber_set (stmt
);
6872 if (gimple_call_flags (stmt
) & (ECF_CONST
|ECF_PURE
|ECF_NOVOPS
))
6873 memset (pt
, 0, sizeof (struct pt_solution
));
6874 else if ((vi
= lookup_call_clobber_vi (stmt
)) != NULL
)
6876 *pt
= find_what_var_points_to (vi
);
6877 /* Escaped (and thus nonlocal) variables are always
6878 implicitly clobbered by calls. */
6879 /* ??? ESCAPED can be empty even though NONLOCAL
6886 /* If there is nothing special about this call then
6887 we have made everything that is used also escape. */
6888 *pt
= cfun
->gimple_df
->escaped
;
6894 timevar_pop (TV_TREE_PTA
);
6898 /* Delete created points-to sets. */
6901 delete_points_to_sets (void)
6905 delete shared_bitmap_table
;
6906 shared_bitmap_table
= NULL
;
6907 if (dump_file
&& (dump_flags
& TDF_STATS
))
6908 fprintf (dump_file
, "Points to sets created:%d\n",
6909 stats
.points_to_sets_created
);
6912 delete call_stmt_vars
;
6913 bitmap_obstack_release (&pta_obstack
);
6914 constraints
.release ();
6916 for (i
= 0; i
< graph
->size
; i
++)
6917 graph
->complex[i
].release ();
6918 free (graph
->complex);
6921 free (graph
->succs
);
6923 free (graph
->pe_rep
);
6924 free (graph
->indirect_cycles
);
6928 variable_info_pool
.release ();
6929 constraint_pool
.release ();
6931 obstack_free (&fake_var_decl_obstack
, NULL
);
6933 delete final_solutions
;
6934 obstack_free (&final_solutions_obstack
, NULL
);
6937 /* Mark "other" loads and stores as belonging to CLIQUE and with
6941 visit_loadstore (gimple
, tree base
, tree ref
, void *clique_
)
6943 unsigned short clique
= (uintptr_t)clique_
;
6944 if (TREE_CODE (base
) == MEM_REF
6945 || TREE_CODE (base
) == TARGET_MEM_REF
)
6947 tree ptr
= TREE_OPERAND (base
, 0);
6948 if (TREE_CODE (ptr
) == SSA_NAME
)
6950 /* ??? We need to make sure 'ptr' doesn't include any of
6951 the restrict tags in its points-to set. */
6955 /* For now let decls through. */
6957 /* Do not overwrite existing cliques (that includes clique, base
6958 pairs we just set). */
6959 if (MR_DEPENDENCE_CLIQUE (base
) == 0)
6961 MR_DEPENDENCE_CLIQUE (base
) = clique
;
6962 MR_DEPENDENCE_BASE (base
) = 0;
6966 /* For plain decl accesses see whether they are accesses to globals
6967 and rewrite them to MEM_REFs with { clique, 0 }. */
6968 if (TREE_CODE (base
) == VAR_DECL
6969 && is_global_var (base
)
6970 /* ??? We can't rewrite a plain decl with the walk_stmt_load_store
6975 while (handled_component_p (*basep
))
6976 basep
= &TREE_OPERAND (*basep
, 0);
6977 gcc_assert (TREE_CODE (*basep
) == VAR_DECL
);
6978 tree ptr
= build_fold_addr_expr (*basep
);
6979 tree zero
= build_int_cst (TREE_TYPE (ptr
), 0);
6980 *basep
= build2 (MEM_REF
, TREE_TYPE (*basep
), ptr
, zero
);
6981 MR_DEPENDENCE_CLIQUE (*basep
) = clique
;
6982 MR_DEPENDENCE_BASE (*basep
) = 0;
6988 /* If REF is a MEM_REF then assign a clique, base pair to it, updating
6989 CLIQUE, *RESTRICT_VAR and LAST_RUID. Return whether dependence info
6990 was assigned to REF. */
6993 maybe_set_dependence_info (tree ref
, tree ptr
,
6994 unsigned short &clique
, varinfo_t restrict_var
,
6995 unsigned short &last_ruid
)
6997 while (handled_component_p (ref
))
6998 ref
= TREE_OPERAND (ref
, 0);
6999 if ((TREE_CODE (ref
) == MEM_REF
7000 || TREE_CODE (ref
) == TARGET_MEM_REF
)
7001 && TREE_OPERAND (ref
, 0) == ptr
)
7003 /* Do not overwrite existing cliques. This avoids overwriting dependence
7004 info inlined from a function with restrict parameters inlined
7005 into a function with restrict parameters. This usually means we
7006 prefer to be precise in innermost loops. */
7007 if (MR_DEPENDENCE_CLIQUE (ref
) == 0)
7010 clique
= ++cfun
->last_clique
;
7011 if (restrict_var
->ruid
== 0)
7012 restrict_var
->ruid
= ++last_ruid
;
7013 MR_DEPENDENCE_CLIQUE (ref
) = clique
;
7014 MR_DEPENDENCE_BASE (ref
) = restrict_var
->ruid
;
7021 /* Compute the set of independend memory references based on restrict
7022 tags and their conservative propagation to the points-to sets. */
7025 compute_dependence_clique (void)
7027 unsigned short clique
= 0;
7028 unsigned short last_ruid
= 0;
7029 for (unsigned i
= 0; i
< num_ssa_names
; ++i
)
7031 tree ptr
= ssa_name (i
);
7032 if (!ptr
|| !POINTER_TYPE_P (TREE_TYPE (ptr
)))
7035 /* Avoid all this when ptr is not dereferenced? */
7037 if (SSA_NAME_IS_DEFAULT_DEF (ptr
)
7038 && (TREE_CODE (SSA_NAME_VAR (ptr
)) == PARM_DECL
7039 || TREE_CODE (SSA_NAME_VAR (ptr
)) == RESULT_DECL
))
7040 p
= SSA_NAME_VAR (ptr
);
7041 varinfo_t vi
= lookup_vi_for_tree (p
);
7044 vi
= get_varinfo (find (vi
->id
));
7047 varinfo_t restrict_var
= NULL
;
7048 EXECUTE_IF_SET_IN_BITMAP (vi
->solution
, 0, j
, bi
)
7050 varinfo_t oi
= get_varinfo (j
);
7051 if (oi
->is_restrict_var
)
7055 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7057 fprintf (dump_file
, "found restrict pointed-to "
7059 print_generic_expr (dump_file
, ptr
, 0);
7060 fprintf (dump_file
, " but not exclusively\n");
7062 restrict_var
= NULL
;
7067 /* NULL is the only other valid points-to entry. */
7068 else if (oi
->id
!= nothing_id
)
7070 restrict_var
= NULL
;
7074 /* Ok, found that ptr must(!) point to a single(!) restrict
7076 /* ??? PTA isn't really a proper propagation engine to compute
7078 ??? We could handle merging of two restricts by unifying them. */
7081 /* Now look at possible dereferences of ptr. */
7082 imm_use_iterator ui
;
7084 FOR_EACH_IMM_USE_STMT (use_stmt
, ui
, ptr
)
7086 /* ??? Calls and asms. */
7087 if (!gimple_assign_single_p (use_stmt
))
7089 maybe_set_dependence_info (gimple_assign_lhs (use_stmt
), ptr
,
7090 clique
, restrict_var
, last_ruid
);
7091 maybe_set_dependence_info (gimple_assign_rhs1 (use_stmt
), ptr
,
7092 clique
, restrict_var
, last_ruid
);
7100 /* Assign the BASE id zero to all accesses not based on a restrict
7101 pointer. That way they get disabiguated against restrict
7102 accesses but not against each other. */
7103 /* ??? For restricts derived from globals (thus not incoming
7104 parameters) we can't restrict scoping properly thus the following
7105 is too aggressive there. For now we have excluded those globals from
7106 getting into the MR_DEPENDENCE machinery. */
7108 FOR_EACH_BB_FN (bb
, cfun
)
7109 for (gimple_stmt_iterator gsi
= gsi_start_bb (bb
);
7110 !gsi_end_p (gsi
); gsi_next (&gsi
))
7112 gimple stmt
= gsi_stmt (gsi
);
7113 walk_stmt_load_store_ops (stmt
, (void *)(uintptr_t)clique
,
7114 visit_loadstore
, visit_loadstore
);
7118 /* Compute points-to information for every SSA_NAME pointer in the
7119 current function and compute the transitive closure of escaped
7120 variables to re-initialize the call-clobber states of local variables. */
7123 compute_may_aliases (void)
7125 if (cfun
->gimple_df
->ipa_pta
)
7129 fprintf (dump_file
, "\nNot re-computing points-to information "
7130 "because IPA points-to information is available.\n\n");
7132 /* But still dump what we have remaining it. */
7133 dump_alias_info (dump_file
);
7139 /* For each pointer P_i, determine the sets of variables that P_i may
7140 point-to. Compute the reachability set of escaped and call-used
7142 compute_points_to_sets ();
7144 /* Debugging dumps. */
7146 dump_alias_info (dump_file
);
7148 /* Compute restrict-based memory disambiguations. */
7149 compute_dependence_clique ();
7151 /* Deallocate memory used by aliasing data structures and the internal
7152 points-to solution. */
7153 delete_points_to_sets ();
7155 gcc_assert (!need_ssa_update_p (cfun
));
7160 /* A dummy pass to cause points-to information to be computed via
7161 TODO_rebuild_alias. */
7165 const pass_data pass_data_build_alias
=
7167 GIMPLE_PASS
, /* type */
7169 OPTGROUP_NONE
, /* optinfo_flags */
7170 TV_NONE
, /* tv_id */
7171 ( PROP_cfg
| PROP_ssa
), /* properties_required */
7172 0, /* properties_provided */
7173 0, /* properties_destroyed */
7174 0, /* todo_flags_start */
7175 TODO_rebuild_alias
, /* todo_flags_finish */
7178 class pass_build_alias
: public gimple_opt_pass
7181 pass_build_alias (gcc::context
*ctxt
)
7182 : gimple_opt_pass (pass_data_build_alias
, ctxt
)
7185 /* opt_pass methods: */
7186 virtual bool gate (function
*) { return flag_tree_pta
; }
7188 }; // class pass_build_alias
7193 make_pass_build_alias (gcc::context
*ctxt
)
7195 return new pass_build_alias (ctxt
);
7198 /* A dummy pass to cause points-to information to be computed via
7199 TODO_rebuild_alias. */
7203 const pass_data pass_data_build_ealias
=
7205 GIMPLE_PASS
, /* type */
7206 "ealias", /* name */
7207 OPTGROUP_NONE
, /* optinfo_flags */
7208 TV_NONE
, /* tv_id */
7209 ( PROP_cfg
| PROP_ssa
), /* properties_required */
7210 0, /* properties_provided */
7211 0, /* properties_destroyed */
7212 0, /* todo_flags_start */
7213 TODO_rebuild_alias
, /* todo_flags_finish */
7216 class pass_build_ealias
: public gimple_opt_pass
7219 pass_build_ealias (gcc::context
*ctxt
)
7220 : gimple_opt_pass (pass_data_build_ealias
, ctxt
)
7223 /* opt_pass methods: */
7224 virtual bool gate (function
*) { return flag_tree_pta
; }
7226 }; // class pass_build_ealias
7231 make_pass_build_ealias (gcc::context
*ctxt
)
7233 return new pass_build_ealias (ctxt
);
7237 /* IPA PTA solutions for ESCAPED. */
7238 struct pt_solution ipa_escaped_pt
7239 = { true, false, false, false, false, false, false, false, NULL
};
7241 /* Associate node with varinfo DATA. Worker for
7242 cgraph_for_node_and_aliases. */
7244 associate_varinfo_to_alias (struct cgraph_node
*node
, void *data
)
7246 if ((node
->alias
|| node
->thunk
.thunk_p
)
7248 insert_vi_for_tree (node
->decl
, (varinfo_t
)data
);
7252 /* Execute the driver for IPA PTA. */
7254 ipa_pta_execute (void)
7256 struct cgraph_node
*node
;
7264 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7266 symtab_node::dump_table (dump_file
);
7267 fprintf (dump_file
, "\n");
7270 /* Build the constraints. */
7271 FOR_EACH_DEFINED_FUNCTION (node
)
7274 /* Nodes without a body are not interesting. Especially do not
7275 visit clones at this point for now - we get duplicate decls
7276 there for inline clones at least. */
7277 if (!node
->has_gimple_body_p () || node
->global
.inlined_to
)
7281 gcc_assert (!node
->clone_of
);
7283 vi
= create_function_info_for (node
->decl
,
7284 alias_get_name (node
->decl
));
7285 node
->call_for_symbol_thunks_and_aliases
7286 (associate_varinfo_to_alias
, vi
, true);
7289 /* Create constraints for global variables and their initializers. */
7290 FOR_EACH_VARIABLE (var
)
7292 if (var
->alias
&& var
->analyzed
)
7295 get_vi_for_tree (var
->decl
);
7301 "Generating constraints for global initializers\n\n");
7302 dump_constraints (dump_file
, 0);
7303 fprintf (dump_file
, "\n");
7305 from
= constraints
.length ();
7307 FOR_EACH_DEFINED_FUNCTION (node
)
7309 struct function
*func
;
7312 /* Nodes without a body are not interesting. */
7313 if (!node
->has_gimple_body_p () || node
->clone_of
)
7319 "Generating constraints for %s", node
->name ());
7320 if (DECL_ASSEMBLER_NAME_SET_P (node
->decl
))
7321 fprintf (dump_file
, " (%s)",
7323 (DECL_ASSEMBLER_NAME (node
->decl
)));
7324 fprintf (dump_file
, "\n");
7327 func
= DECL_STRUCT_FUNCTION (node
->decl
);
7328 gcc_assert (cfun
== NULL
);
7330 /* For externally visible or attribute used annotated functions use
7331 local constraints for their arguments.
7332 For local functions we see all callers and thus do not need initial
7333 constraints for parameters. */
7334 if (node
->used_from_other_partition
7335 || node
->externally_visible
7336 || node
->force_output
7337 || node
->address_taken
)
7339 intra_create_variable_infos (func
);
7341 /* We also need to make function return values escape. Nothing
7342 escapes by returning from main though. */
7343 if (!MAIN_NAME_P (DECL_NAME (node
->decl
)))
7346 fi
= lookup_vi_for_tree (node
->decl
);
7347 rvi
= first_vi_for_offset (fi
, fi_result
);
7348 if (rvi
&& rvi
->offset
== fi_result
)
7350 struct constraint_expr includes
;
7351 struct constraint_expr var
;
7352 includes
.var
= escaped_id
;
7353 includes
.offset
= 0;
7354 includes
.type
= SCALAR
;
7358 process_constraint (new_constraint (includes
, var
));
7363 /* Build constriants for the function body. */
7364 FOR_EACH_BB_FN (bb
, func
)
7366 for (gphi_iterator gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
);
7369 gphi
*phi
= gsi
.phi ();
7371 if (! virtual_operand_p (gimple_phi_result (phi
)))
7372 find_func_aliases (func
, phi
);
7375 for (gimple_stmt_iterator gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
);
7378 gimple stmt
= gsi_stmt (gsi
);
7380 find_func_aliases (func
, stmt
);
7381 find_func_clobbers (func
, stmt
);
7387 fprintf (dump_file
, "\n");
7388 dump_constraints (dump_file
, from
);
7389 fprintf (dump_file
, "\n");
7391 from
= constraints
.length ();
7394 /* From the constraints compute the points-to sets. */
7395 solve_constraints ();
7397 /* Compute the global points-to sets for ESCAPED.
7398 ??? Note that the computed escape set is not correct
7399 for the whole unit as we fail to consider graph edges to
7400 externally visible functions. */
7401 ipa_escaped_pt
= find_what_var_points_to (get_varinfo (escaped_id
));
7403 /* Make sure the ESCAPED solution (which is used as placeholder in
7404 other solutions) does not reference itself. This simplifies
7405 points-to solution queries. */
7406 ipa_escaped_pt
.ipa_escaped
= 0;
7408 /* Assign the points-to sets to the SSA names in the unit. */
7409 FOR_EACH_DEFINED_FUNCTION (node
)
7412 struct function
*fn
;
7416 /* Nodes without a body are not interesting. */
7417 if (!node
->has_gimple_body_p () || node
->clone_of
)
7420 fn
= DECL_STRUCT_FUNCTION (node
->decl
);
7422 /* Compute the points-to sets for pointer SSA_NAMEs. */
7423 FOR_EACH_VEC_ELT (*fn
->gimple_df
->ssa_names
, i
, ptr
)
7426 && POINTER_TYPE_P (TREE_TYPE (ptr
)))
7427 find_what_p_points_to (ptr
);
7430 /* Compute the call-use and call-clobber sets for indirect calls
7431 and calls to external functions. */
7432 FOR_EACH_BB_FN (bb
, fn
)
7434 gimple_stmt_iterator gsi
;
7436 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
7439 struct pt_solution
*pt
;
7443 stmt
= dyn_cast
<gcall
*> (gsi_stmt (gsi
));
7447 /* Handle direct calls to functions with body. */
7448 decl
= gimple_call_fndecl (stmt
);
7450 && (fi
= lookup_vi_for_tree (decl
))
7453 *gimple_call_clobber_set (stmt
)
7454 = find_what_var_points_to
7455 (first_vi_for_offset (fi
, fi_clobbers
));
7456 *gimple_call_use_set (stmt
)
7457 = find_what_var_points_to
7458 (first_vi_for_offset (fi
, fi_uses
));
7460 /* Handle direct calls to external functions. */
7463 pt
= gimple_call_use_set (stmt
);
7464 if (gimple_call_flags (stmt
) & ECF_CONST
)
7465 memset (pt
, 0, sizeof (struct pt_solution
));
7466 else if ((vi
= lookup_call_use_vi (stmt
)) != NULL
)
7468 *pt
= find_what_var_points_to (vi
);
7469 /* Escaped (and thus nonlocal) variables are always
7470 implicitly used by calls. */
7471 /* ??? ESCAPED can be empty even though NONLOCAL
7474 pt
->ipa_escaped
= 1;
7478 /* If there is nothing special about this call then
7479 we have made everything that is used also escape. */
7480 *pt
= ipa_escaped_pt
;
7484 pt
= gimple_call_clobber_set (stmt
);
7485 if (gimple_call_flags (stmt
) & (ECF_CONST
|ECF_PURE
|ECF_NOVOPS
))
7486 memset (pt
, 0, sizeof (struct pt_solution
));
7487 else if ((vi
= lookup_call_clobber_vi (stmt
)) != NULL
)
7489 *pt
= find_what_var_points_to (vi
);
7490 /* Escaped (and thus nonlocal) variables are always
7491 implicitly clobbered by calls. */
7492 /* ??? ESCAPED can be empty even though NONLOCAL
7495 pt
->ipa_escaped
= 1;
7499 /* If there is nothing special about this call then
7500 we have made everything that is used also escape. */
7501 *pt
= ipa_escaped_pt
;
7505 /* Handle indirect calls. */
7507 && (fi
= get_fi_for_callee (stmt
)))
7509 /* We need to accumulate all clobbers/uses of all possible
7511 fi
= get_varinfo (find (fi
->id
));
7512 /* If we cannot constrain the set of functions we'll end up
7513 calling we end up using/clobbering everything. */
7514 if (bitmap_bit_p (fi
->solution
, anything_id
)
7515 || bitmap_bit_p (fi
->solution
, nonlocal_id
)
7516 || bitmap_bit_p (fi
->solution
, escaped_id
))
7518 pt_solution_reset (gimple_call_clobber_set (stmt
));
7519 pt_solution_reset (gimple_call_use_set (stmt
));
7525 struct pt_solution
*uses
, *clobbers
;
7527 uses
= gimple_call_use_set (stmt
);
7528 clobbers
= gimple_call_clobber_set (stmt
);
7529 memset (uses
, 0, sizeof (struct pt_solution
));
7530 memset (clobbers
, 0, sizeof (struct pt_solution
));
7531 EXECUTE_IF_SET_IN_BITMAP (fi
->solution
, 0, i
, bi
)
7533 struct pt_solution sol
;
7535 vi
= get_varinfo (i
);
7536 if (!vi
->is_fn_info
)
7538 /* ??? We could be more precise here? */
7540 uses
->ipa_escaped
= 1;
7541 clobbers
->nonlocal
= 1;
7542 clobbers
->ipa_escaped
= 1;
7546 if (!uses
->anything
)
7548 sol
= find_what_var_points_to
7549 (first_vi_for_offset (vi
, fi_uses
));
7550 pt_solution_ior_into (uses
, &sol
);
7552 if (!clobbers
->anything
)
7554 sol
= find_what_var_points_to
7555 (first_vi_for_offset (vi
, fi_clobbers
));
7556 pt_solution_ior_into (clobbers
, &sol
);
7564 fn
->gimple_df
->ipa_pta
= true;
7567 delete_points_to_sets ();
7576 const pass_data pass_data_ipa_pta
=
7578 SIMPLE_IPA_PASS
, /* type */
7580 OPTGROUP_NONE
, /* optinfo_flags */
7581 TV_IPA_PTA
, /* tv_id */
7582 0, /* properties_required */
7583 0, /* properties_provided */
7584 0, /* properties_destroyed */
7585 0, /* todo_flags_start */
7586 0, /* todo_flags_finish */
7589 class pass_ipa_pta
: public simple_ipa_opt_pass
7592 pass_ipa_pta (gcc::context
*ctxt
)
7593 : simple_ipa_opt_pass (pass_data_ipa_pta
, ctxt
)
7596 /* opt_pass methods: */
7597 virtual bool gate (function
*)
7601 /* Don't bother doing anything if the program has errors. */
7605 virtual unsigned int execute (function
*) { return ipa_pta_execute (); }
7607 }; // class pass_ipa_pta
7611 simple_ipa_opt_pass
*
7612 make_pass_ipa_pta (gcc::context
*ctxt
)
7614 return new pass_ipa_pta (ctxt
);