1 /* Tree based points-to analysis
2 Copyright (C) 2005-2017 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 #include "stringpool.h"
46 /* The idea behind this analyzer is to generate set constraints from the
47 program, then solve the resulting constraints in order to generate the
50 Set constraints are a way of modeling program analysis problems that
51 involve sets. They consist of an inclusion constraint language,
52 describing the variables (each variable is a set) and operations that
53 are involved on the variables, and a set of rules that derive facts
54 from these operations. To solve a system of set constraints, you derive
55 all possible facts under the rules, which gives you the correct sets
58 See "Efficient Field-sensitive pointer analysis for C" by "David
59 J. Pearce and Paul H. J. Kelly and Chris Hankin, at
60 http://citeseer.ist.psu.edu/pearce04efficient.html
62 Also see "Ultra-fast Aliasing Analysis using CLA: A Million Lines
63 of C Code in a Second" by ""Nevin Heintze and Olivier Tardieu" at
64 http://citeseer.ist.psu.edu/heintze01ultrafast.html
66 There are three types of real constraint expressions, DEREF,
67 ADDRESSOF, and SCALAR. Each constraint expression consists
68 of a constraint type, a variable, and an offset.
70 SCALAR is a constraint expression type used to represent x, whether
71 it appears on the LHS or the RHS of a statement.
72 DEREF is a constraint expression type used to represent *x, whether
73 it appears on the LHS or the RHS of a statement.
74 ADDRESSOF is a constraint expression used to represent &x, whether
75 it appears on the LHS or the RHS of a statement.
77 Each pointer variable in the program is assigned an integer id, and
78 each field of a structure variable is assigned an integer id as well.
80 Structure variables are linked to their list of fields through a "next
81 field" in each variable that points to the next field in offset
83 Each variable for a structure field has
85 1. "size", that tells the size in bits of that field.
86 2. "fullsize, that tells the size in bits of the entire structure.
87 3. "offset", that tells the offset in bits from the beginning of the
88 structure to this field.
100 foo.a -> id 1, size 32, offset 0, fullsize 64, next foo.b
101 foo.b -> id 2, size 32, offset 32, fullsize 64, next NULL
102 bar -> id 3, size 32, offset 0, fullsize 32, next NULL
105 In order to solve the system of set constraints, the following is
108 1. Each constraint variable x has a solution set associated with it,
111 2. Constraints are separated into direct, copy, and complex.
112 Direct constraints are ADDRESSOF constraints that require no extra
113 processing, such as P = &Q
114 Copy constraints are those of the form P = Q.
115 Complex constraints are all the constraints involving dereferences
116 and offsets (including offsetted copies).
118 3. All direct constraints of the form P = &Q are processed, such
119 that Q is added to Sol(P)
121 4. All complex constraints for a given constraint variable are stored in a
122 linked list attached to that variable's node.
124 5. A directed graph is built out of the copy constraints. Each
125 constraint variable is a node in the graph, and an edge from
126 Q to P is added for each copy constraint of the form P = Q
128 6. The graph is then walked, and solution sets are
129 propagated along the copy edges, such that an edge from Q to P
130 causes Sol(P) <- Sol(P) union Sol(Q).
132 7. As we visit each node, all complex constraints associated with
133 that node are processed by adding appropriate copy edges to the graph, or the
134 appropriate variables to the solution set.
136 8. The process of walking the graph is iterated until no solution
139 Prior to walking the graph in steps 6 and 7, We perform static
140 cycle elimination on the constraint graph, as well
141 as off-line variable substitution.
143 TODO: Adding offsets to pointer-to-structures can be handled (IE not punted
144 on and turned into anything), but isn't. You can just see what offset
145 inside the pointed-to struct it's going to access.
147 TODO: Constant bounded arrays can be handled as if they were structs of the
148 same number of elements.
150 TODO: Modeling heap and incoming pointers becomes much better if we
151 add fields to them as we discover them, which we could do.
153 TODO: We could handle unions, but to be honest, it's probably not
154 worth the pain or slowdown. */
156 /* IPA-PTA optimizations possible.
158 When the indirect function called is ANYTHING we can add disambiguation
159 based on the function signatures (or simply the parameter count which
160 is the varinfo size). We also do not need to consider functions that
161 do not have their address taken.
163 The is_global_var bit which marks escape points is overly conservative
164 in IPA mode. Split it to is_escape_point and is_global_var - only
165 externally visible globals are escape points in IPA mode.
166 There is now is_ipa_escape_point but this is only used in a few
169 The way we introduce DECL_PT_UID to avoid fixing up all points-to
170 sets in the translation unit when we copy a DECL during inlining
171 pessimizes precision. The advantage is that the DECL_PT_UID keeps
172 compile-time and memory usage overhead low - the points-to sets
173 do not grow or get unshared as they would during a fixup phase.
174 An alternative solution is to delay IPA PTA until after all
175 inlining transformations have been applied.
177 The way we propagate clobber/use information isn't optimized.
178 It should use a new complex constraint that properly filters
179 out local variables of the callee (though that would make
180 the sets invalid after inlining). OTOH we might as well
181 admit defeat to WHOPR and simply do all the clobber/use analysis
182 and propagation after PTA finished but before we threw away
183 points-to information for memory variables. WHOPR and PTA
184 do not play along well anyway - the whole constraint solving
185 would need to be done in WPA phase and it will be very interesting
186 to apply the results to local SSA names during LTRANS phase.
188 We probably should compute a per-function unit-ESCAPE solution
189 propagating it simply like the clobber / uses solutions. The
190 solution can go alongside the non-IPA espaced solution and be
191 used to query which vars escape the unit through a function.
192 This is also required to make the escaped-HEAP trick work in IPA mode.
194 We never put function decls in points-to sets so we do not
195 keep the set of called functions for indirect calls.
197 And probably more. */
199 static bool use_field_sensitive
= true;
200 static int in_ipa_mode
= 0;
202 /* Used for predecessor bitmaps. */
203 static bitmap_obstack predbitmap_obstack
;
205 /* Used for points-to sets. */
206 static bitmap_obstack pta_obstack
;
208 /* Used for oldsolution members of variables. */
209 static bitmap_obstack oldpta_obstack
;
211 /* Used for per-solver-iteration bitmaps. */
212 static bitmap_obstack iteration_obstack
;
214 static unsigned int create_variable_info_for (tree
, const char *, bool);
215 typedef struct constraint_graph
*constraint_graph_t
;
216 static void unify_nodes (constraint_graph_t
, unsigned int, unsigned int, bool);
219 typedef struct constraint
*constraint_t
;
222 #define EXECUTE_IF_IN_NONNULL_BITMAP(a, b, c, d) \
224 EXECUTE_IF_SET_IN_BITMAP (a, b, c, d)
226 static struct constraint_stats
228 unsigned int total_vars
;
229 unsigned int nonpointer_vars
;
230 unsigned int unified_vars_static
;
231 unsigned int unified_vars_dynamic
;
232 unsigned int iterations
;
233 unsigned int num_edges
;
234 unsigned int num_implicit_edges
;
235 unsigned int points_to_sets_created
;
240 /* ID of this variable */
243 /* True if this is a variable created by the constraint analysis, such as
244 heap variables and constraints we had to break up. */
245 unsigned int is_artificial_var
: 1;
247 /* True if this is a special variable whose solution set should not be
249 unsigned int is_special_var
: 1;
251 /* True for variables whose size is not known or variable. */
252 unsigned int is_unknown_size_var
: 1;
254 /* True for (sub-)fields that represent a whole variable. */
255 unsigned int is_full_var
: 1;
257 /* True if this is a heap variable. */
258 unsigned int is_heap_var
: 1;
260 /* True if this is a register variable. */
261 unsigned int is_reg_var
: 1;
263 /* True if this field may contain pointers. */
264 unsigned int may_have_pointers
: 1;
266 /* True if this field has only restrict qualified pointers. */
267 unsigned int only_restrict_pointers
: 1;
269 /* True if this represents a heap var created for a restrict qualified
271 unsigned int is_restrict_var
: 1;
273 /* True if this represents a global variable. */
274 unsigned int is_global_var
: 1;
276 /* True if this represents a module escape point for IPA analysis. */
277 unsigned int is_ipa_escape_point
: 1;
279 /* True if this represents a IPA function info. */
280 unsigned int is_fn_info
: 1;
282 /* ??? Store somewhere better. */
285 /* The ID of the variable for the next field in this structure
286 or zero for the last field in this structure. */
289 /* The ID of the variable for the first field in this structure. */
292 /* Offset of this variable, in bits, from the base variable */
293 unsigned HOST_WIDE_INT offset
;
295 /* Size of the variable, in bits. */
296 unsigned HOST_WIDE_INT size
;
298 /* Full size of the base variable, in bits. */
299 unsigned HOST_WIDE_INT fullsize
;
301 /* Name of this variable */
304 /* Tree that this variable is associated with. */
307 /* Points-to set for this variable. */
310 /* Old points-to set for this variable. */
313 typedef struct variable_info
*varinfo_t
;
315 static varinfo_t
first_vi_for_offset (varinfo_t
, unsigned HOST_WIDE_INT
);
316 static varinfo_t
first_or_preceding_vi_for_offset (varinfo_t
,
317 unsigned HOST_WIDE_INT
);
318 static varinfo_t
lookup_vi_for_tree (tree
);
319 static inline bool type_can_have_subvars (const_tree
);
320 static void make_param_constraints (varinfo_t
);
322 /* Pool of variable info structures. */
323 static object_allocator
<variable_info
> variable_info_pool
324 ("Variable info pool");
326 /* Map varinfo to final pt_solution. */
327 static hash_map
<varinfo_t
, pt_solution
*> *final_solutions
;
328 struct obstack final_solutions_obstack
;
330 /* Table of variable info structures for constraint variables.
331 Indexed directly by variable info id. */
332 static vec
<varinfo_t
> varmap
;
334 /* Return the varmap element N */
336 static inline varinfo_t
337 get_varinfo (unsigned int n
)
342 /* Return the next variable in the list of sub-variables of VI
343 or NULL if VI is the last sub-variable. */
345 static inline varinfo_t
346 vi_next (varinfo_t vi
)
348 return get_varinfo (vi
->next
);
351 /* Static IDs for the special variables. Variable ID zero is unused
352 and used as terminator for the sub-variable chain. */
353 enum { nothing_id
= 1, anything_id
= 2, string_id
= 3,
354 escaped_id
= 4, nonlocal_id
= 5,
355 storedanything_id
= 6, integer_id
= 7 };
357 /* Return a new variable info structure consisting for a variable
358 named NAME, and using constraint graph node NODE. Append it
359 to the vector of variable info structures. */
362 new_var_info (tree t
, const char *name
, bool add_id
)
364 unsigned index
= varmap
.length ();
365 varinfo_t ret
= variable_info_pool
.allocate ();
367 if (dump_file
&& add_id
)
369 char *tempname
= xasprintf ("%s(%d)", name
, index
);
370 name
= ggc_strdup (tempname
);
377 /* Vars without decl are artificial and do not have sub-variables. */
378 ret
->is_artificial_var
= (t
== NULL_TREE
);
379 ret
->is_special_var
= false;
380 ret
->is_unknown_size_var
= false;
381 ret
->is_full_var
= (t
== NULL_TREE
);
382 ret
->is_heap_var
= false;
383 ret
->may_have_pointers
= true;
384 ret
->only_restrict_pointers
= false;
385 ret
->is_restrict_var
= false;
387 ret
->is_global_var
= (t
== NULL_TREE
);
388 ret
->is_ipa_escape_point
= false;
389 ret
->is_fn_info
= false;
391 ret
->is_global_var
= (is_global_var (t
)
392 /* We have to treat even local register variables
394 || (VAR_P (t
) && DECL_HARD_REGISTER (t
)));
395 ret
->is_reg_var
= (t
&& TREE_CODE (t
) == SSA_NAME
);
396 ret
->solution
= BITMAP_ALLOC (&pta_obstack
);
397 ret
->oldsolution
= NULL
;
403 varmap
.safe_push (ret
);
408 /* A map mapping call statements to per-stmt variables for uses
409 and clobbers specific to the call. */
410 static hash_map
<gimple
*, varinfo_t
> *call_stmt_vars
;
412 /* Lookup or create the variable for the call statement CALL. */
415 get_call_vi (gcall
*call
)
420 varinfo_t
*slot_p
= &call_stmt_vars
->get_or_insert (call
, &existed
);
424 vi
= new_var_info (NULL_TREE
, "CALLUSED", true);
428 vi
->is_full_var
= true;
429 vi
->is_reg_var
= true;
431 vi2
= new_var_info (NULL_TREE
, "CALLCLOBBERED", true);
435 vi2
->is_full_var
= true;
436 vi2
->is_reg_var
= true;
444 /* Lookup the variable for the call statement CALL representing
445 the uses. Returns NULL if there is nothing special about this call. */
448 lookup_call_use_vi (gcall
*call
)
450 varinfo_t
*slot_p
= call_stmt_vars
->get (call
);
457 /* Lookup the variable for the call statement CALL representing
458 the clobbers. Returns NULL if there is nothing special about this call. */
461 lookup_call_clobber_vi (gcall
*call
)
463 varinfo_t uses
= lookup_call_use_vi (call
);
467 return vi_next (uses
);
470 /* Lookup or create the variable for the call statement CALL representing
474 get_call_use_vi (gcall
*call
)
476 return get_call_vi (call
);
479 /* Lookup or create the variable for the call statement CALL representing
482 static varinfo_t ATTRIBUTE_UNUSED
483 get_call_clobber_vi (gcall
*call
)
485 return vi_next (get_call_vi (call
));
489 enum constraint_expr_type
{SCALAR
, DEREF
, ADDRESSOF
};
491 /* An expression that appears in a constraint. */
493 struct constraint_expr
495 /* Constraint type. */
496 constraint_expr_type type
;
498 /* Variable we are referring to in the constraint. */
501 /* Offset, in bits, of this constraint from the beginning of
502 variables it ends up referring to.
504 IOW, in a deref constraint, we would deref, get the result set,
505 then add OFFSET to each member. */
506 HOST_WIDE_INT offset
;
509 /* Use 0x8000... as special unknown offset. */
510 #define UNKNOWN_OFFSET HOST_WIDE_INT_MIN
512 typedef struct constraint_expr ce_s
;
513 static void get_constraint_for_1 (tree
, vec
<ce_s
> *, bool, bool);
514 static void get_constraint_for (tree
, vec
<ce_s
> *);
515 static void get_constraint_for_rhs (tree
, vec
<ce_s
> *);
516 static void do_deref (vec
<ce_s
> *);
518 /* Our set constraints are made up of two constraint expressions, one
521 As described in the introduction, our set constraints each represent an
522 operation between set valued variables.
526 struct constraint_expr lhs
;
527 struct constraint_expr rhs
;
530 /* List of constraints that we use to build the constraint graph from. */
532 static vec
<constraint_t
> constraints
;
533 static object_allocator
<constraint
> constraint_pool ("Constraint pool");
535 /* The constraint graph is represented as an array of bitmaps
536 containing successor nodes. */
538 struct constraint_graph
540 /* Size of this graph, which may be different than the number of
541 nodes in the variable map. */
544 /* Explicit successors of each node. */
547 /* Implicit predecessors of each node (Used for variable
549 bitmap
*implicit_preds
;
551 /* Explicit predecessors of each node (Used for variable substitution). */
554 /* Indirect cycle representatives, or -1 if the node has no indirect
556 int *indirect_cycles
;
558 /* Representative node for a node. rep[a] == a unless the node has
562 /* Equivalence class representative for a label. This is used for
563 variable substitution. */
566 /* Pointer equivalence label for a node. All nodes with the same
567 pointer equivalence label can be unified together at some point
568 (either during constraint optimization or after the constraint
572 /* Pointer equivalence representative for a label. This is used to
573 handle nodes that are pointer equivalent but not location
574 equivalent. We can unite these once the addressof constraints
575 are transformed into initial points-to sets. */
578 /* Pointer equivalence label for each node, used during variable
580 unsigned int *pointer_label
;
582 /* Location equivalence label for each node, used during location
583 equivalence finding. */
584 unsigned int *loc_label
;
586 /* Pointed-by set for each node, used during location equivalence
587 finding. This is pointed-by rather than pointed-to, because it
588 is constructed using the predecessor graph. */
591 /* Points to sets for pointer equivalence. This is *not* the actual
592 points-to sets for nodes. */
595 /* Bitmap of nodes where the bit is set if the node is a direct
596 node. Used for variable substitution. */
597 sbitmap direct_nodes
;
599 /* Bitmap of nodes where the bit is set if the node is address
600 taken. Used for variable substitution. */
601 bitmap address_taken
;
603 /* Vector of complex constraints for each graph node. Complex
604 constraints are those involving dereferences or offsets that are
606 vec
<constraint_t
> *complex;
609 static constraint_graph_t graph
;
611 /* During variable substitution and the offline version of indirect
612 cycle finding, we create nodes to represent dereferences and
613 address taken constraints. These represent where these start and
615 #define FIRST_REF_NODE (varmap).length ()
616 #define LAST_REF_NODE (FIRST_REF_NODE + (FIRST_REF_NODE - 1))
618 /* Return the representative node for NODE, if NODE has been unioned
620 This function performs path compression along the way to finding
621 the representative. */
624 find (unsigned int node
)
626 gcc_checking_assert (node
< graph
->size
);
627 if (graph
->rep
[node
] != node
)
628 return graph
->rep
[node
] = find (graph
->rep
[node
]);
632 /* Union the TO and FROM nodes to the TO nodes.
633 Note that at some point in the future, we may want to do
634 union-by-rank, in which case we are going to have to return the
635 node we unified to. */
638 unite (unsigned int to
, unsigned int from
)
640 gcc_checking_assert (to
< graph
->size
&& from
< graph
->size
);
641 if (to
!= from
&& graph
->rep
[from
] != to
)
643 graph
->rep
[from
] = to
;
649 /* Create a new constraint consisting of LHS and RHS expressions. */
652 new_constraint (const struct constraint_expr lhs
,
653 const struct constraint_expr rhs
)
655 constraint_t ret
= constraint_pool
.allocate ();
661 /* Print out constraint C to FILE. */
664 dump_constraint (FILE *file
, constraint_t c
)
666 if (c
->lhs
.type
== ADDRESSOF
)
668 else if (c
->lhs
.type
== DEREF
)
670 fprintf (file
, "%s", get_varinfo (c
->lhs
.var
)->name
);
671 if (c
->lhs
.offset
== UNKNOWN_OFFSET
)
672 fprintf (file
, " + UNKNOWN");
673 else if (c
->lhs
.offset
!= 0)
674 fprintf (file
, " + " HOST_WIDE_INT_PRINT_DEC
, c
->lhs
.offset
);
675 fprintf (file
, " = ");
676 if (c
->rhs
.type
== ADDRESSOF
)
678 else if (c
->rhs
.type
== DEREF
)
680 fprintf (file
, "%s", get_varinfo (c
->rhs
.var
)->name
);
681 if (c
->rhs
.offset
== UNKNOWN_OFFSET
)
682 fprintf (file
, " + UNKNOWN");
683 else if (c
->rhs
.offset
!= 0)
684 fprintf (file
, " + " HOST_WIDE_INT_PRINT_DEC
, c
->rhs
.offset
);
688 void debug_constraint (constraint_t
);
689 void debug_constraints (void);
690 void debug_constraint_graph (void);
691 void debug_solution_for_var (unsigned int);
692 void debug_sa_points_to_info (void);
693 void debug_varinfo (varinfo_t
);
694 void debug_varmap (void);
696 /* Print out constraint C to stderr. */
699 debug_constraint (constraint_t c
)
701 dump_constraint (stderr
, c
);
702 fprintf (stderr
, "\n");
705 /* Print out all constraints to FILE */
708 dump_constraints (FILE *file
, int from
)
712 for (i
= from
; constraints
.iterate (i
, &c
); i
++)
715 dump_constraint (file
, c
);
716 fprintf (file
, "\n");
720 /* Print out all constraints to stderr. */
723 debug_constraints (void)
725 dump_constraints (stderr
, 0);
728 /* Print the constraint graph in dot format. */
731 dump_constraint_graph (FILE *file
)
735 /* Only print the graph if it has already been initialized: */
739 /* Prints the header of the dot file: */
740 fprintf (file
, "strict digraph {\n");
741 fprintf (file
, " node [\n shape = box\n ]\n");
742 fprintf (file
, " edge [\n fontsize = \"12\"\n ]\n");
743 fprintf (file
, "\n // List of nodes and complex constraints in "
744 "the constraint graph:\n");
746 /* The next lines print the nodes in the graph together with the
747 complex constraints attached to them. */
748 for (i
= 1; i
< graph
->size
; i
++)
750 if (i
== FIRST_REF_NODE
)
754 if (i
< FIRST_REF_NODE
)
755 fprintf (file
, "\"%s\"", get_varinfo (i
)->name
);
757 fprintf (file
, "\"*%s\"", get_varinfo (i
- FIRST_REF_NODE
)->name
);
758 if (graph
->complex[i
].exists ())
762 fprintf (file
, " [label=\"\\N\\n");
763 for (j
= 0; graph
->complex[i
].iterate (j
, &c
); ++j
)
765 dump_constraint (file
, c
);
766 fprintf (file
, "\\l");
768 fprintf (file
, "\"]");
770 fprintf (file
, ";\n");
773 /* Go over the edges. */
774 fprintf (file
, "\n // Edges in the constraint graph:\n");
775 for (i
= 1; i
< graph
->size
; i
++)
781 EXECUTE_IF_IN_NONNULL_BITMAP (graph
->succs
[i
], 0, j
, bi
)
783 unsigned to
= find (j
);
786 if (i
< FIRST_REF_NODE
)
787 fprintf (file
, "\"%s\"", get_varinfo (i
)->name
);
789 fprintf (file
, "\"*%s\"", get_varinfo (i
- FIRST_REF_NODE
)->name
);
790 fprintf (file
, " -> ");
791 if (to
< FIRST_REF_NODE
)
792 fprintf (file
, "\"%s\"", get_varinfo (to
)->name
);
794 fprintf (file
, "\"*%s\"", get_varinfo (to
- FIRST_REF_NODE
)->name
);
795 fprintf (file
, ";\n");
799 /* Prints the tail of the dot file. */
800 fprintf (file
, "}\n");
803 /* Print out the constraint graph to stderr. */
806 debug_constraint_graph (void)
808 dump_constraint_graph (stderr
);
813 The solver is a simple worklist solver, that works on the following
816 sbitmap changed_nodes = all zeroes;
818 For each node that is not already collapsed:
820 set bit in changed nodes
822 while (changed_count > 0)
824 compute topological ordering for constraint graph
826 find and collapse cycles in the constraint graph (updating
827 changed if necessary)
829 for each node (n) in the graph in topological order:
832 Process each complex constraint associated with the node,
833 updating changed if necessary.
835 For each outgoing edge from n, propagate the solution from n to
836 the destination of the edge, updating changed as necessary.
840 /* Return true if two constraint expressions A and B are equal. */
843 constraint_expr_equal (struct constraint_expr a
, struct constraint_expr b
)
845 return a
.type
== b
.type
&& a
.var
== b
.var
&& a
.offset
== b
.offset
;
848 /* Return true if constraint expression A is less than constraint expression
849 B. This is just arbitrary, but consistent, in order to give them an
853 constraint_expr_less (struct constraint_expr a
, struct constraint_expr b
)
855 if (a
.type
== b
.type
)
858 return a
.offset
< b
.offset
;
860 return a
.var
< b
.var
;
863 return a
.type
< b
.type
;
866 /* Return true if constraint A is less than constraint B. This is just
867 arbitrary, but consistent, in order to give them an ordering. */
870 constraint_less (const constraint_t
&a
, const constraint_t
&b
)
872 if (constraint_expr_less (a
->lhs
, b
->lhs
))
874 else if (constraint_expr_less (b
->lhs
, a
->lhs
))
877 return constraint_expr_less (a
->rhs
, b
->rhs
);
880 /* Return true if two constraints A and B are equal. */
883 constraint_equal (struct constraint a
, struct constraint b
)
885 return constraint_expr_equal (a
.lhs
, b
.lhs
)
886 && constraint_expr_equal (a
.rhs
, b
.rhs
);
890 /* Find a constraint LOOKFOR in the sorted constraint vector VEC */
893 constraint_vec_find (vec
<constraint_t
> vec
,
894 struct constraint lookfor
)
902 place
= vec
.lower_bound (&lookfor
, constraint_less
);
903 if (place
>= vec
.length ())
906 if (!constraint_equal (*found
, lookfor
))
911 /* Union two constraint vectors, TO and FROM. Put the result in TO.
912 Returns true of TO set is changed. */
915 constraint_set_union (vec
<constraint_t
> *to
,
916 vec
<constraint_t
> *from
)
920 bool any_change
= false;
922 FOR_EACH_VEC_ELT (*from
, i
, c
)
924 if (constraint_vec_find (*to
, *c
) == NULL
)
926 unsigned int place
= to
->lower_bound (c
, constraint_less
);
927 to
->safe_insert (place
, c
);
934 /* Expands the solution in SET to all sub-fields of variables included. */
937 solution_set_expand (bitmap set
, bitmap
*expanded
)
945 *expanded
= BITMAP_ALLOC (&iteration_obstack
);
947 /* In a first pass expand to the head of the variables we need to
948 add all sub-fields off. This avoids quadratic behavior. */
949 EXECUTE_IF_SET_IN_BITMAP (set
, 0, j
, bi
)
951 varinfo_t v
= get_varinfo (j
);
952 if (v
->is_artificial_var
955 bitmap_set_bit (*expanded
, v
->head
);
958 /* In the second pass now expand all head variables with subfields. */
959 EXECUTE_IF_SET_IN_BITMAP (*expanded
, 0, j
, bi
)
961 varinfo_t v
= get_varinfo (j
);
964 for (v
= vi_next (v
); v
!= NULL
; v
= vi_next (v
))
965 bitmap_set_bit (*expanded
, v
->id
);
968 /* And finally set the rest of the bits from SET. */
969 bitmap_ior_into (*expanded
, set
);
974 /* Union solution sets TO and DELTA, and add INC to each member of DELTA in the
978 set_union_with_increment (bitmap to
, bitmap delta
, HOST_WIDE_INT inc
,
979 bitmap
*expanded_delta
)
981 bool changed
= false;
985 /* If the solution of DELTA contains anything it is good enough to transfer
987 if (bitmap_bit_p (delta
, anything_id
))
988 return bitmap_set_bit (to
, anything_id
);
990 /* If the offset is unknown we have to expand the solution to
992 if (inc
== UNKNOWN_OFFSET
)
994 delta
= solution_set_expand (delta
, expanded_delta
);
995 changed
|= bitmap_ior_into (to
, delta
);
999 /* For non-zero offset union the offsetted solution into the destination. */
1000 EXECUTE_IF_SET_IN_BITMAP (delta
, 0, i
, bi
)
1002 varinfo_t vi
= get_varinfo (i
);
1004 /* If this is a variable with just one field just set its bit
1006 if (vi
->is_artificial_var
1007 || vi
->is_unknown_size_var
1009 changed
|= bitmap_set_bit (to
, i
);
1012 HOST_WIDE_INT fieldoffset
= vi
->offset
+ inc
;
1013 unsigned HOST_WIDE_INT size
= vi
->size
;
1015 /* If the offset makes the pointer point to before the
1016 variable use offset zero for the field lookup. */
1017 if (fieldoffset
< 0)
1018 vi
= get_varinfo (vi
->head
);
1020 vi
= first_or_preceding_vi_for_offset (vi
, fieldoffset
);
1024 changed
|= bitmap_set_bit (to
, vi
->id
);
1029 /* We have to include all fields that overlap the current field
1033 while (vi
->offset
< fieldoffset
+ size
);
1040 /* Insert constraint C into the list of complex constraints for graph
1044 insert_into_complex (constraint_graph_t graph
,
1045 unsigned int var
, constraint_t c
)
1047 vec
<constraint_t
> complex = graph
->complex[var
];
1048 unsigned int place
= complex.lower_bound (c
, constraint_less
);
1050 /* Only insert constraints that do not already exist. */
1051 if (place
>= complex.length ()
1052 || !constraint_equal (*c
, *complex[place
]))
1053 graph
->complex[var
].safe_insert (place
, c
);
1057 /* Condense two variable nodes into a single variable node, by moving
1058 all associated info from FROM to TO. Returns true if TO node's
1059 constraint set changes after the merge. */
1062 merge_node_constraints (constraint_graph_t graph
, unsigned int to
,
1067 bool any_change
= false;
1069 gcc_checking_assert (find (from
) == to
);
1071 /* Move all complex constraints from src node into to node */
1072 FOR_EACH_VEC_ELT (graph
->complex[from
], i
, c
)
1074 /* In complex constraints for node FROM, we may have either
1075 a = *FROM, and *FROM = a, or an offseted constraint which are
1076 always added to the rhs node's constraints. */
1078 if (c
->rhs
.type
== DEREF
)
1080 else if (c
->lhs
.type
== DEREF
)
1086 any_change
= constraint_set_union (&graph
->complex[to
],
1087 &graph
->complex[from
]);
1088 graph
->complex[from
].release ();
1093 /* Remove edges involving NODE from GRAPH. */
1096 clear_edges_for_node (constraint_graph_t graph
, unsigned int node
)
1098 if (graph
->succs
[node
])
1099 BITMAP_FREE (graph
->succs
[node
]);
1102 /* Merge GRAPH nodes FROM and TO into node TO. */
1105 merge_graph_nodes (constraint_graph_t graph
, unsigned int to
,
1108 if (graph
->indirect_cycles
[from
] != -1)
1110 /* If we have indirect cycles with the from node, and we have
1111 none on the to node, the to node has indirect cycles from the
1112 from node now that they are unified.
1113 If indirect cycles exist on both, unify the nodes that they
1114 are in a cycle with, since we know they are in a cycle with
1116 if (graph
->indirect_cycles
[to
] == -1)
1117 graph
->indirect_cycles
[to
] = graph
->indirect_cycles
[from
];
1120 /* Merge all the successor edges. */
1121 if (graph
->succs
[from
])
1123 if (!graph
->succs
[to
])
1124 graph
->succs
[to
] = BITMAP_ALLOC (&pta_obstack
);
1125 bitmap_ior_into (graph
->succs
[to
],
1126 graph
->succs
[from
]);
1129 clear_edges_for_node (graph
, from
);
1133 /* Add an indirect graph edge to GRAPH, going from TO to FROM if
1134 it doesn't exist in the graph already. */
1137 add_implicit_graph_edge (constraint_graph_t graph
, unsigned int to
,
1143 if (!graph
->implicit_preds
[to
])
1144 graph
->implicit_preds
[to
] = BITMAP_ALLOC (&predbitmap_obstack
);
1146 if (bitmap_set_bit (graph
->implicit_preds
[to
], from
))
1147 stats
.num_implicit_edges
++;
1150 /* Add a predecessor graph edge to GRAPH, going from TO to FROM if
1151 it doesn't exist in the graph already.
1152 Return false if the edge already existed, true otherwise. */
1155 add_pred_graph_edge (constraint_graph_t graph
, unsigned int to
,
1158 if (!graph
->preds
[to
])
1159 graph
->preds
[to
] = BITMAP_ALLOC (&predbitmap_obstack
);
1160 bitmap_set_bit (graph
->preds
[to
], from
);
1163 /* Add a graph edge to GRAPH, going from FROM to TO if
1164 it doesn't exist in the graph already.
1165 Return false if the edge already existed, true otherwise. */
1168 add_graph_edge (constraint_graph_t graph
, unsigned int to
,
1179 if (!graph
->succs
[from
])
1180 graph
->succs
[from
] = BITMAP_ALLOC (&pta_obstack
);
1181 if (bitmap_set_bit (graph
->succs
[from
], to
))
1184 if (to
< FIRST_REF_NODE
&& from
< FIRST_REF_NODE
)
1192 /* Initialize the constraint graph structure to contain SIZE nodes. */
1195 init_graph (unsigned int size
)
1199 graph
= XCNEW (struct constraint_graph
);
1201 graph
->succs
= XCNEWVEC (bitmap
, graph
->size
);
1202 graph
->indirect_cycles
= XNEWVEC (int, graph
->size
);
1203 graph
->rep
= XNEWVEC (unsigned int, graph
->size
);
1204 /* ??? Macros do not support template types with multiple arguments,
1205 so we use a typedef to work around it. */
1206 typedef vec
<constraint_t
> vec_constraint_t_heap
;
1207 graph
->complex = XCNEWVEC (vec_constraint_t_heap
, size
);
1208 graph
->pe
= XCNEWVEC (unsigned int, graph
->size
);
1209 graph
->pe_rep
= XNEWVEC (int, graph
->size
);
1211 for (j
= 0; j
< graph
->size
; j
++)
1214 graph
->pe_rep
[j
] = -1;
1215 graph
->indirect_cycles
[j
] = -1;
1219 /* Build the constraint graph, adding only predecessor edges right now. */
1222 build_pred_graph (void)
1228 graph
->implicit_preds
= XCNEWVEC (bitmap
, graph
->size
);
1229 graph
->preds
= XCNEWVEC (bitmap
, graph
->size
);
1230 graph
->pointer_label
= XCNEWVEC (unsigned int, graph
->size
);
1231 graph
->loc_label
= XCNEWVEC (unsigned int, graph
->size
);
1232 graph
->pointed_by
= XCNEWVEC (bitmap
, graph
->size
);
1233 graph
->points_to
= XCNEWVEC (bitmap
, graph
->size
);
1234 graph
->eq_rep
= XNEWVEC (int, graph
->size
);
1235 graph
->direct_nodes
= sbitmap_alloc (graph
->size
);
1236 graph
->address_taken
= BITMAP_ALLOC (&predbitmap_obstack
);
1237 bitmap_clear (graph
->direct_nodes
);
1239 for (j
= 1; j
< FIRST_REF_NODE
; j
++)
1241 if (!get_varinfo (j
)->is_special_var
)
1242 bitmap_set_bit (graph
->direct_nodes
, j
);
1245 for (j
= 0; j
< graph
->size
; j
++)
1246 graph
->eq_rep
[j
] = -1;
1248 for (j
= 0; j
< varmap
.length (); j
++)
1249 graph
->indirect_cycles
[j
] = -1;
1251 FOR_EACH_VEC_ELT (constraints
, i
, c
)
1253 struct constraint_expr lhs
= c
->lhs
;
1254 struct constraint_expr rhs
= c
->rhs
;
1255 unsigned int lhsvar
= lhs
.var
;
1256 unsigned int rhsvar
= rhs
.var
;
1258 if (lhs
.type
== DEREF
)
1261 if (rhs
.offset
== 0 && lhs
.offset
== 0 && rhs
.type
== SCALAR
)
1262 add_pred_graph_edge (graph
, FIRST_REF_NODE
+ lhsvar
, rhsvar
);
1264 else if (rhs
.type
== DEREF
)
1267 if (rhs
.offset
== 0 && lhs
.offset
== 0 && lhs
.type
== SCALAR
)
1268 add_pred_graph_edge (graph
, lhsvar
, FIRST_REF_NODE
+ rhsvar
);
1270 bitmap_clear_bit (graph
->direct_nodes
, lhsvar
);
1272 else if (rhs
.type
== ADDRESSOF
)
1277 if (graph
->points_to
[lhsvar
] == NULL
)
1278 graph
->points_to
[lhsvar
] = BITMAP_ALLOC (&predbitmap_obstack
);
1279 bitmap_set_bit (graph
->points_to
[lhsvar
], rhsvar
);
1281 if (graph
->pointed_by
[rhsvar
] == NULL
)
1282 graph
->pointed_by
[rhsvar
] = BITMAP_ALLOC (&predbitmap_obstack
);
1283 bitmap_set_bit (graph
->pointed_by
[rhsvar
], lhsvar
);
1285 /* Implicitly, *x = y */
1286 add_implicit_graph_edge (graph
, FIRST_REF_NODE
+ lhsvar
, rhsvar
);
1288 /* All related variables are no longer direct nodes. */
1289 bitmap_clear_bit (graph
->direct_nodes
, rhsvar
);
1290 v
= get_varinfo (rhsvar
);
1291 if (!v
->is_full_var
)
1293 v
= get_varinfo (v
->head
);
1296 bitmap_clear_bit (graph
->direct_nodes
, v
->id
);
1301 bitmap_set_bit (graph
->address_taken
, rhsvar
);
1303 else if (lhsvar
> anything_id
1304 && lhsvar
!= rhsvar
&& lhs
.offset
== 0 && rhs
.offset
== 0)
1307 add_pred_graph_edge (graph
, lhsvar
, rhsvar
);
1308 /* Implicitly, *x = *y */
1309 add_implicit_graph_edge (graph
, FIRST_REF_NODE
+ lhsvar
,
1310 FIRST_REF_NODE
+ rhsvar
);
1312 else if (lhs
.offset
!= 0 || rhs
.offset
!= 0)
1314 if (rhs
.offset
!= 0)
1315 bitmap_clear_bit (graph
->direct_nodes
, lhs
.var
);
1316 else if (lhs
.offset
!= 0)
1317 bitmap_clear_bit (graph
->direct_nodes
, rhs
.var
);
1322 /* Build the constraint graph, adding successor edges. */
1325 build_succ_graph (void)
1330 FOR_EACH_VEC_ELT (constraints
, i
, c
)
1332 struct constraint_expr lhs
;
1333 struct constraint_expr rhs
;
1334 unsigned int lhsvar
;
1335 unsigned int rhsvar
;
1342 lhsvar
= find (lhs
.var
);
1343 rhsvar
= find (rhs
.var
);
1345 if (lhs
.type
== DEREF
)
1347 if (rhs
.offset
== 0 && lhs
.offset
== 0 && rhs
.type
== SCALAR
)
1348 add_graph_edge (graph
, FIRST_REF_NODE
+ lhsvar
, rhsvar
);
1350 else if (rhs
.type
== DEREF
)
1352 if (rhs
.offset
== 0 && lhs
.offset
== 0 && lhs
.type
== SCALAR
)
1353 add_graph_edge (graph
, lhsvar
, FIRST_REF_NODE
+ rhsvar
);
1355 else if (rhs
.type
== ADDRESSOF
)
1358 gcc_checking_assert (find (rhs
.var
) == rhs
.var
);
1359 bitmap_set_bit (get_varinfo (lhsvar
)->solution
, rhsvar
);
1361 else if (lhsvar
> anything_id
1362 && lhsvar
!= rhsvar
&& lhs
.offset
== 0 && rhs
.offset
== 0)
1364 add_graph_edge (graph
, lhsvar
, rhsvar
);
1368 /* Add edges from STOREDANYTHING to all non-direct nodes that can
1369 receive pointers. */
1370 t
= find (storedanything_id
);
1371 for (i
= integer_id
+ 1; i
< FIRST_REF_NODE
; ++i
)
1373 if (!bitmap_bit_p (graph
->direct_nodes
, i
)
1374 && get_varinfo (i
)->may_have_pointers
)
1375 add_graph_edge (graph
, find (i
), t
);
1378 /* Everything stored to ANYTHING also potentially escapes. */
1379 add_graph_edge (graph
, find (escaped_id
), t
);
1383 /* Changed variables on the last iteration. */
1384 static bitmap changed
;
1386 /* Strongly Connected Component visitation info. */
1390 scc_info (size_t size
);
1393 auto_sbitmap visited
;
1394 auto_sbitmap deleted
;
1396 unsigned int *node_mapping
;
1398 auto_vec
<unsigned> scc_stack
;
1402 /* Recursive routine to find strongly connected components in GRAPH.
1403 SI is the SCC info to store the information in, and N is the id of current
1404 graph node we are processing.
1406 This is Tarjan's strongly connected component finding algorithm, as
1407 modified by Nuutila to keep only non-root nodes on the stack.
1408 The algorithm can be found in "On finding the strongly connected
1409 connected components in a directed graph" by Esko Nuutila and Eljas
1410 Soisalon-Soininen, in Information Processing Letters volume 49,
1411 number 1, pages 9-14. */
1414 scc_visit (constraint_graph_t graph
, struct scc_info
*si
, unsigned int n
)
1418 unsigned int my_dfs
;
1420 bitmap_set_bit (si
->visited
, n
);
1421 si
->dfs
[n
] = si
->current_index
++;
1422 my_dfs
= si
->dfs
[n
];
1424 /* Visit all the successors. */
1425 EXECUTE_IF_IN_NONNULL_BITMAP (graph
->succs
[n
], 0, i
, bi
)
1429 if (i
> LAST_REF_NODE
)
1433 if (bitmap_bit_p (si
->deleted
, w
))
1436 if (!bitmap_bit_p (si
->visited
, w
))
1437 scc_visit (graph
, si
, w
);
1439 unsigned int t
= find (w
);
1440 gcc_checking_assert (find (n
) == n
);
1441 if (si
->dfs
[t
] < si
->dfs
[n
])
1442 si
->dfs
[n
] = si
->dfs
[t
];
1445 /* See if any components have been identified. */
1446 if (si
->dfs
[n
] == my_dfs
)
1448 if (si
->scc_stack
.length () > 0
1449 && si
->dfs
[si
->scc_stack
.last ()] >= my_dfs
)
1451 bitmap scc
= BITMAP_ALLOC (NULL
);
1452 unsigned int lowest_node
;
1455 bitmap_set_bit (scc
, n
);
1457 while (si
->scc_stack
.length () != 0
1458 && si
->dfs
[si
->scc_stack
.last ()] >= my_dfs
)
1460 unsigned int w
= si
->scc_stack
.pop ();
1462 bitmap_set_bit (scc
, w
);
1465 lowest_node
= bitmap_first_set_bit (scc
);
1466 gcc_assert (lowest_node
< FIRST_REF_NODE
);
1468 /* Collapse the SCC nodes into a single node, and mark the
1470 EXECUTE_IF_SET_IN_BITMAP (scc
, 0, i
, bi
)
1472 if (i
< FIRST_REF_NODE
)
1474 if (unite (lowest_node
, i
))
1475 unify_nodes (graph
, lowest_node
, i
, false);
1479 unite (lowest_node
, i
);
1480 graph
->indirect_cycles
[i
- FIRST_REF_NODE
] = lowest_node
;
1484 bitmap_set_bit (si
->deleted
, n
);
1487 si
->scc_stack
.safe_push (n
);
1490 /* Unify node FROM into node TO, updating the changed count if
1491 necessary when UPDATE_CHANGED is true. */
1494 unify_nodes (constraint_graph_t graph
, unsigned int to
, unsigned int from
,
1495 bool update_changed
)
1497 gcc_checking_assert (to
!= from
&& find (to
) == to
);
1499 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1500 fprintf (dump_file
, "Unifying %s to %s\n",
1501 get_varinfo (from
)->name
,
1502 get_varinfo (to
)->name
);
1505 stats
.unified_vars_dynamic
++;
1507 stats
.unified_vars_static
++;
1509 merge_graph_nodes (graph
, to
, from
);
1510 if (merge_node_constraints (graph
, to
, from
))
1513 bitmap_set_bit (changed
, to
);
1516 /* Mark TO as changed if FROM was changed. If TO was already marked
1517 as changed, decrease the changed count. */
1520 && bitmap_clear_bit (changed
, from
))
1521 bitmap_set_bit (changed
, to
);
1522 varinfo_t fromvi
= get_varinfo (from
);
1523 if (fromvi
->solution
)
1525 /* If the solution changes because of the merging, we need to mark
1526 the variable as changed. */
1527 varinfo_t tovi
= get_varinfo (to
);
1528 if (bitmap_ior_into (tovi
->solution
, fromvi
->solution
))
1531 bitmap_set_bit (changed
, to
);
1534 BITMAP_FREE (fromvi
->solution
);
1535 if (fromvi
->oldsolution
)
1536 BITMAP_FREE (fromvi
->oldsolution
);
1538 if (stats
.iterations
> 0
1539 && tovi
->oldsolution
)
1540 BITMAP_FREE (tovi
->oldsolution
);
1542 if (graph
->succs
[to
])
1543 bitmap_clear_bit (graph
->succs
[to
], to
);
1546 /* Information needed to compute the topological ordering of a graph. */
1550 /* sbitmap of visited nodes. */
1552 /* Array that stores the topological order of the graph, *in
1554 vec
<unsigned> topo_order
;
1558 /* Initialize and return a topological info structure. */
1560 static struct topo_info
*
1561 init_topo_info (void)
1563 size_t size
= graph
->size
;
1564 struct topo_info
*ti
= XNEW (struct topo_info
);
1565 ti
->visited
= sbitmap_alloc (size
);
1566 bitmap_clear (ti
->visited
);
1567 ti
->topo_order
.create (1);
1572 /* Free the topological sort info pointed to by TI. */
1575 free_topo_info (struct topo_info
*ti
)
1577 sbitmap_free (ti
->visited
);
1578 ti
->topo_order
.release ();
1582 /* Visit the graph in topological order, and store the order in the
1583 topo_info structure. */
1586 topo_visit (constraint_graph_t graph
, struct topo_info
*ti
,
1592 bitmap_set_bit (ti
->visited
, n
);
1594 if (graph
->succs
[n
])
1595 EXECUTE_IF_SET_IN_BITMAP (graph
->succs
[n
], 0, j
, bi
)
1597 if (!bitmap_bit_p (ti
->visited
, j
))
1598 topo_visit (graph
, ti
, j
);
1601 ti
->topo_order
.safe_push (n
);
1604 /* Process a constraint C that represents x = *(y + off), using DELTA as the
1605 starting solution for y. */
1608 do_sd_constraint (constraint_graph_t graph
, constraint_t c
,
1609 bitmap delta
, bitmap
*expanded_delta
)
1611 unsigned int lhs
= c
->lhs
.var
;
1613 bitmap sol
= get_varinfo (lhs
)->solution
;
1616 HOST_WIDE_INT roffset
= c
->rhs
.offset
;
1618 /* Our IL does not allow this. */
1619 gcc_checking_assert (c
->lhs
.offset
== 0);
1621 /* If the solution of Y contains anything it is good enough to transfer
1623 if (bitmap_bit_p (delta
, anything_id
))
1625 flag
|= bitmap_set_bit (sol
, anything_id
);
1629 /* If we do not know at with offset the rhs is dereferenced compute
1630 the reachability set of DELTA, conservatively assuming it is
1631 dereferenced at all valid offsets. */
1632 if (roffset
== UNKNOWN_OFFSET
)
1634 delta
= solution_set_expand (delta
, expanded_delta
);
1635 /* No further offset processing is necessary. */
1639 /* For each variable j in delta (Sol(y)), add
1640 an edge in the graph from j to x, and union Sol(j) into Sol(x). */
1641 EXECUTE_IF_SET_IN_BITMAP (delta
, 0, j
, bi
)
1643 varinfo_t v
= get_varinfo (j
);
1644 HOST_WIDE_INT fieldoffset
= v
->offset
+ roffset
;
1645 unsigned HOST_WIDE_INT size
= v
->size
;
1650 else if (roffset
!= 0)
1652 if (fieldoffset
< 0)
1653 v
= get_varinfo (v
->head
);
1655 v
= first_or_preceding_vi_for_offset (v
, fieldoffset
);
1658 /* We have to include all fields that overlap the current field
1659 shifted by roffset. */
1664 /* Adding edges from the special vars is pointless.
1665 They don't have sets that can change. */
1666 if (get_varinfo (t
)->is_special_var
)
1667 flag
|= bitmap_ior_into (sol
, get_varinfo (t
)->solution
);
1668 /* Merging the solution from ESCAPED needlessly increases
1669 the set. Use ESCAPED as representative instead. */
1670 else if (v
->id
== escaped_id
)
1671 flag
|= bitmap_set_bit (sol
, escaped_id
);
1672 else if (v
->may_have_pointers
1673 && add_graph_edge (graph
, lhs
, t
))
1674 flag
|= bitmap_ior_into (sol
, get_varinfo (t
)->solution
);
1682 while (v
->offset
< fieldoffset
+ size
);
1686 /* If the LHS solution changed, mark the var as changed. */
1689 get_varinfo (lhs
)->solution
= sol
;
1690 bitmap_set_bit (changed
, lhs
);
1694 /* Process a constraint C that represents *(x + off) = y using DELTA
1695 as the starting solution for x. */
1698 do_ds_constraint (constraint_t c
, bitmap delta
, bitmap
*expanded_delta
)
1700 unsigned int rhs
= c
->rhs
.var
;
1701 bitmap sol
= get_varinfo (rhs
)->solution
;
1704 HOST_WIDE_INT loff
= c
->lhs
.offset
;
1705 bool escaped_p
= false;
1707 /* Our IL does not allow this. */
1708 gcc_checking_assert (c
->rhs
.offset
== 0);
1710 /* If the solution of y contains ANYTHING simply use the ANYTHING
1711 solution. This avoids needlessly increasing the points-to sets. */
1712 if (bitmap_bit_p (sol
, anything_id
))
1713 sol
= get_varinfo (find (anything_id
))->solution
;
1715 /* If the solution for x contains ANYTHING we have to merge the
1716 solution of y into all pointer variables which we do via
1718 if (bitmap_bit_p (delta
, anything_id
))
1720 unsigned t
= find (storedanything_id
);
1721 if (add_graph_edge (graph
, t
, rhs
))
1723 if (bitmap_ior_into (get_varinfo (t
)->solution
, sol
))
1724 bitmap_set_bit (changed
, t
);
1729 /* If we do not know at with offset the rhs is dereferenced compute
1730 the reachability set of DELTA, conservatively assuming it is
1731 dereferenced at all valid offsets. */
1732 if (loff
== UNKNOWN_OFFSET
)
1734 delta
= solution_set_expand (delta
, expanded_delta
);
1738 /* For each member j of delta (Sol(x)), add an edge from y to j and
1739 union Sol(y) into Sol(j) */
1740 EXECUTE_IF_SET_IN_BITMAP (delta
, 0, j
, bi
)
1742 varinfo_t v
= get_varinfo (j
);
1744 HOST_WIDE_INT fieldoffset
= v
->offset
+ loff
;
1745 unsigned HOST_WIDE_INT size
= v
->size
;
1751 if (fieldoffset
< 0)
1752 v
= get_varinfo (v
->head
);
1754 v
= first_or_preceding_vi_for_offset (v
, fieldoffset
);
1757 /* We have to include all fields that overlap the current field
1761 if (v
->may_have_pointers
)
1763 /* If v is a global variable then this is an escape point. */
1764 if (v
->is_global_var
1767 t
= find (escaped_id
);
1768 if (add_graph_edge (graph
, t
, rhs
)
1769 && bitmap_ior_into (get_varinfo (t
)->solution
, sol
))
1770 bitmap_set_bit (changed
, t
);
1771 /* Enough to let rhs escape once. */
1775 if (v
->is_special_var
)
1779 if (add_graph_edge (graph
, t
, rhs
)
1780 && bitmap_ior_into (get_varinfo (t
)->solution
, sol
))
1781 bitmap_set_bit (changed
, t
);
1790 while (v
->offset
< fieldoffset
+ size
);
1794 /* Handle a non-simple (simple meaning requires no iteration),
1795 constraint (IE *x = &y, x = *y, *x = y, and x = y with offsets involved). */
1798 do_complex_constraint (constraint_graph_t graph
, constraint_t c
, bitmap delta
,
1799 bitmap
*expanded_delta
)
1801 if (c
->lhs
.type
== DEREF
)
1803 if (c
->rhs
.type
== ADDRESSOF
)
1810 do_ds_constraint (c
, delta
, expanded_delta
);
1813 else if (c
->rhs
.type
== DEREF
)
1816 if (!(get_varinfo (c
->lhs
.var
)->is_special_var
))
1817 do_sd_constraint (graph
, c
, delta
, expanded_delta
);
1824 gcc_checking_assert (c
->rhs
.type
== SCALAR
&& c
->lhs
.type
== SCALAR
1825 && c
->rhs
.offset
!= 0 && c
->lhs
.offset
== 0);
1826 tmp
= get_varinfo (c
->lhs
.var
)->solution
;
1828 flag
= set_union_with_increment (tmp
, delta
, c
->rhs
.offset
,
1832 bitmap_set_bit (changed
, c
->lhs
.var
);
1836 /* Initialize and return a new SCC info structure. */
1838 scc_info::scc_info (size_t size
) :
1839 visited (size
), deleted (size
), current_index (0), scc_stack (1)
1841 bitmap_clear (visited
);
1842 bitmap_clear (deleted
);
1843 node_mapping
= XNEWVEC (unsigned int, size
);
1844 dfs
= XCNEWVEC (unsigned int, size
);
1846 for (size_t i
= 0; i
< size
; i
++)
1847 node_mapping
[i
] = i
;
1850 /* Free an SCC info structure pointed to by SI */
1852 scc_info::~scc_info ()
1854 free (node_mapping
);
1859 /* Find indirect cycles in GRAPH that occur, using strongly connected
1860 components, and note them in the indirect cycles map.
1862 This technique comes from Ben Hardekopf and Calvin Lin,
1863 "It Pays to be Lazy: Fast and Accurate Pointer Analysis for Millions of
1864 Lines of Code", submitted to PLDI 2007. */
1867 find_indirect_cycles (constraint_graph_t graph
)
1870 unsigned int size
= graph
->size
;
1873 for (i
= 0; i
< MIN (LAST_REF_NODE
, size
); i
++ )
1874 if (!bitmap_bit_p (si
.visited
, i
) && find (i
) == i
)
1875 scc_visit (graph
, &si
, i
);
1878 /* Compute a topological ordering for GRAPH, and store the result in the
1879 topo_info structure TI. */
1882 compute_topo_order (constraint_graph_t graph
,
1883 struct topo_info
*ti
)
1886 unsigned int size
= graph
->size
;
1888 for (i
= 0; i
!= size
; ++i
)
1889 if (!bitmap_bit_p (ti
->visited
, i
) && find (i
) == i
)
1890 topo_visit (graph
, ti
, i
);
1893 /* Structure used to for hash value numbering of pointer equivalence
1896 typedef struct equiv_class_label
1899 unsigned int equivalence_class
;
1901 } *equiv_class_label_t
;
1902 typedef const struct equiv_class_label
*const_equiv_class_label_t
;
1904 /* Equiv_class_label hashtable helpers. */
1906 struct equiv_class_hasher
: free_ptr_hash
<equiv_class_label
>
1908 static inline hashval_t
hash (const equiv_class_label
*);
1909 static inline bool equal (const equiv_class_label
*,
1910 const equiv_class_label
*);
1913 /* Hash function for a equiv_class_label_t */
1916 equiv_class_hasher::hash (const equiv_class_label
*ecl
)
1918 return ecl
->hashcode
;
1921 /* Equality function for two equiv_class_label_t's. */
1924 equiv_class_hasher::equal (const equiv_class_label
*eql1
,
1925 const equiv_class_label
*eql2
)
1927 return (eql1
->hashcode
== eql2
->hashcode
1928 && bitmap_equal_p (eql1
->labels
, eql2
->labels
));
1931 /* A hashtable for mapping a bitmap of labels->pointer equivalence
1933 static hash_table
<equiv_class_hasher
> *pointer_equiv_class_table
;
1935 /* A hashtable for mapping a bitmap of labels->location equivalence
1937 static hash_table
<equiv_class_hasher
> *location_equiv_class_table
;
1939 /* Lookup a equivalence class in TABLE by the bitmap of LABELS with
1940 hash HAS it contains. Sets *REF_LABELS to the bitmap LABELS
1941 is equivalent to. */
1943 static equiv_class_label
*
1944 equiv_class_lookup_or_add (hash_table
<equiv_class_hasher
> *table
,
1947 equiv_class_label
**slot
;
1948 equiv_class_label ecl
;
1950 ecl
.labels
= labels
;
1951 ecl
.hashcode
= bitmap_hash (labels
);
1952 slot
= table
->find_slot (&ecl
, INSERT
);
1955 *slot
= XNEW (struct equiv_class_label
);
1956 (*slot
)->labels
= labels
;
1957 (*slot
)->hashcode
= ecl
.hashcode
;
1958 (*slot
)->equivalence_class
= 0;
1964 /* Perform offline variable substitution.
1966 This is a worst case quadratic time way of identifying variables
1967 that must have equivalent points-to sets, including those caused by
1968 static cycles, and single entry subgraphs, in the constraint graph.
1970 The technique is described in "Exploiting Pointer and Location
1971 Equivalence to Optimize Pointer Analysis. In the 14th International
1972 Static Analysis Symposium (SAS), August 2007." It is known as the
1973 "HU" algorithm, and is equivalent to value numbering the collapsed
1974 constraint graph including evaluating unions.
1976 The general method of finding equivalence classes is as follows:
1977 Add fake nodes (REF nodes) and edges for *a = b and a = *b constraints.
1978 Initialize all non-REF nodes to be direct nodes.
1979 For each constraint a = a U {b}, we set pts(a) = pts(a) u {fresh
1981 For each constraint containing the dereference, we also do the same
1984 We then compute SCC's in the graph and unify nodes in the same SCC,
1987 For each non-collapsed node x:
1988 Visit all unvisited explicit incoming edges.
1989 Ignoring all non-pointers, set pts(x) = Union of pts(a) for y
1991 Lookup the equivalence class for pts(x).
1992 If we found one, equivalence_class(x) = found class.
1993 Otherwise, equivalence_class(x) = new class, and new_class is
1994 added to the lookup table.
1996 All direct nodes with the same equivalence class can be replaced
1997 with a single representative node.
1998 All unlabeled nodes (label == 0) are not pointers and all edges
1999 involving them can be eliminated.
2000 We perform these optimizations during rewrite_constraints
2002 In addition to pointer equivalence class finding, we also perform
2003 location equivalence class finding. This is the set of variables
2004 that always appear together in points-to sets. We use this to
2005 compress the size of the points-to sets. */
2007 /* Current maximum pointer equivalence class id. */
2008 static int pointer_equiv_class
;
2010 /* Current maximum location equivalence class id. */
2011 static int location_equiv_class
;
2013 /* Recursive routine to find strongly connected components in GRAPH,
2014 and label it's nodes with DFS numbers. */
2017 condense_visit (constraint_graph_t graph
, struct scc_info
*si
, unsigned int n
)
2021 unsigned int my_dfs
;
2023 gcc_checking_assert (si
->node_mapping
[n
] == n
);
2024 bitmap_set_bit (si
->visited
, n
);
2025 si
->dfs
[n
] = si
->current_index
++;
2026 my_dfs
= si
->dfs
[n
];
2028 /* Visit all the successors. */
2029 EXECUTE_IF_IN_NONNULL_BITMAP (graph
->preds
[n
], 0, i
, bi
)
2031 unsigned int w
= si
->node_mapping
[i
];
2033 if (bitmap_bit_p (si
->deleted
, w
))
2036 if (!bitmap_bit_p (si
->visited
, w
))
2037 condense_visit (graph
, si
, w
);
2039 unsigned int t
= si
->node_mapping
[w
];
2040 gcc_checking_assert (si
->node_mapping
[n
] == n
);
2041 if (si
->dfs
[t
] < si
->dfs
[n
])
2042 si
->dfs
[n
] = si
->dfs
[t
];
2045 /* Visit all the implicit predecessors. */
2046 EXECUTE_IF_IN_NONNULL_BITMAP (graph
->implicit_preds
[n
], 0, i
, bi
)
2048 unsigned int w
= si
->node_mapping
[i
];
2050 if (bitmap_bit_p (si
->deleted
, w
))
2053 if (!bitmap_bit_p (si
->visited
, w
))
2054 condense_visit (graph
, si
, w
);
2056 unsigned int t
= si
->node_mapping
[w
];
2057 gcc_assert (si
->node_mapping
[n
] == n
);
2058 if (si
->dfs
[t
] < si
->dfs
[n
])
2059 si
->dfs
[n
] = si
->dfs
[t
];
2062 /* See if any components have been identified. */
2063 if (si
->dfs
[n
] == my_dfs
)
2065 while (si
->scc_stack
.length () != 0
2066 && si
->dfs
[si
->scc_stack
.last ()] >= my_dfs
)
2068 unsigned int w
= si
->scc_stack
.pop ();
2069 si
->node_mapping
[w
] = n
;
2071 if (!bitmap_bit_p (graph
->direct_nodes
, w
))
2072 bitmap_clear_bit (graph
->direct_nodes
, n
);
2074 /* Unify our nodes. */
2075 if (graph
->preds
[w
])
2077 if (!graph
->preds
[n
])
2078 graph
->preds
[n
] = BITMAP_ALLOC (&predbitmap_obstack
);
2079 bitmap_ior_into (graph
->preds
[n
], graph
->preds
[w
]);
2081 if (graph
->implicit_preds
[w
])
2083 if (!graph
->implicit_preds
[n
])
2084 graph
->implicit_preds
[n
] = BITMAP_ALLOC (&predbitmap_obstack
);
2085 bitmap_ior_into (graph
->implicit_preds
[n
],
2086 graph
->implicit_preds
[w
]);
2088 if (graph
->points_to
[w
])
2090 if (!graph
->points_to
[n
])
2091 graph
->points_to
[n
] = BITMAP_ALLOC (&predbitmap_obstack
);
2092 bitmap_ior_into (graph
->points_to
[n
],
2093 graph
->points_to
[w
]);
2096 bitmap_set_bit (si
->deleted
, n
);
2099 si
->scc_stack
.safe_push (n
);
2102 /* Label pointer equivalences.
2104 This performs a value numbering of the constraint graph to
2105 discover which variables will always have the same points-to sets
2106 under the current set of constraints.
2108 The way it value numbers is to store the set of points-to bits
2109 generated by the constraints and graph edges. This is just used as a
2110 hash and equality comparison. The *actual set of points-to bits* is
2111 completely irrelevant, in that we don't care about being able to
2114 The equality values (currently bitmaps) just have to satisfy a few
2115 constraints, the main ones being:
2116 1. The combining operation must be order independent.
2117 2. The end result of a given set of operations must be unique iff the
2118 combination of input values is unique
2122 label_visit (constraint_graph_t graph
, struct scc_info
*si
, unsigned int n
)
2124 unsigned int i
, first_pred
;
2127 bitmap_set_bit (si
->visited
, n
);
2129 /* Label and union our incoming edges's points to sets. */
2131 EXECUTE_IF_IN_NONNULL_BITMAP (graph
->preds
[n
], 0, i
, bi
)
2133 unsigned int w
= si
->node_mapping
[i
];
2134 if (!bitmap_bit_p (si
->visited
, w
))
2135 label_visit (graph
, si
, w
);
2137 /* Skip unused edges */
2138 if (w
== n
|| graph
->pointer_label
[w
] == 0)
2141 if (graph
->points_to
[w
])
2143 if (!graph
->points_to
[n
])
2145 if (first_pred
== -1U)
2149 graph
->points_to
[n
] = BITMAP_ALLOC (&predbitmap_obstack
);
2150 bitmap_ior (graph
->points_to
[n
],
2151 graph
->points_to
[first_pred
],
2152 graph
->points_to
[w
]);
2156 bitmap_ior_into (graph
->points_to
[n
], graph
->points_to
[w
]);
2160 /* Indirect nodes get fresh variables and a new pointer equiv class. */
2161 if (!bitmap_bit_p (graph
->direct_nodes
, n
))
2163 if (!graph
->points_to
[n
])
2165 graph
->points_to
[n
] = BITMAP_ALLOC (&predbitmap_obstack
);
2166 if (first_pred
!= -1U)
2167 bitmap_copy (graph
->points_to
[n
], graph
->points_to
[first_pred
]);
2169 bitmap_set_bit (graph
->points_to
[n
], FIRST_REF_NODE
+ n
);
2170 graph
->pointer_label
[n
] = pointer_equiv_class
++;
2171 equiv_class_label_t ecl
;
2172 ecl
= equiv_class_lookup_or_add (pointer_equiv_class_table
,
2173 graph
->points_to
[n
]);
2174 ecl
->equivalence_class
= graph
->pointer_label
[n
];
2178 /* If there was only a single non-empty predecessor the pointer equiv
2179 class is the same. */
2180 if (!graph
->points_to
[n
])
2182 if (first_pred
!= -1U)
2184 graph
->pointer_label
[n
] = graph
->pointer_label
[first_pred
];
2185 graph
->points_to
[n
] = graph
->points_to
[first_pred
];
2190 if (!bitmap_empty_p (graph
->points_to
[n
]))
2192 equiv_class_label_t ecl
;
2193 ecl
= equiv_class_lookup_or_add (pointer_equiv_class_table
,
2194 graph
->points_to
[n
]);
2195 if (ecl
->equivalence_class
== 0)
2196 ecl
->equivalence_class
= pointer_equiv_class
++;
2199 BITMAP_FREE (graph
->points_to
[n
]);
2200 graph
->points_to
[n
] = ecl
->labels
;
2202 graph
->pointer_label
[n
] = ecl
->equivalence_class
;
2206 /* Print the pred graph in dot format. */
2209 dump_pred_graph (struct scc_info
*si
, FILE *file
)
2213 /* Only print the graph if it has already been initialized: */
2217 /* Prints the header of the dot file: */
2218 fprintf (file
, "strict digraph {\n");
2219 fprintf (file
, " node [\n shape = box\n ]\n");
2220 fprintf (file
, " edge [\n fontsize = \"12\"\n ]\n");
2221 fprintf (file
, "\n // List of nodes and complex constraints in "
2222 "the constraint graph:\n");
2224 /* The next lines print the nodes in the graph together with the
2225 complex constraints attached to them. */
2226 for (i
= 1; i
< graph
->size
; i
++)
2228 if (i
== FIRST_REF_NODE
)
2230 if (si
->node_mapping
[i
] != i
)
2232 if (i
< FIRST_REF_NODE
)
2233 fprintf (file
, "\"%s\"", get_varinfo (i
)->name
);
2235 fprintf (file
, "\"*%s\"", get_varinfo (i
- FIRST_REF_NODE
)->name
);
2236 if (graph
->points_to
[i
]
2237 && !bitmap_empty_p (graph
->points_to
[i
]))
2239 if (i
< FIRST_REF_NODE
)
2240 fprintf (file
, "[label=\"%s = {", get_varinfo (i
)->name
);
2242 fprintf (file
, "[label=\"*%s = {",
2243 get_varinfo (i
- FIRST_REF_NODE
)->name
);
2246 EXECUTE_IF_SET_IN_BITMAP (graph
->points_to
[i
], 0, j
, bi
)
2247 fprintf (file
, " %d", j
);
2248 fprintf (file
, " }\"]");
2250 fprintf (file
, ";\n");
2253 /* Go over the edges. */
2254 fprintf (file
, "\n // Edges in the constraint graph:\n");
2255 for (i
= 1; i
< graph
->size
; i
++)
2259 if (si
->node_mapping
[i
] != i
)
2261 EXECUTE_IF_IN_NONNULL_BITMAP (graph
->preds
[i
], 0, j
, bi
)
2263 unsigned from
= si
->node_mapping
[j
];
2264 if (from
< FIRST_REF_NODE
)
2265 fprintf (file
, "\"%s\"", get_varinfo (from
)->name
);
2267 fprintf (file
, "\"*%s\"", get_varinfo (from
- FIRST_REF_NODE
)->name
);
2268 fprintf (file
, " -> ");
2269 if (i
< FIRST_REF_NODE
)
2270 fprintf (file
, "\"%s\"", get_varinfo (i
)->name
);
2272 fprintf (file
, "\"*%s\"", get_varinfo (i
- FIRST_REF_NODE
)->name
);
2273 fprintf (file
, ";\n");
2277 /* Prints the tail of the dot file. */
2278 fprintf (file
, "}\n");
2281 /* Perform offline variable substitution, discovering equivalence
2282 classes, and eliminating non-pointer variables. */
2284 static struct scc_info
*
2285 perform_var_substitution (constraint_graph_t graph
)
2288 unsigned int size
= graph
->size
;
2289 scc_info
*si
= new scc_info (size
);
2291 bitmap_obstack_initialize (&iteration_obstack
);
2292 pointer_equiv_class_table
= new hash_table
<equiv_class_hasher
> (511);
2293 location_equiv_class_table
2294 = new hash_table
<equiv_class_hasher
> (511);
2295 pointer_equiv_class
= 1;
2296 location_equiv_class
= 1;
2298 /* Condense the nodes, which means to find SCC's, count incoming
2299 predecessors, and unite nodes in SCC's. */
2300 for (i
= 1; i
< FIRST_REF_NODE
; i
++)
2301 if (!bitmap_bit_p (si
->visited
, si
->node_mapping
[i
]))
2302 condense_visit (graph
, si
, si
->node_mapping
[i
]);
2304 if (dump_file
&& (dump_flags
& TDF_GRAPH
))
2306 fprintf (dump_file
, "\n\n// The constraint graph before var-substitution "
2307 "in dot format:\n");
2308 dump_pred_graph (si
, dump_file
);
2309 fprintf (dump_file
, "\n\n");
2312 bitmap_clear (si
->visited
);
2313 /* Actually the label the nodes for pointer equivalences */
2314 for (i
= 1; i
< FIRST_REF_NODE
; i
++)
2315 if (!bitmap_bit_p (si
->visited
, si
->node_mapping
[i
]))
2316 label_visit (graph
, si
, si
->node_mapping
[i
]);
2318 /* Calculate location equivalence labels. */
2319 for (i
= 1; i
< FIRST_REF_NODE
; i
++)
2325 if (!graph
->pointed_by
[i
])
2327 pointed_by
= BITMAP_ALLOC (&iteration_obstack
);
2329 /* Translate the pointed-by mapping for pointer equivalence
2331 EXECUTE_IF_SET_IN_BITMAP (graph
->pointed_by
[i
], 0, j
, bi
)
2333 bitmap_set_bit (pointed_by
,
2334 graph
->pointer_label
[si
->node_mapping
[j
]]);
2336 /* The original pointed_by is now dead. */
2337 BITMAP_FREE (graph
->pointed_by
[i
]);
2339 /* Look up the location equivalence label if one exists, or make
2341 equiv_class_label_t ecl
;
2342 ecl
= equiv_class_lookup_or_add (location_equiv_class_table
, pointed_by
);
2343 if (ecl
->equivalence_class
== 0)
2344 ecl
->equivalence_class
= location_equiv_class
++;
2347 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2348 fprintf (dump_file
, "Found location equivalence for node %s\n",
2349 get_varinfo (i
)->name
);
2350 BITMAP_FREE (pointed_by
);
2352 graph
->loc_label
[i
] = ecl
->equivalence_class
;
2356 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2357 for (i
= 1; i
< FIRST_REF_NODE
; i
++)
2359 unsigned j
= si
->node_mapping
[i
];
2362 fprintf (dump_file
, "%s node id %d ",
2363 bitmap_bit_p (graph
->direct_nodes
, i
)
2364 ? "Direct" : "Indirect", i
);
2365 if (i
< FIRST_REF_NODE
)
2366 fprintf (dump_file
, "\"%s\"", get_varinfo (i
)->name
);
2368 fprintf (dump_file
, "\"*%s\"",
2369 get_varinfo (i
- FIRST_REF_NODE
)->name
);
2370 fprintf (dump_file
, " mapped to SCC leader node id %d ", j
);
2371 if (j
< FIRST_REF_NODE
)
2372 fprintf (dump_file
, "\"%s\"\n", get_varinfo (j
)->name
);
2374 fprintf (dump_file
, "\"*%s\"\n",
2375 get_varinfo (j
- FIRST_REF_NODE
)->name
);
2380 "Equivalence classes for %s node id %d ",
2381 bitmap_bit_p (graph
->direct_nodes
, i
)
2382 ? "direct" : "indirect", i
);
2383 if (i
< FIRST_REF_NODE
)
2384 fprintf (dump_file
, "\"%s\"", get_varinfo (i
)->name
);
2386 fprintf (dump_file
, "\"*%s\"",
2387 get_varinfo (i
- FIRST_REF_NODE
)->name
);
2389 ": pointer %d, location %d\n",
2390 graph
->pointer_label
[i
], graph
->loc_label
[i
]);
2394 /* Quickly eliminate our non-pointer variables. */
2396 for (i
= 1; i
< FIRST_REF_NODE
; i
++)
2398 unsigned int node
= si
->node_mapping
[i
];
2400 if (graph
->pointer_label
[node
] == 0)
2402 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2404 "%s is a non-pointer variable, eliminating edges.\n",
2405 get_varinfo (node
)->name
);
2406 stats
.nonpointer_vars
++;
2407 clear_edges_for_node (graph
, node
);
2414 /* Free information that was only necessary for variable
2418 free_var_substitution_info (struct scc_info
*si
)
2421 free (graph
->pointer_label
);
2422 free (graph
->loc_label
);
2423 free (graph
->pointed_by
);
2424 free (graph
->points_to
);
2425 free (graph
->eq_rep
);
2426 sbitmap_free (graph
->direct_nodes
);
2427 delete pointer_equiv_class_table
;
2428 pointer_equiv_class_table
= NULL
;
2429 delete location_equiv_class_table
;
2430 location_equiv_class_table
= NULL
;
2431 bitmap_obstack_release (&iteration_obstack
);
2434 /* Return an existing node that is equivalent to NODE, which has
2435 equivalence class LABEL, if one exists. Return NODE otherwise. */
2438 find_equivalent_node (constraint_graph_t graph
,
2439 unsigned int node
, unsigned int label
)
2441 /* If the address version of this variable is unused, we can
2442 substitute it for anything else with the same label.
2443 Otherwise, we know the pointers are equivalent, but not the
2444 locations, and we can unite them later. */
2446 if (!bitmap_bit_p (graph
->address_taken
, node
))
2448 gcc_checking_assert (label
< graph
->size
);
2450 if (graph
->eq_rep
[label
] != -1)
2452 /* Unify the two variables since we know they are equivalent. */
2453 if (unite (graph
->eq_rep
[label
], node
))
2454 unify_nodes (graph
, graph
->eq_rep
[label
], node
, false);
2455 return graph
->eq_rep
[label
];
2459 graph
->eq_rep
[label
] = node
;
2460 graph
->pe_rep
[label
] = node
;
2465 gcc_checking_assert (label
< graph
->size
);
2466 graph
->pe
[node
] = label
;
2467 if (graph
->pe_rep
[label
] == -1)
2468 graph
->pe_rep
[label
] = node
;
2474 /* Unite pointer equivalent but not location equivalent nodes in
2475 GRAPH. This may only be performed once variable substitution is
2479 unite_pointer_equivalences (constraint_graph_t graph
)
2483 /* Go through the pointer equivalences and unite them to their
2484 representative, if they aren't already. */
2485 for (i
= 1; i
< FIRST_REF_NODE
; i
++)
2487 unsigned int label
= graph
->pe
[i
];
2490 int label_rep
= graph
->pe_rep
[label
];
2492 if (label_rep
== -1)
2495 label_rep
= find (label_rep
);
2496 if (label_rep
>= 0 && unite (label_rep
, find (i
)))
2497 unify_nodes (graph
, label_rep
, i
, false);
2502 /* Move complex constraints to the GRAPH nodes they belong to. */
2505 move_complex_constraints (constraint_graph_t graph
)
2510 FOR_EACH_VEC_ELT (constraints
, i
, c
)
2514 struct constraint_expr lhs
= c
->lhs
;
2515 struct constraint_expr rhs
= c
->rhs
;
2517 if (lhs
.type
== DEREF
)
2519 insert_into_complex (graph
, lhs
.var
, c
);
2521 else if (rhs
.type
== DEREF
)
2523 if (!(get_varinfo (lhs
.var
)->is_special_var
))
2524 insert_into_complex (graph
, rhs
.var
, c
);
2526 else if (rhs
.type
!= ADDRESSOF
&& lhs
.var
> anything_id
2527 && (lhs
.offset
!= 0 || rhs
.offset
!= 0))
2529 insert_into_complex (graph
, rhs
.var
, c
);
2536 /* Optimize and rewrite complex constraints while performing
2537 collapsing of equivalent nodes. SI is the SCC_INFO that is the
2538 result of perform_variable_substitution. */
2541 rewrite_constraints (constraint_graph_t graph
,
2542 struct scc_info
*si
)
2549 for (unsigned int j
= 0; j
< graph
->size
; j
++)
2550 gcc_assert (find (j
) == j
);
2553 FOR_EACH_VEC_ELT (constraints
, i
, c
)
2555 struct constraint_expr lhs
= c
->lhs
;
2556 struct constraint_expr rhs
= c
->rhs
;
2557 unsigned int lhsvar
= find (lhs
.var
);
2558 unsigned int rhsvar
= find (rhs
.var
);
2559 unsigned int lhsnode
, rhsnode
;
2560 unsigned int lhslabel
, rhslabel
;
2562 lhsnode
= si
->node_mapping
[lhsvar
];
2563 rhsnode
= si
->node_mapping
[rhsvar
];
2564 lhslabel
= graph
->pointer_label
[lhsnode
];
2565 rhslabel
= graph
->pointer_label
[rhsnode
];
2567 /* See if it is really a non-pointer variable, and if so, ignore
2571 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2574 fprintf (dump_file
, "%s is a non-pointer variable, "
2575 "ignoring constraint:",
2576 get_varinfo (lhs
.var
)->name
);
2577 dump_constraint (dump_file
, c
);
2578 fprintf (dump_file
, "\n");
2580 constraints
[i
] = NULL
;
2586 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2589 fprintf (dump_file
, "%s is a non-pointer variable, "
2590 "ignoring constraint:",
2591 get_varinfo (rhs
.var
)->name
);
2592 dump_constraint (dump_file
, c
);
2593 fprintf (dump_file
, "\n");
2595 constraints
[i
] = NULL
;
2599 lhsvar
= find_equivalent_node (graph
, lhsvar
, lhslabel
);
2600 rhsvar
= find_equivalent_node (graph
, rhsvar
, rhslabel
);
2601 c
->lhs
.var
= lhsvar
;
2602 c
->rhs
.var
= rhsvar
;
2606 /* Eliminate indirect cycles involving NODE. Return true if NODE was
2607 part of an SCC, false otherwise. */
2610 eliminate_indirect_cycles (unsigned int node
)
2612 if (graph
->indirect_cycles
[node
] != -1
2613 && !bitmap_empty_p (get_varinfo (node
)->solution
))
2616 auto_vec
<unsigned> queue
;
2618 unsigned int to
= find (graph
->indirect_cycles
[node
]);
2621 /* We can't touch the solution set and call unify_nodes
2622 at the same time, because unify_nodes is going to do
2623 bitmap unions into it. */
2625 EXECUTE_IF_SET_IN_BITMAP (get_varinfo (node
)->solution
, 0, i
, bi
)
2627 if (find (i
) == i
&& i
!= to
)
2630 queue
.safe_push (i
);
2635 queue
.iterate (queuepos
, &i
);
2638 unify_nodes (graph
, to
, i
, true);
2645 /* Solve the constraint graph GRAPH using our worklist solver.
2646 This is based on the PW* family of solvers from the "Efficient Field
2647 Sensitive Pointer Analysis for C" paper.
2648 It works by iterating over all the graph nodes, processing the complex
2649 constraints and propagating the copy constraints, until everything stops
2650 changed. This corresponds to steps 6-8 in the solving list given above. */
2653 solve_graph (constraint_graph_t graph
)
2655 unsigned int size
= graph
->size
;
2659 changed
= BITMAP_ALLOC (NULL
);
2661 /* Mark all initial non-collapsed nodes as changed. */
2662 for (i
= 1; i
< size
; i
++)
2664 varinfo_t ivi
= get_varinfo (i
);
2665 if (find (i
) == i
&& !bitmap_empty_p (ivi
->solution
)
2666 && ((graph
->succs
[i
] && !bitmap_empty_p (graph
->succs
[i
]))
2667 || graph
->complex[i
].length () > 0))
2668 bitmap_set_bit (changed
, i
);
2671 /* Allocate a bitmap to be used to store the changed bits. */
2672 pts
= BITMAP_ALLOC (&pta_obstack
);
2674 while (!bitmap_empty_p (changed
))
2677 struct topo_info
*ti
= init_topo_info ();
2680 bitmap_obstack_initialize (&iteration_obstack
);
2682 compute_topo_order (graph
, ti
);
2684 while (ti
->topo_order
.length () != 0)
2687 i
= ti
->topo_order
.pop ();
2689 /* If this variable is not a representative, skip it. */
2693 /* In certain indirect cycle cases, we may merge this
2694 variable to another. */
2695 if (eliminate_indirect_cycles (i
) && find (i
) != i
)
2698 /* If the node has changed, we need to process the
2699 complex constraints and outgoing edges again. */
2700 if (bitmap_clear_bit (changed
, i
))
2705 vec
<constraint_t
> complex = graph
->complex[i
];
2706 varinfo_t vi
= get_varinfo (i
);
2707 bool solution_empty
;
2709 /* Compute the changed set of solution bits. If anything
2710 is in the solution just propagate that. */
2711 if (bitmap_bit_p (vi
->solution
, anything_id
))
2713 /* If anything is also in the old solution there is
2715 ??? But we shouldn't ended up with "changed" set ... */
2717 && bitmap_bit_p (vi
->oldsolution
, anything_id
))
2719 bitmap_copy (pts
, get_varinfo (find (anything_id
))->solution
);
2721 else if (vi
->oldsolution
)
2722 bitmap_and_compl (pts
, vi
->solution
, vi
->oldsolution
);
2724 bitmap_copy (pts
, vi
->solution
);
2726 if (bitmap_empty_p (pts
))
2729 if (vi
->oldsolution
)
2730 bitmap_ior_into (vi
->oldsolution
, pts
);
2733 vi
->oldsolution
= BITMAP_ALLOC (&oldpta_obstack
);
2734 bitmap_copy (vi
->oldsolution
, pts
);
2737 solution
= vi
->solution
;
2738 solution_empty
= bitmap_empty_p (solution
);
2740 /* Process the complex constraints */
2741 bitmap expanded_pts
= NULL
;
2742 FOR_EACH_VEC_ELT (complex, j
, c
)
2744 /* XXX: This is going to unsort the constraints in
2745 some cases, which will occasionally add duplicate
2746 constraints during unification. This does not
2747 affect correctness. */
2748 c
->lhs
.var
= find (c
->lhs
.var
);
2749 c
->rhs
.var
= find (c
->rhs
.var
);
2751 /* The only complex constraint that can change our
2752 solution to non-empty, given an empty solution,
2753 is a constraint where the lhs side is receiving
2754 some set from elsewhere. */
2755 if (!solution_empty
|| c
->lhs
.type
!= DEREF
)
2756 do_complex_constraint (graph
, c
, pts
, &expanded_pts
);
2758 BITMAP_FREE (expanded_pts
);
2760 solution_empty
= bitmap_empty_p (solution
);
2762 if (!solution_empty
)
2765 unsigned eff_escaped_id
= find (escaped_id
);
2767 /* Propagate solution to all successors. */
2768 unsigned to_remove
= ~0U;
2769 EXECUTE_IF_IN_NONNULL_BITMAP (graph
->succs
[i
],
2772 if (to_remove
!= ~0U)
2774 bitmap_clear_bit (graph
->succs
[i
], to_remove
);
2777 unsigned int to
= find (j
);
2780 /* Update the succ graph, avoiding duplicate
2783 if (! bitmap_set_bit (graph
->succs
[i
], to
))
2785 /* We eventually end up processing 'to' twice
2786 as it is undefined whether bitmap iteration
2787 iterates over bits set during iteration.
2788 Play safe instead of doing tricks. */
2790 /* Don't try to propagate to ourselves. */
2794 bitmap tmp
= get_varinfo (to
)->solution
;
2797 /* If we propagate from ESCAPED use ESCAPED as
2799 if (i
== eff_escaped_id
)
2800 flag
= bitmap_set_bit (tmp
, escaped_id
);
2802 flag
= bitmap_ior_into (tmp
, pts
);
2805 bitmap_set_bit (changed
, to
);
2807 if (to_remove
!= ~0U)
2808 bitmap_clear_bit (graph
->succs
[i
], to_remove
);
2812 free_topo_info (ti
);
2813 bitmap_obstack_release (&iteration_obstack
);
2817 BITMAP_FREE (changed
);
2818 bitmap_obstack_release (&oldpta_obstack
);
2821 /* Map from trees to variable infos. */
2822 static hash_map
<tree
, varinfo_t
> *vi_for_tree
;
2825 /* Insert ID as the variable id for tree T in the vi_for_tree map. */
2828 insert_vi_for_tree (tree t
, varinfo_t vi
)
2831 gcc_assert (!vi_for_tree
->put (t
, vi
));
2834 /* Find the variable info for tree T in VI_FOR_TREE. If T does not
2835 exist in the map, return NULL, otherwise, return the varinfo we found. */
2838 lookup_vi_for_tree (tree t
)
2840 varinfo_t
*slot
= vi_for_tree
->get (t
);
2847 /* Return a printable name for DECL */
2850 alias_get_name (tree decl
)
2852 const char *res
= NULL
;
2858 if (TREE_CODE (decl
) == SSA_NAME
)
2860 res
= get_name (decl
);
2862 temp
= xasprintf ("%s_%u", res
, SSA_NAME_VERSION (decl
));
2864 temp
= xasprintf ("_%u", SSA_NAME_VERSION (decl
));
2865 res
= ggc_strdup (temp
);
2868 else if (DECL_P (decl
))
2870 if (DECL_ASSEMBLER_NAME_SET_P (decl
))
2871 res
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
2874 res
= get_name (decl
);
2877 temp
= xasprintf ("D.%u", DECL_UID (decl
));
2878 res
= ggc_strdup (temp
);
2889 /* Find the variable id for tree T in the map.
2890 If T doesn't exist in the map, create an entry for it and return it. */
2893 get_vi_for_tree (tree t
)
2895 varinfo_t
*slot
= vi_for_tree
->get (t
);
2898 unsigned int id
= create_variable_info_for (t
, alias_get_name (t
), false);
2899 return get_varinfo (id
);
2905 /* Get a scalar constraint expression for a new temporary variable. */
2907 static struct constraint_expr
2908 new_scalar_tmp_constraint_exp (const char *name
, bool add_id
)
2910 struct constraint_expr tmp
;
2913 vi
= new_var_info (NULL_TREE
, name
, add_id
);
2917 vi
->is_full_var
= 1;
2927 /* Get a constraint expression vector from an SSA_VAR_P node.
2928 If address_p is true, the result will be taken its address of. */
2931 get_constraint_for_ssa_var (tree t
, vec
<ce_s
> *results
, bool address_p
)
2933 struct constraint_expr cexpr
;
2936 /* We allow FUNCTION_DECLs here even though it doesn't make much sense. */
2937 gcc_assert (TREE_CODE (t
) == SSA_NAME
|| DECL_P (t
));
2939 /* For parameters, get at the points-to set for the actual parm
2941 if (TREE_CODE (t
) == SSA_NAME
2942 && SSA_NAME_IS_DEFAULT_DEF (t
)
2943 && (TREE_CODE (SSA_NAME_VAR (t
)) == PARM_DECL
2944 || TREE_CODE (SSA_NAME_VAR (t
)) == RESULT_DECL
))
2946 get_constraint_for_ssa_var (SSA_NAME_VAR (t
), results
, address_p
);
2950 /* For global variables resort to the alias target. */
2951 if (VAR_P (t
) && (TREE_STATIC (t
) || DECL_EXTERNAL (t
)))
2953 varpool_node
*node
= varpool_node::get (t
);
2954 if (node
&& node
->alias
&& node
->analyzed
)
2956 node
= node
->ultimate_alias_target ();
2957 /* Canonicalize the PT uid of all aliases to the ultimate target.
2958 ??? Hopefully the set of aliases can't change in a way that
2959 changes the ultimate alias target. */
2960 gcc_assert ((! DECL_PT_UID_SET_P (node
->decl
)
2961 || DECL_PT_UID (node
->decl
) == DECL_UID (node
->decl
))
2962 && (! DECL_PT_UID_SET_P (t
)
2963 || DECL_PT_UID (t
) == DECL_UID (node
->decl
)));
2964 DECL_PT_UID (t
) = DECL_UID (node
->decl
);
2968 /* If this is decl may bind to NULL note that. */
2970 && (! node
|| ! node
->nonzero_address ()))
2972 cexpr
.var
= nothing_id
;
2973 cexpr
.type
= SCALAR
;
2975 results
->safe_push (cexpr
);
2979 vi
= get_vi_for_tree (t
);
2981 cexpr
.type
= SCALAR
;
2984 /* If we are not taking the address of the constraint expr, add all
2985 sub-fiels of the variable as well. */
2987 && !vi
->is_full_var
)
2989 for (; vi
; vi
= vi_next (vi
))
2992 results
->safe_push (cexpr
);
2997 results
->safe_push (cexpr
);
3000 /* Process constraint T, performing various simplifications and then
3001 adding it to our list of overall constraints. */
3004 process_constraint (constraint_t t
)
3006 struct constraint_expr rhs
= t
->rhs
;
3007 struct constraint_expr lhs
= t
->lhs
;
3009 gcc_assert (rhs
.var
< varmap
.length ());
3010 gcc_assert (lhs
.var
< varmap
.length ());
3012 /* If we didn't get any useful constraint from the lhs we get
3013 &ANYTHING as fallback from get_constraint_for. Deal with
3014 it here by turning it into *ANYTHING. */
3015 if (lhs
.type
== ADDRESSOF
3016 && lhs
.var
== anything_id
)
3019 /* ADDRESSOF on the lhs is invalid. */
3020 gcc_assert (lhs
.type
!= ADDRESSOF
);
3022 /* We shouldn't add constraints from things that cannot have pointers.
3023 It's not completely trivial to avoid in the callers, so do it here. */
3024 if (rhs
.type
!= ADDRESSOF
3025 && !get_varinfo (rhs
.var
)->may_have_pointers
)
3028 /* Likewise adding to the solution of a non-pointer var isn't useful. */
3029 if (!get_varinfo (lhs
.var
)->may_have_pointers
)
3032 /* This can happen in our IR with things like n->a = *p */
3033 if (rhs
.type
== DEREF
&& lhs
.type
== DEREF
&& rhs
.var
!= anything_id
)
3035 /* Split into tmp = *rhs, *lhs = tmp */
3036 struct constraint_expr tmplhs
;
3037 tmplhs
= new_scalar_tmp_constraint_exp ("doubledereftmp", true);
3038 process_constraint (new_constraint (tmplhs
, rhs
));
3039 process_constraint (new_constraint (lhs
, tmplhs
));
3041 else if ((rhs
.type
!= SCALAR
|| rhs
.offset
!= 0) && lhs
.type
== DEREF
)
3043 /* Split into tmp = &rhs, *lhs = tmp */
3044 struct constraint_expr tmplhs
;
3045 tmplhs
= new_scalar_tmp_constraint_exp ("derefaddrtmp", true);
3046 process_constraint (new_constraint (tmplhs
, rhs
));
3047 process_constraint (new_constraint (lhs
, tmplhs
));
3051 gcc_assert (rhs
.type
!= ADDRESSOF
|| rhs
.offset
== 0);
3052 constraints
.safe_push (t
);
3057 /* Return the position, in bits, of FIELD_DECL from the beginning of its
3060 static HOST_WIDE_INT
3061 bitpos_of_field (const tree fdecl
)
3063 if (!tree_fits_shwi_p (DECL_FIELD_OFFSET (fdecl
))
3064 || !tree_fits_shwi_p (DECL_FIELD_BIT_OFFSET (fdecl
)))
3067 return (tree_to_shwi (DECL_FIELD_OFFSET (fdecl
)) * BITS_PER_UNIT
3068 + tree_to_shwi (DECL_FIELD_BIT_OFFSET (fdecl
)));
3072 /* Get constraint expressions for offsetting PTR by OFFSET. Stores the
3073 resulting constraint expressions in *RESULTS. */
3076 get_constraint_for_ptr_offset (tree ptr
, tree offset
,
3079 struct constraint_expr c
;
3081 HOST_WIDE_INT rhsoffset
;
3083 /* If we do not do field-sensitive PTA adding offsets to pointers
3084 does not change the points-to solution. */
3085 if (!use_field_sensitive
)
3087 get_constraint_for_rhs (ptr
, results
);
3091 /* If the offset is not a non-negative integer constant that fits
3092 in a HOST_WIDE_INT, we have to fall back to a conservative
3093 solution which includes all sub-fields of all pointed-to
3094 variables of ptr. */
3095 if (offset
== NULL_TREE
3096 || TREE_CODE (offset
) != INTEGER_CST
)
3097 rhsoffset
= UNKNOWN_OFFSET
;
3100 /* Sign-extend the offset. */
3101 offset_int soffset
= offset_int::from (offset
, SIGNED
);
3102 if (!wi::fits_shwi_p (soffset
))
3103 rhsoffset
= UNKNOWN_OFFSET
;
3106 /* Make sure the bit-offset also fits. */
3107 HOST_WIDE_INT rhsunitoffset
= soffset
.to_shwi ();
3108 rhsoffset
= rhsunitoffset
* (unsigned HOST_WIDE_INT
) BITS_PER_UNIT
;
3109 if (rhsunitoffset
!= rhsoffset
/ BITS_PER_UNIT
)
3110 rhsoffset
= UNKNOWN_OFFSET
;
3114 get_constraint_for_rhs (ptr
, results
);
3118 /* As we are eventually appending to the solution do not use
3119 vec::iterate here. */
3120 n
= results
->length ();
3121 for (j
= 0; j
< n
; j
++)
3125 curr
= get_varinfo (c
.var
);
3127 if (c
.type
== ADDRESSOF
3128 /* If this varinfo represents a full variable just use it. */
3129 && curr
->is_full_var
)
3131 else if (c
.type
== ADDRESSOF
3132 /* If we do not know the offset add all subfields. */
3133 && rhsoffset
== UNKNOWN_OFFSET
)
3135 varinfo_t temp
= get_varinfo (curr
->head
);
3138 struct constraint_expr c2
;
3140 c2
.type
= ADDRESSOF
;
3142 if (c2
.var
!= c
.var
)
3143 results
->safe_push (c2
);
3144 temp
= vi_next (temp
);
3148 else if (c
.type
== ADDRESSOF
)
3151 unsigned HOST_WIDE_INT offset
= curr
->offset
+ rhsoffset
;
3153 /* If curr->offset + rhsoffset is less than zero adjust it. */
3155 && curr
->offset
< offset
)
3158 /* We have to include all fields that overlap the current
3159 field shifted by rhsoffset. And we include at least
3160 the last or the first field of the variable to represent
3161 reachability of off-bound addresses, in particular &object + 1,
3162 conservatively correct. */
3163 temp
= first_or_preceding_vi_for_offset (curr
, offset
);
3166 temp
= vi_next (temp
);
3168 && temp
->offset
< offset
+ curr
->size
)
3170 struct constraint_expr c2
;
3172 c2
.type
= ADDRESSOF
;
3174 results
->safe_push (c2
);
3175 temp
= vi_next (temp
);
3178 else if (c
.type
== SCALAR
)
3180 gcc_assert (c
.offset
== 0);
3181 c
.offset
= rhsoffset
;
3184 /* We shouldn't get any DEREFs here. */
3192 /* Given a COMPONENT_REF T, return the constraint_expr vector for it.
3193 If address_p is true the result will be taken its address of.
3194 If lhs_p is true then the constraint expression is assumed to be used
3198 get_constraint_for_component_ref (tree t
, vec
<ce_s
> *results
,
3199 bool address_p
, bool lhs_p
)
3202 HOST_WIDE_INT bitsize
= -1;
3203 HOST_WIDE_INT bitmaxsize
= -1;
3204 HOST_WIDE_INT bitpos
;
3208 /* Some people like to do cute things like take the address of
3211 while (handled_component_p (forzero
)
3212 || INDIRECT_REF_P (forzero
)
3213 || TREE_CODE (forzero
) == MEM_REF
)
3214 forzero
= TREE_OPERAND (forzero
, 0);
3216 if (CONSTANT_CLASS_P (forzero
) && integer_zerop (forzero
))
3218 struct constraint_expr temp
;
3221 temp
.var
= integer_id
;
3223 results
->safe_push (temp
);
3227 t
= get_ref_base_and_extent (t
, &bitpos
, &bitsize
, &bitmaxsize
, &reverse
);
3229 /* We can end up here for component references on a
3230 VIEW_CONVERT_EXPR <>(&foobar) or things like a
3231 BIT_FIELD_REF <&MEM[(void *)&b + 4B], ...>. So for
3232 symbolic constants simply give up. */
3233 if (TREE_CODE (t
) == ADDR_EXPR
)
3235 constraint_expr result
;
3236 result
.type
= SCALAR
;
3237 result
.var
= anything_id
;
3239 results
->safe_push (result
);
3243 /* Pretend to take the address of the base, we'll take care of
3244 adding the required subset of sub-fields below. */
3245 get_constraint_for_1 (t
, results
, true, lhs_p
);
3246 /* Strip off nothing_id. */
3247 if (results
->length () == 2)
3249 gcc_assert ((*results
)[0].var
== nothing_id
);
3250 results
->unordered_remove (0);
3252 gcc_assert (results
->length () == 1);
3253 struct constraint_expr
&result
= results
->last ();
3255 if (result
.type
== SCALAR
3256 && get_varinfo (result
.var
)->is_full_var
)
3257 /* For single-field vars do not bother about the offset. */
3259 else if (result
.type
== SCALAR
)
3261 /* In languages like C, you can access one past the end of an
3262 array. You aren't allowed to dereference it, so we can
3263 ignore this constraint. When we handle pointer subtraction,
3264 we may have to do something cute here. */
3266 if ((unsigned HOST_WIDE_INT
)bitpos
< get_varinfo (result
.var
)->fullsize
3269 /* It's also not true that the constraint will actually start at the
3270 right offset, it may start in some padding. We only care about
3271 setting the constraint to the first actual field it touches, so
3273 struct constraint_expr cexpr
= result
;
3277 for (curr
= get_varinfo (cexpr
.var
); curr
; curr
= vi_next (curr
))
3279 if (ranges_overlap_p (curr
->offset
, curr
->size
,
3280 bitpos
, bitmaxsize
))
3282 cexpr
.var
= curr
->id
;
3283 results
->safe_push (cexpr
);
3288 /* If we are going to take the address of this field then
3289 to be able to compute reachability correctly add at least
3290 the last field of the variable. */
3291 if (address_p
&& results
->length () == 0)
3293 curr
= get_varinfo (cexpr
.var
);
3294 while (curr
->next
!= 0)
3295 curr
= vi_next (curr
);
3296 cexpr
.var
= curr
->id
;
3297 results
->safe_push (cexpr
);
3299 else if (results
->length () == 0)
3300 /* Assert that we found *some* field there. The user couldn't be
3301 accessing *only* padding. */
3302 /* Still the user could access one past the end of an array
3303 embedded in a struct resulting in accessing *only* padding. */
3304 /* Or accessing only padding via type-punning to a type
3305 that has a filed just in padding space. */
3307 cexpr
.type
= SCALAR
;
3308 cexpr
.var
= anything_id
;
3310 results
->safe_push (cexpr
);
3313 else if (bitmaxsize
== 0)
3315 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3316 fprintf (dump_file
, "Access to zero-sized part of variable, "
3320 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3321 fprintf (dump_file
, "Access to past the end of variable, ignoring\n");
3323 else if (result
.type
== DEREF
)
3325 /* If we do not know exactly where the access goes say so. Note
3326 that only for non-structure accesses we know that we access
3327 at most one subfiled of any variable. */
3329 || bitsize
!= bitmaxsize
3330 || AGGREGATE_TYPE_P (TREE_TYPE (orig_t
))
3331 || result
.offset
== UNKNOWN_OFFSET
)
3332 result
.offset
= UNKNOWN_OFFSET
;
3334 result
.offset
+= bitpos
;
3336 else if (result
.type
== ADDRESSOF
)
3338 /* We can end up here for component references on constants like
3339 VIEW_CONVERT_EXPR <>({ 0, 1, 2, 3 })[i]. */
3340 result
.type
= SCALAR
;
3341 result
.var
= anything_id
;
3349 /* Dereference the constraint expression CONS, and return the result.
3350 DEREF (ADDRESSOF) = SCALAR
3351 DEREF (SCALAR) = DEREF
3352 DEREF (DEREF) = (temp = DEREF1; result = DEREF(temp))
3353 This is needed so that we can handle dereferencing DEREF constraints. */
3356 do_deref (vec
<ce_s
> *constraints
)
3358 struct constraint_expr
*c
;
3361 FOR_EACH_VEC_ELT (*constraints
, i
, c
)
3363 if (c
->type
== SCALAR
)
3365 else if (c
->type
== ADDRESSOF
)
3367 else if (c
->type
== DEREF
)
3369 struct constraint_expr tmplhs
;
3370 tmplhs
= new_scalar_tmp_constraint_exp ("dereftmp", true);
3371 process_constraint (new_constraint (tmplhs
, *c
));
3372 c
->var
= tmplhs
.var
;
3379 /* Given a tree T, return the constraint expression for taking the
3383 get_constraint_for_address_of (tree t
, vec
<ce_s
> *results
)
3385 struct constraint_expr
*c
;
3388 get_constraint_for_1 (t
, results
, true, true);
3390 FOR_EACH_VEC_ELT (*results
, i
, c
)
3392 if (c
->type
== DEREF
)
3395 c
->type
= ADDRESSOF
;
3399 /* Given a tree T, return the constraint expression for it. */
3402 get_constraint_for_1 (tree t
, vec
<ce_s
> *results
, bool address_p
,
3405 struct constraint_expr temp
;
3407 /* x = integer is all glommed to a single variable, which doesn't
3408 point to anything by itself. That is, of course, unless it is an
3409 integer constant being treated as a pointer, in which case, we
3410 will return that this is really the addressof anything. This
3411 happens below, since it will fall into the default case. The only
3412 case we know something about an integer treated like a pointer is
3413 when it is the NULL pointer, and then we just say it points to
3416 Do not do that if -fno-delete-null-pointer-checks though, because
3417 in that case *NULL does not fail, so it _should_ alias *anything.
3418 It is not worth adding a new option or renaming the existing one,
3419 since this case is relatively obscure. */
3420 if ((TREE_CODE (t
) == INTEGER_CST
3421 && integer_zerop (t
))
3422 /* The only valid CONSTRUCTORs in gimple with pointer typed
3423 elements are zero-initializer. But in IPA mode we also
3424 process global initializers, so verify at least. */
3425 || (TREE_CODE (t
) == CONSTRUCTOR
3426 && CONSTRUCTOR_NELTS (t
) == 0))
3428 if (flag_delete_null_pointer_checks
)
3429 temp
.var
= nothing_id
;
3431 temp
.var
= nonlocal_id
;
3432 temp
.type
= ADDRESSOF
;
3434 results
->safe_push (temp
);
3438 /* String constants are read-only, ideally we'd have a CONST_DECL
3440 if (TREE_CODE (t
) == STRING_CST
)
3442 temp
.var
= string_id
;
3445 results
->safe_push (temp
);
3449 switch (TREE_CODE_CLASS (TREE_CODE (t
)))
3451 case tcc_expression
:
3453 switch (TREE_CODE (t
))
3456 get_constraint_for_address_of (TREE_OPERAND (t
, 0), results
);
3464 switch (TREE_CODE (t
))
3468 struct constraint_expr cs
;
3470 get_constraint_for_ptr_offset (TREE_OPERAND (t
, 0),
3471 TREE_OPERAND (t
, 1), results
);
3474 /* If we are not taking the address then make sure to process
3475 all subvariables we might access. */
3479 cs
= results
->last ();
3480 if (cs
.type
== DEREF
3481 && type_can_have_subvars (TREE_TYPE (t
)))
3483 /* For dereferences this means we have to defer it
3485 results
->last ().offset
= UNKNOWN_OFFSET
;
3488 if (cs
.type
!= SCALAR
)
3491 vi
= get_varinfo (cs
.var
);
3492 curr
= vi_next (vi
);
3493 if (!vi
->is_full_var
3496 unsigned HOST_WIDE_INT size
;
3497 if (tree_fits_uhwi_p (TYPE_SIZE (TREE_TYPE (t
))))
3498 size
= tree_to_uhwi (TYPE_SIZE (TREE_TYPE (t
)));
3501 for (; curr
; curr
= vi_next (curr
))
3503 if (curr
->offset
- vi
->offset
< size
)
3506 results
->safe_push (cs
);
3515 case ARRAY_RANGE_REF
:
3520 get_constraint_for_component_ref (t
, results
, address_p
, lhs_p
);
3522 case VIEW_CONVERT_EXPR
:
3523 get_constraint_for_1 (TREE_OPERAND (t
, 0), results
, address_p
,
3526 /* We are missing handling for TARGET_MEM_REF here. */
3531 case tcc_exceptional
:
3533 switch (TREE_CODE (t
))
3537 get_constraint_for_ssa_var (t
, results
, address_p
);
3545 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t
), i
, val
)
3547 struct constraint_expr
*rhsp
;
3549 get_constraint_for_1 (val
, &tmp
, address_p
, lhs_p
);
3550 FOR_EACH_VEC_ELT (tmp
, j
, rhsp
)
3551 results
->safe_push (*rhsp
);
3554 /* We do not know whether the constructor was complete,
3555 so technically we have to add &NOTHING or &ANYTHING
3556 like we do for an empty constructor as well. */
3563 case tcc_declaration
:
3565 get_constraint_for_ssa_var (t
, results
, address_p
);
3570 /* We cannot refer to automatic variables through constants. */
3571 temp
.type
= ADDRESSOF
;
3572 temp
.var
= nonlocal_id
;
3574 results
->safe_push (temp
);
3580 /* The default fallback is a constraint from anything. */
3581 temp
.type
= ADDRESSOF
;
3582 temp
.var
= anything_id
;
3584 results
->safe_push (temp
);
3587 /* Given a gimple tree T, return the constraint expression vector for it. */
3590 get_constraint_for (tree t
, vec
<ce_s
> *results
)
3592 gcc_assert (results
->length () == 0);
3594 get_constraint_for_1 (t
, results
, false, true);
3597 /* Given a gimple tree T, return the constraint expression vector for it
3598 to be used as the rhs of a constraint. */
3601 get_constraint_for_rhs (tree t
, vec
<ce_s
> *results
)
3603 gcc_assert (results
->length () == 0);
3605 get_constraint_for_1 (t
, results
, false, false);
3609 /* Efficiently generates constraints from all entries in *RHSC to all
3610 entries in *LHSC. */
3613 process_all_all_constraints (vec
<ce_s
> lhsc
,
3616 struct constraint_expr
*lhsp
, *rhsp
;
3619 if (lhsc
.length () <= 1 || rhsc
.length () <= 1)
3621 FOR_EACH_VEC_ELT (lhsc
, i
, lhsp
)
3622 FOR_EACH_VEC_ELT (rhsc
, j
, rhsp
)
3623 process_constraint (new_constraint (*lhsp
, *rhsp
));
3627 struct constraint_expr tmp
;
3628 tmp
= new_scalar_tmp_constraint_exp ("allalltmp", true);
3629 FOR_EACH_VEC_ELT (rhsc
, i
, rhsp
)
3630 process_constraint (new_constraint (tmp
, *rhsp
));
3631 FOR_EACH_VEC_ELT (lhsc
, i
, lhsp
)
3632 process_constraint (new_constraint (*lhsp
, tmp
));
3636 /* Handle aggregate copies by expanding into copies of the respective
3637 fields of the structures. */
3640 do_structure_copy (tree lhsop
, tree rhsop
)
3642 struct constraint_expr
*lhsp
, *rhsp
;
3643 auto_vec
<ce_s
> lhsc
;
3644 auto_vec
<ce_s
> rhsc
;
3647 get_constraint_for (lhsop
, &lhsc
);
3648 get_constraint_for_rhs (rhsop
, &rhsc
);
3651 if (lhsp
->type
== DEREF
3652 || (lhsp
->type
== ADDRESSOF
&& lhsp
->var
== anything_id
)
3653 || rhsp
->type
== DEREF
)
3655 if (lhsp
->type
== DEREF
)
3657 gcc_assert (lhsc
.length () == 1);
3658 lhsp
->offset
= UNKNOWN_OFFSET
;
3660 if (rhsp
->type
== DEREF
)
3662 gcc_assert (rhsc
.length () == 1);
3663 rhsp
->offset
= UNKNOWN_OFFSET
;
3665 process_all_all_constraints (lhsc
, rhsc
);
3667 else if (lhsp
->type
== SCALAR
3668 && (rhsp
->type
== SCALAR
3669 || rhsp
->type
== ADDRESSOF
))
3671 HOST_WIDE_INT lhssize
, lhsmaxsize
, lhsoffset
;
3672 HOST_WIDE_INT rhssize
, rhsmaxsize
, rhsoffset
;
3675 get_ref_base_and_extent (lhsop
, &lhsoffset
, &lhssize
, &lhsmaxsize
,
3677 get_ref_base_and_extent (rhsop
, &rhsoffset
, &rhssize
, &rhsmaxsize
,
3679 for (j
= 0; lhsc
.iterate (j
, &lhsp
);)
3681 varinfo_t lhsv
, rhsv
;
3683 lhsv
= get_varinfo (lhsp
->var
);
3684 rhsv
= get_varinfo (rhsp
->var
);
3685 if (lhsv
->may_have_pointers
3686 && (lhsv
->is_full_var
3687 || rhsv
->is_full_var
3688 || ranges_overlap_p (lhsv
->offset
+ rhsoffset
, lhsv
->size
,
3689 rhsv
->offset
+ lhsoffset
, rhsv
->size
)))
3690 process_constraint (new_constraint (*lhsp
, *rhsp
));
3691 if (!rhsv
->is_full_var
3692 && (lhsv
->is_full_var
3693 || (lhsv
->offset
+ rhsoffset
+ lhsv
->size
3694 > rhsv
->offset
+ lhsoffset
+ rhsv
->size
)))
3697 if (k
>= rhsc
.length ())
3708 /* Create constraints ID = { rhsc }. */
3711 make_constraints_to (unsigned id
, vec
<ce_s
> rhsc
)
3713 struct constraint_expr
*c
;
3714 struct constraint_expr includes
;
3718 includes
.offset
= 0;
3719 includes
.type
= SCALAR
;
3721 FOR_EACH_VEC_ELT (rhsc
, j
, c
)
3722 process_constraint (new_constraint (includes
, *c
));
3725 /* Create a constraint ID = OP. */
3728 make_constraint_to (unsigned id
, tree op
)
3730 auto_vec
<ce_s
> rhsc
;
3731 get_constraint_for_rhs (op
, &rhsc
);
3732 make_constraints_to (id
, rhsc
);
3735 /* Create a constraint ID = &FROM. */
3738 make_constraint_from (varinfo_t vi
, int from
)
3740 struct constraint_expr lhs
, rhs
;
3748 rhs
.type
= ADDRESSOF
;
3749 process_constraint (new_constraint (lhs
, rhs
));
3752 /* Create a constraint ID = FROM. */
3755 make_copy_constraint (varinfo_t vi
, int from
)
3757 struct constraint_expr lhs
, rhs
;
3766 process_constraint (new_constraint (lhs
, rhs
));
3769 /* Make constraints necessary to make OP escape. */
3772 make_escape_constraint (tree op
)
3774 make_constraint_to (escaped_id
, op
);
3777 /* Add constraints to that the solution of VI is transitively closed. */
3780 make_transitive_closure_constraints (varinfo_t vi
)
3782 struct constraint_expr lhs
, rhs
;
3784 /* VAR = *(VAR + UNKNOWN); */
3790 rhs
.offset
= UNKNOWN_OFFSET
;
3791 process_constraint (new_constraint (lhs
, rhs
));
3794 /* Add constraints to that the solution of VI has all subvariables added. */
3797 make_any_offset_constraints (varinfo_t vi
)
3799 struct constraint_expr lhs
, rhs
;
3801 /* VAR = VAR + UNKNOWN; */
3807 rhs
.offset
= UNKNOWN_OFFSET
;
3808 process_constraint (new_constraint (lhs
, rhs
));
3811 /* Temporary storage for fake var decls. */
3812 struct obstack fake_var_decl_obstack
;
3814 /* Build a fake VAR_DECL acting as referrer to a DECL_UID. */
3817 build_fake_var_decl (tree type
)
3819 tree decl
= (tree
) XOBNEW (&fake_var_decl_obstack
, struct tree_var_decl
);
3820 memset (decl
, 0, sizeof (struct tree_var_decl
));
3821 TREE_SET_CODE (decl
, VAR_DECL
);
3822 TREE_TYPE (decl
) = type
;
3823 DECL_UID (decl
) = allocate_decl_uid ();
3824 SET_DECL_PT_UID (decl
, -1);
3825 layout_decl (decl
, 0);
3829 /* Create a new artificial heap variable with NAME.
3830 Return the created variable. */
3833 make_heapvar (const char *name
, bool add_id
)
3838 heapvar
= build_fake_var_decl (ptr_type_node
);
3839 DECL_EXTERNAL (heapvar
) = 1;
3841 vi
= new_var_info (heapvar
, name
, add_id
);
3842 vi
->is_artificial_var
= true;
3843 vi
->is_heap_var
= true;
3844 vi
->is_unknown_size_var
= true;
3848 vi
->is_full_var
= true;
3849 insert_vi_for_tree (heapvar
, vi
);
3854 /* Create a new artificial heap variable with NAME and make a
3855 constraint from it to LHS. Set flags according to a tag used
3856 for tracking restrict pointers. */
3859 make_constraint_from_restrict (varinfo_t lhs
, const char *name
, bool add_id
)
3861 varinfo_t vi
= make_heapvar (name
, add_id
);
3862 vi
->is_restrict_var
= 1;
3863 vi
->is_global_var
= 1;
3864 vi
->may_have_pointers
= 1;
3865 make_constraint_from (lhs
, vi
->id
);
3869 /* Create a new artificial heap variable with NAME and make a
3870 constraint from it to LHS. Set flags according to a tag used
3871 for tracking restrict pointers and make the artificial heap
3872 point to global memory. */
3875 make_constraint_from_global_restrict (varinfo_t lhs
, const char *name
,
3878 varinfo_t vi
= make_constraint_from_restrict (lhs
, name
, add_id
);
3879 make_copy_constraint (vi
, nonlocal_id
);
3883 /* In IPA mode there are varinfos for different aspects of reach
3884 function designator. One for the points-to set of the return
3885 value, one for the variables that are clobbered by the function,
3886 one for its uses and one for each parameter (including a single
3887 glob for remaining variadic arguments). */
3889 enum { fi_clobbers
= 1, fi_uses
= 2,
3890 fi_static_chain
= 3, fi_result
= 4, fi_parm_base
= 5 };
3892 /* Get a constraint for the requested part of a function designator FI
3893 when operating in IPA mode. */
3895 static struct constraint_expr
3896 get_function_part_constraint (varinfo_t fi
, unsigned part
)
3898 struct constraint_expr c
;
3900 gcc_assert (in_ipa_mode
);
3902 if (fi
->id
== anything_id
)
3904 /* ??? We probably should have a ANYFN special variable. */
3905 c
.var
= anything_id
;
3909 else if (TREE_CODE (fi
->decl
) == FUNCTION_DECL
)
3911 varinfo_t ai
= first_vi_for_offset (fi
, part
);
3915 c
.var
= anything_id
;
3929 /* For non-IPA mode, generate constraints necessary for a call on the
3933 handle_rhs_call (gcall
*stmt
, vec
<ce_s
> *results
)
3935 struct constraint_expr rhsc
;
3937 bool returns_uses
= false;
3939 for (i
= 0; i
< gimple_call_num_args (stmt
); ++i
)
3941 tree arg
= gimple_call_arg (stmt
, i
);
3942 int flags
= gimple_call_arg_flags (stmt
, i
);
3944 /* If the argument is not used we can ignore it. */
3945 if (flags
& EAF_UNUSED
)
3948 /* As we compute ESCAPED context-insensitive we do not gain
3949 any precision with just EAF_NOCLOBBER but not EAF_NOESCAPE
3950 set. The argument would still get clobbered through the
3952 if ((flags
& EAF_NOCLOBBER
)
3953 && (flags
& EAF_NOESCAPE
))
3955 varinfo_t uses
= get_call_use_vi (stmt
);
3956 varinfo_t tem
= new_var_info (NULL_TREE
, "callarg", true);
3957 tem
->is_reg_var
= true;
3958 make_constraint_to (tem
->id
, arg
);
3959 make_any_offset_constraints (tem
);
3960 if (!(flags
& EAF_DIRECT
))
3961 make_transitive_closure_constraints (tem
);
3962 make_copy_constraint (uses
, tem
->id
);
3963 returns_uses
= true;
3965 else if (flags
& EAF_NOESCAPE
)
3967 struct constraint_expr lhs
, rhs
;
3968 varinfo_t uses
= get_call_use_vi (stmt
);
3969 varinfo_t clobbers
= get_call_clobber_vi (stmt
);
3970 varinfo_t tem
= new_var_info (NULL_TREE
, "callarg", true);
3971 tem
->is_reg_var
= true;
3972 make_constraint_to (tem
->id
, arg
);
3973 make_any_offset_constraints (tem
);
3974 if (!(flags
& EAF_DIRECT
))
3975 make_transitive_closure_constraints (tem
);
3976 make_copy_constraint (uses
, tem
->id
);
3977 make_copy_constraint (clobbers
, tem
->id
);
3978 /* Add *tem = nonlocal, do not add *tem = callused as
3979 EAF_NOESCAPE parameters do not escape to other parameters
3980 and all other uses appear in NONLOCAL as well. */
3985 rhs
.var
= nonlocal_id
;
3987 process_constraint (new_constraint (lhs
, rhs
));
3988 returns_uses
= true;
3991 make_escape_constraint (arg
);
3994 /* If we added to the calls uses solution make sure we account for
3995 pointers to it to be returned. */
3998 rhsc
.var
= get_call_use_vi (stmt
)->id
;
3999 rhsc
.offset
= UNKNOWN_OFFSET
;
4001 results
->safe_push (rhsc
);
4004 /* The static chain escapes as well. */
4005 if (gimple_call_chain (stmt
))
4006 make_escape_constraint (gimple_call_chain (stmt
));
4008 /* And if we applied NRV the address of the return slot escapes as well. */
4009 if (gimple_call_return_slot_opt_p (stmt
)
4010 && gimple_call_lhs (stmt
) != NULL_TREE
4011 && TREE_ADDRESSABLE (TREE_TYPE (gimple_call_lhs (stmt
))))
4013 auto_vec
<ce_s
> tmpc
;
4014 struct constraint_expr lhsc
, *c
;
4015 get_constraint_for_address_of (gimple_call_lhs (stmt
), &tmpc
);
4016 lhsc
.var
= escaped_id
;
4019 FOR_EACH_VEC_ELT (tmpc
, i
, c
)
4020 process_constraint (new_constraint (lhsc
, *c
));
4023 /* Regular functions return nonlocal memory. */
4024 rhsc
.var
= nonlocal_id
;
4027 results
->safe_push (rhsc
);
4030 /* For non-IPA mode, generate constraints necessary for a call
4031 that returns a pointer and assigns it to LHS. This simply makes
4032 the LHS point to global and escaped variables. */
4035 handle_lhs_call (gcall
*stmt
, tree lhs
, int flags
, vec
<ce_s
> rhsc
,
4038 auto_vec
<ce_s
> lhsc
;
4040 get_constraint_for (lhs
, &lhsc
);
4041 /* If the store is to a global decl make sure to
4042 add proper escape constraints. */
4043 lhs
= get_base_address (lhs
);
4046 && is_global_var (lhs
))
4048 struct constraint_expr tmpc
;
4049 tmpc
.var
= escaped_id
;
4052 lhsc
.safe_push (tmpc
);
4055 /* If the call returns an argument unmodified override the rhs
4057 if (flags
& ERF_RETURNS_ARG
4058 && (flags
& ERF_RETURN_ARG_MASK
) < gimple_call_num_args (stmt
))
4062 arg
= gimple_call_arg (stmt
, flags
& ERF_RETURN_ARG_MASK
);
4063 get_constraint_for (arg
, &rhsc
);
4064 process_all_all_constraints (lhsc
, rhsc
);
4067 else if (flags
& ERF_NOALIAS
)
4070 struct constraint_expr tmpc
;
4072 vi
= make_heapvar ("HEAP", true);
4073 /* We are marking allocated storage local, we deal with it becoming
4074 global by escaping and setting of vars_contains_escaped_heap. */
4075 DECL_EXTERNAL (vi
->decl
) = 0;
4076 vi
->is_global_var
= 0;
4077 /* If this is not a real malloc call assume the memory was
4078 initialized and thus may point to global memory. All
4079 builtin functions with the malloc attribute behave in a sane way. */
4081 || DECL_BUILT_IN_CLASS (fndecl
) != BUILT_IN_NORMAL
)
4082 make_constraint_from (vi
, nonlocal_id
);
4085 tmpc
.type
= ADDRESSOF
;
4086 rhsc
.safe_push (tmpc
);
4087 process_all_all_constraints (lhsc
, rhsc
);
4091 process_all_all_constraints (lhsc
, rhsc
);
4094 /* For non-IPA mode, generate constraints necessary for a call of a
4095 const function that returns a pointer in the statement STMT. */
4098 handle_const_call (gcall
*stmt
, vec
<ce_s
> *results
)
4100 struct constraint_expr rhsc
;
4102 bool need_uses
= false;
4104 /* Treat nested const functions the same as pure functions as far
4105 as the static chain is concerned. */
4106 if (gimple_call_chain (stmt
))
4108 varinfo_t uses
= get_call_use_vi (stmt
);
4109 make_constraint_to (uses
->id
, gimple_call_chain (stmt
));
4113 /* And if we applied NRV the address of the return slot escapes as well. */
4114 if (gimple_call_return_slot_opt_p (stmt
)
4115 && gimple_call_lhs (stmt
) != NULL_TREE
4116 && TREE_ADDRESSABLE (TREE_TYPE (gimple_call_lhs (stmt
))))
4118 varinfo_t uses
= get_call_use_vi (stmt
);
4119 auto_vec
<ce_s
> tmpc
;
4120 get_constraint_for_address_of (gimple_call_lhs (stmt
), &tmpc
);
4121 make_constraints_to (uses
->id
, tmpc
);
4127 varinfo_t uses
= get_call_use_vi (stmt
);
4128 make_any_offset_constraints (uses
);
4129 make_transitive_closure_constraints (uses
);
4130 rhsc
.var
= uses
->id
;
4133 results
->safe_push (rhsc
);
4136 /* May return offsetted arguments. */
4137 varinfo_t tem
= NULL
;
4138 if (gimple_call_num_args (stmt
) != 0)
4140 tem
= new_var_info (NULL_TREE
, "callarg", true);
4141 tem
->is_reg_var
= true;
4143 for (k
= 0; k
< gimple_call_num_args (stmt
); ++k
)
4145 tree arg
= gimple_call_arg (stmt
, k
);
4146 auto_vec
<ce_s
> argc
;
4147 get_constraint_for_rhs (arg
, &argc
);
4148 make_constraints_to (tem
->id
, argc
);
4155 ce
.offset
= UNKNOWN_OFFSET
;
4156 results
->safe_push (ce
);
4159 /* May return addresses of globals. */
4160 rhsc
.var
= nonlocal_id
;
4162 rhsc
.type
= ADDRESSOF
;
4163 results
->safe_push (rhsc
);
4166 /* For non-IPA mode, generate constraints necessary for a call to a
4167 pure function in statement STMT. */
4170 handle_pure_call (gcall
*stmt
, vec
<ce_s
> *results
)
4172 struct constraint_expr rhsc
;
4174 varinfo_t uses
= NULL
;
4176 /* Memory reached from pointer arguments is call-used. */
4177 for (i
= 0; i
< gimple_call_num_args (stmt
); ++i
)
4179 tree arg
= gimple_call_arg (stmt
, i
);
4182 uses
= get_call_use_vi (stmt
);
4183 make_any_offset_constraints (uses
);
4184 make_transitive_closure_constraints (uses
);
4186 make_constraint_to (uses
->id
, arg
);
4189 /* The static chain is used as well. */
4190 if (gimple_call_chain (stmt
))
4194 uses
= get_call_use_vi (stmt
);
4195 make_any_offset_constraints (uses
);
4196 make_transitive_closure_constraints (uses
);
4198 make_constraint_to (uses
->id
, gimple_call_chain (stmt
));
4201 /* And if we applied NRV the address of the return slot. */
4202 if (gimple_call_return_slot_opt_p (stmt
)
4203 && gimple_call_lhs (stmt
) != NULL_TREE
4204 && TREE_ADDRESSABLE (TREE_TYPE (gimple_call_lhs (stmt
))))
4208 uses
= get_call_use_vi (stmt
);
4209 make_any_offset_constraints (uses
);
4210 make_transitive_closure_constraints (uses
);
4212 auto_vec
<ce_s
> tmpc
;
4213 get_constraint_for_address_of (gimple_call_lhs (stmt
), &tmpc
);
4214 make_constraints_to (uses
->id
, tmpc
);
4217 /* Pure functions may return call-used and nonlocal memory. */
4220 rhsc
.var
= uses
->id
;
4223 results
->safe_push (rhsc
);
4225 rhsc
.var
= nonlocal_id
;
4228 results
->safe_push (rhsc
);
4232 /* Return the varinfo for the callee of CALL. */
4235 get_fi_for_callee (gcall
*call
)
4237 tree decl
, fn
= gimple_call_fn (call
);
4239 if (fn
&& TREE_CODE (fn
) == OBJ_TYPE_REF
)
4240 fn
= OBJ_TYPE_REF_EXPR (fn
);
4242 /* If we can directly resolve the function being called, do so.
4243 Otherwise, it must be some sort of indirect expression that
4244 we should still be able to handle. */
4245 decl
= gimple_call_addr_fndecl (fn
);
4247 return get_vi_for_tree (decl
);
4249 /* If the function is anything other than a SSA name pointer we have no
4250 clue and should be getting ANYFN (well, ANYTHING for now). */
4251 if (!fn
|| TREE_CODE (fn
) != SSA_NAME
)
4252 return get_varinfo (anything_id
);
4254 if (SSA_NAME_IS_DEFAULT_DEF (fn
)
4255 && (TREE_CODE (SSA_NAME_VAR (fn
)) == PARM_DECL
4256 || TREE_CODE (SSA_NAME_VAR (fn
)) == RESULT_DECL
))
4257 fn
= SSA_NAME_VAR (fn
);
4259 return get_vi_for_tree (fn
);
4262 /* Create constraints for assigning call argument ARG to the incoming parameter
4263 INDEX of function FI. */
4266 find_func_aliases_for_call_arg (varinfo_t fi
, unsigned index
, tree arg
)
4268 struct constraint_expr lhs
;
4269 lhs
= get_function_part_constraint (fi
, fi_parm_base
+ index
);
4271 auto_vec
<ce_s
, 2> rhsc
;
4272 get_constraint_for_rhs (arg
, &rhsc
);
4275 struct constraint_expr
*rhsp
;
4276 FOR_EACH_VEC_ELT (rhsc
, j
, rhsp
)
4277 process_constraint (new_constraint (lhs
, *rhsp
));
4280 /* Return true if FNDECL may be part of another lto partition. */
4283 fndecl_maybe_in_other_partition (tree fndecl
)
4285 cgraph_node
*fn_node
= cgraph_node::get (fndecl
);
4286 if (fn_node
== NULL
)
4289 return fn_node
->in_other_partition
;
4292 /* Create constraints for the builtin call T. Return true if the call
4293 was handled, otherwise false. */
4296 find_func_aliases_for_builtin_call (struct function
*fn
, gcall
*t
)
4298 tree fndecl
= gimple_call_fndecl (t
);
4299 auto_vec
<ce_s
, 2> lhsc
;
4300 auto_vec
<ce_s
, 4> rhsc
;
4303 if (gimple_call_builtin_p (t
, BUILT_IN_NORMAL
))
4304 /* ??? All builtins that are handled here need to be handled
4305 in the alias-oracle query functions explicitly! */
4306 switch (DECL_FUNCTION_CODE (fndecl
))
4308 /* All the following functions return a pointer to the same object
4309 as their first argument points to. The functions do not add
4310 to the ESCAPED solution. The functions make the first argument
4311 pointed to memory point to what the second argument pointed to
4312 memory points to. */
4313 case BUILT_IN_STRCPY
:
4314 case BUILT_IN_STRNCPY
:
4315 case BUILT_IN_BCOPY
:
4316 case BUILT_IN_MEMCPY
:
4317 case BUILT_IN_MEMMOVE
:
4318 case BUILT_IN_MEMPCPY
:
4319 case BUILT_IN_STPCPY
:
4320 case BUILT_IN_STPNCPY
:
4321 case BUILT_IN_STRCAT
:
4322 case BUILT_IN_STRNCAT
:
4323 case BUILT_IN_STRCPY_CHK
:
4324 case BUILT_IN_STRNCPY_CHK
:
4325 case BUILT_IN_MEMCPY_CHK
:
4326 case BUILT_IN_MEMMOVE_CHK
:
4327 case BUILT_IN_MEMPCPY_CHK
:
4328 case BUILT_IN_STPCPY_CHK
:
4329 case BUILT_IN_STPNCPY_CHK
:
4330 case BUILT_IN_STRCAT_CHK
:
4331 case BUILT_IN_STRNCAT_CHK
:
4332 case BUILT_IN_TM_MEMCPY
:
4333 case BUILT_IN_TM_MEMMOVE
:
4335 tree res
= gimple_call_lhs (t
);
4336 tree dest
= gimple_call_arg (t
, (DECL_FUNCTION_CODE (fndecl
)
4337 == BUILT_IN_BCOPY
? 1 : 0));
4338 tree src
= gimple_call_arg (t
, (DECL_FUNCTION_CODE (fndecl
)
4339 == BUILT_IN_BCOPY
? 0 : 1));
4340 if (res
!= NULL_TREE
)
4342 get_constraint_for (res
, &lhsc
);
4343 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_MEMPCPY
4344 || DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_STPCPY
4345 || DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_STPNCPY
4346 || DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_MEMPCPY_CHK
4347 || DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_STPCPY_CHK
4348 || DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_STPNCPY_CHK
)
4349 get_constraint_for_ptr_offset (dest
, NULL_TREE
, &rhsc
);
4351 get_constraint_for (dest
, &rhsc
);
4352 process_all_all_constraints (lhsc
, rhsc
);
4356 get_constraint_for_ptr_offset (dest
, NULL_TREE
, &lhsc
);
4357 get_constraint_for_ptr_offset (src
, NULL_TREE
, &rhsc
);
4360 process_all_all_constraints (lhsc
, rhsc
);
4363 case BUILT_IN_MEMSET
:
4364 case BUILT_IN_MEMSET_CHK
:
4365 case BUILT_IN_TM_MEMSET
:
4367 tree res
= gimple_call_lhs (t
);
4368 tree dest
= gimple_call_arg (t
, 0);
4371 struct constraint_expr ac
;
4372 if (res
!= NULL_TREE
)
4374 get_constraint_for (res
, &lhsc
);
4375 get_constraint_for (dest
, &rhsc
);
4376 process_all_all_constraints (lhsc
, rhsc
);
4379 get_constraint_for_ptr_offset (dest
, NULL_TREE
, &lhsc
);
4381 if (flag_delete_null_pointer_checks
4382 && integer_zerop (gimple_call_arg (t
, 1)))
4384 ac
.type
= ADDRESSOF
;
4385 ac
.var
= nothing_id
;
4390 ac
.var
= integer_id
;
4393 FOR_EACH_VEC_ELT (lhsc
, i
, lhsp
)
4394 process_constraint (new_constraint (*lhsp
, ac
));
4397 case BUILT_IN_POSIX_MEMALIGN
:
4399 tree ptrptr
= gimple_call_arg (t
, 0);
4400 get_constraint_for (ptrptr
, &lhsc
);
4402 varinfo_t vi
= make_heapvar ("HEAP", true);
4403 /* We are marking allocated storage local, we deal with it becoming
4404 global by escaping and setting of vars_contains_escaped_heap. */
4405 DECL_EXTERNAL (vi
->decl
) = 0;
4406 vi
->is_global_var
= 0;
4407 struct constraint_expr tmpc
;
4410 tmpc
.type
= ADDRESSOF
;
4411 rhsc
.safe_push (tmpc
);
4412 process_all_all_constraints (lhsc
, rhsc
);
4415 case BUILT_IN_ASSUME_ALIGNED
:
4417 tree res
= gimple_call_lhs (t
);
4418 tree dest
= gimple_call_arg (t
, 0);
4419 if (res
!= NULL_TREE
)
4421 get_constraint_for (res
, &lhsc
);
4422 get_constraint_for (dest
, &rhsc
);
4423 process_all_all_constraints (lhsc
, rhsc
);
4427 /* All the following functions do not return pointers, do not
4428 modify the points-to sets of memory reachable from their
4429 arguments and do not add to the ESCAPED solution. */
4430 case BUILT_IN_SINCOS
:
4431 case BUILT_IN_SINCOSF
:
4432 case BUILT_IN_SINCOSL
:
4433 case BUILT_IN_FREXP
:
4434 case BUILT_IN_FREXPF
:
4435 case BUILT_IN_FREXPL
:
4436 case BUILT_IN_GAMMA_R
:
4437 case BUILT_IN_GAMMAF_R
:
4438 case BUILT_IN_GAMMAL_R
:
4439 case BUILT_IN_LGAMMA_R
:
4440 case BUILT_IN_LGAMMAF_R
:
4441 case BUILT_IN_LGAMMAL_R
:
4443 case BUILT_IN_MODFF
:
4444 case BUILT_IN_MODFL
:
4445 case BUILT_IN_REMQUO
:
4446 case BUILT_IN_REMQUOF
:
4447 case BUILT_IN_REMQUOL
:
4450 case BUILT_IN_STRDUP
:
4451 case BUILT_IN_STRNDUP
:
4452 case BUILT_IN_REALLOC
:
4453 if (gimple_call_lhs (t
))
4455 handle_lhs_call (t
, gimple_call_lhs (t
),
4456 gimple_call_return_flags (t
) | ERF_NOALIAS
,
4458 get_constraint_for_ptr_offset (gimple_call_lhs (t
),
4460 get_constraint_for_ptr_offset (gimple_call_arg (t
, 0),
4464 process_all_all_constraints (lhsc
, rhsc
);
4467 /* For realloc the resulting pointer can be equal to the
4468 argument as well. But only doing this wouldn't be
4469 correct because with ptr == 0 realloc behaves like malloc. */
4470 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_REALLOC
)
4472 get_constraint_for (gimple_call_lhs (t
), &lhsc
);
4473 get_constraint_for (gimple_call_arg (t
, 0), &rhsc
);
4474 process_all_all_constraints (lhsc
, rhsc
);
4479 /* String / character search functions return a pointer into the
4480 source string or NULL. */
4481 case BUILT_IN_INDEX
:
4482 case BUILT_IN_STRCHR
:
4483 case BUILT_IN_STRRCHR
:
4484 case BUILT_IN_MEMCHR
:
4485 case BUILT_IN_STRSTR
:
4486 case BUILT_IN_STRPBRK
:
4487 if (gimple_call_lhs (t
))
4489 tree src
= gimple_call_arg (t
, 0);
4490 get_constraint_for_ptr_offset (src
, NULL_TREE
, &rhsc
);
4491 constraint_expr nul
;
4492 nul
.var
= nothing_id
;
4494 nul
.type
= ADDRESSOF
;
4495 rhsc
.safe_push (nul
);
4496 get_constraint_for (gimple_call_lhs (t
), &lhsc
);
4497 process_all_all_constraints (lhsc
, rhsc
);
4500 /* Pure functions that return something not based on any object and
4501 that use the memory pointed to by their arguments (but not
4503 case BUILT_IN_STRCMP
:
4504 case BUILT_IN_STRNCMP
:
4505 case BUILT_IN_STRCASECMP
:
4506 case BUILT_IN_STRNCASECMP
:
4507 case BUILT_IN_MEMCMP
:
4509 case BUILT_IN_STRSPN
:
4510 case BUILT_IN_STRCSPN
:
4512 varinfo_t uses
= get_call_use_vi (t
);
4513 make_any_offset_constraints (uses
);
4514 make_constraint_to (uses
->id
, gimple_call_arg (t
, 0));
4515 make_constraint_to (uses
->id
, gimple_call_arg (t
, 1));
4516 /* No constraints are necessary for the return value. */
4519 case BUILT_IN_STRLEN
:
4521 varinfo_t uses
= get_call_use_vi (t
);
4522 make_any_offset_constraints (uses
);
4523 make_constraint_to (uses
->id
, gimple_call_arg (t
, 0));
4524 /* No constraints are necessary for the return value. */
4527 case BUILT_IN_OBJECT_SIZE
:
4528 case BUILT_IN_CONSTANT_P
:
4530 /* No constraints are necessary for the return value or the
4534 /* Trampolines are special - they set up passing the static
4536 case BUILT_IN_INIT_TRAMPOLINE
:
4538 tree tramp
= gimple_call_arg (t
, 0);
4539 tree nfunc
= gimple_call_arg (t
, 1);
4540 tree frame
= gimple_call_arg (t
, 2);
4542 struct constraint_expr lhs
, *rhsp
;
4545 varinfo_t nfi
= NULL
;
4546 gcc_assert (TREE_CODE (nfunc
) == ADDR_EXPR
);
4547 nfi
= lookup_vi_for_tree (TREE_OPERAND (nfunc
, 0));
4550 lhs
= get_function_part_constraint (nfi
, fi_static_chain
);
4551 get_constraint_for (frame
, &rhsc
);
4552 FOR_EACH_VEC_ELT (rhsc
, i
, rhsp
)
4553 process_constraint (new_constraint (lhs
, *rhsp
));
4556 /* Make the frame point to the function for
4557 the trampoline adjustment call. */
4558 get_constraint_for (tramp
, &lhsc
);
4560 get_constraint_for (nfunc
, &rhsc
);
4561 process_all_all_constraints (lhsc
, rhsc
);
4566 /* Else fallthru to generic handling which will let
4567 the frame escape. */
4570 case BUILT_IN_ADJUST_TRAMPOLINE
:
4572 tree tramp
= gimple_call_arg (t
, 0);
4573 tree res
= gimple_call_lhs (t
);
4574 if (in_ipa_mode
&& res
)
4576 get_constraint_for (res
, &lhsc
);
4577 get_constraint_for (tramp
, &rhsc
);
4579 process_all_all_constraints (lhsc
, rhsc
);
4583 CASE_BUILT_IN_TM_STORE (1):
4584 CASE_BUILT_IN_TM_STORE (2):
4585 CASE_BUILT_IN_TM_STORE (4):
4586 CASE_BUILT_IN_TM_STORE (8):
4587 CASE_BUILT_IN_TM_STORE (FLOAT
):
4588 CASE_BUILT_IN_TM_STORE (DOUBLE
):
4589 CASE_BUILT_IN_TM_STORE (LDOUBLE
):
4590 CASE_BUILT_IN_TM_STORE (M64
):
4591 CASE_BUILT_IN_TM_STORE (M128
):
4592 CASE_BUILT_IN_TM_STORE (M256
):
4594 tree addr
= gimple_call_arg (t
, 0);
4595 tree src
= gimple_call_arg (t
, 1);
4597 get_constraint_for (addr
, &lhsc
);
4599 get_constraint_for (src
, &rhsc
);
4600 process_all_all_constraints (lhsc
, rhsc
);
4603 CASE_BUILT_IN_TM_LOAD (1):
4604 CASE_BUILT_IN_TM_LOAD (2):
4605 CASE_BUILT_IN_TM_LOAD (4):
4606 CASE_BUILT_IN_TM_LOAD (8):
4607 CASE_BUILT_IN_TM_LOAD (FLOAT
):
4608 CASE_BUILT_IN_TM_LOAD (DOUBLE
):
4609 CASE_BUILT_IN_TM_LOAD (LDOUBLE
):
4610 CASE_BUILT_IN_TM_LOAD (M64
):
4611 CASE_BUILT_IN_TM_LOAD (M128
):
4612 CASE_BUILT_IN_TM_LOAD (M256
):
4614 tree dest
= gimple_call_lhs (t
);
4615 tree addr
= gimple_call_arg (t
, 0);
4617 get_constraint_for (dest
, &lhsc
);
4618 get_constraint_for (addr
, &rhsc
);
4620 process_all_all_constraints (lhsc
, rhsc
);
4623 /* Variadic argument handling needs to be handled in IPA
4625 case BUILT_IN_VA_START
:
4627 tree valist
= gimple_call_arg (t
, 0);
4628 struct constraint_expr rhs
, *lhsp
;
4630 get_constraint_for_ptr_offset (valist
, NULL_TREE
, &lhsc
);
4632 /* The va_list gets access to pointers in variadic
4633 arguments. Which we know in the case of IPA analysis
4634 and otherwise are just all nonlocal variables. */
4637 fi
= lookup_vi_for_tree (fn
->decl
);
4638 rhs
= get_function_part_constraint (fi
, ~0);
4639 rhs
.type
= ADDRESSOF
;
4643 rhs
.var
= nonlocal_id
;
4644 rhs
.type
= ADDRESSOF
;
4647 FOR_EACH_VEC_ELT (lhsc
, i
, lhsp
)
4648 process_constraint (new_constraint (*lhsp
, rhs
));
4649 /* va_list is clobbered. */
4650 make_constraint_to (get_call_clobber_vi (t
)->id
, valist
);
4653 /* va_end doesn't have any effect that matters. */
4654 case BUILT_IN_VA_END
:
4656 /* Alternate return. Simply give up for now. */
4657 case BUILT_IN_RETURN
:
4661 || !(fi
= get_vi_for_tree (fn
->decl
)))
4662 make_constraint_from (get_varinfo (escaped_id
), anything_id
);
4663 else if (in_ipa_mode
4666 struct constraint_expr lhs
, rhs
;
4667 lhs
= get_function_part_constraint (fi
, fi_result
);
4668 rhs
.var
= anything_id
;
4671 process_constraint (new_constraint (lhs
, rhs
));
4675 case BUILT_IN_GOMP_PARALLEL
:
4676 case BUILT_IN_GOACC_PARALLEL
:
4680 unsigned int fnpos
, argpos
;
4681 switch (DECL_FUNCTION_CODE (fndecl
))
4683 case BUILT_IN_GOMP_PARALLEL
:
4684 /* __builtin_GOMP_parallel (fn, data, num_threads, flags). */
4688 case BUILT_IN_GOACC_PARALLEL
:
4689 /* __builtin_GOACC_parallel (device, fn, mapnum, hostaddrs,
4690 sizes, kinds, ...). */
4698 tree fnarg
= gimple_call_arg (t
, fnpos
);
4699 gcc_assert (TREE_CODE (fnarg
) == ADDR_EXPR
);
4700 tree fndecl
= TREE_OPERAND (fnarg
, 0);
4701 if (fndecl_maybe_in_other_partition (fndecl
))
4702 /* Fallthru to general call handling. */
4705 tree arg
= gimple_call_arg (t
, argpos
);
4707 varinfo_t fi
= get_vi_for_tree (fndecl
);
4708 find_func_aliases_for_call_arg (fi
, 0, arg
);
4711 /* Else fallthru to generic call handling. */
4714 /* printf-style functions may have hooks to set pointers to
4715 point to somewhere into the generated string. Leave them
4716 for a later exercise... */
4718 /* Fallthru to general call handling. */;
4724 /* Create constraints for the call T. */
4727 find_func_aliases_for_call (struct function
*fn
, gcall
*t
)
4729 tree fndecl
= gimple_call_fndecl (t
);
4732 if (fndecl
!= NULL_TREE
4733 && DECL_BUILT_IN (fndecl
)
4734 && find_func_aliases_for_builtin_call (fn
, t
))
4737 fi
= get_fi_for_callee (t
);
4739 || (fndecl
&& !fi
->is_fn_info
))
4741 auto_vec
<ce_s
, 16> rhsc
;
4742 int flags
= gimple_call_flags (t
);
4744 /* Const functions can return their arguments and addresses
4745 of global memory but not of escaped memory. */
4746 if (flags
& (ECF_CONST
|ECF_NOVOPS
))
4748 if (gimple_call_lhs (t
))
4749 handle_const_call (t
, &rhsc
);
4751 /* Pure functions can return addresses in and of memory
4752 reachable from their arguments, but they are not an escape
4753 point for reachable memory of their arguments. */
4754 else if (flags
& (ECF_PURE
|ECF_LOOPING_CONST_OR_PURE
))
4755 handle_pure_call (t
, &rhsc
);
4757 handle_rhs_call (t
, &rhsc
);
4758 if (gimple_call_lhs (t
))
4759 handle_lhs_call (t
, gimple_call_lhs (t
),
4760 gimple_call_return_flags (t
), rhsc
, fndecl
);
4764 auto_vec
<ce_s
, 2> rhsc
;
4768 /* Assign all the passed arguments to the appropriate incoming
4769 parameters of the function. */
4770 for (j
= 0; j
< gimple_call_num_args (t
); j
++)
4772 tree arg
= gimple_call_arg (t
, j
);
4773 find_func_aliases_for_call_arg (fi
, j
, arg
);
4776 /* If we are returning a value, assign it to the result. */
4777 lhsop
= gimple_call_lhs (t
);
4780 auto_vec
<ce_s
, 2> lhsc
;
4781 struct constraint_expr rhs
;
4782 struct constraint_expr
*lhsp
;
4783 bool aggr_p
= aggregate_value_p (lhsop
, gimple_call_fntype (t
));
4785 get_constraint_for (lhsop
, &lhsc
);
4786 rhs
= get_function_part_constraint (fi
, fi_result
);
4789 auto_vec
<ce_s
, 2> tem
;
4790 tem
.quick_push (rhs
);
4792 gcc_checking_assert (tem
.length () == 1);
4795 FOR_EACH_VEC_ELT (lhsc
, j
, lhsp
)
4796 process_constraint (new_constraint (*lhsp
, rhs
));
4798 /* If we pass the result decl by reference, honor that. */
4801 struct constraint_expr lhs
;
4802 struct constraint_expr
*rhsp
;
4804 get_constraint_for_address_of (lhsop
, &rhsc
);
4805 lhs
= get_function_part_constraint (fi
, fi_result
);
4806 FOR_EACH_VEC_ELT (rhsc
, j
, rhsp
)
4807 process_constraint (new_constraint (lhs
, *rhsp
));
4812 /* If we use a static chain, pass it along. */
4813 if (gimple_call_chain (t
))
4815 struct constraint_expr lhs
;
4816 struct constraint_expr
*rhsp
;
4818 get_constraint_for (gimple_call_chain (t
), &rhsc
);
4819 lhs
= get_function_part_constraint (fi
, fi_static_chain
);
4820 FOR_EACH_VEC_ELT (rhsc
, j
, rhsp
)
4821 process_constraint (new_constraint (lhs
, *rhsp
));
4826 /* Walk statement T setting up aliasing constraints according to the
4827 references found in T. This function is the main part of the
4828 constraint builder. AI points to auxiliary alias information used
4829 when building alias sets and computing alias grouping heuristics. */
4832 find_func_aliases (struct function
*fn
, gimple
*origt
)
4835 auto_vec
<ce_s
, 16> lhsc
;
4836 auto_vec
<ce_s
, 16> rhsc
;
4837 struct constraint_expr
*c
;
4840 /* Now build constraints expressions. */
4841 if (gimple_code (t
) == GIMPLE_PHI
)
4846 /* For a phi node, assign all the arguments to
4848 get_constraint_for (gimple_phi_result (t
), &lhsc
);
4849 for (i
= 0; i
< gimple_phi_num_args (t
); i
++)
4851 tree strippedrhs
= PHI_ARG_DEF (t
, i
);
4853 STRIP_NOPS (strippedrhs
);
4854 get_constraint_for_rhs (gimple_phi_arg_def (t
, i
), &rhsc
);
4856 FOR_EACH_VEC_ELT (lhsc
, j
, c
)
4858 struct constraint_expr
*c2
;
4859 while (rhsc
.length () > 0)
4862 process_constraint (new_constraint (*c
, *c2
));
4868 /* In IPA mode, we need to generate constraints to pass call
4869 arguments through their calls. There are two cases,
4870 either a GIMPLE_CALL returning a value, or just a plain
4871 GIMPLE_CALL when we are not.
4873 In non-ipa mode, we need to generate constraints for each
4874 pointer passed by address. */
4875 else if (is_gimple_call (t
))
4876 find_func_aliases_for_call (fn
, as_a
<gcall
*> (t
));
4878 /* Otherwise, just a regular assignment statement. Only care about
4879 operations with pointer result, others are dealt with as escape
4880 points if they have pointer operands. */
4881 else if (is_gimple_assign (t
))
4883 /* Otherwise, just a regular assignment statement. */
4884 tree lhsop
= gimple_assign_lhs (t
);
4885 tree rhsop
= (gimple_num_ops (t
) == 2) ? gimple_assign_rhs1 (t
) : NULL
;
4887 if (rhsop
&& TREE_CLOBBER_P (rhsop
))
4888 /* Ignore clobbers, they don't actually store anything into
4891 else if (rhsop
&& AGGREGATE_TYPE_P (TREE_TYPE (lhsop
)))
4892 do_structure_copy (lhsop
, rhsop
);
4895 enum tree_code code
= gimple_assign_rhs_code (t
);
4897 get_constraint_for (lhsop
, &lhsc
);
4899 if (code
== POINTER_PLUS_EXPR
)
4900 get_constraint_for_ptr_offset (gimple_assign_rhs1 (t
),
4901 gimple_assign_rhs2 (t
), &rhsc
);
4902 else if (code
== BIT_AND_EXPR
4903 && TREE_CODE (gimple_assign_rhs2 (t
)) == INTEGER_CST
)
4905 /* Aligning a pointer via a BIT_AND_EXPR is offsetting
4906 the pointer. Handle it by offsetting it by UNKNOWN. */
4907 get_constraint_for_ptr_offset (gimple_assign_rhs1 (t
),
4910 else if ((CONVERT_EXPR_CODE_P (code
)
4911 && !(POINTER_TYPE_P (gimple_expr_type (t
))
4912 && !POINTER_TYPE_P (TREE_TYPE (rhsop
))))
4913 || gimple_assign_single_p (t
))
4914 get_constraint_for_rhs (rhsop
, &rhsc
);
4915 else if (code
== COND_EXPR
)
4917 /* The result is a merge of both COND_EXPR arms. */
4918 auto_vec
<ce_s
, 2> tmp
;
4919 struct constraint_expr
*rhsp
;
4921 get_constraint_for_rhs (gimple_assign_rhs2 (t
), &rhsc
);
4922 get_constraint_for_rhs (gimple_assign_rhs3 (t
), &tmp
);
4923 FOR_EACH_VEC_ELT (tmp
, i
, rhsp
)
4924 rhsc
.safe_push (*rhsp
);
4926 else if (truth_value_p (code
))
4927 /* Truth value results are not pointer (parts). Or at least
4928 very unreasonable obfuscation of a part. */
4932 /* All other operations are merges. */
4933 auto_vec
<ce_s
, 4> tmp
;
4934 struct constraint_expr
*rhsp
;
4936 get_constraint_for_rhs (gimple_assign_rhs1 (t
), &rhsc
);
4937 for (i
= 2; i
< gimple_num_ops (t
); ++i
)
4939 get_constraint_for_rhs (gimple_op (t
, i
), &tmp
);
4940 FOR_EACH_VEC_ELT (tmp
, j
, rhsp
)
4941 rhsc
.safe_push (*rhsp
);
4945 process_all_all_constraints (lhsc
, rhsc
);
4947 /* If there is a store to a global variable the rhs escapes. */
4948 if ((lhsop
= get_base_address (lhsop
)) != NULL_TREE
4951 varinfo_t vi
= get_vi_for_tree (lhsop
);
4952 if ((! in_ipa_mode
&& vi
->is_global_var
)
4953 || vi
->is_ipa_escape_point
)
4954 make_escape_constraint (rhsop
);
4957 /* Handle escapes through return. */
4958 else if (gimple_code (t
) == GIMPLE_RETURN
4959 && gimple_return_retval (as_a
<greturn
*> (t
)) != NULL_TREE
)
4961 greturn
*return_stmt
= as_a
<greturn
*> (t
);
4964 || !(fi
= get_vi_for_tree (fn
->decl
)))
4965 make_escape_constraint (gimple_return_retval (return_stmt
));
4966 else if (in_ipa_mode
)
4968 struct constraint_expr lhs
;
4969 struct constraint_expr
*rhsp
;
4972 lhs
= get_function_part_constraint (fi
, fi_result
);
4973 get_constraint_for_rhs (gimple_return_retval (return_stmt
), &rhsc
);
4974 FOR_EACH_VEC_ELT (rhsc
, i
, rhsp
)
4975 process_constraint (new_constraint (lhs
, *rhsp
));
4978 /* Handle asms conservatively by adding escape constraints to everything. */
4979 else if (gasm
*asm_stmt
= dyn_cast
<gasm
*> (t
))
4981 unsigned i
, noutputs
;
4982 const char **oconstraints
;
4983 const char *constraint
;
4984 bool allows_mem
, allows_reg
, is_inout
;
4986 noutputs
= gimple_asm_noutputs (asm_stmt
);
4987 oconstraints
= XALLOCAVEC (const char *, noutputs
);
4989 for (i
= 0; i
< noutputs
; ++i
)
4991 tree link
= gimple_asm_output_op (asm_stmt
, i
);
4992 tree op
= TREE_VALUE (link
);
4994 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link
)));
4995 oconstraints
[i
] = constraint
;
4996 parse_output_constraint (&constraint
, i
, 0, 0, &allows_mem
,
4997 &allows_reg
, &is_inout
);
4999 /* A memory constraint makes the address of the operand escape. */
5000 if (!allows_reg
&& allows_mem
)
5001 make_escape_constraint (build_fold_addr_expr (op
));
5003 /* The asm may read global memory, so outputs may point to
5004 any global memory. */
5007 auto_vec
<ce_s
, 2> lhsc
;
5008 struct constraint_expr rhsc
, *lhsp
;
5010 get_constraint_for (op
, &lhsc
);
5011 rhsc
.var
= nonlocal_id
;
5014 FOR_EACH_VEC_ELT (lhsc
, j
, lhsp
)
5015 process_constraint (new_constraint (*lhsp
, rhsc
));
5018 for (i
= 0; i
< gimple_asm_ninputs (asm_stmt
); ++i
)
5020 tree link
= gimple_asm_input_op (asm_stmt
, i
);
5021 tree op
= TREE_VALUE (link
);
5023 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link
)));
5025 parse_input_constraint (&constraint
, 0, 0, noutputs
, 0, oconstraints
,
5026 &allows_mem
, &allows_reg
);
5028 /* A memory constraint makes the address of the operand escape. */
5029 if (!allows_reg
&& allows_mem
)
5030 make_escape_constraint (build_fold_addr_expr (op
));
5031 /* Strictly we'd only need the constraint to ESCAPED if
5032 the asm clobbers memory, otherwise using something
5033 along the lines of per-call clobbers/uses would be enough. */
5035 make_escape_constraint (op
);
5041 /* Create a constraint adding to the clobber set of FI the memory
5042 pointed to by PTR. */
5045 process_ipa_clobber (varinfo_t fi
, tree ptr
)
5047 vec
<ce_s
> ptrc
= vNULL
;
5048 struct constraint_expr
*c
, lhs
;
5050 get_constraint_for_rhs (ptr
, &ptrc
);
5051 lhs
= get_function_part_constraint (fi
, fi_clobbers
);
5052 FOR_EACH_VEC_ELT (ptrc
, i
, c
)
5053 process_constraint (new_constraint (lhs
, *c
));
5057 /* Walk statement T setting up clobber and use constraints according to the
5058 references found in T. This function is a main part of the
5059 IPA constraint builder. */
5062 find_func_clobbers (struct function
*fn
, gimple
*origt
)
5065 auto_vec
<ce_s
, 16> lhsc
;
5066 auto_vec
<ce_s
, 16> rhsc
;
5069 /* Add constraints for clobbered/used in IPA mode.
5070 We are not interested in what automatic variables are clobbered
5071 or used as we only use the information in the caller to which
5072 they do not escape. */
5073 gcc_assert (in_ipa_mode
);
5075 /* If the stmt refers to memory in any way it better had a VUSE. */
5076 if (gimple_vuse (t
) == NULL_TREE
)
5079 /* We'd better have function information for the current function. */
5080 fi
= lookup_vi_for_tree (fn
->decl
);
5081 gcc_assert (fi
!= NULL
);
5083 /* Account for stores in assignments and calls. */
5084 if (gimple_vdef (t
) != NULL_TREE
5085 && gimple_has_lhs (t
))
5087 tree lhs
= gimple_get_lhs (t
);
5089 while (handled_component_p (tem
))
5090 tem
= TREE_OPERAND (tem
, 0);
5092 && !auto_var_in_fn_p (tem
, fn
->decl
))
5093 || INDIRECT_REF_P (tem
)
5094 || (TREE_CODE (tem
) == MEM_REF
5095 && !(TREE_CODE (TREE_OPERAND (tem
, 0)) == ADDR_EXPR
5097 (TREE_OPERAND (TREE_OPERAND (tem
, 0), 0), fn
->decl
))))
5099 struct constraint_expr lhsc
, *rhsp
;
5101 lhsc
= get_function_part_constraint (fi
, fi_clobbers
);
5102 get_constraint_for_address_of (lhs
, &rhsc
);
5103 FOR_EACH_VEC_ELT (rhsc
, i
, rhsp
)
5104 process_constraint (new_constraint (lhsc
, *rhsp
));
5109 /* Account for uses in assigments and returns. */
5110 if (gimple_assign_single_p (t
)
5111 || (gimple_code (t
) == GIMPLE_RETURN
5112 && gimple_return_retval (as_a
<greturn
*> (t
)) != NULL_TREE
))
5114 tree rhs
= (gimple_assign_single_p (t
)
5115 ? gimple_assign_rhs1 (t
)
5116 : gimple_return_retval (as_a
<greturn
*> (t
)));
5118 while (handled_component_p (tem
))
5119 tem
= TREE_OPERAND (tem
, 0);
5121 && !auto_var_in_fn_p (tem
, fn
->decl
))
5122 || INDIRECT_REF_P (tem
)
5123 || (TREE_CODE (tem
) == MEM_REF
5124 && !(TREE_CODE (TREE_OPERAND (tem
, 0)) == ADDR_EXPR
5126 (TREE_OPERAND (TREE_OPERAND (tem
, 0), 0), fn
->decl
))))
5128 struct constraint_expr lhs
, *rhsp
;
5130 lhs
= get_function_part_constraint (fi
, fi_uses
);
5131 get_constraint_for_address_of (rhs
, &rhsc
);
5132 FOR_EACH_VEC_ELT (rhsc
, i
, rhsp
)
5133 process_constraint (new_constraint (lhs
, *rhsp
));
5138 if (gcall
*call_stmt
= dyn_cast
<gcall
*> (t
))
5140 varinfo_t cfi
= NULL
;
5141 tree decl
= gimple_call_fndecl (t
);
5142 struct constraint_expr lhs
, rhs
;
5145 /* For builtins we do not have separate function info. For those
5146 we do not generate escapes for we have to generate clobbers/uses. */
5147 if (gimple_call_builtin_p (t
, BUILT_IN_NORMAL
))
5148 switch (DECL_FUNCTION_CODE (decl
))
5150 /* The following functions use and clobber memory pointed to
5151 by their arguments. */
5152 case BUILT_IN_STRCPY
:
5153 case BUILT_IN_STRNCPY
:
5154 case BUILT_IN_BCOPY
:
5155 case BUILT_IN_MEMCPY
:
5156 case BUILT_IN_MEMMOVE
:
5157 case BUILT_IN_MEMPCPY
:
5158 case BUILT_IN_STPCPY
:
5159 case BUILT_IN_STPNCPY
:
5160 case BUILT_IN_STRCAT
:
5161 case BUILT_IN_STRNCAT
:
5162 case BUILT_IN_STRCPY_CHK
:
5163 case BUILT_IN_STRNCPY_CHK
:
5164 case BUILT_IN_MEMCPY_CHK
:
5165 case BUILT_IN_MEMMOVE_CHK
:
5166 case BUILT_IN_MEMPCPY_CHK
:
5167 case BUILT_IN_STPCPY_CHK
:
5168 case BUILT_IN_STPNCPY_CHK
:
5169 case BUILT_IN_STRCAT_CHK
:
5170 case BUILT_IN_STRNCAT_CHK
:
5172 tree dest
= gimple_call_arg (t
, (DECL_FUNCTION_CODE (decl
)
5173 == BUILT_IN_BCOPY
? 1 : 0));
5174 tree src
= gimple_call_arg (t
, (DECL_FUNCTION_CODE (decl
)
5175 == BUILT_IN_BCOPY
? 0 : 1));
5177 struct constraint_expr
*rhsp
, *lhsp
;
5178 get_constraint_for_ptr_offset (dest
, NULL_TREE
, &lhsc
);
5179 lhs
= get_function_part_constraint (fi
, fi_clobbers
);
5180 FOR_EACH_VEC_ELT (lhsc
, i
, lhsp
)
5181 process_constraint (new_constraint (lhs
, *lhsp
));
5182 get_constraint_for_ptr_offset (src
, NULL_TREE
, &rhsc
);
5183 lhs
= get_function_part_constraint (fi
, fi_uses
);
5184 FOR_EACH_VEC_ELT (rhsc
, i
, rhsp
)
5185 process_constraint (new_constraint (lhs
, *rhsp
));
5188 /* The following function clobbers memory pointed to by
5190 case BUILT_IN_MEMSET
:
5191 case BUILT_IN_MEMSET_CHK
:
5192 case BUILT_IN_POSIX_MEMALIGN
:
5194 tree dest
= gimple_call_arg (t
, 0);
5197 get_constraint_for_ptr_offset (dest
, NULL_TREE
, &lhsc
);
5198 lhs
= get_function_part_constraint (fi
, fi_clobbers
);
5199 FOR_EACH_VEC_ELT (lhsc
, i
, lhsp
)
5200 process_constraint (new_constraint (lhs
, *lhsp
));
5203 /* The following functions clobber their second and third
5205 case BUILT_IN_SINCOS
:
5206 case BUILT_IN_SINCOSF
:
5207 case BUILT_IN_SINCOSL
:
5209 process_ipa_clobber (fi
, gimple_call_arg (t
, 1));
5210 process_ipa_clobber (fi
, gimple_call_arg (t
, 2));
5213 /* The following functions clobber their second argument. */
5214 case BUILT_IN_FREXP
:
5215 case BUILT_IN_FREXPF
:
5216 case BUILT_IN_FREXPL
:
5217 case BUILT_IN_LGAMMA_R
:
5218 case BUILT_IN_LGAMMAF_R
:
5219 case BUILT_IN_LGAMMAL_R
:
5220 case BUILT_IN_GAMMA_R
:
5221 case BUILT_IN_GAMMAF_R
:
5222 case BUILT_IN_GAMMAL_R
:
5224 case BUILT_IN_MODFF
:
5225 case BUILT_IN_MODFL
:
5227 process_ipa_clobber (fi
, gimple_call_arg (t
, 1));
5230 /* The following functions clobber their third argument. */
5231 case BUILT_IN_REMQUO
:
5232 case BUILT_IN_REMQUOF
:
5233 case BUILT_IN_REMQUOL
:
5235 process_ipa_clobber (fi
, gimple_call_arg (t
, 2));
5238 /* The following functions neither read nor clobber memory. */
5239 case BUILT_IN_ASSUME_ALIGNED
:
5242 /* Trampolines are of no interest to us. */
5243 case BUILT_IN_INIT_TRAMPOLINE
:
5244 case BUILT_IN_ADJUST_TRAMPOLINE
:
5246 case BUILT_IN_VA_START
:
5247 case BUILT_IN_VA_END
:
5249 case BUILT_IN_GOMP_PARALLEL
:
5250 case BUILT_IN_GOACC_PARALLEL
:
5252 unsigned int fnpos
, argpos
;
5253 unsigned int implicit_use_args
[2];
5254 unsigned int num_implicit_use_args
= 0;
5255 switch (DECL_FUNCTION_CODE (decl
))
5257 case BUILT_IN_GOMP_PARALLEL
:
5258 /* __builtin_GOMP_parallel (fn, data, num_threads, flags). */
5262 case BUILT_IN_GOACC_PARALLEL
:
5263 /* __builtin_GOACC_parallel (device, fn, mapnum, hostaddrs,
5264 sizes, kinds, ...). */
5267 implicit_use_args
[num_implicit_use_args
++] = 4;
5268 implicit_use_args
[num_implicit_use_args
++] = 5;
5274 tree fnarg
= gimple_call_arg (t
, fnpos
);
5275 gcc_assert (TREE_CODE (fnarg
) == ADDR_EXPR
);
5276 tree fndecl
= TREE_OPERAND (fnarg
, 0);
5277 if (fndecl_maybe_in_other_partition (fndecl
))
5278 /* Fallthru to general call handling. */
5281 varinfo_t cfi
= get_vi_for_tree (fndecl
);
5283 tree arg
= gimple_call_arg (t
, argpos
);
5285 /* Parameter passed by value is used. */
5286 lhs
= get_function_part_constraint (fi
, fi_uses
);
5287 struct constraint_expr
*rhsp
;
5288 get_constraint_for (arg
, &rhsc
);
5289 FOR_EACH_VEC_ELT (rhsc
, j
, rhsp
)
5290 process_constraint (new_constraint (lhs
, *rhsp
));
5293 /* Handle parameters used by the call, but not used in cfi, as
5294 implicitly used by cfi. */
5295 lhs
= get_function_part_constraint (cfi
, fi_uses
);
5296 for (unsigned i
= 0; i
< num_implicit_use_args
; ++i
)
5298 tree arg
= gimple_call_arg (t
, implicit_use_args
[i
]);
5299 get_constraint_for (arg
, &rhsc
);
5300 FOR_EACH_VEC_ELT (rhsc
, j
, rhsp
)
5301 process_constraint (new_constraint (lhs
, *rhsp
));
5305 /* The caller clobbers what the callee does. */
5306 lhs
= get_function_part_constraint (fi
, fi_clobbers
);
5307 rhs
= get_function_part_constraint (cfi
, fi_clobbers
);
5308 process_constraint (new_constraint (lhs
, rhs
));
5310 /* The caller uses what the callee does. */
5311 lhs
= get_function_part_constraint (fi
, fi_uses
);
5312 rhs
= get_function_part_constraint (cfi
, fi_uses
);
5313 process_constraint (new_constraint (lhs
, rhs
));
5317 /* printf-style functions may have hooks to set pointers to
5318 point to somewhere into the generated string. Leave them
5319 for a later exercise... */
5321 /* Fallthru to general call handling. */;
5324 /* Parameters passed by value are used. */
5325 lhs
= get_function_part_constraint (fi
, fi_uses
);
5326 for (i
= 0; i
< gimple_call_num_args (t
); i
++)
5328 struct constraint_expr
*rhsp
;
5329 tree arg
= gimple_call_arg (t
, i
);
5331 if (TREE_CODE (arg
) == SSA_NAME
5332 || is_gimple_min_invariant (arg
))
5335 get_constraint_for_address_of (arg
, &rhsc
);
5336 FOR_EACH_VEC_ELT (rhsc
, j
, rhsp
)
5337 process_constraint (new_constraint (lhs
, *rhsp
));
5341 /* Build constraints for propagating clobbers/uses along the
5343 cfi
= get_fi_for_callee (call_stmt
);
5344 if (cfi
->id
== anything_id
)
5346 if (gimple_vdef (t
))
5347 make_constraint_from (first_vi_for_offset (fi
, fi_clobbers
),
5349 make_constraint_from (first_vi_for_offset (fi
, fi_uses
),
5354 /* For callees without function info (that's external functions),
5355 ESCAPED is clobbered and used. */
5356 if (gimple_call_fndecl (t
)
5357 && !cfi
->is_fn_info
)
5361 if (gimple_vdef (t
))
5362 make_copy_constraint (first_vi_for_offset (fi
, fi_clobbers
),
5364 make_copy_constraint (first_vi_for_offset (fi
, fi_uses
), escaped_id
);
5366 /* Also honor the call statement use/clobber info. */
5367 if ((vi
= lookup_call_clobber_vi (call_stmt
)) != NULL
)
5368 make_copy_constraint (first_vi_for_offset (fi
, fi_clobbers
),
5370 if ((vi
= lookup_call_use_vi (call_stmt
)) != NULL
)
5371 make_copy_constraint (first_vi_for_offset (fi
, fi_uses
),
5376 /* Otherwise the caller clobbers and uses what the callee does.
5377 ??? This should use a new complex constraint that filters
5378 local variables of the callee. */
5379 if (gimple_vdef (t
))
5381 lhs
= get_function_part_constraint (fi
, fi_clobbers
);
5382 rhs
= get_function_part_constraint (cfi
, fi_clobbers
);
5383 process_constraint (new_constraint (lhs
, rhs
));
5385 lhs
= get_function_part_constraint (fi
, fi_uses
);
5386 rhs
= get_function_part_constraint (cfi
, fi_uses
);
5387 process_constraint (new_constraint (lhs
, rhs
));
5389 else if (gimple_code (t
) == GIMPLE_ASM
)
5391 /* ??? Ick. We can do better. */
5392 if (gimple_vdef (t
))
5393 make_constraint_from (first_vi_for_offset (fi
, fi_clobbers
),
5395 make_constraint_from (first_vi_for_offset (fi
, fi_uses
),
5401 /* Find the first varinfo in the same variable as START that overlaps with
5402 OFFSET. Return NULL if we can't find one. */
5405 first_vi_for_offset (varinfo_t start
, unsigned HOST_WIDE_INT offset
)
5407 /* If the offset is outside of the variable, bail out. */
5408 if (offset
>= start
->fullsize
)
5411 /* If we cannot reach offset from start, lookup the first field
5412 and start from there. */
5413 if (start
->offset
> offset
)
5414 start
= get_varinfo (start
->head
);
5418 /* We may not find a variable in the field list with the actual
5419 offset when we have glommed a structure to a variable.
5420 In that case, however, offset should still be within the size
5422 if (offset
>= start
->offset
5423 && (offset
- start
->offset
) < start
->size
)
5426 start
= vi_next (start
);
5432 /* Find the first varinfo in the same variable as START that overlaps with
5433 OFFSET. If there is no such varinfo the varinfo directly preceding
5434 OFFSET is returned. */
5437 first_or_preceding_vi_for_offset (varinfo_t start
,
5438 unsigned HOST_WIDE_INT offset
)
5440 /* If we cannot reach offset from start, lookup the first field
5441 and start from there. */
5442 if (start
->offset
> offset
)
5443 start
= get_varinfo (start
->head
);
5445 /* We may not find a variable in the field list with the actual
5446 offset when we have glommed a structure to a variable.
5447 In that case, however, offset should still be within the size
5449 If we got beyond the offset we look for return the field
5450 directly preceding offset which may be the last field. */
5452 && offset
>= start
->offset
5453 && !((offset
- start
->offset
) < start
->size
))
5454 start
= vi_next (start
);
5460 /* This structure is used during pushing fields onto the fieldstack
5461 to track the offset of the field, since bitpos_of_field gives it
5462 relative to its immediate containing type, and we want it relative
5463 to the ultimate containing object. */
5467 /* Offset from the base of the base containing object to this field. */
5468 HOST_WIDE_INT offset
;
5470 /* Size, in bits, of the field. */
5471 unsigned HOST_WIDE_INT size
;
5473 unsigned has_unknown_size
: 1;
5475 unsigned must_have_pointers
: 1;
5477 unsigned may_have_pointers
: 1;
5479 unsigned only_restrict_pointers
: 1;
5481 tree restrict_pointed_type
;
5483 typedef struct fieldoff fieldoff_s
;
5486 /* qsort comparison function for two fieldoff's PA and PB */
5489 fieldoff_compare (const void *pa
, const void *pb
)
5491 const fieldoff_s
*foa
= (const fieldoff_s
*)pa
;
5492 const fieldoff_s
*fob
= (const fieldoff_s
*)pb
;
5493 unsigned HOST_WIDE_INT foasize
, fobsize
;
5495 if (foa
->offset
< fob
->offset
)
5497 else if (foa
->offset
> fob
->offset
)
5500 foasize
= foa
->size
;
5501 fobsize
= fob
->size
;
5502 if (foasize
< fobsize
)
5504 else if (foasize
> fobsize
)
5509 /* Sort a fieldstack according to the field offset and sizes. */
5511 sort_fieldstack (vec
<fieldoff_s
> fieldstack
)
5513 fieldstack
.qsort (fieldoff_compare
);
5516 /* Return true if T is a type that can have subvars. */
5519 type_can_have_subvars (const_tree t
)
5521 /* Aggregates without overlapping fields can have subvars. */
5522 return TREE_CODE (t
) == RECORD_TYPE
;
5525 /* Return true if V is a tree that we can have subvars for.
5526 Normally, this is any aggregate type. Also complex
5527 types which are not gimple registers can have subvars. */
5530 var_can_have_subvars (const_tree v
)
5532 /* Volatile variables should never have subvars. */
5533 if (TREE_THIS_VOLATILE (v
))
5536 /* Non decls or memory tags can never have subvars. */
5540 return type_can_have_subvars (TREE_TYPE (v
));
5543 /* Return true if T is a type that does contain pointers. */
5546 type_must_have_pointers (tree type
)
5548 if (POINTER_TYPE_P (type
))
5551 if (TREE_CODE (type
) == ARRAY_TYPE
)
5552 return type_must_have_pointers (TREE_TYPE (type
));
5554 /* A function or method can have pointers as arguments, so track
5555 those separately. */
5556 if (TREE_CODE (type
) == FUNCTION_TYPE
5557 || TREE_CODE (type
) == METHOD_TYPE
)
5564 field_must_have_pointers (tree t
)
5566 return type_must_have_pointers (TREE_TYPE (t
));
5569 /* Given a TYPE, and a vector of field offsets FIELDSTACK, push all
5570 the fields of TYPE onto fieldstack, recording their offsets along
5573 OFFSET is used to keep track of the offset in this entire
5574 structure, rather than just the immediately containing structure.
5575 Returns false if the caller is supposed to handle the field we
5579 push_fields_onto_fieldstack (tree type
, vec
<fieldoff_s
> *fieldstack
,
5580 HOST_WIDE_INT offset
)
5583 bool empty_p
= true;
5585 if (TREE_CODE (type
) != RECORD_TYPE
)
5588 /* If the vector of fields is growing too big, bail out early.
5589 Callers check for vec::length <= MAX_FIELDS_FOR_FIELD_SENSITIVE, make
5591 if (fieldstack
->length () > MAX_FIELDS_FOR_FIELD_SENSITIVE
)
5594 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
5595 if (TREE_CODE (field
) == FIELD_DECL
)
5598 HOST_WIDE_INT foff
= bitpos_of_field (field
);
5599 tree field_type
= TREE_TYPE (field
);
5601 if (!var_can_have_subvars (field
)
5602 || TREE_CODE (field_type
) == QUAL_UNION_TYPE
5603 || TREE_CODE (field_type
) == UNION_TYPE
)
5605 else if (!push_fields_onto_fieldstack
5606 (field_type
, fieldstack
, offset
+ foff
)
5607 && (DECL_SIZE (field
)
5608 && !integer_zerop (DECL_SIZE (field
))))
5609 /* Empty structures may have actual size, like in C++. So
5610 see if we didn't push any subfields and the size is
5611 nonzero, push the field onto the stack. */
5616 fieldoff_s
*pair
= NULL
;
5617 bool has_unknown_size
= false;
5618 bool must_have_pointers_p
;
5620 if (!fieldstack
->is_empty ())
5621 pair
= &fieldstack
->last ();
5623 /* If there isn't anything at offset zero, create sth. */
5625 && offset
+ foff
!= 0)
5628 = {0, offset
+ foff
, false, false, true, false, NULL_TREE
};
5629 pair
= fieldstack
->safe_push (e
);
5632 if (!DECL_SIZE (field
)
5633 || !tree_fits_uhwi_p (DECL_SIZE (field
)))
5634 has_unknown_size
= true;
5636 /* If adjacent fields do not contain pointers merge them. */
5637 must_have_pointers_p
= field_must_have_pointers (field
);
5639 && !has_unknown_size
5640 && !must_have_pointers_p
5641 && !pair
->must_have_pointers
5642 && !pair
->has_unknown_size
5643 && pair
->offset
+ (HOST_WIDE_INT
)pair
->size
== offset
+ foff
)
5645 pair
->size
+= tree_to_uhwi (DECL_SIZE (field
));
5650 e
.offset
= offset
+ foff
;
5651 e
.has_unknown_size
= has_unknown_size
;
5652 if (!has_unknown_size
)
5653 e
.size
= tree_to_uhwi (DECL_SIZE (field
));
5656 e
.must_have_pointers
= must_have_pointers_p
;
5657 e
.may_have_pointers
= true;
5658 e
.only_restrict_pointers
5659 = (!has_unknown_size
5660 && POINTER_TYPE_P (field_type
)
5661 && TYPE_RESTRICT (field_type
));
5662 if (e
.only_restrict_pointers
)
5663 e
.restrict_pointed_type
= TREE_TYPE (field_type
);
5664 fieldstack
->safe_push (e
);
5674 /* Count the number of arguments DECL has, and set IS_VARARGS to true
5675 if it is a varargs function. */
5678 count_num_arguments (tree decl
, bool *is_varargs
)
5680 unsigned int num
= 0;
5683 /* Capture named arguments for K&R functions. They do not
5684 have a prototype and thus no TYPE_ARG_TYPES. */
5685 for (t
= DECL_ARGUMENTS (decl
); t
; t
= DECL_CHAIN (t
))
5688 /* Check if the function has variadic arguments. */
5689 for (t
= TYPE_ARG_TYPES (TREE_TYPE (decl
)); t
; t
= TREE_CHAIN (t
))
5690 if (TREE_VALUE (t
) == void_type_node
)
5698 /* Creation function node for DECL, using NAME, and return the index
5699 of the variable we've created for the function. If NONLOCAL_p, create
5700 initial constraints. */
5703 create_function_info_for (tree decl
, const char *name
, bool add_id
,
5706 struct function
*fn
= DECL_STRUCT_FUNCTION (decl
);
5707 varinfo_t vi
, prev_vi
;
5710 bool is_varargs
= false;
5711 unsigned int num_args
= count_num_arguments (decl
, &is_varargs
);
5713 /* Create the variable info. */
5715 vi
= new_var_info (decl
, name
, add_id
);
5718 vi
->fullsize
= fi_parm_base
+ num_args
;
5720 vi
->may_have_pointers
= false;
5723 insert_vi_for_tree (vi
->decl
, vi
);
5727 /* Create a variable for things the function clobbers and one for
5728 things the function uses. */
5730 varinfo_t clobbervi
, usevi
;
5731 const char *newname
;
5734 tempname
= xasprintf ("%s.clobber", name
);
5735 newname
= ggc_strdup (tempname
);
5738 clobbervi
= new_var_info (NULL
, newname
, false);
5739 clobbervi
->offset
= fi_clobbers
;
5740 clobbervi
->size
= 1;
5741 clobbervi
->fullsize
= vi
->fullsize
;
5742 clobbervi
->is_full_var
= true;
5743 clobbervi
->is_global_var
= false;
5744 clobbervi
->is_reg_var
= true;
5746 gcc_assert (prev_vi
->offset
< clobbervi
->offset
);
5747 prev_vi
->next
= clobbervi
->id
;
5748 prev_vi
= clobbervi
;
5750 tempname
= xasprintf ("%s.use", name
);
5751 newname
= ggc_strdup (tempname
);
5754 usevi
= new_var_info (NULL
, newname
, false);
5755 usevi
->offset
= fi_uses
;
5757 usevi
->fullsize
= vi
->fullsize
;
5758 usevi
->is_full_var
= true;
5759 usevi
->is_global_var
= false;
5760 usevi
->is_reg_var
= true;
5762 gcc_assert (prev_vi
->offset
< usevi
->offset
);
5763 prev_vi
->next
= usevi
->id
;
5767 /* And one for the static chain. */
5768 if (fn
->static_chain_decl
!= NULL_TREE
)
5771 const char *newname
;
5774 tempname
= xasprintf ("%s.chain", name
);
5775 newname
= ggc_strdup (tempname
);
5778 chainvi
= new_var_info (fn
->static_chain_decl
, newname
, false);
5779 chainvi
->offset
= fi_static_chain
;
5781 chainvi
->fullsize
= vi
->fullsize
;
5782 chainvi
->is_full_var
= true;
5783 chainvi
->is_global_var
= false;
5785 insert_vi_for_tree (fn
->static_chain_decl
, chainvi
);
5788 && chainvi
->may_have_pointers
)
5789 make_constraint_from (chainvi
, nonlocal_id
);
5791 gcc_assert (prev_vi
->offset
< chainvi
->offset
);
5792 prev_vi
->next
= chainvi
->id
;
5796 /* Create a variable for the return var. */
5797 if (DECL_RESULT (decl
) != NULL
5798 || !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl
))))
5801 const char *newname
;
5803 tree resultdecl
= decl
;
5805 if (DECL_RESULT (decl
))
5806 resultdecl
= DECL_RESULT (decl
);
5808 tempname
= xasprintf ("%s.result", name
);
5809 newname
= ggc_strdup (tempname
);
5812 resultvi
= new_var_info (resultdecl
, newname
, false);
5813 resultvi
->offset
= fi_result
;
5815 resultvi
->fullsize
= vi
->fullsize
;
5816 resultvi
->is_full_var
= true;
5817 if (DECL_RESULT (decl
))
5818 resultvi
->may_have_pointers
= true;
5820 if (DECL_RESULT (decl
))
5821 insert_vi_for_tree (DECL_RESULT (decl
), resultvi
);
5824 && DECL_RESULT (decl
)
5825 && DECL_BY_REFERENCE (DECL_RESULT (decl
)))
5826 make_constraint_from (resultvi
, nonlocal_id
);
5828 gcc_assert (prev_vi
->offset
< resultvi
->offset
);
5829 prev_vi
->next
= resultvi
->id
;
5833 /* We also need to make function return values escape. Nothing
5834 escapes by returning from main though. */
5836 && !MAIN_NAME_P (DECL_NAME (decl
)))
5839 fi
= lookup_vi_for_tree (decl
);
5840 rvi
= first_vi_for_offset (fi
, fi_result
);
5841 if (rvi
&& rvi
->offset
== fi_result
)
5842 make_copy_constraint (get_varinfo (escaped_id
), rvi
->id
);
5845 /* Set up variables for each argument. */
5846 arg
= DECL_ARGUMENTS (decl
);
5847 for (i
= 0; i
< num_args
; i
++)
5850 const char *newname
;
5852 tree argdecl
= decl
;
5857 tempname
= xasprintf ("%s.arg%d", name
, i
);
5858 newname
= ggc_strdup (tempname
);
5861 argvi
= new_var_info (argdecl
, newname
, false);
5862 argvi
->offset
= fi_parm_base
+ i
;
5864 argvi
->is_full_var
= true;
5865 argvi
->fullsize
= vi
->fullsize
;
5867 argvi
->may_have_pointers
= true;
5870 insert_vi_for_tree (arg
, argvi
);
5873 && argvi
->may_have_pointers
)
5874 make_constraint_from (argvi
, nonlocal_id
);
5876 gcc_assert (prev_vi
->offset
< argvi
->offset
);
5877 prev_vi
->next
= argvi
->id
;
5880 arg
= DECL_CHAIN (arg
);
5883 /* Add one representative for all further args. */
5887 const char *newname
;
5891 tempname
= xasprintf ("%s.varargs", name
);
5892 newname
= ggc_strdup (tempname
);
5895 /* We need sth that can be pointed to for va_start. */
5896 decl
= build_fake_var_decl (ptr_type_node
);
5898 argvi
= new_var_info (decl
, newname
, false);
5899 argvi
->offset
= fi_parm_base
+ num_args
;
5901 argvi
->is_full_var
= true;
5902 argvi
->is_heap_var
= true;
5903 argvi
->fullsize
= vi
->fullsize
;
5906 && argvi
->may_have_pointers
)
5907 make_constraint_from (argvi
, nonlocal_id
);
5909 gcc_assert (prev_vi
->offset
< argvi
->offset
);
5910 prev_vi
->next
= argvi
->id
;
5918 /* Return true if FIELDSTACK contains fields that overlap.
5919 FIELDSTACK is assumed to be sorted by offset. */
5922 check_for_overlaps (vec
<fieldoff_s
> fieldstack
)
5924 fieldoff_s
*fo
= NULL
;
5926 HOST_WIDE_INT lastoffset
= -1;
5928 FOR_EACH_VEC_ELT (fieldstack
, i
, fo
)
5930 if (fo
->offset
== lastoffset
)
5932 lastoffset
= fo
->offset
;
5937 /* Create a varinfo structure for NAME and DECL, and add it to VARMAP.
5938 This will also create any varinfo structures necessary for fields
5939 of DECL. DECL is a function parameter if HANDLE_PARAM is set.
5940 HANDLED_STRUCT_TYPE is used to register struct types reached by following
5941 restrict pointers. This is needed to prevent infinite recursion. */
5944 create_variable_info_for_1 (tree decl
, const char *name
, bool add_id
,
5945 bool handle_param
, bitmap handled_struct_type
)
5947 varinfo_t vi
, newvi
;
5948 tree decl_type
= TREE_TYPE (decl
);
5949 tree declsize
= DECL_P (decl
) ? DECL_SIZE (decl
) : TYPE_SIZE (decl_type
);
5950 auto_vec
<fieldoff_s
> fieldstack
;
5955 || !tree_fits_uhwi_p (declsize
))
5957 vi
= new_var_info (decl
, name
, add_id
);
5961 vi
->is_unknown_size_var
= true;
5962 vi
->is_full_var
= true;
5963 vi
->may_have_pointers
= true;
5967 /* Collect field information. */
5968 if (use_field_sensitive
5969 && var_can_have_subvars (decl
)
5970 /* ??? Force us to not use subfields for globals in IPA mode.
5971 Else we'd have to parse arbitrary initializers. */
5973 && is_global_var (decl
)))
5975 fieldoff_s
*fo
= NULL
;
5976 bool notokay
= false;
5979 push_fields_onto_fieldstack (decl_type
, &fieldstack
, 0);
5981 for (i
= 0; !notokay
&& fieldstack
.iterate (i
, &fo
); i
++)
5982 if (fo
->has_unknown_size
5989 /* We can't sort them if we have a field with a variable sized type,
5990 which will make notokay = true. In that case, we are going to return
5991 without creating varinfos for the fields anyway, so sorting them is a
5995 sort_fieldstack (fieldstack
);
5996 /* Due to some C++ FE issues, like PR 22488, we might end up
5997 what appear to be overlapping fields even though they,
5998 in reality, do not overlap. Until the C++ FE is fixed,
5999 we will simply disable field-sensitivity for these cases. */
6000 notokay
= check_for_overlaps (fieldstack
);
6004 fieldstack
.release ();
6007 /* If we didn't end up collecting sub-variables create a full
6008 variable for the decl. */
6009 if (fieldstack
.length () == 0
6010 || fieldstack
.length () > MAX_FIELDS_FOR_FIELD_SENSITIVE
)
6012 vi
= new_var_info (decl
, name
, add_id
);
6014 vi
->may_have_pointers
= true;
6015 vi
->fullsize
= tree_to_uhwi (declsize
);
6016 vi
->size
= vi
->fullsize
;
6017 vi
->is_full_var
= true;
6018 if (POINTER_TYPE_P (decl_type
)
6019 && TYPE_RESTRICT (decl_type
))
6020 vi
->only_restrict_pointers
= 1;
6021 if (vi
->only_restrict_pointers
6022 && !type_contains_placeholder_p (TREE_TYPE (decl_type
))
6024 && !bitmap_bit_p (handled_struct_type
,
6025 TYPE_UID (TREE_TYPE (decl_type
))))
6028 tree heapvar
= build_fake_var_decl (TREE_TYPE (decl_type
));
6029 DECL_EXTERNAL (heapvar
) = 1;
6030 if (var_can_have_subvars (heapvar
))
6031 bitmap_set_bit (handled_struct_type
,
6032 TYPE_UID (TREE_TYPE (decl_type
)));
6033 rvi
= create_variable_info_for_1 (heapvar
, "PARM_NOALIAS", true,
6034 true, handled_struct_type
);
6035 if (var_can_have_subvars (heapvar
))
6036 bitmap_clear_bit (handled_struct_type
,
6037 TYPE_UID (TREE_TYPE (decl_type
)));
6038 rvi
->is_restrict_var
= 1;
6039 insert_vi_for_tree (heapvar
, rvi
);
6040 make_constraint_from (vi
, rvi
->id
);
6041 make_param_constraints (rvi
);
6043 fieldstack
.release ();
6047 vi
= new_var_info (decl
, name
, add_id
);
6048 vi
->fullsize
= tree_to_uhwi (declsize
);
6049 if (fieldstack
.length () == 1)
6050 vi
->is_full_var
= true;
6051 for (i
= 0, newvi
= vi
;
6052 fieldstack
.iterate (i
, &fo
);
6053 ++i
, newvi
= vi_next (newvi
))
6055 const char *newname
= NULL
;
6060 if (fieldstack
.length () != 1)
6063 = xasprintf ("%s." HOST_WIDE_INT_PRINT_DEC
6064 "+" HOST_WIDE_INT_PRINT_DEC
, name
,
6065 fo
->offset
, fo
->size
);
6066 newname
= ggc_strdup (tempname
);
6074 newvi
->name
= newname
;
6075 newvi
->offset
= fo
->offset
;
6076 newvi
->size
= fo
->size
;
6077 newvi
->fullsize
= vi
->fullsize
;
6078 newvi
->may_have_pointers
= fo
->may_have_pointers
;
6079 newvi
->only_restrict_pointers
= fo
->only_restrict_pointers
;
6081 && newvi
->only_restrict_pointers
6082 && !type_contains_placeholder_p (fo
->restrict_pointed_type
)
6083 && !bitmap_bit_p (handled_struct_type
,
6084 TYPE_UID (fo
->restrict_pointed_type
)))
6087 tree heapvar
= build_fake_var_decl (fo
->restrict_pointed_type
);
6088 DECL_EXTERNAL (heapvar
) = 1;
6089 if (var_can_have_subvars (heapvar
))
6090 bitmap_set_bit (handled_struct_type
,
6091 TYPE_UID (fo
->restrict_pointed_type
));
6092 rvi
= create_variable_info_for_1 (heapvar
, "PARM_NOALIAS", true,
6093 true, handled_struct_type
);
6094 if (var_can_have_subvars (heapvar
))
6095 bitmap_clear_bit (handled_struct_type
,
6096 TYPE_UID (fo
->restrict_pointed_type
));
6097 rvi
->is_restrict_var
= 1;
6098 insert_vi_for_tree (heapvar
, rvi
);
6099 make_constraint_from (newvi
, rvi
->id
);
6100 make_param_constraints (rvi
);
6102 if (i
+ 1 < fieldstack
.length ())
6104 varinfo_t tem
= new_var_info (decl
, name
, false);
6105 newvi
->next
= tem
->id
;
6114 create_variable_info_for (tree decl
, const char *name
, bool add_id
)
6116 varinfo_t vi
= create_variable_info_for_1 (decl
, name
, add_id
, false, NULL
);
6117 unsigned int id
= vi
->id
;
6119 insert_vi_for_tree (decl
, vi
);
6124 /* Create initial constraints for globals. */
6125 for (; vi
; vi
= vi_next (vi
))
6127 if (!vi
->may_have_pointers
6128 || !vi
->is_global_var
)
6131 /* Mark global restrict qualified pointers. */
6132 if ((POINTER_TYPE_P (TREE_TYPE (decl
))
6133 && TYPE_RESTRICT (TREE_TYPE (decl
)))
6134 || vi
->only_restrict_pointers
)
6137 = make_constraint_from_global_restrict (vi
, "GLOBAL_RESTRICT",
6139 /* ??? For now exclude reads from globals as restrict sources
6140 if those are not (indirectly) from incoming parameters. */
6141 rvi
->is_restrict_var
= false;
6145 /* In non-IPA mode the initializer from nonlocal is all we need. */
6147 || DECL_HARD_REGISTER (decl
))
6148 make_copy_constraint (vi
, nonlocal_id
);
6150 /* In IPA mode parse the initializer and generate proper constraints
6154 varpool_node
*vnode
= varpool_node::get (decl
);
6156 /* For escaped variables initialize them from nonlocal. */
6157 if (!vnode
->all_refs_explicit_p ())
6158 make_copy_constraint (vi
, nonlocal_id
);
6160 /* If this is a global variable with an initializer and we are in
6161 IPA mode generate constraints for it. */
6163 for (unsigned idx
= 0; vnode
->iterate_reference (idx
, ref
); ++idx
)
6165 auto_vec
<ce_s
> rhsc
;
6166 struct constraint_expr lhs
, *rhsp
;
6168 get_constraint_for_address_of (ref
->referred
->decl
, &rhsc
);
6172 FOR_EACH_VEC_ELT (rhsc
, i
, rhsp
)
6173 process_constraint (new_constraint (lhs
, *rhsp
));
6174 /* If this is a variable that escapes from the unit
6175 the initializer escapes as well. */
6176 if (!vnode
->all_refs_explicit_p ())
6178 lhs
.var
= escaped_id
;
6181 FOR_EACH_VEC_ELT (rhsc
, i
, rhsp
)
6182 process_constraint (new_constraint (lhs
, *rhsp
));
6191 /* Print out the points-to solution for VAR to FILE. */
6194 dump_solution_for_var (FILE *file
, unsigned int var
)
6196 varinfo_t vi
= get_varinfo (var
);
6200 /* Dump the solution for unified vars anyway, this avoids difficulties
6201 in scanning dumps in the testsuite. */
6202 fprintf (file
, "%s = { ", vi
->name
);
6203 vi
= get_varinfo (find (var
));
6204 EXECUTE_IF_SET_IN_BITMAP (vi
->solution
, 0, i
, bi
)
6205 fprintf (file
, "%s ", get_varinfo (i
)->name
);
6206 fprintf (file
, "}");
6208 /* But note when the variable was unified. */
6210 fprintf (file
, " same as %s", vi
->name
);
6212 fprintf (file
, "\n");
6215 /* Print the points-to solution for VAR to stderr. */
6218 debug_solution_for_var (unsigned int var
)
6220 dump_solution_for_var (stderr
, var
);
6223 /* Register the constraints for function parameter related VI. */
6226 make_param_constraints (varinfo_t vi
)
6228 for (; vi
; vi
= vi_next (vi
))
6230 if (vi
->only_restrict_pointers
)
6232 else if (vi
->may_have_pointers
)
6233 make_constraint_from (vi
, nonlocal_id
);
6235 if (vi
->is_full_var
)
6240 /* Create varinfo structures for all of the variables in the
6241 function for intraprocedural mode. */
6244 intra_create_variable_infos (struct function
*fn
)
6247 bitmap handled_struct_type
= NULL
;
6249 /* For each incoming pointer argument arg, create the constraint ARG
6250 = NONLOCAL or a dummy variable if it is a restrict qualified
6251 passed-by-reference argument. */
6252 for (t
= DECL_ARGUMENTS (fn
->decl
); t
; t
= DECL_CHAIN (t
))
6254 if (handled_struct_type
== NULL
)
6255 handled_struct_type
= BITMAP_ALLOC (NULL
);
6258 = create_variable_info_for_1 (t
, alias_get_name (t
), false, true,
6259 handled_struct_type
);
6260 insert_vi_for_tree (t
, p
);
6262 make_param_constraints (p
);
6265 if (handled_struct_type
!= NULL
)
6266 BITMAP_FREE (handled_struct_type
);
6268 /* Add a constraint for a result decl that is passed by reference. */
6269 if (DECL_RESULT (fn
->decl
)
6270 && DECL_BY_REFERENCE (DECL_RESULT (fn
->decl
)))
6272 varinfo_t p
, result_vi
= get_vi_for_tree (DECL_RESULT (fn
->decl
));
6274 for (p
= result_vi
; p
; p
= vi_next (p
))
6275 make_constraint_from (p
, nonlocal_id
);
6278 /* Add a constraint for the incoming static chain parameter. */
6279 if (fn
->static_chain_decl
!= NULL_TREE
)
6281 varinfo_t p
, chain_vi
= get_vi_for_tree (fn
->static_chain_decl
);
6283 for (p
= chain_vi
; p
; p
= vi_next (p
))
6284 make_constraint_from (p
, nonlocal_id
);
6288 /* Structure used to put solution bitmaps in a hashtable so they can
6289 be shared among variables with the same points-to set. */
6291 typedef struct shared_bitmap_info
6295 } *shared_bitmap_info_t
;
6296 typedef const struct shared_bitmap_info
*const_shared_bitmap_info_t
;
6298 /* Shared_bitmap hashtable helpers. */
6300 struct shared_bitmap_hasher
: free_ptr_hash
<shared_bitmap_info
>
6302 static inline hashval_t
hash (const shared_bitmap_info
*);
6303 static inline bool equal (const shared_bitmap_info
*,
6304 const shared_bitmap_info
*);
6307 /* Hash function for a shared_bitmap_info_t */
6310 shared_bitmap_hasher::hash (const shared_bitmap_info
*bi
)
6312 return bi
->hashcode
;
6315 /* Equality function for two shared_bitmap_info_t's. */
6318 shared_bitmap_hasher::equal (const shared_bitmap_info
*sbi1
,
6319 const shared_bitmap_info
*sbi2
)
6321 return bitmap_equal_p (sbi1
->pt_vars
, sbi2
->pt_vars
);
6324 /* Shared_bitmap hashtable. */
6326 static hash_table
<shared_bitmap_hasher
> *shared_bitmap_table
;
6328 /* Lookup a bitmap in the shared bitmap hashtable, and return an already
6329 existing instance if there is one, NULL otherwise. */
6332 shared_bitmap_lookup (bitmap pt_vars
)
6334 shared_bitmap_info
**slot
;
6335 struct shared_bitmap_info sbi
;
6337 sbi
.pt_vars
= pt_vars
;
6338 sbi
.hashcode
= bitmap_hash (pt_vars
);
6340 slot
= shared_bitmap_table
->find_slot (&sbi
, NO_INSERT
);
6344 return (*slot
)->pt_vars
;
6348 /* Add a bitmap to the shared bitmap hashtable. */
6351 shared_bitmap_add (bitmap pt_vars
)
6353 shared_bitmap_info
**slot
;
6354 shared_bitmap_info_t sbi
= XNEW (struct shared_bitmap_info
);
6356 sbi
->pt_vars
= pt_vars
;
6357 sbi
->hashcode
= bitmap_hash (pt_vars
);
6359 slot
= shared_bitmap_table
->find_slot (sbi
, INSERT
);
6360 gcc_assert (!*slot
);
6365 /* Set bits in INTO corresponding to the variable uids in solution set FROM. */
6368 set_uids_in_ptset (bitmap into
, bitmap from
, struct pt_solution
*pt
,
6373 varinfo_t escaped_vi
= get_varinfo (find (escaped_id
));
6374 bool everything_escaped
6375 = escaped_vi
->solution
&& bitmap_bit_p (escaped_vi
->solution
, anything_id
);
6377 EXECUTE_IF_SET_IN_BITMAP (from
, 0, i
, bi
)
6379 varinfo_t vi
= get_varinfo (i
);
6381 /* The only artificial variables that are allowed in a may-alias
6382 set are heap variables. */
6383 if (vi
->is_artificial_var
&& !vi
->is_heap_var
)
6386 if (everything_escaped
6387 || (escaped_vi
->solution
6388 && bitmap_bit_p (escaped_vi
->solution
, i
)))
6390 pt
->vars_contains_escaped
= true;
6391 pt
->vars_contains_escaped_heap
= vi
->is_heap_var
;
6394 if (vi
->is_restrict_var
)
6395 pt
->vars_contains_restrict
= true;
6397 if (VAR_P (vi
->decl
)
6398 || TREE_CODE (vi
->decl
) == PARM_DECL
6399 || TREE_CODE (vi
->decl
) == RESULT_DECL
)
6401 /* If we are in IPA mode we will not recompute points-to
6402 sets after inlining so make sure they stay valid. */
6404 && !DECL_PT_UID_SET_P (vi
->decl
))
6405 SET_DECL_PT_UID (vi
->decl
, DECL_UID (vi
->decl
));
6407 /* Add the decl to the points-to set. Note that the points-to
6408 set contains global variables. */
6409 bitmap_set_bit (into
, DECL_PT_UID (vi
->decl
));
6410 if (vi
->is_global_var
6411 /* In IPA mode the escaped_heap trick doesn't work as
6412 ESCAPED is escaped from the unit but
6413 pt_solution_includes_global needs to answer true for
6414 all variables not automatic within a function.
6415 For the same reason is_global_var is not the
6416 correct flag to track - local variables from other
6417 functions also need to be considered global.
6418 Conveniently all HEAP vars are not put in function
6422 && ! auto_var_in_fn_p (vi
->decl
, fndecl
)))
6423 pt
->vars_contains_nonlocal
= true;
6425 /* If we have a variable that is interposable record that fact
6426 for pointer comparison simplification. */
6427 if (VAR_P (vi
->decl
)
6428 && (TREE_STATIC (vi
->decl
) || DECL_EXTERNAL (vi
->decl
))
6429 && ! decl_binds_to_current_def_p (vi
->decl
))
6430 pt
->vars_contains_interposable
= true;
6433 else if (TREE_CODE (vi
->decl
) == FUNCTION_DECL
6434 || TREE_CODE (vi
->decl
) == LABEL_DECL
)
6436 /* Nothing should read/write from/to code so we can
6437 save bits by not including them in the points-to bitmaps.
6438 Still mark the points-to set as containing global memory
6439 to make code-patching possible - see PR70128. */
6440 pt
->vars_contains_nonlocal
= true;
6446 /* Compute the points-to solution *PT for the variable VI. */
6448 static struct pt_solution
6449 find_what_var_points_to (tree fndecl
, varinfo_t orig_vi
)
6453 bitmap finished_solution
;
6456 struct pt_solution
*pt
;
6458 /* This variable may have been collapsed, let's get the real
6460 vi
= get_varinfo (find (orig_vi
->id
));
6462 /* See if we have already computed the solution and return it. */
6463 pt_solution
**slot
= &final_solutions
->get_or_insert (vi
);
6467 *slot
= pt
= XOBNEW (&final_solutions_obstack
, struct pt_solution
);
6468 memset (pt
, 0, sizeof (struct pt_solution
));
6470 /* Translate artificial variables into SSA_NAME_PTR_INFO
6472 EXECUTE_IF_SET_IN_BITMAP (vi
->solution
, 0, i
, bi
)
6474 varinfo_t vi
= get_varinfo (i
);
6476 if (vi
->is_artificial_var
)
6478 if (vi
->id
== nothing_id
)
6480 else if (vi
->id
== escaped_id
)
6483 pt
->ipa_escaped
= 1;
6486 /* Expand some special vars of ESCAPED in-place here. */
6487 varinfo_t evi
= get_varinfo (find (escaped_id
));
6488 if (bitmap_bit_p (evi
->solution
, nonlocal_id
))
6491 else if (vi
->id
== nonlocal_id
)
6493 else if (vi
->is_heap_var
)
6494 /* We represent heapvars in the points-to set properly. */
6496 else if (vi
->id
== string_id
)
6497 /* Nobody cares - STRING_CSTs are read-only entities. */
6499 else if (vi
->id
== anything_id
6500 || vi
->id
== integer_id
)
6505 /* Instead of doing extra work, simply do not create
6506 elaborate points-to information for pt_anything pointers. */
6510 /* Share the final set of variables when possible. */
6511 finished_solution
= BITMAP_GGC_ALLOC ();
6512 stats
.points_to_sets_created
++;
6514 set_uids_in_ptset (finished_solution
, vi
->solution
, pt
, fndecl
);
6515 result
= shared_bitmap_lookup (finished_solution
);
6518 shared_bitmap_add (finished_solution
);
6519 pt
->vars
= finished_solution
;
6524 bitmap_clear (finished_solution
);
6530 /* Given a pointer variable P, fill in its points-to set. */
6533 find_what_p_points_to (tree fndecl
, tree p
)
6535 struct ptr_info_def
*pi
;
6538 bool nonnull
= get_ptr_nonnull (p
);
6540 /* For parameters, get at the points-to set for the actual parm
6542 if (TREE_CODE (p
) == SSA_NAME
6543 && SSA_NAME_IS_DEFAULT_DEF (p
)
6544 && (TREE_CODE (SSA_NAME_VAR (p
)) == PARM_DECL
6545 || TREE_CODE (SSA_NAME_VAR (p
)) == RESULT_DECL
))
6546 lookup_p
= SSA_NAME_VAR (p
);
6548 vi
= lookup_vi_for_tree (lookup_p
);
6552 pi
= get_ptr_info (p
);
6553 pi
->pt
= find_what_var_points_to (fndecl
, vi
);
6554 /* Conservatively set to NULL from PTA (to true). */
6556 /* Preserve pointer nonnull computed by VRP. See get_ptr_nonnull
6557 in gcc/tree-ssaname.c for more information. */
6559 set_ptr_nonnull (p
);
6563 /* Query statistics for points-to solutions. */
6566 unsigned HOST_WIDE_INT pt_solution_includes_may_alias
;
6567 unsigned HOST_WIDE_INT pt_solution_includes_no_alias
;
6568 unsigned HOST_WIDE_INT pt_solutions_intersect_may_alias
;
6569 unsigned HOST_WIDE_INT pt_solutions_intersect_no_alias
;
6573 dump_pta_stats (FILE *s
)
6575 fprintf (s
, "\nPTA query stats:\n");
6576 fprintf (s
, " pt_solution_includes: "
6577 HOST_WIDE_INT_PRINT_DEC
" disambiguations, "
6578 HOST_WIDE_INT_PRINT_DEC
" queries\n",
6579 pta_stats
.pt_solution_includes_no_alias
,
6580 pta_stats
.pt_solution_includes_no_alias
6581 + pta_stats
.pt_solution_includes_may_alias
);
6582 fprintf (s
, " pt_solutions_intersect: "
6583 HOST_WIDE_INT_PRINT_DEC
" disambiguations, "
6584 HOST_WIDE_INT_PRINT_DEC
" queries\n",
6585 pta_stats
.pt_solutions_intersect_no_alias
,
6586 pta_stats
.pt_solutions_intersect_no_alias
6587 + pta_stats
.pt_solutions_intersect_may_alias
);
6591 /* Reset the points-to solution *PT to a conservative default
6592 (point to anything). */
6595 pt_solution_reset (struct pt_solution
*pt
)
6597 memset (pt
, 0, sizeof (struct pt_solution
));
6598 pt
->anything
= true;
6602 /* Set the points-to solution *PT to point only to the variables
6603 in VARS. VARS_CONTAINS_GLOBAL specifies whether that contains
6604 global variables and VARS_CONTAINS_RESTRICT specifies whether
6605 it contains restrict tag variables. */
6608 pt_solution_set (struct pt_solution
*pt
, bitmap vars
,
6609 bool vars_contains_nonlocal
)
6611 memset (pt
, 0, sizeof (struct pt_solution
));
6613 pt
->vars_contains_nonlocal
= vars_contains_nonlocal
;
6614 pt
->vars_contains_escaped
6615 = (cfun
->gimple_df
->escaped
.anything
6616 || bitmap_intersect_p (cfun
->gimple_df
->escaped
.vars
, vars
));
6619 /* Set the points-to solution *PT to point only to the variable VAR. */
6622 pt_solution_set_var (struct pt_solution
*pt
, tree var
)
6624 memset (pt
, 0, sizeof (struct pt_solution
));
6625 pt
->vars
= BITMAP_GGC_ALLOC ();
6626 bitmap_set_bit (pt
->vars
, DECL_PT_UID (var
));
6627 pt
->vars_contains_nonlocal
= is_global_var (var
);
6628 pt
->vars_contains_escaped
6629 = (cfun
->gimple_df
->escaped
.anything
6630 || bitmap_bit_p (cfun
->gimple_df
->escaped
.vars
, DECL_PT_UID (var
)));
6633 /* Computes the union of the points-to solutions *DEST and *SRC and
6634 stores the result in *DEST. This changes the points-to bitmap
6635 of *DEST and thus may not be used if that might be shared.
6636 The points-to bitmap of *SRC and *DEST will not be shared after
6637 this function if they were not before. */
6640 pt_solution_ior_into (struct pt_solution
*dest
, struct pt_solution
*src
)
6642 dest
->anything
|= src
->anything
;
6645 pt_solution_reset (dest
);
6649 dest
->nonlocal
|= src
->nonlocal
;
6650 dest
->escaped
|= src
->escaped
;
6651 dest
->ipa_escaped
|= src
->ipa_escaped
;
6652 dest
->null
|= src
->null
;
6653 dest
->vars_contains_nonlocal
|= src
->vars_contains_nonlocal
;
6654 dest
->vars_contains_escaped
|= src
->vars_contains_escaped
;
6655 dest
->vars_contains_escaped_heap
|= src
->vars_contains_escaped_heap
;
6660 dest
->vars
= BITMAP_GGC_ALLOC ();
6661 bitmap_ior_into (dest
->vars
, src
->vars
);
6664 /* Return true if the points-to solution *PT is empty. */
6667 pt_solution_empty_p (struct pt_solution
*pt
)
6674 && !bitmap_empty_p (pt
->vars
))
6677 /* If the solution includes ESCAPED, check if that is empty. */
6679 && !pt_solution_empty_p (&cfun
->gimple_df
->escaped
))
6682 /* If the solution includes ESCAPED, check if that is empty. */
6684 && !pt_solution_empty_p (&ipa_escaped_pt
))
6690 /* Return true if the points-to solution *PT only point to a single var, and
6691 return the var uid in *UID. */
6694 pt_solution_singleton_or_null_p (struct pt_solution
*pt
, unsigned *uid
)
6696 if (pt
->anything
|| pt
->nonlocal
|| pt
->escaped
|| pt
->ipa_escaped
6698 || !bitmap_single_bit_set_p (pt
->vars
))
6701 *uid
= bitmap_first_set_bit (pt
->vars
);
6705 /* Return true if the points-to solution *PT includes global memory. */
6708 pt_solution_includes_global (struct pt_solution
*pt
)
6712 || pt
->vars_contains_nonlocal
6713 /* The following is a hack to make the malloc escape hack work.
6714 In reality we'd need different sets for escaped-through-return
6715 and escaped-to-callees and passes would need to be updated. */
6716 || pt
->vars_contains_escaped_heap
)
6719 /* 'escaped' is also a placeholder so we have to look into it. */
6721 return pt_solution_includes_global (&cfun
->gimple_df
->escaped
);
6723 if (pt
->ipa_escaped
)
6724 return pt_solution_includes_global (&ipa_escaped_pt
);
6729 /* Return true if the points-to solution *PT includes the variable
6730 declaration DECL. */
6733 pt_solution_includes_1 (struct pt_solution
*pt
, const_tree decl
)
6739 && is_global_var (decl
))
6743 && bitmap_bit_p (pt
->vars
, DECL_PT_UID (decl
)))
6746 /* If the solution includes ESCAPED, check it. */
6748 && pt_solution_includes_1 (&cfun
->gimple_df
->escaped
, decl
))
6751 /* If the solution includes ESCAPED, check it. */
6753 && pt_solution_includes_1 (&ipa_escaped_pt
, decl
))
6760 pt_solution_includes (struct pt_solution
*pt
, const_tree decl
)
6762 bool res
= pt_solution_includes_1 (pt
, decl
);
6764 ++pta_stats
.pt_solution_includes_may_alias
;
6766 ++pta_stats
.pt_solution_includes_no_alias
;
6770 /* Return true if both points-to solutions PT1 and PT2 have a non-empty
6774 pt_solutions_intersect_1 (struct pt_solution
*pt1
, struct pt_solution
*pt2
)
6776 if (pt1
->anything
|| pt2
->anything
)
6779 /* If either points to unknown global memory and the other points to
6780 any global memory they alias. */
6783 || pt2
->vars_contains_nonlocal
))
6785 && pt1
->vars_contains_nonlocal
))
6788 /* If either points to all escaped memory and the other points to
6789 any escaped memory they alias. */
6792 || pt2
->vars_contains_escaped
))
6794 && pt1
->vars_contains_escaped
))
6797 /* Check the escaped solution if required.
6798 ??? Do we need to check the local against the IPA escaped sets? */
6799 if ((pt1
->ipa_escaped
|| pt2
->ipa_escaped
)
6800 && !pt_solution_empty_p (&ipa_escaped_pt
))
6802 /* If both point to escaped memory and that solution
6803 is not empty they alias. */
6804 if (pt1
->ipa_escaped
&& pt2
->ipa_escaped
)
6807 /* If either points to escaped memory see if the escaped solution
6808 intersects with the other. */
6809 if ((pt1
->ipa_escaped
6810 && pt_solutions_intersect_1 (&ipa_escaped_pt
, pt2
))
6811 || (pt2
->ipa_escaped
6812 && pt_solutions_intersect_1 (&ipa_escaped_pt
, pt1
)))
6816 /* Now both pointers alias if their points-to solution intersects. */
6819 && bitmap_intersect_p (pt1
->vars
, pt2
->vars
));
6823 pt_solutions_intersect (struct pt_solution
*pt1
, struct pt_solution
*pt2
)
6825 bool res
= pt_solutions_intersect_1 (pt1
, pt2
);
6827 ++pta_stats
.pt_solutions_intersect_may_alias
;
6829 ++pta_stats
.pt_solutions_intersect_no_alias
;
6834 /* Dump points-to information to OUTFILE. */
6837 dump_sa_points_to_info (FILE *outfile
)
6841 fprintf (outfile
, "\nPoints-to sets\n\n");
6843 if (dump_flags
& TDF_STATS
)
6845 fprintf (outfile
, "Stats:\n");
6846 fprintf (outfile
, "Total vars: %d\n", stats
.total_vars
);
6847 fprintf (outfile
, "Non-pointer vars: %d\n",
6848 stats
.nonpointer_vars
);
6849 fprintf (outfile
, "Statically unified vars: %d\n",
6850 stats
.unified_vars_static
);
6851 fprintf (outfile
, "Dynamically unified vars: %d\n",
6852 stats
.unified_vars_dynamic
);
6853 fprintf (outfile
, "Iterations: %d\n", stats
.iterations
);
6854 fprintf (outfile
, "Number of edges: %d\n", stats
.num_edges
);
6855 fprintf (outfile
, "Number of implicit edges: %d\n",
6856 stats
.num_implicit_edges
);
6859 for (i
= 1; i
< varmap
.length (); i
++)
6861 varinfo_t vi
= get_varinfo (i
);
6862 if (!vi
->may_have_pointers
)
6864 dump_solution_for_var (outfile
, i
);
6869 /* Debug points-to information to stderr. */
6872 debug_sa_points_to_info (void)
6874 dump_sa_points_to_info (stderr
);
6878 /* Initialize the always-existing constraint variables for NULL
6879 ANYTHING, READONLY, and INTEGER */
6882 init_base_vars (void)
6884 struct constraint_expr lhs
, rhs
;
6885 varinfo_t var_anything
;
6886 varinfo_t var_nothing
;
6887 varinfo_t var_string
;
6888 varinfo_t var_escaped
;
6889 varinfo_t var_nonlocal
;
6890 varinfo_t var_storedanything
;
6891 varinfo_t var_integer
;
6893 /* Variable ID zero is reserved and should be NULL. */
6894 varmap
.safe_push (NULL
);
6896 /* Create the NULL variable, used to represent that a variable points
6898 var_nothing
= new_var_info (NULL_TREE
, "NULL", false);
6899 gcc_assert (var_nothing
->id
== nothing_id
);
6900 var_nothing
->is_artificial_var
= 1;
6901 var_nothing
->offset
= 0;
6902 var_nothing
->size
= ~0;
6903 var_nothing
->fullsize
= ~0;
6904 var_nothing
->is_special_var
= 1;
6905 var_nothing
->may_have_pointers
= 0;
6906 var_nothing
->is_global_var
= 0;
6908 /* Create the ANYTHING variable, used to represent that a variable
6909 points to some unknown piece of memory. */
6910 var_anything
= new_var_info (NULL_TREE
, "ANYTHING", false);
6911 gcc_assert (var_anything
->id
== anything_id
);
6912 var_anything
->is_artificial_var
= 1;
6913 var_anything
->size
= ~0;
6914 var_anything
->offset
= 0;
6915 var_anything
->fullsize
= ~0;
6916 var_anything
->is_special_var
= 1;
6918 /* Anything points to anything. This makes deref constraints just
6919 work in the presence of linked list and other p = *p type loops,
6920 by saying that *ANYTHING = ANYTHING. */
6922 lhs
.var
= anything_id
;
6924 rhs
.type
= ADDRESSOF
;
6925 rhs
.var
= anything_id
;
6928 /* This specifically does not use process_constraint because
6929 process_constraint ignores all anything = anything constraints, since all
6930 but this one are redundant. */
6931 constraints
.safe_push (new_constraint (lhs
, rhs
));
6933 /* Create the STRING variable, used to represent that a variable
6934 points to a string literal. String literals don't contain
6935 pointers so STRING doesn't point to anything. */
6936 var_string
= new_var_info (NULL_TREE
, "STRING", false);
6937 gcc_assert (var_string
->id
== string_id
);
6938 var_string
->is_artificial_var
= 1;
6939 var_string
->offset
= 0;
6940 var_string
->size
= ~0;
6941 var_string
->fullsize
= ~0;
6942 var_string
->is_special_var
= 1;
6943 var_string
->may_have_pointers
= 0;
6945 /* Create the ESCAPED variable, used to represent the set of escaped
6947 var_escaped
= new_var_info (NULL_TREE
, "ESCAPED", false);
6948 gcc_assert (var_escaped
->id
== escaped_id
);
6949 var_escaped
->is_artificial_var
= 1;
6950 var_escaped
->offset
= 0;
6951 var_escaped
->size
= ~0;
6952 var_escaped
->fullsize
= ~0;
6953 var_escaped
->is_special_var
= 0;
6955 /* Create the NONLOCAL variable, used to represent the set of nonlocal
6957 var_nonlocal
= new_var_info (NULL_TREE
, "NONLOCAL", false);
6958 gcc_assert (var_nonlocal
->id
== nonlocal_id
);
6959 var_nonlocal
->is_artificial_var
= 1;
6960 var_nonlocal
->offset
= 0;
6961 var_nonlocal
->size
= ~0;
6962 var_nonlocal
->fullsize
= ~0;
6963 var_nonlocal
->is_special_var
= 1;
6965 /* ESCAPED = *ESCAPED, because escaped is may-deref'd at calls, etc. */
6967 lhs
.var
= escaped_id
;
6970 rhs
.var
= escaped_id
;
6972 process_constraint (new_constraint (lhs
, rhs
));
6974 /* ESCAPED = ESCAPED + UNKNOWN_OFFSET, because if a sub-field escapes the
6975 whole variable escapes. */
6977 lhs
.var
= escaped_id
;
6980 rhs
.var
= escaped_id
;
6981 rhs
.offset
= UNKNOWN_OFFSET
;
6982 process_constraint (new_constraint (lhs
, rhs
));
6984 /* *ESCAPED = NONLOCAL. This is true because we have to assume
6985 everything pointed to by escaped points to what global memory can
6988 lhs
.var
= escaped_id
;
6991 rhs
.var
= nonlocal_id
;
6993 process_constraint (new_constraint (lhs
, rhs
));
6995 /* NONLOCAL = &NONLOCAL, NONLOCAL = &ESCAPED. This is true because
6996 global memory may point to global memory and escaped memory. */
6998 lhs
.var
= nonlocal_id
;
7000 rhs
.type
= ADDRESSOF
;
7001 rhs
.var
= nonlocal_id
;
7003 process_constraint (new_constraint (lhs
, rhs
));
7004 rhs
.type
= ADDRESSOF
;
7005 rhs
.var
= escaped_id
;
7007 process_constraint (new_constraint (lhs
, rhs
));
7009 /* Create the STOREDANYTHING variable, used to represent the set of
7010 variables stored to *ANYTHING. */
7011 var_storedanything
= new_var_info (NULL_TREE
, "STOREDANYTHING", false);
7012 gcc_assert (var_storedanything
->id
== storedanything_id
);
7013 var_storedanything
->is_artificial_var
= 1;
7014 var_storedanything
->offset
= 0;
7015 var_storedanything
->size
= ~0;
7016 var_storedanything
->fullsize
= ~0;
7017 var_storedanything
->is_special_var
= 0;
7019 /* Create the INTEGER variable, used to represent that a variable points
7020 to what an INTEGER "points to". */
7021 var_integer
= new_var_info (NULL_TREE
, "INTEGER", false);
7022 gcc_assert (var_integer
->id
== integer_id
);
7023 var_integer
->is_artificial_var
= 1;
7024 var_integer
->size
= ~0;
7025 var_integer
->fullsize
= ~0;
7026 var_integer
->offset
= 0;
7027 var_integer
->is_special_var
= 1;
7029 /* INTEGER = ANYTHING, because we don't know where a dereference of
7030 a random integer will point to. */
7032 lhs
.var
= integer_id
;
7034 rhs
.type
= ADDRESSOF
;
7035 rhs
.var
= anything_id
;
7037 process_constraint (new_constraint (lhs
, rhs
));
7040 /* Initialize things necessary to perform PTA */
7043 init_alias_vars (void)
7045 use_field_sensitive
= (MAX_FIELDS_FOR_FIELD_SENSITIVE
> 1);
7047 bitmap_obstack_initialize (&pta_obstack
);
7048 bitmap_obstack_initialize (&oldpta_obstack
);
7049 bitmap_obstack_initialize (&predbitmap_obstack
);
7051 constraints
.create (8);
7053 vi_for_tree
= new hash_map
<tree
, varinfo_t
>;
7054 call_stmt_vars
= new hash_map
<gimple
*, varinfo_t
>;
7056 memset (&stats
, 0, sizeof (stats
));
7057 shared_bitmap_table
= new hash_table
<shared_bitmap_hasher
> (511);
7060 gcc_obstack_init (&fake_var_decl_obstack
);
7062 final_solutions
= new hash_map
<varinfo_t
, pt_solution
*>;
7063 gcc_obstack_init (&final_solutions_obstack
);
7066 /* Remove the REF and ADDRESS edges from GRAPH, as well as all the
7067 predecessor edges. */
7070 remove_preds_and_fake_succs (constraint_graph_t graph
)
7074 /* Clear the implicit ref and address nodes from the successor
7076 for (i
= 1; i
< FIRST_REF_NODE
; i
++)
7078 if (graph
->succs
[i
])
7079 bitmap_clear_range (graph
->succs
[i
], FIRST_REF_NODE
,
7080 FIRST_REF_NODE
* 2);
7083 /* Free the successor list for the non-ref nodes. */
7084 for (i
= FIRST_REF_NODE
+ 1; i
< graph
->size
; i
++)
7086 if (graph
->succs
[i
])
7087 BITMAP_FREE (graph
->succs
[i
]);
7090 /* Now reallocate the size of the successor list as, and blow away
7091 the predecessor bitmaps. */
7092 graph
->size
= varmap
.length ();
7093 graph
->succs
= XRESIZEVEC (bitmap
, graph
->succs
, graph
->size
);
7095 free (graph
->implicit_preds
);
7096 graph
->implicit_preds
= NULL
;
7097 free (graph
->preds
);
7098 graph
->preds
= NULL
;
7099 bitmap_obstack_release (&predbitmap_obstack
);
7102 /* Solve the constraint set. */
7105 solve_constraints (void)
7107 struct scc_info
*si
;
7109 /* Sort varinfos so that ones that cannot be pointed to are last.
7110 This makes bitmaps more efficient. */
7111 unsigned int *map
= XNEWVEC (unsigned int, varmap
.length ());
7112 for (unsigned i
= 0; i
< integer_id
+ 1; ++i
)
7114 /* Start with non-register vars (as possibly address-taken), followed
7115 by register vars as conservative set of vars never appearing in
7116 the points-to solution bitmaps. */
7117 unsigned j
= integer_id
+ 1;
7118 for (unsigned i
= integer_id
+ 1; i
< varmap
.length (); ++i
)
7119 if (! varmap
[i
]->is_reg_var
)
7121 for (unsigned i
= integer_id
+ 1; i
< varmap
.length (); ++i
)
7122 if (varmap
[i
]->is_reg_var
)
7124 /* Shuffle varmap according to map. */
7125 for (unsigned i
= integer_id
+ 1; i
< varmap
.length (); ++i
)
7127 while (map
[varmap
[i
]->id
] != i
)
7128 std::swap (varmap
[i
], varmap
[map
[varmap
[i
]->id
]]);
7129 gcc_assert (bitmap_empty_p (varmap
[i
]->solution
));
7131 varmap
[i
]->next
= map
[varmap
[i
]->next
];
7132 varmap
[i
]->head
= map
[varmap
[i
]->head
];
7134 /* Finally rewrite constraints. */
7135 for (unsigned i
= 0; i
< constraints
.length (); ++i
)
7137 constraints
[i
]->lhs
.var
= map
[constraints
[i
]->lhs
.var
];
7138 constraints
[i
]->rhs
.var
= map
[constraints
[i
]->rhs
.var
];
7144 "\nCollapsing static cycles and doing variable "
7147 init_graph (varmap
.length () * 2);
7150 fprintf (dump_file
, "Building predecessor graph\n");
7151 build_pred_graph ();
7154 fprintf (dump_file
, "Detecting pointer and location "
7156 si
= perform_var_substitution (graph
);
7159 fprintf (dump_file
, "Rewriting constraints and unifying "
7161 rewrite_constraints (graph
, si
);
7163 build_succ_graph ();
7165 free_var_substitution_info (si
);
7167 /* Attach complex constraints to graph nodes. */
7168 move_complex_constraints (graph
);
7171 fprintf (dump_file
, "Uniting pointer but not location equivalent "
7173 unite_pointer_equivalences (graph
);
7176 fprintf (dump_file
, "Finding indirect cycles\n");
7177 find_indirect_cycles (graph
);
7179 /* Implicit nodes and predecessors are no longer necessary at this
7181 remove_preds_and_fake_succs (graph
);
7183 if (dump_file
&& (dump_flags
& TDF_GRAPH
))
7185 fprintf (dump_file
, "\n\n// The constraint graph before solve-graph "
7186 "in dot format:\n");
7187 dump_constraint_graph (dump_file
);
7188 fprintf (dump_file
, "\n\n");
7192 fprintf (dump_file
, "Solving graph\n");
7194 solve_graph (graph
);
7196 if (dump_file
&& (dump_flags
& TDF_GRAPH
))
7198 fprintf (dump_file
, "\n\n// The constraint graph after solve-graph "
7199 "in dot format:\n");
7200 dump_constraint_graph (dump_file
);
7201 fprintf (dump_file
, "\n\n");
7205 dump_sa_points_to_info (dump_file
);
7208 /* Create points-to sets for the current function. See the comments
7209 at the start of the file for an algorithmic overview. */
7212 compute_points_to_sets (void)
7217 timevar_push (TV_TREE_PTA
);
7221 intra_create_variable_infos (cfun
);
7223 /* Now walk all statements and build the constraint set. */
7224 FOR_EACH_BB_FN (bb
, cfun
)
7226 for (gphi_iterator gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
);
7229 gphi
*phi
= gsi
.phi ();
7231 if (! virtual_operand_p (gimple_phi_result (phi
)))
7232 find_func_aliases (cfun
, phi
);
7235 for (gimple_stmt_iterator gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
);
7238 gimple
*stmt
= gsi_stmt (gsi
);
7240 find_func_aliases (cfun
, stmt
);
7246 fprintf (dump_file
, "Points-to analysis\n\nConstraints:\n\n");
7247 dump_constraints (dump_file
, 0);
7250 /* From the constraints compute the points-to sets. */
7251 solve_constraints ();
7253 /* Compute the points-to set for ESCAPED used for call-clobber analysis. */
7254 cfun
->gimple_df
->escaped
= find_what_var_points_to (cfun
->decl
,
7255 get_varinfo (escaped_id
));
7257 /* Make sure the ESCAPED solution (which is used as placeholder in
7258 other solutions) does not reference itself. This simplifies
7259 points-to solution queries. */
7260 cfun
->gimple_df
->escaped
.escaped
= 0;
7262 /* Compute the points-to sets for pointer SSA_NAMEs. */
7266 FOR_EACH_SSA_NAME (i
, ptr
, cfun
)
7268 if (POINTER_TYPE_P (TREE_TYPE (ptr
)))
7269 find_what_p_points_to (cfun
->decl
, ptr
);
7272 /* Compute the call-used/clobbered sets. */
7273 FOR_EACH_BB_FN (bb
, cfun
)
7275 gimple_stmt_iterator gsi
;
7277 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
7280 struct pt_solution
*pt
;
7282 stmt
= dyn_cast
<gcall
*> (gsi_stmt (gsi
));
7286 pt
= gimple_call_use_set (stmt
);
7287 if (gimple_call_flags (stmt
) & ECF_CONST
)
7288 memset (pt
, 0, sizeof (struct pt_solution
));
7289 else if ((vi
= lookup_call_use_vi (stmt
)) != NULL
)
7291 *pt
= find_what_var_points_to (cfun
->decl
, vi
);
7292 /* Escaped (and thus nonlocal) variables are always
7293 implicitly used by calls. */
7294 /* ??? ESCAPED can be empty even though NONLOCAL
7301 /* If there is nothing special about this call then
7302 we have made everything that is used also escape. */
7303 *pt
= cfun
->gimple_df
->escaped
;
7307 pt
= gimple_call_clobber_set (stmt
);
7308 if (gimple_call_flags (stmt
) & (ECF_CONST
|ECF_PURE
|ECF_NOVOPS
))
7309 memset (pt
, 0, sizeof (struct pt_solution
));
7310 else if ((vi
= lookup_call_clobber_vi (stmt
)) != NULL
)
7312 *pt
= find_what_var_points_to (cfun
->decl
, vi
);
7313 /* Escaped (and thus nonlocal) variables are always
7314 implicitly clobbered by calls. */
7315 /* ??? ESCAPED can be empty even though NONLOCAL
7322 /* If there is nothing special about this call then
7323 we have made everything that is used also escape. */
7324 *pt
= cfun
->gimple_df
->escaped
;
7330 timevar_pop (TV_TREE_PTA
);
7334 /* Delete created points-to sets. */
7337 delete_points_to_sets (void)
7341 delete shared_bitmap_table
;
7342 shared_bitmap_table
= NULL
;
7343 if (dump_file
&& (dump_flags
& TDF_STATS
))
7344 fprintf (dump_file
, "Points to sets created:%d\n",
7345 stats
.points_to_sets_created
);
7348 delete call_stmt_vars
;
7349 bitmap_obstack_release (&pta_obstack
);
7350 constraints
.release ();
7352 for (i
= 0; i
< graph
->size
; i
++)
7353 graph
->complex[i
].release ();
7354 free (graph
->complex);
7357 free (graph
->succs
);
7359 free (graph
->pe_rep
);
7360 free (graph
->indirect_cycles
);
7364 variable_info_pool
.release ();
7365 constraint_pool
.release ();
7367 obstack_free (&fake_var_decl_obstack
, NULL
);
7369 delete final_solutions
;
7370 obstack_free (&final_solutions_obstack
, NULL
);
7375 unsigned short clique
;
7379 /* Mark "other" loads and stores as belonging to CLIQUE and with
7383 visit_loadstore (gimple
*, tree base
, tree ref
, void *data
)
7385 unsigned short clique
= ((vls_data
*) data
)->clique
;
7386 bitmap rvars
= ((vls_data
*) data
)->rvars
;
7387 if (TREE_CODE (base
) == MEM_REF
7388 || TREE_CODE (base
) == TARGET_MEM_REF
)
7390 tree ptr
= TREE_OPERAND (base
, 0);
7391 if (TREE_CODE (ptr
) == SSA_NAME
)
7393 /* For parameters, get at the points-to set for the actual parm
7395 if (SSA_NAME_IS_DEFAULT_DEF (ptr
)
7396 && (TREE_CODE (SSA_NAME_VAR (ptr
)) == PARM_DECL
7397 || TREE_CODE (SSA_NAME_VAR (ptr
)) == RESULT_DECL
))
7398 ptr
= SSA_NAME_VAR (ptr
);
7400 /* We need to make sure 'ptr' doesn't include any of
7401 the restrict tags we added bases for in its points-to set. */
7402 varinfo_t vi
= lookup_vi_for_tree (ptr
);
7406 vi
= get_varinfo (find (vi
->id
));
7407 if (bitmap_intersect_p (rvars
, vi
->solution
))
7411 /* Do not overwrite existing cliques (that includes clique, base
7412 pairs we just set). */
7413 if (MR_DEPENDENCE_CLIQUE (base
) == 0)
7415 MR_DEPENDENCE_CLIQUE (base
) = clique
;
7416 MR_DEPENDENCE_BASE (base
) = 0;
7420 /* For plain decl accesses see whether they are accesses to globals
7421 and rewrite them to MEM_REFs with { clique, 0 }. */
7423 && is_global_var (base
)
7424 /* ??? We can't rewrite a plain decl with the walk_stmt_load_store
7429 while (handled_component_p (*basep
))
7430 basep
= &TREE_OPERAND (*basep
, 0);
7431 gcc_assert (VAR_P (*basep
));
7432 tree ptr
= build_fold_addr_expr (*basep
);
7433 tree zero
= build_int_cst (TREE_TYPE (ptr
), 0);
7434 *basep
= build2 (MEM_REF
, TREE_TYPE (*basep
), ptr
, zero
);
7435 MR_DEPENDENCE_CLIQUE (*basep
) = clique
;
7436 MR_DEPENDENCE_BASE (*basep
) = 0;
7442 /* If REF is a MEM_REF then assign a clique, base pair to it, updating
7443 CLIQUE, *RESTRICT_VAR and LAST_RUID. Return whether dependence info
7444 was assigned to REF. */
7447 maybe_set_dependence_info (tree ref
, tree ptr
,
7448 unsigned short &clique
, varinfo_t restrict_var
,
7449 unsigned short &last_ruid
)
7451 while (handled_component_p (ref
))
7452 ref
= TREE_OPERAND (ref
, 0);
7453 if ((TREE_CODE (ref
) == MEM_REF
7454 || TREE_CODE (ref
) == TARGET_MEM_REF
)
7455 && TREE_OPERAND (ref
, 0) == ptr
)
7457 /* Do not overwrite existing cliques. This avoids overwriting dependence
7458 info inlined from a function with restrict parameters inlined
7459 into a function with restrict parameters. This usually means we
7460 prefer to be precise in innermost loops. */
7461 if (MR_DEPENDENCE_CLIQUE (ref
) == 0)
7464 clique
= ++cfun
->last_clique
;
7465 if (restrict_var
->ruid
== 0)
7466 restrict_var
->ruid
= ++last_ruid
;
7467 MR_DEPENDENCE_CLIQUE (ref
) = clique
;
7468 MR_DEPENDENCE_BASE (ref
) = restrict_var
->ruid
;
7475 /* Compute the set of independend memory references based on restrict
7476 tags and their conservative propagation to the points-to sets. */
7479 compute_dependence_clique (void)
7481 unsigned short clique
= 0;
7482 unsigned short last_ruid
= 0;
7483 bitmap rvars
= BITMAP_ALLOC (NULL
);
7484 for (unsigned i
= 0; i
< num_ssa_names
; ++i
)
7486 tree ptr
= ssa_name (i
);
7487 if (!ptr
|| !POINTER_TYPE_P (TREE_TYPE (ptr
)))
7490 /* Avoid all this when ptr is not dereferenced? */
7492 if (SSA_NAME_IS_DEFAULT_DEF (ptr
)
7493 && (TREE_CODE (SSA_NAME_VAR (ptr
)) == PARM_DECL
7494 || TREE_CODE (SSA_NAME_VAR (ptr
)) == RESULT_DECL
))
7495 p
= SSA_NAME_VAR (ptr
);
7496 varinfo_t vi
= lookup_vi_for_tree (p
);
7499 vi
= get_varinfo (find (vi
->id
));
7502 varinfo_t restrict_var
= NULL
;
7503 EXECUTE_IF_SET_IN_BITMAP (vi
->solution
, 0, j
, bi
)
7505 varinfo_t oi
= get_varinfo (j
);
7506 if (oi
->is_restrict_var
)
7510 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7512 fprintf (dump_file
, "found restrict pointed-to "
7514 print_generic_expr (dump_file
, ptr
);
7515 fprintf (dump_file
, " but not exclusively\n");
7517 restrict_var
= NULL
;
7522 /* NULL is the only other valid points-to entry. */
7523 else if (oi
->id
!= nothing_id
)
7525 restrict_var
= NULL
;
7529 /* Ok, found that ptr must(!) point to a single(!) restrict
7531 /* ??? PTA isn't really a proper propagation engine to compute
7533 ??? We could handle merging of two restricts by unifying them. */
7536 /* Now look at possible dereferences of ptr. */
7537 imm_use_iterator ui
;
7540 FOR_EACH_IMM_USE_STMT (use_stmt
, ui
, ptr
)
7542 /* ??? Calls and asms. */
7543 if (!gimple_assign_single_p (use_stmt
))
7545 used
|= maybe_set_dependence_info (gimple_assign_lhs (use_stmt
),
7546 ptr
, clique
, restrict_var
,
7548 used
|= maybe_set_dependence_info (gimple_assign_rhs1 (use_stmt
),
7549 ptr
, clique
, restrict_var
,
7553 bitmap_set_bit (rvars
, restrict_var
->id
);
7559 /* Assign the BASE id zero to all accesses not based on a restrict
7560 pointer. That way they get disambiguated against restrict
7561 accesses but not against each other. */
7562 /* ??? For restricts derived from globals (thus not incoming
7563 parameters) we can't restrict scoping properly thus the following
7564 is too aggressive there. For now we have excluded those globals from
7565 getting into the MR_DEPENDENCE machinery. */
7566 vls_data data
= { clique
, rvars
};
7568 FOR_EACH_BB_FN (bb
, cfun
)
7569 for (gimple_stmt_iterator gsi
= gsi_start_bb (bb
);
7570 !gsi_end_p (gsi
); gsi_next (&gsi
))
7572 gimple
*stmt
= gsi_stmt (gsi
);
7573 walk_stmt_load_store_ops (stmt
, &data
,
7574 visit_loadstore
, visit_loadstore
);
7578 BITMAP_FREE (rvars
);
7581 /* Compute points-to information for every SSA_NAME pointer in the
7582 current function and compute the transitive closure of escaped
7583 variables to re-initialize the call-clobber states of local variables. */
7586 compute_may_aliases (void)
7588 if (cfun
->gimple_df
->ipa_pta
)
7592 fprintf (dump_file
, "\nNot re-computing points-to information "
7593 "because IPA points-to information is available.\n\n");
7595 /* But still dump what we have remaining it. */
7596 dump_alias_info (dump_file
);
7602 /* For each pointer P_i, determine the sets of variables that P_i may
7603 point-to. Compute the reachability set of escaped and call-used
7605 compute_points_to_sets ();
7607 /* Debugging dumps. */
7609 dump_alias_info (dump_file
);
7611 /* Compute restrict-based memory disambiguations. */
7612 compute_dependence_clique ();
7614 /* Deallocate memory used by aliasing data structures and the internal
7615 points-to solution. */
7616 delete_points_to_sets ();
7618 gcc_assert (!need_ssa_update_p (cfun
));
7623 /* A dummy pass to cause points-to information to be computed via
7624 TODO_rebuild_alias. */
7628 const pass_data pass_data_build_alias
=
7630 GIMPLE_PASS
, /* type */
7632 OPTGROUP_NONE
, /* optinfo_flags */
7633 TV_NONE
, /* tv_id */
7634 ( PROP_cfg
| PROP_ssa
), /* properties_required */
7635 0, /* properties_provided */
7636 0, /* properties_destroyed */
7637 0, /* todo_flags_start */
7638 TODO_rebuild_alias
, /* todo_flags_finish */
7641 class pass_build_alias
: public gimple_opt_pass
7644 pass_build_alias (gcc::context
*ctxt
)
7645 : gimple_opt_pass (pass_data_build_alias
, ctxt
)
7648 /* opt_pass methods: */
7649 virtual bool gate (function
*) { return flag_tree_pta
; }
7651 }; // class pass_build_alias
7656 make_pass_build_alias (gcc::context
*ctxt
)
7658 return new pass_build_alias (ctxt
);
7661 /* A dummy pass to cause points-to information to be computed via
7662 TODO_rebuild_alias. */
7666 const pass_data pass_data_build_ealias
=
7668 GIMPLE_PASS
, /* type */
7669 "ealias", /* name */
7670 OPTGROUP_NONE
, /* optinfo_flags */
7671 TV_NONE
, /* tv_id */
7672 ( PROP_cfg
| PROP_ssa
), /* properties_required */
7673 0, /* properties_provided */
7674 0, /* properties_destroyed */
7675 0, /* todo_flags_start */
7676 TODO_rebuild_alias
, /* todo_flags_finish */
7679 class pass_build_ealias
: public gimple_opt_pass
7682 pass_build_ealias (gcc::context
*ctxt
)
7683 : gimple_opt_pass (pass_data_build_ealias
, ctxt
)
7686 /* opt_pass methods: */
7687 virtual bool gate (function
*) { return flag_tree_pta
; }
7689 }; // class pass_build_ealias
7694 make_pass_build_ealias (gcc::context
*ctxt
)
7696 return new pass_build_ealias (ctxt
);
7700 /* IPA PTA solutions for ESCAPED. */
7701 struct pt_solution ipa_escaped_pt
7702 = { true, false, false, false, false,
7703 false, false, false, false, false, NULL
};
7705 /* Associate node with varinfo DATA. Worker for
7706 cgraph_for_symbol_thunks_and_aliases. */
7708 associate_varinfo_to_alias (struct cgraph_node
*node
, void *data
)
7711 || (node
->thunk
.thunk_p
7712 && ! node
->global
.inlined_to
))
7714 insert_vi_for_tree (node
->decl
, (varinfo_t
)data
);
7718 /* Dump varinfo VI to FILE. */
7721 dump_varinfo (FILE *file
, varinfo_t vi
)
7726 fprintf (file
, "%u: %s\n", vi
->id
, vi
->name
);
7728 const char *sep
= " ";
7729 if (vi
->is_artificial_var
)
7730 fprintf (file
, "%sartificial", sep
);
7731 if (vi
->is_special_var
)
7732 fprintf (file
, "%sspecial", sep
);
7733 if (vi
->is_unknown_size_var
)
7734 fprintf (file
, "%sunknown-size", sep
);
7735 if (vi
->is_full_var
)
7736 fprintf (file
, "%sfull", sep
);
7737 if (vi
->is_heap_var
)
7738 fprintf (file
, "%sheap", sep
);
7739 if (vi
->may_have_pointers
)
7740 fprintf (file
, "%smay-have-pointers", sep
);
7741 if (vi
->only_restrict_pointers
)
7742 fprintf (file
, "%sonly-restrict-pointers", sep
);
7743 if (vi
->is_restrict_var
)
7744 fprintf (file
, "%sis-restrict-var", sep
);
7745 if (vi
->is_global_var
)
7746 fprintf (file
, "%sglobal", sep
);
7747 if (vi
->is_ipa_escape_point
)
7748 fprintf (file
, "%sipa-escape-point", sep
);
7750 fprintf (file
, "%sfn-info", sep
);
7752 fprintf (file
, "%srestrict-uid:%u", sep
, vi
->ruid
);
7754 fprintf (file
, "%snext:%u", sep
, vi
->next
);
7755 if (vi
->head
!= vi
->id
)
7756 fprintf (file
, "%shead:%u", sep
, vi
->head
);
7758 fprintf (file
, "%soffset:" HOST_WIDE_INT_PRINT_DEC
, sep
, vi
->offset
);
7759 if (vi
->size
!= ~(unsigned HOST_WIDE_INT
)0)
7760 fprintf (file
, "%ssize:" HOST_WIDE_INT_PRINT_DEC
, sep
, vi
->size
);
7761 if (vi
->fullsize
!= ~(unsigned HOST_WIDE_INT
)0
7762 && vi
->fullsize
!= vi
->size
)
7763 fprintf (file
, "%sfullsize:" HOST_WIDE_INT_PRINT_DEC
, sep
,
7765 fprintf (file
, "\n");
7767 if (vi
->solution
&& !bitmap_empty_p (vi
->solution
))
7771 fprintf (file
, " solution: {");
7772 EXECUTE_IF_SET_IN_BITMAP (vi
->solution
, 0, i
, bi
)
7773 fprintf (file
, " %u", i
);
7774 fprintf (file
, " }\n");
7777 if (vi
->oldsolution
&& !bitmap_empty_p (vi
->oldsolution
)
7778 && !bitmap_equal_p (vi
->solution
, vi
->oldsolution
))
7782 fprintf (file
, " oldsolution: {");
7783 EXECUTE_IF_SET_IN_BITMAP (vi
->oldsolution
, 0, i
, bi
)
7784 fprintf (file
, " %u", i
);
7785 fprintf (file
, " }\n");
7789 /* Dump varinfo VI to stderr. */
7792 debug_varinfo (varinfo_t vi
)
7794 dump_varinfo (stderr
, vi
);
7797 /* Dump varmap to FILE. */
7800 dump_varmap (FILE *file
)
7802 if (varmap
.length () == 0)
7805 fprintf (file
, "variables:\n");
7807 for (unsigned int i
= 0; i
< varmap
.length (); ++i
)
7809 varinfo_t vi
= get_varinfo (i
);
7810 dump_varinfo (file
, vi
);
7813 fprintf (file
, "\n");
7816 /* Dump varmap to stderr. */
7821 dump_varmap (stderr
);
7824 /* Compute whether node is refered to non-locally. Worker for
7825 cgraph_for_symbol_thunks_and_aliases. */
7827 refered_from_nonlocal_fn (struct cgraph_node
*node
, void *data
)
7829 bool *nonlocal_p
= (bool *)data
;
7830 *nonlocal_p
|= (node
->used_from_other_partition
7831 || node
->externally_visible
7832 || node
->force_output
7833 || lookup_attribute ("noipa", DECL_ATTRIBUTES (node
->decl
)));
7837 /* Same for varpool nodes. */
7839 refered_from_nonlocal_var (struct varpool_node
*node
, void *data
)
7841 bool *nonlocal_p
= (bool *)data
;
7842 *nonlocal_p
|= (node
->used_from_other_partition
7843 || node
->externally_visible
7844 || node
->force_output
);
7848 /* Execute the driver for IPA PTA. */
7850 ipa_pta_execute (void)
7852 struct cgraph_node
*node
;
7854 unsigned int from
= 0;
7860 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7862 symtab
->dump (dump_file
);
7863 fprintf (dump_file
, "\n");
7868 fprintf (dump_file
, "Generating generic constraints\n\n");
7869 dump_constraints (dump_file
, from
);
7870 fprintf (dump_file
, "\n");
7871 from
= constraints
.length ();
7874 /* Build the constraints. */
7875 FOR_EACH_DEFINED_FUNCTION (node
)
7878 /* Nodes without a body are not interesting. Especially do not
7879 visit clones at this point for now - we get duplicate decls
7880 there for inline clones at least. */
7881 if (!node
->has_gimple_body_p () || node
->global
.inlined_to
)
7885 gcc_assert (!node
->clone_of
);
7887 /* For externally visible or attribute used annotated functions use
7888 local constraints for their arguments.
7889 For local functions we see all callers and thus do not need initial
7890 constraints for parameters. */
7891 bool nonlocal_p
= (node
->used_from_other_partition
7892 || node
->externally_visible
7893 || node
->force_output
7894 || lookup_attribute ("noipa",
7895 DECL_ATTRIBUTES (node
->decl
)));
7896 node
->call_for_symbol_thunks_and_aliases (refered_from_nonlocal_fn
,
7899 vi
= create_function_info_for (node
->decl
,
7900 alias_get_name (node
->decl
), false,
7903 && from
!= constraints
.length ())
7906 "Generating intial constraints for %s", node
->name ());
7907 if (DECL_ASSEMBLER_NAME_SET_P (node
->decl
))
7908 fprintf (dump_file
, " (%s)",
7910 (DECL_ASSEMBLER_NAME (node
->decl
)));
7911 fprintf (dump_file
, "\n\n");
7912 dump_constraints (dump_file
, from
);
7913 fprintf (dump_file
, "\n");
7915 from
= constraints
.length ();
7918 node
->call_for_symbol_thunks_and_aliases
7919 (associate_varinfo_to_alias
, vi
, true);
7922 /* Create constraints for global variables and their initializers. */
7923 FOR_EACH_VARIABLE (var
)
7925 if (var
->alias
&& var
->analyzed
)
7928 varinfo_t vi
= get_vi_for_tree (var
->decl
);
7930 /* For the purpose of IPA PTA unit-local globals are not
7932 bool nonlocal_p
= (var
->used_from_other_partition
7933 || var
->externally_visible
7934 || var
->force_output
);
7935 var
->call_for_symbol_and_aliases (refered_from_nonlocal_var
,
7938 vi
->is_ipa_escape_point
= true;
7942 && from
!= constraints
.length ())
7945 "Generating constraints for global initializers\n\n");
7946 dump_constraints (dump_file
, from
);
7947 fprintf (dump_file
, "\n");
7948 from
= constraints
.length ();
7951 FOR_EACH_DEFINED_FUNCTION (node
)
7953 struct function
*func
;
7956 /* Nodes without a body are not interesting. */
7957 if (!node
->has_gimple_body_p () || node
->clone_of
)
7963 "Generating constraints for %s", node
->name ());
7964 if (DECL_ASSEMBLER_NAME_SET_P (node
->decl
))
7965 fprintf (dump_file
, " (%s)",
7967 (DECL_ASSEMBLER_NAME (node
->decl
)));
7968 fprintf (dump_file
, "\n");
7971 func
= DECL_STRUCT_FUNCTION (node
->decl
);
7972 gcc_assert (cfun
== NULL
);
7974 /* Build constriants for the function body. */
7975 FOR_EACH_BB_FN (bb
, func
)
7977 for (gphi_iterator gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
);
7980 gphi
*phi
= gsi
.phi ();
7982 if (! virtual_operand_p (gimple_phi_result (phi
)))
7983 find_func_aliases (func
, phi
);
7986 for (gimple_stmt_iterator gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
);
7989 gimple
*stmt
= gsi_stmt (gsi
);
7991 find_func_aliases (func
, stmt
);
7992 find_func_clobbers (func
, stmt
);
7998 fprintf (dump_file
, "\n");
7999 dump_constraints (dump_file
, from
);
8000 fprintf (dump_file
, "\n");
8001 from
= constraints
.length ();
8005 /* From the constraints compute the points-to sets. */
8006 solve_constraints ();
8008 /* Compute the global points-to sets for ESCAPED.
8009 ??? Note that the computed escape set is not correct
8010 for the whole unit as we fail to consider graph edges to
8011 externally visible functions. */
8012 ipa_escaped_pt
= find_what_var_points_to (NULL
, get_varinfo (escaped_id
));
8014 /* Make sure the ESCAPED solution (which is used as placeholder in
8015 other solutions) does not reference itself. This simplifies
8016 points-to solution queries. */
8017 ipa_escaped_pt
.ipa_escaped
= 0;
8019 /* Assign the points-to sets to the SSA names in the unit. */
8020 FOR_EACH_DEFINED_FUNCTION (node
)
8023 struct function
*fn
;
8027 /* Nodes without a body are not interesting. */
8028 if (!node
->has_gimple_body_p () || node
->clone_of
)
8031 fn
= DECL_STRUCT_FUNCTION (node
->decl
);
8033 /* Compute the points-to sets for pointer SSA_NAMEs. */
8034 FOR_EACH_VEC_ELT (*fn
->gimple_df
->ssa_names
, i
, ptr
)
8037 && POINTER_TYPE_P (TREE_TYPE (ptr
)))
8038 find_what_p_points_to (node
->decl
, ptr
);
8041 /* Compute the call-use and call-clobber sets for indirect calls
8042 and calls to external functions. */
8043 FOR_EACH_BB_FN (bb
, fn
)
8045 gimple_stmt_iterator gsi
;
8047 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
8050 struct pt_solution
*pt
;
8054 stmt
= dyn_cast
<gcall
*> (gsi_stmt (gsi
));
8058 /* Handle direct calls to functions with body. */
8059 decl
= gimple_call_fndecl (stmt
);
8062 tree called_decl
= NULL_TREE
;
8063 if (gimple_call_builtin_p (stmt
, BUILT_IN_GOMP_PARALLEL
))
8064 called_decl
= TREE_OPERAND (gimple_call_arg (stmt
, 0), 0);
8065 else if (gimple_call_builtin_p (stmt
, BUILT_IN_GOACC_PARALLEL
))
8066 called_decl
= TREE_OPERAND (gimple_call_arg (stmt
, 1), 0);
8068 if (called_decl
!= NULL_TREE
8069 && !fndecl_maybe_in_other_partition (called_decl
))
8074 && (fi
= lookup_vi_for_tree (decl
))
8077 *gimple_call_clobber_set (stmt
)
8078 = find_what_var_points_to
8079 (node
->decl
, first_vi_for_offset (fi
, fi_clobbers
));
8080 *gimple_call_use_set (stmt
)
8081 = find_what_var_points_to
8082 (node
->decl
, first_vi_for_offset (fi
, fi_uses
));
8084 /* Handle direct calls to external functions. */
8087 pt
= gimple_call_use_set (stmt
);
8088 if (gimple_call_flags (stmt
) & ECF_CONST
)
8089 memset (pt
, 0, sizeof (struct pt_solution
));
8090 else if ((vi
= lookup_call_use_vi (stmt
)) != NULL
)
8092 *pt
= find_what_var_points_to (node
->decl
, vi
);
8093 /* Escaped (and thus nonlocal) variables are always
8094 implicitly used by calls. */
8095 /* ??? ESCAPED can be empty even though NONLOCAL
8098 pt
->ipa_escaped
= 1;
8102 /* If there is nothing special about this call then
8103 we have made everything that is used also escape. */
8104 *pt
= ipa_escaped_pt
;
8108 pt
= gimple_call_clobber_set (stmt
);
8109 if (gimple_call_flags (stmt
) & (ECF_CONST
|ECF_PURE
|ECF_NOVOPS
))
8110 memset (pt
, 0, sizeof (struct pt_solution
));
8111 else if ((vi
= lookup_call_clobber_vi (stmt
)) != NULL
)
8113 *pt
= find_what_var_points_to (node
->decl
, vi
);
8114 /* Escaped (and thus nonlocal) variables are always
8115 implicitly clobbered by calls. */
8116 /* ??? ESCAPED can be empty even though NONLOCAL
8119 pt
->ipa_escaped
= 1;
8123 /* If there is nothing special about this call then
8124 we have made everything that is used also escape. */
8125 *pt
= ipa_escaped_pt
;
8129 /* Handle indirect calls. */
8131 && (fi
= get_fi_for_callee (stmt
)))
8133 /* We need to accumulate all clobbers/uses of all possible
8135 fi
= get_varinfo (find (fi
->id
));
8136 /* If we cannot constrain the set of functions we'll end up
8137 calling we end up using/clobbering everything. */
8138 if (bitmap_bit_p (fi
->solution
, anything_id
)
8139 || bitmap_bit_p (fi
->solution
, nonlocal_id
)
8140 || bitmap_bit_p (fi
->solution
, escaped_id
))
8142 pt_solution_reset (gimple_call_clobber_set (stmt
));
8143 pt_solution_reset (gimple_call_use_set (stmt
));
8149 struct pt_solution
*uses
, *clobbers
;
8151 uses
= gimple_call_use_set (stmt
);
8152 clobbers
= gimple_call_clobber_set (stmt
);
8153 memset (uses
, 0, sizeof (struct pt_solution
));
8154 memset (clobbers
, 0, sizeof (struct pt_solution
));
8155 EXECUTE_IF_SET_IN_BITMAP (fi
->solution
, 0, i
, bi
)
8157 struct pt_solution sol
;
8159 vi
= get_varinfo (i
);
8160 if (!vi
->is_fn_info
)
8162 /* ??? We could be more precise here? */
8164 uses
->ipa_escaped
= 1;
8165 clobbers
->nonlocal
= 1;
8166 clobbers
->ipa_escaped
= 1;
8170 if (!uses
->anything
)
8172 sol
= find_what_var_points_to
8174 first_vi_for_offset (vi
, fi_uses
));
8175 pt_solution_ior_into (uses
, &sol
);
8177 if (!clobbers
->anything
)
8179 sol
= find_what_var_points_to
8181 first_vi_for_offset (vi
, fi_clobbers
));
8182 pt_solution_ior_into (clobbers
, &sol
);
8190 fn
->gimple_df
->ipa_pta
= true;
8192 /* We have to re-set the final-solution cache after each function
8193 because what is a "global" is dependent on function context. */
8194 final_solutions
->empty ();
8195 obstack_free (&final_solutions_obstack
, NULL
);
8196 gcc_obstack_init (&final_solutions_obstack
);
8199 delete_points_to_sets ();
8208 const pass_data pass_data_ipa_pta
=
8210 SIMPLE_IPA_PASS
, /* type */
8212 OPTGROUP_NONE
, /* optinfo_flags */
8213 TV_IPA_PTA
, /* tv_id */
8214 0, /* properties_required */
8215 0, /* properties_provided */
8216 0, /* properties_destroyed */
8217 0, /* todo_flags_start */
8218 0, /* todo_flags_finish */
8221 class pass_ipa_pta
: public simple_ipa_opt_pass
8224 pass_ipa_pta (gcc::context
*ctxt
)
8225 : simple_ipa_opt_pass (pass_data_ipa_pta
, ctxt
)
8228 /* opt_pass methods: */
8229 virtual bool gate (function
*)
8233 /* Don't bother doing anything if the program has errors. */
8237 opt_pass
* clone () { return new pass_ipa_pta (m_ctxt
); }
8239 virtual unsigned int execute (function
*) { return ipa_pta_execute (); }
8241 }; // class pass_ipa_pta
8245 simple_ipa_opt_pass
*
8246 make_pass_ipa_pta (gcc::context
*ctxt
)
8248 return new pass_ipa_pta (ctxt
);