2013-10-23 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / tree-ssa-structalias.c
blobd4b1400117979007e57a8c744287ed45e416823c
1 /* Tree based points-to analysis
2 Copyright (C) 2005-2013 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/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "ggc.h"
26 #include "obstack.h"
27 #include "bitmap.h"
28 #include "flags.h"
29 #include "basic-block.h"
30 #include "tree.h"
31 #include "tree-flow.h"
32 #include "tree-inline.h"
33 #include "diagnostic-core.h"
34 #include "gimple.h"
35 #include "hashtab.h"
36 #include "function.h"
37 #include "cgraph.h"
38 #include "tree-pass.h"
39 #include "alloc-pool.h"
40 #include "splay-tree.h"
41 #include "params.h"
42 #include "cgraph.h"
43 #include "alias.h"
44 #include "pointer-set.h"
46 /* The idea behind this analyzer is to generate set constraints from the
47 program, then solve the resulting constraints in order to generate the
48 points-to sets.
50 Set constraints are a way of modeling program analysis problems that
51 involve sets. They consist of an inclusion constraint language,
52 describing the variables (each variable is a set) and operations that
53 are involved on the variables, and a set of rules that derive facts
54 from these operations. To solve a system of set constraints, you derive
55 all possible facts under the rules, which gives you the correct sets
56 as a consequence.
58 See "Efficient Field-sensitive pointer analysis for C" by "David
59 J. Pearce and Paul H. J. Kelly and Chris Hankin, at
60 http://citeseer.ist.psu.edu/pearce04efficient.html
62 Also see "Ultra-fast Aliasing Analysis using CLA: A Million Lines
63 of C Code in a Second" by ""Nevin Heintze and Olivier Tardieu" at
64 http://citeseer.ist.psu.edu/heintze01ultrafast.html
66 There are three types of real constraint expressions, DEREF,
67 ADDRESSOF, and SCALAR. Each constraint expression consists
68 of a constraint type, a variable, and an offset.
70 SCALAR is a constraint expression type used to represent x, whether
71 it appears on the LHS or the RHS of a statement.
72 DEREF is a constraint expression type used to represent *x, whether
73 it appears on the LHS or the RHS of a statement.
74 ADDRESSOF is a constraint expression used to represent &x, whether
75 it appears on the LHS or the RHS of a statement.
77 Each pointer variable in the program is assigned an integer id, and
78 each field of a structure variable is assigned an integer id as well.
80 Structure variables are linked to their list of fields through a "next
81 field" in each variable that points to the next field in offset
82 order.
83 Each variable for a structure field has
85 1. "size", that tells the size in bits of that field.
86 2. "fullsize, that tells the size in bits of the entire structure.
87 3. "offset", that tells the offset in bits from the beginning of the
88 structure to this field.
90 Thus,
91 struct f
93 int a;
94 int b;
95 } foo;
96 int *bar;
98 looks like
100 foo.a -> id 1, size 32, offset 0, fullsize 64, next foo.b
101 foo.b -> id 2, size 32, offset 32, fullsize 64, next NULL
102 bar -> id 3, size 32, offset 0, fullsize 32, next NULL
105 In order to solve the system of set constraints, the following is
106 done:
108 1. Each constraint variable x has a solution set associated with it,
109 Sol(x).
111 2. Constraints are separated into direct, copy, and complex.
112 Direct constraints are ADDRESSOF constraints that require no extra
113 processing, such as P = &Q
114 Copy constraints are those of the form P = Q.
115 Complex constraints are all the constraints involving dereferences
116 and offsets (including offsetted copies).
118 3. All direct constraints of the form P = &Q are processed, such
119 that Q is added to Sol(P)
121 4. All complex constraints for a given constraint variable are stored in a
122 linked list attached to that variable's node.
124 5. A directed graph is built out of the copy constraints. Each
125 constraint variable is a node in the graph, and an edge from
126 Q to P is added for each copy constraint of the form P = Q
128 6. The graph is then walked, and solution sets are
129 propagated along the copy edges, such that an edge from Q to P
130 causes Sol(P) <- Sol(P) union Sol(Q).
132 7. As we visit each node, all complex constraints associated with
133 that node are processed by adding appropriate copy edges to the graph, or the
134 appropriate variables to the solution set.
136 8. The process of walking the graph is iterated until no solution
137 sets change.
139 Prior to walking the graph in steps 6 and 7, We perform static
140 cycle elimination on the constraint graph, as well
141 as off-line variable substitution.
143 TODO: Adding offsets to pointer-to-structures can be handled (IE not punted
144 on and turned into anything), but isn't. You can just see what offset
145 inside the pointed-to struct it's going to access.
147 TODO: Constant bounded arrays can be handled as if they were structs of the
148 same number of elements.
150 TODO: Modeling heap and incoming pointers becomes much better if we
151 add fields to them as we discover them, which we could do.
153 TODO: We could handle unions, but to be honest, it's probably not
154 worth the pain or slowdown. */
156 /* IPA-PTA optimizations possible.
158 When the indirect function called is ANYTHING we can add disambiguation
159 based on the function signatures (or simply the parameter count which
160 is the varinfo size). We also do not need to consider functions that
161 do not have their address taken.
163 The is_global_var bit which marks escape points is overly conservative
164 in IPA mode. Split it to is_escape_point and is_global_var - only
165 externally visible globals are escape points in IPA mode. This is
166 also needed to fix the pt_solution_includes_global predicate
167 (and thus ptr_deref_may_alias_global_p).
169 The way we introduce DECL_PT_UID to avoid fixing up all points-to
170 sets in the translation unit when we copy a DECL during inlining
171 pessimizes precision. The advantage is that the DECL_PT_UID keeps
172 compile-time and memory usage overhead low - the points-to sets
173 do not grow or get unshared as they would during a fixup phase.
174 An alternative solution is to delay IPA PTA until after all
175 inlining transformations have been applied.
177 The way we propagate clobber/use information isn't optimized.
178 It should use a new complex constraint that properly filters
179 out local variables of the callee (though that would make
180 the sets invalid after inlining). OTOH we might as well
181 admit defeat to WHOPR and simply do all the clobber/use analysis
182 and propagation after PTA finished but before we threw away
183 points-to information for memory variables. WHOPR and PTA
184 do not play along well anyway - the whole constraint solving
185 would need to be done in WPA phase and it will be very interesting
186 to apply the results to local SSA names during LTRANS phase.
188 We probably should compute a per-function unit-ESCAPE solution
189 propagating it simply like the clobber / uses solutions. The
190 solution can go alongside the non-IPA espaced solution and be
191 used to query which vars escape the unit through a function.
193 We never put function decls in points-to sets so we do not
194 keep the set of called functions for indirect calls.
196 And probably more. */
198 static bool use_field_sensitive = true;
199 static int in_ipa_mode = 0;
201 /* Used for predecessor bitmaps. */
202 static bitmap_obstack predbitmap_obstack;
204 /* Used for points-to sets. */
205 static bitmap_obstack pta_obstack;
207 /* Used for oldsolution members of variables. */
208 static bitmap_obstack oldpta_obstack;
210 /* Used for per-solver-iteration bitmaps. */
211 static bitmap_obstack iteration_obstack;
213 static unsigned int create_variable_info_for (tree, const char *);
214 typedef struct constraint_graph *constraint_graph_t;
215 static void unify_nodes (constraint_graph_t, unsigned int, unsigned int, bool);
217 struct constraint;
218 typedef struct constraint *constraint_t;
221 #define EXECUTE_IF_IN_NONNULL_BITMAP(a, b, c, d) \
222 if (a) \
223 EXECUTE_IF_SET_IN_BITMAP (a, b, c, d)
225 static struct constraint_stats
227 unsigned int total_vars;
228 unsigned int nonpointer_vars;
229 unsigned int unified_vars_static;
230 unsigned int unified_vars_dynamic;
231 unsigned int iterations;
232 unsigned int num_edges;
233 unsigned int num_implicit_edges;
234 unsigned int points_to_sets_created;
235 } stats;
237 struct variable_info
239 /* ID of this variable */
240 unsigned int id;
242 /* True if this is a variable created by the constraint analysis, such as
243 heap variables and constraints we had to break up. */
244 unsigned int is_artificial_var : 1;
246 /* True if this is a special variable whose solution set should not be
247 changed. */
248 unsigned int is_special_var : 1;
250 /* True for variables whose size is not known or variable. */
251 unsigned int is_unknown_size_var : 1;
253 /* True for (sub-)fields that represent a whole variable. */
254 unsigned int is_full_var : 1;
256 /* True if this is a heap variable. */
257 unsigned int is_heap_var : 1;
259 /* True if this field may contain pointers. */
260 unsigned int may_have_pointers : 1;
262 /* True if this field has only restrict qualified pointers. */
263 unsigned int only_restrict_pointers : 1;
265 /* True if this represents a global variable. */
266 unsigned int is_global_var : 1;
268 /* True if this represents a IPA function info. */
269 unsigned int is_fn_info : 1;
271 /* A link to the variable for the next field in this structure. */
272 struct variable_info *next;
274 /* Offset of this variable, in bits, from the base variable */
275 unsigned HOST_WIDE_INT offset;
277 /* Size of the variable, in bits. */
278 unsigned HOST_WIDE_INT size;
280 /* Full size of the base variable, in bits. */
281 unsigned HOST_WIDE_INT fullsize;
283 /* Name of this variable */
284 const char *name;
286 /* Tree that this variable is associated with. */
287 tree decl;
289 /* Points-to set for this variable. */
290 bitmap solution;
292 /* Old points-to set for this variable. */
293 bitmap oldsolution;
295 typedef struct variable_info *varinfo_t;
297 static varinfo_t first_vi_for_offset (varinfo_t, unsigned HOST_WIDE_INT);
298 static varinfo_t first_or_preceding_vi_for_offset (varinfo_t,
299 unsigned HOST_WIDE_INT);
300 static varinfo_t lookup_vi_for_tree (tree);
301 static inline bool type_can_have_subvars (const_tree);
303 /* Pool of variable info structures. */
304 static alloc_pool variable_info_pool;
306 /* Map varinfo to final pt_solution. */
307 static pointer_map_t *final_solutions;
308 struct obstack final_solutions_obstack;
310 /* Table of variable info structures for constraint variables.
311 Indexed directly by variable info id. */
312 static vec<varinfo_t> varmap;
314 /* Return the varmap element N */
316 static inline varinfo_t
317 get_varinfo (unsigned int n)
319 return varmap[n];
322 /* Static IDs for the special variables. */
323 enum { nothing_id = 0, anything_id = 1, readonly_id = 2,
324 escaped_id = 3, nonlocal_id = 4,
325 storedanything_id = 5, integer_id = 6 };
327 /* Return a new variable info structure consisting for a variable
328 named NAME, and using constraint graph node NODE. Append it
329 to the vector of variable info structures. */
331 static varinfo_t
332 new_var_info (tree t, const char *name)
334 unsigned index = varmap.length ();
335 varinfo_t ret = (varinfo_t) pool_alloc (variable_info_pool);
337 ret->id = index;
338 ret->name = name;
339 ret->decl = t;
340 /* Vars without decl are artificial and do not have sub-variables. */
341 ret->is_artificial_var = (t == NULL_TREE);
342 ret->is_special_var = false;
343 ret->is_unknown_size_var = false;
344 ret->is_full_var = (t == NULL_TREE);
345 ret->is_heap_var = false;
346 ret->may_have_pointers = true;
347 ret->only_restrict_pointers = false;
348 ret->is_global_var = (t == NULL_TREE);
349 ret->is_fn_info = false;
350 if (t && DECL_P (t))
351 ret->is_global_var = (is_global_var (t)
352 /* We have to treat even local register variables
353 as escape points. */
354 || (TREE_CODE (t) == VAR_DECL
355 && DECL_HARD_REGISTER (t)));
356 ret->solution = BITMAP_ALLOC (&pta_obstack);
357 ret->oldsolution = NULL;
358 ret->next = NULL;
360 stats.total_vars++;
362 varmap.safe_push (ret);
364 return ret;
368 /* A map mapping call statements to per-stmt variables for uses
369 and clobbers specific to the call. */
370 struct pointer_map_t *call_stmt_vars;
372 /* Lookup or create the variable for the call statement CALL. */
374 static varinfo_t
375 get_call_vi (gimple call)
377 void **slot_p;
378 varinfo_t vi, vi2;
380 slot_p = pointer_map_insert (call_stmt_vars, call);
381 if (*slot_p)
382 return (varinfo_t) *slot_p;
384 vi = new_var_info (NULL_TREE, "CALLUSED");
385 vi->offset = 0;
386 vi->size = 1;
387 vi->fullsize = 2;
388 vi->is_full_var = true;
390 vi->next = vi2 = new_var_info (NULL_TREE, "CALLCLOBBERED");
391 vi2->offset = 1;
392 vi2->size = 1;
393 vi2->fullsize = 2;
394 vi2->is_full_var = true;
396 *slot_p = (void *) vi;
397 return vi;
400 /* Lookup the variable for the call statement CALL representing
401 the uses. Returns NULL if there is nothing special about this call. */
403 static varinfo_t
404 lookup_call_use_vi (gimple call)
406 void **slot_p;
408 slot_p = pointer_map_contains (call_stmt_vars, call);
409 if (slot_p)
410 return (varinfo_t) *slot_p;
412 return NULL;
415 /* Lookup the variable for the call statement CALL representing
416 the clobbers. Returns NULL if there is nothing special about this call. */
418 static varinfo_t
419 lookup_call_clobber_vi (gimple call)
421 varinfo_t uses = lookup_call_use_vi (call);
422 if (!uses)
423 return NULL;
425 return uses->next;
428 /* Lookup or create the variable for the call statement CALL representing
429 the uses. */
431 static varinfo_t
432 get_call_use_vi (gimple call)
434 return get_call_vi (call);
437 /* Lookup or create the variable for the call statement CALL representing
438 the clobbers. */
440 static varinfo_t ATTRIBUTE_UNUSED
441 get_call_clobber_vi (gimple call)
443 return get_call_vi (call)->next;
447 typedef enum {SCALAR, DEREF, ADDRESSOF} constraint_expr_type;
449 /* An expression that appears in a constraint. */
451 struct constraint_expr
453 /* Constraint type. */
454 constraint_expr_type type;
456 /* Variable we are referring to in the constraint. */
457 unsigned int var;
459 /* Offset, in bits, of this constraint from the beginning of
460 variables it ends up referring to.
462 IOW, in a deref constraint, we would deref, get the result set,
463 then add OFFSET to each member. */
464 HOST_WIDE_INT offset;
467 /* Use 0x8000... as special unknown offset. */
468 #define UNKNOWN_OFFSET ((HOST_WIDE_INT)-1 << (HOST_BITS_PER_WIDE_INT-1))
470 typedef struct constraint_expr ce_s;
471 static void get_constraint_for_1 (tree, vec<ce_s> *, bool, bool);
472 static void get_constraint_for (tree, vec<ce_s> *);
473 static void get_constraint_for_rhs (tree, vec<ce_s> *);
474 static void do_deref (vec<ce_s> *);
476 /* Our set constraints are made up of two constraint expressions, one
477 LHS, and one RHS.
479 As described in the introduction, our set constraints each represent an
480 operation between set valued variables.
482 struct constraint
484 struct constraint_expr lhs;
485 struct constraint_expr rhs;
488 /* List of constraints that we use to build the constraint graph from. */
490 static vec<constraint_t> constraints;
491 static alloc_pool constraint_pool;
493 /* The constraint graph is represented as an array of bitmaps
494 containing successor nodes. */
496 struct constraint_graph
498 /* Size of this graph, which may be different than the number of
499 nodes in the variable map. */
500 unsigned int size;
502 /* Explicit successors of each node. */
503 bitmap *succs;
505 /* Implicit predecessors of each node (Used for variable
506 substitution). */
507 bitmap *implicit_preds;
509 /* Explicit predecessors of each node (Used for variable substitution). */
510 bitmap *preds;
512 /* Indirect cycle representatives, or -1 if the node has no indirect
513 cycles. */
514 int *indirect_cycles;
516 /* Representative node for a node. rep[a] == a unless the node has
517 been unified. */
518 unsigned int *rep;
520 /* Equivalence class representative for a label. This is used for
521 variable substitution. */
522 int *eq_rep;
524 /* Pointer equivalence label for a node. All nodes with the same
525 pointer equivalence label can be unified together at some point
526 (either during constraint optimization or after the constraint
527 graph is built). */
528 unsigned int *pe;
530 /* Pointer equivalence representative for a label. This is used to
531 handle nodes that are pointer equivalent but not location
532 equivalent. We can unite these once the addressof constraints
533 are transformed into initial points-to sets. */
534 int *pe_rep;
536 /* Pointer equivalence label for each node, used during variable
537 substitution. */
538 unsigned int *pointer_label;
540 /* Location equivalence label for each node, used during location
541 equivalence finding. */
542 unsigned int *loc_label;
544 /* Pointed-by set for each node, used during location equivalence
545 finding. This is pointed-by rather than pointed-to, because it
546 is constructed using the predecessor graph. */
547 bitmap *pointed_by;
549 /* Points to sets for pointer equivalence. This is *not* the actual
550 points-to sets for nodes. */
551 bitmap *points_to;
553 /* Bitmap of nodes where the bit is set if the node is a direct
554 node. Used for variable substitution. */
555 sbitmap direct_nodes;
557 /* Bitmap of nodes where the bit is set if the node is address
558 taken. Used for variable substitution. */
559 bitmap address_taken;
561 /* Vector of complex constraints for each graph node. Complex
562 constraints are those involving dereferences or offsets that are
563 not 0. */
564 vec<constraint_t> *complex;
567 static constraint_graph_t graph;
569 /* During variable substitution and the offline version of indirect
570 cycle finding, we create nodes to represent dereferences and
571 address taken constraints. These represent where these start and
572 end. */
573 #define FIRST_REF_NODE (varmap).length ()
574 #define LAST_REF_NODE (FIRST_REF_NODE + (FIRST_REF_NODE - 1))
576 /* Return the representative node for NODE, if NODE has been unioned
577 with another NODE.
578 This function performs path compression along the way to finding
579 the representative. */
581 static unsigned int
582 find (unsigned int node)
584 gcc_assert (node < graph->size);
585 if (graph->rep[node] != node)
586 return graph->rep[node] = find (graph->rep[node]);
587 return node;
590 /* Union the TO and FROM nodes to the TO nodes.
591 Note that at some point in the future, we may want to do
592 union-by-rank, in which case we are going to have to return the
593 node we unified to. */
595 static bool
596 unite (unsigned int to, unsigned int from)
598 gcc_assert (to < graph->size && from < graph->size);
599 if (to != from && graph->rep[from] != to)
601 graph->rep[from] = to;
602 return true;
604 return false;
607 /* Create a new constraint consisting of LHS and RHS expressions. */
609 static constraint_t
610 new_constraint (const struct constraint_expr lhs,
611 const struct constraint_expr rhs)
613 constraint_t ret = (constraint_t) pool_alloc (constraint_pool);
614 ret->lhs = lhs;
615 ret->rhs = rhs;
616 return ret;
619 /* Print out constraint C to FILE. */
621 static void
622 dump_constraint (FILE *file, constraint_t c)
624 if (c->lhs.type == ADDRESSOF)
625 fprintf (file, "&");
626 else if (c->lhs.type == DEREF)
627 fprintf (file, "*");
628 fprintf (file, "%s", get_varinfo (c->lhs.var)->name);
629 if (c->lhs.offset == UNKNOWN_OFFSET)
630 fprintf (file, " + UNKNOWN");
631 else if (c->lhs.offset != 0)
632 fprintf (file, " + " HOST_WIDE_INT_PRINT_DEC, c->lhs.offset);
633 fprintf (file, " = ");
634 if (c->rhs.type == ADDRESSOF)
635 fprintf (file, "&");
636 else if (c->rhs.type == DEREF)
637 fprintf (file, "*");
638 fprintf (file, "%s", get_varinfo (c->rhs.var)->name);
639 if (c->rhs.offset == UNKNOWN_OFFSET)
640 fprintf (file, " + UNKNOWN");
641 else if (c->rhs.offset != 0)
642 fprintf (file, " + " HOST_WIDE_INT_PRINT_DEC, c->rhs.offset);
646 void debug_constraint (constraint_t);
647 void debug_constraints (void);
648 void debug_constraint_graph (void);
649 void debug_solution_for_var (unsigned int);
650 void debug_sa_points_to_info (void);
652 /* Print out constraint C to stderr. */
654 DEBUG_FUNCTION void
655 debug_constraint (constraint_t c)
657 dump_constraint (stderr, c);
658 fprintf (stderr, "\n");
661 /* Print out all constraints to FILE */
663 static void
664 dump_constraints (FILE *file, int from)
666 int i;
667 constraint_t c;
668 for (i = from; constraints.iterate (i, &c); i++)
669 if (c)
671 dump_constraint (file, c);
672 fprintf (file, "\n");
676 /* Print out all constraints to stderr. */
678 DEBUG_FUNCTION void
679 debug_constraints (void)
681 dump_constraints (stderr, 0);
684 /* Print the constraint graph in dot format. */
686 static void
687 dump_constraint_graph (FILE *file)
689 unsigned int i;
691 /* Only print the graph if it has already been initialized: */
692 if (!graph)
693 return;
695 /* Prints the header of the dot file: */
696 fprintf (file, "strict digraph {\n");
697 fprintf (file, " node [\n shape = box\n ]\n");
698 fprintf (file, " edge [\n fontsize = \"12\"\n ]\n");
699 fprintf (file, "\n // List of nodes and complex constraints in "
700 "the constraint graph:\n");
702 /* The next lines print the nodes in the graph together with the
703 complex constraints attached to them. */
704 for (i = 0; i < graph->size; i++)
706 if (find (i) != i)
707 continue;
708 if (i < FIRST_REF_NODE)
709 fprintf (file, "\"%s\"", get_varinfo (i)->name);
710 else
711 fprintf (file, "\"*%s\"", get_varinfo (i - FIRST_REF_NODE)->name);
712 if (graph->complex[i].exists ())
714 unsigned j;
715 constraint_t c;
716 fprintf (file, " [label=\"\\N\\n");
717 for (j = 0; graph->complex[i].iterate (j, &c); ++j)
719 dump_constraint (file, c);
720 fprintf (file, "\\l");
722 fprintf (file, "\"]");
724 fprintf (file, ";\n");
727 /* Go over the edges. */
728 fprintf (file, "\n // Edges in the constraint graph:\n");
729 for (i = 0; i < graph->size; i++)
731 unsigned j;
732 bitmap_iterator bi;
733 if (find (i) != i)
734 continue;
735 EXECUTE_IF_IN_NONNULL_BITMAP (graph->succs[i], 0, j, bi)
737 unsigned to = find (j);
738 if (i == to)
739 continue;
740 if (i < FIRST_REF_NODE)
741 fprintf (file, "\"%s\"", get_varinfo (i)->name);
742 else
743 fprintf (file, "\"*%s\"", get_varinfo (i - FIRST_REF_NODE)->name);
744 fprintf (file, " -> ");
745 if (to < FIRST_REF_NODE)
746 fprintf (file, "\"%s\"", get_varinfo (to)->name);
747 else
748 fprintf (file, "\"*%s\"", get_varinfo (to - FIRST_REF_NODE)->name);
749 fprintf (file, ";\n");
753 /* Prints the tail of the dot file. */
754 fprintf (file, "}\n");
757 /* Print out the constraint graph to stderr. */
759 DEBUG_FUNCTION void
760 debug_constraint_graph (void)
762 dump_constraint_graph (stderr);
765 /* SOLVER FUNCTIONS
767 The solver is a simple worklist solver, that works on the following
768 algorithm:
770 sbitmap changed_nodes = all zeroes;
771 changed_count = 0;
772 For each node that is not already collapsed:
773 changed_count++;
774 set bit in changed nodes
776 while (changed_count > 0)
778 compute topological ordering for constraint graph
780 find and collapse cycles in the constraint graph (updating
781 changed if necessary)
783 for each node (n) in the graph in topological order:
784 changed_count--;
786 Process each complex constraint associated with the node,
787 updating changed if necessary.
789 For each outgoing edge from n, propagate the solution from n to
790 the destination of the edge, updating changed as necessary.
792 } */
794 /* Return true if two constraint expressions A and B are equal. */
796 static bool
797 constraint_expr_equal (struct constraint_expr a, struct constraint_expr b)
799 return a.type == b.type && a.var == b.var && a.offset == b.offset;
802 /* Return true if constraint expression A is less than constraint expression
803 B. This is just arbitrary, but consistent, in order to give them an
804 ordering. */
806 static bool
807 constraint_expr_less (struct constraint_expr a, struct constraint_expr b)
809 if (a.type == b.type)
811 if (a.var == b.var)
812 return a.offset < b.offset;
813 else
814 return a.var < b.var;
816 else
817 return a.type < b.type;
820 /* Return true if constraint A is less than constraint B. This is just
821 arbitrary, but consistent, in order to give them an ordering. */
823 static bool
824 constraint_less (const constraint_t &a, const constraint_t &b)
826 if (constraint_expr_less (a->lhs, b->lhs))
827 return true;
828 else if (constraint_expr_less (b->lhs, a->lhs))
829 return false;
830 else
831 return constraint_expr_less (a->rhs, b->rhs);
834 /* Return true if two constraints A and B are equal. */
836 static bool
837 constraint_equal (struct constraint a, struct constraint b)
839 return constraint_expr_equal (a.lhs, b.lhs)
840 && constraint_expr_equal (a.rhs, b.rhs);
844 /* Find a constraint LOOKFOR in the sorted constraint vector VEC */
846 static constraint_t
847 constraint_vec_find (vec<constraint_t> vec,
848 struct constraint lookfor)
850 unsigned int place;
851 constraint_t found;
853 if (!vec.exists ())
854 return NULL;
856 place = vec.lower_bound (&lookfor, constraint_less);
857 if (place >= vec.length ())
858 return NULL;
859 found = vec[place];
860 if (!constraint_equal (*found, lookfor))
861 return NULL;
862 return found;
865 /* Union two constraint vectors, TO and FROM. Put the result in TO. */
867 static void
868 constraint_set_union (vec<constraint_t> *to,
869 vec<constraint_t> *from)
871 int i;
872 constraint_t c;
874 FOR_EACH_VEC_ELT (*from, i, c)
876 if (constraint_vec_find (*to, *c) == NULL)
878 unsigned int place = to->lower_bound (c, constraint_less);
879 to->safe_insert (place, c);
884 /* Expands the solution in SET to all sub-fields of variables included.
885 Union the expanded result into RESULT. */
887 static void
888 solution_set_expand (bitmap result, bitmap set)
890 bitmap_iterator bi;
891 bitmap vars = NULL;
892 unsigned j;
894 /* In a first pass record all variables we need to add all
895 sub-fields off. This avoids quadratic behavior. */
896 EXECUTE_IF_SET_IN_BITMAP (set, 0, j, bi)
898 varinfo_t v = get_varinfo (j);
899 if (v->is_artificial_var
900 || v->is_full_var)
901 continue;
902 v = lookup_vi_for_tree (v->decl);
903 if (vars == NULL)
904 vars = BITMAP_ALLOC (NULL);
905 bitmap_set_bit (vars, v->id);
908 /* In the second pass now do the addition to the solution and
909 to speed up solving add it to the delta as well. */
910 if (vars != NULL)
912 EXECUTE_IF_SET_IN_BITMAP (vars, 0, j, bi)
914 varinfo_t v = get_varinfo (j);
915 for (; v != NULL; v = v->next)
916 bitmap_set_bit (result, v->id);
918 BITMAP_FREE (vars);
922 /* Take a solution set SET, add OFFSET to each member of the set, and
923 overwrite SET with the result when done. */
925 static void
926 solution_set_add (bitmap set, HOST_WIDE_INT offset)
928 bitmap result = BITMAP_ALLOC (&iteration_obstack);
929 unsigned int i;
930 bitmap_iterator bi;
932 /* If the offset is unknown we have to expand the solution to
933 all subfields. */
934 if (offset == UNKNOWN_OFFSET)
936 solution_set_expand (set, set);
937 return;
940 EXECUTE_IF_SET_IN_BITMAP (set, 0, i, bi)
942 varinfo_t vi = get_varinfo (i);
944 /* If this is a variable with just one field just set its bit
945 in the result. */
946 if (vi->is_artificial_var
947 || vi->is_unknown_size_var
948 || vi->is_full_var)
949 bitmap_set_bit (result, i);
950 else
952 unsigned HOST_WIDE_INT fieldoffset = vi->offset + offset;
954 /* If the offset makes the pointer point to before the
955 variable use offset zero for the field lookup. */
956 if (offset < 0
957 && fieldoffset > vi->offset)
958 fieldoffset = 0;
960 if (offset != 0)
961 vi = first_or_preceding_vi_for_offset (vi, fieldoffset);
963 bitmap_set_bit (result, vi->id);
964 /* If the result is not exactly at fieldoffset include the next
965 field as well. See get_constraint_for_ptr_offset for more
966 rationale. */
967 if (vi->offset != fieldoffset
968 && vi->next != NULL)
969 bitmap_set_bit (result, vi->next->id);
973 bitmap_copy (set, result);
974 BITMAP_FREE (result);
977 /* Union solution sets TO and FROM, and add INC to each member of FROM in the
978 process. */
980 static bool
981 set_union_with_increment (bitmap to, bitmap from, HOST_WIDE_INT inc)
983 if (inc == 0)
984 return bitmap_ior_into (to, from);
985 else
987 bitmap tmp;
988 bool res;
990 tmp = BITMAP_ALLOC (&iteration_obstack);
991 bitmap_copy (tmp, from);
992 solution_set_add (tmp, inc);
993 res = bitmap_ior_into (to, tmp);
994 BITMAP_FREE (tmp);
995 return res;
999 /* Insert constraint C into the list of complex constraints for graph
1000 node VAR. */
1002 static void
1003 insert_into_complex (constraint_graph_t graph,
1004 unsigned int var, constraint_t c)
1006 vec<constraint_t> complex = graph->complex[var];
1007 unsigned int place = complex.lower_bound (c, constraint_less);
1009 /* Only insert constraints that do not already exist. */
1010 if (place >= complex.length ()
1011 || !constraint_equal (*c, *complex[place]))
1012 graph->complex[var].safe_insert (place, c);
1016 /* Condense two variable nodes into a single variable node, by moving
1017 all associated info from SRC to TO. */
1019 static void
1020 merge_node_constraints (constraint_graph_t graph, unsigned int to,
1021 unsigned int from)
1023 unsigned int i;
1024 constraint_t c;
1026 gcc_assert (find (from) == to);
1028 /* Move all complex constraints from src node into to node */
1029 FOR_EACH_VEC_ELT (graph->complex[from], i, c)
1031 /* In complex constraints for node src, we may have either
1032 a = *src, and *src = a, or an offseted constraint which are
1033 always added to the rhs node's constraints. */
1035 if (c->rhs.type == DEREF)
1036 c->rhs.var = to;
1037 else if (c->lhs.type == DEREF)
1038 c->lhs.var = to;
1039 else
1040 c->rhs.var = to;
1042 constraint_set_union (&graph->complex[to], &graph->complex[from]);
1043 graph->complex[from].release ();
1047 /* Remove edges involving NODE from GRAPH. */
1049 static void
1050 clear_edges_for_node (constraint_graph_t graph, unsigned int node)
1052 if (graph->succs[node])
1053 BITMAP_FREE (graph->succs[node]);
1056 /* Merge GRAPH nodes FROM and TO into node TO. */
1058 static void
1059 merge_graph_nodes (constraint_graph_t graph, unsigned int to,
1060 unsigned int from)
1062 if (graph->indirect_cycles[from] != -1)
1064 /* If we have indirect cycles with the from node, and we have
1065 none on the to node, the to node has indirect cycles from the
1066 from node now that they are unified.
1067 If indirect cycles exist on both, unify the nodes that they
1068 are in a cycle with, since we know they are in a cycle with
1069 each other. */
1070 if (graph->indirect_cycles[to] == -1)
1071 graph->indirect_cycles[to] = graph->indirect_cycles[from];
1074 /* Merge all the successor edges. */
1075 if (graph->succs[from])
1077 if (!graph->succs[to])
1078 graph->succs[to] = BITMAP_ALLOC (&pta_obstack);
1079 bitmap_ior_into (graph->succs[to],
1080 graph->succs[from]);
1083 clear_edges_for_node (graph, from);
1087 /* Add an indirect graph edge to GRAPH, going from TO to FROM if
1088 it doesn't exist in the graph already. */
1090 static void
1091 add_implicit_graph_edge (constraint_graph_t graph, unsigned int to,
1092 unsigned int from)
1094 if (to == from)
1095 return;
1097 if (!graph->implicit_preds[to])
1098 graph->implicit_preds[to] = BITMAP_ALLOC (&predbitmap_obstack);
1100 if (bitmap_set_bit (graph->implicit_preds[to], from))
1101 stats.num_implicit_edges++;
1104 /* Add a predecessor graph edge to GRAPH, going from TO to FROM if
1105 it doesn't exist in the graph already.
1106 Return false if the edge already existed, true otherwise. */
1108 static void
1109 add_pred_graph_edge (constraint_graph_t graph, unsigned int to,
1110 unsigned int from)
1112 if (!graph->preds[to])
1113 graph->preds[to] = BITMAP_ALLOC (&predbitmap_obstack);
1114 bitmap_set_bit (graph->preds[to], from);
1117 /* Add a graph edge to GRAPH, going from FROM to TO if
1118 it doesn't exist in the graph already.
1119 Return false if the edge already existed, true otherwise. */
1121 static bool
1122 add_graph_edge (constraint_graph_t graph, unsigned int to,
1123 unsigned int from)
1125 if (to == from)
1127 return false;
1129 else
1131 bool r = false;
1133 if (!graph->succs[from])
1134 graph->succs[from] = BITMAP_ALLOC (&pta_obstack);
1135 if (bitmap_set_bit (graph->succs[from], to))
1137 r = true;
1138 if (to < FIRST_REF_NODE && from < FIRST_REF_NODE)
1139 stats.num_edges++;
1141 return r;
1146 /* Return true if {DEST.SRC} is an existing graph edge in GRAPH. */
1148 static bool
1149 valid_graph_edge (constraint_graph_t graph, unsigned int src,
1150 unsigned int dest)
1152 return (graph->succs[dest]
1153 && bitmap_bit_p (graph->succs[dest], src));
1156 /* Initialize the constraint graph structure to contain SIZE nodes. */
1158 static void
1159 init_graph (unsigned int size)
1161 unsigned int j;
1163 graph = XCNEW (struct constraint_graph);
1164 graph->size = size;
1165 graph->succs = XCNEWVEC (bitmap, graph->size);
1166 graph->indirect_cycles = XNEWVEC (int, graph->size);
1167 graph->rep = XNEWVEC (unsigned int, graph->size);
1168 /* ??? Macros do not support template types with multiple arguments,
1169 so we use a typedef to work around it. */
1170 typedef vec<constraint_t> vec_constraint_t_heap;
1171 graph->complex = XCNEWVEC (vec_constraint_t_heap, size);
1172 graph->pe = XCNEWVEC (unsigned int, graph->size);
1173 graph->pe_rep = XNEWVEC (int, graph->size);
1175 for (j = 0; j < graph->size; j++)
1177 graph->rep[j] = j;
1178 graph->pe_rep[j] = -1;
1179 graph->indirect_cycles[j] = -1;
1183 /* Build the constraint graph, adding only predecessor edges right now. */
1185 static void
1186 build_pred_graph (void)
1188 int i;
1189 constraint_t c;
1190 unsigned int j;
1192 graph->implicit_preds = XCNEWVEC (bitmap, graph->size);
1193 graph->preds = XCNEWVEC (bitmap, graph->size);
1194 graph->pointer_label = XCNEWVEC (unsigned int, graph->size);
1195 graph->loc_label = XCNEWVEC (unsigned int, graph->size);
1196 graph->pointed_by = XCNEWVEC (bitmap, graph->size);
1197 graph->points_to = XCNEWVEC (bitmap, graph->size);
1198 graph->eq_rep = XNEWVEC (int, graph->size);
1199 graph->direct_nodes = sbitmap_alloc (graph->size);
1200 graph->address_taken = BITMAP_ALLOC (&predbitmap_obstack);
1201 bitmap_clear (graph->direct_nodes);
1203 for (j = 0; j < FIRST_REF_NODE; j++)
1205 if (!get_varinfo (j)->is_special_var)
1206 bitmap_set_bit (graph->direct_nodes, j);
1209 for (j = 0; j < graph->size; j++)
1210 graph->eq_rep[j] = -1;
1212 for (j = 0; j < varmap.length (); j++)
1213 graph->indirect_cycles[j] = -1;
1215 FOR_EACH_VEC_ELT (constraints, i, c)
1217 struct constraint_expr lhs = c->lhs;
1218 struct constraint_expr rhs = c->rhs;
1219 unsigned int lhsvar = lhs.var;
1220 unsigned int rhsvar = rhs.var;
1222 if (lhs.type == DEREF)
1224 /* *x = y. */
1225 if (rhs.offset == 0 && lhs.offset == 0 && rhs.type == SCALAR)
1226 add_pred_graph_edge (graph, FIRST_REF_NODE + lhsvar, rhsvar);
1228 else if (rhs.type == DEREF)
1230 /* x = *y */
1231 if (rhs.offset == 0 && lhs.offset == 0 && lhs.type == SCALAR)
1232 add_pred_graph_edge (graph, lhsvar, FIRST_REF_NODE + rhsvar);
1233 else
1234 bitmap_clear_bit (graph->direct_nodes, lhsvar);
1236 else if (rhs.type == ADDRESSOF)
1238 varinfo_t v;
1240 /* x = &y */
1241 if (graph->points_to[lhsvar] == NULL)
1242 graph->points_to[lhsvar] = BITMAP_ALLOC (&predbitmap_obstack);
1243 bitmap_set_bit (graph->points_to[lhsvar], rhsvar);
1245 if (graph->pointed_by[rhsvar] == NULL)
1246 graph->pointed_by[rhsvar] = BITMAP_ALLOC (&predbitmap_obstack);
1247 bitmap_set_bit (graph->pointed_by[rhsvar], lhsvar);
1249 /* Implicitly, *x = y */
1250 add_implicit_graph_edge (graph, FIRST_REF_NODE + lhsvar, rhsvar);
1252 /* All related variables are no longer direct nodes. */
1253 bitmap_clear_bit (graph->direct_nodes, rhsvar);
1254 v = get_varinfo (rhsvar);
1255 if (!v->is_full_var)
1257 v = lookup_vi_for_tree (v->decl);
1260 bitmap_clear_bit (graph->direct_nodes, v->id);
1261 v = v->next;
1263 while (v != NULL);
1265 bitmap_set_bit (graph->address_taken, rhsvar);
1267 else if (lhsvar > anything_id
1268 && lhsvar != rhsvar && lhs.offset == 0 && rhs.offset == 0)
1270 /* x = y */
1271 add_pred_graph_edge (graph, lhsvar, rhsvar);
1272 /* Implicitly, *x = *y */
1273 add_implicit_graph_edge (graph, FIRST_REF_NODE + lhsvar,
1274 FIRST_REF_NODE + rhsvar);
1276 else if (lhs.offset != 0 || rhs.offset != 0)
1278 if (rhs.offset != 0)
1279 bitmap_clear_bit (graph->direct_nodes, lhs.var);
1280 else if (lhs.offset != 0)
1281 bitmap_clear_bit (graph->direct_nodes, rhs.var);
1286 /* Build the constraint graph, adding successor edges. */
1288 static void
1289 build_succ_graph (void)
1291 unsigned i, t;
1292 constraint_t c;
1294 FOR_EACH_VEC_ELT (constraints, i, c)
1296 struct constraint_expr lhs;
1297 struct constraint_expr rhs;
1298 unsigned int lhsvar;
1299 unsigned int rhsvar;
1301 if (!c)
1302 continue;
1304 lhs = c->lhs;
1305 rhs = c->rhs;
1306 lhsvar = find (lhs.var);
1307 rhsvar = find (rhs.var);
1309 if (lhs.type == DEREF)
1311 if (rhs.offset == 0 && lhs.offset == 0 && rhs.type == SCALAR)
1312 add_graph_edge (graph, FIRST_REF_NODE + lhsvar, rhsvar);
1314 else if (rhs.type == DEREF)
1316 if (rhs.offset == 0 && lhs.offset == 0 && lhs.type == SCALAR)
1317 add_graph_edge (graph, lhsvar, FIRST_REF_NODE + rhsvar);
1319 else if (rhs.type == ADDRESSOF)
1321 /* x = &y */
1322 gcc_assert (find (rhs.var) == rhs.var);
1323 bitmap_set_bit (get_varinfo (lhsvar)->solution, rhsvar);
1325 else if (lhsvar > anything_id
1326 && lhsvar != rhsvar && lhs.offset == 0 && rhs.offset == 0)
1328 add_graph_edge (graph, lhsvar, rhsvar);
1332 /* Add edges from STOREDANYTHING to all non-direct nodes that can
1333 receive pointers. */
1334 t = find (storedanything_id);
1335 for (i = integer_id + 1; i < FIRST_REF_NODE; ++i)
1337 if (!bitmap_bit_p (graph->direct_nodes, i)
1338 && get_varinfo (i)->may_have_pointers)
1339 add_graph_edge (graph, find (i), t);
1342 /* Everything stored to ANYTHING also potentially escapes. */
1343 add_graph_edge (graph, find (escaped_id), t);
1347 /* Changed variables on the last iteration. */
1348 static bitmap changed;
1350 /* Strongly Connected Component visitation info. */
1352 struct scc_info
1354 sbitmap visited;
1355 sbitmap deleted;
1356 unsigned int *dfs;
1357 unsigned int *node_mapping;
1358 int current_index;
1359 vec<unsigned> scc_stack;
1363 /* Recursive routine to find strongly connected components in GRAPH.
1364 SI is the SCC info to store the information in, and N is the id of current
1365 graph node we are processing.
1367 This is Tarjan's strongly connected component finding algorithm, as
1368 modified by Nuutila to keep only non-root nodes on the stack.
1369 The algorithm can be found in "On finding the strongly connected
1370 connected components in a directed graph" by Esko Nuutila and Eljas
1371 Soisalon-Soininen, in Information Processing Letters volume 49,
1372 number 1, pages 9-14. */
1374 static void
1375 scc_visit (constraint_graph_t graph, struct scc_info *si, unsigned int n)
1377 unsigned int i;
1378 bitmap_iterator bi;
1379 unsigned int my_dfs;
1381 bitmap_set_bit (si->visited, n);
1382 si->dfs[n] = si->current_index ++;
1383 my_dfs = si->dfs[n];
1385 /* Visit all the successors. */
1386 EXECUTE_IF_IN_NONNULL_BITMAP (graph->succs[n], 0, i, bi)
1388 unsigned int w;
1390 if (i > LAST_REF_NODE)
1391 break;
1393 w = find (i);
1394 if (bitmap_bit_p (si->deleted, w))
1395 continue;
1397 if (!bitmap_bit_p (si->visited, w))
1398 scc_visit (graph, si, w);
1400 unsigned int t = find (w);
1401 unsigned int nnode = find (n);
1402 gcc_assert (nnode == n);
1404 if (si->dfs[t] < si->dfs[nnode])
1405 si->dfs[n] = si->dfs[t];
1409 /* See if any components have been identified. */
1410 if (si->dfs[n] == my_dfs)
1412 if (si->scc_stack.length () > 0
1413 && si->dfs[si->scc_stack.last ()] >= my_dfs)
1415 bitmap scc = BITMAP_ALLOC (NULL);
1416 unsigned int lowest_node;
1417 bitmap_iterator bi;
1419 bitmap_set_bit (scc, n);
1421 while (si->scc_stack.length () != 0
1422 && si->dfs[si->scc_stack.last ()] >= my_dfs)
1424 unsigned int w = si->scc_stack.pop ();
1426 bitmap_set_bit (scc, w);
1429 lowest_node = bitmap_first_set_bit (scc);
1430 gcc_assert (lowest_node < FIRST_REF_NODE);
1432 /* Collapse the SCC nodes into a single node, and mark the
1433 indirect cycles. */
1434 EXECUTE_IF_SET_IN_BITMAP (scc, 0, i, bi)
1436 if (i < FIRST_REF_NODE)
1438 if (unite (lowest_node, i))
1439 unify_nodes (graph, lowest_node, i, false);
1441 else
1443 unite (lowest_node, i);
1444 graph->indirect_cycles[i - FIRST_REF_NODE] = lowest_node;
1448 bitmap_set_bit (si->deleted, n);
1450 else
1451 si->scc_stack.safe_push (n);
1454 /* Unify node FROM into node TO, updating the changed count if
1455 necessary when UPDATE_CHANGED is true. */
1457 static void
1458 unify_nodes (constraint_graph_t graph, unsigned int to, unsigned int from,
1459 bool update_changed)
1462 gcc_assert (to != from && find (to) == to);
1463 if (dump_file && (dump_flags & TDF_DETAILS))
1464 fprintf (dump_file, "Unifying %s to %s\n",
1465 get_varinfo (from)->name,
1466 get_varinfo (to)->name);
1468 if (update_changed)
1469 stats.unified_vars_dynamic++;
1470 else
1471 stats.unified_vars_static++;
1473 merge_graph_nodes (graph, to, from);
1474 merge_node_constraints (graph, to, from);
1476 /* Mark TO as changed if FROM was changed. If TO was already marked
1477 as changed, decrease the changed count. */
1479 if (update_changed
1480 && bitmap_bit_p (changed, from))
1482 bitmap_clear_bit (changed, from);
1483 bitmap_set_bit (changed, to);
1485 if (get_varinfo (from)->solution)
1487 /* If the solution changes because of the merging, we need to mark
1488 the variable as changed. */
1489 if (bitmap_ior_into (get_varinfo (to)->solution,
1490 get_varinfo (from)->solution))
1492 if (update_changed)
1493 bitmap_set_bit (changed, to);
1496 BITMAP_FREE (get_varinfo (from)->solution);
1497 if (get_varinfo (from)->oldsolution)
1498 BITMAP_FREE (get_varinfo (from)->oldsolution);
1500 if (stats.iterations > 0
1501 && get_varinfo (to)->oldsolution)
1502 BITMAP_FREE (get_varinfo (to)->oldsolution);
1504 if (valid_graph_edge (graph, to, to))
1506 if (graph->succs[to])
1507 bitmap_clear_bit (graph->succs[to], to);
1511 /* Information needed to compute the topological ordering of a graph. */
1513 struct topo_info
1515 /* sbitmap of visited nodes. */
1516 sbitmap visited;
1517 /* Array that stores the topological order of the graph, *in
1518 reverse*. */
1519 vec<unsigned> topo_order;
1523 /* Initialize and return a topological info structure. */
1525 static struct topo_info *
1526 init_topo_info (void)
1528 size_t size = graph->size;
1529 struct topo_info *ti = XNEW (struct topo_info);
1530 ti->visited = sbitmap_alloc (size);
1531 bitmap_clear (ti->visited);
1532 ti->topo_order.create (1);
1533 return ti;
1537 /* Free the topological sort info pointed to by TI. */
1539 static void
1540 free_topo_info (struct topo_info *ti)
1542 sbitmap_free (ti->visited);
1543 ti->topo_order.release ();
1544 free (ti);
1547 /* Visit the graph in topological order, and store the order in the
1548 topo_info structure. */
1550 static void
1551 topo_visit (constraint_graph_t graph, struct topo_info *ti,
1552 unsigned int n)
1554 bitmap_iterator bi;
1555 unsigned int j;
1557 bitmap_set_bit (ti->visited, n);
1559 if (graph->succs[n])
1560 EXECUTE_IF_SET_IN_BITMAP (graph->succs[n], 0, j, bi)
1562 if (!bitmap_bit_p (ti->visited, j))
1563 topo_visit (graph, ti, j);
1566 ti->topo_order.safe_push (n);
1569 /* Process a constraint C that represents x = *(y + off), using DELTA as the
1570 starting solution for y. */
1572 static void
1573 do_sd_constraint (constraint_graph_t graph, constraint_t c,
1574 bitmap delta)
1576 unsigned int lhs = c->lhs.var;
1577 bool flag = false;
1578 bitmap sol = get_varinfo (lhs)->solution;
1579 unsigned int j;
1580 bitmap_iterator bi;
1581 HOST_WIDE_INT roffset = c->rhs.offset;
1583 /* Our IL does not allow this. */
1584 gcc_assert (c->lhs.offset == 0);
1586 /* If the solution of Y contains anything it is good enough to transfer
1587 this to the LHS. */
1588 if (bitmap_bit_p (delta, anything_id))
1590 flag |= bitmap_set_bit (sol, anything_id);
1591 goto done;
1594 /* If we do not know at with offset the rhs is dereferenced compute
1595 the reachability set of DELTA, conservatively assuming it is
1596 dereferenced at all valid offsets. */
1597 if (roffset == UNKNOWN_OFFSET)
1599 solution_set_expand (delta, delta);
1600 /* No further offset processing is necessary. */
1601 roffset = 0;
1604 /* For each variable j in delta (Sol(y)), add
1605 an edge in the graph from j to x, and union Sol(j) into Sol(x). */
1606 EXECUTE_IF_SET_IN_BITMAP (delta, 0, j, bi)
1608 varinfo_t v = get_varinfo (j);
1609 HOST_WIDE_INT fieldoffset = v->offset + roffset;
1610 unsigned int t;
1612 if (v->is_full_var)
1613 fieldoffset = v->offset;
1614 else if (roffset != 0)
1615 v = first_vi_for_offset (v, fieldoffset);
1616 /* If the access is outside of the variable we can ignore it. */
1617 if (!v)
1618 continue;
1622 t = find (v->id);
1624 /* Adding edges from the special vars is pointless.
1625 They don't have sets that can change. */
1626 if (get_varinfo (t)->is_special_var)
1627 flag |= bitmap_ior_into (sol, get_varinfo (t)->solution);
1628 /* Merging the solution from ESCAPED needlessly increases
1629 the set. Use ESCAPED as representative instead. */
1630 else if (v->id == escaped_id)
1631 flag |= bitmap_set_bit (sol, escaped_id);
1632 else if (v->may_have_pointers
1633 && add_graph_edge (graph, lhs, t))
1634 flag |= bitmap_ior_into (sol, get_varinfo (t)->solution);
1636 /* If the variable is not exactly at the requested offset
1637 we have to include the next one. */
1638 if (v->offset == (unsigned HOST_WIDE_INT)fieldoffset
1639 || v->next == NULL)
1640 break;
1642 v = v->next;
1643 fieldoffset = v->offset;
1645 while (1);
1648 done:
1649 /* If the LHS solution changed, mark the var as changed. */
1650 if (flag)
1652 get_varinfo (lhs)->solution = sol;
1653 bitmap_set_bit (changed, lhs);
1657 /* Process a constraint C that represents *(x + off) = y using DELTA
1658 as the starting solution for x. */
1660 static void
1661 do_ds_constraint (constraint_t c, bitmap delta)
1663 unsigned int rhs = c->rhs.var;
1664 bitmap sol = get_varinfo (rhs)->solution;
1665 unsigned int j;
1666 bitmap_iterator bi;
1667 HOST_WIDE_INT loff = c->lhs.offset;
1668 bool escaped_p = false;
1670 /* Our IL does not allow this. */
1671 gcc_assert (c->rhs.offset == 0);
1673 /* If the solution of y contains ANYTHING simply use the ANYTHING
1674 solution. This avoids needlessly increasing the points-to sets. */
1675 if (bitmap_bit_p (sol, anything_id))
1676 sol = get_varinfo (find (anything_id))->solution;
1678 /* If the solution for x contains ANYTHING we have to merge the
1679 solution of y into all pointer variables which we do via
1680 STOREDANYTHING. */
1681 if (bitmap_bit_p (delta, anything_id))
1683 unsigned t = find (storedanything_id);
1684 if (add_graph_edge (graph, t, rhs))
1686 if (bitmap_ior_into (get_varinfo (t)->solution, sol))
1687 bitmap_set_bit (changed, t);
1689 return;
1692 /* If we do not know at with offset the rhs is dereferenced compute
1693 the reachability set of DELTA, conservatively assuming it is
1694 dereferenced at all valid offsets. */
1695 if (loff == UNKNOWN_OFFSET)
1697 solution_set_expand (delta, delta);
1698 loff = 0;
1701 /* For each member j of delta (Sol(x)), add an edge from y to j and
1702 union Sol(y) into Sol(j) */
1703 EXECUTE_IF_SET_IN_BITMAP (delta, 0, j, bi)
1705 varinfo_t v = get_varinfo (j);
1706 unsigned int t;
1707 HOST_WIDE_INT fieldoffset = v->offset + loff;
1709 if (v->is_full_var)
1710 fieldoffset = v->offset;
1711 else if (loff != 0)
1712 v = first_vi_for_offset (v, fieldoffset);
1713 /* If the access is outside of the variable we can ignore it. */
1714 if (!v)
1715 continue;
1719 if (v->may_have_pointers)
1721 /* If v is a global variable then this is an escape point. */
1722 if (v->is_global_var
1723 && !escaped_p)
1725 t = find (escaped_id);
1726 if (add_graph_edge (graph, t, rhs)
1727 && bitmap_ior_into (get_varinfo (t)->solution, sol))
1728 bitmap_set_bit (changed, t);
1729 /* Enough to let rhs escape once. */
1730 escaped_p = true;
1733 if (v->is_special_var)
1734 break;
1736 t = find (v->id);
1737 if (add_graph_edge (graph, t, rhs)
1738 && bitmap_ior_into (get_varinfo (t)->solution, sol))
1739 bitmap_set_bit (changed, t);
1742 /* If the variable is not exactly at the requested offset
1743 we have to include the next one. */
1744 if (v->offset == (unsigned HOST_WIDE_INT)fieldoffset
1745 || v->next == NULL)
1746 break;
1748 v = v->next;
1749 fieldoffset = v->offset;
1751 while (1);
1755 /* Handle a non-simple (simple meaning requires no iteration),
1756 constraint (IE *x = &y, x = *y, *x = y, and x = y with offsets involved). */
1758 static void
1759 do_complex_constraint (constraint_graph_t graph, constraint_t c, bitmap delta)
1761 if (c->lhs.type == DEREF)
1763 if (c->rhs.type == ADDRESSOF)
1765 gcc_unreachable();
1767 else
1769 /* *x = y */
1770 do_ds_constraint (c, delta);
1773 else if (c->rhs.type == DEREF)
1775 /* x = *y */
1776 if (!(get_varinfo (c->lhs.var)->is_special_var))
1777 do_sd_constraint (graph, c, delta);
1779 else
1781 bitmap tmp;
1782 bitmap solution;
1783 bool flag = false;
1785 gcc_assert (c->rhs.type == SCALAR && c->lhs.type == SCALAR);
1786 solution = get_varinfo (c->rhs.var)->solution;
1787 tmp = get_varinfo (c->lhs.var)->solution;
1789 flag = set_union_with_increment (tmp, solution, c->rhs.offset);
1791 if (flag)
1793 get_varinfo (c->lhs.var)->solution = tmp;
1794 bitmap_set_bit (changed, c->lhs.var);
1799 /* Initialize and return a new SCC info structure. */
1801 static struct scc_info *
1802 init_scc_info (size_t size)
1804 struct scc_info *si = XNEW (struct scc_info);
1805 size_t i;
1807 si->current_index = 0;
1808 si->visited = sbitmap_alloc (size);
1809 bitmap_clear (si->visited);
1810 si->deleted = sbitmap_alloc (size);
1811 bitmap_clear (si->deleted);
1812 si->node_mapping = XNEWVEC (unsigned int, size);
1813 si->dfs = XCNEWVEC (unsigned int, size);
1815 for (i = 0; i < size; i++)
1816 si->node_mapping[i] = i;
1818 si->scc_stack.create (1);
1819 return si;
1822 /* Free an SCC info structure pointed to by SI */
1824 static void
1825 free_scc_info (struct scc_info *si)
1827 sbitmap_free (si->visited);
1828 sbitmap_free (si->deleted);
1829 free (si->node_mapping);
1830 free (si->dfs);
1831 si->scc_stack.release ();
1832 free (si);
1836 /* Find indirect cycles in GRAPH that occur, using strongly connected
1837 components, and note them in the indirect cycles map.
1839 This technique comes from Ben Hardekopf and Calvin Lin,
1840 "It Pays to be Lazy: Fast and Accurate Pointer Analysis for Millions of
1841 Lines of Code", submitted to PLDI 2007. */
1843 static void
1844 find_indirect_cycles (constraint_graph_t graph)
1846 unsigned int i;
1847 unsigned int size = graph->size;
1848 struct scc_info *si = init_scc_info (size);
1850 for (i = 0; i < MIN (LAST_REF_NODE, size); i ++ )
1851 if (!bitmap_bit_p (si->visited, i) && find (i) == i)
1852 scc_visit (graph, si, i);
1854 free_scc_info (si);
1857 /* Compute a topological ordering for GRAPH, and store the result in the
1858 topo_info structure TI. */
1860 static void
1861 compute_topo_order (constraint_graph_t graph,
1862 struct topo_info *ti)
1864 unsigned int i;
1865 unsigned int size = graph->size;
1867 for (i = 0; i != size; ++i)
1868 if (!bitmap_bit_p (ti->visited, i) && find (i) == i)
1869 topo_visit (graph, ti, i);
1872 /* Structure used to for hash value numbering of pointer equivalence
1873 classes. */
1875 typedef struct equiv_class_label
1877 hashval_t hashcode;
1878 unsigned int equivalence_class;
1879 bitmap labels;
1880 } *equiv_class_label_t;
1881 typedef const struct equiv_class_label *const_equiv_class_label_t;
1883 /* A hashtable for mapping a bitmap of labels->pointer equivalence
1884 classes. */
1885 static htab_t pointer_equiv_class_table;
1887 /* A hashtable for mapping a bitmap of labels->location equivalence
1888 classes. */
1889 static htab_t location_equiv_class_table;
1891 /* Hash function for a equiv_class_label_t */
1893 static hashval_t
1894 equiv_class_label_hash (const void *p)
1896 const_equiv_class_label_t const ecl = (const_equiv_class_label_t) p;
1897 return ecl->hashcode;
1900 /* Equality function for two equiv_class_label_t's. */
1902 static int
1903 equiv_class_label_eq (const void *p1, const void *p2)
1905 const_equiv_class_label_t const eql1 = (const_equiv_class_label_t) p1;
1906 const_equiv_class_label_t const eql2 = (const_equiv_class_label_t) p2;
1907 return (eql1->hashcode == eql2->hashcode
1908 && bitmap_equal_p (eql1->labels, eql2->labels));
1911 /* Lookup a equivalence class in TABLE by the bitmap of LABELS with
1912 hash HAS it contains. Sets *REF_LABELS to the bitmap LABELS
1913 is equivalent to. */
1915 static equiv_class_label *
1916 equiv_class_lookup_or_add (htab_t table, bitmap labels)
1918 equiv_class_label **slot;
1919 equiv_class_label ecl;
1921 ecl.labels = labels;
1922 ecl.hashcode = bitmap_hash (labels);
1923 slot = (equiv_class_label **) htab_find_slot_with_hash (table, &ecl,
1924 ecl.hashcode, INSERT);
1925 if (!*slot)
1927 *slot = XNEW (struct equiv_class_label);
1928 (*slot)->labels = labels;
1929 (*slot)->hashcode = ecl.hashcode;
1930 (*slot)->equivalence_class = 0;
1933 return *slot;
1936 /* Perform offline variable substitution.
1938 This is a worst case quadratic time way of identifying variables
1939 that must have equivalent points-to sets, including those caused by
1940 static cycles, and single entry subgraphs, in the constraint graph.
1942 The technique is described in "Exploiting Pointer and Location
1943 Equivalence to Optimize Pointer Analysis. In the 14th International
1944 Static Analysis Symposium (SAS), August 2007." It is known as the
1945 "HU" algorithm, and is equivalent to value numbering the collapsed
1946 constraint graph including evaluating unions.
1948 The general method of finding equivalence classes is as follows:
1949 Add fake nodes (REF nodes) and edges for *a = b and a = *b constraints.
1950 Initialize all non-REF nodes to be direct nodes.
1951 For each constraint a = a U {b}, we set pts(a) = pts(a) u {fresh
1952 variable}
1953 For each constraint containing the dereference, we also do the same
1954 thing.
1956 We then compute SCC's in the graph and unify nodes in the same SCC,
1957 including pts sets.
1959 For each non-collapsed node x:
1960 Visit all unvisited explicit incoming edges.
1961 Ignoring all non-pointers, set pts(x) = Union of pts(a) for y
1962 where y->x.
1963 Lookup the equivalence class for pts(x).
1964 If we found one, equivalence_class(x) = found class.
1965 Otherwise, equivalence_class(x) = new class, and new_class is
1966 added to the lookup table.
1968 All direct nodes with the same equivalence class can be replaced
1969 with a single representative node.
1970 All unlabeled nodes (label == 0) are not pointers and all edges
1971 involving them can be eliminated.
1972 We perform these optimizations during rewrite_constraints
1974 In addition to pointer equivalence class finding, we also perform
1975 location equivalence class finding. This is the set of variables
1976 that always appear together in points-to sets. We use this to
1977 compress the size of the points-to sets. */
1979 /* Current maximum pointer equivalence class id. */
1980 static int pointer_equiv_class;
1982 /* Current maximum location equivalence class id. */
1983 static int location_equiv_class;
1985 /* Recursive routine to find strongly connected components in GRAPH,
1986 and label it's nodes with DFS numbers. */
1988 static void
1989 condense_visit (constraint_graph_t graph, struct scc_info *si, unsigned int n)
1991 unsigned int i;
1992 bitmap_iterator bi;
1993 unsigned int my_dfs;
1995 gcc_assert (si->node_mapping[n] == n);
1996 bitmap_set_bit (si->visited, n);
1997 si->dfs[n] = si->current_index ++;
1998 my_dfs = si->dfs[n];
2000 /* Visit all the successors. */
2001 EXECUTE_IF_IN_NONNULL_BITMAP (graph->preds[n], 0, i, bi)
2003 unsigned int w = si->node_mapping[i];
2005 if (bitmap_bit_p (si->deleted, w))
2006 continue;
2008 if (!bitmap_bit_p (si->visited, w))
2009 condense_visit (graph, si, w);
2011 unsigned int t = si->node_mapping[w];
2012 unsigned int nnode = si->node_mapping[n];
2013 gcc_assert (nnode == n);
2015 if (si->dfs[t] < si->dfs[nnode])
2016 si->dfs[n] = si->dfs[t];
2020 /* Visit all the implicit predecessors. */
2021 EXECUTE_IF_IN_NONNULL_BITMAP (graph->implicit_preds[n], 0, i, bi)
2023 unsigned int w = si->node_mapping[i];
2025 if (bitmap_bit_p (si->deleted, w))
2026 continue;
2028 if (!bitmap_bit_p (si->visited, w))
2029 condense_visit (graph, si, w);
2031 unsigned int t = si->node_mapping[w];
2032 unsigned int nnode = si->node_mapping[n];
2033 gcc_assert (nnode == n);
2035 if (si->dfs[t] < si->dfs[nnode])
2036 si->dfs[n] = si->dfs[t];
2040 /* See if any components have been identified. */
2041 if (si->dfs[n] == my_dfs)
2043 while (si->scc_stack.length () != 0
2044 && si->dfs[si->scc_stack.last ()] >= my_dfs)
2046 unsigned int w = si->scc_stack.pop ();
2047 si->node_mapping[w] = n;
2049 if (!bitmap_bit_p (graph->direct_nodes, w))
2050 bitmap_clear_bit (graph->direct_nodes, n);
2052 /* Unify our nodes. */
2053 if (graph->preds[w])
2055 if (!graph->preds[n])
2056 graph->preds[n] = BITMAP_ALLOC (&predbitmap_obstack);
2057 bitmap_ior_into (graph->preds[n], graph->preds[w]);
2059 if (graph->implicit_preds[w])
2061 if (!graph->implicit_preds[n])
2062 graph->implicit_preds[n] = BITMAP_ALLOC (&predbitmap_obstack);
2063 bitmap_ior_into (graph->implicit_preds[n],
2064 graph->implicit_preds[w]);
2066 if (graph->points_to[w])
2068 if (!graph->points_to[n])
2069 graph->points_to[n] = BITMAP_ALLOC (&predbitmap_obstack);
2070 bitmap_ior_into (graph->points_to[n],
2071 graph->points_to[w]);
2074 bitmap_set_bit (si->deleted, n);
2076 else
2077 si->scc_stack.safe_push (n);
2080 /* Label pointer equivalences. */
2082 static void
2083 label_visit (constraint_graph_t graph, struct scc_info *si, unsigned int n)
2085 unsigned int i, first_pred;
2086 bitmap_iterator bi;
2088 bitmap_set_bit (si->visited, n);
2090 /* Label and union our incoming edges's points to sets. */
2091 first_pred = -1U;
2092 EXECUTE_IF_IN_NONNULL_BITMAP (graph->preds[n], 0, i, bi)
2094 unsigned int w = si->node_mapping[i];
2095 if (!bitmap_bit_p (si->visited, w))
2096 label_visit (graph, si, w);
2098 /* Skip unused edges */
2099 if (w == n || graph->pointer_label[w] == 0)
2100 continue;
2102 if (graph->points_to[w])
2104 if (!graph->points_to[n])
2106 if (first_pred == -1U)
2107 first_pred = w;
2108 else
2110 graph->points_to[n] = BITMAP_ALLOC (&predbitmap_obstack);
2111 bitmap_ior (graph->points_to[n],
2112 graph->points_to[first_pred],
2113 graph->points_to[w]);
2116 else
2117 bitmap_ior_into(graph->points_to[n], graph->points_to[w]);
2121 /* Indirect nodes get fresh variables and a new pointer equiv class. */
2122 if (!bitmap_bit_p (graph->direct_nodes, n))
2124 if (!graph->points_to[n])
2126 graph->points_to[n] = BITMAP_ALLOC (&predbitmap_obstack);
2127 if (first_pred != -1U)
2128 bitmap_copy (graph->points_to[n], graph->points_to[first_pred]);
2130 bitmap_set_bit (graph->points_to[n], FIRST_REF_NODE + n);
2131 graph->pointer_label[n] = pointer_equiv_class++;
2132 equiv_class_label_t ecl;
2133 ecl = equiv_class_lookup_or_add (pointer_equiv_class_table,
2134 graph->points_to[n]);
2135 ecl->equivalence_class = graph->pointer_label[n];
2136 return;
2139 /* If there was only a single non-empty predecessor the pointer equiv
2140 class is the same. */
2141 if (!graph->points_to[n])
2143 if (first_pred != -1U)
2145 graph->pointer_label[n] = graph->pointer_label[first_pred];
2146 graph->points_to[n] = graph->points_to[first_pred];
2148 return;
2151 if (!bitmap_empty_p (graph->points_to[n]))
2153 equiv_class_label_t ecl;
2154 ecl = equiv_class_lookup_or_add (pointer_equiv_class_table,
2155 graph->points_to[n]);
2156 if (ecl->equivalence_class == 0)
2157 ecl->equivalence_class = pointer_equiv_class++;
2158 else
2160 BITMAP_FREE (graph->points_to[n]);
2161 graph->points_to[n] = ecl->labels;
2163 graph->pointer_label[n] = ecl->equivalence_class;
2167 /* Perform offline variable substitution, discovering equivalence
2168 classes, and eliminating non-pointer variables. */
2170 static struct scc_info *
2171 perform_var_substitution (constraint_graph_t graph)
2173 unsigned int i;
2174 unsigned int size = graph->size;
2175 struct scc_info *si = init_scc_info (size);
2177 bitmap_obstack_initialize (&iteration_obstack);
2178 pointer_equiv_class_table = htab_create (511, equiv_class_label_hash,
2179 equiv_class_label_eq, free);
2180 location_equiv_class_table = htab_create (511, equiv_class_label_hash,
2181 equiv_class_label_eq, free);
2182 pointer_equiv_class = 1;
2183 location_equiv_class = 1;
2185 /* Condense the nodes, which means to find SCC's, count incoming
2186 predecessors, and unite nodes in SCC's. */
2187 for (i = 0; i < FIRST_REF_NODE; i++)
2188 if (!bitmap_bit_p (si->visited, si->node_mapping[i]))
2189 condense_visit (graph, si, si->node_mapping[i]);
2191 bitmap_clear (si->visited);
2192 /* Actually the label the nodes for pointer equivalences */
2193 for (i = 0; i < FIRST_REF_NODE; i++)
2194 if (!bitmap_bit_p (si->visited, si->node_mapping[i]))
2195 label_visit (graph, si, si->node_mapping[i]);
2197 /* Calculate location equivalence labels. */
2198 for (i = 0; i < FIRST_REF_NODE; i++)
2200 bitmap pointed_by;
2201 bitmap_iterator bi;
2202 unsigned int j;
2204 if (!graph->pointed_by[i])
2205 continue;
2206 pointed_by = BITMAP_ALLOC (&iteration_obstack);
2208 /* Translate the pointed-by mapping for pointer equivalence
2209 labels. */
2210 EXECUTE_IF_SET_IN_BITMAP (graph->pointed_by[i], 0, j, bi)
2212 bitmap_set_bit (pointed_by,
2213 graph->pointer_label[si->node_mapping[j]]);
2215 /* The original pointed_by is now dead. */
2216 BITMAP_FREE (graph->pointed_by[i]);
2218 /* Look up the location equivalence label if one exists, or make
2219 one otherwise. */
2220 equiv_class_label_t ecl;
2221 ecl = equiv_class_lookup_or_add (location_equiv_class_table, pointed_by);
2222 if (ecl->equivalence_class == 0)
2223 ecl->equivalence_class = location_equiv_class++;
2224 else
2226 if (dump_file && (dump_flags & TDF_DETAILS))
2227 fprintf (dump_file, "Found location equivalence for node %s\n",
2228 get_varinfo (i)->name);
2229 BITMAP_FREE (pointed_by);
2231 graph->loc_label[i] = ecl->equivalence_class;
2235 if (dump_file && (dump_flags & TDF_DETAILS))
2236 for (i = 0; i < FIRST_REF_NODE; i++)
2238 unsigned j = si->node_mapping[i];
2239 if (j != i)
2241 fprintf (dump_file, "%s node id %d ",
2242 bitmap_bit_p (graph->direct_nodes, i)
2243 ? "Direct" : "Indirect", i);
2244 if (i < FIRST_REF_NODE)
2245 fprintf (dump_file, "\"%s\"", get_varinfo (i)->name);
2246 else
2247 fprintf (dump_file, "\"*%s\"",
2248 get_varinfo (i - FIRST_REF_NODE)->name);
2249 fprintf (dump_file, " mapped to SCC leader node id %d ", j);
2250 if (j < FIRST_REF_NODE)
2251 fprintf (dump_file, "\"%s\"\n", get_varinfo (j)->name);
2252 else
2253 fprintf (dump_file, "\"*%s\"\n",
2254 get_varinfo (j - FIRST_REF_NODE)->name);
2256 else
2258 fprintf (dump_file,
2259 "Equivalence classes for %s node id %d ",
2260 bitmap_bit_p (graph->direct_nodes, i)
2261 ? "direct" : "indirect", i);
2262 if (i < FIRST_REF_NODE)
2263 fprintf (dump_file, "\"%s\"", get_varinfo (i)->name);
2264 else
2265 fprintf (dump_file, "\"*%s\"",
2266 get_varinfo (i - FIRST_REF_NODE)->name);
2267 fprintf (dump_file,
2268 ": pointer %d, location %d\n",
2269 graph->pointer_label[i], graph->loc_label[i]);
2273 /* Quickly eliminate our non-pointer variables. */
2275 for (i = 0; i < FIRST_REF_NODE; i++)
2277 unsigned int node = si->node_mapping[i];
2279 if (graph->pointer_label[node] == 0)
2281 if (dump_file && (dump_flags & TDF_DETAILS))
2282 fprintf (dump_file,
2283 "%s is a non-pointer variable, eliminating edges.\n",
2284 get_varinfo (node)->name);
2285 stats.nonpointer_vars++;
2286 clear_edges_for_node (graph, node);
2290 return si;
2293 /* Free information that was only necessary for variable
2294 substitution. */
2296 static void
2297 free_var_substitution_info (struct scc_info *si)
2299 free_scc_info (si);
2300 free (graph->pointer_label);
2301 free (graph->loc_label);
2302 free (graph->pointed_by);
2303 free (graph->points_to);
2304 free (graph->eq_rep);
2305 sbitmap_free (graph->direct_nodes);
2306 htab_delete (pointer_equiv_class_table);
2307 htab_delete (location_equiv_class_table);
2308 bitmap_obstack_release (&iteration_obstack);
2311 /* Return an existing node that is equivalent to NODE, which has
2312 equivalence class LABEL, if one exists. Return NODE otherwise. */
2314 static unsigned int
2315 find_equivalent_node (constraint_graph_t graph,
2316 unsigned int node, unsigned int label)
2318 /* If the address version of this variable is unused, we can
2319 substitute it for anything else with the same label.
2320 Otherwise, we know the pointers are equivalent, but not the
2321 locations, and we can unite them later. */
2323 if (!bitmap_bit_p (graph->address_taken, node))
2325 gcc_assert (label < graph->size);
2327 if (graph->eq_rep[label] != -1)
2329 /* Unify the two variables since we know they are equivalent. */
2330 if (unite (graph->eq_rep[label], node))
2331 unify_nodes (graph, graph->eq_rep[label], node, false);
2332 return graph->eq_rep[label];
2334 else
2336 graph->eq_rep[label] = node;
2337 graph->pe_rep[label] = node;
2340 else
2342 gcc_assert (label < graph->size);
2343 graph->pe[node] = label;
2344 if (graph->pe_rep[label] == -1)
2345 graph->pe_rep[label] = node;
2348 return node;
2351 /* Unite pointer equivalent but not location equivalent nodes in
2352 GRAPH. This may only be performed once variable substitution is
2353 finished. */
2355 static void
2356 unite_pointer_equivalences (constraint_graph_t graph)
2358 unsigned int i;
2360 /* Go through the pointer equivalences and unite them to their
2361 representative, if they aren't already. */
2362 for (i = 0; i < FIRST_REF_NODE; i++)
2364 unsigned int label = graph->pe[i];
2365 if (label)
2367 int label_rep = graph->pe_rep[label];
2369 if (label_rep == -1)
2370 continue;
2372 label_rep = find (label_rep);
2373 if (label_rep >= 0 && unite (label_rep, find (i)))
2374 unify_nodes (graph, label_rep, i, false);
2379 /* Move complex constraints to the GRAPH nodes they belong to. */
2381 static void
2382 move_complex_constraints (constraint_graph_t graph)
2384 int i;
2385 constraint_t c;
2387 FOR_EACH_VEC_ELT (constraints, i, c)
2389 if (c)
2391 struct constraint_expr lhs = c->lhs;
2392 struct constraint_expr rhs = c->rhs;
2394 if (lhs.type == DEREF)
2396 insert_into_complex (graph, lhs.var, c);
2398 else if (rhs.type == DEREF)
2400 if (!(get_varinfo (lhs.var)->is_special_var))
2401 insert_into_complex (graph, rhs.var, c);
2403 else if (rhs.type != ADDRESSOF && lhs.var > anything_id
2404 && (lhs.offset != 0 || rhs.offset != 0))
2406 insert_into_complex (graph, rhs.var, c);
2413 /* Optimize and rewrite complex constraints while performing
2414 collapsing of equivalent nodes. SI is the SCC_INFO that is the
2415 result of perform_variable_substitution. */
2417 static void
2418 rewrite_constraints (constraint_graph_t graph,
2419 struct scc_info *si)
2421 int i;
2422 unsigned int j;
2423 constraint_t c;
2425 for (j = 0; j < graph->size; j++)
2426 gcc_assert (find (j) == j);
2428 FOR_EACH_VEC_ELT (constraints, i, c)
2430 struct constraint_expr lhs = c->lhs;
2431 struct constraint_expr rhs = c->rhs;
2432 unsigned int lhsvar = find (lhs.var);
2433 unsigned int rhsvar = find (rhs.var);
2434 unsigned int lhsnode, rhsnode;
2435 unsigned int lhslabel, rhslabel;
2437 lhsnode = si->node_mapping[lhsvar];
2438 rhsnode = si->node_mapping[rhsvar];
2439 lhslabel = graph->pointer_label[lhsnode];
2440 rhslabel = graph->pointer_label[rhsnode];
2442 /* See if it is really a non-pointer variable, and if so, ignore
2443 the constraint. */
2444 if (lhslabel == 0)
2446 if (dump_file && (dump_flags & TDF_DETAILS))
2449 fprintf (dump_file, "%s is a non-pointer variable,"
2450 "ignoring constraint:",
2451 get_varinfo (lhs.var)->name);
2452 dump_constraint (dump_file, c);
2453 fprintf (dump_file, "\n");
2455 constraints[i] = NULL;
2456 continue;
2459 if (rhslabel == 0)
2461 if (dump_file && (dump_flags & TDF_DETAILS))
2464 fprintf (dump_file, "%s is a non-pointer variable,"
2465 "ignoring constraint:",
2466 get_varinfo (rhs.var)->name);
2467 dump_constraint (dump_file, c);
2468 fprintf (dump_file, "\n");
2470 constraints[i] = NULL;
2471 continue;
2474 lhsvar = find_equivalent_node (graph, lhsvar, lhslabel);
2475 rhsvar = find_equivalent_node (graph, rhsvar, rhslabel);
2476 c->lhs.var = lhsvar;
2477 c->rhs.var = rhsvar;
2482 /* Eliminate indirect cycles involving NODE. Return true if NODE was
2483 part of an SCC, false otherwise. */
2485 static bool
2486 eliminate_indirect_cycles (unsigned int node)
2488 if (graph->indirect_cycles[node] != -1
2489 && !bitmap_empty_p (get_varinfo (node)->solution))
2491 unsigned int i;
2492 vec<unsigned> queue = vNULL;
2493 int queuepos;
2494 unsigned int to = find (graph->indirect_cycles[node]);
2495 bitmap_iterator bi;
2497 /* We can't touch the solution set and call unify_nodes
2498 at the same time, because unify_nodes is going to do
2499 bitmap unions into it. */
2501 EXECUTE_IF_SET_IN_BITMAP (get_varinfo (node)->solution, 0, i, bi)
2503 if (find (i) == i && i != to)
2505 if (unite (to, i))
2506 queue.safe_push (i);
2510 for (queuepos = 0;
2511 queue.iterate (queuepos, &i);
2512 queuepos++)
2514 unify_nodes (graph, to, i, true);
2516 queue.release ();
2517 return true;
2519 return false;
2522 /* Solve the constraint graph GRAPH using our worklist solver.
2523 This is based on the PW* family of solvers from the "Efficient Field
2524 Sensitive Pointer Analysis for C" paper.
2525 It works by iterating over all the graph nodes, processing the complex
2526 constraints and propagating the copy constraints, until everything stops
2527 changed. This corresponds to steps 6-8 in the solving list given above. */
2529 static void
2530 solve_graph (constraint_graph_t graph)
2532 unsigned int size = graph->size;
2533 unsigned int i;
2534 bitmap pts;
2536 changed = BITMAP_ALLOC (NULL);
2538 /* Mark all initial non-collapsed nodes as changed. */
2539 for (i = 0; i < size; i++)
2541 varinfo_t ivi = get_varinfo (i);
2542 if (find (i) == i && !bitmap_empty_p (ivi->solution)
2543 && ((graph->succs[i] && !bitmap_empty_p (graph->succs[i]))
2544 || graph->complex[i].length () > 0))
2545 bitmap_set_bit (changed, i);
2548 /* Allocate a bitmap to be used to store the changed bits. */
2549 pts = BITMAP_ALLOC (&pta_obstack);
2551 while (!bitmap_empty_p (changed))
2553 unsigned int i;
2554 struct topo_info *ti = init_topo_info ();
2555 stats.iterations++;
2557 bitmap_obstack_initialize (&iteration_obstack);
2559 compute_topo_order (graph, ti);
2561 while (ti->topo_order.length () != 0)
2564 i = ti->topo_order.pop ();
2566 /* If this variable is not a representative, skip it. */
2567 if (find (i) != i)
2568 continue;
2570 /* In certain indirect cycle cases, we may merge this
2571 variable to another. */
2572 if (eliminate_indirect_cycles (i) && find (i) != i)
2573 continue;
2575 /* If the node has changed, we need to process the
2576 complex constraints and outgoing edges again. */
2577 if (bitmap_clear_bit (changed, i))
2579 unsigned int j;
2580 constraint_t c;
2581 bitmap solution;
2582 vec<constraint_t> complex = graph->complex[i];
2583 varinfo_t vi = get_varinfo (i);
2584 bool solution_empty;
2586 /* Compute the changed set of solution bits. */
2587 if (vi->oldsolution)
2588 bitmap_and_compl (pts, vi->solution, vi->oldsolution);
2589 else
2590 bitmap_copy (pts, vi->solution);
2592 if (bitmap_empty_p (pts))
2593 continue;
2595 if (vi->oldsolution)
2596 bitmap_ior_into (vi->oldsolution, pts);
2597 else
2599 vi->oldsolution = BITMAP_ALLOC (&oldpta_obstack);
2600 bitmap_copy (vi->oldsolution, pts);
2603 solution = vi->solution;
2604 solution_empty = bitmap_empty_p (solution);
2606 /* Process the complex constraints */
2607 FOR_EACH_VEC_ELT (complex, j, c)
2609 /* XXX: This is going to unsort the constraints in
2610 some cases, which will occasionally add duplicate
2611 constraints during unification. This does not
2612 affect correctness. */
2613 c->lhs.var = find (c->lhs.var);
2614 c->rhs.var = find (c->rhs.var);
2616 /* The only complex constraint that can change our
2617 solution to non-empty, given an empty solution,
2618 is a constraint where the lhs side is receiving
2619 some set from elsewhere. */
2620 if (!solution_empty || c->lhs.type != DEREF)
2621 do_complex_constraint (graph, c, pts);
2624 solution_empty = bitmap_empty_p (solution);
2626 if (!solution_empty)
2628 bitmap_iterator bi;
2629 unsigned eff_escaped_id = find (escaped_id);
2631 /* Propagate solution to all successors. */
2632 EXECUTE_IF_IN_NONNULL_BITMAP (graph->succs[i],
2633 0, j, bi)
2635 bitmap tmp;
2636 bool flag;
2638 unsigned int to = find (j);
2639 tmp = get_varinfo (to)->solution;
2640 flag = false;
2642 /* Don't try to propagate to ourselves. */
2643 if (to == i)
2644 continue;
2646 /* If we propagate from ESCAPED use ESCAPED as
2647 placeholder. */
2648 if (i == eff_escaped_id)
2649 flag = bitmap_set_bit (tmp, escaped_id);
2650 else
2651 flag = set_union_with_increment (tmp, pts, 0);
2653 if (flag)
2655 get_varinfo (to)->solution = tmp;
2656 bitmap_set_bit (changed, to);
2662 free_topo_info (ti);
2663 bitmap_obstack_release (&iteration_obstack);
2666 BITMAP_FREE (pts);
2667 BITMAP_FREE (changed);
2668 bitmap_obstack_release (&oldpta_obstack);
2671 /* Map from trees to variable infos. */
2672 static struct pointer_map_t *vi_for_tree;
2675 /* Insert ID as the variable id for tree T in the vi_for_tree map. */
2677 static void
2678 insert_vi_for_tree (tree t, varinfo_t vi)
2680 void **slot = pointer_map_insert (vi_for_tree, t);
2681 gcc_assert (vi);
2682 gcc_assert (*slot == NULL);
2683 *slot = vi;
2686 /* Find the variable info for tree T in VI_FOR_TREE. If T does not
2687 exist in the map, return NULL, otherwise, return the varinfo we found. */
2689 static varinfo_t
2690 lookup_vi_for_tree (tree t)
2692 void **slot = pointer_map_contains (vi_for_tree, t);
2693 if (slot == NULL)
2694 return NULL;
2696 return (varinfo_t) *slot;
2699 /* Return a printable name for DECL */
2701 static const char *
2702 alias_get_name (tree decl)
2704 const char *res = NULL;
2705 char *temp;
2706 int num_printed = 0;
2708 if (!dump_file)
2709 return "NULL";
2711 if (TREE_CODE (decl) == SSA_NAME)
2713 res = get_name (decl);
2714 if (res)
2715 num_printed = asprintf (&temp, "%s_%u", res, SSA_NAME_VERSION (decl));
2716 else
2717 num_printed = asprintf (&temp, "_%u", SSA_NAME_VERSION (decl));
2718 if (num_printed > 0)
2720 res = ggc_strdup (temp);
2721 free (temp);
2724 else if (DECL_P (decl))
2726 if (DECL_ASSEMBLER_NAME_SET_P (decl))
2727 res = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
2728 else
2730 res = get_name (decl);
2731 if (!res)
2733 num_printed = asprintf (&temp, "D.%u", DECL_UID (decl));
2734 if (num_printed > 0)
2736 res = ggc_strdup (temp);
2737 free (temp);
2742 if (res != NULL)
2743 return res;
2745 return "NULL";
2748 /* Find the variable id for tree T in the map.
2749 If T doesn't exist in the map, create an entry for it and return it. */
2751 static varinfo_t
2752 get_vi_for_tree (tree t)
2754 void **slot = pointer_map_contains (vi_for_tree, t);
2755 if (slot == NULL)
2756 return get_varinfo (create_variable_info_for (t, alias_get_name (t)));
2758 return (varinfo_t) *slot;
2761 /* Get a scalar constraint expression for a new temporary variable. */
2763 static struct constraint_expr
2764 new_scalar_tmp_constraint_exp (const char *name)
2766 struct constraint_expr tmp;
2767 varinfo_t vi;
2769 vi = new_var_info (NULL_TREE, name);
2770 vi->offset = 0;
2771 vi->size = -1;
2772 vi->fullsize = -1;
2773 vi->is_full_var = 1;
2775 tmp.var = vi->id;
2776 tmp.type = SCALAR;
2777 tmp.offset = 0;
2779 return tmp;
2782 /* Get a constraint expression vector from an SSA_VAR_P node.
2783 If address_p is true, the result will be taken its address of. */
2785 static void
2786 get_constraint_for_ssa_var (tree t, vec<ce_s> *results, bool address_p)
2788 struct constraint_expr cexpr;
2789 varinfo_t vi;
2791 /* We allow FUNCTION_DECLs here even though it doesn't make much sense. */
2792 gcc_assert (TREE_CODE (t) == SSA_NAME || DECL_P (t));
2794 /* For parameters, get at the points-to set for the actual parm
2795 decl. */
2796 if (TREE_CODE (t) == SSA_NAME
2797 && SSA_NAME_IS_DEFAULT_DEF (t)
2798 && (TREE_CODE (SSA_NAME_VAR (t)) == PARM_DECL
2799 || TREE_CODE (SSA_NAME_VAR (t)) == RESULT_DECL))
2801 get_constraint_for_ssa_var (SSA_NAME_VAR (t), results, address_p);
2802 return;
2805 /* For global variables resort to the alias target. */
2806 if (TREE_CODE (t) == VAR_DECL
2807 && (TREE_STATIC (t) || DECL_EXTERNAL (t)))
2809 struct varpool_node *node = varpool_get_node (t);
2810 if (node && node->alias)
2812 node = varpool_variable_node (node, NULL);
2813 t = node->symbol.decl;
2817 vi = get_vi_for_tree (t);
2818 cexpr.var = vi->id;
2819 cexpr.type = SCALAR;
2820 cexpr.offset = 0;
2821 /* If we determine the result is "anything", and we know this is readonly,
2822 say it points to readonly memory instead. */
2823 if (cexpr.var == anything_id && TREE_READONLY (t))
2825 gcc_unreachable ();
2826 cexpr.type = ADDRESSOF;
2827 cexpr.var = readonly_id;
2830 /* If we are not taking the address of the constraint expr, add all
2831 sub-fiels of the variable as well. */
2832 if (!address_p
2833 && !vi->is_full_var)
2835 for (; vi; vi = vi->next)
2837 cexpr.var = vi->id;
2838 results->safe_push (cexpr);
2840 return;
2843 results->safe_push (cexpr);
2846 /* Process constraint T, performing various simplifications and then
2847 adding it to our list of overall constraints. */
2849 static void
2850 process_constraint (constraint_t t)
2852 struct constraint_expr rhs = t->rhs;
2853 struct constraint_expr lhs = t->lhs;
2855 gcc_assert (rhs.var < varmap.length ());
2856 gcc_assert (lhs.var < varmap.length ());
2858 /* If we didn't get any useful constraint from the lhs we get
2859 &ANYTHING as fallback from get_constraint_for. Deal with
2860 it here by turning it into *ANYTHING. */
2861 if (lhs.type == ADDRESSOF
2862 && lhs.var == anything_id)
2863 lhs.type = DEREF;
2865 /* ADDRESSOF on the lhs is invalid. */
2866 gcc_assert (lhs.type != ADDRESSOF);
2868 /* We shouldn't add constraints from things that cannot have pointers.
2869 It's not completely trivial to avoid in the callers, so do it here. */
2870 if (rhs.type != ADDRESSOF
2871 && !get_varinfo (rhs.var)->may_have_pointers)
2872 return;
2874 /* Likewise adding to the solution of a non-pointer var isn't useful. */
2875 if (!get_varinfo (lhs.var)->may_have_pointers)
2876 return;
2878 /* This can happen in our IR with things like n->a = *p */
2879 if (rhs.type == DEREF && lhs.type == DEREF && rhs.var != anything_id)
2881 /* Split into tmp = *rhs, *lhs = tmp */
2882 struct constraint_expr tmplhs;
2883 tmplhs = new_scalar_tmp_constraint_exp ("doubledereftmp");
2884 process_constraint (new_constraint (tmplhs, rhs));
2885 process_constraint (new_constraint (lhs, tmplhs));
2887 else if (rhs.type == ADDRESSOF && lhs.type == DEREF)
2889 /* Split into tmp = &rhs, *lhs = tmp */
2890 struct constraint_expr tmplhs;
2891 tmplhs = new_scalar_tmp_constraint_exp ("derefaddrtmp");
2892 process_constraint (new_constraint (tmplhs, rhs));
2893 process_constraint (new_constraint (lhs, tmplhs));
2895 else
2897 gcc_assert (rhs.type != ADDRESSOF || rhs.offset == 0);
2898 constraints.safe_push (t);
2903 /* Return the position, in bits, of FIELD_DECL from the beginning of its
2904 structure. */
2906 static HOST_WIDE_INT
2907 bitpos_of_field (const tree fdecl)
2909 if (!host_integerp (DECL_FIELD_OFFSET (fdecl), 0)
2910 || !host_integerp (DECL_FIELD_BIT_OFFSET (fdecl), 0))
2911 return -1;
2913 return (TREE_INT_CST_LOW (DECL_FIELD_OFFSET (fdecl)) * BITS_PER_UNIT
2914 + TREE_INT_CST_LOW (DECL_FIELD_BIT_OFFSET (fdecl)));
2918 /* Get constraint expressions for offsetting PTR by OFFSET. Stores the
2919 resulting constraint expressions in *RESULTS. */
2921 static void
2922 get_constraint_for_ptr_offset (tree ptr, tree offset,
2923 vec<ce_s> *results)
2925 struct constraint_expr c;
2926 unsigned int j, n;
2927 HOST_WIDE_INT rhsoffset;
2929 /* If we do not do field-sensitive PTA adding offsets to pointers
2930 does not change the points-to solution. */
2931 if (!use_field_sensitive)
2933 get_constraint_for_rhs (ptr, results);
2934 return;
2937 /* If the offset is not a non-negative integer constant that fits
2938 in a HOST_WIDE_INT, we have to fall back to a conservative
2939 solution which includes all sub-fields of all pointed-to
2940 variables of ptr. */
2941 if (offset == NULL_TREE
2942 || TREE_CODE (offset) != INTEGER_CST)
2943 rhsoffset = UNKNOWN_OFFSET;
2944 else
2946 /* Sign-extend the offset. */
2947 double_int soffset = tree_to_double_int (offset)
2948 .sext (TYPE_PRECISION (TREE_TYPE (offset)));
2949 if (!soffset.fits_shwi ())
2950 rhsoffset = UNKNOWN_OFFSET;
2951 else
2953 /* Make sure the bit-offset also fits. */
2954 HOST_WIDE_INT rhsunitoffset = soffset.low;
2955 rhsoffset = rhsunitoffset * BITS_PER_UNIT;
2956 if (rhsunitoffset != rhsoffset / BITS_PER_UNIT)
2957 rhsoffset = UNKNOWN_OFFSET;
2961 get_constraint_for_rhs (ptr, results);
2962 if (rhsoffset == 0)
2963 return;
2965 /* As we are eventually appending to the solution do not use
2966 vec::iterate here. */
2967 n = results->length ();
2968 for (j = 0; j < n; j++)
2970 varinfo_t curr;
2971 c = (*results)[j];
2972 curr = get_varinfo (c.var);
2974 if (c.type == ADDRESSOF
2975 /* If this varinfo represents a full variable just use it. */
2976 && curr->is_full_var)
2977 c.offset = 0;
2978 else if (c.type == ADDRESSOF
2979 /* If we do not know the offset add all subfields. */
2980 && rhsoffset == UNKNOWN_OFFSET)
2982 varinfo_t temp = lookup_vi_for_tree (curr->decl);
2985 struct constraint_expr c2;
2986 c2.var = temp->id;
2987 c2.type = ADDRESSOF;
2988 c2.offset = 0;
2989 if (c2.var != c.var)
2990 results->safe_push (c2);
2991 temp = temp->next;
2993 while (temp);
2995 else if (c.type == ADDRESSOF)
2997 varinfo_t temp;
2998 unsigned HOST_WIDE_INT offset = curr->offset + rhsoffset;
3000 /* Search the sub-field which overlaps with the
3001 pointed-to offset. If the result is outside of the variable
3002 we have to provide a conservative result, as the variable is
3003 still reachable from the resulting pointer (even though it
3004 technically cannot point to anything). The last and first
3005 sub-fields are such conservative results.
3006 ??? If we always had a sub-field for &object + 1 then
3007 we could represent this in a more precise way. */
3008 if (rhsoffset < 0
3009 && curr->offset < offset)
3010 offset = 0;
3011 temp = first_or_preceding_vi_for_offset (curr, offset);
3013 /* If the found variable is not exactly at the pointed to
3014 result, we have to include the next variable in the
3015 solution as well. Otherwise two increments by offset / 2
3016 do not result in the same or a conservative superset
3017 solution. */
3018 if (temp->offset != offset
3019 && temp->next != NULL)
3021 struct constraint_expr c2;
3022 c2.var = temp->next->id;
3023 c2.type = ADDRESSOF;
3024 c2.offset = 0;
3025 results->safe_push (c2);
3027 c.var = temp->id;
3028 c.offset = 0;
3030 else
3031 c.offset = rhsoffset;
3033 (*results)[j] = c;
3038 /* Given a COMPONENT_REF T, return the constraint_expr vector for it.
3039 If address_p is true the result will be taken its address of.
3040 If lhs_p is true then the constraint expression is assumed to be used
3041 as the lhs. */
3043 static void
3044 get_constraint_for_component_ref (tree t, vec<ce_s> *results,
3045 bool address_p, bool lhs_p)
3047 tree orig_t = t;
3048 HOST_WIDE_INT bitsize = -1;
3049 HOST_WIDE_INT bitmaxsize = -1;
3050 HOST_WIDE_INT bitpos;
3051 tree forzero;
3053 /* Some people like to do cute things like take the address of
3054 &0->a.b */
3055 forzero = t;
3056 while (handled_component_p (forzero)
3057 || INDIRECT_REF_P (forzero)
3058 || TREE_CODE (forzero) == MEM_REF)
3059 forzero = TREE_OPERAND (forzero, 0);
3061 if (CONSTANT_CLASS_P (forzero) && integer_zerop (forzero))
3063 struct constraint_expr temp;
3065 temp.offset = 0;
3066 temp.var = integer_id;
3067 temp.type = SCALAR;
3068 results->safe_push (temp);
3069 return;
3072 /* Handle type-punning through unions. If we are extracting a pointer
3073 from a union via a possibly type-punning access that pointer
3074 points to anything, similar to a conversion of an integer to
3075 a pointer. */
3076 if (!lhs_p)
3078 tree u;
3079 for (u = t;
3080 TREE_CODE (u) == COMPONENT_REF || TREE_CODE (u) == ARRAY_REF;
3081 u = TREE_OPERAND (u, 0))
3082 if (TREE_CODE (u) == COMPONENT_REF
3083 && TREE_CODE (TREE_TYPE (TREE_OPERAND (u, 0))) == UNION_TYPE)
3085 struct constraint_expr temp;
3087 temp.offset = 0;
3088 temp.var = anything_id;
3089 temp.type = ADDRESSOF;
3090 results->safe_push (temp);
3091 return;
3095 t = get_ref_base_and_extent (t, &bitpos, &bitsize, &bitmaxsize);
3097 /* Pretend to take the address of the base, we'll take care of
3098 adding the required subset of sub-fields below. */
3099 get_constraint_for_1 (t, results, true, lhs_p);
3100 gcc_assert (results->length () == 1);
3101 struct constraint_expr &result = results->last ();
3103 if (result.type == SCALAR
3104 && get_varinfo (result.var)->is_full_var)
3105 /* For single-field vars do not bother about the offset. */
3106 result.offset = 0;
3107 else if (result.type == SCALAR)
3109 /* In languages like C, you can access one past the end of an
3110 array. You aren't allowed to dereference it, so we can
3111 ignore this constraint. When we handle pointer subtraction,
3112 we may have to do something cute here. */
3114 if ((unsigned HOST_WIDE_INT)bitpos < get_varinfo (result.var)->fullsize
3115 && bitmaxsize != 0)
3117 /* It's also not true that the constraint will actually start at the
3118 right offset, it may start in some padding. We only care about
3119 setting the constraint to the first actual field it touches, so
3120 walk to find it. */
3121 struct constraint_expr cexpr = result;
3122 varinfo_t curr;
3123 results->pop ();
3124 cexpr.offset = 0;
3125 for (curr = get_varinfo (cexpr.var); curr; curr = curr->next)
3127 if (ranges_overlap_p (curr->offset, curr->size,
3128 bitpos, bitmaxsize))
3130 cexpr.var = curr->id;
3131 results->safe_push (cexpr);
3132 if (address_p)
3133 break;
3136 /* If we are going to take the address of this field then
3137 to be able to compute reachability correctly add at least
3138 the last field of the variable. */
3139 if (address_p && results->length () == 0)
3141 curr = get_varinfo (cexpr.var);
3142 while (curr->next != NULL)
3143 curr = curr->next;
3144 cexpr.var = curr->id;
3145 results->safe_push (cexpr);
3147 else if (results->length () == 0)
3148 /* Assert that we found *some* field there. The user couldn't be
3149 accessing *only* padding. */
3150 /* Still the user could access one past the end of an array
3151 embedded in a struct resulting in accessing *only* padding. */
3152 /* Or accessing only padding via type-punning to a type
3153 that has a filed just in padding space. */
3155 cexpr.type = SCALAR;
3156 cexpr.var = anything_id;
3157 cexpr.offset = 0;
3158 results->safe_push (cexpr);
3161 else if (bitmaxsize == 0)
3163 if (dump_file && (dump_flags & TDF_DETAILS))
3164 fprintf (dump_file, "Access to zero-sized part of variable,"
3165 "ignoring\n");
3167 else
3168 if (dump_file && (dump_flags & TDF_DETAILS))
3169 fprintf (dump_file, "Access to past the end of variable, ignoring\n");
3171 else if (result.type == DEREF)
3173 /* If we do not know exactly where the access goes say so. Note
3174 that only for non-structure accesses we know that we access
3175 at most one subfiled of any variable. */
3176 if (bitpos == -1
3177 || bitsize != bitmaxsize
3178 || AGGREGATE_TYPE_P (TREE_TYPE (orig_t))
3179 || result.offset == UNKNOWN_OFFSET)
3180 result.offset = UNKNOWN_OFFSET;
3181 else
3182 result.offset += bitpos;
3184 else if (result.type == ADDRESSOF)
3186 /* We can end up here for component references on a
3187 VIEW_CONVERT_EXPR <>(&foobar). */
3188 result.type = SCALAR;
3189 result.var = anything_id;
3190 result.offset = 0;
3192 else
3193 gcc_unreachable ();
3197 /* Dereference the constraint expression CONS, and return the result.
3198 DEREF (ADDRESSOF) = SCALAR
3199 DEREF (SCALAR) = DEREF
3200 DEREF (DEREF) = (temp = DEREF1; result = DEREF(temp))
3201 This is needed so that we can handle dereferencing DEREF constraints. */
3203 static void
3204 do_deref (vec<ce_s> *constraints)
3206 struct constraint_expr *c;
3207 unsigned int i = 0;
3209 FOR_EACH_VEC_ELT (*constraints, i, c)
3211 if (c->type == SCALAR)
3212 c->type = DEREF;
3213 else if (c->type == ADDRESSOF)
3214 c->type = SCALAR;
3215 else if (c->type == DEREF)
3217 struct constraint_expr tmplhs;
3218 tmplhs = new_scalar_tmp_constraint_exp ("dereftmp");
3219 process_constraint (new_constraint (tmplhs, *c));
3220 c->var = tmplhs.var;
3222 else
3223 gcc_unreachable ();
3227 /* Given a tree T, return the constraint expression for taking the
3228 address of it. */
3230 static void
3231 get_constraint_for_address_of (tree t, vec<ce_s> *results)
3233 struct constraint_expr *c;
3234 unsigned int i;
3236 get_constraint_for_1 (t, results, true, true);
3238 FOR_EACH_VEC_ELT (*results, i, c)
3240 if (c->type == DEREF)
3241 c->type = SCALAR;
3242 else
3243 c->type = ADDRESSOF;
3247 /* Given a tree T, return the constraint expression for it. */
3249 static void
3250 get_constraint_for_1 (tree t, vec<ce_s> *results, bool address_p,
3251 bool lhs_p)
3253 struct constraint_expr temp;
3255 /* x = integer is all glommed to a single variable, which doesn't
3256 point to anything by itself. That is, of course, unless it is an
3257 integer constant being treated as a pointer, in which case, we
3258 will return that this is really the addressof anything. This
3259 happens below, since it will fall into the default case. The only
3260 case we know something about an integer treated like a pointer is
3261 when it is the NULL pointer, and then we just say it points to
3262 NULL.
3264 Do not do that if -fno-delete-null-pointer-checks though, because
3265 in that case *NULL does not fail, so it _should_ alias *anything.
3266 It is not worth adding a new option or renaming the existing one,
3267 since this case is relatively obscure. */
3268 if ((TREE_CODE (t) == INTEGER_CST
3269 && integer_zerop (t))
3270 /* The only valid CONSTRUCTORs in gimple with pointer typed
3271 elements are zero-initializer. But in IPA mode we also
3272 process global initializers, so verify at least. */
3273 || (TREE_CODE (t) == CONSTRUCTOR
3274 && CONSTRUCTOR_NELTS (t) == 0))
3276 if (flag_delete_null_pointer_checks)
3277 temp.var = nothing_id;
3278 else
3279 temp.var = nonlocal_id;
3280 temp.type = ADDRESSOF;
3281 temp.offset = 0;
3282 results->safe_push (temp);
3283 return;
3286 /* String constants are read-only. */
3287 if (TREE_CODE (t) == STRING_CST)
3289 temp.var = readonly_id;
3290 temp.type = SCALAR;
3291 temp.offset = 0;
3292 results->safe_push (temp);
3293 return;
3296 switch (TREE_CODE_CLASS (TREE_CODE (t)))
3298 case tcc_expression:
3300 switch (TREE_CODE (t))
3302 case ADDR_EXPR:
3303 get_constraint_for_address_of (TREE_OPERAND (t, 0), results);
3304 return;
3305 default:;
3307 break;
3309 case tcc_reference:
3311 switch (TREE_CODE (t))
3313 case MEM_REF:
3315 struct constraint_expr cs;
3316 varinfo_t vi, curr;
3317 get_constraint_for_ptr_offset (TREE_OPERAND (t, 0),
3318 TREE_OPERAND (t, 1), results);
3319 do_deref (results);
3321 /* If we are not taking the address then make sure to process
3322 all subvariables we might access. */
3323 if (address_p)
3324 return;
3326 cs = results->last ();
3327 if (cs.type == DEREF
3328 && type_can_have_subvars (TREE_TYPE (t)))
3330 /* For dereferences this means we have to defer it
3331 to solving time. */
3332 results->last ().offset = UNKNOWN_OFFSET;
3333 return;
3335 if (cs.type != SCALAR)
3336 return;
3338 vi = get_varinfo (cs.var);
3339 curr = vi->next;
3340 if (!vi->is_full_var
3341 && curr)
3343 unsigned HOST_WIDE_INT size;
3344 if (host_integerp (TYPE_SIZE (TREE_TYPE (t)), 1))
3345 size = TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (t)));
3346 else
3347 size = -1;
3348 for (; curr; curr = curr->next)
3350 if (curr->offset - vi->offset < size)
3352 cs.var = curr->id;
3353 results->safe_push (cs);
3355 else
3356 break;
3359 return;
3361 case ARRAY_REF:
3362 case ARRAY_RANGE_REF:
3363 case COMPONENT_REF:
3364 get_constraint_for_component_ref (t, results, address_p, lhs_p);
3365 return;
3366 case VIEW_CONVERT_EXPR:
3367 get_constraint_for_1 (TREE_OPERAND (t, 0), results, address_p,
3368 lhs_p);
3369 return;
3370 /* We are missing handling for TARGET_MEM_REF here. */
3371 default:;
3373 break;
3375 case tcc_exceptional:
3377 switch (TREE_CODE (t))
3379 case SSA_NAME:
3381 get_constraint_for_ssa_var (t, results, address_p);
3382 return;
3384 case CONSTRUCTOR:
3386 unsigned int i;
3387 tree val;
3388 vec<ce_s> tmp = vNULL;
3389 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (t), i, val)
3391 struct constraint_expr *rhsp;
3392 unsigned j;
3393 get_constraint_for_1 (val, &tmp, address_p, lhs_p);
3394 FOR_EACH_VEC_ELT (tmp, j, rhsp)
3395 results->safe_push (*rhsp);
3396 tmp.truncate (0);
3398 tmp.release ();
3399 /* We do not know whether the constructor was complete,
3400 so technically we have to add &NOTHING or &ANYTHING
3401 like we do for an empty constructor as well. */
3402 return;
3404 default:;
3406 break;
3408 case tcc_declaration:
3410 get_constraint_for_ssa_var (t, results, address_p);
3411 return;
3413 case tcc_constant:
3415 /* We cannot refer to automatic variables through constants. */
3416 temp.type = ADDRESSOF;
3417 temp.var = nonlocal_id;
3418 temp.offset = 0;
3419 results->safe_push (temp);
3420 return;
3422 default:;
3425 /* The default fallback is a constraint from anything. */
3426 temp.type = ADDRESSOF;
3427 temp.var = anything_id;
3428 temp.offset = 0;
3429 results->safe_push (temp);
3432 /* Given a gimple tree T, return the constraint expression vector for it. */
3434 static void
3435 get_constraint_for (tree t, vec<ce_s> *results)
3437 gcc_assert (results->length () == 0);
3439 get_constraint_for_1 (t, results, false, true);
3442 /* Given a gimple tree T, return the constraint expression vector for it
3443 to be used as the rhs of a constraint. */
3445 static void
3446 get_constraint_for_rhs (tree t, vec<ce_s> *results)
3448 gcc_assert (results->length () == 0);
3450 get_constraint_for_1 (t, results, false, false);
3454 /* Efficiently generates constraints from all entries in *RHSC to all
3455 entries in *LHSC. */
3457 static void
3458 process_all_all_constraints (vec<ce_s> lhsc,
3459 vec<ce_s> rhsc)
3461 struct constraint_expr *lhsp, *rhsp;
3462 unsigned i, j;
3464 if (lhsc.length () <= 1 || rhsc.length () <= 1)
3466 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
3467 FOR_EACH_VEC_ELT (rhsc, j, rhsp)
3468 process_constraint (new_constraint (*lhsp, *rhsp));
3470 else
3472 struct constraint_expr tmp;
3473 tmp = new_scalar_tmp_constraint_exp ("allalltmp");
3474 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
3475 process_constraint (new_constraint (tmp, *rhsp));
3476 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
3477 process_constraint (new_constraint (*lhsp, tmp));
3481 /* Handle aggregate copies by expanding into copies of the respective
3482 fields of the structures. */
3484 static void
3485 do_structure_copy (tree lhsop, tree rhsop)
3487 struct constraint_expr *lhsp, *rhsp;
3488 vec<ce_s> lhsc = vNULL;
3489 vec<ce_s> rhsc = vNULL;
3490 unsigned j;
3492 get_constraint_for (lhsop, &lhsc);
3493 get_constraint_for_rhs (rhsop, &rhsc);
3494 lhsp = &lhsc[0];
3495 rhsp = &rhsc[0];
3496 if (lhsp->type == DEREF
3497 || (lhsp->type == ADDRESSOF && lhsp->var == anything_id)
3498 || rhsp->type == DEREF)
3500 if (lhsp->type == DEREF)
3502 gcc_assert (lhsc.length () == 1);
3503 lhsp->offset = UNKNOWN_OFFSET;
3505 if (rhsp->type == DEREF)
3507 gcc_assert (rhsc.length () == 1);
3508 rhsp->offset = UNKNOWN_OFFSET;
3510 process_all_all_constraints (lhsc, rhsc);
3512 else if (lhsp->type == SCALAR
3513 && (rhsp->type == SCALAR
3514 || rhsp->type == ADDRESSOF))
3516 HOST_WIDE_INT lhssize, lhsmaxsize, lhsoffset;
3517 HOST_WIDE_INT rhssize, rhsmaxsize, rhsoffset;
3518 unsigned k = 0;
3519 get_ref_base_and_extent (lhsop, &lhsoffset, &lhssize, &lhsmaxsize);
3520 get_ref_base_and_extent (rhsop, &rhsoffset, &rhssize, &rhsmaxsize);
3521 for (j = 0; lhsc.iterate (j, &lhsp);)
3523 varinfo_t lhsv, rhsv;
3524 rhsp = &rhsc[k];
3525 lhsv = get_varinfo (lhsp->var);
3526 rhsv = get_varinfo (rhsp->var);
3527 if (lhsv->may_have_pointers
3528 && (lhsv->is_full_var
3529 || rhsv->is_full_var
3530 || ranges_overlap_p (lhsv->offset + rhsoffset, lhsv->size,
3531 rhsv->offset + lhsoffset, rhsv->size)))
3532 process_constraint (new_constraint (*lhsp, *rhsp));
3533 if (!rhsv->is_full_var
3534 && (lhsv->is_full_var
3535 || (lhsv->offset + rhsoffset + lhsv->size
3536 > rhsv->offset + lhsoffset + rhsv->size)))
3538 ++k;
3539 if (k >= rhsc.length ())
3540 break;
3542 else
3543 ++j;
3546 else
3547 gcc_unreachable ();
3549 lhsc.release ();
3550 rhsc.release ();
3553 /* Create constraints ID = { rhsc }. */
3555 static void
3556 make_constraints_to (unsigned id, vec<ce_s> rhsc)
3558 struct constraint_expr *c;
3559 struct constraint_expr includes;
3560 unsigned int j;
3562 includes.var = id;
3563 includes.offset = 0;
3564 includes.type = SCALAR;
3566 FOR_EACH_VEC_ELT (rhsc, j, c)
3567 process_constraint (new_constraint (includes, *c));
3570 /* Create a constraint ID = OP. */
3572 static void
3573 make_constraint_to (unsigned id, tree op)
3575 vec<ce_s> rhsc = vNULL;
3576 get_constraint_for_rhs (op, &rhsc);
3577 make_constraints_to (id, rhsc);
3578 rhsc.release ();
3581 /* Create a constraint ID = &FROM. */
3583 static void
3584 make_constraint_from (varinfo_t vi, int from)
3586 struct constraint_expr lhs, rhs;
3588 lhs.var = vi->id;
3589 lhs.offset = 0;
3590 lhs.type = SCALAR;
3592 rhs.var = from;
3593 rhs.offset = 0;
3594 rhs.type = ADDRESSOF;
3595 process_constraint (new_constraint (lhs, rhs));
3598 /* Create a constraint ID = FROM. */
3600 static void
3601 make_copy_constraint (varinfo_t vi, int from)
3603 struct constraint_expr lhs, rhs;
3605 lhs.var = vi->id;
3606 lhs.offset = 0;
3607 lhs.type = SCALAR;
3609 rhs.var = from;
3610 rhs.offset = 0;
3611 rhs.type = SCALAR;
3612 process_constraint (new_constraint (lhs, rhs));
3615 /* Make constraints necessary to make OP escape. */
3617 static void
3618 make_escape_constraint (tree op)
3620 make_constraint_to (escaped_id, op);
3623 /* Add constraints to that the solution of VI is transitively closed. */
3625 static void
3626 make_transitive_closure_constraints (varinfo_t vi)
3628 struct constraint_expr lhs, rhs;
3630 /* VAR = *VAR; */
3631 lhs.type = SCALAR;
3632 lhs.var = vi->id;
3633 lhs.offset = 0;
3634 rhs.type = DEREF;
3635 rhs.var = vi->id;
3636 rhs.offset = 0;
3637 process_constraint (new_constraint (lhs, rhs));
3639 /* VAR = VAR + UNKNOWN; */
3640 lhs.type = SCALAR;
3641 lhs.var = vi->id;
3642 lhs.offset = 0;
3643 rhs.type = SCALAR;
3644 rhs.var = vi->id;
3645 rhs.offset = UNKNOWN_OFFSET;
3646 process_constraint (new_constraint (lhs, rhs));
3649 /* Temporary storage for fake var decls. */
3650 struct obstack fake_var_decl_obstack;
3652 /* Build a fake VAR_DECL acting as referrer to a DECL_UID. */
3654 static tree
3655 build_fake_var_decl (tree type)
3657 tree decl = (tree) XOBNEW (&fake_var_decl_obstack, struct tree_var_decl);
3658 memset (decl, 0, sizeof (struct tree_var_decl));
3659 TREE_SET_CODE (decl, VAR_DECL);
3660 TREE_TYPE (decl) = type;
3661 DECL_UID (decl) = allocate_decl_uid ();
3662 SET_DECL_PT_UID (decl, -1);
3663 layout_decl (decl, 0);
3664 return decl;
3667 /* Create a new artificial heap variable with NAME.
3668 Return the created variable. */
3670 static varinfo_t
3671 make_heapvar (const char *name)
3673 varinfo_t vi;
3674 tree heapvar;
3676 heapvar = build_fake_var_decl (ptr_type_node);
3677 DECL_EXTERNAL (heapvar) = 1;
3679 vi = new_var_info (heapvar, name);
3680 vi->is_artificial_var = true;
3681 vi->is_heap_var = true;
3682 vi->is_unknown_size_var = true;
3683 vi->offset = 0;
3684 vi->fullsize = ~0;
3685 vi->size = ~0;
3686 vi->is_full_var = true;
3687 insert_vi_for_tree (heapvar, vi);
3689 return vi;
3692 /* Create a new artificial heap variable with NAME and make a
3693 constraint from it to LHS. Set flags according to a tag used
3694 for tracking restrict pointers. */
3696 static varinfo_t
3697 make_constraint_from_restrict (varinfo_t lhs, const char *name)
3699 varinfo_t vi = make_heapvar (name);
3700 vi->is_global_var = 1;
3701 vi->may_have_pointers = 1;
3702 make_constraint_from (lhs, vi->id);
3703 return vi;
3706 /* Create a new artificial heap variable with NAME and make a
3707 constraint from it to LHS. Set flags according to a tag used
3708 for tracking restrict pointers and make the artificial heap
3709 point to global memory. */
3711 static varinfo_t
3712 make_constraint_from_global_restrict (varinfo_t lhs, const char *name)
3714 varinfo_t vi = make_constraint_from_restrict (lhs, name);
3715 make_copy_constraint (vi, nonlocal_id);
3716 return vi;
3719 /* In IPA mode there are varinfos for different aspects of reach
3720 function designator. One for the points-to set of the return
3721 value, one for the variables that are clobbered by the function,
3722 one for its uses and one for each parameter (including a single
3723 glob for remaining variadic arguments). */
3725 enum { fi_clobbers = 1, fi_uses = 2,
3726 fi_static_chain = 3, fi_result = 4, fi_parm_base = 5 };
3728 /* Get a constraint for the requested part of a function designator FI
3729 when operating in IPA mode. */
3731 static struct constraint_expr
3732 get_function_part_constraint (varinfo_t fi, unsigned part)
3734 struct constraint_expr c;
3736 gcc_assert (in_ipa_mode);
3738 if (fi->id == anything_id)
3740 /* ??? We probably should have a ANYFN special variable. */
3741 c.var = anything_id;
3742 c.offset = 0;
3743 c.type = SCALAR;
3745 else if (TREE_CODE (fi->decl) == FUNCTION_DECL)
3747 varinfo_t ai = first_vi_for_offset (fi, part);
3748 if (ai)
3749 c.var = ai->id;
3750 else
3751 c.var = anything_id;
3752 c.offset = 0;
3753 c.type = SCALAR;
3755 else
3757 c.var = fi->id;
3758 c.offset = part;
3759 c.type = DEREF;
3762 return c;
3765 /* For non-IPA mode, generate constraints necessary for a call on the
3766 RHS. */
3768 static void
3769 handle_rhs_call (gimple stmt, vec<ce_s> *results)
3771 struct constraint_expr rhsc;
3772 unsigned i;
3773 bool returns_uses = false;
3775 for (i = 0; i < gimple_call_num_args (stmt); ++i)
3777 tree arg = gimple_call_arg (stmt, i);
3778 int flags = gimple_call_arg_flags (stmt, i);
3780 /* If the argument is not used we can ignore it. */
3781 if (flags & EAF_UNUSED)
3782 continue;
3784 /* As we compute ESCAPED context-insensitive we do not gain
3785 any precision with just EAF_NOCLOBBER but not EAF_NOESCAPE
3786 set. The argument would still get clobbered through the
3787 escape solution. */
3788 if ((flags & EAF_NOCLOBBER)
3789 && (flags & EAF_NOESCAPE))
3791 varinfo_t uses = get_call_use_vi (stmt);
3792 if (!(flags & EAF_DIRECT))
3794 varinfo_t tem = new_var_info (NULL_TREE, "callarg");
3795 make_constraint_to (tem->id, arg);
3796 make_transitive_closure_constraints (tem);
3797 make_copy_constraint (uses, tem->id);
3799 else
3800 make_constraint_to (uses->id, arg);
3801 returns_uses = true;
3803 else if (flags & EAF_NOESCAPE)
3805 struct constraint_expr lhs, rhs;
3806 varinfo_t uses = get_call_use_vi (stmt);
3807 varinfo_t clobbers = get_call_clobber_vi (stmt);
3808 varinfo_t tem = new_var_info (NULL_TREE, "callarg");
3809 make_constraint_to (tem->id, arg);
3810 if (!(flags & EAF_DIRECT))
3811 make_transitive_closure_constraints (tem);
3812 make_copy_constraint (uses, tem->id);
3813 make_copy_constraint (clobbers, tem->id);
3814 /* Add *tem = nonlocal, do not add *tem = callused as
3815 EAF_NOESCAPE parameters do not escape to other parameters
3816 and all other uses appear in NONLOCAL as well. */
3817 lhs.type = DEREF;
3818 lhs.var = tem->id;
3819 lhs.offset = 0;
3820 rhs.type = SCALAR;
3821 rhs.var = nonlocal_id;
3822 rhs.offset = 0;
3823 process_constraint (new_constraint (lhs, rhs));
3824 returns_uses = true;
3826 else
3827 make_escape_constraint (arg);
3830 /* If we added to the calls uses solution make sure we account for
3831 pointers to it to be returned. */
3832 if (returns_uses)
3834 rhsc.var = get_call_use_vi (stmt)->id;
3835 rhsc.offset = 0;
3836 rhsc.type = SCALAR;
3837 results->safe_push (rhsc);
3840 /* The static chain escapes as well. */
3841 if (gimple_call_chain (stmt))
3842 make_escape_constraint (gimple_call_chain (stmt));
3844 /* And if we applied NRV the address of the return slot escapes as well. */
3845 if (gimple_call_return_slot_opt_p (stmt)
3846 && gimple_call_lhs (stmt) != NULL_TREE
3847 && TREE_ADDRESSABLE (TREE_TYPE (gimple_call_lhs (stmt))))
3849 vec<ce_s> tmpc = vNULL;
3850 struct constraint_expr lhsc, *c;
3851 get_constraint_for_address_of (gimple_call_lhs (stmt), &tmpc);
3852 lhsc.var = escaped_id;
3853 lhsc.offset = 0;
3854 lhsc.type = SCALAR;
3855 FOR_EACH_VEC_ELT (tmpc, i, c)
3856 process_constraint (new_constraint (lhsc, *c));
3857 tmpc.release ();
3860 /* Regular functions return nonlocal memory. */
3861 rhsc.var = nonlocal_id;
3862 rhsc.offset = 0;
3863 rhsc.type = SCALAR;
3864 results->safe_push (rhsc);
3867 /* For non-IPA mode, generate constraints necessary for a call
3868 that returns a pointer and assigns it to LHS. This simply makes
3869 the LHS point to global and escaped variables. */
3871 static void
3872 handle_lhs_call (gimple stmt, tree lhs, int flags, vec<ce_s> rhsc,
3873 tree fndecl)
3875 vec<ce_s> lhsc = vNULL;
3877 get_constraint_for (lhs, &lhsc);
3878 /* If the store is to a global decl make sure to
3879 add proper escape constraints. */
3880 lhs = get_base_address (lhs);
3881 if (lhs
3882 && DECL_P (lhs)
3883 && is_global_var (lhs))
3885 struct constraint_expr tmpc;
3886 tmpc.var = escaped_id;
3887 tmpc.offset = 0;
3888 tmpc.type = SCALAR;
3889 lhsc.safe_push (tmpc);
3892 /* If the call returns an argument unmodified override the rhs
3893 constraints. */
3894 flags = gimple_call_return_flags (stmt);
3895 if (flags & ERF_RETURNS_ARG
3896 && (flags & ERF_RETURN_ARG_MASK) < gimple_call_num_args (stmt))
3898 tree arg;
3899 rhsc.create (0);
3900 arg = gimple_call_arg (stmt, flags & ERF_RETURN_ARG_MASK);
3901 get_constraint_for (arg, &rhsc);
3902 process_all_all_constraints (lhsc, rhsc);
3903 rhsc.release ();
3905 else if (flags & ERF_NOALIAS)
3907 varinfo_t vi;
3908 struct constraint_expr tmpc;
3909 rhsc.create (0);
3910 vi = make_heapvar ("HEAP");
3911 /* We delay marking allocated storage global until we know if
3912 it escapes. */
3913 DECL_EXTERNAL (vi->decl) = 0;
3914 vi->is_global_var = 0;
3915 /* If this is not a real malloc call assume the memory was
3916 initialized and thus may point to global memory. All
3917 builtin functions with the malloc attribute behave in a sane way. */
3918 if (!fndecl
3919 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
3920 make_constraint_from (vi, nonlocal_id);
3921 tmpc.var = vi->id;
3922 tmpc.offset = 0;
3923 tmpc.type = ADDRESSOF;
3924 rhsc.safe_push (tmpc);
3925 process_all_all_constraints (lhsc, rhsc);
3926 rhsc.release ();
3928 else
3929 process_all_all_constraints (lhsc, rhsc);
3931 lhsc.release ();
3934 /* For non-IPA mode, generate constraints necessary for a call of a
3935 const function that returns a pointer in the statement STMT. */
3937 static void
3938 handle_const_call (gimple stmt, vec<ce_s> *results)
3940 struct constraint_expr rhsc;
3941 unsigned int k;
3943 /* Treat nested const functions the same as pure functions as far
3944 as the static chain is concerned. */
3945 if (gimple_call_chain (stmt))
3947 varinfo_t uses = get_call_use_vi (stmt);
3948 make_transitive_closure_constraints (uses);
3949 make_constraint_to (uses->id, gimple_call_chain (stmt));
3950 rhsc.var = uses->id;
3951 rhsc.offset = 0;
3952 rhsc.type = SCALAR;
3953 results->safe_push (rhsc);
3956 /* May return arguments. */
3957 for (k = 0; k < gimple_call_num_args (stmt); ++k)
3959 tree arg = gimple_call_arg (stmt, k);
3960 vec<ce_s> argc = vNULL;
3961 unsigned i;
3962 struct constraint_expr *argp;
3963 get_constraint_for_rhs (arg, &argc);
3964 FOR_EACH_VEC_ELT (argc, i, argp)
3965 results->safe_push (*argp);
3966 argc.release ();
3969 /* May return addresses of globals. */
3970 rhsc.var = nonlocal_id;
3971 rhsc.offset = 0;
3972 rhsc.type = ADDRESSOF;
3973 results->safe_push (rhsc);
3976 /* For non-IPA mode, generate constraints necessary for a call to a
3977 pure function in statement STMT. */
3979 static void
3980 handle_pure_call (gimple stmt, vec<ce_s> *results)
3982 struct constraint_expr rhsc;
3983 unsigned i;
3984 varinfo_t uses = NULL;
3986 /* Memory reached from pointer arguments is call-used. */
3987 for (i = 0; i < gimple_call_num_args (stmt); ++i)
3989 tree arg = gimple_call_arg (stmt, i);
3990 if (!uses)
3992 uses = get_call_use_vi (stmt);
3993 make_transitive_closure_constraints (uses);
3995 make_constraint_to (uses->id, arg);
3998 /* The static chain is used as well. */
3999 if (gimple_call_chain (stmt))
4001 if (!uses)
4003 uses = get_call_use_vi (stmt);
4004 make_transitive_closure_constraints (uses);
4006 make_constraint_to (uses->id, gimple_call_chain (stmt));
4009 /* Pure functions may return call-used and nonlocal memory. */
4010 if (uses)
4012 rhsc.var = uses->id;
4013 rhsc.offset = 0;
4014 rhsc.type = SCALAR;
4015 results->safe_push (rhsc);
4017 rhsc.var = nonlocal_id;
4018 rhsc.offset = 0;
4019 rhsc.type = SCALAR;
4020 results->safe_push (rhsc);
4024 /* Return the varinfo for the callee of CALL. */
4026 static varinfo_t
4027 get_fi_for_callee (gimple call)
4029 tree decl, fn = gimple_call_fn (call);
4031 if (fn && TREE_CODE (fn) == OBJ_TYPE_REF)
4032 fn = OBJ_TYPE_REF_EXPR (fn);
4034 /* If we can directly resolve the function being called, do so.
4035 Otherwise, it must be some sort of indirect expression that
4036 we should still be able to handle. */
4037 decl = gimple_call_addr_fndecl (fn);
4038 if (decl)
4039 return get_vi_for_tree (decl);
4041 /* If the function is anything other than a SSA name pointer we have no
4042 clue and should be getting ANYFN (well, ANYTHING for now). */
4043 if (!fn || TREE_CODE (fn) != SSA_NAME)
4044 return get_varinfo (anything_id);
4046 if (SSA_NAME_IS_DEFAULT_DEF (fn)
4047 && (TREE_CODE (SSA_NAME_VAR (fn)) == PARM_DECL
4048 || TREE_CODE (SSA_NAME_VAR (fn)) == RESULT_DECL))
4049 fn = SSA_NAME_VAR (fn);
4051 return get_vi_for_tree (fn);
4054 /* Create constraints for the builtin call T. Return true if the call
4055 was handled, otherwise false. */
4057 static bool
4058 find_func_aliases_for_builtin_call (gimple t)
4060 tree fndecl = gimple_call_fndecl (t);
4061 vec<ce_s> lhsc = vNULL;
4062 vec<ce_s> rhsc = vNULL;
4063 varinfo_t fi;
4065 if (gimple_call_builtin_p (t, BUILT_IN_NORMAL))
4066 /* ??? All builtins that are handled here need to be handled
4067 in the alias-oracle query functions explicitly! */
4068 switch (DECL_FUNCTION_CODE (fndecl))
4070 /* All the following functions return a pointer to the same object
4071 as their first argument points to. The functions do not add
4072 to the ESCAPED solution. The functions make the first argument
4073 pointed to memory point to what the second argument pointed to
4074 memory points to. */
4075 case BUILT_IN_STRCPY:
4076 case BUILT_IN_STRNCPY:
4077 case BUILT_IN_BCOPY:
4078 case BUILT_IN_MEMCPY:
4079 case BUILT_IN_MEMMOVE:
4080 case BUILT_IN_MEMPCPY:
4081 case BUILT_IN_STPCPY:
4082 case BUILT_IN_STPNCPY:
4083 case BUILT_IN_STRCAT:
4084 case BUILT_IN_STRNCAT:
4085 case BUILT_IN_STRCPY_CHK:
4086 case BUILT_IN_STRNCPY_CHK:
4087 case BUILT_IN_MEMCPY_CHK:
4088 case BUILT_IN_MEMMOVE_CHK:
4089 case BUILT_IN_MEMPCPY_CHK:
4090 case BUILT_IN_STPCPY_CHK:
4091 case BUILT_IN_STPNCPY_CHK:
4092 case BUILT_IN_STRCAT_CHK:
4093 case BUILT_IN_STRNCAT_CHK:
4094 case BUILT_IN_TM_MEMCPY:
4095 case BUILT_IN_TM_MEMMOVE:
4097 tree res = gimple_call_lhs (t);
4098 tree dest = gimple_call_arg (t, (DECL_FUNCTION_CODE (fndecl)
4099 == BUILT_IN_BCOPY ? 1 : 0));
4100 tree src = gimple_call_arg (t, (DECL_FUNCTION_CODE (fndecl)
4101 == BUILT_IN_BCOPY ? 0 : 1));
4102 if (res != NULL_TREE)
4104 get_constraint_for (res, &lhsc);
4105 if (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_MEMPCPY
4106 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPCPY
4107 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPNCPY
4108 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_MEMPCPY_CHK
4109 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPCPY_CHK
4110 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STPNCPY_CHK)
4111 get_constraint_for_ptr_offset (dest, NULL_TREE, &rhsc);
4112 else
4113 get_constraint_for (dest, &rhsc);
4114 process_all_all_constraints (lhsc, rhsc);
4115 lhsc.release ();
4116 rhsc.release ();
4118 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
4119 get_constraint_for_ptr_offset (src, NULL_TREE, &rhsc);
4120 do_deref (&lhsc);
4121 do_deref (&rhsc);
4122 process_all_all_constraints (lhsc, rhsc);
4123 lhsc.release ();
4124 rhsc.release ();
4125 return true;
4127 case BUILT_IN_MEMSET:
4128 case BUILT_IN_MEMSET_CHK:
4129 case BUILT_IN_TM_MEMSET:
4131 tree res = gimple_call_lhs (t);
4132 tree dest = gimple_call_arg (t, 0);
4133 unsigned i;
4134 ce_s *lhsp;
4135 struct constraint_expr ac;
4136 if (res != NULL_TREE)
4138 get_constraint_for (res, &lhsc);
4139 get_constraint_for (dest, &rhsc);
4140 process_all_all_constraints (lhsc, rhsc);
4141 lhsc.release ();
4142 rhsc.release ();
4144 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
4145 do_deref (&lhsc);
4146 if (flag_delete_null_pointer_checks
4147 && integer_zerop (gimple_call_arg (t, 1)))
4149 ac.type = ADDRESSOF;
4150 ac.var = nothing_id;
4152 else
4154 ac.type = SCALAR;
4155 ac.var = integer_id;
4157 ac.offset = 0;
4158 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
4159 process_constraint (new_constraint (*lhsp, ac));
4160 lhsc.release ();
4161 return true;
4163 case BUILT_IN_ASSUME_ALIGNED:
4165 tree res = gimple_call_lhs (t);
4166 tree dest = gimple_call_arg (t, 0);
4167 if (res != NULL_TREE)
4169 get_constraint_for (res, &lhsc);
4170 get_constraint_for (dest, &rhsc);
4171 process_all_all_constraints (lhsc, rhsc);
4172 lhsc.release ();
4173 rhsc.release ();
4175 return true;
4177 /* All the following functions do not return pointers, do not
4178 modify the points-to sets of memory reachable from their
4179 arguments and do not add to the ESCAPED solution. */
4180 case BUILT_IN_SINCOS:
4181 case BUILT_IN_SINCOSF:
4182 case BUILT_IN_SINCOSL:
4183 case BUILT_IN_FREXP:
4184 case BUILT_IN_FREXPF:
4185 case BUILT_IN_FREXPL:
4186 case BUILT_IN_GAMMA_R:
4187 case BUILT_IN_GAMMAF_R:
4188 case BUILT_IN_GAMMAL_R:
4189 case BUILT_IN_LGAMMA_R:
4190 case BUILT_IN_LGAMMAF_R:
4191 case BUILT_IN_LGAMMAL_R:
4192 case BUILT_IN_MODF:
4193 case BUILT_IN_MODFF:
4194 case BUILT_IN_MODFL:
4195 case BUILT_IN_REMQUO:
4196 case BUILT_IN_REMQUOF:
4197 case BUILT_IN_REMQUOL:
4198 case BUILT_IN_FREE:
4199 return true;
4200 case BUILT_IN_STRDUP:
4201 case BUILT_IN_STRNDUP:
4202 if (gimple_call_lhs (t))
4204 handle_lhs_call (t, gimple_call_lhs (t), gimple_call_flags (t),
4205 vNULL, fndecl);
4206 get_constraint_for_ptr_offset (gimple_call_lhs (t),
4207 NULL_TREE, &lhsc);
4208 get_constraint_for_ptr_offset (gimple_call_arg (t, 0),
4209 NULL_TREE, &rhsc);
4210 do_deref (&lhsc);
4211 do_deref (&rhsc);
4212 process_all_all_constraints (lhsc, rhsc);
4213 lhsc.release ();
4214 rhsc.release ();
4215 return true;
4217 break;
4218 /* Trampolines are special - they set up passing the static
4219 frame. */
4220 case BUILT_IN_INIT_TRAMPOLINE:
4222 tree tramp = gimple_call_arg (t, 0);
4223 tree nfunc = gimple_call_arg (t, 1);
4224 tree frame = gimple_call_arg (t, 2);
4225 unsigned i;
4226 struct constraint_expr lhs, *rhsp;
4227 if (in_ipa_mode)
4229 varinfo_t nfi = NULL;
4230 gcc_assert (TREE_CODE (nfunc) == ADDR_EXPR);
4231 nfi = lookup_vi_for_tree (TREE_OPERAND (nfunc, 0));
4232 if (nfi)
4234 lhs = get_function_part_constraint (nfi, fi_static_chain);
4235 get_constraint_for (frame, &rhsc);
4236 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
4237 process_constraint (new_constraint (lhs, *rhsp));
4238 rhsc.release ();
4240 /* Make the frame point to the function for
4241 the trampoline adjustment call. */
4242 get_constraint_for (tramp, &lhsc);
4243 do_deref (&lhsc);
4244 get_constraint_for (nfunc, &rhsc);
4245 process_all_all_constraints (lhsc, rhsc);
4246 rhsc.release ();
4247 lhsc.release ();
4249 return true;
4252 /* Else fallthru to generic handling which will let
4253 the frame escape. */
4254 break;
4256 case BUILT_IN_ADJUST_TRAMPOLINE:
4258 tree tramp = gimple_call_arg (t, 0);
4259 tree res = gimple_call_lhs (t);
4260 if (in_ipa_mode && res)
4262 get_constraint_for (res, &lhsc);
4263 get_constraint_for (tramp, &rhsc);
4264 do_deref (&rhsc);
4265 process_all_all_constraints (lhsc, rhsc);
4266 rhsc.release ();
4267 lhsc.release ();
4269 return true;
4271 CASE_BUILT_IN_TM_STORE (1):
4272 CASE_BUILT_IN_TM_STORE (2):
4273 CASE_BUILT_IN_TM_STORE (4):
4274 CASE_BUILT_IN_TM_STORE (8):
4275 CASE_BUILT_IN_TM_STORE (FLOAT):
4276 CASE_BUILT_IN_TM_STORE (DOUBLE):
4277 CASE_BUILT_IN_TM_STORE (LDOUBLE):
4278 CASE_BUILT_IN_TM_STORE (M64):
4279 CASE_BUILT_IN_TM_STORE (M128):
4280 CASE_BUILT_IN_TM_STORE (M256):
4282 tree addr = gimple_call_arg (t, 0);
4283 tree src = gimple_call_arg (t, 1);
4285 get_constraint_for (addr, &lhsc);
4286 do_deref (&lhsc);
4287 get_constraint_for (src, &rhsc);
4288 process_all_all_constraints (lhsc, rhsc);
4289 lhsc.release ();
4290 rhsc.release ();
4291 return true;
4293 CASE_BUILT_IN_TM_LOAD (1):
4294 CASE_BUILT_IN_TM_LOAD (2):
4295 CASE_BUILT_IN_TM_LOAD (4):
4296 CASE_BUILT_IN_TM_LOAD (8):
4297 CASE_BUILT_IN_TM_LOAD (FLOAT):
4298 CASE_BUILT_IN_TM_LOAD (DOUBLE):
4299 CASE_BUILT_IN_TM_LOAD (LDOUBLE):
4300 CASE_BUILT_IN_TM_LOAD (M64):
4301 CASE_BUILT_IN_TM_LOAD (M128):
4302 CASE_BUILT_IN_TM_LOAD (M256):
4304 tree dest = gimple_call_lhs (t);
4305 tree addr = gimple_call_arg (t, 0);
4307 get_constraint_for (dest, &lhsc);
4308 get_constraint_for (addr, &rhsc);
4309 do_deref (&rhsc);
4310 process_all_all_constraints (lhsc, rhsc);
4311 lhsc.release ();
4312 rhsc.release ();
4313 return true;
4315 /* Variadic argument handling needs to be handled in IPA
4316 mode as well. */
4317 case BUILT_IN_VA_START:
4319 tree valist = gimple_call_arg (t, 0);
4320 struct constraint_expr rhs, *lhsp;
4321 unsigned i;
4322 get_constraint_for (valist, &lhsc);
4323 do_deref (&lhsc);
4324 /* The va_list gets access to pointers in variadic
4325 arguments. Which we know in the case of IPA analysis
4326 and otherwise are just all nonlocal variables. */
4327 if (in_ipa_mode)
4329 fi = lookup_vi_for_tree (cfun->decl);
4330 rhs = get_function_part_constraint (fi, ~0);
4331 rhs.type = ADDRESSOF;
4333 else
4335 rhs.var = nonlocal_id;
4336 rhs.type = ADDRESSOF;
4337 rhs.offset = 0;
4339 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
4340 process_constraint (new_constraint (*lhsp, rhs));
4341 lhsc.release ();
4342 /* va_list is clobbered. */
4343 make_constraint_to (get_call_clobber_vi (t)->id, valist);
4344 return true;
4346 /* va_end doesn't have any effect that matters. */
4347 case BUILT_IN_VA_END:
4348 return true;
4349 /* Alternate return. Simply give up for now. */
4350 case BUILT_IN_RETURN:
4352 fi = NULL;
4353 if (!in_ipa_mode
4354 || !(fi = get_vi_for_tree (cfun->decl)))
4355 make_constraint_from (get_varinfo (escaped_id), anything_id);
4356 else if (in_ipa_mode
4357 && fi != NULL)
4359 struct constraint_expr lhs, rhs;
4360 lhs = get_function_part_constraint (fi, fi_result);
4361 rhs.var = anything_id;
4362 rhs.offset = 0;
4363 rhs.type = SCALAR;
4364 process_constraint (new_constraint (lhs, rhs));
4366 return true;
4368 /* printf-style functions may have hooks to set pointers to
4369 point to somewhere into the generated string. Leave them
4370 for a later excercise... */
4371 default:
4372 /* Fallthru to general call handling. */;
4375 return false;
4378 /* Create constraints for the call T. */
4380 static void
4381 find_func_aliases_for_call (gimple t)
4383 tree fndecl = gimple_call_fndecl (t);
4384 vec<ce_s> lhsc = vNULL;
4385 vec<ce_s> rhsc = vNULL;
4386 varinfo_t fi;
4388 if (fndecl != NULL_TREE
4389 && DECL_BUILT_IN (fndecl)
4390 && find_func_aliases_for_builtin_call (t))
4391 return;
4393 fi = get_fi_for_callee (t);
4394 if (!in_ipa_mode
4395 || (fndecl && !fi->is_fn_info))
4397 vec<ce_s> rhsc = vNULL;
4398 int flags = gimple_call_flags (t);
4400 /* Const functions can return their arguments and addresses
4401 of global memory but not of escaped memory. */
4402 if (flags & (ECF_CONST|ECF_NOVOPS))
4404 if (gimple_call_lhs (t))
4405 handle_const_call (t, &rhsc);
4407 /* Pure functions can return addresses in and of memory
4408 reachable from their arguments, but they are not an escape
4409 point for reachable memory of their arguments. */
4410 else if (flags & (ECF_PURE|ECF_LOOPING_CONST_OR_PURE))
4411 handle_pure_call (t, &rhsc);
4412 else
4413 handle_rhs_call (t, &rhsc);
4414 if (gimple_call_lhs (t))
4415 handle_lhs_call (t, gimple_call_lhs (t), flags, rhsc, fndecl);
4416 rhsc.release ();
4418 else
4420 tree lhsop;
4421 unsigned j;
4423 /* Assign all the passed arguments to the appropriate incoming
4424 parameters of the function. */
4425 for (j = 0; j < gimple_call_num_args (t); j++)
4427 struct constraint_expr lhs ;
4428 struct constraint_expr *rhsp;
4429 tree arg = gimple_call_arg (t, j);
4431 get_constraint_for_rhs (arg, &rhsc);
4432 lhs = get_function_part_constraint (fi, fi_parm_base + j);
4433 while (rhsc.length () != 0)
4435 rhsp = &rhsc.last ();
4436 process_constraint (new_constraint (lhs, *rhsp));
4437 rhsc.pop ();
4441 /* If we are returning a value, assign it to the result. */
4442 lhsop = gimple_call_lhs (t);
4443 if (lhsop)
4445 struct constraint_expr rhs;
4446 struct constraint_expr *lhsp;
4448 get_constraint_for (lhsop, &lhsc);
4449 rhs = get_function_part_constraint (fi, fi_result);
4450 if (fndecl
4451 && DECL_RESULT (fndecl)
4452 && DECL_BY_REFERENCE (DECL_RESULT (fndecl)))
4454 vec<ce_s> tem = vNULL;
4455 tem.safe_push (rhs);
4456 do_deref (&tem);
4457 rhs = tem[0];
4458 tem.release ();
4460 FOR_EACH_VEC_ELT (lhsc, j, lhsp)
4461 process_constraint (new_constraint (*lhsp, rhs));
4464 /* If we pass the result decl by reference, honor that. */
4465 if (lhsop
4466 && fndecl
4467 && DECL_RESULT (fndecl)
4468 && DECL_BY_REFERENCE (DECL_RESULT (fndecl)))
4470 struct constraint_expr lhs;
4471 struct constraint_expr *rhsp;
4473 get_constraint_for_address_of (lhsop, &rhsc);
4474 lhs = get_function_part_constraint (fi, fi_result);
4475 FOR_EACH_VEC_ELT (rhsc, j, rhsp)
4476 process_constraint (new_constraint (lhs, *rhsp));
4477 rhsc.release ();
4480 /* If we use a static chain, pass it along. */
4481 if (gimple_call_chain (t))
4483 struct constraint_expr lhs;
4484 struct constraint_expr *rhsp;
4486 get_constraint_for (gimple_call_chain (t), &rhsc);
4487 lhs = get_function_part_constraint (fi, fi_static_chain);
4488 FOR_EACH_VEC_ELT (rhsc, j, rhsp)
4489 process_constraint (new_constraint (lhs, *rhsp));
4494 /* Walk statement T setting up aliasing constraints according to the
4495 references found in T. This function is the main part of the
4496 constraint builder. AI points to auxiliary alias information used
4497 when building alias sets and computing alias grouping heuristics. */
4499 static void
4500 find_func_aliases (gimple origt)
4502 gimple t = origt;
4503 vec<ce_s> lhsc = vNULL;
4504 vec<ce_s> rhsc = vNULL;
4505 struct constraint_expr *c;
4506 varinfo_t fi;
4508 /* Now build constraints expressions. */
4509 if (gimple_code (t) == GIMPLE_PHI)
4511 size_t i;
4512 unsigned int j;
4514 /* For a phi node, assign all the arguments to
4515 the result. */
4516 get_constraint_for (gimple_phi_result (t), &lhsc);
4517 for (i = 0; i < gimple_phi_num_args (t); i++)
4519 tree strippedrhs = PHI_ARG_DEF (t, i);
4521 STRIP_NOPS (strippedrhs);
4522 get_constraint_for_rhs (gimple_phi_arg_def (t, i), &rhsc);
4524 FOR_EACH_VEC_ELT (lhsc, j, c)
4526 struct constraint_expr *c2;
4527 while (rhsc.length () > 0)
4529 c2 = &rhsc.last ();
4530 process_constraint (new_constraint (*c, *c2));
4531 rhsc.pop ();
4536 /* In IPA mode, we need to generate constraints to pass call
4537 arguments through their calls. There are two cases,
4538 either a GIMPLE_CALL returning a value, or just a plain
4539 GIMPLE_CALL when we are not.
4541 In non-ipa mode, we need to generate constraints for each
4542 pointer passed by address. */
4543 else if (is_gimple_call (t))
4544 find_func_aliases_for_call (t);
4546 /* Otherwise, just a regular assignment statement. Only care about
4547 operations with pointer result, others are dealt with as escape
4548 points if they have pointer operands. */
4549 else if (is_gimple_assign (t))
4551 /* Otherwise, just a regular assignment statement. */
4552 tree lhsop = gimple_assign_lhs (t);
4553 tree rhsop = (gimple_num_ops (t) == 2) ? gimple_assign_rhs1 (t) : NULL;
4555 if (rhsop && TREE_CLOBBER_P (rhsop))
4556 /* Ignore clobbers, they don't actually store anything into
4557 the LHS. */
4559 else if (rhsop && AGGREGATE_TYPE_P (TREE_TYPE (lhsop)))
4560 do_structure_copy (lhsop, rhsop);
4561 else
4563 enum tree_code code = gimple_assign_rhs_code (t);
4565 get_constraint_for (lhsop, &lhsc);
4567 if (code == POINTER_PLUS_EXPR)
4568 get_constraint_for_ptr_offset (gimple_assign_rhs1 (t),
4569 gimple_assign_rhs2 (t), &rhsc);
4570 else if (code == BIT_AND_EXPR
4571 && TREE_CODE (gimple_assign_rhs2 (t)) == INTEGER_CST)
4573 /* Aligning a pointer via a BIT_AND_EXPR is offsetting
4574 the pointer. Handle it by offsetting it by UNKNOWN. */
4575 get_constraint_for_ptr_offset (gimple_assign_rhs1 (t),
4576 NULL_TREE, &rhsc);
4578 else if ((CONVERT_EXPR_CODE_P (code)
4579 && !(POINTER_TYPE_P (gimple_expr_type (t))
4580 && !POINTER_TYPE_P (TREE_TYPE (rhsop))))
4581 || gimple_assign_single_p (t))
4582 get_constraint_for_rhs (rhsop, &rhsc);
4583 else if (code == COND_EXPR)
4585 /* The result is a merge of both COND_EXPR arms. */
4586 vec<ce_s> tmp = vNULL;
4587 struct constraint_expr *rhsp;
4588 unsigned i;
4589 get_constraint_for_rhs (gimple_assign_rhs2 (t), &rhsc);
4590 get_constraint_for_rhs (gimple_assign_rhs3 (t), &tmp);
4591 FOR_EACH_VEC_ELT (tmp, i, rhsp)
4592 rhsc.safe_push (*rhsp);
4593 tmp.release ();
4595 else if (truth_value_p (code))
4596 /* Truth value results are not pointer (parts). Or at least
4597 very very unreasonable obfuscation of a part. */
4599 else
4601 /* All other operations are merges. */
4602 vec<ce_s> tmp = vNULL;
4603 struct constraint_expr *rhsp;
4604 unsigned i, j;
4605 get_constraint_for_rhs (gimple_assign_rhs1 (t), &rhsc);
4606 for (i = 2; i < gimple_num_ops (t); ++i)
4608 get_constraint_for_rhs (gimple_op (t, i), &tmp);
4609 FOR_EACH_VEC_ELT (tmp, j, rhsp)
4610 rhsc.safe_push (*rhsp);
4611 tmp.truncate (0);
4613 tmp.release ();
4615 process_all_all_constraints (lhsc, rhsc);
4617 /* If there is a store to a global variable the rhs escapes. */
4618 if ((lhsop = get_base_address (lhsop)) != NULL_TREE
4619 && DECL_P (lhsop)
4620 && is_global_var (lhsop)
4621 && (!in_ipa_mode
4622 || DECL_EXTERNAL (lhsop) || TREE_PUBLIC (lhsop)))
4623 make_escape_constraint (rhsop);
4625 /* Handle escapes through return. */
4626 else if (gimple_code (t) == GIMPLE_RETURN
4627 && gimple_return_retval (t) != NULL_TREE)
4629 fi = NULL;
4630 if (!in_ipa_mode
4631 || !(fi = get_vi_for_tree (cfun->decl)))
4632 make_escape_constraint (gimple_return_retval (t));
4633 else if (in_ipa_mode
4634 && fi != NULL)
4636 struct constraint_expr lhs ;
4637 struct constraint_expr *rhsp;
4638 unsigned i;
4640 lhs = get_function_part_constraint (fi, fi_result);
4641 get_constraint_for_rhs (gimple_return_retval (t), &rhsc);
4642 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
4643 process_constraint (new_constraint (lhs, *rhsp));
4646 /* Handle asms conservatively by adding escape constraints to everything. */
4647 else if (gimple_code (t) == GIMPLE_ASM)
4649 unsigned i, noutputs;
4650 const char **oconstraints;
4651 const char *constraint;
4652 bool allows_mem, allows_reg, is_inout;
4654 noutputs = gimple_asm_noutputs (t);
4655 oconstraints = XALLOCAVEC (const char *, noutputs);
4657 for (i = 0; i < noutputs; ++i)
4659 tree link = gimple_asm_output_op (t, i);
4660 tree op = TREE_VALUE (link);
4662 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
4663 oconstraints[i] = constraint;
4664 parse_output_constraint (&constraint, i, 0, 0, &allows_mem,
4665 &allows_reg, &is_inout);
4667 /* A memory constraint makes the address of the operand escape. */
4668 if (!allows_reg && allows_mem)
4669 make_escape_constraint (build_fold_addr_expr (op));
4671 /* The asm may read global memory, so outputs may point to
4672 any global memory. */
4673 if (op)
4675 vec<ce_s> lhsc = vNULL;
4676 struct constraint_expr rhsc, *lhsp;
4677 unsigned j;
4678 get_constraint_for (op, &lhsc);
4679 rhsc.var = nonlocal_id;
4680 rhsc.offset = 0;
4681 rhsc.type = SCALAR;
4682 FOR_EACH_VEC_ELT (lhsc, j, lhsp)
4683 process_constraint (new_constraint (*lhsp, rhsc));
4684 lhsc.release ();
4687 for (i = 0; i < gimple_asm_ninputs (t); ++i)
4689 tree link = gimple_asm_input_op (t, i);
4690 tree op = TREE_VALUE (link);
4692 constraint = TREE_STRING_POINTER (TREE_VALUE (TREE_PURPOSE (link)));
4694 parse_input_constraint (&constraint, 0, 0, noutputs, 0, oconstraints,
4695 &allows_mem, &allows_reg);
4697 /* A memory constraint makes the address of the operand escape. */
4698 if (!allows_reg && allows_mem)
4699 make_escape_constraint (build_fold_addr_expr (op));
4700 /* Strictly we'd only need the constraint to ESCAPED if
4701 the asm clobbers memory, otherwise using something
4702 along the lines of per-call clobbers/uses would be enough. */
4703 else if (op)
4704 make_escape_constraint (op);
4708 rhsc.release ();
4709 lhsc.release ();
4713 /* Create a constraint adding to the clobber set of FI the memory
4714 pointed to by PTR. */
4716 static void
4717 process_ipa_clobber (varinfo_t fi, tree ptr)
4719 vec<ce_s> ptrc = vNULL;
4720 struct constraint_expr *c, lhs;
4721 unsigned i;
4722 get_constraint_for_rhs (ptr, &ptrc);
4723 lhs = get_function_part_constraint (fi, fi_clobbers);
4724 FOR_EACH_VEC_ELT (ptrc, i, c)
4725 process_constraint (new_constraint (lhs, *c));
4726 ptrc.release ();
4729 /* Walk statement T setting up clobber and use constraints according to the
4730 references found in T. This function is a main part of the
4731 IPA constraint builder. */
4733 static void
4734 find_func_clobbers (gimple origt)
4736 gimple t = origt;
4737 vec<ce_s> lhsc = vNULL;
4738 vec<ce_s> rhsc = vNULL;
4739 varinfo_t fi;
4741 /* Add constraints for clobbered/used in IPA mode.
4742 We are not interested in what automatic variables are clobbered
4743 or used as we only use the information in the caller to which
4744 they do not escape. */
4745 gcc_assert (in_ipa_mode);
4747 /* If the stmt refers to memory in any way it better had a VUSE. */
4748 if (gimple_vuse (t) == NULL_TREE)
4749 return;
4751 /* We'd better have function information for the current function. */
4752 fi = lookup_vi_for_tree (cfun->decl);
4753 gcc_assert (fi != NULL);
4755 /* Account for stores in assignments and calls. */
4756 if (gimple_vdef (t) != NULL_TREE
4757 && gimple_has_lhs (t))
4759 tree lhs = gimple_get_lhs (t);
4760 tree tem = lhs;
4761 while (handled_component_p (tem))
4762 tem = TREE_OPERAND (tem, 0);
4763 if ((DECL_P (tem)
4764 && !auto_var_in_fn_p (tem, cfun->decl))
4765 || INDIRECT_REF_P (tem)
4766 || (TREE_CODE (tem) == MEM_REF
4767 && !(TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR
4768 && auto_var_in_fn_p
4769 (TREE_OPERAND (TREE_OPERAND (tem, 0), 0), cfun->decl))))
4771 struct constraint_expr lhsc, *rhsp;
4772 unsigned i;
4773 lhsc = get_function_part_constraint (fi, fi_clobbers);
4774 get_constraint_for_address_of (lhs, &rhsc);
4775 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
4776 process_constraint (new_constraint (lhsc, *rhsp));
4777 rhsc.release ();
4781 /* Account for uses in assigments and returns. */
4782 if (gimple_assign_single_p (t)
4783 || (gimple_code (t) == GIMPLE_RETURN
4784 && gimple_return_retval (t) != NULL_TREE))
4786 tree rhs = (gimple_assign_single_p (t)
4787 ? gimple_assign_rhs1 (t) : gimple_return_retval (t));
4788 tree tem = rhs;
4789 while (handled_component_p (tem))
4790 tem = TREE_OPERAND (tem, 0);
4791 if ((DECL_P (tem)
4792 && !auto_var_in_fn_p (tem, cfun->decl))
4793 || INDIRECT_REF_P (tem)
4794 || (TREE_CODE (tem) == MEM_REF
4795 && !(TREE_CODE (TREE_OPERAND (tem, 0)) == ADDR_EXPR
4796 && auto_var_in_fn_p
4797 (TREE_OPERAND (TREE_OPERAND (tem, 0), 0), cfun->decl))))
4799 struct constraint_expr lhs, *rhsp;
4800 unsigned i;
4801 lhs = get_function_part_constraint (fi, fi_uses);
4802 get_constraint_for_address_of (rhs, &rhsc);
4803 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
4804 process_constraint (new_constraint (lhs, *rhsp));
4805 rhsc.release ();
4809 if (is_gimple_call (t))
4811 varinfo_t cfi = NULL;
4812 tree decl = gimple_call_fndecl (t);
4813 struct constraint_expr lhs, rhs;
4814 unsigned i, j;
4816 /* For builtins we do not have separate function info. For those
4817 we do not generate escapes for we have to generate clobbers/uses. */
4818 if (gimple_call_builtin_p (t, BUILT_IN_NORMAL))
4819 switch (DECL_FUNCTION_CODE (decl))
4821 /* The following functions use and clobber memory pointed to
4822 by their arguments. */
4823 case BUILT_IN_STRCPY:
4824 case BUILT_IN_STRNCPY:
4825 case BUILT_IN_BCOPY:
4826 case BUILT_IN_MEMCPY:
4827 case BUILT_IN_MEMMOVE:
4828 case BUILT_IN_MEMPCPY:
4829 case BUILT_IN_STPCPY:
4830 case BUILT_IN_STPNCPY:
4831 case BUILT_IN_STRCAT:
4832 case BUILT_IN_STRNCAT:
4833 case BUILT_IN_STRCPY_CHK:
4834 case BUILT_IN_STRNCPY_CHK:
4835 case BUILT_IN_MEMCPY_CHK:
4836 case BUILT_IN_MEMMOVE_CHK:
4837 case BUILT_IN_MEMPCPY_CHK:
4838 case BUILT_IN_STPCPY_CHK:
4839 case BUILT_IN_STPNCPY_CHK:
4840 case BUILT_IN_STRCAT_CHK:
4841 case BUILT_IN_STRNCAT_CHK:
4843 tree dest = gimple_call_arg (t, (DECL_FUNCTION_CODE (decl)
4844 == BUILT_IN_BCOPY ? 1 : 0));
4845 tree src = gimple_call_arg (t, (DECL_FUNCTION_CODE (decl)
4846 == BUILT_IN_BCOPY ? 0 : 1));
4847 unsigned i;
4848 struct constraint_expr *rhsp, *lhsp;
4849 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
4850 lhs = get_function_part_constraint (fi, fi_clobbers);
4851 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
4852 process_constraint (new_constraint (lhs, *lhsp));
4853 lhsc.release ();
4854 get_constraint_for_ptr_offset (src, NULL_TREE, &rhsc);
4855 lhs = get_function_part_constraint (fi, fi_uses);
4856 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
4857 process_constraint (new_constraint (lhs, *rhsp));
4858 rhsc.release ();
4859 return;
4861 /* The following function clobbers memory pointed to by
4862 its argument. */
4863 case BUILT_IN_MEMSET:
4864 case BUILT_IN_MEMSET_CHK:
4866 tree dest = gimple_call_arg (t, 0);
4867 unsigned i;
4868 ce_s *lhsp;
4869 get_constraint_for_ptr_offset (dest, NULL_TREE, &lhsc);
4870 lhs = get_function_part_constraint (fi, fi_clobbers);
4871 FOR_EACH_VEC_ELT (lhsc, i, lhsp)
4872 process_constraint (new_constraint (lhs, *lhsp));
4873 lhsc.release ();
4874 return;
4876 /* The following functions clobber their second and third
4877 arguments. */
4878 case BUILT_IN_SINCOS:
4879 case BUILT_IN_SINCOSF:
4880 case BUILT_IN_SINCOSL:
4882 process_ipa_clobber (fi, gimple_call_arg (t, 1));
4883 process_ipa_clobber (fi, gimple_call_arg (t, 2));
4884 return;
4886 /* The following functions clobber their second argument. */
4887 case BUILT_IN_FREXP:
4888 case BUILT_IN_FREXPF:
4889 case BUILT_IN_FREXPL:
4890 case BUILT_IN_LGAMMA_R:
4891 case BUILT_IN_LGAMMAF_R:
4892 case BUILT_IN_LGAMMAL_R:
4893 case BUILT_IN_GAMMA_R:
4894 case BUILT_IN_GAMMAF_R:
4895 case BUILT_IN_GAMMAL_R:
4896 case BUILT_IN_MODF:
4897 case BUILT_IN_MODFF:
4898 case BUILT_IN_MODFL:
4900 process_ipa_clobber (fi, gimple_call_arg (t, 1));
4901 return;
4903 /* The following functions clobber their third argument. */
4904 case BUILT_IN_REMQUO:
4905 case BUILT_IN_REMQUOF:
4906 case BUILT_IN_REMQUOL:
4908 process_ipa_clobber (fi, gimple_call_arg (t, 2));
4909 return;
4911 /* The following functions neither read nor clobber memory. */
4912 case BUILT_IN_ASSUME_ALIGNED:
4913 case BUILT_IN_FREE:
4914 return;
4915 /* Trampolines are of no interest to us. */
4916 case BUILT_IN_INIT_TRAMPOLINE:
4917 case BUILT_IN_ADJUST_TRAMPOLINE:
4918 return;
4919 case BUILT_IN_VA_START:
4920 case BUILT_IN_VA_END:
4921 return;
4922 /* printf-style functions may have hooks to set pointers to
4923 point to somewhere into the generated string. Leave them
4924 for a later excercise... */
4925 default:
4926 /* Fallthru to general call handling. */;
4929 /* Parameters passed by value are used. */
4930 lhs = get_function_part_constraint (fi, fi_uses);
4931 for (i = 0; i < gimple_call_num_args (t); i++)
4933 struct constraint_expr *rhsp;
4934 tree arg = gimple_call_arg (t, i);
4936 if (TREE_CODE (arg) == SSA_NAME
4937 || is_gimple_min_invariant (arg))
4938 continue;
4940 get_constraint_for_address_of (arg, &rhsc);
4941 FOR_EACH_VEC_ELT (rhsc, j, rhsp)
4942 process_constraint (new_constraint (lhs, *rhsp));
4943 rhsc.release ();
4946 /* Build constraints for propagating clobbers/uses along the
4947 callgraph edges. */
4948 cfi = get_fi_for_callee (t);
4949 if (cfi->id == anything_id)
4951 if (gimple_vdef (t))
4952 make_constraint_from (first_vi_for_offset (fi, fi_clobbers),
4953 anything_id);
4954 make_constraint_from (first_vi_for_offset (fi, fi_uses),
4955 anything_id);
4956 return;
4959 /* For callees without function info (that's external functions),
4960 ESCAPED is clobbered and used. */
4961 if (gimple_call_fndecl (t)
4962 && !cfi->is_fn_info)
4964 varinfo_t vi;
4966 if (gimple_vdef (t))
4967 make_copy_constraint (first_vi_for_offset (fi, fi_clobbers),
4968 escaped_id);
4969 make_copy_constraint (first_vi_for_offset (fi, fi_uses), escaped_id);
4971 /* Also honor the call statement use/clobber info. */
4972 if ((vi = lookup_call_clobber_vi (t)) != NULL)
4973 make_copy_constraint (first_vi_for_offset (fi, fi_clobbers),
4974 vi->id);
4975 if ((vi = lookup_call_use_vi (t)) != NULL)
4976 make_copy_constraint (first_vi_for_offset (fi, fi_uses),
4977 vi->id);
4978 return;
4981 /* Otherwise the caller clobbers and uses what the callee does.
4982 ??? This should use a new complex constraint that filters
4983 local variables of the callee. */
4984 if (gimple_vdef (t))
4986 lhs = get_function_part_constraint (fi, fi_clobbers);
4987 rhs = get_function_part_constraint (cfi, fi_clobbers);
4988 process_constraint (new_constraint (lhs, rhs));
4990 lhs = get_function_part_constraint (fi, fi_uses);
4991 rhs = get_function_part_constraint (cfi, fi_uses);
4992 process_constraint (new_constraint (lhs, rhs));
4994 else if (gimple_code (t) == GIMPLE_ASM)
4996 /* ??? Ick. We can do better. */
4997 if (gimple_vdef (t))
4998 make_constraint_from (first_vi_for_offset (fi, fi_clobbers),
4999 anything_id);
5000 make_constraint_from (first_vi_for_offset (fi, fi_uses),
5001 anything_id);
5004 rhsc.release ();
5008 /* Find the first varinfo in the same variable as START that overlaps with
5009 OFFSET. Return NULL if we can't find one. */
5011 static varinfo_t
5012 first_vi_for_offset (varinfo_t start, unsigned HOST_WIDE_INT offset)
5014 /* If the offset is outside of the variable, bail out. */
5015 if (offset >= start->fullsize)
5016 return NULL;
5018 /* If we cannot reach offset from start, lookup the first field
5019 and start from there. */
5020 if (start->offset > offset)
5021 start = lookup_vi_for_tree (start->decl);
5023 while (start)
5025 /* We may not find a variable in the field list with the actual
5026 offset when when we have glommed a structure to a variable.
5027 In that case, however, offset should still be within the size
5028 of the variable. */
5029 if (offset >= start->offset
5030 && (offset - start->offset) < start->size)
5031 return start;
5033 start= start->next;
5036 return NULL;
5039 /* Find the first varinfo in the same variable as START that overlaps with
5040 OFFSET. If there is no such varinfo the varinfo directly preceding
5041 OFFSET is returned. */
5043 static varinfo_t
5044 first_or_preceding_vi_for_offset (varinfo_t start,
5045 unsigned HOST_WIDE_INT offset)
5047 /* If we cannot reach offset from start, lookup the first field
5048 and start from there. */
5049 if (start->offset > offset)
5050 start = lookup_vi_for_tree (start->decl);
5052 /* We may not find a variable in the field list with the actual
5053 offset when when we have glommed a structure to a variable.
5054 In that case, however, offset should still be within the size
5055 of the variable.
5056 If we got beyond the offset we look for return the field
5057 directly preceding offset which may be the last field. */
5058 while (start->next
5059 && offset >= start->offset
5060 && !((offset - start->offset) < start->size))
5061 start = start->next;
5063 return start;
5067 /* This structure is used during pushing fields onto the fieldstack
5068 to track the offset of the field, since bitpos_of_field gives it
5069 relative to its immediate containing type, and we want it relative
5070 to the ultimate containing object. */
5072 struct fieldoff
5074 /* Offset from the base of the base containing object to this field. */
5075 HOST_WIDE_INT offset;
5077 /* Size, in bits, of the field. */
5078 unsigned HOST_WIDE_INT size;
5080 unsigned has_unknown_size : 1;
5082 unsigned must_have_pointers : 1;
5084 unsigned may_have_pointers : 1;
5086 unsigned only_restrict_pointers : 1;
5088 typedef struct fieldoff fieldoff_s;
5091 /* qsort comparison function for two fieldoff's PA and PB */
5093 static int
5094 fieldoff_compare (const void *pa, const void *pb)
5096 const fieldoff_s *foa = (const fieldoff_s *)pa;
5097 const fieldoff_s *fob = (const fieldoff_s *)pb;
5098 unsigned HOST_WIDE_INT foasize, fobsize;
5100 if (foa->offset < fob->offset)
5101 return -1;
5102 else if (foa->offset > fob->offset)
5103 return 1;
5105 foasize = foa->size;
5106 fobsize = fob->size;
5107 if (foasize < fobsize)
5108 return -1;
5109 else if (foasize > fobsize)
5110 return 1;
5111 return 0;
5114 /* Sort a fieldstack according to the field offset and sizes. */
5115 static void
5116 sort_fieldstack (vec<fieldoff_s> fieldstack)
5118 fieldstack.qsort (fieldoff_compare);
5121 /* Return true if T is a type that can have subvars. */
5123 static inline bool
5124 type_can_have_subvars (const_tree t)
5126 /* Aggregates without overlapping fields can have subvars. */
5127 return TREE_CODE (t) == RECORD_TYPE;
5130 /* Return true if V is a tree that we can have subvars for.
5131 Normally, this is any aggregate type. Also complex
5132 types which are not gimple registers can have subvars. */
5134 static inline bool
5135 var_can_have_subvars (const_tree v)
5137 /* Volatile variables should never have subvars. */
5138 if (TREE_THIS_VOLATILE (v))
5139 return false;
5141 /* Non decls or memory tags can never have subvars. */
5142 if (!DECL_P (v))
5143 return false;
5145 return type_can_have_subvars (TREE_TYPE (v));
5148 /* Return true if T is a type that does contain pointers. */
5150 static bool
5151 type_must_have_pointers (tree type)
5153 if (POINTER_TYPE_P (type))
5154 return true;
5156 if (TREE_CODE (type) == ARRAY_TYPE)
5157 return type_must_have_pointers (TREE_TYPE (type));
5159 /* A function or method can have pointers as arguments, so track
5160 those separately. */
5161 if (TREE_CODE (type) == FUNCTION_TYPE
5162 || TREE_CODE (type) == METHOD_TYPE)
5163 return true;
5165 return false;
5168 static bool
5169 field_must_have_pointers (tree t)
5171 return type_must_have_pointers (TREE_TYPE (t));
5174 /* Given a TYPE, and a vector of field offsets FIELDSTACK, push all
5175 the fields of TYPE onto fieldstack, recording their offsets along
5176 the way.
5178 OFFSET is used to keep track of the offset in this entire
5179 structure, rather than just the immediately containing structure.
5180 Returns false if the caller is supposed to handle the field we
5181 recursed for. */
5183 static bool
5184 push_fields_onto_fieldstack (tree type, vec<fieldoff_s> *fieldstack,
5185 HOST_WIDE_INT offset)
5187 tree field;
5188 bool empty_p = true;
5190 if (TREE_CODE (type) != RECORD_TYPE)
5191 return false;
5193 /* If the vector of fields is growing too big, bail out early.
5194 Callers check for vec::length <= MAX_FIELDS_FOR_FIELD_SENSITIVE, make
5195 sure this fails. */
5196 if (fieldstack->length () > MAX_FIELDS_FOR_FIELD_SENSITIVE)
5197 return false;
5199 for (field = TYPE_FIELDS (type); field; field = DECL_CHAIN (field))
5200 if (TREE_CODE (field) == FIELD_DECL)
5202 bool push = false;
5203 HOST_WIDE_INT foff = bitpos_of_field (field);
5205 if (!var_can_have_subvars (field)
5206 || TREE_CODE (TREE_TYPE (field)) == QUAL_UNION_TYPE
5207 || TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
5208 push = true;
5209 else if (!push_fields_onto_fieldstack
5210 (TREE_TYPE (field), fieldstack, offset + foff)
5211 && (DECL_SIZE (field)
5212 && !integer_zerop (DECL_SIZE (field))))
5213 /* Empty structures may have actual size, like in C++. So
5214 see if we didn't push any subfields and the size is
5215 nonzero, push the field onto the stack. */
5216 push = true;
5218 if (push)
5220 fieldoff_s *pair = NULL;
5221 bool has_unknown_size = false;
5222 bool must_have_pointers_p;
5224 if (!fieldstack->is_empty ())
5225 pair = &fieldstack->last ();
5227 /* If there isn't anything at offset zero, create sth. */
5228 if (!pair
5229 && offset + foff != 0)
5231 fieldoff_s e = {0, offset + foff, false, false, false, false};
5232 pair = fieldstack->safe_push (e);
5235 if (!DECL_SIZE (field)
5236 || !host_integerp (DECL_SIZE (field), 1))
5237 has_unknown_size = true;
5239 /* If adjacent fields do not contain pointers merge them. */
5240 must_have_pointers_p = field_must_have_pointers (field);
5241 if (pair
5242 && !has_unknown_size
5243 && !must_have_pointers_p
5244 && !pair->must_have_pointers
5245 && !pair->has_unknown_size
5246 && pair->offset + (HOST_WIDE_INT)pair->size == offset + foff)
5248 pair->size += TREE_INT_CST_LOW (DECL_SIZE (field));
5250 else
5252 fieldoff_s e;
5253 e.offset = offset + foff;
5254 e.has_unknown_size = has_unknown_size;
5255 if (!has_unknown_size)
5256 e.size = TREE_INT_CST_LOW (DECL_SIZE (field));
5257 else
5258 e.size = -1;
5259 e.must_have_pointers = must_have_pointers_p;
5260 e.may_have_pointers = true;
5261 e.only_restrict_pointers
5262 = (!has_unknown_size
5263 && POINTER_TYPE_P (TREE_TYPE (field))
5264 && TYPE_RESTRICT (TREE_TYPE (field)));
5265 fieldstack->safe_push (e);
5269 empty_p = false;
5272 return !empty_p;
5275 /* Count the number of arguments DECL has, and set IS_VARARGS to true
5276 if it is a varargs function. */
5278 static unsigned int
5279 count_num_arguments (tree decl, bool *is_varargs)
5281 unsigned int num = 0;
5282 tree t;
5284 /* Capture named arguments for K&R functions. They do not
5285 have a prototype and thus no TYPE_ARG_TYPES. */
5286 for (t = DECL_ARGUMENTS (decl); t; t = DECL_CHAIN (t))
5287 ++num;
5289 /* Check if the function has variadic arguments. */
5290 for (t = TYPE_ARG_TYPES (TREE_TYPE (decl)); t; t = TREE_CHAIN (t))
5291 if (TREE_VALUE (t) == void_type_node)
5292 break;
5293 if (!t)
5294 *is_varargs = true;
5296 return num;
5299 /* Creation function node for DECL, using NAME, and return the index
5300 of the variable we've created for the function. */
5302 static varinfo_t
5303 create_function_info_for (tree decl, const char *name)
5305 struct function *fn = DECL_STRUCT_FUNCTION (decl);
5306 varinfo_t vi, prev_vi;
5307 tree arg;
5308 unsigned int i;
5309 bool is_varargs = false;
5310 unsigned int num_args = count_num_arguments (decl, &is_varargs);
5312 /* Create the variable info. */
5314 vi = new_var_info (decl, name);
5315 vi->offset = 0;
5316 vi->size = 1;
5317 vi->fullsize = fi_parm_base + num_args;
5318 vi->is_fn_info = 1;
5319 vi->may_have_pointers = false;
5320 if (is_varargs)
5321 vi->fullsize = ~0;
5322 insert_vi_for_tree (vi->decl, vi);
5324 prev_vi = vi;
5326 /* Create a variable for things the function clobbers and one for
5327 things the function uses. */
5329 varinfo_t clobbervi, usevi;
5330 const char *newname;
5331 char *tempname;
5333 asprintf (&tempname, "%s.clobber", name);
5334 newname = ggc_strdup (tempname);
5335 free (tempname);
5337 clobbervi = new_var_info (NULL, newname);
5338 clobbervi->offset = fi_clobbers;
5339 clobbervi->size = 1;
5340 clobbervi->fullsize = vi->fullsize;
5341 clobbervi->is_full_var = true;
5342 clobbervi->is_global_var = false;
5343 gcc_assert (prev_vi->offset < clobbervi->offset);
5344 prev_vi->next = clobbervi;
5345 prev_vi = clobbervi;
5347 asprintf (&tempname, "%s.use", name);
5348 newname = ggc_strdup (tempname);
5349 free (tempname);
5351 usevi = new_var_info (NULL, newname);
5352 usevi->offset = fi_uses;
5353 usevi->size = 1;
5354 usevi->fullsize = vi->fullsize;
5355 usevi->is_full_var = true;
5356 usevi->is_global_var = false;
5357 gcc_assert (prev_vi->offset < usevi->offset);
5358 prev_vi->next = usevi;
5359 prev_vi = usevi;
5362 /* And one for the static chain. */
5363 if (fn->static_chain_decl != NULL_TREE)
5365 varinfo_t chainvi;
5366 const char *newname;
5367 char *tempname;
5369 asprintf (&tempname, "%s.chain", name);
5370 newname = ggc_strdup (tempname);
5371 free (tempname);
5373 chainvi = new_var_info (fn->static_chain_decl, newname);
5374 chainvi->offset = fi_static_chain;
5375 chainvi->size = 1;
5376 chainvi->fullsize = vi->fullsize;
5377 chainvi->is_full_var = true;
5378 chainvi->is_global_var = false;
5379 gcc_assert (prev_vi->offset < chainvi->offset);
5380 prev_vi->next = chainvi;
5381 prev_vi = chainvi;
5382 insert_vi_for_tree (fn->static_chain_decl, chainvi);
5385 /* Create a variable for the return var. */
5386 if (DECL_RESULT (decl) != NULL
5387 || !VOID_TYPE_P (TREE_TYPE (TREE_TYPE (decl))))
5389 varinfo_t resultvi;
5390 const char *newname;
5391 char *tempname;
5392 tree resultdecl = decl;
5394 if (DECL_RESULT (decl))
5395 resultdecl = DECL_RESULT (decl);
5397 asprintf (&tempname, "%s.result", name);
5398 newname = ggc_strdup (tempname);
5399 free (tempname);
5401 resultvi = new_var_info (resultdecl, newname);
5402 resultvi->offset = fi_result;
5403 resultvi->size = 1;
5404 resultvi->fullsize = vi->fullsize;
5405 resultvi->is_full_var = true;
5406 if (DECL_RESULT (decl))
5407 resultvi->may_have_pointers = true;
5408 gcc_assert (prev_vi->offset < resultvi->offset);
5409 prev_vi->next = resultvi;
5410 prev_vi = resultvi;
5411 if (DECL_RESULT (decl))
5412 insert_vi_for_tree (DECL_RESULT (decl), resultvi);
5415 /* Set up variables for each argument. */
5416 arg = DECL_ARGUMENTS (decl);
5417 for (i = 0; i < num_args; i++)
5419 varinfo_t argvi;
5420 const char *newname;
5421 char *tempname;
5422 tree argdecl = decl;
5424 if (arg)
5425 argdecl = arg;
5427 asprintf (&tempname, "%s.arg%d", name, i);
5428 newname = ggc_strdup (tempname);
5429 free (tempname);
5431 argvi = new_var_info (argdecl, newname);
5432 argvi->offset = fi_parm_base + i;
5433 argvi->size = 1;
5434 argvi->is_full_var = true;
5435 argvi->fullsize = vi->fullsize;
5436 if (arg)
5437 argvi->may_have_pointers = true;
5438 gcc_assert (prev_vi->offset < argvi->offset);
5439 prev_vi->next = argvi;
5440 prev_vi = argvi;
5441 if (arg)
5443 insert_vi_for_tree (arg, argvi);
5444 arg = DECL_CHAIN (arg);
5448 /* Add one representative for all further args. */
5449 if (is_varargs)
5451 varinfo_t argvi;
5452 const char *newname;
5453 char *tempname;
5454 tree decl;
5456 asprintf (&tempname, "%s.varargs", name);
5457 newname = ggc_strdup (tempname);
5458 free (tempname);
5460 /* We need sth that can be pointed to for va_start. */
5461 decl = build_fake_var_decl (ptr_type_node);
5463 argvi = new_var_info (decl, newname);
5464 argvi->offset = fi_parm_base + num_args;
5465 argvi->size = ~0;
5466 argvi->is_full_var = true;
5467 argvi->is_heap_var = true;
5468 argvi->fullsize = vi->fullsize;
5469 gcc_assert (prev_vi->offset < argvi->offset);
5470 prev_vi->next = argvi;
5471 prev_vi = argvi;
5474 return vi;
5478 /* Return true if FIELDSTACK contains fields that overlap.
5479 FIELDSTACK is assumed to be sorted by offset. */
5481 static bool
5482 check_for_overlaps (vec<fieldoff_s> fieldstack)
5484 fieldoff_s *fo = NULL;
5485 unsigned int i;
5486 HOST_WIDE_INT lastoffset = -1;
5488 FOR_EACH_VEC_ELT (fieldstack, i, fo)
5490 if (fo->offset == lastoffset)
5491 return true;
5492 lastoffset = fo->offset;
5494 return false;
5497 /* Create a varinfo structure for NAME and DECL, and add it to VARMAP.
5498 This will also create any varinfo structures necessary for fields
5499 of DECL. */
5501 static varinfo_t
5502 create_variable_info_for_1 (tree decl, const char *name)
5504 varinfo_t vi, newvi;
5505 tree decl_type = TREE_TYPE (decl);
5506 tree declsize = DECL_P (decl) ? DECL_SIZE (decl) : TYPE_SIZE (decl_type);
5507 vec<fieldoff_s> fieldstack = vNULL;
5508 fieldoff_s *fo;
5509 unsigned int i;
5511 if (!declsize
5512 || !host_integerp (declsize, 1))
5514 vi = new_var_info (decl, name);
5515 vi->offset = 0;
5516 vi->size = ~0;
5517 vi->fullsize = ~0;
5518 vi->is_unknown_size_var = true;
5519 vi->is_full_var = true;
5520 vi->may_have_pointers = true;
5521 return vi;
5524 /* Collect field information. */
5525 if (use_field_sensitive
5526 && var_can_have_subvars (decl)
5527 /* ??? Force us to not use subfields for global initializers
5528 in IPA mode. Else we'd have to parse arbitrary initializers. */
5529 && !(in_ipa_mode
5530 && is_global_var (decl)
5531 && DECL_INITIAL (decl)))
5533 fieldoff_s *fo = NULL;
5534 bool notokay = false;
5535 unsigned int i;
5537 push_fields_onto_fieldstack (decl_type, &fieldstack, 0);
5539 for (i = 0; !notokay && fieldstack.iterate (i, &fo); i++)
5540 if (fo->has_unknown_size
5541 || fo->offset < 0)
5543 notokay = true;
5544 break;
5547 /* We can't sort them if we have a field with a variable sized type,
5548 which will make notokay = true. In that case, we are going to return
5549 without creating varinfos for the fields anyway, so sorting them is a
5550 waste to boot. */
5551 if (!notokay)
5553 sort_fieldstack (fieldstack);
5554 /* Due to some C++ FE issues, like PR 22488, we might end up
5555 what appear to be overlapping fields even though they,
5556 in reality, do not overlap. Until the C++ FE is fixed,
5557 we will simply disable field-sensitivity for these cases. */
5558 notokay = check_for_overlaps (fieldstack);
5561 if (notokay)
5562 fieldstack.release ();
5565 /* If we didn't end up collecting sub-variables create a full
5566 variable for the decl. */
5567 if (fieldstack.length () <= 1
5568 || fieldstack.length () > MAX_FIELDS_FOR_FIELD_SENSITIVE)
5570 vi = new_var_info (decl, name);
5571 vi->offset = 0;
5572 vi->may_have_pointers = true;
5573 vi->fullsize = TREE_INT_CST_LOW (declsize);
5574 vi->size = vi->fullsize;
5575 vi->is_full_var = true;
5576 fieldstack.release ();
5577 return vi;
5580 vi = new_var_info (decl, name);
5581 vi->fullsize = TREE_INT_CST_LOW (declsize);
5582 for (i = 0, newvi = vi;
5583 fieldstack.iterate (i, &fo);
5584 ++i, newvi = newvi->next)
5586 const char *newname = "NULL";
5587 char *tempname;
5589 if (dump_file)
5591 asprintf (&tempname, "%s." HOST_WIDE_INT_PRINT_DEC
5592 "+" HOST_WIDE_INT_PRINT_DEC, name, fo->offset, fo->size);
5593 newname = ggc_strdup (tempname);
5594 free (tempname);
5596 newvi->name = newname;
5597 newvi->offset = fo->offset;
5598 newvi->size = fo->size;
5599 newvi->fullsize = vi->fullsize;
5600 newvi->may_have_pointers = fo->may_have_pointers;
5601 newvi->only_restrict_pointers = fo->only_restrict_pointers;
5602 if (i + 1 < fieldstack.length ())
5603 newvi->next = new_var_info (decl, name);
5606 fieldstack.release ();
5608 return vi;
5611 static unsigned int
5612 create_variable_info_for (tree decl, const char *name)
5614 varinfo_t vi = create_variable_info_for_1 (decl, name);
5615 unsigned int id = vi->id;
5617 insert_vi_for_tree (decl, vi);
5619 if (TREE_CODE (decl) != VAR_DECL)
5620 return id;
5622 /* Create initial constraints for globals. */
5623 for (; vi; vi = vi->next)
5625 if (!vi->may_have_pointers
5626 || !vi->is_global_var)
5627 continue;
5629 /* Mark global restrict qualified pointers. */
5630 if ((POINTER_TYPE_P (TREE_TYPE (decl))
5631 && TYPE_RESTRICT (TREE_TYPE (decl)))
5632 || vi->only_restrict_pointers)
5634 make_constraint_from_global_restrict (vi, "GLOBAL_RESTRICT");
5635 continue;
5638 /* In non-IPA mode the initializer from nonlocal is all we need. */
5639 if (!in_ipa_mode
5640 || DECL_HARD_REGISTER (decl))
5641 make_copy_constraint (vi, nonlocal_id);
5643 /* In IPA mode parse the initializer and generate proper constraints
5644 for it. */
5645 else
5647 struct varpool_node *vnode = varpool_get_node (decl);
5649 /* For escaped variables initialize them from nonlocal. */
5650 if (!varpool_all_refs_explicit_p (vnode))
5651 make_copy_constraint (vi, nonlocal_id);
5653 /* If this is a global variable with an initializer and we are in
5654 IPA mode generate constraints for it. */
5655 if (DECL_INITIAL (decl)
5656 && vnode->analyzed)
5658 vec<ce_s> rhsc = vNULL;
5659 struct constraint_expr lhs, *rhsp;
5660 unsigned i;
5661 get_constraint_for_rhs (DECL_INITIAL (decl), &rhsc);
5662 lhs.var = vi->id;
5663 lhs.offset = 0;
5664 lhs.type = SCALAR;
5665 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
5666 process_constraint (new_constraint (lhs, *rhsp));
5667 /* If this is a variable that escapes from the unit
5668 the initializer escapes as well. */
5669 if (!varpool_all_refs_explicit_p (vnode))
5671 lhs.var = escaped_id;
5672 lhs.offset = 0;
5673 lhs.type = SCALAR;
5674 FOR_EACH_VEC_ELT (rhsc, i, rhsp)
5675 process_constraint (new_constraint (lhs, *rhsp));
5677 rhsc.release ();
5682 return id;
5685 /* Print out the points-to solution for VAR to FILE. */
5687 static void
5688 dump_solution_for_var (FILE *file, unsigned int var)
5690 varinfo_t vi = get_varinfo (var);
5691 unsigned int i;
5692 bitmap_iterator bi;
5694 /* Dump the solution for unified vars anyway, this avoids difficulties
5695 in scanning dumps in the testsuite. */
5696 fprintf (file, "%s = { ", vi->name);
5697 vi = get_varinfo (find (var));
5698 EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, i, bi)
5699 fprintf (file, "%s ", get_varinfo (i)->name);
5700 fprintf (file, "}");
5702 /* But note when the variable was unified. */
5703 if (vi->id != var)
5704 fprintf (file, " same as %s", vi->name);
5706 fprintf (file, "\n");
5709 /* Print the points-to solution for VAR to stdout. */
5711 DEBUG_FUNCTION void
5712 debug_solution_for_var (unsigned int var)
5714 dump_solution_for_var (stdout, var);
5717 /* Create varinfo structures for all of the variables in the
5718 function for intraprocedural mode. */
5720 static void
5721 intra_create_variable_infos (void)
5723 tree t;
5725 /* For each incoming pointer argument arg, create the constraint ARG
5726 = NONLOCAL or a dummy variable if it is a restrict qualified
5727 passed-by-reference argument. */
5728 for (t = DECL_ARGUMENTS (current_function_decl); t; t = DECL_CHAIN (t))
5730 varinfo_t p = get_vi_for_tree (t);
5732 /* For restrict qualified pointers to objects passed by
5733 reference build a real representative for the pointed-to object.
5734 Treat restrict qualified references the same. */
5735 if (TYPE_RESTRICT (TREE_TYPE (t))
5736 && ((DECL_BY_REFERENCE (t) && POINTER_TYPE_P (TREE_TYPE (t)))
5737 || TREE_CODE (TREE_TYPE (t)) == REFERENCE_TYPE)
5738 && !type_contains_placeholder_p (TREE_TYPE (TREE_TYPE (t))))
5740 struct constraint_expr lhsc, rhsc;
5741 varinfo_t vi;
5742 tree heapvar = build_fake_var_decl (TREE_TYPE (TREE_TYPE (t)));
5743 DECL_EXTERNAL (heapvar) = 1;
5744 vi = create_variable_info_for_1 (heapvar, "PARM_NOALIAS");
5745 insert_vi_for_tree (heapvar, vi);
5746 lhsc.var = p->id;
5747 lhsc.type = SCALAR;
5748 lhsc.offset = 0;
5749 rhsc.var = vi->id;
5750 rhsc.type = ADDRESSOF;
5751 rhsc.offset = 0;
5752 process_constraint (new_constraint (lhsc, rhsc));
5753 for (; vi; vi = vi->next)
5754 if (vi->may_have_pointers)
5756 if (vi->only_restrict_pointers)
5757 make_constraint_from_global_restrict (vi, "GLOBAL_RESTRICT");
5758 else
5759 make_copy_constraint (vi, nonlocal_id);
5761 continue;
5764 if (POINTER_TYPE_P (TREE_TYPE (t))
5765 && TYPE_RESTRICT (TREE_TYPE (t)))
5766 make_constraint_from_global_restrict (p, "PARM_RESTRICT");
5767 else
5769 for (; p; p = p->next)
5771 if (p->only_restrict_pointers)
5772 make_constraint_from_global_restrict (p, "PARM_RESTRICT");
5773 else if (p->may_have_pointers)
5774 make_constraint_from (p, nonlocal_id);
5779 /* Add a constraint for a result decl that is passed by reference. */
5780 if (DECL_RESULT (cfun->decl)
5781 && DECL_BY_REFERENCE (DECL_RESULT (cfun->decl)))
5783 varinfo_t p, result_vi = get_vi_for_tree (DECL_RESULT (cfun->decl));
5785 for (p = result_vi; p; p = p->next)
5786 make_constraint_from (p, nonlocal_id);
5789 /* Add a constraint for the incoming static chain parameter. */
5790 if (cfun->static_chain_decl != NULL_TREE)
5792 varinfo_t p, chain_vi = get_vi_for_tree (cfun->static_chain_decl);
5794 for (p = chain_vi; p; p = p->next)
5795 make_constraint_from (p, nonlocal_id);
5799 /* Structure used to put solution bitmaps in a hashtable so they can
5800 be shared among variables with the same points-to set. */
5802 typedef struct shared_bitmap_info
5804 bitmap pt_vars;
5805 hashval_t hashcode;
5806 } *shared_bitmap_info_t;
5807 typedef const struct shared_bitmap_info *const_shared_bitmap_info_t;
5809 static htab_t shared_bitmap_table;
5811 /* Hash function for a shared_bitmap_info_t */
5813 static hashval_t
5814 shared_bitmap_hash (const void *p)
5816 const_shared_bitmap_info_t const bi = (const_shared_bitmap_info_t) p;
5817 return bi->hashcode;
5820 /* Equality function for two shared_bitmap_info_t's. */
5822 static int
5823 shared_bitmap_eq (const void *p1, const void *p2)
5825 const_shared_bitmap_info_t const sbi1 = (const_shared_bitmap_info_t) p1;
5826 const_shared_bitmap_info_t const sbi2 = (const_shared_bitmap_info_t) p2;
5827 return bitmap_equal_p (sbi1->pt_vars, sbi2->pt_vars);
5830 /* Lookup a bitmap in the shared bitmap hashtable, and return an already
5831 existing instance if there is one, NULL otherwise. */
5833 static bitmap
5834 shared_bitmap_lookup (bitmap pt_vars)
5836 void **slot;
5837 struct shared_bitmap_info sbi;
5839 sbi.pt_vars = pt_vars;
5840 sbi.hashcode = bitmap_hash (pt_vars);
5842 slot = htab_find_slot_with_hash (shared_bitmap_table, &sbi,
5843 sbi.hashcode, NO_INSERT);
5844 if (!slot)
5845 return NULL;
5846 else
5847 return ((shared_bitmap_info_t) *slot)->pt_vars;
5851 /* Add a bitmap to the shared bitmap hashtable. */
5853 static void
5854 shared_bitmap_add (bitmap pt_vars)
5856 void **slot;
5857 shared_bitmap_info_t sbi = XNEW (struct shared_bitmap_info);
5859 sbi->pt_vars = pt_vars;
5860 sbi->hashcode = bitmap_hash (pt_vars);
5862 slot = htab_find_slot_with_hash (shared_bitmap_table, sbi,
5863 sbi->hashcode, INSERT);
5864 gcc_assert (!*slot);
5865 *slot = (void *) sbi;
5869 /* Set bits in INTO corresponding to the variable uids in solution set FROM. */
5871 static void
5872 set_uids_in_ptset (bitmap into, bitmap from, struct pt_solution *pt)
5874 unsigned int i;
5875 bitmap_iterator bi;
5877 EXECUTE_IF_SET_IN_BITMAP (from, 0, i, bi)
5879 varinfo_t vi = get_varinfo (i);
5881 /* The only artificial variables that are allowed in a may-alias
5882 set are heap variables. */
5883 if (vi->is_artificial_var && !vi->is_heap_var)
5884 continue;
5886 if (TREE_CODE (vi->decl) == VAR_DECL
5887 || TREE_CODE (vi->decl) == PARM_DECL
5888 || TREE_CODE (vi->decl) == RESULT_DECL)
5890 /* If we are in IPA mode we will not recompute points-to
5891 sets after inlining so make sure they stay valid. */
5892 if (in_ipa_mode
5893 && !DECL_PT_UID_SET_P (vi->decl))
5894 SET_DECL_PT_UID (vi->decl, DECL_UID (vi->decl));
5896 /* Add the decl to the points-to set. Note that the points-to
5897 set contains global variables. */
5898 bitmap_set_bit (into, DECL_PT_UID (vi->decl));
5899 if (vi->is_global_var)
5900 pt->vars_contains_global = true;
5906 /* Compute the points-to solution *PT for the variable VI. */
5908 static struct pt_solution
5909 find_what_var_points_to (varinfo_t orig_vi)
5911 unsigned int i;
5912 bitmap_iterator bi;
5913 bitmap finished_solution;
5914 bitmap result;
5915 varinfo_t vi;
5916 void **slot;
5917 struct pt_solution *pt;
5919 /* This variable may have been collapsed, let's get the real
5920 variable. */
5921 vi = get_varinfo (find (orig_vi->id));
5923 /* See if we have already computed the solution and return it. */
5924 slot = pointer_map_insert (final_solutions, vi);
5925 if (*slot != NULL)
5926 return *(struct pt_solution *)*slot;
5928 *slot = pt = XOBNEW (&final_solutions_obstack, struct pt_solution);
5929 memset (pt, 0, sizeof (struct pt_solution));
5931 /* Translate artificial variables into SSA_NAME_PTR_INFO
5932 attributes. */
5933 EXECUTE_IF_SET_IN_BITMAP (vi->solution, 0, i, bi)
5935 varinfo_t vi = get_varinfo (i);
5937 if (vi->is_artificial_var)
5939 if (vi->id == nothing_id)
5940 pt->null = 1;
5941 else if (vi->id == escaped_id)
5943 if (in_ipa_mode)
5944 pt->ipa_escaped = 1;
5945 else
5946 pt->escaped = 1;
5948 else if (vi->id == nonlocal_id)
5949 pt->nonlocal = 1;
5950 else if (vi->is_heap_var)
5951 /* We represent heapvars in the points-to set properly. */
5953 else if (vi->id == readonly_id)
5954 /* Nobody cares. */
5956 else if (vi->id == anything_id
5957 || vi->id == integer_id)
5958 pt->anything = 1;
5962 /* Instead of doing extra work, simply do not create
5963 elaborate points-to information for pt_anything pointers. */
5964 if (pt->anything)
5965 return *pt;
5967 /* Share the final set of variables when possible. */
5968 finished_solution = BITMAP_GGC_ALLOC ();
5969 stats.points_to_sets_created++;
5971 set_uids_in_ptset (finished_solution, vi->solution, pt);
5972 result = shared_bitmap_lookup (finished_solution);
5973 if (!result)
5975 shared_bitmap_add (finished_solution);
5976 pt->vars = finished_solution;
5978 else
5980 pt->vars = result;
5981 bitmap_clear (finished_solution);
5984 return *pt;
5987 /* Given a pointer variable P, fill in its points-to set. */
5989 static void
5990 find_what_p_points_to (tree p)
5992 struct ptr_info_def *pi;
5993 tree lookup_p = p;
5994 varinfo_t vi;
5996 /* For parameters, get at the points-to set for the actual parm
5997 decl. */
5998 if (TREE_CODE (p) == SSA_NAME
5999 && SSA_NAME_IS_DEFAULT_DEF (p)
6000 && (TREE_CODE (SSA_NAME_VAR (p)) == PARM_DECL
6001 || TREE_CODE (SSA_NAME_VAR (p)) == RESULT_DECL))
6002 lookup_p = SSA_NAME_VAR (p);
6004 vi = lookup_vi_for_tree (lookup_p);
6005 if (!vi)
6006 return;
6008 pi = get_ptr_info (p);
6009 pi->pt = find_what_var_points_to (vi);
6013 /* Query statistics for points-to solutions. */
6015 static struct {
6016 unsigned HOST_WIDE_INT pt_solution_includes_may_alias;
6017 unsigned HOST_WIDE_INT pt_solution_includes_no_alias;
6018 unsigned HOST_WIDE_INT pt_solutions_intersect_may_alias;
6019 unsigned HOST_WIDE_INT pt_solutions_intersect_no_alias;
6020 } pta_stats;
6022 void
6023 dump_pta_stats (FILE *s)
6025 fprintf (s, "\nPTA query stats:\n");
6026 fprintf (s, " pt_solution_includes: "
6027 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
6028 HOST_WIDE_INT_PRINT_DEC" queries\n",
6029 pta_stats.pt_solution_includes_no_alias,
6030 pta_stats.pt_solution_includes_no_alias
6031 + pta_stats.pt_solution_includes_may_alias);
6032 fprintf (s, " pt_solutions_intersect: "
6033 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
6034 HOST_WIDE_INT_PRINT_DEC" queries\n",
6035 pta_stats.pt_solutions_intersect_no_alias,
6036 pta_stats.pt_solutions_intersect_no_alias
6037 + pta_stats.pt_solutions_intersect_may_alias);
6041 /* Reset the points-to solution *PT to a conservative default
6042 (point to anything). */
6044 void
6045 pt_solution_reset (struct pt_solution *pt)
6047 memset (pt, 0, sizeof (struct pt_solution));
6048 pt->anything = true;
6051 /* Set the points-to solution *PT to point only to the variables
6052 in VARS. VARS_CONTAINS_GLOBAL specifies whether that contains
6053 global variables and VARS_CONTAINS_RESTRICT specifies whether
6054 it contains restrict tag variables. */
6056 void
6057 pt_solution_set (struct pt_solution *pt, bitmap vars, bool vars_contains_global)
6059 memset (pt, 0, sizeof (struct pt_solution));
6060 pt->vars = vars;
6061 pt->vars_contains_global = vars_contains_global;
6064 /* Set the points-to solution *PT to point only to the variable VAR. */
6066 void
6067 pt_solution_set_var (struct pt_solution *pt, tree var)
6069 memset (pt, 0, sizeof (struct pt_solution));
6070 pt->vars = BITMAP_GGC_ALLOC ();
6071 bitmap_set_bit (pt->vars, DECL_PT_UID (var));
6072 pt->vars_contains_global = is_global_var (var);
6075 /* Computes the union of the points-to solutions *DEST and *SRC and
6076 stores the result in *DEST. This changes the points-to bitmap
6077 of *DEST and thus may not be used if that might be shared.
6078 The points-to bitmap of *SRC and *DEST will not be shared after
6079 this function if they were not before. */
6081 static void
6082 pt_solution_ior_into (struct pt_solution *dest, struct pt_solution *src)
6084 dest->anything |= src->anything;
6085 if (dest->anything)
6087 pt_solution_reset (dest);
6088 return;
6091 dest->nonlocal |= src->nonlocal;
6092 dest->escaped |= src->escaped;
6093 dest->ipa_escaped |= src->ipa_escaped;
6094 dest->null |= src->null;
6095 dest->vars_contains_global |= src->vars_contains_global;
6096 if (!src->vars)
6097 return;
6099 if (!dest->vars)
6100 dest->vars = BITMAP_GGC_ALLOC ();
6101 bitmap_ior_into (dest->vars, src->vars);
6104 /* Return true if the points-to solution *PT is empty. */
6106 bool
6107 pt_solution_empty_p (struct pt_solution *pt)
6109 if (pt->anything
6110 || pt->nonlocal)
6111 return false;
6113 if (pt->vars
6114 && !bitmap_empty_p (pt->vars))
6115 return false;
6117 /* If the solution includes ESCAPED, check if that is empty. */
6118 if (pt->escaped
6119 && !pt_solution_empty_p (&cfun->gimple_df->escaped))
6120 return false;
6122 /* If the solution includes ESCAPED, check if that is empty. */
6123 if (pt->ipa_escaped
6124 && !pt_solution_empty_p (&ipa_escaped_pt))
6125 return false;
6127 return true;
6130 /* Return true if the points-to solution *PT only point to a single var, and
6131 return the var uid in *UID. */
6133 bool
6134 pt_solution_singleton_p (struct pt_solution *pt, unsigned *uid)
6136 if (pt->anything || pt->nonlocal || pt->escaped || pt->ipa_escaped
6137 || pt->null || pt->vars == NULL
6138 || !bitmap_single_bit_set_p (pt->vars))
6139 return false;
6141 *uid = bitmap_first_set_bit (pt->vars);
6142 return true;
6145 /* Return true if the points-to solution *PT includes global memory. */
6147 bool
6148 pt_solution_includes_global (struct pt_solution *pt)
6150 if (pt->anything
6151 || pt->nonlocal
6152 || pt->vars_contains_global)
6153 return true;
6155 if (pt->escaped)
6156 return pt_solution_includes_global (&cfun->gimple_df->escaped);
6158 if (pt->ipa_escaped)
6159 return pt_solution_includes_global (&ipa_escaped_pt);
6161 /* ??? This predicate is not correct for the IPA-PTA solution
6162 as we do not properly distinguish between unit escape points
6163 and global variables. */
6164 if (cfun->gimple_df->ipa_pta)
6165 return true;
6167 return false;
6170 /* Return true if the points-to solution *PT includes the variable
6171 declaration DECL. */
6173 static bool
6174 pt_solution_includes_1 (struct pt_solution *pt, const_tree decl)
6176 if (pt->anything)
6177 return true;
6179 if (pt->nonlocal
6180 && is_global_var (decl))
6181 return true;
6183 if (pt->vars
6184 && bitmap_bit_p (pt->vars, DECL_PT_UID (decl)))
6185 return true;
6187 /* If the solution includes ESCAPED, check it. */
6188 if (pt->escaped
6189 && pt_solution_includes_1 (&cfun->gimple_df->escaped, decl))
6190 return true;
6192 /* If the solution includes ESCAPED, check it. */
6193 if (pt->ipa_escaped
6194 && pt_solution_includes_1 (&ipa_escaped_pt, decl))
6195 return true;
6197 return false;
6200 bool
6201 pt_solution_includes (struct pt_solution *pt, const_tree decl)
6203 bool res = pt_solution_includes_1 (pt, decl);
6204 if (res)
6205 ++pta_stats.pt_solution_includes_may_alias;
6206 else
6207 ++pta_stats.pt_solution_includes_no_alias;
6208 return res;
6211 /* Return true if both points-to solutions PT1 and PT2 have a non-empty
6212 intersection. */
6214 static bool
6215 pt_solutions_intersect_1 (struct pt_solution *pt1, struct pt_solution *pt2)
6217 if (pt1->anything || pt2->anything)
6218 return true;
6220 /* If either points to unknown global memory and the other points to
6221 any global memory they alias. */
6222 if ((pt1->nonlocal
6223 && (pt2->nonlocal
6224 || pt2->vars_contains_global))
6225 || (pt2->nonlocal
6226 && pt1->vars_contains_global))
6227 return true;
6229 /* Check the escaped solution if required. */
6230 if ((pt1->escaped || pt2->escaped)
6231 && !pt_solution_empty_p (&cfun->gimple_df->escaped))
6233 /* If both point to escaped memory and that solution
6234 is not empty they alias. */
6235 if (pt1->escaped && pt2->escaped)
6236 return true;
6238 /* If either points to escaped memory see if the escaped solution
6239 intersects with the other. */
6240 if ((pt1->escaped
6241 && pt_solutions_intersect_1 (&cfun->gimple_df->escaped, pt2))
6242 || (pt2->escaped
6243 && pt_solutions_intersect_1 (&cfun->gimple_df->escaped, pt1)))
6244 return true;
6247 /* Check the escaped solution if required.
6248 ??? Do we need to check the local against the IPA escaped sets? */
6249 if ((pt1->ipa_escaped || pt2->ipa_escaped)
6250 && !pt_solution_empty_p (&ipa_escaped_pt))
6252 /* If both point to escaped memory and that solution
6253 is not empty they alias. */
6254 if (pt1->ipa_escaped && pt2->ipa_escaped)
6255 return true;
6257 /* If either points to escaped memory see if the escaped solution
6258 intersects with the other. */
6259 if ((pt1->ipa_escaped
6260 && pt_solutions_intersect_1 (&ipa_escaped_pt, pt2))
6261 || (pt2->ipa_escaped
6262 && pt_solutions_intersect_1 (&ipa_escaped_pt, pt1)))
6263 return true;
6266 /* Now both pointers alias if their points-to solution intersects. */
6267 return (pt1->vars
6268 && pt2->vars
6269 && bitmap_intersect_p (pt1->vars, pt2->vars));
6272 bool
6273 pt_solutions_intersect (struct pt_solution *pt1, struct pt_solution *pt2)
6275 bool res = pt_solutions_intersect_1 (pt1, pt2);
6276 if (res)
6277 ++pta_stats.pt_solutions_intersect_may_alias;
6278 else
6279 ++pta_stats.pt_solutions_intersect_no_alias;
6280 return res;
6284 /* Dump points-to information to OUTFILE. */
6286 static void
6287 dump_sa_points_to_info (FILE *outfile)
6289 unsigned int i;
6291 fprintf (outfile, "\nPoints-to sets\n\n");
6293 if (dump_flags & TDF_STATS)
6295 fprintf (outfile, "Stats:\n");
6296 fprintf (outfile, "Total vars: %d\n", stats.total_vars);
6297 fprintf (outfile, "Non-pointer vars: %d\n",
6298 stats.nonpointer_vars);
6299 fprintf (outfile, "Statically unified vars: %d\n",
6300 stats.unified_vars_static);
6301 fprintf (outfile, "Dynamically unified vars: %d\n",
6302 stats.unified_vars_dynamic);
6303 fprintf (outfile, "Iterations: %d\n", stats.iterations);
6304 fprintf (outfile, "Number of edges: %d\n", stats.num_edges);
6305 fprintf (outfile, "Number of implicit edges: %d\n",
6306 stats.num_implicit_edges);
6309 for (i = 0; i < varmap.length (); i++)
6311 varinfo_t vi = get_varinfo (i);
6312 if (!vi->may_have_pointers)
6313 continue;
6314 dump_solution_for_var (outfile, i);
6319 /* Debug points-to information to stderr. */
6321 DEBUG_FUNCTION void
6322 debug_sa_points_to_info (void)
6324 dump_sa_points_to_info (stderr);
6328 /* Initialize the always-existing constraint variables for NULL
6329 ANYTHING, READONLY, and INTEGER */
6331 static void
6332 init_base_vars (void)
6334 struct constraint_expr lhs, rhs;
6335 varinfo_t var_anything;
6336 varinfo_t var_nothing;
6337 varinfo_t var_readonly;
6338 varinfo_t var_escaped;
6339 varinfo_t var_nonlocal;
6340 varinfo_t var_storedanything;
6341 varinfo_t var_integer;
6343 /* Create the NULL variable, used to represent that a variable points
6344 to NULL. */
6345 var_nothing = new_var_info (NULL_TREE, "NULL");
6346 gcc_assert (var_nothing->id == nothing_id);
6347 var_nothing->is_artificial_var = 1;
6348 var_nothing->offset = 0;
6349 var_nothing->size = ~0;
6350 var_nothing->fullsize = ~0;
6351 var_nothing->is_special_var = 1;
6352 var_nothing->may_have_pointers = 0;
6353 var_nothing->is_global_var = 0;
6355 /* Create the ANYTHING variable, used to represent that a variable
6356 points to some unknown piece of memory. */
6357 var_anything = new_var_info (NULL_TREE, "ANYTHING");
6358 gcc_assert (var_anything->id == anything_id);
6359 var_anything->is_artificial_var = 1;
6360 var_anything->size = ~0;
6361 var_anything->offset = 0;
6362 var_anything->next = NULL;
6363 var_anything->fullsize = ~0;
6364 var_anything->is_special_var = 1;
6366 /* Anything points to anything. This makes deref constraints just
6367 work in the presence of linked list and other p = *p type loops,
6368 by saying that *ANYTHING = ANYTHING. */
6369 lhs.type = SCALAR;
6370 lhs.var = anything_id;
6371 lhs.offset = 0;
6372 rhs.type = ADDRESSOF;
6373 rhs.var = anything_id;
6374 rhs.offset = 0;
6376 /* This specifically does not use process_constraint because
6377 process_constraint ignores all anything = anything constraints, since all
6378 but this one are redundant. */
6379 constraints.safe_push (new_constraint (lhs, rhs));
6381 /* Create the READONLY variable, used to represent that a variable
6382 points to readonly memory. */
6383 var_readonly = new_var_info (NULL_TREE, "READONLY");
6384 gcc_assert (var_readonly->id == readonly_id);
6385 var_readonly->is_artificial_var = 1;
6386 var_readonly->offset = 0;
6387 var_readonly->size = ~0;
6388 var_readonly->fullsize = ~0;
6389 var_readonly->next = NULL;
6390 var_readonly->is_special_var = 1;
6392 /* readonly memory points to anything, in order to make deref
6393 easier. In reality, it points to anything the particular
6394 readonly variable can point to, but we don't track this
6395 separately. */
6396 lhs.type = SCALAR;
6397 lhs.var = readonly_id;
6398 lhs.offset = 0;
6399 rhs.type = ADDRESSOF;
6400 rhs.var = readonly_id; /* FIXME */
6401 rhs.offset = 0;
6402 process_constraint (new_constraint (lhs, rhs));
6404 /* Create the ESCAPED variable, used to represent the set of escaped
6405 memory. */
6406 var_escaped = new_var_info (NULL_TREE, "ESCAPED");
6407 gcc_assert (var_escaped->id == escaped_id);
6408 var_escaped->is_artificial_var = 1;
6409 var_escaped->offset = 0;
6410 var_escaped->size = ~0;
6411 var_escaped->fullsize = ~0;
6412 var_escaped->is_special_var = 0;
6414 /* Create the NONLOCAL variable, used to represent the set of nonlocal
6415 memory. */
6416 var_nonlocal = new_var_info (NULL_TREE, "NONLOCAL");
6417 gcc_assert (var_nonlocal->id == nonlocal_id);
6418 var_nonlocal->is_artificial_var = 1;
6419 var_nonlocal->offset = 0;
6420 var_nonlocal->size = ~0;
6421 var_nonlocal->fullsize = ~0;
6422 var_nonlocal->is_special_var = 1;
6424 /* ESCAPED = *ESCAPED, because escaped is may-deref'd at calls, etc. */
6425 lhs.type = SCALAR;
6426 lhs.var = escaped_id;
6427 lhs.offset = 0;
6428 rhs.type = DEREF;
6429 rhs.var = escaped_id;
6430 rhs.offset = 0;
6431 process_constraint (new_constraint (lhs, rhs));
6433 /* ESCAPED = ESCAPED + UNKNOWN_OFFSET, because if a sub-field escapes the
6434 whole variable escapes. */
6435 lhs.type = SCALAR;
6436 lhs.var = escaped_id;
6437 lhs.offset = 0;
6438 rhs.type = SCALAR;
6439 rhs.var = escaped_id;
6440 rhs.offset = UNKNOWN_OFFSET;
6441 process_constraint (new_constraint (lhs, rhs));
6443 /* *ESCAPED = NONLOCAL. This is true because we have to assume
6444 everything pointed to by escaped points to what global memory can
6445 point to. */
6446 lhs.type = DEREF;
6447 lhs.var = escaped_id;
6448 lhs.offset = 0;
6449 rhs.type = SCALAR;
6450 rhs.var = nonlocal_id;
6451 rhs.offset = 0;
6452 process_constraint (new_constraint (lhs, rhs));
6454 /* NONLOCAL = &NONLOCAL, NONLOCAL = &ESCAPED. This is true because
6455 global memory may point to global memory and escaped memory. */
6456 lhs.type = SCALAR;
6457 lhs.var = nonlocal_id;
6458 lhs.offset = 0;
6459 rhs.type = ADDRESSOF;
6460 rhs.var = nonlocal_id;
6461 rhs.offset = 0;
6462 process_constraint (new_constraint (lhs, rhs));
6463 rhs.type = ADDRESSOF;
6464 rhs.var = escaped_id;
6465 rhs.offset = 0;
6466 process_constraint (new_constraint (lhs, rhs));
6468 /* Create the STOREDANYTHING variable, used to represent the set of
6469 variables stored to *ANYTHING. */
6470 var_storedanything = new_var_info (NULL_TREE, "STOREDANYTHING");
6471 gcc_assert (var_storedanything->id == storedanything_id);
6472 var_storedanything->is_artificial_var = 1;
6473 var_storedanything->offset = 0;
6474 var_storedanything->size = ~0;
6475 var_storedanything->fullsize = ~0;
6476 var_storedanything->is_special_var = 0;
6478 /* Create the INTEGER variable, used to represent that a variable points
6479 to what an INTEGER "points to". */
6480 var_integer = new_var_info (NULL_TREE, "INTEGER");
6481 gcc_assert (var_integer->id == integer_id);
6482 var_integer->is_artificial_var = 1;
6483 var_integer->size = ~0;
6484 var_integer->fullsize = ~0;
6485 var_integer->offset = 0;
6486 var_integer->next = NULL;
6487 var_integer->is_special_var = 1;
6489 /* INTEGER = ANYTHING, because we don't know where a dereference of
6490 a random integer will point to. */
6491 lhs.type = SCALAR;
6492 lhs.var = integer_id;
6493 lhs.offset = 0;
6494 rhs.type = ADDRESSOF;
6495 rhs.var = anything_id;
6496 rhs.offset = 0;
6497 process_constraint (new_constraint (lhs, rhs));
6500 /* Initialize things necessary to perform PTA */
6502 static void
6503 init_alias_vars (void)
6505 use_field_sensitive = (MAX_FIELDS_FOR_FIELD_SENSITIVE > 1);
6507 bitmap_obstack_initialize (&pta_obstack);
6508 bitmap_obstack_initialize (&oldpta_obstack);
6509 bitmap_obstack_initialize (&predbitmap_obstack);
6511 constraint_pool = create_alloc_pool ("Constraint pool",
6512 sizeof (struct constraint), 30);
6513 variable_info_pool = create_alloc_pool ("Variable info pool",
6514 sizeof (struct variable_info), 30);
6515 constraints.create (8);
6516 varmap.create (8);
6517 vi_for_tree = pointer_map_create ();
6518 call_stmt_vars = pointer_map_create ();
6520 memset (&stats, 0, sizeof (stats));
6521 shared_bitmap_table = htab_create (511, shared_bitmap_hash,
6522 shared_bitmap_eq, free);
6523 init_base_vars ();
6525 gcc_obstack_init (&fake_var_decl_obstack);
6527 final_solutions = pointer_map_create ();
6528 gcc_obstack_init (&final_solutions_obstack);
6531 /* Remove the REF and ADDRESS edges from GRAPH, as well as all the
6532 predecessor edges. */
6534 static void
6535 remove_preds_and_fake_succs (constraint_graph_t graph)
6537 unsigned int i;
6539 /* Clear the implicit ref and address nodes from the successor
6540 lists. */
6541 for (i = 0; i < FIRST_REF_NODE; i++)
6543 if (graph->succs[i])
6544 bitmap_clear_range (graph->succs[i], FIRST_REF_NODE,
6545 FIRST_REF_NODE * 2);
6548 /* Free the successor list for the non-ref nodes. */
6549 for (i = FIRST_REF_NODE; i < graph->size; i++)
6551 if (graph->succs[i])
6552 BITMAP_FREE (graph->succs[i]);
6555 /* Now reallocate the size of the successor list as, and blow away
6556 the predecessor bitmaps. */
6557 graph->size = varmap.length ();
6558 graph->succs = XRESIZEVEC (bitmap, graph->succs, graph->size);
6560 free (graph->implicit_preds);
6561 graph->implicit_preds = NULL;
6562 free (graph->preds);
6563 graph->preds = NULL;
6564 bitmap_obstack_release (&predbitmap_obstack);
6567 /* Solve the constraint set. */
6569 static void
6570 solve_constraints (void)
6572 struct scc_info *si;
6574 if (dump_file)
6575 fprintf (dump_file,
6576 "\nCollapsing static cycles and doing variable "
6577 "substitution\n");
6579 init_graph (varmap.length () * 2);
6581 if (dump_file)
6582 fprintf (dump_file, "Building predecessor graph\n");
6583 build_pred_graph ();
6585 if (dump_file)
6586 fprintf (dump_file, "Detecting pointer and location "
6587 "equivalences\n");
6588 si = perform_var_substitution (graph);
6590 if (dump_file)
6591 fprintf (dump_file, "Rewriting constraints and unifying "
6592 "variables\n");
6593 rewrite_constraints (graph, si);
6595 build_succ_graph ();
6597 free_var_substitution_info (si);
6599 /* Attach complex constraints to graph nodes. */
6600 move_complex_constraints (graph);
6602 if (dump_file)
6603 fprintf (dump_file, "Uniting pointer but not location equivalent "
6604 "variables\n");
6605 unite_pointer_equivalences (graph);
6607 if (dump_file)
6608 fprintf (dump_file, "Finding indirect cycles\n");
6609 find_indirect_cycles (graph);
6611 /* Implicit nodes and predecessors are no longer necessary at this
6612 point. */
6613 remove_preds_and_fake_succs (graph);
6615 if (dump_file && (dump_flags & TDF_GRAPH))
6617 fprintf (dump_file, "\n\n// The constraint graph before solve-graph "
6618 "in dot format:\n");
6619 dump_constraint_graph (dump_file);
6620 fprintf (dump_file, "\n\n");
6623 if (dump_file)
6624 fprintf (dump_file, "Solving graph\n");
6626 solve_graph (graph);
6628 if (dump_file && (dump_flags & TDF_GRAPH))
6630 fprintf (dump_file, "\n\n// The constraint graph after solve-graph "
6631 "in dot format:\n");
6632 dump_constraint_graph (dump_file);
6633 fprintf (dump_file, "\n\n");
6636 if (dump_file)
6637 dump_sa_points_to_info (dump_file);
6640 /* Create points-to sets for the current function. See the comments
6641 at the start of the file for an algorithmic overview. */
6643 static void
6644 compute_points_to_sets (void)
6646 basic_block bb;
6647 unsigned i;
6648 varinfo_t vi;
6650 timevar_push (TV_TREE_PTA);
6652 init_alias_vars ();
6654 intra_create_variable_infos ();
6656 /* Now walk all statements and build the constraint set. */
6657 FOR_EACH_BB (bb)
6659 gimple_stmt_iterator gsi;
6661 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
6663 gimple phi = gsi_stmt (gsi);
6665 if (! virtual_operand_p (gimple_phi_result (phi)))
6666 find_func_aliases (phi);
6669 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
6671 gimple stmt = gsi_stmt (gsi);
6673 find_func_aliases (stmt);
6677 if (dump_file)
6679 fprintf (dump_file, "Points-to analysis\n\nConstraints:\n\n");
6680 dump_constraints (dump_file, 0);
6683 /* From the constraints compute the points-to sets. */
6684 solve_constraints ();
6686 /* Compute the points-to set for ESCAPED used for call-clobber analysis. */
6687 cfun->gimple_df->escaped = find_what_var_points_to (get_varinfo (escaped_id));
6689 /* Make sure the ESCAPED solution (which is used as placeholder in
6690 other solutions) does not reference itself. This simplifies
6691 points-to solution queries. */
6692 cfun->gimple_df->escaped.escaped = 0;
6694 /* Mark escaped HEAP variables as global. */
6695 FOR_EACH_VEC_ELT (varmap, i, vi)
6696 if (vi->is_heap_var
6697 && !vi->is_global_var)
6698 DECL_EXTERNAL (vi->decl) = vi->is_global_var
6699 = pt_solution_includes (&cfun->gimple_df->escaped, vi->decl);
6701 /* Compute the points-to sets for pointer SSA_NAMEs. */
6702 for (i = 0; i < num_ssa_names; ++i)
6704 tree ptr = ssa_name (i);
6705 if (ptr
6706 && POINTER_TYPE_P (TREE_TYPE (ptr)))
6707 find_what_p_points_to (ptr);
6710 /* Compute the call-used/clobbered sets. */
6711 FOR_EACH_BB (bb)
6713 gimple_stmt_iterator gsi;
6715 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
6717 gimple stmt = gsi_stmt (gsi);
6718 struct pt_solution *pt;
6719 if (!is_gimple_call (stmt))
6720 continue;
6722 pt = gimple_call_use_set (stmt);
6723 if (gimple_call_flags (stmt) & ECF_CONST)
6724 memset (pt, 0, sizeof (struct pt_solution));
6725 else if ((vi = lookup_call_use_vi (stmt)) != NULL)
6727 *pt = find_what_var_points_to (vi);
6728 /* Escaped (and thus nonlocal) variables are always
6729 implicitly used by calls. */
6730 /* ??? ESCAPED can be empty even though NONLOCAL
6731 always escaped. */
6732 pt->nonlocal = 1;
6733 pt->escaped = 1;
6735 else
6737 /* If there is nothing special about this call then
6738 we have made everything that is used also escape. */
6739 *pt = cfun->gimple_df->escaped;
6740 pt->nonlocal = 1;
6743 pt = gimple_call_clobber_set (stmt);
6744 if (gimple_call_flags (stmt) & (ECF_CONST|ECF_PURE|ECF_NOVOPS))
6745 memset (pt, 0, sizeof (struct pt_solution));
6746 else if ((vi = lookup_call_clobber_vi (stmt)) != NULL)
6748 *pt = find_what_var_points_to (vi);
6749 /* Escaped (and thus nonlocal) variables are always
6750 implicitly clobbered by calls. */
6751 /* ??? ESCAPED can be empty even though NONLOCAL
6752 always escaped. */
6753 pt->nonlocal = 1;
6754 pt->escaped = 1;
6756 else
6758 /* If there is nothing special about this call then
6759 we have made everything that is used also escape. */
6760 *pt = cfun->gimple_df->escaped;
6761 pt->nonlocal = 1;
6766 timevar_pop (TV_TREE_PTA);
6770 /* Delete created points-to sets. */
6772 static void
6773 delete_points_to_sets (void)
6775 unsigned int i;
6777 htab_delete (shared_bitmap_table);
6778 if (dump_file && (dump_flags & TDF_STATS))
6779 fprintf (dump_file, "Points to sets created:%d\n",
6780 stats.points_to_sets_created);
6782 pointer_map_destroy (vi_for_tree);
6783 pointer_map_destroy (call_stmt_vars);
6784 bitmap_obstack_release (&pta_obstack);
6785 constraints.release ();
6787 for (i = 0; i < graph->size; i++)
6788 graph->complex[i].release ();
6789 free (graph->complex);
6791 free (graph->rep);
6792 free (graph->succs);
6793 free (graph->pe);
6794 free (graph->pe_rep);
6795 free (graph->indirect_cycles);
6796 free (graph);
6798 varmap.release ();
6799 free_alloc_pool (variable_info_pool);
6800 free_alloc_pool (constraint_pool);
6802 obstack_free (&fake_var_decl_obstack, NULL);
6804 pointer_map_destroy (final_solutions);
6805 obstack_free (&final_solutions_obstack, NULL);
6809 /* Compute points-to information for every SSA_NAME pointer in the
6810 current function and compute the transitive closure of escaped
6811 variables to re-initialize the call-clobber states of local variables. */
6813 unsigned int
6814 compute_may_aliases (void)
6816 if (cfun->gimple_df->ipa_pta)
6818 if (dump_file)
6820 fprintf (dump_file, "\nNot re-computing points-to information "
6821 "because IPA points-to information is available.\n\n");
6823 /* But still dump what we have remaining it. */
6824 dump_alias_info (dump_file);
6827 return 0;
6830 /* For each pointer P_i, determine the sets of variables that P_i may
6831 point-to. Compute the reachability set of escaped and call-used
6832 variables. */
6833 compute_points_to_sets ();
6835 /* Debugging dumps. */
6836 if (dump_file)
6837 dump_alias_info (dump_file);
6839 /* Deallocate memory used by aliasing data structures and the internal
6840 points-to solution. */
6841 delete_points_to_sets ();
6843 gcc_assert (!need_ssa_update_p (cfun));
6845 return 0;
6848 static bool
6849 gate_tree_pta (void)
6851 return flag_tree_pta;
6854 /* A dummy pass to cause points-to information to be computed via
6855 TODO_rebuild_alias. */
6857 struct gimple_opt_pass pass_build_alias =
6860 GIMPLE_PASS,
6861 "alias", /* name */
6862 OPTGROUP_NONE, /* optinfo_flags */
6863 gate_tree_pta, /* gate */
6864 NULL, /* execute */
6865 NULL, /* sub */
6866 NULL, /* next */
6867 0, /* static_pass_number */
6868 TV_NONE, /* tv_id */
6869 PROP_cfg | PROP_ssa, /* properties_required */
6870 0, /* properties_provided */
6871 0, /* properties_destroyed */
6872 0, /* todo_flags_start */
6873 TODO_rebuild_alias /* todo_flags_finish */
6877 /* A dummy pass to cause points-to information to be computed via
6878 TODO_rebuild_alias. */
6880 struct gimple_opt_pass pass_build_ealias =
6883 GIMPLE_PASS,
6884 "ealias", /* name */
6885 OPTGROUP_NONE, /* optinfo_flags */
6886 gate_tree_pta, /* gate */
6887 NULL, /* execute */
6888 NULL, /* sub */
6889 NULL, /* next */
6890 0, /* static_pass_number */
6891 TV_NONE, /* tv_id */
6892 PROP_cfg | PROP_ssa, /* properties_required */
6893 0, /* properties_provided */
6894 0, /* properties_destroyed */
6895 0, /* todo_flags_start */
6896 TODO_rebuild_alias /* todo_flags_finish */
6901 /* Return true if we should execute IPA PTA. */
6902 static bool
6903 gate_ipa_pta (void)
6905 return (optimize
6906 && flag_ipa_pta
6907 /* Don't bother doing anything if the program has errors. */
6908 && !seen_error ());
6911 /* IPA PTA solutions for ESCAPED. */
6912 struct pt_solution ipa_escaped_pt
6913 = { true, false, false, false, false, false, NULL };
6915 /* Associate node with varinfo DATA. Worker for
6916 cgraph_for_node_and_aliases. */
6917 static bool
6918 associate_varinfo_to_alias (struct cgraph_node *node, void *data)
6920 if (node->alias || node->thunk.thunk_p)
6921 insert_vi_for_tree (node->symbol.decl, (varinfo_t)data);
6922 return false;
6925 /* Execute the driver for IPA PTA. */
6926 static unsigned int
6927 ipa_pta_execute (void)
6929 struct cgraph_node *node;
6930 struct varpool_node *var;
6931 int from;
6933 in_ipa_mode = 1;
6935 init_alias_vars ();
6937 if (dump_file && (dump_flags & TDF_DETAILS))
6939 dump_symtab (dump_file);
6940 fprintf (dump_file, "\n");
6943 /* Build the constraints. */
6944 FOR_EACH_DEFINED_FUNCTION (node)
6946 varinfo_t vi;
6947 /* Nodes without a body are not interesting. Especially do not
6948 visit clones at this point for now - we get duplicate decls
6949 there for inline clones at least. */
6950 if (!cgraph_function_with_gimple_body_p (node))
6951 continue;
6953 gcc_assert (!node->clone_of);
6955 vi = create_function_info_for (node->symbol.decl,
6956 alias_get_name (node->symbol.decl));
6957 cgraph_for_node_and_aliases (node, associate_varinfo_to_alias, vi, true);
6960 /* Create constraints for global variables and their initializers. */
6961 FOR_EACH_VARIABLE (var)
6963 if (var->alias)
6964 continue;
6966 get_vi_for_tree (var->symbol.decl);
6969 if (dump_file)
6971 fprintf (dump_file,
6972 "Generating constraints for global initializers\n\n");
6973 dump_constraints (dump_file, 0);
6974 fprintf (dump_file, "\n");
6976 from = constraints.length ();
6978 FOR_EACH_DEFINED_FUNCTION (node)
6980 struct function *func;
6981 basic_block bb;
6983 /* Nodes without a body are not interesting. */
6984 if (!cgraph_function_with_gimple_body_p (node))
6985 continue;
6987 if (dump_file)
6989 fprintf (dump_file,
6990 "Generating constraints for %s", cgraph_node_name (node));
6991 if (DECL_ASSEMBLER_NAME_SET_P (node->symbol.decl))
6992 fprintf (dump_file, " (%s)",
6993 IDENTIFIER_POINTER
6994 (DECL_ASSEMBLER_NAME (node->symbol.decl)));
6995 fprintf (dump_file, "\n");
6998 func = DECL_STRUCT_FUNCTION (node->symbol.decl);
6999 push_cfun (func);
7001 /* For externally visible or attribute used annotated functions use
7002 local constraints for their arguments.
7003 For local functions we see all callers and thus do not need initial
7004 constraints for parameters. */
7005 if (node->symbol.used_from_other_partition
7006 || node->symbol.externally_visible
7007 || node->symbol.force_output)
7009 intra_create_variable_infos ();
7011 /* We also need to make function return values escape. Nothing
7012 escapes by returning from main though. */
7013 if (!MAIN_NAME_P (DECL_NAME (node->symbol.decl)))
7015 varinfo_t fi, rvi;
7016 fi = lookup_vi_for_tree (node->symbol.decl);
7017 rvi = first_vi_for_offset (fi, fi_result);
7018 if (rvi && rvi->offset == fi_result)
7020 struct constraint_expr includes;
7021 struct constraint_expr var;
7022 includes.var = escaped_id;
7023 includes.offset = 0;
7024 includes.type = SCALAR;
7025 var.var = rvi->id;
7026 var.offset = 0;
7027 var.type = SCALAR;
7028 process_constraint (new_constraint (includes, var));
7033 /* Build constriants for the function body. */
7034 FOR_EACH_BB_FN (bb, func)
7036 gimple_stmt_iterator gsi;
7038 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
7039 gsi_next (&gsi))
7041 gimple phi = gsi_stmt (gsi);
7043 if (! virtual_operand_p (gimple_phi_result (phi)))
7044 find_func_aliases (phi);
7047 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
7049 gimple stmt = gsi_stmt (gsi);
7051 find_func_aliases (stmt);
7052 find_func_clobbers (stmt);
7056 pop_cfun ();
7058 if (dump_file)
7060 fprintf (dump_file, "\n");
7061 dump_constraints (dump_file, from);
7062 fprintf (dump_file, "\n");
7064 from = constraints.length ();
7067 /* From the constraints compute the points-to sets. */
7068 solve_constraints ();
7070 /* Compute the global points-to sets for ESCAPED.
7071 ??? Note that the computed escape set is not correct
7072 for the whole unit as we fail to consider graph edges to
7073 externally visible functions. */
7074 ipa_escaped_pt = find_what_var_points_to (get_varinfo (escaped_id));
7076 /* Make sure the ESCAPED solution (which is used as placeholder in
7077 other solutions) does not reference itself. This simplifies
7078 points-to solution queries. */
7079 ipa_escaped_pt.ipa_escaped = 0;
7081 /* Assign the points-to sets to the SSA names in the unit. */
7082 FOR_EACH_DEFINED_FUNCTION (node)
7084 tree ptr;
7085 struct function *fn;
7086 unsigned i;
7087 varinfo_t fi;
7088 basic_block bb;
7089 struct pt_solution uses, clobbers;
7090 struct cgraph_edge *e;
7092 /* Nodes without a body are not interesting. */
7093 if (!cgraph_function_with_gimple_body_p (node))
7094 continue;
7096 fn = DECL_STRUCT_FUNCTION (node->symbol.decl);
7098 /* Compute the points-to sets for pointer SSA_NAMEs. */
7099 FOR_EACH_VEC_ELT (*fn->gimple_df->ssa_names, i, ptr)
7101 if (ptr
7102 && POINTER_TYPE_P (TREE_TYPE (ptr)))
7103 find_what_p_points_to (ptr);
7106 /* Compute the call-use and call-clobber sets for all direct calls. */
7107 fi = lookup_vi_for_tree (node->symbol.decl);
7108 gcc_assert (fi->is_fn_info);
7109 clobbers
7110 = find_what_var_points_to (first_vi_for_offset (fi, fi_clobbers));
7111 uses = find_what_var_points_to (first_vi_for_offset (fi, fi_uses));
7112 for (e = node->callers; e; e = e->next_caller)
7114 if (!e->call_stmt)
7115 continue;
7117 *gimple_call_clobber_set (e->call_stmt) = clobbers;
7118 *gimple_call_use_set (e->call_stmt) = uses;
7121 /* Compute the call-use and call-clobber sets for indirect calls
7122 and calls to external functions. */
7123 FOR_EACH_BB_FN (bb, fn)
7125 gimple_stmt_iterator gsi;
7127 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
7129 gimple stmt = gsi_stmt (gsi);
7130 struct pt_solution *pt;
7131 varinfo_t vi;
7132 tree decl;
7134 if (!is_gimple_call (stmt))
7135 continue;
7137 /* Handle direct calls to external functions. */
7138 decl = gimple_call_fndecl (stmt);
7139 if (decl
7140 && (!(fi = lookup_vi_for_tree (decl))
7141 || !fi->is_fn_info))
7143 pt = gimple_call_use_set (stmt);
7144 if (gimple_call_flags (stmt) & ECF_CONST)
7145 memset (pt, 0, sizeof (struct pt_solution));
7146 else if ((vi = lookup_call_use_vi (stmt)) != NULL)
7148 *pt = find_what_var_points_to (vi);
7149 /* Escaped (and thus nonlocal) variables are always
7150 implicitly used by calls. */
7151 /* ??? ESCAPED can be empty even though NONLOCAL
7152 always escaped. */
7153 pt->nonlocal = 1;
7154 pt->ipa_escaped = 1;
7156 else
7158 /* If there is nothing special about this call then
7159 we have made everything that is used also escape. */
7160 *pt = ipa_escaped_pt;
7161 pt->nonlocal = 1;
7164 pt = gimple_call_clobber_set (stmt);
7165 if (gimple_call_flags (stmt) & (ECF_CONST|ECF_PURE|ECF_NOVOPS))
7166 memset (pt, 0, sizeof (struct pt_solution));
7167 else if ((vi = lookup_call_clobber_vi (stmt)) != NULL)
7169 *pt = find_what_var_points_to (vi);
7170 /* Escaped (and thus nonlocal) variables are always
7171 implicitly clobbered by calls. */
7172 /* ??? ESCAPED can be empty even though NONLOCAL
7173 always escaped. */
7174 pt->nonlocal = 1;
7175 pt->ipa_escaped = 1;
7177 else
7179 /* If there is nothing special about this call then
7180 we have made everything that is used also escape. */
7181 *pt = ipa_escaped_pt;
7182 pt->nonlocal = 1;
7186 /* Handle indirect calls. */
7187 if (!decl
7188 && (fi = get_fi_for_callee (stmt)))
7190 /* We need to accumulate all clobbers/uses of all possible
7191 callees. */
7192 fi = get_varinfo (find (fi->id));
7193 /* If we cannot constrain the set of functions we'll end up
7194 calling we end up using/clobbering everything. */
7195 if (bitmap_bit_p (fi->solution, anything_id)
7196 || bitmap_bit_p (fi->solution, nonlocal_id)
7197 || bitmap_bit_p (fi->solution, escaped_id))
7199 pt_solution_reset (gimple_call_clobber_set (stmt));
7200 pt_solution_reset (gimple_call_use_set (stmt));
7202 else
7204 bitmap_iterator bi;
7205 unsigned i;
7206 struct pt_solution *uses, *clobbers;
7208 uses = gimple_call_use_set (stmt);
7209 clobbers = gimple_call_clobber_set (stmt);
7210 memset (uses, 0, sizeof (struct pt_solution));
7211 memset (clobbers, 0, sizeof (struct pt_solution));
7212 EXECUTE_IF_SET_IN_BITMAP (fi->solution, 0, i, bi)
7214 struct pt_solution sol;
7216 vi = get_varinfo (i);
7217 if (!vi->is_fn_info)
7219 /* ??? We could be more precise here? */
7220 uses->nonlocal = 1;
7221 uses->ipa_escaped = 1;
7222 clobbers->nonlocal = 1;
7223 clobbers->ipa_escaped = 1;
7224 continue;
7227 if (!uses->anything)
7229 sol = find_what_var_points_to
7230 (first_vi_for_offset (vi, fi_uses));
7231 pt_solution_ior_into (uses, &sol);
7233 if (!clobbers->anything)
7235 sol = find_what_var_points_to
7236 (first_vi_for_offset (vi, fi_clobbers));
7237 pt_solution_ior_into (clobbers, &sol);
7245 fn->gimple_df->ipa_pta = true;
7248 delete_points_to_sets ();
7250 in_ipa_mode = 0;
7252 return 0;
7255 struct simple_ipa_opt_pass pass_ipa_pta =
7258 SIMPLE_IPA_PASS,
7259 "pta", /* name */
7260 OPTGROUP_NONE, /* optinfo_flags */
7261 gate_ipa_pta, /* gate */
7262 ipa_pta_execute, /* execute */
7263 NULL, /* sub */
7264 NULL, /* next */
7265 0, /* static_pass_number */
7266 TV_IPA_PTA, /* tv_id */
7267 0, /* properties_required */
7268 0, /* properties_provided */
7269 0, /* properties_destroyed */
7270 0, /* todo_flags_start */
7271 TODO_update_ssa /* todo_flags_finish */