1 /* Tree based points-to analysis
2 Copyright (C) 2005-2016 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"
28 #include "alloc-pool.h"
29 #include "tree-pass.h"
32 #include "tree-pretty-print.h"
33 #include "diagnostic-core.h"
34 #include "fold-const.h"
35 #include "stor-layout.h"
37 #include "gimple-iterator.h"
38 #include "tree-into-ssa.h"
41 #include "gimple-walk.h"
43 /* The idea behind this analyzer is to generate set constraints from the
44 program, then solve the resulting constraints in order to generate the
47 Set constraints are a way of modeling program analysis problems that
48 involve sets. They consist of an inclusion constraint language,
49 describing the variables (each variable is a set) and operations that
50 are involved on the variables, and a set of rules that derive facts
51 from these operations. To solve a system of set constraints, you derive
52 all possible facts under the rules, which gives you the correct sets
55 See "Efficient Field-sensitive pointer analysis for C" by "David
56 J. Pearce and Paul H. J. Kelly and Chris Hankin, at
57 http://citeseer.ist.psu.edu/pearce04efficient.html
59 Also see "Ultra-fast Aliasing Analysis using CLA: A Million Lines
60 of C Code in a Second" by ""Nevin Heintze and Olivier Tardieu" at
61 http://citeseer.ist.psu.edu/heintze01ultrafast.html
63 There are three types of real constraint expressions, DEREF,
64 ADDRESSOF, and SCALAR. Each constraint expression consists
65 of a constraint type, a variable, and an offset.
67 SCALAR is a constraint expression type used to represent x, whether
68 it appears on the LHS or the RHS of a statement.
69 DEREF is a constraint expression type used to represent *x, whether
70 it appears on the LHS or the RHS of a statement.
71 ADDRESSOF is a constraint expression used to represent &x, whether
72 it appears on the LHS or the RHS of a statement.
74 Each pointer variable in the program is assigned an integer id, and
75 each field of a structure variable is assigned an integer id as well.
77 Structure variables are linked to their list of fields through a "next
78 field" in each variable that points to the next field in offset
80 Each variable for a structure field has
82 1. "size", that tells the size in bits of that field.
83 2. "fullsize, that tells the size in bits of the entire structure.
84 3. "offset", that tells the offset in bits from the beginning of the
85 structure to this field.
97 foo.a -> id 1, size 32, offset 0, fullsize 64, next foo.b
98 foo.b -> id 2, size 32, offset 32, fullsize 64, next NULL
99 bar -> id 3, size 32, offset 0, fullsize 32, next NULL
102 In order to solve the system of set constraints, the following is
105 1. Each constraint variable x has a solution set associated with it,
108 2. Constraints are separated into direct, copy, and complex.
109 Direct constraints are ADDRESSOF constraints that require no extra
110 processing, such as P = &Q
111 Copy constraints are those of the form P = Q.
112 Complex constraints are all the constraints involving dereferences
113 and offsets (including offsetted copies).
115 3. All direct constraints of the form P = &Q are processed, such
116 that Q is added to Sol(P)
118 4. All complex constraints for a given constraint variable are stored in a
119 linked list attached to that variable's node.
121 5. A directed graph is built out of the copy constraints. Each
122 constraint variable is a node in the graph, and an edge from
123 Q to P is added for each copy constraint of the form P = Q
125 6. The graph is then walked, and solution sets are
126 propagated along the copy edges, such that an edge from Q to P
127 causes Sol(P) <- Sol(P) union Sol(Q).
129 7. As we visit each node, all complex constraints associated with
130 that node are processed by adding appropriate copy edges to the graph, or the
131 appropriate variables to the solution set.
133 8. The process of walking the graph is iterated until no solution
136 Prior to walking the graph in steps 6 and 7, We perform static
137 cycle elimination on the constraint graph, as well
138 as off-line variable substitution.
140 TODO: Adding offsets to pointer-to-structures can be handled (IE not punted
141 on and turned into anything), but isn't. You can just see what offset
142 inside the pointed-to struct it's going to access.
144 TODO: Constant bounded arrays can be handled as if they were structs of the
145 same number of elements.
147 TODO: Modeling heap and incoming pointers becomes much better if we
148 add fields to them as we discover them, which we could do.
150 TODO: We could handle unions, but to be honest, it's probably not
151 worth the pain or slowdown. */
153 /* IPA-PTA optimizations possible.
155 When the indirect function called is ANYTHING we can add disambiguation
156 based on the function signatures (or simply the parameter count which
157 is the varinfo size). We also do not need to consider functions that
158 do not have their address taken.
160 The is_global_var bit which marks escape points is overly conservative
161 in IPA mode. Split it to is_escape_point and is_global_var - only
162 externally visible globals are escape points in IPA mode.
163 There is now is_ipa_escape_point but this is only used in a few
166 The way we introduce DECL_PT_UID to avoid fixing up all points-to
167 sets in the translation unit when we copy a DECL during inlining
168 pessimizes precision. The advantage is that the DECL_PT_UID keeps
169 compile-time and memory usage overhead low - the points-to sets
170 do not grow or get unshared as they would during a fixup phase.
171 An alternative solution is to delay IPA PTA until after all
172 inlining transformations have been applied.
174 The way we propagate clobber/use information isn't optimized.
175 It should use a new complex constraint that properly filters
176 out local variables of the callee (though that would make
177 the sets invalid after inlining). OTOH we might as well
178 admit defeat to WHOPR and simply do all the clobber/use analysis
179 and propagation after PTA finished but before we threw away
180 points-to information for memory variables. WHOPR and PTA
181 do not play along well anyway - the whole constraint solving
182 would need to be done in WPA phase and it will be very interesting
183 to apply the results to local SSA names during LTRANS phase.
185 We probably should compute a per-function unit-ESCAPE solution
186 propagating it simply like the clobber / uses solutions. The
187 solution can go alongside the non-IPA espaced solution and be
188 used to query which vars escape the unit through a function.
189 This is also required to make the escaped-HEAP trick work in IPA mode.
191 We never put function decls in points-to sets so we do not
192 keep the set of called functions for indirect calls.
194 And probably more. */
196 static bool use_field_sensitive
= true;
197 static int in_ipa_mode
= 0;
199 /* Used for predecessor bitmaps. */
200 static bitmap_obstack predbitmap_obstack
;
202 /* Used for points-to sets. */
203 static bitmap_obstack pta_obstack
;
205 /* Used for oldsolution members of variables. */
206 static bitmap_obstack oldpta_obstack
;
208 /* Used for per-solver-iteration bitmaps. */
209 static bitmap_obstack iteration_obstack
;
211 static unsigned int create_variable_info_for (tree
, const char *, bool);
212 typedef struct constraint_graph
*constraint_graph_t
;
213 static void unify_nodes (constraint_graph_t
, unsigned int, unsigned int, bool);
216 typedef struct constraint
*constraint_t
;
219 #define EXECUTE_IF_IN_NONNULL_BITMAP(a, b, c, d) \
221 EXECUTE_IF_SET_IN_BITMAP (a, b, c, d)
223 static struct constraint_stats
225 unsigned int total_vars
;
226 unsigned int nonpointer_vars
;
227 unsigned int unified_vars_static
;
228 unsigned int unified_vars_dynamic
;
229 unsigned int iterations
;
230 unsigned int num_edges
;
231 unsigned int num_implicit_edges
;
232 unsigned int points_to_sets_created
;
237 /* ID of this variable */
240 /* True if this is a variable created by the constraint analysis, such as
241 heap variables and constraints we had to break up. */
242 unsigned int is_artificial_var
: 1;
244 /* True if this is a special variable whose solution set should not be
246 unsigned int is_special_var
: 1;
248 /* True for variables whose size is not known or variable. */
249 unsigned int is_unknown_size_var
: 1;
251 /* True for (sub-)fields that represent a whole variable. */
252 unsigned int is_full_var
: 1;
254 /* True if this is a heap variable. */
255 unsigned int is_heap_var
: 1;
257 /* True if this field may contain pointers. */
258 unsigned int may_have_pointers
: 1;
260 /* True if this field has only restrict qualified pointers. */
261 unsigned int only_restrict_pointers
: 1;
263 /* True if this represents a heap var created for a restrict qualified
265 unsigned int is_restrict_var
: 1;
267 /* True if this represents a global variable. */
268 unsigned int is_global_var
: 1;
270 /* True if this represents a module escape point for IPA analysis. */
271 unsigned int is_ipa_escape_point
: 1;
273 /* True if this represents a IPA function info. */
274 unsigned int is_fn_info
: 1;
276 /* ??? Store somewhere better. */
279 /* The ID of the variable for the next field in this structure
280 or zero for the last field in this structure. */
283 /* The ID of the variable for the first field in this structure. */
286 /* Offset of this variable, in bits, from the base variable */
287 unsigned HOST_WIDE_INT offset
;
289 /* Size of the variable, in bits. */
290 unsigned HOST_WIDE_INT size
;
292 /* Full size of the base variable, in bits. */
293 unsigned HOST_WIDE_INT fullsize
;
295 /* Name of this variable */
298 /* Tree that this variable is associated with. */
301 /* Points-to set for this variable. */
304 /* Old points-to set for this variable. */
307 typedef struct variable_info
*varinfo_t
;
309 static varinfo_t
first_vi_for_offset (varinfo_t
, unsigned HOST_WIDE_INT
);
310 static varinfo_t
first_or_preceding_vi_for_offset (varinfo_t
,
311 unsigned HOST_WIDE_INT
);
312 static varinfo_t
lookup_vi_for_tree (tree
);
313 static inline bool type_can_have_subvars (const_tree
);
314 static void make_param_constraints (varinfo_t
);
316 /* Pool of variable info structures. */
317 static object_allocator
<variable_info
> variable_info_pool
318 ("Variable info pool");
320 /* Map varinfo to final pt_solution. */
321 static hash_map
<varinfo_t
, pt_solution
*> *final_solutions
;
322 struct obstack final_solutions_obstack
;
324 /* Table of variable info structures for constraint variables.
325 Indexed directly by variable info id. */
326 static vec
<varinfo_t
> varmap
;
328 /* Return the varmap element N */
330 static inline varinfo_t
331 get_varinfo (unsigned int n
)
336 /* Return the next variable in the list of sub-variables of VI
337 or NULL if VI is the last sub-variable. */
339 static inline varinfo_t
340 vi_next (varinfo_t vi
)
342 return get_varinfo (vi
->next
);
345 /* Static IDs for the special variables. Variable ID zero is unused
346 and used as terminator for the sub-variable chain. */
347 enum { nothing_id
= 1, anything_id
= 2, string_id
= 3,
348 escaped_id
= 4, nonlocal_id
= 5,
349 storedanything_id
= 6, integer_id
= 7 };
351 /* Return a new variable info structure consisting for a variable
352 named NAME, and using constraint graph node NODE. Append it
353 to the vector of variable info structures. */
356 new_var_info (tree t
, const char *name
, bool add_id
)
358 unsigned index
= varmap
.length ();
359 varinfo_t ret
= variable_info_pool
.allocate ();
361 if (dump_file
&& add_id
)
363 char *tempname
= xasprintf ("%s(%d)", name
, index
);
364 name
= ggc_strdup (tempname
);
371 /* Vars without decl are artificial and do not have sub-variables. */
372 ret
->is_artificial_var
= (t
== NULL_TREE
);
373 ret
->is_special_var
= false;
374 ret
->is_unknown_size_var
= false;
375 ret
->is_full_var
= (t
== NULL_TREE
);
376 ret
->is_heap_var
= false;
377 ret
->may_have_pointers
= true;
378 ret
->only_restrict_pointers
= false;
379 ret
->is_restrict_var
= false;
381 ret
->is_global_var
= (t
== NULL_TREE
);
382 ret
->is_ipa_escape_point
= false;
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 || (VAR_P (t
) && DECL_HARD_REGISTER (t
)));
389 ret
->solution
= BITMAP_ALLOC (&pta_obstack
);
390 ret
->oldsolution
= NULL
;
396 varmap
.safe_push (ret
);
401 /* A map mapping call statements to per-stmt variables for uses
402 and clobbers specific to the call. */
403 static hash_map
<gimple
*, varinfo_t
> *call_stmt_vars
;
405 /* Lookup or create the variable for the call statement CALL. */
408 get_call_vi (gcall
*call
)
413 varinfo_t
*slot_p
= &call_stmt_vars
->get_or_insert (call
, &existed
);
417 vi
= new_var_info (NULL_TREE
, "CALLUSED", true);
421 vi
->is_full_var
= true;
423 vi2
= new_var_info (NULL_TREE
, "CALLCLOBBERED", true);
427 vi2
->is_full_var
= true;
435 /* Lookup the variable for the call statement CALL representing
436 the uses. Returns NULL if there is nothing special about this call. */
439 lookup_call_use_vi (gcall
*call
)
441 varinfo_t
*slot_p
= call_stmt_vars
->get (call
);
448 /* Lookup the variable for the call statement CALL representing
449 the clobbers. Returns NULL if there is nothing special about this call. */
452 lookup_call_clobber_vi (gcall
*call
)
454 varinfo_t uses
= lookup_call_use_vi (call
);
458 return vi_next (uses
);
461 /* Lookup or create the variable for the call statement CALL representing
465 get_call_use_vi (gcall
*call
)
467 return get_call_vi (call
);
470 /* Lookup or create the variable for the call statement CALL representing
473 static varinfo_t ATTRIBUTE_UNUSED
474 get_call_clobber_vi (gcall
*call
)
476 return vi_next (get_call_vi (call
));
480 enum constraint_expr_type
{SCALAR
, DEREF
, ADDRESSOF
};
482 /* An expression that appears in a constraint. */
484 struct constraint_expr
486 /* Constraint type. */
487 constraint_expr_type type
;
489 /* Variable we are referring to in the constraint. */
492 /* Offset, in bits, of this constraint from the beginning of
493 variables it ends up referring to.
495 IOW, in a deref constraint, we would deref, get the result set,
496 then add OFFSET to each member. */
497 HOST_WIDE_INT offset
;
500 /* Use 0x8000... as special unknown offset. */
501 #define UNKNOWN_OFFSET HOST_WIDE_INT_MIN
503 typedef struct constraint_expr ce_s
;
504 static void get_constraint_for_1 (tree
, vec
<ce_s
> *, bool, bool);
505 static void get_constraint_for (tree
, vec
<ce_s
> *);
506 static void get_constraint_for_rhs (tree
, vec
<ce_s
> *);
507 static void do_deref (vec
<ce_s
> *);
509 /* Our set constraints are made up of two constraint expressions, one
512 As described in the introduction, our set constraints each represent an
513 operation between set valued variables.
517 struct constraint_expr lhs
;
518 struct constraint_expr rhs
;
521 /* List of constraints that we use to build the constraint graph from. */
523 static vec
<constraint_t
> constraints
;
524 static object_allocator
<constraint
> constraint_pool ("Constraint pool");
526 /* The constraint graph is represented as an array of bitmaps
527 containing successor nodes. */
529 struct constraint_graph
531 /* Size of this graph, which may be different than the number of
532 nodes in the variable map. */
535 /* Explicit successors of each node. */
538 /* Implicit predecessors of each node (Used for variable
540 bitmap
*implicit_preds
;
542 /* Explicit predecessors of each node (Used for variable substitution). */
545 /* Indirect cycle representatives, or -1 if the node has no indirect
547 int *indirect_cycles
;
549 /* Representative node for a node. rep[a] == a unless the node has
553 /* Equivalence class representative for a label. This is used for
554 variable substitution. */
557 /* Pointer equivalence label for a node. All nodes with the same
558 pointer equivalence label can be unified together at some point
559 (either during constraint optimization or after the constraint
563 /* Pointer equivalence representative for a label. This is used to
564 handle nodes that are pointer equivalent but not location
565 equivalent. We can unite these once the addressof constraints
566 are transformed into initial points-to sets. */
569 /* Pointer equivalence label for each node, used during variable
571 unsigned int *pointer_label
;
573 /* Location equivalence label for each node, used during location
574 equivalence finding. */
575 unsigned int *loc_label
;
577 /* Pointed-by set for each node, used during location equivalence
578 finding. This is pointed-by rather than pointed-to, because it
579 is constructed using the predecessor graph. */
582 /* Points to sets for pointer equivalence. This is *not* the actual
583 points-to sets for nodes. */
586 /* Bitmap of nodes where the bit is set if the node is a direct
587 node. Used for variable substitution. */
588 sbitmap direct_nodes
;
590 /* Bitmap of nodes where the bit is set if the node is address
591 taken. Used for variable substitution. */
592 bitmap address_taken
;
594 /* Vector of complex constraints for each graph node. Complex
595 constraints are those involving dereferences or offsets that are
597 vec
<constraint_t
> *complex;
600 static constraint_graph_t graph
;
602 /* During variable substitution and the offline version of indirect
603 cycle finding, we create nodes to represent dereferences and
604 address taken constraints. These represent where these start and
606 #define FIRST_REF_NODE (varmap).length ()
607 #define LAST_REF_NODE (FIRST_REF_NODE + (FIRST_REF_NODE - 1))
609 /* Return the representative node for NODE, if NODE has been unioned
611 This function performs path compression along the way to finding
612 the representative. */
615 find (unsigned int node
)
617 gcc_checking_assert (node
< graph
->size
);
618 if (graph
->rep
[node
] != node
)
619 return graph
->rep
[node
] = find (graph
->rep
[node
]);
623 /* Union the TO and FROM nodes to the TO nodes.
624 Note that at some point in the future, we may want to do
625 union-by-rank, in which case we are going to have to return the
626 node we unified to. */
629 unite (unsigned int to
, unsigned int from
)
631 gcc_checking_assert (to
< graph
->size
&& from
< graph
->size
);
632 if (to
!= from
&& graph
->rep
[from
] != to
)
634 graph
->rep
[from
] = to
;
640 /* Create a new constraint consisting of LHS and RHS expressions. */
643 new_constraint (const struct constraint_expr lhs
,
644 const struct constraint_expr rhs
)
646 constraint_t ret
= constraint_pool
.allocate ();
652 /* Print out constraint C to FILE. */
655 dump_constraint (FILE *file
, constraint_t c
)
657 if (c
->lhs
.type
== ADDRESSOF
)
659 else if (c
->lhs
.type
== DEREF
)
661 fprintf (file
, "%s", get_varinfo (c
->lhs
.var
)->name
);
662 if (c
->lhs
.offset
== UNKNOWN_OFFSET
)
663 fprintf (file
, " + UNKNOWN");
664 else if (c
->lhs
.offset
!= 0)
665 fprintf (file
, " + " HOST_WIDE_INT_PRINT_DEC
, c
->lhs
.offset
);
666 fprintf (file
, " = ");
667 if (c
->rhs
.type
== ADDRESSOF
)
669 else if (c
->rhs
.type
== DEREF
)
671 fprintf (file
, "%s", get_varinfo (c
->rhs
.var
)->name
);
672 if (c
->rhs
.offset
== UNKNOWN_OFFSET
)
673 fprintf (file
, " + UNKNOWN");
674 else if (c
->rhs
.offset
!= 0)
675 fprintf (file
, " + " HOST_WIDE_INT_PRINT_DEC
, c
->rhs
.offset
);
679 void debug_constraint (constraint_t
);
680 void debug_constraints (void);
681 void debug_constraint_graph (void);
682 void debug_solution_for_var (unsigned int);
683 void debug_sa_points_to_info (void);
684 void debug_varinfo (varinfo_t
);
685 void debug_varmap (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. */
1381 scc_info (size_t size
);
1384 auto_sbitmap visited
;
1385 auto_sbitmap deleted
;
1387 unsigned int *node_mapping
;
1389 auto_vec
<unsigned> scc_stack
;
1393 /* Recursive routine to find strongly connected components in GRAPH.
1394 SI is the SCC info to store the information in, and N is the id of current
1395 graph node we are processing.
1397 This is Tarjan's strongly connected component finding algorithm, as
1398 modified by Nuutila to keep only non-root nodes on the stack.
1399 The algorithm can be found in "On finding the strongly connected
1400 connected components in a directed graph" by Esko Nuutila and Eljas
1401 Soisalon-Soininen, in Information Processing Letters volume 49,
1402 number 1, pages 9-14. */
1405 scc_visit (constraint_graph_t graph
, struct scc_info
*si
, unsigned int n
)
1409 unsigned int my_dfs
;
1411 bitmap_set_bit (si
->visited
, n
);
1412 si
->dfs
[n
] = si
->current_index
++;
1413 my_dfs
= si
->dfs
[n
];
1415 /* Visit all the successors. */
1416 EXECUTE_IF_IN_NONNULL_BITMAP (graph
->succs
[n
], 0, i
, bi
)
1420 if (i
> LAST_REF_NODE
)
1424 if (bitmap_bit_p (si
->deleted
, w
))
1427 if (!bitmap_bit_p (si
->visited
, w
))
1428 scc_visit (graph
, si
, w
);
1430 unsigned int t
= find (w
);
1431 gcc_checking_assert (find (n
) == n
);
1432 if (si
->dfs
[t
] < si
->dfs
[n
])
1433 si
->dfs
[n
] = si
->dfs
[t
];
1436 /* See if any components have been identified. */
1437 if (si
->dfs
[n
] == my_dfs
)
1439 if (si
->scc_stack
.length () > 0
1440 && si
->dfs
[si
->scc_stack
.last ()] >= my_dfs
)
1442 bitmap scc
= BITMAP_ALLOC (NULL
);
1443 unsigned int lowest_node
;
1446 bitmap_set_bit (scc
, n
);
1448 while (si
->scc_stack
.length () != 0
1449 && si
->dfs
[si
->scc_stack
.last ()] >= my_dfs
)
1451 unsigned int w
= si
->scc_stack
.pop ();
1453 bitmap_set_bit (scc
, w
);
1456 lowest_node
= bitmap_first_set_bit (scc
);
1457 gcc_assert (lowest_node
< FIRST_REF_NODE
);
1459 /* Collapse the SCC nodes into a single node, and mark the
1461 EXECUTE_IF_SET_IN_BITMAP (scc
, 0, i
, bi
)
1463 if (i
< FIRST_REF_NODE
)
1465 if (unite (lowest_node
, i
))
1466 unify_nodes (graph
, lowest_node
, i
, false);
1470 unite (lowest_node
, i
);
1471 graph
->indirect_cycles
[i
- FIRST_REF_NODE
] = lowest_node
;
1475 bitmap_set_bit (si
->deleted
, n
);
1478 si
->scc_stack
.safe_push (n
);
1481 /* Unify node FROM into node TO, updating the changed count if
1482 necessary when UPDATE_CHANGED is true. */
1485 unify_nodes (constraint_graph_t graph
, unsigned int to
, unsigned int from
,
1486 bool update_changed
)
1488 gcc_checking_assert (to
!= from
&& find (to
) == to
);
1490 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1491 fprintf (dump_file
, "Unifying %s to %s\n",
1492 get_varinfo (from
)->name
,
1493 get_varinfo (to
)->name
);
1496 stats
.unified_vars_dynamic
++;
1498 stats
.unified_vars_static
++;
1500 merge_graph_nodes (graph
, to
, from
);
1501 if (merge_node_constraints (graph
, to
, from
))
1504 bitmap_set_bit (changed
, to
);
1507 /* Mark TO as changed if FROM was changed. If TO was already marked
1508 as changed, decrease the changed count. */
1511 && bitmap_clear_bit (changed
, from
))
1512 bitmap_set_bit (changed
, to
);
1513 varinfo_t fromvi
= get_varinfo (from
);
1514 if (fromvi
->solution
)
1516 /* If the solution changes because of the merging, we need to mark
1517 the variable as changed. */
1518 varinfo_t tovi
= get_varinfo (to
);
1519 if (bitmap_ior_into (tovi
->solution
, fromvi
->solution
))
1522 bitmap_set_bit (changed
, to
);
1525 BITMAP_FREE (fromvi
->solution
);
1526 if (fromvi
->oldsolution
)
1527 BITMAP_FREE (fromvi
->oldsolution
);
1529 if (stats
.iterations
> 0
1530 && tovi
->oldsolution
)
1531 BITMAP_FREE (tovi
->oldsolution
);
1533 if (graph
->succs
[to
])
1534 bitmap_clear_bit (graph
->succs
[to
], to
);
1537 /* Information needed to compute the topological ordering of a graph. */
1541 /* sbitmap of visited nodes. */
1543 /* Array that stores the topological order of the graph, *in
1545 vec
<unsigned> topo_order
;
1549 /* Initialize and return a topological info structure. */
1551 static struct topo_info
*
1552 init_topo_info (void)
1554 size_t size
= graph
->size
;
1555 struct topo_info
*ti
= XNEW (struct topo_info
);
1556 ti
->visited
= sbitmap_alloc (size
);
1557 bitmap_clear (ti
->visited
);
1558 ti
->topo_order
.create (1);
1563 /* Free the topological sort info pointed to by TI. */
1566 free_topo_info (struct topo_info
*ti
)
1568 sbitmap_free (ti
->visited
);
1569 ti
->topo_order
.release ();
1573 /* Visit the graph in topological order, and store the order in the
1574 topo_info structure. */
1577 topo_visit (constraint_graph_t graph
, struct topo_info
*ti
,
1583 bitmap_set_bit (ti
->visited
, n
);
1585 if (graph
->succs
[n
])
1586 EXECUTE_IF_SET_IN_BITMAP (graph
->succs
[n
], 0, j
, bi
)
1588 if (!bitmap_bit_p (ti
->visited
, j
))
1589 topo_visit (graph
, ti
, j
);
1592 ti
->topo_order
.safe_push (n
);
1595 /* Process a constraint C that represents x = *(y + off), using DELTA as the
1596 starting solution for y. */
1599 do_sd_constraint (constraint_graph_t graph
, constraint_t c
,
1600 bitmap delta
, bitmap
*expanded_delta
)
1602 unsigned int lhs
= c
->lhs
.var
;
1604 bitmap sol
= get_varinfo (lhs
)->solution
;
1607 HOST_WIDE_INT roffset
= c
->rhs
.offset
;
1609 /* Our IL does not allow this. */
1610 gcc_checking_assert (c
->lhs
.offset
== 0);
1612 /* If the solution of Y contains anything it is good enough to transfer
1614 if (bitmap_bit_p (delta
, anything_id
))
1616 flag
|= bitmap_set_bit (sol
, anything_id
);
1620 /* If we do not know at with offset the rhs is dereferenced compute
1621 the reachability set of DELTA, conservatively assuming it is
1622 dereferenced at all valid offsets. */
1623 if (roffset
== UNKNOWN_OFFSET
)
1625 delta
= solution_set_expand (delta
, expanded_delta
);
1626 /* No further offset processing is necessary. */
1630 /* For each variable j in delta (Sol(y)), add
1631 an edge in the graph from j to x, and union Sol(j) into Sol(x). */
1632 EXECUTE_IF_SET_IN_BITMAP (delta
, 0, j
, bi
)
1634 varinfo_t v
= get_varinfo (j
);
1635 HOST_WIDE_INT fieldoffset
= v
->offset
+ roffset
;
1636 unsigned HOST_WIDE_INT size
= v
->size
;
1641 else if (roffset
!= 0)
1643 if (fieldoffset
< 0)
1644 v
= get_varinfo (v
->head
);
1646 v
= first_or_preceding_vi_for_offset (v
, fieldoffset
);
1649 /* We have to include all fields that overlap the current field
1650 shifted by roffset. */
1655 /* Adding edges from the special vars is pointless.
1656 They don't have sets that can change. */
1657 if (get_varinfo (t
)->is_special_var
)
1658 flag
|= bitmap_ior_into (sol
, get_varinfo (t
)->solution
);
1659 /* Merging the solution from ESCAPED needlessly increases
1660 the set. Use ESCAPED as representative instead. */
1661 else if (v
->id
== escaped_id
)
1662 flag
|= bitmap_set_bit (sol
, escaped_id
);
1663 else if (v
->may_have_pointers
1664 && add_graph_edge (graph
, lhs
, t
))
1665 flag
|= bitmap_ior_into (sol
, get_varinfo (t
)->solution
);
1673 while (v
->offset
< fieldoffset
+ size
);
1677 /* If the LHS solution changed, mark the var as changed. */
1680 get_varinfo (lhs
)->solution
= sol
;
1681 bitmap_set_bit (changed
, lhs
);
1685 /* Process a constraint C that represents *(x + off) = y using DELTA
1686 as the starting solution for x. */
1689 do_ds_constraint (constraint_t c
, bitmap delta
, bitmap
*expanded_delta
)
1691 unsigned int rhs
= c
->rhs
.var
;
1692 bitmap sol
= get_varinfo (rhs
)->solution
;
1695 HOST_WIDE_INT loff
= c
->lhs
.offset
;
1696 bool escaped_p
= false;
1698 /* Our IL does not allow this. */
1699 gcc_checking_assert (c
->rhs
.offset
== 0);
1701 /* If the solution of y contains ANYTHING simply use the ANYTHING
1702 solution. This avoids needlessly increasing the points-to sets. */
1703 if (bitmap_bit_p (sol
, anything_id
))
1704 sol
= get_varinfo (find (anything_id
))->solution
;
1706 /* If the solution for x contains ANYTHING we have to merge the
1707 solution of y into all pointer variables which we do via
1709 if (bitmap_bit_p (delta
, anything_id
))
1711 unsigned t
= find (storedanything_id
);
1712 if (add_graph_edge (graph
, t
, rhs
))
1714 if (bitmap_ior_into (get_varinfo (t
)->solution
, sol
))
1715 bitmap_set_bit (changed
, t
);
1720 /* If we do not know at with offset the rhs is dereferenced compute
1721 the reachability set of DELTA, conservatively assuming it is
1722 dereferenced at all valid offsets. */
1723 if (loff
== UNKNOWN_OFFSET
)
1725 delta
= solution_set_expand (delta
, expanded_delta
);
1729 /* For each member j of delta (Sol(x)), add an edge from y to j and
1730 union Sol(y) into Sol(j) */
1731 EXECUTE_IF_SET_IN_BITMAP (delta
, 0, j
, bi
)
1733 varinfo_t v
= get_varinfo (j
);
1735 HOST_WIDE_INT fieldoffset
= v
->offset
+ loff
;
1736 unsigned HOST_WIDE_INT size
= v
->size
;
1742 if (fieldoffset
< 0)
1743 v
= get_varinfo (v
->head
);
1745 v
= first_or_preceding_vi_for_offset (v
, fieldoffset
);
1748 /* We have to include all fields that overlap the current field
1752 if (v
->may_have_pointers
)
1754 /* If v is a global variable then this is an escape point. */
1755 if (v
->is_global_var
1758 t
= find (escaped_id
);
1759 if (add_graph_edge (graph
, t
, rhs
)
1760 && bitmap_ior_into (get_varinfo (t
)->solution
, sol
))
1761 bitmap_set_bit (changed
, t
);
1762 /* Enough to let rhs escape once. */
1766 if (v
->is_special_var
)
1770 if (add_graph_edge (graph
, t
, rhs
)
1771 && bitmap_ior_into (get_varinfo (t
)->solution
, sol
))
1772 bitmap_set_bit (changed
, t
);
1781 while (v
->offset
< fieldoffset
+ size
);
1785 /* Handle a non-simple (simple meaning requires no iteration),
1786 constraint (IE *x = &y, x = *y, *x = y, and x = y with offsets involved). */
1789 do_complex_constraint (constraint_graph_t graph
, constraint_t c
, bitmap delta
,
1790 bitmap
*expanded_delta
)
1792 if (c
->lhs
.type
== DEREF
)
1794 if (c
->rhs
.type
== ADDRESSOF
)
1801 do_ds_constraint (c
, delta
, expanded_delta
);
1804 else if (c
->rhs
.type
== DEREF
)
1807 if (!(get_varinfo (c
->lhs
.var
)->is_special_var
))
1808 do_sd_constraint (graph
, c
, delta
, expanded_delta
);
1815 gcc_checking_assert (c
->rhs
.type
== SCALAR
&& c
->lhs
.type
== SCALAR
1816 && c
->rhs
.offset
!= 0 && c
->lhs
.offset
== 0);
1817 tmp
= get_varinfo (c
->lhs
.var
)->solution
;
1819 flag
= set_union_with_increment (tmp
, delta
, c
->rhs
.offset
,
1823 bitmap_set_bit (changed
, c
->lhs
.var
);
1827 /* Initialize and return a new SCC info structure. */
1829 scc_info::scc_info (size_t size
) :
1830 visited (size
), deleted (size
), current_index (0), scc_stack (1)
1832 bitmap_clear (visited
);
1833 bitmap_clear (deleted
);
1834 node_mapping
= XNEWVEC (unsigned int, size
);
1835 dfs
= XCNEWVEC (unsigned int, size
);
1837 for (size_t i
= 0; i
< size
; i
++)
1838 node_mapping
[i
] = i
;
1841 /* Free an SCC info structure pointed to by SI */
1843 scc_info::~scc_info ()
1845 free (node_mapping
);
1850 /* Find indirect cycles in GRAPH that occur, using strongly connected
1851 components, and note them in the indirect cycles map.
1853 This technique comes from Ben Hardekopf and Calvin Lin,
1854 "It Pays to be Lazy: Fast and Accurate Pointer Analysis for Millions of
1855 Lines of Code", submitted to PLDI 2007. */
1858 find_indirect_cycles (constraint_graph_t graph
)
1861 unsigned int size
= graph
->size
;
1864 for (i
= 0; i
< MIN (LAST_REF_NODE
, size
); i
++ )
1865 if (!bitmap_bit_p (si
.visited
, i
) && find (i
) == i
)
1866 scc_visit (graph
, &si
, i
);
1869 /* Compute a topological ordering for GRAPH, and store the result in the
1870 topo_info structure TI. */
1873 compute_topo_order (constraint_graph_t graph
,
1874 struct topo_info
*ti
)
1877 unsigned int size
= graph
->size
;
1879 for (i
= 0; i
!= size
; ++i
)
1880 if (!bitmap_bit_p (ti
->visited
, i
) && find (i
) == i
)
1881 topo_visit (graph
, ti
, i
);
1884 /* Structure used to for hash value numbering of pointer equivalence
1887 typedef struct equiv_class_label
1890 unsigned int equivalence_class
;
1892 } *equiv_class_label_t
;
1893 typedef const struct equiv_class_label
*const_equiv_class_label_t
;
1895 /* Equiv_class_label hashtable helpers. */
1897 struct equiv_class_hasher
: free_ptr_hash
<equiv_class_label
>
1899 static inline hashval_t
hash (const equiv_class_label
*);
1900 static inline bool equal (const equiv_class_label
*,
1901 const equiv_class_label
*);
1904 /* Hash function for a equiv_class_label_t */
1907 equiv_class_hasher::hash (const equiv_class_label
*ecl
)
1909 return ecl
->hashcode
;
1912 /* Equality function for two equiv_class_label_t's. */
1915 equiv_class_hasher::equal (const equiv_class_label
*eql1
,
1916 const equiv_class_label
*eql2
)
1918 return (eql1
->hashcode
== eql2
->hashcode
1919 && bitmap_equal_p (eql1
->labels
, eql2
->labels
));
1922 /* A hashtable for mapping a bitmap of labels->pointer equivalence
1924 static hash_table
<equiv_class_hasher
> *pointer_equiv_class_table
;
1926 /* A hashtable for mapping a bitmap of labels->location equivalence
1928 static hash_table
<equiv_class_hasher
> *location_equiv_class_table
;
1930 /* Lookup a equivalence class in TABLE by the bitmap of LABELS with
1931 hash HAS it contains. Sets *REF_LABELS to the bitmap LABELS
1932 is equivalent to. */
1934 static equiv_class_label
*
1935 equiv_class_lookup_or_add (hash_table
<equiv_class_hasher
> *table
,
1938 equiv_class_label
**slot
;
1939 equiv_class_label ecl
;
1941 ecl
.labels
= labels
;
1942 ecl
.hashcode
= bitmap_hash (labels
);
1943 slot
= table
->find_slot (&ecl
, INSERT
);
1946 *slot
= XNEW (struct equiv_class_label
);
1947 (*slot
)->labels
= labels
;
1948 (*slot
)->hashcode
= ecl
.hashcode
;
1949 (*slot
)->equivalence_class
= 0;
1955 /* Perform offline variable substitution.
1957 This is a worst case quadratic time way of identifying variables
1958 that must have equivalent points-to sets, including those caused by
1959 static cycles, and single entry subgraphs, in the constraint graph.
1961 The technique is described in "Exploiting Pointer and Location
1962 Equivalence to Optimize Pointer Analysis. In the 14th International
1963 Static Analysis Symposium (SAS), August 2007." It is known as the
1964 "HU" algorithm, and is equivalent to value numbering the collapsed
1965 constraint graph including evaluating unions.
1967 The general method of finding equivalence classes is as follows:
1968 Add fake nodes (REF nodes) and edges for *a = b and a = *b constraints.
1969 Initialize all non-REF nodes to be direct nodes.
1970 For each constraint a = a U {b}, we set pts(a) = pts(a) u {fresh
1972 For each constraint containing the dereference, we also do the same
1975 We then compute SCC's in the graph and unify nodes in the same SCC,
1978 For each non-collapsed node x:
1979 Visit all unvisited explicit incoming edges.
1980 Ignoring all non-pointers, set pts(x) = Union of pts(a) for y
1982 Lookup the equivalence class for pts(x).
1983 If we found one, equivalence_class(x) = found class.
1984 Otherwise, equivalence_class(x) = new class, and new_class is
1985 added to the lookup table.
1987 All direct nodes with the same equivalence class can be replaced
1988 with a single representative node.
1989 All unlabeled nodes (label == 0) are not pointers and all edges
1990 involving them can be eliminated.
1991 We perform these optimizations during rewrite_constraints
1993 In addition to pointer equivalence class finding, we also perform
1994 location equivalence class finding. This is the set of variables
1995 that always appear together in points-to sets. We use this to
1996 compress the size of the points-to sets. */
1998 /* Current maximum pointer equivalence class id. */
1999 static int pointer_equiv_class
;
2001 /* Current maximum location equivalence class id. */
2002 static int location_equiv_class
;
2004 /* Recursive routine to find strongly connected components in GRAPH,
2005 and label it's nodes with DFS numbers. */
2008 condense_visit (constraint_graph_t graph
, struct scc_info
*si
, unsigned int n
)
2012 unsigned int my_dfs
;
2014 gcc_checking_assert (si
->node_mapping
[n
] == n
);
2015 bitmap_set_bit (si
->visited
, n
);
2016 si
->dfs
[n
] = si
->current_index
++;
2017 my_dfs
= si
->dfs
[n
];
2019 /* Visit all the successors. */
2020 EXECUTE_IF_IN_NONNULL_BITMAP (graph
->preds
[n
], 0, i
, bi
)
2022 unsigned int w
= si
->node_mapping
[i
];
2024 if (bitmap_bit_p (si
->deleted
, w
))
2027 if (!bitmap_bit_p (si
->visited
, w
))
2028 condense_visit (graph
, si
, w
);
2030 unsigned int t
= si
->node_mapping
[w
];
2031 gcc_checking_assert (si
->node_mapping
[n
] == n
);
2032 if (si
->dfs
[t
] < si
->dfs
[n
])
2033 si
->dfs
[n
] = si
->dfs
[t
];
2036 /* Visit all the implicit predecessors. */
2037 EXECUTE_IF_IN_NONNULL_BITMAP (graph
->implicit_preds
[n
], 0, i
, bi
)
2039 unsigned int w
= si
->node_mapping
[i
];
2041 if (bitmap_bit_p (si
->deleted
, w
))
2044 if (!bitmap_bit_p (si
->visited
, w
))
2045 condense_visit (graph
, si
, w
);
2047 unsigned int t
= si
->node_mapping
[w
];
2048 gcc_assert (si
->node_mapping
[n
] == n
);
2049 if (si
->dfs
[t
] < si
->dfs
[n
])
2050 si
->dfs
[n
] = si
->dfs
[t
];
2053 /* See if any components have been identified. */
2054 if (si
->dfs
[n
] == my_dfs
)
2056 while (si
->scc_stack
.length () != 0
2057 && si
->dfs
[si
->scc_stack
.last ()] >= my_dfs
)
2059 unsigned int w
= si
->scc_stack
.pop ();
2060 si
->node_mapping
[w
] = n
;
2062 if (!bitmap_bit_p (graph
->direct_nodes
, w
))
2063 bitmap_clear_bit (graph
->direct_nodes
, n
);
2065 /* Unify our nodes. */
2066 if (graph
->preds
[w
])
2068 if (!graph
->preds
[n
])
2069 graph
->preds
[n
] = BITMAP_ALLOC (&predbitmap_obstack
);
2070 bitmap_ior_into (graph
->preds
[n
], graph
->preds
[w
]);
2072 if (graph
->implicit_preds
[w
])
2074 if (!graph
->implicit_preds
[n
])
2075 graph
->implicit_preds
[n
] = BITMAP_ALLOC (&predbitmap_obstack
);
2076 bitmap_ior_into (graph
->implicit_preds
[n
],
2077 graph
->implicit_preds
[w
]);
2079 if (graph
->points_to
[w
])
2081 if (!graph
->points_to
[n
])
2082 graph
->points_to
[n
] = BITMAP_ALLOC (&predbitmap_obstack
);
2083 bitmap_ior_into (graph
->points_to
[n
],
2084 graph
->points_to
[w
]);
2087 bitmap_set_bit (si
->deleted
, n
);
2090 si
->scc_stack
.safe_push (n
);
2093 /* Label pointer equivalences.
2095 This performs a value numbering of the constraint graph to
2096 discover which variables will always have the same points-to sets
2097 under the current set of constraints.
2099 The way it value numbers is to store the set of points-to bits
2100 generated by the constraints and graph edges. This is just used as a
2101 hash and equality comparison. The *actual set of points-to bits* is
2102 completely irrelevant, in that we don't care about being able to
2105 The equality values (currently bitmaps) just have to satisfy a few
2106 constraints, the main ones being:
2107 1. The combining operation must be order independent.
2108 2. The end result of a given set of operations must be unique iff the
2109 combination of input values is unique
2113 label_visit (constraint_graph_t graph
, struct scc_info
*si
, unsigned int n
)
2115 unsigned int i
, first_pred
;
2118 bitmap_set_bit (si
->visited
, n
);
2120 /* Label and union our incoming edges's points to sets. */
2122 EXECUTE_IF_IN_NONNULL_BITMAP (graph
->preds
[n
], 0, i
, bi
)
2124 unsigned int w
= si
->node_mapping
[i
];
2125 if (!bitmap_bit_p (si
->visited
, w
))
2126 label_visit (graph
, si
, w
);
2128 /* Skip unused edges */
2129 if (w
== n
|| graph
->pointer_label
[w
] == 0)
2132 if (graph
->points_to
[w
])
2134 if (!graph
->points_to
[n
])
2136 if (first_pred
== -1U)
2140 graph
->points_to
[n
] = BITMAP_ALLOC (&predbitmap_obstack
);
2141 bitmap_ior (graph
->points_to
[n
],
2142 graph
->points_to
[first_pred
],
2143 graph
->points_to
[w
]);
2147 bitmap_ior_into (graph
->points_to
[n
], graph
->points_to
[w
]);
2151 /* Indirect nodes get fresh variables and a new pointer equiv class. */
2152 if (!bitmap_bit_p (graph
->direct_nodes
, n
))
2154 if (!graph
->points_to
[n
])
2156 graph
->points_to
[n
] = BITMAP_ALLOC (&predbitmap_obstack
);
2157 if (first_pred
!= -1U)
2158 bitmap_copy (graph
->points_to
[n
], graph
->points_to
[first_pred
]);
2160 bitmap_set_bit (graph
->points_to
[n
], FIRST_REF_NODE
+ n
);
2161 graph
->pointer_label
[n
] = pointer_equiv_class
++;
2162 equiv_class_label_t ecl
;
2163 ecl
= equiv_class_lookup_or_add (pointer_equiv_class_table
,
2164 graph
->points_to
[n
]);
2165 ecl
->equivalence_class
= graph
->pointer_label
[n
];
2169 /* If there was only a single non-empty predecessor the pointer equiv
2170 class is the same. */
2171 if (!graph
->points_to
[n
])
2173 if (first_pred
!= -1U)
2175 graph
->pointer_label
[n
] = graph
->pointer_label
[first_pred
];
2176 graph
->points_to
[n
] = graph
->points_to
[first_pred
];
2181 if (!bitmap_empty_p (graph
->points_to
[n
]))
2183 equiv_class_label_t ecl
;
2184 ecl
= equiv_class_lookup_or_add (pointer_equiv_class_table
,
2185 graph
->points_to
[n
]);
2186 if (ecl
->equivalence_class
== 0)
2187 ecl
->equivalence_class
= pointer_equiv_class
++;
2190 BITMAP_FREE (graph
->points_to
[n
]);
2191 graph
->points_to
[n
] = ecl
->labels
;
2193 graph
->pointer_label
[n
] = ecl
->equivalence_class
;
2197 /* Print the pred graph in dot format. */
2200 dump_pred_graph (struct scc_info
*si
, FILE *file
)
2204 /* Only print the graph if it has already been initialized: */
2208 /* Prints the header of the dot file: */
2209 fprintf (file
, "strict digraph {\n");
2210 fprintf (file
, " node [\n shape = box\n ]\n");
2211 fprintf (file
, " edge [\n fontsize = \"12\"\n ]\n");
2212 fprintf (file
, "\n // List of nodes and complex constraints in "
2213 "the constraint graph:\n");
2215 /* The next lines print the nodes in the graph together with the
2216 complex constraints attached to them. */
2217 for (i
= 1; i
< graph
->size
; i
++)
2219 if (i
== FIRST_REF_NODE
)
2221 if (si
->node_mapping
[i
] != i
)
2223 if (i
< FIRST_REF_NODE
)
2224 fprintf (file
, "\"%s\"", get_varinfo (i
)->name
);
2226 fprintf (file
, "\"*%s\"", get_varinfo (i
- FIRST_REF_NODE
)->name
);
2227 if (graph
->points_to
[i
]
2228 && !bitmap_empty_p (graph
->points_to
[i
]))
2230 if (i
< FIRST_REF_NODE
)
2231 fprintf (file
, "[label=\"%s = {", get_varinfo (i
)->name
);
2233 fprintf (file
, "[label=\"*%s = {",
2234 get_varinfo (i
- FIRST_REF_NODE
)->name
);
2237 EXECUTE_IF_SET_IN_BITMAP (graph
->points_to
[i
], 0, j
, bi
)
2238 fprintf (file
, " %d", j
);
2239 fprintf (file
, " }\"]");
2241 fprintf (file
, ";\n");
2244 /* Go over the edges. */
2245 fprintf (file
, "\n // Edges in the constraint graph:\n");
2246 for (i
= 1; i
< graph
->size
; i
++)
2250 if (si
->node_mapping
[i
] != i
)
2252 EXECUTE_IF_IN_NONNULL_BITMAP (graph
->preds
[i
], 0, j
, bi
)
2254 unsigned from
= si
->node_mapping
[j
];
2255 if (from
< FIRST_REF_NODE
)
2256 fprintf (file
, "\"%s\"", get_varinfo (from
)->name
);
2258 fprintf (file
, "\"*%s\"", get_varinfo (from
- FIRST_REF_NODE
)->name
);
2259 fprintf (file
, " -> ");
2260 if (i
< FIRST_REF_NODE
)
2261 fprintf (file
, "\"%s\"", get_varinfo (i
)->name
);
2263 fprintf (file
, "\"*%s\"", get_varinfo (i
- FIRST_REF_NODE
)->name
);
2264 fprintf (file
, ";\n");
2268 /* Prints the tail of the dot file. */
2269 fprintf (file
, "}\n");
2272 /* Perform offline variable substitution, discovering equivalence
2273 classes, and eliminating non-pointer variables. */
2275 static struct scc_info
*
2276 perform_var_substitution (constraint_graph_t graph
)
2279 unsigned int size
= graph
->size
;
2280 scc_info
*si
= new scc_info (size
);
2282 bitmap_obstack_initialize (&iteration_obstack
);
2283 pointer_equiv_class_table
= new hash_table
<equiv_class_hasher
> (511);
2284 location_equiv_class_table
2285 = new hash_table
<equiv_class_hasher
> (511);
2286 pointer_equiv_class
= 1;
2287 location_equiv_class
= 1;
2289 /* Condense the nodes, which means to find SCC's, count incoming
2290 predecessors, and unite nodes in SCC's. */
2291 for (i
= 1; i
< FIRST_REF_NODE
; i
++)
2292 if (!bitmap_bit_p (si
->visited
, si
->node_mapping
[i
]))
2293 condense_visit (graph
, si
, si
->node_mapping
[i
]);
2295 if (dump_file
&& (dump_flags
& TDF_GRAPH
))
2297 fprintf (dump_file
, "\n\n// The constraint graph before var-substitution "
2298 "in dot format:\n");
2299 dump_pred_graph (si
, dump_file
);
2300 fprintf (dump_file
, "\n\n");
2303 bitmap_clear (si
->visited
);
2304 /* Actually the label the nodes for pointer equivalences */
2305 for (i
= 1; i
< FIRST_REF_NODE
; i
++)
2306 if (!bitmap_bit_p (si
->visited
, si
->node_mapping
[i
]))
2307 label_visit (graph
, si
, si
->node_mapping
[i
]);
2309 /* Calculate location equivalence labels. */
2310 for (i
= 1; i
< FIRST_REF_NODE
; i
++)
2316 if (!graph
->pointed_by
[i
])
2318 pointed_by
= BITMAP_ALLOC (&iteration_obstack
);
2320 /* Translate the pointed-by mapping for pointer equivalence
2322 EXECUTE_IF_SET_IN_BITMAP (graph
->pointed_by
[i
], 0, j
, bi
)
2324 bitmap_set_bit (pointed_by
,
2325 graph
->pointer_label
[si
->node_mapping
[j
]]);
2327 /* The original pointed_by is now dead. */
2328 BITMAP_FREE (graph
->pointed_by
[i
]);
2330 /* Look up the location equivalence label if one exists, or make
2332 equiv_class_label_t ecl
;
2333 ecl
= equiv_class_lookup_or_add (location_equiv_class_table
, pointed_by
);
2334 if (ecl
->equivalence_class
== 0)
2335 ecl
->equivalence_class
= location_equiv_class
++;
2338 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2339 fprintf (dump_file
, "Found location equivalence for node %s\n",
2340 get_varinfo (i
)->name
);
2341 BITMAP_FREE (pointed_by
);
2343 graph
->loc_label
[i
] = ecl
->equivalence_class
;
2347 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2348 for (i
= 1; i
< FIRST_REF_NODE
; i
++)
2350 unsigned j
= si
->node_mapping
[i
];
2353 fprintf (dump_file
, "%s node id %d ",
2354 bitmap_bit_p (graph
->direct_nodes
, i
)
2355 ? "Direct" : "Indirect", i
);
2356 if (i
< FIRST_REF_NODE
)
2357 fprintf (dump_file
, "\"%s\"", get_varinfo (i
)->name
);
2359 fprintf (dump_file
, "\"*%s\"",
2360 get_varinfo (i
- FIRST_REF_NODE
)->name
);
2361 fprintf (dump_file
, " mapped to SCC leader node id %d ", j
);
2362 if (j
< FIRST_REF_NODE
)
2363 fprintf (dump_file
, "\"%s\"\n", get_varinfo (j
)->name
);
2365 fprintf (dump_file
, "\"*%s\"\n",
2366 get_varinfo (j
- FIRST_REF_NODE
)->name
);
2371 "Equivalence classes for %s node id %d ",
2372 bitmap_bit_p (graph
->direct_nodes
, i
)
2373 ? "direct" : "indirect", i
);
2374 if (i
< FIRST_REF_NODE
)
2375 fprintf (dump_file
, "\"%s\"", get_varinfo (i
)->name
);
2377 fprintf (dump_file
, "\"*%s\"",
2378 get_varinfo (i
- FIRST_REF_NODE
)->name
);
2380 ": pointer %d, location %d\n",
2381 graph
->pointer_label
[i
], graph
->loc_label
[i
]);
2385 /* Quickly eliminate our non-pointer variables. */
2387 for (i
= 1; i
< FIRST_REF_NODE
; i
++)
2389 unsigned int node
= si
->node_mapping
[i
];
2391 if (graph
->pointer_label
[node
] == 0)
2393 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2395 "%s is a non-pointer variable, eliminating edges.\n",
2396 get_varinfo (node
)->name
);
2397 stats
.nonpointer_vars
++;
2398 clear_edges_for_node (graph
, node
);
2405 /* Free information that was only necessary for variable
2409 free_var_substitution_info (struct scc_info
*si
)
2412 free (graph
->pointer_label
);
2413 free (graph
->loc_label
);
2414 free (graph
->pointed_by
);
2415 free (graph
->points_to
);
2416 free (graph
->eq_rep
);
2417 sbitmap_free (graph
->direct_nodes
);
2418 delete pointer_equiv_class_table
;
2419 pointer_equiv_class_table
= NULL
;
2420 delete location_equiv_class_table
;
2421 location_equiv_class_table
= NULL
;
2422 bitmap_obstack_release (&iteration_obstack
);
2425 /* Return an existing node that is equivalent to NODE, which has
2426 equivalence class LABEL, if one exists. Return NODE otherwise. */
2429 find_equivalent_node (constraint_graph_t graph
,
2430 unsigned int node
, unsigned int label
)
2432 /* If the address version of this variable is unused, we can
2433 substitute it for anything else with the same label.
2434 Otherwise, we know the pointers are equivalent, but not the
2435 locations, and we can unite them later. */
2437 if (!bitmap_bit_p (graph
->address_taken
, node
))
2439 gcc_checking_assert (label
< graph
->size
);
2441 if (graph
->eq_rep
[label
] != -1)
2443 /* Unify the two variables since we know they are equivalent. */
2444 if (unite (graph
->eq_rep
[label
], node
))
2445 unify_nodes (graph
, graph
->eq_rep
[label
], node
, false);
2446 return graph
->eq_rep
[label
];
2450 graph
->eq_rep
[label
] = node
;
2451 graph
->pe_rep
[label
] = node
;
2456 gcc_checking_assert (label
< graph
->size
);
2457 graph
->pe
[node
] = label
;
2458 if (graph
->pe_rep
[label
] == -1)
2459 graph
->pe_rep
[label
] = node
;
2465 /* Unite pointer equivalent but not location equivalent nodes in
2466 GRAPH. This may only be performed once variable substitution is
2470 unite_pointer_equivalences (constraint_graph_t graph
)
2474 /* Go through the pointer equivalences and unite them to their
2475 representative, if they aren't already. */
2476 for (i
= 1; i
< FIRST_REF_NODE
; i
++)
2478 unsigned int label
= graph
->pe
[i
];
2481 int label_rep
= graph
->pe_rep
[label
];
2483 if (label_rep
== -1)
2486 label_rep
= find (label_rep
);
2487 if (label_rep
>= 0 && unite (label_rep
, find (i
)))
2488 unify_nodes (graph
, label_rep
, i
, false);
2493 /* Move complex constraints to the GRAPH nodes they belong to. */
2496 move_complex_constraints (constraint_graph_t graph
)
2501 FOR_EACH_VEC_ELT (constraints
, i
, c
)
2505 struct constraint_expr lhs
= c
->lhs
;
2506 struct constraint_expr rhs
= c
->rhs
;
2508 if (lhs
.type
== DEREF
)
2510 insert_into_complex (graph
, lhs
.var
, c
);
2512 else if (rhs
.type
== DEREF
)
2514 if (!(get_varinfo (lhs
.var
)->is_special_var
))
2515 insert_into_complex (graph
, rhs
.var
, c
);
2517 else if (rhs
.type
!= ADDRESSOF
&& lhs
.var
> anything_id
2518 && (lhs
.offset
!= 0 || rhs
.offset
!= 0))
2520 insert_into_complex (graph
, rhs
.var
, c
);
2527 /* Optimize and rewrite complex constraints while performing
2528 collapsing of equivalent nodes. SI is the SCC_INFO that is the
2529 result of perform_variable_substitution. */
2532 rewrite_constraints (constraint_graph_t graph
,
2533 struct scc_info
*si
)
2540 for (unsigned int j
= 0; j
< graph
->size
; j
++)
2541 gcc_assert (find (j
) == j
);
2544 FOR_EACH_VEC_ELT (constraints
, i
, c
)
2546 struct constraint_expr lhs
= c
->lhs
;
2547 struct constraint_expr rhs
= c
->rhs
;
2548 unsigned int lhsvar
= find (lhs
.var
);
2549 unsigned int rhsvar
= find (rhs
.var
);
2550 unsigned int lhsnode
, rhsnode
;
2551 unsigned int lhslabel
, rhslabel
;
2553 lhsnode
= si
->node_mapping
[lhsvar
];
2554 rhsnode
= si
->node_mapping
[rhsvar
];
2555 lhslabel
= graph
->pointer_label
[lhsnode
];
2556 rhslabel
= graph
->pointer_label
[rhsnode
];
2558 /* See if it is really a non-pointer variable, and if so, ignore
2562 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2565 fprintf (dump_file
, "%s is a non-pointer variable,"
2566 "ignoring constraint:",
2567 get_varinfo (lhs
.var
)->name
);
2568 dump_constraint (dump_file
, c
);
2569 fprintf (dump_file
, "\n");
2571 constraints
[i
] = NULL
;
2577 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2580 fprintf (dump_file
, "%s is a non-pointer variable,"
2581 "ignoring constraint:",
2582 get_varinfo (rhs
.var
)->name
);
2583 dump_constraint (dump_file
, c
);
2584 fprintf (dump_file
, "\n");
2586 constraints
[i
] = NULL
;
2590 lhsvar
= find_equivalent_node (graph
, lhsvar
, lhslabel
);
2591 rhsvar
= find_equivalent_node (graph
, rhsvar
, rhslabel
);
2592 c
->lhs
.var
= lhsvar
;
2593 c
->rhs
.var
= rhsvar
;
2597 /* Eliminate indirect cycles involving NODE. Return true if NODE was
2598 part of an SCC, false otherwise. */
2601 eliminate_indirect_cycles (unsigned int node
)
2603 if (graph
->indirect_cycles
[node
] != -1
2604 && !bitmap_empty_p (get_varinfo (node
)->solution
))
2607 auto_vec
<unsigned> queue
;
2609 unsigned int to
= find (graph
->indirect_cycles
[node
]);
2612 /* We can't touch the solution set and call unify_nodes
2613 at the same time, because unify_nodes is going to do
2614 bitmap unions into it. */
2616 EXECUTE_IF_SET_IN_BITMAP (get_varinfo (node
)->solution
, 0, i
, bi
)
2618 if (find (i
) == i
&& i
!= to
)
2621 queue
.safe_push (i
);
2626 queue
.iterate (queuepos
, &i
);
2629 unify_nodes (graph
, to
, i
, true);
2636 /* Solve the constraint graph GRAPH using our worklist solver.
2637 This is based on the PW* family of solvers from the "Efficient Field
2638 Sensitive Pointer Analysis for C" paper.
2639 It works by iterating over all the graph nodes, processing the complex
2640 constraints and propagating the copy constraints, until everything stops
2641 changed. This corresponds to steps 6-8 in the solving list given above. */
2644 solve_graph (constraint_graph_t graph
)
2646 unsigned int size
= graph
->size
;
2650 changed
= BITMAP_ALLOC (NULL
);
2652 /* Mark all initial non-collapsed nodes as changed. */
2653 for (i
= 1; i
< size
; i
++)
2655 varinfo_t ivi
= get_varinfo (i
);
2656 if (find (i
) == i
&& !bitmap_empty_p (ivi
->solution
)
2657 && ((graph
->succs
[i
] && !bitmap_empty_p (graph
->succs
[i
]))
2658 || graph
->complex[i
].length () > 0))
2659 bitmap_set_bit (changed
, i
);
2662 /* Allocate a bitmap to be used to store the changed bits. */
2663 pts
= BITMAP_ALLOC (&pta_obstack
);
2665 while (!bitmap_empty_p (changed
))
2668 struct topo_info
*ti
= init_topo_info ();
2671 bitmap_obstack_initialize (&iteration_obstack
);
2673 compute_topo_order (graph
, ti
);
2675 while (ti
->topo_order
.length () != 0)
2678 i
= ti
->topo_order
.pop ();
2680 /* If this variable is not a representative, skip it. */
2684 /* In certain indirect cycle cases, we may merge this
2685 variable to another. */
2686 if (eliminate_indirect_cycles (i
) && find (i
) != i
)
2689 /* If the node has changed, we need to process the
2690 complex constraints and outgoing edges again. */
2691 if (bitmap_clear_bit (changed
, i
))
2696 vec
<constraint_t
> complex = graph
->complex[i
];
2697 varinfo_t vi
= get_varinfo (i
);
2698 bool solution_empty
;
2700 /* Compute the changed set of solution bits. If anything
2701 is in the solution just propagate that. */
2702 if (bitmap_bit_p (vi
->solution
, anything_id
))
2704 /* If anything is also in the old solution there is
2706 ??? But we shouldn't ended up with "changed" set ... */
2708 && bitmap_bit_p (vi
->oldsolution
, anything_id
))
2710 bitmap_copy (pts
, get_varinfo (find (anything_id
))->solution
);
2712 else if (vi
->oldsolution
)
2713 bitmap_and_compl (pts
, vi
->solution
, vi
->oldsolution
);
2715 bitmap_copy (pts
, vi
->solution
);
2717 if (bitmap_empty_p (pts
))
2720 if (vi
->oldsolution
)
2721 bitmap_ior_into (vi
->oldsolution
, pts
);
2724 vi
->oldsolution
= BITMAP_ALLOC (&oldpta_obstack
);
2725 bitmap_copy (vi
->oldsolution
, pts
);
2728 solution
= vi
->solution
;
2729 solution_empty
= bitmap_empty_p (solution
);
2731 /* Process the complex constraints */
2732 bitmap expanded_pts
= NULL
;
2733 FOR_EACH_VEC_ELT (complex, j
, c
)
2735 /* XXX: This is going to unsort the constraints in
2736 some cases, which will occasionally add duplicate
2737 constraints during unification. This does not
2738 affect correctness. */
2739 c
->lhs
.var
= find (c
->lhs
.var
);
2740 c
->rhs
.var
= find (c
->rhs
.var
);
2742 /* The only complex constraint that can change our
2743 solution to non-empty, given an empty solution,
2744 is a constraint where the lhs side is receiving
2745 some set from elsewhere. */
2746 if (!solution_empty
|| c
->lhs
.type
!= DEREF
)
2747 do_complex_constraint (graph
, c
, pts
, &expanded_pts
);
2749 BITMAP_FREE (expanded_pts
);
2751 solution_empty
= bitmap_empty_p (solution
);
2753 if (!solution_empty
)
2756 unsigned eff_escaped_id
= find (escaped_id
);
2758 /* Propagate solution to all successors. */
2759 EXECUTE_IF_IN_NONNULL_BITMAP (graph
->succs
[i
],
2765 unsigned int to
= find (j
);
2766 tmp
= get_varinfo (to
)->solution
;
2769 /* Don't try to propagate to ourselves. */
2773 /* If we propagate from ESCAPED use ESCAPED as
2775 if (i
== eff_escaped_id
)
2776 flag
= bitmap_set_bit (tmp
, escaped_id
);
2778 flag
= bitmap_ior_into (tmp
, pts
);
2781 bitmap_set_bit (changed
, to
);
2786 free_topo_info (ti
);
2787 bitmap_obstack_release (&iteration_obstack
);
2791 BITMAP_FREE (changed
);
2792 bitmap_obstack_release (&oldpta_obstack
);
2795 /* Map from trees to variable infos. */
2796 static hash_map
<tree
, varinfo_t
> *vi_for_tree
;
2799 /* Insert ID as the variable id for tree T in the vi_for_tree map. */
2802 insert_vi_for_tree (tree t
, varinfo_t vi
)
2805 gcc_assert (!vi_for_tree
->put (t
, vi
));
2808 /* Find the variable info for tree T in VI_FOR_TREE. If T does not
2809 exist in the map, return NULL, otherwise, return the varinfo we found. */
2812 lookup_vi_for_tree (tree t
)
2814 varinfo_t
*slot
= vi_for_tree
->get (t
);
2821 /* Return a printable name for DECL */
2824 alias_get_name (tree decl
)
2826 const char *res
= NULL
;
2828 int num_printed
= 0;
2833 if (TREE_CODE (decl
) == SSA_NAME
)
2835 res
= get_name (decl
);
2837 num_printed
= asprintf (&temp
, "%s_%u", res
, SSA_NAME_VERSION (decl
));
2839 num_printed
= asprintf (&temp
, "_%u", SSA_NAME_VERSION (decl
));
2840 if (num_printed
> 0)
2842 res
= ggc_strdup (temp
);
2846 else if (DECL_P (decl
))
2848 if (DECL_ASSEMBLER_NAME_SET_P (decl
))
2849 res
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
2852 res
= get_name (decl
);
2855 num_printed
= asprintf (&temp
, "D.%u", DECL_UID (decl
));
2856 if (num_printed
> 0)
2858 res
= ggc_strdup (temp
);
2870 /* Find the variable id for tree T in the map.
2871 If T doesn't exist in the map, create an entry for it and return it. */
2874 get_vi_for_tree (tree t
)
2876 varinfo_t
*slot
= vi_for_tree
->get (t
);
2879 unsigned int id
= create_variable_info_for (t
, alias_get_name (t
), false);
2880 return get_varinfo (id
);
2886 /* Get a scalar constraint expression for a new temporary variable. */
2888 static struct constraint_expr
2889 new_scalar_tmp_constraint_exp (const char *name
, bool add_id
)
2891 struct constraint_expr tmp
;
2894 vi
= new_var_info (NULL_TREE
, name
, add_id
);
2898 vi
->is_full_var
= 1;
2907 /* Get a constraint expression vector from an SSA_VAR_P node.
2908 If address_p is true, the result will be taken its address of. */
2911 get_constraint_for_ssa_var (tree t
, vec
<ce_s
> *results
, bool address_p
)
2913 struct constraint_expr cexpr
;
2916 /* We allow FUNCTION_DECLs here even though it doesn't make much sense. */
2917 gcc_assert (TREE_CODE (t
) == SSA_NAME
|| DECL_P (t
));
2919 /* For parameters, get at the points-to set for the actual parm
2921 if (TREE_CODE (t
) == SSA_NAME
2922 && SSA_NAME_IS_DEFAULT_DEF (t
)
2923 && (TREE_CODE (SSA_NAME_VAR (t
)) == PARM_DECL
2924 || TREE_CODE (SSA_NAME_VAR (t
)) == RESULT_DECL
))
2926 get_constraint_for_ssa_var (SSA_NAME_VAR (t
), results
, address_p
);
2930 /* For global variables resort to the alias target. */
2931 if (VAR_P (t
) && (TREE_STATIC (t
) || DECL_EXTERNAL (t
)))
2933 varpool_node
*node
= varpool_node::get (t
);
2934 if (node
&& node
->alias
&& node
->analyzed
)
2936 node
= node
->ultimate_alias_target ();
2937 /* Canonicalize the PT uid of all aliases to the ultimate target.
2938 ??? Hopefully the set of aliases can't change in a way that
2939 changes the ultimate alias target. */
2940 gcc_assert ((! DECL_PT_UID_SET_P (node
->decl
)
2941 || DECL_PT_UID (node
->decl
) == DECL_UID (node
->decl
))
2942 && (! DECL_PT_UID_SET_P (t
)
2943 || DECL_PT_UID (t
) == DECL_UID (node
->decl
)));
2944 DECL_PT_UID (t
) = DECL_UID (node
->decl
);
2948 /* If this is decl may bind to NULL note that. */
2950 && (! node
|| ! node
->nonzero_address ()))
2952 cexpr
.var
= nothing_id
;
2953 cexpr
.type
= SCALAR
;
2955 results
->safe_push (cexpr
);
2959 vi
= get_vi_for_tree (t
);
2961 cexpr
.type
= SCALAR
;
2964 /* If we are not taking the address of the constraint expr, add all
2965 sub-fiels of the variable as well. */
2967 && !vi
->is_full_var
)
2969 for (; vi
; vi
= vi_next (vi
))
2972 results
->safe_push (cexpr
);
2977 results
->safe_push (cexpr
);
2980 /* Process constraint T, performing various simplifications and then
2981 adding it to our list of overall constraints. */
2984 process_constraint (constraint_t t
)
2986 struct constraint_expr rhs
= t
->rhs
;
2987 struct constraint_expr lhs
= t
->lhs
;
2989 gcc_assert (rhs
.var
< varmap
.length ());
2990 gcc_assert (lhs
.var
< varmap
.length ());
2992 /* If we didn't get any useful constraint from the lhs we get
2993 &ANYTHING as fallback from get_constraint_for. Deal with
2994 it here by turning it into *ANYTHING. */
2995 if (lhs
.type
== ADDRESSOF
2996 && lhs
.var
== anything_id
)
2999 /* ADDRESSOF on the lhs is invalid. */
3000 gcc_assert (lhs
.type
!= ADDRESSOF
);
3002 /* We shouldn't add constraints from things that cannot have pointers.
3003 It's not completely trivial to avoid in the callers, so do it here. */
3004 if (rhs
.type
!= ADDRESSOF
3005 && !get_varinfo (rhs
.var
)->may_have_pointers
)
3008 /* Likewise adding to the solution of a non-pointer var isn't useful. */
3009 if (!get_varinfo (lhs
.var
)->may_have_pointers
)
3012 /* This can happen in our IR with things like n->a = *p */
3013 if (rhs
.type
== DEREF
&& lhs
.type
== DEREF
&& rhs
.var
!= anything_id
)
3015 /* Split into tmp = *rhs, *lhs = tmp */
3016 struct constraint_expr tmplhs
;
3017 tmplhs
= new_scalar_tmp_constraint_exp ("doubledereftmp", true);
3018 process_constraint (new_constraint (tmplhs
, rhs
));
3019 process_constraint (new_constraint (lhs
, tmplhs
));
3021 else if ((rhs
.type
!= SCALAR
|| rhs
.offset
!= 0) && lhs
.type
== DEREF
)
3023 /* Split into tmp = &rhs, *lhs = tmp */
3024 struct constraint_expr tmplhs
;
3025 tmplhs
= new_scalar_tmp_constraint_exp ("derefaddrtmp", true);
3026 process_constraint (new_constraint (tmplhs
, rhs
));
3027 process_constraint (new_constraint (lhs
, tmplhs
));
3031 gcc_assert (rhs
.type
!= ADDRESSOF
|| rhs
.offset
== 0);
3032 constraints
.safe_push (t
);
3037 /* Return the position, in bits, of FIELD_DECL from the beginning of its
3040 static HOST_WIDE_INT
3041 bitpos_of_field (const tree fdecl
)
3043 if (!tree_fits_shwi_p (DECL_FIELD_OFFSET (fdecl
))
3044 || !tree_fits_shwi_p (DECL_FIELD_BIT_OFFSET (fdecl
)))
3047 return (tree_to_shwi (DECL_FIELD_OFFSET (fdecl
)) * BITS_PER_UNIT
3048 + tree_to_shwi (DECL_FIELD_BIT_OFFSET (fdecl
)));
3052 /* Get constraint expressions for offsetting PTR by OFFSET. Stores the
3053 resulting constraint expressions in *RESULTS. */
3056 get_constraint_for_ptr_offset (tree ptr
, tree offset
,
3059 struct constraint_expr c
;
3061 HOST_WIDE_INT rhsoffset
;
3063 /* If we do not do field-sensitive PTA adding offsets to pointers
3064 does not change the points-to solution. */
3065 if (!use_field_sensitive
)
3067 get_constraint_for_rhs (ptr
, results
);
3071 /* If the offset is not a non-negative integer constant that fits
3072 in a HOST_WIDE_INT, we have to fall back to a conservative
3073 solution which includes all sub-fields of all pointed-to
3074 variables of ptr. */
3075 if (offset
== NULL_TREE
3076 || TREE_CODE (offset
) != INTEGER_CST
)
3077 rhsoffset
= UNKNOWN_OFFSET
;
3080 /* Sign-extend the offset. */
3081 offset_int soffset
= offset_int::from (offset
, SIGNED
);
3082 if (!wi::fits_shwi_p (soffset
))
3083 rhsoffset
= UNKNOWN_OFFSET
;
3086 /* Make sure the bit-offset also fits. */
3087 HOST_WIDE_INT rhsunitoffset
= soffset
.to_shwi ();
3088 rhsoffset
= rhsunitoffset
* BITS_PER_UNIT
;
3089 if (rhsunitoffset
!= rhsoffset
/ BITS_PER_UNIT
)
3090 rhsoffset
= UNKNOWN_OFFSET
;
3094 get_constraint_for_rhs (ptr
, results
);
3098 /* As we are eventually appending to the solution do not use
3099 vec::iterate here. */
3100 n
= results
->length ();
3101 for (j
= 0; j
< n
; j
++)
3105 curr
= get_varinfo (c
.var
);
3107 if (c
.type
== ADDRESSOF
3108 /* If this varinfo represents a full variable just use it. */
3109 && curr
->is_full_var
)
3111 else if (c
.type
== ADDRESSOF
3112 /* If we do not know the offset add all subfields. */
3113 && rhsoffset
== UNKNOWN_OFFSET
)
3115 varinfo_t temp
= get_varinfo (curr
->head
);
3118 struct constraint_expr c2
;
3120 c2
.type
= ADDRESSOF
;
3122 if (c2
.var
!= c
.var
)
3123 results
->safe_push (c2
);
3124 temp
= vi_next (temp
);
3128 else if (c
.type
== ADDRESSOF
)
3131 unsigned HOST_WIDE_INT offset
= curr
->offset
+ rhsoffset
;
3133 /* If curr->offset + rhsoffset is less than zero adjust it. */
3135 && curr
->offset
< offset
)
3138 /* We have to include all fields that overlap the current
3139 field shifted by rhsoffset. And we include at least
3140 the last or the first field of the variable to represent
3141 reachability of off-bound addresses, in particular &object + 1,
3142 conservatively correct. */
3143 temp
= first_or_preceding_vi_for_offset (curr
, offset
);
3146 temp
= vi_next (temp
);
3148 && temp
->offset
< offset
+ curr
->size
)
3150 struct constraint_expr c2
;
3152 c2
.type
= ADDRESSOF
;
3154 results
->safe_push (c2
);
3155 temp
= vi_next (temp
);
3158 else if (c
.type
== SCALAR
)
3160 gcc_assert (c
.offset
== 0);
3161 c
.offset
= rhsoffset
;
3164 /* We shouldn't get any DEREFs here. */
3172 /* Given a COMPONENT_REF T, return the constraint_expr vector for it.
3173 If address_p is true the result will be taken its address of.
3174 If lhs_p is true then the constraint expression is assumed to be used
3178 get_constraint_for_component_ref (tree t
, vec
<ce_s
> *results
,
3179 bool address_p
, bool lhs_p
)
3182 HOST_WIDE_INT bitsize
= -1;
3183 HOST_WIDE_INT bitmaxsize
= -1;
3184 HOST_WIDE_INT bitpos
;
3188 /* Some people like to do cute things like take the address of
3191 while (handled_component_p (forzero
)
3192 || INDIRECT_REF_P (forzero
)
3193 || TREE_CODE (forzero
) == MEM_REF
)
3194 forzero
= TREE_OPERAND (forzero
, 0);
3196 if (CONSTANT_CLASS_P (forzero
) && integer_zerop (forzero
))
3198 struct constraint_expr temp
;
3201 temp
.var
= integer_id
;
3203 results
->safe_push (temp
);
3207 t
= get_ref_base_and_extent (t
, &bitpos
, &bitsize
, &bitmaxsize
, &reverse
);
3209 /* We can end up here for component references on a
3210 VIEW_CONVERT_EXPR <>(&foobar) or things like a
3211 BIT_FIELD_REF <&MEM[(void *)&b + 4B], ...>. So for
3212 symbolic constants simply give up. */
3213 if (TREE_CODE (t
) == ADDR_EXPR
)
3215 constraint_expr result
;
3216 result
.type
= SCALAR
;
3217 result
.var
= anything_id
;
3219 results
->safe_push (result
);
3223 /* Pretend to take the address of the base, we'll take care of
3224 adding the required subset of sub-fields below. */
3225 get_constraint_for_1 (t
, results
, true, lhs_p
);
3226 /* Strip off nothing_id. */
3227 if (results
->length () == 2)
3229 gcc_assert ((*results
)[0].var
== nothing_id
);
3230 results
->unordered_remove (0);
3232 gcc_assert (results
->length () == 1);
3233 struct constraint_expr
&result
= results
->last ();
3235 if (result
.type
== SCALAR
3236 && get_varinfo (result
.var
)->is_full_var
)
3237 /* For single-field vars do not bother about the offset. */
3239 else if (result
.type
== SCALAR
)
3241 /* In languages like C, you can access one past the end of an
3242 array. You aren't allowed to dereference it, so we can
3243 ignore this constraint. When we handle pointer subtraction,
3244 we may have to do something cute here. */
3246 if ((unsigned HOST_WIDE_INT
)bitpos
< get_varinfo (result
.var
)->fullsize
3249 /* It's also not true that the constraint will actually start at the
3250 right offset, it may start in some padding. We only care about
3251 setting the constraint to the first actual field it touches, so
3253 struct constraint_expr cexpr
= result
;
3257 for (curr
= get_varinfo (cexpr
.var
); curr
; curr
= vi_next (curr
))
3259 if (ranges_overlap_p (curr
->offset
, curr
->size
,
3260 bitpos
, bitmaxsize
))
3262 cexpr
.var
= curr
->id
;
3263 results
->safe_push (cexpr
);
3268 /* If we are going to take the address of this field then
3269 to be able to compute reachability correctly add at least
3270 the last field of the variable. */
3271 if (address_p
&& results
->length () == 0)
3273 curr
= get_varinfo (cexpr
.var
);
3274 while (curr
->next
!= 0)
3275 curr
= vi_next (curr
);
3276 cexpr
.var
= curr
->id
;
3277 results
->safe_push (cexpr
);
3279 else if (results
->length () == 0)
3280 /* Assert that we found *some* field there. The user couldn't be
3281 accessing *only* padding. */
3282 /* Still the user could access one past the end of an array
3283 embedded in a struct resulting in accessing *only* padding. */
3284 /* Or accessing only padding via type-punning to a type
3285 that has a filed just in padding space. */
3287 cexpr
.type
= SCALAR
;
3288 cexpr
.var
= anything_id
;
3290 results
->safe_push (cexpr
);
3293 else if (bitmaxsize
== 0)
3295 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3296 fprintf (dump_file
, "Access to zero-sized part of variable,"
3300 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3301 fprintf (dump_file
, "Access to past the end of variable, ignoring\n");
3303 else if (result
.type
== DEREF
)
3305 /* If we do not know exactly where the access goes say so. Note
3306 that only for non-structure accesses we know that we access
3307 at most one subfiled of any variable. */
3309 || bitsize
!= bitmaxsize
3310 || AGGREGATE_TYPE_P (TREE_TYPE (orig_t
))
3311 || result
.offset
== UNKNOWN_OFFSET
)
3312 result
.offset
= UNKNOWN_OFFSET
;
3314 result
.offset
+= bitpos
;
3316 else if (result
.type
== ADDRESSOF
)
3318 /* We can end up here for component references on constants like
3319 VIEW_CONVERT_EXPR <>({ 0, 1, 2, 3 })[i]. */
3320 result
.type
= SCALAR
;
3321 result
.var
= anything_id
;
3329 /* Dereference the constraint expression CONS, and return the result.
3330 DEREF (ADDRESSOF) = SCALAR
3331 DEREF (SCALAR) = DEREF
3332 DEREF (DEREF) = (temp = DEREF1; result = DEREF(temp))
3333 This is needed so that we can handle dereferencing DEREF constraints. */
3336 do_deref (vec
<ce_s
> *constraints
)
3338 struct constraint_expr
*c
;
3341 FOR_EACH_VEC_ELT (*constraints
, i
, c
)
3343 if (c
->type
== SCALAR
)
3345 else if (c
->type
== ADDRESSOF
)
3347 else if (c
->type
== DEREF
)
3349 struct constraint_expr tmplhs
;
3350 tmplhs
= new_scalar_tmp_constraint_exp ("dereftmp", true);
3351 process_constraint (new_constraint (tmplhs
, *c
));
3352 c
->var
= tmplhs
.var
;
3359 /* Given a tree T, return the constraint expression for taking the
3363 get_constraint_for_address_of (tree t
, vec
<ce_s
> *results
)
3365 struct constraint_expr
*c
;
3368 get_constraint_for_1 (t
, results
, true, true);
3370 FOR_EACH_VEC_ELT (*results
, i
, c
)
3372 if (c
->type
== DEREF
)
3375 c
->type
= ADDRESSOF
;
3379 /* Given a tree T, return the constraint expression for it. */
3382 get_constraint_for_1 (tree t
, vec
<ce_s
> *results
, bool address_p
,
3385 struct constraint_expr temp
;
3387 /* x = integer is all glommed to a single variable, which doesn't
3388 point to anything by itself. That is, of course, unless it is an
3389 integer constant being treated as a pointer, in which case, we
3390 will return that this is really the addressof anything. This
3391 happens below, since it will fall into the default case. The only
3392 case we know something about an integer treated like a pointer is
3393 when it is the NULL pointer, and then we just say it points to
3396 Do not do that if -fno-delete-null-pointer-checks though, because
3397 in that case *NULL does not fail, so it _should_ alias *anything.
3398 It is not worth adding a new option or renaming the existing one,
3399 since this case is relatively obscure. */
3400 if ((TREE_CODE (t
) == INTEGER_CST
3401 && integer_zerop (t
))
3402 /* The only valid CONSTRUCTORs in gimple with pointer typed
3403 elements are zero-initializer. But in IPA mode we also
3404 process global initializers, so verify at least. */
3405 || (TREE_CODE (t
) == CONSTRUCTOR
3406 && CONSTRUCTOR_NELTS (t
) == 0))
3408 if (flag_delete_null_pointer_checks
)
3409 temp
.var
= nothing_id
;
3411 temp
.var
= nonlocal_id
;
3412 temp
.type
= ADDRESSOF
;
3414 results
->safe_push (temp
);
3418 /* String constants are read-only, ideally we'd have a CONST_DECL
3420 if (TREE_CODE (t
) == STRING_CST
)
3422 temp
.var
= string_id
;
3425 results
->safe_push (temp
);
3429 switch (TREE_CODE_CLASS (TREE_CODE (t
)))
3431 case tcc_expression
:
3433 switch (TREE_CODE (t
))
3436 get_constraint_for_address_of (TREE_OPERAND (t
, 0), results
);
3444 switch (TREE_CODE (t
))
3448 struct constraint_expr cs
;
3450 get_constraint_for_ptr_offset (TREE_OPERAND (t
, 0),
3451 TREE_OPERAND (t
, 1), results
);
3454 /* If we are not taking the address then make sure to process
3455 all subvariables we might access. */
3459 cs
= results
->last ();
3460 if (cs
.type
== DEREF
3461 && type_can_have_subvars (TREE_TYPE (t
)))
3463 /* For dereferences this means we have to defer it
3465 results
->last ().offset
= UNKNOWN_OFFSET
;
3468 if (cs
.type
!= SCALAR
)
3471 vi
= get_varinfo (cs
.var
);
3472 curr
= vi_next (vi
);
3473 if (!vi
->is_full_var
3476 unsigned HOST_WIDE_INT size
;
3477 if (tree_fits_uhwi_p (TYPE_SIZE (TREE_TYPE (t
))))
3478 size
= tree_to_uhwi (TYPE_SIZE (TREE_TYPE (t
)));
3481 for (; curr
; curr
= vi_next (curr
))
3483 if (curr
->offset
- vi
->offset
< size
)
3486 results
->safe_push (cs
);
3495 case ARRAY_RANGE_REF
:
3500 get_constraint_for_component_ref (t
, results
, address_p
, lhs_p
);
3502 case VIEW_CONVERT_EXPR
:
3503 get_constraint_for_1 (TREE_OPERAND (t
, 0), results
, address_p
,
3506 /* We are missing handling for TARGET_MEM_REF here. */
3511 case tcc_exceptional
:
3513 switch (TREE_CODE (t
))
3517 get_constraint_for_ssa_var (t
, results
, address_p
);
3525 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t
), i
, val
)
3527 struct constraint_expr
*rhsp
;
3529 get_constraint_for_1 (val
, &tmp
, address_p
, lhs_p
);
3530 FOR_EACH_VEC_ELT (tmp
, j
, rhsp
)
3531 results
->safe_push (*rhsp
);
3534 /* We do not know whether the constructor was complete,
3535 so technically we have to add &NOTHING or &ANYTHING
3536 like we do for an empty constructor as well. */
3543 case tcc_declaration
:
3545 get_constraint_for_ssa_var (t
, results
, address_p
);
3550 /* We cannot refer to automatic variables through constants. */
3551 temp
.type
= ADDRESSOF
;
3552 temp
.var
= nonlocal_id
;
3554 results
->safe_push (temp
);
3560 /* The default fallback is a constraint from anything. */
3561 temp
.type
= ADDRESSOF
;
3562 temp
.var
= anything_id
;
3564 results
->safe_push (temp
);
3567 /* Given a gimple tree T, return the constraint expression vector for it. */
3570 get_constraint_for (tree t
, vec
<ce_s
> *results
)
3572 gcc_assert (results
->length () == 0);
3574 get_constraint_for_1 (t
, results
, false, true);
3577 /* Given a gimple tree T, return the constraint expression vector for it
3578 to be used as the rhs of a constraint. */
3581 get_constraint_for_rhs (tree t
, vec
<ce_s
> *results
)
3583 gcc_assert (results
->length () == 0);
3585 get_constraint_for_1 (t
, results
, false, false);
3589 /* Efficiently generates constraints from all entries in *RHSC to all
3590 entries in *LHSC. */
3593 process_all_all_constraints (vec
<ce_s
> lhsc
,
3596 struct constraint_expr
*lhsp
, *rhsp
;
3599 if (lhsc
.length () <= 1 || rhsc
.length () <= 1)
3601 FOR_EACH_VEC_ELT (lhsc
, i
, lhsp
)
3602 FOR_EACH_VEC_ELT (rhsc
, j
, rhsp
)
3603 process_constraint (new_constraint (*lhsp
, *rhsp
));
3607 struct constraint_expr tmp
;
3608 tmp
= new_scalar_tmp_constraint_exp ("allalltmp", true);
3609 FOR_EACH_VEC_ELT (rhsc
, i
, rhsp
)
3610 process_constraint (new_constraint (tmp
, *rhsp
));
3611 FOR_EACH_VEC_ELT (lhsc
, i
, lhsp
)
3612 process_constraint (new_constraint (*lhsp
, tmp
));
3616 /* Handle aggregate copies by expanding into copies of the respective
3617 fields of the structures. */
3620 do_structure_copy (tree lhsop
, tree rhsop
)
3622 struct constraint_expr
*lhsp
, *rhsp
;
3623 auto_vec
<ce_s
> lhsc
;
3624 auto_vec
<ce_s
> rhsc
;
3627 get_constraint_for (lhsop
, &lhsc
);
3628 get_constraint_for_rhs (rhsop
, &rhsc
);
3631 if (lhsp
->type
== DEREF
3632 || (lhsp
->type
== ADDRESSOF
&& lhsp
->var
== anything_id
)
3633 || rhsp
->type
== DEREF
)
3635 if (lhsp
->type
== DEREF
)
3637 gcc_assert (lhsc
.length () == 1);
3638 lhsp
->offset
= UNKNOWN_OFFSET
;
3640 if (rhsp
->type
== DEREF
)
3642 gcc_assert (rhsc
.length () == 1);
3643 rhsp
->offset
= UNKNOWN_OFFSET
;
3645 process_all_all_constraints (lhsc
, rhsc
);
3647 else if (lhsp
->type
== SCALAR
3648 && (rhsp
->type
== SCALAR
3649 || rhsp
->type
== ADDRESSOF
))
3651 HOST_WIDE_INT lhssize
, lhsmaxsize
, lhsoffset
;
3652 HOST_WIDE_INT rhssize
, rhsmaxsize
, rhsoffset
;
3655 get_ref_base_and_extent (lhsop
, &lhsoffset
, &lhssize
, &lhsmaxsize
,
3657 get_ref_base_and_extent (rhsop
, &rhsoffset
, &rhssize
, &rhsmaxsize
,
3659 for (j
= 0; lhsc
.iterate (j
, &lhsp
);)
3661 varinfo_t lhsv
, rhsv
;
3663 lhsv
= get_varinfo (lhsp
->var
);
3664 rhsv
= get_varinfo (rhsp
->var
);
3665 if (lhsv
->may_have_pointers
3666 && (lhsv
->is_full_var
3667 || rhsv
->is_full_var
3668 || ranges_overlap_p (lhsv
->offset
+ rhsoffset
, lhsv
->size
,
3669 rhsv
->offset
+ lhsoffset
, rhsv
->size
)))
3670 process_constraint (new_constraint (*lhsp
, *rhsp
));
3671 if (!rhsv
->is_full_var
3672 && (lhsv
->is_full_var
3673 || (lhsv
->offset
+ rhsoffset
+ lhsv
->size
3674 > rhsv
->offset
+ lhsoffset
+ rhsv
->size
)))
3677 if (k
>= rhsc
.length ())
3688 /* Create constraints ID = { rhsc }. */
3691 make_constraints_to (unsigned id
, vec
<ce_s
> rhsc
)
3693 struct constraint_expr
*c
;
3694 struct constraint_expr includes
;
3698 includes
.offset
= 0;
3699 includes
.type
= SCALAR
;
3701 FOR_EACH_VEC_ELT (rhsc
, j
, c
)
3702 process_constraint (new_constraint (includes
, *c
));
3705 /* Create a constraint ID = OP. */
3708 make_constraint_to (unsigned id
, tree op
)
3710 auto_vec
<ce_s
> rhsc
;
3711 get_constraint_for_rhs (op
, &rhsc
);
3712 make_constraints_to (id
, rhsc
);
3715 /* Create a constraint ID = &FROM. */
3718 make_constraint_from (varinfo_t vi
, int from
)
3720 struct constraint_expr lhs
, rhs
;
3728 rhs
.type
= ADDRESSOF
;
3729 process_constraint (new_constraint (lhs
, rhs
));
3732 /* Create a constraint ID = FROM. */
3735 make_copy_constraint (varinfo_t vi
, int from
)
3737 struct constraint_expr lhs
, rhs
;
3746 process_constraint (new_constraint (lhs
, rhs
));
3749 /* Make constraints necessary to make OP escape. */
3752 make_escape_constraint (tree op
)
3754 make_constraint_to (escaped_id
, op
);
3757 /* Add constraints to that the solution of VI is transitively closed. */
3760 make_transitive_closure_constraints (varinfo_t vi
)
3762 struct constraint_expr lhs
, rhs
;
3764 /* VAR = *(VAR + UNKNOWN); */
3770 rhs
.offset
= UNKNOWN_OFFSET
;
3771 process_constraint (new_constraint (lhs
, rhs
));
3774 /* Add constraints to that the solution of VI has all subvariables added. */
3777 make_any_offset_constraints (varinfo_t vi
)
3779 struct constraint_expr lhs
, rhs
;
3781 /* VAR = VAR + UNKNOWN; */
3787 rhs
.offset
= UNKNOWN_OFFSET
;
3788 process_constraint (new_constraint (lhs
, rhs
));
3791 /* Temporary storage for fake var decls. */
3792 struct obstack fake_var_decl_obstack
;
3794 /* Build a fake VAR_DECL acting as referrer to a DECL_UID. */
3797 build_fake_var_decl (tree type
)
3799 tree decl
= (tree
) XOBNEW (&fake_var_decl_obstack
, struct tree_var_decl
);
3800 memset (decl
, 0, sizeof (struct tree_var_decl
));
3801 TREE_SET_CODE (decl
, VAR_DECL
);
3802 TREE_TYPE (decl
) = type
;
3803 DECL_UID (decl
) = allocate_decl_uid ();
3804 SET_DECL_PT_UID (decl
, -1);
3805 layout_decl (decl
, 0);
3809 /* Create a new artificial heap variable with NAME.
3810 Return the created variable. */
3813 make_heapvar (const char *name
, bool add_id
)
3818 heapvar
= build_fake_var_decl (ptr_type_node
);
3819 DECL_EXTERNAL (heapvar
) = 1;
3821 vi
= new_var_info (heapvar
, name
, add_id
);
3822 vi
->is_artificial_var
= true;
3823 vi
->is_heap_var
= true;
3824 vi
->is_unknown_size_var
= true;
3828 vi
->is_full_var
= true;
3829 insert_vi_for_tree (heapvar
, vi
);
3834 /* Create a new artificial heap variable with NAME and make a
3835 constraint from it to LHS. Set flags according to a tag used
3836 for tracking restrict pointers. */
3839 make_constraint_from_restrict (varinfo_t lhs
, const char *name
, bool add_id
)
3841 varinfo_t vi
= make_heapvar (name
, add_id
);
3842 vi
->is_restrict_var
= 1;
3843 vi
->is_global_var
= 1;
3844 vi
->may_have_pointers
= 1;
3845 make_constraint_from (lhs
, vi
->id
);
3849 /* Create a new artificial heap variable with NAME and make a
3850 constraint from it to LHS. Set flags according to a tag used
3851 for tracking restrict pointers and make the artificial heap
3852 point to global memory. */
3855 make_constraint_from_global_restrict (varinfo_t lhs
, const char *name
,
3858 varinfo_t vi
= make_constraint_from_restrict (lhs
, name
, add_id
);
3859 make_copy_constraint (vi
, nonlocal_id
);
3863 /* In IPA mode there are varinfos for different aspects of reach
3864 function designator. One for the points-to set of the return
3865 value, one for the variables that are clobbered by the function,
3866 one for its uses and one for each parameter (including a single
3867 glob for remaining variadic arguments). */
3869 enum { fi_clobbers
= 1, fi_uses
= 2,
3870 fi_static_chain
= 3, fi_result
= 4, fi_parm_base
= 5 };
3872 /* Get a constraint for the requested part of a function designator FI
3873 when operating in IPA mode. */
3875 static struct constraint_expr
3876 get_function_part_constraint (varinfo_t fi
, unsigned part
)
3878 struct constraint_expr c
;
3880 gcc_assert (in_ipa_mode
);
3882 if (fi
->id
== anything_id
)
3884 /* ??? We probably should have a ANYFN special variable. */
3885 c
.var
= anything_id
;
3889 else if (TREE_CODE (fi
->decl
) == FUNCTION_DECL
)
3891 varinfo_t ai
= first_vi_for_offset (fi
, part
);
3895 c
.var
= anything_id
;
3909 /* For non-IPA mode, generate constraints necessary for a call on the
3913 handle_rhs_call (gcall
*stmt
, vec
<ce_s
> *results
)
3915 struct constraint_expr rhsc
;
3917 bool returns_uses
= false;
3919 for (i
= 0; i
< gimple_call_num_args (stmt
); ++i
)
3921 tree arg
= gimple_call_arg (stmt
, i
);
3922 int flags
= gimple_call_arg_flags (stmt
, i
);
3924 /* If the argument is not used we can ignore it. */
3925 if (flags
& EAF_UNUSED
)
3928 /* As we compute ESCAPED context-insensitive we do not gain
3929 any precision with just EAF_NOCLOBBER but not EAF_NOESCAPE
3930 set. The argument would still get clobbered through the
3932 if ((flags
& EAF_NOCLOBBER
)
3933 && (flags
& EAF_NOESCAPE
))
3935 varinfo_t uses
= get_call_use_vi (stmt
);
3936 varinfo_t tem
= new_var_info (NULL_TREE
, "callarg", true);
3937 make_constraint_to (tem
->id
, arg
);
3938 make_any_offset_constraints (tem
);
3939 if (!(flags
& EAF_DIRECT
))
3940 make_transitive_closure_constraints (tem
);
3941 make_copy_constraint (uses
, tem
->id
);
3942 returns_uses
= true;
3944 else if (flags
& EAF_NOESCAPE
)
3946 struct constraint_expr lhs
, rhs
;
3947 varinfo_t uses
= get_call_use_vi (stmt
);
3948 varinfo_t clobbers
= get_call_clobber_vi (stmt
);
3949 varinfo_t tem
= new_var_info (NULL_TREE
, "callarg", true);
3950 make_constraint_to (tem
->id
, arg
);
3951 make_any_offset_constraints (tem
);
3952 if (!(flags
& EAF_DIRECT
))
3953 make_transitive_closure_constraints (tem
);
3954 make_copy_constraint (uses
, tem
->id
);
3955 make_copy_constraint (clobbers
, tem
->id
);
3956 /* Add *tem = nonlocal, do not add *tem = callused as
3957 EAF_NOESCAPE parameters do not escape to other parameters
3958 and all other uses appear in NONLOCAL as well. */
3963 rhs
.var
= nonlocal_id
;
3965 process_constraint (new_constraint (lhs
, rhs
));
3966 returns_uses
= true;
3969 make_escape_constraint (arg
);
3972 /* If we added to the calls uses solution make sure we account for
3973 pointers to it to be returned. */
3976 rhsc
.var
= get_call_use_vi (stmt
)->id
;
3977 rhsc
.offset
= UNKNOWN_OFFSET
;
3979 results
->safe_push (rhsc
);
3982 /* The static chain escapes as well. */
3983 if (gimple_call_chain (stmt
))
3984 make_escape_constraint (gimple_call_chain (stmt
));
3986 /* And if we applied NRV the address of the return slot escapes as well. */
3987 if (gimple_call_return_slot_opt_p (stmt
)
3988 && gimple_call_lhs (stmt
) != NULL_TREE
3989 && TREE_ADDRESSABLE (TREE_TYPE (gimple_call_lhs (stmt
))))
3991 auto_vec
<ce_s
> tmpc
;
3992 struct constraint_expr lhsc
, *c
;
3993 get_constraint_for_address_of (gimple_call_lhs (stmt
), &tmpc
);
3994 lhsc
.var
= escaped_id
;
3997 FOR_EACH_VEC_ELT (tmpc
, i
, c
)
3998 process_constraint (new_constraint (lhsc
, *c
));
4001 /* Regular functions return nonlocal memory. */
4002 rhsc
.var
= nonlocal_id
;
4005 results
->safe_push (rhsc
);
4008 /* For non-IPA mode, generate constraints necessary for a call
4009 that returns a pointer and assigns it to LHS. This simply makes
4010 the LHS point to global and escaped variables. */
4013 handle_lhs_call (gcall
*stmt
, tree lhs
, int flags
, vec
<ce_s
> rhsc
,
4016 auto_vec
<ce_s
> lhsc
;
4018 get_constraint_for (lhs
, &lhsc
);
4019 /* If the store is to a global decl make sure to
4020 add proper escape constraints. */
4021 lhs
= get_base_address (lhs
);
4024 && is_global_var (lhs
))
4026 struct constraint_expr tmpc
;
4027 tmpc
.var
= escaped_id
;
4030 lhsc
.safe_push (tmpc
);
4033 /* If the call returns an argument unmodified override the rhs
4035 if (flags
& ERF_RETURNS_ARG
4036 && (flags
& ERF_RETURN_ARG_MASK
) < gimple_call_num_args (stmt
))
4040 arg
= gimple_call_arg (stmt
, flags
& ERF_RETURN_ARG_MASK
);
4041 get_constraint_for (arg
, &rhsc
);
4042 process_all_all_constraints (lhsc
, rhsc
);
4045 else if (flags
& ERF_NOALIAS
)
4048 struct constraint_expr tmpc
;
4050 vi
= make_heapvar ("HEAP", true);
4051 /* We are marking allocated storage local, we deal with it becoming
4052 global by escaping and setting of vars_contains_escaped_heap. */
4053 DECL_EXTERNAL (vi
->decl
) = 0;
4054 vi
->is_global_var
= 0;
4055 /* If this is not a real malloc call assume the memory was
4056 initialized and thus may point to global memory. All
4057 builtin functions with the malloc attribute behave in a sane way. */
4059 || DECL_BUILT_IN_CLASS (fndecl
) != BUILT_IN_NORMAL
)
4060 make_constraint_from (vi
, nonlocal_id
);
4063 tmpc
.type
= ADDRESSOF
;
4064 rhsc
.safe_push (tmpc
);
4065 process_all_all_constraints (lhsc
, rhsc
);
4069 process_all_all_constraints (lhsc
, rhsc
);
4072 /* For non-IPA mode, generate constraints necessary for a call of a
4073 const function that returns a pointer in the statement STMT. */
4076 handle_const_call (gcall
*stmt
, vec
<ce_s
> *results
)
4078 struct constraint_expr rhsc
;
4080 bool need_uses
= false;
4082 /* Treat nested const functions the same as pure functions as far
4083 as the static chain is concerned. */
4084 if (gimple_call_chain (stmt
))
4086 varinfo_t uses
= get_call_use_vi (stmt
);
4087 make_constraint_to (uses
->id
, gimple_call_chain (stmt
));
4091 /* And if we applied NRV the address of the return slot escapes as well. */
4092 if (gimple_call_return_slot_opt_p (stmt
)
4093 && gimple_call_lhs (stmt
) != NULL_TREE
4094 && TREE_ADDRESSABLE (TREE_TYPE (gimple_call_lhs (stmt
))))
4096 varinfo_t uses
= get_call_use_vi (stmt
);
4097 auto_vec
<ce_s
> tmpc
;
4098 get_constraint_for_address_of (gimple_call_lhs (stmt
), &tmpc
);
4099 make_constraints_to (uses
->id
, tmpc
);
4105 varinfo_t uses
= get_call_use_vi (stmt
);
4106 make_any_offset_constraints (uses
);
4107 make_transitive_closure_constraints (uses
);
4108 rhsc
.var
= uses
->id
;
4111 results
->safe_push (rhsc
);
4114 /* May return offsetted arguments. */
4115 varinfo_t tem
= NULL
;
4116 if (gimple_call_num_args (stmt
) != 0)
4117 tem
= new_var_info (NULL_TREE
, "callarg", true);
4118 for (k
= 0; k
< gimple_call_num_args (stmt
); ++k
)
4120 tree arg
= gimple_call_arg (stmt
, k
);
4121 auto_vec
<ce_s
> argc
;
4122 get_constraint_for_rhs (arg
, &argc
);
4123 make_constraints_to (tem
->id
, argc
);
4130 ce
.offset
= UNKNOWN_OFFSET
;
4131 results
->safe_push (ce
);
4134 /* May return addresses of globals. */
4135 rhsc
.var
= nonlocal_id
;
4137 rhsc
.type
= ADDRESSOF
;
4138 results
->safe_push (rhsc
);
4141 /* For non-IPA mode, generate constraints necessary for a call to a
4142 pure function in statement STMT. */
4145 handle_pure_call (gcall
*stmt
, vec
<ce_s
> *results
)
4147 struct constraint_expr rhsc
;
4149 varinfo_t uses
= NULL
;
4151 /* Memory reached from pointer arguments is call-used. */
4152 for (i
= 0; i
< gimple_call_num_args (stmt
); ++i
)
4154 tree arg
= gimple_call_arg (stmt
, i
);
4157 uses
= get_call_use_vi (stmt
);
4158 make_any_offset_constraints (uses
);
4159 make_transitive_closure_constraints (uses
);
4161 make_constraint_to (uses
->id
, arg
);
4164 /* The static chain is used as well. */
4165 if (gimple_call_chain (stmt
))
4169 uses
= get_call_use_vi (stmt
);
4170 make_any_offset_constraints (uses
);
4171 make_transitive_closure_constraints (uses
);
4173 make_constraint_to (uses
->id
, gimple_call_chain (stmt
));
4176 /* And if we applied NRV the address of the return slot. */
4177 if (gimple_call_return_slot_opt_p (stmt
)
4178 && gimple_call_lhs (stmt
) != NULL_TREE
4179 && TREE_ADDRESSABLE (TREE_TYPE (gimple_call_lhs (stmt
))))
4183 uses
= get_call_use_vi (stmt
);
4184 make_any_offset_constraints (uses
);
4185 make_transitive_closure_constraints (uses
);
4187 auto_vec
<ce_s
> tmpc
;
4188 get_constraint_for_address_of (gimple_call_lhs (stmt
), &tmpc
);
4189 make_constraints_to (uses
->id
, tmpc
);
4192 /* Pure functions may return call-used and nonlocal memory. */
4195 rhsc
.var
= uses
->id
;
4198 results
->safe_push (rhsc
);
4200 rhsc
.var
= nonlocal_id
;
4203 results
->safe_push (rhsc
);
4207 /* Return the varinfo for the callee of CALL. */
4210 get_fi_for_callee (gcall
*call
)
4212 tree decl
, fn
= gimple_call_fn (call
);
4214 if (fn
&& TREE_CODE (fn
) == OBJ_TYPE_REF
)
4215 fn
= OBJ_TYPE_REF_EXPR (fn
);
4217 /* If we can directly resolve the function being called, do so.
4218 Otherwise, it must be some sort of indirect expression that
4219 we should still be able to handle. */
4220 decl
= gimple_call_addr_fndecl (fn
);
4222 return get_vi_for_tree (decl
);
4224 /* If the function is anything other than a SSA name pointer we have no
4225 clue and should be getting ANYFN (well, ANYTHING for now). */
4226 if (!fn
|| TREE_CODE (fn
) != SSA_NAME
)
4227 return get_varinfo (anything_id
);
4229 if (SSA_NAME_IS_DEFAULT_DEF (fn
)
4230 && (TREE_CODE (SSA_NAME_VAR (fn
)) == PARM_DECL
4231 || TREE_CODE (SSA_NAME_VAR (fn
)) == RESULT_DECL
))
4232 fn
= SSA_NAME_VAR (fn
);
4234 return get_vi_for_tree (fn
);
4237 /* Create constraints for assigning call argument ARG to the incoming parameter
4238 INDEX of function FI. */
4241 find_func_aliases_for_call_arg (varinfo_t fi
, unsigned index
, tree arg
)
4243 struct constraint_expr lhs
;
4244 lhs
= get_function_part_constraint (fi
, fi_parm_base
+ index
);
4246 auto_vec
<ce_s
, 2> rhsc
;
4247 get_constraint_for_rhs (arg
, &rhsc
);
4250 struct constraint_expr
*rhsp
;
4251 FOR_EACH_VEC_ELT (rhsc
, j
, rhsp
)
4252 process_constraint (new_constraint (lhs
, *rhsp
));
4255 /* Return true if FNDECL may be part of another lto partition. */
4258 fndecl_maybe_in_other_partition (tree fndecl
)
4260 cgraph_node
*fn_node
= cgraph_node::get (fndecl
);
4261 if (fn_node
== NULL
)
4264 return fn_node
->in_other_partition
;
4267 /* Create constraints for the builtin call T. Return true if the call
4268 was handled, otherwise false. */
4271 find_func_aliases_for_builtin_call (struct function
*fn
, gcall
*t
)
4273 tree fndecl
= gimple_call_fndecl (t
);
4274 auto_vec
<ce_s
, 2> lhsc
;
4275 auto_vec
<ce_s
, 4> rhsc
;
4278 if (gimple_call_builtin_p (t
, BUILT_IN_NORMAL
))
4279 /* ??? All builtins that are handled here need to be handled
4280 in the alias-oracle query functions explicitly! */
4281 switch (DECL_FUNCTION_CODE (fndecl
))
4283 /* All the following functions return a pointer to the same object
4284 as their first argument points to. The functions do not add
4285 to the ESCAPED solution. The functions make the first argument
4286 pointed to memory point to what the second argument pointed to
4287 memory points to. */
4288 case BUILT_IN_STRCPY
:
4289 case BUILT_IN_STRNCPY
:
4290 case BUILT_IN_BCOPY
:
4291 case BUILT_IN_MEMCPY
:
4292 case BUILT_IN_MEMMOVE
:
4293 case BUILT_IN_MEMPCPY
:
4294 case BUILT_IN_STPCPY
:
4295 case BUILT_IN_STPNCPY
:
4296 case BUILT_IN_STRCAT
:
4297 case BUILT_IN_STRNCAT
:
4298 case BUILT_IN_STRCPY_CHK
:
4299 case BUILT_IN_STRNCPY_CHK
:
4300 case BUILT_IN_MEMCPY_CHK
:
4301 case BUILT_IN_MEMMOVE_CHK
:
4302 case BUILT_IN_MEMPCPY_CHK
:
4303 case BUILT_IN_STPCPY_CHK
:
4304 case BUILT_IN_STPNCPY_CHK
:
4305 case BUILT_IN_STRCAT_CHK
:
4306 case BUILT_IN_STRNCAT_CHK
:
4307 case BUILT_IN_TM_MEMCPY
:
4308 case BUILT_IN_TM_MEMMOVE
:
4310 tree res
= gimple_call_lhs (t
);
4311 tree dest
= gimple_call_arg (t
, (DECL_FUNCTION_CODE (fndecl
)
4312 == BUILT_IN_BCOPY
? 1 : 0));
4313 tree src
= gimple_call_arg (t
, (DECL_FUNCTION_CODE (fndecl
)
4314 == BUILT_IN_BCOPY
? 0 : 1));
4315 if (res
!= NULL_TREE
)
4317 get_constraint_for (res
, &lhsc
);
4318 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_MEMPCPY
4319 || DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_STPCPY
4320 || DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_STPNCPY
4321 || DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_MEMPCPY_CHK
4322 || DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_STPCPY_CHK
4323 || DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_STPNCPY_CHK
)
4324 get_constraint_for_ptr_offset (dest
, NULL_TREE
, &rhsc
);
4326 get_constraint_for (dest
, &rhsc
);
4327 process_all_all_constraints (lhsc
, rhsc
);
4331 get_constraint_for_ptr_offset (dest
, NULL_TREE
, &lhsc
);
4332 get_constraint_for_ptr_offset (src
, NULL_TREE
, &rhsc
);
4335 process_all_all_constraints (lhsc
, rhsc
);
4338 case BUILT_IN_MEMSET
:
4339 case BUILT_IN_MEMSET_CHK
:
4340 case BUILT_IN_TM_MEMSET
:
4342 tree res
= gimple_call_lhs (t
);
4343 tree dest
= gimple_call_arg (t
, 0);
4346 struct constraint_expr ac
;
4347 if (res
!= NULL_TREE
)
4349 get_constraint_for (res
, &lhsc
);
4350 get_constraint_for (dest
, &rhsc
);
4351 process_all_all_constraints (lhsc
, rhsc
);
4354 get_constraint_for_ptr_offset (dest
, NULL_TREE
, &lhsc
);
4356 if (flag_delete_null_pointer_checks
4357 && integer_zerop (gimple_call_arg (t
, 1)))
4359 ac
.type
= ADDRESSOF
;
4360 ac
.var
= nothing_id
;
4365 ac
.var
= integer_id
;
4368 FOR_EACH_VEC_ELT (lhsc
, i
, lhsp
)
4369 process_constraint (new_constraint (*lhsp
, ac
));
4372 case BUILT_IN_POSIX_MEMALIGN
:
4374 tree ptrptr
= gimple_call_arg (t
, 0);
4375 get_constraint_for (ptrptr
, &lhsc
);
4377 varinfo_t vi
= make_heapvar ("HEAP", true);
4378 /* We are marking allocated storage local, we deal with it becoming
4379 global by escaping and setting of vars_contains_escaped_heap. */
4380 DECL_EXTERNAL (vi
->decl
) = 0;
4381 vi
->is_global_var
= 0;
4382 struct constraint_expr tmpc
;
4385 tmpc
.type
= ADDRESSOF
;
4386 rhsc
.safe_push (tmpc
);
4387 process_all_all_constraints (lhsc
, rhsc
);
4390 case BUILT_IN_ASSUME_ALIGNED
:
4392 tree res
= gimple_call_lhs (t
);
4393 tree dest
= gimple_call_arg (t
, 0);
4394 if (res
!= NULL_TREE
)
4396 get_constraint_for (res
, &lhsc
);
4397 get_constraint_for (dest
, &rhsc
);
4398 process_all_all_constraints (lhsc
, rhsc
);
4402 /* All the following functions do not return pointers, do not
4403 modify the points-to sets of memory reachable from their
4404 arguments and do not add to the ESCAPED solution. */
4405 case BUILT_IN_SINCOS
:
4406 case BUILT_IN_SINCOSF
:
4407 case BUILT_IN_SINCOSL
:
4408 case BUILT_IN_FREXP
:
4409 case BUILT_IN_FREXPF
:
4410 case BUILT_IN_FREXPL
:
4411 case BUILT_IN_GAMMA_R
:
4412 case BUILT_IN_GAMMAF_R
:
4413 case BUILT_IN_GAMMAL_R
:
4414 case BUILT_IN_LGAMMA_R
:
4415 case BUILT_IN_LGAMMAF_R
:
4416 case BUILT_IN_LGAMMAL_R
:
4418 case BUILT_IN_MODFF
:
4419 case BUILT_IN_MODFL
:
4420 case BUILT_IN_REMQUO
:
4421 case BUILT_IN_REMQUOF
:
4422 case BUILT_IN_REMQUOL
:
4425 case BUILT_IN_STRDUP
:
4426 case BUILT_IN_STRNDUP
:
4427 case BUILT_IN_REALLOC
:
4428 if (gimple_call_lhs (t
))
4430 handle_lhs_call (t
, gimple_call_lhs (t
),
4431 gimple_call_return_flags (t
) | ERF_NOALIAS
,
4433 get_constraint_for_ptr_offset (gimple_call_lhs (t
),
4435 get_constraint_for_ptr_offset (gimple_call_arg (t
, 0),
4439 process_all_all_constraints (lhsc
, rhsc
);
4442 /* For realloc the resulting pointer can be equal to the
4443 argument as well. But only doing this wouldn't be
4444 correct because with ptr == 0 realloc behaves like malloc. */
4445 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_REALLOC
)
4447 get_constraint_for (gimple_call_lhs (t
), &lhsc
);
4448 get_constraint_for (gimple_call_arg (t
, 0), &rhsc
);
4449 process_all_all_constraints (lhsc
, rhsc
);
4454 /* String / character search functions return a pointer into the
4455 source string or NULL. */
4456 case BUILT_IN_INDEX
:
4457 case BUILT_IN_STRCHR
:
4458 case BUILT_IN_STRRCHR
:
4459 case BUILT_IN_MEMCHR
:
4460 case BUILT_IN_STRSTR
:
4461 case BUILT_IN_STRPBRK
:
4462 if (gimple_call_lhs (t
))
4464 tree src
= gimple_call_arg (t
, 0);
4465 get_constraint_for_ptr_offset (src
, NULL_TREE
, &rhsc
);
4466 constraint_expr nul
;
4467 nul
.var
= nothing_id
;
4469 nul
.type
= ADDRESSOF
;
4470 rhsc
.safe_push (nul
);
4471 get_constraint_for (gimple_call_lhs (t
), &lhsc
);
4472 process_all_all_constraints (lhsc
, rhsc
);
4475 /* Trampolines are special - they set up passing the static
4477 case BUILT_IN_INIT_TRAMPOLINE
:
4479 tree tramp
= gimple_call_arg (t
, 0);
4480 tree nfunc
= gimple_call_arg (t
, 1);
4481 tree frame
= gimple_call_arg (t
, 2);
4483 struct constraint_expr lhs
, *rhsp
;
4486 varinfo_t nfi
= NULL
;
4487 gcc_assert (TREE_CODE (nfunc
) == ADDR_EXPR
);
4488 nfi
= lookup_vi_for_tree (TREE_OPERAND (nfunc
, 0));
4491 lhs
= get_function_part_constraint (nfi
, fi_static_chain
);
4492 get_constraint_for (frame
, &rhsc
);
4493 FOR_EACH_VEC_ELT (rhsc
, i
, rhsp
)
4494 process_constraint (new_constraint (lhs
, *rhsp
));
4497 /* Make the frame point to the function for
4498 the trampoline adjustment call. */
4499 get_constraint_for (tramp
, &lhsc
);
4501 get_constraint_for (nfunc
, &rhsc
);
4502 process_all_all_constraints (lhsc
, rhsc
);
4507 /* Else fallthru to generic handling which will let
4508 the frame escape. */
4511 case BUILT_IN_ADJUST_TRAMPOLINE
:
4513 tree tramp
= gimple_call_arg (t
, 0);
4514 tree res
= gimple_call_lhs (t
);
4515 if (in_ipa_mode
&& res
)
4517 get_constraint_for (res
, &lhsc
);
4518 get_constraint_for (tramp
, &rhsc
);
4520 process_all_all_constraints (lhsc
, rhsc
);
4524 CASE_BUILT_IN_TM_STORE (1):
4525 CASE_BUILT_IN_TM_STORE (2):
4526 CASE_BUILT_IN_TM_STORE (4):
4527 CASE_BUILT_IN_TM_STORE (8):
4528 CASE_BUILT_IN_TM_STORE (FLOAT
):
4529 CASE_BUILT_IN_TM_STORE (DOUBLE
):
4530 CASE_BUILT_IN_TM_STORE (LDOUBLE
):
4531 CASE_BUILT_IN_TM_STORE (M64
):
4532 CASE_BUILT_IN_TM_STORE (M128
):
4533 CASE_BUILT_IN_TM_STORE (M256
):
4535 tree addr
= gimple_call_arg (t
, 0);
4536 tree src
= gimple_call_arg (t
, 1);
4538 get_constraint_for (addr
, &lhsc
);
4540 get_constraint_for (src
, &rhsc
);
4541 process_all_all_constraints (lhsc
, rhsc
);
4544 CASE_BUILT_IN_TM_LOAD (1):
4545 CASE_BUILT_IN_TM_LOAD (2):
4546 CASE_BUILT_IN_TM_LOAD (4):
4547 CASE_BUILT_IN_TM_LOAD (8):
4548 CASE_BUILT_IN_TM_LOAD (FLOAT
):
4549 CASE_BUILT_IN_TM_LOAD (DOUBLE
):
4550 CASE_BUILT_IN_TM_LOAD (LDOUBLE
):
4551 CASE_BUILT_IN_TM_LOAD (M64
):
4552 CASE_BUILT_IN_TM_LOAD (M128
):
4553 CASE_BUILT_IN_TM_LOAD (M256
):
4555 tree dest
= gimple_call_lhs (t
);
4556 tree addr
= gimple_call_arg (t
, 0);
4558 get_constraint_for (dest
, &lhsc
);
4559 get_constraint_for (addr
, &rhsc
);
4561 process_all_all_constraints (lhsc
, rhsc
);
4564 /* Variadic argument handling needs to be handled in IPA
4566 case BUILT_IN_VA_START
:
4568 tree valist
= gimple_call_arg (t
, 0);
4569 struct constraint_expr rhs
, *lhsp
;
4571 get_constraint_for_ptr_offset (valist
, NULL_TREE
, &lhsc
);
4573 /* The va_list gets access to pointers in variadic
4574 arguments. Which we know in the case of IPA analysis
4575 and otherwise are just all nonlocal variables. */
4578 fi
= lookup_vi_for_tree (fn
->decl
);
4579 rhs
= get_function_part_constraint (fi
, ~0);
4580 rhs
.type
= ADDRESSOF
;
4584 rhs
.var
= nonlocal_id
;
4585 rhs
.type
= ADDRESSOF
;
4588 FOR_EACH_VEC_ELT (lhsc
, i
, lhsp
)
4589 process_constraint (new_constraint (*lhsp
, rhs
));
4590 /* va_list is clobbered. */
4591 make_constraint_to (get_call_clobber_vi (t
)->id
, valist
);
4594 /* va_end doesn't have any effect that matters. */
4595 case BUILT_IN_VA_END
:
4597 /* Alternate return. Simply give up for now. */
4598 case BUILT_IN_RETURN
:
4602 || !(fi
= get_vi_for_tree (fn
->decl
)))
4603 make_constraint_from (get_varinfo (escaped_id
), anything_id
);
4604 else if (in_ipa_mode
4607 struct constraint_expr lhs
, rhs
;
4608 lhs
= get_function_part_constraint (fi
, fi_result
);
4609 rhs
.var
= anything_id
;
4612 process_constraint (new_constraint (lhs
, rhs
));
4616 case BUILT_IN_GOMP_PARALLEL
:
4617 case BUILT_IN_GOACC_PARALLEL
:
4621 unsigned int fnpos
, argpos
;
4622 switch (DECL_FUNCTION_CODE (fndecl
))
4624 case BUILT_IN_GOMP_PARALLEL
:
4625 /* __builtin_GOMP_parallel (fn, data, num_threads, flags). */
4629 case BUILT_IN_GOACC_PARALLEL
:
4630 /* __builtin_GOACC_parallel (device, fn, mapnum, hostaddrs,
4631 sizes, kinds, ...). */
4639 tree fnarg
= gimple_call_arg (t
, fnpos
);
4640 gcc_assert (TREE_CODE (fnarg
) == ADDR_EXPR
);
4641 tree fndecl
= TREE_OPERAND (fnarg
, 0);
4642 if (fndecl_maybe_in_other_partition (fndecl
))
4643 /* Fallthru to general call handling. */
4646 tree arg
= gimple_call_arg (t
, argpos
);
4648 varinfo_t fi
= get_vi_for_tree (fndecl
);
4649 find_func_aliases_for_call_arg (fi
, 0, arg
);
4652 /* Else fallthru to generic call handling. */
4655 /* printf-style functions may have hooks to set pointers to
4656 point to somewhere into the generated string. Leave them
4657 for a later exercise... */
4659 /* Fallthru to general call handling. */;
4665 /* Create constraints for the call T. */
4668 find_func_aliases_for_call (struct function
*fn
, gcall
*t
)
4670 tree fndecl
= gimple_call_fndecl (t
);
4673 if (fndecl
!= NULL_TREE
4674 && DECL_BUILT_IN (fndecl
)
4675 && find_func_aliases_for_builtin_call (fn
, t
))
4678 fi
= get_fi_for_callee (t
);
4680 || (fndecl
&& !fi
->is_fn_info
))
4682 auto_vec
<ce_s
, 16> rhsc
;
4683 int flags
= gimple_call_flags (t
);
4685 /* Const functions can return their arguments and addresses
4686 of global memory but not of escaped memory. */
4687 if (flags
& (ECF_CONST
|ECF_NOVOPS
))
4689 if (gimple_call_lhs (t
))
4690 handle_const_call (t
, &rhsc
);
4692 /* Pure functions can return addresses in and of memory
4693 reachable from their arguments, but they are not an escape
4694 point for reachable memory of their arguments. */
4695 else if (flags
& (ECF_PURE
|ECF_LOOPING_CONST_OR_PURE
))
4696 handle_pure_call (t
, &rhsc
);
4698 handle_rhs_call (t
, &rhsc
);
4699 if (gimple_call_lhs (t
))
4700 handle_lhs_call (t
, gimple_call_lhs (t
),
4701 gimple_call_return_flags (t
), rhsc
, fndecl
);
4705 auto_vec
<ce_s
, 2> rhsc
;
4709 /* Assign all the passed arguments to the appropriate incoming
4710 parameters of the function. */
4711 for (j
= 0; j
< gimple_call_num_args (t
); j
++)
4713 tree arg
= gimple_call_arg (t
, j
);
4714 find_func_aliases_for_call_arg (fi
, j
, arg
);
4717 /* If we are returning a value, assign it to the result. */
4718 lhsop
= gimple_call_lhs (t
);
4721 auto_vec
<ce_s
, 2> lhsc
;
4722 struct constraint_expr rhs
;
4723 struct constraint_expr
*lhsp
;
4724 bool aggr_p
= aggregate_value_p (lhsop
, gimple_call_fntype (t
));
4726 get_constraint_for (lhsop
, &lhsc
);
4727 rhs
= get_function_part_constraint (fi
, fi_result
);
4730 auto_vec
<ce_s
, 2> tem
;
4731 tem
.quick_push (rhs
);
4733 gcc_checking_assert (tem
.length () == 1);
4736 FOR_EACH_VEC_ELT (lhsc
, j
, lhsp
)
4737 process_constraint (new_constraint (*lhsp
, rhs
));
4739 /* If we pass the result decl by reference, honor that. */
4742 struct constraint_expr lhs
;
4743 struct constraint_expr
*rhsp
;
4745 get_constraint_for_address_of (lhsop
, &rhsc
);
4746 lhs
= get_function_part_constraint (fi
, fi_result
);
4747 FOR_EACH_VEC_ELT (rhsc
, j
, rhsp
)
4748 process_constraint (new_constraint (lhs
, *rhsp
));
4753 /* If we use a static chain, pass it along. */
4754 if (gimple_call_chain (t
))
4756 struct constraint_expr lhs
;
4757 struct constraint_expr
*rhsp
;
4759 get_constraint_for (gimple_call_chain (t
), &rhsc
);
4760 lhs
= get_function_part_constraint (fi
, fi_static_chain
);
4761 FOR_EACH_VEC_ELT (rhsc
, j
, rhsp
)
4762 process_constraint (new_constraint (lhs
, *rhsp
));
4767 /* Walk statement T setting up aliasing constraints according to the
4768 references found in T. This function is the main part of the
4769 constraint builder. AI points to auxiliary alias information used
4770 when building alias sets and computing alias grouping heuristics. */
4773 find_func_aliases (struct function
*fn
, gimple
*origt
)
4776 auto_vec
<ce_s
, 16> lhsc
;
4777 auto_vec
<ce_s
, 16> rhsc
;
4778 struct constraint_expr
*c
;
4781 /* Now build constraints expressions. */
4782 if (gimple_code (t
) == GIMPLE_PHI
)
4787 /* For a phi node, assign all the arguments to
4789 get_constraint_for (gimple_phi_result (t
), &lhsc
);
4790 for (i
= 0; i
< gimple_phi_num_args (t
); i
++)
4792 tree strippedrhs
= PHI_ARG_DEF (t
, i
);
4794 STRIP_NOPS (strippedrhs
);
4795 get_constraint_for_rhs (gimple_phi_arg_def (t
, i
), &rhsc
);
4797 FOR_EACH_VEC_ELT (lhsc
, j
, c
)
4799 struct constraint_expr
*c2
;
4800 while (rhsc
.length () > 0)
4803 process_constraint (new_constraint (*c
, *c2
));
4809 /* In IPA mode, we need to generate constraints to pass call
4810 arguments through their calls. There are two cases,
4811 either a GIMPLE_CALL returning a value, or just a plain
4812 GIMPLE_CALL when we are not.
4814 In non-ipa mode, we need to generate constraints for each
4815 pointer passed by address. */
4816 else if (is_gimple_call (t
))
4817 find_func_aliases_for_call (fn
, as_a
<gcall
*> (t
));
4819 /* Otherwise, just a regular assignment statement. Only care about
4820 operations with pointer result, others are dealt with as escape
4821 points if they have pointer operands. */
4822 else if (is_gimple_assign (t
))
4824 /* Otherwise, just a regular assignment statement. */
4825 tree lhsop
= gimple_assign_lhs (t
);
4826 tree rhsop
= (gimple_num_ops (t
) == 2) ? gimple_assign_rhs1 (t
) : NULL
;
4828 if (rhsop
&& TREE_CLOBBER_P (rhsop
))
4829 /* Ignore clobbers, they don't actually store anything into
4832 else if (rhsop
&& AGGREGATE_TYPE_P (TREE_TYPE (lhsop
)))
4833 do_structure_copy (lhsop
, rhsop
);
4836 enum tree_code code
= gimple_assign_rhs_code (t
);
4838 get_constraint_for (lhsop
, &lhsc
);
4840 if (code
== POINTER_PLUS_EXPR
)
4841 get_constraint_for_ptr_offset (gimple_assign_rhs1 (t
),
4842 gimple_assign_rhs2 (t
), &rhsc
);
4843 else if (code
== BIT_AND_EXPR
4844 && TREE_CODE (gimple_assign_rhs2 (t
)) == INTEGER_CST
)
4846 /* Aligning a pointer via a BIT_AND_EXPR is offsetting
4847 the pointer. Handle it by offsetting it by UNKNOWN. */
4848 get_constraint_for_ptr_offset (gimple_assign_rhs1 (t
),
4851 else if ((CONVERT_EXPR_CODE_P (code
)
4852 && !(POINTER_TYPE_P (gimple_expr_type (t
))
4853 && !POINTER_TYPE_P (TREE_TYPE (rhsop
))))
4854 || gimple_assign_single_p (t
))
4855 get_constraint_for_rhs (rhsop
, &rhsc
);
4856 else if (code
== COND_EXPR
)
4858 /* The result is a merge of both COND_EXPR arms. */
4859 auto_vec
<ce_s
, 2> tmp
;
4860 struct constraint_expr
*rhsp
;
4862 get_constraint_for_rhs (gimple_assign_rhs2 (t
), &rhsc
);
4863 get_constraint_for_rhs (gimple_assign_rhs3 (t
), &tmp
);
4864 FOR_EACH_VEC_ELT (tmp
, i
, rhsp
)
4865 rhsc
.safe_push (*rhsp
);
4867 else if (truth_value_p (code
))
4868 /* Truth value results are not pointer (parts). Or at least
4869 very unreasonable obfuscation of a part. */
4873 /* All other operations are merges. */
4874 auto_vec
<ce_s
, 4> tmp
;
4875 struct constraint_expr
*rhsp
;
4877 get_constraint_for_rhs (gimple_assign_rhs1 (t
), &rhsc
);
4878 for (i
= 2; i
< gimple_num_ops (t
); ++i
)
4880 get_constraint_for_rhs (gimple_op (t
, i
), &tmp
);
4881 FOR_EACH_VEC_ELT (tmp
, j
, rhsp
)
4882 rhsc
.safe_push (*rhsp
);
4886 process_all_all_constraints (lhsc
, rhsc
);
4888 /* If there is a store to a global variable the rhs escapes. */
4889 if ((lhsop
= get_base_address (lhsop
)) != NULL_TREE
4892 varinfo_t vi
= get_vi_for_tree (lhsop
);
4893 if ((! in_ipa_mode
&& vi
->is_global_var
)
4894 || vi
->is_ipa_escape_point
)
4895 make_escape_constraint (rhsop
);
4898 /* Handle escapes through return. */
4899 else if (gimple_code (t
) == GIMPLE_RETURN
4900 && gimple_return_retval (as_a
<greturn
*> (t
)) != NULL_TREE
)
4902 greturn
*return_stmt
= as_a
<greturn
*> (t
);
4905 || !(fi
= get_vi_for_tree (fn
->decl
)))
4906 make_escape_constraint (gimple_return_retval (return_stmt
));
4907 else if (in_ipa_mode
)
4909 struct constraint_expr lhs
;
4910 struct constraint_expr
*rhsp
;
4913 lhs
= get_function_part_constraint (fi
, fi_result
);
4914 get_constraint_for_rhs (gimple_return_retval (return_stmt
), &rhsc
);
4915 FOR_EACH_VEC_ELT (rhsc
, i
, rhsp
)
4916 process_constraint (new_constraint (lhs
, *rhsp
));
4919 /* Handle asms conservatively by adding escape constraints to everything. */
4920 else if (gasm
*asm_stmt
= dyn_cast
<gasm
*> (t
))
4922 unsigned i
, noutputs
;
4923 const char **oconstraints
;
4924 const char *constraint
;
4925 bool allows_mem
, allows_reg
, is_inout
;
4927 noutputs
= gimple_asm_noutputs (asm_stmt
);
4928 oconstraints
= XALLOCAVEC (const char *, noutputs
);
4930 for (i
= 0; i
< noutputs
; ++i
)
4932 tree link
= gimple_asm_output_op (asm_stmt
, i
);
4933 tree op
= TREE_VALUE (link
);
4935 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link
)));
4936 oconstraints
[i
] = constraint
;
4937 parse_output_constraint (&constraint
, i
, 0, 0, &allows_mem
,
4938 &allows_reg
, &is_inout
);
4940 /* A memory constraint makes the address of the operand escape. */
4941 if (!allows_reg
&& allows_mem
)
4942 make_escape_constraint (build_fold_addr_expr (op
));
4944 /* The asm may read global memory, so outputs may point to
4945 any global memory. */
4948 auto_vec
<ce_s
, 2> lhsc
;
4949 struct constraint_expr rhsc
, *lhsp
;
4951 get_constraint_for (op
, &lhsc
);
4952 rhsc
.var
= nonlocal_id
;
4955 FOR_EACH_VEC_ELT (lhsc
, j
, lhsp
)
4956 process_constraint (new_constraint (*lhsp
, rhsc
));
4959 for (i
= 0; i
< gimple_asm_ninputs (asm_stmt
); ++i
)
4961 tree link
= gimple_asm_input_op (asm_stmt
, i
);
4962 tree op
= TREE_VALUE (link
);
4964 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link
)));
4966 parse_input_constraint (&constraint
, 0, 0, noutputs
, 0, oconstraints
,
4967 &allows_mem
, &allows_reg
);
4969 /* A memory constraint makes the address of the operand escape. */
4970 if (!allows_reg
&& allows_mem
)
4971 make_escape_constraint (build_fold_addr_expr (op
));
4972 /* Strictly we'd only need the constraint to ESCAPED if
4973 the asm clobbers memory, otherwise using something
4974 along the lines of per-call clobbers/uses would be enough. */
4976 make_escape_constraint (op
);
4982 /* Create a constraint adding to the clobber set of FI the memory
4983 pointed to by PTR. */
4986 process_ipa_clobber (varinfo_t fi
, tree ptr
)
4988 vec
<ce_s
> ptrc
= vNULL
;
4989 struct constraint_expr
*c
, lhs
;
4991 get_constraint_for_rhs (ptr
, &ptrc
);
4992 lhs
= get_function_part_constraint (fi
, fi_clobbers
);
4993 FOR_EACH_VEC_ELT (ptrc
, i
, c
)
4994 process_constraint (new_constraint (lhs
, *c
));
4998 /* Walk statement T setting up clobber and use constraints according to the
4999 references found in T. This function is a main part of the
5000 IPA constraint builder. */
5003 find_func_clobbers (struct function
*fn
, gimple
*origt
)
5006 auto_vec
<ce_s
, 16> lhsc
;
5007 auto_vec
<ce_s
, 16> rhsc
;
5010 /* Add constraints for clobbered/used in IPA mode.
5011 We are not interested in what automatic variables are clobbered
5012 or used as we only use the information in the caller to which
5013 they do not escape. */
5014 gcc_assert (in_ipa_mode
);
5016 /* If the stmt refers to memory in any way it better had a VUSE. */
5017 if (gimple_vuse (t
) == NULL_TREE
)
5020 /* We'd better have function information for the current function. */
5021 fi
= lookup_vi_for_tree (fn
->decl
);
5022 gcc_assert (fi
!= NULL
);
5024 /* Account for stores in assignments and calls. */
5025 if (gimple_vdef (t
) != NULL_TREE
5026 && gimple_has_lhs (t
))
5028 tree lhs
= gimple_get_lhs (t
);
5030 while (handled_component_p (tem
))
5031 tem
= TREE_OPERAND (tem
, 0);
5033 && !auto_var_in_fn_p (tem
, fn
->decl
))
5034 || INDIRECT_REF_P (tem
)
5035 || (TREE_CODE (tem
) == MEM_REF
5036 && !(TREE_CODE (TREE_OPERAND (tem
, 0)) == ADDR_EXPR
5038 (TREE_OPERAND (TREE_OPERAND (tem
, 0), 0), fn
->decl
))))
5040 struct constraint_expr lhsc
, *rhsp
;
5042 lhsc
= get_function_part_constraint (fi
, fi_clobbers
);
5043 get_constraint_for_address_of (lhs
, &rhsc
);
5044 FOR_EACH_VEC_ELT (rhsc
, i
, rhsp
)
5045 process_constraint (new_constraint (lhsc
, *rhsp
));
5050 /* Account for uses in assigments and returns. */
5051 if (gimple_assign_single_p (t
)
5052 || (gimple_code (t
) == GIMPLE_RETURN
5053 && gimple_return_retval (as_a
<greturn
*> (t
)) != NULL_TREE
))
5055 tree rhs
= (gimple_assign_single_p (t
)
5056 ? gimple_assign_rhs1 (t
)
5057 : gimple_return_retval (as_a
<greturn
*> (t
)));
5059 while (handled_component_p (tem
))
5060 tem
= TREE_OPERAND (tem
, 0);
5062 && !auto_var_in_fn_p (tem
, fn
->decl
))
5063 || INDIRECT_REF_P (tem
)
5064 || (TREE_CODE (tem
) == MEM_REF
5065 && !(TREE_CODE (TREE_OPERAND (tem
, 0)) == ADDR_EXPR
5067 (TREE_OPERAND (TREE_OPERAND (tem
, 0), 0), fn
->decl
))))
5069 struct constraint_expr lhs
, *rhsp
;
5071 lhs
= get_function_part_constraint (fi
, fi_uses
);
5072 get_constraint_for_address_of (rhs
, &rhsc
);
5073 FOR_EACH_VEC_ELT (rhsc
, i
, rhsp
)
5074 process_constraint (new_constraint (lhs
, *rhsp
));
5079 if (gcall
*call_stmt
= dyn_cast
<gcall
*> (t
))
5081 varinfo_t cfi
= NULL
;
5082 tree decl
= gimple_call_fndecl (t
);
5083 struct constraint_expr lhs
, rhs
;
5086 /* For builtins we do not have separate function info. For those
5087 we do not generate escapes for we have to generate clobbers/uses. */
5088 if (gimple_call_builtin_p (t
, BUILT_IN_NORMAL
))
5089 switch (DECL_FUNCTION_CODE (decl
))
5091 /* The following functions use and clobber memory pointed to
5092 by their arguments. */
5093 case BUILT_IN_STRCPY
:
5094 case BUILT_IN_STRNCPY
:
5095 case BUILT_IN_BCOPY
:
5096 case BUILT_IN_MEMCPY
:
5097 case BUILT_IN_MEMMOVE
:
5098 case BUILT_IN_MEMPCPY
:
5099 case BUILT_IN_STPCPY
:
5100 case BUILT_IN_STPNCPY
:
5101 case BUILT_IN_STRCAT
:
5102 case BUILT_IN_STRNCAT
:
5103 case BUILT_IN_STRCPY_CHK
:
5104 case BUILT_IN_STRNCPY_CHK
:
5105 case BUILT_IN_MEMCPY_CHK
:
5106 case BUILT_IN_MEMMOVE_CHK
:
5107 case BUILT_IN_MEMPCPY_CHK
:
5108 case BUILT_IN_STPCPY_CHK
:
5109 case BUILT_IN_STPNCPY_CHK
:
5110 case BUILT_IN_STRCAT_CHK
:
5111 case BUILT_IN_STRNCAT_CHK
:
5113 tree dest
= gimple_call_arg (t
, (DECL_FUNCTION_CODE (decl
)
5114 == BUILT_IN_BCOPY
? 1 : 0));
5115 tree src
= gimple_call_arg (t
, (DECL_FUNCTION_CODE (decl
)
5116 == BUILT_IN_BCOPY
? 0 : 1));
5118 struct constraint_expr
*rhsp
, *lhsp
;
5119 get_constraint_for_ptr_offset (dest
, NULL_TREE
, &lhsc
);
5120 lhs
= get_function_part_constraint (fi
, fi_clobbers
);
5121 FOR_EACH_VEC_ELT (lhsc
, i
, lhsp
)
5122 process_constraint (new_constraint (lhs
, *lhsp
));
5123 get_constraint_for_ptr_offset (src
, NULL_TREE
, &rhsc
);
5124 lhs
= get_function_part_constraint (fi
, fi_uses
);
5125 FOR_EACH_VEC_ELT (rhsc
, i
, rhsp
)
5126 process_constraint (new_constraint (lhs
, *rhsp
));
5129 /* The following function clobbers memory pointed to by
5131 case BUILT_IN_MEMSET
:
5132 case BUILT_IN_MEMSET_CHK
:
5133 case BUILT_IN_POSIX_MEMALIGN
:
5135 tree dest
= gimple_call_arg (t
, 0);
5138 get_constraint_for_ptr_offset (dest
, NULL_TREE
, &lhsc
);
5139 lhs
= get_function_part_constraint (fi
, fi_clobbers
);
5140 FOR_EACH_VEC_ELT (lhsc
, i
, lhsp
)
5141 process_constraint (new_constraint (lhs
, *lhsp
));
5144 /* The following functions clobber their second and third
5146 case BUILT_IN_SINCOS
:
5147 case BUILT_IN_SINCOSF
:
5148 case BUILT_IN_SINCOSL
:
5150 process_ipa_clobber (fi
, gimple_call_arg (t
, 1));
5151 process_ipa_clobber (fi
, gimple_call_arg (t
, 2));
5154 /* The following functions clobber their second argument. */
5155 case BUILT_IN_FREXP
:
5156 case BUILT_IN_FREXPF
:
5157 case BUILT_IN_FREXPL
:
5158 case BUILT_IN_LGAMMA_R
:
5159 case BUILT_IN_LGAMMAF_R
:
5160 case BUILT_IN_LGAMMAL_R
:
5161 case BUILT_IN_GAMMA_R
:
5162 case BUILT_IN_GAMMAF_R
:
5163 case BUILT_IN_GAMMAL_R
:
5165 case BUILT_IN_MODFF
:
5166 case BUILT_IN_MODFL
:
5168 process_ipa_clobber (fi
, gimple_call_arg (t
, 1));
5171 /* The following functions clobber their third argument. */
5172 case BUILT_IN_REMQUO
:
5173 case BUILT_IN_REMQUOF
:
5174 case BUILT_IN_REMQUOL
:
5176 process_ipa_clobber (fi
, gimple_call_arg (t
, 2));
5179 /* The following functions neither read nor clobber memory. */
5180 case BUILT_IN_ASSUME_ALIGNED
:
5183 /* Trampolines are of no interest to us. */
5184 case BUILT_IN_INIT_TRAMPOLINE
:
5185 case BUILT_IN_ADJUST_TRAMPOLINE
:
5187 case BUILT_IN_VA_START
:
5188 case BUILT_IN_VA_END
:
5190 case BUILT_IN_GOMP_PARALLEL
:
5191 case BUILT_IN_GOACC_PARALLEL
:
5193 unsigned int fnpos
, argpos
;
5194 unsigned int implicit_use_args
[2];
5195 unsigned int num_implicit_use_args
= 0;
5196 switch (DECL_FUNCTION_CODE (decl
))
5198 case BUILT_IN_GOMP_PARALLEL
:
5199 /* __builtin_GOMP_parallel (fn, data, num_threads, flags). */
5203 case BUILT_IN_GOACC_PARALLEL
:
5204 /* __builtin_GOACC_parallel (device, fn, mapnum, hostaddrs,
5205 sizes, kinds, ...). */
5208 implicit_use_args
[num_implicit_use_args
++] = 4;
5209 implicit_use_args
[num_implicit_use_args
++] = 5;
5215 tree fnarg
= gimple_call_arg (t
, fnpos
);
5216 gcc_assert (TREE_CODE (fnarg
) == ADDR_EXPR
);
5217 tree fndecl
= TREE_OPERAND (fnarg
, 0);
5218 if (fndecl_maybe_in_other_partition (fndecl
))
5219 /* Fallthru to general call handling. */
5222 varinfo_t cfi
= get_vi_for_tree (fndecl
);
5224 tree arg
= gimple_call_arg (t
, argpos
);
5226 /* Parameter passed by value is used. */
5227 lhs
= get_function_part_constraint (fi
, fi_uses
);
5228 struct constraint_expr
*rhsp
;
5229 get_constraint_for (arg
, &rhsc
);
5230 FOR_EACH_VEC_ELT (rhsc
, j
, rhsp
)
5231 process_constraint (new_constraint (lhs
, *rhsp
));
5234 /* Handle parameters used by the call, but not used in cfi, as
5235 implicitly used by cfi. */
5236 lhs
= get_function_part_constraint (cfi
, fi_uses
);
5237 for (unsigned i
= 0; i
< num_implicit_use_args
; ++i
)
5239 tree arg
= gimple_call_arg (t
, implicit_use_args
[i
]);
5240 get_constraint_for (arg
, &rhsc
);
5241 FOR_EACH_VEC_ELT (rhsc
, j
, rhsp
)
5242 process_constraint (new_constraint (lhs
, *rhsp
));
5246 /* The caller clobbers what the callee does. */
5247 lhs
= get_function_part_constraint (fi
, fi_clobbers
);
5248 rhs
= get_function_part_constraint (cfi
, fi_clobbers
);
5249 process_constraint (new_constraint (lhs
, rhs
));
5251 /* The caller uses what the callee does. */
5252 lhs
= get_function_part_constraint (fi
, fi_uses
);
5253 rhs
= get_function_part_constraint (cfi
, fi_uses
);
5254 process_constraint (new_constraint (lhs
, rhs
));
5258 /* printf-style functions may have hooks to set pointers to
5259 point to somewhere into the generated string. Leave them
5260 for a later exercise... */
5262 /* Fallthru to general call handling. */;
5265 /* Parameters passed by value are used. */
5266 lhs
= get_function_part_constraint (fi
, fi_uses
);
5267 for (i
= 0; i
< gimple_call_num_args (t
); i
++)
5269 struct constraint_expr
*rhsp
;
5270 tree arg
= gimple_call_arg (t
, i
);
5272 if (TREE_CODE (arg
) == SSA_NAME
5273 || is_gimple_min_invariant (arg
))
5276 get_constraint_for_address_of (arg
, &rhsc
);
5277 FOR_EACH_VEC_ELT (rhsc
, j
, rhsp
)
5278 process_constraint (new_constraint (lhs
, *rhsp
));
5282 /* Build constraints for propagating clobbers/uses along the
5284 cfi
= get_fi_for_callee (call_stmt
);
5285 if (cfi
->id
== anything_id
)
5287 if (gimple_vdef (t
))
5288 make_constraint_from (first_vi_for_offset (fi
, fi_clobbers
),
5290 make_constraint_from (first_vi_for_offset (fi
, fi_uses
),
5295 /* For callees without function info (that's external functions),
5296 ESCAPED is clobbered and used. */
5297 if (gimple_call_fndecl (t
)
5298 && !cfi
->is_fn_info
)
5302 if (gimple_vdef (t
))
5303 make_copy_constraint (first_vi_for_offset (fi
, fi_clobbers
),
5305 make_copy_constraint (first_vi_for_offset (fi
, fi_uses
), escaped_id
);
5307 /* Also honor the call statement use/clobber info. */
5308 if ((vi
= lookup_call_clobber_vi (call_stmt
)) != NULL
)
5309 make_copy_constraint (first_vi_for_offset (fi
, fi_clobbers
),
5311 if ((vi
= lookup_call_use_vi (call_stmt
)) != NULL
)
5312 make_copy_constraint (first_vi_for_offset (fi
, fi_uses
),
5317 /* Otherwise the caller clobbers and uses what the callee does.
5318 ??? This should use a new complex constraint that filters
5319 local variables of the callee. */
5320 if (gimple_vdef (t
))
5322 lhs
= get_function_part_constraint (fi
, fi_clobbers
);
5323 rhs
= get_function_part_constraint (cfi
, fi_clobbers
);
5324 process_constraint (new_constraint (lhs
, rhs
));
5326 lhs
= get_function_part_constraint (fi
, fi_uses
);
5327 rhs
= get_function_part_constraint (cfi
, fi_uses
);
5328 process_constraint (new_constraint (lhs
, rhs
));
5330 else if (gimple_code (t
) == GIMPLE_ASM
)
5332 /* ??? Ick. We can do better. */
5333 if (gimple_vdef (t
))
5334 make_constraint_from (first_vi_for_offset (fi
, fi_clobbers
),
5336 make_constraint_from (first_vi_for_offset (fi
, fi_uses
),
5342 /* Find the first varinfo in the same variable as START that overlaps with
5343 OFFSET. Return NULL if we can't find one. */
5346 first_vi_for_offset (varinfo_t start
, unsigned HOST_WIDE_INT offset
)
5348 /* If the offset is outside of the variable, bail out. */
5349 if (offset
>= start
->fullsize
)
5352 /* If we cannot reach offset from start, lookup the first field
5353 and start from there. */
5354 if (start
->offset
> offset
)
5355 start
= get_varinfo (start
->head
);
5359 /* We may not find a variable in the field list with the actual
5360 offset when we have glommed a structure to a variable.
5361 In that case, however, offset should still be within the size
5363 if (offset
>= start
->offset
5364 && (offset
- start
->offset
) < start
->size
)
5367 start
= vi_next (start
);
5373 /* Find the first varinfo in the same variable as START that overlaps with
5374 OFFSET. If there is no such varinfo the varinfo directly preceding
5375 OFFSET is returned. */
5378 first_or_preceding_vi_for_offset (varinfo_t start
,
5379 unsigned HOST_WIDE_INT offset
)
5381 /* If we cannot reach offset from start, lookup the first field
5382 and start from there. */
5383 if (start
->offset
> offset
)
5384 start
= get_varinfo (start
->head
);
5386 /* We may not find a variable in the field list with the actual
5387 offset when we have glommed a structure to a variable.
5388 In that case, however, offset should still be within the size
5390 If we got beyond the offset we look for return the field
5391 directly preceding offset which may be the last field. */
5393 && offset
>= start
->offset
5394 && !((offset
- start
->offset
) < start
->size
))
5395 start
= vi_next (start
);
5401 /* This structure is used during pushing fields onto the fieldstack
5402 to track the offset of the field, since bitpos_of_field gives it
5403 relative to its immediate containing type, and we want it relative
5404 to the ultimate containing object. */
5408 /* Offset from the base of the base containing object to this field. */
5409 HOST_WIDE_INT offset
;
5411 /* Size, in bits, of the field. */
5412 unsigned HOST_WIDE_INT size
;
5414 unsigned has_unknown_size
: 1;
5416 unsigned must_have_pointers
: 1;
5418 unsigned may_have_pointers
: 1;
5420 unsigned only_restrict_pointers
: 1;
5422 tree restrict_pointed_type
;
5424 typedef struct fieldoff fieldoff_s
;
5427 /* qsort comparison function for two fieldoff's PA and PB */
5430 fieldoff_compare (const void *pa
, const void *pb
)
5432 const fieldoff_s
*foa
= (const fieldoff_s
*)pa
;
5433 const fieldoff_s
*fob
= (const fieldoff_s
*)pb
;
5434 unsigned HOST_WIDE_INT foasize
, fobsize
;
5436 if (foa
->offset
< fob
->offset
)
5438 else if (foa
->offset
> fob
->offset
)
5441 foasize
= foa
->size
;
5442 fobsize
= fob
->size
;
5443 if (foasize
< fobsize
)
5445 else if (foasize
> fobsize
)
5450 /* Sort a fieldstack according to the field offset and sizes. */
5452 sort_fieldstack (vec
<fieldoff_s
> fieldstack
)
5454 fieldstack
.qsort (fieldoff_compare
);
5457 /* Return true if T is a type that can have subvars. */
5460 type_can_have_subvars (const_tree t
)
5462 /* Aggregates without overlapping fields can have subvars. */
5463 return TREE_CODE (t
) == RECORD_TYPE
;
5466 /* Return true if V is a tree that we can have subvars for.
5467 Normally, this is any aggregate type. Also complex
5468 types which are not gimple registers can have subvars. */
5471 var_can_have_subvars (const_tree v
)
5473 /* Volatile variables should never have subvars. */
5474 if (TREE_THIS_VOLATILE (v
))
5477 /* Non decls or memory tags can never have subvars. */
5481 return type_can_have_subvars (TREE_TYPE (v
));
5484 /* Return true if T is a type that does contain pointers. */
5487 type_must_have_pointers (tree type
)
5489 if (POINTER_TYPE_P (type
))
5492 if (TREE_CODE (type
) == ARRAY_TYPE
)
5493 return type_must_have_pointers (TREE_TYPE (type
));
5495 /* A function or method can have pointers as arguments, so track
5496 those separately. */
5497 if (TREE_CODE (type
) == FUNCTION_TYPE
5498 || TREE_CODE (type
) == METHOD_TYPE
)
5505 field_must_have_pointers (tree t
)
5507 return type_must_have_pointers (TREE_TYPE (t
));
5510 /* Given a TYPE, and a vector of field offsets FIELDSTACK, push all
5511 the fields of TYPE onto fieldstack, recording their offsets along
5514 OFFSET is used to keep track of the offset in this entire
5515 structure, rather than just the immediately containing structure.
5516 Returns false if the caller is supposed to handle the field we
5520 push_fields_onto_fieldstack (tree type
, vec
<fieldoff_s
> *fieldstack
,
5521 HOST_WIDE_INT offset
)
5524 bool empty_p
= true;
5526 if (TREE_CODE (type
) != RECORD_TYPE
)
5529 /* If the vector of fields is growing too big, bail out early.
5530 Callers check for vec::length <= MAX_FIELDS_FOR_FIELD_SENSITIVE, make
5532 if (fieldstack
->length () > MAX_FIELDS_FOR_FIELD_SENSITIVE
)
5535 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
5536 if (TREE_CODE (field
) == FIELD_DECL
)
5539 HOST_WIDE_INT foff
= bitpos_of_field (field
);
5540 tree field_type
= TREE_TYPE (field
);
5542 if (!var_can_have_subvars (field
)
5543 || TREE_CODE (field_type
) == QUAL_UNION_TYPE
5544 || TREE_CODE (field_type
) == UNION_TYPE
)
5546 else if (!push_fields_onto_fieldstack
5547 (field_type
, fieldstack
, offset
+ foff
)
5548 && (DECL_SIZE (field
)
5549 && !integer_zerop (DECL_SIZE (field
))))
5550 /* Empty structures may have actual size, like in C++. So
5551 see if we didn't push any subfields and the size is
5552 nonzero, push the field onto the stack. */
5557 fieldoff_s
*pair
= NULL
;
5558 bool has_unknown_size
= false;
5559 bool must_have_pointers_p
;
5561 if (!fieldstack
->is_empty ())
5562 pair
= &fieldstack
->last ();
5564 /* If there isn't anything at offset zero, create sth. */
5566 && offset
+ foff
!= 0)
5569 = {0, offset
+ foff
, false, false, false, false, NULL_TREE
};
5570 pair
= fieldstack
->safe_push (e
);
5573 if (!DECL_SIZE (field
)
5574 || !tree_fits_uhwi_p (DECL_SIZE (field
)))
5575 has_unknown_size
= true;
5577 /* If adjacent fields do not contain pointers merge them. */
5578 must_have_pointers_p
= field_must_have_pointers (field
);
5580 && !has_unknown_size
5581 && !must_have_pointers_p
5582 && !pair
->must_have_pointers
5583 && !pair
->has_unknown_size
5584 && pair
->offset
+ (HOST_WIDE_INT
)pair
->size
== offset
+ foff
)
5586 pair
->size
+= tree_to_uhwi (DECL_SIZE (field
));
5591 e
.offset
= offset
+ foff
;
5592 e
.has_unknown_size
= has_unknown_size
;
5593 if (!has_unknown_size
)
5594 e
.size
= tree_to_uhwi (DECL_SIZE (field
));
5597 e
.must_have_pointers
= must_have_pointers_p
;
5598 e
.may_have_pointers
= true;
5599 e
.only_restrict_pointers
5600 = (!has_unknown_size
5601 && POINTER_TYPE_P (field_type
)
5602 && TYPE_RESTRICT (field_type
));
5603 if (e
.only_restrict_pointers
)
5604 e
.restrict_pointed_type
= TREE_TYPE (field_type
);
5605 fieldstack
->safe_push (e
);
5615 /* Count the number of arguments DECL has, and set IS_VARARGS to true
5616 if it is a varargs function. */
5619 count_num_arguments (tree decl
, bool *is_varargs
)
5621 unsigned int num
= 0;
5624 /* Capture named arguments for K&R functions. They do not
5625 have a prototype and thus no TYPE_ARG_TYPES. */
5626 for (t
= DECL_ARGUMENTS (decl
); t
; t
= DECL_CHAIN (t
))
5629 /* Check if the function has variadic arguments. */
5630 for (t
= TYPE_ARG_TYPES (TREE_TYPE (decl
)); t
; t
= TREE_CHAIN (t
))
5631 if (TREE_VALUE (t
) == void_type_node
)
5639 /* Creation function node for DECL, using NAME, and return the index
5640 of the variable we've created for the function. If NONLOCAL_p, create
5641 initial constraints. */
5644 create_function_info_for (tree decl
, const char *name
, bool add_id
,
5647 struct function
*fn
= DECL_STRUCT_FUNCTION (decl
);
5648 varinfo_t vi
, prev_vi
;
5651 bool is_varargs
= false;
5652 unsigned int num_args
= count_num_arguments (decl
, &is_varargs
);
5654 /* Create the variable info. */
5656 vi
= new_var_info (decl
, name
, add_id
);
5659 vi
->fullsize
= fi_parm_base
+ num_args
;
5661 vi
->may_have_pointers
= false;
5664 insert_vi_for_tree (vi
->decl
, vi
);
5668 /* Create a variable for things the function clobbers and one for
5669 things the function uses. */
5671 varinfo_t clobbervi
, usevi
;
5672 const char *newname
;
5675 tempname
= xasprintf ("%s.clobber", name
);
5676 newname
= ggc_strdup (tempname
);
5679 clobbervi
= new_var_info (NULL
, newname
, false);
5680 clobbervi
->offset
= fi_clobbers
;
5681 clobbervi
->size
= 1;
5682 clobbervi
->fullsize
= vi
->fullsize
;
5683 clobbervi
->is_full_var
= true;
5684 clobbervi
->is_global_var
= false;
5686 gcc_assert (prev_vi
->offset
< clobbervi
->offset
);
5687 prev_vi
->next
= clobbervi
->id
;
5688 prev_vi
= clobbervi
;
5690 tempname
= xasprintf ("%s.use", name
);
5691 newname
= ggc_strdup (tempname
);
5694 usevi
= new_var_info (NULL
, newname
, false);
5695 usevi
->offset
= fi_uses
;
5697 usevi
->fullsize
= vi
->fullsize
;
5698 usevi
->is_full_var
= true;
5699 usevi
->is_global_var
= false;
5701 gcc_assert (prev_vi
->offset
< usevi
->offset
);
5702 prev_vi
->next
= usevi
->id
;
5706 /* And one for the static chain. */
5707 if (fn
->static_chain_decl
!= NULL_TREE
)
5710 const char *newname
;
5713 tempname
= xasprintf ("%s.chain", name
);
5714 newname
= ggc_strdup (tempname
);
5717 chainvi
= new_var_info (fn
->static_chain_decl
, newname
, false);
5718 chainvi
->offset
= fi_static_chain
;
5720 chainvi
->fullsize
= vi
->fullsize
;
5721 chainvi
->is_full_var
= true;
5722 chainvi
->is_global_var
= false;
5724 insert_vi_for_tree (fn
->static_chain_decl
, chainvi
);
5727 && chainvi
->may_have_pointers
)
5728 make_constraint_from (chainvi
, nonlocal_id
);
5730 gcc_assert (prev_vi
->offset
< chainvi
->offset
);
5731 prev_vi
->next
= chainvi
->id
;
5735 /* Create a variable for the return var. */
5736 if (DECL_RESULT (decl
) != NULL
5737 || !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl
))))
5740 const char *newname
;
5742 tree resultdecl
= decl
;
5744 if (DECL_RESULT (decl
))
5745 resultdecl
= DECL_RESULT (decl
);
5747 tempname
= xasprintf ("%s.result", name
);
5748 newname
= ggc_strdup (tempname
);
5751 resultvi
= new_var_info (resultdecl
, newname
, false);
5752 resultvi
->offset
= fi_result
;
5754 resultvi
->fullsize
= vi
->fullsize
;
5755 resultvi
->is_full_var
= true;
5756 if (DECL_RESULT (decl
))
5757 resultvi
->may_have_pointers
= true;
5759 if (DECL_RESULT (decl
))
5760 insert_vi_for_tree (DECL_RESULT (decl
), resultvi
);
5763 && DECL_RESULT (decl
)
5764 && DECL_BY_REFERENCE (DECL_RESULT (decl
)))
5765 make_constraint_from (resultvi
, nonlocal_id
);
5767 gcc_assert (prev_vi
->offset
< resultvi
->offset
);
5768 prev_vi
->next
= resultvi
->id
;
5772 /* We also need to make function return values escape. Nothing
5773 escapes by returning from main though. */
5775 && !MAIN_NAME_P (DECL_NAME (decl
)))
5778 fi
= lookup_vi_for_tree (decl
);
5779 rvi
= first_vi_for_offset (fi
, fi_result
);
5780 if (rvi
&& rvi
->offset
== fi_result
)
5781 make_copy_constraint (get_varinfo (escaped_id
), rvi
->id
);
5784 /* Set up variables for each argument. */
5785 arg
= DECL_ARGUMENTS (decl
);
5786 for (i
= 0; i
< num_args
; i
++)
5789 const char *newname
;
5791 tree argdecl
= decl
;
5796 tempname
= xasprintf ("%s.arg%d", name
, i
);
5797 newname
= ggc_strdup (tempname
);
5800 argvi
= new_var_info (argdecl
, newname
, false);
5801 argvi
->offset
= fi_parm_base
+ i
;
5803 argvi
->is_full_var
= true;
5804 argvi
->fullsize
= vi
->fullsize
;
5806 argvi
->may_have_pointers
= true;
5809 insert_vi_for_tree (arg
, argvi
);
5812 && argvi
->may_have_pointers
)
5813 make_constraint_from (argvi
, nonlocal_id
);
5815 gcc_assert (prev_vi
->offset
< argvi
->offset
);
5816 prev_vi
->next
= argvi
->id
;
5819 arg
= DECL_CHAIN (arg
);
5822 /* Add one representative for all further args. */
5826 const char *newname
;
5830 tempname
= xasprintf ("%s.varargs", name
);
5831 newname
= ggc_strdup (tempname
);
5834 /* We need sth that can be pointed to for va_start. */
5835 decl
= build_fake_var_decl (ptr_type_node
);
5837 argvi
= new_var_info (decl
, newname
, false);
5838 argvi
->offset
= fi_parm_base
+ num_args
;
5840 argvi
->is_full_var
= true;
5841 argvi
->is_heap_var
= true;
5842 argvi
->fullsize
= vi
->fullsize
;
5845 && argvi
->may_have_pointers
)
5846 make_constraint_from (argvi
, nonlocal_id
);
5848 gcc_assert (prev_vi
->offset
< argvi
->offset
);
5849 prev_vi
->next
= argvi
->id
;
5857 /* Return true if FIELDSTACK contains fields that overlap.
5858 FIELDSTACK is assumed to be sorted by offset. */
5861 check_for_overlaps (vec
<fieldoff_s
> fieldstack
)
5863 fieldoff_s
*fo
= NULL
;
5865 HOST_WIDE_INT lastoffset
= -1;
5867 FOR_EACH_VEC_ELT (fieldstack
, i
, fo
)
5869 if (fo
->offset
== lastoffset
)
5871 lastoffset
= fo
->offset
;
5876 /* Create a varinfo structure for NAME and DECL, and add it to VARMAP.
5877 This will also create any varinfo structures necessary for fields
5878 of DECL. DECL is a function parameter if HANDLE_PARAM is set.
5879 HANDLED_STRUCT_TYPE is used to register struct types reached by following
5880 restrict pointers. This is needed to prevent infinite recursion. */
5883 create_variable_info_for_1 (tree decl
, const char *name
, bool add_id
,
5884 bool handle_param
, bitmap handled_struct_type
)
5886 varinfo_t vi
, newvi
;
5887 tree decl_type
= TREE_TYPE (decl
);
5888 tree declsize
= DECL_P (decl
) ? DECL_SIZE (decl
) : TYPE_SIZE (decl_type
);
5889 auto_vec
<fieldoff_s
> fieldstack
;
5894 || !tree_fits_uhwi_p (declsize
))
5896 vi
= new_var_info (decl
, name
, add_id
);
5900 vi
->is_unknown_size_var
= true;
5901 vi
->is_full_var
= true;
5902 vi
->may_have_pointers
= true;
5906 /* Collect field information. */
5907 if (use_field_sensitive
5908 && var_can_have_subvars (decl
)
5909 /* ??? Force us to not use subfields for globals in IPA mode.
5910 Else we'd have to parse arbitrary initializers. */
5912 && is_global_var (decl
)))
5914 fieldoff_s
*fo
= NULL
;
5915 bool notokay
= false;
5918 push_fields_onto_fieldstack (decl_type
, &fieldstack
, 0);
5920 for (i
= 0; !notokay
&& fieldstack
.iterate (i
, &fo
); i
++)
5921 if (fo
->has_unknown_size
5928 /* We can't sort them if we have a field with a variable sized type,
5929 which will make notokay = true. In that case, we are going to return
5930 without creating varinfos for the fields anyway, so sorting them is a
5934 sort_fieldstack (fieldstack
);
5935 /* Due to some C++ FE issues, like PR 22488, we might end up
5936 what appear to be overlapping fields even though they,
5937 in reality, do not overlap. Until the C++ FE is fixed,
5938 we will simply disable field-sensitivity for these cases. */
5939 notokay
= check_for_overlaps (fieldstack
);
5943 fieldstack
.release ();
5946 /* If we didn't end up collecting sub-variables create a full
5947 variable for the decl. */
5948 if (fieldstack
.length () == 0
5949 || fieldstack
.length () > MAX_FIELDS_FOR_FIELD_SENSITIVE
)
5951 vi
= new_var_info (decl
, name
, add_id
);
5953 vi
->may_have_pointers
= true;
5954 vi
->fullsize
= tree_to_uhwi (declsize
);
5955 vi
->size
= vi
->fullsize
;
5956 vi
->is_full_var
= true;
5957 if (POINTER_TYPE_P (decl_type
)
5958 && TYPE_RESTRICT (decl_type
))
5959 vi
->only_restrict_pointers
= 1;
5960 if (vi
->only_restrict_pointers
5961 && !type_contains_placeholder_p (TREE_TYPE (decl_type
))
5963 && !bitmap_bit_p (handled_struct_type
,
5964 TYPE_UID (TREE_TYPE (decl_type
))))
5967 tree heapvar
= build_fake_var_decl (TREE_TYPE (decl_type
));
5968 DECL_EXTERNAL (heapvar
) = 1;
5969 if (var_can_have_subvars (heapvar
))
5970 bitmap_set_bit (handled_struct_type
,
5971 TYPE_UID (TREE_TYPE (decl_type
)));
5972 rvi
= create_variable_info_for_1 (heapvar
, "PARM_NOALIAS", true,
5973 true, handled_struct_type
);
5974 if (var_can_have_subvars (heapvar
))
5975 bitmap_clear_bit (handled_struct_type
,
5976 TYPE_UID (TREE_TYPE (decl_type
)));
5977 rvi
->is_restrict_var
= 1;
5978 insert_vi_for_tree (heapvar
, rvi
);
5979 make_constraint_from (vi
, rvi
->id
);
5980 make_param_constraints (rvi
);
5982 fieldstack
.release ();
5986 vi
= new_var_info (decl
, name
, add_id
);
5987 vi
->fullsize
= tree_to_uhwi (declsize
);
5988 if (fieldstack
.length () == 1)
5989 vi
->is_full_var
= true;
5990 for (i
= 0, newvi
= vi
;
5991 fieldstack
.iterate (i
, &fo
);
5992 ++i
, newvi
= vi_next (newvi
))
5994 const char *newname
= NULL
;
5999 if (fieldstack
.length () != 1)
6002 = xasprintf ("%s." HOST_WIDE_INT_PRINT_DEC
6003 "+" HOST_WIDE_INT_PRINT_DEC
, name
,
6004 fo
->offset
, fo
->size
);
6005 newname
= ggc_strdup (tempname
);
6013 newvi
->name
= newname
;
6014 newvi
->offset
= fo
->offset
;
6015 newvi
->size
= fo
->size
;
6016 newvi
->fullsize
= vi
->fullsize
;
6017 newvi
->may_have_pointers
= fo
->may_have_pointers
;
6018 newvi
->only_restrict_pointers
= fo
->only_restrict_pointers
;
6020 && newvi
->only_restrict_pointers
6021 && !type_contains_placeholder_p (fo
->restrict_pointed_type
)
6022 && !bitmap_bit_p (handled_struct_type
,
6023 TYPE_UID (fo
->restrict_pointed_type
)))
6026 tree heapvar
= build_fake_var_decl (fo
->restrict_pointed_type
);
6027 DECL_EXTERNAL (heapvar
) = 1;
6028 if (var_can_have_subvars (heapvar
))
6029 bitmap_set_bit (handled_struct_type
,
6030 TYPE_UID (fo
->restrict_pointed_type
));
6031 rvi
= create_variable_info_for_1 (heapvar
, "PARM_NOALIAS", true,
6032 true, handled_struct_type
);
6033 if (var_can_have_subvars (heapvar
))
6034 bitmap_clear_bit (handled_struct_type
,
6035 TYPE_UID (fo
->restrict_pointed_type
));
6036 rvi
->is_restrict_var
= 1;
6037 insert_vi_for_tree (heapvar
, rvi
);
6038 make_constraint_from (newvi
, rvi
->id
);
6039 make_param_constraints (rvi
);
6041 if (i
+ 1 < fieldstack
.length ())
6043 varinfo_t tem
= new_var_info (decl
, name
, false);
6044 newvi
->next
= tem
->id
;
6053 create_variable_info_for (tree decl
, const char *name
, bool add_id
)
6055 varinfo_t vi
= create_variable_info_for_1 (decl
, name
, add_id
, false, NULL
);
6056 unsigned int id
= vi
->id
;
6058 insert_vi_for_tree (decl
, vi
);
6063 /* Create initial constraints for globals. */
6064 for (; vi
; vi
= vi_next (vi
))
6066 if (!vi
->may_have_pointers
6067 || !vi
->is_global_var
)
6070 /* Mark global restrict qualified pointers. */
6071 if ((POINTER_TYPE_P (TREE_TYPE (decl
))
6072 && TYPE_RESTRICT (TREE_TYPE (decl
)))
6073 || vi
->only_restrict_pointers
)
6076 = make_constraint_from_global_restrict (vi
, "GLOBAL_RESTRICT",
6078 /* ??? For now exclude reads from globals as restrict sources
6079 if those are not (indirectly) from incoming parameters. */
6080 rvi
->is_restrict_var
= false;
6084 /* In non-IPA mode the initializer from nonlocal is all we need. */
6086 || DECL_HARD_REGISTER (decl
))
6087 make_copy_constraint (vi
, nonlocal_id
);
6089 /* In IPA mode parse the initializer and generate proper constraints
6093 varpool_node
*vnode
= varpool_node::get (decl
);
6095 /* For escaped variables initialize them from nonlocal. */
6096 if (!vnode
->all_refs_explicit_p ())
6097 make_copy_constraint (vi
, nonlocal_id
);
6099 /* If this is a global variable with an initializer and we are in
6100 IPA mode generate constraints for it. */
6102 for (unsigned idx
= 0; vnode
->iterate_reference (idx
, ref
); ++idx
)
6104 auto_vec
<ce_s
> rhsc
;
6105 struct constraint_expr lhs
, *rhsp
;
6107 get_constraint_for_address_of (ref
->referred
->decl
, &rhsc
);
6111 FOR_EACH_VEC_ELT (rhsc
, i
, rhsp
)
6112 process_constraint (new_constraint (lhs
, *rhsp
));
6113 /* If this is a variable that escapes from the unit
6114 the initializer escapes as well. */
6115 if (!vnode
->all_refs_explicit_p ())
6117 lhs
.var
= escaped_id
;
6120 FOR_EACH_VEC_ELT (rhsc
, i
, rhsp
)
6121 process_constraint (new_constraint (lhs
, *rhsp
));
6130 /* Print out the points-to solution for VAR to FILE. */
6133 dump_solution_for_var (FILE *file
, unsigned int var
)
6135 varinfo_t vi
= get_varinfo (var
);
6139 /* Dump the solution for unified vars anyway, this avoids difficulties
6140 in scanning dumps in the testsuite. */
6141 fprintf (file
, "%s = { ", vi
->name
);
6142 vi
= get_varinfo (find (var
));
6143 EXECUTE_IF_SET_IN_BITMAP (vi
->solution
, 0, i
, bi
)
6144 fprintf (file
, "%s ", get_varinfo (i
)->name
);
6145 fprintf (file
, "}");
6147 /* But note when the variable was unified. */
6149 fprintf (file
, " same as %s", vi
->name
);
6151 fprintf (file
, "\n");
6154 /* Print the points-to solution for VAR to stderr. */
6157 debug_solution_for_var (unsigned int var
)
6159 dump_solution_for_var (stderr
, var
);
6162 /* Register the constraints for function parameter related VI. */
6165 make_param_constraints (varinfo_t vi
)
6167 for (; vi
; vi
= vi_next (vi
))
6169 if (vi
->only_restrict_pointers
)
6171 else if (vi
->may_have_pointers
)
6172 make_constraint_from (vi
, nonlocal_id
);
6174 if (vi
->is_full_var
)
6179 /* Create varinfo structures for all of the variables in the
6180 function for intraprocedural mode. */
6183 intra_create_variable_infos (struct function
*fn
)
6186 bitmap handled_struct_type
= NULL
;
6188 /* For each incoming pointer argument arg, create the constraint ARG
6189 = NONLOCAL or a dummy variable if it is a restrict qualified
6190 passed-by-reference argument. */
6191 for (t
= DECL_ARGUMENTS (fn
->decl
); t
; t
= DECL_CHAIN (t
))
6193 if (handled_struct_type
== NULL
)
6194 handled_struct_type
= BITMAP_ALLOC (NULL
);
6197 = create_variable_info_for_1 (t
, alias_get_name (t
), false, true,
6198 handled_struct_type
);
6199 insert_vi_for_tree (t
, p
);
6201 make_param_constraints (p
);
6204 if (handled_struct_type
!= NULL
)
6205 BITMAP_FREE (handled_struct_type
);
6207 /* Add a constraint for a result decl that is passed by reference. */
6208 if (DECL_RESULT (fn
->decl
)
6209 && DECL_BY_REFERENCE (DECL_RESULT (fn
->decl
)))
6211 varinfo_t p
, result_vi
= get_vi_for_tree (DECL_RESULT (fn
->decl
));
6213 for (p
= result_vi
; p
; p
= vi_next (p
))
6214 make_constraint_from (p
, nonlocal_id
);
6217 /* Add a constraint for the incoming static chain parameter. */
6218 if (fn
->static_chain_decl
!= NULL_TREE
)
6220 varinfo_t p
, chain_vi
= get_vi_for_tree (fn
->static_chain_decl
);
6222 for (p
= chain_vi
; p
; p
= vi_next (p
))
6223 make_constraint_from (p
, nonlocal_id
);
6227 /* Structure used to put solution bitmaps in a hashtable so they can
6228 be shared among variables with the same points-to set. */
6230 typedef struct shared_bitmap_info
6234 } *shared_bitmap_info_t
;
6235 typedef const struct shared_bitmap_info
*const_shared_bitmap_info_t
;
6237 /* Shared_bitmap hashtable helpers. */
6239 struct shared_bitmap_hasher
: free_ptr_hash
<shared_bitmap_info
>
6241 static inline hashval_t
hash (const shared_bitmap_info
*);
6242 static inline bool equal (const shared_bitmap_info
*,
6243 const shared_bitmap_info
*);
6246 /* Hash function for a shared_bitmap_info_t */
6249 shared_bitmap_hasher::hash (const shared_bitmap_info
*bi
)
6251 return bi
->hashcode
;
6254 /* Equality function for two shared_bitmap_info_t's. */
6257 shared_bitmap_hasher::equal (const shared_bitmap_info
*sbi1
,
6258 const shared_bitmap_info
*sbi2
)
6260 return bitmap_equal_p (sbi1
->pt_vars
, sbi2
->pt_vars
);
6263 /* Shared_bitmap hashtable. */
6265 static hash_table
<shared_bitmap_hasher
> *shared_bitmap_table
;
6267 /* Lookup a bitmap in the shared bitmap hashtable, and return an already
6268 existing instance if there is one, NULL otherwise. */
6271 shared_bitmap_lookup (bitmap pt_vars
)
6273 shared_bitmap_info
**slot
;
6274 struct shared_bitmap_info sbi
;
6276 sbi
.pt_vars
= pt_vars
;
6277 sbi
.hashcode
= bitmap_hash (pt_vars
);
6279 slot
= shared_bitmap_table
->find_slot (&sbi
, NO_INSERT
);
6283 return (*slot
)->pt_vars
;
6287 /* Add a bitmap to the shared bitmap hashtable. */
6290 shared_bitmap_add (bitmap pt_vars
)
6292 shared_bitmap_info
**slot
;
6293 shared_bitmap_info_t sbi
= XNEW (struct shared_bitmap_info
);
6295 sbi
->pt_vars
= pt_vars
;
6296 sbi
->hashcode
= bitmap_hash (pt_vars
);
6298 slot
= shared_bitmap_table
->find_slot (sbi
, INSERT
);
6299 gcc_assert (!*slot
);
6304 /* Set bits in INTO corresponding to the variable uids in solution set FROM. */
6307 set_uids_in_ptset (bitmap into
, bitmap from
, struct pt_solution
*pt
,
6312 varinfo_t escaped_vi
= get_varinfo (find (escaped_id
));
6313 bool everything_escaped
6314 = escaped_vi
->solution
&& bitmap_bit_p (escaped_vi
->solution
, anything_id
);
6316 EXECUTE_IF_SET_IN_BITMAP (from
, 0, i
, bi
)
6318 varinfo_t vi
= get_varinfo (i
);
6320 /* The only artificial variables that are allowed in a may-alias
6321 set are heap variables. */
6322 if (vi
->is_artificial_var
&& !vi
->is_heap_var
)
6325 if (everything_escaped
6326 || (escaped_vi
->solution
6327 && bitmap_bit_p (escaped_vi
->solution
, i
)))
6329 pt
->vars_contains_escaped
= true;
6330 pt
->vars_contains_escaped_heap
= vi
->is_heap_var
;
6333 if (vi
->is_restrict_var
)
6334 pt
->vars_contains_restrict
= true;
6336 if (VAR_P (vi
->decl
)
6337 || TREE_CODE (vi
->decl
) == PARM_DECL
6338 || TREE_CODE (vi
->decl
) == RESULT_DECL
)
6340 /* If we are in IPA mode we will not recompute points-to
6341 sets after inlining so make sure they stay valid. */
6343 && !DECL_PT_UID_SET_P (vi
->decl
))
6344 SET_DECL_PT_UID (vi
->decl
, DECL_UID (vi
->decl
));
6346 /* Add the decl to the points-to set. Note that the points-to
6347 set contains global variables. */
6348 bitmap_set_bit (into
, DECL_PT_UID (vi
->decl
));
6349 if (vi
->is_global_var
6350 /* In IPA mode the escaped_heap trick doesn't work as
6351 ESCAPED is escaped from the unit but
6352 pt_solution_includes_global needs to answer true for
6353 all variables not automatic within a function.
6354 For the same reason is_global_var is not the
6355 correct flag to track - local variables from other
6356 functions also need to be considered global.
6357 Conveniently all HEAP vars are not put in function
6361 && ! auto_var_in_fn_p (vi
->decl
, fndecl
)))
6362 pt
->vars_contains_nonlocal
= true;
6365 else if (TREE_CODE (vi
->decl
) == FUNCTION_DECL
6366 || TREE_CODE (vi
->decl
) == LABEL_DECL
)
6368 /* Nothing should read/write from/to code so we can
6369 save bits by not including them in the points-to bitmaps.
6370 Still mark the points-to set as containing global memory
6371 to make code-patching possible - see PR70128. */
6372 pt
->vars_contains_nonlocal
= true;
6378 /* Compute the points-to solution *PT for the variable VI. */
6380 static struct pt_solution
6381 find_what_var_points_to (tree fndecl
, varinfo_t orig_vi
)
6385 bitmap finished_solution
;
6388 struct pt_solution
*pt
;
6390 /* This variable may have been collapsed, let's get the real
6392 vi
= get_varinfo (find (orig_vi
->id
));
6394 /* See if we have already computed the solution and return it. */
6395 pt_solution
**slot
= &final_solutions
->get_or_insert (vi
);
6399 *slot
= pt
= XOBNEW (&final_solutions_obstack
, struct pt_solution
);
6400 memset (pt
, 0, sizeof (struct pt_solution
));
6402 /* Translate artificial variables into SSA_NAME_PTR_INFO
6404 EXECUTE_IF_SET_IN_BITMAP (vi
->solution
, 0, i
, bi
)
6406 varinfo_t vi
= get_varinfo (i
);
6408 if (vi
->is_artificial_var
)
6410 if (vi
->id
== nothing_id
)
6412 else if (vi
->id
== escaped_id
)
6415 pt
->ipa_escaped
= 1;
6418 /* Expand some special vars of ESCAPED in-place here. */
6419 varinfo_t evi
= get_varinfo (find (escaped_id
));
6420 if (bitmap_bit_p (evi
->solution
, nonlocal_id
))
6423 else if (vi
->id
== nonlocal_id
)
6425 else if (vi
->is_heap_var
)
6426 /* We represent heapvars in the points-to set properly. */
6428 else if (vi
->id
== string_id
)
6429 /* Nobody cares - STRING_CSTs are read-only entities. */
6431 else if (vi
->id
== anything_id
6432 || vi
->id
== integer_id
)
6437 /* Instead of doing extra work, simply do not create
6438 elaborate points-to information for pt_anything pointers. */
6442 /* Share the final set of variables when possible. */
6443 finished_solution
= BITMAP_GGC_ALLOC ();
6444 stats
.points_to_sets_created
++;
6446 set_uids_in_ptset (finished_solution
, vi
->solution
, pt
, fndecl
);
6447 result
= shared_bitmap_lookup (finished_solution
);
6450 shared_bitmap_add (finished_solution
);
6451 pt
->vars
= finished_solution
;
6456 bitmap_clear (finished_solution
);
6462 /* Given a pointer variable P, fill in its points-to set. */
6465 find_what_p_points_to (tree fndecl
, tree p
)
6467 struct ptr_info_def
*pi
;
6470 bool nonnull
= get_ptr_nonnull (p
);
6472 /* For parameters, get at the points-to set for the actual parm
6474 if (TREE_CODE (p
) == SSA_NAME
6475 && SSA_NAME_IS_DEFAULT_DEF (p
)
6476 && (TREE_CODE (SSA_NAME_VAR (p
)) == PARM_DECL
6477 || TREE_CODE (SSA_NAME_VAR (p
)) == RESULT_DECL
))
6478 lookup_p
= SSA_NAME_VAR (p
);
6480 vi
= lookup_vi_for_tree (lookup_p
);
6484 pi
= get_ptr_info (p
);
6485 pi
->pt
= find_what_var_points_to (fndecl
, vi
);
6486 /* Conservatively set to NULL from PTA (to true). */
6488 /* Preserve pointer nonnull computed by VRP. See get_ptr_nonnull
6489 in gcc/tree-ssaname.c for more information. */
6491 set_ptr_nonnull (p
);
6495 /* Query statistics for points-to solutions. */
6498 unsigned HOST_WIDE_INT pt_solution_includes_may_alias
;
6499 unsigned HOST_WIDE_INT pt_solution_includes_no_alias
;
6500 unsigned HOST_WIDE_INT pt_solutions_intersect_may_alias
;
6501 unsigned HOST_WIDE_INT pt_solutions_intersect_no_alias
;
6505 dump_pta_stats (FILE *s
)
6507 fprintf (s
, "\nPTA query stats:\n");
6508 fprintf (s
, " pt_solution_includes: "
6509 HOST_WIDE_INT_PRINT_DEC
" disambiguations, "
6510 HOST_WIDE_INT_PRINT_DEC
" queries\n",
6511 pta_stats
.pt_solution_includes_no_alias
,
6512 pta_stats
.pt_solution_includes_no_alias
6513 + pta_stats
.pt_solution_includes_may_alias
);
6514 fprintf (s
, " pt_solutions_intersect: "
6515 HOST_WIDE_INT_PRINT_DEC
" disambiguations, "
6516 HOST_WIDE_INT_PRINT_DEC
" queries\n",
6517 pta_stats
.pt_solutions_intersect_no_alias
,
6518 pta_stats
.pt_solutions_intersect_no_alias
6519 + pta_stats
.pt_solutions_intersect_may_alias
);
6523 /* Reset the points-to solution *PT to a conservative default
6524 (point to anything). */
6527 pt_solution_reset (struct pt_solution
*pt
)
6529 memset (pt
, 0, sizeof (struct pt_solution
));
6530 pt
->anything
= true;
6534 /* Set the points-to solution *PT to point only to the variables
6535 in VARS. VARS_CONTAINS_GLOBAL specifies whether that contains
6536 global variables and VARS_CONTAINS_RESTRICT specifies whether
6537 it contains restrict tag variables. */
6540 pt_solution_set (struct pt_solution
*pt
, bitmap vars
,
6541 bool vars_contains_nonlocal
)
6543 memset (pt
, 0, sizeof (struct pt_solution
));
6545 pt
->vars_contains_nonlocal
= vars_contains_nonlocal
;
6546 pt
->vars_contains_escaped
6547 = (cfun
->gimple_df
->escaped
.anything
6548 || bitmap_intersect_p (cfun
->gimple_df
->escaped
.vars
, vars
));
6551 /* Set the points-to solution *PT to point only to the variable VAR. */
6554 pt_solution_set_var (struct pt_solution
*pt
, tree var
)
6556 memset (pt
, 0, sizeof (struct pt_solution
));
6557 pt
->vars
= BITMAP_GGC_ALLOC ();
6558 bitmap_set_bit (pt
->vars
, DECL_PT_UID (var
));
6559 pt
->vars_contains_nonlocal
= is_global_var (var
);
6560 pt
->vars_contains_escaped
6561 = (cfun
->gimple_df
->escaped
.anything
6562 || bitmap_bit_p (cfun
->gimple_df
->escaped
.vars
, DECL_PT_UID (var
)));
6565 /* Computes the union of the points-to solutions *DEST and *SRC and
6566 stores the result in *DEST. This changes the points-to bitmap
6567 of *DEST and thus may not be used if that might be shared.
6568 The points-to bitmap of *SRC and *DEST will not be shared after
6569 this function if they were not before. */
6572 pt_solution_ior_into (struct pt_solution
*dest
, struct pt_solution
*src
)
6574 dest
->anything
|= src
->anything
;
6577 pt_solution_reset (dest
);
6581 dest
->nonlocal
|= src
->nonlocal
;
6582 dest
->escaped
|= src
->escaped
;
6583 dest
->ipa_escaped
|= src
->ipa_escaped
;
6584 dest
->null
|= src
->null
;
6585 dest
->vars_contains_nonlocal
|= src
->vars_contains_nonlocal
;
6586 dest
->vars_contains_escaped
|= src
->vars_contains_escaped
;
6587 dest
->vars_contains_escaped_heap
|= src
->vars_contains_escaped_heap
;
6592 dest
->vars
= BITMAP_GGC_ALLOC ();
6593 bitmap_ior_into (dest
->vars
, src
->vars
);
6596 /* Return true if the points-to solution *PT is empty. */
6599 pt_solution_empty_p (struct pt_solution
*pt
)
6606 && !bitmap_empty_p (pt
->vars
))
6609 /* If the solution includes ESCAPED, check if that is empty. */
6611 && !pt_solution_empty_p (&cfun
->gimple_df
->escaped
))
6614 /* If the solution includes ESCAPED, check if that is empty. */
6616 && !pt_solution_empty_p (&ipa_escaped_pt
))
6622 /* Return true if the points-to solution *PT only point to a single var, and
6623 return the var uid in *UID. */
6626 pt_solution_singleton_or_null_p (struct pt_solution
*pt
, unsigned *uid
)
6628 if (pt
->anything
|| pt
->nonlocal
|| pt
->escaped
|| pt
->ipa_escaped
6630 || !bitmap_single_bit_set_p (pt
->vars
))
6633 *uid
= bitmap_first_set_bit (pt
->vars
);
6637 /* Return true if the points-to solution *PT includes global memory. */
6640 pt_solution_includes_global (struct pt_solution
*pt
)
6644 || pt
->vars_contains_nonlocal
6645 /* The following is a hack to make the malloc escape hack work.
6646 In reality we'd need different sets for escaped-through-return
6647 and escaped-to-callees and passes would need to be updated. */
6648 || pt
->vars_contains_escaped_heap
)
6651 /* 'escaped' is also a placeholder so we have to look into it. */
6653 return pt_solution_includes_global (&cfun
->gimple_df
->escaped
);
6655 if (pt
->ipa_escaped
)
6656 return pt_solution_includes_global (&ipa_escaped_pt
);
6661 /* Return true if the points-to solution *PT includes the variable
6662 declaration DECL. */
6665 pt_solution_includes_1 (struct pt_solution
*pt
, const_tree decl
)
6671 && is_global_var (decl
))
6675 && bitmap_bit_p (pt
->vars
, DECL_PT_UID (decl
)))
6678 /* If the solution includes ESCAPED, check it. */
6680 && pt_solution_includes_1 (&cfun
->gimple_df
->escaped
, decl
))
6683 /* If the solution includes ESCAPED, check it. */
6685 && pt_solution_includes_1 (&ipa_escaped_pt
, decl
))
6692 pt_solution_includes (struct pt_solution
*pt
, const_tree decl
)
6694 bool res
= pt_solution_includes_1 (pt
, decl
);
6696 ++pta_stats
.pt_solution_includes_may_alias
;
6698 ++pta_stats
.pt_solution_includes_no_alias
;
6702 /* Return true if both points-to solutions PT1 and PT2 have a non-empty
6706 pt_solutions_intersect_1 (struct pt_solution
*pt1
, struct pt_solution
*pt2
)
6708 if (pt1
->anything
|| pt2
->anything
)
6711 /* If either points to unknown global memory and the other points to
6712 any global memory they alias. */
6715 || pt2
->vars_contains_nonlocal
))
6717 && pt1
->vars_contains_nonlocal
))
6720 /* If either points to all escaped memory and the other points to
6721 any escaped memory they alias. */
6724 || pt2
->vars_contains_escaped
))
6726 && pt1
->vars_contains_escaped
))
6729 /* Check the escaped solution if required.
6730 ??? Do we need to check the local against the IPA escaped sets? */
6731 if ((pt1
->ipa_escaped
|| pt2
->ipa_escaped
)
6732 && !pt_solution_empty_p (&ipa_escaped_pt
))
6734 /* If both point to escaped memory and that solution
6735 is not empty they alias. */
6736 if (pt1
->ipa_escaped
&& pt2
->ipa_escaped
)
6739 /* If either points to escaped memory see if the escaped solution
6740 intersects with the other. */
6741 if ((pt1
->ipa_escaped
6742 && pt_solutions_intersect_1 (&ipa_escaped_pt
, pt2
))
6743 || (pt2
->ipa_escaped
6744 && pt_solutions_intersect_1 (&ipa_escaped_pt
, pt1
)))
6748 /* Now both pointers alias if their points-to solution intersects. */
6751 && bitmap_intersect_p (pt1
->vars
, pt2
->vars
));
6755 pt_solutions_intersect (struct pt_solution
*pt1
, struct pt_solution
*pt2
)
6757 bool res
= pt_solutions_intersect_1 (pt1
, pt2
);
6759 ++pta_stats
.pt_solutions_intersect_may_alias
;
6761 ++pta_stats
.pt_solutions_intersect_no_alias
;
6766 /* Dump points-to information to OUTFILE. */
6769 dump_sa_points_to_info (FILE *outfile
)
6773 fprintf (outfile
, "\nPoints-to sets\n\n");
6775 if (dump_flags
& TDF_STATS
)
6777 fprintf (outfile
, "Stats:\n");
6778 fprintf (outfile
, "Total vars: %d\n", stats
.total_vars
);
6779 fprintf (outfile
, "Non-pointer vars: %d\n",
6780 stats
.nonpointer_vars
);
6781 fprintf (outfile
, "Statically unified vars: %d\n",
6782 stats
.unified_vars_static
);
6783 fprintf (outfile
, "Dynamically unified vars: %d\n",
6784 stats
.unified_vars_dynamic
);
6785 fprintf (outfile
, "Iterations: %d\n", stats
.iterations
);
6786 fprintf (outfile
, "Number of edges: %d\n", stats
.num_edges
);
6787 fprintf (outfile
, "Number of implicit edges: %d\n",
6788 stats
.num_implicit_edges
);
6791 for (i
= 1; i
< varmap
.length (); i
++)
6793 varinfo_t vi
= get_varinfo (i
);
6794 if (!vi
->may_have_pointers
)
6796 dump_solution_for_var (outfile
, i
);
6801 /* Debug points-to information to stderr. */
6804 debug_sa_points_to_info (void)
6806 dump_sa_points_to_info (stderr
);
6810 /* Initialize the always-existing constraint variables for NULL
6811 ANYTHING, READONLY, and INTEGER */
6814 init_base_vars (void)
6816 struct constraint_expr lhs
, rhs
;
6817 varinfo_t var_anything
;
6818 varinfo_t var_nothing
;
6819 varinfo_t var_string
;
6820 varinfo_t var_escaped
;
6821 varinfo_t var_nonlocal
;
6822 varinfo_t var_storedanything
;
6823 varinfo_t var_integer
;
6825 /* Variable ID zero is reserved and should be NULL. */
6826 varmap
.safe_push (NULL
);
6828 /* Create the NULL variable, used to represent that a variable points
6830 var_nothing
= new_var_info (NULL_TREE
, "NULL", false);
6831 gcc_assert (var_nothing
->id
== nothing_id
);
6832 var_nothing
->is_artificial_var
= 1;
6833 var_nothing
->offset
= 0;
6834 var_nothing
->size
= ~0;
6835 var_nothing
->fullsize
= ~0;
6836 var_nothing
->is_special_var
= 1;
6837 var_nothing
->may_have_pointers
= 0;
6838 var_nothing
->is_global_var
= 0;
6840 /* Create the ANYTHING variable, used to represent that a variable
6841 points to some unknown piece of memory. */
6842 var_anything
= new_var_info (NULL_TREE
, "ANYTHING", false);
6843 gcc_assert (var_anything
->id
== anything_id
);
6844 var_anything
->is_artificial_var
= 1;
6845 var_anything
->size
= ~0;
6846 var_anything
->offset
= 0;
6847 var_anything
->fullsize
= ~0;
6848 var_anything
->is_special_var
= 1;
6850 /* Anything points to anything. This makes deref constraints just
6851 work in the presence of linked list and other p = *p type loops,
6852 by saying that *ANYTHING = ANYTHING. */
6854 lhs
.var
= anything_id
;
6856 rhs
.type
= ADDRESSOF
;
6857 rhs
.var
= anything_id
;
6860 /* This specifically does not use process_constraint because
6861 process_constraint ignores all anything = anything constraints, since all
6862 but this one are redundant. */
6863 constraints
.safe_push (new_constraint (lhs
, rhs
));
6865 /* Create the STRING variable, used to represent that a variable
6866 points to a string literal. String literals don't contain
6867 pointers so STRING doesn't point to anything. */
6868 var_string
= new_var_info (NULL_TREE
, "STRING", false);
6869 gcc_assert (var_string
->id
== string_id
);
6870 var_string
->is_artificial_var
= 1;
6871 var_string
->offset
= 0;
6872 var_string
->size
= ~0;
6873 var_string
->fullsize
= ~0;
6874 var_string
->is_special_var
= 1;
6875 var_string
->may_have_pointers
= 0;
6877 /* Create the ESCAPED variable, used to represent the set of escaped
6879 var_escaped
= new_var_info (NULL_TREE
, "ESCAPED", false);
6880 gcc_assert (var_escaped
->id
== escaped_id
);
6881 var_escaped
->is_artificial_var
= 1;
6882 var_escaped
->offset
= 0;
6883 var_escaped
->size
= ~0;
6884 var_escaped
->fullsize
= ~0;
6885 var_escaped
->is_special_var
= 0;
6887 /* Create the NONLOCAL variable, used to represent the set of nonlocal
6889 var_nonlocal
= new_var_info (NULL_TREE
, "NONLOCAL", false);
6890 gcc_assert (var_nonlocal
->id
== nonlocal_id
);
6891 var_nonlocal
->is_artificial_var
= 1;
6892 var_nonlocal
->offset
= 0;
6893 var_nonlocal
->size
= ~0;
6894 var_nonlocal
->fullsize
= ~0;
6895 var_nonlocal
->is_special_var
= 1;
6897 /* ESCAPED = *ESCAPED, because escaped is may-deref'd at calls, etc. */
6899 lhs
.var
= escaped_id
;
6902 rhs
.var
= escaped_id
;
6904 process_constraint (new_constraint (lhs
, rhs
));
6906 /* ESCAPED = ESCAPED + UNKNOWN_OFFSET, because if a sub-field escapes the
6907 whole variable escapes. */
6909 lhs
.var
= escaped_id
;
6912 rhs
.var
= escaped_id
;
6913 rhs
.offset
= UNKNOWN_OFFSET
;
6914 process_constraint (new_constraint (lhs
, rhs
));
6916 /* *ESCAPED = NONLOCAL. This is true because we have to assume
6917 everything pointed to by escaped points to what global memory can
6920 lhs
.var
= escaped_id
;
6923 rhs
.var
= nonlocal_id
;
6925 process_constraint (new_constraint (lhs
, rhs
));
6927 /* NONLOCAL = &NONLOCAL, NONLOCAL = &ESCAPED. This is true because
6928 global memory may point to global memory and escaped memory. */
6930 lhs
.var
= nonlocal_id
;
6932 rhs
.type
= ADDRESSOF
;
6933 rhs
.var
= nonlocal_id
;
6935 process_constraint (new_constraint (lhs
, rhs
));
6936 rhs
.type
= ADDRESSOF
;
6937 rhs
.var
= escaped_id
;
6939 process_constraint (new_constraint (lhs
, rhs
));
6941 /* Create the STOREDANYTHING variable, used to represent the set of
6942 variables stored to *ANYTHING. */
6943 var_storedanything
= new_var_info (NULL_TREE
, "STOREDANYTHING", false);
6944 gcc_assert (var_storedanything
->id
== storedanything_id
);
6945 var_storedanything
->is_artificial_var
= 1;
6946 var_storedanything
->offset
= 0;
6947 var_storedanything
->size
= ~0;
6948 var_storedanything
->fullsize
= ~0;
6949 var_storedanything
->is_special_var
= 0;
6951 /* Create the INTEGER variable, used to represent that a variable points
6952 to what an INTEGER "points to". */
6953 var_integer
= new_var_info (NULL_TREE
, "INTEGER", false);
6954 gcc_assert (var_integer
->id
== integer_id
);
6955 var_integer
->is_artificial_var
= 1;
6956 var_integer
->size
= ~0;
6957 var_integer
->fullsize
= ~0;
6958 var_integer
->offset
= 0;
6959 var_integer
->is_special_var
= 1;
6961 /* INTEGER = ANYTHING, because we don't know where a dereference of
6962 a random integer will point to. */
6964 lhs
.var
= integer_id
;
6966 rhs
.type
= ADDRESSOF
;
6967 rhs
.var
= anything_id
;
6969 process_constraint (new_constraint (lhs
, rhs
));
6972 /* Initialize things necessary to perform PTA */
6975 init_alias_vars (void)
6977 use_field_sensitive
= (MAX_FIELDS_FOR_FIELD_SENSITIVE
> 1);
6979 bitmap_obstack_initialize (&pta_obstack
);
6980 bitmap_obstack_initialize (&oldpta_obstack
);
6981 bitmap_obstack_initialize (&predbitmap_obstack
);
6983 constraints
.create (8);
6985 vi_for_tree
= new hash_map
<tree
, varinfo_t
>;
6986 call_stmt_vars
= new hash_map
<gimple
*, varinfo_t
>;
6988 memset (&stats
, 0, sizeof (stats
));
6989 shared_bitmap_table
= new hash_table
<shared_bitmap_hasher
> (511);
6992 gcc_obstack_init (&fake_var_decl_obstack
);
6994 final_solutions
= new hash_map
<varinfo_t
, pt_solution
*>;
6995 gcc_obstack_init (&final_solutions_obstack
);
6998 /* Remove the REF and ADDRESS edges from GRAPH, as well as all the
6999 predecessor edges. */
7002 remove_preds_and_fake_succs (constraint_graph_t graph
)
7006 /* Clear the implicit ref and address nodes from the successor
7008 for (i
= 1; i
< FIRST_REF_NODE
; i
++)
7010 if (graph
->succs
[i
])
7011 bitmap_clear_range (graph
->succs
[i
], FIRST_REF_NODE
,
7012 FIRST_REF_NODE
* 2);
7015 /* Free the successor list for the non-ref nodes. */
7016 for (i
= FIRST_REF_NODE
+ 1; i
< graph
->size
; i
++)
7018 if (graph
->succs
[i
])
7019 BITMAP_FREE (graph
->succs
[i
]);
7022 /* Now reallocate the size of the successor list as, and blow away
7023 the predecessor bitmaps. */
7024 graph
->size
= varmap
.length ();
7025 graph
->succs
= XRESIZEVEC (bitmap
, graph
->succs
, graph
->size
);
7027 free (graph
->implicit_preds
);
7028 graph
->implicit_preds
= NULL
;
7029 free (graph
->preds
);
7030 graph
->preds
= NULL
;
7031 bitmap_obstack_release (&predbitmap_obstack
);
7034 /* Solve the constraint set. */
7037 solve_constraints (void)
7039 struct scc_info
*si
;
7043 "\nCollapsing static cycles and doing variable "
7046 init_graph (varmap
.length () * 2);
7049 fprintf (dump_file
, "Building predecessor graph\n");
7050 build_pred_graph ();
7053 fprintf (dump_file
, "Detecting pointer and location "
7055 si
= perform_var_substitution (graph
);
7058 fprintf (dump_file
, "Rewriting constraints and unifying "
7060 rewrite_constraints (graph
, si
);
7062 build_succ_graph ();
7064 free_var_substitution_info (si
);
7066 /* Attach complex constraints to graph nodes. */
7067 move_complex_constraints (graph
);
7070 fprintf (dump_file
, "Uniting pointer but not location equivalent "
7072 unite_pointer_equivalences (graph
);
7075 fprintf (dump_file
, "Finding indirect cycles\n");
7076 find_indirect_cycles (graph
);
7078 /* Implicit nodes and predecessors are no longer necessary at this
7080 remove_preds_and_fake_succs (graph
);
7082 if (dump_file
&& (dump_flags
& TDF_GRAPH
))
7084 fprintf (dump_file
, "\n\n// The constraint graph before solve-graph "
7085 "in dot format:\n");
7086 dump_constraint_graph (dump_file
);
7087 fprintf (dump_file
, "\n\n");
7091 fprintf (dump_file
, "Solving graph\n");
7093 solve_graph (graph
);
7095 if (dump_file
&& (dump_flags
& TDF_GRAPH
))
7097 fprintf (dump_file
, "\n\n// The constraint graph after solve-graph "
7098 "in dot format:\n");
7099 dump_constraint_graph (dump_file
);
7100 fprintf (dump_file
, "\n\n");
7104 dump_sa_points_to_info (dump_file
);
7107 /* Create points-to sets for the current function. See the comments
7108 at the start of the file for an algorithmic overview. */
7111 compute_points_to_sets (void)
7116 timevar_push (TV_TREE_PTA
);
7120 intra_create_variable_infos (cfun
);
7122 /* Now walk all statements and build the constraint set. */
7123 FOR_EACH_BB_FN (bb
, cfun
)
7125 for (gphi_iterator gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
);
7128 gphi
*phi
= gsi
.phi ();
7130 if (! virtual_operand_p (gimple_phi_result (phi
)))
7131 find_func_aliases (cfun
, phi
);
7134 for (gimple_stmt_iterator gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
);
7137 gimple
*stmt
= gsi_stmt (gsi
);
7139 find_func_aliases (cfun
, stmt
);
7145 fprintf (dump_file
, "Points-to analysis\n\nConstraints:\n\n");
7146 dump_constraints (dump_file
, 0);
7149 /* From the constraints compute the points-to sets. */
7150 solve_constraints ();
7152 /* Compute the points-to set for ESCAPED used for call-clobber analysis. */
7153 cfun
->gimple_df
->escaped
= find_what_var_points_to (cfun
->decl
,
7154 get_varinfo (escaped_id
));
7156 /* Make sure the ESCAPED solution (which is used as placeholder in
7157 other solutions) does not reference itself. This simplifies
7158 points-to solution queries. */
7159 cfun
->gimple_df
->escaped
.escaped
= 0;
7161 /* Compute the points-to sets for pointer SSA_NAMEs. */
7165 FOR_EACH_SSA_NAME (i
, ptr
, cfun
)
7167 if (POINTER_TYPE_P (TREE_TYPE (ptr
)))
7168 find_what_p_points_to (cfun
->decl
, ptr
);
7171 /* Compute the call-used/clobbered sets. */
7172 FOR_EACH_BB_FN (bb
, cfun
)
7174 gimple_stmt_iterator gsi
;
7176 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
7179 struct pt_solution
*pt
;
7181 stmt
= dyn_cast
<gcall
*> (gsi_stmt (gsi
));
7185 pt
= gimple_call_use_set (stmt
);
7186 if (gimple_call_flags (stmt
) & ECF_CONST
)
7187 memset (pt
, 0, sizeof (struct pt_solution
));
7188 else if ((vi
= lookup_call_use_vi (stmt
)) != NULL
)
7190 *pt
= find_what_var_points_to (cfun
->decl
, vi
);
7191 /* Escaped (and thus nonlocal) variables are always
7192 implicitly used by calls. */
7193 /* ??? ESCAPED can be empty even though NONLOCAL
7200 /* If there is nothing special about this call then
7201 we have made everything that is used also escape. */
7202 *pt
= cfun
->gimple_df
->escaped
;
7206 pt
= gimple_call_clobber_set (stmt
);
7207 if (gimple_call_flags (stmt
) & (ECF_CONST
|ECF_PURE
|ECF_NOVOPS
))
7208 memset (pt
, 0, sizeof (struct pt_solution
));
7209 else if ((vi
= lookup_call_clobber_vi (stmt
)) != NULL
)
7211 *pt
= find_what_var_points_to (cfun
->decl
, vi
);
7212 /* Escaped (and thus nonlocal) variables are always
7213 implicitly clobbered by calls. */
7214 /* ??? ESCAPED can be empty even though NONLOCAL
7221 /* If there is nothing special about this call then
7222 we have made everything that is used also escape. */
7223 *pt
= cfun
->gimple_df
->escaped
;
7229 timevar_pop (TV_TREE_PTA
);
7233 /* Delete created points-to sets. */
7236 delete_points_to_sets (void)
7240 delete shared_bitmap_table
;
7241 shared_bitmap_table
= NULL
;
7242 if (dump_file
&& (dump_flags
& TDF_STATS
))
7243 fprintf (dump_file
, "Points to sets created:%d\n",
7244 stats
.points_to_sets_created
);
7247 delete call_stmt_vars
;
7248 bitmap_obstack_release (&pta_obstack
);
7249 constraints
.release ();
7251 for (i
= 0; i
< graph
->size
; i
++)
7252 graph
->complex[i
].release ();
7253 free (graph
->complex);
7256 free (graph
->succs
);
7258 free (graph
->pe_rep
);
7259 free (graph
->indirect_cycles
);
7263 variable_info_pool
.release ();
7264 constraint_pool
.release ();
7266 obstack_free (&fake_var_decl_obstack
, NULL
);
7268 delete final_solutions
;
7269 obstack_free (&final_solutions_obstack
, NULL
);
7274 unsigned short clique
;
7278 /* Mark "other" loads and stores as belonging to CLIQUE and with
7282 visit_loadstore (gimple
*, tree base
, tree ref
, void *data
)
7284 unsigned short clique
= ((vls_data
*) data
)->clique
;
7285 bitmap rvars
= ((vls_data
*) data
)->rvars
;
7286 if (TREE_CODE (base
) == MEM_REF
7287 || TREE_CODE (base
) == TARGET_MEM_REF
)
7289 tree ptr
= TREE_OPERAND (base
, 0);
7290 if (TREE_CODE (ptr
) == SSA_NAME
7291 && ! SSA_NAME_IS_DEFAULT_DEF (ptr
))
7293 /* We need to make sure 'ptr' doesn't include any of
7294 the restrict tags we added bases for in its points-to set. */
7295 varinfo_t vi
= lookup_vi_for_tree (ptr
);
7299 vi
= get_varinfo (find (vi
->id
));
7300 if (bitmap_intersect_p (rvars
, vi
->solution
))
7304 /* Do not overwrite existing cliques (that includes clique, base
7305 pairs we just set). */
7306 if (MR_DEPENDENCE_CLIQUE (base
) == 0)
7308 MR_DEPENDENCE_CLIQUE (base
) = clique
;
7309 MR_DEPENDENCE_BASE (base
) = 0;
7313 /* For plain decl accesses see whether they are accesses to globals
7314 and rewrite them to MEM_REFs with { clique, 0 }. */
7316 && is_global_var (base
)
7317 /* ??? We can't rewrite a plain decl with the walk_stmt_load_store
7322 while (handled_component_p (*basep
))
7323 basep
= &TREE_OPERAND (*basep
, 0);
7324 gcc_assert (VAR_P (*basep
));
7325 tree ptr
= build_fold_addr_expr (*basep
);
7326 tree zero
= build_int_cst (TREE_TYPE (ptr
), 0);
7327 *basep
= build2 (MEM_REF
, TREE_TYPE (*basep
), ptr
, zero
);
7328 MR_DEPENDENCE_CLIQUE (*basep
) = clique
;
7329 MR_DEPENDENCE_BASE (*basep
) = 0;
7335 /* If REF is a MEM_REF then assign a clique, base pair to it, updating
7336 CLIQUE, *RESTRICT_VAR and LAST_RUID. Return whether dependence info
7337 was assigned to REF. */
7340 maybe_set_dependence_info (tree ref
, tree ptr
,
7341 unsigned short &clique
, varinfo_t restrict_var
,
7342 unsigned short &last_ruid
)
7344 while (handled_component_p (ref
))
7345 ref
= TREE_OPERAND (ref
, 0);
7346 if ((TREE_CODE (ref
) == MEM_REF
7347 || TREE_CODE (ref
) == TARGET_MEM_REF
)
7348 && TREE_OPERAND (ref
, 0) == ptr
)
7350 /* Do not overwrite existing cliques. This avoids overwriting dependence
7351 info inlined from a function with restrict parameters inlined
7352 into a function with restrict parameters. This usually means we
7353 prefer to be precise in innermost loops. */
7354 if (MR_DEPENDENCE_CLIQUE (ref
) == 0)
7357 clique
= ++cfun
->last_clique
;
7358 if (restrict_var
->ruid
== 0)
7359 restrict_var
->ruid
= ++last_ruid
;
7360 MR_DEPENDENCE_CLIQUE (ref
) = clique
;
7361 MR_DEPENDENCE_BASE (ref
) = restrict_var
->ruid
;
7368 /* Compute the set of independend memory references based on restrict
7369 tags and their conservative propagation to the points-to sets. */
7372 compute_dependence_clique (void)
7374 unsigned short clique
= 0;
7375 unsigned short last_ruid
= 0;
7376 bitmap rvars
= BITMAP_ALLOC (NULL
);
7377 for (unsigned i
= 0; i
< num_ssa_names
; ++i
)
7379 tree ptr
= ssa_name (i
);
7380 if (!ptr
|| !POINTER_TYPE_P (TREE_TYPE (ptr
)))
7383 /* Avoid all this when ptr is not dereferenced? */
7385 if (SSA_NAME_IS_DEFAULT_DEF (ptr
)
7386 && (TREE_CODE (SSA_NAME_VAR (ptr
)) == PARM_DECL
7387 || TREE_CODE (SSA_NAME_VAR (ptr
)) == RESULT_DECL
))
7388 p
= SSA_NAME_VAR (ptr
);
7389 varinfo_t vi
= lookup_vi_for_tree (p
);
7392 vi
= get_varinfo (find (vi
->id
));
7395 varinfo_t restrict_var
= NULL
;
7396 EXECUTE_IF_SET_IN_BITMAP (vi
->solution
, 0, j
, bi
)
7398 varinfo_t oi
= get_varinfo (j
);
7399 if (oi
->is_restrict_var
)
7403 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7405 fprintf (dump_file
, "found restrict pointed-to "
7407 print_generic_expr (dump_file
, ptr
, 0);
7408 fprintf (dump_file
, " but not exclusively\n");
7410 restrict_var
= NULL
;
7415 /* NULL is the only other valid points-to entry. */
7416 else if (oi
->id
!= nothing_id
)
7418 restrict_var
= NULL
;
7422 /* Ok, found that ptr must(!) point to a single(!) restrict
7424 /* ??? PTA isn't really a proper propagation engine to compute
7426 ??? We could handle merging of two restricts by unifying them. */
7429 /* Now look at possible dereferences of ptr. */
7430 imm_use_iterator ui
;
7433 FOR_EACH_IMM_USE_STMT (use_stmt
, ui
, ptr
)
7435 /* ??? Calls and asms. */
7436 if (!gimple_assign_single_p (use_stmt
))
7438 used
|= maybe_set_dependence_info (gimple_assign_lhs (use_stmt
),
7439 ptr
, clique
, restrict_var
,
7441 used
|= maybe_set_dependence_info (gimple_assign_rhs1 (use_stmt
),
7442 ptr
, clique
, restrict_var
,
7446 bitmap_set_bit (rvars
, restrict_var
->id
);
7452 /* Assign the BASE id zero to all accesses not based on a restrict
7453 pointer. That way they get disambiguated against restrict
7454 accesses but not against each other. */
7455 /* ??? For restricts derived from globals (thus not incoming
7456 parameters) we can't restrict scoping properly thus the following
7457 is too aggressive there. For now we have excluded those globals from
7458 getting into the MR_DEPENDENCE machinery. */
7459 vls_data data
= { clique
, rvars
};
7461 FOR_EACH_BB_FN (bb
, cfun
)
7462 for (gimple_stmt_iterator gsi
= gsi_start_bb (bb
);
7463 !gsi_end_p (gsi
); gsi_next (&gsi
))
7465 gimple
*stmt
= gsi_stmt (gsi
);
7466 walk_stmt_load_store_ops (stmt
, &data
,
7467 visit_loadstore
, visit_loadstore
);
7471 BITMAP_FREE (rvars
);
7474 /* Compute points-to information for every SSA_NAME pointer in the
7475 current function and compute the transitive closure of escaped
7476 variables to re-initialize the call-clobber states of local variables. */
7479 compute_may_aliases (void)
7481 if (cfun
->gimple_df
->ipa_pta
)
7485 fprintf (dump_file
, "\nNot re-computing points-to information "
7486 "because IPA points-to information is available.\n\n");
7488 /* But still dump what we have remaining it. */
7489 dump_alias_info (dump_file
);
7495 /* For each pointer P_i, determine the sets of variables that P_i may
7496 point-to. Compute the reachability set of escaped and call-used
7498 compute_points_to_sets ();
7500 /* Debugging dumps. */
7502 dump_alias_info (dump_file
);
7504 /* Compute restrict-based memory disambiguations. */
7505 compute_dependence_clique ();
7507 /* Deallocate memory used by aliasing data structures and the internal
7508 points-to solution. */
7509 delete_points_to_sets ();
7511 gcc_assert (!need_ssa_update_p (cfun
));
7516 /* A dummy pass to cause points-to information to be computed via
7517 TODO_rebuild_alias. */
7521 const pass_data pass_data_build_alias
=
7523 GIMPLE_PASS
, /* type */
7525 OPTGROUP_NONE
, /* optinfo_flags */
7526 TV_NONE
, /* tv_id */
7527 ( PROP_cfg
| PROP_ssa
), /* properties_required */
7528 0, /* properties_provided */
7529 0, /* properties_destroyed */
7530 0, /* todo_flags_start */
7531 TODO_rebuild_alias
, /* todo_flags_finish */
7534 class pass_build_alias
: public gimple_opt_pass
7537 pass_build_alias (gcc::context
*ctxt
)
7538 : gimple_opt_pass (pass_data_build_alias
, ctxt
)
7541 /* opt_pass methods: */
7542 virtual bool gate (function
*) { return flag_tree_pta
; }
7544 }; // class pass_build_alias
7549 make_pass_build_alias (gcc::context
*ctxt
)
7551 return new pass_build_alias (ctxt
);
7554 /* A dummy pass to cause points-to information to be computed via
7555 TODO_rebuild_alias. */
7559 const pass_data pass_data_build_ealias
=
7561 GIMPLE_PASS
, /* type */
7562 "ealias", /* name */
7563 OPTGROUP_NONE
, /* optinfo_flags */
7564 TV_NONE
, /* tv_id */
7565 ( PROP_cfg
| PROP_ssa
), /* properties_required */
7566 0, /* properties_provided */
7567 0, /* properties_destroyed */
7568 0, /* todo_flags_start */
7569 TODO_rebuild_alias
, /* todo_flags_finish */
7572 class pass_build_ealias
: public gimple_opt_pass
7575 pass_build_ealias (gcc::context
*ctxt
)
7576 : gimple_opt_pass (pass_data_build_ealias
, ctxt
)
7579 /* opt_pass methods: */
7580 virtual bool gate (function
*) { return flag_tree_pta
; }
7582 }; // class pass_build_ealias
7587 make_pass_build_ealias (gcc::context
*ctxt
)
7589 return new pass_build_ealias (ctxt
);
7593 /* IPA PTA solutions for ESCAPED. */
7594 struct pt_solution ipa_escaped_pt
7595 = { true, false, false, false, false, false, false, false, false, NULL
};
7597 /* Associate node with varinfo DATA. Worker for
7598 cgraph_for_symbol_thunks_and_aliases. */
7600 associate_varinfo_to_alias (struct cgraph_node
*node
, void *data
)
7602 if ((node
->alias
|| node
->thunk
.thunk_p
)
7604 insert_vi_for_tree (node
->decl
, (varinfo_t
)data
);
7608 /* Dump varinfo VI to FILE. */
7611 dump_varinfo (FILE *file
, varinfo_t vi
)
7616 fprintf (file
, "%u: %s\n", vi
->id
, vi
->name
);
7618 const char *sep
= " ";
7619 if (vi
->is_artificial_var
)
7620 fprintf (file
, "%sartificial", sep
);
7621 if (vi
->is_special_var
)
7622 fprintf (file
, "%sspecial", sep
);
7623 if (vi
->is_unknown_size_var
)
7624 fprintf (file
, "%sunknown-size", sep
);
7625 if (vi
->is_full_var
)
7626 fprintf (file
, "%sfull", sep
);
7627 if (vi
->is_heap_var
)
7628 fprintf (file
, "%sheap", sep
);
7629 if (vi
->may_have_pointers
)
7630 fprintf (file
, "%smay-have-pointers", sep
);
7631 if (vi
->only_restrict_pointers
)
7632 fprintf (file
, "%sonly-restrict-pointers", sep
);
7633 if (vi
->is_restrict_var
)
7634 fprintf (file
, "%sis-restrict-var", sep
);
7635 if (vi
->is_global_var
)
7636 fprintf (file
, "%sglobal", sep
);
7637 if (vi
->is_ipa_escape_point
)
7638 fprintf (file
, "%sipa-escape-point", sep
);
7640 fprintf (file
, "%sfn-info", sep
);
7642 fprintf (file
, "%srestrict-uid:%u", sep
, vi
->ruid
);
7644 fprintf (file
, "%snext:%u", sep
, vi
->next
);
7645 if (vi
->head
!= vi
->id
)
7646 fprintf (file
, "%shead:%u", sep
, vi
->head
);
7648 fprintf (file
, "%soffset:" HOST_WIDE_INT_PRINT_DEC
, sep
, vi
->offset
);
7649 if (vi
->size
!= ~(unsigned HOST_WIDE_INT
)0)
7650 fprintf (file
, "%ssize:" HOST_WIDE_INT_PRINT_DEC
, sep
, vi
->size
);
7651 if (vi
->fullsize
!= ~(unsigned HOST_WIDE_INT
)0
7652 && vi
->fullsize
!= vi
->size
)
7653 fprintf (file
, "%sfullsize:" HOST_WIDE_INT_PRINT_DEC
, sep
,
7655 fprintf (file
, "\n");
7657 if (vi
->solution
&& !bitmap_empty_p (vi
->solution
))
7661 fprintf (file
, " solution: {");
7662 EXECUTE_IF_SET_IN_BITMAP (vi
->solution
, 0, i
, bi
)
7663 fprintf (file
, " %u", i
);
7664 fprintf (file
, " }\n");
7667 if (vi
->oldsolution
&& !bitmap_empty_p (vi
->oldsolution
)
7668 && !bitmap_equal_p (vi
->solution
, vi
->oldsolution
))
7672 fprintf (file
, " oldsolution: {");
7673 EXECUTE_IF_SET_IN_BITMAP (vi
->oldsolution
, 0, i
, bi
)
7674 fprintf (file
, " %u", i
);
7675 fprintf (file
, " }\n");
7679 /* Dump varinfo VI to stderr. */
7682 debug_varinfo (varinfo_t vi
)
7684 dump_varinfo (stderr
, vi
);
7687 /* Dump varmap to FILE. */
7690 dump_varmap (FILE *file
)
7692 if (varmap
.length () == 0)
7695 fprintf (file
, "variables:\n");
7697 for (unsigned int i
= 0; i
< varmap
.length (); ++i
)
7699 varinfo_t vi
= get_varinfo (i
);
7700 dump_varinfo (file
, vi
);
7703 fprintf (file
, "\n");
7706 /* Dump varmap to stderr. */
7711 dump_varmap (stderr
);
7714 /* Compute whether node is refered to non-locally. Worker for
7715 cgraph_for_symbol_thunks_and_aliases. */
7717 refered_from_nonlocal_fn (struct cgraph_node
*node
, void *data
)
7719 bool *nonlocal_p
= (bool *)data
;
7720 *nonlocal_p
|= (node
->used_from_other_partition
7721 || node
->externally_visible
7722 || node
->force_output
);
7726 /* Same for varpool nodes. */
7728 refered_from_nonlocal_var (struct varpool_node
*node
, void *data
)
7730 bool *nonlocal_p
= (bool *)data
;
7731 *nonlocal_p
|= (node
->used_from_other_partition
7732 || node
->externally_visible
7733 || node
->force_output
);
7737 /* Execute the driver for IPA PTA. */
7739 ipa_pta_execute (void)
7741 struct cgraph_node
*node
;
7743 unsigned int from
= 0;
7749 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7751 symtab_node::dump_table (dump_file
);
7752 fprintf (dump_file
, "\n");
7757 fprintf (dump_file
, "Generating generic constraints\n\n");
7758 dump_constraints (dump_file
, from
);
7759 fprintf (dump_file
, "\n");
7760 from
= constraints
.length ();
7763 /* Build the constraints. */
7764 FOR_EACH_DEFINED_FUNCTION (node
)
7767 /* Nodes without a body are not interesting. Especially do not
7768 visit clones at this point for now - we get duplicate decls
7769 there for inline clones at least. */
7770 if (!node
->has_gimple_body_p () || node
->global
.inlined_to
)
7774 gcc_assert (!node
->clone_of
);
7776 /* For externally visible or attribute used annotated functions use
7777 local constraints for their arguments.
7778 For local functions we see all callers and thus do not need initial
7779 constraints for parameters. */
7780 bool nonlocal_p
= (node
->used_from_other_partition
7781 || node
->externally_visible
7782 || node
->force_output
);
7783 node
->call_for_symbol_thunks_and_aliases (refered_from_nonlocal_fn
,
7786 vi
= create_function_info_for (node
->decl
,
7787 alias_get_name (node
->decl
), false,
7790 && from
!= constraints
.length ())
7793 "Generating intial constraints for %s", node
->name ());
7794 if (DECL_ASSEMBLER_NAME_SET_P (node
->decl
))
7795 fprintf (dump_file
, " (%s)",
7797 (DECL_ASSEMBLER_NAME (node
->decl
)));
7798 fprintf (dump_file
, "\n\n");
7799 dump_constraints (dump_file
, from
);
7800 fprintf (dump_file
, "\n");
7802 from
= constraints
.length ();
7805 node
->call_for_symbol_thunks_and_aliases
7806 (associate_varinfo_to_alias
, vi
, true);
7809 /* Create constraints for global variables and their initializers. */
7810 FOR_EACH_VARIABLE (var
)
7812 if (var
->alias
&& var
->analyzed
)
7815 varinfo_t vi
= get_vi_for_tree (var
->decl
);
7817 /* For the purpose of IPA PTA unit-local globals are not
7819 bool nonlocal_p
= (var
->used_from_other_partition
7820 || var
->externally_visible
7821 || var
->force_output
);
7822 var
->call_for_symbol_and_aliases (refered_from_nonlocal_var
,
7825 vi
->is_ipa_escape_point
= true;
7829 && from
!= constraints
.length ())
7832 "Generating constraints for global initializers\n\n");
7833 dump_constraints (dump_file
, from
);
7834 fprintf (dump_file
, "\n");
7835 from
= constraints
.length ();
7838 FOR_EACH_DEFINED_FUNCTION (node
)
7840 struct function
*func
;
7843 /* Nodes without a body are not interesting. */
7844 if (!node
->has_gimple_body_p () || node
->clone_of
)
7850 "Generating constraints for %s", node
->name ());
7851 if (DECL_ASSEMBLER_NAME_SET_P (node
->decl
))
7852 fprintf (dump_file
, " (%s)",
7854 (DECL_ASSEMBLER_NAME (node
->decl
)));
7855 fprintf (dump_file
, "\n");
7858 func
= DECL_STRUCT_FUNCTION (node
->decl
);
7859 gcc_assert (cfun
== NULL
);
7861 /* Build constriants for the function body. */
7862 FOR_EACH_BB_FN (bb
, func
)
7864 for (gphi_iterator gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
);
7867 gphi
*phi
= gsi
.phi ();
7869 if (! virtual_operand_p (gimple_phi_result (phi
)))
7870 find_func_aliases (func
, phi
);
7873 for (gimple_stmt_iterator gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
);
7876 gimple
*stmt
= gsi_stmt (gsi
);
7878 find_func_aliases (func
, stmt
);
7879 find_func_clobbers (func
, stmt
);
7885 fprintf (dump_file
, "\n");
7886 dump_constraints (dump_file
, from
);
7887 fprintf (dump_file
, "\n");
7888 from
= constraints
.length ();
7892 /* From the constraints compute the points-to sets. */
7893 solve_constraints ();
7895 /* Compute the global points-to sets for ESCAPED.
7896 ??? Note that the computed escape set is not correct
7897 for the whole unit as we fail to consider graph edges to
7898 externally visible functions. */
7899 ipa_escaped_pt
= find_what_var_points_to (NULL
, get_varinfo (escaped_id
));
7901 /* Make sure the ESCAPED solution (which is used as placeholder in
7902 other solutions) does not reference itself. This simplifies
7903 points-to solution queries. */
7904 ipa_escaped_pt
.ipa_escaped
= 0;
7906 /* Assign the points-to sets to the SSA names in the unit. */
7907 FOR_EACH_DEFINED_FUNCTION (node
)
7910 struct function
*fn
;
7914 /* Nodes without a body are not interesting. */
7915 if (!node
->has_gimple_body_p () || node
->clone_of
)
7918 fn
= DECL_STRUCT_FUNCTION (node
->decl
);
7920 /* Compute the points-to sets for pointer SSA_NAMEs. */
7921 FOR_EACH_VEC_ELT (*fn
->gimple_df
->ssa_names
, i
, ptr
)
7924 && POINTER_TYPE_P (TREE_TYPE (ptr
)))
7925 find_what_p_points_to (node
->decl
, ptr
);
7928 /* Compute the call-use and call-clobber sets for indirect calls
7929 and calls to external functions. */
7930 FOR_EACH_BB_FN (bb
, fn
)
7932 gimple_stmt_iterator gsi
;
7934 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
7937 struct pt_solution
*pt
;
7941 stmt
= dyn_cast
<gcall
*> (gsi_stmt (gsi
));
7945 /* Handle direct calls to functions with body. */
7946 decl
= gimple_call_fndecl (stmt
);
7949 tree called_decl
= NULL_TREE
;
7950 if (gimple_call_builtin_p (stmt
, BUILT_IN_GOMP_PARALLEL
))
7951 called_decl
= TREE_OPERAND (gimple_call_arg (stmt
, 0), 0);
7952 else if (gimple_call_builtin_p (stmt
, BUILT_IN_GOACC_PARALLEL
))
7953 called_decl
= TREE_OPERAND (gimple_call_arg (stmt
, 1), 0);
7955 if (called_decl
!= NULL_TREE
7956 && !fndecl_maybe_in_other_partition (called_decl
))
7961 && (fi
= lookup_vi_for_tree (decl
))
7964 *gimple_call_clobber_set (stmt
)
7965 = find_what_var_points_to
7966 (node
->decl
, first_vi_for_offset (fi
, fi_clobbers
));
7967 *gimple_call_use_set (stmt
)
7968 = find_what_var_points_to
7969 (node
->decl
, first_vi_for_offset (fi
, fi_uses
));
7971 /* Handle direct calls to external functions. */
7974 pt
= gimple_call_use_set (stmt
);
7975 if (gimple_call_flags (stmt
) & ECF_CONST
)
7976 memset (pt
, 0, sizeof (struct pt_solution
));
7977 else if ((vi
= lookup_call_use_vi (stmt
)) != NULL
)
7979 *pt
= find_what_var_points_to (node
->decl
, vi
);
7980 /* Escaped (and thus nonlocal) variables are always
7981 implicitly used by calls. */
7982 /* ??? ESCAPED can be empty even though NONLOCAL
7985 pt
->ipa_escaped
= 1;
7989 /* If there is nothing special about this call then
7990 we have made everything that is used also escape. */
7991 *pt
= ipa_escaped_pt
;
7995 pt
= gimple_call_clobber_set (stmt
);
7996 if (gimple_call_flags (stmt
) & (ECF_CONST
|ECF_PURE
|ECF_NOVOPS
))
7997 memset (pt
, 0, sizeof (struct pt_solution
));
7998 else if ((vi
= lookup_call_clobber_vi (stmt
)) != NULL
)
8000 *pt
= find_what_var_points_to (node
->decl
, vi
);
8001 /* Escaped (and thus nonlocal) variables are always
8002 implicitly clobbered by calls. */
8003 /* ??? ESCAPED can be empty even though NONLOCAL
8006 pt
->ipa_escaped
= 1;
8010 /* If there is nothing special about this call then
8011 we have made everything that is used also escape. */
8012 *pt
= ipa_escaped_pt
;
8016 /* Handle indirect calls. */
8018 && (fi
= get_fi_for_callee (stmt
)))
8020 /* We need to accumulate all clobbers/uses of all possible
8022 fi
= get_varinfo (find (fi
->id
));
8023 /* If we cannot constrain the set of functions we'll end up
8024 calling we end up using/clobbering everything. */
8025 if (bitmap_bit_p (fi
->solution
, anything_id
)
8026 || bitmap_bit_p (fi
->solution
, nonlocal_id
)
8027 || bitmap_bit_p (fi
->solution
, escaped_id
))
8029 pt_solution_reset (gimple_call_clobber_set (stmt
));
8030 pt_solution_reset (gimple_call_use_set (stmt
));
8036 struct pt_solution
*uses
, *clobbers
;
8038 uses
= gimple_call_use_set (stmt
);
8039 clobbers
= gimple_call_clobber_set (stmt
);
8040 memset (uses
, 0, sizeof (struct pt_solution
));
8041 memset (clobbers
, 0, sizeof (struct pt_solution
));
8042 EXECUTE_IF_SET_IN_BITMAP (fi
->solution
, 0, i
, bi
)
8044 struct pt_solution sol
;
8046 vi
= get_varinfo (i
);
8047 if (!vi
->is_fn_info
)
8049 /* ??? We could be more precise here? */
8051 uses
->ipa_escaped
= 1;
8052 clobbers
->nonlocal
= 1;
8053 clobbers
->ipa_escaped
= 1;
8057 if (!uses
->anything
)
8059 sol
= find_what_var_points_to
8061 first_vi_for_offset (vi
, fi_uses
));
8062 pt_solution_ior_into (uses
, &sol
);
8064 if (!clobbers
->anything
)
8066 sol
= find_what_var_points_to
8068 first_vi_for_offset (vi
, fi_clobbers
));
8069 pt_solution_ior_into (clobbers
, &sol
);
8077 fn
->gimple_df
->ipa_pta
= true;
8079 /* We have to re-set the final-solution cache after each function
8080 because what is a "global" is dependent on function context. */
8081 final_solutions
->empty ();
8082 obstack_free (&final_solutions_obstack
, NULL
);
8083 gcc_obstack_init (&final_solutions_obstack
);
8086 delete_points_to_sets ();
8095 const pass_data pass_data_ipa_pta
=
8097 SIMPLE_IPA_PASS
, /* type */
8099 OPTGROUP_NONE
, /* optinfo_flags */
8100 TV_IPA_PTA
, /* tv_id */
8101 0, /* properties_required */
8102 0, /* properties_provided */
8103 0, /* properties_destroyed */
8104 0, /* todo_flags_start */
8105 0, /* todo_flags_finish */
8108 class pass_ipa_pta
: public simple_ipa_opt_pass
8111 pass_ipa_pta (gcc::context
*ctxt
)
8112 : simple_ipa_opt_pass (pass_data_ipa_pta
, ctxt
)
8115 /* opt_pass methods: */
8116 virtual bool gate (function
*)
8120 /* Don't bother doing anything if the program has errors. */
8124 opt_pass
* clone () { return new pass_ipa_pta (m_ctxt
); }
8126 virtual unsigned int execute (function
*) { return ipa_pta_execute (); }
8128 }; // class pass_ipa_pta
8132 simple_ipa_opt_pass
*
8133 make_pass_ipa_pta (gcc::context
*ctxt
)
8135 return new pass_ipa_pta (ctxt
);