1 /* Tree based points-to analysis
2 Copyright (C) 2005-2014 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"
34 #include "hard-reg-set.h"
37 #include "dominance.h"
39 #include "basic-block.h"
41 #include "stor-layout.h"
43 #include "hash-table.h"
44 #include "tree-ssa-alias.h"
45 #include "internal-fn.h"
46 #include "gimple-expr.h"
49 #include "gimple-iterator.h"
50 #include "gimple-ssa.h"
52 #include "plugin-api.h"
55 #include "stringpool.h"
56 #include "tree-ssanames.h"
57 #include "tree-into-ssa.h"
60 #include "tree-inline.h"
61 #include "diagnostic-core.h"
62 #include "tree-pass.h"
63 #include "alloc-pool.h"
64 #include "splay-tree.h"
67 #include "tree-phinodes.h"
68 #include "ssa-iterators.h"
69 #include "tree-pretty-print.h"
70 #include "gimple-walk.h"
72 /* The idea behind this analyzer is to generate set constraints from the
73 program, then solve the resulting constraints in order to generate the
76 Set constraints are a way of modeling program analysis problems that
77 involve sets. They consist of an inclusion constraint language,
78 describing the variables (each variable is a set) and operations that
79 are involved on the variables, and a set of rules that derive facts
80 from these operations. To solve a system of set constraints, you derive
81 all possible facts under the rules, which gives you the correct sets
84 See "Efficient Field-sensitive pointer analysis for C" by "David
85 J. Pearce and Paul H. J. Kelly and Chris Hankin, at
86 http://citeseer.ist.psu.edu/pearce04efficient.html
88 Also see "Ultra-fast Aliasing Analysis using CLA: A Million Lines
89 of C Code in a Second" by ""Nevin Heintze and Olivier Tardieu" at
90 http://citeseer.ist.psu.edu/heintze01ultrafast.html
92 There are three types of real constraint expressions, DEREF,
93 ADDRESSOF, and SCALAR. Each constraint expression consists
94 of a constraint type, a variable, and an offset.
96 SCALAR is a constraint expression type used to represent x, whether
97 it appears on the LHS or the RHS of a statement.
98 DEREF is a constraint expression type used to represent *x, whether
99 it appears on the LHS or the RHS of a statement.
100 ADDRESSOF is a constraint expression used to represent &x, whether
101 it appears on the LHS or the RHS of a statement.
103 Each pointer variable in the program is assigned an integer id, and
104 each field of a structure variable is assigned an integer id as well.
106 Structure variables are linked to their list of fields through a "next
107 field" in each variable that points to the next field in offset
109 Each variable for a structure field has
111 1. "size", that tells the size in bits of that field.
112 2. "fullsize, that tells the size in bits of the entire structure.
113 3. "offset", that tells the offset in bits from the beginning of the
114 structure to this field.
126 foo.a -> id 1, size 32, offset 0, fullsize 64, next foo.b
127 foo.b -> id 2, size 32, offset 32, fullsize 64, next NULL
128 bar -> id 3, size 32, offset 0, fullsize 32, next NULL
131 In order to solve the system of set constraints, the following is
134 1. Each constraint variable x has a solution set associated with it,
137 2. Constraints are separated into direct, copy, and complex.
138 Direct constraints are ADDRESSOF constraints that require no extra
139 processing, such as P = &Q
140 Copy constraints are those of the form P = Q.
141 Complex constraints are all the constraints involving dereferences
142 and offsets (including offsetted copies).
144 3. All direct constraints of the form P = &Q are processed, such
145 that Q is added to Sol(P)
147 4. All complex constraints for a given constraint variable are stored in a
148 linked list attached to that variable's node.
150 5. A directed graph is built out of the copy constraints. Each
151 constraint variable is a node in the graph, and an edge from
152 Q to P is added for each copy constraint of the form P = Q
154 6. The graph is then walked, and solution sets are
155 propagated along the copy edges, such that an edge from Q to P
156 causes Sol(P) <- Sol(P) union Sol(Q).
158 7. As we visit each node, all complex constraints associated with
159 that node are processed by adding appropriate copy edges to the graph, or the
160 appropriate variables to the solution set.
162 8. The process of walking the graph is iterated until no solution
165 Prior to walking the graph in steps 6 and 7, We perform static
166 cycle elimination on the constraint graph, as well
167 as off-line variable substitution.
169 TODO: Adding offsets to pointer-to-structures can be handled (IE not punted
170 on and turned into anything), but isn't. You can just see what offset
171 inside the pointed-to struct it's going to access.
173 TODO: Constant bounded arrays can be handled as if they were structs of the
174 same number of elements.
176 TODO: Modeling heap and incoming pointers becomes much better if we
177 add fields to them as we discover them, which we could do.
179 TODO: We could handle unions, but to be honest, it's probably not
180 worth the pain or slowdown. */
182 /* IPA-PTA optimizations possible.
184 When the indirect function called is ANYTHING we can add disambiguation
185 based on the function signatures (or simply the parameter count which
186 is the varinfo size). We also do not need to consider functions that
187 do not have their address taken.
189 The is_global_var bit which marks escape points is overly conservative
190 in IPA mode. Split it to is_escape_point and is_global_var - only
191 externally visible globals are escape points in IPA mode. This is
192 also needed to fix the pt_solution_includes_global predicate
193 (and thus ptr_deref_may_alias_global_p).
195 The way we introduce DECL_PT_UID to avoid fixing up all points-to
196 sets in the translation unit when we copy a DECL during inlining
197 pessimizes precision. The advantage is that the DECL_PT_UID keeps
198 compile-time and memory usage overhead low - the points-to sets
199 do not grow or get unshared as they would during a fixup phase.
200 An alternative solution is to delay IPA PTA until after all
201 inlining transformations have been applied.
203 The way we propagate clobber/use information isn't optimized.
204 It should use a new complex constraint that properly filters
205 out local variables of the callee (though that would make
206 the sets invalid after inlining). OTOH we might as well
207 admit defeat to WHOPR and simply do all the clobber/use analysis
208 and propagation after PTA finished but before we threw away
209 points-to information for memory variables. WHOPR and PTA
210 do not play along well anyway - the whole constraint solving
211 would need to be done in WPA phase and it will be very interesting
212 to apply the results to local SSA names during LTRANS phase.
214 We probably should compute a per-function unit-ESCAPE solution
215 propagating it simply like the clobber / uses solutions. The
216 solution can go alongside the non-IPA espaced solution and be
217 used to query which vars escape the unit through a function.
219 We never put function decls in points-to sets so we do not
220 keep the set of called functions for indirect calls.
222 And probably more. */
224 static bool use_field_sensitive
= true;
225 static int in_ipa_mode
= 0;
227 /* Used for predecessor bitmaps. */
228 static bitmap_obstack predbitmap_obstack
;
230 /* Used for points-to sets. */
231 static bitmap_obstack pta_obstack
;
233 /* Used for oldsolution members of variables. */
234 static bitmap_obstack oldpta_obstack
;
236 /* Used for per-solver-iteration bitmaps. */
237 static bitmap_obstack iteration_obstack
;
239 static unsigned int create_variable_info_for (tree
, const char *);
240 typedef struct constraint_graph
*constraint_graph_t
;
241 static void unify_nodes (constraint_graph_t
, unsigned int, unsigned int, bool);
244 typedef struct constraint
*constraint_t
;
247 #define EXECUTE_IF_IN_NONNULL_BITMAP(a, b, c, d) \
249 EXECUTE_IF_SET_IN_BITMAP (a, b, c, d)
251 static struct constraint_stats
253 unsigned int total_vars
;
254 unsigned int nonpointer_vars
;
255 unsigned int unified_vars_static
;
256 unsigned int unified_vars_dynamic
;
257 unsigned int iterations
;
258 unsigned int num_edges
;
259 unsigned int num_implicit_edges
;
260 unsigned int points_to_sets_created
;
265 /* ID of this variable */
268 /* True if this is a variable created by the constraint analysis, such as
269 heap variables and constraints we had to break up. */
270 unsigned int is_artificial_var
: 1;
272 /* True if this is a special variable whose solution set should not be
274 unsigned int is_special_var
: 1;
276 /* True for variables whose size is not known or variable. */
277 unsigned int is_unknown_size_var
: 1;
279 /* True for (sub-)fields that represent a whole variable. */
280 unsigned int is_full_var
: 1;
282 /* True if this is a heap variable. */
283 unsigned int is_heap_var
: 1;
285 /* True if this field may contain pointers. */
286 unsigned int may_have_pointers
: 1;
288 /* True if this field has only restrict qualified pointers. */
289 unsigned int only_restrict_pointers
: 1;
291 /* True if this represents a heap var created for a restrict qualified
293 unsigned int is_restrict_var
: 1;
295 /* True if this represents a global variable. */
296 unsigned int is_global_var
: 1;
298 /* True if this represents a IPA function info. */
299 unsigned int is_fn_info
: 1;
301 /* ??? Store somewhere better. */
304 /* The ID of the variable for the next field in this structure
305 or zero for the last field in this structure. */
308 /* The ID of the variable for the first field in this structure. */
311 /* Offset of this variable, in bits, from the base variable */
312 unsigned HOST_WIDE_INT offset
;
314 /* Size of the variable, in bits. */
315 unsigned HOST_WIDE_INT size
;
317 /* Full size of the base variable, in bits. */
318 unsigned HOST_WIDE_INT fullsize
;
320 /* Name of this variable */
323 /* Tree that this variable is associated with. */
326 /* Points-to set for this variable. */
329 /* Old points-to set for this variable. */
332 typedef struct variable_info
*varinfo_t
;
334 static varinfo_t
first_vi_for_offset (varinfo_t
, unsigned HOST_WIDE_INT
);
335 static varinfo_t
first_or_preceding_vi_for_offset (varinfo_t
,
336 unsigned HOST_WIDE_INT
);
337 static varinfo_t
lookup_vi_for_tree (tree
);
338 static inline bool type_can_have_subvars (const_tree
);
340 /* Pool of variable info structures. */
341 static alloc_pool variable_info_pool
;
343 /* Map varinfo to final pt_solution. */
344 static hash_map
<varinfo_t
, pt_solution
*> *final_solutions
;
345 struct obstack final_solutions_obstack
;
347 /* Table of variable info structures for constraint variables.
348 Indexed directly by variable info id. */
349 static vec
<varinfo_t
> varmap
;
351 /* Return the varmap element N */
353 static inline varinfo_t
354 get_varinfo (unsigned int n
)
359 /* Return the next variable in the list of sub-variables of VI
360 or NULL if VI is the last sub-variable. */
362 static inline varinfo_t
363 vi_next (varinfo_t vi
)
365 return get_varinfo (vi
->next
);
368 /* Static IDs for the special variables. Variable ID zero is unused
369 and used as terminator for the sub-variable chain. */
370 enum { nothing_id
= 1, anything_id
= 2, string_id
= 3,
371 escaped_id
= 4, nonlocal_id
= 5,
372 storedanything_id
= 6, integer_id
= 7 };
374 /* Return a new variable info structure consisting for a variable
375 named NAME, and using constraint graph node NODE. Append it
376 to the vector of variable info structures. */
379 new_var_info (tree t
, const char *name
)
381 unsigned index
= varmap
.length ();
382 varinfo_t ret
= (varinfo_t
) pool_alloc (variable_info_pool
);
387 /* Vars without decl are artificial and do not have sub-variables. */
388 ret
->is_artificial_var
= (t
== NULL_TREE
);
389 ret
->is_special_var
= false;
390 ret
->is_unknown_size_var
= false;
391 ret
->is_full_var
= (t
== NULL_TREE
);
392 ret
->is_heap_var
= false;
393 ret
->may_have_pointers
= true;
394 ret
->only_restrict_pointers
= false;
395 ret
->is_restrict_var
= false;
396 ret
->is_global_var
= (t
== NULL_TREE
);
397 ret
->is_fn_info
= false;
399 ret
->is_global_var
= (is_global_var (t
)
400 /* We have to treat even local register variables
402 || (TREE_CODE (t
) == VAR_DECL
403 && DECL_HARD_REGISTER (t
)));
404 ret
->solution
= BITMAP_ALLOC (&pta_obstack
);
405 ret
->oldsolution
= NULL
;
411 varmap
.safe_push (ret
);
417 /* A map mapping call statements to per-stmt variables for uses
418 and clobbers specific to the call. */
419 static hash_map
<gimple
, varinfo_t
> *call_stmt_vars
;
421 /* Lookup or create the variable for the call statement CALL. */
424 get_call_vi (gcall
*call
)
429 varinfo_t
*slot_p
= &call_stmt_vars
->get_or_insert (call
, &existed
);
433 vi
= new_var_info (NULL_TREE
, "CALLUSED");
437 vi
->is_full_var
= true;
439 vi2
= new_var_info (NULL_TREE
, "CALLCLOBBERED");
443 vi2
->is_full_var
= true;
451 /* Lookup the variable for the call statement CALL representing
452 the uses. Returns NULL if there is nothing special about this call. */
455 lookup_call_use_vi (gcall
*call
)
457 varinfo_t
*slot_p
= call_stmt_vars
->get (call
);
464 /* Lookup the variable for the call statement CALL representing
465 the clobbers. Returns NULL if there is nothing special about this call. */
468 lookup_call_clobber_vi (gcall
*call
)
470 varinfo_t uses
= lookup_call_use_vi (call
);
474 return vi_next (uses
);
477 /* Lookup or create the variable for the call statement CALL representing
481 get_call_use_vi (gcall
*call
)
483 return get_call_vi (call
);
486 /* Lookup or create the variable for the call statement CALL representing
489 static varinfo_t ATTRIBUTE_UNUSED
490 get_call_clobber_vi (gcall
*call
)
492 return vi_next (get_call_vi (call
));
496 typedef enum {SCALAR
, DEREF
, ADDRESSOF
} constraint_expr_type
;
498 /* An expression that appears in a constraint. */
500 struct constraint_expr
502 /* Constraint type. */
503 constraint_expr_type type
;
505 /* Variable we are referring to in the constraint. */
508 /* Offset, in bits, of this constraint from the beginning of
509 variables it ends up referring to.
511 IOW, in a deref constraint, we would deref, get the result set,
512 then add OFFSET to each member. */
513 HOST_WIDE_INT offset
;
516 /* Use 0x8000... as special unknown offset. */
517 #define UNKNOWN_OFFSET HOST_WIDE_INT_MIN
519 typedef struct constraint_expr ce_s
;
520 static void get_constraint_for_1 (tree
, vec
<ce_s
> *, bool, bool);
521 static void get_constraint_for (tree
, vec
<ce_s
> *);
522 static void get_constraint_for_rhs (tree
, vec
<ce_s
> *);
523 static void do_deref (vec
<ce_s
> *);
525 /* Our set constraints are made up of two constraint expressions, one
528 As described in the introduction, our set constraints each represent an
529 operation between set valued variables.
533 struct constraint_expr lhs
;
534 struct constraint_expr rhs
;
537 /* List of constraints that we use to build the constraint graph from. */
539 static vec
<constraint_t
> constraints
;
540 static alloc_pool constraint_pool
;
542 /* The constraint graph is represented as an array of bitmaps
543 containing successor nodes. */
545 struct constraint_graph
547 /* Size of this graph, which may be different than the number of
548 nodes in the variable map. */
551 /* Explicit successors of each node. */
554 /* Implicit predecessors of each node (Used for variable
556 bitmap
*implicit_preds
;
558 /* Explicit predecessors of each node (Used for variable substitution). */
561 /* Indirect cycle representatives, or -1 if the node has no indirect
563 int *indirect_cycles
;
565 /* Representative node for a node. rep[a] == a unless the node has
569 /* Equivalence class representative for a label. This is used for
570 variable substitution. */
573 /* Pointer equivalence label for a node. All nodes with the same
574 pointer equivalence label can be unified together at some point
575 (either during constraint optimization or after the constraint
579 /* Pointer equivalence representative for a label. This is used to
580 handle nodes that are pointer equivalent but not location
581 equivalent. We can unite these once the addressof constraints
582 are transformed into initial points-to sets. */
585 /* Pointer equivalence label for each node, used during variable
587 unsigned int *pointer_label
;
589 /* Location equivalence label for each node, used during location
590 equivalence finding. */
591 unsigned int *loc_label
;
593 /* Pointed-by set for each node, used during location equivalence
594 finding. This is pointed-by rather than pointed-to, because it
595 is constructed using the predecessor graph. */
598 /* Points to sets for pointer equivalence. This is *not* the actual
599 points-to sets for nodes. */
602 /* Bitmap of nodes where the bit is set if the node is a direct
603 node. Used for variable substitution. */
604 sbitmap direct_nodes
;
606 /* Bitmap of nodes where the bit is set if the node is address
607 taken. Used for variable substitution. */
608 bitmap address_taken
;
610 /* Vector of complex constraints for each graph node. Complex
611 constraints are those involving dereferences or offsets that are
613 vec
<constraint_t
> *complex;
616 static constraint_graph_t graph
;
618 /* During variable substitution and the offline version of indirect
619 cycle finding, we create nodes to represent dereferences and
620 address taken constraints. These represent where these start and
622 #define FIRST_REF_NODE (varmap).length ()
623 #define LAST_REF_NODE (FIRST_REF_NODE + (FIRST_REF_NODE - 1))
625 /* Return the representative node for NODE, if NODE has been unioned
627 This function performs path compression along the way to finding
628 the representative. */
631 find (unsigned int node
)
633 gcc_checking_assert (node
< graph
->size
);
634 if (graph
->rep
[node
] != node
)
635 return graph
->rep
[node
] = find (graph
->rep
[node
]);
639 /* Union the TO and FROM nodes to the TO nodes.
640 Note that at some point in the future, we may want to do
641 union-by-rank, in which case we are going to have to return the
642 node we unified to. */
645 unite (unsigned int to
, unsigned int from
)
647 gcc_checking_assert (to
< graph
->size
&& from
< graph
->size
);
648 if (to
!= from
&& graph
->rep
[from
] != to
)
650 graph
->rep
[from
] = to
;
656 /* Create a new constraint consisting of LHS and RHS expressions. */
659 new_constraint (const struct constraint_expr lhs
,
660 const struct constraint_expr rhs
)
662 constraint_t ret
= (constraint_t
) pool_alloc (constraint_pool
);
668 /* Print out constraint C to FILE. */
671 dump_constraint (FILE *file
, constraint_t c
)
673 if (c
->lhs
.type
== ADDRESSOF
)
675 else if (c
->lhs
.type
== DEREF
)
677 fprintf (file
, "%s", get_varinfo (c
->lhs
.var
)->name
);
678 if (c
->lhs
.offset
== UNKNOWN_OFFSET
)
679 fprintf (file
, " + UNKNOWN");
680 else if (c
->lhs
.offset
!= 0)
681 fprintf (file
, " + " HOST_WIDE_INT_PRINT_DEC
, c
->lhs
.offset
);
682 fprintf (file
, " = ");
683 if (c
->rhs
.type
== ADDRESSOF
)
685 else if (c
->rhs
.type
== DEREF
)
687 fprintf (file
, "%s", get_varinfo (c
->rhs
.var
)->name
);
688 if (c
->rhs
.offset
== UNKNOWN_OFFSET
)
689 fprintf (file
, " + UNKNOWN");
690 else if (c
->rhs
.offset
!= 0)
691 fprintf (file
, " + " HOST_WIDE_INT_PRINT_DEC
, c
->rhs
.offset
);
695 void debug_constraint (constraint_t
);
696 void debug_constraints (void);
697 void debug_constraint_graph (void);
698 void debug_solution_for_var (unsigned int);
699 void debug_sa_points_to_info (void);
701 /* Print out constraint C to stderr. */
704 debug_constraint (constraint_t c
)
706 dump_constraint (stderr
, c
);
707 fprintf (stderr
, "\n");
710 /* Print out all constraints to FILE */
713 dump_constraints (FILE *file
, int from
)
717 for (i
= from
; constraints
.iterate (i
, &c
); i
++)
720 dump_constraint (file
, c
);
721 fprintf (file
, "\n");
725 /* Print out all constraints to stderr. */
728 debug_constraints (void)
730 dump_constraints (stderr
, 0);
733 /* Print the constraint graph in dot format. */
736 dump_constraint_graph (FILE *file
)
740 /* Only print the graph if it has already been initialized: */
744 /* Prints the header of the dot file: */
745 fprintf (file
, "strict digraph {\n");
746 fprintf (file
, " node [\n shape = box\n ]\n");
747 fprintf (file
, " edge [\n fontsize = \"12\"\n ]\n");
748 fprintf (file
, "\n // List of nodes and complex constraints in "
749 "the constraint graph:\n");
751 /* The next lines print the nodes in the graph together with the
752 complex constraints attached to them. */
753 for (i
= 1; i
< graph
->size
; i
++)
755 if (i
== FIRST_REF_NODE
)
759 if (i
< FIRST_REF_NODE
)
760 fprintf (file
, "\"%s\"", get_varinfo (i
)->name
);
762 fprintf (file
, "\"*%s\"", get_varinfo (i
- FIRST_REF_NODE
)->name
);
763 if (graph
->complex[i
].exists ())
767 fprintf (file
, " [label=\"\\N\\n");
768 for (j
= 0; graph
->complex[i
].iterate (j
, &c
); ++j
)
770 dump_constraint (file
, c
);
771 fprintf (file
, "\\l");
773 fprintf (file
, "\"]");
775 fprintf (file
, ";\n");
778 /* Go over the edges. */
779 fprintf (file
, "\n // Edges in the constraint graph:\n");
780 for (i
= 1; i
< graph
->size
; i
++)
786 EXECUTE_IF_IN_NONNULL_BITMAP (graph
->succs
[i
], 0, j
, bi
)
788 unsigned to
= find (j
);
791 if (i
< FIRST_REF_NODE
)
792 fprintf (file
, "\"%s\"", get_varinfo (i
)->name
);
794 fprintf (file
, "\"*%s\"", get_varinfo (i
- FIRST_REF_NODE
)->name
);
795 fprintf (file
, " -> ");
796 if (to
< FIRST_REF_NODE
)
797 fprintf (file
, "\"%s\"", get_varinfo (to
)->name
);
799 fprintf (file
, "\"*%s\"", get_varinfo (to
- FIRST_REF_NODE
)->name
);
800 fprintf (file
, ";\n");
804 /* Prints the tail of the dot file. */
805 fprintf (file
, "}\n");
808 /* Print out the constraint graph to stderr. */
811 debug_constraint_graph (void)
813 dump_constraint_graph (stderr
);
818 The solver is a simple worklist solver, that works on the following
821 sbitmap changed_nodes = all zeroes;
823 For each node that is not already collapsed:
825 set bit in changed nodes
827 while (changed_count > 0)
829 compute topological ordering for constraint graph
831 find and collapse cycles in the constraint graph (updating
832 changed if necessary)
834 for each node (n) in the graph in topological order:
837 Process each complex constraint associated with the node,
838 updating changed if necessary.
840 For each outgoing edge from n, propagate the solution from n to
841 the destination of the edge, updating changed as necessary.
845 /* Return true if two constraint expressions A and B are equal. */
848 constraint_expr_equal (struct constraint_expr a
, struct constraint_expr b
)
850 return a
.type
== b
.type
&& a
.var
== b
.var
&& a
.offset
== b
.offset
;
853 /* Return true if constraint expression A is less than constraint expression
854 B. This is just arbitrary, but consistent, in order to give them an
858 constraint_expr_less (struct constraint_expr a
, struct constraint_expr b
)
860 if (a
.type
== b
.type
)
863 return a
.offset
< b
.offset
;
865 return a
.var
< b
.var
;
868 return a
.type
< b
.type
;
871 /* Return true if constraint A is less than constraint B. This is just
872 arbitrary, but consistent, in order to give them an ordering. */
875 constraint_less (const constraint_t
&a
, const constraint_t
&b
)
877 if (constraint_expr_less (a
->lhs
, b
->lhs
))
879 else if (constraint_expr_less (b
->lhs
, a
->lhs
))
882 return constraint_expr_less (a
->rhs
, b
->rhs
);
885 /* Return true if two constraints A and B are equal. */
888 constraint_equal (struct constraint a
, struct constraint b
)
890 return constraint_expr_equal (a
.lhs
, b
.lhs
)
891 && constraint_expr_equal (a
.rhs
, b
.rhs
);
895 /* Find a constraint LOOKFOR in the sorted constraint vector VEC */
898 constraint_vec_find (vec
<constraint_t
> vec
,
899 struct constraint lookfor
)
907 place
= vec
.lower_bound (&lookfor
, constraint_less
);
908 if (place
>= vec
.length ())
911 if (!constraint_equal (*found
, lookfor
))
916 /* Union two constraint vectors, TO and FROM. Put the result in TO.
917 Returns true of TO set is changed. */
920 constraint_set_union (vec
<constraint_t
> *to
,
921 vec
<constraint_t
> *from
)
925 bool any_change
= false;
927 FOR_EACH_VEC_ELT (*from
, i
, c
)
929 if (constraint_vec_find (*to
, *c
) == NULL
)
931 unsigned int place
= to
->lower_bound (c
, constraint_less
);
932 to
->safe_insert (place
, c
);
939 /* Expands the solution in SET to all sub-fields of variables included. */
942 solution_set_expand (bitmap set
, bitmap
*expanded
)
950 *expanded
= BITMAP_ALLOC (&iteration_obstack
);
952 /* In a first pass expand to the head of the variables we need to
953 add all sub-fields off. This avoids quadratic behavior. */
954 EXECUTE_IF_SET_IN_BITMAP (set
, 0, j
, bi
)
956 varinfo_t v
= get_varinfo (j
);
957 if (v
->is_artificial_var
960 bitmap_set_bit (*expanded
, v
->head
);
963 /* In the second pass now expand all head variables with subfields. */
964 EXECUTE_IF_SET_IN_BITMAP (*expanded
, 0, j
, bi
)
966 varinfo_t v
= get_varinfo (j
);
969 for (v
= vi_next (v
); v
!= NULL
; v
= vi_next (v
))
970 bitmap_set_bit (*expanded
, v
->id
);
973 /* And finally set the rest of the bits from SET. */
974 bitmap_ior_into (*expanded
, set
);
979 /* Union solution sets TO and DELTA, and add INC to each member of DELTA in the
983 set_union_with_increment (bitmap to
, bitmap delta
, HOST_WIDE_INT inc
,
984 bitmap
*expanded_delta
)
986 bool changed
= false;
990 /* If the solution of DELTA contains anything it is good enough to transfer
992 if (bitmap_bit_p (delta
, anything_id
))
993 return bitmap_set_bit (to
, anything_id
);
995 /* If the offset is unknown we have to expand the solution to
997 if (inc
== UNKNOWN_OFFSET
)
999 delta
= solution_set_expand (delta
, expanded_delta
);
1000 changed
|= bitmap_ior_into (to
, delta
);
1004 /* For non-zero offset union the offsetted solution into the destination. */
1005 EXECUTE_IF_SET_IN_BITMAP (delta
, 0, i
, bi
)
1007 varinfo_t vi
= get_varinfo (i
);
1009 /* If this is a variable with just one field just set its bit
1011 if (vi
->is_artificial_var
1012 || vi
->is_unknown_size_var
1014 changed
|= bitmap_set_bit (to
, i
);
1017 HOST_WIDE_INT fieldoffset
= vi
->offset
+ inc
;
1018 unsigned HOST_WIDE_INT size
= vi
->size
;
1020 /* If the offset makes the pointer point to before the
1021 variable use offset zero for the field lookup. */
1022 if (fieldoffset
< 0)
1023 vi
= get_varinfo (vi
->head
);
1025 vi
= first_or_preceding_vi_for_offset (vi
, fieldoffset
);
1029 changed
|= bitmap_set_bit (to
, vi
->id
);
1034 /* We have to include all fields that overlap the current field
1038 while (vi
->offset
< fieldoffset
+ size
);
1045 /* Insert constraint C into the list of complex constraints for graph
1049 insert_into_complex (constraint_graph_t graph
,
1050 unsigned int var
, constraint_t c
)
1052 vec
<constraint_t
> complex = graph
->complex[var
];
1053 unsigned int place
= complex.lower_bound (c
, constraint_less
);
1055 /* Only insert constraints that do not already exist. */
1056 if (place
>= complex.length ()
1057 || !constraint_equal (*c
, *complex[place
]))
1058 graph
->complex[var
].safe_insert (place
, c
);
1062 /* Condense two variable nodes into a single variable node, by moving
1063 all associated info from FROM to TO. Returns true if TO node's
1064 constraint set changes after the merge. */
1067 merge_node_constraints (constraint_graph_t graph
, unsigned int to
,
1072 bool any_change
= false;
1074 gcc_checking_assert (find (from
) == to
);
1076 /* Move all complex constraints from src node into to node */
1077 FOR_EACH_VEC_ELT (graph
->complex[from
], i
, c
)
1079 /* In complex constraints for node FROM, we may have either
1080 a = *FROM, and *FROM = a, or an offseted constraint which are
1081 always added to the rhs node's constraints. */
1083 if (c
->rhs
.type
== DEREF
)
1085 else if (c
->lhs
.type
== DEREF
)
1091 any_change
= constraint_set_union (&graph
->complex[to
],
1092 &graph
->complex[from
]);
1093 graph
->complex[from
].release ();
1098 /* Remove edges involving NODE from GRAPH. */
1101 clear_edges_for_node (constraint_graph_t graph
, unsigned int node
)
1103 if (graph
->succs
[node
])
1104 BITMAP_FREE (graph
->succs
[node
]);
1107 /* Merge GRAPH nodes FROM and TO into node TO. */
1110 merge_graph_nodes (constraint_graph_t graph
, unsigned int to
,
1113 if (graph
->indirect_cycles
[from
] != -1)
1115 /* If we have indirect cycles with the from node, and we have
1116 none on the to node, the to node has indirect cycles from the
1117 from node now that they are unified.
1118 If indirect cycles exist on both, unify the nodes that they
1119 are in a cycle with, since we know they are in a cycle with
1121 if (graph
->indirect_cycles
[to
] == -1)
1122 graph
->indirect_cycles
[to
] = graph
->indirect_cycles
[from
];
1125 /* Merge all the successor edges. */
1126 if (graph
->succs
[from
])
1128 if (!graph
->succs
[to
])
1129 graph
->succs
[to
] = BITMAP_ALLOC (&pta_obstack
);
1130 bitmap_ior_into (graph
->succs
[to
],
1131 graph
->succs
[from
]);
1134 clear_edges_for_node (graph
, from
);
1138 /* Add an indirect graph edge to GRAPH, going from TO to FROM if
1139 it doesn't exist in the graph already. */
1142 add_implicit_graph_edge (constraint_graph_t graph
, unsigned int to
,
1148 if (!graph
->implicit_preds
[to
])
1149 graph
->implicit_preds
[to
] = BITMAP_ALLOC (&predbitmap_obstack
);
1151 if (bitmap_set_bit (graph
->implicit_preds
[to
], from
))
1152 stats
.num_implicit_edges
++;
1155 /* Add a predecessor graph edge to GRAPH, going from TO to FROM if
1156 it doesn't exist in the graph already.
1157 Return false if the edge already existed, true otherwise. */
1160 add_pred_graph_edge (constraint_graph_t graph
, unsigned int to
,
1163 if (!graph
->preds
[to
])
1164 graph
->preds
[to
] = BITMAP_ALLOC (&predbitmap_obstack
);
1165 bitmap_set_bit (graph
->preds
[to
], from
);
1168 /* Add a graph edge to GRAPH, going from FROM to TO if
1169 it doesn't exist in the graph already.
1170 Return false if the edge already existed, true otherwise. */
1173 add_graph_edge (constraint_graph_t graph
, unsigned int to
,
1184 if (!graph
->succs
[from
])
1185 graph
->succs
[from
] = BITMAP_ALLOC (&pta_obstack
);
1186 if (bitmap_set_bit (graph
->succs
[from
], to
))
1189 if (to
< FIRST_REF_NODE
&& from
< FIRST_REF_NODE
)
1197 /* Initialize the constraint graph structure to contain SIZE nodes. */
1200 init_graph (unsigned int size
)
1204 graph
= XCNEW (struct constraint_graph
);
1206 graph
->succs
= XCNEWVEC (bitmap
, graph
->size
);
1207 graph
->indirect_cycles
= XNEWVEC (int, graph
->size
);
1208 graph
->rep
= XNEWVEC (unsigned int, graph
->size
);
1209 /* ??? Macros do not support template types with multiple arguments,
1210 so we use a typedef to work around it. */
1211 typedef vec
<constraint_t
> vec_constraint_t_heap
;
1212 graph
->complex = XCNEWVEC (vec_constraint_t_heap
, size
);
1213 graph
->pe
= XCNEWVEC (unsigned int, graph
->size
);
1214 graph
->pe_rep
= XNEWVEC (int, graph
->size
);
1216 for (j
= 0; j
< graph
->size
; j
++)
1219 graph
->pe_rep
[j
] = -1;
1220 graph
->indirect_cycles
[j
] = -1;
1224 /* Build the constraint graph, adding only predecessor edges right now. */
1227 build_pred_graph (void)
1233 graph
->implicit_preds
= XCNEWVEC (bitmap
, graph
->size
);
1234 graph
->preds
= XCNEWVEC (bitmap
, graph
->size
);
1235 graph
->pointer_label
= XCNEWVEC (unsigned int, graph
->size
);
1236 graph
->loc_label
= XCNEWVEC (unsigned int, graph
->size
);
1237 graph
->pointed_by
= XCNEWVEC (bitmap
, graph
->size
);
1238 graph
->points_to
= XCNEWVEC (bitmap
, graph
->size
);
1239 graph
->eq_rep
= XNEWVEC (int, graph
->size
);
1240 graph
->direct_nodes
= sbitmap_alloc (graph
->size
);
1241 graph
->address_taken
= BITMAP_ALLOC (&predbitmap_obstack
);
1242 bitmap_clear (graph
->direct_nodes
);
1244 for (j
= 1; j
< FIRST_REF_NODE
; j
++)
1246 if (!get_varinfo (j
)->is_special_var
)
1247 bitmap_set_bit (graph
->direct_nodes
, j
);
1250 for (j
= 0; j
< graph
->size
; j
++)
1251 graph
->eq_rep
[j
] = -1;
1253 for (j
= 0; j
< varmap
.length (); j
++)
1254 graph
->indirect_cycles
[j
] = -1;
1256 FOR_EACH_VEC_ELT (constraints
, i
, c
)
1258 struct constraint_expr lhs
= c
->lhs
;
1259 struct constraint_expr rhs
= c
->rhs
;
1260 unsigned int lhsvar
= lhs
.var
;
1261 unsigned int rhsvar
= rhs
.var
;
1263 if (lhs
.type
== DEREF
)
1266 if (rhs
.offset
== 0 && lhs
.offset
== 0 && rhs
.type
== SCALAR
)
1267 add_pred_graph_edge (graph
, FIRST_REF_NODE
+ lhsvar
, rhsvar
);
1269 else if (rhs
.type
== DEREF
)
1272 if (rhs
.offset
== 0 && lhs
.offset
== 0 && lhs
.type
== SCALAR
)
1273 add_pred_graph_edge (graph
, lhsvar
, FIRST_REF_NODE
+ rhsvar
);
1275 bitmap_clear_bit (graph
->direct_nodes
, lhsvar
);
1277 else if (rhs
.type
== ADDRESSOF
)
1282 if (graph
->points_to
[lhsvar
] == NULL
)
1283 graph
->points_to
[lhsvar
] = BITMAP_ALLOC (&predbitmap_obstack
);
1284 bitmap_set_bit (graph
->points_to
[lhsvar
], rhsvar
);
1286 if (graph
->pointed_by
[rhsvar
] == NULL
)
1287 graph
->pointed_by
[rhsvar
] = BITMAP_ALLOC (&predbitmap_obstack
);
1288 bitmap_set_bit (graph
->pointed_by
[rhsvar
], lhsvar
);
1290 /* Implicitly, *x = y */
1291 add_implicit_graph_edge (graph
, FIRST_REF_NODE
+ lhsvar
, rhsvar
);
1293 /* All related variables are no longer direct nodes. */
1294 bitmap_clear_bit (graph
->direct_nodes
, rhsvar
);
1295 v
= get_varinfo (rhsvar
);
1296 if (!v
->is_full_var
)
1298 v
= get_varinfo (v
->head
);
1301 bitmap_clear_bit (graph
->direct_nodes
, v
->id
);
1306 bitmap_set_bit (graph
->address_taken
, rhsvar
);
1308 else if (lhsvar
> anything_id
1309 && lhsvar
!= rhsvar
&& lhs
.offset
== 0 && rhs
.offset
== 0)
1312 add_pred_graph_edge (graph
, lhsvar
, rhsvar
);
1313 /* Implicitly, *x = *y */
1314 add_implicit_graph_edge (graph
, FIRST_REF_NODE
+ lhsvar
,
1315 FIRST_REF_NODE
+ rhsvar
);
1317 else if (lhs
.offset
!= 0 || rhs
.offset
!= 0)
1319 if (rhs
.offset
!= 0)
1320 bitmap_clear_bit (graph
->direct_nodes
, lhs
.var
);
1321 else if (lhs
.offset
!= 0)
1322 bitmap_clear_bit (graph
->direct_nodes
, rhs
.var
);
1327 /* Build the constraint graph, adding successor edges. */
1330 build_succ_graph (void)
1335 FOR_EACH_VEC_ELT (constraints
, i
, c
)
1337 struct constraint_expr lhs
;
1338 struct constraint_expr rhs
;
1339 unsigned int lhsvar
;
1340 unsigned int rhsvar
;
1347 lhsvar
= find (lhs
.var
);
1348 rhsvar
= find (rhs
.var
);
1350 if (lhs
.type
== DEREF
)
1352 if (rhs
.offset
== 0 && lhs
.offset
== 0 && rhs
.type
== SCALAR
)
1353 add_graph_edge (graph
, FIRST_REF_NODE
+ lhsvar
, rhsvar
);
1355 else if (rhs
.type
== DEREF
)
1357 if (rhs
.offset
== 0 && lhs
.offset
== 0 && lhs
.type
== SCALAR
)
1358 add_graph_edge (graph
, lhsvar
, FIRST_REF_NODE
+ rhsvar
);
1360 else if (rhs
.type
== ADDRESSOF
)
1363 gcc_checking_assert (find (rhs
.var
) == rhs
.var
);
1364 bitmap_set_bit (get_varinfo (lhsvar
)->solution
, rhsvar
);
1366 else if (lhsvar
> anything_id
1367 && lhsvar
!= rhsvar
&& lhs
.offset
== 0 && rhs
.offset
== 0)
1369 add_graph_edge (graph
, lhsvar
, rhsvar
);
1373 /* Add edges from STOREDANYTHING to all non-direct nodes that can
1374 receive pointers. */
1375 t
= find (storedanything_id
);
1376 for (i
= integer_id
+ 1; i
< FIRST_REF_NODE
; ++i
)
1378 if (!bitmap_bit_p (graph
->direct_nodes
, i
)
1379 && get_varinfo (i
)->may_have_pointers
)
1380 add_graph_edge (graph
, find (i
), t
);
1383 /* Everything stored to ANYTHING also potentially escapes. */
1384 add_graph_edge (graph
, find (escaped_id
), t
);
1388 /* Changed variables on the last iteration. */
1389 static bitmap changed
;
1391 /* Strongly Connected Component visitation info. */
1398 unsigned int *node_mapping
;
1400 vec
<unsigned> scc_stack
;
1404 /* Recursive routine to find strongly connected components in GRAPH.
1405 SI is the SCC info to store the information in, and N is the id of current
1406 graph node we are processing.
1408 This is Tarjan's strongly connected component finding algorithm, as
1409 modified by Nuutila to keep only non-root nodes on the stack.
1410 The algorithm can be found in "On finding the strongly connected
1411 connected components in a directed graph" by Esko Nuutila and Eljas
1412 Soisalon-Soininen, in Information Processing Letters volume 49,
1413 number 1, pages 9-14. */
1416 scc_visit (constraint_graph_t graph
, struct scc_info
*si
, unsigned int n
)
1420 unsigned int my_dfs
;
1422 bitmap_set_bit (si
->visited
, n
);
1423 si
->dfs
[n
] = si
->current_index
++;
1424 my_dfs
= si
->dfs
[n
];
1426 /* Visit all the successors. */
1427 EXECUTE_IF_IN_NONNULL_BITMAP (graph
->succs
[n
], 0, i
, bi
)
1431 if (i
> LAST_REF_NODE
)
1435 if (bitmap_bit_p (si
->deleted
, w
))
1438 if (!bitmap_bit_p (si
->visited
, w
))
1439 scc_visit (graph
, si
, w
);
1441 unsigned int t
= find (w
);
1442 gcc_checking_assert (find (n
) == n
);
1443 if (si
->dfs
[t
] < si
->dfs
[n
])
1444 si
->dfs
[n
] = si
->dfs
[t
];
1447 /* See if any components have been identified. */
1448 if (si
->dfs
[n
] == my_dfs
)
1450 if (si
->scc_stack
.length () > 0
1451 && si
->dfs
[si
->scc_stack
.last ()] >= my_dfs
)
1453 bitmap scc
= BITMAP_ALLOC (NULL
);
1454 unsigned int lowest_node
;
1457 bitmap_set_bit (scc
, n
);
1459 while (si
->scc_stack
.length () != 0
1460 && si
->dfs
[si
->scc_stack
.last ()] >= my_dfs
)
1462 unsigned int w
= si
->scc_stack
.pop ();
1464 bitmap_set_bit (scc
, w
);
1467 lowest_node
= bitmap_first_set_bit (scc
);
1468 gcc_assert (lowest_node
< FIRST_REF_NODE
);
1470 /* Collapse the SCC nodes into a single node, and mark the
1472 EXECUTE_IF_SET_IN_BITMAP (scc
, 0, i
, bi
)
1474 if (i
< FIRST_REF_NODE
)
1476 if (unite (lowest_node
, i
))
1477 unify_nodes (graph
, lowest_node
, i
, false);
1481 unite (lowest_node
, i
);
1482 graph
->indirect_cycles
[i
- FIRST_REF_NODE
] = lowest_node
;
1486 bitmap_set_bit (si
->deleted
, n
);
1489 si
->scc_stack
.safe_push (n
);
1492 /* Unify node FROM into node TO, updating the changed count if
1493 necessary when UPDATE_CHANGED is true. */
1496 unify_nodes (constraint_graph_t graph
, unsigned int to
, unsigned int from
,
1497 bool update_changed
)
1499 gcc_checking_assert (to
!= from
&& find (to
) == to
);
1501 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1502 fprintf (dump_file
, "Unifying %s to %s\n",
1503 get_varinfo (from
)->name
,
1504 get_varinfo (to
)->name
);
1507 stats
.unified_vars_dynamic
++;
1509 stats
.unified_vars_static
++;
1511 merge_graph_nodes (graph
, to
, from
);
1512 if (merge_node_constraints (graph
, to
, from
))
1515 bitmap_set_bit (changed
, to
);
1518 /* Mark TO as changed if FROM was changed. If TO was already marked
1519 as changed, decrease the changed count. */
1522 && bitmap_clear_bit (changed
, from
))
1523 bitmap_set_bit (changed
, to
);
1524 varinfo_t fromvi
= get_varinfo (from
);
1525 if (fromvi
->solution
)
1527 /* If the solution changes because of the merging, we need to mark
1528 the variable as changed. */
1529 varinfo_t tovi
= get_varinfo (to
);
1530 if (bitmap_ior_into (tovi
->solution
, fromvi
->solution
))
1533 bitmap_set_bit (changed
, to
);
1536 BITMAP_FREE (fromvi
->solution
);
1537 if (fromvi
->oldsolution
)
1538 BITMAP_FREE (fromvi
->oldsolution
);
1540 if (stats
.iterations
> 0
1541 && tovi
->oldsolution
)
1542 BITMAP_FREE (tovi
->oldsolution
);
1544 if (graph
->succs
[to
])
1545 bitmap_clear_bit (graph
->succs
[to
], to
);
1548 /* Information needed to compute the topological ordering of a graph. */
1552 /* sbitmap of visited nodes. */
1554 /* Array that stores the topological order of the graph, *in
1556 vec
<unsigned> topo_order
;
1560 /* Initialize and return a topological info structure. */
1562 static struct topo_info
*
1563 init_topo_info (void)
1565 size_t size
= graph
->size
;
1566 struct topo_info
*ti
= XNEW (struct topo_info
);
1567 ti
->visited
= sbitmap_alloc (size
);
1568 bitmap_clear (ti
->visited
);
1569 ti
->topo_order
.create (1);
1574 /* Free the topological sort info pointed to by TI. */
1577 free_topo_info (struct topo_info
*ti
)
1579 sbitmap_free (ti
->visited
);
1580 ti
->topo_order
.release ();
1584 /* Visit the graph in topological order, and store the order in the
1585 topo_info structure. */
1588 topo_visit (constraint_graph_t graph
, struct topo_info
*ti
,
1594 bitmap_set_bit (ti
->visited
, n
);
1596 if (graph
->succs
[n
])
1597 EXECUTE_IF_SET_IN_BITMAP (graph
->succs
[n
], 0, j
, bi
)
1599 if (!bitmap_bit_p (ti
->visited
, j
))
1600 topo_visit (graph
, ti
, j
);
1603 ti
->topo_order
.safe_push (n
);
1606 /* Process a constraint C that represents x = *(y + off), using DELTA as the
1607 starting solution for y. */
1610 do_sd_constraint (constraint_graph_t graph
, constraint_t c
,
1611 bitmap delta
, bitmap
*expanded_delta
)
1613 unsigned int lhs
= c
->lhs
.var
;
1615 bitmap sol
= get_varinfo (lhs
)->solution
;
1618 HOST_WIDE_INT roffset
= c
->rhs
.offset
;
1620 /* Our IL does not allow this. */
1621 gcc_checking_assert (c
->lhs
.offset
== 0);
1623 /* If the solution of Y contains anything it is good enough to transfer
1625 if (bitmap_bit_p (delta
, anything_id
))
1627 flag
|= bitmap_set_bit (sol
, anything_id
);
1631 /* If we do not know at with offset the rhs is dereferenced compute
1632 the reachability set of DELTA, conservatively assuming it is
1633 dereferenced at all valid offsets. */
1634 if (roffset
== UNKNOWN_OFFSET
)
1636 delta
= solution_set_expand (delta
, expanded_delta
);
1637 /* No further offset processing is necessary. */
1641 /* For each variable j in delta (Sol(y)), add
1642 an edge in the graph from j to x, and union Sol(j) into Sol(x). */
1643 EXECUTE_IF_SET_IN_BITMAP (delta
, 0, j
, bi
)
1645 varinfo_t v
= get_varinfo (j
);
1646 HOST_WIDE_INT fieldoffset
= v
->offset
+ roffset
;
1647 unsigned HOST_WIDE_INT size
= v
->size
;
1652 else if (roffset
!= 0)
1654 if (fieldoffset
< 0)
1655 v
= get_varinfo (v
->head
);
1657 v
= first_or_preceding_vi_for_offset (v
, fieldoffset
);
1660 /* We have to include all fields that overlap the current field
1661 shifted by roffset. */
1666 /* Adding edges from the special vars is pointless.
1667 They don't have sets that can change. */
1668 if (get_varinfo (t
)->is_special_var
)
1669 flag
|= bitmap_ior_into (sol
, get_varinfo (t
)->solution
);
1670 /* Merging the solution from ESCAPED needlessly increases
1671 the set. Use ESCAPED as representative instead. */
1672 else if (v
->id
== escaped_id
)
1673 flag
|= bitmap_set_bit (sol
, escaped_id
);
1674 else if (v
->may_have_pointers
1675 && add_graph_edge (graph
, lhs
, t
))
1676 flag
|= bitmap_ior_into (sol
, get_varinfo (t
)->solution
);
1684 while (v
->offset
< fieldoffset
+ size
);
1688 /* If the LHS solution changed, mark the var as changed. */
1691 get_varinfo (lhs
)->solution
= sol
;
1692 bitmap_set_bit (changed
, lhs
);
1696 /* Process a constraint C that represents *(x + off) = y using DELTA
1697 as the starting solution for x. */
1700 do_ds_constraint (constraint_t c
, bitmap delta
, bitmap
*expanded_delta
)
1702 unsigned int rhs
= c
->rhs
.var
;
1703 bitmap sol
= get_varinfo (rhs
)->solution
;
1706 HOST_WIDE_INT loff
= c
->lhs
.offset
;
1707 bool escaped_p
= false;
1709 /* Our IL does not allow this. */
1710 gcc_checking_assert (c
->rhs
.offset
== 0);
1712 /* If the solution of y contains ANYTHING simply use the ANYTHING
1713 solution. This avoids needlessly increasing the points-to sets. */
1714 if (bitmap_bit_p (sol
, anything_id
))
1715 sol
= get_varinfo (find (anything_id
))->solution
;
1717 /* If the solution for x contains ANYTHING we have to merge the
1718 solution of y into all pointer variables which we do via
1720 if (bitmap_bit_p (delta
, anything_id
))
1722 unsigned t
= find (storedanything_id
);
1723 if (add_graph_edge (graph
, t
, rhs
))
1725 if (bitmap_ior_into (get_varinfo (t
)->solution
, sol
))
1726 bitmap_set_bit (changed
, t
);
1731 /* If we do not know at with offset the rhs is dereferenced compute
1732 the reachability set of DELTA, conservatively assuming it is
1733 dereferenced at all valid offsets. */
1734 if (loff
== UNKNOWN_OFFSET
)
1736 delta
= solution_set_expand (delta
, expanded_delta
);
1740 /* For each member j of delta (Sol(x)), add an edge from y to j and
1741 union Sol(y) into Sol(j) */
1742 EXECUTE_IF_SET_IN_BITMAP (delta
, 0, j
, bi
)
1744 varinfo_t v
= get_varinfo (j
);
1746 HOST_WIDE_INT fieldoffset
= v
->offset
+ loff
;
1747 unsigned HOST_WIDE_INT size
= v
->size
;
1753 if (fieldoffset
< 0)
1754 v
= get_varinfo (v
->head
);
1756 v
= first_or_preceding_vi_for_offset (v
, fieldoffset
);
1759 /* We have to include all fields that overlap the current field
1763 if (v
->may_have_pointers
)
1765 /* If v is a global variable then this is an escape point. */
1766 if (v
->is_global_var
1769 t
= find (escaped_id
);
1770 if (add_graph_edge (graph
, t
, rhs
)
1771 && bitmap_ior_into (get_varinfo (t
)->solution
, sol
))
1772 bitmap_set_bit (changed
, t
);
1773 /* Enough to let rhs escape once. */
1777 if (v
->is_special_var
)
1781 if (add_graph_edge (graph
, t
, rhs
)
1782 && bitmap_ior_into (get_varinfo (t
)->solution
, sol
))
1783 bitmap_set_bit (changed
, t
);
1792 while (v
->offset
< fieldoffset
+ size
);
1796 /* Handle a non-simple (simple meaning requires no iteration),
1797 constraint (IE *x = &y, x = *y, *x = y, and x = y with offsets involved). */
1800 do_complex_constraint (constraint_graph_t graph
, constraint_t c
, bitmap delta
,
1801 bitmap
*expanded_delta
)
1803 if (c
->lhs
.type
== DEREF
)
1805 if (c
->rhs
.type
== ADDRESSOF
)
1812 do_ds_constraint (c
, delta
, expanded_delta
);
1815 else if (c
->rhs
.type
== DEREF
)
1818 if (!(get_varinfo (c
->lhs
.var
)->is_special_var
))
1819 do_sd_constraint (graph
, c
, delta
, expanded_delta
);
1826 gcc_checking_assert (c
->rhs
.type
== SCALAR
&& c
->lhs
.type
== SCALAR
1827 && c
->rhs
.offset
!= 0 && c
->lhs
.offset
== 0);
1828 tmp
= get_varinfo (c
->lhs
.var
)->solution
;
1830 flag
= set_union_with_increment (tmp
, delta
, c
->rhs
.offset
,
1834 bitmap_set_bit (changed
, c
->lhs
.var
);
1838 /* Initialize and return a new SCC info structure. */
1840 static struct scc_info
*
1841 init_scc_info (size_t size
)
1843 struct scc_info
*si
= XNEW (struct scc_info
);
1846 si
->current_index
= 0;
1847 si
->visited
= sbitmap_alloc (size
);
1848 bitmap_clear (si
->visited
);
1849 si
->deleted
= sbitmap_alloc (size
);
1850 bitmap_clear (si
->deleted
);
1851 si
->node_mapping
= XNEWVEC (unsigned int, size
);
1852 si
->dfs
= XCNEWVEC (unsigned int, size
);
1854 for (i
= 0; i
< size
; i
++)
1855 si
->node_mapping
[i
] = i
;
1857 si
->scc_stack
.create (1);
1861 /* Free an SCC info structure pointed to by SI */
1864 free_scc_info (struct scc_info
*si
)
1866 sbitmap_free (si
->visited
);
1867 sbitmap_free (si
->deleted
);
1868 free (si
->node_mapping
);
1870 si
->scc_stack
.release ();
1875 /* Find indirect cycles in GRAPH that occur, using strongly connected
1876 components, and note them in the indirect cycles map.
1878 This technique comes from Ben Hardekopf and Calvin Lin,
1879 "It Pays to be Lazy: Fast and Accurate Pointer Analysis for Millions of
1880 Lines of Code", submitted to PLDI 2007. */
1883 find_indirect_cycles (constraint_graph_t graph
)
1886 unsigned int size
= graph
->size
;
1887 struct scc_info
*si
= init_scc_info (size
);
1889 for (i
= 0; i
< MIN (LAST_REF_NODE
, size
); i
++ )
1890 if (!bitmap_bit_p (si
->visited
, i
) && find (i
) == i
)
1891 scc_visit (graph
, si
, i
);
1896 /* Compute a topological ordering for GRAPH, and store the result in the
1897 topo_info structure TI. */
1900 compute_topo_order (constraint_graph_t graph
,
1901 struct topo_info
*ti
)
1904 unsigned int size
= graph
->size
;
1906 for (i
= 0; i
!= size
; ++i
)
1907 if (!bitmap_bit_p (ti
->visited
, i
) && find (i
) == i
)
1908 topo_visit (graph
, ti
, i
);
1911 /* Structure used to for hash value numbering of pointer equivalence
1914 typedef struct equiv_class_label
1917 unsigned int equivalence_class
;
1919 } *equiv_class_label_t
;
1920 typedef const struct equiv_class_label
*const_equiv_class_label_t
;
1922 /* Equiv_class_label hashtable helpers. */
1924 struct equiv_class_hasher
: typed_free_remove
<equiv_class_label
>
1926 typedef equiv_class_label value_type
;
1927 typedef equiv_class_label compare_type
;
1928 static inline hashval_t
hash (const value_type
*);
1929 static inline bool equal (const value_type
*, const compare_type
*);
1932 /* Hash function for a equiv_class_label_t */
1935 equiv_class_hasher::hash (const value_type
*ecl
)
1937 return ecl
->hashcode
;
1940 /* Equality function for two equiv_class_label_t's. */
1943 equiv_class_hasher::equal (const value_type
*eql1
, const compare_type
*eql2
)
1945 return (eql1
->hashcode
== eql2
->hashcode
1946 && bitmap_equal_p (eql1
->labels
, eql2
->labels
));
1949 /* A hashtable for mapping a bitmap of labels->pointer equivalence
1951 static hash_table
<equiv_class_hasher
> *pointer_equiv_class_table
;
1953 /* A hashtable for mapping a bitmap of labels->location equivalence
1955 static hash_table
<equiv_class_hasher
> *location_equiv_class_table
;
1957 /* Lookup a equivalence class in TABLE by the bitmap of LABELS with
1958 hash HAS it contains. Sets *REF_LABELS to the bitmap LABELS
1959 is equivalent to. */
1961 static equiv_class_label
*
1962 equiv_class_lookup_or_add (hash_table
<equiv_class_hasher
> *table
,
1965 equiv_class_label
**slot
;
1966 equiv_class_label ecl
;
1968 ecl
.labels
= labels
;
1969 ecl
.hashcode
= bitmap_hash (labels
);
1970 slot
= table
->find_slot (&ecl
, INSERT
);
1973 *slot
= XNEW (struct equiv_class_label
);
1974 (*slot
)->labels
= labels
;
1975 (*slot
)->hashcode
= ecl
.hashcode
;
1976 (*slot
)->equivalence_class
= 0;
1982 /* Perform offline variable substitution.
1984 This is a worst case quadratic time way of identifying variables
1985 that must have equivalent points-to sets, including those caused by
1986 static cycles, and single entry subgraphs, in the constraint graph.
1988 The technique is described in "Exploiting Pointer and Location
1989 Equivalence to Optimize Pointer Analysis. In the 14th International
1990 Static Analysis Symposium (SAS), August 2007." It is known as the
1991 "HU" algorithm, and is equivalent to value numbering the collapsed
1992 constraint graph including evaluating unions.
1994 The general method of finding equivalence classes is as follows:
1995 Add fake nodes (REF nodes) and edges for *a = b and a = *b constraints.
1996 Initialize all non-REF nodes to be direct nodes.
1997 For each constraint a = a U {b}, we set pts(a) = pts(a) u {fresh
1999 For each constraint containing the dereference, we also do the same
2002 We then compute SCC's in the graph and unify nodes in the same SCC,
2005 For each non-collapsed node x:
2006 Visit all unvisited explicit incoming edges.
2007 Ignoring all non-pointers, set pts(x) = Union of pts(a) for y
2009 Lookup the equivalence class for pts(x).
2010 If we found one, equivalence_class(x) = found class.
2011 Otherwise, equivalence_class(x) = new class, and new_class is
2012 added to the lookup table.
2014 All direct nodes with the same equivalence class can be replaced
2015 with a single representative node.
2016 All unlabeled nodes (label == 0) are not pointers and all edges
2017 involving them can be eliminated.
2018 We perform these optimizations during rewrite_constraints
2020 In addition to pointer equivalence class finding, we also perform
2021 location equivalence class finding. This is the set of variables
2022 that always appear together in points-to sets. We use this to
2023 compress the size of the points-to sets. */
2025 /* Current maximum pointer equivalence class id. */
2026 static int pointer_equiv_class
;
2028 /* Current maximum location equivalence class id. */
2029 static int location_equiv_class
;
2031 /* Recursive routine to find strongly connected components in GRAPH,
2032 and label it's nodes with DFS numbers. */
2035 condense_visit (constraint_graph_t graph
, struct scc_info
*si
, unsigned int n
)
2039 unsigned int my_dfs
;
2041 gcc_checking_assert (si
->node_mapping
[n
] == n
);
2042 bitmap_set_bit (si
->visited
, n
);
2043 si
->dfs
[n
] = si
->current_index
++;
2044 my_dfs
= si
->dfs
[n
];
2046 /* Visit all the successors. */
2047 EXECUTE_IF_IN_NONNULL_BITMAP (graph
->preds
[n
], 0, i
, bi
)
2049 unsigned int w
= si
->node_mapping
[i
];
2051 if (bitmap_bit_p (si
->deleted
, w
))
2054 if (!bitmap_bit_p (si
->visited
, w
))
2055 condense_visit (graph
, si
, w
);
2057 unsigned int t
= si
->node_mapping
[w
];
2058 gcc_checking_assert (si
->node_mapping
[n
] == n
);
2059 if (si
->dfs
[t
] < si
->dfs
[n
])
2060 si
->dfs
[n
] = si
->dfs
[t
];
2063 /* Visit all the implicit predecessors. */
2064 EXECUTE_IF_IN_NONNULL_BITMAP (graph
->implicit_preds
[n
], 0, i
, bi
)
2066 unsigned int w
= si
->node_mapping
[i
];
2068 if (bitmap_bit_p (si
->deleted
, w
))
2071 if (!bitmap_bit_p (si
->visited
, w
))
2072 condense_visit (graph
, si
, w
);
2074 unsigned int t
= si
->node_mapping
[w
];
2075 gcc_assert (si
->node_mapping
[n
] == n
);
2076 if (si
->dfs
[t
] < si
->dfs
[n
])
2077 si
->dfs
[n
] = si
->dfs
[t
];
2080 /* See if any components have been identified. */
2081 if (si
->dfs
[n
] == my_dfs
)
2083 while (si
->scc_stack
.length () != 0
2084 && si
->dfs
[si
->scc_stack
.last ()] >= my_dfs
)
2086 unsigned int w
= si
->scc_stack
.pop ();
2087 si
->node_mapping
[w
] = n
;
2089 if (!bitmap_bit_p (graph
->direct_nodes
, w
))
2090 bitmap_clear_bit (graph
->direct_nodes
, n
);
2092 /* Unify our nodes. */
2093 if (graph
->preds
[w
])
2095 if (!graph
->preds
[n
])
2096 graph
->preds
[n
] = BITMAP_ALLOC (&predbitmap_obstack
);
2097 bitmap_ior_into (graph
->preds
[n
], graph
->preds
[w
]);
2099 if (graph
->implicit_preds
[w
])
2101 if (!graph
->implicit_preds
[n
])
2102 graph
->implicit_preds
[n
] = BITMAP_ALLOC (&predbitmap_obstack
);
2103 bitmap_ior_into (graph
->implicit_preds
[n
],
2104 graph
->implicit_preds
[w
]);
2106 if (graph
->points_to
[w
])
2108 if (!graph
->points_to
[n
])
2109 graph
->points_to
[n
] = BITMAP_ALLOC (&predbitmap_obstack
);
2110 bitmap_ior_into (graph
->points_to
[n
],
2111 graph
->points_to
[w
]);
2114 bitmap_set_bit (si
->deleted
, n
);
2117 si
->scc_stack
.safe_push (n
);
2120 /* Label pointer equivalences.
2122 This performs a value numbering of the constraint graph to
2123 discover which variables will always have the same points-to sets
2124 under the current set of constraints.
2126 The way it value numbers is to store the set of points-to bits
2127 generated by the constraints and graph edges. This is just used as a
2128 hash and equality comparison. The *actual set of points-to bits* is
2129 completely irrelevant, in that we don't care about being able to
2132 The equality values (currently bitmaps) just have to satisfy a few
2133 constraints, the main ones being:
2134 1. The combining operation must be order independent.
2135 2. The end result of a given set of operations must be unique iff the
2136 combination of input values is unique
2140 label_visit (constraint_graph_t graph
, struct scc_info
*si
, unsigned int n
)
2142 unsigned int i
, first_pred
;
2145 bitmap_set_bit (si
->visited
, n
);
2147 /* Label and union our incoming edges's points to sets. */
2149 EXECUTE_IF_IN_NONNULL_BITMAP (graph
->preds
[n
], 0, i
, bi
)
2151 unsigned int w
= si
->node_mapping
[i
];
2152 if (!bitmap_bit_p (si
->visited
, w
))
2153 label_visit (graph
, si
, w
);
2155 /* Skip unused edges */
2156 if (w
== n
|| graph
->pointer_label
[w
] == 0)
2159 if (graph
->points_to
[w
])
2161 if (!graph
->points_to
[n
])
2163 if (first_pred
== -1U)
2167 graph
->points_to
[n
] = BITMAP_ALLOC (&predbitmap_obstack
);
2168 bitmap_ior (graph
->points_to
[n
],
2169 graph
->points_to
[first_pred
],
2170 graph
->points_to
[w
]);
2174 bitmap_ior_into (graph
->points_to
[n
], graph
->points_to
[w
]);
2178 /* Indirect nodes get fresh variables and a new pointer equiv class. */
2179 if (!bitmap_bit_p (graph
->direct_nodes
, n
))
2181 if (!graph
->points_to
[n
])
2183 graph
->points_to
[n
] = BITMAP_ALLOC (&predbitmap_obstack
);
2184 if (first_pred
!= -1U)
2185 bitmap_copy (graph
->points_to
[n
], graph
->points_to
[first_pred
]);
2187 bitmap_set_bit (graph
->points_to
[n
], FIRST_REF_NODE
+ n
);
2188 graph
->pointer_label
[n
] = pointer_equiv_class
++;
2189 equiv_class_label_t ecl
;
2190 ecl
= equiv_class_lookup_or_add (pointer_equiv_class_table
,
2191 graph
->points_to
[n
]);
2192 ecl
->equivalence_class
= graph
->pointer_label
[n
];
2196 /* If there was only a single non-empty predecessor the pointer equiv
2197 class is the same. */
2198 if (!graph
->points_to
[n
])
2200 if (first_pred
!= -1U)
2202 graph
->pointer_label
[n
] = graph
->pointer_label
[first_pred
];
2203 graph
->points_to
[n
] = graph
->points_to
[first_pred
];
2208 if (!bitmap_empty_p (graph
->points_to
[n
]))
2210 equiv_class_label_t ecl
;
2211 ecl
= equiv_class_lookup_or_add (pointer_equiv_class_table
,
2212 graph
->points_to
[n
]);
2213 if (ecl
->equivalence_class
== 0)
2214 ecl
->equivalence_class
= pointer_equiv_class
++;
2217 BITMAP_FREE (graph
->points_to
[n
]);
2218 graph
->points_to
[n
] = ecl
->labels
;
2220 graph
->pointer_label
[n
] = ecl
->equivalence_class
;
2224 /* Print the pred graph in dot format. */
2227 dump_pred_graph (struct scc_info
*si
, FILE *file
)
2231 /* Only print the graph if it has already been initialized: */
2235 /* Prints the header of the dot file: */
2236 fprintf (file
, "strict digraph {\n");
2237 fprintf (file
, " node [\n shape = box\n ]\n");
2238 fprintf (file
, " edge [\n fontsize = \"12\"\n ]\n");
2239 fprintf (file
, "\n // List of nodes and complex constraints in "
2240 "the constraint graph:\n");
2242 /* The next lines print the nodes in the graph together with the
2243 complex constraints attached to them. */
2244 for (i
= 1; i
< graph
->size
; i
++)
2246 if (i
== FIRST_REF_NODE
)
2248 if (si
->node_mapping
[i
] != i
)
2250 if (i
< FIRST_REF_NODE
)
2251 fprintf (file
, "\"%s\"", get_varinfo (i
)->name
);
2253 fprintf (file
, "\"*%s\"", get_varinfo (i
- FIRST_REF_NODE
)->name
);
2254 if (graph
->points_to
[i
]
2255 && !bitmap_empty_p (graph
->points_to
[i
]))
2257 fprintf (file
, "[label=\"%s = {", get_varinfo (i
)->name
);
2260 EXECUTE_IF_SET_IN_BITMAP (graph
->points_to
[i
], 0, j
, bi
)
2261 fprintf (file
, " %d", j
);
2262 fprintf (file
, " }\"]");
2264 fprintf (file
, ";\n");
2267 /* Go over the edges. */
2268 fprintf (file
, "\n // Edges in the constraint graph:\n");
2269 for (i
= 1; i
< graph
->size
; i
++)
2273 if (si
->node_mapping
[i
] != i
)
2275 EXECUTE_IF_IN_NONNULL_BITMAP (graph
->preds
[i
], 0, j
, bi
)
2277 unsigned from
= si
->node_mapping
[j
];
2278 if (from
< FIRST_REF_NODE
)
2279 fprintf (file
, "\"%s\"", get_varinfo (from
)->name
);
2281 fprintf (file
, "\"*%s\"", get_varinfo (from
- FIRST_REF_NODE
)->name
);
2282 fprintf (file
, " -> ");
2283 if (i
< FIRST_REF_NODE
)
2284 fprintf (file
, "\"%s\"", get_varinfo (i
)->name
);
2286 fprintf (file
, "\"*%s\"", get_varinfo (i
- FIRST_REF_NODE
)->name
);
2287 fprintf (file
, ";\n");
2291 /* Prints the tail of the dot file. */
2292 fprintf (file
, "}\n");
2295 /* Perform offline variable substitution, discovering equivalence
2296 classes, and eliminating non-pointer variables. */
2298 static struct scc_info
*
2299 perform_var_substitution (constraint_graph_t graph
)
2302 unsigned int size
= graph
->size
;
2303 struct scc_info
*si
= init_scc_info (size
);
2305 bitmap_obstack_initialize (&iteration_obstack
);
2306 pointer_equiv_class_table
= new hash_table
<equiv_class_hasher
> (511);
2307 location_equiv_class_table
2308 = new hash_table
<equiv_class_hasher
> (511);
2309 pointer_equiv_class
= 1;
2310 location_equiv_class
= 1;
2312 /* Condense the nodes, which means to find SCC's, count incoming
2313 predecessors, and unite nodes in SCC's. */
2314 for (i
= 1; i
< FIRST_REF_NODE
; i
++)
2315 if (!bitmap_bit_p (si
->visited
, si
->node_mapping
[i
]))
2316 condense_visit (graph
, si
, si
->node_mapping
[i
]);
2318 if (dump_file
&& (dump_flags
& TDF_GRAPH
))
2320 fprintf (dump_file
, "\n\n// The constraint graph before var-substitution "
2321 "in dot format:\n");
2322 dump_pred_graph (si
, dump_file
);
2323 fprintf (dump_file
, "\n\n");
2326 bitmap_clear (si
->visited
);
2327 /* Actually the label the nodes for pointer equivalences */
2328 for (i
= 1; i
< FIRST_REF_NODE
; i
++)
2329 if (!bitmap_bit_p (si
->visited
, si
->node_mapping
[i
]))
2330 label_visit (graph
, si
, si
->node_mapping
[i
]);
2332 /* Calculate location equivalence labels. */
2333 for (i
= 1; i
< FIRST_REF_NODE
; i
++)
2339 if (!graph
->pointed_by
[i
])
2341 pointed_by
= BITMAP_ALLOC (&iteration_obstack
);
2343 /* Translate the pointed-by mapping for pointer equivalence
2345 EXECUTE_IF_SET_IN_BITMAP (graph
->pointed_by
[i
], 0, j
, bi
)
2347 bitmap_set_bit (pointed_by
,
2348 graph
->pointer_label
[si
->node_mapping
[j
]]);
2350 /* The original pointed_by is now dead. */
2351 BITMAP_FREE (graph
->pointed_by
[i
]);
2353 /* Look up the location equivalence label if one exists, or make
2355 equiv_class_label_t ecl
;
2356 ecl
= equiv_class_lookup_or_add (location_equiv_class_table
, pointed_by
);
2357 if (ecl
->equivalence_class
== 0)
2358 ecl
->equivalence_class
= location_equiv_class
++;
2361 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2362 fprintf (dump_file
, "Found location equivalence for node %s\n",
2363 get_varinfo (i
)->name
);
2364 BITMAP_FREE (pointed_by
);
2366 graph
->loc_label
[i
] = ecl
->equivalence_class
;
2370 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2371 for (i
= 1; i
< FIRST_REF_NODE
; i
++)
2373 unsigned j
= si
->node_mapping
[i
];
2376 fprintf (dump_file
, "%s node id %d ",
2377 bitmap_bit_p (graph
->direct_nodes
, i
)
2378 ? "Direct" : "Indirect", i
);
2379 if (i
< FIRST_REF_NODE
)
2380 fprintf (dump_file
, "\"%s\"", get_varinfo (i
)->name
);
2382 fprintf (dump_file
, "\"*%s\"",
2383 get_varinfo (i
- FIRST_REF_NODE
)->name
);
2384 fprintf (dump_file
, " mapped to SCC leader node id %d ", j
);
2385 if (j
< FIRST_REF_NODE
)
2386 fprintf (dump_file
, "\"%s\"\n", get_varinfo (j
)->name
);
2388 fprintf (dump_file
, "\"*%s\"\n",
2389 get_varinfo (j
- FIRST_REF_NODE
)->name
);
2394 "Equivalence classes for %s node id %d ",
2395 bitmap_bit_p (graph
->direct_nodes
, i
)
2396 ? "direct" : "indirect", i
);
2397 if (i
< FIRST_REF_NODE
)
2398 fprintf (dump_file
, "\"%s\"", get_varinfo (i
)->name
);
2400 fprintf (dump_file
, "\"*%s\"",
2401 get_varinfo (i
- FIRST_REF_NODE
)->name
);
2403 ": pointer %d, location %d\n",
2404 graph
->pointer_label
[i
], graph
->loc_label
[i
]);
2408 /* Quickly eliminate our non-pointer variables. */
2410 for (i
= 1; i
< FIRST_REF_NODE
; i
++)
2412 unsigned int node
= si
->node_mapping
[i
];
2414 if (graph
->pointer_label
[node
] == 0)
2416 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2418 "%s is a non-pointer variable, eliminating edges.\n",
2419 get_varinfo (node
)->name
);
2420 stats
.nonpointer_vars
++;
2421 clear_edges_for_node (graph
, node
);
2428 /* Free information that was only necessary for variable
2432 free_var_substitution_info (struct scc_info
*si
)
2435 free (graph
->pointer_label
);
2436 free (graph
->loc_label
);
2437 free (graph
->pointed_by
);
2438 free (graph
->points_to
);
2439 free (graph
->eq_rep
);
2440 sbitmap_free (graph
->direct_nodes
);
2441 delete pointer_equiv_class_table
;
2442 pointer_equiv_class_table
= NULL
;
2443 delete location_equiv_class_table
;
2444 location_equiv_class_table
= NULL
;
2445 bitmap_obstack_release (&iteration_obstack
);
2448 /* Return an existing node that is equivalent to NODE, which has
2449 equivalence class LABEL, if one exists. Return NODE otherwise. */
2452 find_equivalent_node (constraint_graph_t graph
,
2453 unsigned int node
, unsigned int label
)
2455 /* If the address version of this variable is unused, we can
2456 substitute it for anything else with the same label.
2457 Otherwise, we know the pointers are equivalent, but not the
2458 locations, and we can unite them later. */
2460 if (!bitmap_bit_p (graph
->address_taken
, node
))
2462 gcc_checking_assert (label
< graph
->size
);
2464 if (graph
->eq_rep
[label
] != -1)
2466 /* Unify the two variables since we know they are equivalent. */
2467 if (unite (graph
->eq_rep
[label
], node
))
2468 unify_nodes (graph
, graph
->eq_rep
[label
], node
, false);
2469 return graph
->eq_rep
[label
];
2473 graph
->eq_rep
[label
] = node
;
2474 graph
->pe_rep
[label
] = node
;
2479 gcc_checking_assert (label
< graph
->size
);
2480 graph
->pe
[node
] = label
;
2481 if (graph
->pe_rep
[label
] == -1)
2482 graph
->pe_rep
[label
] = node
;
2488 /* Unite pointer equivalent but not location equivalent nodes in
2489 GRAPH. This may only be performed once variable substitution is
2493 unite_pointer_equivalences (constraint_graph_t graph
)
2497 /* Go through the pointer equivalences and unite them to their
2498 representative, if they aren't already. */
2499 for (i
= 1; i
< FIRST_REF_NODE
; i
++)
2501 unsigned int label
= graph
->pe
[i
];
2504 int label_rep
= graph
->pe_rep
[label
];
2506 if (label_rep
== -1)
2509 label_rep
= find (label_rep
);
2510 if (label_rep
>= 0 && unite (label_rep
, find (i
)))
2511 unify_nodes (graph
, label_rep
, i
, false);
2516 /* Move complex constraints to the GRAPH nodes they belong to. */
2519 move_complex_constraints (constraint_graph_t graph
)
2524 FOR_EACH_VEC_ELT (constraints
, i
, c
)
2528 struct constraint_expr lhs
= c
->lhs
;
2529 struct constraint_expr rhs
= c
->rhs
;
2531 if (lhs
.type
== DEREF
)
2533 insert_into_complex (graph
, lhs
.var
, c
);
2535 else if (rhs
.type
== DEREF
)
2537 if (!(get_varinfo (lhs
.var
)->is_special_var
))
2538 insert_into_complex (graph
, rhs
.var
, c
);
2540 else if (rhs
.type
!= ADDRESSOF
&& lhs
.var
> anything_id
2541 && (lhs
.offset
!= 0 || rhs
.offset
!= 0))
2543 insert_into_complex (graph
, rhs
.var
, c
);
2550 /* Optimize and rewrite complex constraints while performing
2551 collapsing of equivalent nodes. SI is the SCC_INFO that is the
2552 result of perform_variable_substitution. */
2555 rewrite_constraints (constraint_graph_t graph
,
2556 struct scc_info
*si
)
2561 #ifdef ENABLE_CHECKING
2562 for (unsigned int j
= 0; j
< graph
->size
; j
++)
2563 gcc_assert (find (j
) == j
);
2566 FOR_EACH_VEC_ELT (constraints
, i
, c
)
2568 struct constraint_expr lhs
= c
->lhs
;
2569 struct constraint_expr rhs
= c
->rhs
;
2570 unsigned int lhsvar
= find (lhs
.var
);
2571 unsigned int rhsvar
= find (rhs
.var
);
2572 unsigned int lhsnode
, rhsnode
;
2573 unsigned int lhslabel
, rhslabel
;
2575 lhsnode
= si
->node_mapping
[lhsvar
];
2576 rhsnode
= si
->node_mapping
[rhsvar
];
2577 lhslabel
= graph
->pointer_label
[lhsnode
];
2578 rhslabel
= graph
->pointer_label
[rhsnode
];
2580 /* See if it is really a non-pointer variable, and if so, ignore
2584 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2587 fprintf (dump_file
, "%s is a non-pointer variable,"
2588 "ignoring constraint:",
2589 get_varinfo (lhs
.var
)->name
);
2590 dump_constraint (dump_file
, c
);
2591 fprintf (dump_file
, "\n");
2593 constraints
[i
] = NULL
;
2599 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2602 fprintf (dump_file
, "%s is a non-pointer variable,"
2603 "ignoring constraint:",
2604 get_varinfo (rhs
.var
)->name
);
2605 dump_constraint (dump_file
, c
);
2606 fprintf (dump_file
, "\n");
2608 constraints
[i
] = NULL
;
2612 lhsvar
= find_equivalent_node (graph
, lhsvar
, lhslabel
);
2613 rhsvar
= find_equivalent_node (graph
, rhsvar
, rhslabel
);
2614 c
->lhs
.var
= lhsvar
;
2615 c
->rhs
.var
= rhsvar
;
2619 /* Eliminate indirect cycles involving NODE. Return true if NODE was
2620 part of an SCC, false otherwise. */
2623 eliminate_indirect_cycles (unsigned int node
)
2625 if (graph
->indirect_cycles
[node
] != -1
2626 && !bitmap_empty_p (get_varinfo (node
)->solution
))
2629 auto_vec
<unsigned> queue
;
2631 unsigned int to
= find (graph
->indirect_cycles
[node
]);
2634 /* We can't touch the solution set and call unify_nodes
2635 at the same time, because unify_nodes is going to do
2636 bitmap unions into it. */
2638 EXECUTE_IF_SET_IN_BITMAP (get_varinfo (node
)->solution
, 0, i
, bi
)
2640 if (find (i
) == i
&& i
!= to
)
2643 queue
.safe_push (i
);
2648 queue
.iterate (queuepos
, &i
);
2651 unify_nodes (graph
, to
, i
, true);
2658 /* Solve the constraint graph GRAPH using our worklist solver.
2659 This is based on the PW* family of solvers from the "Efficient Field
2660 Sensitive Pointer Analysis for C" paper.
2661 It works by iterating over all the graph nodes, processing the complex
2662 constraints and propagating the copy constraints, until everything stops
2663 changed. This corresponds to steps 6-8 in the solving list given above. */
2666 solve_graph (constraint_graph_t graph
)
2668 unsigned int size
= graph
->size
;
2672 changed
= BITMAP_ALLOC (NULL
);
2674 /* Mark all initial non-collapsed nodes as changed. */
2675 for (i
= 1; i
< size
; i
++)
2677 varinfo_t ivi
= get_varinfo (i
);
2678 if (find (i
) == i
&& !bitmap_empty_p (ivi
->solution
)
2679 && ((graph
->succs
[i
] && !bitmap_empty_p (graph
->succs
[i
]))
2680 || graph
->complex[i
].length () > 0))
2681 bitmap_set_bit (changed
, i
);
2684 /* Allocate a bitmap to be used to store the changed bits. */
2685 pts
= BITMAP_ALLOC (&pta_obstack
);
2687 while (!bitmap_empty_p (changed
))
2690 struct topo_info
*ti
= init_topo_info ();
2693 bitmap_obstack_initialize (&iteration_obstack
);
2695 compute_topo_order (graph
, ti
);
2697 while (ti
->topo_order
.length () != 0)
2700 i
= ti
->topo_order
.pop ();
2702 /* If this variable is not a representative, skip it. */
2706 /* In certain indirect cycle cases, we may merge this
2707 variable to another. */
2708 if (eliminate_indirect_cycles (i
) && find (i
) != i
)
2711 /* If the node has changed, we need to process the
2712 complex constraints and outgoing edges again. */
2713 if (bitmap_clear_bit (changed
, i
))
2718 vec
<constraint_t
> complex = graph
->complex[i
];
2719 varinfo_t vi
= get_varinfo (i
);
2720 bool solution_empty
;
2722 /* Compute the changed set of solution bits. If anything
2723 is in the solution just propagate that. */
2724 if (bitmap_bit_p (vi
->solution
, anything_id
))
2726 /* If anything is also in the old solution there is
2728 ??? But we shouldn't ended up with "changed" set ... */
2730 && bitmap_bit_p (vi
->oldsolution
, anything_id
))
2732 bitmap_copy (pts
, get_varinfo (find (anything_id
))->solution
);
2734 else if (vi
->oldsolution
)
2735 bitmap_and_compl (pts
, vi
->solution
, vi
->oldsolution
);
2737 bitmap_copy (pts
, vi
->solution
);
2739 if (bitmap_empty_p (pts
))
2742 if (vi
->oldsolution
)
2743 bitmap_ior_into (vi
->oldsolution
, pts
);
2746 vi
->oldsolution
= BITMAP_ALLOC (&oldpta_obstack
);
2747 bitmap_copy (vi
->oldsolution
, pts
);
2750 solution
= vi
->solution
;
2751 solution_empty
= bitmap_empty_p (solution
);
2753 /* Process the complex constraints */
2754 bitmap expanded_pts
= NULL
;
2755 FOR_EACH_VEC_ELT (complex, j
, c
)
2757 /* XXX: This is going to unsort the constraints in
2758 some cases, which will occasionally add duplicate
2759 constraints during unification. This does not
2760 affect correctness. */
2761 c
->lhs
.var
= find (c
->lhs
.var
);
2762 c
->rhs
.var
= find (c
->rhs
.var
);
2764 /* The only complex constraint that can change our
2765 solution to non-empty, given an empty solution,
2766 is a constraint where the lhs side is receiving
2767 some set from elsewhere. */
2768 if (!solution_empty
|| c
->lhs
.type
!= DEREF
)
2769 do_complex_constraint (graph
, c
, pts
, &expanded_pts
);
2771 BITMAP_FREE (expanded_pts
);
2773 solution_empty
= bitmap_empty_p (solution
);
2775 if (!solution_empty
)
2778 unsigned eff_escaped_id
= find (escaped_id
);
2780 /* Propagate solution to all successors. */
2781 EXECUTE_IF_IN_NONNULL_BITMAP (graph
->succs
[i
],
2787 unsigned int to
= find (j
);
2788 tmp
= get_varinfo (to
)->solution
;
2791 /* Don't try to propagate to ourselves. */
2795 /* If we propagate from ESCAPED use ESCAPED as
2797 if (i
== eff_escaped_id
)
2798 flag
= bitmap_set_bit (tmp
, escaped_id
);
2800 flag
= bitmap_ior_into (tmp
, pts
);
2803 bitmap_set_bit (changed
, to
);
2808 free_topo_info (ti
);
2809 bitmap_obstack_release (&iteration_obstack
);
2813 BITMAP_FREE (changed
);
2814 bitmap_obstack_release (&oldpta_obstack
);
2817 /* Map from trees to variable infos. */
2818 static hash_map
<tree
, varinfo_t
> *vi_for_tree
;
2821 /* Insert ID as the variable id for tree T in the vi_for_tree map. */
2824 insert_vi_for_tree (tree t
, varinfo_t vi
)
2827 gcc_assert (!vi_for_tree
->put (t
, vi
));
2830 /* Find the variable info for tree T in VI_FOR_TREE. If T does not
2831 exist in the map, return NULL, otherwise, return the varinfo we found. */
2834 lookup_vi_for_tree (tree t
)
2836 varinfo_t
*slot
= vi_for_tree
->get (t
);
2843 /* Return a printable name for DECL */
2846 alias_get_name (tree decl
)
2848 const char *res
= NULL
;
2850 int num_printed
= 0;
2855 if (TREE_CODE (decl
) == SSA_NAME
)
2857 res
= get_name (decl
);
2859 num_printed
= asprintf (&temp
, "%s_%u", res
, SSA_NAME_VERSION (decl
));
2861 num_printed
= asprintf (&temp
, "_%u", SSA_NAME_VERSION (decl
));
2862 if (num_printed
> 0)
2864 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 num_printed
= asprintf (&temp
, "D.%u", DECL_UID (decl
));
2878 if (num_printed
> 0)
2880 res
= ggc_strdup (temp
);
2892 /* Find the variable id for tree T in the map.
2893 If T doesn't exist in the map, create an entry for it and return it. */
2896 get_vi_for_tree (tree t
)
2898 varinfo_t
*slot
= vi_for_tree
->get (t
);
2900 return get_varinfo (create_variable_info_for (t
, alias_get_name (t
)));
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
)
2910 struct constraint_expr tmp
;
2913 vi
= new_var_info (NULL_TREE
, name
);
2917 vi
->is_full_var
= 1;
2926 /* Get a constraint expression vector from an SSA_VAR_P node.
2927 If address_p is true, the result will be taken its address of. */
2930 get_constraint_for_ssa_var (tree t
, vec
<ce_s
> *results
, bool address_p
)
2932 struct constraint_expr cexpr
;
2935 /* We allow FUNCTION_DECLs here even though it doesn't make much sense. */
2936 gcc_assert (TREE_CODE (t
) == SSA_NAME
|| DECL_P (t
));
2938 /* For parameters, get at the points-to set for the actual parm
2940 if (TREE_CODE (t
) == SSA_NAME
2941 && SSA_NAME_IS_DEFAULT_DEF (t
)
2942 && (TREE_CODE (SSA_NAME_VAR (t
)) == PARM_DECL
2943 || TREE_CODE (SSA_NAME_VAR (t
)) == RESULT_DECL
))
2945 get_constraint_for_ssa_var (SSA_NAME_VAR (t
), results
, address_p
);
2949 /* For global variables resort to the alias target. */
2950 if (TREE_CODE (t
) == VAR_DECL
2951 && (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 ();
2961 vi
= get_vi_for_tree (t
);
2963 cexpr
.type
= SCALAR
;
2966 /* If we are not taking the address of the constraint expr, add all
2967 sub-fiels of the variable as well. */
2969 && !vi
->is_full_var
)
2971 for (; vi
; vi
= vi_next (vi
))
2974 results
->safe_push (cexpr
);
2979 results
->safe_push (cexpr
);
2982 /* Process constraint T, performing various simplifications and then
2983 adding it to our list of overall constraints. */
2986 process_constraint (constraint_t t
)
2988 struct constraint_expr rhs
= t
->rhs
;
2989 struct constraint_expr lhs
= t
->lhs
;
2991 gcc_assert (rhs
.var
< varmap
.length ());
2992 gcc_assert (lhs
.var
< varmap
.length ());
2994 /* If we didn't get any useful constraint from the lhs we get
2995 &ANYTHING as fallback from get_constraint_for. Deal with
2996 it here by turning it into *ANYTHING. */
2997 if (lhs
.type
== ADDRESSOF
2998 && lhs
.var
== anything_id
)
3001 /* ADDRESSOF on the lhs is invalid. */
3002 gcc_assert (lhs
.type
!= ADDRESSOF
);
3004 /* We shouldn't add constraints from things that cannot have pointers.
3005 It's not completely trivial to avoid in the callers, so do it here. */
3006 if (rhs
.type
!= ADDRESSOF
3007 && !get_varinfo (rhs
.var
)->may_have_pointers
)
3010 /* Likewise adding to the solution of a non-pointer var isn't useful. */
3011 if (!get_varinfo (lhs
.var
)->may_have_pointers
)
3014 /* This can happen in our IR with things like n->a = *p */
3015 if (rhs
.type
== DEREF
&& lhs
.type
== DEREF
&& rhs
.var
!= anything_id
)
3017 /* Split into tmp = *rhs, *lhs = tmp */
3018 struct constraint_expr tmplhs
;
3019 tmplhs
= new_scalar_tmp_constraint_exp ("doubledereftmp");
3020 process_constraint (new_constraint (tmplhs
, rhs
));
3021 process_constraint (new_constraint (lhs
, tmplhs
));
3023 else if (rhs
.type
== ADDRESSOF
&& lhs
.type
== DEREF
)
3025 /* Split into tmp = &rhs, *lhs = tmp */
3026 struct constraint_expr tmplhs
;
3027 tmplhs
= new_scalar_tmp_constraint_exp ("derefaddrtmp");
3028 process_constraint (new_constraint (tmplhs
, rhs
));
3029 process_constraint (new_constraint (lhs
, tmplhs
));
3033 gcc_assert (rhs
.type
!= ADDRESSOF
|| rhs
.offset
== 0);
3034 constraints
.safe_push (t
);
3039 /* Return the position, in bits, of FIELD_DECL from the beginning of its
3042 static HOST_WIDE_INT
3043 bitpos_of_field (const tree fdecl
)
3045 if (!tree_fits_shwi_p (DECL_FIELD_OFFSET (fdecl
))
3046 || !tree_fits_shwi_p (DECL_FIELD_BIT_OFFSET (fdecl
)))
3049 return (tree_to_shwi (DECL_FIELD_OFFSET (fdecl
)) * BITS_PER_UNIT
3050 + tree_to_shwi (DECL_FIELD_BIT_OFFSET (fdecl
)));
3054 /* Get constraint expressions for offsetting PTR by OFFSET. Stores the
3055 resulting constraint expressions in *RESULTS. */
3058 get_constraint_for_ptr_offset (tree ptr
, tree offset
,
3061 struct constraint_expr c
;
3063 HOST_WIDE_INT rhsoffset
;
3065 /* If we do not do field-sensitive PTA adding offsets to pointers
3066 does not change the points-to solution. */
3067 if (!use_field_sensitive
)
3069 get_constraint_for_rhs (ptr
, results
);
3073 /* If the offset is not a non-negative integer constant that fits
3074 in a HOST_WIDE_INT, we have to fall back to a conservative
3075 solution which includes all sub-fields of all pointed-to
3076 variables of ptr. */
3077 if (offset
== NULL_TREE
3078 || TREE_CODE (offset
) != INTEGER_CST
)
3079 rhsoffset
= UNKNOWN_OFFSET
;
3082 /* Sign-extend the offset. */
3083 offset_int soffset
= offset_int::from (offset
, SIGNED
);
3084 if (!wi::fits_shwi_p (soffset
))
3085 rhsoffset
= UNKNOWN_OFFSET
;
3088 /* Make sure the bit-offset also fits. */
3089 HOST_WIDE_INT rhsunitoffset
= soffset
.to_shwi ();
3090 rhsoffset
= rhsunitoffset
* BITS_PER_UNIT
;
3091 if (rhsunitoffset
!= rhsoffset
/ BITS_PER_UNIT
)
3092 rhsoffset
= UNKNOWN_OFFSET
;
3096 get_constraint_for_rhs (ptr
, results
);
3100 /* As we are eventually appending to the solution do not use
3101 vec::iterate here. */
3102 n
= results
->length ();
3103 for (j
= 0; j
< n
; j
++)
3107 curr
= get_varinfo (c
.var
);
3109 if (c
.type
== ADDRESSOF
3110 /* If this varinfo represents a full variable just use it. */
3111 && curr
->is_full_var
)
3113 else if (c
.type
== ADDRESSOF
3114 /* If we do not know the offset add all subfields. */
3115 && rhsoffset
== UNKNOWN_OFFSET
)
3117 varinfo_t temp
= get_varinfo (curr
->head
);
3120 struct constraint_expr c2
;
3122 c2
.type
= ADDRESSOF
;
3124 if (c2
.var
!= c
.var
)
3125 results
->safe_push (c2
);
3126 temp
= vi_next (temp
);
3130 else if (c
.type
== ADDRESSOF
)
3133 unsigned HOST_WIDE_INT offset
= curr
->offset
+ rhsoffset
;
3135 /* If curr->offset + rhsoffset is less than zero adjust it. */
3137 && curr
->offset
< offset
)
3140 /* We have to include all fields that overlap the current
3141 field shifted by rhsoffset. And we include at least
3142 the last or the first field of the variable to represent
3143 reachability of off-bound addresses, in particular &object + 1,
3144 conservatively correct. */
3145 temp
= first_or_preceding_vi_for_offset (curr
, offset
);
3148 temp
= vi_next (temp
);
3150 && temp
->offset
< offset
+ curr
->size
)
3152 struct constraint_expr c2
;
3154 c2
.type
= ADDRESSOF
;
3156 results
->safe_push (c2
);
3157 temp
= vi_next (temp
);
3160 else if (c
.type
== SCALAR
)
3162 gcc_assert (c
.offset
== 0);
3163 c
.offset
= rhsoffset
;
3166 /* We shouldn't get any DEREFs here. */
3174 /* Given a COMPONENT_REF T, return the constraint_expr vector for it.
3175 If address_p is true the result will be taken its address of.
3176 If lhs_p is true then the constraint expression is assumed to be used
3180 get_constraint_for_component_ref (tree t
, vec
<ce_s
> *results
,
3181 bool address_p
, bool lhs_p
)
3184 HOST_WIDE_INT bitsize
= -1;
3185 HOST_WIDE_INT bitmaxsize
= -1;
3186 HOST_WIDE_INT bitpos
;
3189 /* Some people like to do cute things like take the address of
3192 while (handled_component_p (forzero
)
3193 || INDIRECT_REF_P (forzero
)
3194 || TREE_CODE (forzero
) == MEM_REF
)
3195 forzero
= TREE_OPERAND (forzero
, 0);
3197 if (CONSTANT_CLASS_P (forzero
) && integer_zerop (forzero
))
3199 struct constraint_expr temp
;
3202 temp
.var
= integer_id
;
3204 results
->safe_push (temp
);
3208 t
= get_ref_base_and_extent (t
, &bitpos
, &bitsize
, &bitmaxsize
);
3210 /* Pretend to take the address of the base, we'll take care of
3211 adding the required subset of sub-fields below. */
3212 get_constraint_for_1 (t
, results
, true, lhs_p
);
3213 gcc_assert (results
->length () == 1);
3214 struct constraint_expr
&result
= results
->last ();
3216 if (result
.type
== SCALAR
3217 && get_varinfo (result
.var
)->is_full_var
)
3218 /* For single-field vars do not bother about the offset. */
3220 else if (result
.type
== SCALAR
)
3222 /* In languages like C, you can access one past the end of an
3223 array. You aren't allowed to dereference it, so we can
3224 ignore this constraint. When we handle pointer subtraction,
3225 we may have to do something cute here. */
3227 if ((unsigned HOST_WIDE_INT
)bitpos
< get_varinfo (result
.var
)->fullsize
3230 /* It's also not true that the constraint will actually start at the
3231 right offset, it may start in some padding. We only care about
3232 setting the constraint to the first actual field it touches, so
3234 struct constraint_expr cexpr
= result
;
3238 for (curr
= get_varinfo (cexpr
.var
); curr
; curr
= vi_next (curr
))
3240 if (ranges_overlap_p (curr
->offset
, curr
->size
,
3241 bitpos
, bitmaxsize
))
3243 cexpr
.var
= curr
->id
;
3244 results
->safe_push (cexpr
);
3249 /* If we are going to take the address of this field then
3250 to be able to compute reachability correctly add at least
3251 the last field of the variable. */
3252 if (address_p
&& results
->length () == 0)
3254 curr
= get_varinfo (cexpr
.var
);
3255 while (curr
->next
!= 0)
3256 curr
= vi_next (curr
);
3257 cexpr
.var
= curr
->id
;
3258 results
->safe_push (cexpr
);
3260 else if (results
->length () == 0)
3261 /* Assert that we found *some* field there. The user couldn't be
3262 accessing *only* padding. */
3263 /* Still the user could access one past the end of an array
3264 embedded in a struct resulting in accessing *only* padding. */
3265 /* Or accessing only padding via type-punning to a type
3266 that has a filed just in padding space. */
3268 cexpr
.type
= SCALAR
;
3269 cexpr
.var
= anything_id
;
3271 results
->safe_push (cexpr
);
3274 else if (bitmaxsize
== 0)
3276 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3277 fprintf (dump_file
, "Access to zero-sized part of variable,"
3281 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3282 fprintf (dump_file
, "Access to past the end of variable, ignoring\n");
3284 else if (result
.type
== DEREF
)
3286 /* If we do not know exactly where the access goes say so. Note
3287 that only for non-structure accesses we know that we access
3288 at most one subfiled of any variable. */
3290 || bitsize
!= bitmaxsize
3291 || AGGREGATE_TYPE_P (TREE_TYPE (orig_t
))
3292 || result
.offset
== UNKNOWN_OFFSET
)
3293 result
.offset
= UNKNOWN_OFFSET
;
3295 result
.offset
+= bitpos
;
3297 else if (result
.type
== ADDRESSOF
)
3299 /* We can end up here for component references on a
3300 VIEW_CONVERT_EXPR <>(&foobar). */
3301 result
.type
= SCALAR
;
3302 result
.var
= anything_id
;
3310 /* Dereference the constraint expression CONS, and return the result.
3311 DEREF (ADDRESSOF) = SCALAR
3312 DEREF (SCALAR) = DEREF
3313 DEREF (DEREF) = (temp = DEREF1; result = DEREF(temp))
3314 This is needed so that we can handle dereferencing DEREF constraints. */
3317 do_deref (vec
<ce_s
> *constraints
)
3319 struct constraint_expr
*c
;
3322 FOR_EACH_VEC_ELT (*constraints
, i
, c
)
3324 if (c
->type
== SCALAR
)
3326 else if (c
->type
== ADDRESSOF
)
3328 else if (c
->type
== DEREF
)
3330 struct constraint_expr tmplhs
;
3331 tmplhs
= new_scalar_tmp_constraint_exp ("dereftmp");
3332 process_constraint (new_constraint (tmplhs
, *c
));
3333 c
->var
= tmplhs
.var
;
3340 /* Given a tree T, return the constraint expression for taking the
3344 get_constraint_for_address_of (tree t
, vec
<ce_s
> *results
)
3346 struct constraint_expr
*c
;
3349 get_constraint_for_1 (t
, results
, true, true);
3351 FOR_EACH_VEC_ELT (*results
, i
, c
)
3353 if (c
->type
== DEREF
)
3356 c
->type
= ADDRESSOF
;
3360 /* Given a tree T, return the constraint expression for it. */
3363 get_constraint_for_1 (tree t
, vec
<ce_s
> *results
, bool address_p
,
3366 struct constraint_expr temp
;
3368 /* x = integer is all glommed to a single variable, which doesn't
3369 point to anything by itself. That is, of course, unless it is an
3370 integer constant being treated as a pointer, in which case, we
3371 will return that this is really the addressof anything. This
3372 happens below, since it will fall into the default case. The only
3373 case we know something about an integer treated like a pointer is
3374 when it is the NULL pointer, and then we just say it points to
3377 Do not do that if -fno-delete-null-pointer-checks though, because
3378 in that case *NULL does not fail, so it _should_ alias *anything.
3379 It is not worth adding a new option or renaming the existing one,
3380 since this case is relatively obscure. */
3381 if ((TREE_CODE (t
) == INTEGER_CST
3382 && integer_zerop (t
))
3383 /* The only valid CONSTRUCTORs in gimple with pointer typed
3384 elements are zero-initializer. But in IPA mode we also
3385 process global initializers, so verify at least. */
3386 || (TREE_CODE (t
) == CONSTRUCTOR
3387 && CONSTRUCTOR_NELTS (t
) == 0))
3389 if (flag_delete_null_pointer_checks
)
3390 temp
.var
= nothing_id
;
3392 temp
.var
= nonlocal_id
;
3393 temp
.type
= ADDRESSOF
;
3395 results
->safe_push (temp
);
3399 /* String constants are read-only, ideally we'd have a CONST_DECL
3401 if (TREE_CODE (t
) == STRING_CST
)
3403 temp
.var
= string_id
;
3406 results
->safe_push (temp
);
3410 switch (TREE_CODE_CLASS (TREE_CODE (t
)))
3412 case tcc_expression
:
3414 switch (TREE_CODE (t
))
3417 get_constraint_for_address_of (TREE_OPERAND (t
, 0), results
);
3425 switch (TREE_CODE (t
))
3429 struct constraint_expr cs
;
3431 get_constraint_for_ptr_offset (TREE_OPERAND (t
, 0),
3432 TREE_OPERAND (t
, 1), results
);
3435 /* If we are not taking the address then make sure to process
3436 all subvariables we might access. */
3440 cs
= results
->last ();
3441 if (cs
.type
== DEREF
3442 && type_can_have_subvars (TREE_TYPE (t
)))
3444 /* For dereferences this means we have to defer it
3446 results
->last ().offset
= UNKNOWN_OFFSET
;
3449 if (cs
.type
!= SCALAR
)
3452 vi
= get_varinfo (cs
.var
);
3453 curr
= vi_next (vi
);
3454 if (!vi
->is_full_var
3457 unsigned HOST_WIDE_INT size
;
3458 if (tree_fits_uhwi_p (TYPE_SIZE (TREE_TYPE (t
))))
3459 size
= tree_to_uhwi (TYPE_SIZE (TREE_TYPE (t
)));
3462 for (; curr
; curr
= vi_next (curr
))
3464 if (curr
->offset
- vi
->offset
< size
)
3467 results
->safe_push (cs
);
3476 case ARRAY_RANGE_REF
:
3478 get_constraint_for_component_ref (t
, results
, address_p
, lhs_p
);
3480 case VIEW_CONVERT_EXPR
:
3481 get_constraint_for_1 (TREE_OPERAND (t
, 0), results
, address_p
,
3484 /* We are missing handling for TARGET_MEM_REF here. */
3489 case tcc_exceptional
:
3491 switch (TREE_CODE (t
))
3495 get_constraint_for_ssa_var (t
, results
, address_p
);
3503 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t
), i
, val
)
3505 struct constraint_expr
*rhsp
;
3507 get_constraint_for_1 (val
, &tmp
, address_p
, lhs_p
);
3508 FOR_EACH_VEC_ELT (tmp
, j
, rhsp
)
3509 results
->safe_push (*rhsp
);
3512 /* We do not know whether the constructor was complete,
3513 so technically we have to add &NOTHING or &ANYTHING
3514 like we do for an empty constructor as well. */
3521 case tcc_declaration
:
3523 get_constraint_for_ssa_var (t
, results
, address_p
);
3528 /* We cannot refer to automatic variables through constants. */
3529 temp
.type
= ADDRESSOF
;
3530 temp
.var
= nonlocal_id
;
3532 results
->safe_push (temp
);
3538 /* The default fallback is a constraint from anything. */
3539 temp
.type
= ADDRESSOF
;
3540 temp
.var
= anything_id
;
3542 results
->safe_push (temp
);
3545 /* Given a gimple tree T, return the constraint expression vector for it. */
3548 get_constraint_for (tree t
, vec
<ce_s
> *results
)
3550 gcc_assert (results
->length () == 0);
3552 get_constraint_for_1 (t
, results
, false, true);
3555 /* Given a gimple tree T, return the constraint expression vector for it
3556 to be used as the rhs of a constraint. */
3559 get_constraint_for_rhs (tree t
, vec
<ce_s
> *results
)
3561 gcc_assert (results
->length () == 0);
3563 get_constraint_for_1 (t
, results
, false, false);
3567 /* Efficiently generates constraints from all entries in *RHSC to all
3568 entries in *LHSC. */
3571 process_all_all_constraints (vec
<ce_s
> lhsc
,
3574 struct constraint_expr
*lhsp
, *rhsp
;
3577 if (lhsc
.length () <= 1 || rhsc
.length () <= 1)
3579 FOR_EACH_VEC_ELT (lhsc
, i
, lhsp
)
3580 FOR_EACH_VEC_ELT (rhsc
, j
, rhsp
)
3581 process_constraint (new_constraint (*lhsp
, *rhsp
));
3585 struct constraint_expr tmp
;
3586 tmp
= new_scalar_tmp_constraint_exp ("allalltmp");
3587 FOR_EACH_VEC_ELT (rhsc
, i
, rhsp
)
3588 process_constraint (new_constraint (tmp
, *rhsp
));
3589 FOR_EACH_VEC_ELT (lhsc
, i
, lhsp
)
3590 process_constraint (new_constraint (*lhsp
, tmp
));
3594 /* Handle aggregate copies by expanding into copies of the respective
3595 fields of the structures. */
3598 do_structure_copy (tree lhsop
, tree rhsop
)
3600 struct constraint_expr
*lhsp
, *rhsp
;
3601 auto_vec
<ce_s
> lhsc
;
3602 auto_vec
<ce_s
> rhsc
;
3605 get_constraint_for (lhsop
, &lhsc
);
3606 get_constraint_for_rhs (rhsop
, &rhsc
);
3609 if (lhsp
->type
== DEREF
3610 || (lhsp
->type
== ADDRESSOF
&& lhsp
->var
== anything_id
)
3611 || rhsp
->type
== DEREF
)
3613 if (lhsp
->type
== DEREF
)
3615 gcc_assert (lhsc
.length () == 1);
3616 lhsp
->offset
= UNKNOWN_OFFSET
;
3618 if (rhsp
->type
== DEREF
)
3620 gcc_assert (rhsc
.length () == 1);
3621 rhsp
->offset
= UNKNOWN_OFFSET
;
3623 process_all_all_constraints (lhsc
, rhsc
);
3625 else if (lhsp
->type
== SCALAR
3626 && (rhsp
->type
== SCALAR
3627 || rhsp
->type
== ADDRESSOF
))
3629 HOST_WIDE_INT lhssize
, lhsmaxsize
, lhsoffset
;
3630 HOST_WIDE_INT rhssize
, rhsmaxsize
, rhsoffset
;
3632 get_ref_base_and_extent (lhsop
, &lhsoffset
, &lhssize
, &lhsmaxsize
);
3633 get_ref_base_and_extent (rhsop
, &rhsoffset
, &rhssize
, &rhsmaxsize
);
3634 for (j
= 0; lhsc
.iterate (j
, &lhsp
);)
3636 varinfo_t lhsv
, rhsv
;
3638 lhsv
= get_varinfo (lhsp
->var
);
3639 rhsv
= get_varinfo (rhsp
->var
);
3640 if (lhsv
->may_have_pointers
3641 && (lhsv
->is_full_var
3642 || rhsv
->is_full_var
3643 || ranges_overlap_p (lhsv
->offset
+ rhsoffset
, lhsv
->size
,
3644 rhsv
->offset
+ lhsoffset
, rhsv
->size
)))
3645 process_constraint (new_constraint (*lhsp
, *rhsp
));
3646 if (!rhsv
->is_full_var
3647 && (lhsv
->is_full_var
3648 || (lhsv
->offset
+ rhsoffset
+ lhsv
->size
3649 > rhsv
->offset
+ lhsoffset
+ rhsv
->size
)))
3652 if (k
>= rhsc
.length ())
3663 /* Create constraints ID = { rhsc }. */
3666 make_constraints_to (unsigned id
, vec
<ce_s
> rhsc
)
3668 struct constraint_expr
*c
;
3669 struct constraint_expr includes
;
3673 includes
.offset
= 0;
3674 includes
.type
= SCALAR
;
3676 FOR_EACH_VEC_ELT (rhsc
, j
, c
)
3677 process_constraint (new_constraint (includes
, *c
));
3680 /* Create a constraint ID = OP. */
3683 make_constraint_to (unsigned id
, tree op
)
3685 auto_vec
<ce_s
> rhsc
;
3686 get_constraint_for_rhs (op
, &rhsc
);
3687 make_constraints_to (id
, rhsc
);
3690 /* Create a constraint ID = &FROM. */
3693 make_constraint_from (varinfo_t vi
, int from
)
3695 struct constraint_expr lhs
, rhs
;
3703 rhs
.type
= ADDRESSOF
;
3704 process_constraint (new_constraint (lhs
, rhs
));
3707 /* Create a constraint ID = FROM. */
3710 make_copy_constraint (varinfo_t vi
, int from
)
3712 struct constraint_expr lhs
, rhs
;
3721 process_constraint (new_constraint (lhs
, rhs
));
3724 /* Make constraints necessary to make OP escape. */
3727 make_escape_constraint (tree op
)
3729 make_constraint_to (escaped_id
, op
);
3732 /* Add constraints to that the solution of VI is transitively closed. */
3735 make_transitive_closure_constraints (varinfo_t vi
)
3737 struct constraint_expr lhs
, rhs
;
3745 rhs
.offset
= UNKNOWN_OFFSET
;
3746 process_constraint (new_constraint (lhs
, rhs
));
3749 /* Temporary storage for fake var decls. */
3750 struct obstack fake_var_decl_obstack
;
3752 /* Build a fake VAR_DECL acting as referrer to a DECL_UID. */
3755 build_fake_var_decl (tree type
)
3757 tree decl
= (tree
) XOBNEW (&fake_var_decl_obstack
, struct tree_var_decl
);
3758 memset (decl
, 0, sizeof (struct tree_var_decl
));
3759 TREE_SET_CODE (decl
, VAR_DECL
);
3760 TREE_TYPE (decl
) = type
;
3761 DECL_UID (decl
) = allocate_decl_uid ();
3762 SET_DECL_PT_UID (decl
, -1);
3763 layout_decl (decl
, 0);
3767 /* Create a new artificial heap variable with NAME.
3768 Return the created variable. */
3771 make_heapvar (const char *name
)
3776 heapvar
= build_fake_var_decl (ptr_type_node
);
3777 DECL_EXTERNAL (heapvar
) = 1;
3779 vi
= new_var_info (heapvar
, name
);
3780 vi
->is_artificial_var
= true;
3781 vi
->is_heap_var
= true;
3782 vi
->is_unknown_size_var
= true;
3786 vi
->is_full_var
= true;
3787 insert_vi_for_tree (heapvar
, vi
);
3792 /* Create a new artificial heap variable with NAME and make a
3793 constraint from it to LHS. Set flags according to a tag used
3794 for tracking restrict pointers. */
3797 make_constraint_from_restrict (varinfo_t lhs
, const char *name
)
3799 varinfo_t vi
= make_heapvar (name
);
3800 vi
->is_restrict_var
= 1;
3801 vi
->is_global_var
= 1;
3802 vi
->may_have_pointers
= 1;
3803 make_constraint_from (lhs
, vi
->id
);
3807 /* Create a new artificial heap variable with NAME and make a
3808 constraint from it to LHS. Set flags according to a tag used
3809 for tracking restrict pointers and make the artificial heap
3810 point to global memory. */
3813 make_constraint_from_global_restrict (varinfo_t lhs
, const char *name
)
3815 varinfo_t vi
= make_constraint_from_restrict (lhs
, name
);
3816 make_copy_constraint (vi
, nonlocal_id
);
3820 /* In IPA mode there are varinfos for different aspects of reach
3821 function designator. One for the points-to set of the return
3822 value, one for the variables that are clobbered by the function,
3823 one for its uses and one for each parameter (including a single
3824 glob for remaining variadic arguments). */
3826 enum { fi_clobbers
= 1, fi_uses
= 2,
3827 fi_static_chain
= 3, fi_result
= 4, fi_parm_base
= 5 };
3829 /* Get a constraint for the requested part of a function designator FI
3830 when operating in IPA mode. */
3832 static struct constraint_expr
3833 get_function_part_constraint (varinfo_t fi
, unsigned part
)
3835 struct constraint_expr c
;
3837 gcc_assert (in_ipa_mode
);
3839 if (fi
->id
== anything_id
)
3841 /* ??? We probably should have a ANYFN special variable. */
3842 c
.var
= anything_id
;
3846 else if (TREE_CODE (fi
->decl
) == FUNCTION_DECL
)
3848 varinfo_t ai
= first_vi_for_offset (fi
, part
);
3852 c
.var
= anything_id
;
3866 /* For non-IPA mode, generate constraints necessary for a call on the
3870 handle_rhs_call (gcall
*stmt
, vec
<ce_s
> *results
)
3872 struct constraint_expr rhsc
;
3874 bool returns_uses
= false;
3876 for (i
= 0; i
< gimple_call_num_args (stmt
); ++i
)
3878 tree arg
= gimple_call_arg (stmt
, i
);
3879 int flags
= gimple_call_arg_flags (stmt
, i
);
3881 /* If the argument is not used we can ignore it. */
3882 if (flags
& EAF_UNUSED
)
3885 /* As we compute ESCAPED context-insensitive we do not gain
3886 any precision with just EAF_NOCLOBBER but not EAF_NOESCAPE
3887 set. The argument would still get clobbered through the
3889 if ((flags
& EAF_NOCLOBBER
)
3890 && (flags
& EAF_NOESCAPE
))
3892 varinfo_t uses
= get_call_use_vi (stmt
);
3893 if (!(flags
& EAF_DIRECT
))
3895 varinfo_t tem
= new_var_info (NULL_TREE
, "callarg");
3896 make_constraint_to (tem
->id
, arg
);
3897 make_transitive_closure_constraints (tem
);
3898 make_copy_constraint (uses
, tem
->id
);
3901 make_constraint_to (uses
->id
, arg
);
3902 returns_uses
= true;
3904 else if (flags
& EAF_NOESCAPE
)
3906 struct constraint_expr lhs
, rhs
;
3907 varinfo_t uses
= get_call_use_vi (stmt
);
3908 varinfo_t clobbers
= get_call_clobber_vi (stmt
);
3909 varinfo_t tem
= new_var_info (NULL_TREE
, "callarg");
3910 make_constraint_to (tem
->id
, arg
);
3911 if (!(flags
& EAF_DIRECT
))
3912 make_transitive_closure_constraints (tem
);
3913 make_copy_constraint (uses
, tem
->id
);
3914 make_copy_constraint (clobbers
, tem
->id
);
3915 /* Add *tem = nonlocal, do not add *tem = callused as
3916 EAF_NOESCAPE parameters do not escape to other parameters
3917 and all other uses appear in NONLOCAL as well. */
3922 rhs
.var
= nonlocal_id
;
3924 process_constraint (new_constraint (lhs
, rhs
));
3925 returns_uses
= true;
3928 make_escape_constraint (arg
);
3931 /* If we added to the calls uses solution make sure we account for
3932 pointers to it to be returned. */
3935 rhsc
.var
= get_call_use_vi (stmt
)->id
;
3938 results
->safe_push (rhsc
);
3941 /* The static chain escapes as well. */
3942 if (gimple_call_chain (stmt
))
3943 make_escape_constraint (gimple_call_chain (stmt
));
3945 /* And if we applied NRV the address of the return slot escapes as well. */
3946 if (gimple_call_return_slot_opt_p (stmt
)
3947 && gimple_call_lhs (stmt
) != NULL_TREE
3948 && TREE_ADDRESSABLE (TREE_TYPE (gimple_call_lhs (stmt
))))
3950 auto_vec
<ce_s
> tmpc
;
3951 struct constraint_expr lhsc
, *c
;
3952 get_constraint_for_address_of (gimple_call_lhs (stmt
), &tmpc
);
3953 lhsc
.var
= escaped_id
;
3956 FOR_EACH_VEC_ELT (tmpc
, i
, c
)
3957 process_constraint (new_constraint (lhsc
, *c
));
3960 /* Regular functions return nonlocal memory. */
3961 rhsc
.var
= nonlocal_id
;
3964 results
->safe_push (rhsc
);
3967 /* For non-IPA mode, generate constraints necessary for a call
3968 that returns a pointer and assigns it to LHS. This simply makes
3969 the LHS point to global and escaped variables. */
3972 handle_lhs_call (gcall
*stmt
, tree lhs
, int flags
, vec
<ce_s
> rhsc
,
3975 auto_vec
<ce_s
> lhsc
;
3977 get_constraint_for (lhs
, &lhsc
);
3978 /* If the store is to a global decl make sure to
3979 add proper escape constraints. */
3980 lhs
= get_base_address (lhs
);
3983 && is_global_var (lhs
))
3985 struct constraint_expr tmpc
;
3986 tmpc
.var
= escaped_id
;
3989 lhsc
.safe_push (tmpc
);
3992 /* If the call returns an argument unmodified override the rhs
3994 if (flags
& ERF_RETURNS_ARG
3995 && (flags
& ERF_RETURN_ARG_MASK
) < gimple_call_num_args (stmt
))
3999 arg
= gimple_call_arg (stmt
, flags
& ERF_RETURN_ARG_MASK
);
4000 get_constraint_for (arg
, &rhsc
);
4001 process_all_all_constraints (lhsc
, rhsc
);
4004 else if (flags
& ERF_NOALIAS
)
4007 struct constraint_expr tmpc
;
4009 vi
= make_heapvar ("HEAP");
4010 /* We are marking allocated storage local, we deal with it becoming
4011 global by escaping and setting of vars_contains_escaped_heap. */
4012 DECL_EXTERNAL (vi
->decl
) = 0;
4013 vi
->is_global_var
= 0;
4014 /* If this is not a real malloc call assume the memory was
4015 initialized and thus may point to global memory. All
4016 builtin functions with the malloc attribute behave in a sane way. */
4018 || DECL_BUILT_IN_CLASS (fndecl
) != BUILT_IN_NORMAL
)
4019 make_constraint_from (vi
, nonlocal_id
);
4022 tmpc
.type
= ADDRESSOF
;
4023 rhsc
.safe_push (tmpc
);
4024 process_all_all_constraints (lhsc
, rhsc
);
4028 process_all_all_constraints (lhsc
, rhsc
);
4031 /* For non-IPA mode, generate constraints necessary for a call of a
4032 const function that returns a pointer in the statement STMT. */
4035 handle_const_call (gcall
*stmt
, vec
<ce_s
> *results
)
4037 struct constraint_expr rhsc
;
4040 /* Treat nested const functions the same as pure functions as far
4041 as the static chain is concerned. */
4042 if (gimple_call_chain (stmt
))
4044 varinfo_t uses
= get_call_use_vi (stmt
);
4045 make_transitive_closure_constraints (uses
);
4046 make_constraint_to (uses
->id
, gimple_call_chain (stmt
));
4047 rhsc
.var
= uses
->id
;
4050 results
->safe_push (rhsc
);
4053 /* May return arguments. */
4054 for (k
= 0; k
< gimple_call_num_args (stmt
); ++k
)
4056 tree arg
= gimple_call_arg (stmt
, k
);
4057 auto_vec
<ce_s
> argc
;
4059 struct constraint_expr
*argp
;
4060 get_constraint_for_rhs (arg
, &argc
);
4061 FOR_EACH_VEC_ELT (argc
, i
, argp
)
4062 results
->safe_push (*argp
);
4065 /* May return addresses of globals. */
4066 rhsc
.var
= nonlocal_id
;
4068 rhsc
.type
= ADDRESSOF
;
4069 results
->safe_push (rhsc
);
4072 /* For non-IPA mode, generate constraints necessary for a call to a
4073 pure function in statement STMT. */
4076 handle_pure_call (gcall
*stmt
, vec
<ce_s
> *results
)
4078 struct constraint_expr rhsc
;
4080 varinfo_t uses
= NULL
;
4082 /* Memory reached from pointer arguments is call-used. */
4083 for (i
= 0; i
< gimple_call_num_args (stmt
); ++i
)
4085 tree arg
= gimple_call_arg (stmt
, i
);
4088 uses
= get_call_use_vi (stmt
);
4089 make_transitive_closure_constraints (uses
);
4091 make_constraint_to (uses
->id
, arg
);
4094 /* The static chain is used as well. */
4095 if (gimple_call_chain (stmt
))
4099 uses
= get_call_use_vi (stmt
);
4100 make_transitive_closure_constraints (uses
);
4102 make_constraint_to (uses
->id
, gimple_call_chain (stmt
));
4105 /* Pure functions may return call-used and nonlocal memory. */
4108 rhsc
.var
= uses
->id
;
4111 results
->safe_push (rhsc
);
4113 rhsc
.var
= nonlocal_id
;
4116 results
->safe_push (rhsc
);
4120 /* Return the varinfo for the callee of CALL. */
4123 get_fi_for_callee (gcall
*call
)
4125 tree decl
, fn
= gimple_call_fn (call
);
4127 if (fn
&& TREE_CODE (fn
) == OBJ_TYPE_REF
)
4128 fn
= OBJ_TYPE_REF_EXPR (fn
);
4130 /* If we can directly resolve the function being called, do so.
4131 Otherwise, it must be some sort of indirect expression that
4132 we should still be able to handle. */
4133 decl
= gimple_call_addr_fndecl (fn
);
4135 return get_vi_for_tree (decl
);
4137 /* If the function is anything other than a SSA name pointer we have no
4138 clue and should be getting ANYFN (well, ANYTHING for now). */
4139 if (!fn
|| TREE_CODE (fn
) != SSA_NAME
)
4140 return get_varinfo (anything_id
);
4142 if (SSA_NAME_IS_DEFAULT_DEF (fn
)
4143 && (TREE_CODE (SSA_NAME_VAR (fn
)) == PARM_DECL
4144 || TREE_CODE (SSA_NAME_VAR (fn
)) == RESULT_DECL
))
4145 fn
= SSA_NAME_VAR (fn
);
4147 return get_vi_for_tree (fn
);
4150 /* Create constraints for the builtin call T. Return true if the call
4151 was handled, otherwise false. */
4154 find_func_aliases_for_builtin_call (struct function
*fn
, gcall
*t
)
4156 tree fndecl
= gimple_call_fndecl (t
);
4157 auto_vec
<ce_s
, 2> lhsc
;
4158 auto_vec
<ce_s
, 4> rhsc
;
4161 if (gimple_call_builtin_p (t
, BUILT_IN_NORMAL
))
4162 /* ??? All builtins that are handled here need to be handled
4163 in the alias-oracle query functions explicitly! */
4164 switch (DECL_FUNCTION_CODE (fndecl
))
4166 /* All the following functions return a pointer to the same object
4167 as their first argument points to. The functions do not add
4168 to the ESCAPED solution. The functions make the first argument
4169 pointed to memory point to what the second argument pointed to
4170 memory points to. */
4171 case BUILT_IN_STRCPY
:
4172 case BUILT_IN_STRNCPY
:
4173 case BUILT_IN_BCOPY
:
4174 case BUILT_IN_MEMCPY
:
4175 case BUILT_IN_MEMMOVE
:
4176 case BUILT_IN_MEMPCPY
:
4177 case BUILT_IN_STPCPY
:
4178 case BUILT_IN_STPNCPY
:
4179 case BUILT_IN_STRCAT
:
4180 case BUILT_IN_STRNCAT
:
4181 case BUILT_IN_STRCPY_CHK
:
4182 case BUILT_IN_STRNCPY_CHK
:
4183 case BUILT_IN_MEMCPY_CHK
:
4184 case BUILT_IN_MEMMOVE_CHK
:
4185 case BUILT_IN_MEMPCPY_CHK
:
4186 case BUILT_IN_STPCPY_CHK
:
4187 case BUILT_IN_STPNCPY_CHK
:
4188 case BUILT_IN_STRCAT_CHK
:
4189 case BUILT_IN_STRNCAT_CHK
:
4190 case BUILT_IN_TM_MEMCPY
:
4191 case BUILT_IN_TM_MEMMOVE
:
4193 tree res
= gimple_call_lhs (t
);
4194 tree dest
= gimple_call_arg (t
, (DECL_FUNCTION_CODE (fndecl
)
4195 == BUILT_IN_BCOPY
? 1 : 0));
4196 tree src
= gimple_call_arg (t
, (DECL_FUNCTION_CODE (fndecl
)
4197 == BUILT_IN_BCOPY
? 0 : 1));
4198 if (res
!= NULL_TREE
)
4200 get_constraint_for (res
, &lhsc
);
4201 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_MEMPCPY
4202 || DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_STPCPY
4203 || DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_STPNCPY
4204 || DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_MEMPCPY_CHK
4205 || DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_STPCPY_CHK
4206 || DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_STPNCPY_CHK
)
4207 get_constraint_for_ptr_offset (dest
, NULL_TREE
, &rhsc
);
4209 get_constraint_for (dest
, &rhsc
);
4210 process_all_all_constraints (lhsc
, rhsc
);
4214 get_constraint_for_ptr_offset (dest
, NULL_TREE
, &lhsc
);
4215 get_constraint_for_ptr_offset (src
, NULL_TREE
, &rhsc
);
4218 process_all_all_constraints (lhsc
, rhsc
);
4221 case BUILT_IN_MEMSET
:
4222 case BUILT_IN_MEMSET_CHK
:
4223 case BUILT_IN_TM_MEMSET
:
4225 tree res
= gimple_call_lhs (t
);
4226 tree dest
= gimple_call_arg (t
, 0);
4229 struct constraint_expr ac
;
4230 if (res
!= NULL_TREE
)
4232 get_constraint_for (res
, &lhsc
);
4233 get_constraint_for (dest
, &rhsc
);
4234 process_all_all_constraints (lhsc
, rhsc
);
4237 get_constraint_for_ptr_offset (dest
, NULL_TREE
, &lhsc
);
4239 if (flag_delete_null_pointer_checks
4240 && integer_zerop (gimple_call_arg (t
, 1)))
4242 ac
.type
= ADDRESSOF
;
4243 ac
.var
= nothing_id
;
4248 ac
.var
= integer_id
;
4251 FOR_EACH_VEC_ELT (lhsc
, i
, lhsp
)
4252 process_constraint (new_constraint (*lhsp
, ac
));
4255 case BUILT_IN_POSIX_MEMALIGN
:
4257 tree ptrptr
= gimple_call_arg (t
, 0);
4258 get_constraint_for (ptrptr
, &lhsc
);
4260 varinfo_t vi
= make_heapvar ("HEAP");
4261 /* We are marking allocated storage local, we deal with it becoming
4262 global by escaping and setting of vars_contains_escaped_heap. */
4263 DECL_EXTERNAL (vi
->decl
) = 0;
4264 vi
->is_global_var
= 0;
4265 struct constraint_expr tmpc
;
4268 tmpc
.type
= ADDRESSOF
;
4269 rhsc
.safe_push (tmpc
);
4270 process_all_all_constraints (lhsc
, rhsc
);
4273 case BUILT_IN_ASSUME_ALIGNED
:
4275 tree res
= gimple_call_lhs (t
);
4276 tree dest
= gimple_call_arg (t
, 0);
4277 if (res
!= NULL_TREE
)
4279 get_constraint_for (res
, &lhsc
);
4280 get_constraint_for (dest
, &rhsc
);
4281 process_all_all_constraints (lhsc
, rhsc
);
4285 /* All the following functions do not return pointers, do not
4286 modify the points-to sets of memory reachable from their
4287 arguments and do not add to the ESCAPED solution. */
4288 case BUILT_IN_SINCOS
:
4289 case BUILT_IN_SINCOSF
:
4290 case BUILT_IN_SINCOSL
:
4291 case BUILT_IN_FREXP
:
4292 case BUILT_IN_FREXPF
:
4293 case BUILT_IN_FREXPL
:
4294 case BUILT_IN_GAMMA_R
:
4295 case BUILT_IN_GAMMAF_R
:
4296 case BUILT_IN_GAMMAL_R
:
4297 case BUILT_IN_LGAMMA_R
:
4298 case BUILT_IN_LGAMMAF_R
:
4299 case BUILT_IN_LGAMMAL_R
:
4301 case BUILT_IN_MODFF
:
4302 case BUILT_IN_MODFL
:
4303 case BUILT_IN_REMQUO
:
4304 case BUILT_IN_REMQUOF
:
4305 case BUILT_IN_REMQUOL
:
4308 case BUILT_IN_STRDUP
:
4309 case BUILT_IN_STRNDUP
:
4310 case BUILT_IN_REALLOC
:
4311 if (gimple_call_lhs (t
))
4313 handle_lhs_call (t
, gimple_call_lhs (t
),
4314 gimple_call_return_flags (t
) | ERF_NOALIAS
,
4316 get_constraint_for_ptr_offset (gimple_call_lhs (t
),
4318 get_constraint_for_ptr_offset (gimple_call_arg (t
, 0),
4322 process_all_all_constraints (lhsc
, rhsc
);
4325 /* For realloc the resulting pointer can be equal to the
4326 argument as well. But only doing this wouldn't be
4327 correct because with ptr == 0 realloc behaves like malloc. */
4328 if (DECL_FUNCTION_CODE (fndecl
) == BUILT_IN_REALLOC
)
4330 get_constraint_for (gimple_call_lhs (t
), &lhsc
);
4331 get_constraint_for (gimple_call_arg (t
, 0), &rhsc
);
4332 process_all_all_constraints (lhsc
, rhsc
);
4337 /* String / character search functions return a pointer into the
4338 source string or NULL. */
4339 case BUILT_IN_INDEX
:
4340 case BUILT_IN_STRCHR
:
4341 case BUILT_IN_STRRCHR
:
4342 case BUILT_IN_MEMCHR
:
4343 case BUILT_IN_STRSTR
:
4344 case BUILT_IN_STRPBRK
:
4345 if (gimple_call_lhs (t
))
4347 tree src
= gimple_call_arg (t
, 0);
4348 get_constraint_for_ptr_offset (src
, NULL_TREE
, &rhsc
);
4349 constraint_expr nul
;
4350 nul
.var
= nothing_id
;
4352 nul
.type
= ADDRESSOF
;
4353 rhsc
.safe_push (nul
);
4354 get_constraint_for (gimple_call_lhs (t
), &lhsc
);
4355 process_all_all_constraints (lhsc
, rhsc
);
4358 /* Trampolines are special - they set up passing the static
4360 case BUILT_IN_INIT_TRAMPOLINE
:
4362 tree tramp
= gimple_call_arg (t
, 0);
4363 tree nfunc
= gimple_call_arg (t
, 1);
4364 tree frame
= gimple_call_arg (t
, 2);
4366 struct constraint_expr lhs
, *rhsp
;
4369 varinfo_t nfi
= NULL
;
4370 gcc_assert (TREE_CODE (nfunc
) == ADDR_EXPR
);
4371 nfi
= lookup_vi_for_tree (TREE_OPERAND (nfunc
, 0));
4374 lhs
= get_function_part_constraint (nfi
, fi_static_chain
);
4375 get_constraint_for (frame
, &rhsc
);
4376 FOR_EACH_VEC_ELT (rhsc
, i
, rhsp
)
4377 process_constraint (new_constraint (lhs
, *rhsp
));
4380 /* Make the frame point to the function for
4381 the trampoline adjustment call. */
4382 get_constraint_for (tramp
, &lhsc
);
4384 get_constraint_for (nfunc
, &rhsc
);
4385 process_all_all_constraints (lhsc
, rhsc
);
4390 /* Else fallthru to generic handling which will let
4391 the frame escape. */
4394 case BUILT_IN_ADJUST_TRAMPOLINE
:
4396 tree tramp
= gimple_call_arg (t
, 0);
4397 tree res
= gimple_call_lhs (t
);
4398 if (in_ipa_mode
&& res
)
4400 get_constraint_for (res
, &lhsc
);
4401 get_constraint_for (tramp
, &rhsc
);
4403 process_all_all_constraints (lhsc
, rhsc
);
4407 CASE_BUILT_IN_TM_STORE (1):
4408 CASE_BUILT_IN_TM_STORE (2):
4409 CASE_BUILT_IN_TM_STORE (4):
4410 CASE_BUILT_IN_TM_STORE (8):
4411 CASE_BUILT_IN_TM_STORE (FLOAT
):
4412 CASE_BUILT_IN_TM_STORE (DOUBLE
):
4413 CASE_BUILT_IN_TM_STORE (LDOUBLE
):
4414 CASE_BUILT_IN_TM_STORE (M64
):
4415 CASE_BUILT_IN_TM_STORE (M128
):
4416 CASE_BUILT_IN_TM_STORE (M256
):
4418 tree addr
= gimple_call_arg (t
, 0);
4419 tree src
= gimple_call_arg (t
, 1);
4421 get_constraint_for (addr
, &lhsc
);
4423 get_constraint_for (src
, &rhsc
);
4424 process_all_all_constraints (lhsc
, rhsc
);
4427 CASE_BUILT_IN_TM_LOAD (1):
4428 CASE_BUILT_IN_TM_LOAD (2):
4429 CASE_BUILT_IN_TM_LOAD (4):
4430 CASE_BUILT_IN_TM_LOAD (8):
4431 CASE_BUILT_IN_TM_LOAD (FLOAT
):
4432 CASE_BUILT_IN_TM_LOAD (DOUBLE
):
4433 CASE_BUILT_IN_TM_LOAD (LDOUBLE
):
4434 CASE_BUILT_IN_TM_LOAD (M64
):
4435 CASE_BUILT_IN_TM_LOAD (M128
):
4436 CASE_BUILT_IN_TM_LOAD (M256
):
4438 tree dest
= gimple_call_lhs (t
);
4439 tree addr
= gimple_call_arg (t
, 0);
4441 get_constraint_for (dest
, &lhsc
);
4442 get_constraint_for (addr
, &rhsc
);
4444 process_all_all_constraints (lhsc
, rhsc
);
4447 /* Variadic argument handling needs to be handled in IPA
4449 case BUILT_IN_VA_START
:
4451 tree valist
= gimple_call_arg (t
, 0);
4452 struct constraint_expr rhs
, *lhsp
;
4454 get_constraint_for (valist
, &lhsc
);
4456 /* The va_list gets access to pointers in variadic
4457 arguments. Which we know in the case of IPA analysis
4458 and otherwise are just all nonlocal variables. */
4461 fi
= lookup_vi_for_tree (fn
->decl
);
4462 rhs
= get_function_part_constraint (fi
, ~0);
4463 rhs
.type
= ADDRESSOF
;
4467 rhs
.var
= nonlocal_id
;
4468 rhs
.type
= ADDRESSOF
;
4471 FOR_EACH_VEC_ELT (lhsc
, i
, lhsp
)
4472 process_constraint (new_constraint (*lhsp
, rhs
));
4473 /* va_list is clobbered. */
4474 make_constraint_to (get_call_clobber_vi (t
)->id
, valist
);
4477 /* va_end doesn't have any effect that matters. */
4478 case BUILT_IN_VA_END
:
4480 /* Alternate return. Simply give up for now. */
4481 case BUILT_IN_RETURN
:
4485 || !(fi
= get_vi_for_tree (fn
->decl
)))
4486 make_constraint_from (get_varinfo (escaped_id
), anything_id
);
4487 else if (in_ipa_mode
4490 struct constraint_expr lhs
, rhs
;
4491 lhs
= get_function_part_constraint (fi
, fi_result
);
4492 rhs
.var
= anything_id
;
4495 process_constraint (new_constraint (lhs
, rhs
));
4499 /* printf-style functions may have hooks to set pointers to
4500 point to somewhere into the generated string. Leave them
4501 for a later exercise... */
4503 /* Fallthru to general call handling. */;
4509 /* Create constraints for the call T. */
4512 find_func_aliases_for_call (struct function
*fn
, gcall
*t
)
4514 tree fndecl
= gimple_call_fndecl (t
);
4517 if (fndecl
!= NULL_TREE
4518 && DECL_BUILT_IN (fndecl
)
4519 && find_func_aliases_for_builtin_call (fn
, t
))
4522 fi
= get_fi_for_callee (t
);
4524 || (fndecl
&& !fi
->is_fn_info
))
4526 auto_vec
<ce_s
, 16> rhsc
;
4527 int flags
= gimple_call_flags (t
);
4529 /* Const functions can return their arguments and addresses
4530 of global memory but not of escaped memory. */
4531 if (flags
& (ECF_CONST
|ECF_NOVOPS
))
4533 if (gimple_call_lhs (t
))
4534 handle_const_call (t
, &rhsc
);
4536 /* Pure functions can return addresses in and of memory
4537 reachable from their arguments, but they are not an escape
4538 point for reachable memory of their arguments. */
4539 else if (flags
& (ECF_PURE
|ECF_LOOPING_CONST_OR_PURE
))
4540 handle_pure_call (t
, &rhsc
);
4542 handle_rhs_call (t
, &rhsc
);
4543 if (gimple_call_lhs (t
))
4544 handle_lhs_call (t
, gimple_call_lhs (t
),
4545 gimple_call_return_flags (t
), rhsc
, fndecl
);
4549 auto_vec
<ce_s
, 2> rhsc
;
4553 /* Assign all the passed arguments to the appropriate incoming
4554 parameters of the function. */
4555 for (j
= 0; j
< gimple_call_num_args (t
); j
++)
4557 struct constraint_expr lhs
;
4558 struct constraint_expr
*rhsp
;
4559 tree arg
= gimple_call_arg (t
, j
);
4561 get_constraint_for_rhs (arg
, &rhsc
);
4562 lhs
= get_function_part_constraint (fi
, fi_parm_base
+ j
);
4563 while (rhsc
.length () != 0)
4565 rhsp
= &rhsc
.last ();
4566 process_constraint (new_constraint (lhs
, *rhsp
));
4571 /* If we are returning a value, assign it to the result. */
4572 lhsop
= gimple_call_lhs (t
);
4575 auto_vec
<ce_s
, 2> lhsc
;
4576 struct constraint_expr rhs
;
4577 struct constraint_expr
*lhsp
;
4579 get_constraint_for (lhsop
, &lhsc
);
4580 rhs
= get_function_part_constraint (fi
, fi_result
);
4582 && DECL_RESULT (fndecl
)
4583 && DECL_BY_REFERENCE (DECL_RESULT (fndecl
)))
4585 auto_vec
<ce_s
, 2> tem
;
4586 tem
.quick_push (rhs
);
4588 gcc_checking_assert (tem
.length () == 1);
4591 FOR_EACH_VEC_ELT (lhsc
, j
, lhsp
)
4592 process_constraint (new_constraint (*lhsp
, rhs
));
4595 /* If we pass the result decl by reference, honor that. */
4598 && DECL_RESULT (fndecl
)
4599 && DECL_BY_REFERENCE (DECL_RESULT (fndecl
)))
4601 struct constraint_expr lhs
;
4602 struct constraint_expr
*rhsp
;
4604 get_constraint_for_address_of (lhsop
, &rhsc
);
4605 lhs
= get_function_part_constraint (fi
, fi_result
);
4606 FOR_EACH_VEC_ELT (rhsc
, j
, rhsp
)
4607 process_constraint (new_constraint (lhs
, *rhsp
));
4611 /* If we use a static chain, pass it along. */
4612 if (gimple_call_chain (t
))
4614 struct constraint_expr lhs
;
4615 struct constraint_expr
*rhsp
;
4617 get_constraint_for (gimple_call_chain (t
), &rhsc
);
4618 lhs
= get_function_part_constraint (fi
, fi_static_chain
);
4619 FOR_EACH_VEC_ELT (rhsc
, j
, rhsp
)
4620 process_constraint (new_constraint (lhs
, *rhsp
));
4625 /* Walk statement T setting up aliasing constraints according to the
4626 references found in T. This function is the main part of the
4627 constraint builder. AI points to auxiliary alias information used
4628 when building alias sets and computing alias grouping heuristics. */
4631 find_func_aliases (struct function
*fn
, gimple origt
)
4634 auto_vec
<ce_s
, 16> lhsc
;
4635 auto_vec
<ce_s
, 16> rhsc
;
4636 struct constraint_expr
*c
;
4639 /* Now build constraints expressions. */
4640 if (gimple_code (t
) == GIMPLE_PHI
)
4645 /* For a phi node, assign all the arguments to
4647 get_constraint_for (gimple_phi_result (t
), &lhsc
);
4648 for (i
= 0; i
< gimple_phi_num_args (t
); i
++)
4650 tree strippedrhs
= PHI_ARG_DEF (t
, i
);
4652 STRIP_NOPS (strippedrhs
);
4653 get_constraint_for_rhs (gimple_phi_arg_def (t
, i
), &rhsc
);
4655 FOR_EACH_VEC_ELT (lhsc
, j
, c
)
4657 struct constraint_expr
*c2
;
4658 while (rhsc
.length () > 0)
4661 process_constraint (new_constraint (*c
, *c2
));
4667 /* In IPA mode, we need to generate constraints to pass call
4668 arguments through their calls. There are two cases,
4669 either a GIMPLE_CALL returning a value, or just a plain
4670 GIMPLE_CALL when we are not.
4672 In non-ipa mode, we need to generate constraints for each
4673 pointer passed by address. */
4674 else if (is_gimple_call (t
))
4675 find_func_aliases_for_call (fn
, as_a
<gcall
*> (t
));
4677 /* Otherwise, just a regular assignment statement. Only care about
4678 operations with pointer result, others are dealt with as escape
4679 points if they have pointer operands. */
4680 else if (is_gimple_assign (t
))
4682 /* Otherwise, just a regular assignment statement. */
4683 tree lhsop
= gimple_assign_lhs (t
);
4684 tree rhsop
= (gimple_num_ops (t
) == 2) ? gimple_assign_rhs1 (t
) : NULL
;
4686 if (rhsop
&& TREE_CLOBBER_P (rhsop
))
4687 /* Ignore clobbers, they don't actually store anything into
4690 else if (rhsop
&& AGGREGATE_TYPE_P (TREE_TYPE (lhsop
)))
4691 do_structure_copy (lhsop
, rhsop
);
4694 enum tree_code code
= gimple_assign_rhs_code (t
);
4696 get_constraint_for (lhsop
, &lhsc
);
4698 if (FLOAT_TYPE_P (TREE_TYPE (lhsop
)))
4699 /* If the operation produces a floating point result then
4700 assume the value is not produced to transfer a pointer. */
4702 else if (code
== POINTER_PLUS_EXPR
)
4703 get_constraint_for_ptr_offset (gimple_assign_rhs1 (t
),
4704 gimple_assign_rhs2 (t
), &rhsc
);
4705 else if (code
== BIT_AND_EXPR
4706 && TREE_CODE (gimple_assign_rhs2 (t
)) == INTEGER_CST
)
4708 /* Aligning a pointer via a BIT_AND_EXPR is offsetting
4709 the pointer. Handle it by offsetting it by UNKNOWN. */
4710 get_constraint_for_ptr_offset (gimple_assign_rhs1 (t
),
4713 else if ((CONVERT_EXPR_CODE_P (code
)
4714 && !(POINTER_TYPE_P (gimple_expr_type (t
))
4715 && !POINTER_TYPE_P (TREE_TYPE (rhsop
))))
4716 || gimple_assign_single_p (t
))
4717 get_constraint_for_rhs (rhsop
, &rhsc
);
4718 else if (code
== COND_EXPR
)
4720 /* The result is a merge of both COND_EXPR arms. */
4721 auto_vec
<ce_s
, 2> tmp
;
4722 struct constraint_expr
*rhsp
;
4724 get_constraint_for_rhs (gimple_assign_rhs2 (t
), &rhsc
);
4725 get_constraint_for_rhs (gimple_assign_rhs3 (t
), &tmp
);
4726 FOR_EACH_VEC_ELT (tmp
, i
, rhsp
)
4727 rhsc
.safe_push (*rhsp
);
4729 else if (truth_value_p (code
))
4730 /* Truth value results are not pointer (parts). Or at least
4731 very very unreasonable obfuscation of a part. */
4735 /* All other operations are merges. */
4736 auto_vec
<ce_s
, 4> tmp
;
4737 struct constraint_expr
*rhsp
;
4739 get_constraint_for_rhs (gimple_assign_rhs1 (t
), &rhsc
);
4740 for (i
= 2; i
< gimple_num_ops (t
); ++i
)
4742 get_constraint_for_rhs (gimple_op (t
, i
), &tmp
);
4743 FOR_EACH_VEC_ELT (tmp
, j
, rhsp
)
4744 rhsc
.safe_push (*rhsp
);
4748 process_all_all_constraints (lhsc
, rhsc
);
4750 /* If there is a store to a global variable the rhs escapes. */
4751 if ((lhsop
= get_base_address (lhsop
)) != NULL_TREE
4753 && is_global_var (lhsop
)
4755 || DECL_EXTERNAL (lhsop
) || TREE_PUBLIC (lhsop
)))
4756 make_escape_constraint (rhsop
);
4758 /* Handle escapes through return. */
4759 else if (gimple_code (t
) == GIMPLE_RETURN
4760 && gimple_return_retval (as_a
<greturn
*> (t
)) != NULL_TREE
)
4762 greturn
*return_stmt
= as_a
<greturn
*> (t
);
4765 || !(fi
= get_vi_for_tree (fn
->decl
)))
4766 make_escape_constraint (gimple_return_retval (return_stmt
));
4767 else if (in_ipa_mode
4770 struct constraint_expr lhs
;
4771 struct constraint_expr
*rhsp
;
4774 lhs
= get_function_part_constraint (fi
, fi_result
);
4775 get_constraint_for_rhs (gimple_return_retval (return_stmt
), &rhsc
);
4776 FOR_EACH_VEC_ELT (rhsc
, i
, rhsp
)
4777 process_constraint (new_constraint (lhs
, *rhsp
));
4780 /* Handle asms conservatively by adding escape constraints to everything. */
4781 else if (gasm
*asm_stmt
= dyn_cast
<gasm
*> (t
))
4783 unsigned i
, noutputs
;
4784 const char **oconstraints
;
4785 const char *constraint
;
4786 bool allows_mem
, allows_reg
, is_inout
;
4788 noutputs
= gimple_asm_noutputs (asm_stmt
);
4789 oconstraints
= XALLOCAVEC (const char *, noutputs
);
4791 for (i
= 0; i
< noutputs
; ++i
)
4793 tree link
= gimple_asm_output_op (asm_stmt
, i
);
4794 tree op
= TREE_VALUE (link
);
4796 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link
)));
4797 oconstraints
[i
] = constraint
;
4798 parse_output_constraint (&constraint
, i
, 0, 0, &allows_mem
,
4799 &allows_reg
, &is_inout
);
4801 /* A memory constraint makes the address of the operand escape. */
4802 if (!allows_reg
&& allows_mem
)
4803 make_escape_constraint (build_fold_addr_expr (op
));
4805 /* The asm may read global memory, so outputs may point to
4806 any global memory. */
4809 auto_vec
<ce_s
, 2> lhsc
;
4810 struct constraint_expr rhsc
, *lhsp
;
4812 get_constraint_for (op
, &lhsc
);
4813 rhsc
.var
= nonlocal_id
;
4816 FOR_EACH_VEC_ELT (lhsc
, j
, lhsp
)
4817 process_constraint (new_constraint (*lhsp
, rhsc
));
4820 for (i
= 0; i
< gimple_asm_ninputs (asm_stmt
); ++i
)
4822 tree link
= gimple_asm_input_op (asm_stmt
, i
);
4823 tree op
= TREE_VALUE (link
);
4825 constraint
= TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link
)));
4827 parse_input_constraint (&constraint
, 0, 0, noutputs
, 0, oconstraints
,
4828 &allows_mem
, &allows_reg
);
4830 /* A memory constraint makes the address of the operand escape. */
4831 if (!allows_reg
&& allows_mem
)
4832 make_escape_constraint (build_fold_addr_expr (op
));
4833 /* Strictly we'd only need the constraint to ESCAPED if
4834 the asm clobbers memory, otherwise using something
4835 along the lines of per-call clobbers/uses would be enough. */
4837 make_escape_constraint (op
);
4843 /* Create a constraint adding to the clobber set of FI the memory
4844 pointed to by PTR. */
4847 process_ipa_clobber (varinfo_t fi
, tree ptr
)
4849 vec
<ce_s
> ptrc
= vNULL
;
4850 struct constraint_expr
*c
, lhs
;
4852 get_constraint_for_rhs (ptr
, &ptrc
);
4853 lhs
= get_function_part_constraint (fi
, fi_clobbers
);
4854 FOR_EACH_VEC_ELT (ptrc
, i
, c
)
4855 process_constraint (new_constraint (lhs
, *c
));
4859 /* Walk statement T setting up clobber and use constraints according to the
4860 references found in T. This function is a main part of the
4861 IPA constraint builder. */
4864 find_func_clobbers (struct function
*fn
, gimple origt
)
4867 auto_vec
<ce_s
, 16> lhsc
;
4868 auto_vec
<ce_s
, 16> rhsc
;
4871 /* Add constraints for clobbered/used in IPA mode.
4872 We are not interested in what automatic variables are clobbered
4873 or used as we only use the information in the caller to which
4874 they do not escape. */
4875 gcc_assert (in_ipa_mode
);
4877 /* If the stmt refers to memory in any way it better had a VUSE. */
4878 if (gimple_vuse (t
) == NULL_TREE
)
4881 /* We'd better have function information for the current function. */
4882 fi
= lookup_vi_for_tree (fn
->decl
);
4883 gcc_assert (fi
!= NULL
);
4885 /* Account for stores in assignments and calls. */
4886 if (gimple_vdef (t
) != NULL_TREE
4887 && gimple_has_lhs (t
))
4889 tree lhs
= gimple_get_lhs (t
);
4891 while (handled_component_p (tem
))
4892 tem
= TREE_OPERAND (tem
, 0);
4894 && !auto_var_in_fn_p (tem
, fn
->decl
))
4895 || INDIRECT_REF_P (tem
)
4896 || (TREE_CODE (tem
) == MEM_REF
4897 && !(TREE_CODE (TREE_OPERAND (tem
, 0)) == ADDR_EXPR
4899 (TREE_OPERAND (TREE_OPERAND (tem
, 0), 0), fn
->decl
))))
4901 struct constraint_expr lhsc
, *rhsp
;
4903 lhsc
= get_function_part_constraint (fi
, fi_clobbers
);
4904 get_constraint_for_address_of (lhs
, &rhsc
);
4905 FOR_EACH_VEC_ELT (rhsc
, i
, rhsp
)
4906 process_constraint (new_constraint (lhsc
, *rhsp
));
4911 /* Account for uses in assigments and returns. */
4912 if (gimple_assign_single_p (t
)
4913 || (gimple_code (t
) == GIMPLE_RETURN
4914 && gimple_return_retval (as_a
<greturn
*> (t
)) != NULL_TREE
))
4916 tree rhs
= (gimple_assign_single_p (t
)
4917 ? gimple_assign_rhs1 (t
)
4918 : gimple_return_retval (as_a
<greturn
*> (t
)));
4920 while (handled_component_p (tem
))
4921 tem
= TREE_OPERAND (tem
, 0);
4923 && !auto_var_in_fn_p (tem
, fn
->decl
))
4924 || INDIRECT_REF_P (tem
)
4925 || (TREE_CODE (tem
) == MEM_REF
4926 && !(TREE_CODE (TREE_OPERAND (tem
, 0)) == ADDR_EXPR
4928 (TREE_OPERAND (TREE_OPERAND (tem
, 0), 0), fn
->decl
))))
4930 struct constraint_expr lhs
, *rhsp
;
4932 lhs
= get_function_part_constraint (fi
, fi_uses
);
4933 get_constraint_for_address_of (rhs
, &rhsc
);
4934 FOR_EACH_VEC_ELT (rhsc
, i
, rhsp
)
4935 process_constraint (new_constraint (lhs
, *rhsp
));
4940 if (gcall
*call_stmt
= dyn_cast
<gcall
*> (t
))
4942 varinfo_t cfi
= NULL
;
4943 tree decl
= gimple_call_fndecl (t
);
4944 struct constraint_expr lhs
, rhs
;
4947 /* For builtins we do not have separate function info. For those
4948 we do not generate escapes for we have to generate clobbers/uses. */
4949 if (gimple_call_builtin_p (t
, BUILT_IN_NORMAL
))
4950 switch (DECL_FUNCTION_CODE (decl
))
4952 /* The following functions use and clobber memory pointed to
4953 by their arguments. */
4954 case BUILT_IN_STRCPY
:
4955 case BUILT_IN_STRNCPY
:
4956 case BUILT_IN_BCOPY
:
4957 case BUILT_IN_MEMCPY
:
4958 case BUILT_IN_MEMMOVE
:
4959 case BUILT_IN_MEMPCPY
:
4960 case BUILT_IN_STPCPY
:
4961 case BUILT_IN_STPNCPY
:
4962 case BUILT_IN_STRCAT
:
4963 case BUILT_IN_STRNCAT
:
4964 case BUILT_IN_STRCPY_CHK
:
4965 case BUILT_IN_STRNCPY_CHK
:
4966 case BUILT_IN_MEMCPY_CHK
:
4967 case BUILT_IN_MEMMOVE_CHK
:
4968 case BUILT_IN_MEMPCPY_CHK
:
4969 case BUILT_IN_STPCPY_CHK
:
4970 case BUILT_IN_STPNCPY_CHK
:
4971 case BUILT_IN_STRCAT_CHK
:
4972 case BUILT_IN_STRNCAT_CHK
:
4974 tree dest
= gimple_call_arg (t
, (DECL_FUNCTION_CODE (decl
)
4975 == BUILT_IN_BCOPY
? 1 : 0));
4976 tree src
= gimple_call_arg (t
, (DECL_FUNCTION_CODE (decl
)
4977 == BUILT_IN_BCOPY
? 0 : 1));
4979 struct constraint_expr
*rhsp
, *lhsp
;
4980 get_constraint_for_ptr_offset (dest
, NULL_TREE
, &lhsc
);
4981 lhs
= get_function_part_constraint (fi
, fi_clobbers
);
4982 FOR_EACH_VEC_ELT (lhsc
, i
, lhsp
)
4983 process_constraint (new_constraint (lhs
, *lhsp
));
4984 get_constraint_for_ptr_offset (src
, NULL_TREE
, &rhsc
);
4985 lhs
= get_function_part_constraint (fi
, fi_uses
);
4986 FOR_EACH_VEC_ELT (rhsc
, i
, rhsp
)
4987 process_constraint (new_constraint (lhs
, *rhsp
));
4990 /* The following function clobbers memory pointed to by
4992 case BUILT_IN_MEMSET
:
4993 case BUILT_IN_MEMSET_CHK
:
4994 case BUILT_IN_POSIX_MEMALIGN
:
4996 tree dest
= gimple_call_arg (t
, 0);
4999 get_constraint_for_ptr_offset (dest
, NULL_TREE
, &lhsc
);
5000 lhs
= get_function_part_constraint (fi
, fi_clobbers
);
5001 FOR_EACH_VEC_ELT (lhsc
, i
, lhsp
)
5002 process_constraint (new_constraint (lhs
, *lhsp
));
5005 /* The following functions clobber their second and third
5007 case BUILT_IN_SINCOS
:
5008 case BUILT_IN_SINCOSF
:
5009 case BUILT_IN_SINCOSL
:
5011 process_ipa_clobber (fi
, gimple_call_arg (t
, 1));
5012 process_ipa_clobber (fi
, gimple_call_arg (t
, 2));
5015 /* The following functions clobber their second argument. */
5016 case BUILT_IN_FREXP
:
5017 case BUILT_IN_FREXPF
:
5018 case BUILT_IN_FREXPL
:
5019 case BUILT_IN_LGAMMA_R
:
5020 case BUILT_IN_LGAMMAF_R
:
5021 case BUILT_IN_LGAMMAL_R
:
5022 case BUILT_IN_GAMMA_R
:
5023 case BUILT_IN_GAMMAF_R
:
5024 case BUILT_IN_GAMMAL_R
:
5026 case BUILT_IN_MODFF
:
5027 case BUILT_IN_MODFL
:
5029 process_ipa_clobber (fi
, gimple_call_arg (t
, 1));
5032 /* The following functions clobber their third argument. */
5033 case BUILT_IN_REMQUO
:
5034 case BUILT_IN_REMQUOF
:
5035 case BUILT_IN_REMQUOL
:
5037 process_ipa_clobber (fi
, gimple_call_arg (t
, 2));
5040 /* The following functions neither read nor clobber memory. */
5041 case BUILT_IN_ASSUME_ALIGNED
:
5044 /* Trampolines are of no interest to us. */
5045 case BUILT_IN_INIT_TRAMPOLINE
:
5046 case BUILT_IN_ADJUST_TRAMPOLINE
:
5048 case BUILT_IN_VA_START
:
5049 case BUILT_IN_VA_END
:
5051 /* printf-style functions may have hooks to set pointers to
5052 point to somewhere into the generated string. Leave them
5053 for a later exercise... */
5055 /* Fallthru to general call handling. */;
5058 /* Parameters passed by value are used. */
5059 lhs
= get_function_part_constraint (fi
, fi_uses
);
5060 for (i
= 0; i
< gimple_call_num_args (t
); i
++)
5062 struct constraint_expr
*rhsp
;
5063 tree arg
= gimple_call_arg (t
, i
);
5065 if (TREE_CODE (arg
) == SSA_NAME
5066 || is_gimple_min_invariant (arg
))
5069 get_constraint_for_address_of (arg
, &rhsc
);
5070 FOR_EACH_VEC_ELT (rhsc
, j
, rhsp
)
5071 process_constraint (new_constraint (lhs
, *rhsp
));
5075 /* Build constraints for propagating clobbers/uses along the
5077 cfi
= get_fi_for_callee (call_stmt
);
5078 if (cfi
->id
== anything_id
)
5080 if (gimple_vdef (t
))
5081 make_constraint_from (first_vi_for_offset (fi
, fi_clobbers
),
5083 make_constraint_from (first_vi_for_offset (fi
, fi_uses
),
5088 /* For callees without function info (that's external functions),
5089 ESCAPED is clobbered and used. */
5090 if (gimple_call_fndecl (t
)
5091 && !cfi
->is_fn_info
)
5095 if (gimple_vdef (t
))
5096 make_copy_constraint (first_vi_for_offset (fi
, fi_clobbers
),
5098 make_copy_constraint (first_vi_for_offset (fi
, fi_uses
), escaped_id
);
5100 /* Also honor the call statement use/clobber info. */
5101 if ((vi
= lookup_call_clobber_vi (call_stmt
)) != NULL
)
5102 make_copy_constraint (first_vi_for_offset (fi
, fi_clobbers
),
5104 if ((vi
= lookup_call_use_vi (call_stmt
)) != NULL
)
5105 make_copy_constraint (first_vi_for_offset (fi
, fi_uses
),
5110 /* Otherwise the caller clobbers and uses what the callee does.
5111 ??? This should use a new complex constraint that filters
5112 local variables of the callee. */
5113 if (gimple_vdef (t
))
5115 lhs
= get_function_part_constraint (fi
, fi_clobbers
);
5116 rhs
= get_function_part_constraint (cfi
, fi_clobbers
);
5117 process_constraint (new_constraint (lhs
, rhs
));
5119 lhs
= get_function_part_constraint (fi
, fi_uses
);
5120 rhs
= get_function_part_constraint (cfi
, fi_uses
);
5121 process_constraint (new_constraint (lhs
, rhs
));
5123 else if (gimple_code (t
) == GIMPLE_ASM
)
5125 /* ??? Ick. We can do better. */
5126 if (gimple_vdef (t
))
5127 make_constraint_from (first_vi_for_offset (fi
, fi_clobbers
),
5129 make_constraint_from (first_vi_for_offset (fi
, fi_uses
),
5135 /* Find the first varinfo in the same variable as START that overlaps with
5136 OFFSET. Return NULL if we can't find one. */
5139 first_vi_for_offset (varinfo_t start
, unsigned HOST_WIDE_INT offset
)
5141 /* If the offset is outside of the variable, bail out. */
5142 if (offset
>= start
->fullsize
)
5145 /* If we cannot reach offset from start, lookup the first field
5146 and start from there. */
5147 if (start
->offset
> offset
)
5148 start
= get_varinfo (start
->head
);
5152 /* We may not find a variable in the field list with the actual
5153 offset when when we have glommed a structure to a variable.
5154 In that case, however, offset should still be within the size
5156 if (offset
>= start
->offset
5157 && (offset
- start
->offset
) < start
->size
)
5160 start
= vi_next (start
);
5166 /* Find the first varinfo in the same variable as START that overlaps with
5167 OFFSET. If there is no such varinfo the varinfo directly preceding
5168 OFFSET is returned. */
5171 first_or_preceding_vi_for_offset (varinfo_t start
,
5172 unsigned HOST_WIDE_INT offset
)
5174 /* If we cannot reach offset from start, lookup the first field
5175 and start from there. */
5176 if (start
->offset
> offset
)
5177 start
= get_varinfo (start
->head
);
5179 /* We may not find a variable in the field list with the actual
5180 offset when when we have glommed a structure to a variable.
5181 In that case, however, offset should still be within the size
5183 If we got beyond the offset we look for return the field
5184 directly preceding offset which may be the last field. */
5186 && offset
>= start
->offset
5187 && !((offset
- start
->offset
) < start
->size
))
5188 start
= vi_next (start
);
5194 /* This structure is used during pushing fields onto the fieldstack
5195 to track the offset of the field, since bitpos_of_field gives it
5196 relative to its immediate containing type, and we want it relative
5197 to the ultimate containing object. */
5201 /* Offset from the base of the base containing object to this field. */
5202 HOST_WIDE_INT offset
;
5204 /* Size, in bits, of the field. */
5205 unsigned HOST_WIDE_INT size
;
5207 unsigned has_unknown_size
: 1;
5209 unsigned must_have_pointers
: 1;
5211 unsigned may_have_pointers
: 1;
5213 unsigned only_restrict_pointers
: 1;
5215 typedef struct fieldoff fieldoff_s
;
5218 /* qsort comparison function for two fieldoff's PA and PB */
5221 fieldoff_compare (const void *pa
, const void *pb
)
5223 const fieldoff_s
*foa
= (const fieldoff_s
*)pa
;
5224 const fieldoff_s
*fob
= (const fieldoff_s
*)pb
;
5225 unsigned HOST_WIDE_INT foasize
, fobsize
;
5227 if (foa
->offset
< fob
->offset
)
5229 else if (foa
->offset
> fob
->offset
)
5232 foasize
= foa
->size
;
5233 fobsize
= fob
->size
;
5234 if (foasize
< fobsize
)
5236 else if (foasize
> fobsize
)
5241 /* Sort a fieldstack according to the field offset and sizes. */
5243 sort_fieldstack (vec
<fieldoff_s
> fieldstack
)
5245 fieldstack
.qsort (fieldoff_compare
);
5248 /* Return true if T is a type that can have subvars. */
5251 type_can_have_subvars (const_tree t
)
5253 /* Aggregates without overlapping fields can have subvars. */
5254 return TREE_CODE (t
) == RECORD_TYPE
;
5257 /* Return true if V is a tree that we can have subvars for.
5258 Normally, this is any aggregate type. Also complex
5259 types which are not gimple registers can have subvars. */
5262 var_can_have_subvars (const_tree v
)
5264 /* Volatile variables should never have subvars. */
5265 if (TREE_THIS_VOLATILE (v
))
5268 /* Non decls or memory tags can never have subvars. */
5272 return type_can_have_subvars (TREE_TYPE (v
));
5275 /* Return true if T is a type that does contain pointers. */
5278 type_must_have_pointers (tree type
)
5280 if (POINTER_TYPE_P (type
))
5283 if (TREE_CODE (type
) == ARRAY_TYPE
)
5284 return type_must_have_pointers (TREE_TYPE (type
));
5286 /* A function or method can have pointers as arguments, so track
5287 those separately. */
5288 if (TREE_CODE (type
) == FUNCTION_TYPE
5289 || TREE_CODE (type
) == METHOD_TYPE
)
5296 field_must_have_pointers (tree t
)
5298 return type_must_have_pointers (TREE_TYPE (t
));
5301 /* Given a TYPE, and a vector of field offsets FIELDSTACK, push all
5302 the fields of TYPE onto fieldstack, recording their offsets along
5305 OFFSET is used to keep track of the offset in this entire
5306 structure, rather than just the immediately containing structure.
5307 Returns false if the caller is supposed to handle the field we
5311 push_fields_onto_fieldstack (tree type
, vec
<fieldoff_s
> *fieldstack
,
5312 HOST_WIDE_INT offset
)
5315 bool empty_p
= true;
5317 if (TREE_CODE (type
) != RECORD_TYPE
)
5320 /* If the vector of fields is growing too big, bail out early.
5321 Callers check for vec::length <= MAX_FIELDS_FOR_FIELD_SENSITIVE, make
5323 if (fieldstack
->length () > MAX_FIELDS_FOR_FIELD_SENSITIVE
)
5326 for (field
= TYPE_FIELDS (type
); field
; field
= DECL_CHAIN (field
))
5327 if (TREE_CODE (field
) == FIELD_DECL
)
5330 HOST_WIDE_INT foff
= bitpos_of_field (field
);
5332 if (!var_can_have_subvars (field
)
5333 || TREE_CODE (TREE_TYPE (field
)) == QUAL_UNION_TYPE
5334 || TREE_CODE (TREE_TYPE (field
)) == UNION_TYPE
)
5336 else if (!push_fields_onto_fieldstack
5337 (TREE_TYPE (field
), fieldstack
, offset
+ foff
)
5338 && (DECL_SIZE (field
)
5339 && !integer_zerop (DECL_SIZE (field
))))
5340 /* Empty structures may have actual size, like in C++. So
5341 see if we didn't push any subfields and the size is
5342 nonzero, push the field onto the stack. */
5347 fieldoff_s
*pair
= NULL
;
5348 bool has_unknown_size
= false;
5349 bool must_have_pointers_p
;
5351 if (!fieldstack
->is_empty ())
5352 pair
= &fieldstack
->last ();
5354 /* If there isn't anything at offset zero, create sth. */
5356 && offset
+ foff
!= 0)
5358 fieldoff_s e
= {0, offset
+ foff
, false, false, false, false};
5359 pair
= fieldstack
->safe_push (e
);
5362 if (!DECL_SIZE (field
)
5363 || !tree_fits_uhwi_p (DECL_SIZE (field
)))
5364 has_unknown_size
= true;
5366 /* If adjacent fields do not contain pointers merge them. */
5367 must_have_pointers_p
= field_must_have_pointers (field
);
5369 && !has_unknown_size
5370 && !must_have_pointers_p
5371 && !pair
->must_have_pointers
5372 && !pair
->has_unknown_size
5373 && pair
->offset
+ (HOST_WIDE_INT
)pair
->size
== offset
+ foff
)
5375 pair
->size
+= tree_to_uhwi (DECL_SIZE (field
));
5380 e
.offset
= offset
+ foff
;
5381 e
.has_unknown_size
= has_unknown_size
;
5382 if (!has_unknown_size
)
5383 e
.size
= tree_to_uhwi (DECL_SIZE (field
));
5386 e
.must_have_pointers
= must_have_pointers_p
;
5387 e
.may_have_pointers
= true;
5388 e
.only_restrict_pointers
5389 = (!has_unknown_size
5390 && POINTER_TYPE_P (TREE_TYPE (field
))
5391 && TYPE_RESTRICT (TREE_TYPE (field
)));
5392 fieldstack
->safe_push (e
);
5402 /* Count the number of arguments DECL has, and set IS_VARARGS to true
5403 if it is a varargs function. */
5406 count_num_arguments (tree decl
, bool *is_varargs
)
5408 unsigned int num
= 0;
5411 /* Capture named arguments for K&R functions. They do not
5412 have a prototype and thus no TYPE_ARG_TYPES. */
5413 for (t
= DECL_ARGUMENTS (decl
); t
; t
= DECL_CHAIN (t
))
5416 /* Check if the function has variadic arguments. */
5417 for (t
= TYPE_ARG_TYPES (TREE_TYPE (decl
)); t
; t
= TREE_CHAIN (t
))
5418 if (TREE_VALUE (t
) == void_type_node
)
5426 /* Creation function node for DECL, using NAME, and return the index
5427 of the variable we've created for the function. */
5430 create_function_info_for (tree decl
, const char *name
)
5432 struct function
*fn
= DECL_STRUCT_FUNCTION (decl
);
5433 varinfo_t vi
, prev_vi
;
5436 bool is_varargs
= false;
5437 unsigned int num_args
= count_num_arguments (decl
, &is_varargs
);
5439 /* Create the variable info. */
5441 vi
= new_var_info (decl
, name
);
5444 vi
->fullsize
= fi_parm_base
+ num_args
;
5446 vi
->may_have_pointers
= false;
5449 insert_vi_for_tree (vi
->decl
, vi
);
5453 /* Create a variable for things the function clobbers and one for
5454 things the function uses. */
5456 varinfo_t clobbervi
, usevi
;
5457 const char *newname
;
5460 asprintf (&tempname
, "%s.clobber", name
);
5461 newname
= ggc_strdup (tempname
);
5464 clobbervi
= new_var_info (NULL
, newname
);
5465 clobbervi
->offset
= fi_clobbers
;
5466 clobbervi
->size
= 1;
5467 clobbervi
->fullsize
= vi
->fullsize
;
5468 clobbervi
->is_full_var
= true;
5469 clobbervi
->is_global_var
= false;
5470 gcc_assert (prev_vi
->offset
< clobbervi
->offset
);
5471 prev_vi
->next
= clobbervi
->id
;
5472 prev_vi
= clobbervi
;
5474 asprintf (&tempname
, "%s.use", name
);
5475 newname
= ggc_strdup (tempname
);
5478 usevi
= new_var_info (NULL
, newname
);
5479 usevi
->offset
= fi_uses
;
5481 usevi
->fullsize
= vi
->fullsize
;
5482 usevi
->is_full_var
= true;
5483 usevi
->is_global_var
= false;
5484 gcc_assert (prev_vi
->offset
< usevi
->offset
);
5485 prev_vi
->next
= usevi
->id
;
5489 /* And one for the static chain. */
5490 if (fn
->static_chain_decl
!= NULL_TREE
)
5493 const char *newname
;
5496 asprintf (&tempname
, "%s.chain", name
);
5497 newname
= ggc_strdup (tempname
);
5500 chainvi
= new_var_info (fn
->static_chain_decl
, newname
);
5501 chainvi
->offset
= fi_static_chain
;
5503 chainvi
->fullsize
= vi
->fullsize
;
5504 chainvi
->is_full_var
= true;
5505 chainvi
->is_global_var
= false;
5506 gcc_assert (prev_vi
->offset
< chainvi
->offset
);
5507 prev_vi
->next
= chainvi
->id
;
5509 insert_vi_for_tree (fn
->static_chain_decl
, chainvi
);
5512 /* Create a variable for the return var. */
5513 if (DECL_RESULT (decl
) != NULL
5514 || !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl
))))
5517 const char *newname
;
5519 tree resultdecl
= decl
;
5521 if (DECL_RESULT (decl
))
5522 resultdecl
= DECL_RESULT (decl
);
5524 asprintf (&tempname
, "%s.result", name
);
5525 newname
= ggc_strdup (tempname
);
5528 resultvi
= new_var_info (resultdecl
, newname
);
5529 resultvi
->offset
= fi_result
;
5531 resultvi
->fullsize
= vi
->fullsize
;
5532 resultvi
->is_full_var
= true;
5533 if (DECL_RESULT (decl
))
5534 resultvi
->may_have_pointers
= true;
5535 gcc_assert (prev_vi
->offset
< resultvi
->offset
);
5536 prev_vi
->next
= resultvi
->id
;
5538 if (DECL_RESULT (decl
))
5539 insert_vi_for_tree (DECL_RESULT (decl
), resultvi
);
5542 /* Set up variables for each argument. */
5543 arg
= DECL_ARGUMENTS (decl
);
5544 for (i
= 0; i
< num_args
; i
++)
5547 const char *newname
;
5549 tree argdecl
= decl
;
5554 asprintf (&tempname
, "%s.arg%d", name
, i
);
5555 newname
= ggc_strdup (tempname
);
5558 argvi
= new_var_info (argdecl
, newname
);
5559 argvi
->offset
= fi_parm_base
+ i
;
5561 argvi
->is_full_var
= true;
5562 argvi
->fullsize
= vi
->fullsize
;
5564 argvi
->may_have_pointers
= true;
5565 gcc_assert (prev_vi
->offset
< argvi
->offset
);
5566 prev_vi
->next
= argvi
->id
;
5570 insert_vi_for_tree (arg
, argvi
);
5571 arg
= DECL_CHAIN (arg
);
5575 /* Add one representative for all further args. */
5579 const char *newname
;
5583 asprintf (&tempname
, "%s.varargs", name
);
5584 newname
= ggc_strdup (tempname
);
5587 /* We need sth that can be pointed to for va_start. */
5588 decl
= build_fake_var_decl (ptr_type_node
);
5590 argvi
= new_var_info (decl
, newname
);
5591 argvi
->offset
= fi_parm_base
+ num_args
;
5593 argvi
->is_full_var
= true;
5594 argvi
->is_heap_var
= true;
5595 argvi
->fullsize
= vi
->fullsize
;
5596 gcc_assert (prev_vi
->offset
< argvi
->offset
);
5597 prev_vi
->next
= argvi
->id
;
5605 /* Return true if FIELDSTACK contains fields that overlap.
5606 FIELDSTACK is assumed to be sorted by offset. */
5609 check_for_overlaps (vec
<fieldoff_s
> fieldstack
)
5611 fieldoff_s
*fo
= NULL
;
5613 HOST_WIDE_INT lastoffset
= -1;
5615 FOR_EACH_VEC_ELT (fieldstack
, i
, fo
)
5617 if (fo
->offset
== lastoffset
)
5619 lastoffset
= fo
->offset
;
5624 /* Create a varinfo structure for NAME and DECL, and add it to VARMAP.
5625 This will also create any varinfo structures necessary for fields
5629 create_variable_info_for_1 (tree decl
, const char *name
)
5631 varinfo_t vi
, newvi
;
5632 tree decl_type
= TREE_TYPE (decl
);
5633 tree declsize
= DECL_P (decl
) ? DECL_SIZE (decl
) : TYPE_SIZE (decl_type
);
5634 auto_vec
<fieldoff_s
> fieldstack
;
5637 varpool_node
*vnode
;
5640 || !tree_fits_uhwi_p (declsize
))
5642 vi
= new_var_info (decl
, name
);
5646 vi
->is_unknown_size_var
= true;
5647 vi
->is_full_var
= true;
5648 vi
->may_have_pointers
= true;
5652 /* Collect field information. */
5653 if (use_field_sensitive
5654 && var_can_have_subvars (decl
)
5655 /* ??? Force us to not use subfields for global initializers
5656 in IPA mode. Else we'd have to parse arbitrary initializers. */
5658 && is_global_var (decl
)
5659 && (vnode
= varpool_node::get (decl
))
5660 && vnode
->get_constructor ()))
5662 fieldoff_s
*fo
= NULL
;
5663 bool notokay
= false;
5666 push_fields_onto_fieldstack (decl_type
, &fieldstack
, 0);
5668 for (i
= 0; !notokay
&& fieldstack
.iterate (i
, &fo
); i
++)
5669 if (fo
->has_unknown_size
5676 /* We can't sort them if we have a field with a variable sized type,
5677 which will make notokay = true. In that case, we are going to return
5678 without creating varinfos for the fields anyway, so sorting them is a
5682 sort_fieldstack (fieldstack
);
5683 /* Due to some C++ FE issues, like PR 22488, we might end up
5684 what appear to be overlapping fields even though they,
5685 in reality, do not overlap. Until the C++ FE is fixed,
5686 we will simply disable field-sensitivity for these cases. */
5687 notokay
= check_for_overlaps (fieldstack
);
5691 fieldstack
.release ();
5694 /* If we didn't end up collecting sub-variables create a full
5695 variable for the decl. */
5696 if (fieldstack
.length () <= 1
5697 || fieldstack
.length () > MAX_FIELDS_FOR_FIELD_SENSITIVE
)
5699 vi
= new_var_info (decl
, name
);
5701 vi
->may_have_pointers
= true;
5702 vi
->fullsize
= tree_to_uhwi (declsize
);
5703 vi
->size
= vi
->fullsize
;
5704 vi
->is_full_var
= true;
5705 fieldstack
.release ();
5709 vi
= new_var_info (decl
, name
);
5710 vi
->fullsize
= tree_to_uhwi (declsize
);
5711 for (i
= 0, newvi
= vi
;
5712 fieldstack
.iterate (i
, &fo
);
5713 ++i
, newvi
= vi_next (newvi
))
5715 const char *newname
= "NULL";
5720 asprintf (&tempname
, "%s." HOST_WIDE_INT_PRINT_DEC
5721 "+" HOST_WIDE_INT_PRINT_DEC
, name
, fo
->offset
, fo
->size
);
5722 newname
= ggc_strdup (tempname
);
5725 newvi
->name
= newname
;
5726 newvi
->offset
= fo
->offset
;
5727 newvi
->size
= fo
->size
;
5728 newvi
->fullsize
= vi
->fullsize
;
5729 newvi
->may_have_pointers
= fo
->may_have_pointers
;
5730 newvi
->only_restrict_pointers
= fo
->only_restrict_pointers
;
5731 if (i
+ 1 < fieldstack
.length ())
5733 varinfo_t tem
= new_var_info (decl
, name
);
5734 newvi
->next
= tem
->id
;
5743 create_variable_info_for (tree decl
, const char *name
)
5745 varinfo_t vi
= create_variable_info_for_1 (decl
, name
);
5746 unsigned int id
= vi
->id
;
5748 insert_vi_for_tree (decl
, vi
);
5750 if (TREE_CODE (decl
) != VAR_DECL
)
5753 /* Create initial constraints for globals. */
5754 for (; vi
; vi
= vi_next (vi
))
5756 if (!vi
->may_have_pointers
5757 || !vi
->is_global_var
)
5760 /* Mark global restrict qualified pointers. */
5761 if ((POINTER_TYPE_P (TREE_TYPE (decl
))
5762 && TYPE_RESTRICT (TREE_TYPE (decl
)))
5763 || vi
->only_restrict_pointers
)
5766 = make_constraint_from_global_restrict (vi
, "GLOBAL_RESTRICT");
5767 /* ??? For now exclude reads from globals as restrict sources
5768 if those are not (indirectly) from incoming parameters. */
5769 rvi
->is_restrict_var
= false;
5773 /* In non-IPA mode the initializer from nonlocal is all we need. */
5775 || DECL_HARD_REGISTER (decl
))
5776 make_copy_constraint (vi
, nonlocal_id
);
5778 /* In IPA mode parse the initializer and generate proper constraints
5782 varpool_node
*vnode
= varpool_node::get (decl
);
5784 /* For escaped variables initialize them from nonlocal. */
5785 if (!vnode
->all_refs_explicit_p ())
5786 make_copy_constraint (vi
, nonlocal_id
);
5788 /* If this is a global variable with an initializer and we are in
5789 IPA mode generate constraints for it. */
5790 if (vnode
->get_constructor ()
5791 && vnode
->definition
)
5793 auto_vec
<ce_s
> rhsc
;
5794 struct constraint_expr lhs
, *rhsp
;
5796 get_constraint_for_rhs (vnode
->get_constructor (), &rhsc
);
5800 FOR_EACH_VEC_ELT (rhsc
, i
, rhsp
)
5801 process_constraint (new_constraint (lhs
, *rhsp
));
5802 /* If this is a variable that escapes from the unit
5803 the initializer escapes as well. */
5804 if (!vnode
->all_refs_explicit_p ())
5806 lhs
.var
= escaped_id
;
5809 FOR_EACH_VEC_ELT (rhsc
, i
, rhsp
)
5810 process_constraint (new_constraint (lhs
, *rhsp
));
5819 /* Print out the points-to solution for VAR to FILE. */
5822 dump_solution_for_var (FILE *file
, unsigned int var
)
5824 varinfo_t vi
= get_varinfo (var
);
5828 /* Dump the solution for unified vars anyway, this avoids difficulties
5829 in scanning dumps in the testsuite. */
5830 fprintf (file
, "%s = { ", vi
->name
);
5831 vi
= get_varinfo (find (var
));
5832 EXECUTE_IF_SET_IN_BITMAP (vi
->solution
, 0, i
, bi
)
5833 fprintf (file
, "%s ", get_varinfo (i
)->name
);
5834 fprintf (file
, "}");
5836 /* But note when the variable was unified. */
5838 fprintf (file
, " same as %s", vi
->name
);
5840 fprintf (file
, "\n");
5843 /* Print the points-to solution for VAR to stderr. */
5846 debug_solution_for_var (unsigned int var
)
5848 dump_solution_for_var (stderr
, var
);
5851 /* Create varinfo structures for all of the variables in the
5852 function for intraprocedural mode. */
5855 intra_create_variable_infos (struct function
*fn
)
5859 /* For each incoming pointer argument arg, create the constraint ARG
5860 = NONLOCAL or a dummy variable if it is a restrict qualified
5861 passed-by-reference argument. */
5862 for (t
= DECL_ARGUMENTS (fn
->decl
); t
; t
= DECL_CHAIN (t
))
5864 varinfo_t p
= get_vi_for_tree (t
);
5866 /* For restrict qualified pointers to objects passed by
5867 reference build a real representative for the pointed-to object.
5868 Treat restrict qualified references the same. */
5869 if (TYPE_RESTRICT (TREE_TYPE (t
))
5870 && ((DECL_BY_REFERENCE (t
) && POINTER_TYPE_P (TREE_TYPE (t
)))
5871 || TREE_CODE (TREE_TYPE (t
)) == REFERENCE_TYPE
)
5872 && !type_contains_placeholder_p (TREE_TYPE (TREE_TYPE (t
))))
5874 struct constraint_expr lhsc
, rhsc
;
5876 tree heapvar
= build_fake_var_decl (TREE_TYPE (TREE_TYPE (t
)));
5877 DECL_EXTERNAL (heapvar
) = 1;
5878 vi
= create_variable_info_for_1 (heapvar
, "PARM_NOALIAS");
5879 vi
->is_restrict_var
= 1;
5880 insert_vi_for_tree (heapvar
, vi
);
5885 rhsc
.type
= ADDRESSOF
;
5887 process_constraint (new_constraint (lhsc
, rhsc
));
5888 for (; vi
; vi
= vi_next (vi
))
5889 if (vi
->may_have_pointers
)
5891 if (vi
->only_restrict_pointers
)
5892 make_constraint_from_global_restrict (vi
, "GLOBAL_RESTRICT");
5894 make_copy_constraint (vi
, nonlocal_id
);
5899 if (POINTER_TYPE_P (TREE_TYPE (t
))
5900 && TYPE_RESTRICT (TREE_TYPE (t
)))
5901 make_constraint_from_global_restrict (p
, "PARM_RESTRICT");
5904 for (; p
; p
= vi_next (p
))
5906 if (p
->only_restrict_pointers
)
5907 make_constraint_from_global_restrict (p
, "PARM_RESTRICT");
5908 else if (p
->may_have_pointers
)
5909 make_constraint_from (p
, nonlocal_id
);
5914 /* Add a constraint for a result decl that is passed by reference. */
5915 if (DECL_RESULT (fn
->decl
)
5916 && DECL_BY_REFERENCE (DECL_RESULT (fn
->decl
)))
5918 varinfo_t p
, result_vi
= get_vi_for_tree (DECL_RESULT (fn
->decl
));
5920 for (p
= result_vi
; p
; p
= vi_next (p
))
5921 make_constraint_from (p
, nonlocal_id
);
5924 /* Add a constraint for the incoming static chain parameter. */
5925 if (fn
->static_chain_decl
!= NULL_TREE
)
5927 varinfo_t p
, chain_vi
= get_vi_for_tree (fn
->static_chain_decl
);
5929 for (p
= chain_vi
; p
; p
= vi_next (p
))
5930 make_constraint_from (p
, nonlocal_id
);
5934 /* Structure used to put solution bitmaps in a hashtable so they can
5935 be shared among variables with the same points-to set. */
5937 typedef struct shared_bitmap_info
5941 } *shared_bitmap_info_t
;
5942 typedef const struct shared_bitmap_info
*const_shared_bitmap_info_t
;
5944 /* Shared_bitmap hashtable helpers. */
5946 struct shared_bitmap_hasher
: typed_free_remove
<shared_bitmap_info
>
5948 typedef shared_bitmap_info value_type
;
5949 typedef shared_bitmap_info compare_type
;
5950 static inline hashval_t
hash (const value_type
*);
5951 static inline bool equal (const value_type
*, const compare_type
*);
5954 /* Hash function for a shared_bitmap_info_t */
5957 shared_bitmap_hasher::hash (const value_type
*bi
)
5959 return bi
->hashcode
;
5962 /* Equality function for two shared_bitmap_info_t's. */
5965 shared_bitmap_hasher::equal (const value_type
*sbi1
, const compare_type
*sbi2
)
5967 return bitmap_equal_p (sbi1
->pt_vars
, sbi2
->pt_vars
);
5970 /* Shared_bitmap hashtable. */
5972 static hash_table
<shared_bitmap_hasher
> *shared_bitmap_table
;
5974 /* Lookup a bitmap in the shared bitmap hashtable, and return an already
5975 existing instance if there is one, NULL otherwise. */
5978 shared_bitmap_lookup (bitmap pt_vars
)
5980 shared_bitmap_info
**slot
;
5981 struct shared_bitmap_info sbi
;
5983 sbi
.pt_vars
= pt_vars
;
5984 sbi
.hashcode
= bitmap_hash (pt_vars
);
5986 slot
= shared_bitmap_table
->find_slot (&sbi
, NO_INSERT
);
5990 return (*slot
)->pt_vars
;
5994 /* Add a bitmap to the shared bitmap hashtable. */
5997 shared_bitmap_add (bitmap pt_vars
)
5999 shared_bitmap_info
**slot
;
6000 shared_bitmap_info_t sbi
= XNEW (struct shared_bitmap_info
);
6002 sbi
->pt_vars
= pt_vars
;
6003 sbi
->hashcode
= bitmap_hash (pt_vars
);
6005 slot
= shared_bitmap_table
->find_slot (sbi
, INSERT
);
6006 gcc_assert (!*slot
);
6011 /* Set bits in INTO corresponding to the variable uids in solution set FROM. */
6014 set_uids_in_ptset (bitmap into
, bitmap from
, struct pt_solution
*pt
)
6018 varinfo_t escaped_vi
= get_varinfo (find (escaped_id
));
6019 bool everything_escaped
6020 = escaped_vi
->solution
&& bitmap_bit_p (escaped_vi
->solution
, anything_id
);
6022 EXECUTE_IF_SET_IN_BITMAP (from
, 0, i
, bi
)
6024 varinfo_t vi
= get_varinfo (i
);
6026 /* The only artificial variables that are allowed in a may-alias
6027 set are heap variables. */
6028 if (vi
->is_artificial_var
&& !vi
->is_heap_var
)
6031 if (everything_escaped
6032 || (escaped_vi
->solution
6033 && bitmap_bit_p (escaped_vi
->solution
, i
)))
6035 pt
->vars_contains_escaped
= true;
6036 pt
->vars_contains_escaped_heap
= vi
->is_heap_var
;
6039 if (TREE_CODE (vi
->decl
) == VAR_DECL
6040 || TREE_CODE (vi
->decl
) == PARM_DECL
6041 || TREE_CODE (vi
->decl
) == RESULT_DECL
)
6043 /* If we are in IPA mode we will not recompute points-to
6044 sets after inlining so make sure they stay valid. */
6046 && !DECL_PT_UID_SET_P (vi
->decl
))
6047 SET_DECL_PT_UID (vi
->decl
, DECL_UID (vi
->decl
));
6049 /* Add the decl to the points-to set. Note that the points-to
6050 set contains global variables. */
6051 bitmap_set_bit (into
, DECL_PT_UID (vi
->decl
));
6052 if (vi
->is_global_var
)
6053 pt
->vars_contains_nonlocal
= true;
6059 /* Compute the points-to solution *PT for the variable VI. */
6061 static struct pt_solution
6062 find_what_var_points_to (varinfo_t orig_vi
)
6066 bitmap finished_solution
;
6069 struct pt_solution
*pt
;
6071 /* This variable may have been collapsed, let's get the real
6073 vi
= get_varinfo (find (orig_vi
->id
));
6075 /* See if we have already computed the solution and return it. */
6076 pt_solution
**slot
= &final_solutions
->get_or_insert (vi
);
6080 *slot
= pt
= XOBNEW (&final_solutions_obstack
, struct pt_solution
);
6081 memset (pt
, 0, sizeof (struct pt_solution
));
6083 /* Translate artificial variables into SSA_NAME_PTR_INFO
6085 EXECUTE_IF_SET_IN_BITMAP (vi
->solution
, 0, i
, bi
)
6087 varinfo_t vi
= get_varinfo (i
);
6089 if (vi
->is_artificial_var
)
6091 if (vi
->id
== nothing_id
)
6093 else if (vi
->id
== escaped_id
)
6096 pt
->ipa_escaped
= 1;
6099 /* Expand some special vars of ESCAPED in-place here. */
6100 varinfo_t evi
= get_varinfo (find (escaped_id
));
6101 if (bitmap_bit_p (evi
->solution
, nonlocal_id
))
6104 else if (vi
->id
== nonlocal_id
)
6106 else if (vi
->is_heap_var
)
6107 /* We represent heapvars in the points-to set properly. */
6109 else if (vi
->id
== string_id
)
6110 /* Nobody cares - STRING_CSTs are read-only entities. */
6112 else if (vi
->id
== anything_id
6113 || vi
->id
== integer_id
)
6118 /* Instead of doing extra work, simply do not create
6119 elaborate points-to information for pt_anything pointers. */
6123 /* Share the final set of variables when possible. */
6124 finished_solution
= BITMAP_GGC_ALLOC ();
6125 stats
.points_to_sets_created
++;
6127 set_uids_in_ptset (finished_solution
, vi
->solution
, pt
);
6128 result
= shared_bitmap_lookup (finished_solution
);
6131 shared_bitmap_add (finished_solution
);
6132 pt
->vars
= finished_solution
;
6137 bitmap_clear (finished_solution
);
6143 /* Given a pointer variable P, fill in its points-to set. */
6146 find_what_p_points_to (tree p
)
6148 struct ptr_info_def
*pi
;
6152 /* For parameters, get at the points-to set for the actual parm
6154 if (TREE_CODE (p
) == SSA_NAME
6155 && SSA_NAME_IS_DEFAULT_DEF (p
)
6156 && (TREE_CODE (SSA_NAME_VAR (p
)) == PARM_DECL
6157 || TREE_CODE (SSA_NAME_VAR (p
)) == RESULT_DECL
))
6158 lookup_p
= SSA_NAME_VAR (p
);
6160 vi
= lookup_vi_for_tree (lookup_p
);
6164 pi
= get_ptr_info (p
);
6165 pi
->pt
= find_what_var_points_to (vi
);
6169 /* Query statistics for points-to solutions. */
6172 unsigned HOST_WIDE_INT pt_solution_includes_may_alias
;
6173 unsigned HOST_WIDE_INT pt_solution_includes_no_alias
;
6174 unsigned HOST_WIDE_INT pt_solutions_intersect_may_alias
;
6175 unsigned HOST_WIDE_INT pt_solutions_intersect_no_alias
;
6179 dump_pta_stats (FILE *s
)
6181 fprintf (s
, "\nPTA query stats:\n");
6182 fprintf (s
, " pt_solution_includes: "
6183 HOST_WIDE_INT_PRINT_DEC
" disambiguations, "
6184 HOST_WIDE_INT_PRINT_DEC
" queries\n",
6185 pta_stats
.pt_solution_includes_no_alias
,
6186 pta_stats
.pt_solution_includes_no_alias
6187 + pta_stats
.pt_solution_includes_may_alias
);
6188 fprintf (s
, " pt_solutions_intersect: "
6189 HOST_WIDE_INT_PRINT_DEC
" disambiguations, "
6190 HOST_WIDE_INT_PRINT_DEC
" queries\n",
6191 pta_stats
.pt_solutions_intersect_no_alias
,
6192 pta_stats
.pt_solutions_intersect_no_alias
6193 + pta_stats
.pt_solutions_intersect_may_alias
);
6197 /* Reset the points-to solution *PT to a conservative default
6198 (point to anything). */
6201 pt_solution_reset (struct pt_solution
*pt
)
6203 memset (pt
, 0, sizeof (struct pt_solution
));
6204 pt
->anything
= true;
6207 /* Set the points-to solution *PT to point only to the variables
6208 in VARS. VARS_CONTAINS_GLOBAL specifies whether that contains
6209 global variables and VARS_CONTAINS_RESTRICT specifies whether
6210 it contains restrict tag variables. */
6213 pt_solution_set (struct pt_solution
*pt
, bitmap vars
,
6214 bool vars_contains_nonlocal
)
6216 memset (pt
, 0, sizeof (struct pt_solution
));
6218 pt
->vars_contains_nonlocal
= vars_contains_nonlocal
;
6219 pt
->vars_contains_escaped
6220 = (cfun
->gimple_df
->escaped
.anything
6221 || bitmap_intersect_p (cfun
->gimple_df
->escaped
.vars
, vars
));
6224 /* Set the points-to solution *PT to point only to the variable VAR. */
6227 pt_solution_set_var (struct pt_solution
*pt
, tree var
)
6229 memset (pt
, 0, sizeof (struct pt_solution
));
6230 pt
->vars
= BITMAP_GGC_ALLOC ();
6231 bitmap_set_bit (pt
->vars
, DECL_PT_UID (var
));
6232 pt
->vars_contains_nonlocal
= is_global_var (var
);
6233 pt
->vars_contains_escaped
6234 = (cfun
->gimple_df
->escaped
.anything
6235 || bitmap_bit_p (cfun
->gimple_df
->escaped
.vars
, DECL_PT_UID (var
)));
6238 /* Computes the union of the points-to solutions *DEST and *SRC and
6239 stores the result in *DEST. This changes the points-to bitmap
6240 of *DEST and thus may not be used if that might be shared.
6241 The points-to bitmap of *SRC and *DEST will not be shared after
6242 this function if they were not before. */
6245 pt_solution_ior_into (struct pt_solution
*dest
, struct pt_solution
*src
)
6247 dest
->anything
|= src
->anything
;
6250 pt_solution_reset (dest
);
6254 dest
->nonlocal
|= src
->nonlocal
;
6255 dest
->escaped
|= src
->escaped
;
6256 dest
->ipa_escaped
|= src
->ipa_escaped
;
6257 dest
->null
|= src
->null
;
6258 dest
->vars_contains_nonlocal
|= src
->vars_contains_nonlocal
;
6259 dest
->vars_contains_escaped
|= src
->vars_contains_escaped
;
6260 dest
->vars_contains_escaped_heap
|= src
->vars_contains_escaped_heap
;
6265 dest
->vars
= BITMAP_GGC_ALLOC ();
6266 bitmap_ior_into (dest
->vars
, src
->vars
);
6269 /* Return true if the points-to solution *PT is empty. */
6272 pt_solution_empty_p (struct pt_solution
*pt
)
6279 && !bitmap_empty_p (pt
->vars
))
6282 /* If the solution includes ESCAPED, check if that is empty. */
6284 && !pt_solution_empty_p (&cfun
->gimple_df
->escaped
))
6287 /* If the solution includes ESCAPED, check if that is empty. */
6289 && !pt_solution_empty_p (&ipa_escaped_pt
))
6295 /* Return true if the points-to solution *PT only point to a single var, and
6296 return the var uid in *UID. */
6299 pt_solution_singleton_p (struct pt_solution
*pt
, unsigned *uid
)
6301 if (pt
->anything
|| pt
->nonlocal
|| pt
->escaped
|| pt
->ipa_escaped
6302 || pt
->null
|| pt
->vars
== NULL
6303 || !bitmap_single_bit_set_p (pt
->vars
))
6306 *uid
= bitmap_first_set_bit (pt
->vars
);
6310 /* Return true if the points-to solution *PT includes global memory. */
6313 pt_solution_includes_global (struct pt_solution
*pt
)
6317 || pt
->vars_contains_nonlocal
6318 /* The following is a hack to make the malloc escape hack work.
6319 In reality we'd need different sets for escaped-through-return
6320 and escaped-to-callees and passes would need to be updated. */
6321 || pt
->vars_contains_escaped_heap
)
6324 /* 'escaped' is also a placeholder so we have to look into it. */
6326 return pt_solution_includes_global (&cfun
->gimple_df
->escaped
);
6328 if (pt
->ipa_escaped
)
6329 return pt_solution_includes_global (&ipa_escaped_pt
);
6331 /* ??? This predicate is not correct for the IPA-PTA solution
6332 as we do not properly distinguish between unit escape points
6333 and global variables. */
6334 if (cfun
->gimple_df
->ipa_pta
)
6340 /* Return true if the points-to solution *PT includes the variable
6341 declaration DECL. */
6344 pt_solution_includes_1 (struct pt_solution
*pt
, const_tree decl
)
6350 && is_global_var (decl
))
6354 && bitmap_bit_p (pt
->vars
, DECL_PT_UID (decl
)))
6357 /* If the solution includes ESCAPED, check it. */
6359 && pt_solution_includes_1 (&cfun
->gimple_df
->escaped
, decl
))
6362 /* If the solution includes ESCAPED, check it. */
6364 && pt_solution_includes_1 (&ipa_escaped_pt
, decl
))
6371 pt_solution_includes (struct pt_solution
*pt
, const_tree decl
)
6373 bool res
= pt_solution_includes_1 (pt
, decl
);
6375 ++pta_stats
.pt_solution_includes_may_alias
;
6377 ++pta_stats
.pt_solution_includes_no_alias
;
6381 /* Return true if both points-to solutions PT1 and PT2 have a non-empty
6385 pt_solutions_intersect_1 (struct pt_solution
*pt1
, struct pt_solution
*pt2
)
6387 if (pt1
->anything
|| pt2
->anything
)
6390 /* If either points to unknown global memory and the other points to
6391 any global memory they alias. */
6394 || pt2
->vars_contains_nonlocal
))
6396 && pt1
->vars_contains_nonlocal
))
6399 /* If either points to all escaped memory and the other points to
6400 any escaped memory they alias. */
6403 || pt2
->vars_contains_escaped
))
6405 && pt1
->vars_contains_escaped
))
6408 /* Check the escaped solution if required.
6409 ??? Do we need to check the local against the IPA escaped sets? */
6410 if ((pt1
->ipa_escaped
|| pt2
->ipa_escaped
)
6411 && !pt_solution_empty_p (&ipa_escaped_pt
))
6413 /* If both point to escaped memory and that solution
6414 is not empty they alias. */
6415 if (pt1
->ipa_escaped
&& pt2
->ipa_escaped
)
6418 /* If either points to escaped memory see if the escaped solution
6419 intersects with the other. */
6420 if ((pt1
->ipa_escaped
6421 && pt_solutions_intersect_1 (&ipa_escaped_pt
, pt2
))
6422 || (pt2
->ipa_escaped
6423 && pt_solutions_intersect_1 (&ipa_escaped_pt
, pt1
)))
6427 /* Now both pointers alias if their points-to solution intersects. */
6430 && bitmap_intersect_p (pt1
->vars
, pt2
->vars
));
6434 pt_solutions_intersect (struct pt_solution
*pt1
, struct pt_solution
*pt2
)
6436 bool res
= pt_solutions_intersect_1 (pt1
, pt2
);
6438 ++pta_stats
.pt_solutions_intersect_may_alias
;
6440 ++pta_stats
.pt_solutions_intersect_no_alias
;
6445 /* Dump points-to information to OUTFILE. */
6448 dump_sa_points_to_info (FILE *outfile
)
6452 fprintf (outfile
, "\nPoints-to sets\n\n");
6454 if (dump_flags
& TDF_STATS
)
6456 fprintf (outfile
, "Stats:\n");
6457 fprintf (outfile
, "Total vars: %d\n", stats
.total_vars
);
6458 fprintf (outfile
, "Non-pointer vars: %d\n",
6459 stats
.nonpointer_vars
);
6460 fprintf (outfile
, "Statically unified vars: %d\n",
6461 stats
.unified_vars_static
);
6462 fprintf (outfile
, "Dynamically unified vars: %d\n",
6463 stats
.unified_vars_dynamic
);
6464 fprintf (outfile
, "Iterations: %d\n", stats
.iterations
);
6465 fprintf (outfile
, "Number of edges: %d\n", stats
.num_edges
);
6466 fprintf (outfile
, "Number of implicit edges: %d\n",
6467 stats
.num_implicit_edges
);
6470 for (i
= 1; i
< varmap
.length (); i
++)
6472 varinfo_t vi
= get_varinfo (i
);
6473 if (!vi
->may_have_pointers
)
6475 dump_solution_for_var (outfile
, i
);
6480 /* Debug points-to information to stderr. */
6483 debug_sa_points_to_info (void)
6485 dump_sa_points_to_info (stderr
);
6489 /* Initialize the always-existing constraint variables for NULL
6490 ANYTHING, READONLY, and INTEGER */
6493 init_base_vars (void)
6495 struct constraint_expr lhs
, rhs
;
6496 varinfo_t var_anything
;
6497 varinfo_t var_nothing
;
6498 varinfo_t var_string
;
6499 varinfo_t var_escaped
;
6500 varinfo_t var_nonlocal
;
6501 varinfo_t var_storedanything
;
6502 varinfo_t var_integer
;
6504 /* Variable ID zero is reserved and should be NULL. */
6505 varmap
.safe_push (NULL
);
6507 /* Create the NULL variable, used to represent that a variable points
6509 var_nothing
= new_var_info (NULL_TREE
, "NULL");
6510 gcc_assert (var_nothing
->id
== nothing_id
);
6511 var_nothing
->is_artificial_var
= 1;
6512 var_nothing
->offset
= 0;
6513 var_nothing
->size
= ~0;
6514 var_nothing
->fullsize
= ~0;
6515 var_nothing
->is_special_var
= 1;
6516 var_nothing
->may_have_pointers
= 0;
6517 var_nothing
->is_global_var
= 0;
6519 /* Create the ANYTHING variable, used to represent that a variable
6520 points to some unknown piece of memory. */
6521 var_anything
= new_var_info (NULL_TREE
, "ANYTHING");
6522 gcc_assert (var_anything
->id
== anything_id
);
6523 var_anything
->is_artificial_var
= 1;
6524 var_anything
->size
= ~0;
6525 var_anything
->offset
= 0;
6526 var_anything
->fullsize
= ~0;
6527 var_anything
->is_special_var
= 1;
6529 /* Anything points to anything. This makes deref constraints just
6530 work in the presence of linked list and other p = *p type loops,
6531 by saying that *ANYTHING = ANYTHING. */
6533 lhs
.var
= anything_id
;
6535 rhs
.type
= ADDRESSOF
;
6536 rhs
.var
= anything_id
;
6539 /* This specifically does not use process_constraint because
6540 process_constraint ignores all anything = anything constraints, since all
6541 but this one are redundant. */
6542 constraints
.safe_push (new_constraint (lhs
, rhs
));
6544 /* Create the STRING variable, used to represent that a variable
6545 points to a string literal. String literals don't contain
6546 pointers so STRING doesn't point to anything. */
6547 var_string
= new_var_info (NULL_TREE
, "STRING");
6548 gcc_assert (var_string
->id
== string_id
);
6549 var_string
->is_artificial_var
= 1;
6550 var_string
->offset
= 0;
6551 var_string
->size
= ~0;
6552 var_string
->fullsize
= ~0;
6553 var_string
->is_special_var
= 1;
6554 var_string
->may_have_pointers
= 0;
6556 /* Create the ESCAPED variable, used to represent the set of escaped
6558 var_escaped
= new_var_info (NULL_TREE
, "ESCAPED");
6559 gcc_assert (var_escaped
->id
== escaped_id
);
6560 var_escaped
->is_artificial_var
= 1;
6561 var_escaped
->offset
= 0;
6562 var_escaped
->size
= ~0;
6563 var_escaped
->fullsize
= ~0;
6564 var_escaped
->is_special_var
= 0;
6566 /* Create the NONLOCAL variable, used to represent the set of nonlocal
6568 var_nonlocal
= new_var_info (NULL_TREE
, "NONLOCAL");
6569 gcc_assert (var_nonlocal
->id
== nonlocal_id
);
6570 var_nonlocal
->is_artificial_var
= 1;
6571 var_nonlocal
->offset
= 0;
6572 var_nonlocal
->size
= ~0;
6573 var_nonlocal
->fullsize
= ~0;
6574 var_nonlocal
->is_special_var
= 1;
6576 /* ESCAPED = *ESCAPED, because escaped is may-deref'd at calls, etc. */
6578 lhs
.var
= escaped_id
;
6581 rhs
.var
= escaped_id
;
6583 process_constraint (new_constraint (lhs
, rhs
));
6585 /* ESCAPED = ESCAPED + UNKNOWN_OFFSET, because if a sub-field escapes the
6586 whole variable escapes. */
6588 lhs
.var
= escaped_id
;
6591 rhs
.var
= escaped_id
;
6592 rhs
.offset
= UNKNOWN_OFFSET
;
6593 process_constraint (new_constraint (lhs
, rhs
));
6595 /* *ESCAPED = NONLOCAL. This is true because we have to assume
6596 everything pointed to by escaped points to what global memory can
6599 lhs
.var
= escaped_id
;
6602 rhs
.var
= nonlocal_id
;
6604 process_constraint (new_constraint (lhs
, rhs
));
6606 /* NONLOCAL = &NONLOCAL, NONLOCAL = &ESCAPED. This is true because
6607 global memory may point to global memory and escaped memory. */
6609 lhs
.var
= nonlocal_id
;
6611 rhs
.type
= ADDRESSOF
;
6612 rhs
.var
= nonlocal_id
;
6614 process_constraint (new_constraint (lhs
, rhs
));
6615 rhs
.type
= ADDRESSOF
;
6616 rhs
.var
= escaped_id
;
6618 process_constraint (new_constraint (lhs
, rhs
));
6620 /* Create the STOREDANYTHING variable, used to represent the set of
6621 variables stored to *ANYTHING. */
6622 var_storedanything
= new_var_info (NULL_TREE
, "STOREDANYTHING");
6623 gcc_assert (var_storedanything
->id
== storedanything_id
);
6624 var_storedanything
->is_artificial_var
= 1;
6625 var_storedanything
->offset
= 0;
6626 var_storedanything
->size
= ~0;
6627 var_storedanything
->fullsize
= ~0;
6628 var_storedanything
->is_special_var
= 0;
6630 /* Create the INTEGER variable, used to represent that a variable points
6631 to what an INTEGER "points to". */
6632 var_integer
= new_var_info (NULL_TREE
, "INTEGER");
6633 gcc_assert (var_integer
->id
== integer_id
);
6634 var_integer
->is_artificial_var
= 1;
6635 var_integer
->size
= ~0;
6636 var_integer
->fullsize
= ~0;
6637 var_integer
->offset
= 0;
6638 var_integer
->is_special_var
= 1;
6640 /* INTEGER = ANYTHING, because we don't know where a dereference of
6641 a random integer will point to. */
6643 lhs
.var
= integer_id
;
6645 rhs
.type
= ADDRESSOF
;
6646 rhs
.var
= anything_id
;
6648 process_constraint (new_constraint (lhs
, rhs
));
6651 /* Initialize things necessary to perform PTA */
6654 init_alias_vars (void)
6656 use_field_sensitive
= (MAX_FIELDS_FOR_FIELD_SENSITIVE
> 1);
6658 bitmap_obstack_initialize (&pta_obstack
);
6659 bitmap_obstack_initialize (&oldpta_obstack
);
6660 bitmap_obstack_initialize (&predbitmap_obstack
);
6662 constraint_pool
= create_alloc_pool ("Constraint pool",
6663 sizeof (struct constraint
), 30);
6664 variable_info_pool
= create_alloc_pool ("Variable info pool",
6665 sizeof (struct variable_info
), 30);
6666 constraints
.create (8);
6668 vi_for_tree
= new hash_map
<tree
, varinfo_t
>;
6669 call_stmt_vars
= new hash_map
<gimple
, varinfo_t
>;
6671 memset (&stats
, 0, sizeof (stats
));
6672 shared_bitmap_table
= new hash_table
<shared_bitmap_hasher
> (511);
6675 gcc_obstack_init (&fake_var_decl_obstack
);
6677 final_solutions
= new hash_map
<varinfo_t
, pt_solution
*>;
6678 gcc_obstack_init (&final_solutions_obstack
);
6681 /* Remove the REF and ADDRESS edges from GRAPH, as well as all the
6682 predecessor edges. */
6685 remove_preds_and_fake_succs (constraint_graph_t graph
)
6689 /* Clear the implicit ref and address nodes from the successor
6691 for (i
= 1; i
< FIRST_REF_NODE
; i
++)
6693 if (graph
->succs
[i
])
6694 bitmap_clear_range (graph
->succs
[i
], FIRST_REF_NODE
,
6695 FIRST_REF_NODE
* 2);
6698 /* Free the successor list for the non-ref nodes. */
6699 for (i
= FIRST_REF_NODE
+ 1; i
< graph
->size
; i
++)
6701 if (graph
->succs
[i
])
6702 BITMAP_FREE (graph
->succs
[i
]);
6705 /* Now reallocate the size of the successor list as, and blow away
6706 the predecessor bitmaps. */
6707 graph
->size
= varmap
.length ();
6708 graph
->succs
= XRESIZEVEC (bitmap
, graph
->succs
, graph
->size
);
6710 free (graph
->implicit_preds
);
6711 graph
->implicit_preds
= NULL
;
6712 free (graph
->preds
);
6713 graph
->preds
= NULL
;
6714 bitmap_obstack_release (&predbitmap_obstack
);
6717 /* Solve the constraint set. */
6720 solve_constraints (void)
6722 struct scc_info
*si
;
6726 "\nCollapsing static cycles and doing variable "
6729 init_graph (varmap
.length () * 2);
6732 fprintf (dump_file
, "Building predecessor graph\n");
6733 build_pred_graph ();
6736 fprintf (dump_file
, "Detecting pointer and location "
6738 si
= perform_var_substitution (graph
);
6741 fprintf (dump_file
, "Rewriting constraints and unifying "
6743 rewrite_constraints (graph
, si
);
6745 build_succ_graph ();
6747 free_var_substitution_info (si
);
6749 /* Attach complex constraints to graph nodes. */
6750 move_complex_constraints (graph
);
6753 fprintf (dump_file
, "Uniting pointer but not location equivalent "
6755 unite_pointer_equivalences (graph
);
6758 fprintf (dump_file
, "Finding indirect cycles\n");
6759 find_indirect_cycles (graph
);
6761 /* Implicit nodes and predecessors are no longer necessary at this
6763 remove_preds_and_fake_succs (graph
);
6765 if (dump_file
&& (dump_flags
& TDF_GRAPH
))
6767 fprintf (dump_file
, "\n\n// The constraint graph before solve-graph "
6768 "in dot format:\n");
6769 dump_constraint_graph (dump_file
);
6770 fprintf (dump_file
, "\n\n");
6774 fprintf (dump_file
, "Solving graph\n");
6776 solve_graph (graph
);
6778 if (dump_file
&& (dump_flags
& TDF_GRAPH
))
6780 fprintf (dump_file
, "\n\n// The constraint graph after solve-graph "
6781 "in dot format:\n");
6782 dump_constraint_graph (dump_file
);
6783 fprintf (dump_file
, "\n\n");
6787 dump_sa_points_to_info (dump_file
);
6790 /* Create points-to sets for the current function. See the comments
6791 at the start of the file for an algorithmic overview. */
6794 compute_points_to_sets (void)
6800 timevar_push (TV_TREE_PTA
);
6804 intra_create_variable_infos (cfun
);
6806 /* Now walk all statements and build the constraint set. */
6807 FOR_EACH_BB_FN (bb
, cfun
)
6809 for (gphi_iterator gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
);
6812 gphi
*phi
= gsi
.phi ();
6814 if (! virtual_operand_p (gimple_phi_result (phi
)))
6815 find_func_aliases (cfun
, phi
);
6818 for (gimple_stmt_iterator gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
);
6821 gimple stmt
= gsi_stmt (gsi
);
6823 find_func_aliases (cfun
, stmt
);
6829 fprintf (dump_file
, "Points-to analysis\n\nConstraints:\n\n");
6830 dump_constraints (dump_file
, 0);
6833 /* From the constraints compute the points-to sets. */
6834 solve_constraints ();
6836 /* Compute the points-to set for ESCAPED used for call-clobber analysis. */
6837 cfun
->gimple_df
->escaped
= find_what_var_points_to (get_varinfo (escaped_id
));
6839 /* Make sure the ESCAPED solution (which is used as placeholder in
6840 other solutions) does not reference itself. This simplifies
6841 points-to solution queries. */
6842 cfun
->gimple_df
->escaped
.escaped
= 0;
6844 /* Compute the points-to sets for pointer SSA_NAMEs. */
6845 for (i
= 0; i
< num_ssa_names
; ++i
)
6847 tree ptr
= ssa_name (i
);
6849 && POINTER_TYPE_P (TREE_TYPE (ptr
)))
6850 find_what_p_points_to (ptr
);
6853 /* Compute the call-used/clobbered sets. */
6854 FOR_EACH_BB_FN (bb
, cfun
)
6856 gimple_stmt_iterator gsi
;
6858 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
6861 struct pt_solution
*pt
;
6863 stmt
= dyn_cast
<gcall
*> (gsi_stmt (gsi
));
6867 pt
= gimple_call_use_set (stmt
);
6868 if (gimple_call_flags (stmt
) & ECF_CONST
)
6869 memset (pt
, 0, sizeof (struct pt_solution
));
6870 else if ((vi
= lookup_call_use_vi (stmt
)) != NULL
)
6872 *pt
= find_what_var_points_to (vi
);
6873 /* Escaped (and thus nonlocal) variables are always
6874 implicitly used by calls. */
6875 /* ??? ESCAPED can be empty even though NONLOCAL
6882 /* If there is nothing special about this call then
6883 we have made everything that is used also escape. */
6884 *pt
= cfun
->gimple_df
->escaped
;
6888 pt
= gimple_call_clobber_set (stmt
);
6889 if (gimple_call_flags (stmt
) & (ECF_CONST
|ECF_PURE
|ECF_NOVOPS
))
6890 memset (pt
, 0, sizeof (struct pt_solution
));
6891 else if ((vi
= lookup_call_clobber_vi (stmt
)) != NULL
)
6893 *pt
= find_what_var_points_to (vi
);
6894 /* Escaped (and thus nonlocal) variables are always
6895 implicitly clobbered by calls. */
6896 /* ??? ESCAPED can be empty even though NONLOCAL
6903 /* If there is nothing special about this call then
6904 we have made everything that is used also escape. */
6905 *pt
= cfun
->gimple_df
->escaped
;
6911 timevar_pop (TV_TREE_PTA
);
6915 /* Delete created points-to sets. */
6918 delete_points_to_sets (void)
6922 delete shared_bitmap_table
;
6923 shared_bitmap_table
= NULL
;
6924 if (dump_file
&& (dump_flags
& TDF_STATS
))
6925 fprintf (dump_file
, "Points to sets created:%d\n",
6926 stats
.points_to_sets_created
);
6929 delete call_stmt_vars
;
6930 bitmap_obstack_release (&pta_obstack
);
6931 constraints
.release ();
6933 for (i
= 0; i
< graph
->size
; i
++)
6934 graph
->complex[i
].release ();
6935 free (graph
->complex);
6938 free (graph
->succs
);
6940 free (graph
->pe_rep
);
6941 free (graph
->indirect_cycles
);
6945 free_alloc_pool (variable_info_pool
);
6946 free_alloc_pool (constraint_pool
);
6948 obstack_free (&fake_var_decl_obstack
, NULL
);
6950 delete final_solutions
;
6951 obstack_free (&final_solutions_obstack
, NULL
);
6954 /* Mark "other" loads and stores as belonging to CLIQUE and with
6958 visit_loadstore (gimple
, tree base
, tree ref
, void *clique_
)
6960 unsigned short clique
= (uintptr_t)clique_
;
6961 if (TREE_CODE (base
) == MEM_REF
6962 || TREE_CODE (base
) == TARGET_MEM_REF
)
6964 tree ptr
= TREE_OPERAND (base
, 0);
6965 if (TREE_CODE (ptr
) == SSA_NAME
)
6967 /* ??? We need to make sure 'ptr' doesn't include any of
6968 the restrict tags in its points-to set. */
6972 /* For now let decls through. */
6974 /* Do not overwrite existing cliques (that includes clique, base
6975 pairs we just set). */
6976 if (MR_DEPENDENCE_CLIQUE (base
) == 0)
6978 MR_DEPENDENCE_CLIQUE (base
) = clique
;
6979 MR_DEPENDENCE_BASE (base
) = 0;
6983 /* For plain decl accesses see whether they are accesses to globals
6984 and rewrite them to MEM_REFs with { clique, 0 }. */
6985 if (TREE_CODE (base
) == VAR_DECL
6986 && is_global_var (base
)
6987 /* ??? We can't rewrite a plain decl with the walk_stmt_load_store
6992 while (handled_component_p (*basep
))
6993 basep
= &TREE_OPERAND (*basep
, 0);
6994 gcc_assert (TREE_CODE (*basep
) == VAR_DECL
);
6995 tree ptr
= build_fold_addr_expr (*basep
);
6996 tree zero
= build_int_cst (TREE_TYPE (ptr
), 0);
6997 *basep
= build2 (MEM_REF
, TREE_TYPE (*basep
), ptr
, zero
);
6998 MR_DEPENDENCE_CLIQUE (*basep
) = clique
;
6999 MR_DEPENDENCE_BASE (*basep
) = 0;
7005 /* If REF is a MEM_REF then assign a clique, base pair to it, updating
7006 CLIQUE, *RESTRICT_VAR and LAST_RUID. Return whether dependence info
7007 was assigned to REF. */
7010 maybe_set_dependence_info (tree ref
, tree ptr
,
7011 unsigned short &clique
, varinfo_t restrict_var
,
7012 unsigned short &last_ruid
)
7014 while (handled_component_p (ref
))
7015 ref
= TREE_OPERAND (ref
, 0);
7016 if ((TREE_CODE (ref
) == MEM_REF
7017 || TREE_CODE (ref
) == TARGET_MEM_REF
)
7018 && TREE_OPERAND (ref
, 0) == ptr
)
7020 /* Do not overwrite existing cliques. This avoids overwriting dependence
7021 info inlined from a function with restrict parameters inlined
7022 into a function with restrict parameters. This usually means we
7023 prefer to be precise in innermost loops. */
7024 if (MR_DEPENDENCE_CLIQUE (ref
) == 0)
7027 clique
= ++cfun
->last_clique
;
7028 if (restrict_var
->ruid
== 0)
7029 restrict_var
->ruid
= ++last_ruid
;
7030 MR_DEPENDENCE_CLIQUE (ref
) = clique
;
7031 MR_DEPENDENCE_BASE (ref
) = restrict_var
->ruid
;
7038 /* Compute the set of independend memory references based on restrict
7039 tags and their conservative propagation to the points-to sets. */
7042 compute_dependence_clique (void)
7044 unsigned short clique
= 0;
7045 unsigned short last_ruid
= 0;
7046 for (unsigned i
= 0; i
< num_ssa_names
; ++i
)
7048 tree ptr
= ssa_name (i
);
7049 if (!ptr
|| !POINTER_TYPE_P (TREE_TYPE (ptr
)))
7052 /* Avoid all this when ptr is not dereferenced? */
7054 if (SSA_NAME_IS_DEFAULT_DEF (ptr
)
7055 && (TREE_CODE (SSA_NAME_VAR (ptr
)) == PARM_DECL
7056 || TREE_CODE (SSA_NAME_VAR (ptr
)) == RESULT_DECL
))
7057 p
= SSA_NAME_VAR (ptr
);
7058 varinfo_t vi
= lookup_vi_for_tree (p
);
7061 vi
= get_varinfo (find (vi
->id
));
7064 varinfo_t restrict_var
= NULL
;
7065 EXECUTE_IF_SET_IN_BITMAP (vi
->solution
, 0, j
, bi
)
7067 varinfo_t oi
= get_varinfo (j
);
7068 if (oi
->is_restrict_var
)
7072 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7074 fprintf (dump_file
, "found restrict pointed-to "
7076 print_generic_expr (dump_file
, ptr
, 0);
7077 fprintf (dump_file
, " but not exclusively\n");
7079 restrict_var
= NULL
;
7084 /* NULL is the only other valid points-to entry. */
7085 else if (oi
->id
!= nothing_id
)
7087 restrict_var
= NULL
;
7091 /* Ok, found that ptr must(!) point to a single(!) restrict
7093 /* ??? PTA isn't really a proper propagation engine to compute
7095 ??? We could handle merging of two restricts by unifying them. */
7098 /* Now look at possible dereferences of ptr. */
7099 imm_use_iterator ui
;
7101 FOR_EACH_IMM_USE_STMT (use_stmt
, ui
, ptr
)
7103 /* ??? Calls and asms. */
7104 if (!gimple_assign_single_p (use_stmt
))
7106 maybe_set_dependence_info (gimple_assign_lhs (use_stmt
), ptr
,
7107 clique
, restrict_var
, last_ruid
);
7108 maybe_set_dependence_info (gimple_assign_rhs1 (use_stmt
), ptr
,
7109 clique
, restrict_var
, last_ruid
);
7117 /* Assign the BASE id zero to all accesses not based on a restrict
7118 pointer. That way they get disabiguated against restrict
7119 accesses but not against each other. */
7120 /* ??? For restricts derived from globals (thus not incoming
7121 parameters) we can't restrict scoping properly thus the following
7122 is too aggressive there. For now we have excluded those globals from
7123 getting into the MR_DEPENDENCE machinery. */
7125 FOR_EACH_BB_FN (bb
, cfun
)
7126 for (gimple_stmt_iterator gsi
= gsi_start_bb (bb
);
7127 !gsi_end_p (gsi
); gsi_next (&gsi
))
7129 gimple stmt
= gsi_stmt (gsi
);
7130 walk_stmt_load_store_ops (stmt
, (void *)(uintptr_t)clique
,
7131 visit_loadstore
, visit_loadstore
);
7135 /* Compute points-to information for every SSA_NAME pointer in the
7136 current function and compute the transitive closure of escaped
7137 variables to re-initialize the call-clobber states of local variables. */
7140 compute_may_aliases (void)
7142 if (cfun
->gimple_df
->ipa_pta
)
7146 fprintf (dump_file
, "\nNot re-computing points-to information "
7147 "because IPA points-to information is available.\n\n");
7149 /* But still dump what we have remaining it. */
7150 dump_alias_info (dump_file
);
7156 /* For each pointer P_i, determine the sets of variables that P_i may
7157 point-to. Compute the reachability set of escaped and call-used
7159 compute_points_to_sets ();
7161 /* Debugging dumps. */
7163 dump_alias_info (dump_file
);
7165 /* Compute restrict-based memory disambiguations. */
7166 compute_dependence_clique ();
7168 /* Deallocate memory used by aliasing data structures and the internal
7169 points-to solution. */
7170 delete_points_to_sets ();
7172 gcc_assert (!need_ssa_update_p (cfun
));
7177 /* A dummy pass to cause points-to information to be computed via
7178 TODO_rebuild_alias. */
7182 const pass_data pass_data_build_alias
=
7184 GIMPLE_PASS
, /* type */
7186 OPTGROUP_NONE
, /* optinfo_flags */
7187 TV_NONE
, /* tv_id */
7188 ( PROP_cfg
| PROP_ssa
), /* properties_required */
7189 0, /* properties_provided */
7190 0, /* properties_destroyed */
7191 0, /* todo_flags_start */
7192 TODO_rebuild_alias
, /* todo_flags_finish */
7195 class pass_build_alias
: public gimple_opt_pass
7198 pass_build_alias (gcc::context
*ctxt
)
7199 : gimple_opt_pass (pass_data_build_alias
, ctxt
)
7202 /* opt_pass methods: */
7203 virtual bool gate (function
*) { return flag_tree_pta
; }
7205 }; // class pass_build_alias
7210 make_pass_build_alias (gcc::context
*ctxt
)
7212 return new pass_build_alias (ctxt
);
7215 /* A dummy pass to cause points-to information to be computed via
7216 TODO_rebuild_alias. */
7220 const pass_data pass_data_build_ealias
=
7222 GIMPLE_PASS
, /* type */
7223 "ealias", /* name */
7224 OPTGROUP_NONE
, /* optinfo_flags */
7225 TV_NONE
, /* tv_id */
7226 ( PROP_cfg
| PROP_ssa
), /* properties_required */
7227 0, /* properties_provided */
7228 0, /* properties_destroyed */
7229 0, /* todo_flags_start */
7230 TODO_rebuild_alias
, /* todo_flags_finish */
7233 class pass_build_ealias
: public gimple_opt_pass
7236 pass_build_ealias (gcc::context
*ctxt
)
7237 : gimple_opt_pass (pass_data_build_ealias
, ctxt
)
7240 /* opt_pass methods: */
7241 virtual bool gate (function
*) { return flag_tree_pta
; }
7243 }; // class pass_build_ealias
7248 make_pass_build_ealias (gcc::context
*ctxt
)
7250 return new pass_build_ealias (ctxt
);
7254 /* IPA PTA solutions for ESCAPED. */
7255 struct pt_solution ipa_escaped_pt
7256 = { true, false, false, false, false, false, false, false, NULL
};
7258 /* Associate node with varinfo DATA. Worker for
7259 cgraph_for_node_and_aliases. */
7261 associate_varinfo_to_alias (struct cgraph_node
*node
, void *data
)
7263 if ((node
->alias
|| node
->thunk
.thunk_p
)
7265 insert_vi_for_tree (node
->decl
, (varinfo_t
)data
);
7269 /* Execute the driver for IPA PTA. */
7271 ipa_pta_execute (void)
7273 struct cgraph_node
*node
;
7281 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
7283 symtab_node::dump_table (dump_file
);
7284 fprintf (dump_file
, "\n");
7287 /* Build the constraints. */
7288 FOR_EACH_DEFINED_FUNCTION (node
)
7291 /* Nodes without a body are not interesting. Especially do not
7292 visit clones at this point for now - we get duplicate decls
7293 there for inline clones at least. */
7294 if (!node
->has_gimple_body_p () || node
->global
.inlined_to
)
7298 gcc_assert (!node
->clone_of
);
7300 vi
= create_function_info_for (node
->decl
,
7301 alias_get_name (node
->decl
));
7302 node
->call_for_symbol_thunks_and_aliases
7303 (associate_varinfo_to_alias
, vi
, true);
7306 /* Create constraints for global variables and their initializers. */
7307 FOR_EACH_VARIABLE (var
)
7309 if (var
->alias
&& var
->analyzed
)
7312 get_vi_for_tree (var
->decl
);
7318 "Generating constraints for global initializers\n\n");
7319 dump_constraints (dump_file
, 0);
7320 fprintf (dump_file
, "\n");
7322 from
= constraints
.length ();
7324 FOR_EACH_DEFINED_FUNCTION (node
)
7326 struct function
*func
;
7329 /* Nodes without a body are not interesting. */
7330 if (!node
->has_gimple_body_p () || node
->clone_of
)
7336 "Generating constraints for %s", node
->name ());
7337 if (DECL_ASSEMBLER_NAME_SET_P (node
->decl
))
7338 fprintf (dump_file
, " (%s)",
7340 (DECL_ASSEMBLER_NAME (node
->decl
)));
7341 fprintf (dump_file
, "\n");
7344 func
= DECL_STRUCT_FUNCTION (node
->decl
);
7345 gcc_assert (cfun
== NULL
);
7347 /* For externally visible or attribute used annotated functions use
7348 local constraints for their arguments.
7349 For local functions we see all callers and thus do not need initial
7350 constraints for parameters. */
7351 if (node
->used_from_other_partition
7352 || node
->externally_visible
7353 || node
->force_output
)
7355 intra_create_variable_infos (func
);
7357 /* We also need to make function return values escape. Nothing
7358 escapes by returning from main though. */
7359 if (!MAIN_NAME_P (DECL_NAME (node
->decl
)))
7362 fi
= lookup_vi_for_tree (node
->decl
);
7363 rvi
= first_vi_for_offset (fi
, fi_result
);
7364 if (rvi
&& rvi
->offset
== fi_result
)
7366 struct constraint_expr includes
;
7367 struct constraint_expr var
;
7368 includes
.var
= escaped_id
;
7369 includes
.offset
= 0;
7370 includes
.type
= SCALAR
;
7374 process_constraint (new_constraint (includes
, var
));
7379 /* Build constriants for the function body. */
7380 FOR_EACH_BB_FN (bb
, func
)
7382 for (gphi_iterator gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
);
7385 gphi
*phi
= gsi
.phi ();
7387 if (! virtual_operand_p (gimple_phi_result (phi
)))
7388 find_func_aliases (func
, phi
);
7391 for (gimple_stmt_iterator gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
);
7394 gimple stmt
= gsi_stmt (gsi
);
7396 find_func_aliases (func
, stmt
);
7397 find_func_clobbers (func
, stmt
);
7403 fprintf (dump_file
, "\n");
7404 dump_constraints (dump_file
, from
);
7405 fprintf (dump_file
, "\n");
7407 from
= constraints
.length ();
7410 /* From the constraints compute the points-to sets. */
7411 solve_constraints ();
7413 /* Compute the global points-to sets for ESCAPED.
7414 ??? Note that the computed escape set is not correct
7415 for the whole unit as we fail to consider graph edges to
7416 externally visible functions. */
7417 ipa_escaped_pt
= find_what_var_points_to (get_varinfo (escaped_id
));
7419 /* Make sure the ESCAPED solution (which is used as placeholder in
7420 other solutions) does not reference itself. This simplifies
7421 points-to solution queries. */
7422 ipa_escaped_pt
.ipa_escaped
= 0;
7424 /* Assign the points-to sets to the SSA names in the unit. */
7425 FOR_EACH_DEFINED_FUNCTION (node
)
7428 struct function
*fn
;
7432 /* Nodes without a body are not interesting. */
7433 if (!node
->has_gimple_body_p () || node
->clone_of
)
7436 fn
= DECL_STRUCT_FUNCTION (node
->decl
);
7438 /* Compute the points-to sets for pointer SSA_NAMEs. */
7439 FOR_EACH_VEC_ELT (*fn
->gimple_df
->ssa_names
, i
, ptr
)
7442 && POINTER_TYPE_P (TREE_TYPE (ptr
)))
7443 find_what_p_points_to (ptr
);
7446 /* Compute the call-use and call-clobber sets for indirect calls
7447 and calls to external functions. */
7448 FOR_EACH_BB_FN (bb
, fn
)
7450 gimple_stmt_iterator gsi
;
7452 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
7455 struct pt_solution
*pt
;
7459 stmt
= dyn_cast
<gcall
*> (gsi_stmt (gsi
));
7463 /* Handle direct calls to functions with body. */
7464 decl
= gimple_call_fndecl (stmt
);
7466 && (fi
= lookup_vi_for_tree (decl
))
7469 *gimple_call_clobber_set (stmt
)
7470 = find_what_var_points_to
7471 (first_vi_for_offset (fi
, fi_clobbers
));
7472 *gimple_call_use_set (stmt
)
7473 = find_what_var_points_to
7474 (first_vi_for_offset (fi
, fi_uses
));
7476 /* Handle direct calls to external functions. */
7479 pt
= gimple_call_use_set (stmt
);
7480 if (gimple_call_flags (stmt
) & ECF_CONST
)
7481 memset (pt
, 0, sizeof (struct pt_solution
));
7482 else if ((vi
= lookup_call_use_vi (stmt
)) != NULL
)
7484 *pt
= find_what_var_points_to (vi
);
7485 /* Escaped (and thus nonlocal) variables are always
7486 implicitly used by calls. */
7487 /* ??? ESCAPED can be empty even though NONLOCAL
7490 pt
->ipa_escaped
= 1;
7494 /* If there is nothing special about this call then
7495 we have made everything that is used also escape. */
7496 *pt
= ipa_escaped_pt
;
7500 pt
= gimple_call_clobber_set (stmt
);
7501 if (gimple_call_flags (stmt
) & (ECF_CONST
|ECF_PURE
|ECF_NOVOPS
))
7502 memset (pt
, 0, sizeof (struct pt_solution
));
7503 else if ((vi
= lookup_call_clobber_vi (stmt
)) != NULL
)
7505 *pt
= find_what_var_points_to (vi
);
7506 /* Escaped (and thus nonlocal) variables are always
7507 implicitly clobbered by calls. */
7508 /* ??? ESCAPED can be empty even though NONLOCAL
7511 pt
->ipa_escaped
= 1;
7515 /* If there is nothing special about this call then
7516 we have made everything that is used also escape. */
7517 *pt
= ipa_escaped_pt
;
7521 /* Handle indirect calls. */
7523 && (fi
= get_fi_for_callee (stmt
)))
7525 /* We need to accumulate all clobbers/uses of all possible
7527 fi
= get_varinfo (find (fi
->id
));
7528 /* If we cannot constrain the set of functions we'll end up
7529 calling we end up using/clobbering everything. */
7530 if (bitmap_bit_p (fi
->solution
, anything_id
)
7531 || bitmap_bit_p (fi
->solution
, nonlocal_id
)
7532 || bitmap_bit_p (fi
->solution
, escaped_id
))
7534 pt_solution_reset (gimple_call_clobber_set (stmt
));
7535 pt_solution_reset (gimple_call_use_set (stmt
));
7541 struct pt_solution
*uses
, *clobbers
;
7543 uses
= gimple_call_use_set (stmt
);
7544 clobbers
= gimple_call_clobber_set (stmt
);
7545 memset (uses
, 0, sizeof (struct pt_solution
));
7546 memset (clobbers
, 0, sizeof (struct pt_solution
));
7547 EXECUTE_IF_SET_IN_BITMAP (fi
->solution
, 0, i
, bi
)
7549 struct pt_solution sol
;
7551 vi
= get_varinfo (i
);
7552 if (!vi
->is_fn_info
)
7554 /* ??? We could be more precise here? */
7556 uses
->ipa_escaped
= 1;
7557 clobbers
->nonlocal
= 1;
7558 clobbers
->ipa_escaped
= 1;
7562 if (!uses
->anything
)
7564 sol
= find_what_var_points_to
7565 (first_vi_for_offset (vi
, fi_uses
));
7566 pt_solution_ior_into (uses
, &sol
);
7568 if (!clobbers
->anything
)
7570 sol
= find_what_var_points_to
7571 (first_vi_for_offset (vi
, fi_clobbers
));
7572 pt_solution_ior_into (clobbers
, &sol
);
7580 fn
->gimple_df
->ipa_pta
= true;
7583 delete_points_to_sets ();
7592 const pass_data pass_data_ipa_pta
=
7594 SIMPLE_IPA_PASS
, /* type */
7596 OPTGROUP_NONE
, /* optinfo_flags */
7597 TV_IPA_PTA
, /* tv_id */
7598 0, /* properties_required */
7599 0, /* properties_provided */
7600 0, /* properties_destroyed */
7601 0, /* todo_flags_start */
7602 0, /* todo_flags_finish */
7605 class pass_ipa_pta
: public simple_ipa_opt_pass
7608 pass_ipa_pta (gcc::context
*ctxt
)
7609 : simple_ipa_opt_pass (pass_data_ipa_pta
, ctxt
)
7612 /* opt_pass methods: */
7613 virtual bool gate (function
*)
7617 /* Don't bother doing anything if the program has errors. */
7621 virtual unsigned int execute (function
*) { return ipa_pta_execute (); }
7623 }; // class pass_ipa_pta
7627 simple_ipa_opt_pass
*
7628 make_pass_ipa_pta (gcc::context
*ctxt
)
7630 return new pass_ipa_pta (ctxt
);